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