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