r7130: remove 'winbind enable local accounts' code from the 3.0 tree
[tprouty/samba.git] / source / nsswitch / wbinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind status program.
5
6    Copyright (C) Tim Potter      2000-2003
7    Copyright (C) Andrew Bartlett 2002
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "winbindd.h"
26 #include "debug.h"
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_WINBIND
30
31 extern int winbindd_fd;
32
33 static char winbind_separator(void)
34 {
35         struct winbindd_response response;
36         static BOOL got_sep;
37         static char sep;
38
39         if (got_sep)
40                 return sep;
41
42         ZERO_STRUCT(response);
43
44         /* Send off request */
45
46         if (winbindd_request(WINBINDD_INFO, NULL, &response) !=
47             NSS_STATUS_SUCCESS) {
48                 d_printf("could not obtain winbind separator!\n");
49                 /* HACK: (this module should not call lp_ funtions) */
50                 return *lp_winbind_separator();
51         }
52
53         sep = response.data.info.winbind_separator;
54         got_sep = True;
55
56         if (!sep) {
57                 d_printf("winbind separator was NULL!\n");
58                 /* HACK: (this module should not call lp_ funtions) */
59                 sep = *lp_winbind_separator();
60         }
61         
62         return sep;
63 }
64
65 static const char *get_winbind_domain(void)
66 {
67         struct winbindd_response response;
68         static fstring winbind_domain;
69
70         ZERO_STRUCT(response);
71
72         /* Send off request */
73
74         if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
75             NSS_STATUS_SUCCESS) {
76                 d_printf("could not obtain winbind domain name!\n");
77                 
78                 /* HACK: (this module should not call lp_ funtions) */
79                 return lp_workgroup();
80         }
81
82         fstrcpy(winbind_domain, response.data.domain_name);
83
84         return winbind_domain;
85
86 }
87
88 /* Copy of parse_domain_user from winbindd_util.c.  Parse a string of the
89    form DOMAIN/user into a domain and a user */
90
91 static BOOL parse_wbinfo_domain_user(const char *domuser, fstring domain, 
92                                      fstring user)
93 {
94
95         char *p = strchr(domuser,winbind_separator());
96
97         if (!p) {
98                 fstrcpy(user, domuser);
99                 fstrcpy(domain, get_winbind_domain());
100                 return True;
101         }
102         
103         fstrcpy(user, p+1);
104         fstrcpy(domain, domuser);
105         domain[PTR_DIFF(p, domuser)] = 0;
106         strupper_m(domain);
107
108         return True;
109 }
110
111 /* List groups a user is a member of */
112
113 static BOOL wbinfo_get_usergroups(char *user)
114 {
115         struct winbindd_request request;
116         struct winbindd_response response;
117         NSS_STATUS result;
118         int i;
119         
120         ZERO_STRUCT(response);
121
122         /* Send request */
123
124         fstrcpy(request.data.username, user);
125
126         result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
127
128         if (result != NSS_STATUS_SUCCESS)
129                 return False;
130
131         for (i = 0; i < response.data.num_entries; i++)
132                 d_printf("%d\n", (int)((gid_t *)response.extra_data)[i]);
133
134         SAFE_FREE(response.extra_data);
135
136         return True;
137 }
138
139
140 /* List group SIDs a user SID is a member of */
141 static BOOL wbinfo_get_usersids(char *user_sid)
142 {
143         struct winbindd_request request;
144         struct winbindd_response response;
145         NSS_STATUS result;
146         int i;
147         const char *s;
148
149         ZERO_STRUCT(response);
150
151         /* Send request */
152         fstrcpy(request.data.sid, user_sid);
153
154         result = winbindd_request(WINBINDD_GETUSERSIDS, &request, &response);
155
156         if (result != NSS_STATUS_SUCCESS)
157                 return False;
158
159         s = response.extra_data;
160         for (i = 0; i < response.data.num_entries; i++) {
161                 d_printf("%s\n", s);
162                 s += strlen(s) + 1;
163         }
164
165         SAFE_FREE(response.extra_data);
166
167         return True;
168 }
169
170 /* Convert NetBIOS name to IP */
171
172 static BOOL wbinfo_wins_byname(char *name)
173 {
174         struct winbindd_request request;
175         struct winbindd_response response;
176
177         ZERO_STRUCT(request);
178         ZERO_STRUCT(response);
179
180         /* Send request */
181
182         fstrcpy(request.data.winsreq, name);
183
184         if (winbindd_request(WINBINDD_WINS_BYNAME, &request, &response) !=
185             NSS_STATUS_SUCCESS) {
186                 return False;
187         }
188
189         /* Display response */
190
191         printf("%s\n", response.data.winsresp);
192
193         return True;
194 }
195
196 /* Convert IP to NetBIOS name */
197
198 static BOOL wbinfo_wins_byip(char *ip)
199 {
200         struct winbindd_request request;
201         struct winbindd_response response;
202
203         ZERO_STRUCT(request);
204         ZERO_STRUCT(response);
205
206         /* Send request */
207
208         fstrcpy(request.data.winsreq, ip);
209
210         if (winbindd_request(WINBINDD_WINS_BYIP, &request, &response) !=
211             NSS_STATUS_SUCCESS) {
212                 return False;
213         }
214
215         /* Display response */
216
217         printf("%s\n", response.data.winsresp);
218
219         return True;
220 }
221
222 /* List trusted domains */
223
224 static BOOL wbinfo_list_domains(void)
225 {
226         struct winbindd_response response;
227         fstring name;
228
229         ZERO_STRUCT(response);
230
231         /* Send request */
232
233         if (winbindd_request(WINBINDD_LIST_TRUSTDOM, NULL, &response) !=
234             NSS_STATUS_SUCCESS)
235                 return False;
236
237         /* Display response */
238
239         if (response.extra_data) {
240                 const char *extra_data = (char *)response.extra_data;
241
242                 while(next_token(&extra_data, name, ",", sizeof(fstring)))
243                         d_printf("%s\n", name);
244
245                 SAFE_FREE(response.extra_data);
246         }
247
248         return True;
249 }
250
251
252 /* show sequence numbers */
253 static BOOL wbinfo_show_sequence(const char *domain)
254 {
255         struct winbindd_request  request;
256         struct winbindd_response response;
257
258         ZERO_STRUCT(response);
259         ZERO_STRUCT(request);
260
261         if ( domain )
262                 fstrcpy( request.domain_name, domain );
263
264         /* Send request */
265
266         if (winbindd_request(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
267             NSS_STATUS_SUCCESS)
268                 return False;
269
270         /* Display response */
271
272         if (response.extra_data) {
273                 char *extra_data = (char *)response.extra_data;
274                 d_printf("%s", extra_data);
275                 SAFE_FREE(response.extra_data);
276         }
277
278         return True;
279 }
280
281 /* Show domain info */
282
283 static BOOL wbinfo_domain_info(const char *domain_name)
284 {
285         struct winbindd_request request;
286         struct winbindd_response response;
287
288         ZERO_STRUCT(request);
289         ZERO_STRUCT(response);
290
291         fstrcpy(request.domain_name, domain_name);
292
293         /* Send request */
294
295         if (winbindd_request(WINBINDD_DOMAIN_INFO, &request, &response) !=
296             NSS_STATUS_SUCCESS)
297                 return False;
298
299         /* Display response */
300
301         d_printf("Name              : %s\n", response.data.domain_info.name);
302         d_printf("Alt_Name          : %s\n", response.data.domain_info.alt_name);
303
304         d_printf("SID               : %s\n", response.data.domain_info.sid);
305
306         d_printf("Active Directory  : %s\n",
307                  response.data.domain_info.active_directory ? "Yes" : "No");
308         d_printf("Native            : %s\n",
309                  response.data.domain_info.native_mode ? "Yes" : "No");
310
311         d_printf("Primary           : %s\n",
312                  response.data.domain_info.primary ? "Yes" : "No");
313
314         d_printf("Sequence          : %d\n", response.data.domain_info.sequence_number);
315
316         return True;
317 }
318
319 /* Check trust account password */
320
321 static BOOL wbinfo_check_secret(void)
322 {
323         struct winbindd_response response;
324         NSS_STATUS result;
325
326         ZERO_STRUCT(response);
327
328         result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response);
329                 
330         d_printf("checking the trust secret via RPC calls %s\n", 
331                  (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
332
333         if (result != NSS_STATUS_SUCCESS)       
334                 d_printf("error code was %s (0x%x)\n", 
335                          response.data.auth.nt_status_string, 
336                          response.data.auth.nt_status);
337         
338         return result == NSS_STATUS_SUCCESS;    
339 }
340
341 /* Convert uid to sid */
342
343 static BOOL wbinfo_uid_to_sid(uid_t uid)
344 {
345         struct winbindd_request request;
346         struct winbindd_response response;
347
348         ZERO_STRUCT(request);
349         ZERO_STRUCT(response);
350
351         /* Send request */
352
353         request.data.uid = uid;
354
355         if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
356             NSS_STATUS_SUCCESS)
357                 return False;
358
359         /* Display response */
360
361         d_printf("%s\n", response.data.sid.sid);
362
363         return True;
364 }
365
366 /* Convert gid to sid */
367
368 static BOOL wbinfo_gid_to_sid(gid_t gid)
369 {
370         struct winbindd_request request;
371         struct winbindd_response response;
372
373         ZERO_STRUCT(request);
374         ZERO_STRUCT(response);
375
376         /* Send request */
377
378         request.data.gid = gid;
379
380         if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) !=
381             NSS_STATUS_SUCCESS)
382                 return False;
383
384         /* Display response */
385
386         d_printf("%s\n", response.data.sid.sid);
387
388         return True;
389 }
390
391 /* Convert sid to uid */
392
393 static BOOL wbinfo_sid_to_uid(char *sid)
394 {
395         struct winbindd_request request;
396         struct winbindd_response response;
397
398         ZERO_STRUCT(request);
399         ZERO_STRUCT(response);
400
401         /* Send request */
402
403         fstrcpy(request.data.sid, sid);
404
405         if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
406             NSS_STATUS_SUCCESS)
407                 return False;
408
409         /* Display response */
410
411         d_printf("%d\n", (int)response.data.uid);
412
413         return True;
414 }
415
416 static BOOL wbinfo_sid_to_gid(char *sid)
417 {
418         struct winbindd_request request;
419         struct winbindd_response response;
420
421         ZERO_STRUCT(request);
422         ZERO_STRUCT(response);
423
424         /* Send request */
425
426         fstrcpy(request.data.sid, sid);
427
428         if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
429             NSS_STATUS_SUCCESS)
430                 return False;
431
432         /* Display response */
433
434         d_printf("%d\n", (int)response.data.gid);
435
436         return True;
437 }
438
439 static BOOL wbinfo_allocate_rid(void)
440 {
441         uint32 rid;
442
443         if (!winbind_allocate_rid(&rid))
444                 return False;
445
446         d_printf("New rid: %d\n", rid);
447
448         return True;
449 }
450
451 /* Convert sid to string */
452
453 static BOOL wbinfo_lookupsid(char *sid)
454 {
455         struct winbindd_request request;
456         struct winbindd_response response;
457
458         ZERO_STRUCT(request);
459         ZERO_STRUCT(response);
460
461         /* Send off request */
462
463         fstrcpy(request.data.sid, sid);
464
465         if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
466             NSS_STATUS_SUCCESS)
467                 return False;
468
469         /* Display response */
470
471         d_printf("%s%c%s %d\n", response.data.name.dom_name, 
472                  winbind_separator(), response.data.name.name, 
473                  response.data.name.type);
474
475         return True;
476 }
477
478 /* Convert string to sid */
479
480 static BOOL wbinfo_lookupname(char *name)
481 {
482         struct winbindd_request request;
483         struct winbindd_response response;
484
485         /* Send off request */
486
487         ZERO_STRUCT(request);
488         ZERO_STRUCT(response);
489
490         parse_wbinfo_domain_user(name, request.data.name.dom_name, 
491                                  request.data.name.name);
492
493         if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
494             NSS_STATUS_SUCCESS)
495                 return False;
496
497         /* Display response */
498
499         d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
500
501         return True;
502 }
503
504 /* Authenticate a user with a plaintext password */
505
506 static BOOL wbinfo_auth(char *username)
507 {
508         struct winbindd_request request;
509         struct winbindd_response response;
510         NSS_STATUS result;
511         char *p;
512
513         /* Send off request */
514
515         ZERO_STRUCT(request);
516         ZERO_STRUCT(response);
517
518         p = strchr(username, '%');
519
520         if (p) {
521                 *p = 0;
522                 fstrcpy(request.data.auth.user, username);
523                 fstrcpy(request.data.auth.pass, p + 1);
524                 *p = '%';
525         } else
526                 fstrcpy(request.data.auth.user, username);
527
528         result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
529
530         /* Display response */
531
532         d_printf("plaintext password authentication %s\n", 
533                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
534
535         if (response.data.auth.nt_status)
536                 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n", 
537                          response.data.auth.nt_status_string, 
538                          response.data.auth.nt_status,
539                          response.data.auth.error_string);
540
541         return result == NSS_STATUS_SUCCESS;
542 }
543
544 /* Authenticate a user with a challenge/response */
545
546 static BOOL wbinfo_auth_crap(char *username)
547 {
548         struct winbindd_request request;
549         struct winbindd_response response;
550         NSS_STATUS result;
551         fstring name_user;
552         fstring name_domain;
553         fstring pass;
554         char *p;
555
556         /* Send off request */
557
558         ZERO_STRUCT(request);
559         ZERO_STRUCT(response);
560
561         p = strchr(username, '%');
562
563         if (p) {
564                 *p = 0;
565                 fstrcpy(pass, p + 1);
566         }
567                 
568         parse_wbinfo_domain_user(username, name_domain, name_user);
569
570         fstrcpy(request.data.auth_crap.user, name_user);
571
572         fstrcpy(request.data.auth_crap.domain, 
573                               name_domain);
574
575         generate_random_buffer(request.data.auth_crap.chal, 8);
576         
577         if (lp_client_ntlmv2_auth()) {
578                 DATA_BLOB server_chal;
579                 DATA_BLOB names_blob;   
580
581                 DATA_BLOB lm_response;
582                 DATA_BLOB nt_response;
583
584                 server_chal = data_blob(request.data.auth_crap.chal, 8); 
585                 
586                 /* Pretend this is a login to 'us', for blob purposes */
587                 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
588                 
589                 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal, 
590                                       &names_blob,
591                                       &lm_response, &nt_response, NULL)) {
592                         data_blob_free(&names_blob);
593                         data_blob_free(&server_chal);
594                         return False;
595                 }
596                 data_blob_free(&names_blob);
597                 data_blob_free(&server_chal);
598
599                 memcpy(request.data.auth_crap.nt_resp, nt_response.data, 
600                        MIN(nt_response.length, 
601                            sizeof(request.data.auth_crap.nt_resp)));
602                 request.data.auth_crap.nt_resp_len = nt_response.length;
603
604                 memcpy(request.data.auth_crap.lm_resp, lm_response.data, 
605                        MIN(lm_response.length, 
606                            sizeof(request.data.auth_crap.lm_resp)));
607                 request.data.auth_crap.lm_resp_len = lm_response.length;
608                        
609                 data_blob_free(&nt_response);
610                 data_blob_free(&lm_response);
611
612         } else {
613                 if (lp_client_lanman_auth() 
614                     && SMBencrypt(pass, request.data.auth_crap.chal, 
615                                (uchar *)request.data.auth_crap.lm_resp)) {
616                         request.data.auth_crap.lm_resp_len = 24;
617                 } else {
618                         request.data.auth_crap.lm_resp_len = 0;
619                 }
620                 SMBNTencrypt(pass, request.data.auth_crap.chal,
621                              (uchar *)request.data.auth_crap.nt_resp);
622
623                 request.data.auth_crap.nt_resp_len = 24;
624         }
625
626         result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
627
628         /* Display response */
629
630         d_printf("challenge/response password authentication %s\n", 
631                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
632
633         if (response.data.auth.nt_status)
634                 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n", 
635                          response.data.auth.nt_status_string, 
636                          response.data.auth.nt_status,
637                          response.data.auth.error_string);
638
639         return result == NSS_STATUS_SUCCESS;
640 }
641
642 /* Authenticate a user with a plaintext password and set a token */
643
644 static BOOL wbinfo_klog(char *username)
645 {
646         struct winbindd_request request;
647         struct winbindd_response response;
648         NSS_STATUS result;
649         char *p;
650
651         /* Send off request */
652
653         ZERO_STRUCT(request);
654         ZERO_STRUCT(response);
655
656         p = strchr(username, '%');
657
658         if (p) {
659                 *p = 0;
660                 fstrcpy(request.data.auth.user, username);
661                 fstrcpy(request.data.auth.pass, p + 1);
662                 *p = '%';
663         } else {
664                 fstrcpy(request.data.auth.user, username);
665                 fstrcpy(request.data.auth.pass, getpass("Password: "));
666         }
667
668         request.flags |= WBFLAG_PAM_AFS_TOKEN;
669
670         result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
671
672         /* Display response */
673
674         d_printf("plaintext password authentication %s\n", 
675                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
676
677         if (response.data.auth.nt_status)
678                 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n", 
679                          response.data.auth.nt_status_string, 
680                          response.data.auth.nt_status,
681                          response.data.auth.error_string);
682
683         if (result != NSS_STATUS_SUCCESS)
684                 return False;
685
686         if (response.extra_data == NULL) {
687                 d_printf("Did not get token data\n");
688                 return False;
689         }
690
691         if (!afs_settoken_str((char *)response.extra_data)) {
692                 d_printf("Could not set token\n");
693                 return False;
694         }
695
696         d_printf("Successfully created AFS token\n");
697         return True;
698 }
699
700 /* Print domain users */
701
702 static BOOL print_domain_users(const char *domain)
703 {
704         struct winbindd_request request;
705         struct winbindd_response response;
706         const char *extra_data;
707         fstring name;
708
709         /* Send request to winbind daemon */
710
711         ZERO_STRUCT(request);
712         ZERO_STRUCT(response);
713         
714         if (domain) {
715                 /* '.' is the special sign for our own domwin */
716                 if ( strequal(domain, ".") )
717                         fstrcpy( request.domain_name, lp_workgroup() );
718                 else
719                         fstrcpy( request.domain_name, domain );
720         }
721
722         if (winbindd_request(WINBINDD_LIST_USERS, &request, &response) !=
723             NSS_STATUS_SUCCESS)
724                 return False;
725
726         /* Look through extra data */
727
728         if (!response.extra_data)
729                 return False;
730
731         extra_data = (const char *)response.extra_data;
732
733         while(next_token(&extra_data, name, ",", sizeof(fstring)))
734                 d_printf("%s\n", name);
735         
736         SAFE_FREE(response.extra_data);
737
738         return True;
739 }
740
741 /* Print domain groups */
742
743 static BOOL print_domain_groups(const char *domain)
744 {
745         struct winbindd_request  request;
746         struct winbindd_response response;
747         const char *extra_data;
748         fstring name;
749
750         ZERO_STRUCT(request);
751         ZERO_STRUCT(response);
752
753         if (domain) {
754                 if ( strequal(domain, ".") )
755                         fstrcpy( request.domain_name, lp_workgroup() );
756                 else
757                         fstrcpy( request.domain_name, domain );
758         }
759
760         if (winbindd_request(WINBINDD_LIST_GROUPS, &request, &response) !=
761             NSS_STATUS_SUCCESS)
762                 return False;
763
764         /* Look through extra data */
765
766         if (!response.extra_data)
767                 return False;
768
769         extra_data = (const char *)response.extra_data;
770
771         while(next_token(&extra_data, name, ",", sizeof(fstring)))
772                 d_printf("%s\n", name);
773
774         SAFE_FREE(response.extra_data);
775         
776         return True;
777 }
778
779 /* Set the authorised user for winbindd access in secrets.tdb */
780
781 static BOOL wbinfo_set_auth_user(char *username)
782 {
783         const char *password;
784         char *p;
785         fstring user, domain;
786
787         /* Separate into user and password */
788
789         parse_wbinfo_domain_user(username, domain, user);
790
791         p = strchr(user, '%');
792
793         if (p != NULL) {
794                 *p = 0;
795                 password = p+1;
796         } else {
797                 char *thepass = getpass("Password: ");
798                 if (thepass) {
799                         password = thepass;     
800                 } else
801                         password = "";
802         }
803
804         /* Store or remove DOMAIN\username%password in secrets.tdb */
805
806         secrets_init();
807
808         if (user[0]) {
809
810                 if (!secrets_store(SECRETS_AUTH_USER, user,
811                                    strlen(user) + 1)) {
812                         d_fprintf(stderr, "error storing username\n");
813                         return False;
814                 }
815
816                 /* We always have a domain name added by the
817                    parse_wbinfo_domain_user() function. */
818
819                 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
820                                    strlen(domain) + 1)) {
821                         d_fprintf(stderr, "error storing domain name\n");
822                         return False;
823                 }
824
825         } else {
826                 secrets_delete(SECRETS_AUTH_USER);
827                 secrets_delete(SECRETS_AUTH_DOMAIN);
828         }
829
830         if (password[0]) {
831
832                 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
833                                    strlen(password) + 1)) {
834                         d_fprintf(stderr, "error storing password\n");
835                         return False;
836                 }
837
838         } else
839                 secrets_delete(SECRETS_AUTH_PASSWORD);
840
841         return True;
842 }
843
844 static void wbinfo_get_auth_user(void)
845 {
846         char *user, *domain, *password;
847
848         /* Lift data from secrets file */
849         
850         secrets_fetch_ipc_userpass(&user, &domain, &password);
851
852         if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
853
854                 SAFE_FREE(user);
855                 SAFE_FREE(domain);
856                 SAFE_FREE(password);
857                 d_printf("No authorised user configured\n");
858                 return;
859         }
860
861         /* Pretty print authorised user info */
862
863         d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
864                  user, password ? "%" : "", password ? password : "");
865
866         SAFE_FREE(user);
867         SAFE_FREE(domain);
868         SAFE_FREE(password);
869 }
870
871 static BOOL wbinfo_ping(void)
872 {
873         NSS_STATUS result;
874
875         result = winbindd_request(WINBINDD_PING, NULL, NULL);
876
877         /* Display response */
878
879         d_printf("Ping to winbindd %s on fd %d\n", 
880                (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
881
882         return result == NSS_STATUS_SUCCESS;
883 }
884
885 /* Main program */
886
887 enum {
888         OPT_SET_AUTH_USER = 1000,
889         OPT_GET_AUTH_USER,
890         OPT_DOMAIN_NAME,
891         OPT_SEQUENCE,
892         OPT_USERSIDS
893 };
894
895 int main(int argc, char **argv)
896 {
897         int opt;
898
899         poptContext pc;
900         static char *string_arg;
901         static char *opt_domain_name;
902         static int int_arg;
903         int result = 1;
904
905         struct poptOption long_options[] = {
906                 POPT_AUTOHELP
907
908                 /* longName, shortName, argInfo, argPtr, value, descrip, 
909                    argDesc */
910
911                 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
912                 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
913                 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
914                 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
915                 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
916                 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
917                 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
918                 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
919                 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
920                 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
921                 { "allocate-rid", 'A', POPT_ARG_NONE, 0, 'A', "Get a new RID out of idmap" },
922                 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
923                 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
924                 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
925                 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
926                 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
927                 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
928                 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
929                 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
930                 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
931                 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
932                 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
933 #ifdef WITH_FAKE_KASERVER
934                 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
935 #endif
936                 POPT_COMMON_VERSION
937                 POPT_TABLEEND
938         };
939
940         /* Samba client initialisation */
941
942         if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
943                 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
944                         dyn_CONFIGFILE, strerror(errno));
945                 exit(1);
946         }
947
948         if (!init_names())
949                 return 1;
950
951         load_interfaces();
952
953         /* Parse options */
954
955         pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
956
957         /* Parse command line options */
958
959         if (argc == 1) {
960                 poptPrintHelp(pc, stderr, 0);
961                 return 1;
962         }
963
964         while((opt = poptGetNextOpt(pc)) != -1) {
965                 /* get the generic configuration parameters like --domain */
966         }
967
968         poptFreeContext(pc);
969
970         pc = poptGetContext(NULL, argc, (const char **)argv, long_options, 
971                             POPT_CONTEXT_KEEP_FIRST);
972
973         while((opt = poptGetNextOpt(pc)) != -1) {
974                 switch (opt) {
975                 case 'u':
976                         if (!print_domain_users(opt_domain_name)) {
977                                 d_printf("Error looking up domain users\n");
978                                 goto done;
979                         }
980                         break;
981                 case 'g':
982                         if (!print_domain_groups(opt_domain_name)) {
983                                 d_printf("Error looking up domain groups\n");
984                                 goto done;
985                         }
986                         break;
987                 case 's':
988                         if (!wbinfo_lookupsid(string_arg)) {
989                                 d_printf("Could not lookup sid %s\n", string_arg);
990                                 goto done;
991                         }
992                         break;
993                 case 'n':
994                         if (!wbinfo_lookupname(string_arg)) {
995                                 d_printf("Could not lookup name %s\n", string_arg);
996                                 goto done;
997                         }
998                         break;
999                 case 'N':
1000                         if (!wbinfo_wins_byname(string_arg)) {
1001                                 d_printf("Could not lookup WINS by name %s\n", string_arg);
1002                                 goto done;
1003                         }
1004                         break;
1005                 case 'I':
1006                         if (!wbinfo_wins_byip(string_arg)) {
1007                                 d_printf("Could not lookup WINS by IP %s\n", string_arg);
1008                                 goto done;
1009                         }
1010                         break;
1011                 case 'U':
1012                         if (!wbinfo_uid_to_sid(int_arg)) {
1013                                 d_printf("Could not convert uid %d to sid\n", int_arg);
1014                                 goto done;
1015                         }
1016                         break;
1017                 case 'G':
1018                         if (!wbinfo_gid_to_sid(int_arg)) {
1019                                 d_printf("Could not convert gid %d to sid\n",
1020                                        int_arg);
1021                                 goto done;
1022                         }
1023                         break;
1024                 case 'S':
1025                         if (!wbinfo_sid_to_uid(string_arg)) {
1026                                 d_printf("Could not convert sid %s to uid\n",
1027                                        string_arg);
1028                                 goto done;
1029                         }
1030                         break;
1031                 case 'Y':
1032                         if (!wbinfo_sid_to_gid(string_arg)) {
1033                                 d_printf("Could not convert sid %s to gid\n",
1034                                        string_arg);
1035                                 goto done;
1036                         }
1037                         break;
1038                 case 'A':
1039                         if (!wbinfo_allocate_rid()) {
1040                                 d_printf("Could not allocate a RID\n");
1041                                 goto done;
1042                         }
1043                         break;
1044                 case 't':
1045                         if (!wbinfo_check_secret()) {
1046                                 d_printf("Could not check secret\n");
1047                                 goto done;
1048                         }
1049                         break;
1050                 case 'm':
1051                         if (!wbinfo_list_domains()) {
1052                                 d_printf("Could not list trusted domains\n");
1053                                 goto done;
1054                         }
1055                         break;
1056                 case OPT_SEQUENCE:
1057                         if (!wbinfo_show_sequence(opt_domain_name)) {
1058                                 d_printf("Could not show sequence numbers\n");
1059                                 goto done;
1060                         }
1061                         break;
1062                 case 'D':
1063                         if (!wbinfo_domain_info(string_arg)) {
1064                                 d_printf("Could not get domain info\n");
1065                                 goto done;
1066                         }
1067                         break;
1068                 case 'r':
1069                         if (!wbinfo_get_usergroups(string_arg)) {
1070                                 d_printf("Could not get groups for user %s\n", 
1071                                        string_arg);
1072                                 goto done;
1073                         }
1074                         break;
1075                 case OPT_USERSIDS:
1076                         if (!wbinfo_get_usersids(string_arg)) {
1077                                 d_printf("Could not get group SIDs for user SID %s\n", 
1078                                        string_arg);
1079                                 goto done;
1080                         }
1081                         break;
1082                 case 'a': {
1083                                 BOOL got_error = False;
1084
1085                                 if (!wbinfo_auth(string_arg)) {
1086                                         d_printf("Could not authenticate user %s with "
1087                                                 "plaintext password\n", string_arg);
1088                                         got_error = True;
1089                                 }
1090
1091                                 if (!wbinfo_auth_crap(string_arg)) {
1092                                         d_printf("Could not authenticate user %s with "
1093                                                 "challenge/response\n", string_arg);
1094                                         got_error = True;
1095                                 }
1096
1097                                 if (got_error)
1098                                         goto done;
1099                                 break;
1100                         }
1101                 case 'k':
1102                         if (!wbinfo_klog(string_arg)) {
1103                                 d_printf("Could not klog user\n");
1104                                 goto done;
1105                         }
1106                         break;
1107                 case 'p':
1108                         if (!wbinfo_ping()) {
1109                                 d_printf("could not ping winbindd!\n");
1110                                 goto done;
1111                         }
1112                         break;
1113                 case OPT_SET_AUTH_USER:
1114                         wbinfo_set_auth_user(string_arg);
1115                         break;
1116                 case OPT_GET_AUTH_USER:
1117                         wbinfo_get_auth_user();
1118                         break;
1119                 /* generic configuration options */
1120                 case OPT_DOMAIN_NAME:
1121                         break;
1122                 default:
1123                         d_fprintf(stderr, "Invalid option\n");
1124                         poptPrintHelp(pc, stderr, 0);
1125                         goto done;
1126                 }
1127         }
1128
1129         result = 0;
1130
1131         /* Exit code */
1132
1133  done:
1134         poptFreeContext(pc);
1135         return result;
1136 }