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 */
}
+
/* Convert a domain and name to SID */
wbcErr wbcLookupName(const char *domain,
const char *name,
return wbc_status;
}
+
/* Convert a SID to a domain and name */
wbcErr wbcLookupSid(const struct wbcDomainSid *sid,
char **pdomain,
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 */
/* 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;
}
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;
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;
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;
*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;
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);
}
/* 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);
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;
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;
}
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);
}
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);
}
name = talloc_strdup(NULL, pwd->pw_gecos);
BAIL_ON_PTR_ERROR(name, wbc_status);
+ wbcFreeMemory(pwd);
}
wbc_status = WBC_ERR_SUCCESS;