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