Commit the translation of the realm to the netbios domain name in the kerberos
[Samba/gebeck_regimport.git] / source3 / nsswitch / wbinfo.c
blobbca1813167b979312bada9f562e8d01fb15cc2b2
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;
139 /* Convert NetBIOS name to IP */
141 static BOOL wbinfo_wins_byname(char *name)
143 struct winbindd_request request;
144 struct winbindd_response response;
146 ZERO_STRUCT(request);
147 ZERO_STRUCT(response);
149 /* Send request */
151 fstrcpy(request.data.winsreq, name);
153 if (winbindd_request(WINBINDD_WINS_BYNAME, &request, &response) !=
154 NSS_STATUS_SUCCESS) {
155 return False;
158 /* Display response */
160 printf("%s\n", response.data.winsresp);
162 return True;
165 /* Convert IP to NetBIOS name */
167 static BOOL wbinfo_wins_byip(char *ip)
169 struct winbindd_request request;
170 struct winbindd_response response;
172 ZERO_STRUCT(request);
173 ZERO_STRUCT(response);
175 /* Send request */
177 fstrcpy(request.data.winsreq, ip);
179 if (winbindd_request(WINBINDD_WINS_BYIP, &request, &response) !=
180 NSS_STATUS_SUCCESS) {
181 return False;
184 /* Display response */
186 printf("%s\n", response.data.winsresp);
188 return True;
191 /* List trusted domains */
193 static BOOL wbinfo_list_domains(void)
195 struct winbindd_response response;
196 fstring name;
198 ZERO_STRUCT(response);
200 /* Send request */
202 if (winbindd_request(WINBINDD_LIST_TRUSTDOM, NULL, &response) !=
203 NSS_STATUS_SUCCESS)
204 return False;
206 /* Display response */
208 if (response.extra_data) {
209 const char *extra_data = (char *)response.extra_data;
211 while(next_token(&extra_data, name, ",", sizeof(fstring)))
212 d_printf("%s\n", name);
214 SAFE_FREE(response.extra_data);
217 return True;
221 /* show sequence numbers */
222 static BOOL wbinfo_show_sequence(const char *domain)
224 struct winbindd_request request;
225 struct winbindd_response response;
227 ZERO_STRUCT(response);
228 ZERO_STRUCT(request);
230 if ( domain )
231 fstrcpy( request.domain_name, domain );
233 /* Send request */
235 if (winbindd_request(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
236 NSS_STATUS_SUCCESS)
237 return False;
239 /* Display response */
241 if (response.extra_data) {
242 char *extra_data = (char *)response.extra_data;
243 d_printf("%s", extra_data);
244 SAFE_FREE(response.extra_data);
247 return True;
250 /* Show domain info */
252 static BOOL wbinfo_domain_info(const char *domain_name)
254 struct winbindd_request request;
255 struct winbindd_response response;
257 ZERO_STRUCT(request);
258 ZERO_STRUCT(response);
260 fstrcpy(request.domain_name, domain_name);
262 /* Send request */
264 if (winbindd_request(WINBINDD_DOMAIN_INFO, &request, &response) !=
265 NSS_STATUS_SUCCESS)
266 return False;
268 /* Display response */
270 d_printf("Name : %s\n", response.data.domain_info.name);
271 d_printf("Alt_Name: %s\n", response.data.domain_info.alt_name);
273 d_printf("SID : %s\n", response.data.domain_info.sid);
275 d_printf("Native : %s\n",
276 response.data.domain_info.native_mode ? "Yes" : "No");
278 d_printf("Primary : %s\n",
279 response.data.domain_info.primary ? "Yes" : "No");
281 d_printf("Sequence: %d\n", response.data.domain_info.sequence_number);
283 return True;
286 /* Check trust account password */
288 static BOOL wbinfo_check_secret(void)
290 struct winbindd_response response;
291 NSS_STATUS result;
293 ZERO_STRUCT(response);
295 result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response);
297 d_printf("checking the trust secret via RPC calls %s\n",
298 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
300 if (result != NSS_STATUS_SUCCESS)
301 d_printf("error code was %s (0x%x)\n",
302 response.data.auth.nt_status_string,
303 response.data.auth.nt_status);
305 return result == NSS_STATUS_SUCCESS;
308 /* Convert uid to sid */
310 static BOOL wbinfo_uid_to_sid(uid_t uid)
312 struct winbindd_request request;
313 struct winbindd_response response;
315 ZERO_STRUCT(request);
316 ZERO_STRUCT(response);
318 /* Send request */
320 request.data.uid = uid;
322 if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
323 NSS_STATUS_SUCCESS)
324 return False;
326 /* Display response */
328 d_printf("%s\n", response.data.sid.sid);
330 return True;
333 /* Convert gid to sid */
335 static BOOL wbinfo_gid_to_sid(gid_t gid)
337 struct winbindd_request request;
338 struct winbindd_response response;
340 ZERO_STRUCT(request);
341 ZERO_STRUCT(response);
343 /* Send request */
345 request.data.gid = gid;
347 if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) !=
348 NSS_STATUS_SUCCESS)
349 return False;
351 /* Display response */
353 d_printf("%s\n", response.data.sid.sid);
355 return True;
358 /* Convert sid to uid */
360 static BOOL wbinfo_sid_to_uid(char *sid)
362 struct winbindd_request request;
363 struct winbindd_response response;
365 ZERO_STRUCT(request);
366 ZERO_STRUCT(response);
368 /* Send request */
370 fstrcpy(request.data.sid, sid);
372 if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
373 NSS_STATUS_SUCCESS)
374 return False;
376 /* Display response */
378 d_printf("%d\n", (int)response.data.uid);
380 return True;
383 static BOOL wbinfo_sid_to_gid(char *sid)
385 struct winbindd_request request;
386 struct winbindd_response response;
388 ZERO_STRUCT(request);
389 ZERO_STRUCT(response);
391 /* Send request */
393 fstrcpy(request.data.sid, sid);
395 if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
396 NSS_STATUS_SUCCESS)
397 return False;
399 /* Display response */
401 d_printf("%d\n", (int)response.data.gid);
403 return True;
406 /* Convert sid to string */
408 static BOOL wbinfo_lookupsid(char *sid)
410 struct winbindd_request request;
411 struct winbindd_response response;
413 ZERO_STRUCT(request);
414 ZERO_STRUCT(response);
416 /* Send off request */
418 fstrcpy(request.data.sid, sid);
420 if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
421 NSS_STATUS_SUCCESS)
422 return False;
424 /* Display response */
426 d_printf("%s%c%s %d\n", response.data.name.dom_name,
427 winbind_separator(), response.data.name.name,
428 response.data.name.type);
430 return True;
433 /* Convert string to sid */
435 static BOOL wbinfo_lookupname(char *name)
437 struct winbindd_request request;
438 struct winbindd_response response;
440 /* Send off request */
442 ZERO_STRUCT(request);
443 ZERO_STRUCT(response);
445 parse_wbinfo_domain_user(name, request.data.name.dom_name,
446 request.data.name.name);
448 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
449 NSS_STATUS_SUCCESS)
450 return False;
452 /* Display response */
454 d_printf("%s %d\n", response.data.sid.sid, response.data.sid.type);
456 return True;
459 /* Authenticate a user with a plaintext password */
461 static BOOL wbinfo_auth(char *username)
463 struct winbindd_request request;
464 struct winbindd_response response;
465 NSS_STATUS result;
466 char *p;
468 /* Send off request */
470 ZERO_STRUCT(request);
471 ZERO_STRUCT(response);
473 p = strchr(username, '%');
475 if (p) {
476 *p = 0;
477 fstrcpy(request.data.auth.user, username);
478 fstrcpy(request.data.auth.pass, p + 1);
479 *p = '%';
480 } else
481 fstrcpy(request.data.auth.user, username);
483 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
485 /* Display response */
487 d_printf("plaintext password authentication %s\n",
488 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
490 if (response.data.auth.nt_status)
491 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
492 response.data.auth.nt_status_string,
493 response.data.auth.nt_status,
494 response.data.auth.error_string);
496 return result == NSS_STATUS_SUCCESS;
499 /* Authenticate a user with a challenge/response */
501 static BOOL wbinfo_auth_crap(char *username)
503 struct winbindd_request request;
504 struct winbindd_response response;
505 NSS_STATUS result;
506 fstring name_user;
507 fstring name_domain;
508 fstring pass;
509 char *p;
511 /* Send off request */
513 ZERO_STRUCT(request);
514 ZERO_STRUCT(response);
516 p = strchr(username, '%');
518 if (p) {
519 *p = 0;
520 fstrcpy(pass, p + 1);
523 parse_wbinfo_domain_user(username, name_domain, name_user);
525 if (push_utf8_fstring(request.data.auth_crap.user, name_user) == -1) {
526 d_printf("unable to create utf8 string for '%s'\n",
527 name_user);
528 return False;
531 if (push_utf8_fstring(request.data.auth_crap.domain,
532 name_domain) == -1) {
533 d_printf("unable to create utf8 string for '%s'\n",
534 name_domain);
535 return False;
538 generate_random_buffer(request.data.auth_crap.chal, 8, False);
540 SMBencrypt(pass, request.data.auth_crap.chal,
541 (uchar *)request.data.auth_crap.lm_resp);
542 SMBNTencrypt(pass, request.data.auth_crap.chal,
543 (uchar *)request.data.auth_crap.nt_resp);
545 request.data.auth_crap.lm_resp_len = 24;
546 request.data.auth_crap.nt_resp_len = 24;
548 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
550 /* Display response */
552 d_printf("challenge/response password authentication %s\n",
553 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
555 if (response.data.auth.nt_status)
556 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
557 response.data.auth.nt_status_string,
558 response.data.auth.nt_status,
559 response.data.auth.error_string);
561 return result == NSS_STATUS_SUCCESS;
564 /******************************************************************
565 create a winbindd user
566 ******************************************************************/
568 static BOOL wbinfo_create_user(char *username)
570 struct winbindd_request request;
571 struct winbindd_response response;
572 NSS_STATUS result;
574 /* Send off request */
576 ZERO_STRUCT(request);
577 ZERO_STRUCT(response);
579 request.flags = WBFLAG_ALLOCATE_RID;
580 fstrcpy(request.data.acct_mgt.username, username);
582 result = winbindd_request(WINBINDD_CREATE_USER, &request, &response);
584 if ( result == NSS_STATUS_SUCCESS )
585 d_printf("New RID is %d\n", response.data.rid);
587 return result == NSS_STATUS_SUCCESS;
590 /******************************************************************
591 remove a winbindd user
592 ******************************************************************/
594 static BOOL wbinfo_delete_user(char *username)
596 struct winbindd_request request;
597 struct winbindd_response response;
598 NSS_STATUS result;
600 /* Send off request */
602 ZERO_STRUCT(request);
603 ZERO_STRUCT(response);
605 fstrcpy(request.data.acct_mgt.username, username);
607 result = winbindd_request(WINBINDD_DELETE_USER, &request, &response);
609 return result == NSS_STATUS_SUCCESS;
612 /******************************************************************
613 create a winbindd group
614 ******************************************************************/
616 static BOOL wbinfo_create_group(char *groupname)
618 struct winbindd_request request;
619 struct winbindd_response response;
620 NSS_STATUS result;
622 /* Send off request */
624 ZERO_STRUCT(request);
625 ZERO_STRUCT(response);
627 fstrcpy(request.data.acct_mgt.groupname, groupname);
629 result = winbindd_request(WINBINDD_CREATE_GROUP, &request, &response);
631 return result == NSS_STATUS_SUCCESS;
634 /******************************************************************
635 remove a winbindd group
636 ******************************************************************/
638 static BOOL wbinfo_delete_group(char *groupname)
640 struct winbindd_request request;
641 struct winbindd_response response;
642 NSS_STATUS result;
644 /* Send off request */
646 ZERO_STRUCT(request);
647 ZERO_STRUCT(response);
649 fstrcpy(request.data.acct_mgt.groupname, groupname);
651 result = winbindd_request(WINBINDD_DELETE_GROUP, &request, &response);
653 return result == NSS_STATUS_SUCCESS;
656 /******************************************************************
657 parse a string in the form user:group
658 ******************************************************************/
660 static BOOL parse_user_group( const char *string, fstring user, fstring group )
662 char *p;
664 if ( !string )
665 return False;
667 if ( !(p = strchr( string, ':' )) )
668 return False;
670 *p = '\0';
671 p++;
673 fstrcpy( user, string );
674 fstrcpy( group, p );
676 return True;
679 /******************************************************************
680 add a user to a winbindd group
681 ******************************************************************/
683 static BOOL wbinfo_add_user_to_group(char *string)
685 struct winbindd_request request;
686 struct winbindd_response response;
687 NSS_STATUS result;
689 /* Send off request */
691 ZERO_STRUCT(request);
692 ZERO_STRUCT(response);
694 if ( !parse_user_group( string, request.data.acct_mgt.username,
695 request.data.acct_mgt.groupname))
697 d_printf("Can't parse user:group from %s\n", string);
698 return False;
701 result = winbindd_request(WINBINDD_ADD_USER_TO_GROUP, &request, &response);
703 return result == NSS_STATUS_SUCCESS;
706 /******************************************************************
707 remove a user from a winbindd group
708 ******************************************************************/
710 static BOOL wbinfo_remove_user_from_group(char *string)
712 struct winbindd_request request;
713 struct winbindd_response response;
714 NSS_STATUS result;
716 /* Send off request */
718 ZERO_STRUCT(request);
719 ZERO_STRUCT(response);
721 if ( !parse_user_group( string, request.data.acct_mgt.username,
722 request.data.acct_mgt.groupname))
724 d_printf("Can't parse user:group from %s\n", string);
725 return False;
728 result = winbindd_request(WINBINDD_REMOVE_USER_FROM_GROUP, &request, &response);
730 return result == NSS_STATUS_SUCCESS;
733 /* Print domain users */
735 static BOOL print_domain_users(const char *domain)
737 struct winbindd_request request;
738 struct winbindd_response response;
739 const char *extra_data;
740 fstring name;
742 /* Send request to winbind daemon */
744 ZERO_STRUCT(request);
745 ZERO_STRUCT(response);
747 if (domain) {
748 /* '.' is the special sign for our own domwin */
749 if ( strequal(domain, ".") )
750 fstrcpy( request.domain_name, lp_workgroup() );
751 else
752 fstrcpy( request.domain_name, domain );
755 if (winbindd_request(WINBINDD_LIST_USERS, &request, &response) !=
756 NSS_STATUS_SUCCESS)
757 return False;
759 /* Look through extra data */
761 if (!response.extra_data)
762 return False;
764 extra_data = (const char *)response.extra_data;
766 while(next_token(&extra_data, name, ",", sizeof(fstring)))
767 d_printf("%s\n", name);
769 SAFE_FREE(response.extra_data);
771 return True;
774 /* Print domain groups */
776 static BOOL print_domain_groups(const char *domain)
778 struct winbindd_request request;
779 struct winbindd_response response;
780 const char *extra_data;
781 fstring name;
783 ZERO_STRUCT(request);
784 ZERO_STRUCT(response);
786 if (domain) {
787 if ( strequal(domain, ".") )
788 fstrcpy( request.domain_name, lp_workgroup() );
789 else
790 fstrcpy( request.domain_name, domain );
793 if (winbindd_request(WINBINDD_LIST_GROUPS, &request, &response) !=
794 NSS_STATUS_SUCCESS)
795 return False;
797 /* Look through extra data */
799 if (!response.extra_data)
800 return False;
802 extra_data = (const char *)response.extra_data;
804 while(next_token(&extra_data, name, ",", sizeof(fstring)))
805 d_printf("%s\n", name);
807 SAFE_FREE(response.extra_data);
809 return True;
812 /* Set the authorised user for winbindd access in secrets.tdb */
814 static BOOL wbinfo_set_auth_user(char *username)
816 char *password;
817 fstring user, domain;
819 /* Separate into user and password */
821 parse_wbinfo_domain_user(username, domain, user);
823 password = strchr(user, '%');
825 if (password) {
826 *password = 0;
827 password++;
828 } else {
829 char *thepass = getpass("Password: ");
830 if (thepass) {
831 password = thepass;
832 } else
833 password = "";
836 /* Store or remove DOMAIN\username%password in secrets.tdb */
838 secrets_init();
840 if (user[0]) {
842 if (!secrets_store(SECRETS_AUTH_USER, user,
843 strlen(user) + 1)) {
844 d_fprintf(stderr, "error storing username\n");
845 return False;
848 /* We always have a domain name added by the
849 parse_wbinfo_domain_user() function. */
851 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
852 strlen(domain) + 1)) {
853 d_fprintf(stderr, "error storing domain name\n");
854 return False;
857 } else {
858 secrets_delete(SECRETS_AUTH_USER);
859 secrets_delete(SECRETS_AUTH_DOMAIN);
862 if (password[0]) {
864 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
865 strlen(password) + 1)) {
866 d_fprintf(stderr, "error storing password\n");
867 return False;
870 } else
871 secrets_delete(SECRETS_AUTH_PASSWORD);
873 return True;
876 static void wbinfo_get_auth_user(void)
878 char *user, *domain, *password;
880 /* Lift data from secrets file */
882 secrets_init();
884 user = secrets_fetch(SECRETS_AUTH_USER, NULL);
885 domain = secrets_fetch(SECRETS_AUTH_DOMAIN, NULL);
886 password = secrets_fetch(SECRETS_AUTH_PASSWORD, NULL);
888 if (!user && !domain && !password) {
889 d_printf("No authorised user configured\n");
890 return;
893 /* Pretty print authorised user info */
895 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? "\\" : "",
896 user, password ? "%" : "", password ? password : "");
898 SAFE_FREE(user);
899 SAFE_FREE(domain);
900 SAFE_FREE(password);
903 static BOOL wbinfo_ping(void)
905 NSS_STATUS result;
907 result = winbindd_request(WINBINDD_PING, NULL, NULL);
909 /* Display response */
911 d_printf("Ping to winbindd %s on fd %d\n",
912 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
914 return result == NSS_STATUS_SUCCESS;
917 /* Main program */
919 enum {
920 OPT_SET_AUTH_USER = 1000,
921 OPT_GET_AUTH_USER,
922 OPT_DOMAIN_NAME,
923 OPT_SEQUENCE
926 int main(int argc, char **argv)
928 int opt;
930 poptContext pc;
931 static char *string_arg;
932 static char *opt_domain_name;
933 static int int_arg;
934 int result = 1;
936 struct poptOption long_options[] = {
937 POPT_AUTOHELP
939 /* longName, shortName, argInfo, argPtr, value, descrip,
940 argDesc */
942 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
943 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
944 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
945 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
946 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
947 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
948 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
949 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
950 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
951 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
952 { "create-user", 'c', POPT_ARG_STRING, &string_arg, 'c', "Create a local user account", "name" },
953 { "delete-user", 'x', POPT_ARG_STRING, &string_arg, 'x', "Delete a local user account", "name" },
954 { "create-group", 'C', POPT_ARG_STRING, &string_arg, 'C', "Create a local group", "name" },
955 { "delete-group", 'X', POPT_ARG_STRING, &string_arg, 'X', "Delete a local group", "name" },
956 { "add-to-group", 'o', POPT_ARG_STRING, &string_arg, 'o', "Add user to group", "user:group" },
957 { "del-from-group", 'O', POPT_ARG_STRING, &string_arg, 'O', "Remove user from group", "user:group" },
958 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
959 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
960 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
961 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D',
962 "Show all most info we have about the domain" },
963 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
964 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
965 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
966 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
967 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
968 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operatio", "domain" },
969 POPT_COMMON_VERSION
970 POPT_TABLEEND
973 /* Samba client initialisation */
975 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
976 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
977 dyn_CONFIGFILE, strerror(errno));
978 exit(1);
981 if (!init_names())
982 return 1;
984 load_interfaces();
986 /* Parse options */
988 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
990 /* Parse command line options */
992 if (argc == 1) {
993 poptPrintHelp(pc, stderr, 0);
994 return 1;
997 while((opt = poptGetNextOpt(pc)) != -1) {
998 /* get the generic configuration parameters like --domain */
1001 poptFreeContext(pc);
1003 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1004 POPT_CONTEXT_KEEP_FIRST);
1006 while((opt = poptGetNextOpt(pc)) != -1) {
1007 switch (opt) {
1008 case 'u':
1009 if (!print_domain_users(opt_domain_name)) {
1010 d_printf("Error looking up domain users\n");
1011 goto done;
1013 break;
1014 case 'g':
1015 if (!print_domain_groups(opt_domain_name)) {
1016 d_printf("Error looking up domain groups\n");
1017 goto done;
1019 break;
1020 case 's':
1021 if (!wbinfo_lookupsid(string_arg)) {
1022 d_printf("Could not lookup sid %s\n", string_arg);
1023 goto done;
1025 break;
1026 case 'n':
1027 if (!wbinfo_lookupname(string_arg)) {
1028 d_printf("Could not lookup name %s\n", string_arg);
1029 goto done;
1031 break;
1032 case 'N':
1033 if (!wbinfo_wins_byname(string_arg)) {
1034 d_printf("Could not lookup WINS by name %s\n", string_arg);
1035 goto done;
1037 break;
1038 case 'I':
1039 if (!wbinfo_wins_byip(string_arg)) {
1040 d_printf("Could not lookup WINS by IP %s\n", string_arg);
1041 goto done;
1043 break;
1044 case 'U':
1045 if (!wbinfo_uid_to_sid(int_arg)) {
1046 d_printf("Could not convert uid %d to sid\n", int_arg);
1047 goto done;
1049 break;
1050 case 'G':
1051 if (!wbinfo_gid_to_sid(int_arg)) {
1052 d_printf("Could not convert gid %d to sid\n",
1053 int_arg);
1054 goto done;
1056 break;
1057 case 'S':
1058 if (!wbinfo_sid_to_uid(string_arg)) {
1059 d_printf("Could not convert sid %s to uid\n",
1060 string_arg);
1061 goto done;
1063 break;
1064 case 'Y':
1065 if (!wbinfo_sid_to_gid(string_arg)) {
1066 d_printf("Could not convert sid %s to gid\n",
1067 string_arg);
1068 goto done;
1070 break;
1071 case 't':
1072 if (!wbinfo_check_secret()) {
1073 d_printf("Could not check secret\n");
1074 goto done;
1076 break;
1077 case 'm':
1078 if (!wbinfo_list_domains()) {
1079 d_printf("Could not list trusted domains\n");
1080 goto done;
1082 break;
1083 case OPT_SEQUENCE:
1084 if (!wbinfo_show_sequence(opt_domain_name)) {
1085 d_printf("Could not show sequence numbers\n");
1086 goto done;
1088 break;
1089 case 'D':
1090 if (!wbinfo_domain_info(string_arg)) {
1091 d_printf("Could not get domain info\n");
1092 goto done;
1094 break;
1095 case 'r':
1096 if (!wbinfo_get_usergroups(string_arg)) {
1097 d_printf("Could not get groups for user %s\n",
1098 string_arg);
1099 goto done;
1101 break;
1102 case 'a': {
1103 BOOL got_error = False;
1105 if (!wbinfo_auth(string_arg)) {
1106 d_printf("Could not authenticate user %s with "
1107 "plaintext password\n", string_arg);
1108 got_error = True;
1111 if (!wbinfo_auth_crap(string_arg)) {
1112 d_printf("Could not authenticate user %s with "
1113 "challenge/response\n", string_arg);
1114 got_error = True;
1117 if (got_error)
1118 goto done;
1119 break;
1121 case 'c':
1122 if ( !wbinfo_create_user(string_arg) ) {
1123 d_printf("Could not create user account\n");
1124 goto done;
1126 break;
1127 case 'C':
1128 if ( !wbinfo_create_group(string_arg) ) {
1129 d_printf("Could not create group\n");
1130 goto done;
1132 break;
1133 case 'o':
1134 if ( !wbinfo_add_user_to_group(string_arg) ) {
1135 d_printf("Could not add user to group\n");
1136 goto done;
1138 break;
1139 case 'O':
1140 if ( !wbinfo_remove_user_from_group(string_arg) ) {
1141 d_printf("Could not remove user kfrom group\n");
1142 goto done;
1144 break;
1145 case 'x':
1146 if ( !wbinfo_delete_user(string_arg) ) {
1147 d_printf("Could not delete user account\n");
1148 goto done;
1150 break;
1151 case 'X':
1152 if ( !wbinfo_delete_group(string_arg) ) {
1153 d_printf("Could not delete group\n");
1154 goto done;
1156 break;
1157 case 'p':
1158 if (!wbinfo_ping()) {
1159 d_printf("could not ping winbindd!\n");
1160 goto done;
1162 break;
1163 case OPT_SET_AUTH_USER:
1164 wbinfo_set_auth_user(string_arg);
1165 break;
1166 case OPT_GET_AUTH_USER:
1167 wbinfo_get_auth_user();
1168 break;
1169 /* generic configuration options */
1170 case OPT_DOMAIN_NAME:
1171 break;
1172 default:
1173 d_fprintf(stderr, "Invalid option\n");
1174 poptPrintHelp(pc, stderr, 0);
1175 goto done;
1179 result = 0;
1181 /* Exit code */
1183 done:
1184 poptFreeContext(pc);
1185 return result;