792af84827f4e182296fafdb4e6ae150e14afb95
[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 /* Convert NetBIOS name to IP */
171
172 static BOOL wbinfo_wins_byname(char *name)
173 {
174         struct winbindd_request request;
175         struct winbindd_response response;
176
177         ZERO_STRUCT(request);
178         ZERO_STRUCT(response);
179
180         /* Send request */
181
182         fstrcpy(request.data.winsreq, name);
183
184         if (winbindd_request(WINBINDD_WINS_BYNAME, &request, &response) !=
185             NSS_STATUS_SUCCESS) {
186                 return False;
187         }
188
189         /* Display response */
190
191         printf("%s\n", response.data.winsresp);
192
193         return True;
194 }
195
196 /* Convert IP to NetBIOS name */
197
198 static BOOL wbinfo_wins_byip(char *ip)
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, ip);
209
210         if (winbindd_request(WINBINDD_WINS_BYIP, &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 /* List trusted domains */
223
224 static BOOL wbinfo_list_domains(void)
225 {
226         struct winbindd_response response;
227         fstring name;
228
229         ZERO_STRUCT(response);
230
231         /* Send request */
232
233         if (winbindd_request(WINBINDD_LIST_TRUSTDOM, NULL, &response) !=
234             NSS_STATUS_SUCCESS)
235                 return False;
236
237         /* Display response */
238
239         if (response.extra_data) {
240                 const char *extra_data = (char *)response.extra_data;
241
242                 while(next_token(&extra_data, name, ",", sizeof(fstring)))
243                         d_printf("%s\n", name);
244
245                 SAFE_FREE(response.extra_data);
246         }
247
248         return True;
249 }
250
251
252 /* show sequence numbers */
253 static BOOL wbinfo_show_sequence(const char *domain)
254 {
255         struct winbindd_request  request;
256         struct winbindd_response response;
257
258         ZERO_STRUCT(response);
259         ZERO_STRUCT(request);
260
261         if ( domain )
262                 fstrcpy( request.domain_name, domain );
263
264         /* Send request */
265
266         if (winbindd_request(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
267             NSS_STATUS_SUCCESS)
268                 return False;
269
270         /* Display response */
271
272         if (response.extra_data) {
273                 char *extra_data = (char *)response.extra_data;
274                 d_printf("%s", extra_data);
275                 SAFE_FREE(response.extra_data);
276         }
277
278         return True;
279 }
280
281 /* Show domain info */
282
283 static BOOL wbinfo_domain_info(const char *domain_name)
284 {
285         struct winbindd_request request;
286         struct winbindd_response response;
287
288         ZERO_STRUCT(request);
289         ZERO_STRUCT(response);
290
291         fstrcpy(request.domain_name, domain_name);
292
293         /* Send request */
294
295         if (winbindd_request(WINBINDD_DOMAIN_INFO, &request, &response) !=
296             NSS_STATUS_SUCCESS)
297                 return False;
298
299         /* Display response */
300
301         d_printf("Name    : %s\n", response.data.domain_info.name);
302         d_printf("Alt_Name: %s\n", response.data.domain_info.alt_name);
303
304         d_printf("SID     : %s\n", response.data.domain_info.sid);
305
306         d_printf("Native  : %s\n",
307                  response.data.domain_info.native_mode ? "Yes" : "No");
308
309         d_printf("Primary : %s\n",
310                  response.data.domain_info.primary ? "Yes" : "No");
311
312         d_printf("Sequence: %d\n", response.data.domain_info.sequence_number);
313
314         return True;
315 }
316
317 /* Check trust account password */
318
319 static BOOL wbinfo_check_secret(void)
320 {
321         struct winbindd_response response;
322         NSS_STATUS result;
323
324         ZERO_STRUCT(response);
325
326         result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response);
327                 
328         d_printf("checking the trust secret via RPC calls %s\n", 
329                  (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
330
331         if (result != NSS_STATUS_SUCCESS)       
332                 d_printf("error code was %s (0x%x)\n", 
333                          response.data.auth.nt_status_string, 
334                          response.data.auth.nt_status);
335         
336         return result == NSS_STATUS_SUCCESS;    
337 }
338
339 /* Convert uid to sid */
340
341 static BOOL wbinfo_uid_to_sid(uid_t uid)
342 {
343         struct winbindd_request request;
344         struct winbindd_response response;
345
346         ZERO_STRUCT(request);
347         ZERO_STRUCT(response);
348
349         /* Send request */
350
351         request.data.uid = uid;
352
353         if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
354             NSS_STATUS_SUCCESS)
355                 return False;
356
357         /* Display response */
358
359         d_printf("%s\n", response.data.sid.sid);
360
361         return True;
362 }
363
364 /* Convert gid to sid */
365
366 static BOOL wbinfo_gid_to_sid(gid_t gid)
367 {
368         struct winbindd_request request;
369         struct winbindd_response response;
370
371         ZERO_STRUCT(request);
372         ZERO_STRUCT(response);
373
374         /* Send request */
375
376         request.data.gid = gid;
377
378         if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) !=
379             NSS_STATUS_SUCCESS)
380                 return False;
381
382         /* Display response */
383
384         d_printf("%s\n", response.data.sid.sid);
385
386         return True;
387 }
388
389 /* Convert sid to uid */
390
391 static BOOL wbinfo_sid_to_uid(char *sid)
392 {
393         struct winbindd_request request;
394         struct winbindd_response response;
395
396         ZERO_STRUCT(request);
397         ZERO_STRUCT(response);
398
399         /* Send request */
400
401         fstrcpy(request.data.sid, sid);
402
403         if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
404             NSS_STATUS_SUCCESS)
405                 return False;
406
407         /* Display response */
408
409         d_printf("%d\n", (int)response.data.uid);
410
411         return True;
412 }
413
414 static BOOL wbinfo_sid_to_gid(char *sid)
415 {
416         struct winbindd_request request;
417         struct winbindd_response response;
418
419         ZERO_STRUCT(request);
420         ZERO_STRUCT(response);
421
422         /* Send request */
423
424         fstrcpy(request.data.sid, sid);
425
426         if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
427             NSS_STATUS_SUCCESS)
428                 return False;
429
430         /* Display response */
431
432         d_printf("%d\n", (int)response.data.gid);
433
434         return True;
435 }
436
437 /* Convert sid to string */
438
439 static BOOL wbinfo_lookupsid(char *sid)
440 {
441         struct winbindd_request request;
442         struct winbindd_response response;
443
444         ZERO_STRUCT(request);
445         ZERO_STRUCT(response);
446
447         /* Send off request */
448
449         fstrcpy(request.data.sid, sid);
450
451         if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
452             NSS_STATUS_SUCCESS)
453                 return False;
454
455         /* Display response */
456
457         d_printf("%s%c%s %d\n", response.data.name.dom_name, 
458                  winbind_separator(), response.data.name.name, 
459                  response.data.name.type);
460
461         return True;
462 }
463
464 /* Convert string to sid */
465
466 static BOOL wbinfo_lookupname(char *name)
467 {
468         struct winbindd_request request;
469         struct winbindd_response response;
470
471         /* Send off request */
472
473         ZERO_STRUCT(request);
474         ZERO_STRUCT(response);
475
476         parse_wbinfo_domain_user(name, request.data.name.dom_name, 
477                                  request.data.name.name);
478
479         if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
480             NSS_STATUS_SUCCESS)
481                 return False;
482
483         /* Display response */
484
485         d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
486
487         return True;
488 }
489
490 /* Authenticate a user with a plaintext password */
491
492 static BOOL wbinfo_auth(char *username)
493 {
494         struct winbindd_request request;
495         struct winbindd_response response;
496         NSS_STATUS result;
497         char *p;
498
499         /* Send off request */
500
501         ZERO_STRUCT(request);
502         ZERO_STRUCT(response);
503
504         p = strchr(username, '%');
505
506         if (p) {
507                 *p = 0;
508                 fstrcpy(request.data.auth.user, username);
509                 fstrcpy(request.data.auth.pass, p + 1);
510                 *p = '%';
511         } else
512                 fstrcpy(request.data.auth.user, username);
513
514         result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
515
516         /* Display response */
517
518         d_printf("plaintext password authentication %s\n", 
519                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
520
521         if (response.data.auth.nt_status)
522                 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n", 
523                          response.data.auth.nt_status_string, 
524                          response.data.auth.nt_status,
525                          response.data.auth.error_string);
526
527         return result == NSS_STATUS_SUCCESS;
528 }
529
530 /* Authenticate a user with a challenge/response */
531
532 static BOOL wbinfo_auth_crap(char *username)
533 {
534         struct winbindd_request request;
535         struct winbindd_response response;
536         NSS_STATUS result;
537         fstring name_user;
538         fstring name_domain;
539         fstring pass;
540         char *p;
541
542         /* Send off request */
543
544         ZERO_STRUCT(request);
545         ZERO_STRUCT(response);
546
547         p = strchr(username, '%');
548
549         if (p) {
550                 *p = 0;
551                 fstrcpy(pass, p + 1);
552         }
553                 
554         parse_wbinfo_domain_user(username, name_domain, name_user);
555
556         if (push_utf8_fstring(request.data.auth_crap.user, name_user) == -1) {
557                 d_printf("unable to create utf8 string for '%s'\n",
558                          name_user);
559                 return False;
560         }
561
562         if (push_utf8_fstring(request.data.auth_crap.domain, 
563                               name_domain) == -1) {
564                 d_printf("unable to create utf8 string for '%s'\n",
565                          name_domain);
566                 return False;
567         }
568
569         generate_random_buffer(request.data.auth_crap.chal, 8, False);
570         
571         SMBencrypt(pass, request.data.auth_crap.chal, 
572                    (uchar *)request.data.auth_crap.lm_resp);
573         SMBNTencrypt(pass, request.data.auth_crap.chal,
574                      (uchar *)request.data.auth_crap.nt_resp);
575
576         request.data.auth_crap.lm_resp_len = 24;
577         request.data.auth_crap.nt_resp_len = 24;
578
579         result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
580
581         /* Display response */
582
583         d_printf("challenge/response password authentication %s\n", 
584                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
585
586         if (response.data.auth.nt_status)
587                 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n", 
588                          response.data.auth.nt_status_string, 
589                          response.data.auth.nt_status,
590                          response.data.auth.error_string);
591
592         return result == NSS_STATUS_SUCCESS;
593 }
594
595 /******************************************************************
596  create a winbindd user
597 ******************************************************************/
598
599 static BOOL wbinfo_create_user(char *username)
600 {
601         struct winbindd_request request;
602         struct winbindd_response response;
603         NSS_STATUS result;
604
605         /* Send off request */
606
607         ZERO_STRUCT(request);
608         ZERO_STRUCT(response);
609
610         request.flags = WBFLAG_ALLOCATE_RID;
611         fstrcpy(request.data.acct_mgt.username, username);
612
613         result = winbindd_request(WINBINDD_CREATE_USER, &request, &response);
614         
615         if ( result == NSS_STATUS_SUCCESS )
616                 d_printf("New RID is %d\n", response.data.rid);
617         
618         return result == NSS_STATUS_SUCCESS;
619 }
620
621 /******************************************************************
622  remove a winbindd user
623 ******************************************************************/
624
625 static BOOL wbinfo_delete_user(char *username)
626 {
627         struct winbindd_request request;
628         struct winbindd_response response;
629         NSS_STATUS result;
630
631         /* Send off request */
632
633         ZERO_STRUCT(request);
634         ZERO_STRUCT(response);
635
636         fstrcpy(request.data.acct_mgt.username, username);
637
638         result = winbindd_request(WINBINDD_DELETE_USER, &request, &response);
639         
640         return result == NSS_STATUS_SUCCESS;
641 }
642
643 /******************************************************************
644  create a winbindd group
645 ******************************************************************/
646
647 static BOOL wbinfo_create_group(char *groupname)
648 {
649         struct winbindd_request request;
650         struct winbindd_response response;
651         NSS_STATUS result;
652
653         /* Send off request */
654
655         ZERO_STRUCT(request);
656         ZERO_STRUCT(response);
657
658         fstrcpy(request.data.acct_mgt.groupname, groupname);
659
660         result = winbindd_request(WINBINDD_CREATE_GROUP, &request, &response);
661         
662         return result == NSS_STATUS_SUCCESS;
663 }
664
665 /******************************************************************
666  remove a winbindd group
667 ******************************************************************/
668
669 static BOOL wbinfo_delete_group(char *groupname)
670 {
671         struct winbindd_request request;
672         struct winbindd_response response;
673         NSS_STATUS result;
674
675         /* Send off request */
676
677         ZERO_STRUCT(request);
678         ZERO_STRUCT(response);
679
680         fstrcpy(request.data.acct_mgt.groupname, groupname);
681
682         result = winbindd_request(WINBINDD_DELETE_GROUP, &request, &response);
683         
684         return result == NSS_STATUS_SUCCESS;
685 }
686
687 /******************************************************************
688  parse a string in the form user:group
689 ******************************************************************/
690
691 static BOOL parse_user_group( const char *string, fstring user, fstring group )
692 {
693         char *p;
694         
695         if ( !string )
696                 return False;
697         
698         if ( !(p = strchr( string, ':' )) )
699                 return False;
700                 
701         *p = '\0';
702         p++;
703         
704         fstrcpy( user, string );
705         fstrcpy( group, p );
706         
707         return True;
708 }
709
710 /******************************************************************
711  add a user to a winbindd group
712 ******************************************************************/
713
714 static BOOL wbinfo_add_user_to_group(char *string)
715 {
716         struct winbindd_request request;
717         struct winbindd_response response;
718         NSS_STATUS result;
719
720         /* Send off request */
721
722         ZERO_STRUCT(request);
723         ZERO_STRUCT(response);
724
725         if ( !parse_user_group( string, request.data.acct_mgt.username,
726                 request.data.acct_mgt.groupname))
727         {
728                 d_printf("Can't parse user:group from %s\n", string);
729                 return False;
730         }
731
732         result = winbindd_request(WINBINDD_ADD_USER_TO_GROUP, &request, &response);
733         
734         return result == NSS_STATUS_SUCCESS;
735 }
736
737 /******************************************************************
738  remove a user from a winbindd group
739 ******************************************************************/
740
741 static BOOL wbinfo_remove_user_from_group(char *string)
742 {
743         struct winbindd_request request;
744         struct winbindd_response response;
745         NSS_STATUS result;
746
747         /* Send off request */
748
749         ZERO_STRUCT(request);
750         ZERO_STRUCT(response);
751
752         if ( !parse_user_group( string, request.data.acct_mgt.username,
753                 request.data.acct_mgt.groupname))
754         {
755                 d_printf("Can't parse user:group from %s\n", string);
756                 return False;
757         }
758
759         result = winbindd_request(WINBINDD_REMOVE_USER_FROM_GROUP, &request, &response);
760         
761         return result == NSS_STATUS_SUCCESS;
762 }
763
764 /* Print domain users */
765
766 static BOOL print_domain_users(const char *domain)
767 {
768         struct winbindd_request request;
769         struct winbindd_response response;
770         const char *extra_data;
771         fstring name;
772
773         /* Send request to winbind daemon */
774
775         ZERO_STRUCT(request);
776         ZERO_STRUCT(response);
777         
778         if (domain) {
779                 /* '.' is the special sign for our own domwin */
780                 if ( strequal(domain, ".") )
781                         fstrcpy( request.domain_name, lp_workgroup() );
782                 else
783                         fstrcpy( request.domain_name, domain );
784         }
785
786         if (winbindd_request(WINBINDD_LIST_USERS, &request, &response) !=
787             NSS_STATUS_SUCCESS)
788                 return False;
789
790         /* Look through extra data */
791
792         if (!response.extra_data)
793                 return False;
794
795         extra_data = (const char *)response.extra_data;
796
797         while(next_token(&extra_data, name, ",", sizeof(fstring)))
798                 d_printf("%s\n", name);
799         
800         SAFE_FREE(response.extra_data);
801
802         return True;
803 }
804
805 /* Print domain groups */
806
807 static BOOL print_domain_groups(const char *domain)
808 {
809         struct winbindd_request  request;
810         struct winbindd_response response;
811         const char *extra_data;
812         fstring name;
813
814         ZERO_STRUCT(request);
815         ZERO_STRUCT(response);
816
817         if (domain) {
818                 if ( strequal(domain, ".") )
819                         fstrcpy( request.domain_name, lp_workgroup() );
820                 else
821                         fstrcpy( request.domain_name, domain );
822         }
823
824         if (winbindd_request(WINBINDD_LIST_GROUPS, &request, &response) !=
825             NSS_STATUS_SUCCESS)
826                 return False;
827
828         /* Look through extra data */
829
830         if (!response.extra_data)
831                 return False;
832
833         extra_data = (const char *)response.extra_data;
834
835         while(next_token(&extra_data, name, ",", sizeof(fstring)))
836                 d_printf("%s\n", name);
837
838         SAFE_FREE(response.extra_data);
839         
840         return True;
841 }
842
843 /* Set the authorised user for winbindd access in secrets.tdb */
844
845 static BOOL wbinfo_set_auth_user(char *username)
846 {
847         char *password;
848         fstring user, domain;
849
850         /* Separate into user and password */
851
852         parse_wbinfo_domain_user(username, domain, user);
853
854         password = strchr(user, '%');
855
856         if (password) {
857                 *password = 0;
858                 password++;
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_init();
914
915         user = secrets_fetch(SECRETS_AUTH_USER, NULL);
916         domain = secrets_fetch(SECRETS_AUTH_DOMAIN, NULL);
917         password = secrets_fetch(SECRETS_AUTH_PASSWORD, NULL);
918
919         if (!user && !domain && !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 ? "\\" : "",
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(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_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                 { "create-user", 'c', POPT_ARG_STRING, &string_arg, 'c', "Create a local user account", "name" },
985                 { "delete-user", 'x', POPT_ARG_STRING, &string_arg, 'x', "Delete a local user account", "name" },
986                 { "create-group", 'C', POPT_ARG_STRING, &string_arg, 'C', "Create a local group", "name" },
987                 { "delete-group", 'X', POPT_ARG_STRING, &string_arg, 'X', "Delete a local group", "name" },
988                 { "add-to-group", 'o', POPT_ARG_STRING, &string_arg, 'o', "Add user to group", "user:group" },
989                 { "del-from-group", 'O', POPT_ARG_STRING, &string_arg, 'O', "Remove user from group", "user:group" },
990                 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
991                 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
992                 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
993                 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D',
994                   "Show all most info we have about the domain" },
995                 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
996                 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
997                 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
998                 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
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                 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 't':
1105                         if (!wbinfo_check_secret()) {
1106                                 d_printf("Could not check secret\n");
1107                                 goto done;
1108                         }
1109                         break;
1110                 case 'm':
1111                         if (!wbinfo_list_domains()) {
1112                                 d_printf("Could not list trusted domains\n");
1113                                 goto done;
1114                         }
1115                         break;
1116                 case OPT_SEQUENCE:
1117                         if (!wbinfo_show_sequence(opt_domain_name)) {
1118                                 d_printf("Could not show sequence numbers\n");
1119                                 goto done;
1120                         }
1121                         break;
1122                 case 'D':
1123                         if (!wbinfo_domain_info(string_arg)) {
1124                                 d_printf("Could not get domain info\n");
1125                                 goto done;
1126                         }
1127                         break;
1128                 case 'r':
1129                         if (!wbinfo_get_usergroups(string_arg)) {
1130                                 d_printf("Could not get groups for user %s\n", 
1131                                        string_arg);
1132                                 goto done;
1133                         }
1134                         break;
1135                 case OPT_USERSIDS:
1136                         if (!wbinfo_get_usersids(string_arg)) {
1137                                 d_printf("Could not get group SIDs for user SID %s\n", 
1138                                        string_arg);
1139                                 goto done;
1140                         }
1141                         break;
1142                 case 'a': {
1143                                 BOOL got_error = False;
1144
1145                                 if (!wbinfo_auth(string_arg)) {
1146                                         d_printf("Could not authenticate user %s with "
1147                                                 "plaintext password\n", string_arg);
1148                                         got_error = True;
1149                                 }
1150
1151                                 if (!wbinfo_auth_crap(string_arg)) {
1152                                         d_printf("Could not authenticate user %s with "
1153                                                 "challenge/response\n", string_arg);
1154                                         got_error = True;
1155                                 }
1156
1157                                 if (got_error)
1158                                         goto done;
1159                                 break;
1160                         }
1161                 case 'c':
1162                         if ( !wbinfo_create_user(string_arg) ) {
1163                                 d_printf("Could not create user account\n");
1164                                 goto done;
1165                         }
1166                         break;
1167                 case 'C':
1168                         if ( !wbinfo_create_group(string_arg) ) {
1169                                 d_printf("Could not create group\n");
1170                                 goto done;
1171                         }
1172                         break;
1173                 case 'o':
1174                         if ( !wbinfo_add_user_to_group(string_arg) ) {
1175                                 d_printf("Could not add user to group\n");
1176                                 goto done;
1177                         }
1178                         break;
1179                 case 'O':
1180                         if ( !wbinfo_remove_user_from_group(string_arg) ) {
1181                                 d_printf("Could not remove user kfrom group\n");
1182                                 goto done;
1183                         }
1184                         break;
1185                 case 'x':
1186                         if ( !wbinfo_delete_user(string_arg) ) {
1187                                 d_printf("Could not delete user account\n");
1188                                 goto done;
1189                         }
1190                         break;
1191                 case 'X':
1192                         if ( !wbinfo_delete_group(string_arg) ) {
1193                                 d_printf("Could not delete group\n");
1194                                 goto done;
1195                         }
1196                         break;
1197                 case 'p':
1198                         if (!wbinfo_ping()) {
1199                                 d_printf("could not ping winbindd!\n");
1200                                 goto done;
1201                         }
1202                         break;
1203                 case OPT_SET_AUTH_USER:
1204                         wbinfo_set_auth_user(string_arg);
1205                         break;
1206                 case OPT_GET_AUTH_USER:
1207                         wbinfo_get_auth_user();
1208                         break;
1209                 /* generic configuration options */
1210                 case OPT_DOMAIN_NAME:
1211                         break;
1212                 default:
1213                         d_fprintf(stderr, "Invalid option\n");
1214                         poptPrintHelp(pc, stderr, 0);
1215                         goto done;
1216                 }
1217         }
1218
1219         result = 0;
1220
1221         /* Exit code */
1222
1223  done:
1224         poptFreeContext(pc);
1225         return result;
1226 }