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