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