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