r15053: fix portabilities issues between 32-bit winbind clients and a 64-bit winbindd...
[Samba.git] / source / nsswitch / wbinfo.c
blobad5ef71523014bda4cbe7a30e5244150e0d80c49
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett 2002
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.
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.
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.
24 #include "includes.h"
25 #include "winbindd.h"
26 #include "debug.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_WINBIND
31 extern int winbindd_fd;
33 static char winbind_separator_int(BOOL strict)
35 struct winbindd_response response;
36 static BOOL got_sep;
37 static char sep;
39 if (got_sep)
40 return sep;
42 ZERO_STRUCT(response);
44 /* Send off request */
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;
52 /* HACK: (this module should not call lp_ funtions) */
53 return *lp_winbind_separator();
56 sep = response.data.info.winbind_separator;
57 got_sep = True;
59 if (!sep) {
60 d_fprintf(stderr, "winbind separator was NULL!\n");
61 if (strict) {
62 return 0;
64 /* HACK: (this module should not call lp_ funtions) */
65 sep = *lp_winbind_separator();
68 return sep;
71 static char winbind_separator(void)
73 return winbind_separator_int(False);
76 static const char *get_winbind_domain(void)
78 struct winbindd_response response;
79 static fstring winbind_domain;
81 ZERO_STRUCT(response);
83 /* Send off request */
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");
89 /* HACK: (this module should not call lp_ funtions) */
90 return lp_workgroup();
93 fstrcpy(winbind_domain, response.data.domain_name);
95 return winbind_domain;
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 */
102 static BOOL parse_wbinfo_domain_user(const char *domuser, fstring domain,
103 fstring user)
106 char *p = strchr(domuser,winbind_separator());
108 if (!p) {
109 fstrcpy(user, domuser);
110 fstrcpy(domain, get_winbind_domain());
111 return True;
114 fstrcpy(user, p+1);
115 fstrcpy(domain, domuser);
116 domain[PTR_DIFF(p, domuser)] = 0;
117 strupper_m(domain);
119 return True;
122 /* List groups a user is a member of */
124 static BOOL wbinfo_get_usergroups(char *user)
126 struct winbindd_request request;
127 struct winbindd_response response;
128 NSS_STATUS result;
129 int i;
131 ZERO_STRUCT(request);
132 ZERO_STRUCT(response);
134 /* Send request */
136 fstrcpy(request.data.username, user);
138 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
140 if (result != NSS_STATUS_SUCCESS)
141 return False;
143 for (i = 0; i < response.data.num_entries; i++)
144 d_printf("%d\n", (int)((gid_t *)response.extra_data.data)[i]);
146 SAFE_FREE(response.extra_data.data);
148 return True;
152 /* List group SIDs a user SID is a member of */
153 static BOOL wbinfo_get_usersids(char *user_sid)
155 struct winbindd_request request;
156 struct winbindd_response response;
157 NSS_STATUS result;
158 int i;
159 const char *s;
161 ZERO_STRUCT(request);
162 ZERO_STRUCT(response);
164 /* Send request */
165 fstrcpy(request.data.sid, user_sid);
167 result = winbindd_request_response(WINBINDD_GETUSERSIDS, &request, &response);
169 if (result != NSS_STATUS_SUCCESS)
170 return False;
172 s = response.extra_data.data;
173 for (i = 0; i < response.data.num_entries; i++) {
174 d_printf("%s\n", s);
175 s += strlen(s) + 1;
178 SAFE_FREE(response.extra_data.data);
180 return True;
183 static BOOL wbinfo_get_userdomgroups(const char *user_sid)
185 struct winbindd_request request;
186 struct winbindd_response response;
187 NSS_STATUS result;
189 ZERO_STRUCT(request);
190 ZERO_STRUCT(response);
192 /* Send request */
193 fstrcpy(request.data.sid, user_sid);
195 result = winbindd_request_response(WINBINDD_GETUSERDOMGROUPS, &request,
196 &response);
198 if (result != NSS_STATUS_SUCCESS)
199 return False;
201 if (response.data.num_entries != 0)
202 printf("%s", (char *)response.extra_data.data);
204 SAFE_FREE(response.extra_data.data);
206 return True;
209 /* Convert NetBIOS name to IP */
211 static BOOL wbinfo_wins_byname(char *name)
213 struct winbindd_request request;
214 struct winbindd_response response;
216 ZERO_STRUCT(request);
217 ZERO_STRUCT(response);
219 /* Send request */
221 fstrcpy(request.data.winsreq, name);
223 if (winbindd_request_response(WINBINDD_WINS_BYNAME, &request, &response) !=
224 NSS_STATUS_SUCCESS) {
225 return False;
228 /* Display response */
230 d_printf("%s\n", response.data.winsresp);
232 return True;
235 /* Convert IP to NetBIOS name */
237 static BOOL wbinfo_wins_byip(char *ip)
239 struct winbindd_request request;
240 struct winbindd_response response;
242 ZERO_STRUCT(request);
243 ZERO_STRUCT(response);
245 /* Send request */
247 fstrcpy(request.data.winsreq, ip);
249 if (winbindd_request_response(WINBINDD_WINS_BYIP, &request, &response) !=
250 NSS_STATUS_SUCCESS) {
251 return False;
254 /* Display response */
256 d_printf("%s\n", response.data.winsresp);
258 return True;
261 /* List trusted domains */
263 static BOOL wbinfo_list_domains(BOOL list_all_domains)
265 struct winbindd_request request;
266 struct winbindd_response response;
268 ZERO_STRUCT(request);
269 ZERO_STRUCT(response);
271 /* Send request */
273 request.data.list_all_domains = list_all_domains;
275 if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM, &request, &response) !=
276 NSS_STATUS_SUCCESS)
277 return False;
279 /* Display response */
281 if (response.extra_data.data) {
282 const char *extra_data = (char *)response.extra_data.data;
283 fstring name;
284 char *p;
286 while(next_token(&extra_data, name, "\n", sizeof(fstring))) {
287 p = strchr(name, '\\');
288 if (p == 0) {
289 d_fprintf(stderr, "Got invalid response: %s\n",
290 extra_data);
291 return False;
293 *p = 0;
294 d_printf("%s\n", name);
297 SAFE_FREE(response.extra_data.data);
300 return True;
304 /* show sequence numbers */
305 static BOOL wbinfo_show_sequence(const char *domain)
307 struct winbindd_request request;
308 struct winbindd_response response;
310 ZERO_STRUCT(response);
311 ZERO_STRUCT(request);
313 if ( domain )
314 fstrcpy( request.domain_name, domain );
316 /* Send request */
318 if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
319 NSS_STATUS_SUCCESS)
320 return False;
322 /* Display response */
324 if (response.extra_data.data) {
325 char *extra_data = (char *)response.extra_data.data;
326 d_printf("%s", extra_data);
327 SAFE_FREE(response.extra_data.data);
330 return True;
333 /* Show domain info */
335 static BOOL wbinfo_domain_info(const char *domain_name)
337 struct winbindd_request request;
338 struct winbindd_response response;
340 ZERO_STRUCT(request);
341 ZERO_STRUCT(response);
343 fstrcpy(request.domain_name, domain_name);
345 /* Send request */
347 if (winbindd_request_response(WINBINDD_DOMAIN_INFO, &request, &response) !=
348 NSS_STATUS_SUCCESS)
349 return False;
351 /* Display response */
353 d_printf("Name : %s\n", response.data.domain_info.name);
354 d_printf("Alt_Name : %s\n", response.data.domain_info.alt_name);
356 d_printf("SID : %s\n", response.data.domain_info.sid);
358 d_printf("Active Directory : %s\n",
359 response.data.domain_info.active_directory ? "Yes" : "No");
360 d_printf("Native : %s\n",
361 response.data.domain_info.native_mode ? "Yes" : "No");
363 d_printf("Primary : %s\n",
364 response.data.domain_info.primary ? "Yes" : "No");
366 d_printf("Sequence : %d\n", response.data.domain_info.sequence_number);
368 return True;
371 /* Get a foreign DC's name */
372 static BOOL wbinfo_getdcname(const char *domain_name)
374 struct winbindd_request request;
375 struct winbindd_response response;
377 ZERO_STRUCT(request);
378 ZERO_STRUCT(response);
380 fstrcpy(request.domain_name, domain_name);
382 /* Send request */
384 if (winbindd_request_response(WINBINDD_GETDCNAME, &request, &response) !=
385 NSS_STATUS_SUCCESS) {
386 d_fprintf(stderr, "Could not get dc name for %s\n", domain_name);
387 return False;
390 /* Display response */
392 d_printf("%s\n", response.data.dc_name);
394 return True;
397 /* Check trust account password */
399 static BOOL wbinfo_check_secret(void)
401 struct winbindd_response response;
402 NSS_STATUS result;
404 ZERO_STRUCT(response);
406 result = winbindd_request_response(WINBINDD_CHECK_MACHACC, NULL, &response);
408 d_printf("checking the trust secret via RPC calls %s\n",
409 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
411 if (result != NSS_STATUS_SUCCESS)
412 d_fprintf(stderr, "error code was %s (0x%x)\n",
413 response.data.auth.nt_status_string,
414 response.data.auth.nt_status);
416 return result == NSS_STATUS_SUCCESS;
419 /* Convert uid to sid */
421 static BOOL wbinfo_uid_to_sid(uid_t uid)
423 struct winbindd_request request;
424 struct winbindd_response response;
426 ZERO_STRUCT(request);
427 ZERO_STRUCT(response);
429 /* Send request */
431 request.data.uid = uid;
433 if (winbindd_request_response(WINBINDD_UID_TO_SID, &request, &response) !=
434 NSS_STATUS_SUCCESS)
435 return False;
437 /* Display response */
439 d_printf("%s\n", response.data.sid.sid);
441 return True;
444 /* Convert gid to sid */
446 static BOOL wbinfo_gid_to_sid(gid_t gid)
448 struct winbindd_request request;
449 struct winbindd_response response;
451 ZERO_STRUCT(request);
452 ZERO_STRUCT(response);
454 /* Send request */
456 request.data.gid = gid;
458 if (winbindd_request_response(WINBINDD_GID_TO_SID, &request, &response) !=
459 NSS_STATUS_SUCCESS)
460 return False;
462 /* Display response */
464 d_printf("%s\n", response.data.sid.sid);
466 return True;
469 /* Convert sid to uid */
471 static BOOL wbinfo_sid_to_uid(char *sid)
473 struct winbindd_request request;
474 struct winbindd_response response;
476 ZERO_STRUCT(request);
477 ZERO_STRUCT(response);
479 /* Send request */
481 fstrcpy(request.data.sid, sid);
483 if (winbindd_request_response(WINBINDD_SID_TO_UID, &request, &response) !=
484 NSS_STATUS_SUCCESS)
485 return False;
487 /* Display response */
489 d_printf("%d\n", (int)response.data.uid);
491 return True;
494 static BOOL wbinfo_sid_to_gid(char *sid)
496 struct winbindd_request request;
497 struct winbindd_response response;
499 ZERO_STRUCT(request);
500 ZERO_STRUCT(response);
502 /* Send request */
504 fstrcpy(request.data.sid, sid);
506 if (winbindd_request_response(WINBINDD_SID_TO_GID, &request, &response) !=
507 NSS_STATUS_SUCCESS)
508 return False;
510 /* Display response */
512 d_printf("%d\n", (int)response.data.gid);
514 return True;
517 static BOOL wbinfo_allocate_uid(void)
519 uid_t uid;
521 if (!winbind_allocate_uid(&uid))
522 return False;
524 d_printf("New uid: %d\n", uid);
526 return True;
529 static BOOL wbinfo_allocate_gid(void)
531 gid_t gid;
533 if (!winbind_allocate_gid(&gid))
534 return False;
536 d_printf("New gid: %d\n", gid);
538 return True;
541 /* Convert sid to string */
543 static BOOL wbinfo_lookupsid(char *sid)
545 struct winbindd_request request;
546 struct winbindd_response response;
548 ZERO_STRUCT(request);
549 ZERO_STRUCT(response);
551 /* Send off request */
553 fstrcpy(request.data.sid, sid);
555 if (winbindd_request_response(WINBINDD_LOOKUPSID, &request, &response) !=
556 NSS_STATUS_SUCCESS)
557 return False;
559 /* Display response */
561 d_printf("%s%c%s %d\n", response.data.name.dom_name,
562 winbind_separator(), response.data.name.name,
563 response.data.name.type);
565 return True;
568 /* Convert string to sid */
570 static BOOL wbinfo_lookupname(char *name)
572 struct winbindd_request request;
573 struct winbindd_response response;
575 /* Send off request */
577 ZERO_STRUCT(request);
578 ZERO_STRUCT(response);
580 parse_wbinfo_domain_user(name, request.data.name.dom_name,
581 request.data.name.name);
583 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
584 NSS_STATUS_SUCCESS)
585 return False;
587 /* Display response */
589 d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
591 return True;
594 /* Authenticate a user with a plaintext password */
596 static BOOL wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
598 struct winbindd_request request;
599 struct winbindd_response response;
600 NSS_STATUS result;
601 char *p;
603 /* Send off request */
605 ZERO_STRUCT(request);
606 ZERO_STRUCT(response);
608 p = strchr(username, '%');
610 if (p) {
611 *p = 0;
612 fstrcpy(request.data.auth.user, username);
613 fstrcpy(request.data.auth.pass, p + 1);
614 *p = '%';
615 } else
616 fstrcpy(request.data.auth.user, username);
618 request.flags = flags;
620 fstrcpy(request.data.auth.krb5_cc_type, cctype);
622 request.data.auth.uid = geteuid();
624 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
626 /* Display response */
628 d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n",
629 username, (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", cctype);
631 if (response.data.auth.nt_status)
632 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
633 response.data.auth.nt_status_string,
634 response.data.auth.nt_status,
635 response.data.auth.error_string);
637 if (result == NSS_STATUS_SUCCESS) {
639 if (request.flags & WBFLAG_PAM_INFO3_TEXT) {
640 if (response.data.auth.info3.user_flgs & LOGON_CACHED_ACCOUNT) {
641 d_printf("user_flgs: LOGON_CACHED_ACCOUNT\n");
645 if (response.data.auth.krb5ccname[0] != '\0') {
646 d_printf("credentials were put in: %s\n", response.data.auth.krb5ccname);
647 } else {
648 d_printf("no credentials cached\n");
652 return result == NSS_STATUS_SUCCESS;
655 /* Authenticate a user with a plaintext password */
657 static BOOL wbinfo_auth(char *username)
659 struct winbindd_request request;
660 struct winbindd_response response;
661 NSS_STATUS result;
662 char *p;
664 /* Send off request */
666 ZERO_STRUCT(request);
667 ZERO_STRUCT(response);
669 p = strchr(username, '%');
671 if (p) {
672 *p = 0;
673 fstrcpy(request.data.auth.user, username);
674 fstrcpy(request.data.auth.pass, p + 1);
675 *p = '%';
676 } else
677 fstrcpy(request.data.auth.user, username);
679 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
681 /* Display response */
683 d_printf("plaintext password authentication %s\n",
684 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
686 if (response.data.auth.nt_status)
687 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
688 response.data.auth.nt_status_string,
689 response.data.auth.nt_status,
690 response.data.auth.error_string);
692 return result == NSS_STATUS_SUCCESS;
695 /* Authenticate a user with a challenge/response */
697 static BOOL wbinfo_auth_crap(char *username)
699 struct winbindd_request request;
700 struct winbindd_response response;
701 NSS_STATUS result;
702 fstring name_user;
703 fstring name_domain;
704 fstring pass;
705 char *p;
707 /* Send off request */
709 ZERO_STRUCT(request);
710 ZERO_STRUCT(response);
712 p = strchr(username, '%');
714 if (p) {
715 *p = 0;
716 fstrcpy(pass, p + 1);
719 parse_wbinfo_domain_user(username, name_domain, name_user);
721 request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
723 fstrcpy(request.data.auth_crap.user, name_user);
725 fstrcpy(request.data.auth_crap.domain,
726 name_domain);
728 generate_random_buffer(request.data.auth_crap.chal, 8);
730 if (lp_client_ntlmv2_auth()) {
731 DATA_BLOB server_chal;
732 DATA_BLOB names_blob;
734 DATA_BLOB lm_response;
735 DATA_BLOB nt_response;
737 server_chal = data_blob(request.data.auth_crap.chal, 8);
739 /* Pretend this is a login to 'us', for blob purposes */
740 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
742 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
743 &names_blob,
744 &lm_response, &nt_response, NULL)) {
745 data_blob_free(&names_blob);
746 data_blob_free(&server_chal);
747 return False;
749 data_blob_free(&names_blob);
750 data_blob_free(&server_chal);
752 memcpy(request.data.auth_crap.nt_resp, nt_response.data,
753 MIN(nt_response.length,
754 sizeof(request.data.auth_crap.nt_resp)));
755 request.data.auth_crap.nt_resp_len = nt_response.length;
757 memcpy(request.data.auth_crap.lm_resp, lm_response.data,
758 MIN(lm_response.length,
759 sizeof(request.data.auth_crap.lm_resp)));
760 request.data.auth_crap.lm_resp_len = lm_response.length;
762 data_blob_free(&nt_response);
763 data_blob_free(&lm_response);
765 } else {
766 if (lp_client_lanman_auth()
767 && SMBencrypt(pass, request.data.auth_crap.chal,
768 (uchar *)request.data.auth_crap.lm_resp)) {
769 request.data.auth_crap.lm_resp_len = 24;
770 } else {
771 request.data.auth_crap.lm_resp_len = 0;
773 SMBNTencrypt(pass, request.data.auth_crap.chal,
774 (uchar *)request.data.auth_crap.nt_resp);
776 request.data.auth_crap.nt_resp_len = 24;
779 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
781 /* Display response */
783 d_printf("challenge/response password authentication %s\n",
784 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
786 if (response.data.auth.nt_status)
787 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
788 response.data.auth.nt_status_string,
789 response.data.auth.nt_status,
790 response.data.auth.error_string);
792 return result == NSS_STATUS_SUCCESS;
795 /* Authenticate a user with a plaintext password and set a token */
797 static BOOL wbinfo_klog(char *username)
799 struct winbindd_request request;
800 struct winbindd_response response;
801 NSS_STATUS result;
802 char *p;
804 /* Send off request */
806 ZERO_STRUCT(request);
807 ZERO_STRUCT(response);
809 p = strchr(username, '%');
811 if (p) {
812 *p = 0;
813 fstrcpy(request.data.auth.user, username);
814 fstrcpy(request.data.auth.pass, p + 1);
815 *p = '%';
816 } else {
817 fstrcpy(request.data.auth.user, username);
818 fstrcpy(request.data.auth.pass, getpass("Password: "));
821 request.flags |= WBFLAG_PAM_AFS_TOKEN;
823 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
825 /* Display response */
827 d_printf("plaintext password authentication %s\n",
828 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
830 if (response.data.auth.nt_status)
831 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
832 response.data.auth.nt_status_string,
833 response.data.auth.nt_status,
834 response.data.auth.error_string);
836 if (result != NSS_STATUS_SUCCESS)
837 return False;
839 if (response.extra_data.data == NULL) {
840 d_fprintf(stderr, "Did not get token data\n");
841 return False;
844 if (!afs_settoken_str((char *)response.extra_data.data)) {
845 d_fprintf(stderr, "Could not set token\n");
846 return False;
849 d_printf("Successfully created AFS token\n");
850 return True;
853 /* Print domain users */
855 static BOOL print_domain_users(const char *domain)
857 struct winbindd_request request;
858 struct winbindd_response response;
859 const char *extra_data;
860 fstring name;
862 /* Send request to winbind daemon */
864 ZERO_STRUCT(request);
865 ZERO_STRUCT(response);
867 if (domain) {
868 /* '.' is the special sign for our own domwin */
869 if ( strequal(domain, ".") )
870 fstrcpy( request.domain_name, lp_workgroup() );
871 else
872 fstrcpy( request.domain_name, domain );
875 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
876 NSS_STATUS_SUCCESS)
877 return False;
879 /* Look through extra data */
881 if (!response.extra_data.data)
882 return False;
884 extra_data = (const char *)response.extra_data.data;
886 while(next_token(&extra_data, name, ",", sizeof(fstring)))
887 d_printf("%s\n", name);
889 SAFE_FREE(response.extra_data.data);
891 return True;
894 /* Print domain groups */
896 static BOOL print_domain_groups(const char *domain)
898 struct winbindd_request request;
899 struct winbindd_response response;
900 const char *extra_data;
901 fstring name;
903 ZERO_STRUCT(request);
904 ZERO_STRUCT(response);
906 if (domain) {
907 if ( strequal(domain, ".") )
908 fstrcpy( request.domain_name, lp_workgroup() );
909 else
910 fstrcpy( request.domain_name, domain );
913 if (winbindd_request_response(WINBINDD_LIST_GROUPS, &request, &response) !=
914 NSS_STATUS_SUCCESS)
915 return False;
917 /* Look through extra data */
919 if (!response.extra_data.data)
920 return False;
922 extra_data = (const char *)response.extra_data.data;
924 while(next_token(&extra_data, name, ",", sizeof(fstring)))
925 d_printf("%s\n", name);
927 SAFE_FREE(response.extra_data.data);
929 return True;
932 /* Set the authorised user for winbindd access in secrets.tdb */
934 static BOOL wbinfo_set_auth_user(char *username)
936 const char *password;
937 char *p;
938 fstring user, domain;
940 /* Separate into user and password */
942 parse_wbinfo_domain_user(username, domain, user);
944 p = strchr(user, '%');
946 if (p != NULL) {
947 *p = 0;
948 password = p+1;
949 } else {
950 char *thepass = getpass("Password: ");
951 if (thepass) {
952 password = thepass;
953 } else
954 password = "";
957 /* Store or remove DOMAIN\username%password in secrets.tdb */
959 secrets_init();
961 if (user[0]) {
963 if (!secrets_store(SECRETS_AUTH_USER, user,
964 strlen(user) + 1)) {
965 d_fprintf(stderr, "error storing username\n");
966 return False;
969 /* We always have a domain name added by the
970 parse_wbinfo_domain_user() function. */
972 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
973 strlen(domain) + 1)) {
974 d_fprintf(stderr, "error storing domain name\n");
975 return False;
978 } else {
979 secrets_delete(SECRETS_AUTH_USER);
980 secrets_delete(SECRETS_AUTH_DOMAIN);
983 if (password[0]) {
985 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
986 strlen(password) + 1)) {
987 d_fprintf(stderr, "error storing password\n");
988 return False;
991 } else
992 secrets_delete(SECRETS_AUTH_PASSWORD);
994 return True;
997 static void wbinfo_get_auth_user(void)
999 char *user, *domain, *password;
1001 /* Lift data from secrets file */
1003 secrets_fetch_ipc_userpass(&user, &domain, &password);
1005 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
1007 SAFE_FREE(user);
1008 SAFE_FREE(domain);
1009 SAFE_FREE(password);
1010 d_printf("No authorised user configured\n");
1011 return;
1014 /* Pretty print authorised user info */
1016 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
1017 user, password ? "%" : "", password ? password : "");
1019 SAFE_FREE(user);
1020 SAFE_FREE(domain);
1021 SAFE_FREE(password);
1024 static BOOL wbinfo_ping(void)
1026 NSS_STATUS result;
1028 result = winbindd_request_response(WINBINDD_PING, NULL, NULL);
1030 /* Display response */
1032 d_printf("Ping to winbindd %s on fd %d\n",
1033 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
1035 return result == NSS_STATUS_SUCCESS;
1038 /* Main program */
1040 enum {
1041 OPT_SET_AUTH_USER = 1000,
1042 OPT_GET_AUTH_USER,
1043 OPT_DOMAIN_NAME,
1044 OPT_SEQUENCE,
1045 OPT_GETDCNAME,
1046 OPT_USERDOMGROUPS,
1047 OPT_USERSIDS,
1048 OPT_ALLOCATE_UID,
1049 OPT_ALLOCATE_GID,
1050 OPT_SEPARATOR,
1051 OPT_LIST_ALL_DOMAINS
1054 int main(int argc, char **argv)
1056 int opt;
1058 poptContext pc;
1059 static char *string_arg;
1060 static char *opt_domain_name;
1061 static int int_arg;
1062 int result = 1;
1064 struct poptOption long_options[] = {
1065 POPT_AUTOHELP
1067 /* longName, shortName, argInfo, argPtr, value, descrip,
1068 argDesc */
1070 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1071 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1072 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1073 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1074 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1075 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1076 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1077 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1078 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1079 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1080 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1081 "Get a new UID out of idmap" },
1082 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1083 "Get a new GID out of idmap" },
1084 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1085 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1086 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1087 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1088 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1089 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1090 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1091 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1092 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1093 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1094 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1095 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1096 "Get a DC name for a foreign domain", "domainname" },
1097 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1098 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1099 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1100 #ifdef WITH_FAKE_KASERVER
1101 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1102 #endif
1103 #ifdef HAVE_KRB5
1104 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1105 /* destroys wbinfo --help output */
1106 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1107 #endif
1108 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1109 POPT_COMMON_VERSION
1110 POPT_TABLEEND
1113 /* Samba client initialisation */
1114 load_case_tables();
1116 if (!lp_load(dyn_CONFIGFILE, True, False, False, True)) {
1117 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1118 dyn_CONFIGFILE, strerror(errno));
1119 exit(1);
1122 if (!init_names())
1123 return 1;
1125 load_interfaces();
1127 /* Parse options */
1129 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1131 /* Parse command line options */
1133 if (argc == 1) {
1134 poptPrintHelp(pc, stderr, 0);
1135 return 1;
1138 while((opt = poptGetNextOpt(pc)) != -1) {
1139 /* get the generic configuration parameters like --domain */
1142 poptFreeContext(pc);
1144 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1145 POPT_CONTEXT_KEEP_FIRST);
1147 while((opt = poptGetNextOpt(pc)) != -1) {
1148 switch (opt) {
1149 case 'u':
1150 if (!print_domain_users(opt_domain_name)) {
1151 d_fprintf(stderr, "Error looking up domain users\n");
1152 goto done;
1154 break;
1155 case 'g':
1156 if (!print_domain_groups(opt_domain_name)) {
1157 d_fprintf(stderr, "Error looking up domain groups\n");
1158 goto done;
1160 break;
1161 case 's':
1162 if (!wbinfo_lookupsid(string_arg)) {
1163 d_fprintf(stderr, "Could not lookup sid %s\n", string_arg);
1164 goto done;
1166 break;
1167 case 'n':
1168 if (!wbinfo_lookupname(string_arg)) {
1169 d_fprintf(stderr, "Could not lookup name %s\n", string_arg);
1170 goto done;
1172 break;
1173 case 'N':
1174 if (!wbinfo_wins_byname(string_arg)) {
1175 d_fprintf(stderr, "Could not lookup WINS by name %s\n", string_arg);
1176 goto done;
1178 break;
1179 case 'I':
1180 if (!wbinfo_wins_byip(string_arg)) {
1181 d_fprintf(stderr, "Could not lookup WINS by IP %s\n", string_arg);
1182 goto done;
1184 break;
1185 case 'U':
1186 if (!wbinfo_uid_to_sid(int_arg)) {
1187 d_fprintf(stderr, "Could not convert uid %d to sid\n", int_arg);
1188 goto done;
1190 break;
1191 case 'G':
1192 if (!wbinfo_gid_to_sid(int_arg)) {
1193 d_fprintf(stderr, "Could not convert gid %d to sid\n",
1194 int_arg);
1195 goto done;
1197 break;
1198 case 'S':
1199 if (!wbinfo_sid_to_uid(string_arg)) {
1200 d_fprintf(stderr, "Could not convert sid %s to uid\n",
1201 string_arg);
1202 goto done;
1204 break;
1205 case 'Y':
1206 if (!wbinfo_sid_to_gid(string_arg)) {
1207 d_fprintf(stderr, "Could not convert sid %s to gid\n",
1208 string_arg);
1209 goto done;
1211 break;
1212 case OPT_ALLOCATE_UID:
1213 if (!wbinfo_allocate_uid()) {
1214 d_fprintf(stderr, "Could not allocate a uid\n");
1215 goto done;
1217 break;
1218 case OPT_ALLOCATE_GID:
1219 if (!wbinfo_allocate_gid()) {
1220 d_fprintf(stderr, "Could not allocate a gid\n");
1221 goto done;
1223 break;
1224 case 't':
1225 if (!wbinfo_check_secret()) {
1226 d_fprintf(stderr, "Could not check secret\n");
1227 goto done;
1229 break;
1230 case 'm':
1231 if (!wbinfo_list_domains(False)) {
1232 d_fprintf(stderr, "Could not list trusted domains\n");
1233 goto done;
1235 break;
1236 case OPT_SEQUENCE:
1237 if (!wbinfo_show_sequence(opt_domain_name)) {
1238 d_fprintf(stderr, "Could not show sequence numbers\n");
1239 goto done;
1241 break;
1242 case 'D':
1243 if (!wbinfo_domain_info(string_arg)) {
1244 d_fprintf(stderr, "Could not get domain info\n");
1245 goto done;
1247 break;
1248 case 'r':
1249 if (!wbinfo_get_usergroups(string_arg)) {
1250 d_fprintf(stderr, "Could not get groups for user %s\n",
1251 string_arg);
1252 goto done;
1254 break;
1255 case OPT_USERSIDS:
1256 if (!wbinfo_get_usersids(string_arg)) {
1257 d_fprintf(stderr, "Could not get group SIDs for user SID %s\n",
1258 string_arg);
1259 goto done;
1261 break;
1262 case OPT_USERDOMGROUPS:
1263 if (!wbinfo_get_userdomgroups(string_arg)) {
1264 d_fprintf(stderr, "Could not get user's domain groups "
1265 "for user SID %s\n", string_arg);
1266 goto done;
1268 break;
1269 case 'a': {
1270 BOOL got_error = False;
1272 if (!wbinfo_auth(string_arg)) {
1273 d_fprintf(stderr, "Could not authenticate user %s with "
1274 "plaintext password\n", string_arg);
1275 got_error = True;
1278 if (!wbinfo_auth_crap(string_arg)) {
1279 d_fprintf(stderr, "Could not authenticate user %s with "
1280 "challenge/response\n", string_arg);
1281 got_error = True;
1284 if (got_error)
1285 goto done;
1286 break;
1288 case 'K': {
1289 BOOL got_error = False;
1290 uint32 flags = WBFLAG_PAM_KRB5 |
1291 WBFLAG_PAM_CACHED_LOGIN |
1292 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
1293 WBFLAG_PAM_INFO3_TEXT;
1294 fstring tok;
1295 int i;
1296 const char *arg[] = { NULL, NULL };
1297 const char *cctypes[] = { "FILE",
1298 "KCM",
1299 "KCM:0",
1300 "Garbage",
1301 NULL,
1302 "0"};
1304 arg[0] = string_arg;
1306 while (next_token(arg, tok, LIST_SEP, sizeof(tok))) {
1308 for (i=0; i < ARRAY_SIZE(cctypes); i++) {
1309 if (!wbinfo_auth_krb5(tok, cctypes[i], flags)) {
1310 d_fprintf(stderr, "Could not authenticate user [%s] with "
1311 "Kerberos (ccache: %s)\n", tok, cctypes[i]);
1312 got_error = True;
1317 if (got_error)
1318 goto done;
1320 break;
1322 case 'k':
1323 if (!wbinfo_klog(string_arg)) {
1324 d_fprintf(stderr, "Could not klog user\n");
1325 goto done;
1327 break;
1328 case 'p':
1329 if (!wbinfo_ping()) {
1330 d_fprintf(stderr, "could not ping winbindd!\n");
1331 goto done;
1333 break;
1334 case OPT_SET_AUTH_USER:
1335 if (!wbinfo_set_auth_user(string_arg)) {
1336 goto done;
1338 break;
1339 case OPT_GET_AUTH_USER:
1340 wbinfo_get_auth_user();
1341 break;
1342 case OPT_GETDCNAME:
1343 if (!wbinfo_getdcname(string_arg)) {
1344 goto done;
1346 break;
1347 case OPT_SEPARATOR: {
1348 const char sep = winbind_separator_int(True);
1349 if ( !sep ) {
1350 goto done;
1352 d_printf("%c\n", sep);
1353 break;
1355 case OPT_LIST_ALL_DOMAINS:
1356 if (!wbinfo_list_domains(True)) {
1357 goto done;
1359 /* generic configuration options */
1360 case OPT_DOMAIN_NAME:
1361 break;
1362 default:
1363 d_fprintf(stderr, "Invalid option\n");
1364 poptPrintHelp(pc, stderr, 0);
1365 goto done;
1369 result = 0;
1371 /* Exit code */
1373 done:
1374 poptFreeContext(pc);
1375 return result;