libwbclient: Fix some pointless macro calls
[ira/wip.git] / nsswitch / libwbclient / wbc_sid.c
index 99c9d8e1521fa9cd7c51fcddf5b8a2d4fd8342bf..08d63d9699cf502ebb7641f7b3fde79cb7da04ae 100644 (file)
@@ -4,6 +4,7 @@
    Winbind client API
 
    Copyright (C) Gerald (Jerry) Carter 2007
+   Copyright (C) Volker Lendecke 2010
 
 
    This library is free software; you can redistribute it and/or
 
 #include "replace.h"
 #include "libwbclient.h"
-
+#include "../winbind_client.h"
 
 /* Convert a binary SID to a character string */
 wbcErr wbcSidToString(const struct wbcDomainSid *sid,
                      char **sid_string)
 {
-       wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        uint32_t id_auth;
-       int i;
-       char *tmp = NULL;
+       int i, ofs, maxlen;
+       char *result;
 
        if (!sid) {
-               wbc_status = WBC_ERR_INVALID_SID;
-               BAIL_ON_WBC_ERROR(wbc_status);
+               return WBC_ERR_INVALID_SID;
        }
 
+       maxlen = sid->num_auths * 11 + 25;
+
+       result = (char *)wbcAllocateMemory(maxlen, 1, NULL);
+       if (result == NULL) {
+               return WBC_ERR_NO_MEMORY;
+       }
+
+       /*
+        * BIG NOTE: this function only does SIDS where the identauth is not
+        * >= ^32 in a range of 2^48.
+        */
+
        id_auth = sid->id_auth[5] +
                (sid->id_auth[4] << 8) +
                (sid->id_auth[3] << 16) +
                (sid->id_auth[2] << 24);
 
-       tmp = talloc_asprintf(NULL, "S-%d-%d", sid->sid_rev_num, id_auth);
-       BAIL_ON_PTR_ERROR(tmp, wbc_status);
-
-       for (i=0; i<sid->num_auths; i++) {
-               char *tmp2;
-               tmp2 = talloc_asprintf_append(tmp, "-%u", sid->sub_auths[i]);
-               BAIL_ON_PTR_ERROR(tmp2, wbc_status);
+       ofs = snprintf(result, maxlen, "S-%u-%lu",
+                      (unsigned int)sid->sid_rev_num, (unsigned long)id_auth);
 
-               tmp = tmp2;
+       for (i = 0; i < sid->num_auths; i++) {
+               ofs += snprintf(result + ofs, maxlen - ofs, "-%lu",
+                               (unsigned long)sid->sub_auths[i]);
        }
 
-       *sid_string = tmp;
-       tmp = NULL;
-
-       wbc_status = WBC_ERR_SUCCESS;
-
-done:
-       talloc_free(tmp);
-
-       return wbc_status;
+       *sid_string = result;
+       return WBC_ERR_SUCCESS;
 }
 
 /* Convert a character string to a binary SID */
@@ -150,6 +151,7 @@ done:
 
 }
 
