2 /* constraint.c - Overlay to constrain attributes to certain values */
4 * Copyright 2003-2004 Hewlett-Packard Company
5 * Copyright 2007 Emmanuel Dreyfus
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
17 * Authors: Neil Dunbar <neil.dunbar@hp.com>
18 * Emmannuel Dreyfus <manu@netbsd.org>
22 #ifdef SLAPD_OVER_CONSTRAINT
26 #include <ac/string.h>
27 #include <ac/socket.h>
35 * This overlay limits the values which can be placed into an
36 * attribute, over and above the limits placed by the schema.
38 * It traps only LDAP adds and modify commands (and only seeks to
39 * control the add and modify value mods of a modify)
42 #define REGEX_STR "regex"
45 #define SIZE_STR "size"
46 #define COUNT_STR "count"
49 * Linked list of attribute constraints which we should enforce.
50 * This is probably a sub optimal structure - some form of sorted
51 * array would be better if the number of attributes contrained is
52 * likely to be much bigger than 4 or 5. We stick with a list for
56 typedef struct constraint {
57 struct constraint *ap_next;
58 AttributeDescription **ap;
60 LDAPURLDesc *restrict_lud;
61 struct berval restrict_ndn;
62 Filter *restrict_filter;
63 struct berval restrict_val;
70 AttributeDescription **attrs;
71 struct berval val; /* constraint value */
77 CONSTRAINT_ATTRIBUTE = 1
80 static ConfigDriver constraint_cf_gen;
82 static ConfigTable constraintcfg[] = {
83 { "constraint_attribute", "attribute[list]> (regex|uri|set|size|count) <value> [<restrict URI>]",
84 4, 0, 0, ARG_MAGIC | CONSTRAINT_ATTRIBUTE, constraint_cf_gen,
85 "( OLcfgOvAt:13.1 NAME 'olcConstraintAttribute' "
86 "DESC 'constraint for list of attributes' "
87 "EQUALITY caseIgnoreMatch "
88 "SYNTAX OMsDirectoryString )", NULL, NULL },
89 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
92 static ConfigOCs constraintocs[] = {
94 "NAME 'olcConstraintConfig' "
95 "DESC 'Constraint overlay configuration' "
96 "SUP olcOverlayConfig "
97 "MAY ( olcConstraintAttribute ) )",
98 Cft_Overlay, constraintcfg },
103 constraint_free( constraint *cp, int freeme )
105 if (cp->restrict_lud)
106 ldap_free_urldesc(cp->restrict_lud);
107 if (!BER_BVISNULL(&cp->restrict_ndn))
108 ch_free(cp->restrict_ndn.bv_val);
109 if (cp->restrict_filter != NULL && cp->restrict_filter != slap_filter_objectClass_pres)
110 filter_free(cp->restrict_filter);
111 if (!BER_BVISNULL(&cp->restrict_val))
112 ch_free(cp->restrict_val.bv_val);
117 if (!BER_BVISNULL(&cp->val))
118 ch_free(cp->val.bv_val);
120 ldap_free_urldesc(cp->lud);
130 constraint_cf_gen( ConfigArgs *c )
132 slap_overinst *on = (slap_overinst *)(c->bi);
133 constraint *cn = on->on_bi.bi_private, *cp;
136 constraint ap = { NULL };
137 const char *text = NULL;
140 case SLAP_CONFIG_EMIT:
142 case CONSTRAINT_ATTRIBUTE:
143 for (cp=cn; cp; cp=cp->ap_next) {
149 char val_buf[SLAP_TEXT_BUFLEN] = { '\0' };
151 bv.bv_len = STRLENOF(" ");
152 for (j = 0; cp->ap[j]; j++) {
153 bv.bv_len += cp->ap[j]->ad_cname.bv_len;
156 /* room for commas */
162 } else if (cp->lud) {
165 } else if (cp->set) {
168 } else if (cp->size) {
171 } else if (cp->count) {
176 bv.bv_len += strlen(tstr);
177 bv.bv_len += cp->val.bv_len + 2*quotes;
179 if (cp->restrict_lud != NULL) {
180 bv.bv_len += cp->restrict_val.bv_len + STRLENOF(" restrict=\"\"");
183 if (cp->count || cp->size) {
184 int len = snprintf(val_buf, sizeof(val_buf), "%d", val);
192 s = bv.bv_val = ch_malloc(bv.bv_len + 1);
194 s = lutil_strncopy( s, cp->ap[0]->ad_cname.bv_val, cp->ap[0]->ad_cname.bv_len );
195 for (j = 1; cp->ap[j]; j++) {
197 s = lutil_strncopy( s, cp->ap[j]->ad_cname.bv_val, cp->ap[j]->ad_cname.bv_len );
200 s = lutil_strcopy( s, tstr );
202 if (cp->count || cp->size) {
203 s = lutil_strcopy( s, val_buf );
205 if ( quotes ) *s++ = '"';
206 s = lutil_strncopy( s, cp->val.bv_val, cp->val.bv_len );
207 if ( quotes ) *s++ = '"';
209 if (cp->restrict_lud != NULL) {
210 s = lutil_strcopy( s, " restrict=\"" );
211 s = lutil_strncopy( s, cp->restrict_val.bv_val, cp->restrict_val.bv_len );
216 rc = value_add_one( &c->rvalue_vals, &bv );
217 if (rc == LDAP_SUCCESS)
218 rc = value_add_one( &c->rvalue_nvals, &bv );
228 case LDAP_MOD_DELETE:
230 case CONSTRAINT_ATTRIBUTE:
231 if (!cn) break; /* nothing to do */
234 /* zap all constraints */
237 constraint_free( cn, 1 );
241 on->on_bi.bi_private = NULL;
245 /* zap constraint numbered 'valx' */
246 for(i=0, cp = cn, cpp = &cn;
248 i++, cpp = &cp->ap_next, cp = *cpp);
251 /* zap cp, and join cpp to cp->ap_next */
253 constraint_free( cp, 1 );
255 on->on_bi.bi_private = cn;
264 case SLAP_CONFIG_ADD:
267 case CONSTRAINT_ATTRIBUTE: {
269 char **attrs = ldap_str2charray( c->argv[1], "," );
271 for ( j = 0; attrs[j]; j++)
273 ap.ap = ch_calloc( sizeof(AttributeDescription*), j + 1 );
274 for ( j = 0; attrs[j]; j++) {
275 if ( slap_str2ad( attrs[j], &ap.ap[j], &text ) ) {
276 snprintf( c->cr_msg, sizeof( c->cr_msg ),
277 "%s <%s>: %s\n", c->argv[0], attrs[j], text );
283 if ( strcasecmp( c->argv[2], REGEX_STR ) == 0) {
286 ap.re = ch_malloc( sizeof(regex_t) );
287 if ((err = regcomp( ap.re,
288 c->argv[3], REG_EXTENDED )) != 0) {
291 regerror( err, ap.re, errmsg, sizeof(errmsg) );
293 snprintf( c->cr_msg, sizeof( c->cr_msg ),
294 "%s %s: Illegal regular expression \"%s\": Error %s",
295 c->argv[0], c->argv[1], c->argv[3], errmsg);
300 ber_str2bv( c->argv[3], 0, 1, &ap.val );
301 } else if ( strcasecmp( c->argv[2], SIZE_STR ) == 0 ) {
304 if ( ( size = atoi(c->argv[3]) ) != 0 )
306 } else if ( strcasecmp( c->argv[2], COUNT_STR ) == 0 ) {
309 if ( ( count = atoi(c->argv[3]) ) != 0 )
311 } else if ( strcasecmp( c->argv[2], URI_STR ) == 0 ) {
314 err = ldap_url_parse(c->argv[3], &ap.lud);
315 if ( err != LDAP_URL_SUCCESS ) {
316 snprintf( c->cr_msg, sizeof( c->cr_msg ),
317 "%s %s: Invalid URI \"%s\"",
318 c->argv[0], c->argv[1], c->argv[3]);
323 if (ap.lud->lud_host != NULL) {
324 snprintf( c->cr_msg, sizeof( c->cr_msg ),
325 "%s %s: unsupported hostname in URI \"%s\"",
326 c->argv[0], c->argv[1], c->argv[3]);
327 ldap_free_urldesc(ap.lud);
332 for ( i=0; ap.lud->lud_attrs[i]; i++);
333 /* FIXME: This is worthless without at least one attr */
335 ap.attrs = ch_malloc( (i+1)*sizeof(AttributeDescription *));
336 for ( i=0; ap.lud->lud_attrs[i]; i++) {
338 if ( slap_str2ad( ap.lud->lud_attrs[i], &ap.attrs[i], &text ) ) {
340 snprintf( c->cr_msg, sizeof( c->cr_msg ),
341 "%s <%s>: %s\n", c->argv[0], ap.lud->lud_attrs[i], text );
349 if (ap.lud->lud_dn == NULL) {
350 ap.lud->lud_dn = ch_strdup("");
352 struct berval dn, ndn;
354 ber_str2bv( ap.lud->lud_dn, 0, 0, &dn );
355 if (dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL ) ) {
357 snprintf( c->cr_msg, sizeof( c->cr_msg ),
358 "%s %s: URI %s DN normalization failed",
359 c->argv[0], c->argv[1], c->argv[3] );
360 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
361 "%s: %s\n", c->log, c->cr_msg, 0 );
365 ldap_memfree( ap.lud->lud_dn );
366 ap.lud->lud_dn = ndn.bv_val;
369 if (ap.lud->lud_filter == NULL) {
370 ap.lud->lud_filter = ch_strdup("objectClass=*");
371 } else if ( ap.lud->lud_filter[0] == '(' ) {
372 ber_len_t len = strlen( ap.lud->lud_filter );
373 if ( ap.lud->lud_filter[len - 1] != ')' ) {
374 snprintf( c->cr_msg, sizeof( c->cr_msg ),
375 "%s %s: invalid URI filter: %s",
376 c->argv[0], c->argv[1], ap.lud->lud_filter );
380 AC_MEMCPY( &ap.lud->lud_filter[0], &ap.lud->lud_filter[1], len - 2 );
381 ap.lud->lud_filter[len - 2] = '\0';
384 ber_str2bv( c->argv[3], 0, 1, &ap.val );
386 } else if ( strcasecmp( c->argv[2], SET_STR ) == 0 ) {
388 ber_str2bv( c->argv[3], 0, 1, &ap.val );
391 snprintf( c->cr_msg, sizeof( c->cr_msg ),
392 "%s %s: Unknown constraint type: %s",
393 c->argv[0], c->argv[1], c->argv[2] );
401 for ( argidx = 4; argidx < c->argc; argidx++ ) {
402 if ( strncasecmp( c->argv[argidx], "restrict=", STRLENOF("restrict=") ) == 0 ) {
404 char *arg = c->argv[argidx] + STRLENOF("restrict=");
406 err = ldap_url_parse(arg, &ap.restrict_lud);
407 if ( err != LDAP_URL_SUCCESS ) {
408 snprintf( c->cr_msg, sizeof( c->cr_msg ),
409 "%s %s: Invalid restrict URI \"%s\"",
410 c->argv[0], c->argv[1], arg);
415 if (ap.restrict_lud->lud_host != NULL) {
416 snprintf( c->cr_msg, sizeof( c->cr_msg ),
417 "%s %s: unsupported hostname in restrict URI \"%s\"",
418 c->argv[0], c->argv[1], arg);
423 if ( ap.restrict_lud->lud_attrs != NULL ) {
424 if ( ap.restrict_lud->lud_attrs[0] != '\0' ) {
425 snprintf( c->cr_msg, sizeof( c->cr_msg ),
426 "%s %s: attrs not allowed in restrict URI %s\n",
427 c->argv[0], c->argv[1], arg);
431 ldap_memvfree((void *)ap.restrict_lud->lud_attrs);
432 ap.restrict_lud->lud_attrs = NULL;
435 if (ap.restrict_lud->lud_dn != NULL) {
436 if (ap.restrict_lud->lud_dn[0] == '\0') {
437 ldap_memfree(ap.restrict_lud->lud_dn);
438 ap.restrict_lud->lud_dn = NULL;
441 struct berval dn, ndn;
444 ber_str2bv(ap.restrict_lud->lud_dn, 0, 0, &dn);
445 if (dnNormalize(0, NULL, NULL, &dn, &ndn, NULL)) {
447 snprintf( c->cr_msg, sizeof( c->cr_msg ),
448 "%s %s: restrict URI %s DN normalization failed",
449 c->argv[0], c->argv[1], arg );
454 assert(c->be != NULL);
455 if (c->be->be_nsuffix == NULL) {
456 snprintf( c->cr_msg, sizeof( c->cr_msg ),
457 "%s %s: restrict URI requires suffix",
458 c->argv[0], c->argv[1] );
463 for ( j = 0; !BER_BVISNULL(&c->be->be_nsuffix[j]); j++) {
464 if (dnIsSuffix(&ndn, &c->be->be_nsuffix[j])) break;
467 if (BER_BVISNULL(&c->be->be_nsuffix[j])) {
469 snprintf( c->cr_msg, sizeof( c->cr_msg ),
470 "%s %s: restrict URI DN %s not within database naming context(s)",
471 c->argv[0], c->argv[1], dn.bv_val );
476 ap.restrict_ndn = ndn;
480 if (ap.restrict_lud->lud_filter != NULL) {
481 ap.restrict_filter = str2filter(ap.restrict_lud->lud_filter);
482 if (ap.restrict_filter == NULL) {
484 snprintf( c->cr_msg, sizeof( c->cr_msg ),
485 "%s %s: restrict URI filter %s invalid",
486 c->argv[0], c->argv[1], ap.restrict_lud->lud_filter );
492 ber_str2bv(c->argv[argidx] + STRLENOF("restrict="), 0, 1, &ap.restrict_val);
496 snprintf( c->cr_msg, sizeof( c->cr_msg ),
497 "%s %s: unrecognized arg #%d (%s)",
498 c->argv[0], c->argv[1], argidx, c->argv[argidx] );
506 if ( rc == LDAP_SUCCESS ) {
507 constraint *a2 = ch_calloc( sizeof(constraint), 1 );
508 a2->ap_next = on->on_bi.bi_private;
515 a2->count = ap.count;
517 ber_str2bv(a2->lud->lud_dn, 0, 0, &a2->dn);
518 ber_str2bv(a2->lud->lud_filter, 0, 0, &a2->filter);
520 a2->attrs = ap.attrs;
521 a2->restrict_lud = ap.restrict_lud;
522 a2->restrict_ndn = ap.restrict_ndn;
523 a2->restrict_filter = ap.restrict_filter;
524 a2->restrict_val = ap.restrict_val;
525 on->on_bi.bi_private = a2;
528 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
529 "%s: %s\n", c->log, c->cr_msg, 0 );
530 constraint_free( &ap, 0 );
533 ldap_memvfree((void**)attrs);
548 constraint_uri_cb( Operation *op, SlapReply *rs )
550 if(rs->sr_type == REP_SEARCH) {
551 int *foundp = op->o_callback->sc_private;
555 Debug(LDAP_DEBUG_TRACE, "==> constraint_uri_cb <%s>\n",
556 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "UNKNOWN_DN", 0, 0);
562 constraint_violation( constraint *c, struct berval *bv, Operation *op )
564 if ((!c) || (!bv)) return LDAP_SUCCESS;
567 (regexec(c->re, bv->bv_val, 0, NULL, 0) == REG_NOMATCH))
568 return LDAP_CONSTRAINT_VIOLATION; /* regular expression violation */
570 if ((c->size) && (bv->bv_len > c->size))
571 return LDAP_CONSTRAINT_VIOLATION; /* size violation */
575 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
581 struct berval filterstr;
585 cb.sc_response = constraint_uri_cb;
586 cb.sc_cleanup = NULL;
587 cb.sc_private = &found;
589 nop.o_protocol = LDAP_VERSION3;
590 nop.o_tag = LDAP_REQ_SEARCH;
591 nop.o_time = slap_get_time();
592 if (c->lud->lud_dn) {
595 ber_str2bv(c->lud->lud_dn, 0, 0, &dn);
598 nop.o_bd = select_backend(&nop.o_req_ndn, 1 );
600 return LDAP_NO_SUCH_OBJECT; /* unexpected error */
602 if (!nop.o_bd->be_search) {
603 return LDAP_OTHER; /* unexpected error */
606 nop.o_req_dn = nop.o_bd->be_nsuffix[0];
607 nop.o_req_ndn = nop.o_bd->be_nsuffix[0];
608 nop.o_bd = on->on_info->oi_origdb;
610 nop.o_do_not_cache = 1;
611 nop.o_callback = &cb;
613 nop.ors_scope = c->lud->lud_scope;
614 nop.ors_deref = LDAP_DEREF_NEVER;
615 nop.ors_slimit = SLAP_NO_LIMIT;
616 nop.ors_tlimit = SLAP_NO_LIMIT;
617 nop.ors_limit = NULL;
619 nop.ors_attrsonly = 0;
620 nop.ors_attrs = slap_anlist_no_attrs;
622 len = STRLENOF("(&(") +
626 for (i = 0; c->attrs[i]; i++) {
627 len += STRLENOF("(") +
628 c->attrs[i]->ad_cname.bv_len +
634 len += STRLENOF("))");
635 filterstr.bv_len = len;
636 filterstr.bv_val = op->o_tmpalloc(len + 1, op->o_tmpmemctx);
638 ptr = filterstr.bv_val +
639 snprintf(filterstr.bv_val, len, "(&(%s)(|", c->lud->lud_filter);
640 for (i = 0; c->attrs[i]; i++) {
642 ptr = lutil_strcopy( ptr, c->attrs[i]->ad_cname.bv_val );
644 ptr = lutil_strcopy( ptr, bv->bv_val );
651 nop.ors_filterstr = filterstr;
652 nop.ors_filter = str2filter_x(&nop, filterstr.bv_val);
653 if ( nop.ors_filter == NULL ) {
654 Debug( LDAP_DEBUG_ANY,
655 "%s constraint_violation uri filter=\"%s\" invalid\n",
656 op->o_log_prefix, filterstr.bv_val, 0 );
660 SlapReply nrs = { REP_RESULT };
662 Debug(LDAP_DEBUG_TRACE,
663 "==> constraint_violation uri filter = %s\n",
664 filterstr.bv_val, 0, 0);
666 rc = nop.o_bd->be_search( &nop, &nrs );
668 Debug(LDAP_DEBUG_TRACE,
669 "==> constraint_violation uri rc = %d, found = %d\n",
672 op->o_tmpfree(filterstr.bv_val, op->o_tmpmemctx);
674 if ((rc != LDAP_SUCCESS) && (rc != LDAP_NO_SUCH_OBJECT)) {
675 return rc; /* unexpected error */
679 return LDAP_CONSTRAINT_VIOLATION; /* constraint violation */
686 print_message( struct berval *errtext, AttributeDescription *a )
691 sz = errtext->bv_len + sizeof(" on ") + a->ad_cname.bv_len;
693 snprintf( ret, sz, "%s on %s", errtext->bv_val, a->ad_cname.bv_val );
698 constraint_count_attr(Entry *e, AttributeDescription *ad)
702 if ((a = attr_find(e->e_attrs, ad)) != NULL)
708 constraint_check_restrict( Operation *op, constraint *c, Entry *e )
710 assert( c->restrict_lud != NULL );
712 if ( c->restrict_lud->lud_dn != NULL ) {
713 int diff = e->e_nname.bv_len - c->restrict_ndn.bv_len;
719 if ( c->restrict_lud->lud_scope == LDAP_SCOPE_BASE ) {
720 return bvmatch( &e->e_nname, &c->restrict_ndn );
723 if ( !dnIsSuffix( &e->e_nname, &c->restrict_ndn ) ) {
727 if ( c->restrict_lud->lud_scope != LDAP_SCOPE_SUBTREE ) {
734 dnParent( &e->e_nname, &pdn );
736 if ( c->restrict_lud->lud_scope == LDAP_SCOPE_ONELEVEL
737 && pdn.bv_len != c->restrict_ndn.bv_len )
744 if ( c->restrict_filter != NULL ) {
746 struct berval save_dn = op->o_dn, save_ndn = op->o_ndn;
748 op->o_dn = op->o_bd->be_rootdn;
749 op->o_ndn = op->o_bd->be_rootndn;
750 rc = test_filter( op, e, c->restrict_filter );
752 op->o_ndn = save_ndn;
754 if ( rc != LDAP_COMPARE_TRUE ) {
763 constraint_add( Operation *op, SlapReply *rs )
765 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
767 constraint *c = on->on_bi.bi_private, *cp;
770 struct berval rsv = BER_BVC("add breaks constraint");
775 return SLAP_CB_CONTINUE;
778 if ((a = op->ora_e->e_attrs) == NULL) {
779 op->o_bd->bd_info = (BackendInfo *)(on->on_info);
780 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
781 "constraint_add: no attrs");
785 for(; a; a = a->a_next ) {
786 /* we don't constrain operational attributes */
787 if (is_at_operational(a->a_desc->ad_type)) continue;
789 for(cp = c; cp; cp = cp->ap_next) {
791 for (j = 0; cp->ap[j]; j++) {
792 if (cp->ap[j] == a->a_desc) break;
794 if (cp->ap[j] == NULL) continue;
795 if ((b = a->a_vals) == NULL) continue;
797 if (cp->restrict_lud != NULL && constraint_check_restrict(op, cp, op->ora_e) == 0) {
801 Debug(LDAP_DEBUG_TRACE,
802 "==> constraint_add, "
803 "a->a_numvals = %u, cp->count = %lu\n",
804 a->a_numvals, (unsigned long) cp->count, 0);
806 if ((cp->count != 0) && (a->a_numvals > cp->count)) {
807 rc = LDAP_CONSTRAINT_VIOLATION;
811 for ( i = 0; b[i].bv_val; i++ ) {
812 rc = constraint_violation( cp, &b[i], op );
818 if (cp->set && acl_match_set(&cp->val, op, op->ora_e, NULL) == 0) {
819 rc = LDAP_CONSTRAINT_VIOLATION;
820 goto add_violation; /* constraint violation */
826 /* Default is to just fall through to the normal processing */
827 return SLAP_CB_CONTINUE;
830 op->o_bd->bd_info = (BackendInfo *)(on->on_info);
831 if (rc == LDAP_CONSTRAINT_VIOLATION ) {
832 msg = print_message( &rsv, a->a_desc );
834 send_ldap_error(op, rs, rc, msg );
841 constraint_check_count_violation( Modifications *m, Entry *target_entry, constraint *cp )
848 for ( j = 0; cp->ap[j]; j++ ) {
851 /* Get this attribute count */
853 ce = constraint_count_attr( target_entry, cp->ap[j] );
855 for( ; m; m = m->sml_next ) {
856 if ( cp->ap[j] == m->sml_desc ) {
857 switch ( m->sml_op ) {
858 case LDAP_MOD_DELETE:
859 if (( b = m->sml_values ) == NULL || b[0].bv_val == NULL ) {
863 /* No need to check for values' validity. Invalid values
864 * cause the whole transaction to die anyway. */
865 for ( ca = 0; b[ca].bv_val; ++ca );
871 if (( b = m->sml_values ) == NULL || b[0].bv_val == NULL )
874 for ( ca = 0; b[ca].bv_val; ++ca );
878 case LDAP_MOD_REPLACE:
879 if (( b = m->sml_values ) == NULL || b[0].bv_val == NULL )
882 for ( ca = 0; b[ca].bv_val; ++ca );
887 /* impossible! assert? */
891 Debug(LDAP_DEBUG_TRACE,
892 "==> constraint_check_count_violation ce = %u, "
893 "ca = %u, cp->count = %lu\n",
894 ce, ca, (unsigned long) cp->count);
899 return ( ce > cp->count );
903 constraint_update( Operation *op, SlapReply *rs )
905 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
906 Backend *be = op->o_bd;
907 constraint *c = on->on_bi.bi_private, *cp;
908 Entry *target_entry = NULL, *target_entry_copy = NULL;
909 Modifications *modlist, *m;
912 struct berval rsv = BER_BVC("modify breaks constraint");
919 return SLAP_CB_CONTINUE;
922 switch ( op->o_tag ) {
923 case LDAP_REQ_MODIFY:
924 modlist = op->orm_modlist;
927 case LDAP_REQ_MODRDN:
928 modlist = op->orr_modlist;
932 /* impossible! assert? */
936 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "constraint_update()\n", 0,0,0);
937 if ((m = modlist) == NULL) {
938 op->o_bd->bd_info = (BackendInfo *)(on->on_info);
939 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
940 "constraint_update() got null modlist");
944 /* Do we need to count attributes? */
945 for(cp = c; cp; cp = cp->ap_next) {
946 if (cp->count != 0 || cp->set || cp->restrict_lud != 0) {
948 op->o_bd = on->on_info->oi_origdb;
949 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &target_entry );
953 if (rc != 0 || target_entry == NULL) {
954 Debug(LDAP_DEBUG_TRACE,
955 "==> constraint_update rc = %d DN=\"%s\"%s\n",
956 rc, op->o_req_ndn.bv_val,
957 target_entry ? "" : " not found" );
959 rc = LDAP_CONSTRAINT_VIOLATION;
963 is_v = constraint_check_count_violation(m, target_entry, cp);
965 Debug(LDAP_DEBUG_TRACE,
966 "==> constraint_update is_v: %d\n", is_v, 0, 0);
969 rc = LDAP_CONSTRAINT_VIOLATION;
975 rc = LDAP_CONSTRAINT_VIOLATION;
976 for(;m; m = m->sml_next) {
979 if (is_at_operational( m->sml_desc->ad_type )) continue;
981 if ((( m->sml_op & LDAP_MOD_OP ) != LDAP_MOD_ADD) &&
982 (( m->sml_op & LDAP_MOD_OP ) != LDAP_MOD_REPLACE) &&
983 (( m->sml_op & LDAP_MOD_OP ) != LDAP_MOD_DELETE))
985 /* we only care about ADD and REPLACE modifications */
986 /* and DELETE are used to track attribute count */
987 if ((( b = m->sml_values ) == NULL ) || (b[0].bv_val == NULL))
990 for(cp = c; cp; cp = cp->ap_next) {
992 for (j = 0; cp->ap[j]; j++) {
993 if (cp->ap[j] == m->sml_desc) {
997 if (cp->ap[j] == NULL) continue;
999 if (cp->restrict_lud != NULL && constraint_check_restrict(op, cp, target_entry) == 0) {
1003 /* DELETE are to be ignored beyond this point */
1004 if (( m->sml_op & LDAP_MOD_OP ) == LDAP_MOD_DELETE)
1007 for ( i = 0; b[i].bv_val; i++ ) {
1008 rc = constraint_violation( cp, &b[i], op );
1014 if (cp->set && target_entry) {
1015 if (target_entry_copy == NULL) {
1018 target_entry_copy = entry_dup(target_entry);
1020 /* if rename, set the new entry's name
1021 * (in normalized form only) */
1022 if ( op->o_tag == LDAP_REQ_MODRDN ) {
1023 struct berval pdn, ndn = BER_BVNULL;
1025 if ( op->orr_nnewSup ) {
1026 pdn = *op->orr_nnewSup;
1029 dnParent( &target_entry_copy->e_nname, &pdn );
1032 build_new_dn( &ndn, &pdn, &op->orr_nnewrdn, NULL );
1034 ber_memfree( target_entry_copy->e_nname.bv_val );
1035 target_entry_copy->e_nname = ndn;
1036 ber_bvreplace( &target_entry_copy->e_name, &ndn );
1039 /* apply modifications, in an attempt
1040 * to estimate what the entry would
1041 * look like in case all modifications
1043 for ( ml = modlist; ml; ml = ml->sml_next ) {
1044 Modification *mod = &ml->sml_mod;
1046 char textbuf[SLAP_TEXT_BUFLEN];
1047 size_t textlen = sizeof(textbuf);
1050 switch ( mod->sm_op ) {
1052 err = modify_add_values( target_entry_copy,
1053 mod, get_permissiveModify(op),
1054 &text, textbuf, textlen );
1057 case LDAP_MOD_DELETE:
1058 err = modify_delete_values( target_entry_copy,
1059 mod, get_permissiveModify(op),
1060 &text, textbuf, textlen );
1063 case LDAP_MOD_REPLACE:
1064 err = modify_replace_values( target_entry_copy,
1065 mod, get_permissiveModify(op),
1066 &text, textbuf, textlen );
1069 case LDAP_MOD_INCREMENT:
1070 err = modify_increment_values( target_entry_copy,
1071 mod, get_permissiveModify(op),
1072 &text, textbuf, textlen );
1075 case SLAP_MOD_SOFTADD:
1076 mod->sm_op = LDAP_MOD_ADD;
1077 err = modify_add_values( target_entry_copy,
1078 mod, get_permissiveModify(op),
1079 &text, textbuf, textlen );
1080 mod->sm_op = SLAP_MOD_SOFTADD;
1081 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
1086 case SLAP_MOD_SOFTDEL:
1087 mod->sm_op = LDAP_MOD_ADD;
1088 err = modify_delete_values( target_entry_copy,
1089 mod, get_permissiveModify(op),
1090 &text, textbuf, textlen );
1091 mod->sm_op = SLAP_MOD_SOFTDEL;
1092 if ( err == LDAP_NO_SUCH_ATTRIBUTE ) {
1097 case SLAP_MOD_ADD_IF_NOT_PRESENT:
1098 if ( attr_find( target_entry_copy->e_attrs, mod->sm_desc ) ) {
1102 mod->sm_op = LDAP_MOD_ADD;
1103 err = modify_add_values( target_entry_copy,
1104 mod, get_permissiveModify(op),
1105 &text, textbuf, textlen );
1106 mod->sm_op = SLAP_MOD_ADD_IF_NOT_PRESENT;
1114 if ( err != LDAP_SUCCESS ) {
1121 if ( acl_match_set(&cp->val, op, target_entry_copy, NULL) == 0) {
1122 rc = LDAP_CONSTRAINT_VIOLATION;
1130 op->o_bd = on->on_info->oi_origdb;
1131 be_entry_release_r(op, target_entry);
1135 if (target_entry_copy) {
1136 entry_free(target_entry_copy);
1139 return SLAP_CB_CONTINUE;
1144 op->o_bd = on->on_info->oi_origdb;
1145 be_entry_release_r(op, target_entry);
1149 if (target_entry_copy) {
1150 entry_free(target_entry_copy);
1153 op->o_bd->bd_info = (BackendInfo *)(on->on_info);
1154 if ( rc == LDAP_CONSTRAINT_VIOLATION ) {
1155 msg = print_message( &rsv, m->sml_desc );
1157 send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION, msg );
1159 return (rs->sr_err);
1167 slap_overinst *on = (slap_overinst *) be->bd_info;
1168 constraint *ap, *a2;
1170 for ( ap = on->on_bi.bi_private; ap; ap = a2 ) {
1172 constraint_free( ap, 1 );
1178 static slap_overinst constraint_ovl;
1180 #if SLAPD_OVER_CONSTRAINT == SLAPD_MOD_DYNAMIC
1184 constraint_initialize( void ) {
1187 constraint_ovl.on_bi.bi_type = "constraint";
1188 constraint_ovl.on_bi.bi_db_close = constraint_close;
1189 constraint_ovl.on_bi.bi_op_add = constraint_add;
1190 constraint_ovl.on_bi.bi_op_modify = constraint_update;
1191 constraint_ovl.on_bi.bi_op_modrdn = constraint_update;
1193 constraint_ovl.on_bi.bi_private = NULL;
1195 constraint_ovl.on_bi.bi_cf_ocs = constraintocs;
1196 rc = config_register_schema( constraintcfg, constraintocs );
1199 return overlay_register( &constraint_ovl );
1202 #if SLAPD_OVER_CONSTRAINT == SLAPD_MOD_DYNAMIC
1203 int init_module(int argc, char *argv[]) {
1204 return constraint_initialize();
1208 #endif /* defined(SLAPD_OVER_CONSTRAINT) */