Debian Codegraph @

servers/slapd/overlays/unique.c

raw
1 /* unique.c - attribute uniqueness module */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2004-2024 The OpenLDAP Foundation.
6 * Portions Copyright 2004,2006-2007 Symas Corporation.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17 /* ACKNOWLEDGEMENTS:
18 * This work was initially developed by Symas Corporation for
19 * inclusion in OpenLDAP Software, with subsequent enhancements by
20 * Emily Backes at Symas Corporation. This work was sponsored by
21 * Hewlett-Packard.
22 */
23
24 #include "portable.h"
25
26 #ifdef SLAPD_OVER_UNIQUE
27
28 #include <stdio.h>
29
30 #include <ac/string.h>
31 #include <ac/socket.h>
32
33 #include "slap.h"
34 #include "slap-config.h"
35
36 #define UNIQUE_DEFAULT_URI ("ldap:///??sub")
37
38 static slap_overinst unique;
39
40 typedef struct unique_attrs_s {
41 struct unique_attrs_s *next; /* list of attrs */
42 AttributeDescription *attr;
43 } unique_attrs;
44
45 typedef struct unique_domain_uri_s {
46 struct unique_domain_uri_s *next;
47 struct berval dn;
48 struct berval ndn;
49 struct berval filter;
50 Filter *f;
51 struct unique_attrs_s *attrs;
52 int scope;
53 } unique_domain_uri;
54
55 typedef struct unique_domain_s {
56 struct unique_domain_s *next;
57 struct berval domain_spec;
58 struct unique_domain_uri_s *uri;
59 char ignore; /* polarity of attributes */
60 char strict; /* null considered unique too */
61 char serial; /* serialize execution */
62 } unique_domain;
63
64 typedef struct unique_data_s {
65 struct unique_domain_s *domains;
66 struct unique_domain_s *legacy;
67 char legacy_strict_set;
68 ldap_pvt_thread_mutex_t serial_mutex;
69 } unique_data;
70
71 typedef struct unique_counter_s {
72 struct berval *ndn;
73 int count;
74 } unique_counter;
75
76 enum {
77 UNIQUE_BASE = 1,
78 UNIQUE_IGNORE,
79 UNIQUE_ATTR,
80 UNIQUE_STRICT,
81 UNIQUE_URI,
82 };
83
84 static ConfigDriver unique_cf_base;
85 static ConfigDriver unique_cf_attrs;
86 static ConfigDriver unique_cf_strict;
87 static ConfigDriver unique_cf_uri;
88
89 static ConfigTable uniquecfg[] = {
90 { "unique_base", "basedn", 2, 2, 0, ARG_DN|ARG_QUOTE|ARG_MAGIC|UNIQUE_BASE,
91 unique_cf_base, "( OLcfgOvAt:10.1 NAME 'olcUniqueBase' "
92 "DESC 'Subtree for uniqueness searches' "
93 "EQUALITY distinguishedNameMatch "
94 "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
95 { "unique_ignore", "attribute...", 2, 0, 0, ARG_MAGIC|UNIQUE_IGNORE,
96 unique_cf_attrs, "( OLcfgOvAt:10.2 NAME 'olcUniqueIgnore' "
97 "DESC 'Attributes for which uniqueness shall not be enforced' "
98 "EQUALITY caseIgnoreMatch "
99 "ORDERING caseIgnoreOrderingMatch "
100 "SUBSTR caseIgnoreSubstringsMatch "
101 "SYNTAX OMsDirectoryString )", NULL, NULL },
102 { "unique_attributes", "attribute...", 2, 0, 0, ARG_MAGIC|UNIQUE_ATTR,
103 unique_cf_attrs, "( OLcfgOvAt:10.3 NAME 'olcUniqueAttribute' "
104 "DESC 'Attributes for which uniqueness shall be enforced' "
105 "EQUALITY caseIgnoreMatch "
106 "ORDERING caseIgnoreOrderingMatch "
107 "SUBSTR caseIgnoreSubstringsMatch "
108 "SYNTAX OMsDirectoryString )", NULL, NULL },
109 { "unique_strict", "on|off", 1, 2, 0, ARG_MAGIC|UNIQUE_STRICT,
110 unique_cf_strict, "( OLcfgOvAt:10.4 NAME 'olcUniqueStrict' "
111 "DESC 'Enforce uniqueness of null values' "
112 "EQUALITY booleanMatch "
113 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
114 { "unique_uri", "ldapuri", 2, 3, 0, ARG_MAGIC|UNIQUE_URI,
115 unique_cf_uri, "( OLcfgOvAt:10.5 NAME 'olcUniqueURI' "
116 "DESC 'List of keywords and LDAP URIs for a uniqueness domain' "
117 "EQUALITY caseExactMatch "
118 "ORDERING caseExactOrderingMatch "
119 "SUBSTR caseExactSubstringsMatch "
120 "SYNTAX OMsDirectoryString )", NULL, NULL },
121 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
122 };
123
124 static ConfigOCs uniqueocs[] = {
125 { "( OLcfgOvOc:10.1 "
126 "NAME 'olcUniqueConfig' "
127 "DESC 'Attribute value uniqueness configuration' "
128 "SUP olcOverlayConfig "
129 "MAY ( olcUniqueBase $ olcUniqueIgnore $ "
130 "olcUniqueAttribute $ olcUniqueStrict $ "
131 "olcUniqueURI ) )",
132 Cft_Overlay, uniquecfg },
133 { NULL, 0, NULL }
134 };
135
136 static void
137 unique_free_domain_uri ( unique_domain_uri *uri )
138 {
139 unique_domain_uri *next_uri = NULL;
140 unique_attrs *attr, *next_attr = NULL;
141
142 while ( uri ) {
143 next_uri = uri->next;
144 ch_free ( uri->dn.bv_val );
145 ch_free ( uri->ndn.bv_val );
146 ch_free ( uri->filter.bv_val );
147 filter_free( uri->f );
148 attr = uri->attrs;
149 while ( attr ) {
150 next_attr = attr->next;
151 ch_free (attr);
152 attr = next_attr;
153 }
154 ch_free ( uri );
155 uri = next_uri;
156 }
157 }
158
159 /* free an entire stack of domains */
160 static void
161 unique_free_domain ( unique_domain *domain )
162 {
163 unique_domain *next_domain = NULL;
164
165 while ( domain ) {
166 next_domain = domain->next;
167 ch_free ( domain->domain_spec.bv_val );
168 unique_free_domain_uri ( domain->uri );
169 ch_free ( domain );
170 domain = next_domain;
171 }
172 }
173
174 static int
175 unique_new_domain_uri ( unique_domain_uri **urip,
176 const LDAPURLDesc *url_desc,
177 ConfigArgs *c )
178 {
179 int i, rc = LDAP_SUCCESS;
180 unique_domain_uri *uri;
181 struct berval bv = {0, NULL};
182 BackendDB *be = (BackendDB *)c->be;
183 char ** attr_str;
184 AttributeDescription * ad;
185 const char * text;
186
187 uri = ch_calloc ( 1, sizeof ( unique_domain_uri ) );
188
189 if ( url_desc->lud_host && url_desc->lud_host[0] ) {
190 snprintf( c->cr_msg, sizeof( c->cr_msg ),
191 "host <%s> not allowed in URI",
192 url_desc->lud_host );
193 rc = ARG_BAD_CONF;
194 goto exit;
195 }
196
197 if ( url_desc->lud_dn && url_desc->lud_dn[0] ) {
198 ber_str2bv( url_desc->lud_dn, 0, 0, &bv );
199 rc = dnPrettyNormal( NULL,
200 &bv,
201 &uri->dn,
202 &uri->ndn,
203 NULL );
204 if ( rc != LDAP_SUCCESS ) {
205 snprintf( c->cr_msg, sizeof( c->cr_msg ),
206 "<%s> invalid DN %d (%s)",
207 url_desc->lud_dn, rc, ldap_err2string( rc ));
208 rc = ARG_BAD_CONF;
209 goto exit;
210 }
211
212 if ( be->be_nsuffix == NULL ) {
213 snprintf( c->cr_msg, sizeof( c->cr_msg ),
214 "suffix must be set" );
215 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
216 c->cr_msg );
217 rc = ARG_BAD_CONF;
218 goto exit;
219 }
220
221 if ( !dnIsSuffix ( &uri->ndn, &be->be_nsuffix[0] ) ) {
222 snprintf( c->cr_msg, sizeof( c->cr_msg ),
223 "dn <%s> is not a suffix of backend base dn <%s>",
224 uri->dn.bv_val,
225 be->be_nsuffix[0].bv_val );
226 rc = ARG_BAD_CONF;
227 goto exit;
228 }
229
230 if ( BER_BVISNULL( &be->be_rootndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
231 Debug( LDAP_DEBUG_ANY,
232 "slapo-unique needs a rootdn; "
233 "backend <%s> has none, YMMV.\n",
234 be->be_nsuffix[0].bv_val );
235 }
236 }
237
238 attr_str = url_desc->lud_attrs;
239 if ( attr_str ) {
240 for ( i=0; attr_str[i]; ++i ) {
241 unique_attrs * attr;
242 ad = NULL;
243 if ( slap_str2ad ( attr_str[i], &ad, &text )
244 == LDAP_SUCCESS) {
245 attr = ch_calloc ( 1,
246 sizeof ( unique_attrs ) );
247 attr->attr = ad;
248 attr->next = uri->attrs;
249 uri->attrs = attr;
250 } else {
251 snprintf( c->cr_msg, sizeof( c->cr_msg ),
252 "unique: attribute: %s: %s",
253 attr_str[i], text );
254 rc = ARG_BAD_CONF;
255 goto exit;
256 }
257 }
258 }
259
260 uri->scope = url_desc->lud_scope;
261 if ( !uri->scope ) {
262 snprintf( c->cr_msg, sizeof( c->cr_msg ),
263 "unique: uri with base scope will always be unique");
264 rc = ARG_BAD_CONF;
265 goto exit;
266 }
267
268 if (url_desc->lud_filter) {
269 char *ptr;
270 uri->f = str2filter( url_desc->lud_filter );
271 if ( !uri->f ) {
272 snprintf( c->cr_msg, sizeof( c->cr_msg ),
273 "unique: bad filter");
274 rc = ARG_BAD_CONF;
275 goto exit;
276 }
277 /* make sure the strfilter is in normal form (ITS#5581) */
278 filter2bv( uri->f, &uri->filter );
279 ptr = strstr( uri->filter.bv_val, "(?=" /*)*/ );
280 if ( ptr != NULL && ptr <= ( uri->filter.bv_val - STRLENOF( "(?=" /*)*/ ) + uri->filter.bv_len ) )
281 {
282 snprintf( c->cr_msg, sizeof( c->cr_msg ),
283 "unique: bad filter");
284 rc = ARG_BAD_CONF;
285 goto exit;
286 }
287 }
288 exit:
289 uri->next = *urip;
290 *urip = uri;
291 if ( rc ) {
292 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
293 "%s: %s\n", c->log, c->cr_msg );
294 unique_free_domain_uri ( uri );
295 *urip = NULL;
296 }
297 return rc;
298 }
299
300 static int
301 unique_new_domain_uri_basic ( unique_domain_uri **urip,
302 ConfigArgs *c )
303 {
304 LDAPURLDesc *url_desc = NULL;
305 int rc;
306
307 rc = ldap_url_parse ( UNIQUE_DEFAULT_URI, &url_desc );
308 if ( rc ) return rc;
309 rc = unique_new_domain_uri ( urip, url_desc, c );
310 ldap_free_urldesc ( url_desc );
311 return rc;
312 }
313
314 /* if *domain is non-null, it's pushed down the stack.
315 * note that the entire stack is freed if there is an error,
316 * so build added domains in a separate stack before adding them
317 *
318 * domain_specs look like
319 *
320 * [strict ][ignore ][serialize ]uri[[ uri]...]
321 * e.g. "ldap:///ou=foo,o=bar?uid?sub ldap:///ou=baz,o=bar?uid?sub"
322 * "strict ldap:///ou=accounts,o=bar?uid,uidNumber?one"
323 * etc
324 *
325 * so finally strictness is per-domain
326 * but so is ignore-state, and that would be better as a per-url thing
327 */
328 static int
329 unique_new_domain ( unique_domain **domainp,
330 char *domain_spec,
331 ConfigArgs *c )
332 {
333 char *uri_start;
334 int rc = LDAP_SUCCESS;
335 int uri_err = 0;
336 unique_domain * domain;
337 LDAPURLDesc *url_desc, *url_descs = NULL;
338
339 Debug(LDAP_DEBUG_TRACE, "==> unique_new_domain <%s>\n",
340 domain_spec );
341
342 domain = ch_calloc ( 1, sizeof (unique_domain) );
343 ber_str2bv( domain_spec, 0, 1, &domain->domain_spec );
344
345 uri_start = domain_spec;
346 if ( strncasecmp ( uri_start, "ignore ",
347 STRLENOF( "ignore " ) ) == 0 ) {
348 domain->ignore = 1;
349 uri_start += STRLENOF( "ignore " );
350 }
351 if ( strncasecmp ( uri_start, "serialize ",
352 STRLENOF( "serialize " ) ) == 0 ) {
353 domain->serial = 1;
354 uri_start += STRLENOF( "serialize " );
355 }
356 if ( strncasecmp ( uri_start, "strict ",
357 STRLENOF( "strict " ) ) == 0 ) {
358 domain->strict = 1;
359 uri_start += STRLENOF( "strict " );
360 if ( !domain->ignore
361 && strncasecmp ( uri_start, "ignore ",
362 STRLENOF( "ignore " ) ) == 0 ) {
363 domain->ignore = 1;
364 uri_start += STRLENOF( "ignore " );
365 }
366 }
367 rc = ldap_url_parselist_ext ( &url_descs, uri_start, " ", 0 );
368 if ( rc ) {
369 snprintf( c->cr_msg, sizeof( c->cr_msg ),
370 "<%s> invalid ldap urilist",
371 uri_start );
372 rc = ARG_BAD_CONF;
373 goto exit;
374 }
375
376 for ( url_desc = url_descs;
377 url_desc;
378 url_desc = url_desc->lud_next ) {
379 rc = unique_new_domain_uri ( &domain->uri,
380 url_desc,
381 c );
382 if ( rc ) {
383 rc = ARG_BAD_CONF;
384 uri_err = 1;
385 goto exit;
386 }
387 }
388
389 exit:
390 if ( url_descs ) ldap_free_urldesc ( url_descs );
391 domain->next = *domainp;
392 *domainp = domain;
393 if ( rc ) {
394 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
395 "%s: %s\n", c->log, c->cr_msg );
396 unique_free_domain ( domain );
397 *domainp = NULL;
398 }
399 return rc;
400 }
401
402 static int
403 unique_cf_base( ConfigArgs *c )
404 {
405 BackendDB *be = (BackendDB *)c->be;
406 slap_overinst *on = (slap_overinst *)c->bi;
407 unique_data *private = (unique_data *) on->on_bi.bi_private;
408 unique_domain *domains = private->domains;
409 unique_domain *legacy = private->legacy;
410 int rc = ARG_BAD_CONF;
411
412 switch ( c->op ) {
413 case SLAP_CONFIG_EMIT:
414 rc = 0;
415 if ( legacy && legacy->uri && legacy->uri->dn.bv_val ) {
416 rc = value_add_one ( &c->rvalue_vals,
417 &legacy->uri->dn );
418 if ( rc ) return rc;
419 rc = value_add_one ( &c->rvalue_nvals,
420 &legacy->uri->ndn );
421 if ( rc ) return rc;
422 }
423 break;
424 case LDAP_MOD_DELETE:
425 assert ( legacy && legacy->uri && legacy->uri->dn.bv_val );
426 rc = 0;
427 ch_free ( legacy->uri->dn.bv_val );
428 ch_free ( legacy->uri->ndn.bv_val );
429 BER_BVZERO( &legacy->uri->dn );
430 BER_BVZERO( &legacy->uri->ndn );
431 if ( !legacy->uri->attrs ) {
432 unique_free_domain_uri ( legacy->uri );
433 legacy->uri = NULL;
434 }
435 if ( !legacy->uri && !private->legacy_strict_set ) {
436 unique_free_domain ( legacy );
437 private->legacy = legacy = NULL;
438 }
439 break;
440 case LDAP_MOD_ADD:
441 case SLAP_CONFIG_ADD:
442 if ( domains ) {
443 snprintf( c->cr_msg, sizeof( c->cr_msg ),
444 "cannot set legacy attrs when URIs are present" );
445 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
446 c->cr_msg );
447 rc = ARG_BAD_CONF;
448 break;
449 }
450 if ( be->be_nsuffix == NULL ) {
451 snprintf( c->cr_msg, sizeof( c->cr_msg ),
452 "suffix must be set" );
453 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
454 c->cr_msg );
455 rc = ARG_BAD_CONF;
456 break;
457 }
458 if ( !dnIsSuffix ( &c->value_ndn,
459 &be->be_nsuffix[0] ) ) {
460 snprintf( c->cr_msg, sizeof( c->cr_msg ),
461 "dn is not a suffix of backend base" );
462 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
463 c->cr_msg );
464 rc = ARG_BAD_CONF;
465 break;
466 }
467 if ( !legacy ) {
468 unique_new_domain ( &private->legacy,
469 UNIQUE_DEFAULT_URI,
470 c );
471 legacy = private->legacy;
472 }
473 if ( !legacy->uri )
474 unique_new_domain_uri_basic ( &legacy->uri, c );
475 ch_free ( legacy->uri->dn.bv_val );
476 ch_free ( legacy->uri->ndn.bv_val );
477 legacy->uri->dn = c->value_dn;
478 legacy->uri->ndn = c->value_ndn;
479 rc = 0;
480 break;
481 default:
482 abort();
483 }
484
485 if ( rc ) {
486 ch_free( c->value_dn.bv_val );
487 BER_BVZERO( &c->value_dn );
488 ch_free( c->value_ndn.bv_val );
489 BER_BVZERO( &c->value_ndn );
490 }
491
492 return rc;
493 }
494
495 static int
496 unique_cf_attrs( ConfigArgs *c )
497 {
498 slap_overinst *on = (slap_overinst *)c->bi;
499 unique_data *private = (unique_data *) on->on_bi.bi_private;
500 unique_domain *domains = private->domains;
501 unique_domain *legacy = private->legacy;
502 unique_attrs *new_attrs = NULL;
503 unique_attrs *attr, *next_attr, *reverse_attrs;
504 unique_attrs **attrp;
505 int rc = ARG_BAD_CONF;
506 int i;
507
508 switch ( c->op ) {
509 case SLAP_CONFIG_EMIT:
510 if ( legacy
511 && (c->type == UNIQUE_IGNORE) == legacy->ignore
512 && legacy->uri )
513 for ( attr = legacy->uri->attrs;
514 attr;
515 attr = attr->next )
516 value_add_one( &c->rvalue_vals,
517 &attr->attr->ad_cname );
518 rc = 0;
519 break;
520 case LDAP_MOD_DELETE:
521 if ( legacy
522 && (c->type == UNIQUE_IGNORE) == legacy->ignore
523 && legacy->uri
524 && legacy->uri->attrs) {
525 if ( c->valx < 0 ) { /* delete all */
526 for ( attr = legacy->uri->attrs;
527 attr;
528 attr = next_attr ) {
529 next_attr = attr->next;
530 ch_free ( attr );
531 }
532 legacy->uri->attrs = NULL;
533 } else { /* delete by index */
534 attrp = &legacy->uri->attrs;
535 for ( i=0; i < c->valx; ++i )
536 attrp = &(*attrp)->next;
537 attr = *attrp;
538 *attrp = attr->next;
539 ch_free (attr);
540 }
541 if ( !legacy->uri->attrs
542 && !legacy->uri->dn.bv_val ) {
543 unique_free_domain_uri ( legacy->uri );
544 legacy->uri = NULL;
545 }
546 if ( !legacy->uri && !private->legacy_strict_set ) {
547 unique_free_domain ( legacy );
548 private->legacy = legacy = NULL;
549 }
550 }
551 rc = 0;
552 break;
553 case LDAP_MOD_ADD:
554 if ( c->argc > 2 ) {
555 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, "unique config: "
556 "Supplying multiple names in a single %s value is unsupported "
557 "and will be disallowed in a future version\n",
558 c->argv[0] );
559 }
560 /* FALLTHRU */
561 case SLAP_CONFIG_ADD:
562 if ( domains ) {
563 snprintf( c->cr_msg, sizeof( c->cr_msg ),
564 "cannot set legacy attrs when URIs are present" );
565 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
566 c->cr_msg );
567 rc = ARG_BAD_CONF;
568 break;
569 }
570 if ( legacy
571 && legacy->uri
572 && legacy->uri->attrs
573 && (c->type == UNIQUE_IGNORE) != legacy->ignore ) {
574 snprintf( c->cr_msg, sizeof( c->cr_msg ),
575 "cannot set both attrs and ignore-attrs" );
576 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
577 c->cr_msg );
578 rc = ARG_BAD_CONF;
579 break;
580 }
581 if ( !legacy ) {
582 unique_new_domain ( &private->legacy,
583 UNIQUE_DEFAULT_URI,
584 c );
585 legacy = private->legacy;
586 }
587 if ( !legacy->uri )
588 unique_new_domain_uri_basic ( &legacy->uri, c );
589 rc = 0;
590 for ( i=1; c->argv[i]; ++i ) {
591 AttributeDescription * ad = NULL;
592 const char * text;
593 if ( slap_str2ad ( c->argv[i], &ad, &text )
594 == LDAP_SUCCESS) {
595
596 attr = ch_calloc ( 1,
597 sizeof ( unique_attrs ) );
598 attr->attr = ad;
599 attr->next = new_attrs;
600 new_attrs = attr;
601 } else {
602 snprintf( c->cr_msg, sizeof( c->cr_msg ),
603 "unique: attribute: %s: %s",
604 c->argv[i], text );
605 for ( attr = new_attrs;
606 attr;
607 attr=next_attr ) {
608 next_attr = attr->next;
609 ch_free ( attr );
610 }
611 rc = ARG_BAD_CONF;
612 break;
613 }
614 }
615 if ( rc ) break;
616
617 /* (nconc legacy->uri->attrs (nreverse new_attrs)) */
618 reverse_attrs = NULL;
619 for ( attr = new_attrs;
620 attr;
621 attr = next_attr ) {
622 next_attr = attr->next;
623 attr->next = reverse_attrs;
624 reverse_attrs = attr;
625 }
626 for ( attrp = &legacy->uri->attrs;
627 *attrp;
628 attrp = &(*attrp)->next ) ;
629 *attrp = reverse_attrs;
630
631 legacy->ignore = ( c->type == UNIQUE_IGNORE );
632 break;
633 default:
634 abort();
635 }
636
637 if ( rc ) {
638 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
639 "%s: %s\n", c->log, c->cr_msg );
640 }
641 return rc;
642 }
643
644 static int
645 unique_cf_strict( ConfigArgs *c )
646 {
647 slap_overinst *on = (slap_overinst *)c->bi;
648 unique_data *private = (unique_data *) on->on_bi.bi_private;
649 unique_domain *domains = private->domains;
650 unique_domain *legacy = private->legacy;
651 int rc = ARG_BAD_CONF;
652
653 switch ( c->op ) {
654 case SLAP_CONFIG_EMIT:
655 /* We process the boolean manually instead of using
656 * ARG_ON_OFF so that we can three-state it;
657 * olcUniqueStrict is either TRUE, FALSE, or missing,
658 * and missing is necessary to add olcUniqueURIs...
659 */
660 if ( private->legacy_strict_set ) {
661 struct berval bv = legacy->strict ? slap_true_bv : slap_false_bv;
662 value_add_one ( &c->rvalue_vals, &bv );
663 }
664 rc = 0;
665 break;
666 case LDAP_MOD_DELETE:
667 if ( legacy ) {
668 legacy->strict = 0;
669 if ( ! legacy->uri ) {
670 unique_free_domain ( legacy );
671 private->legacy = NULL;
672 }
673 }
674 private->legacy_strict_set = 0;
675 rc = 0;
676 break;
677 case LDAP_MOD_ADD:
678 case SLAP_CONFIG_ADD:
679 if ( domains ) {
680 snprintf( c->cr_msg, sizeof( c->cr_msg ),
681 "cannot set legacy attrs when URIs are present" );
682 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
683 c->cr_msg );
684 rc = ARG_BAD_CONF;
685 break;
686 }
687 if ( ! legacy ) {
688 unique_new_domain ( &private->legacy,
689 UNIQUE_DEFAULT_URI,
690 c );
691 legacy = private->legacy;
692 }
693 /* ... not using ARG_ON_OFF makes this necessary too */
694 assert ( c->argc == 2 );
695 legacy->strict = (strcasecmp ( c->argv[1], "TRUE" ) == 0);
696 private->legacy_strict_set = 1;
697 rc = 0;
698 break;
699 default:
700 abort();
701 }
702
703 return rc;
704 }
705
706 static int
707 unique_cf_uri( ConfigArgs *c )
708 {
709 slap_overinst *on = (slap_overinst *)c->bi;
710 unique_data *private = (unique_data *) on->on_bi.bi_private;
711 unique_domain *domains = private->domains;
712 unique_domain *legacy = private->legacy;
713 unique_domain *domain = NULL, **domainp = NULL;
714 int rc = ARG_BAD_CONF;
715 int i;
716
717 switch ( c->op ) {
718 case SLAP_CONFIG_EMIT:
719 for ( domain = domains;
720 domain;
721 domain = domain->next ) {
722 rc = value_add_one ( &c->rvalue_vals,
723 &domain->domain_spec );
724 if ( rc ) break;
725 }
726 break;
727 case LDAP_MOD_DELETE:
728 if ( c->valx < 0 ) { /* delete them all! */
729 unique_free_domain ( domains );
730 private->domains = NULL;
731 } else { /* delete just one */
732 domainp = &private->domains;
733 for ( i=0; i < c->valx && *domainp; ++i )
734 domainp = &(*domainp)->next;
735
736 /* If *domainp is null, we walked off the end
737 * of the list. This happens when back-config
738 * and the overlay are out-of-sync, like when
739 * rejecting changes before ITS#4752 gets
740 * fixed.
741 *
742 * This should never happen, but will appear
743 * if you backport this version of
744 * slapo-unique without the config-undo fixes
745 *
746 * test024 Will hit this case in such a
747 * situation.
748 */
749 assert (*domainp != NULL);
750
751 domain = *domainp;
752 *domainp = domain->next;
753 domain->next = NULL;
754 unique_free_domain ( domain );
755 }
756 rc = 0;
757 break;
758
759 case SLAP_CONFIG_ADD: /* fallthru */
760 case LDAP_MOD_ADD:
761 if ( legacy ) {
762 snprintf( c->cr_msg, sizeof( c->cr_msg ),
763 "cannot set Uri when legacy attrs are present" );
764 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
765 c->cr_msg );
766 rc = ARG_BAD_CONF;
767 break;
768 }
769 rc = 0;
770 if ( c->line ) rc = unique_new_domain ( &domain, c->line, c );
771 else rc = unique_new_domain ( &domain, c->argv[1], c );
772 if ( rc ) break;
773 assert ( domain->next == NULL );
774 for ( domainp = &private->domains;
775 *domainp;
776 domainp = &(*domainp)->next ) ;
777 *domainp = domain;
778
779 break;
780
781 default:
782 abort ();
783 }
784
785 return rc;
786 }
787
788 /*
789 ** allocate new unique_data;
790 ** initialize, copy basedn;
791 ** store in on_bi.bi_private;
792 **
793 */
794
795 static int
796 unique_db_init(
797 BackendDB *be,
798 ConfigReply *cr
799 )
800 {
801 slap_overinst *on = (slap_overinst *)be->bd_info;
802 unique_data *private;
803
804 Debug(LDAP_DEBUG_TRACE, "==> unique_db_init\n" );
805
806 private = ch_calloc ( 1, sizeof ( unique_data ) );
807 ldap_pvt_thread_mutex_init( &private->serial_mutex );
808 on->on_bi.bi_private = private;
809
810 return 0;
811 }
812
813 static int
814 unique_db_destroy(
815 BackendDB *be,
816 ConfigReply *cr
817 )
818 {
819 slap_overinst *on = (slap_overinst *)be->bd_info;
820 unique_data *private = on->on_bi.bi_private;
821
822 Debug(LDAP_DEBUG_TRACE, "==> unique_db_destroy\n" );
823
824 if ( private ) {
825 unique_domain *domains = private->domains;
826 unique_domain *legacy = private->legacy;
827
828 unique_free_domain ( domains );
829 unique_free_domain ( legacy );
830 ldap_pvt_thread_mutex_destroy( &private->serial_mutex );
831 ch_free ( private );
832 on->on_bi.bi_private = NULL;
833 }
834
835 return 0;
836 }
837
838
839 /*
840 ** search callback
841 ** if this is a REP_SEARCH, count++;
842 **
843 */
844
845 static int count_attr_cb(
846 Operation *op,
847 SlapReply *rs
848 )
849 {
850 unique_counter *uc;
851
852 /* because you never know */
853 if(!op || !rs) return(0);
854
855 /* Only search entries are interesting */
856 if(rs->sr_type != REP_SEARCH) return(0);
857
858 uc = op->o_callback->sc_private;
859
860 /* Ignore the current entry */
861 if ( dn_match( uc->ndn, &rs->sr_entry->e_nname )) return(0);
862
863 Debug(LDAP_DEBUG_TRACE, "==> count_attr_cb <%s>\n",
864 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "UNKNOWN_DN" );
865
866 uc->count++;
867
868 return(0);
869 }
870
871 /* count the length of one attribute ad
872 * (and all of its values b)
873 * in the proposed filter
874 */
875 static int
876 count_filter_len(
877 unique_domain *domain,
878 unique_domain_uri *uri,
879 AttributeDescription *ad,
880 BerVarray b
881 )
882 {
883 unique_attrs *attr;
884 int i;
885 int ks = 0;
886
887 while ( !is_at_operational( ad->ad_type ) ) {
888 if ( uri->attrs ) {
889 for ( attr = uri->attrs; attr; attr = attr->next ) {
890 if ( ad == attr->attr ) {
891 break;
892 }
893 }
894 if ( ( domain->ignore && attr )
895 || (!domain->ignore && !attr )) {
896 break;
897 }
898 }
899 if ( b && b[0].bv_val ) {
900 for (i = 0; b[i].bv_val; i++ ) {
901 /* note: make room for filter escaping... */
902 ks += ( 3 * b[i].bv_len ) + ad->ad_cname.bv_len + STRLENOF( "(=)" );
903 }
904 } else if ( domain->strict ) {
905 ks += ad->ad_cname.bv_len + STRLENOF( "(=*)" ); /* (attr=*) */
906 }
907 break;
908 }
909
910 return ks;
911 }
912
913 static char *
914 build_filter(
915 unique_domain *domain,
916 unique_domain_uri *uri,
917 AttributeDescription *ad,
918 BerVarray b,
919 char *kp,
920 int ks,
921 void *ctx
922 )
923 {
924 unique_attrs *attr;
925 int i;
926
927 while ( !is_at_operational( ad->ad_type ) ) {
928 if ( uri->attrs ) {
929 for ( attr = uri->attrs; attr; attr = attr->next ) {
930 if ( ad == attr->attr ) {
931 break;
932 }
933 }
934 if ( ( domain->ignore && attr )
935 || (!domain->ignore && !attr )) {
936 break;
937 }
938 }
939 if ( b && b[0].bv_val ) {
940 for ( i = 0; b[i].bv_val; i++ ) {
941 struct berval bv;
942 int len;
943
944 ldap_bv2escaped_filter_value_x( &b[i], &bv, 1, ctx );
945 if (!b[i].bv_len)
946 bv.bv_val = b[i].bv_val;
947 len = snprintf( kp, ks, "(%s=%s)", ad->ad_cname.bv_val, bv.bv_val );
948 assert( len >= 0 && len < ks );
949 kp += len;
950 if ( bv.bv_val != b[i].bv_val ) {
951 ber_memfree_x( bv.bv_val, ctx );
952 }
953 }
954 } else if ( domain->strict ) {
955 int len;
956 len = snprintf( kp, ks, "(%s=*)", ad->ad_cname.bv_val );
957 assert( len >= 0 && len < ks );
958 kp += len;
959 }
960 break;
961 }
962 return kp;
963 }
964
965 static int
966 unique_search(
967 Operation *op,
968 Operation *nop,
969 struct berval * dn,
970 int scope,
971 SlapReply *rs,
972 struct berval *key
973 )
974 {
975 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
976 SlapReply nrs = { REP_RESULT };
977 slap_callback cb = { NULL, NULL, NULL, NULL }; /* XXX */
978 unique_counter uq = { NULL, 0 };
979 int rc;
980 char *errmsg;
981 int errmsgsize;
982
983 Debug(LDAP_DEBUG_TRACE, "==> unique_search %s\n", key->bv_val );
984
985 nop->ors_filter = str2filter_x(nop, key->bv_val);
986 if(nop->ors_filter == NULL) {
987 op->o_bd->bd_info = (BackendInfo *) on->on_info;
988 send_ldap_error(op, rs, LDAP_OTHER,
989 "unique_search invalid filter");
990 return(rs->sr_err);
991 }
992
993 nop->ors_filterstr = *key;
994
995 cb.sc_response = (slap_response*)count_attr_cb;
996 cb.sc_private = &uq;
997 nop->o_callback = &cb;
998 nop->o_tag = LDAP_REQ_SEARCH;
999 nop->ors_scope = scope;
1000 nop->ors_deref = LDAP_DEREF_NEVER;
1001 nop->ors_limit = NULL;
1002 nop->ors_slimit = SLAP_NO_LIMIT;
1003 nop->ors_tlimit = SLAP_NO_LIMIT;
1004 nop->ors_attrs = slap_anlist_no_attrs;
1005 nop->ors_attrsonly = 1;
1006 memset( nop->o_ctrlflag, 0, sizeof( nop->o_ctrlflag ));
1007
1008 uq.ndn = &op->o_req_ndn;
1009
1010 nop->o_req_ndn = *dn;
1011 nop->o_ndn = op->o_bd->be_rootndn;
1012
1013 nop->o_bd = on->on_info->oi_origdb;
1014 rc = nop->o_bd->be_search(nop, &nrs);
1015 filter_free_x(nop, nop->ors_filter, 1);
1016
1017 if(rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT) {
1018 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1019 send_ldap_error(op, rs, rc, "unique_search failed");
1020 rc = rs->sr_err;
1021 } else if(uq.count) {
1022 Debug(LDAP_DEBUG_TRACE, "=> unique_search found %d records\n", uq.count );
1023
1024 errmsgsize = sizeof("non-unique attributes found with ") + key->bv_len;
1025 errmsg = op->o_tmpalloc(errmsgsize, op->o_tmpmemctx);
1026 snprintf( errmsg, errmsgsize, "non-unique attributes found with %s", key->bv_val );
1027 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1028 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION, errmsg);
1029 op->o_tmpfree(errmsg, op->o_tmpmemctx);
1030 rc = rs->sr_err;
1031 } else {
1032 Debug(LDAP_DEBUG_TRACE, "=> unique_search found no records\n" );
1033 rc = SLAP_CB_CONTINUE;
1034 }
1035
1036 op->o_tmpfree( key->bv_val, op->o_tmpmemctx );
1037
1038 return(rc);
1039 }
1040
1041 static int
1042 unique_unlock(
1043 Operation *op,
1044 SlapReply *rs
1045 )
1046 {
1047 slap_callback *sc = op->o_callback;
1048 unique_data *private = sc->sc_private;
1049
1050 ldap_pvt_thread_mutex_unlock( &private->serial_mutex );
1051 op->o_callback = sc->sc_next;
1052 op->o_tmpfree( sc, op->o_tmpmemctx );
1053 return 0;
1054 }
1055
1056 static int
1057 unique_add(
1058 Operation *op,
1059 SlapReply *rs
1060 )
1061 {
1062 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1063 unique_data *private = (unique_data *) on->on_bi.bi_private;
1064 unique_domain *domains = private->domains;
1065 unique_domain *legacy = private->legacy;
1066 unique_domain *domain;
1067 Operation nop = *op;
1068 Attribute *a;
1069 char *key, *kp;
1070 struct berval bvkey;
1071 int rc = SLAP_CB_CONTINUE;
1072 int locked = 0;
1073
1074 Debug(LDAP_DEBUG_TRACE, "==> unique_add <%s>\n",
1075 op->o_req_dn.bv_val );
1076
1077 if ( be_shadow_update( op ) || (
1078 get_relax(op) > SLAP_CONTROL_IGNORED
1079 && access_allowed( op, op->ora_e,
1080 slap_schema.si_ad_entry, NULL,
1081 ACL_MANAGE, NULL ) ) ) {
1082 return rc;
1083 }
1084
1085 for ( domain = legacy ? legacy : domains;
1086 domain;
1087 domain = domain->next )
1088 {
1089 unique_domain_uri *uri;
1090
1091 for ( uri = domain->uri;
1092 uri;
1093 uri = uri->next )
1094 {
1095 int len;
1096 int ks = 0;
1097
1098 if ( uri->ndn.bv_val
1099 && !dnIsSuffix( &op->o_req_ndn, &uri->ndn ))
1100 continue;
1101
1102 if ( uri->f ) {
1103 if ( test_filter( NULL, op->ora_e, uri->f )
1104 == LDAP_COMPARE_FALSE )
1105 {
1106 Debug( LDAP_DEBUG_TRACE,
1107 "==> unique_add_skip<%s>\n",
1108 op->o_req_dn.bv_val );
1109 continue;
1110 }
1111 }
1112
1113 if(!(a = op->ora_e->e_attrs)) {
1114 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1115 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1116 "unique_add() got null op.ora_e.e_attrs");
1117 rc = rs->sr_err;
1118 break;
1119
1120 } else {
1121 for(; a; a = a->a_next) {
1122 ks += count_filter_len ( domain,
1123 uri,
1124 a->a_desc,
1125 a->a_vals);
1126 }
1127 }
1128
1129 /* skip this domain-uri if it isn't involved */
1130 if ( !ks ) continue;
1131
1132 if ( domain->serial && !locked ) {
1133 ldap_pvt_thread_mutex_lock( &private->serial_mutex );
1134 locked = 1;
1135 }
1136
1137 /* terminating NUL */
1138 ks += sizeof("(|)");
1139
1140 if ( uri->filter.bv_val && uri->filter.bv_len )
1141 ks += uri->filter.bv_len + STRLENOF ("(&)");
1142 kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1143
1144 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1145 len = snprintf (kp, ks, "(&%s", uri->filter.bv_val);
1146 assert( len >= 0 && len < ks );
1147 kp += len;
1148 }
1149 len = snprintf(kp, ks - (kp - key), "(|");
1150 assert( len >= 0 && len < ks - (kp - key) );
1151 kp += len;
1152
1153 for(a = op->ora_e->e_attrs; a; a = a->a_next)
1154 kp = build_filter(domain,
1155 uri,
1156 a->a_desc,
1157 a->a_vals,
1158 kp,
1159 ks - ( kp - key ),
1160 op->o_tmpmemctx);
1161
1162 len = snprintf(kp, ks - (kp - key), ")");
1163 assert( len >= 0 && len < ks - (kp - key) );
1164 kp += len;
1165 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1166 len = snprintf(kp, ks - (kp - key), ")");
1167 assert( len >= 0 && len < ks - (kp - key) );
1168 kp += len;
1169 }
1170 bvkey.bv_val = key;
1171 bvkey.bv_len = kp - key;
1172
1173 rc = unique_search ( op,
1174 &nop,
1175 uri->ndn.bv_val ?
1176 &uri->ndn :
1177 &op->o_bd->be_nsuffix[0],
1178 uri->scope,
1179 rs,
1180 &bvkey);
1181
1182 if ( rc != SLAP_CB_CONTINUE ) break;
1183 }
1184 if ( rc != SLAP_CB_CONTINUE ) break;
1185 }
1186
1187 if ( locked ) {
1188 if ( rc != SLAP_CB_CONTINUE ) {
1189 ldap_pvt_thread_mutex_unlock( &private->serial_mutex );
1190 } else {
1191 slap_callback *cb = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
1192 cb->sc_cleanup = unique_unlock;
1193 cb->sc_private = private;
1194 cb->sc_next = op->o_callback;
1195 op->o_callback = cb;
1196 }
1197 }
1198 return rc;
1199 }
1200
1201
1202 static int
1203 unique_modify(
1204 Operation *op,
1205 SlapReply *rs
1206 )
1207 {
1208 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1209 unique_data *private = (unique_data *) on->on_bi.bi_private;
1210 unique_domain *domains = private->domains;
1211 unique_domain *legacy = private->legacy;
1212 unique_domain *domain;
1213 Operation nop = *op;
1214 Modifications *m;
1215 Entry *e = NULL;
1216 char *key, *kp;
1217 struct berval bvkey;
1218 int rc = SLAP_CB_CONTINUE;
1219 int locked = 0;
1220
1221 Debug(LDAP_DEBUG_TRACE, "==> unique_modify <%s>\n",
1222 op->o_req_dn.bv_val );
1223
1224 if ( !op->orm_modlist ) {
1225 Debug(LDAP_DEBUG_TRACE, "unique_modify: got empty modify op\n" );
1226 return rc;
1227 }
1228
1229 if ( be_shadow_update( op ) ) {
1230 return rc;
1231 }
1232 if ( get_relax(op) > SLAP_CONTROL_IGNORED
1233 && overlay_entry_get_ov( op, &op->o_req_ndn, NULL, NULL, 0, &e, on ) == LDAP_SUCCESS
1234 && e
1235 && access_allowed( op, e,
1236 slap_schema.si_ad_entry, NULL,
1237 ACL_MANAGE, NULL ) ) {
1238 overlay_entry_release_ov( op, e, 0, on );
1239 return rc;
1240 }
1241 if ( e ) {
1242 overlay_entry_release_ov( op, e, 0, on );
1243 }
1244
1245 for ( domain = legacy ? legacy : domains;
1246 domain;
1247 domain = domain->next )
1248 {
1249 unique_domain_uri *uri;
1250
1251 for ( uri = domain->uri;
1252 uri;
1253 uri = uri->next )
1254 {
1255 int len;
1256 int ks = 0;
1257
1258 if ( uri->ndn.bv_val
1259 && !dnIsSuffix( &op->o_req_ndn, &uri->ndn ))
1260 continue;
1261
1262 for ( m = op->orm_modlist; m; m = m->sml_next)
1263 if ( (m->sml_op & LDAP_MOD_OP)
1264 != LDAP_MOD_DELETE )
1265 ks += count_filter_len
1266 ( domain,
1267 uri,
1268 m->sml_desc,
1269 m->sml_values);
1270
1271 /* skip this domain-uri if it isn't involved */
1272 if ( !ks ) continue;
1273
1274 if ( domain->serial && !locked ) {
1275 ldap_pvt_thread_mutex_lock( &private->serial_mutex );
1276 locked = 1;
1277 }
1278
1279 /* terminating NUL */
1280 ks += sizeof("(|)");
1281
1282 if ( uri->filter.bv_val && uri->filter.bv_len )
1283 ks += uri->filter.bv_len + STRLENOF ("(&)");
1284 kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1285
1286 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1287 len = snprintf(kp, ks, "(&%s", uri->filter.bv_val);
1288 assert( len >= 0 && len < ks );
1289 kp += len;
1290 }
1291 len = snprintf(kp, ks - (kp - key), "(|");
1292 assert( len >= 0 && len < ks - (kp - key) );
1293 kp += len;
1294
1295 for(m = op->orm_modlist; m; m = m->sml_next)
1296 if ( (m->sml_op & LDAP_MOD_OP)
1297 != LDAP_MOD_DELETE )
1298 kp = build_filter ( domain,
1299 uri,
1300 m->sml_desc,
1301 m->sml_values,
1302 kp,
1303 ks - (kp - key),
1304 op->o_tmpmemctx );
1305
1306 len = snprintf(kp, ks - (kp - key), ")");
1307 assert( len >= 0 && len < ks - (kp - key) );
1308 kp += len;
1309 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1310 len = snprintf (kp, ks - (kp - key), ")");
1311 assert( len >= 0 && len < ks - (kp - key) );
1312 kp += len;
1313 }
1314 bvkey.bv_val = key;
1315 bvkey.bv_len = kp - key;
1316
1317 rc = unique_search ( op,
1318 &nop,
1319 uri->ndn.bv_val ?
1320 &uri->ndn :
1321 &op->o_bd->be_nsuffix[0],
1322 uri->scope,
1323 rs,
1324 &bvkey);
1325
1326 if ( rc != SLAP_CB_CONTINUE ) break;
1327 }
1328 if ( rc != SLAP_CB_CONTINUE ) break;
1329 }
1330
1331 if ( locked ) {
1332 if ( rc != SLAP_CB_CONTINUE ) {
1333 ldap_pvt_thread_mutex_unlock( &private->serial_mutex );
1334 } else {
1335 slap_callback *cb = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
1336 cb->sc_cleanup = unique_unlock;
1337 cb->sc_private = private;
1338 cb->sc_next = op->o_callback;
1339 op->o_callback = cb;
1340 }
1341 }
1342 return rc;
1343 }
1344
1345
1346 static int
1347 unique_modrdn(
1348 Operation *op,
1349 SlapReply *rs
1350 )
1351 {
1352 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1353 unique_data *private = (unique_data *) on->on_bi.bi_private;
1354 unique_domain *domains = private->domains;
1355 unique_domain *legacy = private->legacy;
1356 unique_domain *domain;
1357 Operation nop = *op;
1358 Entry *e = NULL;
1359 char *key, *kp;
1360 struct berval bvkey;
1361 LDAPRDN newrdn;
1362 struct berval bv[2];
1363 int rc = SLAP_CB_CONTINUE;
1364 int locked = 0;
1365
1366 Debug(LDAP_DEBUG_TRACE, "==> unique_modrdn <%s> <%s>\n",
1367 op->o_req_dn.bv_val, op->orr_newrdn.bv_val );
1368
1369 if ( be_shadow_update( op ) ) {
1370 return rc;
1371 }
1372 if ( get_relax(op) > SLAP_CONTROL_IGNORED
1373 && overlay_entry_get_ov( op, &op->o_req_ndn, NULL, NULL, 0, &e, on ) == LDAP_SUCCESS
1374 && e
1375 && access_allowed( op, e,
1376 slap_schema.si_ad_entry, NULL,
1377 ACL_MANAGE, NULL ) ) {
1378 overlay_entry_release_ov( op, e, 0, on );
1379 return rc;
1380 }
1381 if ( e ) {
1382 overlay_entry_release_ov( op, e, 0, on );
1383 }
1384
1385 for ( domain = legacy ? legacy : domains;
1386 domain;
1387 domain = domain->next )
1388 {
1389 unique_domain_uri *uri;
1390
1391 for ( uri = domain->uri;
1392 uri;
1393 uri = uri->next )
1394 {
1395 int i, len;
1396 int ks = 0;
1397
1398 if ( uri->ndn.bv_val
1399 && !dnIsSuffix( &op->o_req_ndn, &uri->ndn )
1400 && (!op->orr_nnewSup
1401 || !dnIsSuffix( op->orr_nnewSup, &uri->ndn )))
1402 continue;
1403
1404 if ( ldap_bv2rdn_x ( &op->oq_modrdn.rs_newrdn,
1405 &newrdn,
1406 (char **)&rs->sr_text,
1407 LDAP_DN_FORMAT_LDAP,
1408 op->o_tmpmemctx ) ) {
1409 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1410 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1411 "unknown type(s) used in RDN");
1412 rc = rs->sr_err;
1413 break;
1414 }
1415
1416 rc = SLAP_CB_CONTINUE;
1417 for ( i=0; newrdn[i]; i++) {
1418 AttributeDescription *ad = NULL;
1419 if ( slap_bv2ad( &newrdn[i]->la_attr, &ad, &rs->sr_text )) {
1420 ldap_rdnfree_x( newrdn, op->o_tmpmemctx );
1421 rs->sr_err = LDAP_INVALID_SYNTAX;
1422 send_ldap_result( op, rs );
1423 rc = rs->sr_err;
1424 break;
1425 }
1426 newrdn[i]->la_private = ad;
1427 }
1428 if ( rc != SLAP_CB_CONTINUE ) break;
1429
1430 bv[1].bv_val = NULL;
1431 bv[1].bv_len = 0;
1432
1433 for ( i=0; newrdn[i]; i++ ) {
1434 bv[0] = newrdn[i]->la_value;
1435 ks += count_filter_len ( domain,
1436 uri,
1437 newrdn[i]->la_private,
1438 bv);
1439 }
1440
1441 /* skip this domain if it isn't involved */
1442 if ( !ks ) continue;
1443
1444 if ( domain->serial && !locked ) {
1445 ldap_pvt_thread_mutex_lock( &private->serial_mutex );
1446 locked = 1;
1447 }
1448
1449 /* terminating NUL */
1450 ks += sizeof("(|)");
1451
1452 if ( uri->filter.bv_val && uri->filter.bv_len )
1453 ks += uri->filter.bv_len + STRLENOF ("(&)");
1454 kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1455
1456 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1457 len = snprintf(kp, ks, "(&%s", uri->filter.bv_val);
1458 assert( len >= 0 && len < ks );
1459 kp += len;
1460 }
1461 len = snprintf(kp, ks - (kp - key), "(|");
1462 assert( len >= 0 && len < ks - (kp - key) );
1463 kp += len;
1464
1465 for ( i=0; newrdn[i]; i++) {
1466 bv[0] = newrdn[i]->la_value;
1467 kp = build_filter ( domain,
1468 uri,
1469 newrdn[i]->la_private,
1470 bv,
1471 kp,
1472 ks - (kp - key ),
1473 op->o_tmpmemctx);
1474 }
1475
1476 len = snprintf(kp, ks - (kp - key), ")");
1477 assert( len >= 0 && len < ks - (kp - key) );
1478 kp += len;
1479 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1480 len = snprintf (kp, ks - (kp - key), ")");
1481 assert( len >= 0 && len < ks - (kp - key) );
1482 kp += len;
1483 }
1484 bvkey.bv_val = key;
1485 bvkey.bv_len = kp - key;
1486
1487 rc = unique_search ( op,
1488 &nop,
1489 uri->ndn.bv_val ?
1490 &uri->ndn :
1491 &op->o_bd->be_nsuffix[0],
1492 uri->scope,
1493 rs,
1494 &bvkey);
1495
1496 if ( rc != SLAP_CB_CONTINUE ) break;
1497 }
1498 if ( rc != SLAP_CB_CONTINUE ) break;
1499 }
1500
1501 if ( locked ) {
1502 if ( rc != SLAP_CB_CONTINUE ) {
1503 ldap_pvt_thread_mutex_unlock( &private->serial_mutex );
1504 } else {
1505 slap_callback *cb = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
1506 cb->sc_cleanup = unique_unlock;
1507 cb->sc_private = private;
1508 cb->sc_next = op->o_callback;
1509 op->o_callback = cb;
1510 }
1511 }
1512 return rc;
1513 }
1514
1515 /*
1516 ** init_module is last so the symbols resolve "for free" --
1517 ** it expects to be called automagically during dynamic module initialization
1518 */
1519
1520 int
1521 unique_initialize()
1522 {
1523 int rc;
1524
1525 /* statically declared just after the #includes at top */
1526 memset (&unique, 0, sizeof(unique));
1527
1528 unique.on_bi.bi_type = "unique";
1529 unique.on_bi.bi_flags = SLAPO_BFLAG_SINGLE;
1530 unique.on_bi.bi_db_init = unique_db_init;
1531 unique.on_bi.bi_db_destroy = unique_db_destroy;
1532 unique.on_bi.bi_op_add = unique_add;
1533 unique.on_bi.bi_op_modify = unique_modify;
1534 unique.on_bi.bi_op_modrdn = unique_modrdn;
1535
1536 unique.on_bi.bi_cf_ocs = uniqueocs;
1537 rc = config_register_schema( uniquecfg, uniqueocs );
1538 if ( rc ) return rc;
1539
1540 return(overlay_register(&unique));
1541 }
1542
1543 #if SLAPD_OVER_UNIQUE == SLAPD_MOD_DYNAMIC && defined(PIC)
1544 int init_module(int argc, char *argv[]) {
1545 return unique_initialize();
1546 }
1547 #endif
1548
1549 #endif /* SLAPD_OVER_UNIQUE */