2 Unix SMB/CIFS implementation.
4 Winbind client asynchronous API, utility functions
6 Copyright (C) Gerald (Jerry) Carter 2007-2008
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 3 of the License, or (at your option) any later version.
14 This library 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 GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* Required Headers */
26 #include "libwbclient.h"
27 #include "../winbind_client.h"
29 /** @brief Ping winbindd to see if the daemon is running
31 * @param *ctx wbclient Context
35 wbcErr wbcCtxPing(struct wbcContext *ctx)
37 struct winbindd_request request;
38 struct winbindd_response response;
40 /* Initialize request */
43 ZERO_STRUCT(response);
45 return wbcRequestResponse(ctx, WINBINDD_PING, &request, &response);
50 return wbcCtxPing(NULL);
53 static void wbcInterfaceDetailsDestructor(void *ptr)
55 struct wbcInterfaceDetails *i = (struct wbcInterfaceDetails *)ptr;
56 free(i->winbind_version);
57 free(i->netbios_name);
58 free(i->netbios_domain);
63 * @brief Query useful information about the winbind service
65 * @param *_details pointer to hold the struct wbcInterfaceDetails
70 wbcErr wbcCtxInterfaceDetails(struct wbcContext *ctx,
71 struct wbcInterfaceDetails **_details)
73 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
74 struct wbcInterfaceDetails *info;
75 struct wbcDomainInfo *domain = NULL;
76 struct winbindd_request request;
77 struct winbindd_response response;
79 /* Initialize request */
82 ZERO_STRUCT(response);
84 info = (struct wbcInterfaceDetails *)wbcAllocateMemory(
85 1, sizeof(struct wbcInterfaceDetails),
86 wbcInterfaceDetailsDestructor);
87 BAIL_ON_PTR_ERROR(info, wbc_status);
89 /* first the interface version */
90 wbc_status = wbcRequestResponse(ctx, WINBINDD_INTERFACE_VERSION,
92 BAIL_ON_WBC_ERROR(wbc_status);
93 info->interface_version = response.data.interface_version;
95 /* then the samba version and the winbind separator */
96 wbc_status = wbcRequestResponse(ctx, WINBINDD_INFO, NULL, &response);
97 BAIL_ON_WBC_ERROR(wbc_status);
99 info->winbind_version = strdup(response.data.info.samba_version);
100 BAIL_ON_PTR_ERROR(info->winbind_version, wbc_status);
101 info->winbind_separator = response.data.info.winbind_separator;
103 /* then the local netbios name */
104 wbc_status = wbcRequestResponse(ctx, WINBINDD_NETBIOS_NAME,
106 BAIL_ON_WBC_ERROR(wbc_status);
108 info->netbios_name = strdup(response.data.netbios_name);
109 BAIL_ON_PTR_ERROR(info->netbios_name, wbc_status);
111 /* then the local workgroup name */
112 wbc_status = wbcRequestResponse(ctx, WINBINDD_DOMAIN_NAME,
114 BAIL_ON_WBC_ERROR(wbc_status);
116 info->netbios_domain = strdup(response.data.domain_name);
117 BAIL_ON_PTR_ERROR(info->netbios_domain, wbc_status);
119 wbc_status = wbcCtxDomainInfo(ctx, info->netbios_domain, &domain);
120 if (wbc_status == WBC_ERR_DOMAIN_NOT_FOUND) {
121 /* maybe it's a standalone server */
123 wbc_status = WBC_ERR_SUCCESS;
125 BAIL_ON_WBC_ERROR(wbc_status);
129 info->dns_domain = strdup(domain->dns_name);
130 wbcFreeMemory(domain);
131 BAIL_ON_PTR_ERROR(info->dns_domain, wbc_status);
133 info->dns_domain = NULL;
139 wbc_status = WBC_ERR_SUCCESS;
146 wbcErr wbcInterfaceDetails(struct wbcInterfaceDetails **_details)
148 return wbcCtxInterfaceDetails(NULL, _details);
151 static void wbcDomainInfoDestructor(void *ptr)
153 struct wbcDomainInfo *i = (struct wbcDomainInfo *)ptr;
158 /** @brief Lookup the current status of a trusted domain, sync wrapper
160 * @param domain Domain to query
161 * @param *dinfo Pointer to returned struct wbcDomainInfo
166 wbcErr wbcCtxDomainInfo(struct wbcContext *ctx,
168 struct wbcDomainInfo **dinfo)
170 struct winbindd_request request;
171 struct winbindd_response response;
172 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
173 struct wbcDomainInfo *info = NULL;
175 if (!domain || !dinfo) {
176 wbc_status = WBC_ERR_INVALID_PARAM;
177 BAIL_ON_WBC_ERROR(wbc_status);
180 /* Initialize request */
182 ZERO_STRUCT(request);
183 ZERO_STRUCT(response);
185 strncpy(request.domain_name, domain,
186 sizeof(request.domain_name)-1);
188 wbc_status = wbcRequestResponse(ctx, WINBINDD_DOMAIN_INFO,
191 BAIL_ON_WBC_ERROR(wbc_status);
193 info = (struct wbcDomainInfo *)wbcAllocateMemory(
194 1, sizeof(struct wbcDomainInfo), wbcDomainInfoDestructor);
195 BAIL_ON_PTR_ERROR(info, wbc_status);
197 info->short_name = strdup(response.data.domain_info.name);
198 BAIL_ON_PTR_ERROR(info->short_name, wbc_status);
200 info->dns_name = strdup(response.data.domain_info.alt_name);
201 BAIL_ON_PTR_ERROR(info->dns_name, wbc_status);
203 wbc_status = wbcStringToSid(response.data.domain_info.sid,
205 BAIL_ON_WBC_ERROR(wbc_status);
207 if (response.data.domain_info.native_mode)
208 info->domain_flags |= WBC_DOMINFO_DOMAIN_NATIVE;
209 if (response.data.domain_info.active_directory)
210 info->domain_flags |= WBC_DOMINFO_DOMAIN_AD;
211 if (response.data.domain_info.primary)
212 info->domain_flags |= WBC_DOMINFO_DOMAIN_PRIMARY;
217 wbc_status = WBC_ERR_SUCCESS;
224 wbcErr wbcDomainInfo(const char *domain, struct wbcDomainInfo **dinfo)
226 return wbcCtxDomainInfo(NULL, domain, dinfo);
229 /* Get the list of current DCs */
230 wbcErr wbcCtxDcInfo(struct wbcContext *ctx,
231 const char *domain, size_t *num_dcs,
232 const char ***dc_names, const char ***dc_ips)
234 struct winbindd_request request;
235 struct winbindd_response response;
236 const char **names = NULL;
237 const char **ips = NULL;
238 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
243 /* Initialise request */
245 ZERO_STRUCT(request);
246 ZERO_STRUCT(response);
248 if (domain != NULL) {
249 strncpy(request.domain_name, domain,
250 sizeof(request.domain_name) - 1);
253 wbc_status = wbcRequestResponse(ctx, WINBINDD_DC_INFO,
254 &request, &response);
255 BAIL_ON_WBC_ERROR(wbc_status);
257 names = wbcAllocateStringArray(response.data.num_entries);
258 BAIL_ON_PTR_ERROR(names, wbc_status);
260 ips = wbcAllocateStringArray(response.data.num_entries);
261 BAIL_ON_PTR_ERROR(ips, wbc_status);
263 wbc_status = WBC_ERR_INVALID_RESPONSE;
265 p = (char *)response.extra_data.data;
267 if (response.length < (sizeof(struct winbindd_response)+1)) {
271 extra_len = response.length - sizeof(struct winbindd_response);
273 if (p[extra_len-1] != '\0') {
277 for (i=0; i<response.data.num_entries; i++) {
284 names[i] = strndup(p, q-p);
285 BAIL_ON_PTR_ERROR(names[i], wbc_status);
292 ips[i] = strndup(p, q-p);
293 BAIL_ON_PTR_ERROR(ips[i], wbc_status);
300 wbc_status = WBC_ERR_SUCCESS;
302 if (response.extra_data.data)
303 free(response.extra_data.data);
305 if (WBC_ERROR_IS_OK(wbc_status)) {
306 *num_dcs = response.data.num_entries;
312 wbcFreeMemory(names);
317 wbcErr wbcDcInfo(const char *domain, size_t *num_dcs,
318 const char ***dc_names, const char ***dc_ips)
320 return wbcCtxDcInfo(NULL, domain, num_dcs, dc_names, dc_ips);
323 /* Resolve a NetbiosName via WINS */
324 wbcErr wbcCtxResolveWinsByName(struct wbcContext *ctx,
325 const char *name, char **ip)
327 struct winbindd_request request;
328 struct winbindd_response response;
329 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
332 ZERO_STRUCT(request);
333 ZERO_STRUCT(response);
337 strncpy(request.data.winsreq, name,
338 sizeof(request.data.winsreq)-1);
340 wbc_status = wbcRequestResponse(ctx, WINBINDD_WINS_BYNAME,
343 BAIL_ON_WBC_ERROR(wbc_status);
345 /* Display response */
347 ipaddr = wbcStrDup(response.data.winsresp);
348 BAIL_ON_PTR_ERROR(ipaddr, wbc_status);
351 wbc_status = WBC_ERR_SUCCESS;
357 wbcErr wbcResolveWinsByName(const char *name, char **ip)
359 return wbcCtxResolveWinsByName(NULL, name, ip);
362 /* Resolve an IP address via WINS into a NetbiosName */
363 wbcErr wbcCtxResolveWinsByIP(struct wbcContext *ctx,
364 const char *ip, char **name)
366 struct winbindd_request request;
367 struct winbindd_response response;
368 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
371 ZERO_STRUCT(request);
372 ZERO_STRUCT(response);
376 strncpy(request.data.winsreq, ip,
377 sizeof(request.data.winsreq)-1);
379 wbc_status = wbcRequestResponse(ctx, WINBINDD_WINS_BYIP,
382 BAIL_ON_WBC_ERROR(wbc_status);
384 /* Display response */
386 name_str = wbcStrDup(response.data.winsresp);
387 BAIL_ON_PTR_ERROR(name_str, wbc_status);
390 wbc_status = WBC_ERR_SUCCESS;
396 wbcErr wbcResolveWinsByIP(const char *ip, char **name)
398 return wbcCtxResolveWinsByIP(NULL, ip, name);
404 static wbcErr process_domain_info_string(struct wbcDomainInfo *info,
407 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
414 if ((s = strchr(r, '\\')) == NULL) {
415 wbc_status = WBC_ERR_INVALID_RESPONSE;
416 BAIL_ON_WBC_ERROR(wbc_status);
421 info->short_name = strdup(r);
422 BAIL_ON_PTR_ERROR(info->short_name, wbc_status);
427 if ((s = strchr(r, '\\')) == NULL) {
428 wbc_status = WBC_ERR_INVALID_RESPONSE;
429 BAIL_ON_WBC_ERROR(wbc_status);
434 info->dns_name = strdup(r);
435 BAIL_ON_PTR_ERROR(info->dns_name, wbc_status);
439 if ((s = strchr(r, '\\')) == NULL) {
440 wbc_status = WBC_ERR_INVALID_RESPONSE;
441 BAIL_ON_WBC_ERROR(wbc_status);
446 wbc_status = wbcStringToSid(r, &info->sid);
447 BAIL_ON_WBC_ERROR(wbc_status);
451 if ((s = strchr(r, '\\')) == NULL) {
452 wbc_status = WBC_ERR_INVALID_RESPONSE;
453 BAIL_ON_WBC_ERROR(wbc_status);
458 if (strcmp(r, "None") == 0) {
459 info->trust_type = WBC_DOMINFO_TRUSTTYPE_NONE;
460 } else if (strcmp(r, "External") == 0) {
461 info->trust_type = WBC_DOMINFO_TRUSTTYPE_EXTERNAL;
462 } else if (strcmp(r, "Forest") == 0) {
463 info->trust_type = WBC_DOMINFO_TRUSTTYPE_FOREST;
464 } else if (strcmp(r, "In Forest") == 0) {
465 info->trust_type = WBC_DOMINFO_TRUSTTYPE_IN_FOREST;
467 wbc_status = WBC_ERR_INVALID_RESPONSE;
468 BAIL_ON_WBC_ERROR(wbc_status);
473 if ((s = strchr(r, '\\')) == NULL) {
474 wbc_status = WBC_ERR_INVALID_RESPONSE;
475 BAIL_ON_WBC_ERROR(wbc_status);
480 if (strcmp(r, "Yes") == 0) {
481 info->trust_flags |= WBC_DOMINFO_TRUST_TRANSITIVE;
486 if ((s = strchr(r, '\\')) == NULL) {
487 wbc_status = WBC_ERR_INVALID_RESPONSE;
488 BAIL_ON_WBC_ERROR(wbc_status);
493 if (strcmp(r, "Yes") == 0) {
494 info->trust_flags |= WBC_DOMINFO_TRUST_INCOMING;
499 if ((s = strchr(r, '\\')) == NULL) {
500 wbc_status = WBC_ERR_INVALID_RESPONSE;
501 BAIL_ON_WBC_ERROR(wbc_status);
506 if (strcmp(r, "Yes") == 0) {
507 info->trust_flags |= WBC_DOMINFO_TRUST_OUTGOING;
510 /* Online/Offline status */
512 if ( strcmp(r, "Offline") == 0) {
513 info->domain_flags |= WBC_DOMINFO_DOMAIN_OFFLINE;
516 wbc_status = WBC_ERR_SUCCESS;
522 static void wbcDomainInfoListDestructor(void *ptr)
524 struct wbcDomainInfo *i = (struct wbcDomainInfo *)ptr;
526 while (i->short_name != NULL) {
533 /* Enumerate the domain trusts known by Winbind */
534 wbcErr wbcCtxListTrusts(struct wbcContext *ctx,
535 struct wbcDomainInfo **domains, size_t *num_domains)
537 struct winbindd_response response;
538 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
540 char *extra_data = NULL;
541 struct wbcDomainInfo *d_list = NULL;
547 ZERO_STRUCT(response);
551 wbc_status = wbcRequestResponse(ctx, WINBINDD_LIST_TRUSTDOM,
554 BAIL_ON_WBC_ERROR(wbc_status);
556 /* Decode the response */
558 p = (char *)response.extra_data.data;
560 if ((p == NULL) || (strlen(p) == 0)) {
561 /* We should always at least get back our
564 wbc_status = WBC_ERR_DOMAIN_NOT_FOUND;
565 BAIL_ON_WBC_ERROR(wbc_status);
568 d_list = (struct wbcDomainInfo *)wbcAllocateMemory(
569 response.data.num_entries + 1,sizeof(struct wbcDomainInfo),
570 wbcDomainInfoListDestructor);
571 BAIL_ON_PTR_ERROR(d_list, wbc_status);
573 extra_data = strdup((char*)response.extra_data.data);
574 BAIL_ON_PTR_ERROR(extra_data, wbc_status);
578 /* Outer loop processes the list of domain information */
580 for (i=0; i<response.data.num_entries && p; i++) {
581 char *next = strchr(p, '\n');
588 wbc_status = process_domain_info_string(&d_list[i], p);
589 BAIL_ON_WBC_ERROR(wbc_status);
599 winbindd_free_response(&response);
600 wbcFreeMemory(d_list);
605 wbcErr wbcListTrusts(struct wbcDomainInfo **domains, size_t *num_domains)
607 return wbcCtxListTrusts(NULL, domains, num_domains);
610 static void wbcDomainControllerInfoDestructor(void *ptr)
612 struct wbcDomainControllerInfo *i =
613 (struct wbcDomainControllerInfo *)ptr;
617 /* Enumerate the domain trusts known by Winbind */
618 wbcErr wbcCtxLookupDomainController(struct wbcContext *ctx,
619 const char *domain, uint32_t flags,
620 struct wbcDomainControllerInfo **dc_info)
622 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
623 struct winbindd_request request;
624 struct winbindd_response response;
625 struct wbcDomainControllerInfo *dc = NULL;
627 /* validate input params */
629 if (!domain || !dc_info) {
630 wbc_status = WBC_ERR_INVALID_PARAM;
631 BAIL_ON_WBC_ERROR(wbc_status);
634 ZERO_STRUCT(request);
635 ZERO_STRUCT(response);
637 strncpy(request.data.dsgetdcname.domain_name, domain,
638 sizeof(request.data.dsgetdcname.domain_name)-1);
640 request.flags = flags;
642 dc = (struct wbcDomainControllerInfo *)wbcAllocateMemory(
643 1, sizeof(struct wbcDomainControllerInfo),
644 wbcDomainControllerInfoDestructor);
645 BAIL_ON_PTR_ERROR(dc, wbc_status);
649 wbc_status = wbcRequestResponse(ctx, WINBINDD_DSGETDCNAME,
652 BAIL_ON_WBC_ERROR(wbc_status);
654 dc->dc_name = strdup(response.data.dsgetdcname.dc_unc);
655 BAIL_ON_PTR_ERROR(dc->dc_name, wbc_status);
665 wbcErr wbcLookupDomainController(const char *domain, uint32_t flags,
666 struct wbcDomainControllerInfo **dc_info)
668 return wbcCtxLookupDomainController(NULL, domain, flags, dc_info);
671 static void wbcDomainControllerInfoExDestructor(void *ptr)
673 struct wbcDomainControllerInfoEx *i =
674 (struct wbcDomainControllerInfoEx *)ptr;
675 free(discard_const_p(char, i->dc_unc));
676 free(discard_const_p(char, i->dc_address));
677 free(discard_const_p(char, i->domain_guid));
678 free(discard_const_p(char, i->domain_name));
679 free(discard_const_p(char, i->forest_name));
680 free(discard_const_p(char, i->dc_site_name));
681 free(discard_const_p(char, i->client_site_name));
684 static wbcErr wbc_create_domain_controller_info_ex(const struct winbindd_response *resp,
685 struct wbcDomainControllerInfoEx **_i)
687 wbcErr wbc_status = WBC_ERR_SUCCESS;
688 struct wbcDomainControllerInfoEx *i;
691 i = (struct wbcDomainControllerInfoEx *)wbcAllocateMemory(
692 1, sizeof(struct wbcDomainControllerInfoEx),
693 wbcDomainControllerInfoExDestructor);
694 BAIL_ON_PTR_ERROR(i, wbc_status);
696 i->dc_unc = strdup(resp->data.dsgetdcname.dc_unc);
697 BAIL_ON_PTR_ERROR(i->dc_unc, wbc_status);
699 i->dc_address = strdup(resp->data.dsgetdcname.dc_address);
700 BAIL_ON_PTR_ERROR(i->dc_address, wbc_status);
702 i->dc_address_type = resp->data.dsgetdcname.dc_address_type;
704 wbc_status = wbcStringToGuid(resp->data.dsgetdcname.domain_guid, &guid);
705 if (WBC_ERROR_IS_OK(wbc_status)) {
706 i->domain_guid = (struct wbcGuid *)malloc(
707 sizeof(struct wbcGuid));
708 BAIL_ON_PTR_ERROR(i->domain_guid, wbc_status);
710 *i->domain_guid = guid;
713 i->domain_name = strdup(resp->data.dsgetdcname.domain_name);
714 BAIL_ON_PTR_ERROR(i->domain_name, wbc_status);
716 if (resp->data.dsgetdcname.forest_name[0] != '\0') {
717 i->forest_name = strdup(resp->data.dsgetdcname.forest_name);
718 BAIL_ON_PTR_ERROR(i->forest_name, wbc_status);
721 i->dc_flags = resp->data.dsgetdcname.dc_flags;
723 if (resp->data.dsgetdcname.dc_site_name[0] != '\0') {
724 i->dc_site_name = strdup(resp->data.dsgetdcname.dc_site_name);
725 BAIL_ON_PTR_ERROR(i->dc_site_name, wbc_status);
728 if (resp->data.dsgetdcname.client_site_name[0] != '\0') {
729 i->client_site_name = strdup(
730 resp->data.dsgetdcname.client_site_name);
731 BAIL_ON_PTR_ERROR(i->client_site_name, wbc_status);
744 /* Get extended domain controller information */
745 wbcErr wbcCtxLookupDomainControllerEx(struct wbcContext *ctx,
747 struct wbcGuid *guid,
750 struct wbcDomainControllerInfoEx **dc_info)
752 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
753 struct winbindd_request request;
754 struct winbindd_response response;
756 /* validate input params */
758 if (!domain || !dc_info) {
759 wbc_status = WBC_ERR_INVALID_PARAM;
760 BAIL_ON_WBC_ERROR(wbc_status);
763 ZERO_STRUCT(request);
764 ZERO_STRUCT(response);
766 request.data.dsgetdcname.flags = flags;
768 strncpy(request.data.dsgetdcname.domain_name, domain,
769 sizeof(request.data.dsgetdcname.domain_name)-1);
772 strncpy(request.data.dsgetdcname.site_name, site,
773 sizeof(request.data.dsgetdcname.site_name)-1);
779 wbc_status = wbcGuidToString(guid, &str);
780 BAIL_ON_WBC_ERROR(wbc_status);
782 strncpy(request.data.dsgetdcname.domain_guid, str,
783 sizeof(request.data.dsgetdcname.domain_guid)-1);
790 wbc_status = wbcRequestResponse(ctx, WINBINDD_DSGETDCNAME,
793 BAIL_ON_WBC_ERROR(wbc_status);
796 wbc_status = wbc_create_domain_controller_info_ex(&response,
798 BAIL_ON_WBC_ERROR(wbc_status);
801 wbc_status = WBC_ERR_SUCCESS;
806 wbcErr wbcLookupDomainControllerEx(const char *domain,
807 struct wbcGuid *guid,
810 struct wbcDomainControllerInfoEx **dc_info)
812 return wbcCtxLookupDomainControllerEx(NULL, domain, guid, site,
816 static void wbcNamedBlobDestructor(void *ptr)
818 struct wbcNamedBlob *b = (struct wbcNamedBlob *)ptr;
820 while (b->name != NULL) {
821 free(discard_const_p(char, b->name));
827 /* Initialize a named blob and add to list of blobs */
828 wbcErr wbcAddNamedBlob(size_t *num_blobs,
829 struct wbcNamedBlob **pblobs,
835 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
836 struct wbcNamedBlob *blobs, *blob;
839 return WBC_ERR_INVALID_PARAM;
843 * Overallocate the b->name==NULL terminator for
844 * wbcNamedBlobDestructor
846 blobs = (struct wbcNamedBlob *)wbcAllocateMemory(
847 *num_blobs + 2, sizeof(struct wbcNamedBlob),
848 wbcNamedBlobDestructor);
851 return WBC_ERR_NO_MEMORY;
854 if (*pblobs != NULL) {
855 struct wbcNamedBlob *old = *pblobs;
856 memcpy(blobs, old, sizeof(struct wbcNamedBlob) * (*num_blobs));
857 if (*num_blobs != 0) {
858 /* end indicator for wbcNamedBlobDestructor */
865 blob = &blobs[*num_blobs];
867 blob->name = strdup(name);
868 BAIL_ON_PTR_ERROR(blob->name, wbc_status);
871 blob->blob.length = length;
872 blob->blob.data = (uint8_t *)malloc(length);
873 BAIL_ON_PTR_ERROR(blob->blob.data, wbc_status);
874 memcpy(blob->blob.data, data, length);
880 wbc_status = WBC_ERR_SUCCESS;
882 wbcFreeMemory(blobs);