s3:lib/events: make use of tevent_common_loop_timer_delay()
[Samba/gebeck_regimport.git] / nsswitch / wbinfo.c
blobcd6de34fd5e9f3a8b409bee3bc6d4c0d183c2dc6
1 /*
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/>.
24 #include "includes.h"
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"
31 #else
32 #include "popt_common.h"
33 #endif
35 #ifdef DBGC_CLASS
36 #undef DBGC_CLASS
37 #define DBGC_CLASS DBGC_WINBIND
38 #endif
40 static struct wbcInterfaceDetails *init_interface_details(void)
42 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
43 static struct wbcInterfaceDetails *details;
45 if (details) {
46 return 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));
55 return details;
58 static char winbind_separator(void)
60 struct wbcInterfaceDetails *details;
61 static bool got_sep;
62 static char sep;
64 if (got_sep)
65 return sep;
67 details = init_interface_details();
69 if (!details) {
70 d_fprintf(stderr, "could not obtain winbind separator!\n");
71 return 0;
74 sep = details->winbind_separator;
75 got_sep = true;
77 if (!sep) {
78 d_fprintf(stderr, "winbind separator was NULL!\n");
79 return 0;
82 return sep;
85 static const char *get_winbind_domain(void)
87 static struct wbcInterfaceDetails *details;
89 details = init_interface_details();
91 if (!details) {
92 d_fprintf(stderr, "could not obtain winbind domain name!\n");
93 return 0;
96 return details->netbios_domain;
99 static const char *get_winbind_netbios_name(void)
101 static struct wbcInterfaceDetails *details;
103 details = init_interface_details();
105 if (!details) {
106 d_fprintf(stderr, "could not obtain winbind netbios name!\n");
107 return 0;
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,
117 fstring user)
120 char *p = strchr(domuser,winbind_separator());
122 if (!p) {
123 /* Maybe it was a UPN? */
124 p = strchr(domuser, '@');
125 if (p != NULL) {
126 fstrcpy(domain, "");
127 fstrcpy(user, domuser);
128 return true;
131 fstrcpy(user, domuser);
132 fstrcpy(domain, get_winbind_domain());
133 return true;
136 fstrcpy(user, p+1);
137 fstrcpy(domain, domuser);
138 domain[PTR_DIFF(p, domuser)] = 0;
140 return true;
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)
147 char *tmp, *endptr;
149 if (!arg || !*arg)
150 return false;
152 tmp = strtok(arg, ",");
153 *sid = strtok(NULL, ",");
155 if (!tmp || !*tmp || !*sid || !**sid)
156 return false;
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')
163 return false;
165 return true;
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));
179 return false;
182 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
183 pwd->pw_name,
184 pwd->pw_passwd,
185 (unsigned int)pwd->pw_uid,
186 (unsigned int)pwd->pw_gid,
187 pwd->pw_gecos,
188 pwd->pw_dir,
189 pwd->pw_shell);
191 wbcFreeMemory(pwd);
193 return true;
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));
206 return false;
209 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
210 pwd->pw_name,
211 pwd->pw_passwd,
212 (unsigned int)pwd->pw_uid,
213 (unsigned int)pwd->pw_gid,
214 pwd->pw_gecos,
215 pwd->pw_dir,
216 pwd->pw_shell);
218 wbcFreeMemory(pwd);
220 return true;
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));
234 return false;
237 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
238 pwd->pw_name,
239 pwd->pw_passwd,
240 (unsigned int)pwd->pw_uid,
241 (unsigned int)pwd->pw_gid,
242 pwd->pw_gecos,
243 pwd->pw_dir,
244 pwd->pw_shell);
246 wbcFreeMemory(pwd);
248 return true;
252 /* pull grent for a given group */
253 static bool wbinfo_get_groupinfo(const char *group)
255 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
256 struct group *grp;
257 char **mem;
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));
263 return false;
266 d_printf("%s:%s:%u:",
267 grp->gr_name,
268 grp->gr_passwd,
269 (unsigned int)grp->gr_gid);
271 mem = grp->gr_mem;
272 while (*mem != NULL) {
273 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
274 mem += 1;
276 d_printf("\n");
278 wbcFreeMemory(grp);
280 return true;
283 /* pull grent for a given gid */
284 static bool wbinfo_get_gidinfo(int gid)
286 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
287 struct group *grp;
288 char **mem;
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));
294 return false;
297 d_printf("%s:%s:%u:",
298 grp->gr_name,
299 grp->gr_passwd,
300 (unsigned int)grp->gr_gid);
302 mem = grp->gr_mem;
303 while (*mem != NULL) {
304 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
305 mem += 1;
307 d_printf("\n");
309 wbcFreeMemory(grp);
311 return true;
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;
319 uint32_t num_groups;
320 uint32_t i;
321 gid_t *groups = NULL;
323 /* Send request */
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));
329 return false;
332 for (i = 0; i < num_groups; i++) {
333 d_printf("%d\n", (int)groups[i]);
336 wbcFreeMemory(groups);
338 return true;
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;
346 uint32_t num_sids;
347 uint32_t i;
348 struct wbcDomainSid user_sid, *sids = NULL;
350 /* Send request */
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));
356 return false;
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));
363 return false;
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);
372 wbcFreeMemory(sids);
374 return true;
377 static bool wbinfo_get_userdomgroups(const char *user_sid_str)
379 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
380 uint32_t num_sids;
381 uint32_t i;
382 struct wbcDomainSid user_sid, *sids = NULL;
384 /* Send request */
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));
390 return false;
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));
397 return false;
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);
406 wbcFreeMemory(sids);
408 return true;
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;
416 uint32_t i;
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];
422 /* Send request */
423 if ((domain == NULL) || (strequal(domain, ".")) ||
424 (domain[0] == '\0')) {
425 domain = get_winbind_domain();
428 /* Send request */
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));
434 goto done;
436 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
437 if (!WBC_ERROR_IS_OK(wbc_status)) {
438 goto done;
441 wbc_status = wbcGetSidAliases(&dinfo->sid, &user_sid, 1,
442 &alias_rids, &num_alias_rids);
443 if (!WBC_ERROR_IS_OK(wbc_status)) {
444 goto done;
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);
455 done:
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;
466 char *ip = NULL;
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));
472 return false;
475 /* Display response */
477 d_printf("%s\n", ip);
479 wbcFreeMemory(ip);
481 return true;
484 /* Convert IP to NetBIOS name */
486 static bool wbinfo_wins_byip(const char *ip)
488 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
489 char *name = NULL;
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));
495 return false;
498 /* Display response */
500 d_printf("%s\n", name);
502 wbcFreeMemory(name);
504 return true;
507 /* List all/trusted domains */
509 static bool wbinfo_list_domains(bool list_all_domains, bool verbose)
511 struct wbcDomainInfo *domain_list = NULL;
512 size_t num_domains;
513 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
514 bool print_all = !list_all_domains && verbose;
515 int i;
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));
521 return false;
524 if (print_all) {
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++) {
531 if (print_all) {
532 d_printf("%-16s", domain_list[i].short_name);
533 } else {
534 d_printf("%s", domain_list[i].short_name);
535 d_printf("\n");
536 continue;
539 d_printf("%-65s", domain_list[i].dns_name);
541 switch(domain_list[i].trust_type) {
542 case WBC_DOMINFO_TRUSTTYPE_NONE:
543 d_printf("None ");
544 break;
545 case WBC_DOMINFO_TRUSTTYPE_FOREST:
546 d_printf("Forest ");
547 break;
548 case WBC_DOMINFO_TRUSTTYPE_EXTERNAL:
549 d_printf("External ");
550 break;
551 case WBC_DOMINFO_TRUSTTYPE_IN_FOREST:
552 d_printf("In-Forest ");
553 break;
556 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_TRANSITIVE) {
557 d_printf("Yes ");
558 } else {
559 d_printf("No ");
562 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_INCOMING) {
563 d_printf("Yes ");
564 } else {
565 d_printf("No ");
568 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_OUTGOING) {
569 d_printf("Yes ");
570 } else {
571 d_printf("No ");
574 d_printf("\n");
577 wbcFreeMemory(domain_list);
579 return true;
582 /* List own domain */
584 static bool wbinfo_list_own_domain(void)
586 d_printf("%s\n", get_winbind_domain());
588 return true;
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");
596 return false;
599 /* show sequence numbers */
600 static bool wbinfo_show_onlinestatus(const char *domain)
602 struct wbcDomainInfo *domain_list = NULL;
603 size_t num_domains;
604 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
605 int i;
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));
611 return false;
614 for (i=0; i<num_domains; i++) {
615 bool is_offline;
617 if (domain) {
618 if (!strequal(domain_list[i].short_name, domain)) {
619 continue;
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);
633 return true;
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();
649 /* Send request */
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));
655 return false;
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) ?
671 "Yes" : "No");
673 d_printf("Primary : %s\n",
674 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_PRIMARY) ?
675 "Yes" : "No");
677 wbcFreeMemory(dinfo);
679 return true;
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);
693 /* Send request */
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);
698 return false;
701 /* Display response */
703 d_printf("%s\n", response.data.dc_name);
705 return true;
708 /* Find a DC */
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;
713 char *str = NULL;
715 wbc_status = wbcLookupDomainControllerEx(domain_name, NULL, NULL,
716 flags | DS_DIRECTORY_SERVICE_REQUIRED,
717 &dc_info);
718 if (!WBC_ERROR_IS_OK(wbc_status)) {
719 printf("Could not find dc for %s\n", domain_name);
720 return false;
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);
735 return true;
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;
746 if (domain) {
747 domain_name = domain;
748 } else {
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",
755 domain_name,
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));
766 return false;
769 return true;
772 /* Find the currently connected DCs */
774 static bool wbinfo_dc_info(const char *domain_name)
776 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
777 size_t i, num_dcs;
778 const char **dc_names, **dc_ips;
780 wbc_status = wbcDcInfo(domain_name, &num_dcs,
781 &dc_names, &dc_ips);
782 if (!WBC_ERROR_IS_OK(wbc_status)) {
783 printf("Could not find dc info %s\n",
784 domain_name ? domain_name : "our domain");
785 return false;
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);
794 return true;
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;
805 if (domain) {
806 domain_name = domain;
807 } else {
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",
814 domain_name,
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));
825 return false;
828 return true;
831 /* Check DC connection */
833 static bool wbinfo_ping_dc(void)
835 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
836 struct wbcAuthErrorInfo *error = NULL;
837 char *dcname = 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);
849 return false;
851 if (!WBC_ERROR_IS_OK(wbc_status)) {
852 d_fprintf(stderr, "failed to call wbcPingDc: %s\n",
853 wbcErrorString(wbc_status));
854 return false;
857 return true;
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];
868 /* Send request */
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));
874 return false;
877 wbcSidToStringBuf(&sid, sid_str, sizeof(sid_str));
879 /* Display response */
881 d_printf("%s\n", sid_str);
883 return true;
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];
894 /* Send request */
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));
900 return false;
903 wbcSidToStringBuf(&sid, sid_str, sizeof(sid_str));
905 /* Display response */
907 d_printf("%s\n", sid_str);
909 return true;
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;
918 uid_t uid;
920 /* Send request */
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));
926 return false;
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));
933 return false;
936 /* Display response */
938 d_printf("%d\n", (int)uid);
940 return true;
943 static bool wbinfo_sid_to_gid(const char *sid_str)
945 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
946 struct wbcDomainSid sid;
947 gid_t gid;
949 /* Send request */
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));
955 return false;
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));
962 return false;
965 /* Display response */
967 d_printf("%d\n", (int)gid);
969 return true;
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;
977 int i, num_sids;
978 const char *p;
979 wbcErr wbc_status;
982 num_sids = 0;
983 sids = NULL;
984 p = arg;
986 while (next_token(&p, sidstr, LIST_SEP, sizeof(sidstr))) {
987 sids = talloc_realloc(talloc_tos(), sids, struct wbcDomainSid,
988 num_sids+1);
989 if (sids == NULL) {
990 d_fprintf(stderr, "talloc failed\n");
991 return false;
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));
997 TALLOC_FREE(sids);
998 return false;
1000 num_sids += 1;
1003 unix_ids = talloc_array(talloc_tos(), struct wbcUnixId, num_sids);
1004 if (unix_ids == NULL) {
1005 TALLOC_FREE(sids);
1006 return false;
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));
1013 TALLOC_FREE(sids);
1014 return false;
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);
1024 break;
1025 case WBC_ID_TYPE_GID:
1026 d_printf("%s -> gid %d\n", sidstr, unix_ids[i].id.gid);
1027 break;
1028 case WBC_ID_TYPE_BOTH:
1029 d_printf("%s -> uid/gid %d\n", sidstr, unix_ids[i].id.uid);
1030 break;
1031 default:
1032 d_printf("%s -> unmapped\n", sidstr);
1033 break;
1037 TALLOC_FREE(sids);
1038 TALLOC_FREE(unix_ids);
1040 return true;
1043 static bool wbinfo_allocate_uid(void)
1045 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1046 uid_t uid;
1048 /* Send request */
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));
1054 return false;
1057 /* Display response */
1059 d_printf("New uid: %u\n", (unsigned int)uid);
1061 return true;
1064 static bool wbinfo_allocate_gid(void)
1066 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1067 gid_t gid;
1069 /* Send request */
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));
1075 return false;
1078 /* Display response */
1080 d_printf("New gid: %u\n", (unsigned int)gid);
1082 return true;
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;
1090 /* Send request */
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));
1096 return false;
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));
1103 return false;
1106 /* Display response */
1108 d_printf("uid %u now mapped to sid %s\n",
1109 (unsigned int)uid, sid_str);
1111 return true;
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;
1119 /* Send request */
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));
1125 return false;
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));
1132 return false;
1135 /* Display response */
1137 d_printf("gid %u now mapped to sid %s\n",
1138 (unsigned int)gid, sid_str);
1140 return true;
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;
1148 /* Send request */
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));
1154 return false;
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));
1161 return false;
1164 /* Display response */
1166 d_printf("Removed uid %u to sid %s mapping\n",
1167 (unsigned int)uid, sid_str);
1169 return true;
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;
1177 /* Send request */
1179 wbc_status = wbcStringToSid(sid_str, &sid);
1180 if (!WBC_ERROR_IS_OK(wbc_status)) {
1181 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1182 wbcErrorString(wbc_status));
1183 return false;
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));
1190 return false;
1193 /* Display response */
1195 d_printf("Removed gid %u to sid %s mapping\n",
1196 (unsigned int)gid, sid_str);
1198 return true;
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;
1207 char *domain;
1208 char *name;
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));
1217 return false;
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));
1224 return false;
1227 /* Display response */
1229 d_printf("%s%c%s %d\n",
1230 domain, winbind_separator(), name, type);
1232 wbcFreeMemory(domain);
1233 wbcFreeMemory(name);
1235 return true;
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;
1244 char *domain;
1245 char *name;
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));
1254 return false;
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));
1261 return false;
1264 /* Display response */
1266 d_printf("%s%c%s %d\n",
1267 domain, winbind_separator(), name, type);
1269 wbcFreeMemory(domain);
1270 wbcFreeMemory(name);
1272 return true;
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;
1284 size_t i;
1285 int num_rids;
1286 uint32_t *rids = NULL;
1287 const char *p;
1288 char *ridstr;
1289 TALLOC_CTX *mem_ctx = NULL;
1290 bool ret = false;
1292 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
1293 domain = get_winbind_domain();
1296 /* Send request */
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));
1302 goto done;
1305 mem_ctx = talloc_new(NULL);
1306 if (mem_ctx == NULL) {
1307 d_printf("talloc_new failed\n");
1308 goto done;
1311 num_rids = 0;
1312 rids = NULL;
1313 p = arg;
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);
1318 if (rids == NULL) {
1319 d_printf("talloc_realloc failed\n");
1321 rids[num_rids] = rid;
1322 num_rids += 1;
1325 if (rids == NULL) {
1326 d_printf("no rids\n");
1327 goto done;
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));
1335 goto done;
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]));
1345 ret = true;
1346 done:
1347 wbcFreeMemory(dinfo);
1348 wbcFreeMemory(domain_name);
1349 wbcFreeMemory(names);
1350 wbcFreeMemory(types);
1351 TALLOC_FREE(mem_ctx);
1352 return ret;
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;
1361 int num_domains;
1362 int i, num_sids;
1363 const char *p;
1364 wbcErr wbc_status;
1367 num_sids = 0;
1368 sids = NULL;
1369 p = arg;
1371 while (next_token(&p, sidstr, LIST_SEP, sizeof(sidstr))) {
1372 sids = talloc_realloc(talloc_tos(), sids, struct wbcDomainSid,
1373 num_sids+1);
1374 if (sids == NULL) {
1375 d_fprintf(stderr, "talloc failed\n");
1376 return false;
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));
1382 TALLOC_FREE(sids);
1383 return false;
1385 num_sids += 1;
1388 wbc_status = wbcLookupSids(sids, num_sids, &domains, &num_domains,
1389 &names);
1390 if (!WBC_ERROR_IS_OK(wbc_status)) {
1391 d_fprintf(stderr, "wbcLookupSids failed: %s\n",
1392 wbcErrorString(wbc_status));
1393 TALLOC_FREE(sids);
1394 return false;
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);
1406 return true;
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,
1423 account_name);
1425 wbc_status = wbcLookupName(domain_name, account_name,
1426 &sid, &type);
1427 if (!WBC_ERROR_IS_OK(wbc_status)) {
1428 d_fprintf(stderr, "failed to call wbcLookupName: %s\n",
1429 wbcErrorString(wbc_status));
1430 return false;
1433 wbcSidToStringBuf(&sid, sid_str, sizeof(sid_str));
1435 /* Display response */
1437 d_printf("%s %s (%d)\n", sid_str, wbcSidTypeString(type), type);
1439 return true;
1442 static char *wbinfo_prompt_pass(TALLOC_CTX *mem_ctx,
1443 const char *prefix,
1444 const char *username)
1446 char *prompt;
1447 char buf[1024] = {0};
1448 int rc;
1450 prompt = talloc_asprintf(mem_ctx, "Enter %s's ", username);
1451 if (!prompt) {
1452 return NULL;
1454 if (prefix) {
1455 prompt = talloc_asprintf_append(prompt, "%s ", prefix);
1456 if (!prompt) {
1457 return NULL;
1460 prompt = talloc_asprintf_append(prompt, "password: ");
1461 if (!prompt) {
1462 return NULL;
1465 rc = samba_getpass(prompt, buf, sizeof(buf), false, false);
1466 TALLOC_FREE(prompt);
1467 if (rc < 0) {
1468 return NULL;
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;
1479 char *s = NULL;
1480 char *p = NULL;
1481 char *password = NULL;
1482 char *name = NULL;
1483 char *local_cctype = NULL;
1484 uid_t uid;
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) {
1492 return false;
1495 if ((p = strchr(s, '%')) != NULL) {
1496 *p = 0;
1497 p++;
1498 password = talloc_strdup(frame, p);
1499 } else {
1500 password = wbinfo_prompt_pass(frame, NULL, username);
1503 local_cctype = talloc_strdup(frame, cctype);
1505 name = s;
1507 uid = geteuid();
1509 params.username = name;
1510 params.password = password;
1511 params.num_blobs = 0;
1512 params.blobs = NULL;
1514 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1515 &params.blobs,
1516 "flags",
1518 (uint8_t *)&flags,
1519 sizeof(flags));
1520 if (!WBC_ERROR_IS_OK(wbc_status)) {
1521 d_fprintf(stderr, "failed to call wbcAddNamedBlob: %s\n",
1522 wbcErrorString(wbc_status));
1523 goto done;
1526 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1527 &params.blobs,
1528 "user_uid",
1530 (uint8_t *)&uid,
1531 sizeof(uid));
1532 if (!WBC_ERROR_IS_OK(wbc_status)) {
1533 d_fprintf(stderr, "failed to call wbcAddNamedBlob: %s\n",
1534 wbcErrorString(wbc_status));
1535 goto done;
1538 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1539 &params.blobs,
1540 "krb5_cc_type",
1542 (uint8_t *)local_cctype,
1543 strlen(cctype)+1);
1544 if (!WBC_ERROR_IS_OK(wbc_status)) {
1545 d_fprintf(stderr, "failed to call wbcAddNamedBlob: %s\n",
1546 wbcErrorString(wbc_status));
1547 goto done;
1550 wbc_status = wbcLogonUser(&params, &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",
1555 cctype);
1557 if (error) {
1558 d_fprintf(stderr,
1559 "error code was %s (0x%x)\nerror message was: %s\n",
1560 error->nt_string,
1561 error->nt_status,
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");
1574 if (info) {
1575 int i;
1576 for (i=0; i < info->num_blobs; i++) {
1577 if (strequal(info->blobs[i].name,
1578 "krb5ccname")) {
1579 d_printf("credentials were put "
1580 "in: %s\n",
1581 (const char *)
1582 info->blobs[i].blob.data);
1583 break;
1586 } else {
1587 d_printf("no credentials cached\n");
1590 done:
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;
1602 char *s = NULL;
1603 char *p = NULL;
1604 char *password = NULL;
1605 char *name = NULL;
1606 TALLOC_CTX *frame = talloc_tos();
1608 if ((s = talloc_strdup(frame, username)) == NULL) {
1609 return false;
1612 if ((p = strchr(s, '%')) != NULL) {
1613 *p = 0;
1614 p++;
1615 password = talloc_strdup(frame, p);
1616 } else {
1617 password = wbinfo_prompt_pass(frame, NULL, username);
1620 name = s;
1622 wbc_status = wbcAuthenticateUser(name, password);
1624 d_printf("plaintext password authentication %s\n",
1625 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1627 #if 0
1628 if (response.data.auth.nt_status)
1629 d_fprintf(stderr,
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);
1634 #endif
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;
1649 fstring name_user;
1650 fstring name_domain;
1651 char *pass;
1652 char *p;
1653 TALLOC_CTX *frame = talloc_tos();
1655 p = strchr(username, '%');
1657 if (p) {
1658 *p = 0;
1659 pass = talloc_strdup(frame, p + 1);
1660 } else {
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;
1670 params.flags = 0;
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);
1678 if (use_ntlmv2) {
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,
1690 &server_chal,
1691 &names_blob,
1692 &lm, &nt, NULL, NULL)) {
1693 data_blob_free(&names_blob);
1694 data_blob_free(&server_chal);
1695 TALLOC_FREE(pass);
1696 return false;
1698 data_blob_free(&names_blob);
1699 data_blob_free(&server_chal);
1701 } else {
1702 if (use_lanman) {
1703 bool ok;
1704 lm = data_blob(NULL, 24);
1705 ok = SMBencrypt(pass,
1706 params.password.response.challenge,
1707 lm.data);
1708 if (!ok) {
1709 data_blob_free(&lm);
1712 nt = data_blob(NULL, 24);
1713 SMBNTencrypt(pass, params.password.response.challenge,
1714 nt.data);
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(&params, &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) {
1730 d_fprintf(stderr,
1731 "error code was %s (0x%x)\nerror message was: %s\n",
1732 err->nt_string,
1733 err->nt_status,
1734 err->display_string);
1735 wbcFreeMemory(err);
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;
1753 char *s = NULL;
1754 char *p = NULL;
1755 TALLOC_CTX *frame = talloc_tos();
1756 uint32_t flags;
1757 uint32_t uid;
1759 ZERO_STRUCT(params);
1761 if ((s = talloc_strdup(frame, username)) == NULL) {
1762 return false;
1765 if ((p = strchr(s, '%')) != NULL) {
1766 *p = 0;
1767 p++;
1768 params.password = talloc_strdup(frame, p);
1769 } else {
1770 params.password = wbinfo_prompt_pass(frame, NULL, username);
1772 params.username = s;
1774 flags = WBFLAG_PAM_CACHED_LOGIN;
1776 wbc_status = wbcAddNamedBlob(&params.num_blobs, &params.blobs,
1777 "flags", 0,
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));
1782 return false;
1785 uid = getuid();
1787 wbc_status = wbcAddNamedBlob(&params.num_blobs, &params.blobs,
1788 "user_uid", 0,
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));
1793 return false;
1796 wbc_status = wbcLogonUser(&params, 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)) {
1804 d_fprintf(stderr,
1805 "error code was %s (0x%x)\nerror message was: %s\n",
1806 error->nt_string,
1807 (int)error->nt_status,
1808 error->display_string);
1809 wbcFreeMemory(error);
1810 return false;
1812 return true;
1815 /* Save creds with winbind */
1817 static bool wbinfo_ccache_save(char *username)
1819 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1820 char *s = NULL;
1821 char *p = NULL;
1822 char *password = NULL;
1823 char *name = NULL;
1824 TALLOC_CTX *frame = talloc_stackframe();
1826 s = talloc_strdup(frame, username);
1827 if (s == NULL) {
1828 return false;
1831 p = strchr(s, '%');
1832 if (p != NULL) {
1833 *p = 0;
1834 p++;
1835 password = talloc_strdup(frame, p);
1836 } else {
1837 password = wbinfo_prompt_pass(frame, NULL, username);
1840 name = s;
1842 wbc_status = wbcCredentialSave(name, password);
1844 d_printf("saving creds %s\n",
1845 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1847 TALLOC_FREE(frame);
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;
1859 NSS_STATUS result;
1860 char *p;
1862 /* Send off request */
1864 ZERO_STRUCT(request);
1865 ZERO_STRUCT(response);
1867 p = strchr(username, '%');
1869 if (p) {
1870 *p = 0;
1871 fstrcpy(request.data.auth.user, username);
1872 fstrcpy(request.data.auth.pass, p + 1);
1873 *p = '%';
1874 } else {
1875 fstrcpy(request.data.auth.user, username);
1876 (void) samba_getpass("Password: ",
1877 request.data.auth.pass,
1878 sizeof(request.data.auth.pass),
1879 false, false);
1882 request.flags |= WBFLAG_PAM_AFS_TOKEN;
1884 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request,
1885 &response);
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)
1893 d_fprintf(stderr,
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)
1900 return false;
1902 if (response.extra_data.data == NULL) {
1903 d_fprintf(stderr, "Did not get token data\n");
1904 return false;
1907 if (!afs_settoken_str((char *)response.extra_data.data)) {
1908 d_fprintf(stderr, "Could not set token\n");
1909 return false;
1912 d_printf("Successfully created AFS token\n");
1913 return true;
1915 #else
1916 static bool wbinfo_klog(char *username)
1918 d_fprintf(stderr, "No AFS support compiled in.\n");
1919 return false;
1921 #endif
1923 /* Print domain users */
1925 static bool print_domain_users(const char *domain)
1927 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1928 uint32_t i;
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)) {
1941 return false;
1944 for (i=0; i < num_users; i++) {
1945 d_printf("%s\n", users[i]);
1948 wbcFreeMemory(users);
1950 return true;
1953 /* Print domain groups */
1955 static bool print_domain_groups(const char *domain)
1957 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1958 uint32_t i;
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));
1973 return false;
1976 for (i=0; i < num_groups; i++) {
1977 d_printf("%s\n", groups[i]);
1980 wbcFreeMemory(groups);
1982 return true;
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");
1991 return false;
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)
2002 wbcErr wbc_status;
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)
2016 wbcErr wbc_status;
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);
2034 /* Main program */
2036 enum {
2037 OPT_SET_AUTH_USER = 1000,
2038 OPT_GET_AUTH_USER,
2039 OPT_DOMAIN_NAME,
2040 OPT_SEQUENCE,
2041 OPT_GETDCNAME,
2042 OPT_DSGETDCNAME,
2043 OPT_DC_INFO,
2044 OPT_USERDOMGROUPS,
2045 OPT_SIDALIASES,
2046 OPT_USERSIDS,
2047 OPT_LOOKUP_SIDS,
2048 OPT_ALLOCATE_UID,
2049 OPT_ALLOCATE_GID,
2050 OPT_SET_UID_MAPPING,
2051 OPT_SET_GID_MAPPING,
2052 OPT_REMOVE_UID_MAPPING,
2053 OPT_REMOVE_GID_MAPPING,
2054 OPT_SIDS_TO_XIDS,
2055 OPT_SEPARATOR,
2056 OPT_LIST_ALL_DOMAINS,
2057 OPT_LIST_OWN_DOMAIN,
2058 OPT_UID_INFO,
2059 OPT_USER_SIDINFO,
2060 OPT_GROUP_INFO,
2061 OPT_GID_INFO,
2062 OPT_VERBOSE,
2063 OPT_ONLINESTATUS,
2064 OPT_CHANGE_USER_PASSWORD,
2065 OPT_CCACHE_SAVE,
2066 OPT_SID_TO_FULLNAME,
2067 OPT_NTLMV2,
2068 OPT_PAM_LOGON,
2069 OPT_LOGOFF,
2070 OPT_LOGOFF_USER,
2071 OPT_LOGOFF_UID,
2072 OPT_LANMAN
2075 int main(int argc, char **argv, char **envp)
2077 int opt;
2078 TALLOC_CTX *frame = talloc_stackframe();
2079 poptContext pc;
2080 static char *string_arg;
2081 char *string_subarg = NULL;
2082 static char *opt_domain_name;
2083 static int int_arg;
2084 int int_subarg = -1;
2085 int result = 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[] = {
2093 POPT_AUTOHELP
2095 /* longName, shortName, argInfo, argPtr, value, descrip,
2096 argDesc */
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",
2109 "Sid-List"},
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" },
2167 #endif
2168 #ifdef HAVE_KRB5
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" }, */
2172 #endif
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},
2178 POPT_COMMON_VERSION
2179 POPT_TABLEEND
2182 /* Samba client initialisation */
2183 load_case_tables();
2186 /* Parse options */
2188 pc = poptGetContext("wbinfo", argc, (const char **)argv,
2189 long_options, 0);
2191 /* Parse command line options */
2193 if (argc == 1) {
2194 poptPrintHelp(pc, stderr, 0);
2195 return 1;
2198 while((opt = poptGetNextOpt(pc)) != -1) {
2199 /* get the generic configuration parameters like --domain */
2200 switch (opt) {
2201 case OPT_VERBOSE:
2202 verbose = true;
2203 break;
2204 case OPT_NTLMV2:
2205 use_ntlmv2 = true;
2206 break;
2207 case OPT_LANMAN:
2208 use_lanman = true;
2209 break;
2213 poptFreeContext(pc);
2215 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2216 POPT_CONTEXT_KEEP_FIRST);
2218 while((opt = poptGetNextOpt(pc)) != -1) {
2219 switch (opt) {
2220 case 'u':
2221 if (!print_domain_users(opt_domain_name)) {
2222 d_fprintf(stderr,
2223 "Error looking up domain users\n");
2224 goto done;
2226 break;
2227 case 'g':
2228 if (!print_domain_groups(opt_domain_name)) {
2229 d_fprintf(stderr,
2230 "Error looking up domain groups\n");
2231 goto done;
2233 break;
2234 case 's':
2235 if (!wbinfo_lookupsid(string_arg)) {
2236 d_fprintf(stderr,
2237 "Could not lookup sid %s\n",
2238 string_arg);
2239 goto done;
2241 break;
2242 case OPT_SID_TO_FULLNAME:
2243 if (!wbinfo_lookupsid_fullname(string_arg)) {
2244 d_fprintf(stderr, "Could not lookup sid %s\n",
2245 string_arg);
2246 goto done;
2248 break;
2249 case 'R':
2250 if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
2251 d_fprintf(stderr, "Could not lookup RIDs %s\n",
2252 string_arg);
2253 goto done;
2255 break;
2256 case OPT_LOOKUP_SIDS:
2257 if (!wbinfo_lookup_sids(string_arg)) {
2258 d_fprintf(stderr, "Could not lookup SIDs %s\n",
2259 string_arg);
2260 goto done;
2262 break;
2263 case 'n':
2264 if (!wbinfo_lookupname(string_arg)) {
2265 d_fprintf(stderr, "Could not lookup name %s\n",
2266 string_arg);
2267 goto done;
2269 break;
2270 case 'N':
2271 if (!wbinfo_wins_byname(string_arg)) {
2272 d_fprintf(stderr,
2273 "Could not lookup WINS by name %s\n",
2274 string_arg);
2275 goto done;
2277 break;
2278 case 'I':
2279 if (!wbinfo_wins_byip(string_arg)) {
2280 d_fprintf(stderr,
2281 "Could not lookup WINS by IP %s\n",
2282 string_arg);
2283 goto done;
2285 break;
2286 case 'U':
2287 if (!wbinfo_uid_to_sid(int_arg)) {
2288 d_fprintf(stderr,
2289 "Could not convert uid %d to sid\n",
2290 int_arg);
2291 goto done;
2293 break;
2294 case 'G':
2295 if (!wbinfo_gid_to_sid(int_arg)) {
2296 d_fprintf(stderr,
2297 "Could not convert gid %d to sid\n",
2298 int_arg);
2299 goto done;
2301 break;
2302 case 'S':
2303 if (!wbinfo_sid_to_uid(string_arg)) {
2304 d_fprintf(stderr,
2305 "Could not convert sid %s to uid\n",
2306 string_arg);
2307 goto done;
2309 break;
2310 case 'Y':
2311 if (!wbinfo_sid_to_gid(string_arg)) {
2312 d_fprintf(stderr,
2313 "Could not convert sid %s to gid\n",
2314 string_arg);
2315 goto done;
2317 break;
2318 case OPT_ALLOCATE_UID:
2319 if (!wbinfo_allocate_uid()) {
2320 d_fprintf(stderr, "Could not allocate a uid\n");
2321 goto done;
2323 break;
2324 case OPT_ALLOCATE_GID:
2325 if (!wbinfo_allocate_gid()) {
2326 d_fprintf(stderr, "Could not allocate a gid\n");
2327 goto done;
2329 break;
2330 case OPT_SET_UID_MAPPING:
2331 if (!parse_mapping_arg(string_arg, &int_subarg,
2332 &string_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");
2337 goto done;
2339 break;
2340 case OPT_SET_GID_MAPPING:
2341 if (!parse_mapping_arg(string_arg, &int_subarg,
2342 &string_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");
2347 goto done;
2349 break;
2350 case OPT_REMOVE_UID_MAPPING:
2351 if (!parse_mapping_arg(string_arg, &int_subarg,
2352 &string_subarg) ||
2353 !wbinfo_remove_uid_mapping(int_subarg,
2354 string_subarg))
2356 d_fprintf(stderr, "Could not remove uid to sid "
2357 "mapping\n");
2358 goto done;
2360 break;
2361 case OPT_REMOVE_GID_MAPPING:
2362 if (!parse_mapping_arg(string_arg, &int_subarg,
2363 &string_subarg) ||
2364 !wbinfo_remove_gid_mapping(int_subarg,
2365 string_subarg))
2367 d_fprintf(stderr, "Could not remove gid to sid "
2368 "mapping\n");
2369 goto done;
2371 break;
2372 case OPT_SIDS_TO_XIDS:
2373 if (!wbinfo_sids_to_unix_ids(string_arg)) {
2374 d_fprintf(stderr, "wbinfo_sids_to_unix_ids "
2375 "failed\n");
2376 goto done;
2378 break;
2379 case 't':
2380 if (!wbinfo_check_secret(opt_domain_name)) {
2381 d_fprintf(stderr, "Could not check secret\n");
2382 goto done;
2384 break;
2385 case 'c':
2386 if (!wbinfo_change_secret(opt_domain_name)) {
2387 d_fprintf(stderr, "Could not change secret\n");
2388 goto done;
2390 break;
2391 case 'P':
2392 if (!wbinfo_ping_dc()) {
2393 goto done;
2395 break;
2396 case 'm':
2397 if (!wbinfo_list_domains(false, verbose)) {
2398 d_fprintf(stderr,
2399 "Could not list trusted domains\n");
2400 goto done;
2402 break;
2403 case OPT_SEQUENCE:
2404 if (!wbinfo_show_sequence(opt_domain_name)) {
2405 d_fprintf(stderr,
2406 "Could not show sequence numbers\n");
2407 goto done;
2409 break;
2410 case OPT_ONLINESTATUS:
2411 if (!wbinfo_show_onlinestatus(opt_domain_name)) {
2412 d_fprintf(stderr,
2413 "Could not show online-status\n");
2414 goto done;
2416 break;
2417 case 'D':
2418 if (!wbinfo_domain_info(string_arg)) {
2419 d_fprintf(stderr,
2420 "Could not get domain info\n");
2421 goto done;
2423 break;
2424 case 'i':
2425 if (!wbinfo_get_userinfo(string_arg)) {
2426 d_fprintf(stderr,
2427 "Could not get info for user %s\n",
2428 string_arg);
2429 goto done;
2431 break;
2432 case OPT_USER_SIDINFO:
2433 if ( !wbinfo_get_user_sidinfo(string_arg)) {
2434 d_fprintf(stderr,
2435 "Could not get info for user "
2436 "sid %s\n", string_arg);
2437 goto done;
2439 break;
2440 case OPT_UID_INFO:
2441 if ( !wbinfo_get_uidinfo(int_arg)) {
2442 d_fprintf(stderr, "Could not get info for uid "
2443 "%d\n", int_arg);
2444 goto done;
2446 break;
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);
2451 goto done;
2453 break;
2454 case OPT_GID_INFO:
2455 if ( !wbinfo_get_gidinfo(int_arg)) {
2456 d_fprintf(stderr, "Could not get info for gid "
2457 "%d\n", int_arg);
2458 goto done;
2460 break;
2461 case 'r':
2462 if (!wbinfo_get_usergroups(string_arg)) {
2463 d_fprintf(stderr,
2464 "Could not get groups for user %s\n",
2465 string_arg);
2466 goto done;
2468 break;
2469 case OPT_USERSIDS:
2470 if (!wbinfo_get_usersids(string_arg)) {
2471 d_fprintf(stderr, "Could not get group SIDs "
2472 "for user SID %s\n",
2473 string_arg);
2474 goto done;
2476 break;
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",
2481 string_arg);
2482 goto done;
2484 break;
2485 case OPT_SIDALIASES:
2486 if (!wbinfo_get_sidaliases(opt_domain_name,
2487 string_arg)) {
2488 d_fprintf(stderr, "Could not get sid aliases "
2489 "for user SID %s\n", string_arg);
2490 goto done;
2492 break;
2493 case 'a': {
2494 bool got_error = false;
2496 if (!wbinfo_auth(string_arg)) {
2497 d_fprintf(stderr,
2498 "Could not authenticate user "
2499 "%s with plaintext "
2500 "password\n", string_arg);
2501 got_error = true;
2504 if (!wbinfo_auth_crap(string_arg, use_ntlmv2,
2505 use_lanman)) {
2506 d_fprintf(stderr,
2507 "Could not authenticate user "
2508 "%s with challenge/response\n",
2509 string_arg);
2510 got_error = true;
2513 if (got_error)
2514 goto done;
2515 break;
2517 case OPT_PAM_LOGON:
2518 if (!wbinfo_pam_logon(string_arg)) {
2519 d_fprintf(stderr, "pam_logon failed for %s\n",
2520 string_arg);
2521 goto done;
2523 break;
2524 case OPT_LOGOFF:
2526 wbcErr wbc_status;
2528 wbc_status = wbcLogoffUser(logoff_user, logoff_uid,
2529 "");
2530 d_printf("Logoff %s (%d): %s\n", logoff_user,
2531 logoff_uid, wbcErrorString(wbc_status));
2532 break;
2534 case 'K': {
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",
2542 flags)) {
2543 d_fprintf(stderr,
2544 "Could not authenticate user "
2545 "[%s] with Kerberos "
2546 "(ccache: %s)\n", string_arg,
2547 "FILE");
2548 goto done;
2550 break;
2552 case 'k':
2553 if (!wbinfo_klog(string_arg)) {
2554 d_fprintf(stderr, "Could not klog user\n");
2555 goto done;
2557 break;
2558 case 'p':
2559 if (!wbinfo_ping()) {
2560 d_fprintf(stderr, "could not ping winbindd!\n");
2561 goto done;
2563 break;
2564 case OPT_SET_AUTH_USER:
2565 if (!wbinfo_set_auth_user(string_arg)) {
2566 goto done;
2568 break;
2569 case OPT_GET_AUTH_USER:
2570 wbinfo_get_auth_user();
2571 goto done;
2572 break;
2573 case OPT_CCACHE_SAVE:
2574 if (!wbinfo_ccache_save(string_arg)) {
2575 goto done;
2577 break;
2578 case OPT_GETDCNAME:
2579 if (!wbinfo_getdcname(string_arg)) {
2580 goto done;
2582 break;
2583 case OPT_DSGETDCNAME:
2584 if (!wbinfo_dsgetdcname(string_arg, 0)) {
2585 goto done;
2587 break;
2588 case OPT_DC_INFO:
2589 if (!wbinfo_dc_info(string_arg)) {
2590 goto done;
2592 break;
2593 case OPT_SEPARATOR: {
2594 const char sep = winbind_separator();
2595 if ( !sep ) {
2596 goto done;
2598 d_printf("%c\n", sep);
2599 break;
2601 case OPT_LIST_ALL_DOMAINS:
2602 if (!wbinfo_list_domains(true, verbose)) {
2603 goto done;
2605 break;
2606 case OPT_LIST_OWN_DOMAIN:
2607 if (!wbinfo_list_own_domain()) {
2608 goto done;
2610 break;
2611 case OPT_CHANGE_USER_PASSWORD:
2612 if (!wbinfo_change_user_password(string_arg)) {
2613 d_fprintf(stderr,
2614 "Could not change user password "
2615 "for user %s\n", string_arg);
2616 goto done;
2618 break;
2620 /* generic configuration options */
2621 case OPT_DOMAIN_NAME:
2622 case OPT_VERBOSE:
2623 case OPT_NTLMV2:
2624 case OPT_LANMAN:
2625 case OPT_LOGOFF_USER:
2626 case OPT_LOGOFF_UID:
2627 break;
2628 default:
2629 d_fprintf(stderr, "Invalid option\n");
2630 poptPrintHelp(pc, stderr, 0);
2631 goto done;
2635 result = 0;
2637 /* Exit code */
2639 done:
2640 talloc_free(frame);
2642 poptFreeContext(pc);
2643 return result;