Fix some of the winbind tests.
[samba.git] / source4 / torture / winbind / struct_based.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB torture tester - winbind struct based protocol
4    Copyright (C) Stefan Metzmacher 2007
5    Copyright (C) Michael Adam 2007
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "pstring.h"
23 #include "torture/torture.h"
24 #include "torture/winbind/proto.h"
25 #include "nsswitch/winbind_client.h"
26 #include "libcli/security/security.h"
27 #include "librpc/gen_ndr/netlogon.h"
28 #include "param/param.h"
29 #include "auth/pam_errors.h"
30
31 #define DO_STRUCT_REQ_REP_EXT(op,req,rep,expected,strict,warnaction,cmt) do { \
32         NSS_STATUS __got, __expected = (expected); \
33         __got = winbindd_request_response(op, req, rep); \
34         if (__got != __expected) { \
35                 const char *__cmt = (cmt); \
36                 if (strict) { \
37                         torture_result(torture, TORTURE_FAIL, \
38                                 __location__ ": " __STRING(op) \
39                                 " returned %d, expected %d%s%s", \
40                                 __got, __expected, \
41                                 (__cmt) ? ": " : "", \
42                                 (__cmt) ? (__cmt) : ""); \
43                         return false; \
44                 } else { \
45                         torture_warning(torture, \
46                                 __location__ ": " __STRING(op) \
47                                 " returned %d, expected %d%s%s", \
48                                 __got, __expected, \
49                                 (__cmt) ? ": " : "", \
50                                 (__cmt) ? (__cmt) : ""); \
51                         warnaction; \
52                 } \
53         } \
54 } while(0)
55
56 #define DO_STRUCT_REQ_REP(op,req,rep) do { \
57         bool __noop = false; \
58         DO_STRUCT_REQ_REP_EXT(op,req,rep,NSS_STATUS_SUCCESS,true,__noop=true,NULL); \
59 } while (0)
60
61 static bool torture_winbind_struct_interface_version(struct torture_context *torture)
62 {
63         struct winbindd_request req;
64         struct winbindd_response rep;
65
66         ZERO_STRUCT(req);
67         ZERO_STRUCT(rep);
68
69         torture_comment(torture, "Running WINBINDD_INTERFACE_VERSION (struct based)\n");
70
71         DO_STRUCT_REQ_REP(WINBINDD_INTERFACE_VERSION, &req, &rep);
72
73         torture_assert_int_equal(torture,
74                                  rep.data.interface_version,
75                                  WINBIND_INTERFACE_VERSION,
76                                  "winbind server and client doesn't match");
77
78         return true;
79 }
80
81 static bool torture_winbind_struct_ping(struct torture_context *torture)
82 {
83         struct timeval tv = timeval_current();
84         int timelimit = torture_setting_int(torture, "timelimit", 5);
85         uint32_t total = 0;
86
87         torture_comment(torture,
88                         "Running WINBINDD_PING (struct based) for %d seconds\n",
89                         timelimit);
90
91         while (timeval_elapsed(&tv) < timelimit) {
92                 DO_STRUCT_REQ_REP(WINBINDD_PING, NULL, NULL);
93                 total++;
94         }
95
96         torture_comment(torture,
97                         "%u (%.1f/s) WINBINDD_PING (struct based)\n",
98                         total, total / timeval_elapsed(&tv));
99
100         return true;
101 }
102
103
104 static char winbind_separator(struct torture_context *torture)
105 {
106         struct winbindd_response rep;
107
108         ZERO_STRUCT(rep);
109
110         DO_STRUCT_REQ_REP(WINBINDD_INFO, NULL, &rep);
111
112         return rep.data.info.winbind_separator;
113 }
114
115 static bool torture_winbind_struct_info(struct torture_context *torture)
116 {
117         struct winbindd_response rep;
118         const char *separator;
119
120         ZERO_STRUCT(rep);
121
122         torture_comment(torture, "Running WINBINDD_INFO (struct based)\n");
123
124         DO_STRUCT_REQ_REP(WINBINDD_INFO, NULL, &rep);
125
126         separator = torture_setting_string(torture,
127                                            "winbindd separator",
128                                            lp_winbind_separator(torture->lp_ctx));
129         torture_assert_int_equal(torture,
130                                  rep.data.info.winbind_separator,
131                                  *separator,
132                                  "winbind separator doesn't match");
133
134         torture_comment(torture, "Samba Version '%s'\n",
135                         rep.data.info.samba_version);
136
137         return true;
138 }
139
140 static bool torture_winbind_struct_priv_pipe_dir(struct torture_context *torture)
141 {
142         struct winbindd_response rep;
143         const char *default_dir;
144         const char *expected_dir;
145         const char *got_dir;
146
147         ZERO_STRUCT(rep);
148
149         torture_comment(torture, "Running WINBINDD_PRIV_PIPE_DIR (struct based)\n");
150
151         DO_STRUCT_REQ_REP(WINBINDD_PRIV_PIPE_DIR, NULL, &rep);
152
153         got_dir = (const char *)rep.extra_data.data;
154
155         torture_assert(torture, got_dir, "NULL WINBINDD_PRIV_PIPE_DIR\n");
156
157         default_dir = lock_path(torture, torture->lp_ctx, 
158                                 WINBINDD_PRIV_SOCKET_SUBDIR);
159         expected_dir = torture_setting_string(torture,
160                                               "winbindd private pipe dir",
161                                               default_dir);
162
163         torture_assert_str_equal(torture, got_dir, expected_dir,
164                                  "WINBINDD_PRIV_PIPE_DIR doesn't match");
165
166         SAFE_FREE(rep.extra_data.data);
167         return true;
168 }
169
170 static bool torture_winbind_struct_netbios_name(struct torture_context *torture)
171 {
172         struct winbindd_response rep;
173         const char *expected;
174
175         ZERO_STRUCT(rep);
176
177         torture_comment(torture, "Running WINBINDD_NETBIOS_NAME (struct based)\n");
178
179         DO_STRUCT_REQ_REP(WINBINDD_NETBIOS_NAME, NULL, &rep);
180
181         expected = torture_setting_string(torture,
182                                           "winbindd netbios name",
183                                           lp_netbios_name(torture->lp_ctx));
184         expected = strupper_talloc(torture, expected);
185         
186         torture_assert_str_equal(torture,
187                                  rep.data.netbios_name, expected,
188                                  "winbindd's netbios name doesn't match");
189
190         return true;
191 }
192
193 static bool get_winbind_domain(struct torture_context *torture, char **domain)
194 {
195         struct winbindd_response rep;
196
197         ZERO_STRUCT(rep);
198
199         DO_STRUCT_REQ_REP(WINBINDD_DOMAIN_NAME, NULL, &rep);
200
201         *domain = talloc_strdup(torture, rep.data.domain_name);
202         torture_assert(torture, domain, "talloc error");
203
204         return true;
205 }
206
207 static bool torture_winbind_struct_domain_name(struct torture_context *torture)
208 {
209         const char *expected;
210         char *domain;
211
212         torture_comment(torture, "Running WINBINDD_DOMAIN_NAME (struct based)\n");
213
214         expected = torture_setting_string(torture,
215                                           "winbindd netbios domain",
216                                           lp_workgroup(torture->lp_ctx));
217
218         get_winbind_domain(torture, &domain);
219
220         torture_assert_str_equal(torture, domain, expected,
221                                  "winbindd's netbios domain doesn't match");
222
223         return true;
224 }
225
226 static bool torture_winbind_struct_check_machacc(struct torture_context *torture)
227 {
228         bool ok;
229         bool strict = torture_setting_bool(torture, "strict mode", false);
230         struct winbindd_response rep;
231
232         ZERO_STRUCT(rep);
233
234         torture_comment(torture, "Running WINBINDD_CHECK_MACHACC (struct based)\n");
235
236         ok = true;
237         DO_STRUCT_REQ_REP_EXT(WINBINDD_CHECK_MACHACC, NULL, &rep,
238                               NSS_STATUS_SUCCESS, strict, ok = false,
239                               "WINBINDD_CHECK_MACHACC");
240
241         if (!ok) {
242                 torture_assert(torture,
243                                strlen(rep.data.auth.nt_status_string)>0,
244                                "Failed with empty nt_status_string");
245
246                 torture_warning(torture,"%s:%s:%s:%d\n",
247                                 nt_errstr(NT_STATUS(rep.data.auth.nt_status)),
248                                 rep.data.auth.nt_status_string,
249                                 rep.data.auth.error_string,
250                                 rep.data.auth.pam_error);
251                 return true;
252         }
253
254         torture_assert_ntstatus_ok(torture,
255                                    NT_STATUS(rep.data.auth.nt_status),
256                                    "WINBINDD_CHECK_MACHACC ok: nt_status");
257
258         torture_assert_str_equal(torture,
259                                  rep.data.auth.nt_status_string,
260                                  nt_errstr(NT_STATUS_OK),
261                                  "WINBINDD_CHECK_MACHACC ok:nt_status_string");
262
263         torture_assert_str_equal(torture,
264                                  rep.data.auth.error_string,
265                                  nt_errstr(NT_STATUS_OK),
266                                  "WINBINDD_CHECK_MACHACC ok: error_string");
267
268         torture_assert_int_equal(torture,
269                                  rep.data.auth.pam_error,
270                                  nt_status_to_pam(NT_STATUS_OK),
271                                  "WINBINDD_CHECK_MACHACC ok: pam_error");
272
273         return true;
274 }
275
276 struct torture_trust_domain {
277         const char *netbios_name;
278         const char *dns_name;
279         struct dom_sid *sid;
280 };
281
282 static bool get_trusted_domains(struct torture_context *torture,
283                                 struct torture_trust_domain **_d)
284 {
285         struct winbindd_request req;
286         struct winbindd_response rep;
287         struct torture_trust_domain *d = NULL;
288         uint32_t dcount = 0;
289         fstring line;
290         const char *extra_data;
291
292         ZERO_STRUCT(req);
293         ZERO_STRUCT(rep);
294
295         DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
296
297         extra_data = (char *)rep.extra_data.data;
298         torture_assert(torture, extra_data, "NULL trust list");
299
300         while (next_token(&extra_data, line, "\n", sizeof(fstring))) {
301                 char *p, *lp;
302
303                 d = talloc_realloc(torture, d,
304                                    struct torture_trust_domain,
305                                    dcount + 2);
306                 ZERO_STRUCT(d[dcount+1]);
307
308                 lp = line;
309                 p = strchr(lp, '\\');
310                 torture_assert(torture, p, "missing 1st '\\' in line");
311                 *p = 0;
312                 d[dcount].netbios_name = talloc_strdup(d, lp);
313                 torture_assert(torture, strlen(d[dcount].netbios_name) > 0,
314                                "empty netbios_name");
315
316                 lp = p+1;
317                 p = strchr(lp, '\\');
318                 torture_assert(torture, p, "missing 2nd '\\' in line");
319                 *p = 0;
320                 d[dcount].dns_name = talloc_strdup(d, lp);
321                 /* it's ok to have an empty dns_name */
322
323                 lp = p+1;
324                 d[dcount].sid = dom_sid_parse_talloc(d, lp);
325                 torture_assert(torture, d[dcount].sid,
326                                "failed to parse sid");
327
328                 dcount++;
329         }
330         SAFE_FREE(rep.extra_data.data);
331
332         torture_assert(torture, dcount >= 2,
333                        "The list of trusted domain should contain 2 entries");
334
335         *_d = d;
336         return true;
337 }
338
339 static bool torture_winbind_struct_list_trustdom(struct torture_context *torture)
340 {
341         struct winbindd_request req;
342         struct winbindd_response rep;
343         char *list1;
344         char *list2;
345         bool ok;
346         struct torture_trust_domain *listd = NULL;
347         uint32_t i;
348
349         torture_comment(torture, "Running WINBINDD_LIST_TRUSTDOM (struct based)\n");
350
351         ZERO_STRUCT(req);
352         ZERO_STRUCT(rep);
353
354         req.data.list_all_domains = false;
355
356         DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
357
358         list1 = (char *)rep.extra_data.data;
359         torture_assert(torture, list1, "NULL trust list");
360
361         torture_comment(torture, "%s\n", list1);
362
363         ZERO_STRUCT(req);
364         ZERO_STRUCT(rep);
365
366         req.data.list_all_domains = true;
367
368         DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
369
370         list2 = (char *)rep.extra_data.data;
371         torture_assert(torture, list2, "NULL trust list");
372
373         /*
374          * The list_all_domains parameter should be ignored
375          */
376         torture_assert_str_equal(torture, list2, list1, "list_all_domains not ignored");
377
378         SAFE_FREE(list1);
379         SAFE_FREE(list2);
380
381         ok = get_trusted_domains(torture, &listd);
382         torture_assert(torture, ok, "failed to get trust list");
383
384         for (i=0; listd[i].netbios_name; i++) {
385                 if (i == 0) {
386                         struct dom_sid *builtin_sid;
387
388                         builtin_sid = dom_sid_parse_talloc(torture, SID_BUILTIN);
389
390                         torture_assert_str_equal(torture,
391                                                  listd[i].netbios_name,
392                                                  NAME_BUILTIN,
393                                                  "first domain should be 'BUILTIN'");
394
395                         torture_assert_str_equal(torture,
396                                                  listd[i].dns_name,
397                                                  "",
398                                                  "BUILTIN domain should not have a dns name");
399
400                         ok = dom_sid_equal(builtin_sid,
401                                            listd[i].sid);
402                         torture_assert(torture, ok, "BUILTIN domain should have S-1-5-32");
403
404                         continue;
405                 }
406
407                 /*
408                  * TODO: verify the content of the 2nd and 3rd (in member server mode)
409                  *       domain entries
410                  */
411         }
412
413         return true;
414 }
415
416 static bool torture_winbind_struct_domain_info(struct torture_context *torture)
417 {
418         bool ok;
419         struct torture_trust_domain *listd = NULL;
420         uint32_t i;
421
422         torture_comment(torture, "Running WINBINDD_DOMAIN_INFO (struct based)\n");
423
424         ok = get_trusted_domains(torture, &listd);
425         torture_assert(torture, ok, "failed to get trust list");
426
427         for (i=0; listd[i].netbios_name; i++) {
428                 struct winbindd_request req;
429                 struct winbindd_response rep;
430                 struct dom_sid *sid;
431                 char *flagstr = talloc_strdup(torture," ");
432
433                 ZERO_STRUCT(req);
434                 ZERO_STRUCT(rep);
435
436                 fstrcpy(req.domain_name, listd[i].netbios_name);
437
438                 DO_STRUCT_REQ_REP(WINBINDD_DOMAIN_INFO, &req, &rep);
439
440                 torture_assert_str_equal(torture,
441                                          rep.data.domain_info.name,
442                                          listd[i].netbios_name,
443                                          "Netbios domain name doesn't match");
444
445                 torture_assert_str_equal(torture,
446                                          rep.data.domain_info.alt_name,
447                                          listd[i].dns_name,
448                                          "DNS domain name doesn't match");
449
450                 sid = dom_sid_parse_talloc(torture, rep.data.domain_info.sid);
451                 torture_assert(torture, sid, "Failed to parse SID");
452
453                 ok = dom_sid_equal(listd[i].sid, sid);
454                 torture_assert(torture, ok, "SID's doesn't match");
455
456                 if (rep.data.domain_info.primary) {
457                         flagstr = talloc_strdup_append(flagstr, "PR ");
458                 }
459
460                 if (rep.data.domain_info.active_directory) {
461                         torture_assert(torture,
462                                        strlen(rep.data.domain_info.alt_name)>0,
463                                        "Active Directory without DNS name");
464                         flagstr = talloc_strdup_append(flagstr, "AD ");
465                 }
466
467                 if (rep.data.domain_info.native_mode) {
468                         torture_assert(torture,
469                                        rep.data.domain_info.active_directory,
470                                        "Native-Mode, but no Active Directory");
471                         flagstr = talloc_strdup_append(flagstr, "NA ");
472                 }
473
474                 torture_comment(torture, "DOMAIN '%s' => '%s' [%s]\n",
475                                 rep.data.domain_info.name,
476                                 rep.data.domain_info.alt_name,
477                                 flagstr);
478         }
479
480         return true;
481 }
482
483 static bool torture_winbind_struct_getdcname(struct torture_context *torture)
484 {
485         bool ok;
486         bool strict = torture_setting_bool(torture, "strict mode", false);
487         struct torture_trust_domain *listd = NULL;
488         uint32_t i;
489
490         torture_comment(torture, "Running WINBINDD_GETDCNAME (struct based)\n");
491
492         ok = get_trusted_domains(torture, &listd);
493         torture_assert(torture, ok, "failed to get trust list");
494
495         for (i=0; listd[i].netbios_name; i++) {
496                 struct winbindd_request req;
497                 struct winbindd_response rep;
498
499                 ZERO_STRUCT(req);
500                 ZERO_STRUCT(rep);
501
502                 fstrcpy(req.domain_name, listd[i].netbios_name);
503
504                 ok = true;
505                 DO_STRUCT_REQ_REP_EXT(WINBINDD_GETDCNAME, &req, &rep,
506                                       NSS_STATUS_SUCCESS,
507                                       (i <2 || strict), ok = false,
508                                       talloc_asprintf(torture, "DOMAIN '%s'",
509                                                       req.domain_name));
510                 if (!ok) continue;
511
512                 /* TODO: check rep.data.dc_name; */
513                 torture_comment(torture, "DOMAIN '%s' => DCNAME '%s'\n",
514                                 req.domain_name, rep.data.dc_name);
515         }
516
517         return true;
518 }
519
520 static bool torture_winbind_struct_dsgetdcname(struct torture_context *torture)
521 {
522         bool ok;
523         bool strict = torture_setting_bool(torture, "strict mode", false);
524         struct torture_trust_domain *listd = NULL;
525         uint32_t i;
526         uint32_t count = 0;
527
528         torture_comment(torture, "Running WINBINDD_DSGETDCNAME (struct based)\n");
529
530         ok = get_trusted_domains(torture, &listd);
531         torture_assert(torture, ok, "failed to get trust list");
532
533         for (i=0; listd[i].netbios_name; i++) {
534                 struct winbindd_request req;
535                 struct winbindd_response rep;
536
537                 ZERO_STRUCT(req);
538                 ZERO_STRUCT(rep);
539
540                 if (strlen(listd[i].dns_name) == 0) continue;
541
542                 /*
543                  * TODO: remove this and let winbindd give no dns name
544                  *       for NT4 domains
545                  */
546                 if (strcmp(listd[i].dns_name, listd[i].netbios_name) == 0) {
547                         continue;
548                 }
549
550                 fstrcpy(req.domain_name, listd[i].dns_name);
551
552                 /* TODO: test more flag combinations */
553                 req.flags = DS_DIRECTORY_SERVICE_REQUIRED;
554
555                 ok = true;
556                 DO_STRUCT_REQ_REP_EXT(WINBINDD_DSGETDCNAME, &req, &rep,
557                                       NSS_STATUS_SUCCESS,
558                                       strict, ok = false,
559                                       talloc_asprintf(torture, "DOMAIN '%s'",
560                                                       req.domain_name));
561                 if (!ok) continue;
562
563                 /* TODO: check rep.data.dc_name; */
564                 torture_comment(torture, "DOMAIN '%s' => DCNAME '%s'\n",
565                                 req.domain_name, rep.data.dc_name);
566
567                 count++;
568         }
569
570         if (count == 0) {
571                 torture_warning(torture, "WINBINDD_DSGETDCNAME"
572                                 " was not tested with %d non-AD domains",
573                                 i);
574         }
575
576         if (strict) {
577                 torture_assert(torture, count > 0,
578                                "WiNBINDD_DSGETDCNAME was not tested");
579         }
580
581         return true;
582 }
583
584 static bool get_user_list(struct torture_context *torture, char ***users)
585 {
586         struct winbindd_request req;
587         struct winbindd_response rep;
588         char **u = NULL;
589         uint32_t count;
590         fstring name;
591         const char *extra_data;
592
593         ZERO_STRUCT(req);
594         ZERO_STRUCT(rep);
595
596         DO_STRUCT_REQ_REP(WINBINDD_LIST_USERS, &req, &rep);
597
598         extra_data = (char *)rep.extra_data.data;
599         torture_assert(torture, extra_data, "NULL extra data");
600
601         for(count = 0;
602             next_token(&extra_data, name, ",", sizeof(fstring));
603             count++)
604         {
605                 u = talloc_realloc(torture, u, char *, count + 2);
606                 u[count+1] = NULL;
607                 u[count] = talloc_strdup(u, name);
608         }
609
610         SAFE_FREE(rep.extra_data.data);
611
612         *users = u;
613         return true;
614 }
615
616 static bool torture_winbind_struct_list_users(struct torture_context *torture)
617 {
618         char **users;
619         uint32_t count;
620         bool ok;
621
622         torture_comment(torture, "Running WINBINDD_LIST_USERS (struct based)\n");
623
624         ok = get_user_list(torture, &users);
625         torture_assert(torture, ok, "failed to get group list");
626
627         for (count = 0; users[count]; count++) { }
628
629         torture_comment(torture, "got %d users\n", count);
630
631         return true;
632 }
633
634 static bool get_group_list(struct torture_context *torture, char ***groups)
635 {
636         struct winbindd_request req;
637         struct winbindd_response rep;
638         char **g = NULL;
639         uint32_t count;
640         fstring name;
641         const char *extra_data;
642
643         ZERO_STRUCT(req);
644         ZERO_STRUCT(rep);
645
646         DO_STRUCT_REQ_REP(WINBINDD_LIST_GROUPS, &req, &rep);
647
648         extra_data = (char *)rep.extra_data.data;
649         torture_assert(torture, extra_data, "NULL extra data");
650
651         for(count = 0;
652             next_token(&extra_data, name, ",", sizeof(fstring));
653             count++)
654         {
655                 g = talloc_realloc(torture, g, char *, count + 2);
656                 g[count+1] = NULL;
657                 g[count] = talloc_strdup(g, name);
658         }
659
660         SAFE_FREE(rep.extra_data.data);
661
662         *groups = g;
663         return true;
664 }
665
666 static bool torture_winbind_struct_list_groups(struct torture_context *torture)
667 {
668         char **groups;
669         uint32_t count;
670         bool ok;
671
672         torture_comment(torture, "Running WINBINDD_LIST_GROUPS (struct based)\n");
673
674         ok = get_group_list(torture, &groups);
675         torture_assert(torture, ok, "failed to get group list");
676
677         for (count = 0; groups[count]; count++) { }
678
679         torture_comment(torture, "got %d groups\n", count);
680
681         return true;
682 }
683
684 struct torture_domain_sequence {
685         const char *netbios_name;
686         uint32_t seq;
687 };
688
689 static bool get_sequence_numbers(struct torture_context *torture,
690                                  struct torture_domain_sequence **seqs)
691 {
692         struct winbindd_request req;
693         struct winbindd_response rep;
694         const char *extra_data;
695         fstring line;
696         uint32_t count = 0;
697         struct torture_domain_sequence *s = NULL;
698
699         ZERO_STRUCT(req);
700         ZERO_STRUCT(rep);
701
702         DO_STRUCT_REQ_REP(WINBINDD_SHOW_SEQUENCE, &req, &rep);
703
704         extra_data = (char *)rep.extra_data.data;
705         torture_assert(torture, extra_data, "NULL sequence list");
706
707         while (next_token(&extra_data, line, "\n", sizeof(fstring))) {
708                 char *p, *lp;
709                 uint32_t seq;
710
711                 s = talloc_realloc(torture, s, struct torture_domain_sequence,
712                                    count + 2);
713                 ZERO_STRUCT(s[count+1]);
714
715                 lp = line;
716                 p = strchr(lp, ' ');
717                 torture_assert(torture, p, "invalid line format");
718                 *p = 0;
719                 s[count].netbios_name = talloc_strdup(s, lp);
720
721                 lp = p+1;
722                 torture_assert(torture, strncmp(lp, ": ", 2) == 0,
723                                "invalid line format");
724                 lp += 2;
725                 if (strcmp(lp, "DISCONNECTED") == 0) {
726                         seq = (uint32_t)-1;
727                 } else {
728                         seq = (uint32_t)strtol(lp, &p, 10);
729                         torture_assert(torture, (*p == '\0'),
730                                        "invalid line format");
731                         torture_assert(torture, (seq != (uint32_t)-1),
732                                        "sequence number -1 encountered");
733                 }
734                 s[count].seq = seq;
735
736                 count++;
737         }
738         SAFE_FREE(rep.extra_data.data);
739
740         torture_assert(torture, count >= 2, "The list of domain sequence "
741                        "numbers should contain 2 entries");
742
743         *seqs = s;
744         return true;
745 }
746
747 static bool torture_winbind_struct_show_sequence(struct torture_context *torture)
748 {
749         bool ok;
750         uint32_t i;
751         struct torture_trust_domain *domlist = NULL;
752         struct torture_domain_sequence *s = NULL;
753
754         torture_comment(torture, "Running WINBINDD_SHOW_SEQUENCE (struct based)\n");
755
756         ok = get_sequence_numbers(torture, &s);
757         torture_assert(torture, ok, "failed to get list of sequence numbers");
758
759         ok = get_trusted_domains(torture, &domlist);
760         torture_assert(torture, ok, "failed to get trust list");
761
762         for (i=0; domlist[i].netbios_name; i++) {
763                 struct winbindd_request req;
764                 struct winbindd_response rep;
765                 uint32_t seq;
766
767                 torture_assert(torture, s[i].netbios_name,
768                                "more domains recieved in second run");
769                 torture_assert_str_equal(torture, domlist[i].netbios_name,
770                                          s[i].netbios_name,
771                                          "inconsistent order of domain lists");
772
773                 ZERO_STRUCT(req);
774                 ZERO_STRUCT(rep);
775                 fstrcpy(req.domain_name, domlist[i].netbios_name);
776
777                 DO_STRUCT_REQ_REP(WINBINDD_SHOW_SEQUENCE, &req, &rep);
778
779                 seq = rep.data.sequence_number;
780
781                 if (i == 0) {
782                         torture_assert(torture, (seq != (uint32_t)-1),
783                                        "BUILTIN domain disconnected");
784                 } else if (i == 1) {
785                         torture_assert(torture, (seq != (uint32_t)-1),
786                                        "local domain disconnected");
787                 }
788
789
790                 if (seq == (uint32_t)-1) {
791                         torture_comment(torture, " * %s : DISCONNECTED\n",
792                                         req.domain_name);
793                 } else {
794                         torture_comment(torture, " * %s : %d\n",
795                                         req.domain_name, seq);
796                 }
797                 torture_assert(torture, (seq >= s[i].seq),
798                                "illegal sequence number encountered");
799         }
800
801         return true;
802 }
803
804 static bool torture_winbind_struct_setpwent(struct torture_context *torture)
805 {
806         struct winbindd_request req;
807         struct winbindd_response rep;
808
809         torture_comment(torture, "Running WINBINDD_SETPWENT (struct based)\n");
810
811         ZERO_STRUCT(req);
812         ZERO_STRUCT(rep);
813
814         DO_STRUCT_REQ_REP(WINBINDD_SETPWENT, &req, &rep);
815
816         return true;
817 }
818
819 static bool torture_winbind_struct_getpwent(struct torture_context *torture)
820 {
821         struct winbindd_request req;
822         struct winbindd_response rep;
823         struct winbindd_pw *pwent;
824
825         torture_comment(torture, "Running WINBINDD_GETPWENT (struct based)\n");
826
827         torture_comment(torture, " - Running WINBINDD_SETPWENT first\n");
828         ZERO_STRUCT(req);
829         ZERO_STRUCT(rep);
830         DO_STRUCT_REQ_REP(WINBINDD_SETPWENT, &req, &rep);
831
832         torture_comment(torture, " - Running WINBINDD_GETPWENT now\n");
833         ZERO_STRUCT(req);
834         ZERO_STRUCT(rep);
835         req.data.num_entries = 1;
836         DO_STRUCT_REQ_REP(WINBINDD_GETPWENT, &req, &rep);
837         pwent = (struct winbindd_pw *)rep.extra_data.data;
838         torture_assert(torture, (pwent != NULL), "NULL pwent");
839         torture_comment(torture, "name: %s, uid: %d, gid: %d, shell: %s\n",
840                         pwent->pw_name, pwent->pw_uid, pwent->pw_gid,
841                         pwent->pw_shell);
842
843         return true;
844 }
845
846 static bool torture_winbind_struct_endpwent(struct torture_context *torture)
847 {
848         struct winbindd_request req;
849         struct winbindd_response rep;
850
851         torture_comment(torture, "Running WINBINDD_ENDPWENT (struct based)\n");
852
853         ZERO_STRUCT(req);
854         ZERO_STRUCT(rep);
855
856         DO_STRUCT_REQ_REP(WINBINDD_ENDPWENT, &req, &rep);
857
858         return true;
859 }
860
861 /* Copy of parse_domain_user from winbindd_util.c.  Parse a string of the
862    form DOMAIN/user into a domain and a user */
863
864 static bool parse_domain_user(struct torture_context *torture,
865                               const char *domuser, fstring domain,
866                               fstring user)
867 {
868         char *p = strchr(domuser, winbind_separator(torture));
869         char *dom;
870
871         if (!p) {
872                 /* Maybe it was a UPN? */
873                 if ((p = strchr(domuser, '@')) != NULL) {
874                         fstrcpy(domain, "");
875                         fstrcpy(user, domuser);
876                         return true;
877                 }
878
879                 fstrcpy(user, domuser);
880                 get_winbind_domain(torture, &dom);
881                 fstrcpy(domain, dom);
882                 return true;
883         }
884
885         fstrcpy(user, p+1);
886         fstrcpy(domain, domuser);
887         domain[PTR_DIFF(p, domuser)] = 0;
888         strupper_m(domain);
889
890         return true;
891 }
892
893 static bool lookup_name_sid_list(struct torture_context *torture, char **list)
894 {
895         uint32_t count;
896
897         for (count = 0; list[count]; count++) {
898                 struct winbindd_request req;
899                 struct winbindd_response rep;
900                 char *sid;
901                 char *name;
902
903                 ZERO_STRUCT(req);
904                 ZERO_STRUCT(rep);
905
906                 parse_domain_user(torture, list[count], req.data.name.dom_name,
907                                   req.data.name.name);
908
909                 DO_STRUCT_REQ_REP(WINBINDD_LOOKUPNAME, &req, &rep);
910
911                 sid = talloc_strdup(torture, rep.data.sid.sid);
912
913                 ZERO_STRUCT(req);
914                 ZERO_STRUCT(rep);
915
916                 fstrcpy(req.data.sid, sid);
917
918                 DO_STRUCT_REQ_REP(WINBINDD_LOOKUPSID, &req, &rep);
919
920                 name = talloc_asprintf(torture, "%s%c%s",
921                                        rep.data.name.dom_name,
922                                        winbind_separator(torture),
923                                        rep.data.name.name);
924
925                 torture_assert_casestr_equal(torture, list[count], name,
926                                          "LOOKUP_SID after LOOKUP_NAME != id");
927
928 #if 0
929                 torture_comment(torture, " %s -> %s -> %s\n", list[count],
930                                 sid, name);
931 #endif
932
933                 talloc_free(sid);
934                 talloc_free(name);
935         }
936
937         return true;
938 }
939
940 static bool name_is_in_list(const char *name, const char **list)
941 {
942         uint32_t count;
943
944         for (count = 0; list[count]; count++) {
945                 if (strequal(name, list[count])) {
946                         return true;
947                 }
948         }
949         return false;
950 }
951
952 static bool torture_winbind_struct_lookup_name_sid(struct torture_context *torture)
953 {
954         struct winbindd_request req;
955         struct winbindd_response rep;
956         const char *invalid_sid = "S-0-0-7";
957         char *domain;
958         const char *invalid_user = "noone";
959         char *invalid_name;
960         bool strict = torture_setting_bool(torture, "strict mode", false);
961         char **users;
962         char **groups;
963         uint32_t count;
964         bool ok;
965
966         torture_comment(torture, "Running WINBINDD_LOOKUP_NAME_SID (struct based)\n");
967
968         ok = get_user_list(torture, &users);
969         torture_assert(torture, ok, "failed to retrieve list of users");
970         lookup_name_sid_list(torture, users);
971
972         ok = get_group_list(torture, &groups);
973         torture_assert(torture, ok, "failed to retrieve list of groups");
974         lookup_name_sid_list(torture, groups);
975
976         ZERO_STRUCT(req);
977         ZERO_STRUCT(rep);
978
979         fstrcpy(req.data.sid, invalid_sid);
980
981         ok = true;
982         DO_STRUCT_REQ_REP_EXT(WINBINDD_LOOKUPSID, &req, &rep,
983                               NSS_STATUS_NOTFOUND,
984                               strict,
985                               ok=false,
986                               talloc_asprintf(torture,
987                                               "invalid sid %s was resolved",
988                                               invalid_sid));
989
990         ZERO_STRUCT(req);
991         ZERO_STRUCT(rep);
992
993         /* try to find an invalid name... */
994
995         count = 0;
996         get_winbind_domain(torture, &domain);
997         do {
998                 count++;
999                 invalid_name = talloc_asprintf(torture, "%s\\%s%u",
1000                                                domain,
1001                                                invalid_user, count);
1002         } while(name_is_in_list(invalid_name, (const char **)users) ||
1003                 name_is_in_list(invalid_name, (const char **)groups));
1004
1005         fstrcpy(req.data.name.dom_name, domain);
1006         fstrcpy(req.data.name.name,
1007                 talloc_asprintf(torture, "%s%u", invalid_user,
1008                                 count));
1009
1010         ok = true;
1011         DO_STRUCT_REQ_REP_EXT(WINBINDD_LOOKUPNAME, &req, &rep,
1012                               NSS_STATUS_NOTFOUND,
1013                               strict,
1014                               ok=false,
1015                               talloc_asprintf(torture,
1016                                               "invalid name %s was resolved",
1017                                               invalid_name));
1018
1019         talloc_free(users);
1020         talloc_free(groups);
1021
1022         return true;
1023 }
1024
1025 struct torture_suite *torture_winbind_struct_init(void)
1026 {
1027         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "STRUCT");
1028
1029         torture_suite_add_simple_test(suite, "INTERFACE_VERSION", torture_winbind_struct_interface_version);
1030         torture_suite_add_simple_test(suite, "PING", torture_winbind_struct_ping);
1031         torture_suite_add_simple_test(suite, "INFO", torture_winbind_struct_info);
1032         torture_suite_add_simple_test(suite, "PRIV_PIPE_DIR", torture_winbind_struct_priv_pipe_dir);
1033         torture_suite_add_simple_test(suite, "NETBIOS_NAME", torture_winbind_struct_netbios_name);
1034         torture_suite_add_simple_test(suite, "DOMAIN_NAME", torture_winbind_struct_domain_name);
1035         torture_suite_add_simple_test(suite, "CHECK_MACHACC", torture_winbind_struct_check_machacc);
1036         torture_suite_add_simple_test(suite, "LIST_TRUSTDOM", torture_winbind_struct_list_trustdom);
1037         torture_suite_add_simple_test(suite, "DOMAIN_INFO", torture_winbind_struct_domain_info);
1038         torture_suite_add_simple_test(suite, "GETDCNAME", torture_winbind_struct_getdcname);
1039         torture_suite_add_simple_test(suite, "DSGETDCNAME", torture_winbind_struct_dsgetdcname);
1040         torture_suite_add_simple_test(suite, "LIST_USERS", torture_winbind_struct_list_users);
1041         torture_suite_add_simple_test(suite, "LIST_GROUPS", torture_winbind_struct_list_groups);
1042         torture_suite_add_simple_test(suite, "SHOW_SEQUENCE", torture_winbind_struct_show_sequence);
1043         torture_suite_add_simple_test(suite, "SETPWENT", torture_winbind_struct_setpwent);
1044         torture_suite_add_simple_test(suite, "GETPWENT", torture_winbind_struct_getpwent);
1045         torture_suite_add_simple_test(suite, "ENDPWENT", torture_winbind_struct_endpwent);
1046         torture_suite_add_simple_test(suite, "LOOKUP_NAME_SID", torture_winbind_struct_lookup_name_sid);
1047
1048         suite->description = talloc_strdup(suite, "WINBIND - struct based protocol tests");
1049
1050         return suite;
1051 }