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
,
195 /* pull pwent info for a given uid */
196 static bool wbinfo_get_uidinfo(int uid
)
198 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
199 struct passwd
*pwd
= NULL
;
201 wbc_status
= wbcGetpwuid(uid
, &pwd
);
202 if (!WBC_ERROR_IS_OK(wbc_status
)) {
203 d_fprintf(stderr
, "failed to call wbcGetpwuid: %s\n",
204 wbcErrorString(wbc_status
));
208 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
211 (unsigned int)pwd
->pw_uid
,
212 (unsigned int)pwd
->pw_gid
,
220 static bool wbinfo_get_user_sidinfo(const char *sid_str
)
222 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
223 struct passwd
*pwd
= NULL
;
224 struct wbcDomainSid sid
;
226 wbc_status
= wbcStringToSid(sid_str
, &sid
);
227 wbc_status
= wbcGetpwsid(&sid
, &pwd
);
228 if (!WBC_ERROR_IS_OK(wbc_status
)) {
229 d_fprintf(stderr
, "failed to call wbcGetpwsid: %s\n",
230 wbcErrorString(wbc_status
));
234 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
237 (unsigned int)pwd
->pw_uid
,
238 (unsigned int)pwd
->pw_gid
,
247 /* pull grent for a given group */
248 static bool wbinfo_get_groupinfo(const char *group
)
250 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
254 wbc_status
= wbcGetgrnam(group
, &grp
);
255 if (!WBC_ERROR_IS_OK(wbc_status
)) {
256 d_fprintf(stderr
, "failed to call wbcGetgrnam: %s\n",
257 wbcErrorString(wbc_status
));
261 d_printf("%s:%s:%u:",
264 (unsigned int)grp
->gr_gid
);
267 while (*mem
!= NULL
) {
268 d_printf("%s%s", *mem
, *(mem
+1) != NULL
? "," : "");
278 /* pull grent for a given gid */
279 static bool wbinfo_get_gidinfo(int gid
)
281 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
285 wbc_status
= wbcGetgrgid(gid
, &grp
);
286 if (!WBC_ERROR_IS_OK(wbc_status
)) {
287 d_fprintf(stderr
, "failed to call wbcGetgrgid: %s\n",
288 wbcErrorString(wbc_status
));
292 d_printf("%s:%s:%u:",
295 (unsigned int)grp
->gr_gid
);
298 while (*mem
!= NULL
) {
299 d_printf("%s%s", *mem
, *(mem
+1) != NULL
? "," : "");
309 /* List groups a user is a member of */
311 static bool wbinfo_get_usergroups(const char *user
)
313 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
316 gid_t
*groups
= NULL
;
320 wbc_status
= wbcGetGroups(user
, &num_groups
, &groups
);
321 if (!WBC_ERROR_IS_OK(wbc_status
)) {
322 d_fprintf(stderr
, "failed to call wbcGetGroups: %s\n",
323 wbcErrorString(wbc_status
));
327 for (i
= 0; i
< num_groups
; i
++) {
328 d_printf("%d\n", (int)groups
[i
]);
331 wbcFreeMemory(groups
);
337 /* List group SIDs a user SID is a member of */
338 static bool wbinfo_get_usersids(const char *user_sid_str
)
340 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
343 struct wbcDomainSid user_sid
, *sids
= NULL
;
347 wbc_status
= wbcStringToSid(user_sid_str
, &user_sid
);
348 if (!WBC_ERROR_IS_OK(wbc_status
)) {
349 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
350 wbcErrorString(wbc_status
));
354 wbc_status
= wbcLookupUserSids(&user_sid
, false, &num_sids
, &sids
);
355 if (!WBC_ERROR_IS_OK(wbc_status
)) {
356 d_fprintf(stderr
, "failed to call wbcLookupUserSids: %s\n",
357 wbcErrorString(wbc_status
));
361 for (i
= 0; i
< num_sids
; i
++) {
362 char str
[WBC_SID_STRING_BUFLEN
];
363 wbcSidToStringBuf(&sids
[i
], str
, sizeof(str
));
364 d_printf("%s\n", str
);
372 static bool wbinfo_get_userdomgroups(const char *user_sid_str
)
374 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
377 struct wbcDomainSid user_sid
, *sids
= NULL
;
381 wbc_status
= wbcStringToSid(user_sid_str
, &user_sid
);
382 if (!WBC_ERROR_IS_OK(wbc_status
)) {
383 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
384 wbcErrorString(wbc_status
));
388 wbc_status
= wbcLookupUserSids(&user_sid
, true, &num_sids
, &sids
);
389 if (!WBC_ERROR_IS_OK(wbc_status
)) {
390 d_fprintf(stderr
, "failed to call wbcLookupUserSids: %s\n",
391 wbcErrorString(wbc_status
));
395 for (i
= 0; i
< num_sids
; i
++) {
396 char str
[WBC_SID_STRING_BUFLEN
];
397 wbcSidToStringBuf(&sids
[i
], str
, sizeof(str
));
398 d_printf("%s\n", str
);
406 static bool wbinfo_get_sidaliases(const char *domain
,
407 const char *user_sid_str
)
409 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
410 struct wbcDomainInfo
*dinfo
= NULL
;
412 struct wbcDomainSid user_sid
;
413 uint32_t *alias_rids
= NULL
;
414 uint32_t num_alias_rids
;
415 char domain_sid_str
[WBC_SID_STRING_BUFLEN
];
418 if ((domain
== NULL
) || (strequal(domain
, ".")) ||
419 (domain
[0] == '\0')) {
420 domain
= get_winbind_domain();
425 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
426 if (!WBC_ERROR_IS_OK(wbc_status
)) {
427 d_fprintf(stderr
, "wbcDomainInfo(%s) failed: %s\n", domain
,
428 wbcErrorString(wbc_status
));
431 wbc_status
= wbcStringToSid(user_sid_str
, &user_sid
);
432 if (!WBC_ERROR_IS_OK(wbc_status
)) {
436 wbc_status
= wbcGetSidAliases(&dinfo
->sid
, &user_sid
, 1,
437 &alias_rids
, &num_alias_rids
);
438 if (!WBC_ERROR_IS_OK(wbc_status
)) {
442 wbcSidToStringBuf(&dinfo
->sid
, domain_sid_str
, sizeof(domain_sid_str
));
444 for (i
= 0; i
< num_alias_rids
; i
++) {
445 d_printf("%s-%d\n", domain_sid_str
, alias_rids
[i
]);
448 wbcFreeMemory(alias_rids
);
451 wbcFreeMemory(dinfo
);
452 return (WBC_ERR_SUCCESS
== wbc_status
);
456 /* Convert NetBIOS name to IP */
458 static bool wbinfo_wins_byname(const char *name
)
460 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
463 wbc_status
= wbcResolveWinsByName(name
, &ip
);
464 if (!WBC_ERROR_IS_OK(wbc_status
)) {
465 d_fprintf(stderr
, "failed to call wbcResolveWinsByName: %s\n",
466 wbcErrorString(wbc_status
));
470 /* Display response */
472 d_printf("%s\n", ip
);
479 /* Convert IP to NetBIOS name */
481 static bool wbinfo_wins_byip(const char *ip
)
483 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
486 wbc_status
= wbcResolveWinsByIP(ip
, &name
);
487 if (!WBC_ERROR_IS_OK(wbc_status
)) {
488 d_fprintf(stderr
, "failed to call wbcResolveWinsByIP: %s\n",
489 wbcErrorString(wbc_status
));
493 /* Display response */
495 d_printf("%s\n", name
);
502 /* List all/trusted domains */
504 static bool wbinfo_list_domains(bool list_all_domains
, bool verbose
)
506 struct wbcDomainInfo
*domain_list
= NULL
;
508 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
509 bool print_all
= !list_all_domains
&& verbose
;
512 wbc_status
= wbcListTrusts(&domain_list
, &num_domains
);
513 if (!WBC_ERROR_IS_OK(wbc_status
)) {
514 d_fprintf(stderr
, "failed to call wbcListTrusts: %s\n",
515 wbcErrorString(wbc_status
));
520 d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n",
521 "Domain Name", "DNS Domain", "Trust Type",
522 "Transitive", "In", "Out");
525 for (i
=0; i
<num_domains
; i
++) {
527 d_printf("%-16s", domain_list
[i
].short_name
);
529 d_printf("%s", domain_list
[i
].short_name
);
534 d_printf("%-24s", domain_list
[i
].dns_name
);
536 switch(domain_list
[i
].trust_type
) {
537 case WBC_DOMINFO_TRUSTTYPE_NONE
:
540 case WBC_DOMINFO_TRUSTTYPE_FOREST
:
543 case WBC_DOMINFO_TRUSTTYPE_EXTERNAL
:
544 d_printf("External ");
546 case WBC_DOMINFO_TRUSTTYPE_IN_FOREST
:
547 d_printf("In-Forest ");
551 if (domain_list
[i
].trust_flags
& WBC_DOMINFO_TRUST_TRANSITIVE
) {
557 if (domain_list
[i
].trust_flags
& WBC_DOMINFO_TRUST_INCOMING
) {
563 if (domain_list
[i
].trust_flags
& WBC_DOMINFO_TRUST_OUTGOING
) {
572 wbcFreeMemory(domain_list
);
577 /* List own domain */
579 static bool wbinfo_list_own_domain(void)
581 d_printf("%s\n", get_winbind_domain());
586 /* show sequence numbers */
587 static bool wbinfo_show_sequence(const char *domain
)
589 d_printf("This command has been deprecated. Please use the "
590 "--online-status option instead.\n");
594 /* show sequence numbers */
595 static bool wbinfo_show_onlinestatus(const char *domain
)
597 struct wbcDomainInfo
*domain_list
= NULL
;
599 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
602 wbc_status
= wbcListTrusts(&domain_list
, &num_domains
);
603 if (!WBC_ERROR_IS_OK(wbc_status
)) {
604 d_fprintf(stderr
, "failed to call wbcListTrusts: %s\n",
605 wbcErrorString(wbc_status
));
609 for (i
=0; i
<num_domains
; i
++) {
613 if (!strequal(domain_list
[i
].short_name
, domain
)) {
618 is_offline
= (domain_list
[i
].domain_flags
&
619 WBC_DOMINFO_DOMAIN_OFFLINE
);
621 d_printf("%s : %s\n",
622 domain_list
[i
].short_name
,
623 is_offline
? "offline" : "online" );
626 wbcFreeMemory(domain_list
);
632 /* Show domain info */
634 static bool wbinfo_domain_info(const char *domain
)
636 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
637 struct wbcDomainInfo
*dinfo
= NULL
;
638 char sid_str
[WBC_SID_STRING_BUFLEN
];
640 if ((domain
== NULL
) || (strequal(domain
, ".")) || (domain
[0] == '\0')){
641 domain
= get_winbind_domain();
646 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
647 if (!WBC_ERROR_IS_OK(wbc_status
)) {
648 d_fprintf(stderr
, "failed to call wbcDomainInfo: %s\n",
649 wbcErrorString(wbc_status
));
653 wbcSidToStringBuf(&dinfo
->sid
, sid_str
, sizeof(sid_str
));
655 /* Display response */
657 d_printf("Name : %s\n", dinfo
->short_name
);
658 d_printf("Alt_Name : %s\n", dinfo
->dns_name
);
660 d_printf("SID : %s\n", sid_str
);
662 d_printf("Active Directory : %s\n",
663 (dinfo
->domain_flags
& WBC_DOMINFO_DOMAIN_AD
) ? "Yes" : "No");
664 d_printf("Native : %s\n",
665 (dinfo
->domain_flags
& WBC_DOMINFO_DOMAIN_NATIVE
) ?
668 d_printf("Primary : %s\n",
669 (dinfo
->domain_flags
& WBC_DOMINFO_DOMAIN_PRIMARY
) ?
672 wbcFreeMemory(dinfo
);
677 /* Get a foreign DC's name */
678 static bool wbinfo_getdcname(const char *domain_name
)
680 struct winbindd_request request
;
681 struct winbindd_response response
;
683 ZERO_STRUCT(request
);
684 ZERO_STRUCT(response
);
686 fstrcpy(request
.domain_name
, domain_name
);
690 if (winbindd_request_response(WINBINDD_GETDCNAME
, &request
,
691 &response
) != NSS_STATUS_SUCCESS
) {
692 d_fprintf(stderr
, "Could not get dc name for %s\n",domain_name
);
696 /* Display response */
698 d_printf("%s\n", response
.data
.dc_name
);
704 static bool wbinfo_dsgetdcname(const char *domain_name
, uint32_t flags
)
706 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
707 struct wbcDomainControllerInfoEx
*dc_info
;
710 wbc_status
= wbcLookupDomainControllerEx(domain_name
, NULL
, NULL
,
711 flags
| DS_DIRECTORY_SERVICE_REQUIRED
,
713 if (!WBC_ERROR_IS_OK(wbc_status
)) {
714 printf("Could not find dc for %s\n", domain_name
);
718 wbcGuidToString(dc_info
->domain_guid
, &str
);
720 d_printf("%s\n", dc_info
->dc_unc
);
721 d_printf("%s\n", dc_info
->dc_address
);
722 d_printf("%d\n", dc_info
->dc_address_type
);
723 d_printf("%s\n", str
);
724 d_printf("%s\n", dc_info
->domain_name
);
725 d_printf("%s\n", dc_info
->forest_name
);
726 d_printf("0x%08x\n", dc_info
->dc_flags
);
727 d_printf("%s\n", dc_info
->dc_site_name
);
728 d_printf("%s\n", dc_info
->client_site_name
);
733 /* Check trust account password */
735 static bool wbinfo_check_secret(const char *domain
)
737 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
738 struct wbcAuthErrorInfo
*error
= NULL
;
739 const char *domain_name
;
742 domain_name
= domain
;
744 domain_name
= get_winbind_domain();
747 wbc_status
= wbcCheckTrustCredentials(domain_name
, &error
);
749 d_printf("checking the trust secret for domain %s via RPC calls %s\n",
751 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
753 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
754 d_fprintf(stderr
, "error code was %s (0x%x)\n",
755 error
->nt_string
, error
->nt_status
);
756 wbcFreeMemory(error
);
758 if (!WBC_ERROR_IS_OK(wbc_status
)) {
759 d_fprintf(stderr
, "failed to call wbcCheckTrustCredentials: "
760 "%s\n", wbcErrorString(wbc_status
));
767 /* Find the currently connected DCs */
769 static bool wbinfo_dc_info(const char *domain_name
)
771 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
773 const char **dc_names
, **dc_ips
;
775 wbc_status
= wbcDcInfo(domain_name
, &num_dcs
,
777 if (!WBC_ERROR_IS_OK(wbc_status
)) {
778 printf("Could not find dc info %s\n",
779 domain_name
? domain_name
: "our domain");
783 for (i
=0; i
<num_dcs
; i
++) {
784 printf("%s (%s)\n", dc_names
[i
], dc_ips
[i
]);
786 wbcFreeMemory(dc_names
);
787 wbcFreeMemory(dc_ips
);
792 /* Change trust account password */
794 static bool wbinfo_change_secret(const char *domain
)
796 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
797 struct wbcAuthErrorInfo
*error
= NULL
;
798 const char *domain_name
;
801 domain_name
= domain
;
803 domain_name
= get_winbind_domain();
806 wbc_status
= wbcChangeTrustCredentials(domain_name
, &error
);
808 d_printf("changing the trust secret for domain %s via RPC calls %s\n",
810 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
812 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
813 d_fprintf(stderr
, "error code was %s (0x%x)\n",
814 error
->nt_string
, error
->nt_status
);
815 wbcFreeMemory(error
);
817 if (!WBC_ERROR_IS_OK(wbc_status
)) {
818 d_fprintf(stderr
, "failed to call wbcChangeTrustCredentials: "
819 "%s\n", wbcErrorString(wbc_status
));
826 /* Check DC connection */
828 static bool wbinfo_ping_dc(void)
830 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
831 struct wbcAuthErrorInfo
*error
= NULL
;
833 wbc_status
= wbcPingDc(NULL
, &error
);
835 d_printf("checking the NETLOGON dc connection %s\n",
836 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
838 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
839 d_fprintf(stderr
, "error code was %s (0x%x)\n",
840 error
->nt_string
, error
->nt_status
);
841 wbcFreeMemory(error
);
843 if (!WBC_ERROR_IS_OK(wbc_status
)) {
844 d_fprintf(stderr
, "failed to call wbcPingDc: %s\n",
845 wbcErrorString(wbc_status
));
852 /* Convert uid to sid */
854 static bool wbinfo_uid_to_sid(uid_t uid
)
856 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
857 struct wbcDomainSid sid
;
858 char sid_str
[WBC_SID_STRING_BUFLEN
];
862 wbc_status
= wbcUidToSid(uid
, &sid
);
863 if (!WBC_ERROR_IS_OK(wbc_status
)) {
864 d_fprintf(stderr
, "failed to call wbcUidToSid: %s\n",
865 wbcErrorString(wbc_status
));
869 wbcSidToStringBuf(&sid
, sid_str
, sizeof(sid_str
));
871 /* Display response */
873 d_printf("%s\n", sid_str
);
878 /* Convert gid to sid */
880 static bool wbinfo_gid_to_sid(gid_t gid
)
882 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
883 struct wbcDomainSid sid
;
884 char sid_str
[WBC_SID_STRING_BUFLEN
];
888 wbc_status
= wbcGidToSid(gid
, &sid
);
889 if (!WBC_ERROR_IS_OK(wbc_status
)) {
890 d_fprintf(stderr
, "failed to call wbcGidToSid: %s\n",
891 wbcErrorString(wbc_status
));
895 wbcSidToStringBuf(&sid
, sid_str
, sizeof(sid_str
));
897 /* Display response */
899 d_printf("%s\n", sid_str
);
904 /* Convert sid to uid */
906 static bool wbinfo_sid_to_uid(const char *sid_str
)
908 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
909 struct wbcDomainSid sid
;
914 wbc_status
= wbcStringToSid(sid_str
, &sid
);
915 if (!WBC_ERROR_IS_OK(wbc_status
)) {
916 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
917 wbcErrorString(wbc_status
));
921 wbc_status
= wbcSidToUid(&sid
, &uid
);
922 if (!WBC_ERROR_IS_OK(wbc_status
)) {
923 d_fprintf(stderr
, "failed to call wbcSidToUid: %s\n",
924 wbcErrorString(wbc_status
));
928 /* Display response */
930 d_printf("%d\n", (int)uid
);
935 static bool wbinfo_sid_to_gid(const char *sid_str
)
937 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
938 struct wbcDomainSid sid
;
943 wbc_status
= wbcStringToSid(sid_str
, &sid
);
944 if (!WBC_ERROR_IS_OK(wbc_status
)) {
945 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
946 wbcErrorString(wbc_status
));
950 wbc_status
= wbcSidToGid(&sid
, &gid
);
951 if (!WBC_ERROR_IS_OK(wbc_status
)) {
952 d_fprintf(stderr
, "failed to call wbcSidToGid: %s\n",
953 wbcErrorString(wbc_status
));
957 /* Display response */
959 d_printf("%d\n", (int)gid
);
964 static bool wbinfo_allocate_uid(void)
966 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
971 wbc_status
= wbcAllocateUid(&uid
);
972 if (!WBC_ERROR_IS_OK(wbc_status
)) {
973 d_fprintf(stderr
, "failed to call wbcAllocateUid: %s\n",
974 wbcErrorString(wbc_status
));
978 /* Display response */
980 d_printf("New uid: %u\n", (unsigned int)uid
);
985 static bool wbinfo_allocate_gid(void)
987 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
992 wbc_status
= wbcAllocateGid(&gid
);
993 if (!WBC_ERROR_IS_OK(wbc_status
)) {
994 d_fprintf(stderr
, "failed to call wbcAllocateGid: %s\n",
995 wbcErrorString(wbc_status
));
999 /* Display response */
1001 d_printf("New gid: %u\n", (unsigned int)gid
);
1006 static bool wbinfo_set_uid_mapping(uid_t uid
, const char *sid_str
)
1008 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1009 struct wbcDomainSid sid
;
1013 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1014 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1015 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1016 wbcErrorString(wbc_status
));
1020 wbc_status
= wbcSetUidMapping(uid
, &sid
);
1021 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1022 d_fprintf(stderr
, "failed to call wbcSetUidMapping: %s\n",
1023 wbcErrorString(wbc_status
));
1027 /* Display response */
1029 d_printf("uid %u now mapped to sid %s\n",
1030 (unsigned int)uid
, sid_str
);
1035 static bool wbinfo_set_gid_mapping(gid_t gid
, const char *sid_str
)
1037 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1038 struct wbcDomainSid sid
;
1042 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1043 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1044 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1045 wbcErrorString(wbc_status
));
1049 wbc_status
= wbcSetGidMapping(gid
, &sid
);
1050 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1051 d_fprintf(stderr
, "failed to call wbcSetGidMapping: %s\n",
1052 wbcErrorString(wbc_status
));
1056 /* Display response */
1058 d_printf("gid %u now mapped to sid %s\n",
1059 (unsigned int)gid
, sid_str
);
1064 static bool wbinfo_remove_uid_mapping(uid_t uid
, const char *sid_str
)
1066 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1067 struct wbcDomainSid sid
;
1071 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1072 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1073 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1074 wbcErrorString(wbc_status
));
1078 wbc_status
= wbcRemoveUidMapping(uid
, &sid
);
1079 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1080 d_fprintf(stderr
, "failed to call wbcRemoveUidMapping: %s\n",
1081 wbcErrorString(wbc_status
));
1085 /* Display response */
1087 d_printf("Removed uid %u to sid %s mapping\n",
1088 (unsigned int)uid
, sid_str
);
1093 static bool wbinfo_remove_gid_mapping(gid_t gid
, const char *sid_str
)
1095 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1096 struct wbcDomainSid sid
;
1100 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1101 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1102 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1103 wbcErrorString(wbc_status
));
1107 wbc_status
= wbcRemoveGidMapping(gid
, &sid
);
1108 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1109 d_fprintf(stderr
, "failed to call wbcRemoveGidMapping: %s\n",
1110 wbcErrorString(wbc_status
));
1114 /* Display response */
1116 d_printf("Removed gid %u to sid %s mapping\n",
1117 (unsigned int)gid
, sid_str
);
1122 /* Convert sid to string */
1124 static bool wbinfo_lookupsid(const char *sid_str
)
1126 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1127 struct wbcDomainSid sid
;
1130 enum wbcSidType type
;
1132 /* Send off request */
1134 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1135 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1136 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1137 wbcErrorString(wbc_status
));
1141 wbc_status
= wbcLookupSid(&sid
, &domain
, &name
, &type
);
1142 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1143 d_fprintf(stderr
, "failed to call wbcLookupSid: %s\n",
1144 wbcErrorString(wbc_status
));
1148 /* Display response */
1150 d_printf("%s%c%s %d\n",
1151 domain
, winbind_separator(), name
, type
);
1156 /* Convert sid to fullname */
1158 static bool wbinfo_lookupsid_fullname(const char *sid_str
)
1160 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1161 struct wbcDomainSid sid
;
1164 enum wbcSidType type
;
1166 /* Send off request */
1168 wbc_status
= wbcStringToSid(sid_str
, &sid
);
1169 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1170 d_fprintf(stderr
, "failed to call wbcStringToSid: %s\n",
1171 wbcErrorString(wbc_status
));
1175 wbc_status
= wbcGetDisplayName(&sid
, &domain
, &name
, &type
);
1176 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1177 d_fprintf(stderr
, "failed to call wbcGetDisplayName: %s\n",
1178 wbcErrorString(wbc_status
));
1182 /* Display response */
1184 d_printf("%s%c%s %d\n",
1185 domain
, winbind_separator(), name
, type
);
1190 /* Lookup a list of RIDs */
1192 static bool wbinfo_lookuprids(const char *domain
, const char *arg
)
1194 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1195 struct wbcDomainInfo
*dinfo
= NULL
;
1196 char *domain_name
= NULL
;
1197 const char **names
= NULL
;
1198 enum wbcSidType
*types
= NULL
;
1201 uint32_t *rids
= NULL
;
1204 TALLOC_CTX
*mem_ctx
= NULL
;
1207 if ((domain
== NULL
) || (strequal(domain
, ".")) || (domain
[0] == '\0')){
1208 domain
= get_winbind_domain();
1213 wbc_status
= wbcDomainInfo(domain
, &dinfo
);
1214 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1215 d_printf("wbcDomainInfo(%s) failed: %s\n", domain
,
1216 wbcErrorString(wbc_status
));
1220 mem_ctx
= talloc_new(NULL
);
1221 if (mem_ctx
== NULL
) {
1222 d_printf("talloc_new failed\n");
1230 while (next_token_talloc(mem_ctx
, &p
, &ridstr
, " ,\n")) {
1231 uint32_t rid
= strtoul(ridstr
, NULL
, 10);
1232 rids
= talloc_realloc(mem_ctx
, rids
, uint32_t, num_rids
+ 1);
1234 d_printf("talloc_realloc failed\n");
1236 rids
[num_rids
] = rid
;
1241 d_printf("no rids\n");
1245 wbc_status
= wbcLookupRids(&dinfo
->sid
, num_rids
, rids
,
1246 (const char **)&domain_name
, &names
, &types
);
1247 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1248 d_printf("winbind_lookup_rids failed: %s\n",
1249 wbcErrorString(wbc_status
));
1253 d_printf("Domain: %s\n", domain_name
);
1255 for (i
=0; i
<num_rids
; i
++) {
1256 d_printf("%8d: %s (%s)\n", rids
[i
], names
[i
],
1257 wbcSidTypeString(types
[i
]));
1262 wbcFreeMemory(dinfo
);
1263 wbcFreeMemory(domain_name
);
1264 wbcFreeMemory(names
);
1265 wbcFreeMemory(types
);
1266 TALLOC_FREE(mem_ctx
);
1270 /* Convert string to sid */
1272 static bool wbinfo_lookupname(const char *full_name
)
1274 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1275 struct wbcDomainSid sid
;
1276 char sid_str
[WBC_SID_STRING_BUFLEN
];
1277 enum wbcSidType type
;
1278 fstring domain_name
;
1279 fstring account_name
;
1281 /* Send off request */
1283 parse_wbinfo_domain_user(full_name
, domain_name
,
1286 wbc_status
= wbcLookupName(domain_name
, account_name
,
1288 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1289 d_fprintf(stderr
, "failed to call wbcLookupName: %s\n",
1290 wbcErrorString(wbc_status
));
1294 wbcSidToStringBuf(&sid
, sid_str
, sizeof(sid_str
));
1296 /* Display response */
1298 d_printf("%s %s (%d)\n", sid_str
, wbcSidTypeString(type
), type
);
1303 static char *wbinfo_prompt_pass(TALLOC_CTX
*mem_ctx
,
1305 const char *username
)
1308 const char *ret
= NULL
;
1310 prompt
= talloc_asprintf(mem_ctx
, "Enter %s's ", username
);
1315 prompt
= talloc_asprintf_append(prompt
, "%s ", prefix
);
1320 prompt
= talloc_asprintf_append(prompt
, "password: ");
1325 ret
= getpass(prompt
);
1326 TALLOC_FREE(prompt
);
1328 return talloc_strdup(mem_ctx
, ret
);
1331 /* Authenticate a user with a plaintext password */
1333 static bool wbinfo_auth_krb5(char *username
, const char *cctype
, uint32_t flags
)
1335 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1338 char *password
= NULL
;
1340 char *local_cctype
= NULL
;
1342 struct wbcLogonUserParams params
;
1343 struct wbcLogonUserInfo
*info
;
1344 struct wbcAuthErrorInfo
*error
;
1345 struct wbcUserPasswordPolicyInfo
*policy
;
1346 TALLOC_CTX
*frame
= talloc_tos();
1348 if ((s
= talloc_strdup(frame
, username
)) == NULL
) {
1352 if ((p
= strchr(s
, '%')) != NULL
) {
1355 password
= talloc_strdup(frame
, p
);
1357 password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1360 local_cctype
= talloc_strdup(frame
, cctype
);
1366 params
.username
= name
;
1367 params
.password
= password
;
1368 params
.num_blobs
= 0;
1369 params
.blobs
= NULL
;
1371 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
,
1377 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1378 d_fprintf(stderr
, "failed to call wbcAddNamedBlob: %s\n",
1379 wbcErrorString(wbc_status
));
1383 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
,
1389 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1390 d_fprintf(stderr
, "failed to call wbcAddNamedBlob: %s\n",
1391 wbcErrorString(wbc_status
));
1395 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
,
1399 (uint8_t *)local_cctype
,
1401 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1402 d_fprintf(stderr
, "failed to call wbcAddNamedBlob: %s\n",
1403 wbcErrorString(wbc_status
));
1407 wbc_status
= wbcLogonUser(¶ms
, &info
, &error
, &policy
);
1409 d_printf("plaintext kerberos password authentication for [%s] %s "
1410 "(requesting cctype: %s)\n",
1411 username
, WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed",
1416 "error code was %s (0x%x)\nerror message was: %s\n",
1419 error
->display_string
);
1422 if (WBC_ERROR_IS_OK(wbc_status
)) {
1423 if (flags
& WBFLAG_PAM_INFO3_TEXT
) {
1424 if (info
&& info
->info
&& info
->info
->user_flags
&
1425 NETLOGON_CACHED_ACCOUNT
) {
1426 d_printf("user_flgs: "
1427 "NETLOGON_CACHED_ACCOUNT\n");
1433 for (i
=0; i
< info
->num_blobs
; i
++) {
1434 if (strequal(info
->blobs
[i
].name
,
1436 d_printf("credentials were put "
1439 info
->blobs
[i
].blob
.data
);
1444 d_printf("no credentials cached\n");
1449 wbcFreeMemory(params
.blobs
);
1451 return WBC_ERROR_IS_OK(wbc_status
);
1454 /* Authenticate a user with a plaintext password */
1456 static bool wbinfo_auth(char *username
)
1458 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1461 char *password
= NULL
;
1463 TALLOC_CTX
*frame
= talloc_tos();
1465 if ((s
= talloc_strdup(frame
, username
)) == NULL
) {
1469 if ((p
= strchr(s
, '%')) != NULL
) {
1472 password
= talloc_strdup(frame
, p
);
1474 password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1479 wbc_status
= wbcAuthenticateUser(name
, password
);
1481 d_printf("plaintext password authentication %s\n",
1482 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1485 if (response
.data
.auth
.nt_status
)
1487 "error code was %s (0x%x)\nerror message was: %s\n",
1488 response
.data
.auth
.nt_status_string
,
1489 response
.data
.auth
.nt_status
,
1490 response
.data
.auth
.error_string
);
1493 return WBC_ERROR_IS_OK(wbc_status
);
1496 /* Authenticate a user with a challenge/response */
1498 static bool wbinfo_auth_crap(char *username
, bool use_ntlmv2
, bool use_lanman
)
1500 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1501 struct wbcAuthUserParams params
;
1502 struct wbcAuthUserInfo
*info
= NULL
;
1503 struct wbcAuthErrorInfo
*err
= NULL
;
1504 DATA_BLOB lm
= data_blob_null
;
1505 DATA_BLOB nt
= data_blob_null
;
1507 fstring name_domain
;
1510 TALLOC_CTX
*frame
= talloc_tos();
1512 p
= strchr(username
, '%');
1516 pass
= talloc_strdup(frame
, p
+ 1);
1518 pass
= wbinfo_prompt_pass(frame
, NULL
, username
);
1521 parse_wbinfo_domain_user(username
, name_domain
, name_user
);
1523 params
.account_name
= name_user
;
1524 params
.domain_name
= name_domain
;
1525 params
.workstation_name
= NULL
;
1528 params
.parameter_control
= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
|
1529 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
;
1531 params
.level
= WBC_AUTH_USER_LEVEL_RESPONSE
;
1533 generate_random_buffer(params
.password
.response
.challenge
, 8);
1536 DATA_BLOB server_chal
;
1537 DATA_BLOB names_blob
;
1539 server_chal
= data_blob(params
.password
.response
.challenge
, 8);
1541 /* Pretend this is a login to 'us', for blob purposes */
1542 names_blob
= NTLMv2_generate_names_blob(NULL
,
1543 get_winbind_netbios_name(),
1544 get_winbind_domain());
1546 if (!SMBNTLMv2encrypt(NULL
, name_user
, name_domain
, pass
,
1549 &lm
, &nt
, NULL
, NULL
)) {
1550 data_blob_free(&names_blob
);
1551 data_blob_free(&server_chal
);
1555 data_blob_free(&names_blob
);
1556 data_blob_free(&server_chal
);
1561 lm
= data_blob(NULL
, 24);
1562 ok
= SMBencrypt(pass
,
1563 params
.password
.response
.challenge
,
1566 data_blob_free(&lm
);
1569 nt
= data_blob(NULL
, 24);
1570 SMBNTencrypt(pass
, params
.password
.response
.challenge
,
1574 params
.password
.response
.nt_length
= nt
.length
;
1575 params
.password
.response
.nt_data
= nt
.data
;
1576 params
.password
.response
.lm_length
= lm
.length
;
1577 params
.password
.response
.lm_data
= lm
.data
;
1579 wbc_status
= wbcAuthenticateUserEx(¶ms
, &info
, &err
);
1581 /* Display response */
1583 d_printf("challenge/response password authentication %s\n",
1584 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1586 if (wbc_status
== WBC_ERR_AUTH_ERROR
) {
1588 "error code was %s (0x%x)\nerror message was: %s\n",
1591 err
->display_string
);
1593 } else if (WBC_ERROR_IS_OK(wbc_status
)) {
1594 wbcFreeMemory(info
);
1597 data_blob_free(&nt
);
1598 data_blob_free(&lm
);
1600 return WBC_ERROR_IS_OK(wbc_status
);
1603 /* Authenticate a user with a plaintext password */
1605 static bool wbinfo_pam_logon(char *username
)
1607 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1608 struct wbcLogonUserParams params
;
1609 struct wbcAuthErrorInfo
*error
;
1612 TALLOC_CTX
*frame
= talloc_tos();
1616 ZERO_STRUCT(params
);
1618 if ((s
= talloc_strdup(frame
, username
)) == NULL
) {
1622 if ((p
= strchr(s
, '%')) != NULL
) {
1625 params
.password
= talloc_strdup(frame
, p
);
1627 params
.password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1629 params
.username
= s
;
1631 flags
= WBFLAG_PAM_CACHED_LOGIN
;
1633 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
, ¶ms
.blobs
,
1635 (uint8_t *)&flags
, sizeof(flags
));
1636 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1637 d_printf("wbcAddNamedBlob failed: %s\n",
1638 wbcErrorString(wbc_status
));
1644 wbc_status
= wbcAddNamedBlob(¶ms
.num_blobs
, ¶ms
.blobs
,
1646 (uint8_t *)&uid
, sizeof(uid
));
1647 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1648 d_printf("wbcAddNamedBlob failed: %s\n",
1649 wbcErrorString(wbc_status
));
1653 wbc_status
= wbcLogonUser(¶ms
, NULL
, &error
, NULL
);
1655 wbcFreeMemory(params
.blobs
);
1657 d_printf("plaintext password authentication %s\n",
1658 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1660 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1662 "error code was %s (0x%x)\nerror message was: %s\n",
1664 (int)error
->nt_status
,
1665 error
->display_string
);
1666 wbcFreeMemory(error
);
1672 /* Save creds with winbind */
1674 static bool wbinfo_ccache_save(char *username
)
1676 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1679 char *password
= NULL
;
1681 TALLOC_CTX
*frame
= talloc_stackframe();
1683 s
= talloc_strdup(frame
, username
);
1692 password
= talloc_strdup(frame
, p
);
1694 password
= wbinfo_prompt_pass(frame
, NULL
, username
);
1699 wbc_status
= wbcCredentialSave(name
, password
);
1701 d_printf("saving creds %s\n",
1702 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1706 return WBC_ERROR_IS_OK(wbc_status
);
1709 #ifdef WITH_FAKE_KASERVER
1710 /* Authenticate a user with a plaintext password and set a token */
1712 static bool wbinfo_klog(char *username
)
1714 struct winbindd_request request
;
1715 struct winbindd_response response
;
1719 /* Send off request */
1721 ZERO_STRUCT(request
);
1722 ZERO_STRUCT(response
);
1724 p
= strchr(username
, '%');
1728 fstrcpy(request
.data
.auth
.user
, username
);
1729 fstrcpy(request
.data
.auth
.pass
, p
+ 1);
1732 fstrcpy(request
.data
.auth
.user
, username
);
1733 fstrcpy(request
.data
.auth
.pass
, getpass("Password: "));
1736 request
.flags
|= WBFLAG_PAM_AFS_TOKEN
;
1738 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
,
1741 /* Display response */
1743 d_printf("plaintext password authentication %s\n",
1744 (result
== NSS_STATUS_SUCCESS
) ? "succeeded" : "failed");
1746 if (response
.data
.auth
.nt_status
)
1748 "error code was %s (0x%x)\nerror message was: %s\n",
1749 response
.data
.auth
.nt_status_string
,
1750 response
.data
.auth
.nt_status
,
1751 response
.data
.auth
.error_string
);
1753 if (result
!= NSS_STATUS_SUCCESS
)
1756 if (response
.extra_data
.data
== NULL
) {
1757 d_fprintf(stderr
, "Did not get token data\n");
1761 if (!afs_settoken_str((char *)response
.extra_data
.data
)) {
1762 d_fprintf(stderr
, "Could not set token\n");
1766 d_printf("Successfully created AFS token\n");
1770 static bool wbinfo_klog(char *username
)
1772 d_fprintf(stderr
, "No AFS support compiled in.\n");
1777 /* Print domain users */
1779 static bool print_domain_users(const char *domain
)
1781 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1783 uint32_t num_users
= 0;
1784 const char **users
= NULL
;
1786 /* Send request to winbind daemon */
1788 /* '.' is the special sign for our own domain */
1789 if (domain
&& strcmp(domain
, ".") == 0) {
1790 domain
= get_winbind_domain();
1793 wbc_status
= wbcListUsers(domain
, &num_users
, &users
);
1794 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1798 for (i
=0; i
< num_users
; i
++) {
1799 d_printf("%s\n", users
[i
]);
1802 wbcFreeMemory(users
);
1807 /* Print domain groups */
1809 static bool print_domain_groups(const char *domain
)
1811 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1813 uint32_t num_groups
= 0;
1814 const char **groups
= NULL
;
1816 /* Send request to winbind daemon */
1818 /* '.' is the special sign for our own domain */
1819 if (domain
&& strcmp(domain
, ".") == 0) {
1820 domain
= get_winbind_domain();
1823 wbc_status
= wbcListGroups(domain
, &num_groups
, &groups
);
1824 if (!WBC_ERROR_IS_OK(wbc_status
)) {
1825 d_fprintf(stderr
, "failed to call wbcListGroups: %s\n",
1826 wbcErrorString(wbc_status
));
1830 for (i
=0; i
< num_groups
; i
++) {
1831 d_printf("%s\n", groups
[i
]);
1834 wbcFreeMemory(groups
);
1839 /* Set the authorised user for winbindd access in secrets.tdb */
1841 static bool wbinfo_set_auth_user(char *username
)
1843 d_fprintf(stderr
, "This functionality was moved to the 'net' utility.\n"
1844 "See 'net help setauthuser' for details.\n");
1848 static void wbinfo_get_auth_user(void)
1850 d_fprintf(stderr
, "This functionality was moved to the 'net' utility.\n"
1851 "See 'net help getauthuser' for details.\n");
1854 static bool wbinfo_ping(void)
1858 wbc_status
= wbcPing();
1860 /* Display response */
1862 d_printf("Ping to winbindd %s\n",
1863 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1865 return WBC_ERROR_IS_OK(wbc_status
);
1868 static bool wbinfo_change_user_password(const char *username
)
1871 char *old_password
= NULL
;
1872 char *new_password
= NULL
;
1873 TALLOC_CTX
*frame
= talloc_tos();
1875 old_password
= wbinfo_prompt_pass(frame
, "old", username
);
1876 new_password
= wbinfo_prompt_pass(frame
, "new", username
);
1878 wbc_status
= wbcChangeUserPassword(username
, old_password
,new_password
);
1880 /* Display response */
1882 d_printf("Password change for user %s %s\n", username
,
1883 WBC_ERROR_IS_OK(wbc_status
) ? "succeeded" : "failed");
1885 return WBC_ERROR_IS_OK(wbc_status
);
1891 OPT_SET_AUTH_USER
= 1000,
1903 OPT_SET_UID_MAPPING
,
1904 OPT_SET_GID_MAPPING
,
1905 OPT_REMOVE_UID_MAPPING
,
1906 OPT_REMOVE_GID_MAPPING
,
1908 OPT_LIST_ALL_DOMAINS
,
1909 OPT_LIST_OWN_DOMAIN
,
1916 OPT_CHANGE_USER_PASSWORD
,
1918 OPT_SID_TO_FULLNAME
,
1927 int main(int argc
, char **argv
, char **envp
)
1930 TALLOC_CTX
*frame
= talloc_stackframe();
1932 static char *string_arg
;
1933 char *string_subarg
= NULL
;
1934 static char *opt_domain_name
;
1936 int int_subarg
= -1;
1938 bool verbose
= false;
1939 bool use_ntlmv2
= false;
1940 bool use_lanman
= false;
1941 char *logoff_user
= getenv("USER");
1942 int logoff_uid
= geteuid();
1944 struct poptOption long_options
[] = {
1947 /* longName, shortName, argInfo, argPtr, value, descrip,
1950 { "domain-users", 'u', POPT_ARG_NONE
, 0, 'u', "Lists all domain users", "domain"},
1951 { "domain-groups", 'g', POPT_ARG_NONE
, 0, 'g', "Lists all domain groups", "domain" },
1952 { "WINS-by-name", 'N', POPT_ARG_STRING
, &string_arg
, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1953 { "WINS-by-ip", 'I', POPT_ARG_STRING
, &string_arg
, 'I', "Converts IP address to NetBIOS name", "IP" },
1954 { "name-to-sid", 'n', POPT_ARG_STRING
, &string_arg
, 'n', "Converts name to sid", "NAME" },
1955 { "sid-to-name", 's', POPT_ARG_STRING
, &string_arg
, 's', "Converts sid to name", "SID" },
1956 { "sid-to-fullname", 0, POPT_ARG_STRING
, &string_arg
,
1957 OPT_SID_TO_FULLNAME
, "Converts sid to fullname", "SID" },
1958 { "lookup-rids", 'R', POPT_ARG_STRING
, &string_arg
, 'R', "Converts RIDs to names", "RIDs" },
1959 { "uid-to-sid", 'U', POPT_ARG_INT
, &int_arg
, 'U', "Converts uid to sid" , "UID" },
1960 { "gid-to-sid", 'G', POPT_ARG_INT
, &int_arg
, 'G', "Converts gid to sid", "GID" },
1961 { "sid-to-uid", 'S', POPT_ARG_STRING
, &string_arg
, 'S', "Converts sid to uid", "SID" },
1962 { "sid-to-gid", 'Y', POPT_ARG_STRING
, &string_arg
, 'Y', "Converts sid to gid", "SID" },
1963 { "allocate-uid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_UID
,
1964 "Get a new UID out of idmap" },
1965 { "allocate-gid", 0, POPT_ARG_NONE
, 0, OPT_ALLOCATE_GID
,
1966 "Get a new GID out of idmap" },
1967 { "set-uid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_UID_MAPPING
, "Create or modify uid to sid mapping in idmap", "UID,SID" },
1968 { "set-gid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_GID_MAPPING
, "Create or modify gid to sid mapping in idmap", "GID,SID" },
1969 { "remove-uid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_REMOVE_UID_MAPPING
, "Remove uid to sid mapping in idmap", "UID,SID" },
1970 { "remove-gid-mapping", 0, POPT_ARG_STRING
, &string_arg
, OPT_REMOVE_GID_MAPPING
, "Remove gid to sid mapping in idmap", "GID,SID" },
1971 { "check-secret", 't', POPT_ARG_NONE
, 0, 't', "Check shared secret" },
1972 { "change-secret", 'c', POPT_ARG_NONE
, 0, 'c', "Change shared secret" },
1973 { "ping-dc", 'P', POPT_ARG_NONE
, 0, 'P',
1974 "Check the NETLOGON connection" },
1975 { "trusted-domains", 'm', POPT_ARG_NONE
, 0, 'm', "List trusted domains" },
1976 { "all-domains", 0, POPT_ARG_NONE
, 0, OPT_LIST_ALL_DOMAINS
, "List all domains (trusted and own domain)" },
1977 { "own-domain", 0, POPT_ARG_NONE
, 0, OPT_LIST_OWN_DOMAIN
, "List own domain" },
1978 { "sequence", 0, POPT_ARG_NONE
, 0, OPT_SEQUENCE
, "Deprecated command, see --online-status" },
1979 { "online-status", 0, POPT_ARG_NONE
, 0, OPT_ONLINESTATUS
, "Show whether domains are marked as online or offline"},
1980 { "domain-info", 'D', POPT_ARG_STRING
, &string_arg
, 'D', "Show most of the info we have about the domain" },
1981 { "user-info", 'i', POPT_ARG_STRING
, &string_arg
, 'i', "Get user info", "USER" },
1982 { "uid-info", 0, POPT_ARG_INT
, &int_arg
, OPT_UID_INFO
, "Get user info from uid", "UID" },
1983 { "group-info", 0, POPT_ARG_STRING
, &string_arg
, OPT_GROUP_INFO
, "Get group info", "GROUP" },
1984 { "user-sidinfo", 0, POPT_ARG_STRING
, &string_arg
, OPT_USER_SIDINFO
, "Get user info from sid", "SID" },
1985 { "gid-info", 0, POPT_ARG_INT
, &int_arg
, OPT_GID_INFO
, "Get group info from gid", "GID" },
1986 { "user-groups", 'r', POPT_ARG_STRING
, &string_arg
, 'r', "Get user groups", "USER" },
1987 { "user-domgroups", 0, POPT_ARG_STRING
, &string_arg
,
1988 OPT_USERDOMGROUPS
, "Get user domain groups", "SID" },
1989 { "sid-aliases", 0, POPT_ARG_STRING
, &string_arg
, OPT_SIDALIASES
, "Get sid aliases", "SID" },
1990 { "user-sids", 0, POPT_ARG_STRING
, &string_arg
, OPT_USERSIDS
, "Get user group sids for user SID", "SID" },
1991 { "authenticate", 'a', POPT_ARG_STRING
, &string_arg
, 'a', "authenticate user", "user%password" },
1992 { "pam-logon", 0, POPT_ARG_STRING
, &string_arg
, OPT_PAM_LOGON
,
1993 "do a pam logon equivalent", "user%password" },
1994 { "logoff", 0, POPT_ARG_NONE
, NULL
, OPT_LOGOFF
,
1995 "log off user", "uid" },
1996 { "logoff-user", 0, POPT_ARG_STRING
, &logoff_user
,
1997 OPT_LOGOFF_USER
, "username to log off" },
1998 { "logoff-uid", 0, POPT_ARG_INT
, &logoff_uid
,
1999 OPT_LOGOFF_UID
, "uid to log off" },
2000 { "set-auth-user", 0, POPT_ARG_STRING
, &string_arg
, OPT_SET_AUTH_USER
, "Store user and password used by winbindd (root only)", "user%password" },
2001 { "ccache-save", 0, POPT_ARG_STRING
, &string_arg
,
2002 OPT_CCACHE_SAVE
, "Store user and password for ccache "
2003 "operation", "user%password" },
2004 { "getdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_GETDCNAME
,
2005 "Get a DC name for a foreign domain", "domainname" },
2006 { "dsgetdcname", 0, POPT_ARG_STRING
, &string_arg
, OPT_DSGETDCNAME
, "Find a DC for a domain", "domainname" },
2007 { "dc-info", 0, POPT_ARG_STRING
, &string_arg
, OPT_DC_INFO
,
2008 "Find the currently known DCs", "domainname" },
2009 { "get-auth-user", 0, POPT_ARG_NONE
, NULL
, OPT_GET_AUTH_USER
, "Retrieve user and password used by winbindd (root only)", NULL
},
2010 { "ping", 'p', POPT_ARG_NONE
, 0, 'p', "Ping winbindd to see if it is alive" },
2011 { "domain", 0, POPT_ARG_STRING
, &opt_domain_name
, OPT_DOMAIN_NAME
, "Define to the domain to restrict operation", "domain" },
2012 #ifdef WITH_FAKE_KASERVER
2013 { "klog", 'k', POPT_ARG_STRING
, &string_arg
, 'k', "set an AFS token from winbind", "user%password" },
2016 { "krb5auth", 'K', POPT_ARG_STRING
, &string_arg
, 'K', "authenticate user using Kerberos", "user%password" },
2017 /* destroys wbinfo --help output */
2018 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
2020 { "separator", 0, POPT_ARG_NONE
, 0, OPT_SEPARATOR
, "Get the active winbind separator", NULL
},
2021 { "verbose", 0, POPT_ARG_NONE
, 0, OPT_VERBOSE
, "Print additional information per command", NULL
},
2022 { "change-user-password", 0, POPT_ARG_STRING
, &string_arg
, OPT_CHANGE_USER_PASSWORD
, "Change the password for a user", NULL
},
2023 { "ntlmv2", 0, POPT_ARG_NONE
, 0, OPT_NTLMV2
, "Use NTLMv2 cryptography for user authentication", NULL
},
2024 { "lanman", 0, POPT_ARG_NONE
, 0, OPT_LANMAN
, "Use lanman cryptography for user authentication", NULL
},
2029 /* Samba client initialisation */
2035 pc
= poptGetContext("wbinfo", argc
, (const char **)argv
,
2038 /* Parse command line options */
2041 poptPrintHelp(pc
, stderr
, 0);
2045 while((opt
= poptGetNextOpt(pc
)) != -1) {
2046 /* get the generic configuration parameters like --domain */
2060 poptFreeContext(pc
);
2062 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
2063 POPT_CONTEXT_KEEP_FIRST
);
2065 while((opt
= poptGetNextOpt(pc
)) != -1) {
2068 if (!print_domain_users(opt_domain_name
)) {
2070 "Error looking up domain users\n");
2075 if (!print_domain_groups(opt_domain_name
)) {
2077 "Error looking up domain groups\n");
2082 if (!wbinfo_lookupsid(string_arg
)) {
2084 "Could not lookup sid %s\n",
2089 case OPT_SID_TO_FULLNAME
:
2090 if (!wbinfo_lookupsid_fullname(string_arg
)) {
2091 d_fprintf(stderr
, "Could not lookup sid %s\n",
2097 if (!wbinfo_lookuprids(opt_domain_name
, string_arg
)) {
2098 d_fprintf(stderr
, "Could not lookup RIDs %s\n",
2104 if (!wbinfo_lookupname(string_arg
)) {
2105 d_fprintf(stderr
, "Could not lookup name %s\n",
2111 if (!wbinfo_wins_byname(string_arg
)) {
2113 "Could not lookup WINS by name %s\n",
2119 if (!wbinfo_wins_byip(string_arg
)) {
2121 "Could not lookup WINS by IP %s\n",
2127 if (!wbinfo_uid_to_sid(int_arg
)) {
2129 "Could not convert uid %d to sid\n",
2135 if (!wbinfo_gid_to_sid(int_arg
)) {
2137 "Could not convert gid %d to sid\n",
2143 if (!wbinfo_sid_to_uid(string_arg
)) {
2145 "Could not convert sid %s to uid\n",
2151 if (!wbinfo_sid_to_gid(string_arg
)) {
2153 "Could not convert sid %s to gid\n",
2158 case OPT_ALLOCATE_UID
:
2159 if (!wbinfo_allocate_uid()) {
2160 d_fprintf(stderr
, "Could not allocate a uid\n");
2164 case OPT_ALLOCATE_GID
:
2165 if (!wbinfo_allocate_gid()) {
2166 d_fprintf(stderr
, "Could not allocate a gid\n");
2170 case OPT_SET_UID_MAPPING
:
2171 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2173 !wbinfo_set_uid_mapping(int_subarg
, string_subarg
))
2175 d_fprintf(stderr
, "Could not create or modify "
2176 "uid to sid mapping\n");
2180 case OPT_SET_GID_MAPPING
:
2181 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2183 !wbinfo_set_gid_mapping(int_subarg
, string_subarg
))
2185 d_fprintf(stderr
, "Could not create or modify "
2186 "gid to sid mapping\n");
2190 case OPT_REMOVE_UID_MAPPING
:
2191 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2193 !wbinfo_remove_uid_mapping(int_subarg
,
2196 d_fprintf(stderr
, "Could not remove uid to sid "
2201 case OPT_REMOVE_GID_MAPPING
:
2202 if (!parse_mapping_arg(string_arg
, &int_subarg
,
2204 !wbinfo_remove_gid_mapping(int_subarg
,
2207 d_fprintf(stderr
, "Could not remove gid to sid "
2213 if (!wbinfo_check_secret(opt_domain_name
)) {
2214 d_fprintf(stderr
, "Could not check secret\n");
2219 if (!wbinfo_change_secret(opt_domain_name
)) {
2220 d_fprintf(stderr
, "Could not change secret\n");
2225 if (!wbinfo_ping_dc()) {
2226 d_fprintf(stderr
, "Could not ping our DC\n");
2231 if (!wbinfo_list_domains(false, verbose
)) {
2233 "Could not list trusted domains\n");
2238 if (!wbinfo_show_sequence(opt_domain_name
)) {
2240 "Could not show sequence numbers\n");
2244 case OPT_ONLINESTATUS
:
2245 if (!wbinfo_show_onlinestatus(opt_domain_name
)) {
2247 "Could not show online-status\n");
2252 if (!wbinfo_domain_info(string_arg
)) {
2254 "Could not get domain info\n");
2259 if (!wbinfo_get_userinfo(string_arg
)) {
2261 "Could not get info for user %s\n",
2266 case OPT_USER_SIDINFO
:
2267 if ( !wbinfo_get_user_sidinfo(string_arg
)) {
2269 "Could not get info for user "
2270 "sid %s\n", string_arg
);
2275 if ( !wbinfo_get_uidinfo(int_arg
)) {
2276 d_fprintf(stderr
, "Could not get info for uid "
2281 case OPT_GROUP_INFO
:
2282 if ( !wbinfo_get_groupinfo(string_arg
)) {
2283 d_fprintf(stderr
, "Could not get info for "
2284 "group %s\n", string_arg
);
2289 if ( !wbinfo_get_gidinfo(int_arg
)) {
2290 d_fprintf(stderr
, "Could not get info for gid "
2296 if (!wbinfo_get_usergroups(string_arg
)) {
2298 "Could not get groups for user %s\n",
2304 if (!wbinfo_get_usersids(string_arg
)) {
2305 d_fprintf(stderr
, "Could not get group SIDs "
2306 "for user SID %s\n",
2311 case OPT_USERDOMGROUPS
:
2312 if (!wbinfo_get_userdomgroups(string_arg
)) {
2313 d_fprintf(stderr
, "Could not get user's domain "
2314 "groups for user SID %s\n",
2319 case OPT_SIDALIASES
:
2320 if (!wbinfo_get_sidaliases(opt_domain_name
,
2322 d_fprintf(stderr
, "Could not get sid aliases "
2323 "for user SID %s\n", string_arg
);
2328 bool got_error
= false;
2330 if (!wbinfo_auth(string_arg
)) {
2332 "Could not authenticate user "
2333 "%s with plaintext "
2334 "password\n", string_arg
);
2338 if (!wbinfo_auth_crap(string_arg
, use_ntlmv2
,
2341 "Could not authenticate user "
2342 "%s with challenge/response\n",
2352 if (!wbinfo_pam_logon(string_arg
)) {
2353 d_fprintf(stderr
, "pam_logon failed for %s\n",
2362 wbc_status
= wbcLogoffUser(logoff_user
, logoff_uid
,
2364 d_printf("Logoff %s (%d): %s\n", logoff_user
,
2365 logoff_uid
, wbcErrorString(wbc_status
));
2369 uint32_t flags
= WBFLAG_PAM_KRB5
|
2370 WBFLAG_PAM_CACHED_LOGIN
|
2371 WBFLAG_PAM_FALLBACK_AFTER_KRB5
|
2372 WBFLAG_PAM_INFO3_TEXT
|
2373 WBFLAG_PAM_CONTACT_TRUSTDOM
;
2375 if (!wbinfo_auth_krb5(string_arg
, "FILE",
2378 "Could not authenticate user "
2379 "[%s] with Kerberos "
2380 "(ccache: %s)\n", string_arg
,
2387 if (!wbinfo_klog(string_arg
)) {
2388 d_fprintf(stderr
, "Could not klog user\n");
2393 if (!wbinfo_ping()) {
2394 d_fprintf(stderr
, "could not ping winbindd!\n");
2398 case OPT_SET_AUTH_USER
:
2399 if (!wbinfo_set_auth_user(string_arg
)) {
2403 case OPT_GET_AUTH_USER
:
2404 wbinfo_get_auth_user();
2407 case OPT_CCACHE_SAVE
:
2408 if (!wbinfo_ccache_save(string_arg
)) {
2413 if (!wbinfo_getdcname(string_arg
)) {
2417 case OPT_DSGETDCNAME
:
2418 if (!wbinfo_dsgetdcname(string_arg
, 0)) {
2423 if (!wbinfo_dc_info(string_arg
)) {
2427 case OPT_SEPARATOR
: {
2428 const char sep
= winbind_separator();
2432 d_printf("%c\n", sep
);
2435 case OPT_LIST_ALL_DOMAINS
:
2436 if (!wbinfo_list_domains(true, verbose
)) {
2440 case OPT_LIST_OWN_DOMAIN
:
2441 if (!wbinfo_list_own_domain()) {
2445 case OPT_CHANGE_USER_PASSWORD
:
2446 if (!wbinfo_change_user_password(string_arg
)) {
2448 "Could not change user password "
2449 "for user %s\n", string_arg
);
2454 /* generic configuration options */
2455 case OPT_DOMAIN_NAME
:
2459 case OPT_LOGOFF_USER
:
2460 case OPT_LOGOFF_UID
:
2463 d_fprintf(stderr
, "Invalid option\n");
2464 poptPrintHelp(pc
, stderr
, 0);
2476 poptFreeContext(pc
);