2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett 2002-2007
8 Copyright (C) Volker Lendecke 2009
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "winbind_client.h"
26 #include "libwbclient/wbclient.h"
27 #include "lib/popt/popt.h"
28 #include "../libcli/auth/libcli_auth.h"
29 #if (_SAMBA_BUILD_) >= 4
30 #include "lib/cmdline/popt_common.h"
32 #include "popt_common.h"
37 #define DBGC_CLASS DBGC_WINBIND
40 static struct wbcInterfaceDetails *init_interface_details(void)
42 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
43 static struct wbcInterfaceDetails *details;
49 wbc_status = wbcInterfaceDetails(&details);
50 if (!WBC_ERROR_IS_OK(wbc_status)) {
51 d_fprintf(stderr, "could not obtain winbind interface "
52 "details: %s\n", wbcErrorString(wbc_status));
58 static char winbind_separator(void)
60 struct wbcInterfaceDetails *details;
67 details = init_interface_details();
70 d_fprintf(stderr, "could not obtain winbind separator!\n");
74 sep = details->winbind_separator;
78 d_fprintf(stderr, "winbind separator was NULL!\n");
85 static const char *get_winbind_domain(void)
87 static struct wbcInterfaceDetails *details;
89 details = init_interface_details();
92 d_fprintf(stderr, "could not obtain winbind domain name!\n");
96 return details->netbios_domain;
99 static const char *get_winbind_netbios_name(void)
101 static struct wbcInterfaceDetails *details;
103 details = init_interface_details();
106 d_fprintf(stderr, "could not obtain winbind netbios name!\n");
110 return details->netbios_name;
113 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
114 form DOMAIN/user into a domain and a user */
116 static bool parse_wbinfo_domain_user(const char *domuser, fstring domain,
120 char *p = strchr(domuser,winbind_separator());
123 /* Maybe it was a UPN? */
124 p = strchr(domuser, '@');
127 fstrcpy(user, domuser);
131 fstrcpy(user, domuser);
132 fstrcpy(domain, get_winbind_domain());
137 fstrcpy(domain, domuser);
138 domain[PTR_DIFF(p, domuser)] = 0;
143 /* Parse string of "uid,sid" or "gid,sid" into separate int and string values.
144 * Return true if input was valid, false otherwise. */
145 static bool parse_mapping_arg(char *arg, int *id, char **sid)
152 tmp = strtok(arg, ",");
153 *sid = strtok(NULL, ",");
155 if (!tmp || !*tmp || !*sid || !**sid)
158 /* Because atoi() can return 0 on invalid input, which would be a valid
159 * UID/GID we must use strtoul() and do error checking */
160 *id = strtoul(tmp, &endptr, 10);
162 if (endptr[0] != '\0')
168 /* pull pwent info for a given user */
170 static bool wbinfo_get_userinfo(char *user)
172 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
173 struct passwd *pwd = NULL;
175 wbc_status = wbcGetpwnam(user, &pwd);
176 if (!WBC_ERROR_IS_OK(wbc_status)) {
177 d_fprintf(stderr, "failed to call wbcGetpwnam: %s\n",
178 wbcErrorString(wbc_status));
182 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
185 (unsigned int)pwd->pw_uid,
186 (unsigned int)pwd->pw_gid,
196 /* pull pwent info for a given uid */
197 static bool wbinfo_get_uidinfo(int uid)
199 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
200 struct passwd *pwd = NULL;
202 wbc_status = wbcGetpwuid(uid, &pwd);
203 if (!WBC_ERROR_IS_OK(wbc_status)) {
204 d_fprintf(stderr, "failed to call wbcGetpwuid: %s\n",
205 wbcErrorString(wbc_status));
209 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
212 (unsigned int)pwd->pw_uid,
213 (unsigned int)pwd->pw_gid,
223 static bool wbinfo_get_user_sidinfo(const char *sid_str)
225 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
226 struct passwd *pwd = NULL;
227 struct wbcDomainSid sid;
229 wbc_status = wbcStringToSid(sid_str, &sid);
230 wbc_status = wbcGetpwsid(&sid, &pwd);
231 if (!WBC_ERROR_IS_OK(wbc_status)) {
232 d_fprintf(stderr, "failed to call wbcGetpwsid: %s\n",
233 wbcErrorString(wbc_status));
237 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
240 (unsigned int)pwd->pw_uid,
241 (unsigned int)pwd->pw_gid,
252 /* pull grent for a given group */
253 static bool wbinfo_get_groupinfo(const char *group)
255 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
259 wbc_status = wbcGetgrnam(group, &grp);
260 if (!WBC_ERROR_IS_OK(wbc_status)) {
261 d_fprintf(stderr, "failed to call wbcGetgrnam: %s\n",
262 wbcErrorString(wbc_status));
266 d_printf("%s:%s:%u:",
269 (unsigned int)grp->gr_gid);
272 while (*mem != NULL) {
273 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
283 /* pull grent for a given gid */
284 static bool wbinfo_get_gidinfo(int gid)
286 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
290 wbc_status = wbcGetgrgid(gid, &grp);
291 if (!WBC_ERROR_IS_OK(wbc_status)) {
292 d_fprintf(stderr, "failed to call wbcGetgrgid: %s\n",
293 wbcErrorString(wbc_status));
297 d_printf("%s:%s:%u:",
300 (unsigned int)grp->gr_gid);
303 while (*mem != NULL) {
304 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
314 /* List groups a user is a member of */
316 static bool wbinfo_get_usergroups(const char *user)
318 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
321 gid_t *groups = NULL;
325 wbc_status = wbcGetGroups(user, &num_groups, &groups);
326 if (!WBC_ERROR_IS_OK(wbc_status)) {
327 d_fprintf(stderr, "failed to call wbcGetGroups: %s\n",
328 wbcErrorString(wbc_status));
332 for (i = 0; i < num_groups; i++) {
333 d_printf("%d\n", (int)groups[i]);
336 wbcFreeMemory(groups);
342 /* List group SIDs a user SID is a member of */
343 static bool wbinfo_get_usersids(const char *user_sid_str)
345 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
348 struct wbcDomainSid user_sid, *sids = NULL;
352 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
353 if (!WBC_ERROR_IS_OK(wbc_status)) {
354 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
355 wbcErrorString(wbc_status));
359 wbc_status = wbcLookupUserSids(&user_sid, false, &num_sids, &sids);
360 if (!WBC_ERROR_IS_OK(wbc_status)) {
361 d_fprintf(stderr, "failed to call wbcLookupUserSids: %s\n",
362 wbcErrorString(wbc_status));
366 for (i = 0; i < num_sids; i++) {
367 char str[WBC_SID_STRING_BUFLEN];
368 wbcSidToStringBuf(&sids[i], str, sizeof(str));
369 d_printf("%s\n", str);
377 static bool wbinfo_get_userdomgroups(const char *user_sid_str)
379 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
382 struct wbcDomainSid user_sid, *sids = NULL;
386 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
387 if (!WBC_ERROR_IS_OK(wbc_status)) {
388 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
389 wbcErrorString(wbc_status));
393 wbc_status = wbcLookupUserSids(&user_sid, true, &num_sids, &sids);
394 if (!WBC_ERROR_IS_OK(wbc_status)) {
395 d_fprintf(stderr, "failed to call wbcLookupUserSids: %s\n",
396 wbcErrorString(wbc_status));
400 for (i = 0; i < num_sids; i++) {
401 char str[WBC_SID_STRING_BUFLEN];
402 wbcSidToStringBuf(&sids[i], str, sizeof(str));
403 d_printf("%s\n", str);
411 static bool wbinfo_get_sidaliases(const char *domain,
412 const char *user_sid_str)
414 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
415 struct wbcDomainInfo *dinfo = NULL;
417 struct wbcDomainSid user_sid;
418 uint32_t *alias_rids = NULL;
419 uint32_t num_alias_rids;
420 char domain_sid_str[WBC_SID_STRING_BUFLEN];
423 if ((domain == NULL) || (strequal(domain, ".")) ||
424 (domain[0] == '\0')) {
425 domain = get_winbind_domain();
430 wbc_status = wbcDomainInfo(domain, &dinfo);
431 if (!WBC_ERROR_IS_OK(wbc_status)) {
432 d_fprintf(stderr, "wbcDomainInfo(%s) failed: %s\n", domain,
433 wbcErrorString(wbc_status));
436 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
437 if (!WBC_ERROR_IS_OK(wbc_status)) {
441 wbc_status = wbcGetSidAliases(&dinfo->sid, &user_sid, 1,
442 &alias_rids, &num_alias_rids);
443 if (!WBC_ERROR_IS_OK(wbc_status)) {
447 wbcSidToStringBuf(&dinfo->sid, domain_sid_str, sizeof(domain_sid_str));
449 for (i = 0; i < num_alias_rids; i++) {
450 d_printf("%s-%d\n", domain_sid_str, alias_rids[i]);
453 wbcFreeMemory(alias_rids);
456 wbcFreeMemory(dinfo);
457 return (WBC_ERR_SUCCESS == wbc_status);
461 /* Convert NetBIOS name to IP */
463 static bool wbinfo_wins_byname(const char *name)
465 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
468 wbc_status = wbcResolveWinsByName(name, &ip);
469 if (!WBC_ERROR_IS_OK(wbc_status)) {
470 d_fprintf(stderr, "failed to call wbcResolveWinsByName: %s\n",
471 wbcErrorString(wbc_status));
475 /* Display response */
477 d_printf("%s\n", ip);
484 /* Convert IP to NetBIOS name */
486 static bool wbinfo_wins_byip(const char *ip)
488 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
491 wbc_status = wbcResolveWinsByIP(ip, &name);
492 if (!WBC_ERROR_IS_OK(wbc_status)) {
493 d_fprintf(stderr, "failed to call wbcResolveWinsByIP: %s\n",
494 wbcErrorString(wbc_status));
498 /* Display response */
500 d_printf("%s\n", name);
507 /* List all/trusted domains */
509 static bool wbinfo_list_domains(bool list_all_domains, bool verbose)
511 struct wbcDomainInfo *domain_list = NULL;
513 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
514 bool print_all = !list_all_domains && verbose;
517 wbc_status = wbcListTrusts(&domain_list, &num_domains);
518 if (!WBC_ERROR_IS_OK(wbc_status)) {
519 d_fprintf(stderr, "failed to call wbcListTrusts: %s\n",
520 wbcErrorString(wbc_status));
525 d_printf("%-16s%-65s%-12s%-12s%-5s%-5s\n",
526 "Domain Name", "DNS Domain", "Trust Type",
527 "Transitive", "In", "Out");
530 for (i=0; i<num_domains; i++) {
532 d_printf("%-16s", domain_list[i].short_name);
534 d_printf("%s", domain_list[i].short_name);
539 d_printf("%-65s", domain_list[i].dns_name);
541 switch(domain_list[i].trust_type) {
542 case WBC_DOMINFO_TRUSTTYPE_NONE:
545 case WBC_DOMINFO_TRUSTTYPE_FOREST:
548 case WBC_DOMINFO_TRUSTTYPE_EXTERNAL:
549 d_printf("External ");
551 case WBC_DOMINFO_TRUSTTYPE_IN_FOREST:
552 d_printf("In-Forest ");
556 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_TRANSITIVE) {
562 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_INCOMING) {
568 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_OUTGOING) {
577 wbcFreeMemory(domain_list);
582 /* List own domain */
584 static bool wbinfo_list_own_domain(void)
586 d_printf("%s\n", get_winbind_domain());
591 /* show sequence numbers */
592 static bool wbinfo_show_sequence(const char *domain)
594 d_printf("This command has been deprecated. Please use the "
595 "--online-status option instead.\n");
599 /* show sequence numbers */
600 static bool wbinfo_show_onlinestatus(const char *domain)
602 struct wbcDomainInfo *domain_list = NULL;
604 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
607 wbc_status = wbcListTrusts(&domain_list, &num_domains);
608 if (!WBC_ERROR_IS_OK(wbc_status)) {
609 d_fprintf(stderr, "failed to call wbcListTrusts: %s\n",
610 wbcErrorString(wbc_status));
614 for (i=0; i<num_domains; i++) {
618 if (!strequal(domain_list[i].short_name, domain)) {
623 is_offline = (domain_list[i].domain_flags &
624 WBC_DOMINFO_DOMAIN_OFFLINE);
626 d_printf("%s : %s\n",
627 domain_list[i].short_name,
628 is_offline ? "offline" : "online" );
631 wbcFreeMemory(domain_list);
637 /* Show domain info */
639 static bool wbinfo_domain_info(const char *domain)
641 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
642 struct wbcDomainInfo *dinfo = NULL;
643 char sid_str[WBC_SID_STRING_BUFLEN];
645 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
646 domain = get_winbind_domain();
651 wbc_status = wbcDomainInfo(domain, &dinfo);
652 if (!WBC_ERROR_IS_OK(wbc_status)) {
653 d_fprintf(stderr, "failed to call wbcDomainInfo: %s\n",
654 wbcErrorString(wbc_status));
658 wbcSidToStringBuf(&dinfo->sid, sid_str, sizeof(sid_str));
660 /* Display response */
662 d_printf("Name : %s\n", dinfo->short_name);
663 d_printf("Alt_Name : %s\n", dinfo->dns_name);
665 d_printf("SID : %s\n", sid_str);
667 d_printf("Active Directory : %s\n",
668 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_AD) ? "Yes" : "No");
669 d_printf("Native : %s\n",
670 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_NATIVE) ?
673 d_printf("Primary : %s\n",
674 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_PRIMARY) ?
677 wbcFreeMemory(dinfo);
682 /* Get a foreign DC's name */
683 static bool wbinfo_getdcname(const char *domain_name)
685 struct winbindd_request request;
686 struct winbindd_response response;
688 ZERO_STRUCT(request);
689 ZERO_STRUCT(response);
691 fstrcpy(request.domain_name, domain_name);
695 if (winbindd_request_response(WINBINDD_GETDCNAME, &request,
696 &response) != NSS_STATUS_SUCCESS) {
697 d_fprintf(stderr, "Could not get dc name for %s\n",domain_name);
701 /* Display response */
703 d_printf("%s\n", response.data.dc_name);
709 static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
711 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
712 struct wbcDomainControllerInfoEx *dc_info;
715 wbc_status = wbcLookupDomainControllerEx(domain_name, NULL, NULL,
716 flags | DS_DIRECTORY_SERVICE_REQUIRED,
718 if (!WBC_ERROR_IS_OK(wbc_status)) {
719 printf("Could not find dc for %s\n", domain_name);
723 wbcGuidToString(dc_info->domain_guid, &str);
725 d_printf("%s\n", dc_info->dc_unc);
726 d_printf("%s\n", dc_info->dc_address);
727 d_printf("%d\n", dc_info->dc_address_type);
728 d_printf("%s\n", str);
729 d_printf("%s\n", dc_info->domain_name);
730 d_printf("%s\n", dc_info->forest_name);
731 d_printf("0x%08x\n", dc_info->dc_flags);
732 d_printf("%s\n", dc_info->dc_site_name);
733 d_printf("%s\n", dc_info->client_site_name);
738 /* Check trust account password */
740 static bool wbinfo_check_secret(const char *domain)
742 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
743 struct wbcAuthErrorInfo *error = NULL;
744 const char *domain_name;
747 domain_name = domain;
749 domain_name = get_winbind_domain();
752 wbc_status = wbcCheckTrustCredentials(domain_name, &error);
754 d_printf("checking the trust secret for domain %s via RPC calls %s\n",
756 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
758 if (wbc_status == WBC_ERR_AUTH_ERROR) {
759 d_fprintf(stderr, "error code was %s (0x%x)\n",
760 error->nt_string, error->nt_status);
761 wbcFreeMemory(error);
763 if (!WBC_ERROR_IS_OK(wbc_status)) {
764 d_fprintf(stderr, "failed to call wbcCheckTrustCredentials: "
765 "%s\n", wbcErrorString(wbc_status));
772 /* Find the currently connected DCs */
774 static bool wbinfo_dc_info(const char *domain_name)
776 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
778 const char **dc_names, **dc_ips;
780 wbc_status = wbcDcInfo(domain_name, &num_dcs,
782 if (!WBC_ERROR_IS_OK(wbc_status)) {
783 printf("Could not find dc info %s\n",
784 domain_name ? domain_name : "our domain");
788 for (i=0; i<num_dcs; i++) {
789 printf("%s (%s)\n", dc_names[i], dc_ips[i]);
791 wbcFreeMemory(dc_names);
792 wbcFreeMemory(dc_ips);
797 /* Change trust account password */
799 static bool wbinfo_change_secret(const char *domain)
801 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
802 struct wbcAuthErrorInfo *error = NULL;
803 const char *domain_name;
806 domain_name = domain;
808 domain_name = get_winbind_domain();
811 wbc_status = wbcChangeTrustCredentials(domain_name, &error);
813 d_printf("changing the trust secret for domain %s via RPC calls %s\n",
815 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
817 if (wbc_status == WBC_ERR_AUTH_ERROR) {
818 d_fprintf(stderr, "error code was %s (0x%x)\n",
819 error->nt_string, error->nt_status);
820 wbcFreeMemory(error);
822 if (!WBC_ERROR_IS_OK(wbc_status)) {
823 d_fprintf(stderr, "failed to call wbcChangeTrustCredentials: "
824 "%s\n", wbcErrorString(wbc_status));
831 /* Check DC connection */
833 static bool wbinfo_ping_dc(void)
835 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
836 struct wbcAuthErrorInfo *error = NULL;
839 wbc_status = wbcPingDc2(NULL, &error, &dcname);
841 d_printf("checking the NETLOGON dc connection to \"%s\" %s\n",
842 dcname ? dcname : "",
843 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
845 if (wbc_status == WBC_ERR_AUTH_ERROR) {
846 d_fprintf(stderr, "error code was %s (0x%x)\n",
847 error->nt_string, error->nt_status);
848 wbcFreeMemory(error);
851 if (!WBC_ERROR_IS_OK(wbc_status)) {
852 d_fprintf(stderr, "failed to call wbcPingDc: %s\n",
853 wbcErrorString(wbc_status));
860 /* Convert uid to sid */
862 static bool wbinfo_uid_to_sid(uid_t uid)
864 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
865 struct wbcDomainSid sid;
866 char sid_str[WBC_SID_STRING_BUFLEN];
870 wbc_status = wbcUidToSid(uid, &sid);
871 if (!WBC_ERROR_IS_OK(wbc_status)) {
872 d_fprintf(stderr, "failed to call wbcUidToSid: %s\n",
873 wbcErrorString(wbc_status));
877 wbcSidToStringBuf(&sid, sid_str, sizeof(sid_str));
879 /* Display response */
881 d_printf("%s\n", sid_str);
886 /* Convert gid to sid */
888 static bool wbinfo_gid_to_sid(gid_t gid)
890 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
891 struct wbcDomainSid sid;
892 char sid_str[WBC_SID_STRING_BUFLEN];
896 wbc_status = wbcGidToSid(gid, &sid);
897 if (!WBC_ERROR_IS_OK(wbc_status)) {
898 d_fprintf(stderr, "failed to call wbcGidToSid: %s\n",
899 wbcErrorString(wbc_status));
903 wbcSidToStringBuf(&sid, sid_str, sizeof(sid_str));
905 /* Display response */
907 d_printf("%s\n", sid_str);
912 /* Convert sid to uid */
914 static bool wbinfo_sid_to_uid(const char *sid_str)
916 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
917 struct wbcDomainSid sid;
922 wbc_status = wbcStringToSid(sid_str, &sid);
923 if (!WBC_ERROR_IS_OK(wbc_status)) {
924 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
925 wbcErrorString(wbc_status));
929 wbc_status = wbcSidToUid(&sid, &uid);
930 if (!WBC_ERROR_IS_OK(wbc_status)) {
931 d_fprintf(stderr, "failed to call wbcSidToUid: %s\n",
932 wbcErrorString(wbc_status));
936 /* Display response */
938 d_printf("%d\n", (int)uid);
943 static bool wbinfo_sid_to_gid(const char *sid_str)
945 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
946 struct wbcDomainSid sid;
951 wbc_status = wbcStringToSid(sid_str, &sid);
952 if (!WBC_ERROR_IS_OK(wbc_status)) {
953 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
954 wbcErrorString(wbc_status));
958 wbc_status = wbcSidToGid(&sid, &gid);
959 if (!WBC_ERROR_IS_OK(wbc_status)) {
960 d_fprintf(stderr, "failed to call wbcSidToGid: %s\n",
961 wbcErrorString(wbc_status));
965 /* Display response */
967 d_printf("%d\n", (int)gid);
972 static bool wbinfo_sids_to_unix_ids(const char *arg)
974 char sidstr[WBC_SID_STRING_BUFLEN];
975 struct wbcDomainSid *sids;
976 struct wbcUnixId *unix_ids;
986 while (next_token(&p, sidstr, LIST_SEP, sizeof(sidstr))) {
987 sids = talloc_realloc(talloc_tos(), sids, struct wbcDomainSid,
990 d_fprintf(stderr, "talloc failed\n");
993 wbc_status = wbcStringToSid(sidstr, &sids[num_sids]);
994 if (!WBC_ERROR_IS_OK(wbc_status)) {
995 d_fprintf(stderr, "wbcSidToString(%s) failed: %s\n",
996 sidstr, wbcErrorString(wbc_status));
1003 unix_ids = talloc_array(talloc_tos(), struct wbcUnixId, num_sids);
1004 if (unix_ids == NULL) {
1009 wbc_status = wbcSidsToUnixIds(sids, num_sids, unix_ids);
1010 if (!WBC_ERROR_IS_OK(wbc_status)) {
1011 d_fprintf(stderr, "wbcSidsToUnixIds failed: %s\n",
1012 wbcErrorString(wbc_status));
1017 for (i=0; i<num_sids; i++) {
1019 wbcSidToStringBuf(&sids[i], sidstr, sizeof(sidstr));
1021 switch(unix_ids[i].type) {
1022 case WBC_ID_TYPE_UID:
1023 d_printf("%s -> uid %d\n", sidstr, unix_ids[i].id.uid);
1025 case WBC_ID_TYPE_GID:
1026 d_printf("%s -> gid %d\n", sidstr, unix_ids[i].id.gid);
1028 case WBC_ID_TYPE_BOTH:
1029 d_printf("%s -> uid/gid %d\n", sidstr, unix_ids[i].id.uid);
1032 d_printf("%s -> unmapped\n", sidstr);
1038 TALLOC_FREE(unix_ids);
1043 static bool wbinfo_allocate_uid(void)
1045 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1050 wbc_status = wbcAllocateUid(&uid);
1051 if (!WBC_ERROR_IS_OK(wbc_status)) {
1052 d_fprintf(stderr, "failed to call wbcAllocateUid: %s\n",
1053 wbcErrorString(wbc_status));
1057 /* Display response */
1059 d_printf("New uid: %u\n", (unsigned int)uid);
1064 static bool wbinfo_allocate_gid(void)
1066 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1071 wbc_status = wbcAllocateGid(&gid);
1072 if (!WBC_ERROR_IS_OK(wbc_status)) {
1073 d_fprintf(stderr, "failed to call wbcAllocateGid: %s\n",
1074 wbcErrorString(wbc_status));
1078 /* Display response */
1080 d_printf("New gid: %u\n", (unsigned int)gid);
1085 static bool wbinfo_set_uid_mapping(uid_t uid, const char *sid_str)
1087 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1088 struct wbcDomainSid sid;
1092 wbc_status = wbcStringToSid(sid_str, &sid);
1093 if (!WBC_ERROR_IS_OK(wbc_status)) {
1094 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1095 wbcErrorString(wbc_status));
1099 wbc_status = wbcSetUidMapping(uid, &sid);
1100 if (!WBC_ERROR_IS_OK(wbc_status)) {
1101 d_fprintf(stderr, "failed to call wbcSetUidMapping: %s\n",
1102 wbcErrorString(wbc_status));
1106 /* Display response */
1108 d_printf("uid %u now mapped to sid %s\n",
1109 (unsigned int)uid, sid_str);
1114 static bool wbinfo_set_gid_mapping(gid_t gid, const char *sid_str)
1116 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1117 struct wbcDomainSid sid;
1121 wbc_status = wbcStringToSid(sid_str, &sid);
1122 if (!WBC_ERROR_IS_OK(wbc_status)) {
1123 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1124 wbcErrorString(wbc_status));
1128 wbc_status = wbcSetGidMapping(gid, &sid);
1129 if (!WBC_ERROR_IS_OK(wbc_status)) {
1130 d_fprintf(stderr, "failed to call wbcSetGidMapping: %s\n",
1131 wbcErrorString(wbc_status));
1135 /* Display response */
1137 d_printf("gid %u now mapped to sid %s\n",
1138 (unsigned int)gid, sid_str);
1143 static bool wbinfo_remove_uid_mapping(uid_t uid, const char *sid_str)
1145 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1146 struct wbcDomainSid sid;
1150 wbc_status = wbcStringToSid(sid_str, &sid);
1151 if (!WBC_ERROR_IS_OK(wbc_status)) {
1152 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1153 wbcErrorString(wbc_status));
1157 wbc_status = wbcRemoveUidMapping(uid, &sid);
1158 if (!WBC_ERROR_IS_OK(wbc_status)) {
1159 d_fprintf(stderr, "failed to call wbcRemoveUidMapping: %s\n",
1160 wbcErrorString(wbc_status));
1164 /* Display response */
1166 d_printf("Removed uid %u to sid %s mapping\n",
1167 (unsigned int)uid, sid_str);
1172 static bool wbinfo_remove_gid_mapping(gid_t gid, const char *sid_str)
1174 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1175 struct wbcDomainSid sid;
1179 wbc_status = wbcStringToSid(sid_str, &sid);
1180 if (!WBC_ERROR_IS_OK(wbc_status)) {
1181 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1182 wbcErrorString(wbc_status));
1186 wbc_status = wbcRemoveGidMapping(gid, &sid);
1187 if (!WBC_ERROR_IS_OK(wbc_status)) {
1188 d_fprintf(stderr, "failed to call wbcRemoveGidMapping: %s\n",
1189 wbcErrorString(wbc_status));
1193 /* Display response */
1195 d_printf("Removed gid %u to sid %s mapping\n",
1196 (unsigned int)gid, sid_str);
1201 /* Convert sid to string */
1203 static bool wbinfo_lookupsid(const char *sid_str)
1205 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1206 struct wbcDomainSid sid;
1209 enum wbcSidType type;
1211 /* Send off request */
1213 wbc_status = wbcStringToSid(sid_str, &sid);
1214 if (!WBC_ERROR_IS_OK(wbc_status)) {
1215 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1216 wbcErrorString(wbc_status));
1220 wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
1221 if (!WBC_ERROR_IS_OK(wbc_status)) {
1222 d_fprintf(stderr, "failed to call wbcLookupSid: %s\n",
1223 wbcErrorString(wbc_status));
1227 /* Display response */
1229 d_printf("%s%c%s %d\n",
1230 domain, winbind_separator(), name, type);
1232 wbcFreeMemory(domain);
1233 wbcFreeMemory(name);
1238 /* Convert sid to fullname */
1240 static bool wbinfo_lookupsid_fullname(const char *sid_str)
1242 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1243 struct wbcDomainSid sid;
1246 enum wbcSidType type;
1248 /* Send off request */
1250 wbc_status = wbcStringToSid(sid_str, &sid);
1251 if (!WBC_ERROR_IS_OK(wbc_status)) {
1252 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1253 wbcErrorString(wbc_status));
1257 wbc_status = wbcGetDisplayName(&sid, &domain, &name, &type);
1258 if (!WBC_ERROR_IS_OK(wbc_status)) {
1259 d_fprintf(stderr, "failed to call wbcGetDisplayName: %s\n",
1260 wbcErrorString(wbc_status));
1264 /* Display response */
1266 d_printf("%s%c%s %d\n",
1267 domain, winbind_separator(), name, type);
1269 wbcFreeMemory(domain);
1270 wbcFreeMemory(name);
1275 /* Lookup a list of RIDs */
1277 static bool wbinfo_lookuprids(const char *domain, const char *arg)
1279 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1280 struct wbcDomainInfo *dinfo = NULL;
1281 char *domain_name = NULL;
1282 const char **names = NULL;
1283 enum wbcSidType *types = NULL;
1286 uint32_t *rids = NULL;
1289 TALLOC_CTX *mem_ctx = NULL;
1292 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
1293 domain = get_winbind_domain();
1298 wbc_status = wbcDomainInfo(domain, &dinfo);
1299 if (!WBC_ERROR_IS_OK(wbc_status)) {
1300 d_printf("wbcDomainInfo(%s) failed: %s\n", domain,
1301 wbcErrorString(wbc_status));
1305 mem_ctx = talloc_new(NULL);
1306 if (mem_ctx == NULL) {
1307 d_printf("talloc_new failed\n");
1315 while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
1316 uint32_t rid = strtoul(ridstr, NULL, 10);
1317 rids = talloc_realloc(mem_ctx, rids, uint32_t, num_rids + 1);
1319 d_printf("talloc_realloc failed\n");
1321 rids[num_rids] = rid;
1326 d_printf("no rids\n");
1330 wbc_status = wbcLookupRids(&dinfo->sid, num_rids, rids,
1331 (const char **)&domain_name, &names, &types);
1332 if (!WBC_ERROR_IS_OK(wbc_status)) {
1333 d_printf("winbind_lookup_rids failed: %s\n",
1334 wbcErrorString(wbc_status));
1338 d_printf("Domain: %s\n", domain_name);
1340 for (i=0; i<num_rids; i++) {
1341 d_printf("%8d: %s (%s)\n", rids[i], names[i],
1342 wbcSidTypeString(types[i]));
1347 wbcFreeMemory(dinfo);
1348 wbcFreeMemory(domain_name);
1349 wbcFreeMemory(names);
1350 wbcFreeMemory(types);
1351 TALLOC_FREE(mem_ctx);
1355 static bool wbinfo_lookup_sids(const char *arg)
1357 char sidstr[WBC_SID_STRING_BUFLEN];
1358 struct wbcDomainSid *sids;
1359 struct wbcDomainInfo *domains;
1360 struct wbcTranslatedName *names;
1371 while (next_token(&p, sidstr, LIST_SEP, sizeof(sidstr))) {
1372 sids = talloc_realloc(talloc_tos(), sids, struct wbcDomainSid,
1375 d_fprintf(stderr, "talloc failed\n");
1378 wbc_status = wbcStringToSid(sidstr, &sids[num_sids]);
1379 if (!WBC_ERROR_IS_OK(wbc_status)) {
1380 d_fprintf(stderr, "wbcSidToString(%s) failed: %s\n",
1381 sidstr, wbcErrorString(wbc_status));
1388 wbc_status = wbcLookupSids(sids, num_sids, &domains, &num_domains,
1390 if (!WBC_ERROR_IS_OK(wbc_status)) {
1391 d_fprintf(stderr, "wbcLookupSids failed: %s\n",
1392 wbcErrorString(wbc_status));
1397 for (i=0; i<num_sids; i++) {
1398 wbcSidToStringBuf(&sids[i], sidstr, sizeof(sidstr));
1400 d_printf("%s -> %s\\%s %d\n", sidstr,
1401 domains[names[i].domain_index].short_name,
1402 names[i].name, names[i].type);
1404 wbcFreeMemory(names);
1405 wbcFreeMemory(domains);
1409 /* Convert string to sid */
1411 static bool wbinfo_lookupname(const char *full_name)
1413 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1414 struct wbcDomainSid sid;
1415 char sid_str[WBC_SID_STRING_BUFLEN];
1416 enum wbcSidType type;
1417 fstring domain_name;
1418 fstring account_name;
1420 /* Send off request */
1422 parse_wbinfo_domain_user(full_name, domain_name,
1425 wbc_status = wbcLookupName(domain_name, account_name,
1427 if (!WBC_ERROR_IS_OK(wbc_status)) {
1428 d_fprintf(stderr, "failed to call wbcLookupName: %s\n",
1429 wbcErrorString(wbc_status));
1433 wbcSidToStringBuf(&sid, sid_str, sizeof(sid_str));
1435 /* Display response */
1437 d_printf("%s %s (%d)\n", sid_str, wbcSidTypeString(type), type);
1442 static char *wbinfo_prompt_pass(TALLOC_CTX *mem_ctx,
1444 const char *username)
1447 char buf[1024] = {0};
1450 prompt = talloc_asprintf(mem_ctx, "Enter %s's ", username);
1455 prompt = talloc_asprintf_append(prompt, "%s ", prefix);
1460 prompt = talloc_asprintf_append(prompt, "password: ");
1465 rc = samba_getpass(prompt, buf, sizeof(buf), false, false);
1466 TALLOC_FREE(prompt);
1471 return talloc_strdup(mem_ctx, buf);
1474 /* Authenticate a user with a plaintext password */
1476 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32_t flags)
1478 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1481 char *password = NULL;
1483 char *local_cctype = NULL;
1485 struct wbcLogonUserParams params;
1486 struct wbcLogonUserInfo *info;
1487 struct wbcAuthErrorInfo *error;
1488 struct wbcUserPasswordPolicyInfo *policy;
1489 TALLOC_CTX *frame = talloc_tos();
1491 if ((s = talloc_strdup(frame, username)) == NULL) {
1495 if ((p = strchr(s, '%')) != NULL) {
1498 password = talloc_strdup(frame, p);
1500 password = wbinfo_prompt_pass(frame, NULL, username);
1503 local_cctype = talloc_strdup(frame, cctype);
1509 params.username = name;
1510 params.password = password;
1511 params.num_blobs = 0;
1512 params.blobs = NULL;
1514 wbc_status = wbcAddNamedBlob(¶ms.num_blobs,
1520 if (!WBC_ERROR_IS_OK(wbc_status)) {
1521 d_fprintf(stderr, "failed to call wbcAddNamedBlob: %s\n",
1522 wbcErrorString(wbc_status));
1526 wbc_status = wbcAddNamedBlob(¶ms.num_blobs,
1532 if (!WBC_ERROR_IS_OK(wbc_status)) {
1533 d_fprintf(stderr, "failed to call wbcAddNamedBlob: %s\n",
1534 wbcErrorString(wbc_status));
1538 wbc_status = wbcAddNamedBlob(¶ms.num_blobs,
1542 (uint8_t *)local_cctype,
1544 if (!WBC_ERROR_IS_OK(wbc_status)) {
1545 d_fprintf(stderr, "failed to call wbcAddNamedBlob: %s\n",
1546 wbcErrorString(wbc_status));
1550 wbc_status = wbcLogonUser(¶ms, &info, &error, &policy);
1552 d_printf("plaintext kerberos password authentication for [%s] %s "
1553 "(requesting cctype: %s)\n",
1554 username, WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed",
1559 "error code was %s (0x%x)\nerror message was: %s\n",
1562 error->display_string);
1565 if (WBC_ERROR_IS_OK(wbc_status)) {
1566 if (flags & WBFLAG_PAM_INFO3_TEXT) {
1567 if (info && info->info && info->info->user_flags &
1568 NETLOGON_CACHED_ACCOUNT) {
1569 d_printf("user_flgs: "
1570 "NETLOGON_CACHED_ACCOUNT\n");
1576 for (i=0; i < info->num_blobs; i++) {
1577 if (strequal(info->blobs[i].name,
1579 d_printf("credentials were put "
1582 info->blobs[i].blob.data);
1587 d_printf("no credentials cached\n");
1592 wbcFreeMemory(params.blobs);
1594 return WBC_ERROR_IS_OK(wbc_status);
1597 /* Authenticate a user with a plaintext password */
1599 static bool wbinfo_auth(char *username)
1601 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1604 char *password = NULL;
1606 TALLOC_CTX *frame = talloc_tos();
1608 if ((s = talloc_strdup(frame, username)) == NULL) {
1612 if ((p = strchr(s, '%')) != NULL) {
1615 password = talloc_strdup(frame, p);
1617 password = wbinfo_prompt_pass(frame, NULL, username);
1622 wbc_status = wbcAuthenticateUser(name, password);
1624 d_printf("plaintext password authentication %s\n",
1625 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1628 if (response.data.auth.nt_status)
1630 "error code was %s (0x%x)\nerror message was: %s\n",
1631 response.data.auth.nt_status_string,
1632 response.data.auth.nt_status,
1633 response.data.auth.error_string);
1636 return WBC_ERROR_IS_OK(wbc_status);
1639 /* Authenticate a user with a challenge/response */
1641 static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
1643 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1644 struct wbcAuthUserParams params;
1645 struct wbcAuthUserInfo *info = NULL;
1646 struct wbcAuthErrorInfo *err = NULL;
1647 DATA_BLOB lm = data_blob_null;
1648 DATA_BLOB nt = data_blob_null;
1650 fstring name_domain;
1653 TALLOC_CTX *frame = talloc_tos();
1655 p = strchr(username, '%');
1659 pass = talloc_strdup(frame, p + 1);
1661 pass = wbinfo_prompt_pass(frame, NULL, username);
1664 parse_wbinfo_domain_user(username, name_domain, name_user);
1666 params.account_name = name_user;
1667 params.domain_name = name_domain;
1668 params.workstation_name = NULL;
1671 params.parameter_control= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
1672 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
1674 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
1676 generate_random_buffer(params.password.response.challenge, 8);
1679 DATA_BLOB server_chal;
1680 DATA_BLOB names_blob;
1682 server_chal = data_blob(params.password.response.challenge, 8);
1684 /* Pretend this is a login to 'us', for blob purposes */
1685 names_blob = NTLMv2_generate_names_blob(NULL,
1686 get_winbind_netbios_name(),
1687 get_winbind_domain());
1689 if (!SMBNTLMv2encrypt(NULL, name_user, name_domain, pass,
1692 &lm, &nt, NULL, NULL)) {
1693 data_blob_free(&names_blob);
1694 data_blob_free(&server_chal);
1698 data_blob_free(&names_blob);
1699 data_blob_free(&server_chal);
1704 lm = data_blob(NULL, 24);
1705 ok = SMBencrypt(pass,
1706 params.password.response.challenge,
1709 data_blob_free(&lm);
1712 nt = data_blob(NULL, 24);
1713 SMBNTencrypt(pass, params.password.response.challenge,
1717 params.password.response.nt_length = nt.length;
1718 params.password.response.nt_data = nt.data;
1719 params.password.response.lm_length = lm.length;
1720 params.password.response.lm_data = lm.data;
1722 wbc_status = wbcAuthenticateUserEx(¶ms, &info, &err);
1724 /* Display response */
1726 d_printf("challenge/response password authentication %s\n",
1727 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1729 if (wbc_status == WBC_ERR_AUTH_ERROR) {
1731 "error code was %s (0x%x)\nerror message was: %s\n",
1734 err->display_string);
1736 } else if (WBC_ERROR_IS_OK(wbc_status)) {
1737 wbcFreeMemory(info);
1740 data_blob_free(&nt);
1741 data_blob_free(&lm);
1743 return WBC_ERROR_IS_OK(wbc_status);
1746 /* Authenticate a user with a plaintext password */
1748 static bool wbinfo_pam_logon(char *username)
1750 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1751 struct wbcLogonUserParams params;
1752 struct wbcAuthErrorInfo *error;
1755 TALLOC_CTX *frame = talloc_tos();
1759 ZERO_STRUCT(params);
1761 if ((s = talloc_strdup(frame, username)) == NULL) {
1765 if ((p = strchr(s, '%')) != NULL) {
1768 params.password = talloc_strdup(frame, p);
1770 params.password = wbinfo_prompt_pass(frame, NULL, username);
1772 params.username = s;
1774 flags = WBFLAG_PAM_CACHED_LOGIN;
1776 wbc_status = wbcAddNamedBlob(¶ms.num_blobs, ¶ms.blobs,
1778 (uint8_t *)&flags, sizeof(flags));
1779 if (!WBC_ERROR_IS_OK(wbc_status)) {
1780 d_printf("wbcAddNamedBlob failed: %s\n",
1781 wbcErrorString(wbc_status));
1787 wbc_status = wbcAddNamedBlob(¶ms.num_blobs, ¶ms.blobs,
1789 (uint8_t *)&uid, sizeof(uid));
1790 if (!WBC_ERROR_IS_OK(wbc_status)) {
1791 d_printf("wbcAddNamedBlob failed: %s\n",
1792 wbcErrorString(wbc_status));
1796 wbc_status = wbcLogonUser(¶ms, NULL, &error, NULL);
1798 wbcFreeMemory(params.blobs);
1800 d_printf("plaintext password authentication %s\n",
1801 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1803 if (!WBC_ERROR_IS_OK(wbc_status)) {
1805 "error code was %s (0x%x)\nerror message was: %s\n",
1807 (int)error->nt_status,
1808 error->display_string);
1809 wbcFreeMemory(error);
1815 /* Save creds with winbind */
1817 static bool wbinfo_ccache_save(char *username)
1819 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1822 char *password = NULL;
1824 TALLOC_CTX *frame = talloc_stackframe();
1826 s = talloc_strdup(frame, username);
1835 password = talloc_strdup(frame, p);
1837 password = wbinfo_prompt_pass(frame, NULL, username);
1842 wbc_status = wbcCredentialSave(name, password);
1844 d_printf("saving creds %s\n",
1845 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1849 return WBC_ERROR_IS_OK(wbc_status);
1852 #ifdef WITH_FAKE_KASERVER
1853 /* Authenticate a user with a plaintext password and set a token */
1855 static bool wbinfo_klog(char *username)
1857 struct winbindd_request request;
1858 struct winbindd_response response;
1862 /* Send off request */
1864 ZERO_STRUCT(request);
1865 ZERO_STRUCT(response);
1867 p = strchr(username, '%');
1871 fstrcpy(request.data.auth.user, username);
1872 fstrcpy(request.data.auth.pass, p + 1);
1875 fstrcpy(request.data.auth.user, username);
1876 (void) samba_getpass("Password: ",
1877 request.data.auth.pass,
1878 sizeof(request.data.auth.pass),
1882 request.flags |= WBFLAG_PAM_AFS_TOKEN;
1884 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request,
1887 /* Display response */
1889 d_printf("plaintext password authentication %s\n",
1890 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1892 if (response.data.auth.nt_status)
1894 "error code was %s (0x%x)\nerror message was: %s\n",
1895 response.data.auth.nt_status_string,
1896 response.data.auth.nt_status,
1897 response.data.auth.error_string);
1899 if (result != NSS_STATUS_SUCCESS)
1902 if (response.extra_data.data == NULL) {
1903 d_fprintf(stderr, "Did not get token data\n");
1907 if (!afs_settoken_str((char *)response.extra_data.data)) {
1908 d_fprintf(stderr, "Could not set token\n");
1912 d_printf("Successfully created AFS token\n");
1916 static bool wbinfo_klog(char *username)
1918 d_fprintf(stderr, "No AFS support compiled in.\n");
1923 /* Print domain users */
1925 static bool print_domain_users(const char *domain)
1927 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1929 uint32_t num_users = 0;
1930 const char **users = NULL;
1932 /* Send request to winbind daemon */
1934 /* '.' is the special sign for our own domain */
1935 if (domain && strcmp(domain, ".") == 0) {
1936 domain = get_winbind_domain();
1939 wbc_status = wbcListUsers(domain, &num_users, &users);
1940 if (!WBC_ERROR_IS_OK(wbc_status)) {
1944 for (i=0; i < num_users; i++) {
1945 d_printf("%s\n", users[i]);
1948 wbcFreeMemory(users);
1953 /* Print domain groups */
1955 static bool print_domain_groups(const char *domain)
1957 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1959 uint32_t num_groups = 0;
1960 const char **groups = NULL;
1962 /* Send request to winbind daemon */
1964 /* '.' is the special sign for our own domain */
1965 if (domain && strcmp(domain, ".") == 0) {
1966 domain = get_winbind_domain();
1969 wbc_status = wbcListGroups(domain, &num_groups, &groups);
1970 if (!WBC_ERROR_IS_OK(wbc_status)) {
1971 d_fprintf(stderr, "failed to call wbcListGroups: %s\n",
1972 wbcErrorString(wbc_status));
1976 for (i=0; i < num_groups; i++) {
1977 d_printf("%s\n", groups[i]);
1980 wbcFreeMemory(groups);
1985 /* Set the authorised user for winbindd access in secrets.tdb */
1987 static bool wbinfo_set_auth_user(char *username)
1989 d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1990 "See 'net help setauthuser' for details.\n");
1994 static void wbinfo_get_auth_user(void)
1996 d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1997 "See 'net help getauthuser' for details.\n");
2000 static bool wbinfo_ping(void)
2004 wbc_status = wbcPing();
2006 /* Display response */
2008 d_printf("Ping to winbindd %s\n",
2009 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
2011 return WBC_ERROR_IS_OK(wbc_status);
2014 static bool wbinfo_change_user_password(const char *username)
2017 char *old_password = NULL;
2018 char *new_password = NULL;
2019 TALLOC_CTX *frame = talloc_tos();
2021 old_password = wbinfo_prompt_pass(frame, "old", username);
2022 new_password = wbinfo_prompt_pass(frame, "new", username);
2024 wbc_status = wbcChangeUserPassword(username, old_password,new_password);
2026 /* Display response */
2028 d_printf("Password change for user %s %s\n", username,
2029 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
2031 return WBC_ERROR_IS_OK(wbc_status);
2037 OPT_SET_AUTH_USER = 1000,
2050 OPT_SET_UID_MAPPING,
2051 OPT_SET_GID_MAPPING,
2052 OPT_REMOVE_UID_MAPPING,
2053 OPT_REMOVE_GID_MAPPING,
2056 OPT_LIST_ALL_DOMAINS,
2057 OPT_LIST_OWN_DOMAIN,
2064 OPT_CHANGE_USER_PASSWORD,
2066 OPT_SID_TO_FULLNAME,
2075 int main(int argc, char **argv, char **envp)
2078 TALLOC_CTX *frame = talloc_stackframe();
2080 static char *string_arg;
2081 char *string_subarg = NULL;
2082 static char *opt_domain_name;
2084 int int_subarg = -1;
2086 bool verbose = false;
2087 bool use_ntlmv2 = false;
2088 bool use_lanman = false;
2089 char *logoff_user = getenv("USER");
2090 int logoff_uid = geteuid();
2092 struct poptOption long_options[] = {
2095 /* longName, shortName, argInfo, argPtr, value, descrip,
2098 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
2099 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
2100 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
2101 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
2102 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
2103 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
2104 { "sid-to-fullname", 0, POPT_ARG_STRING, &string_arg,
2105 OPT_SID_TO_FULLNAME, "Converts sid to fullname", "SID" },
2106 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
2107 { "lookup-sids", 0, POPT_ARG_STRING, &string_arg,
2108 OPT_LOOKUP_SIDS, "Converts SIDs to types and names",
2110 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
2111 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
2112 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
2113 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
2114 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
2115 "Get a new UID out of idmap" },
2116 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
2117 "Get a new GID out of idmap" },
2118 { "set-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_UID_MAPPING, "Create or modify uid to sid mapping in idmap", "UID,SID" },
2119 { "set-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_GID_MAPPING, "Create or modify gid to sid mapping in idmap", "GID,SID" },
2120 { "remove-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_UID_MAPPING, "Remove uid to sid mapping in idmap", "UID,SID" },
2121 { "remove-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_GID_MAPPING, "Remove gid to sid mapping in idmap", "GID,SID" },
2122 { "sids-to-unix-ids", 0, POPT_ARG_STRING, &string_arg,
2123 OPT_SIDS_TO_XIDS, "Translate SIDs to Unix IDs", "Sid-List" },
2124 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
2125 { "change-secret", 'c', POPT_ARG_NONE, 0, 'c', "Change shared secret" },
2126 { "ping-dc", 'P', POPT_ARG_NONE, 0, 'P',
2127 "Check the NETLOGON connection" },
2128 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
2129 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
2130 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
2131 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Deprecated command, see --online-status" },
2132 { "online-status", 0, POPT_ARG_NONE, 0, OPT_ONLINESTATUS, "Show whether domains are marked as online or offline"},
2133 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
2134 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
2135 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
2136 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
2137 { "user-sidinfo", 0, POPT_ARG_STRING, &string_arg, OPT_USER_SIDINFO, "Get user info from sid", "SID" },
2138 { "gid-info", 0, POPT_ARG_INT, &int_arg, OPT_GID_INFO, "Get group info from gid", "GID" },
2139 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
2140 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
2141 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
2142 { "sid-aliases", 0, POPT_ARG_STRING, &string_arg, OPT_SIDALIASES, "Get sid aliases", "SID" },
2143 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
2144 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
2145 { "pam-logon", 0, POPT_ARG_STRING, &string_arg, OPT_PAM_LOGON,
2146 "do a pam logon equivalent", "user%password" },
2147 { "logoff", 0, POPT_ARG_NONE, NULL, OPT_LOGOFF,
2148 "log off user", "uid" },
2149 { "logoff-user", 0, POPT_ARG_STRING, &logoff_user,
2150 OPT_LOGOFF_USER, "username to log off" },
2151 { "logoff-uid", 0, POPT_ARG_INT, &logoff_uid,
2152 OPT_LOGOFF_UID, "uid to log off" },
2153 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
2154 { "ccache-save", 0, POPT_ARG_STRING, &string_arg,
2155 OPT_CCACHE_SAVE, "Store user and password for ccache "
2156 "operation", "user%password" },
2157 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
2158 "Get a DC name for a foreign domain", "domainname" },
2159 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
2160 { "dc-info", 0, POPT_ARG_STRING, &string_arg, OPT_DC_INFO,
2161 "Find the currently known DCs", "domainname" },
2162 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
2163 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
2164 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
2165 #ifdef WITH_FAKE_KASERVER
2166 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
2169 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
2170 /* destroys wbinfo --help output */
2171 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
2173 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
2174 { "verbose", 0, POPT_ARG_NONE, 0, OPT_VERBOSE, "Print additional information per command", NULL },
2175 { "change-user-password", 0, POPT_ARG_STRING, &string_arg, OPT_CHANGE_USER_PASSWORD, "Change the password for a user", NULL },
2176 { "ntlmv2", 0, POPT_ARG_NONE, 0, OPT_NTLMV2, "Use NTLMv2 cryptography for user authentication", NULL},
2177 { "lanman", 0, POPT_ARG_NONE, 0, OPT_LANMAN, "Use lanman cryptography for user authentication", NULL},
2182 /* Samba client initialisation */
2188 pc = poptGetContext("wbinfo", argc, (const char **)argv,
2191 /* Parse command line options */
2194 poptPrintHelp(pc, stderr, 0);
2198 while((opt = poptGetNextOpt(pc)) != -1) {
2199 /* get the generic configuration parameters like --domain */
2213 poptFreeContext(pc);
2215 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2216 POPT_CONTEXT_KEEP_FIRST);
2218 while((opt = poptGetNextOpt(pc)) != -1) {
2221 if (!print_domain_users(opt_domain_name)) {
2223 "Error looking up domain users\n");
2228 if (!print_domain_groups(opt_domain_name)) {
2230 "Error looking up domain groups\n");
2235 if (!wbinfo_lookupsid(string_arg)) {
2237 "Could not lookup sid %s\n",
2242 case OPT_SID_TO_FULLNAME:
2243 if (!wbinfo_lookupsid_fullname(string_arg)) {
2244 d_fprintf(stderr, "Could not lookup sid %s\n",
2250 if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
2251 d_fprintf(stderr, "Could not lookup RIDs %s\n",
2256 case OPT_LOOKUP_SIDS:
2257 if (!wbinfo_lookup_sids(string_arg)) {
2258 d_fprintf(stderr, "Could not lookup SIDs %s\n",
2264 if (!wbinfo_lookupname(string_arg)) {
2265 d_fprintf(stderr, "Could not lookup name %s\n",
2271 if (!wbinfo_wins_byname(string_arg)) {
2273 "Could not lookup WINS by name %s\n",
2279 if (!wbinfo_wins_byip(string_arg)) {
2281 "Could not lookup WINS by IP %s\n",
2287 if (!wbinfo_uid_to_sid(int_arg)) {
2289 "Could not convert uid %d to sid\n",
2295 if (!wbinfo_gid_to_sid(int_arg)) {
2297 "Could not convert gid %d to sid\n",
2303 if (!wbinfo_sid_to_uid(string_arg)) {
2305 "Could not convert sid %s to uid\n",
2311 if (!wbinfo_sid_to_gid(string_arg)) {
2313 "Could not convert sid %s to gid\n",
2318 case OPT_ALLOCATE_UID:
2319 if (!wbinfo_allocate_uid()) {
2320 d_fprintf(stderr, "Could not allocate a uid\n");
2324 case OPT_ALLOCATE_GID:
2325 if (!wbinfo_allocate_gid()) {
2326 d_fprintf(stderr, "Could not allocate a gid\n");
2330 case OPT_SET_UID_MAPPING:
2331 if (!parse_mapping_arg(string_arg, &int_subarg,
2333 !wbinfo_set_uid_mapping(int_subarg, string_subarg))
2335 d_fprintf(stderr, "Could not create or modify "
2336 "uid to sid mapping\n");
2340 case OPT_SET_GID_MAPPING:
2341 if (!parse_mapping_arg(string_arg, &int_subarg,
2343 !wbinfo_set_gid_mapping(int_subarg, string_subarg))
2345 d_fprintf(stderr, "Could not create or modify "
2346 "gid to sid mapping\n");
2350 case OPT_REMOVE_UID_MAPPING:
2351 if (!parse_mapping_arg(string_arg, &int_subarg,
2353 !wbinfo_remove_uid_mapping(int_subarg,
2356 d_fprintf(stderr, "Could not remove uid to sid "
2361 case OPT_REMOVE_GID_MAPPING:
2362 if (!parse_mapping_arg(string_arg, &int_subarg,
2364 !wbinfo_remove_gid_mapping(int_subarg,
2367 d_fprintf(stderr, "Could not remove gid to sid "
2372 case OPT_SIDS_TO_XIDS:
2373 if (!wbinfo_sids_to_unix_ids(string_arg)) {
2374 d_fprintf(stderr, "wbinfo_sids_to_unix_ids "
2380 if (!wbinfo_check_secret(opt_domain_name)) {
2381 d_fprintf(stderr, "Could not check secret\n");
2386 if (!wbinfo_change_secret(opt_domain_name)) {
2387 d_fprintf(stderr, "Could not change secret\n");
2392 if (!wbinfo_ping_dc()) {
2397 if (!wbinfo_list_domains(false, verbose)) {
2399 "Could not list trusted domains\n");
2404 if (!wbinfo_show_sequence(opt_domain_name)) {
2406 "Could not show sequence numbers\n");
2410 case OPT_ONLINESTATUS:
2411 if (!wbinfo_show_onlinestatus(opt_domain_name)) {
2413 "Could not show online-status\n");
2418 if (!wbinfo_domain_info(string_arg)) {
2420 "Could not get domain info\n");
2425 if (!wbinfo_get_userinfo(string_arg)) {
2427 "Could not get info for user %s\n",
2432 case OPT_USER_SIDINFO:
2433 if ( !wbinfo_get_user_sidinfo(string_arg)) {
2435 "Could not get info for user "
2436 "sid %s\n", string_arg);
2441 if ( !wbinfo_get_uidinfo(int_arg)) {
2442 d_fprintf(stderr, "Could not get info for uid "
2447 case OPT_GROUP_INFO:
2448 if ( !wbinfo_get_groupinfo(string_arg)) {
2449 d_fprintf(stderr, "Could not get info for "
2450 "group %s\n", string_arg);
2455 if ( !wbinfo_get_gidinfo(int_arg)) {
2456 d_fprintf(stderr, "Could not get info for gid "
2462 if (!wbinfo_get_usergroups(string_arg)) {
2464 "Could not get groups for user %s\n",
2470 if (!wbinfo_get_usersids(string_arg)) {
2471 d_fprintf(stderr, "Could not get group SIDs "
2472 "for user SID %s\n",
2477 case OPT_USERDOMGROUPS:
2478 if (!wbinfo_get_userdomgroups(string_arg)) {
2479 d_fprintf(stderr, "Could not get user's domain "
2480 "groups for user SID %s\n",
2485 case OPT_SIDALIASES:
2486 if (!wbinfo_get_sidaliases(opt_domain_name,
2488 d_fprintf(stderr, "Could not get sid aliases "
2489 "for user SID %s\n", string_arg);
2494 bool got_error = false;
2496 if (!wbinfo_auth(string_arg)) {
2498 "Could not authenticate user "
2499 "%s with plaintext "
2500 "password\n", string_arg);
2504 if (!wbinfo_auth_crap(string_arg, use_ntlmv2,
2507 "Could not authenticate user "
2508 "%s with challenge/response\n",
2518 if (!wbinfo_pam_logon(string_arg)) {
2519 d_fprintf(stderr, "pam_logon failed for %s\n",
2528 wbc_status = wbcLogoffUser(logoff_user, logoff_uid,
2530 d_printf("Logoff %s (%d): %s\n", logoff_user,
2531 logoff_uid, wbcErrorString(wbc_status));
2535 uint32_t flags = WBFLAG_PAM_KRB5 |
2536 WBFLAG_PAM_CACHED_LOGIN |
2537 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
2538 WBFLAG_PAM_INFO3_TEXT |
2539 WBFLAG_PAM_CONTACT_TRUSTDOM;
2541 if (!wbinfo_auth_krb5(string_arg, "FILE",
2544 "Could not authenticate user "
2545 "[%s] with Kerberos "
2546 "(ccache: %s)\n", string_arg,
2553 if (!wbinfo_klog(string_arg)) {
2554 d_fprintf(stderr, "Could not klog user\n");
2559 if (!wbinfo_ping()) {
2560 d_fprintf(stderr, "could not ping winbindd!\n");
2564 case OPT_SET_AUTH_USER:
2565 if (!wbinfo_set_auth_user(string_arg)) {
2569 case OPT_GET_AUTH_USER:
2570 wbinfo_get_auth_user();
2573 case OPT_CCACHE_SAVE:
2574 if (!wbinfo_ccache_save(string_arg)) {
2579 if (!wbinfo_getdcname(string_arg)) {
2583 case OPT_DSGETDCNAME:
2584 if (!wbinfo_dsgetdcname(string_arg, 0)) {
2589 if (!wbinfo_dc_info(string_arg)) {
2593 case OPT_SEPARATOR: {
2594 const char sep = winbind_separator();
2598 d_printf("%c\n", sep);
2601 case OPT_LIST_ALL_DOMAINS:
2602 if (!wbinfo_list_domains(true, verbose)) {
2606 case OPT_LIST_OWN_DOMAIN:
2607 if (!wbinfo_list_own_domain()) {
2611 case OPT_CHANGE_USER_PASSWORD:
2612 if (!wbinfo_change_user_password(string_arg)) {
2614 "Could not change user password "
2615 "for user %s\n", string_arg);
2620 /* generic configuration options */
2621 case OPT_DOMAIN_NAME:
2625 case OPT_LOGOFF_USER:
2626 case OPT_LOGOFF_UID:
2629 d_fprintf(stderr, "Invalid option\n");
2630 poptPrintHelp(pc, stderr, 0);
2642 poptFreeContext(pc);