smbd: Clarify smb2 lock checks
[Samba.git] / source4 / torture / winbind / struct_based.c
blobd47d068909f68f1c2603e8dd3cf5b0e9d138f2c8
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(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 #define DO_STRUCT_REQ_REP(op,req,rep) do { \
56 bool __noop = false; \
57 DO_STRUCT_REQ_REP_EXT(op,req,rep,NSS_STATUS_SUCCESS,true,__noop=true,NULL); \
58 } while (0)
60 static bool torture_winbind_struct_interface_version(struct torture_context *torture)
62 struct winbindd_request req;
63 struct winbindd_response rep;
65 ZERO_STRUCT(req);
66 ZERO_STRUCT(rep);
68 torture_comment(torture, "Running WINBINDD_INTERFACE_VERSION (struct based)\n");
70 DO_STRUCT_REQ_REP(WINBINDD_INTERFACE_VERSION, &req, &rep);
72 torture_assert_int_equal(torture,
73 rep.data.interface_version,
74 WINBIND_INTERFACE_VERSION,
75 "winbind server and client doesn't match");
77 return true;
80 static bool torture_winbind_struct_ping(struct torture_context *torture)
82 struct timeval tv = timeval_current();
83 int timelimit = torture_setting_int(torture, "timelimit", 5);
84 uint32_t total = 0;
86 torture_comment(torture,
87 "Running WINBINDD_PING (struct based) for %d seconds\n",
88 timelimit);
90 while (timeval_elapsed(&tv) < timelimit) {
91 DO_STRUCT_REQ_REP(WINBINDD_PING, NULL, NULL);
92 total++;
95 torture_comment(torture,
96 "%u (%.1f/s) WINBINDD_PING (struct based)\n",
97 total, total / timeval_elapsed(&tv));
99 return true;
103 static char winbind_separator(struct torture_context *torture)
105 struct winbindd_response rep;
107 ZERO_STRUCT(rep);
109 DO_STRUCT_REQ_REP(WINBINDD_INFO, NULL, &rep);
111 return rep.data.info.winbind_separator;
114 static bool torture_winbind_struct_info(struct torture_context *torture)
116 struct winbindd_response rep;
117 const char *separator;
119 ZERO_STRUCT(rep);
121 torture_comment(torture, "Running WINBINDD_INFO (struct based)\n");
123 DO_STRUCT_REQ_REP(WINBINDD_INFO, NULL, &rep);
125 separator = torture_setting_string(torture,
126 "winbindd_separator",
127 lpcfg_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");
134 torture_comment(torture, "Samba Version '%s'\n",
135 rep.data.info.samba_version);
137 return true;
140 static bool torture_winbind_struct_priv_pipe_dir(struct torture_context *torture)
142 struct winbindd_response rep;
143 const char *got_dir;
145 ZERO_STRUCT(rep);
147 torture_comment(torture, "Running WINBINDD_PRIV_PIPE_DIR (struct based)\n");
149 DO_STRUCT_REQ_REP(WINBINDD_PRIV_PIPE_DIR, NULL, &rep);
151 got_dir = (const char *)rep.extra_data.data;
153 torture_assert(torture, got_dir, "NULL WINBINDD_PRIV_PIPE_DIR\n");
155 SAFE_FREE(rep.extra_data.data);
156 return true;
159 static bool torture_winbind_struct_netbios_name(struct torture_context *torture)
161 struct winbindd_response rep;
162 const char *expected;
164 ZERO_STRUCT(rep);
166 torture_comment(torture, "Running WINBINDD_NETBIOS_NAME (struct based)\n");
168 DO_STRUCT_REQ_REP(WINBINDD_NETBIOS_NAME, NULL, &rep);
170 expected = torture_setting_string(torture,
171 "winbindd_netbios_name",
172 lpcfg_netbios_name(torture->lp_ctx));
173 expected = strupper_talloc(torture, expected);
175 torture_assert_str_equal(torture,
176 rep.data.netbios_name, expected,
177 "winbindd's netbios name doesn't match");
179 return true;
182 static bool get_winbind_domain(struct torture_context *torture, char **domain)
184 struct winbindd_response rep;
186 ZERO_STRUCT(rep);
188 DO_STRUCT_REQ_REP(WINBINDD_DOMAIN_NAME, NULL, &rep);
190 *domain = talloc_strdup(torture, rep.data.domain_name);
191 torture_assert(torture, domain, "talloc error");
193 return true;
196 static bool torture_winbind_struct_domain_name(struct torture_context *torture)
198 const char *expected;
199 char *domain;
201 torture_comment(torture, "Running WINBINDD_DOMAIN_NAME (struct based)\n");
203 expected = torture_setting_string(torture,
204 "winbindd_netbios_domain",
205 lpcfg_workgroup(torture->lp_ctx));
207 get_winbind_domain(torture, &domain);
209 torture_assert_str_equal(torture, domain, expected,
210 "winbindd's netbios domain doesn't match");
212 return true;
215 static bool torture_winbind_struct_check_machacc(struct torture_context *torture)
217 bool ok;
218 bool strict = torture_setting_bool(torture, "strict mode", false);
219 struct winbindd_response rep;
221 ZERO_STRUCT(rep);
223 torture_comment(torture, "Running WINBINDD_CHECK_MACHACC (struct based)\n");
225 ok = true;
226 DO_STRUCT_REQ_REP_EXT(WINBINDD_CHECK_MACHACC, NULL, &rep,
227 NSS_STATUS_SUCCESS, strict, ok = false,
228 "WINBINDD_CHECK_MACHACC");
230 if (!ok) {
231 torture_assert(torture,
232 strlen(rep.data.auth.nt_status_string)>0,
233 "Failed with empty nt_status_string");
235 torture_warning(torture,"%s:%s:%s:%d\n",
236 nt_errstr(NT_STATUS(rep.data.auth.nt_status)),
237 rep.data.auth.nt_status_string,
238 rep.data.auth.error_string,
239 rep.data.auth.pam_error);
240 return true;
243 torture_assert_ntstatus_ok(torture,
244 NT_STATUS(rep.data.auth.nt_status),
245 "WINBINDD_CHECK_MACHACC ok: nt_status");
247 torture_assert_str_equal(torture,
248 rep.data.auth.nt_status_string,
249 nt_errstr(NT_STATUS_OK),
250 "WINBINDD_CHECK_MACHACC ok:nt_status_string");
252 torture_assert_str_equal(torture,
253 rep.data.auth.error_string,
254 get_friendly_nt_error_msg(NT_STATUS_OK),
255 "WINBINDD_CHECK_MACHACC ok: error_string");
257 torture_assert_int_equal(torture,
258 rep.data.auth.pam_error,
259 nt_status_to_pam(NT_STATUS_OK),
260 "WINBINDD_CHECK_MACHACC ok: pam_error");
262 return true;
265 struct torture_trust_domain {
266 const char *netbios_name;
267 const char *dns_name;
268 struct dom_sid *sid;
271 static bool get_trusted_domains(struct torture_context *torture,
272 struct torture_trust_domain **_d)
274 struct winbindd_request req;
275 struct winbindd_response rep;
276 struct torture_trust_domain *d = NULL;
277 uint32_t dcount = 0;
278 char line[256];
279 const char *extra_data;
281 ZERO_STRUCT(req);
282 ZERO_STRUCT(rep);
284 DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
286 extra_data = (char *)rep.extra_data.data;
287 if (!extra_data) {
288 return true;
291 torture_assert(torture, extra_data, "NULL trust list");
293 while (next_token(&extra_data, line, "\n", sizeof(line))) {
294 char *p, *lp;
296 d = talloc_realloc(torture, d,
297 struct torture_trust_domain,
298 dcount + 2);
299 ZERO_STRUCT(d[dcount+1]);
301 lp = line;
302 p = strchr(lp, '\\');
303 torture_assert(torture, p, "missing 1st '\\' in line");
304 *p = 0;
305 d[dcount].netbios_name = talloc_strdup(d, lp);
306 torture_assert(torture, strlen(d[dcount].netbios_name) > 0,
307 "empty netbios_name");
309 lp = p+1;
310 p = strchr(lp, '\\');
311 torture_assert(torture, p, "missing 2nd '\\' in line");
312 *p = 0;
313 d[dcount].dns_name = talloc_strdup(d, lp);
314 /* it's ok to have an empty dns_name */
316 lp = p+1;
317 d[dcount].sid = dom_sid_parse_talloc(d, lp);
318 torture_assert(torture, d[dcount].sid,
319 "failed to parse sid");
321 dcount++;
323 SAFE_FREE(rep.extra_data.data);
325 torture_assert(torture, dcount >= 2,
326 "The list of trusted domain should contain 2 entries");
328 *_d = d;
329 return true;
332 static bool torture_winbind_struct_list_trustdom(struct torture_context *torture)
334 struct winbindd_request req;
335 struct winbindd_response rep;
336 char *list1;
337 char *list2;
338 bool ok;
339 struct torture_trust_domain *listd = NULL;
340 uint32_t i;
342 torture_comment(torture, "Running WINBINDD_LIST_TRUSTDOM (struct based)\n");
344 ZERO_STRUCT(req);
345 ZERO_STRUCT(rep);
347 req.data.list_all_domains = false;
349 DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
351 list1 = (char *)rep.extra_data.data;
353 torture_comment(torture, "%s\n", list1);
355 ZERO_STRUCT(req);
356 ZERO_STRUCT(rep);
358 req.data.list_all_domains = true;
360 DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
362 list2 = (char *)rep.extra_data.data;
365 * The list_all_domains parameter should be ignored
367 torture_assert_str_equal(torture, list2, list1, "list_all_domains not ignored");
369 SAFE_FREE(list1);
370 SAFE_FREE(list2);
372 ok = get_trusted_domains(torture, &listd);
373 torture_assert(torture, ok, "failed to get trust list");
375 for (i=0; listd && listd[i].netbios_name; i++) {
376 if (i == 0) {
377 struct dom_sid *builtin_sid;
379 builtin_sid = dom_sid_parse_talloc(torture, SID_BUILTIN);
381 torture_assert_str_equal(torture,
382 listd[i].netbios_name,
383 NAME_BUILTIN,
384 "first domain should be 'BUILTIN'");
386 torture_assert_str_equal(torture,
387 listd[i].dns_name,
389 "BUILTIN domain should not have a dns name");
391 ok = dom_sid_equal(builtin_sid,
392 listd[i].sid);
393 torture_assert(torture, ok, "BUILTIN domain should have S-1-5-32");
395 continue;
399 * TODO: verify the content of the 2nd and 3rd (in member server mode)
400 * domain entries
404 return true;
407 static bool torture_winbind_struct_domain_info(struct torture_context *torture)
409 bool ok;
410 struct torture_trust_domain *listd = NULL;
411 uint32_t i;
413 torture_comment(torture, "Running WINBINDD_DOMAIN_INFO (struct based)\n");
415 ok = get_trusted_domains(torture, &listd);
416 torture_assert(torture, ok, "failed to get trust list");
418 for (i=0; listd && listd[i].netbios_name; i++) {
419 struct winbindd_request req;
420 struct winbindd_response rep;
421 struct dom_sid *sid;
422 char *flagstr = talloc_strdup(torture," ");
424 ZERO_STRUCT(req);
425 ZERO_STRUCT(rep);
427 fstrcpy(req.domain_name, listd[i].netbios_name);
429 DO_STRUCT_REQ_REP(WINBINDD_DOMAIN_INFO, &req, &rep);
431 torture_assert_str_equal(torture,
432 rep.data.domain_info.name,
433 listd[i].netbios_name,
434 "Netbios domain name doesn't match");
436 torture_assert_str_equal(torture,
437 rep.data.domain_info.alt_name,
438 listd[i].dns_name,
439 "DNS domain name doesn't match");
441 sid = dom_sid_parse_talloc(torture, rep.data.domain_info.sid);
442 torture_assert(torture, sid, "Failed to parse SID");
444 ok = dom_sid_equal(listd[i].sid, sid);
445 torture_assert(torture, ok, "SID's doesn't match");
447 if (rep.data.domain_info.primary) {
448 flagstr = talloc_strdup_append(flagstr, "PR ");
451 if (rep.data.domain_info.active_directory) {
452 torture_assert(torture,
453 strlen(rep.data.domain_info.alt_name)>0,
454 "Active Directory without DNS name");
455 flagstr = talloc_strdup_append(flagstr, "AD ");
458 if (rep.data.domain_info.native_mode) {
459 torture_assert(torture,
460 rep.data.domain_info.active_directory,
461 "Native-Mode, but no Active Directory");
462 flagstr = talloc_strdup_append(flagstr, "NA ");
465 torture_comment(torture, "DOMAIN '%s' => '%s' [%s]\n",
466 rep.data.domain_info.name,
467 rep.data.domain_info.alt_name,
468 flagstr);
471 return true;
474 static bool torture_winbind_struct_getdcname(struct torture_context *torture)
476 bool ok;
477 bool strict = torture_setting_bool(torture, "strict mode", false);
478 const char *domain_name = torture_setting_string(torture,
479 "winbindd_netbios_domain",
480 lpcfg_workgroup(torture->lp_ctx));
481 struct torture_trust_domain *listd = NULL;
482 uint32_t i, count = 0;
484 torture_comment(torture, "Running WINBINDD_GETDCNAME (struct based)\n");
486 ok = get_trusted_domains(torture, &listd);
487 torture_assert(torture, ok, "failed to get trust list");
489 for (i=0; listd && listd[i].netbios_name; i++) {
490 struct winbindd_request req;
491 struct winbindd_response rep;
493 /* getdcname is not expected to work on "BUILTIN" or our own
494 * domain */
495 if (strequal(listd[i].netbios_name, "BUILTIN") ||
496 strequal(listd[i].netbios_name, domain_name)) {
497 continue;
500 ZERO_STRUCT(req);
501 ZERO_STRUCT(rep);
503 fstrcpy(req.domain_name, listd[i].netbios_name);
505 ok = true;
506 DO_STRUCT_REQ_REP_EXT(WINBINDD_GETDCNAME, &req, &rep,
507 NSS_STATUS_SUCCESS,
508 (i <2 || strict), ok = false,
509 talloc_asprintf(torture, "DOMAIN '%s'",
510 req.domain_name));
511 if (!ok) continue;
513 /* TODO: check rep.data.dc_name; */
514 torture_comment(torture, "DOMAIN '%s' => DCNAME '%s'\n",
515 req.domain_name, rep.data.dc_name);
516 count++;
519 if (strict) {
520 torture_assert(torture, count > 0,
521 "WiNBINDD_GETDCNAME was not tested");
523 return true;
526 static bool torture_winbind_struct_dsgetdcname(struct torture_context *torture)
528 bool ok;
529 bool strict = torture_setting_bool(torture, "strict mode", false);
530 struct torture_trust_domain *listd = NULL;
531 uint32_t i;
532 uint32_t count = 0;
534 torture_comment(torture, "Running WINBINDD_DSGETDCNAME (struct based)\n");
536 ok = get_trusted_domains(torture, &listd);
537 torture_assert(torture, ok, "failed to get trust list");
539 for (i=0; listd && listd[i].netbios_name; i++) {
540 struct winbindd_request req;
541 struct winbindd_response rep;
543 ZERO_STRUCT(req);
544 ZERO_STRUCT(rep);
546 if (strlen(listd[i].dns_name) == 0) continue;
549 * TODO: remove this and let winbindd give no dns name
550 * for NT4 domains
552 if (strcmp(listd[i].dns_name, listd[i].netbios_name) == 0) {
553 continue;
556 fstrcpy(req.domain_name, listd[i].dns_name);
558 /* TODO: test more flag combinations */
559 req.flags = DS_DIRECTORY_SERVICE_REQUIRED;
561 ok = true;
562 DO_STRUCT_REQ_REP_EXT(WINBINDD_DSGETDCNAME, &req, &rep,
563 NSS_STATUS_SUCCESS,
564 strict, ok = false,
565 talloc_asprintf(torture, "DOMAIN '%s'",
566 req.domain_name));
567 if (!ok) continue;
569 /* TODO: check rep.data.dc_name; */
570 torture_comment(torture, "DOMAIN '%s' => DCNAME '%s'\n",
571 req.domain_name, rep.data.dc_name);
573 count++;
576 if (count == 0) {
577 torture_warning(torture, "WINBINDD_DSGETDCNAME"
578 " was not tested with %d non-AD domains",
582 if (strict) {
583 torture_assert(torture, count > 0,
584 "WiNBINDD_DSGETDCNAME was not tested");
587 return true;
590 static bool get_user_list(struct torture_context *torture, char ***users)
592 struct winbindd_request req;
593 struct winbindd_response rep;
594 char **u = NULL;
595 uint32_t count;
596 char name[256];
597 const char *extra_data;
599 ZERO_STRUCT(req);
600 ZERO_STRUCT(rep);
602 DO_STRUCT_REQ_REP(WINBINDD_LIST_USERS, &req, &rep);
604 extra_data = (char *)rep.extra_data.data;
605 torture_assert(torture, extra_data, "NULL extra data");
607 for(count = 0;
608 next_token(&extra_data, name, ",", sizeof(name));
609 count++)
611 u = talloc_realloc(torture, u, char *, count + 2);
612 u[count+1] = NULL;
613 u[count] = talloc_strdup(u, name);
616 SAFE_FREE(rep.extra_data.data);
618 *users = u;
619 return true;
622 static bool torture_winbind_struct_list_users(struct torture_context *torture)
624 char **users;
625 uint32_t count;
626 bool ok;
628 torture_comment(torture, "Running WINBINDD_LIST_USERS (struct based)\n");
630 ok = get_user_list(torture, &users);
631 torture_assert(torture, ok, "failed to get user list");
633 for (count = 0; users[count]; count++) { }
635 torture_comment(torture, "got %d users\n", count);
637 return true;
640 static bool get_group_list(struct torture_context *torture,
641 unsigned int *num_entries,
642 char ***groups)
644 struct winbindd_request req;
645 struct winbindd_response rep;
646 char **g = NULL;
647 uint32_t count;
648 char name[256];
649 const char *extra_data;
651 ZERO_STRUCT(req);
652 ZERO_STRUCT(rep);
654 DO_STRUCT_REQ_REP(WINBINDD_LIST_GROUPS, &req, &rep);
655 extra_data = (char *)rep.extra_data.data;
657 *num_entries = rep.data.num_entries;
659 if (*num_entries == 0) {
660 torture_assert(torture, extra_data == NULL,
661 "extra data is null for >0 reported entries\n");
662 *groups = NULL;
663 return true;
666 torture_assert(torture, extra_data, "NULL extra data");
668 for(count = 0;
669 next_token(&extra_data, name, ",", sizeof(name));
670 count++)
672 g = talloc_realloc(torture, g, char *, count + 2);
673 g[count+1] = NULL;
674 g[count] = talloc_strdup(g, name);
677 SAFE_FREE(rep.extra_data.data);
679 torture_assert_int_equal(torture, *num_entries, count,
680 "Wrong number of group entries reported.");
682 *groups = g;
683 return true;
686 static bool torture_winbind_struct_list_groups(struct torture_context *torture)
688 char **groups;
689 uint32_t count;
690 bool ok;
692 torture_comment(torture, "Running WINBINDD_LIST_GROUPS (struct based)\n");
694 ok = get_group_list(torture, &count, &groups);
695 torture_assert(torture, ok, "failed to get group list");
697 torture_comment(torture, "got %d groups\n", count);
699 return true;
702 struct torture_domain_sequence {
703 const char *netbios_name;
704 uint32_t seq;
707 static bool get_sequence_numbers(struct torture_context *torture,
708 struct torture_domain_sequence **seqs)
710 struct winbindd_request req;
711 struct winbindd_response rep;
712 const char *extra_data;
713 char line[256];
714 uint32_t count = 0;
715 struct torture_domain_sequence *s = NULL;
717 ZERO_STRUCT(req);
718 ZERO_STRUCT(rep);
720 DO_STRUCT_REQ_REP(WINBINDD_SHOW_SEQUENCE, &req, &rep);
722 extra_data = (char *)rep.extra_data.data;
723 torture_assert(torture, extra_data, "NULL sequence list");
725 while (next_token(&extra_data, line, "\n", sizeof(line))) {
726 char *p, *lp;
727 uint32_t seq;
729 s = talloc_realloc(torture, s, struct torture_domain_sequence,
730 count + 2);
731 ZERO_STRUCT(s[count+1]);
733 lp = line;
734 p = strchr(lp, ' ');
735 torture_assert(torture, p, "invalid line format");
736 *p = 0;
737 s[count].netbios_name = talloc_strdup(s, lp);
739 lp = p+1;
740 torture_assert(torture, strncmp(lp, ": ", 2) == 0,
741 "invalid line format");
742 lp += 2;
743 if (strcmp(lp, "DISCONNECTED") == 0) {
744 seq = (uint32_t)-1;
745 } else {
746 seq = (uint32_t)strtol(lp, &p, 10);
747 torture_assert(torture, (*p == '\0'),
748 "invalid line format");
749 torture_assert(torture, (seq != (uint32_t)-1),
750 "sequence number -1 encountered");
752 s[count].seq = seq;
754 count++;
756 SAFE_FREE(rep.extra_data.data);
758 torture_assert(torture, count >= 2, "The list of domain sequence "
759 "numbers should contain 2 entries");
761 *seqs = s;
762 return true;
765 static bool torture_winbind_struct_show_sequence(struct torture_context *torture)
767 bool ok;
768 uint32_t i;
769 struct torture_trust_domain *domlist = NULL;
770 struct torture_domain_sequence *s = NULL;
772 torture_comment(torture, "Running WINBINDD_SHOW_SEQUENCE (struct based)\n");
774 ok = get_sequence_numbers(torture, &s);
775 torture_assert(torture, ok, "failed to get list of sequence numbers");
777 ok = get_trusted_domains(torture, &domlist);
778 torture_assert(torture, ok, "failed to get trust list");
780 for (i=0; domlist[i].netbios_name; i++) {
781 struct winbindd_request req;
782 struct winbindd_response rep;
783 uint32_t seq;
785 torture_assert(torture, s[i].netbios_name,
786 "more domains received in second run");
787 torture_assert_str_equal(torture, domlist[i].netbios_name,
788 s[i].netbios_name,
789 "inconsistent order of domain lists");
791 ZERO_STRUCT(req);
792 ZERO_STRUCT(rep);
793 fstrcpy(req.domain_name, domlist[i].netbios_name);
795 DO_STRUCT_REQ_REP(WINBINDD_SHOW_SEQUENCE, &req, &rep);
797 seq = rep.data.sequence_number;
799 if (i == 0) {
800 torture_assert(torture, (seq != (uint32_t)-1),
801 "BUILTIN domain disconnected");
802 } else if (i == 1) {
803 torture_assert(torture, (seq != (uint32_t)-1),
804 "local domain disconnected");
808 if (seq == (uint32_t)-1) {
809 torture_comment(torture, " * %s : DISCONNECTED\n",
810 req.domain_name);
811 } else {
812 torture_comment(torture, " * %s : %d\n",
813 req.domain_name, seq);
815 torture_assert(torture, (seq >= s[i].seq),
816 "illegal sequence number encountered");
819 return true;
822 static bool torture_winbind_struct_setpwent(struct torture_context *torture)
824 struct winbindd_request req;
825 struct winbindd_response rep;
827 torture_comment(torture, "Running WINBINDD_SETPWENT (struct based)\n");
829 ZERO_STRUCT(req);
830 ZERO_STRUCT(rep);
832 DO_STRUCT_REQ_REP(WINBINDD_SETPWENT, &req, &rep);
834 return true;
837 static bool torture_winbind_struct_getpwent(struct torture_context *torture)
839 struct winbindd_request req;
840 struct winbindd_response rep;
841 struct winbindd_pw *pwent;
843 torture_comment(torture, "Running WINBINDD_GETPWENT (struct based)\n");
845 torture_comment(torture, " - Running WINBINDD_SETPWENT first\n");
846 ZERO_STRUCT(req);
847 ZERO_STRUCT(rep);
848 DO_STRUCT_REQ_REP(WINBINDD_SETPWENT, &req, &rep);
850 torture_comment(torture, " - Running WINBINDD_GETPWENT now\n");
851 ZERO_STRUCT(req);
852 ZERO_STRUCT(rep);
853 req.data.num_entries = 1;
854 if (torture_setting_bool(torture, "samba3", false)) {
855 bool __noop = false;
856 DO_STRUCT_REQ_REP_EXT(WINBINDD_GETPWENT, &req, &rep,
857 NSS_STATUS_SUCCESS, false, __noop=true,
858 NULL);
859 } else {
860 DO_STRUCT_REQ_REP(WINBINDD_GETPWENT, &req, &rep);
862 pwent = (struct winbindd_pw *)rep.extra_data.data;
863 if (!torture_setting_bool(torture, "samba3", false)) {
864 torture_assert(torture, (pwent != NULL), "NULL pwent");
866 if (pwent) {
867 torture_comment(torture, "name: %s, uid: %d, gid: %d, shell: %s\n",
868 pwent->pw_name, pwent->pw_uid, pwent->pw_gid,
869 pwent->pw_shell);
872 return true;
875 static bool torture_winbind_struct_endpwent(struct torture_context *torture)
877 struct winbindd_request req;
878 struct winbindd_response rep;
880 torture_comment(torture, "Running WINBINDD_ENDPWENT (struct based)\n");
882 ZERO_STRUCT(req);
883 ZERO_STRUCT(rep);
885 DO_STRUCT_REQ_REP(WINBINDD_ENDPWENT, &req, &rep);
887 return true;
890 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
891 form DOMAIN/user into a domain and a user */
893 static bool parse_domain_user(struct torture_context *torture,
894 const char *domuser, fstring domain,
895 fstring user)
897 char *p = strchr(domuser, winbind_separator(torture));
898 char *dom;
900 if (!p) {
901 /* Maybe it was a UPN? */
902 if ((p = strchr(domuser, '@')) != NULL) {
903 fstrcpy(domain, "");
904 fstrcpy(user, domuser);
905 return true;
908 fstrcpy(user, domuser);
909 get_winbind_domain(torture, &dom);
910 fstrcpy(domain, dom);
911 return true;
914 fstrcpy(user, p+1);
915 fstrcpy(domain, domuser);
916 domain[PTR_DIFF(p, domuser)] = 0;
918 return true;
921 static bool lookup_name_sid_list(struct torture_context *torture, char **list)
923 uint32_t count;
925 for (count = 0; list[count]; count++) {
926 struct winbindd_request req;
927 struct winbindd_response rep;
928 char *sid;
929 char *name;
930 const char *domain_name = torture_setting_string(torture,
931 "winbindd_netbios_domain",
932 lpcfg_workgroup(torture->lp_ctx));
934 ZERO_STRUCT(req);
935 ZERO_STRUCT(rep);
937 parse_domain_user(torture, list[count], req.data.name.dom_name,
938 req.data.name.name);
940 DO_STRUCT_REQ_REP(WINBINDD_LOOKUPNAME, &req, &rep);
942 sid = talloc_strdup(torture, rep.data.sid.sid);
944 ZERO_STRUCT(req);
945 ZERO_STRUCT(rep);
947 fstrcpy(req.data.sid, sid);
949 DO_STRUCT_REQ_REP(WINBINDD_LOOKUPSID, &req, &rep);
951 if (strequal(rep.data.name.dom_name, domain_name)) {
952 name = talloc_asprintf(torture, "%s",
953 rep.data.name.name);
954 } else {
955 name = talloc_asprintf(torture, "%s%c%s",
956 rep.data.name.dom_name,
957 winbind_separator(torture),
958 rep.data.name.name);
961 torture_assert_casestr_equal(torture, list[count], name,
962 "LOOKUP_SID after LOOKUP_NAME != id");
964 #if 0
965 torture_comment(torture, " %s -> %s -> %s\n", list[count],
966 sid, name);
967 #endif
969 talloc_free(sid);
970 talloc_free(name);
973 return true;
976 static bool name_is_in_list(const char *name, const char **list)
978 uint32_t count;
980 for (count = 0; list && list[count]; count++) {
981 if (strequal(name, list[count])) {
982 return true;
985 return false;
988 static bool torture_winbind_struct_lookup_name_sid(struct torture_context *torture)
990 struct winbindd_request req;
991 struct winbindd_response rep;
992 const char *invalid_sid = "S-0-0-7";
993 char *domain;
994 const char *invalid_user = "noone";
995 char *invalid_name;
996 bool strict = torture_setting_bool(torture, "strict mode", false);
997 char **users;
998 char **groups;
999 uint32_t count, num_groups;
1000 bool ok;
1002 torture_comment(torture, "Running WINBINDD_LOOKUP_NAME_SID (struct based)\n");
1004 ok = get_user_list(torture, &users);
1005 torture_assert(torture, ok, "failed to retrieve list of users");
1006 lookup_name_sid_list(torture, users);
1008 ok = get_group_list(torture, &num_groups, &groups);
1009 torture_assert(torture, ok, "failed to retrieve list of groups");
1010 if (num_groups > 0) {
1011 lookup_name_sid_list(torture, groups);
1014 ZERO_STRUCT(req);
1015 ZERO_STRUCT(rep);
1017 fstrcpy(req.data.sid, invalid_sid);
1019 ok = true;
1020 DO_STRUCT_REQ_REP_EXT(WINBINDD_LOOKUPSID, &req, &rep,
1021 NSS_STATUS_NOTFOUND,
1022 strict,
1023 ok=false,
1024 talloc_asprintf(torture,
1025 "invalid sid %s was resolved",
1026 invalid_sid));
1028 ZERO_STRUCT(req);
1029 ZERO_STRUCT(rep);
1031 /* try to find an invalid name... */
1033 count = 0;
1034 get_winbind_domain(torture, &domain);
1035 do {
1036 count++;
1037 invalid_name = talloc_asprintf(torture, "%s/%s%u",
1038 domain,
1039 invalid_user, count);
1040 } while(name_is_in_list(invalid_name, (const char **)users) ||
1041 name_is_in_list(invalid_name, (const char **)groups));
1043 fstrcpy(req.data.name.dom_name, domain);
1044 fstrcpy(req.data.name.name,
1045 talloc_asprintf(torture, "%s%u", invalid_user,
1046 count));
1048 ok = true;
1049 DO_STRUCT_REQ_REP_EXT(WINBINDD_LOOKUPNAME, &req, &rep,
1050 NSS_STATUS_NOTFOUND,
1051 strict,
1052 ok=false,
1053 talloc_asprintf(torture,
1054 "invalid name %s was resolved",
1055 invalid_name));
1057 talloc_free(users);
1058 talloc_free(groups);
1060 return true;
1063 struct torture_suite *torture_winbind_struct_init(void)
1065 struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "struct");
1067 torture_suite_add_simple_test(suite, "interface_version", torture_winbind_struct_interface_version);
1068 torture_suite_add_simple_test(suite, "ping", torture_winbind_struct_ping);
1069 torture_suite_add_simple_test(suite, "info", torture_winbind_struct_info);
1070 torture_suite_add_simple_test(suite, "priv_pipe_dir", torture_winbind_struct_priv_pipe_dir);
1071 torture_suite_add_simple_test(suite, "netbios_name", torture_winbind_struct_netbios_name);
1072 torture_suite_add_simple_test(suite, "domain_name", torture_winbind_struct_domain_name);
1073 torture_suite_add_simple_test(suite, "check_machacc", torture_winbind_struct_check_machacc);
1074 torture_suite_add_simple_test(suite, "list_trustdom", torture_winbind_struct_list_trustdom);
1075 torture_suite_add_simple_test(suite, "domain_info", torture_winbind_struct_domain_info);
1076 torture_suite_add_simple_test(suite, "getdcname", torture_winbind_struct_getdcname);
1077 torture_suite_add_simple_test(suite, "dsgetdcname", torture_winbind_struct_dsgetdcname);
1078 torture_suite_add_simple_test(suite, "list_users", torture_winbind_struct_list_users);
1079 torture_suite_add_simple_test(suite, "list_groups", torture_winbind_struct_list_groups);
1080 torture_suite_add_simple_test(suite, "show_sequence", torture_winbind_struct_show_sequence);
1081 torture_suite_add_simple_test(suite, "setpwent", torture_winbind_struct_setpwent);
1082 torture_suite_add_simple_test(suite, "getpwent", torture_winbind_struct_getpwent);
1083 torture_suite_add_simple_test(suite, "endpwent", torture_winbind_struct_endpwent);
1084 torture_suite_add_simple_test(suite, "lookup_name_sid", torture_winbind_struct_lookup_name_sid);
1086 suite->description = talloc_strdup(suite, "WINBIND - struct based protocol tests");
1088 return suite;