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.
29 #define DBGC_CLASS DBGC_WINBIND
31 extern int winbindd_fd;
33 static char winbind_separator_int(BOOL strict)
35 struct winbindd_response response;
42 ZERO_STRUCT(response);
44 /* Send off request */
46 if (winbindd_request_response(WINBINDD_INFO, NULL, &response) !=
48 d_fprintf(stderr, "could not obtain winbind separator!\n");
52 /* HACK: (this module should not call lp_ funtions) */
53 return *lp_winbind_separator();
56 sep = response.data.info.winbind_separator;
60 d_fprintf(stderr, "winbind separator was NULL!\n");
64 /* HACK: (this module should not call lp_ funtions) */
65 sep = *lp_winbind_separator();
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) !=
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,
106 char *p = strchr(domuser,winbind_separator());
109 fstrcpy(user, domuser);
110 fstrcpy(domain, get_winbind_domain());
115 fstrcpy(domain, domuser);
116 domain[PTR_DIFF(p, domuser)] = 0;
122 /* pull pwent info for a given user */
124 static BOOL wbinfo_get_userinfo(char *user)
126 struct winbindd_request request;
127 struct winbindd_response response;
130 ZERO_STRUCT(request);
131 ZERO_STRUCT(response);
135 fstrcpy(request.data.username, user);
137 result = winbindd_request_response(WINBINDD_GETPWNAM, &request, &response);
139 if (result != NSS_STATUS_SUCCESS)
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 );
154 /* pull grent for a given group */
155 static BOOL wbinfo_get_groupinfo(char *group)
157 struct winbindd_request request;
158 struct winbindd_response response;
161 ZERO_STRUCT(request);
162 ZERO_STRUCT(response);
166 fstrcpy(request.data.groupname, group);
168 result = winbindd_request_response(WINBINDD_GETGRNAM, &request,
171 if ( result != NSS_STATUS_SUCCESS)
174 d_printf( "%s:%s:%d\n",
175 response.data.gr.gr_name,
176 response.data.gr.gr_passwd,
177 response.data.gr.gr_gid );
182 /* List groups a user is a member of */
184 static BOOL wbinfo_get_usergroups(char *user)
186 struct winbindd_request request;
187 struct winbindd_response response;
191 ZERO_STRUCT(request);
192 ZERO_STRUCT(response);
196 fstrcpy(request.data.username, user);
198 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
200 if (result != NSS_STATUS_SUCCESS)
203 for (i = 0; i < response.data.num_entries; i++)
204 d_printf("%d\n", (int)((gid_t *)response.extra_data.data)[i]);
206 SAFE_FREE(response.extra_data.data);
212 /* List group SIDs a user SID is a member of */
213 static BOOL wbinfo_get_usersids(char *user_sid)
215 struct winbindd_request request;
216 struct winbindd_response response;
221 ZERO_STRUCT(request);
222 ZERO_STRUCT(response);
225 fstrcpy(request.data.sid, user_sid);
227 result = winbindd_request_response(WINBINDD_GETUSERSIDS, &request, &response);
229 if (result != NSS_STATUS_SUCCESS)
232 s = (const char *)response.extra_data.data;
233 for (i = 0; i < response.data.num_entries; i++) {
238 SAFE_FREE(response.extra_data.data);
243 static BOOL wbinfo_get_userdomgroups(const char *user_sid)
245 struct winbindd_request request;
246 struct winbindd_response response;
249 ZERO_STRUCT(request);
250 ZERO_STRUCT(response);
253 fstrcpy(request.data.sid, user_sid);
255 result = winbindd_request_response(WINBINDD_GETUSERDOMGROUPS, &request,
258 if (result != NSS_STATUS_SUCCESS)
261 if (response.data.num_entries != 0)
262 printf("%s", (char *)response.extra_data.data);
264 SAFE_FREE(response.extra_data.data);
269 /* Convert NetBIOS name to IP */
271 static BOOL wbinfo_wins_byname(char *name)
273 struct winbindd_request request;
274 struct winbindd_response response;
276 ZERO_STRUCT(request);
277 ZERO_STRUCT(response);
281 fstrcpy(request.data.winsreq, name);
283 if (winbindd_request_response(WINBINDD_WINS_BYNAME, &request, &response) !=
284 NSS_STATUS_SUCCESS) {
288 /* Display response */
290 d_printf("%s\n", response.data.winsresp);
295 /* Convert IP to NetBIOS name */
297 static BOOL wbinfo_wins_byip(char *ip)
299 struct winbindd_request request;
300 struct winbindd_response response;
302 ZERO_STRUCT(request);
303 ZERO_STRUCT(response);
307 fstrcpy(request.data.winsreq, ip);
309 if (winbindd_request_response(WINBINDD_WINS_BYIP, &request, &response) !=
310 NSS_STATUS_SUCCESS) {
314 /* Display response */
316 d_printf("%s\n", response.data.winsresp);
321 /* List trusted domains */
323 static BOOL wbinfo_list_domains(BOOL list_all_domains)
325 struct winbindd_request request;
326 struct winbindd_response response;
328 ZERO_STRUCT(request);
329 ZERO_STRUCT(response);
333 request.data.list_all_domains = list_all_domains;
335 if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM, &request, &response) !=
339 /* Display response */
341 if (response.extra_data.data) {
342 const char *extra_data = (char *)response.extra_data.data;
346 while(next_token(&extra_data, name, "\n", sizeof(fstring))) {
347 p = strchr(name, '\\');
349 d_fprintf(stderr, "Got invalid response: %s\n",
354 d_printf("%s\n", name);
357 SAFE_FREE(response.extra_data.data);
363 /* List own domain */
365 static BOOL wbinfo_list_own_domain(void)
367 d_printf("%s\n", get_winbind_domain());
372 /* show sequence numbers */
373 static BOOL wbinfo_show_sequence(const char *domain)
375 struct winbindd_request request;
376 struct winbindd_response response;
378 ZERO_STRUCT(response);
379 ZERO_STRUCT(request);
382 fstrcpy( request.domain_name, domain );
386 if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
390 /* Display response */
392 if (response.extra_data.data) {
393 char *extra_data = (char *)response.extra_data.data;
394 d_printf("%s", extra_data);
395 SAFE_FREE(response.extra_data.data);
401 /* Show domain info */
403 static BOOL wbinfo_domain_info(const char *domain_name)
405 struct winbindd_request request;
406 struct winbindd_response response;
408 ZERO_STRUCT(request);
409 ZERO_STRUCT(response);
411 if ((strequal(domain_name, ".")) || (domain_name[0] == '\0'))
412 fstrcpy(request.domain_name, get_winbind_domain());
414 fstrcpy(request.domain_name, domain_name);
418 if (winbindd_request_response(WINBINDD_DOMAIN_INFO, &request, &response) !=
422 /* Display response */
424 d_printf("Name : %s\n", response.data.domain_info.name);
425 d_printf("Alt_Name : %s\n", response.data.domain_info.alt_name);
427 d_printf("SID : %s\n", response.data.domain_info.sid);
429 d_printf("Active Directory : %s\n",
430 response.data.domain_info.active_directory ? "Yes" : "No");
431 d_printf("Native : %s\n",
432 response.data.domain_info.native_mode ? "Yes" : "No");
434 d_printf("Primary : %s\n",
435 response.data.domain_info.primary ? "Yes" : "No");
437 d_printf("Sequence : %d\n", response.data.domain_info.sequence_number);
442 /* Get a foreign DC's name */
443 static BOOL wbinfo_getdcname(const char *domain_name)
445 struct winbindd_request request;
446 struct winbindd_response response;
448 ZERO_STRUCT(request);
449 ZERO_STRUCT(response);
451 fstrcpy(request.domain_name, domain_name);
455 if (winbindd_request_response(WINBINDD_GETDCNAME, &request, &response) !=
456 NSS_STATUS_SUCCESS) {
457 d_fprintf(stderr, "Could not get dc name for %s\n", domain_name);
461 /* Display response */
463 d_printf("%s\n", response.data.dc_name);
468 /* Check trust account password */
470 static BOOL wbinfo_check_secret(void)
472 struct winbindd_response response;
475 ZERO_STRUCT(response);
477 result = winbindd_request_response(WINBINDD_CHECK_MACHACC, NULL, &response);
479 d_printf("checking the trust secret via RPC calls %s\n",
480 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
482 if (result != NSS_STATUS_SUCCESS)
483 d_fprintf(stderr, "error code was %s (0x%x)\n",
484 response.data.auth.nt_status_string,
485 response.data.auth.nt_status);
487 return result == NSS_STATUS_SUCCESS;
490 /* Convert uid to sid */
492 static BOOL wbinfo_uid_to_sid(uid_t uid)
494 struct winbindd_request request;
495 struct winbindd_response response;
497 ZERO_STRUCT(request);
498 ZERO_STRUCT(response);
502 request.data.uid = uid;
504 if (winbindd_request_response(WINBINDD_UID_TO_SID, &request, &response) !=
508 /* Display response */
510 d_printf("%s\n", response.data.sid.sid);
515 /* Convert gid to sid */
517 static BOOL wbinfo_gid_to_sid(gid_t gid)
519 struct winbindd_request request;
520 struct winbindd_response response;
522 ZERO_STRUCT(request);
523 ZERO_STRUCT(response);
527 request.data.gid = gid;
529 if (winbindd_request_response(WINBINDD_GID_TO_SID, &request, &response) !=
533 /* Display response */
535 d_printf("%s\n", response.data.sid.sid);
540 /* Convert sid to uid */
542 static BOOL wbinfo_sid_to_uid(char *sid)
544 struct winbindd_request request;
545 struct winbindd_response response;
547 ZERO_STRUCT(request);
548 ZERO_STRUCT(response);
552 fstrcpy(request.data.sid, sid);
554 if (winbindd_request_response(WINBINDD_SID_TO_UID, &request, &response) !=
558 /* Display response */
560 d_printf("%d\n", (int)response.data.uid);
565 static BOOL wbinfo_sid_to_gid(char *sid)
567 struct winbindd_request request;
568 struct winbindd_response response;
570 ZERO_STRUCT(request);
571 ZERO_STRUCT(response);
575 fstrcpy(request.data.sid, sid);
577 if (winbindd_request_response(WINBINDD_SID_TO_GID, &request, &response) !=
581 /* Display response */
583 d_printf("%d\n", (int)response.data.gid);
588 static BOOL wbinfo_allocate_uid(void)
592 if (!winbind_allocate_uid(&uid))
595 d_printf("New uid: %d\n", uid);
600 static BOOL wbinfo_allocate_gid(void)
604 if (!winbind_allocate_gid(&gid))
607 d_printf("New gid: %d\n", gid);
612 /* Convert sid to string */
614 static BOOL wbinfo_lookupsid(char *sid)
616 struct winbindd_request request;
617 struct winbindd_response response;
619 ZERO_STRUCT(request);
620 ZERO_STRUCT(response);
622 /* Send off request */
624 fstrcpy(request.data.sid, sid);
626 if (winbindd_request_response(WINBINDD_LOOKUPSID, &request, &response) !=
630 /* Display response */
632 d_printf("%s%c%s %d\n", response.data.name.dom_name,
633 winbind_separator(), response.data.name.name,
634 response.data.name.type);
639 /* Lookup a list of RIDs */
641 static BOOL wbinfo_lookuprids(char *domain_sid, char *arg)
650 enum lsa_SidType *types;
651 const char *domain_name;
654 if (!string_to_sid(&sid, domain_sid)) {
655 d_printf("Could not convert %s to sid\n", domain_sid);
659 mem_ctx = talloc_new(NULL);
660 if (mem_ctx == NULL) {
661 d_printf("talloc_new failed\n");
669 while (next_token(&p, ridstr, " ,\n", sizeof(ridstr))) {
670 uint32 rid = strtoul(ridstr, NULL, 10);
671 ADD_TO_ARRAY(mem_ctx, uint32, rid, &rids, &num_rids);
675 TALLOC_FREE(mem_ctx);
679 if (!winbind_lookup_rids(mem_ctx, &sid, num_rids, rids,
680 &domain_name, &names, &types)) {
681 d_printf("winbind_lookup_rids failed\n");
682 TALLOC_FREE(mem_ctx);
686 d_printf("Domain: %s\n", domain_name);
688 for (i=0; i<num_rids; i++) {
689 d_printf("%8d: %s (%s)\n", rids[i], names[i],
690 sid_type_lookup(types[i]));
693 TALLOC_FREE(mem_ctx);
697 /* Convert string to sid */
699 static BOOL wbinfo_lookupname(char *name)
701 struct winbindd_request request;
702 struct winbindd_response response;
704 /* Send off request */
706 ZERO_STRUCT(request);
707 ZERO_STRUCT(response);
709 parse_wbinfo_domain_user(name, request.data.name.dom_name,
710 request.data.name.name);
712 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
716 /* Display response */
718 d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
723 /* Authenticate a user with a plaintext password */
725 static BOOL wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
727 struct winbindd_request request;
728 struct winbindd_response response;
732 /* Send off request */
734 ZERO_STRUCT(request);
735 ZERO_STRUCT(response);
737 p = strchr(username, '%');
741 fstrcpy(request.data.auth.user, username);
742 fstrcpy(request.data.auth.pass, p + 1);
745 fstrcpy(request.data.auth.user, username);
747 request.flags = flags;
749 fstrcpy(request.data.auth.krb5_cc_type, cctype);
751 request.data.auth.uid = geteuid();
753 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
755 /* Display response */
757 d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n",
758 username, (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", cctype);
760 if (response.data.auth.nt_status)
761 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
762 response.data.auth.nt_status_string,
763 response.data.auth.nt_status,
764 response.data.auth.error_string);
766 if (result == NSS_STATUS_SUCCESS) {
768 if (request.flags & WBFLAG_PAM_INFO3_TEXT) {
769 if (response.data.auth.info3.user_flgs & LOGON_CACHED_ACCOUNT) {
770 d_printf("user_flgs: LOGON_CACHED_ACCOUNT\n");
774 if (response.data.auth.krb5ccname[0] != '\0') {
775 d_printf("credentials were put in: %s\n", response.data.auth.krb5ccname);
777 d_printf("no credentials cached\n");
781 return result == NSS_STATUS_SUCCESS;
784 /* Authenticate a user with a plaintext password */
786 static BOOL wbinfo_auth(char *username)
788 struct winbindd_request request;
789 struct winbindd_response response;
793 /* Send off request */
795 ZERO_STRUCT(request);
796 ZERO_STRUCT(response);
798 p = strchr(username, '%');
802 fstrcpy(request.data.auth.user, username);
803 fstrcpy(request.data.auth.pass, p + 1);
806 fstrcpy(request.data.auth.user, username);
808 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
810 /* Display response */
812 d_printf("plaintext password authentication %s\n",
813 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
815 if (response.data.auth.nt_status)
816 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
817 response.data.auth.nt_status_string,
818 response.data.auth.nt_status,
819 response.data.auth.error_string);
821 return result == NSS_STATUS_SUCCESS;
824 /* Authenticate a user with a challenge/response */
826 static BOOL wbinfo_auth_crap(char *username)
828 struct winbindd_request request;
829 struct winbindd_response response;
836 /* Send off request */
838 ZERO_STRUCT(request);
839 ZERO_STRUCT(response);
841 p = strchr(username, '%');
845 fstrcpy(pass, p + 1);
848 parse_wbinfo_domain_user(username, name_domain, name_user);
850 request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
852 fstrcpy(request.data.auth_crap.user, name_user);
854 fstrcpy(request.data.auth_crap.domain,
857 generate_random_buffer(request.data.auth_crap.chal, 8);
859 if (lp_client_ntlmv2_auth()) {
860 DATA_BLOB server_chal;
861 DATA_BLOB names_blob;
863 DATA_BLOB lm_response;
864 DATA_BLOB nt_response;
866 server_chal = data_blob(request.data.auth_crap.chal, 8);
868 /* Pretend this is a login to 'us', for blob purposes */
869 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
871 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
873 &lm_response, &nt_response, NULL)) {
874 data_blob_free(&names_blob);
875 data_blob_free(&server_chal);
878 data_blob_free(&names_blob);
879 data_blob_free(&server_chal);
881 memcpy(request.data.auth_crap.nt_resp, nt_response.data,
882 MIN(nt_response.length,
883 sizeof(request.data.auth_crap.nt_resp)));
884 request.data.auth_crap.nt_resp_len = nt_response.length;
886 memcpy(request.data.auth_crap.lm_resp, lm_response.data,
887 MIN(lm_response.length,
888 sizeof(request.data.auth_crap.lm_resp)));
889 request.data.auth_crap.lm_resp_len = lm_response.length;
891 data_blob_free(&nt_response);
892 data_blob_free(&lm_response);
895 if (lp_client_lanman_auth()
896 && SMBencrypt(pass, request.data.auth_crap.chal,
897 (uchar *)request.data.auth_crap.lm_resp)) {
898 request.data.auth_crap.lm_resp_len = 24;
900 request.data.auth_crap.lm_resp_len = 0;
902 SMBNTencrypt(pass, request.data.auth_crap.chal,
903 (uchar *)request.data.auth_crap.nt_resp);
905 request.data.auth_crap.nt_resp_len = 24;
908 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
910 /* Display response */
912 d_printf("challenge/response password authentication %s\n",
913 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
915 if (response.data.auth.nt_status)
916 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
917 response.data.auth.nt_status_string,
918 response.data.auth.nt_status,
919 response.data.auth.error_string);
921 return result == NSS_STATUS_SUCCESS;
924 /* Authenticate a user with a plaintext password and set a token */
926 static BOOL wbinfo_klog(char *username)
928 struct winbindd_request request;
929 struct winbindd_response response;
933 /* Send off request */
935 ZERO_STRUCT(request);
936 ZERO_STRUCT(response);
938 p = strchr(username, '%');
942 fstrcpy(request.data.auth.user, username);
943 fstrcpy(request.data.auth.pass, p + 1);
946 fstrcpy(request.data.auth.user, username);
947 fstrcpy(request.data.auth.pass, getpass("Password: "));
950 request.flags |= WBFLAG_PAM_AFS_TOKEN;
952 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
954 /* Display response */
956 d_printf("plaintext password authentication %s\n",
957 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
959 if (response.data.auth.nt_status)
960 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
961 response.data.auth.nt_status_string,
962 response.data.auth.nt_status,
963 response.data.auth.error_string);
965 if (result != NSS_STATUS_SUCCESS)
968 if (response.extra_data.data == NULL) {
969 d_fprintf(stderr, "Did not get token data\n");
973 if (!afs_settoken_str((char *)response.extra_data.data)) {
974 d_fprintf(stderr, "Could not set token\n");
978 d_printf("Successfully created AFS token\n");
982 /* Print domain users */
984 static BOOL print_domain_users(const char *domain)
986 struct winbindd_request request;
987 struct winbindd_response response;
988 const char *extra_data;
991 /* Send request to winbind daemon */
993 ZERO_STRUCT(request);
994 ZERO_STRUCT(response);
997 /* '.' is the special sign for our own domain */
998 if ( strequal(domain, ".") )
999 fstrcpy( request.domain_name, get_winbind_domain() );
1001 fstrcpy( request.domain_name, domain );
1004 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
1008 /* Look through extra data */
1010 if (!response.extra_data.data)
1013 extra_data = (const char *)response.extra_data.data;
1015 while(next_token(&extra_data, name, ",", sizeof(fstring)))
1016 d_printf("%s\n", name);
1018 SAFE_FREE(response.extra_data.data);
1023 /* Print domain groups */
1025 static BOOL print_domain_groups(const char *domain)
1027 struct winbindd_request request;
1028 struct winbindd_response response;
1029 const char *extra_data;
1032 ZERO_STRUCT(request);
1033 ZERO_STRUCT(response);
1036 if ( strequal(domain, ".") )
1037 fstrcpy( request.domain_name, get_winbind_domain() );
1039 fstrcpy( request.domain_name, domain );
1042 if (winbindd_request_response(WINBINDD_LIST_GROUPS, &request, &response) !=
1046 /* Look through extra data */
1048 if (!response.extra_data.data)
1051 extra_data = (const char *)response.extra_data.data;
1053 while(next_token(&extra_data, name, ",", sizeof(fstring)))
1054 d_printf("%s\n", name);
1056 SAFE_FREE(response.extra_data.data);
1061 /* Set the authorised user for winbindd access in secrets.tdb */
1063 static BOOL wbinfo_set_auth_user(char *username)
1065 const char *password;
1067 fstring user, domain;
1069 /* Separate into user and password */
1071 parse_wbinfo_domain_user(username, domain, user);
1073 p = strchr(user, '%');
1079 char *thepass = getpass("Password: ");
1086 /* Store or remove DOMAIN\username%password in secrets.tdb */
1092 if (!secrets_store(SECRETS_AUTH_USER, user,
1093 strlen(user) + 1)) {
1094 d_fprintf(stderr, "error storing username\n");
1098 /* We always have a domain name added by the
1099 parse_wbinfo_domain_user() function. */
1101 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
1102 strlen(domain) + 1)) {
1103 d_fprintf(stderr, "error storing domain name\n");
1108 secrets_delete(SECRETS_AUTH_USER);
1109 secrets_delete(SECRETS_AUTH_DOMAIN);
1114 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
1115 strlen(password) + 1)) {
1116 d_fprintf(stderr, "error storing password\n");
1121 secrets_delete(SECRETS_AUTH_PASSWORD);
1126 static void wbinfo_get_auth_user(void)
1128 char *user, *domain, *password;
1130 /* Lift data from secrets file */
1132 secrets_fetch_ipc_userpass(&user, &domain, &password);
1134 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
1138 SAFE_FREE(password);
1139 d_printf("No authorised user configured\n");
1143 /* Pretty print authorised user info */
1145 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
1146 user, password ? "%" : "", password ? password : "");
1150 SAFE_FREE(password);
1153 static BOOL wbinfo_ping(void)
1157 result = winbindd_request_response(WINBINDD_PING, NULL, NULL);
1159 /* Display response */
1161 d_printf("Ping to winbindd %s on fd %d\n",
1162 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
1164 return result == NSS_STATUS_SUCCESS;
1170 OPT_SET_AUTH_USER = 1000,
1180 OPT_LIST_ALL_DOMAINS,
1181 OPT_LIST_OWN_DOMAIN,
1185 int main(int argc, char **argv, char **envp)
1190 static char *string_arg;
1191 static char *opt_domain_name;
1195 struct poptOption long_options[] = {
1198 /* longName, shortName, argInfo, argPtr, value, descrip,
1201 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1202 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1203 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1204 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1205 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1206 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1207 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1208 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1209 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1210 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1211 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1212 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1213 "Get a new UID out of idmap" },
1214 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1215 "Get a new GID out of idmap" },
1216 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1217 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1218 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1219 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1220 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1221 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1222 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1223 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1224 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1225 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1226 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1227 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1228 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1229 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1230 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1231 "Get a DC name for a foreign domain", "domainname" },
1232 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1233 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1234 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1235 #ifdef WITH_FAKE_KASERVER
1236 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1239 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1240 /* destroys wbinfo --help output */
1241 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1243 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1248 /* Samba client initialisation */
1251 if (!lp_load(dyn_CONFIGFILE, True, False, False, True)) {
1252 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1253 dyn_CONFIGFILE, strerror(errno));
1264 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1266 /* Parse command line options */
1269 poptPrintHelp(pc, stderr, 0);
1273 while((opt = poptGetNextOpt(pc)) != -1) {
1274 /* get the generic configuration parameters like --domain */
1277 poptFreeContext(pc);
1279 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1280 POPT_CONTEXT_KEEP_FIRST);
1282 while((opt = poptGetNextOpt(pc)) != -1) {
1285 if (!print_domain_users(opt_domain_name)) {
1286 d_fprintf(stderr, "Error looking up domain users\n");
1291 if (!print_domain_groups(opt_domain_name)) {
1292 d_fprintf(stderr, "Error looking up domain groups\n");
1297 if (!wbinfo_lookupsid(string_arg)) {
1298 d_fprintf(stderr, "Could not lookup sid %s\n", string_arg);
1303 if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
1304 d_fprintf(stderr, "Could not lookup RIDs %s\n", string_arg);
1309 if (!wbinfo_lookupname(string_arg)) {
1310 d_fprintf(stderr, "Could not lookup name %s\n", string_arg);
1315 if (!wbinfo_wins_byname(string_arg)) {
1316 d_fprintf(stderr, "Could not lookup WINS by name %s\n", string_arg);
1321 if (!wbinfo_wins_byip(string_arg)) {
1322 d_fprintf(stderr, "Could not lookup WINS by IP %s\n", string_arg);
1327 if (!wbinfo_uid_to_sid(int_arg)) {
1328 d_fprintf(stderr, "Could not convert uid %d to sid\n", int_arg);
1333 if (!wbinfo_gid_to_sid(int_arg)) {
1334 d_fprintf(stderr, "Could not convert gid %d to sid\n",
1340 if (!wbinfo_sid_to_uid(string_arg)) {
1341 d_fprintf(stderr, "Could not convert sid %s to uid\n",
1347 if (!wbinfo_sid_to_gid(string_arg)) {
1348 d_fprintf(stderr, "Could not convert sid %s to gid\n",
1353 case OPT_ALLOCATE_UID:
1354 if (!wbinfo_allocate_uid()) {
1355 d_fprintf(stderr, "Could not allocate a uid\n");
1359 case OPT_ALLOCATE_GID:
1360 if (!wbinfo_allocate_gid()) {
1361 d_fprintf(stderr, "Could not allocate a gid\n");
1366 if (!wbinfo_check_secret()) {
1367 d_fprintf(stderr, "Could not check secret\n");
1372 if (!wbinfo_list_domains(False)) {
1373 d_fprintf(stderr, "Could not list trusted domains\n");
1378 if (!wbinfo_show_sequence(opt_domain_name)) {
1379 d_fprintf(stderr, "Could not show sequence numbers\n");
1384 if (!wbinfo_domain_info(string_arg)) {
1385 d_fprintf(stderr, "Could not get domain info\n");
1390 if (!wbinfo_get_userinfo(string_arg)) {
1391 d_fprintf(stderr, "Could not get info for user %s\n",
1396 case OPT_GROUP_INFO:
1397 if ( !wbinfo_get_groupinfo(string_arg)) {
1398 d_fprintf(stderr, "Could not get info for "
1399 "group %s\n", string_arg);
1404 if (!wbinfo_get_usergroups(string_arg)) {
1405 d_fprintf(stderr, "Could not get groups for user %s\n",
1411 if (!wbinfo_get_usersids(string_arg)) {
1412 d_fprintf(stderr, "Could not get group SIDs for user SID %s\n",
1417 case OPT_USERDOMGROUPS:
1418 if (!wbinfo_get_userdomgroups(string_arg)) {
1419 d_fprintf(stderr, "Could not get user's domain groups "
1420 "for user SID %s\n", string_arg);
1425 BOOL got_error = False;
1427 if (!wbinfo_auth(string_arg)) {
1428 d_fprintf(stderr, "Could not authenticate user %s with "
1429 "plaintext password\n", string_arg);
1433 if (!wbinfo_auth_crap(string_arg)) {
1434 d_fprintf(stderr, "Could not authenticate user %s with "
1435 "challenge/response\n", string_arg);
1444 BOOL got_error = False;
1445 uint32 flags = WBFLAG_PAM_KRB5 |
1446 WBFLAG_PAM_CACHED_LOGIN |
1447 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
1448 WBFLAG_PAM_INFO3_TEXT;
1451 const char *arg[] = { NULL, NULL };
1452 const char *cctypes[] = { "FILE",
1459 arg[0] = string_arg;
1461 while (next_token(arg, tok, LIST_SEP, sizeof(tok))) {
1463 for (i=0; i < ARRAY_SIZE(cctypes); i++) {
1464 if (!wbinfo_auth_krb5(tok, cctypes[i], flags)) {
1465 d_fprintf(stderr, "Could not authenticate user [%s] with "
1466 "Kerberos (ccache: %s)\n", tok, cctypes[i]);
1478 if (!wbinfo_klog(string_arg)) {
1479 d_fprintf(stderr, "Could not klog user\n");
1484 if (!wbinfo_ping()) {
1485 d_fprintf(stderr, "could not ping winbindd!\n");
1489 case OPT_SET_AUTH_USER:
1490 if (!wbinfo_set_auth_user(string_arg)) {
1494 case OPT_GET_AUTH_USER:
1495 wbinfo_get_auth_user();
1498 if (!wbinfo_getdcname(string_arg)) {
1502 case OPT_SEPARATOR: {
1503 const char sep = winbind_separator_int(True);
1507 d_printf("%c\n", sep);
1510 case OPT_LIST_ALL_DOMAINS:
1511 if (!wbinfo_list_domains(True)) {
1515 case OPT_LIST_OWN_DOMAIN:
1516 if (!wbinfo_list_own_domain()) {
1520 /* generic configuration options */
1521 case OPT_DOMAIN_NAME:
1524 d_fprintf(stderr, "Invalid option\n");
1525 poptPrintHelp(pc, stderr, 0);
1535 poptFreeContext(pc);