mirror of
https://github.com/samba-team/samba.git
synced 2025-01-13 13:18:06 +03:00
building with Makefile.ldb now works
This commit is contained in:
parent
dc6f41f9e7
commit
12538cd2c6
@ -1,9 +1,11 @@
|
||||
CFLAGS=-Wall -g -Iinclude -I. -DSTANDALONE=1 -DUSE_MMAP=1
|
||||
OPENLDAP=/home/tridge/samba/openldap/prefix
|
||||
TDBDIR=../tdb
|
||||
|
||||
CFLAGS=-Wall -g -Iinclude -I. -I.. -DSTANDALONE=1 -DUSE_MMAP=1
|
||||
|
||||
LIB_FLAGS=-Llib -lldb -L$(OPENLDAP)/lib -lldap
|
||||
|
||||
TDB_OBJ=tdb/tdb.o tdb/spinlock.o
|
||||
TDB_OBJ=$(TDBDIR)/tdb.o $(TDBDIR)/spinlock.o
|
||||
|
||||
LDB_TDB_OBJ=ldb_tdb/ldb_match.o ldb_tdb/ldb_tdb.o \
|
||||
ldb_tdb/ldb_pack.o ldb_tdb/ldb_search.o ldb_tdb/ldb_index.o
|
||||
@ -20,7 +22,15 @@ BINS = bin/ldbadd bin/ldbsearch bin/ldbdel
|
||||
|
||||
LIBS = $(LDB_LIB)($(OBJS))
|
||||
|
||||
all: $(BINS) $(LIBS)
|
||||
DIRS = lib bin
|
||||
|
||||
all: $(DIRS) $(BINS) $(LIBS)
|
||||
|
||||
lib:
|
||||
mkdir -p lib
|
||||
|
||||
bin:
|
||||
mkdir -p bin
|
||||
|
||||
lib/libldb.a: $(OBJS)
|
||||
|
||||
|
126
source/lib/ldb/include/proto.h
Normal file
126
source/lib/ldb/include/proto.h
Normal file
@ -0,0 +1,126 @@
|
||||
#ifndef _PROTO_H_
|
||||
#define _PROTO_H_
|
||||
|
||||
/* This file is automatically generated with "make proto". DO NOT EDIT */
|
||||
|
||||
|
||||
/* The following definitions come from common/ldb.c */
|
||||
|
||||
struct ldb_context *ldb_connect(const char *url, unsigned int flags,
|
||||
const char *options[]);
|
||||
int ldb_close(struct ldb_context *ldb);
|
||||
int ldb_search(struct ldb_context *ldb,
|
||||
const char *base,
|
||||
enum ldb_scope scope,
|
||||
const char *expression,
|
||||
const char *attrs[], struct ldb_message ***res);
|
||||
int ldb_search_free(struct ldb_context *ldb, struct ldb_message **msgs);
|
||||
int ldb_add(struct ldb_context *ldb,
|
||||
const struct ldb_message *message);
|
||||
int ldb_modify(struct ldb_context *ldb,
|
||||
const struct ldb_message *message);
|
||||
int ldb_delete(struct ldb_context *ldb, const char *dn);
|
||||
const char *ldb_errstring(struct ldb_context *ldb);
|
||||
|
||||
/* The following definitions come from common/ldb_ldif.c */
|
||||
|
||||
char *ldb_base64_encode(const char *buf, int len);
|
||||
int ldb_should_b64_encode(const struct ldb_val *val);
|
||||
int ldif_write(int (*fprintf_fn)(void *, const char *, ...),
|
||||
void *private,
|
||||
const struct ldb_message *msg);
|
||||
void ldif_read_free(struct ldb_message *msg);
|
||||
struct ldb_message *ldif_read(int (*fgetc_fn)(void *), void *private);
|
||||
struct ldb_message *ldif_read_file(FILE *f);
|
||||
struct ldb_message *ldif_read_string(const char *s);
|
||||
int ldif_write_file(FILE *f, const struct ldb_message *msg);
|
||||
|
||||
/* The following definitions come from common/ldb_parse.c */
|
||||
|
||||
struct ldb_parse_tree *ldb_parse_tree(const char *s);
|
||||
void ldb_parse_tree_free(struct ldb_parse_tree *tree);
|
||||
|
||||
/* The following definitions come from common/util.c */
|
||||
|
||||
void *realloc_array(void *ptr, size_t el_size, unsigned count);
|
||||
int list_find(const void *needle,
|
||||
const void *base, size_t nmemb, size_t size, comparison_fn_t comp_fn);
|
||||
|
||||
/* The following definitions come from ldb_ldap/ldb_ldap.c */
|
||||
|
||||
struct ldb_context *lldb_connect(const char *url,
|
||||
unsigned int flags,
|
||||
const char *options[]);
|
||||
|
||||
/* The following definitions come from ldb_tdb/ldb_index.c */
|
||||
|
||||
int ltdb_search_indexed(struct ldb_context *ldb,
|
||||
const char *base,
|
||||
enum ldb_scope scope,
|
||||
struct ldb_parse_tree *tree,
|
||||
const char *attrs[], struct ldb_message ***res);
|
||||
int ltdb_index_add(struct ldb_context *ldb, const struct ldb_message *msg);
|
||||
int ltdb_index_del(struct ldb_context *ldb, const struct ldb_message *msg);
|
||||
|
||||
/* The following definitions come from ldb_tdb/ldb_match.c */
|
||||
|
||||
int ldb_message_match(struct ldb_context *ldb,
|
||||
struct ldb_message *msg,
|
||||
struct ldb_parse_tree *tree,
|
||||
const char *base,
|
||||
enum ldb_scope scope);
|
||||
|
||||
/* The following definitions come from ldb_tdb/ldb_pack.c */
|
||||
|
||||
int ltdb_pack_data(struct ldb_context *ctx,
|
||||
const struct ldb_message *message,
|
||||
struct TDB_DATA *data);
|
||||
int ltdb_unpack_data(struct ldb_context *ctx,
|
||||
const struct TDB_DATA *data,
|
||||
struct ldb_message *message);
|
||||
|
||||
/* The following definitions come from ldb_tdb/ldb_search.c */
|
||||
|
||||
int ldb_msg_find_attr(const struct ldb_message *msg, const char *attr);
|
||||
int ltdb_has_wildcard(const struct ldb_val *val);
|
||||
void ltdb_search_dn1_free(struct ldb_context *ldb, struct ldb_message *msg);
|
||||
int ltdb_search_dn1(struct ldb_context *ldb, const char *dn, struct ldb_message *msg);
|
||||
int ltdb_search_dn(struct ldb_context *ldb, char *dn,
|
||||
const char *attrs[], struct ldb_message ***res);
|
||||
int ltdb_add_attr_results(struct ldb_context *ldb, struct ldb_message *msg,
|
||||
const char *attrs[],
|
||||
unsigned int *count,
|
||||
struct ldb_message ***res);
|
||||
int ltdb_search_free(struct ldb_context *ldb, struct ldb_message **msgs);
|
||||
int ltdb_search(struct ldb_context *ldb, const char *base,
|
||||
enum ldb_scope scope, const char *expression,
|
||||
const char *attrs[], struct ldb_message ***res);
|
||||
|
||||
/* The following definitions come from ldb_tdb/ldb_tdb.c */
|
||||
|
||||
struct TDB_DATA ltdb_key(const char *dn);
|
||||
int ltdb_store(struct ldb_context *ldb, const struct ldb_message *msg, int flgs);
|
||||
int ltdb_delete_noindex(struct ldb_context *ldb, const char *dn);
|
||||
struct ldb_context *ltdb_connect(const char *url,
|
||||
unsigned int flags,
|
||||
const char *options[]);
|
||||
|
||||
/* The following definitions come from ldb_tdb/ldbadd.c */
|
||||
|
||||
|
||||
/* The following definitions come from ldb_tdb/ldbdel.c */
|
||||
|
||||
|
||||
/* The following definitions come from ldb_tdb/ldbsearch.c */
|
||||
|
||||
|
||||
/* The following definitions come from tools/ldbadd.c */
|
||||
|
||||
|
||||
/* The following definitions come from tools/ldbdel.c */
|
||||
|
||||
|
||||
/* The following definitions come from tools/ldbsearch.c */
|
||||
|
||||
|
||||
#endif /* _PROTO_H_ */
|
@ -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");
|
||||
}
|
@ -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 */
|
||||
|
@ -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;
|
||||
};
|
Loading…
Reference in New Issue
Block a user