r10656: BIG merge from trunk. Features not copied over
[Samba/nascimento.git] / source3 / nsswitch / wbinfo.c
blob60f1d6b7221d21f1722938a9554bd313c1c7b75a
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_response(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_response(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_response(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_response(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_response(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;
195 /* Convert NetBIOS name to IP */
197 static BOOL wbinfo_wins_byname(char *name)
199 struct winbindd_request request;
200 struct winbindd_response response;
202 ZERO_STRUCT(request);
203 ZERO_STRUCT(response);
205 /* Send request */
207 fstrcpy(request.data.winsreq, name);
209 if (winbindd_request_response(WINBINDD_WINS_BYNAME, &request, &response) !=
210 NSS_STATUS_SUCCESS) {
211 return False;
214 /* Display response */
216 printf("%s\n", response.data.winsresp);
218 return True;
221 /* Convert IP to NetBIOS name */
223 static BOOL wbinfo_wins_byip(char *ip)
225 struct winbindd_request request;
226 struct winbindd_response response;
228 ZERO_STRUCT(request);
229 ZERO_STRUCT(response);
231 /* Send request */
233 fstrcpy(request.data.winsreq, ip);
235 if (winbindd_request_response(WINBINDD_WINS_BYIP, &request, &response) !=
236 NSS_STATUS_SUCCESS) {
237 return False;
240 /* Display response */
242 printf("%s\n", response.data.winsresp);
244 return True;
247 /* List trusted domains */
249 static BOOL wbinfo_list_domains(void)
251 struct winbindd_response response;
253 ZERO_STRUCT(response);
255 /* Send request */
257 if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM, NULL, &response) !=
258 NSS_STATUS_SUCCESS)
259 return False;
261 /* Display response */
263 if (response.extra_data) {
264 const char *extra_data = (char *)response.extra_data;
265 fstring name;
266 char *p;
268 while(next_token(&extra_data, name, "\n", sizeof(fstring))) {
269 p = strchr(name, '\\');
270 if (p == 0) {
271 d_printf("Got invalid response: %s\n",
272 extra_data);
273 return False;
275 *p = 0;
276 d_printf("%s\n", name);
279 SAFE_FREE(response.extra_data);
282 return True;
286 /* show sequence numbers */
287 static BOOL wbinfo_show_sequence(const char *domain)
289 struct winbindd_request request;
290 struct winbindd_response response;
292 ZERO_STRUCT(response);
293 ZERO_STRUCT(request);
295 if ( domain )
296 fstrcpy( request.domain_name, domain );
298 /* Send request */
300 if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
301 NSS_STATUS_SUCCESS)
302 return False;
304 /* Display response */
306 if (response.extra_data) {
307 char *extra_data = (char *)response.extra_data;
308 d_printf("%s", extra_data);
309 SAFE_FREE(response.extra_data);
312 return True;
315 /* Show domain info */
317 static BOOL wbinfo_domain_info(const char *domain_name)
319 struct winbindd_request request;
320 struct winbindd_response response;
322 ZERO_STRUCT(request);
323 ZERO_STRUCT(response);
325 fstrcpy(request.domain_name, domain_name);
327 /* Send request */
329 if (winbindd_request_response(WINBINDD_DOMAIN_INFO, &request, &response) !=
330 NSS_STATUS_SUCCESS)
331 return False;
333 /* Display response */
335 d_printf("Name : %s\n", response.data.domain_info.name);
336 d_printf("Alt_Name : %s\n", response.data.domain_info.alt_name);
338 d_printf("SID : %s\n", response.data.domain_info.sid);
340 d_printf("Active Directory : %s\n",
341 response.data.domain_info.active_directory ? "Yes" : "No");
342 d_printf("Native : %s\n",
343 response.data.domain_info.native_mode ? "Yes" : "No");
345 d_printf("Primary : %s\n",
346 response.data.domain_info.primary ? "Yes" : "No");
348 d_printf("Sequence : %d\n", response.data.domain_info.sequence_number);
350 return True;
353 /* Get a foreign DC's name */
354 static BOOL wbinfo_getdcname(const char *domain_name)
356 struct winbindd_request request;
357 struct winbindd_response response;
359 ZERO_STRUCT(request);
360 ZERO_STRUCT(response);
362 fstrcpy(request.domain_name, domain_name);
364 /* Send request */
366 if (winbindd_request_response(WINBINDD_GETDCNAME, &request, &response) !=
367 NSS_STATUS_SUCCESS) {
368 d_printf("Could not get dc name for %s\n", domain_name);
369 return False;
372 /* Display response */
374 d_printf("%s\n", response.data.dc_name);
376 return True;
379 /* Check trust account password */
381 static BOOL wbinfo_check_secret(void)
383 struct winbindd_response response;
384 NSS_STATUS result;
386 ZERO_STRUCT(response);
388 result = winbindd_request_response(WINBINDD_CHECK_MACHACC, NULL, &response);
390 d_printf("checking the trust secret via RPC calls %s\n",
391 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
393 if (result != NSS_STATUS_SUCCESS)
394 d_printf("error code was %s (0x%x)\n",
395 response.data.auth.nt_status_string,
396 response.data.auth.nt_status);
398 return result == NSS_STATUS_SUCCESS;
401 /* Convert uid to sid */
403 static BOOL wbinfo_uid_to_sid(uid_t uid)
405 struct winbindd_request request;
406 struct winbindd_response response;
408 ZERO_STRUCT(request);
409 ZERO_STRUCT(response);
411 /* Send request */
413 request.data.uid = uid;
415 if (winbindd_request_response(WINBINDD_UID_TO_SID, &request, &response) !=
416 NSS_STATUS_SUCCESS)
417 return False;
419 /* Display response */
421 d_printf("%s\n", response.data.sid.sid);
423 return True;
426 /* Convert gid to sid */
428 static BOOL wbinfo_gid_to_sid(gid_t gid)
430 struct winbindd_request request;
431 struct winbindd_response response;
433 ZERO_STRUCT(request);
434 ZERO_STRUCT(response);
436 /* Send request */
438 request.data.gid = gid;
440 if (winbindd_request_response(WINBINDD_GID_TO_SID, &request, &response) !=
441 NSS_STATUS_SUCCESS)
442 return False;
444 /* Display response */
446 d_printf("%s\n", response.data.sid.sid);
448 return True;
451 /* Convert sid to uid */
453 static BOOL wbinfo_sid_to_uid(char *sid)
455 struct winbindd_request request;
456 struct winbindd_response response;
458 ZERO_STRUCT(request);
459 ZERO_STRUCT(response);
461 /* Send request */
463 fstrcpy(request.data.sid, sid);
465 if (winbindd_request_response(WINBINDD_SID_TO_UID, &request, &response) !=
466 NSS_STATUS_SUCCESS)
467 return False;
469 /* Display response */
471 d_printf("%d\n", (int)response.data.uid);
473 return True;
476 static BOOL wbinfo_sid_to_gid(char *sid)
478 struct winbindd_request request;
479 struct winbindd_response response;
481 ZERO_STRUCT(request);
482 ZERO_STRUCT(response);
484 /* Send request */
486 fstrcpy(request.data.sid, sid);
488 if (winbindd_request_response(WINBINDD_SID_TO_GID, &request, &response) !=
489 NSS_STATUS_SUCCESS)
490 return False;
492 /* Display response */
494 d_printf("%d\n", (int)response.data.gid);
496 return True;
499 static BOOL wbinfo_allocate_rid(void)
501 uint32 rid;
503 if (!winbind_allocate_rid(&rid))
504 return False;
506 d_printf("New rid: %d\n", rid);
508 return True;
511 /* Convert sid to string */
513 static BOOL wbinfo_lookupsid(char *sid)
515 struct winbindd_request request;
516 struct winbindd_response response;
518 ZERO_STRUCT(request);
519 ZERO_STRUCT(response);
521 /* Send off request */
523 fstrcpy(request.data.sid, sid);
525 if (winbindd_request_response(WINBINDD_LOOKUPSID, &request, &response) !=
526 NSS_STATUS_SUCCESS)
527 return False;
529 /* Display response */
531 d_printf("%s%c%s %d\n", response.data.name.dom_name,
532 winbind_separator(), response.data.name.name,
533 response.data.name.type);
535 return True;
538 /* Convert string to sid */
540 static BOOL wbinfo_lookupname(char *name)
542 struct winbindd_request request;
543 struct winbindd_response response;
545 /* Send off request */
547 ZERO_STRUCT(request);
548 ZERO_STRUCT(response);
550 parse_wbinfo_domain_user(name, request.data.name.dom_name,
551 request.data.name.name);
553 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
554 NSS_STATUS_SUCCESS)
555 return False;
557 /* Display response */
559 d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
561 return True;
564 /* Authenticate a user with a plaintext password */
566 static BOOL wbinfo_auth(char *username)
568 struct winbindd_request request;
569 struct winbindd_response response;
570 NSS_STATUS result;
571 char *p;
573 /* Send off request */
575 ZERO_STRUCT(request);
576 ZERO_STRUCT(response);
578 p = strchr(username, '%');
580 if (p) {
581 *p = 0;
582 fstrcpy(request.data.auth.user, username);
583 fstrcpy(request.data.auth.pass, p + 1);
584 *p = '%';
585 } else
586 fstrcpy(request.data.auth.user, username);
588 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
590 /* Display response */
592 d_printf("plaintext password authentication %s\n",
593 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
595 if (response.data.auth.nt_status)
596 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
597 response.data.auth.nt_status_string,
598 response.data.auth.nt_status,
599 response.data.auth.error_string);
601 return result == NSS_STATUS_SUCCESS;
604 /* Authenticate a user with a challenge/response */
606 static BOOL wbinfo_auth_crap(char *username)
608 struct winbindd_request request;
609 struct winbindd_response response;
610 NSS_STATUS result;
611 fstring name_user;
612 fstring name_domain;
613 fstring pass;
614 char *p;
616 /* Send off request */
618 ZERO_STRUCT(request);
619 ZERO_STRUCT(response);
621 p = strchr(username, '%');
623 if (p) {
624 *p = 0;
625 fstrcpy(pass, p + 1);
628 parse_wbinfo_domain_user(username, name_domain, name_user);
630 fstrcpy(request.data.auth_crap.user, name_user);
632 fstrcpy(request.data.auth_crap.domain,
633 name_domain);
635 generate_random_buffer(request.data.auth_crap.chal, 8);
637 if (lp_client_ntlmv2_auth()) {
638 DATA_BLOB server_chal;
639 DATA_BLOB names_blob;
641 DATA_BLOB lm_response;
642 DATA_BLOB nt_response;
644 server_chal = data_blob(request.data.auth_crap.chal, 8);
646 /* Pretend this is a login to 'us', for blob purposes */
647 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
649 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
650 &names_blob,
651 &lm_response, &nt_response, NULL)) {
652 data_blob_free(&names_blob);
653 data_blob_free(&server_chal);
654 return False;
656 data_blob_free(&names_blob);
657 data_blob_free(&server_chal);
659 memcpy(request.data.auth_crap.nt_resp, nt_response.data,
660 MIN(nt_response.length,
661 sizeof(request.data.auth_crap.nt_resp)));
662 request.data.auth_crap.nt_resp_len = nt_response.length;
664 memcpy(request.data.auth_crap.lm_resp, lm_response.data,
665 MIN(lm_response.length,
666 sizeof(request.data.auth_crap.lm_resp)));
667 request.data.auth_crap.lm_resp_len = lm_response.length;
669 data_blob_free(&nt_response);
670 data_blob_free(&lm_response);
672 } else {
673 if (lp_client_lanman_auth()
674 && SMBencrypt(pass, request.data.auth_crap.chal,
675 (uchar *)request.data.auth_crap.lm_resp)) {
676 request.data.auth_crap.lm_resp_len = 24;
677 } else {
678 request.data.auth_crap.lm_resp_len = 0;
680 SMBNTencrypt(pass, request.data.auth_crap.chal,
681 (uchar *)request.data.auth_crap.nt_resp);
683 request.data.auth_crap.nt_resp_len = 24;
686 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
688 /* Display response */
690 d_printf("challenge/response password authentication %s\n",
691 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
693 if (response.data.auth.nt_status)
694 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
695 response.data.auth.nt_status_string,
696 response.data.auth.nt_status,
697 response.data.auth.error_string);
699 return result == NSS_STATUS_SUCCESS;
702 /* Authenticate a user with a plaintext password and set a token */
704 static BOOL wbinfo_klog(char *username)
706 struct winbindd_request request;
707 struct winbindd_response response;
708 NSS_STATUS result;
709 char *p;
711 /* Send off request */
713 ZERO_STRUCT(request);
714 ZERO_STRUCT(response);
716 p = strchr(username, '%');
718 if (p) {
719 *p = 0;
720 fstrcpy(request.data.auth.user, username);
721 fstrcpy(request.data.auth.pass, p + 1);
722 *p = '%';
723 } else {
724 fstrcpy(request.data.auth.user, username);
725 fstrcpy(request.data.auth.pass, getpass("Password: "));
728 request.flags |= WBFLAG_PAM_AFS_TOKEN;
730 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
732 /* Display response */
734 d_printf("plaintext password authentication %s\n",
735 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
737 if (response.data.auth.nt_status)
738 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
739 response.data.auth.nt_status_string,
740 response.data.auth.nt_status,
741 response.data.auth.error_string);
743 if (result != NSS_STATUS_SUCCESS)
744 return False;
746 if (response.extra_data == NULL) {
747 d_printf("Did not get token data\n");
748 return False;
751 if (!afs_settoken_str((char *)response.extra_data)) {
752 d_printf("Could not set token\n");
753 return False;
756 d_printf("Successfully created AFS token\n");
757 return True;
760 /* Print domain users */
762 static BOOL print_domain_users(const char *domain)
764 struct winbindd_request request;
765 struct winbindd_response response;
766 const char *extra_data;
767 fstring name;
769 /* Send request to winbind daemon */
771 ZERO_STRUCT(request);
772 ZERO_STRUCT(response);
774 if (domain) {
775 /* '.' is the special sign for our own domwin */
776 if ( strequal(domain, ".") )
777 fstrcpy( request.domain_name, lp_workgroup() );
778 else
779 fstrcpy( request.domain_name, domain );
782 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
783 NSS_STATUS_SUCCESS)
784 return False;
786 /* Look through extra data */
788 if (!response.extra_data)
789 return False;
791 extra_data = (const char *)response.extra_data;
793 while(next_token(&extra_data, name, ",", sizeof(fstring)))
794 d_printf("%s\n", name);
796 SAFE_FREE(response.extra_data);
798 return True;
801 /* Print domain groups */
803 static BOOL print_domain_groups(const char *domain)
805 struct winbindd_request request;
806 struct winbindd_response response;
807 const char *extra_data;
808 fstring name;
810 ZERO_STRUCT(request);
811 ZERO_STRUCT(response);
813 if (domain) {
814 if ( strequal(domain, ".") )
815 fstrcpy( request.domain_name, lp_workgroup() );
816 else
817 fstrcpy( request.domain_name, domain );
820 if (winbindd_request_response(WINBINDD_LIST_GROUPS, &request, &response) !=
821 NSS_STATUS_SUCCESS)
822 return False;
824 /* Look through extra data */
826 if (!response.extra_data)
827 return False;
829 extra_data = (const char *)response.extra_data;
831 while(next_token(&extra_data, name, ",", sizeof(fstring)))
832 d_printf("%s\n", name);
834 SAFE_FREE(response.extra_data);
836 return True;
839 /* Set the authorised user for winbindd access in secrets.tdb */
841 static BOOL wbinfo_set_auth_user(char *username)
843 const char *password;
844 char *p;
845 fstring user, domain;
847 /* Separate into user and password */
849 parse_wbinfo_domain_user(username, domain, user);
851 p = strchr(user, '%');
853 if (p != NULL) {
854 *p = 0;
855 password = p+1;
856 } else {
857 char *thepass = getpass("Password: ");
858 if (thepass) {
859 password = thepass;
860 } else
861 password = "";
864 /* Store or remove DOMAIN\username%password in secrets.tdb */
866 secrets_init();
868 if (user[0]) {
870 if (!secrets_store(SECRETS_AUTH_USER, user,
871 strlen(user) + 1)) {
872 d_fprintf(stderr, "error storing username\n");
873 return False;
876 /* We always have a domain name added by the
877 parse_wbinfo_domain_user() function. */
879 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
880 strlen(domain) + 1)) {
881 d_fprintf(stderr, "error storing domain name\n");
882 return False;
885 } else {
886 secrets_delete(SECRETS_AUTH_USER);
887 secrets_delete(SECRETS_AUTH_DOMAIN);
890 if (password[0]) {
892 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
893 strlen(password) + 1)) {
894 d_fprintf(stderr, "error storing password\n");
895 return False;
898 } else
899 secrets_delete(SECRETS_AUTH_PASSWORD);
901 return True;
904 static void wbinfo_get_auth_user(void)
906 char *user, *domain, *password;
908 /* Lift data from secrets file */
910 secrets_fetch_ipc_userpass(&user, &domain, &password);
912 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
914 SAFE_FREE(user);
915 SAFE_FREE(domain);
916 SAFE_FREE(password);
917 d_printf("No authorised user configured\n");
918 return;
921 /* Pretty print authorised user info */
923 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
924 user, password ? "%" : "", password ? password : "");
926 SAFE_FREE(user);
927 SAFE_FREE(domain);
928 SAFE_FREE(password);
931 static BOOL wbinfo_ping(void)
933 NSS_STATUS result;
935 result = winbindd_request_response(WINBINDD_PING, NULL, NULL);
937 /* Display response */
939 d_printf("Ping to winbindd %s on fd %d\n",
940 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
942 return result == NSS_STATUS_SUCCESS;
945 /* Main program */
947 enum {
948 OPT_SET_AUTH_USER = 1000,
949 OPT_GET_AUTH_USER,
950 OPT_DOMAIN_NAME,
951 OPT_SEQUENCE,
952 OPT_GETDCNAME,
953 OPT_USERDOMGROUPS,
954 OPT_USERSIDS
957 int main(int argc, char **argv)
959 int opt;
961 poptContext pc;
962 static char *string_arg;
963 static char *opt_domain_name;
964 static int int_arg;
965 int result = 1;
967 struct poptOption long_options[] = {
968 POPT_AUTOHELP
970 /* longName, shortName, argInfo, argPtr, value, descrip,
971 argDesc */
973 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
974 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
975 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
976 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
977 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
978 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
979 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
980 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
981 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
982 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
983 { "allocate-rid", 'A', POPT_ARG_NONE, 0, 'A', "Get a new RID out of idmap" },
984 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
985 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
986 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
987 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
988 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
989 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
990 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
991 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
992 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
993 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
994 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
995 "Get a DC name for a foreign domain", "domainname" },
996 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
997 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
998 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
999 #ifdef WITH_FAKE_KASERVER
1000 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1001 #endif
1002 POPT_COMMON_VERSION
1003 POPT_TABLEEND
1006 /* Samba client initialisation */
1008 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
1009 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1010 dyn_CONFIGFILE, strerror(errno));
1011 exit(1);
1014 if (!init_names())
1015 return 1;
1017 load_interfaces();
1019 /* Parse options */
1021 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1023 /* Parse command line options */
1025 if (argc == 1) {
1026 poptPrintHelp(pc, stderr, 0);
1027 return 1;
1030 while((opt = poptGetNextOpt(pc)) != -1) {
1031 /* get the generic configuration parameters like --domain */
1034 poptFreeContext(pc);
1036 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1037 POPT_CONTEXT_KEEP_FIRST);
1039 while((opt = poptGetNextOpt(pc)) != -1) {
1040 switch (opt) {
1041 case 'u':
1042 if (!print_domain_users(opt_domain_name)) {
1043 d_printf("Error looking up domain users\n");
1044 goto done;
1046 break;
1047 case 'g':
1048 if (!print_domain_groups(opt_domain_name)) {
1049 d_printf("Error looking up domain groups\n");
1050 goto done;
1052 break;
1053 case 's':
1054 if (!wbinfo_lookupsid(string_arg)) {
1055 d_printf("Could not lookup sid %s\n", string_arg);
1056 goto done;
1058 break;
1059 case 'n':
1060 if (!wbinfo_lookupname(string_arg)) {
1061 d_printf("Could not lookup name %s\n", string_arg);
1062 goto done;
1064 break;
1065 case 'N':
1066 if (!wbinfo_wins_byname(string_arg)) {
1067 d_printf("Could not lookup WINS by name %s\n", string_arg);
1068 goto done;
1070 break;
1071 case 'I':
1072 if (!wbinfo_wins_byip(string_arg)) {
1073 d_printf("Could not lookup WINS by IP %s\n", string_arg);
1074 goto done;
1076 break;
1077 case 'U':
1078 if (!wbinfo_uid_to_sid(int_arg)) {
1079 d_printf("Could not convert uid %d to sid\n", int_arg);
1080 goto done;
1082 break;
1083 case 'G':
1084 if (!wbinfo_gid_to_sid(int_arg)) {
1085 d_printf("Could not convert gid %d to sid\n",
1086 int_arg);
1087 goto done;
1089 break;
1090 case 'S':
1091 if (!wbinfo_sid_to_uid(string_arg)) {
1092 d_printf("Could not convert sid %s to uid\n",
1093 string_arg);
1094 goto done;
1096 break;
1097 case 'Y':
1098 if (!wbinfo_sid_to_gid(string_arg)) {
1099 d_printf("Could not convert sid %s to gid\n",
1100 string_arg);
1101 goto done;
1103 break;
1104 case 'A':
1105 if (!wbinfo_allocate_rid()) {
1106 d_printf("Could not allocate a RID\n");
1107 goto done;
1109 break;
1110 case 't':
1111 if (!wbinfo_check_secret()) {
1112 d_printf("Could not check secret\n");
1113 goto done;
1115 break;
1116 case 'm':
1117 if (!wbinfo_list_domains()) {
1118 d_printf("Could not list trusted domains\n");
1119 goto done;
1121 break;
1122 case OPT_SEQUENCE:
1123 if (!wbinfo_show_sequence(opt_domain_name)) {
1124 d_printf("Could not show sequence numbers\n");
1125 goto done;
1127 break;
1128 case 'D':
1129 if (!wbinfo_domain_info(string_arg)) {
1130 d_printf("Could not get domain info\n");
1131 goto done;
1133 break;
1134 case 'r':
1135 if (!wbinfo_get_usergroups(string_arg)) {
1136 d_printf("Could not get groups for user %s\n",
1137 string_arg);
1138 goto done;
1140 break;
1141 case OPT_USERSIDS:
1142 if (!wbinfo_get_usersids(string_arg)) {
1143 d_printf("Could not get group SIDs for user SID %s\n",
1144 string_arg);
1145 goto done;
1147 break;
1148 case OPT_USERDOMGROUPS:
1149 if (!wbinfo_get_userdomgroups(string_arg)) {
1150 d_printf("Could not get user's domain groups "
1151 "for user SID %s\n", string_arg);
1152 goto done;
1154 break;
1155 case 'a': {
1156 BOOL got_error = False;
1158 if (!wbinfo_auth(string_arg)) {
1159 d_printf("Could not authenticate user %s with "
1160 "plaintext password\n", string_arg);
1161 got_error = True;
1164 if (!wbinfo_auth_crap(string_arg)) {
1165 d_printf("Could not authenticate user %s with "
1166 "challenge/response\n", string_arg);
1167 got_error = True;
1170 if (got_error)
1171 goto done;
1172 break;
1174 case 'k':
1175 if (!wbinfo_klog(string_arg)) {
1176 d_printf("Could not klog user\n");
1177 goto done;
1179 break;
1180 case 'p':
1181 if (!wbinfo_ping()) {
1182 d_printf("could not ping winbindd!\n");
1183 goto done;
1185 break;
1186 case OPT_SET_AUTH_USER:
1187 wbinfo_set_auth_user(string_arg);
1188 break;
1189 case OPT_GET_AUTH_USER:
1190 wbinfo_get_auth_user();
1191 break;
1192 case OPT_GETDCNAME:
1193 wbinfo_getdcname(string_arg);
1194 break;
1195 /* generic configuration options */
1196 case OPT_DOMAIN_NAME:
1197 break;
1198 default:
1199 d_fprintf(stderr, "Invalid option\n");
1200 poptPrintHelp(pc, stderr, 0);
1201 goto done;
1205 result = 0;
1207 /* Exit code */
1209 done:
1210 poptFreeContext(pc);
1211 return result;