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
++) {
360 char str
[WBC_SID_STRING_BUFLEN
];
361 wbcSidToStringBuf(&sids
[i
], str
, sizeof(str
));
362 d_printf("%s\n", str
);
370 static bool wbinfo_get_userdomgroups(const char *user_sid_str
)
372 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
375 struct wbcDomainSid user_sid
, *sids
= NULL
;
379 wbc_status
= wbcStringToSid(user_sid_str
, &user_sid
);
380 if (!WBC_ERROR_IS_OK(wbc_status
)) {
381 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
382 wbcErrorString(wbc_status
));
386 wbc_status
= wbcLookupUserSids(&user_sid
, true, &num_sids
, &sids
);
387 if (!WBC_ERROR_IS_OK(wbc_status
)) {
388 d_fprintf(stderr
, "failed to call wbcLookupUserSids: %s\n",
389 wbcErrorString(wbc_status
));
393 for (i
= 0; i
< num_sids
; i
++) {
394 char str
[WBC_SID_STRING_BUFLEN
];
395 wbcSidToStringBuf(&sids
[i
], str
, sizeof(str
));
396 d_printf("%s\n", str
);
404 static bool wbinfo_get_sidaliases(const char *domain
,
405 const char *user_sid_str
)
407 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
408 struct wbcDomainInfo
*dinfo
= NULL
;
410 struct wbcDomainSid user_sid
;
411 uint32_t *alias_rids
= NULL
;
412 uint32_t num_alias_rids
;
413 char domain_sid_str
[WBC_SID_STRING_BUFLEN
];
416 if ((domain
== NULL
) || (strequal(domain
, ".")) ||
417 (domain
[0] == '\0')) {
418 domain
= get_winbind_domain();
423 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
424 if (!WBC_ERROR_IS_OK(wbc_status
)) {
425 d_fprintf(stderr
, "wbcDomainInfo(%s) failed: %s\n", domain
,
426 wbcErrorString(wbc_status
));
429 wbc_status
= wbcStringToSid(user_sid_str
, &user_sid
);
430 if (!WBC_ERROR_IS_OK(wbc_status
)) {
434 wbc_status
= wbcGetSidAliases(&dinfo
->sid
, &user_sid
, 1,
435 &alias_rids
, &num_alias_rids
);
436 if (!WBC_ERROR_IS_OK(wbc_status
)) {
440 wbcSidToStringBuf(&dinfo
->sid
, domain_sid_str
, sizeof(domain_sid_str
));
442 for (i
= 0; i
< num_alias_rids
; i
++) {
443 d_printf("%s-%d\n", domain_sid_str
, alias_rids
[i
]);
446 wbcFreeMemory(alias_rids
);
449 wbcFreeMemory(dinfo
);
450 return (WBC_ERR_SUCCESS
== wbc_status
);
454 /* Convert NetBIOS name to IP */
456 static bool wbinfo_wins_byname(const char *name
)
458 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
461 wbc_status
= wbcResolveWinsByName(name
, &ip
);
462 if (!WBC_ERROR_IS_OK(wbc_status
)) {
463 d_fprintf(stderr
, "failed to call wbcResolveWinsByName: %s\n",
464 wbcErrorString(wbc_status
));
468 /* Display response */
470 d_printf("%s\n", ip
);
477 /* Convert IP to NetBIOS name */
479 static bool wbinfo_wins_byip(const char *ip
)
481 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
484 wbc_status
= wbcResolveWinsByIP(ip
, &name
);
485 if (!WBC_ERROR_IS_OK(wbc_status
)) {
486 d_fprintf(stderr
, "failed to call wbcResolveWinsByIP: %s\n",
487 wbcErrorString(wbc_status
));
491 /* Display response */
493 d_printf("%s\n", name
);
500 /* List all/trusted domains */
502 static bool wbinfo_list_domains(bool list_all_domains
, bool verbose
)
504 struct wbcDomainInfo
*domain_list
= NULL
;
506 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
507 bool print_all
= !list_all_domains
&& verbose
;
510 wbc_status
= wbcListTrusts(&domain_list
, &num_domains
);
511 if (!WBC_ERROR_IS_OK(wbc_status
)) {
512 d_fprintf(stderr
, "failed to call wbcListTrusts: %s\n",
513 wbcErrorString(wbc_status
));
518 d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n",
519 "Domain Name", "DNS Domain", "Trust Type",
520 "Transitive", "In", "Out");
523 for (i
=0; i
<num_domains
; i
++) {
525 d_printf("%-16s", domain_list
[i
].short_name
);
527 d_printf("%s", domain_list
[i
].short_name
);
532 d_printf("%-24s", domain_list
[i
].dns_name
);
534 switch(domain_list
[i
].trust_type
) {
535 case WBC_DOMINFO_TRUSTTYPE_NONE
:
538 case WBC_DOMINFO_TRUSTTYPE_FOREST
:
541 case WBC_DOMINFO_TRUSTTYPE_EXTERNAL
:
542 d_printf("External ");
544 case WBC_DOMINFO_TRUSTTYPE_IN_FOREST
:
545 d_printf("In-Forest ");
549 if (domain_list
[i
].trust_flags
& WBC_DOMINFO_TRUST_TRANSITIVE
) {
555 if (domain_list
[i
].trust_flags
& WBC_DOMINFO_TRUST_INCOMING
) {
561 if (domain_list
[i
].trust_flags
& WBC_DOMINFO_TRUST_OUTGOING
) {
570 wbcFreeMemory(domain_list
);
575 /* List own domain */
577 static bool wbinfo_list_own_domain(void)
579 d_printf("%s\n", get_winbind_domain());
584 /* show sequence numbers */
585 static bool wbinfo_show_sequence(const char *domain
)
587 d_printf("This command has been deprecated. Please use the "
588 "--online-status option instead.\n");
592 /* show sequence numbers */
593 static bool wbinfo_show_onlinestatus(const char *domain
)
595 struct wbcDomainInfo
*domain_list
= NULL
;
597 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
600 wbc_status
= wbcListTrusts(&domain_list
, &num_domains
);
601 if (!WBC_ERROR_IS_OK(wbc_status
)) {
602 d_fprintf(stderr
, "failed to call wbcListTrusts: %s\n",
603 wbcErrorString(wbc_status
));
607 for (i
=0; i
<num_domains
; i
++) {
611 if (!strequal(domain_list
[i
].short_name
, domain
)) {
616 is_offline
= (domain_list
[i
].domain_flags
&
617 WBC_DOMINFO_DOMAIN_OFFLINE
);
619 d_printf("%s : %s\n",
620 domain_list
[i
].short_name
,
621 is_offline
? "offline" : "online" );
624 wbcFreeMemory(domain_list
);
630 /* Show domain info */
632 static bool wbinfo_domain_info(const char *domain
)
634 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
635 struct wbcDomainInfo
*dinfo
= NULL
;
636 char sid_str
[WBC_SID_STRING_BUFLEN
];
638 if ((domain
== NULL
) || (strequal(domain
, ".")) || (domain
[0] == '\0')){
639 domain
= get_winbind_domain();
644 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
645 if (!WBC_ERROR_IS_OK(wbc_status
)) {
646 d_fprintf(stderr
, "failed to call wbcDomainInfo: %s\n",
647 wbcErrorString(wbc_status
));
651 wbcSidToStringBuf(&dinfo
->sid
, sid_str
, sizeof(sid_str
));
653 /* Display response */
655 d_printf("Name : %s\n", dinfo
->short_name
);
656 d_printf("Alt_Name : %s\n", dinfo
->dns_name
);
658 d_printf("SID : %s\n", sid_str
);
660 d_printf("Active Directory : %s\n",
661 (dinfo
->domain_flags
& WBC_DOMINFO_DOMAIN_AD
) ? "Yes" : "No");
662 d_printf("Native : %s\n",
663 (dinfo
->domain_flags
& WBC_DOMINFO_DOMAIN_NATIVE
) ?
666 d_printf("Primary : %s\n",
667 (dinfo
->domain_flags
& WBC_DOMINFO_DOMAIN_PRIMARY
) ?
670 wbcFreeMemory(dinfo
);
675 /* Get a foreign DC's name */
676 static bool wbinfo_getdcname(const char *domain_name
)
678 struct winbindd_request request
;
679 struct winbindd_response response
;
681 ZERO_STRUCT(request
);
682 ZERO_STRUCT(response
);
684 fstrcpy(request
.domain_name
, domain_name
);
688 if (winbindd_request_response(WINBINDD_GETDCNAME
, &request
,
689 &response
) != NSS_STATUS_SUCCESS
) {
690 d_fprintf(stderr
, "Could not get dc name for %s\n",domain_name
);
694 /* Display response */
696 d_printf("%s\n", response
.data
.dc_name
);
702 static bool wbinfo_dsgetdcname(const char *domain_name
, uint32_t flags
)
704 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
705 struct wbcDomainControllerInfoEx
*dc_info
;
708 wbc_status
= wbcLookupDomainControllerEx(domain_name
, NULL
, NULL
,
709 flags
| DS_DIRECTORY_SERVICE_REQUIRED
,
711 if (!WBC_ERROR_IS_OK(wbc_status
)) {
712 printf("Could not find dc for %s\n", domain_name
);
716 wbcGuidToString(dc_info
->domain_guid
, &str
);
718 d_printf("%s\n", dc_info
->dc_unc
);
719 d_printf("%s\n", dc_info
->dc_address
);
720 d_printf("%d\n", dc_info
->dc_address_type
);
721 d_printf("%s\n", str
);
722 d_printf("%s\n", dc_info
->domain_name
);
723 d_printf("%s\n", dc_info
->forest_name
);
724 d_printf("0x%08x\n", dc_info
->dc_flags
);
725 d_printf("%s\n", dc_info
->dc_site_name
);
726 d_printf("%s\n", dc_info
->client_site_name
);
731 /* Check trust account password */
733 static bool wbinfo_check_secret(const char *domain
)
735 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
736 struct wbcAuthErrorInfo
*error
= NULL
;
737 const char *domain_name
;
740 domain_name
= domain
;
742 domain_name
= get_winbind_domain();
745 wbc_status
= wbcCheckTrustCredentials(domain_name
, &error
);
747 d_printf("checking the trust secret for domain %s via RPC calls %s\n",
749 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
751 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
752 d_fprintf(stderr
, "error code was %s (0x%x)\n",
753 error
->nt_string
, error
->nt_status
);
754 wbcFreeMemory(error
);
756 if (!WBC_ERROR_IS_OK(wbc_status
)) {
757 d_fprintf(stderr
, "failed to call wbcCheckTrustCredentials: "
758 "%s\n", wbcErrorString(wbc_status
));
765 /* Find the currently connected DCs */
767 static bool wbinfo_dc_info(const char *domain_name
)
769 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
771 const char **dc_names
, **dc_ips
;
773 wbc_status
= wbcDcInfo(domain_name
, &num_dcs
,
775 if (!WBC_ERROR_IS_OK(wbc_status
)) {
776 printf("Could not find dc info %s\n",
777 domain_name
? domain_name
: "our domain");
781 for (i
=0; i
<num_dcs
; i
++) {
782 printf("%s (%s)\n", dc_names
[i
], dc_ips
[i
]);
784 wbcFreeMemory(dc_names
);
785 wbcFreeMemory(dc_ips
);
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
[WBC_SID_STRING_BUFLEN
];
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 wbcSidToStringBuf(&sid
, sid_str
, sizeof(sid_str
));
869 /* Display response */
871 d_printf("%s\n", sid_str
);
876 /* Convert gid to sid */
878 static bool wbinfo_gid_to_sid(gid_t gid
)
880 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
881 struct wbcDomainSid sid
;
882 char sid_str
[WBC_SID_STRING_BUFLEN
];
886 wbc_status
= wbcGidToSid(gid
, &sid
);
887 if (!WBC_ERROR_IS_OK(wbc_status
)) {
888 d_fprintf(stderr
, "failed to call wbcGidToSid: %s\n",
889 wbcErrorString(wbc_status
));
893 wbcSidToStringBuf(&sid
, sid_str
, sizeof(sid_str
));
895 /* Display response */
897 d_printf("%s\n", sid_str
);
902 /* Convert sid to uid */
904 static bool wbinfo_sid_to_uid(const char *sid_str
)
906 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
907 struct wbcDomainSid sid
;
912 wbc_status
= wbcStringToSid(sid_str
, &sid
);
913 if (!WBC_ERROR_IS_OK(wbc_status
)) {
914 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
915 wbcErrorString(wbc_status
));
919 wbc_status
= wbcSidToUid(&sid
, &uid
);
920 if (!WBC_ERROR_IS_OK(wbc_status
)) {
921 d_fprintf(stderr
, "failed to call wbcSidToUid: %s\n",
922 wbcErrorString(wbc_status
));
926 /* Display response */
928 d_printf("%d\n", (int)uid
);
933 static bool wbinfo_sid_to_gid(const char *sid_str
)
935 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
936 struct wbcDomainSid sid
;
941 wbc_status
= wbcStringToSid(sid_str
, &sid
);
942 if (!WBC_ERROR_IS_OK(wbc_status
)) {
943 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
944 wbcErrorString(wbc_status
));
948 wbc_status
= wbcSidToGid(&sid
, &gid
);
949 if (!WBC_ERROR_IS_OK(wbc_status
)) {
950 d_fprintf(stderr
, "failed to call wbcSidToGid: %s\n",
951 wbcErrorString(wbc_status
));
955 /* Display response */
957 d_printf("%d\n", (int)gid
);
962 static bool wbinfo_allocate_uid(void)
964 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
969 wbc_status
= wbcAllocateUid(&uid
);
970 if (!WBC_ERROR_IS_OK(wbc_status
)) {
971 d_fprintf(stderr
, "failed to call wbcAllocateUid: %s\n",
972 wbcErrorString(wbc_status
));
976 /* Display response */
978 d_printf("New uid: %u\n", (unsigned int)uid
);
983 static bool wbinfo_allocate_gid(void)
985 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
990 wbc_status
= wbcAllocateGid(&gid
);
991 if (!WBC_ERROR_IS_OK(wbc_status
)) {
992 d_fprintf(stderr
, "failed to call wbcAllocateGid: %s\n",
993 wbcErrorString(wbc_status
));
997 /* Display response */
999 d_printf("New gid: %u\n", (unsigned int)gid
);
1004 static bool wbinfo_set_uid_mapping(uid_t uid
, const char *sid_str
)
1006 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1007 struct wbcDomainSid sid
;
1011 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1012 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1013 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1014 wbcErrorString(wbc_status
));
1018 wbc_status
= wbcSetUidMapping(uid
, &sid
);
1019 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1020 d_fprintf(stderr
, "failed to call wbcSetUidMapping: %s\n",
1021 wbcErrorString(wbc_status
));
1025 /* Display response */
1027 d_printf("uid %u now mapped to sid %s\n",
1028 (unsigned int)uid
, sid_str
);
1033 static bool wbinfo_set_gid_mapping(gid_t gid
, const char *sid_str
)
1035 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1036 struct wbcDomainSid sid
;
1040 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1041 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1042 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1043 wbcErrorString(wbc_status
));
1047 wbc_status
= wbcSetGidMapping(gid
, &sid
);
1048 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1049 d_fprintf(stderr
, "failed to call wbcSetGidMapping: %s\n",
1050 wbcErrorString(wbc_status
));
1054 /* Display response */
1056 d_printf("gid %u now mapped to sid %s\n",
1057 (unsigned int)gid
, sid_str
);
1062 static bool wbinfo_remove_uid_mapping(uid_t uid
, const char *sid_str
)
1064 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1065 struct wbcDomainSid sid
;
1069 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1070 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1071 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1072 wbcErrorString(wbc_status
));
1076 wbc_status
= wbcRemoveUidMapping(uid
, &sid
);
1077 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1078 d_fprintf(stderr
, "failed to call wbcRemoveUidMapping: %s\n",
1079 wbcErrorString(wbc_status
));
1083 /* Display response */
1085 d_printf("Removed uid %u to sid %s mapping\n",
1086 (unsigned int)uid
, sid_str
);
1091 static bool wbinfo_remove_gid_mapping(gid_t gid
, const char *sid_str
)
1093 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1094 struct wbcDomainSid sid
;
1098 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1099 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1100 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1101 wbcErrorString(wbc_status
));
1105 wbc_status
= wbcRemoveGidMapping(gid
, &sid
);
1106 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1107 d_fprintf(stderr
, "failed to call wbcRemoveGidMapping: %s\n",
1108 wbcErrorString(wbc_status
));
1112 /* Display response */
1114 d_printf("Removed gid %u to sid %s mapping\n",
1115 (unsigned int)gid
, sid_str
);
1120 /* Convert sid to string */
1122 static bool wbinfo_lookupsid(const char *sid_str
)
1124 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1125 struct wbcDomainSid sid
;
1128 enum wbcSidType type
;
1130 /* Send off request */
1132 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1133 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1134 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1135 wbcErrorString(wbc_status
));
1139 wbc_status
= wbcLookupSid(&sid
, &domain
, &name
, &type
);
1140 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1141 d_fprintf(stderr
, "failed to call wbcLookupSid: %s\n",
1142 wbcErrorString(wbc_status
));
1146 /* Display response */
1148 d_printf("%s%c%s %d\n",
1149 domain
, winbind_separator(), name
, type
);
1154 /* Convert sid to fullname */
1156 static bool wbinfo_lookupsid_fullname(const char *sid_str
)
1158 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1159 struct wbcDomainSid sid
;
1162 enum wbcSidType type
;
1164 /* Send off request */
1166 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1167 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1168 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1169 wbcErrorString(wbc_status
));
1173 wbc_status
= wbcGetDisplayName(&sid
, &domain
, &name
, &type
);
1174 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1175 d_fprintf(stderr
, "failed to call wbcGetDisplayName: %s\n",
1176 wbcErrorString(wbc_status
));
1180 /* Display response */
1182 d_printf("%s%c%s %d\n",
1183 domain
, winbind_separator(), name
, type
);
1188 /* Lookup a list of RIDs */
1190 static bool wbinfo_lookuprids(const char *domain
, const char *arg
)
1192 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1193 struct wbcDomainInfo
*dinfo
= NULL
;
1194 char *domain_name
= NULL
;
1195 const char **names
= NULL
;
1196 enum wbcSidType
*types
= NULL
;
1199 uint32_t *rids
= NULL
;
1202 TALLOC_CTX
*mem_ctx
= NULL
;
1205 if ((domain
== NULL
) || (strequal(domain
, ".")) || (domain
[0] == '\0')){
1206 domain
= get_winbind_domain();
1211 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
1212 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1213 d_printf("wbcDomainInfo(%s) failed: %s\n", domain
,
1214 wbcErrorString(wbc_status
));
1218 mem_ctx
= talloc_new(NULL
);
1219 if (mem_ctx
== NULL
) {
1220 d_printf("talloc_new failed\n");
1228 while (next_token_talloc(mem_ctx
, &p
, &ridstr
, " ,\n")) {
1229 uint32_t rid
= strtoul(ridstr
, NULL
, 10);
1230 rids
= talloc_realloc(mem_ctx
, rids
, uint32_t, num_rids
+ 1);
1232 d_printf("talloc_realloc failed\n");
1234 rids
[num_rids
] = rid
;
1239 d_printf("no rids\n");
1243 wbc_status
= wbcLookupRids(&dinfo
->sid
, num_rids
, rids
,
1244 (const char **)&domain_name
, &names
, &types
);
1245 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1246 d_printf("winbind_lookup_rids failed: %s\n",
1247 wbcErrorString(wbc_status
));
1251 d_printf("Domain: %s\n", domain_name
);
1253 for (i
=0; i
<num_rids
; i
++) {
1254 d_printf("%8d: %s (%s)\n", rids
[i
], names
[i
],
1255 wbcSidTypeString(types
[i
]));
1260 wbcFreeMemory(dinfo
);
1261 wbcFreeMemory(domain_name
);
1262 wbcFreeMemory(names
);
1263 wbcFreeMemory(types
);
1264 TALLOC_FREE(mem_ctx
);
1268 /* Convert string to sid */
1270 static bool wbinfo_lookupname(const char *full_name
)
1272 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1273 struct wbcDomainSid sid
;
1274 char sid_str
[WBC_SID_STRING_BUFLEN
];
1275 enum wbcSidType type
;
1276 fstring domain_name
;
1277 fstring account_name
;
1279 /* Send off request */
1281 parse_wbinfo_domain_user(full_name
, domain_name
,
1284 wbc_status
= wbcLookupName(domain_name
, account_name
,
1286 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1287 d_fprintf(stderr
, "failed to call wbcLookupName: %s\n",
1288 wbcErrorString(wbc_status
));
1292 wbcSidToStringBuf(&sid
, sid_str
, sizeof(sid_str
));
1294 /* Display response */
1296 d_printf("%s %s (%d)\n", sid_str
, wbcSidTypeString(type
), type
);
1301 static char *wbinfo_prompt_pass(TALLOC_CTX
*mem_ctx
,
1303 const char *username
)
1306 const char *ret
= NULL
;
1308 prompt
= talloc_asprintf(mem_ctx
, "Enter %s's ", username
);
1313 prompt
= talloc_asprintf_append(prompt
, "%s ", prefix
);
1318 prompt
= talloc_asprintf_append(prompt
, "password: ");
1323 ret
= getpass(prompt
);
1324 TALLOC_FREE(prompt
);
1326 return talloc_strdup(mem_ctx
, ret
);
1329 /* Authenticate a user with a plaintext password */
1331 static bool wbinfo_auth_krb5(char *username
, const char *cctype
, uint32_t flags
)
1333 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1336 char *password
= NULL
;
1338 char *local_cctype
= NULL
;
1340 struct wbcLogonUserParams params
;
1341 struct wbcLogonUserInfo
*info
;
1342 struct wbcAuthErrorInfo
*error
;
1343 struct wbcUserPasswordPolicyInfo
*policy
;
1344 TALLOC_CTX
*frame
= talloc_tos();
1346 if ((s
= talloc_strdup(frame
, username
)) == NULL
) {
1350 if ((p
= strchr(s
, '%')) != NULL
) {
1353 password
= talloc_strdup(frame
, p
);
1355 password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1358 local_cctype
= talloc_strdup(frame
, cctype
);
1364 params
.username
= name
;
1365 params
.password
= password
;
1366 params
.num_blobs
= 0;
1367 params
.blobs
= NULL
;
1369 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
,
1375 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1376 d_fprintf(stderr
, "failed to call wbcAddNamedBlob: %s\n",
1377 wbcErrorString(wbc_status
));
1381 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
,
1387 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1388 d_fprintf(stderr
, "failed to call wbcAddNamedBlob: %s\n",
1389 wbcErrorString(wbc_status
));
1393 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
,
1397 (uint8_t *)local_cctype
,
1399 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1400 d_fprintf(stderr
, "failed to call wbcAddNamedBlob: %s\n",
1401 wbcErrorString(wbc_status
));
1405 wbc_status
= wbcLogonUser(¶ms
, &info
, &error
, &policy
);
1407 d_printf("plaintext kerberos password authentication for [%s] %s "
1408 "(requesting cctype: %s)\n",
1409 username
, WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed",
1414 "error code was %s (0x%x)\nerror message was: %s\n",
1417 error
->display_string
);
1420 if (WBC_ERROR_IS_OK(wbc_status
)) {
1421 if (flags
& WBFLAG_PAM_INFO3_TEXT
) {
1422 if (info
&& info
->info
&& info
->info
->user_flags
&
1423 NETLOGON_CACHED_ACCOUNT
) {
1424 d_printf("user_flgs: "
1425 "NETLOGON_CACHED_ACCOUNT\n");
1431 for (i
=0; i
< info
->num_blobs
; i
++) {
1432 if (strequal(info
->blobs
[i
].name
,
1434 d_printf("credentials were put "
1437 info
->blobs
[i
].blob
.data
);
1442 d_printf("no credentials cached\n");
1447 wbcFreeMemory(params
.blobs
);
1449 return WBC_ERROR_IS_OK(wbc_status
);
1452 /* Authenticate a user with a plaintext password */
1454 static bool wbinfo_auth(char *username
)
1456 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1459 char *password
= NULL
;
1461 TALLOC_CTX
*frame
= talloc_tos();
1463 if ((s
= talloc_strdup(frame
, username
)) == NULL
) {
1467 if ((p
= strchr(s
, '%')) != NULL
) {
1470 password
= talloc_strdup(frame
, p
);
1472 password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1477 wbc_status
= wbcAuthenticateUser(name
, password
);
1479 d_printf("plaintext password authentication %s\n",
1480 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1483 if (response
.data
.auth
.nt_status
)
1485 "error code was %s (0x%x)\nerror message was: %s\n",
1486 response
.data
.auth
.nt_status_string
,
1487 response
.data
.auth
.nt_status
,
1488 response
.data
.auth
.error_string
);
1491 return WBC_ERROR_IS_OK(wbc_status
);
1494 /* Authenticate a user with a challenge/response */
1496 static bool wbinfo_auth_crap(char *username
, bool use_ntlmv2
, bool use_lanman
)
1498 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1499 struct wbcAuthUserParams params
;
1500 struct wbcAuthUserInfo
*info
= NULL
;
1501 struct wbcAuthErrorInfo
*err
= NULL
;
1502 DATA_BLOB lm
= data_blob_null
;
1503 DATA_BLOB nt
= data_blob_null
;
1505 fstring name_domain
;
1508 TALLOC_CTX
*frame
= talloc_tos();
1510 p
= strchr(username
, '%');
1514 pass
= talloc_strdup(frame
, p
+ 1);
1516 pass
= wbinfo_prompt_pass(frame
, NULL
, username
);
1519 parse_wbinfo_domain_user(username
, name_domain
, name_user
);
1521 params
.account_name
= name_user
;
1522 params
.domain_name
= name_domain
;
1523 params
.workstation_name
= NULL
;
1526 params
.parameter_control
= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
|
1527 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
;
1529 params
.level
= WBC_AUTH_USER_LEVEL_RESPONSE
;
1531 generate_random_buffer(params
.password
.response
.challenge
, 8);
1534 DATA_BLOB server_chal
;
1535 DATA_BLOB names_blob
;
1537 server_chal
= data_blob(params
.password
.response
.challenge
, 8);
1539 /* Pretend this is a login to 'us', for blob purposes */
1540 names_blob
= NTLMv2_generate_names_blob(NULL
,
1541 get_winbind_netbios_name(),
1542 get_winbind_domain());
1544 if (!SMBNTLMv2encrypt(NULL
, name_user
, name_domain
, pass
,
1547 &lm
, &nt
, NULL
, NULL
)) {
1548 data_blob_free(&names_blob
);
1549 data_blob_free(&server_chal
);
1553 data_blob_free(&names_blob
);
1554 data_blob_free(&server_chal
);
1559 lm
= data_blob(NULL
, 24);
1560 ok
= SMBencrypt(pass
,
1561 params
.password
.response
.challenge
,
1564 data_blob_free(&lm
);
1567 nt
= data_blob(NULL
, 24);
1568 SMBNTencrypt(pass
, params
.password
.response
.challenge
,
1572 params
.password
.response
.nt_length
= nt
.length
;
1573 params
.password
.response
.nt_data
= nt
.data
;
1574 params
.password
.response
.lm_length
= lm
.length
;
1575 params
.password
.response
.lm_data
= lm
.data
;
1577 wbc_status
= wbcAuthenticateUserEx(¶ms
, &info
, &err
);
1579 /* Display response */
1581 d_printf("challenge/response password authentication %s\n",
1582 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1584 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
1586 "error code was %s (0x%x)\nerror message was: %s\n",
1589 err
->display_string
);
1591 } else if (WBC_ERROR_IS_OK(wbc_status
)) {
1592 wbcFreeMemory(info
);
1595 data_blob_free(&nt
);
1596 data_blob_free(&lm
);
1598 return WBC_ERROR_IS_OK(wbc_status
);
1601 /* Authenticate a user with a plaintext password */
1603 static bool wbinfo_pam_logon(char *username
)
1605 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1606 struct wbcLogonUserParams params
;
1607 struct wbcAuthErrorInfo
*error
;
1610 TALLOC_CTX
*frame
= talloc_tos();
1614 ZERO_STRUCT(params
);
1616 if ((s
= talloc_strdup(frame
, username
)) == NULL
) {
1620 if ((p
= strchr(s
, '%')) != NULL
) {
1623 params
.password
= talloc_strdup(frame
, p
);
1625 params
.password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1627 params
.username
= s
;
1629 flags
= WBFLAG_PAM_CACHED_LOGIN
;
1631 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
, ¶ms
.blobs
,
1633 (uint8_t *)&flags
, sizeof(flags
));
1634 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1635 d_printf("wbcAddNamedBlob failed: %s\n",
1636 wbcErrorString(wbc_status
));
1642 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
, ¶ms
.blobs
,
1644 (uint8_t *)&uid
, sizeof(uid
));
1645 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1646 d_printf("wbcAddNamedBlob failed: %s\n",
1647 wbcErrorString(wbc_status
));
1651 wbc_status
= wbcLogonUser(¶ms
, NULL
, &error
, NULL
);
1653 wbcFreeMemory(params
.blobs
);
1655 d_printf("plaintext password authentication %s\n",
1656 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1658 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1660 "error code was %s (0x%x)\nerror message was: %s\n",
1662 (int)error
->nt_status
,
1663 error
->display_string
);
1664 wbcFreeMemory(error
);
1670 /* Save creds with winbind */
1672 static bool wbinfo_ccache_save(char *username
)
1674 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1677 char *password
= NULL
;
1679 TALLOC_CTX
*frame
= talloc_stackframe();
1681 s
= talloc_strdup(frame
, username
);
1690 password
= talloc_strdup(frame
, p
);
1692 password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1697 wbc_status
= wbcCredentialSave(name
, password
);
1699 d_printf("saving creds %s\n",
1700 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1704 return WBC_ERROR_IS_OK(wbc_status
);
1707 #ifdef WITH_FAKE_KASERVER
1708 /* Authenticate a user with a plaintext password and set a token */
1710 static bool wbinfo_klog(char *username
)
1712 struct winbindd_request request
;
1713 struct winbindd_response response
;
1717 /* Send off request */
1719 ZERO_STRUCT(request
);
1720 ZERO_STRUCT(response
);
1722 p
= strchr(username
, '%');
1726 fstrcpy(request
.data
.auth
.user
, username
);
1727 fstrcpy(request
.data
.auth
.pass
, p
+ 1);
1730 fstrcpy(request
.data
.auth
.user
, username
);
1731 fstrcpy(request
.data
.auth
.pass
, getpass("Password: "));
1734 request
.flags
|= WBFLAG_PAM_AFS_TOKEN
;
1736 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
,
1739 /* Display response */
1741 d_printf("plaintext password authentication %s\n",
1742 (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed");
1744 if (response
.data
.auth
.nt_status
)
1746 "error code was %s (0x%x)\nerror message was: %s\n",
1747 response
.data
.auth
.nt_status_string
,
1748 response
.data
.auth
.nt_status
,
1749 response
.data
.auth
.error_string
);
1751 if (result
!= NSS_STATUS_SUCCESS
)
1754 if (response
.extra_data
.data
== NULL
) {
1755 d_fprintf(stderr
, "Did not get token data\n");
1759 if (!afs_settoken_str((char *)response
.extra_data
.data
)) {
1760 d_fprintf(stderr
, "Could not set token\n");
1764 d_printf("Successfully created AFS token\n");
1768 static bool wbinfo_klog(char *username
)
1770 d_fprintf(stderr
, "No AFS support compiled in.\n");
1775 /* Print domain users */
1777 static bool print_domain_users(const char *domain
)
1779 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1781 uint32_t num_users
= 0;
1782 const char **users
= NULL
;
1784 /* Send request to winbind daemon */
1786 /* '.' is the special sign for our own domain */
1787 if (domain
&& strcmp(domain
, ".") == 0) {
1788 domain
= get_winbind_domain();
1791 wbc_status
= wbcListUsers(domain
, &num_users
, &users
);
1792 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1796 for (i
=0; i
< num_users
; i
++) {
1797 d_printf("%s\n", users
[i
]);
1800 wbcFreeMemory(users
);
1805 /* Print domain groups */
1807 static bool print_domain_groups(const char *domain
)
1809 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1811 uint32_t num_groups
= 0;
1812 const char **groups
= NULL
;
1814 /* Send request to winbind daemon */
1816 /* '.' is the special sign for our own domain */
1817 if (domain
&& strcmp(domain
, ".") == 0) {
1818 domain
= get_winbind_domain();
1821 wbc_status
= wbcListGroups(domain
, &num_groups
, &groups
);
1822 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1823 d_fprintf(stderr
, "failed to call wbcListGroups: %s\n",
1824 wbcErrorString(wbc_status
));
1828 for (i
=0; i
< num_groups
; i
++) {
1829 d_printf("%s\n", groups
[i
]);
1832 wbcFreeMemory(groups
);
1837 /* Set the authorised user for winbindd access in secrets.tdb */
1839 static bool wbinfo_set_auth_user(char *username
)
1841 d_fprintf(stderr
, "This functionality was moved to the 'net' utility.\n"
1842 "See 'net help setauthuser' for details.\n");
1846 static void wbinfo_get_auth_user(void)
1848 d_fprintf(stderr
, "This functionality was moved to the 'net' utility.\n"
1849 "See 'net help getauthuser' for details.\n");
1852 static bool wbinfo_ping(void)
1856 wbc_status
= wbcPing();
1858 /* Display response */
1860 d_printf("Ping to winbindd %s\n",
1861 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1863 return WBC_ERROR_IS_OK(wbc_status
);
1866 static bool wbinfo_change_user_password(const char *username
)
1869 char *old_password
= NULL
;
1870 char *new_password
= NULL
;
1871 TALLOC_CTX
*frame
= talloc_tos();
1873 old_password
= wbinfo_prompt_pass(frame
, "old", username
);
1874 new_password
= wbinfo_prompt_pass(frame
, "new", username
);
1876 wbc_status
= wbcChangeUserPassword(username
, old_password
,new_password
);
1878 /* Display response */
1880 d_printf("Password change for user %s %s\n", username
,
1881 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1883 return WBC_ERROR_IS_OK(wbc_status
);
1889 OPT_SET_AUTH_USER
= 1000,
1901 OPT_SET_UID_MAPPING
,
1902 OPT_SET_GID_MAPPING
,
1903 OPT_REMOVE_UID_MAPPING
,
1904 OPT_REMOVE_GID_MAPPING
,
1906 OPT_LIST_ALL_DOMAINS
,
1907 OPT_LIST_OWN_DOMAIN
,
1914 OPT_CHANGE_USER_PASSWORD
,
1916 OPT_SID_TO_FULLNAME
,
1925 int main(int argc
, char **argv
, char **envp
)
1928 TALLOC_CTX
*frame
= talloc_stackframe();
1930 static char *string_arg
;
1931 char *string_subarg
= NULL
;
1932 static char *opt_domain_name
;
1934 int int_subarg
= -1;
1936 bool verbose
= false;
1937 bool use_ntlmv2
= false;
1938 bool use_lanman
= false;
1939 char *logoff_user
= getenv("USER");
1940 int logoff_uid
= geteuid();
1942 struct poptOption long_options
[] = {
1945 /* longName, shortName, argInfo, argPtr, value, descrip,
1948 { "domain-users", 'u', POPT_ARG_NONE
, 0, 'u', "Lists all domain users", "domain"},
1949 { "domain-groups", 'g', POPT_ARG_NONE
, 0, 'g', "Lists all domain groups", "domain" },
1950 { "WINS-by-name", 'N', POPT_ARG_STRING
, &string_arg
, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1951 { "WINS-by-ip", 'I', POPT_ARG_STRING
, &string_arg
, 'I', "Converts IP address to NetBIOS name", "IP" },
1952 { "name-to-sid", 'n', POPT_ARG_STRING
, &string_arg
, 'n', "Converts name to sid", "NAME" },
1953 { "sid-to-name", 's', POPT_ARG_STRING
, &string_arg
, 's', "Converts sid to name", "SID" },
1954 { "sid-to-fullname", 0, POPT_ARG_STRING
, &string_arg
,
1955 OPT_SID_TO_FULLNAME
, "Converts sid to fullname", "SID" },
1956 { "lookup-rids", 'R', POPT_ARG_STRING
, &string_arg
, 'R', "Converts RIDs to names", "RIDs" },
1957 { "uid-to-sid", 'U', POPT_ARG_INT
, &int_arg
, 'U', "Converts uid to sid" , "UID" },
1958 { "gid-to-sid", 'G', POPT_ARG_INT
, &int_arg
, 'G', "Converts gid to sid", "GID" },
1959 { "sid-to-uid", 'S', POPT_ARG_STRING
, &string_arg
, 'S', "Converts sid to uid", "SID" },
1960 { "sid-to-gid", 'Y', POPT_ARG_STRING
, &string_arg
, 'Y', "Converts sid to gid", "SID" },
1961 { "allocate-uid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_UID
,
1962 "Get a new UID out of idmap" },
1963 { "allocate-gid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_GID
,
1964 "Get a new GID out of idmap" },
1965 { "set-uid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_UID_MAPPING
, "Create or modify uid to sid mapping in idmap", "UID,SID" },
1966 { "set-gid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_GID_MAPPING
, "Create or modify gid to sid mapping in idmap", "GID,SID" },
1967 { "remove-uid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_REMOVE_UID_MAPPING
, "Remove uid to sid mapping in idmap", "UID,SID" },
1968 { "remove-gid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_REMOVE_GID_MAPPING
, "Remove gid to sid mapping in idmap", "GID,SID" },
1969 { "check-secret", 't', POPT_ARG_NONE
, 0, 't', "Check shared secret" },
1970 { "change-secret", 'c', POPT_ARG_NONE
, 0, 'c', "Change shared secret" },
1971 { "ping-dc", 'P', POPT_ARG_NONE
, 0, 'P',
1972 "Check the NETLOGON connection" },
1973 { "trusted-domains", 'm', POPT_ARG_NONE
, 0, 'm', "List trusted domains" },
1974 { "all-domains", 0, POPT_ARG_NONE
, 0, OPT_LIST_ALL_DOMAINS
, "List all domains (trusted and own domain)" },
1975 { "own-domain", 0, POPT_ARG_NONE
, 0, OPT_LIST_OWN_DOMAIN
, "List own domain" },
1976 { "sequence", 0, POPT_ARG_NONE
, 0, OPT_SEQUENCE
, "Deprecated command, see --online-status" },
1977 { "online-status", 0, POPT_ARG_NONE
, 0, OPT_ONLINESTATUS
, "Show whether domains are marked as online or offline"},
1978 { "domain-info", 'D', POPT_ARG_STRING
, &string_arg
, 'D', "Show most of the info we have about the domain" },
1979 { "user-info", 'i', POPT_ARG_STRING
, &string_arg
, 'i', "Get user info", "USER" },
1980 { "uid-info", 0, POPT_ARG_INT
, &int_arg
, OPT_UID_INFO
, "Get user info from uid", "UID" },
1981 { "group-info", 0, POPT_ARG_STRING
, &string_arg
, OPT_GROUP_INFO
, "Get group info", "GROUP" },
1982 { "user-sidinfo", 0, POPT_ARG_STRING
, &string_arg
, OPT_USER_SIDINFO
, "Get user info from sid", "SID" },
1983 { "gid-info", 0, POPT_ARG_INT
, &int_arg
, OPT_GID_INFO
, "Get group info from gid", "GID" },
1984 { "user-groups", 'r', POPT_ARG_STRING
, &string_arg
, 'r', "Get user groups", "USER" },
1985 { "user-domgroups", 0, POPT_ARG_STRING
, &string_arg
,
1986 OPT_USERDOMGROUPS
, "Get user domain groups", "SID" },
1987 { "sid-aliases", 0, POPT_ARG_STRING
, &string_arg
, OPT_SIDALIASES
, "Get sid aliases", "SID" },
1988 { "user-sids", 0, POPT_ARG_STRING
, &string_arg
, OPT_USERSIDS
, "Get user group sids for user SID", "SID" },
1989 { "authenticate", 'a', POPT_ARG_STRING
, &string_arg
, 'a', "authenticate user", "user%password" },
1990 { "pam-logon", 0, POPT_ARG_STRING
, &string_arg
, OPT_PAM_LOGON
,
1991 "do a pam logon equivalent", "user%password" },
1992 { "logoff", 0, POPT_ARG_NONE
, NULL
, OPT_LOGOFF
,
1993 "log off user", "uid" },
1994 { "logoff-user", 0, POPT_ARG_STRING
, &logoff_user
,
1995 OPT_LOGOFF_USER
, "username to log off" },
1996 { "logoff-uid", 0, POPT_ARG_INT
, &logoff_uid
,
1997 OPT_LOGOFF_UID
, "uid to log off" },
1998 { "set-auth-user", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_AUTH_USER
, "Store user and password used by winbindd (root only)", "user%password" },
1999 { "ccache-save", 0, POPT_ARG_STRING
, &string_arg
,
2000 OPT_CCACHE_SAVE
, "Store user and password for ccache "
2001 "operation", "user%password" },
2002 { "getdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_GETDCNAME
,
2003 "Get a DC name for a foreign domain", "domainname" },
2004 { "dsgetdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_DSGETDCNAME
, "Find a DC for a domain", "domainname" },
2005 { "dc-info", 0, POPT_ARG_STRING
, &string_arg
, OPT_DC_INFO
,
2006 "Find the currently known DCs", "domainname" },
2007 { "get-auth-user", 0, POPT_ARG_NONE
, NULL
, OPT_GET_AUTH_USER
, "Retrieve user and password used by winbindd (root only)", NULL
},
2008 { "ping", 'p', POPT_ARG_NONE
, 0, 'p', "Ping winbindd to see if it is alive" },
2009 { "domain", 0, POPT_ARG_STRING
, &opt_domain_name
, OPT_DOMAIN_NAME
, "Define to the domain to restrict operation", "domain" },
2010 #ifdef WITH_FAKE_KASERVER
2011 { "klog", 'k', POPT_ARG_STRING
, &string_arg
, 'k', "set an AFS token from winbind", "user%password" },
2014 { "krb5auth", 'K', POPT_ARG_STRING
, &string_arg
, 'K', "authenticate user using Kerberos", "user%password" },
2015 /* destroys wbinfo --help output */
2016 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
2018 { "separator", 0, POPT_ARG_NONE
, 0, OPT_SEPARATOR
, "Get the active winbind separator", NULL
},
2019 { "verbose", 0, POPT_ARG_NONE
, 0, OPT_VERBOSE
, "Print additional information per command", NULL
},
2020 { "change-user-password", 0, POPT_ARG_STRING
, &string_arg
, OPT_CHANGE_USER_PASSWORD
, "Change the password for a user", NULL
},
2021 { "ntlmv2", 0, POPT_ARG_NONE
, 0, OPT_NTLMV2
, "Use NTLMv2 cryptography for user authentication", NULL
},
2022 { "lanman", 0, POPT_ARG_NONE
, 0, OPT_LANMAN
, "Use lanman cryptography for user authentication", NULL
},
2027 /* Samba client initialisation */
2033 pc
= poptGetContext("wbinfo", argc
, (const char **)argv
,
2036 /* Parse command line options */
2039 poptPrintHelp(pc
, stderr
, 0);
2043 while((opt
= poptGetNextOpt(pc
)) != -1) {
2044 /* get the generic configuration parameters like --domain */
2058 poptFreeContext(pc
);
2060 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
2061 POPT_CONTEXT_KEEP_FIRST
);
2063 while((opt
= poptGetNextOpt(pc
)) != -1) {
2066 if (!print_domain_users(opt_domain_name
)) {
2068 "Error looking up domain users\n");
2073 if (!print_domain_groups(opt_domain_name
)) {
2075 "Error looking up domain groups\n");
2080 if (!wbinfo_lookupsid(string_arg
)) {
2082 "Could not lookup sid %s\n",
2087 case OPT_SID_TO_FULLNAME
:
2088 if (!wbinfo_lookupsid_fullname(string_arg
)) {
2089 d_fprintf(stderr
, "Could not lookup sid %s\n",
2095 if (!wbinfo_lookuprids(opt_domain_name
, string_arg
)) {
2096 d_fprintf(stderr
, "Could not lookup RIDs %s\n",
2102 if (!wbinfo_lookupname(string_arg
)) {
2103 d_fprintf(stderr
, "Could not lookup name %s\n",
2109 if (!wbinfo_wins_byname(string_arg
)) {
2111 "Could not lookup WINS by name %s\n",
2117 if (!wbinfo_wins_byip(string_arg
)) {
2119 "Could not lookup WINS by IP %s\n",
2125 if (!wbinfo_uid_to_sid(int_arg
)) {
2127 "Could not convert uid %d to sid\n",
2133 if (!wbinfo_gid_to_sid(int_arg
)) {
2135 "Could not convert gid %d to sid\n",
2141 if (!wbinfo_sid_to_uid(string_arg
)) {
2143 "Could not convert sid %s to uid\n",
2149 if (!wbinfo_sid_to_gid(string_arg
)) {
2151 "Could not convert sid %s to gid\n",
2156 case OPT_ALLOCATE_UID
:
2157 if (!wbinfo_allocate_uid()) {
2158 d_fprintf(stderr
, "Could not allocate a uid\n");
2162 case OPT_ALLOCATE_GID
:
2163 if (!wbinfo_allocate_gid()) {
2164 d_fprintf(stderr
, "Could not allocate a gid\n");
2168 case OPT_SET_UID_MAPPING
:
2169 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2171 !wbinfo_set_uid_mapping(int_subarg
, string_subarg
))
2173 d_fprintf(stderr
, "Could not create or modify "
2174 "uid to sid mapping\n");
2178 case OPT_SET_GID_MAPPING
:
2179 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2181 !wbinfo_set_gid_mapping(int_subarg
, string_subarg
))
2183 d_fprintf(stderr
, "Could not create or modify "
2184 "gid to sid mapping\n");
2188 case OPT_REMOVE_UID_MAPPING
:
2189 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2191 !wbinfo_remove_uid_mapping(int_subarg
,
2194 d_fprintf(stderr
, "Could not remove uid to sid "
2199 case OPT_REMOVE_GID_MAPPING
:
2200 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2202 !wbinfo_remove_gid_mapping(int_subarg
,
2205 d_fprintf(stderr
, "Could not remove gid to sid "
2211 if (!wbinfo_check_secret(opt_domain_name
)) {
2212 d_fprintf(stderr
, "Could not check secret\n");
2217 if (!wbinfo_change_secret(opt_domain_name
)) {
2218 d_fprintf(stderr
, "Could not change secret\n");
2223 if (!wbinfo_ping_dc()) {
2224 d_fprintf(stderr
, "Could not ping our DC\n");
2229 if (!wbinfo_list_domains(false, verbose
)) {
2231 "Could not list trusted domains\n");
2236 if (!wbinfo_show_sequence(opt_domain_name
)) {
2238 "Could not show sequence numbers\n");
2242 case OPT_ONLINESTATUS
:
2243 if (!wbinfo_show_onlinestatus(opt_domain_name
)) {
2245 "Could not show online-status\n");
2250 if (!wbinfo_domain_info(string_arg
)) {
2252 "Could not get domain info\n");
2257 if (!wbinfo_get_userinfo(string_arg
)) {
2259 "Could not get info for user %s\n",
2264 case OPT_USER_SIDINFO
:
2265 if ( !wbinfo_get_user_sidinfo(string_arg
)) {
2267 "Could not get info for user "
2268 "sid %s\n", string_arg
);
2273 if ( !wbinfo_get_uidinfo(int_arg
)) {
2274 d_fprintf(stderr
, "Could not get info for uid "
2279 case OPT_GROUP_INFO
:
2280 if ( !wbinfo_get_groupinfo(string_arg
)) {
2281 d_fprintf(stderr
, "Could not get info for "
2282 "group %s\n", string_arg
);
2287 if ( !wbinfo_get_gidinfo(int_arg
)) {
2288 d_fprintf(stderr
, "Could not get info for gid "
2294 if (!wbinfo_get_usergroups(string_arg
)) {
2296 "Could not get groups for user %s\n",
2302 if (!wbinfo_get_usersids(string_arg
)) {
2303 d_fprintf(stderr
, "Could not get group SIDs "
2304 "for user SID %s\n",
2309 case OPT_USERDOMGROUPS
:
2310 if (!wbinfo_get_userdomgroups(string_arg
)) {
2311 d_fprintf(stderr
, "Could not get user's domain "
2312 "groups for user SID %s\n",
2317 case OPT_SIDALIASES
:
2318 if (!wbinfo_get_sidaliases(opt_domain_name
,
2320 d_fprintf(stderr
, "Could not get sid aliases "
2321 "for user SID %s\n", string_arg
);
2326 bool got_error
= false;
2328 if (!wbinfo_auth(string_arg
)) {
2330 "Could not authenticate user "
2331 "%s with plaintext "
2332 "password\n", string_arg
);
2336 if (!wbinfo_auth_crap(string_arg
, use_ntlmv2
,
2339 "Could not authenticate user "
2340 "%s with challenge/response\n",
2350 if (!wbinfo_pam_logon(string_arg
)) {
2351 d_fprintf(stderr
, "pam_logon failed for %s\n",
2360 wbc_status
= wbcLogoffUser(logoff_user
, logoff_uid
,
2362 d_printf("Logoff %s (%d): %s\n", logoff_user
,
2363 logoff_uid
, wbcErrorString(wbc_status
));
2367 uint32_t flags
= WBFLAG_PAM_KRB5
|
2368 WBFLAG_PAM_CACHED_LOGIN
|
2369 WBFLAG_PAM_FALLBACK_AFTER_KRB5
|
2370 WBFLAG_PAM_INFO3_TEXT
|
2371 WBFLAG_PAM_CONTACT_TRUSTDOM
;
2373 if (!wbinfo_auth_krb5(string_arg
, "FILE",
2376 "Could not authenticate user "
2377 "[%s] with Kerberos "
2378 "(ccache: %s)\n", string_arg
,
2385 if (!wbinfo_klog(string_arg
)) {
2386 d_fprintf(stderr
, "Could not klog user\n");
2391 if (!wbinfo_ping()) {
2392 d_fprintf(stderr
, "could not ping winbindd!\n");
2396 case OPT_SET_AUTH_USER
:
2397 if (!wbinfo_set_auth_user(string_arg
)) {
2401 case OPT_GET_AUTH_USER
:
2402 wbinfo_get_auth_user();
2405 case OPT_CCACHE_SAVE
:
2406 if (!wbinfo_ccache_save(string_arg
)) {
2411 if (!wbinfo_getdcname(string_arg
)) {
2415 case OPT_DSGETDCNAME
:
2416 if (!wbinfo_dsgetdcname(string_arg
, 0)) {
2421 if (!wbinfo_dc_info(string_arg
)) {
2425 case OPT_SEPARATOR
: {
2426 const char sep
= winbind_separator();
2430 d_printf("%c\n", sep
);
2433 case OPT_LIST_ALL_DOMAINS
:
2434 if (!wbinfo_list_domains(true, verbose
)) {
2438 case OPT_LIST_OWN_DOMAIN
:
2439 if (!wbinfo_list_own_domain()) {
2443 case OPT_CHANGE_USER_PASSWORD
:
2444 if (!wbinfo_change_user_password(string_arg
)) {
2446 "Could not change user password "
2447 "for user %s\n", string_arg
);
2452 /* generic configuration options */
2453 case OPT_DOMAIN_NAME
:
2457 case OPT_LOGOFF_USER
:
2458 case OPT_LOGOFF_UID
:
2461 d_fprintf(stderr
, "Invalid option\n");
2462 poptPrintHelp(pc
, stderr
, 0);
2474 poptFreeContext(pc
);