diff options
author | Andrew Tridgell <tridge@samba.org> | 2004-03-31 06:51:44 +0000 |
---|---|---|
committer | Andrew Tridgell <tridge@samba.org> | 2004-03-31 06:51:44 +0000 |
commit | 4258c7f27ff628b93e296fd0fc0f7a5a5bf2efeb (patch) | |
tree | 31b8cc73bc52284bcf958d69200fd5e051d4dcdc /source4/lib/ldb/ldb_tdb | |
parent | 58d50a614f1b4a3fc6b60ad5f777d987263fe54f (diff) | |
download | samba-4258c7f27ff628b93e296fd0fc0f7a5a5bf2efeb.tar.gz samba-4258c7f27ff628b93e296fd0fc0f7a5a5bf2efeb.tar.bz2 samba-4258c7f27ff628b93e296fd0fc0f7a5a5bf2efeb.zip |
building with Makefile.ldb now works
(This used to be commit 12538cd2c650bacabd37f4d2ecd3ff3ffce87a00)
Diffstat (limited to 'source4/lib/ldb/ldb_tdb')
-rw-r--r-- | source4/lib/ldb/ldb_tdb/ldb_ldif.c | 366 | ||||
-rw-r--r-- | source4/lib/ldb/ldb_tdb/ldb_parse.c | 448 | ||||
-rw-r--r-- | source4/lib/ldb/ldb_tdb/ldb_parse.h | 40 |
3 files changed, 0 insertions, 854 deletions
diff --git a/source4/lib/ldb/ldb_tdb/ldb_ldif.c b/source4/lib/ldb/ldb_tdb/ldb_ldif.c deleted file mode 100644 index 170685cc03..0000000000 --- a/source4/lib/ldb/ldb_tdb/ldb_ldif.c +++ /dev/null @@ -1,366 +0,0 @@ - /* - Unix SMB/CIFS implementation. - - ldif utilities for ldb - - Copyright (C) Andrew Tridgell 2004 - - 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 2 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, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -#include "includes.h" - - -/* - this base64 decoder was taken from jitterbug (written by tridge). - we might need to replace it with a new version -*/ -static int base64_decode(char *s) -{ - const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - int bit_offset, byte_offset, idx, i, n; - unsigned char *d = (unsigned char *)s; - char *p; - - n=i=0; - - while (*s && (p=strchr(b64,*s))) { - idx = (int)(p - b64); - byte_offset = (i*6)/8; - bit_offset = (i*6)%8; - d[byte_offset] &= ~((1<<(8-bit_offset))-1); - if (bit_offset < 3) { - d[byte_offset] |= (idx << (2-bit_offset)); - n = byte_offset+1; - } else { - d[byte_offset] |= (idx >> (bit_offset-2)); - d[byte_offset+1] = 0; - d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF; - n = byte_offset+2; - } - s++; i++; - } - - if (*s && !p) { - /* the only termination allowed */ - if (*s != '=') { - return -1; - } - } - - /* null terminate */ - d[n] = 0; - return n; -} - - -/* - encode as base64 - caller frees -*/ -char *ldb_base64_encode(const char *buf, int len) -{ - const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - int bit_offset, byte_offset, idx, i; - unsigned char *d = (unsigned char *)buf; - int bytes = (len*8 + 5)/6; - char *out; - - out = malloc(bytes+2); - if (!out) return NULL; - - for (i=0;i<bytes;i++) { - byte_offset = (i*6)/8; - bit_offset = (i*6)%8; - if (bit_offset < 3) { - idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F; - } else { - idx = (d[byte_offset] << (bit_offset-2)) & 0x3F; - if (byte_offset+1 < len) { - idx |= (d[byte_offset+1] >> (8-(bit_offset-2))); - } - } - out[i] = b64[idx]; - } - - out[i++] = '='; - out[i] = 0; - - return out; -} - -/* - see if a buffer should be base64 encoded -*/ -int ldb_should_b64_encode(const struct ldb_val *val) -{ - int i; - unsigned char *p = val->data; - - if (val->length == 0 || p[0] == ' ' || p[0] == ':') { - return 1; - } - - for (i=0; i<val->length; i++) { - if (!isprint(p[i]) || p[i] == '\n') { - return 1; - } - } - return 0; -} - - -/* - encode as base64 to a file -*/ -static int base64_encode_f(FILE *f, const char *buf, int len, int start_pos) -{ - int i; - char *b = ldb_base64_encode(buf, len); - - if (!b) { - return -1; - } - - for (i=0;b[i];i++) { - fputc(b[i], f); - if (b[i+1] && (i + start_pos) % 77 == 0) { - fputc('\n', f); - fputc(' ', f); - } - } - free(b); - return 0; -} - -/* - write a line folded string onto a file -*/ -static void fold_string(FILE *f, const char *buf, size_t length, int start_pos) -{ - int i; - - for (i=0;i<length;i++) { - fputc(buf[i], f); - if (i != (length-1) && (i + start_pos) % 77 == 0) { - fputc('\n', f); - fputc(' ', f); - } - } -} - - -/* - pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF - this routine removes any RFC2849 continuations and comments - - caller frees -*/ -static char *next_chunk(FILE *f) -{ - size_t alloc_size=0, chunk_size = 0; - char *chunk = NULL; - int c; - int in_comment = 0; - - while ((c = fgetc(f)) != EOF) { - if (chunk_size == alloc_size) { - char *c2; - alloc_size += 1024; - c2 = realloc_p(chunk, char, alloc_size); - if (!c2) { - free(chunk); - errno = ENOMEM; - return NULL; - } - chunk = c2; - } - - if (in_comment) { - if (c == '\n') { - in_comment = 0; - } - continue; - } - - /* handle continuation lines - see RFC2849 */ - if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') { - chunk_size--; - continue; - } - - /* chunks are terminated by a double line-feed */ - if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') { - chunk[chunk_size-1] = 0; - return chunk; - } - - if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) { - in_comment = 1; - continue; - } - - chunk[chunk_size++] = c; - } - - return chunk; -} - - -/* simple ldif attribute parser */ -static int next_attr(char **s, char **attr, struct ldb_val *value) -{ - char *p; - int base64_encoded = 0; - - p = strchr(*s, ':'); - if (!p) { - return -1; - } - - *p++ = 0; - - if (*p == ':') { - base64_encoded = 1; - p++; - } - - *attr = *s; - - while (isspace(*p)) { - p++; - } - - value->data = p; - - p = strchr(p, '\n'); - - if (!p) { - value->length = strlen((char *)value->data); - *s = ((char *)value->data) + value->length; - } else { - value->length = p - (char *)value->data; - *s = p+1; - *p = 0; - } - - if (base64_encoded) { - int len = base64_decode(value->data); - if (len == -1) { - /* it wasn't valid base64 data */ - return -1; - } - value->length = len; - } - - return 0; -} - - -/* - free a message from a ldif_read -*/ -void ldif_read_free(struct ldb_message *msg) -{ - if (msg->elements) free(msg->elements); - if (msg->private) free(msg->private); - free(msg); -} - -/* - read from a LDIF file, creating a ldb_message -*/ -struct ldb_message *ldif_read(FILE *f) -{ - struct ldb_message *msg; - char *attr=NULL, *chunk=NULL, *s; - struct ldb_val value; - - value.data = NULL; - - msg = malloc_p(struct ldb_message); - if (!msg) return NULL; - - msg->dn = NULL; - msg->elements = NULL; - msg->num_elements = 0; - msg->private = NULL; - - chunk = next_chunk(f); - if (!chunk) { - goto failed; - } - - msg->private = chunk; - s = chunk; - - if (next_attr(&s, &attr, &value) != 0) { - goto failed; - } - - /* first line must be a dn */ - if (strcmp(attr, "dn") != 0) { - fprintf(stderr, "First line must be a dn not '%s'\n", attr); - goto failed; - } - - msg->dn = value.data; - - while (next_attr(&s, &attr, &value) == 0) { - msg->elements = realloc_p(msg->elements, - struct ldb_message_element, - msg->num_elements+1); - if (!msg->elements) { - goto failed; - } - msg->elements[msg->num_elements].flags = 0; - msg->elements[msg->num_elements].name = attr; - msg->elements[msg->num_elements].value = value; - msg->num_elements++; - } - - return msg; - -failed: - if (msg) ldif_read_free(msg); - return NULL; -} - - -/* - write to a ldif file -*/ -void ldif_write(FILE *f, const struct ldb_message *msg) -{ - int i; - fprintf(f, "dn: %s\n", msg->dn); - for (i=0;i<msg->num_elements;i++) { - if (ldb_should_b64_encode(&msg->elements[i].value)) { - fprintf(f, "%s:: ", msg->elements[i].name); - base64_encode_f(f, - msg->elements[i].value.data, - msg->elements[i].value.length, - strlen(msg->elements[i].name)+3); - fprintf(f, "\n"); - } else { - fprintf(f, "%s: ", msg->elements[i].name); - fold_string(f, msg->elements[i].value.data, - msg->elements[i].value.length, - strlen(msg->elements[i].name)+2); - fprintf(f, "\n"); - } - } - fprintf(f,"\n"); -} diff --git a/source4/lib/ldb/ldb_tdb/ldb_parse.c b/source4/lib/ldb/ldb_tdb/ldb_parse.c deleted file mode 100644 index 44cfc5b4ff..0000000000 --- a/source4/lib/ldb/ldb_tdb/ldb_parse.c +++ /dev/null @@ -1,448 +0,0 @@ - /* - Unix SMB/CIFS implementation. - - parse a LDAP-like expression - - Copyright (C) Andrew Tridgell 2004 - - 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 2 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, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -/* - TODO: - - add RFC2254 binary string handling - - possibly add ~=, <= and >= handling - - expand the test suite - - add better parse error handling - -*/ - -#include "includes.h" - - -/* -a filter is defined by: - <filter> ::= '(' <filtercomp> ')' - <filtercomp> ::= <and> | <or> | <not> | <simple> - <and> ::= '&' <filterlist> - <or> ::= '|' <filterlist> - <not> ::= '!' <filter> - <filterlist> ::= <filter> | <filter> <filterlist> - <simple> ::= <attributetype> <filtertype> <attributevalue> - <filtertype> ::= '=' | '~=' | '<=' | '>=' -*/ - -/* - return next token element. Caller frees -*/ -static char *ldb_parse_lex(const char **s) -{ - const char *p = *s; - char *ret; - - while (isspace(*p)) { - p++; - } - *s = p; - - if (*p == 0) { - return NULL; - } - - if (strchr("()&|=!", *p)) { - (*s) = p+1; - ret = strndup(p, 1); - if (!ret) { - errno = ENOMEM; - } - return ret; - } - - while (*p && (isalnum(*p) || !strchr("()&|=!", *p))) { - p++; - } - - if (p == *s) { - return NULL; - } - - ret = strndup(*s, p - *s); - if (!ret) { - errno = ENOMEM; - } - - *s = p; - - return ret; -} - -/* - find a matching close brace in a string -*/ -static const char *match_brace(const char *s) -{ - unsigned int count = 0; - while (*s && (count != 0 || *s != ')')) { - if (*s == '(') { - count++; - } - if (*s == ')') { - count--; - } - s++; - } - if (! *s) { - return NULL; - } - return s; -} - - -static struct ldb_parse_tree *ldb_parse_filter(const char **s); - -/* - <simple> ::= <attributetype> <filtertype> <attributevalue> -*/ -static struct ldb_parse_tree *ldb_parse_simple(const char *s) -{ - char *eq, *val, *l; - struct ldb_parse_tree *ret; - - l = ldb_parse_lex(&s); - if (!l) { - fprintf(stderr, "Unexpected end of expression\n"); - return NULL; - } - - if (strchr("()&|=", *l)) { - fprintf(stderr, "Unexpected token '%s'\n", l); - free(l); - return NULL; - } - - eq = ldb_parse_lex(&s); - if (!eq || strcmp(eq, "=") != 0) { - fprintf(stderr, "Expected '='\n"); - free(l); - if (eq) free(eq); - return NULL; - } - free(eq); - - val = ldb_parse_lex(&s); - if (val && strchr("()&|=", *val)) { - fprintf(stderr, "Unexpected token '%s'\n", val); - free(l); - if (val) free(val); - return NULL; - } - - ret = malloc_p(struct ldb_parse_tree); - if (!ret) { - errno = ENOMEM; - return NULL; - } - - ret->operation = LDB_OP_SIMPLE; - ret->u.simple.attr = l; - ret->u.simple.value.data = val; - ret->u.simple.value.length = val?strlen(val):0; - - return ret; -} - - -/* - parse a filterlist - <and> ::= '&' <filterlist> - <or> ::= '|' <filterlist> - <filterlist> ::= <filter> | <filter> <filterlist> -*/ -static struct ldb_parse_tree *ldb_parse_filterlist(enum ldb_parse_op op, const char *s) -{ - struct ldb_parse_tree *ret, *next; - - ret = malloc_p(struct ldb_parse_tree); - if (!ret) { - errno = ENOMEM; - return NULL; - } - - ret->operation = op; - ret->u.list.num_elements = 1; - ret->u.list.elements = malloc_p(struct ldb_parse_tree *); - if (!ret->u.list.elements) { - errno = ENOMEM; - free(ret); - return NULL; - } - - ret->u.list.elements[0] = ldb_parse_filter(&s); - if (!ret->u.list.elements[0]) { - free(ret->u.list.elements); - free(ret); - return NULL; - } - - while (isspace(*s)) s++; - - while (*s && (next = ldb_parse_filter(&s))) { - struct ldb_parse_tree **e; - e = realloc_p(ret->u.list.elements, - struct ldb_parse_tree *, - ret->u.list.num_elements+1); - if (!e) { - errno = ENOMEM; - ldb_parse_tree_free(next); - ldb_parse_tree_free(ret); - return NULL; - } - ret->u.list.elements = e; - ret->u.list.elements[ret->u.list.num_elements] = next; - ret->u.list.num_elements++; - while (isspace(*s)) s++; - } - - return ret; -} - - -/* - <not> ::= '!' <filter> -*/ -static struct ldb_parse_tree *ldb_parse_not(const char *s) -{ - struct ldb_parse_tree *ret; - - ret = malloc_p(struct ldb_parse_tree); - if (!ret) { - errno = ENOMEM; - return NULL; - } - - ret->operation = LDB_OP_NOT; - ret->u.not.child = ldb_parse_filter(&s); - if (!ret->u.not.child) { - free(ret); - return NULL; - } - - return ret; -} - -/* - parse a filtercomp - <filtercomp> ::= <and> | <or> | <not> | <simple> -*/ -static struct ldb_parse_tree *ldb_parse_filtercomp(const char *s) -{ - while (isspace(*s)) s++; - - switch (*s) { - case '&': - return ldb_parse_filterlist(LDB_OP_AND, s+1); - - case '|': - return ldb_parse_filterlist(LDB_OP_OR, s+1); - - case '!': - return ldb_parse_not(s+1); - - case '(': - case ')': - fprintf(stderr, "Unexpected token '%c'\n", *s); - return NULL; - } - - return ldb_parse_simple(s); -} - - -/* - <filter> ::= '(' <filtercomp> ')' -*/ -static struct ldb_parse_tree *ldb_parse_filter(const char **s) -{ - char *l, *s2; - const char *p, *p2; - struct ldb_parse_tree *ret; - - l = ldb_parse_lex(s); - if (!l) { - fprintf(stderr, "Unexpected end of expression\n"); - return NULL; - } - - if (strcmp(l, "(") != 0) { - free(l); - fprintf(stderr, "Expected '('\n"); - return NULL; - } - free(l); - - p = match_brace(*s); - if (!p) { - fprintf(stderr, "Parse error - mismatched braces\n"); - return NULL; - } - p2 = p + 1; - - s2 = strndup(*s, p - *s); - if (!s2) { - errno = ENOMEM; - return NULL; - } - - ret = ldb_parse_filtercomp(s2); - free(s2); - - *s = p2; - - return ret; -} - - -/* - main parser entry point. Takes a search string and returns a parse tree - - expression ::= <simple> | <filter> -*/ -struct ldb_parse_tree *ldb_parse_tree(const char *s) -{ - while (isspace(*s)) s++; - - if (*s == '(') { - return ldb_parse_filter(&s); - } - - return ldb_parse_simple(s); -} - -/* - free a parse tree returned from ldb_parse_tree() -*/ -void ldb_parse_tree_free(struct ldb_parse_tree *tree) -{ - int i; - - switch (tree->operation) { - case LDB_OP_SIMPLE: - free(tree->u.simple.attr); - if (tree->u.simple.value.data) free(tree->u.simple.value.data); - break; - - case LDB_OP_AND: - case LDB_OP_OR: - for (i=0;i<tree->u.list.num_elements;i++) { - ldb_parse_tree_free(tree->u.list.elements[i]); - } - if (tree->u.list.elements) free(tree->u.list.elements); - break; - - case LDB_OP_NOT: - ldb_parse_tree_free(tree->u.not.child); - break; - } - - free(tree); -} - -#if TEST_PROGRAM -/* - return a string representation of a parse tree - used for debugging -*/ -static char *tree_string(struct ldb_parse_tree *tree) -{ - char *s = NULL; - char *s1, *s2; - int i; - - switch (tree->operation) { - case LDB_OP_SIMPLE: - asprintf(&s, "( %s = \"%s\" )", tree->u.simple.attr, - (char *)tree->u.simple.value.data); - break; - - case LDB_OP_AND: - case LDB_OP_OR: - asprintf(&s, "( %c", tree->operation==LDB_OP_AND?'&':'|'); - if (!s) return NULL; - - for (i=0;i<tree->u.list.num_elements;i++) { - s1 = tree_string(tree->u.list.elements[i]); - if (!s1) { - free(s); - return NULL; - } - asprintf(&s2, "%s %s", s, s1); - free(s); - free(s1); - s = s2; - } - if (!s) { - return NULL; - } - asprintf(&s2, "%s )", s); - free(s); - s = s2; - break; - - case LDB_OP_NOT: - s1 = tree_string(tree->u.not.child); - asprintf(&s, "( ! %s )", s1); - free(s1); - break; - } - return s; -} - - -/* - print a tree - */ -static void print_tree(struct ldb_parse_tree *tree) -{ - char *s = tree_string(tree); - printf("%s\n", s); - free(s); -} - - - int main(void) -{ - char line[1000]; - int ret = 0; - - while (fgets(line, sizeof(line)-1, stdin)) { - struct ldb_parse_tree *tree; - - if (line[strlen(line)-1] == '\n') { - line[strlen(line)-1] = 0; - } - tree = ldb_parse_tree(line); - if (!tree) { - fprintf(stderr, "Failed to parse\n"); - ret = 1; - continue; - } - print_tree(tree); - ldb_parse_tree_free(tree); - } - - return ret; -} -#endif /* TEST_PROGRAM */ - diff --git a/source4/lib/ldb/ldb_tdb/ldb_parse.h b/source4/lib/ldb/ldb_tdb/ldb_parse.h deleted file mode 100644 index 995e0e04ff..0000000000 --- a/source4/lib/ldb/ldb_tdb/ldb_parse.h +++ /dev/null @@ -1,40 +0,0 @@ - /* - Unix SMB/CIFS implementation. - - parse a LDAP-like expression - header - - Copyright (C) Andrew Tridgell 2004 - - 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 2 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, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -enum ldb_parse_op {LDB_OP_SIMPLE, LDB_OP_AND, LDB_OP_OR, LDB_OP_NOT}; - -struct ldb_parse_tree { - enum ldb_parse_op operation; - union { - struct { - char *attr; - struct ldb_val value; - } simple; - struct { - unsigned int num_elements; - struct ldb_parse_tree **elements; - } list; - struct { - struct ldb_parse_tree *child; - } not; - } u; -}; |