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