s3-waf: Add dummy configuration option for pthreadpool
[Samba/gebeck_regimport.git] / nsswitch / wbinfo.c
blob3c1db8bf03d151091b481dbc8404092728b18e50
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 #endif
33 #ifdef DBGC_CLASS
34 #undef DBGC_CLASS
35 #define DBGC_CLASS DBGC_WINBIND
36 #endif
38 static struct wbcInterfaceDetails *init_interface_details(void)
40 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
41 static struct wbcInterfaceDetails *details;
43 if (details) {
44 return details;
47 wbc_status = wbcInterfaceDetails(&details);
48 if (!WBC_ERROR_IS_OK(wbc_status)) {
49 d_fprintf(stderr, "could not obtain winbind interface "
50 "details: %s\n", wbcErrorString(wbc_status));
53 return details;
56 static char winbind_separator(void)
58 struct wbcInterfaceDetails *details;
59 static bool got_sep;
60 static char sep;
62 if (got_sep)
63 return sep;
65 details = init_interface_details();
67 if (!details) {
68 d_fprintf(stderr, "could not obtain winbind separator!\n");
69 return 0;
72 sep = details->winbind_separator;
73 got_sep = true;
75 if (!sep) {
76 d_fprintf(stderr, "winbind separator was NULL!\n");
77 return 0;
80 return sep;
83 static const char *get_winbind_domain(void)
85 static struct wbcInterfaceDetails *details;
87 details = init_interface_details();
89 if (!details) {
90 d_fprintf(stderr, "could not obtain winbind domain name!\n");
91 return 0;
94 return details->netbios_domain;
97 static const char *get_winbind_netbios_name(void)
99 static struct wbcInterfaceDetails *details;
101 details = init_interface_details();
103 if (!details) {
104 d_fprintf(stderr, "could not obtain winbind netbios name!\n");
105 return 0;
108 return details->netbios_name;
111 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
112 form DOMAIN/user into a domain and a user */
114 static bool parse_wbinfo_domain_user(const char *domuser, fstring domain,
115 fstring user)
118 char *p = strchr(domuser,winbind_separator());
120 if (!p) {
121 /* Maybe it was a UPN? */
122 if ((p = strchr(domuser, '@')) != NULL) {
123 fstrcpy(domain, "");
124 fstrcpy(user, domuser);
125 return true;
128 fstrcpy(user, domuser);
129 fstrcpy(domain, get_winbind_domain());
130 return true;
133 fstrcpy(user, p+1);
134 fstrcpy(domain, domuser);
135 domain[PTR_DIFF(p, domuser)] = 0;
136 strupper_m(domain);
138 return true;
141 /* Parse string of "uid,sid" or "gid,sid" into separate int and string values.
142 * Return true if input was valid, false otherwise. */
143 static bool parse_mapping_arg(char *arg, int *id, char **sid)
145 char *tmp, *endptr;
147 if (!arg || !*arg)
148 return false;
150 tmp = strtok(arg, ",");
151 *sid = strtok(NULL, ",");
153 if (!tmp || !*tmp || !*sid || !**sid)
154 return false;
156 /* Because atoi() can return 0 on invalid input, which would be a valid
157 * UID/GID we must use strtoul() and do error checking */
158 *id = strtoul(tmp, &endptr, 10);
160 if (endptr[0] != '\0')
161 return false;
163 return true;
166 /* pull pwent info for a given user */
168 static bool wbinfo_get_userinfo(char *user)
170 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
171 struct passwd *pwd = NULL;
173 wbc_status = wbcGetpwnam(user, &pwd);
174 if (!WBC_ERROR_IS_OK(wbc_status)) {
175 d_fprintf(stderr, "failed to call wbcGetpwnam: %s",
176 wbcErrorString(wbc_status));
177 return false;
180 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
181 pwd->pw_name,
182 pwd->pw_passwd,
183 (unsigned int)pwd->pw_uid,
184 (unsigned int)pwd->pw_gid,
185 pwd->pw_gecos,
186 pwd->pw_dir,
187 pwd->pw_shell);
189 return true;
192 /* pull pwent info for a given uid */
193 static bool wbinfo_get_uidinfo(int uid)
195 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
196 struct passwd *pwd = NULL;
198 wbc_status = wbcGetpwuid(uid, &pwd);
199 if (!WBC_ERROR_IS_OK(wbc_status)) {
200 d_fprintf(stderr, "failed to call wbcGetpwuid: %s",
201 wbcErrorString(wbc_status));
202 return false;
205 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
206 pwd->pw_name,
207 pwd->pw_passwd,
208 (unsigned int)pwd->pw_uid,
209 (unsigned int)pwd->pw_gid,
210 pwd->pw_gecos,
211 pwd->pw_dir,
212 pwd->pw_shell);
214 return true;
217 static bool wbinfo_get_user_sidinfo(const char *sid_str)
219 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
220 struct passwd *pwd = NULL;
221 struct wbcDomainSid sid;
223 wbc_status = wbcStringToSid(sid_str, &sid);
224 wbc_status = wbcGetpwsid(&sid, &pwd);
225 if (!WBC_ERROR_IS_OK(wbc_status)) {
226 d_fprintf(stderr, "failed to call wbcGetpwsid: %s",
227 wbcErrorString(wbc_status));
228 return false;
231 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
232 pwd->pw_name,
233 pwd->pw_passwd,
234 (unsigned int)pwd->pw_uid,
235 (unsigned int)pwd->pw_gid,
236 pwd->pw_gecos,
237 pwd->pw_dir,
238 pwd->pw_shell);
240 return true;
244 /* pull grent for a given group */
245 static bool wbinfo_get_groupinfo(const char *group)
247 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
248 struct group *grp;
249 char **mem;
251 wbc_status = wbcGetgrnam(group, &grp);
252 if (!WBC_ERROR_IS_OK(wbc_status)) {
253 d_fprintf(stderr, "failed to call wbcGetgrnam: %s",
254 wbcErrorString(wbc_status));
255 return false;
258 d_printf("%s:%s:%u:",
259 grp->gr_name,
260 grp->gr_passwd,
261 (unsigned int)grp->gr_gid);
263 mem = grp->gr_mem;
264 while (*mem != NULL) {
265 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
266 mem += 1;
268 d_printf("\n");
270 wbcFreeMemory(grp);
272 return true;
275 /* pull grent for a given gid */
276 static bool wbinfo_get_gidinfo(int gid)
278 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
279 struct group *grp;
280 char **mem;
282 wbc_status = wbcGetgrgid(gid, &grp);
283 if (!WBC_ERROR_IS_OK(wbc_status)) {
284 d_fprintf(stderr, "failed to call wbcGetgrgid: %s",
285 wbcErrorString(wbc_status));
286 return false;
289 d_printf("%s:%s:%u:",
290 grp->gr_name,
291 grp->gr_passwd,
292 (unsigned int)grp->gr_gid);
294 mem = grp->gr_mem;
295 while (*mem != NULL) {
296 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
297 mem += 1;
299 d_printf("\n");
301 wbcFreeMemory(grp);
303 return true;
306 /* List groups a user is a member of */
308 static bool wbinfo_get_usergroups(const char *user)
310 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
311 uint32_t num_groups;
312 uint32_t i;
313 gid_t *groups = NULL;
315 /* Send request */
317 wbc_status = wbcGetGroups(user, &num_groups, &groups);
318 if (!WBC_ERROR_IS_OK(wbc_status)) {
319 d_fprintf(stderr, "failed to call wbcGetGroups: %s",
320 wbcErrorString(wbc_status));
321 return false;
324 for (i = 0; i < num_groups; i++) {
325 d_printf("%d\n", (int)groups[i]);
328 wbcFreeMemory(groups);
330 return true;
334 /* List group SIDs a user SID is a member of */
335 static bool wbinfo_get_usersids(const char *user_sid_str)
337 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
338 uint32_t num_sids;
339 uint32_t i;
340 struct wbcDomainSid user_sid, *sids = NULL;
342 /* Send request */
344 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
345 if (!WBC_ERROR_IS_OK(wbc_status)) {
346 d_fprintf(stderr, "failed to call wbcStringToSid: %s",
347 wbcErrorString(wbc_status));
348 return false;
351 wbc_status = wbcLookupUserSids(&user_sid, false, &num_sids, &sids);
352 if (!WBC_ERROR_IS_OK(wbc_status)) {
353 d_fprintf(stderr, "failed to call wbcLookupUserSids: %s",
354 wbcErrorString(wbc_status));
355 return false;
358 for (i = 0; i < num_sids; i++) {
359 char *str = NULL;
360 wbc_status = wbcSidToString(&sids[i], &str);
361 if (!WBC_ERROR_IS_OK(wbc_status)) {
362 d_fprintf(stderr, "failed to call wbcSidToString: %s",
363 wbcErrorString(wbc_status));
364 wbcFreeMemory(sids);
365 return false;
367 d_printf("%s\n", str);
368 wbcFreeMemory(str);
371 wbcFreeMemory(sids);
373 return true;
376 static bool wbinfo_get_userdomgroups(const char *user_sid_str)
378 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
379 uint32_t num_sids;
380 uint32_t i;
381 struct wbcDomainSid user_sid, *sids = NULL;
383 /* Send request */
385 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
386 if (!WBC_ERROR_IS_OK(wbc_status)) {
387 d_fprintf(stderr, "failed to call wbcSidToString: %s",
388 wbcErrorString(wbc_status));
389 return false;
392 wbc_status = wbcLookupUserSids(&user_sid, true, &num_sids, &sids);
393 if (!WBC_ERROR_IS_OK(wbc_status)) {
394 d_fprintf(stderr, "failed to call wbcLookupUserSids: %s",
395 wbcErrorString(wbc_status));
396 return false;
399 for (i = 0; i < num_sids; i++) {
400 char *str = NULL;
401 wbc_status = wbcSidToString(&sids[i], &str);
402 if (!WBC_ERROR_IS_OK(wbc_status)) {
403 d_fprintf(stderr, "failed to call wbcSidToString: %s",
404 wbcErrorString(wbc_status));
405 wbcFreeMemory(sids);
406 return false;
408 d_printf("%s\n", str);
409 wbcFreeMemory(str);
412 wbcFreeMemory(sids);
414 return true;
417 static bool wbinfo_get_sidaliases(const char *domain,
418 const char *user_sid_str)
420 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
421 struct wbcDomainInfo *dinfo = NULL;
422 uint32_t i;
423 struct wbcDomainSid user_sid;
424 uint32_t *alias_rids = NULL;
425 uint32_t num_alias_rids;
426 char *domain_sid_str = NULL;
428 /* Send request */
429 if ((domain == NULL) || (strequal(domain, ".")) ||
430 (domain[0] == '\0')) {
431 domain = get_winbind_domain();
434 /* Send request */
436 wbc_status = wbcDomainInfo(domain, &dinfo);
437 if (!WBC_ERROR_IS_OK(wbc_status)) {
438 d_fprintf(stderr, "wbcDomainInfo(%s) failed: %s\n", domain,
439 wbcErrorString(wbc_status));
440 goto done;
442 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
443 if (!WBC_ERROR_IS_OK(wbc_status)) {
444 goto done;
447 wbc_status = wbcGetSidAliases(&dinfo->sid, &user_sid, 1,
448 &alias_rids, &num_alias_rids);
449 if (!WBC_ERROR_IS_OK(wbc_status)) {
450 goto done;
453 wbc_status = wbcSidToString(&dinfo->sid, &domain_sid_str);
454 if (!WBC_ERROR_IS_OK(wbc_status)) {
455 goto done;
458 for (i = 0; i < num_alias_rids; i++) {
459 d_printf("%s-%d\n", domain_sid_str, alias_rids[i]);
462 wbcFreeMemory(alias_rids);
464 done:
465 if (domain_sid_str) {
466 wbcFreeMemory(domain_sid_str);
468 if (dinfo) {
469 wbcFreeMemory(dinfo);
471 return (WBC_ERR_SUCCESS == wbc_status);
475 /* Convert NetBIOS name to IP */
477 static bool wbinfo_wins_byname(const char *name)
479 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
480 char *ip = NULL;
482 wbc_status = wbcResolveWinsByName(name, &ip);
483 if (!WBC_ERROR_IS_OK(wbc_status)) {
484 d_fprintf(stderr, "failed to call wbcResolveWinsByName: %s",
485 wbcErrorString(wbc_status));
486 return false;
489 /* Display response */
491 d_printf("%s\n", ip);
493 wbcFreeMemory(ip);
495 return true;
498 /* Convert IP to NetBIOS name */
500 static bool wbinfo_wins_byip(const char *ip)
502 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
503 char *name = NULL;
505 wbc_status = wbcResolveWinsByIP(ip, &name);
506 if (!WBC_ERROR_IS_OK(wbc_status)) {
507 d_fprintf(stderr, "failed to call wbcResolveWinsByIP: %s",
508 wbcErrorString(wbc_status));
509 return false;
512 /* Display response */
514 d_printf("%s\n", name);
516 wbcFreeMemory(name);
518 return true;
521 /* List all/trusted domains */
523 static bool wbinfo_list_domains(bool list_all_domains, bool verbose)
525 struct wbcDomainInfo *domain_list = NULL;
526 size_t num_domains;
527 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
528 bool print_all = !list_all_domains && verbose;
529 int i;
531 wbc_status = wbcListTrusts(&domain_list, &num_domains);
532 if (!WBC_ERROR_IS_OK(wbc_status)) {
533 d_fprintf(stderr, "failed to call wbcListTrusts: %s",
534 wbcErrorString(wbc_status));
535 return false;
538 if (print_all) {
539 d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n",
540 "Domain Name", "DNS Domain", "Trust Type",
541 "Transitive", "In", "Out");
544 for (i=0; i<num_domains; i++) {
545 if (print_all) {
546 d_printf("%-16s", domain_list[i].short_name);
547 } else {
548 d_printf("%s", domain_list[i].short_name);
549 d_printf("\n");
550 continue;
553 d_printf("%-24s", domain_list[i].dns_name);
555 switch(domain_list[i].trust_type) {
556 case WBC_DOMINFO_TRUSTTYPE_NONE:
557 d_printf("None ");
558 break;
559 case WBC_DOMINFO_TRUSTTYPE_FOREST:
560 d_printf("Forest ");
561 break;
562 case WBC_DOMINFO_TRUSTTYPE_EXTERNAL:
563 d_printf("External ");
564 break;
565 case WBC_DOMINFO_TRUSTTYPE_IN_FOREST:
566 d_printf("In-Forest ");
567 break;
570 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_TRANSITIVE) {
571 d_printf("Yes ");
572 } else {
573 d_printf("No ");
576 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_INCOMING) {
577 d_printf("Yes ");
578 } else {
579 d_printf("No ");
582 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_OUTGOING) {
583 d_printf("Yes ");
584 } else {
585 d_printf("No ");
588 d_printf("\n");
591 return true;
594 /* List own domain */
596 static bool wbinfo_list_own_domain(void)
598 d_printf("%s\n", get_winbind_domain());
600 return true;
603 /* show sequence numbers */
604 static bool wbinfo_show_sequence(const char *domain)
606 d_printf("This command has been deprecated. Please use the "
607 "--online-status option instead.\n");
608 return false;
611 /* show sequence numbers */
612 static bool wbinfo_show_onlinestatus(const char *domain)
614 struct wbcDomainInfo *domain_list = NULL;
615 size_t num_domains;
616 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
617 int i;
619 wbc_status = wbcListTrusts(&domain_list, &num_domains);
620 if (!WBC_ERROR_IS_OK(wbc_status)) {
621 d_fprintf(stderr, "failed to call wbcListTrusts: %s",
622 wbcErrorString(wbc_status));
623 return false;
626 for (i=0; i<num_domains; i++) {
627 bool is_offline;
629 if (domain) {
630 if (!strequal(domain_list[i].short_name, domain)) {
631 continue;
635 is_offline = (domain_list[i].domain_flags &
636 WBC_DOMINFO_DOMAIN_OFFLINE);
638 d_printf("%s : %s\n",
639 domain_list[i].short_name,
640 is_offline ? "offline" : "online" );
643 return true;
647 /* Show domain info */
649 static bool wbinfo_domain_info(const char *domain)
651 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
652 struct wbcDomainInfo *dinfo = NULL;
653 char *sid_str = NULL;
655 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
656 domain = get_winbind_domain();
659 /* Send request */
661 wbc_status = wbcDomainInfo(domain, &dinfo);
662 if (!WBC_ERROR_IS_OK(wbc_status)) {
663 d_fprintf(stderr, "failed to call wbcDomainInfo: %s",
664 wbcErrorString(wbc_status));
665 return false;
668 wbc_status = wbcSidToString(&dinfo->sid, &sid_str);
669 if (!WBC_ERROR_IS_OK(wbc_status)) {
670 d_fprintf(stderr, "failed to call wbcSidToString: %s",
671 wbcErrorString(wbc_status));
672 wbcFreeMemory(dinfo);
673 return false;
676 /* Display response */
678 d_printf("Name : %s\n", dinfo->short_name);
679 d_printf("Alt_Name : %s\n", dinfo->dns_name);
681 d_printf("SID : %s\n", sid_str);
683 d_printf("Active Directory : %s\n",
684 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_AD) ? "Yes" : "No");
685 d_printf("Native : %s\n",
686 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_NATIVE) ?
687 "Yes" : "No");
689 d_printf("Primary : %s\n",
690 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_PRIMARY) ?
691 "Yes" : "No");
693 wbcFreeMemory(sid_str);
694 wbcFreeMemory(dinfo);
696 return true;
699 /* Get a foreign DC's name */
700 static bool wbinfo_getdcname(const char *domain_name)
702 struct winbindd_request request;
703 struct winbindd_response response;
705 ZERO_STRUCT(request);
706 ZERO_STRUCT(response);
708 fstrcpy(request.domain_name, domain_name);
710 /* Send request */
712 if (winbindd_request_response(WINBINDD_GETDCNAME, &request,
713 &response) != NSS_STATUS_SUCCESS) {
714 d_fprintf(stderr, "Could not get dc name for %s\n",domain_name);
715 return false;
718 /* Display response */
720 d_printf("%s\n", response.data.dc_name);
722 return true;
725 /* Find a DC */
726 static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
728 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
729 struct wbcDomainControllerInfoEx *dc_info;
730 char *str = NULL;
732 wbc_status = wbcLookupDomainControllerEx(domain_name, NULL, NULL,
733 flags | DS_DIRECTORY_SERVICE_REQUIRED,
734 &dc_info);
735 if (!WBC_ERROR_IS_OK(wbc_status)) {
736 printf("Could not find dc for %s\n", domain_name);
737 return false;
740 wbcGuidToString(dc_info->domain_guid, &str);
742 d_printf("%s\n", dc_info->dc_unc);
743 d_printf("%s\n", dc_info->dc_address);
744 d_printf("%d\n", dc_info->dc_address_type);
745 d_printf("%s\n", str);
746 d_printf("%s\n", dc_info->domain_name);
747 d_printf("%s\n", dc_info->forest_name);
748 d_printf("0x%08x\n", dc_info->dc_flags);
749 d_printf("%s\n", dc_info->dc_site_name);
750 d_printf("%s\n", dc_info->client_site_name);
752 return true;
755 /* Check trust account password */
757 static bool wbinfo_check_secret(const char *domain)
759 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
760 struct wbcAuthErrorInfo *error = NULL;
761 const char *domain_name;
763 if (domain) {
764 domain_name = domain;
765 } else {
766 domain_name = get_winbind_domain();
769 wbc_status = wbcCheckTrustCredentials(domain_name, &error);
771 d_printf("checking the trust secret for domain %s via RPC calls %s\n",
772 domain_name,
773 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
775 if (wbc_status == WBC_ERR_AUTH_ERROR) {
776 d_fprintf(stderr, "error code was %s (0x%x)\n",
777 error->nt_string, error->nt_status);
778 wbcFreeMemory(error);
780 if (!WBC_ERROR_IS_OK(wbc_status)) {
781 d_fprintf(stderr, "failed to call wbcCheckTrustCredentials: %s",
782 wbcErrorString(wbc_status));
783 return false;
786 return true;
789 /* Change trust account password */
791 static bool wbinfo_change_secret(const char *domain)
793 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
794 struct wbcAuthErrorInfo *error = NULL;
795 const char *domain_name;
797 if (domain) {
798 domain_name = domain;
799 } else {
800 domain_name = get_winbind_domain();
803 wbc_status = wbcChangeTrustCredentials(domain_name, &error);
805 d_printf("changing the trust secret for domain %s via RPC calls %s\n",
806 domain_name,
807 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
809 if (wbc_status == WBC_ERR_AUTH_ERROR) {
810 d_fprintf(stderr, "error code was %s (0x%x)\n",
811 error->nt_string, error->nt_status);
812 wbcFreeMemory(error);
814 if (!WBC_ERROR_IS_OK(wbc_status)) {
815 d_fprintf(stderr, "failed to call wbcChangeTrustCredentials: %s",
816 wbcErrorString(wbc_status));
817 return false;
820 return true;
823 /* Check DC connection */
825 static bool wbinfo_ping_dc(void)
827 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
828 struct wbcAuthErrorInfo *error = NULL;
830 wbc_status = wbcPingDc(NULL, &error);
832 d_printf("checking the NETLOGON dc connection %s\n",
833 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
835 if (wbc_status == WBC_ERR_AUTH_ERROR) {
836 d_fprintf(stderr, "error code was %s (0x%x)\n",
837 error->nt_string, error->nt_status);
838 wbcFreeMemory(error);
840 if (!WBC_ERROR_IS_OK(wbc_status)) {
841 d_fprintf(stderr, "failed to call wbcPingDc: %s",
842 wbcErrorString(wbc_status));
843 return false;
846 return true;
849 /* Convert uid to sid */
851 static bool wbinfo_uid_to_sid(uid_t uid)
853 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
854 struct wbcDomainSid sid;
855 char *sid_str = NULL;
857 /* Send request */
859 wbc_status = wbcUidToSid(uid, &sid);
860 if (!WBC_ERROR_IS_OK(wbc_status)) {
861 d_fprintf(stderr, "failed to call wbcUidToSid: %s",
862 wbcErrorString(wbc_status));
863 return false;
866 wbc_status = wbcSidToString(&sid, &sid_str);
867 if (!WBC_ERROR_IS_OK(wbc_status)) {
868 d_fprintf(stderr, "failed to call wbcSidToString: %s",
869 wbcErrorString(wbc_status));
870 return false;
873 /* Display response */
875 d_printf("%s\n", sid_str);
877 wbcFreeMemory(sid_str);
879 return true;
882 /* Convert gid to sid */
884 static bool wbinfo_gid_to_sid(gid_t gid)
886 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
887 struct wbcDomainSid sid;
888 char *sid_str = NULL;
890 /* Send request */
892 wbc_status = wbcGidToSid(gid, &sid);
893 if (!WBC_ERROR_IS_OK(wbc_status)) {
894 d_fprintf(stderr, "failed to call wbcGidToSid: %s",
895 wbcErrorString(wbc_status));
896 return false;
899 wbc_status = wbcSidToString(&sid, &sid_str);
900 if (!WBC_ERROR_IS_OK(wbc_status)) {
901 d_fprintf(stderr, "failed to call wbcSidToString: %s",
902 wbcErrorString(wbc_status));
903 return false;
906 /* Display response */
908 d_printf("%s\n", sid_str);
910 wbcFreeMemory(sid_str);
912 return true;
915 /* Convert sid to uid */
917 static bool wbinfo_sid_to_uid(const char *sid_str)
919 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
920 struct wbcDomainSid sid;
921 uid_t uid;
923 /* Send request */
925 wbc_status = wbcStringToSid(sid_str, &sid);
926 if (!WBC_ERROR_IS_OK(wbc_status)) {
927 d_fprintf(stderr, "failed to call wbcStringToSid: %s",
928 wbcErrorString(wbc_status));
929 return false;
932 wbc_status = wbcSidToUid(&sid, &uid);
933 if (!WBC_ERROR_IS_OK(wbc_status)) {
934 d_fprintf(stderr, "failed to call wbcSidToUid: %s",
935 wbcErrorString(wbc_status));
936 return false;
939 /* Display response */
941 d_printf("%d\n", (int)uid);
943 return true;
946 static bool wbinfo_sid_to_gid(const char *sid_str)
948 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
949 struct wbcDomainSid sid;
950 gid_t gid;
952 /* Send request */
954 wbc_status = wbcStringToSid(sid_str, &sid);
955 if (!WBC_ERROR_IS_OK(wbc_status)) {
956 d_fprintf(stderr, "failed to call wbcStringToSid: %s",
957 wbcErrorString(wbc_status));
958 return false;
961 wbc_status = wbcSidToGid(&sid, &gid);
962 if (!WBC_ERROR_IS_OK(wbc_status)) {
963 d_fprintf(stderr, "failed to call wbcSidToGid: %s",
964 wbcErrorString(wbc_status));
965 return false;
968 /* Display response */
970 d_printf("%d\n", (int)gid);
972 return true;
975 static bool wbinfo_allocate_uid(void)
977 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
978 uid_t uid;
980 /* Send request */
982 wbc_status = wbcAllocateUid(&uid);
983 if (!WBC_ERROR_IS_OK(wbc_status)) {
984 d_fprintf(stderr, "failed to call wbcAllocateUid: %s",
985 wbcErrorString(wbc_status));
986 return false;
989 /* Display response */
991 d_printf("New uid: %u\n", (unsigned int)uid);
993 return true;
996 static bool wbinfo_allocate_gid(void)
998 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
999 gid_t gid;
1001 /* Send request */
1003 wbc_status = wbcAllocateGid(&gid);
1004 if (!WBC_ERROR_IS_OK(wbc_status)) {
1005 d_fprintf(stderr, "failed to call wbcAllocateGid: %s",
1006 wbcErrorString(wbc_status));
1007 return false;
1010 /* Display response */
1012 d_printf("New gid: %u\n", (unsigned int)gid);
1014 return true;
1017 static bool wbinfo_set_uid_mapping(uid_t uid, const char *sid_str)
1019 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1020 struct wbcDomainSid sid;
1022 /* Send request */
1024 wbc_status = wbcStringToSid(sid_str, &sid);
1025 if (!WBC_ERROR_IS_OK(wbc_status)) {
1026 d_fprintf(stderr, "failed to call wbcStringToSid: %s",
1027 wbcErrorString(wbc_status));
1028 return false;
1031 wbc_status = wbcSetUidMapping(uid, &sid);
1032 if (!WBC_ERROR_IS_OK(wbc_status)) {
1033 d_fprintf(stderr, "failed to call wbcSetUidMapping: %s",
1034 wbcErrorString(wbc_status));
1035 return false;
1038 /* Display response */
1040 d_printf("uid %u now mapped to sid %s\n",
1041 (unsigned int)uid, sid_str);
1043 return true;
1046 static bool wbinfo_set_gid_mapping(gid_t gid, const char *sid_str)
1048 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1049 struct wbcDomainSid sid;
1051 /* Send request */
1053 wbc_status = wbcStringToSid(sid_str, &sid);
1054 if (!WBC_ERROR_IS_OK(wbc_status)) {
1055 d_fprintf(stderr, "failed to call wbcStringToSid: %s",
1056 wbcErrorString(wbc_status));
1057 return false;
1060 wbc_status = wbcSetGidMapping(gid, &sid);
1061 if (!WBC_ERROR_IS_OK(wbc_status)) {
1062 d_fprintf(stderr, "failed to call wbcSetGidMapping: %s",
1063 wbcErrorString(wbc_status));
1064 return false;
1067 /* Display response */
1069 d_printf("gid %u now mapped to sid %s\n",
1070 (unsigned int)gid, sid_str);
1072 return true;
1075 static bool wbinfo_remove_uid_mapping(uid_t uid, const char *sid_str)
1077 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1078 struct wbcDomainSid sid;
1080 /* Send request */
1082 wbc_status = wbcStringToSid(sid_str, &sid);
1083 if (!WBC_ERROR_IS_OK(wbc_status)) {
1084 d_fprintf(stderr, "failed to call wbcStringToSid: %s",
1085 wbcErrorString(wbc_status));
1086 return false;
1089 wbc_status = wbcRemoveUidMapping(uid, &sid);
1090 if (!WBC_ERROR_IS_OK(wbc_status)) {
1091 d_fprintf(stderr, "failed to call wbcRemoveUidMapping: %s",
1092 wbcErrorString(wbc_status));
1093 return false;
1096 /* Display response */
1098 d_printf("Removed uid %u to sid %s mapping\n",
1099 (unsigned int)uid, sid_str);
1101 return true;
1104 static bool wbinfo_remove_gid_mapping(gid_t gid, const char *sid_str)
1106 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1107 struct wbcDomainSid sid;
1109 /* Send request */
1111 wbc_status = wbcStringToSid(sid_str, &sid);
1112 if (!WBC_ERROR_IS_OK(wbc_status)) {
1113 d_fprintf(stderr, "failed to call wbcStringToSid: %s",
1114 wbcErrorString(wbc_status));
1115 return false;
1118 wbc_status = wbcRemoveGidMapping(gid, &sid);
1119 if (!WBC_ERROR_IS_OK(wbc_status)) {
1120 d_fprintf(stderr, "failed to call wbcRemoveGidMapping: %s",
1121 wbcErrorString(wbc_status));
1122 return false;
1125 /* Display response */
1127 d_printf("Removed gid %u to sid %s mapping\n",
1128 (unsigned int)gid, sid_str);
1130 return true;
1133 /* Convert sid to string */
1135 static bool wbinfo_lookupsid(const char *sid_str)
1137 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1138 struct wbcDomainSid sid;
1139 char *domain;
1140 char *name;
1141 enum wbcSidType type;
1143 /* Send off request */
1145 wbc_status = wbcStringToSid(sid_str, &sid);
1146 if (!WBC_ERROR_IS_OK(wbc_status)) {
1147 d_fprintf(stderr, "failed to call wbcStringToSid: %s",
1148 wbcErrorString(wbc_status));
1149 return false;
1152 wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
1153 if (!WBC_ERROR_IS_OK(wbc_status)) {
1154 d_fprintf(stderr, "failed to call wbcLookupSid: %s",
1155 wbcErrorString(wbc_status));
1156 return false;
1159 /* Display response */
1161 d_printf("%s%c%s %d\n",
1162 domain, winbind_separator(), name, type);
1164 return true;
1167 /* Convert sid to fullname */
1169 static bool wbinfo_lookupsid_fullname(const char *sid_str)
1171 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1172 struct wbcDomainSid sid;
1173 char *domain;
1174 char *name;
1175 enum wbcSidType type;
1177 /* Send off 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",
1182 wbcErrorString(wbc_status));
1183 return false;
1186 wbc_status = wbcGetDisplayName(&sid, &domain, &name, &type);
1187 if (!WBC_ERROR_IS_OK(wbc_status)) {
1188 d_fprintf(stderr, "failed to call wbcGetDisplayName: %s",
1189 wbcErrorString(wbc_status));
1190 return false;
1193 /* Display response */
1195 d_printf("%s%c%s %d\n",
1196 domain, winbind_separator(), name, type);
1198 return true;
1201 /* Lookup a list of RIDs */
1203 static bool wbinfo_lookuprids(const char *domain, const char *arg)
1205 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1206 struct wbcDomainInfo *dinfo = NULL;
1207 char *domain_name = NULL;
1208 const char **names = NULL;
1209 enum wbcSidType *types = NULL;
1210 size_t i;
1211 int num_rids;
1212 uint32_t *rids = NULL;
1213 const char *p;
1214 char *ridstr;
1215 TALLOC_CTX *mem_ctx = NULL;
1216 bool ret = false;
1218 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
1219 domain = get_winbind_domain();
1222 /* Send request */
1224 wbc_status = wbcDomainInfo(domain, &dinfo);
1225 if (!WBC_ERROR_IS_OK(wbc_status)) {
1226 d_printf("wbcDomainInfo(%s) failed: %s\n", domain,
1227 wbcErrorString(wbc_status));
1228 goto done;
1231 mem_ctx = talloc_new(NULL);
1232 if (mem_ctx == NULL) {
1233 d_printf("talloc_new failed\n");
1234 goto done;
1237 num_rids = 0;
1238 rids = NULL;
1239 p = arg;
1241 while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
1242 uint32_t rid = strtoul(ridstr, NULL, 10);
1243 rids = talloc_realloc(mem_ctx, rids, uint32_t, num_rids + 1);
1244 if (rids == NULL) {
1245 d_printf("talloc_realloc failed\n");
1247 rids[num_rids] = rid;
1248 num_rids += 1;
1251 if (rids == NULL) {
1252 d_printf("no rids\n");
1253 goto done;
1256 wbc_status = wbcLookupRids(&dinfo->sid, num_rids, rids,
1257 (const char **)&domain_name, &names, &types);
1258 if (!WBC_ERROR_IS_OK(wbc_status)) {
1259 d_printf("winbind_lookup_rids failed: %s\n",
1260 wbcErrorString(wbc_status));
1261 goto done;
1264 d_printf("Domain: %s\n", domain_name);
1266 for (i=0; i<num_rids; i++) {
1267 d_printf("%8d: %s (%s)\n", rids[i], names[i],
1268 wbcSidTypeString(types[i]));
1271 ret = true;
1272 done:
1273 if (dinfo) {
1274 wbcFreeMemory(dinfo);
1276 if (domain_name) {
1277 wbcFreeMemory(domain_name);
1279 if (names) {
1280 wbcFreeMemory(names);
1282 if (types) {
1283 wbcFreeMemory(types);
1285 TALLOC_FREE(mem_ctx);
1286 return ret;
1289 /* Convert string to sid */
1291 static bool wbinfo_lookupname(const char *full_name)
1293 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1294 struct wbcDomainSid sid;
1295 char *sid_str;
1296 enum wbcSidType type;
1297 fstring domain_name;
1298 fstring account_name;
1300 /* Send off request */
1302 parse_wbinfo_domain_user(full_name, domain_name,
1303 account_name);
1305 wbc_status = wbcLookupName(domain_name, account_name,
1306 &sid, &type);
1307 if (!WBC_ERROR_IS_OK(wbc_status)) {
1308 d_fprintf(stderr, "failed to call wbcLookupName: %s",
1309 wbcErrorString(wbc_status));
1310 return false;
1313 wbc_status = wbcSidToString(&sid, &sid_str);
1314 if (!WBC_ERROR_IS_OK(wbc_status)) {
1315 d_fprintf(stderr, "failed to call wbcSidToString: %s",
1316 wbcErrorString(wbc_status));
1317 return false;
1320 /* Display response */
1322 d_printf("%s %s (%d)\n", sid_str, wbcSidTypeString(type), type);
1324 wbcFreeMemory(sid_str);
1326 return true;
1329 static char *wbinfo_prompt_pass(TALLOC_CTX *mem_ctx,
1330 const char *prefix,
1331 const char *username)
1333 char *prompt;
1334 const char *ret = NULL;
1336 prompt = talloc_asprintf(mem_ctx, "Enter %s's ", username);
1337 if (!prompt) {
1338 return NULL;
1340 if (prefix) {
1341 prompt = talloc_asprintf_append(prompt, "%s ", prefix);
1342 if (!prompt) {
1343 return NULL;
1346 prompt = talloc_asprintf_append(prompt, "password: ");
1347 if (!prompt) {
1348 return NULL;
1351 ret = getpass(prompt);
1352 TALLOC_FREE(prompt);
1354 return talloc_strdup(mem_ctx, ret);
1357 /* Authenticate a user with a plaintext password */
1359 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32_t flags)
1361 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1362 char *s = NULL;
1363 char *p = NULL;
1364 char *password = NULL;
1365 char *name = NULL;
1366 char *local_cctype = NULL;
1367 uid_t uid;
1368 struct wbcLogonUserParams params;
1369 struct wbcLogonUserInfo *info;
1370 struct wbcAuthErrorInfo *error;
1371 struct wbcUserPasswordPolicyInfo *policy;
1372 TALLOC_CTX *frame = talloc_tos();
1374 if ((s = talloc_strdup(frame, username)) == NULL) {
1375 return false;
1378 if ((p = strchr(s, '%')) != NULL) {
1379 *p = 0;
1380 p++;
1381 password = talloc_strdup(frame, p);
1382 } else {
1383 password = wbinfo_prompt_pass(frame, NULL, username);
1386 local_cctype = talloc_strdup(frame, cctype);
1388 name = s;
1390 uid = geteuid();
1392 params.username = name;
1393 params.password = password;
1394 params.num_blobs = 0;
1395 params.blobs = NULL;
1397 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1398 &params.blobs,
1399 "flags",
1401 (uint8_t *)&flags,
1402 sizeof(flags));
1403 if (!WBC_ERROR_IS_OK(wbc_status)) {
1404 d_fprintf(stderr, "failed to call wbcAddNamedBlob: %s",
1405 wbcErrorString(wbc_status));
1406 goto done;
1409 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1410 &params.blobs,
1411 "user_uid",
1413 (uint8_t *)&uid,
1414 sizeof(uid));
1415 if (!WBC_ERROR_IS_OK(wbc_status)) {
1416 d_fprintf(stderr, "failed to call wbcAddNamedBlob: %s",
1417 wbcErrorString(wbc_status));
1418 goto done;
1421 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1422 &params.blobs,
1423 "krb5_cc_type",
1425 (uint8_t *)local_cctype,
1426 strlen(cctype)+1);
1427 if (!WBC_ERROR_IS_OK(wbc_status)) {
1428 d_fprintf(stderr, "failed to call wbcAddNamedBlob: %s",
1429 wbcErrorString(wbc_status));
1430 goto done;
1433 wbc_status = wbcLogonUser(&params, &info, &error, &policy);
1435 d_printf("plaintext kerberos password authentication for [%s] %s "
1436 "(requesting cctype: %s)\n",
1437 username, WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed",
1438 cctype);
1440 if (error) {
1441 d_fprintf(stderr,
1442 "error code was %s (0x%x)\nerror messsage was: %s\n",
1443 error->nt_string,
1444 error->nt_status,
1445 error->display_string);
1448 if (WBC_ERROR_IS_OK(wbc_status)) {
1449 if (flags & WBFLAG_PAM_INFO3_TEXT) {
1450 if (info && info->info && info->info->user_flags &
1451 NETLOGON_CACHED_ACCOUNT) {
1452 d_printf("user_flgs: "
1453 "NETLOGON_CACHED_ACCOUNT\n");
1457 if (info) {
1458 int i;
1459 for (i=0; i < info->num_blobs; i++) {
1460 if (strequal(info->blobs[i].name,
1461 "krb5ccname")) {
1462 d_printf("credentials were put "
1463 "in: %s\n",
1464 (const char *)
1465 info->blobs[i].blob.data);
1466 break;
1469 } else {
1470 d_printf("no credentials cached\n");
1473 done:
1475 wbcFreeMemory(params.blobs);
1477 return WBC_ERROR_IS_OK(wbc_status);
1480 /* Authenticate a user with a plaintext password */
1482 static bool wbinfo_auth(char *username)
1484 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1485 char *s = NULL;
1486 char *p = NULL;
1487 char *password = NULL;
1488 char *name = NULL;
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 name = s;
1505 wbc_status = wbcAuthenticateUser(name, password);
1507 d_printf("plaintext password authentication %s\n",
1508 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1510 #if 0
1511 if (response.data.auth.nt_status)
1512 d_fprintf(stderr,
1513 "error code was %s (0x%x)\nerror messsage was: %s\n",
1514 response.data.auth.nt_status_string,
1515 response.data.auth.nt_status,
1516 response.data.auth.error_string);
1517 #endif
1519 return WBC_ERROR_IS_OK(wbc_status);
1522 /* Authenticate a user with a challenge/response */
1524 static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
1526 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1527 struct wbcAuthUserParams params;
1528 struct wbcAuthUserInfo *info = NULL;
1529 struct wbcAuthErrorInfo *err = NULL;
1530 DATA_BLOB lm = data_blob_null;
1531 DATA_BLOB nt = data_blob_null;
1532 fstring name_user;
1533 fstring name_domain;
1534 char *pass;
1535 char *p;
1536 TALLOC_CTX *frame = talloc_tos();
1538 p = strchr(username, '%');
1540 if (p) {
1541 *p = 0;
1542 pass = talloc_strdup(frame, p + 1);
1543 } else {
1544 pass = wbinfo_prompt_pass(frame, NULL, username);
1547 parse_wbinfo_domain_user(username, name_domain, name_user);
1549 params.account_name = name_user;
1550 params.domain_name = name_domain;
1551 params.workstation_name = NULL;
1553 params.flags = 0;
1554 params.parameter_control= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
1555 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
1557 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
1559 generate_random_buffer(params.password.response.challenge, 8);
1561 if (use_ntlmv2) {
1562 DATA_BLOB server_chal;
1563 DATA_BLOB names_blob;
1565 server_chal = data_blob(params.password.response.challenge, 8);
1567 /* Pretend this is a login to 'us', for blob purposes */
1568 names_blob = NTLMv2_generate_names_blob(NULL,
1569 get_winbind_netbios_name(),
1570 get_winbind_domain());
1572 if (!SMBNTLMv2encrypt(NULL, name_user, name_domain, pass,
1573 &server_chal,
1574 &names_blob,
1575 &lm, &nt, NULL, NULL)) {
1576 data_blob_free(&names_blob);
1577 data_blob_free(&server_chal);
1578 TALLOC_FREE(pass);
1579 return false;
1581 data_blob_free(&names_blob);
1582 data_blob_free(&server_chal);
1584 } else {
1585 if (use_lanman) {
1586 bool ok;
1587 lm = data_blob(NULL, 24);
1588 ok = SMBencrypt(pass,
1589 params.password.response.challenge,
1590 lm.data);
1591 if (!ok) {
1592 data_blob_free(&lm);
1595 nt = data_blob(NULL, 24);
1596 SMBNTencrypt(pass, params.password.response.challenge,
1597 nt.data);
1600 params.password.response.nt_length = nt.length;
1601 params.password.response.nt_data = nt.data;
1602 params.password.response.lm_length = lm.length;
1603 params.password.response.lm_data = lm.data;
1605 wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
1607 /* Display response */
1609 d_printf("challenge/response password authentication %s\n",
1610 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1612 if (wbc_status == WBC_ERR_AUTH_ERROR) {
1613 d_fprintf(stderr,
1614 "error code was %s (0x%x)\nerror messsage was: %s\n",
1615 err->nt_string,
1616 err->nt_status,
1617 err->display_string);
1618 wbcFreeMemory(err);
1619 } else if (WBC_ERROR_IS_OK(wbc_status)) {
1620 wbcFreeMemory(info);
1623 data_blob_free(&nt);
1624 data_blob_free(&lm);
1626 return WBC_ERROR_IS_OK(wbc_status);
1629 /* Authenticate a user with a plaintext password */
1631 static bool wbinfo_pam_logon(char *username)
1633 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1634 struct wbcLogonUserParams params;
1635 struct wbcAuthErrorInfo *error;
1636 char *s = NULL;
1637 char *p = NULL;
1638 TALLOC_CTX *frame = talloc_tos();
1639 uint32_t flags;
1640 uint32_t uid;
1642 ZERO_STRUCT(params);
1644 if ((s = talloc_strdup(frame, username)) == NULL) {
1645 return false;
1648 if ((p = strchr(s, '%')) != NULL) {
1649 *p = 0;
1650 p++;
1651 params.password = talloc_strdup(frame, p);
1652 } else {
1653 params.password = wbinfo_prompt_pass(frame, NULL, username);
1655 params.username = s;
1657 flags = WBFLAG_PAM_CACHED_LOGIN;
1659 wbc_status = wbcAddNamedBlob(&params.num_blobs, &params.blobs,
1660 "flags", 0,
1661 (uint8_t *)&flags, sizeof(flags));
1662 if (!WBC_ERROR_IS_OK(wbc_status)) {
1663 d_printf("wbcAddNamedBlob failed: %s\n",
1664 wbcErrorString(wbc_status));
1665 return false;
1668 uid = getuid();
1670 wbc_status = wbcAddNamedBlob(&params.num_blobs, &params.blobs,
1671 "user_uid", 0,
1672 (uint8_t *)&uid, sizeof(uid));
1673 if (!WBC_ERROR_IS_OK(wbc_status)) {
1674 d_printf("wbcAddNamedBlob failed: %s\n",
1675 wbcErrorString(wbc_status));
1676 return false;
1679 wbc_status = wbcLogonUser(&params, NULL, &error, NULL);
1681 wbcFreeMemory(params.blobs);
1683 d_printf("plaintext password authentication %s\n",
1684 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1686 if (!WBC_ERROR_IS_OK(wbc_status)) {
1687 d_fprintf(stderr,
1688 "error code was %s (0x%x)\nerror messsage was: %s\n",
1689 error->nt_string,
1690 (int)error->nt_status,
1691 error->display_string);
1692 wbcFreeMemory(error);
1693 return false;
1695 return true;
1698 /* Save creds with winbind */
1700 static bool wbinfo_ccache_save(char *username)
1702 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1703 char *s = NULL;
1704 char *p = NULL;
1705 char *password = NULL;
1706 char *name = NULL;
1707 TALLOC_CTX *frame = talloc_stackframe();
1709 s = talloc_strdup(frame, username);
1710 if (s == NULL) {
1711 return false;
1714 p = strchr(s, '%');
1715 if (p != NULL) {
1716 *p = 0;
1717 p++;
1718 password = talloc_strdup(frame, p);
1719 } else {
1720 password = wbinfo_prompt_pass(frame, NULL, username);
1723 name = s;
1725 wbc_status = wbcCredentialSave(name, password);
1727 d_printf("saving creds %s\n",
1728 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1730 TALLOC_FREE(frame);
1732 return WBC_ERROR_IS_OK(wbc_status);
1735 #ifdef WITH_FAKE_KASERVER
1736 /* Authenticate a user with a plaintext password and set a token */
1738 static bool wbinfo_klog(char *username)
1740 struct winbindd_request request;
1741 struct winbindd_response response;
1742 NSS_STATUS result;
1743 char *p;
1745 /* Send off request */
1747 ZERO_STRUCT(request);
1748 ZERO_STRUCT(response);
1750 p = strchr(username, '%');
1752 if (p) {
1753 *p = 0;
1754 fstrcpy(request.data.auth.user, username);
1755 fstrcpy(request.data.auth.pass, p + 1);
1756 *p = '%';
1757 } else {
1758 fstrcpy(request.data.auth.user, username);
1759 fstrcpy(request.data.auth.pass, getpass("Password: "));
1762 request.flags |= WBFLAG_PAM_AFS_TOKEN;
1764 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request,
1765 &response);
1767 /* Display response */
1769 d_printf("plaintext password authentication %s\n",
1770 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1772 if (response.data.auth.nt_status)
1773 d_fprintf(stderr,
1774 "error code was %s (0x%x)\nerror messsage was: %s\n",
1775 response.data.auth.nt_status_string,
1776 response.data.auth.nt_status,
1777 response.data.auth.error_string);
1779 if (result != NSS_STATUS_SUCCESS)
1780 return false;
1782 if (response.extra_data.data == NULL) {
1783 d_fprintf(stderr, "Did not get token data\n");
1784 return false;
1787 if (!afs_settoken_str((char *)response.extra_data.data)) {
1788 d_fprintf(stderr, "Could not set token\n");
1789 return false;
1792 d_printf("Successfully created AFS token\n");
1793 return true;
1795 #else
1796 static bool wbinfo_klog(char *username)
1798 d_fprintf(stderr, "No AFS support compiled in.\n");
1799 return false;
1801 #endif
1803 /* Print domain users */
1805 static bool print_domain_users(const char *domain)
1807 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1808 uint32_t i;
1809 uint32_t num_users = 0;
1810 const char **users = NULL;
1812 /* Send request to winbind daemon */
1814 /* '.' is the special sign for our own domain */
1815 if (domain && strcmp(domain, ".") == 0) {
1816 domain = get_winbind_domain();
1819 wbc_status = wbcListUsers(domain, &num_users, &users);
1820 if (!WBC_ERROR_IS_OK(wbc_status)) {
1821 return false;
1824 for (i=0; i < num_users; i++) {
1825 d_printf("%s\n", users[i]);
1828 wbcFreeMemory(users);
1830 return true;
1833 /* Print domain groups */
1835 static bool print_domain_groups(const char *domain)
1837 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1838 uint32_t i;
1839 uint32_t num_groups = 0;
1840 const char **groups = NULL;
1842 /* Send request to winbind daemon */
1844 /* '.' is the special sign for our own domain */
1845 if (domain && strcmp(domain, ".") == 0) {
1846 domain = get_winbind_domain();
1849 wbc_status = wbcListGroups(domain, &num_groups, &groups);
1850 if (!WBC_ERROR_IS_OK(wbc_status)) {
1851 d_fprintf(stderr, "failed to call wbcListGroups: %s",
1852 wbcErrorString(wbc_status));
1853 return false;
1856 for (i=0; i < num_groups; i++) {
1857 d_printf("%s\n", groups[i]);
1860 wbcFreeMemory(groups);
1862 return true;
1865 /* Set the authorised user for winbindd access in secrets.tdb */
1867 static bool wbinfo_set_auth_user(char *username)
1869 d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1870 "See 'net help setauthuser' for details.\n");
1871 return false;
1874 static void wbinfo_get_auth_user(void)
1876 d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1877 "See 'net help getauthuser' for details.\n");
1880 static bool wbinfo_ping(void)
1882 wbcErr wbc_status;
1884 wbc_status = wbcPing();
1886 /* Display response */
1888 d_printf("Ping to winbindd %s\n",
1889 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1891 return WBC_ERROR_IS_OK(wbc_status);
1894 static bool wbinfo_change_user_password(const char *username)
1896 wbcErr wbc_status;
1897 char *old_password = NULL;
1898 char *new_password = NULL;
1899 TALLOC_CTX *frame = talloc_tos();
1901 old_password = wbinfo_prompt_pass(frame, "old", username);
1902 new_password = wbinfo_prompt_pass(frame, "new", username);
1904 wbc_status = wbcChangeUserPassword(username, old_password,new_password);
1906 /* Display response */
1908 d_printf("Password change for user %s %s\n", username,
1909 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1911 return WBC_ERROR_IS_OK(wbc_status);
1914 /* Main program */
1916 enum {
1917 OPT_SET_AUTH_USER = 1000,
1918 OPT_GET_AUTH_USER,
1919 OPT_DOMAIN_NAME,
1920 OPT_SEQUENCE,
1921 OPT_GETDCNAME,
1922 OPT_DSGETDCNAME,
1923 OPT_USERDOMGROUPS,
1924 OPT_SIDALIASES,
1925 OPT_USERSIDS,
1926 OPT_ALLOCATE_UID,
1927 OPT_ALLOCATE_GID,
1928 OPT_SET_UID_MAPPING,
1929 OPT_SET_GID_MAPPING,
1930 OPT_REMOVE_UID_MAPPING,
1931 OPT_REMOVE_GID_MAPPING,
1932 OPT_SEPARATOR,
1933 OPT_LIST_ALL_DOMAINS,
1934 OPT_LIST_OWN_DOMAIN,
1935 OPT_UID_INFO,
1936 OPT_USER_SIDINFO,
1937 OPT_GROUP_INFO,
1938 OPT_GID_INFO,
1939 OPT_VERBOSE,
1940 OPT_ONLINESTATUS,
1941 OPT_CHANGE_USER_PASSWORD,
1942 OPT_PING_DC,
1943 OPT_CCACHE_SAVE,
1944 OPT_SID_TO_FULLNAME,
1945 OPT_NTLMV2,
1946 OPT_PAM_LOGON,
1947 OPT_LOGOFF,
1948 OPT_LOGOFF_USER,
1949 OPT_LOGOFF_UID,
1950 OPT_LANMAN
1953 int main(int argc, char **argv, char **envp)
1955 int opt;
1956 TALLOC_CTX *frame = talloc_stackframe();
1957 poptContext pc;
1958 static char *string_arg;
1959 char *string_subarg = NULL;
1960 static char *opt_domain_name;
1961 static int int_arg;
1962 int int_subarg = -1;
1963 int result = 1;
1964 bool verbose = false;
1965 bool use_ntlmv2 = false;
1966 bool use_lanman = false;
1967 char *logoff_user = getenv("USER");
1968 int logoff_uid = geteuid();
1970 struct poptOption long_options[] = {
1971 POPT_AUTOHELP
1973 /* longName, shortName, argInfo, argPtr, value, descrip,
1974 argDesc */
1976 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1977 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1978 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1979 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1980 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1981 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1982 { "sid-to-fullname", 0, POPT_ARG_STRING, &string_arg,
1983 OPT_SID_TO_FULLNAME, "Converts sid to fullname", "SID" },
1984 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1985 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1986 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1987 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1988 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1989 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1990 "Get a new UID out of idmap" },
1991 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1992 "Get a new GID out of idmap" },
1993 { "set-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_UID_MAPPING, "Create or modify uid to sid mapping in idmap", "UID,SID" },
1994 { "set-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_GID_MAPPING, "Create or modify gid to sid mapping in idmap", "GID,SID" },
1995 { "remove-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_UID_MAPPING, "Remove uid to sid mapping in idmap", "UID,SID" },
1996 { "remove-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_GID_MAPPING, "Remove gid to sid mapping in idmap", "GID,SID" },
1997 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1998 { "change-secret", 'c', POPT_ARG_NONE, 0, 'c', "Change shared secret" },
1999 { "ping-dc", 0, POPT_ARG_NONE, 0, OPT_PING_DC,
2000 "Check the NETLOGON connection" },
2001 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
2002 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
2003 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
2004 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
2005 { "online-status", 0, POPT_ARG_NONE, 0, OPT_ONLINESTATUS, "Show whether domains are marked as online or offline"},
2006 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
2007 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
2008 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
2009 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
2010 { "user-sidinfo", 0, POPT_ARG_STRING, &string_arg, OPT_USER_SIDINFO, "Get user info from sid", "SID" },
2011 { "gid-info", 0, POPT_ARG_INT, &int_arg, OPT_GID_INFO, "Get group info from gid", "GID" },
2012 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
2013 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
2014 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
2015 { "sid-aliases", 0, POPT_ARG_STRING, &string_arg, OPT_SIDALIASES, "Get sid aliases", "SID" },
2016 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
2017 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
2018 { "pam-logon", 0, POPT_ARG_STRING, string_arg, OPT_PAM_LOGON,
2019 "do a pam logon equivalent", "user%password" },
2020 { "logoff", 0, POPT_ARG_NONE, NULL, OPT_LOGOFF,
2021 "log off user", "uid" },
2022 { "logoff-user", 0, POPT_ARG_STRING, &logoff_user,
2023 OPT_LOGOFF_USER, "username to log off" },
2024 { "logoff-uid", 0, POPT_ARG_INT, &logoff_uid,
2025 OPT_LOGOFF_UID, "uid to log off" },
2026 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
2027 { "ccache-save", 0, POPT_ARG_STRING, &string_arg,
2028 OPT_CCACHE_SAVE, "Store user and password for ccache "
2029 "operation", "user%password" },
2030 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
2031 "Get a DC name for a foreign domain", "domainname" },
2032 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
2033 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
2034 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
2035 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
2036 #ifdef WITH_FAKE_KASERVER
2037 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
2038 #endif
2039 #ifdef HAVE_KRB5
2040 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
2041 /* destroys wbinfo --help output */
2042 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
2043 #endif
2044 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
2045 { "verbose", 0, POPT_ARG_NONE, 0, OPT_VERBOSE, "Print additional information per command", NULL },
2046 { "change-user-password", 0, POPT_ARG_STRING, &string_arg, OPT_CHANGE_USER_PASSWORD, "Change the password for a user", NULL },
2047 { "ntlmv2", 0, POPT_ARG_NONE, 0, OPT_NTLMV2, "Use NTLMv2 cryptography for user authentication", NULL},
2048 { "lanman", 0, POPT_ARG_NONE, 0, OPT_LANMAN, "Use lanman cryptography for user authentication", NULL},
2049 POPT_COMMON_VERSION
2050 POPT_TABLEEND
2053 /* Samba client initialisation */
2054 load_case_tables();
2057 /* Parse options */
2059 pc = poptGetContext("wbinfo", argc, (const char **)argv,
2060 long_options, 0);
2062 /* Parse command line options */
2064 if (argc == 1) {
2065 poptPrintHelp(pc, stderr, 0);
2066 return 1;
2069 while((opt = poptGetNextOpt(pc)) != -1) {
2070 /* get the generic configuration parameters like --domain */
2071 switch (opt) {
2072 case OPT_VERBOSE:
2073 verbose = true;
2074 break;
2075 case OPT_NTLMV2:
2076 use_ntlmv2 = true;
2077 break;
2078 case OPT_LANMAN:
2079 use_lanman = true;
2080 break;
2084 poptFreeContext(pc);
2086 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2087 POPT_CONTEXT_KEEP_FIRST);
2089 while((opt = poptGetNextOpt(pc)) != -1) {
2090 switch (opt) {
2091 case 'u':
2092 if (!print_domain_users(opt_domain_name)) {
2093 d_fprintf(stderr,
2094 "Error looking up domain users\n");
2095 goto done;
2097 break;
2098 case 'g':
2099 if (!print_domain_groups(opt_domain_name)) {
2100 d_fprintf(stderr,
2101 "Error looking up domain groups\n");
2102 goto done;
2104 break;
2105 case 's':
2106 if (!wbinfo_lookupsid(string_arg)) {
2107 d_fprintf(stderr,
2108 "Could not lookup sid %s\n",
2109 string_arg);
2110 goto done;
2112 break;
2113 case OPT_SID_TO_FULLNAME:
2114 if (!wbinfo_lookupsid_fullname(string_arg)) {
2115 d_fprintf(stderr, "Could not lookup sid %s\n",
2116 string_arg);
2117 goto done;
2119 break;
2120 case 'R':
2121 if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
2122 d_fprintf(stderr, "Could not lookup RIDs %s\n",
2123 string_arg);
2124 goto done;
2126 break;
2127 case 'n':
2128 if (!wbinfo_lookupname(string_arg)) {
2129 d_fprintf(stderr, "Could not lookup name %s\n",
2130 string_arg);
2131 goto done;
2133 break;
2134 case 'N':
2135 if (!wbinfo_wins_byname(string_arg)) {
2136 d_fprintf(stderr,
2137 "Could not lookup WINS by name %s\n",
2138 string_arg);
2139 goto done;
2141 break;
2142 case 'I':
2143 if (!wbinfo_wins_byip(string_arg)) {
2144 d_fprintf(stderr,
2145 "Could not lookup WINS by IP %s\n",
2146 string_arg);
2147 goto done;
2149 break;
2150 case 'U':
2151 if (!wbinfo_uid_to_sid(int_arg)) {
2152 d_fprintf(stderr,
2153 "Could not convert uid %d to sid\n",
2154 int_arg);
2155 goto done;
2157 break;
2158 case 'G':
2159 if (!wbinfo_gid_to_sid(int_arg)) {
2160 d_fprintf(stderr,
2161 "Could not convert gid %d to sid\n",
2162 int_arg);
2163 goto done;
2165 break;
2166 case 'S':
2167 if (!wbinfo_sid_to_uid(string_arg)) {
2168 d_fprintf(stderr,
2169 "Could not convert sid %s to uid\n",
2170 string_arg);
2171 goto done;
2173 break;
2174 case 'Y':
2175 if (!wbinfo_sid_to_gid(string_arg)) {
2176 d_fprintf(stderr,
2177 "Could not convert sid %s to gid\n",
2178 string_arg);
2179 goto done;
2181 break;
2182 case OPT_ALLOCATE_UID:
2183 if (!wbinfo_allocate_uid()) {
2184 d_fprintf(stderr, "Could not allocate a uid\n");
2185 goto done;
2187 break;
2188 case OPT_ALLOCATE_GID:
2189 if (!wbinfo_allocate_gid()) {
2190 d_fprintf(stderr, "Could not allocate a gid\n");
2191 goto done;
2193 break;
2194 case OPT_SET_UID_MAPPING:
2195 if (!parse_mapping_arg(string_arg, &int_subarg,
2196 &string_subarg) ||
2197 !wbinfo_set_uid_mapping(int_subarg, string_subarg))
2199 d_fprintf(stderr, "Could not create or modify "
2200 "uid to sid mapping\n");
2201 goto done;
2203 break;
2204 case OPT_SET_GID_MAPPING:
2205 if (!parse_mapping_arg(string_arg, &int_subarg,
2206 &string_subarg) ||
2207 !wbinfo_set_gid_mapping(int_subarg, string_subarg))
2209 d_fprintf(stderr, "Could not create or modify "
2210 "gid to sid mapping\n");
2211 goto done;
2213 break;
2214 case OPT_REMOVE_UID_MAPPING:
2215 if (!parse_mapping_arg(string_arg, &int_subarg,
2216 &string_subarg) ||
2217 !wbinfo_remove_uid_mapping(int_subarg,
2218 string_subarg))
2220 d_fprintf(stderr, "Could not remove uid to sid "
2221 "mapping\n");
2222 goto done;
2224 break;
2225 case OPT_REMOVE_GID_MAPPING:
2226 if (!parse_mapping_arg(string_arg, &int_subarg,
2227 &string_subarg) ||
2228 !wbinfo_remove_gid_mapping(int_subarg,
2229 string_subarg))
2231 d_fprintf(stderr, "Could not remove gid to sid "
2232 "mapping\n");
2233 goto done;
2235 break;
2236 case 't':
2237 if (!wbinfo_check_secret(opt_domain_name)) {
2238 d_fprintf(stderr, "Could not check secret\n");
2239 goto done;
2241 break;
2242 case 'c':
2243 if (!wbinfo_change_secret(opt_domain_name)) {
2244 d_fprintf(stderr, "Could not change secret\n");
2245 goto done;
2247 break;
2248 case OPT_PING_DC:
2249 if (!wbinfo_ping_dc()) {
2250 d_fprintf(stderr, "Could not ping our DC\n");
2251 goto done;
2253 break;
2254 case 'm':
2255 if (!wbinfo_list_domains(false, verbose)) {
2256 d_fprintf(stderr,
2257 "Could not list trusted domains\n");
2258 goto done;
2260 break;
2261 case OPT_SEQUENCE:
2262 if (!wbinfo_show_sequence(opt_domain_name)) {
2263 d_fprintf(stderr,
2264 "Could not show sequence numbers\n");
2265 goto done;
2267 break;
2268 case OPT_ONLINESTATUS:
2269 if (!wbinfo_show_onlinestatus(opt_domain_name)) {
2270 d_fprintf(stderr,
2271 "Could not show online-status\n");
2272 goto done;
2274 break;
2275 case 'D':
2276 if (!wbinfo_domain_info(string_arg)) {
2277 d_fprintf(stderr,
2278 "Could not get domain info\n");
2279 goto done;
2281 break;
2282 case 'i':
2283 if (!wbinfo_get_userinfo(string_arg)) {
2284 d_fprintf(stderr,
2285 "Could not get info for user %s\n",
2286 string_arg);
2287 goto done;
2289 break;
2290 case OPT_USER_SIDINFO:
2291 if ( !wbinfo_get_user_sidinfo(string_arg)) {
2292 d_fprintf(stderr,
2293 "Could not get info for user "
2294 "sid %s\n", string_arg);
2295 goto done;
2297 break;
2298 case OPT_UID_INFO:
2299 if ( !wbinfo_get_uidinfo(int_arg)) {
2300 d_fprintf(stderr, "Could not get info for uid "
2301 "%d\n", int_arg);
2302 goto done;
2304 break;
2305 case OPT_GROUP_INFO:
2306 if ( !wbinfo_get_groupinfo(string_arg)) {
2307 d_fprintf(stderr, "Could not get info for "
2308 "group %s\n", string_arg);
2309 goto done;
2311 break;
2312 case OPT_GID_INFO:
2313 if ( !wbinfo_get_gidinfo(int_arg)) {
2314 d_fprintf(stderr, "Could not get info for gid "
2315 "%d\n", int_arg);
2316 goto done;
2318 break;
2319 case 'r':
2320 if (!wbinfo_get_usergroups(string_arg)) {
2321 d_fprintf(stderr,
2322 "Could not get groups for user %s\n",
2323 string_arg);
2324 goto done;
2326 break;
2327 case OPT_USERSIDS:
2328 if (!wbinfo_get_usersids(string_arg)) {
2329 d_fprintf(stderr, "Could not get group SIDs "
2330 "for user SID %s\n",
2331 string_arg);
2332 goto done;
2334 break;
2335 case OPT_USERDOMGROUPS:
2336 if (!wbinfo_get_userdomgroups(string_arg)) {
2337 d_fprintf(stderr, "Could not get user's domain "
2338 "groups for user SID %s\n",
2339 string_arg);
2340 goto done;
2342 break;
2343 case OPT_SIDALIASES:
2344 if (!wbinfo_get_sidaliases(opt_domain_name,
2345 string_arg)) {
2346 d_fprintf(stderr, "Could not get sid aliases "
2347 "for user SID %s\n", string_arg);
2348 goto done;
2350 break;
2351 case 'a': {
2352 bool got_error = false;
2354 if (!wbinfo_auth(string_arg)) {
2355 d_fprintf(stderr,
2356 "Could not authenticate user "
2357 "%s with plaintext "
2358 "password\n", string_arg);
2359 got_error = true;
2362 if (!wbinfo_auth_crap(string_arg, use_ntlmv2,
2363 use_lanman)) {
2364 d_fprintf(stderr,
2365 "Could not authenticate user "
2366 "%s with challenge/response\n",
2367 string_arg);
2368 got_error = true;
2371 if (got_error)
2372 goto done;
2373 break;
2375 case OPT_PAM_LOGON:
2376 if (!wbinfo_pam_logon(string_arg)) {
2377 d_fprintf(stderr, "pam_logon failed for %s\n",
2378 string_arg);
2379 goto done;
2381 case OPT_LOGOFF:
2383 wbcErr wbc_status;
2385 wbc_status = wbcLogoffUser(logoff_user, logoff_uid,
2386 "");
2387 d_printf("Logoff %s (%d): %s\n", logoff_user,
2388 logoff_uid, wbcErrorString(wbc_status));
2389 break;
2391 case 'K': {
2392 uint32_t flags = WBFLAG_PAM_KRB5 |
2393 WBFLAG_PAM_CACHED_LOGIN |
2394 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
2395 WBFLAG_PAM_INFO3_TEXT |
2396 WBFLAG_PAM_CONTACT_TRUSTDOM;
2398 if (!wbinfo_auth_krb5(string_arg, "FILE",
2399 flags)) {
2400 d_fprintf(stderr,
2401 "Could not authenticate user "
2402 "[%s] with Kerberos "
2403 "(ccache: %s)\n", string_arg,
2404 "FILE");
2405 goto done;
2407 break;
2409 case 'k':
2410 if (!wbinfo_klog(string_arg)) {
2411 d_fprintf(stderr, "Could not klog user\n");
2412 goto done;
2414 break;
2415 case 'p':
2416 if (!wbinfo_ping()) {
2417 d_fprintf(stderr, "could not ping winbindd!\n");
2418 goto done;
2420 break;
2421 case OPT_SET_AUTH_USER:
2422 if (!wbinfo_set_auth_user(string_arg)) {
2423 goto done;
2425 break;
2426 case OPT_GET_AUTH_USER:
2427 wbinfo_get_auth_user();
2428 goto done;
2429 break;
2430 case OPT_CCACHE_SAVE:
2431 if (!wbinfo_ccache_save(string_arg)) {
2432 goto done;
2434 break;
2435 case OPT_GETDCNAME:
2436 if (!wbinfo_getdcname(string_arg)) {
2437 goto done;
2439 break;
2440 case OPT_DSGETDCNAME:
2441 if (!wbinfo_dsgetdcname(string_arg, 0)) {
2442 goto done;
2444 break;
2445 case OPT_SEPARATOR: {
2446 const char sep = winbind_separator();
2447 if ( !sep ) {
2448 goto done;
2450 d_printf("%c\n", sep);
2451 break;
2453 case OPT_LIST_ALL_DOMAINS:
2454 if (!wbinfo_list_domains(true, verbose)) {
2455 goto done;
2457 break;
2458 case OPT_LIST_OWN_DOMAIN:
2459 if (!wbinfo_list_own_domain()) {
2460 goto done;
2462 break;
2463 case OPT_CHANGE_USER_PASSWORD:
2464 if (!wbinfo_change_user_password(string_arg)) {
2465 d_fprintf(stderr,
2466 "Could not change user password "
2467 "for user %s\n", string_arg);
2468 goto done;
2470 break;
2472 /* generic configuration options */
2473 case OPT_DOMAIN_NAME:
2474 case OPT_VERBOSE:
2475 case OPT_NTLMV2:
2476 case OPT_LANMAN:
2477 case OPT_LOGOFF_USER:
2478 case OPT_LOGOFF_UID:
2479 break;
2480 default:
2481 d_fprintf(stderr, "Invalid option\n");
2482 poptPrintHelp(pc, stderr, 0);
2483 goto done;
2487 result = 0;
2489 /* Exit code */
2491 done:
2492 talloc_free(frame);
2494 poptFreeContext(pc);
2495 return result;