]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/constraint.c
c86c11a53a2e141698e1d8c0a5666c747696674e
[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           "EQUALITY caseIgnoreMatch "
68       "SYNTAX OMsDirectoryString )", NULL, NULL },
69     { NULL, NULL, 0, 0, 0, ARG_IGNORED }
70 };
71
72 static ConfigOCs constraintocs[] = {
73     { "( OLcfgOvOc:13.1 "
74       "NAME 'olcConstraintConfig' "
75       "DESC 'Constraint overlay configuration' "
76       "SUP olcOverlayConfig "
77       "MAY ( olcConstraintAttribute ) )",
78       Cft_Overlay, constraintcfg },
79     { NULL, 0, NULL }
80 };
81
82 static int
83 constraint_cf_gen( ConfigArgs *c )
84 {
85     slap_overinst *on = (slap_overinst *)(c->bi);
86     constraint *cn = on->on_bi.bi_private, *cp;
87     struct berval bv;
88     int i, rc = 0;
89     constraint ap = { NULL, NULL, NULL  }, *a2 = NULL;
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 = &cn;
144                             (cp) && (i<c->valx);
145                             i++, cpp = &cp->ap_next, cp = *cpp);
146
147                         if (cp) {
148                                 /* zap cp, and join cpp to cp->ap_next */
149                             *cpp = 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                         }
157                         on->on_bi.bi_private = cn;
158                     }
159                     
160                     break;
161                 default:
162                     abort();
163                     break;
164             }
165             break;
166         case SLAP_CONFIG_ADD:
167         case LDAP_MOD_ADD:
168             switch (c->type) {
169                 case CONSTRAINT_ATTRIBUTE:
170                     if ( slap_str2ad( c->argv[1], &ap.ap, &text ) ) {
171                                                 snprintf( c->msg, sizeof( c->msg ),
172                                                         "%s <%s>: %s\n", c->argv[0], c->argv[1], text );
173                         Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
174                                "%s: %s\n", c->log, c->msg, 0 );
175                         return( ARG_BAD_CONF );
176                     }
177
178                     if ( strcasecmp( c->argv[2], "regex" ) == 0) {
179                         int err;
180             
181                         ap.re = ch_malloc( sizeof(regex_t) );
182                         if ((err = regcomp( ap.re,
183                                             c->argv[3], REG_EXTENDED )) != 0) {
184                             char errmsg[1024];
185                             
186                             regerror( err, ap.re, errmsg, sizeof(errmsg) );
187                             ch_free(ap.re);
188                                                         snprintf( c->msg, sizeof( c->msg ),
189                                    "%s %s: Illegal regular expression \"%s\": Error %s",
190                                    c->argv[0], c->argv[1], c->argv[3], errmsg);
191                             Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
192                                                                         "%s: %s\n", c->log, c->msg, 0 );
193                             ap.re = NULL;
194                             return( ARG_BAD_CONF );
195                         }
196                         ap.re_str = ch_strdup( c->argv[3] );
197                     } else {
198                                                 snprintf( c->msg, sizeof( c->msg ),
199                                "%s %s: Unknown constraint type: %s",
200                                c->argv[0], c->argv[1], c->argv[2] );
201                         Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
202                                "%s: %s\n", c->log, c->msg, 0 );
203                         return ( ARG_BAD_CONF );
204                     }
205                     
206
207                     a2 = ch_malloc( sizeof(constraint) );
208                     a2->ap_next = on->on_bi.bi_private;
209                     a2->ap = ap.ap;
210                     a2->re = ap.re;
211                     a2->re_str = ap.re_str;
212                     on->on_bi.bi_private = a2;
213                     break;
214                 default:
215                     abort();
216                     break;
217             }
218             break;
219         default:
220             abort();
221     }
222
223     return rc;
224 }
225
226 static int
227 constraint_violation( constraint *c, struct berval *bv )
228 {
229     if ((!c) || (!bv)) return 0;
230     
231     if ((c->re) &&
232         (regexec(c->re, bv->bv_val, 0, NULL, 0) == REG_NOMATCH))
233         
234         return 1; /* regular expression violation */
235     
236     return 0;
237 }
238
239 static char *
240 print_message( const char *fmt, AttributeDescription *a )
241 {
242     char *ret;
243     int sz;
244     
245     sz = strlen(fmt) + a->ad_cname.bv_len + 1;
246     ret = ch_malloc(sz);
247     snprintf( ret, sz, fmt, a->ad_cname.bv_val );
248     return ret;
249 }
250
251 static int
252 constraint_add( Operation *op, SlapReply *rs )
253 {
254     slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
255     Attribute *a;
256     constraint *c = on->on_bi.bi_private, *cp;
257     BerVarray b = NULL;
258     int i;
259     const char *rsv = "add breaks regular expression constraint on %s";
260     char *msg;
261     
262     if ((a = op->ora_e->e_attrs) == NULL) {
263         op->o_bd->bd_info = (BackendInfo *)(on->on_info);
264         send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
265                         "constraint_add() got null op.ora_e.e_attrs");
266         return(rs->sr_err);
267     }
268
269     for(; a; a = a->a_next ) {
270             /* we don't constrain operational attributes */
271     
272         if (is_at_operational(a->a_desc->ad_type)) continue;
273         
274         for(cp = c; cp; cp = cp->ap_next) {
275             if (cp->ap != a->a_desc) continue;
276             if ((b = a->a_vals) == NULL) continue;
277                 
278             for(i=0; b[i].bv_val; i++) {
279                 int cv = constraint_violation( cp, &b[i]);
280                     
281                 if (cv) {
282                         /* regex violation */
283                     op->o_bd->bd_info = (BackendInfo *)(on->on_info);
284                     msg = print_message( rsv, a->a_desc );
285                     send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION, msg );
286                     ch_free(msg);
287                     return (rs->sr_err);
288                 }
289             }
290         }
291     }
292         /* Default is to just fall through to the normal processing */
293     return SLAP_CB_CONTINUE;
294 }
295
296 static int
297 constraint_modify( Operation *op, SlapReply *rs )
298 {
299     slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
300     constraint *c = on->on_bi.bi_private, *cp;
301     Modifications *m;
302     BerVarray b = NULL;
303     int i;
304     const char *rsv = "modify breaks regular expression constraint on %s";
305     char *msg;
306     
307     if ((m = op->orm_modlist) == NULL) {
308         op->o_bd->bd_info = (BackendInfo *)(on->on_info);
309         send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
310                         "constraint_modify() got null orm_modlist");
311         return(rs->sr_err);
312     }
313
314     for(;m; m = m->sml_next) {
315         if (is_at_operational( m->sml_desc->ad_type )) continue;
316         if ((( m->sml_op & LDAP_MOD_OP ) != LDAP_MOD_ADD) &&
317             (( m->sml_op & LDAP_MOD_OP ) != LDAP_MOD_REPLACE))
318             continue;
319             /* we only care about ADD and REPLACE modifications */
320         if ((( b = m->sml_values ) == NULL ) || (b[0].bv_val == NULL))
321             continue;
322
323         for(cp = c; cp; cp = cp->ap_next) {
324             if (cp->ap != m->sml_desc) continue;
325             
326             for(i=0; b[i].bv_val; i++) {
327                 int cv = constraint_violation( cp, &b[i]);
328                 
329                 if (cv) {
330                         /* regex violation */
331                     op->o_bd->bd_info = (BackendInfo *)(on->on_info);
332                     msg = print_message( rsv, m->sml_desc );
333                     send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION, msg );
334                     ch_free(msg);
335                     return (rs->sr_err);
336                 }
337             }
338         }
339     }
340     
341     return SLAP_CB_CONTINUE;
342 }
343
344 static int
345 constraint_close(
346     BackendDB *be
347     )
348 {
349     slap_overinst *on = (slap_overinst *) be->bd_info;
350     constraint *ap, *a2;
351
352     for ( ap = on->on_bi.bi_private; ap; ap = a2 ) {
353         a2 = ap->ap_next;
354         if (ap->re_str) ch_free(ap->re_str);
355         if (ap->re) {
356             regfree( ap->re );
357             ch_free( ap->re );
358         }
359         
360         ch_free( ap );
361     }
362
363     return 0;
364 }
365
366 static slap_overinst constraint_ovl;
367
368 /* This overlay is set up for dynamic loading via moduleload. For static
369  * configuration, you'll need to arrange for the slap_overinst to be
370  * initialized and registered by some other function inside slapd.
371  */
372
373 #if SLAPD_OVER_CONSTRAINT == SLAPD_MOD_DYNAMIC
374 static
375 #endif
376 int
377 constraint_initialize( void ) {
378     int rc;
379
380     constraint_ovl.on_bi.bi_type = "constraint";
381     constraint_ovl.on_bi.bi_db_close = constraint_close;
382     constraint_ovl.on_bi.bi_op_add = constraint_add;
383     constraint_ovl.on_bi.bi_op_modify = constraint_modify;
384
385     constraint_ovl.on_bi.bi_private = NULL;
386     
387     constraint_ovl.on_bi.bi_cf_ocs = constraintocs;
388     rc = config_register_schema( constraintcfg, constraintocs );
389     if (rc) return rc;
390     
391     return overlay_register( &constraint_ovl );
392 }
393
394 #if SLAPD_OVER_CONSTRAINT == SLAPD_MOD_DYNAMIC
395 int init_module(int argc, char *argv[]) {
396     return constraint_initialize();
397 }
398 #endif
399
400 #endif /* defined(SLAPD_OVER_CONSTRAINT) */
401