c5428afac43be61311116bed591e9d3a8fcf23ed
[sfrench/samba-autobuild/.git] / nsswitch / libwbclient / tests / wbclient.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Guenther Deschner 2009-2010
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 "lib/replace/replace.h"
21 #include "libcli/util/ntstatus.h"
22 #include "libcli/util/werror.h"
23 #include "lib/util/data_blob.h"
24 #include "lib/util/time.h"
25 #include "libcli/resolve/resolve.h"
26 #include "nsswitch/libwbclient/wbclient.h"
27 #include "torture/smbtorture.h"
28 #include "torture/winbind/proto.h"
29 #include "lib/util/util_net.h"
30 #include "lib/util/charset/charset.h"
31 #include "libcli/auth/libcli_auth.h"
32 #include "lib/param/param.h"
33 #include "lib/util/samba_util.h"
34 #include "lib/crypto/arcfour.h"
35 #include "auth/credentials/credentials.h"
36 #include "lib/cmdline/popt_common.h"
37
38 #define WBC_ERROR_EQUAL(x,y) (x == y)
39
40 #define torture_assert_wbc_equal(torture_ctx, got, expected, cmt, cmt_arg)      \
41         do { wbcErr __got = got, __expected = expected; \
42         if (!WBC_ERROR_EQUAL(__got, __expected)) { \
43                 torture_result(torture_ctx, TORTURE_FAIL, __location__": "#got" was %s, expected %s: " cmt, wbcErrorString(__got), wbcErrorString(__expected), cmt_arg); \
44                 return false; \
45         } \
46         } while (0)
47
48 #define torture_assert_wbc_ok(torture_ctx,expr,cmt,cmt_arg)                     \
49         torture_assert_wbc_equal(torture_ctx,expr,WBC_ERR_SUCCESS,cmt,cmt_arg)
50
51 #define torture_assert_wbc_equal_goto_fail(torture_ctx, got, expected, cmt, cmt_arg)    \
52         do { wbcErr __got = got, __expected = expected; \
53         if (!WBC_ERROR_EQUAL(__got, __expected)) { \
54                 torture_result(torture_ctx, TORTURE_FAIL, __location__": "#got" was %s, expected %s: " cmt, wbcErrorString(__got), wbcErrorString(__expected), cmt_arg); \
55                 goto fail;                                              \
56         } \
57         } while (0)
58
59 #define torture_assert_wbc_ok_goto_fail(torture_ctx,expr,cmt,cmt_arg)                   \
60         torture_assert_wbc_equal_goto_fail(torture_ctx,expr,WBC_ERR_SUCCESS,cmt,cmt_arg)
61
62 #define torture_assert_str_equal_goto_fail(torture_ctx,got,expected,cmt)\
63         do { const char *__got = (got), *__expected = (expected); \
64         if (strcmp(__got, __expected) != 0) { \
65                 torture_result(torture_ctx, TORTURE_FAIL, \
66                         __location__": "#got" was %s, expected %s: %s", \
67                         __got, __expected, cmt); \
68                 goto fail;;                      \
69         } \
70         } while(0)
71
72 static bool test_wbc_ping(struct torture_context *tctx)
73 {
74         torture_assert_wbc_ok(tctx, wbcPing(),
75                 "%s", "wbcPing failed");
76
77         return true;
78 }
79
80 static bool test_wbc_pingdc(struct torture_context *tctx)
81 {
82         struct wbcInterfaceDetails *details = NULL;
83         wbcErr ret = false;
84
85         torture_assert_wbc_equal_goto_fail(tctx,
86                                            wbcPingDc("random_string", NULL),
87                                            WBC_ERR_DOMAIN_NOT_FOUND,
88                                            "%s",
89                                            "wbcPingDc failed");
90         torture_assert_wbc_ok_goto_fail(tctx, wbcPingDc(NULL, NULL),
91                 "%s", "wbcPingDc failed");
92
93         torture_assert_wbc_ok_goto_fail(tctx, wbcInterfaceDetails(&details),
94                 "%s", "wbcInterfaceDetails failed");
95         torture_assert_goto(tctx, details, ret, fail,
96                        "wbcInterfaceDetails returned NULL pointer");
97         torture_assert_goto(tctx, details->netbios_domain, ret, fail,
98                        "wbcInterfaceDetails returned NULL netbios_domain");
99
100         torture_assert_wbc_ok_goto_fail(tctx,
101                                         wbcPingDc(details->netbios_domain, NULL),
102                                         "wbcPingDc(%s) failed",
103                                         details->netbios_domain);
104
105         torture_assert_wbc_ok_goto_fail(tctx,
106                                         wbcPingDc("BUILTIN", NULL),
107                                         "%s",
108                                         "wbcPingDc(BUILTIN) failed");
109
110         ret = true;
111 fail:
112         wbcFreeMemory(details);
113         return ret;
114 }
115
116 static bool test_wbc_pingdc2(struct torture_context *tctx)
117 {
118         struct wbcInterfaceDetails *details = NULL;
119         char *name = NULL;
120         wbcErr ret = false;
121
122         torture_assert_wbc_equal_goto_fail(tctx,
123                                            wbcPingDc2("random_string", NULL, &name),
124                                            WBC_ERR_DOMAIN_NOT_FOUND,
125                                            "%s",
126                                            "wbcPingDc2 failed");
127         torture_assert_wbc_ok_goto_fail(tctx,
128                                         wbcPingDc2(NULL, NULL, &name),
129                                         "%s",
130                                         "wbcPingDc2 failed");
131         wbcFreeMemory(name);
132         name = NULL;
133
134         torture_assert_wbc_ok_goto_fail(tctx,
135                                         wbcInterfaceDetails(&details),
136                                         "%s",
137                                         "wbcInterfaceDetails failed");
138         torture_assert_goto(tctx,
139                             details,
140                             ret,
141                             fail,
142                             "wbcInterfaceDetails returned NULL pointer");
143         torture_assert_goto(tctx,
144                             details->netbios_domain,
145                             ret,
146                             fail,
147                             "wbcInterfaceDetails returned NULL netbios_domain");
148
149         torture_assert_wbc_ok_goto_fail(tctx,
150                                         wbcPingDc2(details->netbios_domain, NULL, &name),
151                                         "wbcPingDc2(%s) failed",
152                                         details->netbios_domain);
153         wbcFreeMemory(name);
154         name = NULL;
155
156         torture_assert_wbc_ok_goto_fail(tctx,
157                                         wbcPingDc2("BUILTIN", NULL, &name),
158                                         "%s",
159                                         "wbcPingDc2(BUILTIN) failed");
160
161         ret = true;
162 fail:
163         wbcFreeMemory(name);
164         wbcFreeMemory(details);
165
166         return ret;
167 }
168
169 static bool test_wbc_library_details(struct torture_context *tctx)
170 {
171         struct wbcLibraryDetails *details;
172
173         torture_assert_wbc_ok(tctx, wbcLibraryDetails(&details),
174                 "%s", "wbcLibraryDetails failed");
175         torture_assert(tctx, details,
176                 "wbcLibraryDetails returned NULL pointer");
177
178         wbcFreeMemory(details);
179
180         return true;
181 }
182
183 static bool test_wbc_interface_details(struct torture_context *tctx)
184 {
185         struct wbcInterfaceDetails *details;
186
187         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
188                 "%s", "wbcInterfaceDetails failed");
189         torture_assert(tctx, details,
190                        "wbcInterfaceDetails returned NULL pointer");
191
192         wbcFreeMemory(details);
193
194         return true;
195 }
196
197 static bool test_wbc_sidtypestring(struct torture_context *tctx)
198 {
199         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_USE_NONE),
200                                  "SID_NONE", "SID_NONE failed");
201         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_USER),
202                                  "SID_USER", "SID_USER failed");
203         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_DOM_GRP),
204                                  "SID_DOM_GROUP", "SID_DOM_GROUP failed");
205         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_DOMAIN),
206                                  "SID_DOMAIN", "SID_DOMAIN failed");
207         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_ALIAS),
208                                  "SID_ALIAS", "SID_ALIAS failed");
209         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_WKN_GRP),
210                                  "SID_WKN_GROUP", "SID_WKN_GROUP failed");
211         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_DELETED),
212                                  "SID_DELETED", "SID_DELETED failed");
213         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_INVALID),
214                                  "SID_INVALID", "SID_INVALID failed");
215         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_UNKNOWN),
216                                  "SID_UNKNOWN", "SID_UNKNOWN failed");
217         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_COMPUTER),
218                                  "SID_COMPUTER",  "SID_COMPUTER failed");
219         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_LABEL),
220                                  "SID_LABEL",  "SID_LABEL failed");
221         return true;
222 }
223
224 static bool test_wbc_sidtostring(struct torture_context *tctx)
225 {
226         struct wbcDomainSid sid;
227         const char *sid_string = "S-1-5-32";
228         char *sid_string2;
229
230         torture_assert_wbc_ok(tctx, wbcStringToSid(sid_string, &sid),
231                               "wbcStringToSid of %s failed", sid_string);
232         torture_assert_wbc_ok(tctx, wbcSidToString(&sid, &sid_string2),
233                               "wbcSidToString of %s failed", sid_string);
234         torture_assert_str_equal(tctx, sid_string, sid_string2,
235                 "sid strings differ");
236         wbcFreeMemory(sid_string2);
237
238         return true;
239 }
240
241 static bool test_wbc_guidtostring(struct torture_context *tctx)
242 {
243         struct wbcGuid guid;
244         const char *guid_string = "f7cf07b4-1487-45c7-824d-8b18cc580811";
245         char *guid_string2;
246
247         torture_assert_wbc_ok(tctx, wbcStringToGuid(guid_string, &guid),
248                               "wbcStringToGuid of %s failed", guid_string);
249         torture_assert_wbc_ok(tctx, wbcGuidToString(&guid, &guid_string2),
250                               "wbcGuidToString of %s failed", guid_string);
251         torture_assert_str_equal(tctx, guid_string, guid_string2,
252                                  "guid strings differ");
253         wbcFreeMemory(guid_string2);
254
255         return true;
256 }
257
258 static bool test_wbc_domain_info(struct torture_context *tctx)
259 {
260         struct wbcDomainInfo *info = NULL;
261         struct wbcInterfaceDetails *details = NULL;
262         wbcErr ret = false;
263
264         torture_assert_wbc_ok_goto_fail(tctx,
265                                         wbcInterfaceDetails(&details),
266                                         "%s",
267                                         "wbcInterfaceDetails failed");
268         torture_assert_wbc_ok_goto_fail(tctx,
269                                         wbcDomainInfo(details->netbios_domain, &info),
270                                         "%s",
271                                         "wbcDomainInfo failed");
272
273         torture_assert_goto(tctx,
274                             info,
275                             ret,
276                             fail,
277                             "wbcDomainInfo returned NULL pointer");
278
279         ret = true;
280 fail:
281         wbcFreeMemory(details);
282         wbcFreeMemory(info);
283
284         return ret;
285 }
286
287 static bool test_wbc_users(struct torture_context *tctx)
288 {
289         const char *domain_name = NULL;
290         uint32_t num_users;
291         const char **users = NULL;
292         uint32_t i;
293         struct wbcInterfaceDetails *details = NULL;
294         struct wbcDomainSid *sids = NULL;
295         char *domain = NULL;
296         char *name = NULL;
297         char *sid_string = NULL;
298         wbcErr ret = false;
299
300         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
301                 "%s", "wbcInterfaceDetails failed");
302
303         domain_name = talloc_strdup(tctx, details->netbios_domain);
304         torture_assert_goto(tctx,
305                             domain_name != NULL,
306                             ret,
307                             fail,
308                             "Failed to allocate domain_name");
309         wbcFreeMemory(details);
310         details = NULL;
311
312         torture_assert_wbc_ok_goto_fail(tctx,
313                                         wbcListUsers(domain_name, &num_users, &users),
314                                         "%s",
315                                         "wbcListUsers failed");
316         torture_assert_goto(tctx,
317                             !(num_users > 0 && !users),
318                             ret,
319                             fail,
320                             "wbcListUsers returned invalid results");
321
322         for (i = 0; i < MIN(num_users, 100); i++) {
323                 struct wbcDomainSid sid;
324                 enum wbcSidType name_type;
325                 uint32_t num_sids;
326
327                 torture_assert_wbc_ok_goto_fail(tctx,
328                                                 wbcLookupName(domain_name, users[i], &sid, &name_type),
329                                                 "wbcLookupName of %s failed",
330                                                 users[i]);
331                 torture_assert_int_equal_goto(tctx,
332                                               name_type, WBC_SID_NAME_USER,
333                                               ret,
334                                               fail,
335                                               "wbcLookupName expected WBC_SID_NAME_USER");
336                 wbcSidToString(&sid, &sid_string);
337                 torture_assert_wbc_ok_goto_fail(tctx,
338                                                 wbcLookupSid(&sid,
339                                                              &domain,
340                                                              &name,
341                                                              &name_type),
342                                                 "wbcLookupSid of %s failed",
343                                                 sid_string);
344                 torture_assert_int_equal_goto(tctx,
345                                               name_type, WBC_SID_NAME_USER,
346                                               ret,
347                                               fail,
348                                               "wbcLookupSid of expected WBC_SID_NAME_USER");
349                 torture_assert_goto(tctx,
350                                     name,
351                                     ret,
352                                     fail,
353                                     "wbcLookupSid returned no name");
354                 wbcFreeMemory(domain);
355                 domain = NULL;
356                 wbcFreeMemory(name);
357                 name = NULL;
358
359                 torture_assert_wbc_ok_goto_fail(tctx,
360                                                 wbcLookupUserSids(&sid, true, &num_sids, &sids),
361                                                 "wbcLookupUserSids of %s failed", sid_string);
362                 torture_assert_wbc_ok_goto_fail(tctx,
363                                                 wbcGetDisplayName(&sid,
364                                                                   &domain,
365                                                                   &name,
366                                                                   &name_type),
367                                                 "wbcGetDisplayName of %s failed",
368                                                 sid_string);
369                 wbcFreeMemory(domain);
370                 domain = NULL;
371                 wbcFreeMemory(name);
372                 name = NULL;
373                 wbcFreeMemory(sids);
374                 sids = NULL;
375                 wbcFreeMemory(sid_string);
376                 sid_string = NULL;
377         }
378
379         ret = true;
380 fail:
381         wbcFreeMemory(details);
382         wbcFreeMemory(users);
383         wbcFreeMemory(domain);
384         wbcFreeMemory(name);
385         wbcFreeMemory(sids);
386         wbcFreeMemory(sid_string);
387
388         return ret;
389 }
390
391 static bool test_wbc_groups(struct torture_context *tctx)
392 {
393         wbcErr ret = false;
394         const char *domain_name = NULL;
395         uint32_t num_groups;
396         const char **groups = NULL;
397         uint32_t i;
398         struct wbcInterfaceDetails *details = NULL;
399         char *domain = NULL;
400         char *name = NULL;
401         char *sid_string = NULL;
402
403         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
404                               "%s", "wbcInterfaceDetails failed");
405
406         domain_name = talloc_strdup(tctx, details->netbios_domain);
407         torture_assert_goto(tctx,
408                             domain_name != NULL,
409                             ret,
410                             fail,
411                             "Failed to allocate domain_name");
412         wbcFreeMemory(details);
413         details = NULL;
414
415         torture_assert_wbc_ok_goto_fail(tctx,
416                                         wbcListGroups(domain_name, &num_groups, &groups),
417                                         "wbcListGroups in %s failed",
418                                         domain_name);
419         torture_assert_goto(tctx,
420                             !(num_groups > 0 && !groups),
421                             ret,
422                             fail,
423                             "wbcListGroups returned invalid results");
424
425         for (i=0; i < MIN(num_groups,100); i++) {
426                 struct wbcDomainSid sid;
427                 enum wbcSidType name_type;
428
429                 torture_assert_wbc_ok_goto_fail(tctx,
430                                                 wbcLookupName(domain_name,
431                                                               groups[i],
432                                                               &sid,
433                                                               &name_type),
434                                                 "wbcLookupName for %s failed",
435                                                 domain_name);
436                 wbcSidToString(&sid, &sid_string);
437                 torture_assert_wbc_ok_goto_fail(tctx,
438                                                 wbcLookupSid(&sid,
439                                                              &domain,
440                                                              &name,
441                                                              &name_type),
442                                                 "wbcLookupSid of %s failed",
443                                                 sid_string);
444                 torture_assert_goto(tctx,
445                                     name,
446                                     ret,
447                                     fail,
448                                     "wbcLookupSid returned no name");
449
450                 wbcFreeMemory(domain);
451                 domain = NULL;
452                 wbcFreeMemory(name);
453                 name = NULL;
454                 wbcFreeMemory(sid_string);
455                 sid_string = NULL;
456         }
457
458         ret = true;
459 fail:
460         wbcFreeMemory(details);
461         wbcFreeMemory(groups);
462         wbcFreeMemory(domain);
463         wbcFreeMemory(name);
464         wbcFreeMemory(sid_string);
465
466         return ret;
467 }
468
469 static bool test_wbc_trusts(struct torture_context *tctx)
470 {
471         struct wbcDomainInfo *domains = NULL;
472         struct wbcAuthErrorInfo *error = NULL;
473         size_t num_domains;
474         uint32_t i;
475         wbcErr ret = false;
476
477         torture_assert_wbc_ok_goto_fail(tctx,
478                                         wbcListTrusts(&domains, &num_domains),
479                                         "%s",
480                                         "wbcListTrusts failed");
481         torture_assert_goto(tctx,
482                             !(num_domains > 0 && !domains),
483                             ret,
484                             fail,
485                             "wbcListTrusts returned invalid results");
486
487         for (i=0; i < MIN(num_domains,100); i++) {
488
489                 /*
490                 struct wbcDomainSid sid;
491                 enum wbcSidType name_type;
492                 char *domain;
493                 char *name;
494                 */
495                 torture_assert_wbc_ok_goto_fail(tctx,
496                                                 wbcCheckTrustCredentials(domains[i].short_name,
497                                                                          &error),
498                                                 "%s",
499                                                 "wbcCheckTrustCredentials failed");
500                 /*
501                 torture_assert_wbc_ok(tctx, wbcLookupName(domains[i].short_name, NULL, &sid, &name_type),
502                         "wbcLookupName failed");
503                 torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_DOMAIN,
504                         "wbcLookupName expected WBC_SID_NAME_DOMAIN");
505                 torture_assert_wbc_ok(tctx, wbcLookupSid(&sid, &domain, &name, &name_type),
506                         "wbcLookupSid failed");
507                 torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_DOMAIN,
508                         "wbcLookupSid expected WBC_SID_NAME_DOMAIN");
509                 torture_assert(tctx, name,
510                         "wbcLookupSid returned no name");
511                 */
512                 wbcFreeMemory(error);
513                 error = NULL;
514         }
515
516         ret = true;
517 fail:
518         wbcFreeMemory(domains);
519         wbcFreeMemory(error);
520
521         return ret;
522 }
523
524 static bool test_wbc_lookupdc(struct torture_context *tctx)
525 {
526         const char *domain_name = NULL;
527         struct wbcInterfaceDetails *details;
528         struct wbcDomainControllerInfo *dc_info;
529
530         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
531                 "%s", "wbcInterfaceDetails failed");
532
533         domain_name = talloc_strdup(tctx, details->netbios_domain);
534         wbcFreeMemory(details);
535
536         torture_assert_wbc_ok(tctx, wbcLookupDomainController(domain_name, 0, &dc_info),
537                               "wbcLookupDomainController for %s failed", domain_name);
538         wbcFreeMemory(dc_info);
539
540         return true;
541 }
542
543 static bool test_wbc_lookupdcex(struct torture_context *tctx)
544 {
545         const char *domain_name = NULL;
546         struct wbcInterfaceDetails *details;
547         struct wbcDomainControllerInfoEx *dc_info;
548
549         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
550                 "%s", "wbcInterfaceDetails failed");
551
552         domain_name = talloc_strdup(tctx, details->netbios_domain);
553         wbcFreeMemory(details);
554
555         torture_assert_wbc_ok(tctx, wbcLookupDomainControllerEx(domain_name, NULL, NULL, 0, &dc_info),
556                 "wbcLookupDomainControllerEx for %s failed", domain_name);
557         wbcFreeMemory(dc_info);
558
559         return true;
560 }
561
562 static bool test_wbc_resolve_winsbyname(struct torture_context *tctx)
563 {
564         const char *name;
565         char *ip;
566         wbcErr ret;
567
568         name = torture_setting_string(tctx, "host", NULL);
569
570         torture_comment(tctx, "test-WinsByName: host='%s'\n", name);
571
572         ret = wbcResolveWinsByName(name, &ip);
573
574         if (is_ipaddress(name)) {
575                 torture_assert_wbc_equal(tctx, ret, WBC_ERR_DOMAIN_NOT_FOUND, "wbcResolveWinsByName of %s failed", name);
576         } else {
577                 torture_assert_wbc_ok(tctx, ret, "wbcResolveWinsByName for %s failed", name);
578         }
579
580         return true;
581 }
582
583 static bool test_wbc_resolve_winsbyip(struct torture_context *tctx)
584 {
585         const char *ip;
586         const char *host;
587         struct nbt_name nbt_name;
588         char *name;
589         wbcErr ret;
590         NTSTATUS status;
591
592         host = torture_setting_string(tctx, "host", NULL);
593
594         torture_comment(tctx, "test-WinsByIp: host='%s'\n", host);
595
596         make_nbt_name_server(&nbt_name, host);
597
598         status = resolve_name_ex(lpcfg_resolve_context(tctx->lp_ctx),
599                                  0, 0, &nbt_name, tctx, &ip, tctx->ev);
600         torture_assert_ntstatus_ok(tctx, status,
601                         talloc_asprintf(tctx,"Failed to resolve %s: %s",
602                                         nbt_name.name, nt_errstr(status)));
603
604         torture_comment(tctx, "test-WinsByIp: ip='%s'\n", ip);
605
606         ret = wbcResolveWinsByIP(ip, &name);
607
608         torture_assert_wbc_ok(tctx, ret, "wbcResolveWinsByIP for %s failed", ip);
609
610         wbcFreeMemory(name);
611
612         return true;
613 }
614
615 static bool test_wbc_lookup_rids(struct torture_context *tctx)
616 {
617         struct wbcDomainSid builtin;
618         uint32_t rids[2] = { 544, 545 };
619         const char *domain_name = NULL;
620         const char **names = NULL;
621         enum wbcSidType *types;
622         wbcErr ret = false;
623
624         wbcStringToSid("S-1-5-32", &builtin);
625
626         ret = wbcLookupRids(&builtin, 2, rids, &domain_name, &names,
627                             &types);
628         torture_assert_wbc_ok_goto_fail(
629                 tctx, ret, "%s", "wbcLookupRids for 544 and 545 failed");
630
631         torture_assert_str_equal(
632                 tctx, names[0], "Administrators",
633                 "S-1-5-32-544 not mapped to 'Administrators'");
634         torture_assert_str_equal_goto_fail(
635                 tctx, names[1], "Users", "S-1-5-32-545 not mapped to 'Users'");
636
637         ret = true;
638 fail:
639         wbcFreeMemory(discard_const_p(char ,domain_name));
640         wbcFreeMemory(names);
641         wbcFreeMemory(types);
642         return ret;
643 }
644
645 static bool test_wbc_get_sidaliases(struct torture_context *tctx)
646 {
647         struct wbcDomainSid builtin;
648         struct wbcDomainInfo *info = NULL;
649         struct wbcInterfaceDetails *details = NULL;
650         struct wbcDomainSid sids[2];
651         uint32_t *rids = NULL;
652         uint32_t num_rids;
653         wbcErr ret = false;
654
655         torture_assert_wbc_ok_goto_fail(tctx,
656                                         wbcInterfaceDetails(&details),
657                                         "%s",
658                                         "wbcInterfaceDetails failed");
659         torture_assert_wbc_ok_goto_fail(tctx,
660                                         wbcDomainInfo(details->netbios_domain, &info),
661                                         "wbcDomainInfo of %s failed",
662                                         details->netbios_domain);
663
664         sids[0] = info->sid;
665         sids[0].sub_auths[sids[0].num_auths++] = 500;
666         sids[1] = info->sid;
667         sids[1].sub_auths[sids[1].num_auths++] = 512;
668
669         torture_assert_wbc_ok_goto_fail(tctx,
670                                         wbcStringToSid("S-1-5-32", &builtin),
671                                         "wbcStringToSid of %s failed",
672                                         "S-1-5-32");
673
674         ret = wbcGetSidAliases(&builtin, sids, 2, &rids, &num_rids);
675         torture_assert_wbc_ok_goto_fail(tctx,
676                                         ret,
677                                         "%s",
678                                         "wbcGetSidAliases failed");
679
680         ret = true;
681 fail:
682         wbcFreeMemory(details);
683         wbcFreeMemory(info);
684         wbcFreeMemory(rids);
685         return ret;
686 }
687
688 static bool test_wbc_authenticate_user_int(struct torture_context *tctx,
689                                            const char *correct_password)
690 {
691         struct wbcAuthUserParams params;
692         struct wbcAuthUserInfo *info = NULL;
693         struct wbcAuthErrorInfo *error = NULL;
694         wbcErr ret;
695
696         ret = wbcAuthenticateUser(cli_credentials_get_username(cmdline_credentials), correct_password);
697         torture_assert_wbc_equal(tctx, ret, WBC_ERR_SUCCESS,
698                                  "wbcAuthenticateUser of %s failed",
699                                  cli_credentials_get_username(cmdline_credentials));
700
701         ZERO_STRUCT(params);
702         params.account_name             = cli_credentials_get_username(cmdline_credentials);
703         params.level                    = WBC_AUTH_USER_LEVEL_PLAIN;
704         params.password.plaintext       = correct_password;
705
706         ret = wbcAuthenticateUserEx(&params, &info, &error);
707         torture_assert_wbc_equal(tctx, ret, WBC_ERR_SUCCESS,
708                                  "wbcAuthenticateUserEx of %s failed", params.account_name);
709         wbcFreeMemory(info);
710         info = NULL;
711
712         wbcFreeMemory(error);
713         error = NULL;
714
715         params.password.plaintext       = "wrong";
716         ret = wbcAuthenticateUserEx(&params, &info, &error);
717         torture_assert_wbc_equal(tctx, ret, WBC_ERR_AUTH_ERROR,
718                                  "wbcAuthenticateUserEx for %s succeeded where it "
719                                  "should have failed", params.account_name);
720         wbcFreeMemory(info);
721         info = NULL;
722
723         wbcFreeMemory(error);
724         error = NULL;
725
726         return true;
727 }
728
729 static bool test_wbc_authenticate_user(struct torture_context *tctx)
730 {
731         return test_wbc_authenticate_user_int(tctx, cli_credentials_get_password(cmdline_credentials));
732 }
733
734 static bool test_wbc_change_password(struct torture_context *tctx)
735 {
736         wbcErr ret;
737         const char *oldpass = cli_credentials_get_password(cmdline_credentials);
738         const char *newpass = "Koo8irei%$";
739
740         struct samr_CryptPassword new_nt_password;
741         struct samr_CryptPassword new_lm_password;
742         struct samr_Password old_nt_hash_enc;
743         struct samr_Password old_lanman_hash_enc;
744
745         uint8_t old_nt_hash[16];
746         uint8_t old_lanman_hash[16];
747         uint8_t new_nt_hash[16];
748         uint8_t new_lanman_hash[16];
749
750         struct wbcChangePasswordParams params;
751
752         if (oldpass == NULL) {
753                 torture_skip(tctx,
754                         "skipping wbcChangeUserPassword test as old password cannot be retrieved\n");
755         }
756
757         ZERO_STRUCT(params);
758
759         E_md4hash(oldpass, old_nt_hash);
760         E_md4hash(newpass, new_nt_hash);
761
762         if (lpcfg_client_lanman_auth(tctx->lp_ctx) &&
763             E_deshash(newpass, new_lanman_hash) &&
764             E_deshash(oldpass, old_lanman_hash)) {
765
766                 /* E_deshash returns false for 'long' passwords (> 14
767                    DOS chars).  This allows us to match Win2k, which
768                    does not store a LM hash for these passwords (which
769                    would reduce the effective password length to 14) */
770
771                 encode_pw_buffer(new_lm_password.data, newpass, STR_UNICODE);
772                 arcfour_crypt(new_lm_password.data, old_nt_hash, 516);
773                 E_old_pw_hash(new_nt_hash, old_lanman_hash,
774                               old_lanman_hash_enc.hash);
775
776                 params.old_password.response.old_lm_hash_enc_length =
777                         sizeof(old_lanman_hash_enc.hash);
778                 params.old_password.response.old_lm_hash_enc_data =
779                         old_lanman_hash_enc.hash;
780                 params.new_password.response.lm_length =
781                         sizeof(new_lm_password.data);
782                 params.new_password.response.lm_data =
783                         new_lm_password.data;
784         } else {
785                 ZERO_STRUCT(new_lm_password);
786                 ZERO_STRUCT(old_lanman_hash_enc);
787         }
788
789         encode_pw_buffer(new_nt_password.data, newpass, STR_UNICODE);
790
791         arcfour_crypt(new_nt_password.data, old_nt_hash, 516);
792         E_old_pw_hash(new_nt_hash, old_nt_hash, old_nt_hash_enc.hash);
793
794         params.old_password.response.old_nt_hash_enc_length =
795                 sizeof(old_nt_hash_enc.hash);
796         params.old_password.response.old_nt_hash_enc_data =
797                 old_nt_hash_enc.hash;
798         params.new_password.response.nt_length = sizeof(new_nt_password.data);
799         params.new_password.response.nt_data = new_nt_password.data;
800
801         params.level = WBC_CHANGE_PASSWORD_LEVEL_RESPONSE;
802         params.account_name = cli_credentials_get_username(cmdline_credentials);
803         params.domain_name = cli_credentials_get_domain(cmdline_credentials);
804
805         ret = wbcChangeUserPasswordEx(&params, NULL, NULL, NULL);
806         torture_assert_wbc_equal(tctx, ret, WBC_ERR_SUCCESS,
807                                  "wbcChangeUserPassword for %s failed", params.account_name);
808
809         if (!test_wbc_authenticate_user_int(tctx, newpass)) {
810                 return false;
811         }
812
813         ret = wbcChangeUserPassword(cli_credentials_get_username(cmdline_credentials), newpass,
814                                     cli_credentials_get_password(cmdline_credentials));
815         torture_assert_wbc_equal(tctx, ret, WBC_ERR_SUCCESS,
816                                  "wbcChangeUserPassword for %s failed", params.account_name);
817
818         return test_wbc_authenticate_user_int(tctx, cli_credentials_get_password(cmdline_credentials));
819 }
820
821 static bool test_wbc_logon_user(struct torture_context *tctx)
822 {
823         struct wbcLogonUserParams params;
824         struct wbcLogonUserInfo *info = NULL;
825         struct wbcAuthErrorInfo *error = NULL;
826         struct wbcUserPasswordPolicyInfo *policy = NULL;
827         struct wbcInterfaceDetails *iface;
828         struct wbcDomainSid sid;
829         enum wbcSidType sidtype;
830         char *sidstr;
831         wbcErr ret;
832
833         ZERO_STRUCT(params);
834
835         ret = wbcLogonUser(&params, &info, &error, &policy);
836         torture_assert_wbc_equal(tctx, ret, WBC_ERR_INVALID_PARAM,
837                                  "%s", "wbcLogonUser succeeded for NULL where it should "
838                                  "have failed");
839
840         params.username = cli_credentials_get_username(cmdline_credentials);
841         params.password = cli_credentials_get_password(cmdline_credentials);
842
843         ret = wbcAddNamedBlob(&params.num_blobs, &params.blobs,
844                               "foo", 0, discard_const_p(uint8_t, "bar"), 4);
845         torture_assert_wbc_equal(tctx, ret, WBC_ERR_SUCCESS,
846                                  "%s", "wbcAddNamedBlob failed");
847
848         ret = wbcLogonUser(&params, &info, &error, &policy);
849         torture_assert_wbc_equal(tctx, ret, WBC_ERR_SUCCESS,
850                                  "wbcLogonUser for %s failed", params.username);
851         wbcFreeMemory(info); info = NULL;
852         wbcFreeMemory(error); error = NULL;
853         wbcFreeMemory(policy); policy = NULL;
854
855         params.password = "wrong";
856
857         ret = wbcLogonUser(&params, &info, &error, &policy);
858         torture_assert_wbc_equal(tctx, ret, WBC_ERR_AUTH_ERROR,
859                                  "wbcLogonUser for %s should have failed with "
860                                  "WBC_ERR_AUTH_ERROR", params.username);
861         wbcFreeMemory(info); info = NULL;
862         wbcFreeMemory(error); error = NULL;
863         wbcFreeMemory(policy); policy = NULL;
864
865         ret = wbcAddNamedBlob(&params.num_blobs, &params.blobs,
866                               "membership_of", 0,
867                               discard_const_p(uint8_t, "S-1-2-3-4"),
868                               strlen("S-1-2-3-4")+1);
869         torture_assert_wbc_equal(tctx, ret, WBC_ERR_SUCCESS,
870                                  "%s", "wbcAddNamedBlob failed");
871         params.password = cli_credentials_get_password(cmdline_credentials);
872         ret = wbcLogonUser(&params, &info, &error, &policy);
873         torture_assert_wbc_equal(tctx, ret, WBC_ERR_AUTH_ERROR,
874                                  "wbcLogonUser for %s should have failed with "
875                                  "WBC_ERR_AUTH_ERROR", params.username);
876         wbcFreeMemory(info); info = NULL;
877         wbcFreeMemory(error); error = NULL;
878         wbcFreeMemory(policy); policy = NULL;
879         wbcFreeMemory(params.blobs);
880         params.blobs = NULL; params.num_blobs = 0;
881
882         ret = wbcInterfaceDetails(&iface);
883         torture_assert_wbc_equal(tctx, ret, WBC_ERR_SUCCESS,
884                                  "%s", "wbcInterfaceDetails failed");
885
886         ret = wbcLookupName(iface->netbios_domain, cli_credentials_get_username(cmdline_credentials), &sid,
887                             &sidtype);
888         wbcFreeMemory(iface);
889         torture_assert_wbc_equal(tctx, ret, WBC_ERR_SUCCESS,
890                                  "wbcLookupName for %s failed", cli_credentials_get_username(cmdline_credentials));
891
892         ret = wbcSidToString(&sid, &sidstr);
893         torture_assert_wbc_equal(tctx, ret, WBC_ERR_SUCCESS,
894                                  "%s", "wbcSidToString failed");
895
896         ret = wbcAddNamedBlob(&params.num_blobs, &params.blobs,
897                               "membership_of", 0,
898                               (uint8_t *)sidstr, strlen(sidstr)+1);
899         torture_assert_wbc_equal(tctx, ret, WBC_ERR_SUCCESS,
900                                  "%s", "wbcAddNamedBlob failed");
901         wbcFreeMemory(sidstr);
902         params.password = cli_credentials_get_password(cmdline_credentials);
903         ret = wbcLogonUser(&params, &info, &error, &policy);
904         torture_assert_wbc_equal(tctx, ret, WBC_ERR_SUCCESS,
905                                  "wbcLogonUser for %s failed", params.username);
906         wbcFreeMemory(info); info = NULL;
907         wbcFreeMemory(error); error = NULL;
908         wbcFreeMemory(policy); policy = NULL;
909         wbcFreeMemory(params.blobs);
910         params.blobs = NULL; params.num_blobs = 0;
911
912         return true;
913 }
914
915 static bool test_wbc_getgroups(struct torture_context *tctx)
916 {
917         wbcErr ret;
918         uint32_t num_groups;
919         gid_t *groups;
920
921         ret = wbcGetGroups(cli_credentials_get_username(cmdline_credentials), &num_groups, &groups);
922         torture_assert_wbc_equal(tctx, ret, WBC_ERR_SUCCESS,
923                                  "wbcGetGroups for %s failed", cli_credentials_get_username(cmdline_credentials));
924         wbcFreeMemory(groups);
925         return true;
926 }
927
928 struct torture_suite *torture_wbclient(TALLOC_CTX *ctx)
929 {
930         struct torture_suite *suite = torture_suite_create(ctx, "wbclient");
931
932         torture_suite_add_simple_test(suite, "wbcPing", test_wbc_ping);
933         torture_suite_add_simple_test(suite, "wbcPingDc", test_wbc_pingdc);
934         torture_suite_add_simple_test(suite, "wbcPingDc2", test_wbc_pingdc2);
935         torture_suite_add_simple_test(suite, "wbcLibraryDetails", test_wbc_library_details);
936         torture_suite_add_simple_test(suite, "wbcInterfaceDetails", test_wbc_interface_details);
937         torture_suite_add_simple_test(suite, "wbcSidTypeString", test_wbc_sidtypestring);
938         torture_suite_add_simple_test(suite, "wbcSidToString", test_wbc_sidtostring);
939         torture_suite_add_simple_test(suite, "wbcGuidToString", test_wbc_guidtostring);
940         torture_suite_add_simple_test(suite, "wbcDomainInfo", test_wbc_domain_info);
941         torture_suite_add_simple_test(suite, "wbcListUsers", test_wbc_users);
942         torture_suite_add_simple_test(suite, "wbcListGroups", test_wbc_groups);
943         torture_suite_add_simple_test(suite, "wbcListTrusts", test_wbc_trusts);
944         torture_suite_add_simple_test(suite, "wbcLookupDomainController", test_wbc_lookupdc);
945         torture_suite_add_simple_test(suite, "wbcLookupDomainControllerEx", test_wbc_lookupdcex);
946         torture_suite_add_simple_test(suite, "wbcResolveWinsByName", test_wbc_resolve_winsbyname);
947         torture_suite_add_simple_test(suite, "wbcResolveWinsByIP", test_wbc_resolve_winsbyip);
948         torture_suite_add_simple_test(suite, "wbcLookupRids",
949                                       test_wbc_lookup_rids);
950         torture_suite_add_simple_test(suite, "wbcGetSidAliases",
951                                       test_wbc_get_sidaliases);
952         torture_suite_add_simple_test(suite, "wbcAuthenticateUser",
953                                       test_wbc_authenticate_user);
954         torture_suite_add_simple_test(suite, "wbcLogonUser",
955                                       test_wbc_logon_user);
956         torture_suite_add_simple_test(suite, "wbcChangeUserPassword",
957                                       test_wbc_change_password);
958         torture_suite_add_simple_test(suite, "wbcGetGroups",
959                                       test_wbc_getgroups);
960
961         return suite;
962 }