2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett 2002-2007
8 Copyright (C) Volker Lendecke 2009
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "popt_common.h"
26 #include "winbind_client.h"
27 #include "libwbclient/wbclient.h"
28 #include "lib/popt/popt.h"
29 #include "../libcli/auth/libcli_auth.h"
30 #if (_SAMBA_BUILD_) >= 4
31 #include "lib/cmdline/popt_common.h"
36 #define DBGC_CLASS DBGC_WINBIND
39 static struct wbcInterfaceDetails
*init_interface_details(void)
41 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
42 static struct wbcInterfaceDetails
*details
;
48 wbc_status
= wbcInterfaceDetails(&details
);
49 if (!WBC_ERROR_IS_OK(wbc_status
)) {
50 d_fprintf(stderr
, "could not obtain winbind interface "
51 "details: %s\n", wbcErrorString(wbc_status
));
57 static char winbind_separator(void)
59 struct wbcInterfaceDetails
*details
;
66 details
= init_interface_details();
69 d_fprintf(stderr
, "could not obtain winbind separator!\n");
73 sep
= details
->winbind_separator
;
77 d_fprintf(stderr
, "winbind separator was NULL!\n");
84 static const char *get_winbind_domain(void)
86 static struct wbcInterfaceDetails
*details
;
88 details
= init_interface_details();
91 d_fprintf(stderr
, "could not obtain winbind domain name!\n");
95 return details
->netbios_domain
;
98 static const char *get_winbind_netbios_name(void)
100 static struct wbcInterfaceDetails
*details
;
102 details
= init_interface_details();
105 d_fprintf(stderr
, "could not obtain winbind netbios name!\n");
109 return details
->netbios_name
;
112 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
113 form DOMAIN/user into a domain and a user */
115 static bool parse_wbinfo_domain_user(const char *domuser
, fstring domain
,
119 char *p
= strchr(domuser
,winbind_separator());
122 /* Maybe it was a UPN? */
123 if ((p
= strchr(domuser
, '@')) != NULL
) {
125 fstrcpy(user
, domuser
);
129 fstrcpy(user
, domuser
);
130 fstrcpy(domain
, get_winbind_domain());
135 fstrcpy(domain
, domuser
);
136 domain
[PTR_DIFF(p
, domuser
)] = 0;
142 /* Parse string of "uid,sid" or "gid,sid" into separate int and string values.
143 * Return true if input was valid, false otherwise. */
144 static bool parse_mapping_arg(char *arg
, int *id
, char **sid
)
151 tmp
= strtok(arg
, ",");
152 *sid
= strtok(NULL
, ",");
154 if (!tmp
|| !*tmp
|| !*sid
|| !**sid
)
157 /* Because atoi() can return 0 on invalid input, which would be a valid
158 * UID/GID we must use strtoul() and do error checking */
159 *id
= strtoul(tmp
, &endptr
, 10);
161 if (endptr
[0] != '\0')
167 /* pull pwent info for a given user */
169 static bool wbinfo_get_userinfo(char *user
)
171 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
172 struct passwd
*pwd
= NULL
;
174 wbc_status
= wbcGetpwnam(user
, &pwd
);
175 if (!WBC_ERROR_IS_OK(wbc_status
)) {
176 d_fprintf(stderr
, "failed to call wbcGetpwnam: %s\n",
177 wbcErrorString(wbc_status
));
181 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
184 (unsigned int)pwd
->pw_uid
,
185 (unsigned int)pwd
->pw_gid
,
193 /* pull pwent info for a given uid */
194 static bool wbinfo_get_uidinfo(int uid
)
196 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
197 struct passwd
*pwd
= NULL
;
199 wbc_status
= wbcGetpwuid(uid
, &pwd
);
200 if (!WBC_ERROR_IS_OK(wbc_status
)) {
201 d_fprintf(stderr
, "failed to call wbcGetpwuid: %s\n",
202 wbcErrorString(wbc_status
));
206 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
209 (unsigned int)pwd
->pw_uid
,
210 (unsigned int)pwd
->pw_gid
,
218 static bool wbinfo_get_user_sidinfo(const char *sid_str
)
220 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
221 struct passwd
*pwd
= NULL
;
222 struct wbcDomainSid sid
;
224 wbc_status
= wbcStringToSid(sid_str
, &sid
);
225 wbc_status
= wbcGetpwsid(&sid
, &pwd
);
226 if (!WBC_ERROR_IS_OK(wbc_status
)) {
227 d_fprintf(stderr
, "failed to call wbcGetpwsid: %s\n",
228 wbcErrorString(wbc_status
));
232 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
235 (unsigned int)pwd
->pw_uid
,
236 (unsigned int)pwd
->pw_gid
,
245 /* pull grent for a given group */
246 static bool wbinfo_get_groupinfo(const char *group
)
248 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
252 wbc_status
= wbcGetgrnam(group
, &grp
);
253 if (!WBC_ERROR_IS_OK(wbc_status
)) {
254 d_fprintf(stderr
, "failed to call wbcGetgrnam: %s\n",
255 wbcErrorString(wbc_status
));
259 d_printf("%s:%s:%u:",
262 (unsigned int)grp
->gr_gid
);
265 while (*mem
!= NULL
) {
266 d_printf("%s%s", *mem
, *(mem
+1) != NULL
? "," : "");
276 /* pull grent for a given gid */
277 static bool wbinfo_get_gidinfo(int gid
)
279 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
283 wbc_status
= wbcGetgrgid(gid
, &grp
);
284 if (!WBC_ERROR_IS_OK(wbc_status
)) {
285 d_fprintf(stderr
, "failed to call wbcGetgrgid: %s\n",
286 wbcErrorString(wbc_status
));
290 d_printf("%s:%s:%u:",
293 (unsigned int)grp
->gr_gid
);
296 while (*mem
!= NULL
) {
297 d_printf("%s%s", *mem
, *(mem
+1) != NULL
? "," : "");
307 /* List groups a user is a member of */
309 static bool wbinfo_get_usergroups(const char *user
)
311 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
314 gid_t
*groups
= NULL
;
318 wbc_status
= wbcGetGroups(user
, &num_groups
, &groups
);
319 if (!WBC_ERROR_IS_OK(wbc_status
)) {
320 d_fprintf(stderr
, "failed to call wbcGetGroups: %s\n",
321 wbcErrorString(wbc_status
));
325 for (i
= 0; i
< num_groups
; i
++) {
326 d_printf("%d\n", (int)groups
[i
]);
329 wbcFreeMemory(groups
);
335 /* List group SIDs a user SID is a member of */
336 static bool wbinfo_get_usersids(const char *user_sid_str
)
338 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
341 struct wbcDomainSid user_sid
, *sids
= NULL
;
345 wbc_status
= wbcStringToSid(user_sid_str
, &user_sid
);
346 if (!WBC_ERROR_IS_OK(wbc_status
)) {
347 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
348 wbcErrorString(wbc_status
));
352 wbc_status
= wbcLookupUserSids(&user_sid
, false, &num_sids
, &sids
);
353 if (!WBC_ERROR_IS_OK(wbc_status
)) {
354 d_fprintf(stderr
, "failed to call wbcLookupUserSids: %s\n",
355 wbcErrorString(wbc_status
));
359 for (i
= 0; i
< num_sids
; i
++) {
361 wbc_status
= wbcSidToString(&sids
[i
], &str
);
362 if (!WBC_ERROR_IS_OK(wbc_status
)) {
363 d_fprintf(stderr
, "failed to call wbcSidToString: "
364 "%s\n", wbcErrorString(wbc_status
));
368 d_printf("%s\n", str
);
377 static bool wbinfo_get_userdomgroups(const char *user_sid_str
)
379 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
382 struct wbcDomainSid user_sid
, *sids
= NULL
;
386 wbc_status
= wbcStringToSid(user_sid_str
, &user_sid
);
387 if (!WBC_ERROR_IS_OK(wbc_status
)) {
388 d_fprintf(stderr
, "failed to call wbcSidToString: %s\n",
389 wbcErrorString(wbc_status
));
393 wbc_status
= wbcLookupUserSids(&user_sid
, true, &num_sids
, &sids
);
394 if (!WBC_ERROR_IS_OK(wbc_status
)) {
395 d_fprintf(stderr
, "failed to call wbcLookupUserSids: %s\n",
396 wbcErrorString(wbc_status
));
400 for (i
= 0; i
< num_sids
; i
++) {
402 wbc_status
= wbcSidToString(&sids
[i
], &str
);
403 if (!WBC_ERROR_IS_OK(wbc_status
)) {
404 d_fprintf(stderr
, "failed to call wbcSidToString: "
405 "%s\n", wbcErrorString(wbc_status
));
409 d_printf("%s\n", str
);
418 static bool wbinfo_get_sidaliases(const char *domain
,
419 const char *user_sid_str
)
421 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
422 struct wbcDomainInfo
*dinfo
= NULL
;
424 struct wbcDomainSid user_sid
;
425 uint32_t *alias_rids
= NULL
;
426 uint32_t num_alias_rids
;
427 char *domain_sid_str
= NULL
;
430 if ((domain
== NULL
) || (strequal(domain
, ".")) ||
431 (domain
[0] == '\0')) {
432 domain
= get_winbind_domain();
437 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
438 if (!WBC_ERROR_IS_OK(wbc_status
)) {
439 d_fprintf(stderr
, "wbcDomainInfo(%s) failed: %s\n", domain
,
440 wbcErrorString(wbc_status
));
443 wbc_status
= wbcStringToSid(user_sid_str
, &user_sid
);
444 if (!WBC_ERROR_IS_OK(wbc_status
)) {
448 wbc_status
= wbcGetSidAliases(&dinfo
->sid
, &user_sid
, 1,
449 &alias_rids
, &num_alias_rids
);
450 if (!WBC_ERROR_IS_OK(wbc_status
)) {
454 wbc_status
= wbcSidToString(&dinfo
->sid
, &domain_sid_str
);
455 if (!WBC_ERROR_IS_OK(wbc_status
)) {
459 for (i
= 0; i
< num_alias_rids
; i
++) {
460 d_printf("%s-%d\n", domain_sid_str
, alias_rids
[i
]);
463 wbcFreeMemory(alias_rids
);
466 if (domain_sid_str
) {
467 wbcFreeMemory(domain_sid_str
);
470 wbcFreeMemory(dinfo
);
472 return (WBC_ERR_SUCCESS
== wbc_status
);
476 /* Convert NetBIOS name to IP */
478 static bool wbinfo_wins_byname(const char *name
)
480 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
483 wbc_status
= wbcResolveWinsByName(name
, &ip
);
484 if (!WBC_ERROR_IS_OK(wbc_status
)) {
485 d_fprintf(stderr
, "failed to call wbcResolveWinsByName: %s\n",
486 wbcErrorString(wbc_status
));
490 /* Display response */
492 d_printf("%s\n", ip
);
499 /* Convert IP to NetBIOS name */
501 static bool wbinfo_wins_byip(const char *ip
)
503 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
506 wbc_status
= wbcResolveWinsByIP(ip
, &name
);
507 if (!WBC_ERROR_IS_OK(wbc_status
)) {
508 d_fprintf(stderr
, "failed to call wbcResolveWinsByIP: %s\n",
509 wbcErrorString(wbc_status
));
513 /* Display response */
515 d_printf("%s\n", name
);
522 /* List all/trusted domains */
524 static bool wbinfo_list_domains(bool list_all_domains
, bool verbose
)
526 struct wbcDomainInfo
*domain_list
= NULL
;
528 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
529 bool print_all
= !list_all_domains
&& verbose
;
532 wbc_status
= wbcListTrusts(&domain_list
, &num_domains
);
533 if (!WBC_ERROR_IS_OK(wbc_status
)) {
534 d_fprintf(stderr
, "failed to call wbcListTrusts: %s\n",
535 wbcErrorString(wbc_status
));
540 d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n",
541 "Domain Name", "DNS Domain", "Trust Type",
542 "Transitive", "In", "Out");
545 for (i
=0; i
<num_domains
; i
++) {
547 d_printf("%-16s", domain_list
[i
].short_name
);
549 d_printf("%s", domain_list
[i
].short_name
);
554 d_printf("%-24s", domain_list
[i
].dns_name
);
556 switch(domain_list
[i
].trust_type
) {
557 case WBC_DOMINFO_TRUSTTYPE_NONE
:
560 case WBC_DOMINFO_TRUSTTYPE_FOREST
:
563 case WBC_DOMINFO_TRUSTTYPE_EXTERNAL
:
564 d_printf("External ");
566 case WBC_DOMINFO_TRUSTTYPE_IN_FOREST
:
567 d_printf("In-Forest ");
571 if (domain_list
[i
].trust_flags
& WBC_DOMINFO_TRUST_TRANSITIVE
) {
577 if (domain_list
[i
].trust_flags
& WBC_DOMINFO_TRUST_INCOMING
) {
583 if (domain_list
[i
].trust_flags
& WBC_DOMINFO_TRUST_OUTGOING
) {
595 /* List own domain */
597 static bool wbinfo_list_own_domain(void)
599 d_printf("%s\n", get_winbind_domain());
604 /* show sequence numbers */
605 static bool wbinfo_show_sequence(const char *domain
)
607 d_printf("This command has been deprecated. Please use the "
608 "--online-status option instead.\n");
612 /* show sequence numbers */
613 static bool wbinfo_show_onlinestatus(const char *domain
)
615 struct wbcDomainInfo
*domain_list
= NULL
;
617 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
620 wbc_status
= wbcListTrusts(&domain_list
, &num_domains
);
621 if (!WBC_ERROR_IS_OK(wbc_status
)) {
622 d_fprintf(stderr
, "failed to call wbcListTrusts: %s\n",
623 wbcErrorString(wbc_status
));
627 for (i
=0; i
<num_domains
; i
++) {
631 if (!strequal(domain_list
[i
].short_name
, domain
)) {
636 is_offline
= (domain_list
[i
].domain_flags
&
637 WBC_DOMINFO_DOMAIN_OFFLINE
);
639 d_printf("%s : %s\n",
640 domain_list
[i
].short_name
,
641 is_offline
? "offline" : "online" );
648 /* Show domain info */
650 static bool wbinfo_domain_info(const char *domain
)
652 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
653 struct wbcDomainInfo
*dinfo
= NULL
;
654 char *sid_str
= NULL
;
656 if ((domain
== NULL
) || (strequal(domain
, ".")) || (domain
[0] == '\0')){
657 domain
= get_winbind_domain();
662 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
663 if (!WBC_ERROR_IS_OK(wbc_status
)) {
664 d_fprintf(stderr
, "failed to call wbcDomainInfo: %s\n",
665 wbcErrorString(wbc_status
));
669 wbc_status
= wbcSidToString(&dinfo
->sid
, &sid_str
);
670 if (!WBC_ERROR_IS_OK(wbc_status
)) {
671 d_fprintf(stderr
, "failed to call wbcSidToString: %s\n",
672 wbcErrorString(wbc_status
));
673 wbcFreeMemory(dinfo
);
677 /* Display response */
679 d_printf("Name : %s\n", dinfo
->short_name
);
680 d_printf("Alt_Name : %s\n", dinfo
->dns_name
);
682 d_printf("SID : %s\n", sid_str
);
684 d_printf("Active Directory : %s\n",
685 (dinfo
->domain_flags
& WBC_DOMINFO_DOMAIN_AD
) ? "Yes" : "No");
686 d_printf("Native : %s\n",
687 (dinfo
->domain_flags
& WBC_DOMINFO_DOMAIN_NATIVE
) ?
690 d_printf("Primary : %s\n",
691 (dinfo
->domain_flags
& WBC_DOMINFO_DOMAIN_PRIMARY
) ?
694 wbcFreeMemory(sid_str
);
695 wbcFreeMemory(dinfo
);
700 /* Get a foreign DC's name */
701 static bool wbinfo_getdcname(const char *domain_name
)
703 struct winbindd_request request
;
704 struct winbindd_response response
;
706 ZERO_STRUCT(request
);
707 ZERO_STRUCT(response
);
709 fstrcpy(request
.domain_name
, domain_name
);
713 if (winbindd_request_response(WINBINDD_GETDCNAME
, &request
,
714 &response
) != NSS_STATUS_SUCCESS
) {
715 d_fprintf(stderr
, "Could not get dc name for %s\n",domain_name
);
719 /* Display response */
721 d_printf("%s\n", response
.data
.dc_name
);
727 static bool wbinfo_dsgetdcname(const char *domain_name
, uint32_t flags
)
729 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
730 struct wbcDomainControllerInfoEx
*dc_info
;
733 wbc_status
= wbcLookupDomainControllerEx(domain_name
, NULL
, NULL
,
734 flags
| DS_DIRECTORY_SERVICE_REQUIRED
,
736 if (!WBC_ERROR_IS_OK(wbc_status
)) {
737 printf("Could not find dc for %s\n", domain_name
);
741 wbcGuidToString(dc_info
->domain_guid
, &str
);
743 d_printf("%s\n", dc_info
->dc_unc
);
744 d_printf("%s\n", dc_info
->dc_address
);
745 d_printf("%d\n", dc_info
->dc_address_type
);
746 d_printf("%s\n", str
);
747 d_printf("%s\n", dc_info
->domain_name
);
748 d_printf("%s\n", dc_info
->forest_name
);
749 d_printf("0x%08x\n", dc_info
->dc_flags
);
750 d_printf("%s\n", dc_info
->dc_site_name
);
751 d_printf("%s\n", dc_info
->client_site_name
);
756 /* Check trust account password */
758 static bool wbinfo_check_secret(const char *domain
)
760 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
761 struct wbcAuthErrorInfo
*error
= NULL
;
762 const char *domain_name
;
765 domain_name
= domain
;
767 domain_name
= get_winbind_domain();
770 wbc_status
= wbcCheckTrustCredentials(domain_name
, &error
);
772 d_printf("checking the trust secret for domain %s via RPC calls %s\n",
774 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
776 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
777 d_fprintf(stderr
, "error code was %s (0x%x)\n",
778 error
->nt_string
, error
->nt_status
);
779 wbcFreeMemory(error
);
781 if (!WBC_ERROR_IS_OK(wbc_status
)) {
782 d_fprintf(stderr
, "failed to call wbcCheckTrustCredentials: "
783 "%s\n", wbcErrorString(wbc_status
));
790 /* Change trust account password */
792 static bool wbinfo_change_secret(const char *domain
)
794 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
795 struct wbcAuthErrorInfo
*error
= NULL
;
796 const char *domain_name
;
799 domain_name
= domain
;
801 domain_name
= get_winbind_domain();
804 wbc_status
= wbcChangeTrustCredentials(domain_name
, &error
);
806 d_printf("changing the trust secret for domain %s via RPC calls %s\n",
808 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
810 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
811 d_fprintf(stderr
, "error code was %s (0x%x)\n",
812 error
->nt_string
, error
->nt_status
);
813 wbcFreeMemory(error
);
815 if (!WBC_ERROR_IS_OK(wbc_status
)) {
816 d_fprintf(stderr
, "failed to call wbcChangeTrustCredentials: "
817 "%s\n", wbcErrorString(wbc_status
));
824 /* Check DC connection */
826 static bool wbinfo_ping_dc(void)
828 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
829 struct wbcAuthErrorInfo
*error
= NULL
;
831 wbc_status
= wbcPingDc(NULL
, &error
);
833 d_printf("checking the NETLOGON dc connection %s\n",
834 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
836 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
837 d_fprintf(stderr
, "error code was %s (0x%x)\n",
838 error
->nt_string
, error
->nt_status
);
839 wbcFreeMemory(error
);
841 if (!WBC_ERROR_IS_OK(wbc_status
)) {
842 d_fprintf(stderr
, "failed to call wbcPingDc: %s\n",
843 wbcErrorString(wbc_status
));
850 /* Convert uid to sid */
852 static bool wbinfo_uid_to_sid(uid_t uid
)
854 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
855 struct wbcDomainSid sid
;
856 char *sid_str
= NULL
;
860 wbc_status
= wbcUidToSid(uid
, &sid
);
861 if (!WBC_ERROR_IS_OK(wbc_status
)) {
862 d_fprintf(stderr
, "failed to call wbcUidToSid: %s\n",
863 wbcErrorString(wbc_status
));
867 wbc_status
= wbcSidToString(&sid
, &sid_str
);
868 if (!WBC_ERROR_IS_OK(wbc_status
)) {
869 d_fprintf(stderr
, "failed to call wbcSidToString: %s\n",
870 wbcErrorString(wbc_status
));
874 /* Display response */
876 d_printf("%s\n", sid_str
);
878 wbcFreeMemory(sid_str
);
883 /* Convert gid to sid */
885 static bool wbinfo_gid_to_sid(gid_t gid
)
887 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
888 struct wbcDomainSid sid
;
889 char *sid_str
= NULL
;
893 wbc_status
= wbcGidToSid(gid
, &sid
);
894 if (!WBC_ERROR_IS_OK(wbc_status
)) {
895 d_fprintf(stderr
, "failed to call wbcGidToSid: %s\n",
896 wbcErrorString(wbc_status
));
900 wbc_status
= wbcSidToString(&sid
, &sid_str
);
901 if (!WBC_ERROR_IS_OK(wbc_status
)) {
902 d_fprintf(stderr
, "failed to call wbcSidToString: %s\n",
903 wbcErrorString(wbc_status
));
907 /* Display response */
909 d_printf("%s\n", sid_str
);
911 wbcFreeMemory(sid_str
);
916 /* Convert sid to uid */
918 static bool wbinfo_sid_to_uid(const char *sid_str
)
920 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
921 struct wbcDomainSid sid
;
926 wbc_status
= wbcStringToSid(sid_str
, &sid
);
927 if (!WBC_ERROR_IS_OK(wbc_status
)) {
928 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
929 wbcErrorString(wbc_status
));
933 wbc_status
= wbcSidToUid(&sid
, &uid
);
934 if (!WBC_ERROR_IS_OK(wbc_status
)) {
935 d_fprintf(stderr
, "failed to call wbcSidToUid: %s\n",
936 wbcErrorString(wbc_status
));
940 /* Display response */
942 d_printf("%d\n", (int)uid
);
947 static bool wbinfo_sid_to_gid(const char *sid_str
)
949 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
950 struct wbcDomainSid sid
;
955 wbc_status
= wbcStringToSid(sid_str
, &sid
);
956 if (!WBC_ERROR_IS_OK(wbc_status
)) {
957 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
958 wbcErrorString(wbc_status
));
962 wbc_status
= wbcSidToGid(&sid
, &gid
);
963 if (!WBC_ERROR_IS_OK(wbc_status
)) {
964 d_fprintf(stderr
, "failed to call wbcSidToGid: %s\n",
965 wbcErrorString(wbc_status
));
969 /* Display response */
971 d_printf("%d\n", (int)gid
);
976 static bool wbinfo_allocate_uid(void)
978 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
983 wbc_status
= wbcAllocateUid(&uid
);
984 if (!WBC_ERROR_IS_OK(wbc_status
)) {
985 d_fprintf(stderr
, "failed to call wbcAllocateUid: %s\n",
986 wbcErrorString(wbc_status
));
990 /* Display response */
992 d_printf("New uid: %u\n", (unsigned int)uid
);
997 static bool wbinfo_allocate_gid(void)
999 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1004 wbc_status
= wbcAllocateGid(&gid
);
1005 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1006 d_fprintf(stderr
, "failed to call wbcAllocateGid: %s\n",
1007 wbcErrorString(wbc_status
));
1011 /* Display response */
1013 d_printf("New gid: %u\n", (unsigned int)gid
);
1018 static bool wbinfo_set_uid_mapping(uid_t uid
, const char *sid_str
)
1020 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1021 struct wbcDomainSid sid
;
1025 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1026 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1027 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1028 wbcErrorString(wbc_status
));
1032 wbc_status
= wbcSetUidMapping(uid
, &sid
);
1033 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1034 d_fprintf(stderr
, "failed to call wbcSetUidMapping: %s\n",
1035 wbcErrorString(wbc_status
));
1039 /* Display response */
1041 d_printf("uid %u now mapped to sid %s\n",
1042 (unsigned int)uid
, sid_str
);
1047 static bool wbinfo_set_gid_mapping(gid_t gid
, const char *sid_str
)
1049 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1050 struct wbcDomainSid sid
;
1054 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1055 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1056 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1057 wbcErrorString(wbc_status
));
1061 wbc_status
= wbcSetGidMapping(gid
, &sid
);
1062 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1063 d_fprintf(stderr
, "failed to call wbcSetGidMapping: %s\n",
1064 wbcErrorString(wbc_status
));
1068 /* Display response */
1070 d_printf("gid %u now mapped to sid %s\n",
1071 (unsigned int)gid
, sid_str
);
1076 static bool wbinfo_remove_uid_mapping(uid_t uid
, const char *sid_str
)
1078 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1079 struct wbcDomainSid sid
;
1083 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1084 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1085 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1086 wbcErrorString(wbc_status
));
1090 wbc_status
= wbcRemoveUidMapping(uid
, &sid
);
1091 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1092 d_fprintf(stderr
, "failed to call wbcRemoveUidMapping: %s\n",
1093 wbcErrorString(wbc_status
));
1097 /* Display response */
1099 d_printf("Removed uid %u to sid %s mapping\n",
1100 (unsigned int)uid
, sid_str
);
1105 static bool wbinfo_remove_gid_mapping(gid_t gid
, const char *sid_str
)
1107 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1108 struct wbcDomainSid sid
;
1112 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1113 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1114 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1115 wbcErrorString(wbc_status
));
1119 wbc_status
= wbcRemoveGidMapping(gid
, &sid
);
1120 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1121 d_fprintf(stderr
, "failed to call wbcRemoveGidMapping: %s\n",
1122 wbcErrorString(wbc_status
));
1126 /* Display response */
1128 d_printf("Removed gid %u to sid %s mapping\n",
1129 (unsigned int)gid
, sid_str
);
1134 /* Convert sid to string */
1136 static bool wbinfo_lookupsid(const char *sid_str
)
1138 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1139 struct wbcDomainSid sid
;
1142 enum wbcSidType type
;
1144 /* Send off request */
1146 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1147 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1148 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1149 wbcErrorString(wbc_status
));
1153 wbc_status
= wbcLookupSid(&sid
, &domain
, &name
, &type
);
1154 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1155 d_fprintf(stderr
, "failed to call wbcLookupSid: %s\n",
1156 wbcErrorString(wbc_status
));
1160 /* Display response */
1162 d_printf("%s%c%s %d\n",
1163 domain
, winbind_separator(), name
, type
);
1168 /* Convert sid to fullname */
1170 static bool wbinfo_lookupsid_fullname(const char *sid_str
)
1172 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1173 struct wbcDomainSid sid
;
1176 enum wbcSidType type
;
1178 /* Send off request */
1180 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1181 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1182 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1183 wbcErrorString(wbc_status
));
1187 wbc_status
= wbcGetDisplayName(&sid
, &domain
, &name
, &type
);
1188 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1189 d_fprintf(stderr
, "failed to call wbcGetDisplayName: %s\n",
1190 wbcErrorString(wbc_status
));
1194 /* Display response */
1196 d_printf("%s%c%s %d\n",
1197 domain
, winbind_separator(), name
, type
);
1202 /* Lookup a list of RIDs */
1204 static bool wbinfo_lookuprids(const char *domain
, const char *arg
)
1206 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1207 struct wbcDomainInfo
*dinfo
= NULL
;
1208 char *domain_name
= NULL
;
1209 const char **names
= NULL
;
1210 enum wbcSidType
*types
= NULL
;
1213 uint32_t *rids
= NULL
;
1216 TALLOC_CTX
*mem_ctx
= NULL
;
1219 if ((domain
== NULL
) || (strequal(domain
, ".")) || (domain
[0] == '\0')){
1220 domain
= get_winbind_domain();
1225 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
1226 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1227 d_printf("wbcDomainInfo(%s) failed: %s\n", domain
,
1228 wbcErrorString(wbc_status
));
1232 mem_ctx
= talloc_new(NULL
);
1233 if (mem_ctx
== NULL
) {
1234 d_printf("talloc_new failed\n");
1242 while (next_token_talloc(mem_ctx
, &p
, &ridstr
, " ,\n")) {
1243 uint32_t rid
= strtoul(ridstr
, NULL
, 10);
1244 rids
= talloc_realloc(mem_ctx
, rids
, uint32_t, num_rids
+ 1);
1246 d_printf("talloc_realloc failed\n");
1248 rids
[num_rids
] = rid
;
1253 d_printf("no rids\n");
1257 wbc_status
= wbcLookupRids(&dinfo
->sid
, num_rids
, rids
,
1258 (const char **)&domain_name
, &names
, &types
);
1259 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1260 d_printf("winbind_lookup_rids failed: %s\n",
1261 wbcErrorString(wbc_status
));
1265 d_printf("Domain: %s\n", domain_name
);
1267 for (i
=0; i
<num_rids
; i
++) {
1268 d_printf("%8d: %s (%s)\n", rids
[i
], names
[i
],
1269 wbcSidTypeString(types
[i
]));
1275 wbcFreeMemory(dinfo
);
1278 wbcFreeMemory(domain_name
);
1281 wbcFreeMemory(names
);
1284 wbcFreeMemory(types
);
1286 TALLOC_FREE(mem_ctx
);
1290 /* Convert string to sid */
1292 static bool wbinfo_lookupname(const char *full_name
)
1294 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1295 struct wbcDomainSid sid
;
1297 enum wbcSidType type
;
1298 fstring domain_name
;
1299 fstring account_name
;
1301 /* Send off request */
1303 parse_wbinfo_domain_user(full_name
, domain_name
,
1306 wbc_status
= wbcLookupName(domain_name
, account_name
,
1308 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1309 d_fprintf(stderr
, "failed to call wbcLookupName: %s\n",
1310 wbcErrorString(wbc_status
));
1314 wbc_status
= wbcSidToString(&sid
, &sid_str
);
1315 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1316 d_fprintf(stderr
, "failed to call wbcSidToString: %s\n",
1317 wbcErrorString(wbc_status
));
1321 /* Display response */
1323 d_printf("%s %s (%d)\n", sid_str
, wbcSidTypeString(type
), type
);
1325 wbcFreeMemory(sid_str
);
1330 static char *wbinfo_prompt_pass(TALLOC_CTX
*mem_ctx
,
1332 const char *username
)
1335 const char *ret
= NULL
;
1337 prompt
= talloc_asprintf(mem_ctx
, "Enter %s's ", username
);
1342 prompt
= talloc_asprintf_append(prompt
, "%s ", prefix
);
1347 prompt
= talloc_asprintf_append(prompt
, "password: ");
1352 ret
= getpass(prompt
);
1353 TALLOC_FREE(prompt
);
1355 return talloc_strdup(mem_ctx
, ret
);
1358 /* Authenticate a user with a plaintext password */
1360 static bool wbinfo_auth_krb5(char *username
, const char *cctype
, uint32_t flags
)
1362 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1365 char *password
= NULL
;
1367 char *local_cctype
= NULL
;
1369 struct wbcLogonUserParams params
;
1370 struct wbcLogonUserInfo
*info
;
1371 struct wbcAuthErrorInfo
*error
;
1372 struct wbcUserPasswordPolicyInfo
*policy
;
1373 TALLOC_CTX
*frame
= talloc_tos();
1375 if ((s
= talloc_strdup(frame
, username
)) == NULL
) {
1379 if ((p
= strchr(s
, '%')) != NULL
) {
1382 password
= talloc_strdup(frame
, p
);
1384 password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1387 local_cctype
= talloc_strdup(frame
, cctype
);
1393 params
.username
= name
;
1394 params
.password
= password
;
1395 params
.num_blobs
= 0;
1396 params
.blobs
= NULL
;
1398 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
,
1404 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1405 d_fprintf(stderr
, "failed to call wbcAddNamedBlob: %s\n",
1406 wbcErrorString(wbc_status
));
1410 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
,
1416 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1417 d_fprintf(stderr
, "failed to call wbcAddNamedBlob: %s\n",
1418 wbcErrorString(wbc_status
));
1422 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
,
1426 (uint8_t *)local_cctype
,
1428 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1429 d_fprintf(stderr
, "failed to call wbcAddNamedBlob: %s\n",
1430 wbcErrorString(wbc_status
));
1434 wbc_status
= wbcLogonUser(¶ms
, &info
, &error
, &policy
);
1436 d_printf("plaintext kerberos password authentication for [%s] %s "
1437 "(requesting cctype: %s)\n",
1438 username
, WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed",
1443 "error code was %s (0x%x)\nerror messsage was: %s\n",
1446 error
->display_string
);
1449 if (WBC_ERROR_IS_OK(wbc_status
)) {
1450 if (flags
& WBFLAG_PAM_INFO3_TEXT
) {
1451 if (info
&& info
->info
&& info
->info
->user_flags
&
1452 NETLOGON_CACHED_ACCOUNT
) {
1453 d_printf("user_flgs: "
1454 "NETLOGON_CACHED_ACCOUNT\n");
1460 for (i
=0; i
< info
->num_blobs
; i
++) {
1461 if (strequal(info
->blobs
[i
].name
,
1463 d_printf("credentials were put "
1466 info
->blobs
[i
].blob
.data
);
1471 d_printf("no credentials cached\n");
1476 wbcFreeMemory(params
.blobs
);
1478 return WBC_ERROR_IS_OK(wbc_status
);
1481 /* Authenticate a user with a plaintext password */
1483 static bool wbinfo_auth(char *username
)
1485 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1488 char *password
= NULL
;
1490 TALLOC_CTX
*frame
= talloc_tos();
1492 if ((s
= talloc_strdup(frame
, username
)) == NULL
) {
1496 if ((p
= strchr(s
, '%')) != NULL
) {
1499 password
= talloc_strdup(frame
, p
);
1501 password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1506 wbc_status
= wbcAuthenticateUser(name
, password
);
1508 d_printf("plaintext password authentication %s\n",
1509 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1512 if (response
.data
.auth
.nt_status
)
1514 "error code was %s (0x%x)\nerror messsage was: %s\n",
1515 response
.data
.auth
.nt_status_string
,
1516 response
.data
.auth
.nt_status
,
1517 response
.data
.auth
.error_string
);
1520 return WBC_ERROR_IS_OK(wbc_status
);
1523 /* Authenticate a user with a challenge/response */
1525 static bool wbinfo_auth_crap(char *username
, bool use_ntlmv2
, bool use_lanman
)
1527 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1528 struct wbcAuthUserParams params
;
1529 struct wbcAuthUserInfo
*info
= NULL
;
1530 struct wbcAuthErrorInfo
*err
= NULL
;
1531 DATA_BLOB lm
= data_blob_null
;
1532 DATA_BLOB nt
= data_blob_null
;
1534 fstring name_domain
;
1537 TALLOC_CTX
*frame
= talloc_tos();
1539 p
= strchr(username
, '%');
1543 pass
= talloc_strdup(frame
, p
+ 1);
1545 pass
= wbinfo_prompt_pass(frame
, NULL
, username
);
1548 parse_wbinfo_domain_user(username
, name_domain
, name_user
);
1550 params
.account_name
= name_user
;
1551 params
.domain_name
= name_domain
;
1552 params
.workstation_name
= NULL
;
1555 params
.parameter_control
= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
|
1556 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
;
1558 params
.level
= WBC_AUTH_USER_LEVEL_RESPONSE
;
1560 generate_random_buffer(params
.password
.response
.challenge
, 8);
1563 DATA_BLOB server_chal
;
1564 DATA_BLOB names_blob
;
1566 server_chal
= data_blob(params
.password
.response
.challenge
, 8);
1568 /* Pretend this is a login to 'us', for blob purposes */
1569 names_blob
= NTLMv2_generate_names_blob(NULL
,
1570 get_winbind_netbios_name(),
1571 get_winbind_domain());
1573 if (!SMBNTLMv2encrypt(NULL
, name_user
, name_domain
, pass
,
1576 &lm
, &nt
, NULL
, NULL
)) {
1577 data_blob_free(&names_blob
);
1578 data_blob_free(&server_chal
);
1582 data_blob_free(&names_blob
);
1583 data_blob_free(&server_chal
);
1588 lm
= data_blob(NULL
, 24);
1589 ok
= SMBencrypt(pass
,
1590 params
.password
.response
.challenge
,
1593 data_blob_free(&lm
);
1596 nt
= data_blob(NULL
, 24);
1597 SMBNTencrypt(pass
, params
.password
.response
.challenge
,
1601 params
.password
.response
.nt_length
= nt
.length
;
1602 params
.password
.response
.nt_data
= nt
.data
;
1603 params
.password
.response
.lm_length
= lm
.length
;
1604 params
.password
.response
.lm_data
= lm
.data
;
1606 wbc_status
= wbcAuthenticateUserEx(¶ms
, &info
, &err
);
1608 /* Display response */
1610 d_printf("challenge/response password authentication %s\n",
1611 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1613 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
1615 "error code was %s (0x%x)\nerror messsage was: %s\n",
1618 err
->display_string
);
1620 } else if (WBC_ERROR_IS_OK(wbc_status
)) {
1621 wbcFreeMemory(info
);
1624 data_blob_free(&nt
);
1625 data_blob_free(&lm
);
1627 return WBC_ERROR_IS_OK(wbc_status
);
1630 /* Authenticate a user with a plaintext password */
1632 static bool wbinfo_pam_logon(char *username
)
1634 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1635 struct wbcLogonUserParams params
;
1636 struct wbcAuthErrorInfo
*error
;
1639 TALLOC_CTX
*frame
= talloc_tos();
1643 ZERO_STRUCT(params
);
1645 if ((s
= talloc_strdup(frame
, username
)) == NULL
) {
1649 if ((p
= strchr(s
, '%')) != NULL
) {
1652 params
.password
= talloc_strdup(frame
, p
);
1654 params
.password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1656 params
.username
= s
;
1658 flags
= WBFLAG_PAM_CACHED_LOGIN
;
1660 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
, ¶ms
.blobs
,
1662 (uint8_t *)&flags
, sizeof(flags
));
1663 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1664 d_printf("wbcAddNamedBlob failed: %s\n",
1665 wbcErrorString(wbc_status
));
1671 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
, ¶ms
.blobs
,
1673 (uint8_t *)&uid
, sizeof(uid
));
1674 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1675 d_printf("wbcAddNamedBlob failed: %s\n",
1676 wbcErrorString(wbc_status
));
1680 wbc_status
= wbcLogonUser(¶ms
, NULL
, &error
, NULL
);
1682 wbcFreeMemory(params
.blobs
);
1684 d_printf("plaintext password authentication %s\n",
1685 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1687 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1689 "error code was %s (0x%x)\nerror messsage was: %s\n",
1691 (int)error
->nt_status
,
1692 error
->display_string
);
1693 wbcFreeMemory(error
);
1699 /* Save creds with winbind */
1701 static bool wbinfo_ccache_save(char *username
)
1703 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1706 char *password
= NULL
;
1708 TALLOC_CTX
*frame
= talloc_stackframe();
1710 s
= talloc_strdup(frame
, username
);
1719 password
= talloc_strdup(frame
, p
);
1721 password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1726 wbc_status
= wbcCredentialSave(name
, password
);
1728 d_printf("saving creds %s\n",
1729 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1733 return WBC_ERROR_IS_OK(wbc_status
);
1736 #ifdef WITH_FAKE_KASERVER
1737 /* Authenticate a user with a plaintext password and set a token */
1739 static bool wbinfo_klog(char *username
)
1741 struct winbindd_request request
;
1742 struct winbindd_response response
;
1746 /* Send off request */
1748 ZERO_STRUCT(request
);
1749 ZERO_STRUCT(response
);
1751 p
= strchr(username
, '%');
1755 fstrcpy(request
.data
.auth
.user
, username
);
1756 fstrcpy(request
.data
.auth
.pass
, p
+ 1);
1759 fstrcpy(request
.data
.auth
.user
, username
);
1760 fstrcpy(request
.data
.auth
.pass
, getpass("Password: "));
1763 request
.flags
|= WBFLAG_PAM_AFS_TOKEN
;
1765 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
,
1768 /* Display response */
1770 d_printf("plaintext password authentication %s\n",
1771 (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed");
1773 if (response
.data
.auth
.nt_status
)
1775 "error code was %s (0x%x)\nerror messsage was: %s\n",
1776 response
.data
.auth
.nt_status_string
,
1777 response
.data
.auth
.nt_status
,
1778 response
.data
.auth
.error_string
);
1780 if (result
!= NSS_STATUS_SUCCESS
)
1783 if (response
.extra_data
.data
== NULL
) {
1784 d_fprintf(stderr
, "Did not get token data\n");
1788 if (!afs_settoken_str((char *)response
.extra_data
.data
)) {
1789 d_fprintf(stderr
, "Could not set token\n");
1793 d_printf("Successfully created AFS token\n");
1797 static bool wbinfo_klog(char *username
)
1799 d_fprintf(stderr
, "No AFS support compiled in.\n");
1804 /* Print domain users */
1806 static bool print_domain_users(const char *domain
)
1808 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1810 uint32_t num_users
= 0;
1811 const char **users
= NULL
;
1813 /* Send request to winbind daemon */
1815 /* '.' is the special sign for our own domain */
1816 if (domain
&& strcmp(domain
, ".") == 0) {
1817 domain
= get_winbind_domain();
1820 wbc_status
= wbcListUsers(domain
, &num_users
, &users
);
1821 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1825 for (i
=0; i
< num_users
; i
++) {
1826 d_printf("%s\n", users
[i
]);
1829 wbcFreeMemory(users
);
1834 /* Print domain groups */
1836 static bool print_domain_groups(const char *domain
)
1838 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1840 uint32_t num_groups
= 0;
1841 const char **groups
= NULL
;
1843 /* Send request to winbind daemon */
1845 /* '.' is the special sign for our own domain */
1846 if (domain
&& strcmp(domain
, ".") == 0) {
1847 domain
= get_winbind_domain();
1850 wbc_status
= wbcListGroups(domain
, &num_groups
, &groups
);
1851 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1852 d_fprintf(stderr
, "failed to call wbcListGroups: %s\n",
1853 wbcErrorString(wbc_status
));
1857 for (i
=0; i
< num_groups
; i
++) {
1858 d_printf("%s\n", groups
[i
]);
1861 wbcFreeMemory(groups
);
1866 /* Set the authorised user for winbindd access in secrets.tdb */
1868 static bool wbinfo_set_auth_user(char *username
)
1870 d_fprintf(stderr
, "This functionality was moved to the 'net' utility.\n"
1871 "See 'net help setauthuser' for details.\n");
1875 static void wbinfo_get_auth_user(void)
1877 d_fprintf(stderr
, "This functionality was moved to the 'net' utility.\n"
1878 "See 'net help getauthuser' for details.\n");
1881 static bool wbinfo_ping(void)
1885 wbc_status
= wbcPing();
1887 /* Display response */
1889 d_printf("Ping to winbindd %s\n",
1890 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1892 return WBC_ERROR_IS_OK(wbc_status
);
1895 static bool wbinfo_change_user_password(const char *username
)
1898 char *old_password
= NULL
;
1899 char *new_password
= NULL
;
1900 TALLOC_CTX
*frame
= talloc_tos();
1902 old_password
= wbinfo_prompt_pass(frame
, "old", username
);
1903 new_password
= wbinfo_prompt_pass(frame
, "new", username
);
1905 wbc_status
= wbcChangeUserPassword(username
, old_password
,new_password
);
1907 /* Display response */
1909 d_printf("Password change for user %s %s\n", username
,
1910 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1912 return WBC_ERROR_IS_OK(wbc_status
);
1918 OPT_SET_AUTH_USER
= 1000,
1929 OPT_SET_UID_MAPPING
,
1930 OPT_SET_GID_MAPPING
,
1931 OPT_REMOVE_UID_MAPPING
,
1932 OPT_REMOVE_GID_MAPPING
,
1934 OPT_LIST_ALL_DOMAINS
,
1935 OPT_LIST_OWN_DOMAIN
,
1942 OPT_CHANGE_USER_PASSWORD
,
1945 OPT_SID_TO_FULLNAME
,
1954 int main(int argc
, char **argv
, char **envp
)
1957 TALLOC_CTX
*frame
= talloc_stackframe();
1959 static char *string_arg
;
1960 char *string_subarg
= NULL
;
1961 static char *opt_domain_name
;
1963 int int_subarg
= -1;
1965 bool verbose
= false;
1966 bool use_ntlmv2
= false;
1967 bool use_lanman
= false;
1968 char *logoff_user
= getenv("USER");
1969 int logoff_uid
= geteuid();
1971 struct poptOption long_options
[] = {
1974 /* longName, shortName, argInfo, argPtr, value, descrip,
1977 { "domain-users", 'u', POPT_ARG_NONE
, 0, 'u', "Lists all domain users", "domain"},
1978 { "domain-groups", 'g', POPT_ARG_NONE
, 0, 'g', "Lists all domain groups", "domain" },
1979 { "WINS-by-name", 'N', POPT_ARG_STRING
, &string_arg
, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1980 { "WINS-by-ip", 'I', POPT_ARG_STRING
, &string_arg
, 'I', "Converts IP address to NetBIOS name", "IP" },
1981 { "name-to-sid", 'n', POPT_ARG_STRING
, &string_arg
, 'n', "Converts name to sid", "NAME" },
1982 { "sid-to-name", 's', POPT_ARG_STRING
, &string_arg
, 's', "Converts sid to name", "SID" },
1983 { "sid-to-fullname", 0, POPT_ARG_STRING
, &string_arg
,
1984 OPT_SID_TO_FULLNAME
, "Converts sid to fullname", "SID" },
1985 { "lookup-rids", 'R', POPT_ARG_STRING
, &string_arg
, 'R', "Converts RIDs to names", "RIDs" },
1986 { "uid-to-sid", 'U', POPT_ARG_INT
, &int_arg
, 'U', "Converts uid to sid" , "UID" },
1987 { "gid-to-sid", 'G', POPT_ARG_INT
, &int_arg
, 'G', "Converts gid to sid", "GID" },
1988 { "sid-to-uid", 'S', POPT_ARG_STRING
, &string_arg
, 'S', "Converts sid to uid", "SID" },
1989 { "sid-to-gid", 'Y', POPT_ARG_STRING
, &string_arg
, 'Y', "Converts sid to gid", "SID" },
1990 { "allocate-uid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_UID
,
1991 "Get a new UID out of idmap" },
1992 { "allocate-gid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_GID
,
1993 "Get a new GID out of idmap" },
1994 { "set-uid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_UID_MAPPING
, "Create or modify uid to sid mapping in idmap", "UID,SID" },
1995 { "set-gid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_GID_MAPPING
, "Create or modify gid to sid mapping in idmap", "GID,SID" },
1996 { "remove-uid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_REMOVE_UID_MAPPING
, "Remove uid to sid mapping in idmap", "UID,SID" },
1997 { "remove-gid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_REMOVE_GID_MAPPING
, "Remove gid to sid mapping in idmap", "GID,SID" },
1998 { "check-secret", 't', POPT_ARG_NONE
, 0, 't', "Check shared secret" },
1999 { "change-secret", 'c', POPT_ARG_NONE
, 0, 'c', "Change shared secret" },
2000 { "ping-dc", 0, POPT_ARG_NONE
, 0, OPT_PING_DC
,
2001 "Check the NETLOGON connection" },
2002 { "trusted-domains", 'm', POPT_ARG_NONE
, 0, 'm', "List trusted domains" },
2003 { "all-domains", 0, POPT_ARG_NONE
, 0, OPT_LIST_ALL_DOMAINS
, "List all domains (trusted and own domain)" },
2004 { "own-domain", 0, POPT_ARG_NONE
, 0, OPT_LIST_OWN_DOMAIN
, "List own domain" },
2005 { "sequence", 0, POPT_ARG_NONE
, 0, OPT_SEQUENCE
, "Deprecated command, see --online-status" },
2006 { "online-status", 0, POPT_ARG_NONE
, 0, OPT_ONLINESTATUS
, "Show whether domains are marked as online or offline"},
2007 { "domain-info", 'D', POPT_ARG_STRING
, &string_arg
, 'D', "Show most of the info we have about the domain" },
2008 { "user-info", 'i', POPT_ARG_STRING
, &string_arg
, 'i', "Get user info", "USER" },
2009 { "uid-info", 0, POPT_ARG_INT
, &int_arg
, OPT_UID_INFO
, "Get user info from uid", "UID" },
2010 { "group-info", 0, POPT_ARG_STRING
, &string_arg
, OPT_GROUP_INFO
, "Get group info", "GROUP" },
2011 { "user-sidinfo", 0, POPT_ARG_STRING
, &string_arg
, OPT_USER_SIDINFO
, "Get user info from sid", "SID" },
2012 { "gid-info", 0, POPT_ARG_INT
, &int_arg
, OPT_GID_INFO
, "Get group info from gid", "GID" },
2013 { "user-groups", 'r', POPT_ARG_STRING
, &string_arg
, 'r', "Get user groups", "USER" },
2014 { "user-domgroups", 0, POPT_ARG_STRING
, &string_arg
,
2015 OPT_USERDOMGROUPS
, "Get user domain groups", "SID" },
2016 { "sid-aliases", 0, POPT_ARG_STRING
, &string_arg
, OPT_SIDALIASES
, "Get sid aliases", "SID" },
2017 { "user-sids", 0, POPT_ARG_STRING
, &string_arg
, OPT_USERSIDS
, "Get user group sids for user SID", "SID" },
2018 { "authenticate", 'a', POPT_ARG_STRING
, &string_arg
, 'a', "authenticate user", "user%password" },
2019 { "pam-logon", 0, POPT_ARG_STRING
, string_arg
, OPT_PAM_LOGON
,
2020 "do a pam logon equivalent", "user%password" },
2021 { "logoff", 0, POPT_ARG_NONE
, NULL
, OPT_LOGOFF
,
2022 "log off user", "uid" },
2023 { "logoff-user", 0, POPT_ARG_STRING
, &logoff_user
,
2024 OPT_LOGOFF_USER
, "username to log off" },
2025 { "logoff-uid", 0, POPT_ARG_INT
, &logoff_uid
,
2026 OPT_LOGOFF_UID
, "uid to log off" },
2027 { "set-auth-user", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_AUTH_USER
, "Store user and password used by winbindd (root only)", "user%password" },
2028 { "ccache-save", 0, POPT_ARG_STRING
, &string_arg
,
2029 OPT_CCACHE_SAVE
, "Store user and password for ccache "
2030 "operation", "user%password" },
2031 { "getdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_GETDCNAME
,
2032 "Get a DC name for a foreign domain", "domainname" },
2033 { "dsgetdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_DSGETDCNAME
, "Find a DC for a domain", "domainname" },
2034 { "get-auth-user", 0, POPT_ARG_NONE
, NULL
, OPT_GET_AUTH_USER
, "Retrieve user and password used by winbindd (root only)", NULL
},
2035 { "ping", 'p', POPT_ARG_NONE
, 0, 'p', "Ping winbindd to see if it is alive" },
2036 { "domain", 0, POPT_ARG_STRING
, &opt_domain_name
, OPT_DOMAIN_NAME
, "Define to the domain to restrict operation", "domain" },
2037 #ifdef WITH_FAKE_KASERVER
2038 { "klog", 'k', POPT_ARG_STRING
, &string_arg
, 'k', "set an AFS token from winbind", "user%password" },
2041 { "krb5auth", 'K', POPT_ARG_STRING
, &string_arg
, 'K', "authenticate user using Kerberos", "user%password" },
2042 /* destroys wbinfo --help output */
2043 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
2045 { "separator", 0, POPT_ARG_NONE
, 0, OPT_SEPARATOR
, "Get the active winbind separator", NULL
},
2046 { "verbose", 0, POPT_ARG_NONE
, 0, OPT_VERBOSE
, "Print additional information per command", NULL
},
2047 { "change-user-password", 0, POPT_ARG_STRING
, &string_arg
, OPT_CHANGE_USER_PASSWORD
, "Change the password for a user", NULL
},
2048 { "ntlmv2", 0, POPT_ARG_NONE
, 0, OPT_NTLMV2
, "Use NTLMv2 cryptography for user authentication", NULL
},
2049 { "lanman", 0, POPT_ARG_NONE
, 0, OPT_LANMAN
, "Use lanman cryptography for user authentication", NULL
},
2054 /* Samba client initialisation */
2060 pc
= poptGetContext("wbinfo", argc
, (const char **)argv
,
2063 /* Parse command line options */
2066 poptPrintHelp(pc
, stderr
, 0);
2070 while((opt
= poptGetNextOpt(pc
)) != -1) {
2071 /* get the generic configuration parameters like --domain */
2085 poptFreeContext(pc
);
2087 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
2088 POPT_CONTEXT_KEEP_FIRST
);
2090 while((opt
= poptGetNextOpt(pc
)) != -1) {
2093 if (!print_domain_users(opt_domain_name
)) {
2095 "Error looking up domain users\n");
2100 if (!print_domain_groups(opt_domain_name
)) {
2102 "Error looking up domain groups\n");
2107 if (!wbinfo_lookupsid(string_arg
)) {
2109 "Could not lookup sid %s\n",
2114 case OPT_SID_TO_FULLNAME
:
2115 if (!wbinfo_lookupsid_fullname(string_arg
)) {
2116 d_fprintf(stderr
, "Could not lookup sid %s\n",
2122 if (!wbinfo_lookuprids(opt_domain_name
, string_arg
)) {
2123 d_fprintf(stderr
, "Could not lookup RIDs %s\n",
2129 if (!wbinfo_lookupname(string_arg
)) {
2130 d_fprintf(stderr
, "Could not lookup name %s\n",
2136 if (!wbinfo_wins_byname(string_arg
)) {
2138 "Could not lookup WINS by name %s\n",
2144 if (!wbinfo_wins_byip(string_arg
)) {
2146 "Could not lookup WINS by IP %s\n",
2152 if (!wbinfo_uid_to_sid(int_arg
)) {
2154 "Could not convert uid %d to sid\n",
2160 if (!wbinfo_gid_to_sid(int_arg
)) {
2162 "Could not convert gid %d to sid\n",
2168 if (!wbinfo_sid_to_uid(string_arg
)) {
2170 "Could not convert sid %s to uid\n",
2176 if (!wbinfo_sid_to_gid(string_arg
)) {
2178 "Could not convert sid %s to gid\n",
2183 case OPT_ALLOCATE_UID
:
2184 if (!wbinfo_allocate_uid()) {
2185 d_fprintf(stderr
, "Could not allocate a uid\n");
2189 case OPT_ALLOCATE_GID
:
2190 if (!wbinfo_allocate_gid()) {
2191 d_fprintf(stderr
, "Could not allocate a gid\n");
2195 case OPT_SET_UID_MAPPING
:
2196 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2198 !wbinfo_set_uid_mapping(int_subarg
, string_subarg
))
2200 d_fprintf(stderr
, "Could not create or modify "
2201 "uid to sid mapping\n");
2205 case OPT_SET_GID_MAPPING
:
2206 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2208 !wbinfo_set_gid_mapping(int_subarg
, string_subarg
))
2210 d_fprintf(stderr
, "Could not create or modify "
2211 "gid to sid mapping\n");
2215 case OPT_REMOVE_UID_MAPPING
:
2216 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2218 !wbinfo_remove_uid_mapping(int_subarg
,
2221 d_fprintf(stderr
, "Could not remove uid to sid "
2226 case OPT_REMOVE_GID_MAPPING
:
2227 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2229 !wbinfo_remove_gid_mapping(int_subarg
,
2232 d_fprintf(stderr
, "Could not remove gid to sid "
2238 if (!wbinfo_check_secret(opt_domain_name
)) {
2239 d_fprintf(stderr
, "Could not check secret\n");
2244 if (!wbinfo_change_secret(opt_domain_name
)) {
2245 d_fprintf(stderr
, "Could not change secret\n");
2250 if (!wbinfo_ping_dc()) {
2251 d_fprintf(stderr
, "Could not ping our DC\n");
2256 if (!wbinfo_list_domains(false, verbose
)) {
2258 "Could not list trusted domains\n");
2263 if (!wbinfo_show_sequence(opt_domain_name
)) {
2265 "Could not show sequence numbers\n");
2269 case OPT_ONLINESTATUS
:
2270 if (!wbinfo_show_onlinestatus(opt_domain_name
)) {
2272 "Could not show online-status\n");
2277 if (!wbinfo_domain_info(string_arg
)) {
2279 "Could not get domain info\n");
2284 if (!wbinfo_get_userinfo(string_arg
)) {
2286 "Could not get info for user %s\n",
2291 case OPT_USER_SIDINFO
:
2292 if ( !wbinfo_get_user_sidinfo(string_arg
)) {
2294 "Could not get info for user "
2295 "sid %s\n", string_arg
);
2300 if ( !wbinfo_get_uidinfo(int_arg
)) {
2301 d_fprintf(stderr
, "Could not get info for uid "
2306 case OPT_GROUP_INFO
:
2307 if ( !wbinfo_get_groupinfo(string_arg
)) {
2308 d_fprintf(stderr
, "Could not get info for "
2309 "group %s\n", string_arg
);
2314 if ( !wbinfo_get_gidinfo(int_arg
)) {
2315 d_fprintf(stderr
, "Could not get info for gid "
2321 if (!wbinfo_get_usergroups(string_arg
)) {
2323 "Could not get groups for user %s\n",
2329 if (!wbinfo_get_usersids(string_arg
)) {
2330 d_fprintf(stderr
, "Could not get group SIDs "
2331 "for user SID %s\n",
2336 case OPT_USERDOMGROUPS
:
2337 if (!wbinfo_get_userdomgroups(string_arg
)) {
2338 d_fprintf(stderr
, "Could not get user's domain "
2339 "groups for user SID %s\n",
2344 case OPT_SIDALIASES
:
2345 if (!wbinfo_get_sidaliases(opt_domain_name
,
2347 d_fprintf(stderr
, "Could not get sid aliases "
2348 "for user SID %s\n", string_arg
);
2353 bool got_error
= false;
2355 if (!wbinfo_auth(string_arg
)) {
2357 "Could not authenticate user "
2358 "%s with plaintext "
2359 "password\n", string_arg
);
2363 if (!wbinfo_auth_crap(string_arg
, use_ntlmv2
,
2366 "Could not authenticate user "
2367 "%s with challenge/response\n",
2377 if (!wbinfo_pam_logon(string_arg
)) {
2378 d_fprintf(stderr
, "pam_logon failed for %s\n",
2386 wbc_status
= wbcLogoffUser(logoff_user
, logoff_uid
,
2388 d_printf("Logoff %s (%d): %s\n", logoff_user
,
2389 logoff_uid
, wbcErrorString(wbc_status
));
2393 uint32_t flags
= WBFLAG_PAM_KRB5
|
2394 WBFLAG_PAM_CACHED_LOGIN
|
2395 WBFLAG_PAM_FALLBACK_AFTER_KRB5
|
2396 WBFLAG_PAM_INFO3_TEXT
|
2397 WBFLAG_PAM_CONTACT_TRUSTDOM
;
2399 if (!wbinfo_auth_krb5(string_arg
, "FILE",
2402 "Could not authenticate user "
2403 "[%s] with Kerberos "
2404 "(ccache: %s)\n", string_arg
,
2411 if (!wbinfo_klog(string_arg
)) {
2412 d_fprintf(stderr
, "Could not klog user\n");
2417 if (!wbinfo_ping()) {
2418 d_fprintf(stderr
, "could not ping winbindd!\n");
2422 case OPT_SET_AUTH_USER
:
2423 if (!wbinfo_set_auth_user(string_arg
)) {
2427 case OPT_GET_AUTH_USER
:
2428 wbinfo_get_auth_user();
2431 case OPT_CCACHE_SAVE
:
2432 if (!wbinfo_ccache_save(string_arg
)) {
2437 if (!wbinfo_getdcname(string_arg
)) {
2441 case OPT_DSGETDCNAME
:
2442 if (!wbinfo_dsgetdcname(string_arg
, 0)) {
2446 case OPT_SEPARATOR
: {
2447 const char sep
= winbind_separator();
2451 d_printf("%c\n", sep
);
2454 case OPT_LIST_ALL_DOMAINS
:
2455 if (!wbinfo_list_domains(true, verbose
)) {
2459 case OPT_LIST_OWN_DOMAIN
:
2460 if (!wbinfo_list_own_domain()) {
2464 case OPT_CHANGE_USER_PASSWORD
:
2465 if (!wbinfo_change_user_password(string_arg
)) {
2467 "Could not change user password "
2468 "for user %s\n", string_arg
);
2473 /* generic configuration options */
2474 case OPT_DOMAIN_NAME
:
2478 case OPT_LOGOFF_USER
:
2479 case OPT_LOGOFF_UID
:
2482 d_fprintf(stderr
, "Invalid option\n");
2483 poptPrintHelp(pc
, stderr
, 0);
2495 poptFreeContext(pc
);