s4:torture/winbind: avoid compiler warnings
[Samba.git] / source4 / torture / winbind / struct_based.c
blob76e448cd79f38f5ea53bc76cbad078ed943d2131
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 "../libcli/auth/pam_errors.h"
28 #include "torture/winbind/proto.h"
30 #define DO_STRUCT_REQ_REP_EXT(op,req,rep,expected,strict,warnaction,cmt) do { \
31 NSS_STATUS __got, __expected = (expected); \
32 __got = winbindd_request_response(NULL, op, req, rep); \
33 if (__got != __expected) { \
34 const char *__cmt = (cmt); \
35 if (strict) { \
36 torture_result(torture, TORTURE_FAIL, \
37 __location__ ": " __STRING(op) \
38 " returned %d, expected %d%s%s", \
39 __got, __expected, \
40 (__cmt) ? ": " : "", \
41 (__cmt) ? (__cmt) : ""); \
42 return false; \
43 } else { \
44 torture_warning(torture, \
45 __location__ ": " __STRING(op) \
46 " returned %d, expected %d%s%s", \
47 __got, __expected, \
48 (__cmt) ? ": " : "", \
49 (__cmt) ? (__cmt) : ""); \
50 warnaction; \
51 } \
52 } \
53 } while(0)
55 #undef _STRUCT_NOOP
56 #define _STRUCT_NOOP do {} while(0);
57 #define DO_STRUCT_REQ_REP(op,req,rep) do { \
58 DO_STRUCT_REQ_REP_EXT(op,req,rep,NSS_STATUS_SUCCESS,true, _STRUCT_NOOP, NULL); \
59 } while (0)
61 static bool torture_winbind_struct_interface_version(struct torture_context *torture)
63 struct winbindd_request req;
64 struct winbindd_response rep;
66 ZERO_STRUCT(req);
67 ZERO_STRUCT(rep);
69 torture_comment(torture, "Running WINBINDD_INTERFACE_VERSION (struct based)\n");
71 DO_STRUCT_REQ_REP(WINBINDD_INTERFACE_VERSION, &req, &rep);
73 torture_assert_int_equal(torture,
74 rep.data.interface_version,
75 WINBIND_INTERFACE_VERSION,
76 "winbind server and client doesn't match");
78 return true;
81 static bool torture_winbind_struct_ping(struct torture_context *torture)
83 struct timeval tv = timeval_current();
84 int timelimit = torture_setting_int(torture, "timelimit", 5);
85 uint32_t total = 0;
87 torture_comment(torture,
88 "Running WINBINDD_PING (struct based) for %d seconds\n",
89 timelimit);
91 while (timeval_elapsed(&tv) < timelimit) {
92 DO_STRUCT_REQ_REP(WINBINDD_PING, NULL, NULL);
93 total++;
96 torture_comment(torture,
97 "%u (%.1f/s) WINBINDD_PING (struct based)\n",
98 total, total / timeval_elapsed(&tv));
100 return true;
104 static char winbind_separator(struct torture_context *torture)
106 struct winbindd_response rep;
108 ZERO_STRUCT(rep);
110 DO_STRUCT_REQ_REP(WINBINDD_INFO, NULL, &rep);
112 return rep.data.info.winbind_separator;
115 static bool torture_winbind_struct_info(struct torture_context *torture)
117 struct winbindd_response rep;
118 const char *separator;
120 ZERO_STRUCT(rep);
122 torture_comment(torture, "Running WINBINDD_INFO (struct based)\n");
124 DO_STRUCT_REQ_REP(WINBINDD_INFO, NULL, &rep);
126 separator = torture_setting_string(torture,
127 "winbindd_separator",
128 lpcfg_winbind_separator(torture->lp_ctx));
130 torture_assert_int_equal(torture,
131 rep.data.info.winbind_separator,
132 *separator,
133 "winbind separator doesn't match");
135 torture_comment(torture, "Samba Version '%s'\n",
136 rep.data.info.samba_version);
138 return true;
141 static bool torture_winbind_struct_priv_pipe_dir(struct torture_context *torture)
143 struct winbindd_response rep;
144 const char *got_dir;
146 ZERO_STRUCT(rep);
148 torture_comment(torture, "Running WINBINDD_PRIV_PIPE_DIR (struct based)\n");
150 DO_STRUCT_REQ_REP(WINBINDD_PRIV_PIPE_DIR, NULL, &rep);
152 got_dir = (const char *)rep.extra_data.data;
154 torture_assert(torture, got_dir, "NULL WINBINDD_PRIV_PIPE_DIR\n");
156 SAFE_FREE(rep.extra_data.data);
157 return true;
160 static bool torture_winbind_struct_netbios_name(struct torture_context *torture)
162 struct winbindd_response rep;
163 const char *expected;
165 ZERO_STRUCT(rep);
167 torture_comment(torture, "Running WINBINDD_NETBIOS_NAME (struct based)\n");
169 DO_STRUCT_REQ_REP(WINBINDD_NETBIOS_NAME, NULL, &rep);
171 expected = torture_setting_string(torture,
172 "winbindd_netbios_name",
173 lpcfg_netbios_name(torture->lp_ctx));
174 expected = strupper_talloc(torture, expected);
176 torture_assert_str_equal(torture,
177 rep.data.netbios_name, expected,
178 "winbindd's netbios name doesn't match");
180 return true;
183 static bool get_winbind_domain(struct torture_context *torture, char **domain)
185 struct winbindd_response rep;
187 ZERO_STRUCT(rep);
189 DO_STRUCT_REQ_REP(WINBINDD_DOMAIN_NAME, NULL, &rep);
191 *domain = talloc_strdup(torture, rep.data.domain_name);
192 torture_assert(torture, domain, "talloc error");
194 return true;
197 static bool torture_winbind_struct_domain_name(struct torture_context *torture)
199 const char *expected;
200 char *domain;
202 torture_comment(torture, "Running WINBINDD_DOMAIN_NAME (struct based)\n");
204 expected = torture_setting_string(torture,
205 "winbindd_netbios_domain",
206 lpcfg_workgroup(torture->lp_ctx));
208 get_winbind_domain(torture, &domain);
210 torture_assert_str_equal(torture, domain, expected,
211 "winbindd's netbios domain doesn't match");
213 return true;
216 static bool torture_winbind_struct_check_machacc(struct torture_context *torture)
218 bool ok;
219 bool strict = torture_setting_bool(torture, "strict mode", false);
220 struct winbindd_response rep;
222 ZERO_STRUCT(rep);
224 torture_comment(torture, "Running WINBINDD_CHECK_MACHACC (struct based)\n");
226 ok = true;
227 DO_STRUCT_REQ_REP_EXT(WINBINDD_CHECK_MACHACC, NULL, &rep,
228 NSS_STATUS_SUCCESS, strict, ok = false,
229 "WINBINDD_CHECK_MACHACC");
231 if (!ok) {
232 torture_assert(torture,
233 strlen(rep.data.auth.nt_status_string)>0,
234 "Failed with empty nt_status_string");
236 torture_warning(torture,"%s:%s:%s:%d\n",
237 nt_errstr(NT_STATUS(rep.data.auth.nt_status)),
238 rep.data.auth.nt_status_string,
239 rep.data.auth.error_string,
240 rep.data.auth.pam_error);
241 return true;
244 torture_assert_ntstatus_ok(torture,
245 NT_STATUS(rep.data.auth.nt_status),
246 "WINBINDD_CHECK_MACHACC ok: nt_status");
248 torture_assert_str_equal(torture,
249 rep.data.auth.nt_status_string,
250 nt_errstr(NT_STATUS_OK),
251 "WINBINDD_CHECK_MACHACC ok:nt_status_string");
253 torture_assert_str_equal(torture,
254 rep.data.auth.error_string,
255 get_friendly_nt_error_msg(NT_STATUS_OK),
256 "WINBINDD_CHECK_MACHACC ok: error_string");
258 torture_assert_int_equal(torture,
259 rep.data.auth.pam_error,
260 nt_status_to_pam(NT_STATUS_OK),
261 "WINBINDD_CHECK_MACHACC ok: pam_error");
263 return true;
266 struct torture_trust_domain {
267 const char *netbios_name;
268 const char *dns_name;
269 struct dom_sid *sid;
272 static bool get_trusted_domains(struct torture_context *torture,
273 struct torture_trust_domain **_d)
275 struct winbindd_request req;
276 struct winbindd_response rep;
277 struct torture_trust_domain *d = NULL;
278 uint32_t dcount = 0;
279 char line[256];
280 const char *extra_data;
282 ZERO_STRUCT(req);
283 ZERO_STRUCT(rep);
285 DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
287 extra_data = (char *)rep.extra_data.data;
288 if (!extra_data) {
289 return true;
292 torture_assert(torture, extra_data, "NULL trust list");
294 while (next_token(&extra_data, line, "\n", sizeof(line))) {
295 char *p, *lp;
297 d = talloc_realloc(torture, d,
298 struct torture_trust_domain,
299 dcount + 2);
300 ZERO_STRUCT(d[dcount+1]);
302 lp = line;
303 p = strchr(lp, '\\');
304 torture_assert(torture, p, "missing 1st '\\' in line");
305 *p = 0;
306 d[dcount].netbios_name = talloc_strdup(d, lp);
307 torture_assert(torture, strlen(d[dcount].netbios_name) > 0,
308 "empty netbios_name");
310 lp = p+1;
311 p = strchr(lp, '\\');
312 torture_assert(torture, p, "missing 2nd '\\' in line");
313 *p = 0;
314 d[dcount].dns_name = talloc_strdup(d, lp);
315 /* it's ok to have an empty dns_name */
317 lp = p+1;
318 d[dcount].sid = dom_sid_parse_talloc(d, lp);
319 torture_assert(torture, d[dcount].sid,
320 "failed to parse sid");
322 dcount++;
324 SAFE_FREE(rep.extra_data.data);
326 torture_assert(torture, dcount >= 2,
327 "The list of trusted domain should contain 2 entries");
329 *_d = d;
330 return true;
333 static bool torture_winbind_struct_list_trustdom(struct torture_context *torture)
335 struct winbindd_request req;
336 struct winbindd_response rep;
337 char *list1;
338 char *list2;
339 bool ok;
340 struct torture_trust_domain *listd = NULL;
341 uint32_t i;
343 torture_comment(torture, "Running WINBINDD_LIST_TRUSTDOM (struct based)\n");
345 ZERO_STRUCT(req);
346 ZERO_STRUCT(rep);
348 req.data.list_all_domains = false;
350 DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
352 list1 = (char *)rep.extra_data.data;
354 torture_comment(torture, "%s\n", list1);
356 ZERO_STRUCT(req);
357 ZERO_STRUCT(rep);
359 req.data.list_all_domains = true;
361 DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
363 list2 = (char *)rep.extra_data.data;
366 * The list_all_domains parameter should be ignored
368 torture_assert_str_equal(torture, list2, list1, "list_all_domains not ignored");
370 SAFE_FREE(list1);
371 SAFE_FREE(list2);
373 ok = get_trusted_domains(torture, &listd);
374 torture_assert(torture, ok, "failed to get trust list");
376 for (i=0; listd && listd[i].netbios_name; i++) {
377 if (i == 0) {
378 struct dom_sid *builtin_sid;
380 builtin_sid = dom_sid_parse_talloc(torture, SID_BUILTIN);
382 torture_assert_str_equal(torture,
383 listd[i].netbios_name,
384 NAME_BUILTIN,
385 "first domain should be 'BUILTIN'");
387 torture_assert_str_equal(torture,
388 listd[i].dns_name,
390 "BUILTIN domain should not have a dns name");
392 ok = dom_sid_equal(builtin_sid,
393 listd[i].sid);
394 torture_assert(torture, ok, "BUILTIN domain should have S-1-5-32");
396 continue;
400 * TODO: verify the content of the 2nd and 3rd (in member server mode)
401 * domain entries
405 return true;
408 static bool torture_winbind_struct_domain_info(struct torture_context *torture)
410 bool ok;
411 struct torture_trust_domain *listd = NULL;
412 uint32_t i;
414 torture_comment(torture, "Running WINBINDD_DOMAIN_INFO (struct based)\n");
416 ok = get_trusted_domains(torture, &listd);
417 torture_assert(torture, ok, "failed to get trust list");
419 for (i=0; listd && listd[i].netbios_name; i++) {
420 struct winbindd_request req;
421 struct winbindd_response rep;
422 struct dom_sid *sid;
423 char *flagstr = talloc_strdup(torture," ");
425 ZERO_STRUCT(req);
426 ZERO_STRUCT(rep);
428 fstrcpy(req.domain_name, listd[i].netbios_name);
430 DO_STRUCT_REQ_REP(WINBINDD_DOMAIN_INFO, &req, &rep);
432 if (rep.data.domain_info.primary) {
433 flagstr = talloc_strdup_append(flagstr, "PR ");
436 if (rep.data.domain_info.active_directory) {
437 torture_assert(torture,
438 strlen(rep.data.domain_info.alt_name)>0,
439 "Active Directory without DNS name");
440 flagstr = talloc_strdup_append(flagstr, "AD ");
443 if (rep.data.domain_info.native_mode) {
444 torture_assert(torture,
445 rep.data.domain_info.active_directory,
446 "Native-Mode, but no Active Directory");
447 flagstr = talloc_strdup_append(flagstr, "NA ");
450 torture_comment(torture, "DOMAIN '%s' => '%s' [%s] [%s]\n",
451 rep.data.domain_info.name,
452 rep.data.domain_info.alt_name,
453 flagstr,
454 rep.data.domain_info.sid);
456 sid = dom_sid_parse_talloc(torture, rep.data.domain_info.sid);
457 torture_assert(torture, sid, "Failed to parse SID");
459 ok = dom_sid_equal(listd[i].sid, sid);
460 torture_assert(torture, ok, "SID's doesn't match");
462 torture_assert_str_equal(torture,
463 rep.data.domain_info.name,
464 listd[i].netbios_name,
465 "Netbios domain name doesn't match");
467 torture_assert_str_equal(torture,
468 rep.data.domain_info.alt_name,
469 listd[i].dns_name,
470 "DNS domain name doesn't match");
473 return true;
476 static bool torture_winbind_struct_getdcname(struct torture_context *torture)
478 bool ok;
479 bool strict = torture_setting_bool(torture, "strict mode", false);
480 const char *domain_name = torture_setting_string(torture,
481 "winbindd_netbios_domain",
482 lpcfg_workgroup(torture->lp_ctx));
483 struct torture_trust_domain *listd = NULL;
484 uint32_t i, count = 0;
486 torture_comment(torture, "Running WINBINDD_GETDCNAME (struct based)\n");
488 ok = get_trusted_domains(torture, &listd);
489 torture_assert(torture, ok, "failed to get trust list");
491 for (i=0; listd && listd[i].netbios_name; i++) {
492 struct winbindd_request req;
493 struct winbindd_response rep;
495 /* getdcname is not expected to work on "BUILTIN" or our own
496 * domain */
497 if (strequal(listd[i].netbios_name, "BUILTIN") ||
498 strequal(listd[i].netbios_name, domain_name)) {
499 continue;
502 ZERO_STRUCT(req);
503 ZERO_STRUCT(rep);
505 fstrcpy(req.domain_name, listd[i].netbios_name);
507 ok = true;
508 DO_STRUCT_REQ_REP_EXT(WINBINDD_GETDCNAME, &req, &rep,
509 NSS_STATUS_SUCCESS,
510 (i <2 || strict), ok = false,
511 talloc_asprintf(torture, "DOMAIN '%s'",
512 req.domain_name));
513 if (!ok) continue;
515 /* TODO: check rep.data.dc_name; */
516 torture_comment(torture, "DOMAIN '%s' => DCNAME '%s'\n",
517 req.domain_name, rep.data.dc_name);
518 count++;
521 if (strict) {
522 torture_assert(torture, count > 0,
523 "WiNBINDD_GETDCNAME was not tested");
525 return true;
528 static bool torture_winbind_struct_dsgetdcname(struct torture_context *torture)
530 bool ok;
531 bool strict = torture_setting_bool(torture, "strict mode", false);
532 struct torture_trust_domain *listd = NULL;
533 uint32_t i;
534 uint32_t count = 0;
536 torture_comment(torture, "Running WINBINDD_DSGETDCNAME (struct based)\n");
538 ok = get_trusted_domains(torture, &listd);
539 torture_assert(torture, ok, "failed to get trust list");
541 for (i=0; listd && listd[i].netbios_name; i++) {
542 struct winbindd_request req;
543 struct winbindd_response rep;
545 ZERO_STRUCT(req);
546 ZERO_STRUCT(rep);
548 if (strlen(listd[i].dns_name) == 0) continue;
551 * TODO: remove this and let winbindd give no dns name
552 * for NT4 domains
554 if (strcmp(listd[i].dns_name, listd[i].netbios_name) == 0) {
555 continue;
558 fstrcpy(req.domain_name, listd[i].dns_name);
560 /* TODO: test more flag combinations */
561 req.flags = DS_DIRECTORY_SERVICE_REQUIRED;
563 ok = true;
564 DO_STRUCT_REQ_REP_EXT(WINBINDD_DSGETDCNAME, &req, &rep,
565 NSS_STATUS_SUCCESS,
566 strict, ok = false,
567 talloc_asprintf(torture, "DOMAIN '%s'",
568 req.domain_name));
569 if (!ok) continue;
571 /* TODO: check rep.data.dc_name; */
572 torture_comment(torture, "DOMAIN '%s' => DCNAME '%s'\n",
573 req.domain_name, rep.data.dc_name);
575 count++;
578 if (count == 0) {
579 torture_warning(torture, "WINBINDD_DSGETDCNAME"
580 " was not tested with %d non-AD domains",
584 if (strict) {
585 torture_assert(torture, count > 0,
586 "WiNBINDD_DSGETDCNAME was not tested");
589 return true;
592 static bool get_user_list(struct torture_context *torture, char ***users)
594 struct winbindd_request req;
595 struct winbindd_response rep;
596 char **u = NULL;
597 uint32_t count;
598 char name[256];
599 const char *extra_data;
601 ZERO_STRUCT(req);
602 ZERO_STRUCT(rep);
604 DO_STRUCT_REQ_REP(WINBINDD_LIST_USERS, &req, &rep);
606 extra_data = (char *)rep.extra_data.data;
607 torture_assert(torture, extra_data, "NULL extra data");
609 for(count = 0;
610 next_token(&extra_data, name, ",", sizeof(name));
611 count++)
613 u = talloc_realloc(torture, u, char *, count + 2);
614 u[count+1] = NULL;
615 u[count] = talloc_strdup(u, name);
618 SAFE_FREE(rep.extra_data.data);
620 *users = u;
621 return true;
624 static bool torture_winbind_struct_list_users(struct torture_context *torture)
626 char **users;
627 uint32_t count;
628 bool ok;
630 torture_comment(torture, "Running WINBINDD_LIST_USERS (struct based)\n");
632 ok = get_user_list(torture, &users);
633 torture_assert(torture, ok, "failed to get user list");
635 for (count = 0; users[count]; count++) { }
637 torture_comment(torture, "got %d users\n", count);
639 return true;
642 static bool get_group_list(struct torture_context *torture,
643 unsigned int *num_entries,
644 char ***groups)
646 struct winbindd_request req;
647 struct winbindd_response rep;
648 char **g = NULL;
649 uint32_t count;
650 char name[256];
651 const char *extra_data;
653 ZERO_STRUCT(req);
654 ZERO_STRUCT(rep);
656 DO_STRUCT_REQ_REP(WINBINDD_LIST_GROUPS, &req, &rep);
657 extra_data = (char *)rep.extra_data.data;
659 *num_entries = rep.data.num_entries;
661 if (*num_entries == 0) {
662 torture_assert(torture, extra_data == NULL,
663 "extra data is null for >0 reported entries\n");
664 *groups = NULL;
665 return true;
668 torture_assert(torture, extra_data, "NULL extra data");
670 for(count = 0;
671 next_token(&extra_data, name, ",", sizeof(name));
672 count++)
674 g = talloc_realloc(torture, g, char *, count + 2);
675 g[count+1] = NULL;
676 g[count] = talloc_strdup(g, name);
679 SAFE_FREE(rep.extra_data.data);
681 torture_assert_int_equal(torture, *num_entries, count,
682 "Wrong number of group entries reported.");
684 *groups = g;
685 return true;
688 static bool torture_winbind_struct_list_groups(struct torture_context *torture)
690 char **groups;
691 uint32_t count;
692 bool ok;
694 torture_comment(torture, "Running WINBINDD_LIST_GROUPS (struct based)\n");
696 ok = get_group_list(torture, &count, &groups);
697 torture_assert(torture, ok, "failed to get group list");
699 torture_comment(torture, "got %d groups\n", count);
701 return true;
704 struct torture_domain_sequence {
705 const char *netbios_name;
706 uint32_t seq;
709 static bool get_sequence_numbers(struct torture_context *torture,
710 struct torture_domain_sequence **seqs)
712 struct winbindd_request req;
713 struct winbindd_response rep;
714 const char *extra_data;
715 char line[256];
716 uint32_t count = 0;
717 struct torture_domain_sequence *s = NULL;
719 ZERO_STRUCT(req);
720 ZERO_STRUCT(rep);
722 DO_STRUCT_REQ_REP(WINBINDD_SHOW_SEQUENCE, &req, &rep);
724 extra_data = (char *)rep.extra_data.data;
725 torture_assert(torture, extra_data, "NULL sequence list");
727 while (next_token(&extra_data, line, "\n", sizeof(line))) {
728 char *p, *lp;
729 uint32_t seq;
731 s = talloc_realloc(torture, s, struct torture_domain_sequence,
732 count + 2);
733 ZERO_STRUCT(s[count+1]);
735 lp = line;
736 p = strchr(lp, ' ');
737 torture_assert(torture, p, "invalid line format");
738 *p = 0;
739 s[count].netbios_name = talloc_strdup(s, lp);
741 lp = p+1;
742 torture_assert(torture, strncmp(lp, ": ", 2) == 0,
743 "invalid line format");
744 lp += 2;
745 if (strcmp(lp, "DISCONNECTED") == 0) {
746 seq = (uint32_t)-1;
747 } else {
748 seq = (uint32_t)strtol(lp, &p, 10);
749 torture_assert(torture, (*p == '\0'),
750 "invalid line format");
751 torture_assert(torture, (seq != (uint32_t)-1),
752 "sequence number -1 encountered");
754 s[count].seq = seq;
756 count++;
758 SAFE_FREE(rep.extra_data.data);
760 torture_assert(torture, count >= 2, "The list of domain sequence "
761 "numbers should contain 2 entries");
763 *seqs = s;
764 return true;
767 static bool torture_winbind_struct_show_sequence(struct torture_context *torture)
769 bool ok;
770 uint32_t i;
771 struct torture_trust_domain *domlist = NULL;
772 struct torture_domain_sequence *s = NULL;
774 torture_comment(torture, "Running WINBINDD_SHOW_SEQUENCE (struct based)\n");
776 ok = get_sequence_numbers(torture, &s);
777 torture_assert(torture, ok, "failed to get list of sequence numbers");
779 ok = get_trusted_domains(torture, &domlist);
780 torture_assert(torture, ok, "failed to get trust list");
782 for (i=0; domlist[i].netbios_name; i++) {
783 struct winbindd_request req;
784 struct winbindd_response rep;
785 uint32_t seq;
787 torture_assert(torture, s[i].netbios_name,
788 "more domains received in second run");
789 torture_assert_str_equal(torture, domlist[i].netbios_name,
790 s[i].netbios_name,
791 "inconsistent order of domain lists");
793 ZERO_STRUCT(req);
794 ZERO_STRUCT(rep);
795 fstrcpy(req.domain_name, domlist[i].netbios_name);
797 DO_STRUCT_REQ_REP(WINBINDD_SHOW_SEQUENCE, &req, &rep);
799 seq = rep.data.sequence_number;
801 if (i == 0) {
802 torture_assert(torture, (seq != (uint32_t)-1),
803 "BUILTIN domain disconnected");
804 } else if (i == 1) {
805 torture_assert(torture, (seq != (uint32_t)-1),
806 "local domain disconnected");
810 if (seq == (uint32_t)-1) {
811 torture_comment(torture, " * %s : DISCONNECTED\n",
812 req.domain_name);
813 } else {
814 torture_comment(torture, " * %s : %d\n",
815 req.domain_name, seq);
817 torture_assert(torture, (seq >= s[i].seq),
818 "illegal sequence number encountered");
821 return true;
824 static bool torture_winbind_struct_setpwent(struct torture_context *torture)
826 struct winbindd_request req;
827 struct winbindd_response rep;
829 torture_comment(torture, "Running WINBINDD_SETPWENT (struct based)\n");
831 ZERO_STRUCT(req);
832 ZERO_STRUCT(rep);
834 DO_STRUCT_REQ_REP(WINBINDD_SETPWENT, &req, &rep);
836 return true;
839 static bool torture_winbind_struct_getpwent(struct torture_context *torture)
841 struct winbindd_request req;
842 struct winbindd_response rep;
843 struct winbindd_pw *pwent;
845 torture_comment(torture, "Running WINBINDD_GETPWENT (struct based)\n");
847 torture_comment(torture, " - Running WINBINDD_SETPWENT first\n");
848 ZERO_STRUCT(req);
849 ZERO_STRUCT(rep);
850 DO_STRUCT_REQ_REP(WINBINDD_SETPWENT, &req, &rep);
852 torture_comment(torture, " - Running WINBINDD_GETPWENT now\n");
853 ZERO_STRUCT(req);
854 ZERO_STRUCT(rep);
855 req.data.num_entries = 1;
856 if (torture_setting_bool(torture, "samba3", false)) {
857 DO_STRUCT_REQ_REP_EXT(WINBINDD_GETPWENT, &req, &rep,
858 NSS_STATUS_SUCCESS, false, _STRUCT_NOOP,
859 NULL);
860 } else {
861 DO_STRUCT_REQ_REP(WINBINDD_GETPWENT, &req, &rep);
863 pwent = (struct winbindd_pw *)rep.extra_data.data;
864 if (!torture_setting_bool(torture, "samba3", false)) {
865 torture_assert(torture, (pwent != NULL), "NULL pwent");
867 if (pwent) {
868 torture_comment(torture, "name: %s, uid: %d, gid: %d, shell: %s\n",
869 pwent->pw_name, pwent->pw_uid, pwent->pw_gid,
870 pwent->pw_shell);
873 return true;
876 static bool torture_winbind_struct_endpwent(struct torture_context *torture)
878 struct winbindd_request req;
879 struct winbindd_response rep;
881 torture_comment(torture, "Running WINBINDD_ENDPWENT (struct based)\n");
883 ZERO_STRUCT(req);
884 ZERO_STRUCT(rep);
886 DO_STRUCT_REQ_REP(WINBINDD_ENDPWENT, &req, &rep);
888 return true;
891 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
892 form DOMAIN/user into a domain and a user */
894 static bool parse_domain_user(struct torture_context *torture,
895 const char *domuser, fstring domain,
896 fstring user)
898 char *p = strchr(domuser, winbind_separator(torture));
899 char *dom = NULL;
901 if (!p) {
902 /* Maybe it was a UPN? */
903 if ((p = strchr(domuser, '@')) != NULL) {
904 fstrcpy(domain, "");
905 fstrcpy(user, domuser);
906 return true;
909 fstrcpy(user, domuser);
910 get_winbind_domain(torture, &dom);
911 fstrcpy(domain, dom);
912 return true;
915 fstrcpy(user, p+1);
916 fstrcpy(domain, domuser);
917 domain[PTR_DIFF(p, domuser)] = 0;
919 return true;
922 static bool lookup_name_sid_list(struct torture_context *torture, char **list)
924 uint32_t count;
926 for (count = 0; list[count]; count++) {
927 struct winbindd_request req;
928 struct winbindd_response rep;
929 char *sid;
930 char *name;
931 const char *domain_name = torture_setting_string(torture,
932 "winbindd_netbios_domain",
933 lpcfg_workgroup(torture->lp_ctx));
935 ZERO_STRUCT(req);
936 ZERO_STRUCT(rep);
938 parse_domain_user(torture, list[count], req.data.name.dom_name,
939 req.data.name.name);
941 DO_STRUCT_REQ_REP(WINBINDD_LOOKUPNAME, &req, &rep);
943 sid = talloc_strdup(torture, rep.data.sid.sid);
945 ZERO_STRUCT(req);
946 ZERO_STRUCT(rep);
948 fstrcpy(req.data.sid, sid);
950 DO_STRUCT_REQ_REP(WINBINDD_LOOKUPSID, &req, &rep);
952 if (strequal(rep.data.name.dom_name, domain_name)) {
953 name = talloc_asprintf(torture, "%s",
954 rep.data.name.name);
955 } else {
956 name = talloc_asprintf(torture, "%s%c%s",
957 rep.data.name.dom_name,
958 winbind_separator(torture),
959 rep.data.name.name);
962 torture_assert_casestr_equal(torture, list[count], name,
963 "LOOKUP_SID after LOOKUP_NAME != id");
965 #if 0
966 torture_comment(torture, " %s -> %s -> %s\n", list[count],
967 sid, name);
968 #endif
970 talloc_free(sid);
971 talloc_free(name);
974 return true;
977 static bool name_is_in_list(const char *name, char **list)
979 uint32_t count;
981 for (count = 0; list && list[count]; count++) {
982 if (strequal(name, list[count])) {
983 return true;
986 return false;
989 static bool torture_winbind_struct_lookup_name_sid(struct torture_context *torture)
991 struct winbindd_request req;
992 struct winbindd_response rep;
993 const char *invalid_sid = "S-0-0-7";
994 char *domain = NULL;
995 const char *invalid_user = "noone";
996 char *invalid_name;
997 bool strict = torture_setting_bool(torture, "strict mode", false);
998 char **users;
999 char **groups;
1000 uint32_t count, num_groups;
1001 bool ok;
1003 torture_comment(torture, "Running WINBINDD_LOOKUP_NAME_SID (struct based)\n");
1005 ok = get_user_list(torture, &users);
1006 torture_assert(torture, ok, "failed to retrieve list of users");
1007 lookup_name_sid_list(torture, users);
1009 ok = get_group_list(torture, &num_groups, &groups);
1010 torture_assert(torture, ok, "failed to retrieve list of groups");
1011 if (num_groups > 0) {
1012 lookup_name_sid_list(torture, groups);
1015 ZERO_STRUCT(req);
1016 ZERO_STRUCT(rep);
1018 fstrcpy(req.data.sid, invalid_sid);
1020 ok = true;
1021 DO_STRUCT_REQ_REP_EXT(WINBINDD_LOOKUPSID, &req, &rep,
1022 NSS_STATUS_NOTFOUND,
1023 strict,
1024 ok=false,
1025 talloc_asprintf(torture,
1026 "invalid sid %s was resolved",
1027 invalid_sid));
1029 ZERO_STRUCT(req);
1030 ZERO_STRUCT(rep);
1032 /* try to find an invalid name... */
1034 count = 0;
1035 get_winbind_domain(torture, &domain);
1036 do {
1037 count++;
1038 invalid_name = talloc_asprintf(torture, "%s/%s%u",
1039 domain,
1040 invalid_user, count);
1041 } while(name_is_in_list(invalid_name, users) ||
1042 name_is_in_list(invalid_name, groups));
1044 fstrcpy(req.data.name.dom_name, domain);
1045 fstrcpy(req.data.name.name,
1046 talloc_asprintf(torture, "%s%u", invalid_user,
1047 count));
1049 ok = true;
1050 DO_STRUCT_REQ_REP_EXT(WINBINDD_LOOKUPNAME, &req, &rep,
1051 NSS_STATUS_NOTFOUND,
1052 strict,
1053 ok=false,
1054 talloc_asprintf(torture,
1055 "invalid name %s was resolved",
1056 invalid_name));
1058 talloc_free(users);
1059 talloc_free(groups);
1061 return true;
1064 struct torture_suite *torture_winbind_struct_init(void)
1066 struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "struct");
1068 torture_suite_add_simple_test(suite, "interface_version", torture_winbind_struct_interface_version);
1069 torture_suite_add_simple_test(suite, "ping", torture_winbind_struct_ping);
1070 torture_suite_add_simple_test(suite, "info", torture_winbind_struct_info);
1071 torture_suite_add_simple_test(suite, "priv_pipe_dir", torture_winbind_struct_priv_pipe_dir);
1072 torture_suite_add_simple_test(suite, "netbios_name", torture_winbind_struct_netbios_name);
1073 torture_suite_add_simple_test(suite, "domain_name", torture_winbind_struct_domain_name);
1074 torture_suite_add_simple_test(suite, "check_machacc", torture_winbind_struct_check_machacc);
1075 torture_suite_add_simple_test(suite, "list_trustdom", torture_winbind_struct_list_trustdom);
1076 torture_suite_add_simple_test(suite, "domain_info", torture_winbind_struct_domain_info);
1077 torture_suite_add_simple_test(suite, "getdcname", torture_winbind_struct_getdcname);
1078 torture_suite_add_simple_test(suite, "dsgetdcname", torture_winbind_struct_dsgetdcname);
1079 torture_suite_add_simple_test(suite, "list_users", torture_winbind_struct_list_users);
1080 torture_suite_add_simple_test(suite, "list_groups", torture_winbind_struct_list_groups);
1081 torture_suite_add_simple_test(suite, "show_sequence", torture_winbind_struct_show_sequence);
1082 torture_suite_add_simple_test(suite, "setpwent", torture_winbind_struct_setpwent);
1083 torture_suite_add_simple_test(suite, "getpwent", torture_winbind_struct_getpwent);
1084 torture_suite_add_simple_test(suite, "endpwent", torture_winbind_struct_endpwent);
1085 torture_suite_add_simple_test(suite, "lookup_name_sid", torture_winbind_struct_lookup_name_sid);
1087 suite->description = talloc_strdup(suite, "WINBIND - struct based protocol tests");
1089 return suite;