/*
Unix SMB/CIFS implementation.
Infrastructure for async ldap client requests
Copyright (C) Volker Lendecke 2009
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "tldap.h"
#include "../lib/util/asn1.h"
#include "../lib/tsocket/tsocket.h"
#include "../lib/util/tevent_unix.h"
static int tldap_simple_recv(struct tevent_req *req);
bool tevent_req_is_ldap_error(struct tevent_req *req, int *perr)
{
enum tevent_req_state state;
uint64_t err;
if (!tevent_req_is_error(req, &state, &err)) {
return false;
}
switch (state) {
case TEVENT_REQ_TIMED_OUT:
*perr = TLDAP_TIMEOUT;
break;
case TEVENT_REQ_NO_MEMORY:
*perr = TLDAP_NO_MEMORY;
break;
case TEVENT_REQ_USER_ERROR:
*perr = err;
break;
default:
*perr = TLDAP_OPERATIONS_ERROR;
break;
}
return true;
}
struct tldap_ctx_attribute {
char *name;
void *ptr;
};
struct tldap_context {
int ld_version;
int ld_deref;
int ld_sizelimit;
int ld_timelimit;
struct tstream_context *conn;
bool server_down;
int msgid;
struct tevent_queue *outgoing;
struct tevent_req **pending;
/* For the sync wrappers we need something like get_last_error... */
struct tldap_message *last_msg;
/* debug */
void (*log_fn)(void *context, enum tldap_debug_level level,
const char *fmt, va_list ap);
void *log_private;
struct tldap_ctx_attribute *ctx_attrs;
};
struct tldap_message {
struct asn1_data *data;
uint8_t *inbuf;
int type;
int id;
/* RESULT_ENTRY */
char *dn;
struct tldap_attribute *attribs;
/* Error data sent by the server */
int lderr;
char *res_matcheddn;
char *res_diagnosticmessage;
char *res_referral;
struct tldap_control *res_sctrls;
/* Controls sent by the server */
struct tldap_control *ctrls;
};
void tldap_set_debug(struct tldap_context *ld,
void (*log_fn)(void *log_private,
enum tldap_debug_level level,
const char *fmt,
va_list ap) PRINTF_ATTRIBUTE(3,0),
void *log_private)
{
ld->log_fn = log_fn;
ld->log_private = log_private;
}
static void tldap_debug(struct tldap_context *ld,
enum tldap_debug_level level,
const char *fmt, ...)
{
va_list ap;
if (!ld) {
return;
}
if (ld->log_fn == NULL) {
return;
}
va_start(ap, fmt);
ld->log_fn(ld->log_private, level, fmt, ap);
va_end(ap);
}
static int tldap_next_msgid(struct tldap_context *ld)
{
int result;
result = ld->msgid++;
if (ld->msgid == 2147483647) {
ld->msgid = 1;
}
return result;
}
struct tldap_context *tldap_context_create(TALLOC_CTX *mem_ctx, int fd)
{
struct tldap_context *ctx;
int ret;
ctx = talloc_zero(mem_ctx, struct tldap_context);
if (ctx == NULL) {
return NULL;
}
ret = tstream_bsd_existing_socket(ctx, fd, &ctx->conn);
if (ret == -1) {
TALLOC_FREE(ctx);
return NULL;
}
ctx->msgid = 1;
ctx->ld_version = 3;
ctx->outgoing = tevent_queue_create(ctx, "tldap_outgoing");
if (ctx->outgoing == NULL) {
TALLOC_FREE(ctx);
return NULL;
}
return ctx;
}
bool tldap_connection_ok(struct tldap_context *ld)
{
if (ld == NULL) {
return false;
}
return !ld->server_down;
}
static struct tldap_ctx_attribute *tldap_context_findattr(
struct tldap_context *ld, const char *name)
{
int i, num_attrs;
num_attrs = talloc_array_length(ld->ctx_attrs);
for (i=0; i<num_attrs; i++) {
if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
return &ld->ctx_attrs[i];
}
}
return NULL;
}
bool tldap_context_setattr(struct tldap_context *ld,
const char *name, const void *_pptr)
{
struct tldap_ctx_attribute *tmp, *attr;
char *tmpname;
int num_attrs;
void **pptr = (void **)discard_const_p(void,_pptr);
attr = tldap_context_findattr(ld, name);
if (attr != NULL) {
/*
* We don't actually delete attrs, we don't expect tons of
* attributes being shuffled around.
*/
TALLOC_FREE(attr->ptr);
if (*pptr != NULL) {
attr->ptr = talloc_move(ld->ctx_attrs, pptr);
*pptr = NULL;
}
return true;
}
tmpname = talloc_strdup(ld, name);
if (tmpname == NULL) {
return false;
}
num_attrs = talloc_array_length(ld->ctx_attrs);
tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
num_attrs+1);
if (tmp == NULL) {
TALLOC_FREE(tmpname);
return false;
}
tmp[num_attrs].name = talloc_move(tmp, &tmpname);
if (*pptr != NULL) {
tmp[num_attrs].ptr = talloc_move(tmp, pptr);
} else {
tmp[num_attrs].ptr = NULL;
}
*pptr = NULL;
ld->ctx_attrs = tmp;
return true;
}
void *tldap_context_getattr(struct tldap_context *ld, const char *name)
{
struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
if (attr == NULL) {
return NULL;
}
return attr->ptr;
}
struct read_ldap_state {
uint8_t *buf;
bool done;
};
static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data);
static void read_ldap_done(struct tevent_req *subreq);
static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct tstream_context *conn)
{
struct tevent_req *req, *subreq;
struct read_ldap_state *state;
req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
if (req == NULL) {
return NULL;
}
state->done = false;
subreq = tstream_read_packet_send(state, ev, conn, 2, read_ldap_more,
state);
if (tevent_req_nomem(subreq, req)) {
return tevent_req_post(req, ev);
}
tevent_req_set_callback(subreq, read_ldap_done, req);
return req;
}
static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
{
struct read_ldap_state *state = talloc_get_type_abort(
private_data, struct read_ldap_state);
size_t len;
int i, lensize;
if (state->done) {
/* We've been here, we're done */
return 0;
}
/*
* From ldap.h: LDAP_TAG_MESSAGE is 0x30
*/
if (buf[0] != 0x30) {
return -1;
}
len = buf[1];
if ((len & 0x80) == 0) {
state->done = true;
return len;
}
lensize = (len & 0x7f);
len = 0;
if (buflen == 2) {
/* Please get us the full length */
return lensize;
}
if (buflen > 2 + lensize) {
state->done = true;
return 0;
}
if (buflen != 2 + lensize) {
return -1;
}
for (i=0; i<lensize; i++) {
len = (len << 8) | buf[2+i];
}
return len;
}
static void read_ldap_done(struct tevent_req *subreq)
{
struct tevent_req *req = tevent_req_callback_data(
subreq, struct tevent_req);
struct read_ldap_state *state = tevent_req_data(
req, struct read_ldap_state);
ssize_t nread;
int err;
nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
TALLOC_FREE(subreq);
if (nread == -1) {
tevent_req_error(req, err);
return;
}
tevent_req_done(req);
}
static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
uint8_t **pbuf, int *perrno)
{
struct read_ldap_state *state = tevent_req_data(
req, struct read_ldap_state);
if (tevent_req_is_unix_error(req, perrno)) {
return -1;
}
*pbuf = talloc_move(mem_ctx, &state->buf);
return talloc_get_size(*pbuf);
}
struct tldap_msg_state {
struct tldap_context *ld;
struct tevent_context *ev;
int id;
struct iovec iov;
struct asn1_data *data;
uint8_t *inbuf;
};
static void tldap_push_controls(struct asn1_data *data,
struct tldap_control *sctrls,
int num_sctrls)
{
int i;
if ((sctrls == NULL) || (num_sctrls == 0)) {
return;
}
asn1_push_tag(data, ASN1_CONTEXT(0));
for (i=0; i<num_sctrls; i++) {
struct tldap_control *c = &sctrls[i];
asn1_push_tag(data, ASN1_SEQUENCE(0));
asn1_write_OctetString(data, c->oid, strlen(c->oid));
if (c->critical) {
asn1_write_BOOLEAN(data, true);
}
if (c->value.data != NULL) {
asn1_write_OctetString(data, c->value.data,
c->value.length);
}
asn1_pop_tag(data); /* ASN1_SEQUENCE(0) */
}
asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
}
static void tldap_msg_sent(struct tevent_req *subreq);
static void tldap_msg_received(struct tevent_req *subreq);
static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct tldap_context *ld,
int id, struct asn1_data *data,
struct tldap_control *sctrls,
int num_sctrls)
{
struct tevent_req *req, *subreq;
struct tldap_msg_state *state;
DATA_BLOB blob;
tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
id);
req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
if (req == NULL) {
return NULL;
}
state->ld = ld;
state->ev = ev;
state->id = id;
if (state->ld->server_down) {
tevent_req_error(req, TLDAP_SERVER_DOWN);
return tevent_req_post(req, ev);
}
tldap_push_controls(data, sctrls, num_sctrls);
asn1_pop_tag(data);
if (!asn1_blob(data, &blob)) {
tevent_req_error(req, TLDAP_ENCODING_ERROR);
return tevent_req_post(req, ev);
}
state->iov.iov_base = (void *)blob.data;
state->iov.iov_len = blob.length;
subreq = tstream_writev_queue_send(state, ev, ld->conn, ld->outgoing,
&state->iov, 1);
if (tevent_req_nomem(subreq, req)) {
return tevent_req_post(req, ev);
}
tevent_req_set_callback(subreq, tldap_msg_sent, req);
return req;
}
static void tldap_msg_unset_pending(struct tevent_req *req)
{
struct tldap_msg_state *state = tevent_req_data(
req, struct tldap_msg_state);
struct tldap_context *ld = state->ld;
int num_pending = talloc_array_length(ld->pending);
int i;
if (num_pending == 1) {
TALLOC_FREE(ld->pending);
return;
}
for (i=0; i<num_pending; i++) {
if (req == ld->pending[i]) {
break;
}
}
if (i == num_pending) {
/*
* Something's seriously broken. Just returning here is the
* right thing nevertheless, the point of this routine is to
* remove ourselves from cli->pending.
*/
return;
}
/*
* Remove ourselves from the cli->pending array
*/
if (num_pending > 1) {
ld->pending[i] = ld->pending[num_pending-1];
}
/*
* No NULL check here, we're shrinking by sizeof(void *), and
* talloc_realloc just adjusts the size for this.
*/
ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
num_pending - 1);
return;
}
static int tldap_msg_destructor(struct tevent_req *req)
{
tldap_msg_unset_pending(req);
return 0;
}
static bool tldap_msg_set_pending(struct tevent_req *req)
{
struct tldap_msg_state *state = tevent_req_data(
req, struct tldap_msg_state);
struct tldap_context *ld;
struct tevent_req **pending;
int num_pending;
struct tevent_req *subreq;
ld = state->ld;
num_pending = talloc_array_length(ld->pending);
pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
num_pending+1);
if (pending == NULL) {
return false;
}
pending[num_pending] = req;
ld->pending = pending;
talloc_set_destructor(req, tldap_msg_destructor);
if (num_pending > 0) {
return true;
}
/*
* We're the first one, add the read_ldap request that waits for the
* answer from the server
*/
subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
if (subreq == NULL) {
tldap_msg_unset_pending(req);
return false;
}
tevent_req_set_callback(subreq, tldap_msg_received, ld);
return true;
}
static void tldap_msg_sent(struct tevent_req *subreq)
{
struct tevent_req *req = tevent_req_callback_data(
subreq, struct tevent_req);
struct tldap_msg_state *state = tevent_req_data(
req, struct tldap_msg_state);
ssize_t nwritten;
int err;
nwritten = tstream_writev_queue_recv(subreq, &err);
TALLOC_FREE(subreq);
if (nwritten == -1) {
state->ld->server_down = true;
tevent_req_error(req, TLDAP_SERVER_DOWN);
return;
}
if (!tldap_msg_set_pending(req)) {
tevent_req_oom(req);
return;
}
}
static int tldap_msg_msgid(struct tevent_req *req)
{
struct tldap_msg_state *state = tevent_req_data(
req, struct tldap_msg_state);
return state->id;
}
static void tldap_msg_received(struct tevent_req *subreq)
{
struct tldap_context *ld = tevent_req_callback_data(
subreq, struct tldap_context);
struct tevent_req *req;
struct tldap_msg_state *state;
struct asn1_data *data;
uint8_t *inbuf;
ssize_t received;
size_t num_pending;
int i, err, status;
int id;
uint8_t type;
bool ok;
received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
TALLOC_FREE(subreq);
if (received == -1) {
status = TLDAP_SERVER_DOWN;
goto fail;
}
data = asn1_init(talloc_tos());
if (data == NULL) {
status = TLDAP_NO_MEMORY;
goto fail;
}
asn1_load_nocopy(data, inbuf, received);
ok = true;
ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
ok &= asn1_read_Integer(data, &id);
ok &= asn1_peek_uint8(data, &type);
if (!ok) {
status = TLDAP_PROTOCOL_ERROR;
goto fail;
}
tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
|