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(char *name
)
302 struct winbindd_request request
;
303 struct winbindd_response response
;
305 ZERO_STRUCT(request
);
306 ZERO_STRUCT(response
);
310 fstrcpy(request
.data
.winsreq
, name
);
312 if (winbindd_request_response(WINBINDD_WINS_BYNAME
, &request
, &response
) !=
313 NSS_STATUS_SUCCESS
) {
317 /* Display response */
319 d_printf("%s\n", response
.data
.winsresp
);
324 /* Convert IP to NetBIOS name */
326 static bool wbinfo_wins_byip(char *ip
)
328 struct winbindd_request request
;
329 struct winbindd_response response
;
331 ZERO_STRUCT(request
);
332 ZERO_STRUCT(response
);
336 fstrcpy(request
.data
.winsreq
, ip
);
338 if (winbindd_request_response(WINBINDD_WINS_BYIP
, &request
, &response
) !=
339 NSS_STATUS_SUCCESS
) {
343 /* Display response */
345 d_printf("%s\n", response
.data
.winsresp
);
350 /* List all/trusted domains */
352 static bool wbinfo_list_domains(bool list_all_domains
, bool verbose
)
354 struct winbindd_request request
;
355 struct winbindd_response response
;
357 bool print_all
= !list_all_domains
&& verbose
;
359 ZERO_STRUCT(request
);
360 ZERO_STRUCT(response
);
364 request
.data
.list_all_domains
= list_all_domains
;
366 if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM
, &request
, &response
) !=
370 /* Display response */
372 if (response
.extra_data
.data
) {
373 const char *extra_data
= (char *)response
.extra_data
.data
;
376 TALLOC_CTX
*frame
= talloc_stackframe();
379 d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n",
380 "Domain Name", "DNS Domain", "Trust Type",
381 "Transitive", "In", "Out");
384 while(next_token_talloc(frame
,&extra_data
,&name
,"\n")) {
385 /* Print Domain Name */
386 if ((beg
= strchr(name
, '\\')) == NULL
)
390 if ((end
= strchr(beg
, '\\')) == NULL
)
394 /* Print short name */
396 d_printf("%-16s", name
);
403 /* Print DNS domain */
406 d_printf("%-24s", beg
);
411 if ((end
= strchr(beg
, '\\')) == NULL
)
414 /* Print Trust Type */
416 if ((end
= strchr(beg
, '\\')) == NULL
)
419 d_printf("%-12s", beg
);
421 /* Print Transitive */
423 if ((end
= strchr(beg
, '\\')) == NULL
)
426 d_printf("%-12s", beg
);
430 if ((end
= strchr(beg
, '\\')) == NULL
)
433 d_printf("%-5s", beg
);
437 d_printf("%-5s\n", beg
);
442 d_fprintf(stderr
, "Got invalid response: %s\n", extra_data
);
444 SAFE_FREE(response
.extra_data
.data
);
448 SAFE_FREE(response
.extra_data
.data
);
454 /* List own domain */
456 static bool wbinfo_list_own_domain(void)
458 d_printf("%s\n", get_winbind_domain());
463 /* show sequence numbers */
464 static bool wbinfo_show_sequence(const char *domain
)
466 struct winbindd_request request
;
467 struct winbindd_response response
;
469 ZERO_STRUCT(response
);
470 ZERO_STRUCT(request
);
473 fstrcpy( request
.domain_name
, domain
);
477 if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE
, &request
, &response
) !=
481 /* Display response */
484 d_printf("%s : ", domain
);
485 if (response
.data
.sequence_number
== (uint32_t)-1) {
486 d_printf("DISCONNECTED\n");
488 d_printf("%d\n", response
.data
.sequence_number
);
490 } else if (response
.extra_data
.data
) {
491 char *extra_data
= (char *)response
.extra_data
.data
;
492 d_printf("%s", extra_data
);
493 SAFE_FREE(response
.extra_data
.data
);
499 /* Show domain info */
501 static bool wbinfo_domain_info(const char *domain
)
503 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
504 struct wbcDomainInfo
*dinfo
= NULL
;
505 char *sid_str
= NULL
;
507 if ((domain
== NULL
) || (strequal(domain
, ".")) || (domain
[0] == '\0')) {
508 domain
= get_winbind_domain();
513 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
514 if (!WBC_ERROR_IS_OK(wbc_status
)) {
518 wbc_status
= wbcSidToString(&dinfo
->sid
, &sid_str
);
519 if (!WBC_ERROR_IS_OK(wbc_status
)) {
520 wbcFreeMemory(dinfo
);
524 /* Display response */
526 d_printf("Name : %s\n", dinfo
->short_name
);
527 d_printf("Alt_Name : %s\n", dinfo
->dns_name
);
529 d_printf("SID : %s\n", sid_str
);
531 d_printf("Active Directory : %s\n",
532 (dinfo
->flags
& WBC_DOMINFO_AD
) ? "Yes" : "No");
533 d_printf("Native : %s\n",
534 (dinfo
->flags
& WBC_DOMINFO_NATIVE
) ? "Yes" : "No");
536 d_printf("Primary : %s\n",
537 (dinfo
->flags
& WBC_DOMINFO_PRIMARY
) ? "Yes" : "No");
539 wbcFreeMemory(sid_str
);
540 wbcFreeMemory(dinfo
);
545 /* Get a foreign DC's name */
546 static bool wbinfo_getdcname(const char *domain_name
)
548 struct winbindd_request request
;
549 struct winbindd_response response
;
551 ZERO_STRUCT(request
);
552 ZERO_STRUCT(response
);
554 fstrcpy(request
.domain_name
, domain_name
);
558 if (winbindd_request_response(WINBINDD_GETDCNAME
, &request
, &response
) !=
559 NSS_STATUS_SUCCESS
) {
560 d_fprintf(stderr
, "Could not get dc name for %s\n", domain_name
);
564 /* Display response */
566 d_printf("%s\n", response
.data
.dc_name
);
572 static bool wbinfo_dsgetdcname(const char *domain_name
, uint32_t flags
)
574 struct winbindd_request request
;
575 struct winbindd_response response
;
577 ZERO_STRUCT(request
);
578 ZERO_STRUCT(response
);
580 fstrcpy(request
.domain_name
, domain_name
);
581 request
.flags
= flags
;
583 request
.flags
|= DS_DIRECTORY_SERVICE_REQUIRED
;
587 if (winbindd_request_response(WINBINDD_DSGETDCNAME
, &request
, &response
) !=
588 NSS_STATUS_SUCCESS
) {
589 d_fprintf(stderr
, "Could not find dc for %s\n", domain_name
);
593 /* Display response */
595 d_printf("%s\n", response
.data
.dc_name
);
600 /* Check trust account password */
602 static bool wbinfo_check_secret(void)
604 struct winbindd_response response
;
607 ZERO_STRUCT(response
);
609 result
= winbindd_request_response(WINBINDD_CHECK_MACHACC
, NULL
, &response
);
611 d_printf("checking the trust secret via RPC calls %s\n",
612 (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed");
614 if (result
!= NSS_STATUS_SUCCESS
)
615 d_fprintf(stderr
, "error code was %s (0x%x)\n",
616 response
.data
.auth
.nt_status_string
,
617 response
.data
.auth
.nt_status
);
619 return result
== NSS_STATUS_SUCCESS
;
622 /* Convert uid to sid */
624 static bool wbinfo_uid_to_sid(uid_t uid
)
626 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
627 struct wbcDomainSid sid
;
628 char *sid_str
= NULL
;
632 wbc_status
= wbcUidToSid(uid
, &sid
);
633 if (!WBC_ERROR_IS_OK(wbc_status
)) {
637 wbc_status
= wbcSidToString(&sid
, &sid_str
);
638 if (!WBC_ERROR_IS_OK(wbc_status
)) {
642 /* Display response */
644 d_printf("%s\n", sid_str
);
646 wbcFreeMemory(sid_str
);
651 /* Convert gid to sid */
653 static bool wbinfo_gid_to_sid(gid_t gid
)
655 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
656 struct wbcDomainSid sid
;
657 char *sid_str
= NULL
;
661 wbc_status
= wbcGidToSid(gid
, &sid
);
662 if (!WBC_ERROR_IS_OK(wbc_status
)) {
666 wbc_status
= wbcSidToString(&sid
, &sid_str
);
667 if (!WBC_ERROR_IS_OK(wbc_status
)) {
671 /* Display response */
673 d_printf("%s\n", sid_str
);
675 wbcFreeMemory(sid_str
);
680 /* Convert sid to uid */
682 static bool wbinfo_sid_to_uid(const char *sid_str
)
684 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
685 struct wbcDomainSid sid
;
690 wbc_status
= wbcStringToSid(sid_str
, &sid
);
691 if (!WBC_ERROR_IS_OK(wbc_status
)) {
695 wbc_status
= wbcSidToUid(&sid
, &uid
);
696 if (!WBC_ERROR_IS_OK(wbc_status
)) {
700 /* Display response */
702 d_printf("%d\n", (int)uid
);
707 static bool wbinfo_sid_to_gid(const char *sid_str
)
709 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
710 struct wbcDomainSid sid
;
715 wbc_status
= wbcStringToSid(sid_str
, &sid
);
716 if (!WBC_ERROR_IS_OK(wbc_status
)) {
720 wbc_status
= wbcSidToGid(&sid
, &gid
);
721 if (!WBC_ERROR_IS_OK(wbc_status
)) {
725 /* Display response */
727 d_printf("%d\n", (int)gid
);
732 static bool wbinfo_allocate_uid(void)
734 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
739 wbc_status
= wbcAllocateUid(&uid
);
740 if (!WBC_ERROR_IS_OK(wbc_status
)) {
744 /* Display response */
746 d_printf("New uid: %d\n", uid
);
751 static bool wbinfo_allocate_gid(void)
753 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
758 wbc_status
= wbcAllocateGid(&gid
);
759 if (!WBC_ERROR_IS_OK(wbc_status
)) {
763 /* Display response */
765 d_printf("New gid: %d\n", gid
);
770 /* Convert sid to string */
772 static bool wbinfo_lookupsid(const char *sid_str
)
774 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
775 struct wbcDomainSid sid
;
778 enum wbcSidType type
;
780 /* Send off request */
782 wbc_status
= wbcStringToSid(sid_str
, &sid
);
783 if (!WBC_ERROR_IS_OK(wbc_status
)) {
787 wbc_status
= wbcLookupSid(&sid
, &domain
, &name
, &type
);
788 if (!WBC_ERROR_IS_OK(wbc_status
)) {
792 /* Display response */
794 d_printf("%s%c%s %d\n",
795 domain
, winbind_separator(), name
, type
);
800 /* Lookup a list of RIDs */
802 static bool wbinfo_lookuprids(const char *domain
, const char *arg
)
804 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
805 struct wbcDomainInfo
*dinfo
= NULL
;
806 char *domain_name
= NULL
;
807 const char **names
= NULL
;
808 enum wbcSidType
*types
= NULL
;
814 TALLOC_CTX
*mem_ctx
= NULL
;
817 if ((domain
== NULL
) || (strequal(domain
, ".")) || (domain
[0] == '\0')) {
818 domain
= get_winbind_domain();
823 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
824 if (!WBC_ERROR_IS_OK(wbc_status
)) {
825 d_printf("wbcDomainInfo(%s) failed: %s\n", domain
,
826 wbcErrorString(wbc_status
));
830 mem_ctx
= talloc_new(NULL
);
831 if (mem_ctx
== NULL
) {
832 d_printf("talloc_new failed\n");
840 while (next_token_talloc(mem_ctx
, &p
, &ridstr
, " ,\n")) {
841 uint32 rid
= strtoul(ridstr
, NULL
, 10);
842 ADD_TO_ARRAY(mem_ctx
, uint32
, rid
, &rids
, &num_rids
);
846 d_printf("no rids\n");
850 wbc_status
= wbcLookupRids(&dinfo
->sid
, num_rids
, rids
,
851 (const char **)&domain_name
, &names
, &types
);
852 if (!WBC_ERROR_IS_OK(wbc_status
)) {
853 d_printf("winbind_lookup_rids failed: %s\n",
854 wbcErrorString(wbc_status
));
858 d_printf("Domain: %s\n", domain_name
);
860 for (i
=0; i
<num_rids
; i
++) {
861 d_printf("%8d: %s (%s)\n", rids
[i
], names
[i
],
862 sid_type_lookup(types
[i
]));
868 wbcFreeMemory(dinfo
);
871 wbcFreeMemory(domain_name
);
874 wbcFreeMemory(names
);
877 wbcFreeMemory(types
);
879 TALLOC_FREE(mem_ctx
);
883 /* Convert string to sid */
885 static bool wbinfo_lookupname(const char *full_name
)
887 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
888 struct wbcDomainSid sid
;
890 enum wbcSidType type
;
892 fstring account_name
;
894 /* Send off request */
896 parse_wbinfo_domain_user(full_name
, domain_name
,
899 wbc_status
= wbcLookupName(domain_name
, account_name
,
901 if (!WBC_ERROR_IS_OK(wbc_status
)) {
905 wbc_status
= wbcSidToString(&sid
, &sid_str
);
906 if (!WBC_ERROR_IS_OK(wbc_status
)) {
910 /* Display response */
912 d_printf("%s %s (%d)\n", sid_str
, sid_type_lookup(type
), type
);
914 wbcFreeMemory(sid_str
);
919 /* Authenticate a user with a plaintext password */
921 static bool wbinfo_auth_krb5(char *username
, const char *cctype
, uint32 flags
)
923 struct winbindd_request request
;
924 struct winbindd_response response
;
928 /* Send off request */
930 ZERO_STRUCT(request
);
931 ZERO_STRUCT(response
);
933 p
= strchr(username
, '%');
937 fstrcpy(request
.data
.auth
.user
, username
);
938 fstrcpy(request
.data
.auth
.pass
, p
+ 1);
941 fstrcpy(request
.data
.auth
.user
, username
);
943 request
.flags
= flags
;
945 fstrcpy(request
.data
.auth
.krb5_cc_type
, cctype
);
947 request
.data
.auth
.uid
= geteuid();
949 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
, &response
);
951 /* Display response */
953 d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n",
954 username
, (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed", cctype
);
956 if (response
.data
.auth
.nt_status
)
957 d_fprintf(stderr
, "error code was %s (0x%x)\nerror messsage was: %s\n",
958 response
.data
.auth
.nt_status_string
,
959 response
.data
.auth
.nt_status
,
960 response
.data
.auth
.error_string
);
962 if (result
== NSS_STATUS_SUCCESS
) {
964 if (request
.flags
& WBFLAG_PAM_INFO3_TEXT
) {
965 if (response
.data
.auth
.info3
.user_flgs
& NETLOGON_CACHED_ACCOUNT
) {
966 d_printf("user_flgs: NETLOGON_CACHED_ACCOUNT\n");
970 if (response
.data
.auth
.krb5ccname
[0] != '\0') {
971 d_printf("credentials were put in: %s\n", response
.data
.auth
.krb5ccname
);
973 d_printf("no credentials cached\n");
977 return result
== NSS_STATUS_SUCCESS
;
980 /* Authenticate a user with a plaintext password */
982 static bool wbinfo_auth(char *username
)
984 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
987 const char *password
= NULL
;
990 if ((s
= SMB_STRDUP(username
)) == NULL
) {
994 if ((p
= strchr(s
, '%')) != NULL
) {
1004 wbc_status
= wbcAuthenticateUser(name
, password
);
1006 d_printf("plaintext password authentication %s\n",
1007 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1010 if (response
.data
.auth
.nt_status
)
1011 d_fprintf(stderr
, "error code was %s (0x%x)\nerror messsage was: %s\n",
1012 response
.data
.auth
.nt_status_string
,
1013 response
.data
.auth
.nt_status
,
1014 response
.data
.auth
.error_string
);
1019 return WBC_ERROR_IS_OK(wbc_status
);
1022 /* Authenticate a user with a challenge/response */
1024 static bool wbinfo_auth_crap(char *username
)
1026 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1027 struct wbcAuthUserParams params
;
1028 struct wbcAuthUserInfo
*info
= NULL
;
1029 struct wbcAuthErrorInfo
*err
= NULL
;
1030 DATA_BLOB lm
= data_blob_null
;
1031 DATA_BLOB nt
= data_blob_null
;
1033 fstring name_domain
;
1037 p
= strchr(username
, '%');
1041 fstrcpy(pass
, p
+ 1);
1044 parse_wbinfo_domain_user(username
, name_domain
, name_user
);
1046 params
.account_name
= name_user
;
1047 params
.domain_name
= name_domain
;
1048 params
.workstation_name
= NULL
;
1051 params
.parameter_control
= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
|
1052 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
;
1054 params
.level
= WBC_AUTH_USER_LEVEL_RESPONSE
;
1056 generate_random_buffer(params
.password
.response
.challenge
, 8);
1058 if (lp_client_ntlmv2_auth()) {
1059 DATA_BLOB server_chal
;
1060 DATA_BLOB names_blob
;
1062 server_chal
= data_blob(params
.password
.response
.challenge
, 8);
1064 /* Pretend this is a login to 'us', for blob purposes */
1065 names_blob
= NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
1067 if (!SMBNTLMv2encrypt(name_user
, name_domain
, pass
, &server_chal
,
1070 data_blob_free(&names_blob
);
1071 data_blob_free(&server_chal
);
1074 data_blob_free(&names_blob
);
1075 data_blob_free(&server_chal
);
1078 if (lp_client_lanman_auth()) {
1080 lm
= data_blob(NULL
, 24);
1081 ok
= SMBencrypt(pass
, params
.password
.response
.challenge
,
1084 data_blob_free(&lm
);
1087 nt
= data_blob(NULL
, 24);
1088 SMBNTencrypt(pass
, params
.password
.response
.challenge
,
1092 params
.password
.response
.nt_length
= nt
.length
;
1093 params
.password
.response
.nt_data
= nt
.data
;
1094 params
.password
.response
.lm_length
= lm
.length
;
1095 params
.password
.response
.lm_data
= lm
.data
;
1097 wbc_status
= wbcAuthenticateUserEx(¶ms
, &info
, &err
);
1099 /* Display response */
1101 d_printf("challenge/response password authentication %s\n",
1102 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1104 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
1105 d_fprintf(stderr
, "error code was %s (0x%x)\nerror messsage was: %s\n",
1108 err
->display_string
);
1110 } else if (WBC_ERROR_IS_OK(wbc_status
)) {
1111 wbcFreeMemory(info
);
1114 data_blob_free(&nt
);
1115 data_blob_free(&lm
);
1117 return WBC_ERROR_IS_OK(wbc_status
);
1120 /* Authenticate a user with a plaintext password and set a token */
1122 static bool wbinfo_klog(char *username
)
1124 struct winbindd_request request
;
1125 struct winbindd_response response
;
1129 /* Send off request */
1131 ZERO_STRUCT(request
);
1132 ZERO_STRUCT(response
);
1134 p
= strchr(username
, '%');
1138 fstrcpy(request
.data
.auth
.user
, username
);
1139 fstrcpy(request
.data
.auth
.pass
, p
+ 1);
1142 fstrcpy(request
.data
.auth
.user
, username
);
1143 fstrcpy(request
.data
.auth
.pass
, getpass("Password: "));
1146 request
.flags
|= WBFLAG_PAM_AFS_TOKEN
;
1148 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
, &response
);
1150 /* Display response */
1152 d_printf("plaintext password authentication %s\n",
1153 (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed");
1155 if (response
.data
.auth
.nt_status
)
1156 d_fprintf(stderr
, "error code was %s (0x%x)\nerror messsage was: %s\n",
1157 response
.data
.auth
.nt_status_string
,
1158 response
.data
.auth
.nt_status
,
1159 response
.data
.auth
.error_string
);
1161 if (result
!= NSS_STATUS_SUCCESS
)
1164 if (response
.extra_data
.data
== NULL
) {
1165 d_fprintf(stderr
, "Did not get token data\n");
1169 if (!afs_settoken_str((char *)response
.extra_data
.data
)) {
1170 d_fprintf(stderr
, "Could not set token\n");
1174 d_printf("Successfully created AFS token\n");
1178 /* Print domain users */
1180 static bool print_domain_users(const char *domain
)
1182 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1184 uint32_t num_users
= 0;
1185 const char **users
= NULL
;
1187 /* Send request to winbind daemon */
1189 /* '.' is the special sign for our own domain */
1190 if (domain
&& strcmp(domain
, ".") == 0) {
1191 domain
= get_winbind_domain();
1194 wbc_status
= wbcListUsers(domain
, &num_users
, &users
);
1195 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1199 for (i
=0; i
< num_users
; i
++) {
1200 d_printf("%s\n", users
[i
]);
1203 wbcFreeMemory(users
);
1208 /* Print domain groups */
1210 static bool print_domain_groups(const char *domain
)
1212 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1214 uint32_t num_groups
= 0;
1215 const char **groups
= NULL
;
1217 /* Send request to winbind daemon */
1219 /* '.' is the special sign for our own domain */
1220 if (domain
&& strcmp(domain
, ".") == 0) {
1221 domain
= get_winbind_domain();
1224 wbc_status
= wbcListGroups(domain
, &num_groups
, &groups
);
1225 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1229 for (i
=0; i
< num_groups
; i
++) {
1230 d_printf("%s\n", groups
[i
]);
1233 wbcFreeMemory(groups
);
1238 /* Set the authorised user for winbindd access in secrets.tdb */
1240 static bool wbinfo_set_auth_user(char *username
)
1242 const char *password
;
1244 fstring user
, domain
;
1246 /* Separate into user and password */
1248 parse_wbinfo_domain_user(username
, domain
, user
);
1250 p
= strchr(user
, '%');
1256 char *thepass
= getpass("Password: ");
1263 /* Store or remove DOMAIN\username%password in secrets.tdb */
1269 if (!secrets_store(SECRETS_AUTH_USER
, user
,
1270 strlen(user
) + 1)) {
1271 d_fprintf(stderr
, "error storing username\n");
1275 /* We always have a domain name added by the
1276 parse_wbinfo_domain_user() function. */
1278 if (!secrets_store(SECRETS_AUTH_DOMAIN
, domain
,
1279 strlen(domain
) + 1)) {
1280 d_fprintf(stderr
, "error storing domain name\n");
1285 secrets_delete(SECRETS_AUTH_USER
);
1286 secrets_delete(SECRETS_AUTH_DOMAIN
);
1291 if (!secrets_store(SECRETS_AUTH_PASSWORD
, password
,
1292 strlen(password
) + 1)) {
1293 d_fprintf(stderr
, "error storing password\n");
1298 secrets_delete(SECRETS_AUTH_PASSWORD
);
1303 static void wbinfo_get_auth_user(void)
1305 char *user
, *domain
, *password
;
1307 /* Lift data from secrets file */
1309 secrets_fetch_ipc_userpass(&user
, &domain
, &password
);
1311 if ((!user
|| !*user
) && (!domain
|| !*domain
) && (!password
|| !*password
)){
1315 SAFE_FREE(password
);
1316 d_printf("No authorised user configured\n");
1320 /* Pretty print authorised user info */
1322 d_printf("%s%s%s%s%s\n", domain
? domain
: "", domain
? lp_winbind_separator(): "",
1323 user
, password
? "%" : "", password
? password
: "");
1327 SAFE_FREE(password
);
1330 static bool wbinfo_ping(void)
1334 wbc_status
= wbcPing();
1336 /* Display response */
1338 d_printf("Ping to winbindd %s\n",
1339 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1341 return WBC_ERROR_IS_OK(wbc_status
);
1347 OPT_SET_AUTH_USER
= 1000,
1358 OPT_LIST_ALL_DOMAINS
,
1359 OPT_LIST_OWN_DOMAIN
,
1365 int main(int argc
, char **argv
, char **envp
)
1368 TALLOC_CTX
*frame
= talloc_stackframe();
1370 static char *string_arg
;
1371 static char *opt_domain_name
;
1374 bool verbose
= false;
1376 struct poptOption long_options
[] = {
1379 /* longName, shortName, argInfo, argPtr, value, descrip,
1382 { "domain-users", 'u', POPT_ARG_NONE
, 0, 'u', "Lists all domain users", "domain"},
1383 { "domain-groups", 'g', POPT_ARG_NONE
, 0, 'g', "Lists all domain groups", "domain" },
1384 { "WINS-by-name", 'N', POPT_ARG_STRING
, &string_arg
, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1385 { "WINS-by-ip", 'I', POPT_ARG_STRING
, &string_arg
, 'I', "Converts IP address to NetBIOS name", "IP" },
1386 { "name-to-sid", 'n', POPT_ARG_STRING
, &string_arg
, 'n', "Converts name to sid", "NAME" },
1387 { "sid-to-name", 's', POPT_ARG_STRING
, &string_arg
, 's', "Converts sid to name", "SID" },
1388 { "lookup-rids", 'R', POPT_ARG_STRING
, &string_arg
, 'R', "Converts RIDs to names", "RIDs" },
1389 { "uid-to-sid", 'U', POPT_ARG_INT
, &int_arg
, 'U', "Converts uid to sid" , "UID" },
1390 { "gid-to-sid", 'G', POPT_ARG_INT
, &int_arg
, 'G', "Converts gid to sid", "GID" },
1391 { "sid-to-uid", 'S', POPT_ARG_STRING
, &string_arg
, 'S', "Converts sid to uid", "SID" },
1392 { "sid-to-gid", 'Y', POPT_ARG_STRING
, &string_arg
, 'Y', "Converts sid to gid", "SID" },
1393 { "allocate-uid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_UID
,
1394 "Get a new UID out of idmap" },
1395 { "allocate-gid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_GID
,
1396 "Get a new GID out of idmap" },
1397 { "check-secret", 't', POPT_ARG_NONE
, 0, 't', "Check shared secret" },
1398 { "trusted-domains", 'm', POPT_ARG_NONE
, 0, 'm', "List trusted domains" },
1399 { "all-domains", 0, POPT_ARG_NONE
, 0, OPT_LIST_ALL_DOMAINS
, "List all domains (trusted and own domain)" },
1400 { "own-domain", 0, POPT_ARG_NONE
, 0, OPT_LIST_OWN_DOMAIN
, "List own domain" },
1401 { "sequence", 0, POPT_ARG_NONE
, 0, OPT_SEQUENCE
, "Show sequence numbers of all domains" },
1402 { "domain-info", 'D', POPT_ARG_STRING
, &string_arg
, 'D', "Show most of the info we have about the domain" },
1403 { "user-info", 'i', POPT_ARG_STRING
, &string_arg
, 'i', "Get user info", "USER" },
1404 { "uid-info", 0, POPT_ARG_INT
, &int_arg
, OPT_UID_INFO
, "Get user info from uid", "UID" },
1405 { "group-info", 0, POPT_ARG_STRING
, &string_arg
, OPT_GROUP_INFO
, "Get group info", "GROUP" },
1406 { "user-groups", 'r', POPT_ARG_STRING
, &string_arg
, 'r', "Get user groups", "USER" },
1407 { "user-domgroups", 0, POPT_ARG_STRING
, &string_arg
,
1408 OPT_USERDOMGROUPS
, "Get user domain groups", "SID" },
1409 { "user-sids", 0, POPT_ARG_STRING
, &string_arg
, OPT_USERSIDS
, "Get user group sids for user SID", "SID" },
1410 { "authenticate", 'a', POPT_ARG_STRING
, &string_arg
, 'a', "authenticate user", "user%password" },
1411 { "set-auth-user", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_AUTH_USER
, "Store user and password used by winbindd (root only)", "user%password" },
1412 { "getdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_GETDCNAME
,
1413 "Get a DC name for a foreign domain", "domainname" },
1414 { "dsgetdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_DSGETDCNAME
, "Find a DC for a domain", "domainname" },
1415 { "get-auth-user", 0, POPT_ARG_NONE
, NULL
, OPT_GET_AUTH_USER
, "Retrieve user and password used by winbindd (root only)", NULL
},
1416 { "ping", 'p', POPT_ARG_NONE
, 0, 'p', "Ping winbindd to see if it is alive" },
1417 { "domain", 0, POPT_ARG_STRING
, &opt_domain_name
, OPT_DOMAIN_NAME
, "Define to the domain to restrict operation", "domain" },
1418 #ifdef WITH_FAKE_KASERVER
1419 { "klog", 'k', POPT_ARG_STRING
, &string_arg
, 'k', "set an AFS token from winbind", "user%password" },
1422 { "krb5auth", 'K', POPT_ARG_STRING
, &string_arg
, 'K', "authenticate user using Kerberos", "user%password" },
1423 /* destroys wbinfo --help output */
1424 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1426 { "separator", 0, POPT_ARG_NONE
, 0, OPT_SEPARATOR
, "Get the active winbind separator", NULL
},
1427 { "verbose", 0, POPT_ARG_NONE
, 0, OPT_VERBOSE
, "Print additional information per command", NULL
},
1428 POPT_COMMON_CONFIGFILE
1433 /* Samba client initialisation */
1439 pc
= poptGetContext("wbinfo", argc
, (const char **)argv
, long_options
, 0);
1441 /* Parse command line options */
1444 poptPrintHelp(pc
, stderr
, 0);
1448 while((opt
= poptGetNextOpt(pc
)) != -1) {
1449 /* get the generic configuration parameters like --domain */
1457 poptFreeContext(pc
);
1459 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, true)) {
1460 d_fprintf(stderr
, "wbinfo: error opening config file %s. Error was %s\n",
1461 get_dyn_CONFIGFILE(), strerror(errno
));
1470 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
1471 POPT_CONTEXT_KEEP_FIRST
);
1473 while((opt
= poptGetNextOpt(pc
)) != -1) {
1476 if (!print_domain_users(opt_domain_name
)) {
1477 d_fprintf(stderr
, "Error looking up domain users\n");
1482 if (!print_domain_groups(opt_domain_name
)) {
1483 d_fprintf(stderr
, "Error looking up domain groups\n");
1488 if (!wbinfo_lookupsid(string_arg
)) {
1489 d_fprintf(stderr
, "Could not lookup sid %s\n", string_arg
);
1494 if (!wbinfo_lookuprids(opt_domain_name
, string_arg
)) {
1495 d_fprintf(stderr
, "Could not lookup RIDs %s\n", string_arg
);
1500 if (!wbinfo_lookupname(string_arg
)) {
1501 d_fprintf(stderr
, "Could not lookup name %s\n", string_arg
);
1506 if (!wbinfo_wins_byname(string_arg
)) {
1507 d_fprintf(stderr
, "Could not lookup WINS by name %s\n", string_arg
);
1512 if (!wbinfo_wins_byip(string_arg
)) {
1513 d_fprintf(stderr
, "Could not lookup WINS by IP %s\n", string_arg
);
1518 if (!wbinfo_uid_to_sid(int_arg
)) {
1519 d_fprintf(stderr
, "Could not convert uid %d to sid\n", int_arg
);
1524 if (!wbinfo_gid_to_sid(int_arg
)) {
1525 d_fprintf(stderr
, "Could not convert gid %d to sid\n",
1531 if (!wbinfo_sid_to_uid(string_arg
)) {
1532 d_fprintf(stderr
, "Could not convert sid %s to uid\n",
1538 if (!wbinfo_sid_to_gid(string_arg
)) {
1539 d_fprintf(stderr
, "Could not convert sid %s to gid\n",
1544 case OPT_ALLOCATE_UID
:
1545 if (!wbinfo_allocate_uid()) {
1546 d_fprintf(stderr
, "Could not allocate a uid\n");
1550 case OPT_ALLOCATE_GID
:
1551 if (!wbinfo_allocate_gid()) {
1552 d_fprintf(stderr
, "Could not allocate a gid\n");
1557 if (!wbinfo_check_secret()) {
1558 d_fprintf(stderr
, "Could not check secret\n");
1563 if (!wbinfo_list_domains(false, verbose
)) {
1564 d_fprintf(stderr
, "Could not list trusted domains\n");
1569 if (!wbinfo_show_sequence(opt_domain_name
)) {
1570 d_fprintf(stderr
, "Could not show sequence numbers\n");
1575 if (!wbinfo_domain_info(string_arg
)) {
1576 d_fprintf(stderr
, "Could not get domain info\n");
1581 if (!wbinfo_get_userinfo(string_arg
)) {
1582 d_fprintf(stderr
, "Could not get info for user %s\n",
1588 if ( !wbinfo_get_uidinfo(int_arg
)) {
1589 d_fprintf(stderr
, "Could not get info for uid "
1594 case OPT_GROUP_INFO
:
1595 if ( !wbinfo_get_groupinfo(string_arg
)) {
1596 d_fprintf(stderr
, "Could not get info for "
1597 "group %s\n", string_arg
);
1602 if (!wbinfo_get_usergroups(string_arg
)) {
1603 d_fprintf(stderr
, "Could not get groups for user %s\n",
1609 if (!wbinfo_get_usersids(string_arg
)) {
1610 d_fprintf(stderr
, "Could not get group SIDs for user SID %s\n",
1615 case OPT_USERDOMGROUPS
:
1616 if (!wbinfo_get_userdomgroups(string_arg
)) {
1617 d_fprintf(stderr
, "Could not get user's domain groups "
1618 "for user SID %s\n", string_arg
);
1623 bool got_error
= false;
1625 if (!wbinfo_auth(string_arg
)) {
1626 d_fprintf(stderr
, "Could not authenticate user %s with "
1627 "plaintext password\n", string_arg
);
1631 if (!wbinfo_auth_crap(string_arg
)) {
1632 d_fprintf(stderr
, "Could not authenticate user %s with "
1633 "challenge/response\n", string_arg
);
1642 uint32 flags
= WBFLAG_PAM_KRB5
|
1643 WBFLAG_PAM_CACHED_LOGIN
|
1644 WBFLAG_PAM_FALLBACK_AFTER_KRB5
|
1645 WBFLAG_PAM_INFO3_TEXT
;
1647 if (!wbinfo_auth_krb5(string_arg
, "FILE", flags
)) {
1648 d_fprintf(stderr
, "Could not authenticate user [%s] with "
1649 "Kerberos (ccache: %s)\n", string_arg
, "FILE");
1655 if (!wbinfo_klog(string_arg
)) {
1656 d_fprintf(stderr
, "Could not klog user\n");
1661 if (!wbinfo_ping()) {
1662 d_fprintf(stderr
, "could not ping winbindd!\n");
1666 case OPT_SET_AUTH_USER
:
1667 if (!wbinfo_set_auth_user(string_arg
)) {
1671 case OPT_GET_AUTH_USER
:
1672 wbinfo_get_auth_user();
1675 if (!wbinfo_getdcname(string_arg
)) {
1679 case OPT_DSGETDCNAME
:
1680 if (!wbinfo_dsgetdcname(string_arg
, 0)) {
1684 case OPT_SEPARATOR
: {
1685 const char sep
= winbind_separator_int(true);
1689 d_printf("%c\n", sep
);
1692 case OPT_LIST_ALL_DOMAINS
:
1693 if (!wbinfo_list_domains(true, verbose
)) {
1697 case OPT_LIST_OWN_DOMAIN
:
1698 if (!wbinfo_list_own_domain()) {
1702 /* generic configuration options */
1703 case OPT_DOMAIN_NAME
:
1708 d_fprintf(stderr
, "Invalid option\n");
1709 poptPrintHelp(pc
, stderr
, 0);
1719 talloc_destroy(frame
);
1721 poptFreeContext(pc
);