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