3 * Copyright 2004-2005 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>.
17 #include <ac/string.h>
19 #include <ac/signal.h>
21 #include <ac/stdlib.h>
24 #include <ac/unistd.h>
29 * Schema reader that allows us to define DSA schema (including
30 * operational attributes and non-user object classes)
32 * A kludge, at best, and in order to avoid including slapd
33 * headers we use fprintf() rather than slapd's native logging,
34 * which may confuse users...
39 #include <ldap_schema.h>
41 extern int at_add(LDAPAttributeType *at, const char **err);
42 extern int oc_add(LDAPObjectClass *oc, int user, const char **err);
43 extern int cr_add(LDAPContentRule *cr, int user, const char **err);
47 static char *fp_getline(FILE *fp, int *lineno);
48 static void fp_getline_init(int *lineno);
49 static int fp_parse_line(int lineno, char *line);
50 static char *strtok_quote( char *line, char *sep );
52 static char **cargv = NULL;
53 static int cargv_size = 0;
55 static char *strtok_quote_ptr;
57 int init_module(int argc, char *argv[]);
59 static int dsaschema_parse_at(const char *fname, int lineno, char *line, char **argv)
61 LDAPAttributeType *at;
65 at = ldap_str2attributetype(line, &code, &err, LDAP_SCHEMA_ALLOW_ALL);
67 fprintf(stderr, "%s: line %d: %s before %s\n",
68 fname, lineno, ldap_scherr2str(code), err);
72 if (at->at_oid == NULL) {
73 fprintf(stderr, "%s: line %d: attributeType has no OID\n",
78 code = at_add(at, &err);
80 fprintf(stderr, "%s: line %d: %s: \"%s\"\n",
81 fname, lineno, ldap_scherr2str(code), err);
90 static int dsaschema_parse_oc(const char *fname, int lineno, char *line, char **argv)
96 oc = ldap_str2objectclass(line, &code, &err, LDAP_SCHEMA_ALLOW_ALL);
98 fprintf(stderr, "%s: line %d: %s before %s\n",
99 fname, lineno, ldap_scherr2str(code), err);
103 if (oc->oc_oid == NULL) {
105 "%s: line %d: objectclass has no OID\n",
110 code = oc_add(oc, 0, &err);
112 fprintf(stderr, "%s: line %d: %s: \"%s\"\n",
113 fname, lineno, ldap_scherr2str(code), err);
121 static int dsaschema_parse_cr(const char *fname, int lineno, char *line, char **argv)
127 cr = ldap_str2contentrule(line, &code, &err, LDAP_SCHEMA_ALLOW_ALL);
129 fprintf(stderr, "%s: line %d: %s before %s\n",
130 fname, lineno, ldap_scherr2str(code), err);
134 if (cr->cr_oid == NULL) {
136 "%s: line %d: objectclass has no OID\n",
141 code = cr_add(cr, 0, &err);
143 fprintf(stderr, "%s: line %d: %s: \"%s\"\n",
144 fname, lineno, ldap_scherr2str(code), err);
152 static int dsaschema_read_config(const char *fname, int depth)
155 char *line, *savefname, *saveline;
156 int savelineno, lineno;
160 cargv = calloc(ARGS_STEP + 1, sizeof(*cargv));
164 cargv_size = ARGS_STEP + 1;
167 fp = fopen(fname, "r");
169 fprintf(stderr, "could not open config file \"%s\": %s (%d)\n",
170 fname, strerror(errno), errno);
173 fp_getline_init(&lineno);
175 while ((line = fp_getline(fp, &lineno)) != NULL) {
176 /* skip comments and blank lines */
177 if (line[0] == '#' || line[0] == '\0') {
181 saveline = strdup(line);
182 if (saveline == NULL) {
186 if (fp_parse_line(lineno, line) != 0) {
194 if (strcasecmp(cargv[0], "attributetype") == 0 ||
195 strcasecmp(cargv[0], "attribute") == 0) {
197 fprintf(stderr, "%s: line %d: illegal attribute type format\n",
200 } else if (*cargv[1] == '(' /*')'*/) {
203 p = strchr(saveline, '(' /*')'*/);
204 rc = dsaschema_parse_at(fname, lineno, p, cargv);
208 fprintf(stderr, "%s: line %d: old attribute type format not supported\n",
211 } else if (strcasecmp(cargv[0], "ditcontentrule") == 0) {
213 p = strchr(saveline, '(' /*')'*/);
214 rc = dsaschema_parse_cr(fname, lineno, p, cargv);
217 } else if (strcasecmp(cargv[0], "objectclass") == 0) {
219 fprintf(stderr, "%s: line %d: illegal objectclass format\n",
222 } else if (*cargv[1] == '(' /*')'*/) {
225 p = strchr(saveline, '(' /*')'*/);
226 rc = dsaschema_parse_oc(fname, lineno, p, cargv);
230 fprintf(stderr, "%s: line %d: object class format not supported\n",
233 } else if (strcasecmp(cargv[0], "include") == 0) {
235 fprintf(stderr, "%s: line %d: missing file name in \"include <filename>\" line",
239 savefname = strdup(cargv[1]);
240 if (savefname == NULL) {
243 if (dsaschema_read_config(savefname, depth + 1) != 0) {
247 lineno = savelineno - 1;
249 fprintf(stderr, "%s: line %d: unknown directive \"%s\" (ignored)\n",
250 fname, lineno, cargv[0]);
262 int init_module(int argc, char *argv[])
267 for (i = 0; i < argc; i++) {
268 rc = dsaschema_read_config(argv[i], 0);
287 token = strtok_quote( line, " \t" );
289 if ( strtok_quote_ptr ) {
290 *strtok_quote_ptr = ' ';
293 if ( strtok_quote_ptr ) {
294 *strtok_quote_ptr = '\0';
297 for ( ; token != NULL; token = strtok_quote( NULL, " \t" ) ) {
298 if ( cargc == cargv_size - 1 ) {
300 tmp = realloc( cargv, (cargv_size + ARGS_STEP) *
306 cargv_size += ARGS_STEP;
308 cargv[cargc++] = token;
315 strtok_quote( char *line, char *sep )
321 strtok_quote_ptr = NULL;
322 if ( line != NULL ) {
325 while ( *next && strchr( sep, *next ) ) {
329 if ( *next == '\0' ) {
335 for ( inquote = 0; *next; ) {
343 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
349 next + 1, strlen( next + 1 ) + 1 );
350 next++; /* dont parse the escaped character */
355 if ( strchr( sep, *next ) != NULL ) {
356 strtok_quote_ptr = next;
369 static char buf[BUFSIZ];
371 static size_t lmax, lcur;
373 #define CATLINE( buf ) \
375 size_t len = strlen( buf ); \
376 while ( lcur + len + 1 > lmax ) { \
378 line = (char *) realloc( line, lmax ); \
380 strcpy( line + lcur, buf ); \
385 fp_getline( FILE *fp, int *lineno )
393 /* hack attack - keeps us from having to keep a stack of bufs... */
394 if ( strncasecmp( line, "include", 7 ) == 0 ) {
399 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
400 /* trim off \r\n or \n */
401 if ( (p = strchr( buf, '\n' )) != NULL ) {
402 if( p > buf && p[-1] == '\r' ) --p;
406 /* trim off trailing \ and append the next line */
407 if ( line[ 0 ] != '\0'
408 && (p = line + strlen( line ) - 1)[ 0 ] == '\\'
409 && p[ -1 ] != '\\' ) {
414 if ( ! isspace( (unsigned char) buf[0] ) ) {
418 /* change leading whitespace to a space */
427 return( line[0] ? line : NULL );
431 fp_getline_init( int *lineno )