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/>.
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 struct winbindd_response response;
78 static fstring winbind_domain;
80 ZERO_STRUCT(response);
82 /* Send off request */
84 if (winbindd_request_response(WINBINDD_DOMAIN_NAME, NULL, &response) !=
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, response.data.domain_name);
94 return winbind_domain;
98 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
99 form DOMAIN/user into a domain and a user */
101 static BOOL parse_wbinfo_domain_user(const char *domuser, fstring domain,
105 char *p = strchr(domuser,winbind_separator());
108 /* Maybe it was a UPN? */
109 if ((p = strchr(domuser, '@')) != NULL) {
111 fstrcpy(user, domuser);
115 fstrcpy(user, domuser);
116 fstrcpy(domain, get_winbind_domain());
121 fstrcpy(domain, domuser);
122 domain[PTR_DIFF(p, domuser)] = 0;
128 /* pull pwent info for a given user */
130 static BOOL wbinfo_get_userinfo(char *user)
132 struct winbindd_request request;
133 struct winbindd_response response;
136 ZERO_STRUCT(request);
137 ZERO_STRUCT(response);
141 fstrcpy(request.data.username, user);
143 result = winbindd_request_response(WINBINDD_GETPWNAM, &request, &response);
145 if (result != NSS_STATUS_SUCCESS)
148 d_printf( "%s:%s:%d:%d:%s:%s:%s\n",
149 response.data.pw.pw_name,
150 response.data.pw.pw_passwd,
151 response.data.pw.pw_uid,
152 response.data.pw.pw_gid,
153 response.data.pw.pw_gecos,
154 response.data.pw.pw_dir,
155 response.data.pw.pw_shell );
160 /* pull pwent info for a given uid */
161 static BOOL wbinfo_get_uidinfo(int uid)
163 struct winbindd_request request;
164 struct winbindd_response response;
167 ZERO_STRUCT(request);
168 ZERO_STRUCT(response);
170 request.data.uid = uid;
172 result = winbindd_request_response(WINBINDD_GETPWUID, &request, &response);
174 if (result != NSS_STATUS_SUCCESS)
177 d_printf( "%s:%s:%d:%d:%s:%s:%s\n",
178 response.data.pw.pw_name,
179 response.data.pw.pw_passwd,
180 response.data.pw.pw_uid,
181 response.data.pw.pw_gid,
182 response.data.pw.pw_gecos,
183 response.data.pw.pw_dir,
184 response.data.pw.pw_shell );
189 /* pull grent for a given group */
190 static BOOL wbinfo_get_groupinfo(char *group)
192 struct winbindd_request request;
193 struct winbindd_response response;
196 ZERO_STRUCT(request);
197 ZERO_STRUCT(response);
201 fstrcpy(request.data.groupname, group);
203 result = winbindd_request_response(WINBINDD_GETGRNAM, &request,
206 if ( result != NSS_STATUS_SUCCESS)
209 d_printf( "%s:%s:%d\n",
210 response.data.gr.gr_name,
211 response.data.gr.gr_passwd,
212 response.data.gr.gr_gid );
217 /* List groups a user is a member of */
219 static BOOL wbinfo_get_usergroups(char *user)
221 struct winbindd_request request;
222 struct winbindd_response response;
226 ZERO_STRUCT(request);
227 ZERO_STRUCT(response);
231 fstrcpy(request.data.username, user);
233 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
235 if (result != NSS_STATUS_SUCCESS)
238 for (i = 0; i < response.data.num_entries; i++)
239 d_printf("%d\n", (int)((gid_t *)response.extra_data.data)[i]);
241 SAFE_FREE(response.extra_data.data);
247 /* List group SIDs a user SID is a member of */
248 static BOOL wbinfo_get_usersids(char *user_sid)
250 struct winbindd_request request;
251 struct winbindd_response response;
256 ZERO_STRUCT(request);
257 ZERO_STRUCT(response);
260 fstrcpy(request.data.sid, user_sid);
262 result = winbindd_request_response(WINBINDD_GETUSERSIDS, &request, &response);
264 if (result != NSS_STATUS_SUCCESS)
267 s = (const char *)response.extra_data.data;
268 for (i = 0; i < response.data.num_entries; i++) {
273 SAFE_FREE(response.extra_data.data);
278 static BOOL wbinfo_get_userdomgroups(const char *user_sid)
280 struct winbindd_request request;
281 struct winbindd_response response;
284 ZERO_STRUCT(request);
285 ZERO_STRUCT(response);
288 fstrcpy(request.data.sid, user_sid);
290 result = winbindd_request_response(WINBINDD_GETUSERDOMGROUPS, &request,
293 if (result != NSS_STATUS_SUCCESS)
296 if (response.data.num_entries != 0)
297 printf("%s", (char *)response.extra_data.data);
299 SAFE_FREE(response.extra_data.data);
304 /* Convert NetBIOS name to IP */
306 static BOOL wbinfo_wins_byname(char *name)
308 struct winbindd_request request;
309 struct winbindd_response response;
311 ZERO_STRUCT(request);
312 ZERO_STRUCT(response);
316 fstrcpy(request.data.winsreq, name);
318 if (winbindd_request_response(WINBINDD_WINS_BYNAME, &request, &response) !=
319 NSS_STATUS_SUCCESS) {
323 /* Display response */
325 d_printf("%s\n", response.data.winsresp);
330 /* Convert IP to NetBIOS name */
332 static BOOL wbinfo_wins_byip(char *ip)
334 struct winbindd_request request;
335 struct winbindd_response response;
337 ZERO_STRUCT(request);
338 ZERO_STRUCT(response);
342 fstrcpy(request.data.winsreq, ip);
344 if (winbindd_request_response(WINBINDD_WINS_BYIP, &request, &response) !=
345 NSS_STATUS_SUCCESS) {
349 /* Display response */
351 d_printf("%s\n", response.data.winsresp);
356 /* List trusted domains */
358 static BOOL wbinfo_list_domains(BOOL list_all_domains)
360 struct winbindd_request request;
361 struct winbindd_response response;
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;
381 while(next_token(&extra_data, name, "\n", sizeof(fstring))) {
382 p = strchr(name, '\\');
384 d_fprintf(stderr, "Got invalid response: %s\n",
389 d_printf("%s\n", name);
392 SAFE_FREE(response.extra_data.data);
398 /* List own domain */
400 static BOOL wbinfo_list_own_domain(void)
402 d_printf("%s\n", get_winbind_domain());
407 /* show sequence numbers */
408 static BOOL wbinfo_show_sequence(const char *domain)
410 struct winbindd_request request;
411 struct winbindd_response response;
413 ZERO_STRUCT(response);
414 ZERO_STRUCT(request);
417 fstrcpy( request.domain_name, domain );
421 if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
425 /* Display response */
427 if (response.extra_data.data) {
428 char *extra_data = (char *)response.extra_data.data;
429 d_printf("%s", extra_data);
430 SAFE_FREE(response.extra_data.data);
436 /* Show domain info */
438 static BOOL wbinfo_domain_info(const char *domain_name)
440 struct winbindd_request request;
441 struct winbindd_response response;
443 ZERO_STRUCT(request);
444 ZERO_STRUCT(response);
446 if ((strequal(domain_name, ".")) || (domain_name[0] == '\0'))
447 fstrcpy(request.domain_name, get_winbind_domain());
449 fstrcpy(request.domain_name, domain_name);
453 if (winbindd_request_response(WINBINDD_DOMAIN_INFO, &request, &response) !=
457 /* Display response */
459 d_printf("Name : %s\n", response.data.domain_info.name);
460 d_printf("Alt_Name : %s\n", response.data.domain_info.alt_name);
462 d_printf("SID : %s\n", response.data.domain_info.sid);
464 d_printf("Active Directory : %s\n",
465 response.data.domain_info.active_directory ? "Yes" : "No");
466 d_printf("Native : %s\n",
467 response.data.domain_info.native_mode ? "Yes" : "No");
469 d_printf("Primary : %s\n",
470 response.data.domain_info.primary ? "Yes" : "No");
472 d_printf("Sequence : %d\n", response.data.domain_info.sequence_number);
477 /* Get a foreign DC's name */
478 static BOOL wbinfo_getdcname(const char *domain_name)
480 struct winbindd_request request;
481 struct winbindd_response response;
483 ZERO_STRUCT(request);
484 ZERO_STRUCT(response);
486 fstrcpy(request.domain_name, domain_name);
490 if (winbindd_request_response(WINBINDD_GETDCNAME, &request, &response) !=
491 NSS_STATUS_SUCCESS) {
492 d_fprintf(stderr, "Could not get dc name for %s\n", domain_name);
496 /* Display response */
498 d_printf("%s\n", response.data.dc_name);
503 /* Check trust account password */
505 static BOOL wbinfo_check_secret(void)
507 struct winbindd_response response;
510 ZERO_STRUCT(response);
512 result = winbindd_request_response(WINBINDD_CHECK_MACHACC, NULL, &response);
514 d_printf("checking the trust secret via RPC calls %s\n",
515 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
517 if (result != NSS_STATUS_SUCCESS)
518 d_fprintf(stderr, "error code was %s (0x%x)\n",
519 response.data.auth.nt_status_string,
520 response.data.auth.nt_status);
522 return result == NSS_STATUS_SUCCESS;
525 /* Convert uid to sid */
527 static BOOL wbinfo_uid_to_sid(uid_t uid)
529 struct winbindd_request request;
530 struct winbindd_response response;
532 ZERO_STRUCT(request);
533 ZERO_STRUCT(response);
537 request.data.uid = uid;
539 if (winbindd_request_response(WINBINDD_UID_TO_SID, &request, &response) !=
543 /* Display response */
545 d_printf("%s\n", response.data.sid.sid);
550 /* Convert gid to sid */
552 static BOOL wbinfo_gid_to_sid(gid_t gid)
554 struct winbindd_request request;
555 struct winbindd_response response;
557 ZERO_STRUCT(request);
558 ZERO_STRUCT(response);
562 request.data.gid = gid;
564 if (winbindd_request_response(WINBINDD_GID_TO_SID, &request, &response) !=
568 /* Display response */
570 d_printf("%s\n", response.data.sid.sid);
575 /* Convert sid to uid */
577 static BOOL wbinfo_sid_to_uid(char *sid)
579 struct winbindd_request request;
580 struct winbindd_response response;
582 ZERO_STRUCT(request);
583 ZERO_STRUCT(response);
587 fstrcpy(request.data.sid, sid);
589 if (winbindd_request_response(WINBINDD_SID_TO_UID, &request, &response) !=
593 /* Display response */
595 d_printf("%d\n", (int)response.data.uid);
600 static BOOL wbinfo_sid_to_gid(char *sid)
602 struct winbindd_request request;
603 struct winbindd_response response;
605 ZERO_STRUCT(request);
606 ZERO_STRUCT(response);
610 fstrcpy(request.data.sid, sid);
612 if (winbindd_request_response(WINBINDD_SID_TO_GID, &request, &response) !=
616 /* Display response */
618 d_printf("%d\n", (int)response.data.gid);
623 static BOOL wbinfo_allocate_uid(void)
627 if (!winbind_allocate_uid(&uid))
630 d_printf("New uid: %d\n", uid);
635 static BOOL wbinfo_allocate_gid(void)
639 if (!winbind_allocate_gid(&gid))
642 d_printf("New gid: %d\n", gid);
647 /* Convert sid to string */
649 static BOOL wbinfo_lookupsid(char *sid)
651 struct winbindd_request request;
652 struct winbindd_response response;
654 ZERO_STRUCT(request);
655 ZERO_STRUCT(response);
657 /* Send off request */
659 fstrcpy(request.data.sid, sid);
661 if (winbindd_request_response(WINBINDD_LOOKUPSID, &request, &response) !=
665 /* Display response */
667 d_printf("%s%c%s %d\n", response.data.name.dom_name,
668 winbind_separator(), response.data.name.name,
669 response.data.name.type);
674 /* Lookup a list of RIDs */
676 static BOOL wbinfo_lookuprids(char *domain, char *arg)
685 enum lsa_SidType *types;
686 const char *domain_name;
688 struct winbindd_request request;
689 struct winbindd_response response;
691 ZERO_STRUCT(request);
692 ZERO_STRUCT(response);
694 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0'))
695 fstrcpy(request.domain_name, get_winbind_domain());
697 fstrcpy(request.domain_name, domain);
701 if (winbindd_request_response(WINBINDD_DOMAIN_INFO, &request, &response) !=
702 NSS_STATUS_SUCCESS) {
703 d_printf("Could not get domain sid for %s\n", request.domain_name);
707 if (!string_to_sid(&sid, response.data.domain_info.sid)) {
708 d_printf("Could not convert %s to sid\n", response.data.domain_info.sid);
712 mem_ctx = talloc_new(NULL);
713 if (mem_ctx == NULL) {
714 d_printf("talloc_new failed\n");
722 while (next_token(&p, ridstr, " ,\n", sizeof(ridstr))) {
723 uint32 rid = strtoul(ridstr, NULL, 10);
724 ADD_TO_ARRAY(mem_ctx, uint32, rid, &rids, &num_rids);
728 TALLOC_FREE(mem_ctx);
732 if (!winbind_lookup_rids(mem_ctx, &sid, num_rids, rids,
733 &domain_name, &names, &types)) {
734 d_printf("winbind_lookup_rids failed\n");
735 TALLOC_FREE(mem_ctx);
739 d_printf("Domain: %s\n", domain_name);
741 for (i=0; i<num_rids; i++) {
742 d_printf("%8d: %s (%s)\n", rids[i], names[i],
743 sid_type_lookup(types[i]));
746 TALLOC_FREE(mem_ctx);
750 /* Convert string to sid */
752 static BOOL wbinfo_lookupname(char *name)
754 struct winbindd_request request;
755 struct winbindd_response response;
757 /* Send off request */
759 ZERO_STRUCT(request);
760 ZERO_STRUCT(response);
762 parse_wbinfo_domain_user(name, request.data.name.dom_name,
763 request.data.name.name);
765 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
769 /* Display response */
771 d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
776 /* Authenticate a user with a plaintext password */
778 static BOOL wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
780 struct winbindd_request request;
781 struct winbindd_response response;
785 /* Send off request */
787 ZERO_STRUCT(request);
788 ZERO_STRUCT(response);
790 p = strchr(username, '%');
794 fstrcpy(request.data.auth.user, username);
795 fstrcpy(request.data.auth.pass, p + 1);
798 fstrcpy(request.data.auth.user, username);
800 request.flags = flags;
802 fstrcpy(request.data.auth.krb5_cc_type, cctype);
804 request.data.auth.uid = geteuid();
806 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
808 /* Display response */
810 d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n",
811 username, (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", cctype);
813 if (response.data.auth.nt_status)
814 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
815 response.data.auth.nt_status_string,
816 response.data.auth.nt_status,
817 response.data.auth.error_string);
819 if (result == NSS_STATUS_SUCCESS) {
821 if (request.flags & WBFLAG_PAM_INFO3_TEXT) {
822 if (response.data.auth.info3.user_flgs & LOGON_CACHED_ACCOUNT) {
823 d_printf("user_flgs: LOGON_CACHED_ACCOUNT\n");
827 if (response.data.auth.krb5ccname[0] != '\0') {
828 d_printf("credentials were put in: %s\n", response.data.auth.krb5ccname);
830 d_printf("no credentials cached\n");
834 return result == NSS_STATUS_SUCCESS;
837 /* Authenticate a user with a plaintext password */
839 static BOOL wbinfo_auth(char *username)
841 struct winbindd_request request;
842 struct winbindd_response response;
846 /* Send off request */
848 ZERO_STRUCT(request);
849 ZERO_STRUCT(response);
851 p = strchr(username, '%');
855 fstrcpy(request.data.auth.user, username);
856 fstrcpy(request.data.auth.pass, p + 1);
859 fstrcpy(request.data.auth.user, username);
861 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
863 /* Display response */
865 d_printf("plaintext password authentication %s\n",
866 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
868 if (response.data.auth.nt_status)
869 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
870 response.data.auth.nt_status_string,
871 response.data.auth.nt_status,
872 response.data.auth.error_string);
874 return result == NSS_STATUS_SUCCESS;
877 /* Authenticate a user with a challenge/response */
879 static BOOL wbinfo_auth_crap(char *username)
881 struct winbindd_request request;
882 struct winbindd_response response;
889 /* Send off request */
891 ZERO_STRUCT(request);
892 ZERO_STRUCT(response);
894 p = strchr(username, '%');
898 fstrcpy(pass, p + 1);
901 parse_wbinfo_domain_user(username, name_domain, name_user);
903 request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
905 fstrcpy(request.data.auth_crap.user, name_user);
907 fstrcpy(request.data.auth_crap.domain,
910 generate_random_buffer(request.data.auth_crap.chal, 8);
912 if (lp_client_ntlmv2_auth()) {
913 DATA_BLOB server_chal;
914 DATA_BLOB names_blob;
916 DATA_BLOB lm_response;
917 DATA_BLOB nt_response;
919 server_chal = data_blob(request.data.auth_crap.chal, 8);
921 /* Pretend this is a login to 'us', for blob purposes */
922 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
924 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
926 &lm_response, &nt_response, NULL)) {
927 data_blob_free(&names_blob);
928 data_blob_free(&server_chal);
931 data_blob_free(&names_blob);
932 data_blob_free(&server_chal);
934 memcpy(request.data.auth_crap.nt_resp, nt_response.data,
935 MIN(nt_response.length,
936 sizeof(request.data.auth_crap.nt_resp)));
937 request.data.auth_crap.nt_resp_len = nt_response.length;
939 memcpy(request.data.auth_crap.lm_resp, lm_response.data,
940 MIN(lm_response.length,
941 sizeof(request.data.auth_crap.lm_resp)));
942 request.data.auth_crap.lm_resp_len = lm_response.length;
944 data_blob_free(&nt_response);
945 data_blob_free(&lm_response);
948 if (lp_client_lanman_auth()
949 && SMBencrypt(pass, request.data.auth_crap.chal,
950 (uchar *)request.data.auth_crap.lm_resp)) {
951 request.data.auth_crap.lm_resp_len = 24;
953 request.data.auth_crap.lm_resp_len = 0;
955 SMBNTencrypt(pass, request.data.auth_crap.chal,
956 (uchar *)request.data.auth_crap.nt_resp);
958 request.data.auth_crap.nt_resp_len = 24;
961 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
963 /* Display response */
965 d_printf("challenge/response password authentication %s\n",
966 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
968 if (response.data.auth.nt_status)
969 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
970 response.data.auth.nt_status_string,
971 response.data.auth.nt_status,
972 response.data.auth.error_string);
974 return result == NSS_STATUS_SUCCESS;
977 /* Authenticate a user with a plaintext password and set a token */
979 static BOOL wbinfo_klog(char *username)
981 struct winbindd_request request;
982 struct winbindd_response response;
986 /* Send off request */
988 ZERO_STRUCT(request);
989 ZERO_STRUCT(response);
991 p = strchr(username, '%');
995 fstrcpy(request.data.auth.user, username);
996 fstrcpy(request.data.auth.pass, p + 1);
999 fstrcpy(request.data.auth.user, username);
1000 fstrcpy(request.data.auth.pass, getpass("Password: "));
1003 request.flags |= WBFLAG_PAM_AFS_TOKEN;
1005 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
1007 /* Display response */
1009 d_printf("plaintext password authentication %s\n",
1010 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1012 if (response.data.auth.nt_status)
1013 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1014 response.data.auth.nt_status_string,
1015 response.data.auth.nt_status,
1016 response.data.auth.error_string);
1018 if (result != NSS_STATUS_SUCCESS)
1021 if (response.extra_data.data == NULL) {
1022 d_fprintf(stderr, "Did not get token data\n");
1026 if (!afs_settoken_str((char *)response.extra_data.data)) {
1027 d_fprintf(stderr, "Could not set token\n");
1031 d_printf("Successfully created AFS token\n");
1035 /* Print domain users */
1037 static BOOL print_domain_users(const char *domain)
1039 struct winbindd_request request;
1040 struct winbindd_response response;
1041 const char *extra_data;
1044 /* Send request to winbind daemon */
1046 ZERO_STRUCT(request);
1047 ZERO_STRUCT(response);
1050 /* '.' is the special sign for our own domain */
1051 if ( strequal(domain, ".") )
1052 fstrcpy( request.domain_name, get_winbind_domain() );
1054 fstrcpy( request.domain_name, domain );
1057 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
1061 /* Look through extra data */
1063 if (!response.extra_data.data)
1066 extra_data = (const char *)response.extra_data.data;
1068 while(next_token(&extra_data, name, ",", sizeof(fstring)))
1069 d_printf("%s\n", name);
1071 SAFE_FREE(response.extra_data.data);
1076 /* Print domain groups */
1078 static BOOL print_domain_groups(const char *domain)
1080 struct winbindd_request request;
1081 struct winbindd_response response;
1082 const char *extra_data;
1085 ZERO_STRUCT(request);
1086 ZERO_STRUCT(response);
1089 if ( strequal(domain, ".") )
1090 fstrcpy( request.domain_name, get_winbind_domain() );
1092 fstrcpy( request.domain_name, domain );
1095 if (winbindd_request_response(WINBINDD_LIST_GROUPS, &request, &response) !=
1099 /* Look through extra data */
1101 if (!response.extra_data.data)
1104 extra_data = (const char *)response.extra_data.data;
1106 while(next_token(&extra_data, name, ",", sizeof(fstring)))
1107 d_printf("%s\n", name);
1109 SAFE_FREE(response.extra_data.data);
1114 /* Set the authorised user for winbindd access in secrets.tdb */
1116 static BOOL wbinfo_set_auth_user(char *username)
1118 const char *password;
1120 fstring user, domain;
1122 /* Separate into user and password */
1124 parse_wbinfo_domain_user(username, domain, user);
1126 p = strchr(user, '%');
1132 char *thepass = getpass("Password: ");
1139 /* Store or remove DOMAIN\username%password in secrets.tdb */
1145 if (!secrets_store(SECRETS_AUTH_USER, user,
1146 strlen(user) + 1)) {
1147 d_fprintf(stderr, "error storing username\n");
1151 /* We always have a domain name added by the
1152 parse_wbinfo_domain_user() function. */
1154 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
1155 strlen(domain) + 1)) {
1156 d_fprintf(stderr, "error storing domain name\n");
1161 secrets_delete(SECRETS_AUTH_USER);
1162 secrets_delete(SECRETS_AUTH_DOMAIN);
1167 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
1168 strlen(password) + 1)) {
1169 d_fprintf(stderr, "error storing password\n");
1174 secrets_delete(SECRETS_AUTH_PASSWORD);
1179 static void wbinfo_get_auth_user(void)
1181 char *user, *domain, *password;
1183 /* Lift data from secrets file */
1185 secrets_fetch_ipc_userpass(&user, &domain, &password);
1187 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
1191 SAFE_FREE(password);
1192 d_printf("No authorised user configured\n");
1196 /* Pretty print authorised user info */
1198 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
1199 user, password ? "%" : "", password ? password : "");
1203 SAFE_FREE(password);
1206 static BOOL wbinfo_ping(void)
1210 result = winbindd_request_response(WINBINDD_PING, NULL, NULL);
1212 /* Display response */
1214 d_printf("Ping to winbindd %s on fd %d\n",
1215 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
1217 return result == NSS_STATUS_SUCCESS;
1223 OPT_SET_AUTH_USER = 1000,
1233 OPT_LIST_ALL_DOMAINS,
1234 OPT_LIST_OWN_DOMAIN,
1239 int main(int argc, char **argv, char **envp)
1244 static char *string_arg;
1245 static char *opt_domain_name;
1249 struct poptOption long_options[] = {
1252 /* longName, shortName, argInfo, argPtr, value, descrip,
1255 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1256 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1257 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1258 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1259 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1260 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1261 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1262 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1263 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1264 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1265 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1266 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1267 "Get a new UID out of idmap" },
1268 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1269 "Get a new GID out of idmap" },
1270 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1271 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1272 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1273 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1274 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1275 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1276 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1277 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1278 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1279 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1280 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1281 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1282 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1283 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1284 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1285 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1286 "Get a DC name for a foreign domain", "domainname" },
1287 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1288 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1289 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1290 #ifdef WITH_FAKE_KASERVER
1291 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1294 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1295 /* destroys wbinfo --help output */
1296 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1298 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1303 /* Samba client initialisation */
1306 if (!lp_load(dyn_CONFIGFILE, True, False, False, True)) {
1307 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1308 dyn_CONFIGFILE, strerror(errno));
1319 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1321 /* Parse command line options */
1324 poptPrintHelp(pc, stderr, 0);
1328 while((opt = poptGetNextOpt(pc)) != -1) {
1329 /* get the generic configuration parameters like --domain */
1332 poptFreeContext(pc);
1334 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1335 POPT_CONTEXT_KEEP_FIRST);
1337 while((opt = poptGetNextOpt(pc)) != -1) {
1340 if (!print_domain_users(opt_domain_name)) {
1341 d_fprintf(stderr, "Error looking up domain users\n");
1346 if (!print_domain_groups(opt_domain_name)) {
1347 d_fprintf(stderr, "Error looking up domain groups\n");
1352 if (!wbinfo_lookupsid(string_arg)) {
1353 d_fprintf(stderr, "Could not lookup sid %s\n", string_arg);
1358 if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
1359 d_fprintf(stderr, "Could not lookup RIDs %s\n", string_arg);
1364 if (!wbinfo_lookupname(string_arg)) {
1365 d_fprintf(stderr, "Could not lookup name %s\n", string_arg);
1370 if (!wbinfo_wins_byname(string_arg)) {
1371 d_fprintf(stderr, "Could not lookup WINS by name %s\n", string_arg);
1376 if (!wbinfo_wins_byip(string_arg)) {
1377 d_fprintf(stderr, "Could not lookup WINS by IP %s\n", string_arg);
1382 if (!wbinfo_uid_to_sid(int_arg)) {
1383 d_fprintf(stderr, "Could not convert uid %d to sid\n", int_arg);
1388 if (!wbinfo_gid_to_sid(int_arg)) {
1389 d_fprintf(stderr, "Could not convert gid %d to sid\n",
1395 if (!wbinfo_sid_to_uid(string_arg)) {
1396 d_fprintf(stderr, "Could not convert sid %s to uid\n",
1402 if (!wbinfo_sid_to_gid(string_arg)) {
1403 d_fprintf(stderr, "Could not convert sid %s to gid\n",
1408 case OPT_ALLOCATE_UID:
1409 if (!wbinfo_allocate_uid()) {
1410 d_fprintf(stderr, "Could not allocate a uid\n");
1414 case OPT_ALLOCATE_GID:
1415 if (!wbinfo_allocate_gid()) {
1416 d_fprintf(stderr, "Could not allocate a gid\n");
1421 if (!wbinfo_check_secret()) {
1422 d_fprintf(stderr, "Could not check secret\n");
1427 if (!wbinfo_list_domains(False)) {
1428 d_fprintf(stderr, "Could not list trusted domains\n");
1433 if (!wbinfo_show_sequence(opt_domain_name)) {
1434 d_fprintf(stderr, "Could not show sequence numbers\n");
1439 if (!wbinfo_domain_info(string_arg)) {
1440 d_fprintf(stderr, "Could not get domain info\n");
1445 if (!wbinfo_get_userinfo(string_arg)) {
1446 d_fprintf(stderr, "Could not get info for user %s\n",
1452 if ( !wbinfo_get_uidinfo(int_arg)) {
1453 d_fprintf(stderr, "Could not get info for uid "
1458 case OPT_GROUP_INFO:
1459 if ( !wbinfo_get_groupinfo(string_arg)) {
1460 d_fprintf(stderr, "Could not get info for "
1461 "group %s\n", string_arg);
1466 if (!wbinfo_get_usergroups(string_arg)) {
1467 d_fprintf(stderr, "Could not get groups for user %s\n",
1473 if (!wbinfo_get_usersids(string_arg)) {
1474 d_fprintf(stderr, "Could not get group SIDs for user SID %s\n",
1479 case OPT_USERDOMGROUPS:
1480 if (!wbinfo_get_userdomgroups(string_arg)) {
1481 d_fprintf(stderr, "Could not get user's domain groups "
1482 "for user SID %s\n", string_arg);
1487 BOOL got_error = False;
1489 if (!wbinfo_auth(string_arg)) {
1490 d_fprintf(stderr, "Could not authenticate user %s with "
1491 "plaintext password\n", string_arg);
1495 if (!wbinfo_auth_crap(string_arg)) {
1496 d_fprintf(stderr, "Could not authenticate user %s with "
1497 "challenge/response\n", string_arg);
1506 uint32 flags = WBFLAG_PAM_KRB5 |
1507 WBFLAG_PAM_CACHED_LOGIN |
1508 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
1509 WBFLAG_PAM_INFO3_TEXT;
1511 if (!wbinfo_auth_krb5(string_arg, "FILE", flags)) {
1512 d_fprintf(stderr, "Could not authenticate user [%s] with "
1513 "Kerberos (ccache: %s)\n", string_arg, "FILE");
1519 if (!wbinfo_klog(string_arg)) {
1520 d_fprintf(stderr, "Could not klog user\n");
1525 if (!wbinfo_ping()) {
1526 d_fprintf(stderr, "could not ping winbindd!\n");
1530 case OPT_SET_AUTH_USER:
1531 if (!wbinfo_set_auth_user(string_arg)) {
1535 case OPT_GET_AUTH_USER:
1536 wbinfo_get_auth_user();
1539 if (!wbinfo_getdcname(string_arg)) {
1543 case OPT_SEPARATOR: {
1544 const char sep = winbind_separator_int(True);
1548 d_printf("%c\n", sep);
1551 case OPT_LIST_ALL_DOMAINS:
1552 if (!wbinfo_list_domains(True)) {
1556 case OPT_LIST_OWN_DOMAIN:
1557 if (!wbinfo_list_own_domain()) {
1561 /* generic configuration options */
1562 case OPT_DOMAIN_NAME:
1565 d_fprintf(stderr, "Invalid option\n");
1566 poptPrintHelp(pc, stderr, 0);
1576 poptFreeContext(pc);