Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / source / nsswitch / wbinfo.c
blob69d7a1069f9c8bda91a6ae716e4c70b13381f527
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;
335 /* List own domain */
337 static BOOL wbinfo_list_own_domain(void)
339 d_printf("%s\n", get_winbind_domain());
341 return True;
344 /* show sequence numbers */
345 static BOOL wbinfo_show_sequence(const char *domain)
347 struct winbindd_request request;
348 struct winbindd_response response;
350 ZERO_STRUCT(response);
351 ZERO_STRUCT(request);
353 if ( domain )
354 fstrcpy( request.domain_name, domain );
356 /* Send request */
358 if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
359 NSS_STATUS_SUCCESS)
360 return False;
362 /* Display response */
364 if (response.extra_data.data) {
365 char *extra_data = (char *)response.extra_data.data;
366 d_printf("%s", extra_data);
367 SAFE_FREE(response.extra_data.data);
370 return True;
373 /* Show domain info */
375 static BOOL wbinfo_domain_info(const char *domain_name)
377 struct winbindd_request request;
378 struct winbindd_response response;
380 ZERO_STRUCT(request);
381 ZERO_STRUCT(response);
383 fstrcpy(request.domain_name, domain_name);
385 /* Send request */
387 if (winbindd_request_response(WINBINDD_DOMAIN_INFO, &request, &response) !=
388 NSS_STATUS_SUCCESS)
389 return False;
391 /* Display response */
393 d_printf("Name : %s\n", response.data.domain_info.name);
394 d_printf("Alt_Name : %s\n", response.data.domain_info.alt_name);
396 d_printf("SID : %s\n", response.data.domain_info.sid);
398 d_printf("Active Directory : %s\n",
399 response.data.domain_info.active_directory ? "Yes" : "No");
400 d_printf("Native : %s\n",
401 response.data.domain_info.native_mode ? "Yes" : "No");
403 d_printf("Primary : %s\n",
404 response.data.domain_info.primary ? "Yes" : "No");
406 d_printf("Sequence : %d\n", response.data.domain_info.sequence_number);
408 return True;
411 /* Get a foreign DC's name */
412 static BOOL wbinfo_getdcname(const char *domain_name)
414 struct winbindd_request request;
415 struct winbindd_response response;
417 ZERO_STRUCT(request);
418 ZERO_STRUCT(response);
420 fstrcpy(request.domain_name, domain_name);
422 /* Send request */
424 if (winbindd_request_response(WINBINDD_GETDCNAME, &request, &response) !=
425 NSS_STATUS_SUCCESS) {
426 d_fprintf(stderr, "Could not get dc name for %s\n", domain_name);
427 return False;
430 /* Display response */
432 d_printf("%s\n", response.data.dc_name);
434 return True;
437 /* Check trust account password */
439 static BOOL wbinfo_check_secret(void)
441 struct winbindd_response response;
442 NSS_STATUS result;
444 ZERO_STRUCT(response);
446 result = winbindd_request_response(WINBINDD_CHECK_MACHACC, NULL, &response);
448 d_printf("checking the trust secret via RPC calls %s\n",
449 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
451 if (result != NSS_STATUS_SUCCESS)
452 d_fprintf(stderr, "error code was %s (0x%x)\n",
453 response.data.auth.nt_status_string,
454 response.data.auth.nt_status);
456 return result == NSS_STATUS_SUCCESS;
459 /* Convert uid to sid */
461 static BOOL wbinfo_uid_to_sid(uid_t uid)
463 struct winbindd_request request;
464 struct winbindd_response response;
466 ZERO_STRUCT(request);
467 ZERO_STRUCT(response);
469 /* Send request */
471 request.data.uid = uid;
473 if (winbindd_request_response(WINBINDD_UID_TO_SID, &request, &response) !=
474 NSS_STATUS_SUCCESS)
475 return False;
477 /* Display response */
479 d_printf("%s\n", response.data.sid.sid);
481 return True;
484 /* Convert gid to sid */
486 static BOOL wbinfo_gid_to_sid(gid_t gid)
488 struct winbindd_request request;
489 struct winbindd_response response;
491 ZERO_STRUCT(request);
492 ZERO_STRUCT(response);
494 /* Send request */
496 request.data.gid = gid;
498 if (winbindd_request_response(WINBINDD_GID_TO_SID, &request, &response) !=
499 NSS_STATUS_SUCCESS)
500 return False;
502 /* Display response */
504 d_printf("%s\n", response.data.sid.sid);
506 return True;
509 /* Convert sid to uid */
511 static BOOL wbinfo_sid_to_uid(char *sid)
513 struct winbindd_request request;
514 struct winbindd_response response;
516 ZERO_STRUCT(request);
517 ZERO_STRUCT(response);
519 /* Send request */
521 fstrcpy(request.data.sid, sid);
523 if (winbindd_request_response(WINBINDD_SID_TO_UID, &request, &response) !=
524 NSS_STATUS_SUCCESS)
525 return False;
527 /* Display response */
529 d_printf("%d\n", (int)response.data.uid);
531 return True;
534 static BOOL wbinfo_sid_to_gid(char *sid)
536 struct winbindd_request request;
537 struct winbindd_response response;
539 ZERO_STRUCT(request);
540 ZERO_STRUCT(response);
542 /* Send request */
544 fstrcpy(request.data.sid, sid);
546 if (winbindd_request_response(WINBINDD_SID_TO_GID, &request, &response) !=
547 NSS_STATUS_SUCCESS)
548 return False;
550 /* Display response */
552 d_printf("%d\n", (int)response.data.gid);
554 return True;
557 static BOOL wbinfo_allocate_uid(void)
559 uid_t uid;
561 if (!winbind_allocate_uid(&uid))
562 return False;
564 d_printf("New uid: %d\n", uid);
566 return True;
569 static BOOL wbinfo_allocate_gid(void)
571 gid_t gid;
573 if (!winbind_allocate_gid(&gid))
574 return False;
576 d_printf("New gid: %d\n", gid);
578 return True;
581 /* Convert sid to string */
583 static BOOL wbinfo_lookupsid(char *sid)
585 struct winbindd_request request;
586 struct winbindd_response response;
588 ZERO_STRUCT(request);
589 ZERO_STRUCT(response);
591 /* Send off request */
593 fstrcpy(request.data.sid, sid);
595 if (winbindd_request_response(WINBINDD_LOOKUPSID, &request, &response) !=
596 NSS_STATUS_SUCCESS)
597 return False;
599 /* Display response */
601 d_printf("%s%c%s %d\n", response.data.name.dom_name,
602 winbind_separator(), response.data.name.name,
603 response.data.name.type);
605 return True;
608 /* Convert string to sid */
610 static BOOL wbinfo_lookupname(char *name)
612 struct winbindd_request request;
613 struct winbindd_response response;
615 /* Send off request */
617 ZERO_STRUCT(request);
618 ZERO_STRUCT(response);
620 parse_wbinfo_domain_user(name, request.data.name.dom_name,
621 request.data.name.name);
623 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
624 NSS_STATUS_SUCCESS)
625 return False;
627 /* Display response */
629 d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
631 return True;
634 /* Authenticate a user with a plaintext password */
636 static BOOL wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
638 struct winbindd_request request;
639 struct winbindd_response response;
640 NSS_STATUS result;
641 char *p;
643 /* Send off request */
645 ZERO_STRUCT(request);
646 ZERO_STRUCT(response);
648 p = strchr(username, '%');
650 if (p) {
651 *p = 0;
652 fstrcpy(request.data.auth.user, username);
653 fstrcpy(request.data.auth.pass, p + 1);
654 *p = '%';
655 } else
656 fstrcpy(request.data.auth.user, username);
658 request.flags = flags;
660 fstrcpy(request.data.auth.krb5_cc_type, cctype);
662 request.data.auth.uid = geteuid();
664 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
666 /* Display response */
668 d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n",
669 username, (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", cctype);
671 if (response.data.auth.nt_status)
672 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
673 response.data.auth.nt_status_string,
674 response.data.auth.nt_status,
675 response.data.auth.error_string);
677 if (result == NSS_STATUS_SUCCESS) {
679 if (request.flags & WBFLAG_PAM_INFO3_TEXT) {
680 if (response.data.auth.info3.user_flgs & LOGON_CACHED_ACCOUNT) {
681 d_printf("user_flgs: LOGON_CACHED_ACCOUNT\n");
685 if (response.data.auth.krb5ccname[0] != '\0') {
686 d_printf("credentials were put in: %s\n", response.data.auth.krb5ccname);
687 } else {
688 d_printf("no credentials cached\n");
692 return result == NSS_STATUS_SUCCESS;
695 /* Authenticate a user with a plaintext password */
697 static BOOL wbinfo_auth(char *username)
699 struct winbindd_request request;
700 struct winbindd_response response;
701 NSS_STATUS result;
702 char *p;
704 /* Send off request */
706 ZERO_STRUCT(request);
707 ZERO_STRUCT(response);
709 p = strchr(username, '%');
711 if (p) {
712 *p = 0;
713 fstrcpy(request.data.auth.user, username);
714 fstrcpy(request.data.auth.pass, p + 1);
715 *p = '%';
716 } else
717 fstrcpy(request.data.auth.user, username);
719 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
721 /* Display response */
723 d_printf("plaintext password authentication %s\n",
724 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
726 if (response.data.auth.nt_status)
727 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
728 response.data.auth.nt_status_string,
729 response.data.auth.nt_status,
730 response.data.auth.error_string);
732 return result == NSS_STATUS_SUCCESS;
735 /* Authenticate a user with a challenge/response */
737 static BOOL wbinfo_auth_crap(char *username)
739 struct winbindd_request request;
740 struct winbindd_response response;
741 NSS_STATUS result;
742 fstring name_user;
743 fstring name_domain;
744 fstring pass;
745 char *p;
747 /* Send off request */
749 ZERO_STRUCT(request);
750 ZERO_STRUCT(response);
752 p = strchr(username, '%');
754 if (p) {
755 *p = 0;
756 fstrcpy(pass, p + 1);
759 parse_wbinfo_domain_user(username, name_domain, name_user);
761 request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
763 fstrcpy(request.data.auth_crap.user, name_user);
765 fstrcpy(request.data.auth_crap.domain,
766 name_domain);
768 generate_random_buffer(request.data.auth_crap.chal, 8);
770 if (lp_client_ntlmv2_auth()) {
771 DATA_BLOB server_chal;
772 DATA_BLOB names_blob;
774 DATA_BLOB lm_response;
775 DATA_BLOB nt_response;
777 server_chal = data_blob(request.data.auth_crap.chal, 8);
779 /* Pretend this is a login to 'us', for blob purposes */
780 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
782 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
783 &names_blob,
784 &lm_response, &nt_response, NULL)) {
785 data_blob_free(&names_blob);
786 data_blob_free(&server_chal);
787 return False;
789 data_blob_free(&names_blob);
790 data_blob_free(&server_chal);
792 memcpy(request.data.auth_crap.nt_resp, nt_response.data,
793 MIN(nt_response.length,
794 sizeof(request.data.auth_crap.nt_resp)));
795 request.data.auth_crap.nt_resp_len = nt_response.length;
797 memcpy(request.data.auth_crap.lm_resp, lm_response.data,
798 MIN(lm_response.length,
799 sizeof(request.data.auth_crap.lm_resp)));
800 request.data.auth_crap.lm_resp_len = lm_response.length;
802 data_blob_free(&nt_response);
803 data_blob_free(&lm_response);
805 } else {
806 if (lp_client_lanman_auth()
807 && SMBencrypt(pass, request.data.auth_crap.chal,
808 (uchar *)request.data.auth_crap.lm_resp)) {
809 request.data.auth_crap.lm_resp_len = 24;
810 } else {
811 request.data.auth_crap.lm_resp_len = 0;
813 SMBNTencrypt(pass, request.data.auth_crap.chal,
814 (uchar *)request.data.auth_crap.nt_resp);
816 request.data.auth_crap.nt_resp_len = 24;
819 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
821 /* Display response */
823 d_printf("challenge/response password authentication %s\n",
824 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
826 if (response.data.auth.nt_status)
827 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
828 response.data.auth.nt_status_string,
829 response.data.auth.nt_status,
830 response.data.auth.error_string);
832 return result == NSS_STATUS_SUCCESS;
835 /* Authenticate a user with a plaintext password and set a token */
837 static BOOL wbinfo_klog(char *username)
839 struct winbindd_request request;
840 struct winbindd_response response;
841 NSS_STATUS result;
842 char *p;
844 /* Send off request */
846 ZERO_STRUCT(request);
847 ZERO_STRUCT(response);
849 p = strchr(username, '%');
851 if (p) {
852 *p = 0;
853 fstrcpy(request.data.auth.user, username);
854 fstrcpy(request.data.auth.pass, p + 1);
855 *p = '%';
856 } else {
857 fstrcpy(request.data.auth.user, username);
858 fstrcpy(request.data.auth.pass, getpass("Password: "));
861 request.flags |= WBFLAG_PAM_AFS_TOKEN;
863 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
865 /* Display response */
867 d_printf("plaintext password authentication %s\n",
868 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
870 if (response.data.auth.nt_status)
871 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
872 response.data.auth.nt_status_string,
873 response.data.auth.nt_status,
874 response.data.auth.error_string);
876 if (result != NSS_STATUS_SUCCESS)
877 return False;
879 if (response.extra_data.data == NULL) {
880 d_fprintf(stderr, "Did not get token data\n");
881 return False;
884 if (!afs_settoken_str((char *)response.extra_data.data)) {
885 d_fprintf(stderr, "Could not set token\n");
886 return False;
889 d_printf("Successfully created AFS token\n");
890 return True;
893 /* Print domain users */
895 static BOOL print_domain_users(const char *domain)
897 struct winbindd_request request;
898 struct winbindd_response response;
899 const char *extra_data;
900 fstring name;
902 /* Send request to winbind daemon */
904 ZERO_STRUCT(request);
905 ZERO_STRUCT(response);
907 if (domain) {
908 /* '.' is the special sign for our own domwin */
909 if ( strequal(domain, ".") )
910 fstrcpy( request.domain_name, lp_workgroup() );
911 else
912 fstrcpy( request.domain_name, domain );
915 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
916 NSS_STATUS_SUCCESS)
917 return False;
919 /* Look through extra data */
921 if (!response.extra_data.data)
922 return False;
924 extra_data = (const char *)response.extra_data.data;
926 while(next_token(&extra_data, name, ",", sizeof(fstring)))
927 d_printf("%s\n", name);
929 SAFE_FREE(response.extra_data.data);
931 return True;
934 /* Print domain groups */
936 static BOOL print_domain_groups(const char *domain)
938 struct winbindd_request request;
939 struct winbindd_response response;
940 const char *extra_data;
941 fstring name;
943 ZERO_STRUCT(request);
944 ZERO_STRUCT(response);
946 if (domain) {
947 if ( strequal(domain, ".") )
948 fstrcpy( request.domain_name, lp_workgroup() );
949 else
950 fstrcpy( request.domain_name, domain );
953 if (winbindd_request_response(WINBINDD_LIST_GROUPS, &request, &response) !=
954 NSS_STATUS_SUCCESS)
955 return False;
957 /* Look through extra data */
959 if (!response.extra_data.data)
960 return False;
962 extra_data = (const char *)response.extra_data.data;
964 while(next_token(&extra_data, name, ",", sizeof(fstring)))
965 d_printf("%s\n", name);
967 SAFE_FREE(response.extra_data.data);
969 return True;
972 /* Set the authorised user for winbindd access in secrets.tdb */
974 static BOOL wbinfo_set_auth_user(char *username)
976 const char *password;
977 char *p;
978 fstring user, domain;
980 /* Separate into user and password */
982 parse_wbinfo_domain_user(username, domain, user);
984 p = strchr(user, '%');
986 if (p != NULL) {
987 *p = 0;
988 password = p+1;
989 } else {
990 char *thepass = getpass("Password: ");
991 if (thepass) {
992 password = thepass;
993 } else
994 password = "";
997 /* Store or remove DOMAIN\username%password in secrets.tdb */
999 secrets_init();
1001 if (user[0]) {
1003 if (!secrets_store(SECRETS_AUTH_USER, user,
1004 strlen(user) + 1)) {
1005 d_fprintf(stderr, "error storing username\n");
1006 return False;
1009 /* We always have a domain name added by the
1010 parse_wbinfo_domain_user() function. */
1012 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
1013 strlen(domain) + 1)) {
1014 d_fprintf(stderr, "error storing domain name\n");
1015 return False;
1018 } else {
1019 secrets_delete(SECRETS_AUTH_USER);
1020 secrets_delete(SECRETS_AUTH_DOMAIN);
1023 if (password[0]) {
1025 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
1026 strlen(password) + 1)) {
1027 d_fprintf(stderr, "error storing password\n");
1028 return False;
1031 } else
1032 secrets_delete(SECRETS_AUTH_PASSWORD);
1034 return True;
1037 static void wbinfo_get_auth_user(void)
1039 char *user, *domain, *password;
1041 /* Lift data from secrets file */
1043 secrets_fetch_ipc_userpass(&user, &domain, &password);
1045 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
1047 SAFE_FREE(user);
1048 SAFE_FREE(domain);
1049 SAFE_FREE(password);
1050 d_printf("No authorised user configured\n");
1051 return;
1054 /* Pretty print authorised user info */
1056 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
1057 user, password ? "%" : "", password ? password : "");
1059 SAFE_FREE(user);
1060 SAFE_FREE(domain);
1061 SAFE_FREE(password);
1064 static BOOL wbinfo_ping(void)
1066 NSS_STATUS result;
1068 result = winbindd_request_response(WINBINDD_PING, NULL, NULL);
1070 /* Display response */
1072 d_printf("Ping to winbindd %s on fd %d\n",
1073 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
1075 return result == NSS_STATUS_SUCCESS;
1078 /* Main program */
1080 enum {
1081 OPT_SET_AUTH_USER = 1000,
1082 OPT_GET_AUTH_USER,
1083 OPT_DOMAIN_NAME,
1084 OPT_SEQUENCE,
1085 OPT_GETDCNAME,
1086 OPT_USERDOMGROUPS,
1087 OPT_USERSIDS,
1088 OPT_ALLOCATE_UID,
1089 OPT_ALLOCATE_GID,
1090 OPT_SEPARATOR,
1091 OPT_LIST_ALL_DOMAINS,
1092 OPT_LIST_OWN_DOMAIN
1095 int main(int argc, char **argv)
1097 int opt;
1099 poptContext pc;
1100 static char *string_arg;
1101 static char *opt_domain_name;
1102 static int int_arg;
1103 int result = 1;
1105 struct poptOption long_options[] = {
1106 POPT_AUTOHELP
1108 /* longName, shortName, argInfo, argPtr, value, descrip,
1109 argDesc */
1111 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1112 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1113 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1114 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1115 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1116 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1117 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1118 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1119 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1120 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1121 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1122 "Get a new UID out of idmap" },
1123 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1124 "Get a new GID out of idmap" },
1125 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1126 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1127 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1128 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1129 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1130 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1131 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1132 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1133 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1134 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1135 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1136 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1137 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1138 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1139 "Get a DC name for a foreign domain", "domainname" },
1140 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1141 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1142 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1143 #ifdef WITH_FAKE_KASERVER
1144 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1145 #endif
1146 #ifdef HAVE_KRB5
1147 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1148 /* destroys wbinfo --help output */
1149 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1150 #endif
1151 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1152 POPT_COMMON_VERSION
1153 POPT_TABLEEND
1156 /* Samba client initialisation */
1157 load_case_tables();
1159 if (!lp_load(dyn_CONFIGFILE, True, False, False, True)) {
1160 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1161 dyn_CONFIGFILE, strerror(errno));
1162 exit(1);
1165 if (!init_names())
1166 return 1;
1168 load_interfaces();
1170 /* Parse options */
1172 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1174 /* Parse command line options */
1176 if (argc == 1) {
1177 poptPrintHelp(pc, stderr, 0);
1178 return 1;
1181 while((opt = poptGetNextOpt(pc)) != -1) {
1182 /* get the generic configuration parameters like --domain */
1185 poptFreeContext(pc);
1187 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1188 POPT_CONTEXT_KEEP_FIRST);
1190 while((opt = poptGetNextOpt(pc)) != -1) {
1191 switch (opt) {
1192 case 'u':
1193 if (!print_domain_users(opt_domain_name)) {
1194 d_fprintf(stderr, "Error looking up domain users\n");
1195 goto done;
1197 break;
1198 case 'g':
1199 if (!print_domain_groups(opt_domain_name)) {
1200 d_fprintf(stderr, "Error looking up domain groups\n");
1201 goto done;
1203 break;
1204 case 's':
1205 if (!wbinfo_lookupsid(string_arg)) {
1206 d_fprintf(stderr, "Could not lookup sid %s\n", string_arg);
1207 goto done;
1209 break;
1210 case 'n':
1211 if (!wbinfo_lookupname(string_arg)) {
1212 d_fprintf(stderr, "Could not lookup name %s\n", string_arg);
1213 goto done;
1215 break;
1216 case 'N':
1217 if (!wbinfo_wins_byname(string_arg)) {
1218 d_fprintf(stderr, "Could not lookup WINS by name %s\n", string_arg);
1219 goto done;
1221 break;
1222 case 'I':
1223 if (!wbinfo_wins_byip(string_arg)) {
1224 d_fprintf(stderr, "Could not lookup WINS by IP %s\n", string_arg);
1225 goto done;
1227 break;
1228 case 'U':
1229 if (!wbinfo_uid_to_sid(int_arg)) {
1230 d_fprintf(stderr, "Could not convert uid %d to sid\n", int_arg);
1231 goto done;
1233 break;
1234 case 'G':
1235 if (!wbinfo_gid_to_sid(int_arg)) {
1236 d_fprintf(stderr, "Could not convert gid %d to sid\n",
1237 int_arg);
1238 goto done;
1240 break;
1241 case 'S':
1242 if (!wbinfo_sid_to_uid(string_arg)) {
1243 d_fprintf(stderr, "Could not convert sid %s to uid\n",
1244 string_arg);
1245 goto done;
1247 break;
1248 case 'Y':
1249 if (!wbinfo_sid_to_gid(string_arg)) {
1250 d_fprintf(stderr, "Could not convert sid %s to gid\n",
1251 string_arg);
1252 goto done;
1254 break;
1255 case OPT_ALLOCATE_UID:
1256 if (!wbinfo_allocate_uid()) {
1257 d_fprintf(stderr, "Could not allocate a uid\n");
1258 goto done;
1260 break;
1261 case OPT_ALLOCATE_GID:
1262 if (!wbinfo_allocate_gid()) {
1263 d_fprintf(stderr, "Could not allocate a gid\n");
1264 goto done;
1266 break;
1267 case 't':
1268 if (!wbinfo_check_secret()) {
1269 d_fprintf(stderr, "Could not check secret\n");
1270 goto done;
1272 break;
1273 case 'm':
1274 if (!wbinfo_list_domains(False)) {
1275 d_fprintf(stderr, "Could not list trusted domains\n");
1276 goto done;
1278 break;
1279 case OPT_SEQUENCE:
1280 if (!wbinfo_show_sequence(opt_domain_name)) {
1281 d_fprintf(stderr, "Could not show sequence numbers\n");
1282 goto done;
1284 break;
1285 case 'D':
1286 if (!wbinfo_domain_info(string_arg)) {
1287 d_fprintf(stderr, "Could not get domain info\n");
1288 goto done;
1290 break;
1291 case 'i':
1292 if (!wbinfo_get_userinfo(string_arg)) {
1293 d_fprintf(stderr, "Could not get info for user %s\n",
1294 string_arg);
1295 goto done;
1297 break;
1298 case 'r':
1299 if (!wbinfo_get_usergroups(string_arg)) {
1300 d_fprintf(stderr, "Could not get groups for user %s\n",
1301 string_arg);
1302 goto done;
1304 break;
1305 case OPT_USERSIDS:
1306 if (!wbinfo_get_usersids(string_arg)) {
1307 d_fprintf(stderr, "Could not get group SIDs for user SID %s\n",
1308 string_arg);
1309 goto done;
1311 break;
1312 case OPT_USERDOMGROUPS:
1313 if (!wbinfo_get_userdomgroups(string_arg)) {
1314 d_fprintf(stderr, "Could not get user's domain groups "
1315 "for user SID %s\n", string_arg);
1316 goto done;
1318 break;
1319 case 'a': {
1320 BOOL got_error = False;
1322 if (!wbinfo_auth(string_arg)) {
1323 d_fprintf(stderr, "Could not authenticate user %s with "
1324 "plaintext password\n", string_arg);
1325 got_error = True;
1328 if (!wbinfo_auth_crap(string_arg)) {
1329 d_fprintf(stderr, "Could not authenticate user %s with "
1330 "challenge/response\n", string_arg);
1331 got_error = True;
1334 if (got_error)
1335 goto done;
1336 break;
1338 case 'K': {
1339 BOOL got_error = False;
1340 uint32 flags = WBFLAG_PAM_KRB5 |
1341 WBFLAG_PAM_CACHED_LOGIN |
1342 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
1343 WBFLAG_PAM_INFO3_TEXT;
1344 fstring tok;
1345 int i;
1346 const char *arg[] = { NULL, NULL };
1347 const char *cctypes[] = { "FILE",
1348 "KCM",
1349 "KCM:0",
1350 "Garbage",
1351 NULL,
1352 "0"};
1354 arg[0] = string_arg;
1356 while (next_token(arg, tok, LIST_SEP, sizeof(tok))) {
1358 for (i=0; i < ARRAY_SIZE(cctypes); i++) {
1359 if (!wbinfo_auth_krb5(tok, cctypes[i], flags)) {
1360 d_fprintf(stderr, "Could not authenticate user [%s] with "
1361 "Kerberos (ccache: %s)\n", tok, cctypes[i]);
1362 got_error = True;
1367 if (got_error)
1368 goto done;
1370 break;
1372 case 'k':
1373 if (!wbinfo_klog(string_arg)) {
1374 d_fprintf(stderr, "Could not klog user\n");
1375 goto done;
1377 break;
1378 case 'p':
1379 if (!wbinfo_ping()) {
1380 d_fprintf(stderr, "could not ping winbindd!\n");
1381 goto done;
1383 break;
1384 case OPT_SET_AUTH_USER:
1385 if (!wbinfo_set_auth_user(string_arg)) {
1386 goto done;
1388 break;
1389 case OPT_GET_AUTH_USER:
1390 wbinfo_get_auth_user();
1391 break;
1392 case OPT_GETDCNAME:
1393 if (!wbinfo_getdcname(string_arg)) {
1394 goto done;
1396 break;
1397 case OPT_SEPARATOR: {
1398 const char sep = winbind_separator_int(True);
1399 if ( !sep ) {
1400 goto done;
1402 d_printf("%c\n", sep);
1403 break;
1405 case OPT_LIST_ALL_DOMAINS:
1406 if (!wbinfo_list_domains(True)) {
1407 goto done;
1409 break;
1410 case OPT_LIST_OWN_DOMAIN:
1411 if (!wbinfo_list_own_domain()) {
1412 goto done;
1414 break;
1415 /* generic configuration options */
1416 case OPT_DOMAIN_NAME:
1417 break;
1418 default:
1419 d_fprintf(stderr, "Invalid option\n");
1420 poptPrintHelp(pc, stderr, 0);
1421 goto done;
1425 result = 0;
1427 /* Exit code */
1429 done:
1430 poptFreeContext(pc);
1431 return result;