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