]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/constraint.c
really check if filter is valid...(more on ITS#5581)
[openldap] / servers / slapd / overlays / constraint.c
1 /* $OpenLDAP$ */
2 /* constraint.c - Overlay to constrain attributes to certain values */
3 /* 
4  * Copyright 2003-2004 Hewlett-Packard Company
5  * Copyright 2007 Emmanuel Dreyfus
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /*
17  * Authors: Neil Dunbar <neil.dunbar@hp.com>
18  *                      Emmannuel Dreyfus <manu@netbsd.org>
19  */
20 #include "portable.h"
21
22 #ifdef SLAPD_OVER_CONSTRAINT
23
24 #include <stdio.h>
25
26 #include <ac/string.h>
27 #include <ac/socket.h>
28 #include <ac/regex.h>
29
30 #include "lutil.h"
31 #include "slap.h"
32 #include "config.h"
33
34 /*
35  * This overlay limits the values which can be placed into an
36  * attribute, over and above the limits placed by the schema.
37  *
38  * It traps only LDAP adds and modify commands (and only seeks to
39  * control the add and modify value mods of a modify)
40  */
41
42 #define REGEX_STR "regex"
43 #define URI_STR "uri"
44 #define SIZE_STR "size"
45 #define COUNT_STR "count"
46
47 /*
48  * Linked list of attribute constraints which we should enforce.
49  * This is probably a sub optimal structure - some form of sorted
50  * array would be better if the number of attributes contrained is
51  * likely to be much bigger than 4 or 5. We stick with a list for
52  * the moment.
53  */
54
55 typedef struct constraint {
56         struct constraint *ap_next;
57         AttributeDescription *ap;
58         regex_t *re;
59         LDAPURLDesc *lud;
60         size_t size;
61         size_t count;
62         AttributeDescription **attrs;
63         struct berval val; /* constraint value */
64         struct berval dn;
65         struct berval filter;
66 } constraint;
67
68 enum {
69         CONSTRAINT_ATTRIBUTE = 1
70 };
71
72 static ConfigDriver constraint_cf_gen;
73
74 static ConfigTable constraintcfg[] = {
75         { "constraint_attribute", "attribute> (regex|uri) <value",
76           4, 4, 0, ARG_MAGIC | CONSTRAINT_ATTRIBUTE, constraint_cf_gen,
77           "( OLcfgOvAt:13.1 NAME 'olcConstraintAttribute' "
78           "DESC 'regular expression constraint for attribute' "
79           "EQUALITY caseIgnoreMatch "
80           "SYNTAX OMsDirectoryString )", NULL, NULL },
81         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
82 };
83
84 static ConfigOCs constraintocs[] = {
85         { "( OLcfgOvOc:13.1 "
86           "NAME 'olcConstraintConfig' "
87           "DESC 'Constraint overlay configuration' "
88           "SUP olcOverlayConfig "
89           "MAY ( olcConstraintAttribute ) )",
90           Cft_Overlay, constraintcfg },
91         { NULL, 0, NULL }
92 };
93
94 static void
95 constraint_free( constraint *cp )
96 {
97         if (cp->re) {
98                 regfree(cp->re);
99                 ch_free(cp->re);
100         }
101         if (!BER_BVISNULL(&cp->val))
102                 ch_free(cp->val.bv_val);
103         if (cp->lud)
104                 ldap_free_urldesc(cp->lud);
105         if (cp->attrs)
106                 ch_free(cp->attrs);
107         ch_free(cp);
108 }
109
110 static int
111 constraint_cf_gen( ConfigArgs *c )
112 {
113         slap_overinst *on = (slap_overinst *)(c->bi);
114         constraint *cn = on->on_bi.bi_private, *cp;
115         struct berval bv;
116         int i, rc = 0;
117         constraint ap = { NULL, NULL, NULL      }, *a2 = NULL;
118         const char *text = NULL;
119         
120         switch ( c->op ) {
121         case SLAP_CONFIG_EMIT:
122                 switch (c->type) {
123                 case CONSTRAINT_ATTRIBUTE:
124                         for (cp=cn; cp; cp=cp->ap_next) {
125                                 int len;
126                                 char *s;
127                                 char *tstr = NULL;
128
129                                 len = cp->ap->ad_cname.bv_len + 3;
130                                 if (cp->re) {
131                                         len += STRLENOF(REGEX_STR);
132                                         tstr = REGEX_STR;
133                                 } else if (cp->lud) {
134                                         len += STRLENOF(URI_STR);
135                                         tstr = URI_STR;
136                                 } else if (cp->size) {
137                                         len += STRLENOF(SIZE_STR);
138                                         tstr = SIZE_STR;
139                                 } else if (cp->count) {
140                                         len += STRLENOF(COUNT_STR);
141                                         tstr = COUNT_STR;
142                                 }
143                                 len += cp->val.bv_len;
144
145                                 s = ch_malloc(len);
146
147                                 bv.bv_len = snprintf(s, len, "%s %s %s", cp->ap->ad_cname.bv_val,
148                                                  tstr, cp->val.bv_val);
149                                 bv.bv_val = s;
150                                 rc = value_add_one( &c->rvalue_vals, &bv );
151                                 if (rc) return rc;
152                                 rc = value_add_one( &c->rvalue_nvals, &bv );
153                                 if (rc) return rc;
154                                 ch_free(s);
155                         }
156                         break;
157                 default:
158                         abort();
159                         break;
160                 }
161                 break;
162         case LDAP_MOD_DELETE:
163                 switch (c->type) {
164                 case CONSTRAINT_ATTRIBUTE:
165                         if (!cn) break; /* nothing to do */
166                                         
167                         if (c->valx < 0) {
168                                 /* zap all constraints */
169                                 while (cn) {
170                                         cp = cn->ap_next;
171                                         constraint_free( cn );
172                                         cn = cp;
173                                 }
174                                                 
175                                 on->on_bi.bi_private = NULL;
176                         } else {
177                                 constraint **cpp;
178                                                 
179                                 /* zap constraint numbered 'valx' */
180                                 for(i=0, cp = cn, cpp = &cn;
181                                         (cp) && (i<c->valx);
182                                         i++, cpp = &cp->ap_next, cp = *cpp);
183
184                                 if (cp) {
185                                         /* zap cp, and join cpp to cp->ap_next */
186                                         *cpp = cp->ap_next;
187                                         constraint_free( cp );
188                                 }
189                                 on->on_bi.bi_private = cn;
190                         }
191                         break;
192
193                 default:
194                         abort();
195                         break;
196                 }
197                 break;
198         case SLAP_CONFIG_ADD:
199         case LDAP_MOD_ADD:
200                 switch (c->type) {
201                 case CONSTRAINT_ATTRIBUTE:
202                         if ( slap_str2ad( c->argv[1], &ap.ap, &text ) ) {
203                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
204                                         "%s <%s>: %s\n", c->argv[0], c->argv[1], text );
205                                 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
206                                            "%s: %s\n", c->log, c->cr_msg, 0 );
207                                 return( ARG_BAD_CONF );
208                         }
209
210                         if ( strcasecmp( c->argv[2], REGEX_STR ) == 0) {
211                                 int err;
212                         
213                                 ap.re = ch_malloc( sizeof(regex_t) );
214                                 if ((err = regcomp( ap.re,
215                                         c->argv[3], REG_EXTENDED )) != 0) {
216                                         char errmsg[1024];
217                                                         
218                                         regerror( err, ap.re, errmsg, sizeof(errmsg) );
219                                         ch_free(ap.re);
220                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
221                                            "%s %s: Illegal regular expression \"%s\": Error %s",
222                                            c->argv[0], c->argv[1], c->argv[3], errmsg);
223                                         Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
224                                                 "%s: %s\n", c->log, c->cr_msg, 0 );
225                                         ap.re = NULL;
226                                         return( ARG_BAD_CONF );
227                                 }
228                                 ber_str2bv( c->argv[3], 0, 1, &ap.val );
229                         } else if ( strcasecmp( c->argv[2], SIZE_STR ) == 0 ) {
230                                 size_t size;
231
232                                 if ( ( size = atoi(c->argv[3]) ) != 0 )
233                                         ap.size = size; 
234                         } else if ( strcasecmp( c->argv[2], COUNT_STR ) == 0 ) {
235                                 size_t count;
236
237                                 if ( ( count = atoi(c->argv[3]) ) != 0 )
238                                         ap.count = count;       
239                         } else if ( strcasecmp( c->argv[2], URI_STR ) == 0 ) {
240                                 int err;
241                         
242                                 err = ldap_url_parse(c->argv[3], &ap.lud);
243                                 if ( err != LDAP_URL_SUCCESS ) {
244                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
245                                                 "%s %s: Invalid URI \"%s\"",
246                                                 c->argv[0], c->argv[1], c->argv[3]);
247                                         Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
248                                                 "%s: %s\n", c->log, c->cr_msg, 0 );
249                                         return( ARG_BAD_CONF );
250                                 }
251
252                                 if (ap.lud->lud_host != NULL) {
253                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
254                                                 "%s %s: unsupported hostname in URI \"%s\"",
255                                                 c->argv[0], c->argv[1], c->argv[3]);
256                                         Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
257                                                 "%s: %s\n", c->log, c->cr_msg, 0 );
258
259                                         ldap_free_urldesc(ap.lud);
260
261                                         return( ARG_BAD_CONF );
262                                 }
263
264                                 for ( i=0; ap.lud->lud_attrs[i]; i++);
265                                 /* FIXME: This is worthless without at least one attr */
266                                 if ( i ) {
267                                         ap.attrs = ch_malloc( (i+1)*sizeof(AttributeDescription *));
268                                         for ( i=0; ap.lud->lud_attrs[i]; i++) {
269                                                 ap.attrs[i] = NULL;
270                                                 if ( slap_str2ad( ap.lud->lud_attrs[i], &ap.attrs[i], &text ) ) {
271                                                         ch_free( ap.attrs );
272                                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
273                                                                 "%s <%s>: %s\n", c->argv[0], ap.lud->lud_attrs[i], text );
274                                                         Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
275                                                                    "%s: %s\n", c->log, c->cr_msg, 0 );
276                                                         return( ARG_BAD_CONF );
277                                                 }
278                                         }
279                                         ap.attrs[i] = NULL;
280                                 }
281
282                                 if (ap.lud->lud_dn == NULL)
283                                         ap.lud->lud_dn = ch_strdup("");
284
285                                 if (ap.lud->lud_filter == NULL) {
286                                         ap.lud->lud_filter = ch_strdup("objectClass=*");
287                                 } else if ( ap.lud->lud_filter[0] == '(' ) {
288                                         ber_len_t len = strlen( ap.lud->lud_filter );
289                                         if ( ap.lud->lud_filter[len - 1] != ')' ) {
290                                                         return( ARG_BAD_CONF );
291                                         }
292                                         AC_MEMCPY( &ap.lud->lud_filter[0], &ap.lud->lud_filter[1], len - 2 );
293                                         ap.lud->lud_filter[len - 2] = '\0';
294                                 }
295
296                                 ber_str2bv( c->argv[3], 0, 1, &ap.val );
297                         } else {
298                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
299                                    "%s %s: Unknown constraint type: %s",
300                                    c->argv[0], c->argv[1], c->argv[2] );
301                                 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
302                                    "%s: %s\n", c->log, c->cr_msg, 0 );
303                                 return ( ARG_BAD_CONF );
304                         }
305
306                         a2 = ch_calloc( sizeof(constraint), 1 );
307                         a2->ap_next = on->on_bi.bi_private;
308                         a2->ap = ap.ap;
309                         a2->re = ap.re;
310                         a2->val = ap.val;
311                         a2->lud = ap.lud;
312                         a2->size = ap.size;
313                         a2->count = ap.count;
314                         if ( a2->lud ) {
315                                 ber_str2bv(a2->lud->lud_dn, 0, 0, &a2->dn);
316                                 ber_str2bv(a2->lud->lud_filter, 0, 0, &a2->filter);
317                         }
318                         a2->attrs = ap.attrs;
319                         on->on_bi.bi_private = a2;
320                         break;
321                 default:
322                         abort();
323                         break;
324                 }
325                 break;
326         default:
327                 abort();
328         }
329
330         return rc;
331 }
332
333 static int
334 constraint_uri_cb( Operation *op, SlapReply *rs ) 
335 {
336         if(rs->sr_type == REP_SEARCH) {
337                 int *foundp = op->o_callback->sc_private;
338
339                 *foundp = 1;
340
341                 Debug(LDAP_DEBUG_TRACE, "==> constraint_uri_cb <%s>\n",
342                         rs->sr_entry ? rs->sr_entry->e_name.bv_val : "UNKNOWN_DN", 0, 0);
343         }
344         return 0;
345 }
346
347 static int
348 constraint_violation( constraint *c, struct berval *bv, Operation *op, SlapReply *rs)
349 {
350         if ((!c) || (!bv)) return LDAP_SUCCESS;
351         
352         if ((c->re) &&
353                 (regexec(c->re, bv->bv_val, 0, NULL, 0) == REG_NOMATCH))
354                 return LDAP_CONSTRAINT_VIOLATION; /* regular expression violation */
355
356         if ((c->size) && (bv->bv_len > c->size))
357                 return LDAP_CONSTRAINT_VIOLATION; /* size violation */
358
359         if (c->lud) {
360                 Operation nop = *op;
361                 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
362                 slap_callback cb;
363                 SlapReply nrs = { REP_RESULT };
364                 int i;
365                 int found;
366                 int rc;
367                 size_t len;
368                 struct berval filterstr;
369                 char *ptr;
370
371                 found = 0;
372
373                 nrs.sr_entry = NULL;
374                 nrs.sr_nentries = 0;
375
376                 cb.sc_next = NULL;
377                 cb.sc_response = constraint_uri_cb;
378                 cb.sc_cleanup = NULL;
379                 cb.sc_private = &found;
380
381                 nop.o_protocol = LDAP_VERSION3;
382                 nop.o_tag = LDAP_REQ_SEARCH;
383                 nop.o_time = slap_get_time();
384                 if (c->lud->lud_dn) {
385                         struct berval dn;
386
387                         ber_str2bv(c->lud->lud_dn, 0, 0, &dn);
388                         nop.o_req_dn = dn;
389                         nop.o_req_ndn = dn;
390                         nop.o_bd = select_backend(&nop.o_req_ndn, 1 );
391                         if (!nop.o_bd) {
392                                 return LDAP_NO_SUCH_OBJECT; /* unexpected error */
393                         }
394                         if (!nop.o_bd->be_search) {
395                                 return LDAP_OTHER; /* unexpected error */
396                         }
397                 } else {
398                         nop.o_req_dn = nop.o_bd->be_nsuffix[0];
399                         nop.o_req_ndn = nop.o_bd->be_nsuffix[0];
400                         nop.o_bd = on->on_info->oi_origdb;
401                 }
402                 nop.o_do_not_cache = 1;
403                 nop.o_callback = &cb;
404
405                 nop.ors_scope = c->lud->lud_scope;
406                 nop.ors_deref = LDAP_DEREF_NEVER;
407                 nop.ors_slimit = SLAP_NO_LIMIT;
408                 nop.ors_tlimit = SLAP_NO_LIMIT;
409                 nop.ors_limit = NULL;
410
411                 nop.ors_attrsonly = 0;
412                 nop.ors_attrs = slap_anlist_no_attrs;
413
414                 len = STRLENOF("(&(") + 
415                           c->filter.bv_len +
416                           STRLENOF(")(|");
417
418                 for (i = 0; c->attrs[i]; i++) {
419                         len += STRLENOF("(") +
420                                    c->attrs[i]->ad_cname.bv_len +
421                                    STRLENOF("=") + 
422                                    bv->bv_len +
423                                    STRLENOF(")");
424                 }
425
426                 len += STRLENOF("))");
427                 filterstr.bv_len = len;
428                 filterstr.bv_val = op->o_tmpalloc(len + 1, op->o_tmpmemctx);
429
430                 ptr = filterstr.bv_val +
431                         snprintf(filterstr.bv_val, len, "(&(%s)(|", c->lud->lud_filter);
432                 for (i = 0; c->attrs[i]; i++) {
433                         *ptr++ = '(';
434                         ptr = lutil_strcopy( ptr, c->attrs[i]->ad_cname.bv_val );
435                         *ptr++ = '=';
436                         ptr = lutil_strcopy( ptr, bv->bv_val );
437                         *ptr++ = ')';
438                 }
439                 *ptr++ = ')';
440                 *ptr++ = ')';
441                 *ptr++ = '\0';
442
443                 nop.ors_filterstr = filterstr;
444                 nop.ors_filter = str2filter_x(&nop, filterstr.bv_val);
445                 if ( nop.ors_filter == NULL ) {
446                         Debug( LDAP_DEBUG_ANY,
447                                 "%s constraint_violation uri filter=\"%s\" invalid\n",
448                                 op->o_log_prefix, filterstr.bv_val, 0 );
449
450                 } else {
451                         Debug(LDAP_DEBUG_TRACE, 
452                                 "==> constraint_violation uri filter = %s\n",
453                                 filterstr.bv_val, 0, 0);
454
455                         rc = nop.o_bd->be_search( &nop, &nrs );
456                 
457                         Debug(LDAP_DEBUG_TRACE, 
458                                 "==> constraint_violation uri rc = %d, found = %d\n",
459                                 rc, found, 0);
460                 }
461                 op->o_tmpfree(filterstr.bv_val, op->o_tmpmemctx);
462
463                 if((rc != LDAP_SUCCESS) && (rc != LDAP_NO_SUCH_OBJECT)) {
464                         return rc; /* unexpected error */
465                 }
466
467                 if (!found)
468                         return LDAP_CONSTRAINT_VIOLATION; /* constraint violation */
469                         
470         }
471         
472         return LDAP_SUCCESS;
473 }
474
475 static char *
476 print_message( struct berval *errtext, AttributeDescription *a )
477 {
478         char *ret;
479         int sz;
480         
481         sz = errtext->bv_len + sizeof(" on ") + a->ad_cname.bv_len;
482         ret = ch_malloc(sz);
483         snprintf( ret, sz, "%s on %s", errtext->bv_val, a->ad_cname.bv_val );
484         return ret;
485 }
486
487 static unsigned
488 constraint_count_attr(Entry *e, AttributeDescription *ad)
489 {
490         struct Attribute *a;
491
492         if ((a = attr_find(e->e_attrs, ad)) != NULL)
493                 return a->a_numvals;
494         return 0;
495 }
496
497 static int
498 constraint_add( Operation *op, SlapReply *rs )
499 {
500         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
501         Backend *be = op->o_bd;
502         Attribute *a;
503         constraint *c = on->on_bi.bi_private, *cp;
504         BerVarray b = NULL;
505         int i;
506         struct berval rsv = BER_BVC("add breaks constraint");
507         int rc;
508         char *msg = NULL;
509
510         if ((a = op->ora_e->e_attrs) == NULL) {
511                 op->o_bd->bd_info = (BackendInfo *)(on->on_info);
512                 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
513                         "constraint_add: no attrs");
514                 return(rs->sr_err);
515         }
516
517         for(; a; a = a->a_next ) {
518                 /* we don't constrain operational attributes */
519                 if (is_at_operational(a->a_desc->ad_type)) continue;
520
521                 for(cp = c; cp; cp = cp->ap_next) {
522                         if (cp->ap != a->a_desc) continue;
523                         if ((b = a->a_vals) == NULL) continue;
524                                 
525                         Debug(LDAP_DEBUG_TRACE, 
526                                 "==> constraint_add, "
527                                 "a->a_numvals = %d, cp->count = %d\n",
528                                 a->a_numvals, cp->count, 0);
529
530                         if ((cp->count != 0) && (a->a_numvals > cp->count)) {
531                                 rc = LDAP_CONSTRAINT_VIOLATION;
532                                 goto add_violation;
533                         }
534
535                         for ( i = 0; b[i].bv_val; i++ ) {
536                                 rc = constraint_violation( cp, &b[i], op, rs );
537                                 if ( rc ) {
538                                         goto add_violation;
539                                 }
540                         }
541                 }
542         }
543         /* Default is to just fall through to the normal processing */
544         return SLAP_CB_CONTINUE;
545
546 add_violation:
547         op->o_bd->bd_info = (BackendInfo *)(on->on_info);
548         if (rc == LDAP_CONSTRAINT_VIOLATION ) {
549                 msg = print_message( &rsv, a->a_desc );
550         }
551         send_ldap_error(op, rs, rc, msg );
552         ch_free(msg);
553         return (rs->sr_err);
554 }
555
556
557 static int
558 constraint_modify( Operation *op, SlapReply *rs )
559 {
560         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
561         Backend *be = op->o_bd;
562         constraint *c = on->on_bi.bi_private, *cp;
563         Entry *target_entry = NULL;
564         Modifications *m;
565         BerVarray b = NULL;
566         int i;
567         struct berval rsv = BER_BVC("modify breaks constraint");
568         int rc;
569         char *msg = NULL;
570         
571         Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "constraint_modify()", 0,0,0);
572         if ((m = op->orm_modlist) == NULL) {
573                 op->o_bd->bd_info = (BackendInfo *)(on->on_info);
574                 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
575                                                 "constraint_modify() got null orm_modlist");
576                 return(rs->sr_err);
577         }
578
579         /* Do we need to count attributes? */
580         for(cp = c; cp; cp = cp->ap_next) {
581                 if (cp->count != 0) {
582
583                         op->o_bd = on->on_info->oi_origdb;
584                         rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &target_entry );
585                         op->o_bd = be;
586
587                         if (rc != 0 || target_entry == NULL) {
588                                 Debug(LDAP_DEBUG_TRACE, 
589                                         "==> constraint_modify rc = %d DN=\"%s\"%s\n",
590                                         rc, op->o_req_ndn.bv_val,
591                                         target_entry ? "" : " not found" );
592                                 if ( rc == 0 ) 
593                                         rc = LDAP_CONSTRAINT_VIOLATION;
594                                 goto mod_violation;
595                         }
596                         break;
597                 }
598         }
599
600         rc = LDAP_CONSTRAINT_VIOLATION;
601         for(;m; m = m->sml_next) {
602                 int ce = 0;
603
604                 /* Get this attribute count, if needed */
605                 if (target_entry)
606                         ce = constraint_count_attr(target_entry, m->sml_desc);
607
608                 if (is_at_operational( m->sml_desc->ad_type )) continue;
609                 if ((( m->sml_op & LDAP_MOD_OP ) != LDAP_MOD_ADD) &&
610                         (( m->sml_op & LDAP_MOD_OP ) != LDAP_MOD_REPLACE) &&
611                         (( m->sml_op & LDAP_MOD_OP ) != LDAP_MOD_DELETE))
612                         continue;
613                 /* we only care about ADD and REPLACE modifications */
614                 /* and DELETE are used to track attribute count */
615                 if ((( b = m->sml_values ) == NULL ) || (b[0].bv_val == NULL))
616                         continue;
617
618                 for(cp = c; cp; cp = cp->ap_next) {
619                         if (cp->ap != m->sml_desc) continue;
620                         
621                         if (cp->count != 0) {
622                                 int ca;
623
624                                 if (m->sml_op == LDAP_MOD_DELETE)
625                                         ce = 0;
626
627                                 for (ca = 0; b[ca].bv_val; ++ca);
628
629                                 Debug(LDAP_DEBUG_TRACE, 
630                                         "==> constraint_modify ce = %d, "
631                                         "ca = %d, cp->count = %d\n",
632                                         ce, ca, cp->count);
633
634                                 if (m->sml_op == LDAP_MOD_ADD) {
635                                         if (ca + ce > cp->count) {
636                                                 rc = LDAP_CONSTRAINT_VIOLATION;
637                                                 goto mod_violation;
638                                         }
639                                 }
640                                 if (m->sml_op == LDAP_MOD_REPLACE) {
641                                         if (ca > cp->count) {
642                                                 rc = LDAP_CONSTRAINT_VIOLATION;
643                                                 goto mod_violation;
644                                         }
645                                         ce = ca;
646                                 }
647                         } 
648
649                         /* DELETE are to be ignored beyond this point */
650                         if (( m->sml_op & LDAP_MOD_OP ) == LDAP_MOD_DELETE)
651                                 continue;
652
653                         for ( i = 0; b[i].bv_val; i++ ) {
654                                 rc = constraint_violation( cp, &b[i], op, rs );
655                                 if ( rc ) {
656                                         goto mod_violation;
657                                 }
658                         }
659                 }
660         }
661         
662         if (target_entry) {
663                 op->o_bd = on->on_info->oi_origdb;
664                 be_entry_release_r(op, target_entry);
665                 op->o_bd = be;
666         }
667         return SLAP_CB_CONTINUE;
668
669 mod_violation:
670         /* violation */
671         if (target_entry) {
672                 op->o_bd = on->on_info->oi_origdb;
673                 be_entry_release_r(op, target_entry);
674                 op->o_bd = be;
675         }
676         op->o_bd->bd_info = (BackendInfo *)(on->on_info);
677         if ( rc == LDAP_CONSTRAINT_VIOLATION ) {
678                 msg = print_message( &rsv, m->sml_desc );
679         }
680         send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION, msg );
681         ch_free(msg);
682         return (rs->sr_err);
683 }
684
685 static int
686 constraint_close(
687         BackendDB *be,
688         ConfigReply *cr )
689 {
690         slap_overinst *on = (slap_overinst *) be->bd_info;
691         constraint *ap, *a2;
692
693         for ( ap = on->on_bi.bi_private; ap; ap = a2 ) {
694                 a2 = ap->ap_next;
695                 constraint_free( ap );
696         }
697
698         return 0;
699 }
700
701 static slap_overinst constraint_ovl;
702
703 #if SLAPD_OVER_CONSTRAINT == SLAPD_MOD_DYNAMIC
704 static
705 #endif
706 int
707 constraint_initialize( void ) {
708         int rc;
709
710         constraint_ovl.on_bi.bi_type = "constraint";
711         constraint_ovl.on_bi.bi_db_close = constraint_close;
712         constraint_ovl.on_bi.bi_op_add = constraint_add;
713         constraint_ovl.on_bi.bi_op_modify = constraint_modify;
714
715         constraint_ovl.on_bi.bi_private = NULL;
716         
717         constraint_ovl.on_bi.bi_cf_ocs = constraintocs;
718         rc = config_register_schema( constraintcfg, constraintocs );
719         if (rc) return rc;
720         
721         return overlay_register( &constraint_ovl );
722 }
723
724 #if SLAPD_OVER_CONSTRAINT == SLAPD_MOD_DYNAMIC
725 int init_module(int argc, char *argv[]) {
726         return constraint_initialize();
727 }
728 #endif
729
730 #endif /* defined(SLAPD_OVER_CONSTRAINT) */
731