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