#include "tldap.h"
#include "tldap_util.h"
#include "../libcli/security/security.h"
+#include "../lib/util/asn1.h"
bool tldap_entry_values(struct tldap_message *msg, const char *attribute,
- int *num_values, DATA_BLOB **values)
+ DATA_BLOB **values, int *num_values)
{
struct tldap_attribute *attributes;
int i, num_attributes;
- if (!tldap_entry_attributes(msg, &num_attributes, &attributes)) {
+ if (!tldap_entry_attributes(msg, &attributes, &num_attributes)) {
return false;
}
if (attribute == NULL) {
return NULL;
}
- if (!tldap_entry_values(msg, attribute, &num_values, &values)) {
+ if (!tldap_entry_values(msg, attribute, &values, &num_values)) {
return NULL;
}
if (num_values != 1) {
size_t len;
if (!tldap_get_single_valueblob(msg, attribute, &val)) {
- return false;
+ return NULL;
}
if (!convert_string_talloc(mem_ctx, CH_UTF8, CH_UNIX,
val.data, val.length,
- &result, &len, false)) {
+ &result, &len)) {
return NULL;
}
return result;
struct dom_sid *sid)
{
DATA_BLOB val;
+ struct sid_parse_ret ret;
if (!tldap_get_single_valueblob(msg, attribute, &val)) {
return false;
}
- return sid_parse((char *)val.data, val.length, sid);
+ ret = sid_parse(val.data, val.length, sid);
+ return (ret.len != -1);
}
bool tldap_pull_guid(struct tldap_message *msg, const char *attribute,
}
bool tldap_add_mod_blobs(TALLOC_CTX *mem_ctx,
- struct tldap_mod **pmods,
+ struct tldap_mod **pmods, int *pnum_mods,
int mod_op, const char *attrib,
DATA_BLOB *newvals, int num_newvals)
{
return false;
}
- num_mods = talloc_array_length(mods);
+ num_mods = *pnum_mods;
for (i=0; i<num_mods; i++) {
if ((mods[i].mod_op == mod_op)
return false;
}
- if (i == num_mods) {
+ if ((i == num_mods) && (talloc_array_length(mods) < num_mods + 1)) {
mods = talloc_realloc(talloc_tos(), mods, struct tldap_mod,
num_mods+1);
if (mods == NULL) {
}
*pmods = mods;
+ *pnum_mods += 1;
return true;
}
-bool tldap_add_mod_str(TALLOC_CTX *mem_ctx, struct tldap_mod **pmods,
+bool tldap_add_mod_str(TALLOC_CTX *mem_ctx,
+ struct tldap_mod **pmods, int *pnum_mods,
int mod_op, const char *attrib, const char *str)
{
DATA_BLOB utf8;
bool ret;
if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF8, str,
- strlen(str), &utf8.data, &utf8.length,
- false)) {
+ strlen(str), &utf8.data, &utf8.length)) {
return false;
}
- ret = tldap_add_mod_blobs(mem_ctx, pmods, mod_op, attrib, &utf8, 1);
+ ret = tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods, mod_op, attrib,
+ &utf8, 1);
TALLOC_FREE(utf8.data);
return ret;
}
DATA_BLOB oldval = data_blob_null;
if ((existing != NULL)
- && tldap_entry_values(existing, attrib, &num_values, &values)) {
+ && tldap_entry_values(existing, attrib, &values, &num_values)) {
if (num_values > 1) {
/* can't change multivalue attributes atm */
/* Believe it or not, but LDAP will deny a delete and
an add at the same time if the values are the
same... */
- DEBUG(10,("smbldap_make_mod_blob: attribute |%s| not "
+ DEBUG(10,("tldap_make_mod_blob_int: attribute |%s| not "
"changed.\n", attrib));
return true;
}
* Novell NDS. In NDS you have to first remove attribute and
* then you could add new value */
- DEBUG(10, ("smbldap_make_mod_blob: deleting attribute |%s|\n",
+ DEBUG(10, ("tldap_make_mod_blob_int: deleting attribute |%s|\n",
attrib));
- if (!tldap_add_mod_blobs(mem_ctx, pmods, TLDAP_MOD_DELETE,
+ if (!tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods,
+ TLDAP_MOD_DELETE,
attrib, &oldval, 1)) {
return false;
}
the old value, should it exist. */
if (newval.data != NULL) {
- DEBUG(10, ("smbldap_make_mod: adding attribute |%s| value len "
+ DEBUG(10, ("tldap_make_mod_blob_int: adding attribute |%s| value len "
"%d\n", attrib, (int)newval.length));
- if (!tldap_add_mod_blobs(mem_ctx, pmods, TLDAP_MOD_ADD,
+ if (!tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods,
+ TLDAP_MOD_ADD,
attrib, &newval, 1)) {
return false;
}
}
- *pnum_mods = talloc_array_length(*pmods);
return true;
}
bool tldap_make_mod_blob(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
- int *pnum_mods, struct tldap_mod **pmods,
+ struct tldap_mod **pmods, int *pnum_mods,
const char *attrib, DATA_BLOB newval)
{
return tldap_make_mod_blob_int(existing, mem_ctx, pmods, pnum_mods,
int ret;
if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d1->data,
- d1->length, &s1, &s1len, false)) {
+ d1->length, &s1, &s1len)) {
/* can't do much here */
return 0;
}
if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d2->data,
- d2->length, &s2, &s2len, false)) {
+ d2->length, &s2, &s2len)) {
/* can't do much here */
TALLOC_FREE(s1);
return 0;
}
- ret = StrCaseCmp(s1, s2);
+ ret = strcasecmp_m(s1, s2);
TALLOC_FREE(s2);
TALLOC_FREE(s1);
return ret;
}
bool tldap_make_mod_fmt(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
- int *pnum_mods, struct tldap_mod **pmods,
+ struct tldap_mod **pmods, int *pnum_mods,
const char *attrib, const char *fmt, ...)
{
va_list ap;
blob.length = strlen(newval);
if (blob.length != 0) {
- blob.data = CONST_DISCARD(uint8_t *, newval);
+ blob.data = discard_const_p(uint8_t, newval);
}
ret = tldap_make_mod_blob_int(existing, mem_ctx, pmods, pnum_mods,
attrib, blob, compare_utf8_blobs);
return ret;
}
-const char *tldap_errstr(TALLOC_CTX *mem_ctx, struct tldap_context *ld, int rc)
+const char *tldap_errstr(TALLOC_CTX *mem_ctx, struct tldap_context *ld,
+ TLDAPRC rc)
{
const char *ld_error = NULL;
char *res;
- ld_error = tldap_msg_diagnosticmessage(tldap_ctx_lastmsg(ld));
- res = talloc_asprintf(mem_ctx, "LDAP error %d (%s), %s", rc,
- tldap_err2string(rc),
+ if (ld != NULL) {
+ ld_error = tldap_msg_diagnosticmessage(tldap_ctx_lastmsg(ld));
+ }
+ res = talloc_asprintf(mem_ctx, "LDAP error %d (%s), %s",
+ (int)TLDAP_RC_V(rc), tldap_rc2string(rc),
ld_error ? ld_error : "unknown");
return res;
}
-int tldap_search_va(struct tldap_context *ld, const char *base, int scope,
- const char *attrs[], int num_attrs, int attrsonly,
- TALLOC_CTX *mem_ctx, struct tldap_message ***res,
- const char *fmt, va_list ap)
+TLDAPRC tldap_search_va(struct tldap_context *ld, const char *base, int scope,
+ const char *attrs[], int num_attrs, int attrsonly,
+ TALLOC_CTX *mem_ctx, struct tldap_message ***res,
+ const char *fmt, va_list ap)
{
char *filter;
- int ret;
+ TLDAPRC rc;
filter = talloc_vasprintf(talloc_tos(), fmt, ap);
if (filter == NULL) {
return TLDAP_NO_MEMORY;
}
- ret = tldap_search(ld, base, scope, filter,
- attrs, num_attrs, attrsonly,
- NULL /*sctrls*/, 0, NULL /*cctrls*/, 0,
- 0 /*timelimit*/, 0 /*sizelimit*/, 0 /*deref*/,
- mem_ctx, res, NULL);
+ rc = tldap_search(ld, base, scope, filter,
+ attrs, num_attrs, attrsonly,
+ NULL /*sctrls*/, 0, NULL /*cctrls*/, 0,
+ 0 /*timelimit*/, 0 /*sizelimit*/, 0 /*deref*/,
+ mem_ctx, res);
TALLOC_FREE(filter);
- return ret;
+ return rc;
}
-int tldap_search_fmt(struct tldap_context *ld, const char *base, int scope,
- const char *attrs[], int num_attrs, int attrsonly,
- TALLOC_CTX *mem_ctx, struct tldap_message ***res,
- const char *fmt, ...)
+TLDAPRC tldap_search_fmt(struct tldap_context *ld, const char *base, int scope,
+ const char *attrs[], int num_attrs, int attrsonly,
+ TALLOC_CTX *mem_ctx, struct tldap_message ***res,
+ const char *fmt, ...)
{
va_list ap;
- int ret;
+ TLDAPRC rc;
va_start(ap, fmt);
- ret = tldap_search_va(ld, base, scope, attrs, num_attrs, attrsonly,
- mem_ctx, res, fmt, ap);
+ rc = tldap_search_va(ld, base, scope, attrs, num_attrs, attrsonly,
+ mem_ctx, res, fmt, ap);
va_end(ap);
- return ret;
+ return rc;
}
bool tldap_pull_uint64(struct tldap_message *msg, const char *attr,
{
char *str;
uint64_t result;
+ int error = 0;
str = tldap_talloc_single_attribute(msg, attr, talloc_tos());
if (str == NULL) {
DEBUG(10, ("Could not find attribute %s\n", attr));
return false;
}
- result = strtoull(str, NULL, 10);
+
+ result = strtoull_err(str, NULL, 10, &error);
+ if (error != 0) {
+ DBG_DEBUG("Attribute conversion failed (%s)\n",
+ strerror(error));
+ TALLOC_FREE(str);
+ return false;
+ }
+
TALLOC_FREE(str);
*presult = result;
return true;
struct tldap_fetch_rootdse_state *state = tevent_req_data(
req, struct tldap_fetch_rootdse_state);
struct tldap_message *msg;
- int rc;
+ TLDAPRC rc;
rc = tldap_search_recv(subreq, state, &msg);
- if (rc != TLDAP_SUCCESS) {
- TALLOC_FREE(subreq);
- tevent_req_error(req, rc);
+ if (tevent_req_ldap_error(req, rc)) {
return;
}
return;
protocol_error:
- tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
+ tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
return;
}
-int tldap_fetch_rootdse_recv(struct tevent_req *req)
+TLDAPRC tldap_fetch_rootdse_recv(struct tevent_req *req)
{
struct tldap_fetch_rootdse_state *state = tevent_req_data(
req, struct tldap_fetch_rootdse_state);
- int err;
+ TLDAPRC rc;
char *dn;
- if (tevent_req_is_ldap_error(req, &err)) {
- return err;
+ if (tevent_req_is_ldap_error(req, &rc)) {
+ return rc;
}
/* Trigger parsing the dn, just to make sure it's ok */
if (!tldap_entry_dn(state->rootdse, &dn)) {
&state->rootdse)) {
return TLDAP_NO_MEMORY;
}
- return 0;
+ return TLDAP_SUCCESS;
}
-int tldap_fetch_rootdse(struct tldap_context *ld)
+TLDAPRC tldap_fetch_rootdse(struct tldap_context *ld)
{
TALLOC_CTX *frame = talloc_stackframe();
struct tevent_context *ev;
struct tevent_req *req;
- int result;
+ TLDAPRC rc = TLDAP_NO_MEMORY;
- ev = event_context_init(frame);
+ ev = samba_tevent_context_init(frame);
if (ev == NULL) {
- result = TLDAP_NO_MEMORY;
goto fail;
}
-
req = tldap_fetch_rootdse_send(frame, ev, ld);
if (req == NULL) {
- result = TLDAP_NO_MEMORY;
goto fail;
}
-
if (!tevent_req_poll(req, ev)) {
- result = TLDAP_OPERATIONS_ERROR;
+ rc = TLDAP_OPERATIONS_ERROR;
goto fail;
}
- result = tldap_fetch_rootdse_recv(req);
+ rc = tldap_fetch_rootdse_recv(req);
fail:
TALLOC_FREE(frame);
- return result;
+ return rc;
}
struct tldap_message *tldap_rootdse(struct tldap_context *ld)
int i, num_values;
DATA_BLOB *values;
- if (!tldap_entry_values(msg, attribute, &num_values, &values)) {
+ if (!tldap_entry_values(msg, attribute, &values, &num_values)) {
return false;
}
for (i=0; i<num_values; i++) {
struct tldap_search_paged_state *state)
{
struct tldap_control *pgctrl;
- struct asn1_data *asn1;
+ struct asn1_data *asn1 = NULL;
asn1 = asn1_init(state);
if (asn1 == NULL) {
return NULL;
}
- asn1_push_tag(asn1, ASN1_SEQUENCE(0));
- asn1_write_Integer(asn1, state->page_size);
- asn1_write_OctetString(asn1, state->cookie.data, state->cookie.length);
- asn1_pop_tag(asn1);
- if (asn1->has_error) {
- TALLOC_FREE(asn1);
- return NULL;
- }
+ if (!asn1_push_tag(asn1, ASN1_SEQUENCE(0))) goto err;
+ if (!asn1_write_Integer(asn1, state->page_size)) goto err;
+ if (!asn1_write_OctetString(asn1, state->cookie.data, state->cookie.length)) goto err;
+ if (!asn1_pop_tag(asn1)) goto err;
state->asn1 = asn1;
pgctrl = &state->sctrls[state->num_sctrls-1];
pgctrl->oid = TLDAP_CONTROL_PAGEDRESULTS;
pgctrl->critical = true;
if (!asn1_blob(state->asn1, &pgctrl->value)) {
- TALLOC_FREE(asn1);
- return NULL;
+ goto err;
}
return tldap_search_send(mem_ctx, state->ev, state->ld, state->base,
state->scope, state->filter, state->attrs,
state->cctrls, state->num_cctrls,
state->timelimit, state->sizelimit,
state->deref);
+
+ err:
+
+ TALLOC_FREE(asn1);
+ return NULL;
}
static void tldap_search_paged_done(struct tevent_req *subreq);
subreq, struct tevent_req);
struct tldap_search_paged_state *state = tevent_req_data(
req, struct tldap_search_paged_state);
- struct asn1_data *asn1;
+ struct asn1_data *asn1 = NULL;
struct tldap_control *pgctrl;
- int rc, size;
+ TLDAPRC rc;
+ int size;
rc = tldap_search_recv(subreq, state, &state->result);
- if (rc != TLDAP_SUCCESS) {
- TALLOC_FREE(subreq);
- tevent_req_error(req, rc);
+ if (tevent_req_ldap_error(req, rc)) {
return;
}
break;
default:
TALLOC_FREE(subreq);
- tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
+ tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
return;
}
TLDAP_CONTROL_PAGEDRESULTS);
if (pgctrl == NULL) {
/* RFC2696 requires the server to return the control */
- tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
+ tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
return;
}
TALLOC_FREE(state->cookie.data);
asn1 = asn1_init(talloc_tos());
- if (asn1 == NULL) {
- tevent_req_error(req, TLDAP_NO_MEMORY);
+ if (tevent_req_nomem(asn1, req)) {
return;
}
asn1_load_nocopy(asn1, pgctrl->value.data, pgctrl->value.length);
- asn1_start_tag(asn1, ASN1_SEQUENCE(0));
- asn1_read_Integer(asn1, &size);
- asn1_read_OctetString(asn1, state, &state->cookie);
- asn1_end_tag(asn1);
- if (asn1->has_error) {
- tevent_req_error(req, TLDAP_DECODING_ERROR);
- return;
- }
+ if (!asn1_start_tag(asn1, ASN1_SEQUENCE(0))) goto err;
+ if (!asn1_read_Integer(asn1, &size)) goto err;
+ if (!asn1_read_OctetString(asn1, state, &state->cookie)) goto err;
+ if (!asn1_end_tag(asn1)) goto err;
+
TALLOC_FREE(asn1);
if (state->cookie.length == 0) {
return;
}
tevent_req_set_callback(subreq, tldap_search_paged_done, req);
+
+ err:
+
+ TALLOC_FREE(asn1);
+ tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
}
-int tldap_search_paged_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
- struct tldap_message **pmsg)
+TLDAPRC tldap_search_paged_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+ struct tldap_message **pmsg)
{
struct tldap_search_paged_state *state = tevent_req_data(
req, struct tldap_search_paged_state);
- int err;
+ TLDAPRC rc;
if (!tevent_req_is_in_progress(req)
- && tevent_req_is_ldap_error(req, &err)) {
- return err;
+ && tevent_req_is_ldap_error(req, &rc)) {
+ return rc;
}
if (tevent_req_is_in_progress(req)) {
switch (tldap_msg_type(state->result)) {