1 /* unique.c - attribute uniqueness module */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2004-2009 The OpenLDAP Foundation.
6 * Portions Copyright 2004,2006-2007 Symas Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by Symas Corporation for
19 * inclusion in OpenLDAP Software, with subsequent enhancements by
20 * Matthew Backes at Symas Corporation. This work was sponsored by
26 #ifdef SLAPD_OVER_UNIQUE
30 #include <ac/string.h>
31 #include <ac/socket.h>
36 #define UNIQUE_DEFAULT_URI ("ldap:///??sub")
38 static slap_overinst unique;
40 typedef struct unique_attrs_s {
41 struct unique_attrs_s *next; /* list of attrs */
42 AttributeDescription *attr;
45 typedef struct unique_domain_uri_s {
46 struct unique_domain_uri_s *next;
51 struct unique_attrs_s *attrs;
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 */
63 typedef struct unique_data_s {
64 struct unique_domain_s *domains;
65 struct unique_domain_s *legacy;
66 char legacy_strict_set;
69 typedef struct unique_counter_s {
82 static ConfigDriver unique_cf_base;
83 static ConfigDriver unique_cf_attrs;
84 static ConfigDriver unique_cf_strict;
85 static ConfigDriver unique_cf_uri;
87 static ConfigTable uniquecfg[] = {
88 { "unique_base", "basedn", 2, 2, 0, ARG_DN|ARG_MAGIC|UNIQUE_BASE,
89 unique_cf_base, "( OLcfgOvAt:10.1 NAME 'olcUniqueBase' "
90 "DESC 'Subtree for uniqueness searches' "
91 "EQUALITY distinguishedNameMatch "
92 "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
93 { "unique_ignore", "attribute...", 2, 0, 0, ARG_MAGIC|UNIQUE_IGNORE,
94 unique_cf_attrs, "( OLcfgOvAt:10.2 NAME 'olcUniqueIgnore' "
95 "DESC 'Attributes for which uniqueness shall not be enforced' "
96 "EQUALITY caseIgnoreMatch "
97 "ORDERING caseIgnoreOrderingMatch "
98 "SUBSTR caseIgnoreSubstringsMatch "
99 "SYNTAX OMsDirectoryString )", NULL, NULL },
100 { "unique_attributes", "attribute...", 2, 0, 0, ARG_MAGIC|UNIQUE_ATTR,
101 unique_cf_attrs, "( OLcfgOvAt:10.3 NAME 'olcUniqueAttribute' "
102 "DESC 'Attributes for which uniqueness shall be enforced' "
103 "EQUALITY caseIgnoreMatch "
104 "ORDERING caseIgnoreOrderingMatch "
105 "SUBSTR caseIgnoreSubstringsMatch "
106 "SYNTAX OMsDirectoryString )", NULL, NULL },
107 { "unique_strict", "on|off", 1, 2, 0, ARG_MAGIC|UNIQUE_STRICT,
108 unique_cf_strict, "( OLcfgOvAt:10.4 NAME 'olcUniqueStrict' "
109 "DESC 'Enforce uniqueness of null values' "
110 "EQUALITY booleanMatch "
111 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
112 { "unique_uri", "ldapuri", 2, 3, 0, ARG_MAGIC|UNIQUE_URI,
113 unique_cf_uri, "( OLcfgOvAt:10.5 NAME 'olcUniqueURI' "
114 "DESC 'List of keywords and LDAP URIs for a uniqueness domain' "
115 "EQUALITY caseExactMatch "
116 "ORDERING caseExactOrderingMatch "
117 "SUBSTR caseExactSubstringsMatch "
118 "SYNTAX OMsDirectoryString )", NULL, NULL },
119 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
122 static ConfigOCs uniqueocs[] = {
123 { "( OLcfgOvOc:10.1 "
124 "NAME 'olcUniqueConfig' "
125 "DESC 'Attribute value uniqueness configuration' "
126 "SUP olcOverlayConfig "
127 "MAY ( olcUniqueBase $ olcUniqueIgnore $ "
128 "olcUniqueAttribute $ olcUniqueStrict $ "
130 Cft_Overlay, uniquecfg },
135 unique_free_domain_uri ( unique_domain_uri *uri )
137 unique_domain_uri *next_uri = NULL;
138 unique_attrs *attr, *next_attr = NULL;
141 next_uri = uri->next;
142 ch_free ( uri->dn.bv_val );
143 ch_free ( uri->ndn.bv_val );
144 ch_free ( uri->filter.bv_val );
145 filter_free( uri->f );
148 next_attr = attr->next;
157 /* free an entire stack of domains */
159 unique_free_domain ( unique_domain *domain )
161 unique_domain *next_domain = NULL;
164 next_domain = domain->next;
165 ch_free ( domain->domain_spec.bv_val );
166 unique_free_domain_uri ( domain->uri );
168 domain = next_domain;
173 unique_new_domain_uri ( unique_domain_uri **urip,
174 const LDAPURLDesc *url_desc,
177 int i, rc = LDAP_SUCCESS;
178 unique_domain_uri *uri;
179 struct berval bv = {0, NULL};
180 BackendDB *be = (BackendDB *)c->be;
182 AttributeDescription * ad;
185 uri = ch_calloc ( 1, sizeof ( unique_domain_uri ) );
187 if ( url_desc->lud_dn && url_desc->lud_dn[0] ) {
188 ber_str2bv( url_desc->lud_dn, 0, 0, &bv );
189 rc = dnPrettyNormal( NULL,
194 if ( rc != LDAP_SUCCESS ) {
195 snprintf( c->cr_msg, sizeof( c->cr_msg ),
196 "<%s> invalid DN %d (%s)",
197 url_desc->lud_dn, rc, ldap_err2string( rc ));
202 if ( be->be_nsuffix == NULL ) {
203 snprintf( c->cr_msg, sizeof( c->cr_msg ),
204 "suffix must be set" );
205 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
206 c->cr_msg, NULL, NULL );
211 if ( !dnIsSuffix ( &uri->ndn, &be->be_nsuffix[0] ) ) {
212 snprintf( c->cr_msg, sizeof( c->cr_msg ),
213 "dn <%s> is not a suffix of backend base dn <%s>",
215 be->be_nsuffix[0].bv_val );
220 if ( BER_BVISNULL( &be->be_rootndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
221 Debug( LDAP_DEBUG_ANY,
222 "slapo-unique needs a rootdn; "
223 "backend <%s> has none, YMMV.\n",
224 be->be_nsuffix[0].bv_val, 0, 0 );
228 attr_str = url_desc->lud_attrs;
230 for ( i=0; attr_str[i]; ++i ) {
233 if ( slap_str2ad ( attr_str[i], &ad, &text )
235 attr = ch_calloc ( 1,
236 sizeof ( unique_attrs ) );
238 attr->next = uri->attrs;
241 snprintf( c->cr_msg, sizeof( c->cr_msg ),
242 "unique: attribute: %s: %s",
250 uri->scope = url_desc->lud_scope;
252 snprintf( c->cr_msg, sizeof( c->cr_msg ),
253 "unique: uri with base scope will always be unique");
258 if (url_desc->lud_filter) {
260 uri->f = str2filter( url_desc->lud_filter );
262 snprintf( c->cr_msg, sizeof( c->cr_msg ),
263 "unique: bad filter");
267 /* make sure the strfilter is in normal form (ITS#5581) */
268 filter2bv( uri->f, &uri->filter );
269 ptr = strstr( uri->filter.bv_val, "(?=" /*)*/ );
270 if ( ptr != NULL && ptr <= ( uri->filter.bv_val - STRLENOF( "(?=" /*)*/ ) + uri->filter.bv_len ) )
272 snprintf( c->cr_msg, sizeof( c->cr_msg ),
273 "unique: bad filter");
282 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
283 "%s: %s\n", c->log, c->cr_msg, 0 );
284 unique_free_domain_uri ( uri );
291 unique_new_domain_uri_basic ( unique_domain_uri **urip,
294 LDAPURLDesc *url_desc = NULL;
297 rc = ldap_url_parse ( UNIQUE_DEFAULT_URI, &url_desc );
299 rc = unique_new_domain_uri ( urip, url_desc, c );
300 ldap_free_urldesc ( url_desc );
304 /* if *domain is non-null, it's pushed down the stack.
305 * note that the entire stack is freed if there is an error,
306 * so build added domains in a separate stack before adding them
308 * domain_specs look like
310 * [strict ][ignore ]uri[[ uri]...]
311 * e.g. "ldap:///ou=foo,o=bar?uid?sub ldap:///ou=baz,o=bar?uid?sub"
312 * "strict ldap:///ou=accounts,o=bar?uid,uidNumber?one"
315 * so finally strictness is per-domain
316 * but so is ignore-state, and that would be better as a per-url thing
319 unique_new_domain ( unique_domain **domainp,
324 int rc = LDAP_SUCCESS;
326 unique_domain * domain;
327 LDAPURLDesc *url_desc, *url_descs = NULL;
329 Debug(LDAP_DEBUG_TRACE, "==> unique_new_domain <%s>\n",
332 domain = ch_calloc ( 1, sizeof (unique_domain) );
333 ber_str2bv( domain_spec, 0, 1, &domain->domain_spec );
335 uri_start = domain_spec;
336 if ( strncasecmp ( uri_start, "ignore ",
337 STRLENOF( "ignore " ) ) == 0 ) {
339 uri_start += STRLENOF( "ignore " );
341 if ( strncasecmp ( uri_start, "strict ",
342 STRLENOF( "strict " ) ) == 0 ) {
344 uri_start += STRLENOF( "strict " );
346 && strncasecmp ( uri_start, "ignore ",
347 STRLENOF( "ignore " ) ) == 0 ) {
349 uri_start += STRLENOF( "ignore " );
352 rc = ldap_url_parselist_ext ( &url_descs, uri_start, " ", 0 );
354 snprintf( c->cr_msg, sizeof( c->cr_msg ),
355 "<%s> invalid ldap urilist",
361 for ( url_desc = url_descs;
363 url_desc = url_descs->lud_next ) {
364 rc = unique_new_domain_uri ( &domain->uri,
375 if ( url_descs ) ldap_free_urldesc ( url_descs );
376 domain->next = *domainp;
379 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
380 "%s: %s\n", c->log, c->cr_msg, 0 );
381 unique_free_domain ( domain );
388 unique_cf_base( ConfigArgs *c )
390 BackendDB *be = (BackendDB *)c->be;
391 slap_overinst *on = (slap_overinst *)c->bi;
392 unique_data *private = (unique_data *) on->on_bi.bi_private;
393 unique_domain *domains = private->domains;
394 unique_domain *legacy = private->legacy;
395 int rc = ARG_BAD_CONF;
398 case SLAP_CONFIG_EMIT:
400 if ( legacy && legacy->uri && legacy->uri->dn.bv_val ) {
401 rc = value_add_one ( &c->rvalue_vals,
404 rc = value_add_one ( &c->rvalue_nvals,
409 case LDAP_MOD_DELETE:
410 assert ( legacy && legacy->uri && legacy->uri->dn.bv_val );
412 ch_free ( legacy->uri->dn.bv_val );
413 ch_free ( legacy->uri->ndn.bv_val );
414 BER_BVZERO( &legacy->uri->dn );
415 BER_BVZERO( &legacy->uri->ndn );
416 if ( !legacy->uri->attrs ) {
417 unique_free_domain_uri ( legacy->uri );
420 if ( !legacy->uri && !private->legacy_strict_set ) {
421 unique_free_domain ( legacy );
422 private->legacy = legacy = NULL;
426 case SLAP_CONFIG_ADD:
428 snprintf( c->cr_msg, sizeof( c->cr_msg ),
429 "cannot set legacy attrs when URIs are present" );
430 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
431 c->cr_msg, NULL, NULL );
435 if ( be->be_nsuffix == NULL ) {
436 snprintf( c->cr_msg, sizeof( c->cr_msg ),
437 "suffix must be set" );
438 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
439 c->cr_msg, NULL, NULL );
443 if ( !dnIsSuffix ( &c->value_ndn,
444 &be->be_nsuffix[0] ) ) {
445 snprintf( c->cr_msg, sizeof( c->cr_msg ),
446 "dn is not a suffix of backend base" );
447 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
448 c->cr_msg, NULL, NULL );
453 unique_new_domain ( &private->legacy,
456 legacy = private->legacy;
459 unique_new_domain_uri_basic ( &legacy->uri, c );
460 ch_free ( legacy->uri->dn.bv_val );
461 ch_free ( legacy->uri->ndn.bv_val );
462 legacy->uri->dn = c->value_dn;
463 legacy->uri->ndn = c->value_ndn;
471 ch_free( c->value_dn.bv_val );
472 BER_BVZERO( &c->value_dn );
473 ch_free( c->value_ndn.bv_val );
474 BER_BVZERO( &c->value_ndn );
481 unique_cf_attrs( ConfigArgs *c )
483 slap_overinst *on = (slap_overinst *)c->bi;
484 unique_data *private = (unique_data *) on->on_bi.bi_private;
485 unique_domain *domains = private->domains;
486 unique_domain *legacy = private->legacy;
487 unique_attrs *new_attrs = NULL;
488 unique_attrs *attr, *next_attr, *reverse_attrs;
489 unique_attrs **attrp;
490 int rc = ARG_BAD_CONF;
494 case SLAP_CONFIG_EMIT:
496 && (c->type == UNIQUE_IGNORE) == legacy->ignore
498 for ( attr = legacy->uri->attrs;
501 value_add_one( &c->rvalue_vals,
502 &attr->attr->ad_cname );
505 case LDAP_MOD_DELETE:
507 && (c->type == UNIQUE_IGNORE) == legacy->ignore
509 && legacy->uri->attrs) {
510 if ( c->valx < 0 ) { /* delete all */
511 for ( attr = legacy->uri->attrs;
514 next_attr = attr->next;
517 legacy->uri->attrs = NULL;
518 } else { /* delete by index */
519 attrp = &legacy->uri->attrs;
520 for ( i=0; i < c->valx; ++i )
521 attrp = &(*attrp)->next;
526 if ( !legacy->uri->attrs
527 && !legacy->uri->dn.bv_val ) {
528 unique_free_domain_uri ( legacy->uri );
531 if ( !legacy->uri && !private->legacy_strict_set ) {
532 unique_free_domain ( legacy );
533 private->legacy = legacy = NULL;
539 case SLAP_CONFIG_ADD:
541 snprintf( c->cr_msg, sizeof( c->cr_msg ),
542 "cannot set legacy attrs when URIs are present" );
543 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
544 c->cr_msg, NULL, NULL );
550 && legacy->uri->attrs
551 && (c->type == UNIQUE_IGNORE) != legacy->ignore ) {
552 snprintf( c->cr_msg, sizeof( c->cr_msg ),
553 "cannot set both attrs and ignore-attrs" );
554 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
555 c->cr_msg, NULL, NULL );
560 unique_new_domain ( &private->legacy,
563 legacy = private->legacy;
566 unique_new_domain_uri_basic ( &legacy->uri, c );
568 for ( i=1; c->argv[i]; ++i ) {
569 AttributeDescription * ad = NULL;
571 if ( slap_str2ad ( c->argv[i], &ad, &text )
574 attr = ch_calloc ( 1,
575 sizeof ( unique_attrs ) );
577 attr->next = new_attrs;
580 snprintf( c->cr_msg, sizeof( c->cr_msg ),
581 "unique: attribute: %s: %s",
583 for ( attr = new_attrs;
586 next_attr = attr->next;
595 /* (nconc legacy->uri->attrs (nreverse new_attrs)) */
596 reverse_attrs = NULL;
597 for ( attr = new_attrs;
600 next_attr = attr->next;
601 attr->next = reverse_attrs;
602 reverse_attrs = attr;
604 for ( attrp = &legacy->uri->attrs;
606 attrp = &(*attrp)->next ) ;
607 *attrp = reverse_attrs;
609 legacy->ignore = ( c->type == UNIQUE_IGNORE );
616 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
617 "%s: %s\n", c->log, c->cr_msg, 0 );
623 unique_cf_strict( ConfigArgs *c )
625 slap_overinst *on = (slap_overinst *)c->bi;
626 unique_data *private = (unique_data *) on->on_bi.bi_private;
627 unique_domain *domains = private->domains;
628 unique_domain *legacy = private->legacy;
629 int rc = ARG_BAD_CONF;
632 case SLAP_CONFIG_EMIT:
633 /* We process the boolean manually instead of using
634 * ARG_ON_OFF so that we can three-state it;
635 * olcUniqueStrict is either TRUE, FALSE, or missing,
636 * and missing is necessary to add olcUniqueURIs...
638 if ( private->legacy_strict_set ) {
640 bv.bv_val = legacy->strict ? "TRUE" : "FALSE";
641 bv.bv_len = legacy->strict ?
644 value_add_one ( &c->rvalue_vals, &bv );
648 case LDAP_MOD_DELETE:
651 if ( ! legacy->uri ) {
652 unique_free_domain ( legacy );
653 private->legacy = NULL;
656 private->legacy_strict_set = 0;
660 case SLAP_CONFIG_ADD:
662 snprintf( c->cr_msg, sizeof( c->cr_msg ),
663 "cannot set legacy attrs when URIs are present" );
664 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
665 c->cr_msg, NULL, NULL );
670 unique_new_domain ( &private->legacy,
673 legacy = private->legacy;
675 /* ... not using ARG_ON_OFF makes this necessary too */
676 assert ( c->argc == 2 );
677 legacy->strict = (strcasecmp ( c->argv[1], "TRUE" ) == 0);
678 private->legacy_strict_set = 1;
689 unique_cf_uri( ConfigArgs *c )
691 slap_overinst *on = (slap_overinst *)c->bi;
692 unique_data *private = (unique_data *) on->on_bi.bi_private;
693 unique_domain *domains = private->domains;
694 unique_domain *legacy = private->legacy;
695 unique_domain *domain = NULL, **domainp = NULL;
696 int rc = ARG_BAD_CONF;
700 case SLAP_CONFIG_EMIT:
701 for ( domain = domains;
703 domain = domain->next ) {
704 rc = value_add_one ( &c->rvalue_vals,
705 &domain->domain_spec );
709 case LDAP_MOD_DELETE:
710 if ( c->valx < 0 ) { /* delete them all! */
711 unique_free_domain ( domains );
712 private->domains = NULL;
713 } else { /* delete just one */
714 domainp = &private->domains;
715 for ( i=0; i < c->valx && *domainp; ++i )
716 domainp = &(*domainp)->next;
718 /* If *domainp is null, we walked off the end
719 * of the list. This happens when back-config
720 * and the overlay are out-of-sync, like when
721 * rejecting changes before ITS#4752 gets
724 * This should never happen, but will appear
725 * if you backport this version of
726 * slapo-unique without the config-undo fixes
728 * test024 Will hit this case in such a
731 assert (*domainp != NULL);
734 *domainp = domain->next;
736 unique_free_domain ( domain );
741 case SLAP_CONFIG_ADD: /* fallthrough */
744 snprintf( c->cr_msg, sizeof( c->cr_msg ),
745 "cannot set Uri when legacy attrs are present" );
746 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
747 c->cr_msg, NULL, NULL );
752 if ( c->line ) rc = unique_new_domain ( &domain, c->line, c );
753 else rc = unique_new_domain ( &domain, c->argv[1], c );
755 assert ( domain->next == NULL );
756 for ( domainp = &private->domains;
758 domainp = &(*domainp)->next ) ;
771 ** allocate new unique_data;
772 ** initialize, copy basedn;
773 ** store in on_bi.bi_private;
783 slap_overinst *on = (slap_overinst *)be->bd_info;
784 unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
786 Debug(LDAP_DEBUG_TRACE, "==> unique_db_init\n", 0, 0, 0);
788 *privatep = ch_calloc ( 1, sizeof ( unique_data ) );
799 slap_overinst *on = (slap_overinst *)be->bd_info;
800 unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
801 unique_data *private = *privatep;
803 Debug(LDAP_DEBUG_TRACE, "==> unique_db_destroy\n", 0, 0, 0);
806 unique_domain *domains = private->domains;
807 unique_domain *legacy = private->legacy;
809 unique_free_domain ( domains );
810 unique_free_domain ( legacy );
824 Debug(LDAP_DEBUG_TRACE, "unique_open: overlay initialized\n", 0, 0, 0);
831 ** Leave unique_data but wipe out config
841 slap_overinst *on = (slap_overinst *) be->bd_info;
842 unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
843 unique_data *private = *privatep;
845 Debug(LDAP_DEBUG_TRACE, "==> unique_close\n", 0, 0, 0);
848 unique_domain *domains = private->domains;
849 unique_domain *legacy = private->legacy;
851 unique_free_domain ( domains );
852 unique_free_domain ( legacy );
853 memset ( private, 0, sizeof ( unique_data ) );
862 ** if this is a REP_SEARCH, count++;
866 static int count_attr_cb(
873 /* because you never know */
874 if(!op || !rs) return(0);
876 /* Only search entries are interesting */
877 if(rs->sr_type != REP_SEARCH) return(0);
879 uc = op->o_callback->sc_private;
881 /* Ignore the current entry */
882 if ( dn_match( uc->ndn, &rs->sr_entry->e_nname )) return(0);
884 Debug(LDAP_DEBUG_TRACE, "==> count_attr_cb <%s>\n",
885 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "UNKNOWN_DN", 0, 0);
892 /* count the length of one attribute ad
893 * (and all of its values b)
894 * in the proposed filter
898 unique_domain *domain,
899 unique_domain_uri *uri,
900 AttributeDescription *ad,
908 while ( !is_at_operational( ad->ad_type ) ) {
910 for ( attr = uri->attrs; attr; attr = attr->next ) {
911 if ( ad == attr->attr ) {
915 if ( ( domain->ignore && attr )
916 || (!domain->ignore && !attr )) {
920 if ( b && b[0].bv_val ) {
921 for (i = 0; b[i].bv_val; i++ ) {
922 /* note: make room for filter escaping... */
923 ks += ( 3 * b[i].bv_len ) + ad->ad_cname.bv_len + STRLENOF( "(=)" );
925 } else if ( domain->strict ) {
926 ks += ad->ad_cname.bv_len + STRLENOF( "(=*)" ); /* (attr=*) */
936 unique_domain *domain,
937 unique_domain_uri *uri,
938 AttributeDescription *ad,
948 while ( !is_at_operational( ad->ad_type ) ) {
950 for ( attr = uri->attrs; attr; attr = attr->next ) {
951 if ( ad == attr->attr ) {
955 if ( ( domain->ignore && attr )
956 || (!domain->ignore && !attr )) {
960 if ( b && b[0].bv_val ) {
961 for ( i = 0; b[i].bv_val; i++ ) {
965 ldap_bv2escaped_filter_value_x( &b[i], &bv, 1, ctx );
966 len = snprintf( kp, ks, "(%s=%s)", ad->ad_cname.bv_val, bv.bv_val );
967 assert( len >= 0 && len < ks );
969 if ( bv.bv_val != b[i].bv_val ) {
970 ber_memfree_x( bv.bv_val, ctx );
973 } else if ( domain->strict ) {
975 len = snprintf( kp, ks, "(%s=*)", ad->ad_cname.bv_val );
976 assert( len >= 0 && len < ks );
994 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
995 SlapReply nrs = { REP_RESULT };
996 slap_callback cb = { NULL, NULL, NULL, NULL }; /* XXX */
997 unique_counter uq = { NULL, 0 };
1000 Debug(LDAP_DEBUG_TRACE, "==> unique_search %s\n", key->bv_val, 0, 0);
1002 nop->ors_filter = str2filter_x(nop, key->bv_val);
1003 if(nop->ors_filter == NULL) {
1004 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1005 send_ldap_error(op, rs, LDAP_OTHER,
1006 "unique_search invalid filter");
1010 nop->ors_filterstr = *key;
1012 cb.sc_response = (slap_response*)count_attr_cb;
1013 cb.sc_private = &uq;
1014 nop->o_callback = &cb;
1015 nop->o_tag = LDAP_REQ_SEARCH;
1016 nop->ors_scope = scope;
1017 nop->ors_deref = LDAP_DEREF_NEVER;
1018 nop->ors_limit = NULL;
1019 nop->ors_slimit = SLAP_NO_LIMIT;
1020 nop->ors_tlimit = SLAP_NO_LIMIT;
1021 nop->ors_attrs = slap_anlist_no_attrs;
1022 nop->ors_attrsonly = 1;
1024 uq.ndn = &op->o_req_ndn;
1026 nop->o_req_ndn = *dn;
1027 nop->o_ndn = op->o_bd->be_rootndn;
1029 nop->o_bd = on->on_info->oi_origdb;
1030 rc = nop->o_bd->be_search(nop, &nrs);
1031 filter_free_x(nop, nop->ors_filter, 1);
1032 op->o_tmpfree( key->bv_val, op->o_tmpmemctx );
1034 if(rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT) {
1035 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1036 send_ldap_error(op, rs, rc, "unique_search failed");
1040 Debug(LDAP_DEBUG_TRACE, "=> unique_search found %d records\n", uq.count, 0, 0);
1043 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1044 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
1045 "some attributes not unique");
1049 return(SLAP_CB_CONTINUE);
1058 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1059 unique_data *private = (unique_data *) on->on_bi.bi_private;
1060 unique_domain *domains = private->domains;
1061 unique_domain *legacy = private->legacy;
1062 unique_domain *domain;
1063 Operation nop = *op;
1066 struct berval bvkey;
1067 int rc = SLAP_CB_CONTINUE;
1069 Debug(LDAP_DEBUG_TRACE, "==> unique_add <%s>\n",
1070 op->o_req_dn.bv_val, 0, 0);
1072 for ( domain = legacy ? legacy : domains;
1074 domain = domain->next )
1076 unique_domain_uri *uri;
1078 for ( uri = domain->uri;
1085 if ( uri->ndn.bv_val
1086 && !dnIsSuffix( &op->o_req_ndn, &uri->ndn ))
1090 if ( test_filter( NULL, op->ora_e, uri->f )
1091 == LDAP_COMPARE_FALSE )
1093 Debug( LDAP_DEBUG_TRACE,
1094 "==> unique_add_skip<%s>\n",
1095 op->o_req_dn.bv_val, 0, 0 );
1100 if(!(a = op->ora_e->e_attrs)) {
1101 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1102 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1103 "unique_add() got null op.ora_e.e_attrs");
1108 for(; a; a = a->a_next) {
1109 ks += count_filter_len ( domain,
1116 /* skip this domain-uri if it isn't involved */
1117 if ( !ks ) continue;
1119 /* terminating NUL */
1120 ks += sizeof("(|)");
1122 if ( uri->filter.bv_val && uri->filter.bv_len )
1123 ks += uri->filter.bv_len + STRLENOF ("(&)");
1124 kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1126 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1127 len = snprintf (kp, ks, "(&%s", uri->filter.bv_val);
1128 assert( len >= 0 && len < ks );
1131 len = snprintf(kp, ks - (kp - key), "(|");
1132 assert( len >= 0 && len < ks - (kp - key) );
1135 for(a = op->ora_e->e_attrs; a; a = a->a_next)
1136 kp = build_filter(domain,
1144 len = snprintf(kp, ks - (kp - key), ")");
1145 assert( len >= 0 && len < ks - (kp - key) );
1147 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1148 len = snprintf(kp, ks - (kp - key), ")");
1149 assert( len >= 0 && len < ks - (kp - key) );
1153 bvkey.bv_len = kp - key;
1155 rc = unique_search ( op,
1159 &op->o_bd->be_nsuffix[0],
1164 if ( rc != SLAP_CB_CONTINUE ) break;
1166 if ( rc != SLAP_CB_CONTINUE ) break;
1179 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1180 unique_data *private = (unique_data *) on->on_bi.bi_private;
1181 unique_domain *domains = private->domains;
1182 unique_domain *legacy = private->legacy;
1183 unique_domain *domain;
1184 Operation nop = *op;
1187 struct berval bvkey;
1188 int rc = SLAP_CB_CONTINUE;
1190 Debug(LDAP_DEBUG_TRACE, "==> unique_modify <%s>\n",
1191 op->o_req_dn.bv_val, 0, 0);
1193 for ( domain = legacy ? legacy : domains;
1195 domain = domain->next )
1197 unique_domain_uri *uri;
1199 for ( uri = domain->uri;
1206 if ( uri->ndn.bv_val
1207 && !dnIsSuffix( &op->o_req_ndn, &uri->ndn ))
1210 if ( !(m = op->orm_modlist) ) {
1211 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1212 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1213 "unique_modify() got null op.orm_modlist");
1218 for ( ; m; m = m->sml_next)
1219 if ( (m->sml_op & LDAP_MOD_OP)
1220 != LDAP_MOD_DELETE )
1221 ks += count_filter_len
1227 /* skip this domain-uri if it isn't involved */
1228 if ( !ks ) continue;
1230 /* terminating NUL */
1231 ks += sizeof("(|)");
1233 if ( uri->filter.bv_val && uri->filter.bv_len )
1234 ks += uri->filter.bv_len + STRLENOF ("(&)");
1235 kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1237 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1238 len = snprintf(kp, ks, "(&%s", uri->filter.bv_val);
1239 assert( len >= 0 && len < ks );
1242 len = snprintf(kp, ks - (kp - key), "(|");
1243 assert( len >= 0 && len < ks - (kp - key) );
1246 for(m = op->orm_modlist; m; m = m->sml_next)
1247 if ( (m->sml_op & LDAP_MOD_OP)
1248 != LDAP_MOD_DELETE )
1249 kp = build_filter ( domain,
1257 len = snprintf(kp, ks - (kp - key), ")");
1258 assert( len >= 0 && len < ks - (kp - key) );
1260 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1261 len = snprintf (kp, ks - (kp - key), ")");
1262 assert( len >= 0 && len < ks - (kp - key) );
1266 bvkey.bv_len = kp - key;
1268 rc = unique_search ( op,
1272 &op->o_bd->be_nsuffix[0],
1277 if ( rc != SLAP_CB_CONTINUE ) break;
1279 if ( rc != SLAP_CB_CONTINUE ) break;
1292 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1293 unique_data *private = (unique_data *) on->on_bi.bi_private;
1294 unique_domain *domains = private->domains;
1295 unique_domain *legacy = private->legacy;
1296 unique_domain *domain;
1297 Operation nop = *op;
1299 struct berval bvkey;
1301 struct berval bv[2];
1302 int rc = SLAP_CB_CONTINUE;
1304 Debug(LDAP_DEBUG_TRACE, "==> unique_modrdn <%s> <%s>\n",
1305 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
1307 for ( domain = legacy ? legacy : domains;
1309 domain = domain->next )
1311 unique_domain_uri *uri;
1313 for ( uri = domain->uri;
1320 if ( uri->ndn.bv_val
1321 && !dnIsSuffix( &op->o_req_ndn, &uri->ndn )
1322 && (!op->orr_nnewSup
1323 || !dnIsSuffix( op->orr_nnewSup, &uri->ndn )))
1326 if ( ldap_bv2rdn_x ( &op->oq_modrdn.rs_newrdn,
1328 (char **)&rs->sr_text,
1329 LDAP_DN_FORMAT_LDAP,
1330 op->o_tmpmemctx ) ) {
1331 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1332 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1333 "unknown type(s) used in RDN");
1338 rc = SLAP_CB_CONTINUE;
1339 for ( i=0; newrdn[i]; i++) {
1340 AttributeDescription *ad = NULL;
1341 if ( slap_bv2ad( &newrdn[i]->la_attr, &ad, &rs->sr_text )) {
1342 ldap_rdnfree_x( newrdn, op->o_tmpmemctx );
1343 rs->sr_err = LDAP_INVALID_SYNTAX;
1344 send_ldap_result( op, rs );
1348 newrdn[i]->la_private = ad;
1350 if ( rc != SLAP_CB_CONTINUE ) break;
1352 bv[1].bv_val = NULL;
1355 for ( i=0; newrdn[i]; i++ ) {
1356 bv[0] = newrdn[i]->la_value;
1357 ks += count_filter_len ( domain,
1359 newrdn[i]->la_private,
1363 /* skip this domain if it isn't involved */
1364 if ( !ks ) continue;
1366 /* terminating NUL */
1367 ks += sizeof("(|)");
1369 if ( uri->filter.bv_val && uri->filter.bv_len )
1370 ks += uri->filter.bv_len + STRLENOF ("(&)");
1371 kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1373 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1374 len = snprintf(kp, ks, "(&%s", uri->filter.bv_val);
1375 assert( len >= 0 && len < ks );
1378 len = snprintf(kp, ks - (kp - key), "(|");
1379 assert( len >= 0 && len < ks - (kp - key) );
1382 for ( i=0; newrdn[i]; i++) {
1383 bv[0] = newrdn[i]->la_value;
1384 kp = build_filter ( domain,
1386 newrdn[i]->la_private,
1393 len = snprintf(kp, ks - (kp - key), ")");
1394 assert( len >= 0 && len < ks - (kp - key) );
1396 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1397 len = snprintf (kp, ks - (kp - key), ")");
1398 assert( len >= 0 && len < ks - (kp - key) );
1402 bvkey.bv_len = kp - key;
1404 rc = unique_search ( op,
1408 &op->o_bd->be_nsuffix[0],
1413 if ( rc != SLAP_CB_CONTINUE ) break;
1415 if ( rc != SLAP_CB_CONTINUE ) break;
1422 ** init_module is last so the symbols resolve "for free" --
1423 ** it expects to be called automagically during dynamic module initialization
1431 /* statically declared just after the #includes at top */
1432 memset (&unique, 0, sizeof(unique));
1434 unique.on_bi.bi_type = "unique";
1435 unique.on_bi.bi_db_init = unique_db_init;
1436 unique.on_bi.bi_db_destroy = unique_db_destroy;
1437 unique.on_bi.bi_db_open = unique_open;
1438 unique.on_bi.bi_db_close = unique_close;
1439 unique.on_bi.bi_op_add = unique_add;
1440 unique.on_bi.bi_op_modify = unique_modify;
1441 unique.on_bi.bi_op_modrdn = unique_modrdn;
1443 unique.on_bi.bi_cf_ocs = uniqueocs;
1444 rc = config_register_schema( uniquecfg, uniqueocs );
1445 if ( rc ) return rc;
1447 return(overlay_register(&unique));
1450 #if SLAPD_OVER_UNIQUE == SLAPD_MOD_DYNAMIC && defined(PIC)
1451 int init_module(int argc, char *argv[]) {
1452 return unique_initialize();
1456 #endif /* SLAPD_OVER_UNIQUE */