s3-librpc: Register endpoints using ncalrpc.
[Samba.git] / nsswitch / wbinfo.c
blob2436b3bd3c952d2b67039a559aa8be82ab79ec8c
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 "popt_common.h"
26 #include "winbind_client.h"
27 #include "libwbclient/wbclient.h"
28 #include "lib/popt/popt.h"
29 #include "../libcli/auth/libcli_auth.h"
30 #if (_SAMBA_BUILD_) >= 4
31 #include "lib/cmdline/popt_common.h"
32 #endif
34 #ifdef DBGC_CLASS
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_WINBIND
37 #endif
39 static struct wbcInterfaceDetails *init_interface_details(void)
41 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
42 static struct wbcInterfaceDetails *details;
44 if (details) {
45 return details;
48 wbc_status = wbcInterfaceDetails(&details);
49 if (!WBC_ERROR_IS_OK(wbc_status)) {
50 d_fprintf(stderr, "could not obtain winbind interface "
51 "details: %s\n", wbcErrorString(wbc_status));
54 return details;
57 static char winbind_separator(void)
59 struct wbcInterfaceDetails *details;
60 static bool got_sep;
61 static char sep;
63 if (got_sep)
64 return sep;
66 details = init_interface_details();
68 if (!details) {
69 d_fprintf(stderr, "could not obtain winbind separator!\n");
70 return 0;
73 sep = details->winbind_separator;
74 got_sep = true;
76 if (!sep) {
77 d_fprintf(stderr, "winbind separator was NULL!\n");
78 return 0;
81 return sep;
84 static const char *get_winbind_domain(void)
86 static struct wbcInterfaceDetails *details;
88 details = init_interface_details();
90 if (!details) {
91 d_fprintf(stderr, "could not obtain winbind domain name!\n");
92 return 0;
95 return details->netbios_domain;
98 static const char *get_winbind_netbios_name(void)
100 static struct wbcInterfaceDetails *details;
102 details = init_interface_details();
104 if (!details) {
105 d_fprintf(stderr, "could not obtain winbind netbios name!\n");
106 return 0;
109 return details->netbios_name;
112 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
113 form DOMAIN/user into a domain and a user */
115 static bool parse_wbinfo_domain_user(const char *domuser, fstring domain,
116 fstring user)
119 char *p = strchr(domuser,winbind_separator());
121 if (!p) {
122 /* Maybe it was a UPN? */
123 if ((p = strchr(domuser, '@')) != NULL) {
124 fstrcpy(domain, "");
125 fstrcpy(user, domuser);
126 return true;
129 fstrcpy(user, domuser);
130 fstrcpy(domain, get_winbind_domain());
131 return true;
134 fstrcpy(user, p+1);
135 fstrcpy(domain, domuser);
136 domain[PTR_DIFF(p, domuser)] = 0;
137 strupper_m(domain);
139 return true;
142 /* Parse string of "uid,sid" or "gid,sid" into separate int and string values.
143 * Return true if input was valid, false otherwise. */
144 static bool parse_mapping_arg(char *arg, int *id, char **sid)
146 char *tmp, *endptr;
148 if (!arg || !*arg)
149 return false;
151 tmp = strtok(arg, ",");
152 *sid = strtok(NULL, ",");
154 if (!tmp || !*tmp || !*sid || !**sid)
155 return false;
157 /* Because atoi() can return 0 on invalid input, which would be a valid
158 * UID/GID we must use strtoul() and do error checking */
159 *id = strtoul(tmp, &endptr, 10);
161 if (endptr[0] != '\0')
162 return false;
164 return true;
167 /* pull pwent info for a given user */
169 static bool wbinfo_get_userinfo(char *user)
171 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
172 struct passwd *pwd = NULL;
174 wbc_status = wbcGetpwnam(user, &pwd);
175 if (!WBC_ERROR_IS_OK(wbc_status)) {
176 d_fprintf(stderr, "failed to call wbcGetpwnam: %s\n",
177 wbcErrorString(wbc_status));
178 return false;
181 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
182 pwd->pw_name,
183 pwd->pw_passwd,
184 (unsigned int)pwd->pw_uid,
185 (unsigned int)pwd->pw_gid,
186 pwd->pw_gecos,
187 pwd->pw_dir,
188 pwd->pw_shell);
190 return true;
193 /* pull pwent info for a given uid */
194 static bool wbinfo_get_uidinfo(int uid)
196 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
197 struct passwd *pwd = NULL;
199 wbc_status = wbcGetpwuid(uid, &pwd);
200 if (!WBC_ERROR_IS_OK(wbc_status)) {
201 d_fprintf(stderr, "failed to call wbcGetpwuid: %s\n",
202 wbcErrorString(wbc_status));
203 return false;
206 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
207 pwd->pw_name,
208 pwd->pw_passwd,
209 (unsigned int)pwd->pw_uid,
210 (unsigned int)pwd->pw_gid,
211 pwd->pw_gecos,
212 pwd->pw_dir,
213 pwd->pw_shell);
215 return true;
218 static bool wbinfo_get_user_sidinfo(const char *sid_str)
220 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
221 struct passwd *pwd = NULL;
222 struct wbcDomainSid sid;
224 wbc_status = wbcStringToSid(sid_str, &sid);
225 wbc_status = wbcGetpwsid(&sid, &pwd);
226 if (!WBC_ERROR_IS_OK(wbc_status)) {
227 d_fprintf(stderr, "failed to call wbcGetpwsid: %s\n",
228 wbcErrorString(wbc_status));
229 return false;
232 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
233 pwd->pw_name,
234 pwd->pw_passwd,
235 (unsigned int)pwd->pw_uid,
236 (unsigned int)pwd->pw_gid,
237 pwd->pw_gecos,
238 pwd->pw_dir,
239 pwd->pw_shell);
241 return true;
245 /* pull grent for a given group */
246 static bool wbinfo_get_groupinfo(const char *group)
248 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
249 struct group *grp;
250 char **mem;
252 wbc_status = wbcGetgrnam(group, &grp);
253 if (!WBC_ERROR_IS_OK(wbc_status)) {
254 d_fprintf(stderr, "failed to call wbcGetgrnam: %s\n",
255 wbcErrorString(wbc_status));
256 return false;
259 d_printf("%s:%s:%u:",
260 grp->gr_name,
261 grp->gr_passwd,
262 (unsigned int)grp->gr_gid);
264 mem = grp->gr_mem;
265 while (*mem != NULL) {
266 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
267 mem += 1;
269 d_printf("\n");
271 wbcFreeMemory(grp);
273 return true;
276 /* pull grent for a given gid */
277 static bool wbinfo_get_gidinfo(int gid)
279 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
280 struct group *grp;
281 char **mem;
283 wbc_status = wbcGetgrgid(gid, &grp);
284 if (!WBC_ERROR_IS_OK(wbc_status)) {
285 d_fprintf(stderr, "failed to call wbcGetgrgid: %s\n",
286 wbcErrorString(wbc_status));
287 return false;
290 d_printf("%s:%s:%u:",
291 grp->gr_name,
292 grp->gr_passwd,
293 (unsigned int)grp->gr_gid);
295 mem = grp->gr_mem;
296 while (*mem != NULL) {
297 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
298 mem += 1;
300 d_printf("\n");
302 wbcFreeMemory(grp);
304 return true;
307 /* List groups a user is a member of */
309 static bool wbinfo_get_usergroups(const char *user)
311 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
312 uint32_t num_groups;
313 uint32_t i;
314 gid_t *groups = NULL;
316 /* Send request */
318 wbc_status = wbcGetGroups(user, &num_groups, &groups);
319 if (!WBC_ERROR_IS_OK(wbc_status)) {
320 d_fprintf(stderr, "failed to call wbcGetGroups: %s\n",
321 wbcErrorString(wbc_status));
322 return false;
325 for (i = 0; i < num_groups; i++) {
326 d_printf("%d\n", (int)groups[i]);
329 wbcFreeMemory(groups);
331 return true;
335 /* List group SIDs a user SID is a member of */
336 static bool wbinfo_get_usersids(const char *user_sid_str)
338 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
339 uint32_t num_sids;
340 uint32_t i;
341 struct wbcDomainSid user_sid, *sids = NULL;
343 /* Send request */
345 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
346 if (!WBC_ERROR_IS_OK(wbc_status)) {
347 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
348 wbcErrorString(wbc_status));
349 return false;
352 wbc_status = wbcLookupUserSids(&user_sid, false, &num_sids, &sids);
353 if (!WBC_ERROR_IS_OK(wbc_status)) {
354 d_fprintf(stderr, "failed to call wbcLookupUserSids: %s\n",
355 wbcErrorString(wbc_status));
356 return false;
359 for (i = 0; i < num_sids; i++) {
360 char str[WBC_SID_STRING_BUFLEN];
361 wbcSidToStringBuf(&sids[i], str, sizeof(str));
362 d_printf("%s\n", str);
365 wbcFreeMemory(sids);
367 return true;
370 static bool wbinfo_get_userdomgroups(const char *user_sid_str)
372 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
373 uint32_t num_sids;
374 uint32_t i;
375 struct wbcDomainSid user_sid, *sids = NULL;
377 /* Send request */
379 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
380 if (!WBC_ERROR_IS_OK(wbc_status)) {
381 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
382 wbcErrorString(wbc_status));
383 return false;
386 wbc_status = wbcLookupUserSids(&user_sid, true, &num_sids, &sids);
387 if (!WBC_ERROR_IS_OK(wbc_status)) {
388 d_fprintf(stderr, "failed to call wbcLookupUserSids: %s\n",
389 wbcErrorString(wbc_status));
390 return false;
393 for (i = 0; i < num_sids; i++) {
394 char str[WBC_SID_STRING_BUFLEN];
395 wbcSidToStringBuf(&sids[i], str, sizeof(str));
396 d_printf("%s\n", str);
399 wbcFreeMemory(sids);
401 return true;
404 static bool wbinfo_get_sidaliases(const char *domain,
405 const char *user_sid_str)
407 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
408 struct wbcDomainInfo *dinfo = NULL;
409 uint32_t i;
410 struct wbcDomainSid user_sid;
411 uint32_t *alias_rids = NULL;
412 uint32_t num_alias_rids;
413 char domain_sid_str[WBC_SID_STRING_BUFLEN];
415 /* Send request */
416 if ((domain == NULL) || (strequal(domain, ".")) ||
417 (domain[0] == '\0')) {
418 domain = get_winbind_domain();
421 /* Send request */
423 wbc_status = wbcDomainInfo(domain, &dinfo);
424 if (!WBC_ERROR_IS_OK(wbc_status)) {
425 d_fprintf(stderr, "wbcDomainInfo(%s) failed: %s\n", domain,
426 wbcErrorString(wbc_status));
427 goto done;
429 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
430 if (!WBC_ERROR_IS_OK(wbc_status)) {
431 goto done;
434 wbc_status = wbcGetSidAliases(&dinfo->sid, &user_sid, 1,
435 &alias_rids, &num_alias_rids);
436 if (!WBC_ERROR_IS_OK(wbc_status)) {
437 goto done;
440 wbcSidToStringBuf(&dinfo->sid, domain_sid_str, sizeof(domain_sid_str));
442 for (i = 0; i < num_alias_rids; i++) {
443 d_printf("%s-%d\n", domain_sid_str, alias_rids[i]);
446 wbcFreeMemory(alias_rids);
448 done:
449 wbcFreeMemory(dinfo);
450 return (WBC_ERR_SUCCESS == wbc_status);
454 /* Convert NetBIOS name to IP */
456 static bool wbinfo_wins_byname(const char *name)
458 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
459 char *ip = NULL;
461 wbc_status = wbcResolveWinsByName(name, &ip);
462 if (!WBC_ERROR_IS_OK(wbc_status)) {
463 d_fprintf(stderr, "failed to call wbcResolveWinsByName: %s\n",
464 wbcErrorString(wbc_status));
465 return false;
468 /* Display response */
470 d_printf("%s\n", ip);
472 wbcFreeMemory(ip);
474 return true;
477 /* Convert IP to NetBIOS name */
479 static bool wbinfo_wins_byip(const char *ip)
481 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
482 char *name = NULL;
484 wbc_status = wbcResolveWinsByIP(ip, &name);
485 if (!WBC_ERROR_IS_OK(wbc_status)) {
486 d_fprintf(stderr, "failed to call wbcResolveWinsByIP: %s\n",
487 wbcErrorString(wbc_status));
488 return false;
491 /* Display response */
493 d_printf("%s\n", name);
495 wbcFreeMemory(name);
497 return true;
500 /* List all/trusted domains */
502 static bool wbinfo_list_domains(bool list_all_domains, bool verbose)
504 struct wbcDomainInfo *domain_list = NULL;
505 size_t num_domains;
506 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
507 bool print_all = !list_all_domains && verbose;
508 int i;
510 wbc_status = wbcListTrusts(&domain_list, &num_domains);
511 if (!WBC_ERROR_IS_OK(wbc_status)) {
512 d_fprintf(stderr, "failed to call wbcListTrusts: %s\n",
513 wbcErrorString(wbc_status));
514 return false;
517 if (print_all) {
518 d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n",
519 "Domain Name", "DNS Domain", "Trust Type",
520 "Transitive", "In", "Out");
523 for (i=0; i<num_domains; i++) {
524 if (print_all) {
525 d_printf("%-16s", domain_list[i].short_name);
526 } else {
527 d_printf("%s", domain_list[i].short_name);
528 d_printf("\n");
529 continue;
532 d_printf("%-24s", domain_list[i].dns_name);
534 switch(domain_list[i].trust_type) {
535 case WBC_DOMINFO_TRUSTTYPE_NONE:
536 d_printf("None ");
537 break;
538 case WBC_DOMINFO_TRUSTTYPE_FOREST:
539 d_printf("Forest ");
540 break;
541 case WBC_DOMINFO_TRUSTTYPE_EXTERNAL:
542 d_printf("External ");
543 break;
544 case WBC_DOMINFO_TRUSTTYPE_IN_FOREST:
545 d_printf("In-Forest ");
546 break;
549 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_TRANSITIVE) {
550 d_printf("Yes ");
551 } else {
552 d_printf("No ");
555 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_INCOMING) {
556 d_printf("Yes ");
557 } else {
558 d_printf("No ");
561 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_OUTGOING) {
562 d_printf("Yes ");
563 } else {
564 d_printf("No ");
567 d_printf("\n");
570 return true;
573 /* List own domain */
575 static bool wbinfo_list_own_domain(void)
577 d_printf("%s\n", get_winbind_domain());
579 return true;
582 /* show sequence numbers */
583 static bool wbinfo_show_sequence(const char *domain)
585 d_printf("This command has been deprecated. Please use the "
586 "--online-status option instead.\n");
587 return false;
590 /* show sequence numbers */
591 static bool wbinfo_show_onlinestatus(const char *domain)
593 struct wbcDomainInfo *domain_list = NULL;
594 size_t num_domains;
595 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
596 int i;
598 wbc_status = wbcListTrusts(&domain_list, &num_domains);
599 if (!WBC_ERROR_IS_OK(wbc_status)) {
600 d_fprintf(stderr, "failed to call wbcListTrusts: %s\n",
601 wbcErrorString(wbc_status));
602 return false;
605 for (i=0; i<num_domains; i++) {
606 bool is_offline;
608 if (domain) {
609 if (!strequal(domain_list[i].short_name, domain)) {
610 continue;
614 is_offline = (domain_list[i].domain_flags &
615 WBC_DOMINFO_DOMAIN_OFFLINE);
617 d_printf("%s : %s\n",
618 domain_list[i].short_name,
619 is_offline ? "offline" : "online" );
622 return true;
626 /* Show domain info */
628 static bool wbinfo_domain_info(const char *domain)
630 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
631 struct wbcDomainInfo *dinfo = NULL;
632 char sid_str[WBC_SID_STRING_BUFLEN];
634 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
635 domain = get_winbind_domain();
638 /* Send request */
640 wbc_status = wbcDomainInfo(domain, &dinfo);
641 if (!WBC_ERROR_IS_OK(wbc_status)) {
642 d_fprintf(stderr, "failed to call wbcDomainInfo: %s\n",
643 wbcErrorString(wbc_status));
644 return false;
647 wbcSidToStringBuf(&dinfo->sid, sid_str, sizeof(sid_str));
649 /* Display response */
651 d_printf("Name : %s\n", dinfo->short_name);
652 d_printf("Alt_Name : %s\n", dinfo->dns_name);
654 d_printf("SID : %s\n", sid_str);
656 d_printf("Active Directory : %s\n",
657 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_AD) ? "Yes" : "No");
658 d_printf("Native : %s\n",
659 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_NATIVE) ?
660 "Yes" : "No");
662 d_printf("Primary : %s\n",
663 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_PRIMARY) ?
664 "Yes" : "No");
666 wbcFreeMemory(dinfo);
668 return true;
671 /* Get a foreign DC's name */
672 static bool wbinfo_getdcname(const char *domain_name)
674 struct winbindd_request request;
675 struct winbindd_response response;
677 ZERO_STRUCT(request);
678 ZERO_STRUCT(response);
680 fstrcpy(request.domain_name, domain_name);
682 /* Send request */
684 if (winbindd_request_response(WINBINDD_GETDCNAME, &request,
685 &response) != NSS_STATUS_SUCCESS) {
686 d_fprintf(stderr, "Could not get dc name for %s\n",domain_name);
687 return false;
690 /* Display response */
692 d_printf("%s\n", response.data.dc_name);
694 return true;
697 /* Find a DC */
698 static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
700 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
701 struct wbcDomainControllerInfoEx *dc_info;
702 char *str = NULL;
704 wbc_status = wbcLookupDomainControllerEx(domain_name, NULL, NULL,
705 flags | DS_DIRECTORY_SERVICE_REQUIRED,
706 &dc_info);
707 if (!WBC_ERROR_IS_OK(wbc_status)) {
708 printf("Could not find dc for %s\n", domain_name);
709 return false;
712 wbcGuidToString(dc_info->domain_guid, &str);
714 d_printf("%s\n", dc_info->dc_unc);
715 d_printf("%s\n", dc_info->dc_address);
716 d_printf("%d\n", dc_info->dc_address_type);
717 d_printf("%s\n", str);
718 d_printf("%s\n", dc_info->domain_name);
719 d_printf("%s\n", dc_info->forest_name);
720 d_printf("0x%08x\n", dc_info->dc_flags);
721 d_printf("%s\n", dc_info->dc_site_name);
722 d_printf("%s\n", dc_info->client_site_name);
724 return true;
727 /* Check trust account password */
729 static bool wbinfo_check_secret(const char *domain)
731 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
732 struct wbcAuthErrorInfo *error = NULL;
733 const char *domain_name;
735 if (domain) {
736 domain_name = domain;
737 } else {
738 domain_name = get_winbind_domain();
741 wbc_status = wbcCheckTrustCredentials(domain_name, &error);
743 d_printf("checking the trust secret for domain %s via RPC calls %s\n",
744 domain_name,
745 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
747 if (wbc_status == WBC_ERR_AUTH_ERROR) {
748 d_fprintf(stderr, "error code was %s (0x%x)\n",
749 error->nt_string, error->nt_status);
750 wbcFreeMemory(error);
752 if (!WBC_ERROR_IS_OK(wbc_status)) {
753 d_fprintf(stderr, "failed to call wbcCheckTrustCredentials: "
754 "%s\n", wbcErrorString(wbc_status));
755 return false;
758 return true;
761 /* Find the currently connected DCs */
763 static bool wbinfo_dc_info(const char *domain_name)
765 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
766 size_t i, num_dcs;
767 const char **dc_names, **dc_ips;
769 wbc_status = wbcDcInfo(domain_name, &num_dcs,
770 &dc_names, &dc_ips);
771 if (!WBC_ERROR_IS_OK(wbc_status)) {
772 printf("Could not find dc info %s\n",
773 domain_name ? domain_name : "our domain");
774 return false;
777 for (i=0; i<num_dcs; i++) {
778 printf("%s (%s)\n", dc_names[i], dc_ips[i]);
780 wbcFreeMemory(dc_names);
781 wbcFreeMemory(dc_ips);
783 return true;
786 /* Change trust account password */
788 static bool wbinfo_change_secret(const char *domain)
790 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
791 struct wbcAuthErrorInfo *error = NULL;
792 const char *domain_name;
794 if (domain) {
795 domain_name = domain;
796 } else {
797 domain_name = get_winbind_domain();
800 wbc_status = wbcChangeTrustCredentials(domain_name, &error);
802 d_printf("changing the trust secret for domain %s via RPC calls %s\n",
803 domain_name,
804 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
806 if (wbc_status == WBC_ERR_AUTH_ERROR) {
807 d_fprintf(stderr, "error code was %s (0x%x)\n",
808 error->nt_string, error->nt_status);
809 wbcFreeMemory(error);
811 if (!WBC_ERROR_IS_OK(wbc_status)) {
812 d_fprintf(stderr, "failed to call wbcChangeTrustCredentials: "
813 "%s\n", wbcErrorString(wbc_status));
814 return false;
817 return true;
820 /* Check DC connection */
822 static bool wbinfo_ping_dc(void)
824 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
825 struct wbcAuthErrorInfo *error = NULL;
827 wbc_status = wbcPingDc(NULL, &error);
829 d_printf("checking the NETLOGON dc connection %s\n",
830 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
832 if (wbc_status == WBC_ERR_AUTH_ERROR) {
833 d_fprintf(stderr, "error code was %s (0x%x)\n",
834 error->nt_string, error->nt_status);
835 wbcFreeMemory(error);
837 if (!WBC_ERROR_IS_OK(wbc_status)) {
838 d_fprintf(stderr, "failed to call wbcPingDc: %s\n",
839 wbcErrorString(wbc_status));
840 return false;
843 return true;
846 /* Convert uid to sid */
848 static bool wbinfo_uid_to_sid(uid_t uid)
850 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
851 struct wbcDomainSid sid;
852 char sid_str[WBC_SID_STRING_BUFLEN];
854 /* Send request */
856 wbc_status = wbcUidToSid(uid, &sid);
857 if (!WBC_ERROR_IS_OK(wbc_status)) {
858 d_fprintf(stderr, "failed to call wbcUidToSid: %s\n",
859 wbcErrorString(wbc_status));
860 return false;
863 wbcSidToStringBuf(&sid, sid_str, sizeof(sid_str));
865 /* Display response */
867 d_printf("%s\n", sid_str);
869 return true;
872 /* Convert gid to sid */
874 static bool wbinfo_gid_to_sid(gid_t gid)
876 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
877 struct wbcDomainSid sid;
878 char sid_str[WBC_SID_STRING_BUFLEN];
880 /* Send request */
882 wbc_status = wbcGidToSid(gid, &sid);
883 if (!WBC_ERROR_IS_OK(wbc_status)) {
884 d_fprintf(stderr, "failed to call wbcGidToSid: %s\n",
885 wbcErrorString(wbc_status));
886 return false;
889 wbcSidToStringBuf(&sid, sid_str, sizeof(sid_str));
891 /* Display response */
893 d_printf("%s\n", sid_str);
895 return true;
898 /* Convert sid to uid */
900 static bool wbinfo_sid_to_uid(const char *sid_str)
902 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
903 struct wbcDomainSid sid;
904 uid_t uid;
906 /* Send request */
908 wbc_status = wbcStringToSid(sid_str, &sid);
909 if (!WBC_ERROR_IS_OK(wbc_status)) {
910 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
911 wbcErrorString(wbc_status));
912 return false;
915 wbc_status = wbcSidToUid(&sid, &uid);
916 if (!WBC_ERROR_IS_OK(wbc_status)) {
917 d_fprintf(stderr, "failed to call wbcSidToUid: %s\n",
918 wbcErrorString(wbc_status));
919 return false;
922 /* Display response */
924 d_printf("%d\n", (int)uid);
926 return true;
929 static bool wbinfo_sid_to_gid(const char *sid_str)
931 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
932 struct wbcDomainSid sid;
933 gid_t gid;
935 /* Send request */
937 wbc_status = wbcStringToSid(sid_str, &sid);
938 if (!WBC_ERROR_IS_OK(wbc_status)) {
939 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
940 wbcErrorString(wbc_status));
941 return false;
944 wbc_status = wbcSidToGid(&sid, &gid);
945 if (!WBC_ERROR_IS_OK(wbc_status)) {
946 d_fprintf(stderr, "failed to call wbcSidToGid: %s\n",
947 wbcErrorString(wbc_status));
948 return false;
951 /* Display response */
953 d_printf("%d\n", (int)gid);
955 return true;
958 static bool wbinfo_allocate_uid(void)
960 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
961 uid_t uid;
963 /* Send request */
965 wbc_status = wbcAllocateUid(&uid);
966 if (!WBC_ERROR_IS_OK(wbc_status)) {
967 d_fprintf(stderr, "failed to call wbcAllocateUid: %s\n",
968 wbcErrorString(wbc_status));
969 return false;
972 /* Display response */
974 d_printf("New uid: %u\n", (unsigned int)uid);
976 return true;
979 static bool wbinfo_allocate_gid(void)
981 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
982 gid_t gid;
984 /* Send request */
986 wbc_status = wbcAllocateGid(&gid);
987 if (!WBC_ERROR_IS_OK(wbc_status)) {
988 d_fprintf(stderr, "failed to call wbcAllocateGid: %s\n",
989 wbcErrorString(wbc_status));
990 return false;
993 /* Display response */
995 d_printf("New gid: %u\n", (unsigned int)gid);
997 return true;
1000 static bool wbinfo_set_uid_mapping(uid_t uid, const char *sid_str)
1002 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1003 struct wbcDomainSid sid;
1005 /* Send request */
1007 wbc_status = wbcStringToSid(sid_str, &sid);
1008 if (!WBC_ERROR_IS_OK(wbc_status)) {
1009 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1010 wbcErrorString(wbc_status));
1011 return false;
1014 wbc_status = wbcSetUidMapping(uid, &sid);
1015 if (!WBC_ERROR_IS_OK(wbc_status)) {
1016 d_fprintf(stderr, "failed to call wbcSetUidMapping: %s\n",
1017 wbcErrorString(wbc_status));
1018 return false;
1021 /* Display response */
1023 d_printf("uid %u now mapped to sid %s\n",
1024 (unsigned int)uid, sid_str);
1026 return true;
1029 static bool wbinfo_set_gid_mapping(gid_t gid, const char *sid_str)
1031 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1032 struct wbcDomainSid sid;
1034 /* Send request */
1036 wbc_status = wbcStringToSid(sid_str, &sid);
1037 if (!WBC_ERROR_IS_OK(wbc_status)) {
1038 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1039 wbcErrorString(wbc_status));
1040 return false;
1043 wbc_status = wbcSetGidMapping(gid, &sid);
1044 if (!WBC_ERROR_IS_OK(wbc_status)) {
1045 d_fprintf(stderr, "failed to call wbcSetGidMapping: %s\n",
1046 wbcErrorString(wbc_status));
1047 return false;
1050 /* Display response */
1052 d_printf("gid %u now mapped to sid %s\n",
1053 (unsigned int)gid, sid_str);
1055 return true;
1058 static bool wbinfo_remove_uid_mapping(uid_t uid, const char *sid_str)
1060 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1061 struct wbcDomainSid sid;
1063 /* Send request */
1065 wbc_status = wbcStringToSid(sid_str, &sid);
1066 if (!WBC_ERROR_IS_OK(wbc_status)) {
1067 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1068 wbcErrorString(wbc_status));
1069 return false;
1072 wbc_status = wbcRemoveUidMapping(uid, &sid);
1073 if (!WBC_ERROR_IS_OK(wbc_status)) {
1074 d_fprintf(stderr, "failed to call wbcRemoveUidMapping: %s\n",
1075 wbcErrorString(wbc_status));
1076 return false;
1079 /* Display response */
1081 d_printf("Removed uid %u to sid %s mapping\n",
1082 (unsigned int)uid, sid_str);
1084 return true;
1087 static bool wbinfo_remove_gid_mapping(gid_t gid, const char *sid_str)
1089 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1090 struct wbcDomainSid sid;
1092 /* Send request */
1094 wbc_status = wbcStringToSid(sid_str, &sid);
1095 if (!WBC_ERROR_IS_OK(wbc_status)) {
1096 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1097 wbcErrorString(wbc_status));
1098 return false;
1101 wbc_status = wbcRemoveGidMapping(gid, &sid);
1102 if (!WBC_ERROR_IS_OK(wbc_status)) {
1103 d_fprintf(stderr, "failed to call wbcRemoveGidMapping: %s\n",
1104 wbcErrorString(wbc_status));
1105 return false;
1108 /* Display response */
1110 d_printf("Removed gid %u to sid %s mapping\n",
1111 (unsigned int)gid, sid_str);
1113 return true;
1116 /* Convert sid to string */
1118 static bool wbinfo_lookupsid(const char *sid_str)
1120 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1121 struct wbcDomainSid sid;
1122 char *domain;
1123 char *name;
1124 enum wbcSidType type;
1126 /* Send off request */
1128 wbc_status = wbcStringToSid(sid_str, &sid);
1129 if (!WBC_ERROR_IS_OK(wbc_status)) {
1130 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1131 wbcErrorString(wbc_status));
1132 return false;
1135 wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
1136 if (!WBC_ERROR_IS_OK(wbc_status)) {
1137 d_fprintf(stderr, "failed to call wbcLookupSid: %s\n",
1138 wbcErrorString(wbc_status));
1139 return false;
1142 /* Display response */
1144 d_printf("%s%c%s %d\n",
1145 domain, winbind_separator(), name, type);
1147 return true;
1150 /* Convert sid to fullname */
1152 static bool wbinfo_lookupsid_fullname(const char *sid_str)
1154 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1155 struct wbcDomainSid sid;
1156 char *domain;
1157 char *name;
1158 enum wbcSidType type;
1160 /* Send off request */
1162 wbc_status = wbcStringToSid(sid_str, &sid);
1163 if (!WBC_ERROR_IS_OK(wbc_status)) {
1164 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1165 wbcErrorString(wbc_status));
1166 return false;
1169 wbc_status = wbcGetDisplayName(&sid, &domain, &name, &type);
1170 if (!WBC_ERROR_IS_OK(wbc_status)) {
1171 d_fprintf(stderr, "failed to call wbcGetDisplayName: %s\n",
1172 wbcErrorString(wbc_status));
1173 return false;
1176 /* Display response */
1178 d_printf("%s%c%s %d\n",
1179 domain, winbind_separator(), name, type);
1181 return true;
1184 /* Lookup a list of RIDs */
1186 static bool wbinfo_lookuprids(const char *domain, const char *arg)
1188 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1189 struct wbcDomainInfo *dinfo = NULL;
1190 char *domain_name = NULL;
1191 const char **names = NULL;
1192 enum wbcSidType *types = NULL;
1193 size_t i;
1194 int num_rids;
1195 uint32_t *rids = NULL;
1196 const char *p;
1197 char *ridstr;
1198 TALLOC_CTX *mem_ctx = NULL;
1199 bool ret = false;
1201 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
1202 domain = get_winbind_domain();
1205 /* Send request */
1207 wbc_status = wbcDomainInfo(domain, &dinfo);
1208 if (!WBC_ERROR_IS_OK(wbc_status)) {
1209 d_printf("wbcDomainInfo(%s) failed: %s\n", domain,
1210 wbcErrorString(wbc_status));
1211 goto done;
1214 mem_ctx = talloc_new(NULL);
1215 if (mem_ctx == NULL) {
1216 d_printf("talloc_new failed\n");
1217 goto done;
1220 num_rids = 0;
1221 rids = NULL;
1222 p = arg;
1224 while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
1225 uint32_t rid = strtoul(ridstr, NULL, 10);
1226 rids = talloc_realloc(mem_ctx, rids, uint32_t, num_rids + 1);
1227 if (rids == NULL) {
1228 d_printf("talloc_realloc failed\n");
1230 rids[num_rids] = rid;
1231 num_rids += 1;
1234 if (rids == NULL) {
1235 d_printf("no rids\n");
1236 goto done;
1239 wbc_status = wbcLookupRids(&dinfo->sid, num_rids, rids,
1240 (const char **)&domain_name, &names, &types);
1241 if (!WBC_ERROR_IS_OK(wbc_status)) {
1242 d_printf("winbind_lookup_rids failed: %s\n",
1243 wbcErrorString(wbc_status));
1244 goto done;
1247 d_printf("Domain: %s\n", domain_name);
1249 for (i=0; i<num_rids; i++) {
1250 d_printf("%8d: %s (%s)\n", rids[i], names[i],
1251 wbcSidTypeString(types[i]));
1254 ret = true;
1255 done:
1256 wbcFreeMemory(dinfo);
1257 wbcFreeMemory(domain_name);
1258 wbcFreeMemory(names);
1259 wbcFreeMemory(types);
1260 TALLOC_FREE(mem_ctx);
1261 return ret;
1264 /* Convert string to sid */
1266 static bool wbinfo_lookupname(const char *full_name)
1268 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1269 struct wbcDomainSid sid;
1270 char sid_str[WBC_SID_STRING_BUFLEN];
1271 enum wbcSidType type;
1272 fstring domain_name;
1273 fstring account_name;
1275 /* Send off request */
1277 parse_wbinfo_domain_user(full_name, domain_name,
1278 account_name);
1280 wbc_status = wbcLookupName(domain_name, account_name,
1281 &sid, &type);
1282 if (!WBC_ERROR_IS_OK(wbc_status)) {
1283 d_fprintf(stderr, "failed to call wbcLookupName: %s\n",
1284 wbcErrorString(wbc_status));
1285 return false;
1288 wbcSidToStringBuf(&sid, sid_str, sizeof(sid_str));
1290 /* Display response */
1292 d_printf("%s %s (%d)\n", sid_str, wbcSidTypeString(type), type);
1294 return true;
1297 static char *wbinfo_prompt_pass(TALLOC_CTX *mem_ctx,
1298 const char *prefix,
1299 const char *username)
1301 char *prompt;
1302 const char *ret = NULL;
1304 prompt = talloc_asprintf(mem_ctx, "Enter %s's ", username);
1305 if (!prompt) {
1306 return NULL;
1308 if (prefix) {
1309 prompt = talloc_asprintf_append(prompt, "%s ", prefix);
1310 if (!prompt) {
1311 return NULL;
1314 prompt = talloc_asprintf_append(prompt, "password: ");
1315 if (!prompt) {
1316 return NULL;
1319 ret = getpass(prompt);
1320 TALLOC_FREE(prompt);
1322 return talloc_strdup(mem_ctx, ret);
1325 /* Authenticate a user with a plaintext password */
1327 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32_t flags)
1329 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1330 char *s = NULL;
1331 char *p = NULL;
1332 char *password = NULL;
1333 char *name = NULL;
1334 char *local_cctype = NULL;
1335 uid_t uid;
1336 struct wbcLogonUserParams params;
1337 struct wbcLogonUserInfo *info;
1338 struct wbcAuthErrorInfo *error;
1339 struct wbcUserPasswordPolicyInfo *policy;
1340 TALLOC_CTX *frame = talloc_tos();
1342 if ((s = talloc_strdup(frame, username)) == NULL) {
1343 return false;
1346 if ((p = strchr(s, '%')) != NULL) {
1347 *p = 0;
1348 p++;
1349 password = talloc_strdup(frame, p);
1350 } else {
1351 password = wbinfo_prompt_pass(frame, NULL, username);
1354 local_cctype = talloc_strdup(frame, cctype);
1356 name = s;
1358 uid = geteuid();
1360 params.username = name;
1361 params.password = password;
1362 params.num_blobs = 0;
1363 params.blobs = NULL;
1365 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1366 &params.blobs,
1367 "flags",
1369 (uint8_t *)&flags,
1370 sizeof(flags));
1371 if (!WBC_ERROR_IS_OK(wbc_status)) {
1372 d_fprintf(stderr, "failed to call wbcAddNamedBlob: %s\n",
1373 wbcErrorString(wbc_status));
1374 goto done;
1377 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1378 &params.blobs,
1379 "user_uid",
1381 (uint8_t *)&uid,
1382 sizeof(uid));
1383 if (!WBC_ERROR_IS_OK(wbc_status)) {
1384 d_fprintf(stderr, "failed to call wbcAddNamedBlob: %s\n",
1385 wbcErrorString(wbc_status));
1386 goto done;
1389 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1390 &params.blobs,
1391 "krb5_cc_type",
1393 (uint8_t *)local_cctype,
1394 strlen(cctype)+1);
1395 if (!WBC_ERROR_IS_OK(wbc_status)) {
1396 d_fprintf(stderr, "failed to call wbcAddNamedBlob: %s\n",
1397 wbcErrorString(wbc_status));
1398 goto done;
1401 wbc_status = wbcLogonUser(&params, &info, &error, &policy);
1403 d_printf("plaintext kerberos password authentication for [%s] %s "
1404 "(requesting cctype: %s)\n",
1405 username, WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed",
1406 cctype);
1408 if (error) {
1409 d_fprintf(stderr,
1410 "error code was %s (0x%x)\nerror message was: %s\n",
1411 error->nt_string,
1412 error->nt_status,
1413 error->display_string);
1416 if (WBC_ERROR_IS_OK(wbc_status)) {
1417 if (flags & WBFLAG_PAM_INFO3_TEXT) {
1418 if (info && info->info && info->info->user_flags &
1419 NETLOGON_CACHED_ACCOUNT) {
1420 d_printf("user_flgs: "
1421 "NETLOGON_CACHED_ACCOUNT\n");
1425 if (info) {
1426 int i;
1427 for (i=0; i < info->num_blobs; i++) {
1428 if (strequal(info->blobs[i].name,
1429 "krb5ccname")) {
1430 d_printf("credentials were put "
1431 "in: %s\n",
1432 (const char *)
1433 info->blobs[i].blob.data);
1434 break;
1437 } else {
1438 d_printf("no credentials cached\n");
1441 done:
1443 wbcFreeMemory(params.blobs);
1445 return WBC_ERROR_IS_OK(wbc_status);
1448 /* Authenticate a user with a plaintext password */
1450 static bool wbinfo_auth(char *username)
1452 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1453 char *s = NULL;
1454 char *p = NULL;
1455 char *password = NULL;
1456 char *name = NULL;
1457 TALLOC_CTX *frame = talloc_tos();
1459 if ((s = talloc_strdup(frame, username)) == NULL) {
1460 return false;
1463 if ((p = strchr(s, '%')) != NULL) {
1464 *p = 0;
1465 p++;
1466 password = talloc_strdup(frame, p);
1467 } else {
1468 password = wbinfo_prompt_pass(frame, NULL, username);
1471 name = s;
1473 wbc_status = wbcAuthenticateUser(name, password);
1475 d_printf("plaintext password authentication %s\n",
1476 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1478 #if 0
1479 if (response.data.auth.nt_status)
1480 d_fprintf(stderr,
1481 "error code was %s (0x%x)\nerror message was: %s\n",
1482 response.data.auth.nt_status_string,
1483 response.data.auth.nt_status,
1484 response.data.auth.error_string);
1485 #endif
1487 return WBC_ERROR_IS_OK(wbc_status);
1490 /* Authenticate a user with a challenge/response */
1492 static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
1494 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1495 struct wbcAuthUserParams params;
1496 struct wbcAuthUserInfo *info = NULL;
1497 struct wbcAuthErrorInfo *err = NULL;
1498 DATA_BLOB lm = data_blob_null;
1499 DATA_BLOB nt = data_blob_null;
1500 fstring name_user;
1501 fstring name_domain;
1502 char *pass;
1503 char *p;
1504 TALLOC_CTX *frame = talloc_tos();
1506 p = strchr(username, '%');
1508 if (p) {
1509 *p = 0;
1510 pass = talloc_strdup(frame, p + 1);
1511 } else {
1512 pass = wbinfo_prompt_pass(frame, NULL, username);
1515 parse_wbinfo_domain_user(username, name_domain, name_user);
1517 params.account_name = name_user;
1518 params.domain_name = name_domain;
1519 params.workstation_name = NULL;
1521 params.flags = 0;
1522 params.parameter_control= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
1523 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
1525 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
1527 generate_random_buffer(params.password.response.challenge, 8);
1529 if (use_ntlmv2) {
1530 DATA_BLOB server_chal;
1531 DATA_BLOB names_blob;
1533 server_chal = data_blob(params.password.response.challenge, 8);
1535 /* Pretend this is a login to 'us', for blob purposes */
1536 names_blob = NTLMv2_generate_names_blob(NULL,
1537 get_winbind_netbios_name(),
1538 get_winbind_domain());
1540 if (!SMBNTLMv2encrypt(NULL, name_user, name_domain, pass,
1541 &server_chal,
1542 &names_blob,
1543 &lm, &nt, NULL, NULL)) {
1544 data_blob_free(&names_blob);
1545 data_blob_free(&server_chal);
1546 TALLOC_FREE(pass);
1547 return false;
1549 data_blob_free(&names_blob);
1550 data_blob_free(&server_chal);
1552 } else {
1553 if (use_lanman) {
1554 bool ok;
1555 lm = data_blob(NULL, 24);
1556 ok = SMBencrypt(pass,
1557 params.password.response.challenge,
1558 lm.data);
1559 if (!ok) {
1560 data_blob_free(&lm);
1563 nt = data_blob(NULL, 24);
1564 SMBNTencrypt(pass, params.password.response.challenge,
1565 nt.data);
1568 params.password.response.nt_length = nt.length;
1569 params.password.response.nt_data = nt.data;
1570 params.password.response.lm_length = lm.length;
1571 params.password.response.lm_data = lm.data;
1573 wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
1575 /* Display response */
1577 d_printf("challenge/response password authentication %s\n",
1578 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1580 if (wbc_status == WBC_ERR_AUTH_ERROR) {
1581 d_fprintf(stderr,
1582 "error code was %s (0x%x)\nerror message was: %s\n",
1583 err->nt_string,
1584 err->nt_status,
1585 err->display_string);
1586 wbcFreeMemory(err);
1587 } else if (WBC_ERROR_IS_OK(wbc_status)) {
1588 wbcFreeMemory(info);
1591 data_blob_free(&nt);
1592 data_blob_free(&lm);
1594 return WBC_ERROR_IS_OK(wbc_status);
1597 /* Authenticate a user with a plaintext password */
1599 static bool wbinfo_pam_logon(char *username)
1601 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1602 struct wbcLogonUserParams params;
1603 struct wbcAuthErrorInfo *error;
1604 char *s = NULL;
1605 char *p = NULL;
1606 TALLOC_CTX *frame = talloc_tos();
1607 uint32_t flags;
1608 uint32_t uid;
1610 ZERO_STRUCT(params);
1612 if ((s = talloc_strdup(frame, username)) == NULL) {
1613 return false;
1616 if ((p = strchr(s, '%')) != NULL) {
1617 *p = 0;
1618 p++;
1619 params.password = talloc_strdup(frame, p);
1620 } else {
1621 params.password = wbinfo_prompt_pass(frame, NULL, username);
1623 params.username = s;
1625 flags = WBFLAG_PAM_CACHED_LOGIN;
1627 wbc_status = wbcAddNamedBlob(&params.num_blobs, &params.blobs,
1628 "flags", 0,
1629 (uint8_t *)&flags, sizeof(flags));
1630 if (!WBC_ERROR_IS_OK(wbc_status)) {
1631 d_printf("wbcAddNamedBlob failed: %s\n",
1632 wbcErrorString(wbc_status));
1633 return false;
1636 uid = getuid();
1638 wbc_status = wbcAddNamedBlob(&params.num_blobs, &params.blobs,
1639 "user_uid", 0,
1640 (uint8_t *)&uid, sizeof(uid));
1641 if (!WBC_ERROR_IS_OK(wbc_status)) {
1642 d_printf("wbcAddNamedBlob failed: %s\n",
1643 wbcErrorString(wbc_status));
1644 return false;
1647 wbc_status = wbcLogonUser(&params, NULL, &error, NULL);
1649 wbcFreeMemory(params.blobs);
1651 d_printf("plaintext password authentication %s\n",
1652 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1654 if (!WBC_ERROR_IS_OK(wbc_status)) {
1655 d_fprintf(stderr,
1656 "error code was %s (0x%x)\nerror message was: %s\n",
1657 error->nt_string,
1658 (int)error->nt_status,
1659 error->display_string);
1660 wbcFreeMemory(error);
1661 return false;
1663 return true;
1666 /* Save creds with winbind */
1668 static bool wbinfo_ccache_save(char *username)
1670 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1671 char *s = NULL;
1672 char *p = NULL;
1673 char *password = NULL;
1674 char *name = NULL;
1675 TALLOC_CTX *frame = talloc_stackframe();
1677 s = talloc_strdup(frame, username);
1678 if (s == NULL) {
1679 return false;
1682 p = strchr(s, '%');
1683 if (p != NULL) {
1684 *p = 0;
1685 p++;
1686 password = talloc_strdup(frame, p);
1687 } else {
1688 password = wbinfo_prompt_pass(frame, NULL, username);
1691 name = s;
1693 wbc_status = wbcCredentialSave(name, password);
1695 d_printf("saving creds %s\n",
1696 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1698 TALLOC_FREE(frame);
1700 return WBC_ERROR_IS_OK(wbc_status);
1703 #ifdef WITH_FAKE_KASERVER
1704 /* Authenticate a user with a plaintext password and set a token */
1706 static bool wbinfo_klog(char *username)
1708 struct winbindd_request request;
1709 struct winbindd_response response;
1710 NSS_STATUS result;
1711 char *p;
1713 /* Send off request */
1715 ZERO_STRUCT(request);
1716 ZERO_STRUCT(response);
1718 p = strchr(username, '%');
1720 if (p) {
1721 *p = 0;
1722 fstrcpy(request.data.auth.user, username);
1723 fstrcpy(request.data.auth.pass, p + 1);
1724 *p = '%';
1725 } else {
1726 fstrcpy(request.data.auth.user, username);
1727 fstrcpy(request.data.auth.pass, getpass("Password: "));
1730 request.flags |= WBFLAG_PAM_AFS_TOKEN;
1732 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request,
1733 &response);
1735 /* Display response */
1737 d_printf("plaintext password authentication %s\n",
1738 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1740 if (response.data.auth.nt_status)
1741 d_fprintf(stderr,
1742 "error code was %s (0x%x)\nerror message was: %s\n",
1743 response.data.auth.nt_status_string,
1744 response.data.auth.nt_status,
1745 response.data.auth.error_string);
1747 if (result != NSS_STATUS_SUCCESS)
1748 return false;
1750 if (response.extra_data.data == NULL) {
1751 d_fprintf(stderr, "Did not get token data\n");
1752 return false;
1755 if (!afs_settoken_str((char *)response.extra_data.data)) {
1756 d_fprintf(stderr, "Could not set token\n");
1757 return false;
1760 d_printf("Successfully created AFS token\n");
1761 return true;
1763 #else
1764 static bool wbinfo_klog(char *username)
1766 d_fprintf(stderr, "No AFS support compiled in.\n");
1767 return false;
1769 #endif
1771 /* Print domain users */
1773 static bool print_domain_users(const char *domain)
1775 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1776 uint32_t i;
1777 uint32_t num_users = 0;
1778 const char **users = NULL;
1780 /* Send request to winbind daemon */
1782 /* '.' is the special sign for our own domain */
1783 if (domain && strcmp(domain, ".") == 0) {
1784 domain = get_winbind_domain();
1787 wbc_status = wbcListUsers(domain, &num_users, &users);
1788 if (!WBC_ERROR_IS_OK(wbc_status)) {
1789 return false;
1792 for (i=0; i < num_users; i++) {
1793 d_printf("%s\n", users[i]);
1796 wbcFreeMemory(users);
1798 return true;
1801 /* Print domain groups */
1803 static bool print_domain_groups(const char *domain)
1805 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1806 uint32_t i;
1807 uint32_t num_groups = 0;
1808 const char **groups = NULL;
1810 /* Send request to winbind daemon */
1812 /* '.' is the special sign for our own domain */
1813 if (domain && strcmp(domain, ".") == 0) {
1814 domain = get_winbind_domain();
1817 wbc_status = wbcListGroups(domain, &num_groups, &groups);
1818 if (!WBC_ERROR_IS_OK(wbc_status)) {
1819 d_fprintf(stderr, "failed to call wbcListGroups: %s\n",
1820 wbcErrorString(wbc_status));
1821 return false;
1824 for (i=0; i < num_groups; i++) {
1825 d_printf("%s\n", groups[i]);
1828 wbcFreeMemory(groups);
1830 return true;
1833 /* Set the authorised user for winbindd access in secrets.tdb */
1835 static bool wbinfo_set_auth_user(char *username)
1837 d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1838 "See 'net help setauthuser' for details.\n");
1839 return false;
1842 static void wbinfo_get_auth_user(void)
1844 d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1845 "See 'net help getauthuser' for details.\n");
1848 static bool wbinfo_ping(void)
1850 wbcErr wbc_status;
1852 wbc_status = wbcPing();
1854 /* Display response */
1856 d_printf("Ping to winbindd %s\n",
1857 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1859 return WBC_ERROR_IS_OK(wbc_status);
1862 static bool wbinfo_change_user_password(const char *username)
1864 wbcErr wbc_status;
1865 char *old_password = NULL;
1866 char *new_password = NULL;
1867 TALLOC_CTX *frame = talloc_tos();
1869 old_password = wbinfo_prompt_pass(frame, "old", username);
1870 new_password = wbinfo_prompt_pass(frame, "new", username);
1872 wbc_status = wbcChangeUserPassword(username, old_password,new_password);
1874 /* Display response */
1876 d_printf("Password change for user %s %s\n", username,
1877 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1879 return WBC_ERROR_IS_OK(wbc_status);
1882 /* Main program */
1884 enum {
1885 OPT_SET_AUTH_USER = 1000,
1886 OPT_GET_AUTH_USER,
1887 OPT_DOMAIN_NAME,
1888 OPT_SEQUENCE,
1889 OPT_GETDCNAME,
1890 OPT_DSGETDCNAME,
1891 OPT_DC_INFO,
1892 OPT_USERDOMGROUPS,
1893 OPT_SIDALIASES,
1894 OPT_USERSIDS,
1895 OPT_ALLOCATE_UID,
1896 OPT_ALLOCATE_GID,
1897 OPT_SET_UID_MAPPING,
1898 OPT_SET_GID_MAPPING,
1899 OPT_REMOVE_UID_MAPPING,
1900 OPT_REMOVE_GID_MAPPING,
1901 OPT_SEPARATOR,
1902 OPT_LIST_ALL_DOMAINS,
1903 OPT_LIST_OWN_DOMAIN,
1904 OPT_UID_INFO,
1905 OPT_USER_SIDINFO,
1906 OPT_GROUP_INFO,
1907 OPT_GID_INFO,
1908 OPT_VERBOSE,
1909 OPT_ONLINESTATUS,
1910 OPT_CHANGE_USER_PASSWORD,
1911 OPT_CCACHE_SAVE,
1912 OPT_SID_TO_FULLNAME,
1913 OPT_NTLMV2,
1914 OPT_PAM_LOGON,
1915 OPT_LOGOFF,
1916 OPT_LOGOFF_USER,
1917 OPT_LOGOFF_UID,
1918 OPT_LANMAN
1921 int main(int argc, char **argv, char **envp)
1923 int opt;
1924 TALLOC_CTX *frame = talloc_stackframe();
1925 poptContext pc;
1926 static char *string_arg;
1927 char *string_subarg = NULL;
1928 static char *opt_domain_name;
1929 static int int_arg;
1930 int int_subarg = -1;
1931 int result = 1;
1932 bool verbose = false;
1933 bool use_ntlmv2 = false;
1934 bool use_lanman = false;
1935 char *logoff_user = getenv("USER");
1936 int logoff_uid = geteuid();
1938 struct poptOption long_options[] = {
1939 POPT_AUTOHELP
1941 /* longName, shortName, argInfo, argPtr, value, descrip,
1942 argDesc */
1944 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1945 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1946 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1947 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1948 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1949 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1950 { "sid-to-fullname", 0, POPT_ARG_STRING, &string_arg,
1951 OPT_SID_TO_FULLNAME, "Converts sid to fullname", "SID" },
1952 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1953 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1954 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1955 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1956 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1957 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1958 "Get a new UID out of idmap" },
1959 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1960 "Get a new GID out of idmap" },
1961 { "set-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_UID_MAPPING, "Create or modify uid to sid mapping in idmap", "UID,SID" },
1962 { "set-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_GID_MAPPING, "Create or modify gid to sid mapping in idmap", "GID,SID" },
1963 { "remove-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_UID_MAPPING, "Remove uid to sid mapping in idmap", "UID,SID" },
1964 { "remove-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_GID_MAPPING, "Remove gid to sid mapping in idmap", "GID,SID" },
1965 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1966 { "change-secret", 'c', POPT_ARG_NONE, 0, 'c', "Change shared secret" },
1967 { "ping-dc", 'P', POPT_ARG_NONE, 0, 'P',
1968 "Check the NETLOGON connection" },
1969 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1970 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1971 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1972 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Deprecated command, see --online-status" },
1973 { "online-status", 0, POPT_ARG_NONE, 0, OPT_ONLINESTATUS, "Show whether domains are marked as online or offline"},
1974 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1975 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1976 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1977 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1978 { "user-sidinfo", 0, POPT_ARG_STRING, &string_arg, OPT_USER_SIDINFO, "Get user info from sid", "SID" },
1979 { "gid-info", 0, POPT_ARG_INT, &int_arg, OPT_GID_INFO, "Get group info from gid", "GID" },
1980 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1981 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1982 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1983 { "sid-aliases", 0, POPT_ARG_STRING, &string_arg, OPT_SIDALIASES, "Get sid aliases", "SID" },
1984 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1985 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1986 { "pam-logon", 0, POPT_ARG_STRING, &string_arg, OPT_PAM_LOGON,
1987 "do a pam logon equivalent", "user%password" },
1988 { "logoff", 0, POPT_ARG_NONE, NULL, OPT_LOGOFF,
1989 "log off user", "uid" },
1990 { "logoff-user", 0, POPT_ARG_STRING, &logoff_user,
1991 OPT_LOGOFF_USER, "username to log off" },
1992 { "logoff-uid", 0, POPT_ARG_INT, &logoff_uid,
1993 OPT_LOGOFF_UID, "uid to log off" },
1994 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1995 { "ccache-save", 0, POPT_ARG_STRING, &string_arg,
1996 OPT_CCACHE_SAVE, "Store user and password for ccache "
1997 "operation", "user%password" },
1998 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1999 "Get a DC name for a foreign domain", "domainname" },
2000 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
2001 { "dc-info", 0, POPT_ARG_STRING, &string_arg, OPT_DC_INFO,
2002 "Find the currently known DCs", "domainname" },
2003 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
2004 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
2005 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
2006 #ifdef WITH_FAKE_KASERVER
2007 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
2008 #endif
2009 #ifdef HAVE_KRB5
2010 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
2011 /* destroys wbinfo --help output */
2012 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
2013 #endif
2014 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
2015 { "verbose", 0, POPT_ARG_NONE, 0, OPT_VERBOSE, "Print additional information per command", NULL },
2016 { "change-user-password", 0, POPT_ARG_STRING, &string_arg, OPT_CHANGE_USER_PASSWORD, "Change the password for a user", NULL },
2017 { "ntlmv2", 0, POPT_ARG_NONE, 0, OPT_NTLMV2, "Use NTLMv2 cryptography for user authentication", NULL},
2018 { "lanman", 0, POPT_ARG_NONE, 0, OPT_LANMAN, "Use lanman cryptography for user authentication", NULL},
2019 POPT_COMMON_VERSION
2020 POPT_TABLEEND
2023 /* Samba client initialisation */
2024 load_case_tables();
2027 /* Parse options */
2029 pc = poptGetContext("wbinfo", argc, (const char **)argv,
2030 long_options, 0);
2032 /* Parse command line options */
2034 if (argc == 1) {
2035 poptPrintHelp(pc, stderr, 0);
2036 return 1;
2039 while((opt = poptGetNextOpt(pc)) != -1) {
2040 /* get the generic configuration parameters like --domain */
2041 switch (opt) {
2042 case OPT_VERBOSE:
2043 verbose = true;
2044 break;
2045 case OPT_NTLMV2:
2046 use_ntlmv2 = true;
2047 break;
2048 case OPT_LANMAN:
2049 use_lanman = true;
2050 break;
2054 poptFreeContext(pc);
2056 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2057 POPT_CONTEXT_KEEP_FIRST);
2059 while((opt = poptGetNextOpt(pc)) != -1) {
2060 switch (opt) {
2061 case 'u':
2062 if (!print_domain_users(opt_domain_name)) {
2063 d_fprintf(stderr,
2064 "Error looking up domain users\n");
2065 goto done;
2067 break;
2068 case 'g':
2069 if (!print_domain_groups(opt_domain_name)) {
2070 d_fprintf(stderr,
2071 "Error looking up domain groups\n");
2072 goto done;
2074 break;
2075 case 's':
2076 if (!wbinfo_lookupsid(string_arg)) {
2077 d_fprintf(stderr,
2078 "Could not lookup sid %s\n",
2079 string_arg);
2080 goto done;
2082 break;
2083 case OPT_SID_TO_FULLNAME:
2084 if (!wbinfo_lookupsid_fullname(string_arg)) {
2085 d_fprintf(stderr, "Could not lookup sid %s\n",
2086 string_arg);
2087 goto done;
2089 break;
2090 case 'R':
2091 if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
2092 d_fprintf(stderr, "Could not lookup RIDs %s\n",
2093 string_arg);
2094 goto done;
2096 break;
2097 case 'n':
2098 if (!wbinfo_lookupname(string_arg)) {
2099 d_fprintf(stderr, "Could not lookup name %s\n",
2100 string_arg);
2101 goto done;
2103 break;
2104 case 'N':
2105 if (!wbinfo_wins_byname(string_arg)) {
2106 d_fprintf(stderr,
2107 "Could not lookup WINS by name %s\n",
2108 string_arg);
2109 goto done;
2111 break;
2112 case 'I':
2113 if (!wbinfo_wins_byip(string_arg)) {
2114 d_fprintf(stderr,
2115 "Could not lookup WINS by IP %s\n",
2116 string_arg);
2117 goto done;
2119 break;
2120 case 'U':
2121 if (!wbinfo_uid_to_sid(int_arg)) {
2122 d_fprintf(stderr,
2123 "Could not convert uid %d to sid\n",
2124 int_arg);
2125 goto done;
2127 break;
2128 case 'G':
2129 if (!wbinfo_gid_to_sid(int_arg)) {
2130 d_fprintf(stderr,
2131 "Could not convert gid %d to sid\n",
2132 int_arg);
2133 goto done;
2135 break;
2136 case 'S':
2137 if (!wbinfo_sid_to_uid(string_arg)) {
2138 d_fprintf(stderr,
2139 "Could not convert sid %s to uid\n",
2140 string_arg);
2141 goto done;
2143 break;
2144 case 'Y':
2145 if (!wbinfo_sid_to_gid(string_arg)) {
2146 d_fprintf(stderr,
2147 "Could not convert sid %s to gid\n",
2148 string_arg);
2149 goto done;
2151 break;
2152 case OPT_ALLOCATE_UID:
2153 if (!wbinfo_allocate_uid()) {
2154 d_fprintf(stderr, "Could not allocate a uid\n");
2155 goto done;
2157 break;
2158 case OPT_ALLOCATE_GID:
2159 if (!wbinfo_allocate_gid()) {
2160 d_fprintf(stderr, "Could not allocate a gid\n");
2161 goto done;
2163 break;
2164 case OPT_SET_UID_MAPPING:
2165 if (!parse_mapping_arg(string_arg, &int_subarg,
2166 &string_subarg) ||
2167 !wbinfo_set_uid_mapping(int_subarg, string_subarg))
2169 d_fprintf(stderr, "Could not create or modify "
2170 "uid to sid mapping\n");
2171 goto done;
2173 break;
2174 case OPT_SET_GID_MAPPING:
2175 if (!parse_mapping_arg(string_arg, &int_subarg,
2176 &string_subarg) ||
2177 !wbinfo_set_gid_mapping(int_subarg, string_subarg))
2179 d_fprintf(stderr, "Could not create or modify "
2180 "gid to sid mapping\n");
2181 goto done;
2183 break;
2184 case OPT_REMOVE_UID_MAPPING:
2185 if (!parse_mapping_arg(string_arg, &int_subarg,
2186 &string_subarg) ||
2187 !wbinfo_remove_uid_mapping(int_subarg,
2188 string_subarg))
2190 d_fprintf(stderr, "Could not remove uid to sid "
2191 "mapping\n");
2192 goto done;
2194 break;
2195 case OPT_REMOVE_GID_MAPPING:
2196 if (!parse_mapping_arg(string_arg, &int_subarg,
2197 &string_subarg) ||
2198 !wbinfo_remove_gid_mapping(int_subarg,
2199 string_subarg))
2201 d_fprintf(stderr, "Could not remove gid to sid "
2202 "mapping\n");
2203 goto done;
2205 break;
2206 case 't':
2207 if (!wbinfo_check_secret(opt_domain_name)) {
2208 d_fprintf(stderr, "Could not check secret\n");
2209 goto done;
2211 break;
2212 case 'c':
2213 if (!wbinfo_change_secret(opt_domain_name)) {
2214 d_fprintf(stderr, "Could not change secret\n");
2215 goto done;
2217 break;
2218 case 'P':
2219 if (!wbinfo_ping_dc()) {
2220 d_fprintf(stderr, "Could not ping our DC\n");
2221 goto done;
2223 break;
2224 case 'm':
2225 if (!wbinfo_list_domains(false, verbose)) {
2226 d_fprintf(stderr,
2227 "Could not list trusted domains\n");
2228 goto done;
2230 break;
2231 case OPT_SEQUENCE:
2232 if (!wbinfo_show_sequence(opt_domain_name)) {
2233 d_fprintf(stderr,
2234 "Could not show sequence numbers\n");
2235 goto done;
2237 break;
2238 case OPT_ONLINESTATUS:
2239 if (!wbinfo_show_onlinestatus(opt_domain_name)) {
2240 d_fprintf(stderr,
2241 "Could not show online-status\n");
2242 goto done;
2244 break;
2245 case 'D':
2246 if (!wbinfo_domain_info(string_arg)) {
2247 d_fprintf(stderr,
2248 "Could not get domain info\n");
2249 goto done;
2251 break;
2252 case 'i':
2253 if (!wbinfo_get_userinfo(string_arg)) {
2254 d_fprintf(stderr,
2255 "Could not get info for user %s\n",
2256 string_arg);
2257 goto done;
2259 break;
2260 case OPT_USER_SIDINFO:
2261 if ( !wbinfo_get_user_sidinfo(string_arg)) {
2262 d_fprintf(stderr,
2263 "Could not get info for user "
2264 "sid %s\n", string_arg);
2265 goto done;
2267 break;
2268 case OPT_UID_INFO:
2269 if ( !wbinfo_get_uidinfo(int_arg)) {
2270 d_fprintf(stderr, "Could not get info for uid "
2271 "%d\n", int_arg);
2272 goto done;
2274 break;
2275 case OPT_GROUP_INFO:
2276 if ( !wbinfo_get_groupinfo(string_arg)) {
2277 d_fprintf(stderr, "Could not get info for "
2278 "group %s\n", string_arg);
2279 goto done;
2281 break;
2282 case OPT_GID_INFO:
2283 if ( !wbinfo_get_gidinfo(int_arg)) {
2284 d_fprintf(stderr, "Could not get info for gid "
2285 "%d\n", int_arg);
2286 goto done;
2288 break;
2289 case 'r':
2290 if (!wbinfo_get_usergroups(string_arg)) {
2291 d_fprintf(stderr,
2292 "Could not get groups for user %s\n",
2293 string_arg);
2294 goto done;
2296 break;
2297 case OPT_USERSIDS:
2298 if (!wbinfo_get_usersids(string_arg)) {
2299 d_fprintf(stderr, "Could not get group SIDs "
2300 "for user SID %s\n",
2301 string_arg);
2302 goto done;
2304 break;
2305 case OPT_USERDOMGROUPS:
2306 if (!wbinfo_get_userdomgroups(string_arg)) {
2307 d_fprintf(stderr, "Could not get user's domain "
2308 "groups for user SID %s\n",
2309 string_arg);
2310 goto done;
2312 break;
2313 case OPT_SIDALIASES:
2314 if (!wbinfo_get_sidaliases(opt_domain_name,
2315 string_arg)) {
2316 d_fprintf(stderr, "Could not get sid aliases "
2317 "for user SID %s\n", string_arg);
2318 goto done;
2320 break;
2321 case 'a': {
2322 bool got_error = false;
2324 if (!wbinfo_auth(string_arg)) {
2325 d_fprintf(stderr,
2326 "Could not authenticate user "
2327 "%s with plaintext "
2328 "password\n", string_arg);
2329 got_error = true;
2332 if (!wbinfo_auth_crap(string_arg, use_ntlmv2,
2333 use_lanman)) {
2334 d_fprintf(stderr,
2335 "Could not authenticate user "
2336 "%s with challenge/response\n",
2337 string_arg);
2338 got_error = true;
2341 if (got_error)
2342 goto done;
2343 break;
2345 case OPT_PAM_LOGON:
2346 if (!wbinfo_pam_logon(string_arg)) {
2347 d_fprintf(stderr, "pam_logon failed for %s\n",
2348 string_arg);
2349 goto done;
2351 break;
2352 case OPT_LOGOFF:
2354 wbcErr wbc_status;
2356 wbc_status = wbcLogoffUser(logoff_user, logoff_uid,
2357 "");
2358 d_printf("Logoff %s (%d): %s\n", logoff_user,
2359 logoff_uid, wbcErrorString(wbc_status));
2360 break;
2362 case 'K': {
2363 uint32_t flags = WBFLAG_PAM_KRB5 |
2364 WBFLAG_PAM_CACHED_LOGIN |
2365 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
2366 WBFLAG_PAM_INFO3_TEXT |
2367 WBFLAG_PAM_CONTACT_TRUSTDOM;
2369 if (!wbinfo_auth_krb5(string_arg, "FILE",
2370 flags)) {
2371 d_fprintf(stderr,
2372 "Could not authenticate user "
2373 "[%s] with Kerberos "
2374 "(ccache: %s)\n", string_arg,
2375 "FILE");
2376 goto done;
2378 break;
2380 case 'k':
2381 if (!wbinfo_klog(string_arg)) {
2382 d_fprintf(stderr, "Could not klog user\n");
2383 goto done;
2385 break;
2386 case 'p':
2387 if (!wbinfo_ping()) {
2388 d_fprintf(stderr, "could not ping winbindd!\n");
2389 goto done;
2391 break;
2392 case OPT_SET_AUTH_USER:
2393 if (!wbinfo_set_auth_user(string_arg)) {
2394 goto done;
2396 break;
2397 case OPT_GET_AUTH_USER:
2398 wbinfo_get_auth_user();
2399 goto done;
2400 break;
2401 case OPT_CCACHE_SAVE:
2402 if (!wbinfo_ccache_save(string_arg)) {
2403 goto done;
2405 break;
2406 case OPT_GETDCNAME:
2407 if (!wbinfo_getdcname(string_arg)) {
2408 goto done;
2410 break;
2411 case OPT_DSGETDCNAME:
2412 if (!wbinfo_dsgetdcname(string_arg, 0)) {
2413 goto done;
2415 break;
2416 case OPT_DC_INFO:
2417 if (!wbinfo_dc_info(string_arg)) {
2418 goto done;
2420 break;
2421 case OPT_SEPARATOR: {
2422 const char sep = winbind_separator();
2423 if ( !sep ) {
2424 goto done;
2426 d_printf("%c\n", sep);
2427 break;
2429 case OPT_LIST_ALL_DOMAINS:
2430 if (!wbinfo_list_domains(true, verbose)) {
2431 goto done;
2433 break;
2434 case OPT_LIST_OWN_DOMAIN:
2435 if (!wbinfo_list_own_domain()) {
2436 goto done;
2438 break;
2439 case OPT_CHANGE_USER_PASSWORD:
2440 if (!wbinfo_change_user_password(string_arg)) {
2441 d_fprintf(stderr,
2442 "Could not change user password "
2443 "for user %s\n", string_arg);
2444 goto done;
2446 break;
2448 /* generic configuration options */
2449 case OPT_DOMAIN_NAME:
2450 case OPT_VERBOSE:
2451 case OPT_NTLMV2:
2452 case OPT_LANMAN:
2453 case OPT_LOGOFF_USER:
2454 case OPT_LOGOFF_UID:
2455 break;
2456 default:
2457 d_fprintf(stderr, "Invalid option\n");
2458 poptPrintHelp(pc, stderr, 0);
2459 goto done;
2463 result = 0;
2465 /* Exit code */
2467 done:
2468 talloc_free(frame);
2470 poptFreeContext(pc);
2471 return result;