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 3 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, see <http://www.gnu.org/licenses/>.
24 #include "winbind_client.h"
25 #include "libwbclient/wbclient.h"
28 #define DBGC_CLASS DBGC_WINBIND
30 extern int winbindd_fd;
32 static char winbind_separator_int(bool strict)
34 struct winbindd_response response;
41 ZERO_STRUCT(response);
43 /* Send off request */
45 if (winbindd_request_response(WINBINDD_INFO, NULL, &response) !=
47 d_fprintf(stderr, "could not obtain winbind separator!\n");
51 /* HACK: (this module should not call lp_ funtions) */
52 return *lp_winbind_separator();
55 sep = response.data.info.winbind_separator;
59 d_fprintf(stderr, "winbind separator was NULL!\n");
63 /* HACK: (this module should not call lp_ funtions) */
64 sep = *lp_winbind_separator();
70 static char winbind_separator(void)
72 return winbind_separator_int(false);
75 static const char *get_winbind_domain(void)
77 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
78 struct wbcDomainInfo *dinfo = NULL;
79 static fstring winbind_domain;
83 wbc_status = wbcDomainInfo(".", &dinfo);
85 if (!WBC_ERROR_IS_OK(wbc_status)) {
86 d_fprintf(stderr, "could not obtain winbind domain name!\n");
88 /* HACK: (this module should not call lp_ funtions) */
89 return lp_workgroup();
92 fstrcpy(winbind_domain, dinfo->short_name);
96 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 /* Maybe it was a UPN? */
110 if ((p = strchr(domuser, '@')) != NULL) {
112 fstrcpy(user, domuser);
116 fstrcpy(user, domuser);
117 fstrcpy(domain, get_winbind_domain());
122 fstrcpy(domain, domuser);
123 domain[PTR_DIFF(p, domuser)] = 0;
129 /* pull pwent info for a given user */
131 static bool wbinfo_get_userinfo(char *user)
133 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
134 struct passwd *pwd = NULL;
136 wbc_status = wbcGetpwnam(user, &pwd);
137 if (!WBC_ERROR_IS_OK(wbc_status)) {
141 d_printf("%s:%s:%d:%d:%s:%s:%s\n",
153 /* pull pwent info for a given uid */
154 static bool wbinfo_get_uidinfo(int uid)
156 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
157 struct passwd *pwd = NULL;
159 wbc_status = wbcGetpwuid(uid, &pwd);
160 if (!WBC_ERROR_IS_OK(wbc_status)) {
164 d_printf("%s:%s:%d:%d:%s:%s:%s\n",
176 /* pull grent for a given group */
177 static bool wbinfo_get_groupinfo(char *group)
179 struct winbindd_request request;
180 struct winbindd_response response;
183 ZERO_STRUCT(request);
184 ZERO_STRUCT(response);
188 fstrcpy(request.data.groupname, group);
190 result = winbindd_request_response(WINBINDD_GETGRNAM, &request,
193 if ( result != NSS_STATUS_SUCCESS)
196 d_printf( "%s:%s:%d\n",
197 response.data.gr.gr_name,
198 response.data.gr.gr_passwd,
199 response.data.gr.gr_gid );
204 /* List groups a user is a member of */
206 static bool wbinfo_get_usergroups(char *user)
208 struct winbindd_request request;
209 struct winbindd_response response;
213 ZERO_STRUCT(request);
214 ZERO_STRUCT(response);
218 fstrcpy(request.data.username, user);
220 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
222 if (result != NSS_STATUS_SUCCESS)
225 for (i = 0; i < response.data.num_entries; i++)
226 d_printf("%d\n", (int)((gid_t *)response.extra_data.data)[i]);
228 SAFE_FREE(response.extra_data.data);
234 /* List group SIDs a user SID is a member of */
235 static bool wbinfo_get_usersids(const char *user_sid_str)
237 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
240 struct wbcDomainSid user_sid, *sids = NULL;
244 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
245 if (!WBC_ERROR_IS_OK(wbc_status)) {
249 wbc_status = wbcLookupUserSids(&user_sid, false, &num_sids, &sids);
250 if (!WBC_ERROR_IS_OK(wbc_status)) {
254 for (i = 0; i < num_sids; i++) {
256 wbc_status = wbcSidToString(&sids[i], &str);
257 if (!WBC_ERROR_IS_OK(wbc_status)) {
261 d_printf("%s\n", str);
270 static bool wbinfo_get_userdomgroups(const char *user_sid_str)
272 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
275 struct wbcDomainSid user_sid, *sids = NULL;
279 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
280 if (!WBC_ERROR_IS_OK(wbc_status)) {
284 wbc_status = wbcLookupUserSids(&user_sid, true, &num_sids, &sids);
285 if (!WBC_ERROR_IS_OK(wbc_status)) {
289 for (i = 0; i < num_sids; i++) {
291 wbc_status = wbcSidToString(&sids[i], &str);
292 if (!WBC_ERROR_IS_OK(wbc_status)) {
296 d_printf("%s\n", str);
305 /* Convert NetBIOS name to IP */
307 static bool wbinfo_wins_byname(char *name)
309 struct winbindd_request request;
310 struct winbindd_response response;
312 ZERO_STRUCT(request);
313 ZERO_STRUCT(response);
317 fstrcpy(request.data.winsreq, name);
319 if (winbindd_request_response(WINBINDD_WINS_BYNAME, &request, &response) !=
320 NSS_STATUS_SUCCESS) {
324 /* Display response */
326 d_printf("%s\n", response.data.winsresp);
331 /* Convert IP to NetBIOS name */
333 static bool wbinfo_wins_byip(char *ip)
335 struct winbindd_request request;
336 struct winbindd_response response;
338 ZERO_STRUCT(request);
339 ZERO_STRUCT(response);
343 fstrcpy(request.data.winsreq, ip);
345 if (winbindd_request_response(WINBINDD_WINS_BYIP, &request, &response) !=
346 NSS_STATUS_SUCCESS) {
350 /* Display response */
352 d_printf("%s\n", response.data.winsresp);
357 /* List trusted domains */
359 static bool wbinfo_list_domains(bool list_all_domains)
361 struct winbindd_request request;
362 struct winbindd_response response;
364 ZERO_STRUCT(request);
365 ZERO_STRUCT(response);
369 request.data.list_all_domains = list_all_domains;
371 if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM, &request, &response) !=
375 /* Display response */
377 if (response.extra_data.data) {
378 const char *extra_data = (char *)response.extra_data.data;
381 TALLOC_CTX *frame = talloc_stackframe();
383 while(next_token_talloc(frame,&extra_data,&name,"\n")) {
384 p = strchr(name, '\\');
386 d_fprintf(stderr, "Got invalid response: %s\n",
389 SAFE_FREE(response.extra_data.data);
393 d_printf("%s\n", name);
396 SAFE_FREE(response.extra_data.data);
402 /* List own domain */
404 static bool wbinfo_list_own_domain(void)
406 d_printf("%s\n", get_winbind_domain());
411 /* show sequence numbers */
412 static bool wbinfo_show_sequence(const char *domain)
414 struct winbindd_request request;
415 struct winbindd_response response;
417 ZERO_STRUCT(response);
418 ZERO_STRUCT(request);
421 fstrcpy( request.domain_name, domain );
425 if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
429 /* Display response */
432 d_printf("%s : ", domain);
433 if (response.data.sequence_number == (uint32_t)-1) {
434 d_printf("DISCONNECTED\n");
436 d_printf("%d\n", response.data.sequence_number);
438 } else if (response.extra_data.data) {
439 char *extra_data = (char *)response.extra_data.data;
440 d_printf("%s", extra_data);
441 SAFE_FREE(response.extra_data.data);
447 /* Show domain info */
449 static bool wbinfo_domain_info(const char *domain_name)
451 struct winbindd_request request;
452 struct winbindd_response response;
454 ZERO_STRUCT(request);
455 ZERO_STRUCT(response);
457 if ((strequal(domain_name, ".")) || (domain_name[0] == '\0'))
458 fstrcpy(request.domain_name, get_winbind_domain());
460 fstrcpy(request.domain_name, domain_name);
464 if (winbindd_request_response(WINBINDD_DOMAIN_INFO, &request, &response) !=
468 /* Display response */
470 d_printf("Name : %s\n", response.data.domain_info.name);
471 d_printf("Alt_Name : %s\n", response.data.domain_info.alt_name);
473 d_printf("SID : %s\n", response.data.domain_info.sid);
475 d_printf("Active Directory : %s\n",
476 response.data.domain_info.active_directory ? "Yes" : "No");
477 d_printf("Native : %s\n",
478 response.data.domain_info.native_mode ? "Yes" : "No");
480 d_printf("Primary : %s\n",
481 response.data.domain_info.primary ? "Yes" : "No");
486 /* Get a foreign DC's name */
487 static bool wbinfo_getdcname(const char *domain_name)
489 struct winbindd_request request;
490 struct winbindd_response response;
492 ZERO_STRUCT(request);
493 ZERO_STRUCT(response);
495 fstrcpy(request.domain_name, domain_name);
499 if (winbindd_request_response(WINBINDD_GETDCNAME, &request, &response) !=
500 NSS_STATUS_SUCCESS) {
501 d_fprintf(stderr, "Could not get dc name for %s\n", domain_name);
505 /* Display response */
507 d_printf("%s\n", response.data.dc_name);
513 static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
515 struct winbindd_request request;
516 struct winbindd_response response;
518 ZERO_STRUCT(request);
519 ZERO_STRUCT(response);
521 fstrcpy(request.domain_name, domain_name);
522 request.flags = flags;
524 request.flags |= DS_DIRECTORY_SERVICE_REQUIRED;
528 if (winbindd_request_response(WINBINDD_DSGETDCNAME, &request, &response) !=
529 NSS_STATUS_SUCCESS) {
530 d_fprintf(stderr, "Could not find dc for %s\n", domain_name);
534 /* Display response */
536 d_printf("%s\n", response.data.dc_name);
541 /* Check trust account password */
543 static bool wbinfo_check_secret(void)
545 struct winbindd_response response;
548 ZERO_STRUCT(response);
550 result = winbindd_request_response(WINBINDD_CHECK_MACHACC, NULL, &response);
552 d_printf("checking the trust secret via RPC calls %s\n",
553 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
555 if (result != NSS_STATUS_SUCCESS)
556 d_fprintf(stderr, "error code was %s (0x%x)\n",
557 response.data.auth.nt_status_string,
558 response.data.auth.nt_status);
560 return result == NSS_STATUS_SUCCESS;
563 /* Convert uid to sid */
565 static bool wbinfo_uid_to_sid(uid_t uid)
567 struct winbindd_request request;
568 struct winbindd_response response;
570 ZERO_STRUCT(request);
571 ZERO_STRUCT(response);
575 request.data.uid = uid;
577 if (winbindd_request_response(WINBINDD_UID_TO_SID, &request, &response) !=
581 /* Display response */
583 d_printf("%s\n", response.data.sid.sid);
588 /* Convert gid to sid */
590 static bool wbinfo_gid_to_sid(gid_t gid)
592 struct winbindd_request request;
593 struct winbindd_response response;
595 ZERO_STRUCT(request);
596 ZERO_STRUCT(response);
600 request.data.gid = gid;
602 if (winbindd_request_response(WINBINDD_GID_TO_SID, &request, &response) !=
606 /* Display response */
608 d_printf("%s\n", response.data.sid.sid);
613 /* Convert sid to uid */
615 static bool wbinfo_sid_to_uid(char *sid)
617 struct winbindd_request request;
618 struct winbindd_response response;
620 ZERO_STRUCT(request);
621 ZERO_STRUCT(response);
625 fstrcpy(request.data.sid, sid);
627 if (winbindd_request_response(WINBINDD_SID_TO_UID, &request, &response) !=
631 /* Display response */
633 d_printf("%d\n", (int)response.data.uid);
638 static bool wbinfo_sid_to_gid(char *sid)
640 struct winbindd_request request;
641 struct winbindd_response response;
643 ZERO_STRUCT(request);
644 ZERO_STRUCT(response);
648 fstrcpy(request.data.sid, sid);
650 if (winbindd_request_response(WINBINDD_SID_TO_GID, &request, &response) !=
654 /* Display response */
656 d_printf("%d\n", (int)response.data.gid);
661 static bool wbinfo_allocate_uid(void)
665 if (!winbind_allocate_uid(&uid))
668 d_printf("New uid: %d\n", uid);
673 static bool wbinfo_allocate_gid(void)
677 if (!winbind_allocate_gid(&gid))
680 d_printf("New gid: %d\n", gid);
685 /* Convert sid to string */
687 static bool wbinfo_lookupsid(char *sid)
689 struct winbindd_request request;
690 struct winbindd_response response;
692 ZERO_STRUCT(request);
693 ZERO_STRUCT(response);
695 /* Send off request */
697 fstrcpy(request.data.sid, sid);
699 if (winbindd_request_response(WINBINDD_LOOKUPSID, &request, &response) !=
703 /* Display response */
705 d_printf("%s%c%s %d\n", response.data.name.dom_name,
706 winbind_separator(), response.data.name.name,
707 response.data.name.type);
712 /* Lookup a list of RIDs */
714 static bool wbinfo_lookuprids(char *domain, char *arg)
723 enum lsa_SidType *types;
724 const char *domain_name;
726 struct winbindd_request request;
727 struct winbindd_response response;
729 ZERO_STRUCT(request);
730 ZERO_STRUCT(response);
732 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0'))
733 fstrcpy(request.domain_name, get_winbind_domain());
735 fstrcpy(request.domain_name, domain);
739 if (winbindd_request_response(WINBINDD_DOMAIN_INFO, &request, &response) !=
740 NSS_STATUS_SUCCESS) {
741 d_printf("Could not get domain sid for %s\n", request.domain_name);
745 if (!string_to_sid(&sid, response.data.domain_info.sid)) {
746 d_printf("Could not convert %s to sid\n", response.data.domain_info.sid);
750 mem_ctx = talloc_new(NULL);
751 if (mem_ctx == NULL) {
752 d_printf("talloc_new failed\n");
760 while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
761 uint32 rid = strtoul(ridstr, NULL, 10);
762 ADD_TO_ARRAY(mem_ctx, uint32, rid, &rids, &num_rids);
766 TALLOC_FREE(mem_ctx);
770 if (!winbind_lookup_rids(mem_ctx, &sid, num_rids, rids,
771 &domain_name, &names, &types)) {
772 d_printf("winbind_lookup_rids failed\n");
773 TALLOC_FREE(mem_ctx);
777 d_printf("Domain: %s\n", domain_name);
779 for (i=0; i<num_rids; i++) {
780 d_printf("%8d: %s (%s)\n", rids[i], names[i],
781 sid_type_lookup(types[i]));
784 TALLOC_FREE(mem_ctx);
788 /* Convert string to sid */
790 static bool wbinfo_lookupname(char *name)
792 struct winbindd_request request;
793 struct winbindd_response response;
795 /* Send off request */
797 ZERO_STRUCT(request);
798 ZERO_STRUCT(response);
800 parse_wbinfo_domain_user(name, request.data.name.dom_name,
801 request.data.name.name);
803 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
807 /* Display response */
809 d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
814 /* Authenticate a user with a plaintext password */
816 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
818 struct winbindd_request request;
819 struct winbindd_response response;
823 /* Send off request */
825 ZERO_STRUCT(request);
826 ZERO_STRUCT(response);
828 p = strchr(username, '%');
832 fstrcpy(request.data.auth.user, username);
833 fstrcpy(request.data.auth.pass, p + 1);
836 fstrcpy(request.data.auth.user, username);
838 request.flags = flags;
840 fstrcpy(request.data.auth.krb5_cc_type, cctype);
842 request.data.auth.uid = geteuid();
844 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
846 /* Display response */
848 d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n",
849 username, (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", cctype);
851 if (response.data.auth.nt_status)
852 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
853 response.data.auth.nt_status_string,
854 response.data.auth.nt_status,
855 response.data.auth.error_string);
857 if (result == NSS_STATUS_SUCCESS) {
859 if (request.flags & WBFLAG_PAM_INFO3_TEXT) {
860 if (response.data.auth.info3.user_flgs & NETLOGON_CACHED_ACCOUNT) {
861 d_printf("user_flgs: NETLOGON_CACHED_ACCOUNT\n");
865 if (response.data.auth.krb5ccname[0] != '\0') {
866 d_printf("credentials were put in: %s\n", response.data.auth.krb5ccname);
868 d_printf("no credentials cached\n");
872 return result == NSS_STATUS_SUCCESS;
875 /* Authenticate a user with a plaintext password */
877 static bool wbinfo_auth(char *username)
879 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
882 const char *password = NULL;
885 if ((s = SMB_STRDUP(username)) == NULL) {
889 if ((p = strchr(s, '%')) != NULL) {
899 wbc_status = wbcAuthenticateUser(name, password);
901 d_printf("plaintext password authentication %s\n",
902 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
905 if (response.data.auth.nt_status)
906 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
907 response.data.auth.nt_status_string,
908 response.data.auth.nt_status,
909 response.data.auth.error_string);
914 return WBC_ERROR_IS_OK(wbc_status);
917 /* Authenticate a user with a challenge/response */
919 static bool wbinfo_auth_crap(char *username)
921 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
922 struct wbcAuthUserParams params;
923 struct wbcAuthUserInfo *info = NULL;
924 struct wbcAuthErrorInfo *err = NULL;
925 DATA_BLOB lm = data_blob_null;
926 DATA_BLOB nt = data_blob_null;
932 p = strchr(username, '%');
936 fstrcpy(pass, p + 1);
939 parse_wbinfo_domain_user(username, name_domain, name_user);
941 params.account_name = name_user;
942 params.domain_name = name_domain;
943 params.workstation_name = NULL;
946 params.parameter_control= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
947 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
949 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
951 generate_random_buffer(params.password.response.challenge, 8);
953 if (lp_client_ntlmv2_auth()) {
954 DATA_BLOB server_chal;
955 DATA_BLOB names_blob;
957 server_chal = data_blob(params.password.response.challenge, 8);
959 /* Pretend this is a login to 'us', for blob purposes */
960 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
962 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
965 data_blob_free(&names_blob);
966 data_blob_free(&server_chal);
969 data_blob_free(&names_blob);
970 data_blob_free(&server_chal);
973 if (lp_client_lanman_auth()) {
975 lm = data_blob(NULL, 24);
976 ok = SMBencrypt(pass, params.password.response.challenge,
982 nt = data_blob(NULL, 24);
983 SMBNTencrypt(pass, params.password.response.challenge,
987 params.password.response.nt_length = nt.length;
988 params.password.response.nt_data = nt.data;
989 params.password.response.lm_length = lm.length;
990 params.password.response.lm_data = lm.data;
992 wbc_status = wbcAuthenticateUserEx(¶ms, &info, &err);
994 /* Display response */
996 d_printf("challenge/response password authentication %s\n",
997 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
999 if (wbc_status == WBC_ERR_AUTH_ERROR) {
1000 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1003 err->display_string);
1005 } else if (WBC_ERROR_IS_OK(wbc_status)) {
1006 wbcFreeMemory(info);
1009 data_blob_free(&nt);
1010 data_blob_free(&lm);
1012 return WBC_ERROR_IS_OK(wbc_status);
1015 /* Authenticate a user with a plaintext password and set a token */
1017 static bool wbinfo_klog(char *username)
1019 struct winbindd_request request;
1020 struct winbindd_response response;
1024 /* Send off request */
1026 ZERO_STRUCT(request);
1027 ZERO_STRUCT(response);
1029 p = strchr(username, '%');
1033 fstrcpy(request.data.auth.user, username);
1034 fstrcpy(request.data.auth.pass, p + 1);
1037 fstrcpy(request.data.auth.user, username);
1038 fstrcpy(request.data.auth.pass, getpass("Password: "));
1041 request.flags |= WBFLAG_PAM_AFS_TOKEN;
1043 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
1045 /* Display response */
1047 d_printf("plaintext password authentication %s\n",
1048 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1050 if (response.data.auth.nt_status)
1051 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1052 response.data.auth.nt_status_string,
1053 response.data.auth.nt_status,
1054 response.data.auth.error_string);
1056 if (result != NSS_STATUS_SUCCESS)
1059 if (response.extra_data.data == NULL) {
1060 d_fprintf(stderr, "Did not get token data\n");
1064 if (!afs_settoken_str((char *)response.extra_data.data)) {
1065 d_fprintf(stderr, "Could not set token\n");
1069 d_printf("Successfully created AFS token\n");
1073 /* Print domain users */
1075 static bool print_domain_users(const char *domain)
1077 struct winbindd_request request;
1078 struct winbindd_response response;
1079 const char *extra_data;
1081 TALLOC_CTX *frame = NULL;
1083 /* Send request to winbind daemon */
1085 ZERO_STRUCT(request);
1086 ZERO_STRUCT(response);
1089 /* '.' is the special sign for our own domain */
1090 if ( strequal(domain, ".") )
1091 fstrcpy( request.domain_name, get_winbind_domain() );
1093 fstrcpy( request.domain_name, domain );
1096 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
1100 /* Look through extra data */
1102 if (!response.extra_data.data)
1105 extra_data = (const char *)response.extra_data.data;
1107 frame = talloc_stackframe();
1108 while(next_token_talloc(frame,&extra_data,&name, ","))
1109 d_printf("%s\n", name);
1112 SAFE_FREE(response.extra_data.data);
1117 /* Print domain groups */
1119 static bool print_domain_groups(const char *domain)
1121 struct winbindd_request request;
1122 struct winbindd_response response;
1123 const char *extra_data;
1124 TALLOC_CTX *frame = NULL;
1127 ZERO_STRUCT(request);
1128 ZERO_STRUCT(response);
1131 if ( strequal(domain, ".") )
1132 fstrcpy( request.domain_name, get_winbind_domain() );
1134 fstrcpy( request.domain_name, domain );
1137 if (winbindd_request_response(WINBINDD_LIST_GROUPS, &request, &response) !=
1141 /* Look through extra data */
1143 if (!response.extra_data.data)
1146 extra_data = (const char *)response.extra_data.data;
1148 frame = talloc_stackframe();
1149 while(next_token_talloc(frame,&extra_data,&name, ","))
1150 d_printf("%s\n", name);
1153 SAFE_FREE(response.extra_data.data);
1158 /* Set the authorised user for winbindd access in secrets.tdb */
1160 static bool wbinfo_set_auth_user(char *username)
1162 const char *password;
1164 fstring user, domain;
1166 /* Separate into user and password */
1168 parse_wbinfo_domain_user(username, domain, user);
1170 p = strchr(user, '%');
1176 char *thepass = getpass("Password: ");
1183 /* Store or remove DOMAIN\username%password in secrets.tdb */
1189 if (!secrets_store(SECRETS_AUTH_USER, user,
1190 strlen(user) + 1)) {
1191 d_fprintf(stderr, "error storing username\n");
1195 /* We always have a domain name added by the
1196 parse_wbinfo_domain_user() function. */
1198 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
1199 strlen(domain) + 1)) {
1200 d_fprintf(stderr, "error storing domain name\n");
1205 secrets_delete(SECRETS_AUTH_USER);
1206 secrets_delete(SECRETS_AUTH_DOMAIN);
1211 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
1212 strlen(password) + 1)) {
1213 d_fprintf(stderr, "error storing password\n");
1218 secrets_delete(SECRETS_AUTH_PASSWORD);
1223 static void wbinfo_get_auth_user(void)
1225 char *user, *domain, *password;
1227 /* Lift data from secrets file */
1229 secrets_fetch_ipc_userpass(&user, &domain, &password);
1231 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
1235 SAFE_FREE(password);
1236 d_printf("No authorised user configured\n");
1240 /* Pretty print authorised user info */
1242 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
1243 user, password ? "%" : "", password ? password : "");
1247 SAFE_FREE(password);
1250 static bool wbinfo_ping(void)
1254 result = winbindd_request_response(WINBINDD_PING, NULL, NULL);
1256 /* Display response */
1258 d_printf("Ping to winbindd %s on fd %d\n",
1259 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
1261 return result == NSS_STATUS_SUCCESS;
1267 OPT_SET_AUTH_USER = 1000,
1278 OPT_LIST_ALL_DOMAINS,
1279 OPT_LIST_OWN_DOMAIN,
1284 int main(int argc, char **argv, char **envp)
1287 TALLOC_CTX *frame = talloc_stackframe();
1289 static char *string_arg;
1290 static char *opt_domain_name;
1294 struct poptOption long_options[] = {
1297 /* longName, shortName, argInfo, argPtr, value, descrip,
1300 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1301 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1302 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1303 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1304 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1305 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1306 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1307 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1308 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1309 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1310 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1311 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1312 "Get a new UID out of idmap" },
1313 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1314 "Get a new GID out of idmap" },
1315 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1316 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1317 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1318 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1319 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1320 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1321 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1322 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1323 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1324 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1325 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1326 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1327 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1328 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1329 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1330 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1331 "Get a DC name for a foreign domain", "domainname" },
1332 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
1333 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1334 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1335 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1336 #ifdef WITH_FAKE_KASERVER
1337 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1340 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1341 /* destroys wbinfo --help output */
1342 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1344 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1345 POPT_COMMON_CONFIGFILE
1350 /* Samba client initialisation */
1356 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1358 /* Parse command line options */
1361 poptPrintHelp(pc, stderr, 0);
1365 while((opt = poptGetNextOpt(pc)) != -1) {
1366 /* get the generic configuration parameters like --domain */
1369 poptFreeContext(pc);
1371 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, true)) {
1372 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1373 get_dyn_CONFIGFILE(), strerror(errno));
1382 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1383 POPT_CONTEXT_KEEP_FIRST);
1385 while((opt = poptGetNextOpt(pc)) != -1) {
1388 if (!print_domain_users(opt_domain_name)) {
1389 d_fprintf(stderr, "Error looking up domain users\n");
1394 if (!print_domain_groups(opt_domain_name)) {
1395 d_fprintf(stderr, "Error looking up domain groups\n");
1400 if (!wbinfo_lookupsid(string_arg)) {
1401 d_fprintf(stderr, "Could not lookup sid %s\n", string_arg);
1406 if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
1407 d_fprintf(stderr, "Could not lookup RIDs %s\n", string_arg);
1412 if (!wbinfo_lookupname(string_arg)) {
1413 d_fprintf(stderr, "Could not lookup name %s\n", string_arg);
1418 if (!wbinfo_wins_byname(string_arg)) {
1419 d_fprintf(stderr, "Could not lookup WINS by name %s\n", string_arg);
1424 if (!wbinfo_wins_byip(string_arg)) {
1425 d_fprintf(stderr, "Could not lookup WINS by IP %s\n", string_arg);
1430 if (!wbinfo_uid_to_sid(int_arg)) {
1431 d_fprintf(stderr, "Could not convert uid %d to sid\n", int_arg);
1436 if (!wbinfo_gid_to_sid(int_arg)) {
1437 d_fprintf(stderr, "Could not convert gid %d to sid\n",
1443 if (!wbinfo_sid_to_uid(string_arg)) {
1444 d_fprintf(stderr, "Could not convert sid %s to uid\n",
1450 if (!wbinfo_sid_to_gid(string_arg)) {
1451 d_fprintf(stderr, "Could not convert sid %s to gid\n",
1456 case OPT_ALLOCATE_UID:
1457 if (!wbinfo_allocate_uid()) {
1458 d_fprintf(stderr, "Could not allocate a uid\n");
1462 case OPT_ALLOCATE_GID:
1463 if (!wbinfo_allocate_gid()) {
1464 d_fprintf(stderr, "Could not allocate a gid\n");
1469 if (!wbinfo_check_secret()) {
1470 d_fprintf(stderr, "Could not check secret\n");
1475 if (!wbinfo_list_domains(false)) {
1476 d_fprintf(stderr, "Could not list trusted domains\n");
1481 if (!wbinfo_show_sequence(opt_domain_name)) {
1482 d_fprintf(stderr, "Could not show sequence numbers\n");
1487 if (!wbinfo_domain_info(string_arg)) {
1488 d_fprintf(stderr, "Could not get domain info\n");
1493 if (!wbinfo_get_userinfo(string_arg)) {
1494 d_fprintf(stderr, "Could not get info for user %s\n",
1500 if ( !wbinfo_get_uidinfo(int_arg)) {
1501 d_fprintf(stderr, "Could not get info for uid "
1506 case OPT_GROUP_INFO:
1507 if ( !wbinfo_get_groupinfo(string_arg)) {
1508 d_fprintf(stderr, "Could not get info for "
1509 "group %s\n", string_arg);
1514 if (!wbinfo_get_usergroups(string_arg)) {
1515 d_fprintf(stderr, "Could not get groups for user %s\n",
1521 if (!wbinfo_get_usersids(string_arg)) {
1522 d_fprintf(stderr, "Could not get group SIDs for user SID %s\n",
1527 case OPT_USERDOMGROUPS:
1528 if (!wbinfo_get_userdomgroups(string_arg)) {
1529 d_fprintf(stderr, "Could not get user's domain groups "
1530 "for user SID %s\n", string_arg);
1535 bool got_error = false;
1537 if (!wbinfo_auth(string_arg)) {
1538 d_fprintf(stderr, "Could not authenticate user %s with "
1539 "plaintext password\n", string_arg);
1543 if (!wbinfo_auth_crap(string_arg)) {
1544 d_fprintf(stderr, "Could not authenticate user %s with "
1545 "challenge/response\n", string_arg);
1554 uint32 flags = WBFLAG_PAM_KRB5 |
1555 WBFLAG_PAM_CACHED_LOGIN |
1556 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
1557 WBFLAG_PAM_INFO3_TEXT;
1559 if (!wbinfo_auth_krb5(string_arg, "FILE", flags)) {
1560 d_fprintf(stderr, "Could not authenticate user [%s] with "
1561 "Kerberos (ccache: %s)\n", string_arg, "FILE");
1567 if (!wbinfo_klog(string_arg)) {
1568 d_fprintf(stderr, "Could not klog user\n");
1573 if (!wbinfo_ping()) {
1574 d_fprintf(stderr, "could not ping winbindd!\n");
1578 case OPT_SET_AUTH_USER:
1579 if (!wbinfo_set_auth_user(string_arg)) {
1583 case OPT_GET_AUTH_USER:
1584 wbinfo_get_auth_user();
1587 if (!wbinfo_getdcname(string_arg)) {
1591 case OPT_DSGETDCNAME:
1592 if (!wbinfo_dsgetdcname(string_arg, 0)) {
1596 case OPT_SEPARATOR: {
1597 const char sep = winbind_separator_int(true);
1601 d_printf("%c\n", sep);
1604 case OPT_LIST_ALL_DOMAINS:
1605 if (!wbinfo_list_domains(true)) {
1609 case OPT_LIST_OWN_DOMAIN:
1610 if (!wbinfo_list_own_domain()) {
1614 /* generic configuration options */
1615 case OPT_DOMAIN_NAME:
1618 d_fprintf(stderr, "Invalid option\n");
1619 poptPrintHelp(pc, stderr, 0);
1629 talloc_destroy(frame);
1631 poptFreeContext(pc);