r25290: remove wrong comment
[kai/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
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "pstring.h"
22 #include "torture/torture.h"
23 #include "torture/winbind/proto.h"
24 #include "nsswitch/winbind_client.h"
25 #include "libcli/security/security.h"
26 #include "param/param.h"
27
28 #define DO_STRUCT_REQ_REP_EXT(op,req,rep,expected,strict,warnaction,cmt) do { \
29         NSS_STATUS __got, __expected = (expected); \
30         __got = winbindd_request_response(op, req, rep); \
31         if (__got != __expected) { \
32                 const char *__cmt = (cmt); \
33                 if (strict) { \
34                         torture_result(torture, TORTURE_FAIL, \
35                                 __location__ ": " __STRING(op) \
36                                 " returned %d, expected %d%s%s", \
37                                 __got, __expected, \
38                                 (__cmt) ? ": " : "", \
39                                 (__cmt) ? (__cmt) : ""); \
40                         return false; \
41                 } else { \
42                         torture_warning(torture, \
43                                 __location__ ": " __STRING(op) \
44                                 " returned %d, expected %d%s%s", \
45                                 __got, __expected, \
46                                 (__cmt) ? ": " : "", \
47                                 (__cmt) ? (__cmt) : ""); \
48                         warnaction; \
49                 } \
50         } \
51 } while(0)
52
53 #define DO_STRUCT_REQ_REP(op,req,rep) do { \
54         bool __noop = false; \
55         DO_STRUCT_REQ_REP_EXT(op,req,rep,NSS_STATUS_SUCCESS,true,__noop=true,NULL); \
56 } while (0)
57
58 static bool torture_winbind_struct_interface_version(struct torture_context *torture)
59 {
60         struct winbindd_request req;
61         struct winbindd_response rep;
62
63         ZERO_STRUCT(req);
64         ZERO_STRUCT(rep);
65
66         torture_comment(torture, "Running WINBINDD_INTERFACE_VERSION (struct based)\n");
67
68         DO_STRUCT_REQ_REP(WINBINDD_INTERFACE_VERSION, &req, &rep);
69
70         torture_assert_int_equal(torture,
71                                  rep.data.interface_version,
72                                  WINBIND_INTERFACE_VERSION,
73                                  "winbind server and client doesn't match");
74
75         return true;
76 }
77
78 static bool torture_winbind_struct_ping(struct torture_context *torture)
79 {
80         struct timeval tv = timeval_current();
81         int timelimit = torture_setting_int(torture, "timelimit", 5);
82         uint32_t total = 0;
83
84         torture_comment(torture,
85                         "Running WINBINDD_PING (struct based) for %d seconds\n",
86                         timelimit);
87
88         while (timeval_elapsed(&tv) < timelimit) {
89                 DO_STRUCT_REQ_REP(WINBINDD_PING, NULL, NULL);
90                 total++;
91         }
92
93         torture_comment(torture,
94                         "%u (%.1f/s) WINBINDD_PING (struct based)\n",
95                         total, total / timeval_elapsed(&tv));
96
97         return true;
98 }
99
100 static bool torture_winbind_struct_info(struct torture_context *torture)
101 {
102         struct winbindd_response rep;
103         const char *separator;
104
105         ZERO_STRUCT(rep);
106
107         torture_comment(torture, "Running WINBINDD_INFO (struct based)\n");
108
109         DO_STRUCT_REQ_REP(WINBINDD_INFO, NULL, &rep);
110
111         separator = torture_setting_string(torture,
112                                            "winbindd separator",
113                                            lp_winbind_separator());
114         torture_assert_int_equal(torture,
115                                  rep.data.info.winbind_separator,
116                                  *separator,
117                                  "winbind separator doesn't match");
118
119         torture_comment(torture, "Samba Version '%s'\n",
120                         rep.data.info.samba_version);
121
122         return true;
123 }
124
125 static bool torture_winbind_struct_netbios_name(struct torture_context *torture)
126 {
127         struct winbindd_request req;
128         struct winbindd_response rep;
129         const char *expected;
130
131         ZERO_STRUCT(req);
132         ZERO_STRUCT(rep);
133
134         torture_comment(torture, "Running WINBINDD_NETBIOS_NAME (struct based)\n");
135
136         DO_STRUCT_REQ_REP(WINBINDD_NETBIOS_NAME, &req, &rep);
137
138         expected = torture_setting_string(torture,
139                                           "winbindd netbios name",
140                                           lp_netbios_name());
141
142         torture_assert_str_equal(torture,
143                                  rep.data.netbios_name, expected,
144                                  "winbindd's netbios name doesn't match");
145
146         return true;
147 }
148
149 static bool torture_winbind_struct_domain_name(struct torture_context *torture)
150 {
151         struct winbindd_request req;
152         struct winbindd_response rep;
153         const char *expected;
154
155         ZERO_STRUCT(req);
156         ZERO_STRUCT(rep);
157
158         torture_comment(torture, "Running WINBINDD_DOMAIN_NAME (struct based)\n");
159
160         DO_STRUCT_REQ_REP(WINBINDD_DOMAIN_NAME, &req, &rep);
161
162         expected = torture_setting_string(torture,
163                                           "winbindd netbios domain",
164                                           lp_workgroup());
165
166         torture_assert_str_equal(torture,
167                                  rep.data.domain_name, expected,
168                                  "winbindd's netbios domain doesn't match");
169
170         return true;
171 }
172
173 struct torture_trust_domain {
174         const char *netbios_name;
175         const char *dns_name;
176         struct dom_sid *sid;
177 };
178
179 static bool get_trusted_domains(struct torture_context *torture,
180                                 struct torture_trust_domain **_d)
181 {
182         struct winbindd_request req;
183         struct winbindd_response rep;
184         struct torture_trust_domain *d = NULL;
185         uint32_t dcount = 0;
186         fstring line;
187         const char *extra_data;
188
189         ZERO_STRUCT(req);
190         ZERO_STRUCT(rep);
191
192         DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
193
194         extra_data = (char *)rep.extra_data.data;
195         torture_assert(torture, extra_data, "NULL trust list");
196
197         while (next_token(&extra_data, line, "\n", sizeof(fstring))) {
198                 char *p, *lp;
199
200                 d = talloc_realloc(torture, d,
201                                    struct torture_trust_domain,
202                                    dcount + 2);
203                 ZERO_STRUCT(d[dcount+1]);
204
205                 lp = line;
206                 p = strchr(lp, '\\');
207                 torture_assert(torture, p, "missing 1st '\\' in line");
208                 *p = 0;
209                 d[dcount].netbios_name = talloc_strdup(d, lp);
210                 torture_assert(torture, strlen(d[dcount].netbios_name) > 0,
211                                "empty netbios_name");
212
213                 lp = p+1;
214                 p = strchr(lp, '\\');
215                 torture_assert(torture, p, "missing 2nd '\\' in line");
216                 *p = 0;
217                 d[dcount].dns_name = talloc_strdup(d, lp);
218                 /* it's ok to have an empty dns_name */
219
220                 lp = p+1;
221                 d[dcount].sid = dom_sid_parse_talloc(d, lp);
222                 torture_assert(torture, d[dcount].sid,
223                                "failed to parse sid");
224
225                 dcount++;
226         }
227         SAFE_FREE(rep.extra_data.data);
228
229         torture_assert(torture, dcount >= 2,
230                        "The list of trusted domain should contain 2 entries");
231
232         *_d = d;
233         return true;
234 }
235
236 static bool torture_winbind_struct_list_trustdom(struct torture_context *torture)
237 {
238         struct winbindd_request req;
239         struct winbindd_response rep;
240         char *list1;
241         char *list2;
242         bool ok;
243         struct torture_trust_domain *listd = NULL;
244         uint32_t i;
245
246         torture_comment(torture, "Running WINBINDD_LIST_TRUSTDOM (struct based)\n");
247
248         ZERO_STRUCT(req);
249         ZERO_STRUCT(rep);
250
251         req.data.list_all_domains = false;
252
253         DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
254
255         list1 = (char *)rep.extra_data.data;
256         torture_assert(torture, list1, "NULL trust list");
257
258         torture_comment(torture, "%s\n", list1);
259
260         ZERO_STRUCT(req);
261         ZERO_STRUCT(rep);
262
263         req.data.list_all_domains = true;
264
265         DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
266
267         list2 = (char *)rep.extra_data.data;
268         torture_assert(torture, list2, "NULL trust list");
269
270         /*
271          * The list_all_domains parameter should be ignored
272          */
273         torture_assert_str_equal(torture, list2, list1, "list_all_domains not ignored");
274
275         SAFE_FREE(list1);
276         SAFE_FREE(list2);
277
278         ok = get_trusted_domains(torture, &listd);
279         torture_assert(torture, ok, "failed to get trust list");
280
281         for (i=0; listd[i].netbios_name; i++) {
282                 if (i == 0) {
283                         struct dom_sid *builtin_sid;
284
285                         builtin_sid = dom_sid_parse_talloc(torture, SID_BUILTIN);
286
287                         torture_assert_str_equal(torture,
288                                                  listd[i].netbios_name,
289                                                  NAME_BUILTIN,
290                                                  "first domain should be 'BUILTIN'");
291
292                         torture_assert_str_equal(torture,
293                                                  listd[i].dns_name,
294                                                  "",
295                                                  "BUILTIN domain should not have a dns name");
296
297                         ok = dom_sid_equal(builtin_sid,
298                                            listd[i].sid);
299                         torture_assert(torture, ok, "BUILTIN domain should have S-1-5-32");
300
301                         continue;
302                 }
303
304                 /*
305                  * TODO: verify the content of the 2nd and 3rd (in member server mode)
306                  *       domain entries
307                  */
308         }
309
310         return true;
311 }
312
313 static bool torture_winbind_struct_domain_info(struct torture_context *torture)
314 {
315         bool ok;
316         struct torture_trust_domain *listd = NULL;
317         uint32_t i;
318
319         torture_comment(torture, "Running WINBINDD_DOMAIN_INFO (struct based)\n");
320
321         ok = get_trusted_domains(torture, &listd);
322         torture_assert(torture, ok, "failed to get trust list");
323
324         for (i=0; listd[i].netbios_name; i++) {
325                 struct winbindd_request req;
326                 struct winbindd_response rep;
327                 struct dom_sid *sid;
328                 char *flagstr = talloc_strdup(torture," ");
329
330                 ZERO_STRUCT(req);
331                 ZERO_STRUCT(rep);
332
333                 fstrcpy(req.domain_name, listd[i].netbios_name);
334
335                 DO_STRUCT_REQ_REP(WINBINDD_DOMAIN_INFO, &req, &rep);
336
337                 torture_assert_str_equal(torture,
338                                          rep.data.domain_info.name,
339                                          listd[i].netbios_name,
340                                          "Netbios domain name doesn't match");
341
342                 torture_assert_str_equal(torture,
343                                          rep.data.domain_info.alt_name,
344                                          listd[i].dns_name,
345                                          "DNS domain name doesn't match");
346
347                 sid = dom_sid_parse_talloc(torture, rep.data.domain_info.sid);
348                 torture_assert(torture, sid, "Failed to parse SID");
349
350                 ok = dom_sid_equal(listd[i].sid, sid);
351                 torture_assert(torture, ok, "SID's doesn't match");
352
353                 if (rep.data.domain_info.primary) {
354                         flagstr = talloc_strdup_append(flagstr, "PR ");
355                 }
356
357                 if (rep.data.domain_info.active_directory) {
358                         torture_assert(torture,
359                                        strlen(rep.data.domain_info.alt_name)>0,
360                                        "Active Directory without DNS name");
361                         flagstr = talloc_strdup_append(flagstr, "AD ");
362                 }
363
364                 if (rep.data.domain_info.native_mode) {
365                         torture_assert(torture,
366                                        rep.data.domain_info.active_directory,
367                                        "Native-Mode, but no Active Directory");
368                         flagstr = talloc_strdup_append(flagstr, "NA ");
369                 }
370
371                 torture_comment(torture, "DOMAIN '%s' => '%s' [%s]\n",
372                                 rep.data.domain_info.name,
373                                 rep.data.domain_info.alt_name,
374                                 flagstr);
375         }
376
377         return true;
378 }
379
380 static bool torture_winbind_struct_getdcname(struct torture_context *torture)
381 {
382         bool ok;
383         bool strict = torture_setting_bool(torture, "strict mode", false);
384         struct torture_trust_domain *listd = NULL;
385         uint32_t i;
386
387         torture_comment(torture, "Running WINBINDD_GETDCNAME (struct based)\n");
388
389         ok = get_trusted_domains(torture, &listd);
390         torture_assert(torture, ok, "failed to get trust list");
391
392         for (i=0; listd[i].netbios_name; i++) {
393                 struct winbindd_request req;
394                 struct winbindd_response rep;
395
396                 ZERO_STRUCT(req);
397                 ZERO_STRUCT(rep);
398
399                 fstrcpy(req.domain_name, listd[i].netbios_name);
400
401                 ok = true;
402                 DO_STRUCT_REQ_REP_EXT(WINBINDD_GETDCNAME, &req, &rep,
403                                       NSS_STATUS_SUCCESS,
404                                       (i <2 || strict), ok = false,
405                                       talloc_asprintf(torture, "DOMAIN '%s'",
406                                                       req.domain_name));
407                 if (!ok) continue;
408
409                 /* TODO: check rep.data.dc_name; */
410                 torture_comment(torture, "DOMAIN '%s' => DCNAME '%s'\n",
411                                 req.domain_name, rep.data.dc_name);
412         }
413
414         return true;
415 }
416
417 struct torture_suite *torture_winbind_struct_init(void)
418 {
419         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "STRUCT");
420
421         torture_suite_add_simple_test(suite, "INTERFACE_VERSION", torture_winbind_struct_interface_version);
422         torture_suite_add_simple_test(suite, "PING", torture_winbind_struct_ping);
423         torture_suite_add_simple_test(suite, "INFO", torture_winbind_struct_info);
424         torture_suite_add_simple_test(suite, "NETBIOS_NAME", torture_winbind_struct_netbios_name);
425         torture_suite_add_simple_test(suite, "DOMAIN_NAME", torture_winbind_struct_domain_name);
426         torture_suite_add_simple_test(suite, "LIST_TRUSTDOM", torture_winbind_struct_list_trustdom);
427         torture_suite_add_simple_test(suite, "DOMAIN_INFO", torture_winbind_struct_domain_info);
428         torture_suite_add_simple_test(suite, "GETDCNAME", torture_winbind_struct_getdcname);
429
430         suite->description = talloc_strdup(suite, "WINBIND - struct based protocol tests");
431
432         return suite;
433 }