+
 /* Convert a domain and name to SID */
 wbcErr wbcLookupName(const char *domain,
                     const char *name,
@@ -193,6 +195,7 @@ wbcErr wbcLookupName(const char *domain,
        return wbc_status;
 }
 
+
 /* Convert a SID to a domain and name */
 wbcErr wbcLookupSid(const struct wbcDomainSid *sid,
                    char **pdomain,
@@ -203,13 +206,10 @@ wbcErr wbcLookupSid(const struct wbcDomainSid *sid,
        struct winbindd_response response;
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        char *sid_string = NULL;
-       char *domain = NULL;
-       char *name = NULL;
-       enum wbcSidType name_type = WBC_SID_NAME_USE_NONE;
+       char *domain, *name;
 
        if (!sid) {
-               wbc_status = WBC_ERR_INVALID_PARAM;
-               BAIL_ON_WBC_ERROR(wbc_status);
+               return WBC_ERR_INVALID_PARAM;
        }
 
        /* Initialize request */
@@ -220,64 +220,50 @@ wbcErr wbcLookupSid(const struct wbcDomainSid *sid,
        /* dst is already null terminated from the memset above */
 
        wbc_status = wbcSidToString(sid, &sid_string);
-       BAIL_ON_WBC_ERROR(wbc_status);
+       if (!WBC_ERROR_IS_OK(wbc_status)) {
+               return wbc_status;
+       }
 
        strncpy(request.data.sid, sid_string, sizeof(request.data.sid)-1);
        wbcFreeMemory(sid_string);
 
        /* Make request */
 
-       wbc_status = wbcRequestResponse(WINBINDD_LOOKUPSID,
-                                          &request,
-                                          &response);
-       BAIL_ON_WBC_ERROR(wbc_status);
+       wbc_status = wbcRequestResponse(WINBINDD_LOOKUPSID, &request,
+                                       &response);
+       if (!WBC_ERROR_IS_OK(wbc_status)) {
+               return wbc_status;
+       }
 
        /* Copy out result */
 
-       domain = talloc_strdup(NULL, response.data.name.dom_name);
-       BAIL_ON_PTR_ERROR(domain, wbc_status);
-
-       name = talloc_strdup(NULL, response.data.name.name);
-       BAIL_ON_PTR_ERROR(name, wbc_status);
-
-       name_type = (enum wbcSidType)response.data.name.type;
-
-       wbc_status = WBC_ERR_SUCCESS;
+       wbc_status = WBC_ERR_NO_MEMORY;
+       domain = NULL;
+       name = NULL;
 
- done:
-       if (WBC_ERROR_IS_OK(wbc_status)) {
-               if (pdomain != NULL) {
-                       *pdomain = domain;
-               } else {
-                       TALLOC_FREE(domain);
-               }
-               if (pname != NULL) {
-                       *pname = name;
-               } else {
-                       TALLOC_FREE(name);
-               }
-               if (pname_type != NULL) {
-                       *pname_type = name_type;
-               }
+       domain = wbcStrDup(response.data.name.dom_name);
+       if (domain == NULL) {
+               goto done;
        }
-       else {
-#if 0
-               /*
-                * Found by Coverity: In this particular routine we can't end
-                * up here with a non-NULL name. Further up there are just two
-                * exit paths that lead here, neither of which leave an
-                * allocated name. If you add more paths up there, re-activate
-                * this.
-                */
-               if (name != NULL) {
-                       talloc_free(name);
-               }
-#endif
-               if (domain != NULL) {
-                       talloc_free(domain);
-               }
+       name = wbcStrDup(response.data.name.name);
+       if (name == NULL) {
+               goto done;
        }
-
+       if (pdomain != NULL) {
+               *pdomain = domain;
+               domain = NULL;
+       }
+       if (pname != NULL) {
+               *pname = name;
+               name = NULL;
+       }
+       if (pname_type != NULL) {
+               *pname_type = (enum wbcSidType)response.data.name.type;
+       }
+       wbc_status = WBC_ERR_SUCCESS;
+done:
+       wbcFreeMemory(name);
+       wbcFreeMemory(domain);
        return wbc_status;
 }
 
@@ -324,36 +310,34 @@ wbcErr wbcLookupRids(struct wbcDomainSid *dom_sid,
 
        ridbuf_size = (sizeof(char)*11) * num_rids + 1;
 
-       ridlist = talloc_zero_array(NULL, char, ridbuf_size);
+       ridlist = (char *)malloc(ridbuf_size);
        BAIL_ON_PTR_ERROR(ridlist, wbc_status);
 
        len = 0;
-       for (i=0; i<num_rids && (len-1)>0; i++) {
-               char ridstr[12];
-
-               len = strlen(ridlist);
-               p = ridlist + len;
-
-               snprintf( ridstr, sizeof(ridstr)-1, "%u\n", rids[i]);
-               strncat(p, ridstr, ridbuf_size-len-1);
+       for (i=0; i<num_rids; i++) {
+               len += snprintf(ridlist + len, ridbuf_size - len, "%u\n",
+                               rids[i]);
        }
+       ridlist[len] = '\0';
+       len += 1;
 
        request.extra_data.data = ridlist;
-       request.extra_len = strlen(ridlist)+1;
+       request.extra_len = len;
 
        wbc_status = wbcRequestResponse(WINBINDD_LOOKUPRIDS,
                                        &request,
                                        &response);
-       talloc_free(ridlist);
+       free(ridlist);
        BAIL_ON_WBC_ERROR(wbc_status);
 
-       domain_name = talloc_strdup(NULL, response.data.domain_name);
+       domain_name = wbcStrDup(response.data.domain_name);
        BAIL_ON_PTR_ERROR(domain_name, wbc_status);
 
-       names = talloc_array(NULL, const char*, num_rids);
+       names = wbcAllocateStringArray(num_rids);
        BAIL_ON_PTR_ERROR(names, wbc_status);
 
-       types = talloc_array(NULL, enum wbcSidType, num_rids);
+       types = (enum wbcSidType *)wbcAllocateMemory(
+               num_rids, sizeof(enum wbcSidType), NULL);
        BAIL_ON_PTR_ERROR(types, wbc_status);
 
        p = (char *)response.extra_data.data;
@@ -363,26 +347,26 @@ wbcErr wbcLookupRids(struct wbcDomainSid *dom_sid,
 
                if (*p == '\0') {
                        wbc_status = WBC_ERR_INVALID_RESPONSE;
-                       BAIL_ON_WBC_ERROR(wbc_status);
+                       goto done;
                }
 
                types[i] = (enum wbcSidType)strtoul(p, &q, 10);
 
                if (*q != ' ') {
                        wbc_status = WBC_ERR_INVALID_RESPONSE;
-                       BAIL_ON_WBC_ERROR(wbc_status);
+                       goto done;
                }
 
                p = q+1;
 
                if ((q = strchr(p, '\n')) == NULL) {
                        wbc_status = WBC_ERR_INVALID_RESPONSE;
-                       BAIL_ON_WBC_ERROR(wbc_status);
+                       goto done;
                }
 
                *q = '\0';
 
-               names[i] = talloc_strdup(names, p);
+               names[i] = strdup(p);
                BAIL_ON_PTR_ERROR(names[i], wbc_status);
 
                p = q+1;
@@ -390,15 +374,13 @@ wbcErr wbcLookupRids(struct wbcDomainSid *dom_sid,
 
        if (*p != '\0') {
                wbc_status = WBC_ERR_INVALID_RESPONSE;
-               BAIL_ON_WBC_ERROR(wbc_status);
+               goto done;
        }
 
        wbc_status = WBC_ERR_SUCCESS;
 
  done:
-       if (response.extra_data.data) {
-               free(response.extra_data.data);
-       }
+       winbindd_free_response(&response);
 
        if (WBC_ERROR_IS_OK(wbc_status)) {
                *pp_domain_name = domain_name;
@@ -406,12 +388,9 @@ wbcErr wbcLookupRids(struct wbcDomainSid *dom_sid,
                *ptypes = types;
        }
        else {
-               if (domain_name)
-                       talloc_free(domain_name);
-               if (names)
-                       talloc_free(names);
-               if (types)
-                       talloc_free(types);
+               wbcFreeMemory(domain_name);
+               wbcFreeMemory(names);
+               wbcFreeMemory(types);
        }
 
        return wbc_status;
@@ -486,9 +465,7 @@ wbcErr wbcLookupUserSids(const struct wbcDomainSid *user_sid,
        wbc_status = WBC_ERR_SUCCESS;
 
  done:
-       if (response.extra_data.data) {
-               free(response.extra_data.data);
-       }
+       winbindd_free_response(&response);
        if (sids) {
                talloc_free(sids);
        }
@@ -555,10 +532,8 @@ wbcErr wbcGetSidAliases(const struct wbcDomainSid *dom_sid,
 
        /* Build the sid list */
        for (i=0; i<num_sids; i++) {
-               if (sid_string) {
-                       wbcFreeMemory(sid_string);
-                       sid_string = NULL;
-               }
+               wbcFreeMemory(sid_string);
+               sid_string = NULL;
                wbc_status = wbcSidToString(&sids[i], &sid_string);
                BAIL_ON_WBC_ERROR(wbc_status);
 
@@ -580,6 +555,7 @@ wbcErr wbcGetSidAliases(const struct wbcDomainSid *dom_sid,
                extra_data[extra_data_len++] = '\n';
                extra_data[extra_data_len] = '\0';
        }
+       extra_data_len += 1;
 
        request.extra_data.data = extra_data;
        request.extra_len = extra_data_len;
@@ -618,19 +594,10 @@ wbcErr wbcGetSidAliases(const struct wbcDomainSid *dom_sid,
        wbc_status = WBC_ERR_SUCCESS;
 
  done:
-       if (sid_string) {
-               wbcFreeMemory(sid_string);
-       }
-       if (extra_data) {
-               talloc_free(extra_data);
-       }
-       if (response.extra_data.data) {
-               free(response.extra_data.data);
-       }
-       if (rids) {
-               talloc_free(rids);
-       }
-
+       wbcFreeMemory(sid_string);
+       talloc_free(extra_data);
+       winbindd_free_response(&response);
+       talloc_free(rids);
        return wbc_status;
 }
 
@@ -694,9 +661,7 @@ wbcErr wbcListUsers(const char *domain_name,
        wbc_status = WBC_ERR_SUCCESS;
 
  done:
-       if (response.extra_data.data) {
-               free(response.extra_data.data);
-       }
+       winbindd_free_response(&response);
        if (users) {
                talloc_free(users);
        }
@@ -762,9 +727,7 @@ wbcErr wbcListGroups(const char *domain_name,
        wbc_status = WBC_ERR_SUCCESS;
 
  done:
-       if (response.extra_data.data) {
-               free(response.extra_data.data);
-       }
+       winbindd_free_response(&response);
        if (groups) {
                talloc_free(groups);
        }
@@ -798,6 +761,7 @@ wbcErr wbcGetDisplayName(const struct wbcDomainSid *sid,
 
                name = talloc_strdup(NULL, pwd->pw_gecos);
                BAIL_ON_PTR_ERROR(name, wbc_status);
+               wbcFreeMemory(pwd);
        }
 
        wbc_status = WBC_ERR_SUCCESS;