wbinfo: use wbcAllocateGid()
[Samba.git] / source / nsswitch / wbinfo.c
blob82d1061f6edb2e231a3f16565406194ca830bf77
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett 2002
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "winbind_client.h"
25 #include "libwbclient/wbclient.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
30 static char winbind_separator_int(bool strict)
32 struct winbindd_response response;
33 static bool got_sep;
34 static char sep;
36 if (got_sep)
37 return sep;
39 ZERO_STRUCT(response);
41 /* Send off request */
43 if (winbindd_request_response(WINBINDD_INFO, NULL, &response) !=
44 NSS_STATUS_SUCCESS) {
45 d_fprintf(stderr, "could not obtain winbind separator!\n");
46 if (strict) {
47 return 0;
49 /* HACK: (this module should not call lp_ funtions) */
50 return *lp_winbind_separator();
53 sep = response.data.info.winbind_separator;
54 got_sep = true;
56 if (!sep) {
57 d_fprintf(stderr, "winbind separator was NULL!\n");
58 if (strict) {
59 return 0;
61 /* HACK: (this module should not call lp_ funtions) */
62 sep = *lp_winbind_separator();
65 return sep;
68 static char winbind_separator(void)
70 return winbind_separator_int(false);
73 static const char *get_winbind_domain(void)
75 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
76 struct wbcDomainInfo *dinfo = NULL;
77 static fstring winbind_domain;
79 ZERO_STRUCT(dinfo);
81 wbc_status = wbcDomainInfo(".", &dinfo);
83 if (!WBC_ERROR_IS_OK(wbc_status)) {
84 d_fprintf(stderr, "could not obtain winbind domain name!\n");
86 /* HACK: (this module should not call lp_ funtions) */
87 return lp_workgroup();
90 fstrcpy(winbind_domain, dinfo->short_name);
92 wbcFreeMemory(dinfo);
94 return winbind_domain;
97 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
98 form DOMAIN/user into a domain and a user */
100 static bool parse_wbinfo_domain_user(const char *domuser, fstring domain,
101 fstring user)
104 char *p = strchr(domuser,winbind_separator());
106 if (!p) {
107 /* Maybe it was a UPN? */
108 if ((p = strchr(domuser, '@')) != NULL) {
109 fstrcpy(domain, "");
110 fstrcpy(user, domuser);
111 return true;
114 fstrcpy(user, domuser);
115 fstrcpy(domain, get_winbind_domain());
116 return true;
119 fstrcpy(user, p+1);
120 fstrcpy(domain, domuser);
121 domain[PTR_DIFF(p, domuser)] = 0;
122 strupper_m(domain);
124 return true;
127 /* pull pwent info for a given user */
129 static bool wbinfo_get_userinfo(char *user)
131 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
132 struct passwd *pwd = NULL;
134 wbc_status = wbcGetpwnam(user, &pwd);
135 if (!WBC_ERROR_IS_OK(wbc_status)) {
136 return false;
139 d_printf("%s:%s:%d:%d:%s:%s:%s\n",
140 pwd->pw_name,
141 pwd->pw_passwd,
142 pwd->pw_uid,
143 pwd->pw_gid,
144 pwd->pw_gecos,
145 pwd->pw_dir,
146 pwd->pw_shell);
148 return true;
151 /* pull pwent info for a given uid */
152 static bool wbinfo_get_uidinfo(int uid)
154 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
155 struct passwd *pwd = NULL;
157 wbc_status = wbcGetpwuid(uid, &pwd);
158 if (!WBC_ERROR_IS_OK(wbc_status)) {
159 return false;
162 d_printf("%s:%s:%d:%d:%s:%s:%s\n",
163 pwd->pw_name,
164 pwd->pw_passwd,
165 pwd->pw_uid,
166 pwd->pw_gid,
167 pwd->pw_gecos,
168 pwd->pw_dir,
169 pwd->pw_shell);
171 return true;
174 /* pull grent for a given group */
175 static bool wbinfo_get_groupinfo(const char *group)
177 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
178 struct group *grp;
180 wbc_status = wbcGetgrnam(group, &grp);
181 if (!WBC_ERROR_IS_OK(wbc_status)) {
182 return false;
185 d_printf("%s:%s:%d\n",
186 grp->gr_name,
187 grp->gr_passwd,
188 grp->gr_gid);
190 wbcFreeMemory(grp);
192 return true;
195 /* List groups a user is a member of */
197 static bool wbinfo_get_usergroups(char *user)
199 struct winbindd_request request;
200 struct winbindd_response response;
201 NSS_STATUS result;
202 int i;
204 ZERO_STRUCT(request);
205 ZERO_STRUCT(response);
207 /* Send request */
209 fstrcpy(request.data.username, user);
211 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
213 if (result != NSS_STATUS_SUCCESS)
214 return false;
216 for (i = 0; i < response.data.num_entries; i++)
217 d_printf("%d\n", (int)((gid_t *)response.extra_data.data)[i]);
219 SAFE_FREE(response.extra_data.data);
221 return true;
225 /* List group SIDs a user SID is a member of */
226 static bool wbinfo_get_usersids(const char *user_sid_str)
228 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
229 uint32_t num_sids;
230 uint32_t i;
231 struct wbcDomainSid user_sid, *sids = NULL;
233 /* Send request */
235 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
236 if (!WBC_ERROR_IS_OK(wbc_status)) {
237 return false;
240 wbc_status = wbcLookupUserSids(&user_sid, false, &num_sids, &sids);
241 if (!WBC_ERROR_IS_OK(wbc_status)) {
242 return false;
245 for (i = 0; i < num_sids; i++) {
246 char *str = NULL;
247 wbc_status = wbcSidToString(&sids[i], &str);
248 if (!WBC_ERROR_IS_OK(wbc_status)) {
249 wbcFreeMemory(sids);
250 return false;
252 d_printf("%s\n", str);
253 wbcFreeMemory(str);
256 wbcFreeMemory(sids);
258 return true;
261 static bool wbinfo_get_userdomgroups(const char *user_sid_str)
263 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
264 uint32_t num_sids;
265 uint32_t i;
266 struct wbcDomainSid user_sid, *sids = NULL;
268 /* Send request */
270 wbc_status = wbcStringToSid(user_sid_str, &user_sid);
271 if (!WBC_ERROR_IS_OK(wbc_status)) {
272 return false;
275 wbc_status = wbcLookupUserSids(&user_sid, true, &num_sids, &sids);
276 if (!WBC_ERROR_IS_OK(wbc_status)) {
277 return false;
280 for (i = 0; i < num_sids; i++) {
281 char *str = NULL;
282 wbc_status = wbcSidToString(&sids[i], &str);
283 if (!WBC_ERROR_IS_OK(wbc_status)) {
284 wbcFreeMemory(sids);
285 return false;
287 d_printf("%s\n", str);
288 wbcFreeMemory(str);
291 wbcFreeMemory(sids);
293 return true;
296 /* Convert NetBIOS name to IP */
298 static bool wbinfo_wins_byname(char *name)
300 struct winbindd_request request;
301 struct winbindd_response response;
303 ZERO_STRUCT(request);
304 ZERO_STRUCT(response);
306 /* Send request */
308 fstrcpy(request.data.winsreq, name);
310 if (winbindd_request_response(WINBINDD_WINS_BYNAME, &request, &response) !=
311 NSS_STATUS_SUCCESS) {
312 return false;
315 /* Display response */
317 d_printf("%s\n", response.data.winsresp);
319 return true;
322 /* Convert IP to NetBIOS name */
324 static bool wbinfo_wins_byip(char *ip)
326 struct winbindd_request request;
327 struct winbindd_response response;
329 ZERO_STRUCT(request);
330 ZERO_STRUCT(response);
332 /* Send request */
334 fstrcpy(request.data.winsreq, ip);
336 if (winbindd_request_response(WINBINDD_WINS_BYIP, &request, &response) !=
337 NSS_STATUS_SUCCESS) {
338 return false;
341 /* Display response */
343 d_printf("%s\n", response.data.winsresp);
345 return true;
348 /* List trusted domains */
350 static bool wbinfo_list_domains(bool list_all_domains)
352 struct winbindd_request request;
353 struct winbindd_response response;
355 ZERO_STRUCT(request);
356 ZERO_STRUCT(response);
358 /* Send request */
360 request.data.list_all_domains = list_all_domains;
362 if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM, &request, &response) !=
363 NSS_STATUS_SUCCESS)
364 return false;
366 /* Display response */
368 if (response.extra_data.data) {
369 const char *extra_data = (char *)response.extra_data.data;
370 char *name;
371 char *p;
372 TALLOC_CTX *frame = talloc_stackframe();
374 while(next_token_talloc(frame,&extra_data,&name,"\n")) {
375 p = strchr(name, '\\');
376 if (p == 0) {
377 d_fprintf(stderr, "Got invalid response: %s\n",
378 extra_data);
379 TALLOC_FREE(frame);
380 SAFE_FREE(response.extra_data.data);
381 return false;
383 *p = 0;
384 d_printf("%s\n", name);
386 TALLOC_FREE(frame);
387 SAFE_FREE(response.extra_data.data);
390 return true;
393 /* List own domain */
395 static bool wbinfo_list_own_domain(void)
397 d_printf("%s\n", get_winbind_domain());
399 return true;
402 /* show sequence numbers */
403 static bool wbinfo_show_sequence(const char *domain)
405 struct winbindd_request request;
406 struct winbindd_response response;
408 ZERO_STRUCT(response);
409 ZERO_STRUCT(request);
411 if ( domain )
412 fstrcpy( request.domain_name, domain );
414 /* Send request */
416 if (winbindd_request_response(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
417 NSS_STATUS_SUCCESS)
418 return false;
420 /* Display response */
422 if (domain) {
423 d_printf("%s : ", domain);
424 if (response.data.sequence_number == (uint32_t)-1) {
425 d_printf("DISCONNECTED\n");
426 } else {
427 d_printf("%d\n", response.data.sequence_number);
429 } else if (response.extra_data.data) {
430 char *extra_data = (char *)response.extra_data.data;
431 d_printf("%s", extra_data);
432 SAFE_FREE(response.extra_data.data);
435 return true;
438 /* Show domain info */
440 static bool wbinfo_domain_info(const char *domain)
442 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
443 struct wbcDomainInfo *dinfo = NULL;
444 char *sid_str = NULL;
446 if (strcmp(domain, ".") == 0 || domain[0] == '\0') {
447 domain = get_winbind_domain();
450 /* Send request */
452 wbc_status = wbcDomainInfo(domain, &dinfo);
453 if (!WBC_ERROR_IS_OK(wbc_status)) {
454 return false;
457 wbc_status = wbcSidToString(&dinfo->sid, &sid_str);
458 if (!WBC_ERROR_IS_OK(wbc_status)) {
459 wbcFreeMemory(dinfo);
460 return false;
463 /* Display response */
465 d_printf("Name : %s\n", dinfo->short_name);
466 d_printf("Alt_Name : %s\n", dinfo->dns_name);
468 d_printf("SID : %s\n", sid_str);
470 d_printf("Active Directory : %s\n",
471 (dinfo->flags & WBC_DOMINFO_AD) ? "Yes" : "No");
472 d_printf("Native : %s\n",
473 (dinfo->flags & WBC_DOMINFO_NATIVE) ? "Yes" : "No");
475 d_printf("Primary : %s\n",
476 (dinfo->flags & WBC_DOMINFO_PRIMARY) ? "Yes" : "No");
478 wbcFreeMemory(sid_str);
479 wbcFreeMemory(dinfo);
481 return true;
484 /* Get a foreign DC's name */
485 static bool wbinfo_getdcname(const char *domain_name)
487 struct winbindd_request request;
488 struct winbindd_response response;
490 ZERO_STRUCT(request);
491 ZERO_STRUCT(response);
493 fstrcpy(request.domain_name, domain_name);
495 /* Send request */
497 if (winbindd_request_response(WINBINDD_GETDCNAME, &request, &response) !=
498 NSS_STATUS_SUCCESS) {
499 d_fprintf(stderr, "Could not get dc name for %s\n", domain_name);
500 return false;
503 /* Display response */
505 d_printf("%s\n", response.data.dc_name);
507 return true;
510 /* Find a DC */
511 static bool wbinfo_dsgetdcname(const char *domain_name, uint32_t flags)
513 struct winbindd_request request;
514 struct winbindd_response response;
516 ZERO_STRUCT(request);
517 ZERO_STRUCT(response);
519 fstrcpy(request.domain_name, domain_name);
520 request.flags = flags;
522 request.flags |= DS_DIRECTORY_SERVICE_REQUIRED;
524 /* Send request */
526 if (winbindd_request_response(WINBINDD_DSGETDCNAME, &request, &response) !=
527 NSS_STATUS_SUCCESS) {
528 d_fprintf(stderr, "Could not find dc for %s\n", domain_name);
529 return false;
532 /* Display response */
534 d_printf("%s\n", response.data.dc_name);
536 return true;
539 /* Check trust account password */
541 static bool wbinfo_check_secret(void)
543 struct winbindd_response response;
544 NSS_STATUS result;
546 ZERO_STRUCT(response);
548 result = winbindd_request_response(WINBINDD_CHECK_MACHACC, NULL, &response);
550 d_printf("checking the trust secret via RPC calls %s\n",
551 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
553 if (result != NSS_STATUS_SUCCESS)
554 d_fprintf(stderr, "error code was %s (0x%x)\n",
555 response.data.auth.nt_status_string,
556 response.data.auth.nt_status);
558 return result == NSS_STATUS_SUCCESS;
561 /* Convert uid to sid */
563 static bool wbinfo_uid_to_sid(uid_t uid)
565 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
566 struct wbcDomainSid sid;
567 char *sid_str = NULL;
569 /* Send request */
571 wbc_status = wbcUidToSid(uid, &sid);
572 if (!WBC_ERROR_IS_OK(wbc_status)) {
573 return false;
576 wbc_status = wbcSidToString(&sid, &sid_str);
577 if (!WBC_ERROR_IS_OK(wbc_status)) {
578 return false;
581 /* Display response */
583 d_printf("%s\n", sid_str);
585 wbcFreeMemory(sid_str);
587 return true;
590 /* Convert gid to sid */
592 static bool wbinfo_gid_to_sid(gid_t gid)
594 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
595 struct wbcDomainSid sid;
596 char *sid_str = NULL;
598 /* Send request */
600 wbc_status = wbcGidToSid(gid, &sid);
601 if (!WBC_ERROR_IS_OK(wbc_status)) {
602 return false;
605 wbc_status = wbcSidToString(&sid, &sid_str);
606 if (!WBC_ERROR_IS_OK(wbc_status)) {
607 return false;
610 /* Display response */
612 d_printf("%s\n", sid_str);
614 wbcFreeMemory(sid_str);
616 return true;
619 /* Convert sid to uid */
621 static bool wbinfo_sid_to_uid(const char *sid_str)
623 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
624 struct wbcDomainSid sid;
625 uid_t uid;
627 /* Send request */
629 wbc_status = wbcStringToSid(sid_str, &sid);
630 if (!WBC_ERROR_IS_OK(wbc_status)) {
631 return false;
634 wbc_status = wbcSidToUid(&sid, &uid);
635 if (!WBC_ERROR_IS_OK(wbc_status)) {
636 return false;
639 /* Display response */
641 d_printf("%d\n", (int)uid);
643 return true;
646 static bool wbinfo_sid_to_gid(const char *sid_str)
648 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
649 struct wbcDomainSid sid;
650 gid_t gid;
652 /* Send request */
654 wbc_status = wbcStringToSid(sid_str, &sid);
655 if (!WBC_ERROR_IS_OK(wbc_status)) {
656 return false;
659 wbc_status = wbcSidToGid(&sid, &gid);
660 if (!WBC_ERROR_IS_OK(wbc_status)) {
661 return false;
664 /* Display response */
666 d_printf("%d\n", (int)gid);
668 return true;
671 static bool wbinfo_allocate_uid(void)
673 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
674 uid_t uid;
676 /* Send request */
678 wbc_status = wbcAllocateUid(&uid);
679 if (!WBC_ERROR_IS_OK(wbc_status)) {
680 return false;
683 /* Display response */
685 d_printf("New uid: %d\n", uid);
687 return true;
690 static bool wbinfo_allocate_gid(void)
692 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
693 gid_t gid;
695 /* Send request */
697 wbc_status = wbcAllocateGid(&gid);
698 if (!WBC_ERROR_IS_OK(wbc_status)) {
699 return false;
702 /* Display response */
704 d_printf("New gid: %d\n", gid);
706 return true;
709 /* Convert sid to string */
711 static bool wbinfo_lookupsid(char *sid)
713 struct winbindd_request request;
714 struct winbindd_response response;
716 ZERO_STRUCT(request);
717 ZERO_STRUCT(response);
719 /* Send off request */
721 fstrcpy(request.data.sid, sid);
723 if (winbindd_request_response(WINBINDD_LOOKUPSID, &request, &response) !=
724 NSS_STATUS_SUCCESS)
725 return false;
727 /* Display response */
729 d_printf("%s%c%s %d\n", response.data.name.dom_name,
730 winbind_separator(), response.data.name.name,
731 response.data.name.type);
733 return true;
736 /* Lookup a list of RIDs */
738 static bool wbinfo_lookuprids(char *domain, char *arg)
740 size_t i;
741 DOM_SID sid;
742 int num_rids;
743 uint32 *rids;
744 const char *p;
745 char *ridstr;
746 const char **names;
747 enum lsa_SidType *types;
748 const char *domain_name;
749 TALLOC_CTX *mem_ctx;
750 struct winbindd_request request;
751 struct winbindd_response response;
753 ZERO_STRUCT(request);
754 ZERO_STRUCT(response);
756 if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0'))
757 fstrcpy(request.domain_name, get_winbind_domain());
758 else
759 fstrcpy(request.domain_name, domain);
761 /* Send request */
763 if (winbindd_request_response(WINBINDD_DOMAIN_INFO, &request, &response) !=
764 NSS_STATUS_SUCCESS) {
765 d_printf("Could not get domain sid for %s\n", request.domain_name);
766 return false;
769 if (!string_to_sid(&sid, response.data.domain_info.sid)) {
770 d_printf("Could not convert %s to sid\n", response.data.domain_info.sid);
771 return false;
774 mem_ctx = talloc_new(NULL);
775 if (mem_ctx == NULL) {
776 d_printf("talloc_new failed\n");
777 return false;
780 num_rids = 0;
781 rids = NULL;
782 p = arg;
784 while (next_token_talloc(mem_ctx, &p, &ridstr, " ,\n")) {
785 uint32 rid = strtoul(ridstr, NULL, 10);
786 ADD_TO_ARRAY(mem_ctx, uint32, rid, &rids, &num_rids);
789 if (rids == NULL) {
790 TALLOC_FREE(mem_ctx);
791 return false;
794 if (!winbind_lookup_rids(mem_ctx, &sid, num_rids, rids,
795 &domain_name, &names, &types)) {
796 d_printf("winbind_lookup_rids failed\n");
797 TALLOC_FREE(mem_ctx);
798 return false;
801 d_printf("Domain: %s\n", domain_name);
803 for (i=0; i<num_rids; i++) {
804 d_printf("%8d: %s (%s)\n", rids[i], names[i],
805 sid_type_lookup(types[i]));
808 TALLOC_FREE(mem_ctx);
809 return true;
812 /* Convert string to sid */
814 static bool wbinfo_lookupname(char *name)
816 struct winbindd_request request;
817 struct winbindd_response response;
819 /* Send off request */
821 ZERO_STRUCT(request);
822 ZERO_STRUCT(response);
824 parse_wbinfo_domain_user(name, request.data.name.dom_name,
825 request.data.name.name);
827 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
828 NSS_STATUS_SUCCESS)
829 return false;
831 /* Display response */
833 d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
835 return true;
838 /* Authenticate a user with a plaintext password */
840 static bool wbinfo_auth_krb5(char *username, const char *cctype, uint32 flags)
842 struct winbindd_request request;
843 struct winbindd_response response;
844 NSS_STATUS result;
845 char *p;
847 /* Send off request */
849 ZERO_STRUCT(request);
850 ZERO_STRUCT(response);
852 p = strchr(username, '%');
854 if (p) {
855 *p = 0;
856 fstrcpy(request.data.auth.user, username);
857 fstrcpy(request.data.auth.pass, p + 1);
858 *p = '%';
859 } else
860 fstrcpy(request.data.auth.user, username);
862 request.flags = flags;
864 fstrcpy(request.data.auth.krb5_cc_type, cctype);
866 request.data.auth.uid = geteuid();
868 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
870 /* Display response */
872 d_printf("plaintext kerberos password authentication for [%s] %s (requesting cctype: %s)\n",
873 username, (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed", cctype);
875 if (response.data.auth.nt_status)
876 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
877 response.data.auth.nt_status_string,
878 response.data.auth.nt_status,
879 response.data.auth.error_string);
881 if (result == NSS_STATUS_SUCCESS) {
883 if (request.flags & WBFLAG_PAM_INFO3_TEXT) {
884 if (response.data.auth.info3.user_flgs & NETLOGON_CACHED_ACCOUNT) {
885 d_printf("user_flgs: NETLOGON_CACHED_ACCOUNT\n");
889 if (response.data.auth.krb5ccname[0] != '\0') {
890 d_printf("credentials were put in: %s\n", response.data.auth.krb5ccname);
891 } else {
892 d_printf("no credentials cached\n");
896 return result == NSS_STATUS_SUCCESS;
899 /* Authenticate a user with a plaintext password */
901 static bool wbinfo_auth(char *username)
903 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
904 char *s = NULL;
905 char *p = NULL;
906 const char *password = NULL;
907 char *name = NULL;
909 if ((s = SMB_STRDUP(username)) == NULL) {
910 return false;
913 if ((p = strchr(s, '%')) != NULL) {
914 *p = 0;
915 p++;
916 password = p;
917 } else {
918 password = "";
921 name = s;
923 wbc_status = wbcAuthenticateUser(name, password);
925 d_printf("plaintext password authentication %s\n",
926 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
928 #if 0
929 if (response.data.auth.nt_status)
930 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
931 response.data.auth.nt_status_string,
932 response.data.auth.nt_status,
933 response.data.auth.error_string);
934 #endif
936 SAFE_FREE(s);
938 return WBC_ERROR_IS_OK(wbc_status);
941 /* Authenticate a user with a challenge/response */
943 static bool wbinfo_auth_crap(char *username)
945 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
946 struct wbcAuthUserParams params;
947 struct wbcAuthUserInfo *info = NULL;
948 struct wbcAuthErrorInfo *err = NULL;
949 DATA_BLOB lm = data_blob_null;
950 DATA_BLOB nt = data_blob_null;
951 fstring name_user;
952 fstring name_domain;
953 fstring pass;
954 char *p;
956 p = strchr(username, '%');
958 if (p) {
959 *p = 0;
960 fstrcpy(pass, p + 1);
963 parse_wbinfo_domain_user(username, name_domain, name_user);
965 params.account_name = name_user;
966 params.domain_name = name_domain;
967 params.workstation_name = NULL;
969 params.flags = 0;
970 params.parameter_control= WBC_MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT |
971 WBC_MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
973 params.level = WBC_AUTH_USER_LEVEL_RESPONSE;
975 generate_random_buffer(params.password.response.challenge, 8);
977 if (lp_client_ntlmv2_auth()) {
978 DATA_BLOB server_chal;
979 DATA_BLOB names_blob;
981 server_chal = data_blob(params.password.response.challenge, 8);
983 /* Pretend this is a login to 'us', for blob purposes */
984 names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
986 if (!SMBNTLMv2encrypt(name_user, name_domain, pass, &server_chal,
987 &names_blob,
988 &lm, &nt, NULL)) {
989 data_blob_free(&names_blob);
990 data_blob_free(&server_chal);
991 return false;
993 data_blob_free(&names_blob);
994 data_blob_free(&server_chal);
996 } else {
997 if (lp_client_lanman_auth()) {
998 bool ok;
999 lm = data_blob(NULL, 24);
1000 ok = SMBencrypt(pass, params.password.response.challenge,
1001 lm.data);
1002 if (!ok) {
1003 data_blob_free(&lm);
1006 nt = data_blob(NULL, 24);
1007 SMBNTencrypt(pass, params.password.response.challenge,
1008 nt.data);
1011 params.password.response.nt_length = nt.length;
1012 params.password.response.nt_data = nt.data;
1013 params.password.response.lm_length = lm.length;
1014 params.password.response.lm_data = lm.data;
1016 wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
1018 /* Display response */
1020 d_printf("challenge/response password authentication %s\n",
1021 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1023 if (wbc_status == WBC_ERR_AUTH_ERROR) {
1024 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1025 err->nt_string,
1026 err->nt_status,
1027 err->display_string);
1028 wbcFreeMemory(err);
1029 } else if (WBC_ERROR_IS_OK(wbc_status)) {
1030 wbcFreeMemory(info);
1033 data_blob_free(&nt);
1034 data_blob_free(&lm);
1036 return WBC_ERROR_IS_OK(wbc_status);
1039 /* Authenticate a user with a plaintext password and set a token */
1041 static bool wbinfo_klog(char *username)
1043 struct winbindd_request request;
1044 struct winbindd_response response;
1045 NSS_STATUS result;
1046 char *p;
1048 /* Send off request */
1050 ZERO_STRUCT(request);
1051 ZERO_STRUCT(response);
1053 p = strchr(username, '%');
1055 if (p) {
1056 *p = 0;
1057 fstrcpy(request.data.auth.user, username);
1058 fstrcpy(request.data.auth.pass, p + 1);
1059 *p = '%';
1060 } else {
1061 fstrcpy(request.data.auth.user, username);
1062 fstrcpy(request.data.auth.pass, getpass("Password: "));
1065 request.flags |= WBFLAG_PAM_AFS_TOKEN;
1067 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
1069 /* Display response */
1071 d_printf("plaintext password authentication %s\n",
1072 (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed");
1074 if (response.data.auth.nt_status)
1075 d_fprintf(stderr, "error code was %s (0x%x)\nerror messsage was: %s\n",
1076 response.data.auth.nt_status_string,
1077 response.data.auth.nt_status,
1078 response.data.auth.error_string);
1080 if (result != NSS_STATUS_SUCCESS)
1081 return false;
1083 if (response.extra_data.data == NULL) {
1084 d_fprintf(stderr, "Did not get token data\n");
1085 return false;
1088 if (!afs_settoken_str((char *)response.extra_data.data)) {
1089 d_fprintf(stderr, "Could not set token\n");
1090 return false;
1093 d_printf("Successfully created AFS token\n");
1094 return true;
1097 /* Print domain users */
1099 static bool print_domain_users(const char *domain)
1101 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1102 uint32_t i;
1103 uint32_t num_users = 0;
1104 const char **users = NULL;
1106 /* Send request to winbind daemon */
1108 /* '.' is the special sign for our own domain */
1109 if (domain && strcmp(domain, ".") == 0) {
1110 domain = get_winbind_domain();
1113 wbc_status = wbcListUsers(domain, &num_users, &users);
1114 if (!WBC_ERROR_IS_OK(wbc_status)) {
1115 return false;
1118 for (i=0; i < num_users; i++) {
1119 d_printf("%s\n", users[i]);
1122 wbcFreeMemory(users);
1124 return true;
1127 /* Print domain groups */
1129 static bool print_domain_groups(const char *domain)
1131 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
1132 uint32_t i;
1133 uint32_t num_groups = 0;
1134 const char **groups = NULL;
1136 /* Send request to winbind daemon */
1138 /* '.' is the special sign for our own domain */
1139 if (domain && strcmp(domain, ".") == 0) {
1140 domain = get_winbind_domain();
1143 wbc_status = wbcListGroups(domain, &num_groups, &groups);
1144 if (!WBC_ERROR_IS_OK(wbc_status)) {
1145 return false;
1148 for (i=0; i < num_groups; i++) {
1149 d_printf("%s\n", groups[i]);
1152 wbcFreeMemory(groups);
1154 return true;
1157 /* Set the authorised user for winbindd access in secrets.tdb */
1159 static bool wbinfo_set_auth_user(char *username)
1161 const char *password;
1162 char *p;
1163 fstring user, domain;
1165 /* Separate into user and password */
1167 parse_wbinfo_domain_user(username, domain, user);
1169 p = strchr(user, '%');
1171 if (p != NULL) {
1172 *p = 0;
1173 password = p+1;
1174 } else {
1175 char *thepass = getpass("Password: ");
1176 if (thepass) {
1177 password = thepass;
1178 } else
1179 password = "";
1182 /* Store or remove DOMAIN\username%password in secrets.tdb */
1184 secrets_init();
1186 if (user[0]) {
1188 if (!secrets_store(SECRETS_AUTH_USER, user,
1189 strlen(user) + 1)) {
1190 d_fprintf(stderr, "error storing username\n");
1191 return false;
1194 /* We always have a domain name added by the
1195 parse_wbinfo_domain_user() function. */
1197 if (!secrets_store(SECRETS_AUTH_DOMAIN, domain,
1198 strlen(domain) + 1)) {
1199 d_fprintf(stderr, "error storing domain name\n");
1200 return false;
1203 } else {
1204 secrets_delete(SECRETS_AUTH_USER);
1205 secrets_delete(SECRETS_AUTH_DOMAIN);
1208 if (password[0]) {
1210 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
1211 strlen(password) + 1)) {
1212 d_fprintf(stderr, "error storing password\n");
1213 return false;
1216 } else
1217 secrets_delete(SECRETS_AUTH_PASSWORD);
1219 return true;
1222 static void wbinfo_get_auth_user(void)
1224 char *user, *domain, *password;
1226 /* Lift data from secrets file */
1228 secrets_fetch_ipc_userpass(&user, &domain, &password);
1230 if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
1232 SAFE_FREE(user);
1233 SAFE_FREE(domain);
1234 SAFE_FREE(password);
1235 d_printf("No authorised user configured\n");
1236 return;
1239 /* Pretty print authorised user info */
1241 d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
1242 user, password ? "%" : "", password ? password : "");
1244 SAFE_FREE(user);
1245 SAFE_FREE(domain);
1246 SAFE_FREE(password);
1249 static bool wbinfo_ping(void)
1251 wbcErr wbc_status;
1253 wbc_status = wbcPing();
1255 /* Display response */
1257 d_printf("Ping to winbindd %s\n",
1258 WBC_ERROR_IS_OK(wbc_status) ? "succeeded" : "failed");
1260 return WBC_ERROR_IS_OK(wbc_status);
1263 /* Main program */
1265 enum {
1266 OPT_SET_AUTH_USER = 1000,
1267 OPT_GET_AUTH_USER,
1268 OPT_DOMAIN_NAME,
1269 OPT_SEQUENCE,
1270 OPT_GETDCNAME,
1271 OPT_DSGETDCNAME,
1272 OPT_USERDOMGROUPS,
1273 OPT_USERSIDS,
1274 OPT_ALLOCATE_UID,
1275 OPT_ALLOCATE_GID,
1276 OPT_SEPARATOR,
1277 OPT_LIST_ALL_DOMAINS,
1278 OPT_LIST_OWN_DOMAIN,
1279 OPT_UID_INFO,
1280 OPT_GROUP_INFO,
1283 int main(int argc, char **argv, char **envp)
1285 int opt;
1286 TALLOC_CTX *frame = talloc_stackframe();
1287 poptContext pc;
1288 static char *string_arg;
1289 static char *opt_domain_name;
1290 static int int_arg;
1291 int result = 1;
1293 struct poptOption long_options[] = {
1294 POPT_AUTOHELP
1296 /* longName, shortName, argInfo, argPtr, value, descrip,
1297 argDesc */
1299 { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
1300 { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
1301 { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
1302 { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
1303 { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
1304 { "sid-to-name", 's', POPT_ARG_STRING, &string_arg, 's', "Converts sid to name", "SID" },
1305 { "lookup-rids", 'R', POPT_ARG_STRING, &string_arg, 'R', "Converts RIDs to names", "RIDs" },
1306 { "uid-to-sid", 'U', POPT_ARG_INT, &int_arg, 'U', "Converts uid to sid" , "UID" },
1307 { "gid-to-sid", 'G', POPT_ARG_INT, &int_arg, 'G', "Converts gid to sid", "GID" },
1308 { "sid-to-uid", 'S', POPT_ARG_STRING, &string_arg, 'S', "Converts sid to uid", "SID" },
1309 { "sid-to-gid", 'Y', POPT_ARG_STRING, &string_arg, 'Y', "Converts sid to gid", "SID" },
1310 { "allocate-uid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_UID,
1311 "Get a new UID out of idmap" },
1312 { "allocate-gid", 0, POPT_ARG_NONE, 0, OPT_ALLOCATE_GID,
1313 "Get a new GID out of idmap" },
1314 { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
1315 { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
1316 { "all-domains", 0, POPT_ARG_NONE, 0, OPT_LIST_ALL_DOMAINS, "List all domains (trusted and own domain)" },
1317 { "own-domain", 0, POPT_ARG_NONE, 0, OPT_LIST_OWN_DOMAIN, "List own domain" },
1318 { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
1319 { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
1320 { "user-info", 'i', POPT_ARG_STRING, &string_arg, 'i', "Get user info", "USER" },
1321 { "uid-info", 0, POPT_ARG_INT, &int_arg, OPT_UID_INFO, "Get user info from uid", "UID" },
1322 { "group-info", 0, POPT_ARG_STRING, &string_arg, OPT_GROUP_INFO, "Get group info", "GROUP" },
1323 { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
1324 { "user-domgroups", 0, POPT_ARG_STRING, &string_arg,
1325 OPT_USERDOMGROUPS, "Get user domain groups", "SID" },
1326 { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
1327 { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
1328 { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
1329 { "getdcname", 0, POPT_ARG_STRING, &string_arg, OPT_GETDCNAME,
1330 "Get a DC name for a foreign domain", "domainname" },
1331 { "dsgetdcname", 0, POPT_ARG_STRING, &string_arg, OPT_DSGETDCNAME, "Find a DC for a domain", "domainname" },
1332 { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
1333 { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
1334 { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
1335 #ifdef WITH_FAKE_KASERVER
1336 { "klog", 'k', POPT_ARG_STRING, &string_arg, 'k', "set an AFS token from winbind", "user%password" },
1337 #endif
1338 #ifdef HAVE_KRB5
1339 { "krb5auth", 'K', POPT_ARG_STRING, &string_arg, 'K', "authenticate user using Kerberos", "user%password" },
1340 /* destroys wbinfo --help output */
1341 /* "user%password,DOM\\user%password,user@EXAMPLE.COM,EXAMPLE.COM\\user%password" }, */
1342 #endif
1343 { "separator", 0, POPT_ARG_NONE, 0, OPT_SEPARATOR, "Get the active winbind separator", NULL },
1344 POPT_COMMON_CONFIGFILE
1345 POPT_COMMON_VERSION
1346 POPT_TABLEEND
1349 /* Samba client initialisation */
1350 load_case_tables();
1353 /* Parse options */
1355 pc = poptGetContext("wbinfo", argc, (const char **)argv, long_options, 0);
1357 /* Parse command line options */
1359 if (argc == 1) {
1360 poptPrintHelp(pc, stderr, 0);
1361 return 1;
1364 while((opt = poptGetNextOpt(pc)) != -1) {
1365 /* get the generic configuration parameters like --domain */
1368 poptFreeContext(pc);
1370 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, true)) {
1371 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1372 get_dyn_CONFIGFILE(), strerror(errno));
1373 exit(1);
1376 if (!init_names())
1377 return 1;
1379 load_interfaces();
1381 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1382 POPT_CONTEXT_KEEP_FIRST);
1384 while((opt = poptGetNextOpt(pc)) != -1) {
1385 switch (opt) {
1386 case 'u':
1387 if (!print_domain_users(opt_domain_name)) {
1388 d_fprintf(stderr, "Error looking up domain users\n");
1389 goto done;
1391 break;
1392 case 'g':
1393 if (!print_domain_groups(opt_domain_name)) {
1394 d_fprintf(stderr, "Error looking up domain groups\n");
1395 goto done;
1397 break;
1398 case 's':
1399 if (!wbinfo_lookupsid(string_arg)) {
1400 d_fprintf(stderr, "Could not lookup sid %s\n", string_arg);
1401 goto done;
1403 break;
1404 case 'R':
1405 if (!wbinfo_lookuprids(opt_domain_name, string_arg)) {
1406 d_fprintf(stderr, "Could not lookup RIDs %s\n", string_arg);
1407 goto done;
1409 break;
1410 case 'n':
1411 if (!wbinfo_lookupname(string_arg)) {
1412 d_fprintf(stderr, "Could not lookup name %s\n", string_arg);
1413 goto done;
1415 break;
1416 case 'N':
1417 if (!wbinfo_wins_byname(string_arg)) {
1418 d_fprintf(stderr, "Could not lookup WINS by name %s\n", string_arg);
1419 goto done;
1421 break;
1422 case 'I':
1423 if (!wbinfo_wins_byip(string_arg)) {
1424 d_fprintf(stderr, "Could not lookup WINS by IP %s\n", string_arg);
1425 goto done;
1427 break;
1428 case 'U':
1429 if (!wbinfo_uid_to_sid(int_arg)) {
1430 d_fprintf(stderr, "Could not convert uid %d to sid\n", int_arg);
1431 goto done;
1433 break;
1434 case 'G':
1435 if (!wbinfo_gid_to_sid(int_arg)) {
1436 d_fprintf(stderr, "Could not convert gid %d to sid\n",
1437 int_arg);
1438 goto done;
1440 break;
1441 case 'S':
1442 if (!wbinfo_sid_to_uid(string_arg)) {
1443 d_fprintf(stderr, "Could not convert sid %s to uid\n",
1444 string_arg);
1445 goto done;
1447 break;
1448 case 'Y':
1449 if (!wbinfo_sid_to_gid(string_arg)) {
1450 d_fprintf(stderr, "Could not convert sid %s to gid\n",
1451 string_arg);
1452 goto done;
1454 break;
1455 case OPT_ALLOCATE_UID:
1456 if (!wbinfo_allocate_uid()) {
1457 d_fprintf(stderr, "Could not allocate a uid\n");
1458 goto done;
1460 break;
1461 case OPT_ALLOCATE_GID:
1462 if (!wbinfo_allocate_gid()) {
1463 d_fprintf(stderr, "Could not allocate a gid\n");
1464 goto done;
1466 break;
1467 case 't':
1468 if (!wbinfo_check_secret()) {
1469 d_fprintf(stderr, "Could not check secret\n");
1470 goto done;
1472 break;
1473 case 'm':
1474 if (!wbinfo_list_domains(false)) {
1475 d_fprintf(stderr, "Could not list trusted domains\n");
1476 goto done;
1478 break;
1479 case OPT_SEQUENCE:
1480 if (!wbinfo_show_sequence(opt_domain_name)) {
1481 d_fprintf(stderr, "Could not show sequence numbers\n");
1482 goto done;
1484 break;
1485 case 'D':
1486 if (!wbinfo_domain_info(string_arg)) {
1487 d_fprintf(stderr, "Could not get domain info\n");
1488 goto done;
1490 break;
1491 case 'i':
1492 if (!wbinfo_get_userinfo(string_arg)) {
1493 d_fprintf(stderr, "Could not get info for user %s\n",
1494 string_arg);
1495 goto done;
1497 break;
1498 case OPT_UID_INFO:
1499 if ( !wbinfo_get_uidinfo(int_arg)) {
1500 d_fprintf(stderr, "Could not get info for uid "
1501 "%d\n", int_arg);
1502 goto done;
1504 break;
1505 case OPT_GROUP_INFO:
1506 if ( !wbinfo_get_groupinfo(string_arg)) {
1507 d_fprintf(stderr, "Could not get info for "
1508 "group %s\n", string_arg);
1509 goto done;
1511 break;
1512 case 'r':
1513 if (!wbinfo_get_usergroups(string_arg)) {
1514 d_fprintf(stderr, "Could not get groups for user %s\n",
1515 string_arg);
1516 goto done;
1518 break;
1519 case OPT_USERSIDS:
1520 if (!wbinfo_get_usersids(string_arg)) {
1521 d_fprintf(stderr, "Could not get group SIDs for user SID %s\n",
1522 string_arg);
1523 goto done;
1525 break;
1526 case OPT_USERDOMGROUPS:
1527 if (!wbinfo_get_userdomgroups(string_arg)) {
1528 d_fprintf(stderr, "Could not get user's domain groups "
1529 "for user SID %s\n", string_arg);
1530 goto done;
1532 break;
1533 case 'a': {
1534 bool got_error = false;
1536 if (!wbinfo_auth(string_arg)) {
1537 d_fprintf(stderr, "Could not authenticate user %s with "
1538 "plaintext password\n", string_arg);
1539 got_error = true;
1542 if (!wbinfo_auth_crap(string_arg)) {
1543 d_fprintf(stderr, "Could not authenticate user %s with "
1544 "challenge/response\n", string_arg);
1545 got_error = true;
1548 if (got_error)
1549 goto done;
1550 break;
1552 case 'K': {
1553 uint32 flags = WBFLAG_PAM_KRB5 |
1554 WBFLAG_PAM_CACHED_LOGIN |
1555 WBFLAG_PAM_FALLBACK_AFTER_KRB5 |
1556 WBFLAG_PAM_INFO3_TEXT;
1558 if (!wbinfo_auth_krb5(string_arg, "FILE", flags)) {
1559 d_fprintf(stderr, "Could not authenticate user [%s] with "
1560 "Kerberos (ccache: %s)\n", string_arg, "FILE");
1561 goto done;
1563 break;
1565 case 'k':
1566 if (!wbinfo_klog(string_arg)) {
1567 d_fprintf(stderr, "Could not klog user\n");
1568 goto done;
1570 break;
1571 case 'p':
1572 if (!wbinfo_ping()) {
1573 d_fprintf(stderr, "could not ping winbindd!\n");
1574 goto done;
1576 break;
1577 case OPT_SET_AUTH_USER:
1578 if (!wbinfo_set_auth_user(string_arg)) {
1579 goto done;
1581 break;
1582 case OPT_GET_AUTH_USER:
1583 wbinfo_get_auth_user();
1584 break;
1585 case OPT_GETDCNAME:
1586 if (!wbinfo_getdcname(string_arg)) {
1587 goto done;
1589 break;
1590 case OPT_DSGETDCNAME:
1591 if (!wbinfo_dsgetdcname(string_arg, 0)) {
1592 goto done;
1594 break;
1595 case OPT_SEPARATOR: {
1596 const char sep = winbind_separator_int(true);
1597 if ( !sep ) {
1598 goto done;
1600 d_printf("%c\n", sep);
1601 break;
1603 case OPT_LIST_ALL_DOMAINS:
1604 if (!wbinfo_list_domains(true)) {
1605 goto done;
1607 break;
1608 case OPT_LIST_OWN_DOMAIN:
1609 if (!wbinfo_list_own_domain()) {
1610 goto done;
1612 break;
1613 /* generic configuration options */
1614 case OPT_DOMAIN_NAME:
1615 break;
1616 default:
1617 d_fprintf(stderr, "Invalid option\n");
1618 poptPrintHelp(pc, stderr, 0);
1619 goto done;
1623 result = 0;
1625 /* Exit code */
1627 done:
1628 talloc_destroy(frame);
1630 poptFreeContext(pc);
1631 return result;