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