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_sid.c

    r414 r745  
    55
    66   Copyright (C) Gerald (Jerry) Carter 2007
     7   Copyright (C) Volker Lendecke 2010
    78
    89
     
    2526#include "replace.h"
    2627#include "libwbclient.h"
    27 
    28 
    29 /* Convert a binary SID to a character string */
    30 wbcErr wbcSidToString(const struct wbcDomainSid *sid,
    31                       char **sid_string)
    32 {
    33         wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
     28#include "../winbind_client.h"
     29
     30/* Convert a sid to a string into a buffer. Return the string
     31 * length. If buflen is too small, return the string length that would
     32 * result if it was long enough. */
     33int wbcSidToStringBuf(const struct wbcDomainSid *sid, char *buf, int buflen)
     34{
    3435        uint32_t id_auth;
    35         int i;
    36         char *tmp = NULL;
     36        int i, ofs;
    3737
    3838        if (!sid) {
    39                 wbc_status = WBC_ERR_INVALID_SID;
    40                 BAIL_ON_WBC_ERROR(wbc_status);
    41         }
     39                strlcpy(buf, "(NULL SID)", buflen);
     40                return 10;      /* strlen("(NULL SID)") */
     41        }
     42
     43        /*
     44         * BIG NOTE: this function only does SIDS where the identauth is not
     45         * >= ^32 in a range of 2^48.
     46         */
    4247
    4348        id_auth = sid->id_auth[5] +
     
    4651                (sid->id_auth[2] << 24);
    4752
    48         tmp = talloc_asprintf(NULL, "S-%d-%d", sid->sid_rev_num, id_auth);
    49         BAIL_ON_PTR_ERROR(tmp, wbc_status);
    50 
    51         for (i=0; i<sid->num_auths; i++) {
    52                 char *tmp2;
    53                 tmp2 = talloc_asprintf_append(tmp, "-%u", sid->sub_auths[i]);
    54                 BAIL_ON_PTR_ERROR(tmp2, wbc_status);
    55 
    56                 tmp = tmp2;
    57         }
    58 
    59         *sid_string = tmp;
    60         tmp = NULL;
    61 
    62         wbc_status = WBC_ERR_SUCCESS;
    63 
    64 done:
    65         talloc_free(tmp);
    66 
    67         return wbc_status;
     53        ofs = snprintf(buf, buflen, "S-%u-%lu",
     54                       (unsigned int)sid->sid_rev_num, (unsigned long)id_auth);
     55
     56        for (i = 0; i < sid->num_auths; i++) {
     57                ofs += snprintf(buf + ofs, MAX(buflen - ofs, 0), "-%lu",
     58                                (unsigned long)sid->sub_auths[i]);
     59        }
     60        return ofs;
     61}
     62
     63/* Convert a binary SID to a character string */
     64wbcErr wbcSidToString(const struct wbcDomainSid *sid,
     65                      char **sid_string)
     66{
     67        char buf[WBC_SID_STRING_BUFLEN];
     68        char *result;
     69        int len;
     70
     71        if (!sid) {
     72                return WBC_ERR_INVALID_SID;
     73        }
     74
     75        len = wbcSidToStringBuf(sid, buf, sizeof(buf));
     76
     77        if (len+1 > sizeof(buf)) {
     78                return WBC_ERR_INVALID_SID;
     79        }
     80
     81        result = (char *)wbcAllocateMemory(len+1, 1, NULL);
     82        if (result == NULL) {
     83                return WBC_ERR_NO_MEMORY;
     84        }
     85        memcpy(result, buf, len+1);
     86
     87        *sid_string = result;
     88        return WBC_ERR_SUCCESS;
    6889}
    6990
     
    132153                sid->sub_auths[sid->num_auths++] = x;
    133154
    134                 if ((*q!='-') || (*q=='\0'))
     155                if (*q != '-') {
    135156                        break;
     157                }
    136158                p = q + 1;
    137159        }
     
    150172
    151173}
     174
    152175
    153176/* Convert a domain and name to SID */
     
    194217}
    195218
     219
    196220/* Convert a SID to a domain and name */
    197221wbcErr wbcLookupSid(const struct wbcDomainSid *sid,
     
    203227        struct winbindd_response response;
    204228        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
    205         char *sid_string = NULL;
    206         char *domain = NULL;
    207         char *name = NULL;
    208         enum wbcSidType name_type = WBC_SID_NAME_USE_NONE;
     229        char *domain, *name;
    209230
    210231        if (!sid) {
    211                 wbc_status = WBC_ERR_INVALID_PARAM;
    212                 BAIL_ON_WBC_ERROR(wbc_status);
     232                return WBC_ERR_INVALID_PARAM;
    213233        }
    214234
     
    218238        ZERO_STRUCT(response);
    219239
    220         /* dst is already null terminated from the memset above */
    221 
    222         wbc_status = wbcSidToString(sid, &sid_string);
    223         BAIL_ON_WBC_ERROR(wbc_status);
    224 
    225         strncpy(request.data.sid, sid_string, sizeof(request.data.sid)-1);
    226         wbcFreeMemory(sid_string);
     240        wbcSidToStringBuf(sid, request.data.sid, sizeof(request.data.sid));
    227241
    228242        /* Make request */
    229243
    230         wbc_status = wbcRequestResponse(WINBINDD_LOOKUPSID,
    231                                            &request,
    232                                            &response);
    233         BAIL_ON_WBC_ERROR(wbc_status);
     244        wbc_status = wbcRequestResponse(WINBINDD_LOOKUPSID, &request,
     245                                        &response);
     246        if (!WBC_ERROR_IS_OK(wbc_status)) {
     247                return wbc_status;
     248        }
    234249
    235250        /* Copy out result */
    236251
    237         domain = talloc_strdup(NULL, response.data.name.dom_name);
    238         BAIL_ON_PTR_ERROR(domain, wbc_status);
    239 
    240         name = talloc_strdup(NULL, response.data.name.name);
    241         BAIL_ON_PTR_ERROR(name, wbc_status);
    242 
    243         name_type = (enum wbcSidType)response.data.name.type;
    244 
     252        wbc_status = WBC_ERR_NO_MEMORY;
     253        domain = NULL;
     254        name = NULL;
     255
     256        domain = wbcStrDup(response.data.name.dom_name);
     257        if (domain == NULL) {
     258                goto done;
     259        }
     260        name = wbcStrDup(response.data.name.name);
     261        if (name == NULL) {
     262                goto done;
     263        }
     264        if (pdomain != NULL) {
     265                *pdomain = domain;
     266                domain = NULL;
     267        }
     268        if (pname != NULL) {
     269                *pname = name;
     270                name = NULL;
     271        }
     272        if (pname_type != NULL) {
     273                *pname_type = (enum wbcSidType)response.data.name.type;
     274        }
    245275        wbc_status = WBC_ERR_SUCCESS;
    246 
    247  done:
    248         if (WBC_ERROR_IS_OK(wbc_status)) {
    249                 if (pdomain != NULL) {
    250                         *pdomain = domain;
    251                 } else {
    252                         TALLOC_FREE(domain);
    253                 }
    254                 if (pname != NULL) {
    255                         *pname = name;
    256                 } else {
    257                         TALLOC_FREE(name);
    258                 }
    259                 if (pname_type != NULL) {
    260                         *pname_type = name_type;
    261                 }
    262         }
    263         else {
    264 #if 0
    265                 /*
    266                  * Found by Coverity: In this particular routine we can't end
    267                  * up here with a non-NULL name. Further up there are just two
    268                  * exit paths that lead here, neither of which leave an
    269                  * allocated name. If you add more paths up there, re-activate
    270                  * this.
    271                  */
    272                 if (name != NULL) {
    273                         talloc_free(name);
    274                 }
    275 #endif
    276                 if (domain != NULL) {
    277                         talloc_free(domain);
    278                 }
    279         }
    280 
     276done:
     277        wbcFreeMemory(name);
     278        wbcFreeMemory(domain);
     279        return wbc_status;
     280}
     281
     282static void wbcDomainInfosDestructor(void *ptr)
     283{
     284        struct wbcDomainInfo *i = (struct wbcDomainInfo *)ptr;
     285
     286        while (i->short_name != NULL) {
     287                wbcFreeMemory(i->short_name);
     288                wbcFreeMemory(i->dns_name);
     289                i += 1;
     290        }
     291}
     292
     293static void wbcTranslatedNamesDestructor(void *ptr)
     294{
     295        struct wbcTranslatedName *n = (struct wbcTranslatedName *)ptr;
     296
     297        while (n->name != NULL) {
     298                free(n->name);
     299                n += 1;
     300        }
     301}
     302
     303wbcErr wbcLookupSids(const struct wbcDomainSid *sids, int num_sids,
     304                     struct wbcDomainInfo **pdomains, int *pnum_domains,
     305                     struct wbcTranslatedName **pnames)
     306{
     307        struct winbindd_request request;
     308        struct winbindd_response response;
     309        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
     310        int buflen, i, extra_len, num_domains, num_names;
     311        char *sidlist, *p, *q, *extra_data;
     312        struct wbcDomainInfo *domains = NULL;
     313        struct wbcTranslatedName *names = NULL;
     314
     315        buflen = num_sids * (WBC_SID_STRING_BUFLEN + 1) + 1;
     316
     317        sidlist = (char *)malloc(buflen);
     318        if (sidlist == NULL) {
     319                return WBC_ERR_NO_MEMORY;
     320        }
     321
     322        p = sidlist;
     323
     324        for (i=0; i<num_sids; i++) {
     325                int remaining;
     326                int len;
     327
     328                remaining = buflen - (p - sidlist);
     329
     330                len = wbcSidToStringBuf(&sids[i], p, remaining);
     331                if (len > remaining) {
     332                        free(sidlist);
     333                        return WBC_ERR_UNKNOWN_FAILURE;
     334                }
     335
     336                p += len;
     337                *p++ = '\n';
     338        }
     339        *p++ = '\0';
     340
     341        ZERO_STRUCT(request);
     342        ZERO_STRUCT(response);
     343
     344        request.extra_data.data = sidlist;
     345        request.extra_len = p - sidlist;
     346
     347        wbc_status = wbcRequestResponse(WINBINDD_LOOKUPSIDS,
     348                                        &request, &response);
     349        free(sidlist);
     350        if (!WBC_ERROR_IS_OK(wbc_status)) {
     351                return wbc_status;
     352        }
     353
     354        extra_len = response.length - sizeof(struct winbindd_response);
     355        extra_data = (char *)response.extra_data.data;
     356
     357        if ((extra_len <= 0) || (extra_data[extra_len-1] != '\0')) {
     358                goto wbc_err_invalid;
     359        }
     360
     361        p = extra_data;
     362
     363        num_domains = strtoul(p, &q, 10);
     364        if (*q != '\n') {
     365                goto wbc_err_invalid;
     366        }
     367        p = q+1;
     368
     369        domains = (struct wbcDomainInfo *)wbcAllocateMemory(
     370                num_domains+1, sizeof(struct wbcDomainInfo),
     371                wbcDomainInfosDestructor);
     372        if (domains == NULL) {
     373                wbc_status = WBC_ERR_NO_MEMORY;
     374                goto fail;
     375        }
     376
     377        for (i=0; i<num_domains; i++) {
     378
     379                q = strchr(p, ' ');
     380                if (q == NULL) {
     381                        goto wbc_err_invalid;
     382                }
     383                *q = '\0';
     384                wbc_status = wbcStringToSid(p, &domains[i].sid);
     385                if (!WBC_ERROR_IS_OK(wbc_status)) {
     386                        goto fail;
     387                }
     388                p = q+1;
     389
     390                q = strchr(p, '\n');
     391                if (q == NULL) {
     392                        goto wbc_err_invalid;
     393                }
     394                *q = '\0';
     395                domains[i].short_name = wbcStrDup(p);
     396                if (domains[i].short_name == NULL) {
     397                        wbc_status = WBC_ERR_NO_MEMORY;
     398                        goto fail;
     399                }
     400                p = q+1;
     401        }
     402
     403        num_names = strtoul(p, &q, 10);
     404        if (*q != '\n') {
     405                goto wbc_err_invalid;
     406        }
     407        p = q+1;
     408
     409        if (num_names != num_sids) {
     410                goto wbc_err_invalid;
     411        }
     412
     413        names = (struct wbcTranslatedName *)wbcAllocateMemory(
     414                num_names+1, sizeof(struct wbcTranslatedName),
     415                wbcTranslatedNamesDestructor);
     416        if (names == NULL) {
     417                wbc_status = WBC_ERR_NO_MEMORY;
     418                goto fail;
     419        }
     420
     421        for (i=0; i<num_names; i++) {
     422
     423                names[i].domain_index = strtoul(p, &q, 10);
     424                if (*q != ' ') {
     425                        goto wbc_err_invalid;
     426                }
     427                p = q+1;
     428
     429                names[i].type = strtoul(p, &q, 10);
     430                if (*q != ' ') {
     431                        goto wbc_err_invalid;
     432                }
     433                p = q+1;
     434
     435                q = strchr(p, '\n');
     436                if (q == NULL) {
     437                        goto wbc_err_invalid;
     438                }
     439                *q = '\0';
     440                names[i].name = wbcStrDup(p);
     441                if (names[i].name == NULL) {
     442                        wbc_status = WBC_ERR_NO_MEMORY;
     443                        goto fail;
     444                }
     445                p = q+1;
     446        }
     447        if (*p != '\0') {
     448                goto wbc_err_invalid;
     449        }
     450
     451        *pdomains = domains;
     452        *pnames = names;
     453        winbindd_free_response(&response);
     454        return WBC_ERR_SUCCESS;
     455
     456wbc_err_invalid:
     457        wbc_status = WBC_ERR_INVALID_RESPONSE;
     458fail:
     459        winbindd_free_response(&response);
     460        wbcFreeMemory(domains);
     461        wbcFreeMemory(names);
    281462        return wbc_status;
    282463}
     
    296477        struct winbindd_request request;
    297478        struct winbindd_response response;
    298         char *sid_string = NULL;
    299479        char *domain_name = NULL;
    300480        const char **names = NULL;
     
    312492        }
    313493
    314         wbc_status = wbcSidToString(dom_sid, &sid_string);
    315         BAIL_ON_WBC_ERROR(wbc_status);
    316 
    317         strncpy(request.data.sid, sid_string, sizeof(request.data.sid)-1);
    318         wbcFreeMemory(sid_string);
     494        wbcSidToStringBuf(dom_sid, request.data.sid, sizeof(request.data.sid));
    319495
    320496        /* Even if all the Rids were of maximum 32bit values,
     
    325501        ridbuf_size = (sizeof(char)*11) * num_rids + 1;
    326502
    327         ridlist = talloc_zero_array(NULL, char, ridbuf_size);
     503        ridlist = (char *)malloc(ridbuf_size);
    328504        BAIL_ON_PTR_ERROR(ridlist, wbc_status);
    329505
    330506        len = 0;
    331         for (i=0; i<num_rids && (len-1)>0; i++) {
    332                 char ridstr[12];
    333 
    334                 len = strlen(ridlist);
    335                 p = ridlist + len;
    336 
    337                 snprintf( ridstr, sizeof(ridstr)-1, "%u\n", rids[i]);
    338                 strncat(p, ridstr, ridbuf_size-len-1);
    339         }
     507        for (i=0; i<num_rids; i++) {
     508                len += snprintf(ridlist + len, ridbuf_size - len, "%u\n",
     509                                rids[i]);
     510        }
     511        ridlist[len] = '\0';
     512        len += 1;
    340513
    341514        request.extra_data.data = ridlist;
    342         request.extra_len = strlen(ridlist)+1;
     515        request.extra_len = len;
    343516
    344517        wbc_status = wbcRequestResponse(WINBINDD_LOOKUPRIDS,
    345518                                        &request,
    346519                                        &response);
    347         talloc_free(ridlist);
     520        free(ridlist);
    348521        BAIL_ON_WBC_ERROR(wbc_status);
    349522
    350         domain_name = talloc_strdup(NULL, response.data.domain_name);
     523        domain_name = wbcStrDup(response.data.domain_name);
    351524        BAIL_ON_PTR_ERROR(domain_name, wbc_status);
    352525
    353         names = talloc_array(NULL, const char*, num_rids);
     526        names = wbcAllocateStringArray(num_rids);
    354527        BAIL_ON_PTR_ERROR(names, wbc_status);
    355528
    356         types = talloc_array(NULL, enum wbcSidType, num_rids);
     529        types = (enum wbcSidType *)wbcAllocateMemory(
     530                num_rids, sizeof(enum wbcSidType), NULL);
    357531        BAIL_ON_PTR_ERROR(types, wbc_status);
    358532
     
    364538                if (*p == '\0') {
    365539                        wbc_status = WBC_ERR_INVALID_RESPONSE;
    366                         BAIL_ON_WBC_ERROR(wbc_status);
     540                        goto done;
    367541                }
    368542
     
    371545                if (*q != ' ') {
    372546                        wbc_status = WBC_ERR_INVALID_RESPONSE;
    373                         BAIL_ON_WBC_ERROR(wbc_status);
     547                        goto done;
    374548                }
    375549
     
    378552                if ((q = strchr(p, '\n')) == NULL) {
    379553                        wbc_status = WBC_ERR_INVALID_RESPONSE;
    380                         BAIL_ON_WBC_ERROR(wbc_status);
     554                        goto done;
    381555                }
    382556
    383557                *q = '\0';
    384558
    385                 names[i] = talloc_strdup(names, p);
     559                names[i] = strdup(p);
    386560                BAIL_ON_PTR_ERROR(names[i], wbc_status);
    387561
     
    391565        if (*p != '\0') {
    392566                wbc_status = WBC_ERR_INVALID_RESPONSE;
    393                 BAIL_ON_WBC_ERROR(wbc_status);
     567                goto done;
    394568        }
    395569
     
    397571
    398572 done:
    399         if (response.extra_data.data) {
    400                 free(response.extra_data.data);
    401         }
     573        winbindd_free_response(&response);
    402574
    403575        if (WBC_ERROR_IS_OK(wbc_status)) {
     
    407579        }
    408580        else {
    409                 if (domain_name)
    410                         talloc_free(domain_name);
    411                 if (names)
    412                         talloc_free(names);
    413                 if (types)
    414                         talloc_free(types);
     581                wbcFreeMemory(domain_name);
     582                wbcFreeMemory(names);
     583                wbcFreeMemory(types);
    415584        }
    416585
     
    428597        struct winbindd_request request;
    429598        struct winbindd_response response;
    430         char *sid_string = NULL;
    431599        struct wbcDomainSid *sids = NULL;
    432600        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
     
    443611        }
    444612
    445         wbc_status = wbcSidToString(user_sid, &sid_string);
    446         BAIL_ON_WBC_ERROR(wbc_status);
    447 
    448         strncpy(request.data.sid, sid_string, sizeof(request.data.sid)-1);
    449         wbcFreeMemory(sid_string);
     613        wbcSidToStringBuf(user_sid, request.data.sid, sizeof(request.data.sid));
    450614
    451615        if (domain_groups_only) {
     
    466630        }
    467631
    468         sids = talloc_array(NULL, struct wbcDomainSid,
    469                             response.data.num_entries);
     632        sids = (struct wbcDomainSid *)wbcAllocateMemory(
     633                response.data.num_entries, sizeof(struct wbcDomainSid),
     634                NULL);
    470635        BAIL_ON_PTR_ERROR(sids, wbc_status);
    471636
     
    487652
    488653 done:
    489         if (response.extra_data.data) {
    490                 free(response.extra_data.data);
    491         }
     654        winbindd_free_response(&response);
    492655        if (sids) {
    493                 talloc_free(sids);
     656                wbcFreeMemory(sids);
    494657        }
    495658
     
    519682        struct winbindd_request request;
    520683        struct winbindd_response response;
    521         char *sid_string = NULL;
    522         ssize_t sid_len;
    523684        ssize_t extra_data_len = 0;
    524685        char * extra_data = NULL;
     
    535696        if (!dom_sid) {
    536697                wbc_status = WBC_ERR_INVALID_PARAM;
    537                 BAIL_ON_WBC_ERROR(wbc_status);
    538         }
    539 
    540         wbc_status = wbcSidToString(dom_sid, &sid_string);
    541         BAIL_ON_WBC_ERROR(wbc_status);
    542 
    543         strncpy(request.data.sid, sid_string, sizeof(request.data.sid)-1);
    544         wbcFreeMemory(sid_string);
    545         sid_string = NULL;
    546 
    547         /* Lets assume each sid is around 54 characters
    548          * S-1-5-AAAAAAAAAAA-BBBBBBBBBBB-CCCCCCCCCCC-DDDDDDDDDDD\n */
    549         buflen = 54 * num_sids;
    550         extra_data = talloc_array(NULL, char, buflen);
     698                goto done;
     699        }
     700
     701        wbcSidToStringBuf(dom_sid, request.data.sid, sizeof(request.data.sid));
     702
     703        /* Lets assume each sid is around 57 characters
     704         * S-1-5-21-AAAAAAAAAAA-BBBBBBBBBBB-CCCCCCCCCCC-DDDDDDDDDDD\n */
     705        buflen = 57 * num_sids;
     706        extra_data = (char *)malloc(buflen);
    551707        if (!extra_data) {
    552708                wbc_status = WBC_ERR_NO_MEMORY;
    553                 BAIL_ON_WBC_ERROR(wbc_status);
     709                goto done;
    554710        }
    555711
    556712        /* Build the sid list */
    557713        for (i=0; i<num_sids; i++) {
    558                 if (sid_string) {
    559                         wbcFreeMemory(sid_string);
    560                         sid_string = NULL;
    561                 }
    562                 wbc_status = wbcSidToString(&sids[i], &sid_string);
    563                 BAIL_ON_WBC_ERROR(wbc_status);
    564 
    565                 sid_len = strlen(sid_string);
     714                char sid_str[WBC_SID_STRING_BUFLEN];
     715                size_t sid_len;
     716
     717                sid_len = wbcSidToStringBuf(&sids[i], sid_str, sizeof(sid_str));
    566718
    567719                if (buflen < extra_data_len + sid_len + 2) {
    568720                        buflen *= 2;
    569                         extra_data = talloc_realloc(NULL, extra_data,
    570                             char, buflen);
     721                        extra_data = (char *)realloc(extra_data, buflen);
    571722                        if (!extra_data) {
    572723                                wbc_status = WBC_ERR_NO_MEMORY;
     
    575726                }
    576727
    577                 strncpy(&extra_data[extra_data_len], sid_string,
     728                strncpy(&extra_data[extra_data_len], sid_str,
    578729                        buflen - extra_data_len);
    579730                extra_data_len += sid_len;
     
    581732                extra_data[extra_data_len] = '\0';
    582733        }
     734        extra_data_len += 1;
    583735
    584736        request.extra_data.data = extra_data;
     
    593745            !response.extra_data.data) {
    594746                wbc_status = WBC_ERR_INVALID_RESPONSE;
    595                 BAIL_ON_WBC_ERROR(wbc_status);
    596         }
    597 
    598         rids = talloc_array(NULL, uint32_t,
    599                             response.data.num_entries);
     747                goto done;
     748        }
     749
     750        rids = (uint32_t *)wbcAllocateMemory(response.data.num_entries,
     751                                             sizeof(uint32_t), NULL);
    600752        BAIL_ON_PTR_ERROR(sids, wbc_status);
    601753
     
    619771
    620772 done:
    621         if (sid_string) {
    622                 wbcFreeMemory(sid_string);
    623         }
    624         if (extra_data) {
    625                 talloc_free(extra_data);
    626         }
    627         if (response.extra_data.data) {
    628                 free(response.extra_data.data);
    629         }
    630         if (rids) {
    631                 talloc_free(rids);
    632         }
    633 
     773        free(extra_data);
     774        winbindd_free_response(&response);
     775        wbcFreeMemory(rids);
    634776        return wbc_status;
    635777}
     
    663805        BAIL_ON_WBC_ERROR(wbc_status);
    664806
     807        users = wbcAllocateStringArray(response.data.num_entries);
     808        if (users == NULL) {
     809                return WBC_ERR_NO_MEMORY;
     810        }
     811
    665812        /* Look through extra data */
    666813
    667814        next = (const char *)response.extra_data.data;
    668815        while (next) {
    669                 const char **tmp;
    670                 const char *current = next;
    671                 char *k = strchr(next, ',');
     816                const char *current;
     817                char *k;
     818
     819                if (num_users >= response.data.num_entries) {
     820                        wbc_status = WBC_ERR_INVALID_RESPONSE;
     821                        goto done;
     822                }
     823
     824                current = next;
     825                k = strchr(next, ',');
     826
    672827                if (k) {
    673828                        k[0] = '\0';
     
    677832                }
    678833
    679                 tmp = talloc_realloc(NULL, users,
    680                                      const char *,
    681                                      num_users+1);
    682                 BAIL_ON_PTR_ERROR(tmp, wbc_status);
    683                 users = tmp;
    684 
    685                 users[num_users] = talloc_strdup(users, current);
     834                users[num_users] = strdup(current);
    686835                BAIL_ON_PTR_ERROR(users[num_users], wbc_status);
    687 
    688                 num_users++;
    689         }
    690 
    691         *_num_users = num_users;
     836                num_users += 1;
     837        }
     838        if (num_users != response.data.num_entries) {
     839                wbc_status = WBC_ERR_INVALID_RESPONSE;
     840                goto done;
     841        }
     842
     843        *_num_users = response.data.num_entries;
    692844        *_users = users;
    693845        users = NULL;
     
    695847
    696848 done:
    697         if (response.extra_data.data) {
    698                 free(response.extra_data.data);
    699         }
    700         if (users) {
    701                 talloc_free(users);
    702         }
     849        winbindd_free_response(&response);
     850        wbcFreeMemory(users);
    703851        return wbc_status;
    704852}
     
    731879        BAIL_ON_WBC_ERROR(wbc_status);
    732880
     881        groups = wbcAllocateStringArray(response.data.num_entries);
     882        if (groups == NULL) {
     883                return WBC_ERR_NO_MEMORY;
     884        }
     885
    733886        /* Look through extra data */
    734887
    735888        next = (const char *)response.extra_data.data;
    736889        while (next) {
    737                 const char **tmp;
    738                 const char *current = next;
    739                 char *k = strchr(next, ',');
     890                const char *current;
     891                char *k;
     892
     893                if (num_groups >= response.data.num_entries) {
     894                        wbc_status = WBC_ERR_INVALID_RESPONSE;
     895                        goto done;
     896                }
     897
     898                current = next;
     899                k = strchr(next, ',');
     900
    740901                if (k) {
    741902                        k[0] = '\0';
     
    745906                }
    746907
    747                 tmp = talloc_realloc(NULL, groups,
    748                                      const char *,
    749                                      num_groups+1);
    750                 BAIL_ON_PTR_ERROR(tmp, wbc_status);
    751                 groups = tmp;
    752 
    753                 groups[num_groups] = talloc_strdup(groups, current);
     908                groups[num_groups] = strdup(current);
    754909                BAIL_ON_PTR_ERROR(groups[num_groups], wbc_status);
    755 
    756                 num_groups++;
    757         }
    758 
    759         *_num_groups = num_groups;
     910                num_groups += 1;
     911        }
     912        if (num_groups != response.data.num_entries) {
     913                wbc_status = WBC_ERR_INVALID_RESPONSE;
     914                goto done;
     915        }
     916
     917        *_num_groups = response.data.num_entries;
    760918        *_groups = groups;
    761919        groups = NULL;
     
    763921
    764922 done:
    765         if (response.extra_data.data) {
    766                 free(response.extra_data.data);
    767         }
    768         if (groups) {
    769                 talloc_free(groups);
    770         }
     923        winbindd_free_response(&response);
     924        wbcFreeMemory(groups);
    771925        return wbc_status;
    772926}
     
    797951                wbcFreeMemory(name);
    798952
    799                 name = talloc_strdup(NULL, pwd->pw_gecos);
     953                name = wbcStrDup(pwd->pw_gecos);
     954                wbcFreeMemory(pwd);
    800955                BAIL_ON_PTR_ERROR(name, wbc_status);
    801956        }
Note: See TracChangeset for help on using the changeset viewer.