nsswitch: Fix a memleak in wbinfo
[Samba/gebeck_regimport.git] / nsswitch / wbinfo.c
blobafecc86f1e54c724b6d4727d7097f2eeee4e3020
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!\n");
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 wbcFreeMemory(details);
77 if (!sep) {
78 d_fprintf(stderr, "winbind separator was NULL!\n");
79 return 0;
82 return sep;
85 static const char *get_winbind_domain(void)
87 static struct wbcInterfaceDetails *details;
89 details = init_interface_details();
91 if (!details) {
92 d_fprintf(stderr, "could not obtain winbind domain name!\n");
93 return 0;
96 return details->netbios_domain;
99 static const char *get_winbind_netbios_name(void)
101 static struct wbcInterfaceDetails *details;
103 details = init_interface_details();
105 if (!details) {
106 d_fprintf(stderr, "could not obtain winbind netbios name!\n");
107 return 0;
110 return details->netbios_name;
113 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
114 form DOMAIN/user into a domain and a user */
116 static bool parse_wbinfo_domain_user(const char *domuser, fstring domain,
117 fstring user)
120 char *p = strchr(domuser,winbind_separator());
122 if (!p) {
123 /* Maybe it was a UPN? */
124 if ((p = strchr(domuser, '@')) != NULL) {
125 fstrcpy(domain, "");
126 fstrcpy(user, domuser);
127 return true;
130 fstrcpy(user, domuser);
131 fstrcpy(domain, get_winbind_domain());
132 return true;
135 fstrcpy(user, p+1);
136 fstrcpy(domain, domuser);
137 domain[PTR_DIFF(p, domuser)] = 0;
138 strupper_m(domain);
140 return true;
143 /* Parse string of "uid,sid" or "gid,sid" into separate int and string values.
144 * Return true if input was valid, false otherwise. */
145 static bool parse_mapping_arg(char *arg, int *id, char **sid)
147 char *tmp, *endptr;
149 if (!arg || !*arg)
150 return false;
152 tmp = strtok(arg, ",");
153 *sid = strtok(NULL, ",");
155 if (!tmp || !*tmp || !*sid || !**sid)
156 return false;
158 /* Because atoi() can return 0 on invalid input, which would be a valid
159 * UID/GID we must use strtoul() and do error checking */
160 *id = strtoul(tmp, &endptr, 10);
162 if (endptr[0] != '\0')
163 return false;
165 return true;
168 /* pull pwent info for a given user */
170 static bool wbinfo_get_userinfo(char *user)
172 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
173 struct passwd *pwd = NULL;
175 wbc_status = wbcGetpwnam(user, &pwd);
176 if (!WBC_ERROR_IS_OK(wbc_status)) {
177 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 return false;
203 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
204 pwd->pw_name,
205 pwd->pw_passwd,
206 (unsigned int)pwd->pw_uid,
207 (unsigned int)pwd->pw_gid,
208 pwd->pw_gecos,
209 pwd->pw_dir,
210 pwd->pw_shell);
212 return true;
215 static bool wbinfo_get_user_sidinfo(const char *sid_str)
217 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
218 struct passwd *pwd = NULL;
219 struct wbcDomainSid sid;
221 wbc_status = wbcStringToSid(sid_str, &sid);
222 wbc_status = wbcGetpwsid(&sid, &pwd);
223 if (!WBC_ERROR_IS_OK(wbc_status)) {
224 return false;
227 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
228 pwd->pw_name,
229 pwd->pw_passwd,
230 (unsigned int)pwd->pw_uid,
231 (unsigned int)pwd->pw_gid,
232 pwd->pw_gecos,
233 pwd->pw_dir,
234 pwd->pw_shell);
236 return true;
240 /* pull grent for a given group */
241 static bool wbinfo_get_groupinfo(const char *group)
243 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
244 struct group *grp;
245 char **mem;
247 wbc_status = wbcGetgrnam(group, &grp);
248 if (!WBC_ERROR_IS_OK(wbc_status)) {
249 return false;
252 d_printf("%s:%s:%u:",
253 grp->gr_name,
254 grp->gr_passwd,
255 (unsigned int)grp->gr_gid);
257 mem = grp->gr_mem;
258 while (*mem != NULL) {
259 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
260 mem += 1;
262 d_printf("\n");
264 wbcFreeMemory(grp);
266 return true;
269 /* pull grent for a given gid */
270 static bool wbinfo_get_gidinfo(int gid)
272 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
273 struct group *grp;
274 char **mem;
276 wbc_status = wbcGetgrgid(gid, &grp);
277 if (!WBC_ERROR_IS_OK(wbc_status)) {
278 return false;
281 d_printf("%s:%s:%u:",
282 grp->gr_name,
283 grp->gr_passwd,
284 (unsigned int)grp->gr_gid);
286 mem = grp->gr_mem;
287 while (*mem != NULL) {
288 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
289 mem += 1;
291 d_printf("\n");
293 wbcFreeMemory(grp);
295 return true;
298 /* List groups a user is a member of */
300 static bool wbinfo_get_usergroups(const char *user)
302 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
303 uint32_t num_groups;
304 uint32_t i;
305 gid_t *groups = NULL;
307 /* Send request */
309 wbc_status = wbcGetGroups(user, &num_groups, &groups);
310 if (!WBC_ERROR_IS_OK(wbc_status)) {
311 return false;
314 for (i = 0; i < num_groups; i++) {
315 d_printf("%d\n", (int)groups[i]);
318 wbcFreeMemory(groups);
320 return true;
324 /* List group SIDs a user SID is a member of */
325 static bool wbinfo_get_usersids(const char *user_sid_str)
327 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
328 uint32_t num_sids;
329 uint32_t i;
330 struct wbcDomainSid user_sid, *sids = NULL;
332 /* Send request */
334 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
335 if (!WBC_ERROR_IS_OK(wbc_status)) {
336 return false;
339 wbc_status = wbcLookupUserSids(&user_sid, false, &num_sids, &sids);
340 if (!WBC_ERROR_IS_OK(wbc_status)) {
341 return false;
344 for (i = 0; i < num_sids; i++) {
345 char *str = NULL;
346 wbc_status = wbcSidToString(&sids[i], &str);
347 if (!WBC_ERROR_IS_OK(wbc_status)) {
348 wbcFreeMemory(sids);
349 return false;
351 d_printf("%s\n", str);
352 wbcFreeMemory(str);
355 wbcFreeMemory(sids);
357 return true;
360 static bool wbinfo_get_userdomgroups(const char *user_sid_str)
362 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
363 uint32_t num_sids;
364 uint32_t i;
365 struct wbcDomainSid user_sid, *sids = NULL;
367 /* Send request */
369 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
370 if (!WBC_ERROR_IS_OK(wbc_status)) {
371 return false;
374 wbc_status = wbcLookupUserSids(&user_sid, true, &num_sids, &sids);
375 if (!WBC_ERROR_IS_OK(wbc_status)) {
376 return false;
379 for (i = 0; i < num_sids; i++) {
380 char *str = NULL;
381 wbc_status = wbcSidToString(&sids[i], &str);
382 if (!WBC_ERROR_IS_OK(wbc_status)) {
383 wbcFreeMemory(sids);
384 return false;
386 d_printf("%s\n", str);
387 wbcFreeMemory(str);
390 wbcFreeMemory(sids);
392 return true;
395 static bool wbinfo_get_sidaliases(const char *domain,
396 const char *user_sid_str)
398 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
399 struct wbcDomainInfo *dinfo = NULL;
400 uint32_t i;
401 struct wbcDomainSid user_sid;
402 uint32_t *alias_rids = NULL;
403 uint32_t num_alias_rids;
404 char *domain_sid_str = NULL;
406 /* Send request */
407 if ((domain == NULL) || (strequal(domain, ".")) ||
408 (domain[0] == '\0')) {
409 domain = get_winbind_domain();
412 /* Send request */
414 wbc_status = wbcDomainInfo(domain, &dinfo);
415 if (!WBC_ERROR_IS_OK(wbc_status)) {
416 d_printf("wbcDomainInfo(%s) failed: %s\n", domain,
417 wbcErrorString(wbc_status));
418 goto done;
420 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
421 if (!WBC_ERROR_IS_OK(wbc_status)) {
422 goto done;
425 wbc_status = wbcGetSidAliases(&dinfo->sid, &user_sid, 1,
426 &alias_rids, &num_alias_rids);
427 if (!WBC_ERROR_IS_OK(wbc_status)) {
428 goto done;
431 wbc_status = wbcSidToString(&dinfo->sid, &domain_sid_str);
432 if (!WBC_ERROR_IS_OK(wbc_status)) {
433 goto done;
436 for (i = 0; i < num_alias_rids; i++) {
437 d_printf("%s-%d\n", domain_sid_str, alias_rids[i]);
440 wbcFreeMemory(alias_rids);
442 done:
443 if (domain_sid_str) {
444 wbcFreeMemory(domain_sid_str);
446 if (dinfo) {
447 wbcFreeMemory(dinfo);
449 return (WBC_ERR_SUCCESS == wbc_status);
453 /* Convert NetBIOS name to IP */
455 static bool wbinfo_wins_byname(const char *name)
457 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
458 char *ip = NULL;
460 wbc_status = wbcResolveWinsByName(name, &ip);
461 if (!WBC_ERROR_IS_OK(wbc_status)) {
462 return false;
465 /* Display response */
467 d_printf("%s\n", ip);
469 wbcFreeMemory(ip);
471 return true;
474 /* Convert IP to NetBIOS name */
476 static bool wbinfo_wins_byip(const char *ip)
478 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
479 char *name = NULL;
481 wbc_status = wbcResolveWinsByIP(ip, &name);
482 if (!WBC_ERROR_IS_OK(wbc_status)) {
483 return false;
486 /* Display response */
488 d_printf("%s\n", name);
490 wbcFreeMemory(name);
492 return true;
495 /* List all/trusted domains */
497 static bool wbinfo_list_domains(bool list_all_domains, bool verbose)
499 struct wbcDomainInfo *domain_list = NULL;
500 size_t num_domains;
501 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
502 bool print_all = !list_all_domains && verbose;
503 int i;
505 wbc_status = wbcListTrusts(&domain_list, &num_domains);
506 if (!WBC_ERROR_IS_OK(wbc_status)) {
507 return false;
510 if (print_all) {
511 d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n",
512 "Domain Name", "DNS Domain", "Trust Type",
513 "Transitive", "In", "Out");
516 for (i=0; i<num_domains; i++) {
517 if (print_all) {
518 d_printf("%-16s", domain_list[i].short_name);
519 } else {
520 d_printf("%s", domain_list[i].short_name);
521 d_printf("\n");
522 continue;
525 d_printf("%-24s", domain_list[i].dns_name);
527 switch(domain_list[i].trust_type) {
528 case WBC_DOMINFO_TRUSTTYPE_NONE:
529 d_printf("None ");
530 break;
531 case WBC_DOMINFO_TRUSTTYPE_FOREST:
532 d_printf("Forest ");
533 break;
534 case WBC_DOMINFO_TRUSTTYPE_EXTERNAL:
535 d_printf("External ");
536 break;
537 case WBC_DOMINFO_TRUSTTYPE_IN_FOREST:
538 d_printf("In-Forest ");
539 break;
542 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_TRANSITIVE) {
543 d_printf("Yes ");
544 } else {
545 d_printf("No ");
548 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_INCOMING) {
549 d_printf("Yes ");
550 } else {
551 d_printf("No ");
554 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_OUTGOING) {
555 d_printf("Yes ");
556 } else {
557 d_printf("No ");
560 d_printf("\n");
563 return true;
566 /* List own domain */
568 static bool wbinfo_list_own_domain(void)
570 d_printf("%s\n", get_winbind_domain());
572 return true;
575 /* show sequence numbers */
576 static bool wbinfo_show_sequence(const char *domain)
578 d_printf("This command has been deprecated. Please use the "
579 "--online-status option instead.\n");
580 return false;
583 /* show sequence numbers */
584 static bool wbinfo_show_onlinestatus(const char *domain)
586 struct wbcDomainInfo *domain_list = NULL;
587 size_t num_domains;
588 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
589 int i;
591 wbc_status = wbcListTrusts(&domain_list, &num_domains);
592 if (!WBC_ERROR_IS_OK(wbc_status)) {
593 return false;
596 for (i=0; i<num_domains; i++) {
597 bool is_offline;
599 if (domain) {
600 if (!strequal(domain_list[i].short_name, domain)) {
601 continue;
605 is_offline = (domain_list[i].domain_flags &
606 WBC_DOMINFO_DOMAIN_OFFLINE);
608 d_printf("%s : %s\n",
609 domain_list[i].short_name,
610 is_offline ? "offline" : "online" );
613 return true;
617 /* Show domain info */
619 static bool wbinfo_domain_info(const char *domain)
621 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
622 struct wbcDomainInfo *dinfo = NULL;
623 char *sid_str = NULL;
625 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
626 domain = get_winbind_domain();
629 /* Send request */
631 wbc_status = wbcDomainInfo(domain, &dinfo);
632 if (!WBC_ERROR_IS_OK(wbc_status)) {
633 return false;
636 wbc_status = wbcSidToString(&dinfo->sid, &sid_str);
637 if (!WBC_ERROR_IS_OK(wbc_status)) {
638 wbcFreeMemory(dinfo);
639 return false;
642 /* Display response */
644 d_printf("Name : %s\n", dinfo->short_name);
645 d_printf("Alt_Name : %s\n", dinfo->dns_name);
647 d_printf("SID : %s\n", sid_str);
649 d_printf("Active Directory : %s\n",
650 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_AD) ? "Yes" : "No");
651 d_printf("Native : %s\n",
652 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_NATIVE) ?
653 "Yes" : "No");
655 d_printf("Primary : %s\n",
656 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_PRIMARY) ?
657 "Yes" : "No");
659 wbcFreeMemory(sid_str);
660 wbcFreeMemory(dinfo);
662 return true;
665 /* Get a foreign DC's name */
666 static bool wbinfo_getdcname(const char *domain_name)
668 struct winbindd_request request;
669 struct winbindd_response response;
671 ZERO_STRUCT(request);
672 ZERO_STRUCT(response);
674 fstrcpy(request.domain_name, domain_name);
676 /* Send request */
678 if (winbindd_request_response(WINBINDD_GETDCNAME, &request,
679 &response) != NSS_STATUS_SUCCESS) {
680 d_fprintf(stderr, "Could not get dc name for %s\n",domain_name);
681 return false;
684 /* Display response */
686 d_printf("%s\n", response.data.dc_name);
688 return true;
691 /* Find a DC */
692 static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
694 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
695 struct wbcDomainControllerInfoEx *dc_info;
696 char *str = NULL;
698 wbc_status = wbcLookupDomainControllerEx(domain_name, NULL, NULL,
699 flags | DS_DIRECTORY_SERVICE_REQUIRED,
700 &dc_info);
701 if (!WBC_ERROR_IS_OK(wbc_status)) {
702 printf("Could not find dc for %s\n", domain_name);
703 return false;
706 wbcGuidToString(dc_info->domain_guid, &str);
708 d_printf("%s\n", dc_info->dc_unc);
709 d_printf("%s\n", dc_info->dc_address);
710 d_printf("%d\n", dc_info->dc_address_type);
711 d_printf("%s\n", str);
712 d_printf("%s\n", dc_info->domain_name);
713 d_printf("%s\n", dc_info->forest_name);
714 d_printf("0x%08x\n", dc_info->dc_flags);
715 d_printf("%s\n", dc_info->dc_site_name);
716 d_printf("%s\n", dc_info->client_site_name);
718 return true;
721 /* Check trust account password */
723 static bool wbinfo_check_secret(const char *domain)
725 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
726 struct wbcAuthErrorInfo *error = NULL;
727 const char *domain_name;
729 if (domain) {
730 domain_name = domain;
731 } else {
732 domain_name = get_winbind_domain();
735 wbc_status = wbcCheckTrustCredentials(domain_name, &error);
737 d_printf("checking the trust secret for domain %s via RPC calls %s\n",
738 domain_name,
739 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
741 if (wbc_status == WBC_ERR_AUTH_ERROR) {
742 d_fprintf(stderr, "error code was %s (0x%x)\n",
743 error->nt_string, error->nt_status);
744 wbcFreeMemory(error);
746 if (!WBC_ERROR_IS_OK(wbc_status)) {
747 return false;
750 return true;
753 /* Change trust account password */
755 static bool wbinfo_change_secret(const char *domain)
757 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
758 struct wbcAuthErrorInfo *error = NULL;
759 const char *domain_name;
761 if (domain) {
762 domain_name = domain;
763 } else {
764 domain_name = get_winbind_domain();
767 wbc_status = wbcChangeTrustCredentials(domain_name, &error);
769 d_printf("changing the trust secret for domain %s via RPC calls %s\n",
770 domain_name,
771 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
773 if (wbc_status == WBC_ERR_AUTH_ERROR) {
774 d_fprintf(stderr, "error code was %s (0x%x)\n",
775 error->nt_string, error->nt_status);
776 wbcFreeMemory(error);
778 if (!WBC_ERROR_IS_OK(wbc_status)) {
779 return false;
782 return true;
785 /* Check DC connection */
787 static bool wbinfo_ping_dc(void)
789 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
790 struct wbcAuthErrorInfo *error = NULL;
792 wbc_status = wbcPingDc(NULL, &error);
794 d_printf("checking the NETLOGON dc connection %s\n",
795 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
797 if (wbc_status == WBC_ERR_AUTH_ERROR) {
798 d_fprintf(stderr, "error code was %s (0x%x)\n",
799 error->nt_string, error->nt_status);
800 wbcFreeMemory(error);
802 if (!WBC_ERROR_IS_OK(wbc_status)) {
803 return false;
806 return true;
809 /* Convert uid to sid */
811 static bool wbinfo_uid_to_sid(uid_t uid)
813 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
814 struct wbcDomainSid sid;
815 char *sid_str = NULL;
817 /* Send request */
819 wbc_status = wbcUidToSid(uid, &sid);
820 if (!WBC_ERROR_IS_OK(wbc_status)) {
821 return false;
824 wbc_status = wbcSidToString(&sid, &sid_str);
825 if (!WBC_ERROR_IS_OK(wbc_status)) {
826 return false;
829 /* Display response */
831 d_printf("%s\n", sid_str);
833 wbcFreeMemory(sid_str);
835 return true;
838 /* Convert gid to sid */
840 static bool wbinfo_gid_to_sid(gid_t gid)
842 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
843 struct wbcDomainSid sid;
844 char *sid_str = NULL;
846 /* Send request */
848 wbc_status = wbcGidToSid(gid, &sid);
849 if (!WBC_ERROR_IS_OK(wbc_status)) {
850 return false;
853 wbc_status = wbcSidToString(&sid, &sid_str);
854 if (!WBC_ERROR_IS_OK(wbc_status)) {
855 return false;
858 /* Display response */
860 d_printf("%s\n", sid_str);
862 wbcFreeMemory(sid_str);
864 return true;
867 /* Convert sid to uid */
869 static bool wbinfo_sid_to_uid(const char *sid_str)
871 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
872 struct wbcDomainSid sid;
873 uid_t uid;
875 /* Send request */
877 wbc_status = wbcStringToSid(sid_str, &sid);
878 if (!WBC_ERROR_IS_OK(wbc_status)) {
879 return false;
882 wbc_status = wbcSidToUid(&sid, &uid);
883 if (!WBC_ERROR_IS_OK(wbc_status)) {
884 return false;
887 /* Display response */
889 d_printf("%d\n", (int)uid);
891 return true;
894 static bool wbinfo_sid_to_gid(const char *sid_str)
896 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
897 struct wbcDomainSid sid;
898 gid_t gid;
900 /* Send request */
902 wbc_status = wbcStringToSid(sid_str, &sid);
903 if (!WBC_ERROR_IS_OK(wbc_status)) {
904 return false;
907 wbc_status = wbcSidToGid(&sid, &gid);
908 if (!WBC_ERROR_IS_OK(wbc_status)) {
909 return false;
912 /* Display response */
914 d_printf("%d\n", (int)gid);
916 return true;
919 static bool wbinfo_allocate_uid(void)
921 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
922 uid_t uid;
924 /* Send request */
926 wbc_status = wbcAllocateUid(&uid);
927 if (!WBC_ERROR_IS_OK(wbc_status)) {
928 return false;
931 /* Display response */
933 d_printf("New uid: %u\n", (unsigned int)uid);
935 return true;
938 static bool wbinfo_allocate_gid(void)
940 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
941 gid_t gid;
943 /* Send request */
945 wbc_status = wbcAllocateGid(&gid);
946 if (!WBC_ERROR_IS_OK(wbc_status)) {
947 return false;
950 /* Display response */
952 d_printf("New gid: %u\n", (unsigned int)gid);
954 return true;
957 static bool wbinfo_set_uid_mapping(uid_t uid, const char *sid_str)
959 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
960 struct wbcDomainSid sid;
962 /* Send request */
964 wbc_status = wbcStringToSid(sid_str, &sid);
965 if (!WBC_ERROR_IS_OK(wbc_status)) {
966 return false;
969 wbc_status = wbcSetUidMapping(uid, &sid);
970 if (!WBC_ERROR_IS_OK(wbc_status)) {
971 return false;
974 /* Display response */
976 d_printf("uid %u now mapped to sid %s\n",
977 (unsigned int)uid, sid_str);
979 return true;
982 static bool wbinfo_set_gid_mapping(gid_t gid, const char *sid_str)
984 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
985 struct wbcDomainSid sid;
987 /* Send request */
989 wbc_status = wbcStringToSid(sid_str, &sid);
990 if (!WBC_ERROR_IS_OK(wbc_status)) {
991 return false;
994 wbc_status = wbcSetGidMapping(gid, &sid);
995 if (!WBC_ERROR_IS_OK(wbc_status)) {
996 return false;
999 /* Display response */
1001 d_printf("gid %u now mapped to sid %s\n",
1002 (unsigned int)gid, sid_str);
1004 return true;
1007 static bool wbinfo_remove_uid_mapping(uid_t uid, const char *sid_str)
1009 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1010 struct wbcDomainSid sid;
1012 /* Send request */
1014 wbc_status = wbcStringToSid(sid_str, &sid);
1015 if (!WBC_ERROR_IS_OK(wbc_status)) {
1016 return false;
1019 wbc_status = wbcRemoveUidMapping(uid, &sid);
1020 if (!WBC_ERROR_IS_OK(wbc_status)) {
1021 return false;
1024 /* Display response */
1026 d_printf("Removed uid %u to sid %s mapping\n",
1027 (unsigned int)uid, sid_str);
1029 return true;
1032 static bool wbinfo_remove_gid_mapping(gid_t gid, const char *sid_str)
1034 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1035 struct wbcDomainSid sid;
1037 /* Send request */
1039 wbc_status = wbcStringToSid(sid_str, &sid);
1040 if (!WBC_ERROR_IS_OK(wbc_status)) {
1041 return false;
1044 wbc_status = wbcRemoveGidMapping(gid, &sid);
1045 if (!WBC_ERROR_IS_OK(wbc_status)) {
1046 return false;
1049 /* Display response */
1051 d_printf("Removed gid %u to sid %s mapping\n",
1052 (unsigned int)gid, sid_str);
1054 return true;
1057 /* Convert sid to string */
1059 static bool wbinfo_lookupsid(const char *sid_str)
1061 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1062 struct wbcDomainSid sid;
1063 char *domain;
1064 char *name;
1065 enum wbcSidType type;
1067 /* Send off request */
1069 wbc_status = wbcStringToSid(sid_str, &sid);
1070 if (!WBC_ERROR_IS_OK(wbc_status)) {
1071 return false;
1074 wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
1075 if (!WBC_ERROR_IS_OK(wbc_status)) {
1076 return false;
1079 /* Display response */
1081 d_printf("%s%c%s %d\n",
1082 domain, winbind_separator(), name, type);
1084 return true;
1087 /* Convert sid to fullname */
1089 static bool wbinfo_lookupsid_fullname(const char *sid_str)
1091 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1092 struct wbcDomainSid sid;
1093 char *domain;
1094 char *name;
1095 enum wbcSidType type;
1097 /* Send off request */
1099 wbc_status = wbcStringToSid(sid_str, &sid);
1100 if (!WBC_ERROR_IS_OK(wbc_status)) {
1101 return false;
1104 wbc_status = wbcGetDisplayName(&sid, &domain, &name, &type);
1105 if (!WBC_ERROR_IS_OK(wbc_status)) {
1106 return false;
1109 /* Display response */
1111 d_printf("%s%c%s %d\n",
1112 domain, winbind_separator(), name, type);
1114 return true;
1117 /* Lookup a list of RIDs */
1119 static bool wbinfo_lookuprids(const char *domain, const char *arg)
1121 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1122 struct wbcDomainInfo *dinfo = NULL;
1123 char *domain_name = NULL;
1124 const char **names = NULL;
1125 enum wbcSidType *types = NULL;
1126 size_t i;
1127 int num_rids;
1128 uint32_t *rids = NULL;
1129 const char *p;
1130 char *ridstr;
1131 TALLOC_CTX *mem_ctx = NULL;
1132 bool ret = false;
1134 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
1135 domain = get_winbind_domain();
1138 /* Send request */
1140 wbc_status = wbcDomainInfo(domain, &dinfo);
1141 if (!WBC_ERROR_IS_OK(wbc_status)) {
1142 d_printf("wbcDomainInfo(%s) failed: %s\n", domain,
1143 wbcErrorString(wbc_status));
1144 goto done;
1147 mem_ctx = talloc_new(NULL);
1148 if (mem_ctx == NULL) {
1149 d_printf("talloc_new failed\n");
1150 goto done;
1153 num_rids = 0;
1154 rids = NULL;
1155 p = arg;
1157 while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
1158 uint32_t rid = strtoul(ridstr, NULL, 10);
1159 rids = talloc_realloc(mem_ctx, rids, uint32_t, num_rids + 1);
1160 if (rids == NULL) {
1161 d_printf("talloc_realloc failed\n");
1163 rids[num_rids] = rid;
1164 num_rids += 1;
1167 if (rids == NULL) {
1168 d_printf("no rids\n");
1169 goto done;
1172 wbc_status = wbcLookupRids(&dinfo->sid, num_rids, rids,
1173 (const char **)&domain_name, &names, &types);
1174 if (!WBC_ERROR_IS_OK(wbc_status)) {
1175 d_printf("winbind_lookup_rids failed: %s\n",
1176 wbcErrorString(wbc_status));
1177 goto done;
1180 d_printf("Domain: %s\n", domain_name);
1182 for (i=0; i<num_rids; i++) {
1183 d_printf("%8d: %s (%s)\n", rids[i], names[i],
1184 wbcSidTypeString(types[i]));
1187 ret = true;
1188 done:
1189 if (dinfo) {
1190 wbcFreeMemory(dinfo);
1192 if (domain_name) {
1193 wbcFreeMemory(domain_name);
1195 if (names) {
1196 wbcFreeMemory(names);
1198 if (types) {
1199 wbcFreeMemory(types);
1201 TALLOC_FREE(mem_ctx);
1202 return ret;
1205 /* Convert string to sid */
1207 static bool wbinfo_lookupname(const char *full_name)
1209 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1210 struct wbcDomainSid sid;
1211 char *sid_str;
1212 enum wbcSidType type;
1213 fstring domain_name;
1214 fstring account_name;
1216 /* Send off request */
1218 parse_wbinfo_domain_user(full_name, domain_name,
1219 account_name);
1221 wbc_status = wbcLookupName(domain_name, account_name,
1222 &sid, &type);
1223 if (!WBC_ERROR_IS_OK(wbc_status)) {
1224 return false;
1227 wbc_status = wbcSidToString(&sid, &sid_str);
1228 if (!WBC_ERROR_IS_OK(wbc_status)) {
1229 return false;
1232 /* Display response */
1234 d_printf("%s %s (%d)\n", sid_str, wbcSidTypeString(type), type);
1236 wbcFreeMemory(sid_str);
1238 return true;
1241 static char *wbinfo_prompt_pass(TALLOC_CTX *mem_ctx,
1242 const char *prefix,
1243 const char *username)
1245 char *prompt;
1246 const char *ret = NULL;
1248 prompt = talloc_asprintf(mem_ctx, "Enter %s's ", username);
1249 if (!prompt) {
1250 return NULL;
1252 if (prefix) {
1253 prompt = talloc_asprintf_append(prompt, "%s ", prefix);
1254 if (!prompt) {
1255 return NULL;
1258 prompt = talloc_asprintf_append(prompt, "password: ");
1259 if (!prompt) {
1260 return NULL;
1263 ret = getpass(prompt);
1264 TALLOC_FREE(prompt);
1266 return talloc_strdup(mem_ctx, ret);
1269 /* Authenticate a user with a plaintext password */
1271 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32_t flags)
1273 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1274 char *s = NULL;
1275 char *p = NULL;
1276 char *password = NULL;
1277 char *name = NULL;
1278 char *local_cctype = NULL;
1279 uid_t uid;
1280 struct wbcLogonUserParams params;
1281 struct wbcLogonUserInfo *info;
1282 struct wbcAuthErrorInfo *error;
1283 struct wbcUserPasswordPolicyInfo *policy;
1284 TALLOC_CTX *frame = talloc_tos();
1286 if ((s = talloc_strdup(frame, username)) == NULL) {
1287 return false;
1290 if ((p = strchr(s, '%')) != NULL) {
1291 *p = 0;
1292 p++;
1293 password = talloc_strdup(frame, p);
1294 } else {
1295 password = wbinfo_prompt_pass(frame, NULL, username);
1298 local_cctype = talloc_strdup(frame, cctype);
1300 name = s;
1302 uid = geteuid();
1304 params.username = name;
1305 params.password = password;
1306 params.num_blobs = 0;
1307 params.blobs = NULL;
1309 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1310 &params.blobs,
1311 "flags",
1313 (uint8_t *)&flags,
1314 sizeof(flags));
1315 if (!WBC_ERROR_IS_OK(wbc_status)) {
1316 goto done;
1319 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1320 &params.blobs,
1321 "user_uid",
1323 (uint8_t *)&uid,
1324 sizeof(uid));
1325 if (!WBC_ERROR_IS_OK(wbc_status)) {
1326 goto done;
1329 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1330 &params.blobs,
1331 "krb5_cc_type",
1333 (uint8_t *)local_cctype,
1334 strlen(cctype)+1);
1335 if (!WBC_ERROR_IS_OK(wbc_status)) {
1336 goto done;
1339 wbc_status = wbcLogonUser(&params, &info, &error, &policy);
1341 d_printf("plaintext kerberos password authentication for [%s] %s "
1342 "(requesting cctype: %s)\n",
1343 username, WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed",
1344 cctype);
1346 if (error) {
1347 d_fprintf(stderr,
1348 "error code was %s (0x%x)\nerror messsage was: %s\n",
1349 error->nt_string,
1350 error->nt_status,
1351 error->display_string);
1354 if (WBC_ERROR_IS_OK(wbc_status)) {
1355 if (flags & WBFLAG_PAM_INFO3_TEXT) {
1356 if (info && info->info && info->info->user_flags &
1357 NETLOGON_CACHED_ACCOUNT) {
1358 d_printf("user_flgs: "
1359 "NETLOGON_CACHED_ACCOUNT\n");
1363 if (info) {
1364 int i;
1365 for (i=0; i < info->num_blobs; i++) {
1366 if (strequal(info->blobs[i].name,
1367 "krb5ccname")) {
1368 d_printf("credentials were put "
1369 "in: %s\n",
1370 (const char *)
1371 info->blobs[i].blob.data);
1372 break;
1375 } else {
1376 d_printf("no credentials cached\n");
1379 done:
1381 wbcFreeMemory(params.blobs);
1383 return WBC_ERROR_IS_OK(wbc_status);
1386 /* Authenticate a user with a plaintext password */
1388 static bool wbinfo_auth(char *username)
1390 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1391 char *s = NULL;
1392 char *p = NULL;
1393 char *password = NULL;
1394 char *name = NULL;
1395 TALLOC_CTX *frame = talloc_tos();
1397 if ((s = talloc_strdup(frame, username)) == NULL) {
1398 return false;
1401 if ((p = strchr(s, '%')) != NULL) {
1402 *p = 0;
1403 p++;
1404 password = talloc_strdup(frame, p);
1405 } else {
1406 password = wbinfo_prompt_pass(frame, NULL, username);
1409 name = s;
1411 wbc_status = wbcAuthenticateUser(name, password);
1413 d_printf("plaintext password authentication %s\n",
1414 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1416 #if 0
1417 if (response.data.auth.nt_status)
1418 d_fprintf(stderr,
1419 "error code was %s (0x%x)\nerror messsage was: %s\n",
1420 response.data.auth.nt_status_string,
1421 response.data.auth.nt_status,
1422 response.data.auth.error_string);
1423 #endif
1425 return WBC_ERROR_IS_OK(wbc_status);
1428 /* Authenticate a user with a challenge/response */
1430 static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
1432 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1433 struct wbcAuthUserParams params;
1434 struct wbcAuthUserInfo *info = NULL;
1435 struct wbcAuthErrorInfo *err = NULL;
1436 DATA_BLOB lm = data_blob_null;
1437 DATA_BLOB nt = data_blob_null;
1438 fstring name_user;
1439 fstring name_domain;
1440 char *pass;
1441 char *p;
1442 TALLOC_CTX *frame = talloc_tos();
1444 p = strchr(username, '%');
1446 if (p) {
1447 *p = 0;
1448 pass = talloc_strdup(frame, p + 1);
1449 } else {
1450 pass = wbinfo_prompt_pass(frame, NULL, username);
1453 parse_wbinfo_domain_user(username, name_domain, name_user);
1455 params.account_name = name_user;
1456 params.domain_name = name_domain;
1457 params.workstation_name = NULL;
1459 params.flags = 0;
1460 params.parameter_control= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
1461 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
1463 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
1465 generate_random_buffer(params.password.response.challenge, 8);
1467 if (use_ntlmv2) {
1468 DATA_BLOB server_chal;
1469 DATA_BLOB names_blob;
1471 server_chal = data_blob(params.password.response.challenge, 8);
1473 /* Pretend this is a login to 'us', for blob purposes */
1474 names_blob = NTLMv2_generate_names_blob(NULL,
1475 get_winbind_netbios_name(),
1476 get_winbind_domain());
1478 if (!SMBNTLMv2encrypt(NULL, name_user, name_domain, pass,
1479 &server_chal,
1480 &names_blob,
1481 &lm, &nt, NULL, NULL)) {
1482 data_blob_free(&names_blob);
1483 data_blob_free(&server_chal);
1484 TALLOC_FREE(pass);
1485 return false;
1487 data_blob_free(&names_blob);
1488 data_blob_free(&server_chal);
1490 } else {
1491 if (use_lanman) {
1492 bool ok;
1493 lm = data_blob(NULL, 24);
1494 ok = SMBencrypt(pass,
1495 params.password.response.challenge,
1496 lm.data);
1497 if (!ok) {
1498 data_blob_free(&lm);
1501 nt = data_blob(NULL, 24);
1502 SMBNTencrypt(pass, params.password.response.challenge,
1503 nt.data);
1506 params.password.response.nt_length = nt.length;
1507 params.password.response.nt_data = nt.data;
1508 params.password.response.lm_length = lm.length;
1509 params.password.response.lm_data = lm.data;
1511 wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
1513 /* Display response */
1515 d_printf("challenge/response password authentication %s\n",
1516 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1518 if (wbc_status == WBC_ERR_AUTH_ERROR) {
1519 d_fprintf(stderr,
1520 "error code was %s (0x%x)\nerror messsage was: %s\n",
1521 err->nt_string,
1522 err->nt_status,
1523 err->display_string);
1524 wbcFreeMemory(err);
1525 } else if (WBC_ERROR_IS_OK(wbc_status)) {
1526 wbcFreeMemory(info);
1529 data_blob_free(&nt);
1530 data_blob_free(&lm);
1532 return WBC_ERROR_IS_OK(wbc_status);
1535 /* Save creds with winbind */
1537 static bool wbinfo_ccache_save(char *username)
1539 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1540 char *s = NULL;
1541 char *p = NULL;
1542 char *password = NULL;
1543 char *name = NULL;
1544 TALLOC_CTX *frame = talloc_stackframe();
1546 s = talloc_strdup(frame, username);
1547 if (s == NULL) {
1548 return false;
1551 p = strchr(s, '%');
1552 if (p != NULL) {
1553 *p = 0;
1554 p++;
1555 password = talloc_strdup(frame, p);
1556 } else {
1557 password = wbinfo_prompt_pass(frame, NULL, username);
1560 name = s;
1562 wbc_status = wbcCredentialSave(name, password);
1564 d_printf("saving creds %s\n",
1565 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1567 TALLOC_FREE(frame);
1569 return WBC_ERROR_IS_OK(wbc_status);
1572 #ifdef WITH_FAKE_KASERVER
1573 /* Authenticate a user with a plaintext password and set a token */
1575 static bool wbinfo_klog(char *username)
1577 struct winbindd_request request;
1578 struct winbindd_response response;
1579 NSS_STATUS result;
1580 char *p;
1582 /* Send off request */
1584 ZERO_STRUCT(request);
1585 ZERO_STRUCT(response);
1587 p = strchr(username, '%');
1589 if (p) {
1590 *p = 0;
1591 fstrcpy(request.data.auth.user, username);
1592 fstrcpy(request.data.auth.pass, p + 1);
1593 *p = '%';
1594 } else {
1595 fstrcpy(request.data.auth.user, username);
1596 fstrcpy(request.data.auth.pass, getpass("Password: "));
1599 request.flags |= WBFLAG_PAM_AFS_TOKEN;
1601 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request,
1602 &response);
1604 /* Display response */
1606 d_printf("plaintext password authentication %s\n",
1607 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1609 if (response.data.auth.nt_status)
1610 d_fprintf(stderr,
1611 "error code was %s (0x%x)\nerror messsage was: %s\n",
1612 response.data.auth.nt_status_string,
1613 response.data.auth.nt_status,
1614 response.data.auth.error_string);
1616 if (result != NSS_STATUS_SUCCESS)
1617 return false;
1619 if (response.extra_data.data == NULL) {
1620 d_fprintf(stderr, "Did not get token data\n");
1621 return false;
1624 if (!afs_settoken_str((char *)response.extra_data.data)) {
1625 d_fprintf(stderr, "Could not set token\n");
1626 return false;
1629 d_printf("Successfully created AFS token\n");
1630 return true;
1632 #else
1633 static bool wbinfo_klog(char *username)
1635 d_fprintf(stderr, "No AFS support compiled in.\n");
1636 return false;
1638 #endif
1640 /* Print domain users */
1642 static bool print_domain_users(const char *domain)
1644 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1645 uint32_t i;
1646 uint32_t num_users = 0;
1647 const char **users = NULL;
1649 /* Send request to winbind daemon */
1651 /* '.' is the special sign for our own domain */
1652 if (domain && strcmp(domain, ".") == 0) {
1653 domain = get_winbind_domain();
1656 wbc_status = wbcListUsers(domain, &num_users, &users);
1657 if (!WBC_ERROR_IS_OK(wbc_status)) {
1658 return false;
1661 for (i=0; i < num_users; i++) {
1662 d_printf("%s\n", users[i]);
1665 wbcFreeMemory(users);
1667 return true;
1670 /* Print domain groups */
1672 static bool print_domain_groups(const char *domain)
1674 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1675 uint32_t i;
1676 uint32_t num_groups = 0;
1677 const char **groups = NULL;
1679 /* Send request to winbind daemon */
1681 /* '.' is the special sign for our own domain */
1682 if (domain && strcmp(domain, ".") == 0) {
1683 domain = get_winbind_domain();
1686 wbc_status = wbcListGroups(domain, &num_groups, &groups);
1687 if (!WBC_ERROR_IS_OK(wbc_status)) {
1688 return false;
1691 for (i=0; i < num_groups; i++) {
1692 d_printf("%s\n", groups[i]);
1695 wbcFreeMemory(groups);
1697 return true;
1700 /* Set the authorised user for winbindd access in secrets.tdb */
1702 static bool wbinfo_set_auth_user(char *username)
1704 d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1705 "See 'net help setauthuser' for details.\n");
1706 return false;
1709 static void wbinfo_get_auth_user(void)
1711 d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1712 "See 'net help getauthuser' for details.\n");
1715 static bool wbinfo_ping(void)
1717 wbcErr wbc_status;
1719 wbc_status = wbcPing();
1721 /* Display response */
1723 d_printf("Ping to winbindd %s\n",
1724 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1726 return WBC_ERROR_IS_OK(wbc_status);
1729 static bool wbinfo_change_user_password(const char *username)
1731 wbcErr wbc_status;
1732 char *old_password = NULL;
1733 char *new_password = NULL;
1734 TALLOC_CTX *frame = talloc_tos();
1736 old_password = wbinfo_prompt_pass(frame, "old", username);
1737 new_password = wbinfo_prompt_pass(frame, "new", username);
1739 wbc_status = wbcChangeUserPassword(username, old_password,new_password);
1741 /* Display response */
1743 d_printf("Password change for user %s %s\n", username,
1744 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1746 return WBC_ERROR_IS_OK(wbc_status);
1749 /* Main program */
1751 enum {
1752 OPT_SET_AUTH_USER = 1000,
1753 OPT_GET_AUTH_USER,
1754 OPT_DOMAIN_NAME,
1755 OPT_SEQUENCE,
1756 OPT_GETDCNAME,
1757 OPT_DSGETDCNAME,
1758 OPT_USERDOMGROUPS,
1759 OPT_SIDALIASES,
1760 OPT_USERSIDS,
1761 OPT_ALLOCATE_UID,
1762 OPT_ALLOCATE_GID,
1763 OPT_SET_UID_MAPPING,
1764 OPT_SET_GID_MAPPING,
1765 OPT_REMOVE_UID_MAPPING,
1766 OPT_REMOVE_GID_MAPPING,
1767 OPT_SEPARATOR,
1768 OPT_LIST_ALL_DOMAINS,
1769 OPT_LIST_OWN_DOMAIN,
1770 OPT_UID_INFO,
1771 OPT_USER_SIDINFO,
1772 OPT_GROUP_INFO,
1773 OPT_GID_INFO,
1774 OPT_VERBOSE,
1775 OPT_ONLINESTATUS,
1776 OPT_CHANGE_USER_PASSWORD,
1777 OPT_PING_DC,
1778 OPT_CCACHE_SAVE,
1779 OPT_SID_TO_FULLNAME,
1780 OPT_NTLMV2,
1781 OPT_LOGOFF,
1782 OPT_LOGOFF_USER,
1783 OPT_LOGOFF_UID,
1784 OPT_LANMAN
1787 int main(int argc, char **argv, char **envp)
1789 int opt;
1790 TALLOC_CTX *frame = talloc_stackframe();
1791 poptContext pc;
1792 static char *string_arg;
1793 char *string_subarg = NULL;
1794 static char *opt_domain_name;
1795 static int int_arg;
1796 int int_subarg = -1;
1797 int result = 1;
1798 bool verbose = false;
1799 bool use_ntlmv2 = false;
1800 bool use_lanman = false;
1801 char *logoff_user = getenv("USER");
1802 int logoff_uid = geteuid();
1804 struct poptOption long_options[] = {
1805 POPT_AUTOHELP
1807 /* longName, shortName, argInfo, argPtr, value, descrip,
1808 argDesc */
1810 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1811 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1812 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1813 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1814 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1815 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1816 { "sid-to-fullname", 0, POPT_ARG_STRING, &string_arg,
1817 OPT_SID_TO_FULLNAME, "Converts sid to fullname", "SID" },
1818 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1819 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1820 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1821 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1822 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1823 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1824 "Get a new UID out of idmap" },
1825 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1826 "Get a new GID out of idmap" },
1827 { "set-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_UID_MAPPING, "Create or modify uid to sid mapping in idmap", "UID,SID" },
1828 { "set-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_GID_MAPPING, "Create or modify gid to sid mapping in idmap", "GID,SID" },
1829 { "remove-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_UID_MAPPING, "Remove uid to sid mapping in idmap", "UID,SID" },
1830 { "remove-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_GID_MAPPING, "Remove gid to sid mapping in idmap", "GID,SID" },
1831 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1832 { "change-secret", 'c', POPT_ARG_NONE, 0, 'c', "Change shared secret" },
1833 { "ping-dc", 0, POPT_ARG_NONE, 0, OPT_PING_DC,
1834 "Check the NETLOGON connection" },
1835 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1836 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1837 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1838 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1839 { "online-status", 0, POPT_ARG_NONE, 0, OPT_ONLINESTATUS, "Show whether domains are marked as online or offline"},
1840 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1841 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1842 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1843 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1844 { "user-sidinfo", 0, POPT_ARG_STRING, &string_arg, OPT_USER_SIDINFO, "Get user info from sid", "SID" },
1845 { "gid-info", 0, POPT_ARG_INT, &int_arg, OPT_GID_INFO, "Get group info from gid", "GID" },
1846 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1847 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1848 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1849 { "sid-aliases", 0, POPT_ARG_STRING, &string_arg, OPT_SIDALIASES, "Get sid aliases", "SID" },
1850 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1851 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1852 { "logoff", 0, POPT_ARG_NONE, NULL, OPT_LOGOFF,
1853 "log off user", "uid" },
1854 { "logoff-user", 0, POPT_ARG_STRING, &logoff_user,
1855 OPT_LOGOFF_USER, "username to log off" },
1856 { "logoff-uid", 0, POPT_ARG_INT, &logoff_uid,
1857 OPT_LOGOFF_UID, "uid to log off" },
1858 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1859 { "ccache-save", 0, POPT_ARG_STRING, &string_arg,
1860 OPT_CCACHE_SAVE, "Store user and password for ccache "
1861 "operation", "user%password" },
1862 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1863 "Get a DC name for a foreign domain", "domainname" },
1864 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
1865 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1866 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1867 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1868 #ifdef WITH_FAKE_KASERVER
1869 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1870 #endif
1871 #ifdef HAVE_KRB5
1872 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1873 /* destroys wbinfo --help output */
1874 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1875 #endif
1876 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1877 { "verbose", 0, POPT_ARG_NONE, 0, OPT_VERBOSE, "Print additional information per command", NULL },
1878 { "change-user-password", 0, POPT_ARG_STRING, &string_arg, OPT_CHANGE_USER_PASSWORD, "Change the password for a user", NULL },
1879 { "ntlmv2", 0, POPT_ARG_NONE, 0, OPT_NTLMV2, "Use NTLMv2 cryptography for user authentication", NULL},
1880 { "lanman", 0, POPT_ARG_NONE, 0, OPT_LANMAN, "Use lanman cryptography for user authentication", NULL},
1881 POPT_COMMON_VERSION
1882 POPT_TABLEEND
1885 /* Samba client initialisation */
1886 load_case_tables();
1889 /* Parse options */
1891 pc = poptGetContext("wbinfo", argc, (const char **)argv,
1892 long_options, 0);
1894 /* Parse command line options */
1896 if (argc == 1) {
1897 poptPrintHelp(pc, stderr, 0);
1898 return 1;
1901 while((opt = poptGetNextOpt(pc)) != -1) {
1902 /* get the generic configuration parameters like --domain */
1903 switch (opt) {
1904 case OPT_VERBOSE:
1905 verbose = true;
1906 break;
1907 case OPT_NTLMV2:
1908 use_ntlmv2 = true;
1909 break;
1910 case OPT_LANMAN:
1911 use_lanman = true;
1912 break;
1916 poptFreeContext(pc);
1918 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1919 POPT_CONTEXT_KEEP_FIRST);
1921 while((opt = poptGetNextOpt(pc)) != -1) {
1922 switch (opt) {
1923 case 'u':
1924 if (!print_domain_users(opt_domain_name)) {
1925 d_fprintf(stderr,
1926 "Error looking up domain users\n");
1927 goto done;
1929 break;
1930 case 'g':
1931 if (!print_domain_groups(opt_domain_name)) {
1932 d_fprintf(stderr,
1933 "Error looking up domain groups\n");
1934 goto done;
1936 break;
1937 case 's':
1938 if (!wbinfo_lookupsid(string_arg)) {
1939 d_fprintf(stderr,
1940 "Could not lookup sid %s\n",
1941 string_arg);
1942 goto done;
1944 break;
1945 case OPT_SID_TO_FULLNAME:
1946 if (!wbinfo_lookupsid_fullname(string_arg)) {
1947 d_fprintf(stderr, "Could not lookup sid %s\n",
1948 string_arg);
1949 goto done;
1951 break;
1952 case 'R':
1953 if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
1954 d_fprintf(stderr, "Could not lookup RIDs %s\n",
1955 string_arg);
1956 goto done;
1958 break;
1959 case 'n':
1960 if (!wbinfo_lookupname(string_arg)) {
1961 d_fprintf(stderr, "Could not lookup name %s\n",
1962 string_arg);
1963 goto done;
1965 break;
1966 case 'N':
1967 if (!wbinfo_wins_byname(string_arg)) {
1968 d_fprintf(stderr,
1969 "Could not lookup WINS by name %s\n",
1970 string_arg);
1971 goto done;
1973 break;
1974 case 'I':
1975 if (!wbinfo_wins_byip(string_arg)) {
1976 d_fprintf(stderr,
1977 "Could not lookup WINS by IP %s\n",
1978 string_arg);
1979 goto done;
1981 break;
1982 case 'U':
1983 if (!wbinfo_uid_to_sid(int_arg)) {
1984 d_fprintf(stderr,
1985 "Could not convert uid %d to sid\n",
1986 int_arg);
1987 goto done;
1989 break;
1990 case 'G':
1991 if (!wbinfo_gid_to_sid(int_arg)) {
1992 d_fprintf(stderr,
1993 "Could not convert gid %d to sid\n",
1994 int_arg);
1995 goto done;
1997 break;
1998 case 'S':
1999 if (!wbinfo_sid_to_uid(string_arg)) {
2000 d_fprintf(stderr,
2001 "Could not convert sid %s to uid\n",
2002 string_arg);
2003 goto done;
2005 break;
2006 case 'Y':
2007 if (!wbinfo_sid_to_gid(string_arg)) {
2008 d_fprintf(stderr,
2009 "Could not convert sid %s to gid\n",
2010 string_arg);
2011 goto done;
2013 break;
2014 case OPT_ALLOCATE_UID:
2015 if (!wbinfo_allocate_uid()) {
2016 d_fprintf(stderr, "Could not allocate a uid\n");
2017 goto done;
2019 break;
2020 case OPT_ALLOCATE_GID:
2021 if (!wbinfo_allocate_gid()) {
2022 d_fprintf(stderr, "Could not allocate a gid\n");
2023 goto done;
2025 break;
2026 case OPT_SET_UID_MAPPING:
2027 if (!parse_mapping_arg(string_arg, &int_subarg,
2028 &string_subarg) ||
2029 !wbinfo_set_uid_mapping(int_subarg, string_subarg))
2031 d_fprintf(stderr, "Could not create or modify "
2032 "uid to sid mapping\n");
2033 goto done;
2035 break;
2036 case OPT_SET_GID_MAPPING:
2037 if (!parse_mapping_arg(string_arg, &int_subarg,
2038 &string_subarg) ||
2039 !wbinfo_set_gid_mapping(int_subarg, string_subarg))
2041 d_fprintf(stderr, "Could not create or modify "
2042 "gid to sid mapping\n");
2043 goto done;
2045 break;
2046 case OPT_REMOVE_UID_MAPPING:
2047 if (!parse_mapping_arg(string_arg, &int_subarg,
2048 &string_subarg) ||
2049 !wbinfo_remove_uid_mapping(int_subarg,
2050 string_subarg))
2052 d_fprintf(stderr, "Could not remove uid to sid "
2053 "mapping\n");
2054 goto done;
2056 break;
2057 case OPT_REMOVE_GID_MAPPING:
2058 if (!parse_mapping_arg(string_arg, &int_subarg,
2059 &string_subarg) ||
2060 !wbinfo_remove_gid_mapping(int_subarg,
2061 string_subarg))
2063 d_fprintf(stderr, "Could not remove gid to sid "
2064 "mapping\n");
2065 goto done;
2067 break;
2068 case 't':
2069 if (!wbinfo_check_secret(opt_domain_name)) {
2070 d_fprintf(stderr, "Could not check secret\n");
2071 goto done;
2073 break;
2074 case 'c':
2075 if (!wbinfo_change_secret(opt_domain_name)) {
2076 d_fprintf(stderr, "Could not change secret\n");
2077 goto done;
2079 break;
2080 case OPT_PING_DC:
2081 if (!wbinfo_ping_dc()) {
2082 d_fprintf(stderr, "Could not ping our DC\n");
2083 goto done;
2085 break;
2086 case 'm':
2087 if (!wbinfo_list_domains(false, verbose)) {
2088 d_fprintf(stderr,
2089 "Could not list trusted domains\n");
2090 goto done;
2092 break;
2093 case OPT_SEQUENCE:
2094 if (!wbinfo_show_sequence(opt_domain_name)) {
2095 d_fprintf(stderr,
2096 "Could not show sequence numbers\n");
2097 goto done;
2099 break;
2100 case OPT_ONLINESTATUS:
2101 if (!wbinfo_show_onlinestatus(opt_domain_name)) {
2102 d_fprintf(stderr,
2103 "Could not show online-status\n");
2104 goto done;
2106 break;
2107 case 'D':
2108 if (!wbinfo_domain_info(string_arg)) {
2109 d_fprintf(stderr,
2110 "Could not get domain info\n");
2111 goto done;
2113 break;
2114 case 'i':
2115 if (!wbinfo_get_userinfo(string_arg)) {
2116 d_fprintf(stderr,
2117 "Could not get info for user %s\n",
2118 string_arg);
2119 goto done;
2121 break;
2122 case OPT_USER_SIDINFO:
2123 if ( !wbinfo_get_user_sidinfo(string_arg)) {
2124 d_fprintf(stderr,
2125 "Could not get info for user "
2126 "sid %s\n", string_arg);
2127 goto done;
2129 break;
2130 case OPT_UID_INFO:
2131 if ( !wbinfo_get_uidinfo(int_arg)) {
2132 d_fprintf(stderr, "Could not get info for uid "
2133 "%d\n", int_arg);
2134 goto done;
2136 break;
2137 case OPT_GROUP_INFO:
2138 if ( !wbinfo_get_groupinfo(string_arg)) {
2139 d_fprintf(stderr, "Could not get info for "
2140 "group %s\n", string_arg);
2141 goto done;
2143 break;
2144 case OPT_GID_INFO:
2145 if ( !wbinfo_get_gidinfo(int_arg)) {
2146 d_fprintf(stderr, "Could not get info for gid "
2147 "%d\n", int_arg);
2148 goto done;
2150 break;
2151 case 'r':
2152 if (!wbinfo_get_usergroups(string_arg)) {
2153 d_fprintf(stderr,
2154 "Could not get groups for user %s\n",
2155 string_arg);
2156 goto done;
2158 break;
2159 case OPT_USERSIDS:
2160 if (!wbinfo_get_usersids(string_arg)) {
2161 d_fprintf(stderr, "Could not get group SIDs "
2162 "for user SID %s\n",
2163 string_arg);
2164 goto done;
2166 break;
2167 case OPT_USERDOMGROUPS:
2168 if (!wbinfo_get_userdomgroups(string_arg)) {
2169 d_fprintf(stderr, "Could not get user's domain "
2170 "groups for user SID %s\n",
2171 string_arg);
2172 goto done;
2174 break;
2175 case OPT_SIDALIASES:
2176 if (!wbinfo_get_sidaliases(opt_domain_name,
2177 string_arg)) {
2178 d_fprintf(stderr, "Could not get sid aliases "
2179 "for user SID %s\n", string_arg);
2180 goto done;
2182 break;
2183 case 'a': {
2184 bool got_error = false;
2186 if (!wbinfo_auth(string_arg)) {
2187 d_fprintf(stderr,
2188 "Could not authenticate user "
2189 "%s with plaintext "
2190 "password\n", string_arg);
2191 got_error = true;
2194 if (!wbinfo_auth_crap(string_arg, use_ntlmv2,
2195 use_lanman)) {
2196 d_fprintf(stderr,
2197 "Could not authenticate user "
2198 "%s with challenge/response\n",
2199 string_arg);
2200 got_error = true;
2203 if (got_error)
2204 goto done;
2205 break;
2207 case OPT_LOGOFF:
2209 wbcErr wbc_status;
2211 wbc_status = wbcLogoffUser(logoff_user, logoff_uid,
2212 "");
2213 d_printf("Logoff %s (%d): %s\n", logoff_user,
2214 logoff_uid, wbcErrorString(wbc_status));
2215 break;
2217 case 'K': {
2218 uint32_t flags = WBFLAG_PAM_KRB5 |
2219 WBFLAG_PAM_CACHED_LOGIN |
2220 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
2221 WBFLAG_PAM_INFO3_TEXT |
2222 WBFLAG_PAM_CONTACT_TRUSTDOM;
2224 if (!wbinfo_auth_krb5(string_arg, "FILE",
2225 flags)) {
2226 d_fprintf(stderr,
2227 "Could not authenticate user "
2228 "[%s] with Kerberos "
2229 "(ccache: %s)\n", string_arg,
2230 "FILE");
2231 goto done;
2233 break;
2235 case 'k':
2236 if (!wbinfo_klog(string_arg)) {
2237 d_fprintf(stderr, "Could not klog user\n");
2238 goto done;
2240 break;
2241 case 'p':
2242 if (!wbinfo_ping()) {
2243 d_fprintf(stderr, "could not ping winbindd!\n");
2244 goto done;
2246 break;
2247 case OPT_SET_AUTH_USER:
2248 if (!wbinfo_set_auth_user(string_arg)) {
2249 goto done;
2251 break;
2252 case OPT_GET_AUTH_USER:
2253 wbinfo_get_auth_user();
2254 goto done;
2255 break;
2256 case OPT_CCACHE_SAVE:
2257 if (!wbinfo_ccache_save(string_arg)) {
2258 goto done;
2260 break;
2261 case OPT_GETDCNAME:
2262 if (!wbinfo_getdcname(string_arg)) {
2263 goto done;
2265 break;
2266 case OPT_DSGETDCNAME:
2267 if (!wbinfo_dsgetdcname(string_arg, 0)) {
2268 goto done;
2270 break;
2271 case OPT_SEPARATOR: {
2272 const char sep = winbind_separator();
2273 if ( !sep ) {
2274 goto done;
2276 d_printf("%c\n", sep);
2277 break;
2279 case OPT_LIST_ALL_DOMAINS:
2280 if (!wbinfo_list_domains(true, verbose)) {
2281 goto done;
2283 break;
2284 case OPT_LIST_OWN_DOMAIN:
2285 if (!wbinfo_list_own_domain()) {
2286 goto done;
2288 break;
2289 case OPT_CHANGE_USER_PASSWORD:
2290 if (!wbinfo_change_user_password(string_arg)) {
2291 d_fprintf(stderr,
2292 "Could not change user password "
2293 "for user %s\n", string_arg);
2294 goto done;
2296 break;
2298 /* generic configuration options */
2299 case OPT_DOMAIN_NAME:
2300 case OPT_VERBOSE:
2301 case OPT_NTLMV2:
2302 case OPT_LANMAN:
2303 case OPT_LOGOFF_USER:
2304 case OPT_LOGOFF_UID:
2305 break;
2306 default:
2307 d_fprintf(stderr, "Invalid option\n");
2308 poptPrintHelp(pc, stderr, 0);
2309 goto done;
2313 result = 0;
2315 /* Exit code */
2317 done:
2318 talloc_free(frame);
2320 poptFreeContext(pc);
2321 return result;