s3-rpc_client: fix non initialized structure in rpccli_lsa_lookup_sids_noalloc.
[Samba/aatanasov.git] / nsswitch / wbinfo.c
bloba80b69f2b6bcb05e529e82f04e5c0355c3898745
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett 2002-2007
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"
26 #include "lib/popt/popt.h"
27 #include "../libcli/auth/libcli_auth.h"
28 #if !(_SAMBA_VERSION_) < 4
29 #include "lib/cmdline/popt_common.h"
30 #endif
32 #ifdef DBGC_CLASS
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_WINBIND
35 #endif
37 static struct wbcInterfaceDetails *init_interface_details(void)
39 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
40 static struct wbcInterfaceDetails *details;
42 if (details) {
43 return details;
46 wbc_status = wbcInterfaceDetails(&details);
47 if (!WBC_ERROR_IS_OK(wbc_status)) {
48 d_fprintf(stderr, "could not obtain winbind interface "
49 "details!\n");
52 return details;
55 static char winbind_separator(void)
57 struct wbcInterfaceDetails *details;
58 static bool got_sep;
59 static char sep;
61 if (got_sep)
62 return sep;
64 details = init_interface_details();
66 if (!details) {
67 d_fprintf(stderr, "could not obtain winbind separator!\n");
68 return 0;
71 sep = details->winbind_separator;
72 got_sep = true;
74 if (!sep) {
75 d_fprintf(stderr, "winbind separator was NULL!\n");
76 return 0;
79 return sep;
82 static const char *get_winbind_domain(void)
84 static struct wbcInterfaceDetails *details;
86 details = init_interface_details();
88 if (!details) {
89 d_fprintf(stderr, "could not obtain winbind domain name!\n");
90 return 0;
93 return details->netbios_domain;
96 static const char *get_winbind_netbios_name(void)
98 static struct wbcInterfaceDetails *details;
100 details = init_interface_details();
102 if (!details) {
103 d_fprintf(stderr, "could not obtain winbind netbios name!\n");
104 return 0;
107 return details->netbios_name;
110 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
111 form DOMAIN/user into a domain and a user */
113 static bool parse_wbinfo_domain_user(const char *domuser, fstring domain,
114 fstring user)
117 char *p = strchr(domuser,winbind_separator());
119 if (!p) {
120 /* Maybe it was a UPN? */
121 if ((p = strchr(domuser, '@')) != NULL) {
122 fstrcpy(domain, "");
123 fstrcpy(user, domuser);
124 return true;
127 fstrcpy(user, domuser);
128 fstrcpy(domain, get_winbind_domain());
129 return true;
132 fstrcpy(user, p+1);
133 fstrcpy(domain, domuser);
134 domain[PTR_DIFF(p, domuser)] = 0;
135 strupper_m(domain);
137 return true;
140 /* Parse string of "uid,sid" or "gid,sid" into separate int and string values.
141 * Return true if input was valid, false otherwise. */
142 static bool parse_mapping_arg(char *arg, int *id, char **sid)
144 char *tmp, *endptr;
146 if (!arg || !*arg)
147 return false;
149 tmp = strtok(arg, ",");
150 *sid = strtok(NULL, ",");
152 if (!tmp || !*tmp || !*sid || !**sid)
153 return false;
155 /* Because atoi() can return 0 on invalid input, which would be a valid
156 * UID/GID we must use strtoul() and do error checking */
157 *id = strtoul(tmp, &endptr, 10);
159 if (endptr[0] != '\0')
160 return false;
162 return true;
165 /* pull pwent info for a given user */
167 static bool wbinfo_get_userinfo(char *user)
169 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
170 struct passwd *pwd = NULL;
172 wbc_status = wbcGetpwnam(user, &pwd);
173 if (!WBC_ERROR_IS_OK(wbc_status)) {
174 return false;
177 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
178 pwd->pw_name,
179 pwd->pw_passwd,
180 (unsigned int)pwd->pw_uid,
181 (unsigned int)pwd->pw_gid,
182 pwd->pw_gecos,
183 pwd->pw_dir,
184 pwd->pw_shell);
186 return true;
189 /* pull pwent info for a given uid */
190 static bool wbinfo_get_uidinfo(int uid)
192 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
193 struct passwd *pwd = NULL;
195 wbc_status = wbcGetpwuid(uid, &pwd);
196 if (!WBC_ERROR_IS_OK(wbc_status)) {
197 return false;
200 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
201 pwd->pw_name,
202 pwd->pw_passwd,
203 (unsigned int)pwd->pw_uid,
204 (unsigned int)pwd->pw_gid,
205 pwd->pw_gecos,
206 pwd->pw_dir,
207 pwd->pw_shell);
209 return true;
212 static bool wbinfo_get_user_sidinfo(const char *sid_str)
214 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
215 struct passwd *pwd = NULL;
216 struct wbcDomainSid sid;
218 wbc_status = wbcStringToSid(sid_str, &sid);
219 wbc_status = wbcGetpwsid(&sid, &pwd);
220 if (!WBC_ERROR_IS_OK(wbc_status)) {
221 return false;
224 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
225 pwd->pw_name,
226 pwd->pw_passwd,
227 (unsigned int)pwd->pw_uid,
228 (unsigned int)pwd->pw_gid,
229 pwd->pw_gecos,
230 pwd->pw_dir,
231 pwd->pw_shell);
233 return true;
237 /* pull grent for a given group */
238 static bool wbinfo_get_groupinfo(const char *group)
240 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
241 struct group *grp;
242 char **mem;
244 wbc_status = wbcGetgrnam(group, &grp);
245 if (!WBC_ERROR_IS_OK(wbc_status)) {
246 return false;
249 d_printf("%s:%s:%u:",
250 grp->gr_name,
251 grp->gr_passwd,
252 (unsigned int)grp->gr_gid);
254 mem = grp->gr_mem;
255 while (*mem != NULL) {
256 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
257 mem += 1;
259 d_printf("\n");
261 wbcFreeMemory(grp);
263 return true;
266 /* pull grent for a given gid */
267 static bool wbinfo_get_gidinfo(int gid)
269 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
270 struct group *grp;
271 char **mem;
273 wbc_status = wbcGetgrgid(gid, &grp);
274 if (!WBC_ERROR_IS_OK(wbc_status)) {
275 return false;
278 d_printf("%s:%s:%u:",
279 grp->gr_name,
280 grp->gr_passwd,
281 (unsigned int)grp->gr_gid);
283 mem = grp->gr_mem;
284 while (*mem != NULL) {
285 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
286 mem += 1;
288 d_printf("\n");
290 wbcFreeMemory(grp);
292 return true;
295 /* List groups a user is a member of */
297 static bool wbinfo_get_usergroups(const char *user)
299 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
300 uint32_t num_groups;
301 uint32_t i;
302 gid_t *groups = NULL;
304 /* Send request */
306 wbc_status = wbcGetGroups(user, &num_groups, &groups);
307 if (!WBC_ERROR_IS_OK(wbc_status)) {
308 return false;
311 for (i = 0; i < num_groups; i++) {
312 d_printf("%d\n", (int)groups[i]);
315 wbcFreeMemory(groups);
317 return true;
321 /* List group SIDs a user SID is a member of */
322 static bool wbinfo_get_usersids(const char *user_sid_str)
324 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
325 uint32_t num_sids;
326 uint32_t i;
327 struct wbcDomainSid user_sid, *sids = NULL;
329 /* Send request */
331 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
332 if (!WBC_ERROR_IS_OK(wbc_status)) {
333 return false;
336 wbc_status = wbcLookupUserSids(&user_sid, false, &num_sids, &sids);
337 if (!WBC_ERROR_IS_OK(wbc_status)) {
338 return false;
341 for (i = 0; i < num_sids; i++) {
342 char *str = NULL;
343 wbc_status = wbcSidToString(&sids[i], &str);
344 if (!WBC_ERROR_IS_OK(wbc_status)) {
345 wbcFreeMemory(sids);
346 return false;
348 d_printf("%s\n", str);
349 wbcFreeMemory(str);
352 wbcFreeMemory(sids);
354 return true;
357 static bool wbinfo_get_userdomgroups(const char *user_sid_str)
359 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
360 uint32_t num_sids;
361 uint32_t i;
362 struct wbcDomainSid user_sid, *sids = NULL;
364 /* Send request */
366 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
367 if (!WBC_ERROR_IS_OK(wbc_status)) {
368 return false;
371 wbc_status = wbcLookupUserSids(&user_sid, true, &num_sids, &sids);
372 if (!WBC_ERROR_IS_OK(wbc_status)) {
373 return false;
376 for (i = 0; i < num_sids; i++) {
377 char *str = NULL;
378 wbc_status = wbcSidToString(&sids[i], &str);
379 if (!WBC_ERROR_IS_OK(wbc_status)) {
380 wbcFreeMemory(sids);
381 return false;
383 d_printf("%s\n", str);
384 wbcFreeMemory(str);
387 wbcFreeMemory(sids);
389 return true;
392 static bool wbinfo_get_sidaliases(const char *domain,
393 const char *user_sid_str)
395 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
396 struct wbcDomainInfo *dinfo = NULL;
397 uint32_t i;
398 struct wbcDomainSid user_sid;
399 uint32_t *alias_rids = NULL;
400 uint32_t num_alias_rids;
401 char *domain_sid_str = NULL;
403 /* Send request */
404 if ((domain == NULL) || (strequal(domain, ".")) ||
405 (domain[0] == '\0')) {
406 domain = get_winbind_domain();
409 /* Send request */
411 wbc_status = wbcDomainInfo(domain, &dinfo);
412 if (!WBC_ERROR_IS_OK(wbc_status)) {
413 d_printf("wbcDomainInfo(%s) failed: %s\n", domain,
414 wbcErrorString(wbc_status));
415 goto done;
417 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
418 if (!WBC_ERROR_IS_OK(wbc_status)) {
419 goto done;
422 wbc_status = wbcGetSidAliases(&dinfo->sid, &user_sid, 1,
423 &alias_rids, &num_alias_rids);
424 if (!WBC_ERROR_IS_OK(wbc_status)) {
425 goto done;
428 wbc_status = wbcSidToString(&dinfo->sid, &domain_sid_str);
429 if (!WBC_ERROR_IS_OK(wbc_status)) {
430 goto done;
433 for (i = 0; i < num_alias_rids; i++) {
434 d_printf("%s-%d\n", domain_sid_str, alias_rids[i]);
437 wbcFreeMemory(alias_rids);
439 done:
440 if (domain_sid_str) {
441 wbcFreeMemory(domain_sid_str);
443 if (dinfo) {
444 wbcFreeMemory(dinfo);
446 return (WBC_ERR_SUCCESS == wbc_status);
450 /* Convert NetBIOS name to IP */
452 static bool wbinfo_wins_byname(const char *name)
454 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
455 char *ip = NULL;
457 wbc_status = wbcResolveWinsByName(name, &ip);
458 if (!WBC_ERROR_IS_OK(wbc_status)) {
459 return false;
462 /* Display response */
464 d_printf("%s\n", ip);
466 wbcFreeMemory(ip);
468 return true;
471 /* Convert IP to NetBIOS name */
473 static bool wbinfo_wins_byip(const char *ip)
475 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
476 char *name = NULL;
478 wbc_status = wbcResolveWinsByIP(ip, &name);
479 if (!WBC_ERROR_IS_OK(wbc_status)) {
480 return false;
483 /* Display response */
485 d_printf("%s\n", name);
487 wbcFreeMemory(name);
489 return true;
492 /* List all/trusted domains */
494 static bool wbinfo_list_domains(bool list_all_domains, bool verbose)
496 struct wbcDomainInfo *domain_list = NULL;
497 size_t num_domains;
498 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
499 bool print_all = !list_all_domains && verbose;
500 int i;
502 wbc_status = wbcListTrusts(&domain_list, &num_domains);
503 if (!WBC_ERROR_IS_OK(wbc_status)) {
504 return false;
507 if (print_all) {
508 d_printf("%-16s%-24s%-12s%-12s%-5s%-5s\n",
509 "Domain Name", "DNS Domain", "Trust Type",
510 "Transitive", "In", "Out");
513 for (i=0; i<num_domains; i++) {
514 if (print_all) {
515 d_printf("%-16s", domain_list[i].short_name);
516 } else {
517 d_printf("%s", domain_list[i].short_name);
518 d_printf("\n");
519 continue;
522 d_printf("%-24s", domain_list[i].dns_name);
524 switch(domain_list[i].trust_type) {
525 case WBC_DOMINFO_TRUSTTYPE_NONE:
526 d_printf("None ");
527 break;
528 case WBC_DOMINFO_TRUSTTYPE_FOREST:
529 d_printf("Forest ");
530 break;
531 case WBC_DOMINFO_TRUSTTYPE_EXTERNAL:
532 d_printf("External ");
533 break;
534 case WBC_DOMINFO_TRUSTTYPE_IN_FOREST:
535 d_printf("In-Forest ");
536 break;
539 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_TRANSITIVE) {
540 d_printf("Yes ");
541 } else {
542 d_printf("No ");
545 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_INCOMING) {
546 d_printf("Yes ");
547 } else {
548 d_printf("No ");
551 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_OUTGOING) {
552 d_printf("Yes ");
553 } else {
554 d_printf("No ");
557 d_printf("\n");
560 return true;
563 /* List own domain */
565 static bool wbinfo_list_own_domain(void)
567 d_printf("%s\n", get_winbind_domain());
569 return true;
572 /* show sequence numbers */
573 static bool wbinfo_show_sequence(const char *domain)
575 d_printf("This command has been deprecated. Please use the "
576 "--online-status option instead.\n");
577 return false;
580 /* show sequence numbers */
581 static bool wbinfo_show_onlinestatus(const char *domain)
583 struct wbcDomainInfo *domain_list = NULL;
584 size_t num_domains;
585 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
586 int i;
588 wbc_status = wbcListTrusts(&domain_list, &num_domains);
589 if (!WBC_ERROR_IS_OK(wbc_status)) {
590 return false;
593 for (i=0; i<num_domains; i++) {
594 bool is_offline;
596 if (domain) {
597 if (!strequal(domain_list[i].short_name, domain)) {
598 continue;
602 is_offline = (domain_list[i].domain_flags &
603 WBC_DOMINFO_DOMAIN_OFFLINE);
605 d_printf("%s : %s\n",
606 domain_list[i].short_name,
607 is_offline ? "offline" : "online" );
610 return true;
614 /* Show domain info */
616 static bool wbinfo_domain_info(const char *domain)
618 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
619 struct wbcDomainInfo *dinfo = NULL;
620 char *sid_str = NULL;
622 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
623 domain = get_winbind_domain();
626 /* Send request */
628 wbc_status = wbcDomainInfo(domain, &dinfo);
629 if (!WBC_ERROR_IS_OK(wbc_status)) {
630 return false;
633 wbc_status = wbcSidToString(&dinfo->sid, &sid_str);
634 if (!WBC_ERROR_IS_OK(wbc_status)) {
635 wbcFreeMemory(dinfo);
636 return false;
639 /* Display response */
641 d_printf("Name : %s\n", dinfo->short_name);
642 d_printf("Alt_Name : %s\n", dinfo->dns_name);
644 d_printf("SID : %s\n", sid_str);
646 d_printf("Active Directory : %s\n",
647 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_AD) ? "Yes" : "No");
648 d_printf("Native : %s\n",
649 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_NATIVE) ?
650 "Yes" : "No");
652 d_printf("Primary : %s\n",
653 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_PRIMARY) ?
654 "Yes" : "No");
656 wbcFreeMemory(sid_str);
657 wbcFreeMemory(dinfo);
659 return true;
662 /* Get a foreign DC's name */
663 static bool wbinfo_getdcname(const char *domain_name)
665 struct winbindd_request request;
666 struct winbindd_response response;
668 ZERO_STRUCT(request);
669 ZERO_STRUCT(response);
671 fstrcpy(request.domain_name, domain_name);
673 /* Send request */
675 if (winbindd_request_response(WINBINDD_GETDCNAME, &request,
676 &response) != NSS_STATUS_SUCCESS) {
677 d_fprintf(stderr, "Could not get dc name for %s\n",domain_name);
678 return false;
681 /* Display response */
683 d_printf("%s\n", response.data.dc_name);
685 return true;
688 /* Find a DC */
689 static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
691 struct winbindd_request request;
692 struct winbindd_response response;
694 ZERO_STRUCT(request);
695 ZERO_STRUCT(response);
697 fstrcpy(request.data.dsgetdcname.domain_name, domain_name);
698 request.data.dsgetdcname.flags = flags;
700 request.flags |= DS_DIRECTORY_SERVICE_REQUIRED;
702 /* Send request */
704 if (winbindd_request_response(WINBINDD_DSGETDCNAME, &request,
705 &response) != NSS_STATUS_SUCCESS) {
706 d_fprintf(stderr, "Could not find dc for %s\n", domain_name);
707 return false;
710 /* Display response */
712 d_printf("%s\n", response.data.dsgetdcname.dc_unc);
713 d_printf("%s\n", response.data.dsgetdcname.dc_address);
714 d_printf("%d\n", response.data.dsgetdcname.dc_address_type);
715 d_printf("%s\n", response.data.dsgetdcname.domain_guid);
716 d_printf("%s\n", response.data.dsgetdcname.domain_name);
717 d_printf("%s\n", response.data.dsgetdcname.forest_name);
718 d_printf("0x%08x\n", response.data.dsgetdcname.dc_flags);
719 d_printf("%s\n", response.data.dsgetdcname.dc_site_name);
720 d_printf("%s\n", response.data.dsgetdcname.client_site_name);
722 return true;
725 /* Check trust account password */
727 static bool wbinfo_check_secret(void)
729 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
730 struct wbcAuthErrorInfo *error = NULL;
732 wbc_status = wbcCheckTrustCredentials(NULL, &error);
734 d_printf("checking the trust secret via RPC calls %s\n",
735 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
737 if (wbc_status == WBC_ERR_AUTH_ERROR) {
738 d_fprintf(stderr, "error code was %s (0x%x)\n",
739 error->nt_string, error->nt_status);
740 wbcFreeMemory(error);
742 if (!WBC_ERROR_IS_OK(wbc_status)) {
743 return false;
746 return true;
749 /* Convert uid to sid */
751 static bool wbinfo_uid_to_sid(uid_t uid)
753 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
754 struct wbcDomainSid sid;
755 char *sid_str = NULL;
757 /* Send request */
759 wbc_status = wbcUidToSid(uid, &sid);
760 if (!WBC_ERROR_IS_OK(wbc_status)) {
761 return false;
764 wbc_status = wbcSidToString(&sid, &sid_str);
765 if (!WBC_ERROR_IS_OK(wbc_status)) {
766 return false;
769 /* Display response */
771 d_printf("%s\n", sid_str);
773 wbcFreeMemory(sid_str);
775 return true;
778 /* Convert gid to sid */
780 static bool wbinfo_gid_to_sid(gid_t gid)
782 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
783 struct wbcDomainSid sid;
784 char *sid_str = NULL;
786 /* Send request */
788 wbc_status = wbcGidToSid(gid, &sid);
789 if (!WBC_ERROR_IS_OK(wbc_status)) {
790 return false;
793 wbc_status = wbcSidToString(&sid, &sid_str);
794 if (!WBC_ERROR_IS_OK(wbc_status)) {
795 return false;
798 /* Display response */
800 d_printf("%s\n", sid_str);
802 wbcFreeMemory(sid_str);
804 return true;
807 /* Convert sid to uid */
809 static bool wbinfo_sid_to_uid(const char *sid_str)
811 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
812 struct wbcDomainSid sid;
813 uid_t uid;
815 /* Send request */
817 wbc_status = wbcStringToSid(sid_str, &sid);
818 if (!WBC_ERROR_IS_OK(wbc_status)) {
819 return false;
822 wbc_status = wbcSidToUid(&sid, &uid);
823 if (!WBC_ERROR_IS_OK(wbc_status)) {
824 return false;
827 /* Display response */
829 d_printf("%d\n", (int)uid);
831 return true;
834 static bool wbinfo_sid_to_gid(const char *sid_str)
836 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
837 struct wbcDomainSid sid;
838 gid_t gid;
840 /* Send request */
842 wbc_status = wbcStringToSid(sid_str, &sid);
843 if (!WBC_ERROR_IS_OK(wbc_status)) {
844 return false;
847 wbc_status = wbcSidToGid(&sid, &gid);
848 if (!WBC_ERROR_IS_OK(wbc_status)) {
849 return false;
852 /* Display response */
854 d_printf("%d\n", (int)gid);
856 return true;
859 static bool wbinfo_allocate_uid(void)
861 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
862 uid_t uid;
864 /* Send request */
866 wbc_status = wbcAllocateUid(&uid);
867 if (!WBC_ERROR_IS_OK(wbc_status)) {
868 return false;
871 /* Display response */
873 d_printf("New uid: %u\n", (unsigned int)uid);
875 return true;
878 static bool wbinfo_allocate_gid(void)
880 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
881 gid_t gid;
883 /* Send request */
885 wbc_status = wbcAllocateGid(&gid);
886 if (!WBC_ERROR_IS_OK(wbc_status)) {
887 return false;
890 /* Display response */
892 d_printf("New gid: %u\n", (unsigned int)gid);
894 return true;
897 static bool wbinfo_set_uid_mapping(uid_t uid, const char *sid_str)
899 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
900 struct wbcDomainSid sid;
902 /* Send request */
904 wbc_status = wbcStringToSid(sid_str, &sid);
905 if (!WBC_ERROR_IS_OK(wbc_status)) {
906 return false;
909 wbc_status = wbcSetUidMapping(uid, &sid);
910 if (!WBC_ERROR_IS_OK(wbc_status)) {
911 return false;
914 /* Display response */
916 d_printf("uid %u now mapped to sid %s\n",
917 (unsigned int)uid, sid_str);
919 return true;
922 static bool wbinfo_set_gid_mapping(gid_t gid, const char *sid_str)
924 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
925 struct wbcDomainSid sid;
927 /* Send request */
929 wbc_status = wbcStringToSid(sid_str, &sid);
930 if (!WBC_ERROR_IS_OK(wbc_status)) {
931 return false;
934 wbc_status = wbcSetGidMapping(gid, &sid);
935 if (!WBC_ERROR_IS_OK(wbc_status)) {
936 return false;
939 /* Display response */
941 d_printf("gid %u now mapped to sid %s\n",
942 (unsigned int)gid, sid_str);
944 return true;
947 static bool wbinfo_remove_uid_mapping(uid_t uid, const char *sid_str)
949 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
950 struct wbcDomainSid sid;
952 /* Send request */
954 wbc_status = wbcStringToSid(sid_str, &sid);
955 if (!WBC_ERROR_IS_OK(wbc_status)) {
956 return false;
959 wbc_status = wbcRemoveUidMapping(uid, &sid);
960 if (!WBC_ERROR_IS_OK(wbc_status)) {
961 return false;
964 /* Display response */
966 d_printf("Removed uid %u to sid %s mapping\n",
967 (unsigned int)uid, sid_str);
969 return true;
972 static bool wbinfo_remove_gid_mapping(gid_t gid, const char *sid_str)
974 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
975 struct wbcDomainSid sid;
977 /* Send request */
979 wbc_status = wbcStringToSid(sid_str, &sid);
980 if (!WBC_ERROR_IS_OK(wbc_status)) {
981 return false;
984 wbc_status = wbcRemoveGidMapping(gid, &sid);
985 if (!WBC_ERROR_IS_OK(wbc_status)) {
986 return false;
989 /* Display response */
991 d_printf("Removed gid %u to sid %s mapping\n",
992 (unsigned int)gid, sid_str);
994 return true;
997 /* Convert sid to string */
999 static bool wbinfo_lookupsid(const char *sid_str)
1001 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1002 struct wbcDomainSid sid;
1003 char *domain;
1004 char *name;
1005 enum wbcSidType type;
1007 /* Send off request */
1009 wbc_status = wbcStringToSid(sid_str, &sid);
1010 if (!WBC_ERROR_IS_OK(wbc_status)) {
1011 return false;
1014 wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
1015 if (!WBC_ERROR_IS_OK(wbc_status)) {
1016 return false;
1019 /* Display response */
1021 d_printf("%s%c%s %d\n",
1022 domain, winbind_separator(), name, type);
1024 return true;
1027 /* Convert sid to fullname */
1029 static bool wbinfo_lookupsid_fullname(const char *sid_str)
1031 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1032 struct wbcDomainSid sid;
1033 char *domain;
1034 char *name;
1035 enum wbcSidType type;
1037 /* Send off request */
1039 wbc_status = wbcStringToSid(sid_str, &sid);
1040 if (!WBC_ERROR_IS_OK(wbc_status)) {
1041 return false;
1044 wbc_status = wbcGetDisplayName(&sid, &domain, &name, &type);
1045 if (!WBC_ERROR_IS_OK(wbc_status)) {
1046 return false;
1049 /* Display response */
1051 d_printf("%s%c%s %d\n",
1052 domain, winbind_separator(), name, type);
1054 return true;
1057 /* Lookup a list of RIDs */
1059 static bool wbinfo_lookuprids(const char *domain, const char *arg)
1061 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1062 struct wbcDomainInfo *dinfo = NULL;
1063 char *domain_name = NULL;
1064 const char **names = NULL;
1065 enum wbcSidType *types = NULL;
1066 size_t i;
1067 int num_rids;
1068 uint32_t *rids = NULL;
1069 const char *p;
1070 char *ridstr;
1071 TALLOC_CTX *mem_ctx = NULL;
1072 bool ret = false;
1074 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
1075 domain = get_winbind_domain();
1078 /* Send request */
1080 wbc_status = wbcDomainInfo(domain, &dinfo);
1081 if (!WBC_ERROR_IS_OK(wbc_status)) {
1082 d_printf("wbcDomainInfo(%s) failed: %s\n", domain,
1083 wbcErrorString(wbc_status));
1084 goto done;
1087 mem_ctx = talloc_new(NULL);
1088 if (mem_ctx == NULL) {
1089 d_printf("talloc_new failed\n");
1090 goto done;
1093 num_rids = 0;
1094 rids = NULL;
1095 p = arg;
1097 while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
1098 uint32_t rid = strtoul(ridstr, NULL, 10);
1099 rids = talloc_realloc(mem_ctx, rids, uint32_t, num_rids + 1);
1100 if (rids == NULL) {
1101 d_printf("talloc_realloc failed\n");
1103 rids[num_rids] = rid;
1104 num_rids += 1;
1107 if (rids == NULL) {
1108 d_printf("no rids\n");
1109 goto done;
1112 wbc_status = wbcLookupRids(&dinfo->sid, num_rids, rids,
1113 (const char **)&domain_name, &names, &types);
1114 if (!WBC_ERROR_IS_OK(wbc_status)) {
1115 d_printf("winbind_lookup_rids failed: %s\n",
1116 wbcErrorString(wbc_status));
1117 goto done;
1120 d_printf("Domain: %s\n", domain_name);
1122 for (i=0; i<num_rids; i++) {
1123 d_printf("%8d: %s (%s)\n", rids[i], names[i],
1124 wbcSidTypeString(types[i]));
1127 ret = true;
1128 done:
1129 if (dinfo) {
1130 wbcFreeMemory(dinfo);
1132 if (domain_name) {
1133 wbcFreeMemory(domain_name);
1135 if (names) {
1136 wbcFreeMemory(names);
1138 if (types) {
1139 wbcFreeMemory(types);
1141 TALLOC_FREE(mem_ctx);
1142 return ret;
1145 /* Convert string to sid */
1147 static bool wbinfo_lookupname(const char *full_name)
1149 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1150 struct wbcDomainSid sid;
1151 char *sid_str;
1152 enum wbcSidType type;
1153 fstring domain_name;
1154 fstring account_name;
1156 /* Send off request */
1158 parse_wbinfo_domain_user(full_name, domain_name,
1159 account_name);
1161 wbc_status = wbcLookupName(domain_name, account_name,
1162 &sid, &type);
1163 if (!WBC_ERROR_IS_OK(wbc_status)) {
1164 return false;
1167 wbc_status = wbcSidToString(&sid, &sid_str);
1168 if (!WBC_ERROR_IS_OK(wbc_status)) {
1169 return false;
1172 /* Display response */
1174 d_printf("%s %s (%d)\n", sid_str, wbcSidTypeString(type), type);
1176 wbcFreeMemory(sid_str);
1178 return true;
1181 static char *wbinfo_prompt_pass(TALLOC_CTX *mem_ctx,
1182 const char *prefix,
1183 const char *username)
1185 char *prompt;
1186 const char *ret = NULL;
1188 prompt = talloc_asprintf(mem_ctx, "Enter %s's ", username);
1189 if (!prompt) {
1190 return NULL;
1192 if (prefix) {
1193 prompt = talloc_asprintf_append(prompt, "%s ", prefix);
1194 if (!prompt) {
1195 return NULL;
1198 prompt = talloc_asprintf_append(prompt, "password: ");
1199 if (!prompt) {
1200 return NULL;
1203 ret = getpass(prompt);
1204 TALLOC_FREE(prompt);
1206 return talloc_strdup(mem_ctx, ret);
1209 /* Authenticate a user with a plaintext password */
1211 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32_t flags)
1213 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1214 char *s = NULL;
1215 char *p = NULL;
1216 char *password = NULL;
1217 char *name = NULL;
1218 char *local_cctype = NULL;
1219 uid_t uid;
1220 struct wbcLogonUserParams params;
1221 struct wbcLogonUserInfo *info;
1222 struct wbcAuthErrorInfo *error;
1223 struct wbcUserPasswordPolicyInfo *policy;
1224 TALLOC_CTX *frame = talloc_tos();
1226 if ((s = talloc_strdup(frame, username)) == NULL) {
1227 return false;
1230 if ((p = strchr(s, '%')) != NULL) {
1231 *p = 0;
1232 p++;
1233 password = talloc_strdup(frame, p);
1234 } else {
1235 password = wbinfo_prompt_pass(frame, NULL, username);
1238 local_cctype = talloc_strdup(frame, cctype);
1240 name = s;
1242 uid = geteuid();
1244 params.username = name;
1245 params.password = password;
1246 params.num_blobs = 0;
1247 params.blobs = NULL;
1249 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1250 &params.blobs,
1251 "flags",
1253 (uint8_t *)&flags,
1254 sizeof(flags));
1255 if (!WBC_ERROR_IS_OK(wbc_status)) {
1256 goto done;
1259 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1260 &params.blobs,
1261 "user_uid",
1263 (uint8_t *)&uid,
1264 sizeof(uid));
1265 if (!WBC_ERROR_IS_OK(wbc_status)) {
1266 goto done;
1269 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1270 &params.blobs,
1271 "krb5_cc_type",
1273 (uint8_t *)local_cctype,
1274 strlen(cctype)+1);
1275 if (!WBC_ERROR_IS_OK(wbc_status)) {
1276 goto done;
1279 wbc_status = wbcLogonUser(&params, &info, &error, &policy);
1281 d_printf("plaintext kerberos password authentication for [%s] %s "
1282 "(requesting cctype: %s)\n",
1283 username, WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed",
1284 cctype);
1286 if (error) {
1287 d_fprintf(stderr,
1288 "error code was %s (0x%x)\nerror messsage was: %s\n",
1289 error->nt_string,
1290 error->nt_status,
1291 error->display_string);
1294 if (WBC_ERROR_IS_OK(wbc_status)) {
1295 if (flags & WBFLAG_PAM_INFO3_TEXT) {
1296 if (info && info->info && info->info->user_flags &
1297 NETLOGON_CACHED_ACCOUNT) {
1298 d_printf("user_flgs: "
1299 "NETLOGON_CACHED_ACCOUNT\n");
1303 if (info) {
1304 int i;
1305 for (i=0; i < info->num_blobs; i++) {
1306 if (strequal(info->blobs[i].name,
1307 "krb5ccname")) {
1308 d_printf("credentials were put "
1309 "in: %s\n",
1310 (const char *)
1311 info->blobs[i].blob.data);
1312 break;
1315 } else {
1316 d_printf("no credentials cached\n");
1319 done:
1321 wbcFreeMemory(params.blobs);
1323 return WBC_ERROR_IS_OK(wbc_status);
1326 /* Authenticate a user with a plaintext password */
1328 static bool wbinfo_auth(char *username)
1330 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1331 char *s = NULL;
1332 char *p = NULL;
1333 char *password = NULL;
1334 char *name = NULL;
1335 TALLOC_CTX *frame = talloc_tos();
1337 if ((s = talloc_strdup(frame, username)) == NULL) {
1338 return false;
1341 if ((p = strchr(s, '%')) != NULL) {
1342 *p = 0;
1343 p++;
1344 password = talloc_strdup(frame, p);
1345 } else {
1346 password = wbinfo_prompt_pass(frame, NULL, username);
1349 name = s;
1351 wbc_status = wbcAuthenticateUser(name, password);
1353 d_printf("plaintext password authentication %s\n",
1354 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1356 #if 0
1357 if (response.data.auth.nt_status)
1358 d_fprintf(stderr,
1359 "error code was %s (0x%x)\nerror messsage was: %s\n",
1360 response.data.auth.nt_status_string,
1361 response.data.auth.nt_status,
1362 response.data.auth.error_string);
1363 #endif
1365 return WBC_ERROR_IS_OK(wbc_status);
1368 /* Authenticate a user with a challenge/response */
1370 static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
1372 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1373 struct wbcAuthUserParams params;
1374 struct wbcAuthUserInfo *info = NULL;
1375 struct wbcAuthErrorInfo *err = NULL;
1376 DATA_BLOB lm = data_blob_null;
1377 DATA_BLOB nt = data_blob_null;
1378 fstring name_user;
1379 fstring name_domain;
1380 char *pass;
1381 char *p;
1382 TALLOC_CTX *frame = talloc_tos();
1384 p = strchr(username, '%');
1386 if (p) {
1387 *p = 0;
1388 pass = talloc_strdup(frame, p + 1);
1389 } else {
1390 pass = wbinfo_prompt_pass(frame, NULL, username);
1393 parse_wbinfo_domain_user(username, name_domain, name_user);
1395 params.account_name = name_user;
1396 params.domain_name = name_domain;
1397 params.workstation_name = NULL;
1399 params.flags = 0;
1400 params.parameter_control= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
1401 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
1403 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
1405 generate_random_buffer(params.password.response.challenge, 8);
1407 if (use_ntlmv2) {
1408 DATA_BLOB server_chal;
1409 DATA_BLOB names_blob;
1411 server_chal = data_blob(params.password.response.challenge, 8);
1413 /* Pretend this is a login to 'us', for blob purposes */
1414 names_blob = NTLMv2_generate_names_blob(NULL,
1415 get_winbind_netbios_name(),
1416 get_winbind_domain());
1418 if (!SMBNTLMv2encrypt(NULL, name_user, name_domain, pass,
1419 &server_chal,
1420 &names_blob,
1421 &lm, &nt, NULL, NULL)) {
1422 data_blob_free(&names_blob);
1423 data_blob_free(&server_chal);
1424 TALLOC_FREE(pass);
1425 return false;
1427 data_blob_free(&names_blob);
1428 data_blob_free(&server_chal);
1430 } else {
1431 if (use_lanman) {
1432 bool ok;
1433 lm = data_blob(NULL, 24);
1434 ok = SMBencrypt(pass,
1435 params.password.response.challenge,
1436 lm.data);
1437 if (!ok) {
1438 data_blob_free(&lm);
1441 nt = data_blob(NULL, 24);
1442 SMBNTencrypt(pass, params.password.response.challenge,
1443 nt.data);
1446 params.password.response.nt_length = nt.length;
1447 params.password.response.nt_data = nt.data;
1448 params.password.response.lm_length = lm.length;
1449 params.password.response.lm_data = lm.data;
1451 wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
1453 /* Display response */
1455 d_printf("challenge/response password authentication %s\n",
1456 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1458 if (wbc_status == WBC_ERR_AUTH_ERROR) {
1459 d_fprintf(stderr,
1460 "error code was %s (0x%x)\nerror messsage was: %s\n",
1461 err->nt_string,
1462 err->nt_status,
1463 err->display_string);
1464 wbcFreeMemory(err);
1465 } else if (WBC_ERROR_IS_OK(wbc_status)) {
1466 wbcFreeMemory(info);
1469 data_blob_free(&nt);
1470 data_blob_free(&lm);
1472 return WBC_ERROR_IS_OK(wbc_status);
1475 #ifdef WITH_FAKE_KASERVER
1476 /* Authenticate a user with a plaintext password and set a token */
1478 static bool wbinfo_klog(char *username)
1480 struct winbindd_request request;
1481 struct winbindd_response response;
1482 NSS_STATUS result;
1483 char *p;
1485 /* Send off request */
1487 ZERO_STRUCT(request);
1488 ZERO_STRUCT(response);
1490 p = strchr(username, '%');
1492 if (p) {
1493 *p = 0;
1494 fstrcpy(request.data.auth.user, username);
1495 fstrcpy(request.data.auth.pass, p + 1);
1496 *p = '%';
1497 } else {
1498 fstrcpy(request.data.auth.user, username);
1499 fstrcpy(request.data.auth.pass, getpass("Password: "));
1502 request.flags |= WBFLAG_PAM_AFS_TOKEN;
1504 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request,
1505 &response);
1507 /* Display response */
1509 d_printf("plaintext password authentication %s\n",
1510 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1512 if (response.data.auth.nt_status)
1513 d_fprintf(stderr,
1514 "error code was %s (0x%x)\nerror messsage was: %s\n",
1515 response.data.auth.nt_status_string,
1516 response.data.auth.nt_status,
1517 response.data.auth.error_string);
1519 if (result != NSS_STATUS_SUCCESS)
1520 return false;
1522 if (response.extra_data.data == NULL) {
1523 d_fprintf(stderr, "Did not get token data\n");
1524 return false;
1527 if (!afs_settoken_str((char *)response.extra_data.data)) {
1528 d_fprintf(stderr, "Could not set token\n");
1529 return false;
1532 d_printf("Successfully created AFS token\n");
1533 return true;
1535 #else
1536 static bool wbinfo_klog(char *username)
1538 d_fprintf(stderr, "No AFS support compiled in.\n");
1539 return false;
1541 #endif
1543 /* Print domain users */
1545 static bool print_domain_users(const char *domain)
1547 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1548 uint32_t i;
1549 uint32_t num_users = 0;
1550 const char **users = NULL;
1552 /* Send request to winbind daemon */
1554 /* '.' is the special sign for our own domain */
1555 if (domain && strcmp(domain, ".") == 0) {
1556 domain = get_winbind_domain();
1559 wbc_status = wbcListUsers(domain, &num_users, &users);
1560 if (!WBC_ERROR_IS_OK(wbc_status)) {
1561 return false;
1564 for (i=0; i < num_users; i++) {
1565 d_printf("%s\n", users[i]);
1568 wbcFreeMemory(users);
1570 return true;
1573 /* Print domain groups */
1575 static bool print_domain_groups(const char *domain)
1577 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1578 uint32_t i;
1579 uint32_t num_groups = 0;
1580 const char **groups = NULL;
1582 /* Send request to winbind daemon */
1584 /* '.' is the special sign for our own domain */
1585 if (domain && strcmp(domain, ".") == 0) {
1586 domain = get_winbind_domain();
1589 wbc_status = wbcListGroups(domain, &num_groups, &groups);
1590 if (!WBC_ERROR_IS_OK(wbc_status)) {
1591 return false;
1594 for (i=0; i < num_groups; i++) {
1595 d_printf("%s\n", groups[i]);
1598 wbcFreeMemory(groups);
1600 return true;
1603 /* Set the authorised user for winbindd access in secrets.tdb */
1605 static bool wbinfo_set_auth_user(char *username)
1607 d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1608 "See 'net help setauthuser' for details.\n");
1609 return false;
1612 static void wbinfo_get_auth_user(void)
1614 d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
1615 "See 'net help getauthuser' for details.\n");
1618 static bool wbinfo_ping(void)
1620 wbcErr wbc_status;
1622 wbc_status = wbcPing();
1624 /* Display response */
1626 d_printf("Ping to winbindd %s\n",
1627 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1629 return WBC_ERROR_IS_OK(wbc_status);
1632 static bool wbinfo_change_user_password(const char *username)
1634 wbcErr wbc_status;
1635 char *old_password = NULL;
1636 char *new_password = NULL;
1637 TALLOC_CTX *frame = talloc_tos();
1639 old_password = wbinfo_prompt_pass(frame, "old", username);
1640 new_password = wbinfo_prompt_pass(frame, "new", username);
1642 wbc_status = wbcChangeUserPassword(username, old_password,new_password);
1644 /* Display response */
1646 d_printf("Password change for user %s %s\n", username,
1647 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1649 return WBC_ERROR_IS_OK(wbc_status);
1652 /* Main program */
1654 enum {
1655 OPT_SET_AUTH_USER = 1000,
1656 OPT_GET_AUTH_USER,
1657 OPT_DOMAIN_NAME,
1658 OPT_SEQUENCE,
1659 OPT_GETDCNAME,
1660 OPT_DSGETDCNAME,
1661 OPT_USERDOMGROUPS,
1662 OPT_SIDALIASES,
1663 OPT_USERSIDS,
1664 OPT_ALLOCATE_UID,
1665 OPT_ALLOCATE_GID,
1666 OPT_SET_UID_MAPPING,
1667 OPT_SET_GID_MAPPING,
1668 OPT_REMOVE_UID_MAPPING,
1669 OPT_REMOVE_GID_MAPPING,
1670 OPT_SEPARATOR,
1671 OPT_LIST_ALL_DOMAINS,
1672 OPT_LIST_OWN_DOMAIN,
1673 OPT_UID_INFO,
1674 OPT_USER_SIDINFO,
1675 OPT_GROUP_INFO,
1676 OPT_GID_INFO,
1677 OPT_VERBOSE,
1678 OPT_ONLINESTATUS,
1679 OPT_CHANGE_USER_PASSWORD,
1680 OPT_SID_TO_FULLNAME,
1681 OPT_NTLMV2,
1682 OPT_LANMAN
1685 int main(int argc, char **argv, char **envp)
1687 int opt;
1688 TALLOC_CTX *frame = talloc_stackframe();
1689 poptContext pc;
1690 static char *string_arg;
1691 char *string_subarg = NULL;
1692 static char *opt_domain_name;
1693 static int int_arg;
1694 int int_subarg = -1;
1695 int result = 1;
1696 bool verbose = false;
1697 bool use_ntlmv2 = false;
1698 bool use_lanman = false;
1700 struct poptOption long_options[] = {
1701 POPT_AUTOHELP
1703 /* longName, shortName, argInfo, argPtr, value, descrip,
1704 argDesc */
1706 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1707 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1708 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1709 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1710 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1711 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1712 { "sid-to-fullname", 0, POPT_ARG_STRING, &string_arg,
1713 OPT_SID_TO_FULLNAME, "Converts sid to fullname", "SID" },
1714 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1715 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1716 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1717 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1718 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1719 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1720 "Get a new UID out of idmap" },
1721 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1722 "Get a new GID out of idmap" },
1723 { "set-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_UID_MAPPING, "Create or modify uid to sid mapping in idmap", "UID,SID" },
1724 { "set-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_SET_GID_MAPPING, "Create or modify gid to sid mapping in idmap", "GID,SID" },
1725 { "remove-uid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_UID_MAPPING, "Remove uid to sid mapping in idmap", "UID,SID" },
1726 { "remove-gid-mapping", 0, POPT_ARG_STRING, &string_arg, OPT_REMOVE_GID_MAPPING, "Remove gid to sid mapping in idmap", "GID,SID" },
1727 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1728 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1729 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1730 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1731 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1732 { "online-status", 0, POPT_ARG_NONE, 0, OPT_ONLINESTATUS, "Show whether domains are marked as online or offline"},
1733 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1734 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1735 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1736 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1737 { "user-sidinfo", 0, POPT_ARG_STRING, &string_arg, OPT_USER_SIDINFO, "Get user info from sid", "SID" },
1738 { "gid-info", 0, POPT_ARG_INT, &int_arg, OPT_GID_INFO, "Get group info from gid", "GID" },
1739 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1740 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1741 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1742 { "sid-aliases", 0, POPT_ARG_STRING, &string_arg, OPT_SIDALIASES, "Get sid aliases", "SID" },
1743 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1744 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1745 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1746 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1747 "Get a DC name for a foreign domain", "domainname" },
1748 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
1749 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1750 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1751 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1752 #ifdef WITH_FAKE_KASERVER
1753 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1754 #endif
1755 #ifdef HAVE_KRB5
1756 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1757 /* destroys wbinfo --help output */
1758 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1759 #endif
1760 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1761 { "verbose", 0, POPT_ARG_NONE, 0, OPT_VERBOSE, "Print additional information per command", NULL },
1762 { "change-user-password", 0, POPT_ARG_STRING, &string_arg, OPT_CHANGE_USER_PASSWORD, "Change the password for a user", NULL },
1763 { "ntlmv2", 0, POPT_ARG_NONE, 0, OPT_NTLMV2, "Use NTLMv2 cryptography for user authentication", NULL},
1764 { "lanman", 0, POPT_ARG_NONE, 0, OPT_LANMAN, "Use lanman cryptography for user authentication", NULL},
1765 POPT_COMMON_VERSION
1766 POPT_TABLEEND
1769 /* Samba client initialisation */
1770 load_case_tables();
1773 /* Parse options */
1775 pc = poptGetContext("wbinfo", argc, (const char **)argv,
1776 long_options, 0);
1778 /* Parse command line options */
1780 if (argc == 1) {
1781 poptPrintHelp(pc, stderr, 0);
1782 return 1;
1785 while((opt = poptGetNextOpt(pc)) != -1) {
1786 /* get the generic configuration parameters like --domain */
1787 switch (opt) {
1788 case OPT_VERBOSE:
1789 verbose = true;
1790 break;
1791 case OPT_NTLMV2:
1792 use_ntlmv2 = true;
1793 break;
1794 case OPT_LANMAN:
1795 use_lanman = true;
1796 break;
1800 poptFreeContext(pc);
1802 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1803 POPT_CONTEXT_KEEP_FIRST);
1805 while((opt = poptGetNextOpt(pc)) != -1) {
1806 switch (opt) {
1807 case 'u':
1808 if (!print_domain_users(opt_domain_name)) {
1809 d_fprintf(stderr,
1810 "Error looking up domain users\n");
1811 goto done;
1813 break;
1814 case 'g':
1815 if (!print_domain_groups(opt_domain_name)) {
1816 d_fprintf(stderr,
1817 "Error looking up domain groups\n");
1818 goto done;
1820 break;
1821 case 's':
1822 if (!wbinfo_lookupsid(string_arg)) {
1823 d_fprintf(stderr,
1824 "Could not lookup sid %s\n",
1825 string_arg);
1826 goto done;
1828 break;
1829 case OPT_SID_TO_FULLNAME:
1830 if (!wbinfo_lookupsid_fullname(string_arg)) {
1831 d_fprintf(stderr, "Could not lookup sid %s\n",
1832 string_arg);
1833 goto done;
1835 break;
1836 case 'R':
1837 if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
1838 d_fprintf(stderr, "Could not lookup RIDs %s\n",
1839 string_arg);
1840 goto done;
1842 break;
1843 case 'n':
1844 if (!wbinfo_lookupname(string_arg)) {
1845 d_fprintf(stderr, "Could not lookup name %s\n",
1846 string_arg);
1847 goto done;
1849 break;
1850 case 'N':
1851 if (!wbinfo_wins_byname(string_arg)) {
1852 d_fprintf(stderr,
1853 "Could not lookup WINS by name %s\n",
1854 string_arg);
1855 goto done;
1857 break;
1858 case 'I':
1859 if (!wbinfo_wins_byip(string_arg)) {
1860 d_fprintf(stderr,
1861 "Could not lookup WINS by IP %s\n",
1862 string_arg);
1863 goto done;
1865 break;
1866 case 'U':
1867 if (!wbinfo_uid_to_sid(int_arg)) {
1868 d_fprintf(stderr,
1869 "Could not convert uid %d to sid\n",
1870 int_arg);
1871 goto done;
1873 break;
1874 case 'G':
1875 if (!wbinfo_gid_to_sid(int_arg)) {
1876 d_fprintf(stderr,
1877 "Could not convert gid %d to sid\n",
1878 int_arg);
1879 goto done;
1881 break;
1882 case 'S':
1883 if (!wbinfo_sid_to_uid(string_arg)) {
1884 d_fprintf(stderr,
1885 "Could not convert sid %s to uid\n",
1886 string_arg);
1887 goto done;
1889 break;
1890 case 'Y':
1891 if (!wbinfo_sid_to_gid(string_arg)) {
1892 d_fprintf(stderr,
1893 "Could not convert sid %s to gid\n",
1894 string_arg);
1895 goto done;
1897 break;
1898 case OPT_ALLOCATE_UID:
1899 if (!wbinfo_allocate_uid()) {
1900 d_fprintf(stderr, "Could not allocate a uid\n");
1901 goto done;
1903 break;
1904 case OPT_ALLOCATE_GID:
1905 if (!wbinfo_allocate_gid()) {
1906 d_fprintf(stderr, "Could not allocate a gid\n");
1907 goto done;
1909 break;
1910 case OPT_SET_UID_MAPPING:
1911 if (!parse_mapping_arg(string_arg, &int_subarg,
1912 &string_subarg) ||
1913 !wbinfo_set_uid_mapping(int_subarg, string_subarg))
1915 d_fprintf(stderr, "Could not create or modify "
1916 "uid to sid mapping\n");
1917 goto done;
1919 break;
1920 case OPT_SET_GID_MAPPING:
1921 if (!parse_mapping_arg(string_arg, &int_subarg,
1922 &string_subarg) ||
1923 !wbinfo_set_gid_mapping(int_subarg, string_subarg))
1925 d_fprintf(stderr, "Could not create or modify "
1926 "gid to sid mapping\n");
1927 goto done;
1929 break;
1930 case OPT_REMOVE_UID_MAPPING:
1931 if (!parse_mapping_arg(string_arg, &int_subarg,
1932 &string_subarg) ||
1933 !wbinfo_remove_uid_mapping(int_subarg,
1934 string_subarg))
1936 d_fprintf(stderr, "Could not remove uid to sid "
1937 "mapping\n");
1938 goto done;
1940 break;
1941 case OPT_REMOVE_GID_MAPPING:
1942 if (!parse_mapping_arg(string_arg, &int_subarg,
1943 &string_subarg) ||
1944 !wbinfo_remove_gid_mapping(int_subarg,
1945 string_subarg))
1947 d_fprintf(stderr, "Could not remove gid to sid "
1948 "mapping\n");
1949 goto done;
1951 break;
1952 case 't':
1953 if (!wbinfo_check_secret()) {
1954 d_fprintf(stderr, "Could not check secret\n");
1955 goto done;
1957 break;
1958 case 'm':
1959 if (!wbinfo_list_domains(false, verbose)) {
1960 d_fprintf(stderr,
1961 "Could not list trusted domains\n");
1962 goto done;
1964 break;
1965 case OPT_SEQUENCE:
1966 if (!wbinfo_show_sequence(opt_domain_name)) {
1967 d_fprintf(stderr,
1968 "Could not show sequence numbers\n");
1969 goto done;
1971 break;
1972 case OPT_ONLINESTATUS:
1973 if (!wbinfo_show_onlinestatus(opt_domain_name)) {
1974 d_fprintf(stderr,
1975 "Could not show online-status\n");
1976 goto done;
1978 break;
1979 case 'D':
1980 if (!wbinfo_domain_info(string_arg)) {
1981 d_fprintf(stderr,
1982 "Could not get domain info\n");
1983 goto done;
1985 break;
1986 case 'i':
1987 if (!wbinfo_get_userinfo(string_arg)) {
1988 d_fprintf(stderr,
1989 "Could not get info for user %s\n",
1990 string_arg);
1991 goto done;
1993 break;
1994 case OPT_USER_SIDINFO:
1995 if ( !wbinfo_get_user_sidinfo(string_arg)) {
1996 d_fprintf(stderr,
1997 "Could not get info for user "
1998 "sid %s\n", string_arg);
1999 goto done;
2001 break;
2002 case OPT_UID_INFO:
2003 if ( !wbinfo_get_uidinfo(int_arg)) {
2004 d_fprintf(stderr, "Could not get info for uid "
2005 "%d\n", int_arg);
2006 goto done;
2008 break;
2009 case OPT_GROUP_INFO:
2010 if ( !wbinfo_get_groupinfo(string_arg)) {
2011 d_fprintf(stderr, "Could not get info for "
2012 "group %s\n", string_arg);
2013 goto done;
2015 break;
2016 case OPT_GID_INFO:
2017 if ( !wbinfo_get_gidinfo(int_arg)) {
2018 d_fprintf(stderr, "Could not get info for gid "
2019 "%d\n", int_arg);
2020 goto done;
2022 break;
2023 case 'r':
2024 if (!wbinfo_get_usergroups(string_arg)) {
2025 d_fprintf(stderr,
2026 "Could not get groups for user %s\n",
2027 string_arg);
2028 goto done;
2030 break;
2031 case OPT_USERSIDS:
2032 if (!wbinfo_get_usersids(string_arg)) {
2033 d_fprintf(stderr, "Could not get group SIDs "
2034 "for user SID %s\n",
2035 string_arg);
2036 goto done;
2038 break;
2039 case OPT_USERDOMGROUPS:
2040 if (!wbinfo_get_userdomgroups(string_arg)) {
2041 d_fprintf(stderr, "Could not get user's domain "
2042 "groups for user SID %s\n",
2043 string_arg);
2044 goto done;
2046 break;
2047 case OPT_SIDALIASES:
2048 if (!wbinfo_get_sidaliases(opt_domain_name,
2049 string_arg)) {
2050 d_fprintf(stderr, "Could not get sid aliases "
2051 "for user SID %s\n", string_arg);
2052 goto done;
2054 break;
2055 case 'a': {
2056 bool got_error = false;
2058 if (!wbinfo_auth(string_arg)) {
2059 d_fprintf(stderr,
2060 "Could not authenticate user "
2061 "%s with plaintext "
2062 "password\n", string_arg);
2063 got_error = true;
2066 if (!wbinfo_auth_crap(string_arg, use_ntlmv2,
2067 use_lanman)) {
2068 d_fprintf(stderr,
2069 "Could not authenticate user "
2070 "%s with challenge/response\n",
2071 string_arg);
2072 got_error = true;
2075 if (got_error)
2076 goto done;
2077 break;
2079 case 'K': {
2080 uint32_t flags = WBFLAG_PAM_KRB5 |
2081 WBFLAG_PAM_CACHED_LOGIN |
2082 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
2083 WBFLAG_PAM_INFO3_TEXT |
2084 WBFLAG_PAM_CONTACT_TRUSTDOM;
2086 if (!wbinfo_auth_krb5(string_arg, "FILE",
2087 flags)) {
2088 d_fprintf(stderr,
2089 "Could not authenticate user "
2090 "[%s] with Kerberos "
2091 "(ccache: %s)\n", string_arg,
2092 "FILE");
2093 goto done;
2095 break;
2097 case 'k':
2098 if (!wbinfo_klog(string_arg)) {
2099 d_fprintf(stderr, "Could not klog user\n");
2100 goto done;
2102 break;
2103 case 'p':
2104 if (!wbinfo_ping()) {
2105 d_fprintf(stderr, "could not ping winbindd!\n");
2106 goto done;
2108 break;
2109 case OPT_SET_AUTH_USER:
2110 if (!wbinfo_set_auth_user(string_arg)) {
2111 goto done;
2113 break;
2114 case OPT_GET_AUTH_USER:
2115 wbinfo_get_auth_user();
2116 goto done;
2117 break;
2118 case OPT_GETDCNAME:
2119 if (!wbinfo_getdcname(string_arg)) {
2120 goto done;
2122 break;
2123 case OPT_DSGETDCNAME:
2124 if (!wbinfo_dsgetdcname(string_arg, 0)) {
2125 goto done;
2127 break;
2128 case OPT_SEPARATOR: {
2129 const char sep = winbind_separator();
2130 if ( !sep ) {
2131 goto done;
2133 d_printf("%c\n", sep);
2134 break;
2136 case OPT_LIST_ALL_DOMAINS:
2137 if (!wbinfo_list_domains(true, verbose)) {
2138 goto done;
2140 break;
2141 case OPT_LIST_OWN_DOMAIN:
2142 if (!wbinfo_list_own_domain()) {
2143 goto done;
2145 break;
2146 case OPT_CHANGE_USER_PASSWORD:
2147 if (!wbinfo_change_user_password(string_arg)) {
2148 d_fprintf(stderr,
2149 "Could not change user password "
2150 "for user %s\n", string_arg);
2151 goto done;
2153 break;
2155 /* generic configuration options */
2156 case OPT_DOMAIN_NAME:
2157 break;
2158 case OPT_VERBOSE:
2159 break;
2160 case OPT_NTLMV2:
2161 break;
2162 case OPT_LANMAN:
2163 break;
2164 default:
2165 d_fprintf(stderr, "Invalid option\n");
2166 poptPrintHelp(pc, stderr, 0);
2167 goto done;
2171 result = 0;
2173 /* Exit code */
2175 done:
2176 talloc_free(frame);
2178 poptFreeContext(pc);
2179 return result;