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 static char winbind_separator_int(bool strict)
32 struct winbindd_response response;
39 ZERO_STRUCT(response);
41 /* Send off request */
43 if (winbindd_request_response(WINBINDD_INFO, NULL, &response) !=
45 d_fprintf(stderr, "could not obtain winbind separator!\n");
49 /* HACK: (this module should not call lp_ funtions) */
50 return *lp_winbind_separator();
53 sep = response.data.info.winbind_separator;
57 d_fprintf(stderr, "winbind separator was NULL!\n");
61 /* HACK: (this module should not call lp_ funtions) */
62 sep = *lp_winbind_separator();
68 static char winbind_separator(void)
70 return winbind_separator_int(false);
73 static const char *get_winbind_domain(void)
75 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
76 struct wbcDomainInfo *dinfo = NULL;
77 static fstring winbind_domain;
81 wbc_status = wbcDomainInfo(".", &dinfo);
83 if (!WBC_ERROR_IS_OK(wbc_status)) {
84 d_fprintf(stderr, "could not obtain winbind domain name!\n");
86 /* HACK: (this module should not call lp_ funtions) */
87 return lp_workgroup();
90 fstrcpy(winbind_domain, dinfo->short_name);
94 return winbind_domain;
97 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
98 form DOMAIN/user into a domain and a user */
100 static bool parse_wbinfo_domain_user(const char *domuser, fstring domain,
104 char *p = strchr(domuser,winbind_separator());
107 /* Maybe it was a UPN? */
108 if ((p = strchr(domuser, '@')) != NULL) {
110 fstrcpy(user, domuser);
114 fstrcpy(user, domuser);
115 fstrcpy(domain, get_winbind_domain());
120 fstrcpy(domain, domuser);
121 domain[PTR_DIFF(p, domuser)] = 0;
127 /* pull pwent info for a given user */
129 static bool wbinfo_get_userinfo(char *user)
131 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
132 struct passwd *pwd = NULL;
134 wbc_status = wbcGetpwnam(user, &pwd);
135 if (!WBC_ERROR_IS_OK(wbc_status)) {
139 d_printf("%s:%s:%d:%d:%s:%s:%s\n",
151 /* pull pwent info for a given uid */
152 static bool wbinfo_get_uidinfo(int uid)
154 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
155 struct passwd *pwd = NULL;
157 wbc_status = wbcGetpwuid(uid, &pwd);
158 if (!WBC_ERROR_IS_OK(wbc_status)) {
162 d_printf("%s:%s:%d:%d:%s:%s:%s\n",
174 /* pull grent for a given group */
175 static bool wbinfo_get_groupinfo(const char *group)
177 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
180 wbc_status = wbcGetgrnam(group, &grp);
181 if (!WBC_ERROR_IS_OK(wbc_status)) {
185 d_printf("%s:%s:%d\n",
195 /* List groups a user is a member of */
197 static bool wbinfo_get_usergroups(char *user)
199 struct winbindd_request request;
200 struct winbindd_response response;
204 ZERO_STRUCT(request);
205 ZERO_STRUCT(response);
209 fstrcpy(request.data.username, user);
211 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
213 if (result != NSS_STATUS_SUCCESS)
216 for (i = 0; i < response.data.num_entries; i++)
217 d_printf("%d\n", (int)((gid_t *)response.extra_data.data)[i]);
219 SAFE_FREE(response.extra_data.data);
225 /* List group SIDs a user SID is a member of */
226 static bool wbinfo_get_usersids(const char *user_sid_str)
228 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
231 struct wbcDomainSid user_sid, *sids = NULL;
235 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
236 if (!WBC_ERROR_IS_OK(wbc_status)) {
240 wbc_status = wbcLookupUserSids(&user_sid, false, &num_sids, &sids);
241 if (!WBC_ERROR_IS_OK(wbc_status)) {
245 for (i = 0; i < num_sids; i++) {
247 wbc_status = wbcSidToString(&sids[i], &str);
248 if (!WBC_ERROR_IS_OK(wbc_status)) {
252 d_printf("%s\n", str);
261 static bool wbinfo_get_userdomgroups(const char *user_sid_str)
263 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
266 struct wbcDomainSid user_sid, *sids = NULL;
270 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
271 if (!WBC_ERROR_IS_OK(wbc_status)) {
275 wbc_status = wbcLookupUserSids(&user_sid, true, &num_sids, &sids);
276 if (!WBC_ERROR_IS_OK(wbc_status)) {
280 for (i = 0; i < num_sids; i++) {
282 wbc_status = wbcSidToString(&sids[i], &str);
283 if (!WBC_ERROR_IS_OK(wbc_status)) {
287 d_printf("%s\n", str);
296 /* Convert NetBIOS name to IP */
298 static bool wbinfo_wins_byname(char *name)
300 struct winbindd_request request;
301 struct winbindd_response response;
303 ZERO_STRUCT(request);
304 ZERO_STRUCT(response);
308 fstrcpy(request.data.winsreq, name);
310 if (winbindd_request_response(WINBINDD_WINS_BYNAME, &request, &response) !=
311 NSS_STATUS_SUCCESS) {
315 /* Display response */
317 d_printf("%s\n", response.data.winsresp);
322 /* Convert IP to NetBIOS name */
324 static bool wbinfo_wins_byip(char *ip)
326 struct winbindd_request request;
327 struct winbindd_response response;
329 ZERO_STRUCT(request);
330 ZERO_STRUCT(response);
334 fstrcpy(request.data.winsreq, ip);
336 if (winbindd_request_response(WINBINDD_WINS_BYIP, &request, &response) !=
337 NSS_STATUS_SUCCESS) {
341 /* Display response */
343 d_printf("%s\n", response.data.winsresp);
348 /* List trusted domains */
350 static bool wbinfo_list_domains(bool list_all_domains)
352 struct winbindd_request request;
353 struct winbindd_response response;
355 ZERO_STRUCT(request);
356 ZERO_STRUCT(response);
360 request.data.list_all_domains = list_all_domains;
362 if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM, &request, &response) !=
366 /* Display response */
368 if (response.extra_data.data) {
369 const char *extra_data = (char *)response.extra_data.data;
372 TALLOC_CTX *frame = talloc_stackframe();
374 while(next_token_talloc(frame,&extra_data,&name,"\n")) {
375 p = strchr(name, '\\');
377 d_fprintf(stderr, "Got invalid response: %s\n",
380 SAFE_FREE(response.extra_data.data);
384 d_printf("%s\n", name);
387 SAFE_FREE(response.extra_data.data);
393 /* List own domain */
395 static bool wbinfo_list_own_domain(void)
397 d_printf("%s\n", get_winbind_domain());
402 /* show sequence numbers */
403 static bool wbinfo_show_sequence(const char *domain)
405 struct winbindd_request request;
406 struct winbindd_response response;
408 ZERO_STRUCT(response);
409 ZERO_STRUCT(request);
412 fstrcpy( request.domain_name, domain );
416 if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
420 /* Display response */
423 d_printf("%s : ", domain);
424 if (response.data.sequence_number == (uint32_t)-1) {
425 d_printf("DISCONNECTED\n");
427 d_printf("%d\n", response.data.sequence_number);
429 } else if (response.extra_data.data) {
430 char *extra_data = (char *)response.extra_data.data;
431 d_printf("%s", extra_data);
432 SAFE_FREE(response.extra_data.data);
438 /* Show domain info */
440 static bool wbinfo_domain_info(const char *domain)
442 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
443 struct wbcDomainInfo *dinfo = NULL;
444 char *sid_str = NULL;
446 if (strcmp(domain, ".") == 0 || domain[0] == '\0') {
447 domain = get_winbind_domain();
452 wbc_status = wbcDomainInfo(domain, &dinfo);
453 if (!WBC_ERROR_IS_OK(wbc_status)) {
457 wbc_status = wbcSidToString(&dinfo->sid, &sid_str);
458 if (!WBC_ERROR_IS_OK(wbc_status)) {
459 wbcFreeMemory(dinfo);
463 /* Display response */
465 d_printf("Name : %s\n", dinfo->short_name);
466 d_printf("Alt_Name : %s\n", dinfo->dns_name);
468 d_printf("SID : %s\n", sid_str);
470 d_printf("Active Directory : %s\n",
471 (dinfo->flags & WBC_DOMINFO_AD) ? "Yes" : "No");
472 d_printf("Native : %s\n",
473 (dinfo->flags & WBC_DOMINFO_NATIVE) ? "Yes" : "No");
475 d_printf("Primary : %s\n",
476 (dinfo->flags & WBC_DOMINFO_PRIMARY) ? "Yes" : "No");
478 wbcFreeMemory(sid_str);
479 wbcFreeMemory(dinfo);
484 /* Get a foreign DC's name */
485 static bool wbinfo_getdcname(const char *domain_name)
487 struct winbindd_request request;
488 struct winbindd_response response;
490 ZERO_STRUCT(request);
491 ZERO_STRUCT(response);
493 fstrcpy(request.domain_name, domain_name);
497 if (winbindd_request_response(WINBINDD_GETDCNAME, &request, &response) !=
498 NSS_STATUS_SUCCESS) {
499 d_fprintf(stderr, "Could not get dc name for %s\n", domain_name);
503 /* Display response */
505 d_printf("%s\n", response.data.dc_name);
511 static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
513 struct winbindd_request request;
514 struct winbindd_response response;
516 ZERO_STRUCT(request);
517 ZERO_STRUCT(response);
519 fstrcpy(request.domain_name, domain_name);
520 request.flags = flags;
522 request.flags |= DS_DIRECTORY_SERVICE_REQUIRED;
526 if (winbindd_request_response(WINBINDD_DSGETDCNAME, &request, &response) !=
527 NSS_STATUS_SUCCESS) {
528 d_fprintf(stderr, "Could not find dc for %s\n", domain_name);
532 /* Display response */
534 d_printf("%s\n", response.data.dc_name);
539 /* Check trust account password */
541 static bool wbinfo_check_secret(void)
543 struct winbindd_response response;
546 ZERO_STRUCT(response);
548 result = winbindd_request_response(WINBINDD_CHECK_MACHACC, NULL, &response);
550 d_printf("checking the trust secret via RPC calls %s\n",
551 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
553 if (result != NSS_STATUS_SUCCESS)
554 d_fprintf(stderr, "error code was %s (0x%x)\n",
555 response.data.auth.nt_status_string,
556 response.data.auth.nt_status);
558 return result == NSS_STATUS_SUCCESS;
561 /* Convert uid to sid */
563 static bool wbinfo_uid_to_sid(uid_t uid)
565 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
566 struct wbcDomainSid sid;
567 char *sid_str = NULL;
571 wbc_status = wbcUidToSid(uid, &sid);
572 if (!WBC_ERROR_IS_OK(wbc_status)) {
576 wbc_status = wbcSidToString(&sid, &sid_str);
577 if (!WBC_ERROR_IS_OK(wbc_status)) {
581 /* Display response */
583 d_printf("%s\n", sid_str);
585 wbcFreeMemory(sid_str);
590 /* Convert gid to sid */
592 static bool wbinfo_gid_to_sid(gid_t gid)
594 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
595 struct wbcDomainSid sid;
596 char *sid_str = NULL;
600 wbc_status = wbcGidToSid(gid, &sid);
601 if (!WBC_ERROR_IS_OK(wbc_status)) {
605 wbc_status = wbcSidToString(&sid, &sid_str);
606 if (!WBC_ERROR_IS_OK(wbc_status)) {
610 /* Display response */
612 d_printf("%s\n", sid_str);
614 wbcFreeMemory(sid_str);
619 /* Convert sid to uid */
621 static bool wbinfo_sid_to_uid(char *sid)
623 struct winbindd_request request;
624 struct winbindd_response response;
626 ZERO_STRUCT(request);
627 ZERO_STRUCT(response);
631 fstrcpy(request.data.sid, sid);
633 if (winbindd_request_response(WINBINDD_SID_TO_UID, &request, &response) !=
637 /* Display response */
639 d_printf("%d\n", (int)response.data.uid);
644 static bool wbinfo_sid_to_gid(char *sid)
646 struct winbindd_request request;
647 struct winbindd_response response;
649 ZERO_STRUCT(request);
650 ZERO_STRUCT(response);
654 fstrcpy(request.data.sid, sid);
656 if (winbindd_request_response(WINBINDD_SID_TO_GID, &request, &response) !=
660 /* Display response */
662 d_printf("%d\n", (int)response.data.gid);
667 static bool wbinfo_allocate_uid(void)
671 if (!winbind_allocate_uid(&uid))
674 d_printf("New uid: %d\n", uid);
679 static bool wbinfo_allocate_gid(void)
683 if (!winbind_allocate_gid(&gid))
686 d_printf("New gid: %d\n", gid);
691 /* Convert sid to string */
693 static bool wbinfo_lookupsid(char *sid)
695 struct winbindd_request request;
696 struct winbindd_response response;
698 ZERO_STRUCT(request);
699 ZERO_STRUCT(response);
701 /* Send off request */
703 fstrcpy(request.data.sid, sid);
705 if (winbindd_request_response(WINBINDD_LOOKUPSID, &request, &response) !=
709 /* Display response */
711 d_printf("%s%c%s %d\n", response.data.name.dom_name,
712 winbind_separator(), response.data.name.name,
713 response.data.name.type);
718 /* Lookup a list of RIDs */
720 static bool wbinfo_lookuprids(char *domain, char *arg)
729 enum lsa_SidType *types;
730 const char *domain_name;
732 struct winbindd_request request;
733 struct winbindd_response response;
735 ZERO_STRUCT(request);
736 ZERO_STRUCT(response);
738 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0'))
739 fstrcpy(request.domain_name, get_winbind_domain());
741 fstrcpy(request.domain_name, domain);
745 if (winbindd_request_response(WINBINDD_DOMAIN_INFO, &request, &response) !=
746 NSS_STATUS_SUCCESS) {
747 d_printf("Could not get domain sid for %s\n", request.domain_name);
751 if (!string_to_sid(&sid, response.data.domain_info.sid)) {
752 d_printf("Could not convert %s to sid\n", response.data.domain_info.sid);
756 mem_ctx = talloc_new(NULL);
757 if (mem_ctx == NULL) {
758 d_printf("talloc_new failed\n");
766 while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
767 uint32 rid = strtoul(ridstr, NULL, 10);
768 ADD_TO_ARRAY(mem_ctx, uint32, rid, &rids, &num_rids);
772 TALLOC_FREE(mem_ctx);
776 if (!winbind_lookup_rids(mem_ctx, &sid, num_rids, rids,
777 &domain_name, &names, &types)) {
778 d_printf("winbind_lookup_rids failed\n");
779 TALLOC_FREE(mem_ctx);
783 d_printf("Domain: %s\n", domain_name);
785 for (i=0; i<num_rids; i++) {
786 d_printf("%8d: %s (%s)\n", rids[i], names[i],
787 sid_type_lookup(types[i]));
790 TALLOC_FREE(mem_ctx);
794 /* Convert string to sid */
796 static bool wbinfo_lookupname(char *name)
798 struct winbindd_request request;
799 struct winbindd_response response;
801 /* Send off request */
803 ZERO_STRUCT(request);
804 ZERO_STRUCT(response);
806 parse_wbinfo_domain_user(name, request.data.name.dom_name,
807 request.data.name.name);
809 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
813 /* Display response */
815 d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
820 /* Authenticate a user with a plaintext password */
822 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
824 struct winbindd_request request;
825 struct winbindd_response response;
829 /* Send off request */
831 ZERO_STRUCT(request);
832 ZERO_STRUCT(response);
834 p = strchr(username, '%');
838 fstrcpy(request.data.auth.user, username);
839 fstrcpy(request.data.auth.pass, p + 1);
842 fstrcpy(request.data.auth.user, username);
844 request.flags = flags;
846 fstrcpy(request.data.auth.krb5_cc_type, cctype);
848 request.data.auth.uid = geteuid();
850 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
852 /* Display response */
854 d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n",
855 username, (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", cctype);
857 if (response.data.auth.nt_status)
858 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
859 response.data.auth.nt_status_string,
860 response.data.auth.nt_status,
861 response.data.auth.error_string);
863 if (result == NSS_STATUS_SUCCESS) {
865 if (request.flags & WBFLAG_PAM_INFO3_TEXT) {
866 if (response.data.auth.info3.user_flgs & NETLOGON_CACHED_ACCOUNT) {
867 d_printf("user_flgs: NETLOGON_CACHED_ACCOUNT\n");
871 if (response.data.auth.krb5ccname[0] != '\0') {
872 d_printf("credentials were put in: %s\n", response.data.auth.krb5ccname);
874 d_printf("no credentials cached\n");
878 return result == NSS_STATUS_SUCCESS;
881 /* Authenticate a user with a plaintext password */
883 static bool wbinfo_auth(char *username)
885 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
888 const char *password = NULL;
891 if ((s = SMB_STRDUP(username)) == NULL) {
895 if ((p = strchr(s, '%')) != NULL) {
905 wbc_status = wbcAuthenticateUser(name, password);
907 d_printf("plaintext password authentication %s\n",
908 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
911 if (response.data.auth.nt_status)
912 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
913 response.data.auth.nt_status_string,
914 response.data.auth.nt_status,
915 response.data.auth.error_string);
920 return WBC_ERROR_IS_OK(wbc_status);
923 /* Authenticate a user with a challenge/response */
925 static bool wbinfo_auth_crap(char *username)
927 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
928 struct wbcAuthUserParams params;
929 struct wbcAuthUserInfo *info = NULL;
930 struct wbcAuthErrorInfo *err = NULL;
931 DATA_BLOB lm = data_blob_null;
932 DATA_BLOB nt = data_blob_null;
938 p = strchr(username, '%');
942 fstrcpy(pass, p + 1);
945 parse_wbinfo_domain_user(username, name_domain, name_user);
947 params.account_name = name_user;
948 params.domain_name = name_domain;
949 params.workstation_name = NULL;
952 params.parameter_control= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
953 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
955 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
957 generate_random_buffer(params.password.response.challenge, 8);
959 if (lp_client_ntlmv2_auth()) {
960 DATA_BLOB server_chal;
961 DATA_BLOB names_blob;
963 server_chal = data_blob(params.password.response.challenge, 8);
965 /* Pretend this is a login to 'us', for blob purposes */
966 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
968 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
971 data_blob_free(&names_blob);
972 data_blob_free(&server_chal);
975 data_blob_free(&names_blob);
976 data_blob_free(&server_chal);
979 if (lp_client_lanman_auth()) {
981 lm = data_blob(NULL, 24);
982 ok = SMBencrypt(pass, params.password.response.challenge,
988 nt = data_blob(NULL, 24);
989 SMBNTencrypt(pass, params.password.response.challenge,
993 params.password.response.nt_length = nt.length;
994 params.password.response.nt_data = nt.data;
995 params.password.response.lm_length = lm.length;
996 params.password.response.lm_data = lm.data;
998 wbc_status = wbcAuthenticateUserEx(¶ms, &info, &err);
1000 /* Display response */
1002 d_printf("challenge/response password authentication %s\n",
1003 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1005 if (wbc_status == WBC_ERR_AUTH_ERROR) {
1006 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1009 err->display_string);
1011 } else if (WBC_ERROR_IS_OK(wbc_status)) {
1012 wbcFreeMemory(info);
1015 data_blob_free(&nt);
1016 data_blob_free(&lm);
1018 return WBC_ERROR_IS_OK(wbc_status);
1021 /* Authenticate a user with a plaintext password and set a token */
1023 static bool wbinfo_klog(char *username)
1025 struct winbindd_request request;
1026 struct winbindd_response response;
1030 /* Send off request */
1032 ZERO_STRUCT(request);
1033 ZERO_STRUCT(response);
1035 p = strchr(username, '%');
1039 fstrcpy(request.data.auth.user, username);
1040 fstrcpy(request.data.auth.pass, p + 1);
1043 fstrcpy(request.data.auth.user, username);
1044 fstrcpy(request.data.auth.pass, getpass("Password: "));
1047 request.flags |= WBFLAG_PAM_AFS_TOKEN;
1049 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
1051 /* Display response */
1053 d_printf("plaintext password authentication %s\n",
1054 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1056 if (response.data.auth.nt_status)
1057 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1058 response.data.auth.nt_status_string,
1059 response.data.auth.nt_status,
1060 response.data.auth.error_string);
1062 if (result != NSS_STATUS_SUCCESS)
1065 if (response.extra_data.data == NULL) {
1066 d_fprintf(stderr, "Did not get token data\n");
1070 if (!afs_settoken_str((char *)response.extra_data.data)) {
1071 d_fprintf(stderr, "Could not set token\n");
1075 d_printf("Successfully created AFS token\n");
1079 /* Print domain users */
1081 static bool print_domain_users(const char *domain)
1083 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1085 uint32_t num_users = 0;
1086 const char **users = NULL;
1088 /* Send request to winbind daemon */
1090 /* '.' is the special sign for our own domain */
1091 if (domain && strcmp(domain, ".") == 0) {
1092 domain = get_winbind_domain();
1095 wbc_status = wbcListUsers(domain, &num_users, &users);
1096 if (!WBC_ERROR_IS_OK(wbc_status)) {
1100 for (i=0; i < num_users; i++) {
1101 d_printf("%s\n", users[i]);
1104 wbcFreeMemory(users);
1109 /* Print domain groups */
1111 static bool print_domain_groups(const char *domain)
1113 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1115 uint32_t num_groups = 0;
1116 const char **groups = NULL;
1118 /* Send request to winbind daemon */
1120 /* '.' is the special sign for our own domain */
1121 if (domain && strcmp(domain, ".") == 0) {
1122 domain = get_winbind_domain();
1125 wbc_status = wbcListGroups(domain, &num_groups, &groups);
1126 if (!WBC_ERROR_IS_OK(wbc_status)) {
1130 for (i=0; i < num_groups; i++) {
1131 d_printf("%s\n", groups[i]);
1134 wbcFreeMemory(groups);
1139 /* Set the authorised user for winbindd access in secrets.tdb */
1141 static bool wbinfo_set_auth_user(char *username)
1143 const char *password;
1145 fstring user, domain;
1147 /* Separate into user and password */
1149 parse_wbinfo_domain_user(username, domain, user);
1151 p = strchr(user, '%');
1157 char *thepass = getpass("Password: ");
1164 /* Store or remove DOMAIN\username%password in secrets.tdb */
1170 if (!secrets_store(SECRETS_AUTH_USER, user,
1171 strlen(user) + 1)) {
1172 d_fprintf(stderr, "error storing username\n");
1176 /* We always have a domain name added by the
1177 parse_wbinfo_domain_user() function. */
1179 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
1180 strlen(domain) + 1)) {
1181 d_fprintf(stderr, "error storing domain name\n");
1186 secrets_delete(SECRETS_AUTH_USER);
1187 secrets_delete(SECRETS_AUTH_DOMAIN);
1192 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
1193 strlen(password) + 1)) {
1194 d_fprintf(stderr, "error storing password\n");
1199 secrets_delete(SECRETS_AUTH_PASSWORD);
1204 static void wbinfo_get_auth_user(void)
1206 char *user, *domain, *password;
1208 /* Lift data from secrets file */
1210 secrets_fetch_ipc_userpass(&user, &domain, &password);
1212 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
1216 SAFE_FREE(password);
1217 d_printf("No authorised user configured\n");
1221 /* Pretty print authorised user info */
1223 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
1224 user, password ? "%" : "", password ? password : "");
1228 SAFE_FREE(password);
1231 static bool wbinfo_ping(void)
1235 wbc_status = wbcPing();
1237 /* Display response */
1239 d_printf("Ping to winbindd %s\n",
1240 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1242 return WBC_ERROR_IS_OK(wbc_status);
1248 OPT_SET_AUTH_USER = 1000,
1259 OPT_LIST_ALL_DOMAINS,
1260 OPT_LIST_OWN_DOMAIN,
1265 int main(int argc, char **argv, char **envp)
1268 TALLOC_CTX *frame = talloc_stackframe();
1270 static char *string_arg;
1271 static char *opt_domain_name;
1275 struct poptOption long_options[] = {
1278 /* longName, shortName, argInfo, argPtr, value, descrip,
1281 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1282 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1283 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1284 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1285 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1286 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1287 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1288 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1289 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1290 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1291 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1292 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1293 "Get a new UID out of idmap" },
1294 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1295 "Get a new GID out of idmap" },
1296 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1297 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1298 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1299 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1300 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1301 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1302 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1303 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1304 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1305 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1306 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1307 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1308 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1309 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1310 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1311 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1312 "Get a DC name for a foreign domain", "domainname" },
1313 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
1314 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1315 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1316 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1317 #ifdef WITH_FAKE_KASERVER
1318 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1321 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1322 /* destroys wbinfo --help output */
1323 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1325 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1326 POPT_COMMON_CONFIGFILE
1331 /* Samba client initialisation */
1337 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1339 /* Parse command line options */
1342 poptPrintHelp(pc, stderr, 0);
1346 while((opt = poptGetNextOpt(pc)) != -1) {
1347 /* get the generic configuration parameters like --domain */
1350 poptFreeContext(pc);
1352 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, true)) {
1353 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1354 get_dyn_CONFIGFILE(), strerror(errno));
1363 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1364 POPT_CONTEXT_KEEP_FIRST);
1366 while((opt = poptGetNextOpt(pc)) != -1) {
1369 if (!print_domain_users(opt_domain_name)) {
1370 d_fprintf(stderr, "Error looking up domain users\n");
1375 if (!print_domain_groups(opt_domain_name)) {
1376 d_fprintf(stderr, "Error looking up domain groups\n");
1381 if (!wbinfo_lookupsid(string_arg)) {
1382 d_fprintf(stderr, "Could not lookup sid %s\n", string_arg);
1387 if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
1388 d_fprintf(stderr, "Could not lookup RIDs %s\n", string_arg);
1393 if (!wbinfo_lookupname(string_arg)) {
1394 d_fprintf(stderr, "Could not lookup name %s\n", string_arg);
1399 if (!wbinfo_wins_byname(string_arg)) {
1400 d_fprintf(stderr, "Could not lookup WINS by name %s\n", string_arg);
1405 if (!wbinfo_wins_byip(string_arg)) {
1406 d_fprintf(stderr, "Could not lookup WINS by IP %s\n", string_arg);
1411 if (!wbinfo_uid_to_sid(int_arg)) {
1412 d_fprintf(stderr, "Could not convert uid %d to sid\n", int_arg);
1417 if (!wbinfo_gid_to_sid(int_arg)) {
1418 d_fprintf(stderr, "Could not convert gid %d to sid\n",
1424 if (!wbinfo_sid_to_uid(string_arg)) {
1425 d_fprintf(stderr, "Could not convert sid %s to uid\n",
1431 if (!wbinfo_sid_to_gid(string_arg)) {
1432 d_fprintf(stderr, "Could not convert sid %s to gid\n",
1437 case OPT_ALLOCATE_UID:
1438 if (!wbinfo_allocate_uid()) {
1439 d_fprintf(stderr, "Could not allocate a uid\n");
1443 case OPT_ALLOCATE_GID:
1444 if (!wbinfo_allocate_gid()) {
1445 d_fprintf(stderr, "Could not allocate a gid\n");
1450 if (!wbinfo_check_secret()) {
1451 d_fprintf(stderr, "Could not check secret\n");
1456 if (!wbinfo_list_domains(false)) {
1457 d_fprintf(stderr, "Could not list trusted domains\n");
1462 if (!wbinfo_show_sequence(opt_domain_name)) {
1463 d_fprintf(stderr, "Could not show sequence numbers\n");
1468 if (!wbinfo_domain_info(string_arg)) {
1469 d_fprintf(stderr, "Could not get domain info\n");
1474 if (!wbinfo_get_userinfo(string_arg)) {
1475 d_fprintf(stderr, "Could not get info for user %s\n",
1481 if ( !wbinfo_get_uidinfo(int_arg)) {
1482 d_fprintf(stderr, "Could not get info for uid "
1487 case OPT_GROUP_INFO:
1488 if ( !wbinfo_get_groupinfo(string_arg)) {
1489 d_fprintf(stderr, "Could not get info for "
1490 "group %s\n", string_arg);
1495 if (!wbinfo_get_usergroups(string_arg)) {
1496 d_fprintf(stderr, "Could not get groups for user %s\n",
1502 if (!wbinfo_get_usersids(string_arg)) {
1503 d_fprintf(stderr, "Could not get group SIDs for user SID %s\n",
1508 case OPT_USERDOMGROUPS:
1509 if (!wbinfo_get_userdomgroups(string_arg)) {
1510 d_fprintf(stderr, "Could not get user's domain groups "
1511 "for user SID %s\n", string_arg);
1516 bool got_error = false;
1518 if (!wbinfo_auth(string_arg)) {
1519 d_fprintf(stderr, "Could not authenticate user %s with "
1520 "plaintext password\n", string_arg);
1524 if (!wbinfo_auth_crap(string_arg)) {
1525 d_fprintf(stderr, "Could not authenticate user %s with "
1526 "challenge/response\n", string_arg);
1535 uint32 flags = WBFLAG_PAM_KRB5 |
1536 WBFLAG_PAM_CACHED_LOGIN |
1537 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
1538 WBFLAG_PAM_INFO3_TEXT;
1540 if (!wbinfo_auth_krb5(string_arg, "FILE", flags)) {
1541 d_fprintf(stderr, "Could not authenticate user [%s] with "
1542 "Kerberos (ccache: %s)\n", string_arg, "FILE");
1548 if (!wbinfo_klog(string_arg)) {
1549 d_fprintf(stderr, "Could not klog user\n");
1554 if (!wbinfo_ping()) {
1555 d_fprintf(stderr, "could not ping winbindd!\n");
1559 case OPT_SET_AUTH_USER:
1560 if (!wbinfo_set_auth_user(string_arg)) {
1564 case OPT_GET_AUTH_USER:
1565 wbinfo_get_auth_user();
1568 if (!wbinfo_getdcname(string_arg)) {
1572 case OPT_DSGETDCNAME:
1573 if (!wbinfo_dsgetdcname(string_arg, 0)) {
1577 case OPT_SEPARATOR: {
1578 const char sep = winbind_separator_int(true);
1582 d_printf("%c\n", sep);
1585 case OPT_LIST_ALL_DOMAINS:
1586 if (!wbinfo_list_domains(true)) {
1590 case OPT_LIST_OWN_DOMAIN:
1591 if (!wbinfo_list_own_domain()) {
1595 /* generic configuration options */
1596 case OPT_DOMAIN_NAME:
1599 d_fprintf(stderr, "Invalid option\n");
1600 poptPrintHelp(pc, stderr, 0);
1610 talloc_destroy(frame);
1612 poptFreeContext(pc);