Debian Codegraph @

servers/slapd/overlays/constraint.c

raw
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 <[email protected]>
18 * Emmanuel Dreyfus <[email protected]>
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 "slap-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 NEG_REGEX_STR "negregex"
44 #define URI_STR "uri"
45 #define SET_STR "set"
46 #define SIZE_STR "size"
47 #define COUNT_STR "count"
48
49 /*
50 * Linked list of attribute constraints which we should enforce.
51 * This is probably a sub optimal structure - some form of sorted
52 * array would be better if the number of attributes constrained is
53 * likely to be much bigger than 4 or 5. We stick with a list for
54 * the moment.
55 */
56
57 typedef struct constraint {
58 struct constraint *ap_next;
59 AttributeDescription **ap;
60
61 LDAPURLDesc *restrict_lud;
62 struct berval restrict_ndn;
63 Filter *restrict_filter;
64 struct berval restrict_val;
65
66 int type;
67 regex_t *re;
68 LDAPURLDesc *lud;
69 int set;
70 size_t size;
71 size_t count;
72 AttributeDescription **attrs;
73 struct berval val; /* constraint value */
74 struct berval dn;
75 struct berval filter;
76 } constraint;
77
78 enum {
79 CONSTRAINT_ATTRIBUTE = 1,
80 CONSTRAINT_COUNT,
81 CONSTRAINT_SIZE,
82 CONSTRAINT_REGEX,
83 CONSTRAINT_NEG_REGEX,
84 CONSTRAINT_SET,
85 CONSTRAINT_URI,
86 };
87
88 static ConfigDriver constraint_cf_gen;
89
90 static ConfigTable constraintcfg[] = {
91 { "constraint_attribute", "attribute[list]> (regex|negregex|uri|set|size|count) <value> [<restrict URI>]",
92 4, 0, 0, ARG_MAGIC | CONSTRAINT_ATTRIBUTE, constraint_cf_gen,
93 "( OLcfgOvAt:13.1 NAME 'olcConstraintAttribute' "
94 "DESC 'constraint for list of attributes' "
95 "EQUALITY caseIgnoreMatch "
96 "SYNTAX OMsDirectoryString )", NULL, NULL },
97 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
98 };
99
100 static ConfigOCs constraintocs[] = {
101 { "( OLcfgOvOc:13.1 "
102 "NAME 'olcConstraintConfig' "
103 "DESC 'Constraint overlay configuration' "
104 "SUP olcOverlayConfig "
105 "MAY ( olcConstraintAttribute ) )",
106 Cft_Overlay, constraintcfg },
107 { NULL, 0, NULL }
108 };
109
110 static void
111 constraint_free( constraint *cp, int freeme )
112 {
113 if (cp->restrict_lud)
114 ldap_free_urldesc(cp->restrict_lud);
115 if (!BER_BVISNULL(&cp->restrict_ndn))
116 ch_free(cp->restrict_ndn.bv_val);
117 if (cp->restrict_filter != NULL && cp->restrict_filter != slap_filter_objectClass_pres)
118 filter_free(cp->restrict_filter);
119 if (!BER_BVISNULL(&cp->restrict_val))
120 ch_free(cp->restrict_val.bv_val);
121 if (cp->re) {
122 regfree(cp->re);
123 ch_free(cp->re);
124 }
125 if (!BER_BVISNULL(&cp->val))
126 ch_free(cp->val.bv_val);
127 if (cp->lud)
128 ldap_free_urldesc(cp->lud);
129 if (cp->attrs)
130 ch_free(cp->attrs);
131 if (cp->ap)
132 ch_free(cp->ap);
133 if (freeme)
134 ch_free(cp);
135 }
136
137 static int
138 constraint_cf_gen( ConfigArgs *c )
139 {
140 slap_overinst *on = (slap_overinst *)(c->bi);
141 constraint *cn = on->on_bi.bi_private, *cp;
142 struct berval bv;
143 int i, rc = 0;
144 constraint ap = { NULL };
145 const char *text = NULL;
146
147 switch ( c->op ) {
148 case SLAP_CONFIG_EMIT:
149 switch (c->type) {
150 case CONSTRAINT_ATTRIBUTE:
151 for (cp=cn; cp; cp=cp->ap_next) {
152 char *s;
153 char *tstr = NULL;
154 int quotes = 0, numeric = 0;
155 int j;
156 size_t val;
157 char val_buf[SLAP_TEXT_BUFLEN] = { '\0' };
158
159 bv.bv_len = STRLENOF(" ");
160 for (j = 0; cp->ap[j]; j++) {
161 bv.bv_len += cp->ap[j]->ad_cname.bv_len;
162 }
163
164 /* room for commas */
165 bv.bv_len += j - 1;
166
167 switch (cp->type) {
168 case CONSTRAINT_COUNT:
169 tstr = COUNT_STR;
170 val = cp->count;
171 numeric = 1;
172 break;
173 case CONSTRAINT_SIZE:
174 tstr = SIZE_STR;
175 val = cp->size;
176 numeric = 1;
177 break;
178 case CONSTRAINT_REGEX:
179 tstr = REGEX_STR;
180 quotes = 1;
181 break;
182 case CONSTRAINT_NEG_REGEX:
183 tstr = NEG_REGEX_STR;
184 quotes = 1;
185 break;
186 case CONSTRAINT_SET:
187 tstr = SET_STR;
188 quotes = 1;
189 break;
190 case CONSTRAINT_URI:
191 tstr = URI_STR;
192 quotes = 1;
193 break;
194 default:
195 abort();
196 }
197
198 bv.bv_len += strlen(tstr);
199 bv.bv_len += cp->val.bv_len + 2*quotes;
200
201 if (cp->restrict_lud != NULL) {
202 bv.bv_len += cp->restrict_val.bv_len + STRLENOF(" restrict=\"\"");
203 }
204
205 if (numeric) {
206 int len = snprintf(val_buf, sizeof(val_buf), "%zu", val);
207 if (len <= 0) {
208 /* error */
209 return -1;
210 }
211 bv.bv_len += len;
212 }
213
214 s = bv.bv_val = ch_malloc(bv.bv_len + 1);
215
216 s = lutil_strncopy( s, cp->ap[0]->ad_cname.bv_val, cp->ap[0]->ad_cname.bv_len );
217 for (j = 1; cp->ap[j]; j++) {
218 *s++ = ',';
219 s = lutil_strncopy( s, cp->ap[j]->ad_cname.bv_val, cp->ap[j]->ad_cname.bv_len );
220 }
221 *s++ = ' ';
222 s = lutil_strcopy( s, tstr );
223 *s++ = ' ';
224 if (numeric) {
225 s = lutil_strcopy( s, val_buf );
226 } else {
227 if ( quotes ) *s++ = '"';
228 s = lutil_strncopy( s, cp->val.bv_val, cp->val.bv_len );
229 if ( quotes ) *s++ = '"';
230 }
231 if (cp->restrict_lud != NULL) {
232 s = lutil_strcopy( s, " restrict=\"" );
233 s = lutil_strncopy( s, cp->restrict_val.bv_val, cp->restrict_val.bv_len );
234 *s++ = '"';
235 }
236 *s = '\0';
237
238 rc = value_add_one( &c->rvalue_vals, &bv );
239 if (rc == LDAP_SUCCESS)
240 rc = value_add_one( &c->rvalue_nvals, &bv );
241 ch_free(bv.bv_val);
242 if (rc) return rc;
243 }
244 break;
245 default:
246 abort();
247 break;
248 }
249 break;
250 case LDAP_MOD_DELETE:
251 switch (c->type) {
252 case CONSTRAINT_ATTRIBUTE:
253 if (!cn) break; /* nothing to do */
254
255 if (c->valx < 0) {
256 /* zap all constraints */
257 while (cn) {
258 cp = cn->ap_next;
259 constraint_free( cn, 1 );
260 cn = cp;
261 }
262
263 on->on_bi.bi_private = NULL;
264 } else {
265 constraint **cpp;
266
267 /* zap constraint numbered 'valx' */
268 for(i=0, cp = cn, cpp = &cn;
269 (cp) && (i<c->valx);
270 i++, cpp = &cp->ap_next, cp = *cpp);
271
272 if (cp) {
273 /* zap cp, and join cpp to cp->ap_next */
274 *cpp = cp->ap_next;
275 constraint_free( cp, 1 );
276 }
277 on->on_bi.bi_private = cn;
278 }
279 break;
280
281 default:
282 abort();
283 break;
284 }
285 break;
286 case SLAP_CONFIG_ADD:
287 case LDAP_MOD_ADD:
288 switch (c->type) {
289 case CONSTRAINT_ATTRIBUTE: {
290 int j;
291 char **attrs = ldap_str2charray( c->argv[1], "," );
292
293 for ( j = 0; attrs[j]; j++)
294 /* just count */ ;
295 ap.ap = ch_calloc( sizeof(AttributeDescription*), j + 1 );
296 for ( j = 0; attrs[j]; j++) {
297 if ( slap_str2ad( attrs[j], &ap.ap[j], &text ) ) {
298 snprintf( c->cr_msg, sizeof( c->cr_msg ),
299 "%s <%s>: %s\n", c->argv[0], attrs[j], text );
300 rc = ARG_BAD_CONF;
301 goto done;
302 }
303 }
304
305 int is_regex = strcasecmp( c->argv[2], REGEX_STR ) == 0;
306 int is_neg_regex = strcasecmp( c->argv[2], NEG_REGEX_STR ) == 0;
307 if ( is_regex || is_neg_regex ) {
308 int err;
309
310 ap.type = is_regex ? CONSTRAINT_REGEX : CONSTRAINT_NEG_REGEX;
311 ap.re = ch_malloc( sizeof(regex_t) );
312 if ((err = regcomp( ap.re,
313 c->argv[3], REG_EXTENDED )) != 0) {
314 char errmsg[1024];
315
316 regerror( err, ap.re, errmsg, sizeof(errmsg) );
317 ch_free(ap.re);
318 snprintf( c->cr_msg, sizeof( c->cr_msg ),
319 "%s %s: Illegal regular expression \"%s\": Error %s",
320 c->argv[0], c->argv[1], c->argv[3], errmsg);
321 ap.re = NULL;
322 rc = ARG_BAD_CONF;
323 goto done;
324 }
325 ber_str2bv( c->argv[3], 0, 1, &ap.val );
326 } else if ( strcasecmp( c->argv[2], SIZE_STR ) == 0 ) {
327 size_t size;
328 char *endptr;
329
330 ap.type = CONSTRAINT_SIZE;
331 ap.size = strtoull(c->argv[3], &endptr, 10);
332 if ( *endptr )
333 rc = ARG_BAD_CONF;
334 } else if ( strcasecmp( c->argv[2], COUNT_STR ) == 0 ) {
335 size_t count;
336 char *endptr;
337
338 ap.type = CONSTRAINT_COUNT;
339 ap.count = strtoull(c->argv[3], &endptr, 10);
340 if ( *endptr )
341 rc = ARG_BAD_CONF;
342 } else if ( strcasecmp( c->argv[2], URI_STR ) == 0 ) {
343 int err;
344
345 ap.type = CONSTRAINT_URI;
346 err = ldap_url_parse(c->argv[3], &ap.lud);
347 if ( err != LDAP_URL_SUCCESS ) {
348 snprintf( c->cr_msg, sizeof( c->cr_msg ),
349 "%s %s: Invalid URI \"%s\"",
350 c->argv[0], c->argv[1], c->argv[3]);
351 rc = ARG_BAD_CONF;
352 goto done;
353 }
354
355 if (ap.lud->lud_host != NULL) {
356 snprintf( c->cr_msg, sizeof( c->cr_msg ),
357 "%s %s: unsupported hostname in URI \"%s\"",
358 c->argv[0], c->argv[1], c->argv[3]);
359 ldap_free_urldesc(ap.lud);
360 rc = ARG_BAD_CONF;
361 goto done;
362 }
363
364 for ( i=0; ap.lud->lud_attrs[i]; i++);
365 /* FIXME: This is worthless without at least one attr */
366 if ( i ) {
367 ap.attrs = ch_malloc( (i+1)*sizeof(AttributeDescription *));
368 for ( i=0; ap.lud->lud_attrs[i]; i++) {
369 ap.attrs[i] = NULL;
370 if ( slap_str2ad( ap.lud->lud_attrs[i], &ap.attrs[i], &text ) ) {
371 ch_free( ap.attrs );
372 ap.attrs = NULL;
373 snprintf( c->cr_msg, sizeof( c->cr_msg ),
374 "%s <%s>: %s\n", c->argv[0], ap.lud->lud_attrs[i], text );
375 rc = ARG_BAD_CONF;
376 goto done;
377 }
378 }
379 ap.attrs[i] = NULL;
380 }
381
382 if (ap.lud->lud_dn == NULL) {
383 ap.lud->lud_dn = ch_strdup("");
384 } else {
385 struct berval dn, ndn;
386
387 ber_str2bv( ap.lud->lud_dn, 0, 0, &dn );
388 if (dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL ) ) {
389 /* cleanup */
390 snprintf( c->cr_msg, sizeof( c->cr_msg ),
391 "%s %s: URI %s DN normalization failed",
392 c->argv[0], c->argv[1], c->argv[3] );
393 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
394 "%s: %s\n", c->log, c->cr_msg );
395 rc = ARG_BAD_CONF;
396 goto done;
397 }
398 ldap_memfree( ap.lud->lud_dn );
399 ap.lud->lud_dn = ndn.bv_val;
400 }
401
402 if (ap.lud->lud_filter == NULL) {
403 ap.lud->lud_filter = ch_strdup("objectClass=*");
404 } else if ( ap.lud->lud_filter[0] == '(' ) {
405 ber_len_t len = strlen( ap.lud->lud_filter );
406 if ( ap.lud->lud_filter[len - 1] != ')' ) {
407 snprintf( c->cr_msg, sizeof( c->cr_msg ),
408 "%s %s: invalid URI filter: %s",
409 c->argv[0], c->argv[1], ap.lud->lud_filter );
410 rc = ARG_BAD_CONF;
411 goto done;
412 }
413 AC_MEMCPY( &ap.lud->lud_filter[0], &ap.lud->lud_filter[1], len - 2 );
414 ap.lud->lud_filter[len - 2] = '\0';
415 }
416
417 ber_str2bv( c->argv[3], 0, 1, &ap.val );
418
419 } else if ( strcasecmp( c->argv[2], SET_STR ) == 0 ) {
420 ap.set = 1;
421 ber_str2bv( c->argv[3], 0, 1, &ap.val );
422 ap.type = CONSTRAINT_SET;
423
424 } else {
425 snprintf( c->cr_msg, sizeof( c->cr_msg ),
426 "%s %s: Unknown constraint type: %s",
427 c->argv[0], c->argv[1], c->argv[2] );
428 rc = ARG_BAD_CONF;
429 goto done;
430 }
431
432 if ( c->argc > 4 ) {
433 int argidx;
434
435 for ( argidx = 4; argidx < c->argc; argidx++ ) {
436 if ( strncasecmp( c->argv[argidx], "restrict=", STRLENOF("restrict=") ) == 0 ) {
437 int err;
438 char *arg = c->argv[argidx] + STRLENOF("restrict=");
439
440 err = ldap_url_parse(arg, &ap.restrict_lud);
441 if ( err != LDAP_URL_SUCCESS ) {
442 snprintf( c->cr_msg, sizeof( c->cr_msg ),
443 "%s %s: Invalid restrict URI \"%s\"",
444 c->argv[0], c->argv[1], arg);
445 rc = ARG_BAD_CONF;
446 goto done;
447 }
448
449 if (ap.restrict_lud->lud_host != NULL) {
450 snprintf( c->cr_msg, sizeof( c->cr_msg ),
451 "%s %s: unsupported hostname in restrict URI \"%s\"",
452 c->argv[0], c->argv[1], arg);
453 rc = ARG_BAD_CONF;
454 goto done;
455 }
456
457 if ( ap.restrict_lud->lud_attrs != NULL ) {
458 if ( ap.restrict_lud->lud_attrs[0] != NULL ) {
459 snprintf( c->cr_msg, sizeof( c->cr_msg ),
460 "%s %s: attrs not allowed in restrict URI %s\n",
461 c->argv[0], c->argv[1], arg);
462 rc = ARG_BAD_CONF;
463 goto done;
464 }
465 ldap_memvfree((void *)ap.restrict_lud->lud_attrs);
466 ap.restrict_lud->lud_attrs = NULL;
467 }
468
469 if (ap.restrict_lud->lud_dn != NULL) {
470 if (ap.restrict_lud->lud_dn[0] == '\0') {
471 ldap_memfree(ap.restrict_lud->lud_dn);
472 ap.restrict_lud->lud_dn = NULL;
473
474 } else {
475 struct berval dn, ndn;
476 int j;
477
478 ber_str2bv(ap.restrict_lud->lud_dn, 0, 0, &dn);
479 if (dnNormalize(0, NULL, NULL, &dn, &ndn, NULL)) {
480 /* cleanup */
481 snprintf( c->cr_msg, sizeof( c->cr_msg ),
482 "%s %s: restrict URI %s DN normalization failed",
483 c->argv[0], c->argv[1], arg );
484 rc = ARG_BAD_CONF;
485 goto done;
486 }
487
488 assert(c->be != NULL);
489 if (c->be->be_nsuffix == NULL) {
490 snprintf( c->cr_msg, sizeof( c->cr_msg ),
491 "%s %s: restrict URI requires suffix",
492 c->argv[0], c->argv[1] );
493 rc = ARG_BAD_CONF;
494 goto done;
495 }
496
497 for ( j = 0; !BER_BVISNULL(&c->be->be_nsuffix[j]); j++) {
498 if (dnIsSuffix(&ndn, &c->be->be_nsuffix[j])) break;
499 }
500
501 if (BER_BVISNULL(&c->be->be_nsuffix[j])) {
502 /* error */
503 snprintf( c->cr_msg, sizeof( c->cr_msg ),
504 "%s %s: restrict URI DN %s not within database naming context(s)",
505 c->argv[0], c->argv[1], dn.bv_val );
506 rc = ARG_BAD_CONF;
507 goto done;
508 }
509
510 ap.restrict_ndn = ndn;
511 }
512 }
513
514 if (ap.restrict_lud->lud_filter != NULL) {
515 ap.restrict_filter = str2filter(ap.restrict_lud->lud_filter);
516 if (ap.restrict_filter == NULL) {
517 /* error */
518 snprintf( c->cr_msg, sizeof( c->cr_msg ),
519 "%s %s: restrict URI filter %s invalid",
520 c->argv[0], c->argv[1], ap.restrict_lud->lud_filter );
521 rc = ARG_BAD_CONF;
522 goto done;
523 }
524 }
525
526 ber_str2bv(c->argv[argidx] + STRLENOF("restrict="), 0, 1, &ap.restrict_val);
527
528 } else {
529 /* cleanup */
530 snprintf( c->cr_msg, sizeof( c->cr_msg ),
531 "%s %s: unrecognized arg #%d (%s)",
532 c->argv[0], c->argv[1], argidx, c->argv[argidx] );
533 rc = ARG_BAD_CONF;
534 goto done;
535 }
536 }
537 }
538
539 done:;
540 if ( rc == LDAP_SUCCESS ) {
541 constraint **app, *a2 = ch_calloc( sizeof(constraint), 1 );
542
543 a2->ap = ap.ap;
544 a2->type = ap.type;
545 a2->re = ap.re;
546 a2->val = ap.val;
547 a2->lud = ap.lud;
548 a2->set = ap.set;
549 a2->size = ap.size;
550 a2->count = ap.count;
551 if ( a2->lud ) {
552 ber_str2bv(a2->lud->lud_dn, 0, 0, &a2->dn);
553 ber_str2bv(a2->lud->lud_filter, 0, 0, &a2->filter);
554 }
555 a2->attrs = ap.attrs;
556 a2->restrict_lud = ap.restrict_lud;
557 a2->restrict_ndn = ap.restrict_ndn;
558 a2->restrict_filter = ap.restrict_filter;
559 a2->restrict_val = ap.restrict_val;
560
561 for ( app = (constraint **)&on->on_bi.bi_private; *app; app = &(*app)->ap_next )
562 /* Get to the end */ ;
563
564 a2->ap_next = *app;
565 *app = a2;
566
567 } else {
568 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
569 "%s: %s\n", c->log, c->cr_msg );
570 constraint_free( &ap, 0 );
571 }
572
573 ldap_memvfree((void**)attrs);
574 } break;
575 default:
576 abort();
577 break;
578 }
579 break;
580 default:
581 abort();
582 }
583
584 return rc;
585 }
586
587 static int
588 constraint_uri_cb( Operation *op, SlapReply *rs )
589 {
590 if(rs->sr_type == REP_SEARCH) {
591 int *foundp = op->o_callback->sc_private;
592
593 *foundp = 1;
594
595 Debug(LDAP_DEBUG_TRACE, "==> constraint_uri_cb <%s>\n",
596 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "UNKNOWN_DN" );
597 }
598 return 0;
599 }
600
601 static int
602 constraint_violation( constraint *c, struct berval *bv, Operation *op )
603 {
604 if ((!c) || (!bv)) return LDAP_SUCCESS;
605
606 switch (c->type) {
607 case CONSTRAINT_SIZE:
608 if (bv->bv_len > c->size)
609 return LDAP_CONSTRAINT_VIOLATION; /* size violation */
610 break;
611 case CONSTRAINT_REGEX:
612 if (regexec(c->re, bv->bv_val, 0, NULL, 0) == REG_NOMATCH)
613 return LDAP_CONSTRAINT_VIOLATION; /* regular expression violation */
614 break;
615 case CONSTRAINT_NEG_REGEX:
616 if (regexec(c->re, bv->bv_val, 0, NULL, 0) != REG_NOMATCH)
617 return LDAP_CONSTRAINT_VIOLATION; /* regular expression violation */
618 break;
619 case CONSTRAINT_URI: {
620 Operation nop = *op;
621 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
622 slap_callback cb = { 0 };
623 int i;
624 int found = 0;
625 int rc;
626 size_t len;
627 struct berval filterstr;
628 char *ptr;
629
630 cb.sc_response = constraint_uri_cb;
631 cb.sc_private = &found;
632
633 nop.o_protocol = LDAP_VERSION3;
634 nop.o_tag = LDAP_REQ_SEARCH;
635 nop.o_time = slap_get_time();
636 if (c->lud->lud_dn) {
637 struct berval dn;
638
639 ber_str2bv(c->lud->lud_dn, 0, 0, &dn);
640 nop.o_req_dn = dn;
641 nop.o_req_ndn = dn;
642 nop.o_bd = select_backend(&nop.o_req_ndn, 1 );
643 if (!nop.o_bd) {
644 return LDAP_NO_SUCH_OBJECT; /* unexpected error */
645 }
646 if (!nop.o_bd->be_search) {
647 return LDAP_OTHER; /* unexpected error */
648 }
649 } else {
650 nop.o_req_dn = nop.o_bd->be_nsuffix[0];
651 nop.o_req_ndn = nop.o_bd->be_nsuffix[0];
652 nop.o_bd = on->on_info->oi_origdb;
653 }
654 nop.o_do_not_cache = 1;
655 nop.o_callback = &cb;
656
657 nop.ors_scope = c->lud->lud_scope;
658 nop.ors_deref = LDAP_DEREF_NEVER;
659 nop.ors_slimit = SLAP_NO_LIMIT;
660 nop.ors_tlimit = SLAP_NO_LIMIT;
661 nop.ors_limit = NULL;
662
663 nop.ors_attrsonly = 0;
664 nop.ors_attrs = slap_anlist_no_attrs;
665
666 len = STRLENOF("(&(") +
667 c->filter.bv_len +
668 STRLENOF(")(|");
669
670 for (i = 0; c->attrs[i]; i++) {
671 len += STRLENOF("(") +
672 c->attrs[i]->ad_cname.bv_len +
673 STRLENOF("=") +
674 bv->bv_len +
675 STRLENOF(")");
676 }
677
678 len += STRLENOF("))");
679 filterstr.bv_len = len;
680 filterstr.bv_val = op->o_tmpalloc(len + 1, op->o_tmpmemctx);
681
682 ptr = filterstr.bv_val +
683 snprintf(filterstr.bv_val, len, "(&(%s)(|", c->lud->lud_filter);
684 for (i = 0; c->attrs[i]; i++) {
685 *ptr++ = '(';
686 ptr = lutil_strcopy( ptr, c->attrs[i]->ad_cname.bv_val );
687 *ptr++ = '=';
688 ptr = lutil_strcopy( ptr, bv->bv_val );
689 *ptr++ = ')';
690 }
691 *ptr++ = ')';
692 *ptr++ = ')';
693 *ptr++ = '\0';
694
695 nop.ors_filterstr = filterstr;
696 nop.ors_filter = str2filter_x(&nop, filterstr.bv_val);
697 if ( nop.ors_filter == NULL ) {
698 Debug( LDAP_DEBUG_ANY,
699 "%s constraint_violation uri filter=\"%s\" invalid\n",
700 op->o_log_prefix, filterstr.bv_val );
701 rc = LDAP_OTHER;
702
703 } else {
704 SlapReply nrs = { REP_RESULT };
705
706 Debug(LDAP_DEBUG_TRACE,
707 "==> constraint_violation uri filter = %s\n",
708 filterstr.bv_val );
709
710 rc = nop.o_bd->be_search( &nop, &nrs );
711
712 Debug(LDAP_DEBUG_TRACE,
713 "==> constraint_violation uri rc = %d, found = %d\n",
714 rc, found );
715 }
716 op->o_tmpfree(filterstr.bv_val, op->o_tmpmemctx);
717
718 if ((rc != LDAP_SUCCESS) && (rc != LDAP_NO_SUCH_OBJECT)) {
719 return rc; /* unexpected error */
720 }
721
722 if (!found)
723 return LDAP_CONSTRAINT_VIOLATION; /* constraint violation */
724 break;
725 }
726 }
727
728 return LDAP_SUCCESS;
729 }
730
731 static char *
732 print_message( struct berval *errtext, AttributeDescription *a )
733 {
734 char *ret;
735 int sz;
736
737 sz = errtext->bv_len + sizeof(" on ") + a->ad_cname.bv_len;
738 ret = ch_malloc(sz);
739 snprintf( ret, sz, "%s on %s", errtext->bv_val, a->ad_cname.bv_val );
740 return ret;
741 }
742
743 static unsigned
744 constraint_count_attr(Entry *e, AttributeDescription *ad)
745 {
746 struct Attribute *a;
747
748 if ((a = attr_find(e->e_attrs, ad)) != NULL)
749 return a->a_numvals;
750 return 0;
751 }
752
753 static int
754 constraint_check_restrict( Operation *op, constraint *c, Entry *e )
755 {
756 assert( c->restrict_lud != NULL );
757
758 if ( c->restrict_lud->lud_dn != NULL ) {
759 int diff = e->e_nname.bv_len - c->restrict_ndn.bv_len;
760
761 if ( diff < 0 ) {
762 return 0;
763 }
764
765 if ( c->restrict_lud->lud_scope == LDAP_SCOPE_BASE ) {
766 return bvmatch( &e->e_nname, &c->restrict_ndn );
767 }
768
769 if ( !dnIsSuffix( &e->e_nname, &c->restrict_ndn ) ) {
770 return 0;
771 }
772
773 if ( c->restrict_lud->lud_scope != LDAP_SCOPE_SUBTREE ) {
774 struct berval pdn;
775
776 if ( diff == 0 ) {
777 return 0;
778 }
779
780 dnParent( &e->e_nname, &pdn );
781
782 if ( c->restrict_lud->lud_scope == LDAP_SCOPE_ONELEVEL
783 && pdn.bv_len != c->restrict_ndn.bv_len )
784 {
785 return 0;
786 }
787 }
788 }
789
790 if ( c->restrict_filter != NULL ) {
791 int rc;
792 struct berval save_dn = op->o_dn, save_ndn = op->o_ndn;
793
794 op->o_dn = op->o_bd->be_rootdn;
795 op->o_ndn = op->o_bd->be_rootndn;
796 rc = test_filter( op, e, c->restrict_filter );
797 op->o_dn = save_dn;
798 op->o_ndn = save_ndn;
799
800 if ( rc != LDAP_COMPARE_TRUE ) {
801 return 0;
802 }
803 }
804
805 return 1;
806 }
807
808 static int
809 constraint_add( Operation *op, SlapReply *rs )
810 {
811 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
812 Attribute *a;
813 constraint *c = on->on_bi.bi_private, *cp;
814 BerVarray b = NULL;
815 int i;
816 struct berval rsv = BER_BVC("add breaks constraint");
817 int rc = 0;
818 char *msg = NULL;
819
820 if ( get_relax(op) || be_shadow_update( op ) ) {
821 return SLAP_CB_CONTINUE;
822 }
823
824 if ((a = op->ora_e->e_attrs) == NULL) {
825 op->o_bd->bd_info = (BackendInfo *)(on->on_info);
826 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
827 "constraint_add: no attrs");
828 return(rs->sr_err);
829 }
830
831 for(; a; a = a->a_next ) {
832 /* we don't constrain operational attributes */
833 if (is_at_operational(a->a_desc->ad_type)) continue;
834
835 for(cp = c; cp; cp = cp->ap_next) {
836 int j;
837 for (j = 0; cp->ap[j]; j++) {
838 if (cp->ap[j] == a->a_desc) break;
839 }
840 if (cp->ap[j] == NULL) continue;
841 if ((b = a->a_vals) == NULL) continue;
842
843 if (cp->restrict_lud != NULL && constraint_check_restrict(op, cp, op->ora_e) == 0) {
844 continue;
845 }
846
847 Debug(LDAP_DEBUG_TRACE,
848 "==> constraint_add, "
849 "a->a_numvals = %u, cp->count = %lu\n",
850 a->a_numvals, (unsigned long) cp->count );
851
852 switch (cp->type) {
853 case CONSTRAINT_COUNT:
854 if (a->a_numvals > cp->count)
855 rc = LDAP_CONSTRAINT_VIOLATION;
856 break;
857 case CONSTRAINT_SET:
858 if (acl_match_set(&cp->val, op, op->ora_e, NULL) == 0)
859 rc = LDAP_CONSTRAINT_VIOLATION;
860 break;
861 default:
862 for ( i = 0; b[i].bv_val; i++ ) {
863 rc = constraint_violation( cp, &b[i], op );
864 if ( rc ) {
865 goto add_violation;
866 }
867 }
868 }
869 if ( rc )
870 goto add_violation;
871
872 }
873 }
874
875 /* Default is to just fall through to the normal processing */
876 return SLAP_CB_CONTINUE;
877
878 add_violation:
879 op->o_bd->bd_info = (BackendInfo *)(on->on_info);
880 if (rc == LDAP_CONSTRAINT_VIOLATION ) {
881 msg = print_message( &rsv, a->a_desc );
882 }
883 send_ldap_error(op, rs, rc, msg );
884 ch_free(msg);
885 return (rs->sr_err);
886 }
887
888
889 static int
890 constraint_check_count_violation( Modifications *m, Entry *target_entry, constraint *cp )
891 {
892 BerVarray b = NULL;
893 unsigned ce = 0;
894 unsigned ca;
895 int j;
896
897 for ( j = 0; cp->ap[j]; j++ ) {
898 /* Get this attribute count */
899 if ( target_entry )
900 ce = constraint_count_attr( target_entry, cp->ap[j] );
901
902 for( ; m; m = m->sml_next ) {
903 if ( cp->ap[j] == m->sml_desc ) {
904 ca = m->sml_numvals;
905 switch ( m->sml_op ) {
906 case LDAP_MOD_DELETE:
907 case SLAP_MOD_SOFTDEL:
908 if ( !ca || ca > ce ) {
909 ce = 0;
910 } else {
911 /* No need to check for values' validity. Invalid values
912 * cause the whole transaction to die anyway. */
913 ce -= ca;
914 }
915 break;
916
917 case LDAP_MOD_ADD:
918 case SLAP_MOD_SOFTADD:
919 ce += ca;
920 break;
921
922 case LDAP_MOD_REPLACE:
923 ce = ca;
924 break;
925
926 #if 0
927 /* TODO */
928 case handle SLAP_MOD_ADD_IF_NOT_PRESENT:
929 #endif
930
931 default:
932 /* impossible! assert? */
933 return 1;
934 }
935
936 Debug(LDAP_DEBUG_TRACE,
937 "==> constraint_check_count_violation ce = %u, "
938 "ca = %u, cp->count = %lu\n",
939 ce, ca, (unsigned long) cp->count);
940 }
941 }
942 }
943
944 return ( ce > cp->count );
945 }
946
947 static int
948 constraint_update( Operation *op, SlapReply *rs )
949 {
950 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
951 Backend *be = op->o_bd;
952 constraint *c = on->on_bi.bi_private, *cp;
953 Entry *target_entry = NULL, *target_entry_copy = NULL;
954 Modifications *modlist, *m;
955 BerVarray b = NULL;
956 int i;
957 struct berval rsv = BER_BVC("modify breaks constraint");
958 int rc;
959 char *msg = NULL;
960 int is_v;
961
962 if ( get_relax(op) || be_shadow_update( op ) ) {
963 return SLAP_CB_CONTINUE;
964 }
965
966 switch ( op->o_tag ) {
967 case LDAP_REQ_MODIFY:
968 modlist = op->orm_modlist;
969 break;
970
971 case LDAP_REQ_MODRDN:
972 modlist = op->orr_modlist;
973 break;
974
975 default:
976 /* impossible! assert? */
977 return LDAP_OTHER;
978 }
979
980 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "constraint_update()\n" );
981 if ((m = modlist) == NULL) {
982 op->o_bd->bd_info = (BackendInfo *)(on->on_info);
983 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
984 "constraint_update() got null modlist");
985 return(rs->sr_err);
986 }
987
988 op->o_bd = on->on_info->oi_origdb;
989 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &target_entry );
990 op->o_bd = be;
991
992 /* let the backend send the error */
993 if ( target_entry == NULL )
994 return SLAP_CB_CONTINUE;
995
996 /* Do we need to count attributes? */
997 for(cp = c; cp; cp = cp->ap_next) {
998 if (cp->type == CONSTRAINT_COUNT) {
999 if (cp->restrict_lud && constraint_check_restrict(op, cp, target_entry) == 0) {
1000 continue;
1001 }
1002
1003 is_v = constraint_check_count_violation(m, target_entry, cp);
1004
1005 Debug(LDAP_DEBUG_TRACE,
1006 "==> constraint_update is_v: %d\n", is_v );
1007
1008 if (is_v) {
1009 rc = LDAP_CONSTRAINT_VIOLATION;
1010 goto mod_violation;
1011 }
1012 }
1013 }
1014
1015 rc = LDAP_CONSTRAINT_VIOLATION;
1016 for(;m; m = m->sml_next) {
1017 unsigned ce = 0;
1018
1019 if (is_at_operational( m->sml_desc->ad_type )) continue;
1020
1021 if ((( m->sml_op & LDAP_MOD_OP ) != LDAP_MOD_ADD) &&
1022 (( m->sml_op & LDAP_MOD_OP ) != LDAP_MOD_REPLACE) &&
1023 (( m->sml_op & LDAP_MOD_OP ) != LDAP_MOD_DELETE))
1024 continue;
1025 /* we only care about ADD and REPLACE modifications */
1026 /* and DELETE are used to track attribute count */
1027 if ((( b = m->sml_values ) == NULL ) || (b[0].bv_val == NULL))
1028 continue;
1029
1030 for(cp = c; cp; cp = cp->ap_next) {
1031 int j;
1032 for (j = 0; cp->ap[j]; j++) {
1033 if (cp->ap[j] == m->sml_desc) {
1034 break;
1035 }
1036 }
1037 if (cp->ap[j] == NULL) continue;
1038
1039 if (cp->restrict_lud != NULL && constraint_check_restrict(op, cp, target_entry) == 0) {
1040 continue;
1041 }
1042
1043 /* DELETE are to be ignored beyond this point */
1044 if (( m->sml_op & LDAP_MOD_OP ) == LDAP_MOD_DELETE)
1045 continue;
1046
1047 for ( i = 0; b[i].bv_val; i++ ) {
1048 rc = constraint_violation( cp, &b[i], op );
1049 if ( rc ) {
1050 goto mod_violation;
1051 }
1052 }
1053
1054 if (cp->type == CONSTRAINT_SET && target_entry) {
1055 if (target_entry_copy == NULL) {
1056 Modifications *ml;
1057
1058 target_entry_copy = entry_dup(target_entry);
1059
1060 /* if rename, set the new entry's name */
1061 if ( op->o_tag == LDAP_REQ_MODRDN ) {
1062 ber_bvreplace( &target_entry_copy->e_name, &op->orr_newDN );
1063 ber_bvreplace( &target_entry_copy->e_nname, &op->orr_nnewDN );
1064 }
1065
1066 /* apply modifications, in an attempt
1067 * to estimate what the entry would
1068 * look like in case all modifications
1069 * pass */
1070 for ( ml = modlist; ml; ml = ml->sml_next ) {
1071 Modification *mod = &ml->sml_mod;
1072 const char *text;
1073 char textbuf[SLAP_TEXT_BUFLEN];
1074 size_t textlen = sizeof(textbuf);
1075 int err;
1076
1077 switch ( mod->sm_op ) {
1078 case LDAP_MOD_ADD:
1079 err = modify_add_values( target_entry_copy,
1080 mod, get_permissiveModify(op),
1081 &text, textbuf, textlen );
1082 break;
1083
1084 case LDAP_MOD_DELETE:
1085 err = modify_delete_values( target_entry_copy,
1086 mod, get_permissiveModify(op),
1087 &text, textbuf, textlen );
1088 break;
1089
1090 case LDAP_MOD_REPLACE:
1091 err = modify_replace_values( target_entry_copy,
1092 mod, get_permissiveModify(op),
1093 &text, textbuf, textlen );
1094 break;
1095
1096 case LDAP_MOD_INCREMENT:
1097 err = modify_increment_values( target_entry_copy,
1098 mod, get_permissiveModify(op),
1099 &text, textbuf, textlen );
1100 break;
1101
1102 case SLAP_MOD_SOFTADD:
1103 mod->sm_op = LDAP_MOD_ADD;
1104 err = modify_add_values( target_entry_copy,
1105 mod, get_permissiveModify(op),
1106 &text, textbuf, textlen );
1107 mod->sm_op = SLAP_MOD_SOFTADD;
1108 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
1109 err = LDAP_SUCCESS;
1110 }
1111 break;
1112
1113 case SLAP_MOD_SOFTDEL:
1114 mod->sm_op = LDAP_MOD_ADD;
1115 err = modify_delete_values( target_entry_copy,
1116 mod, get_permissiveModify(op),
1117 &text, textbuf, textlen );
1118 mod->sm_op = SLAP_MOD_SOFTDEL;
1119 if ( err == LDAP_NO_SUCH_ATTRIBUTE ) {
1120 err = LDAP_SUCCESS;
1121 }
1122 break;
1123
1124 case SLAP_MOD_ADD_IF_NOT_PRESENT:
1125 if ( attr_find( target_entry_copy->e_attrs, mod->sm_desc ) ) {
1126 err = LDAP_SUCCESS;
1127 break;
1128 }
1129 mod->sm_op = LDAP_MOD_ADD;
1130 err = modify_add_values( target_entry_copy,
1131 mod, get_permissiveModify(op),
1132 &text, textbuf, textlen );
1133 mod->sm_op = SLAP_MOD_ADD_IF_NOT_PRESENT;
1134 break;
1135
1136 default:
1137 err = LDAP_OTHER;
1138 break;
1139 }
1140
1141 if ( err != LDAP_SUCCESS ) {
1142 rc = err;
1143 goto mod_violation;
1144 }
1145 }
1146 }
1147
1148 if ( acl_match_set(&cp->val, op, target_entry_copy, NULL) == 0) {
1149 rc = LDAP_CONSTRAINT_VIOLATION;
1150 goto mod_violation;
1151 }
1152 }
1153 }
1154 }
1155
1156 if (target_entry) {
1157 op->o_bd = on->on_info->oi_origdb;
1158 be_entry_release_r(op, target_entry);
1159 op->o_bd = be;
1160 }
1161
1162 if (target_entry_copy) {
1163 entry_free(target_entry_copy);
1164 }
1165
1166 return SLAP_CB_CONTINUE;
1167
1168 mod_violation:
1169 /* violation */
1170 if (target_entry) {
1171 op->o_bd = on->on_info->oi_origdb;
1172 be_entry_release_r(op, target_entry);
1173 op->o_bd = be;
1174 }
1175
1176 if (target_entry_copy) {
1177 entry_free(target_entry_copy);
1178 }
1179
1180 op->o_bd->bd_info = (BackendInfo *)(on->on_info);
1181 if ( rc == LDAP_CONSTRAINT_VIOLATION ) {
1182 msg = print_message( &rsv, m->sml_desc );
1183 }
1184 send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION, msg );
1185 ch_free(msg);
1186 return (rs->sr_err);
1187 }
1188
1189 static int
1190 constraint_destroy(
1191 BackendDB *be,
1192 ConfigReply *cr )
1193 {
1194 slap_overinst *on = (slap_overinst *) be->bd_info;
1195 constraint *ap, *a2;
1196
1197 for ( ap = on->on_bi.bi_private; ap; ap = a2 ) {
1198 a2 = ap->ap_next;
1199 constraint_free( ap, 1 );
1200 }
1201
1202 return 0;
1203 }
1204
1205 static slap_overinst constraint_ovl;
1206
1207 #if SLAPD_OVER_CONSTRAINT == SLAPD_MOD_DYNAMIC
1208 static
1209 #endif
1210 int
1211 constraint_initialize( void ) {
1212 int rc;
1213
1214 constraint_ovl.on_bi.bi_type = "constraint";
1215 constraint_ovl.on_bi.bi_flags = SLAPO_BFLAG_SINGLE;
1216 constraint_ovl.on_bi.bi_db_destroy = constraint_destroy;
1217 constraint_ovl.on_bi.bi_op_add = constraint_add;
1218 constraint_ovl.on_bi.bi_op_modify = constraint_update;
1219 constraint_ovl.on_bi.bi_op_modrdn = constraint_update;
1220
1221 constraint_ovl.on_bi.bi_private = NULL;
1222
1223 constraint_ovl.on_bi.bi_cf_ocs = constraintocs;
1224 rc = config_register_schema( constraintcfg, constraintocs );
1225 if (rc) return rc;
1226
1227 return overlay_register( &constraint_ovl );
1228 }
1229
1230 #if SLAPD_OVER_CONSTRAINT == SLAPD_MOD_DYNAMIC
1231 int init_module(int argc, char *argv[]) {
1232 return constraint_initialize();
1233 }
1234 #endif
1235
1236 #endif /* defined(SLAPD_OVER_CONSTRAINT) */
1237