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"
27 #define DBGC_CLASS DBGC_WINBIND
29 extern int winbindd_fd;
31 static char winbind_separator_int(bool strict)
33 struct winbindd_response response;
40 ZERO_STRUCT(response);
42 /* Send off request */
44 if (winbindd_request_response(WINBINDD_INFO, NULL, &response) !=
46 d_fprintf(stderr, "could not obtain winbind separator!\n");
50 /* HACK: (this module should not call lp_ funtions) */
51 return *lp_winbind_separator();
54 sep = response.data.info.winbind_separator;
58 d_fprintf(stderr, "winbind separator was NULL!\n");
62 /* HACK: (this module should not call lp_ funtions) */
63 sep = *lp_winbind_separator();
69 static char winbind_separator(void)
71 return winbind_separator_int(False);
74 static const char *get_winbind_domain(void)
76 struct winbindd_response response;
77 static fstring winbind_domain;
79 ZERO_STRUCT(response);
81 /* Send off request */
83 if (winbindd_request_response(WINBINDD_DOMAIN_NAME, NULL, &response) !=
85 d_fprintf(stderr, "could not obtain winbind domain name!\n");
87 /* HACK: (this module should not call lp_ funtions) */
88 return lp_workgroup();
91 fstrcpy(winbind_domain, response.data.domain_name);
93 return winbind_domain;
97 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
98 form DOMAIN/user into a domain and a user */
100 static bool parse_wbinfo_domain_user(const char *domuser, fstring domain,
104 char *p = strchr(domuser,winbind_separator());
107 /* Maybe it was a UPN? */
108 if ((p = strchr(domuser, '@')) != NULL) {
110 fstrcpy(user, domuser);
114 fstrcpy(user, domuser);
115 fstrcpy(domain, get_winbind_domain());
120 fstrcpy(domain, domuser);
121 domain[PTR_DIFF(p, domuser)] = 0;
127 /* pull pwent info for a given user */
129 static bool wbinfo_get_userinfo(char *user)
131 struct winbindd_request request;
132 struct winbindd_response response;
135 ZERO_STRUCT(request);
136 ZERO_STRUCT(response);
140 fstrcpy(request.data.username, user);
142 result = winbindd_request_response(WINBINDD_GETPWNAM, &request, &response);
144 if (result != NSS_STATUS_SUCCESS)
147 d_printf( "%s:%s:%d:%d:%s:%s:%s\n",
148 response.data.pw.pw_name,
149 response.data.pw.pw_passwd,
150 response.data.pw.pw_uid,
151 response.data.pw.pw_gid,
152 response.data.pw.pw_gecos,
153 response.data.pw.pw_dir,
154 response.data.pw.pw_shell );
159 /* pull pwent info for a given uid */
160 static bool wbinfo_get_uidinfo(int uid)
162 struct winbindd_request request;
163 struct winbindd_response response;
166 ZERO_STRUCT(request);
167 ZERO_STRUCT(response);
169 request.data.uid = uid;
171 result = winbindd_request_response(WINBINDD_GETPWUID, &request, &response);
173 if (result != NSS_STATUS_SUCCESS)
176 d_printf( "%s:%s:%d:%d:%s:%s:%s\n",
177 response.data.pw.pw_name,
178 response.data.pw.pw_passwd,
179 response.data.pw.pw_uid,
180 response.data.pw.pw_gid,
181 response.data.pw.pw_gecos,
182 response.data.pw.pw_dir,
183 response.data.pw.pw_shell );
188 /* pull grent for a given group */
189 static bool wbinfo_get_groupinfo(char *group)
191 struct winbindd_request request;
192 struct winbindd_response response;
195 ZERO_STRUCT(request);
196 ZERO_STRUCT(response);
200 fstrcpy(request.data.groupname, group);
202 result = winbindd_request_response(WINBINDD_GETGRNAM, &request,
205 if ( result != NSS_STATUS_SUCCESS)
208 d_printf( "%s:%s:%d\n",
209 response.data.gr.gr_name,
210 response.data.gr.gr_passwd,
211 response.data.gr.gr_gid );
216 /* List groups a user is a member of */
218 static bool wbinfo_get_usergroups(char *user)
220 struct winbindd_request request;
221 struct winbindd_response response;
225 ZERO_STRUCT(request);
226 ZERO_STRUCT(response);
230 fstrcpy(request.data.username, user);
232 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
234 if (result != NSS_STATUS_SUCCESS)
237 for (i = 0; i < response.data.num_entries; i++)
238 d_printf("%d\n", (int)((gid_t *)response.extra_data.data)[i]);
240 SAFE_FREE(response.extra_data.data);
246 /* List group SIDs a user SID is a member of */
247 static bool wbinfo_get_usersids(char *user_sid)
249 struct winbindd_request request;
250 struct winbindd_response response;
255 ZERO_STRUCT(request);
256 ZERO_STRUCT(response);
259 fstrcpy(request.data.sid, user_sid);
261 result = winbindd_request_response(WINBINDD_GETUSERSIDS, &request, &response);
263 if (result != NSS_STATUS_SUCCESS)
266 s = (const char *)response.extra_data.data;
267 for (i = 0; i < response.data.num_entries; i++) {
272 SAFE_FREE(response.extra_data.data);
277 static bool wbinfo_get_userdomgroups(const char *user_sid)
279 struct winbindd_request request;
280 struct winbindd_response response;
283 ZERO_STRUCT(request);
284 ZERO_STRUCT(response);
287 fstrcpy(request.data.sid, user_sid);
289 result = winbindd_request_response(WINBINDD_GETUSERDOMGROUPS, &request,
292 if (result != NSS_STATUS_SUCCESS)
295 if (response.data.num_entries != 0)
296 printf("%s", (char *)response.extra_data.data);
298 SAFE_FREE(response.extra_data.data);
303 /* Convert NetBIOS name to IP */
305 static bool wbinfo_wins_byname(char *name)
307 struct winbindd_request request;
308 struct winbindd_response response;
310 ZERO_STRUCT(request);
311 ZERO_STRUCT(response);
315 fstrcpy(request.data.winsreq, name);
317 if (winbindd_request_response(WINBINDD_WINS_BYNAME, &request, &response) !=
318 NSS_STATUS_SUCCESS) {
322 /* Display response */
324 d_printf("%s\n", response.data.winsresp);
329 /* Convert IP to NetBIOS name */
331 static bool wbinfo_wins_byip(char *ip)
333 struct winbindd_request request;
334 struct winbindd_response response;
336 ZERO_STRUCT(request);
337 ZERO_STRUCT(response);
341 fstrcpy(request.data.winsreq, ip);
343 if (winbindd_request_response(WINBINDD_WINS_BYIP, &request, &response) !=
344 NSS_STATUS_SUCCESS) {
348 /* Display response */
350 d_printf("%s\n", response.data.winsresp);
355 /* List trusted domains */
357 static bool wbinfo_list_domains(bool list_all_domains)
359 struct winbindd_request request;
360 struct winbindd_response response;
362 ZERO_STRUCT(request);
363 ZERO_STRUCT(response);
367 request.data.list_all_domains = list_all_domains;
369 if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM, &request, &response) !=
373 /* Display response */
375 if (response.extra_data.data) {
376 const char *extra_data = (char *)response.extra_data.data;
380 while(next_token(&extra_data, name, "\n", sizeof(fstring))) {
381 p = strchr(name, '\\');
383 d_fprintf(stderr, "Got invalid response: %s\n",
388 d_printf("%s\n", name);
391 SAFE_FREE(response.extra_data.data);
397 /* List own domain */
399 static bool wbinfo_list_own_domain(void)
401 d_printf("%s\n", get_winbind_domain());
406 /* show sequence numbers */
407 static bool wbinfo_show_sequence(const char *domain)
409 struct winbindd_request request;
410 struct winbindd_response response;
412 ZERO_STRUCT(response);
413 ZERO_STRUCT(request);
416 fstrcpy( request.domain_name, domain );
420 if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
424 /* Display response */
427 d_printf("%s : ", domain);
428 if (response.data.sequence_number == (uint32_t)-1) {
429 d_printf("DISCONNECTED\n");
431 d_printf("%d\n", response.data.sequence_number);
433 } else if (response.extra_data.data) {
434 char *extra_data = (char *)response.extra_data.data;
435 d_printf("%s", extra_data);
436 SAFE_FREE(response.extra_data.data);
442 /* Show domain info */
444 static bool wbinfo_domain_info(const char *domain_name)
446 struct winbindd_request request;
447 struct winbindd_response response;
449 ZERO_STRUCT(request);
450 ZERO_STRUCT(response);
452 if ((strequal(domain_name, ".")) || (domain_name[0] == '\0'))
453 fstrcpy(request.domain_name, get_winbind_domain());
455 fstrcpy(request.domain_name, domain_name);
459 if (winbindd_request_response(WINBINDD_DOMAIN_INFO, &request, &response) !=
463 /* Display response */
465 d_printf("Name : %s\n", response.data.domain_info.name);
466 d_printf("Alt_Name : %s\n", response.data.domain_info.alt_name);
468 d_printf("SID : %s\n", response.data.domain_info.sid);
470 d_printf("Active Directory : %s\n",
471 response.data.domain_info.active_directory ? "Yes" : "No");
472 d_printf("Native : %s\n",
473 response.data.domain_info.native_mode ? "Yes" : "No");
475 d_printf("Primary : %s\n",
476 response.data.domain_info.primary ? "Yes" : "No");
481 /* Get a foreign DC's name */
482 static bool wbinfo_getdcname(const char *domain_name)
484 struct winbindd_request request;
485 struct winbindd_response response;
487 ZERO_STRUCT(request);
488 ZERO_STRUCT(response);
490 fstrcpy(request.domain_name, domain_name);
494 if (winbindd_request_response(WINBINDD_GETDCNAME, &request, &response) !=
495 NSS_STATUS_SUCCESS) {
496 d_fprintf(stderr, "Could not get dc name for %s\n", domain_name);
500 /* Display response */
502 d_printf("%s\n", response.data.dc_name);
508 static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
510 struct winbindd_request request;
511 struct winbindd_response response;
513 ZERO_STRUCT(request);
514 ZERO_STRUCT(response);
516 fstrcpy(request.domain_name, domain_name);
517 request.flags = flags;
519 request.flags |= DS_DIRECTORY_SERVICE_REQUIRED;
523 if (winbindd_request_response(WINBINDD_DSGETDCNAME, &request, &response) !=
524 NSS_STATUS_SUCCESS) {
525 d_fprintf(stderr, "Could not find dc for %s\n", domain_name);
529 /* Display response */
531 d_printf("%s\n", response.data.dc_name);
536 /* Check trust account password */
538 static bool wbinfo_check_secret(void)
540 struct winbindd_response response;
543 ZERO_STRUCT(response);
545 result = winbindd_request_response(WINBINDD_CHECK_MACHACC, NULL, &response);
547 d_printf("checking the trust secret via RPC calls %s\n",
548 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
550 if (result != NSS_STATUS_SUCCESS)
551 d_fprintf(stderr, "error code was %s (0x%x)\n",
552 response.data.auth.nt_status_string,
553 response.data.auth.nt_status);
555 return result == NSS_STATUS_SUCCESS;
558 /* Convert uid to sid */
560 static bool wbinfo_uid_to_sid(uid_t uid)
562 struct winbindd_request request;
563 struct winbindd_response response;
565 ZERO_STRUCT(request);
566 ZERO_STRUCT(response);
570 request.data.uid = uid;
572 if (winbindd_request_response(WINBINDD_UID_TO_SID, &request, &response) !=
576 /* Display response */
578 d_printf("%s\n", response.data.sid.sid);
583 /* Convert gid to sid */
585 static bool wbinfo_gid_to_sid(gid_t gid)
587 struct winbindd_request request;
588 struct winbindd_response response;
590 ZERO_STRUCT(request);
591 ZERO_STRUCT(response);
595 request.data.gid = gid;
597 if (winbindd_request_response(WINBINDD_GID_TO_SID, &request, &response) !=
601 /* Display response */
603 d_printf("%s\n", response.data.sid.sid);
608 /* Convert sid to uid */
610 static bool wbinfo_sid_to_uid(char *sid)
612 struct winbindd_request request;
613 struct winbindd_response response;
615 ZERO_STRUCT(request);
616 ZERO_STRUCT(response);
620 fstrcpy(request.data.sid, sid);
622 if (winbindd_request_response(WINBINDD_SID_TO_UID, &request, &response) !=
626 /* Display response */
628 d_printf("%d\n", (int)response.data.uid);
633 static bool wbinfo_sid_to_gid(char *sid)
635 struct winbindd_request request;
636 struct winbindd_response response;
638 ZERO_STRUCT(request);
639 ZERO_STRUCT(response);
643 fstrcpy(request.data.sid, sid);
645 if (winbindd_request_response(WINBINDD_SID_TO_GID, &request, &response) !=
649 /* Display response */
651 d_printf("%d\n", (int)response.data.gid);
656 static bool wbinfo_allocate_uid(void)
660 if (!winbind_allocate_uid(&uid))
663 d_printf("New uid: %d\n", uid);
668 static bool wbinfo_allocate_gid(void)
672 if (!winbind_allocate_gid(&gid))
675 d_printf("New gid: %d\n", gid);
680 /* Convert sid to string */
682 static bool wbinfo_lookupsid(char *sid)
684 struct winbindd_request request;
685 struct winbindd_response response;
687 ZERO_STRUCT(request);
688 ZERO_STRUCT(response);
690 /* Send off request */
692 fstrcpy(request.data.sid, sid);
694 if (winbindd_request_response(WINBINDD_LOOKUPSID, &request, &response) !=
698 /* Display response */
700 d_printf("%s%c%s %d\n", response.data.name.dom_name,
701 winbind_separator(), response.data.name.name,
702 response.data.name.type);
707 /* Lookup a list of RIDs */
709 static bool wbinfo_lookuprids(char *domain, char *arg)
718 enum lsa_SidType *types;
719 const char *domain_name;
721 struct winbindd_request request;
722 struct winbindd_response response;
724 ZERO_STRUCT(request);
725 ZERO_STRUCT(response);
727 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0'))
728 fstrcpy(request.domain_name, get_winbind_domain());
730 fstrcpy(request.domain_name, domain);
734 if (winbindd_request_response(WINBINDD_DOMAIN_INFO, &request, &response) !=
735 NSS_STATUS_SUCCESS) {
736 d_printf("Could not get domain sid for %s\n", request.domain_name);
740 if (!string_to_sid(&sid, response.data.domain_info.sid)) {
741 d_printf("Could not convert %s to sid\n", response.data.domain_info.sid);
745 mem_ctx = talloc_new(NULL);
746 if (mem_ctx == NULL) {
747 d_printf("talloc_new failed\n");
755 while (next_token(&p, ridstr, " ,\n", sizeof(ridstr))) {
756 uint32 rid = strtoul(ridstr, NULL, 10);
757 ADD_TO_ARRAY(mem_ctx, uint32, rid, &rids, &num_rids);
761 TALLOC_FREE(mem_ctx);
765 if (!winbind_lookup_rids(mem_ctx, &sid, num_rids, rids,
766 &domain_name, &names, &types)) {
767 d_printf("winbind_lookup_rids failed\n");
768 TALLOC_FREE(mem_ctx);
772 d_printf("Domain: %s\n", domain_name);
774 for (i=0; i<num_rids; i++) {
775 d_printf("%8d: %s (%s)\n", rids[i], names[i],
776 sid_type_lookup(types[i]));
779 TALLOC_FREE(mem_ctx);
783 /* Convert string to sid */
785 static bool wbinfo_lookupname(char *name)
787 struct winbindd_request request;
788 struct winbindd_response response;
790 /* Send off request */
792 ZERO_STRUCT(request);
793 ZERO_STRUCT(response);
795 parse_wbinfo_domain_user(name, request.data.name.dom_name,
796 request.data.name.name);
798 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
802 /* Display response */
804 d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
809 /* Authenticate a user with a plaintext password */
811 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
813 struct winbindd_request request;
814 struct winbindd_response response;
818 /* Send off request */
820 ZERO_STRUCT(request);
821 ZERO_STRUCT(response);
823 p = strchr(username, '%');
827 fstrcpy(request.data.auth.user, username);
828 fstrcpy(request.data.auth.pass, p + 1);
831 fstrcpy(request.data.auth.user, username);
833 request.flags = flags;
835 fstrcpy(request.data.auth.krb5_cc_type, cctype);
837 request.data.auth.uid = geteuid();
839 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
841 /* Display response */
843 d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n",
844 username, (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", cctype);
846 if (response.data.auth.nt_status)
847 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
848 response.data.auth.nt_status_string,
849 response.data.auth.nt_status,
850 response.data.auth.error_string);
852 if (result == NSS_STATUS_SUCCESS) {
854 if (request.flags & WBFLAG_PAM_INFO3_TEXT) {
855 if (response.data.auth.info3.user_flgs & LOGON_CACHED_ACCOUNT) {
856 d_printf("user_flgs: LOGON_CACHED_ACCOUNT\n");
860 if (response.data.auth.krb5ccname[0] != '\0') {
861 d_printf("credentials were put in: %s\n", response.data.auth.krb5ccname);
863 d_printf("no credentials cached\n");
867 return result == NSS_STATUS_SUCCESS;
870 /* Authenticate a user with a plaintext password */
872 static bool wbinfo_auth(char *username)
874 struct winbindd_request request;
875 struct winbindd_response response;
879 /* Send off request */
881 ZERO_STRUCT(request);
882 ZERO_STRUCT(response);
884 p = strchr(username, '%');
888 fstrcpy(request.data.auth.user, username);
889 fstrcpy(request.data.auth.pass, p + 1);
892 fstrcpy(request.data.auth.user, username);
894 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
896 /* Display response */
898 d_printf("plaintext password authentication %s\n",
899 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
901 if (response.data.auth.nt_status)
902 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
903 response.data.auth.nt_status_string,
904 response.data.auth.nt_status,
905 response.data.auth.error_string);
907 return result == NSS_STATUS_SUCCESS;
910 /* Authenticate a user with a challenge/response */
912 static bool wbinfo_auth_crap(char *username)
914 struct winbindd_request request;
915 struct winbindd_response response;
922 /* Send off request */
924 ZERO_STRUCT(request);
925 ZERO_STRUCT(response);
927 p = strchr(username, '%');
931 fstrcpy(pass, p + 1);
934 parse_wbinfo_domain_user(username, name_domain, name_user);
936 request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
938 fstrcpy(request.data.auth_crap.user, name_user);
940 fstrcpy(request.data.auth_crap.domain,
943 generate_random_buffer(request.data.auth_crap.chal, 8);
945 if (lp_client_ntlmv2_auth()) {
946 DATA_BLOB server_chal;
947 DATA_BLOB names_blob;
949 DATA_BLOB lm_response;
950 DATA_BLOB nt_response;
952 server_chal = data_blob(request.data.auth_crap.chal, 8);
954 /* Pretend this is a login to 'us', for blob purposes */
955 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
957 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
959 &lm_response, &nt_response, NULL)) {
960 data_blob_free(&names_blob);
961 data_blob_free(&server_chal);
964 data_blob_free(&names_blob);
965 data_blob_free(&server_chal);
967 memcpy(request.data.auth_crap.nt_resp, nt_response.data,
968 MIN(nt_response.length,
969 sizeof(request.data.auth_crap.nt_resp)));
970 request.data.auth_crap.nt_resp_len = nt_response.length;
972 memcpy(request.data.auth_crap.lm_resp, lm_response.data,
973 MIN(lm_response.length,
974 sizeof(request.data.auth_crap.lm_resp)));
975 request.data.auth_crap.lm_resp_len = lm_response.length;
977 data_blob_free(&nt_response);
978 data_blob_free(&lm_response);
981 if (lp_client_lanman_auth()
982 && SMBencrypt(pass, request.data.auth_crap.chal,
983 (uchar *)request.data.auth_crap.lm_resp)) {
984 request.data.auth_crap.lm_resp_len = 24;
986 request.data.auth_crap.lm_resp_len = 0;
988 SMBNTencrypt(pass, request.data.auth_crap.chal,
989 (uchar *)request.data.auth_crap.nt_resp);
991 request.data.auth_crap.nt_resp_len = 24;
994 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
996 /* Display response */
998 d_printf("challenge/response password authentication %s\n",
999 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1001 if (response.data.auth.nt_status)
1002 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1003 response.data.auth.nt_status_string,
1004 response.data.auth.nt_status,
1005 response.data.auth.error_string);
1007 return result == NSS_STATUS_SUCCESS;
1010 /* Authenticate a user with a plaintext password and set a token */
1012 static bool wbinfo_klog(char *username)
1014 struct winbindd_request request;
1015 struct winbindd_response response;
1019 /* Send off request */
1021 ZERO_STRUCT(request);
1022 ZERO_STRUCT(response);
1024 p = strchr(username, '%');
1028 fstrcpy(request.data.auth.user, username);
1029 fstrcpy(request.data.auth.pass, p + 1);
1032 fstrcpy(request.data.auth.user, username);
1033 fstrcpy(request.data.auth.pass, getpass("Password: "));
1036 request.flags |= WBFLAG_PAM_AFS_TOKEN;
1038 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
1040 /* Display response */
1042 d_printf("plaintext password authentication %s\n",
1043 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1045 if (response.data.auth.nt_status)
1046 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1047 response.data.auth.nt_status_string,
1048 response.data.auth.nt_status,
1049 response.data.auth.error_string);
1051 if (result != NSS_STATUS_SUCCESS)
1054 if (response.extra_data.data == NULL) {
1055 d_fprintf(stderr, "Did not get token data\n");
1059 if (!afs_settoken_str((char *)response.extra_data.data)) {
1060 d_fprintf(stderr, "Could not set token\n");
1064 d_printf("Successfully created AFS token\n");
1068 /* Print domain users */
1070 static bool print_domain_users(const char *domain)
1072 struct winbindd_request request;
1073 struct winbindd_response response;
1074 const char *extra_data;
1077 /* Send request to winbind daemon */
1079 ZERO_STRUCT(request);
1080 ZERO_STRUCT(response);
1083 /* '.' is the special sign for our own domain */
1084 if ( strequal(domain, ".") )
1085 fstrcpy( request.domain_name, get_winbind_domain() );
1087 fstrcpy( request.domain_name, domain );
1090 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
1094 /* Look through extra data */
1096 if (!response.extra_data.data)
1099 extra_data = (const char *)response.extra_data.data;
1101 while(next_token(&extra_data, name, ",", sizeof(fstring)))
1102 d_printf("%s\n", name);
1104 SAFE_FREE(response.extra_data.data);
1109 /* Print domain groups */
1111 static bool print_domain_groups(const char *domain)
1113 struct winbindd_request request;
1114 struct winbindd_response response;
1115 const char *extra_data;
1118 ZERO_STRUCT(request);
1119 ZERO_STRUCT(response);
1122 if ( strequal(domain, ".") )
1123 fstrcpy( request.domain_name, get_winbind_domain() );
1125 fstrcpy( request.domain_name, domain );
1128 if (winbindd_request_response(WINBINDD_LIST_GROUPS, &request, &response) !=
1132 /* Look through extra data */
1134 if (!response.extra_data.data)
1137 extra_data = (const char *)response.extra_data.data;
1139 while(next_token(&extra_data, name, ",", sizeof(fstring)))
1140 d_printf("%s\n", name);
1142 SAFE_FREE(response.extra_data.data);
1147 /* Set the authorised user for winbindd access in secrets.tdb */
1149 static bool wbinfo_set_auth_user(char *username)
1151 const char *password;
1153 fstring user, domain;
1155 /* Separate into user and password */
1157 parse_wbinfo_domain_user(username, domain, user);
1159 p = strchr(user, '%');
1165 char *thepass = getpass("Password: ");
1172 /* Store or remove DOMAIN\username%password in secrets.tdb */
1178 if (!secrets_store(SECRETS_AUTH_USER, user,
1179 strlen(user) + 1)) {
1180 d_fprintf(stderr, "error storing username\n");
1184 /* We always have a domain name added by the
1185 parse_wbinfo_domain_user() function. */
1187 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
1188 strlen(domain) + 1)) {
1189 d_fprintf(stderr, "error storing domain name\n");
1194 secrets_delete(SECRETS_AUTH_USER);
1195 secrets_delete(SECRETS_AUTH_DOMAIN);
1200 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
1201 strlen(password) + 1)) {
1202 d_fprintf(stderr, "error storing password\n");
1207 secrets_delete(SECRETS_AUTH_PASSWORD);
1212 static void wbinfo_get_auth_user(void)
1214 char *user, *domain, *password;
1216 /* Lift data from secrets file */
1218 secrets_fetch_ipc_userpass(&user, &domain, &password);
1220 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
1224 SAFE_FREE(password);
1225 d_printf("No authorised user configured\n");
1229 /* Pretty print authorised user info */
1231 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
1232 user, password ? "%" : "", password ? password : "");
1236 SAFE_FREE(password);
1239 static bool wbinfo_ping(void)
1243 result = winbindd_request_response(WINBINDD_PING, NULL, NULL);
1245 /* Display response */
1247 d_printf("Ping to winbindd %s on fd %d\n",
1248 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
1250 return result == NSS_STATUS_SUCCESS;
1256 OPT_SET_AUTH_USER = 1000,
1267 OPT_LIST_ALL_DOMAINS,
1268 OPT_LIST_OWN_DOMAIN,
1273 int main(int argc, char **argv, char **envp)
1278 static char *string_arg;
1279 static char *opt_domain_name;
1283 struct poptOption long_options[] = {
1286 /* longName, shortName, argInfo, argPtr, value, descrip,
1289 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1290 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1291 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1292 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1293 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1294 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1295 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1296 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1297 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1298 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1299 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1300 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1301 "Get a new UID out of idmap" },
1302 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1303 "Get a new GID out of idmap" },
1304 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1305 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1306 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1307 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1308 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1309 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1310 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1311 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1312 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1313 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1314 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1315 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1316 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1317 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1318 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1319 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1320 "Get a DC name for a foreign domain", "domainname" },
1321 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
1322 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1323 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1324 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1325 #ifdef WITH_FAKE_KASERVER
1326 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1329 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1330 /* destroys wbinfo --help output */
1331 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1333 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1338 /* Samba client initialisation */
1341 if (!lp_load(dyn_CONFIGFILE, True, False, False, True)) {
1342 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1343 dyn_CONFIGFILE, strerror(errno));
1354 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1356 /* Parse command line options */
1359 poptPrintHelp(pc, stderr, 0);
1363 while((opt = poptGetNextOpt(pc)) != -1) {
1364 /* get the generic configuration parameters like --domain */
1367 poptFreeContext(pc);
1369 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1370 POPT_CONTEXT_KEEP_FIRST);
1372 while((opt = poptGetNextOpt(pc)) != -1) {
1375 if (!print_domain_users(opt_domain_name)) {
1376 d_fprintf(stderr, "Error looking up domain users\n");
1381 if (!print_domain_groups(opt_domain_name)) {
1382 d_fprintf(stderr, "Error looking up domain groups\n");
1387 if (!wbinfo_lookupsid(string_arg)) {
1388 d_fprintf(stderr, "Could not lookup sid %s\n", string_arg);
1393 if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
1394 d_fprintf(stderr, "Could not lookup RIDs %s\n", string_arg);
1399 if (!wbinfo_lookupname(string_arg)) {
1400 d_fprintf(stderr, "Could not lookup name %s\n", string_arg);
1405 if (!wbinfo_wins_byname(string_arg)) {
1406 d_fprintf(stderr, "Could not lookup WINS by name %s\n", string_arg);
1411 if (!wbinfo_wins_byip(string_arg)) {
1412 d_fprintf(stderr, "Could not lookup WINS by IP %s\n", string_arg);
1417 if (!wbinfo_uid_to_sid(int_arg)) {
1418 d_fprintf(stderr, "Could not convert uid %d to sid\n", int_arg);
1423 if (!wbinfo_gid_to_sid(int_arg)) {
1424 d_fprintf(stderr, "Could not convert gid %d to sid\n",
1430 if (!wbinfo_sid_to_uid(string_arg)) {
1431 d_fprintf(stderr, "Could not convert sid %s to uid\n",
1437 if (!wbinfo_sid_to_gid(string_arg)) {
1438 d_fprintf(stderr, "Could not convert sid %s to gid\n",
1443 case OPT_ALLOCATE_UID:
1444 if (!wbinfo_allocate_uid()) {
1445 d_fprintf(stderr, "Could not allocate a uid\n");
1449 case OPT_ALLOCATE_GID:
1450 if (!wbinfo_allocate_gid()) {
1451 d_fprintf(stderr, "Could not allocate a gid\n");
1456 if (!wbinfo_check_secret()) {
1457 d_fprintf(stderr, "Could not check secret\n");
1462 if (!wbinfo_list_domains(False)) {
1463 d_fprintf(stderr, "Could not list trusted domains\n");
1468 if (!wbinfo_show_sequence(opt_domain_name)) {
1469 d_fprintf(stderr, "Could not show sequence numbers\n");
1474 if (!wbinfo_domain_info(string_arg)) {
1475 d_fprintf(stderr, "Could not get domain info\n");
1480 if (!wbinfo_get_userinfo(string_arg)) {
1481 d_fprintf(stderr, "Could not get info for user %s\n",
1487 if ( !wbinfo_get_uidinfo(int_arg)) {
1488 d_fprintf(stderr, "Could not get info for uid "
1493 case OPT_GROUP_INFO:
1494 if ( !wbinfo_get_groupinfo(string_arg)) {
1495 d_fprintf(stderr, "Could not get info for "
1496 "group %s\n", string_arg);
1501 if (!wbinfo_get_usergroups(string_arg)) {
1502 d_fprintf(stderr, "Could not get groups for user %s\n",
1508 if (!wbinfo_get_usersids(string_arg)) {
1509 d_fprintf(stderr, "Could not get group SIDs for user SID %s\n",
1514 case OPT_USERDOMGROUPS:
1515 if (!wbinfo_get_userdomgroups(string_arg)) {
1516 d_fprintf(stderr, "Could not get user's domain groups "
1517 "for user SID %s\n", string_arg);
1522 bool got_error = False;
1524 if (!wbinfo_auth(string_arg)) {
1525 d_fprintf(stderr, "Could not authenticate user %s with "
1526 "plaintext password\n", string_arg);
1530 if (!wbinfo_auth_crap(string_arg)) {
1531 d_fprintf(stderr, "Could not authenticate user %s with "
1532 "challenge/response\n", string_arg);
1541 uint32 flags = WBFLAG_PAM_KRB5 |
1542 WBFLAG_PAM_CACHED_LOGIN |
1543 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
1544 WBFLAG_PAM_INFO3_TEXT;
1546 if (!wbinfo_auth_krb5(string_arg, "FILE", flags)) {
1547 d_fprintf(stderr, "Could not authenticate user [%s] with "
1548 "Kerberos (ccache: %s)\n", string_arg, "FILE");
1554 if (!wbinfo_klog(string_arg)) {
1555 d_fprintf(stderr, "Could not klog user\n");
1560 if (!wbinfo_ping()) {
1561 d_fprintf(stderr, "could not ping winbindd!\n");
1565 case OPT_SET_AUTH_USER:
1566 if (!wbinfo_set_auth_user(string_arg)) {
1570 case OPT_GET_AUTH_USER:
1571 wbinfo_get_auth_user();
1574 if (!wbinfo_getdcname(string_arg)) {
1578 case OPT_DSGETDCNAME:
1579 if (!wbinfo_dsgetdcname(string_arg, 0)) {
1583 case OPT_SEPARATOR: {
1584 const char sep = winbind_separator_int(True);
1588 d_printf("%c\n", sep);
1591 case OPT_LIST_ALL_DOMAINS:
1592 if (!wbinfo_list_domains(True)) {
1596 case OPT_LIST_OWN_DOMAIN:
1597 if (!wbinfo_list_own_domain()) {
1601 /* generic configuration options */
1602 case OPT_DOMAIN_NAME:
1605 d_fprintf(stderr, "Invalid option\n");
1606 poptPrintHelp(pc, stderr, 0);
1616 poptFreeContext(pc);