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