2 Unix SMB/CIFS implementation.
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 */
25 #include "libwbclient.h"
29 /** @brief Ping winbindd to see if the daemon is running
36 struct winbindd_request request;
37 struct winbindd_response response;
39 /* Initialize request */
42 ZERO_STRUCT(response);
44 return wbcRequestResponse(WINBINDD_PING, &request, &response);
47 wbcErr wbcInterfaceDetails(struct wbcInterfaceDetails **_details)
49 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
50 struct wbcInterfaceDetails *info;
51 struct wbcDomainInfo *domain = NULL;
52 struct winbindd_request request;
53 struct winbindd_response response;
55 /* Initialize request */
58 ZERO_STRUCT(response);
60 info = talloc(NULL, struct wbcInterfaceDetails);
61 BAIL_ON_PTR_ERROR(info, wbc_status);
63 /* first the interface version */
64 wbc_status = wbcRequestResponse(WINBINDD_INTERFACE_VERSION, NULL, &response);
65 BAIL_ON_WBC_ERROR(wbc_status);
66 info->interface_version = response.data.interface_version;
68 /* then the samba version and the winbind separator */
69 wbc_status = wbcRequestResponse(WINBINDD_INFO, NULL, &response);
70 BAIL_ON_WBC_ERROR(wbc_status);
72 info->winbind_version = talloc_strdup(info,
73 response.data.info.samba_version);
74 BAIL_ON_PTR_ERROR(info->winbind_version, wbc_status);
75 info->winbind_separator = response.data.info.winbind_separator;
77 /* then the local netbios name */
78 wbc_status = wbcRequestResponse(WINBINDD_NETBIOS_NAME, NULL, &response);
79 BAIL_ON_WBC_ERROR(wbc_status);
81 info->netbios_name = talloc_strdup(info,
82 response.data.netbios_name);
83 BAIL_ON_PTR_ERROR(info->netbios_name, wbc_status);
85 /* then the local workgroup name */
86 wbc_status = wbcRequestResponse(WINBINDD_DOMAIN_NAME, NULL, &response);
87 BAIL_ON_WBC_ERROR(wbc_status);
89 info->netbios_domain = talloc_strdup(info,
90 response.data.domain_name);
91 BAIL_ON_PTR_ERROR(info->netbios_domain, wbc_status);
93 wbc_status = wbcDomainInfo(info->netbios_domain, &domain);
94 if (wbc_status == WBC_ERR_DOMAIN_NOT_FOUND) {
95 /* maybe it's a standalone server */
97 wbc_status = WBC_ERR_SUCCESS;
99 BAIL_ON_WBC_ERROR(wbc_status);
103 info->dns_domain = talloc_strdup(info,
105 wbcFreeMemory(domain);
106 BAIL_ON_PTR_ERROR(info->dns_domain, wbc_status);
108 info->dns_domain = NULL;
114 wbc_status = WBC_ERR_SUCCESS;
122 /** @brief Lookup the current status of a trusted domain
124 * @param domain Domain to query
125 * @param *dinfo Pointer to returned domain_info struct
132 wbcErr wbcDomainInfo(const char *domain, struct wbcDomainInfo **dinfo)
134 struct winbindd_request request;
135 struct winbindd_response response;
136 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
137 struct wbcDomainInfo *info = NULL;
139 if (!domain || !dinfo) {
140 wbc_status = WBC_ERR_INVALID_PARAM;
141 BAIL_ON_WBC_ERROR(wbc_status);
144 /* Initialize request */
146 ZERO_STRUCT(request);
147 ZERO_STRUCT(response);
149 strncpy(request.domain_name, domain,
150 sizeof(request.domain_name)-1);
152 wbc_status = wbcRequestResponse(WINBINDD_DOMAIN_INFO,
155 BAIL_ON_WBC_ERROR(wbc_status);
157 info = talloc(NULL, struct wbcDomainInfo);
158 BAIL_ON_PTR_ERROR(info, wbc_status);
160 info->short_name = talloc_strdup(info,
161 response.data.domain_info.name);
162 BAIL_ON_PTR_ERROR(info->short_name, wbc_status);
164 info->dns_name = talloc_strdup(info,
165 response.data.domain_info.alt_name);
166 BAIL_ON_PTR_ERROR(info->dns_name, wbc_status);
168 wbc_status = wbcStringToSid(response.data.domain_info.sid,
170 BAIL_ON_WBC_ERROR(wbc_status);
172 if (response.data.domain_info.native_mode)
173 info->domain_flags |= WBC_DOMINFO_DOMAIN_NATIVE;
174 if (response.data.domain_info.active_directory)
175 info->domain_flags |= WBC_DOMINFO_DOMAIN_AD;
176 if (response.data.domain_info.primary)
177 info->domain_flags |= WBC_DOMINFO_DOMAIN_PRIMARY;
181 wbc_status = WBC_ERR_SUCCESS;
184 if (!WBC_ERROR_IS_OK(wbc_status)) {
192 /** @brief Resolve a NetbiosName via WINS
194 * @param name Name to resolve
195 * @param *ip Pointer to the ip address string
200 wbcErr wbcResolveWinsByName(const char *name, char **ip)
202 struct winbindd_request request;
203 struct winbindd_response response;
204 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
207 ZERO_STRUCT(request);
208 ZERO_STRUCT(response);
212 strncpy(request.data.winsreq, name,
213 sizeof(request.data.winsreq)-1);
215 wbc_status = wbcRequestResponse(WINBINDD_WINS_BYNAME,
218 BAIL_ON_WBC_ERROR(wbc_status);
220 /* Display response */
222 ipaddr = talloc_strdup(NULL, response.data.winsresp);
223 BAIL_ON_PTR_ERROR(ipaddr, wbc_status);
226 wbc_status = WBC_ERR_SUCCESS;
232 /** @brief Resolve an IP address via WINS into a NetbiosName
234 * @param ip The ip address string
235 * @param *name Pointer to the name
240 wbcErr wbcResolveWinsByIP(const char *ip, char **name)
242 struct winbindd_request request;
243 struct winbindd_response response;
244 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
247 ZERO_STRUCT(request);
248 ZERO_STRUCT(response);
252 strncpy(request.data.winsreq, ip,
253 sizeof(request.data.winsreq)-1);
255 wbc_status = wbcRequestResponse(WINBINDD_WINS_BYIP,
258 BAIL_ON_WBC_ERROR(wbc_status);
260 /* Display response */
262 name_str = talloc_strdup(NULL, response.data.winsresp);
263 BAIL_ON_PTR_ERROR(name_str, wbc_status);
266 wbc_status = WBC_ERR_SUCCESS;
275 static wbcErr process_domain_info_string(TALLOC_CTX *ctx,
276 struct wbcDomainInfo *info,
279 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
283 if (!info || !info_string) {
284 wbc_status = WBC_ERR_INVALID_PARAM;
285 BAIL_ON_WBC_ERROR(wbc_status);
291 if ((s = strchr(r, '\\')) == NULL) {
292 wbc_status = WBC_ERR_INVALID_RESPONSE;
293 BAIL_ON_WBC_ERROR(wbc_status);
298 info->short_name = talloc_strdup(ctx, r);
299 BAIL_ON_PTR_ERROR(info->short_name, wbc_status);
304 if ((s = strchr(r, '\\')) == NULL) {
305 wbc_status = WBC_ERR_INVALID_RESPONSE;
306 BAIL_ON_WBC_ERROR(wbc_status);
311 info->dns_name = talloc_strdup(ctx, r);
312 BAIL_ON_PTR_ERROR(info->dns_name, wbc_status);
316 if ((s = strchr(r, '\\')) == NULL) {
317 wbc_status = WBC_ERR_INVALID_RESPONSE;
318 BAIL_ON_WBC_ERROR(wbc_status);
323 wbc_status = wbcStringToSid(r, &info->sid);
324 BAIL_ON_WBC_ERROR(wbc_status);
328 if ((s = strchr(r, '\\')) == NULL) {
329 wbc_status = WBC_ERR_INVALID_RESPONSE;
330 BAIL_ON_WBC_ERROR(wbc_status);
335 if (strcmp(r, "None") == 0) {
336 info->trust_type = WBC_DOMINFO_TRUSTTYPE_NONE;
337 } else if (strcmp(r, "External") == 0) {
338 info->trust_type = WBC_DOMINFO_TRUSTTYPE_EXTERNAL;
339 } else if (strcmp(r, "Forest") == 0) {
340 info->trust_type = WBC_DOMINFO_TRUSTTYPE_FOREST;
341 } else if (strcmp(r, "In Forest") == 0) {
342 info->trust_type = WBC_DOMINFO_TRUSTTYPE_IN_FOREST;
344 wbc_status = WBC_ERR_INVALID_RESPONSE;
345 BAIL_ON_WBC_ERROR(wbc_status);
350 if ((s = strchr(r, '\\')) == NULL) {
351 wbc_status = WBC_ERR_INVALID_RESPONSE;
352 BAIL_ON_WBC_ERROR(wbc_status);
357 if (strcmp(r, "Yes") == 0) {
358 info->trust_flags |= WBC_DOMINFO_TRUST_TRANSITIVE;
363 if ((s = strchr(r, '\\')) == NULL) {
364 wbc_status = WBC_ERR_INVALID_RESPONSE;
365 BAIL_ON_WBC_ERROR(wbc_status);
370 if (strcmp(r, "Yes") == 0) {
371 info->trust_flags |= WBC_DOMINFO_TRUST_INCOMING;
376 if ((s = strchr(r, '\\')) == NULL) {
377 wbc_status = WBC_ERR_INVALID_RESPONSE;
378 BAIL_ON_WBC_ERROR(wbc_status);
383 if (strcmp(r, "Yes") == 0) {
384 info->trust_flags |= WBC_DOMINFO_TRUST_OUTGOING;
387 /* Online/Offline status */
391 wbc_status = WBC_ERR_INVALID_RESPONSE;
392 BAIL_ON_WBC_ERROR(wbc_status);
394 if ( strcmp(r, "Offline") == 0) {
395 info->domain_flags |= WBC_DOMINFO_DOMAIN_OFFLINE;
398 wbc_status = WBC_ERR_SUCCESS;
404 /** @brief Enumerate the domain trusts known by Winbind
406 * @param **domains Pointer to the allocated domain list array
407 * @param *num_domains Pointer to number of domains returned
412 wbcErr wbcListTrusts(struct wbcDomainInfo **domains, size_t *num_domains)
414 struct winbindd_response response;
415 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
418 char *extra_data = NULL;
420 struct wbcDomainInfo *d_list = NULL;
426 ZERO_STRUCT(response);
430 wbc_status = wbcRequestResponse(WINBINDD_LIST_TRUSTDOM,
433 BAIL_ON_WBC_ERROR(wbc_status);
435 /* Decode the response */
437 p = (char *)response.extra_data.data;
439 if (strlen(p) == 0) {
440 /* We should always at least get back our
443 wbc_status = WBC_ERR_DOMAIN_NOT_FOUND;
444 BAIL_ON_WBC_ERROR(wbc_status);
447 /* Count number of domains */
453 if ((q = strchr(p, '\n')) != NULL)
458 d_list = talloc_array(NULL, struct wbcDomainInfo, count);
459 BAIL_ON_PTR_ERROR(d_list, wbc_status);
461 extra_data = strdup((char*)response.extra_data.data);
462 BAIL_ON_PTR_ERROR(extra_data, wbc_status);
466 /* Outer loop processes the list of domain information */
468 for (i=0; i<count && p; i++) {
469 char *next = strchr(p, '\n');
476 wbc_status = process_domain_info_string(d_list, &d_list[i], p);
477 BAIL_ON_WBC_ERROR(wbc_status);
486 if (!WBC_ERROR_IS_OK(wbc_status)) {
496 /** @brief Enumerate the domain trusts known by Winbind
498 * @param domain Name of the domain to query for a DC
499 * @param flags Bit flags used to control the domain location query
500 * @param *dc_info Pointer to the returned domain controller information
508 wbcErr wbcLookupDomainController(const char *domain,
510 struct wbcDomainControllerInfo **dc_info)
512 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
513 struct winbindd_request request;
514 struct winbindd_response response;
515 struct wbcDomainControllerInfo *dc = NULL;
517 /* validate input params */
519 if (!domain || !dc_info) {
520 wbc_status = WBC_ERR_INVALID_PARAM;
521 BAIL_ON_WBC_ERROR(wbc_status);
524 ZERO_STRUCT(request);
525 ZERO_STRUCT(response);
527 strncpy(request.domain_name, domain, sizeof(request.domain_name)-1);
529 request.flags = flags;
531 dc = talloc(NULL, struct wbcDomainControllerInfo);
532 BAIL_ON_PTR_ERROR(dc, wbc_status);
536 wbc_status = wbcRequestResponse(WINBINDD_DSGETDCNAME,
539 BAIL_ON_WBC_ERROR(wbc_status);
541 dc->dc_name = talloc_strdup(dc, response.data.dc_name);
542 BAIL_ON_PTR_ERROR(dc->dc_name, wbc_status);
547 if (!WBC_ERROR_IS_OK(wbc_status)) {
554 static wbcErr wbc_create_domain_controller_info_ex(TALLOC_CTX *mem_ctx,
555 const struct winbindd_response *resp,
556 struct wbcDomainControllerInfoEx **_i)
558 wbcErr wbc_status = WBC_ERR_SUCCESS;
559 struct wbcDomainControllerInfoEx *i;
562 i = talloc(mem_ctx, struct wbcDomainControllerInfoEx);
563 BAIL_ON_PTR_ERROR(i, wbc_status);
565 i->dc_unc = talloc_strdup(i, resp->data.dsgetdcname.dc_unc);
566 BAIL_ON_PTR_ERROR(i->dc_unc, wbc_status);
568 i->dc_address = talloc_strdup(i, resp->data.dsgetdcname.dc_address);
569 BAIL_ON_PTR_ERROR(i->dc_address, wbc_status);
571 i->dc_address_type = resp->data.dsgetdcname.dc_address_type;
573 wbc_status = wbcStringToGuid(resp->data.dsgetdcname.domain_guid, &guid);
574 if (WBC_ERROR_IS_OK(wbc_status)) {
575 i->domain_guid = talloc(i, struct wbcGuid);
576 BAIL_ON_PTR_ERROR(i->domain_guid, wbc_status);
578 *i->domain_guid = guid;
580 i->domain_guid = NULL;
583 i->domain_name = talloc_strdup(i, resp->data.dsgetdcname.domain_name);
584 BAIL_ON_PTR_ERROR(i->domain_name, wbc_status);
586 if (resp->data.dsgetdcname.forest_name[0] != '\0') {
587 i->forest_name = talloc_strdup(i,
588 resp->data.dsgetdcname.forest_name);
589 BAIL_ON_PTR_ERROR(i->forest_name, wbc_status);
591 i->forest_name = NULL;
594 i->dc_flags = resp->data.dsgetdcname.dc_flags;
596 if (resp->data.dsgetdcname.dc_site_name[0] != '\0') {
597 i->dc_site_name = talloc_strdup(i,
598 resp->data.dsgetdcname.dc_site_name);
599 BAIL_ON_PTR_ERROR(i->dc_site_name, wbc_status);
601 i->dc_site_name = NULL;
604 if (resp->data.dsgetdcname.client_site_name[0] != '\0') {
605 i->client_site_name = talloc_strdup(i,
606 resp->data.dsgetdcname.client_site_name);
607 BAIL_ON_PTR_ERROR(i->client_site_name, wbc_status);
609 i->client_site_name = NULL;
620 /** @brief Get extended domain controller information
622 * @param domain Name of the domain to query for a DC
623 * @param guid Guid of the domain to query for a DC
624 * @param site Site of the domain to query for a DC
625 * @param flags Bit flags used to control the domain location query
626 * @param *dc_info Pointer to the returned extended domain controller information
632 wbcErr wbcLookupDomainControllerEx(const char *domain,
633 struct wbcGuid *guid,
636 struct wbcDomainControllerInfoEx **dc_info)
638 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
639 struct winbindd_request request;
640 struct winbindd_response response;
642 /* validate input params */
644 if (!domain || !dc_info) {
645 wbc_status = WBC_ERR_INVALID_PARAM;
646 BAIL_ON_WBC_ERROR(wbc_status);
649 ZERO_STRUCT(request);
650 ZERO_STRUCT(response);
652 request.data.dsgetdcname.flags = flags;
654 strncpy(request.data.dsgetdcname.domain_name, domain,
655 sizeof(request.data.dsgetdcname.domain_name)-1);
658 strncpy(request.data.dsgetdcname.site_name, site,
659 sizeof(request.data.dsgetdcname.site_name)-1);
665 wbc_status = wbcGuidToString(guid, &str);
666 BAIL_ON_WBC_ERROR(wbc_status);
668 strncpy(request.data.dsgetdcname.domain_guid, str,
669 sizeof(request.data.dsgetdcname.domain_guid)-1);
676 wbc_status = wbcRequestResponse(WINBINDD_DSGETDCNAME,
679 BAIL_ON_WBC_ERROR(wbc_status);
682 wbc_status = wbc_create_domain_controller_info_ex(NULL,
685 BAIL_ON_WBC_ERROR(wbc_status);
688 wbc_status = WBC_ERR_SUCCESS;
693 /** @brief Initialize a named blob and add to list of blobs
695 * @param[in,out] num_blobs Pointer to the number of blobs
696 * @param[in,out] blobs Pointer to an array of blobs
697 * @param[in] name Name of the new named blob
698 * @param[in] flags Flags of the new named blob
699 * @param[in] data Blob data of new blob
700 * @param[in] length Blob data length of new blob
706 wbcErr wbcAddNamedBlob(size_t *num_blobs,
707 struct wbcNamedBlob **blobs,
713 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
714 struct wbcNamedBlob blob;
716 *blobs = talloc_realloc(NULL, *blobs, struct wbcNamedBlob,
718 BAIL_ON_PTR_ERROR(*blobs, wbc_status);
720 blob.name = talloc_strdup(*blobs, name);
721 BAIL_ON_PTR_ERROR(blob.name, wbc_status);
723 blob.blob.length = length;
724 blob.blob.data = (uint8_t *)talloc_memdup(*blobs, data, length);
725 BAIL_ON_PTR_ERROR(blob.blob.data, wbc_status);
727 (*(blobs))[*num_blobs] = blob;
730 wbc_status = WBC_ERR_SUCCESS;
732 if (!WBC_ERROR_IS_OK(wbc_status) && blobs) {
733 wbcFreeMemory(*blobs);