1 /* $OpenBSD: schema.h,v 1.7 2010/11/04 15:35:00 martinh Exp $ */ 2 3 /* 4 * Copyright (c) 2010 Martin Hedenfalk <martinh@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #ifndef _schema_h_ 20 #define _schema_h_ 21 22 #define OBJ_NAME(obj) ((obj)->names ? SLIST_FIRST((obj)->names)->name : \ 23 (obj)->oid) 24 #define ATTR_NAME(at) OBJ_NAME(at) 25 26 enum usage { 27 USAGE_USER_APP, 28 USAGE_DIR_OP, /* operational attribute */ 29 USAGE_DIST_OP, /* operational attribute */ 30 USAGE_DSA_OP /* operational attribute */ 31 }; 32 33 enum match_rule_type { 34 MATCH_EQUALITY, 35 MATCH_ORDERING, 36 MATCH_SUBSTR, 37 }; 38 39 struct name { 40 SLIST_ENTRY(name) next; 41 char *name; 42 }; 43 SLIST_HEAD(name_list, name); 44 45 struct schema; 46 struct syntax { 47 char *oid; 48 char *desc; 49 int (*is_valid)(struct schema *schema, char *value, 50 size_t len); 51 }; 52 53 struct match_rule 54 { 55 char *oid; 56 char *name; 57 enum match_rule_type type; 58 int (*prepare)(char *value, size_t len); 59 const char *syntax_oid; 60 const char **alt_syntax_oids; 61 }; 62 63 struct attr_type { 64 RB_ENTRY(attr_type) link; 65 char *oid; 66 struct name_list *names; 67 char *desc; 68 int obsolete; 69 struct attr_type *sup; 70 const struct match_rule *equality; 71 const struct match_rule *ordering; 72 const struct match_rule *substr; 73 const struct syntax *syntax; 74 int single; 75 int collective; 76 int immutable; /* no-user-modification */ 77 enum usage usage; 78 }; 79 RB_HEAD(attr_type_tree, attr_type); 80 RB_PROTOTYPE(attr_type_tree, attr_type, link, attr_oid_cmp); 81 82 struct attr_ptr { 83 SLIST_ENTRY(attr_ptr) next; 84 struct attr_type *attr_type; 85 }; 86 SLIST_HEAD(attr_list, attr_ptr); 87 88 enum object_kind { 89 KIND_ABSTRACT, 90 KIND_STRUCTURAL, 91 KIND_AUXILIARY 92 }; 93 94 struct object; 95 struct obj_ptr { 96 SLIST_ENTRY(obj_ptr) next; 97 struct object *object; 98 }; 99 SLIST_HEAD(obj_list, obj_ptr); 100 101 struct object { 102 RB_ENTRY(object) link; 103 char *oid; 104 struct name_list *names; 105 char *desc; 106 int obsolete; 107 struct obj_list *sup; 108 enum object_kind kind; 109 struct attr_list *must; 110 struct attr_list *may; 111 }; 112 RB_HEAD(object_tree, object); 113 RB_PROTOTYPE(object_tree, object, link, obj_oid_cmp); 114 115 struct oidname { 116 RB_ENTRY(oidname) link; 117 const char *on_name; 118 #define on_attr_type on_ptr.ou_attr_type 119 #define on_object on_ptr.ou_object 120 union { 121 struct attr_type *ou_attr_type; 122 struct object *ou_object; 123 } on_ptr; 124 }; 125 RB_HEAD(oidname_tree, oidname); 126 RB_PROTOTYPE(oidname_tree, oidname, link, oidname_cmp); 127 128 struct symoid { 129 RB_ENTRY(symoid) link; 130 char *name; /* symbolic name */ 131 char *oid; 132 }; 133 RB_HEAD(symoid_tree, symoid); 134 RB_PROTOTYPE(symoid_tree, symoid, link, symoid_cmp); 135 136 #define SCHEMA_MAXPUSHBACK 128 137 138 struct schema 139 { 140 struct attr_type_tree attr_types; 141 struct oidname_tree attr_names; 142 struct object_tree objects; 143 struct oidname_tree object_names; 144 struct symoid_tree symbolic_oids; 145 146 FILE *fp; 147 const char *filename; 148 char pushback_buffer[SCHEMA_MAXPUSHBACK]; 149 int pushback_index; 150 int lineno; 151 int error; 152 }; 153 154 struct schema *schema_new(void); 155 int schema_parse(struct schema *schema, 156 const char *filename); 157 int schema_dump_object(struct object *obj, 158 char *buf, size_t size); 159 int schema_dump_attribute(struct attr_type *obj, 160 char *buf, size_t size); 161 int schema_dump_match_rule(struct match_rule *mr, 162 char *buf, size_t size); 163 164 struct attr_type *lookup_attribute_by_oid(struct schema *schema, char *oid); 165 struct attr_type *lookup_attribute_by_name(struct schema *schema, char *name); 166 struct attr_type *lookup_attribute(struct schema *schema, char *oid_or_name); 167 struct object *lookup_object_by_oid(struct schema *schema, char *oid); 168 struct object *lookup_object_by_name(struct schema *schema, char *name); 169 struct object *lookup_object(struct schema *schema, char *oid_or_name); 170 char *lookup_symbolic_oid(struct schema *schema, char *name); 171 int is_oidstr(const char *oidstr); 172 173 /* syntax.c */ 174 const struct syntax *syntax_lookup(const char *oid); 175 176 /* matching.c */ 177 extern struct match_rule match_rules[]; 178 extern int num_match_rules; 179 const struct match_rule *match_rule_lookup(const char *oid); 180 181 #endif 182 183