r84: Implement --required-membership-of=, an ntlm_auth option that restricts
[Samba/gebeck_regimport.git] / source / nsswitch / wbinfo.c
blob2cea4130adcd0b650e4ef993d1a68870fcedcee4
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 /* Convert NetBIOS name to IP */
172 static BOOL wbinfo_wins_byname(char *name)
174 struct winbindd_request request;
175 struct winbindd_response response;
177 ZERO_STRUCT(request);
178 ZERO_STRUCT(response);
180 /* Send request */
182 fstrcpy(request.data.winsreq, name);
184 if (winbindd_request(WINBINDD_WINS_BYNAME, &request, &response) !=
185 NSS_STATUS_SUCCESS) {
186 return False;
189 /* Display response */
191 printf("%s\n", response.data.winsresp);
193 return True;
196 /* Convert IP to NetBIOS name */
198 static BOOL wbinfo_wins_byip(char *ip)
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, ip);
210 if (winbindd_request(WINBINDD_WINS_BYIP, &request, &response) !=
211 NSS_STATUS_SUCCESS) {
212 return False;
215 /* Display response */
217 printf("%s\n", response.data.winsresp);
219 return True;
222 /* List trusted domains */
224 static BOOL wbinfo_list_domains(void)
226 struct winbindd_response response;
227 fstring name;
229 ZERO_STRUCT(response);
231 /* Send request */
233 if (winbindd_request(WINBINDD_LIST_TRUSTDOM, NULL, &response) !=
234 NSS_STATUS_SUCCESS)
235 return False;
237 /* Display response */
239 if (response.extra_data) {
240 const char *extra_data = (char *)response.extra_data;
242 while(next_token(&extra_data, name, ",", sizeof(fstring)))
243 d_printf("%s\n", name);
245 SAFE_FREE(response.extra_data);
248 return True;
252 /* show sequence numbers */
253 static BOOL wbinfo_show_sequence(const char *domain)
255 struct winbindd_request request;
256 struct winbindd_response response;
258 ZERO_STRUCT(response);
259 ZERO_STRUCT(request);
261 if ( domain )
262 fstrcpy( request.domain_name, domain );
264 /* Send request */
266 if (winbindd_request(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
267 NSS_STATUS_SUCCESS)
268 return False;
270 /* Display response */
272 if (response.extra_data) {
273 char *extra_data = (char *)response.extra_data;
274 d_printf("%s", extra_data);
275 SAFE_FREE(response.extra_data);
278 return True;
281 /* Show domain info */
283 static BOOL wbinfo_domain_info(const char *domain_name)
285 struct winbindd_request request;
286 struct winbindd_response response;
288 ZERO_STRUCT(request);
289 ZERO_STRUCT(response);
291 fstrcpy(request.domain_name, domain_name);
293 /* Send request */
295 if (winbindd_request(WINBINDD_DOMAIN_INFO, &request, &response) !=
296 NSS_STATUS_SUCCESS)
297 return False;
299 /* Display response */
301 d_printf("Name : %s\n", response.data.domain_info.name);
302 d_printf("Alt_Name : %s\n", response.data.domain_info.alt_name);
304 d_printf("SID : %s\n", response.data.domain_info.sid);
306 d_printf("Active Directory : %s\n",
307 response.data.domain_info.active_directory ? "Yes" : "No");
308 d_printf("Native : %s\n",
309 response.data.domain_info.native_mode ? "Yes" : "No");
311 d_printf("Primary : %s\n",
312 response.data.domain_info.primary ? "Yes" : "No");
314 d_printf("Sequence : %d\n", response.data.domain_info.sequence_number);
316 return True;
319 /* Check trust account password */
321 static BOOL wbinfo_check_secret(void)
323 struct winbindd_response response;
324 NSS_STATUS result;
326 ZERO_STRUCT(response);
328 result = winbindd_request(WINBINDD_CHECK_MACHACC, NULL, &response);
330 d_printf("checking the trust secret via RPC calls %s\n",
331 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
333 if (result != NSS_STATUS_SUCCESS)
334 d_printf("error code was %s (0x%x)\n",
335 response.data.auth.nt_status_string,
336 response.data.auth.nt_status);
338 return result == NSS_STATUS_SUCCESS;
341 /* Convert uid to sid */
343 static BOOL wbinfo_uid_to_sid(uid_t uid)
345 struct winbindd_request request;
346 struct winbindd_response response;
348 ZERO_STRUCT(request);
349 ZERO_STRUCT(response);
351 /* Send request */
353 request.data.uid = uid;
355 if (winbindd_request(WINBINDD_UID_TO_SID, &request, &response) !=
356 NSS_STATUS_SUCCESS)
357 return False;
359 /* Display response */
361 d_printf("%s\n", response.data.sid.sid);
363 return True;
366 /* Convert gid to sid */
368 static BOOL wbinfo_gid_to_sid(gid_t gid)
370 struct winbindd_request request;
371 struct winbindd_response response;
373 ZERO_STRUCT(request);
374 ZERO_STRUCT(response);
376 /* Send request */
378 request.data.gid = gid;
380 if (winbindd_request(WINBINDD_GID_TO_SID, &request, &response) !=
381 NSS_STATUS_SUCCESS)
382 return False;
384 /* Display response */
386 d_printf("%s\n", response.data.sid.sid);
388 return True;
391 /* Convert sid to uid */
393 static BOOL wbinfo_sid_to_uid(char *sid)
395 struct winbindd_request request;
396 struct winbindd_response response;
398 ZERO_STRUCT(request);
399 ZERO_STRUCT(response);
401 /* Send request */
403 fstrcpy(request.data.sid, sid);
405 if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
406 NSS_STATUS_SUCCESS)
407 return False;
409 /* Display response */
411 d_printf("%d\n", (int)response.data.uid);
413 return True;
416 static BOOL wbinfo_sid_to_gid(char *sid)
418 struct winbindd_request request;
419 struct winbindd_response response;
421 ZERO_STRUCT(request);
422 ZERO_STRUCT(response);
424 /* Send request */
426 fstrcpy(request.data.sid, sid);
428 if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
429 NSS_STATUS_SUCCESS)
430 return False;
432 /* Display response */
434 d_printf("%d\n", (int)response.data.gid);
436 return True;
439 /* Convert sid to string */
441 static BOOL wbinfo_lookupsid(char *sid)
443 struct winbindd_request request;
444 struct winbindd_response response;
446 ZERO_STRUCT(request);
447 ZERO_STRUCT(response);
449 /* Send off request */
451 fstrcpy(request.data.sid, sid);
453 if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
454 NSS_STATUS_SUCCESS)
455 return False;
457 /* Display response */
459 d_printf("%s%c%s %d\n", response.data.name.dom_name,
460 winbind_separator(), response.data.name.name,
461 response.data.name.type);
463 return True;
466 /* Convert string to sid */
468 static BOOL wbinfo_lookupname(char *name)
470 struct winbindd_request request;
471 struct winbindd_response response;
473 /* Send off request */
475 ZERO_STRUCT(request);
476 ZERO_STRUCT(response);
478 parse_wbinfo_domain_user(name, request.data.name.dom_name,
479 request.data.name.name);
481 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
482 NSS_STATUS_SUCCESS)
483 return False;
485 /* Display response */
487 d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
489 return True;
492 /* Authenticate a user with a plaintext password */
494 static BOOL wbinfo_auth(char *username)
496 struct winbindd_request request;
497 struct winbindd_response response;
498 NSS_STATUS result;
499 char *p;
501 /* Send off request */
503 ZERO_STRUCT(request);
504 ZERO_STRUCT(response);
506 p = strchr(username, '%');
508 if (p) {
509 *p = 0;
510 fstrcpy(request.data.auth.user, username);
511 fstrcpy(request.data.auth.pass, p + 1);
512 *p = '%';
513 } else
514 fstrcpy(request.data.auth.user, username);
516 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
518 /* Display response */
520 d_printf("plaintext password authentication %s\n",
521 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
523 if (response.data.auth.nt_status)
524 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
525 response.data.auth.nt_status_string,
526 response.data.auth.nt_status,
527 response.data.auth.error_string);
529 return result == NSS_STATUS_SUCCESS;
532 /* Authenticate a user with a challenge/response */
534 static BOOL wbinfo_auth_crap(char *username)
536 struct winbindd_request request;
537 struct winbindd_response response;
538 NSS_STATUS result;
539 fstring name_user;
540 fstring name_domain;
541 fstring pass;
542 char *p;
544 /* Send off request */
546 ZERO_STRUCT(request);
547 ZERO_STRUCT(response);
549 p = strchr(username, '%');
551 if (p) {
552 *p = 0;
553 fstrcpy(pass, p + 1);
556 parse_wbinfo_domain_user(username, name_domain, name_user);
558 if (push_utf8_fstring(request.data.auth_crap.user, name_user) == -1) {
559 d_printf("unable to create utf8 string for '%s'\n",
560 name_user);
561 return False;
564 if (push_utf8_fstring(request.data.auth_crap.domain,
565 name_domain) == -1) {
566 d_printf("unable to create utf8 string for '%s'\n",
567 name_domain);
568 return False;
571 generate_random_buffer(request.data.auth_crap.chal, 8, False);
573 SMBencrypt(pass, request.data.auth_crap.chal,
574 (uchar *)request.data.auth_crap.lm_resp);
575 SMBNTencrypt(pass, request.data.auth_crap.chal,
576 (uchar *)request.data.auth_crap.nt_resp);
578 request.data.auth_crap.lm_resp_len = 24;
579 request.data.auth_crap.nt_resp_len = 24;
581 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
583 /* Display response */
585 d_printf("challenge/response password authentication %s\n",
586 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
588 if (response.data.auth.nt_status)
589 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
590 response.data.auth.nt_status_string,
591 response.data.auth.nt_status,
592 response.data.auth.error_string);
594 return result == NSS_STATUS_SUCCESS;
597 /* Authenticate a user with a plaintext password and set a token */
599 static BOOL wbinfo_klog(char *username)
601 struct winbindd_request request;
602 struct winbindd_response response;
603 NSS_STATUS result;
604 char *p;
606 /* Send off request */
608 ZERO_STRUCT(request);
609 ZERO_STRUCT(response);
611 p = strchr(username, '%');
613 if (p) {
614 *p = 0;
615 fstrcpy(request.data.auth.user, username);
616 fstrcpy(request.data.auth.pass, p + 1);
617 *p = '%';
618 } else {
619 fstrcpy(request.data.auth.user, username);
620 fstrcpy(request.data.auth.pass, getpass("Password: "));
623 request.flags |= WBFLAG_PAM_AFS_TOKEN;
625 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
627 /* Display response */
629 d_printf("plaintext password authentication %s\n",
630 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
632 if (response.data.auth.nt_status)
633 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
634 response.data.auth.nt_status_string,
635 response.data.auth.nt_status,
636 response.data.auth.error_string);
638 if (result != NSS_STATUS_SUCCESS)
639 return False;
641 if (response.extra_data == NULL) {
642 d_printf("Did not get token data\n");
643 return False;
646 if (!afs_settoken_str((char *)response.extra_data)) {
647 d_printf("Could not set token\n");
648 return False;
651 d_printf("Successfully created AFS token\n");
652 return True;
655 /******************************************************************
656 create a winbindd user
657 ******************************************************************/
659 static BOOL wbinfo_create_user(char *username)
661 struct winbindd_request request;
662 struct winbindd_response response;
663 NSS_STATUS result;
665 /* Send off request */
667 ZERO_STRUCT(request);
668 ZERO_STRUCT(response);
670 request.flags = WBFLAG_ALLOCATE_RID;
671 fstrcpy(request.data.acct_mgt.username, username);
673 result = winbindd_request(WINBINDD_CREATE_USER, &request, &response);
675 if ( result == NSS_STATUS_SUCCESS )
676 d_printf("New RID is %d\n", response.data.rid);
678 return result == NSS_STATUS_SUCCESS;
681 /******************************************************************
682 remove a winbindd user
683 ******************************************************************/
685 static BOOL wbinfo_delete_user(char *username)
687 struct winbindd_request request;
688 struct winbindd_response response;
689 NSS_STATUS result;
691 /* Send off request */
693 ZERO_STRUCT(request);
694 ZERO_STRUCT(response);
696 fstrcpy(request.data.acct_mgt.username, username);
698 result = winbindd_request(WINBINDD_DELETE_USER, &request, &response);
700 return result == NSS_STATUS_SUCCESS;
703 /******************************************************************
704 create a winbindd group
705 ******************************************************************/
707 static BOOL wbinfo_create_group(char *groupname)
709 struct winbindd_request request;
710 struct winbindd_response response;
711 NSS_STATUS result;
713 /* Send off request */
715 ZERO_STRUCT(request);
716 ZERO_STRUCT(response);
718 fstrcpy(request.data.acct_mgt.groupname, groupname);
720 result = winbindd_request(WINBINDD_CREATE_GROUP, &request, &response);
722 return result == NSS_STATUS_SUCCESS;
725 /******************************************************************
726 remove a winbindd group
727 ******************************************************************/
729 static BOOL wbinfo_delete_group(char *groupname)
731 struct winbindd_request request;
732 struct winbindd_response response;
733 NSS_STATUS result;
735 /* Send off request */
737 ZERO_STRUCT(request);
738 ZERO_STRUCT(response);
740 fstrcpy(request.data.acct_mgt.groupname, groupname);
742 result = winbindd_request(WINBINDD_DELETE_GROUP, &request, &response);
744 return result == NSS_STATUS_SUCCESS;
747 /******************************************************************
748 parse a string in the form user:group
749 ******************************************************************/
751 static BOOL parse_user_group( const char *string, fstring user, fstring group )
753 char *p;
755 if ( !string )
756 return False;
758 if ( !(p = strchr( string, ':' )) )
759 return False;
761 *p = '\0';
762 p++;
764 fstrcpy( user, string );
765 fstrcpy( group, p );
767 return True;
770 /******************************************************************
771 add a user to a winbindd group
772 ******************************************************************/
774 static BOOL wbinfo_add_user_to_group(char *string)
776 struct winbindd_request request;
777 struct winbindd_response response;
778 NSS_STATUS result;
780 /* Send off request */
782 ZERO_STRUCT(request);
783 ZERO_STRUCT(response);
785 if ( !parse_user_group( string, request.data.acct_mgt.username,
786 request.data.acct_mgt.groupname))
788 d_printf("Can't parse user:group from %s\n", string);
789 return False;
792 result = winbindd_request(WINBINDD_ADD_USER_TO_GROUP, &request, &response);
794 return result == NSS_STATUS_SUCCESS;
797 /******************************************************************
798 remove a user from a winbindd group
799 ******************************************************************/
801 static BOOL wbinfo_remove_user_from_group(char *string)
803 struct winbindd_request request;
804 struct winbindd_response response;
805 NSS_STATUS result;
807 /* Send off request */
809 ZERO_STRUCT(request);
810 ZERO_STRUCT(response);
812 if ( !parse_user_group( string, request.data.acct_mgt.username,
813 request.data.acct_mgt.groupname))
815 d_printf("Can't parse user:group from %s\n", string);
816 return False;
819 result = winbindd_request(WINBINDD_REMOVE_USER_FROM_GROUP, &request, &response);
821 return result == NSS_STATUS_SUCCESS;
824 /* Print domain users */
826 static BOOL print_domain_users(const char *domain)
828 struct winbindd_request request;
829 struct winbindd_response response;
830 const char *extra_data;
831 fstring name;
833 /* Send request to winbind daemon */
835 ZERO_STRUCT(request);
836 ZERO_STRUCT(response);
838 if (domain) {
839 /* '.' is the special sign for our own domwin */
840 if ( strequal(domain, ".") )
841 fstrcpy( request.domain_name, lp_workgroup() );
842 else
843 fstrcpy( request.domain_name, domain );
846 if (winbindd_request(WINBINDD_LIST_USERS, &request, &response) !=
847 NSS_STATUS_SUCCESS)
848 return False;
850 /* Look through extra data */
852 if (!response.extra_data)
853 return False;
855 extra_data = (const char *)response.extra_data;
857 while(next_token(&extra_data, name, ",", sizeof(fstring)))
858 d_printf("%s\n", name);
860 SAFE_FREE(response.extra_data);
862 return True;
865 /* Print domain groups */
867 static BOOL print_domain_groups(const char *domain)
869 struct winbindd_request request;
870 struct winbindd_response response;
871 const char *extra_data;
872 fstring name;
874 ZERO_STRUCT(request);
875 ZERO_STRUCT(response);
877 if (domain) {
878 if ( strequal(domain, ".") )
879 fstrcpy( request.domain_name, lp_workgroup() );
880 else
881 fstrcpy( request.domain_name, domain );
884 if (winbindd_request(WINBINDD_LIST_GROUPS, &request, &response) !=
885 NSS_STATUS_SUCCESS)
886 return False;
888 /* Look through extra data */
890 if (!response.extra_data)
891 return False;
893 extra_data = (const char *)response.extra_data;
895 while(next_token(&extra_data, name, ",", sizeof(fstring)))
896 d_printf("%s\n", name);
898 SAFE_FREE(response.extra_data);
900 return True;
903 /* Set the authorised user for winbindd access in secrets.tdb */
905 static BOOL wbinfo_set_auth_user(char *username)
907 const char *password;
908 char *p;
909 fstring user, domain;
911 /* Separate into user and password */
913 parse_wbinfo_domain_user(username, domain, user);
915 p = strchr(user, '%');
917 if (p != NULL) {
918 *p = 0;
919 password = p+1;
920 } else {
921 char *thepass = getpass("Password: ");
922 if (thepass) {
923 password = thepass;
924 } else
925 password = "";
928 /* Store or remove DOMAIN\username%password in secrets.tdb */
930 secrets_init();
932 if (user[0]) {
934 if (!secrets_store(SECRETS_AUTH_USER, user,
935 strlen(user) + 1)) {
936 d_fprintf(stderr, "error storing username\n");
937 return False;
940 /* We always have a domain name added by the
941 parse_wbinfo_domain_user() function. */
943 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
944 strlen(domain) + 1)) {
945 d_fprintf(stderr, "error storing domain name\n");
946 return False;
949 } else {
950 secrets_delete(SECRETS_AUTH_USER);
951 secrets_delete(SECRETS_AUTH_DOMAIN);
954 if (password[0]) {
956 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
957 strlen(password) + 1)) {
958 d_fprintf(stderr, "error storing password\n");
959 return False;
962 } else
963 secrets_delete(SECRETS_AUTH_PASSWORD);
965 return True;
968 static void wbinfo_get_auth_user(void)
970 char *user, *domain, *password;
972 /* Lift data from secrets file */
974 secrets_fetch_ipc_userpass(&user, &domain, &password);
976 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
978 SAFE_FREE(user);
979 SAFE_FREE(domain);
980 SAFE_FREE(password);
981 d_printf("No authorised user configured\n");
982 return;
985 /* Pretty print authorised user info */
987 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
988 user, password ? "%" : "", password ? password : "");
990 SAFE_FREE(user);
991 SAFE_FREE(domain);
992 SAFE_FREE(password);
995 static BOOL wbinfo_ping(void)
997 NSS_STATUS result;
999 result = winbindd_request(WINBINDD_PING, NULL, NULL);
1001 /* Display response */
1003 d_printf("Ping to winbindd %s on fd %d\n",
1004 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
1006 return result == NSS_STATUS_SUCCESS;
1009 /* Main program */
1011 enum {
1012 OPT_SET_AUTH_USER = 1000,
1013 OPT_GET_AUTH_USER,
1014 OPT_DOMAIN_NAME,
1015 OPT_SEQUENCE,
1016 OPT_USERSIDS
1019 int main(int argc, char **argv)
1021 int opt;
1023 poptContext pc;
1024 static char *string_arg;
1025 static char *opt_domain_name;
1026 static int int_arg;
1027 int result = 1;
1029 struct poptOption long_options[] = {
1030 POPT_AUTOHELP
1032 /* longName, shortName, argInfo, argPtr, value, descrip,
1033 argDesc */
1035 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1036 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1037 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1038 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1039 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1040 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1041 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1042 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1043 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1044 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1045 { "create-user", 'c', POPT_ARG_STRING, &string_arg, 'c', "Create a local user account", "name" },
1046 { "delete-user", 'x', POPT_ARG_STRING, &string_arg, 'x', "Delete a local user account", "name" },
1047 { "create-group", 'C', POPT_ARG_STRING, &string_arg, 'C', "Create a local group", "name" },
1048 { "delete-group", 'X', POPT_ARG_STRING, &string_arg, 'X', "Delete a local group", "name" },
1049 { "add-to-group", 'o', POPT_ARG_STRING, &string_arg, 'o', "Add user to group", "user:group" },
1050 { "del-from-group", 'O', POPT_ARG_STRING, &string_arg, 'O', "Remove user from group", "user:group" },
1051 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1052 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1053 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1054 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1055 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1056 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1057 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1058 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1059 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1060 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1061 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1062 #ifdef WITH_FAKE_KASERVER
1063 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1064 #endif
1065 POPT_COMMON_VERSION
1066 POPT_TABLEEND
1069 /* Samba client initialisation */
1071 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
1072 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1073 dyn_CONFIGFILE, strerror(errno));
1074 exit(1);
1077 if (!init_names())
1078 return 1;
1080 load_interfaces();
1082 /* Parse options */
1084 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1086 /* Parse command line options */
1088 if (argc == 1) {
1089 poptPrintHelp(pc, stderr, 0);
1090 return 1;
1093 while((opt = poptGetNextOpt(pc)) != -1) {
1094 /* get the generic configuration parameters like --domain */
1097 poptFreeContext(pc);
1099 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1100 POPT_CONTEXT_KEEP_FIRST);
1102 while((opt = poptGetNextOpt(pc)) != -1) {
1103 switch (opt) {
1104 case 'u':
1105 if (!print_domain_users(opt_domain_name)) {
1106 d_printf("Error looking up domain users\n");
1107 goto done;
1109 break;
1110 case 'g':
1111 if (!print_domain_groups(opt_domain_name)) {
1112 d_printf("Error looking up domain groups\n");
1113 goto done;
1115 break;
1116 case 's':
1117 if (!wbinfo_lookupsid(string_arg)) {
1118 d_printf("Could not lookup sid %s\n", string_arg);
1119 goto done;
1121 break;
1122 case 'n':
1123 if (!wbinfo_lookupname(string_arg)) {
1124 d_printf("Could not lookup name %s\n", string_arg);
1125 goto done;
1127 break;
1128 case 'N':
1129 if (!wbinfo_wins_byname(string_arg)) {
1130 d_printf("Could not lookup WINS by name %s\n", string_arg);
1131 goto done;
1133 break;
1134 case 'I':
1135 if (!wbinfo_wins_byip(string_arg)) {
1136 d_printf("Could not lookup WINS by IP %s\n", string_arg);
1137 goto done;
1139 break;
1140 case 'U':
1141 if (!wbinfo_uid_to_sid(int_arg)) {
1142 d_printf("Could not convert uid %d to sid\n", int_arg);
1143 goto done;
1145 break;
1146 case 'G':
1147 if (!wbinfo_gid_to_sid(int_arg)) {
1148 d_printf("Could not convert gid %d to sid\n",
1149 int_arg);
1150 goto done;
1152 break;
1153 case 'S':
1154 if (!wbinfo_sid_to_uid(string_arg)) {
1155 d_printf("Could not convert sid %s to uid\n",
1156 string_arg);
1157 goto done;
1159 break;
1160 case 'Y':
1161 if (!wbinfo_sid_to_gid(string_arg)) {
1162 d_printf("Could not convert sid %s to gid\n",
1163 string_arg);
1164 goto done;
1166 break;
1167 case 't':
1168 if (!wbinfo_check_secret()) {
1169 d_printf("Could not check secret\n");
1170 goto done;
1172 break;
1173 case 'm':
1174 if (!wbinfo_list_domains()) {
1175 d_printf("Could not list trusted domains\n");
1176 goto done;
1178 break;
1179 case OPT_SEQUENCE:
1180 if (!wbinfo_show_sequence(opt_domain_name)) {
1181 d_printf("Could not show sequence numbers\n");
1182 goto done;
1184 break;
1185 case 'D':
1186 if (!wbinfo_domain_info(string_arg)) {
1187 d_printf("Could not get domain info\n");
1188 goto done;
1190 break;
1191 case 'r':
1192 if (!wbinfo_get_usergroups(string_arg)) {
1193 d_printf("Could not get groups for user %s\n",
1194 string_arg);
1195 goto done;
1197 break;
1198 case OPT_USERSIDS:
1199 if (!wbinfo_get_usersids(string_arg)) {
1200 d_printf("Could not get group SIDs for user SID %s\n",
1201 string_arg);
1202 goto done;
1204 break;
1205 case 'a': {
1206 BOOL got_error = False;
1208 if (!wbinfo_auth(string_arg)) {
1209 d_printf("Could not authenticate user %s with "
1210 "plaintext password\n", string_arg);
1211 got_error = True;
1214 if (!wbinfo_auth_crap(string_arg)) {
1215 d_printf("Could not authenticate user %s with "
1216 "challenge/response\n", string_arg);
1217 got_error = True;
1220 if (got_error)
1221 goto done;
1222 break;
1224 case 'k':
1225 if (!wbinfo_klog(string_arg)) {
1226 d_printf("Could not klog user\n");
1227 goto done;
1229 break;
1230 case 'c':
1231 if ( !wbinfo_create_user(string_arg) ) {
1232 d_printf("Could not create user account\n");
1233 goto done;
1235 break;
1236 case 'C':
1237 if ( !wbinfo_create_group(string_arg) ) {
1238 d_printf("Could not create group\n");
1239 goto done;
1241 break;
1242 case 'o':
1243 if ( !wbinfo_add_user_to_group(string_arg) ) {
1244 d_printf("Could not add user to group\n");
1245 goto done;
1247 break;
1248 case 'O':
1249 if ( !wbinfo_remove_user_from_group(string_arg) ) {
1250 d_printf("Could not remove user from group\n");
1251 goto done;
1253 break;
1254 case 'x':
1255 if ( !wbinfo_delete_user(string_arg) ) {
1256 d_printf("Could not delete user account\n");
1257 goto done;
1259 break;
1260 case 'X':
1261 if ( !wbinfo_delete_group(string_arg) ) {
1262 d_printf("Could not delete group\n");
1263 goto done;
1265 break;
1266 case 'p':
1267 if (!wbinfo_ping()) {
1268 d_printf("could not ping winbindd!\n");
1269 goto done;
1271 break;
1272 case OPT_SET_AUTH_USER:
1273 wbinfo_set_auth_user(string_arg);
1274 break;
1275 case OPT_GET_AUTH_USER:
1276 wbinfo_get_auth_user();
1277 break;
1278 /* generic configuration options */
1279 case OPT_DOMAIN_NAME:
1280 break;
1281 default:
1282 d_fprintf(stderr, "Invalid option\n");
1283 poptPrintHelp(pc, stderr, 0);
1284 goto done;
1288 result = 0;
1290 /* Exit code */
1292 done:
1293 poptFreeContext(pc);
1294 return result;