sync 3.0 into HEAD for the last time
[vlendec/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(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         if (push_utf8_fstring(request.data.auth_crap.user, name_user) == -1) {
490                 d_printf("unable to create utf8 string for '%s'\n",
491                          name_user);
492                 return False;
493         }
494
495         if (push_utf8_fstring(request.data.auth_crap.domain, 
496                               name_domain) == -1) {
497                 d_printf("unable to create utf8 string for '%s'\n",
498                          name_domain);
499                 return False;
500         }
501
502         generate_random_buffer(request.data.auth_crap.chal, 8, False);
503         
504         SMBencrypt(pass, request.data.auth_crap.chal, 
505                    (uchar *)request.data.auth_crap.lm_resp);
506         SMBNTencrypt(pass, request.data.auth_crap.chal,
507                      (uchar *)request.data.auth_crap.nt_resp);
508
509         request.data.auth_crap.lm_resp_len = 24;
510         request.data.auth_crap.nt_resp_len = 24;
511
512         result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
513
514         /* Display response */
515
516         d_printf("challenge/response password authentication %s\n", 
517                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
518
519         if (response.data.auth.nt_status)
520                 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n", 
521                          response.data.auth.nt_status_string, 
522                          response.data.auth.nt_status,
523                          response.data.auth.error_string);
524
525         return result == NSS_STATUS_SUCCESS;
526 }
527
528 /******************************************************************
529  create a winbindd user
530 ******************************************************************/
531
532 static BOOL wbinfo_create_user(char *username)
533 {
534         struct winbindd_request request;
535         struct winbindd_response response;
536         NSS_STATUS result;
537
538         /* Send off request */
539
540         ZERO_STRUCT(request);
541         ZERO_STRUCT(response);
542
543         request.flags = WBFLAG_ALLOCATE_RID;
544         fstrcpy(request.data.acct_mgt.username, username);
545
546         result = winbindd_request(WINBINDD_CREATE_USER, &request, &response);
547         
548         if ( result == NSS_STATUS_SUCCESS )
549                 d_printf("New RID is %d\n", response.data.rid);
550         
551         return result == NSS_STATUS_SUCCESS;
552 }
553
554 /******************************************************************
555  remove a winbindd user
556 ******************************************************************/
557
558 static BOOL wbinfo_delete_user(char *username)
559 {
560         struct winbindd_request request;
561         struct winbindd_response response;
562         NSS_STATUS result;
563
564         /* Send off request */
565
566         ZERO_STRUCT(request);
567         ZERO_STRUCT(response);
568
569         fstrcpy(request.data.acct_mgt.username, username);
570
571         result = winbindd_request(WINBINDD_DELETE_USER, &request, &response);
572         
573         return result == NSS_STATUS_SUCCESS;
574 }
575
576 /******************************************************************
577  create a winbindd group
578 ******************************************************************/
579
580 static BOOL wbinfo_create_group(char *groupname)
581 {
582         struct winbindd_request request;
583         struct winbindd_response response;
584         NSS_STATUS result;
585
586         /* Send off request */
587
588         ZERO_STRUCT(request);
589         ZERO_STRUCT(response);
590
591         fstrcpy(request.data.acct_mgt.groupname, groupname);
592
593         result = winbindd_request(WINBINDD_CREATE_GROUP, &request, &response);
594         
595         return result == NSS_STATUS_SUCCESS;
596 }
597
598 /******************************************************************
599  remove a winbindd group
600 ******************************************************************/
601
602 static BOOL wbinfo_delete_group(char *groupname)
603 {
604         struct winbindd_request request;
605         struct winbindd_response response;
606         NSS_STATUS result;
607
608         /* Send off request */
609
610         ZERO_STRUCT(request);
611         ZERO_STRUCT(response);
612
613         fstrcpy(request.data.acct_mgt.groupname, groupname);
614
615         result = winbindd_request(WINBINDD_DELETE_GROUP, &request, &response);
616         
617         return result == NSS_STATUS_SUCCESS;
618 }
619
620 /******************************************************************
621  parse a string in the form user:group
622 ******************************************************************/
623
624 static BOOL parse_user_group( const char *string, fstring user, fstring group )
625 {
626         char *p;
627         
628         if ( !string )
629                 return False;
630         
631         if ( !(p = strchr( string, ':' )) )
632                 return False;
633                 
634         *p = '\0';
635         p++;
636         
637         fstrcpy( user, string );
638         fstrcpy( group, p );
639         
640         return True;
641 }
642
643 /******************************************************************
644  add a user to a winbindd group
645 ******************************************************************/
646
647 static BOOL wbinfo_add_user_to_group(char *string)
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         if ( !parse_user_group( string, request.data.acct_mgt.username,
659                 request.data.acct_mgt.groupname))
660         {
661                 d_printf("Can't parse user:group from %s\n", string);
662                 return False;
663         }
664
665         result = winbindd_request(WINBINDD_ADD_USER_TO_GROUP, &request, &response);
666         
667         return result == NSS_STATUS_SUCCESS;
668 }
669
670 /******************************************************************
671  remove a user from a winbindd group
672 ******************************************************************/
673
674 static BOOL wbinfo_remove_user_from_group(char *string)
675 {
676         struct winbindd_request request;
677         struct winbindd_response response;
678         NSS_STATUS result;
679
680         /* Send off request */
681
682         ZERO_STRUCT(request);
683         ZERO_STRUCT(response);
684
685         if ( !parse_user_group( string, request.data.acct_mgt.username,
686                 request.data.acct_mgt.groupname))
687         {
688                 d_printf("Can't parse user:group from %s\n", string);
689                 return False;
690         }
691
692         result = winbindd_request(WINBINDD_REMOVE_USER_FROM_GROUP, &request, &response);
693         
694         return result == NSS_STATUS_SUCCESS;
695 }
696
697 /* Print domain users */
698
699 static BOOL print_domain_users(const char *domain)
700 {
701         struct winbindd_request request;
702         struct winbindd_response response;
703         const char *extra_data;
704         fstring name;
705
706         /* Send request to winbind daemon */
707
708         ZERO_STRUCT(request);
709         ZERO_STRUCT(response);
710         
711         if (domain) {
712                 /* '.' is the special sign for our own domwin */
713                 if ( strequal(domain, ".") )
714                         fstrcpy( request.domain_name, lp_workgroup() );
715                 else
716                         fstrcpy( request.domain_name, domain );
717         }
718
719         if (winbindd_request(WINBINDD_LIST_USERS, &request, &response) !=
720             NSS_STATUS_SUCCESS)
721                 return False;
722
723         /* Look through extra data */
724
725         if (!response.extra_data)
726                 return False;
727
728         extra_data = (const char *)response.extra_data;
729
730         while(next_token(&extra_data, name, ",", sizeof(fstring)))
731                 d_printf("%s\n", name);
732         
733         SAFE_FREE(response.extra_data);
734
735         return True;
736 }
737
738 /* Print domain groups */
739
740 static BOOL print_domain_groups(const char *domain)
741 {
742         struct winbindd_request  request;
743         struct winbindd_response response;
744         const char *extra_data;
745         fstring name;
746
747         ZERO_STRUCT(request);
748         ZERO_STRUCT(response);
749
750         if (domain) {
751                 if ( strequal(domain, ".") )
752                         fstrcpy( request.domain_name, lp_workgroup() );
753                 else
754                         fstrcpy( request.domain_name, domain );
755         }
756
757         if (winbindd_request(WINBINDD_LIST_GROUPS, &request, &response) !=
758             NSS_STATUS_SUCCESS)
759                 return False;
760
761         /* Look through extra data */
762
763         if (!response.extra_data)
764                 return False;
765
766         extra_data = (const char *)response.extra_data;
767
768         while(next_token(&extra_data, name, ",", sizeof(fstring)))
769                 d_printf("%s\n", name);
770
771         SAFE_FREE(response.extra_data);
772         
773         return True;
774 }
775
776 /* Set the authorised user for winbindd access in secrets.tdb */
777
778 static BOOL wbinfo_set_auth_user(char *username)
779 {
780         char *password;
781         fstring user, domain;
782
783         /* Separate into user and password */
784
785         parse_wbinfo_domain_user(username, domain, user);
786
787         password = strchr(user, '%');
788
789         if (password) {
790                 *password = 0;
791                 password++;
792         } else
793                 password = "";
794
795         /* Store or remove DOMAIN\username%password in secrets.tdb */
796
797         secrets_init();
798
799         if (user[0]) {
800
801                 if (!secrets_store(SECRETS_AUTH_USER, user,
802                                    strlen(user) + 1)) {
803                         d_fprintf(stderr, "error storing username\n");
804                         return False;
805                 }
806
807                 /* We always have a domain name added by the
808                    parse_wbinfo_domain_user() function. */
809
810                 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
811                                    strlen(domain) + 1)) {
812                         d_fprintf(stderr, "error storing domain name\n");
813                         return False;
814                 }
815
816         } else {
817                 secrets_delete(SECRETS_AUTH_USER);
818                 secrets_delete(SECRETS_AUTH_DOMAIN);
819         }
820
821         if (password[0]) {
822
823                 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
824                                    strlen(password) + 1)) {
825                         d_fprintf(stderr, "error storing password\n");
826                         return False;
827                 }
828
829         } else
830                 secrets_delete(SECRETS_AUTH_PASSWORD);
831
832         return True;
833 }
834
835 static void wbinfo_get_auth_user(void)
836 {
837         char *user, *domain, *password;
838
839         /* Lift data from secrets file */
840
841         secrets_init();
842
843         user = secrets_fetch(SECRETS_AUTH_USER, NULL);
844         domain = secrets_fetch(SECRETS_AUTH_DOMAIN, NULL);
845         password = secrets_fetch(SECRETS_AUTH_PASSWORD, NULL);
846
847         if (!user && !domain && !password) {
848                 d_printf("No authorised user configured\n");
849                 return;
850         }
851
852         /* Pretty print authorised user info */
853
854         d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? "\\" : "",
855                  user, password ? "%" : "", password ? password : "");
856
857         SAFE_FREE(user);
858         SAFE_FREE(domain);
859         SAFE_FREE(password);
860 }
861
862 static BOOL wbinfo_ping(void)
863 {
864         NSS_STATUS result;
865
866         result = winbindd_request(WINBINDD_PING, NULL, NULL);
867
868         /* Display response */
869
870         d_printf("Ping to winbindd %s on fd %d\n", 
871                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
872
873         return result == NSS_STATUS_SUCCESS;
874 }
875
876 /* Main program */
877
878 enum {
879         OPT_SET_AUTH_USER = 1000,
880         OPT_GET_AUTH_USER,
881         OPT_DOMAIN_NAME,
882         OPT_SEQUENCE
883 };
884
885 int main(int argc, char **argv)
886 {
887         int opt;
888
889         poptContext pc;
890         static char *string_arg;
891         static char *opt_domain_name;
892         static int int_arg;
893         int result = 1;
894
895         struct poptOption long_options[] = {
896                 POPT_AUTOHELP
897
898                 /* longName, shortName, argInfo, argPtr, value, descrip, 
899                    argDesc */
900
901                 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
902                 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
903                 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
904                 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
905                 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
906                 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
907                 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
908                 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
909                 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
910                 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
911                 { "create-user", 'c', POPT_ARG_STRING, &string_arg, 'c', "Create a local user account", "name" },
912                 { "delete-user", 'x', POPT_ARG_STRING, &string_arg, 'x', "Delete a local user account", "name" },
913                 { "create-group", 'C', POPT_ARG_STRING, &string_arg, 'C', "Create a local group", "name" },
914                 { "delete-group", 'X', POPT_ARG_STRING, &string_arg, 'X', "Delete a local group", "name" },
915                 { "add-to-group", 'o', POPT_ARG_STRING, &string_arg, 'o', "Add user to group", "user:group" },
916                 { "del-from-group", 'O', POPT_ARG_STRING, &string_arg, 'O', "Remove user from group", "user:group" },
917                 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
918                 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
919                 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
920                 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
921                 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
922                 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
923                 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
924                 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
925                 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operatio", "domain" },
926                 POPT_COMMON_VERSION
927                 POPT_TABLEEND
928         };
929
930         /* Samba client initialisation */
931
932         if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
933                 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
934                         dyn_CONFIGFILE, strerror(errno));
935                 exit(1);
936         }
937
938         if (!init_names())
939                 return 1;
940
941         load_interfaces();
942
943         /* Parse options */
944
945         pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
946
947         /* Parse command line options */
948
949         if (argc == 1) {
950                 poptPrintHelp(pc, stderr, 0);
951                 return 1;
952         }
953
954         while((opt = poptGetNextOpt(pc)) != -1) {
955                 /* get the generic configuration parameters like --domain */
956         }
957
958         poptFreeContext(pc);
959
960         pc = poptGetContext(NULL, argc, (const char **)argv, long_options, 
961                             POPT_CONTEXT_KEEP_FIRST);
962
963         while((opt = poptGetNextOpt(pc)) != -1) {
964                 switch (opt) {
965                 case 'u':
966                         if (!print_domain_users(opt_domain_name)) {
967                                 d_printf("Error looking up domain users\n");
968                                 goto done;
969                         }
970                         break;
971                 case 'g':
972                         if (!print_domain_groups(opt_domain_name)) {
973                                 d_printf("Error looking up domain groups\n");
974                                 goto done;
975                         }
976                         break;
977                 case 's':
978                         if (!wbinfo_lookupsid(string_arg)) {
979                                 d_printf("Could not lookup sid %s\n", string_arg);
980                                 goto done;
981                         }
982                         break;
983                 case 'n':
984                         if (!wbinfo_lookupname(string_arg)) {
985                                 d_printf("Could not lookup name %s\n", string_arg);
986                                 goto done;
987                         }
988                         break;
989                 case 'N':
990                         if (!wbinfo_wins_byname(string_arg)) {
991                                 d_printf("Could not lookup WINS by name %s\n", string_arg);
992                                 goto done;
993                         }
994                         break;
995                 case 'I':
996                         if (!wbinfo_wins_byip(string_arg)) {
997                                 d_printf("Could not lookup WINS by IP %s\n", string_arg);
998                                 goto done;
999                         }
1000                         break;
1001                 case 'U':
1002                         if (!wbinfo_uid_to_sid(int_arg)) {
1003                                 d_printf("Could not convert uid %d to sid\n", int_arg);
1004                                 goto done;
1005                         }
1006                         break;
1007                 case 'G':
1008                         if (!wbinfo_gid_to_sid(int_arg)) {
1009                                 d_printf("Could not convert gid %d to sid\n",
1010                                        int_arg);
1011                                 goto done;
1012                         }
1013                         break;
1014                 case 'S':
1015                         if (!wbinfo_sid_to_uid(string_arg)) {
1016                                 d_printf("Could not convert sid %s to uid\n",
1017                                        string_arg);
1018                                 goto done;
1019                         }
1020                         break;
1021                 case 'Y':
1022                         if (!wbinfo_sid_to_gid(string_arg)) {
1023                                 d_printf("Could not convert sid %s to gid\n",
1024                                        string_arg);
1025                                 goto done;
1026                         }
1027                         break;
1028                 case 't':
1029                         if (!wbinfo_check_secret()) {
1030                                 d_printf("Could not check secret\n");
1031                                 goto done;
1032                         }
1033                         break;
1034                 case 'm':
1035                         if (!wbinfo_list_domains()) {
1036                                 d_printf("Could not list trusted domains\n");
1037                                 goto done;
1038                         }
1039                         break;
1040                 case OPT_SEQUENCE:
1041                         if (!wbinfo_show_sequence(opt_domain_name)) {
1042                                 d_printf("Could not show sequence numbers\n");
1043                                 goto done;
1044                         }
1045                         break;
1046                 case 'r':
1047                         if (!wbinfo_get_usergroups(string_arg)) {
1048                                 d_printf("Could not get groups for user %s\n", 
1049                                        string_arg);
1050                                 goto done;
1051                         }
1052                         break;
1053                 case 'a': {
1054                                 BOOL got_error = False;
1055
1056                                 if (!wbinfo_auth(string_arg)) {
1057                                         d_printf("Could not authenticate user %s with "
1058                                                 "plaintext password\n", string_arg);
1059                                         got_error = True;
1060                                 }
1061
1062                                 if (!wbinfo_auth_crap(string_arg)) {
1063                                         d_printf("Could not authenticate user %s with "
1064                                                 "challenge/response\n", string_arg);
1065                                         got_error = True;
1066                                 }
1067
1068                                 if (got_error)
1069                                         goto done;
1070                                 break;
1071                         }
1072                 case 'c':
1073                         if ( !wbinfo_create_user(string_arg) ) {
1074                                 d_printf("Could not create user account\n");
1075                                 goto done;
1076                         }
1077                         break;
1078                 case 'C':
1079                         if ( !wbinfo_create_group(string_arg) ) {
1080                                 d_printf("Could not create group\n");
1081                                 goto done;
1082                         }
1083                         break;
1084                 case 'o':
1085                         if ( !wbinfo_add_user_to_group(string_arg) ) {
1086                                 d_printf("Could not add user to group\n");
1087                                 goto done;
1088                         }
1089                         break;
1090                 case 'O':
1091                         if ( !wbinfo_remove_user_from_group(string_arg) ) {
1092                                 d_printf("Could not remove user kfrom group\n");
1093                                 goto done;
1094                         }
1095                         break;
1096                 case 'x':
1097                         if ( !wbinfo_delete_user(string_arg) ) {
1098                                 d_printf("Could not delete user account\n");
1099                                 goto done;
1100                         }
1101                         break;
1102                 case 'X':
1103                         if ( !wbinfo_delete_group(string_arg) ) {
1104                                 d_printf("Could not delete group\n");
1105                                 goto done;
1106                         }
1107                         break;
1108                 case 'p':
1109                         if (!wbinfo_ping()) {
1110                                 d_printf("could not ping winbindd!\n");
1111                                 goto done;
1112                         }
1113                         break;
1114                 case OPT_SET_AUTH_USER:
1115                         wbinfo_set_auth_user(string_arg);
1116                         break;
1117                 case OPT_GET_AUTH_USER:
1118                         wbinfo_get_auth_user();
1119                         break;
1120                 /* generic configuration options */
1121                 case OPT_DOMAIN_NAME:
1122                         break;
1123                 default:
1124                         d_fprintf(stderr, "Invalid option\n");
1125                         poptPrintHelp(pc, stderr, 0);
1126                         goto done;
1127                 }
1128         }
1129
1130         result = 0;
1131
1132         /* Exit code */
1133
1134  done:
1135         poptFreeContext(pc);
1136         return result;
1137 }