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 "winbind_client.h"
26 #include "libwbclient/wbclient.h"
27 #include "lib/popt/popt.h"
28 #include "../libcli/auth/libcli_auth.h"
29 #if (_SAMBA_BUILD_) >= 4
30 #include "lib/cmdline/popt_common.h"
35 #define DBGC_CLASS DBGC_WINBIND
38 static struct wbcInterfaceDetails
*init_interface_details(void)
40 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
41 static struct wbcInterfaceDetails
*details
;
47 wbc_status
= wbcInterfaceDetails(&details
);
48 if (!WBC_ERROR_IS_OK(wbc_status
)) {
49 d_fprintf(stderr
, "could not obtain winbind interface "
50 "details: %s\n", wbcErrorString(wbc_status
));
56 static char winbind_separator(void)
58 struct wbcInterfaceDetails
*details
;
65 details
= init_interface_details();
68 d_fprintf(stderr
, "could not obtain winbind separator!\n");
72 sep
= details
->winbind_separator
;
76 d_fprintf(stderr
, "winbind separator was NULL!\n");
83 static const char *get_winbind_domain(void)
85 static struct wbcInterfaceDetails
*details
;
87 details
= init_interface_details();
90 d_fprintf(stderr
, "could not obtain winbind domain name!\n");
94 return details
->netbios_domain
;
97 static const char *get_winbind_netbios_name(void)
99 static struct wbcInterfaceDetails
*details
;
101 details
= init_interface_details();
104 d_fprintf(stderr
, "could not obtain winbind netbios name!\n");
108 return details
->netbios_name
;
111 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
112 form DOMAIN/user into a domain and a user */
114 static bool parse_wbinfo_domain_user(const char *domuser
, fstring domain
,
118 char *p
= strchr(domuser
,winbind_separator());
121 /* Maybe it was a UPN? */
122 if ((p
= strchr(domuser
, '@')) != NULL
) {
124 fstrcpy(user
, domuser
);
128 fstrcpy(user
, domuser
);
129 fstrcpy(domain
, get_winbind_domain());
134 fstrcpy(domain
, domuser
);
135 domain
[PTR_DIFF(p
, domuser
)] = 0;
141 /* Parse string of "uid,sid" or "gid,sid" into separate int and string values.
142 * Return true if input was valid, false otherwise. */
143 static bool parse_mapping_arg(char *arg
, int *id
, char **sid
)
150 tmp
= strtok(arg
, ",");
151 *sid
= strtok(NULL
, ",");
153 if (!tmp
|| !*tmp
|| !*sid
|| !**sid
)
156 /* Because atoi() can return 0 on invalid input, which would be a valid
157 * UID/GID we must use strtoul() and do error checking */
158 *id
= strtoul(tmp
, &endptr
, 10);
160 if (endptr
[0] != '\0')
166 /* pull pwent info for a given user */
168 static bool wbinfo_get_userinfo(char *user
)
170 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
171 struct passwd
*pwd
= NULL
;
173 wbc_status
= wbcGetpwnam(user
, &pwd
);
174 if (!WBC_ERROR_IS_OK(wbc_status
)) {
175 d_fprintf(stderr
, "failed to call wbcGetpwnam: %s",
176 wbcErrorString(wbc_status
));
180 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
183 (unsigned int)pwd
->pw_uid
,
184 (unsigned int)pwd
->pw_gid
,
192 /* pull pwent info for a given uid */
193 static bool wbinfo_get_uidinfo(int uid
)
195 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
196 struct passwd
*pwd
= NULL
;
198 wbc_status
= wbcGetpwuid(uid
, &pwd
);
199 if (!WBC_ERROR_IS_OK(wbc_status
)) {
200 d_fprintf(stderr
, "failed to call wbcGetpwuid: %s",
201 wbcErrorString(wbc_status
));
205 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
208 (unsigned int)pwd
->pw_uid
,
209 (unsigned int)pwd
->pw_gid
,
217 static bool wbinfo_get_user_sidinfo(const char *sid_str
)
219 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
220 struct passwd
*pwd
= NULL
;
221 struct wbcDomainSid sid
;
223 wbc_status
= wbcStringToSid(sid_str
, &sid
);
224 wbc_status
= wbcGetpwsid(&sid
, &pwd
);
225 if (!WBC_ERROR_IS_OK(wbc_status
)) {
226 d_fprintf(stderr
, "failed to call wbcGetpwsid: %s",
227 wbcErrorString(wbc_status
));
231 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
234 (unsigned int)pwd
->pw_uid
,
235 (unsigned int)pwd
->pw_gid
,
244 /* pull grent for a given group */
245 static bool wbinfo_get_groupinfo(const char *group
)
247 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
251 wbc_status
= wbcGetgrnam(group
, &grp
);
252 if (!WBC_ERROR_IS_OK(wbc_status
)) {
253 d_fprintf(stderr
, "failed to call wbcGetgrnam: %s",
254 wbcErrorString(wbc_status
));
258 d_printf("%s:%s:%u:",
261 (unsigned int)grp
->gr_gid
);
264 while (*mem
!= NULL
) {
265 d_printf("%s%s", *mem
, *(mem
+1) != NULL
? "," : "");
275 /* pull grent for a given gid */
276 static bool wbinfo_get_gidinfo(int gid
)
278 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
282 wbc_status
= wbcGetgrgid(gid
, &grp
);
283 if (!WBC_ERROR_IS_OK(wbc_status
)) {
284 d_fprintf(stderr
, "failed to call wbcGetgrgid: %s",
285 wbcErrorString(wbc_status
));
289 d_printf("%s:%s:%u:",
292 (unsigned int)grp
->gr_gid
);
295 while (*mem
!= NULL
) {
296 d_printf("%s%s", *mem
, *(mem
+1) != NULL
? "," : "");
306 /* List groups a user is a member of */
308 static bool wbinfo_get_usergroups(const char *user
)
310 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
313 gid_t
*groups
= NULL
;
317 wbc_status
= wbcGetGroups(user
, &num_groups
, &groups
);
318 if (!WBC_ERROR_IS_OK(wbc_status
)) {
319 d_fprintf(stderr
, "failed to call wbcGetGroups: %s",
320 wbcErrorString(wbc_status
));
324 for (i
= 0; i
< num_groups
; i
++) {
325 d_printf("%d\n", (int)groups
[i
]);
328 wbcFreeMemory(groups
);
334 /* List group SIDs a user SID is a member of */
335 static bool wbinfo_get_usersids(const char *user_sid_str
)
337 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
340 struct wbcDomainSid user_sid
, *sids
= NULL
;
344 wbc_status
= wbcStringToSid(user_sid_str
, &user_sid
);
345 if (!WBC_ERROR_IS_OK(wbc_status
)) {
346 d_fprintf(stderr
, "failed to call wbcStringToSid: %s",
347 wbcErrorString(wbc_status
));
351 wbc_status
= wbcLookupUserSids(&user_sid
, false, &num_sids
, &sids
);
352 if (!WBC_ERROR_IS_OK(wbc_status
)) {
353 d_fprintf(stderr
, "failed to call wbcLookupUserSids: %s",
354 wbcErrorString(wbc_status
));
358 for (i
= 0; i
< num_sids
; i
++) {
360 wbc_status
= wbcSidToString(&sids
[i
], &str
);
361 if (!WBC_ERROR_IS_OK(wbc_status
)) {
362 d_fprintf(stderr
, "failed to call wbcSidToString: %s",
363 wbcErrorString(wbc_status
));
367 d_printf("%s\n", str
);
376 static bool wbinfo_get_userdomgroups(const char *user_sid_str
)
378 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
381 struct wbcDomainSid user_sid
, *sids
= NULL
;
385 wbc_status
= wbcStringToSid(user_sid_str
, &user_sid
);
386 if (!WBC_ERROR_IS_OK(wbc_status
)) {
387 d_fprintf(stderr
, "failed to call wbcSidToString: %s",
388 wbcErrorString(wbc_status
));
392 wbc_status
= wbcLookupUserSids(&user_sid
, true, &num_sids
, &sids
);
393 if (!WBC_ERROR_IS_OK(wbc_status
)) {
394 d_fprintf(stderr
, "failed to call wbcLookupUserSids: %s",
395 wbcErrorString(wbc_status
));
399 for (i
= 0; i
< num_sids
; i
++) {
401 wbc_status
= wbcSidToString(&sids
[i
], &str
);
402 if (!WBC_ERROR_IS_OK(wbc_status
)) {
403 d_fprintf(stderr
, "failed to call wbcSidToString: %s",
404 wbcErrorString(wbc_status
));
408 d_printf("%s\n", str
);
417 static bool wbinfo_get_sidaliases(const char *domain
,
418 const char *user_sid_str
)
420 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
421 struct wbcDomainInfo
*dinfo
= NULL
;
423 struct wbcDomainSid user_sid
;
424 uint32_t *alias_rids
= NULL
;
425 uint32_t num_alias_rids
;
426 char *domain_sid_str
= NULL
;
429 if ((domain
== NULL
) || (strequal(domain
, ".")) ||
430 (domain
[0] == '\0')) {
431 domain
= get_winbind_domain();
436 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
437 if (!WBC_ERROR_IS_OK(wbc_status
)) {
438 d_fprintf(stderr
, "wbcDomainInfo(%s) failed: %s\n", domain
,
439 wbcErrorString(wbc_status
));
442 wbc_status
= wbcStringToSid(user_sid_str
, &user_sid
);
443 if (!WBC_ERROR_IS_OK(wbc_status
)) {
447 wbc_status
= wbcGetSidAliases(&dinfo
->sid
, &user_sid
, 1,
448 &alias_rids
, &num_alias_rids
);
449 if (!WBC_ERROR_IS_OK(wbc_status
)) {
453 wbc_status
= wbcSidToString(&dinfo
->sid
, &domain_sid_str
);
454 if (!WBC_ERROR_IS_OK(wbc_status
)) {
458 for (i
= 0; i
< num_alias_rids
; i
++) {
459 d_printf("%s-%d\n", domain_sid_str
, alias_rids
[i
]);
462 wbcFreeMemory(alias_rids
);
465 if (domain_sid_str
) {
466 wbcFreeMemory(domain_sid_str
);
469 wbcFreeMemory(dinfo
);
471 return (WBC_ERR_SUCCESS
== wbc_status
);
475 /* Convert NetBIOS name to IP */
477 static bool wbinfo_wins_byname(const char *name
)
479 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
482 wbc_status
= wbcResolveWinsByName(name
, &ip
);
483 if (!WBC_ERROR_IS_OK(wbc_status
)) {
484 d_fprintf(stderr
, "failed to call wbcResolveWinsByName: %s",
485 wbcErrorString(wbc_status
));
489 /* Display response */
491 d_printf("%s\n", ip
);
498 /* Convert IP to NetBIOS name */
500 static bool wbinfo_wins_byip(const char *ip
)
502 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
505 wbc_status
= wbcResolveWinsByIP(ip
, &name
);
506 if (!WBC_ERROR_IS_OK(wbc_status
)) {
507 d_fprintf(stderr
, "failed to call wbcResolveWinsByIP: %s",
508 wbcErrorString(wbc_status
));
512 /* Display response */
514 d_printf("%s\n", name
);
521 /* List all/trusted domains */
523 static bool wbinfo_list_domains(bool list_all_domains
, bool verbose
)
525 struct wbcDomainInfo
*domain_list
= NULL
;
527 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
528 bool print_all
= !list_all_domains
&& verbose
;
531 wbc_status
= wbcListTrusts(&domain_list
, &num_domains
);
532 if (!WBC_ERROR_IS_OK(wbc_status
)) {
533 d_fprintf(stderr
, "failed to call wbcListTrusts: %s",
534 wbcErrorString(wbc_status
));
539 d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n",
540 "Domain Name", "DNS Domain", "Trust Type",
541 "Transitive", "In", "Out");
544 for (i
=0; i
<num_domains
; i
++) {
546 d_printf("%-16s", domain_list
[i
].short_name
);
548 d_printf("%s", domain_list
[i
].short_name
);
553 d_printf("%-24s", domain_list
[i
].dns_name
);
555 switch(domain_list
[i
].trust_type
) {
556 case WBC_DOMINFO_TRUSTTYPE_NONE
:
559 case WBC_DOMINFO_TRUSTTYPE_FOREST
:
562 case WBC_DOMINFO_TRUSTTYPE_EXTERNAL
:
563 d_printf("External ");
565 case WBC_DOMINFO_TRUSTTYPE_IN_FOREST
:
566 d_printf("In-Forest ");
570 if (domain_list
[i
].trust_flags
& WBC_DOMINFO_TRUST_TRANSITIVE
) {
576 if (domain_list
[i
].trust_flags
& WBC_DOMINFO_TRUST_INCOMING
) {
582 if (domain_list
[i
].trust_flags
& WBC_DOMINFO_TRUST_OUTGOING
) {
594 /* List own domain */
596 static bool wbinfo_list_own_domain(void)
598 d_printf("%s\n", get_winbind_domain());
603 /* show sequence numbers */
604 static bool wbinfo_show_sequence(const char *domain
)
606 d_printf("This command has been deprecated. Please use the "
607 "--online-status option instead.\n");
611 /* show sequence numbers */
612 static bool wbinfo_show_onlinestatus(const char *domain
)
614 struct wbcDomainInfo
*domain_list
= NULL
;
616 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
619 wbc_status
= wbcListTrusts(&domain_list
, &num_domains
);
620 if (!WBC_ERROR_IS_OK(wbc_status
)) {
621 d_fprintf(stderr
, "failed to call wbcListTrusts: %s",
622 wbcErrorString(wbc_status
));
626 for (i
=0; i
<num_domains
; i
++) {
630 if (!strequal(domain_list
[i
].short_name
, domain
)) {
635 is_offline
= (domain_list
[i
].domain_flags
&
636 WBC_DOMINFO_DOMAIN_OFFLINE
);
638 d_printf("%s : %s\n",
639 domain_list
[i
].short_name
,
640 is_offline
? "offline" : "online" );
647 /* Show domain info */
649 static bool wbinfo_domain_info(const char *domain
)
651 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
652 struct wbcDomainInfo
*dinfo
= NULL
;
653 char *sid_str
= NULL
;
655 if ((domain
== NULL
) || (strequal(domain
, ".")) || (domain
[0] == '\0')){
656 domain
= get_winbind_domain();
661 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
662 if (!WBC_ERROR_IS_OK(wbc_status
)) {
663 d_fprintf(stderr
, "failed to call wbcDomainInfo: %s",
664 wbcErrorString(wbc_status
));
668 wbc_status
= wbcSidToString(&dinfo
->sid
, &sid_str
);
669 if (!WBC_ERROR_IS_OK(wbc_status
)) {
670 d_fprintf(stderr
, "failed to call wbcSidToString: %s",
671 wbcErrorString(wbc_status
));
672 wbcFreeMemory(dinfo
);
676 /* Display response */
678 d_printf("Name : %s\n", dinfo
->short_name
);
679 d_printf("Alt_Name : %s\n", dinfo
->dns_name
);
681 d_printf("SID : %s\n", sid_str
);
683 d_printf("Active Directory : %s\n",
684 (dinfo
->domain_flags
& WBC_DOMINFO_DOMAIN_AD
) ? "Yes" : "No");
685 d_printf("Native : %s\n",
686 (dinfo
->domain_flags
& WBC_DOMINFO_DOMAIN_NATIVE
) ?
689 d_printf("Primary : %s\n",
690 (dinfo
->domain_flags
& WBC_DOMINFO_DOMAIN_PRIMARY
) ?
693 wbcFreeMemory(sid_str
);
694 wbcFreeMemory(dinfo
);
699 /* Get a foreign DC's name */
700 static bool wbinfo_getdcname(const char *domain_name
)
702 struct winbindd_request request
;
703 struct winbindd_response response
;
705 ZERO_STRUCT(request
);
706 ZERO_STRUCT(response
);
708 fstrcpy(request
.domain_name
, domain_name
);
712 if (winbindd_request_response(WINBINDD_GETDCNAME
, &request
,
713 &response
) != NSS_STATUS_SUCCESS
) {
714 d_fprintf(stderr
, "Could not get dc name for %s\n",domain_name
);
718 /* Display response */
720 d_printf("%s\n", response
.data
.dc_name
);
726 static bool wbinfo_dsgetdcname(const char *domain_name
, uint32_t flags
)
728 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
729 struct wbcDomainControllerInfoEx
*dc_info
;
732 wbc_status
= wbcLookupDomainControllerEx(domain_name
, NULL
, NULL
,
733 flags
| DS_DIRECTORY_SERVICE_REQUIRED
,
735 if (!WBC_ERROR_IS_OK(wbc_status
)) {
736 printf("Could not find dc for %s\n", domain_name
);
740 wbcGuidToString(dc_info
->domain_guid
, &str
);
742 d_printf("%s\n", dc_info
->dc_unc
);
743 d_printf("%s\n", dc_info
->dc_address
);
744 d_printf("%d\n", dc_info
->dc_address_type
);
745 d_printf("%s\n", str
);
746 d_printf("%s\n", dc_info
->domain_name
);
747 d_printf("%s\n", dc_info
->forest_name
);
748 d_printf("0x%08x\n", dc_info
->dc_flags
);
749 d_printf("%s\n", dc_info
->dc_site_name
);
750 d_printf("%s\n", dc_info
->client_site_name
);
755 /* Check trust account password */
757 static bool wbinfo_check_secret(const char *domain
)
759 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
760 struct wbcAuthErrorInfo
*error
= NULL
;
761 const char *domain_name
;
764 domain_name
= domain
;
766 domain_name
= get_winbind_domain();
769 wbc_status
= wbcCheckTrustCredentials(domain_name
, &error
);
771 d_printf("checking the trust secret for domain %s via RPC calls %s\n",
773 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
775 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
776 d_fprintf(stderr
, "error code was %s (0x%x)\n",
777 error
->nt_string
, error
->nt_status
);
778 wbcFreeMemory(error
);
780 if (!WBC_ERROR_IS_OK(wbc_status
)) {
781 d_fprintf(stderr
, "failed to call wbcCheckTrustCredentials: %s",
782 wbcErrorString(wbc_status
));
789 /* Change trust account password */
791 static bool wbinfo_change_secret(const char *domain
)
793 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
794 struct wbcAuthErrorInfo
*error
= NULL
;
795 const char *domain_name
;
798 domain_name
= domain
;
800 domain_name
= get_winbind_domain();
803 wbc_status
= wbcChangeTrustCredentials(domain_name
, &error
);
805 d_printf("changing the trust secret for domain %s via RPC calls %s\n",
807 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
809 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
810 d_fprintf(stderr
, "error code was %s (0x%x)\n",
811 error
->nt_string
, error
->nt_status
);
812 wbcFreeMemory(error
);
814 if (!WBC_ERROR_IS_OK(wbc_status
)) {
815 d_fprintf(stderr
, "failed to call wbcChangeTrustCredentials: %s",
816 wbcErrorString(wbc_status
));
823 /* Check DC connection */
825 static bool wbinfo_ping_dc(void)
827 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
828 struct wbcAuthErrorInfo
*error
= NULL
;
830 wbc_status
= wbcPingDc(NULL
, &error
);
832 d_printf("checking the NETLOGON dc connection %s\n",
833 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
835 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
836 d_fprintf(stderr
, "error code was %s (0x%x)\n",
837 error
->nt_string
, error
->nt_status
);
838 wbcFreeMemory(error
);
840 if (!WBC_ERROR_IS_OK(wbc_status
)) {
841 d_fprintf(stderr
, "failed to call wbcPingDc: %s",
842 wbcErrorString(wbc_status
));
849 /* Convert uid to sid */
851 static bool wbinfo_uid_to_sid(uid_t uid
)
853 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
854 struct wbcDomainSid sid
;
855 char *sid_str
= NULL
;
859 wbc_status
= wbcUidToSid(uid
, &sid
);
860 if (!WBC_ERROR_IS_OK(wbc_status
)) {
861 d_fprintf(stderr
, "failed to call wbcUidToSid: %s",
862 wbcErrorString(wbc_status
));
866 wbc_status
= wbcSidToString(&sid
, &sid_str
);
867 if (!WBC_ERROR_IS_OK(wbc_status
)) {
868 d_fprintf(stderr
, "failed to call wbcSidToString: %s",
869 wbcErrorString(wbc_status
));
873 /* Display response */
875 d_printf("%s\n", sid_str
);
877 wbcFreeMemory(sid_str
);
882 /* Convert gid to sid */
884 static bool wbinfo_gid_to_sid(gid_t gid
)
886 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
887 struct wbcDomainSid sid
;
888 char *sid_str
= NULL
;
892 wbc_status
= wbcGidToSid(gid
, &sid
);
893 if (!WBC_ERROR_IS_OK(wbc_status
)) {
894 d_fprintf(stderr
, "failed to call wbcGidToSid: %s",
895 wbcErrorString(wbc_status
));
899 wbc_status
= wbcSidToString(&sid
, &sid_str
);
900 if (!WBC_ERROR_IS_OK(wbc_status
)) {
901 d_fprintf(stderr
, "failed to call wbcSidToString: %s",
902 wbcErrorString(wbc_status
));
906 /* Display response */
908 d_printf("%s\n", sid_str
);
910 wbcFreeMemory(sid_str
);
915 /* Convert sid to uid */
917 static bool wbinfo_sid_to_uid(const char *sid_str
)
919 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
920 struct wbcDomainSid sid
;
925 wbc_status
= wbcStringToSid(sid_str
, &sid
);
926 if (!WBC_ERROR_IS_OK(wbc_status
)) {
927 d_fprintf(stderr
, "failed to call wbcStringToSid: %s",
928 wbcErrorString(wbc_status
));
932 wbc_status
= wbcSidToUid(&sid
, &uid
);
933 if (!WBC_ERROR_IS_OK(wbc_status
)) {
934 d_fprintf(stderr
, "failed to call wbcSidToUid: %s",
935 wbcErrorString(wbc_status
));
939 /* Display response */
941 d_printf("%d\n", (int)uid
);
946 static bool wbinfo_sid_to_gid(const char *sid_str
)
948 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
949 struct wbcDomainSid sid
;
954 wbc_status
= wbcStringToSid(sid_str
, &sid
);
955 if (!WBC_ERROR_IS_OK(wbc_status
)) {
956 d_fprintf(stderr
, "failed to call wbcStringToSid: %s",
957 wbcErrorString(wbc_status
));
961 wbc_status
= wbcSidToGid(&sid
, &gid
);
962 if (!WBC_ERROR_IS_OK(wbc_status
)) {
963 d_fprintf(stderr
, "failed to call wbcSidToGid: %s",
964 wbcErrorString(wbc_status
));
968 /* Display response */
970 d_printf("%d\n", (int)gid
);
975 static bool wbinfo_allocate_uid(void)
977 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
982 wbc_status
= wbcAllocateUid(&uid
);
983 if (!WBC_ERROR_IS_OK(wbc_status
)) {
984 d_fprintf(stderr
, "failed to call wbcAllocateUid: %s",
985 wbcErrorString(wbc_status
));
989 /* Display response */
991 d_printf("New uid: %u\n", (unsigned int)uid
);
996 static bool wbinfo_allocate_gid(void)
998 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1003 wbc_status
= wbcAllocateGid(&gid
);
1004 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1005 d_fprintf(stderr
, "failed to call wbcAllocateGid: %s",
1006 wbcErrorString(wbc_status
));
1010 /* Display response */
1012 d_printf("New gid: %u\n", (unsigned int)gid
);
1017 static bool wbinfo_set_uid_mapping(uid_t uid
, const char *sid_str
)
1019 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1020 struct wbcDomainSid sid
;
1024 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1025 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1026 d_fprintf(stderr
, "failed to call wbcStringToSid: %s",
1027 wbcErrorString(wbc_status
));
1031 wbc_status
= wbcSetUidMapping(uid
, &sid
);
1032 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1033 d_fprintf(stderr
, "failed to call wbcSetUidMapping: %s",
1034 wbcErrorString(wbc_status
));
1038 /* Display response */
1040 d_printf("uid %u now mapped to sid %s\n",
1041 (unsigned int)uid
, sid_str
);
1046 static bool wbinfo_set_gid_mapping(gid_t gid
, const char *sid_str
)
1048 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1049 struct wbcDomainSid sid
;
1053 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1054 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1055 d_fprintf(stderr
, "failed to call wbcStringToSid: %s",
1056 wbcErrorString(wbc_status
));
1060 wbc_status
= wbcSetGidMapping(gid
, &sid
);
1061 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1062 d_fprintf(stderr
, "failed to call wbcSetGidMapping: %s",
1063 wbcErrorString(wbc_status
));
1067 /* Display response */
1069 d_printf("gid %u now mapped to sid %s\n",
1070 (unsigned int)gid
, sid_str
);
1075 static bool wbinfo_remove_uid_mapping(uid_t uid
, const char *sid_str
)
1077 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1078 struct wbcDomainSid sid
;
1082 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1083 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1084 d_fprintf(stderr
, "failed to call wbcStringToSid: %s",
1085 wbcErrorString(wbc_status
));
1089 wbc_status
= wbcRemoveUidMapping(uid
, &sid
);
1090 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1091 d_fprintf(stderr
, "failed to call wbcRemoveUidMapping: %s",
1092 wbcErrorString(wbc_status
));
1096 /* Display response */
1098 d_printf("Removed uid %u to sid %s mapping\n",
1099 (unsigned int)uid
, sid_str
);
1104 static bool wbinfo_remove_gid_mapping(gid_t gid
, const char *sid_str
)
1106 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1107 struct wbcDomainSid sid
;
1111 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1112 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1113 d_fprintf(stderr
, "failed to call wbcStringToSid: %s",
1114 wbcErrorString(wbc_status
));
1118 wbc_status
= wbcRemoveGidMapping(gid
, &sid
);
1119 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1120 d_fprintf(stderr
, "failed to call wbcRemoveGidMapping: %s",
1121 wbcErrorString(wbc_status
));
1125 /* Display response */
1127 d_printf("Removed gid %u to sid %s mapping\n",
1128 (unsigned int)gid
, sid_str
);
1133 /* Convert sid to string */
1135 static bool wbinfo_lookupsid(const char *sid_str
)
1137 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1138 struct wbcDomainSid sid
;
1141 enum wbcSidType type
;
1143 /* Send off request */
1145 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1146 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1147 d_fprintf(stderr
, "failed to call wbcStringToSid: %s",
1148 wbcErrorString(wbc_status
));
1152 wbc_status
= wbcLookupSid(&sid
, &domain
, &name
, &type
);
1153 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1154 d_fprintf(stderr
, "failed to call wbcLookupSid: %s",
1155 wbcErrorString(wbc_status
));
1159 /* Display response */
1161 d_printf("%s%c%s %d\n",
1162 domain
, winbind_separator(), name
, type
);
1167 /* Convert sid to fullname */
1169 static bool wbinfo_lookupsid_fullname(const char *sid_str
)
1171 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1172 struct wbcDomainSid sid
;
1175 enum wbcSidType type
;
1177 /* Send off request */
1179 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1180 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1181 d_fprintf(stderr
, "failed to call wbcStringToSid: %s",
1182 wbcErrorString(wbc_status
));
1186 wbc_status
= wbcGetDisplayName(&sid
, &domain
, &name
, &type
);
1187 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1188 d_fprintf(stderr
, "failed to call wbcGetDisplayName: %s",
1189 wbcErrorString(wbc_status
));
1193 /* Display response */
1195 d_printf("%s%c%s %d\n",
1196 domain
, winbind_separator(), name
, type
);
1201 /* Lookup a list of RIDs */
1203 static bool wbinfo_lookuprids(const char *domain
, const char *arg
)
1205 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1206 struct wbcDomainInfo
*dinfo
= NULL
;
1207 char *domain_name
= NULL
;
1208 const char **names
= NULL
;
1209 enum wbcSidType
*types
= NULL
;
1212 uint32_t *rids
= NULL
;
1215 TALLOC_CTX
*mem_ctx
= NULL
;
1218 if ((domain
== NULL
) || (strequal(domain
, ".")) || (domain
[0] == '\0')){
1219 domain
= get_winbind_domain();
1224 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
1225 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1226 d_printf("wbcDomainInfo(%s) failed: %s\n", domain
,
1227 wbcErrorString(wbc_status
));
1231 mem_ctx
= talloc_new(NULL
);
1232 if (mem_ctx
== NULL
) {
1233 d_printf("talloc_new failed\n");
1241 while (next_token_talloc(mem_ctx
, &p
, &ridstr
, " ,\n")) {
1242 uint32_t rid
= strtoul(ridstr
, NULL
, 10);
1243 rids
= talloc_realloc(mem_ctx
, rids
, uint32_t, num_rids
+ 1);
1245 d_printf("talloc_realloc failed\n");
1247 rids
[num_rids
] = rid
;
1252 d_printf("no rids\n");
1256 wbc_status
= wbcLookupRids(&dinfo
->sid
, num_rids
, rids
,
1257 (const char **)&domain_name
, &names
, &types
);
1258 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1259 d_printf("winbind_lookup_rids failed: %s\n",
1260 wbcErrorString(wbc_status
));
1264 d_printf("Domain: %s\n", domain_name
);
1266 for (i
=0; i
<num_rids
; i
++) {
1267 d_printf("%8d: %s (%s)\n", rids
[i
], names
[i
],
1268 wbcSidTypeString(types
[i
]));
1274 wbcFreeMemory(dinfo
);
1277 wbcFreeMemory(domain_name
);
1280 wbcFreeMemory(names
);
1283 wbcFreeMemory(types
);
1285 TALLOC_FREE(mem_ctx
);
1289 /* Convert string to sid */
1291 static bool wbinfo_lookupname(const char *full_name
)
1293 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1294 struct wbcDomainSid sid
;
1296 enum wbcSidType type
;
1297 fstring domain_name
;
1298 fstring account_name
;
1300 /* Send off request */
1302 parse_wbinfo_domain_user(full_name
, domain_name
,
1305 wbc_status
= wbcLookupName(domain_name
, account_name
,
1307 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1308 d_fprintf(stderr
, "failed to call wbcLookupName: %s",
1309 wbcErrorString(wbc_status
));
1313 wbc_status
= wbcSidToString(&sid
, &sid_str
);
1314 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1315 d_fprintf(stderr
, "failed to call wbcSidToString: %s",
1316 wbcErrorString(wbc_status
));
1320 /* Display response */
1322 d_printf("%s %s (%d)\n", sid_str
, wbcSidTypeString(type
), type
);
1324 wbcFreeMemory(sid_str
);
1329 static char *wbinfo_prompt_pass(TALLOC_CTX
*mem_ctx
,
1331 const char *username
)
1334 const char *ret
= NULL
;
1336 prompt
= talloc_asprintf(mem_ctx
, "Enter %s's ", username
);
1341 prompt
= talloc_asprintf_append(prompt
, "%s ", prefix
);
1346 prompt
= talloc_asprintf_append(prompt
, "password: ");
1351 ret
= getpass(prompt
);
1352 TALLOC_FREE(prompt
);
1354 return talloc_strdup(mem_ctx
, ret
);
1357 /* Authenticate a user with a plaintext password */
1359 static bool wbinfo_auth_krb5(char *username
, const char *cctype
, uint32_t flags
)
1361 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1364 char *password
= NULL
;
1366 char *local_cctype
= NULL
;
1368 struct wbcLogonUserParams params
;
1369 struct wbcLogonUserInfo
*info
;
1370 struct wbcAuthErrorInfo
*error
;
1371 struct wbcUserPasswordPolicyInfo
*policy
;
1372 TALLOC_CTX
*frame
= talloc_tos();
1374 if ((s
= talloc_strdup(frame
, username
)) == NULL
) {
1378 if ((p
= strchr(s
, '%')) != NULL
) {
1381 password
= talloc_strdup(frame
, p
);
1383 password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1386 local_cctype
= talloc_strdup(frame
, cctype
);
1392 params
.username
= name
;
1393 params
.password
= password
;
1394 params
.num_blobs
= 0;
1395 params
.blobs
= NULL
;
1397 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
,
1403 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1404 d_fprintf(stderr
, "failed to call wbcAddNamedBlob: %s",
1405 wbcErrorString(wbc_status
));
1409 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
,
1415 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1416 d_fprintf(stderr
, "failed to call wbcAddNamedBlob: %s",
1417 wbcErrorString(wbc_status
));
1421 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
,
1425 (uint8_t *)local_cctype
,
1427 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1428 d_fprintf(stderr
, "failed to call wbcAddNamedBlob: %s",
1429 wbcErrorString(wbc_status
));
1433 wbc_status
= wbcLogonUser(¶ms
, &info
, &error
, &policy
);
1435 d_printf("plaintext kerberos password authentication for [%s] %s "
1436 "(requesting cctype: %s)\n",
1437 username
, WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed",
1442 "error code was %s (0x%x)\nerror messsage was: %s\n",
1445 error
->display_string
);
1448 if (WBC_ERROR_IS_OK(wbc_status
)) {
1449 if (flags
& WBFLAG_PAM_INFO3_TEXT
) {
1450 if (info
&& info
->info
&& info
->info
->user_flags
&
1451 NETLOGON_CACHED_ACCOUNT
) {
1452 d_printf("user_flgs: "
1453 "NETLOGON_CACHED_ACCOUNT\n");
1459 for (i
=0; i
< info
->num_blobs
; i
++) {
1460 if (strequal(info
->blobs
[i
].name
,
1462 d_printf("credentials were put "
1465 info
->blobs
[i
].blob
.data
);
1470 d_printf("no credentials cached\n");
1475 wbcFreeMemory(params
.blobs
);
1477 return WBC_ERROR_IS_OK(wbc_status
);
1480 /* Authenticate a user with a plaintext password */
1482 static bool wbinfo_auth(char *username
)
1484 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1487 char *password
= NULL
;
1489 TALLOC_CTX
*frame
= talloc_tos();
1491 if ((s
= talloc_strdup(frame
, username
)) == NULL
) {
1495 if ((p
= strchr(s
, '%')) != NULL
) {
1498 password
= talloc_strdup(frame
, p
);
1500 password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1505 wbc_status
= wbcAuthenticateUser(name
, password
);
1507 d_printf("plaintext password authentication %s\n",
1508 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1511 if (response
.data
.auth
.nt_status
)
1513 "error code was %s (0x%x)\nerror messsage was: %s\n",
1514 response
.data
.auth
.nt_status_string
,
1515 response
.data
.auth
.nt_status
,
1516 response
.data
.auth
.error_string
);
1519 return WBC_ERROR_IS_OK(wbc_status
);
1522 /* Authenticate a user with a challenge/response */
1524 static bool wbinfo_auth_crap(char *username
, bool use_ntlmv2
, bool use_lanman
)
1526 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1527 struct wbcAuthUserParams params
;
1528 struct wbcAuthUserInfo
*info
= NULL
;
1529 struct wbcAuthErrorInfo
*err
= NULL
;
1530 DATA_BLOB lm
= data_blob_null
;
1531 DATA_BLOB nt
= data_blob_null
;
1533 fstring name_domain
;
1536 TALLOC_CTX
*frame
= talloc_tos();
1538 p
= strchr(username
, '%');
1542 pass
= talloc_strdup(frame
, p
+ 1);
1544 pass
= wbinfo_prompt_pass(frame
, NULL
, username
);
1547 parse_wbinfo_domain_user(username
, name_domain
, name_user
);
1549 params
.account_name
= name_user
;
1550 params
.domain_name
= name_domain
;
1551 params
.workstation_name
= NULL
;
1554 params
.parameter_control
= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
|
1555 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
;
1557 params
.level
= WBC_AUTH_USER_LEVEL_RESPONSE
;
1559 generate_random_buffer(params
.password
.response
.challenge
, 8);
1562 DATA_BLOB server_chal
;
1563 DATA_BLOB names_blob
;
1565 server_chal
= data_blob(params
.password
.response
.challenge
, 8);
1567 /* Pretend this is a login to 'us', for blob purposes */
1568 names_blob
= NTLMv2_generate_names_blob(NULL
,
1569 get_winbind_netbios_name(),
1570 get_winbind_domain());
1572 if (!SMBNTLMv2encrypt(NULL
, name_user
, name_domain
, pass
,
1575 &lm
, &nt
, NULL
, NULL
)) {
1576 data_blob_free(&names_blob
);
1577 data_blob_free(&server_chal
);
1581 data_blob_free(&names_blob
);
1582 data_blob_free(&server_chal
);
1587 lm
= data_blob(NULL
, 24);
1588 ok
= SMBencrypt(pass
,
1589 params
.password
.response
.challenge
,
1592 data_blob_free(&lm
);
1595 nt
= data_blob(NULL
, 24);
1596 SMBNTencrypt(pass
, params
.password
.response
.challenge
,
1600 params
.password
.response
.nt_length
= nt
.length
;
1601 params
.password
.response
.nt_data
= nt
.data
;
1602 params
.password
.response
.lm_length
= lm
.length
;
1603 params
.password
.response
.lm_data
= lm
.data
;
1605 wbc_status
= wbcAuthenticateUserEx(¶ms
, &info
, &err
);
1607 /* Display response */
1609 d_printf("challenge/response password authentication %s\n",
1610 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1612 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
1614 "error code was %s (0x%x)\nerror messsage was: %s\n",
1617 err
->display_string
);
1619 } else if (WBC_ERROR_IS_OK(wbc_status
)) {
1620 wbcFreeMemory(info
);
1623 data_blob_free(&nt
);
1624 data_blob_free(&lm
);
1626 return WBC_ERROR_IS_OK(wbc_status
);
1629 /* Authenticate a user with a plaintext password */
1631 static bool wbinfo_pam_logon(char *username
)
1633 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1634 struct wbcLogonUserParams params
;
1635 struct wbcAuthErrorInfo
*error
;
1638 TALLOC_CTX
*frame
= talloc_tos();
1642 ZERO_STRUCT(params
);
1644 if ((s
= talloc_strdup(frame
, username
)) == NULL
) {
1648 if ((p
= strchr(s
, '%')) != NULL
) {
1651 params
.password
= talloc_strdup(frame
, p
);
1653 params
.password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1655 params
.username
= s
;
1657 flags
= WBFLAG_PAM_CACHED_LOGIN
;
1659 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
, ¶ms
.blobs
,
1661 (uint8_t *)&flags
, sizeof(flags
));
1662 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1663 d_printf("wbcAddNamedBlob failed: %s\n",
1664 wbcErrorString(wbc_status
));
1670 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
, ¶ms
.blobs
,
1672 (uint8_t *)&uid
, sizeof(uid
));
1673 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1674 d_printf("wbcAddNamedBlob failed: %s\n",
1675 wbcErrorString(wbc_status
));
1679 wbc_status
= wbcLogonUser(¶ms
, NULL
, &error
, NULL
);
1681 wbcFreeMemory(params
.blobs
);
1683 d_printf("plaintext password authentication %s\n",
1684 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1686 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1688 "error code was %s (0x%x)\nerror messsage was: %s\n",
1690 (int)error
->nt_status
,
1691 error
->display_string
);
1692 wbcFreeMemory(error
);
1698 /* Save creds with winbind */
1700 static bool wbinfo_ccache_save(char *username
)
1702 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1705 char *password
= NULL
;
1707 TALLOC_CTX
*frame
= talloc_stackframe();
1709 s
= talloc_strdup(frame
, username
);
1718 password
= talloc_strdup(frame
, p
);
1720 password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1725 wbc_status
= wbcCredentialSave(name
, password
);
1727 d_printf("saving creds %s\n",
1728 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1732 return WBC_ERROR_IS_OK(wbc_status
);
1735 #ifdef WITH_FAKE_KASERVER
1736 /* Authenticate a user with a plaintext password and set a token */
1738 static bool wbinfo_klog(char *username
)
1740 struct winbindd_request request
;
1741 struct winbindd_response response
;
1745 /* Send off request */
1747 ZERO_STRUCT(request
);
1748 ZERO_STRUCT(response
);
1750 p
= strchr(username
, '%');
1754 fstrcpy(request
.data
.auth
.user
, username
);
1755 fstrcpy(request
.data
.auth
.pass
, p
+ 1);
1758 fstrcpy(request
.data
.auth
.user
, username
);
1759 fstrcpy(request
.data
.auth
.pass
, getpass("Password: "));
1762 request
.flags
|= WBFLAG_PAM_AFS_TOKEN
;
1764 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
,
1767 /* Display response */
1769 d_printf("plaintext password authentication %s\n",
1770 (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed");
1772 if (response
.data
.auth
.nt_status
)
1774 "error code was %s (0x%x)\nerror messsage was: %s\n",
1775 response
.data
.auth
.nt_status_string
,
1776 response
.data
.auth
.nt_status
,
1777 response
.data
.auth
.error_string
);
1779 if (result
!= NSS_STATUS_SUCCESS
)
1782 if (response
.extra_data
.data
== NULL
) {
1783 d_fprintf(stderr
, "Did not get token data\n");
1787 if (!afs_settoken_str((char *)response
.extra_data
.data
)) {
1788 d_fprintf(stderr
, "Could not set token\n");
1792 d_printf("Successfully created AFS token\n");
1796 static bool wbinfo_klog(char *username
)
1798 d_fprintf(stderr
, "No AFS support compiled in.\n");
1803 /* Print domain users */
1805 static bool print_domain_users(const char *domain
)
1807 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1809 uint32_t num_users
= 0;
1810 const char **users
= NULL
;
1812 /* Send request to winbind daemon */
1814 /* '.' is the special sign for our own domain */
1815 if (domain
&& strcmp(domain
, ".") == 0) {
1816 domain
= get_winbind_domain();
1819 wbc_status
= wbcListUsers(domain
, &num_users
, &users
);
1820 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1824 for (i
=0; i
< num_users
; i
++) {
1825 d_printf("%s\n", users
[i
]);
1828 wbcFreeMemory(users
);
1833 /* Print domain groups */
1835 static bool print_domain_groups(const char *domain
)
1837 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1839 uint32_t num_groups
= 0;
1840 const char **groups
= NULL
;
1842 /* Send request to winbind daemon */
1844 /* '.' is the special sign for our own domain */
1845 if (domain
&& strcmp(domain
, ".") == 0) {
1846 domain
= get_winbind_domain();
1849 wbc_status
= wbcListGroups(domain
, &num_groups
, &groups
);
1850 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1851 d_fprintf(stderr
, "failed to call wbcListGroups: %s",
1852 wbcErrorString(wbc_status
));
1856 for (i
=0; i
< num_groups
; i
++) {
1857 d_printf("%s\n", groups
[i
]);
1860 wbcFreeMemory(groups
);
1865 /* Set the authorised user for winbindd access in secrets.tdb */
1867 static bool wbinfo_set_auth_user(char *username
)
1869 d_fprintf(stderr
, "This functionality was moved to the 'net' utility.\n"
1870 "See 'net help setauthuser' for details.\n");
1874 static void wbinfo_get_auth_user(void)
1876 d_fprintf(stderr
, "This functionality was moved to the 'net' utility.\n"
1877 "See 'net help getauthuser' for details.\n");
1880 static bool wbinfo_ping(void)
1884 wbc_status
= wbcPing();
1886 /* Display response */
1888 d_printf("Ping to winbindd %s\n",
1889 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1891 return WBC_ERROR_IS_OK(wbc_status
);
1894 static bool wbinfo_change_user_password(const char *username
)
1897 char *old_password
= NULL
;
1898 char *new_password
= NULL
;
1899 TALLOC_CTX
*frame
= talloc_tos();
1901 old_password
= wbinfo_prompt_pass(frame
, "old", username
);
1902 new_password
= wbinfo_prompt_pass(frame
, "new", username
);
1904 wbc_status
= wbcChangeUserPassword(username
, old_password
,new_password
);
1906 /* Display response */
1908 d_printf("Password change for user %s %s\n", username
,
1909 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1911 return WBC_ERROR_IS_OK(wbc_status
);
1917 OPT_SET_AUTH_USER
= 1000,
1928 OPT_SET_UID_MAPPING
,
1929 OPT_SET_GID_MAPPING
,
1930 OPT_REMOVE_UID_MAPPING
,
1931 OPT_REMOVE_GID_MAPPING
,
1933 OPT_LIST_ALL_DOMAINS
,
1934 OPT_LIST_OWN_DOMAIN
,
1941 OPT_CHANGE_USER_PASSWORD
,
1944 OPT_SID_TO_FULLNAME
,
1953 int main(int argc
, char **argv
, char **envp
)
1956 TALLOC_CTX
*frame
= talloc_stackframe();
1958 static char *string_arg
;
1959 char *string_subarg
= NULL
;
1960 static char *opt_domain_name
;
1962 int int_subarg
= -1;
1964 bool verbose
= false;
1965 bool use_ntlmv2
= false;
1966 bool use_lanman
= false;
1967 char *logoff_user
= getenv("USER");
1968 int logoff_uid
= geteuid();
1970 struct poptOption long_options
[] = {
1973 /* longName, shortName, argInfo, argPtr, value, descrip,
1976 { "domain-users", 'u', POPT_ARG_NONE
, 0, 'u', "Lists all domain users", "domain"},
1977 { "domain-groups", 'g', POPT_ARG_NONE
, 0, 'g', "Lists all domain groups", "domain" },
1978 { "WINS-by-name", 'N', POPT_ARG_STRING
, &string_arg
, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1979 { "WINS-by-ip", 'I', POPT_ARG_STRING
, &string_arg
, 'I', "Converts IP address to NetBIOS name", "IP" },
1980 { "name-to-sid", 'n', POPT_ARG_STRING
, &string_arg
, 'n', "Converts name to sid", "NAME" },
1981 { "sid-to-name", 's', POPT_ARG_STRING
, &string_arg
, 's', "Converts sid to name", "SID" },
1982 { "sid-to-fullname", 0, POPT_ARG_STRING
, &string_arg
,
1983 OPT_SID_TO_FULLNAME
, "Converts sid to fullname", "SID" },
1984 { "lookup-rids", 'R', POPT_ARG_STRING
, &string_arg
, 'R', "Converts RIDs to names", "RIDs" },
1985 { "uid-to-sid", 'U', POPT_ARG_INT
, &int_arg
, 'U', "Converts uid to sid" , "UID" },
1986 { "gid-to-sid", 'G', POPT_ARG_INT
, &int_arg
, 'G', "Converts gid to sid", "GID" },
1987 { "sid-to-uid", 'S', POPT_ARG_STRING
, &string_arg
, 'S', "Converts sid to uid", "SID" },
1988 { "sid-to-gid", 'Y', POPT_ARG_STRING
, &string_arg
, 'Y', "Converts sid to gid", "SID" },
1989 { "allocate-uid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_UID
,
1990 "Get a new UID out of idmap" },
1991 { "allocate-gid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_GID
,
1992 "Get a new GID out of idmap" },
1993 { "set-uid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_UID_MAPPING
, "Create or modify uid to sid mapping in idmap", "UID,SID" },
1994 { "set-gid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_GID_MAPPING
, "Create or modify gid to sid mapping in idmap", "GID,SID" },
1995 { "remove-uid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_REMOVE_UID_MAPPING
, "Remove uid to sid mapping in idmap", "UID,SID" },
1996 { "remove-gid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_REMOVE_GID_MAPPING
, "Remove gid to sid mapping in idmap", "GID,SID" },
1997 { "check-secret", 't', POPT_ARG_NONE
, 0, 't', "Check shared secret" },
1998 { "change-secret", 'c', POPT_ARG_NONE
, 0, 'c', "Change shared secret" },
1999 { "ping-dc", 0, POPT_ARG_NONE
, 0, OPT_PING_DC
,
2000 "Check the NETLOGON connection" },
2001 { "trusted-domains", 'm', POPT_ARG_NONE
, 0, 'm', "List trusted domains" },
2002 { "all-domains", 0, POPT_ARG_NONE
, 0, OPT_LIST_ALL_DOMAINS
, "List all domains (trusted and own domain)" },
2003 { "own-domain", 0, POPT_ARG_NONE
, 0, OPT_LIST_OWN_DOMAIN
, "List own domain" },
2004 { "sequence", 0, POPT_ARG_NONE
, 0, OPT_SEQUENCE
, "Show sequence numbers of all domains" },
2005 { "online-status", 0, POPT_ARG_NONE
, 0, OPT_ONLINESTATUS
, "Show whether domains are marked as online or offline"},
2006 { "domain-info", 'D', POPT_ARG_STRING
, &string_arg
, 'D', "Show most of the info we have about the domain" },
2007 { "user-info", 'i', POPT_ARG_STRING
, &string_arg
, 'i', "Get user info", "USER" },
2008 { "uid-info", 0, POPT_ARG_INT
, &int_arg
, OPT_UID_INFO
, "Get user info from uid", "UID" },
2009 { "group-info", 0, POPT_ARG_STRING
, &string_arg
, OPT_GROUP_INFO
, "Get group info", "GROUP" },
2010 { "user-sidinfo", 0, POPT_ARG_STRING
, &string_arg
, OPT_USER_SIDINFO
, "Get user info from sid", "SID" },
2011 { "gid-info", 0, POPT_ARG_INT
, &int_arg
, OPT_GID_INFO
, "Get group info from gid", "GID" },
2012 { "user-groups", 'r', POPT_ARG_STRING
, &string_arg
, 'r', "Get user groups", "USER" },
2013 { "user-domgroups", 0, POPT_ARG_STRING
, &string_arg
,
2014 OPT_USERDOMGROUPS
, "Get user domain groups", "SID" },
2015 { "sid-aliases", 0, POPT_ARG_STRING
, &string_arg
, OPT_SIDALIASES
, "Get sid aliases", "SID" },
2016 { "user-sids", 0, POPT_ARG_STRING
, &string_arg
, OPT_USERSIDS
, "Get user group sids for user SID", "SID" },
2017 { "authenticate", 'a', POPT_ARG_STRING
, &string_arg
, 'a', "authenticate user", "user%password" },
2018 { "pam-logon", 0, POPT_ARG_STRING
, string_arg
, OPT_PAM_LOGON
,
2019 "do a pam logon equivalent", "user%password" },
2020 { "logoff", 0, POPT_ARG_NONE
, NULL
, OPT_LOGOFF
,
2021 "log off user", "uid" },
2022 { "logoff-user", 0, POPT_ARG_STRING
, &logoff_user
,
2023 OPT_LOGOFF_USER
, "username to log off" },
2024 { "logoff-uid", 0, POPT_ARG_INT
, &logoff_uid
,
2025 OPT_LOGOFF_UID
, "uid to log off" },
2026 { "set-auth-user", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_AUTH_USER
, "Store user and password used by winbindd (root only)", "user%password" },
2027 { "ccache-save", 0, POPT_ARG_STRING
, &string_arg
,
2028 OPT_CCACHE_SAVE
, "Store user and password for ccache "
2029 "operation", "user%password" },
2030 { "getdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_GETDCNAME
,
2031 "Get a DC name for a foreign domain", "domainname" },
2032 { "dsgetdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_DSGETDCNAME
, "Find a DC for a domain", "domainname" },
2033 { "get-auth-user", 0, POPT_ARG_NONE
, NULL
, OPT_GET_AUTH_USER
, "Retrieve user and password used by winbindd (root only)", NULL
},
2034 { "ping", 'p', POPT_ARG_NONE
, 0, 'p', "Ping winbindd to see if it is alive" },
2035 { "domain", 0, POPT_ARG_STRING
, &opt_domain_name
, OPT_DOMAIN_NAME
, "Define to the domain to restrict operation", "domain" },
2036 #ifdef WITH_FAKE_KASERVER
2037 { "klog", 'k', POPT_ARG_STRING
, &string_arg
, 'k', "set an AFS token from winbind", "user%password" },
2040 { "krb5auth", 'K', POPT_ARG_STRING
, &string_arg
, 'K', "authenticate user using Kerberos", "user%password" },
2041 /* destroys wbinfo --help output */
2042 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
2044 { "separator", 0, POPT_ARG_NONE
, 0, OPT_SEPARATOR
, "Get the active winbind separator", NULL
},
2045 { "verbose", 0, POPT_ARG_NONE
, 0, OPT_VERBOSE
, "Print additional information per command", NULL
},
2046 { "change-user-password", 0, POPT_ARG_STRING
, &string_arg
, OPT_CHANGE_USER_PASSWORD
, "Change the password for a user", NULL
},
2047 { "ntlmv2", 0, POPT_ARG_NONE
, 0, OPT_NTLMV2
, "Use NTLMv2 cryptography for user authentication", NULL
},
2048 { "lanman", 0, POPT_ARG_NONE
, 0, OPT_LANMAN
, "Use lanman cryptography for user authentication", NULL
},
2053 /* Samba client initialisation */
2059 pc
= poptGetContext("wbinfo", argc
, (const char **)argv
,
2062 /* Parse command line options */
2065 poptPrintHelp(pc
, stderr
, 0);
2069 while((opt
= poptGetNextOpt(pc
)) != -1) {
2070 /* get the generic configuration parameters like --domain */
2084 poptFreeContext(pc
);
2086 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
2087 POPT_CONTEXT_KEEP_FIRST
);
2089 while((opt
= poptGetNextOpt(pc
)) != -1) {
2092 if (!print_domain_users(opt_domain_name
)) {
2094 "Error looking up domain users\n");
2099 if (!print_domain_groups(opt_domain_name
)) {
2101 "Error looking up domain groups\n");
2106 if (!wbinfo_lookupsid(string_arg
)) {
2108 "Could not lookup sid %s\n",
2113 case OPT_SID_TO_FULLNAME
:
2114 if (!wbinfo_lookupsid_fullname(string_arg
)) {
2115 d_fprintf(stderr
, "Could not lookup sid %s\n",
2121 if (!wbinfo_lookuprids(opt_domain_name
, string_arg
)) {
2122 d_fprintf(stderr
, "Could not lookup RIDs %s\n",
2128 if (!wbinfo_lookupname(string_arg
)) {
2129 d_fprintf(stderr
, "Could not lookup name %s\n",
2135 if (!wbinfo_wins_byname(string_arg
)) {
2137 "Could not lookup WINS by name %s\n",
2143 if (!wbinfo_wins_byip(string_arg
)) {
2145 "Could not lookup WINS by IP %s\n",
2151 if (!wbinfo_uid_to_sid(int_arg
)) {
2153 "Could not convert uid %d to sid\n",
2159 if (!wbinfo_gid_to_sid(int_arg
)) {
2161 "Could not convert gid %d to sid\n",
2167 if (!wbinfo_sid_to_uid(string_arg
)) {
2169 "Could not convert sid %s to uid\n",
2175 if (!wbinfo_sid_to_gid(string_arg
)) {
2177 "Could not convert sid %s to gid\n",
2182 case OPT_ALLOCATE_UID
:
2183 if (!wbinfo_allocate_uid()) {
2184 d_fprintf(stderr
, "Could not allocate a uid\n");
2188 case OPT_ALLOCATE_GID
:
2189 if (!wbinfo_allocate_gid()) {
2190 d_fprintf(stderr
, "Could not allocate a gid\n");
2194 case OPT_SET_UID_MAPPING
:
2195 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2197 !wbinfo_set_uid_mapping(int_subarg
, string_subarg
))
2199 d_fprintf(stderr
, "Could not create or modify "
2200 "uid to sid mapping\n");
2204 case OPT_SET_GID_MAPPING
:
2205 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2207 !wbinfo_set_gid_mapping(int_subarg
, string_subarg
))
2209 d_fprintf(stderr
, "Could not create or modify "
2210 "gid to sid mapping\n");
2214 case OPT_REMOVE_UID_MAPPING
:
2215 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2217 !wbinfo_remove_uid_mapping(int_subarg
,
2220 d_fprintf(stderr
, "Could not remove uid to sid "
2225 case OPT_REMOVE_GID_MAPPING
:
2226 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2228 !wbinfo_remove_gid_mapping(int_subarg
,
2231 d_fprintf(stderr
, "Could not remove gid to sid "
2237 if (!wbinfo_check_secret(opt_domain_name
)) {
2238 d_fprintf(stderr
, "Could not check secret\n");
2243 if (!wbinfo_change_secret(opt_domain_name
)) {
2244 d_fprintf(stderr
, "Could not change secret\n");
2249 if (!wbinfo_ping_dc()) {
2250 d_fprintf(stderr
, "Could not ping our DC\n");
2255 if (!wbinfo_list_domains(false, verbose
)) {
2257 "Could not list trusted domains\n");
2262 if (!wbinfo_show_sequence(opt_domain_name
)) {
2264 "Could not show sequence numbers\n");
2268 case OPT_ONLINESTATUS
:
2269 if (!wbinfo_show_onlinestatus(opt_domain_name
)) {
2271 "Could not show online-status\n");
2276 if (!wbinfo_domain_info(string_arg
)) {
2278 "Could not get domain info\n");
2283 if (!wbinfo_get_userinfo(string_arg
)) {
2285 "Could not get info for user %s\n",
2290 case OPT_USER_SIDINFO
:
2291 if ( !wbinfo_get_user_sidinfo(string_arg
)) {
2293 "Could not get info for user "
2294 "sid %s\n", string_arg
);
2299 if ( !wbinfo_get_uidinfo(int_arg
)) {
2300 d_fprintf(stderr
, "Could not get info for uid "
2305 case OPT_GROUP_INFO
:
2306 if ( !wbinfo_get_groupinfo(string_arg
)) {
2307 d_fprintf(stderr
, "Could not get info for "
2308 "group %s\n", string_arg
);
2313 if ( !wbinfo_get_gidinfo(int_arg
)) {
2314 d_fprintf(stderr
, "Could not get info for gid "
2320 if (!wbinfo_get_usergroups(string_arg
)) {
2322 "Could not get groups for user %s\n",
2328 if (!wbinfo_get_usersids(string_arg
)) {
2329 d_fprintf(stderr
, "Could not get group SIDs "
2330 "for user SID %s\n",
2335 case OPT_USERDOMGROUPS
:
2336 if (!wbinfo_get_userdomgroups(string_arg
)) {
2337 d_fprintf(stderr
, "Could not get user's domain "
2338 "groups for user SID %s\n",
2343 case OPT_SIDALIASES
:
2344 if (!wbinfo_get_sidaliases(opt_domain_name
,
2346 d_fprintf(stderr
, "Could not get sid aliases "
2347 "for user SID %s\n", string_arg
);
2352 bool got_error
= false;
2354 if (!wbinfo_auth(string_arg
)) {
2356 "Could not authenticate user "
2357 "%s with plaintext "
2358 "password\n", string_arg
);
2362 if (!wbinfo_auth_crap(string_arg
, use_ntlmv2
,
2365 "Could not authenticate user "
2366 "%s with challenge/response\n",
2376 if (!wbinfo_pam_logon(string_arg
)) {
2377 d_fprintf(stderr
, "pam_logon failed for %s\n",
2385 wbc_status
= wbcLogoffUser(logoff_user
, logoff_uid
,
2387 d_printf("Logoff %s (%d): %s\n", logoff_user
,
2388 logoff_uid
, wbcErrorString(wbc_status
));
2392 uint32_t flags
= WBFLAG_PAM_KRB5
|
2393 WBFLAG_PAM_CACHED_LOGIN
|
2394 WBFLAG_PAM_FALLBACK_AFTER_KRB5
|
2395 WBFLAG_PAM_INFO3_TEXT
|
2396 WBFLAG_PAM_CONTACT_TRUSTDOM
;
2398 if (!wbinfo_auth_krb5(string_arg
, "FILE",
2401 "Could not authenticate user "
2402 "[%s] with Kerberos "
2403 "(ccache: %s)\n", string_arg
,
2410 if (!wbinfo_klog(string_arg
)) {
2411 d_fprintf(stderr
, "Could not klog user\n");
2416 if (!wbinfo_ping()) {
2417 d_fprintf(stderr
, "could not ping winbindd!\n");
2421 case OPT_SET_AUTH_USER
:
2422 if (!wbinfo_set_auth_user(string_arg
)) {
2426 case OPT_GET_AUTH_USER
:
2427 wbinfo_get_auth_user();
2430 case OPT_CCACHE_SAVE
:
2431 if (!wbinfo_ccache_save(string_arg
)) {
2436 if (!wbinfo_getdcname(string_arg
)) {
2440 case OPT_DSGETDCNAME
:
2441 if (!wbinfo_dsgetdcname(string_arg
, 0)) {
2445 case OPT_SEPARATOR
: {
2446 const char sep
= winbind_separator();
2450 d_printf("%c\n", sep
);
2453 case OPT_LIST_ALL_DOMAINS
:
2454 if (!wbinfo_list_domains(true, verbose
)) {
2458 case OPT_LIST_OWN_DOMAIN
:
2459 if (!wbinfo_list_own_domain()) {
2463 case OPT_CHANGE_USER_PASSWORD
:
2464 if (!wbinfo_change_user_password(string_arg
)) {
2466 "Could not change user password "
2467 "for user %s\n", string_arg
);
2472 /* generic configuration options */
2473 case OPT_DOMAIN_NAME
:
2477 case OPT_LOGOFF_USER
:
2478 case OPT_LOGOFF_UID
:
2481 d_fprintf(stderr
, "Invalid option\n");
2482 poptPrintHelp(pc
, stderr
, 0);
2494 poptFreeContext(pc
);