]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/constraint.c
s/HPcfgOv/OLcfgOv/
[openldap] / servers / slapd / overlays / constraint.c
1 /* constraint.c - Overlay to constrain attributes to certain values */
2 /* 
3  *
4  * Copyright 2003-2004 Hewlett-Packard Company
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /*
16  * Author: Neil Dunbar <neil.dunbar@hp.com>
17  */
18 #include "portable.h"
19
20 #ifdef SLAPD_OVER_CONSTRAINT
21
22 #include <stdio.h>
23
24 #include <ac/string.h>
25 #include <ac/socket.h>
26 #include <ac/regex.h>
27
28 #include "slap.h"
29 #include "config.h"
30
31 /*
32  * This overlay limits the values which can be placed into an
33  * attribute, over and above the limits placed by the schema.
34  *
35  * It traps only LDAP adds and modify commands (and only seeks to
36  * control the add and modify value mods of a modify)
37  */
38
39 #define REGEX_STR "regex"
40
41 /*
42  * Linked list of attribute constraints which we should enforce.
43  * This is probably a sub optimal structure - some form of sorted
44  * array would be better if the number of attributes contrained is
45  * likely to be much bigger than 4 or 5. We stick with a list for
46  * the moment.
47  */
48
49 typedef struct constraint {
50     struct constraint *ap_next;
51     AttributeDescription *ap;
52     regex_t *re;
53     char *re_str; /* string representation of regex */
54 } constraint;
55
56 enum {
57     CONSTRAINT_ATTRIBUTE = 1
58 };
59
60 static ConfigDriver constraint_cf_gen;
61
62 static ConfigTable constraintcfg[] = {
63     { "constraint_attribute", "attribute regex <regular expression>",
64       4, 4, 0, ARG_MAGIC | CONSTRAINT_ATTRIBUTE, constraint_cf_gen,
65       "( OLcfgOvAt:13.1 NAME 'olcConstraintAttribute' "
66       "DESC 'regular expression constraint for attribute' "
67       "SYNTAX OMsDirectoryString )", NULL, NULL },
68     { NULL, NULL, 0, 0, 0, ARG_IGNORED }
69 };
70
71 static ConfigOCs constraintocs[] = {
72     { "( OLcfgOvOc:13.1 "
73       "NAME 'olcConstraintConfig' "
74       "DESC 'Constraint overlay configuration' "
75       "SUP olcOverlayConfig "
76       "MAY ( olcConstraintAttribute ) )",
77       Cft_Overlay, constraintcfg },
78     { NULL, 0, NULL }
79 };
80
81 static int
82 constraint_cf_gen( ConfigArgs *c )
83 {
84     slap_overinst *on = (slap_overinst *)(c->bi);
85     constraint *cn = on->on_bi.bi_private, *cp;
86     struct berval bv;
87     int i, rc = 0;
88     constraint ap = { NULL, NULL, NULL  }, *a2 = NULL;
89     regmatch_t rm[2];
90     const char *text = NULL;
91     
92     switch ( c->op ) {
93         case SLAP_CONFIG_EMIT:
94             switch (c->type) {
95                 case CONSTRAINT_ATTRIBUTE:
96                     for (cp=cn; cp; cp=cp->ap_next) {
97                         int len;
98                         char *s;
99                         
100                         len = cp->ap->ad_cname.bv_len +
101                             strlen( REGEX_STR ) + strlen( cp->re_str) + 3;
102                         s = ch_malloc(len);
103                         if (!s) continue;
104                         snprintf(s, len, "%s %s %s", cp->ap->ad_cname.bv_val,
105                                  REGEX_STR, cp->re_str);
106                         bv.bv_val = s;
107                         bv.bv_len = strlen(s);
108                         rc = value_add_one( &c->rvalue_vals, &bv );
109                         if (rc) return rc;
110                         rc = value_add_one( &c->rvalue_nvals, &bv );
111                         if (rc) return rc;
112                         ch_free(s);
113                     }
114                     break;
115                 default:
116                     abort();
117                     break;
118             }
119             break;
120         case LDAP_MOD_DELETE:
121             switch (c->type) {
122                 case CONSTRAINT_ATTRIBUTE:
123                     if (!cn) break; /* nothing to do */
124                     
125                     if (c->valx < 0) {
126                             /* zap all constraints */
127                         while (cn) {
128                             cp = cn->ap_next;
129                             if (cn->re) {
130                                 regfree(cn->re);
131                                 ch_free(cn->re);
132                             }
133                             if (cn->re_str) ch_free(cn->re_str);
134                             ch_free(cn);
135                             cn = cp;
136                         }
137                         
138                         on->on_bi.bi_private = NULL;
139                     } else {
140                         constraint *cpp;
141                         
142                             /* zap constraint numbered 'valx' */
143                         for(i=0, cp = cn, cpp = NULL;
144                             (cp) && (i<c->valx);
145                             i++, cpp = cp, cp=cp->ap_next);
146
147                         if (cpp) {
148                                 /* zap cp, and join cpp to cp->ap_next */
149                             cpp->ap_next = cp->ap_next;
150                             if (cp->re) {
151                                 regfree(cp->re);
152                                 ch_free(cp->re);
153                             }
154                             if (cp->re_str) ch_free(cp->re_str);
155                             ch_free(cp);
156                         } else {
157                                 /* zap the list head */
158                             if (cn->re) {
159                                 regfree(cn->re);
160                                 ch_free(cn->re);
161                             }
162                             if (cn->re_str) ch_free(cn->re_str);
163                             ch_free(cn);
164                             on->on_bi.bi_private = cn->ap_next;
165                         }
166                     }
167                     
168                     break;
169                 default:
170                     abort();
171                     break;
172             }
173             break;
174         case SLAP_CONFIG_ADD:
175         case LDAP_MOD_ADD:
176             switch (c->type) {
177                 case CONSTRAINT_ATTRIBUTE:
178                     if ( slap_str2ad( c->argv[1], &ap.ap, &text ) ) {
179                         Debug( LDAP_DEBUG_CONFIG,
180                                "constraint_add: <%s>: attribute description unknown %s.\n",
181                                c->argv[1], text, 0 );
182                         return( ARG_BAD_CONF );
183                     }
184
185                     if ( strcasecmp( c->argv[2], "regex" ) == 0) {
186                         int err;
187             
188                         ap.re = ch_malloc( sizeof(regex_t) );
189                         if ((err = regcomp( ap.re,
190                                             c->argv[3], REG_EXTENDED )) != 0) {
191                             char errmsg[1024];
192                             
193                             regerror( err, ap.re, errmsg, sizeof(errmsg) );
194                             ch_free(ap.re);
195                             Debug( LDAP_DEBUG_CONFIG,
196                                    "%s: Illegal regular expression \"%s\": Error %s\n",
197                                    c->argv[1], c->argv[3], errmsg);
198                             ap.re = NULL;
199                             return( ARG_BAD_CONF );
200                         }
201                         ap.re_str = ch_strdup( c->argv[3] );
202                     } else {
203                         Debug( LDAP_DEBUG_CONFIG,
204                                "%s: Unknown constraint type: %s\n",
205                                c->argv[1], c->argv[2], 0 );
206                         return ( ARG_BAD_CONF );
207                     }
208                     
209
210                     a2 = ch_malloc( sizeof(constraint) );
211                     a2->ap_next = on->on_bi.bi_private;
212                     a2->ap = ap.ap;
213                     a2->re = ap.re;
214                     a2->re_str = ap.re_str;
215                     on->on_bi.bi_private = a2;
216                     break;
217                 default:
218                     abort();
219                     break;
220             }
221             break;
222         default:
223             abort();
224     }
225
226     return rc;
227 }
228
229 static int
230 constraint_violation( constraint *c, struct berval *bv )
231 {
232     if ((!c) || (!bv)) return 0;
233     
234     if ((c->re) &&
235         (regexec(c->re, bv->bv_val, 0, NULL, 0) == REG_NOMATCH))
236         
237         return 1; /* regular expression violation */
238     
239     return 0;
240 }
241
242 static char *
243 print_message( const char *fmt, AttributeDescription *a )
244 {
245     char *ret;
246     int sz;
247     
248     sz = strlen(fmt) + a->ad_cname.bv_len + 1;
249     ret = ch_malloc(sz);
250     snprintf( ret, sz, fmt, a->ad_cname.bv_val );
251     return ret;
252 }
253
254 static int
255 constraint_add( Operation *op, SlapReply *rs )
256 {
257     slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
258     Attribute *a;
259     constraint *c = on->on_bi.bi_private, *cp;
260     BerVarray b = NULL;
261     int i;
262     const char *rsv = "add breaks regular expression constraint on %s";
263     char *msg;
264     
265     if ((a = op->ora_e->e_attrs) == NULL) {
266         op->o_bd->bd_info = (BackendInfo *)(on->on_info);
267         send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
268                         "constraint_add() got null op.ora_e.e_attrs");
269         return(rs->sr_err);
270     }
271
272     for(; a; a = a->a_next ) {
273             /* we don't constrain operational attributes */
274     
275         if (is_at_operational(a->a_desc->ad_type)) continue;
276         
277         for(cp = c; cp; cp = cp->ap_next) {
278             if (cp->ap != a->a_desc) continue;
279             if ((b = a->a_vals) == NULL) continue;
280                 
281             for(i=0; b[i].bv_val; i++) {
282                 int cv = constraint_violation( cp, &b[i]);
283                     
284                 if (cv) {
285                         /* regex violation */
286                     op->o_bd->bd_info = (BackendInfo *)(on->on_info);
287                     msg = print_message( rsv, a->a_desc );
288                     send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION, msg );
289                     ch_free(msg);
290                     return (rs->sr_err);
291                 }
292             }
293         }
294     }
295         /* Default is to just fall through to the normal processing */
296     return SLAP_CB_CONTINUE;
297 }
298
299 static int
300 constraint_modify( Operation *op, SlapReply *rs )
301 {
302     slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
303     constraint *c = on->on_bi.bi_private, *cp;
304     Modifications *m;
305     BerVarray b = NULL;
306     int i;
307     const char *rsv = "modify breaks regular expression constraint on %s";
308     char *msg;
309     
310     if ((m = op->orm_modlist) == NULL) {
311         op->o_bd->bd_info = (BackendInfo *)(on->on_info);
312         send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
313                         "constraint_modify() got null orm_modlist");
314         return(rs->sr_err);
315     }
316
317     for(;m; m = m->sml_next) {
318         if (is_at_operational( m->sml_desc->ad_type )) continue;
319         if ((( m->sml_op & LDAP_MOD_OP ) != LDAP_MOD_ADD) &&
320             (( m->sml_op & LDAP_MOD_OP ) != LDAP_MOD_REPLACE))
321             continue;
322             /* we only care about ADD and REPLACE modifications */
323         if ((( b = m->sml_values ) == NULL ) || (b[0].bv_val == NULL))
324             continue;
325
326         for(cp = c; cp; cp = cp->ap_next) {
327             if (cp->ap != m->sml_desc) continue;
328             
329             for(i=0; b[i].bv_val; i++) {
330                 int cv = constraint_violation( cp, &b[i]);
331                 
332                 if (cv) {
333                         /* regex violation */
334                     op->o_bd->bd_info = (BackendInfo *)(on->on_info);
335                     msg = print_message( rsv, m->sml_desc );
336                     send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION, msg );
337                     ch_free(msg);
338                     return (rs->sr_err);
339                 }
340             }
341         }
342     }
343     
344     return SLAP_CB_CONTINUE;
345 }
346
347 static int
348 constraint_close(
349     BackendDB *be
350     )
351 {
352     slap_overinst *on = (slap_overinst *) be->bd_info;
353     constraint *ap, *a2;
354
355     for ( ap = on->on_bi.bi_private; ap; ap = a2 ) {
356         a2 = ap->ap_next;
357         if (ap->re_str) ch_free(ap->re_str);
358         if (ap->re) {
359             regfree( ap->re );
360             ch_free( ap->re );
361         }
362         
363         ch_free( ap );
364     }
365
366     return 0;
367 }
368
369 static slap_overinst constraint_ovl;
370
371 /* This overlay is set up for dynamic loading via moduleload. For static
372  * configuration, you'll need to arrange for the slap_overinst to be
373  * initialized and registered by some other function inside slapd.
374  */
375
376 #if SLAPD_OVER_CONSTRAINT == SLAPD_MOD_DYNAMIC
377 static
378 #endif
379 int
380 constraint_initialize( void ) {
381     int rc;
382
383     constraint_ovl.on_bi.bi_type = "constraint";
384     constraint_ovl.on_bi.bi_db_close = constraint_close;
385     constraint_ovl.on_bi.bi_op_add = constraint_add;
386     constraint_ovl.on_bi.bi_op_modify = constraint_modify;
387
388     constraint_ovl.on_bi.bi_private = NULL;
389     
390     constraint_ovl.on_bi.bi_cf_ocs = constraintocs;
391     rc = config_register_schema( constraintcfg, constraintocs );
392     if (rc) return rc;
393     
394     return overlay_register( &constraint_ovl );
395 }
396
397 #if SLAPD_OVER_CONSTRAINT == SLAPD_MOD_DYNAMIC
398 int init_module(int argc, char *argv[]) {
399     return constraint_initialize();
400 }
401 #endif
402
403 #endif /* defined(SLAPD_OVER_CONSTRAINT) */
404