2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett 2002
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "winbind_client.h"
25 #include "libwbclient/wbclient.h"
28 #define DBGC_CLASS DBGC_WINBIND
30 extern int winbindd_fd
;
32 static char winbind_separator_int(bool strict
)
34 struct winbindd_response response
;
41 ZERO_STRUCT(response
);
43 /* Send off request */
45 if (winbindd_request_response(WINBINDD_INFO
, NULL
, &response
) !=
47 d_fprintf(stderr
, "could not obtain winbind separator!\n");
51 /* HACK: (this module should not call lp_ funtions) */
52 return *lp_winbind_separator();
55 sep
= response
.data
.info
.winbind_separator
;
59 d_fprintf(stderr
, "winbind separator was NULL!\n");
63 /* HACK: (this module should not call lp_ funtions) */
64 sep
= *lp_winbind_separator();
70 static char winbind_separator(void)
72 return winbind_separator_int(False
);
75 static const char *get_winbind_domain(void)
77 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
78 struct wbcDomainInfo
*dinfo
= NULL
;
79 static fstring winbind_domain
;
83 wbc_status
= wbcDomainInfo(".", &dinfo
);
85 if (!WBC_ERROR_IS_OK(wbc_status
)) {
86 d_fprintf(stderr
, "could not obtain winbind domain name!\n");
88 /* HACK: (this module should not call lp_ funtions) */
89 return lp_workgroup();
92 fstrcpy(winbind_domain
, dinfo
->short_name
);
96 return winbind_domain
;
99 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
100 form DOMAIN/user into a domain and a user */
102 static bool parse_wbinfo_domain_user(const char *domuser
, fstring domain
,
106 char *p
= strchr(domuser
,winbind_separator());
109 /* Maybe it was a UPN? */
110 if ((p
= strchr(domuser
, '@')) != NULL
) {
112 fstrcpy(user
, domuser
);
116 fstrcpy(user
, domuser
);
117 fstrcpy(domain
, get_winbind_domain());
122 fstrcpy(domain
, domuser
);
123 domain
[PTR_DIFF(p
, domuser
)] = 0;
129 /* pull pwent info for a given user */
131 static bool wbinfo_get_userinfo(char *user
)
133 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
134 struct passwd
*pwd
= NULL
;
136 wbc_status
= wbcGetpwnam(user
, &pwd
);
137 if (!WBC_ERROR_IS_OK(wbc_status
)) {
141 d_printf("%s:%s:%d:%d:%s:%s:%s\n",
153 /* pull pwent info for a given uid */
154 static bool wbinfo_get_uidinfo(int uid
)
156 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
157 struct passwd
*pwd
= NULL
;
159 wbc_status
= wbcGetpwuid(uid
, &pwd
);
160 if (!WBC_ERROR_IS_OK(wbc_status
)) {
164 d_printf("%s:%s:%d:%d:%s:%s:%s\n",
176 /* pull grent for a given group */
177 static bool wbinfo_get_groupinfo(char *group
)
179 struct winbindd_request request
;
180 struct winbindd_response response
;
183 ZERO_STRUCT(request
);
184 ZERO_STRUCT(response
);
188 fstrcpy(request
.data
.groupname
, group
);
190 result
= winbindd_request_response(WINBINDD_GETGRNAM
, &request
,
193 if ( result
!= NSS_STATUS_SUCCESS
)
196 d_printf( "%s:%s:%d\n",
197 response
.data
.gr
.gr_name
,
198 response
.data
.gr
.gr_passwd
,
199 response
.data
.gr
.gr_gid
);
204 /* List groups a user is a member of */
206 static bool wbinfo_get_usergroups(char *user
)
208 struct winbindd_request request
;
209 struct winbindd_response response
;
213 ZERO_STRUCT(request
);
214 ZERO_STRUCT(response
);
218 fstrcpy(request
.data
.username
, user
);
220 result
= winbindd_request_response(WINBINDD_GETGROUPS
, &request
, &response
);
222 if (result
!= NSS_STATUS_SUCCESS
)
225 for (i
= 0; i
< response
.data
.num_entries
; i
++)
226 d_printf("%d\n", (int)((gid_t
*)response
.extra_data
.data
)[i
]);
228 SAFE_FREE(response
.extra_data
.data
);
234 /* List group SIDs a user SID is a member of */
235 static bool wbinfo_get_usersids(char *user_sid
)
237 struct winbindd_request request
;
238 struct winbindd_response response
;
243 ZERO_STRUCT(request
);
244 ZERO_STRUCT(response
);
247 fstrcpy(request
.data
.sid
, user_sid
);
249 result
= winbindd_request_response(WINBINDD_GETUSERSIDS
, &request
, &response
);
251 if (result
!= NSS_STATUS_SUCCESS
)
254 s
= (const char *)response
.extra_data
.data
;
255 for (i
= 0; i
< response
.data
.num_entries
; i
++) {
260 SAFE_FREE(response
.extra_data
.data
);
265 static bool wbinfo_get_userdomgroups(const char *user_sid
)
267 struct winbindd_request request
;
268 struct winbindd_response response
;
271 ZERO_STRUCT(request
);
272 ZERO_STRUCT(response
);
275 fstrcpy(request
.data
.sid
, user_sid
);
277 result
= winbindd_request_response(WINBINDD_GETUSERDOMGROUPS
, &request
,
280 if (result
!= NSS_STATUS_SUCCESS
)
283 if (response
.data
.num_entries
!= 0)
284 printf("%s", (char *)response
.extra_data
.data
);
286 SAFE_FREE(response
.extra_data
.data
);
291 /* Convert NetBIOS name to IP */
293 static bool wbinfo_wins_byname(char *name
)
295 struct winbindd_request request
;
296 struct winbindd_response response
;
298 ZERO_STRUCT(request
);
299 ZERO_STRUCT(response
);
303 fstrcpy(request
.data
.winsreq
, name
);
305 if (winbindd_request_response(WINBINDD_WINS_BYNAME
, &request
, &response
) !=
306 NSS_STATUS_SUCCESS
) {
310 /* Display response */
312 d_printf("%s\n", response
.data
.winsresp
);
317 /* Convert IP to NetBIOS name */
319 static bool wbinfo_wins_byip(char *ip
)
321 struct winbindd_request request
;
322 struct winbindd_response response
;
324 ZERO_STRUCT(request
);
325 ZERO_STRUCT(response
);
329 fstrcpy(request
.data
.winsreq
, ip
);
331 if (winbindd_request_response(WINBINDD_WINS_BYIP
, &request
, &response
) !=
332 NSS_STATUS_SUCCESS
) {
336 /* Display response */
338 d_printf("%s\n", response
.data
.winsresp
);
343 /* List trusted domains */
345 static bool wbinfo_list_domains(bool list_all_domains
)
347 struct winbindd_request request
;
348 struct winbindd_response response
;
350 ZERO_STRUCT(request
);
351 ZERO_STRUCT(response
);
355 request
.data
.list_all_domains
= list_all_domains
;
357 if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM
, &request
, &response
) !=
361 /* Display response */
363 if (response
.extra_data
.data
) {
364 const char *extra_data
= (char *)response
.extra_data
.data
;
367 TALLOC_CTX
*frame
= talloc_stackframe();
369 while(next_token_talloc(frame
,&extra_data
,&name
,"\n")) {
370 p
= strchr(name
, '\\');
372 d_fprintf(stderr
, "Got invalid response: %s\n",
375 SAFE_FREE(response
.extra_data
.data
);
379 d_printf("%s\n", name
);
382 SAFE_FREE(response
.extra_data
.data
);
388 /* List own domain */
390 static bool wbinfo_list_own_domain(void)
392 d_printf("%s\n", get_winbind_domain());
397 /* show sequence numbers */
398 static bool wbinfo_show_sequence(const char *domain
)
400 struct winbindd_request request
;
401 struct winbindd_response response
;
403 ZERO_STRUCT(response
);
404 ZERO_STRUCT(request
);
407 fstrcpy( request
.domain_name
, domain
);
411 if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE
, &request
, &response
) !=
415 /* Display response */
418 d_printf("%s : ", domain
);
419 if (response
.data
.sequence_number
== (uint32_t)-1) {
420 d_printf("DISCONNECTED\n");
422 d_printf("%d\n", response
.data
.sequence_number
);
424 } else if (response
.extra_data
.data
) {
425 char *extra_data
= (char *)response
.extra_data
.data
;
426 d_printf("%s", extra_data
);
427 SAFE_FREE(response
.extra_data
.data
);
433 /* Show domain info */
435 static bool wbinfo_domain_info(const char *domain_name
)
437 struct winbindd_request request
;
438 struct winbindd_response response
;
440 ZERO_STRUCT(request
);
441 ZERO_STRUCT(response
);
443 if ((strequal(domain_name
, ".")) || (domain_name
[0] == '\0'))
444 fstrcpy(request
.domain_name
, get_winbind_domain());
446 fstrcpy(request
.domain_name
, domain_name
);
450 if (winbindd_request_response(WINBINDD_DOMAIN_INFO
, &request
, &response
) !=
454 /* Display response */
456 d_printf("Name : %s\n", response
.data
.domain_info
.name
);
457 d_printf("Alt_Name : %s\n", response
.data
.domain_info
.alt_name
);
459 d_printf("SID : %s\n", response
.data
.domain_info
.sid
);
461 d_printf("Active Directory : %s\n",
462 response
.data
.domain_info
.active_directory
? "Yes" : "No");
463 d_printf("Native : %s\n",
464 response
.data
.domain_info
.native_mode
? "Yes" : "No");
466 d_printf("Primary : %s\n",
467 response
.data
.domain_info
.primary
? "Yes" : "No");
472 /* Get a foreign DC's name */
473 static bool wbinfo_getdcname(const char *domain_name
)
475 struct winbindd_request request
;
476 struct winbindd_response response
;
478 ZERO_STRUCT(request
);
479 ZERO_STRUCT(response
);
481 fstrcpy(request
.domain_name
, domain_name
);
485 if (winbindd_request_response(WINBINDD_GETDCNAME
, &request
, &response
) !=
486 NSS_STATUS_SUCCESS
) {
487 d_fprintf(stderr
, "Could not get dc name for %s\n", domain_name
);
491 /* Display response */
493 d_printf("%s\n", response
.data
.dc_name
);
499 static bool wbinfo_dsgetdcname(const char *domain_name
, uint32_t flags
)
501 struct winbindd_request request
;
502 struct winbindd_response response
;
504 ZERO_STRUCT(request
);
505 ZERO_STRUCT(response
);
507 fstrcpy(request
.domain_name
, domain_name
);
508 request
.flags
= flags
;
510 request
.flags
|= DS_DIRECTORY_SERVICE_REQUIRED
;
514 if (winbindd_request_response(WINBINDD_DSGETDCNAME
, &request
, &response
) !=
515 NSS_STATUS_SUCCESS
) {
516 d_fprintf(stderr
, "Could not find dc for %s\n", domain_name
);
520 /* Display response */
522 d_printf("%s\n", response
.data
.dc_name
);
527 /* Check trust account password */
529 static bool wbinfo_check_secret(void)
531 struct winbindd_response response
;
534 ZERO_STRUCT(response
);
536 result
= winbindd_request_response(WINBINDD_CHECK_MACHACC
, NULL
, &response
);
538 d_printf("checking the trust secret via RPC calls %s\n",
539 (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed");
541 if (result
!= NSS_STATUS_SUCCESS
)
542 d_fprintf(stderr
, "error code was %s (0x%x)\n",
543 response
.data
.auth
.nt_status_string
,
544 response
.data
.auth
.nt_status
);
546 return result
== NSS_STATUS_SUCCESS
;
549 /* Convert uid to sid */
551 static bool wbinfo_uid_to_sid(uid_t uid
)
553 struct winbindd_request request
;
554 struct winbindd_response response
;
556 ZERO_STRUCT(request
);
557 ZERO_STRUCT(response
);
561 request
.data
.uid
= uid
;
563 if (winbindd_request_response(WINBINDD_UID_TO_SID
, &request
, &response
) !=
567 /* Display response */
569 d_printf("%s\n", response
.data
.sid
.sid
);
574 /* Convert gid to sid */
576 static bool wbinfo_gid_to_sid(gid_t gid
)
578 struct winbindd_request request
;
579 struct winbindd_response response
;
581 ZERO_STRUCT(request
);
582 ZERO_STRUCT(response
);
586 request
.data
.gid
= gid
;
588 if (winbindd_request_response(WINBINDD_GID_TO_SID
, &request
, &response
) !=
592 /* Display response */
594 d_printf("%s\n", response
.data
.sid
.sid
);
599 /* Convert sid to uid */
601 static bool wbinfo_sid_to_uid(char *sid
)
603 struct winbindd_request request
;
604 struct winbindd_response response
;
606 ZERO_STRUCT(request
);
607 ZERO_STRUCT(response
);
611 fstrcpy(request
.data
.sid
, sid
);
613 if (winbindd_request_response(WINBINDD_SID_TO_UID
, &request
, &response
) !=
617 /* Display response */
619 d_printf("%d\n", (int)response
.data
.uid
);
624 static bool wbinfo_sid_to_gid(char *sid
)
626 struct winbindd_request request
;
627 struct winbindd_response response
;
629 ZERO_STRUCT(request
);
630 ZERO_STRUCT(response
);
634 fstrcpy(request
.data
.sid
, sid
);
636 if (winbindd_request_response(WINBINDD_SID_TO_GID
, &request
, &response
) !=
640 /* Display response */
642 d_printf("%d\n", (int)response
.data
.gid
);
647 static bool wbinfo_allocate_uid(void)
651 if (!winbind_allocate_uid(&uid
))
654 d_printf("New uid: %d\n", uid
);
659 static bool wbinfo_allocate_gid(void)
663 if (!winbind_allocate_gid(&gid
))
666 d_printf("New gid: %d\n", gid
);
671 /* Convert sid to string */
673 static bool wbinfo_lookupsid(char *sid
)
675 struct winbindd_request request
;
676 struct winbindd_response response
;
678 ZERO_STRUCT(request
);
679 ZERO_STRUCT(response
);
681 /* Send off request */
683 fstrcpy(request
.data
.sid
, sid
);
685 if (winbindd_request_response(WINBINDD_LOOKUPSID
, &request
, &response
) !=
689 /* Display response */
691 d_printf("%s%c%s %d\n", response
.data
.name
.dom_name
,
692 winbind_separator(), response
.data
.name
.name
,
693 response
.data
.name
.type
);
698 /* Lookup a list of RIDs */
700 static bool wbinfo_lookuprids(char *domain
, char *arg
)
709 enum lsa_SidType
*types
;
710 const char *domain_name
;
712 struct winbindd_request request
;
713 struct winbindd_response response
;
715 ZERO_STRUCT(request
);
716 ZERO_STRUCT(response
);
718 if ((domain
== NULL
) || (strequal(domain
, ".")) || (domain
[0] == '\0'))
719 fstrcpy(request
.domain_name
, get_winbind_domain());
721 fstrcpy(request
.domain_name
, domain
);
725 if (winbindd_request_response(WINBINDD_DOMAIN_INFO
, &request
, &response
) !=
726 NSS_STATUS_SUCCESS
) {
727 d_printf("Could not get domain sid for %s\n", request
.domain_name
);
731 if (!string_to_sid(&sid
, response
.data
.domain_info
.sid
)) {
732 d_printf("Could not convert %s to sid\n", response
.data
.domain_info
.sid
);
736 mem_ctx
= talloc_new(NULL
);
737 if (mem_ctx
== NULL
) {
738 d_printf("talloc_new failed\n");
746 while (next_token_talloc(mem_ctx
, &p
, &ridstr
, " ,\n")) {
747 uint32 rid
= strtoul(ridstr
, NULL
, 10);
748 ADD_TO_ARRAY(mem_ctx
, uint32
, rid
, &rids
, &num_rids
);
752 TALLOC_FREE(mem_ctx
);
756 if (!winbind_lookup_rids(mem_ctx
, &sid
, num_rids
, rids
,
757 &domain_name
, &names
, &types
)) {
758 d_printf("winbind_lookup_rids failed\n");
759 TALLOC_FREE(mem_ctx
);
763 d_printf("Domain: %s\n", domain_name
);
765 for (i
=0; i
<num_rids
; i
++) {
766 d_printf("%8d: %s (%s)\n", rids
[i
], names
[i
],
767 sid_type_lookup(types
[i
]));
770 TALLOC_FREE(mem_ctx
);
774 /* Convert string to sid */
776 static bool wbinfo_lookupname(char *name
)
778 struct winbindd_request request
;
779 struct winbindd_response response
;
781 /* Send off request */
783 ZERO_STRUCT(request
);
784 ZERO_STRUCT(response
);
786 parse_wbinfo_domain_user(name
, request
.data
.name
.dom_name
,
787 request
.data
.name
.name
);
789 if (winbindd_request_response(WINBINDD_LOOKUPNAME
, &request
, &response
) !=
793 /* Display response */
795 d_printf("%s %s (%d)\n", response
.data
.sid
.sid
, sid_type_lookup(response
.data
.sid
.type
), response
.data
.sid
.type
);
800 /* Authenticate a user with a plaintext password */
802 static bool wbinfo_auth_krb5(char *username
, const char *cctype
, uint32 flags
)
804 struct winbindd_request request
;
805 struct winbindd_response response
;
809 /* Send off request */
811 ZERO_STRUCT(request
);
812 ZERO_STRUCT(response
);
814 p
= strchr(username
, '%');
818 fstrcpy(request
.data
.auth
.user
, username
);
819 fstrcpy(request
.data
.auth
.pass
, p
+ 1);
822 fstrcpy(request
.data
.auth
.user
, username
);
824 request
.flags
= flags
;
826 fstrcpy(request
.data
.auth
.krb5_cc_type
, cctype
);
828 request
.data
.auth
.uid
= geteuid();
830 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
, &response
);
832 /* Display response */
834 d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n",
835 username
, (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed", cctype
);
837 if (response
.data
.auth
.nt_status
)
838 d_fprintf(stderr
, "error code was %s (0x%x)\nerror messsage was: %s\n",
839 response
.data
.auth
.nt_status_string
,
840 response
.data
.auth
.nt_status
,
841 response
.data
.auth
.error_string
);
843 if (result
== NSS_STATUS_SUCCESS
) {
845 if (request
.flags
& WBFLAG_PAM_INFO3_TEXT
) {
846 if (response
.data
.auth
.info3
.user_flgs
& LOGON_CACHED_ACCOUNT
) {
847 d_printf("user_flgs: LOGON_CACHED_ACCOUNT\n");
851 if (response
.data
.auth
.krb5ccname
[0] != '\0') {
852 d_printf("credentials were put in: %s\n", response
.data
.auth
.krb5ccname
);
854 d_printf("no credentials cached\n");
858 return result
== NSS_STATUS_SUCCESS
;
861 /* Authenticate a user with a plaintext password */
863 static bool wbinfo_auth(char *username
)
865 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
868 char *password
= NULL
;
871 if ((s
= SMB_STRDUP(username
)) == NULL
) {
875 if ((p
= strchr(s
, '%')) != NULL
) {
883 wbc_status
= wbcAuthenticateUser(name
, password
);
885 d_printf("plaintext password authentication %s\n",
886 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
889 if (response
.data
.auth
.nt_status
)
890 d_fprintf(stderr
, "error code was %s (0x%x)\nerror messsage was: %s\n",
891 response
.data
.auth
.nt_status_string
,
892 response
.data
.auth
.nt_status
,
893 response
.data
.auth
.error_string
);
898 return WBC_ERROR_IS_OK(wbc_status
);
901 /* Authenticate a user with a challenge/response */
903 static bool wbinfo_auth_crap(char *username
)
905 struct winbindd_request request
;
906 struct winbindd_response response
;
913 /* Send off request */
915 ZERO_STRUCT(request
);
916 ZERO_STRUCT(response
);
918 p
= strchr(username
, '%');
922 fstrcpy(pass
, p
+ 1);
925 parse_wbinfo_domain_user(username
, name_domain
, name_user
);
927 request
.data
.auth_crap
.logon_parameters
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
| MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
;
929 fstrcpy(request
.data
.auth_crap
.user
, name_user
);
931 fstrcpy(request
.data
.auth_crap
.domain
,
934 generate_random_buffer(request
.data
.auth_crap
.chal
, 8);
936 if (lp_client_ntlmv2_auth()) {
937 DATA_BLOB server_chal
;
938 DATA_BLOB names_blob
;
940 DATA_BLOB lm_response
;
941 DATA_BLOB nt_response
;
943 server_chal
= data_blob(request
.data
.auth_crap
.chal
, 8);
945 /* Pretend this is a login to 'us', for blob purposes */
946 names_blob
= NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
948 if (!SMBNTLMv2encrypt(name_user
, name_domain
, pass
, &server_chal
,
950 &lm_response
, &nt_response
, NULL
)) {
951 data_blob_free(&names_blob
);
952 data_blob_free(&server_chal
);
955 data_blob_free(&names_blob
);
956 data_blob_free(&server_chal
);
958 memcpy(request
.data
.auth_crap
.nt_resp
, nt_response
.data
,
959 MIN(nt_response
.length
,
960 sizeof(request
.data
.auth_crap
.nt_resp
)));
961 request
.data
.auth_crap
.nt_resp_len
= nt_response
.length
;
963 memcpy(request
.data
.auth_crap
.lm_resp
, lm_response
.data
,
964 MIN(lm_response
.length
,
965 sizeof(request
.data
.auth_crap
.lm_resp
)));
966 request
.data
.auth_crap
.lm_resp_len
= lm_response
.length
;
968 data_blob_free(&nt_response
);
969 data_blob_free(&lm_response
);
972 if (lp_client_lanman_auth()
973 && SMBencrypt(pass
, request
.data
.auth_crap
.chal
,
974 (uchar
*)request
.data
.auth_crap
.lm_resp
)) {
975 request
.data
.auth_crap
.lm_resp_len
= 24;
977 request
.data
.auth_crap
.lm_resp_len
= 0;
979 SMBNTencrypt(pass
, request
.data
.auth_crap
.chal
,
980 (uchar
*)request
.data
.auth_crap
.nt_resp
);
982 request
.data
.auth_crap
.nt_resp_len
= 24;
985 result
= winbindd_request_response(WINBINDD_PAM_AUTH_CRAP
, &request
, &response
);
987 /* Display response */
989 d_printf("challenge/response password authentication %s\n",
990 (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed");
992 if (response
.data
.auth
.nt_status
)
993 d_fprintf(stderr
, "error code was %s (0x%x)\nerror messsage was: %s\n",
994 response
.data
.auth
.nt_status_string
,
995 response
.data
.auth
.nt_status
,
996 response
.data
.auth
.error_string
);
998 return result
== NSS_STATUS_SUCCESS
;
1001 /* Authenticate a user with a plaintext password and set a token */
1003 static bool wbinfo_klog(char *username
)
1005 struct winbindd_request request
;
1006 struct winbindd_response response
;
1010 /* Send off request */
1012 ZERO_STRUCT(request
);
1013 ZERO_STRUCT(response
);
1015 p
= strchr(username
, '%');
1019 fstrcpy(request
.data
.auth
.user
, username
);
1020 fstrcpy(request
.data
.auth
.pass
, p
+ 1);
1023 fstrcpy(request
.data
.auth
.user
, username
);
1024 fstrcpy(request
.data
.auth
.pass
, getpass("Password: "));
1027 request
.flags
|= WBFLAG_PAM_AFS_TOKEN
;
1029 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
, &response
);
1031 /* Display response */
1033 d_printf("plaintext password authentication %s\n",
1034 (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed");
1036 if (response
.data
.auth
.nt_status
)
1037 d_fprintf(stderr
, "error code was %s (0x%x)\nerror messsage was: %s\n",
1038 response
.data
.auth
.nt_status_string
,
1039 response
.data
.auth
.nt_status
,
1040 response
.data
.auth
.error_string
);
1042 if (result
!= NSS_STATUS_SUCCESS
)
1045 if (response
.extra_data
.data
== NULL
) {
1046 d_fprintf(stderr
, "Did not get token data\n");
1050 if (!afs_settoken_str((char *)response
.extra_data
.data
)) {
1051 d_fprintf(stderr
, "Could not set token\n");
1055 d_printf("Successfully created AFS token\n");
1059 /* Print domain users */
1061 static bool print_domain_users(const char *domain
)
1063 struct winbindd_request request
;
1064 struct winbindd_response response
;
1065 const char *extra_data
;
1067 TALLOC_CTX
*frame
= NULL
;
1069 /* Send request to winbind daemon */
1071 ZERO_STRUCT(request
);
1072 ZERO_STRUCT(response
);
1075 /* '.' is the special sign for our own domain */
1076 if ( strequal(domain
, ".") )
1077 fstrcpy( request
.domain_name
, get_winbind_domain() );
1079 fstrcpy( request
.domain_name
, domain
);
1082 if (winbindd_request_response(WINBINDD_LIST_USERS
, &request
, &response
) !=
1086 /* Look through extra data */
1088 if (!response
.extra_data
.data
)
1091 extra_data
= (const char *)response
.extra_data
.data
;
1093 frame
= talloc_stackframe();
1094 while(next_token_talloc(frame
,&extra_data
,&name
, ","))
1095 d_printf("%s\n", name
);
1098 SAFE_FREE(response
.extra_data
.data
);
1103 /* Print domain groups */
1105 static bool print_domain_groups(const char *domain
)
1107 struct winbindd_request request
;
1108 struct winbindd_response response
;
1109 const char *extra_data
;
1110 TALLOC_CTX
*frame
= NULL
;
1113 ZERO_STRUCT(request
);
1114 ZERO_STRUCT(response
);
1117 if ( strequal(domain
, ".") )
1118 fstrcpy( request
.domain_name
, get_winbind_domain() );
1120 fstrcpy( request
.domain_name
, domain
);
1123 if (winbindd_request_response(WINBINDD_LIST_GROUPS
, &request
, &response
) !=
1127 /* Look through extra data */
1129 if (!response
.extra_data
.data
)
1132 extra_data
= (const char *)response
.extra_data
.data
;
1134 frame
= talloc_stackframe();
1135 while(next_token_talloc(frame
,&extra_data
,&name
, ","))
1136 d_printf("%s\n", name
);
1139 SAFE_FREE(response
.extra_data
.data
);
1144 /* Set the authorised user for winbindd access in secrets.tdb */
1146 static bool wbinfo_set_auth_user(char *username
)
1148 const char *password
;
1150 fstring user
, domain
;
1152 /* Separate into user and password */
1154 parse_wbinfo_domain_user(username
, domain
, user
);
1156 p
= strchr(user
, '%');
1162 char *thepass
= getpass("Password: ");
1169 /* Store or remove DOMAIN\username%password in secrets.tdb */
1175 if (!secrets_store(SECRETS_AUTH_USER
, user
,
1176 strlen(user
) + 1)) {
1177 d_fprintf(stderr
, "error storing username\n");
1181 /* We always have a domain name added by the
1182 parse_wbinfo_domain_user() function. */
1184 if (!secrets_store(SECRETS_AUTH_DOMAIN
, domain
,
1185 strlen(domain
) + 1)) {
1186 d_fprintf(stderr
, "error storing domain name\n");
1191 secrets_delete(SECRETS_AUTH_USER
);
1192 secrets_delete(SECRETS_AUTH_DOMAIN
);
1197 if (!secrets_store(SECRETS_AUTH_PASSWORD
, password
,
1198 strlen(password
) + 1)) {
1199 d_fprintf(stderr
, "error storing password\n");
1204 secrets_delete(SECRETS_AUTH_PASSWORD
);
1209 static void wbinfo_get_auth_user(void)
1211 char *user
, *domain
, *password
;
1213 /* Lift data from secrets file */
1215 secrets_fetch_ipc_userpass(&user
, &domain
, &password
);
1217 if ((!user
|| !*user
) && (!domain
|| !*domain
) && (!password
|| !*password
)){
1221 SAFE_FREE(password
);
1222 d_printf("No authorised user configured\n");
1226 /* Pretty print authorised user info */
1228 d_printf("%s%s%s%s%s\n", domain
? domain
: "", domain
? lp_winbind_separator(): "",
1229 user
, password
? "%" : "", password
? password
: "");
1233 SAFE_FREE(password
);
1236 static bool wbinfo_ping(void)
1240 result
= winbindd_request_response(WINBINDD_PING
, NULL
, NULL
);
1242 /* Display response */
1244 d_printf("Ping to winbindd %s on fd %d\n",
1245 (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed", winbindd_fd
);
1247 return result
== NSS_STATUS_SUCCESS
;
1253 OPT_SET_AUTH_USER
= 1000,
1264 OPT_LIST_ALL_DOMAINS
,
1265 OPT_LIST_OWN_DOMAIN
,
1270 int main(int argc
, char **argv
, char **envp
)
1273 TALLOC_CTX
*frame
= talloc_stackframe();
1275 static char *string_arg
;
1276 static char *opt_domain_name
;
1280 struct poptOption long_options
[] = {
1283 /* longName, shortName, argInfo, argPtr, value, descrip,
1286 { "domain-users", 'u', POPT_ARG_NONE
, 0, 'u', "Lists all domain users", "domain"},
1287 { "domain-groups", 'g', POPT_ARG_NONE
, 0, 'g', "Lists all domain groups", "domain" },
1288 { "WINS-by-name", 'N', POPT_ARG_STRING
, &string_arg
, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1289 { "WINS-by-ip", 'I', POPT_ARG_STRING
, &string_arg
, 'I', "Converts IP address to NetBIOS name", "IP" },
1290 { "name-to-sid", 'n', POPT_ARG_STRING
, &string_arg
, 'n', "Converts name to sid", "NAME" },
1291 { "sid-to-name", 's', POPT_ARG_STRING
, &string_arg
, 's', "Converts sid to name", "SID" },
1292 { "lookup-rids", 'R', POPT_ARG_STRING
, &string_arg
, 'R', "Converts RIDs to names", "RIDs" },
1293 { "uid-to-sid", 'U', POPT_ARG_INT
, &int_arg
, 'U', "Converts uid to sid" , "UID" },
1294 { "gid-to-sid", 'G', POPT_ARG_INT
, &int_arg
, 'G', "Converts gid to sid", "GID" },
1295 { "sid-to-uid", 'S', POPT_ARG_STRING
, &string_arg
, 'S', "Converts sid to uid", "SID" },
1296 { "sid-to-gid", 'Y', POPT_ARG_STRING
, &string_arg
, 'Y', "Converts sid to gid", "SID" },
1297 { "allocate-uid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_UID
,
1298 "Get a new UID out of idmap" },
1299 { "allocate-gid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_GID
,
1300 "Get a new GID out of idmap" },
1301 { "check-secret", 't', POPT_ARG_NONE
, 0, 't', "Check shared secret" },
1302 { "trusted-domains", 'm', POPT_ARG_NONE
, 0, 'm', "List trusted domains" },
1303 { "all-domains", 0, POPT_ARG_NONE
, 0, OPT_LIST_ALL_DOMAINS
, "List all domains (trusted and own domain)" },
1304 { "own-domain", 0, POPT_ARG_NONE
, 0, OPT_LIST_OWN_DOMAIN
, "List own domain" },
1305 { "sequence", 0, POPT_ARG_NONE
, 0, OPT_SEQUENCE
, "Show sequence numbers of all domains" },
1306 { "domain-info", 'D', POPT_ARG_STRING
, &string_arg
, 'D', "Show most of the info we have about the domain" },
1307 { "user-info", 'i', POPT_ARG_STRING
, &string_arg
, 'i', "Get user info", "USER" },
1308 { "uid-info", 0, POPT_ARG_INT
, &int_arg
, OPT_UID_INFO
, "Get user info from uid", "UID" },
1309 { "group-info", 0, POPT_ARG_STRING
, &string_arg
, OPT_GROUP_INFO
, "Get group info", "GROUP" },
1310 { "user-groups", 'r', POPT_ARG_STRING
, &string_arg
, 'r', "Get user groups", "USER" },
1311 { "user-domgroups", 0, POPT_ARG_STRING
, &string_arg
,
1312 OPT_USERDOMGROUPS
, "Get user domain groups", "SID" },
1313 { "user-sids", 0, POPT_ARG_STRING
, &string_arg
, OPT_USERSIDS
, "Get user group sids for user SID", "SID" },
1314 { "authenticate", 'a', POPT_ARG_STRING
, &string_arg
, 'a', "authenticate user", "user%password" },
1315 { "set-auth-user", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_AUTH_USER
, "Store user and password used by winbindd (root only)", "user%password" },
1316 { "getdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_GETDCNAME
,
1317 "Get a DC name for a foreign domain", "domainname" },
1318 { "dsgetdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_DSGETDCNAME
, "Find a DC for a domain", "domainname" },
1319 { "get-auth-user", 0, POPT_ARG_NONE
, NULL
, OPT_GET_AUTH_USER
, "Retrieve user and password used by winbindd (root only)", NULL
},
1320 { "ping", 'p', POPT_ARG_NONE
, 0, 'p', "Ping winbindd to see if it is alive" },
1321 { "domain", 0, POPT_ARG_STRING
, &opt_domain_name
, OPT_DOMAIN_NAME
, "Define to the domain to restrict operation", "domain" },
1322 #ifdef WITH_FAKE_KASERVER
1323 { "klog", 'k', POPT_ARG_STRING
, &string_arg
, 'k', "set an AFS token from winbind", "user%password" },
1326 { "krb5auth", 'K', POPT_ARG_STRING
, &string_arg
, 'K', "authenticate user using Kerberos", "user%password" },
1327 /* destroys wbinfo --help output */
1328 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1330 { "separator", 0, POPT_ARG_NONE
, 0, OPT_SEPARATOR
, "Get the active winbind separator", NULL
},
1331 POPT_COMMON_CONFIGFILE
1336 /* Samba client initialisation */
1342 pc
= poptGetContext("wbinfo", argc
, (const char **)argv
, long_options
, 0);
1344 /* Parse command line options */
1347 poptPrintHelp(pc
, stderr
, 0);
1351 while((opt
= poptGetNextOpt(pc
)) != -1) {
1352 /* get the generic configuration parameters like --domain */
1355 poptFreeContext(pc
);
1357 if (!lp_load(get_dyn_CONFIGFILE(), True
, False
, False
, True
)) {
1358 d_fprintf(stderr
, "wbinfo: error opening config file %s. Error was %s\n",
1359 get_dyn_CONFIGFILE(), strerror(errno
));
1368 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
1369 POPT_CONTEXT_KEEP_FIRST
);
1371 while((opt
= poptGetNextOpt(pc
)) != -1) {
1374 if (!print_domain_users(opt_domain_name
)) {
1375 d_fprintf(stderr
, "Error looking up domain users\n");
1380 if (!print_domain_groups(opt_domain_name
)) {
1381 d_fprintf(stderr
, "Error looking up domain groups\n");
1386 if (!wbinfo_lookupsid(string_arg
)) {
1387 d_fprintf(stderr
, "Could not lookup sid %s\n", string_arg
);
1392 if (!wbinfo_lookuprids(opt_domain_name
, string_arg
)) {
1393 d_fprintf(stderr
, "Could not lookup RIDs %s\n", string_arg
);
1398 if (!wbinfo_lookupname(string_arg
)) {
1399 d_fprintf(stderr
, "Could not lookup name %s\n", string_arg
);
1404 if (!wbinfo_wins_byname(string_arg
)) {
1405 d_fprintf(stderr
, "Could not lookup WINS by name %s\n", string_arg
);
1410 if (!wbinfo_wins_byip(string_arg
)) {
1411 d_fprintf(stderr
, "Could not lookup WINS by IP %s\n", string_arg
);
1416 if (!wbinfo_uid_to_sid(int_arg
)) {
1417 d_fprintf(stderr
, "Could not convert uid %d to sid\n", int_arg
);
1422 if (!wbinfo_gid_to_sid(int_arg
)) {
1423 d_fprintf(stderr
, "Could not convert gid %d to sid\n",
1429 if (!wbinfo_sid_to_uid(string_arg
)) {
1430 d_fprintf(stderr
, "Could not convert sid %s to uid\n",
1436 if (!wbinfo_sid_to_gid(string_arg
)) {
1437 d_fprintf(stderr
, "Could not convert sid %s to gid\n",
1442 case OPT_ALLOCATE_UID
:
1443 if (!wbinfo_allocate_uid()) {
1444 d_fprintf(stderr
, "Could not allocate a uid\n");
1448 case OPT_ALLOCATE_GID
:
1449 if (!wbinfo_allocate_gid()) {
1450 d_fprintf(stderr
, "Could not allocate a gid\n");
1455 if (!wbinfo_check_secret()) {
1456 d_fprintf(stderr
, "Could not check secret\n");
1461 if (!wbinfo_list_domains(False
)) {
1462 d_fprintf(stderr
, "Could not list trusted domains\n");
1467 if (!wbinfo_show_sequence(opt_domain_name
)) {
1468 d_fprintf(stderr
, "Could not show sequence numbers\n");
1473 if (!wbinfo_domain_info(string_arg
)) {
1474 d_fprintf(stderr
, "Could not get domain info\n");
1479 if (!wbinfo_get_userinfo(string_arg
)) {
1480 d_fprintf(stderr
, "Could not get info for user %s\n",
1486 if ( !wbinfo_get_uidinfo(int_arg
)) {
1487 d_fprintf(stderr
, "Could not get info for uid "
1492 case OPT_GROUP_INFO
:
1493 if ( !wbinfo_get_groupinfo(string_arg
)) {
1494 d_fprintf(stderr
, "Could not get info for "
1495 "group %s\n", string_arg
);
1500 if (!wbinfo_get_usergroups(string_arg
)) {
1501 d_fprintf(stderr
, "Could not get groups for user %s\n",
1507 if (!wbinfo_get_usersids(string_arg
)) {
1508 d_fprintf(stderr
, "Could not get group SIDs for user SID %s\n",
1513 case OPT_USERDOMGROUPS
:
1514 if (!wbinfo_get_userdomgroups(string_arg
)) {
1515 d_fprintf(stderr
, "Could not get user's domain groups "
1516 "for user SID %s\n", string_arg
);
1521 bool got_error
= False
;
1523 if (!wbinfo_auth(string_arg
)) {
1524 d_fprintf(stderr
, "Could not authenticate user %s with "
1525 "plaintext password\n", string_arg
);
1529 if (!wbinfo_auth_crap(string_arg
)) {
1530 d_fprintf(stderr
, "Could not authenticate user %s with "
1531 "challenge/response\n", string_arg
);
1540 uint32 flags
= WBFLAG_PAM_KRB5
|
1541 WBFLAG_PAM_CACHED_LOGIN
|
1542 WBFLAG_PAM_FALLBACK_AFTER_KRB5
|
1543 WBFLAG_PAM_INFO3_TEXT
;
1545 if (!wbinfo_auth_krb5(string_arg
, "FILE", flags
)) {
1546 d_fprintf(stderr
, "Could not authenticate user [%s] with "
1547 "Kerberos (ccache: %s)\n", string_arg
, "FILE");
1553 if (!wbinfo_klog(string_arg
)) {
1554 d_fprintf(stderr
, "Could not klog user\n");
1559 if (!wbinfo_ping()) {
1560 d_fprintf(stderr
, "could not ping winbindd!\n");
1564 case OPT_SET_AUTH_USER
:
1565 if (!wbinfo_set_auth_user(string_arg
)) {
1569 case OPT_GET_AUTH_USER
:
1570 wbinfo_get_auth_user();
1573 if (!wbinfo_getdcname(string_arg
)) {
1577 case OPT_DSGETDCNAME
:
1578 if (!wbinfo_dsgetdcname(string_arg
, 0)) {
1582 case OPT_SEPARATOR
: {
1583 const char sep
= winbind_separator_int(True
);
1587 d_printf("%c\n", sep
);
1590 case OPT_LIST_ALL_DOMAINS
:
1591 if (!wbinfo_list_domains(True
)) {
1595 case OPT_LIST_OWN_DOMAIN
:
1596 if (!wbinfo_list_own_domain()) {
1600 /* generic configuration options */
1601 case OPT_DOMAIN_NAME
:
1604 d_fprintf(stderr
, "Invalid option\n");
1605 poptPrintHelp(pc
, stderr
, 0);
1615 talloc_destroy(frame
);
1617 poptFreeContext(pc
);