]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/constraint.c
Sync with HEAD
[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
30 /*
31  * This overlay limits the values which can be placed into an
32  * attribute, over and above the limits placed by the schema.
33  *
34  * It traps only LDAP adds and modify commands (and only seeks to
35  * control the add and modify value mods of a modify)
36  */
37
38 /*
39  * Linked list of attribute constraints which we should enforce.
40  * This is probably a sub optimal structure - some form of sorted
41  * array would be better if the number of attributes contrained is
42  * likely to be much bigger than 4 or 5. We stick with a list for
43  * the moment.
44  */
45 typedef struct constraint {
46     struct constraint *ap_next;
47     AttributeDescription *ap;
48     regex_t *re;
49 } constraint;
50
51 static int
52 constraint_violation( constraint *c, struct berval *bv )
53 {
54     if ((!c) || (!bv)) return 0;
55     
56     if ((c->re) &&
57         (regexec(c->re, bv->bv_val, 0, NULL, 0) == REG_NOMATCH))
58         
59         return 1; /* regular expression violation */
60     
61     return 0;
62 }
63
64 static char *
65 print_message( const char *fmt, AttributeDescription *a )
66 {
67     char *ret;
68     int sz;
69     
70     sz = strlen(fmt) + a->ad_cname.bv_len + 1;
71     ret = ch_malloc(sz);
72     snprintf( ret, sz, fmt, a->ad_cname.bv_val );
73     return ret;
74 }
75
76 static int
77 constraint_add( Operation *op, SlapReply *rs )
78 {
79     slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
80     Attribute *a;
81     constraint *c = on->on_bi.bi_private, *cp;
82     BerVarray b = NULL;
83     int i;
84     const char *rsv = "add breaks regular expression constraint on %s";
85     char *msg;
86     
87     if ((a = op->ora_e->e_attrs) == NULL) {
88         op->o_bd->bd_info = (BackendInfo *)(on->on_info);
89         send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
90                         "constraint_add() got null op.ora_e.e_attrs");
91         return(rs->sr_err);
92     }
93
94     for(; a; a = a->a_next ) {
95             /* we don't constrain operational attributes */
96     
97         if (is_at_operational(a->a_desc->ad_type)) continue;
98         
99         for(cp = c; cp; cp = cp->ap_next) {
100             if (cp->ap != a->a_desc) continue;
101             if ((b = a->a_vals) == NULL) continue;
102                 
103             for(i=0; b[i].bv_val; i++) {
104                 int cv = constraint_violation( cp, &b[i]);
105                     
106                 if (cv) {
107                         /* regex violation */
108                     op->o_bd->bd_info = (BackendInfo *)(on->on_info);
109                     msg = print_message( rsv, a->a_desc );
110                     send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION, msg );
111                     ch_free(msg);
112                     return (rs->sr_err);
113                 }
114             }
115         }
116     }
117         /* Default is to just fall through to the normal processing */
118     return SLAP_CB_CONTINUE;
119 }
120
121 static int
122 constraint_modify( Operation *op, SlapReply *rs )
123 {
124     slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
125     constraint *c = on->on_bi.bi_private, *cp;
126     Modifications *m;
127     BerVarray b = NULL;
128     int i;
129     const char *rsv = "modify breaks regular expression constraint on %s";
130     char *msg;
131     
132     if ((m = op->orm_modlist) == NULL) {
133         op->o_bd->bd_info = (BackendInfo *)(on->on_info);
134         send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
135                         "constraint_modify() got null orm_modlist");
136         return(rs->sr_err);
137     }
138
139     for(;m; m = m->sml_next) {
140         if (is_at_operational( m->sml_desc->ad_type )) continue;
141         if ((( m->sml_op & LDAP_MOD_OP ) != LDAP_MOD_ADD) &&
142             (( m->sml_op & LDAP_MOD_OP ) != LDAP_MOD_REPLACE))
143             continue;
144             /* we only care about ADD and REPLACE modifications */
145         if ((( b = m->sml_values ) == NULL ) || (b[0].bv_val == NULL))
146             continue;
147
148         for(cp = c; cp; cp = cp->ap_next) {
149             if (cp->ap != m->sml_desc) continue;
150             
151             for(i=0; b[i].bv_val; i++) {
152                 int cv = constraint_violation( cp, &b[i]);
153                 
154                 if (cv) {
155                         /* regex violation */
156                     op->o_bd->bd_info = (BackendInfo *)(on->on_info);
157                     msg = print_message( rsv, m->sml_desc );
158                     send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION, msg );
159                     ch_free(msg);
160                     return (rs->sr_err);
161                 }
162             }
163         }
164     }
165     
166     return SLAP_CB_CONTINUE;
167 }
168
169 static int constraint_config(
170     BackendDB   *be,
171     const char  *fname,
172     int         lineno,
173     int         argc,
174     char        **argv
175     )
176 {
177     slap_overinst *on = (slap_overinst *) be->bd_info;
178     constraint ap = { NULL, NULL, NULL  }, *a2 = NULL;
179     regmatch_t rm[2];
180     
181     if ( strcasecmp( argv[0], "constraint_attribute" ) == 0 ) {
182         const char *text;
183                 
184         if ( argc != 4 ) {
185             Debug( LDAP_DEBUG_ANY, "%s: line %d: "
186                    "wrong number of parameters in"
187                    "\"constraint_attribute <attribute> <constraint> <constraint_value>\" line.\n",
188                    fname, lineno, 0 );
189             return( 1 );
190         }
191         if ( slap_str2ad( argv[1], &ap.ap, &text ) ) {
192             Debug( LDAP_DEBUG_ANY, "%s: line %d: "
193                    "attribute description unknown \"constraint_attribute\" line: %s.\n",
194                    fname, lineno, text );
195             return( 1 );
196         }
197
198         if ( strcasecmp( argv[2], "regex" ) == 0) {
199             int err;
200             
201             ap.re = ch_malloc( sizeof(regex_t) );
202             if ((err = regcomp( ap.re, argv[3], REG_EXTENDED )) != 0) {
203                 const char *fmt = "%s: line %d: Illegal regular expression \"%s\": Error %s\n";
204                 char errmsg[1024], *msg;
205                 int i, l, msgsize;
206                 
207                 msgsize = regerror( err, ap.re, errmsg, sizeof(errmsg) );
208                 msgsize += strlen(fmt) + strlen(argv[3]) + strlen(fname);
209                 for(l=lineno; l>0; l/=10, msgsize++);
210                 msgsize++;
211
212                 msg = ch_malloc( msgsize + 1 );
213                 snprintf( msg, msgsize, fmt, fname, lineno, argv[3], errmsg );
214                 ch_free(ap.re);
215                 Debug( LDAP_DEBUG_ANY, msg, 0, 0, 0);
216                 ch_free(msg);
217                 ap.re = NULL;
218                 return(1);
219             }
220         } else
221             Debug( LDAP_DEBUG_ANY, "%s: line %d: "
222                    "Unknown constraint type: %s",
223                    fname, lineno, argv[2] );
224         
225
226         a2 = ch_malloc( sizeof(constraint) );
227         a2->ap_next = on->on_bi.bi_private;
228         a2->ap = ap.ap;
229         a2->re = ap.re;
230         on->on_bi.bi_private = a2;
231     } else {
232         return SLAP_CONF_UNKNOWN;
233     }
234     
235     return 0;
236 }
237
238 static int
239 constraint_close(
240     BackendDB *be
241     )
242 {
243     slap_overinst *on = (slap_overinst *) be->bd_info;
244     constraint *ap, *a2;
245
246     for ( ap = on->on_bi.bi_private; ap; ap = a2 ) {
247         a2 = ap->ap_next;
248         if (ap->re) {
249             regfree( ap->re );
250             ch_free( ap->re );
251         }
252         
253         ch_free( ap );
254     }
255
256     return 0;
257 }
258
259 static slap_overinst constraint_ovl;
260
261 /* This overlay is set up for dynamic loading via moduleload. For static
262  * configuration, you'll need to arrange for the slap_overinst to be
263  * initialized and registered by some other function inside slapd.
264  */
265
266 #if SLAPD_OVER_CONSTRAINT == SLAPD_MOD_DYNAMIC
267 static
268 #endif
269 int
270 constraint_initialize( void ) {
271     constraint_ovl.on_bi.bi_type = "constraint";
272     constraint_ovl.on_bi.bi_db_config = constraint_config;
273     constraint_ovl.on_bi.bi_db_close = constraint_close;
274     constraint_ovl.on_bi.bi_op_add = constraint_add;
275     constraint_ovl.on_bi.bi_op_modify = constraint_modify;
276
277     return overlay_register( &constraint_ovl );
278 }
279
280 #if SLAPD_OVER_CONSTRAINT == SLAPD_MOD_DYNAMIC
281 int init_module(int argc, char *argv[]) {
282     return constraint_initialize();
283 }
284 #endif
285
286 #endif /* defined(SLAPD_OVER_CONSTRAINT) */
287