nsswitch: Add wbinfo --pam-logon
[kai/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    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 /* Authenticate a user with a plaintext password */
1536
1537 static bool wbinfo_pam_logon(char *username)
1538 {
1539         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1540         struct wbcLogonUserParams params;
1541         struct wbcAuthErrorInfo *error;
1542         char *s = NULL;
1543         char *p = NULL;
1544         TALLOC_CTX *frame = talloc_tos();
1545         uint32_t flags;
1546         uint32_t uid;
1547
1548         ZERO_STRUCT(params);
1549
1550         if ((s = talloc_strdup(frame, username)) == NULL) {
1551                 return false;
1552         }
1553
1554         if ((p = strchr(s, '%')) != NULL) {
1555                 *p = 0;
1556                 p++;
1557                 params.password = talloc_strdup(frame, p);
1558         } else {
1559                 params.password = wbinfo_prompt_pass(frame, NULL, username);
1560         }
1561         params.username = s;
1562
1563         flags = WBFLAG_PAM_CACHED_LOGIN;
1564
1565         wbc_status = wbcAddNamedBlob(&params.num_blobs, &params.blobs,
1566                                      "flags", 0,
1567                                      (uint8_t *)&flags, sizeof(flags));
1568         if (!WBC_ERROR_IS_OK(wbc_status)) {
1569                 d_printf("wbcAddNamedBlob failed: %s\n",
1570                          wbcErrorString(wbc_status));
1571                 return false;
1572         }
1573
1574         uid = getuid();
1575
1576         wbc_status = wbcAddNamedBlob(&params.num_blobs, &params.blobs,
1577                                      "user_uid", 0,
1578                                      (uint8_t *)&uid, sizeof(uid));
1579         if (!WBC_ERROR_IS_OK(wbc_status)) {
1580                 d_printf("wbcAddNamedBlob failed: %s\n",
1581                          wbcErrorString(wbc_status));
1582                 return false;
1583         }
1584
1585         wbc_status = wbcLogonUser(&params, NULL, &error, NULL);
1586
1587         wbcFreeMemory(params.blobs);
1588
1589         d_printf("plaintext password authentication %s\n",
1590                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1591
1592         if (!WBC_ERROR_IS_OK(wbc_status)) {
1593                 d_fprintf(stderr,
1594                           "error code was %s (0x%x)\nerror messsage was: %s\n",
1595                           error->nt_string,
1596                           (int)error->nt_status,
1597                           error->display_string);
1598                 wbcFreeMemory(error);
1599                 return false;
1600         }
1601         return true;
1602 }
1603
1604 /* Save creds with winbind */
1605
1606 static bool wbinfo_ccache_save(char *username)
1607 {
1608         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1609         char *s = NULL;
1610         char *p = NULL;
1611         char *password = NULL;
1612         char *name = NULL;
1613         TALLOC_CTX *frame = talloc_stackframe();
1614
1615         s = talloc_strdup(frame, username);
1616         if (s == NULL) {
1617                 return false;
1618         }
1619
1620         p = strchr(s, '%');
1621         if (p != NULL) {
1622                 *p = 0;
1623                 p++;
1624                 password = talloc_strdup(frame, p);
1625         } else {
1626                 password = wbinfo_prompt_pass(frame, NULL, username);
1627         }
1628
1629         name = s;
1630
1631         wbc_status = wbcCredentialSave(name, password);
1632
1633         d_printf("saving creds %s\n",
1634                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1635
1636         TALLOC_FREE(frame);
1637
1638         return WBC_ERROR_IS_OK(wbc_status);
1639 }
1640
1641 #ifdef WITH_FAKE_KASERVER
1642 /* Authenticate a user with a plaintext password and set a token */
1643
1644 static bool wbinfo_klog(char *username)
1645 {
1646         struct winbindd_request request;
1647         struct winbindd_response response;
1648         NSS_STATUS result;
1649         char *p;
1650
1651         /* Send off request */
1652
1653         ZERO_STRUCT(request);
1654         ZERO_STRUCT(response);
1655
1656         p = strchr(username, '%');
1657
1658         if (p) {
1659                 *p = 0;
1660                 fstrcpy(request.data.auth.user, username);
1661                 fstrcpy(request.data.auth.pass, p + 1);
1662                 *p = '%';
1663         } else {
1664                 fstrcpy(request.data.auth.user, username);
1665                 fstrcpy(request.data.auth.pass, getpass("Password: "));
1666         }
1667
1668         request.flags |= WBFLAG_PAM_AFS_TOKEN;
1669
1670         result = winbindd_request_response(WINBINDD_PAM_AUTH, &request,
1671                                            &response);
1672
1673         /* Display response */
1674
1675         d_printf("plaintext password authentication %s\n",
1676                  (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1677
1678         if (response.data.auth.nt_status)
1679                 d_fprintf(stderr,
1680                          "error code was %s (0x%x)\nerror messsage was: %s\n",
1681                          response.data.auth.nt_status_string,
1682                          response.data.auth.nt_status,
1683                          response.data.auth.error_string);
1684
1685         if (result != NSS_STATUS_SUCCESS)
1686                 return false;
1687
1688         if (response.extra_data.data == NULL) {
1689                 d_fprintf(stderr, "Did not get token data\n");
1690                 return false;
1691         }
1692
1693         if (!afs_settoken_str((char *)response.extra_data.data)) {
1694                 d_fprintf(stderr, "Could not set token\n");
1695                 return false;
1696         }
1697
1698         d_printf("Successfully created AFS token\n");
1699         return true;
1700 }
1701 #else
1702 static bool wbinfo_klog(char *username)
1703 {
1704         d_fprintf(stderr, "No AFS support compiled in.\n");
1705         return false;
1706 }
1707 #endif
1708
1709 /* Print domain users */
1710
1711 static bool print_domain_users(const char *domain)
1712 {
1713         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1714         uint32_t i;
1715         uint32_t num_users = 0;
1716         const char **users = NULL;
1717
1718         /* Send request to winbind daemon */
1719
1720         /* '.' is the special sign for our own domain */
1721         if (domain && strcmp(domain, ".") == 0) {
1722                 domain = get_winbind_domain();
1723         }
1724
1725         wbc_status = wbcListUsers(domain, &num_users, &users);
1726         if (!WBC_ERROR_IS_OK(wbc_status)) {
1727                 return false;
1728         }
1729
1730         for (i=0; i < num_users; i++) {
1731                 d_printf("%s\n", users[i]);
1732         }
1733
1734         wbcFreeMemory(users);
1735
1736         return true;
1737 }
1738
1739 /* Print domain groups */
1740
1741 static bool print_domain_groups(const char *domain)
1742 {
1743         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1744         uint32_t i;
1745         uint32_t num_groups = 0;
1746         const char **groups = NULL;
1747
1748         /* Send request to winbind daemon */
1749
1750         /* '.' is the special sign for our own domain */
1751         if (domain && strcmp(domain, ".") == 0) {
1752                 domain = get_winbind_domain();
1753         }
1754
1755         wbc_status = wbcListGroups(domain, &num_groups, &groups);
1756         if (!WBC_ERROR_IS_OK(wbc_status)) {
1757                 return false;
1758         }
1759
1760         for (i=0; i < num_groups; i++) {
1761                 d_printf("%s\n", groups[i]);
1762         }
1763
1764         wbcFreeMemory(groups);
1765
1766         return true;
1767 }
1768
1769 /* Set the authorised user for winbindd access in secrets.tdb */
1770
1771 static bool wbinfo_set_auth_user(char *username)
1772 {
1773         d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1774                           "See 'net help setauthuser' for details.\n");
1775         return false;
1776 }
1777
1778 static void wbinfo_get_auth_user(void)
1779 {
1780         d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1781                           "See 'net help getauthuser' for details.\n");
1782 }
1783
1784 static bool wbinfo_ping(void)
1785 {
1786         wbcErr wbc_status;
1787
1788         wbc_status = wbcPing();
1789
1790         /* Display response */
1791
1792         d_printf("Ping to winbindd %s\n",
1793                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1794
1795         return WBC_ERROR_IS_OK(wbc_status);
1796 }
1797
1798 static bool wbinfo_change_user_password(const char *username)
1799 {
1800         wbcErr wbc_status;
1801         char *old_password = NULL;
1802         char *new_password = NULL;
1803         TALLOC_CTX *frame = talloc_tos();
1804
1805         old_password = wbinfo_prompt_pass(frame, "old", username);
1806         new_password = wbinfo_prompt_pass(frame, "new", username);
1807
1808         wbc_status = wbcChangeUserPassword(username, old_password,new_password);
1809
1810         /* Display response */
1811
1812         d_printf("Password change for user %s %s\n", username,
1813                 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1814
1815         return WBC_ERROR_IS_OK(wbc_status);
1816 }
1817
1818 /* Main program */
1819
1820 enum {
1821         OPT_SET_AUTH_USER = 1000,
1822         OPT_GET_AUTH_USER,
1823         OPT_DOMAIN_NAME,
1824         OPT_SEQUENCE,
1825         OPT_GETDCNAME,
1826         OPT_DSGETDCNAME,
1827         OPT_USERDOMGROUPS,
1828         OPT_SIDALIASES,
1829         OPT_USERSIDS,
1830         OPT_ALLOCATE_UID,
1831         OPT_ALLOCATE_GID,
1832         OPT_SET_UID_MAPPING,
1833         OPT_SET_GID_MAPPING,
1834         OPT_REMOVE_UID_MAPPING,
1835         OPT_REMOVE_GID_MAPPING,
1836         OPT_SEPARATOR,
1837         OPT_LIST_ALL_DOMAINS,
1838         OPT_LIST_OWN_DOMAIN,
1839         OPT_UID_INFO,
1840         OPT_USER_SIDINFO,
1841         OPT_GROUP_INFO,
1842         OPT_GID_INFO,
1843         OPT_VERBOSE,
1844         OPT_ONLINESTATUS,
1845         OPT_CHANGE_USER_PASSWORD,
1846         OPT_PING_DC,
1847         OPT_CCACHE_SAVE,
1848         OPT_SID_TO_FULLNAME,
1849         OPT_NTLMV2,
1850         OPT_PAM_LOGON,
1851         OPT_LOGOFF,
1852         OPT_LOGOFF_USER,
1853         OPT_LOGOFF_UID,
1854         OPT_LANMAN
1855 };
1856
1857 int main(int argc, char **argv, char **envp)
1858 {
1859         int opt;
1860         TALLOC_CTX *frame = talloc_stackframe();
1861         poptContext pc;
1862         static char *string_arg;
1863         char *string_subarg = NULL;
1864         static char *opt_domain_name;
1865         static int int_arg;
1866         int int_subarg = -1;
1867         int result = 1;
1868         bool verbose = false;
1869         bool use_ntlmv2 = false;
1870         bool use_lanman = false;
1871         char *logoff_user = getenv("USER");
1872         int logoff_uid = geteuid();
1873
1874         struct poptOption long_options[] = {
1875                 POPT_AUTOHELP
1876
1877                 /* longName, shortName, argInfo, argPtr, value, descrip,
1878                    argDesc */
1879
1880                 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1881                 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1882                 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1883                 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1884                 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1885                 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1886                 { "sid-to-fullname", 0, POPT_ARG_STRING, &string_arg,
1887                   OPT_SID_TO_FULLNAME, "Converts sid to fullname", "SID" },
1888                 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1889                 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1890                 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1891                 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1892                 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1893                 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1894                   "Get a new UID out of idmap" },
1895                 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1896                   "Get a new GID out of idmap" },
1897                 { "set-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_UID_MAPPING, "Create or modify uid to sid mapping in idmap", "UID,SID" },
1898                 { "set-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_GID_MAPPING, "Create or modify gid to sid mapping in idmap", "GID,SID" },
1899                 { "remove-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_UID_MAPPING, "Remove uid to sid mapping in idmap", "UID,SID" },
1900                 { "remove-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_GID_MAPPING, "Remove gid to sid mapping in idmap", "GID,SID" },
1901                 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1902                 { "change-secret", 'c', POPT_ARG_NONE, 0, 'c', "Change shared secret" },
1903                 { "ping-dc", 0, POPT_ARG_NONE, 0, OPT_PING_DC,
1904                   "Check the NETLOGON connection" },
1905                 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1906                 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1907                 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1908                 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1909                 { "online-status", 0, POPT_ARG_NONE, 0, OPT_ONLINESTATUS, "Show whether domains are marked as online or offline"},
1910                 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1911                 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1912                 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1913                 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1914                 { "user-sidinfo", 0, POPT_ARG_STRING, &string_arg, OPT_USER_SIDINFO, "Get user info from sid", "SID" },
1915                 { "gid-info", 0, POPT_ARG_INT, &int_arg, OPT_GID_INFO, "Get group info from gid", "GID" },
1916                 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1917                 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1918                   OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1919                 { "sid-aliases", 0, POPT_ARG_STRING, &string_arg, OPT_SIDALIASES, "Get sid aliases", "SID" },
1920                 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1921                 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1922                 { "pam-logon", 0, POPT_ARG_STRING, string_arg, OPT_PAM_LOGON,
1923                   "do a pam logon equivalent", "user%password" },
1924                 { "logoff", 0, POPT_ARG_NONE, NULL, OPT_LOGOFF,
1925                   "log off user", "uid" },
1926                 { "logoff-user", 0, POPT_ARG_STRING, &logoff_user,
1927                   OPT_LOGOFF_USER, "username to log off" },
1928                 { "logoff-uid", 0, POPT_ARG_INT, &logoff_uid,
1929                   OPT_LOGOFF_UID, "uid to log off" },
1930                 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1931                 { "ccache-save", 0, POPT_ARG_STRING, &string_arg,
1932                   OPT_CCACHE_SAVE, "Store user and password for ccache "
1933                   "operation", "user%password" },
1934                 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1935                   "Get a DC name for a foreign domain", "domainname" },
1936                 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
1937                 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1938                 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1939                 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1940 #ifdef WITH_FAKE_KASERVER
1941                 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1942 #endif
1943 #ifdef HAVE_KRB5
1944                 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1945                         /* destroys wbinfo --help output */
1946                         /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1947 #endif
1948                 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1949                 { "verbose", 0, POPT_ARG_NONE, 0, OPT_VERBOSE, "Print additional information per command", NULL },
1950                 { "change-user-password", 0, POPT_ARG_STRING, &string_arg, OPT_CHANGE_USER_PASSWORD, "Change the password for a user", NULL },
1951                 { "ntlmv2", 0, POPT_ARG_NONE, 0, OPT_NTLMV2, "Use NTLMv2 cryptography for user authentication", NULL},
1952                 { "lanman", 0, POPT_ARG_NONE, 0, OPT_LANMAN, "Use lanman cryptography for user authentication", NULL},
1953                 POPT_COMMON_VERSION
1954                 POPT_TABLEEND
1955         };
1956
1957         /* Samba client initialisation */
1958         load_case_tables();
1959
1960
1961         /* Parse options */
1962
1963         pc = poptGetContext("wbinfo", argc, (const char **)argv,
1964                             long_options, 0);
1965
1966         /* Parse command line options */
1967
1968         if (argc == 1) {
1969                 poptPrintHelp(pc, stderr, 0);
1970                 return 1;
1971         }
1972
1973         while((opt = poptGetNextOpt(pc)) != -1) {
1974                 /* get the generic configuration parameters like --domain */
1975                 switch (opt) {
1976                 case OPT_VERBOSE:
1977                         verbose = true;
1978                         break;
1979                 case OPT_NTLMV2:
1980                         use_ntlmv2 = true;
1981                         break;
1982                 case OPT_LANMAN:
1983                         use_lanman = true;
1984                         break;
1985                 }
1986         }
1987
1988         poptFreeContext(pc);
1989
1990         pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1991                             POPT_CONTEXT_KEEP_FIRST);
1992
1993         while((opt = poptGetNextOpt(pc)) != -1) {
1994                 switch (opt) {
1995                 case 'u':
1996                         if (!print_domain_users(opt_domain_name)) {
1997                                 d_fprintf(stderr,
1998                                           "Error looking up domain users\n");
1999                                 goto done;
2000                         }
2001                         break;
2002                 case 'g':
2003                         if (!print_domain_groups(opt_domain_name)) {
2004                                 d_fprintf(stderr,
2005                                           "Error looking up domain groups\n");
2006                                 goto done;
2007                         }
2008                         break;
2009                 case 's':
2010                         if (!wbinfo_lookupsid(string_arg)) {
2011                                 d_fprintf(stderr,
2012                                           "Could not lookup sid %s\n",
2013                                           string_arg);
2014                                 goto done;
2015                         }
2016                         break;
2017                 case OPT_SID_TO_FULLNAME:
2018                         if (!wbinfo_lookupsid_fullname(string_arg)) {
2019                                 d_fprintf(stderr, "Could not lookup sid %s\n",
2020                                           string_arg);
2021                                 goto done;
2022                         }
2023                         break;
2024                 case 'R':
2025                         if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
2026                                 d_fprintf(stderr, "Could not lookup RIDs %s\n",
2027                                           string_arg);
2028                                 goto done;
2029                         }
2030                         break;
2031                 case 'n':
2032                         if (!wbinfo_lookupname(string_arg)) {
2033                                 d_fprintf(stderr, "Could not lookup name %s\n",
2034                                           string_arg);
2035                                 goto done;
2036                         }
2037                         break;
2038                 case 'N':
2039                         if (!wbinfo_wins_byname(string_arg)) {
2040                                 d_fprintf(stderr,
2041                                           "Could not lookup WINS by name %s\n",
2042                                           string_arg);
2043                                 goto done;
2044                         }
2045                         break;
2046                 case 'I':
2047                         if (!wbinfo_wins_byip(string_arg)) {
2048                                 d_fprintf(stderr,
2049                                           "Could not lookup WINS by IP %s\n",
2050                                           string_arg);
2051                                 goto done;
2052                         }
2053                         break;
2054                 case 'U':
2055                         if (!wbinfo_uid_to_sid(int_arg)) {
2056                                 d_fprintf(stderr,
2057                                           "Could not convert uid %d to sid\n",
2058                                           int_arg);
2059                                 goto done;
2060                         }
2061                         break;
2062                 case 'G':
2063                         if (!wbinfo_gid_to_sid(int_arg)) {
2064                                 d_fprintf(stderr,
2065                                           "Could not convert gid %d to sid\n",
2066                                           int_arg);
2067                                 goto done;
2068                         }
2069                         break;
2070                 case 'S':
2071                         if (!wbinfo_sid_to_uid(string_arg)) {
2072                                 d_fprintf(stderr,
2073                                           "Could not convert sid %s to uid\n",
2074                                           string_arg);
2075                                 goto done;
2076                         }
2077                         break;
2078                 case 'Y':
2079                         if (!wbinfo_sid_to_gid(string_arg)) {
2080                                 d_fprintf(stderr,
2081                                           "Could not convert sid %s to gid\n",
2082                                           string_arg);
2083                                 goto done;
2084                         }
2085                         break;
2086                 case OPT_ALLOCATE_UID:
2087                         if (!wbinfo_allocate_uid()) {
2088                                 d_fprintf(stderr, "Could not allocate a uid\n");
2089                                 goto done;
2090                         }
2091                         break;
2092                 case OPT_ALLOCATE_GID:
2093                         if (!wbinfo_allocate_gid()) {
2094                                 d_fprintf(stderr, "Could not allocate a gid\n");
2095                                 goto done;
2096                         }
2097                         break;
2098                 case OPT_SET_UID_MAPPING:
2099                         if (!parse_mapping_arg(string_arg, &int_subarg,
2100                                 &string_subarg) ||
2101                             !wbinfo_set_uid_mapping(int_subarg, string_subarg))
2102                         {
2103                                 d_fprintf(stderr, "Could not create or modify "
2104                                           "uid to sid mapping\n");
2105                                 goto done;
2106                         }
2107                         break;
2108                 case OPT_SET_GID_MAPPING:
2109                         if (!parse_mapping_arg(string_arg, &int_subarg,
2110                                 &string_subarg) ||
2111                             !wbinfo_set_gid_mapping(int_subarg, string_subarg))
2112                         {
2113                                 d_fprintf(stderr, "Could not create or modify "
2114                                           "gid to sid mapping\n");
2115                                 goto done;
2116                         }
2117                         break;
2118                 case OPT_REMOVE_UID_MAPPING:
2119                         if (!parse_mapping_arg(string_arg, &int_subarg,
2120                                 &string_subarg) ||
2121                             !wbinfo_remove_uid_mapping(int_subarg,
2122                                 string_subarg))
2123                         {
2124                                 d_fprintf(stderr, "Could not remove uid to sid "
2125                                     "mapping\n");
2126                                 goto done;
2127                         }
2128                         break;
2129                 case OPT_REMOVE_GID_MAPPING:
2130                         if (!parse_mapping_arg(string_arg, &int_subarg,
2131                                 &string_subarg) ||
2132                             !wbinfo_remove_gid_mapping(int_subarg,
2133                                 string_subarg))
2134                         {
2135                                 d_fprintf(stderr, "Could not remove gid to sid "
2136                                     "mapping\n");
2137                                 goto done;
2138                         }
2139                         break;
2140                 case 't':
2141                         if (!wbinfo_check_secret(opt_domain_name)) {
2142                                 d_fprintf(stderr, "Could not check secret\n");
2143                                 goto done;
2144                         }
2145                         break;
2146                 case 'c':
2147                         if (!wbinfo_change_secret(opt_domain_name)) {
2148                                 d_fprintf(stderr, "Could not change secret\n");
2149                                 goto done;
2150                         }
2151                         break;
2152                 case OPT_PING_DC:
2153                         if (!wbinfo_ping_dc()) {
2154                                 d_fprintf(stderr, "Could not ping our DC\n");
2155                                 goto done;
2156                         }
2157                         break;
2158                 case 'm':
2159                         if (!wbinfo_list_domains(false, verbose)) {
2160                                 d_fprintf(stderr,
2161                                           "Could not list trusted domains\n");
2162                                 goto done;
2163                         }
2164                         break;
2165                 case OPT_SEQUENCE:
2166                         if (!wbinfo_show_sequence(opt_domain_name)) {
2167                                 d_fprintf(stderr,
2168                                           "Could not show sequence numbers\n");
2169                                 goto done;
2170                         }
2171                         break;
2172                 case OPT_ONLINESTATUS:
2173                         if (!wbinfo_show_onlinestatus(opt_domain_name)) {
2174                                 d_fprintf(stderr,
2175                                           "Could not show online-status\n");
2176                                 goto done;
2177                         }
2178                         break;
2179                 case 'D':
2180                         if (!wbinfo_domain_info(string_arg)) {
2181                                 d_fprintf(stderr,
2182                                           "Could not get domain info\n");
2183                                 goto done;
2184                         }
2185                         break;
2186                 case 'i':
2187                         if (!wbinfo_get_userinfo(string_arg)) {
2188                                 d_fprintf(stderr,
2189                                           "Could not get info for user %s\n",
2190                                           string_arg);
2191                                 goto done;
2192                         }
2193                         break;
2194                 case OPT_USER_SIDINFO:
2195                         if ( !wbinfo_get_user_sidinfo(string_arg)) {
2196                                 d_fprintf(stderr,
2197                                           "Could not get info for user "
2198                                           "sid %s\n", string_arg);
2199                                 goto done;
2200                         }
2201                         break;
2202                 case OPT_UID_INFO:
2203                         if ( !wbinfo_get_uidinfo(int_arg)) {
2204                                 d_fprintf(stderr, "Could not get info for uid "
2205                                                 "%d\n", int_arg);
2206                                 goto done;
2207                         }
2208                         break;
2209                 case OPT_GROUP_INFO:
2210                         if ( !wbinfo_get_groupinfo(string_arg)) {
2211                                 d_fprintf(stderr, "Could not get info for "
2212                                           "group %s\n", string_arg);
2213                                 goto done;
2214                         }
2215                         break;
2216                 case OPT_GID_INFO:
2217                         if ( !wbinfo_get_gidinfo(int_arg)) {
2218                                 d_fprintf(stderr, "Could not get info for gid "
2219                                                 "%d\n", int_arg);
2220                                 goto done;
2221                         }
2222                         break;
2223                 case 'r':
2224                         if (!wbinfo_get_usergroups(string_arg)) {
2225                                 d_fprintf(stderr,
2226                                           "Could not get groups for user %s\n",
2227                                           string_arg);
2228                                 goto done;
2229                         }
2230                         break;
2231                 case OPT_USERSIDS:
2232                         if (!wbinfo_get_usersids(string_arg)) {
2233                                 d_fprintf(stderr, "Could not get group SIDs "
2234                                           "for user SID %s\n",
2235                                           string_arg);
2236                                 goto done;
2237                         }
2238                         break;
2239                 case OPT_USERDOMGROUPS:
2240                         if (!wbinfo_get_userdomgroups(string_arg)) {
2241                                 d_fprintf(stderr, "Could not get user's domain "
2242                                          "groups for user SID %s\n",
2243                                          string_arg);
2244                                 goto done;
2245                         }
2246                         break;
2247                 case OPT_SIDALIASES:
2248                         if (!wbinfo_get_sidaliases(opt_domain_name,
2249                                                    string_arg)) {
2250                                 d_fprintf(stderr, "Could not get sid aliases "
2251                                          "for user SID %s\n", string_arg);
2252                                 goto done;
2253                         }
2254                         break;
2255                 case 'a': {
2256                                 bool got_error = false;
2257
2258                                 if (!wbinfo_auth(string_arg)) {
2259                                         d_fprintf(stderr,
2260                                                   "Could not authenticate user "
2261                                                   "%s with plaintext "
2262                                                   "password\n", string_arg);
2263                                         got_error = true;
2264                                 }
2265
2266                                 if (!wbinfo_auth_crap(string_arg, use_ntlmv2,
2267                                                       use_lanman)) {
2268                                         d_fprintf(stderr,
2269                                                 "Could not authenticate user "
2270                                                 "%s with challenge/response\n",
2271                                                 string_arg);
2272                                         got_error = true;
2273                                 }
2274
2275                                 if (got_error)
2276                                         goto done;
2277                                 break;
2278                         }
2279                 case OPT_PAM_LOGON:
2280                         if (!wbinfo_pam_logon(string_arg)) {
2281                                 d_fprintf(stderr, "pam_logon failed for %s\n",
2282                                           string_arg);
2283                                 goto done;
2284                         }
2285                 case OPT_LOGOFF:
2286                 {
2287                         wbcErr wbc_status;
2288
2289                         wbc_status = wbcLogoffUser(logoff_user, logoff_uid,
2290                                                    "");
2291                         d_printf("Logoff %s (%d): %s\n", logoff_user,
2292                                  logoff_uid, wbcErrorString(wbc_status));
2293                         break;
2294                 }
2295                 case 'K': {
2296                                 uint32_t flags = WBFLAG_PAM_KRB5 |
2297                                                  WBFLAG_PAM_CACHED_LOGIN |
2298                                                 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
2299                                                  WBFLAG_PAM_INFO3_TEXT |
2300                                                  WBFLAG_PAM_CONTACT_TRUSTDOM;
2301
2302                                 if (!wbinfo_auth_krb5(string_arg, "FILE",
2303                                                       flags)) {
2304                                         d_fprintf(stderr,
2305                                                 "Could not authenticate user "
2306                                                 "[%s] with Kerberos "
2307                                                 "(ccache: %s)\n", string_arg,
2308                                                 "FILE");
2309                                         goto done;
2310                                 }
2311                                 break;
2312                         }
2313                 case 'k':
2314                         if (!wbinfo_klog(string_arg)) {
2315                                 d_fprintf(stderr, "Could not klog user\n");
2316                                 goto done;
2317                         }
2318                         break;
2319                 case 'p':
2320                         if (!wbinfo_ping()) {
2321                                 d_fprintf(stderr, "could not ping winbindd!\n");
2322                                 goto done;
2323                         }
2324                         break;
2325                 case OPT_SET_AUTH_USER:
2326                         if (!wbinfo_set_auth_user(string_arg)) {
2327                                 goto done;
2328                         }
2329                         break;
2330                 case OPT_GET_AUTH_USER:
2331                         wbinfo_get_auth_user();
2332                         goto done;
2333                         break;
2334                 case OPT_CCACHE_SAVE:
2335                         if (!wbinfo_ccache_save(string_arg)) {
2336                                 goto done;
2337                         }
2338                         break;
2339                 case OPT_GETDCNAME:
2340                         if (!wbinfo_getdcname(string_arg)) {
2341                                 goto done;
2342                         }
2343                         break;
2344                 case OPT_DSGETDCNAME:
2345                         if (!wbinfo_dsgetdcname(string_arg, 0)) {
2346                                 goto done;
2347                         }
2348                         break;
2349                 case OPT_SEPARATOR: {
2350                         const char sep = winbind_separator();
2351                         if ( !sep ) {
2352                                 goto done;
2353                         }
2354                         d_printf("%c\n", sep);
2355                         break;
2356                 }
2357                 case OPT_LIST_ALL_DOMAINS:
2358                         if (!wbinfo_list_domains(true, verbose)) {
2359                                 goto done;
2360                         }
2361                         break;
2362                 case OPT_LIST_OWN_DOMAIN:
2363                         if (!wbinfo_list_own_domain()) {
2364                                 goto done;
2365                         }
2366                         break;
2367                 case OPT_CHANGE_USER_PASSWORD:
2368                         if (!wbinfo_change_user_password(string_arg)) {
2369                                 d_fprintf(stderr,
2370                                         "Could not change user password "
2371                                          "for user %s\n", string_arg);
2372                                 goto done;
2373                         }
2374                         break;
2375
2376                 /* generic configuration options */
2377                 case OPT_DOMAIN_NAME:
2378                 case OPT_VERBOSE:
2379                 case OPT_NTLMV2:
2380                 case OPT_LANMAN:
2381                 case OPT_LOGOFF_USER:
2382                 case OPT_LOGOFF_UID:
2383                         break;
2384                 default:
2385                         d_fprintf(stderr, "Invalid option\n");
2386                         poptPrintHelp(pc, stderr, 0);
2387                         goto done;
2388                 }
2389         }
2390
2391         result = 0;
2392
2393         /* Exit code */
2394
2395  done:
2396         talloc_free(frame);
2397
2398         poptFreeContext(pc);
2399         return result;
2400 }