80a72903438f70387db3497686a3954bcc172c9e
[samba.git] / source / 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
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "winbind_client.h"
25 #include "libwbclient/wbclient.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
29
30 static struct wbcInterfaceDetails *init_interface_details(void)
31 {
32         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
33         static struct wbcInterfaceDetails *details;
34
35         if (details) {
36                 return details;
37         }
38
39         wbc_status = wbcInterfaceDetails(&details);
40         if (!WBC_ERROR_IS_OK(wbc_status)) {
41                 d_fprintf(stderr, "could not obtain winbind interface details!\n");
42         }
43
44         return details;
45 }
46
47 static char winbind_separator_int(bool strict)
48 {
49         struct wbcInterfaceDetails *details;
50         static bool got_sep;
51         static char sep;
52
53         if (got_sep)
54                 return sep;
55
56         details = init_interface_details();
57
58         if (!details) {
59                 d_fprintf(stderr, "could not obtain winbind separator!\n");
60                 if (strict) {
61                         return 0;
62                 }
63                 /* HACK: (this module should not call lp_ funtions) */
64                 return *lp_winbind_separator();
65         }
66
67         sep = details->winbind_separator;
68         got_sep = true;
69
70         if (!sep) {
71                 d_fprintf(stderr, "winbind separator was NULL!\n");
72                 if (strict) {
73                         return 0;
74                 }
75                 /* HACK: (this module should not call lp_ funtions) */
76                 sep = *lp_winbind_separator();
77         }
78         
79         return sep;
80 }
81
82 static char winbind_separator(void)
83 {
84         return winbind_separator_int(false);
85 }
86
87 static const char *get_winbind_domain(void)
88 {
89         static struct wbcInterfaceDetails *details;
90
91         details = init_interface_details();
92
93         if (!details) {
94                 d_fprintf(stderr, "could not obtain winbind domain name!\n");
95
96                 /* HACK: (this module should not call lp_ functions) */
97                 return lp_workgroup();
98         }
99
100         return details->netbios_domain;
101 }
102
103 /* Copy of parse_domain_user from winbindd_util.c.  Parse a string of the
104    form DOMAIN/user into a domain and a user */
105
106 static bool parse_wbinfo_domain_user(const char *domuser, fstring domain,
107                                      fstring user)
108 {
109
110         char *p = strchr(domuser,winbind_separator());
111
112         if (!p) {
113                 /* Maybe it was a UPN? */
114                 if ((p = strchr(domuser, '@')) != NULL) {
115                         fstrcpy(domain, "");
116                         fstrcpy(user, domuser);
117                         return true;
118                 }
119
120                 fstrcpy(user, domuser);
121                 fstrcpy(domain, get_winbind_domain());
122                 return true;
123         }
124
125         fstrcpy(user, p+1);
126         fstrcpy(domain, domuser);
127         domain[PTR_DIFF(p, domuser)] = 0;
128         strupper_m(domain);
129
130         return true;
131 }
132
133 /* pull pwent info for a given user */
134
135 static bool wbinfo_get_userinfo(char *user)
136 {
137         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
138         struct passwd *pwd = NULL;
139
140         wbc_status = wbcGetpwnam(user, &pwd);
141         if (!WBC_ERROR_IS_OK(wbc_status)) {
142                 return false;
143         }
144
145         d_printf("%s:%s:%d:%d:%s:%s:%s\n",
146                  pwd->pw_name,
147                  pwd->pw_passwd,
148                  pwd->pw_uid,
149                  pwd->pw_gid,
150                  pwd->pw_gecos,
151                  pwd->pw_dir,
152                  pwd->pw_shell);
153
154         return true;
155 }
156
157 /* pull pwent info for a given uid */
158 static bool wbinfo_get_uidinfo(int uid)
159 {
160         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
161         struct passwd *pwd = NULL;
162
163         wbc_status = wbcGetpwuid(uid, &pwd);
164         if (!WBC_ERROR_IS_OK(wbc_status)) {
165                 return false;
166         }
167
168         d_printf("%s:%s:%d:%d:%s:%s:%s\n",
169                  pwd->pw_name,
170                  pwd->pw_passwd,
171                  pwd->pw_uid,
172                  pwd->pw_gid,
173                  pwd->pw_gecos,
174                  pwd->pw_dir,
175                  pwd->pw_shell);
176
177         return true;
178 }
179
180 /* pull grent for a given group */
181 static bool wbinfo_get_groupinfo(const char *group)
182 {
183         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
184         struct group *grp;
185
186         wbc_status = wbcGetgrnam(group, &grp);
187         if (!WBC_ERROR_IS_OK(wbc_status)) {
188                 return false;
189         }
190
191         d_printf("%s:%s:%d\n",
192                  grp->gr_name,
193                  grp->gr_passwd,
194                  grp->gr_gid);
195
196         wbcFreeMemory(grp);
197
198         return true;
199 }
200
201 /* List groups a user is a member of */
202
203 static bool wbinfo_get_usergroups(const char *user)
204 {
205         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
206         uint32_t num_groups;
207         uint32_t i;
208         gid_t *groups = NULL;
209
210         /* Send request */
211
212         wbc_status = wbcGetGroups(user, &num_groups, &groups);
213         if (!WBC_ERROR_IS_OK(wbc_status)) {
214                 return false;
215         }
216
217         for (i = 0; i < num_groups; i++) {
218                 d_printf("%d\n", (int)groups[i]);
219         }
220
221         wbcFreeMemory(groups);
222
223         return true;
224 }
225
226
227 /* List group SIDs a user SID is a member of */
228 static bool wbinfo_get_usersids(const char *user_sid_str)
229 {
230         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
231         uint32_t num_sids;
232         uint32_t i;
233         struct wbcDomainSid user_sid, *sids = NULL;
234
235         /* Send request */
236
237         wbc_status = wbcStringToSid(user_sid_str, &user_sid);
238         if (!WBC_ERROR_IS_OK(wbc_status)) {
239                 return false;
240         }
241
242         wbc_status = wbcLookupUserSids(&user_sid, false, &num_sids, &sids);
243         if (!WBC_ERROR_IS_OK(wbc_status)) {
244                 return false;
245         }
246
247         for (i = 0; i < num_sids; i++) {
248                 char *str = NULL;
249                 wbc_status = wbcSidToString(&sids[i], &str);
250                 if (!WBC_ERROR_IS_OK(wbc_status)) {
251                         wbcFreeMemory(sids);
252                         return false;
253                 }
254                 d_printf("%s\n", str);
255                 wbcFreeMemory(str);
256         }
257
258         wbcFreeMemory(sids);
259
260         return true;
261 }
262
263 static bool wbinfo_get_userdomgroups(const char *user_sid_str)
264 {
265         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
266         uint32_t num_sids;
267         uint32_t i;
268         struct wbcDomainSid user_sid, *sids = NULL;
269
270         /* Send request */
271
272         wbc_status = wbcStringToSid(user_sid_str, &user_sid);
273         if (!WBC_ERROR_IS_OK(wbc_status)) {
274                 return false;
275         }
276
277         wbc_status = wbcLookupUserSids(&user_sid, true, &num_sids, &sids);
278         if (!WBC_ERROR_IS_OK(wbc_status)) {
279                 return false;
280         }
281
282         for (i = 0; i < num_sids; i++) {
283                 char *str = NULL;
284                 wbc_status = wbcSidToString(&sids[i], &str);
285                 if (!WBC_ERROR_IS_OK(wbc_status)) {
286                         wbcFreeMemory(sids);
287                         return false;
288                 }
289                 d_printf("%s\n", str);
290                 wbcFreeMemory(str);
291         }
292
293         wbcFreeMemory(sids);
294
295         return true;
296 }
297
298 /* Convert NetBIOS name to IP */
299
300 static bool wbinfo_wins_byname(char *name)
301 {
302         struct winbindd_request request;
303         struct winbindd_response response;
304
305         ZERO_STRUCT(request);
306         ZERO_STRUCT(response);
307
308         /* Send request */
309
310         fstrcpy(request.data.winsreq, name);
311
312         if (winbindd_request_response(WINBINDD_WINS_BYNAME, &request, &response) !=
313             NSS_STATUS_SUCCESS) {
314                 return false;
315         }
316
317         /* Display response */
318
319         d_printf("%s\n", response.data.winsresp);
320
321         return true;
322 }
323
324 /* Convert IP to NetBIOS name */
325
326 static bool wbinfo_wins_byip(char *ip)
327 {
328         struct winbindd_request request;
329         struct winbindd_response response;
330
331         ZERO_STRUCT(request);
332         ZERO_STRUCT(response);
333
334         /* Send request */
335
336         fstrcpy(request.data.winsreq, ip);
337
338         if (winbindd_request_response(WINBINDD_WINS_BYIP, &request, &response) !=
339             NSS_STATUS_SUCCESS) {
340                 return false;
341         }
342
343         /* Display response */
344
345         d_printf("%s\n", response.data.winsresp);
346
347         return true;
348 }
349
350 /* List all/trusted domains */
351
352 static bool wbinfo_list_domains(bool list_all_domains, bool verbose)
353 {
354         struct winbindd_request request;
355         struct winbindd_response response;
356
357         bool print_all = !list_all_domains && verbose;
358
359         ZERO_STRUCT(request);
360         ZERO_STRUCT(response);
361
362         /* Send request */
363
364         request.data.list_all_domains = list_all_domains;
365
366         if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM, &request, &response) !=
367             NSS_STATUS_SUCCESS)
368                 return false;
369
370         /* Display response */
371
372         if (response.extra_data.data) {
373                 const char *extra_data = (char *)response.extra_data.data;
374                 char *name;
375                 char *beg, *end;
376                 TALLOC_CTX *frame = talloc_stackframe();
377
378                 if (print_all) {
379                         d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n", 
380                                  "Domain Name", "DNS Domain", "Trust Type", 
381                                  "Transitive", "In", "Out");
382                 }
383
384                 while(next_token_talloc(frame,&extra_data,&name,"\n")) {
385                         /* Print Domain Name */
386                         if ((beg = strchr(name, '\\')) == NULL)
387                                 goto error;
388                         *beg = 0;
389                         beg++;
390                         if ((end = strchr(beg, '\\')) == NULL)
391                                 goto error;
392                         *end = 0;
393
394                         /* Print short name */
395
396                         d_printf("%-16s", name);
397
398                         if (!print_all) {
399                                 d_printf("\n"); 
400                                 continue;
401                         }
402
403                         /* Print DNS domain */
404
405                         if (beg) {
406                                 d_printf("%-24s", beg);
407                         }
408
409                         /* Skip SID */
410                         beg = ++end;
411                         if ((end = strchr(beg, '\\')) == NULL)
412                                 goto error;
413
414                         /* Print Trust Type */
415                         beg = ++end;
416                         if ((end = strchr(beg, '\\')) == NULL)
417                                 goto error;
418                         *end = 0;
419                         d_printf("%-12s", beg);
420
421                         /* Print Transitive */
422                         beg = ++end;
423                         if ((end = strchr(beg, '\\')) == NULL)
424                                 goto error;
425                         *end = 0;
426                         d_printf("%-12s", beg);
427
428                         /* Print Incoming */
429                         beg = ++end;
430                         if ((end = strchr(beg, '\\')) == NULL)
431                                 goto error;
432                         *end = 0;
433                         d_printf("%-5s", beg);
434
435                         /* Print Outgoing */
436                         beg = ++end;
437                         d_printf("%-5s\n", beg);
438                 }
439                 goto out;
440
441 error:
442                 d_fprintf(stderr, "Got invalid response: %s\n", extra_data);
443                 TALLOC_FREE(frame);
444                 SAFE_FREE(response.extra_data.data);
445                 return false;
446 out:
447                 TALLOC_FREE(frame);
448                 SAFE_FREE(response.extra_data.data);
449         }
450
451         return true;
452 }
453
454 /* List own domain */
455
456 static bool wbinfo_list_own_domain(void)
457 {
458         d_printf("%s\n", get_winbind_domain());
459
460         return true;
461 }
462
463 /* show sequence numbers */
464 static bool wbinfo_show_sequence(const char *domain)
465 {
466         struct winbindd_request  request;
467         struct winbindd_response response;
468
469         ZERO_STRUCT(response);
470         ZERO_STRUCT(request);
471
472         if ( domain )
473                 fstrcpy( request.domain_name, domain );
474
475         /* Send request */
476
477         if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
478             NSS_STATUS_SUCCESS)
479                 return false;
480
481         /* Display response */
482
483         if (domain) {
484                 d_printf("%s : ", domain);
485                 if (response.data.sequence_number == (uint32_t)-1) {
486                         d_printf("DISCONNECTED\n");
487                 } else {
488                         d_printf("%d\n", response.data.sequence_number);
489                 }
490         } else if (response.extra_data.data) {
491                 char *extra_data = (char *)response.extra_data.data;
492                 d_printf("%s", extra_data);
493                 SAFE_FREE(response.extra_data.data);
494         }
495
496         return true;
497 }
498
499 /* Show domain info */
500
501 static bool wbinfo_domain_info(const char *domain)
502 {
503         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
504         struct wbcDomainInfo *dinfo = NULL;
505         char *sid_str = NULL;
506
507         if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')) {
508                 domain = get_winbind_domain();
509         }
510
511         /* Send request */
512
513         wbc_status = wbcDomainInfo(domain, &dinfo);
514         if (!WBC_ERROR_IS_OK(wbc_status)) {
515                 return false;
516         }
517
518         wbc_status = wbcSidToString(&dinfo->sid, &sid_str);
519         if (!WBC_ERROR_IS_OK(wbc_status)) {
520                 wbcFreeMemory(dinfo);
521                 return false;
522         }
523
524         /* Display response */
525
526         d_printf("Name              : %s\n", dinfo->short_name);
527         d_printf("Alt_Name          : %s\n", dinfo->dns_name);
528
529         d_printf("SID               : %s\n", sid_str);
530
531         d_printf("Active Directory  : %s\n",
532                  (dinfo->flags & WBC_DOMINFO_AD) ? "Yes" : "No");
533         d_printf("Native            : %s\n",
534                  (dinfo->flags & WBC_DOMINFO_NATIVE) ? "Yes" : "No");
535
536         d_printf("Primary           : %s\n",
537                  (dinfo->flags & WBC_DOMINFO_PRIMARY) ? "Yes" : "No");
538
539         wbcFreeMemory(sid_str);
540         wbcFreeMemory(dinfo);
541
542         return true;
543 }
544
545 /* Get a foreign DC's name */
546 static bool wbinfo_getdcname(const char *domain_name)
547 {
548         struct winbindd_request request;
549         struct winbindd_response response;
550
551         ZERO_STRUCT(request);
552         ZERO_STRUCT(response);
553
554         fstrcpy(request.domain_name, domain_name);
555
556         /* Send request */
557
558         if (winbindd_request_response(WINBINDD_GETDCNAME, &request, &response) !=
559             NSS_STATUS_SUCCESS) {
560                 d_fprintf(stderr, "Could not get dc name for %s\n", domain_name);
561                 return false;
562         }
563
564         /* Display response */
565
566         d_printf("%s\n", response.data.dc_name);
567
568         return true;
569 }
570
571 /* Find a DC */
572 static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
573 {
574         struct winbindd_request request;
575         struct winbindd_response response;
576
577         ZERO_STRUCT(request);
578         ZERO_STRUCT(response);
579
580         fstrcpy(request.domain_name, domain_name);
581         request.flags = flags;
582
583         request.flags |= DS_DIRECTORY_SERVICE_REQUIRED;
584
585         /* Send request */
586
587         if (winbindd_request_response(WINBINDD_DSGETDCNAME, &request, &response) !=
588             NSS_STATUS_SUCCESS) {
589                 d_fprintf(stderr, "Could not find dc for %s\n", domain_name);
590                 return false;
591         }
592
593         /* Display response */
594
595         d_printf("%s\n", response.data.dc_name);
596
597         return true;
598 }
599
600 /* Check trust account password */
601
602 static bool wbinfo_check_secret(void)
603 {
604         struct winbindd_response response;
605         NSS_STATUS result;
606
607         ZERO_STRUCT(response);
608
609         result = winbindd_request_response(WINBINDD_CHECK_MACHACC, NULL, &response);
610
611         d_printf("checking the trust secret via RPC calls %s\n",
612                  (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
613
614         if (result != NSS_STATUS_SUCCESS)
615                 d_fprintf(stderr, "error code was %s (0x%x)\n",
616                          response.data.auth.nt_status_string,
617                          response.data.auth.nt_status);
618
619         return result == NSS_STATUS_SUCCESS;    
620 }
621
622 /* Convert uid to sid */
623
624 static bool wbinfo_uid_to_sid(uid_t uid)
625 {
626         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
627         struct wbcDomainSid sid;
628         char *sid_str = NULL;
629
630         /* Send request */
631
632         wbc_status = wbcUidToSid(uid, &sid);
633         if (!WBC_ERROR_IS_OK(wbc_status)) {
634                 return false;
635         }
636
637         wbc_status = wbcSidToString(&sid, &sid_str);
638         if (!WBC_ERROR_IS_OK(wbc_status)) {
639                 return false;
640         }
641
642         /* Display response */
643
644         d_printf("%s\n", sid_str);
645
646         wbcFreeMemory(sid_str);
647
648         return true;
649 }
650
651 /* Convert gid to sid */
652
653 static bool wbinfo_gid_to_sid(gid_t gid)
654 {
655         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
656         struct wbcDomainSid sid;
657         char *sid_str = NULL;
658
659         /* Send request */
660
661         wbc_status = wbcGidToSid(gid, &sid);
662         if (!WBC_ERROR_IS_OK(wbc_status)) {
663                 return false;
664         }
665
666         wbc_status = wbcSidToString(&sid, &sid_str);
667         if (!WBC_ERROR_IS_OK(wbc_status)) {
668                 return false;
669         }
670
671         /* Display response */
672
673         d_printf("%s\n", sid_str);
674
675         wbcFreeMemory(sid_str);
676
677         return true;
678 }
679
680 /* Convert sid to uid */
681
682 static bool wbinfo_sid_to_uid(const char *sid_str)
683 {
684         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
685         struct wbcDomainSid sid;
686         uid_t uid;
687
688         /* Send request */
689
690         wbc_status = wbcStringToSid(sid_str, &sid);
691         if (!WBC_ERROR_IS_OK(wbc_status)) {
692                 return false;
693         }
694
695         wbc_status = wbcSidToUid(&sid, &uid);
696         if (!WBC_ERROR_IS_OK(wbc_status)) {
697                 return false;
698         }
699
700         /* Display response */
701
702         d_printf("%d\n", (int)uid);
703
704         return true;
705 }
706
707 static bool wbinfo_sid_to_gid(const char *sid_str)
708 {
709         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
710         struct wbcDomainSid sid;
711         gid_t gid;
712
713         /* Send request */
714
715         wbc_status = wbcStringToSid(sid_str, &sid);
716         if (!WBC_ERROR_IS_OK(wbc_status)) {
717                 return false;
718         }
719
720         wbc_status = wbcSidToGid(&sid, &gid);
721         if (!WBC_ERROR_IS_OK(wbc_status)) {
722                 return false;
723         }
724
725         /* Display response */
726
727         d_printf("%d\n", (int)gid);
728
729         return true;
730 }
731
732 static bool wbinfo_allocate_uid(void)
733 {
734         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
735         uid_t uid;
736
737         /* Send request */
738
739         wbc_status = wbcAllocateUid(&uid);
740         if (!WBC_ERROR_IS_OK(wbc_status)) {
741                 return false;
742         }
743
744         /* Display response */
745
746         d_printf("New uid: %d\n", uid);
747
748         return true;
749 }
750
751 static bool wbinfo_allocate_gid(void)
752 {
753         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
754         gid_t gid;
755
756         /* Send request */
757
758         wbc_status = wbcAllocateGid(&gid);
759         if (!WBC_ERROR_IS_OK(wbc_status)) {
760                 return false;
761         }
762
763         /* Display response */
764
765         d_printf("New gid: %d\n", gid);
766
767         return true;
768 }
769
770 /* Convert sid to string */
771
772 static bool wbinfo_lookupsid(const char *sid_str)
773 {
774         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
775         struct wbcDomainSid sid;
776         char *domain;
777         char *name;
778         enum wbcSidType type;
779
780         /* Send off request */
781
782         wbc_status = wbcStringToSid(sid_str, &sid);
783         if (!WBC_ERROR_IS_OK(wbc_status)) {
784                 return false;
785         }
786
787         wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
788         if (!WBC_ERROR_IS_OK(wbc_status)) {
789                 return false;
790         }
791
792         /* Display response */
793
794         d_printf("%s%c%s %d\n",
795                  domain, winbind_separator(), name, type);
796
797         return true;
798 }
799
800 /* Lookup a list of RIDs */
801
802 static bool wbinfo_lookuprids(const char *domain, const char *arg)
803 {
804         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
805         struct wbcDomainInfo *dinfo = NULL;
806         char *domain_name = NULL;
807         const char **names = NULL;
808         enum wbcSidType *types = NULL;
809         size_t i;
810         int num_rids;
811         uint32 *rids = NULL;
812         const char *p;
813         char *ridstr;
814         TALLOC_CTX *mem_ctx;
815         bool ret = false;
816
817         if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')) {
818                 domain = get_winbind_domain();
819         }
820
821         /* Send request */
822
823         wbc_status = wbcDomainInfo(domain, &dinfo);
824         if (!WBC_ERROR_IS_OK(wbc_status)) {
825                 d_printf("wbcDomainInfo(%s) failed: %s\n", domain,
826                          wbcErrorString(wbc_status));
827                 goto done;
828         }
829
830         mem_ctx = talloc_new(NULL);
831         if (mem_ctx == NULL) {
832                 d_printf("talloc_new failed\n");
833                 goto done;
834         }
835
836         num_rids = 0;
837         rids = NULL;
838         p = arg;
839
840         while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
841                 uint32 rid = strtoul(ridstr, NULL, 10);
842                 ADD_TO_ARRAY(mem_ctx, uint32, rid, &rids, &num_rids);
843         }
844
845         if (rids == NULL) {
846                 d_printf("no rids\n");
847                 goto done;
848         }
849
850         wbc_status = wbcLookupRids(&dinfo->sid, num_rids, rids,
851                                    (const char **)&domain_name, &names, &types);
852         if (!WBC_ERROR_IS_OK(wbc_status)) {
853                 d_printf("winbind_lookup_rids failed: %s\n",
854                          wbcErrorString(wbc_status));
855                 goto done;
856         }
857
858         d_printf("Domain: %s\n", domain_name);
859
860         for (i=0; i<num_rids; i++) {
861                 d_printf("%8d: %s (%s)\n", rids[i], names[i],
862                          sid_type_lookup(types[i]));
863         }
864
865         ret = true;
866 done:
867         if (dinfo) {
868                 wbcFreeMemory(dinfo);
869         }
870         if (domain_name) {
871                 wbcFreeMemory(domain_name);
872         }
873         if (names) {
874                 wbcFreeMemory(names);
875         }
876         if (types) {
877                 wbcFreeMemory(types);
878         }
879         TALLOC_FREE(mem_ctx);
880         return ret;
881 }
882
883 /* Convert string to sid */
884
885 static bool wbinfo_lookupname(const char *full_name)
886 {
887         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
888         struct wbcDomainSid sid;
889         char *sid_str;
890         enum wbcSidType type;
891         fstring domain_name;
892         fstring account_name;
893
894         /* Send off request */
895
896         parse_wbinfo_domain_user(full_name, domain_name,
897                                  account_name);
898
899         wbc_status = wbcLookupName(domain_name, account_name,
900                                    &sid, &type);
901         if (!WBC_ERROR_IS_OK(wbc_status)) {
902                 return false;
903         }
904
905         wbc_status = wbcSidToString(&sid, &sid_str);
906         if (!WBC_ERROR_IS_OK(wbc_status)) {
907                 return false;
908         }
909
910         /* Display response */
911
912         d_printf("%s %s (%d)\n", sid_str, sid_type_lookup(type), type);
913
914         wbcFreeMemory(sid_str);
915
916         return true;
917 }
918
919 /* Authenticate a user with a plaintext password */
920
921 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
922 {
923         struct winbindd_request request;
924         struct winbindd_response response;
925         NSS_STATUS result;
926         char *p;
927
928         /* Send off request */
929
930         ZERO_STRUCT(request);
931         ZERO_STRUCT(response);
932
933         p = strchr(username, '%');
934
935         if (p) {
936                 *p = 0;
937                 fstrcpy(request.data.auth.user, username);
938                 fstrcpy(request.data.auth.pass, p + 1);
939                 *p = '%';
940         } else
941                 fstrcpy(request.data.auth.user, username);
942
943         request.flags = flags;
944
945         fstrcpy(request.data.auth.krb5_cc_type, cctype);
946
947         request.data.auth.uid = geteuid();
948
949         result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
950
951         /* Display response */
952
953         d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n", 
954                 username, (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", cctype);
955
956         if (response.data.auth.nt_status)
957                 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n", 
958                          response.data.auth.nt_status_string, 
959                          response.data.auth.nt_status,
960                          response.data.auth.error_string);
961
962         if (result == NSS_STATUS_SUCCESS) {
963
964                 if (request.flags & WBFLAG_PAM_INFO3_TEXT) {
965                         if (response.data.auth.info3.user_flgs & NETLOGON_CACHED_ACCOUNT) {
966                                 d_printf("user_flgs: NETLOGON_CACHED_ACCOUNT\n");
967                         }
968                 }
969
970                 if (response.data.auth.krb5ccname[0] != '\0') {
971                         d_printf("credentials were put in: %s\n", response.data.auth.krb5ccname);
972                 } else {
973                         d_printf("no credentials cached\n");
974                 }
975         }
976
977         return result == NSS_STATUS_SUCCESS;
978 }
979
980 /* Authenticate a user with a plaintext password */
981
982 static bool wbinfo_auth(char *username)
983 {
984         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
985         char *s = NULL;
986         char *p = NULL;
987         const char *password = NULL;
988         char *name = NULL;
989
990         if ((s = SMB_STRDUP(username)) == NULL) {
991                 return false;
992         }
993
994         if ((p = strchr(s, '%')) != NULL) {
995                 *p = 0;
996                 p++;
997                 password = p;
998         } else {
999                 password = "";
1000         }
1001
1002         name = s;
1003
1004         wbc_status = wbcAuthenticateUser(name, password);
1005
1006         d_printf("plaintext password authentication %s\n",
1007                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1008
1009 #if 0
1010         if (response.data.auth.nt_status)
1011                 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n", 
1012                          response.data.auth.nt_status_string,
1013                          response.data.auth.nt_status,
1014                          response.data.auth.error_string);
1015 #endif
1016
1017         SAFE_FREE(s);
1018
1019         return WBC_ERROR_IS_OK(wbc_status);
1020 }
1021
1022 /* Authenticate a user with a challenge/response */
1023
1024 static bool wbinfo_auth_crap(char *username)
1025 {
1026         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1027         struct wbcAuthUserParams params;
1028         struct wbcAuthUserInfo *info = NULL;
1029         struct wbcAuthErrorInfo *err = NULL;
1030         DATA_BLOB lm = data_blob_null;
1031         DATA_BLOB nt = data_blob_null;
1032         fstring name_user;
1033         fstring name_domain;
1034         fstring pass;
1035         char *p;
1036
1037         p = strchr(username, '%');
1038
1039         if (p) {
1040                 *p = 0;
1041                 fstrcpy(pass, p + 1);
1042         }
1043                 
1044         parse_wbinfo_domain_user(username, name_domain, name_user);
1045
1046         params.account_name     = name_user;
1047         params.domain_name      = name_domain;
1048         params.workstation_name = NULL;
1049
1050         params.flags            = 0;
1051         params.parameter_control= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
1052                                   WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
1053
1054         params.level            = WBC_AUTH_USER_LEVEL_RESPONSE;
1055
1056         generate_random_buffer(params.password.response.challenge, 8);
1057
1058         if (lp_client_ntlmv2_auth()) {
1059                 DATA_BLOB server_chal;
1060                 DATA_BLOB names_blob;
1061
1062                 server_chal = data_blob(params.password.response.challenge, 8);
1063
1064                 /* Pretend this is a login to 'us', for blob purposes */
1065                 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
1066
1067                 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
1068                                       &names_blob,
1069                                       &lm, &nt, NULL)) {
1070                         data_blob_free(&names_blob);
1071                         data_blob_free(&server_chal);
1072                         return false;
1073                 }
1074                 data_blob_free(&names_blob);
1075                 data_blob_free(&server_chal);
1076
1077         } else {
1078                 if (lp_client_lanman_auth()) {
1079                         bool ok;
1080                         lm = data_blob(NULL, 24);
1081                         ok = SMBencrypt(pass, params.password.response.challenge,
1082                                         lm.data);
1083                         if (!ok) {
1084                                 data_blob_free(&lm);
1085                         }
1086                 }
1087                 nt = data_blob(NULL, 24);
1088                 SMBNTencrypt(pass, params.password.response.challenge,
1089                              nt.data);
1090         }
1091
1092         params.password.response.nt_length      = nt.length;
1093         params.password.response.nt_data        = nt.data;
1094         params.password.response.lm_length      = lm.length;
1095         params.password.response.lm_data        = lm.data;
1096
1097         wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
1098
1099         /* Display response */
1100
1101         d_printf("challenge/response password authentication %s\n",
1102                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1103
1104         if (wbc_status == WBC_ERR_AUTH_ERROR) {
1105                 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n", 
1106                          err->nt_string,
1107                          err->nt_status,
1108                          err->display_string);
1109                 wbcFreeMemory(err);
1110         } else if (WBC_ERROR_IS_OK(wbc_status)) {
1111                 wbcFreeMemory(info);
1112         }
1113
1114         data_blob_free(&nt);
1115         data_blob_free(&lm);
1116
1117         return WBC_ERROR_IS_OK(wbc_status);
1118 }
1119
1120 /* Authenticate a user with a plaintext password and set a token */
1121
1122 static bool wbinfo_klog(char *username)
1123 {
1124         struct winbindd_request request;
1125         struct winbindd_response response;
1126         NSS_STATUS result;
1127         char *p;
1128
1129         /* Send off request */
1130
1131         ZERO_STRUCT(request);
1132         ZERO_STRUCT(response);
1133
1134         p = strchr(username, '%');
1135
1136         if (p) {
1137                 *p = 0;
1138                 fstrcpy(request.data.auth.user, username);
1139                 fstrcpy(request.data.auth.pass, p + 1);
1140                 *p = '%';
1141         } else {
1142                 fstrcpy(request.data.auth.user, username);
1143                 fstrcpy(request.data.auth.pass, getpass("Password: "));
1144         }
1145
1146         request.flags |= WBFLAG_PAM_AFS_TOKEN;
1147
1148         result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
1149
1150         /* Display response */
1151
1152         d_printf("plaintext password authentication %s\n",
1153                  (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1154
1155         if (response.data.auth.nt_status)
1156                 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n", 
1157                          response.data.auth.nt_status_string,
1158                          response.data.auth.nt_status,
1159                          response.data.auth.error_string);
1160
1161         if (result != NSS_STATUS_SUCCESS)
1162                 return false;
1163
1164         if (response.extra_data.data == NULL) {
1165                 d_fprintf(stderr, "Did not get token data\n");
1166                 return false;
1167         }
1168
1169         if (!afs_settoken_str((char *)response.extra_data.data)) {
1170                 d_fprintf(stderr, "Could not set token\n");
1171                 return false;
1172         }
1173
1174         d_printf("Successfully created AFS token\n");
1175         return true;
1176 }
1177
1178 /* Print domain users */
1179
1180 static bool print_domain_users(const char *domain)
1181 {
1182         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1183         uint32_t i;
1184         uint32_t num_users = 0;
1185         const char **users = NULL;
1186
1187         /* Send request to winbind daemon */
1188
1189         /* '.' is the special sign for our own domain */
1190         if (domain && strcmp(domain, ".") == 0) {
1191                 domain = get_winbind_domain();
1192         }
1193
1194         wbc_status = wbcListUsers(domain, &num_users, &users);
1195         if (!WBC_ERROR_IS_OK(wbc_status)) {
1196                 return false;
1197         }
1198
1199         for (i=0; i < num_users; i++) {
1200                 d_printf("%s\n", users[i]);
1201         }
1202
1203         wbcFreeMemory(users);
1204
1205         return true;
1206 }
1207
1208 /* Print domain groups */
1209
1210 static bool print_domain_groups(const char *domain)
1211 {
1212         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1213         uint32_t i;
1214         uint32_t num_groups = 0;
1215         const char **groups = NULL;
1216
1217         /* Send request to winbind daemon */
1218
1219         /* '.' is the special sign for our own domain */
1220         if (domain && strcmp(domain, ".") == 0) {
1221                 domain = get_winbind_domain();
1222         }
1223
1224         wbc_status = wbcListGroups(domain, &num_groups, &groups);
1225         if (!WBC_ERROR_IS_OK(wbc_status)) {
1226                 return false;
1227         }
1228
1229         for (i=0; i < num_groups; i++) {
1230                 d_printf("%s\n", groups[i]);
1231         }
1232
1233         wbcFreeMemory(groups);
1234
1235         return true;
1236 }
1237
1238 /* Set the authorised user for winbindd access in secrets.tdb */
1239
1240 static bool wbinfo_set_auth_user(char *username)
1241 {
1242         const char *password;
1243         char *p;
1244         fstring user, domain;
1245
1246         /* Separate into user and password */
1247
1248         parse_wbinfo_domain_user(username, domain, user);
1249
1250         p = strchr(user, '%');
1251
1252         if (p != NULL) {
1253                 *p = 0;
1254                 password = p+1;
1255         } else {
1256                 char *thepass = getpass("Password: ");
1257                 if (thepass) {
1258                         password = thepass;
1259                 } else
1260                         password = "";
1261         }
1262
1263         /* Store or remove DOMAIN\username%password in secrets.tdb */
1264
1265         secrets_init();
1266
1267         if (user[0]) {
1268
1269                 if (!secrets_store(SECRETS_AUTH_USER, user,
1270                                    strlen(user) + 1)) {
1271                         d_fprintf(stderr, "error storing username\n");
1272                         return false;
1273                 }
1274
1275                 /* We always have a domain name added by the
1276                    parse_wbinfo_domain_user() function. */
1277
1278                 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
1279                                    strlen(domain) + 1)) {
1280                         d_fprintf(stderr, "error storing domain name\n");
1281                         return false;
1282                 }
1283
1284         } else {
1285                 secrets_delete(SECRETS_AUTH_USER);
1286                 secrets_delete(SECRETS_AUTH_DOMAIN);
1287         }
1288
1289         if (password[0]) {
1290
1291                 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
1292                                    strlen(password) + 1)) {
1293                         d_fprintf(stderr, "error storing password\n");
1294                         return false;
1295                 }
1296
1297         } else
1298                 secrets_delete(SECRETS_AUTH_PASSWORD);
1299
1300         return true;
1301 }
1302
1303 static void wbinfo_get_auth_user(void)
1304 {
1305         char *user, *domain, *password;
1306
1307         /* Lift data from secrets file */
1308
1309         secrets_fetch_ipc_userpass(&user, &domain, &password);
1310
1311         if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
1312
1313                 SAFE_FREE(user);
1314                 SAFE_FREE(domain);
1315                 SAFE_FREE(password);
1316                 d_printf("No authorised user configured\n");
1317                 return;
1318         }
1319
1320         /* Pretty print authorised user info */
1321
1322         d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
1323                  user, password ? "%" : "", password ? password : "");
1324
1325         SAFE_FREE(user);
1326         SAFE_FREE(domain);
1327         SAFE_FREE(password);
1328 }
1329
1330 static bool wbinfo_ping(void)
1331 {
1332         wbcErr wbc_status;
1333
1334         wbc_status = wbcPing();
1335
1336         /* Display response */
1337
1338         d_printf("Ping to winbindd %s\n",
1339                  WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1340
1341         return WBC_ERROR_IS_OK(wbc_status);
1342 }
1343
1344 /* Main program */
1345
1346 enum {
1347         OPT_SET_AUTH_USER = 1000,
1348         OPT_GET_AUTH_USER,
1349         OPT_DOMAIN_NAME,
1350         OPT_SEQUENCE,
1351         OPT_GETDCNAME,
1352         OPT_DSGETDCNAME,
1353         OPT_USERDOMGROUPS,
1354         OPT_USERSIDS,
1355         OPT_ALLOCATE_UID,
1356         OPT_ALLOCATE_GID,
1357         OPT_SEPARATOR,
1358         OPT_LIST_ALL_DOMAINS,
1359         OPT_LIST_OWN_DOMAIN,
1360         OPT_UID_INFO,
1361         OPT_GROUP_INFO,
1362         OPT_VERBOSE
1363 };
1364
1365 int main(int argc, char **argv, char **envp)
1366 {
1367         int opt;
1368         TALLOC_CTX *frame = talloc_stackframe();
1369         poptContext pc;
1370         static char *string_arg;
1371         static char *opt_domain_name;
1372         static int int_arg;
1373         int result = 1;
1374         bool verbose = false;
1375
1376         struct poptOption long_options[] = {
1377                 POPT_AUTOHELP
1378
1379                 /* longName, shortName, argInfo, argPtr, value, descrip,
1380                    argDesc */
1381
1382                 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1383                 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1384                 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1385                 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1386                 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1387                 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1388                 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1389                 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1390                 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1391                 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1392                 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1393                 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1394                   "Get a new UID out of idmap" },
1395                 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1396                   "Get a new GID out of idmap" },
1397                 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1398                 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1399                 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1400                 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1401                 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1402                 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1403                 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1404                 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1405                 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1406                 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1407                 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1408                   OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1409                 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1410                 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1411                 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1412                 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1413                   "Get a DC name for a foreign domain", "domainname" },
1414                 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
1415                 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1416                 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1417                 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1418 #ifdef WITH_FAKE_KASERVER
1419                 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1420 #endif
1421 #ifdef HAVE_KRB5
1422                 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1423                         /* destroys wbinfo --help output */
1424                         /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1425 #endif
1426                 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1427                 { "verbose", 0, POPT_ARG_NONE, 0, OPT_VERBOSE, "Print additional information per command", NULL },
1428                 POPT_COMMON_CONFIGFILE
1429                 POPT_COMMON_VERSION
1430                 POPT_TABLEEND
1431         };
1432
1433         /* Samba client initialisation */
1434         load_case_tables();
1435
1436
1437         /* Parse options */
1438
1439         pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1440
1441         /* Parse command line options */
1442
1443         if (argc == 1) {
1444                 poptPrintHelp(pc, stderr, 0);
1445                 return 1;
1446         }
1447
1448         while((opt = poptGetNextOpt(pc)) != -1) {
1449                 /* get the generic configuration parameters like --domain */
1450                 switch (opt) {
1451                 case OPT_VERBOSE:
1452                         verbose = True;
1453                         break;
1454                 }
1455         }
1456
1457         poptFreeContext(pc);
1458
1459         if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, true)) {
1460                 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1461                         get_dyn_CONFIGFILE(), strerror(errno));
1462                 exit(1);
1463         }
1464
1465         if (!init_names())
1466                 return 1;
1467
1468         load_interfaces();
1469
1470         pc = poptGetContext(NULL, argc, (const char **)argv, long_options, 
1471                             POPT_CONTEXT_KEEP_FIRST);
1472
1473         while((opt = poptGetNextOpt(pc)) != -1) {
1474                 switch (opt) {
1475                 case 'u':
1476                         if (!print_domain_users(opt_domain_name)) {
1477                                 d_fprintf(stderr, "Error looking up domain users\n");
1478                                 goto done;
1479                         }
1480                         break;
1481                 case 'g':
1482                         if (!print_domain_groups(opt_domain_name)) {
1483                                 d_fprintf(stderr, "Error looking up domain groups\n");
1484                                 goto done;
1485                         }
1486                         break;
1487                 case 's':
1488                         if (!wbinfo_lookupsid(string_arg)) {
1489                                 d_fprintf(stderr, "Could not lookup sid %s\n", string_arg);
1490                                 goto done;
1491                         }
1492                         break;
1493                 case 'R':
1494                         if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
1495                                 d_fprintf(stderr, "Could not lookup RIDs %s\n", string_arg);
1496                                 goto done;
1497                         }
1498                         break;
1499                 case 'n':
1500                         if (!wbinfo_lookupname(string_arg)) {
1501                                 d_fprintf(stderr, "Could not lookup name %s\n", string_arg);
1502                                 goto done;
1503                         }
1504                         break;
1505                 case 'N':
1506                         if (!wbinfo_wins_byname(string_arg)) {
1507                                 d_fprintf(stderr, "Could not lookup WINS by name %s\n", string_arg);
1508                                 goto done;
1509                         }
1510                         break;
1511                 case 'I':
1512                         if (!wbinfo_wins_byip(string_arg)) {
1513                                 d_fprintf(stderr, "Could not lookup WINS by IP %s\n", string_arg);
1514                                 goto done;
1515                         }
1516                         break;
1517                 case 'U':
1518                         if (!wbinfo_uid_to_sid(int_arg)) {
1519                                 d_fprintf(stderr, "Could not convert uid %d to sid\n", int_arg);
1520                                 goto done;
1521                         }
1522                         break;
1523                 case 'G':
1524                         if (!wbinfo_gid_to_sid(int_arg)) {
1525                                 d_fprintf(stderr, "Could not convert gid %d to sid\n",
1526                                        int_arg);
1527                                 goto done;
1528                         }
1529                         break;
1530                 case 'S':
1531                         if (!wbinfo_sid_to_uid(string_arg)) {
1532                                 d_fprintf(stderr, "Could not convert sid %s to uid\n",
1533                                        string_arg);
1534                                 goto done;
1535                         }
1536                         break;
1537                 case 'Y':
1538                         if (!wbinfo_sid_to_gid(string_arg)) {
1539                                 d_fprintf(stderr, "Could not convert sid %s to gid\n",
1540                                        string_arg);
1541                                 goto done;
1542                         }
1543                         break;
1544                 case OPT_ALLOCATE_UID:
1545                         if (!wbinfo_allocate_uid()) {
1546                                 d_fprintf(stderr, "Could not allocate a uid\n");
1547                                 goto done;
1548                         }
1549                         break;
1550                 case OPT_ALLOCATE_GID:
1551                         if (!wbinfo_allocate_gid()) {
1552                                 d_fprintf(stderr, "Could not allocate a gid\n");
1553                                 goto done;
1554                         }
1555                         break;
1556                 case 't':
1557                         if (!wbinfo_check_secret()) {
1558                                 d_fprintf(stderr, "Could not check secret\n");
1559                                 goto done;
1560                         }
1561                         break;
1562                 case 'm':
1563                         if (!wbinfo_list_domains(false, verbose)) {
1564                                 d_fprintf(stderr, "Could not list trusted domains\n");
1565                                 goto done;
1566                         }
1567                         break;
1568                 case OPT_SEQUENCE:
1569                         if (!wbinfo_show_sequence(opt_domain_name)) {
1570                                 d_fprintf(stderr, "Could not show sequence numbers\n");
1571                                 goto done;
1572                         }
1573                         break;
1574                 case 'D':
1575                         if (!wbinfo_domain_info(string_arg)) {
1576                                 d_fprintf(stderr, "Could not get domain info\n");
1577                                 goto done;
1578                         }
1579                         break;
1580                 case 'i':
1581                         if (!wbinfo_get_userinfo(string_arg)) {
1582                                 d_fprintf(stderr, "Could not get info for user %s\n",
1583                                                   string_arg);
1584                                 goto done;
1585                         }
1586                         break;
1587                 case OPT_UID_INFO:
1588                         if ( !wbinfo_get_uidinfo(int_arg)) {
1589                                 d_fprintf(stderr, "Could not get info for uid "
1590                                                 "%d\n", int_arg);
1591                                 goto done;
1592                         }
1593                         break;
1594                 case OPT_GROUP_INFO:
1595                         if ( !wbinfo_get_groupinfo(string_arg)) {
1596                                 d_fprintf(stderr, "Could not get info for "
1597                                           "group %s\n", string_arg);
1598                                 goto done;
1599                         }
1600                         break;
1601                 case 'r':
1602                         if (!wbinfo_get_usergroups(string_arg)) {
1603                                 d_fprintf(stderr, "Could not get groups for user %s\n", 
1604                                        string_arg);
1605                                 goto done;
1606                         }
1607                         break;
1608                 case OPT_USERSIDS:
1609                         if (!wbinfo_get_usersids(string_arg)) {
1610                                 d_fprintf(stderr, "Could not get group SIDs for user SID %s\n", 
1611                                        string_arg);
1612                                 goto done;
1613                         }
1614                         break;
1615                 case OPT_USERDOMGROUPS:
1616                         if (!wbinfo_get_userdomgroups(string_arg)) {
1617                                 d_fprintf(stderr, "Could not get user's domain groups "
1618                                          "for user SID %s\n", string_arg);
1619                                 goto done;
1620                         }
1621                         break;
1622                 case 'a': {
1623                                 bool got_error = false;
1624
1625                                 if (!wbinfo_auth(string_arg)) {
1626                                         d_fprintf(stderr, "Could not authenticate user %s with "
1627                                                 "plaintext password\n", string_arg);
1628                                         got_error = true;
1629                                 }
1630
1631                                 if (!wbinfo_auth_crap(string_arg)) {
1632                                         d_fprintf(stderr, "Could not authenticate user %s with "
1633                                                 "challenge/response\n", string_arg);
1634                                         got_error = true;
1635                                 }
1636
1637                                 if (got_error)
1638                                         goto done;
1639                                 break;
1640                         }
1641                 case 'K': {
1642                                 uint32 flags =  WBFLAG_PAM_KRB5 |
1643                                                 WBFLAG_PAM_CACHED_LOGIN |
1644                                                 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
1645                                                 WBFLAG_PAM_INFO3_TEXT;
1646
1647                                 if (!wbinfo_auth_krb5(string_arg, "FILE", flags)) {
1648                                         d_fprintf(stderr, "Could not authenticate user [%s] with "
1649                                                 "Kerberos (ccache: %s)\n", string_arg, "FILE");
1650                                         goto done;
1651                                 }
1652                                 break;
1653                         }
1654                 case 'k':
1655                         if (!wbinfo_klog(string_arg)) {
1656                                 d_fprintf(stderr, "Could not klog user\n");
1657                                 goto done;
1658                         }
1659                         break;
1660                 case 'p':
1661                         if (!wbinfo_ping()) {
1662                                 d_fprintf(stderr, "could not ping winbindd!\n");
1663                                 goto done;
1664                         }
1665                         break;
1666                 case OPT_SET_AUTH_USER:
1667                         if (!wbinfo_set_auth_user(string_arg)) {
1668                                 goto done;
1669                         }
1670                         break;
1671                 case OPT_GET_AUTH_USER:
1672                         wbinfo_get_auth_user();
1673                         break;
1674                 case OPT_GETDCNAME:
1675                         if (!wbinfo_getdcname(string_arg)) {
1676                                 goto done;
1677                         }
1678                         break;
1679                 case OPT_DSGETDCNAME:
1680                         if (!wbinfo_dsgetdcname(string_arg, 0)) {
1681                                 goto done;
1682                         }
1683                         break;
1684                 case OPT_SEPARATOR: {
1685                         const char sep = winbind_separator_int(true);
1686                         if ( !sep ) {
1687                                 goto done;
1688                         }
1689                         d_printf("%c\n", sep);
1690                         break;
1691                 }
1692                 case OPT_LIST_ALL_DOMAINS:
1693                         if (!wbinfo_list_domains(true, verbose)) {
1694                                 goto done;
1695                         }
1696                         break;
1697                 case OPT_LIST_OWN_DOMAIN:
1698                         if (!wbinfo_list_own_domain()) {
1699                                 goto done;
1700                         }
1701                         break;
1702                 /* generic configuration options */
1703                 case OPT_DOMAIN_NAME:
1704                         break;
1705                 case OPT_VERBOSE:
1706                         break;
1707                 default:
1708                         d_fprintf(stderr, "Invalid option\n");
1709                         poptPrintHelp(pc, stderr, 0);
1710                         goto done;
1711                 }
1712         }
1713
1714         result = 0;
1715
1716         /* Exit code */
1717
1718  done:
1719         talloc_destroy(frame);
1720
1721         poptFreeContext(pc);
1722         return result;
1723 }