📄 ldb_parse.c
字号:
} talloc_free(value); break; case LDB_OP_GREATER: ret->operation = LDB_OP_GREATER; ret->u.comparison.attr = attr; ret->u.comparison.value = ldb_binary_decode(ret, value); if (ret->u.comparison.value.data == NULL) { talloc_free(ret); return NULL; } talloc_free(value); break; case LDB_OP_LESS: ret->operation = LDB_OP_LESS; ret->u.comparison.attr = attr; ret->u.comparison.value = ldb_binary_decode(ret, value); if (ret->u.comparison.value.data == NULL) { talloc_free(ret); return NULL; } talloc_free(value); break; case LDB_OP_APPROX: ret->operation = LDB_OP_APPROX; ret->u.comparison.attr = attr; ret->u.comparison.value = ldb_binary_decode(ret, value); if (ret->u.comparison.value.data == NULL) { talloc_free(ret); return NULL; } talloc_free(value); break; case LDB_OP_EXTENDED: ret = ldb_parse_extended(ret, attr, value); break; default: talloc_free(ret); return NULL; } return ret;}/* parse a filterlist <and> ::= '&' <filterlist> <or> ::= '|' <filterlist> <filterlist> ::= <filter> | <filter> <filterlist>*/static struct ldb_parse_tree *ldb_parse_filterlist(void *mem_ctx, const char **s){ struct ldb_parse_tree *ret, *next; enum ldb_parse_op op; const char *p = *s; switch (*p) { case '&': op = LDB_OP_AND; break; case '|': op = LDB_OP_OR; break; default: return NULL; } p++; while (isspace((unsigned char)*p)) p++; ret = talloc(mem_ctx, struct ldb_parse_tree); if (!ret) { errno = ENOMEM; return NULL; } ret->operation = op; ret->u.list.num_elements = 1; ret->u.list.elements = talloc(ret, struct ldb_parse_tree *); if (!ret->u.list.elements) { errno = ENOMEM; talloc_free(ret); return NULL; } ret->u.list.elements[0] = ldb_parse_filter(ret->u.list.elements, &p); if (!ret->u.list.elements[0]) { talloc_free(ret); return NULL; } while (isspace((unsigned char)*p)) p++; while (*p && (next = ldb_parse_filter(ret->u.list.elements, &p))) { struct ldb_parse_tree **e; e = talloc_realloc(ret, ret->u.list.elements, struct ldb_parse_tree *, ret->u.list.num_elements + 1); if (!e) { errno = ENOMEM; talloc_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((unsigned char)*p)) p++; } *s = p; return ret;}/* <not> ::= '!' <filter>*/static struct ldb_parse_tree *ldb_parse_not(void *mem_ctx, const char **s){ struct ldb_parse_tree *ret; const char *p = *s; if (*p != '!') { return NULL; } p++; ret = talloc(mem_ctx, struct ldb_parse_tree); if (!ret) { errno = ENOMEM; return NULL; } ret->operation = LDB_OP_NOT; ret->u.isnot.child = ldb_parse_filter(ret, &p); if (!ret->u.isnot.child) { talloc_free(ret); return NULL; } *s = p; return ret;}/* parse a filtercomp <filtercomp> ::= <and> | <or> | <not> | <simple>*/static struct ldb_parse_tree *ldb_parse_filtercomp(void *mem_ctx, const char **s){ struct ldb_parse_tree *ret; const char *p = *s; while (isspace((unsigned char)*p)) p++; switch (*p) { case '&': ret = ldb_parse_filterlist(mem_ctx, &p); break; case '|': ret = ldb_parse_filterlist(mem_ctx, &p); break; case '!': ret = ldb_parse_not(mem_ctx, &p); break; case '(': case ')': return NULL; default: ret = ldb_parse_simple(mem_ctx, &p); } *s = p; return ret;}/* <filter> ::= '(' <filtercomp> ')'*/static struct ldb_parse_tree *ldb_parse_filter(void *mem_ctx, const char **s){ struct ldb_parse_tree *ret; const char *p = *s; if (*p != '(') { return NULL; } p++; ret = ldb_parse_filtercomp(mem_ctx, &p); if (*p != ')') { return NULL; } p++; while (isspace((unsigned char)*p)) { p++; } *s = p; 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(void *mem_ctx, const char *s){ if (s == NULL || *s == 0) { s = "(|(objectClass=*)(distinguishedName=*))"; } while (isspace((unsigned char)*s)) s++; if (*s == '(') { return ldb_parse_filter(mem_ctx, &s); } return ldb_parse_simple(mem_ctx, &s);}/* construct a ldap parse filter given a parse tree*/char *ldb_filter_from_tree(void *mem_ctx, struct ldb_parse_tree *tree){ char *s, *s2, *ret; int i; if (tree == NULL) { return NULL; } switch (tree->operation) { case LDB_OP_AND: case LDB_OP_OR: ret = talloc_asprintf(mem_ctx, "(%c", tree->operation==LDB_OP_AND?'&':'|'); if (ret == NULL) return NULL; for (i=0;i<tree->u.list.num_elements;i++) { s = ldb_filter_from_tree(mem_ctx, tree->u.list.elements[i]); if (s == NULL) { talloc_free(ret); return NULL; } s2 = talloc_asprintf_append(ret, "%s", s); talloc_free(s); if (s2 == NULL) { talloc_free(ret); return NULL; } ret = s2; } s = talloc_asprintf_append(ret, ")"); if (s == NULL) { talloc_free(ret); return NULL; } return s; case LDB_OP_NOT: s = ldb_filter_from_tree(mem_ctx, tree->u.isnot.child); if (s == NULL) return NULL; ret = talloc_asprintf(mem_ctx, "(!%s)", s); talloc_free(s); return ret; case LDB_OP_EQUALITY: s = ldb_binary_encode(mem_ctx, tree->u.equality.value); if (s == NULL) return NULL; ret = talloc_asprintf(mem_ctx, "(%s=%s)", tree->u.equality.attr, s); talloc_free(s); return ret; case LDB_OP_SUBSTRING: ret = talloc_asprintf(mem_ctx, "(%s=%s", tree->u.substring.attr, tree->u.substring.start_with_wildcard?"*":""); if (ret == NULL) return NULL; for (i = 0; tree->u.substring.chunks[i]; i++) { s2 = ldb_binary_encode(mem_ctx, *(tree->u.substring.chunks[i])); if (s2 == NULL) { talloc_free(ret); return NULL; } if (tree->u.substring.chunks[i+1] || tree->u.substring.end_with_wildcard) { s = talloc_asprintf_append(ret, "%s*", s2); } else { s = talloc_asprintf_append(ret, "%s", s2); } if (s == NULL) { talloc_free(ret); return NULL; } ret = s; } s = talloc_asprintf_append(ret, ")"); if (s == NULL) { talloc_free(ret); return NULL; } ret = s; return ret; case LDB_OP_GREATER: s = ldb_binary_encode(mem_ctx, tree->u.equality.value); if (s == NULL) return NULL; ret = talloc_asprintf(mem_ctx, "(%s>=%s)", tree->u.equality.attr, s); talloc_free(s); return ret; case LDB_OP_LESS: s = ldb_binary_encode(mem_ctx, tree->u.equality.value); if (s == NULL) return NULL; ret = talloc_asprintf(mem_ctx, "(%s<=%s)", tree->u.equality.attr, s); talloc_free(s); return ret; case LDB_OP_PRESENT: ret = talloc_asprintf(mem_ctx, "(%s=*)", tree->u.present.attr); return ret; case LDB_OP_APPROX: s = ldb_binary_encode(mem_ctx, tree->u.equality.value); if (s == NULL) return NULL; ret = talloc_asprintf(mem_ctx, "(%s~=%s)", tree->u.equality.attr, s); talloc_free(s); return ret; case LDB_OP_EXTENDED: s = ldb_binary_encode(mem_ctx, tree->u.extended.value); if (s == NULL) return NULL; ret = talloc_asprintf(mem_ctx, "(%s%s%s%s:=%s)", tree->u.extended.attr?tree->u.extended.attr:"", tree->u.extended.dnAttributes?":dn":"", tree->u.extended.rule_id?":":"", tree->u.extended.rule_id?tree->u.extended.rule_id:"", s); talloc_free(s); return ret; } return NULL;}/* replace any occurances of an attribute name in the parse tree with a new name*/void ldb_parse_tree_attr_replace(struct ldb_parse_tree *tree, const char *attr, const char *replace){ int i; switch (tree->operation) { case LDB_OP_AND: case LDB_OP_OR: for (i=0;i<tree->u.list.num_elements;i++) { ldb_parse_tree_attr_replace(tree->u.list.elements[i], attr, replace); } break; case LDB_OP_NOT: ldb_parse_tree_attr_replace(tree->u.isnot.child, attr, replace); break; case LDB_OP_EQUALITY: case LDB_OP_GREATER: case LDB_OP_LESS: case LDB_OP_APPROX: if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) { tree->u.equality.attr = replace; } break; case LDB_OP_SUBSTRING: if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) { tree->u.substring.attr = replace; } break; case LDB_OP_PRESENT: if (ldb_attr_cmp(tree->u.present.attr, attr) == 0) { tree->u.present.attr = replace; } break; case LDB_OP_EXTENDED: if (tree->u.extended.attr && ldb_attr_cmp(tree->u.extended.attr, attr) == 0) { tree->u.extended.attr = replace; } break; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -