r15837: starting sync up for 3.0.23rc1 (in sync with SAMBA_3_0 r15822)
[Samba.git] / source / nsswitch / wbinfo.c
blobc004b842f4f97aee1d0ee2b7ceb808401b0f4325
1 /*
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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
25 #include "winbindd.h"
26 #include "debug.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_WINBIND
31 extern int winbindd_fd;
33 static char winbind_separator_int(BOOL strict)
35 struct winbindd_response response;
36 static BOOL got_sep;
37 static char sep;
39 if (got_sep)
40 return sep;
42 ZERO_STRUCT(response);
44 /* Send off request */
46 if (winbindd_request_response(WINBINDD_INFO, NULL, &response) !=
47 NSS_STATUS_SUCCESS) {
48 d_fprintf(stderr, "could not obtain winbind separator!\n");
49 if (strict) {
50 return 0;
52 /* HACK: (this module should not call lp_ funtions) */
53 return *lp_winbind_separator();
56 sep = response.data.info.winbind_separator;
57 got_sep = True;
59 if (!sep) {
60 d_fprintf(stderr, "winbind separator was NULL!\n");
61 if (strict) {
62 return 0;
64 /* HACK: (this module should not call lp_ funtions) */
65 sep = *lp_winbind_separator();
68 return sep;
71 static char winbind_separator(void)
73 return winbind_separator_int(False);
76 static const char *get_winbind_domain(void)
78 struct winbindd_response response;
79 static fstring winbind_domain;
81 ZERO_STRUCT(response);
83 /* Send off request */
85 if (winbindd_request_response(WINBINDD_DOMAIN_NAME, NULL, &response) !=
86 NSS_STATUS_SUCCESS) {
87 d_fprintf(stderr, "could not obtain winbind domain name!\n");
89 /* HACK: (this module should not call lp_ funtions) */
90 return lp_workgroup();
93 fstrcpy(winbind_domain, response.data.domain_name);
95 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,
103 fstring user)
106 char *p = strchr(domuser,winbind_separator());
108 if (!p) {
109 fstrcpy(user, domuser);
110 fstrcpy(domain, get_winbind_domain());
111 return True;
114 fstrcpy(user, p+1);
115 fstrcpy(domain, domuser);
116 domain[PTR_DIFF(p, domuser)] = 0;
117 strupper_m(domain);
119 return True;
122 /* pull pwent info for a given user */
124 static BOOL wbinfo_get_userinfo(char *user)
126 struct winbindd_request request;
127 struct winbindd_response response;
128 NSS_STATUS result;
130 ZERO_STRUCT(request);
131 ZERO_STRUCT(response);
133 /* Send request */
135 fstrcpy(request.data.username, user);
137 result = winbindd_request_response(WINBINDD_GETPWNAM, &request, &response);
139 if (result != NSS_STATUS_SUCCESS)
140 return False;
142 d_printf( "%s:%s:%d:%d:%s:%s:%s\n",
143 response.data.pw.pw_name,
144 response.data.pw.pw_passwd,
145 response.data.pw.pw_uid,
146 response.data.pw.pw_gid,
147 response.data.pw.pw_gecos,
148 response.data.pw.pw_dir,
149 response.data.pw.pw_shell );
151 return True;
154 /* List groups a user is a member of */
156 static BOOL wbinfo_get_usergroups(char *user)
158 struct winbindd_request request;
159 struct winbindd_response response;
160 NSS_STATUS result;
161 int i;
163 ZERO_STRUCT(request);
164 ZERO_STRUCT(response);
166 /* Send request */
168 fstrcpy(request.data.username, user);
170 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
172 if (result != NSS_STATUS_SUCCESS)
173 return False;
175 for (i = 0; i < response.data.num_entries; i++)
176 d_printf("%d\n", (int)((gid_t *)response.extra_data.data)[i]);
178 SAFE_FREE(response.extra_data.data);
180 return True;
184 /* List group SIDs a user SID is a member of */
185 static BOOL wbinfo_get_usersids(char *user_sid)
187 struct winbindd_request request;
188 struct winbindd_response response;
189 NSS_STATUS result;
190 int i;
191 const char *s;
193 ZERO_STRUCT(request);
194 ZERO_STRUCT(response);
196 /* Send request */
197 fstrcpy(request.data.sid, user_sid);
199 result = winbindd_request_response(WINBINDD_GETUSERSIDS, &request, &response);
201 if (result != NSS_STATUS_SUCCESS)
202 return False;
204 s = response.extra_data.data;
205 for (i = 0; i < response.data.num_entries; i++) {
206 d_printf("%s\n", s);
207 s += strlen(s) + 1;
210 SAFE_FREE(response.extra_data.data);
212 return True;
215 static BOOL wbinfo_get_userdomgroups(const char *user_sid)
217 struct winbindd_request request;
218 struct winbindd_response response;
219 NSS_STATUS result;
221 ZERO_STRUCT(request);
222 ZERO_STRUCT(response);
224 /* Send request */
225 fstrcpy(request.data.sid, user_sid);
227 result = winbindd_request_response(WINBINDD_GETUSERDOMGROUPS, &request,
228 &response);
230 if (result != NSS_STATUS_SUCCESS)
231 return False;
233 if (response.data.num_entries != 0)
234 printf("%s", (char *)response.extra_data.data);
236 SAFE_FREE(response.extra_data.data);
238 return True;
241 /* Convert NetBIOS name to IP */
243 static BOOL wbinfo_wins_byname(char *name)
245 struct winbindd_request request;
246 struct winbindd_response response;
248 ZERO_STRUCT(request);
249 ZERO_STRUCT(response);
251 /* Send request */
253 fstrcpy(request.data.winsreq, name);
255 if (winbindd_request_response(WINBINDD_WINS_BYNAME, &request, &response) !=
256 NSS_STATUS_SUCCESS) {
257 return False;
260 /* Display response */
262 d_printf("%s\n", response.data.winsresp);
264 return True;
267 /* Convert IP to NetBIOS name */
269 static BOOL wbinfo_wins_byip(char *ip)
271 struct winbindd_request request;
272 struct winbindd_response response;
274 ZERO_STRUCT(request);
275 ZERO_STRUCT(response);
277 /* Send request */
279 fstrcpy(request.data.winsreq, ip);
281 if (winbindd_request_response(WINBINDD_WINS_BYIP, &request, &response) !=
282 NSS_STATUS_SUCCESS) {
283 return False;
286 /* Display response */
288 d_printf("%s\n", response.data.winsresp);
290 return True;
293 /* List trusted domains */
295 static BOOL wbinfo_list_domains(BOOL list_all_domains)
297 struct winbindd_request request;
298 struct winbindd_response response;
300 ZERO_STRUCT(request);
301 ZERO_STRUCT(response);
303 /* Send request */
305 request.data.list_all_domains = list_all_domains;
307 if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM, &request, &response) !=
308 NSS_STATUS_SUCCESS)
309 return False;
311 /* Display response */
313 if (response.extra_data.data) {
314 const char *extra_data = (char *)response.extra_data.data;
315 fstring name;
316 char *p;
318 while(next_token(&extra_data, name, "\n", sizeof(fstring))) {
319 p = strchr(name, '\\');
320 if (p == 0) {
321 d_fprintf(stderr, "Got invalid response: %s\n",
322 extra_data);
323 return False;
325 *p = 0;
326 d_printf("%s\n", name);
329 SAFE_FREE(response.extra_data.data);
332 return True;
336 /* show sequence numbers */
337 static BOOL wbinfo_show_sequence(const char *domain)
339 struct winbindd_request request;
340 struct winbindd_response response;
342 ZERO_STRUCT(response);
343 ZERO_STRUCT(request);
345 if ( domain )
346 fstrcpy( request.domain_name, domain );
348 /* Send request */
350 if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
351 NSS_STATUS_SUCCESS)
352 return False;
354 /* Display response */
356 if (response.extra_data.data) {
357 char *extra_data = (char *)response.extra_data.data;
358 d_printf("%s", extra_data);
359 SAFE_FREE(response.extra_data.data);
362 return True;
365 /* Show domain info */
367 static BOOL wbinfo_domain_info(const char *domain_name)
369 struct winbindd_request request;
370 struct winbindd_response response;
372 ZERO_STRUCT(request);
373 ZERO_STRUCT(response);
375 fstrcpy(request.domain_name, domain_name);
377 /* Send request */
379 if (winbindd_request_response(WINBINDD_DOMAIN_INFO, &request, &response) !=
380 NSS_STATUS_SUCCESS)
381 return False;
383 /* Display response */
385 d_printf("Name : %s\n", response.data.domain_info.name);
386 d_printf("Alt_Name : %s\n", response.data.domain_info.alt_name);
388 d_printf("SID : %s\n", response.data.domain_info.sid);
390 d_printf("Active Directory : %s\n",
391 response.data.domain_info.active_directory ? "Yes" : "No");
392 d_printf("Native : %s\n",
393 response.data.domain_info.native_mode ? "Yes" : "No");
395 d_printf("Primary : %s\n",
396 response.data.domain_info.primary ? "Yes" : "No");
398 d_printf("Sequence : %d\n", response.data.domain_info.sequence_number);
400 return True;
403 /* Get a foreign DC's name */
404 static BOOL wbinfo_getdcname(const char *domain_name)
406 struct winbindd_request request;
407 struct winbindd_response response;
409 ZERO_STRUCT(request);
410 ZERO_STRUCT(response);
412 fstrcpy(request.domain_name, domain_name);
414 /* Send request */
416 if (winbindd_request_response(WINBINDD_GETDCNAME, &request, &response) !=
417 NSS_STATUS_SUCCESS) {
418 d_fprintf(stderr, "Could not get dc name for %s\n", domain_name);
419 return False;
422 /* Display response */
424 d_printf("%s\n", response.data.dc_name);
426 return True;
429 /* Check trust account password */
431 static BOOL wbinfo_check_secret(void)
433 struct winbindd_response response;
434 NSS_STATUS result;
436 ZERO_STRUCT(response);
438 result = winbindd_request_response(WINBINDD_CHECK_MACHACC, NULL, &response);
440 d_printf("checking the trust secret via RPC calls %s\n",
441 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
443 if (result != NSS_STATUS_SUCCESS)
444 d_fprintf(stderr, "error code was %s (0x%x)\n",
445 response.data.auth.nt_status_string,
446 response.data.auth.nt_status);
448 return result == NSS_STATUS_SUCCESS;
451 /* Convert uid to sid */
453 static BOOL wbinfo_uid_to_sid(uid_t uid)
455 struct winbindd_request request;
456 struct winbindd_response response;
458 ZERO_STRUCT(request);
459 ZERO_STRUCT(response);
461 /* Send request */
463 request.data.uid = uid;
465 if (winbindd_request_response(WINBINDD_UID_TO_SID, &request, &response) !=
466 NSS_STATUS_SUCCESS)
467 return False;
469 /* Display response */
471 d_printf("%s\n", response.data.sid.sid);
473 return True;
476 /* Convert gid to sid */
478 static BOOL wbinfo_gid_to_sid(gid_t gid)
480 struct winbindd_request request;
481 struct winbindd_response response;
483 ZERO_STRUCT(request);
484 ZERO_STRUCT(response);
486 /* Send request */
488 request.data.gid = gid;
490 if (winbindd_request_response(WINBINDD_GID_TO_SID, &request, &response) !=
491 NSS_STATUS_SUCCESS)
492 return False;
494 /* Display response */
496 d_printf("%s\n", response.data.sid.sid);
498 return True;
501 /* Convert sid to uid */
503 static BOOL wbinfo_sid_to_uid(char *sid)
505 struct winbindd_request request;
506 struct winbindd_response response;
508 ZERO_STRUCT(request);
509 ZERO_STRUCT(response);
511 /* Send request */
513 fstrcpy(request.data.sid, sid);
515 if (winbindd_request_response(WINBINDD_SID_TO_UID, &request, &response) !=
516 NSS_STATUS_SUCCESS)
517 return False;
519 /* Display response */
521 d_printf("%d\n", (int)response.data.uid);
523 return True;
526 static BOOL wbinfo_sid_to_gid(char *sid)
528 struct winbindd_request request;
529 struct winbindd_response response;
531 ZERO_STRUCT(request);
532 ZERO_STRUCT(response);
534 /* Send request */
536 fstrcpy(request.data.sid, sid);
538 if (winbindd_request_response(WINBINDD_SID_TO_GID, &request, &response) !=
539 NSS_STATUS_SUCCESS)
540 return False;
542 /* Display response */
544 d_printf("%d\n", (int)response.data.gid);
546 return True;
549 static BOOL wbinfo_allocate_uid(void)
551 uid_t uid;
553 if (!winbind_allocate_uid(&uid))
554 return False;
556 d_printf("New uid: %d\n", uid);
558 return True;
561 static BOOL wbinfo_allocate_gid(void)
563 gid_t gid;
565 if (!winbind_allocate_gid(&gid))
566 return False;
568 d_printf("New gid: %d\n", gid);
570 return True;
573 /* Convert sid to string */
575 static BOOL wbinfo_lookupsid(char *sid)
577 struct winbindd_request request;
578 struct winbindd_response response;
580 ZERO_STRUCT(request);
581 ZERO_STRUCT(response);
583 /* Send off request */
585 fstrcpy(request.data.sid, sid);
587 if (winbindd_request_response(WINBINDD_LOOKUPSID, &request, &response) !=
588 NSS_STATUS_SUCCESS)
589 return False;
591 /* Display response */
593 d_printf("%s%c%s %d\n", response.data.name.dom_name,
594 winbind_separator(), response.data.name.name,
595 response.data.name.type);
597 return True;
600 /* Convert string to sid */
602 static BOOL wbinfo_lookupname(char *name)
604 struct winbindd_request request;
605 struct winbindd_response response;
607 /* Send off request */
609 ZERO_STRUCT(request);
610 ZERO_STRUCT(response);
612 parse_wbinfo_domain_user(name, request.data.name.dom_name,
613 request.data.name.name);
615 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
616 NSS_STATUS_SUCCESS)
617 return False;
619 /* Display response */
621 d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
623 return True;
626 /* Authenticate a user with a plaintext password */
628 static BOOL wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
630 struct winbindd_request request;
631 struct winbindd_response response;
632 NSS_STATUS result;
633 char *p;
635 /* Send off request */
637 ZERO_STRUCT(request);
638 ZERO_STRUCT(response);
640 p = strchr(username, '%');
642 if (p) {
643 *p = 0;
644 fstrcpy(request.data.auth.user, username);
645 fstrcpy(request.data.auth.pass, p + 1);
646 *p = '%';
647 } else
648 fstrcpy(request.data.auth.user, username);
650 request.flags = flags;
652 fstrcpy(request.data.auth.krb5_cc_type, cctype);
654 request.data.auth.uid = geteuid();
656 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
658 /* Display response */
660 d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n",
661 username, (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", cctype);
663 if (response.data.auth.nt_status)
664 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
665 response.data.auth.nt_status_string,
666 response.data.auth.nt_status,
667 response.data.auth.error_string);
669 if (result == NSS_STATUS_SUCCESS) {
671 if (request.flags & WBFLAG_PAM_INFO3_TEXT) {
672 if (response.data.auth.info3.user_flgs & LOGON_CACHED_ACCOUNT) {
673 d_printf("user_flgs: LOGON_CACHED_ACCOUNT\n");
677 if (response.data.auth.krb5ccname[0] != '\0') {
678 d_printf("credentials were put in: %s\n", response.data.auth.krb5ccname);
679 } else {
680 d_printf("no credentials cached\n");
684 return result == NSS_STATUS_SUCCESS;
687 /* Authenticate a user with a plaintext password */
689 static BOOL wbinfo_auth(char *username)
691 struct winbindd_request request;
692 struct winbindd_response response;
693 NSS_STATUS result;
694 char *p;
696 /* Send off request */
698 ZERO_STRUCT(request);
699 ZERO_STRUCT(response);
701 p = strchr(username, '%');
703 if (p) {
704 *p = 0;
705 fstrcpy(request.data.auth.user, username);
706 fstrcpy(request.data.auth.pass, p + 1);
707 *p = '%';
708 } else
709 fstrcpy(request.data.auth.user, username);
711 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
713 /* Display response */
715 d_printf("plaintext password authentication %s\n",
716 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
718 if (response.data.auth.nt_status)
719 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
720 response.data.auth.nt_status_string,
721 response.data.auth.nt_status,
722 response.data.auth.error_string);
724 return result == NSS_STATUS_SUCCESS;
727 /* Authenticate a user with a challenge/response */
729 static BOOL wbinfo_auth_crap(char *username)
731 struct winbindd_request request;
732 struct winbindd_response response;
733 NSS_STATUS result;
734 fstring name_user;
735 fstring name_domain;
736 fstring pass;
737 char *p;
739 /* Send off request */
741 ZERO_STRUCT(request);
742 ZERO_STRUCT(response);
744 p = strchr(username, '%');
746 if (p) {
747 *p = 0;
748 fstrcpy(pass, p + 1);
751 parse_wbinfo_domain_user(username, name_domain, name_user);
753 request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
755 fstrcpy(request.data.auth_crap.user, name_user);
757 fstrcpy(request.data.auth_crap.domain,
758 name_domain);
760 generate_random_buffer(request.data.auth_crap.chal, 8);
762 if (lp_client_ntlmv2_auth()) {
763 DATA_BLOB server_chal;
764 DATA_BLOB names_blob;
766 DATA_BLOB lm_response;
767 DATA_BLOB nt_response;
769 server_chal = data_blob(request.data.auth_crap.chal, 8);
771 /* Pretend this is a login to 'us', for blob purposes */
772 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
774 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
775 &names_blob,
776 &lm_response, &nt_response, NULL)) {
777 data_blob_free(&names_blob);
778 data_blob_free(&server_chal);
779 return False;
781 data_blob_free(&names_blob);
782 data_blob_free(&server_chal);
784 memcpy(request.data.auth_crap.nt_resp, nt_response.data,
785 MIN(nt_response.length,
786 sizeof(request.data.auth_crap.nt_resp)));
787 request.data.auth_crap.nt_resp_len = nt_response.length;
789 memcpy(request.data.auth_crap.lm_resp, lm_response.data,
790 MIN(lm_response.length,
791 sizeof(request.data.auth_crap.lm_resp)));
792 request.data.auth_crap.lm_resp_len = lm_response.length;
794 data_blob_free(&nt_response);
795 data_blob_free(&lm_response);
797 } else {
798 if (lp_client_lanman_auth()
799 && SMBencrypt(pass, request.data.auth_crap.chal,
800 (uchar *)request.data.auth_crap.lm_resp)) {
801 request.data.auth_crap.lm_resp_len = 24;
802 } else {
803 request.data.auth_crap.lm_resp_len = 0;
805 SMBNTencrypt(pass, request.data.auth_crap.chal,
806 (uchar *)request.data.auth_crap.nt_resp);
808 request.data.auth_crap.nt_resp_len = 24;
811 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
813 /* Display response */
815 d_printf("challenge/response password authentication %s\n",
816 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
818 if (response.data.auth.nt_status)
819 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
820 response.data.auth.nt_status_string,
821 response.data.auth.nt_status,
822 response.data.auth.error_string);
824 return result == NSS_STATUS_SUCCESS;
827 /* Authenticate a user with a plaintext password and set a token */
829 static BOOL wbinfo_klog(char *username)
831 struct winbindd_request request;
832 struct winbindd_response response;
833 NSS_STATUS result;
834 char *p;
836 /* Send off request */
838 ZERO_STRUCT(request);
839 ZERO_STRUCT(response);
841 p = strchr(username, '%');
843 if (p) {
844 *p = 0;
845 fstrcpy(request.data.auth.user, username);
846 fstrcpy(request.data.auth.pass, p + 1);
847 *p = '%';
848 } else {
849 fstrcpy(request.data.auth.user, username);
850 fstrcpy(request.data.auth.pass, getpass("Password: "));
853 request.flags |= WBFLAG_PAM_AFS_TOKEN;
855 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
857 /* Display response */
859 d_printf("plaintext password authentication %s\n",
860 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
862 if (response.data.auth.nt_status)
863 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
864 response.data.auth.nt_status_string,
865 response.data.auth.nt_status,
866 response.data.auth.error_string);
868 if (result != NSS_STATUS_SUCCESS)
869 return False;
871 if (response.extra_data.data == NULL) {
872 d_fprintf(stderr, "Did not get token data\n");
873 return False;
876 if (!afs_settoken_str((char *)response.extra_data.data)) {
877 d_fprintf(stderr, "Could not set token\n");
878 return False;
881 d_printf("Successfully created AFS token\n");
882 return True;
885 /* Print domain users */
887 static BOOL print_domain_users(const char *domain)
889 struct winbindd_request request;
890 struct winbindd_response response;
891 const char *extra_data;
892 fstring name;
894 /* Send request to winbind daemon */
896 ZERO_STRUCT(request);
897 ZERO_STRUCT(response);
899 if (domain) {
900 /* '.' is the special sign for our own domwin */
901 if ( strequal(domain, ".") )
902 fstrcpy( request.domain_name, lp_workgroup() );
903 else
904 fstrcpy( request.domain_name, domain );
907 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
908 NSS_STATUS_SUCCESS)
909 return False;
911 /* Look through extra data */
913 if (!response.extra_data.data)
914 return False;
916 extra_data = (const char *)response.extra_data.data;
918 while(next_token(&extra_data, name, ",", sizeof(fstring)))
919 d_printf("%s\n", name);
921 SAFE_FREE(response.extra_data.data);
923 return True;
926 /* Print domain groups */
928 static BOOL print_domain_groups(const char *domain)
930 struct winbindd_request request;
931 struct winbindd_response response;
932 const char *extra_data;
933 fstring name;
935 ZERO_STRUCT(request);
936 ZERO_STRUCT(response);
938 if (domain) {
939 if ( strequal(domain, ".") )
940 fstrcpy( request.domain_name, lp_workgroup() );
941 else
942 fstrcpy( request.domain_name, domain );
945 if (winbindd_request_response(WINBINDD_LIST_GROUPS, &request, &response) !=
946 NSS_STATUS_SUCCESS)
947 return False;
949 /* Look through extra data */
951 if (!response.extra_data.data)
952 return False;
954 extra_data = (const char *)response.extra_data.data;
956 while(next_token(&extra_data, name, ",", sizeof(fstring)))
957 d_printf("%s\n", name);
959 SAFE_FREE(response.extra_data.data);
961 return True;
964 /* Set the authorised user for winbindd access in secrets.tdb */
966 static BOOL wbinfo_set_auth_user(char *username)
968 const char *password;
969 char *p;
970 fstring user, domain;
972 /* Separate into user and password */
974 parse_wbinfo_domain_user(username, domain, user);
976 p = strchr(user, '%');
978 if (p != NULL) {
979 *p = 0;
980 password = p+1;
981 } else {
982 char *thepass = getpass("Password: ");
983 if (thepass) {
984 password = thepass;
985 } else
986 password = "";
989 /* Store or remove DOMAIN\username%password in secrets.tdb */
991 secrets_init();
993 if (user[0]) {
995 if (!secrets_store(SECRETS_AUTH_USER, user,
996 strlen(user) + 1)) {
997 d_fprintf(stderr, "error storing username\n");
998 return False;
1001 /* We always have a domain name added by the
1002 parse_wbinfo_domain_user() function. */
1004 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
1005 strlen(domain) + 1)) {
1006 d_fprintf(stderr, "error storing domain name\n");
1007 return False;
1010 } else {
1011 secrets_delete(SECRETS_AUTH_USER);
1012 secrets_delete(SECRETS_AUTH_DOMAIN);
1015 if (password[0]) {
1017 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
1018 strlen(password) + 1)) {
1019 d_fprintf(stderr, "error storing password\n");
1020 return False;
1023 } else
1024 secrets_delete(SECRETS_AUTH_PASSWORD);
1026 return True;
1029 static void wbinfo_get_auth_user(void)
1031 char *user, *domain, *password;
1033 /* Lift data from secrets file */
1035 secrets_fetch_ipc_userpass(&user, &domain, &password);
1037 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
1039 SAFE_FREE(user);
1040 SAFE_FREE(domain);
1041 SAFE_FREE(password);
1042 d_printf("No authorised user configured\n");
1043 return;
1046 /* Pretty print authorised user info */
1048 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
1049 user, password ? "%" : "", password ? password : "");
1051 SAFE_FREE(user);
1052 SAFE_FREE(domain);
1053 SAFE_FREE(password);
1056 static BOOL wbinfo_ping(void)
1058 NSS_STATUS result;
1060 result = winbindd_request_response(WINBINDD_PING, NULL, NULL);
1062 /* Display response */
1064 d_printf("Ping to winbindd %s on fd %d\n",
1065 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
1067 return result == NSS_STATUS_SUCCESS;
1070 /* Main program */
1072 enum {
1073 OPT_SET_AUTH_USER = 1000,
1074 OPT_GET_AUTH_USER,
1075 OPT_DOMAIN_NAME,
1076 OPT_SEQUENCE,
1077 OPT_GETDCNAME,
1078 OPT_USERDOMGROUPS,
1079 OPT_USERSIDS,
1080 OPT_ALLOCATE_UID,
1081 OPT_ALLOCATE_GID,
1082 OPT_SEPARATOR,
1083 OPT_LIST_ALL_DOMAINS
1086 int main(int argc, char **argv)
1088 int opt;
1090 poptContext pc;
1091 static char *string_arg;
1092 static char *opt_domain_name;
1093 static int int_arg;
1094 int result = 1;
1096 struct poptOption long_options[] = {
1097 POPT_AUTOHELP
1099 /* longName, shortName, argInfo, argPtr, value, descrip,
1100 argDesc */
1102 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1103 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1104 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1105 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1106 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1107 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1108 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1109 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1110 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1111 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1112 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1113 "Get a new UID out of idmap" },
1114 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1115 "Get a new GID out of idmap" },
1116 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1117 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1118 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1119 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1120 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1121 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1122 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1123 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1124 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1125 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1126 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1127 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1128 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1129 "Get a DC name for a foreign domain", "domainname" },
1130 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1131 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1132 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1133 #ifdef WITH_FAKE_KASERVER
1134 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1135 #endif
1136 #ifdef HAVE_KRB5
1137 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1138 /* destroys wbinfo --help output */
1139 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1140 #endif
1141 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1142 POPT_COMMON_VERSION
1143 POPT_TABLEEND
1146 /* Samba client initialisation */
1147 load_case_tables();
1149 if (!lp_load(dyn_CONFIGFILE, True, False, False, True)) {
1150 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1151 dyn_CONFIGFILE, strerror(errno));
1152 exit(1);
1155 if (!init_names())
1156 return 1;
1158 load_interfaces();
1160 /* Parse options */
1162 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1164 /* Parse command line options */
1166 if (argc == 1) {
1167 poptPrintHelp(pc, stderr, 0);
1168 return 1;
1171 while((opt = poptGetNextOpt(pc)) != -1) {
1172 /* get the generic configuration parameters like --domain */
1175 poptFreeContext(pc);
1177 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1178 POPT_CONTEXT_KEEP_FIRST);
1180 while((opt = poptGetNextOpt(pc)) != -1) {
1181 switch (opt) {
1182 case 'u':
1183 if (!print_domain_users(opt_domain_name)) {
1184 d_fprintf(stderr, "Error looking up domain users\n");
1185 goto done;
1187 break;
1188 case 'g':
1189 if (!print_domain_groups(opt_domain_name)) {
1190 d_fprintf(stderr, "Error looking up domain groups\n");
1191 goto done;
1193 break;
1194 case 's':
1195 if (!wbinfo_lookupsid(string_arg)) {
1196 d_fprintf(stderr, "Could not lookup sid %s\n", string_arg);
1197 goto done;
1199 break;
1200 case 'n':
1201 if (!wbinfo_lookupname(string_arg)) {
1202 d_fprintf(stderr, "Could not lookup name %s\n", string_arg);
1203 goto done;
1205 break;
1206 case 'N':
1207 if (!wbinfo_wins_byname(string_arg)) {
1208 d_fprintf(stderr, "Could not lookup WINS by name %s\n", string_arg);
1209 goto done;
1211 break;
1212 case 'I':
1213 if (!wbinfo_wins_byip(string_arg)) {
1214 d_fprintf(stderr, "Could not lookup WINS by IP %s\n", string_arg);
1215 goto done;
1217 break;
1218 case 'U':
1219 if (!wbinfo_uid_to_sid(int_arg)) {
1220 d_fprintf(stderr, "Could not convert uid %d to sid\n", int_arg);
1221 goto done;
1223 break;
1224 case 'G':
1225 if (!wbinfo_gid_to_sid(int_arg)) {
1226 d_fprintf(stderr, "Could not convert gid %d to sid\n",
1227 int_arg);
1228 goto done;
1230 break;
1231 case 'S':
1232 if (!wbinfo_sid_to_uid(string_arg)) {
1233 d_fprintf(stderr, "Could not convert sid %s to uid\n",
1234 string_arg);
1235 goto done;
1237 break;
1238 case 'Y':
1239 if (!wbinfo_sid_to_gid(string_arg)) {
1240 d_fprintf(stderr, "Could not convert sid %s to gid\n",
1241 string_arg);
1242 goto done;
1244 break;
1245 case OPT_ALLOCATE_UID:
1246 if (!wbinfo_allocate_uid()) {
1247 d_fprintf(stderr, "Could not allocate a uid\n");
1248 goto done;
1250 break;
1251 case OPT_ALLOCATE_GID:
1252 if (!wbinfo_allocate_gid()) {
1253 d_fprintf(stderr, "Could not allocate a gid\n");
1254 goto done;
1256 break;
1257 case 't':
1258 if (!wbinfo_check_secret()) {
1259 d_fprintf(stderr, "Could not check secret\n");
1260 goto done;
1262 break;
1263 case 'm':
1264 if (!wbinfo_list_domains(False)) {
1265 d_fprintf(stderr, "Could not list trusted domains\n");
1266 goto done;
1268 break;
1269 case OPT_SEQUENCE:
1270 if (!wbinfo_show_sequence(opt_domain_name)) {
1271 d_fprintf(stderr, "Could not show sequence numbers\n");
1272 goto done;
1274 break;
1275 case 'D':
1276 if (!wbinfo_domain_info(string_arg)) {
1277 d_fprintf(stderr, "Could not get domain info\n");
1278 goto done;
1280 break;
1281 case 'i':
1282 if (!wbinfo_get_userinfo(string_arg)) {
1283 d_fprintf(stderr, "Could not get info for user %s\n",
1284 string_arg);
1285 goto done;
1287 break;
1288 case 'r':
1289 if (!wbinfo_get_usergroups(string_arg)) {
1290 d_fprintf(stderr, "Could not get groups for user %s\n",
1291 string_arg);
1292 goto done;
1294 break;
1295 case OPT_USERSIDS:
1296 if (!wbinfo_get_usersids(string_arg)) {
1297 d_fprintf(stderr, "Could not get group SIDs for user SID %s\n",
1298 string_arg);
1299 goto done;
1301 break;
1302 case OPT_USERDOMGROUPS:
1303 if (!wbinfo_get_userdomgroups(string_arg)) {
1304 d_fprintf(stderr, "Could not get user's domain groups "
1305 "for user SID %s\n", string_arg);
1306 goto done;
1308 break;
1309 case 'a': {
1310 BOOL got_error = False;
1312 if (!wbinfo_auth(string_arg)) {
1313 d_fprintf(stderr, "Could not authenticate user %s with "
1314 "plaintext password\n", string_arg);
1315 got_error = True;
1318 if (!wbinfo_auth_crap(string_arg)) {
1319 d_fprintf(stderr, "Could not authenticate user %s with "
1320 "challenge/response\n", string_arg);
1321 got_error = True;
1324 if (got_error)
1325 goto done;
1326 break;
1328 case 'K': {
1329 BOOL got_error = False;
1330 uint32 flags = WBFLAG_PAM_KRB5 |
1331 WBFLAG_PAM_CACHED_LOGIN |
1332 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
1333 WBFLAG_PAM_INFO3_TEXT;
1334 fstring tok;
1335 int i;
1336 const char *arg[] = { NULL, NULL };
1337 const char *cctypes[] = { "FILE",
1338 "KCM",
1339 "KCM:0",
1340 "Garbage",
1341 NULL,
1342 "0"};
1344 arg[0] = string_arg;
1346 while (next_token(arg, tok, LIST_SEP, sizeof(tok))) {
1348 for (i=0; i < ARRAY_SIZE(cctypes); i++) {
1349 if (!wbinfo_auth_krb5(tok, cctypes[i], flags)) {
1350 d_fprintf(stderr, "Could not authenticate user [%s] with "
1351 "Kerberos (ccache: %s)\n", tok, cctypes[i]);
1352 got_error = True;
1357 if (got_error)
1358 goto done;
1360 break;
1362 case 'k':
1363 if (!wbinfo_klog(string_arg)) {
1364 d_fprintf(stderr, "Could not klog user\n");
1365 goto done;
1367 break;
1368 case 'p':
1369 if (!wbinfo_ping()) {
1370 d_fprintf(stderr, "could not ping winbindd!\n");
1371 goto done;
1373 break;
1374 case OPT_SET_AUTH_USER:
1375 if (!wbinfo_set_auth_user(string_arg)) {
1376 goto done;
1378 break;
1379 case OPT_GET_AUTH_USER:
1380 wbinfo_get_auth_user();
1381 break;
1382 case OPT_GETDCNAME:
1383 if (!wbinfo_getdcname(string_arg)) {
1384 goto done;
1386 break;
1387 case OPT_SEPARATOR: {
1388 const char sep = winbind_separator_int(True);
1389 if ( !sep ) {
1390 goto done;
1392 d_printf("%c\n", sep);
1393 break;
1395 case OPT_LIST_ALL_DOMAINS:
1396 if (!wbinfo_list_domains(True)) {
1397 goto done;
1399 /* generic configuration options */
1400 case OPT_DOMAIN_NAME:
1401 break;
1402 default:
1403 d_fprintf(stderr, "Invalid option\n");
1404 poptPrintHelp(pc, stderr, 0);
1405 goto done;
1409 result = 0;
1411 /* Exit code */
1413 done:
1414 poptFreeContext(pc);
1415 return result;