1 /* acl.c - routines to parse and check acl's */
8 #include <sys/socket.h>
9 #include <netinet/in.h>
17 extern Filter *str2filter();
18 extern struct acl *global_acl;
19 extern char **str2charray();
20 extern char *dn_upcase();
23 static void acl_append();
24 static void access_append();
25 static void acl_usage();
27 static void print_acl();
28 static void print_access();
32 regtest(char *fname, int lineno, char *pat) {
48 for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
50 if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
67 if ( size >= (sizeof(buf)-1) ) {
69 "%s: line %d: regular expression \"%s\" too large\n",
70 fname, lineno, pat, 0 );
74 if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
76 regerror(e, &re, error, sizeof(error));
78 "%s: line %d: regular expression \"%s\" bad because of %s\n",
79 fname, lineno, pat, error );
97 char *e, *left, *right;
102 for ( i = 1; i < argc; i++ ) {
103 /* to clause - select which entries are protected */
104 if ( strcasecmp( argv[i], "to" ) == 0 ) {
107 "%s: line %d: only one to clause allowed in access line\n",
111 a = (struct acl *) ch_calloc( 1, sizeof(struct acl) );
112 for ( ++i; i < argc; i++ ) {
113 if ( strcasecmp( argv[i], "by" ) == 0 ) {
118 if ( strcasecmp( argv[i], "*" ) == 0 ) {
120 if ((e = regcomp( &a->acl_dnre, ".*",
121 REG_EXTENDED|REG_ICASE)))
124 regerror(e, &a->acl_dnre, buf, sizeof(buf));
126 "%s: line %d: regular expression \"%s\" bad because of %s\n",
127 fname, lineno, right, buf );
130 a->acl_dnpat = strdup( ".*" );
134 split( argv[i], '=', &left, &right );
135 if ( right == NULL || *right == '\0' ) {
137 "%s: line %d: missing \"=\" in (or value after) \"%s\" in to clause\n",
138 fname, lineno, left );
142 if ( strcasecmp( left, "filter" ) == 0 ) {
143 if ( (a->acl_filter = str2filter(
146 "%s: line %d: bad filter \"%s\" in to clause\n",
147 fname, lineno, right );
150 } else if ( strcasecmp( left, "dn" ) == 0 ) {
152 if ((e = regcomp(&a->acl_dnre, right,
153 REG_EXTENDED|REG_ICASE))) {
155 regerror(e, &a->acl_dnre, buf, sizeof(buf));
157 "%s: line %d: regular expression \"%s\" bad because of %s\n",
158 fname, lineno, right, buf );
162 a->acl_dnpat = dn_upcase(strdup( right ));
164 } else if ( strncasecmp( left, "attr", 4 )
168 alist = str2charray( right, "," );
169 charray_merge( &a->acl_attrs, alist );
173 "%s: line %d: expecting <what> got \"%s\"\n",
174 fname, lineno, left );
179 /* by clause - select who has what access to entries */
180 } else if ( strcasecmp( argv[i], "by" ) == 0 ) {
183 "%s: line %d: to clause required before by clause in access line\n",
188 * by clause consists of <who> and <access>
191 b = (struct access *) ch_calloc( 1,
192 sizeof(struct access) );
196 "%s: line %d: premature eol: expecting <who>\n",
202 split( argv[i], '=', &left, &right );
203 if ( strcasecmp( argv[i], "*" ) == 0 ) {
204 b->a_dnpat = strdup( ".*" );
205 } else if ( strcasecmp( argv[i], "self" ) == 0 ) {
206 b->a_dnpat = strdup( "self" );
207 } else if ( strcasecmp( left, "dn" ) == 0 ) {
208 regtest(fname, lineno, right);
209 b->a_dnpat = dn_upcase( strdup( right ) );
210 } else if ( strcasecmp( left, "dnattr" ) == 0 ) {
211 b->a_dnattr = strdup( right );
214 } else if ( strcasecmp( left, "group" ) == 0 ) {
215 regtest(fname, lineno, right);
216 b->a_group = dn_upcase(strdup( right ));
217 #endif /* ACLGROUP */
218 } else if ( strcasecmp( left, "domain" ) == 0 ) {
220 regtest(fname, lineno, right);
221 b->a_domainpat = strdup( right );
223 /* normalize the domain */
224 for ( s = b->a_domainpat; *s; s++ ) {
227 } else if ( strcasecmp( left, "addr" ) == 0 ) {
228 regtest(fname, lineno, right);
229 b->a_addrpat = strdup( right );
232 "%s: line %d: expecting <who> got \"%s\"\n",
233 fname, lineno, left );
239 "%s: line %d: premature eol: expecting <access>\n",
245 split( argv[i], '=', &left, &right );
246 if ( (b->a_access = str2access( left )) == -1 ) {
248 "%s: line %d: expecting <access> got \"%s\"\n",
249 fname, lineno, left );
252 access_append( &a->acl_access, b );
256 "%s: line %d: expecting \"to\" or \"by\" got \"%s\"\n",
257 fname, lineno, argv[i] );
262 /* if we have no real access clause, complain and do nothing */
265 "%s: line %d: warning: no access clause(s) specified in access line\n",
270 if ( a->acl_access == NULL ) {
272 "%s: line %d: warning: no by clause(s) specified in access line\n",
277 acl_append( &be->be_acl, a );
279 acl_append( &global_acl, a );
285 access2str( int access )
289 if ( access & ACL_SELF ) {
290 strcpy( buf, "self" );
295 if ( access & ACL_NONE ) {
296 strcat( buf, "none" );
297 } else if ( access & ACL_COMPARE ) {
298 strcat( buf, "compare" );
299 } else if ( access & ACL_SEARCH ) {
300 strcat( buf, "search" );
301 } else if ( access & ACL_READ ) {
302 strcat( buf, "read" );
303 } else if ( access & ACL_WRITE ) {
304 strcat( buf, "write" );
306 strcat( buf, "unknown" );
313 str2access( char *str )
318 if ( strncasecmp( str, "self", 4 ) == 0 ) {
323 if ( strcasecmp( str, "none" ) == 0 ) {
325 } else if ( strcasecmp( str, "compare" ) == 0 ) {
326 access |= ACL_COMPARE;
327 } else if ( strcasecmp( str, "search" ) == 0 ) {
328 access |= ACL_SEARCH;
329 } else if ( strcasecmp( str, "read" ) == 0 ) {
331 } else if ( strcasecmp( str, "write" ) == 0 ) {
343 fprintf( stderr, "\n<access clause> ::= access to <what> [ by <who> <access> ]+ \n" );
344 fprintf( stderr, "<what> ::= * | [dn=<regex>] [filter=<ldapfilter>] [attrs=<attrlist>]\n" );
345 fprintf( stderr, "<attrlist> ::= <attr> | <attr> , <attrlist>\n" );
346 fprintf( stderr, "<attr> ::= <attrname> | entry | children\n" );
347 fprintf( stderr, "<who> ::= * | self | dn=<regex> | addr=<regex> |\n\tdomain=<regex> | dnattr=<dnattrname>\n" );
348 fprintf( stderr, "<access> ::= [self]{none | compare | search | read | write }\n" );
361 if ( (*right = strchr( line, splitchar )) != NULL ) {
362 *((*right)++) = '\0';
367 access_append( struct access **l, struct access *a )
369 for ( ; *l != NULL; l = &(*l)->a_next )
376 acl_append( struct acl **l, struct acl *a )
378 for ( ; *l != NULL; l = &(*l)->acl_next )
387 print_access( struct access *b )
390 if ( b->a_dnpat != NULL ) {
391 printf( " dn=%s", b->a_dnpat );
392 } else if ( b->a_addrpat != NULL ) {
393 printf( " addr=%s", b->a_addrpat );
394 } else if ( b->a_domainpat != NULL ) {
395 printf( " domain=%s", b->a_domainpat );
396 } else if ( b->a_dnattr != NULL ) {
397 printf( " dnattr=%s", b->a_dnattr );
399 printf( " %s\n", access2str( b->a_access ) );
403 print_acl( struct acl *a )
411 printf( "access to" );
412 if ( a->acl_filter != NULL ) {
413 printf( " filter=" );
414 filter_print( a->acl_filter );
416 if ( a->acl_dnpat != NULL ) {
418 printf( a->acl_dnpat );
420 if ( a->acl_attrs != NULL ) {
424 for ( i = 0; a->acl_attrs[i] != NULL; i++ ) {
428 printf( a->acl_attrs[i] );
433 for ( b = a->acl_access; b != NULL; b = b->a_next ) {
438 #endif /* LDAP_DEBUG */