Ignore:
Timestamp:
Nov 27, 2012, 4:43:17 PM (13 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: updated trunk to 3.6.0

Location:
trunk/server
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/server

  • trunk/server/nsswitch/libwbclient/wbc_util.c

    r414 r745  
    22   Unix SMB/CIFS implementation.
    33
    4    Winbind client API
     4   Winbind client asynchronous API, utility functions
    55
    66   Copyright (C) Gerald (Jerry) Carter 2007-2008
     
    2525#include "replace.h"
    2626#include "libwbclient.h"
    27 
    28 
     27#include "../winbind_client.h"
    2928
    3029/** @brief Ping winbindd to see if the daemon is running
     
    3231 * @return #wbcErr
    3332 **/
    34 
    3533wbcErr wbcPing(void)
    3634{
     
    4543        return wbcRequestResponse(WINBINDD_PING, &request, &response);
    4644}
     45
     46static void wbcInterfaceDetailsDestructor(void *ptr)
     47{
     48        struct wbcInterfaceDetails *i = (struct wbcInterfaceDetails *)ptr;
     49        free(i->winbind_version);
     50        free(i->netbios_name);
     51        free(i->netbios_domain);
     52        free(i->dns_domain);
     53}
     54
     55/**
     56 * @brief Query useful information about the winbind service
     57 *
     58 * @param *_details     pointer to hold the struct wbcInterfaceDetails
     59 *
     60 * @return #wbcErr
     61 */
    4762
    4863wbcErr wbcInterfaceDetails(struct wbcInterfaceDetails **_details)
     
    5974        ZERO_STRUCT(response);
    6075
    61         info = talloc(NULL, struct wbcInterfaceDetails);
     76        info = (struct wbcInterfaceDetails *)wbcAllocateMemory(
     77                1, sizeof(struct wbcInterfaceDetails),
     78                wbcInterfaceDetailsDestructor);
    6279        BAIL_ON_PTR_ERROR(info, wbc_status);
    6380
     
    7188        BAIL_ON_WBC_ERROR(wbc_status);
    7289
    73         info->winbind_version = talloc_strdup(info,
    74                                               response.data.info.samba_version);
     90        info->winbind_version = strdup(response.data.info.samba_version);
    7591        BAIL_ON_PTR_ERROR(info->winbind_version, wbc_status);
    7692        info->winbind_separator = response.data.info.winbind_separator;
     
    8096        BAIL_ON_WBC_ERROR(wbc_status);
    8197
    82         info->netbios_name = talloc_strdup(info,
    83                                            response.data.netbios_name);
     98        info->netbios_name = strdup(response.data.netbios_name);
    8499        BAIL_ON_PTR_ERROR(info->netbios_name, wbc_status);
    85100
     
    88103        BAIL_ON_WBC_ERROR(wbc_status);
    89104
    90         info->netbios_domain = talloc_strdup(info,
    91                                         response.data.domain_name);
     105        info->netbios_domain = strdup(response.data.domain_name);
    92106        BAIL_ON_PTR_ERROR(info->netbios_domain, wbc_status);
    93107
     
    102116
    103117        if (domain) {
    104                 info->dns_domain = talloc_strdup(info,
    105                                                  domain->dns_name);
     118                info->dns_domain = strdup(domain->dns_name);
    106119                wbcFreeMemory(domain);
    107120                BAIL_ON_PTR_ERROR(info->dns_domain, wbc_status);
     
    116129
    117130done:
    118         talloc_free(info);
    119         return wbc_status;
    120 }
    121 
    122 
    123 /* Lookup the current status of a trusted domain */
     131        wbcFreeMemory(info);
     132        return wbc_status;
     133}
     134
     135static void wbcDomainInfoDestructor(void *ptr)
     136{
     137        struct wbcDomainInfo *i = (struct wbcDomainInfo *)ptr;
     138        free(i->short_name);
     139        free(i->dns_name);
     140}
     141
     142/** @brief Lookup the current status of a trusted domain, sync wrapper
     143 *
     144 * @param domain      Domain to query
     145 * @param *dinfo       Pointer to returned struct wbcDomainInfo
     146 *
     147 * @return #wbcErr
     148 */
     149
    124150wbcErr wbcDomainInfo(const char *domain, struct wbcDomainInfo **dinfo)
    125151{
     
    147173        BAIL_ON_WBC_ERROR(wbc_status);
    148174
    149         info = talloc(NULL, struct wbcDomainInfo);
     175        info = (struct wbcDomainInfo *)wbcAllocateMemory(
     176                1, sizeof(struct wbcDomainInfo), wbcDomainInfoDestructor);
    150177        BAIL_ON_PTR_ERROR(info, wbc_status);
    151178
    152         info->short_name = talloc_strdup(info,
    153                                          response.data.domain_info.name);
     179        info->short_name = strdup(response.data.domain_info.name);
    154180        BAIL_ON_PTR_ERROR(info->short_name, wbc_status);
    155181
    156         info->dns_name = talloc_strdup(info,
    157                                        response.data.domain_info.alt_name);
     182        info->dns_name = strdup(response.data.domain_info.alt_name);
    158183        BAIL_ON_PTR_ERROR(info->dns_name, wbc_status);
    159184
     
    170195
    171196        *dinfo = info;
     197        info = NULL;
    172198
    173199        wbc_status = WBC_ERR_SUCCESS;
    174200
    175201 done:
    176         if (!WBC_ERROR_IS_OK(wbc_status)) {
    177                 talloc_free(info);
    178         }
    179 
    180         return wbc_status;
    181 }
    182 
     202        wbcFreeMemory(info);
     203        return wbc_status;
     204}
     205
     206/* Get the list of current DCs */
     207wbcErr wbcDcInfo(const char *domain, size_t *num_dcs,
     208                 const char ***dc_names, const char ***dc_ips)
     209{
     210        struct winbindd_request request;
     211        struct winbindd_response response;
     212        const char **names = NULL;
     213        const char **ips = NULL;
     214        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
     215        size_t extra_len;
     216        int i;
     217        char *p;
     218
     219        /* Initialise request */
     220
     221        ZERO_STRUCT(request);
     222        ZERO_STRUCT(response);
     223
     224        if (domain != NULL) {
     225                strncpy(request.domain_name, domain,
     226                        sizeof(request.domain_name) - 1);
     227        }
     228
     229        wbc_status = wbcRequestResponse(WINBINDD_DC_INFO,
     230                                        &request, &response);
     231        BAIL_ON_WBC_ERROR(wbc_status);
     232
     233        names = wbcAllocateStringArray(response.data.num_entries);
     234        BAIL_ON_PTR_ERROR(names, wbc_status);
     235
     236        ips = wbcAllocateStringArray(response.data.num_entries);
     237        BAIL_ON_PTR_ERROR(ips, wbc_status);
     238
     239        wbc_status = WBC_ERR_INVALID_RESPONSE;
     240
     241        p = (char *)response.extra_data.data;
     242
     243        if (response.length < (sizeof(struct winbindd_response)+1)) {
     244                goto done;
     245        }
     246
     247        extra_len = response.length - sizeof(struct winbindd_response);
     248
     249        if (p[extra_len-1] != '\0') {
     250                goto done;
     251        }
     252
     253        for (i=0; i<response.data.num_entries; i++) {
     254                char *q;
     255
     256                q = strchr(p, '\n');
     257                if (q == NULL) {
     258                        goto done;
     259                }
     260                names[i] = strndup(p, q-p);
     261                BAIL_ON_PTR_ERROR(names[i], wbc_status);
     262                p = q+1;
     263
     264                q = strchr(p, '\n');
     265                if (q == NULL) {
     266                        goto done;
     267                }
     268                ips[i] = strndup(p, q-p);
     269                BAIL_ON_PTR_ERROR(ips[i], wbc_status);
     270                p = q+1;
     271        }
     272        if (p[0] != '\0') {
     273                goto done;
     274        }
     275
     276        wbc_status = WBC_ERR_SUCCESS;
     277done:
     278        if (response.extra_data.data)
     279                free(response.extra_data.data);
     280
     281        if (WBC_ERROR_IS_OK(wbc_status)) {
     282                *num_dcs = response.data.num_entries;
     283                *dc_names = names;
     284                names = NULL;
     285                *dc_ips = ips;
     286                ips = NULL;
     287        }
     288        wbcFreeMemory(names);
     289        wbcFreeMemory(ips);
     290        return wbc_status;
     291}
    183292
    184293/* Resolve a NetbiosName via WINS */
     
    205314        /* Display response */
    206315
    207         ipaddr = talloc_strdup(NULL, response.data.winsresp);
     316        ipaddr = wbcStrDup(response.data.winsresp);
    208317        BAIL_ON_PTR_ERROR(ipaddr, wbc_status);
    209318
     
    238347        /* Display response */
    239348
    240         name_str = talloc_strdup(NULL, response.data.winsresp);
     349        name_str = wbcStrDup(response.data.winsresp);
    241350        BAIL_ON_PTR_ERROR(name_str, wbc_status);
    242351
     
    251360 */
    252361
    253 static wbcErr process_domain_info_string(TALLOC_CTX *ctx,
    254                                          struct wbcDomainInfo *info,
     362static wbcErr process_domain_info_string(struct wbcDomainInfo *info,
    255363                                         char *info_string)
    256364{
     
    258366        char *r = NULL;
    259367        char *s = NULL;
    260 
    261         if (!info || !info_string) {
    262                 wbc_status = WBC_ERR_INVALID_PARAM;
    263                 BAIL_ON_WBC_ERROR(wbc_status);
    264         }
    265 
    266         ZERO_STRUCTP(info);
    267368
    268369        r = info_string;
     
    276377        s++;
    277378
    278         info->short_name = talloc_strdup(ctx, r);
     379        info->short_name = strdup(r);
    279380        BAIL_ON_PTR_ERROR(info->short_name, wbc_status);
    280381
     
    289390        s++;
    290391
    291         info->dns_name = talloc_strdup(ctx, r);
     392        info->dns_name = strdup(r);
    292393        BAIL_ON_PTR_ERROR(info->dns_name, wbc_status);
    293394
     
    382483}
    383484
     485static void wbcDomainInfoListDestructor(void *ptr)
     486{
     487        struct wbcDomainInfo *i = (struct wbcDomainInfo *)ptr;
     488
     489        while (i->short_name != NULL) {
     490                free(i->short_name);
     491                free(i->dns_name);
     492                i += 1;
     493        }
     494}
     495
    384496/* Enumerate the domain trusts known by Winbind */
    385497wbcErr wbcListTrusts(struct wbcDomainInfo **domains, size_t *num_domains)
     
    388500        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
    389501        char *p = NULL;
    390         char *q = NULL;
    391502        char *extra_data = NULL;
    392         int count = 0;
    393503        struct wbcDomainInfo *d_list = NULL;
    394504        int i = 0;
     
    418528        }
    419529
    420         /* Count number of domains */
    421 
    422         count = 0;
    423         while (p) {
    424                 count++;
    425 
    426                 if ((q = strchr(p, '\n')) != NULL)
    427                         q++;
    428                 p = q;
    429         }
    430 
    431         d_list = talloc_array(NULL, struct wbcDomainInfo, count);
     530        d_list = (struct wbcDomainInfo *)wbcAllocateMemory(
     531                response.data.num_entries + 1,sizeof(struct wbcDomainInfo),
     532                wbcDomainInfoListDestructor);
    432533        BAIL_ON_PTR_ERROR(d_list, wbc_status);
    433534
     
    439540        /* Outer loop processes the list of domain information */
    440541
    441         for (i=0; i<count && p; i++) {
     542        for (i=0; i<response.data.num_entries && p; i++) {
    442543                char *next = strchr(p, '\n');
    443544
     
    447548                }
    448549
    449                 wbc_status = process_domain_info_string(d_list, &d_list[i], p);
     550                wbc_status = process_domain_info_string(&d_list[i], p);
    450551                BAIL_ON_WBC_ERROR(wbc_status);
    451552
     
    454555
    455556        *domains = d_list;
     557        d_list = NULL;
    456558        *num_domains = i;
    457559
    458560 done:
    459         if (!WBC_ERROR_IS_OK(wbc_status)) {
    460                 if (d_list)
    461                         talloc_free(d_list);
    462                 if (extra_data)
    463                         free(extra_data);
    464         }
    465 
    466         return wbc_status;
     561        winbindd_free_response(&response);
     562        wbcFreeMemory(d_list);
     563        free(extra_data);
     564        return wbc_status;
     565}
     566
     567static void wbcDomainControllerInfoDestructor(void *ptr)
     568{
     569        struct wbcDomainControllerInfo *i =
     570                (struct wbcDomainControllerInfo *)ptr;
     571        free(i->dc_name);
    467572}
    468573
     
    492597        request.flags = flags;
    493598
    494         dc = talloc(NULL, struct wbcDomainControllerInfo);
     599        dc = (struct wbcDomainControllerInfo *)wbcAllocateMemory(
     600                 1, sizeof(struct wbcDomainControllerInfo),
     601                wbcDomainControllerInfoDestructor);
    495602        BAIL_ON_PTR_ERROR(dc, wbc_status);
    496603
     
    502609        BAIL_ON_WBC_ERROR(wbc_status);
    503610
    504         dc->dc_name = talloc_strdup(dc, response.data.dsgetdcname.dc_unc);
     611        dc->dc_name = strdup(response.data.dsgetdcname.dc_unc);
    505612        BAIL_ON_PTR_ERROR(dc->dc_name, wbc_status);
    506613
    507614        *dc_info = dc;
     615        dc = NULL;
    508616
    509617done:
    510         if (!WBC_ERROR_IS_OK(wbc_status)) {
    511                 talloc_free(dc);
    512         }
    513 
    514         return wbc_status;
    515 }
    516 
    517 static wbcErr wbc_create_domain_controller_info_ex(TALLOC_CTX *mem_ctx,
    518                                                    const struct winbindd_response *resp,
     618        wbcFreeMemory(dc);
     619        return wbc_status;
     620}
     621
     622static void wbcDomainControllerInfoExDestructor(void *ptr)
     623{
     624        struct wbcDomainControllerInfoEx *i =
     625                (struct wbcDomainControllerInfoEx *)ptr;
     626        free((char *)(i->dc_unc));
     627        free((char *)(i->dc_address));
     628        free((char *)(i->domain_guid));
     629        free((char *)(i->domain_name));
     630        free((char *)(i->forest_name));
     631        free((char *)(i->dc_site_name));
     632        free((char *)(i->client_site_name));
     633}
     634
     635static wbcErr wbc_create_domain_controller_info_ex(const struct winbindd_response *resp,
    519636                                                   struct wbcDomainControllerInfoEx **_i)
    520637{
     
    523640        struct wbcGuid guid;
    524641
    525         i = talloc(mem_ctx, struct wbcDomainControllerInfoEx);
     642        i = (struct wbcDomainControllerInfoEx *)wbcAllocateMemory(
     643                1, sizeof(struct wbcDomainControllerInfoEx),
     644                wbcDomainControllerInfoExDestructor);
    526645        BAIL_ON_PTR_ERROR(i, wbc_status);
    527646
    528         i->dc_unc = talloc_strdup(i, resp->data.dsgetdcname.dc_unc);
     647        i->dc_unc = strdup(resp->data.dsgetdcname.dc_unc);
    529648        BAIL_ON_PTR_ERROR(i->dc_unc, wbc_status);
    530649
    531         i->dc_address = talloc_strdup(i, resp->data.dsgetdcname.dc_address);
     650        i->dc_address = strdup(resp->data.dsgetdcname.dc_address);
    532651        BAIL_ON_PTR_ERROR(i->dc_address, wbc_status);
    533652
     
    536655        wbc_status = wbcStringToGuid(resp->data.dsgetdcname.domain_guid, &guid);
    537656        if (WBC_ERROR_IS_OK(wbc_status)) {
    538                 i->domain_guid = talloc(i, struct wbcGuid);
     657                i->domain_guid = (struct wbcGuid *)malloc(
     658                        sizeof(struct wbcGuid));
    539659                BAIL_ON_PTR_ERROR(i->domain_guid, wbc_status);
    540660
    541661                *i->domain_guid = guid;
    542         } else {
    543                 i->domain_guid = NULL;
    544         }
    545 
    546         i->domain_name = talloc_strdup(i, resp->data.dsgetdcname.domain_name);
     662        }
     663
     664        i->domain_name = strdup(resp->data.dsgetdcname.domain_name);
    547665        BAIL_ON_PTR_ERROR(i->domain_name, wbc_status);
    548666
    549667        if (resp->data.dsgetdcname.forest_name[0] != '\0') {
    550                 i->forest_name = talloc_strdup(i,
    551                         resp->data.dsgetdcname.forest_name);
     668                i->forest_name = strdup(resp->data.dsgetdcname.forest_name);
    552669                BAIL_ON_PTR_ERROR(i->forest_name, wbc_status);
    553         } else {
    554                 i->forest_name = NULL;
    555670        }
    556671
     
    558673
    559674        if (resp->data.dsgetdcname.dc_site_name[0] != '\0') {
    560                 i->dc_site_name = talloc_strdup(i,
    561                         resp->data.dsgetdcname.dc_site_name);
     675                i->dc_site_name = strdup(resp->data.dsgetdcname.dc_site_name);
    562676                BAIL_ON_PTR_ERROR(i->dc_site_name, wbc_status);
    563         } else {
    564                 i->dc_site_name = NULL;
    565677        }
    566678
    567679        if (resp->data.dsgetdcname.client_site_name[0] != '\0') {
    568                 i->client_site_name = talloc_strdup(i,
     680                i->client_site_name = strdup(
    569681                        resp->data.dsgetdcname.client_site_name);
    570682                BAIL_ON_PTR_ERROR(i->client_site_name, wbc_status);
    571         } else {
    572                 i->client_site_name = NULL;
    573683        }
    574684
     
    577687
    578688done:
    579         talloc_free(i);
     689        if (i != NULL) {
     690                wbcFreeMemory(i);
     691        }
    580692        return wbc_status;
    581693}
     
    632744
    633745        if (dc_info) {
    634                 wbc_status = wbc_create_domain_controller_info_ex(NULL,
    635                                                                   &response,
     746                wbc_status = wbc_create_domain_controller_info_ex(&response,
    636747                                                                  dc_info);
    637748                BAIL_ON_WBC_ERROR(wbc_status);
     
    643754}
    644755
     756static void wbcNamedBlobDestructor(void *ptr)
     757{
     758        struct wbcNamedBlob *b = (struct wbcNamedBlob *)ptr;
     759
     760        while (b->name != NULL) {
     761                free((char *)(b->name));
     762                free(b->blob.data);
     763                b += 1;
     764        }
     765}
     766
    645767/* Initialize a named blob and add to list of blobs */
    646768wbcErr wbcAddNamedBlob(size_t *num_blobs,
    647                        struct wbcNamedBlob **blobs,
     769                       struct wbcNamedBlob **pblobs,
    648770                       const char *name,
    649771                       uint32_t flags,
     
    652774{
    653775        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
    654         struct wbcNamedBlob blob;
    655 
    656         *blobs = talloc_realloc(NULL, *blobs, struct wbcNamedBlob,
    657                                 *(num_blobs)+1);
    658         BAIL_ON_PTR_ERROR(*blobs, wbc_status);
    659 
    660         blob.name               = talloc_strdup(*blobs, name);
    661         BAIL_ON_PTR_ERROR(blob.name, wbc_status);
    662         blob.flags              = flags;
    663         blob.blob.length        = length;
    664         blob.blob.data          = (uint8_t *)talloc_memdup(*blobs, data, length);
    665         BAIL_ON_PTR_ERROR(blob.blob.data, wbc_status);
    666 
    667         (*(blobs))[*num_blobs] = blob;
    668         *(num_blobs) += 1;
     776        struct wbcNamedBlob *blobs, *blob;
     777
     778        if (name == NULL) {
     779                return WBC_ERR_INVALID_PARAM;
     780        }
     781
     782        /*
     783         * Overallocate the b->name==NULL terminator for
     784         * wbcNamedBlobDestructor
     785         */
     786        blobs = (struct wbcNamedBlob *)wbcAllocateMemory(
     787                *num_blobs + 2, sizeof(struct wbcNamedBlob),
     788                wbcNamedBlobDestructor);
     789
     790        if (blobs == NULL) {
     791                return WBC_ERR_NO_MEMORY;
     792        }
     793
     794        if (*pblobs != NULL) {
     795                struct wbcNamedBlob *old = *pblobs;
     796                memcpy(blobs, old, sizeof(struct wbcNamedBlob) * (*num_blobs));
     797                if (*num_blobs != 0) {
     798                        /* end indicator for wbcNamedBlobDestructor */
     799                        old[0].name = NULL;
     800                }
     801                wbcFreeMemory(old);
     802        }
     803        *pblobs = blobs;
     804
     805        blob = &blobs[*num_blobs];
     806
     807        blob->name = strdup(name);
     808        BAIL_ON_PTR_ERROR(blob->name, wbc_status);
     809        blob->flags = flags;
     810
     811        blob->blob.length = length;
     812        blob->blob.data = (uint8_t *)malloc(length);
     813        BAIL_ON_PTR_ERROR(blob->blob.data, wbc_status);
     814        memcpy(blob->blob.data, data, length);
     815
     816        *num_blobs += 1;
     817        *pblobs = blobs;
     818        blobs = NULL;
    669819
    670820        wbc_status = WBC_ERR_SUCCESS;
    671821done:
    672         if (!WBC_ERROR_IS_OK(wbc_status) && blobs) {
    673                 wbcFreeMemory(*blobs);
    674         }
    675         return wbc_status;
    676 }
     822        wbcFreeMemory(blobs);
     823        return wbc_status;
     824}
Note: See TracChangeset for help on using the changeset viewer.