wbinfo: use wbcLookupDomainControllerEx for wbinfo --dsgetdcname.
[sfrench/samba-autobuild/.git] / nsswitch / wbinfo.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Winbind status program.
5
6    Copyright (C) Tim Potter      2000-2003
7    Copyright (C) Andrew Bartlett 2002-2007
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "winbind_client.h"
25 #include "libwbclient/wbclient.h"
26 #include "lib/popt/popt.h"
27 #include "../libcli/auth/libcli_auth.h"
28 #if !(_SAMBA_VERSION_) < 4
29 #include "lib/cmdline/popt_common.h"
30 #endif
31
32 #ifdef DBGC_CLASS
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_WINBIND
35 #endif
36
37 static struct wbcInterfaceDetails *init_interface_details(void)
38 {
39         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
40         static struct wbcInterfaceDetails *details;
41
42         if (details) {
43                 return details;
44         }
45
46         wbc_status = wbcInterfaceDetails(&details);
47         if (!WBC_ERROR_IS_OK(wbc_status)) {
48                 d_fprintf(stderr, "could not obtain winbind interface "
49                                   "details!\n");
50         }
51
52         return details;
53 }
54
55 static char winbind_separator(void)
56 {
57         struct wbcInterfaceDetails *details;
58         static bool got_sep;
59         static char sep;
60
61         if (got_sep)
62                 return sep;
63
64         details = init_interface_details();
65
66         if (!details) {
67                 d_fprintf(stderr, "could not obtain winbind separator!\n");
68                 return 0;
69         }
70
71         sep = details->winbind_separator;
72         got_sep = true;
73
74         if (!sep) {
75                 d_fprintf(stderr, "winbind separator was NULL!\n");
76                 return 0;
77         }
78
79         return sep;
80 }
81
82 static const char *get_winbind_domain(void)
83 {
84         static struct wbcInterfaceDetails *details;
85
86         details = init_interface_details();
87
88         if (!details) {
89                 d_fprintf(stderr, "could not obtain winbind domain name!\n");
90                 return 0;
91         }
92
93         return details->netbios_domain;
94 }
95
96 static const char *get_winbind_netbios_name(void)
97 {
98         static struct wbcInterfaceDetails *details;
99
100         details = init_interface_details();
101
102         if (!details) {
103                 d_fprintf(stderr, "could not obtain winbind netbios name!\n");
104                 return 0;
105         }
106
107         return details->netbios_name;
108 }
109
110 /* Copy of parse_domain_user from winbindd_util.c.  Parse a string of the
111    form DOMAIN/user into a domain and a user */
112
113 static bool parse_wbinfo_domain_user(const char *domuser, fstring domain,
114                                      fstring user)
115 {
116
117         char *p = strchr(domuser,winbind_separator());
118
119         if (!p) {
120                 /* Maybe it was a UPN? */
121                 if ((p = strchr(domuser, '@')) != NULL) {
122                         fstrcpy(domain, "");
123                         fstrcpy(user, domuser);
124                         return true;
125                 }
126
127                 fstrcpy(user, domuser);
128                 fstrcpy(domain, get_winbind_domain());
129                 return true;
130         }
131
132         fstrcpy(user, p+1);
133         fstrcpy(domain, domuser);
134         domain[PTR_DIFF(p, domuser)] = 0;
135         strupper_m(domain);
136
137         return true;
138 }
139
140 /* Parse string of "uid,sid" or "gid,sid" into separate int and string values.
141  * Return true if input was valid, false otherwise. */
142 static bool parse_mapping_arg(char *arg, int *id, char **sid)
143 {
144         char *tmp, *endptr;
145
146         if (!arg || !*arg)
147                 return false;
148
149         tmp = strtok(arg, ",");
150         *sid = strtok(NULL, ",");
151
152         if (!tmp || !*tmp || !*sid || !**sid)
153                 return false;
154
155         /* Because atoi() can return 0 on invalid input, which would be a valid
156          * UID/GID we must use strtoul() and do error checking */
157         *id = strtoul(tmp, &endptr, 10);
158
159         if (endptr[0] != '\0')
160                 return false;
161
162         return true;
163 }
164
165 /* pull pwent info for a given user */
166
167 static bool wbinfo_get_userinfo(char *user)
168 {
169         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
170         struct passwd *pwd = NULL;
171
172         wbc_status = wbcGetpwnam(user, &pwd);
173         if (!WBC_ERROR_IS_OK(wbc_status)) {
174                 return false;
175         }
176
177         d_printf("%s:%s:%u:%u:%s:%s:%s\n",
178                  pwd->pw_name,
179                  pwd->pw_passwd,
180                  (unsigned int)pwd->pw_uid,
181                  (unsigned int)pwd->pw_gid,
182                  pwd->pw_gecos,
183                  pwd->pw_dir,
184                  pwd->pw_shell);
185
186         return true;
187 }
188
189 /* pull pwent info for a given uid */
190 static bool wbinfo_get_uidinfo(int uid)
191 {
192         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
193         struct passwd *pwd = NULL;
194
195         wbc_status = wbcGetpwuid(uid, &pwd);
196         if (!WBC_ERROR_IS_OK(wbc_status)) {
197                 return false;
198         }
199
200         d_printf("%s:%s:%u:%u:%s:%s:%s\n",
201                  pwd->pw_name,
202                  pwd->pw_passwd,
203                  (unsigned int)pwd->pw_uid,
204                  (unsigned int)pwd->pw_gid,
205                  pwd->pw_gecos,
206                  pwd->pw_dir,
207                  pwd->pw_shell);
208
209         return true;
210 }
211
212 static bool wbinfo_get_user_sidinfo(const char *sid_str)
213 {
214         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
215         struct passwd *pwd = NULL;
216         struct wbcDomainSid sid;
217
218         wbc_status = wbcStringToSid(sid_str, &sid);
219         wbc_status = wbcGetpwsid(&sid, &pwd);
220         if (!WBC_ERROR_IS_OK(wbc_status)) {
221                 return false;
222         }
223
224         d_printf("%s:%s:%u:%u:%s:%s:%s\n",
225                  pwd->pw_name,
226                  pwd->pw_passwd,
227                  (unsigned int)pwd->pw_uid,
228                  (unsigned int)pwd->pw_gid,
229                  pwd->pw_gecos,
230                  pwd->pw_dir,
231                  pwd->pw_shell);
232
233         return true;
234 }
235
236
237 /* pull grent for a given group */
238 static bool wbinfo_get_groupinfo(const char *group)
239 {
240         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
241         struct group *grp;
242         char **mem;
243
244         wbc_status = wbcGetgrnam(group, &grp);
245         if (!WBC_ERROR_IS_OK(wbc_status)) {
246                 return false;
247         }
248
249         d_printf("%s:%s:%u:",
250                  grp->gr_name,
251                  grp->gr_passwd,
252                  (unsigned int)grp->gr_gid);
253
254         mem = grp->gr_mem;
255         while (*mem != NULL) {
256                 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
257                 mem += 1;
258         }
259         d_printf("\n");
260
261         wbcFreeMemory(grp);
262
263         return true;
264 }
265
266 /* pull grent for a given gid */
267 static bool wbinfo_get_gidinfo(int gid)
268 {
269         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
270         struct group *grp;
271         char **mem;
272
273         wbc_status = wbcGetgrgid(gid, &grp);
274         if (!WBC_ERROR_IS_OK(wbc_status)) {
275                 return false;
276         }
277
278         d_printf("%s:%s:%u:",
279                  grp->gr_name,
280                  grp->gr_passwd,
281                  (unsigned int)grp->gr_gid);
282
283         mem = grp->gr_mem;
284         while (*mem != NULL) {
285                 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
286                 mem += 1;
287         }
288         d_printf("\n");
289
290         wbcFreeMemory(grp);
291
292         return true;
293 }
294
295 /* List groups a user is a member of */
296
297 static bool wbinfo_get_usergroups(const char *user)
298 {
299         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
300         uint32_t num_groups;
301         uint32_t i;
302         gid_t *groups = NULL;
303
304         /* Send request */
305
306         wbc_status = wbcGetGroups(user, &num_groups, &groups);
307         if (!WBC_ERROR_IS_OK(wbc_status)) {
308                 return false;
309         }
310
311         for (i = 0; i < num_groups; i++) {
312                 d_printf("%d\n", (int)groups[i]);
313         }
314
315         wbcFreeMemory(groups);
316
317         return true;
318 }
319
320
321 /* List group SIDs a user SID is a member of */
322 static bool wbinfo_get_usersids(const char *user_sid_str)
323 {
324         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
325         uint32_t num_sids;
326         uint32_t i;
327         struct wbcDomainSid user_sid, *sids = NULL;
328
329         /* Send request */
330
331         wbc_status = wbcStringToSid(user_sid_str, &user_sid);
332         if (!WBC_ERROR_IS_OK(wbc_status)) {
333                 return false;
334         }
335
336         wbc_status = wbcLookupUserSids(&user_sid, false, &num_sids, &sids);
337         if (!WBC_ERROR_IS_OK(wbc_status)) {
338                 return false;
339         }
340
341         for (i = 0; i < num_sids; i++) {
342                 char *str = NULL;
343                 wbc_status = wbcSidToString(&sids[i], &str);
344                 if (!WBC_ERROR_IS_OK(wbc_status)) {
345                         wbcFreeMemory(sids);
346                         return false;
347                 }
348                 d_printf("%s\n", str);
349                 wbcFreeMemory(str);
350         }
351
352         wbcFreeMemory(sids);
353
354         return true;
355 }
356
357 static bool wbinfo_get_userdomgroups(const char *user_sid_str)
358 {
359         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
360         uint32_t num_sids;
361         uint32_t i;
362         struct wbcDomainSid user_sid, *sids = NULL;
363
364         /* Send request */
365
366         wbc_status = wbcStringToSid(user_sid_str, &user_sid);
367         if (!WBC_ERROR_IS_OK(wbc_status)) {
368                 return false;
369         }
370
371         wbc_status = wbcLookupUserSids(&user_sid, true, &num_sids, &sids);
372         if (!WBC_ERROR_IS_OK(wbc_status)) {
373                 return false;
374         }
375
376         for (i = 0; i < num_sids; i++) {
377                 char *str = NULL;
378                 wbc_status = wbcSidToString(&sids[i], &str);
379                 if (!WBC_ERROR_IS_OK(wbc_status)) {
380                         wbcFreeMemory(sids);
381                         return false;
382                 }
383                 d_printf("%s\n", str);
384                 wbcFreeMemory(str);
385         }
386
387         wbcFreeMemory(sids);
388
389         return true;
390 }
391
392 static bool wbinfo_get_sidaliases(const char *domain,
393                                   const char *user_sid_str)
394 {
395         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
396         struct wbcDomainInfo *dinfo = NULL;
397         uint32_t i;
398         struct wbcDomainSid user_sid;
399         uint32_t *alias_rids = NULL;
400         uint32_t num_alias_rids;
401         char *domain_sid_str = NULL;
402
403         /* Send request */
404         if ((domain == NULL) || (strequal(domain, ".")) ||
405            (domain[0] == '\0')) {
406                 domain = get_winbind_domain();
407         }
408
409         /* Send request */
410
411         wbc_status = wbcDomainInfo(domain, &dinfo);
412         if (!WBC_ERROR_IS_OK(wbc_status)) {
413                 d_printf("wbcDomainInfo(%s) failed: %s\n", domain,
414                          wbcErrorString(wbc_status));
415                 goto done;
416         }
417         wbc_status = wbcStringToSid(user_sid_str, &user_sid);
418         if (!WBC_ERROR_IS_OK(wbc_status)) {
419                 goto done;
420         }
421
422         wbc_status = wbcGetSidAliases(&dinfo->sid, &user_sid, 1,
423             &alias_rids, &num_alias_rids);
424         if (!WBC_ERROR_IS_OK(wbc_status)) {
425                 goto done;
426         }
427
428         wbc_status = wbcSidToString(&dinfo->sid, &domain_sid_str);
429         if (!WBC_ERROR_IS_OK(wbc_status)) {
430                 goto done;
431         }
432
433         for (i = 0; i < num_alias_rids; i++) {
434                 d_printf("%s-%d\n", domain_sid_str, alias_rids[i]);
435         }
436
437         wbcFreeMemory(alias_rids);
438
439 done:
440         if (domain_sid_str) {
441                 wbcFreeMemory(domain_sid_str);
442         }
443         if (dinfo) {
444                 wbcFreeMemory(dinfo);
445         }
446         return (WBC_ERR_SUCCESS == wbc_status);
447 }
448
449
450 /* Convert NetBIOS name to IP */
451
452 static bool wbinfo_wins_byname(const char *name)
453 {
454         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
455         char *ip = NULL;
456
457         wbc_status = wbcResolveWinsByName(name, &ip);
458         if (!WBC_ERROR_IS_OK(wbc_status)) {
459                 return false;
460         }
461
462         /* Display response */
463
464         d_printf("%s\n", ip);
465
466         wbcFreeMemory(ip);
467
468         return true;
469 }
470
471 /* Convert IP to NetBIOS name */
472
473 static bool wbinfo_wins_byip(const char *ip)
474 {
475         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
476         char *name = NULL;
477
478         wbc_status = wbcResolveWinsByIP(ip, &name);
479         if (!WBC_ERROR_IS_OK(wbc_status)) {
480                 return false;
481         }
482
483         /* Display response */
484
485         d_printf("%s\n", name);
486
487         wbcFreeMemory(name);
488
489         return true;
490 }
491
492 /* List all/trusted domains */
493
494 static bool wbinfo_list_domains(bool list_all_domains, bool verbose)
495 {
496         struct wbcDomainInfo *domain_list = NULL;
497         size_t num_domains;
498         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
499         bool print_all = !list_all_domains && verbose;
500         int i;
501
502         wbc_status = wbcListTrusts(&domain_list, &num_domains);
503         if (!WBC_ERROR_IS_OK(wbc_status)) {
504                 return false;
505         }
506
507         if (print_all) {
508                 d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n",
509                          "Domain Name", "DNS Domain", "Trust Type",
510                          "Transitive", "In", "Out");
511         }
512
513         for (i=0; i<num_domains; i++) {
514                 if (print_all) {
515                         d_printf("%-16s", domain_list[i].short_name);
516                 } else {
517                         d_printf("%s", domain_list[i].short_name);
518                         d_printf("\n");
519                         continue;
520                 }
521
522                 d_printf("%-24s", domain_list[i].dns_name);
523
524                 switch(domain_list[i].trust_type) {
525                 case WBC_DOMINFO_TRUSTTYPE_NONE:
526                         d_printf("None        ");
527                         break;
528                 case WBC_DOMINFO_TRUSTTYPE_FOREST:
529                         d_printf("Forest      ");
530                         break;
531                 case WBC_DOMINFO_TRUSTTYPE_EXTERNAL:
532                         d_printf("External    ");
533                         break;
534                 case WBC_DOMINFO_TRUSTTYPE_IN_FOREST:
535                         d_printf("In-Forest   ");
536                         break;
537                 }
538
539                 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_TRANSITIVE) {
540                         d_printf("Yes         ");
541                 } else {
542                         d_printf("No          ");
543                 }
544
545                 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_INCOMING) {
546                         d_printf("Yes  ");
547                 } else {
548                         d_printf("No   ");
549                 }
550
551                 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_OUTGOING) {
552                         d_printf("Yes  ");
553                 } else {
554                         d_printf("No   ");
555                 }
556
557                 d_printf("\n");
558         }
559
560         return true;
561 }
562
563 /* List own domain */
564
565 static bool wbinfo_list_own_domain(void)
566 {
567         d_printf("%s\n", get_winbind_domain());
568
569         return true;
570 }
571
572 /* show sequence numbers */
573 static bool wbinfo_show_sequence(const char *domain)
574 {
575         d_printf("This command has been deprecated.  Please use the "
576                  "--online-status option instead.\n");
577         return false;
578 }
579
580 /* show sequence numbers */
581 static bool wbinfo_show_onlinestatus(const char *domain)
582 {
583         struct wbcDomainInfo *domain_list = NULL;
584         size_t num_domains;
585         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
586         int i;
587
588         wbc_status = wbcListTrusts(&domain_list, &num_domains);
589         if (!WBC_ERROR_IS_OK(wbc_status)) {
590                 return false;
591         }
592
593         for (i=0; i<num_domains; i++) {
594                 bool is_offline;
595
596                 if (domain) {
597                         if (!strequal(domain_list[i].short_name, domain)) {
598                                 continue;
599                         }
600                 }
601
602                 is_offline = (domain_list[i].domain_flags &
603                               WBC_DOMINFO_DOMAIN_OFFLINE);
604
605                 d_printf("%s : %s\n",
606                          domain_list[i].short_name,
607                          is_offline ? "offline" : "online" );
608         }
609
610         return true;
611 }
612
613
614 /* Show domain info */
615
616 static bool wbinfo_domain_info(const char *domain)
617 {
618         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
619         struct wbcDomainInfo *dinfo = NULL;
620         char *sid_str = NULL;
621
622         if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
623                 domain = get_winbind_domain();
624         }
625
626         /* Send request */
627
628         wbc_status = wbcDomainInfo(domain, &dinfo);
629         if (!WBC_ERROR_IS_OK(wbc_status)) {
630                 return false;
631         }
632
633         wbc_status = wbcSidToString(&dinfo->sid, &sid_str);
634         if (!WBC_ERROR_IS_OK(wbc_status)) {
635                 wbcFreeMemory(dinfo);
636                 return false;
637         }
638
639         /* Display response */
640
641         d_printf("Name              : %s\n", dinfo->short_name);
642         d_printf("Alt_Name          : %s\n", dinfo->dns_name);
643
644         d_printf("SID               : %s\n", sid_str);
645
646         d_printf("Active Directory  : %s\n",
647                  (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_AD) ? "Yes" : "No");
648         d_printf("Native            : %s\n",
649                  (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_NATIVE) ?
650                  "Yes" : "No");
651
652         d_printf("Primary           : %s\n",
653                  (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_PRIMARY) ?
654                  "Yes" : "No");
655
656         wbcFreeMemory(sid_str);
657         wbcFreeMemory(dinfo);
658
659         return true;
660 }
661
662 /* Get a foreign DC's name */
663 static bool wbinfo_getdcname(const char *domain_name)
664 {
665         struct winbindd_request request;
666         struct winbindd_response response;
667
668         ZERO_STRUCT(request);
669         ZERO_STRUCT(response);
670
671         fstrcpy(request.domain_name, domain_name);
672
673         /* Send request */
674
675         if (winbindd_request_response(WINBINDD_GETDCNAME, &request,
676                                       &response) != NSS_STATUS_SUCCESS) {
677                 d_fprintf(stderr, "Could not get dc name for %s\n",domain_name);
678                 return false;
679         }
680
681         /* Display response */
682
683         d_printf("%s\n", response.data.dc_name);
684
685         return true;
686 }
687
688 /* Find a DC */
689 static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
690 {
691         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
692         struct wbcDomainControllerInfoEx *dc_info;
693         char *str = NULL;
694
695         wbc_status = wbcLookupDomainControllerEx(domain_name, NULL, NULL,
696                                                  flags | DS_DIRECTORY_SERVICE_REQUIRED,
697                                                  &dc_info);
698         if (!WBC_ERROR_IS_OK(wbc_status)) {
699                 printf("Could not find dc for %s\n", domain_name);
700                 return false;
701         }
702
703         wbcGuidToString(dc_info->domain_guid, &str);
704
705         d_printf("%s\n", dc_info->dc_unc);
706         d_printf("%s\n", dc_info->dc_address);
707         d_printf("%d\n", dc_info->dc_address_type);
708         d_printf("%s\n", str);
709         d_printf("%s\n", dc_info->domain_name);
710         d_printf("%s\n", dc_info->forest_name);
711         d_printf("0x%08x\n", dc_info->dc_flags);
712         d_printf("%s\n", dc_info->dc_site_name);
713         d_printf("%s\n", dc_info->client_site_name);
714
715         return true;
716 }
717
718 /* Check trust account password */
719
720 static bool wbinfo_check_secret(const char *domain)
721 {
722         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
723         struct wbcAuthErrorInfo *error = NULL;
724         const char *domain_name;
725
726         if (domain) {
727                 domain_name = domain;
728         } else {
729                 domain_name = get_winbind_domain();
730         }
731
732         wbc_status = wbcCheckTrustCredentials(domain_name, &error);
733
734         d_printf("checking the trust secret for domain %s via RPC calls %s\n",
735                 domain_name,
736                 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
737
738         if (wbc_status == WBC_ERR_AUTH_ERROR) {
739                 d_fprintf(stderr, "error code was %s (0x%x)\n",
740                           error->nt_string, error->nt_status);
741                 wbcFreeMemory(error);
742         }
743         if (!WBC_ERROR_IS_OK(wbc_status)) {
744                 return false;
745         }
746
747         return true;
748 }
749
750 /* Change trust account password */
751
752 static bool wbinfo_change_secret(const char *domain)
753 {
754         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
755         struct wbcAuthErrorInfo *error = NULL;
756         const char *domain_name;
757
758         if (domain) {
759                 domain_name = domain;
760         } else {
761                 domain_name = get_winbind_domain();
762         }
763
764         wbc_status = wbcChangeTrustCredentials(domain_name, &error);
765
766         d_printf("changing the trust secret for domain %s via RPC calls %s\n",
767                 domain_name,
768                 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
769
770         if (wbc_status == WBC_ERR_AUTH_ERROR) {
771                 d_fprintf(stderr, "error code was %s (0x%x)\n",
772                           error->nt_string, error->nt_status);
773                 wbcFreeMemory(error);
774         }
775         if (!WBC_ERROR_IS_OK(wbc_status)) {
776                 return false;
777         }
778
779         return true;
780 }
781
782 /* Convert uid to sid */
783
784 static bool wbinfo_uid_to_sid(uid_t uid)
785 {
786         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
787         struct wbcDomainSid sid;
788         char *sid_str = NULL;
789
790         /* Send request */
791
792         wbc_status = wbcUidToSid(uid, &sid);
793         if (!WBC_ERROR_IS_OK(wbc_status)) {
794                 return false;
795         }
796
797         wbc_status = wbcSidToString(&sid, &sid_str);
798         if (!WBC_ERROR_IS_OK(wbc_status)) {
799                 return false;
800         }
801
802         /* Display response */
803
804         d_printf("%s\n", sid_str);
805
806         wbcFreeMemory(sid_str);
807
808         return true;
809 }
810
811 /* Convert gid to sid */
812
813 static bool wbinfo_gid_to_sid(gid_t gid)
814 {
815         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
816         struct wbcDomainSid sid;
817         char *sid_str = NULL;
818
819         /* Send request */
820
821         wbc_status = wbcGidToSid(gid, &sid);
822         if (!WBC_ERROR_IS_OK(wbc_status)) {
823                 return false;
824         }
825
826         wbc_status = wbcSidToString(&sid, &sid_str);
827         if (!WBC_ERROR_IS_OK(wbc_status)) {
828                 return false;
829         }
830
831         /* Display response */
832
833         d_printf("%s\n", sid_str);
834
835         wbcFreeMemory(sid_str);
836
837         return true;
838 }
839
840 /* Convert sid to uid */
841
842 static bool wbinfo_sid_to_uid(const char *sid_str)
843 {
844         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
845         struct wbcDomainSid sid;
846         uid_t uid;
847
848         /* Send request */
849
850         wbc_status = wbcStringToSid(sid_str, &sid);
851         if (!WBC_ERROR_IS_OK(wbc_status)) {
852                 return false;
853         }
854
855         wbc_status = wbcSidToUid(&sid, &uid);
856         if (!WBC_ERROR_IS_OK(wbc_status)) {
857                 return false;
858         }
859
860         /* Display response */
861
862         d_printf("%d\n", (int)uid);
863
864         return true;
865 }
866
867 static bool wbinfo_sid_to_gid(const char *sid_str)
868 {
869         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
870         struct wbcDomainSid sid;
871         gid_t gid;
872
873         /* Send request */
874
875         wbc_status = wbcStringToSid(sid_str, &sid);
876         if (!WBC_ERROR_IS_OK(wbc_status)) {
877                 return false;
878         }
879
880         wbc_status = wbcSidToGid(&sid, &gid);
881         if (!WBC_ERROR_IS_OK(wbc_status)) {
882                 return false;
883         }
884
885         /* Display response */
886
887         d_printf("%d\n", (int)gid);
888
889         return true;
890 }
891
892 static bool wbinfo_allocate_uid(void)
893 {
894         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
895         uid_t uid;
896
897         /* Send request */
898
899         wbc_status = wbcAllocateUid(&uid);
900         if (!WBC_ERROR_IS_OK(wbc_status)) {
901                 return false;
902         }
903
904         /* Display response */
905
906         d_printf("New uid: %u\n", (unsigned int)uid);
907
908         return true;
909 }
910
911 static bool wbinfo_allocate_gid(void)
912 {
913         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
914         gid_t gid;
915
916         /* Send request */
917
918         wbc_status = wbcAllocateGid(&gid);
919         if (!WBC_ERROR_IS_OK(wbc_status)) {
920                 return false;
921         }
922
923         /* Display response */
924
925         d_printf("New gid: %u\n", (unsigned int)gid);
926
927         return true;
928 }
929
930 static bool wbinfo_set_uid_mapping(uid_t uid, const char *sid_str)
931 {
932         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
933         struct wbcDomainSid sid;
934
935         /* Send request */
936
937         wbc_status = wbcStringToSid(sid_str, &sid);
938         if (!WBC_ERROR_IS_OK(wbc_status)) {
939                 return false;
940         }
941
942         wbc_status = wbcSetUidMapping(uid, &sid);
943         if (!WBC_ERROR_IS_OK(wbc_status)) {
944                 return false;
945         }
946
947         /* Display response */
948
949         d_printf("uid %u now mapped to sid %s\n",
950                 (unsigned int)uid, sid_str);
951
952         return true;
953 }
954
955 static bool wbinfo_set_gid_mapping(gid_t gid, const char *sid_str)
956 {
957         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
958         struct wbcDomainSid sid;
959
960         /* Send request */
961
962         wbc_status = wbcStringToSid(sid_str, &sid);
963         if (!WBC_ERROR_IS_OK(wbc_status)) {
964                 return false;
965         }
966
967         wbc_status = wbcSetGidMapping(gid, &sid);
968         if (!WBC_ERROR_IS_OK(wbc_status)) {
969                 return false;
970         }
971
972         /* Display response */
973
974         d_printf("gid %u now mapped to sid %s\n",
975                 (unsigned int)gid, sid_str);
976
977         return true;
978 }
979
980 static bool wbinfo_remove_uid_mapping(uid_t uid, const char *sid_str)
981 {
982         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
983         struct wbcDomainSid sid;
984
985         /* Send request */
986
987         wbc_status = wbcStringToSid(sid_str, &sid);
988         if (!WBC_ERROR_IS_OK(wbc_status)) {
989                 return false;
990         }
991
992         wbc_status = wbcRemoveUidMapping(uid, &sid);
993         if (!WBC_ERROR_IS_OK(wbc_status)) {
994                 return false;
995         }
996
997         /* Display response */
998
999         d_printf("Removed uid %u to sid %s mapping\n",
1000                 (unsigned int)uid, sid_str);
1001
1002         return true;
1003 }
1004
1005 static bool wbinfo_remove_gid_mapping(gid_t gid, const char *sid_str)
1006 {
1007         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1008         struct wbcDomainSid sid;
1009
1010         /* Send request */
1011
1012         wbc_status = wbcStringToSid(sid_str, &sid);
1013         if (!WBC_ERROR_IS_OK(wbc_status)) {
1014                 return false;
1015         }
1016
1017         wbc_status = wbcRemoveGidMapping(gid, &sid);
1018         if (!WBC_ERROR_IS_OK(wbc_status)) {
1019                 return false;
1020         }
1021
1022         /* Display response */
1023
1024         d_printf("Removed gid %u to sid %s mapping\n",
1025                 (unsigned int)gid, sid_str);
1026
1027         return true;
1028 }
1029
1030 /* Convert sid to string */
1031
1032 static bool wbinfo_lookupsid(const char *sid_str)
1033 {
1034         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1035         struct wbcDomainSid sid;
1036         char *domain;
1037         char *name;
1038         enum wbcSidType type;
1039
1040         /* Send off request */
1041
1042         wbc_status = wbcStringToSid(sid_str, &sid);
1043         if (!WBC_ERROR_IS_OK(wbc_status)) {
1044                 return false;
1045         }
1046
1047         wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
1048         if (!WBC_ERROR_IS_OK(wbc_status)) {
1049                 return false;
1050         }
1051
1052         /* Display response */
1053
1054         d_printf("%s%c%s %d\n",
1055                  domain, winbind_separator(), name, type);
1056
1057         return true;
1058 }
1059
1060 /* Convert sid to fullname */
1061
1062 static bool wbinfo_lookupsid_fullname(const char *sid_str)
1063 {
1064         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1065         struct wbcDomainSid sid;
1066         char *domain;
1067         char *name;
1068         enum wbcSidType type;
1069
1070         /* Send off request */
1071
1072         wbc_status = wbcStringToSid(sid_str, &sid);
1073         if (!WBC_ERROR_IS_OK(wbc_status)) {
1074                 return false;
1075         }
1076
1077         wbc_status = wbcGetDisplayName(&sid, &domain, &name, &type);
1078         if (!WBC_ERROR_IS_OK(wbc_status)) {
1079                 return false;
1080         }
1081
1082         /* Display response */
1083
1084         d_printf("%s%c%s %d\n",
1085                  domain, winbind_separator(), name, type);
1086
1087         return true;
1088 }
1089
1090 /* Lookup a list of RIDs */
1091
1092 static bool wbinfo_lookuprids(const char *domain, const char *arg)
1093 {
1094         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1095         struct wbcDomainInfo *dinfo = NULL;
1096         char *domain_name = NULL;
1097         const char **names = NULL;
1098         enum wbcSidType *types = NULL;
1099         size_t i;
1100         int num_rids;
1101         uint32_t *rids = NULL;
1102         const char *p;
1103         char *ridstr;
1104         TALLOC_CTX *mem_ctx = NULL;
1105         bool ret = false;
1106
1107         if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
1108                 domain = get_winbind_domain();
1109         }
1110
1111         /* Send request */
1112
1113         wbc_status = wbcDomainInfo(domain, &dinfo);
1114         if (!WBC_ERROR_IS_OK(wbc_status)) {
1115                 d_printf("wbcDomainInfo(%s) failed: %s\n", domain,
1116                          wbcErrorString(wbc_status));
1117                 goto done;
1118         }
1119
1120         mem_ctx = talloc_new(NULL);
1121         if (mem_ctx == NULL) {
1122                 d_printf("talloc_new failed\n");
1123                 goto done;
1124         }
1125
1126         num_rids = 0;
1127         rids = NULL;
1128         p = arg;
1129
1130         while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
1131                 uint32_t rid = strtoul(ridstr, NULL, 10);
1132                 rids = talloc_realloc(mem_ctx, rids, uint32_t, num_rids + 1);
1133                 if (rids == NULL) {
1134                         d_printf("talloc_realloc failed\n");
1135                 }
1136                 rids[num_rids] = rid;
1137                 num_rids += 1;
1138         }
1139
1140         if (rids == NULL) {
1141                 d_printf("no rids\n");
1142                 goto done;
1143         }
1144
1145         wbc_status = wbcLookupRids(&dinfo->sid, num_rids, rids,
1146                                    (const char **)&domain_name, &names, &types);
1147         if (!WBC_ERROR_IS_OK(wbc_status)) {
1148                 d_printf("winbind_lookup_rids failed: %s\n",
1149                          wbcErrorString(wbc_status));
1150                 goto done;
1151         }
1152
1153         d_printf("Domain: %s\n", domain_name);
1154
1155         for (i=0; i<num_rids; i++) {
1156                 d_printf("%8d: %s (%s)\n", rids[i], names[i],
1157                          wbcSidTypeString(types[i]));
1158         }
1159
1160         ret = true;
1161 done:
1162         if (dinfo) {
1163                 wbcFreeMemory(dinfo);
1164         }
1165         if (domain_name) {
1166                 wbcFreeMemory(domain_name);
1167         }
1168         if (names) {
1169                 wbcFreeMemory(names);
1170         }
1171         if (types) {
1172                 wbcFreeMemory(types);
1173         }
1174         TALLOC_FREE(mem_ctx);
1175         return ret;
1176 }
1177
1178 /* Convert string to sid */
1179
1180 static bool wbinfo_lookupname(const char *full_name)
1181 {
1182         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1183         struct wbcDomainSid sid;
1184         char *sid_str;
1185         enum wbcSidType type;
1186         fstring domain_name;
1187         fstring account_name;
1188
1189         /* Send off request */
1190
1191         parse_wbinfo_domain_user(full_name, domain_name,
1192                                  account_name);
1193
1194         wbc_status = wbcLookupName(domain_name, account_name,
1195                                    &sid, &type);
1196         if (!WBC_ERROR_IS_OK(wbc_status)) {
1197                 return false;
1198         }
1199
1200         wbc_status = wbcSidToString(&sid, &sid_str);
1201         if (!WBC_ERROR_IS_OK(wbc_status)) {
1202                 return false;
1203         }
1204
1205         /* Display response */
1206
1207         d_printf("%s %s (%d)\n", sid_str, wbcSidTypeString(type), type);
1208
1209         wbcFreeMemory(sid_str);
1210
1211         return true;
1212 }
1213
1214 static char *wbinfo_prompt_pass(TALLOC_CTX *mem_ctx,
1215                                 const char *prefix,
1216                                 const char *username)
1217 {
1218         char *prompt;
1219         const char *ret = NULL;
1220
1221         prompt = talloc_asprintf(mem_ctx, "Enter %s's ", username);
1222         if (!prompt) {
1223                 return NULL;
1224         }
1225         if (prefix) {
1226                 prompt = talloc_asprintf_append(prompt, "%s ", prefix);
1227                 if (!prompt) {
1228                         return NULL;
1229                 }
1230         }
1231         prompt = talloc_asprintf_append(prompt, "password: ");
1232         if (!prompt) {
1233                 return NULL;
1234         }
1235
1236         ret = getpass(prompt);
1237         TALLOC_FREE(prompt);
1238
1239         return talloc_strdup(mem_ctx, ret);
1240 }
1241
1242 /* Authenticate a user with a plaintext password */
1243
1244 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32_t flags)
1245 {
1246         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1247         char *s = NULL;
1248         char *p = NULL;
1249         char *password = NULL;
1250         char *name = NULL;
1251         char *local_cctype = NULL;
1252         uid_t uid;
1253         struct wbcLogonUserParams params;
1254         struct wbcLogonUserInfo *info;
1255         struct wbcAuthErrorInfo *error;
1256         struct wbcUserPasswordPolicyInfo *policy;
1257         TALLOC_CTX *frame = talloc_tos();
1258
1259         if ((s = talloc_strdup(frame, username)) == NULL) {
1260                 return false;
1261         }
1262
1263         if ((p = strchr(s, '%')) != NULL) {
1264                 *p = 0;
1265                 p++;
1266                 password = talloc_strdup(frame, p);
1267         } else {
1268                 password = wbinfo_prompt_pass(frame, NULL, username);
1269         }
1270
1271         local_cctype = talloc_strdup(frame, cctype);
1272
1273         name = s;
1274
1275         uid = geteuid();
1276
1277         params.username = name;
1278         params.password = password;
1279         params.num_blobs = 0;
1280         params.blobs = NULL;
1281
1282         wbc_status = wbcAddNamedBlob(&params.num_blobs,
1283                                      &params.blobs,
1284                                      "flags",
1285                                      0,
1286                                      (uint8_t *)&flags,
1287                                      sizeof(flags));
1288         if (!WBC_ERROR_IS_OK(wbc_status)) {
1289                 goto done;
1290         }
1291
1292         wbc_status = wbcAddNamedBlob(&params.num_blobs,
1293                                      &params.blobs,
1294                                      "user_uid",
1295                                      0,
1296                                      (uint8_t *)&uid,
1297                                      sizeof(uid));
1298         if (!WBC_ERROR_IS_OK(wbc_status)) {
1299                 goto done;
1300         }
1301
1302         wbc_status = wbcAddNamedBlob(&params.num_blobs,
1303                                      &params.blobs,
1304                                      "krb5_cc_type",
1305                                      0,
1306                                      (uint8_t *)local_cctype,
1307                                      strlen(cctype)+1);
1308         if (!WBC_ERROR_IS_OK(wbc_status)) {
1309                 goto done;
1310         }
1311
1312         wbc_status = wbcLogonUser(&params, &info, &error, &policy);
1313
1314         d_printf("plaintext kerberos password authentication for [%s] %s "
1315                  "(requesting cctype: %s)\n",
1316                  username, WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed",
1317                  cctype);
1318
1319         if (error) {
1320                 d_fprintf(stderr,
1321                          "error code was %s (0x%x)\nerror messsage was: %s\n",
1322                          error->nt_string,
1323                          error->nt_status,
1324                          error->display_string);
1325         }
1326
1327         if (WBC_ERROR_IS_OK(wbc_status)) {
1328                 if (flags & WBFLAG_PAM_INFO3_TEXT) {
1329                         if (info && info->info && info->info->user_flags &
1330                             NETLOGON_CACHED_ACCOUNT) {
1331                                 d_printf("user_flgs: "
1332                                          "NETLOGON_CACHED_ACCOUNT\n");
1333                         }
1334                 }
1335
1336                 if (info) {
1337                         int i;
1338                         for (i=0; i < info->num_blobs; i++) {
1339                                 if (strequal(info->blobs[i].name,
1340                                              "krb5ccname")) {
1341                                         d_printf("credentials were put "
1342                                                  "in: %s\n",
1343                                                 (const char *)
1344                                                       info->blobs[i].blob.data);
1345                                         break;
1346                                 }
1347                         }
1348                 } else {
1349                         d_printf("no credentials cached\n");
1350                 }
1351         }
1352  done:
1353
1354         wbcFreeMemory(params.blobs);
1355
1356         return WBC_ERROR_IS_OK(wbc_status);
1357 }
1358
1359 /* Authenticate a user with a plaintext password */
1360
1361 static bool wbinfo_auth(char *username)
1362 {
1363         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1364         char *s = NULL;
1365         char *p = NULL;
1366         char *password = NULL;
1367         char *name = NULL;
1368         TALLOC_CTX *frame = talloc_tos();
1369
1370         if ((s = talloc_strdup(frame, username)) == NULL) {
1371                 return false;
1372         }
1373
1374         if ((p = strchr(s, '%')) != NULL) {
1375                 *p = 0;
1376                 p++;
1377                 password = talloc_strdup(frame, p);
1378         } else {
1379                 password = wbinfo_prompt_pass(frame, NULL, username);
1380         }
1381
1382         name = s;
1383
1384         wbc_status = wbcAuthenticateUser(name, password);
1385
1386         d_printf("plaintext password authentication %s\n",
1387                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1388
1389 #if 0
1390         if (response.data.auth.nt_status)
1391                 d_fprintf(stderr,
1392                          "error code was %s (0x%x)\nerror messsage was: %s\n",
1393                          response.data.auth.nt_status_string,
1394                          response.data.auth.nt_status,
1395                          response.data.auth.error_string);
1396 #endif
1397
1398         return WBC_ERROR_IS_OK(wbc_status);
1399 }
1400
1401 /* Authenticate a user with a challenge/response */
1402
1403 static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
1404 {
1405         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1406         struct wbcAuthUserParams params;
1407         struct wbcAuthUserInfo *info = NULL;
1408         struct wbcAuthErrorInfo *err = NULL;
1409         DATA_BLOB lm = data_blob_null;
1410         DATA_BLOB nt = data_blob_null;
1411         fstring name_user;
1412         fstring name_domain;
1413         char *pass;
1414         char *p;
1415         TALLOC_CTX *frame = talloc_tos();
1416
1417         p = strchr(username, '%');
1418
1419         if (p) {
1420                 *p = 0;
1421                 pass = talloc_strdup(frame, p + 1);
1422         } else {
1423                 pass = wbinfo_prompt_pass(frame, NULL, username);
1424         }
1425
1426         parse_wbinfo_domain_user(username, name_domain, name_user);
1427
1428         params.account_name     = name_user;
1429         params.domain_name      = name_domain;
1430         params.workstation_name = NULL;
1431
1432         params.flags            = 0;
1433         params.parameter_control= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
1434                                   WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
1435
1436         params.level            = WBC_AUTH_USER_LEVEL_RESPONSE;
1437
1438         generate_random_buffer(params.password.response.challenge, 8);
1439
1440         if (use_ntlmv2) {
1441                 DATA_BLOB server_chal;
1442                 DATA_BLOB names_blob;
1443
1444                 server_chal = data_blob(params.password.response.challenge, 8);
1445
1446                 /* Pretend this is a login to 'us', for blob purposes */
1447                 names_blob = NTLMv2_generate_names_blob(NULL,
1448                                                 get_winbind_netbios_name(),
1449                                                 get_winbind_domain());
1450
1451                 if (!SMBNTLMv2encrypt(NULL, name_user, name_domain, pass,
1452                                       &server_chal,
1453                                       &names_blob,
1454                                       &lm, &nt, NULL, NULL)) {
1455                         data_blob_free(&names_blob);
1456                         data_blob_free(&server_chal);
1457                         TALLOC_FREE(pass);
1458                         return false;
1459                 }
1460                 data_blob_free(&names_blob);
1461                 data_blob_free(&server_chal);
1462
1463         } else {
1464                 if (use_lanman) {
1465                         bool ok;
1466                         lm = data_blob(NULL, 24);
1467                         ok = SMBencrypt(pass,
1468                                         params.password.response.challenge,
1469                                         lm.data);
1470                         if (!ok) {
1471                                 data_blob_free(&lm);
1472                         }
1473                 }
1474                 nt = data_blob(NULL, 24);
1475                 SMBNTencrypt(pass, params.password.response.challenge,
1476                              nt.data);
1477         }
1478
1479         params.password.response.nt_length      = nt.length;
1480         params.password.response.nt_data        = nt.data;
1481         params.password.response.lm_length      = lm.length;
1482         params.password.response.lm_data        = lm.data;
1483
1484         wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
1485
1486         /* Display response */
1487
1488         d_printf("challenge/response password authentication %s\n",
1489                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1490
1491         if (wbc_status == WBC_ERR_AUTH_ERROR) {
1492                 d_fprintf(stderr,
1493                          "error code was %s (0x%x)\nerror messsage was: %s\n",
1494                          err->nt_string,
1495                          err->nt_status,
1496                          err->display_string);
1497                 wbcFreeMemory(err);
1498         } else if (WBC_ERROR_IS_OK(wbc_status)) {
1499                 wbcFreeMemory(info);
1500         }
1501
1502         data_blob_free(&nt);
1503         data_blob_free(&lm);
1504
1505         return WBC_ERROR_IS_OK(wbc_status);
1506 }
1507
1508 #ifdef WITH_FAKE_KASERVER
1509 /* Authenticate a user with a plaintext password and set a token */
1510
1511 static bool wbinfo_klog(char *username)
1512 {
1513         struct winbindd_request request;
1514         struct winbindd_response response;
1515         NSS_STATUS result;
1516         char *p;
1517
1518         /* Send off request */
1519
1520         ZERO_STRUCT(request);
1521         ZERO_STRUCT(response);
1522
1523         p = strchr(username, '%');
1524
1525         if (p) {
1526                 *p = 0;
1527                 fstrcpy(request.data.auth.user, username);
1528                 fstrcpy(request.data.auth.pass, p + 1);
1529                 *p = '%';
1530         } else {
1531                 fstrcpy(request.data.auth.user, username);
1532                 fstrcpy(request.data.auth.pass, getpass("Password: "));
1533         }
1534
1535         request.flags |= WBFLAG_PAM_AFS_TOKEN;
1536
1537         result = winbindd_request_response(WINBINDD_PAM_AUTH, &request,
1538                                            &response);
1539
1540         /* Display response */
1541
1542         d_printf("plaintext password authentication %s\n",
1543                  (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1544
1545         if (response.data.auth.nt_status)
1546                 d_fprintf(stderr,
1547                          "error code was %s (0x%x)\nerror messsage was: %s\n",
1548                          response.data.auth.nt_status_string,
1549                          response.data.auth.nt_status,
1550                          response.data.auth.error_string);
1551
1552         if (result != NSS_STATUS_SUCCESS)
1553                 return false;
1554
1555         if (response.extra_data.data == NULL) {
1556                 d_fprintf(stderr, "Did not get token data\n");
1557                 return false;
1558         }
1559
1560         if (!afs_settoken_str((char *)response.extra_data.data)) {
1561                 d_fprintf(stderr, "Could not set token\n");
1562                 return false;
1563         }
1564
1565         d_printf("Successfully created AFS token\n");
1566         return true;
1567 }
1568 #else
1569 static bool wbinfo_klog(char *username)
1570 {
1571         d_fprintf(stderr, "No AFS support compiled in.\n");
1572         return false;
1573 }
1574 #endif
1575
1576 /* Print domain users */
1577
1578 static bool print_domain_users(const char *domain)
1579 {
1580         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1581         uint32_t i;
1582         uint32_t num_users = 0;
1583         const char **users = NULL;
1584
1585         /* Send request to winbind daemon */
1586
1587         /* '.' is the special sign for our own domain */
1588         if (domain && strcmp(domain, ".") == 0) {
1589                 domain = get_winbind_domain();
1590         }
1591
1592         wbc_status = wbcListUsers(domain, &num_users, &users);
1593         if (!WBC_ERROR_IS_OK(wbc_status)) {
1594                 return false;
1595         }
1596
1597         for (i=0; i < num_users; i++) {
1598                 d_printf("%s\n", users[i]);
1599         }
1600
1601         wbcFreeMemory(users);
1602
1603         return true;
1604 }
1605
1606 /* Print domain groups */
1607
1608 static bool print_domain_groups(const char *domain)
1609 {
1610         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1611         uint32_t i;
1612         uint32_t num_groups = 0;
1613         const char **groups = NULL;
1614
1615         /* Send request to winbind daemon */
1616
1617         /* '.' is the special sign for our own domain */
1618         if (domain && strcmp(domain, ".") == 0) {
1619                 domain = get_winbind_domain();
1620         }
1621
1622         wbc_status = wbcListGroups(domain, &num_groups, &groups);
1623         if (!WBC_ERROR_IS_OK(wbc_status)) {
1624                 return false;
1625         }
1626
1627         for (i=0; i < num_groups; i++) {
1628                 d_printf("%s\n", groups[i]);
1629         }
1630
1631         wbcFreeMemory(groups);
1632
1633         return true;
1634 }
1635
1636 /* Set the authorised user for winbindd access in secrets.tdb */
1637
1638 static bool wbinfo_set_auth_user(char *username)
1639 {
1640         d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1641                           "See 'net help setauthuser' for details.\n");
1642         return false;
1643 }
1644
1645 static void wbinfo_get_auth_user(void)
1646 {
1647         d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1648                           "See 'net help getauthuser' for details.\n");
1649 }
1650
1651 static bool wbinfo_ping(void)
1652 {
1653         wbcErr wbc_status;
1654
1655         wbc_status = wbcPing();
1656
1657         /* Display response */
1658
1659         d_printf("Ping to winbindd %s\n",
1660                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1661
1662         return WBC_ERROR_IS_OK(wbc_status);
1663 }
1664
1665 static bool wbinfo_change_user_password(const char *username)
1666 {
1667         wbcErr wbc_status;
1668         char *old_password = NULL;
1669         char *new_password = NULL;
1670         TALLOC_CTX *frame = talloc_tos();
1671
1672         old_password = wbinfo_prompt_pass(frame, "old", username);
1673         new_password = wbinfo_prompt_pass(frame, "new", username);
1674
1675         wbc_status = wbcChangeUserPassword(username, old_password,new_password);
1676
1677         /* Display response */
1678
1679         d_printf("Password change for user %s %s\n", username,
1680                 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1681
1682         return WBC_ERROR_IS_OK(wbc_status);
1683 }
1684
1685 /* Main program */
1686
1687 enum {
1688         OPT_SET_AUTH_USER = 1000,
1689         OPT_GET_AUTH_USER,
1690         OPT_DOMAIN_NAME,
1691         OPT_SEQUENCE,
1692         OPT_GETDCNAME,
1693         OPT_DSGETDCNAME,
1694         OPT_USERDOMGROUPS,
1695         OPT_SIDALIASES,
1696         OPT_USERSIDS,
1697         OPT_ALLOCATE_UID,
1698         OPT_ALLOCATE_GID,
1699         OPT_SET_UID_MAPPING,
1700         OPT_SET_GID_MAPPING,
1701         OPT_REMOVE_UID_MAPPING,
1702         OPT_REMOVE_GID_MAPPING,
1703         OPT_SEPARATOR,
1704         OPT_LIST_ALL_DOMAINS,
1705         OPT_LIST_OWN_DOMAIN,
1706         OPT_UID_INFO,
1707         OPT_USER_SIDINFO,
1708         OPT_GROUP_INFO,
1709         OPT_GID_INFO,
1710         OPT_VERBOSE,
1711         OPT_ONLINESTATUS,
1712         OPT_CHANGE_USER_PASSWORD,
1713         OPT_SID_TO_FULLNAME,
1714         OPT_NTLMV2,
1715         OPT_LANMAN
1716 };
1717
1718 int main(int argc, char **argv, char **envp)
1719 {
1720         int opt;
1721         TALLOC_CTX *frame = talloc_stackframe();
1722         poptContext pc;
1723         static char *string_arg;
1724         char *string_subarg = NULL;
1725         static char *opt_domain_name;
1726         static int int_arg;
1727         int int_subarg = -1;
1728         int result = 1;
1729         bool verbose = false;
1730         bool use_ntlmv2 = false;
1731         bool use_lanman = false;
1732
1733         struct poptOption long_options[] = {
1734                 POPT_AUTOHELP
1735
1736                 /* longName, shortName, argInfo, argPtr, value, descrip,
1737                    argDesc */
1738
1739                 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1740                 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1741                 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1742                 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1743                 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1744                 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1745                 { "sid-to-fullname", 0, POPT_ARG_STRING, &string_arg,
1746                   OPT_SID_TO_FULLNAME, "Converts sid to fullname", "SID" },
1747                 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1748                 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1749                 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1750                 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1751                 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1752                 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1753                   "Get a new UID out of idmap" },
1754                 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1755                   "Get a new GID out of idmap" },
1756                 { "set-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_UID_MAPPING, "Create or modify uid to sid mapping in idmap", "UID,SID" },
1757                 { "set-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_GID_MAPPING, "Create or modify gid to sid mapping in idmap", "GID,SID" },
1758                 { "remove-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_UID_MAPPING, "Remove uid to sid mapping in idmap", "UID,SID" },
1759                 { "remove-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_GID_MAPPING, "Remove gid to sid mapping in idmap", "GID,SID" },
1760                 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1761                 { "change-secret", 'c', POPT_ARG_NONE, 0, 'c', "Change shared secret" },
1762                 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1763                 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1764                 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1765                 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1766                 { "online-status", 0, POPT_ARG_NONE, 0, OPT_ONLINESTATUS, "Show whether domains are marked as online or offline"},
1767                 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1768                 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1769                 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1770                 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1771                 { "user-sidinfo", 0, POPT_ARG_STRING, &string_arg, OPT_USER_SIDINFO, "Get user info from sid", "SID" },
1772                 { "gid-info", 0, POPT_ARG_INT, &int_arg, OPT_GID_INFO, "Get group info from gid", "GID" },
1773                 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1774                 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1775                   OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1776                 { "sid-aliases", 0, POPT_ARG_STRING, &string_arg, OPT_SIDALIASES, "Get sid aliases", "SID" },
1777                 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1778                 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1779                 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1780                 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1781                   "Get a DC name for a foreign domain", "domainname" },
1782                 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
1783                 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1784                 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1785                 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1786 #ifdef WITH_FAKE_KASERVER
1787                 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1788 #endif
1789 #ifdef HAVE_KRB5
1790                 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1791                         /* destroys wbinfo --help output */
1792                         /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1793 #endif
1794                 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1795                 { "verbose", 0, POPT_ARG_NONE, 0, OPT_VERBOSE, "Print additional information per command", NULL },
1796                 { "change-user-password", 0, POPT_ARG_STRING, &string_arg, OPT_CHANGE_USER_PASSWORD, "Change the password for a user", NULL },
1797                 { "ntlmv2", 0, POPT_ARG_NONE, 0, OPT_NTLMV2, "Use NTLMv2 cryptography for user authentication", NULL},
1798                 { "lanman", 0, POPT_ARG_NONE, 0, OPT_LANMAN, "Use lanman cryptography for user authentication", NULL},
1799                 POPT_COMMON_VERSION
1800                 POPT_TABLEEND
1801         };
1802
1803         /* Samba client initialisation */
1804         load_case_tables();
1805
1806
1807         /* Parse options */
1808
1809         pc = poptGetContext("wbinfo", argc, (const char **)argv,
1810                             long_options, 0);
1811
1812         /* Parse command line options */
1813
1814         if (argc == 1) {
1815                 poptPrintHelp(pc, stderr, 0);
1816                 return 1;
1817         }
1818
1819         while((opt = poptGetNextOpt(pc)) != -1) {
1820                 /* get the generic configuration parameters like --domain */
1821                 switch (opt) {
1822                 case OPT_VERBOSE:
1823                         verbose = true;
1824                         break;
1825                 case OPT_NTLMV2:
1826                         use_ntlmv2 = true;
1827                         break;
1828                 case OPT_LANMAN:
1829                         use_lanman = true;
1830                         break;
1831                 }
1832         }
1833
1834         poptFreeContext(pc);
1835
1836         pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1837                             POPT_CONTEXT_KEEP_FIRST);
1838
1839         while((opt = poptGetNextOpt(pc)) != -1) {
1840                 switch (opt) {
1841                 case 'u':
1842                         if (!print_domain_users(opt_domain_name)) {
1843                                 d_fprintf(stderr,
1844                                           "Error looking up domain users\n");
1845                                 goto done;
1846                         }
1847                         break;
1848                 case 'g':
1849                         if (!print_domain_groups(opt_domain_name)) {
1850                                 d_fprintf(stderr,
1851                                           "Error looking up domain groups\n");
1852                                 goto done;
1853                         }
1854                         break;
1855                 case 's':
1856                         if (!wbinfo_lookupsid(string_arg)) {
1857                                 d_fprintf(stderr,
1858                                           "Could not lookup sid %s\n",
1859                                           string_arg);
1860                                 goto done;
1861                         }
1862                         break;
1863                 case OPT_SID_TO_FULLNAME:
1864                         if (!wbinfo_lookupsid_fullname(string_arg)) {
1865                                 d_fprintf(stderr, "Could not lookup sid %s\n",
1866                                           string_arg);
1867                                 goto done;
1868                         }
1869                         break;
1870                 case 'R':
1871                         if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
1872                                 d_fprintf(stderr, "Could not lookup RIDs %s\n",
1873                                           string_arg);
1874                                 goto done;
1875                         }
1876                         break;
1877                 case 'n':
1878                         if (!wbinfo_lookupname(string_arg)) {
1879                                 d_fprintf(stderr, "Could not lookup name %s\n",
1880                                           string_arg);
1881                                 goto done;
1882                         }
1883                         break;
1884                 case 'N':
1885                         if (!wbinfo_wins_byname(string_arg)) {
1886                                 d_fprintf(stderr,
1887                                           "Could not lookup WINS by name %s\n",
1888                                           string_arg);
1889                                 goto done;
1890                         }
1891                         break;
1892                 case 'I':
1893                         if (!wbinfo_wins_byip(string_arg)) {
1894                                 d_fprintf(stderr,
1895                                           "Could not lookup WINS by IP %s\n",
1896                                           string_arg);
1897                                 goto done;
1898                         }
1899                         break;
1900                 case 'U':
1901                         if (!wbinfo_uid_to_sid(int_arg)) {
1902                                 d_fprintf(stderr,
1903                                           "Could not convert uid %d to sid\n",
1904                                           int_arg);
1905                                 goto done;
1906                         }
1907                         break;
1908                 case 'G':
1909                         if (!wbinfo_gid_to_sid(int_arg)) {
1910                                 d_fprintf(stderr,
1911                                           "Could not convert gid %d to sid\n",
1912                                           int_arg);
1913                                 goto done;
1914                         }
1915                         break;
1916                 case 'S':
1917                         if (!wbinfo_sid_to_uid(string_arg)) {
1918                                 d_fprintf(stderr,
1919                                           "Could not convert sid %s to uid\n",
1920                                           string_arg);
1921                                 goto done;
1922                         }
1923                         break;
1924                 case 'Y':
1925                         if (!wbinfo_sid_to_gid(string_arg)) {
1926                                 d_fprintf(stderr,
1927                                           "Could not convert sid %s to gid\n",
1928                                           string_arg);
1929                                 goto done;
1930                         }
1931                         break;
1932                 case OPT_ALLOCATE_UID:
1933                         if (!wbinfo_allocate_uid()) {
1934                                 d_fprintf(stderr, "Could not allocate a uid\n");
1935                                 goto done;
1936                         }
1937                         break;
1938                 case OPT_ALLOCATE_GID:
1939                         if (!wbinfo_allocate_gid()) {
1940                                 d_fprintf(stderr, "Could not allocate a gid\n");
1941                                 goto done;
1942                         }
1943                         break;
1944                 case OPT_SET_UID_MAPPING:
1945                         if (!parse_mapping_arg(string_arg, &int_subarg,
1946                                 &string_subarg) ||
1947                             !wbinfo_set_uid_mapping(int_subarg, string_subarg))
1948                         {
1949                                 d_fprintf(stderr, "Could not create or modify "
1950                                           "uid to sid mapping\n");
1951                                 goto done;
1952                         }
1953                         break;
1954                 case OPT_SET_GID_MAPPING:
1955                         if (!parse_mapping_arg(string_arg, &int_subarg,
1956                                 &string_subarg) ||
1957                             !wbinfo_set_gid_mapping(int_subarg, string_subarg))
1958                         {
1959                                 d_fprintf(stderr, "Could not create or modify "
1960                                           "gid to sid mapping\n");
1961                                 goto done;
1962                         }
1963                         break;
1964                 case OPT_REMOVE_UID_MAPPING:
1965                         if (!parse_mapping_arg(string_arg, &int_subarg,
1966                                 &string_subarg) ||
1967                             !wbinfo_remove_uid_mapping(int_subarg,
1968                                 string_subarg))
1969                         {
1970                                 d_fprintf(stderr, "Could not remove uid to sid "
1971                                     "mapping\n");
1972                                 goto done;
1973                         }
1974                         break;
1975                 case OPT_REMOVE_GID_MAPPING:
1976                         if (!parse_mapping_arg(string_arg, &int_subarg,
1977                                 &string_subarg) ||
1978                             !wbinfo_remove_gid_mapping(int_subarg,
1979                                 string_subarg))
1980                         {
1981                                 d_fprintf(stderr, "Could not remove gid to sid "
1982                                     "mapping\n");
1983                                 goto done;
1984                         }
1985                         break;
1986                 case 't':
1987                         if (!wbinfo_check_secret(opt_domain_name)) {
1988                                 d_fprintf(stderr, "Could not check secret\n");
1989                                 goto done;
1990                         }
1991                         break;
1992                 case 'c':
1993                         if (!wbinfo_change_secret(opt_domain_name)) {
1994                                 d_fprintf(stderr, "Could not change secret\n");
1995                                 goto done;
1996                         }
1997                         break;
1998                 case 'm':
1999                         if (!wbinfo_list_domains(false, verbose)) {
2000                                 d_fprintf(stderr,
2001                                           "Could not list trusted domains\n");
2002                                 goto done;
2003                         }
2004                         break;
2005                 case OPT_SEQUENCE:
2006                         if (!wbinfo_show_sequence(opt_domain_name)) {
2007                                 d_fprintf(stderr,
2008                                           "Could not show sequence numbers\n");
2009                                 goto done;
2010                         }
2011                         break;
2012                 case OPT_ONLINESTATUS:
2013                         if (!wbinfo_show_onlinestatus(opt_domain_name)) {
2014                                 d_fprintf(stderr,
2015                                           "Could not show online-status\n");
2016                                 goto done;
2017                         }
2018                         break;
2019                 case 'D':
2020                         if (!wbinfo_domain_info(string_arg)) {
2021                                 d_fprintf(stderr,
2022                                           "Could not get domain info\n");
2023                                 goto done;
2024                         }
2025                         break;
2026                 case 'i':
2027                         if (!wbinfo_get_userinfo(string_arg)) {
2028                                 d_fprintf(stderr,
2029                                           "Could not get info for user %s\n",
2030                                           string_arg);
2031                                 goto done;
2032                         }
2033                         break;
2034                 case OPT_USER_SIDINFO:
2035                         if ( !wbinfo_get_user_sidinfo(string_arg)) {
2036                                 d_fprintf(stderr,
2037                                           "Could not get info for user "
2038                                           "sid %s\n", string_arg);
2039                                 goto done;
2040                         }
2041                         break;
2042                 case OPT_UID_INFO:
2043                         if ( !wbinfo_get_uidinfo(int_arg)) {
2044                                 d_fprintf(stderr, "Could not get info for uid "
2045                                                 "%d\n", int_arg);
2046                                 goto done;
2047                         }
2048                         break;
2049                 case OPT_GROUP_INFO:
2050                         if ( !wbinfo_get_groupinfo(string_arg)) {
2051                                 d_fprintf(stderr, "Could not get info for "
2052                                           "group %s\n", string_arg);
2053                                 goto done;
2054                         }
2055                         break;
2056                 case OPT_GID_INFO:
2057                         if ( !wbinfo_get_gidinfo(int_arg)) {
2058                                 d_fprintf(stderr, "Could not get info for gid "
2059                                                 "%d\n", int_arg);
2060                                 goto done;
2061                         }
2062                         break;
2063                 case 'r':
2064                         if (!wbinfo_get_usergroups(string_arg)) {
2065                                 d_fprintf(stderr,
2066                                           "Could not get groups for user %s\n",
2067                                           string_arg);
2068                                 goto done;
2069                         }
2070                         break;
2071                 case OPT_USERSIDS:
2072                         if (!wbinfo_get_usersids(string_arg)) {
2073                                 d_fprintf(stderr, "Could not get group SIDs "
2074                                           "for user SID %s\n",
2075                                           string_arg);
2076                                 goto done;
2077                         }
2078                         break;
2079                 case OPT_USERDOMGROUPS:
2080                         if (!wbinfo_get_userdomgroups(string_arg)) {
2081                                 d_fprintf(stderr, "Could not get user's domain "
2082                                          "groups for user SID %s\n",
2083                                          string_arg);
2084                                 goto done;
2085                         }
2086                         break;
2087                 case OPT_SIDALIASES:
2088                         if (!wbinfo_get_sidaliases(opt_domain_name,
2089                                                    string_arg)) {
2090                                 d_fprintf(stderr, "Could not get sid aliases "
2091                                          "for user SID %s\n", string_arg);
2092                                 goto done;
2093                         }
2094                         break;
2095                 case 'a': {
2096                                 bool got_error = false;
2097
2098                                 if (!wbinfo_auth(string_arg)) {
2099                                         d_fprintf(stderr,
2100                                                   "Could not authenticate user "
2101                                                   "%s with plaintext "
2102                                                   "password\n", string_arg);
2103                                         got_error = true;
2104                                 }
2105
2106                                 if (!wbinfo_auth_crap(string_arg, use_ntlmv2,
2107                                                       use_lanman)) {
2108                                         d_fprintf(stderr,
2109                                                 "Could not authenticate user "
2110                                                 "%s with challenge/response\n",
2111                                                 string_arg);
2112                                         got_error = true;
2113                                 }
2114
2115                                 if (got_error)
2116                                         goto done;
2117                                 break;
2118                         }
2119                 case 'K': {
2120                                 uint32_t flags = WBFLAG_PAM_KRB5 |
2121                                                  WBFLAG_PAM_CACHED_LOGIN |
2122                                                 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
2123                                                  WBFLAG_PAM_INFO3_TEXT |
2124                                                  WBFLAG_PAM_CONTACT_TRUSTDOM;
2125
2126                                 if (!wbinfo_auth_krb5(string_arg, "FILE",
2127                                                       flags)) {
2128                                         d_fprintf(stderr,
2129                                                 "Could not authenticate user "
2130                                                 "[%s] with Kerberos "
2131                                                 "(ccache: %s)\n", string_arg,
2132                                                 "FILE");
2133                                         goto done;
2134                                 }
2135                                 break;
2136                         }
2137                 case 'k':
2138                         if (!wbinfo_klog(string_arg)) {
2139                                 d_fprintf(stderr, "Could not klog user\n");
2140                                 goto done;
2141                         }
2142                         break;
2143                 case 'p':
2144                         if (!wbinfo_ping()) {
2145                                 d_fprintf(stderr, "could not ping winbindd!\n");
2146                                 goto done;
2147                         }
2148                         break;
2149                 case OPT_SET_AUTH_USER:
2150                         if (!wbinfo_set_auth_user(string_arg)) {
2151                                 goto done;
2152                         }
2153                         break;
2154                 case OPT_GET_AUTH_USER:
2155                         wbinfo_get_auth_user();
2156                         goto done;
2157                         break;
2158                 case OPT_GETDCNAME:
2159                         if (!wbinfo_getdcname(string_arg)) {
2160                                 goto done;
2161                         }
2162                         break;
2163                 case OPT_DSGETDCNAME:
2164                         if (!wbinfo_dsgetdcname(string_arg, 0)) {
2165                                 goto done;
2166                         }
2167                         break;
2168                 case OPT_SEPARATOR: {
2169                         const char sep = winbind_separator();
2170                         if ( !sep ) {
2171                                 goto done;
2172                         }
2173                         d_printf("%c\n", sep);
2174                         break;
2175                 }
2176                 case OPT_LIST_ALL_DOMAINS:
2177                         if (!wbinfo_list_domains(true, verbose)) {
2178                                 goto done;
2179                         }
2180                         break;
2181                 case OPT_LIST_OWN_DOMAIN:
2182                         if (!wbinfo_list_own_domain()) {
2183                                 goto done;
2184                         }
2185                         break;
2186                 case OPT_CHANGE_USER_PASSWORD:
2187                         if (!wbinfo_change_user_password(string_arg)) {
2188                                 d_fprintf(stderr,
2189                                         "Could not change user password "
2190                                          "for user %s\n", string_arg);
2191                                 goto done;
2192                         }
2193                         break;
2194
2195                 /* generic configuration options */
2196                 case OPT_DOMAIN_NAME:
2197                         break;
2198                 case OPT_VERBOSE:
2199                         break;
2200                 case OPT_NTLMV2:
2201                         break;
2202                 case OPT_LANMAN:
2203                         break;
2204                 default:
2205                         d_fprintf(stderr, "Invalid option\n");
2206                         poptPrintHelp(pc, stderr, 0);
2207                         goto done;
2208                 }
2209         }
2210
2211         result = 0;
2212
2213         /* Exit code */
2214
2215  done:
2216         talloc_free(frame);
2217
2218         poptFreeContext(pc);
2219         return result;
2220 }