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