Module: sip-router Branch: janakj/ldap Commit: b5695d3fc766ade1a6cb9254541b3167496689ae URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=b5695d3f...
Author: Jan Janak jan@iptel.org Committer: Jan Janak jan@iptel.org Date: Tue May 13 12:31:10 2008 +0000
- added support for boolean attributes in result - added support for attribute syntaxes in the config file - the code converting integers rearranged
---
modules/db_ldap/ld_config.c | 100 +++++++++++++++++++++++++++++++++++++------ modules/db_ldap/ld_config.h | 18 ++++++- modules/db_ldap/ld_fld.c | 33 +++++++++++--- modules/db_ldap/ld_fld.h | 4 +- 4 files changed, 129 insertions(+), 26 deletions(-)
diff --git a/modules/db_ldap/ld_config.c b/modules/db_ldap/ld_config.c index a813c1a..57bcee1 100644 --- a/modules/db_ldap/ld_config.c +++ b/modules/db_ldap/ld_config.c @@ -91,6 +91,7 @@ struct parser_tab {
static struct parser_tab token_scope[]; static struct parser_tab option_name[]; +static struct parser_tab token_syntax[];
/* @@ -581,6 +582,7 @@ static int parse_field_map(token_t* token) int ret; token_t t; void* ptr; + struct parser_tab* r;
ret = lex(&t, 0); if (ret < 0) return -1; @@ -596,24 +598,33 @@ static int parse_field_map(token_t* token) return -1; }
- ptr = pkg_realloc(pstate.cfg->fields, sizeof(char*) * (pstate.cfg->n + 1)); + ptr = pkg_realloc(pstate.cfg->field, sizeof(char*) * (pstate.cfg->n + 1)); if (ptr == NULL) { ERR("ldap:%s:%d: Out of memory\n", pstate.file, token->start.line); return -1; } - pstate.cfg->fields = (char**)ptr; - pstate.cfg->fields[pstate.cfg->n] = NULL; + pstate.cfg->field = (char**)ptr; + pstate.cfg->field[pstate.cfg->n] = NULL;
- ptr = pkg_realloc(pstate.cfg->attrs, sizeof(char*) * (pstate.cfg->n + 1)); + ptr = pkg_realloc(pstate.cfg->attr, sizeof(char*) * (pstate.cfg->n + 1)); if (ptr == NULL) { ERR("ldap:%s:%d: Out of memory\n", pstate.file, token->start.line); return -1; } - pstate.cfg->attrs = (char**)ptr; - pstate.cfg->attrs[pstate.cfg->n] = NULL; + pstate.cfg->attr = (char**)ptr; + pstate.cfg->attr[pstate.cfg->n] = NULL;
+ ptr = pkg_realloc(pstate.cfg->syntax, sizeof(enum ld_syntax) * (pstate.cfg->n + 1)); + if (ptr == NULL) { + ERR("ldap:%s:%d: Out of memory\n", + pstate.file, token->start.line); + return -1; + } + pstate.cfg->syntax = (enum ld_syntax*)ptr; + pstate.cfg->syntax[pstate.cfg->n] = LD_SYNTAX_STRING; + pstate.cfg->n++;
ret = lex(&t, 0); @@ -631,8 +642,8 @@ static int parse_field_map(token_t* token) return -1; } - pstate.cfg->fields[pstate.cfg->n - 1] = as_asciiz(&t.val); - if (pstate.cfg->fields[pstate.cfg->n - 1] == NULL) { + pstate.cfg->field[pstate.cfg->n - 1] = as_asciiz(&t.val); + if (pstate.cfg->field[pstate.cfg->n - 1] == NULL) { ERR("ldap:%s:%d: Out of memory\n", pstate.file, token->start.line); return -1; @@ -654,11 +665,58 @@ static int parse_field_map(token_t* token) ret = lex(&t, 0); if (ret < 0) return -1; if (ret == 0) { - ERR("ldap:%s:%d:%d: LDAP Attribute name expected\n", + ERR("ldap:%s:%d:%d: LDAP Attribute syntax or name expected\n", pstate.file, token->start.line, token->start.col); return -1; }
+ if (t.type == '(') { + ret = lex(&t, 0); + if (ret < 0) return -1; + if (ret == 0) { + ERR("ldap:%s:%d:%d: LDAP Attribute Syntax expected\n", + pstate.file, token->start.line, token->start.col); + return -1; + } + if (t.type != TOKEN_ALPHA) { + ERR("ldap:%s:%d:%d: Invalid LDAP attribute syntax format %d:'%.*s'\n", + pstate.file, t.start.line, t.start.col, + t.type, t.val.len, ZSW(t.val.s)); + return -1; + } + + r = lookup_token(token_syntax, &t.val); + if (!r) { + ERR("ldap:%s:%d:%d: Invalid syntaxt value '%.*s'\n", + pstate.file, t.start.line, t.start.col, + t.val.len, ZSW(t.val.s)); + return -1; + } + pstate.cfg->syntax[pstate.cfg->n - 1] = r->u.ival; + + ret = lex(&t, 0); + if (ret < 0) return -1; + if (ret == 0) { + ERR("ldap:%s:%d:%d: Closing ')' missing in attribute syntax\n", + pstate.file, token->start.line, token->start.col); + return -1; + } + + if (t.type != ')') { + ERR("ldap:%s:%d:%d: Syntax error, ')' expected\n", + pstate.file, t.start.line, t.start.col); + return -1; + } + + ret = lex(&t, 0); + if (ret < 0) return -1; + if (ret == 0) { + ERR("ldap:%s:%d:%d: LDAP Attribute name expected\n", + pstate.file, token->start.line, token->start.col); + return -1; + } + } + if (t.type != TOKEN_ALPHA) { ERR("ldap:%s:%d:%d: Invalid LDAP attribute name format %d:'%.*s'\n", pstate.file, t.start.line, t.start.col, @@ -666,8 +724,8 @@ static int parse_field_map(token_t* token) return -1; }
- pstate.cfg->attrs[pstate.cfg->n - 1] = as_asciiz(&t.val); - if (pstate.cfg->attrs[pstate.cfg->n - 1] == NULL) { + pstate.cfg->attr[pstate.cfg->n - 1] = as_asciiz(&t.val); + if (pstate.cfg->attr[pstate.cfg->n - 1] == NULL) { ERR("ldap:%s:%d: Out of memory\n", pstate.file, token->start.line); return -1; @@ -846,13 +904,15 @@ struct ld_config* ld_find_config(str* table) }
-char* ld_find_attr_name(struct ld_config* cfg, char* fld_name) +char* ld_find_attr_name(enum ld_syntax* syntax, struct ld_config* cfg, char* fld_name) { int i;
for(i = 0; i < cfg->n; i++) { - if (!strcmp(fld_name, cfg->fields[i])) - return cfg->attrs[i]; + if (!strcmp(fld_name, cfg->field[i])) { + *syntax = cfg->syntax[i]; + return cfg->attr[i]; + } } return NULL; } @@ -867,6 +927,18 @@ static struct parser_tab token_scope[] = { };
+static struct parser_tab token_syntax[] = { + {STR_STATIC_INIT("GeneralizedTime"), {.ival = LD_SYNTAX_GENTIME}}, + {STR_STATIC_INIT("Integer"), {.ival = LD_SYNTAX_INT}}, + {STR_STATIC_INIT("BitString"), {.ival = LD_SYNTAX_BIT}}, + {STR_STATIC_INIT("Boolean"), {.ival = LD_SYNTAX_BOOL}}, + {STR_STATIC_INIT("String"), {.ival = LD_SYNTAX_STRING}}, + {STR_STATIC_INIT("Binary"), {.ival = LD_SYNTAX_BIN}}, + {STR_STATIC_INIT("Float"), {.ival = LD_SYNTAX_FLOAT}}, + {STR_NULL} +}; + + static struct parser_tab option_name[] = { {STR_STATIC_INIT("base"), {.fval = parse_search_base}}, {STR_STATIC_INIT("scope"), {.fval = parse_search_scope}}, diff --git a/modules/db_ldap/ld_config.h b/modules/db_ldap/ld_config.h index 980c5fc..640d856 100644 --- a/modules/db_ldap/ld_config.h +++ b/modules/db_ldap/ld_config.h @@ -34,13 +34,25 @@ #include "../../str.h"
+enum ld_syntax { + LD_SYNTAX_STRING = 0, + LD_SYNTAX_GENTIME, + LD_SYNTAX_INT, + LD_SYNTAX_BIT, + LD_SYNTAX_BOOL, + LD_SYNTAX_BIN, + LD_SYNTAX_FLOAT +}; + + struct ld_config { str table; /**< Name of the db api table */ char* base; /**< The search base to be used with the table */ int scope; /**< LDAP scope */ char* filter; /**< The search filter */ - char** fields; /**< An array of DB API fields */ - char** attrs; /**< An array of LDAP attribute names */ + char** field; /**< An array of DB API fields */ + char** attr; /**< An array of LDAP attribute names */ + enum ld_syntax* syntax; /**< An array of configured LDAP syntaxes */ int n; /**< Number of fields in the arrays */ struct ld_config* next; /**< The next table in the list */ }; @@ -49,7 +61,7 @@ extern struct ld_config* ld_cfg_root;
struct ld_config* ld_find_config(str* table);
-char* ld_find_attr_name(struct ld_config* cfg, char* fld_name); +char* ld_find_attr_name(enum ld_syntax* syntax, struct ld_config* cfg, char* fld_name);
int ld_load_config(str* filename);
diff --git a/modules/db_ldap/ld_fld.c b/modules/db_ldap/ld_fld.c index f9d2c95..c8cfb6c 100644 --- a/modules/db_ldap/ld_fld.c +++ b/modules/db_ldap/ld_fld.c @@ -98,7 +98,7 @@ int ld_resolve_fld(db_fld_t* fld, struct ld_config* cfg)
for(i = 0; !DB_FLD_EMPTY(fld) && !DB_FLD_LAST(fld[i]); i++) { lfld = DB_GET_PAYLOAD(fld + i); - lfld->attr.s = ld_find_attr_name(cfg, fld[i].name); + lfld->attr.s = ld_find_attr_name(&lfld->syntax, cfg, fld[i].name); if (lfld->attr.s == NULL) lfld->attr.s = fld[i].name; if (lfld->attr.s) lfld->attr.len = strlen(lfld->attr.s); } @@ -227,18 +227,35 @@ int ld_ldap2fld(db_fld_t* fld, LDAP* ldap, LDAPMessage* msg)
case DB_INT: case DB_BITMAP: - if (v.s[0] == ''' && - v.s[v.len - 1] == 'B' && + if (v.s[0] == ''' && v.s[v.len - 1] == 'B' && v.s[v.len - 2] == ''') { - v.s++; v.len -= 3; - return ldap_bit2db_int(&fld[i].v.int4, &v); - } else { - return ldap_int2db_int(&fld[i].v.int4, &v); + if (ldap_bit2db_int(&fld[i].v.int4, &v) != 0) { + ERR("ldap: Error while converting bit string '%.*s'\n", + v.len, ZSW(v.s)); + return -1; + } + break; + } + + if (v.len == 4 && !strncasecmp("TRUE", v.s, v.len)) { + fld[i].v.int4 = 1; + break; + } + + if (v.len == 5 && !strncasecmp("FALSE", v.s, v.len)) { + fld[i].v.int4 = 0; + break; } - break;
+ if (ldap_int2db_int(&fld[i].v.int4, &v) != 0) { + ERR("ldap: Error while converting %.*s to integer\n", + v.len, ZSW(v.s)); + return -1; + } + break; + case DB_DATETIME: if (ldap_gentime2db_datetime(&fld[i].v.time, &v) != 0) { ERR("ldap: Error while converting LDAP time value '%.*s'\n", diff --git a/modules/db_ldap/ld_fld.h b/modules/db_ldap/ld_fld.h index 6994ace..c50535e 100644 --- a/modules/db_ldap/ld_fld.h +++ b/modules/db_ldap/ld_fld.h @@ -45,9 +45,11 @@
#include <ldap.h>
+ struct ld_fld { db_drv_t gen; - str attr; /**< Name of corresponding LDAP attribute */ + str attr; /**< Name of corresponding LDAP attribute */ + enum ld_syntax syntax; /**< LDAP attribute syntax */ struct berval** values; /**< Values retrieved from the LDAP result */ };