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 struct wbcInterfaceDetails *init_interface_details(void)
32 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
33 static struct wbcInterfaceDetails *details;
39 wbc_status = wbcInterfaceDetails(&details);
40 if (!WBC_ERROR_IS_OK(wbc_status)) {
41 d_fprintf(stderr, "could not obtain winbind interface details!\n");
47 static char winbind_separator_int(bool strict)
49 struct wbcInterfaceDetails *details;
56 details = init_interface_details();
59 d_fprintf(stderr, "could not obtain winbind separator!\n");
63 /* HACK: (this module should not call lp_ funtions) */
64 return *lp_winbind_separator();
67 sep = details->winbind_separator;
71 d_fprintf(stderr, "winbind separator was NULL!\n");
75 /* HACK: (this module should not call lp_ funtions) */
76 sep = *lp_winbind_separator();
82 static char winbind_separator(void)
84 return winbind_separator_int(false);
87 static const char *get_winbind_domain(void)
89 static struct wbcInterfaceDetails *details;
91 details = init_interface_details();
94 d_fprintf(stderr, "could not obtain winbind domain name!\n");
96 /* HACK: (this module should not call lp_ functions) */
97 return lp_workgroup();
100 return details->netbios_domain;
103 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
104 form DOMAIN/user into a domain and a user */
106 static bool parse_wbinfo_domain_user(const char *domuser, fstring domain,
110 char *p = strchr(domuser,winbind_separator());
113 /* Maybe it was a UPN? */
114 if ((p = strchr(domuser, '@')) != NULL) {
116 fstrcpy(user, domuser);
120 fstrcpy(user, domuser);
121 fstrcpy(domain, get_winbind_domain());
126 fstrcpy(domain, domuser);
127 domain[PTR_DIFF(p, domuser)] = 0;
133 /* pull pwent info for a given user */
135 static bool wbinfo_get_userinfo(char *user)
137 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
138 struct passwd *pwd = NULL;
140 wbc_status = wbcGetpwnam(user, &pwd);
141 if (!WBC_ERROR_IS_OK(wbc_status)) {
145 d_printf("%s:%s:%d:%d:%s:%s:%s\n",
157 /* pull pwent info for a given uid */
158 static bool wbinfo_get_uidinfo(int uid)
160 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
161 struct passwd *pwd = NULL;
163 wbc_status = wbcGetpwuid(uid, &pwd);
164 if (!WBC_ERROR_IS_OK(wbc_status)) {
168 d_printf("%s:%s:%d:%d:%s:%s:%s\n",
180 /* pull grent for a given group */
181 static bool wbinfo_get_groupinfo(const char *group)
183 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
186 wbc_status = wbcGetgrnam(group, &grp);
187 if (!WBC_ERROR_IS_OK(wbc_status)) {
191 d_printf("%s:%s:%d\n",
201 /* List groups a user is a member of */
203 static bool wbinfo_get_usergroups(const char *user)
205 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
208 gid_t *groups = NULL;
212 wbc_status = wbcGetGroups(user, &num_groups, &groups);
213 if (!WBC_ERROR_IS_OK(wbc_status)) {
217 for (i = 0; i < num_groups; i++) {
218 d_printf("%d\n", (int)groups[i]);
221 wbcFreeMemory(groups);
227 /* List group SIDs a user SID is a member of */
228 static bool wbinfo_get_usersids(const char *user_sid_str)
230 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
233 struct wbcDomainSid user_sid, *sids = NULL;
237 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
238 if (!WBC_ERROR_IS_OK(wbc_status)) {
242 wbc_status = wbcLookupUserSids(&user_sid, false, &num_sids, &sids);
243 if (!WBC_ERROR_IS_OK(wbc_status)) {
247 for (i = 0; i < num_sids; i++) {
249 wbc_status = wbcSidToString(&sids[i], &str);
250 if (!WBC_ERROR_IS_OK(wbc_status)) {
254 d_printf("%s\n", str);
263 static bool wbinfo_get_userdomgroups(const char *user_sid_str)
265 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
268 struct wbcDomainSid user_sid, *sids = NULL;
272 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
273 if (!WBC_ERROR_IS_OK(wbc_status)) {
277 wbc_status = wbcLookupUserSids(&user_sid, true, &num_sids, &sids);
278 if (!WBC_ERROR_IS_OK(wbc_status)) {
282 for (i = 0; i < num_sids; i++) {
284 wbc_status = wbcSidToString(&sids[i], &str);
285 if (!WBC_ERROR_IS_OK(wbc_status)) {
289 d_printf("%s\n", str);
298 /* Convert NetBIOS name to IP */
300 static bool wbinfo_wins_byname(const char *name)
302 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
303 const char *ip = NULL;
305 wbc_status = wbcResolveWinsByName(name, &ip);
306 if (!WBC_ERROR_IS_OK(wbc_status)) {
310 /* Display response */
312 d_printf("%s\n", ip);
319 /* Convert IP to NetBIOS name */
321 static bool wbinfo_wins_byip(const char *ip)
323 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
324 const char *name = NULL;
326 wbc_status = wbcResolveWinsByIP(ip, &name);
327 if (!WBC_ERROR_IS_OK(wbc_status)) {
331 /* Display response */
333 d_printf("%s\n", name);
340 /* List all/trusted domains */
342 static bool wbinfo_list_domains(bool list_all_domains, bool verbose)
344 struct winbindd_request request;
345 struct winbindd_response response;
347 bool print_all = !list_all_domains && verbose;
349 ZERO_STRUCT(request);
350 ZERO_STRUCT(response);
354 request.data.list_all_domains = list_all_domains;
356 if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM, &request, &response) !=
360 /* Display response */
362 if (response.extra_data.data) {
363 const char *extra_data = (char *)response.extra_data.data;
366 TALLOC_CTX *frame = talloc_stackframe();
369 d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n",
370 "Domain Name", "DNS Domain", "Trust Type",
371 "Transitive", "In", "Out");
374 while(next_token_talloc(frame,&extra_data,&name,"\n")) {
375 /* Print Domain Name */
376 if ((beg = strchr(name, '\\')) == NULL)
380 if ((end = strchr(beg, '\\')) == NULL)
384 /* Print short name */
386 d_printf("%-16s", name);
393 /* Print DNS domain */
396 d_printf("%-24s", beg);
401 if ((end = strchr(beg, '\\')) == NULL)
404 /* Print Trust Type */
406 if ((end = strchr(beg, '\\')) == NULL)
409 d_printf("%-12s", beg);
411 /* Print Transitive */
413 if ((end = strchr(beg, '\\')) == NULL)
416 d_printf("%-12s", beg);
420 if ((end = strchr(beg, '\\')) == NULL)
423 d_printf("%-5s", beg);
427 d_printf("%-5s\n", beg);
432 d_fprintf(stderr, "Got invalid response: %s\n", extra_data);
434 SAFE_FREE(response.extra_data.data);
438 SAFE_FREE(response.extra_data.data);
444 /* List own domain */
446 static bool wbinfo_list_own_domain(void)
448 d_printf("%s\n", get_winbind_domain());
453 /* show sequence numbers */
454 static bool wbinfo_show_sequence(const char *domain)
456 struct winbindd_request request;
457 struct winbindd_response response;
459 ZERO_STRUCT(response);
460 ZERO_STRUCT(request);
463 fstrcpy( request.domain_name, domain );
467 if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
471 /* Display response */
474 d_printf("%s : ", domain);
475 if (response.data.sequence_number == (uint32_t)-1) {
476 d_printf("DISCONNECTED\n");
478 d_printf("%d\n", response.data.sequence_number);
480 } else if (response.extra_data.data) {
481 char *extra_data = (char *)response.extra_data.data;
482 d_printf("%s", extra_data);
483 SAFE_FREE(response.extra_data.data);
489 /* Show domain info */
491 static bool wbinfo_domain_info(const char *domain)
493 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
494 struct wbcDomainInfo *dinfo = NULL;
495 char *sid_str = NULL;
497 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')) {
498 domain = get_winbind_domain();
503 wbc_status = wbcDomainInfo(domain, &dinfo);
504 if (!WBC_ERROR_IS_OK(wbc_status)) {
508 wbc_status = wbcSidToString(&dinfo->sid, &sid_str);
509 if (!WBC_ERROR_IS_OK(wbc_status)) {
510 wbcFreeMemory(dinfo);
514 /* Display response */
516 d_printf("Name : %s\n", dinfo->short_name);
517 d_printf("Alt_Name : %s\n", dinfo->dns_name);
519 d_printf("SID : %s\n", sid_str);
521 d_printf("Active Directory : %s\n",
522 (dinfo->flags & WBC_DOMINFO_AD) ? "Yes" : "No");
523 d_printf("Native : %s\n",
524 (dinfo->flags & WBC_DOMINFO_NATIVE) ? "Yes" : "No");
526 d_printf("Primary : %s\n",
527 (dinfo->flags & WBC_DOMINFO_PRIMARY) ? "Yes" : "No");
529 wbcFreeMemory(sid_str);
530 wbcFreeMemory(dinfo);
535 /* Get a foreign DC's name */
536 static bool wbinfo_getdcname(const char *domain_name)
538 struct winbindd_request request;
539 struct winbindd_response response;
541 ZERO_STRUCT(request);
542 ZERO_STRUCT(response);
544 fstrcpy(request.domain_name, domain_name);
548 if (winbindd_request_response(WINBINDD_GETDCNAME, &request, &response) !=
549 NSS_STATUS_SUCCESS) {
550 d_fprintf(stderr, "Could not get dc name for %s\n", domain_name);
554 /* Display response */
556 d_printf("%s\n", response.data.dc_name);
562 static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
564 struct winbindd_request request;
565 struct winbindd_response response;
567 ZERO_STRUCT(request);
568 ZERO_STRUCT(response);
570 fstrcpy(request.domain_name, domain_name);
571 request.flags = flags;
573 request.flags |= DS_DIRECTORY_SERVICE_REQUIRED;
577 if (winbindd_request_response(WINBINDD_DSGETDCNAME, &request, &response) !=
578 NSS_STATUS_SUCCESS) {
579 d_fprintf(stderr, "Could not find dc for %s\n", domain_name);
583 /* Display response */
585 d_printf("%s\n", response.data.dc_name);
590 /* Check trust account password */
592 static bool wbinfo_check_secret(void)
594 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
595 struct wbcAuthErrorInfo *error = NULL;
597 wbc_status = wbcCheckTrustCredentials(NULL, &error);
599 d_printf("checking the trust secret via RPC calls %s\n",
600 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
602 if (wbc_status == WBC_ERR_AUTH_ERROR) {
603 d_fprintf(stderr, "error code was %s (0x%x)\n",
604 error->nt_string, error->nt_status);
605 wbcFreeMemory(error);
607 if (!WBC_ERROR_IS_OK(wbc_status)) {
614 /* Convert uid to sid */
616 static bool wbinfo_uid_to_sid(uid_t uid)
618 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
619 struct wbcDomainSid sid;
620 char *sid_str = NULL;
624 wbc_status = wbcUidToSid(uid, &sid);
625 if (!WBC_ERROR_IS_OK(wbc_status)) {
629 wbc_status = wbcSidToString(&sid, &sid_str);
630 if (!WBC_ERROR_IS_OK(wbc_status)) {
634 /* Display response */
636 d_printf("%s\n", sid_str);
638 wbcFreeMemory(sid_str);
643 /* Convert gid to sid */
645 static bool wbinfo_gid_to_sid(gid_t gid)
647 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
648 struct wbcDomainSid sid;
649 char *sid_str = NULL;
653 wbc_status = wbcGidToSid(gid, &sid);
654 if (!WBC_ERROR_IS_OK(wbc_status)) {
658 wbc_status = wbcSidToString(&sid, &sid_str);
659 if (!WBC_ERROR_IS_OK(wbc_status)) {
663 /* Display response */
665 d_printf("%s\n", sid_str);
667 wbcFreeMemory(sid_str);
672 /* Convert sid to uid */
674 static bool wbinfo_sid_to_uid(const char *sid_str)
676 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
677 struct wbcDomainSid sid;
682 wbc_status = wbcStringToSid(sid_str, &sid);
683 if (!WBC_ERROR_IS_OK(wbc_status)) {
687 wbc_status = wbcSidToUid(&sid, &uid);
688 if (!WBC_ERROR_IS_OK(wbc_status)) {
692 /* Display response */
694 d_printf("%d\n", (int)uid);
699 static bool wbinfo_sid_to_gid(const char *sid_str)
701 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
702 struct wbcDomainSid sid;
707 wbc_status = wbcStringToSid(sid_str, &sid);
708 if (!WBC_ERROR_IS_OK(wbc_status)) {
712 wbc_status = wbcSidToGid(&sid, &gid);
713 if (!WBC_ERROR_IS_OK(wbc_status)) {
717 /* Display response */
719 d_printf("%d\n", (int)gid);
724 static bool wbinfo_allocate_uid(void)
726 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
731 wbc_status = wbcAllocateUid(&uid);
732 if (!WBC_ERROR_IS_OK(wbc_status)) {
736 /* Display response */
738 d_printf("New uid: %d\n", uid);
743 static bool wbinfo_allocate_gid(void)
745 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
750 wbc_status = wbcAllocateGid(&gid);
751 if (!WBC_ERROR_IS_OK(wbc_status)) {
755 /* Display response */
757 d_printf("New gid: %d\n", gid);
762 /* Convert sid to string */
764 static bool wbinfo_lookupsid(const char *sid_str)
766 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
767 struct wbcDomainSid sid;
770 enum wbcSidType type;
772 /* Send off request */
774 wbc_status = wbcStringToSid(sid_str, &sid);
775 if (!WBC_ERROR_IS_OK(wbc_status)) {
779 wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
780 if (!WBC_ERROR_IS_OK(wbc_status)) {
784 /* Display response */
786 d_printf("%s%c%s %d\n",
787 domain, winbind_separator(), name, type);
792 /* Lookup a list of RIDs */
794 static bool wbinfo_lookuprids(const char *domain, const char *arg)
796 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
797 struct wbcDomainInfo *dinfo = NULL;
798 char *domain_name = NULL;
799 const char **names = NULL;
800 enum wbcSidType *types = NULL;
806 TALLOC_CTX *mem_ctx = NULL;
809 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')) {
810 domain = get_winbind_domain();
815 wbc_status = wbcDomainInfo(domain, &dinfo);
816 if (!WBC_ERROR_IS_OK(wbc_status)) {
817 d_printf("wbcDomainInfo(%s) failed: %s\n", domain,
818 wbcErrorString(wbc_status));
822 mem_ctx = talloc_new(NULL);
823 if (mem_ctx == NULL) {
824 d_printf("talloc_new failed\n");
832 while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
833 uint32 rid = strtoul(ridstr, NULL, 10);
834 ADD_TO_ARRAY(mem_ctx, uint32, rid, &rids, &num_rids);
838 d_printf("no rids\n");
842 wbc_status = wbcLookupRids(&dinfo->sid, num_rids, rids,
843 (const char **)&domain_name, &names, &types);
844 if (!WBC_ERROR_IS_OK(wbc_status)) {
845 d_printf("winbind_lookup_rids failed: %s\n",
846 wbcErrorString(wbc_status));
850 d_printf("Domain: %s\n", domain_name);
852 for (i=0; i<num_rids; i++) {
853 d_printf("%8d: %s (%s)\n", rids[i], names[i],
854 sid_type_lookup(types[i]));
860 wbcFreeMemory(dinfo);
863 wbcFreeMemory(domain_name);
866 wbcFreeMemory(names);
869 wbcFreeMemory(types);
871 TALLOC_FREE(mem_ctx);
875 /* Convert string to sid */
877 static bool wbinfo_lookupname(const char *full_name)
879 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
880 struct wbcDomainSid sid;
882 enum wbcSidType type;
884 fstring account_name;
886 /* Send off request */
888 parse_wbinfo_domain_user(full_name, domain_name,
891 wbc_status = wbcLookupName(domain_name, account_name,
893 if (!WBC_ERROR_IS_OK(wbc_status)) {
897 wbc_status = wbcSidToString(&sid, &sid_str);
898 if (!WBC_ERROR_IS_OK(wbc_status)) {
902 /* Display response */
904 d_printf("%s %s (%d)\n", sid_str, sid_type_lookup(type), type);
906 wbcFreeMemory(sid_str);
911 /* Authenticate a user with a plaintext password */
913 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
915 struct winbindd_request request;
916 struct winbindd_response response;
920 /* Send off request */
922 ZERO_STRUCT(request);
923 ZERO_STRUCT(response);
925 p = strchr(username, '%');
929 fstrcpy(request.data.auth.user, username);
930 fstrcpy(request.data.auth.pass, p + 1);
933 fstrcpy(request.data.auth.user, username);
935 request.flags = flags;
937 fstrcpy(request.data.auth.krb5_cc_type, cctype);
939 request.data.auth.uid = geteuid();
941 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
943 /* Display response */
945 d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n",
946 username, (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", cctype);
948 if (response.data.auth.nt_status)
949 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
950 response.data.auth.nt_status_string,
951 response.data.auth.nt_status,
952 response.data.auth.error_string);
954 if (result == NSS_STATUS_SUCCESS) {
956 if (request.flags & WBFLAG_PAM_INFO3_TEXT) {
957 if (response.data.auth.info3.user_flgs & NETLOGON_CACHED_ACCOUNT) {
958 d_printf("user_flgs: NETLOGON_CACHED_ACCOUNT\n");
962 if (response.data.auth.krb5ccname[0] != '\0') {
963 d_printf("credentials were put in: %s\n", response.data.auth.krb5ccname);
965 d_printf("no credentials cached\n");
969 return result == NSS_STATUS_SUCCESS;
972 /* Authenticate a user with a plaintext password */
974 static bool wbinfo_auth(char *username)
976 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
979 const char *password = NULL;
982 if ((s = SMB_STRDUP(username)) == NULL) {
986 if ((p = strchr(s, '%')) != NULL) {
996 wbc_status = wbcAuthenticateUser(name, password);
998 d_printf("plaintext password authentication %s\n",
999 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1002 if (response.data.auth.nt_status)
1003 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1004 response.data.auth.nt_status_string,
1005 response.data.auth.nt_status,
1006 response.data.auth.error_string);
1011 return WBC_ERROR_IS_OK(wbc_status);
1014 /* Authenticate a user with a challenge/response */
1016 static bool wbinfo_auth_crap(char *username)
1018 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1019 struct wbcAuthUserParams params;
1020 struct wbcAuthUserInfo *info = NULL;
1021 struct wbcAuthErrorInfo *err = NULL;
1022 DATA_BLOB lm = data_blob_null;
1023 DATA_BLOB nt = data_blob_null;
1025 fstring name_domain;
1029 p = strchr(username, '%');
1033 fstrcpy(pass, p + 1);
1036 parse_wbinfo_domain_user(username, name_domain, name_user);
1038 params.account_name = name_user;
1039 params.domain_name = name_domain;
1040 params.workstation_name = NULL;
1043 params.parameter_control= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
1044 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
1046 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
1048 generate_random_buffer(params.password.response.challenge, 8);
1050 if (lp_client_ntlmv2_auth()) {
1051 DATA_BLOB server_chal;
1052 DATA_BLOB names_blob;
1054 server_chal = data_blob(params.password.response.challenge, 8);
1056 /* Pretend this is a login to 'us', for blob purposes */
1057 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
1059 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
1062 data_blob_free(&names_blob);
1063 data_blob_free(&server_chal);
1066 data_blob_free(&names_blob);
1067 data_blob_free(&server_chal);
1070 if (lp_client_lanman_auth()) {
1072 lm = data_blob(NULL, 24);
1073 ok = SMBencrypt(pass, params.password.response.challenge,
1076 data_blob_free(&lm);
1079 nt = data_blob(NULL, 24);
1080 SMBNTencrypt(pass, params.password.response.challenge,
1084 params.password.response.nt_length = nt.length;
1085 params.password.response.nt_data = nt.data;
1086 params.password.response.lm_length = lm.length;
1087 params.password.response.lm_data = lm.data;
1089 wbc_status = wbcAuthenticateUserEx(¶ms, &info, &err);
1091 /* Display response */
1093 d_printf("challenge/response password authentication %s\n",
1094 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1096 if (wbc_status == WBC_ERR_AUTH_ERROR) {
1097 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1100 err->display_string);
1102 } else if (WBC_ERROR_IS_OK(wbc_status)) {
1103 wbcFreeMemory(info);
1106 data_blob_free(&nt);
1107 data_blob_free(&lm);
1109 return WBC_ERROR_IS_OK(wbc_status);
1112 /* Authenticate a user with a plaintext password and set a token */
1114 static bool wbinfo_klog(char *username)
1116 struct winbindd_request request;
1117 struct winbindd_response response;
1121 /* Send off request */
1123 ZERO_STRUCT(request);
1124 ZERO_STRUCT(response);
1126 p = strchr(username, '%');
1130 fstrcpy(request.data.auth.user, username);
1131 fstrcpy(request.data.auth.pass, p + 1);
1134 fstrcpy(request.data.auth.user, username);
1135 fstrcpy(request.data.auth.pass, getpass("Password: "));
1138 request.flags |= WBFLAG_PAM_AFS_TOKEN;
1140 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
1142 /* Display response */
1144 d_printf("plaintext password authentication %s\n",
1145 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1147 if (response.data.auth.nt_status)
1148 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1149 response.data.auth.nt_status_string,
1150 response.data.auth.nt_status,
1151 response.data.auth.error_string);
1153 if (result != NSS_STATUS_SUCCESS)
1156 if (response.extra_data.data == NULL) {
1157 d_fprintf(stderr, "Did not get token data\n");
1161 if (!afs_settoken_str((char *)response.extra_data.data)) {
1162 d_fprintf(stderr, "Could not set token\n");
1166 d_printf("Successfully created AFS token\n");
1170 /* Print domain users */
1172 static bool print_domain_users(const char *domain)
1174 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1176 uint32_t num_users = 0;
1177 const char **users = NULL;
1179 /* Send request to winbind daemon */
1181 /* '.' is the special sign for our own domain */
1182 if (domain && strcmp(domain, ".") == 0) {
1183 domain = get_winbind_domain();
1186 wbc_status = wbcListUsers(domain, &num_users, &users);
1187 if (!WBC_ERROR_IS_OK(wbc_status)) {
1191 for (i=0; i < num_users; i++) {
1192 d_printf("%s\n", users[i]);
1195 wbcFreeMemory(users);
1200 /* Print domain groups */
1202 static bool print_domain_groups(const char *domain)
1204 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1206 uint32_t num_groups = 0;
1207 const char **groups = NULL;
1209 /* Send request to winbind daemon */
1211 /* '.' is the special sign for our own domain */
1212 if (domain && strcmp(domain, ".") == 0) {
1213 domain = get_winbind_domain();
1216 wbc_status = wbcListGroups(domain, &num_groups, &groups);
1217 if (!WBC_ERROR_IS_OK(wbc_status)) {
1221 for (i=0; i < num_groups; i++) {
1222 d_printf("%s\n", groups[i]);
1225 wbcFreeMemory(groups);
1230 /* Set the authorised user for winbindd access in secrets.tdb */
1232 static bool wbinfo_set_auth_user(char *username)
1234 const char *password;
1236 fstring user, domain;
1238 /* Separate into user and password */
1240 parse_wbinfo_domain_user(username, domain, user);
1242 p = strchr(user, '%');
1248 char *thepass = getpass("Password: ");
1255 /* Store or remove DOMAIN\username%password in secrets.tdb */
1261 if (!secrets_store(SECRETS_AUTH_USER, user,
1262 strlen(user) + 1)) {
1263 d_fprintf(stderr, "error storing username\n");
1267 /* We always have a domain name added by the
1268 parse_wbinfo_domain_user() function. */
1270 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
1271 strlen(domain) + 1)) {
1272 d_fprintf(stderr, "error storing domain name\n");
1277 secrets_delete(SECRETS_AUTH_USER);
1278 secrets_delete(SECRETS_AUTH_DOMAIN);
1283 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
1284 strlen(password) + 1)) {
1285 d_fprintf(stderr, "error storing password\n");
1290 secrets_delete(SECRETS_AUTH_PASSWORD);
1295 static void wbinfo_get_auth_user(void)
1297 char *user, *domain, *password;
1299 /* Lift data from secrets file */
1301 secrets_fetch_ipc_userpass(&user, &domain, &password);
1303 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
1307 SAFE_FREE(password);
1308 d_printf("No authorised user configured\n");
1312 /* Pretty print authorised user info */
1314 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
1315 user, password ? "%" : "", password ? password : "");
1319 SAFE_FREE(password);
1322 static bool wbinfo_ping(void)
1326 wbc_status = wbcPing();
1328 /* Display response */
1330 d_printf("Ping to winbindd %s\n",
1331 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1333 return WBC_ERROR_IS_OK(wbc_status);
1339 OPT_SET_AUTH_USER = 1000,
1350 OPT_LIST_ALL_DOMAINS,
1351 OPT_LIST_OWN_DOMAIN,
1357 int main(int argc, char **argv, char **envp)
1360 TALLOC_CTX *frame = talloc_stackframe();
1362 static char *string_arg;
1363 static char *opt_domain_name;
1366 bool verbose = false;
1368 struct poptOption long_options[] = {
1371 /* longName, shortName, argInfo, argPtr, value, descrip,
1374 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1375 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1376 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1377 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1378 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1379 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1380 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1381 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1382 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1383 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1384 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1385 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1386 "Get a new UID out of idmap" },
1387 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1388 "Get a new GID out of idmap" },
1389 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1390 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1391 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1392 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1393 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1394 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1395 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1396 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1397 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1398 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1399 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1400 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1401 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1402 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1403 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1404 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1405 "Get a DC name for a foreign domain", "domainname" },
1406 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
1407 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1408 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1409 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1410 #ifdef WITH_FAKE_KASERVER
1411 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1414 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1415 /* destroys wbinfo --help output */
1416 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1418 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1419 { "verbose", 0, POPT_ARG_NONE, 0, OPT_VERBOSE, "Print additional information per command", NULL },
1420 POPT_COMMON_CONFIGFILE
1425 /* Samba client initialisation */
1431 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1433 /* Parse command line options */
1436 poptPrintHelp(pc, stderr, 0);
1440 while((opt = poptGetNextOpt(pc)) != -1) {
1441 /* get the generic configuration parameters like --domain */
1449 poptFreeContext(pc);
1451 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, true)) {
1452 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1453 get_dyn_CONFIGFILE(), strerror(errno));
1462 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1463 POPT_CONTEXT_KEEP_FIRST);
1465 while((opt = poptGetNextOpt(pc)) != -1) {
1468 if (!print_domain_users(opt_domain_name)) {
1469 d_fprintf(stderr, "Error looking up domain users\n");
1474 if (!print_domain_groups(opt_domain_name)) {
1475 d_fprintf(stderr, "Error looking up domain groups\n");
1480 if (!wbinfo_lookupsid(string_arg)) {
1481 d_fprintf(stderr, "Could not lookup sid %s\n", string_arg);
1486 if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
1487 d_fprintf(stderr, "Could not lookup RIDs %s\n", string_arg);
1492 if (!wbinfo_lookupname(string_arg)) {
1493 d_fprintf(stderr, "Could not lookup name %s\n", string_arg);
1498 if (!wbinfo_wins_byname(string_arg)) {
1499 d_fprintf(stderr, "Could not lookup WINS by name %s\n", string_arg);
1504 if (!wbinfo_wins_byip(string_arg)) {
1505 d_fprintf(stderr, "Could not lookup WINS by IP %s\n", string_arg);
1510 if (!wbinfo_uid_to_sid(int_arg)) {
1511 d_fprintf(stderr, "Could not convert uid %d to sid\n", int_arg);
1516 if (!wbinfo_gid_to_sid(int_arg)) {
1517 d_fprintf(stderr, "Could not convert gid %d to sid\n",
1523 if (!wbinfo_sid_to_uid(string_arg)) {
1524 d_fprintf(stderr, "Could not convert sid %s to uid\n",
1530 if (!wbinfo_sid_to_gid(string_arg)) {
1531 d_fprintf(stderr, "Could not convert sid %s to gid\n",
1536 case OPT_ALLOCATE_UID:
1537 if (!wbinfo_allocate_uid()) {
1538 d_fprintf(stderr, "Could not allocate a uid\n");
1542 case OPT_ALLOCATE_GID:
1543 if (!wbinfo_allocate_gid()) {
1544 d_fprintf(stderr, "Could not allocate a gid\n");
1549 if (!wbinfo_check_secret()) {
1550 d_fprintf(stderr, "Could not check secret\n");
1555 if (!wbinfo_list_domains(false, verbose)) {
1556 d_fprintf(stderr, "Could not list trusted domains\n");
1561 if (!wbinfo_show_sequence(opt_domain_name)) {
1562 d_fprintf(stderr, "Could not show sequence numbers\n");
1567 if (!wbinfo_domain_info(string_arg)) {
1568 d_fprintf(stderr, "Could not get domain info\n");
1573 if (!wbinfo_get_userinfo(string_arg)) {
1574 d_fprintf(stderr, "Could not get info for user %s\n",
1580 if ( !wbinfo_get_uidinfo(int_arg)) {
1581 d_fprintf(stderr, "Could not get info for uid "
1586 case OPT_GROUP_INFO:
1587 if ( !wbinfo_get_groupinfo(string_arg)) {
1588 d_fprintf(stderr, "Could not get info for "
1589 "group %s\n", string_arg);
1594 if (!wbinfo_get_usergroups(string_arg)) {
1595 d_fprintf(stderr, "Could not get groups for user %s\n",
1601 if (!wbinfo_get_usersids(string_arg)) {
1602 d_fprintf(stderr, "Could not get group SIDs for user SID %s\n",
1607 case OPT_USERDOMGROUPS:
1608 if (!wbinfo_get_userdomgroups(string_arg)) {
1609 d_fprintf(stderr, "Could not get user's domain groups "
1610 "for user SID %s\n", string_arg);
1615 bool got_error = false;
1617 if (!wbinfo_auth(string_arg)) {
1618 d_fprintf(stderr, "Could not authenticate user %s with "
1619 "plaintext password\n", string_arg);
1623 if (!wbinfo_auth_crap(string_arg)) {
1624 d_fprintf(stderr, "Could not authenticate user %s with "
1625 "challenge/response\n", string_arg);
1634 uint32 flags = WBFLAG_PAM_KRB5 |
1635 WBFLAG_PAM_CACHED_LOGIN |
1636 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
1637 WBFLAG_PAM_INFO3_TEXT;
1639 if (!wbinfo_auth_krb5(string_arg, "FILE", flags)) {
1640 d_fprintf(stderr, "Could not authenticate user [%s] with "
1641 "Kerberos (ccache: %s)\n", string_arg, "FILE");
1647 if (!wbinfo_klog(string_arg)) {
1648 d_fprintf(stderr, "Could not klog user\n");
1653 if (!wbinfo_ping()) {
1654 d_fprintf(stderr, "could not ping winbindd!\n");
1658 case OPT_SET_AUTH_USER:
1659 if (!wbinfo_set_auth_user(string_arg)) {
1663 case OPT_GET_AUTH_USER:
1664 wbinfo_get_auth_user();
1667 if (!wbinfo_getdcname(string_arg)) {
1671 case OPT_DSGETDCNAME:
1672 if (!wbinfo_dsgetdcname(string_arg, 0)) {
1676 case OPT_SEPARATOR: {
1677 const char sep = winbind_separator_int(true);
1681 d_printf("%c\n", sep);
1684 case OPT_LIST_ALL_DOMAINS:
1685 if (!wbinfo_list_domains(true, verbose)) {
1689 case OPT_LIST_OWN_DOMAIN:
1690 if (!wbinfo_list_own_domain()) {
1694 /* generic configuration options */
1695 case OPT_DOMAIN_NAME:
1700 d_fprintf(stderr, "Invalid option\n");
1701 poptPrintHelp(pc, stderr, 0);
1711 talloc_destroy(frame);
1713 poptFreeContext(pc);