r7415: * big change -- volker's new async winbindd from trunk
[tprouty/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 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(void)
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(WINBINDD_INFO, NULL, &response) !=
47             NSS_STATUS_SUCCESS) {
48                 d_printf("could not obtain winbind separator!\n");
49                 /* HACK: (this module should not call lp_ funtions) */
50                 return *lp_winbind_separator();
51         }
52
53         sep = response.data.info.winbind_separator;
54         got_sep = True;
55
56         if (!sep) {
57                 d_printf("winbind separator was NULL!\n");
58                 /* HACK: (this module should not call lp_ funtions) */
59                 sep = *lp_winbind_separator();
60         }
61         
62         return sep;
63 }
64
65 static const char *get_winbind_domain(void)
66 {
67         struct winbindd_response response;
68         static fstring winbind_domain;
69
70         ZERO_STRUCT(response);
71
72         /* Send off request */
73
74         if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
75             NSS_STATUS_SUCCESS) {
76                 d_printf("could not obtain winbind domain name!\n");
77                 
78                 /* HACK: (this module should not call lp_ funtions) */
79                 return lp_workgroup();
80         }
81
82         fstrcpy(winbind_domain, response.data.domain_name);
83
84         return winbind_domain;
85
86 }
87
88 /* Copy of parse_domain_user from winbindd_util.c.  Parse a string of the
89    form DOMAIN/user into a domain and a user */
90
91 static BOOL parse_wbinfo_domain_user(const char *domuser, fstring domain, 
92                                      fstring user)
93 {
94
95         char *p = strchr(domuser,winbind_separator());
96
97         if (!p) {
98                 fstrcpy(user, domuser);
99                 fstrcpy(domain, get_winbind_domain());
100                 return True;
101         }
102         
103         fstrcpy(user, p+1);
104         fstrcpy(domain, domuser);
105         domain[PTR_DIFF(p, domuser)] = 0;
106         strupper_m(domain);
107
108         return True;
109 }
110
111 /* List groups a user is a member of */
112
113 static BOOL wbinfo_get_usergroups(char *user)
114 {
115         struct winbindd_request request;
116         struct winbindd_response response;
117         NSS_STATUS result;
118         int i;
119         
120         ZERO_STRUCT(response);
121
122         /* Send request */
123
124         fstrcpy(request.data.username, user);
125
126         result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
127
128         if (result != NSS_STATUS_SUCCESS)
129                 return False;
130
131         for (i = 0; i < response.data.num_entries; i++)
132                 d_printf("%d\n", (int)((gid_t *)response.extra_data)[i]);
133
134         SAFE_FREE(response.extra_data);
135
136         return True;
137 }
138
139
140 /* List group SIDs a user SID is a member of */
141 static BOOL wbinfo_get_usersids(char *user_sid)
142 {
143         struct winbindd_request request;
144         struct winbindd_response response;
145         NSS_STATUS result;
146         int i;
147         const char *s;
148
149         ZERO_STRUCT(response);
150
151         /* Send request */
152         fstrcpy(request.data.sid, user_sid);
153
154         result = winbindd_request(WINBINDD_GETUSERSIDS, &request, &response);
155
156         if (result != NSS_STATUS_SUCCESS)
157                 return False;
158
159         s = response.extra_data;
160         for (i = 0; i < response.data.num_entries; i++) {
161                 d_printf("%s\n", s);
162                 s += strlen(s) + 1;
163         }
164
165         SAFE_FREE(response.extra_data);
166
167         return True;
168 }
169
170 static BOOL wbinfo_get_userdomgroups(const char *user_sid)
171 {
172         struct winbindd_request request;
173         struct winbindd_response response;
174         NSS_STATUS result;
175
176         ZERO_STRUCT(response);
177
178         /* Send request */
179         fstrcpy(request.data.sid, user_sid);
180
181         result = winbindd_request(WINBINDD_GETUSERDOMGROUPS, &request,
182                                   &response);
183
184         if (result != NSS_STATUS_SUCCESS)
185                 return False;
186
187         if (response.data.num_entries != 0)
188                 printf("%s", (char *)response.extra_data);
189         
190         SAFE_FREE(response.extra_data);
191
192         return True;
193 }
194
195
196 /* Convert NetBIOS name to IP */
197
198 static BOOL wbinfo_wins_byname(char *name)
199 {
200         struct winbindd_request request;
201         struct winbindd_response response;
202
203         ZERO_STRUCT(request);
204         ZERO_STRUCT(response);
205
206         /* Send request */
207
208         fstrcpy(request.data.winsreq, name);
209
210         if (winbindd_request(WINBINDD_WINS_BYNAME, &request, &response) !=
211             NSS_STATUS_SUCCESS) {
212                 return False;
213         }
214
215         /* Display response */
216
217         printf("%s\n", response.data.winsresp);
218
219         return True;
220 }
221
222 /* Convert IP to NetBIOS name */
223
224 static BOOL wbinfo_wins_byip(char *ip)
225 {
226         struct winbindd_request request;
227         struct winbindd_response response;
228
229         ZERO_STRUCT(request);
230         ZERO_STRUCT(response);
231
232         /* Send request */
233
234         fstrcpy(request.data.winsreq, ip);
235
236         if (winbindd_request(WINBINDD_WINS_BYIP, &request, &response) !=
237             NSS_STATUS_SUCCESS) {
238                 return False;
239         }
240
241         /* Display response */
242
243         printf("%s\n", response.data.winsresp);
244
245         return True;
246 }
247
248 /* List trusted domains */
249
250 static BOOL wbinfo_list_domains(void)
251 {
252         struct winbindd_response response;
253
254         ZERO_STRUCT(response);
255
256         /* Send request */
257
258         if (winbindd_request(WINBINDD_LIST_TRUSTDOM, NULL, &response) !=
259             NSS_STATUS_SUCCESS)
260                 return False;
261
262         /* Display response */
263
264         if (response.extra_data) {
265                 const char *extra_data = (char *)response.extra_data;
266                 fstring name;
267                 char *p;
268
269                 while(next_token(&extra_data, name, "\n", sizeof(fstring))) {
270                         p = strchr(name, '\\');
271                         if (p == 0) {
272                                 d_printf("Got invalid response: %s\n",
273                                          extra_data);
274                                 return False;
275                         }
276                         *p = 0;
277                         d_printf("%s\n", name);
278                 }
279
280                 SAFE_FREE(response.extra_data);
281         }
282
283         return True;
284 }
285
286
287 /* show sequence numbers */
288 static BOOL wbinfo_show_sequence(const char *domain)
289 {
290         struct winbindd_request  request;
291         struct winbindd_response response;
292
293         ZERO_STRUCT(response);
294         ZERO_STRUCT(request);
295
296         if ( domain )
297                 fstrcpy( request.domain_name, domain );
298
299         /* Send request */
300
301         if (winbindd_request(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
302             NSS_STATUS_SUCCESS)
303                 return False;
304
305         /* Display response */
306
307         if (response.extra_data) {
308                 char *extra_data = (char *)response.extra_data;
309                 d_printf("%s", extra_data);
310                 SAFE_FREE(response.extra_data);
311         }
312
313         return True;
314 }
315
316 /* Show domain info */
317
318 static BOOL wbinfo_domain_info(const char *domain_name)
319 {
320         struct winbindd_request request;
321         struct winbindd_response response;
322
323         ZERO_STRUCT(request);
324         ZERO_STRUCT(response);
325
326         fstrcpy(request.domain_name, domain_name);
327
328         /* Send request */
329
330         if (winbindd_request(WINBINDD_DOMAIN_INFO, &request, &response) !=
331             NSS_STATUS_SUCCESS)
332                 return False;
333
334         /* Display response */
335
336         d_printf("Name              : %s\n", response.data.domain_info.name);
337         d_printf("Alt_Name          : %s\n", response.data.domain_info.alt_name);
338
339         d_printf("SID               : %s\n", response.data.domain_info.sid);
340
341         d_printf("Active Directory  : %s\n",
342                  response.data.domain_info.active_directory ? "Yes" : "No");
343         d_printf("Native            : %s\n",
344                  response.data.domain_info.native_mode ? "Yes" : "No");
345
346         d_printf("Primary           : %s\n",
347                  response.data.domain_info.primary ? "Yes" : "No");
348
349         d_printf("Sequence          : %d\n", response.data.domain_info.sequence_number);
350
351         return True;
352 }
353
354 /* Get a foreign DC's name */
355 static BOOL wbinfo_getdcname(const char *domain_name)
356 {
357         struct winbindd_request request;
358         struct winbindd_response response;
359
360         ZERO_STRUCT(request);
361         ZERO_STRUCT(response);
362
363         fstrcpy(request.domain_name, domain_name);
364
365         /* Send request */
366
367         if (winbindd_request(WINBINDD_GETDCNAME, &request, &response) !=
368             NSS_STATUS_SUCCESS) {
369                 d_printf("Could not get dc name for %s\n", domain_name);
370                 return False;
371         }
372
373         /* Display response */
374
375         d_printf("%s\n", response.data.dc_name);
376
377         return True;
378 }
379
380 /* Check trust account password */
381
382 static BOOL wbinfo_check_secret(void)
383 {
384         struct winbindd_response response;
385         NSS_STATUS result;
386
387         ZERO_STRUCT(response);
388
389         result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response);
390                 
391         d_printf("checking the trust secret via RPC calls %s\n", 
392                  (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
393
394         if (result != NSS_STATUS_SUCCESS)       
395                 d_printf("error code was %s (0x%x)\n", 
396                          response.data.auth.nt_status_string, 
397                          response.data.auth.nt_status);
398         
399         return result == NSS_STATUS_SUCCESS;    
400 }
401
402 /* Convert uid to sid */
403
404 static BOOL wbinfo_uid_to_sid(uid_t uid)
405 {
406         struct winbindd_request request;
407         struct winbindd_response response;
408
409         ZERO_STRUCT(request);
410         ZERO_STRUCT(response);
411
412         /* Send request */
413
414         request.data.uid = uid;
415
416         if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
417             NSS_STATUS_SUCCESS)
418                 return False;
419
420         /* Display response */
421
422         d_printf("%s\n", response.data.sid.sid);
423
424         return True;
425 }
426
427 /* Convert gid to sid */
428
429 static BOOL wbinfo_gid_to_sid(gid_t gid)
430 {
431         struct winbindd_request request;
432         struct winbindd_response response;
433
434         ZERO_STRUCT(request);
435         ZERO_STRUCT(response);
436
437         /* Send request */
438
439         request.data.gid = gid;
440
441         if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) !=
442             NSS_STATUS_SUCCESS)
443                 return False;
444
445         /* Display response */
446
447         d_printf("%s\n", response.data.sid.sid);
448
449         return True;
450 }
451
452 /* Convert sid to uid */
453
454 static BOOL wbinfo_sid_to_uid(char *sid)
455 {
456         struct winbindd_request request;
457         struct winbindd_response response;
458
459         ZERO_STRUCT(request);
460         ZERO_STRUCT(response);
461
462         /* Send request */
463
464         fstrcpy(request.data.sid, sid);
465
466         if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
467             NSS_STATUS_SUCCESS)
468                 return False;
469
470         /* Display response */
471
472         d_printf("%d\n", (int)response.data.uid);
473
474         return True;
475 }
476
477 static BOOL wbinfo_sid_to_gid(char *sid)
478 {
479         struct winbindd_request request;
480         struct winbindd_response response;
481
482         ZERO_STRUCT(request);
483         ZERO_STRUCT(response);
484
485         /* Send request */
486
487         fstrcpy(request.data.sid, sid);
488
489         if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
490             NSS_STATUS_SUCCESS)
491                 return False;
492
493         /* Display response */
494
495         d_printf("%d\n", (int)response.data.gid);
496
497         return True;
498 }
499
500 static BOOL wbinfo_allocate_rid(void)
501 {
502         uint32 rid;
503
504         if (!winbind_allocate_rid(&rid))
505                 return False;
506
507         d_printf("New rid: %d\n", rid);
508
509         return True;
510 }
511
512 /* Convert sid to string */
513
514 static BOOL wbinfo_lookupsid(char *sid)
515 {
516         struct winbindd_request request;
517         struct winbindd_response response;
518
519         ZERO_STRUCT(request);
520         ZERO_STRUCT(response);
521
522         /* Send off request */
523
524         fstrcpy(request.data.sid, sid);
525
526         if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
527             NSS_STATUS_SUCCESS)
528                 return False;
529
530         /* Display response */
531
532         d_printf("%s%c%s %d\n", response.data.name.dom_name, 
533                  winbind_separator(), response.data.name.name, 
534                  response.data.name.type);
535
536         return True;
537 }
538
539 /* Convert string to sid */
540
541 static BOOL wbinfo_lookupname(char *name)
542 {
543         struct winbindd_request request;
544         struct winbindd_response response;
545
546         /* Send off request */
547
548         ZERO_STRUCT(request);
549         ZERO_STRUCT(response);
550
551         parse_wbinfo_domain_user(name, request.data.name.dom_name, 
552                                  request.data.name.name);
553
554         if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
555             NSS_STATUS_SUCCESS)
556                 return False;
557
558         /* Display response */
559
560         d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
561
562         return True;
563 }
564
565 /* Authenticate a user with a plaintext password */
566
567 static BOOL wbinfo_auth(char *username)
568 {
569         struct winbindd_request request;
570         struct winbindd_response response;
571         NSS_STATUS result;
572         char *p;
573
574         /* Send off request */
575
576         ZERO_STRUCT(request);
577         ZERO_STRUCT(response);
578
579         p = strchr(username, '%');
580
581         if (p) {
582                 *p = 0;
583                 fstrcpy(request.data.auth.user, username);
584                 fstrcpy(request.data.auth.pass, p + 1);
585                 *p = '%';
586         } else
587                 fstrcpy(request.data.auth.user, username);
588
589         result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
590
591         /* Display response */
592
593         d_printf("plaintext password authentication %s\n", 
594                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
595
596         if (response.data.auth.nt_status)
597                 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n", 
598                          response.data.auth.nt_status_string, 
599                          response.data.auth.nt_status,
600                          response.data.auth.error_string);
601
602         return result == NSS_STATUS_SUCCESS;
603 }
604
605 /* Authenticate a user with a challenge/response */
606
607 static BOOL wbinfo_auth_crap(char *username)
608 {
609         struct winbindd_request request;
610         struct winbindd_response response;
611         NSS_STATUS result;
612         fstring name_user;
613         fstring name_domain;
614         fstring pass;
615         char *p;
616
617         /* Send off request */
618
619         ZERO_STRUCT(request);
620         ZERO_STRUCT(response);
621
622         p = strchr(username, '%');
623
624         if (p) {
625                 *p = 0;
626                 fstrcpy(pass, p + 1);
627         }
628                 
629         parse_wbinfo_domain_user(username, name_domain, name_user);
630
631         fstrcpy(request.data.auth_crap.user, name_user);
632
633         fstrcpy(request.data.auth_crap.domain, 
634                               name_domain);
635
636         generate_random_buffer(request.data.auth_crap.chal, 8);
637         
638         if (lp_client_ntlmv2_auth()) {
639                 DATA_BLOB server_chal;
640                 DATA_BLOB names_blob;   
641
642                 DATA_BLOB lm_response;
643                 DATA_BLOB nt_response;
644
645                 server_chal = data_blob(request.data.auth_crap.chal, 8); 
646                 
647                 /* Pretend this is a login to 'us', for blob purposes */
648                 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
649                 
650                 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal, 
651                                       &names_blob,
652                                       &lm_response, &nt_response, NULL)) {
653                         data_blob_free(&names_blob);
654                         data_blob_free(&server_chal);
655                         return False;
656                 }
657                 data_blob_free(&names_blob);
658                 data_blob_free(&server_chal);
659
660                 memcpy(request.data.auth_crap.nt_resp, nt_response.data, 
661                        MIN(nt_response.length, 
662                            sizeof(request.data.auth_crap.nt_resp)));
663                 request.data.auth_crap.nt_resp_len = nt_response.length;
664
665                 memcpy(request.data.auth_crap.lm_resp, lm_response.data, 
666                        MIN(lm_response.length, 
667                            sizeof(request.data.auth_crap.lm_resp)));
668                 request.data.auth_crap.lm_resp_len = lm_response.length;
669                        
670                 data_blob_free(&nt_response);
671                 data_blob_free(&lm_response);
672
673         } else {
674                 if (lp_client_lanman_auth() 
675                     && SMBencrypt(pass, request.data.auth_crap.chal, 
676                                (uchar *)request.data.auth_crap.lm_resp)) {
677                         request.data.auth_crap.lm_resp_len = 24;
678                 } else {
679                         request.data.auth_crap.lm_resp_len = 0;
680                 }
681                 SMBNTencrypt(pass, request.data.auth_crap.chal,
682                              (uchar *)request.data.auth_crap.nt_resp);
683
684                 request.data.auth_crap.nt_resp_len = 24;
685         }
686
687         result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
688
689         /* Display response */
690
691         d_printf("challenge/response password authentication %s\n", 
692                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
693
694         if (response.data.auth.nt_status)
695                 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n", 
696                          response.data.auth.nt_status_string, 
697                          response.data.auth.nt_status,
698                          response.data.auth.error_string);
699
700         return result == NSS_STATUS_SUCCESS;
701 }
702
703 /* Authenticate a user with a plaintext password and set a token */
704
705 static BOOL wbinfo_klog(char *username)
706 {
707         struct winbindd_request request;
708         struct winbindd_response response;
709         NSS_STATUS result;
710         char *p;
711
712         /* Send off request */
713
714         ZERO_STRUCT(request);
715         ZERO_STRUCT(response);
716
717         p = strchr(username, '%');
718
719         if (p) {
720                 *p = 0;
721                 fstrcpy(request.data.auth.user, username);
722                 fstrcpy(request.data.auth.pass, p + 1);
723                 *p = '%';
724         } else {
725                 fstrcpy(request.data.auth.user, username);
726                 fstrcpy(request.data.auth.pass, getpass("Password: "));
727         }
728
729         request.flags |= WBFLAG_PAM_AFS_TOKEN;
730
731         result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
732
733         /* Display response */
734
735         d_printf("plaintext password authentication %s\n", 
736                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
737
738         if (response.data.auth.nt_status)
739                 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n", 
740                          response.data.auth.nt_status_string, 
741                          response.data.auth.nt_status,
742                          response.data.auth.error_string);
743
744         if (result != NSS_STATUS_SUCCESS)
745                 return False;
746
747         if (response.extra_data == NULL) {
748                 d_printf("Did not get token data\n");
749                 return False;
750         }
751
752         if (!afs_settoken_str((char *)response.extra_data)) {
753                 d_printf("Could not set token\n");
754                 return False;
755         }
756
757         d_printf("Successfully created AFS token\n");
758         return True;
759 }
760
761 /* Print domain users */
762
763 static BOOL print_domain_users(const char *domain)
764 {
765         struct winbindd_request request;
766         struct winbindd_response response;
767         const char *extra_data;
768         fstring name;
769
770         /* Send request to winbind daemon */
771
772         ZERO_STRUCT(request);
773         ZERO_STRUCT(response);
774         
775         if (domain) {
776                 /* '.' is the special sign for our own domwin */
777                 if ( strequal(domain, ".") )
778                         fstrcpy( request.domain_name, lp_workgroup() );
779                 else
780                         fstrcpy( request.domain_name, domain );
781         }
782
783         if (winbindd_request(WINBINDD_LIST_USERS, &request, &response) !=
784             NSS_STATUS_SUCCESS)
785                 return False;
786
787         /* Look through extra data */
788
789         if (!response.extra_data)
790                 return False;
791
792         extra_data = (const char *)response.extra_data;
793
794         while(next_token(&extra_data, name, ",", sizeof(fstring)))
795                 d_printf("%s\n", name);
796         
797         SAFE_FREE(response.extra_data);
798
799         return True;
800 }
801
802 /* Print domain groups */
803
804 static BOOL print_domain_groups(const char *domain)
805 {
806         struct winbindd_request  request;
807         struct winbindd_response response;
808         const char *extra_data;
809         fstring name;
810
811         ZERO_STRUCT(request);
812         ZERO_STRUCT(response);
813
814         if (domain) {
815                 if ( strequal(domain, ".") )
816                         fstrcpy( request.domain_name, lp_workgroup() );
817                 else
818                         fstrcpy( request.domain_name, domain );
819         }
820
821         if (winbindd_request(WINBINDD_LIST_GROUPS, &request, &response) !=
822             NSS_STATUS_SUCCESS)
823                 return False;
824
825         /* Look through extra data */
826
827         if (!response.extra_data)
828                 return False;
829
830         extra_data = (const char *)response.extra_data;
831
832         while(next_token(&extra_data, name, ",", sizeof(fstring)))
833                 d_printf("%s\n", name);
834
835         SAFE_FREE(response.extra_data);
836         
837         return True;
838 }
839
840 /* Set the authorised user for winbindd access in secrets.tdb */
841
842 static BOOL wbinfo_set_auth_user(char *username)
843 {
844         const char *password;
845         char *p;
846         fstring user, domain;
847
848         /* Separate into user and password */
849
850         parse_wbinfo_domain_user(username, domain, user);
851
852         p = strchr(user, '%');
853
854         if (p != NULL) {
855                 *p = 0;
856                 password = p+1;
857         } else {
858                 char *thepass = getpass("Password: ");
859                 if (thepass) {
860                         password = thepass;     
861                 } else
862                         password = "";
863         }
864
865         /* Store or remove DOMAIN\username%password in secrets.tdb */
866
867         secrets_init();
868
869         if (user[0]) {
870
871                 if (!secrets_store(SECRETS_AUTH_USER, user,
872                                    strlen(user) + 1)) {
873                         d_fprintf(stderr, "error storing username\n");
874                         return False;
875                 }
876
877                 /* We always have a domain name added by the
878                    parse_wbinfo_domain_user() function. */
879
880                 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
881                                    strlen(domain) + 1)) {
882                         d_fprintf(stderr, "error storing domain name\n");
883                         return False;
884                 }
885
886         } else {
887                 secrets_delete(SECRETS_AUTH_USER);
888                 secrets_delete(SECRETS_AUTH_DOMAIN);
889         }
890
891         if (password[0]) {
892
893                 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
894                                    strlen(password) + 1)) {
895                         d_fprintf(stderr, "error storing password\n");
896                         return False;
897                 }
898
899         } else
900                 secrets_delete(SECRETS_AUTH_PASSWORD);
901
902         return True;
903 }
904
905 static void wbinfo_get_auth_user(void)
906 {
907         char *user, *domain, *password;
908
909         /* Lift data from secrets file */
910         
911         secrets_fetch_ipc_userpass(&user, &domain, &password);
912
913         if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
914
915                 SAFE_FREE(user);
916                 SAFE_FREE(domain);
917                 SAFE_FREE(password);
918                 d_printf("No authorised user configured\n");
919                 return;
920         }
921
922         /* Pretty print authorised user info */
923
924         d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
925                  user, password ? "%" : "", password ? password : "");
926
927         SAFE_FREE(user);
928         SAFE_FREE(domain);
929         SAFE_FREE(password);
930 }
931
932 static BOOL wbinfo_ping(void)
933 {
934         NSS_STATUS result;
935
936         result = winbindd_request(WINBINDD_PING, NULL, NULL);
937
938         /* Display response */
939
940         d_printf("Ping to winbindd %s on fd %d\n", 
941                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
942
943         return result == NSS_STATUS_SUCCESS;
944 }
945
946 /* Main program */
947
948 enum {
949         OPT_SET_AUTH_USER = 1000,
950         OPT_GET_AUTH_USER,
951         OPT_DOMAIN_NAME,
952         OPT_SEQUENCE,
953         OPT_GETDCNAME,
954         OPT_USERDOMGROUPS,
955         OPT_USERSIDS
956 };
957
958 int main(int argc, char **argv)
959 {
960         int opt;
961
962         poptContext pc;
963         static char *string_arg;
964         static char *opt_domain_name;
965         static int int_arg;
966         int result = 1;
967
968         struct poptOption long_options[] = {
969                 POPT_AUTOHELP
970
971                 /* longName, shortName, argInfo, argPtr, value, descrip, 
972                    argDesc */
973
974                 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
975                 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
976                 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
977                 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
978                 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
979                 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
980                 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
981                 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
982                 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
983                 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
984                 { "allocate-rid", 'A', POPT_ARG_NONE, 0, 'A', "Get a new RID out of idmap" },
985                 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
986                 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
987                 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
988                 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
989                 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
990                 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
991                   OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
992                 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
993                 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
994                 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
995                 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
996                   "Get a DC name for a foreign domain", "domainname" },
997                 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
998                 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
999                 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1000 #ifdef WITH_FAKE_KASERVER
1001                 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1002 #endif
1003                 POPT_COMMON_VERSION
1004                 POPT_TABLEEND
1005         };
1006
1007         /* Samba client initialisation */
1008
1009         if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
1010                 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1011                         dyn_CONFIGFILE, strerror(errno));
1012                 exit(1);
1013         }
1014
1015         if (!init_names())
1016                 return 1;
1017
1018         load_interfaces();
1019
1020         /* Parse options */
1021
1022         pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1023
1024         /* Parse command line options */
1025
1026         if (argc == 1) {
1027                 poptPrintHelp(pc, stderr, 0);
1028                 return 1;
1029         }
1030
1031         while((opt = poptGetNextOpt(pc)) != -1) {
1032                 /* get the generic configuration parameters like --domain */
1033         }
1034
1035         poptFreeContext(pc);
1036
1037         pc = poptGetContext(NULL, argc, (const char **)argv, long_options, 
1038                             POPT_CONTEXT_KEEP_FIRST);
1039
1040         while((opt = poptGetNextOpt(pc)) != -1) {
1041                 switch (opt) {
1042                 case 'u':
1043                         if (!print_domain_users(opt_domain_name)) {
1044                                 d_printf("Error looking up domain users\n");
1045                                 goto done;
1046                         }
1047                         break;
1048                 case 'g':
1049                         if (!print_domain_groups(opt_domain_name)) {
1050                                 d_printf("Error looking up domain groups\n");
1051                                 goto done;
1052                         }
1053                         break;
1054                 case 's':
1055                         if (!wbinfo_lookupsid(string_arg)) {
1056                                 d_printf("Could not lookup sid %s\n", string_arg);
1057                                 goto done;
1058                         }
1059                         break;
1060                 case 'n':
1061                         if (!wbinfo_lookupname(string_arg)) {
1062                                 d_printf("Could not lookup name %s\n", string_arg);
1063                                 goto done;
1064                         }
1065                         break;
1066                 case 'N':
1067                         if (!wbinfo_wins_byname(string_arg)) {
1068                                 d_printf("Could not lookup WINS by name %s\n", string_arg);
1069                                 goto done;
1070                         }
1071                         break;
1072                 case 'I':
1073                         if (!wbinfo_wins_byip(string_arg)) {
1074                                 d_printf("Could not lookup WINS by IP %s\n", string_arg);
1075                                 goto done;
1076                         }
1077                         break;
1078                 case 'U':
1079                         if (!wbinfo_uid_to_sid(int_arg)) {
1080                                 d_printf("Could not convert uid %d to sid\n", int_arg);
1081                                 goto done;
1082                         }
1083                         break;
1084                 case 'G':
1085                         if (!wbinfo_gid_to_sid(int_arg)) {
1086                                 d_printf("Could not convert gid %d to sid\n",
1087                                        int_arg);
1088                                 goto done;
1089                         }
1090                         break;
1091                 case 'S':
1092                         if (!wbinfo_sid_to_uid(string_arg)) {
1093                                 d_printf("Could not convert sid %s to uid\n",
1094                                        string_arg);
1095                                 goto done;
1096                         }
1097                         break;
1098                 case 'Y':
1099                         if (!wbinfo_sid_to_gid(string_arg)) {
1100                                 d_printf("Could not convert sid %s to gid\n",
1101                                        string_arg);
1102                                 goto done;
1103                         }
1104                         break;
1105                 case 'A':
1106                         if (!wbinfo_allocate_rid()) {
1107                                 d_printf("Could not allocate a RID\n");
1108                                 goto done;
1109                         }
1110                         break;
1111                 case 't':
1112                         if (!wbinfo_check_secret()) {
1113                                 d_printf("Could not check secret\n");
1114                                 goto done;
1115                         }
1116                         break;
1117                 case 'm':
1118                         if (!wbinfo_list_domains()) {
1119                                 d_printf("Could not list trusted domains\n");
1120                                 goto done;
1121                         }
1122                         break;
1123                 case OPT_SEQUENCE:
1124                         if (!wbinfo_show_sequence(opt_domain_name)) {
1125                                 d_printf("Could not show sequence numbers\n");
1126                                 goto done;
1127                         }
1128                         break;
1129                 case 'D':
1130                         if (!wbinfo_domain_info(string_arg)) {
1131                                 d_printf("Could not get domain info\n");
1132                                 goto done;
1133                         }
1134                         break;
1135                 case 'r':
1136                         if (!wbinfo_get_usergroups(string_arg)) {
1137                                 d_printf("Could not get groups for user %s\n", 
1138                                        string_arg);
1139                                 goto done;
1140                         }
1141                         break;
1142                 case OPT_USERSIDS:
1143                         if (!wbinfo_get_usersids(string_arg)) {
1144                                 d_printf("Could not get group SIDs for user SID %s\n", 
1145                                        string_arg);
1146                                 goto done;
1147                         }
1148                         break;
1149                 case OPT_USERDOMGROUPS:
1150                         if (!wbinfo_get_userdomgroups(string_arg)) {
1151                                 d_printf("Could not get user's domain groups "
1152                                          "for user SID %s\n", string_arg);
1153                                 goto done;
1154                         }
1155                         break;
1156                 case 'a': {
1157                                 BOOL got_error = False;
1158
1159                                 if (!wbinfo_auth(string_arg)) {
1160                                         d_printf("Could not authenticate user %s with "
1161                                                 "plaintext password\n", string_arg);
1162                                         got_error = True;
1163                                 }
1164
1165                                 if (!wbinfo_auth_crap(string_arg)) {
1166                                         d_printf("Could not authenticate user %s with "
1167                                                 "challenge/response\n", string_arg);
1168                                         got_error = True;
1169                                 }
1170
1171                                 if (got_error)
1172                                         goto done;
1173                                 break;
1174                         }
1175                 case 'k':
1176                         if (!wbinfo_klog(string_arg)) {
1177                                 d_printf("Could not klog user\n");
1178                                 goto done;
1179                         }
1180                         break;
1181                 case 'p':
1182                         if (!wbinfo_ping()) {
1183                                 d_printf("could not ping winbindd!\n");
1184                                 goto done;
1185                         }
1186                         break;
1187                 case OPT_SET_AUTH_USER:
1188                         wbinfo_set_auth_user(string_arg);
1189                         break;
1190                 case OPT_GET_AUTH_USER:
1191                         wbinfo_get_auth_user();
1192                         break;
1193                 case OPT_GETDCNAME:
1194                         wbinfo_getdcname(string_arg);
1195                         break;
1196                 /* generic configuration options */
1197                 case OPT_DOMAIN_NAME:
1198                         break;
1199                 default:
1200                         d_fprintf(stderr, "Invalid option\n");
1201                         poptPrintHelp(pc, stderr, 0);
1202                         goto done;
1203                 }
1204         }
1205
1206         result = 0;
1207
1208         /* Exit code */
1209
1210  done:
1211         poptFreeContext(pc);
1212         return result;
1213 }