r7415: * big change -- volker's new async winbindd from trunk
[Samba/gbeck.git] / source / nsswitch / wbinfo.c
blob8407bb1e3a2fae4b5d8d51a2802a3677cff2842b
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(void)
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(WINBINDD_INFO, NULL, &response) !=
47 NSS_STATUS_SUCCESS) {
48 d_printf("could not obtain winbind separator!\n");
49 /* HACK: (this module should not call lp_ funtions) */
50 return *lp_winbind_separator();
53 sep = response.data.info.winbind_separator;
54 got_sep = True;
56 if (!sep) {
57 d_printf("winbind separator was NULL!\n");
58 /* HACK: (this module should not call lp_ funtions) */
59 sep = *lp_winbind_separator();
62 return sep;
65 static const char *get_winbind_domain(void)
67 struct winbindd_response response;
68 static fstring winbind_domain;
70 ZERO_STRUCT(response);
72 /* Send off request */
74 if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
75 NSS_STATUS_SUCCESS) {
76 d_printf("could not obtain winbind domain name!\n");
78 /* HACK: (this module should not call lp_ funtions) */
79 return lp_workgroup();
82 fstrcpy(winbind_domain, response.data.domain_name);
84 return winbind_domain;
88 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
89 form DOMAIN/user into a domain and a user */
91 static BOOL parse_wbinfo_domain_user(const char *domuser, fstring domain,
92 fstring user)
95 char *p = strchr(domuser,winbind_separator());
97 if (!p) {
98 fstrcpy(user, domuser);
99 fstrcpy(domain, get_winbind_domain());
100 return True;
103 fstrcpy(user, p+1);
104 fstrcpy(domain, domuser);
105 domain[PTR_DIFF(p, domuser)] = 0;
106 strupper_m(domain);
108 return True;
111 /* List groups a user is a member of */
113 static BOOL wbinfo_get_usergroups(char *user)
115 struct winbindd_request request;
116 struct winbindd_response response;
117 NSS_STATUS result;
118 int i;
120 ZERO_STRUCT(response);
122 /* Send request */
124 fstrcpy(request.data.username, user);
126 result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
128 if (result != NSS_STATUS_SUCCESS)
129 return False;
131 for (i = 0; i < response.data.num_entries; i++)
132 d_printf("%d\n", (int)((gid_t *)response.extra_data)[i]);
134 SAFE_FREE(response.extra_data);
136 return True;
140 /* List group SIDs a user SID is a member of */
141 static BOOL wbinfo_get_usersids(char *user_sid)
143 struct winbindd_request request;
144 struct winbindd_response response;
145 NSS_STATUS result;
146 int i;
147 const char *s;
149 ZERO_STRUCT(response);
151 /* Send request */
152 fstrcpy(request.data.sid, user_sid);
154 result = winbindd_request(WINBINDD_GETUSERSIDS, &request, &response);
156 if (result != NSS_STATUS_SUCCESS)
157 return False;
159 s = response.extra_data;
160 for (i = 0; i < response.data.num_entries; i++) {
161 d_printf("%s\n", s);
162 s += strlen(s) + 1;
165 SAFE_FREE(response.extra_data);
167 return True;
170 static BOOL wbinfo_get_userdomgroups(const char *user_sid)
172 struct winbindd_request request;
173 struct winbindd_response response;
174 NSS_STATUS result;
176 ZERO_STRUCT(response);
178 /* Send request */
179 fstrcpy(request.data.sid, user_sid);
181 result = winbindd_request(WINBINDD_GETUSERDOMGROUPS, &request,
182 &response);
184 if (result != NSS_STATUS_SUCCESS)
185 return False;
187 if (response.data.num_entries != 0)
188 printf("%s", (char *)response.extra_data);
190 SAFE_FREE(response.extra_data);
192 return True;
196 /* Convert NetBIOS name to IP */
198 static BOOL wbinfo_wins_byname(char *name)
200 struct winbindd_request request;
201 struct winbindd_response response;
203 ZERO_STRUCT(request);
204 ZERO_STRUCT(response);
206 /* Send request */
208 fstrcpy(request.data.winsreq, name);
210 if (winbindd_request(WINBINDD_WINS_BYNAME, &request, &response) !=
211 NSS_STATUS_SUCCESS) {
212 return False;
215 /* Display response */
217 printf("%s\n", response.data.winsresp);
219 return True;
222 /* Convert IP to NetBIOS name */
224 static BOOL wbinfo_wins_byip(char *ip)
226 struct winbindd_request request;
227 struct winbindd_response response;
229 ZERO_STRUCT(request);
230 ZERO_STRUCT(response);
232 /* Send request */
234 fstrcpy(request.data.winsreq, ip);
236 if (winbindd_request(WINBINDD_WINS_BYIP, &request, &response) !=
237 NSS_STATUS_SUCCESS) {
238 return False;
241 /* Display response */
243 printf("%s\n", response.data.winsresp);
245 return True;
248 /* List trusted domains */
250 static BOOL wbinfo_list_domains(void)
252 struct winbindd_response response;
254 ZERO_STRUCT(response);
256 /* Send request */
258 if (winbindd_request(WINBINDD_LIST_TRUSTDOM, NULL, &response) !=
259 NSS_STATUS_SUCCESS)
260 return False;
262 /* Display response */
264 if (response.extra_data) {
265 const char *extra_data = (char *)response.extra_data;
266 fstring name;
267 char *p;
269 while(next_token(&extra_data, name, "\n", sizeof(fstring))) {
270 p = strchr(name, '\\');
271 if (p == 0) {
272 d_printf("Got invalid response: %s\n",
273 extra_data);
274 return False;
276 *p = 0;
277 d_printf("%s\n", name);
280 SAFE_FREE(response.extra_data);
283 return True;
287 /* show sequence numbers */
288 static BOOL wbinfo_show_sequence(const char *domain)
290 struct winbindd_request request;
291 struct winbindd_response response;
293 ZERO_STRUCT(response);
294 ZERO_STRUCT(request);
296 if ( domain )
297 fstrcpy( request.domain_name, domain );
299 /* Send request */
301 if (winbindd_request(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
302 NSS_STATUS_SUCCESS)
303 return False;
305 /* Display response */
307 if (response.extra_data) {
308 char *extra_data = (char *)response.extra_data;
309 d_printf("%s", extra_data);
310 SAFE_FREE(response.extra_data);
313 return True;
316 /* Show domain info */
318 static BOOL wbinfo_domain_info(const char *domain_name)
320 struct winbindd_request request;
321 struct winbindd_response response;
323 ZERO_STRUCT(request);
324 ZERO_STRUCT(response);
326 fstrcpy(request.domain_name, domain_name);
328 /* Send request */
330 if (winbindd_request(WINBINDD_DOMAIN_INFO, &request, &response) !=
331 NSS_STATUS_SUCCESS)
332 return False;
334 /* Display response */
336 d_printf("Name : %s\n", response.data.domain_info.name);
337 d_printf("Alt_Name : %s\n", response.data.domain_info.alt_name);
339 d_printf("SID : %s\n", response.data.domain_info.sid);
341 d_printf("Active Directory : %s\n",
342 response.data.domain_info.active_directory ? "Yes" : "No");
343 d_printf("Native : %s\n",
344 response.data.domain_info.native_mode ? "Yes" : "No");
346 d_printf("Primary : %s\n",
347 response.data.domain_info.primary ? "Yes" : "No");
349 d_printf("Sequence : %d\n", response.data.domain_info.sequence_number);
351 return True;
354 /* Get a foreign DC's name */
355 static BOOL wbinfo_getdcname(const char *domain_name)
357 struct winbindd_request request;
358 struct winbindd_response response;
360 ZERO_STRUCT(request);
361 ZERO_STRUCT(response);
363 fstrcpy(request.domain_name, domain_name);
365 /* Send request */
367 if (winbindd_request(WINBINDD_GETDCNAME, &request, &response) !=
368 NSS_STATUS_SUCCESS) {
369 d_printf("Could not get dc name for %s\n", domain_name);
370 return False;
373 /* Display response */
375 d_printf("%s\n", response.data.dc_name);
377 return True;
380 /* Check trust account password */
382 static BOOL wbinfo_check_secret(void)
384 struct winbindd_response response;
385 NSS_STATUS result;
387 ZERO_STRUCT(response);
389 result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response);
391 d_printf("checking the trust secret via RPC calls %s\n",
392 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
394 if (result != NSS_STATUS_SUCCESS)
395 d_printf("error code was %s (0x%x)\n",
396 response.data.auth.nt_status_string,
397 response.data.auth.nt_status);
399 return result == NSS_STATUS_SUCCESS;
402 /* Convert uid to sid */
404 static BOOL wbinfo_uid_to_sid(uid_t uid)
406 struct winbindd_request request;
407 struct winbindd_response response;
409 ZERO_STRUCT(request);
410 ZERO_STRUCT(response);
412 /* Send request */
414 request.data.uid = uid;
416 if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
417 NSS_STATUS_SUCCESS)
418 return False;
420 /* Display response */
422 d_printf("%s\n", response.data.sid.sid);
424 return True;
427 /* Convert gid to sid */
429 static BOOL wbinfo_gid_to_sid(gid_t gid)
431 struct winbindd_request request;
432 struct winbindd_response response;
434 ZERO_STRUCT(request);
435 ZERO_STRUCT(response);
437 /* Send request */
439 request.data.gid = gid;
441 if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) !=
442 NSS_STATUS_SUCCESS)
443 return False;
445 /* Display response */
447 d_printf("%s\n", response.data.sid.sid);
449 return True;
452 /* Convert sid to uid */
454 static BOOL wbinfo_sid_to_uid(char *sid)
456 struct winbindd_request request;
457 struct winbindd_response response;
459 ZERO_STRUCT(request);
460 ZERO_STRUCT(response);
462 /* Send request */
464 fstrcpy(request.data.sid, sid);
466 if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
467 NSS_STATUS_SUCCESS)
468 return False;
470 /* Display response */
472 d_printf("%d\n", (int)response.data.uid);
474 return True;
477 static BOOL wbinfo_sid_to_gid(char *sid)
479 struct winbindd_request request;
480 struct winbindd_response response;
482 ZERO_STRUCT(request);
483 ZERO_STRUCT(response);
485 /* Send request */
487 fstrcpy(request.data.sid, sid);
489 if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
490 NSS_STATUS_SUCCESS)
491 return False;
493 /* Display response */
495 d_printf("%d\n", (int)response.data.gid);
497 return True;
500 static BOOL wbinfo_allocate_rid(void)
502 uint32 rid;
504 if (!winbind_allocate_rid(&rid))
505 return False;
507 d_printf("New rid: %d\n", rid);
509 return True;
512 /* Convert sid to string */
514 static BOOL wbinfo_lookupsid(char *sid)
516 struct winbindd_request request;
517 struct winbindd_response response;
519 ZERO_STRUCT(request);
520 ZERO_STRUCT(response);
522 /* Send off request */
524 fstrcpy(request.data.sid, sid);
526 if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
527 NSS_STATUS_SUCCESS)
528 return False;
530 /* Display response */
532 d_printf("%s%c%s %d\n", response.data.name.dom_name,
533 winbind_separator(), response.data.name.name,
534 response.data.name.type);
536 return True;
539 /* Convert string to sid */
541 static BOOL wbinfo_lookupname(char *name)
543 struct winbindd_request request;
544 struct winbindd_response response;
546 /* Send off request */
548 ZERO_STRUCT(request);
549 ZERO_STRUCT(response);
551 parse_wbinfo_domain_user(name, request.data.name.dom_name,
552 request.data.name.name);
554 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
555 NSS_STATUS_SUCCESS)
556 return False;
558 /* Display response */
560 d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
562 return True;
565 /* Authenticate a user with a plaintext password */
567 static BOOL wbinfo_auth(char *username)
569 struct winbindd_request request;
570 struct winbindd_response response;
571 NSS_STATUS result;
572 char *p;
574 /* Send off request */
576 ZERO_STRUCT(request);
577 ZERO_STRUCT(response);
579 p = strchr(username, '%');
581 if (p) {
582 *p = 0;
583 fstrcpy(request.data.auth.user, username);
584 fstrcpy(request.data.auth.pass, p + 1);
585 *p = '%';
586 } else
587 fstrcpy(request.data.auth.user, username);
589 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
591 /* Display response */
593 d_printf("plaintext password authentication %s\n",
594 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
596 if (response.data.auth.nt_status)
597 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
598 response.data.auth.nt_status_string,
599 response.data.auth.nt_status,
600 response.data.auth.error_string);
602 return result == NSS_STATUS_SUCCESS;
605 /* Authenticate a user with a challenge/response */
607 static BOOL wbinfo_auth_crap(char *username)
609 struct winbindd_request request;
610 struct winbindd_response response;
611 NSS_STATUS result;
612 fstring name_user;
613 fstring name_domain;
614 fstring pass;
615 char *p;
617 /* Send off request */
619 ZERO_STRUCT(request);
620 ZERO_STRUCT(response);
622 p = strchr(username, '%');
624 if (p) {
625 *p = 0;
626 fstrcpy(pass, p + 1);
629 parse_wbinfo_domain_user(username, name_domain, name_user);
631 fstrcpy(request.data.auth_crap.user, name_user);
633 fstrcpy(request.data.auth_crap.domain,
634 name_domain);
636 generate_random_buffer(request.data.auth_crap.chal, 8);
638 if (lp_client_ntlmv2_auth()) {
639 DATA_BLOB server_chal;
640 DATA_BLOB names_blob;
642 DATA_BLOB lm_response;
643 DATA_BLOB nt_response;
645 server_chal = data_blob(request.data.auth_crap.chal, 8);
647 /* Pretend this is a login to 'us', for blob purposes */
648 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
650 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
651 &names_blob,
652 &lm_response, &nt_response, NULL)) {
653 data_blob_free(&names_blob);
654 data_blob_free(&server_chal);
655 return False;
657 data_blob_free(&names_blob);
658 data_blob_free(&server_chal);
660 memcpy(request.data.auth_crap.nt_resp, nt_response.data,
661 MIN(nt_response.length,
662 sizeof(request.data.auth_crap.nt_resp)));
663 request.data.auth_crap.nt_resp_len = nt_response.length;
665 memcpy(request.data.auth_crap.lm_resp, lm_response.data,
666 MIN(lm_response.length,
667 sizeof(request.data.auth_crap.lm_resp)));
668 request.data.auth_crap.lm_resp_len = lm_response.length;
670 data_blob_free(&nt_response);
671 data_blob_free(&lm_response);
673 } else {
674 if (lp_client_lanman_auth()
675 && SMBencrypt(pass, request.data.auth_crap.chal,
676 (uchar *)request.data.auth_crap.lm_resp)) {
677 request.data.auth_crap.lm_resp_len = 24;
678 } else {
679 request.data.auth_crap.lm_resp_len = 0;
681 SMBNTencrypt(pass, request.data.auth_crap.chal,
682 (uchar *)request.data.auth_crap.nt_resp);
684 request.data.auth_crap.nt_resp_len = 24;
687 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
689 /* Display response */
691 d_printf("challenge/response password authentication %s\n",
692 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
694 if (response.data.auth.nt_status)
695 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
696 response.data.auth.nt_status_string,
697 response.data.auth.nt_status,
698 response.data.auth.error_string);
700 return result == NSS_STATUS_SUCCESS;
703 /* Authenticate a user with a plaintext password and set a token */
705 static BOOL wbinfo_klog(char *username)
707 struct winbindd_request request;
708 struct winbindd_response response;
709 NSS_STATUS result;
710 char *p;
712 /* Send off request */
714 ZERO_STRUCT(request);
715 ZERO_STRUCT(response);
717 p = strchr(username, '%');
719 if (p) {
720 *p = 0;
721 fstrcpy(request.data.auth.user, username);
722 fstrcpy(request.data.auth.pass, p + 1);
723 *p = '%';
724 } else {
725 fstrcpy(request.data.auth.user, username);
726 fstrcpy(request.data.auth.pass, getpass("Password: "));
729 request.flags |= WBFLAG_PAM_AFS_TOKEN;
731 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
733 /* Display response */
735 d_printf("plaintext password authentication %s\n",
736 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
738 if (response.data.auth.nt_status)
739 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
740 response.data.auth.nt_status_string,
741 response.data.auth.nt_status,
742 response.data.auth.error_string);
744 if (result != NSS_STATUS_SUCCESS)
745 return False;
747 if (response.extra_data == NULL) {
748 d_printf("Did not get token data\n");
749 return False;
752 if (!afs_settoken_str((char *)response.extra_data)) {
753 d_printf("Could not set token\n");
754 return False;
757 d_printf("Successfully created AFS token\n");
758 return True;
761 /* Print domain users */
763 static BOOL print_domain_users(const char *domain)
765 struct winbindd_request request;
766 struct winbindd_response response;
767 const char *extra_data;
768 fstring name;
770 /* Send request to winbind daemon */
772 ZERO_STRUCT(request);
773 ZERO_STRUCT(response);
775 if (domain) {
776 /* '.' is the special sign for our own domwin */
777 if ( strequal(domain, ".") )
778 fstrcpy( request.domain_name, lp_workgroup() );
779 else
780 fstrcpy( request.domain_name, domain );
783 if (winbindd_request(WINBINDD_LIST_USERS, &request, &response) !=
784 NSS_STATUS_SUCCESS)
785 return False;
787 /* Look through extra data */
789 if (!response.extra_data)
790 return False;
792 extra_data = (const char *)response.extra_data;
794 while(next_token(&extra_data, name, ",", sizeof(fstring)))
795 d_printf("%s\n", name);
797 SAFE_FREE(response.extra_data);
799 return True;
802 /* Print domain groups */
804 static BOOL print_domain_groups(const char *domain)
806 struct winbindd_request request;
807 struct winbindd_response response;
808 const char *extra_data;
809 fstring name;
811 ZERO_STRUCT(request);
812 ZERO_STRUCT(response);
814 if (domain) {
815 if ( strequal(domain, ".") )
816 fstrcpy( request.domain_name, lp_workgroup() );
817 else
818 fstrcpy( request.domain_name, domain );
821 if (winbindd_request(WINBINDD_LIST_GROUPS, &request, &response) !=
822 NSS_STATUS_SUCCESS)
823 return False;
825 /* Look through extra data */
827 if (!response.extra_data)
828 return False;
830 extra_data = (const char *)response.extra_data;
832 while(next_token(&extra_data, name, ",", sizeof(fstring)))
833 d_printf("%s\n", name);
835 SAFE_FREE(response.extra_data);
837 return True;
840 /* Set the authorised user for winbindd access in secrets.tdb */
842 static BOOL wbinfo_set_auth_user(char *username)
844 const char *password;
845 char *p;
846 fstring user, domain;
848 /* Separate into user and password */
850 parse_wbinfo_domain_user(username, domain, user);
852 p = strchr(user, '%');
854 if (p != NULL) {
855 *p = 0;
856 password = p+1;
857 } else {
858 char *thepass = getpass("Password: ");
859 if (thepass) {
860 password = thepass;
861 } else
862 password = "";
865 /* Store or remove DOMAIN\username%password in secrets.tdb */
867 secrets_init();
869 if (user[0]) {
871 if (!secrets_store(SECRETS_AUTH_USER, user,
872 strlen(user) + 1)) {
873 d_fprintf(stderr, "error storing username\n");
874 return False;
877 /* We always have a domain name added by the
878 parse_wbinfo_domain_user() function. */
880 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
881 strlen(domain) + 1)) {
882 d_fprintf(stderr, "error storing domain name\n");
883 return False;
886 } else {
887 secrets_delete(SECRETS_AUTH_USER);
888 secrets_delete(SECRETS_AUTH_DOMAIN);
891 if (password[0]) {
893 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
894 strlen(password) + 1)) {
895 d_fprintf(stderr, "error storing password\n");
896 return False;
899 } else
900 secrets_delete(SECRETS_AUTH_PASSWORD);
902 return True;
905 static void wbinfo_get_auth_user(void)
907 char *user, *domain, *password;
909 /* Lift data from secrets file */
911 secrets_fetch_ipc_userpass(&user, &domain, &password);
913 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
915 SAFE_FREE(user);
916 SAFE_FREE(domain);
917 SAFE_FREE(password);
918 d_printf("No authorised user configured\n");
919 return;
922 /* Pretty print authorised user info */
924 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
925 user, password ? "%" : "", password ? password : "");
927 SAFE_FREE(user);
928 SAFE_FREE(domain);
929 SAFE_FREE(password);
932 static BOOL wbinfo_ping(void)
934 NSS_STATUS result;
936 result = winbindd_request(WINBINDD_PING, NULL, NULL);
938 /* Display response */
940 d_printf("Ping to winbindd %s on fd %d\n",
941 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
943 return result == NSS_STATUS_SUCCESS;
946 /* Main program */
948 enum {
949 OPT_SET_AUTH_USER = 1000,
950 OPT_GET_AUTH_USER,
951 OPT_DOMAIN_NAME,
952 OPT_SEQUENCE,
953 OPT_GETDCNAME,
954 OPT_USERDOMGROUPS,
955 OPT_USERSIDS
958 int main(int argc, char **argv)
960 int opt;
962 poptContext pc;
963 static char *string_arg;
964 static char *opt_domain_name;
965 static int int_arg;
966 int result = 1;
968 struct poptOption long_options[] = {
969 POPT_AUTOHELP
971 /* longName, shortName, argInfo, argPtr, value, descrip,
972 argDesc */
974 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
975 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
976 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
977 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
978 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
979 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
980 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
981 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
982 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
983 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
984 { "allocate-rid", 'A', POPT_ARG_NONE, 0, 'A', "Get a new RID out of idmap" },
985 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
986 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
987 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
988 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
989 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
990 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
991 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
992 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
993 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
994 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
995 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
996 "Get a DC name for a foreign domain", "domainname" },
997 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
998 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
999 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1000 #ifdef WITH_FAKE_KASERVER
1001 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1002 #endif
1003 POPT_COMMON_VERSION
1004 POPT_TABLEEND
1007 /* Samba client initialisation */
1009 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
1010 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1011 dyn_CONFIGFILE, strerror(errno));
1012 exit(1);
1015 if (!init_names())
1016 return 1;
1018 load_interfaces();
1020 /* Parse options */
1022 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1024 /* Parse command line options */
1026 if (argc == 1) {
1027 poptPrintHelp(pc, stderr, 0);
1028 return 1;
1031 while((opt = poptGetNextOpt(pc)) != -1) {
1032 /* get the generic configuration parameters like --domain */
1035 poptFreeContext(pc);
1037 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1038 POPT_CONTEXT_KEEP_FIRST);
1040 while((opt = poptGetNextOpt(pc)) != -1) {
1041 switch (opt) {
1042 case 'u':
1043 if (!print_domain_users(opt_domain_name)) {
1044 d_printf("Error looking up domain users\n");
1045 goto done;
1047 break;
1048 case 'g':
1049 if (!print_domain_groups(opt_domain_name)) {
1050 d_printf("Error looking up domain groups\n");
1051 goto done;
1053 break;
1054 case 's':
1055 if (!wbinfo_lookupsid(string_arg)) {
1056 d_printf("Could not lookup sid %s\n", string_arg);
1057 goto done;
1059 break;
1060 case 'n':
1061 if (!wbinfo_lookupname(string_arg)) {
1062 d_printf("Could not lookup name %s\n", string_arg);
1063 goto done;
1065 break;
1066 case 'N':
1067 if (!wbinfo_wins_byname(string_arg)) {
1068 d_printf("Could not lookup WINS by name %s\n", string_arg);
1069 goto done;
1071 break;
1072 case 'I':
1073 if (!wbinfo_wins_byip(string_arg)) {
1074 d_printf("Could not lookup WINS by IP %s\n", string_arg);
1075 goto done;
1077 break;
1078 case 'U':
1079 if (!wbinfo_uid_to_sid(int_arg)) {
1080 d_printf("Could not convert uid %d to sid\n", int_arg);
1081 goto done;
1083 break;
1084 case 'G':
1085 if (!wbinfo_gid_to_sid(int_arg)) {
1086 d_printf("Could not convert gid %d to sid\n",
1087 int_arg);
1088 goto done;
1090 break;
1091 case 'S':
1092 if (!wbinfo_sid_to_uid(string_arg)) {
1093 d_printf("Could not convert sid %s to uid\n",
1094 string_arg);
1095 goto done;
1097 break;
1098 case 'Y':
1099 if (!wbinfo_sid_to_gid(string_arg)) {
1100 d_printf("Could not convert sid %s to gid\n",
1101 string_arg);
1102 goto done;
1104 break;
1105 case 'A':
1106 if (!wbinfo_allocate_rid()) {
1107 d_printf("Could not allocate a RID\n");
1108 goto done;
1110 break;
1111 case 't':
1112 if (!wbinfo_check_secret()) {
1113 d_printf("Could not check secret\n");
1114 goto done;
1116 break;
1117 case 'm':
1118 if (!wbinfo_list_domains()) {
1119 d_printf("Could not list trusted domains\n");
1120 goto done;
1122 break;
1123 case OPT_SEQUENCE:
1124 if (!wbinfo_show_sequence(opt_domain_name)) {
1125 d_printf("Could not show sequence numbers\n");
1126 goto done;
1128 break;
1129 case 'D':
1130 if (!wbinfo_domain_info(string_arg)) {
1131 d_printf("Could not get domain info\n");
1132 goto done;
1134 break;
1135 case 'r':
1136 if (!wbinfo_get_usergroups(string_arg)) {
1137 d_printf("Could not get groups for user %s\n",
1138 string_arg);
1139 goto done;
1141 break;
1142 case OPT_USERSIDS:
1143 if (!wbinfo_get_usersids(string_arg)) {
1144 d_printf("Could not get group SIDs for user SID %s\n",
1145 string_arg);
1146 goto done;
1148 break;
1149 case OPT_USERDOMGROUPS:
1150 if (!wbinfo_get_userdomgroups(string_arg)) {
1151 d_printf("Could not get user's domain groups "
1152 "for user SID %s\n", string_arg);
1153 goto done;
1155 break;
1156 case 'a': {
1157 BOOL got_error = False;
1159 if (!wbinfo_auth(string_arg)) {
1160 d_printf("Could not authenticate user %s with "
1161 "plaintext password\n", string_arg);
1162 got_error = True;
1165 if (!wbinfo_auth_crap(string_arg)) {
1166 d_printf("Could not authenticate user %s with "
1167 "challenge/response\n", string_arg);
1168 got_error = True;
1171 if (got_error)
1172 goto done;
1173 break;
1175 case 'k':
1176 if (!wbinfo_klog(string_arg)) {
1177 d_printf("Could not klog user\n");
1178 goto done;
1180 break;
1181 case 'p':
1182 if (!wbinfo_ping()) {
1183 d_printf("could not ping winbindd!\n");
1184 goto done;
1186 break;
1187 case OPT_SET_AUTH_USER:
1188 wbinfo_set_auth_user(string_arg);
1189 break;
1190 case OPT_GET_AUTH_USER:
1191 wbinfo_get_auth_user();
1192 break;
1193 case OPT_GETDCNAME:
1194 wbinfo_getdcname(string_arg);
1195 break;
1196 /* generic configuration options */
1197 case OPT_DOMAIN_NAME:
1198 break;
1199 default:
1200 d_fprintf(stderr, "Invalid option\n");
1201 poptPrintHelp(pc, stderr, 0);
1202 goto done;
1206 result = 0;
1208 /* Exit code */
1210 done:
1211 poptFreeContext(pc);
1212 return result;