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