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(char *user
)
205 struct winbindd_request request
;
206 struct winbindd_response response
;
210 ZERO_STRUCT(request
);
211 ZERO_STRUCT(response
);
215 fstrcpy(request
.data
.username
, user
);
217 result
= winbindd_request_response(WINBINDD_GETGROUPS
, &request
, &response
);
219 if (result
!= NSS_STATUS_SUCCESS
)
222 for (i
= 0; i
< response
.data
.num_entries
; i
++)
223 d_printf("%d\n", (int)((gid_t
*)response
.extra_data
.data
)[i
]);
225 SAFE_FREE(response
.extra_data
.data
);
231 /* List group SIDs a user SID is a member of */
232 static bool wbinfo_get_usersids(const char *user_sid_str
)
234 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
237 struct wbcDomainSid user_sid
, *sids
= NULL
;
241 wbc_status
= wbcStringToSid(user_sid_str
, &user_sid
);
242 if (!WBC_ERROR_IS_OK(wbc_status
)) {
246 wbc_status
= wbcLookupUserSids(&user_sid
, false, &num_sids
, &sids
);
247 if (!WBC_ERROR_IS_OK(wbc_status
)) {
251 for (i
= 0; i
< num_sids
; i
++) {
253 wbc_status
= wbcSidToString(&sids
[i
], &str
);
254 if (!WBC_ERROR_IS_OK(wbc_status
)) {
258 d_printf("%s\n", str
);
267 static bool wbinfo_get_userdomgroups(const char *user_sid_str
)
269 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
272 struct wbcDomainSid user_sid
, *sids
= NULL
;
276 wbc_status
= wbcStringToSid(user_sid_str
, &user_sid
);
277 if (!WBC_ERROR_IS_OK(wbc_status
)) {
281 wbc_status
= wbcLookupUserSids(&user_sid
, true, &num_sids
, &sids
);
282 if (!WBC_ERROR_IS_OK(wbc_status
)) {
286 for (i
= 0; i
< num_sids
; i
++) {
288 wbc_status
= wbcSidToString(&sids
[i
], &str
);
289 if (!WBC_ERROR_IS_OK(wbc_status
)) {
293 d_printf("%s\n", str
);
302 /* Convert NetBIOS name to IP */
304 static bool wbinfo_wins_byname(char *name
)
306 struct winbindd_request request
;
307 struct winbindd_response response
;
309 ZERO_STRUCT(request
);
310 ZERO_STRUCT(response
);
314 fstrcpy(request
.data
.winsreq
, name
);
316 if (winbindd_request_response(WINBINDD_WINS_BYNAME
, &request
, &response
) !=
317 NSS_STATUS_SUCCESS
) {
321 /* Display response */
323 d_printf("%s\n", response
.data
.winsresp
);
328 /* Convert IP to NetBIOS name */
330 static bool wbinfo_wins_byip(char *ip
)
332 struct winbindd_request request
;
333 struct winbindd_response response
;
335 ZERO_STRUCT(request
);
336 ZERO_STRUCT(response
);
340 fstrcpy(request
.data
.winsreq
, ip
);
342 if (winbindd_request_response(WINBINDD_WINS_BYIP
, &request
, &response
) !=
343 NSS_STATUS_SUCCESS
) {
347 /* Display response */
349 d_printf("%s\n", response
.data
.winsresp
);
354 /* List all/trusted domains */
356 static bool wbinfo_list_domains(bool list_all_domains
, bool verbose
)
358 struct winbindd_request request
;
359 struct winbindd_response response
;
361 bool print_all
= !list_all_domains
&& verbose
;
363 ZERO_STRUCT(request
);
364 ZERO_STRUCT(response
);
368 request
.data
.list_all_domains
= list_all_domains
;
370 if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM
, &request
, &response
) !=
374 /* Display response */
376 if (response
.extra_data
.data
) {
377 const char *extra_data
= (char *)response
.extra_data
.data
;
380 TALLOC_CTX
*frame
= talloc_stackframe();
383 d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n",
384 "Domain Name", "DNS Domain", "Trust Type",
385 "Transitive", "In", "Out");
388 while(next_token_talloc(frame
,&extra_data
,&name
,"\n")) {
389 /* Print Domain Name */
390 if ((beg
= strchr(name
, '\\')) == NULL
)
394 if ((end
= strchr(beg
, '\\')) == NULL
)
398 /* Print short name */
400 d_printf("%-16s", name
);
407 /* Print DNS domain */
410 d_printf("%-24s", beg
);
415 if ((end
= strchr(beg
, '\\')) == NULL
)
418 /* Print Trust Type */
420 if ((end
= strchr(beg
, '\\')) == NULL
)
423 d_printf("%-12s", beg
);
425 /* Print Transitive */
427 if ((end
= strchr(beg
, '\\')) == NULL
)
430 d_printf("%-12s", beg
);
434 if ((end
= strchr(beg
, '\\')) == NULL
)
437 d_printf("%-5s", beg
);
441 d_printf("%-5s\n", beg
);
446 d_fprintf(stderr
, "Got invalid response: %s\n", extra_data
);
448 SAFE_FREE(response
.extra_data
.data
);
452 SAFE_FREE(response
.extra_data
.data
);
458 /* List own domain */
460 static bool wbinfo_list_own_domain(void)
462 d_printf("%s\n", get_winbind_domain());
467 /* show sequence numbers */
468 static bool wbinfo_show_sequence(const char *domain
)
470 struct winbindd_request request
;
471 struct winbindd_response response
;
473 ZERO_STRUCT(response
);
474 ZERO_STRUCT(request
);
477 fstrcpy( request
.domain_name
, domain
);
481 if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE
, &request
, &response
) !=
485 /* Display response */
488 d_printf("%s : ", domain
);
489 if (response
.data
.sequence_number
== (uint32_t)-1) {
490 d_printf("DISCONNECTED\n");
492 d_printf("%d\n", response
.data
.sequence_number
);
494 } else if (response
.extra_data
.data
) {
495 char *extra_data
= (char *)response
.extra_data
.data
;
496 d_printf("%s", extra_data
);
497 SAFE_FREE(response
.extra_data
.data
);
503 /* Show domain info */
505 static bool wbinfo_domain_info(const char *domain
)
507 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
508 struct wbcDomainInfo
*dinfo
= NULL
;
509 char *sid_str
= NULL
;
511 if ((domain
== NULL
) || (strequal(domain
, ".")) || (domain
[0] == '\0')) {
512 domain
= get_winbind_domain();
517 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
518 if (!WBC_ERROR_IS_OK(wbc_status
)) {
522 wbc_status
= wbcSidToString(&dinfo
->sid
, &sid_str
);
523 if (!WBC_ERROR_IS_OK(wbc_status
)) {
524 wbcFreeMemory(dinfo
);
528 /* Display response */
530 d_printf("Name : %s\n", dinfo
->short_name
);
531 d_printf("Alt_Name : %s\n", dinfo
->dns_name
);
533 d_printf("SID : %s\n", sid_str
);
535 d_printf("Active Directory : %s\n",
536 (dinfo
->flags
& WBC_DOMINFO_AD
) ? "Yes" : "No");
537 d_printf("Native : %s\n",
538 (dinfo
->flags
& WBC_DOMINFO_NATIVE
) ? "Yes" : "No");
540 d_printf("Primary : %s\n",
541 (dinfo
->flags
& WBC_DOMINFO_PRIMARY
) ? "Yes" : "No");
543 wbcFreeMemory(sid_str
);
544 wbcFreeMemory(dinfo
);
549 /* Get a foreign DC's name */
550 static bool wbinfo_getdcname(const char *domain_name
)
552 struct winbindd_request request
;
553 struct winbindd_response response
;
555 ZERO_STRUCT(request
);
556 ZERO_STRUCT(response
);
558 fstrcpy(request
.domain_name
, domain_name
);
562 if (winbindd_request_response(WINBINDD_GETDCNAME
, &request
, &response
) !=
563 NSS_STATUS_SUCCESS
) {
564 d_fprintf(stderr
, "Could not get dc name for %s\n", domain_name
);
568 /* Display response */
570 d_printf("%s\n", response
.data
.dc_name
);
576 static bool wbinfo_dsgetdcname(const char *domain_name
, uint32_t flags
)
578 struct winbindd_request request
;
579 struct winbindd_response response
;
581 ZERO_STRUCT(request
);
582 ZERO_STRUCT(response
);
584 fstrcpy(request
.domain_name
, domain_name
);
585 request
.flags
= flags
;
587 request
.flags
|= DS_DIRECTORY_SERVICE_REQUIRED
;
591 if (winbindd_request_response(WINBINDD_DSGETDCNAME
, &request
, &response
) !=
592 NSS_STATUS_SUCCESS
) {
593 d_fprintf(stderr
, "Could not find dc for %s\n", domain_name
);
597 /* Display response */
599 d_printf("%s\n", response
.data
.dc_name
);
604 /* Check trust account password */
606 static bool wbinfo_check_secret(void)
608 struct winbindd_response response
;
611 ZERO_STRUCT(response
);
613 result
= winbindd_request_response(WINBINDD_CHECK_MACHACC
, NULL
, &response
);
615 d_printf("checking the trust secret via RPC calls %s\n",
616 (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed");
618 if (result
!= NSS_STATUS_SUCCESS
)
619 d_fprintf(stderr
, "error code was %s (0x%x)\n",
620 response
.data
.auth
.nt_status_string
,
621 response
.data
.auth
.nt_status
);
623 return result
== NSS_STATUS_SUCCESS
;
626 /* Convert uid to sid */
628 static bool wbinfo_uid_to_sid(uid_t uid
)
630 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
631 struct wbcDomainSid sid
;
632 char *sid_str
= NULL
;
636 wbc_status
= wbcUidToSid(uid
, &sid
);
637 if (!WBC_ERROR_IS_OK(wbc_status
)) {
641 wbc_status
= wbcSidToString(&sid
, &sid_str
);
642 if (!WBC_ERROR_IS_OK(wbc_status
)) {
646 /* Display response */
648 d_printf("%s\n", sid_str
);
650 wbcFreeMemory(sid_str
);
655 /* Convert gid to sid */
657 static bool wbinfo_gid_to_sid(gid_t gid
)
659 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
660 struct wbcDomainSid sid
;
661 char *sid_str
= NULL
;
665 wbc_status
= wbcGidToSid(gid
, &sid
);
666 if (!WBC_ERROR_IS_OK(wbc_status
)) {
670 wbc_status
= wbcSidToString(&sid
, &sid_str
);
671 if (!WBC_ERROR_IS_OK(wbc_status
)) {
675 /* Display response */
677 d_printf("%s\n", sid_str
);
679 wbcFreeMemory(sid_str
);
684 /* Convert sid to uid */
686 static bool wbinfo_sid_to_uid(const char *sid_str
)
688 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
689 struct wbcDomainSid sid
;
694 wbc_status
= wbcStringToSid(sid_str
, &sid
);
695 if (!WBC_ERROR_IS_OK(wbc_status
)) {
699 wbc_status
= wbcSidToUid(&sid
, &uid
);
700 if (!WBC_ERROR_IS_OK(wbc_status
)) {
704 /* Display response */
706 d_printf("%d\n", (int)uid
);
711 static bool wbinfo_sid_to_gid(const char *sid_str
)
713 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
714 struct wbcDomainSid sid
;
719 wbc_status
= wbcStringToSid(sid_str
, &sid
);
720 if (!WBC_ERROR_IS_OK(wbc_status
)) {
724 wbc_status
= wbcSidToGid(&sid
, &gid
);
725 if (!WBC_ERROR_IS_OK(wbc_status
)) {
729 /* Display response */
731 d_printf("%d\n", (int)gid
);
736 static bool wbinfo_allocate_uid(void)
738 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
743 wbc_status
= wbcAllocateUid(&uid
);
744 if (!WBC_ERROR_IS_OK(wbc_status
)) {
748 /* Display response */
750 d_printf("New uid: %d\n", uid
);
755 static bool wbinfo_allocate_gid(void)
757 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
762 wbc_status
= wbcAllocateGid(&gid
);
763 if (!WBC_ERROR_IS_OK(wbc_status
)) {
767 /* Display response */
769 d_printf("New gid: %d\n", gid
);
774 /* Convert sid to string */
776 static bool wbinfo_lookupsid(const char *sid_str
)
778 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
779 struct wbcDomainSid sid
;
782 enum wbcSidType type
;
784 /* Send off request */
786 wbc_status
= wbcStringToSid(sid_str
, &sid
);
787 if (!WBC_ERROR_IS_OK(wbc_status
)) {
791 wbc_status
= wbcLookupSid(&sid
, &domain
, &name
, &type
);
792 if (!WBC_ERROR_IS_OK(wbc_status
)) {
796 /* Display response */
798 d_printf("%s%c%s %d\n",
799 domain
, winbind_separator(), name
, type
);
804 /* Lookup a list of RIDs */
806 static bool wbinfo_lookuprids(char *domain
, char *arg
)
815 enum lsa_SidType
*types
;
816 const char *domain_name
;
818 struct winbindd_request request
;
819 struct winbindd_response response
;
821 ZERO_STRUCT(request
);
822 ZERO_STRUCT(response
);
824 if ((domain
== NULL
) || (strequal(domain
, ".")) || (domain
[0] == '\0'))
825 fstrcpy(request
.domain_name
, get_winbind_domain());
827 fstrcpy(request
.domain_name
, domain
);
831 if (winbindd_request_response(WINBINDD_DOMAIN_INFO
, &request
, &response
) !=
832 NSS_STATUS_SUCCESS
) {
833 d_printf("Could not get domain sid for %s\n", request
.domain_name
);
837 if (!string_to_sid(&sid
, response
.data
.domain_info
.sid
)) {
838 d_printf("Could not convert %s to sid\n", response
.data
.domain_info
.sid
);
842 mem_ctx
= talloc_new(NULL
);
843 if (mem_ctx
== NULL
) {
844 d_printf("talloc_new failed\n");
852 while (next_token_talloc(mem_ctx
, &p
, &ridstr
, " ,\n")) {
853 uint32 rid
= strtoul(ridstr
, NULL
, 10);
854 ADD_TO_ARRAY(mem_ctx
, uint32
, rid
, &rids
, &num_rids
);
858 TALLOC_FREE(mem_ctx
);
862 if (!winbind_lookup_rids(mem_ctx
, &sid
, num_rids
, rids
,
863 &domain_name
, &names
, &types
)) {
864 d_printf("winbind_lookup_rids failed\n");
865 TALLOC_FREE(mem_ctx
);
869 d_printf("Domain: %s\n", domain_name
);
871 for (i
=0; i
<num_rids
; i
++) {
872 d_printf("%8d: %s (%s)\n", rids
[i
], names
[i
],
873 sid_type_lookup(types
[i
]));
876 TALLOC_FREE(mem_ctx
);
880 /* Convert string to sid */
882 static bool wbinfo_lookupname(char *name
)
884 struct winbindd_request request
;
885 struct winbindd_response response
;
887 /* Send off request */
889 ZERO_STRUCT(request
);
890 ZERO_STRUCT(response
);
892 parse_wbinfo_domain_user(name
, request
.data
.name
.dom_name
,
893 request
.data
.name
.name
);
895 if (winbindd_request_response(WINBINDD_LOOKUPNAME
, &request
, &response
) !=
899 /* Display response */
901 d_printf("%s %s (%d)\n", response
.data
.sid
.sid
, sid_type_lookup(response
.data
.sid
.type
), response
.data
.sid
.type
);
906 /* Authenticate a user with a plaintext password */
908 static bool wbinfo_auth_krb5(char *username
, const char *cctype
, uint32 flags
)
910 struct winbindd_request request
;
911 struct winbindd_response response
;
915 /* Send off request */
917 ZERO_STRUCT(request
);
918 ZERO_STRUCT(response
);
920 p
= strchr(username
, '%');
924 fstrcpy(request
.data
.auth
.user
, username
);
925 fstrcpy(request
.data
.auth
.pass
, p
+ 1);
928 fstrcpy(request
.data
.auth
.user
, username
);
930 request
.flags
= flags
;
932 fstrcpy(request
.data
.auth
.krb5_cc_type
, cctype
);
934 request
.data
.auth
.uid
= geteuid();
936 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
, &response
);
938 /* Display response */
940 d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n",
941 username
, (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed", cctype
);
943 if (response
.data
.auth
.nt_status
)
944 d_fprintf(stderr
, "error code was %s (0x%x)\nerror messsage was: %s\n",
945 response
.data
.auth
.nt_status_string
,
946 response
.data
.auth
.nt_status
,
947 response
.data
.auth
.error_string
);
949 if (result
== NSS_STATUS_SUCCESS
) {
951 if (request
.flags
& WBFLAG_PAM_INFO3_TEXT
) {
952 if (response
.data
.auth
.info3
.user_flgs
& NETLOGON_CACHED_ACCOUNT
) {
953 d_printf("user_flgs: NETLOGON_CACHED_ACCOUNT\n");
957 if (response
.data
.auth
.krb5ccname
[0] != '\0') {
958 d_printf("credentials were put in: %s\n", response
.data
.auth
.krb5ccname
);
960 d_printf("no credentials cached\n");
964 return result
== NSS_STATUS_SUCCESS
;
967 /* Authenticate a user with a plaintext password */
969 static bool wbinfo_auth(char *username
)
971 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
974 const char *password
= NULL
;
977 if ((s
= SMB_STRDUP(username
)) == NULL
) {
981 if ((p
= strchr(s
, '%')) != NULL
) {
991 wbc_status
= wbcAuthenticateUser(name
, password
);
993 d_printf("plaintext password authentication %s\n",
994 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
997 if (response
.data
.auth
.nt_status
)
998 d_fprintf(stderr
, "error code was %s (0x%x)\nerror messsage was: %s\n",
999 response
.data
.auth
.nt_status_string
,
1000 response
.data
.auth
.nt_status
,
1001 response
.data
.auth
.error_string
);
1006 return WBC_ERROR_IS_OK(wbc_status
);
1009 /* Authenticate a user with a challenge/response */
1011 static bool wbinfo_auth_crap(char *username
)
1013 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1014 struct wbcAuthUserParams params
;
1015 struct wbcAuthUserInfo
*info
= NULL
;
1016 struct wbcAuthErrorInfo
*err
= NULL
;
1017 DATA_BLOB lm
= data_blob_null
;
1018 DATA_BLOB nt
= data_blob_null
;
1020 fstring name_domain
;
1024 p
= strchr(username
, '%');
1028 fstrcpy(pass
, p
+ 1);
1031 parse_wbinfo_domain_user(username
, name_domain
, name_user
);
1033 params
.account_name
= name_user
;
1034 params
.domain_name
= name_domain
;
1035 params
.workstation_name
= NULL
;
1038 params
.parameter_control
= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
|
1039 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
;
1041 params
.level
= WBC_AUTH_USER_LEVEL_RESPONSE
;
1043 generate_random_buffer(params
.password
.response
.challenge
, 8);
1045 if (lp_client_ntlmv2_auth()) {
1046 DATA_BLOB server_chal
;
1047 DATA_BLOB names_blob
;
1049 server_chal
= data_blob(params
.password
.response
.challenge
, 8);
1051 /* Pretend this is a login to 'us', for blob purposes */
1052 names_blob
= NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
1054 if (!SMBNTLMv2encrypt(name_user
, name_domain
, pass
, &server_chal
,
1057 data_blob_free(&names_blob
);
1058 data_blob_free(&server_chal
);
1061 data_blob_free(&names_blob
);
1062 data_blob_free(&server_chal
);
1065 if (lp_client_lanman_auth()) {
1067 lm
= data_blob(NULL
, 24);
1068 ok
= SMBencrypt(pass
, params
.password
.response
.challenge
,
1071 data_blob_free(&lm
);
1074 nt
= data_blob(NULL
, 24);
1075 SMBNTencrypt(pass
, params
.password
.response
.challenge
,
1079 params
.password
.response
.nt_length
= nt
.length
;
1080 params
.password
.response
.nt_data
= nt
.data
;
1081 params
.password
.response
.lm_length
= lm
.length
;
1082 params
.password
.response
.lm_data
= lm
.data
;
1084 wbc_status
= wbcAuthenticateUserEx(¶ms
, &info
, &err
);
1086 /* Display response */
1088 d_printf("challenge/response password authentication %s\n",
1089 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1091 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
1092 d_fprintf(stderr
, "error code was %s (0x%x)\nerror messsage was: %s\n",
1095 err
->display_string
);
1097 } else if (WBC_ERROR_IS_OK(wbc_status
)) {
1098 wbcFreeMemory(info
);
1101 data_blob_free(&nt
);
1102 data_blob_free(&lm
);
1104 return WBC_ERROR_IS_OK(wbc_status
);
1107 /* Authenticate a user with a plaintext password and set a token */
1109 static bool wbinfo_klog(char *username
)
1111 struct winbindd_request request
;
1112 struct winbindd_response response
;
1116 /* Send off request */
1118 ZERO_STRUCT(request
);
1119 ZERO_STRUCT(response
);
1121 p
= strchr(username
, '%');
1125 fstrcpy(request
.data
.auth
.user
, username
);
1126 fstrcpy(request
.data
.auth
.pass
, p
+ 1);
1129 fstrcpy(request
.data
.auth
.user
, username
);
1130 fstrcpy(request
.data
.auth
.pass
, getpass("Password: "));
1133 request
.flags
|= WBFLAG_PAM_AFS_TOKEN
;
1135 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
, &response
);
1137 /* Display response */
1139 d_printf("plaintext password authentication %s\n",
1140 (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed");
1142 if (response
.data
.auth
.nt_status
)
1143 d_fprintf(stderr
, "error code was %s (0x%x)\nerror messsage was: %s\n",
1144 response
.data
.auth
.nt_status_string
,
1145 response
.data
.auth
.nt_status
,
1146 response
.data
.auth
.error_string
);
1148 if (result
!= NSS_STATUS_SUCCESS
)
1151 if (response
.extra_data
.data
== NULL
) {
1152 d_fprintf(stderr
, "Did not get token data\n");
1156 if (!afs_settoken_str((char *)response
.extra_data
.data
)) {
1157 d_fprintf(stderr
, "Could not set token\n");
1161 d_printf("Successfully created AFS token\n");
1165 /* Print domain users */
1167 static bool print_domain_users(const char *domain
)
1169 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1171 uint32_t num_users
= 0;
1172 const char **users
= NULL
;
1174 /* Send request to winbind daemon */
1176 /* '.' is the special sign for our own domain */
1177 if (domain
&& strcmp(domain
, ".") == 0) {
1178 domain
= get_winbind_domain();
1181 wbc_status
= wbcListUsers(domain
, &num_users
, &users
);
1182 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1186 for (i
=0; i
< num_users
; i
++) {
1187 d_printf("%s\n", users
[i
]);
1190 wbcFreeMemory(users
);
1195 /* Print domain groups */
1197 static bool print_domain_groups(const char *domain
)
1199 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1201 uint32_t num_groups
= 0;
1202 const char **groups
= NULL
;
1204 /* Send request to winbind daemon */
1206 /* '.' is the special sign for our own domain */
1207 if (domain
&& strcmp(domain
, ".") == 0) {
1208 domain
= get_winbind_domain();
1211 wbc_status
= wbcListGroups(domain
, &num_groups
, &groups
);
1212 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1216 for (i
=0; i
< num_groups
; i
++) {
1217 d_printf("%s\n", groups
[i
]);
1220 wbcFreeMemory(groups
);
1225 /* Set the authorised user for winbindd access in secrets.tdb */
1227 static bool wbinfo_set_auth_user(char *username
)
1229 const char *password
;
1231 fstring user
, domain
;
1233 /* Separate into user and password */
1235 parse_wbinfo_domain_user(username
, domain
, user
);
1237 p
= strchr(user
, '%');
1243 char *thepass
= getpass("Password: ");
1250 /* Store or remove DOMAIN\username%password in secrets.tdb */
1256 if (!secrets_store(SECRETS_AUTH_USER
, user
,
1257 strlen(user
) + 1)) {
1258 d_fprintf(stderr
, "error storing username\n");
1262 /* We always have a domain name added by the
1263 parse_wbinfo_domain_user() function. */
1265 if (!secrets_store(SECRETS_AUTH_DOMAIN
, domain
,
1266 strlen(domain
) + 1)) {
1267 d_fprintf(stderr
, "error storing domain name\n");
1272 secrets_delete(SECRETS_AUTH_USER
);
1273 secrets_delete(SECRETS_AUTH_DOMAIN
);
1278 if (!secrets_store(SECRETS_AUTH_PASSWORD
, password
,
1279 strlen(password
) + 1)) {
1280 d_fprintf(stderr
, "error storing password\n");
1285 secrets_delete(SECRETS_AUTH_PASSWORD
);
1290 static void wbinfo_get_auth_user(void)
1292 char *user
, *domain
, *password
;
1294 /* Lift data from secrets file */
1296 secrets_fetch_ipc_userpass(&user
, &domain
, &password
);
1298 if ((!user
|| !*user
) && (!domain
|| !*domain
) && (!password
|| !*password
)){
1302 SAFE_FREE(password
);
1303 d_printf("No authorised user configured\n");
1307 /* Pretty print authorised user info */
1309 d_printf("%s%s%s%s%s\n", domain
? domain
: "", domain
? lp_winbind_separator(): "",
1310 user
, password
? "%" : "", password
? password
: "");
1314 SAFE_FREE(password
);
1317 static bool wbinfo_ping(void)
1321 wbc_status
= wbcPing();
1323 /* Display response */
1325 d_printf("Ping to winbindd %s\n",
1326 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1328 return WBC_ERROR_IS_OK(wbc_status
);
1334 OPT_SET_AUTH_USER
= 1000,
1345 OPT_LIST_ALL_DOMAINS
,
1346 OPT_LIST_OWN_DOMAIN
,
1352 int main(int argc
, char **argv
, char **envp
)
1355 TALLOC_CTX
*frame
= talloc_stackframe();
1357 static char *string_arg
;
1358 static char *opt_domain_name
;
1361 bool verbose
= false;
1363 struct poptOption long_options
[] = {
1366 /* longName, shortName, argInfo, argPtr, value, descrip,
1369 { "domain-users", 'u', POPT_ARG_NONE
, 0, 'u', "Lists all domain users", "domain"},
1370 { "domain-groups", 'g', POPT_ARG_NONE
, 0, 'g', "Lists all domain groups", "domain" },
1371 { "WINS-by-name", 'N', POPT_ARG_STRING
, &string_arg
, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1372 { "WINS-by-ip", 'I', POPT_ARG_STRING
, &string_arg
, 'I', "Converts IP address to NetBIOS name", "IP" },
1373 { "name-to-sid", 'n', POPT_ARG_STRING
, &string_arg
, 'n', "Converts name to sid", "NAME" },
1374 { "sid-to-name", 's', POPT_ARG_STRING
, &string_arg
, 's', "Converts sid to name", "SID" },
1375 { "lookup-rids", 'R', POPT_ARG_STRING
, &string_arg
, 'R', "Converts RIDs to names", "RIDs" },
1376 { "uid-to-sid", 'U', POPT_ARG_INT
, &int_arg
, 'U', "Converts uid to sid" , "UID" },
1377 { "gid-to-sid", 'G', POPT_ARG_INT
, &int_arg
, 'G', "Converts gid to sid", "GID" },
1378 { "sid-to-uid", 'S', POPT_ARG_STRING
, &string_arg
, 'S', "Converts sid to uid", "SID" },
1379 { "sid-to-gid", 'Y', POPT_ARG_STRING
, &string_arg
, 'Y', "Converts sid to gid", "SID" },
1380 { "allocate-uid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_UID
,
1381 "Get a new UID out of idmap" },
1382 { "allocate-gid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_GID
,
1383 "Get a new GID out of idmap" },
1384 { "check-secret", 't', POPT_ARG_NONE
, 0, 't', "Check shared secret" },
1385 { "trusted-domains", 'm', POPT_ARG_NONE
, 0, 'm', "List trusted domains" },
1386 { "all-domains", 0, POPT_ARG_NONE
, 0, OPT_LIST_ALL_DOMAINS
, "List all domains (trusted and own domain)" },
1387 { "own-domain", 0, POPT_ARG_NONE
, 0, OPT_LIST_OWN_DOMAIN
, "List own domain" },
1388 { "sequence", 0, POPT_ARG_NONE
, 0, OPT_SEQUENCE
, "Show sequence numbers of all domains" },
1389 { "domain-info", 'D', POPT_ARG_STRING
, &string_arg
, 'D', "Show most of the info we have about the domain" },
1390 { "user-info", 'i', POPT_ARG_STRING
, &string_arg
, 'i', "Get user info", "USER" },
1391 { "uid-info", 0, POPT_ARG_INT
, &int_arg
, OPT_UID_INFO
, "Get user info from uid", "UID" },
1392 { "group-info", 0, POPT_ARG_STRING
, &string_arg
, OPT_GROUP_INFO
, "Get group info", "GROUP" },
1393 { "user-groups", 'r', POPT_ARG_STRING
, &string_arg
, 'r', "Get user groups", "USER" },
1394 { "user-domgroups", 0, POPT_ARG_STRING
, &string_arg
,
1395 OPT_USERDOMGROUPS
, "Get user domain groups", "SID" },
1396 { "user-sids", 0, POPT_ARG_STRING
, &string_arg
, OPT_USERSIDS
, "Get user group sids for user SID", "SID" },
1397 { "authenticate", 'a', POPT_ARG_STRING
, &string_arg
, 'a', "authenticate user", "user%password" },
1398 { "set-auth-user", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_AUTH_USER
, "Store user and password used by winbindd (root only)", "user%password" },
1399 { "getdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_GETDCNAME
,
1400 "Get a DC name for a foreign domain", "domainname" },
1401 { "dsgetdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_DSGETDCNAME
, "Find a DC for a domain", "domainname" },
1402 { "get-auth-user", 0, POPT_ARG_NONE
, NULL
, OPT_GET_AUTH_USER
, "Retrieve user and password used by winbindd (root only)", NULL
},
1403 { "ping", 'p', POPT_ARG_NONE
, 0, 'p', "Ping winbindd to see if it is alive" },
1404 { "domain", 0, POPT_ARG_STRING
, &opt_domain_name
, OPT_DOMAIN_NAME
, "Define to the domain to restrict operation", "domain" },
1405 #ifdef WITH_FAKE_KASERVER
1406 { "klog", 'k', POPT_ARG_STRING
, &string_arg
, 'k', "set an AFS token from winbind", "user%password" },
1409 { "krb5auth", 'K', POPT_ARG_STRING
, &string_arg
, 'K', "authenticate user using Kerberos", "user%password" },
1410 /* destroys wbinfo --help output */
1411 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1413 { "separator", 0, POPT_ARG_NONE
, 0, OPT_SEPARATOR
, "Get the active winbind separator", NULL
},
1414 { "verbose", 0, POPT_ARG_NONE
, 0, OPT_VERBOSE
, "Print additional information per command", NULL
},
1415 POPT_COMMON_CONFIGFILE
1420 /* Samba client initialisation */
1426 pc
= poptGetContext("wbinfo", argc
, (const char **)argv
, long_options
, 0);
1428 /* Parse command line options */
1431 poptPrintHelp(pc
, stderr
, 0);
1435 while((opt
= poptGetNextOpt(pc
)) != -1) {
1436 /* get the generic configuration parameters like --domain */
1444 poptFreeContext(pc
);
1446 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, true)) {
1447 d_fprintf(stderr
, "wbinfo: error opening config file %s. Error was %s\n",
1448 get_dyn_CONFIGFILE(), strerror(errno
));
1457 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
1458 POPT_CONTEXT_KEEP_FIRST
);
1460 while((opt
= poptGetNextOpt(pc
)) != -1) {
1463 if (!print_domain_users(opt_domain_name
)) {
1464 d_fprintf(stderr
, "Error looking up domain users\n");
1469 if (!print_domain_groups(opt_domain_name
)) {
1470 d_fprintf(stderr
, "Error looking up domain groups\n");
1475 if (!wbinfo_lookupsid(string_arg
)) {
1476 d_fprintf(stderr
, "Could not lookup sid %s\n", string_arg
);
1481 if (!wbinfo_lookuprids(opt_domain_name
, string_arg
)) {
1482 d_fprintf(stderr
, "Could not lookup RIDs %s\n", string_arg
);
1487 if (!wbinfo_lookupname(string_arg
)) {
1488 d_fprintf(stderr
, "Could not lookup name %s\n", string_arg
);
1493 if (!wbinfo_wins_byname(string_arg
)) {
1494 d_fprintf(stderr
, "Could not lookup WINS by name %s\n", string_arg
);
1499 if (!wbinfo_wins_byip(string_arg
)) {
1500 d_fprintf(stderr
, "Could not lookup WINS by IP %s\n", string_arg
);
1505 if (!wbinfo_uid_to_sid(int_arg
)) {
1506 d_fprintf(stderr
, "Could not convert uid %d to sid\n", int_arg
);
1511 if (!wbinfo_gid_to_sid(int_arg
)) {
1512 d_fprintf(stderr
, "Could not convert gid %d to sid\n",
1518 if (!wbinfo_sid_to_uid(string_arg
)) {
1519 d_fprintf(stderr
, "Could not convert sid %s to uid\n",
1525 if (!wbinfo_sid_to_gid(string_arg
)) {
1526 d_fprintf(stderr
, "Could not convert sid %s to gid\n",
1531 case OPT_ALLOCATE_UID
:
1532 if (!wbinfo_allocate_uid()) {
1533 d_fprintf(stderr
, "Could not allocate a uid\n");
1537 case OPT_ALLOCATE_GID
:
1538 if (!wbinfo_allocate_gid()) {
1539 d_fprintf(stderr
, "Could not allocate a gid\n");
1544 if (!wbinfo_check_secret()) {
1545 d_fprintf(stderr
, "Could not check secret\n");
1550 if (!wbinfo_list_domains(false, verbose
)) {
1551 d_fprintf(stderr
, "Could not list trusted domains\n");
1556 if (!wbinfo_show_sequence(opt_domain_name
)) {
1557 d_fprintf(stderr
, "Could not show sequence numbers\n");
1562 if (!wbinfo_domain_info(string_arg
)) {
1563 d_fprintf(stderr
, "Could not get domain info\n");
1568 if (!wbinfo_get_userinfo(string_arg
)) {
1569 d_fprintf(stderr
, "Could not get info for user %s\n",
1575 if ( !wbinfo_get_uidinfo(int_arg
)) {
1576 d_fprintf(stderr
, "Could not get info for uid "
1581 case OPT_GROUP_INFO
:
1582 if ( !wbinfo_get_groupinfo(string_arg
)) {
1583 d_fprintf(stderr
, "Could not get info for "
1584 "group %s\n", string_arg
);
1589 if (!wbinfo_get_usergroups(string_arg
)) {
1590 d_fprintf(stderr
, "Could not get groups for user %s\n",
1596 if (!wbinfo_get_usersids(string_arg
)) {
1597 d_fprintf(stderr
, "Could not get group SIDs for user SID %s\n",
1602 case OPT_USERDOMGROUPS
:
1603 if (!wbinfo_get_userdomgroups(string_arg
)) {
1604 d_fprintf(stderr
, "Could not get user's domain groups "
1605 "for user SID %s\n", string_arg
);
1610 bool got_error
= false;
1612 if (!wbinfo_auth(string_arg
)) {
1613 d_fprintf(stderr
, "Could not authenticate user %s with "
1614 "plaintext password\n", string_arg
);
1618 if (!wbinfo_auth_crap(string_arg
)) {
1619 d_fprintf(stderr
, "Could not authenticate user %s with "
1620 "challenge/response\n", string_arg
);
1629 uint32 flags
= WBFLAG_PAM_KRB5
|
1630 WBFLAG_PAM_CACHED_LOGIN
|
1631 WBFLAG_PAM_FALLBACK_AFTER_KRB5
|
1632 WBFLAG_PAM_INFO3_TEXT
;
1634 if (!wbinfo_auth_krb5(string_arg
, "FILE", flags
)) {
1635 d_fprintf(stderr
, "Could not authenticate user [%s] with "
1636 "Kerberos (ccache: %s)\n", string_arg
, "FILE");
1642 if (!wbinfo_klog(string_arg
)) {
1643 d_fprintf(stderr
, "Could not klog user\n");
1648 if (!wbinfo_ping()) {
1649 d_fprintf(stderr
, "could not ping winbindd!\n");
1653 case OPT_SET_AUTH_USER
:
1654 if (!wbinfo_set_auth_user(string_arg
)) {
1658 case OPT_GET_AUTH_USER
:
1659 wbinfo_get_auth_user();
1662 if (!wbinfo_getdcname(string_arg
)) {
1666 case OPT_DSGETDCNAME
:
1667 if (!wbinfo_dsgetdcname(string_arg
, 0)) {
1671 case OPT_SEPARATOR
: {
1672 const char sep
= winbind_separator_int(true);
1676 d_printf("%c\n", sep
);
1679 case OPT_LIST_ALL_DOMAINS
:
1680 if (!wbinfo_list_domains(true, verbose
)) {
1684 case OPT_LIST_OWN_DOMAIN
:
1685 if (!wbinfo_list_own_domain()) {
1689 /* generic configuration options */
1690 case OPT_DOMAIN_NAME
:
1695 d_fprintf(stderr
, "Invalid option\n");
1696 poptPrintHelp(pc
, stderr
, 0);
1706 talloc_destroy(frame
);
1708 poptFreeContext(pc
);