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