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