577b7dbcab836cb4349bc9ebeb5a5694db9514c2
[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_netbios_name(struct torture_context *torture)
101 {
102         struct winbindd_request req;
103         struct winbindd_response rep;
104         const char *expected;
105
106         ZERO_STRUCT(req);
107         ZERO_STRUCT(rep);
108
109         torture_comment(torture, "Running WINBINDD_NETBIOS_NAME (struct based)\n");
110
111         DO_STRUCT_REQ_REP(WINBINDD_NETBIOS_NAME, &req, &rep);
112
113         expected = torture_setting_string(torture,
114                                           "winbindd netbios name",
115                                           lp_netbios_name());
116
117         torture_assert_str_equal(torture,
118                                  rep.data.netbios_name, expected,
119                                  "winbindd's netbios name doesn't match");
120
121         return true;
122 }
123
124 static bool torture_winbind_struct_domain_name(struct torture_context *torture)
125 {
126         struct winbindd_request req;
127         struct winbindd_response rep;
128         const char *expected;
129
130         ZERO_STRUCT(req);
131         ZERO_STRUCT(rep);
132
133         torture_comment(torture, "Running WINBINDD_DOMAIN_NAME (struct based)\n");
134
135         DO_STRUCT_REQ_REP(WINBINDD_DOMAIN_NAME, &req, &rep);
136
137         expected = torture_setting_string(torture,
138                                           "winbindd netbios domain",
139                                           lp_workgroup());
140
141         torture_assert_str_equal(torture,
142                                  rep.data.domain_name, expected,
143                                  "winbindd's netbios domain doesn't match");
144
145         return true;
146 }
147
148 struct torture_trust_domain {
149         const char *netbios_name;
150         const char *dns_name;
151         struct dom_sid *sid;
152 };
153
154 static bool get_trusted_domains(struct torture_context *torture,
155                                 struct torture_trust_domain **_d)
156 {
157         struct winbindd_request req;
158         struct winbindd_response rep;
159         struct torture_trust_domain *d = NULL;
160         uint32_t dcount = 0;
161         fstring line;
162         const char *extra_data;
163
164         ZERO_STRUCT(req);
165         ZERO_STRUCT(rep);
166
167         DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
168
169         extra_data = (char *)rep.extra_data.data;
170         torture_assert(torture, extra_data, "NULL trust list");
171
172         while (next_token(&extra_data, line, "\n", sizeof(fstring))) {
173                 char *p, *lp;
174
175                 d = talloc_realloc(torture, d,
176                                    struct torture_trust_domain,
177                                    dcount + 2);
178                 ZERO_STRUCT(d[dcount+1]);
179
180                 lp = line;
181                 p = strchr(lp, '\\');
182                 torture_assert(torture, p, "missing 1st '\\' in line");
183                 *p = 0;
184                 d[dcount].netbios_name = talloc_strdup(d, lp);
185                 torture_assert(torture, strlen(d[dcount].netbios_name) > 0,
186                                "empty netbios_name");
187
188                 lp = p+1;
189                 p = strchr(lp, '\\');
190                 torture_assert(torture, p, "missing 2nd '\\' in line");
191                 *p = 0;
192                 d[dcount].dns_name = talloc_strdup(d, lp);
193                 /* it's ok to have an empty dns_name */
194
195                 lp = p+1;
196                 d[dcount].sid = dom_sid_parse_talloc(d, lp);
197                 torture_assert(torture, d[dcount].sid,
198                                "failed to parse sid");
199
200                 dcount++;
201         }
202         SAFE_FREE(rep.extra_data.data);
203
204         torture_assert(torture, dcount >= 2,
205                        "The list of trusted domain should contain 2 entries");
206
207         *_d = d;
208         return true;
209 }
210
211 static bool torture_winbind_struct_list_trustdom(struct torture_context *torture)
212 {
213         struct winbindd_request req;
214         struct winbindd_response rep;
215         char *list1;
216         char *list2;
217         bool ok;
218         struct torture_trust_domain *listd = NULL;
219         uint32_t i;
220
221         torture_comment(torture, "Running WINBINDD_LIST_TRUSTDOM (struct based)\n");
222
223         ZERO_STRUCT(req);
224         ZERO_STRUCT(rep);
225
226         req.data.list_all_domains = false;
227
228         DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
229
230         list1 = (char *)rep.extra_data.data;
231         torture_assert(torture, list1, "NULL trust list");
232
233         torture_comment(torture, "%s\n", list1);
234
235         ZERO_STRUCT(req);
236         ZERO_STRUCT(rep);
237
238         req.data.list_all_domains = true;
239
240         DO_STRUCT_REQ_REP(WINBINDD_LIST_TRUSTDOM, &req, &rep);
241
242         list2 = (char *)rep.extra_data.data;
243         torture_assert(torture, list2, "NULL trust list");
244
245         /*
246          * The list_all_domains parameter should be ignored
247          */
248         torture_assert_str_equal(torture, list2, list1, "list_all_domains not ignored");
249
250         SAFE_FREE(list1);
251         SAFE_FREE(list2);
252
253         ok = get_trusted_domains(torture, &listd);
254         torture_assert(torture, ok, "failed to get trust list");
255
256         for (i=0; listd[i].netbios_name; i++) {
257                 if (i == 0) {
258                         struct dom_sid *builtin_sid;
259
260                         builtin_sid = dom_sid_parse_talloc(torture, SID_BUILTIN);
261
262                         torture_assert_str_equal(torture,
263                                                  listd[i].netbios_name,
264                                                  NAME_BUILTIN,
265                                                  "first domain should be 'BUILTIN'");
266
267                         torture_assert_str_equal(torture,
268                                                  listd[i].dns_name,
269                                                  "",
270                                                  "BUILTIN domain should not have a dns name");
271
272                         ok = dom_sid_equal(builtin_sid,
273                                            listd[i].sid);
274                         torture_assert(torture, ok, "BUILTIN domain should have S-1-5-32");
275
276                         continue;
277                 }
278
279                 /*
280                  * TODO: verify the content of the 2nd and 3rd (in member server mode)
281                  *       domain entries
282                  */
283         }
284
285         return true;
286 }
287
288 static bool torture_winbind_struct_domain_info(struct torture_context *torture)
289 {
290         bool ok;
291         struct torture_trust_domain *listd = NULL;
292         uint32_t i;
293
294         torture_comment(torture, "Running WINBINDD_DOMAIN_INFO (struct based)\n");
295
296         ok = get_trusted_domains(torture, &listd);
297         torture_assert(torture, ok, "failed to get trust list");
298
299         for (i=0; listd[i].netbios_name; i++) {
300                 struct winbindd_request req;
301                 struct winbindd_response rep;
302                 struct dom_sid *sid;
303                 char *flagstr = talloc_strdup(torture," ");
304
305                 ZERO_STRUCT(req);
306                 ZERO_STRUCT(rep);
307
308                 fstrcpy(req.domain_name, listd[i].netbios_name);
309
310                 DO_STRUCT_REQ_REP(WINBINDD_DOMAIN_INFO, &req, &rep);
311
312                 torture_assert_str_equal(torture,
313                                          rep.data.domain_info.name,
314                                          listd[i].netbios_name,
315                                          "Netbios domain name doesn't match");
316
317                 torture_assert_str_equal(torture,
318                                          rep.data.domain_info.alt_name,
319                                          listd[i].dns_name,
320                                          "DNS domain name doesn't match");
321
322                 sid = dom_sid_parse_talloc(torture, rep.data.domain_info.sid);
323                 torture_assert(torture, sid, "Failed to parse SID");
324
325                 ok = dom_sid_equal(listd[i].sid, sid);
326                 torture_assert(torture, ok, "SID's doesn't match");
327
328                 if (rep.data.domain_info.primary) {
329                         flagstr = talloc_strdup_append(flagstr, "PR ");
330                 }
331
332                 if (rep.data.domain_info.active_directory) {
333                         torture_assert(torture,
334                                        strlen(rep.data.domain_info.alt_name)>0,
335                                        "Active Directory without DNS name");
336                         flagstr = talloc_strdup_append(flagstr, "AD ");
337                 }
338
339                 if (rep.data.domain_info.native_mode) {
340                         torture_assert(torture,
341                                        rep.data.domain_info.active_directory,
342                                        "Native-Mode, but no Active Directory");
343                         flagstr = talloc_strdup_append(flagstr, "NA ");
344                 }
345
346                 /* TODO: check rep.data.dc_name; */
347                 torture_comment(torture, "DOMAIN '%s' => '%s' [%s]\n",
348                                 rep.data.domain_info.name,
349                                 rep.data.domain_info.alt_name,
350                                 flagstr);
351         }
352
353         return true;
354 }
355
356 static bool torture_winbind_struct_getdcname(struct torture_context *torture)
357 {
358         bool ok;
359         bool strict = torture_setting_bool(torture, "strict mode", false);
360         struct torture_trust_domain *listd = NULL;
361         uint32_t i;
362
363         torture_comment(torture, "Running WINBINDD_GETDCNAME (struct based)\n");
364
365         ok = get_trusted_domains(torture, &listd);
366         torture_assert(torture, ok, "failed to get trust list");
367
368         for (i=0; listd[i].netbios_name; i++) {
369                 struct winbindd_request req;
370                 struct winbindd_response rep;
371
372                 ZERO_STRUCT(req);
373                 ZERO_STRUCT(rep);
374
375                 fstrcpy(req.domain_name, listd[i].netbios_name);
376
377                 ok = true;
378                 DO_STRUCT_REQ_REP_EXT(WINBINDD_GETDCNAME, &req, &rep,
379                                       NSS_STATUS_SUCCESS,
380                                       (i <2 || strict), ok = false,
381                                       talloc_asprintf(torture, "DOMAIN '%s'",
382                                                       req.domain_name));
383                 if (!ok) continue;
384
385                 /* TODO: check rep.data.dc_name; */
386                 torture_comment(torture, "DOMAIN '%s' => DCNAME '%s'\n",
387                                 req.domain_name, rep.data.dc_name);
388         }
389
390         return true;
391 }
392
393 struct torture_suite *torture_winbind_struct_init(void)
394 {
395         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "STRUCT");
396
397         torture_suite_add_simple_test(suite, "INTERFACE_VERSION", torture_winbind_struct_interface_version);
398         torture_suite_add_simple_test(suite, "PING", torture_winbind_struct_ping);
399         torture_suite_add_simple_test(suite, "NETBIOS_NAME", torture_winbind_struct_netbios_name);
400         torture_suite_add_simple_test(suite, "DOMAIN_NAME", torture_winbind_struct_domain_name);
401         torture_suite_add_simple_test(suite, "LIST_TRUSTDOM", torture_winbind_struct_list_trustdom);
402         torture_suite_add_simple_test(suite, "DOMAIN_INFO", torture_winbind_struct_domain_info);
403         torture_suite_add_simple_test(suite, "GETDCNAME", torture_winbind_struct_getdcname);
404
405         suite->description = talloc_strdup(suite, "WINBIND - struct based protocol tests");
406
407         return suite;
408 }