r1643: syncing all changes from 3.0 and hopefully get 3.0.6rc2 out tomorrow
[Samba.git] / source / nsswitch / wbinfo.c
blob0028982d201f83b277b862b53cdea0ecd8d5d354
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 /* First see whether the SID is actually a user -- otherwise
402 * winbind might end up a uid number for a group SID and this
403 * is asking for trouble later. */
405 fstrcpy(request.data.sid, sid);
407 if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
408 NSS_STATUS_SUCCESS) {
409 d_printf("Could not lookup sid %s\n", sid);
410 return False;
413 if (response.data.name.type != SID_NAME_USER) {
414 d_printf("SID is of type %s\n",
415 sid_type_lookup(response.data.name.type));
416 return False;
419 ZERO_STRUCT(request);
420 ZERO_STRUCT(response);
422 /* Send request */
424 fstrcpy(request.data.sid, sid);
426 if (winbindd_request(WINBINDD_SID_TO_UID, &request, &response) !=
427 NSS_STATUS_SUCCESS)
428 return False;
430 /* Display response */
432 d_printf("%d\n", (int)response.data.uid);
434 return True;
437 static BOOL wbinfo_sid_to_gid(char *sid)
439 struct winbindd_request request;
440 struct winbindd_response response;
442 ZERO_STRUCT(request);
443 ZERO_STRUCT(response);
445 /* First see whether the SID is actually a group -- otherwise
446 * winbind might end up a gid number for a user SID and this
447 * is asking for trouble later. */
449 fstrcpy(request.data.sid, sid);
451 if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
452 NSS_STATUS_SUCCESS) {
453 d_printf("Could not lookup sid %s\n", sid);
454 return False;
457 if ((response.data.name.type != SID_NAME_DOM_GRP) &&
458 (response.data.name.type != SID_NAME_ALIAS) &&
459 (response.data.name.type != SID_NAME_WKN_GRP)) {
460 d_printf("SID is of type %s\n",
461 sid_type_lookup(response.data.name.type));
462 return False;
465 /* Send request */
467 fstrcpy(request.data.sid, sid);
469 if (winbindd_request(WINBINDD_SID_TO_GID, &request, &response) !=
470 NSS_STATUS_SUCCESS)
471 return False;
473 /* Display response */
475 d_printf("%d\n", (int)response.data.gid);
477 return True;
480 static BOOL wbinfo_allocate_rid(void)
482 uint32 rid;
484 if (!winbind_allocate_rid(&rid))
485 return False;
487 d_printf("New rid: %d\n", rid);
489 return True;
492 /* Convert sid to string */
494 static BOOL wbinfo_lookupsid(char *sid)
496 struct winbindd_request request;
497 struct winbindd_response response;
499 ZERO_STRUCT(request);
500 ZERO_STRUCT(response);
502 /* Send off request */
504 fstrcpy(request.data.sid, sid);
506 if (winbindd_request(WINBINDD_LOOKUPSID, &request, &response) !=
507 NSS_STATUS_SUCCESS)
508 return False;
510 /* Display response */
512 d_printf("%s%c%s %d\n", response.data.name.dom_name,
513 winbind_separator(), response.data.name.name,
514 response.data.name.type);
516 return True;
519 /* Convert string to sid */
521 static BOOL wbinfo_lookupname(char *name)
523 struct winbindd_request request;
524 struct winbindd_response response;
526 /* Send off request */
528 ZERO_STRUCT(request);
529 ZERO_STRUCT(response);
531 parse_wbinfo_domain_user(name, request.data.name.dom_name,
532 request.data.name.name);
534 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
535 NSS_STATUS_SUCCESS)
536 return False;
538 /* Display response */
540 d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
542 return True;
545 /* Authenticate a user with a plaintext password */
547 static BOOL wbinfo_auth(char *username)
549 struct winbindd_request request;
550 struct winbindd_response response;
551 NSS_STATUS result;
552 char *p;
554 /* Send off request */
556 ZERO_STRUCT(request);
557 ZERO_STRUCT(response);
559 p = strchr(username, '%');
561 if (p) {
562 *p = 0;
563 fstrcpy(request.data.auth.user, username);
564 fstrcpy(request.data.auth.pass, p + 1);
565 *p = '%';
566 } else
567 fstrcpy(request.data.auth.user, username);
569 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
571 /* Display response */
573 d_printf("plaintext password authentication %s\n",
574 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
576 if (response.data.auth.nt_status)
577 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
578 response.data.auth.nt_status_string,
579 response.data.auth.nt_status,
580 response.data.auth.error_string);
582 return result == NSS_STATUS_SUCCESS;
585 /* Authenticate a user with a challenge/response */
587 static BOOL wbinfo_auth_crap(char *username)
589 struct winbindd_request request;
590 struct winbindd_response response;
591 NSS_STATUS result;
592 fstring name_user;
593 fstring name_domain;
594 fstring pass;
595 char *p;
597 /* Send off request */
599 ZERO_STRUCT(request);
600 ZERO_STRUCT(response);
602 p = strchr(username, '%');
604 if (p) {
605 *p = 0;
606 fstrcpy(pass, p + 1);
609 parse_wbinfo_domain_user(username, name_domain, name_user);
611 if (push_utf8_fstring(request.data.auth_crap.user, name_user) == -1) {
612 d_printf("unable to create utf8 string for '%s'\n",
613 name_user);
614 return False;
617 if (push_utf8_fstring(request.data.auth_crap.domain,
618 name_domain) == -1) {
619 d_printf("unable to create utf8 string for '%s'\n",
620 name_domain);
621 return False;
624 generate_random_buffer(request.data.auth_crap.chal, 8);
626 SMBencrypt(pass, request.data.auth_crap.chal,
627 (uchar *)request.data.auth_crap.lm_resp);
628 SMBNTencrypt(pass, request.data.auth_crap.chal,
629 (uchar *)request.data.auth_crap.nt_resp);
631 request.data.auth_crap.lm_resp_len = 24;
632 request.data.auth_crap.nt_resp_len = 24;
634 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
636 /* Display response */
638 d_printf("challenge/response password authentication %s\n",
639 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
641 if (response.data.auth.nt_status)
642 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
643 response.data.auth.nt_status_string,
644 response.data.auth.nt_status,
645 response.data.auth.error_string);
647 return result == NSS_STATUS_SUCCESS;
650 /* Authenticate a user with a plaintext password and set a token */
652 static BOOL wbinfo_klog(char *username)
654 struct winbindd_request request;
655 struct winbindd_response response;
656 NSS_STATUS result;
657 char *p;
659 /* Send off request */
661 ZERO_STRUCT(request);
662 ZERO_STRUCT(response);
664 p = strchr(username, '%');
666 if (p) {
667 *p = 0;
668 fstrcpy(request.data.auth.user, username);
669 fstrcpy(request.data.auth.pass, p + 1);
670 *p = '%';
671 } else {
672 fstrcpy(request.data.auth.user, username);
673 fstrcpy(request.data.auth.pass, getpass("Password: "));
676 request.flags |= WBFLAG_PAM_AFS_TOKEN;
678 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
680 /* Display response */
682 d_printf("plaintext password authentication %s\n",
683 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
685 if (response.data.auth.nt_status)
686 d_printf("error code was %s (0x%x)\nerror messsage was: %s\n",
687 response.data.auth.nt_status_string,
688 response.data.auth.nt_status,
689 response.data.auth.error_string);
691 if (result != NSS_STATUS_SUCCESS)
692 return False;
694 if (response.extra_data == NULL) {
695 d_printf("Did not get token data\n");
696 return False;
699 if (!afs_settoken_str((char *)response.extra_data)) {
700 d_printf("Could not set token\n");
701 return False;
704 d_printf("Successfully created AFS token\n");
705 return True;
708 /******************************************************************
709 create a winbindd user
710 ******************************************************************/
712 static BOOL wbinfo_create_user(char *username)
714 struct winbindd_request request;
715 struct winbindd_response response;
716 NSS_STATUS result;
718 /* Send off request */
720 ZERO_STRUCT(request);
721 ZERO_STRUCT(response);
723 request.flags = WBFLAG_ALLOCATE_RID;
724 fstrcpy(request.data.acct_mgt.username, username);
726 result = winbindd_request(WINBINDD_CREATE_USER, &request, &response);
728 if ( result == NSS_STATUS_SUCCESS )
729 d_printf("New RID is %d\n", response.data.rid);
731 return result == NSS_STATUS_SUCCESS;
734 /******************************************************************
735 remove a winbindd user
736 ******************************************************************/
738 static BOOL wbinfo_delete_user(char *username)
740 struct winbindd_request request;
741 struct winbindd_response response;
742 NSS_STATUS result;
744 /* Send off request */
746 ZERO_STRUCT(request);
747 ZERO_STRUCT(response);
749 fstrcpy(request.data.acct_mgt.username, username);
751 result = winbindd_request(WINBINDD_DELETE_USER, &request, &response);
753 return result == NSS_STATUS_SUCCESS;
756 /******************************************************************
757 create a winbindd group
758 ******************************************************************/
760 static BOOL wbinfo_create_group(char *groupname)
762 struct winbindd_request request;
763 struct winbindd_response response;
764 NSS_STATUS result;
766 /* Send off request */
768 ZERO_STRUCT(request);
769 ZERO_STRUCT(response);
771 fstrcpy(request.data.acct_mgt.groupname, groupname);
773 result = winbindd_request(WINBINDD_CREATE_GROUP, &request, &response);
775 return result == NSS_STATUS_SUCCESS;
778 /******************************************************************
779 remove a winbindd group
780 ******************************************************************/
782 static BOOL wbinfo_delete_group(char *groupname)
784 struct winbindd_request request;
785 struct winbindd_response response;
786 NSS_STATUS result;
788 /* Send off request */
790 ZERO_STRUCT(request);
791 ZERO_STRUCT(response);
793 fstrcpy(request.data.acct_mgt.groupname, groupname);
795 result = winbindd_request(WINBINDD_DELETE_GROUP, &request, &response);
797 return result == NSS_STATUS_SUCCESS;
800 /******************************************************************
801 parse a string in the form user:group
802 ******************************************************************/
804 static BOOL parse_user_group( const char *string, fstring user, fstring group )
806 char *p;
808 if ( !string )
809 return False;
811 if ( !(p = strchr( string, ':' )) )
812 return False;
814 *p = '\0';
815 p++;
817 fstrcpy( user, string );
818 fstrcpy( group, p );
820 return True;
823 /******************************************************************
824 add a user to a winbindd group
825 ******************************************************************/
827 static BOOL wbinfo_add_user_to_group(char *string)
829 struct winbindd_request request;
830 struct winbindd_response response;
831 NSS_STATUS result;
833 /* Send off request */
835 ZERO_STRUCT(request);
836 ZERO_STRUCT(response);
838 if ( !parse_user_group( string, request.data.acct_mgt.username,
839 request.data.acct_mgt.groupname))
841 d_printf("Can't parse user:group from %s\n", string);
842 return False;
845 result = winbindd_request(WINBINDD_ADD_USER_TO_GROUP, &request, &response);
847 return result == NSS_STATUS_SUCCESS;
850 /******************************************************************
851 remove a user from a winbindd group
852 ******************************************************************/
854 static BOOL wbinfo_remove_user_from_group(char *string)
856 struct winbindd_request request;
857 struct winbindd_response response;
858 NSS_STATUS result;
860 /* Send off request */
862 ZERO_STRUCT(request);
863 ZERO_STRUCT(response);
865 if ( !parse_user_group( string, request.data.acct_mgt.username,
866 request.data.acct_mgt.groupname))
868 d_printf("Can't parse user:group from %s\n", string);
869 return False;
872 result = winbindd_request(WINBINDD_REMOVE_USER_FROM_GROUP, &request, &response);
874 return result == NSS_STATUS_SUCCESS;
877 /* Print domain users */
879 static BOOL print_domain_users(const char *domain)
881 struct winbindd_request request;
882 struct winbindd_response response;
883 const char *extra_data;
884 fstring name;
886 /* Send request to winbind daemon */
888 ZERO_STRUCT(request);
889 ZERO_STRUCT(response);
891 if (domain) {
892 /* '.' is the special sign for our own domwin */
893 if ( strequal(domain, ".") )
894 fstrcpy( request.domain_name, lp_workgroup() );
895 else
896 fstrcpy( request.domain_name, domain );
899 if (winbindd_request(WINBINDD_LIST_USERS, &request, &response) !=
900 NSS_STATUS_SUCCESS)
901 return False;
903 /* Look through extra data */
905 if (!response.extra_data)
906 return False;
908 extra_data = (const char *)response.extra_data;
910 while(next_token(&extra_data, name, ",", sizeof(fstring)))
911 d_printf("%s\n", name);
913 SAFE_FREE(response.extra_data);
915 return True;
918 /* Print domain groups */
920 static BOOL print_domain_groups(const char *domain)
922 struct winbindd_request request;
923 struct winbindd_response response;
924 const char *extra_data;
925 fstring name;
927 ZERO_STRUCT(request);
928 ZERO_STRUCT(response);
930 if (domain) {
931 if ( strequal(domain, ".") )
932 fstrcpy( request.domain_name, lp_workgroup() );
933 else
934 fstrcpy( request.domain_name, domain );
937 if (winbindd_request(WINBINDD_LIST_GROUPS, &request, &response) !=
938 NSS_STATUS_SUCCESS)
939 return False;
941 /* Look through extra data */
943 if (!response.extra_data)
944 return False;
946 extra_data = (const char *)response.extra_data;
948 while(next_token(&extra_data, name, ",", sizeof(fstring)))
949 d_printf("%s\n", name);
951 SAFE_FREE(response.extra_data);
953 return True;
956 /* Set the authorised user for winbindd access in secrets.tdb */
958 static BOOL wbinfo_set_auth_user(char *username)
960 const char *password;
961 char *p;
962 fstring user, domain;
964 /* Separate into user and password */
966 parse_wbinfo_domain_user(username, domain, user);
968 p = strchr(user, '%');
970 if (p != NULL) {
971 *p = 0;
972 password = p+1;
973 } else {
974 char *thepass = getpass("Password: ");
975 if (thepass) {
976 password = thepass;
977 } else
978 password = "";
981 /* Store or remove DOMAIN\username%password in secrets.tdb */
983 secrets_init();
985 if (user[0]) {
987 if (!secrets_store(SECRETS_AUTH_USER, user,
988 strlen(user) + 1)) {
989 d_fprintf(stderr, "error storing username\n");
990 return False;
993 /* We always have a domain name added by the
994 parse_wbinfo_domain_user() function. */
996 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
997 strlen(domain) + 1)) {
998 d_fprintf(stderr, "error storing domain name\n");
999 return False;
1002 } else {
1003 secrets_delete(SECRETS_AUTH_USER);
1004 secrets_delete(SECRETS_AUTH_DOMAIN);
1007 if (password[0]) {
1009 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
1010 strlen(password) + 1)) {
1011 d_fprintf(stderr, "error storing password\n");
1012 return False;
1015 } else
1016 secrets_delete(SECRETS_AUTH_PASSWORD);
1018 return True;
1021 static void wbinfo_get_auth_user(void)
1023 char *user, *domain, *password;
1025 /* Lift data from secrets file */
1027 secrets_fetch_ipc_userpass(&user, &domain, &password);
1029 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
1031 SAFE_FREE(user);
1032 SAFE_FREE(domain);
1033 SAFE_FREE(password);
1034 d_printf("No authorised user configured\n");
1035 return;
1038 /* Pretty print authorised user info */
1040 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
1041 user, password ? "%" : "", password ? password : "");
1043 SAFE_FREE(user);
1044 SAFE_FREE(domain);
1045 SAFE_FREE(password);
1048 static BOOL wbinfo_ping(void)
1050 NSS_STATUS result;
1052 result = winbindd_request(WINBINDD_PING, NULL, NULL);
1054 /* Display response */
1056 d_printf("Ping to winbindd %s on fd %d\n",
1057 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", winbindd_fd);
1059 return result == NSS_STATUS_SUCCESS;
1062 /* Main program */
1064 enum {
1065 OPT_SET_AUTH_USER = 1000,
1066 OPT_GET_AUTH_USER,
1067 OPT_DOMAIN_NAME,
1068 OPT_SEQUENCE,
1069 OPT_USERSIDS
1072 int main(int argc, char **argv)
1074 int opt;
1076 poptContext pc;
1077 static char *string_arg;
1078 static char *opt_domain_name;
1079 static int int_arg;
1080 int result = 1;
1082 struct poptOption long_options[] = {
1083 POPT_AUTOHELP
1085 /* longName, shortName, argInfo, argPtr, value, descrip,
1086 argDesc */
1088 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1089 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1090 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1091 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1092 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1093 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1094 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1095 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1096 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1097 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1098 { "allocate-rid", 'A', POPT_ARG_NONE, 0, 'A', "Get a new RID out of idmap" },
1099 { "create-user", 'c', POPT_ARG_STRING, &string_arg, 'c', "Create a local user account", "name" },
1100 { "delete-user", 'x', POPT_ARG_STRING, &string_arg, 'x', "Delete a local user account", "name" },
1101 { "create-group", 'C', POPT_ARG_STRING, &string_arg, 'C', "Create a local group", "name" },
1102 { "delete-group", 'X', POPT_ARG_STRING, &string_arg, 'X', "Delete a local group", "name" },
1103 { "add-to-group", 'o', POPT_ARG_STRING, &string_arg, 'o', "Add user to group", "user:group" },
1104 { "del-from-group", 'O', POPT_ARG_STRING, &string_arg, 'O', "Remove user from group", "user:group" },
1105 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1106 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1107 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1108 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1109 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1110 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1111 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1112 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1113 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1114 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1115 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1116 #ifdef WITH_FAKE_KASERVER
1117 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1118 #endif
1119 POPT_COMMON_VERSION
1120 POPT_TABLEEND
1123 /* Samba client initialisation */
1125 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
1126 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1127 dyn_CONFIGFILE, strerror(errno));
1128 exit(1);
1131 if (!init_names())
1132 return 1;
1134 load_interfaces();
1136 /* Parse options */
1138 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1140 /* Parse command line options */
1142 if (argc == 1) {
1143 poptPrintHelp(pc, stderr, 0);
1144 return 1;
1147 while((opt = poptGetNextOpt(pc)) != -1) {
1148 /* get the generic configuration parameters like --domain */
1151 poptFreeContext(pc);
1153 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1154 POPT_CONTEXT_KEEP_FIRST);
1156 while((opt = poptGetNextOpt(pc)) != -1) {
1157 switch (opt) {
1158 case 'u':
1159 if (!print_domain_users(opt_domain_name)) {
1160 d_printf("Error looking up domain users\n");
1161 goto done;
1163 break;
1164 case 'g':
1165 if (!print_domain_groups(opt_domain_name)) {
1166 d_printf("Error looking up domain groups\n");
1167 goto done;
1169 break;
1170 case 's':
1171 if (!wbinfo_lookupsid(string_arg)) {
1172 d_printf("Could not lookup sid %s\n", string_arg);
1173 goto done;
1175 break;
1176 case 'n':
1177 if (!wbinfo_lookupname(string_arg)) {
1178 d_printf("Could not lookup name %s\n", string_arg);
1179 goto done;
1181 break;
1182 case 'N':
1183 if (!wbinfo_wins_byname(string_arg)) {
1184 d_printf("Could not lookup WINS by name %s\n", string_arg);
1185 goto done;
1187 break;
1188 case 'I':
1189 if (!wbinfo_wins_byip(string_arg)) {
1190 d_printf("Could not lookup WINS by IP %s\n", string_arg);
1191 goto done;
1193 break;
1194 case 'U':
1195 if (!wbinfo_uid_to_sid(int_arg)) {
1196 d_printf("Could not convert uid %d to sid\n", int_arg);
1197 goto done;
1199 break;
1200 case 'G':
1201 if (!wbinfo_gid_to_sid(int_arg)) {
1202 d_printf("Could not convert gid %d to sid\n",
1203 int_arg);
1204 goto done;
1206 break;
1207 case 'S':
1208 if (!wbinfo_sid_to_uid(string_arg)) {
1209 d_printf("Could not convert sid %s to uid\n",
1210 string_arg);
1211 goto done;
1213 break;
1214 case 'Y':
1215 if (!wbinfo_sid_to_gid(string_arg)) {
1216 d_printf("Could not convert sid %s to gid\n",
1217 string_arg);
1218 goto done;
1220 break;
1221 case 'A':
1222 if (!wbinfo_allocate_rid()) {
1223 d_printf("Could not allocate a RID\n");
1224 goto done;
1226 break;
1227 case 't':
1228 if (!wbinfo_check_secret()) {
1229 d_printf("Could not check secret\n");
1230 goto done;
1232 break;
1233 case 'm':
1234 if (!wbinfo_list_domains()) {
1235 d_printf("Could not list trusted domains\n");
1236 goto done;
1238 break;
1239 case OPT_SEQUENCE:
1240 if (!wbinfo_show_sequence(opt_domain_name)) {
1241 d_printf("Could not show sequence numbers\n");
1242 goto done;
1244 break;
1245 case 'D':
1246 if (!wbinfo_domain_info(string_arg)) {
1247 d_printf("Could not get domain info\n");
1248 goto done;
1250 break;
1251 case 'r':
1252 if (!wbinfo_get_usergroups(string_arg)) {
1253 d_printf("Could not get groups for user %s\n",
1254 string_arg);
1255 goto done;
1257 break;
1258 case OPT_USERSIDS:
1259 if (!wbinfo_get_usersids(string_arg)) {
1260 d_printf("Could not get group SIDs for user SID %s\n",
1261 string_arg);
1262 goto done;
1264 break;
1265 case 'a': {
1266 BOOL got_error = False;
1268 if (!wbinfo_auth(string_arg)) {
1269 d_printf("Could not authenticate user %s with "
1270 "plaintext password\n", string_arg);
1271 got_error = True;
1274 if (!wbinfo_auth_crap(string_arg)) {
1275 d_printf("Could not authenticate user %s with "
1276 "challenge/response\n", string_arg);
1277 got_error = True;
1280 if (got_error)
1281 goto done;
1282 break;
1284 case 'k':
1285 if (!wbinfo_klog(string_arg)) {
1286 d_printf("Could not klog user\n");
1287 goto done;
1289 break;
1290 case 'c':
1291 if ( !wbinfo_create_user(string_arg) ) {
1292 d_printf("Could not create user account\n");
1293 goto done;
1295 break;
1296 case 'C':
1297 if ( !wbinfo_create_group(string_arg) ) {
1298 d_printf("Could not create group\n");
1299 goto done;
1301 break;
1302 case 'o':
1303 if ( !wbinfo_add_user_to_group(string_arg) ) {
1304 d_printf("Could not add user to group\n");
1305 goto done;
1307 break;
1308 case 'O':
1309 if ( !wbinfo_remove_user_from_group(string_arg) ) {
1310 d_printf("Could not remove user from group\n");
1311 goto done;
1313 break;
1314 case 'x':
1315 if ( !wbinfo_delete_user(string_arg) ) {
1316 d_printf("Could not delete user account\n");
1317 goto done;
1319 break;
1320 case 'X':
1321 if ( !wbinfo_delete_group(string_arg) ) {
1322 d_printf("Could not delete group\n");
1323 goto done;
1325 break;
1326 case 'p':
1327 if (!wbinfo_ping()) {
1328 d_printf("could not ping winbindd!\n");
1329 goto done;
1331 break;
1332 case OPT_SET_AUTH_USER:
1333 wbinfo_set_auth_user(string_arg);
1334 break;
1335 case OPT_GET_AUTH_USER:
1336 wbinfo_get_auth_user();
1337 break;
1338 /* generic configuration options */
1339 case OPT_DOMAIN_NAME:
1340 break;
1341 default:
1342 d_fprintf(stderr, "Invalid option\n");
1343 poptPrintHelp(pc, stderr, 0);
1344 goto done;
1348 result = 0;
1350 /* Exit code */
1352 done:
1353 poptFreeContext(pc);
1354 return result;