1 /* unique.c - attribute uniqueness module */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2004-2007 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;
49 struct berval *filter;
50 struct unique_attrs_s *attrs;
54 typedef struct unique_domain_s {
55 struct unique_domain_s *next;
56 struct berval *domain_spec;
57 struct unique_domain_uri_s *uri;
58 char ignore; /* polarity of attributes */
59 char strict; /* null considered unique too */
62 typedef struct unique_data_s {
63 struct unique_domain_s *domains;
64 struct unique_domain_s *legacy;
65 char legacy_strict_set;
68 typedef struct unique_counter_s {
81 static ConfigDriver unique_cf_base;
82 static ConfigDriver unique_cf_attrs;
83 static ConfigDriver unique_cf_strict;
84 static ConfigDriver unique_cf_uri;
86 static ConfigTable uniquecfg[] = {
87 { "unique_base", "basedn", 2, 2, 0, ARG_DN|ARG_MAGIC|UNIQUE_BASE,
88 unique_cf_base, "( OLcfgOvAt:10.1 NAME 'olcUniqueBase' "
89 "DESC 'Subtree for uniqueness searches' "
90 "EQUALITY distinguishedNameMatch "
91 "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
92 { "unique_ignore", "attribute...", 2, 0, 0, ARG_MAGIC|UNIQUE_IGNORE,
93 unique_cf_attrs, "( OLcfgOvAt:10.2 NAME 'olcUniqueIgnore' "
94 "DESC 'Attributes for which uniqueness shall not be enforced' "
95 "EQUALITY caseIgnoreMatch "
96 "ORDERING caseIgnoreOrderingMatch "
97 "SUBSTR caseIgnoreSubstringsMatch "
98 "SYNTAX OMsDirectoryString )", NULL, NULL },
99 { "unique_attributes", "attribute...", 2, 0, 0, ARG_MAGIC|UNIQUE_ATTR,
100 unique_cf_attrs, "( OLcfgOvAt:10.3 NAME 'olcUniqueAttribute' "
101 "DESC 'Attributes for which uniqueness shall be enforced' "
102 "EQUALITY caseIgnoreMatch "
103 "ORDERING caseIgnoreOrderingMatch "
104 "SUBSTR caseIgnoreSubstringsMatch "
105 "SYNTAX OMsDirectoryString )", NULL, NULL },
106 { "unique_strict", "on|off", 1, 2, 0, ARG_MAGIC|UNIQUE_STRICT,
107 unique_cf_strict, "( OLcfgOvAt:10.4 NAME 'olcUniqueStrict' "
108 "DESC 'Enforce uniqueness of null values' "
109 "EQUALITY booleanMatch "
110 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
111 { "unique_uri", "ldapuri", 2, 3, 0, ARG_MAGIC|UNIQUE_URI,
112 unique_cf_uri, "( OLcfgOvAt:10.5 NAME 'olcUniqueURI' "
113 "DESC 'List of keywords and LDAP URIs for a uniqueness domain' "
114 "EQUALITY caseExactMatch "
115 "ORDERING caseExactOrderingMatch "
116 "SUBSTR caseExactSubstringsMatch "
117 "SYNTAX OMsDirectoryString )", NULL, NULL },
118 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
121 static ConfigOCs uniqueocs[] = {
122 { "( OLcfgOvOc:10.1 "
123 "NAME 'olcUniqueConfig' "
124 "DESC 'Attribute value uniqueness configuration' "
125 "SUP olcOverlayConfig "
126 "MAY ( olcUniqueBase $ olcUniqueIgnore $ "
127 "olcUniqueAttribute $ olcUniqueStrict $ "
129 Cft_Overlay, uniquecfg },
134 unique_free_domain_uri ( unique_domain_uri *uri )
136 unique_domain_uri *next_uri = NULL;
137 unique_attrs *attr, *next_attr = NULL;
140 next_uri = uri->next;
141 ber_bvfree ( uri->dn );
142 ber_bvfree ( uri->ndn );
143 ber_bvfree ( uri->filter );
146 next_attr = attr->next;
155 /* free an entire stack of domains */
157 unique_free_domain ( unique_domain *domain )
159 unique_domain *next_domain = NULL;
162 next_domain = domain->next;
163 ber_bvfree ( domain->domain_spec );
164 unique_free_domain_uri ( domain->uri );
166 domain = next_domain;
171 unique_new_domain_uri ( unique_domain_uri **urip,
172 const LDAPURLDesc *url_desc,
175 int i, rc = LDAP_SUCCESS;
176 unique_domain_uri *uri;
177 struct berval bv = {0, NULL};
178 BackendDB *be = (BackendDB *)c->be;
180 AttributeDescription * ad;
183 uri = ch_calloc ( 1, sizeof ( unique_domain_uri ) );
185 if ( url_desc->lud_dn && url_desc->lud_dn[0] ) {
186 ber_str2bv( url_desc->lud_dn, 0, 1, &bv );
187 rc = dnPrettyNormal( NULL,
192 if ( rc != LDAP_SUCCESS ) {
193 snprintf( c->msg, sizeof( c->msg ),
194 "<%s> invalid DN %d (%s)",
195 url_desc->lud_dn, rc, ldap_err2string( rc ));
200 if ( !dnIsSuffix ( uri->ndn, &be->be_nsuffix[0] ) ) {
202 "dn <%s> is not a suffix of backend base dn <%s>",
204 be->be_nsuffix[0].bv_val );
210 attr_str = url_desc->lud_attrs;
212 for ( i=0; attr_str[i]; ++i ) {
215 if ( slap_str2ad ( attr_str[i], &ad, &text )
217 attr = ch_calloc ( 1,
218 sizeof ( unique_attrs ) );
220 attr->next = uri->attrs;
223 snprintf( c->msg, sizeof( c->msg ),
224 "unique: attribute: %s: %s",
232 uri->scope = url_desc->lud_scope;
234 snprintf( c->msg, sizeof( c->msg ),
235 "unique: uri with base scope will always be unique");
240 if (url_desc->lud_filter) {
242 uri->filter = ber_str2bv( url_desc->lud_filter, 0, 1, NULL);
243 f = str2filter( uri->filter->bv_val );
245 snprintf( c->msg, sizeof( c->msg ),
246 "unique: bad filter");
253 if ( bv.bv_val ) ber_memfree ( bv.bv_val );
257 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
258 "%s: %s\n", c->log, c->msg, 0 );
259 unique_free_domain_uri ( uri );
266 unique_new_domain_uri_basic ( unique_domain_uri **urip,
269 LDAPURLDesc *url_desc = NULL;
272 rc = ldap_url_parse ( UNIQUE_DEFAULT_URI, &url_desc );
274 rc = unique_new_domain_uri ( urip, url_desc, c );
275 ldap_free_urldesc ( url_desc );
279 /* if *domain is non-null, it's pushed down the stack.
280 * note that the entire stack is freed if there is an error,
281 * so build added domains in a separate stack before adding them
283 * domain_specs look like
285 * [strict ][ignore ]uri[[ uri]...]
286 * e.g. "ldap:///ou=foo,o=bar?uid?sub ldap:///ou=baz,o=bar?uid?sub"
287 * "strict ldap:///ou=accounts,o=bar?uid,uidNumber?one"
290 * so finally strictness is per-domain
291 * but so is ignore-state, and that would be better as a per-url thing
294 unique_new_domain ( unique_domain **domainp,
299 int rc = LDAP_SUCCESS;
301 unique_domain * domain;
302 LDAPURLDesc *url_desc, *url_descs = NULL;
304 Debug(LDAP_DEBUG_TRACE, "==> unique_new_domain <%s>\n",
307 domain = ch_calloc ( 1, sizeof (unique_domain) );
308 domain->domain_spec = ber_str2bv( domain_spec, 0, 1, NULL );
310 uri_start = domain_spec;
311 if ( strncasecmp ( uri_start, "ignore ",
312 STRLENOF( "ignore " ) ) == 0 ) {
314 uri_start += STRLENOF( "ignore " );
316 if ( strncasecmp ( uri_start, "strict ",
317 STRLENOF( "strict " ) ) == 0 ) {
319 uri_start += STRLENOF( "strict " );
321 && strncasecmp ( uri_start, "ignore ",
322 STRLENOF( "ignore " ) ) == 0 ) {
324 uri_start += STRLENOF( "ignore " );
327 rc = ldap_url_parselist_ext ( &url_descs, uri_start, " ", 0 );
329 snprintf( c->msg, sizeof( c->msg ),
330 "<%s> invalid ldap urilist",
336 for ( url_desc = url_descs;
338 url_desc = url_descs->lud_next ) {
339 rc = unique_new_domain_uri ( &domain->uri,
350 if ( url_descs ) ldap_free_urldesc ( url_descs );
351 domain->next = *domainp;
354 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
355 "%s: %s\n", c->log, c->msg, 0 );
356 unique_free_domain ( domain );
363 unique_cf_base( ConfigArgs *c )
365 BackendDB *be = (BackendDB *)c->be;
366 slap_overinst *on = (slap_overinst *)c->bi;
367 unique_data *private = (unique_data *) on->on_bi.bi_private;
368 unique_domain *domains = private->domains;
369 unique_domain *legacy = private->legacy;
370 int rc = ARG_BAD_CONF;
373 case SLAP_CONFIG_EMIT:
375 if ( legacy && legacy->uri && legacy->uri->dn ) {
376 rc = value_add_one ( &c->rvalue_vals,
379 rc = value_add_one ( &c->rvalue_nvals,
384 case LDAP_MOD_DELETE:
385 assert ( legacy && legacy->uri && legacy->uri->dn );
387 ber_bvfree ( legacy->uri->dn );
388 ber_bvfree ( legacy->uri->ndn );
389 legacy->uri->dn = NULL;
390 legacy->uri->ndn = NULL;
391 if ( !legacy->uri->attrs
392 && !legacy->uri->dn ) {
393 unique_free_domain_uri ( legacy->uri );
396 if ( !legacy->uri && !private->legacy_strict_set ) {
397 unique_free_domain ( legacy );
398 private->legacy = legacy = NULL;
402 case SLAP_CONFIG_ADD:
405 "cannot set legacy attrs when URIs are present" );
406 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
407 c->msg, NULL, NULL );
411 if ( !dnIsSuffix ( &c->value_ndn,
412 &be->be_nsuffix[0] ) ) {
414 "dn is not a suffix of backend base" );
415 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
416 c->msg, NULL, NULL );
421 unique_new_domain ( &private->legacy,
424 legacy = private->legacy;
427 unique_new_domain_uri_basic ( &legacy->uri, c );
428 ber_bvfree ( legacy->uri->dn );
429 ber_bvfree ( legacy->uri->ndn );
430 legacy->uri->dn = ber_bvdup ( &c->value_dn );
431 legacy->uri->ndn = ber_bvdup ( &c->value_ndn );
442 unique_cf_attrs( ConfigArgs *c )
444 slap_overinst *on = (slap_overinst *)c->bi;
445 unique_data *private = (unique_data *) on->on_bi.bi_private;
446 unique_domain *domains = private->domains;
447 unique_domain *legacy = private->legacy;
448 unique_attrs *new_attrs = NULL;
449 unique_attrs *attr, *next_attr, *reverse_attrs;
450 unique_attrs **attrp;
451 int rc = ARG_BAD_CONF;
455 case SLAP_CONFIG_EMIT:
457 && (c->type == UNIQUE_IGNORE) == legacy->ignore
459 for ( attr = legacy->uri->attrs;
462 value_add_one( &c->rvalue_vals,
463 &attr->attr->ad_cname );
466 case LDAP_MOD_DELETE:
468 && (c->type == UNIQUE_IGNORE) == legacy->ignore
470 && legacy->uri->attrs) {
471 if ( c->valx < 0 ) { /* delete all */
472 for ( attr = legacy->uri->attrs;
475 next_attr = attr->next;
478 legacy->uri->attrs = NULL;
479 } else { /* delete by index */
480 attrp = &legacy->uri->attrs;
481 for ( i=0; i < c->valx; ++i )
482 attrp = &(*attrp)->next;
487 if ( !legacy->uri->attrs
488 && !legacy->uri->dn ) {
489 unique_free_domain_uri ( legacy->uri );
492 if ( !legacy->uri && !private->legacy_strict_set ) {
493 unique_free_domain ( legacy );
494 private->legacy = legacy = NULL;
500 case SLAP_CONFIG_ADD:
503 "cannot set legacy attrs when URIs are present" );
504 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
505 c->msg, NULL, NULL );
511 && legacy->uri->attrs
512 && (c->type == UNIQUE_IGNORE) != legacy->ignore ) {
514 "cannot set both attrs and ignore-attrs" );
515 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
516 c->msg, NULL, NULL );
521 unique_new_domain ( &private->legacy,
524 legacy = private->legacy;
527 unique_new_domain_uri_basic ( &legacy->uri, c );
529 for ( i=1; c->argv[i]; ++i ) {
530 AttributeDescription * ad = NULL;
532 if ( slap_str2ad ( c->argv[i], &ad, &text )
535 attr = ch_calloc ( 1,
536 sizeof ( unique_attrs ) );
538 attr->next = new_attrs;
541 snprintf( c->msg, sizeof( c->msg ),
542 "unique: attribute: %s: %s",
544 for ( attr = new_attrs;
547 next_attr = attr->next;
556 /* (nconc legacy->uri->attrs (nreverse new_attrs)) */
557 reverse_attrs = NULL;
558 for ( attr = new_attrs;
561 next_attr = attr->next;
562 attr->next = reverse_attrs;
563 reverse_attrs = attr;
565 for ( attrp = &legacy->uri->attrs;
567 attrp = &(*attrp)->next ) ;
568 *attrp = reverse_attrs;
570 legacy->ignore = ( c->type == UNIQUE_IGNORE );
577 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
578 "%s: %s\n", c->log, c->msg, 0 );
584 unique_cf_strict( ConfigArgs *c )
586 slap_overinst *on = (slap_overinst *)c->bi;
587 unique_data *private = (unique_data *) on->on_bi.bi_private;
588 unique_domain *domains = private->domains;
589 unique_domain *legacy = private->legacy;
590 int rc = ARG_BAD_CONF;
593 case SLAP_CONFIG_EMIT:
594 /* We process the boolean manually instead of using
595 * ARG_ON_OFF so that we can three-state it;
596 * olcUniqueStrict is either TRUE, FALSE, or missing,
597 * and missing is necessary to add olcUniqueURIs...
599 if ( private->legacy_strict_set ) {
601 bv.bv_val = legacy->strict ? "TRUE" : "FALSE";
602 bv.bv_len = legacy->strict ?
605 value_add_one ( &c->rvalue_vals, &bv );
609 case LDAP_MOD_DELETE:
612 if ( ! legacy->uri ) {
613 unique_free_domain ( legacy );
614 private->legacy = NULL;
617 private->legacy_strict_set = 0;
621 case SLAP_CONFIG_ADD:
624 "cannot set legacy attrs when URIs are present" );
625 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
626 c->msg, NULL, NULL );
631 unique_new_domain ( &private->legacy,
634 legacy = private->legacy;
636 /* ... not using ARG_ON_OFF makes this necessary too */
637 assert ( c->argc == 2 );
638 legacy->strict = (strcasecmp ( c->argv[1], "TRUE" ) == 0);
639 private->legacy_strict_set = 1;
650 unique_cf_uri( ConfigArgs *c )
652 slap_overinst *on = (slap_overinst *)c->bi;
653 unique_data *private = (unique_data *) on->on_bi.bi_private;
654 unique_domain *domains = private->domains;
655 unique_domain *legacy = private->legacy;
656 unique_domain *domain = NULL, **domainp = NULL;
657 int rc = ARG_BAD_CONF;
661 case SLAP_CONFIG_EMIT:
662 for ( domain = domains;
664 domain = domain->next ) {
665 rc = value_add_one ( &c->rvalue_vals,
666 domain->domain_spec );
668 rc = value_add_one ( &c->rvalue_nvals,
669 domain->domain_spec );
673 case LDAP_MOD_DELETE:
674 if ( c->valx < 0 ) { /* delete them all! */
675 unique_free_domain ( domains );
676 private->domains = NULL;
677 } else { /* delete just one */
678 domainp = &private->domains;
679 for ( i=0; i < c->valx && *domainp; ++i )
680 domainp = &(*domainp)->next;
682 /* If *domainp is null, we walked off the end
683 * of the list. This happens when back-config
684 * and the overlay are out-of-sync, like when
685 * rejecting changes before ITS#4752 gets
688 * This should never happen, but will appear
689 * if you backport this version of
690 * slapo-unique without the config-undo fixes
692 * test024 Will hit this case in such a
695 assert (*domainp != NULL);
698 *domainp = domain->next;
700 unique_free_domain ( domain );
705 case SLAP_CONFIG_ADD: /* fallthrough */
709 "cannot set Uri when legacy attrs are present" );
710 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
711 c->msg, NULL, NULL );
716 if ( c->line ) rc = unique_new_domain ( &domain, c->line, c );
717 else rc = unique_new_domain ( &domain, c->argv[1], c );
719 assert ( domain->next == NULL );
720 for ( domainp = &private->domains;
722 domainp = &(*domainp)->next ) ;
735 ** allocate new unique_data;
736 ** initialize, copy basedn;
737 ** store in on_bi.bi_private;
746 slap_overinst *on = (slap_overinst *)be->bd_info;
747 unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
749 Debug(LDAP_DEBUG_TRACE, "==> unique_db_init\n", 0, 0, 0);
751 *privatep = ch_calloc ( 1, sizeof ( unique_data ) );
761 slap_overinst *on = (slap_overinst *)be->bd_info;
762 unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
763 unique_data *private = *privatep;
765 Debug(LDAP_DEBUG_TRACE, "==> unique_db_destroy\n", 0, 0, 0);
768 unique_domain *domains = private->domains;
769 unique_domain *legacy = private->legacy;
771 unique_free_domain ( domains );
772 unique_free_domain ( legacy );
785 Debug(LDAP_DEBUG_TRACE, "unique_open: overlay initialized\n", 0, 0, 0);
792 ** Leave unique_data but wipe out config
801 slap_overinst *on = (slap_overinst *) be->bd_info;
802 unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
803 unique_data *private = *privatep;
805 Debug(LDAP_DEBUG_TRACE, "==> unique_close\n", 0, 0, 0);
808 unique_domain *domains = private->domains;
809 unique_domain *legacy = private->legacy;
811 unique_free_domain ( domains );
812 unique_free_domain ( legacy );
813 memset ( private, 0, sizeof ( unique_data ) );
822 ** if this is a REP_SEARCH, count++;
826 static int count_attr_cb(
833 /* because you never know */
834 if(!op || !rs) return(0);
836 /* Only search entries are interesting */
837 if(rs->sr_type != REP_SEARCH) return(0);
839 uc = op->o_callback->sc_private;
841 /* Ignore the current entry */
842 if ( dn_match( uc->ndn, &rs->sr_entry->e_nname )) return(0);
844 Debug(LDAP_DEBUG_TRACE, "==> count_attr_cb <%s>\n",
845 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "UNKNOWN_DN", 0, 0);
852 /* count the length of one attribute ad
853 * (and all of its values b)
854 * in the proposed filter
858 unique_domain *domain,
859 unique_domain_uri *uri,
860 AttributeDescription *ad,
868 while ( !is_at_operational( ad->ad_type ) ) {
870 for ( attr = uri->attrs; attr; attr = attr->next ) {
871 if ( ad == attr->attr ) {
875 if ( ( domain->ignore && attr )
876 || (!domain->ignore && !attr )) {
880 if ( b && b[0].bv_val ) {
881 for (i = 0; b[i].bv_val; i++ ) {
882 /* note: make room for filter escaping... */
883 ks += ( 3 * b[i].bv_len ) + ad->ad_cname.bv_len + STRLENOF( "(=)" );
885 } else if ( domain->strict ) {
886 ks += ad->ad_cname.bv_len + STRLENOF( "(=*)" ); /* (attr=*) */
896 unique_domain *domain,
897 unique_domain_uri *uri,
898 AttributeDescription *ad,
907 while ( !is_at_operational( ad->ad_type ) ) {
909 for ( attr = uri->attrs; attr; attr = attr->next ) {
910 if ( ad == attr->attr ) {
914 if ( ( domain->ignore && attr )
915 || (!domain->ignore && !attr )) {
919 if ( b && b[0].bv_val ) {
920 for ( i = 0; b[i].bv_val; i++ ) {
923 ldap_bv2escaped_filter_value_x( &b[i], &bv, 1, ctx );
924 kp += sprintf( kp, "(%s=%s)", ad->ad_cname.bv_val, bv.bv_val );
925 if ( bv.bv_val != b[i].bv_val ) {
926 ber_memfree_x( bv.bv_val, ctx );
929 } else if ( domain->strict ) {
930 kp += sprintf( kp, "(%s=*)", ad->ad_cname.bv_val );
947 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
948 SlapReply nrs = { REP_RESULT };
949 slap_callback cb = { NULL, NULL, NULL, NULL }; /* XXX */
950 unique_counter uq = { NULL, 0 };
953 Debug(LDAP_DEBUG_TRACE, "==> unique_search %s\n", key, 0, 0);
955 nop->ors_filter = str2filter_x(nop, key);
956 ber_str2bv(key, 0, 0, &nop->ors_filterstr);
958 cb.sc_response = (slap_response*)count_attr_cb;
960 nop->o_callback = &cb;
961 nop->o_tag = LDAP_REQ_SEARCH;
962 nop->ors_scope = scope;
963 nop->ors_deref = LDAP_DEREF_NEVER;
964 nop->ors_limit = NULL;
965 nop->ors_slimit = SLAP_NO_LIMIT;
966 nop->ors_tlimit = SLAP_NO_LIMIT;
967 nop->ors_attrs = slap_anlist_no_attrs;
968 nop->ors_attrsonly = 1;
970 uq.ndn = &op->o_req_ndn;
972 nop->o_req_ndn = *dn;
973 nop->o_ndn = op->o_bd->be_rootndn;
975 nop->o_bd = on->on_info->oi_origdb;
976 rc = nop->o_bd->be_search(nop, &nrs);
977 filter_free_x(nop, nop->ors_filter);
978 op->o_tmpfree( key, op->o_tmpmemctx );
980 if(rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT) {
981 op->o_bd->bd_info = (BackendInfo *) on->on_info;
982 send_ldap_error(op, rs, rc, "unique_search failed");
986 Debug(LDAP_DEBUG_TRACE, "=> unique_search found %d records\n", uq.count, 0, 0);
989 op->o_bd->bd_info = (BackendInfo *) on->on_info;
990 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
991 "some attributes not unique");
995 return(SLAP_CB_CONTINUE);
1004 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1005 unique_data *private = (unique_data *) on->on_bi.bi_private;
1006 unique_domain *domains = private->domains;
1007 unique_domain *legacy = private->legacy;
1008 unique_domain *domain;
1009 Operation nop = *op;
1012 int rc = SLAP_CB_CONTINUE;
1014 Debug(LDAP_DEBUG_TRACE, "==> unique_add <%s>\n",
1015 op->o_req_dn.bv_val, 0, 0);
1017 for ( domain = legacy ? legacy : domains;
1019 domain = domain->next ) {
1020 unique_domain_uri *uri;
1023 for ( uri = domain->uri;
1028 && !dnIsSuffix( &op->o_req_ndn, uri->ndn ))
1031 if(!(a = op->ora_e->e_attrs)) {
1032 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1033 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1034 "unique_add() got null op.ora_e.e_attrs");
1039 for(; a; a = a->a_next) {
1040 ks += count_filter_len ( domain,
1047 /* skip this domain-uri if it isn't involved */
1048 if ( !ks ) continue;
1050 if ( uri->filter && uri->filter->bv_len )
1051 ks += uri->filter->bv_len + STRLENOF ("(&)");
1052 kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1054 if ( uri->filter && uri->filter->bv_len )
1055 kp += sprintf (kp, "(&%s", uri->filter->bv_val);
1056 kp += sprintf(kp, "(|");
1058 for(a = op->ora_e->e_attrs; a; a = a->a_next)
1059 kp = build_filter(domain,
1066 kp += sprintf(kp, ")");
1067 if ( uri->filter && uri->filter->bv_len )
1068 kp += sprintf (kp, ")");
1070 rc = unique_search ( op,
1074 &op->o_bd->be_nsuffix[0],
1079 if ( rc != SLAP_CB_CONTINUE ) break;
1081 if ( rc != SLAP_CB_CONTINUE ) break;
1094 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1095 unique_data *private = (unique_data *) on->on_bi.bi_private;
1096 unique_domain *domains = private->domains;
1097 unique_domain *legacy = private->legacy;
1098 unique_domain *domain;
1099 Operation nop = *op;
1102 int rc = SLAP_CB_CONTINUE;
1104 Debug(LDAP_DEBUG_TRACE, "==> unique_modify <%s>\n",
1105 op->o_req_dn.bv_val, 0, 0);
1107 for ( domain = legacy ? legacy : domains;
1109 domain = domain->next ) {
1110 unique_domain_uri *uri;
1113 for ( uri = domain->uri;
1118 && !dnIsSuffix( &op->o_req_ndn, uri->ndn ))
1121 if ( !(m = op->orm_modlist) ) {
1122 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1123 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1124 "unique_modify() got null op.orm_modlist");
1129 for ( ; m; m = m->sml_next)
1130 if ( (m->sml_op & LDAP_MOD_OP)
1131 != LDAP_MOD_DELETE )
1132 ks += count_filter_len
1138 /* skip this domain-uri if it isn't involved */
1139 if ( !ks ) continue;
1141 if ( uri->filter && uri->filter->bv_len )
1142 ks += uri->filter->bv_len + STRLENOF ("(&)");
1143 kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1145 if ( uri->filter && uri->filter->bv_len )
1146 kp += sprintf (kp, "(&%s", uri->filter->bv_val);
1147 kp += sprintf(kp, "(|");
1149 for(m = op->orm_modlist; m; m = m->sml_next)
1150 if ( (m->sml_op & LDAP_MOD_OP)
1151 != LDAP_MOD_DELETE )
1152 kp = build_filter ( domain,
1159 kp += sprintf (kp, ")");
1160 if ( uri->filter && uri->filter->bv_len )
1161 kp += sprintf (kp, ")");
1163 rc = unique_search ( op,
1167 &op->o_bd->be_nsuffix[0],
1172 if ( rc != SLAP_CB_CONTINUE ) break;
1174 if ( rc != SLAP_CB_CONTINUE ) break;
1187 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1188 unique_data *private = (unique_data *) on->on_bi.bi_private;
1189 unique_domain *domains = private->domains;
1190 unique_domain *legacy = private->legacy;
1191 unique_domain *domain;
1192 Operation nop = *op;
1195 struct berval bv[2];
1196 int rc = SLAP_CB_CONTINUE;
1198 Debug(LDAP_DEBUG_TRACE, "==> unique_modrdn <%s> <%s>\n",
1199 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
1201 for ( domain = legacy ? legacy : domains;
1203 domain = domain->next ) {
1204 unique_domain_uri *uri;
1207 for ( uri = domain->uri;
1213 && !dnIsSuffix( &op->o_req_ndn, uri->ndn )
1214 && (!op->orr_nnewSup
1215 || !dnIsSuffix( op->orr_nnewSup, uri->ndn )))
1218 if ( ldap_bv2rdn_x ( &op->oq_modrdn.rs_newrdn,
1220 (char **)&rs->sr_text,
1221 LDAP_DN_FORMAT_LDAP,
1222 op->o_tmpmemctx ) ) {
1223 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1224 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1225 "unknown type(s) used in RDN");
1230 rc = SLAP_CB_CONTINUE;
1231 for ( i=0; newrdn[i]; i++) {
1232 AttributeDescription *ad = NULL;
1233 if ( slap_bv2ad( &newrdn[i]->la_attr, &ad, &rs->sr_text )) {
1234 ldap_rdnfree_x( newrdn, op->o_tmpmemctx );
1235 rs->sr_err = LDAP_INVALID_SYNTAX;
1236 send_ldap_result( op, rs );
1240 newrdn[i]->la_private = ad;
1242 if ( rc != SLAP_CB_CONTINUE ) break;
1244 bv[1].bv_val = NULL;
1247 for ( i=0; newrdn[i]; i++ ) {
1248 bv[0] = newrdn[i]->la_value;
1249 ks += count_filter_len ( domain,
1251 newrdn[i]->la_private,
1255 /* skip this domain if it isn't involved */
1256 if ( !ks ) continue;
1258 if ( uri->filter && uri->filter->bv_len )
1259 ks += uri->filter->bv_len + STRLENOF ("(&)");
1260 kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1262 if ( uri->filter && uri->filter->bv_len )
1263 kp += sprintf (kp, "(&%s", uri->filter->bv_val);
1264 kp += sprintf(kp, "(|");
1266 for ( i=0; newrdn[i]; i++) {
1267 bv[0] = newrdn[i]->la_value;
1268 kp = build_filter ( domain,
1270 newrdn[i]->la_private,
1276 kp += sprintf(kp, ")");
1277 if ( uri->filter && uri->filter->bv_len )
1278 kp += sprintf (kp, ")");
1280 rc = unique_search ( op,
1284 &op->o_bd->be_nsuffix[0],
1289 if ( rc != SLAP_CB_CONTINUE ) break;
1291 if ( rc != SLAP_CB_CONTINUE ) break;
1298 ** init_module is last so the symbols resolve "for free" --
1299 ** it expects to be called automagically during dynamic module initialization
1307 /* statically declared just after the #includes at top */
1308 memset (&unique, 0, sizeof(unique));
1310 unique.on_bi.bi_type = "unique";
1311 unique.on_bi.bi_db_init = unique_db_init;
1312 unique.on_bi.bi_db_destroy = unique_db_destroy;
1313 unique.on_bi.bi_db_open = unique_open;
1314 unique.on_bi.bi_db_close = unique_close;
1315 unique.on_bi.bi_op_add = unique_add;
1316 unique.on_bi.bi_op_modify = unique_modify;
1317 unique.on_bi.bi_op_modrdn = unique_modrdn;
1319 unique.on_bi.bi_cf_ocs = uniqueocs;
1320 rc = config_register_schema( uniquecfg, uniqueocs );
1321 if ( rc ) return rc;
1323 return(overlay_register(&unique));
1326 #if SLAPD_OVER_UNIQUE == SLAPD_MOD_DYNAMIC && defined(PIC)
1327 int init_module(int argc, char *argv[]) {
1328 return unique_initialize();
1332 #endif /* SLAPD_OVER_UNIQUE */