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
) {
573 /* List own domain */
575 static bool wbinfo_list_own_domain(void)
577 d_printf("%s\n", get_winbind_domain());
582 /* show sequence numbers */
583 static bool wbinfo_show_sequence(const char *domain
)
585 d_printf("This command has been deprecated. Please use the "
586 "--online-status option instead.\n");
590 /* show sequence numbers */
591 static bool wbinfo_show_onlinestatus(const char *domain
)
593 struct wbcDomainInfo
*domain_list
= NULL
;
595 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
598 wbc_status
= wbcListTrusts(&domain_list
, &num_domains
);
599 if (!WBC_ERROR_IS_OK(wbc_status
)) {
600 d_fprintf(stderr
, "failed to call wbcListTrusts: %s\n",
601 wbcErrorString(wbc_status
));
605 for (i
=0; i
<num_domains
; i
++) {
609 if (!strequal(domain_list
[i
].short_name
, domain
)) {
614 is_offline
= (domain_list
[i
].domain_flags
&
615 WBC_DOMINFO_DOMAIN_OFFLINE
);
617 d_printf("%s : %s\n",
618 domain_list
[i
].short_name
,
619 is_offline
? "offline" : "online" );
626 /* Show domain info */
628 static bool wbinfo_domain_info(const char *domain
)
630 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
631 struct wbcDomainInfo
*dinfo
= NULL
;
632 char sid_str
[WBC_SID_STRING_BUFLEN
];
634 if ((domain
== NULL
) || (strequal(domain
, ".")) || (domain
[0] == '\0')){
635 domain
= get_winbind_domain();
640 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
641 if (!WBC_ERROR_IS_OK(wbc_status
)) {
642 d_fprintf(stderr
, "failed to call wbcDomainInfo: %s\n",
643 wbcErrorString(wbc_status
));
647 wbcSidToStringBuf(&dinfo
->sid
, sid_str
, sizeof(sid_str
));
649 /* Display response */
651 d_printf("Name : %s\n", dinfo
->short_name
);
652 d_printf("Alt_Name : %s\n", dinfo
->dns_name
);
654 d_printf("SID : %s\n", sid_str
);
656 d_printf("Active Directory : %s\n",
657 (dinfo
->domain_flags
& WBC_DOMINFO_DOMAIN_AD
) ? "Yes" : "No");
658 d_printf("Native : %s\n",
659 (dinfo
->domain_flags
& WBC_DOMINFO_DOMAIN_NATIVE
) ?
662 d_printf("Primary : %s\n",
663 (dinfo
->domain_flags
& WBC_DOMINFO_DOMAIN_PRIMARY
) ?
666 wbcFreeMemory(dinfo
);
671 /* Get a foreign DC's name */
672 static bool wbinfo_getdcname(const char *domain_name
)
674 struct winbindd_request request
;
675 struct winbindd_response response
;
677 ZERO_STRUCT(request
);
678 ZERO_STRUCT(response
);
680 fstrcpy(request
.domain_name
, domain_name
);
684 if (winbindd_request_response(WINBINDD_GETDCNAME
, &request
,
685 &response
) != NSS_STATUS_SUCCESS
) {
686 d_fprintf(stderr
, "Could not get dc name for %s\n",domain_name
);
690 /* Display response */
692 d_printf("%s\n", response
.data
.dc_name
);
698 static bool wbinfo_dsgetdcname(const char *domain_name
, uint32_t flags
)
700 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
701 struct wbcDomainControllerInfoEx
*dc_info
;
704 wbc_status
= wbcLookupDomainControllerEx(domain_name
, NULL
, NULL
,
705 flags
| DS_DIRECTORY_SERVICE_REQUIRED
,
707 if (!WBC_ERROR_IS_OK(wbc_status
)) {
708 printf("Could not find dc for %s\n", domain_name
);
712 wbcGuidToString(dc_info
->domain_guid
, &str
);
714 d_printf("%s\n", dc_info
->dc_unc
);
715 d_printf("%s\n", dc_info
->dc_address
);
716 d_printf("%d\n", dc_info
->dc_address_type
);
717 d_printf("%s\n", str
);
718 d_printf("%s\n", dc_info
->domain_name
);
719 d_printf("%s\n", dc_info
->forest_name
);
720 d_printf("0x%08x\n", dc_info
->dc_flags
);
721 d_printf("%s\n", dc_info
->dc_site_name
);
722 d_printf("%s\n", dc_info
->client_site_name
);
727 /* Check trust account password */
729 static bool wbinfo_check_secret(const char *domain
)
731 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
732 struct wbcAuthErrorInfo
*error
= NULL
;
733 const char *domain_name
;
736 domain_name
= domain
;
738 domain_name
= get_winbind_domain();
741 wbc_status
= wbcCheckTrustCredentials(domain_name
, &error
);
743 d_printf("checking the trust secret for domain %s via RPC calls %s\n",
745 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
747 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
748 d_fprintf(stderr
, "error code was %s (0x%x)\n",
749 error
->nt_string
, error
->nt_status
);
750 wbcFreeMemory(error
);
752 if (!WBC_ERROR_IS_OK(wbc_status
)) {
753 d_fprintf(stderr
, "failed to call wbcCheckTrustCredentials: "
754 "%s\n", wbcErrorString(wbc_status
));
761 /* Find the currently connected DCs */
763 static bool wbinfo_dc_info(const char *domain_name
)
765 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
767 const char **dc_names
, **dc_ips
;
769 wbc_status
= wbcDcInfo(domain_name
, &num_dcs
,
771 if (!WBC_ERROR_IS_OK(wbc_status
)) {
772 printf("Could not find dc info %s\n",
773 domain_name
? domain_name
: "our domain");
777 for (i
=0; i
<num_dcs
; i
++) {
778 printf("%s (%s)\n", dc_names
[i
], dc_ips
[i
]);
780 wbcFreeMemory(dc_names
);
781 wbcFreeMemory(dc_ips
);
786 /* Change trust account password */
788 static bool wbinfo_change_secret(const char *domain
)
790 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
791 struct wbcAuthErrorInfo
*error
= NULL
;
792 const char *domain_name
;
795 domain_name
= domain
;
797 domain_name
= get_winbind_domain();
800 wbc_status
= wbcChangeTrustCredentials(domain_name
, &error
);
802 d_printf("changing the trust secret for domain %s via RPC calls %s\n",
804 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
806 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
807 d_fprintf(stderr
, "error code was %s (0x%x)\n",
808 error
->nt_string
, error
->nt_status
);
809 wbcFreeMemory(error
);
811 if (!WBC_ERROR_IS_OK(wbc_status
)) {
812 d_fprintf(stderr
, "failed to call wbcChangeTrustCredentials: "
813 "%s\n", wbcErrorString(wbc_status
));
820 /* Check DC connection */
822 static bool wbinfo_ping_dc(void)
824 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
825 struct wbcAuthErrorInfo
*error
= NULL
;
827 wbc_status
= wbcPingDc(NULL
, &error
);
829 d_printf("checking the NETLOGON dc connection %s\n",
830 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
832 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
833 d_fprintf(stderr
, "error code was %s (0x%x)\n",
834 error
->nt_string
, error
->nt_status
);
835 wbcFreeMemory(error
);
837 if (!WBC_ERROR_IS_OK(wbc_status
)) {
838 d_fprintf(stderr
, "failed to call wbcPingDc: %s\n",
839 wbcErrorString(wbc_status
));
846 /* Convert uid to sid */
848 static bool wbinfo_uid_to_sid(uid_t uid
)
850 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
851 struct wbcDomainSid sid
;
852 char sid_str
[WBC_SID_STRING_BUFLEN
];
856 wbc_status
= wbcUidToSid(uid
, &sid
);
857 if (!WBC_ERROR_IS_OK(wbc_status
)) {
858 d_fprintf(stderr
, "failed to call wbcUidToSid: %s\n",
859 wbcErrorString(wbc_status
));
863 wbcSidToStringBuf(&sid
, sid_str
, sizeof(sid_str
));
865 /* Display response */
867 d_printf("%s\n", sid_str
);
872 /* Convert gid to sid */
874 static bool wbinfo_gid_to_sid(gid_t gid
)
876 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
877 struct wbcDomainSid sid
;
878 char sid_str
[WBC_SID_STRING_BUFLEN
];
882 wbc_status
= wbcGidToSid(gid
, &sid
);
883 if (!WBC_ERROR_IS_OK(wbc_status
)) {
884 d_fprintf(stderr
, "failed to call wbcGidToSid: %s\n",
885 wbcErrorString(wbc_status
));
889 wbcSidToStringBuf(&sid
, sid_str
, sizeof(sid_str
));
891 /* Display response */
893 d_printf("%s\n", sid_str
);
898 /* Convert sid to uid */
900 static bool wbinfo_sid_to_uid(const char *sid_str
)
902 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
903 struct wbcDomainSid sid
;
908 wbc_status
= wbcStringToSid(sid_str
, &sid
);
909 if (!WBC_ERROR_IS_OK(wbc_status
)) {
910 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
911 wbcErrorString(wbc_status
));
915 wbc_status
= wbcSidToUid(&sid
, &uid
);
916 if (!WBC_ERROR_IS_OK(wbc_status
)) {
917 d_fprintf(stderr
, "failed to call wbcSidToUid: %s\n",
918 wbcErrorString(wbc_status
));
922 /* Display response */
924 d_printf("%d\n", (int)uid
);
929 static bool wbinfo_sid_to_gid(const char *sid_str
)
931 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
932 struct wbcDomainSid sid
;
937 wbc_status
= wbcStringToSid(sid_str
, &sid
);
938 if (!WBC_ERROR_IS_OK(wbc_status
)) {
939 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
940 wbcErrorString(wbc_status
));
944 wbc_status
= wbcSidToGid(&sid
, &gid
);
945 if (!WBC_ERROR_IS_OK(wbc_status
)) {
946 d_fprintf(stderr
, "failed to call wbcSidToGid: %s\n",
947 wbcErrorString(wbc_status
));
951 /* Display response */
953 d_printf("%d\n", (int)gid
);
958 static bool wbinfo_allocate_uid(void)
960 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
965 wbc_status
= wbcAllocateUid(&uid
);
966 if (!WBC_ERROR_IS_OK(wbc_status
)) {
967 d_fprintf(stderr
, "failed to call wbcAllocateUid: %s\n",
968 wbcErrorString(wbc_status
));
972 /* Display response */
974 d_printf("New uid: %u\n", (unsigned int)uid
);
979 static bool wbinfo_allocate_gid(void)
981 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
986 wbc_status
= wbcAllocateGid(&gid
);
987 if (!WBC_ERROR_IS_OK(wbc_status
)) {
988 d_fprintf(stderr
, "failed to call wbcAllocateGid: %s\n",
989 wbcErrorString(wbc_status
));
993 /* Display response */
995 d_printf("New gid: %u\n", (unsigned int)gid
);
1000 static bool wbinfo_set_uid_mapping(uid_t uid
, const char *sid_str
)
1002 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1003 struct wbcDomainSid sid
;
1007 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1008 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1009 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1010 wbcErrorString(wbc_status
));
1014 wbc_status
= wbcSetUidMapping(uid
, &sid
);
1015 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1016 d_fprintf(stderr
, "failed to call wbcSetUidMapping: %s\n",
1017 wbcErrorString(wbc_status
));
1021 /* Display response */
1023 d_printf("uid %u now mapped to sid %s\n",
1024 (unsigned int)uid
, sid_str
);
1029 static bool wbinfo_set_gid_mapping(gid_t gid
, const char *sid_str
)
1031 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1032 struct wbcDomainSid sid
;
1036 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1037 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1038 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1039 wbcErrorString(wbc_status
));
1043 wbc_status
= wbcSetGidMapping(gid
, &sid
);
1044 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1045 d_fprintf(stderr
, "failed to call wbcSetGidMapping: %s\n",
1046 wbcErrorString(wbc_status
));
1050 /* Display response */
1052 d_printf("gid %u now mapped to sid %s\n",
1053 (unsigned int)gid
, sid_str
);
1058 static bool wbinfo_remove_uid_mapping(uid_t uid
, const char *sid_str
)
1060 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1061 struct wbcDomainSid sid
;
1065 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1066 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1067 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1068 wbcErrorString(wbc_status
));
1072 wbc_status
= wbcRemoveUidMapping(uid
, &sid
);
1073 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1074 d_fprintf(stderr
, "failed to call wbcRemoveUidMapping: %s\n",
1075 wbcErrorString(wbc_status
));
1079 /* Display response */
1081 d_printf("Removed uid %u to sid %s mapping\n",
1082 (unsigned int)uid
, sid_str
);
1087 static bool wbinfo_remove_gid_mapping(gid_t gid
, const char *sid_str
)
1089 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1090 struct wbcDomainSid sid
;
1094 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1095 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1096 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1097 wbcErrorString(wbc_status
));
1101 wbc_status
= wbcRemoveGidMapping(gid
, &sid
);
1102 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1103 d_fprintf(stderr
, "failed to call wbcRemoveGidMapping: %s\n",
1104 wbcErrorString(wbc_status
));
1108 /* Display response */
1110 d_printf("Removed gid %u to sid %s mapping\n",
1111 (unsigned int)gid
, sid_str
);
1116 /* Convert sid to string */
1118 static bool wbinfo_lookupsid(const char *sid_str
)
1120 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1121 struct wbcDomainSid sid
;
1124 enum wbcSidType type
;
1126 /* Send off request */
1128 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1129 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1130 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1131 wbcErrorString(wbc_status
));
1135 wbc_status
= wbcLookupSid(&sid
, &domain
, &name
, &type
);
1136 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1137 d_fprintf(stderr
, "failed to call wbcLookupSid: %s\n",
1138 wbcErrorString(wbc_status
));
1142 /* Display response */
1144 d_printf("%s%c%s %d\n",
1145 domain
, winbind_separator(), name
, type
);
1150 /* Convert sid to fullname */
1152 static bool wbinfo_lookupsid_fullname(const char *sid_str
)
1154 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1155 struct wbcDomainSid sid
;
1158 enum wbcSidType type
;
1160 /* Send off request */
1162 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1163 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1164 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1165 wbcErrorString(wbc_status
));
1169 wbc_status
= wbcGetDisplayName(&sid
, &domain
, &name
, &type
);
1170 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1171 d_fprintf(stderr
, "failed to call wbcGetDisplayName: %s\n",
1172 wbcErrorString(wbc_status
));
1176 /* Display response */
1178 d_printf("%s%c%s %d\n",
1179 domain
, winbind_separator(), name
, type
);
1184 /* Lookup a list of RIDs */
1186 static bool wbinfo_lookuprids(const char *domain
, const char *arg
)
1188 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1189 struct wbcDomainInfo
*dinfo
= NULL
;
1190 char *domain_name
= NULL
;
1191 const char **names
= NULL
;
1192 enum wbcSidType
*types
= NULL
;
1195 uint32_t *rids
= NULL
;
1198 TALLOC_CTX
*mem_ctx
= NULL
;
1201 if ((domain
== NULL
) || (strequal(domain
, ".")) || (domain
[0] == '\0')){
1202 domain
= get_winbind_domain();
1207 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
1208 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1209 d_printf("wbcDomainInfo(%s) failed: %s\n", domain
,
1210 wbcErrorString(wbc_status
));
1214 mem_ctx
= talloc_new(NULL
);
1215 if (mem_ctx
== NULL
) {
1216 d_printf("talloc_new failed\n");
1224 while (next_token_talloc(mem_ctx
, &p
, &ridstr
, " ,\n")) {
1225 uint32_t rid
= strtoul(ridstr
, NULL
, 10);
1226 rids
= talloc_realloc(mem_ctx
, rids
, uint32_t, num_rids
+ 1);
1228 d_printf("talloc_realloc failed\n");
1230 rids
[num_rids
] = rid
;
1235 d_printf("no rids\n");
1239 wbc_status
= wbcLookupRids(&dinfo
->sid
, num_rids
, rids
,
1240 (const char **)&domain_name
, &names
, &types
);
1241 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1242 d_printf("winbind_lookup_rids failed: %s\n",
1243 wbcErrorString(wbc_status
));
1247 d_printf("Domain: %s\n", domain_name
);
1249 for (i
=0; i
<num_rids
; i
++) {
1250 d_printf("%8d: %s (%s)\n", rids
[i
], names
[i
],
1251 wbcSidTypeString(types
[i
]));
1256 wbcFreeMemory(dinfo
);
1257 wbcFreeMemory(domain_name
);
1258 wbcFreeMemory(names
);
1259 wbcFreeMemory(types
);
1260 TALLOC_FREE(mem_ctx
);
1264 /* Convert string to sid */
1266 static bool wbinfo_lookupname(const char *full_name
)
1268 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1269 struct wbcDomainSid sid
;
1270 char sid_str
[WBC_SID_STRING_BUFLEN
];
1271 enum wbcSidType type
;
1272 fstring domain_name
;
1273 fstring account_name
;
1275 /* Send off request */
1277 parse_wbinfo_domain_user(full_name
, domain_name
,
1280 wbc_status
= wbcLookupName(domain_name
, account_name
,
1282 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1283 d_fprintf(stderr
, "failed to call wbcLookupName: %s\n",
1284 wbcErrorString(wbc_status
));
1288 wbcSidToStringBuf(&sid
, sid_str
, sizeof(sid_str
));
1290 /* Display response */
1292 d_printf("%s %s (%d)\n", sid_str
, wbcSidTypeString(type
), type
);
1297 static char *wbinfo_prompt_pass(TALLOC_CTX
*mem_ctx
,
1299 const char *username
)
1302 const char *ret
= NULL
;
1304 prompt
= talloc_asprintf(mem_ctx
, "Enter %s's ", username
);
1309 prompt
= talloc_asprintf_append(prompt
, "%s ", prefix
);
1314 prompt
= talloc_asprintf_append(prompt
, "password: ");
1319 ret
= getpass(prompt
);
1320 TALLOC_FREE(prompt
);
1322 return talloc_strdup(mem_ctx
, ret
);
1325 /* Authenticate a user with a plaintext password */
1327 static bool wbinfo_auth_krb5(char *username
, const char *cctype
, uint32_t flags
)
1329 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1332 char *password
= NULL
;
1334 char *local_cctype
= NULL
;
1336 struct wbcLogonUserParams params
;
1337 struct wbcLogonUserInfo
*info
;
1338 struct wbcAuthErrorInfo
*error
;
1339 struct wbcUserPasswordPolicyInfo
*policy
;
1340 TALLOC_CTX
*frame
= talloc_tos();
1342 if ((s
= talloc_strdup(frame
, username
)) == NULL
) {
1346 if ((p
= strchr(s
, '%')) != NULL
) {
1349 password
= talloc_strdup(frame
, p
);
1351 password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1354 local_cctype
= talloc_strdup(frame
, cctype
);
1360 params
.username
= name
;
1361 params
.password
= password
;
1362 params
.num_blobs
= 0;
1363 params
.blobs
= NULL
;
1365 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
,
1371 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1372 d_fprintf(stderr
, "failed to call wbcAddNamedBlob: %s\n",
1373 wbcErrorString(wbc_status
));
1377 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
,
1383 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1384 d_fprintf(stderr
, "failed to call wbcAddNamedBlob: %s\n",
1385 wbcErrorString(wbc_status
));
1389 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
,
1393 (uint8_t *)local_cctype
,
1395 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1396 d_fprintf(stderr
, "failed to call wbcAddNamedBlob: %s\n",
1397 wbcErrorString(wbc_status
));
1401 wbc_status
= wbcLogonUser(¶ms
, &info
, &error
, &policy
);
1403 d_printf("plaintext kerberos password authentication for [%s] %s "
1404 "(requesting cctype: %s)\n",
1405 username
, WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed",
1410 "error code was %s (0x%x)\nerror message was: %s\n",
1413 error
->display_string
);
1416 if (WBC_ERROR_IS_OK(wbc_status
)) {
1417 if (flags
& WBFLAG_PAM_INFO3_TEXT
) {
1418 if (info
&& info
->info
&& info
->info
->user_flags
&
1419 NETLOGON_CACHED_ACCOUNT
) {
1420 d_printf("user_flgs: "
1421 "NETLOGON_CACHED_ACCOUNT\n");
1427 for (i
=0; i
< info
->num_blobs
; i
++) {
1428 if (strequal(info
->blobs
[i
].name
,
1430 d_printf("credentials were put "
1433 info
->blobs
[i
].blob
.data
);
1438 d_printf("no credentials cached\n");
1443 wbcFreeMemory(params
.blobs
);
1445 return WBC_ERROR_IS_OK(wbc_status
);
1448 /* Authenticate a user with a plaintext password */
1450 static bool wbinfo_auth(char *username
)
1452 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1455 char *password
= NULL
;
1457 TALLOC_CTX
*frame
= talloc_tos();
1459 if ((s
= talloc_strdup(frame
, username
)) == NULL
) {
1463 if ((p
= strchr(s
, '%')) != NULL
) {
1466 password
= talloc_strdup(frame
, p
);
1468 password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1473 wbc_status
= wbcAuthenticateUser(name
, password
);
1475 d_printf("plaintext password authentication %s\n",
1476 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1479 if (response
.data
.auth
.nt_status
)
1481 "error code was %s (0x%x)\nerror message was: %s\n",
1482 response
.data
.auth
.nt_status_string
,
1483 response
.data
.auth
.nt_status
,
1484 response
.data
.auth
.error_string
);
1487 return WBC_ERROR_IS_OK(wbc_status
);
1490 /* Authenticate a user with a challenge/response */
1492 static bool wbinfo_auth_crap(char *username
, bool use_ntlmv2
, bool use_lanman
)
1494 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1495 struct wbcAuthUserParams params
;
1496 struct wbcAuthUserInfo
*info
= NULL
;
1497 struct wbcAuthErrorInfo
*err
= NULL
;
1498 DATA_BLOB lm
= data_blob_null
;
1499 DATA_BLOB nt
= data_blob_null
;
1501 fstring name_domain
;
1504 TALLOC_CTX
*frame
= talloc_tos();
1506 p
= strchr(username
, '%');
1510 pass
= talloc_strdup(frame
, p
+ 1);
1512 pass
= wbinfo_prompt_pass(frame
, NULL
, username
);
1515 parse_wbinfo_domain_user(username
, name_domain
, name_user
);
1517 params
.account_name
= name_user
;
1518 params
.domain_name
= name_domain
;
1519 params
.workstation_name
= NULL
;
1522 params
.parameter_control
= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
|
1523 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
;
1525 params
.level
= WBC_AUTH_USER_LEVEL_RESPONSE
;
1527 generate_random_buffer(params
.password
.response
.challenge
, 8);
1530 DATA_BLOB server_chal
;
1531 DATA_BLOB names_blob
;
1533 server_chal
= data_blob(params
.password
.response
.challenge
, 8);
1535 /* Pretend this is a login to 'us', for blob purposes */
1536 names_blob
= NTLMv2_generate_names_blob(NULL
,
1537 get_winbind_netbios_name(),
1538 get_winbind_domain());
1540 if (!SMBNTLMv2encrypt(NULL
, name_user
, name_domain
, pass
,
1543 &lm
, &nt
, NULL
, NULL
)) {
1544 data_blob_free(&names_blob
);
1545 data_blob_free(&server_chal
);
1549 data_blob_free(&names_blob
);
1550 data_blob_free(&server_chal
);
1555 lm
= data_blob(NULL
, 24);
1556 ok
= SMBencrypt(pass
,
1557 params
.password
.response
.challenge
,
1560 data_blob_free(&lm
);
1563 nt
= data_blob(NULL
, 24);
1564 SMBNTencrypt(pass
, params
.password
.response
.challenge
,
1568 params
.password
.response
.nt_length
= nt
.length
;
1569 params
.password
.response
.nt_data
= nt
.data
;
1570 params
.password
.response
.lm_length
= lm
.length
;
1571 params
.password
.response
.lm_data
= lm
.data
;
1573 wbc_status
= wbcAuthenticateUserEx(¶ms
, &info
, &err
);
1575 /* Display response */
1577 d_printf("challenge/response password authentication %s\n",
1578 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1580 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
1582 "error code was %s (0x%x)\nerror message was: %s\n",
1585 err
->display_string
);
1587 } else if (WBC_ERROR_IS_OK(wbc_status
)) {
1588 wbcFreeMemory(info
);
1591 data_blob_free(&nt
);
1592 data_blob_free(&lm
);
1594 return WBC_ERROR_IS_OK(wbc_status
);
1597 /* Authenticate a user with a plaintext password */
1599 static bool wbinfo_pam_logon(char *username
)
1601 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1602 struct wbcLogonUserParams params
;
1603 struct wbcAuthErrorInfo
*error
;
1606 TALLOC_CTX
*frame
= talloc_tos();
1610 ZERO_STRUCT(params
);
1612 if ((s
= talloc_strdup(frame
, username
)) == NULL
) {
1616 if ((p
= strchr(s
, '%')) != NULL
) {
1619 params
.password
= talloc_strdup(frame
, p
);
1621 params
.password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1623 params
.username
= s
;
1625 flags
= WBFLAG_PAM_CACHED_LOGIN
;
1627 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
, ¶ms
.blobs
,
1629 (uint8_t *)&flags
, sizeof(flags
));
1630 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1631 d_printf("wbcAddNamedBlob failed: %s\n",
1632 wbcErrorString(wbc_status
));
1638 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
, ¶ms
.blobs
,
1640 (uint8_t *)&uid
, sizeof(uid
));
1641 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1642 d_printf("wbcAddNamedBlob failed: %s\n",
1643 wbcErrorString(wbc_status
));
1647 wbc_status
= wbcLogonUser(¶ms
, NULL
, &error
, NULL
);
1649 wbcFreeMemory(params
.blobs
);
1651 d_printf("plaintext password authentication %s\n",
1652 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1654 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1656 "error code was %s (0x%x)\nerror message was: %s\n",
1658 (int)error
->nt_status
,
1659 error
->display_string
);
1660 wbcFreeMemory(error
);
1666 /* Save creds with winbind */
1668 static bool wbinfo_ccache_save(char *username
)
1670 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1673 char *password
= NULL
;
1675 TALLOC_CTX
*frame
= talloc_stackframe();
1677 s
= talloc_strdup(frame
, username
);
1686 password
= talloc_strdup(frame
, p
);
1688 password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1693 wbc_status
= wbcCredentialSave(name
, password
);
1695 d_printf("saving creds %s\n",
1696 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1700 return WBC_ERROR_IS_OK(wbc_status
);
1703 #ifdef WITH_FAKE_KASERVER
1704 /* Authenticate a user with a plaintext password and set a token */
1706 static bool wbinfo_klog(char *username
)
1708 struct winbindd_request request
;
1709 struct winbindd_response response
;
1713 /* Send off request */
1715 ZERO_STRUCT(request
);
1716 ZERO_STRUCT(response
);
1718 p
= strchr(username
, '%');
1722 fstrcpy(request
.data
.auth
.user
, username
);
1723 fstrcpy(request
.data
.auth
.pass
, p
+ 1);
1726 fstrcpy(request
.data
.auth
.user
, username
);
1727 fstrcpy(request
.data
.auth
.pass
, getpass("Password: "));
1730 request
.flags
|= WBFLAG_PAM_AFS_TOKEN
;
1732 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
,
1735 /* Display response */
1737 d_printf("plaintext password authentication %s\n",
1738 (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed");
1740 if (response
.data
.auth
.nt_status
)
1742 "error code was %s (0x%x)\nerror message was: %s\n",
1743 response
.data
.auth
.nt_status_string
,
1744 response
.data
.auth
.nt_status
,
1745 response
.data
.auth
.error_string
);
1747 if (result
!= NSS_STATUS_SUCCESS
)
1750 if (response
.extra_data
.data
== NULL
) {
1751 d_fprintf(stderr
, "Did not get token data\n");
1755 if (!afs_settoken_str((char *)response
.extra_data
.data
)) {
1756 d_fprintf(stderr
, "Could not set token\n");
1760 d_printf("Successfully created AFS token\n");
1764 static bool wbinfo_klog(char *username
)
1766 d_fprintf(stderr
, "No AFS support compiled in.\n");
1771 /* Print domain users */
1773 static bool print_domain_users(const char *domain
)
1775 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1777 uint32_t num_users
= 0;
1778 const char **users
= NULL
;
1780 /* Send request to winbind daemon */
1782 /* '.' is the special sign for our own domain */
1783 if (domain
&& strcmp(domain
, ".") == 0) {
1784 domain
= get_winbind_domain();
1787 wbc_status
= wbcListUsers(domain
, &num_users
, &users
);
1788 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1792 for (i
=0; i
< num_users
; i
++) {
1793 d_printf("%s\n", users
[i
]);
1796 wbcFreeMemory(users
);
1801 /* Print domain groups */
1803 static bool print_domain_groups(const char *domain
)
1805 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1807 uint32_t num_groups
= 0;
1808 const char **groups
= NULL
;
1810 /* Send request to winbind daemon */
1812 /* '.' is the special sign for our own domain */
1813 if (domain
&& strcmp(domain
, ".") == 0) {
1814 domain
= get_winbind_domain();
1817 wbc_status
= wbcListGroups(domain
, &num_groups
, &groups
);
1818 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1819 d_fprintf(stderr
, "failed to call wbcListGroups: %s\n",
1820 wbcErrorString(wbc_status
));
1824 for (i
=0; i
< num_groups
; i
++) {
1825 d_printf("%s\n", groups
[i
]);
1828 wbcFreeMemory(groups
);
1833 /* Set the authorised user for winbindd access in secrets.tdb */
1835 static bool wbinfo_set_auth_user(char *username
)
1837 d_fprintf(stderr
, "This functionality was moved to the 'net' utility.\n"
1838 "See 'net help setauthuser' for details.\n");
1842 static void wbinfo_get_auth_user(void)
1844 d_fprintf(stderr
, "This functionality was moved to the 'net' utility.\n"
1845 "See 'net help getauthuser' for details.\n");
1848 static bool wbinfo_ping(void)
1852 wbc_status
= wbcPing();
1854 /* Display response */
1856 d_printf("Ping to winbindd %s\n",
1857 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1859 return WBC_ERROR_IS_OK(wbc_status
);
1862 static bool wbinfo_change_user_password(const char *username
)
1865 char *old_password
= NULL
;
1866 char *new_password
= NULL
;
1867 TALLOC_CTX
*frame
= talloc_tos();
1869 old_password
= wbinfo_prompt_pass(frame
, "old", username
);
1870 new_password
= wbinfo_prompt_pass(frame
, "new", username
);
1872 wbc_status
= wbcChangeUserPassword(username
, old_password
,new_password
);
1874 /* Display response */
1876 d_printf("Password change for user %s %s\n", username
,
1877 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1879 return WBC_ERROR_IS_OK(wbc_status
);
1885 OPT_SET_AUTH_USER
= 1000,
1897 OPT_SET_UID_MAPPING
,
1898 OPT_SET_GID_MAPPING
,
1899 OPT_REMOVE_UID_MAPPING
,
1900 OPT_REMOVE_GID_MAPPING
,
1902 OPT_LIST_ALL_DOMAINS
,
1903 OPT_LIST_OWN_DOMAIN
,
1910 OPT_CHANGE_USER_PASSWORD
,
1912 OPT_SID_TO_FULLNAME
,
1921 int main(int argc
, char **argv
, char **envp
)
1924 TALLOC_CTX
*frame
= talloc_stackframe();
1926 static char *string_arg
;
1927 char *string_subarg
= NULL
;
1928 static char *opt_domain_name
;
1930 int int_subarg
= -1;
1932 bool verbose
= false;
1933 bool use_ntlmv2
= false;
1934 bool use_lanman
= false;
1935 char *logoff_user
= getenv("USER");
1936 int logoff_uid
= geteuid();
1938 struct poptOption long_options
[] = {
1941 /* longName, shortName, argInfo, argPtr, value, descrip,
1944 { "domain-users", 'u', POPT_ARG_NONE
, 0, 'u', "Lists all domain users", "domain"},
1945 { "domain-groups", 'g', POPT_ARG_NONE
, 0, 'g', "Lists all domain groups", "domain" },
1946 { "WINS-by-name", 'N', POPT_ARG_STRING
, &string_arg
, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1947 { "WINS-by-ip", 'I', POPT_ARG_STRING
, &string_arg
, 'I', "Converts IP address to NetBIOS name", "IP" },
1948 { "name-to-sid", 'n', POPT_ARG_STRING
, &string_arg
, 'n', "Converts name to sid", "NAME" },
1949 { "sid-to-name", 's', POPT_ARG_STRING
, &string_arg
, 's', "Converts sid to name", "SID" },
1950 { "sid-to-fullname", 0, POPT_ARG_STRING
, &string_arg
,
1951 OPT_SID_TO_FULLNAME
, "Converts sid to fullname", "SID" },
1952 { "lookup-rids", 'R', POPT_ARG_STRING
, &string_arg
, 'R', "Converts RIDs to names", "RIDs" },
1953 { "uid-to-sid", 'U', POPT_ARG_INT
, &int_arg
, 'U', "Converts uid to sid" , "UID" },
1954 { "gid-to-sid", 'G', POPT_ARG_INT
, &int_arg
, 'G', "Converts gid to sid", "GID" },
1955 { "sid-to-uid", 'S', POPT_ARG_STRING
, &string_arg
, 'S', "Converts sid to uid", "SID" },
1956 { "sid-to-gid", 'Y', POPT_ARG_STRING
, &string_arg
, 'Y', "Converts sid to gid", "SID" },
1957 { "allocate-uid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_UID
,
1958 "Get a new UID out of idmap" },
1959 { "allocate-gid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_GID
,
1960 "Get a new GID out of idmap" },
1961 { "set-uid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_UID_MAPPING
, "Create or modify uid to sid mapping in idmap", "UID,SID" },
1962 { "set-gid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_GID_MAPPING
, "Create or modify gid to sid mapping in idmap", "GID,SID" },
1963 { "remove-uid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_REMOVE_UID_MAPPING
, "Remove uid to sid mapping in idmap", "UID,SID" },
1964 { "remove-gid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_REMOVE_GID_MAPPING
, "Remove gid to sid mapping in idmap", "GID,SID" },
1965 { "check-secret", 't', POPT_ARG_NONE
, 0, 't', "Check shared secret" },
1966 { "change-secret", 'c', POPT_ARG_NONE
, 0, 'c', "Change shared secret" },
1967 { "ping-dc", 'P', POPT_ARG_NONE
, 0, 'P',
1968 "Check the NETLOGON connection" },
1969 { "trusted-domains", 'm', POPT_ARG_NONE
, 0, 'm', "List trusted domains" },
1970 { "all-domains", 0, POPT_ARG_NONE
, 0, OPT_LIST_ALL_DOMAINS
, "List all domains (trusted and own domain)" },
1971 { "own-domain", 0, POPT_ARG_NONE
, 0, OPT_LIST_OWN_DOMAIN
, "List own domain" },
1972 { "sequence", 0, POPT_ARG_NONE
, 0, OPT_SEQUENCE
, "Deprecated command, see --online-status" },
1973 { "online-status", 0, POPT_ARG_NONE
, 0, OPT_ONLINESTATUS
, "Show whether domains are marked as online or offline"},
1974 { "domain-info", 'D', POPT_ARG_STRING
, &string_arg
, 'D', "Show most of the info we have about the domain" },
1975 { "user-info", 'i', POPT_ARG_STRING
, &string_arg
, 'i', "Get user info", "USER" },
1976 { "uid-info", 0, POPT_ARG_INT
, &int_arg
, OPT_UID_INFO
, "Get user info from uid", "UID" },
1977 { "group-info", 0, POPT_ARG_STRING
, &string_arg
, OPT_GROUP_INFO
, "Get group info", "GROUP" },
1978 { "user-sidinfo", 0, POPT_ARG_STRING
, &string_arg
, OPT_USER_SIDINFO
, "Get user info from sid", "SID" },
1979 { "gid-info", 0, POPT_ARG_INT
, &int_arg
, OPT_GID_INFO
, "Get group info from gid", "GID" },
1980 { "user-groups", 'r', POPT_ARG_STRING
, &string_arg
, 'r', "Get user groups", "USER" },
1981 { "user-domgroups", 0, POPT_ARG_STRING
, &string_arg
,
1982 OPT_USERDOMGROUPS
, "Get user domain groups", "SID" },
1983 { "sid-aliases", 0, POPT_ARG_STRING
, &string_arg
, OPT_SIDALIASES
, "Get sid aliases", "SID" },
1984 { "user-sids", 0, POPT_ARG_STRING
, &string_arg
, OPT_USERSIDS
, "Get user group sids for user SID", "SID" },
1985 { "authenticate", 'a', POPT_ARG_STRING
, &string_arg
, 'a', "authenticate user", "user%password" },
1986 { "pam-logon", 0, POPT_ARG_STRING
, &string_arg
, OPT_PAM_LOGON
,
1987 "do a pam logon equivalent", "user%password" },
1988 { "logoff", 0, POPT_ARG_NONE
, NULL
, OPT_LOGOFF
,
1989 "log off user", "uid" },
1990 { "logoff-user", 0, POPT_ARG_STRING
, &logoff_user
,
1991 OPT_LOGOFF_USER
, "username to log off" },
1992 { "logoff-uid", 0, POPT_ARG_INT
, &logoff_uid
,
1993 OPT_LOGOFF_UID
, "uid to log off" },
1994 { "set-auth-user", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_AUTH_USER
, "Store user and password used by winbindd (root only)", "user%password" },
1995 { "ccache-save", 0, POPT_ARG_STRING
, &string_arg
,
1996 OPT_CCACHE_SAVE
, "Store user and password for ccache "
1997 "operation", "user%password" },
1998 { "getdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_GETDCNAME
,
1999 "Get a DC name for a foreign domain", "domainname" },
2000 { "dsgetdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_DSGETDCNAME
, "Find a DC for a domain", "domainname" },
2001 { "dc-info", 0, POPT_ARG_STRING
, &string_arg
, OPT_DC_INFO
,
2002 "Find the currently known DCs", "domainname" },
2003 { "get-auth-user", 0, POPT_ARG_NONE
, NULL
, OPT_GET_AUTH_USER
, "Retrieve user and password used by winbindd (root only)", NULL
},
2004 { "ping", 'p', POPT_ARG_NONE
, 0, 'p', "Ping winbindd to see if it is alive" },
2005 { "domain", 0, POPT_ARG_STRING
, &opt_domain_name
, OPT_DOMAIN_NAME
, "Define to the domain to restrict operation", "domain" },
2006 #ifdef WITH_FAKE_KASERVER
2007 { "klog", 'k', POPT_ARG_STRING
, &string_arg
, 'k', "set an AFS token from winbind", "user%password" },
2010 { "krb5auth", 'K', POPT_ARG_STRING
, &string_arg
, 'K', "authenticate user using Kerberos", "user%password" },
2011 /* destroys wbinfo --help output */
2012 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
2014 { "separator", 0, POPT_ARG_NONE
, 0, OPT_SEPARATOR
, "Get the active winbind separator", NULL
},
2015 { "verbose", 0, POPT_ARG_NONE
, 0, OPT_VERBOSE
, "Print additional information per command", NULL
},
2016 { "change-user-password", 0, POPT_ARG_STRING
, &string_arg
, OPT_CHANGE_USER_PASSWORD
, "Change the password for a user", NULL
},
2017 { "ntlmv2", 0, POPT_ARG_NONE
, 0, OPT_NTLMV2
, "Use NTLMv2 cryptography for user authentication", NULL
},
2018 { "lanman", 0, POPT_ARG_NONE
, 0, OPT_LANMAN
, "Use lanman cryptography for user authentication", NULL
},
2023 /* Samba client initialisation */
2029 pc
= poptGetContext("wbinfo", argc
, (const char **)argv
,
2032 /* Parse command line options */
2035 poptPrintHelp(pc
, stderr
, 0);
2039 while((opt
= poptGetNextOpt(pc
)) != -1) {
2040 /* get the generic configuration parameters like --domain */
2054 poptFreeContext(pc
);
2056 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
2057 POPT_CONTEXT_KEEP_FIRST
);
2059 while((opt
= poptGetNextOpt(pc
)) != -1) {
2062 if (!print_domain_users(opt_domain_name
)) {
2064 "Error looking up domain users\n");
2069 if (!print_domain_groups(opt_domain_name
)) {
2071 "Error looking up domain groups\n");
2076 if (!wbinfo_lookupsid(string_arg
)) {
2078 "Could not lookup sid %s\n",
2083 case OPT_SID_TO_FULLNAME
:
2084 if (!wbinfo_lookupsid_fullname(string_arg
)) {
2085 d_fprintf(stderr
, "Could not lookup sid %s\n",
2091 if (!wbinfo_lookuprids(opt_domain_name
, string_arg
)) {
2092 d_fprintf(stderr
, "Could not lookup RIDs %s\n",
2098 if (!wbinfo_lookupname(string_arg
)) {
2099 d_fprintf(stderr
, "Could not lookup name %s\n",
2105 if (!wbinfo_wins_byname(string_arg
)) {
2107 "Could not lookup WINS by name %s\n",
2113 if (!wbinfo_wins_byip(string_arg
)) {
2115 "Could not lookup WINS by IP %s\n",
2121 if (!wbinfo_uid_to_sid(int_arg
)) {
2123 "Could not convert uid %d to sid\n",
2129 if (!wbinfo_gid_to_sid(int_arg
)) {
2131 "Could not convert gid %d to sid\n",
2137 if (!wbinfo_sid_to_uid(string_arg
)) {
2139 "Could not convert sid %s to uid\n",
2145 if (!wbinfo_sid_to_gid(string_arg
)) {
2147 "Could not convert sid %s to gid\n",
2152 case OPT_ALLOCATE_UID
:
2153 if (!wbinfo_allocate_uid()) {
2154 d_fprintf(stderr
, "Could not allocate a uid\n");
2158 case OPT_ALLOCATE_GID
:
2159 if (!wbinfo_allocate_gid()) {
2160 d_fprintf(stderr
, "Could not allocate a gid\n");
2164 case OPT_SET_UID_MAPPING
:
2165 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2167 !wbinfo_set_uid_mapping(int_subarg
, string_subarg
))
2169 d_fprintf(stderr
, "Could not create or modify "
2170 "uid to sid mapping\n");
2174 case OPT_SET_GID_MAPPING
:
2175 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2177 !wbinfo_set_gid_mapping(int_subarg
, string_subarg
))
2179 d_fprintf(stderr
, "Could not create or modify "
2180 "gid to sid mapping\n");
2184 case OPT_REMOVE_UID_MAPPING
:
2185 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2187 !wbinfo_remove_uid_mapping(int_subarg
,
2190 d_fprintf(stderr
, "Could not remove uid to sid "
2195 case OPT_REMOVE_GID_MAPPING
:
2196 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2198 !wbinfo_remove_gid_mapping(int_subarg
,
2201 d_fprintf(stderr
, "Could not remove gid to sid "
2207 if (!wbinfo_check_secret(opt_domain_name
)) {
2208 d_fprintf(stderr
, "Could not check secret\n");
2213 if (!wbinfo_change_secret(opt_domain_name
)) {
2214 d_fprintf(stderr
, "Could not change secret\n");
2219 if (!wbinfo_ping_dc()) {
2220 d_fprintf(stderr
, "Could not ping our DC\n");
2225 if (!wbinfo_list_domains(false, verbose
)) {
2227 "Could not list trusted domains\n");
2232 if (!wbinfo_show_sequence(opt_domain_name
)) {
2234 "Could not show sequence numbers\n");
2238 case OPT_ONLINESTATUS
:
2239 if (!wbinfo_show_onlinestatus(opt_domain_name
)) {
2241 "Could not show online-status\n");
2246 if (!wbinfo_domain_info(string_arg
)) {
2248 "Could not get domain info\n");
2253 if (!wbinfo_get_userinfo(string_arg
)) {
2255 "Could not get info for user %s\n",
2260 case OPT_USER_SIDINFO
:
2261 if ( !wbinfo_get_user_sidinfo(string_arg
)) {
2263 "Could not get info for user "
2264 "sid %s\n", string_arg
);
2269 if ( !wbinfo_get_uidinfo(int_arg
)) {
2270 d_fprintf(stderr
, "Could not get info for uid "
2275 case OPT_GROUP_INFO
:
2276 if ( !wbinfo_get_groupinfo(string_arg
)) {
2277 d_fprintf(stderr
, "Could not get info for "
2278 "group %s\n", string_arg
);
2283 if ( !wbinfo_get_gidinfo(int_arg
)) {
2284 d_fprintf(stderr
, "Could not get info for gid "
2290 if (!wbinfo_get_usergroups(string_arg
)) {
2292 "Could not get groups for user %s\n",
2298 if (!wbinfo_get_usersids(string_arg
)) {
2299 d_fprintf(stderr
, "Could not get group SIDs "
2300 "for user SID %s\n",
2305 case OPT_USERDOMGROUPS
:
2306 if (!wbinfo_get_userdomgroups(string_arg
)) {
2307 d_fprintf(stderr
, "Could not get user's domain "
2308 "groups for user SID %s\n",
2313 case OPT_SIDALIASES
:
2314 if (!wbinfo_get_sidaliases(opt_domain_name
,
2316 d_fprintf(stderr
, "Could not get sid aliases "
2317 "for user SID %s\n", string_arg
);
2322 bool got_error
= false;
2324 if (!wbinfo_auth(string_arg
)) {
2326 "Could not authenticate user "
2327 "%s with plaintext "
2328 "password\n", string_arg
);
2332 if (!wbinfo_auth_crap(string_arg
, use_ntlmv2
,
2335 "Could not authenticate user "
2336 "%s with challenge/response\n",
2346 if (!wbinfo_pam_logon(string_arg
)) {
2347 d_fprintf(stderr
, "pam_logon failed for %s\n",
2356 wbc_status
= wbcLogoffUser(logoff_user
, logoff_uid
,
2358 d_printf("Logoff %s (%d): %s\n", logoff_user
,
2359 logoff_uid
, wbcErrorString(wbc_status
));
2363 uint32_t flags
= WBFLAG_PAM_KRB5
|
2364 WBFLAG_PAM_CACHED_LOGIN
|
2365 WBFLAG_PAM_FALLBACK_AFTER_KRB5
|
2366 WBFLAG_PAM_INFO3_TEXT
|
2367 WBFLAG_PAM_CONTACT_TRUSTDOM
;
2369 if (!wbinfo_auth_krb5(string_arg
, "FILE",
2372 "Could not authenticate user "
2373 "[%s] with Kerberos "
2374 "(ccache: %s)\n", string_arg
,
2381 if (!wbinfo_klog(string_arg
)) {
2382 d_fprintf(stderr
, "Could not klog user\n");
2387 if (!wbinfo_ping()) {
2388 d_fprintf(stderr
, "could not ping winbindd!\n");
2392 case OPT_SET_AUTH_USER
:
2393 if (!wbinfo_set_auth_user(string_arg
)) {
2397 case OPT_GET_AUTH_USER
:
2398 wbinfo_get_auth_user();
2401 case OPT_CCACHE_SAVE
:
2402 if (!wbinfo_ccache_save(string_arg
)) {
2407 if (!wbinfo_getdcname(string_arg
)) {
2411 case OPT_DSGETDCNAME
:
2412 if (!wbinfo_dsgetdcname(string_arg
, 0)) {
2417 if (!wbinfo_dc_info(string_arg
)) {
2421 case OPT_SEPARATOR
: {
2422 const char sep
= winbind_separator();
2426 d_printf("%c\n", sep
);
2429 case OPT_LIST_ALL_DOMAINS
:
2430 if (!wbinfo_list_domains(true, verbose
)) {
2434 case OPT_LIST_OWN_DOMAIN
:
2435 if (!wbinfo_list_own_domain()) {
2439 case OPT_CHANGE_USER_PASSWORD
:
2440 if (!wbinfo_change_user_password(string_arg
)) {
2442 "Could not change user password "
2443 "for user %s\n", string_arg
);
2448 /* generic configuration options */
2449 case OPT_DOMAIN_NAME
:
2453 case OPT_LOGOFF_USER
:
2454 case OPT_LOGOFF_UID
:
2457 d_fprintf(stderr
, "Invalid option\n");
2458 poptPrintHelp(pc
, stderr
, 0);
2470 poptFreeContext(pc
);