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