3 * Copyright 2004 The OpenLDAP Foundation.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted only as authorized by the OpenLDAP
10 * A copy of this license is available in the file LICENSE in the
11 * top-level directory of the distribution or, alternatively, at
12 * <http://www.OpenLDAP.org/license.html>.
25 * Schema reader that allows us to define DSA schema (including
26 * operational attributes and non-user object classes)
28 * A kludge, at best, and in order to avoid including slapd
29 * headers we use fprintf() rather than slapd's native logging,
30 * which may confuse users...
35 #include <ldap_schema.h>
37 extern int at_add(LDAPAttributeType *at, const char **err);
38 extern int oc_add(LDAPObjectClass *oc, int user, const char **err);
39 extern int cr_add(LDAPContentRule *cr, int user, const char **err);
43 static char *fp_getline(FILE *fp, int *lineno);
44 static void fp_getline_init(int *lineno);
45 static int fp_parse_line(int lineno, char *line);
46 static char *strtok_quote( char *line, char *sep );
48 static char **cargv = NULL;
49 static int cargv_size = 0;
51 static char *strtok_quote_ptr;
53 int init_module(int argc, char *argv[]);
55 static int dsaschema_parse_at(const char *fname, int lineno, char *line, char **argv)
57 LDAPAttributeType *at;
61 at = ldap_str2attributetype(line, &code, &err, LDAP_SCHEMA_ALLOW_ALL);
63 fprintf(stderr, "%s: line %d: %s before %s\n",
64 fname, lineno, ldap_scherr2str(code), err);
68 if (at->at_oid == NULL) {
69 fprintf(stderr, "%s: line %d: attributeType has no OID\n",
74 code = at_add(at, &err);
76 fprintf(stderr, "%s: line %d: %s: \"%s\"\n",
77 fname, lineno, ldap_scherr2str(code), err);
86 static int dsaschema_parse_oc(const char *fname, int lineno, char *line, char **argv)
92 oc = ldap_str2objectclass(line, &code, &err, LDAP_SCHEMA_ALLOW_ALL);
94 fprintf(stderr, "%s: line %d: %s before %s\n",
95 fname, lineno, ldap_scherr2str(code), err);
99 if (oc->oc_oid == NULL) {
101 "%s: line %d: objectclass has no OID\n",
106 code = oc_add(oc, 0, &err);
108 fprintf(stderr, "%s: line %d: %s: \"%s\"\n",
109 fname, lineno, ldap_scherr2str(code), err);
117 static int dsaschema_parse_cr(const char *fname, int lineno, char *line, char **argv)
123 cr = ldap_str2contentrule(line, &code, &err, LDAP_SCHEMA_ALLOW_ALL);
125 fprintf(stderr, "%s: line %d: %s before %s\n",
126 fname, lineno, ldap_scherr2str(code), err);
130 if (cr->cr_oid == NULL) {
132 "%s: line %d: objectclass has no OID\n",
137 code = cr_add(cr, 0, &err);
139 fprintf(stderr, "%s: line %d: %s: \"%s\"\n",
140 fname, lineno, ldap_scherr2str(code), err);
148 static int dsaschema_read_config(const char *fname, int depth)
151 char *line, *savefname, *saveline;
152 int savelineno, lineno;
156 cargv = calloc(ARGS_STEP + 1, sizeof(*cargv));
160 cargv_size = ARGS_STEP + 1;
163 fp = fopen(fname, "r");
165 fprintf(stderr, "could not open config file \"%s\": %s (%d)\n",
166 fname, strerror(errno), errno);
169 fp_getline_init(&lineno);
171 while ((line = fp_getline(fp, &lineno)) != NULL) {
172 /* skip comments and blank lines */
173 if (line[0] == '#' || line[0] == '\0') {
177 saveline = strdup(line);
178 if (saveline == NULL) {
182 if (fp_parse_line(lineno, line) != 0) {
190 if (strcasecmp(cargv[0], "attributetype") == 0 ||
191 strcasecmp(cargv[0], "attribute") == 0) {
193 fprintf(stderr, "%s: line %d: illegal attribute type format\n",
196 } else if (*cargv[1] == '(' /*')'*/) {
199 p = strchr(saveline, '(' /*')'*/);
200 rc = dsaschema_parse_at(fname, lineno, p, cargv);
204 fprintf(stderr, "%s: line %d: old attribute type format not supported\n",
207 } else if (strcasecmp(cargv[0], "ditcontentrule") == 0) {
209 p = strchr(saveline, '(' /*')'*/);
210 rc = dsaschema_parse_cr(fname, lineno, p, cargv);
213 } else if (strcasecmp(cargv[0], "objectclass") == 0) {
215 fprintf(stderr, "%s: line %d: illegal objectclass format\n",
218 } else if (*cargv[1] == '(' /*')'*/) {
221 p = strchr(saveline, '(' /*')'*/);
222 rc = dsaschema_parse_oc(fname, lineno, p, cargv);
226 fprintf(stderr, "%s: line %d: object class format not supported\n",
229 } else if (strcasecmp(cargv[0], "include") == 0) {
231 fprintf(stderr, "%s: line %d: missing file name in \"include <filename>\" line",
235 savefname = strdup(cargv[1]);
236 if (savefname == NULL) {
239 if (dsaschema_read_config(savefname, depth + 1) != 0) {
243 lineno = savelineno - 1;
245 fprintf(stderr, "%s: line %d: unknown directive \"%s\" (ignored)\n",
246 fname, lineno, cargv[0]);
258 int init_module(int argc, char *argv[])
263 for (i = 0; i < argc; i++) {
264 rc = dsaschema_read_config(argv[i], 0);
283 token = strtok_quote( line, " \t" );
285 if ( strtok_quote_ptr ) {
286 *strtok_quote_ptr = ' ';
289 if ( strtok_quote_ptr ) {
290 *strtok_quote_ptr = '\0';
293 for ( ; token != NULL; token = strtok_quote( NULL, " \t" ) ) {
294 if ( cargc == cargv_size - 1 ) {
296 tmp = realloc( cargv, (cargv_size + ARGS_STEP) *
302 cargv_size += ARGS_STEP;
304 cargv[cargc++] = token;
311 strtok_quote( char *line, char *sep )
317 strtok_quote_ptr = NULL;
318 if ( line != NULL ) {
321 while ( *next && strchr( sep, *next ) ) {
325 if ( *next == '\0' ) {
331 for ( inquote = 0; *next; ) {
339 memcpy( next, next + 1, strlen( next + 1 ) + 1 );
345 next + 1, strlen( next + 1 ) + 1 );
346 next++; /* dont parse the escaped character */
351 if ( strchr( sep, *next ) != NULL ) {
352 strtok_quote_ptr = next;
365 static char buf[BUFSIZ];
367 static size_t lmax, lcur;
369 #define CATLINE( buf ) \
371 size_t len = strlen( buf ); \
372 while ( lcur + len + 1 > lmax ) { \
374 line = (char *) realloc( line, lmax ); \
376 strcpy( line + lcur, buf ); \
381 fp_getline( FILE *fp, int *lineno )
389 /* hack attack - keeps us from having to keep a stack of bufs... */
390 if ( strncasecmp( line, "include", 7 ) == 0 ) {
395 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
396 /* trim off \r\n or \n */
397 if ( (p = strchr( buf, '\n' )) != NULL ) {
398 if( p > buf && p[-1] == '\r' ) --p;
402 /* trim off trailing \ and append the next line */
403 if ( line[ 0 ] != '\0'
404 && (p = line + strlen( line ) - 1)[ 0 ] == '\\'
405 && p[ -1 ] != '\\' ) {
410 if ( ! isspace( (unsigned char) buf[0] ) ) {
414 /* change leading whitespace to a space */
423 return( line[0] ? line : NULL );
427 fp_getline_init( int *lineno )