summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Hedenfal <martinh@cvs.openbsd.org>2010-06-15 19:30:27 +0000
committerMartin Hedenfal <martinh@cvs.openbsd.org>2010-06-15 19:30:27 +0000
commit70831cf8947e943063239f013640b0cc48f408fc (patch)
tree4b0b67d7576594618b7e8471cacb44a4048fe786
parent0fbf228732204e6c0bd4394a6b9a6fc635793c0f (diff)
Move around some functions to ease re-use from ldapctl.
-rw-r--r--usr.sbin/ldapd/ldapd.h5
-rw-r--r--usr.sbin/ldapd/namespace.c118
-rw-r--r--usr.sbin/ldapd/parse.y23
-rw-r--r--usr.sbin/ldapd/util.c105
4 files changed, 133 insertions, 118 deletions
diff --git a/usr.sbin/ldapd/ldapd.h b/usr.sbin/ldapd/ldapd.h
index e50e4e0eff0..a0b62c7f440 100644
--- a/usr.sbin/ldapd/ldapd.h
+++ b/usr.sbin/ldapd/ldapd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: ldapd.h,v 1.5 2010/06/15 15:47:56 martinh Exp $ */
+/* $OpenBSD: ldapd.h,v 1.6 2010/06/15 19:30:26 martinh Exp $ */
/*
* Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
@@ -594,6 +594,9 @@ int bsnprintf(char *str, size_t size,
int has_suffix(struct btval *key, const char *suffix);
int has_prefix(struct btval *key, const char *prefix);
void normalize_dn(char *dn);
+int ber2db(struct ber_element *root, struct btval *val,
+ int compression_level);
+struct ber_element *db2ber(struct btval *val, int compression_level);
/* index.c */
int index_namespace(struct namespace *ns);
diff --git a/usr.sbin/ldapd/namespace.c b/usr.sbin/ldapd/namespace.c
index cb50b0e5405..4b8c56a61e8 100644
--- a/usr.sbin/ldapd/namespace.c
+++ b/usr.sbin/ldapd/namespace.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: namespace.c,v 1.6 2010/06/15 15:47:56 martinh Exp $ */
+/* $OpenBSD: namespace.c,v 1.7 2010/06/15 19:30:26 martinh Exp $ */
/*
* Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
@@ -38,27 +38,6 @@ static void namespace_queue_replay(int fd, short event, void *arg);
static int namespace_set_fd(struct namespace *ns,
struct btree **bt, int fd, unsigned int flags);
-struct namespace *
-namespace_new(const char *suffix)
-{
- struct namespace *ns;
-
- if ((ns = calloc(1, sizeof(*ns))) == NULL)
- return NULL;
- ns->suffix = strdup(suffix);
- ns->sync = 1;
- if (ns->suffix == NULL) {
- free(ns->suffix);
- free(ns);
- return NULL;
- }
- TAILQ_INIT(&ns->indices);
- TAILQ_INIT(&ns->request_queue);
- SIMPLEQ_INIT(&ns->acl);
-
- return ns;
-}
-
int
namespace_begin_txn(struct namespace *ns, struct btree_txn **data_txn,
struct btree_txn **indx_txn, int rdonly)
@@ -327,104 +306,13 @@ int
namespace_ber2db(struct namespace *ns, struct ber_element *root,
struct btval *val)
{
- int rc;
- ssize_t len;
- uLongf destlen;
- Bytef *dest;
- void *buf;
- struct ber ber;
-
- bzero(val, sizeof(*val));
-
- bzero(&ber, sizeof(ber));
- ber.fd = -1;
- ber_write_elements(&ber, root);
-
- if ((len = ber_get_writebuf(&ber, &buf)) == -1)
- return -1;
-
- if (ns->compression_level > 0) {
- val->size = compressBound(len);
- val->data = malloc(val->size + sizeof(uint32_t));
- if (val->data == NULL) {
- log_warn("malloc(%u)", val->size + sizeof(uint32_t));
- ber_free(&ber);
- return -1;
- }
- dest = (char *)val->data + sizeof(uint32_t);
- destlen = val->size - sizeof(uint32_t);
- if ((rc = compress2(dest, &destlen, buf, len,
- ns->compression_level)) != Z_OK) {
- log_warn("compress returned %i", rc);
- free(val->data);
- ber_free(&ber);
- return -1;
- }
- log_debug("compressed entry from %u -> %u byte",
- len, destlen + sizeof(uint32_t));
-
- *(uint32_t *)val->data = len;
- val->size = destlen + sizeof(uint32_t);
- val->free_data = 1;
- } else {
- val->data = buf;
- val->size = len;
- val->free_data = 1; /* XXX: take over internal br_wbuf */
- ber.br_wbuf = NULL;
- }
-
- ber_free(&ber);
-
- return 0;
+ return ber2db(root, val, ns->compression_level);
}
struct ber_element *
namespace_db2ber(struct namespace *ns, struct btval *val)
{
- int rc;
- uLongf len;
- void *buf;
- Bytef *src;
- uLong srclen;
- struct ber_element *elm;
- struct ber ber;
-
- assert(ns != NULL);
- assert(val != NULL);
-
- bzero(&ber, sizeof(ber));
- ber.fd = -1;
-
- if (ns->compression_level > 0) {
- if (val->size < sizeof(uint32_t))
- return NULL;
-
- len = *(uint32_t *)val->data;
- if ((buf = malloc(len)) == NULL) {
- log_warn("malloc(%u)", len);
- return NULL;
- }
-
- src = (char *)val->data + sizeof(uint32_t);
- srclen = val->size - sizeof(uint32_t);
- rc = uncompress(buf, &len, src, srclen);
- if (rc != Z_OK) {
- log_warnx("dbt_to_ber: uncompress returned %i", rc);
- free(buf);
- return NULL;
- }
-
- log_debug("uncompressed entry from %u -> %u byte",
- val->size, len);
-
- ber_set_readbuf(&ber, buf, len);
- elm = ber_read_elements(&ber, NULL);
- free(buf);
- return elm;
- } else {
- ber_set_readbuf(&ber, val->data, val->size);
- return ber_read_elements(&ber, NULL);
- }
+ return db2ber(val, ns->compression_level);
}
static int
diff --git a/usr.sbin/ldapd/parse.y b/usr.sbin/ldapd/parse.y
index 25e48b67a78..480b4d4be2f 100644
--- a/usr.sbin/ldapd/parse.y
+++ b/usr.sbin/ldapd/parse.y
@@ -1,4 +1,4 @@
-/* $OpenBSD: parse.y,v 1.3 2010/06/02 09:12:13 martinh Exp $ */
+/* $OpenBSD: parse.y,v 1.4 2010/06/15 19:30:26 martinh Exp $ */
/*
* Copyright (c) 2009 Martin Hedenfalk <martin@bzero.se>
@@ -1526,3 +1526,24 @@ mk_aci(int type, int rights, enum scope scope, char *target, char *subject)
return aci;
}
+struct namespace *
+namespace_new(const char *suffix)
+{
+ struct namespace *ns;
+
+ if ((ns = calloc(1, sizeof(*ns))) == NULL)
+ return NULL;
+ ns->suffix = strdup(suffix);
+ ns->sync = 1;
+ if (ns->suffix == NULL) {
+ free(ns->suffix);
+ free(ns);
+ return NULL;
+ }
+ TAILQ_INIT(&ns->indices);
+ TAILQ_INIT(&ns->request_queue);
+ SIMPLEQ_INIT(&ns->acl);
+
+ return ns;
+}
+
diff --git a/usr.sbin/ldapd/util.c b/usr.sbin/ldapd/util.c
index 752ee89463d..34885175a7d 100644
--- a/usr.sbin/ldapd/util.c
+++ b/usr.sbin/ldapd/util.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.c,v 1.1 2010/05/31 17:36:31 martinh Exp $ */
+/* $OpenBSD: util.c,v 1.2 2010/06/15 19:30:26 martinh Exp $ */
/*
* Copyright (c) 2009 Martin Hedenfalk <martin@bzero.se>
@@ -22,6 +22,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
+#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
@@ -96,3 +97,105 @@ has_prefix(struct btval *key, const char *prefix)
return (memcmp(key->data, prefix, pfxlen) == 0);
}
+int
+ber2db(struct ber_element *root, struct btval *val, int compression_level)
+{
+ int rc;
+ ssize_t len;
+ uLongf destlen;
+ Bytef *dest;
+ void *buf;
+ struct ber ber;
+
+ bzero(val, sizeof(*val));
+
+ bzero(&ber, sizeof(ber));
+ ber.fd = -1;
+ ber_write_elements(&ber, root);
+
+ if ((len = ber_get_writebuf(&ber, &buf)) == -1)
+ return -1;
+
+ if (compression_level > 0) {
+ val->size = compressBound(len);
+ val->data = malloc(val->size + sizeof(uint32_t));
+ if (val->data == NULL) {
+ log_warn("malloc(%u)", val->size + sizeof(uint32_t));
+ ber_free(&ber);
+ return -1;
+ }
+ dest = (char *)val->data + sizeof(uint32_t);
+ destlen = val->size - sizeof(uint32_t);
+ if ((rc = compress2(dest, &destlen, buf, len,
+ compression_level)) != Z_OK) {
+ log_warn("compress returned %i", rc);
+ free(val->data);
+ ber_free(&ber);
+ return -1;
+ }
+ log_debug("compressed entry from %u -> %u byte",
+ len, destlen + sizeof(uint32_t));
+
+ *(uint32_t *)val->data = len;
+ val->size = destlen + sizeof(uint32_t);
+ val->free_data = 1;
+ } else {
+ val->data = buf;
+ val->size = len;
+ val->free_data = 1; /* XXX: take over internal br_wbuf */
+ ber.br_wbuf = NULL;
+ }
+
+ ber_free(&ber);
+
+ return 0;
+}
+
+struct ber_element *
+db2ber(struct btval *val, int compression_level)
+{
+ int rc;
+ uLongf len;
+ void *buf;
+ Bytef *src;
+ uLong srclen;
+ struct ber_element *elm;
+ struct ber ber;
+
+ assert(val != NULL);
+
+ bzero(&ber, sizeof(ber));
+ ber.fd = -1;
+
+ if (compression_level > 0) {
+ if (val->size < sizeof(uint32_t))
+ return NULL;
+
+ len = *(uint32_t *)val->data;
+ if ((buf = malloc(len)) == NULL) {
+ log_warn("malloc(%u)", len);
+ return NULL;
+ }
+
+ src = (char *)val->data + sizeof(uint32_t);
+ srclen = val->size - sizeof(uint32_t);
+ rc = uncompress(buf, &len, src, srclen);
+ if (rc != Z_OK) {
+ log_warnx("dbt_to_ber: uncompress returned %i", rc);
+ free(buf);
+ return NULL;
+ }
+
+ log_debug("uncompressed entry from %u -> %u byte",
+ val->size, len);
+
+ ber_set_readbuf(&ber, buf, len);
+ elm = ber_read_elements(&ber, NULL);
+ free(buf);
+ return elm;
+ } else {
+ ber_set_readbuf(&ber, val->data, val->size);
+ return ber_read_elements(&ber, NULL);
+ }
+}
+