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