wbinfo: allow to check trusts via "wbinfo -t --domain DOMAINNAME".
[ira/wip.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         struct winbindd_request request;
692         struct winbindd_response response;
693
694         ZERO_STRUCT(request);
695         ZERO_STRUCT(response);
696
697         fstrcpy(request.data.dsgetdcname.domain_name, domain_name);
698         request.data.dsgetdcname.flags = flags;
699
700         request.flags |= DS_DIRECTORY_SERVICE_REQUIRED;
701
702         /* Send request */
703
704         if (winbindd_request_response(WINBINDD_DSGETDCNAME, &request,
705                                       &response) != NSS_STATUS_SUCCESS) {
706                 d_fprintf(stderr, "Could not find dc for %s\n", domain_name);
707                 return false;
708         }
709
710         /* Display response */
711
712         d_printf("%s\n", response.data.dsgetdcname.dc_unc);
713         d_printf("%s\n", response.data.dsgetdcname.dc_address);
714         d_printf("%d\n", response.data.dsgetdcname.dc_address_type);
715         d_printf("%s\n", response.data.dsgetdcname.domain_guid);
716         d_printf("%s\n", response.data.dsgetdcname.domain_name);
717         d_printf("%s\n", response.data.dsgetdcname.forest_name);
718         d_printf("0x%08x\n", response.data.dsgetdcname.dc_flags);
719         d_printf("%s\n", response.data.dsgetdcname.dc_site_name);
720         d_printf("%s\n", response.data.dsgetdcname.client_site_name);
721
722         return true;
723 }
724
725 /* Check trust account password */
726
727 static bool wbinfo_check_secret(const char *domain)
728 {
729         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
730         struct wbcAuthErrorInfo *error = NULL;
731         const char *domain_name;
732
733         if (domain) {
734                 domain_name = domain;
735         } else {
736                 domain_name = get_winbind_domain();
737         }
738
739         wbc_status = wbcCheckTrustCredentials(domain_name, &error);
740
741         d_printf("checking the trust secret for domain %s via RPC calls %s\n",
742                 domain_name,
743                 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
744
745         if (wbc_status == WBC_ERR_AUTH_ERROR) {
746                 d_fprintf(stderr, "error code was %s (0x%x)\n",
747                           error->nt_string, error->nt_status);
748                 wbcFreeMemory(error);
749         }
750         if (!WBC_ERROR_IS_OK(wbc_status)) {
751                 return false;
752         }
753
754         return true;
755 }
756
757 /* Convert uid to sid */
758
759 static bool wbinfo_uid_to_sid(uid_t uid)
760 {
761         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
762         struct wbcDomainSid sid;
763         char *sid_str = NULL;
764
765         /* Send request */
766
767         wbc_status = wbcUidToSid(uid, &sid);
768         if (!WBC_ERROR_IS_OK(wbc_status)) {
769                 return false;
770         }
771
772         wbc_status = wbcSidToString(&sid, &sid_str);
773         if (!WBC_ERROR_IS_OK(wbc_status)) {
774                 return false;
775         }
776
777         /* Display response */
778
779         d_printf("%s\n", sid_str);
780
781         wbcFreeMemory(sid_str);
782
783         return true;
784 }
785
786 /* Convert gid to sid */
787
788 static bool wbinfo_gid_to_sid(gid_t gid)
789 {
790         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
791         struct wbcDomainSid sid;
792         char *sid_str = NULL;
793
794         /* Send request */
795
796         wbc_status = wbcGidToSid(gid, &sid);
797         if (!WBC_ERROR_IS_OK(wbc_status)) {
798                 return false;
799         }
800
801         wbc_status = wbcSidToString(&sid, &sid_str);
802         if (!WBC_ERROR_IS_OK(wbc_status)) {
803                 return false;
804         }
805
806         /* Display response */
807
808         d_printf("%s\n", sid_str);
809
810         wbcFreeMemory(sid_str);
811
812         return true;
813 }
814
815 /* Convert sid to uid */
816
817 static bool wbinfo_sid_to_uid(const char *sid_str)
818 {
819         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
820         struct wbcDomainSid sid;
821         uid_t uid;
822
823         /* Send request */
824
825         wbc_status = wbcStringToSid(sid_str, &sid);
826         if (!WBC_ERROR_IS_OK(wbc_status)) {
827                 return false;
828         }
829
830         wbc_status = wbcSidToUid(&sid, &uid);
831         if (!WBC_ERROR_IS_OK(wbc_status)) {
832                 return false;
833         }
834
835         /* Display response */
836
837         d_printf("%d\n", (int)uid);
838
839         return true;
840 }
841
842 static bool wbinfo_sid_to_gid(const char *sid_str)
843 {
844         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
845         struct wbcDomainSid sid;
846         gid_t gid;
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 = wbcSidToGid(&sid, &gid);
856         if (!WBC_ERROR_IS_OK(wbc_status)) {
857                 return false;
858         }
859
860         /* Display response */
861
862         d_printf("%d\n", (int)gid);
863
864         return true;
865 }
866
867 static bool wbinfo_allocate_uid(void)
868 {
869         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
870         uid_t uid;
871
872         /* Send request */
873
874         wbc_status = wbcAllocateUid(&uid);
875         if (!WBC_ERROR_IS_OK(wbc_status)) {
876                 return false;
877         }
878
879         /* Display response */
880
881         d_printf("New uid: %u\n", (unsigned int)uid);
882
883         return true;
884 }
885
886 static bool wbinfo_allocate_gid(void)
887 {
888         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
889         gid_t gid;
890
891         /* Send request */
892
893         wbc_status = wbcAllocateGid(&gid);
894         if (!WBC_ERROR_IS_OK(wbc_status)) {
895                 return false;
896         }
897
898         /* Display response */
899
900         d_printf("New gid: %u\n", (unsigned int)gid);
901
902         return true;
903 }
904
905 static bool wbinfo_set_uid_mapping(uid_t uid, const char *sid_str)
906 {
907         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
908         struct wbcDomainSid sid;
909
910         /* Send request */
911
912         wbc_status = wbcStringToSid(sid_str, &sid);
913         if (!WBC_ERROR_IS_OK(wbc_status)) {
914                 return false;
915         }
916
917         wbc_status = wbcSetUidMapping(uid, &sid);
918         if (!WBC_ERROR_IS_OK(wbc_status)) {
919                 return false;
920         }
921
922         /* Display response */
923
924         d_printf("uid %u now mapped to sid %s\n",
925                 (unsigned int)uid, sid_str);
926
927         return true;
928 }
929
930 static bool wbinfo_set_gid_mapping(gid_t gid, 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 = wbcSetGidMapping(gid, &sid);
943         if (!WBC_ERROR_IS_OK(wbc_status)) {
944                 return false;
945         }
946
947         /* Display response */
948
949         d_printf("gid %u now mapped to sid %s\n",
950                 (unsigned int)gid, sid_str);
951
952         return true;
953 }
954
955 static bool wbinfo_remove_uid_mapping(uid_t uid, 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 = wbcRemoveUidMapping(uid, &sid);
968         if (!WBC_ERROR_IS_OK(wbc_status)) {
969                 return false;
970         }
971
972         /* Display response */
973
974         d_printf("Removed uid %u to sid %s mapping\n",
975                 (unsigned int)uid, sid_str);
976
977         return true;
978 }
979
980 static bool wbinfo_remove_gid_mapping(gid_t gid, 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 = wbcRemoveGidMapping(gid, &sid);
993         if (!WBC_ERROR_IS_OK(wbc_status)) {
994                 return false;
995         }
996
997         /* Display response */
998
999         d_printf("Removed gid %u to sid %s mapping\n",
1000                 (unsigned int)gid, sid_str);
1001
1002         return true;
1003 }
1004
1005 /* Convert sid to string */
1006
1007 static bool wbinfo_lookupsid(const char *sid_str)
1008 {
1009         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1010         struct wbcDomainSid sid;
1011         char *domain;
1012         char *name;
1013         enum wbcSidType type;
1014
1015         /* Send off request */
1016
1017         wbc_status = wbcStringToSid(sid_str, &sid);
1018         if (!WBC_ERROR_IS_OK(wbc_status)) {
1019                 return false;
1020         }
1021
1022         wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
1023         if (!WBC_ERROR_IS_OK(wbc_status)) {
1024                 return false;
1025         }
1026
1027         /* Display response */
1028
1029         d_printf("%s%c%s %d\n",
1030                  domain, winbind_separator(), name, type);
1031
1032         return true;
1033 }
1034
1035 /* Convert sid to fullname */
1036
1037 static bool wbinfo_lookupsid_fullname(const char *sid_str)
1038 {
1039         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1040         struct wbcDomainSid sid;
1041         char *domain;
1042         char *name;
1043         enum wbcSidType type;
1044
1045         /* Send off request */
1046
1047         wbc_status = wbcStringToSid(sid_str, &sid);
1048         if (!WBC_ERROR_IS_OK(wbc_status)) {
1049                 return false;
1050         }
1051
1052         wbc_status = wbcGetDisplayName(&sid, &domain, &name, &type);
1053         if (!WBC_ERROR_IS_OK(wbc_status)) {
1054                 return false;
1055         }
1056
1057         /* Display response */
1058
1059         d_printf("%s%c%s %d\n",
1060                  domain, winbind_separator(), name, type);
1061
1062         return true;
1063 }
1064
1065 /* Lookup a list of RIDs */
1066
1067 static bool wbinfo_lookuprids(const char *domain, const char *arg)
1068 {
1069         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1070         struct wbcDomainInfo *dinfo = NULL;
1071         char *domain_name = NULL;
1072         const char **names = NULL;
1073         enum wbcSidType *types = NULL;
1074         size_t i;
1075         int num_rids;
1076         uint32_t *rids = NULL;
1077         const char *p;
1078         char *ridstr;
1079         TALLOC_CTX *mem_ctx = NULL;
1080         bool ret = false;
1081
1082         if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
1083                 domain = get_winbind_domain();
1084         }
1085
1086         /* Send request */
1087
1088         wbc_status = wbcDomainInfo(domain, &dinfo);
1089         if (!WBC_ERROR_IS_OK(wbc_status)) {
1090                 d_printf("wbcDomainInfo(%s) failed: %s\n", domain,
1091                          wbcErrorString(wbc_status));
1092                 goto done;
1093         }
1094
1095         mem_ctx = talloc_new(NULL);
1096         if (mem_ctx == NULL) {
1097                 d_printf("talloc_new failed\n");
1098                 goto done;
1099         }
1100
1101         num_rids = 0;
1102         rids = NULL;
1103         p = arg;
1104
1105         while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
1106                 uint32_t rid = strtoul(ridstr, NULL, 10);
1107                 rids = talloc_realloc(mem_ctx, rids, uint32_t, num_rids + 1);
1108                 if (rids == NULL) {
1109                         d_printf("talloc_realloc failed\n");
1110                 }
1111                 rids[num_rids] = rid;
1112                 num_rids += 1;
1113         }
1114
1115         if (rids == NULL) {
1116                 d_printf("no rids\n");
1117                 goto done;
1118         }
1119
1120         wbc_status = wbcLookupRids(&dinfo->sid, num_rids, rids,
1121                                    (const char **)&domain_name, &names, &types);
1122         if (!WBC_ERROR_IS_OK(wbc_status)) {
1123                 d_printf("winbind_lookup_rids failed: %s\n",
1124                          wbcErrorString(wbc_status));
1125                 goto done;
1126         }
1127
1128         d_printf("Domain: %s\n", domain_name);
1129
1130         for (i=0; i<num_rids; i++) {
1131                 d_printf("%8d: %s (%s)\n", rids[i], names[i],
1132                          wbcSidTypeString(types[i]));
1133         }
1134
1135         ret = true;
1136 done:
1137         if (dinfo) {
1138                 wbcFreeMemory(dinfo);
1139         }
1140         if (domain_name) {
1141                 wbcFreeMemory(domain_name);
1142         }
1143         if (names) {
1144                 wbcFreeMemory(names);
1145         }
1146         if (types) {
1147                 wbcFreeMemory(types);
1148         }
1149         TALLOC_FREE(mem_ctx);
1150         return ret;
1151 }
1152
1153 /* Convert string to sid */
1154
1155 static bool wbinfo_lookupname(const char *full_name)
1156 {
1157         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1158         struct wbcDomainSid sid;
1159         char *sid_str;
1160         enum wbcSidType type;
1161         fstring domain_name;
1162         fstring account_name;
1163
1164         /* Send off request */
1165
1166         parse_wbinfo_domain_user(full_name, domain_name,
1167                                  account_name);
1168
1169         wbc_status = wbcLookupName(domain_name, account_name,
1170                                    &sid, &type);
1171         if (!WBC_ERROR_IS_OK(wbc_status)) {
1172                 return false;
1173         }
1174
1175         wbc_status = wbcSidToString(&sid, &sid_str);
1176         if (!WBC_ERROR_IS_OK(wbc_status)) {
1177                 return false;
1178         }
1179
1180         /* Display response */
1181
1182         d_printf("%s %s (%d)\n", sid_str, wbcSidTypeString(type), type);
1183
1184         wbcFreeMemory(sid_str);
1185
1186         return true;
1187 }
1188
1189 static char *wbinfo_prompt_pass(TALLOC_CTX *mem_ctx,
1190                                 const char *prefix,
1191                                 const char *username)
1192 {
1193         char *prompt;
1194         const char *ret = NULL;
1195
1196         prompt = talloc_asprintf(mem_ctx, "Enter %s's ", username);
1197         if (!prompt) {
1198                 return NULL;
1199         }
1200         if (prefix) {
1201                 prompt = talloc_asprintf_append(prompt, "%s ", prefix);
1202                 if (!prompt) {
1203                         return NULL;
1204                 }
1205         }
1206         prompt = talloc_asprintf_append(prompt, "password: ");
1207         if (!prompt) {
1208                 return NULL;
1209         }
1210
1211         ret = getpass(prompt);
1212         TALLOC_FREE(prompt);
1213
1214         return talloc_strdup(mem_ctx, ret);
1215 }
1216
1217 /* Authenticate a user with a plaintext password */
1218
1219 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32_t flags)
1220 {
1221         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1222         char *s = NULL;
1223         char *p = NULL;
1224         char *password = NULL;
1225         char *name = NULL;
1226         char *local_cctype = NULL;
1227         uid_t uid;
1228         struct wbcLogonUserParams params;
1229         struct wbcLogonUserInfo *info;
1230         struct wbcAuthErrorInfo *error;
1231         struct wbcUserPasswordPolicyInfo *policy;
1232         TALLOC_CTX *frame = talloc_tos();
1233
1234         if ((s = talloc_strdup(frame, username)) == NULL) {
1235                 return false;
1236         }
1237
1238         if ((p = strchr(s, '%')) != NULL) {
1239                 *p = 0;
1240                 p++;
1241                 password = talloc_strdup(frame, p);
1242         } else {
1243                 password = wbinfo_prompt_pass(frame, NULL, username);
1244         }
1245
1246         local_cctype = talloc_strdup(frame, cctype);
1247
1248         name = s;
1249
1250         uid = geteuid();
1251
1252         params.username = name;
1253         params.password = password;
1254         params.num_blobs = 0;
1255         params.blobs = NULL;
1256
1257         wbc_status = wbcAddNamedBlob(&params.num_blobs,
1258                                      &params.blobs,
1259                                      "flags",
1260                                      0,
1261                                      (uint8_t *)&flags,
1262                                      sizeof(flags));
1263         if (!WBC_ERROR_IS_OK(wbc_status)) {
1264                 goto done;
1265         }
1266
1267         wbc_status = wbcAddNamedBlob(&params.num_blobs,
1268                                      &params.blobs,
1269                                      "user_uid",
1270                                      0,
1271                                      (uint8_t *)&uid,
1272                                      sizeof(uid));
1273         if (!WBC_ERROR_IS_OK(wbc_status)) {
1274                 goto done;
1275         }
1276
1277         wbc_status = wbcAddNamedBlob(&params.num_blobs,
1278                                      &params.blobs,
1279                                      "krb5_cc_type",
1280                                      0,
1281                                      (uint8_t *)local_cctype,
1282                                      strlen(cctype)+1);
1283         if (!WBC_ERROR_IS_OK(wbc_status)) {
1284                 goto done;
1285         }
1286
1287         wbc_status = wbcLogonUser(&params, &info, &error, &policy);
1288
1289         d_printf("plaintext kerberos password authentication for [%s] %s "
1290                  "(requesting cctype: %s)\n",
1291                  username, WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed",
1292                  cctype);
1293
1294         if (error) {
1295                 d_fprintf(stderr,
1296                          "error code was %s (0x%x)\nerror messsage was: %s\n",
1297                          error->nt_string,
1298                          error->nt_status,
1299                          error->display_string);
1300         }
1301
1302         if (WBC_ERROR_IS_OK(wbc_status)) {
1303                 if (flags & WBFLAG_PAM_INFO3_TEXT) {
1304                         if (info && info->info && info->info->user_flags &
1305                             NETLOGON_CACHED_ACCOUNT) {
1306                                 d_printf("user_flgs: "
1307                                          "NETLOGON_CACHED_ACCOUNT\n");
1308                         }
1309                 }
1310
1311                 if (info) {
1312                         int i;
1313                         for (i=0; i < info->num_blobs; i++) {
1314                                 if (strequal(info->blobs[i].name,
1315                                              "krb5ccname")) {
1316                                         d_printf("credentials were put "
1317                                                  "in: %s\n",
1318                                                 (const char *)
1319                                                       info->blobs[i].blob.data);
1320                                         break;
1321                                 }
1322                         }
1323                 } else {
1324                         d_printf("no credentials cached\n");
1325                 }
1326         }
1327  done:
1328
1329         wbcFreeMemory(params.blobs);
1330
1331         return WBC_ERROR_IS_OK(wbc_status);
1332 }
1333
1334 /* Authenticate a user with a plaintext password */
1335
1336 static bool wbinfo_auth(char *username)
1337 {
1338         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1339         char *s = NULL;
1340         char *p = NULL;
1341         char *password = NULL;
1342         char *name = NULL;
1343         TALLOC_CTX *frame = talloc_tos();
1344
1345         if ((s = talloc_strdup(frame, username)) == NULL) {
1346                 return false;
1347         }
1348
1349         if ((p = strchr(s, '%')) != NULL) {
1350                 *p = 0;
1351                 p++;
1352                 password = talloc_strdup(frame, p);
1353         } else {
1354                 password = wbinfo_prompt_pass(frame, NULL, username);
1355         }
1356
1357         name = s;
1358
1359         wbc_status = wbcAuthenticateUser(name, password);
1360
1361         d_printf("plaintext password authentication %s\n",
1362                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1363
1364 #if 0
1365         if (response.data.auth.nt_status)
1366                 d_fprintf(stderr,
1367                          "error code was %s (0x%x)\nerror messsage was: %s\n",
1368                          response.data.auth.nt_status_string,
1369                          response.data.auth.nt_status,
1370                          response.data.auth.error_string);
1371 #endif
1372
1373         return WBC_ERROR_IS_OK(wbc_status);
1374 }
1375
1376 /* Authenticate a user with a challenge/response */
1377
1378 static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
1379 {
1380         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1381         struct wbcAuthUserParams params;
1382         struct wbcAuthUserInfo *info = NULL;
1383         struct wbcAuthErrorInfo *err = NULL;
1384         DATA_BLOB lm = data_blob_null;
1385         DATA_BLOB nt = data_blob_null;
1386         fstring name_user;
1387         fstring name_domain;
1388         char *pass;
1389         char *p;
1390         TALLOC_CTX *frame = talloc_tos();
1391
1392         p = strchr(username, '%');
1393
1394         if (p) {
1395                 *p = 0;
1396                 pass = talloc_strdup(frame, p + 1);
1397         } else {
1398                 pass = wbinfo_prompt_pass(frame, NULL, username);
1399         }
1400
1401         parse_wbinfo_domain_user(username, name_domain, name_user);
1402
1403         params.account_name     = name_user;
1404         params.domain_name      = name_domain;
1405         params.workstation_name = NULL;
1406
1407         params.flags            = 0;
1408         params.parameter_control= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
1409                                   WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
1410
1411         params.level            = WBC_AUTH_USER_LEVEL_RESPONSE;
1412
1413         generate_random_buffer(params.password.response.challenge, 8);
1414
1415         if (use_ntlmv2) {
1416                 DATA_BLOB server_chal;
1417                 DATA_BLOB names_blob;
1418
1419                 server_chal = data_blob(params.password.response.challenge, 8);
1420
1421                 /* Pretend this is a login to 'us', for blob purposes */
1422                 names_blob = NTLMv2_generate_names_blob(NULL,
1423                                                 get_winbind_netbios_name(),
1424                                                 get_winbind_domain());
1425
1426                 if (!SMBNTLMv2encrypt(NULL, name_user, name_domain, pass,
1427                                       &server_chal,
1428                                       &names_blob,
1429                                       &lm, &nt, NULL, NULL)) {
1430                         data_blob_free(&names_blob);
1431                         data_blob_free(&server_chal);
1432                         TALLOC_FREE(pass);
1433                         return false;
1434                 }
1435                 data_blob_free(&names_blob);
1436                 data_blob_free(&server_chal);
1437
1438         } else {
1439                 if (use_lanman) {
1440                         bool ok;
1441                         lm = data_blob(NULL, 24);
1442                         ok = SMBencrypt(pass,
1443                                         params.password.response.challenge,
1444                                         lm.data);
1445                         if (!ok) {
1446                                 data_blob_free(&lm);
1447                         }
1448                 }
1449                 nt = data_blob(NULL, 24);
1450                 SMBNTencrypt(pass, params.password.response.challenge,
1451                              nt.data);
1452         }
1453
1454         params.password.response.nt_length      = nt.length;
1455         params.password.response.nt_data        = nt.data;
1456         params.password.response.lm_length      = lm.length;
1457         params.password.response.lm_data        = lm.data;
1458
1459         wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
1460
1461         /* Display response */
1462
1463         d_printf("challenge/response password authentication %s\n",
1464                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1465
1466         if (wbc_status == WBC_ERR_AUTH_ERROR) {
1467                 d_fprintf(stderr,
1468                          "error code was %s (0x%x)\nerror messsage was: %s\n",
1469                          err->nt_string,
1470                          err->nt_status,
1471                          err->display_string);
1472                 wbcFreeMemory(err);
1473         } else if (WBC_ERROR_IS_OK(wbc_status)) {
1474                 wbcFreeMemory(info);
1475         }
1476
1477         data_blob_free(&nt);
1478         data_blob_free(&lm);
1479
1480         return WBC_ERROR_IS_OK(wbc_status);
1481 }
1482
1483 #ifdef WITH_FAKE_KASERVER
1484 /* Authenticate a user with a plaintext password and set a token */
1485
1486 static bool wbinfo_klog(char *username)
1487 {
1488         struct winbindd_request request;
1489         struct winbindd_response response;
1490         NSS_STATUS result;
1491         char *p;
1492
1493         /* Send off request */
1494
1495         ZERO_STRUCT(request);
1496         ZERO_STRUCT(response);
1497
1498         p = strchr(username, '%');
1499
1500         if (p) {
1501                 *p = 0;
1502                 fstrcpy(request.data.auth.user, username);
1503                 fstrcpy(request.data.auth.pass, p + 1);
1504                 *p = '%';
1505         } else {
1506                 fstrcpy(request.data.auth.user, username);
1507                 fstrcpy(request.data.auth.pass, getpass("Password: "));
1508         }
1509
1510         request.flags |= WBFLAG_PAM_AFS_TOKEN;
1511
1512         result = winbindd_request_response(WINBINDD_PAM_AUTH, &request,
1513                                            &response);
1514
1515         /* Display response */
1516
1517         d_printf("plaintext password authentication %s\n",
1518                  (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1519
1520         if (response.data.auth.nt_status)
1521                 d_fprintf(stderr,
1522                          "error code was %s (0x%x)\nerror messsage was: %s\n",
1523                          response.data.auth.nt_status_string,
1524                          response.data.auth.nt_status,
1525                          response.data.auth.error_string);
1526
1527         if (result != NSS_STATUS_SUCCESS)
1528                 return false;
1529
1530         if (response.extra_data.data == NULL) {
1531                 d_fprintf(stderr, "Did not get token data\n");
1532                 return false;
1533         }
1534
1535         if (!afs_settoken_str((char *)response.extra_data.data)) {
1536                 d_fprintf(stderr, "Could not set token\n");
1537                 return false;
1538         }
1539
1540         d_printf("Successfully created AFS token\n");
1541         return true;
1542 }
1543 #else
1544 static bool wbinfo_klog(char *username)
1545 {
1546         d_fprintf(stderr, "No AFS support compiled in.\n");
1547         return false;
1548 }
1549 #endif
1550
1551 /* Print domain users */
1552
1553 static bool print_domain_users(const char *domain)
1554 {
1555         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1556         uint32_t i;
1557         uint32_t num_users = 0;
1558         const char **users = NULL;
1559
1560         /* Send request to winbind daemon */
1561
1562         /* '.' is the special sign for our own domain */
1563         if (domain && strcmp(domain, ".") == 0) {
1564                 domain = get_winbind_domain();
1565         }
1566
1567         wbc_status = wbcListUsers(domain, &num_users, &users);
1568         if (!WBC_ERROR_IS_OK(wbc_status)) {
1569                 return false;
1570         }
1571
1572         for (i=0; i < num_users; i++) {
1573                 d_printf("%s\n", users[i]);
1574         }
1575
1576         wbcFreeMemory(users);
1577
1578         return true;
1579 }
1580
1581 /* Print domain groups */
1582
1583 static bool print_domain_groups(const char *domain)
1584 {
1585         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1586         uint32_t i;
1587         uint32_t num_groups = 0;
1588         const char **groups = NULL;
1589
1590         /* Send request to winbind daemon */
1591
1592         /* '.' is the special sign for our own domain */
1593         if (domain && strcmp(domain, ".") == 0) {
1594                 domain = get_winbind_domain();
1595         }
1596
1597         wbc_status = wbcListGroups(domain, &num_groups, &groups);
1598         if (!WBC_ERROR_IS_OK(wbc_status)) {
1599                 return false;
1600         }
1601
1602         for (i=0; i < num_groups; i++) {
1603                 d_printf("%s\n", groups[i]);
1604         }
1605
1606         wbcFreeMemory(groups);
1607
1608         return true;
1609 }
1610
1611 /* Set the authorised user for winbindd access in secrets.tdb */
1612
1613 static bool wbinfo_set_auth_user(char *username)
1614 {
1615         d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1616                           "See 'net help setauthuser' for details.\n");
1617         return false;
1618 }
1619
1620 static void wbinfo_get_auth_user(void)
1621 {
1622         d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1623                           "See 'net help getauthuser' for details.\n");
1624 }
1625
1626 static bool wbinfo_ping(void)
1627 {
1628         wbcErr wbc_status;
1629
1630         wbc_status = wbcPing();
1631
1632         /* Display response */
1633
1634         d_printf("Ping to winbindd %s\n",
1635                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1636
1637         return WBC_ERROR_IS_OK(wbc_status);
1638 }
1639
1640 static bool wbinfo_change_user_password(const char *username)
1641 {
1642         wbcErr wbc_status;
1643         char *old_password = NULL;
1644         char *new_password = NULL;
1645         TALLOC_CTX *frame = talloc_tos();
1646
1647         old_password = wbinfo_prompt_pass(frame, "old", username);
1648         new_password = wbinfo_prompt_pass(frame, "new", username);
1649
1650         wbc_status = wbcChangeUserPassword(username, old_password,new_password);
1651
1652         /* Display response */
1653
1654         d_printf("Password change for user %s %s\n", username,
1655                 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1656
1657         return WBC_ERROR_IS_OK(wbc_status);
1658 }
1659
1660 /* Main program */
1661
1662 enum {
1663         OPT_SET_AUTH_USER = 1000,
1664         OPT_GET_AUTH_USER,
1665         OPT_DOMAIN_NAME,
1666         OPT_SEQUENCE,
1667         OPT_GETDCNAME,
1668         OPT_DSGETDCNAME,
1669         OPT_USERDOMGROUPS,
1670         OPT_SIDALIASES,
1671         OPT_USERSIDS,
1672         OPT_ALLOCATE_UID,
1673         OPT_ALLOCATE_GID,
1674         OPT_SET_UID_MAPPING,
1675         OPT_SET_GID_MAPPING,
1676         OPT_REMOVE_UID_MAPPING,
1677         OPT_REMOVE_GID_MAPPING,
1678         OPT_SEPARATOR,
1679         OPT_LIST_ALL_DOMAINS,
1680         OPT_LIST_OWN_DOMAIN,
1681         OPT_UID_INFO,
1682         OPT_USER_SIDINFO,
1683         OPT_GROUP_INFO,
1684         OPT_GID_INFO,
1685         OPT_VERBOSE,
1686         OPT_ONLINESTATUS,
1687         OPT_CHANGE_USER_PASSWORD,
1688         OPT_SID_TO_FULLNAME,
1689         OPT_NTLMV2,
1690         OPT_LANMAN
1691 };
1692
1693 int main(int argc, char **argv, char **envp)
1694 {
1695         int opt;
1696         TALLOC_CTX *frame = talloc_stackframe();
1697         poptContext pc;
1698         static char *string_arg;
1699         char *string_subarg = NULL;
1700         static char *opt_domain_name;
1701         static int int_arg;
1702         int int_subarg = -1;
1703         int result = 1;
1704         bool verbose = false;
1705         bool use_ntlmv2 = false;
1706         bool use_lanman = false;
1707
1708         struct poptOption long_options[] = {
1709                 POPT_AUTOHELP
1710
1711                 /* longName, shortName, argInfo, argPtr, value, descrip,
1712                    argDesc */
1713
1714                 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1715                 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1716                 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1717                 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1718                 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1719                 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1720                 { "sid-to-fullname", 0, POPT_ARG_STRING, &string_arg,
1721                   OPT_SID_TO_FULLNAME, "Converts sid to fullname", "SID" },
1722                 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1723                 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1724                 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1725                 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1726                 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1727                 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1728                   "Get a new UID out of idmap" },
1729                 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1730                   "Get a new GID out of idmap" },
1731                 { "set-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_UID_MAPPING, "Create or modify uid to sid mapping in idmap", "UID,SID" },
1732                 { "set-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_GID_MAPPING, "Create or modify gid to sid mapping in idmap", "GID,SID" },
1733                 { "remove-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_UID_MAPPING, "Remove uid to sid mapping in idmap", "UID,SID" },
1734                 { "remove-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_GID_MAPPING, "Remove gid to sid mapping in idmap", "GID,SID" },
1735                 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1736                 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1737                 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1738                 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1739                 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1740                 { "online-status", 0, POPT_ARG_NONE, 0, OPT_ONLINESTATUS, "Show whether domains are marked as online or offline"},
1741                 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1742                 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1743                 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1744                 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1745                 { "user-sidinfo", 0, POPT_ARG_STRING, &string_arg, OPT_USER_SIDINFO, "Get user info from sid", "SID" },
1746                 { "gid-info", 0, POPT_ARG_INT, &int_arg, OPT_GID_INFO, "Get group info from gid", "GID" },
1747                 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1748                 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1749                   OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1750                 { "sid-aliases", 0, POPT_ARG_STRING, &string_arg, OPT_SIDALIASES, "Get sid aliases", "SID" },
1751                 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1752                 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1753                 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1754                 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1755                   "Get a DC name for a foreign domain", "domainname" },
1756                 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
1757                 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1758                 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1759                 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1760 #ifdef WITH_FAKE_KASERVER
1761                 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1762 #endif
1763 #ifdef HAVE_KRB5
1764                 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1765                         /* destroys wbinfo --help output */
1766                         /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1767 #endif
1768                 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1769                 { "verbose", 0, POPT_ARG_NONE, 0, OPT_VERBOSE, "Print additional information per command", NULL },
1770                 { "change-user-password", 0, POPT_ARG_STRING, &string_arg, OPT_CHANGE_USER_PASSWORD, "Change the password for a user", NULL },
1771                 { "ntlmv2", 0, POPT_ARG_NONE, 0, OPT_NTLMV2, "Use NTLMv2 cryptography for user authentication", NULL},
1772                 { "lanman", 0, POPT_ARG_NONE, 0, OPT_LANMAN, "Use lanman cryptography for user authentication", NULL},
1773                 POPT_COMMON_VERSION
1774                 POPT_TABLEEND
1775         };
1776
1777         /* Samba client initialisation */
1778         load_case_tables();
1779
1780
1781         /* Parse options */
1782
1783         pc = poptGetContext("wbinfo", argc, (const char **)argv,
1784                             long_options, 0);
1785
1786         /* Parse command line options */
1787
1788         if (argc == 1) {
1789                 poptPrintHelp(pc, stderr, 0);
1790                 return 1;
1791         }
1792
1793         while((opt = poptGetNextOpt(pc)) != -1) {
1794                 /* get the generic configuration parameters like --domain */
1795                 switch (opt) {
1796                 case OPT_VERBOSE:
1797                         verbose = true;
1798                         break;
1799                 case OPT_NTLMV2:
1800                         use_ntlmv2 = true;
1801                         break;
1802                 case OPT_LANMAN:
1803                         use_lanman = true;
1804                         break;
1805                 }
1806         }
1807
1808         poptFreeContext(pc);
1809
1810         pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1811                             POPT_CONTEXT_KEEP_FIRST);
1812
1813         while((opt = poptGetNextOpt(pc)) != -1) {
1814                 switch (opt) {
1815                 case 'u':
1816                         if (!print_domain_users(opt_domain_name)) {
1817                                 d_fprintf(stderr,
1818                                           "Error looking up domain users\n");
1819                                 goto done;
1820                         }
1821                         break;
1822                 case 'g':
1823                         if (!print_domain_groups(opt_domain_name)) {
1824                                 d_fprintf(stderr,
1825                                           "Error looking up domain groups\n");
1826                                 goto done;
1827                         }
1828                         break;
1829                 case 's':
1830                         if (!wbinfo_lookupsid(string_arg)) {
1831                                 d_fprintf(stderr,
1832                                           "Could not lookup sid %s\n",
1833                                           string_arg);
1834                                 goto done;
1835                         }
1836                         break;
1837                 case OPT_SID_TO_FULLNAME:
1838                         if (!wbinfo_lookupsid_fullname(string_arg)) {
1839                                 d_fprintf(stderr, "Could not lookup sid %s\n",
1840                                           string_arg);
1841                                 goto done;
1842                         }
1843                         break;
1844                 case 'R':
1845                         if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
1846                                 d_fprintf(stderr, "Could not lookup RIDs %s\n",
1847                                           string_arg);
1848                                 goto done;
1849                         }
1850                         break;
1851                 case 'n':
1852                         if (!wbinfo_lookupname(string_arg)) {
1853                                 d_fprintf(stderr, "Could not lookup name %s\n",
1854                                           string_arg);
1855                                 goto done;
1856                         }
1857                         break;
1858                 case 'N':
1859                         if (!wbinfo_wins_byname(string_arg)) {
1860                                 d_fprintf(stderr,
1861                                           "Could not lookup WINS by name %s\n",
1862                                           string_arg);
1863                                 goto done;
1864                         }
1865                         break;
1866                 case 'I':
1867                         if (!wbinfo_wins_byip(string_arg)) {
1868                                 d_fprintf(stderr,
1869                                           "Could not lookup WINS by IP %s\n",
1870                                           string_arg);
1871                                 goto done;
1872                         }
1873                         break;
1874                 case 'U':
1875                         if (!wbinfo_uid_to_sid(int_arg)) {
1876                                 d_fprintf(stderr,
1877                                           "Could not convert uid %d to sid\n",
1878                                           int_arg);
1879                                 goto done;
1880                         }
1881                         break;
1882                 case 'G':
1883                         if (!wbinfo_gid_to_sid(int_arg)) {
1884                                 d_fprintf(stderr,
1885                                           "Could not convert gid %d to sid\n",
1886                                           int_arg);
1887                                 goto done;
1888                         }
1889                         break;
1890                 case 'S':
1891                         if (!wbinfo_sid_to_uid(string_arg)) {
1892                                 d_fprintf(stderr,
1893                                           "Could not convert sid %s to uid\n",
1894                                           string_arg);
1895                                 goto done;
1896                         }
1897                         break;
1898                 case 'Y':
1899                         if (!wbinfo_sid_to_gid(string_arg)) {
1900                                 d_fprintf(stderr,
1901                                           "Could not convert sid %s to gid\n",
1902                                           string_arg);
1903                                 goto done;
1904                         }
1905                         break;
1906                 case OPT_ALLOCATE_UID:
1907                         if (!wbinfo_allocate_uid()) {
1908                                 d_fprintf(stderr, "Could not allocate a uid\n");
1909                                 goto done;
1910                         }
1911                         break;
1912                 case OPT_ALLOCATE_GID:
1913                         if (!wbinfo_allocate_gid()) {
1914                                 d_fprintf(stderr, "Could not allocate a gid\n");
1915                                 goto done;
1916                         }
1917                         break;
1918                 case OPT_SET_UID_MAPPING:
1919                         if (!parse_mapping_arg(string_arg, &int_subarg,
1920                                 &string_subarg) ||
1921                             !wbinfo_set_uid_mapping(int_subarg, string_subarg))
1922                         {
1923                                 d_fprintf(stderr, "Could not create or modify "
1924                                           "uid to sid mapping\n");
1925                                 goto done;
1926                         }
1927                         break;
1928                 case OPT_SET_GID_MAPPING:
1929                         if (!parse_mapping_arg(string_arg, &int_subarg,
1930                                 &string_subarg) ||
1931                             !wbinfo_set_gid_mapping(int_subarg, string_subarg))
1932                         {
1933                                 d_fprintf(stderr, "Could not create or modify "
1934                                           "gid to sid mapping\n");
1935                                 goto done;
1936                         }
1937                         break;
1938                 case OPT_REMOVE_UID_MAPPING:
1939                         if (!parse_mapping_arg(string_arg, &int_subarg,
1940                                 &string_subarg) ||
1941                             !wbinfo_remove_uid_mapping(int_subarg,
1942                                 string_subarg))
1943                         {
1944                                 d_fprintf(stderr, "Could not remove uid to sid "
1945                                     "mapping\n");
1946                                 goto done;
1947                         }
1948                         break;
1949                 case OPT_REMOVE_GID_MAPPING:
1950                         if (!parse_mapping_arg(string_arg, &int_subarg,
1951                                 &string_subarg) ||
1952                             !wbinfo_remove_gid_mapping(int_subarg,
1953                                 string_subarg))
1954                         {
1955                                 d_fprintf(stderr, "Could not remove gid to sid "
1956                                     "mapping\n");
1957                                 goto done;
1958                         }
1959                         break;
1960                 case 't':
1961                         if (!wbinfo_check_secret(opt_domain_name)) {
1962                                 d_fprintf(stderr, "Could not check secret\n");
1963                                 goto done;
1964                         }
1965                         break;
1966                 case 'm':
1967                         if (!wbinfo_list_domains(false, verbose)) {
1968                                 d_fprintf(stderr,
1969                                           "Could not list trusted domains\n");
1970                                 goto done;
1971                         }
1972                         break;
1973                 case OPT_SEQUENCE:
1974                         if (!wbinfo_show_sequence(opt_domain_name)) {
1975                                 d_fprintf(stderr,
1976                                           "Could not show sequence numbers\n");
1977                                 goto done;
1978                         }
1979                         break;
1980                 case OPT_ONLINESTATUS:
1981                         if (!wbinfo_show_onlinestatus(opt_domain_name)) {
1982                                 d_fprintf(stderr,
1983                                           "Could not show online-status\n");
1984                                 goto done;
1985                         }
1986                         break;
1987                 case 'D':
1988                         if (!wbinfo_domain_info(string_arg)) {
1989                                 d_fprintf(stderr,
1990                                           "Could not get domain info\n");
1991                                 goto done;
1992                         }
1993                         break;
1994                 case 'i':
1995                         if (!wbinfo_get_userinfo(string_arg)) {
1996                                 d_fprintf(stderr,
1997                                           "Could not get info for user %s\n",
1998                                           string_arg);
1999                                 goto done;
2000                         }
2001                         break;
2002                 case OPT_USER_SIDINFO:
2003                         if ( !wbinfo_get_user_sidinfo(string_arg)) {
2004                                 d_fprintf(stderr,
2005                                           "Could not get info for user "
2006                                           "sid %s\n", string_arg);
2007                                 goto done;
2008                         }
2009                         break;
2010                 case OPT_UID_INFO:
2011                         if ( !wbinfo_get_uidinfo(int_arg)) {
2012                                 d_fprintf(stderr, "Could not get info for uid "
2013                                                 "%d\n", int_arg);
2014                                 goto done;
2015                         }
2016                         break;
2017                 case OPT_GROUP_INFO:
2018                         if ( !wbinfo_get_groupinfo(string_arg)) {
2019                                 d_fprintf(stderr, "Could not get info for "
2020                                           "group %s\n", string_arg);
2021                                 goto done;
2022                         }
2023                         break;
2024                 case OPT_GID_INFO:
2025                         if ( !wbinfo_get_gidinfo(int_arg)) {
2026                                 d_fprintf(stderr, "Could not get info for gid "
2027                                                 "%d\n", int_arg);
2028                                 goto done;
2029                         }
2030                         break;
2031                 case 'r':
2032                         if (!wbinfo_get_usergroups(string_arg)) {
2033                                 d_fprintf(stderr,
2034                                           "Could not get groups for user %s\n",
2035                                           string_arg);
2036                                 goto done;
2037                         }
2038                         break;
2039                 case OPT_USERSIDS:
2040                         if (!wbinfo_get_usersids(string_arg)) {
2041                                 d_fprintf(stderr, "Could not get group SIDs "
2042                                           "for user SID %s\n",
2043                                           string_arg);
2044                                 goto done;
2045                         }
2046                         break;
2047                 case OPT_USERDOMGROUPS:
2048                         if (!wbinfo_get_userdomgroups(string_arg)) {
2049                                 d_fprintf(stderr, "Could not get user's domain "
2050                                          "groups for user SID %s\n",
2051                                          string_arg);
2052                                 goto done;
2053                         }
2054                         break;
2055                 case OPT_SIDALIASES:
2056                         if (!wbinfo_get_sidaliases(opt_domain_name,
2057                                                    string_arg)) {
2058                                 d_fprintf(stderr, "Could not get sid aliases "
2059                                          "for user SID %s\n", string_arg);
2060                                 goto done;
2061                         }
2062                         break;
2063                 case 'a': {
2064                                 bool got_error = false;
2065
2066                                 if (!wbinfo_auth(string_arg)) {
2067                                         d_fprintf(stderr,
2068                                                   "Could not authenticate user "
2069                                                   "%s with plaintext "
2070                                                   "password\n", string_arg);
2071                                         got_error = true;
2072                                 }
2073
2074                                 if (!wbinfo_auth_crap(string_arg, use_ntlmv2,
2075                                                       use_lanman)) {
2076                                         d_fprintf(stderr,
2077                                                 "Could not authenticate user "
2078                                                 "%s with challenge/response\n",
2079                                                 string_arg);
2080                                         got_error = true;
2081                                 }
2082
2083                                 if (got_error)
2084                                         goto done;
2085                                 break;
2086                         }
2087                 case 'K': {
2088                                 uint32_t flags = WBFLAG_PAM_KRB5 |
2089                                                  WBFLAG_PAM_CACHED_LOGIN |
2090                                                 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
2091                                                  WBFLAG_PAM_INFO3_TEXT |
2092                                                  WBFLAG_PAM_CONTACT_TRUSTDOM;
2093
2094                                 if (!wbinfo_auth_krb5(string_arg, "FILE",
2095                                                       flags)) {
2096                                         d_fprintf(stderr,
2097                                                 "Could not authenticate user "
2098                                                 "[%s] with Kerberos "
2099                                                 "(ccache: %s)\n", string_arg,
2100                                                 "FILE");
2101                                         goto done;
2102                                 }
2103                                 break;
2104                         }
2105                 case 'k':
2106                         if (!wbinfo_klog(string_arg)) {
2107                                 d_fprintf(stderr, "Could not klog user\n");
2108                                 goto done;
2109                         }
2110                         break;
2111                 case 'p':
2112                         if (!wbinfo_ping()) {
2113                                 d_fprintf(stderr, "could not ping winbindd!\n");
2114                                 goto done;
2115                         }
2116                         break;
2117                 case OPT_SET_AUTH_USER:
2118                         if (!wbinfo_set_auth_user(string_arg)) {
2119                                 goto done;
2120                         }
2121                         break;
2122                 case OPT_GET_AUTH_USER:
2123                         wbinfo_get_auth_user();
2124                         goto done;
2125                         break;
2126                 case OPT_GETDCNAME:
2127                         if (!wbinfo_getdcname(string_arg)) {
2128                                 goto done;
2129                         }
2130                         break;
2131                 case OPT_DSGETDCNAME:
2132                         if (!wbinfo_dsgetdcname(string_arg, 0)) {
2133                                 goto done;
2134                         }
2135                         break;
2136                 case OPT_SEPARATOR: {
2137                         const char sep = winbind_separator();
2138                         if ( !sep ) {
2139                                 goto done;
2140                         }
2141                         d_printf("%c\n", sep);
2142                         break;
2143                 }
2144                 case OPT_LIST_ALL_DOMAINS:
2145                         if (!wbinfo_list_domains(true, verbose)) {
2146                                 goto done;
2147                         }
2148                         break;
2149                 case OPT_LIST_OWN_DOMAIN:
2150                         if (!wbinfo_list_own_domain()) {
2151                                 goto done;
2152                         }
2153                         break;
2154                 case OPT_CHANGE_USER_PASSWORD:
2155                         if (!wbinfo_change_user_password(string_arg)) {
2156                                 d_fprintf(stderr,
2157                                         "Could not change user password "
2158                                          "for user %s\n", string_arg);
2159                                 goto done;
2160                         }
2161                         break;
2162
2163                 /* generic configuration options */
2164                 case OPT_DOMAIN_NAME:
2165                         break;
2166                 case OPT_VERBOSE:
2167                         break;
2168                 case OPT_NTLMV2:
2169                         break;
2170                 case OPT_LANMAN:
2171                         break;
2172                 default:
2173                         d_fprintf(stderr, "Invalid option\n");
2174                         poptPrintHelp(pc, stderr, 0);
2175                         goto done;
2176                 }
2177         }
2178
2179         result = 0;
2180
2181         /* Exit code */
2182
2183  done:
2184         talloc_free(frame);
2185
2186         poptFreeContext(pc);
2187         return result;
2188 }