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