smbd: Update comment for durable handles
[Samba.git] / nsswitch / wbinfo.c
blob1656f02f0e68e73afb331b625796ed7001480e6c
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett 2002-2007
8 Copyright (C) Volker Lendecke 2009
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "winbind_client.h"
26 #include "libwbclient/wbclient.h"
27 #include "../libcli/auth/libcli_auth.h"
28 #include "lib/cmdline/cmdline.h"
29 #include "lib/afs/afs_settoken.h"
30 #include "lib/util/smb_strtox.h"
31 #include "lib/util/string_wrappers.h"
33 #ifdef DBGC_CLASS
34 #undef DBGC_CLASS
35 #define DBGC_CLASS DBGC_WINBIND
36 #endif
38 static struct wbcInterfaceDetails *init_interface_details(void)
40 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
41 static struct wbcInterfaceDetails *details;
43 if (details) {
44 return details;
47 wbc_status = wbcInterfaceDetails(&details);
48 if (!WBC_ERROR_IS_OK(wbc_status)) {
49 d_fprintf(stderr, "could not obtain winbind interface "
50 "details: %s\n", wbcErrorString(wbc_status));
53 return details;
56 static char winbind_separator(void)
58 struct wbcInterfaceDetails *details;
59 static bool got_sep;
60 static char sep;
62 if (got_sep)
63 return sep;
65 details = init_interface_details();
67 if (!details) {
68 d_fprintf(stderr, "could not obtain winbind separator!\n");
69 return 0;
72 sep = details->winbind_separator;
73 got_sep = true;
75 if (!sep) {
76 d_fprintf(stderr, "winbind separator was NULL!\n");
77 return 0;
80 return sep;
83 static const char *get_winbind_domain(void)
85 static struct wbcInterfaceDetails *details;
87 details = init_interface_details();
89 if (!details) {
90 d_fprintf(stderr, "could not obtain winbind domain name!\n");
91 return 0;
94 return details->netbios_domain;
97 static const char *get_winbind_netbios_name(void)
99 static struct wbcInterfaceDetails *details;
101 details = init_interface_details();
103 if (!details) {
104 d_fprintf(stderr, "could not obtain winbind netbios name!\n");
105 return 0;
108 return details->netbios_name;
111 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
112 form DOMAIN/user into a domain and a user */
114 static bool parse_wbinfo_domain_user(const char *domuser, fstring domain,
115 fstring user)
118 char *p = strchr(domuser,winbind_separator());
120 if (!p) {
121 /* Maybe it was a UPN? */
122 p = strchr(domuser, '@');
123 if (p != NULL) {
124 fstrcpy(domain, "");
125 fstrcpy(user, domuser);
126 return true;
129 fstrcpy(user, domuser);
130 fstrcpy(domain, get_winbind_domain());
131 return true;
134 fstrcpy(user, p+1);
135 fstrcpy(domain, domuser);
136 domain[PTR_DIFF(p, domuser)] = 0;
138 return true;
141 /* Parse string of "uid,sid" or "gid,sid" into separate int and string values.
142 * Return true if input was valid, false otherwise. */
143 static bool parse_mapping_arg(char *arg, int *id, char **sid)
145 char *tmp;
146 int error = 0;
148 if (!arg || !*arg)
149 return false;
151 tmp = strtok(arg, ",");
152 *sid = strtok(NULL, ",");
154 if (!tmp || !*tmp || !*sid || !**sid)
155 return false;
157 /* Because atoi() can return 0 on invalid input, which would be a valid
158 * UID/GID we must use strtoul() and do error checking */
159 *id = smb_strtoul(tmp, NULL, 10, &error, SMB_STR_FULL_STR_CONV);
160 if (error != 0)
161 return false;
163 return true;
166 /* pull pwent info for a given user */
168 static bool wbinfo_get_userinfo(char *user)
170 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
171 struct passwd *pwd = NULL;
173 wbc_status = wbcGetpwnam(user, &pwd);
174 if (!WBC_ERROR_IS_OK(wbc_status)) {
175 d_fprintf(stderr, "failed to call wbcGetpwnam: %s\n",
176 wbcErrorString(wbc_status));
177 return false;
180 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
181 pwd->pw_name,
182 pwd->pw_passwd,
183 (unsigned int)pwd->pw_uid,
184 (unsigned int)pwd->pw_gid,
185 pwd->pw_gecos,
186 pwd->pw_dir,
187 pwd->pw_shell);
189 wbcFreeMemory(pwd);
191 return true;
194 /* pull pwent info for a given uid */
195 static bool wbinfo_get_uidinfo(int uid)
197 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
198 struct passwd *pwd = NULL;
200 wbc_status = wbcGetpwuid(uid, &pwd);
201 if (!WBC_ERROR_IS_OK(wbc_status)) {
202 d_fprintf(stderr, "failed to call wbcGetpwuid: %s\n",
203 wbcErrorString(wbc_status));
204 return false;
207 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
208 pwd->pw_name,
209 pwd->pw_passwd,
210 (unsigned int)pwd->pw_uid,
211 (unsigned int)pwd->pw_gid,
212 pwd->pw_gecos,
213 pwd->pw_dir,
214 pwd->pw_shell);
216 wbcFreeMemory(pwd);
218 return true;
221 static bool wbinfo_get_user_sidinfo(const char *sid_str)
223 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
224 struct passwd *pwd = NULL;
225 struct wbcDomainSid sid;
227 wbc_status = wbcStringToSid(sid_str, &sid);
228 wbc_status = wbcGetpwsid(&sid, &pwd);
229 if (!WBC_ERROR_IS_OK(wbc_status)) {
230 d_fprintf(stderr, "failed to call wbcGetpwsid: %s\n",
231 wbcErrorString(wbc_status));
232 return false;
235 d_printf("%s:%s:%u:%u:%s:%s:%s\n",
236 pwd->pw_name,
237 pwd->pw_passwd,
238 (unsigned int)pwd->pw_uid,
239 (unsigned int)pwd->pw_gid,
240 pwd->pw_gecos,
241 pwd->pw_dir,
242 pwd->pw_shell);
244 wbcFreeMemory(pwd);
246 return true;
250 /* pull grent for a given group */
251 static bool wbinfo_get_groupinfo(const char *group)
253 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
254 struct group *grp;
255 char **mem;
257 wbc_status = wbcGetgrnam(group, &grp);
258 if (!WBC_ERROR_IS_OK(wbc_status)) {
259 d_fprintf(stderr, "failed to call wbcGetgrnam: %s\n",
260 wbcErrorString(wbc_status));
261 return false;
264 d_printf("%s:%s:%u:",
265 grp->gr_name,
266 grp->gr_passwd,
267 (unsigned int)grp->gr_gid);
269 mem = grp->gr_mem;
270 while (*mem != NULL) {
271 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
272 mem += 1;
274 d_printf("\n");
276 wbcFreeMemory(grp);
278 return true;
281 /* pull grent for a given gid */
282 static bool wbinfo_get_gidinfo(int gid)
284 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
285 struct group *grp;
286 char **mem;
288 wbc_status = wbcGetgrgid(gid, &grp);
289 if (!WBC_ERROR_IS_OK(wbc_status)) {
290 d_fprintf(stderr, "failed to call wbcGetgrgid: %s\n",
291 wbcErrorString(wbc_status));
292 return false;
295 d_printf("%s:%s:%u:",
296 grp->gr_name,
297 grp->gr_passwd,
298 (unsigned int)grp->gr_gid);
300 mem = grp->gr_mem;
301 while (*mem != NULL) {
302 d_printf("%s%s", *mem, *(mem+1) != NULL ? "," : "");
303 mem += 1;
305 d_printf("\n");
307 wbcFreeMemory(grp);
309 return true;
312 /* List groups a user is a member of */
314 static bool wbinfo_get_usergroups(const char *user)
316 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
317 uint32_t num_groups;
318 uint32_t i;
319 gid_t *groups = NULL;
321 /* Send request */
323 wbc_status = wbcGetGroups(user, &num_groups, &groups);
324 if (!WBC_ERROR_IS_OK(wbc_status)) {
325 d_fprintf(stderr, "failed to call wbcGetGroups: %s\n",
326 wbcErrorString(wbc_status));
327 return false;
330 for (i = 0; i < num_groups; i++) {
331 d_printf("%d\n", (int)groups[i]);
334 wbcFreeMemory(groups);
336 return true;
340 /* List group SIDs a user SID is a member of */
341 static bool wbinfo_get_usersids(const char *user_sid_str)
343 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
344 uint32_t num_sids;
345 uint32_t i;
346 struct wbcDomainSid user_sid, *sids = NULL;
348 /* Send request */
350 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
351 if (!WBC_ERROR_IS_OK(wbc_status)) {
352 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
353 wbcErrorString(wbc_status));
354 return false;
357 wbc_status = wbcLookupUserSids(&user_sid, false, &num_sids, &sids);
358 if (!WBC_ERROR_IS_OK(wbc_status)) {
359 d_fprintf(stderr, "failed to call wbcLookupUserSids: %s\n",
360 wbcErrorString(wbc_status));
361 return false;
364 for (i = 0; i < num_sids; i++) {
365 char str[WBC_SID_STRING_BUFLEN];
366 wbcSidToStringBuf(&sids[i], str, sizeof(str));
367 d_printf("%s\n", str);
370 wbcFreeMemory(sids);
372 return true;
375 static bool wbinfo_get_userdomgroups(const char *user_sid_str)
377 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
378 uint32_t num_sids;
379 uint32_t i;
380 struct wbcDomainSid user_sid, *sids = NULL;
382 /* Send request */
384 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
385 if (!WBC_ERROR_IS_OK(wbc_status)) {
386 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
387 wbcErrorString(wbc_status));
388 return false;
391 wbc_status = wbcLookupUserSids(&user_sid, true, &num_sids, &sids);
392 if (!WBC_ERROR_IS_OK(wbc_status)) {
393 d_fprintf(stderr, "failed to call wbcLookupUserSids: %s\n",
394 wbcErrorString(wbc_status));
395 return false;
398 for (i = 0; i < num_sids; i++) {
399 char str[WBC_SID_STRING_BUFLEN];
400 wbcSidToStringBuf(&sids[i], str, sizeof(str));
401 d_printf("%s\n", str);
404 wbcFreeMemory(sids);
406 return true;
409 static bool wbinfo_get_sidaliases(const char *domain,
410 const char *user_sid_str)
412 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
413 struct wbcDomainInfo *dinfo = NULL;
414 uint32_t i;
415 struct wbcDomainSid user_sid;
416 uint32_t *alias_rids = NULL;
417 uint32_t num_alias_rids;
418 char domain_sid_str[WBC_SID_STRING_BUFLEN];
420 /* Send request */
421 if ((domain == NULL) || (strequal(domain, ".")) ||
422 (domain[0] == '\0')) {
423 domain = get_winbind_domain();
426 /* Send request */
428 wbc_status = wbcDomainInfo(domain, &dinfo);
429 if (!WBC_ERROR_IS_OK(wbc_status)) {
430 d_fprintf(stderr, "wbcDomainInfo(%s) failed: %s\n", domain,
431 wbcErrorString(wbc_status));
432 goto done;
434 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
435 if (!WBC_ERROR_IS_OK(wbc_status)) {
436 goto done;
439 wbc_status = wbcGetSidAliases(&dinfo->sid, &user_sid, 1,
440 &alias_rids, &num_alias_rids);
441 if (!WBC_ERROR_IS_OK(wbc_status)) {
442 goto done;
445 wbcSidToStringBuf(&dinfo->sid, domain_sid_str, sizeof(domain_sid_str));
447 for (i = 0; i < num_alias_rids; i++) {
448 d_printf("%s-%d\n", domain_sid_str, alias_rids[i]);
451 wbcFreeMemory(alias_rids);
453 done:
454 wbcFreeMemory(dinfo);
455 return (WBC_ERR_SUCCESS == wbc_status);
459 /* Convert NetBIOS name to IP */
461 static bool wbinfo_wins_byname(const char *name)
463 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
464 char *ip = NULL;
466 wbc_status = wbcResolveWinsByName(name, &ip);
467 if (!WBC_ERROR_IS_OK(wbc_status)) {
468 d_fprintf(stderr, "failed to call wbcResolveWinsByName: %s\n",
469 wbcErrorString(wbc_status));
470 return false;
473 /* Display response */
475 d_printf("%s\n", ip);
477 wbcFreeMemory(ip);
479 return true;
482 /* Convert IP to NetBIOS name */
484 static bool wbinfo_wins_byip(const char *ip)
486 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
487 char *name = NULL;
489 wbc_status = wbcResolveWinsByIP(ip, &name);
490 if (!WBC_ERROR_IS_OK(wbc_status)) {
491 d_fprintf(stderr, "failed to call wbcResolveWinsByIP: %s\n",
492 wbcErrorString(wbc_status));
493 return false;
496 /* Display response */
498 d_printf("%s\n", name);
500 wbcFreeMemory(name);
502 return true;
505 /* List all/trusted domains */
507 static bool wbinfo_list_domains(bool list_all_domains, bool verbose)
509 struct wbcDomainInfo *domain_list = NULL;
510 size_t i, num_domains;
511 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
512 bool print_all = !list_all_domains && verbose;
514 wbc_status = wbcListTrusts(&domain_list, &num_domains);
515 if (!WBC_ERROR_IS_OK(wbc_status)) {
516 d_fprintf(stderr, "failed to call wbcListTrusts: %s\n",
517 wbcErrorString(wbc_status));
518 return false;
521 if (print_all) {
522 d_printf("%-16s%-65s%-12s%-12s%-5s%-5s\n",
523 "Domain Name", "DNS Domain", "Trust Type",
524 "Transitive", "In", "Out");
527 for (i=0; i<num_domains; i++) {
528 if (print_all) {
529 d_printf("%-16s", domain_list[i].short_name);
530 } else {
531 d_printf("%s", domain_list[i].short_name);
532 d_printf("\n");
533 continue;
536 d_printf("%-65s", domain_list[i].dns_name);
538 switch(domain_list[i].trust_type) {
539 case WBC_DOMINFO_TRUSTTYPE_NONE:
540 if (domain_list[i].trust_routing != NULL) {
541 d_printf("%s\n", domain_list[i].trust_routing);
542 } else {
543 d_printf("None\n");
545 continue;
546 case WBC_DOMINFO_TRUSTTYPE_LOCAL:
547 d_printf("Local\n");
548 continue;
549 case WBC_DOMINFO_TRUSTTYPE_RWDC:
550 d_printf("RWDC\n");
551 continue;
552 case WBC_DOMINFO_TRUSTTYPE_RODC:
553 d_printf("RODC\n");
554 continue;
555 case WBC_DOMINFO_TRUSTTYPE_PDC:
556 d_printf("PDC\n");
557 continue;
558 case WBC_DOMINFO_TRUSTTYPE_WKSTA:
559 d_printf("Workstation ");
560 break;
561 case WBC_DOMINFO_TRUSTTYPE_FOREST:
562 d_printf("Forest ");
563 break;
564 case WBC_DOMINFO_TRUSTTYPE_EXTERNAL:
565 d_printf("External ");
566 break;
567 case WBC_DOMINFO_TRUSTTYPE_IN_FOREST:
568 d_printf("In-Forest ");
569 break;
572 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_TRANSITIVE) {
573 d_printf("Yes ");
574 } else {
575 d_printf("No ");
578 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_INCOMING) {
579 d_printf("Yes ");
580 } else {
581 d_printf("No ");
584 if (domain_list[i].trust_flags & WBC_DOMINFO_TRUST_OUTGOING) {
585 d_printf("Yes ");
586 } else {
587 d_printf("No ");
590 d_printf("\n");
593 wbcFreeMemory(domain_list);
595 return true;
598 /* List own domain */
600 static bool wbinfo_list_own_domain(void)
602 d_printf("%s\n", get_winbind_domain());
604 return true;
607 /* show sequence numbers */
608 static bool wbinfo_show_sequence(const char *domain)
610 d_printf("This command has been deprecated. Please use the "
611 "--online-status option instead.\n");
612 return false;
615 /* show sequence numbers */
616 static bool wbinfo_show_onlinestatus(const char *domain)
618 struct wbcDomainInfo *domain_list = NULL;
619 size_t i, num_domains;
620 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
622 wbc_status = wbcListTrusts(&domain_list, &num_domains);
623 if (!WBC_ERROR_IS_OK(wbc_status)) {
624 d_fprintf(stderr, "failed to call wbcListTrusts: %s\n",
625 wbcErrorString(wbc_status));
626 return false;
629 for (i=0; i<num_domains; i++) {
630 bool is_offline;
632 if (domain) {
633 if (!strequal(domain_list[i].short_name, domain)) {
634 continue;
638 is_offline = (domain_list[i].domain_flags &
639 WBC_DOMINFO_DOMAIN_OFFLINE);
641 d_printf("%s : %s\n",
642 domain_list[i].short_name,
643 is_offline ? "no active connection" : "active connection" );
646 wbcFreeMemory(domain_list);
648 return true;
652 /* Show domain info */
654 static bool wbinfo_domain_info(const char *domain)
656 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
657 struct wbcDomainInfo *dinfo = NULL;
658 char sid_str[WBC_SID_STRING_BUFLEN];
660 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
661 domain = get_winbind_domain();
664 /* Send request */
666 wbc_status = wbcDomainInfo(domain, &dinfo);
667 if (!WBC_ERROR_IS_OK(wbc_status)) {
668 d_fprintf(stderr, "failed to call wbcDomainInfo: %s\n",
669 wbcErrorString(wbc_status));
670 return false;
673 wbcSidToStringBuf(&dinfo->sid, sid_str, sizeof(sid_str));
675 /* Display response */
677 d_printf("Name : %s\n", dinfo->short_name);
678 d_printf("Alt_Name : %s\n", dinfo->dns_name);
680 d_printf("SID : %s\n", sid_str);
682 d_printf("Active Directory : %s\n",
683 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_AD) ? "Yes" : "No");
684 d_printf("Native : %s\n",
685 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_NATIVE) ?
686 "Yes" : "No");
688 d_printf("Primary : %s\n",
689 (dinfo->domain_flags & WBC_DOMINFO_DOMAIN_PRIMARY) ?
690 "Yes" : "No");
692 wbcFreeMemory(dinfo);
694 return true;
697 /* Get a foreign DC's name */
698 static bool wbinfo_getdcname(const char *domain_name)
700 struct winbindd_request request;
701 struct winbindd_response response;
703 ZERO_STRUCT(request);
704 ZERO_STRUCT(response);
706 fstrcpy(request.domain_name, domain_name);
708 /* Send request */
710 if (winbindd_request_response(NULL, WINBINDD_GETDCNAME, &request,
711 &response) != NSS_STATUS_SUCCESS) {
712 d_fprintf(stderr, "Could not get dc name for %s\n",domain_name);
713 return false;
716 /* Display response */
718 d_printf("%s\n", response.data.dc_name);
720 return true;
723 /* Find a DC */
724 static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
726 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
727 struct wbcDomainControllerInfoEx *dc_info;
728 char *str = NULL;
730 wbc_status = wbcLookupDomainControllerEx(domain_name, NULL, NULL,
731 flags | DS_DIRECTORY_SERVICE_REQUIRED,
732 &dc_info);
733 if (!WBC_ERROR_IS_OK(wbc_status)) {
734 printf("Could not find dc for %s\n", domain_name);
735 return false;
738 wbcGuidToString(dc_info->domain_guid, &str);
740 d_printf("%s\n", dc_info->dc_unc);
741 d_printf("%s\n", dc_info->dc_address);
742 d_printf("%d\n", dc_info->dc_address_type);
743 d_printf("%s\n", str);
744 d_printf("%s\n", dc_info->domain_name);
745 d_printf("%s\n", dc_info->forest_name);
746 d_printf("0x%08x\n", dc_info->dc_flags);
747 d_printf("%s\n", dc_info->dc_site_name);
748 d_printf("%s\n", dc_info->client_site_name);
750 wbcFreeMemory(str);
751 wbcFreeMemory(dc_info);
753 return true;
756 /* Check trust account password */
758 static bool wbinfo_check_secret(const char *domain)
760 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
761 struct wbcAuthErrorInfo *error = NULL;
762 const char *domain_name;
764 if (domain) {
765 domain_name = domain;
766 } else {
767 domain_name = get_winbind_domain();
770 wbc_status = wbcCheckTrustCredentials(domain_name, &error);
772 d_printf("checking the trust secret for domain %s via RPC calls %s\n",
773 domain_name,
774 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
776 if (wbc_status == WBC_ERR_AUTH_ERROR) {
777 d_fprintf(stderr, "wbcCheckTrustCredentials(%s): error code was %s (0x%x)\n",
778 domain_name, error->nt_string, error->nt_status);
779 wbcFreeMemory(error);
781 if (!WBC_ERROR_IS_OK(wbc_status)) {
782 d_fprintf(stderr, "failed to call wbcCheckTrustCredentials: "
783 "%s\n", wbcErrorString(wbc_status));
784 return false;
787 return true;
790 /* Find the currently connected DCs */
792 static bool wbinfo_dc_info(const char *domain_name)
794 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
795 size_t i, num_dcs;
796 const char **dc_names, **dc_ips;
798 wbc_status = wbcDcInfo(domain_name, &num_dcs,
799 &dc_names, &dc_ips);
800 if (!WBC_ERROR_IS_OK(wbc_status)) {
801 printf("Could not find dc info %s\n",
802 domain_name ? domain_name : "our domain");
803 return false;
806 for (i=0; i<num_dcs; i++) {
807 printf("%s (%s)\n", dc_names[i], dc_ips[i]);
809 wbcFreeMemory(dc_names);
810 wbcFreeMemory(dc_ips);
812 return true;
815 /* Change trust account password */
817 static bool wbinfo_change_secret(const char *domain)
819 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
820 struct wbcAuthErrorInfo *error = NULL;
821 const char *domain_name;
823 if (domain) {
824 domain_name = domain;
825 } else {
826 domain_name = get_winbind_domain();
829 wbc_status = wbcChangeTrustCredentials(domain_name, &error);
831 d_printf("changing the trust secret for domain %s via RPC calls %s\n",
832 domain_name,
833 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
835 if (wbc_status == WBC_ERR_AUTH_ERROR) {
836 d_fprintf(stderr, "wbcChangeTrustCredentials(%s): error code was %s (0x%x)\n",
837 domain_name, error->nt_string, error->nt_status);
838 wbcFreeMemory(error);
840 if (!WBC_ERROR_IS_OK(wbc_status)) {
841 d_fprintf(stderr, "failed to call wbcChangeTrustCredentials: "
842 "%s\n", wbcErrorString(wbc_status));
843 return false;
846 return true;
849 /* Check DC connection */
851 static bool wbinfo_ping_dc(const char *domain)
853 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
854 struct wbcAuthErrorInfo *error = NULL;
855 char *dcname = NULL;
857 const char *domain_name;
859 if (domain) {
860 domain_name = domain;
861 } else {
862 domain_name = get_winbind_domain();
865 wbc_status = wbcPingDc2(domain_name, &error, &dcname);
867 d_printf("checking the NETLOGON for domain[%s] dc connection to \"%s\" %s\n",
868 domain_name ? domain_name : "",
869 dcname ? dcname : "",
870 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
872 wbcFreeMemory(dcname);
873 if (wbc_status == WBC_ERR_AUTH_ERROR) {
874 d_fprintf(stderr, "wbcPingDc2(%s): error code was %s (0x%x)\n",
875 domain_name, error->nt_string, error->nt_status);
876 wbcFreeMemory(error);
877 return false;
879 if (!WBC_ERROR_IS_OK(wbc_status)) {
880 d_fprintf(stderr, "failed to call wbcPingDc: %s\n",
881 wbcErrorString(wbc_status));
882 return false;
885 return true;
888 /* Convert uid to sid */
890 static bool wbinfo_uid_to_sid(uid_t uid)
892 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
893 struct wbcDomainSid sid;
894 char sid_str[WBC_SID_STRING_BUFLEN];
896 /* Send request */
898 wbc_status = wbcUidToSid(uid, &sid);
899 if (!WBC_ERROR_IS_OK(wbc_status)) {
900 d_fprintf(stderr, "failed to call wbcUidToSid: %s\n",
901 wbcErrorString(wbc_status));
902 return false;
905 wbcSidToStringBuf(&sid, sid_str, sizeof(sid_str));
907 /* Display response */
909 d_printf("%s\n", sid_str);
911 return true;
914 /* Convert gid to sid */
916 static bool wbinfo_gid_to_sid(gid_t gid)
918 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
919 struct wbcDomainSid sid;
920 char sid_str[WBC_SID_STRING_BUFLEN];
922 /* Send request */
924 wbc_status = wbcGidToSid(gid, &sid);
925 if (!WBC_ERROR_IS_OK(wbc_status)) {
926 d_fprintf(stderr, "failed to call wbcGidToSid: %s\n",
927 wbcErrorString(wbc_status));
928 return false;
931 wbcSidToStringBuf(&sid, sid_str, sizeof(sid_str));
933 /* Display response */
935 d_printf("%s\n", sid_str);
937 return true;
940 /* Convert sid to uid */
942 static bool wbinfo_sid_to_uid(const char *sid_str)
944 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
945 struct wbcDomainSid sid;
946 uid_t uid;
948 /* Send request */
950 wbc_status = wbcStringToSid(sid_str, &sid);
951 if (!WBC_ERROR_IS_OK(wbc_status)) {
952 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
953 wbcErrorString(wbc_status));
954 return false;
957 wbc_status = wbcSidToUid(&sid, &uid);
958 if (!WBC_ERROR_IS_OK(wbc_status)) {
959 d_fprintf(stderr, "failed to call wbcSidToUid: %s\n",
960 wbcErrorString(wbc_status));
961 return false;
964 /* Display response */
966 d_printf("%d\n", (int)uid);
968 return true;
971 static bool wbinfo_sid_to_gid(const char *sid_str)
973 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
974 struct wbcDomainSid sid;
975 gid_t gid;
977 /* Send request */
979 wbc_status = wbcStringToSid(sid_str, &sid);
980 if (!WBC_ERROR_IS_OK(wbc_status)) {
981 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
982 wbcErrorString(wbc_status));
983 return false;
986 wbc_status = wbcSidToGid(&sid, &gid);
987 if (!WBC_ERROR_IS_OK(wbc_status)) {
988 d_fprintf(stderr, "failed to call wbcSidToGid: %s\n",
989 wbcErrorString(wbc_status));
990 return false;
993 /* Display response */
995 d_printf("%d\n", (int)gid);
997 return true;
1000 static bool wbinfo_sids_to_unix_ids(const char *arg)
1002 char sidstr[WBC_SID_STRING_BUFLEN];
1003 struct wbcDomainSid *sids;
1004 struct wbcUnixId *unix_ids;
1005 int i, num_sids;
1006 const char *p;
1007 wbcErr wbc_status;
1010 num_sids = 0;
1011 sids = NULL;
1012 p = arg;
1014 while (next_token(&p, sidstr, LIST_SEP, sizeof(sidstr))) {
1015 sids = talloc_realloc(talloc_tos(), sids, struct wbcDomainSid,
1016 num_sids+1);
1017 if (sids == NULL) {
1018 d_fprintf(stderr, "talloc failed\n");
1019 return false;
1021 wbc_status = wbcStringToSid(sidstr, &sids[num_sids]);
1022 if (!WBC_ERROR_IS_OK(wbc_status)) {
1023 d_fprintf(stderr, "wbcSidToString(%s) failed: %s\n",
1024 sidstr, wbcErrorString(wbc_status));
1025 TALLOC_FREE(sids);
1026 return false;
1028 num_sids += 1;
1031 unix_ids = talloc_array(talloc_tos(), struct wbcUnixId, num_sids);
1032 if (unix_ids == NULL) {
1033 TALLOC_FREE(sids);
1034 return false;
1037 wbc_status = wbcSidsToUnixIds(sids, num_sids, unix_ids);
1038 if (!WBC_ERROR_IS_OK(wbc_status)) {
1039 d_fprintf(stderr, "wbcSidsToUnixIds failed: %s\n",
1040 wbcErrorString(wbc_status));
1041 TALLOC_FREE(sids);
1042 return false;
1045 for (i=0; i<num_sids; i++) {
1047 wbcSidToStringBuf(&sids[i], sidstr, sizeof(sidstr));
1049 switch(unix_ids[i].type) {
1050 case WBC_ID_TYPE_UID:
1051 d_printf("%s -> uid %d\n", sidstr, unix_ids[i].id.uid);
1052 break;
1053 case WBC_ID_TYPE_GID:
1054 d_printf("%s -> gid %d\n", sidstr, unix_ids[i].id.gid);
1055 break;
1056 case WBC_ID_TYPE_BOTH:
1057 d_printf("%s -> uid/gid %d\n", sidstr, unix_ids[i].id.uid);
1058 break;
1059 default:
1060 d_printf("%s -> unmapped\n", sidstr);
1061 break;
1065 TALLOC_FREE(sids);
1066 TALLOC_FREE(unix_ids);
1068 return true;
1071 static bool wbinfo_xids_to_sids(const char *arg)
1073 fstring idstr;
1074 struct wbcUnixId *xids = NULL;
1075 struct wbcDomainSid *sids;
1076 wbcErr wbc_status;
1077 int num_xids = 0;
1078 const char *p;
1079 int i;
1081 p = arg;
1083 while (next_token(&p, idstr, LIST_SEP, sizeof(idstr))) {
1084 xids = talloc_realloc(talloc_tos(), xids, struct wbcUnixId,
1085 num_xids+1);
1086 if (xids == NULL) {
1087 d_fprintf(stderr, "talloc failed\n");
1088 return false;
1091 switch (idstr[0]) {
1092 case 'u':
1093 xids[num_xids] = (struct wbcUnixId) {
1094 .type = WBC_ID_TYPE_UID,
1095 .id.uid = atoi(&idstr[1])
1097 break;
1098 case 'g':
1099 xids[num_xids] = (struct wbcUnixId) {
1100 .type = WBC_ID_TYPE_GID,
1101 .id.gid = atoi(&idstr[1])
1103 break;
1104 default:
1105 d_fprintf(stderr, "%s is an invalid id\n", idstr);
1106 TALLOC_FREE(xids);
1107 return false;
1109 num_xids += 1;
1112 sids = talloc_array(talloc_tos(), struct wbcDomainSid, num_xids);
1113 if (sids == NULL) {
1114 d_fprintf(stderr, "talloc failed\n");
1115 TALLOC_FREE(xids);
1116 return false;
1119 wbc_status = wbcUnixIdsToSids(xids, num_xids, sids);
1120 if (!WBC_ERROR_IS_OK(wbc_status)) {
1121 d_fprintf(stderr, "wbcUnixIdsToSids failed: %s\n",
1122 wbcErrorString(wbc_status));
1123 TALLOC_FREE(sids);
1124 TALLOC_FREE(xids);
1125 return false;
1128 for (i=0; i<num_xids; i++) {
1129 char str[WBC_SID_STRING_BUFLEN];
1130 struct wbcDomainSid null_sid = { 0 };
1132 if (memcmp(&null_sid, &sids[i], sizeof(struct wbcDomainSid)) == 0) {
1133 d_printf("NOT MAPPED\n");
1134 continue;
1136 wbcSidToStringBuf(&sids[i], str, sizeof(str));
1137 d_printf("%s\n", str);
1140 return true;
1143 static bool wbinfo_allocate_uid(void)
1145 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1146 uid_t uid;
1148 /* Send request */
1150 wbc_status = wbcAllocateUid(&uid);
1151 if (!WBC_ERROR_IS_OK(wbc_status)) {
1152 d_fprintf(stderr, "failed to call wbcAllocateUid: %s\n",
1153 wbcErrorString(wbc_status));
1154 return false;
1157 /* Display response */
1159 d_printf("New uid: %u\n", (unsigned int)uid);
1161 return true;
1164 static bool wbinfo_allocate_gid(void)
1166 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1167 gid_t gid;
1169 /* Send request */
1171 wbc_status = wbcAllocateGid(&gid);
1172 if (!WBC_ERROR_IS_OK(wbc_status)) {
1173 d_fprintf(stderr, "failed to call wbcAllocateGid: %s\n",
1174 wbcErrorString(wbc_status));
1175 return false;
1178 /* Display response */
1180 d_printf("New gid: %u\n", (unsigned int)gid);
1182 return true;
1185 static bool wbinfo_set_uid_mapping(uid_t uid, const char *sid_str)
1187 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1188 struct wbcDomainSid sid;
1190 /* Send request */
1192 wbc_status = wbcStringToSid(sid_str, &sid);
1193 if (!WBC_ERROR_IS_OK(wbc_status)) {
1194 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1195 wbcErrorString(wbc_status));
1196 return false;
1199 wbc_status = wbcSetUidMapping(uid, &sid);
1200 if (!WBC_ERROR_IS_OK(wbc_status)) {
1201 d_fprintf(stderr, "failed to call wbcSetUidMapping: %s\n",
1202 wbcErrorString(wbc_status));
1203 return false;
1206 /* Display response */
1208 d_printf("uid %u now mapped to sid %s\n",
1209 (unsigned int)uid, sid_str);
1211 return true;
1214 static bool wbinfo_set_gid_mapping(gid_t gid, const char *sid_str)
1216 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1217 struct wbcDomainSid sid;
1219 /* Send request */
1221 wbc_status = wbcStringToSid(sid_str, &sid);
1222 if (!WBC_ERROR_IS_OK(wbc_status)) {
1223 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1224 wbcErrorString(wbc_status));
1225 return false;
1228 wbc_status = wbcSetGidMapping(gid, &sid);
1229 if (!WBC_ERROR_IS_OK(wbc_status)) {
1230 d_fprintf(stderr, "failed to call wbcSetGidMapping: %s\n",
1231 wbcErrorString(wbc_status));
1232 return false;
1235 /* Display response */
1237 d_printf("gid %u now mapped to sid %s\n",
1238 (unsigned int)gid, sid_str);
1240 return true;
1243 static bool wbinfo_remove_uid_mapping(uid_t uid, const char *sid_str)
1245 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1246 struct wbcDomainSid sid;
1248 /* Send request */
1250 wbc_status = wbcStringToSid(sid_str, &sid);
1251 if (!WBC_ERROR_IS_OK(wbc_status)) {
1252 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1253 wbcErrorString(wbc_status));
1254 return false;
1257 wbc_status = wbcRemoveUidMapping(uid, &sid);
1258 if (!WBC_ERROR_IS_OK(wbc_status)) {
1259 d_fprintf(stderr, "failed to call wbcRemoveUidMapping: %s\n",
1260 wbcErrorString(wbc_status));
1261 return false;
1264 /* Display response */
1266 d_printf("Removed uid %u to sid %s mapping\n",
1267 (unsigned int)uid, sid_str);
1269 return true;
1272 static bool wbinfo_remove_gid_mapping(gid_t gid, const char *sid_str)
1274 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1275 struct wbcDomainSid sid;
1277 /* Send request */
1279 wbc_status = wbcStringToSid(sid_str, &sid);
1280 if (!WBC_ERROR_IS_OK(wbc_status)) {
1281 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1282 wbcErrorString(wbc_status));
1283 return false;
1286 wbc_status = wbcRemoveGidMapping(gid, &sid);
1287 if (!WBC_ERROR_IS_OK(wbc_status)) {
1288 d_fprintf(stderr, "failed to call wbcRemoveGidMapping: %s\n",
1289 wbcErrorString(wbc_status));
1290 return false;
1293 /* Display response */
1295 d_printf("Removed gid %u to sid %s mapping\n",
1296 (unsigned int)gid, sid_str);
1298 return true;
1301 /* Convert sid to string */
1303 static bool wbinfo_lookupsid(const char *sid_str)
1305 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1306 struct wbcDomainSid sid;
1307 char *domain;
1308 char *name;
1309 enum wbcSidType type;
1311 /* Send off request */
1313 wbc_status = wbcStringToSid(sid_str, &sid);
1314 if (!WBC_ERROR_IS_OK(wbc_status)) {
1315 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1316 wbcErrorString(wbc_status));
1317 return false;
1320 wbc_status = wbcLookupSid(&sid, &domain, &name, &type);
1321 if (!WBC_ERROR_IS_OK(wbc_status)) {
1322 d_fprintf(stderr, "failed to call wbcLookupSid: %s\n",
1323 wbcErrorString(wbc_status));
1324 return false;
1327 /* Display response */
1329 if (type == WBC_SID_NAME_DOMAIN) {
1330 d_printf("%s %d\n", domain, type);
1331 } else {
1332 d_printf("%s%c%s %d\n",
1333 domain, winbind_separator(), name, type);
1336 wbcFreeMemory(domain);
1337 wbcFreeMemory(name);
1339 return true;
1342 /* Convert sid to fullname */
1344 static bool wbinfo_lookupsid_fullname(const char *sid_str)
1346 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1347 struct wbcDomainSid sid;
1348 char *domain;
1349 char *name;
1350 enum wbcSidType type;
1352 /* Send off request */
1354 wbc_status = wbcStringToSid(sid_str, &sid);
1355 if (!WBC_ERROR_IS_OK(wbc_status)) {
1356 d_fprintf(stderr, "failed to call wbcStringToSid: %s\n",
1357 wbcErrorString(wbc_status));
1358 return false;
1361 wbc_status = wbcGetDisplayName(&sid, &domain, &name, &type);
1362 if (!WBC_ERROR_IS_OK(wbc_status)) {
1363 d_fprintf(stderr, "failed to call wbcGetDisplayName: %s\n",
1364 wbcErrorString(wbc_status));
1365 return false;
1368 /* Display response */
1370 d_printf("%s%c%s %d\n",
1371 domain, winbind_separator(), name, type);
1373 wbcFreeMemory(domain);
1374 wbcFreeMemory(name);
1376 return true;
1379 /* Lookup a list of RIDs */
1381 static bool wbinfo_lookuprids(const char *domain, const char *arg)
1383 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1384 struct wbcDomainSid dsid;
1385 char *domain_name = NULL;
1386 const char **names = NULL;
1387 enum wbcSidType *types = NULL;
1388 size_t i, num_rids;
1389 uint32_t *rids = NULL;
1390 const char *p;
1391 char *ridstr;
1392 TALLOC_CTX *mem_ctx = NULL;
1393 bool ret = false;
1395 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')){
1396 domain = get_winbind_domain();
1399 wbc_status = wbcStringToSid(domain, &dsid);
1400 if (!WBC_ERROR_IS_OK(wbc_status)) {
1401 struct wbcDomainInfo *dinfo = NULL;
1403 wbc_status = wbcDomainInfo(domain, &dinfo);
1404 if (!WBC_ERROR_IS_OK(wbc_status)) {
1405 d_printf("wbcDomainInfo(%s) failed: %s\n", domain,
1406 wbcErrorString(wbc_status));
1407 goto done;
1410 dsid = dinfo->sid;
1411 wbcFreeMemory(dinfo);
1414 mem_ctx = talloc_new(NULL);
1415 if (mem_ctx == NULL) {
1416 d_printf("talloc_new failed\n");
1417 goto done;
1420 num_rids = 0;
1421 rids = NULL;
1422 p = arg;
1424 while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
1425 int error = 0;
1426 uint32_t rid;
1428 rid = smb_strtoul(ridstr, NULL, 10, &error, SMB_STR_STANDARD);
1429 if (error != 0) {
1430 d_printf("failed to convert rid\n");
1431 goto done;
1433 rids = talloc_realloc(mem_ctx, rids, uint32_t, num_rids + 1);
1434 if (rids == NULL) {
1435 d_printf("talloc_realloc failed\n");
1437 rids[num_rids] = rid;
1438 num_rids += 1;
1441 if (rids == NULL) {
1442 d_printf("no rids\n");
1443 goto done;
1446 wbc_status = wbcLookupRids(
1447 &dsid, num_rids, rids, &p, &names, &types);
1448 if (!WBC_ERROR_IS_OK(wbc_status)) {
1449 d_printf("winbind_lookup_rids failed: %s\n",
1450 wbcErrorString(wbc_status));
1451 goto done;
1454 domain_name = discard_const_p(char, p);
1455 d_printf("Domain: %s\n", domain_name);
1457 for (i=0; i<num_rids; i++) {
1458 d_printf("%8d: %s (%s)\n", rids[i], names[i],
1459 wbcSidTypeString(types[i]));
1462 ret = true;
1463 done:
1464 wbcFreeMemory(domain_name);
1465 wbcFreeMemory(names);
1466 wbcFreeMemory(types);
1467 TALLOC_FREE(mem_ctx);
1468 return ret;
1471 static bool wbinfo_lookup_sids(const char *arg)
1473 char sidstr[WBC_SID_STRING_BUFLEN];
1474 struct wbcDomainSid *sids;
1475 struct wbcDomainInfo *domains;
1476 struct wbcTranslatedName *names;
1477 int num_domains;
1478 int i, num_sids;
1479 const char *p;
1480 wbcErr wbc_status;
1483 num_sids = 0;
1484 sids = NULL;
1485 p = arg;
1487 while (next_token(&p, sidstr, LIST_SEP, sizeof(sidstr))) {
1488 sids = talloc_realloc(talloc_tos(), sids, struct wbcDomainSid,
1489 num_sids+1);
1490 if (sids == NULL) {
1491 d_fprintf(stderr, "talloc failed\n");
1492 return false;
1494 wbc_status = wbcStringToSid(sidstr, &sids[num_sids]);
1495 if (!WBC_ERROR_IS_OK(wbc_status)) {
1496 d_fprintf(stderr, "wbcSidToString(%s) failed: %s\n",
1497 sidstr, wbcErrorString(wbc_status));
1498 TALLOC_FREE(sids);
1499 return false;
1501 num_sids += 1;
1504 wbc_status = wbcLookupSids(sids, num_sids, &domains, &num_domains,
1505 &names);
1506 if (!WBC_ERROR_IS_OK(wbc_status)) {
1507 d_fprintf(stderr, "wbcLookupSids failed: %s\n",
1508 wbcErrorString(wbc_status));
1509 TALLOC_FREE(sids);
1510 return false;
1513 for (i=0; i<num_sids; i++) {
1514 const char *domain = NULL;
1516 wbcSidToStringBuf(&sids[i], sidstr, sizeof(sidstr));
1518 if (names[i].domain_index >= num_domains) {
1519 domain = "<none>";
1520 } else if (names[i].domain_index < 0) {
1521 domain = "<none>";
1522 } else {
1523 domain = domains[names[i].domain_index].short_name;
1526 if (names[i].type == WBC_SID_NAME_DOMAIN) {
1527 d_printf("%s -> %s %d\n", sidstr,
1528 domain,
1529 names[i].type);
1530 } else {
1531 d_printf("%s -> %s%c%s %d\n", sidstr,
1532 domain,
1533 winbind_separator(),
1534 names[i].name, names[i].type);
1537 wbcFreeMemory(names);
1538 wbcFreeMemory(domains);
1539 return true;
1542 /* Convert string to sid */
1544 static bool wbinfo_lookupname(const char *full_name)
1546 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1547 struct wbcDomainSid sid;
1548 char sid_str[WBC_SID_STRING_BUFLEN];
1549 enum wbcSidType type;
1550 fstring domain_name;
1551 fstring account_name;
1553 /* Send off request */
1555 parse_wbinfo_domain_user(full_name, domain_name,
1556 account_name);
1558 wbc_status = wbcLookupName(domain_name, account_name,
1559 &sid, &type);
1560 if (!WBC_ERROR_IS_OK(wbc_status)) {
1561 d_fprintf(stderr, "failed to call wbcLookupName: %s\n",
1562 wbcErrorString(wbc_status));
1563 return false;
1566 wbcSidToStringBuf(&sid, sid_str, sizeof(sid_str));
1568 /* Display response */
1570 d_printf("%s %s (%d)\n", sid_str, wbcSidTypeString(type), type);
1572 return true;
1575 static char *wbinfo_prompt_pass(TALLOC_CTX *mem_ctx,
1576 const char *prefix,
1577 const char *username)
1579 char *prompt;
1580 char buf[1024] = {0};
1581 int rc;
1583 prompt = talloc_asprintf(mem_ctx, "Enter %s's ", username);
1584 if (!prompt) {
1585 return NULL;
1587 if (prefix) {
1588 prompt = talloc_asprintf_append(prompt, "%s ", prefix);
1589 if (!prompt) {
1590 return NULL;
1593 prompt = talloc_asprintf_append(prompt, "password: ");
1594 if (!prompt) {
1595 return NULL;
1598 rc = samba_getpass(prompt, buf, sizeof(buf), false, false);
1599 TALLOC_FREE(prompt);
1600 if (rc < 0) {
1601 return NULL;
1604 return talloc_strdup(mem_ctx, buf);
1607 /* Authenticate a user with a plaintext password */
1609 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32_t flags)
1611 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1612 char *s = NULL;
1613 char *p = NULL;
1614 char *password = NULL;
1615 char *name = NULL;
1616 char *local_cctype = NULL;
1617 uid_t uid;
1618 struct wbcLogonUserParams params;
1619 struct wbcLogonUserInfo *info;
1620 struct wbcAuthErrorInfo *error;
1621 struct wbcUserPasswordPolicyInfo *policy;
1622 TALLOC_CTX *frame = talloc_tos();
1624 if ((s = talloc_strdup(frame, username)) == NULL) {
1625 return false;
1628 if ((p = strchr(s, '%')) != NULL) {
1629 *p = 0;
1630 p++;
1631 password = talloc_strdup(frame, p);
1632 } else {
1633 password = wbinfo_prompt_pass(frame, NULL, username);
1636 local_cctype = talloc_strdup(frame, cctype);
1638 name = s;
1640 uid = geteuid();
1642 params.username = name;
1643 params.password = password;
1644 params.num_blobs = 0;
1645 params.blobs = NULL;
1647 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1648 &params.blobs,
1649 "flags",
1651 (uint8_t *)&flags,
1652 sizeof(flags));
1653 if (!WBC_ERROR_IS_OK(wbc_status)) {
1654 d_fprintf(stderr, "failed to call wbcAddNamedBlob: %s\n",
1655 wbcErrorString(wbc_status));
1656 goto done;
1659 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1660 &params.blobs,
1661 "user_uid",
1663 (uint8_t *)&uid,
1664 sizeof(uid));
1665 if (!WBC_ERROR_IS_OK(wbc_status)) {
1666 d_fprintf(stderr, "failed to call wbcAddNamedBlob: %s\n",
1667 wbcErrorString(wbc_status));
1668 goto done;
1671 wbc_status = wbcAddNamedBlob(&params.num_blobs,
1672 &params.blobs,
1673 "krb5_cc_type",
1675 (uint8_t *)local_cctype,
1676 strlen(cctype)+1);
1677 if (!WBC_ERROR_IS_OK(wbc_status)) {
1678 d_fprintf(stderr, "failed to call wbcAddNamedBlob: %s\n",
1679 wbcErrorString(wbc_status));
1680 goto done;
1683 wbc_status = wbcLogonUser(&params, &info, &error, &policy);
1685 d_printf("plaintext kerberos password authentication for [%s] %s "
1686 "(requesting cctype: %s)\n",
1687 name, WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed",
1688 cctype);
1690 if (error) {
1691 d_fprintf(stderr,
1692 "wbcLogonUser(%s): error code was %s (0x%x)\n"
1693 "error message was: %s\n",
1694 params.username, error->nt_string,
1695 error->nt_status,
1696 error->display_string);
1699 if (WBC_ERROR_IS_OK(wbc_status)) {
1700 if (flags & WBFLAG_PAM_INFO3_TEXT) {
1701 if (info && info->info && info->info->user_flags &
1702 NETLOGON_CACHED_ACCOUNT) {
1703 d_printf("user_flgs: "
1704 "NETLOGON_CACHED_ACCOUNT\n");
1708 if (info) {
1709 size_t i;
1710 for (i=0; i < info->num_blobs; i++) {
1711 if (strequal(info->blobs[i].name,
1712 "krb5ccname")) {
1713 d_printf("credentials were put "
1714 "in: %s\n",
1715 (const char *)
1716 info->blobs[i].blob.data);
1717 break;
1720 } else {
1721 d_printf("no credentials cached\n");
1724 done:
1726 wbcFreeMemory(params.blobs);
1728 return WBC_ERROR_IS_OK(wbc_status);
1731 /* Authenticate a user with a plaintext password */
1733 static bool wbinfo_auth(char *username)
1735 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1736 char *s = NULL;
1737 char *p = NULL;
1738 char *password = NULL;
1739 char *name = NULL;
1740 TALLOC_CTX *frame = talloc_tos();
1742 if ((s = talloc_strdup(frame, username)) == NULL) {
1743 return false;
1746 if ((p = strchr(s, '%')) != NULL) {
1747 *p = 0;
1748 p++;
1749 password = talloc_strdup(frame, p);
1750 } else {
1751 password = wbinfo_prompt_pass(frame, NULL, username);
1754 name = s;
1756 wbc_status = wbcAuthenticateUser(name, password);
1758 d_printf("plaintext password authentication %s\n",
1759 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1761 #if 0
1762 if (response.data.auth.nt_status)
1763 d_fprintf(stderr,
1764 "error code was %s (0x%x)\nerror message was: %s\n",
1765 response.data.auth.nt_status_string,
1766 response.data.auth.nt_status,
1767 response.data.auth.error_string);
1768 #endif
1770 return WBC_ERROR_IS_OK(wbc_status);
1773 /* Authenticate a user with a challenge/response */
1775 static bool wbinfo_auth_crap(char *username, bool use_ntlmv2, bool use_lanman)
1777 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1778 struct wbcAuthUserParams params;
1779 struct wbcAuthUserInfo *info = NULL;
1780 struct wbcAuthErrorInfo *err = NULL;
1781 DATA_BLOB lm = data_blob_null;
1782 DATA_BLOB nt = data_blob_null;
1783 fstring name_user;
1784 fstring name_domain;
1785 char *pass;
1786 char *p;
1787 TALLOC_CTX *frame = talloc_tos();
1789 p = strchr(username, '%');
1791 if (p) {
1792 *p = 0;
1793 pass = talloc_strdup(frame, p + 1);
1794 } else {
1795 pass = wbinfo_prompt_pass(frame, NULL, username);
1798 parse_wbinfo_domain_user(username, name_domain, name_user);
1800 params.account_name = name_user;
1801 params.domain_name = name_domain;
1802 params.workstation_name = NULL;
1804 params.flags = 0;
1805 params.parameter_control= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
1806 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
1808 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
1810 generate_random_buffer(params.password.response.challenge, 8);
1812 if (use_ntlmv2) {
1813 DATA_BLOB server_chal;
1814 DATA_BLOB names_blob;
1815 const char *netbios_name = NULL;
1816 const char *domain = NULL;
1818 netbios_name = get_winbind_netbios_name(),
1819 domain = get_winbind_domain();
1820 if (domain == NULL) {
1821 d_fprintf(stderr, "Failed to get domain from winbindd\n");
1822 return false;
1825 server_chal = data_blob(params.password.response.challenge, 8);
1827 /* Pretend this is a login to 'us', for blob purposes */
1828 names_blob = NTLMv2_generate_names_blob(NULL,
1829 netbios_name,
1830 domain);
1832 if (pass != NULL &&
1833 !SMBNTLMv2encrypt(NULL, name_user, name_domain, pass,
1834 &server_chal,
1835 &names_blob,
1836 &lm, &nt, NULL, NULL)) {
1837 data_blob_free(&names_blob);
1838 data_blob_free(&server_chal);
1839 TALLOC_FREE(pass);
1840 return false;
1842 data_blob_free(&names_blob);
1843 data_blob_free(&server_chal);
1845 } else {
1846 if (use_lanman) {
1847 bool ok;
1848 lm = data_blob(NULL, 24);
1849 ok = SMBencrypt(pass,
1850 params.password.response.challenge,
1851 lm.data);
1852 if (!ok) {
1853 data_blob_free(&lm);
1856 nt = data_blob(NULL, 24);
1857 SMBNTencrypt(pass, params.password.response.challenge,
1858 nt.data);
1861 params.password.response.nt_length = nt.length;
1862 params.password.response.nt_data = nt.data;
1863 params.password.response.lm_length = lm.length;
1864 params.password.response.lm_data = lm.data;
1866 wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
1868 /* Display response */
1870 d_printf("challenge/response password authentication %s\n",
1871 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1873 if (wbc_status == WBC_ERR_AUTH_ERROR) {
1874 d_fprintf(stderr,
1875 "wbcAuthenticateUserEx(%s%c%s): error code was "
1876 "%s (0x%x, authoritative=%"PRIu8")\n"
1877 "error message was: %s\n",
1878 name_domain,
1879 winbind_separator(),
1880 name_user,
1881 err->nt_string,
1882 err->nt_status,
1883 err->authoritative,
1884 err->display_string);
1885 wbcFreeMemory(err);
1886 } else if (WBC_ERROR_IS_OK(wbc_status)) {
1887 wbcFreeMemory(info);
1890 data_blob_free(&nt);
1891 data_blob_free(&lm);
1893 return WBC_ERROR_IS_OK(wbc_status);
1896 /* Authenticate a user with a plaintext password */
1898 static bool wbinfo_pam_logon(char *username, bool verbose)
1900 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1901 struct wbcLogonUserParams params;
1902 struct wbcLogonUserInfo *info = NULL;
1903 struct wbcAuthErrorInfo *error = NULL;
1904 char *s = NULL;
1905 char *p = NULL;
1906 TALLOC_CTX *frame = talloc_tos();
1907 uint32_t flags;
1908 uint32_t uid;
1910 ZERO_STRUCT(params);
1912 if ((s = talloc_strdup(frame, username)) == NULL) {
1913 return false;
1916 if ((p = strchr(s, '%')) != NULL) {
1917 *p = 0;
1918 p++;
1919 params.password = talloc_strdup(frame, p);
1920 } else {
1921 params.password = wbinfo_prompt_pass(frame, NULL, username);
1923 params.username = s;
1925 flags = WBFLAG_PAM_CACHED_LOGIN;
1927 wbc_status = wbcAddNamedBlob(&params.num_blobs, &params.blobs,
1928 "flags", 0,
1929 (uint8_t *)&flags, sizeof(flags));
1930 if (!WBC_ERROR_IS_OK(wbc_status)) {
1931 d_printf("wbcAddNamedBlob failed: %s\n",
1932 wbcErrorString(wbc_status));
1933 return false;
1936 uid = getuid();
1938 wbc_status = wbcAddNamedBlob(&params.num_blobs, &params.blobs,
1939 "user_uid", 0,
1940 (uint8_t *)&uid, sizeof(uid));
1941 if (!WBC_ERROR_IS_OK(wbc_status)) {
1942 d_printf("wbcAddNamedBlob failed: %s\n",
1943 wbcErrorString(wbc_status));
1944 return false;
1947 wbc_status = wbcLogonUser(&params, &info, &error, NULL);
1949 if (verbose && (info != NULL)) {
1950 struct wbcAuthUserInfo *i = info->info;
1951 uint32_t j;
1953 if (i->account_name != NULL) {
1954 d_printf("account_name: %s\n", i->account_name);
1956 if (i->user_principal != NULL) {
1957 d_printf("user_principal: %s\n", i->user_principal);
1959 if (i->full_name != NULL) {
1960 d_printf("full_name: %s\n", i->full_name);
1962 if (i->domain_name != NULL) {
1963 d_printf("domain_name: %s\n", i->domain_name);
1965 if (i->dns_domain_name != NULL) {
1966 d_printf("dns_domain_name: %s\n", i->dns_domain_name);
1968 if (i->logon_server != NULL) {
1969 d_printf("logon_server: %s\n", i->logon_server);
1971 if (i->logon_script != NULL) {
1972 d_printf("logon_script: %s\n", i->logon_script);
1974 if (i->profile_path != NULL) {
1975 d_printf("profile_path: %s\n", i->profile_path);
1977 if (i->home_directory != NULL) {
1978 d_printf("home_directory: %s\n", i->home_directory);
1980 if (i->home_drive != NULL) {
1981 d_printf("home_drive: %s\n", i->home_drive);
1984 d_printf("sids:");
1986 for (j=0; j<i->num_sids; j++) {
1987 char buf[WBC_SID_STRING_BUFLEN];
1988 wbcSidToStringBuf(&i->sids[j].sid, buf, sizeof(buf));
1989 d_printf(" %s", buf);
1991 d_printf("\n");
1993 wbcFreeMemory(info);
1994 info = NULL;
1997 wbcFreeMemory(params.blobs);
1999 d_printf("plaintext password authentication %s\n",
2000 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
2002 if (!WBC_ERROR_IS_OK(wbc_status) && (error != NULL)) {
2003 d_fprintf(stderr,
2004 "wbcLogonUser(%s): error code was %s (0x%x)\n"
2005 "error message was: %s\n",
2006 params.username,
2007 error->nt_string,
2008 (int)error->nt_status,
2009 error->display_string);
2010 wbcFreeMemory(error);
2012 return WBC_ERROR_IS_OK(wbc_status);
2015 /* Save creds with winbind */
2017 static bool wbinfo_ccache_save(char *username)
2019 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
2020 char *s = NULL;
2021 char *p = NULL;
2022 char *password = NULL;
2023 char *name = NULL;
2024 TALLOC_CTX *frame = talloc_stackframe();
2026 s = talloc_strdup(frame, username);
2027 if (s == NULL) {
2028 return false;
2031 p = strchr(s, '%');
2032 if (p != NULL) {
2033 *p = 0;
2034 p++;
2035 password = talloc_strdup(frame, p);
2036 } else {
2037 password = wbinfo_prompt_pass(frame, NULL, username);
2040 name = s;
2042 wbc_status = wbcCredentialSave(name, password);
2044 d_printf("saving creds %s\n",
2045 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
2047 TALLOC_FREE(frame);
2049 return WBC_ERROR_IS_OK(wbc_status);
2052 #ifdef WITH_FAKE_KASERVER
2053 /* Authenticate a user with a plaintext password and set a token */
2055 static bool wbinfo_klog(char *username)
2057 struct winbindd_request request;
2058 struct winbindd_response response;
2059 NSS_STATUS result;
2060 char *p;
2062 /* Send off request */
2064 ZERO_STRUCT(request);
2065 ZERO_STRUCT(response);
2067 p = strchr(username, '%');
2069 if (p) {
2070 *p = 0;
2071 fstrcpy(request.data.auth.user, username);
2072 fstrcpy(request.data.auth.pass, p + 1);
2073 *p = '%';
2074 } else {
2075 fstrcpy(request.data.auth.user, username);
2076 (void) samba_getpass("Password: ",
2077 request.data.auth.pass,
2078 sizeof(request.data.auth.pass),
2079 false, false);
2082 request.flags |= WBFLAG_PAM_AFS_TOKEN;
2084 result = winbindd_request_response(NULL, WINBINDD_PAM_AUTH, &request,
2085 &response);
2087 /* Display response */
2089 d_printf("plaintext password authentication %s\n",
2090 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
2092 if (response.data.auth.nt_status)
2093 d_fprintf(stderr,
2094 "error code was %s (0x%x)\nerror message was: %s\n",
2095 response.data.auth.nt_status_string,
2096 response.data.auth.nt_status,
2097 response.data.auth.error_string);
2099 if (result != NSS_STATUS_SUCCESS)
2100 return false;
2102 if (response.extra_data.data == NULL) {
2103 d_fprintf(stderr, "Did not get token data\n");
2104 return false;
2107 if (!afs_settoken_str((char *)response.extra_data.data)) {
2108 d_fprintf(stderr, "Could not set token\n");
2109 return false;
2112 d_printf("Successfully created AFS token\n");
2113 return true;
2115 #else
2116 static bool wbinfo_klog(char *username)
2118 d_fprintf(stderr, "No AFS support compiled in.\n");
2119 return false;
2121 #endif
2123 /* Print domain users */
2125 static bool print_domain_users(const char *domain)
2127 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
2128 uint32_t i;
2129 uint32_t num_users = 0;
2130 const char **users = NULL;
2132 /* Send request to winbind daemon */
2134 if (domain == NULL) {
2135 domain = get_winbind_domain();
2136 } else {
2137 /* '.' is the special sign for our own domain */
2138 if ((domain[0] == '\0') || strcmp(domain, ".") == 0) {
2139 domain = get_winbind_domain();
2140 /* '*' is the special sign for all domains */
2141 } else if (strcmp(domain, "*") == 0) {
2142 domain = NULL;
2146 wbc_status = wbcListUsers(domain, &num_users, &users);
2147 if (!WBC_ERROR_IS_OK(wbc_status)) {
2148 return false;
2151 for (i=0; i < num_users; i++) {
2152 d_printf("%s\n", users[i]);
2155 wbcFreeMemory(users);
2157 return true;
2160 /* Print domain groups */
2162 static bool print_domain_groups(const char *domain)
2164 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
2165 uint32_t i;
2166 uint32_t num_groups = 0;
2167 const char **groups = NULL;
2169 /* Send request to winbind daemon */
2171 if (domain == NULL) {
2172 domain = get_winbind_domain();
2173 } else {
2174 /* '.' is the special sign for our own domain */
2175 if ((domain[0] == '\0') || strcmp(domain, ".") == 0) {
2176 domain = get_winbind_domain();
2177 /* '*' is the special sign for all domains */
2178 } else if (strcmp(domain, "*") == 0) {
2179 domain = NULL;
2183 wbc_status = wbcListGroups(domain, &num_groups, &groups);
2184 if (!WBC_ERROR_IS_OK(wbc_status)) {
2185 d_fprintf(stderr, "failed to call wbcListGroups: %s\n",
2186 wbcErrorString(wbc_status));
2187 return false;
2190 for (i=0; i < num_groups; i++) {
2191 d_printf("%s\n", groups[i]);
2194 wbcFreeMemory(groups);
2196 return true;
2199 /* Set the authorised user for winbindd access in secrets.tdb */
2201 static bool wbinfo_set_auth_user(char *username)
2203 d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
2204 "See 'net help setauthuser' for details.\n");
2205 return false;
2208 static void wbinfo_get_auth_user(void)
2210 d_fprintf(stderr, "This functionality was moved to the 'net' utility.\n"
2211 "See 'net help getauthuser' for details.\n");
2214 static bool wbinfo_ping(void)
2216 wbcErr wbc_status;
2218 wbc_status = wbcPing();
2220 /* Display response */
2222 d_printf("Ping to winbindd %s\n",
2223 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
2225 return WBC_ERROR_IS_OK(wbc_status);
2228 static bool wbinfo_change_user_password(const char *username)
2230 wbcErr wbc_status;
2231 char *old_password = NULL;
2232 char *new_password = NULL;
2233 TALLOC_CTX *frame = talloc_tos();
2235 old_password = wbinfo_prompt_pass(frame, "old", username);
2236 new_password = wbinfo_prompt_pass(frame, "new", username);
2238 wbc_status = wbcChangeUserPassword(username, old_password,new_password);
2240 /* Display response */
2242 d_printf("Password change for user %s %s\n", username,
2243 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
2245 return WBC_ERROR_IS_OK(wbc_status);
2248 /* Main program */
2250 enum {
2251 OPT_SET_AUTH_USER = 1000,
2252 OPT_GET_AUTH_USER,
2253 OPT_DOMAIN_NAME,
2254 OPT_SEQUENCE,
2255 OPT_GETDCNAME,
2256 OPT_DSGETDCNAME,
2257 OPT_DC_INFO,
2258 OPT_USERDOMGROUPS,
2259 OPT_SIDALIASES,
2260 OPT_USERSIDS,
2261 OPT_LOOKUP_SIDS,
2262 OPT_ALLOCATE_UID,
2263 OPT_ALLOCATE_GID,
2264 OPT_SET_UID_MAPPING,
2265 OPT_SET_GID_MAPPING,
2266 OPT_REMOVE_UID_MAPPING,
2267 OPT_REMOVE_GID_MAPPING,
2268 OPT_SIDS_TO_XIDS,
2269 OPT_XIDS_TO_SIDS,
2270 OPT_SEPARATOR,
2271 OPT_LIST_ALL_DOMAINS,
2272 OPT_LIST_OWN_DOMAIN,
2273 OPT_UID_INFO,
2274 OPT_USER_SIDINFO,
2275 OPT_GROUP_INFO,
2276 OPT_GID_INFO,
2277 OPT_VERBOSE,
2278 OPT_ONLINESTATUS,
2279 OPT_CHANGE_USER_PASSWORD,
2280 OPT_CCACHE_SAVE,
2281 OPT_SID_TO_FULLNAME,
2282 OPT_NTLMV1,
2283 OPT_NTLMV2,
2284 OPT_PAM_LOGON,
2285 OPT_LOGOFF,
2286 OPT_LOGOFF_USER,
2287 OPT_LOGOFF_UID,
2288 OPT_LANMAN,
2289 OPT_KRB5CCNAME
2292 int main(int argc, const char **argv, char **envp)
2294 int opt;
2295 TALLOC_CTX *frame = talloc_stackframe();
2296 poptContext pc;
2297 static char *string_arg;
2298 char *string_subarg = NULL;
2299 static char *opt_domain_name;
2300 static int int_arg;
2301 int int_subarg = -1;
2302 int result = 1;
2303 bool verbose = false;
2304 bool use_ntlmv2 = true;
2305 bool use_lanman = false;
2306 char *logoff_user = getenv("USER");
2307 int logoff_uid = geteuid();
2308 const char *opt_krb5ccname = "FILE";
2310 struct poptOption long_options[] = {
2311 POPT_AUTOHELP
2313 /* longName, shortName, argInfo, argPtr, value, descrip,
2314 argDesc */
2317 .longName = "domain-users",
2318 .shortName = 'u',
2319 .argInfo = POPT_ARG_NONE,
2320 .val = 'u',
2321 .descrip = "Lists all domain users",
2322 .argDescrip = "domain"
2325 .longName = "domain-groups",
2326 .shortName = 'g',
2327 .argInfo = POPT_ARG_NONE,
2328 .val = 'g',
2329 .descrip = "Lists all domain groups",
2330 .argDescrip = "domain"
2333 .longName = "WINS-by-name",
2334 .shortName = 'N',
2335 .argInfo = POPT_ARG_STRING,
2336 .arg = &string_arg,
2337 .val = 'N',
2338 .descrip = "Converts NetBIOS name to IP",
2339 .argDescrip = "NETBIOS-NAME"
2342 .longName = "WINS-by-ip",
2343 .shortName = 'I',
2344 .argInfo = POPT_ARG_STRING,
2345 .arg = &string_arg,
2346 .val = 'I',
2347 .descrip = "Converts IP address to NetBIOS name",
2348 .argDescrip = "IP"
2351 .longName = "name-to-sid",
2352 .shortName = 'n',
2353 .argInfo = POPT_ARG_STRING,
2354 .arg = &string_arg,
2355 .val = 'n',
2356 .descrip = "Converts name to sid",
2357 .argDescrip = "NAME"
2360 .longName = "sid-to-name",
2361 .shortName = 's',
2362 .argInfo = POPT_ARG_STRING,
2363 .arg = &string_arg,
2364 .val = 's',
2365 .descrip = "Converts sid to name",
2366 .argDescrip = "SID"
2369 .longName = "sid-to-fullname",
2370 .argInfo = POPT_ARG_STRING,
2371 .arg = &string_arg,
2372 .val = OPT_SID_TO_FULLNAME,
2373 .descrip = "Converts sid to fullname",
2374 .argDescrip = "SID"
2377 .longName = "lookup-rids",
2378 .shortName = 'R',
2379 .argInfo = POPT_ARG_STRING,
2380 .arg = &string_arg,
2381 .val = 'R',
2382 .descrip = "Converts RIDs to names",
2383 .argDescrip = "RIDs"
2386 .longName = "lookup-sids",
2387 .argInfo = POPT_ARG_STRING,
2388 .arg = &string_arg,
2389 .val = OPT_LOOKUP_SIDS,
2390 .descrip = "Converts SIDs to types and names",
2391 .argDescrip = "Sid-List"
2394 .longName = "uid-to-sid",
2395 .shortName = 'U',
2396 .argInfo = POPT_ARG_INT,
2397 .arg = &int_arg,
2398 .val = 'U',
2399 .descrip = "Converts uid to sid",
2400 .argDescrip = "UID"
2403 .longName = "gid-to-sid",
2404 .shortName = 'G',
2405 .argInfo = POPT_ARG_INT,
2406 .arg = &int_arg,
2407 .val = 'G',
2408 .descrip = "Converts gid to sid",
2409 .argDescrip = "GID"
2412 .longName = "sid-to-uid",
2413 .shortName = 'S',
2414 .argInfo = POPT_ARG_STRING,
2415 .arg = &string_arg,
2416 .val = 'S',
2417 .descrip = "Converts sid to uid",
2418 .argDescrip = "SID"
2421 .longName = "sid-to-gid",
2422 .shortName = 'Y',
2423 .argInfo = POPT_ARG_STRING,
2424 .arg = &string_arg,
2425 .val = 'Y',
2426 .descrip = "Converts sid to gid",
2427 .argDescrip = "SID"
2430 .longName = "allocate-uid",
2431 .argInfo = POPT_ARG_NONE,
2432 .val = OPT_ALLOCATE_UID,
2433 .descrip = "Get a new UID out of idmap"
2436 .longName = "allocate-gid",
2437 .argInfo = POPT_ARG_NONE,
2438 .val = OPT_ALLOCATE_GID,
2439 .descrip = "Get a new GID out of idmap"
2442 .longName = "set-uid-mapping",
2443 .argInfo = POPT_ARG_STRING,
2444 .arg = &string_arg,
2445 .val = OPT_SET_UID_MAPPING,
2446 .descrip = "Create or modify uid to sid mapping in "
2447 "idmap",
2448 .argDescrip = "UID,SID"
2451 .longName = "set-gid-mapping",
2452 .argInfo = POPT_ARG_STRING,
2453 .arg = &string_arg,
2454 .val = OPT_SET_GID_MAPPING,
2455 .descrip = "Create or modify gid to sid mapping in "
2456 "idmap",
2457 .argDescrip = "GID,SID"
2460 .longName = "remove-uid-mapping",
2461 .argInfo = POPT_ARG_STRING,
2462 .arg = &string_arg,
2463 .val = OPT_REMOVE_UID_MAPPING,
2464 .descrip = "Remove uid to sid mapping in idmap",
2465 .argDescrip = "UID,SID"
2468 .longName = "remove-gid-mapping",
2469 .argInfo = POPT_ARG_STRING,
2470 .arg = &string_arg,
2471 .val = OPT_REMOVE_GID_MAPPING,
2472 .descrip = "Remove gid to sid mapping in idmap",
2473 .argDescrip = "GID,SID",
2476 .longName = "sids-to-unix-ids",
2477 .argInfo = POPT_ARG_STRING,
2478 .arg = &string_arg,
2479 .val = OPT_SIDS_TO_XIDS,
2480 .descrip = "Translate SIDs to Unix IDs",
2481 .argDescrip = "Sid-List",
2484 .longName = "unix-ids-to-sids",
2485 .argInfo = POPT_ARG_STRING,
2486 .arg = &string_arg,
2487 .val = OPT_XIDS_TO_SIDS,
2488 .descrip = "Translate Unix IDs to SIDs",
2489 .argDescrip = "ID-List (u<num> g<num>)",
2492 .longName = "check-secret",
2493 .shortName = 't',
2494 .argInfo = POPT_ARG_NONE,
2495 .val = 't',
2496 .descrip = "Check shared secret",
2499 .longName = "change-secret",
2500 .shortName = 'c',
2501 .argInfo = POPT_ARG_NONE,
2502 .val = 'c',
2503 .descrip = "Change shared secret",
2506 .longName = "ping-dc",
2507 .shortName = 'P',
2508 .argInfo = POPT_ARG_NONE,
2509 .val = 'P',
2510 .descrip = "Check the NETLOGON connection",
2513 .longName = "trusted-domains",
2514 .shortName = 'm',
2515 .argInfo = POPT_ARG_NONE,
2516 .val = 'm',
2517 .descrip = "List trusted domains",
2520 .longName = "all-domains",
2521 .argInfo = POPT_ARG_NONE,
2522 .val = OPT_LIST_ALL_DOMAINS,
2523 .descrip = "List all domains (trusted and own "
2524 "domain)",
2527 .longName = "own-domain",
2528 .argInfo = POPT_ARG_NONE,
2529 .val = OPT_LIST_OWN_DOMAIN,
2530 .descrip = "List own domain",
2533 .longName = "sequence",
2534 .argInfo = POPT_ARG_NONE,
2535 .val = OPT_SEQUENCE,
2536 .descrip = "Deprecated command, see --online-status",
2539 .longName = "online-status",
2540 .argInfo = POPT_ARG_NONE,
2541 .val = OPT_ONLINESTATUS,
2542 .descrip = "Show whether domains maintain an active "
2543 "connection",
2546 .longName = "domain-info",
2547 .shortName = 'D',
2548 .argInfo = POPT_ARG_STRING,
2549 .arg = &string_arg,
2550 .val = 'D',
2551 .descrip = "Show most of the info we have about the "
2552 "domain",
2555 .longName = "user-info",
2556 .shortName = 'i',
2557 .argInfo = POPT_ARG_STRING,
2558 .arg = &string_arg,
2559 .val = 'i',
2560 .descrip = "Get user info",
2561 .argDescrip = "USER",
2564 .longName = "uid-info",
2565 .argInfo = POPT_ARG_INT,
2566 .arg = &int_arg,
2567 .val = OPT_UID_INFO,
2568 .descrip = "Get user info from uid",
2569 .argDescrip = "UID",
2572 .longName = "group-info",
2573 .argInfo = POPT_ARG_STRING,
2574 .arg = &string_arg,
2575 .val = OPT_GROUP_INFO,
2576 .descrip = "Get group info",
2577 .argDescrip = "GROUP",
2580 .longName = "user-sidinfo",
2581 .argInfo = POPT_ARG_STRING,
2582 .arg = &string_arg,
2583 .val = OPT_USER_SIDINFO,
2584 .descrip = "Get user info from sid",
2585 .argDescrip = "SID",
2588 .longName = "gid-info",
2589 .argInfo = POPT_ARG_INT,
2590 .arg = &int_arg,
2591 .val = OPT_GID_INFO,
2592 .descrip = "Get group info from gid",
2593 .argDescrip = "GID",
2596 .longName = "user-groups",
2597 .shortName = 'r',
2598 .argInfo = POPT_ARG_STRING,
2599 .arg = &string_arg,
2600 .val = 'r',
2601 .descrip = "Get user groups",
2602 .argDescrip = "USER",
2605 .longName = "user-domgroups",
2606 .argInfo = POPT_ARG_STRING,
2607 .arg = &string_arg,
2608 .val = OPT_USERDOMGROUPS,
2609 .descrip = "Get user domain groups",
2610 .argDescrip = "SID",
2613 .longName = "sid-aliases",
2614 .argInfo = POPT_ARG_STRING,
2615 .arg = &string_arg,
2616 .val = OPT_SIDALIASES,
2617 .descrip = "Get sid aliases",
2618 .argDescrip = "SID",
2621 .longName = "user-sids",
2622 .argInfo = POPT_ARG_STRING,
2623 .arg = &string_arg,
2624 .val = OPT_USERSIDS,
2625 .descrip = "Get user group sids for user SID",
2626 .argDescrip = "SID",
2629 .longName = "authenticate",
2630 .shortName = 'a',
2631 .argInfo = POPT_ARG_STRING,
2632 .arg = &string_arg,
2633 .val = 'a',
2634 .descrip = "authenticate user",
2635 .argDescrip = "user%password",
2638 .longName = "pam-logon",
2639 .argInfo = POPT_ARG_STRING,
2640 .arg = &string_arg,
2641 .val = OPT_PAM_LOGON,
2642 .descrip = "do a pam logon equivalent",
2643 .argDescrip = "user%password",
2646 .longName = "logoff",
2647 .argInfo = POPT_ARG_NONE,
2648 .val = OPT_LOGOFF,
2649 .descrip = "log off user",
2650 .argDescrip = "uid",
2653 .longName = "logoff-user",
2654 .argInfo = POPT_ARG_STRING,
2655 .arg = &logoff_user,
2656 .val = OPT_LOGOFF_USER,
2657 .descrip = "username to log off"
2660 .longName = "logoff-uid",
2661 .argInfo = POPT_ARG_INT,
2662 .arg = &logoff_uid,
2663 .val = OPT_LOGOFF_UID,
2664 .descrip = "uid to log off",
2667 .longName = "set-auth-user",
2668 .argInfo = POPT_ARG_STRING,
2669 .arg = &string_arg,
2670 .val = OPT_SET_AUTH_USER,
2671 .descrip = "Store user and password used by "
2672 "winbindd (root only)",
2673 .argDescrip = "user%password",
2676 .longName = "ccache-save",
2677 .shortName = 0,
2678 .argInfo = POPT_ARG_STRING,
2679 .arg = &string_arg,
2680 .val = OPT_CCACHE_SAVE,
2681 .descrip = "Store user and password for ccache "
2682 "operation",
2683 .argDescrip = "user%password",
2686 .longName = "getdcname",
2687 .argInfo = POPT_ARG_STRING,
2688 .arg = &string_arg,
2689 .val = OPT_GETDCNAME,
2690 .descrip = "Get a DC name for a foreign domain",
2691 .argDescrip = "domainname",
2694 .longName = "dsgetdcname",
2695 .argInfo = POPT_ARG_STRING,
2696 .arg = &string_arg,
2697 .val = OPT_DSGETDCNAME,
2698 .descrip = "Find a DC for a domain",
2699 .argDescrip = "domainname",
2702 .longName = "dc-info",
2703 .argInfo = POPT_ARG_STRING,
2704 .arg = &string_arg,
2705 .val = OPT_DC_INFO,
2706 .descrip = "Find the currently known DCs",
2707 .argDescrip = "domainname",
2710 .longName = "get-auth-user",
2711 .argInfo = POPT_ARG_NONE,
2712 .val = OPT_GET_AUTH_USER,
2713 .descrip = "Retrieve user and password used by "
2714 "winbindd (root only)",
2717 .longName = "ping",
2718 .shortName = 'p',
2719 .argInfo = POPT_ARG_NONE,
2720 .arg = 0,
2721 .val = 'p',
2722 .descrip = "Ping winbindd to see if it is alive",
2725 .longName = "domain",
2726 .shortName = 0,
2727 .argInfo = POPT_ARG_STRING,
2728 .arg = &opt_domain_name,
2729 .val = OPT_DOMAIN_NAME,
2730 .descrip = "Define to the domain to restrict "
2731 "operation",
2732 .argDescrip = "domain",
2734 #ifdef WITH_FAKE_KASERVER
2736 .longName = "klog",
2737 .shortName = 'k',
2738 .argInfo = POPT_ARG_STRING,
2739 .arg = &string_arg,
2740 .val = 'k',
2741 .descrip = "set an AFS token from winbind",
2742 .argDescrip = "user%password",
2744 #endif
2745 #ifdef HAVE_KRB5
2747 .longName = "krb5auth",
2748 .shortName = 'K',
2749 .argInfo = POPT_ARG_STRING,
2750 .arg = &string_arg,
2751 .val = 'K',
2752 .descrip = "authenticate user using Kerberos",
2753 .argDescrip = "user%password",
2755 /* destroys wbinfo --help output */
2756 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" },
2759 .longName = "krb5ccname",
2760 .argInfo = POPT_ARG_STRING,
2761 .arg = &opt_krb5ccname,
2762 .val = OPT_KRB5CCNAME,
2763 .descrip = "authenticate user using Kerberos and "
2764 "specific credential cache type",
2765 .argDescrip = "krb5ccname",
2767 #endif
2769 .longName = "separator",
2770 .argInfo = POPT_ARG_NONE,
2771 .val = OPT_SEPARATOR,
2772 .descrip = "Get the active winbind separator",
2775 .longName = "verbose",
2776 .argInfo = POPT_ARG_NONE,
2777 .val = OPT_VERBOSE,
2778 .descrip = "Print additional information per command",
2781 .longName = "change-user-password",
2782 .argInfo = POPT_ARG_STRING,
2783 .arg = &string_arg,
2784 .val = OPT_CHANGE_USER_PASSWORD,
2785 .descrip = "Change the password for a user",
2788 .longName = "ntlmv1",
2789 .argInfo = POPT_ARG_NONE,
2790 .val = OPT_NTLMV1,
2791 .descrip = "Use NTLMv1 cryptography for user authentication",
2794 .longName = "ntlmv2",
2795 .argInfo = POPT_ARG_NONE,
2796 .val = OPT_NTLMV2,
2797 .descrip = "Use NTLMv2 cryptography for user authentication",
2800 .longName = "lanman",
2801 .argInfo = POPT_ARG_NONE,
2802 .val = OPT_LANMAN,
2803 .descrip = "Use lanman cryptography for user authentication",
2805 POPT_COMMON_VERSION
2806 POPT_TABLEEND
2809 /* Samba client initialisation */
2810 smb_init_locale();
2813 /* Parse options */
2815 pc = samba_popt_get_context(getprogname(),
2816 argc,
2817 argv,
2818 long_options,
2820 if (pc == NULL) {
2821 DBG_ERR("Failed to setup popt context!\n");
2822 exit(1);
2825 /* Parse command line options */
2827 if (argc == 1) {
2828 poptPrintHelp(pc, stderr, 0);
2829 return 1;
2832 while((opt = poptGetNextOpt(pc)) != -1) {
2833 /* get the generic configuration parameters like --domain */
2834 switch (opt) {
2835 case OPT_VERBOSE:
2836 verbose = true;
2837 break;
2838 case OPT_NTLMV1:
2839 use_ntlmv2 = false;
2840 break;
2841 case OPT_LANMAN:
2842 use_lanman = true;
2843 break;
2847 poptFreeContext(pc);
2849 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2850 POPT_CONTEXT_KEEP_FIRST);
2852 while((opt = poptGetNextOpt(pc)) != -1) {
2853 switch (opt) {
2854 case 'u':
2855 if (!print_domain_users(opt_domain_name)) {
2856 d_fprintf(stderr,
2857 "Error looking up domain users\n");
2858 goto done;
2860 break;
2861 case 'g':
2862 if (!print_domain_groups(opt_domain_name)) {
2863 d_fprintf(stderr,
2864 "Error looking up domain groups\n");
2865 goto done;
2867 break;
2868 case 's':
2869 if (!wbinfo_lookupsid(string_arg)) {
2870 d_fprintf(stderr,
2871 "Could not lookup sid %s\n",
2872 string_arg);
2873 goto done;
2875 break;
2876 case OPT_SID_TO_FULLNAME:
2877 if (!wbinfo_lookupsid_fullname(string_arg)) {
2878 d_fprintf(stderr, "Could not lookup sid %s\n",
2879 string_arg);
2880 goto done;
2882 break;
2883 case 'R':
2884 if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
2885 d_fprintf(stderr, "Could not lookup RIDs %s\n",
2886 string_arg);
2887 goto done;
2889 break;
2890 case OPT_LOOKUP_SIDS:
2891 if (!wbinfo_lookup_sids(string_arg)) {
2892 d_fprintf(stderr, "Could not lookup SIDs %s\n",
2893 string_arg);
2894 goto done;
2896 break;
2897 case 'n':
2898 if (!wbinfo_lookupname(string_arg)) {
2899 d_fprintf(stderr, "Could not lookup name %s\n",
2900 string_arg);
2901 goto done;
2903 break;
2904 case 'N':
2905 if (!wbinfo_wins_byname(string_arg)) {
2906 d_fprintf(stderr,
2907 "Could not lookup WINS by name %s\n",
2908 string_arg);
2909 goto done;
2911 break;
2912 case 'I':
2913 if (!wbinfo_wins_byip(string_arg)) {
2914 d_fprintf(stderr,
2915 "Could not lookup WINS by IP %s\n",
2916 string_arg);
2917 goto done;
2919 break;
2920 case 'U':
2921 if (!wbinfo_uid_to_sid(int_arg)) {
2922 d_fprintf(stderr,
2923 "Could not convert uid %d to sid\n",
2924 int_arg);
2925 goto done;
2927 break;
2928 case 'G':
2929 if (!wbinfo_gid_to_sid(int_arg)) {
2930 d_fprintf(stderr,
2931 "Could not convert gid %d to sid\n",
2932 int_arg);
2933 goto done;
2935 break;
2936 case 'S':
2937 if (!wbinfo_sid_to_uid(string_arg)) {
2938 d_fprintf(stderr,
2939 "Could not convert sid %s to uid\n",
2940 string_arg);
2941 goto done;
2943 break;
2944 case 'Y':
2945 if (!wbinfo_sid_to_gid(string_arg)) {
2946 d_fprintf(stderr,
2947 "Could not convert sid %s to gid\n",
2948 string_arg);
2949 goto done;
2951 break;
2952 case OPT_ALLOCATE_UID:
2953 if (!wbinfo_allocate_uid()) {
2954 d_fprintf(stderr, "Could not allocate a uid\n");
2955 goto done;
2957 break;
2958 case OPT_ALLOCATE_GID:
2959 if (!wbinfo_allocate_gid()) {
2960 d_fprintf(stderr, "Could not allocate a gid\n");
2961 goto done;
2963 break;
2964 case OPT_SET_UID_MAPPING:
2965 if (!parse_mapping_arg(string_arg, &int_subarg,
2966 &string_subarg) ||
2967 !wbinfo_set_uid_mapping(int_subarg, string_subarg))
2969 d_fprintf(stderr, "Could not create or modify "
2970 "uid to sid mapping\n");
2971 goto done;
2973 break;
2974 case OPT_SET_GID_MAPPING:
2975 if (!parse_mapping_arg(string_arg, &int_subarg,
2976 &string_subarg) ||
2977 !wbinfo_set_gid_mapping(int_subarg, string_subarg))
2979 d_fprintf(stderr, "Could not create or modify "
2980 "gid to sid mapping\n");
2981 goto done;
2983 break;
2984 case OPT_REMOVE_UID_MAPPING:
2985 if (!parse_mapping_arg(string_arg, &int_subarg,
2986 &string_subarg) ||
2987 !wbinfo_remove_uid_mapping(int_subarg,
2988 string_subarg))
2990 d_fprintf(stderr, "Could not remove uid to sid "
2991 "mapping\n");
2992 goto done;
2994 break;
2995 case OPT_REMOVE_GID_MAPPING:
2996 if (!parse_mapping_arg(string_arg, &int_subarg,
2997 &string_subarg) ||
2998 !wbinfo_remove_gid_mapping(int_subarg,
2999 string_subarg))
3001 d_fprintf(stderr, "Could not remove gid to sid "
3002 "mapping\n");
3003 goto done;
3005 break;
3006 case OPT_SIDS_TO_XIDS:
3007 if (!wbinfo_sids_to_unix_ids(string_arg)) {
3008 d_fprintf(stderr, "wbinfo_sids_to_unix_ids "
3009 "failed\n");
3010 goto done;
3012 break;
3013 case OPT_XIDS_TO_SIDS:
3014 if (!wbinfo_xids_to_sids(string_arg)) {
3015 d_fprintf(stderr, "wbinfo_xids_to_sids "
3016 "failed\n");
3017 goto done;
3019 break;
3020 case 't':
3021 if (!wbinfo_check_secret(opt_domain_name)) {
3022 d_fprintf(stderr, "Could not check secret\n");
3023 goto done;
3025 break;
3026 case 'c':
3027 if (!wbinfo_change_secret(opt_domain_name)) {
3028 d_fprintf(stderr, "Could not change secret\n");
3029 goto done;
3031 break;
3032 case 'P':
3033 if (!wbinfo_ping_dc(opt_domain_name)) {
3034 goto done;
3036 break;
3037 case 'm':
3038 if (!wbinfo_list_domains(false, verbose)) {
3039 d_fprintf(stderr,
3040 "Could not list trusted domains\n");
3041 goto done;
3043 break;
3044 case OPT_SEQUENCE:
3045 if (!wbinfo_show_sequence(opt_domain_name)) {
3046 d_fprintf(stderr,
3047 "Could not show sequence numbers\n");
3048 goto done;
3050 break;
3051 case OPT_ONLINESTATUS:
3052 if (!wbinfo_show_onlinestatus(opt_domain_name)) {
3053 d_fprintf(stderr,
3054 "Could not show online-status\n");
3055 goto done;
3057 break;
3058 case 'D':
3059 if (!wbinfo_domain_info(string_arg)) {
3060 d_fprintf(stderr,
3061 "Could not get domain info\n");
3062 goto done;
3064 break;
3065 case 'i':
3066 if (!wbinfo_get_userinfo(string_arg)) {
3067 d_fprintf(stderr,
3068 "Could not get info for user %s\n",
3069 string_arg);
3070 goto done;
3072 break;
3073 case OPT_USER_SIDINFO:
3074 if ( !wbinfo_get_user_sidinfo(string_arg)) {
3075 d_fprintf(stderr,
3076 "Could not get info for user "
3077 "sid %s\n", string_arg);
3078 goto done;
3080 break;
3081 case OPT_UID_INFO:
3082 if ( !wbinfo_get_uidinfo(int_arg)) {
3083 d_fprintf(stderr, "Could not get info for uid "
3084 "%d\n", int_arg);
3085 goto done;
3087 break;
3088 case OPT_GROUP_INFO:
3089 if ( !wbinfo_get_groupinfo(string_arg)) {
3090 d_fprintf(stderr, "Could not get info for "
3091 "group %s\n", string_arg);
3092 goto done;
3094 break;
3095 case OPT_GID_INFO:
3096 if ( !wbinfo_get_gidinfo(int_arg)) {
3097 d_fprintf(stderr, "Could not get info for gid "
3098 "%d\n", int_arg);
3099 goto done;
3101 break;
3102 case 'r':
3103 if (!wbinfo_get_usergroups(string_arg)) {
3104 d_fprintf(stderr,
3105 "Could not get groups for user %s\n",
3106 string_arg);
3107 goto done;
3109 break;
3110 case OPT_USERSIDS:
3111 if (!wbinfo_get_usersids(string_arg)) {
3112 d_fprintf(stderr, "Could not get group SIDs "
3113 "for user SID %s\n",
3114 string_arg);
3115 goto done;
3117 break;
3118 case OPT_USERDOMGROUPS:
3119 if (!wbinfo_get_userdomgroups(string_arg)) {
3120 d_fprintf(stderr, "Could not get user's domain "
3121 "groups for user SID %s\n",
3122 string_arg);
3123 goto done;
3125 break;
3126 case OPT_SIDALIASES:
3127 if (!wbinfo_get_sidaliases(opt_domain_name,
3128 string_arg)) {
3129 d_fprintf(stderr, "Could not get sid aliases "
3130 "for user SID %s\n", string_arg);
3131 goto done;
3133 break;
3134 case 'a': {
3135 bool got_error = false;
3137 if (!wbinfo_auth(string_arg)) {
3138 d_fprintf(stderr,
3139 "Could not authenticate user "
3140 "%s with plaintext "
3141 "password\n", string_arg);
3142 got_error = true;
3145 if (!wbinfo_auth_crap(string_arg, use_ntlmv2,
3146 use_lanman)) {
3147 d_fprintf(stderr,
3148 "Could not authenticate user "
3149 "%s with challenge/response\n",
3150 string_arg);
3151 got_error = true;
3154 if (got_error)
3155 goto done;
3156 break;
3158 case OPT_PAM_LOGON:
3159 if (!wbinfo_pam_logon(string_arg, verbose)) {
3160 d_fprintf(stderr, "pam_logon failed for %s\n",
3161 string_arg);
3162 goto done;
3164 break;
3165 case OPT_LOGOFF:
3167 wbcErr wbc_status;
3169 wbc_status = wbcLogoffUser(logoff_user, logoff_uid,
3170 "");
3171 d_printf("Logoff %s (%d): %s\n", logoff_user,
3172 logoff_uid, wbcErrorString(wbc_status));
3173 break;
3175 case 'K': {
3176 uint32_t flags = WBFLAG_PAM_KRB5 |
3177 WBFLAG_PAM_CACHED_LOGIN |
3178 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
3179 WBFLAG_PAM_INFO3_TEXT |
3180 WBFLAG_PAM_CONTACT_TRUSTDOM;
3182 if (!wbinfo_auth_krb5(string_arg, opt_krb5ccname,
3183 flags)) {
3184 d_fprintf(stderr,
3185 "Could not authenticate user "
3186 "[%s] with Kerberos "
3187 "(ccache: %s)\n", string_arg,
3188 opt_krb5ccname);
3189 goto done;
3191 break;
3193 case 'k':
3194 if (!wbinfo_klog(string_arg)) {
3195 d_fprintf(stderr, "Could not klog user\n");
3196 goto done;
3198 break;
3199 case 'p':
3200 if (!wbinfo_ping()) {
3201 d_fprintf(stderr, "could not ping winbindd!\n");
3202 goto done;
3204 break;
3205 case OPT_SET_AUTH_USER:
3206 if (!wbinfo_set_auth_user(string_arg)) {
3207 goto done;
3209 break;
3210 case OPT_GET_AUTH_USER:
3211 wbinfo_get_auth_user();
3212 goto done;
3213 break;
3214 case OPT_CCACHE_SAVE:
3215 if (!wbinfo_ccache_save(string_arg)) {
3216 goto done;
3218 break;
3219 case OPT_GETDCNAME:
3220 if (!wbinfo_getdcname(string_arg)) {
3221 goto done;
3223 break;
3224 case OPT_DSGETDCNAME:
3225 if (!wbinfo_dsgetdcname(string_arg, 0)) {
3226 goto done;
3228 break;
3229 case OPT_DC_INFO:
3230 if (!wbinfo_dc_info(string_arg)) {
3231 goto done;
3233 break;
3234 case OPT_SEPARATOR: {
3235 const char sep = winbind_separator();
3236 if ( !sep ) {
3237 goto done;
3239 d_printf("%c\n", sep);
3240 break;
3242 case OPT_LIST_ALL_DOMAINS:
3243 if (!wbinfo_list_domains(true, verbose)) {
3244 goto done;
3246 break;
3247 case OPT_LIST_OWN_DOMAIN:
3248 if (!wbinfo_list_own_domain()) {
3249 goto done;
3251 break;
3252 case OPT_CHANGE_USER_PASSWORD:
3253 if (!wbinfo_change_user_password(string_arg)) {
3254 d_fprintf(stderr,
3255 "Could not change user password "
3256 "for user %s\n", string_arg);
3257 goto done;
3259 break;
3261 /* generic configuration options */
3262 case OPT_DOMAIN_NAME:
3263 case OPT_VERBOSE:
3264 case OPT_NTLMV1:
3265 case OPT_NTLMV2:
3266 case OPT_LANMAN:
3267 case OPT_LOGOFF_USER:
3268 case OPT_LOGOFF_UID:
3269 case OPT_KRB5CCNAME:
3270 break;
3271 default:
3272 d_fprintf(stderr, "Invalid option\n");
3273 poptPrintHelp(pc, stderr, 0);
3274 goto done;
3278 result = 0;
3280 /* Exit code */
3282 done:
3283 talloc_free(frame);
3285 poptFreeContext(pc);
3286 return result;