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