Remove some unused variables uncovered by the build farm.
[Samba/gebeck_regimport.git] / source3 / nsswitch / wbinfo.c
blob0018e99f60f83e017f227d12eaa8dd45cc7d8910
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 /* Check trust account password */
252 static BOOL wbinfo_check_secret(void)
254 struct winbindd_response response;
255 NSS_STATUS result;
257 ZERO_STRUCT(response);
259 result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response);
261 d_printf("checking the trust secret via RPC calls %s\n",
262 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
264 if (result != NSS_STATUS_SUCCESS)
265 d_printf("error code was %s (0x%x)\n",
266 response.data.auth.nt_status_string,
267 response.data.auth.nt_status);
269 return result == NSS_STATUS_SUCCESS;
272 /* Convert uid to sid */
274 static BOOL wbinfo_uid_to_sid(uid_t uid)
276 struct winbindd_request request;
277 struct winbindd_response response;
279 ZERO_STRUCT(request);
280 ZERO_STRUCT(response);
282 /* Send request */
284 request.data.uid = uid;
286 if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
287 NSS_STATUS_SUCCESS)
288 return False;
290 /* Display response */
292 d_printf("%s\n", response.data.sid.sid);
294 return True;
297 /* Convert gid to sid */
299 static BOOL wbinfo_gid_to_sid(gid_t gid)
301 struct winbindd_request request;
302 struct winbindd_response response;
304 ZERO_STRUCT(request);
305 ZERO_STRUCT(response);
307 /* Send request */
309 request.data.gid = gid;
311 if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) !=
312 NSS_STATUS_SUCCESS)
313 return False;
315 /* Display response */
317 d_printf("%s\n", response.data.sid.sid);
319 return True;
322 /* Convert sid to uid */
324 static BOOL wbinfo_sid_to_uid(char *sid)
326 struct winbindd_request request;
327 struct winbindd_response response;
329 ZERO_STRUCT(request);
330 ZERO_STRUCT(response);
332 /* Send request */
334 fstrcpy(request.data.sid, sid);
336 if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
337 NSS_STATUS_SUCCESS)
338 return False;
340 /* Display response */
342 d_printf("%d\n", (int)response.data.uid);
344 return True;
347 static BOOL wbinfo_sid_to_gid(char *sid)
349 struct winbindd_request request;
350 struct winbindd_response response;
352 ZERO_STRUCT(request);
353 ZERO_STRUCT(response);
355 /* Send request */
357 fstrcpy(request.data.sid, sid);
359 if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
360 NSS_STATUS_SUCCESS)
361 return False;
363 /* Display response */
365 d_printf("%d\n", (int)response.data.gid);
367 return True;
370 /* Convert sid to string */
372 static BOOL wbinfo_lookupsid(char *sid)
374 struct winbindd_request request;
375 struct winbindd_response response;
377 ZERO_STRUCT(request);
378 ZERO_STRUCT(response);
380 /* Send off request */
382 fstrcpy(request.data.sid, sid);
384 if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
385 NSS_STATUS_SUCCESS)
386 return False;
388 /* Display response */
390 d_printf("%s%c%s %d\n", response.data.name.dom_name,
391 winbind_separator(), response.data.name.name,
392 response.data.name.type);
394 return True;
397 /* Convert string to sid */
399 static BOOL wbinfo_lookupname(char *name)
401 struct winbindd_request request;
402 struct winbindd_response response;
404 /* Send off request */
406 ZERO_STRUCT(request);
407 ZERO_STRUCT(response);
409 parse_wbinfo_domain_user(name, request.data.name.dom_name,
410 request.data.name.name);
412 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
413 NSS_STATUS_SUCCESS)
414 return False;
416 /* Display response */
418 d_printf("%s %d\n", response.data.sid.sid, response.data.sid.type);
420 return True;
423 /* Authenticate a user with a plaintext password */
425 static BOOL wbinfo_auth(char *username)
427 struct winbindd_request request;
428 struct winbindd_response response;
429 NSS_STATUS result;
430 char *p;
432 /* Send off request */
434 ZERO_STRUCT(request);
435 ZERO_STRUCT(response);
437 p = strchr(username, '%');
439 if (p) {
440 *p = 0;
441 fstrcpy(request.data.auth.user, username);
442 fstrcpy(request.data.auth.pass, p + 1);
443 *p = '%';
444 } else
445 fstrcpy(request.data.auth.user, username);
447 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
449 /* Display response */
451 d_printf("plaintext password authentication %s\n",
452 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
454 if (response.data.auth.nt_status)
455 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
456 response.data.auth.nt_status_string,
457 response.data.auth.nt_status,
458 response.data.auth.error_string);
460 return result == NSS_STATUS_SUCCESS;
463 /* Authenticate a user with a challenge/response */
465 static BOOL wbinfo_auth_crap(char *username)
467 struct winbindd_request request;
468 struct winbindd_response response;
469 NSS_STATUS result;
470 fstring name_user;
471 fstring name_domain;
472 fstring pass;
473 char *p;
475 /* Send off request */
477 ZERO_STRUCT(request);
478 ZERO_STRUCT(response);
480 p = strchr(username, '%');
482 if (p) {
483 *p = 0;
484 fstrcpy(pass, p + 1);
487 parse_wbinfo_domain_user(username, name_domain, name_user);
489 if (push_utf8_fstring(request.data.auth_crap.user, name_user) == -1) {
490 d_printf("unable to create utf8 string for '%s'\n",
491 name_user);
492 return False;
495 if (push_utf8_fstring(request.data.auth_crap.domain,
496 name_domain) == -1) {
497 d_printf("unable to create utf8 string for '%s'\n",
498 name_domain);
499 return False;
502 generate_random_buffer(request.data.auth_crap.chal, 8, False);
504 SMBencrypt(pass, request.data.auth_crap.chal,
505 (uchar *)request.data.auth_crap.lm_resp);
506 SMBNTencrypt(pass, request.data.auth_crap.chal,
507 (uchar *)request.data.auth_crap.nt_resp);
509 request.data.auth_crap.lm_resp_len = 24;
510 request.data.auth_crap.nt_resp_len = 24;
512 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
514 /* Display response */
516 d_printf("challenge/response password authentication %s\n",
517 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
519 if (response.data.auth.nt_status)
520 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
521 response.data.auth.nt_status_string,
522 response.data.auth.nt_status,
523 response.data.auth.error_string);
525 return result == NSS_STATUS_SUCCESS;
528 /******************************************************************
529 create a winbindd user
530 ******************************************************************/
532 static BOOL wbinfo_create_user(char *username)
534 struct winbindd_request request;
535 struct winbindd_response response;
536 NSS_STATUS result;
538 /* Send off request */
540 ZERO_STRUCT(request);
541 ZERO_STRUCT(response);
543 request.flags = WBFLAG_ALLOCATE_RID;
544 fstrcpy(request.data.acct_mgt.username, username);
546 result = winbindd_request(WINBINDD_CREATE_USER, &request, &response);
548 if ( result == NSS_STATUS_SUCCESS )
549 d_printf("New RID is %d\n", response.data.rid);
551 return result == NSS_STATUS_SUCCESS;
554 /******************************************************************
555 remove a winbindd user
556 ******************************************************************/
558 static BOOL wbinfo_delete_user(char *username)
560 struct winbindd_request request;
561 struct winbindd_response response;
562 NSS_STATUS result;
564 /* Send off request */
566 ZERO_STRUCT(request);
567 ZERO_STRUCT(response);
569 fstrcpy(request.data.acct_mgt.username, username);
571 result = winbindd_request(WINBINDD_DELETE_USER, &request, &response);
573 return result == NSS_STATUS_SUCCESS;
576 /******************************************************************
577 create a winbindd group
578 ******************************************************************/
580 static BOOL wbinfo_create_group(char *groupname)
582 struct winbindd_request request;
583 struct winbindd_response response;
584 NSS_STATUS result;
586 /* Send off request */
588 ZERO_STRUCT(request);
589 ZERO_STRUCT(response);
591 fstrcpy(request.data.acct_mgt.groupname, groupname);
593 result = winbindd_request(WINBINDD_CREATE_GROUP, &request, &response);
595 return result == NSS_STATUS_SUCCESS;
598 /******************************************************************
599 remove a winbindd group
600 ******************************************************************/
602 static BOOL wbinfo_delete_group(char *groupname)
604 struct winbindd_request request;
605 struct winbindd_response response;
606 NSS_STATUS result;
608 /* Send off request */
610 ZERO_STRUCT(request);
611 ZERO_STRUCT(response);
613 fstrcpy(request.data.acct_mgt.groupname, groupname);
615 result = winbindd_request(WINBINDD_DELETE_GROUP, &request, &response);
617 return result == NSS_STATUS_SUCCESS;
620 /******************************************************************
621 parse a string in the form user:group
622 ******************************************************************/
624 static BOOL parse_user_group( const char *string, fstring user, fstring group )
626 char *p;
628 if ( !string )
629 return False;
631 if ( !(p = strchr( string, ':' )) )
632 return False;
634 *p = '\0';
635 p++;
637 fstrcpy( user, string );
638 fstrcpy( group, p );
640 return True;
643 /******************************************************************
644 add a user to a winbindd group
645 ******************************************************************/
647 static BOOL wbinfo_add_user_to_group(char *string)
649 struct winbindd_request request;
650 struct winbindd_response response;
651 NSS_STATUS result;
653 /* Send off request */
655 ZERO_STRUCT(request);
656 ZERO_STRUCT(response);
658 if ( !parse_user_group( string, request.data.acct_mgt.username,
659 request.data.acct_mgt.groupname))
661 d_printf("Can't parse user:group from %s\n", string);
662 return False;
665 result = winbindd_request(WINBINDD_ADD_USER_TO_GROUP, &request, &response);
667 return result == NSS_STATUS_SUCCESS;
670 /******************************************************************
671 remove a user from a winbindd group
672 ******************************************************************/
674 static BOOL wbinfo_remove_user_from_group(char *string)
676 struct winbindd_request request;
677 struct winbindd_response response;
678 NSS_STATUS result;
680 /* Send off request */
682 ZERO_STRUCT(request);
683 ZERO_STRUCT(response);
685 if ( !parse_user_group( string, request.data.acct_mgt.username,
686 request.data.acct_mgt.groupname))
688 d_printf("Can't parse user:group from %s\n", string);
689 return False;
692 result = winbindd_request(WINBINDD_REMOVE_USER_FROM_GROUP, &request, &response);
694 return result == NSS_STATUS_SUCCESS;
697 /* Print domain users */
699 static BOOL print_domain_users(const char *domain)
701 struct winbindd_request request;
702 struct winbindd_response response;
703 const char *extra_data;
704 fstring name;
706 /* Send request to winbind daemon */
708 ZERO_STRUCT(request);
709 ZERO_STRUCT(response);
711 if (domain) {
712 /* '.' is the special sign for our own domwin */
713 if ( strequal(domain, ".") )
714 fstrcpy( request.domain_name, lp_workgroup() );
715 else
716 fstrcpy( request.domain_name, domain );
719 if (winbindd_request(WINBINDD_LIST_USERS, &request, &response) !=
720 NSS_STATUS_SUCCESS)
721 return False;
723 /* Look through extra data */
725 if (!response.extra_data)
726 return False;
728 extra_data = (const char *)response.extra_data;
730 while(next_token(&extra_data, name, ",", sizeof(fstring)))
731 d_printf("%s\n", name);
733 SAFE_FREE(response.extra_data);
735 return True;
738 /* Print domain groups */
740 static BOOL print_domain_groups(const char *domain)
742 struct winbindd_request request;
743 struct winbindd_response response;
744 const char *extra_data;
745 fstring name;
747 ZERO_STRUCT(request);
748 ZERO_STRUCT(response);
750 if (domain) {
751 if ( strequal(domain, ".") )
752 fstrcpy( request.domain_name, lp_workgroup() );
753 else
754 fstrcpy( request.domain_name, domain );
757 if (winbindd_request(WINBINDD_LIST_GROUPS, &request, &response) !=
758 NSS_STATUS_SUCCESS)
759 return False;
761 /* Look through extra data */
763 if (!response.extra_data)
764 return False;
766 extra_data = (const char *)response.extra_data;
768 while(next_token(&extra_data, name, ",", sizeof(fstring)))
769 d_printf("%s\n", name);
771 SAFE_FREE(response.extra_data);
773 return True;
776 /* Set the authorised user for winbindd access in secrets.tdb */
778 static BOOL wbinfo_set_auth_user(char *username)
780 char *password;
781 fstring user, domain;
783 /* Separate into user and password */
785 parse_wbinfo_domain_user(username, domain, user);
787 password = strchr(user, '%');
789 if (password) {
790 *password = 0;
791 password++;
792 } else
793 password = "";
795 /* Store or remove DOMAIN\username%password in secrets.tdb */
797 secrets_init();
799 if (user[0]) {
801 if (!secrets_store(SECRETS_AUTH_USER, user,
802 strlen(user) + 1)) {
803 d_fprintf(stderr, "error storing username\n");
804 return False;
807 /* We always have a domain name added by the
808 parse_wbinfo_domain_user() function. */
810 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
811 strlen(domain) + 1)) {
812 d_fprintf(stderr, "error storing domain name\n");
813 return False;
816 } else {
817 secrets_delete(SECRETS_AUTH_USER);
818 secrets_delete(SECRETS_AUTH_DOMAIN);
821 if (password[0]) {
823 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
824 strlen(password) + 1)) {
825 d_fprintf(stderr, "error storing password\n");
826 return False;
829 } else
830 secrets_delete(SECRETS_AUTH_PASSWORD);
832 return True;
835 static void wbinfo_get_auth_user(void)
837 char *user, *domain, *password;
839 /* Lift data from secrets file */
841 secrets_init();
843 user = secrets_fetch(SECRETS_AUTH_USER, NULL);
844 domain = secrets_fetch(SECRETS_AUTH_DOMAIN, NULL);
845 password = secrets_fetch(SECRETS_AUTH_PASSWORD, NULL);
847 if (!user && !domain && !password) {
848 d_printf("No authorised user configured\n");
849 return;
852 /* Pretty print authorised user info */
854 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? "\\" : "",
855 user, password ? "%" : "", password ? password : "");
857 SAFE_FREE(user);
858 SAFE_FREE(domain);
859 SAFE_FREE(password);
862 static BOOL wbinfo_ping(void)
864 NSS_STATUS result;
866 result = winbindd_request(WINBINDD_PING, NULL, NULL);
868 /* Display response */
870 d_printf("Ping to winbindd %s on fd %d\n",
871 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
873 return result == NSS_STATUS_SUCCESS;
876 /* Main program */
878 enum {
879 OPT_SET_AUTH_USER = 1000,
880 OPT_GET_AUTH_USER,
881 OPT_DOMAIN_NAME,
882 OPT_SEQUENCE
885 int main(int argc, char **argv)
887 int opt;
889 poptContext pc;
890 static char *string_arg;
891 static char *opt_domain_name;
892 static int int_arg;
893 int result = 1;
895 struct poptOption long_options[] = {
896 POPT_AUTOHELP
898 /* longName, shortName, argInfo, argPtr, value, descrip,
899 argDesc */
901 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
902 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
903 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
904 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
905 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
906 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
907 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
908 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
909 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
910 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
911 { "create-user", 'c', POPT_ARG_STRING, &string_arg, 'c', "Create a local user account", "name" },
912 { "delete-user", 'x', POPT_ARG_STRING, &string_arg, 'x', "Delete a local user account", "name" },
913 { "create-group", 'C', POPT_ARG_STRING, &string_arg, 'C', "Create a local group", "name" },
914 { "delete-group", 'X', POPT_ARG_STRING, &string_arg, 'X', "Delete a local group", "name" },
915 { "add-to-group", 'o', POPT_ARG_STRING, &string_arg, 'o', "Add user to group", "user:group" },
916 { "del-from-group", 'O', POPT_ARG_STRING, &string_arg, 'O', "Remove user from group", "user:group" },
917 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
918 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
919 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
920 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
921 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
922 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
923 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
924 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
925 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operatio", "domain" },
926 POPT_COMMON_VERSION
927 POPT_TABLEEND
930 /* Samba client initialisation */
932 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
933 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
934 dyn_CONFIGFILE, strerror(errno));
935 exit(1);
938 if (!init_names())
939 return 1;
941 load_interfaces();
943 /* Parse options */
945 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
947 /* Parse command line options */
949 if (argc == 1) {
950 poptPrintHelp(pc, stderr, 0);
951 return 1;
954 while((opt = poptGetNextOpt(pc)) != -1) {
955 /* get the generic configuration parameters like --domain */
958 poptFreeContext(pc);
960 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
961 POPT_CONTEXT_KEEP_FIRST);
963 while((opt = poptGetNextOpt(pc)) != -1) {
964 switch (opt) {
965 case 'u':
966 if (!print_domain_users(opt_domain_name)) {
967 d_printf("Error looking up domain users\n");
968 goto done;
970 break;
971 case 'g':
972 if (!print_domain_groups(opt_domain_name)) {
973 d_printf("Error looking up domain groups\n");
974 goto done;
976 break;
977 case 's':
978 if (!wbinfo_lookupsid(string_arg)) {
979 d_printf("Could not lookup sid %s\n", string_arg);
980 goto done;
982 break;
983 case 'n':
984 if (!wbinfo_lookupname(string_arg)) {
985 d_printf("Could not lookup name %s\n", string_arg);
986 goto done;
988 break;
989 case 'N':
990 if (!wbinfo_wins_byname(string_arg)) {
991 d_printf("Could not lookup WINS by name %s\n", string_arg);
992 goto done;
994 break;
995 case 'I':
996 if (!wbinfo_wins_byip(string_arg)) {
997 d_printf("Could not lookup WINS by IP %s\n", string_arg);
998 goto done;
1000 break;
1001 case 'U':
1002 if (!wbinfo_uid_to_sid(int_arg)) {
1003 d_printf("Could not convert uid %d to sid\n", int_arg);
1004 goto done;
1006 break;
1007 case 'G':
1008 if (!wbinfo_gid_to_sid(int_arg)) {
1009 d_printf("Could not convert gid %d to sid\n",
1010 int_arg);
1011 goto done;
1013 break;
1014 case 'S':
1015 if (!wbinfo_sid_to_uid(string_arg)) {
1016 d_printf("Could not convert sid %s to uid\n",
1017 string_arg);
1018 goto done;
1020 break;
1021 case 'Y':
1022 if (!wbinfo_sid_to_gid(string_arg)) {
1023 d_printf("Could not convert sid %s to gid\n",
1024 string_arg);
1025 goto done;
1027 break;
1028 case 't':
1029 if (!wbinfo_check_secret()) {
1030 d_printf("Could not check secret\n");
1031 goto done;
1033 break;
1034 case 'm':
1035 if (!wbinfo_list_domains()) {
1036 d_printf("Could not list trusted domains\n");
1037 goto done;
1039 break;
1040 case OPT_SEQUENCE:
1041 if (!wbinfo_show_sequence(opt_domain_name)) {
1042 d_printf("Could not show sequence numbers\n");
1043 goto done;
1045 break;
1046 case 'r':
1047 if (!wbinfo_get_usergroups(string_arg)) {
1048 d_printf("Could not get groups for user %s\n",
1049 string_arg);
1050 goto done;
1052 break;
1053 case 'a': {
1054 BOOL got_error = False;
1056 if (!wbinfo_auth(string_arg)) {
1057 d_printf("Could not authenticate user %s with "
1058 "plaintext password\n", string_arg);
1059 got_error = True;
1062 if (!wbinfo_auth_crap(string_arg)) {
1063 d_printf("Could not authenticate user %s with "
1064 "challenge/response\n", string_arg);
1065 got_error = True;
1068 if (got_error)
1069 goto done;
1070 break;
1072 case 'c':
1073 if ( !wbinfo_create_user(string_arg) ) {
1074 d_printf("Could not create user account\n");
1075 goto done;
1077 break;
1078 case 'C':
1079 if ( !wbinfo_create_group(string_arg) ) {
1080 d_printf("Could not create group\n");
1081 goto done;
1083 break;
1084 case 'o':
1085 if ( !wbinfo_add_user_to_group(string_arg) ) {
1086 d_printf("Could not add user to group\n");
1087 goto done;
1089 break;
1090 case 'O':
1091 if ( !wbinfo_remove_user_from_group(string_arg) ) {
1092 d_printf("Could not remove user kfrom group\n");
1093 goto done;
1095 break;
1096 case 'x':
1097 if ( !wbinfo_delete_user(string_arg) ) {
1098 d_printf("Could not delete user account\n");
1099 goto done;
1101 break;
1102 case 'X':
1103 if ( !wbinfo_delete_group(string_arg) ) {
1104 d_printf("Could not delete group\n");
1105 goto done;
1107 break;
1108 case 'p':
1109 if (!wbinfo_ping()) {
1110 d_printf("could not ping winbindd!\n");
1111 goto done;
1113 break;
1114 case OPT_SET_AUTH_USER:
1115 wbinfo_set_auth_user(string_arg);
1116 break;
1117 case OPT_GET_AUTH_USER:
1118 wbinfo_get_auth_user();
1119 break;
1120 /* generic configuration options */
1121 case OPT_DOMAIN_NAME:
1122 break;
1123 default:
1124 d_fprintf(stderr, "Invalid option\n");
1125 poptPrintHelp(pc, stderr, 0);
1126 goto done;
1130 result = 0;
1132 /* Exit code */
1134 done:
1135 poptFreeContext(pc);
1136 return result;