r13230: ready to do for 3.0.21b
[Samba.git] / source / nsswitch / wbinfo.c
blob3a7a9e4b4f9b3d779fb308856404fd9ab0098f3f
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 /* List groups a user is a member of */
124 static BOOL wbinfo_get_usergroups(char *user)
126 struct winbindd_request request;
127 struct winbindd_response response;
128 NSS_STATUS result;
129 int i;
131 ZERO_STRUCT(request);
132 ZERO_STRUCT(response);
134 /* Send request */
136 fstrcpy(request.data.username, user);
138 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
140 if (result != NSS_STATUS_SUCCESS)
141 return False;
143 for (i = 0; i < response.data.num_entries; i++)
144 d_printf("%d\n", (int)((gid_t *)response.extra_data)[i]);
146 SAFE_FREE(response.extra_data);
148 return True;
152 /* List group SIDs a user SID is a member of */
153 static BOOL wbinfo_get_usersids(char *user_sid)
155 struct winbindd_request request;
156 struct winbindd_response response;
157 NSS_STATUS result;
158 int i;
159 const char *s;
161 ZERO_STRUCT(request);
162 ZERO_STRUCT(response);
164 /* Send request */
165 fstrcpy(request.data.sid, user_sid);
167 result = winbindd_request_response(WINBINDD_GETUSERSIDS, &request, &response);
169 if (result != NSS_STATUS_SUCCESS)
170 return False;
172 s = response.extra_data;
173 for (i = 0; i < response.data.num_entries; i++) {
174 d_printf("%s\n", s);
175 s += strlen(s) + 1;
178 SAFE_FREE(response.extra_data);
180 return True;
183 static BOOL wbinfo_get_userdomgroups(const char *user_sid)
185 struct winbindd_request request;
186 struct winbindd_response response;
187 NSS_STATUS result;
189 ZERO_STRUCT(request);
190 ZERO_STRUCT(response);
192 /* Send request */
193 fstrcpy(request.data.sid, user_sid);
195 result = winbindd_request_response(WINBINDD_GETUSERDOMGROUPS, &request,
196 &response);
198 if (result != NSS_STATUS_SUCCESS)
199 return False;
201 if (response.data.num_entries != 0)
202 d_printf("%s", (char *)response.extra_data);
204 SAFE_FREE(response.extra_data);
206 return True;
209 /* Convert NetBIOS name to IP */
211 static BOOL wbinfo_wins_byname(char *name)
213 struct winbindd_request request;
214 struct winbindd_response response;
216 ZERO_STRUCT(request);
217 ZERO_STRUCT(response);
219 /* Send request */
221 fstrcpy(request.data.winsreq, name);
223 if (winbindd_request_response(WINBINDD_WINS_BYNAME, &request, &response) !=
224 NSS_STATUS_SUCCESS) {
225 return False;
228 /* Display response */
230 d_printf("%s\n", response.data.winsresp);
232 return True;
235 /* Convert IP to NetBIOS name */
237 static BOOL wbinfo_wins_byip(char *ip)
239 struct winbindd_request request;
240 struct winbindd_response response;
242 ZERO_STRUCT(request);
243 ZERO_STRUCT(response);
245 /* Send request */
247 fstrcpy(request.data.winsreq, ip);
249 if (winbindd_request_response(WINBINDD_WINS_BYIP, &request, &response) !=
250 NSS_STATUS_SUCCESS) {
251 return False;
254 /* Display response */
256 d_printf("%s\n", response.data.winsresp);
258 return True;
261 /* List trusted domains */
263 static BOOL wbinfo_list_domains(void)
265 struct winbindd_response response;
267 ZERO_STRUCT(response);
269 /* Send request */
271 if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM, NULL, &response) !=
272 NSS_STATUS_SUCCESS)
273 return False;
275 /* Display response */
277 if (response.extra_data) {
278 const char *extra_data = (char *)response.extra_data;
279 fstring name;
280 char *p;
282 while(next_token(&extra_data, name, "\n", sizeof(fstring))) {
283 p = strchr(name, '\\');
284 if (p == 0) {
285 d_fprintf(stderr, "Got invalid response: %s\n",
286 extra_data);
287 return False;
289 *p = 0;
290 d_printf("%s\n", name);
293 SAFE_FREE(response.extra_data);
296 return True;
300 /* show sequence numbers */
301 static BOOL wbinfo_show_sequence(const char *domain)
303 struct winbindd_request request;
304 struct winbindd_response response;
306 ZERO_STRUCT(response);
307 ZERO_STRUCT(request);
309 if ( domain )
310 fstrcpy( request.domain_name, domain );
312 /* Send request */
314 if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
315 NSS_STATUS_SUCCESS)
316 return False;
318 /* Display response */
320 if (response.extra_data) {
321 char *extra_data = (char *)response.extra_data;
322 d_printf("%s", extra_data);
323 SAFE_FREE(response.extra_data);
326 return True;
329 /* Show domain info */
331 static BOOL wbinfo_domain_info(const char *domain_name)
333 struct winbindd_request request;
334 struct winbindd_response response;
336 ZERO_STRUCT(request);
337 ZERO_STRUCT(response);
339 fstrcpy(request.domain_name, domain_name);
341 /* Send request */
343 if (winbindd_request_response(WINBINDD_DOMAIN_INFO, &request, &response) !=
344 NSS_STATUS_SUCCESS)
345 return False;
347 /* Display response */
349 d_printf("Name : %s\n", response.data.domain_info.name);
350 d_printf("Alt_Name : %s\n", response.data.domain_info.alt_name);
352 d_printf("SID : %s\n", response.data.domain_info.sid);
354 d_printf("Active Directory : %s\n",
355 response.data.domain_info.active_directory ? "Yes" : "No");
356 d_printf("Native : %s\n",
357 response.data.domain_info.native_mode ? "Yes" : "No");
359 d_printf("Primary : %s\n",
360 response.data.domain_info.primary ? "Yes" : "No");
362 d_printf("Sequence : %d\n", response.data.domain_info.sequence_number);
364 return True;
367 /* Get a foreign DC's name */
368 static BOOL wbinfo_getdcname(const char *domain_name)
370 struct winbindd_request request;
371 struct winbindd_response response;
373 ZERO_STRUCT(request);
374 ZERO_STRUCT(response);
376 fstrcpy(request.domain_name, domain_name);
378 /* Send request */
380 if (winbindd_request_response(WINBINDD_GETDCNAME, &request, &response) !=
381 NSS_STATUS_SUCCESS) {
382 d_fprintf(stderr, "Could not get dc name for %s\n", domain_name);
383 return False;
386 /* Display response */
388 d_printf("%s\n", response.data.dc_name);
390 return True;
393 /* Check trust account password */
395 static BOOL wbinfo_check_secret(void)
397 struct winbindd_response response;
398 NSS_STATUS result;
400 ZERO_STRUCT(response);
402 result = winbindd_request_response(WINBINDD_CHECK_MACHACC, NULL, &response);
404 d_printf("checking the trust secret via RPC calls %s\n",
405 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
407 if (result != NSS_STATUS_SUCCESS)
408 d_fprintf(stderr, "error code was %s (0x%x)\n",
409 response.data.auth.nt_status_string,
410 response.data.auth.nt_status);
412 return result == NSS_STATUS_SUCCESS;
415 /* Convert uid to sid */
417 static BOOL wbinfo_uid_to_sid(uid_t uid)
419 struct winbindd_request request;
420 struct winbindd_response response;
422 ZERO_STRUCT(request);
423 ZERO_STRUCT(response);
425 /* Send request */
427 request.data.uid = uid;
429 if (winbindd_request_response(WINBINDD_UID_TO_SID, &request, &response) !=
430 NSS_STATUS_SUCCESS)
431 return False;
433 /* Display response */
435 d_printf("%s\n", response.data.sid.sid);
437 return True;
440 /* Convert gid to sid */
442 static BOOL wbinfo_gid_to_sid(gid_t gid)
444 struct winbindd_request request;
445 struct winbindd_response response;
447 ZERO_STRUCT(request);
448 ZERO_STRUCT(response);
450 /* Send request */
452 request.data.gid = gid;
454 if (winbindd_request_response(WINBINDD_GID_TO_SID, &request, &response) !=
455 NSS_STATUS_SUCCESS)
456 return False;
458 /* Display response */
460 d_printf("%s\n", response.data.sid.sid);
462 return True;
465 /* Convert sid to uid */
467 static BOOL wbinfo_sid_to_uid(char *sid)
469 struct winbindd_request request;
470 struct winbindd_response response;
472 ZERO_STRUCT(request);
473 ZERO_STRUCT(response);
475 /* Send request */
477 fstrcpy(request.data.sid, sid);
479 if (winbindd_request_response(WINBINDD_SID_TO_UID, &request, &response) !=
480 NSS_STATUS_SUCCESS)
481 return False;
483 /* Display response */
485 d_printf("%d\n", (int)response.data.uid);
487 return True;
490 static BOOL wbinfo_sid_to_gid(char *sid)
492 struct winbindd_request request;
493 struct winbindd_response response;
495 ZERO_STRUCT(request);
496 ZERO_STRUCT(response);
498 /* Send request */
500 fstrcpy(request.data.sid, sid);
502 if (winbindd_request_response(WINBINDD_SID_TO_GID, &request, &response) !=
503 NSS_STATUS_SUCCESS)
504 return False;
506 /* Display response */
508 d_printf("%d\n", (int)response.data.gid);
510 return True;
513 static BOOL wbinfo_allocate_rid(void)
515 uint32 rid;
517 if (!winbind_allocate_rid(&rid))
518 return False;
520 d_printf("New rid: %d\n", rid);
522 return True;
525 /* Convert sid to string */
527 static BOOL wbinfo_lookupsid(char *sid)
529 struct winbindd_request request;
530 struct winbindd_response response;
532 ZERO_STRUCT(request);
533 ZERO_STRUCT(response);
535 /* Send off request */
537 fstrcpy(request.data.sid, sid);
539 if (winbindd_request_response(WINBINDD_LOOKUPSID, &request, &response) !=
540 NSS_STATUS_SUCCESS)
541 return False;
543 /* Display response */
545 d_printf("%s%c%s %d\n", response.data.name.dom_name,
546 winbind_separator(), response.data.name.name,
547 response.data.name.type);
549 return True;
552 /* Convert string to sid */
554 static BOOL wbinfo_lookupname(char *name)
556 struct winbindd_request request;
557 struct winbindd_response response;
559 /* Send off request */
561 ZERO_STRUCT(request);
562 ZERO_STRUCT(response);
564 parse_wbinfo_domain_user(name, request.data.name.dom_name,
565 request.data.name.name);
567 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
568 NSS_STATUS_SUCCESS)
569 return False;
571 /* Display response */
573 d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
575 return True;
578 /* Authenticate a user with a plaintext password */
580 static BOOL wbinfo_auth(char *username)
582 struct winbindd_request request;
583 struct winbindd_response response;
584 NSS_STATUS result;
585 char *p;
587 /* Send off request */
589 ZERO_STRUCT(request);
590 ZERO_STRUCT(response);
592 p = strchr(username, '%');
594 if (p) {
595 *p = 0;
596 fstrcpy(request.data.auth.user, username);
597 fstrcpy(request.data.auth.pass, p + 1);
598 *p = '%';
599 } else
600 fstrcpy(request.data.auth.user, username);
602 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
604 /* Display response */
606 d_printf("plaintext password authentication %s\n",
607 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
609 if (response.data.auth.nt_status)
610 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
611 response.data.auth.nt_status_string,
612 response.data.auth.nt_status,
613 response.data.auth.error_string);
615 return result == NSS_STATUS_SUCCESS;
618 /* Authenticate a user with a challenge/response */
620 static BOOL wbinfo_auth_crap(char *username)
622 struct winbindd_request request;
623 struct winbindd_response response;
624 NSS_STATUS result;
625 fstring name_user;
626 fstring name_domain;
627 fstring pass;
628 char *p;
630 /* Send off request */
632 ZERO_STRUCT(request);
633 ZERO_STRUCT(response);
635 p = strchr(username, '%');
637 if (p) {
638 *p = 0;
639 fstrcpy(pass, p + 1);
642 parse_wbinfo_domain_user(username, name_domain, name_user);
644 request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
646 fstrcpy(request.data.auth_crap.user, name_user);
648 fstrcpy(request.data.auth_crap.domain,
649 name_domain);
651 generate_random_buffer(request.data.auth_crap.chal, 8);
653 if (lp_client_ntlmv2_auth()) {
654 DATA_BLOB server_chal;
655 DATA_BLOB names_blob;
657 DATA_BLOB lm_response;
658 DATA_BLOB nt_response;
660 server_chal = data_blob(request.data.auth_crap.chal, 8);
662 /* Pretend this is a login to 'us', for blob purposes */
663 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
665 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
666 &names_blob,
667 &lm_response, &nt_response, NULL)) {
668 data_blob_free(&names_blob);
669 data_blob_free(&server_chal);
670 return False;
672 data_blob_free(&names_blob);
673 data_blob_free(&server_chal);
675 memcpy(request.data.auth_crap.nt_resp, nt_response.data,
676 MIN(nt_response.length,
677 sizeof(request.data.auth_crap.nt_resp)));
678 request.data.auth_crap.nt_resp_len = nt_response.length;
680 memcpy(request.data.auth_crap.lm_resp, lm_response.data,
681 MIN(lm_response.length,
682 sizeof(request.data.auth_crap.lm_resp)));
683 request.data.auth_crap.lm_resp_len = lm_response.length;
685 data_blob_free(&nt_response);
686 data_blob_free(&lm_response);
688 } else {
689 if (lp_client_lanman_auth()
690 && SMBencrypt(pass, request.data.auth_crap.chal,
691 (uchar *)request.data.auth_crap.lm_resp)) {
692 request.data.auth_crap.lm_resp_len = 24;
693 } else {
694 request.data.auth_crap.lm_resp_len = 0;
696 SMBNTencrypt(pass, request.data.auth_crap.chal,
697 (uchar *)request.data.auth_crap.nt_resp);
699 request.data.auth_crap.nt_resp_len = 24;
702 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
704 /* Display response */
706 d_printf("challenge/response password authentication %s\n",
707 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
709 if (response.data.auth.nt_status)
710 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
711 response.data.auth.nt_status_string,
712 response.data.auth.nt_status,
713 response.data.auth.error_string);
715 return result == NSS_STATUS_SUCCESS;
718 /* Authenticate a user with a plaintext password and set a token */
720 static BOOL wbinfo_klog(char *username)
722 struct winbindd_request request;
723 struct winbindd_response response;
724 NSS_STATUS result;
725 char *p;
727 /* Send off request */
729 ZERO_STRUCT(request);
730 ZERO_STRUCT(response);
732 p = strchr(username, '%');
734 if (p) {
735 *p = 0;
736 fstrcpy(request.data.auth.user, username);
737 fstrcpy(request.data.auth.pass, p + 1);
738 *p = '%';
739 } else {
740 fstrcpy(request.data.auth.user, username);
741 fstrcpy(request.data.auth.pass, getpass("Password: "));
744 request.flags |= WBFLAG_PAM_AFS_TOKEN;
746 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
748 /* Display response */
750 d_printf("plaintext password authentication %s\n",
751 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
753 if (response.data.auth.nt_status)
754 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
755 response.data.auth.nt_status_string,
756 response.data.auth.nt_status,
757 response.data.auth.error_string);
759 if (result != NSS_STATUS_SUCCESS)
760 return False;
762 if (response.extra_data == NULL) {
763 d_fprintf(stderr, "Did not get token data\n");
764 return False;
767 if (!afs_settoken_str((char *)response.extra_data)) {
768 d_fprintf(stderr, "Could not set token\n");
769 return False;
772 d_printf("Successfully created AFS token\n");
773 return True;
776 /* Print domain users */
778 static BOOL print_domain_users(const char *domain)
780 struct winbindd_request request;
781 struct winbindd_response response;
782 const char *extra_data;
783 fstring name;
785 /* Send request to winbind daemon */
787 ZERO_STRUCT(request);
788 ZERO_STRUCT(response);
790 if (domain) {
791 /* '.' is the special sign for our own domwin */
792 if ( strequal(domain, ".") )
793 fstrcpy( request.domain_name, lp_workgroup() );
794 else
795 fstrcpy( request.domain_name, domain );
798 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
799 NSS_STATUS_SUCCESS)
800 return False;
802 /* Look through extra data */
804 if (!response.extra_data)
805 return False;
807 extra_data = (const char *)response.extra_data;
809 while(next_token(&extra_data, name, ",", sizeof(fstring)))
810 d_printf("%s\n", name);
812 SAFE_FREE(response.extra_data);
814 return True;
817 /* Print domain groups */
819 static BOOL print_domain_groups(const char *domain)
821 struct winbindd_request request;
822 struct winbindd_response response;
823 const char *extra_data;
824 fstring name;
826 ZERO_STRUCT(request);
827 ZERO_STRUCT(response);
829 if (domain) {
830 if ( strequal(domain, ".") )
831 fstrcpy( request.domain_name, lp_workgroup() );
832 else
833 fstrcpy( request.domain_name, domain );
836 if (winbindd_request_response(WINBINDD_LIST_GROUPS, &request, &response) !=
837 NSS_STATUS_SUCCESS)
838 return False;
840 /* Look through extra data */
842 if (!response.extra_data)
843 return False;
845 extra_data = (const char *)response.extra_data;
847 while(next_token(&extra_data, name, ",", sizeof(fstring)))
848 d_printf("%s\n", name);
850 SAFE_FREE(response.extra_data);
852 return True;
855 /* Set the authorised user for winbindd access in secrets.tdb */
857 static BOOL wbinfo_set_auth_user(char *username)
859 const char *password;
860 char *p;
861 fstring user, domain;
863 /* Separate into user and password */
865 parse_wbinfo_domain_user(username, domain, user);
867 p = strchr(user, '%');
869 if (p != NULL) {
870 *p = 0;
871 password = p+1;
872 } else {
873 char *thepass = getpass("Password: ");
874 if (thepass) {
875 password = thepass;
876 } else
877 password = "";
880 /* Store or remove DOMAIN\username%password in secrets.tdb */
882 secrets_init();
884 if (user[0]) {
886 if (!secrets_store(SECRETS_AUTH_USER, user,
887 strlen(user) + 1)) {
888 d_fprintf(stderr, "error storing username\n");
889 return False;
892 /* We always have a domain name added by the
893 parse_wbinfo_domain_user() function. */
895 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
896 strlen(domain) + 1)) {
897 d_fprintf(stderr, "error storing domain name\n");
898 return False;
901 } else {
902 secrets_delete(SECRETS_AUTH_USER);
903 secrets_delete(SECRETS_AUTH_DOMAIN);
906 if (password[0]) {
908 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
909 strlen(password) + 1)) {
910 d_fprintf(stderr, "error storing password\n");
911 return False;
914 } else
915 secrets_delete(SECRETS_AUTH_PASSWORD);
917 return True;
920 static void wbinfo_get_auth_user(void)
922 char *user, *domain, *password;
924 /* Lift data from secrets file */
926 secrets_fetch_ipc_userpass(&user, &domain, &password);
928 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
930 SAFE_FREE(user);
931 SAFE_FREE(domain);
932 SAFE_FREE(password);
933 d_printf("No authorised user configured\n");
934 return;
937 /* Pretty print authorised user info */
939 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
940 user, password ? "%" : "", password ? password : "");
942 SAFE_FREE(user);
943 SAFE_FREE(domain);
944 SAFE_FREE(password);
947 static BOOL wbinfo_ping(void)
949 NSS_STATUS result;
951 result = winbindd_request_response(WINBINDD_PING, NULL, NULL);
953 /* Display response */
955 d_printf("Ping to winbindd %s on fd %d\n",
956 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
958 return result == NSS_STATUS_SUCCESS;
961 /* Main program */
963 enum {
964 OPT_SET_AUTH_USER = 1000,
965 OPT_GET_AUTH_USER,
966 OPT_DOMAIN_NAME,
967 OPT_SEQUENCE,
968 OPT_GETDCNAME,
969 OPT_USERDOMGROUPS,
970 OPT_USERSIDS,
971 OPT_SEPARATOR
974 int main(int argc, char **argv)
976 int opt;
978 poptContext pc;
979 static char *string_arg;
980 static char *opt_domain_name;
981 static int int_arg;
982 int result = 1;
984 struct poptOption long_options[] = {
985 POPT_AUTOHELP
987 /* longName, shortName, argInfo, argPtr, value, descrip,
988 argDesc */
990 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
991 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
992 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
993 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
994 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
995 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
996 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
997 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
998 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
999 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1000 { "allocate-rid", 'A', POPT_ARG_NONE, 0, 'A', "Get a new RID out of idmap" },
1001 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1002 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1003 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1004 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1005 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1006 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1007 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1008 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1009 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1010 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1011 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1012 "Get a DC name for a foreign domain", "domainname" },
1013 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1014 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1015 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1016 #ifdef WITH_FAKE_KASERVER
1017 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1018 #endif
1019 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1020 POPT_COMMON_VERSION
1021 POPT_TABLEEND
1024 /* Samba client initialisation */
1025 load_case_tables();
1027 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
1028 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1029 dyn_CONFIGFILE, strerror(errno));
1030 exit(1);
1033 if (!init_names())
1034 return 1;
1036 load_interfaces();
1038 /* Parse options */
1040 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1042 /* Parse command line options */
1044 if (argc == 1) {
1045 poptPrintHelp(pc, stderr, 0);
1046 return 1;
1049 while((opt = poptGetNextOpt(pc)) != -1) {
1050 /* get the generic configuration parameters like --domain */
1053 poptFreeContext(pc);
1055 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1056 POPT_CONTEXT_KEEP_FIRST);
1058 while((opt = poptGetNextOpt(pc)) != -1) {
1059 switch (opt) {
1060 case 'u':
1061 if (!print_domain_users(opt_domain_name)) {
1062 d_fprintf(stderr, "Error looking up domain users\n");
1063 goto done;
1065 break;
1066 case 'g':
1067 if (!print_domain_groups(opt_domain_name)) {
1068 d_fprintf(stderr, "Error looking up domain groups\n");
1069 goto done;
1071 break;
1072 case 's':
1073 if (!wbinfo_lookupsid(string_arg)) {
1074 d_fprintf(stderr, "Could not lookup sid %s\n", string_arg);
1075 goto done;
1077 break;
1078 case 'n':
1079 if (!wbinfo_lookupname(string_arg)) {
1080 d_fprintf(stderr, "Could not lookup name %s\n", string_arg);
1081 goto done;
1083 break;
1084 case 'N':
1085 if (!wbinfo_wins_byname(string_arg)) {
1086 d_fprintf(stderr, "Could not lookup WINS by name %s\n", string_arg);
1087 goto done;
1089 break;
1090 case 'I':
1091 if (!wbinfo_wins_byip(string_arg)) {
1092 d_fprintf(stderr, "Could not lookup WINS by IP %s\n", string_arg);
1093 goto done;
1095 break;
1096 case 'U':
1097 if (!wbinfo_uid_to_sid(int_arg)) {
1098 d_fprintf(stderr, "Could not convert uid %d to sid\n", int_arg);
1099 goto done;
1101 break;
1102 case 'G':
1103 if (!wbinfo_gid_to_sid(int_arg)) {
1104 d_fprintf(stderr, "Could not convert gid %d to sid\n",
1105 int_arg);
1106 goto done;
1108 break;
1109 case 'S':
1110 if (!wbinfo_sid_to_uid(string_arg)) {
1111 d_fprintf(stderr, "Could not convert sid %s to uid\n",
1112 string_arg);
1113 goto done;
1115 break;
1116 case 'Y':
1117 if (!wbinfo_sid_to_gid(string_arg)) {
1118 d_fprintf(stderr, "Could not convert sid %s to gid\n",
1119 string_arg);
1120 goto done;
1122 break;
1123 case 'A':
1124 if (!wbinfo_allocate_rid()) {
1125 d_fprintf(stderr, "Could not allocate a RID\n");
1126 goto done;
1128 break;
1129 case 't':
1130 if (!wbinfo_check_secret()) {
1131 d_fprintf(stderr, "Could not check secret\n");
1132 goto done;
1134 break;
1135 case 'm':
1136 if (!wbinfo_list_domains()) {
1137 d_fprintf(stderr, "Could not list trusted domains\n");
1138 goto done;
1140 break;
1141 case OPT_SEQUENCE:
1142 if (!wbinfo_show_sequence(opt_domain_name)) {
1143 d_fprintf(stderr, "Could not show sequence numbers\n");
1144 goto done;
1146 break;
1147 case 'D':
1148 if (!wbinfo_domain_info(string_arg)) {
1149 d_fprintf(stderr, "Could not get domain info\n");
1150 goto done;
1152 break;
1153 case 'r':
1154 if (!wbinfo_get_usergroups(string_arg)) {
1155 d_fprintf(stderr, "Could not get groups for user %s\n",
1156 string_arg);
1157 goto done;
1159 break;
1160 case OPT_USERSIDS:
1161 if (!wbinfo_get_usersids(string_arg)) {
1162 d_fprintf(stderr, "Could not get group SIDs for user SID %s\n",
1163 string_arg);
1164 goto done;
1166 break;
1167 case OPT_USERDOMGROUPS:
1168 if (!wbinfo_get_userdomgroups(string_arg)) {
1169 d_fprintf(stderr, "Could not get user's domain groups "
1170 "for user SID %s\n", string_arg);
1171 goto done;
1173 break;
1174 case 'a': {
1175 BOOL got_error = False;
1177 if (!wbinfo_auth(string_arg)) {
1178 d_fprintf(stderr, "Could not authenticate user %s with "
1179 "plaintext password\n", string_arg);
1180 got_error = True;
1183 if (!wbinfo_auth_crap(string_arg)) {
1184 d_fprintf(stderr, "Could not authenticate user %s with "
1185 "challenge/response\n", string_arg);
1186 got_error = True;
1189 if (got_error)
1190 goto done;
1191 break;
1193 case 'k':
1194 if (!wbinfo_klog(string_arg)) {
1195 d_fprintf(stderr, "Could not klog user\n");
1196 goto done;
1198 break;
1199 case 'p':
1200 if (!wbinfo_ping()) {
1201 d_fprintf(stderr, "Could not ping winbindd!\n");
1202 goto done;
1204 break;
1205 case OPT_SET_AUTH_USER:
1206 if (!wbinfo_set_auth_user(string_arg)) {
1207 goto done;
1209 break;
1210 case OPT_GET_AUTH_USER:
1211 wbinfo_get_auth_user();
1212 break;
1213 case OPT_GETDCNAME:
1214 if (!wbinfo_getdcname(string_arg)) {
1215 goto done;
1217 break;
1218 case OPT_SEPARATOR: {
1219 const char sep = winbind_separator_int(True);
1220 if ( !sep ) {
1221 goto done;
1223 d_printf("%c\n", sep);
1224 break;
1226 /* generic configuration options */
1227 case OPT_DOMAIN_NAME:
1228 break;
1229 default:
1230 d_fprintf(stderr, "Invalid option\n");
1231 poptPrintHelp(pc, stderr, 0);
1232 goto done;
1236 result = 0;
1238 /* Exit code */
1240 done:
1241 poptFreeContext(pc);
1242 return result;