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