s3 selftest: Fix WINBINDD_LIST_GROUPS test
[Samba/gebeck_regimport.git] / source4 / torture / winbind / struct_based.c
blobd15bd6b8070be38c7db8f2bd7adb6e16a3963dca
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
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.
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.
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/>.
21 #include "includes.h"
22 #include "torture/torture.h"
23 #include "nsswitch/winbind_client.h"
24 #include "libcli/security/security.h"
25 #include "librpc/gen_ndr/netlogon.h"
26 #include "param/param.h"
27 #include "auth/ntlm/pam_errors.h"
29 #define DO_STRUCT_REQ_REP_EXT(op,req,rep,expected,strict,warnaction,cmt) do { \
30 NSS_STATUS __got, __expected = (expected); \
31 __got = winbindd_request_response(op, req, rep); \
32 if (__got != __expected) { \
33 const char *__cmt = (cmt); \
34 if (strict) { \
35 torture_result(torture, TORTURE_FAIL, \
36 __location__ ": " __STRING(op) \
37 " returned %d, expected %d%s%s", \
38 __got, __expected, \
39 (__cmt) ? ": " : "", \
40 (__cmt) ? (__cmt) : ""); \
41 return false; \
42 } else { \
43 torture_warning(torture, \
44 __location__ ": " __STRING(op) \
45 " returned %d, expected %d%s%s", \
46 __got, __expected, \
47 (__cmt) ? ": " : "", \
48 (__cmt) ? (__cmt) : ""); \
49 warnaction; \
50 } \
51 } \
52 } while(0)
54 #define DO_STRUCT_REQ_REP(op,req,rep) do { \
55 bool __noop = false; \
56 DO_STRUCT_REQ_REP_EXT(op,req,rep,NSS_STATUS_SUCCESS,true,__noop=true,NULL); \
57 } while (0)
59 static bool torture_winbind_struct_interface_version(struct torture_context *torture)
61 struct winbindd_request req;
62 struct winbindd_response rep;
64 ZERO_STRUCT(req);
65 ZERO_STRUCT(rep);
67 torture_comment(torture, "Running WINBINDD_INTERFACE_VERSION (struct based)\n");
69 DO_STRUCT_REQ_REP(WINBINDD_INTERFACE_VERSION, &req, &rep);
71 torture_assert_int_equal(torture,
72 rep.data.interface_version,
73 WINBIND_INTERFACE_VERSION,
74 "winbind server and client doesn't match");
76 return true;
79 static bool torture_winbind_struct_ping(struct torture_context *torture)
81 struct timeval tv = timeval_current();
82 int timelimit = torture_setting_int(torture, "timelimit", 5);
83 uint32_t total = 0;
85 torture_comment(torture,
86 "Running WINBINDD_PING (struct based) for %d seconds\n",
87 timelimit);
89 while (timeval_elapsed(&tv) < timelimit) {
90 DO_STRUCT_REQ_REP(WINBINDD_PING, NULL, NULL);
91 total++;
94 torture_comment(torture,
95 "%u (%.1f/s) WINBINDD_PING (struct based)\n",
96 total, total / timeval_elapsed(&tv));
98 return true;
102 static char winbind_separator(struct torture_context *torture)
104 struct winbindd_response rep;
106 ZERO_STRUCT(rep);
108 DO_STRUCT_REQ_REP(WINBINDD_INFO, NULL, &rep);
110 return rep.data.info.winbind_separator;
113 static bool torture_winbind_struct_info(struct torture_context *torture)
115 struct winbindd_response rep;
116 const char *separator;
118 ZERO_STRUCT(rep);
120 torture_comment(torture, "Running WINBINDD_INFO (struct based)\n");
122 DO_STRUCT_REQ_REP(WINBINDD_INFO, NULL, &rep);
124 separator = torture_setting_string(torture,
125 "winbindd separator",
126 lp_winbind_separator(torture->lp_ctx));
127 torture_assert_int_equal(torture,
128 rep.data.info.winbind_separator,
129 *separator,
130 "winbind separator doesn't match");
132 torture_comment(torture, "Samba Version '%s'\n",
133 rep.data.info.samba_version);
135 return true;
138 static bool torture_winbind_struct_priv_pipe_dir(struct torture_context *torture)
140 struct winbindd_response rep;
141 const char *got_dir;
143 ZERO_STRUCT(rep);
145 torture_comment(torture, "Running WINBINDD_PRIV_PIPE_DIR (struct based)\n");
147 DO_STRUCT_REQ_REP(WINBINDD_PRIV_PIPE_DIR, NULL, &rep);
149 got_dir = (const char *)rep.extra_data.data;
151 torture_assert(torture, got_dir, "NULL WINBINDD_PRIV_PIPE_DIR\n");
153 SAFE_FREE(rep.extra_data.data);
154 return true;
157 static bool torture_winbind_struct_netbios_name(struct torture_context *torture)
159 struct winbindd_response rep;
160 const char *expected;
162 ZERO_STRUCT(rep);
164 torture_comment(torture, "Running WINBINDD_NETBIOS_NAME (struct based)\n");
166 DO_STRUCT_REQ_REP(WINBINDD_NETBIOS_NAME, NULL, &rep);
168 expected = torture_setting_string(torture,
169 "winbindd netbios name",
170 lp_netbios_name(torture->lp_ctx));
171 expected = strupper_talloc(torture, expected);
173 torture_assert_str_equal(torture,
174 rep.data.netbios_name, expected,
175 "winbindd's netbios name doesn't match");
177 return true;
180 static bool get_winbind_domain(struct torture_context *torture, char **domain)
182 struct winbindd_response rep;
184 ZERO_STRUCT(rep);
186 DO_STRUCT_REQ_REP(WINBINDD_DOMAIN_NAME, NULL, &rep);
188 *domain = talloc_strdup(torture, rep.data.domain_name);
189 torture_assert(torture, domain, "talloc error");
191 return true;
194 static bool torture_winbind_struct_domain_name(struct torture_context *torture)
196 const char *expected;
197 char *domain;
199 torture_comment(torture, "Running WINBINDD_DOMAIN_NAME (struct based)\n");
201 expected = torture_setting_string(torture,
202 "winbindd netbios domain",
203 lp_workgroup(torture->lp_ctx));
205 get_winbind_domain(torture, &domain);
207 torture_assert_str_equal(torture, domain, expected,
208 "winbindd's netbios domain doesn't match");
210 return true;
213 static bool torture_winbind_struct_check_machacc(struct torture_context *torture)
215 bool ok;
216 bool strict = torture_setting_bool(torture, "strict mode", false);
217 struct winbindd_response rep;
219 ZERO_STRUCT(rep);
221 torture_comment(torture, "Running WINBINDD_CHECK_MACHACC (struct based)\n");
223 ok = true;
224 DO_STRUCT_REQ_REP_EXT(WINBINDD_CHECK_MACHACC, NULL, &rep,
225 NSS_STATUS_SUCCESS, strict, ok = false,
226 "WINBINDD_CHECK_MACHACC");
228 if (!ok) {
229 torture_assert(torture,
230 strlen(rep.data.auth.nt_status_string)>0,
231 "Failed with empty nt_status_string");
233 torture_warning(torture,"%s:%s:%s:%d\n",
234 nt_errstr(NT_STATUS(rep.data.auth.nt_status)),
235 rep.data.auth.nt_status_string,
236 rep.data.auth.error_string,
237 rep.data.auth.pam_error);
238 return true;
241 torture_assert_ntstatus_ok(torture,
242 NT_STATUS(rep.data.auth.nt_status),
243 "WINBINDD_CHECK_MACHACC ok: nt_status");
245 torture_assert_str_equal(torture,
246 rep.data.auth.nt_status_string,
247 nt_errstr(NT_STATUS_OK),
248 "WINBINDD_CHECK_MACHACC ok:nt_status_string");
250 torture_assert_str_equal(torture,
251 rep.data.auth.error_string,
252 get_friendly_nt_error_msg(NT_STATUS_OK),
253 "WINBINDD_CHECK_MACHACC ok: error_string");
255 torture_assert_int_equal(torture,
256 rep.data.auth.pam_error,
257 nt_status_to_pam(NT_STATUS_OK),
258 "WINBINDD_CHECK_MACHACC ok: pam_error");
260 return true;
263 struct torture_trust_domain {
264 const char *netbios_name;
265 const char *dns_name;
266 struct dom_sid *sid;
269 static bool get_trusted_domains(struct torture_context *torture,
270 struct torture_trust_domain **_d)
272 struct winbindd_request req;
273 struct winbindd_response rep;
274 struct torture_trust_domain *d = NULL;
275 uint32_t dcount = 0;
276 char line[256];
277 const char *extra_data;
279 ZERO_STRUCT(req);
280 ZERO_STRUCT(rep);
282 DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
284 extra_data = (char *)rep.extra_data.data;
285 if (!extra_data) {
286 return true;
289 torture_assert(torture, extra_data, "NULL trust list");
291 while (next_token(&extra_data, line, "\n", sizeof(line))) {
292 char *p, *lp;
294 d = talloc_realloc(torture, d,
295 struct torture_trust_domain,
296 dcount + 2);
297 ZERO_STRUCT(d[dcount+1]);
299 lp = line;
300 p = strchr(lp, '\\');
301 torture_assert(torture, p, "missing 1st '\\' in line");
302 *p = 0;
303 d[dcount].netbios_name = talloc_strdup(d, lp);
304 torture_assert(torture, strlen(d[dcount].netbios_name) > 0,
305 "empty netbios_name");
307 lp = p+1;
308 p = strchr(lp, '\\');
309 torture_assert(torture, p, "missing 2nd '\\' in line");
310 *p = 0;
311 d[dcount].dns_name = talloc_strdup(d, lp);
312 /* it's ok to have an empty dns_name */
314 lp = p+1;
315 d[dcount].sid = dom_sid_parse_talloc(d, lp);
316 torture_assert(torture, d[dcount].sid,
317 "failed to parse sid");
319 dcount++;
321 SAFE_FREE(rep.extra_data.data);
323 torture_assert(torture, dcount >= 2,
324 "The list of trusted domain should contain 2 entries");
326 *_d = d;
327 return true;
330 static bool torture_winbind_struct_list_trustdom(struct torture_context *torture)
332 struct winbindd_request req;
333 struct winbindd_response rep;
334 char *list1;
335 char *list2;
336 bool ok;
337 struct torture_trust_domain *listd = NULL;
338 uint32_t i;
340 torture_comment(torture, "Running WINBINDD_LIST_TRUSTDOM (struct based)\n");
342 ZERO_STRUCT(req);
343 ZERO_STRUCT(rep);
345 req.data.list_all_domains = false;
347 DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
349 list1 = (char *)rep.extra_data.data;
351 torture_comment(torture, "%s\n", list1);
353 ZERO_STRUCT(req);
354 ZERO_STRUCT(rep);
356 req.data.list_all_domains = true;
358 DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
360 list2 = (char *)rep.extra_data.data;
363 * The list_all_domains parameter should be ignored
365 torture_assert_str_equal(torture, list2, list1, "list_all_domains not ignored");
367 SAFE_FREE(list1);
368 SAFE_FREE(list2);
370 ok = get_trusted_domains(torture, &listd);
371 torture_assert(torture, ok, "failed to get trust list");
373 for (i=0; listd && listd[i].netbios_name; i++) {
374 if (i == 0) {
375 struct dom_sid *builtin_sid;
377 builtin_sid = dom_sid_parse_talloc(torture, SID_BUILTIN);
379 torture_assert_str_equal(torture,
380 listd[i].netbios_name,
381 NAME_BUILTIN,
382 "first domain should be 'BUILTIN'");
384 torture_assert_str_equal(torture,
385 listd[i].dns_name,
387 "BUILTIN domain should not have a dns name");
389 ok = dom_sid_equal(builtin_sid,
390 listd[i].sid);
391 torture_assert(torture, ok, "BUILTIN domain should have S-1-5-32");
393 continue;
397 * TODO: verify the content of the 2nd and 3rd (in member server mode)
398 * domain entries
402 return true;
405 static bool torture_winbind_struct_domain_info(struct torture_context *torture)
407 bool ok;
408 struct torture_trust_domain *listd = NULL;
409 uint32_t i;
411 torture_comment(torture, "Running WINBINDD_DOMAIN_INFO (struct based)\n");
413 ok = get_trusted_domains(torture, &listd);
414 torture_assert(torture, ok, "failed to get trust list");
416 for (i=0; listd && listd[i].netbios_name; i++) {
417 struct winbindd_request req;
418 struct winbindd_response rep;
419 struct dom_sid *sid;
420 char *flagstr = talloc_strdup(torture," ");
422 ZERO_STRUCT(req);
423 ZERO_STRUCT(rep);
425 fstrcpy(req.domain_name, listd[i].netbios_name);
427 DO_STRUCT_REQ_REP(WINBINDD_DOMAIN_INFO, &req, &rep);
429 torture_assert_str_equal(torture,
430 rep.data.domain_info.name,
431 listd[i].netbios_name,
432 "Netbios domain name doesn't match");
434 torture_assert_str_equal(torture,
435 rep.data.domain_info.alt_name,
436 listd[i].dns_name,
437 "DNS domain name doesn't match");
439 sid = dom_sid_parse_talloc(torture, rep.data.domain_info.sid);
440 torture_assert(torture, sid, "Failed to parse SID");
442 ok = dom_sid_equal(listd[i].sid, sid);
443 torture_assert(torture, ok, "SID's doesn't match");
445 if (rep.data.domain_info.primary) {
446 flagstr = talloc_strdup_append(flagstr, "PR ");
449 if (rep.data.domain_info.active_directory) {
450 torture_assert(torture,
451 strlen(rep.data.domain_info.alt_name)>0,
452 "Active Directory without DNS name");
453 flagstr = talloc_strdup_append(flagstr, "AD ");
456 if (rep.data.domain_info.native_mode) {
457 torture_assert(torture,
458 rep.data.domain_info.active_directory,
459 "Native-Mode, but no Active Directory");
460 flagstr = talloc_strdup_append(flagstr, "NA ");
463 torture_comment(torture, "DOMAIN '%s' => '%s' [%s]\n",
464 rep.data.domain_info.name,
465 rep.data.domain_info.alt_name,
466 flagstr);
469 return true;
472 static bool torture_winbind_struct_getdcname(struct torture_context *torture)
474 bool ok;
475 bool strict = torture_setting_bool(torture, "strict mode", false);
476 const char *domain_name = torture_setting_string(torture,
477 "winbindd netbios domain",
478 lp_workgroup(torture->lp_ctx));
479 struct torture_trust_domain *listd = NULL;
480 uint32_t i, count = 0;
482 torture_comment(torture, "Running WINBINDD_GETDCNAME (struct based)\n");
484 ok = get_trusted_domains(torture, &listd);
485 torture_assert(torture, ok, "failed to get trust list");
487 for (i=0; listd && listd[i].netbios_name; i++) {
488 struct winbindd_request req;
489 struct winbindd_response rep;
491 /* getdcname is not expected to work on "BUILTIN" or our own
492 * domain */
493 if (strequal(listd[i].netbios_name, "BUILTIN") ||
494 strequal(listd[i].netbios_name, domain_name)) {
495 continue;
498 ZERO_STRUCT(req);
499 ZERO_STRUCT(rep);
501 fstrcpy(req.domain_name, listd[i].netbios_name);
503 ok = true;
504 DO_STRUCT_REQ_REP_EXT(WINBINDD_GETDCNAME, &req, &rep,
505 NSS_STATUS_SUCCESS,
506 (i <2 || strict), ok = false,
507 talloc_asprintf(torture, "DOMAIN '%s'",
508 req.domain_name));
509 if (!ok) continue;
511 /* TODO: check rep.data.dc_name; */
512 torture_comment(torture, "DOMAIN '%s' => DCNAME '%s'\n",
513 req.domain_name, rep.data.dc_name);
514 count++;
517 if (strict) {
518 torture_assert(torture, count > 0,
519 "WiNBINDD_GETDCNAME was not tested");
521 return true;
524 static bool torture_winbind_struct_dsgetdcname(struct torture_context *torture)
526 bool ok;
527 bool strict = torture_setting_bool(torture, "strict mode", false);
528 struct torture_trust_domain *listd = NULL;
529 uint32_t i;
530 uint32_t count = 0;
532 torture_comment(torture, "Running WINBINDD_DSGETDCNAME (struct based)\n");
534 ok = get_trusted_domains(torture, &listd);
535 torture_assert(torture, ok, "failed to get trust list");
537 for (i=0; listd && listd[i].netbios_name; i++) {
538 struct winbindd_request req;
539 struct winbindd_response rep;
541 ZERO_STRUCT(req);
542 ZERO_STRUCT(rep);
544 if (strlen(listd[i].dns_name) == 0) continue;
547 * TODO: remove this and let winbindd give no dns name
548 * for NT4 domains
550 if (strcmp(listd[i].dns_name, listd[i].netbios_name) == 0) {
551 continue;
554 fstrcpy(req.domain_name, listd[i].dns_name);
556 /* TODO: test more flag combinations */
557 req.flags = DS_DIRECTORY_SERVICE_REQUIRED;
559 ok = true;
560 DO_STRUCT_REQ_REP_EXT(WINBINDD_DSGETDCNAME, &req, &rep,
561 NSS_STATUS_SUCCESS,
562 strict, ok = false,
563 talloc_asprintf(torture, "DOMAIN '%s'",
564 req.domain_name));
565 if (!ok) continue;
567 /* TODO: check rep.data.dc_name; */
568 torture_comment(torture, "DOMAIN '%s' => DCNAME '%s'\n",
569 req.domain_name, rep.data.dc_name);
571 count++;
574 if (count == 0) {
575 torture_warning(torture, "WINBINDD_DSGETDCNAME"
576 " was not tested with %d non-AD domains",
580 if (strict) {
581 torture_assert(torture, count > 0,
582 "WiNBINDD_DSGETDCNAME was not tested");
585 return true;
588 static bool get_user_list(struct torture_context *torture, char ***users)
590 struct winbindd_request req;
591 struct winbindd_response rep;
592 char **u = NULL;
593 uint32_t count;
594 char name[256];
595 const char *extra_data;
597 ZERO_STRUCT(req);
598 ZERO_STRUCT(rep);
600 DO_STRUCT_REQ_REP(WINBINDD_LIST_USERS, &req, &rep);
602 extra_data = (char *)rep.extra_data.data;
603 torture_assert(torture, extra_data, "NULL extra data");
605 for(count = 0;
606 next_token(&extra_data, name, ",", sizeof(name));
607 count++)
609 u = talloc_realloc(torture, u, char *, count + 2);
610 u[count+1] = NULL;
611 u[count] = talloc_strdup(u, name);
614 SAFE_FREE(rep.extra_data.data);
616 *users = u;
617 return true;
620 static bool torture_winbind_struct_list_users(struct torture_context *torture)
622 char **users;
623 uint32_t count;
624 bool ok;
626 torture_comment(torture, "Running WINBINDD_LIST_USERS (struct based)\n");
628 ok = get_user_list(torture, &users);
629 torture_assert(torture, ok, "failed to get user list");
631 for (count = 0; users[count]; count++) { }
633 torture_comment(torture, "got %d users\n", count);
635 return true;
638 static bool get_group_list(struct torture_context *torture, int *num_entries,
639 char ***groups)
641 struct winbindd_request req;
642 struct winbindd_response rep;
643 char **g = NULL;
644 uint32_t count;
645 char name[256];
646 const char *extra_data;
648 ZERO_STRUCT(req);
649 ZERO_STRUCT(rep);
651 DO_STRUCT_REQ_REP(WINBINDD_LIST_GROUPS, &req, &rep);
652 extra_data = (char *)rep.extra_data.data;
654 *num_entries = rep.data.num_entries;
656 if (*num_entries == 0) {
657 torture_assert(torture, extra_data == NULL,
658 "extra data is null for >0 reported entries\n");
659 *groups = NULL;
660 return true;
663 torture_assert(torture, extra_data, "NULL extra data");
665 for(count = 0;
666 next_token(&extra_data, name, ",", sizeof(name));
667 count++)
669 g = talloc_realloc(torture, g, char *, count + 2);
670 g[count+1] = NULL;
671 g[count] = talloc_strdup(g, name);
674 SAFE_FREE(rep.extra_data.data);
676 torture_assert_int_equal(torture, *num_entries, count,
677 "Wrong number of group entries reported.");
679 *groups = g;
680 return true;
683 static bool torture_winbind_struct_list_groups(struct torture_context *torture)
685 char **groups;
686 uint32_t count;
687 bool ok;
689 torture_comment(torture, "Running WINBINDD_LIST_GROUPS (struct based)\n");
691 ok = get_group_list(torture, &count, &groups);
692 torture_assert(torture, ok, "failed to get group list");
694 torture_comment(torture, "got %d groups\n", count);
696 return true;
699 struct torture_domain_sequence {
700 const char *netbios_name;
701 uint32_t seq;
704 static bool get_sequence_numbers(struct torture_context *torture,
705 struct torture_domain_sequence **seqs)
707 struct winbindd_request req;
708 struct winbindd_response rep;
709 const char *extra_data;
710 char line[256];
711 uint32_t count = 0;
712 struct torture_domain_sequence *s = NULL;
714 ZERO_STRUCT(req);
715 ZERO_STRUCT(rep);
717 DO_STRUCT_REQ_REP(WINBINDD_SHOW_SEQUENCE, &req, &rep);
719 extra_data = (char *)rep.extra_data.data;
720 torture_assert(torture, extra_data, "NULL sequence list");
722 while (next_token(&extra_data, line, "\n", sizeof(line))) {
723 char *p, *lp;
724 uint32_t seq;
726 s = talloc_realloc(torture, s, struct torture_domain_sequence,
727 count + 2);
728 ZERO_STRUCT(s[count+1]);
730 lp = line;
731 p = strchr(lp, ' ');
732 torture_assert(torture, p, "invalid line format");
733 *p = 0;
734 s[count].netbios_name = talloc_strdup(s, lp);
736 lp = p+1;
737 torture_assert(torture, strncmp(lp, ": ", 2) == 0,
738 "invalid line format");
739 lp += 2;
740 if (strcmp(lp, "DISCONNECTED") == 0) {
741 seq = (uint32_t)-1;
742 } else {
743 seq = (uint32_t)strtol(lp, &p, 10);
744 torture_assert(torture, (*p == '\0'),
745 "invalid line format");
746 torture_assert(torture, (seq != (uint32_t)-1),
747 "sequence number -1 encountered");
749 s[count].seq = seq;
751 count++;
753 SAFE_FREE(rep.extra_data.data);
755 torture_assert(torture, count >= 2, "The list of domain sequence "
756 "numbers should contain 2 entries");
758 *seqs = s;
759 return true;
762 static bool torture_winbind_struct_show_sequence(struct torture_context *torture)
764 bool ok;
765 uint32_t i;
766 struct torture_trust_domain *domlist = NULL;
767 struct torture_domain_sequence *s = NULL;
769 torture_comment(torture, "Running WINBINDD_SHOW_SEQUENCE (struct based)\n");
771 ok = get_sequence_numbers(torture, &s);
772 torture_assert(torture, ok, "failed to get list of sequence numbers");
774 ok = get_trusted_domains(torture, &domlist);
775 torture_assert(torture, ok, "failed to get trust list");
777 for (i=0; domlist[i].netbios_name; i++) {
778 struct winbindd_request req;
779 struct winbindd_response rep;
780 uint32_t seq;
782 torture_assert(torture, s[i].netbios_name,
783 "more domains recieved in second run");
784 torture_assert_str_equal(torture, domlist[i].netbios_name,
785 s[i].netbios_name,
786 "inconsistent order of domain lists");
788 ZERO_STRUCT(req);
789 ZERO_STRUCT(rep);
790 fstrcpy(req.domain_name, domlist[i].netbios_name);
792 DO_STRUCT_REQ_REP(WINBINDD_SHOW_SEQUENCE, &req, &rep);
794 seq = rep.data.sequence_number;
796 if (i == 0) {
797 torture_assert(torture, (seq != (uint32_t)-1),
798 "BUILTIN domain disconnected");
799 } else if (i == 1) {
800 torture_assert(torture, (seq != (uint32_t)-1),
801 "local domain disconnected");
805 if (seq == (uint32_t)-1) {
806 torture_comment(torture, " * %s : DISCONNECTED\n",
807 req.domain_name);
808 } else {
809 torture_comment(torture, " * %s : %d\n",
810 req.domain_name, seq);
812 torture_assert(torture, (seq >= s[i].seq),
813 "illegal sequence number encountered");
816 return true;
819 static bool torture_winbind_struct_setpwent(struct torture_context *torture)
821 struct winbindd_request req;
822 struct winbindd_response rep;
824 torture_comment(torture, "Running WINBINDD_SETPWENT (struct based)\n");
826 ZERO_STRUCT(req);
827 ZERO_STRUCT(rep);
829 DO_STRUCT_REQ_REP(WINBINDD_SETPWENT, &req, &rep);
831 return true;
834 static bool torture_winbind_struct_getpwent(struct torture_context *torture)
836 struct winbindd_request req;
837 struct winbindd_response rep;
838 struct winbindd_pw *pwent;
840 torture_comment(torture, "Running WINBINDD_GETPWENT (struct based)\n");
842 torture_comment(torture, " - Running WINBINDD_SETPWENT first\n");
843 ZERO_STRUCT(req);
844 ZERO_STRUCT(rep);
845 DO_STRUCT_REQ_REP(WINBINDD_SETPWENT, &req, &rep);
847 torture_comment(torture, " - Running WINBINDD_GETPWENT now\n");
848 ZERO_STRUCT(req);
849 ZERO_STRUCT(rep);
850 req.data.num_entries = 1;
851 DO_STRUCT_REQ_REP(WINBINDD_GETPWENT, &req, &rep);
852 pwent = (struct winbindd_pw *)rep.extra_data.data;
853 torture_assert(torture, (pwent != NULL), "NULL pwent");
854 torture_comment(torture, "name: %s, uid: %d, gid: %d, shell: %s\n",
855 pwent->pw_name, pwent->pw_uid, pwent->pw_gid,
856 pwent->pw_shell);
858 return true;
861 static bool torture_winbind_struct_endpwent(struct torture_context *torture)
863 struct winbindd_request req;
864 struct winbindd_response rep;
866 torture_comment(torture, "Running WINBINDD_ENDPWENT (struct based)\n");
868 ZERO_STRUCT(req);
869 ZERO_STRUCT(rep);
871 DO_STRUCT_REQ_REP(WINBINDD_ENDPWENT, &req, &rep);
873 return true;
876 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
877 form DOMAIN/user into a domain and a user */
879 static bool parse_domain_user(struct torture_context *torture,
880 const char *domuser, fstring domain,
881 fstring user)
883 char *p = strchr(domuser, winbind_separator(torture));
884 char *dom;
886 if (!p) {
887 /* Maybe it was a UPN? */
888 if ((p = strchr(domuser, '@')) != NULL) {
889 fstrcpy(domain, "");
890 fstrcpy(user, domuser);
891 return true;
894 fstrcpy(user, domuser);
895 get_winbind_domain(torture, &dom);
896 fstrcpy(domain, dom);
897 return true;
900 fstrcpy(user, p+1);
901 fstrcpy(domain, domuser);
902 domain[PTR_DIFF(p, domuser)] = 0;
903 strupper_m(domain);
905 return true;
908 static bool lookup_name_sid_list(struct torture_context *torture, char **list)
910 uint32_t count;
912 for (count = 0; list[count]; count++) {
913 struct winbindd_request req;
914 struct winbindd_response rep;
915 char *sid;
916 char *name;
918 ZERO_STRUCT(req);
919 ZERO_STRUCT(rep);
921 parse_domain_user(torture, list[count], req.data.name.dom_name,
922 req.data.name.name);
924 DO_STRUCT_REQ_REP(WINBINDD_LOOKUPNAME, &req, &rep);
926 sid = talloc_strdup(torture, rep.data.sid.sid);
928 ZERO_STRUCT(req);
929 ZERO_STRUCT(rep);
931 fstrcpy(req.data.sid, sid);
933 DO_STRUCT_REQ_REP(WINBINDD_LOOKUPSID, &req, &rep);
935 name = talloc_asprintf(torture, "%s%c%s",
936 rep.data.name.dom_name,
937 winbind_separator(torture),
938 rep.data.name.name);
940 torture_assert_casestr_equal(torture, list[count], name,
941 "LOOKUP_SID after LOOKUP_NAME != id");
943 #if 0
944 torture_comment(torture, " %s -> %s -> %s\n", list[count],
945 sid, name);
946 #endif
948 talloc_free(sid);
949 talloc_free(name);
952 return true;
955 static bool name_is_in_list(const char *name, const char **list)
957 uint32_t count;
959 for (count = 0; list && list[count]; count++) {
960 if (strequal(name, list[count])) {
961 return true;
964 return false;
967 static bool torture_winbind_struct_lookup_name_sid(struct torture_context *torture)
969 struct winbindd_request req;
970 struct winbindd_response rep;
971 const char *invalid_sid = "S-0-0-7";
972 char *domain;
973 const char *invalid_user = "noone";
974 char *invalid_name;
975 bool strict = torture_setting_bool(torture, "strict mode", false);
976 char **users;
977 char **groups;
978 uint32_t count, num_groups;
979 bool ok;
981 torture_comment(torture, "Running WINBINDD_LOOKUP_NAME_SID (struct based)\n");
983 ok = get_user_list(torture, &users);
984 torture_assert(torture, ok, "failed to retrieve list of users");
985 lookup_name_sid_list(torture, users);
987 ok = get_group_list(torture, &num_groups, &groups);
988 torture_assert(torture, ok, "failed to retrieve list of groups");
989 if (num_groups > 0) {
990 lookup_name_sid_list(torture, groups);
993 ZERO_STRUCT(req);
994 ZERO_STRUCT(rep);
996 fstrcpy(req.data.sid, invalid_sid);
998 ok = true;
999 DO_STRUCT_REQ_REP_EXT(WINBINDD_LOOKUPSID, &req, &rep,
1000 NSS_STATUS_NOTFOUND,
1001 strict,
1002 ok=false,
1003 talloc_asprintf(torture,
1004 "invalid sid %s was resolved",
1005 invalid_sid));
1007 ZERO_STRUCT(req);
1008 ZERO_STRUCT(rep);
1010 /* try to find an invalid name... */
1012 count = 0;
1013 get_winbind_domain(torture, &domain);
1014 do {
1015 count++;
1016 invalid_name = talloc_asprintf(torture, "%s\\%s%u",
1017 domain,
1018 invalid_user, count);
1019 } while(name_is_in_list(invalid_name, (const char **)users) ||
1020 name_is_in_list(invalid_name, (const char **)groups));
1022 fstrcpy(req.data.name.dom_name, domain);
1023 fstrcpy(req.data.name.name,
1024 talloc_asprintf(torture, "%s%u", invalid_user,
1025 count));
1027 ok = true;
1028 DO_STRUCT_REQ_REP_EXT(WINBINDD_LOOKUPNAME, &req, &rep,
1029 NSS_STATUS_NOTFOUND,
1030 strict,
1031 ok=false,
1032 talloc_asprintf(torture,
1033 "invalid name %s was resolved",
1034 invalid_name));
1036 talloc_free(users);
1037 talloc_free(groups);
1039 return true;
1042 struct torture_suite *torture_winbind_struct_init(void)
1044 struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "STRUCT");
1046 torture_suite_add_simple_test(suite, "INTERFACE_VERSION", torture_winbind_struct_interface_version);
1047 torture_suite_add_simple_test(suite, "PING", torture_winbind_struct_ping);
1048 torture_suite_add_simple_test(suite, "INFO", torture_winbind_struct_info);
1049 torture_suite_add_simple_test(suite, "PRIV_PIPE_DIR", torture_winbind_struct_priv_pipe_dir);
1050 torture_suite_add_simple_test(suite, "NETBIOS_NAME", torture_winbind_struct_netbios_name);
1051 torture_suite_add_simple_test(suite, "DOMAIN_NAME", torture_winbind_struct_domain_name);
1052 torture_suite_add_simple_test(suite, "CHECK_MACHACC", torture_winbind_struct_check_machacc);
1053 torture_suite_add_simple_test(suite, "LIST_TRUSTDOM", torture_winbind_struct_list_trustdom);
1054 torture_suite_add_simple_test(suite, "DOMAIN_INFO", torture_winbind_struct_domain_info);
1055 torture_suite_add_simple_test(suite, "GETDCNAME", torture_winbind_struct_getdcname);
1056 torture_suite_add_simple_test(suite, "DSGETDCNAME", torture_winbind_struct_dsgetdcname);
1057 torture_suite_add_simple_test(suite, "LIST_USERS", torture_winbind_struct_list_users);
1058 torture_suite_add_simple_test(suite, "LIST_GROUPS", torture_winbind_struct_list_groups);
1059 torture_suite_add_simple_test(suite, "SHOW_SEQUENCE", torture_winbind_struct_show_sequence);
1060 torture_suite_add_simple_test(suite, "SETPWENT", torture_winbind_struct_setpwent);
1061 torture_suite_add_simple_test(suite, "GETPWENT", torture_winbind_struct_getpwent);
1062 torture_suite_add_simple_test(suite, "ENDPWENT", torture_winbind_struct_endpwent);
1063 torture_suite_add_simple_test(suite, "LOOKUP_NAME_SID", torture_winbind_struct_lookup_name_sid);
1065 suite->description = talloc_strdup(suite, "WINBIND - struct based protocol tests");
1067 return suite;