Add an entry for the "check" command to the tdbtool manpage.
[Samba/gebeck_regimport.git] / nsswitch / wbinfo.c
blob36c2818ccf26b91a4a34528cdebd5f50def59ff4
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett 2002
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "winbind_client.h"
25 #include "libwbclient/wbclient.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
30 static struct wbcInterfaceDetails *init_interface_details(void)
32 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
33 static struct wbcInterfaceDetails *details;
35 if (details) {
36 return details;
39 wbc_status = wbcInterfaceDetails(&details);
40 if (!WBC_ERROR_IS_OK(wbc_status)) {
41 d_fprintf(stderr, "could not obtain winbind interface details!\n");
44 return details;
47 static char winbind_separator_int(bool strict)
49 struct wbcInterfaceDetails *details;
50 static bool got_sep;
51 static char sep;
53 if (got_sep)
54 return sep;
56 details = init_interface_details();
58 if (!details) {
59 d_fprintf(stderr, "could not obtain winbind separator!\n");
60 if (strict) {
61 return 0;
63 /* HACK: (this module should not call lp_ funtions) */
64 return *lp_winbind_separator();
67 sep = details->winbind_separator;
68 got_sep = true;
70 if (!sep) {
71 d_fprintf(stderr, "winbind separator was NULL!\n");
72 if (strict) {
73 return 0;
75 /* HACK: (this module should not call lp_ funtions) */
76 sep = *lp_winbind_separator();
79 return sep;
82 static char winbind_separator(void)
84 return winbind_separator_int(false);
87 static const char *get_winbind_domain(void)
89 static struct wbcInterfaceDetails *details;
91 details = init_interface_details();
93 if (!details) {
94 d_fprintf(stderr, "could not obtain winbind domain name!\n");
96 /* HACK: (this module should not call lp_ functions) */
97 return lp_workgroup();
100 return details->netbios_domain;
103 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
104 form DOMAIN/user into a domain and a user */
106 static bool parse_wbinfo_domain_user(const char *domuser, fstring domain,
107 fstring user)
110 char *p = strchr(domuser,winbind_separator());
112 if (!p) {
113 /* Maybe it was a UPN? */
114 if ((p = strchr(domuser, '@')) != NULL) {
115 fstrcpy(domain, "");
116 fstrcpy(user, domuser);
117 return true;
120 fstrcpy(user, domuser);
121 fstrcpy(domain, get_winbind_domain());
122 return true;
125 fstrcpy(user, p+1);
126 fstrcpy(domain, domuser);
127 domain[PTR_DIFF(p, domuser)] = 0;
128 strupper_m(domain);
130 return true;
133 /* Parse string of "uid,sid" or "gid,sid" into separate int and string values.
134 * Return true if input was valid, false otherwise. */
135 static bool parse_mapping_arg(char *arg, int *id, char **sid)
137 char *tmp, *endptr;
139 if (!arg || !*arg)
140 return false;
142 tmp = strtok(arg, ",");
143 *sid = strtok(NULL, ",");
145 if (!tmp || !*tmp || !*sid || !**sid)
146 return false;
148 /* Because atoi() can return 0 on invalid input, which would be a valid
149 * UID/GID we must use strtoul() and do error checking */
150 *id = strtoul(tmp, &endptr, 10);
152 if (endptr[0] != '\0')
153 return false;
155 return true;
158 /* pull pwent info for a given user */
160 static bool wbinfo_get_userinfo(char *user)
162 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
163 struct passwd *pwd = NULL;
165 wbc_status = wbcGetpwnam(user, &pwd);
166 if (!WBC_ERROR_IS_OK(wbc_status)) {
167 return false;
170 d_printf("%s:%s:%d:%d:%s:%s:%s\n",
171 pwd->pw_name,
172 pwd->pw_passwd,
173 pwd->pw_uid,
174 pwd->pw_gid,
175 pwd->pw_gecos,
176 pwd->pw_dir,
177 pwd->pw_shell);
179 return true;
182 /* pull pwent info for a given uid */
183 static bool wbinfo_get_uidinfo(int uid)
185 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
186 struct passwd *pwd = NULL;
188 wbc_status = wbcGetpwuid(uid, &pwd);
189 if (!WBC_ERROR_IS_OK(wbc_status)) {
190 return false;
193 d_printf("%s:%s:%d:%d:%s:%s:%s\n",
194 pwd->pw_name,
195 pwd->pw_passwd,
196 pwd->pw_uid,
197 pwd->pw_gid,
198 pwd->pw_gecos,
199 pwd->pw_dir,
200 pwd->pw_shell);
202 return true;
205 /* pull grent for a given group */
206 static bool wbinfo_get_groupinfo(const char *group)
208 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
209 struct group *grp;
211 wbc_status = wbcGetgrnam(group, &grp);
212 if (!WBC_ERROR_IS_OK(wbc_status)) {
213 return false;
216 d_printf("%s:%s:%d\n",
217 grp->gr_name,
218 grp->gr_passwd,
219 grp->gr_gid);
221 wbcFreeMemory(grp);
223 return true;
226 /* List groups a user is a member of */
228 static bool wbinfo_get_usergroups(const char *user)
230 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
231 uint32_t num_groups;
232 uint32_t i;
233 gid_t *groups = NULL;
235 /* Send request */
237 wbc_status = wbcGetGroups(user, &num_groups, &groups);
238 if (!WBC_ERROR_IS_OK(wbc_status)) {
239 return false;
242 for (i = 0; i < num_groups; i++) {
243 d_printf("%d\n", (int)groups[i]);
246 wbcFreeMemory(groups);
248 return true;
252 /* List group SIDs a user SID is a member of */
253 static bool wbinfo_get_usersids(const char *user_sid_str)
255 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
256 uint32_t num_sids;
257 uint32_t i;
258 struct wbcDomainSid user_sid, *sids = NULL;
260 /* Send request */
262 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
263 if (!WBC_ERROR_IS_OK(wbc_status)) {
264 return false;
267 wbc_status = wbcLookupUserSids(&user_sid, false, &num_sids, &sids);
268 if (!WBC_ERROR_IS_OK(wbc_status)) {
269 return false;
272 for (i = 0; i < num_sids; i++) {
273 char *str = NULL;
274 wbc_status = wbcSidToString(&sids[i], &str);
275 if (!WBC_ERROR_IS_OK(wbc_status)) {
276 wbcFreeMemory(sids);
277 return false;
279 d_printf("%s\n", str);
280 wbcFreeMemory(str);
283 wbcFreeMemory(sids);
285 return true;
288 static bool wbinfo_get_userdomgroups(const char *user_sid_str)
290 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
291 uint32_t num_sids;
292 uint32_t i;
293 struct wbcDomainSid user_sid, *sids = NULL;
295 /* Send request */
297 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
298 if (!WBC_ERROR_IS_OK(wbc_status)) {
299 return false;
302 wbc_status = wbcLookupUserSids(&user_sid, true, &num_sids, &sids);
303 if (!WBC_ERROR_IS_OK(wbc_status)) {
304 return false;
307 for (i = 0; i < num_sids; i++) {
308 char *str = NULL;
309 wbc_status = wbcSidToString(&sids[i], &str);
310 if (!WBC_ERROR_IS_OK(wbc_status)) {
311 wbcFreeMemory(sids);
312 return false;
314 d_printf("%s\n", str);
315 wbcFreeMemory(str);
318 wbcFreeMemory(sids);
320 return true;
323 /* Convert NetBIOS name to IP */
325 static bool wbinfo_wins_byname(const char *name)
327 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
328 char *ip = NULL;
330 wbc_status = wbcResolveWinsByName(name, &ip);
331 if (!WBC_ERROR_IS_OK(wbc_status)) {
332 return false;
335 /* Display response */
337 d_printf("%s\n", ip);
339 wbcFreeMemory(ip);
341 return true;
344 /* Convert IP to NetBIOS name */
346 static bool wbinfo_wins_byip(const char *ip)
348 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
349 char *name = NULL;
351 wbc_status = wbcResolveWinsByIP(ip, &name);
352 if (!WBC_ERROR_IS_OK(wbc_status)) {
353 return false;
356 /* Display response */
358 d_printf("%s\n", name);
360 wbcFreeMemory(name);
362 return true;
365 /* List all/trusted domains */
367 static bool wbinfo_list_domains(bool list_all_domains, bool verbose)
369 struct wbcDomainInfo *domain_list = NULL;
370 size_t num_domains;
371 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
372 bool print_all = !list_all_domains && verbose;
373 int i;
375 wbc_status = wbcListTrusts(&domain_list, &num_domains);
376 if (!WBC_ERROR_IS_OK(wbc_status)) {
377 return false;
380 if (print_all) {
381 d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n",
382 "Domain Name", "DNS Domain", "Trust Type",
383 "Transitive", "In", "Out");
386 for (i=0; i<num_domains; i++) {
387 if (print_all) {
388 d_printf("%-16s", domain_list[i].short_name);
389 } else {
390 d_printf("%s", domain_list[i].short_name);
391 d_printf("\n");
392 continue;
395 d_printf("%-24s", domain_list[i].dns_name);
397 switch(domain_list[i].trust_type) {
398 case WBC_DOMINFO_TRUSTTYPE_NONE:
399 d_printf("None ");
400 break;
401 case WBC_DOMINFO_TRUSTTYPE_FOREST:
402 d_printf("Forest ");
403 break;
404 case WBC_DOMINFO_TRUSTTYPE_EXTERNAL:
405 d_printf("External ");
406 break;
407 case WBC_DOMINFO_TRUSTTYPE_IN_FOREST:
408 d_printf("In-Forest ");
409 break;
412 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_TRANSITIVE) {
413 d_printf("Yes ");
414 } else {
415 d_printf("No ");
418 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_INCOMING) {
419 d_printf("Yes ");
420 } else {
421 d_printf("No ");
424 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_OUTGOING) {
425 d_printf("Yes ");
426 } else {
427 d_printf("No ");
430 d_printf("\n");
433 return true;
436 /* List own domain */
438 static bool wbinfo_list_own_domain(void)
440 d_printf("%s\n", get_winbind_domain());
442 return true;
445 /* show sequence numbers */
446 static bool wbinfo_show_sequence(const char *domain)
448 d_printf("This command has been deprecated. Please use the --online-status option instead.\n");
449 return false;
452 /* show sequence numbers */
453 static bool wbinfo_show_onlinestatus(const char *domain)
455 struct wbcDomainInfo *domain_list = NULL;
456 size_t num_domains;
457 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
458 int i;
460 wbc_status = wbcListTrusts(&domain_list, &num_domains);
461 if (!WBC_ERROR_IS_OK(wbc_status)) {
462 return false;
465 for (i=0; i<num_domains; i++) {
466 bool is_offline;
468 if (domain) {
469 if (!strequal(domain_list[i].short_name, domain)) {
470 continue;
474 is_offline = (domain_list[i].domain_flags & WBC_DOMINFO_DOMAIN_OFFLINE);
476 d_printf("%s : %s\n",
477 domain_list[i].short_name,
478 is_offline ? "offline" : "online" );
481 return true;
485 /* Show domain info */
487 static bool wbinfo_domain_info(const char *domain)
489 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
490 struct wbcDomainInfo *dinfo = NULL;
491 char *sid_str = NULL;
493 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')) {
494 domain = get_winbind_domain();
497 /* Send request */
499 wbc_status = wbcDomainInfo(domain, &dinfo);
500 if (!WBC_ERROR_IS_OK(wbc_status)) {
501 return false;
504 wbc_status = wbcSidToString(&dinfo->sid, &sid_str);
505 if (!WBC_ERROR_IS_OK(wbc_status)) {
506 wbcFreeMemory(dinfo);
507 return false;
510 /* Display response */
512 d_printf("Name : %s\n", dinfo->short_name);
513 d_printf("Alt_Name : %s\n", dinfo->dns_name);
515 d_printf("SID : %s\n", sid_str);
517 d_printf("Active Directory : %s\n",
518 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_AD) ? "Yes" : "No");
519 d_printf("Native : %s\n",
520 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_NATIVE) ? "Yes" : "No");
522 d_printf("Primary : %s\n",
523 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_PRIMARY) ? "Yes" : "No");
525 wbcFreeMemory(sid_str);
526 wbcFreeMemory(dinfo);
528 return true;
531 /* Get a foreign DC's name */
532 static bool wbinfo_getdcname(const char *domain_name)
534 struct winbindd_request request;
535 struct winbindd_response response;
537 ZERO_STRUCT(request);
538 ZERO_STRUCT(response);
540 fstrcpy(request.domain_name, domain_name);
542 /* Send request */
544 if (winbindd_request_response(WINBINDD_GETDCNAME, &request, &response) !=
545 NSS_STATUS_SUCCESS) {
546 d_fprintf(stderr, "Could not get dc name for %s\n", domain_name);
547 return false;
550 /* Display response */
552 d_printf("%s\n", response.data.dc_name);
554 return true;
557 /* Find a DC */
558 static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
560 struct winbindd_request request;
561 struct winbindd_response response;
563 ZERO_STRUCT(request);
564 ZERO_STRUCT(response);
566 fstrcpy(request.data.dsgetdcname.domain_name, domain_name);
567 request.data.dsgetdcname.flags = flags;
569 request.flags |= DS_DIRECTORY_SERVICE_REQUIRED;
571 /* Send request */
573 if (winbindd_request_response(WINBINDD_DSGETDCNAME, &request, &response) !=
574 NSS_STATUS_SUCCESS) {
575 d_fprintf(stderr, "Could not find dc for %s\n", domain_name);
576 return false;
579 /* Display response */
581 d_printf("%s\n", response.data.dsgetdcname.dc_unc);
582 d_printf("%s\n", response.data.dsgetdcname.dc_address);
583 d_printf("%d\n", response.data.dsgetdcname.dc_address_type);
584 d_printf("%s\n", response.data.dsgetdcname.domain_guid);
585 d_printf("%s\n", response.data.dsgetdcname.domain_name);
586 d_printf("%s\n", response.data.dsgetdcname.forest_name);
587 d_printf("0x%08x\n", response.data.dsgetdcname.dc_flags);
588 d_printf("%s\n", response.data.dsgetdcname.dc_site_name);
589 d_printf("%s\n", response.data.dsgetdcname.client_site_name);
591 return true;
594 /* Check trust account password */
596 static bool wbinfo_check_secret(void)
598 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
599 struct wbcAuthErrorInfo *error = NULL;
601 wbc_status = wbcCheckTrustCredentials(NULL, &error);
603 d_printf("checking the trust secret via RPC calls %s\n",
604 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
606 if (wbc_status == WBC_ERR_AUTH_ERROR) {
607 d_fprintf(stderr, "error code was %s (0x%x)\n",
608 error->nt_string, error->nt_status);
609 wbcFreeMemory(error);
611 if (!WBC_ERROR_IS_OK(wbc_status)) {
612 return false;
615 return true;
618 /* Convert uid to sid */
620 static bool wbinfo_uid_to_sid(uid_t uid)
622 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
623 struct wbcDomainSid sid;
624 char *sid_str = NULL;
626 /* Send request */
628 wbc_status = wbcUidToSid(uid, &sid);
629 if (!WBC_ERROR_IS_OK(wbc_status)) {
630 return false;
633 wbc_status = wbcSidToString(&sid, &sid_str);
634 if (!WBC_ERROR_IS_OK(wbc_status)) {
635 return false;
638 /* Display response */
640 d_printf("%s\n", sid_str);
642 wbcFreeMemory(sid_str);
644 return true;
647 /* Convert gid to sid */
649 static bool wbinfo_gid_to_sid(gid_t gid)
651 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
652 struct wbcDomainSid sid;
653 char *sid_str = NULL;
655 /* Send request */
657 wbc_status = wbcGidToSid(gid, &sid);
658 if (!WBC_ERROR_IS_OK(wbc_status)) {
659 return false;
662 wbc_status = wbcSidToString(&sid, &sid_str);
663 if (!WBC_ERROR_IS_OK(wbc_status)) {
664 return false;
667 /* Display response */
669 d_printf("%s\n", sid_str);
671 wbcFreeMemory(sid_str);
673 return true;
676 /* Convert sid to uid */
678 static bool wbinfo_sid_to_uid(const char *sid_str)
680 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
681 struct wbcDomainSid sid;
682 uid_t uid;
684 /* Send request */
686 wbc_status = wbcStringToSid(sid_str, &sid);
687 if (!WBC_ERROR_IS_OK(wbc_status)) {
688 return false;
691 wbc_status = wbcSidToUid(&sid, &uid);
692 if (!WBC_ERROR_IS_OK(wbc_status)) {
693 return false;
696 /* Display response */
698 d_printf("%d\n", (int)uid);
700 return true;
703 static bool wbinfo_sid_to_gid(const char *sid_str)
705 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
706 struct wbcDomainSid sid;
707 gid_t gid;
709 /* Send request */
711 wbc_status = wbcStringToSid(sid_str, &sid);
712 if (!WBC_ERROR_IS_OK(wbc_status)) {
713 return false;
716 wbc_status = wbcSidToGid(&sid, &gid);
717 if (!WBC_ERROR_IS_OK(wbc_status)) {
718 return false;
721 /* Display response */
723 d_printf("%d\n", (int)gid);
725 return true;
728 static bool wbinfo_allocate_uid(void)
730 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
731 uid_t uid;
733 /* Send request */
735 wbc_status = wbcAllocateUid(&uid);
736 if (!WBC_ERROR_IS_OK(wbc_status)) {
737 return false;
740 /* Display response */
742 d_printf("New uid: %d\n", uid);
744 return true;
747 static bool wbinfo_allocate_gid(void)
749 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
750 gid_t gid;
752 /* Send request */
754 wbc_status = wbcAllocateGid(&gid);
755 if (!WBC_ERROR_IS_OK(wbc_status)) {
756 return false;
759 /* Display response */
761 d_printf("New gid: %d\n", gid);
763 return true;
766 static bool wbinfo_set_uid_mapping(uid_t uid, const char *sid_str)
768 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
769 struct wbcDomainSid sid;
771 /* Send request */
773 wbc_status = wbcStringToSid(sid_str, &sid);
774 if (!WBC_ERROR_IS_OK(wbc_status)) {
775 return false;
778 wbc_status = wbcSetUidMapping(uid, &sid);
779 if (!WBC_ERROR_IS_OK(wbc_status)) {
780 return false;
783 /* Display response */
785 d_printf("uid %d now mapped to sid %s\n", uid, sid_str);
787 return true;
790 static bool wbinfo_set_gid_mapping(gid_t gid, const char *sid_str)
792 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
793 struct wbcDomainSid sid;
795 /* Send request */
797 wbc_status = wbcStringToSid(sid_str, &sid);
798 if (!WBC_ERROR_IS_OK(wbc_status)) {
799 return false;
802 wbc_status = wbcSetGidMapping(gid, &sid);
803 if (!WBC_ERROR_IS_OK(wbc_status)) {
804 return false;
807 /* Display response */
809 d_printf("gid %d now mapped to sid %s\n", gid, sid_str);
811 return true;
814 static bool wbinfo_remove_uid_mapping(uid_t uid, const char *sid_str)
816 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
817 struct wbcDomainSid sid;
819 /* Send request */
821 wbc_status = wbcStringToSid(sid_str, &sid);
822 if (!WBC_ERROR_IS_OK(wbc_status)) {
823 return false;
826 wbc_status = wbcRemoveUidMapping(uid, &sid);
827 if (!WBC_ERROR_IS_OK(wbc_status)) {
828 return false;
831 /* Display response */
833 d_printf("Removed uid %d to sid %s mapping\n", uid, sid_str);
835 return true;
838 static bool wbinfo_remove_gid_mapping(gid_t gid, const char *sid_str)
840 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
841 struct wbcDomainSid sid;
843 /* Send request */
845 wbc_status = wbcStringToSid(sid_str, &sid);
846 if (!WBC_ERROR_IS_OK(wbc_status)) {
847 return false;
850 wbc_status = wbcRemoveGidMapping(gid, &sid);
851 if (!WBC_ERROR_IS_OK(wbc_status)) {
852 return false;
855 /* Display response */
857 d_printf("Removed gid %d to sid %s mapping\n", gid, sid_str);
859 return true;
862 /* Convert sid to string */
864 static bool wbinfo_lookupsid(const char *sid_str)
866 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
867 struct wbcDomainSid sid;
868 char *domain;
869 char *name;
870 enum wbcSidType type;
872 /* Send off request */
874 wbc_status = wbcStringToSid(sid_str, &sid);
875 if (!WBC_ERROR_IS_OK(wbc_status)) {
876 return false;
879 wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
880 if (!WBC_ERROR_IS_OK(wbc_status)) {
881 return false;
884 /* Display response */
886 d_printf("%s%c%s %d\n",
887 domain, winbind_separator(), name, type);
889 return true;
892 /* Convert sid to fullname */
894 static bool wbinfo_lookupsid_fullname(const char *sid_str)
896 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
897 struct wbcDomainSid sid;
898 char *domain;
899 char *name;
900 enum wbcSidType type;
902 /* Send off request */
904 wbc_status = wbcStringToSid(sid_str, &sid);
905 if (!WBC_ERROR_IS_OK(wbc_status)) {
906 return false;
909 wbc_status = wbcGetDisplayName(&sid, &domain, &name, &type);
910 if (!WBC_ERROR_IS_OK(wbc_status)) {
911 return false;
914 /* Display response */
916 d_printf("%s%c%s %d\n",
917 domain, winbind_separator(), name, type);
919 return true;
922 /* Lookup a list of RIDs */
924 static bool wbinfo_lookuprids(const char *domain, const char *arg)
926 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
927 struct wbcDomainInfo *dinfo = NULL;
928 char *domain_name = NULL;
929 const char **names = NULL;
930 enum wbcSidType *types = NULL;
931 size_t i;
932 int num_rids;
933 uint32 *rids = NULL;
934 const char *p;
935 char *ridstr;
936 TALLOC_CTX *mem_ctx = NULL;
937 bool ret = false;
939 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')) {
940 domain = get_winbind_domain();
943 /* Send request */
945 wbc_status = wbcDomainInfo(domain, &dinfo);
946 if (!WBC_ERROR_IS_OK(wbc_status)) {
947 d_printf("wbcDomainInfo(%s) failed: %s\n", domain,
948 wbcErrorString(wbc_status));
949 goto done;
952 mem_ctx = talloc_new(NULL);
953 if (mem_ctx == NULL) {
954 d_printf("talloc_new failed\n");
955 goto done;
958 num_rids = 0;
959 rids = NULL;
960 p = arg;
962 while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
963 uint32 rid = strtoul(ridstr, NULL, 10);
964 ADD_TO_ARRAY(mem_ctx, uint32, rid, &rids, &num_rids);
967 if (rids == NULL) {
968 d_printf("no rids\n");
969 goto done;
972 wbc_status = wbcLookupRids(&dinfo->sid, num_rids, rids,
973 (const char **)&domain_name, &names, &types);
974 if (!WBC_ERROR_IS_OK(wbc_status)) {
975 d_printf("winbind_lookup_rids failed: %s\n",
976 wbcErrorString(wbc_status));
977 goto done;
980 d_printf("Domain: %s\n", domain_name);
982 for (i=0; i<num_rids; i++) {
983 d_printf("%8d: %s (%s)\n", rids[i], names[i],
984 sid_type_lookup(types[i]));
987 ret = true;
988 done:
989 if (dinfo) {
990 wbcFreeMemory(dinfo);
992 if (domain_name) {
993 wbcFreeMemory(domain_name);
995 if (names) {
996 wbcFreeMemory(names);
998 if (types) {
999 wbcFreeMemory(types);
1001 TALLOC_FREE(mem_ctx);
1002 return ret;
1005 /* Convert string to sid */
1007 static bool wbinfo_lookupname(const char *full_name)
1009 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1010 struct wbcDomainSid sid;
1011 char *sid_str;
1012 enum wbcSidType type;
1013 fstring domain_name;
1014 fstring account_name;
1016 /* Send off request */
1018 parse_wbinfo_domain_user(full_name, domain_name,
1019 account_name);
1021 wbc_status = wbcLookupName(domain_name, account_name,
1022 &sid, &type);
1023 if (!WBC_ERROR_IS_OK(wbc_status)) {
1024 return false;
1027 wbc_status = wbcSidToString(&sid, &sid_str);
1028 if (!WBC_ERROR_IS_OK(wbc_status)) {
1029 return false;
1032 /* Display response */
1034 d_printf("%s %s (%d)\n", sid_str, sid_type_lookup(type), type);
1036 wbcFreeMemory(sid_str);
1038 return true;
1041 static char *wbinfo_prompt_pass(const char *prefix,
1042 const char *username)
1044 char *prompt;
1045 const char *ret = NULL;
1047 prompt = talloc_asprintf(talloc_tos(), "Enter %s's ", username);
1048 if (!prompt) {
1049 return NULL;
1051 if (prefix) {
1052 prompt = talloc_asprintf_append(prompt, "%s ", prefix);
1053 if (!prompt) {
1054 return NULL;
1057 prompt = talloc_asprintf_append(prompt, "password: ");
1058 if (!prompt) {
1059 return NULL;
1062 ret = getpass(prompt);
1063 TALLOC_FREE(prompt);
1065 return SMB_STRDUP(ret);
1068 /* Authenticate a user with a plaintext password */
1070 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
1072 struct winbindd_request request;
1073 struct winbindd_response response;
1074 NSS_STATUS result;
1075 char *p;
1076 char *password;
1078 /* Send off request */
1080 ZERO_STRUCT(request);
1081 ZERO_STRUCT(response);
1083 p = strchr(username, '%');
1085 if (p) {
1086 *p = 0;
1087 fstrcpy(request.data.auth.user, username);
1088 fstrcpy(request.data.auth.pass, p + 1);
1089 *p = '%';
1090 } else {
1091 fstrcpy(request.data.auth.user, username);
1092 password = wbinfo_prompt_pass(NULL, username);
1093 fstrcpy(request.data.auth.pass, password);
1094 SAFE_FREE(password);
1097 request.flags = flags;
1099 fstrcpy(request.data.auth.krb5_cc_type, cctype);
1101 request.data.auth.uid = geteuid();
1103 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
1105 /* Display response */
1107 d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n",
1108 username, (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", cctype);
1110 if (response.data.auth.nt_status)
1111 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1112 response.data.auth.nt_status_string,
1113 response.data.auth.nt_status,
1114 response.data.auth.error_string);
1116 if (result == NSS_STATUS_SUCCESS) {
1118 if (request.flags & WBFLAG_PAM_INFO3_TEXT) {
1119 if (response.data.auth.info3.user_flgs & NETLOGON_CACHED_ACCOUNT) {
1120 d_printf("user_flgs: NETLOGON_CACHED_ACCOUNT\n");
1124 if (response.data.auth.krb5ccname[0] != '\0') {
1125 d_printf("credentials were put in: %s\n", response.data.auth.krb5ccname);
1126 } else {
1127 d_printf("no credentials cached\n");
1131 return result == NSS_STATUS_SUCCESS;
1134 /* Authenticate a user with a plaintext password */
1136 static bool wbinfo_auth(char *username)
1138 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1139 char *s = NULL;
1140 char *p = NULL;
1141 char *password = NULL;
1142 char *name = NULL;
1144 if ((s = SMB_STRDUP(username)) == NULL) {
1145 return false;
1148 if ((p = strchr(s, '%')) != NULL) {
1149 *p = 0;
1150 p++;
1151 password = SMB_STRDUP(p);
1152 } else {
1153 password = wbinfo_prompt_pass(NULL, username);
1156 name = s;
1158 wbc_status = wbcAuthenticateUser(name, password);
1160 d_printf("plaintext password authentication %s\n",
1161 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1163 #if 0
1164 if (response.data.auth.nt_status)
1165 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1166 response.data.auth.nt_status_string,
1167 response.data.auth.nt_status,
1168 response.data.auth.error_string);
1169 #endif
1171 SAFE_FREE(s);
1172 SAFE_FREE(password);
1174 return WBC_ERROR_IS_OK(wbc_status);
1177 /* Authenticate a user with a challenge/response */
1179 static bool wbinfo_auth_crap(char *username)
1181 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1182 struct wbcAuthUserParams params;
1183 struct wbcAuthUserInfo *info = NULL;
1184 struct wbcAuthErrorInfo *err = NULL;
1185 DATA_BLOB lm = data_blob_null;
1186 DATA_BLOB nt = data_blob_null;
1187 fstring name_user;
1188 fstring name_domain;
1189 char *pass;
1190 char *p;
1192 p = strchr(username, '%');
1194 if (p) {
1195 *p = 0;
1196 pass = SMB_STRDUP(p + 1);
1197 } else {
1198 pass = wbinfo_prompt_pass(NULL, username);
1201 parse_wbinfo_domain_user(username, name_domain, name_user);
1203 params.account_name = name_user;
1204 params.domain_name = name_domain;
1205 params.workstation_name = NULL;
1207 params.flags = 0;
1208 params.parameter_control= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
1209 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
1211 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
1213 generate_random_buffer(params.password.response.challenge, 8);
1215 if (lp_client_ntlmv2_auth()) {
1216 DATA_BLOB server_chal;
1217 DATA_BLOB names_blob;
1219 server_chal = data_blob(params.password.response.challenge, 8);
1221 /* Pretend this is a login to 'us', for blob purposes */
1222 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
1224 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
1225 &names_blob,
1226 &lm, &nt, NULL)) {
1227 data_blob_free(&names_blob);
1228 data_blob_free(&server_chal);
1229 SAFE_FREE(pass);
1230 return false;
1232 data_blob_free(&names_blob);
1233 data_blob_free(&server_chal);
1235 } else {
1236 if (lp_client_lanman_auth()) {
1237 bool ok;
1238 lm = data_blob(NULL, 24);
1239 ok = SMBencrypt(pass, params.password.response.challenge,
1240 lm.data);
1241 if (!ok) {
1242 data_blob_free(&lm);
1245 nt = data_blob(NULL, 24);
1246 SMBNTencrypt(pass, params.password.response.challenge,
1247 nt.data);
1250 params.password.response.nt_length = nt.length;
1251 params.password.response.nt_data = nt.data;
1252 params.password.response.lm_length = lm.length;
1253 params.password.response.lm_data = lm.data;
1255 wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
1257 /* Display response */
1259 d_printf("challenge/response password authentication %s\n",
1260 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1262 if (wbc_status == WBC_ERR_AUTH_ERROR) {
1263 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1264 err->nt_string,
1265 err->nt_status,
1266 err->display_string);
1267 wbcFreeMemory(err);
1268 } else if (WBC_ERROR_IS_OK(wbc_status)) {
1269 wbcFreeMemory(info);
1272 data_blob_free(&nt);
1273 data_blob_free(&lm);
1274 SAFE_FREE(pass);
1276 return WBC_ERROR_IS_OK(wbc_status);
1279 /* Authenticate a user with a plaintext password and set a token */
1281 static bool wbinfo_klog(char *username)
1283 struct winbindd_request request;
1284 struct winbindd_response response;
1285 NSS_STATUS result;
1286 char *p;
1288 /* Send off request */
1290 ZERO_STRUCT(request);
1291 ZERO_STRUCT(response);
1293 p = strchr(username, '%');
1295 if (p) {
1296 *p = 0;
1297 fstrcpy(request.data.auth.user, username);
1298 fstrcpy(request.data.auth.pass, p + 1);
1299 *p = '%';
1300 } else {
1301 fstrcpy(request.data.auth.user, username);
1302 fstrcpy(request.data.auth.pass, getpass("Password: "));
1305 request.flags |= WBFLAG_PAM_AFS_TOKEN;
1307 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
1309 /* Display response */
1311 d_printf("plaintext password authentication %s\n",
1312 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1314 if (response.data.auth.nt_status)
1315 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1316 response.data.auth.nt_status_string,
1317 response.data.auth.nt_status,
1318 response.data.auth.error_string);
1320 if (result != NSS_STATUS_SUCCESS)
1321 return false;
1323 if (response.extra_data.data == NULL) {
1324 d_fprintf(stderr, "Did not get token data\n");
1325 return false;
1328 if (!afs_settoken_str((char *)response.extra_data.data)) {
1329 d_fprintf(stderr, "Could not set token\n");
1330 return false;
1333 d_printf("Successfully created AFS token\n");
1334 return true;
1337 /* Print domain users */
1339 static bool print_domain_users(const char *domain)
1341 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1342 uint32_t i;
1343 uint32_t num_users = 0;
1344 const char **users = NULL;
1346 /* Send request to winbind daemon */
1348 /* '.' is the special sign for our own domain */
1349 if (domain && strcmp(domain, ".") == 0) {
1350 domain = get_winbind_domain();
1353 wbc_status = wbcListUsers(domain, &num_users, &users);
1354 if (!WBC_ERROR_IS_OK(wbc_status)) {
1355 return false;
1358 for (i=0; i < num_users; i++) {
1359 d_printf("%s\n", users[i]);
1362 wbcFreeMemory(users);
1364 return true;
1367 /* Print domain groups */
1369 static bool print_domain_groups(const char *domain)
1371 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1372 uint32_t i;
1373 uint32_t num_groups = 0;
1374 const char **groups = NULL;
1376 /* Send request to winbind daemon */
1378 /* '.' is the special sign for our own domain */
1379 if (domain && strcmp(domain, ".") == 0) {
1380 domain = get_winbind_domain();
1383 wbc_status = wbcListGroups(domain, &num_groups, &groups);
1384 if (!WBC_ERROR_IS_OK(wbc_status)) {
1385 return false;
1388 for (i=0; i < num_groups; i++) {
1389 d_printf("%s\n", groups[i]);
1392 wbcFreeMemory(groups);
1394 return true;
1397 /* Set the authorised user for winbindd access in secrets.tdb */
1399 static bool wbinfo_set_auth_user(char *username)
1401 const char *password;
1402 char *p;
1403 fstring user, domain;
1405 /* Separate into user and password */
1407 parse_wbinfo_domain_user(username, domain, user);
1409 p = strchr(user, '%');
1411 if (p != NULL) {
1412 *p = 0;
1413 password = p+1;
1414 } else {
1415 char *thepass = getpass("Password: ");
1416 if (thepass) {
1417 password = thepass;
1418 } else
1419 password = "";
1422 /* Store or remove DOMAIN\username%password in secrets.tdb */
1424 secrets_init();
1426 if (user[0]) {
1428 if (!secrets_store(SECRETS_AUTH_USER, user,
1429 strlen(user) + 1)) {
1430 d_fprintf(stderr, "error storing username\n");
1431 return false;
1434 /* We always have a domain name added by the
1435 parse_wbinfo_domain_user() function. */
1437 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
1438 strlen(domain) + 1)) {
1439 d_fprintf(stderr, "error storing domain name\n");
1440 return false;
1443 } else {
1444 secrets_delete(SECRETS_AUTH_USER);
1445 secrets_delete(SECRETS_AUTH_DOMAIN);
1448 if (password[0]) {
1450 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
1451 strlen(password) + 1)) {
1452 d_fprintf(stderr, "error storing password\n");
1453 return false;
1456 } else
1457 secrets_delete(SECRETS_AUTH_PASSWORD);
1459 return true;
1462 static void wbinfo_get_auth_user(void)
1464 char *user, *domain, *password;
1466 /* Lift data from secrets file */
1468 secrets_fetch_ipc_userpass(&user, &domain, &password);
1470 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
1472 SAFE_FREE(user);
1473 SAFE_FREE(domain);
1474 SAFE_FREE(password);
1475 d_printf("No authorised user configured\n");
1476 return;
1479 /* Pretty print authorised user info */
1481 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
1482 user, password ? "%" : "", password ? password : "");
1484 SAFE_FREE(user);
1485 SAFE_FREE(domain);
1486 SAFE_FREE(password);
1489 static bool wbinfo_ping(void)
1491 wbcErr wbc_status;
1493 wbc_status = wbcPing();
1495 /* Display response */
1497 d_printf("Ping to winbindd %s\n",
1498 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1500 return WBC_ERROR_IS_OK(wbc_status);
1503 static bool wbinfo_change_user_password(const char *username)
1505 wbcErr wbc_status;
1506 char *old_password = NULL;
1507 char *new_password = NULL;
1509 old_password = wbinfo_prompt_pass("old", username);
1510 new_password = wbinfo_prompt_pass("new", username);
1512 wbc_status = wbcChangeUserPassword(username, old_password, new_password);
1514 /* Display response */
1516 d_printf("Password change for user %s %s\n", username,
1517 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1519 SAFE_FREE(old_password);
1520 SAFE_FREE(new_password);
1522 return WBC_ERROR_IS_OK(wbc_status);
1525 /* Main program */
1527 enum {
1528 OPT_SET_AUTH_USER = 1000,
1529 OPT_GET_AUTH_USER,
1530 OPT_DOMAIN_NAME,
1531 OPT_SEQUENCE,
1532 OPT_GETDCNAME,
1533 OPT_DSGETDCNAME,
1534 OPT_USERDOMGROUPS,
1535 OPT_USERSIDS,
1536 OPT_ALLOCATE_UID,
1537 OPT_ALLOCATE_GID,
1538 OPT_SET_UID_MAPPING,
1539 OPT_SET_GID_MAPPING,
1540 OPT_REMOVE_UID_MAPPING,
1541 OPT_REMOVE_GID_MAPPING,
1542 OPT_SEPARATOR,
1543 OPT_LIST_ALL_DOMAINS,
1544 OPT_LIST_OWN_DOMAIN,
1545 OPT_UID_INFO,
1546 OPT_GROUP_INFO,
1547 OPT_VERBOSE,
1548 OPT_ONLINESTATUS,
1549 OPT_CHANGE_USER_PASSWORD,
1550 OPT_SID_TO_FULLNAME
1553 int main(int argc, char **argv, char **envp)
1555 int opt;
1556 TALLOC_CTX *frame = talloc_stackframe();
1557 poptContext pc;
1558 static char *string_arg;
1559 char *string_subarg = NULL;
1560 static char *opt_domain_name;
1561 static int int_arg;
1562 int int_subarg = -1;
1563 int result = 1;
1564 bool verbose = false;
1566 struct poptOption long_options[] = {
1567 POPT_AUTOHELP
1569 /* longName, shortName, argInfo, argPtr, value, descrip,
1570 argDesc */
1572 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1573 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1574 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1575 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1576 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1577 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1578 { "sid-to-fullname", 0, POPT_ARG_STRING, &string_arg,
1579 OPT_SID_TO_FULLNAME, "Converts sid to fullname", "SID" },
1580 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1581 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1582 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1583 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1584 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1585 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1586 "Get a new UID out of idmap" },
1587 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1588 "Get a new GID out of idmap" },
1589 { "set-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_UID_MAPPING, "Create or modify uid to sid mapping in idmap", "UID,SID" },
1590 { "set-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_GID_MAPPING, "Create or modify gid to sid mapping in idmap", "GID,SID" },
1591 { "remove-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_UID_MAPPING, "Remove uid to sid mapping in idmap", "UID,SID" },
1592 { "remove-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_GID_MAPPING, "Remove gid to sid mapping in idmap", "GID,SID" },
1593 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1594 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1595 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1596 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1597 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1598 { "online-status", 0, POPT_ARG_NONE, 0, OPT_ONLINESTATUS, "Show whether domains are marked as online or offline"},
1599 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1600 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1601 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1602 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1603 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1604 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1605 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1606 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1607 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1608 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1609 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1610 "Get a DC name for a foreign domain", "domainname" },
1611 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
1612 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1613 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1614 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1615 #ifdef WITH_FAKE_KASERVER
1616 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1617 #endif
1618 #ifdef HAVE_KRB5
1619 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1620 /* destroys wbinfo --help output */
1621 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1622 #endif
1623 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1624 { "verbose", 0, POPT_ARG_NONE, 0, OPT_VERBOSE, "Print additional information per command", NULL },
1625 { "change-user-password", 0, POPT_ARG_STRING, &string_arg, OPT_CHANGE_USER_PASSWORD, "Change the password for a user", NULL },
1626 POPT_COMMON_CONFIGFILE
1627 POPT_COMMON_VERSION
1628 POPT_TABLEEND
1631 /* Samba client initialisation */
1632 load_case_tables();
1635 /* Parse options */
1637 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1639 /* Parse command line options */
1641 if (argc == 1) {
1642 poptPrintHelp(pc, stderr, 0);
1643 return 1;
1646 while((opt = poptGetNextOpt(pc)) != -1) {
1647 /* get the generic configuration parameters like --domain */
1648 switch (opt) {
1649 case OPT_VERBOSE:
1650 verbose = True;
1651 break;
1655 poptFreeContext(pc);
1657 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, true)) {
1658 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1659 get_dyn_CONFIGFILE(), strerror(errno));
1660 exit(1);
1663 if (!init_names())
1664 return 1;
1666 load_interfaces();
1668 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1669 POPT_CONTEXT_KEEP_FIRST);
1671 while((opt = poptGetNextOpt(pc)) != -1) {
1672 switch (opt) {
1673 case 'u':
1674 if (!print_domain_users(opt_domain_name)) {
1675 d_fprintf(stderr, "Error looking up domain users\n");
1676 goto done;
1678 break;
1679 case 'g':
1680 if (!print_domain_groups(opt_domain_name)) {
1681 d_fprintf(stderr, "Error looking up domain groups\n");
1682 goto done;
1684 break;
1685 case 's':
1686 if (!wbinfo_lookupsid(string_arg)) {
1687 d_fprintf(stderr, "Could not lookup sid %s\n", string_arg);
1688 goto done;
1690 break;
1691 case OPT_SID_TO_FULLNAME:
1692 if (!wbinfo_lookupsid_fullname(string_arg)) {
1693 d_fprintf(stderr, "Could not lookup sid %s\n",
1694 string_arg);
1695 goto done;
1697 break;
1698 case 'R':
1699 if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
1700 d_fprintf(stderr, "Could not lookup RIDs %s\n", string_arg);
1701 goto done;
1703 break;
1704 case 'n':
1705 if (!wbinfo_lookupname(string_arg)) {
1706 d_fprintf(stderr, "Could not lookup name %s\n", string_arg);
1707 goto done;
1709 break;
1710 case 'N':
1711 if (!wbinfo_wins_byname(string_arg)) {
1712 d_fprintf(stderr, "Could not lookup WINS by name %s\n", string_arg);
1713 goto done;
1715 break;
1716 case 'I':
1717 if (!wbinfo_wins_byip(string_arg)) {
1718 d_fprintf(stderr, "Could not lookup WINS by IP %s\n", string_arg);
1719 goto done;
1721 break;
1722 case 'U':
1723 if (!wbinfo_uid_to_sid(int_arg)) {
1724 d_fprintf(stderr, "Could not convert uid %d to sid\n", int_arg);
1725 goto done;
1727 break;
1728 case 'G':
1729 if (!wbinfo_gid_to_sid(int_arg)) {
1730 d_fprintf(stderr, "Could not convert gid %d to sid\n",
1731 int_arg);
1732 goto done;
1734 break;
1735 case 'S':
1736 if (!wbinfo_sid_to_uid(string_arg)) {
1737 d_fprintf(stderr, "Could not convert sid %s to uid\n",
1738 string_arg);
1739 goto done;
1741 break;
1742 case 'Y':
1743 if (!wbinfo_sid_to_gid(string_arg)) {
1744 d_fprintf(stderr, "Could not convert sid %s to gid\n",
1745 string_arg);
1746 goto done;
1748 break;
1749 case OPT_ALLOCATE_UID:
1750 if (!wbinfo_allocate_uid()) {
1751 d_fprintf(stderr, "Could not allocate a uid\n");
1752 goto done;
1754 break;
1755 case OPT_ALLOCATE_GID:
1756 if (!wbinfo_allocate_gid()) {
1757 d_fprintf(stderr, "Could not allocate a gid\n");
1758 goto done;
1760 break;
1761 case OPT_SET_UID_MAPPING:
1762 if (!parse_mapping_arg(string_arg, &int_subarg,
1763 &string_subarg) ||
1764 !wbinfo_set_uid_mapping(int_subarg, string_subarg))
1766 d_fprintf(stderr, "Could not create or modify "
1767 "uid to sid mapping\n");
1768 goto done;
1770 break;
1771 case OPT_SET_GID_MAPPING:
1772 if (!parse_mapping_arg(string_arg, &int_subarg,
1773 &string_subarg) ||
1774 !wbinfo_set_gid_mapping(int_subarg, string_subarg))
1776 d_fprintf(stderr, "Could not create or modify "
1777 "gid to sid mapping\n");
1778 goto done;
1780 break;
1781 case OPT_REMOVE_UID_MAPPING:
1782 if (!parse_mapping_arg(string_arg, &int_subarg,
1783 &string_subarg) ||
1784 !wbinfo_remove_uid_mapping(int_subarg,
1785 string_subarg))
1787 d_fprintf(stderr, "Could not remove uid to sid "
1788 "mapping\n");
1789 goto done;
1791 break;
1792 case OPT_REMOVE_GID_MAPPING:
1793 if (!parse_mapping_arg(string_arg, &int_subarg,
1794 &string_subarg) ||
1795 !wbinfo_remove_gid_mapping(int_subarg,
1796 string_subarg))
1798 d_fprintf(stderr, "Could not remove gid to sid "
1799 "mapping\n");
1800 goto done;
1802 break;
1803 case 't':
1804 if (!wbinfo_check_secret()) {
1805 d_fprintf(stderr, "Could not check secret\n");
1806 goto done;
1808 break;
1809 case 'm':
1810 if (!wbinfo_list_domains(false, verbose)) {
1811 d_fprintf(stderr, "Could not list trusted domains\n");
1812 goto done;
1814 break;
1815 case OPT_SEQUENCE:
1816 if (!wbinfo_show_sequence(opt_domain_name)) {
1817 d_fprintf(stderr, "Could not show sequence numbers\n");
1818 goto done;
1820 break;
1821 case OPT_ONLINESTATUS:
1822 if (!wbinfo_show_onlinestatus(opt_domain_name)) {
1823 d_fprintf(stderr, "Could not show online-status\n");
1824 goto done;
1826 break;
1827 case 'D':
1828 if (!wbinfo_domain_info(string_arg)) {
1829 d_fprintf(stderr, "Could not get domain info\n");
1830 goto done;
1832 break;
1833 case 'i':
1834 if (!wbinfo_get_userinfo(string_arg)) {
1835 d_fprintf(stderr, "Could not get info for user %s\n",
1836 string_arg);
1837 goto done;
1839 break;
1840 case OPT_UID_INFO:
1841 if ( !wbinfo_get_uidinfo(int_arg)) {
1842 d_fprintf(stderr, "Could not get info for uid "
1843 "%d\n", int_arg);
1844 goto done;
1846 break;
1847 case OPT_GROUP_INFO:
1848 if ( !wbinfo_get_groupinfo(string_arg)) {
1849 d_fprintf(stderr, "Could not get info for "
1850 "group %s\n", string_arg);
1851 goto done;
1853 break;
1854 case 'r':
1855 if (!wbinfo_get_usergroups(string_arg)) {
1856 d_fprintf(stderr, "Could not get groups for user %s\n",
1857 string_arg);
1858 goto done;
1860 break;
1861 case OPT_USERSIDS:
1862 if (!wbinfo_get_usersids(string_arg)) {
1863 d_fprintf(stderr, "Could not get group SIDs for user SID %s\n",
1864 string_arg);
1865 goto done;
1867 break;
1868 case OPT_USERDOMGROUPS:
1869 if (!wbinfo_get_userdomgroups(string_arg)) {
1870 d_fprintf(stderr, "Could not get user's domain groups "
1871 "for user SID %s\n", string_arg);
1872 goto done;
1874 break;
1875 case 'a': {
1876 bool got_error = false;
1878 if (!wbinfo_auth(string_arg)) {
1879 d_fprintf(stderr, "Could not authenticate user %s with "
1880 "plaintext password\n", string_arg);
1881 got_error = true;
1884 if (!wbinfo_auth_crap(string_arg)) {
1885 d_fprintf(stderr, "Could not authenticate user %s with "
1886 "challenge/response\n", string_arg);
1887 got_error = true;
1890 if (got_error)
1891 goto done;
1892 break;
1894 case 'K': {
1895 uint32 flags = WBFLAG_PAM_KRB5 |
1896 WBFLAG_PAM_CACHED_LOGIN |
1897 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
1898 WBFLAG_PAM_INFO3_TEXT;
1900 if (!wbinfo_auth_krb5(string_arg, "FILE", flags)) {
1901 d_fprintf(stderr, "Could not authenticate user [%s] with "
1902 "Kerberos (ccache: %s)\n", string_arg, "FILE");
1903 goto done;
1905 break;
1907 case 'k':
1908 if (!wbinfo_klog(string_arg)) {
1909 d_fprintf(stderr, "Could not klog user\n");
1910 goto done;
1912 break;
1913 case 'p':
1914 if (!wbinfo_ping()) {
1915 d_fprintf(stderr, "could not ping winbindd!\n");
1916 goto done;
1918 break;
1919 case OPT_SET_AUTH_USER:
1920 if (!wbinfo_set_auth_user(string_arg)) {
1921 goto done;
1923 break;
1924 case OPT_GET_AUTH_USER:
1925 wbinfo_get_auth_user();
1926 break;
1927 case OPT_GETDCNAME:
1928 if (!wbinfo_getdcname(string_arg)) {
1929 goto done;
1931 break;
1932 case OPT_DSGETDCNAME:
1933 if (!wbinfo_dsgetdcname(string_arg, 0)) {
1934 goto done;
1936 break;
1937 case OPT_SEPARATOR: {
1938 const char sep = winbind_separator_int(true);
1939 if ( !sep ) {
1940 goto done;
1942 d_printf("%c\n", sep);
1943 break;
1945 case OPT_LIST_ALL_DOMAINS:
1946 if (!wbinfo_list_domains(true, verbose)) {
1947 goto done;
1949 break;
1950 case OPT_LIST_OWN_DOMAIN:
1951 if (!wbinfo_list_own_domain()) {
1952 goto done;
1954 break;
1955 case OPT_CHANGE_USER_PASSWORD:
1956 if (!wbinfo_change_user_password(string_arg)) {
1957 d_fprintf(stderr, "Could not change user password "
1958 "for user %s\n", string_arg);
1959 goto done;
1961 break;
1963 /* generic configuration options */
1964 case OPT_DOMAIN_NAME:
1965 break;
1966 case OPT_VERBOSE:
1967 break;
1968 default:
1969 d_fprintf(stderr, "Invalid option\n");
1970 poptPrintHelp(pc, stderr, 0);
1971 goto done;
1975 result = 0;
1977 /* Exit code */
1979 done:
1980 talloc_destroy(frame);
1982 poptFreeContext(pc);
1983 return result;