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->cr_msg, sizeof( c->cr_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] ) ) {
201 snprintf( c->cr_msg, sizeof( c->cr_msg ),
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->cr_msg, sizeof( c->cr_msg ),
224 "unique: attribute: %s: %s",
232 uri->scope = url_desc->lud_scope;
234 snprintf( c->cr_msg, sizeof( c->cr_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->cr_msg, sizeof( c->cr_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->cr_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->cr_msg, sizeof( c->cr_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->cr_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:
404 snprintf( c->cr_msg, sizeof( c->cr_msg ),
405 "cannot set legacy attrs when URIs are present" );
406 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
407 c->cr_msg, NULL, NULL );
411 if ( !dnIsSuffix ( &c->value_ndn,
412 &be->be_nsuffix[0] ) ) {
413 snprintf( c->cr_msg, sizeof( c->cr_msg ),
414 "dn is not a suffix of backend base" );
415 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
416 c->cr_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:
502 snprintf( c->cr_msg, sizeof( c->cr_msg ),
503 "cannot set legacy attrs when URIs are present" );
504 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
505 c->cr_msg, NULL, NULL );
511 && legacy->uri->attrs
512 && (c->type == UNIQUE_IGNORE) != legacy->ignore ) {
513 snprintf( c->cr_msg, sizeof( c->cr_msg ),
514 "cannot set both attrs and ignore-attrs" );
515 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
516 c->cr_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->cr_msg, sizeof( c->cr_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->cr_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:
623 snprintf( c->cr_msg, sizeof( c->cr_msg ),
624 "cannot set legacy attrs when URIs are present" );
625 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
626 c->cr_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 */
708 snprintf( c->cr_msg, sizeof( c->cr_msg ),
709 "cannot set Uri when legacy attrs are present" );
710 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
711 c->cr_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;
747 slap_overinst *on = (slap_overinst *)be->bd_info;
748 unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
750 Debug(LDAP_DEBUG_TRACE, "==> unique_db_init\n", 0, 0, 0);
752 *privatep = ch_calloc ( 1, sizeof ( unique_data ) );
763 slap_overinst *on = (slap_overinst *)be->bd_info;
764 unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
765 unique_data *private = *privatep;
767 Debug(LDAP_DEBUG_TRACE, "==> unique_db_destroy\n", 0, 0, 0);
770 unique_domain *domains = private->domains;
771 unique_domain *legacy = private->legacy;
773 unique_free_domain ( domains );
774 unique_free_domain ( legacy );
788 Debug(LDAP_DEBUG_TRACE, "unique_open: overlay initialized\n", 0, 0, 0);
795 ** Leave unique_data but wipe out config
805 slap_overinst *on = (slap_overinst *) be->bd_info;
806 unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
807 unique_data *private = *privatep;
809 Debug(LDAP_DEBUG_TRACE, "==> unique_close\n", 0, 0, 0);
812 unique_domain *domains = private->domains;
813 unique_domain *legacy = private->legacy;
815 unique_free_domain ( domains );
816 unique_free_domain ( legacy );
817 memset ( private, 0, sizeof ( unique_data ) );
826 ** if this is a REP_SEARCH, count++;
830 static int count_attr_cb(
837 /* because you never know */
838 if(!op || !rs) return(0);
840 /* Only search entries are interesting */
841 if(rs->sr_type != REP_SEARCH) return(0);
843 uc = op->o_callback->sc_private;
845 /* Ignore the current entry */
846 if ( dn_match( uc->ndn, &rs->sr_entry->e_nname )) return(0);
848 Debug(LDAP_DEBUG_TRACE, "==> count_attr_cb <%s>\n",
849 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "UNKNOWN_DN", 0, 0);
856 /* count the length of one attribute ad
857 * (and all of its values b)
858 * in the proposed filter
862 unique_domain *domain,
863 unique_domain_uri *uri,
864 AttributeDescription *ad,
872 while ( !is_at_operational( ad->ad_type ) ) {
874 for ( attr = uri->attrs; attr; attr = attr->next ) {
875 if ( ad == attr->attr ) {
879 if ( ( domain->ignore && attr )
880 || (!domain->ignore && !attr )) {
884 if ( b && b[0].bv_val ) {
885 for (i = 0; b[i].bv_val; i++ ) {
886 /* note: make room for filter escaping... */
887 ks += ( 3 * b[i].bv_len ) + ad->ad_cname.bv_len + STRLENOF( "(=)" );
889 } else if ( domain->strict ) {
890 ks += ad->ad_cname.bv_len + STRLENOF( "(=*)" ); /* (attr=*) */
900 unique_domain *domain,
901 unique_domain_uri *uri,
902 AttributeDescription *ad,
912 while ( !is_at_operational( ad->ad_type ) ) {
914 for ( attr = uri->attrs; attr; attr = attr->next ) {
915 if ( ad == attr->attr ) {
919 if ( ( domain->ignore && attr )
920 || (!domain->ignore && !attr )) {
924 if ( b && b[0].bv_val ) {
925 for ( i = 0; b[i].bv_val; i++ ) {
929 ldap_bv2escaped_filter_value_x( &b[i], &bv, 1, ctx );
930 len = snprintf( kp, ks, "(%s=%s)", ad->ad_cname.bv_val, bv.bv_val );
931 assert( len >= 0 && len < ks );
933 if ( bv.bv_val != b[i].bv_val ) {
934 ber_memfree_x( bv.bv_val, ctx );
937 } else if ( domain->strict ) {
939 len = snprintf( kp, ks, "(%s=*)", ad->ad_cname.bv_val );
940 assert( len >= 0 && len < ks );
958 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
959 SlapReply nrs = { REP_RESULT };
960 slap_callback cb = { NULL, NULL, NULL, NULL }; /* XXX */
961 unique_counter uq = { NULL, 0 };
964 Debug(LDAP_DEBUG_TRACE, "==> unique_search %s\n", key, 0, 0);
966 nop->ors_filter = str2filter_x(nop, key);
967 ber_str2bv(key, 0, 0, &nop->ors_filterstr);
969 cb.sc_response = (slap_response*)count_attr_cb;
971 nop->o_callback = &cb;
972 nop->o_tag = LDAP_REQ_SEARCH;
973 nop->ors_scope = scope;
974 nop->ors_deref = LDAP_DEREF_NEVER;
975 nop->ors_limit = NULL;
976 nop->ors_slimit = SLAP_NO_LIMIT;
977 nop->ors_tlimit = SLAP_NO_LIMIT;
978 nop->ors_attrs = slap_anlist_no_attrs;
979 nop->ors_attrsonly = 1;
981 uq.ndn = &op->o_req_ndn;
983 nop->o_req_ndn = *dn;
984 nop->o_ndn = op->o_bd->be_rootndn;
986 nop->o_bd = on->on_info->oi_origdb;
987 rc = nop->o_bd->be_search(nop, &nrs);
988 filter_free_x(nop, nop->ors_filter);
989 op->o_tmpfree( key, op->o_tmpmemctx );
991 if(rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT) {
992 op->o_bd->bd_info = (BackendInfo *) on->on_info;
993 send_ldap_error(op, rs, rc, "unique_search failed");
997 Debug(LDAP_DEBUG_TRACE, "=> unique_search found %d records\n", uq.count, 0, 0);
1000 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1001 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
1002 "some attributes not unique");
1006 return(SLAP_CB_CONTINUE);
1015 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1016 unique_data *private = (unique_data *) on->on_bi.bi_private;
1017 unique_domain *domains = private->domains;
1018 unique_domain *legacy = private->legacy;
1019 unique_domain *domain;
1020 Operation nop = *op;
1023 int rc = SLAP_CB_CONTINUE;
1025 Debug(LDAP_DEBUG_TRACE, "==> unique_add <%s>\n",
1026 op->o_req_dn.bv_val, 0, 0);
1028 for ( domain = legacy ? legacy : domains;
1030 domain = domain->next )
1032 unique_domain_uri *uri;
1035 for ( uri = domain->uri;
1042 && !dnIsSuffix( &op->o_req_ndn, uri->ndn ))
1045 if(!(a = op->ora_e->e_attrs)) {
1046 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1047 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1048 "unique_add() got null op.ora_e.e_attrs");
1053 for(; a; a = a->a_next) {
1054 ks += count_filter_len ( domain,
1061 /* skip this domain-uri if it isn't involved */
1062 if ( !ks ) continue;
1064 if ( uri->filter && uri->filter->bv_len )
1065 ks += uri->filter->bv_len + STRLENOF ("(&)");
1066 kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1068 if ( uri->filter && uri->filter->bv_len ) {
1069 len = snprintf (kp, ks, "(&%s", uri->filter->bv_val);
1070 assert( len >= 0 && len < ks );
1073 len = snprintf(kp, ks - (kp - key), "(|");
1074 assert( len >= 0 && len < ks - (kp - key) );
1077 for(a = op->ora_e->e_attrs; a; a = a->a_next)
1078 kp = build_filter(domain,
1086 len = snprintf(kp, ks - (kp - key), ")");
1087 assert( len >= 0 && len < ks - (kp - key) );
1089 if ( uri->filter && uri->filter->bv_len ) {
1090 len = snprintf(kp, ks - (kp - key), ")");
1091 assert( len >= 0 && len < ks - (kp - key) );
1095 rc = unique_search ( op,
1099 &op->o_bd->be_nsuffix[0],
1104 if ( rc != SLAP_CB_CONTINUE ) break;
1106 if ( rc != SLAP_CB_CONTINUE ) break;
1119 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1120 unique_data *private = (unique_data *) on->on_bi.bi_private;
1121 unique_domain *domains = private->domains;
1122 unique_domain *legacy = private->legacy;
1123 unique_domain *domain;
1124 Operation nop = *op;
1127 int rc = SLAP_CB_CONTINUE;
1129 Debug(LDAP_DEBUG_TRACE, "==> unique_modify <%s>\n",
1130 op->o_req_dn.bv_val, 0, 0);
1132 for ( domain = legacy ? legacy : domains;
1134 domain = domain->next )
1136 unique_domain_uri *uri;
1139 for ( uri = domain->uri;
1146 && !dnIsSuffix( &op->o_req_ndn, uri->ndn ))
1149 if ( !(m = op->orm_modlist) ) {
1150 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1151 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1152 "unique_modify() got null op.orm_modlist");
1157 for ( ; m; m = m->sml_next)
1158 if ( (m->sml_op & LDAP_MOD_OP)
1159 != LDAP_MOD_DELETE )
1160 ks += count_filter_len
1166 /* skip this domain-uri if it isn't involved */
1167 if ( !ks ) continue;
1169 if ( uri->filter && uri->filter->bv_len )
1170 ks += uri->filter->bv_len + STRLENOF ("(&)");
1171 kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1173 if ( uri->filter && uri->filter->bv_len ) {
1174 len = snprintf(kp, ks, "(&%s", uri->filter->bv_val);
1175 assert( len >= 0 && len < ks );
1178 len = snprintf(kp, ks - (kp - key), "(|");
1179 assert( len >= 0 && len < ks - (kp - key) );
1182 for(m = op->orm_modlist; m; m = m->sml_next)
1183 if ( (m->sml_op & LDAP_MOD_OP)
1184 != LDAP_MOD_DELETE )
1185 kp = build_filter ( domain,
1193 len = snprintf(kp, ks - (kp - key), ")");
1194 assert( len >= 0 && len < ks - (kp - key) );
1196 if ( uri->filter && uri->filter->bv_len ) {
1197 len = snprintf (kp, ks - (kp - key), ")");
1198 assert( len >= 0 && len < ks - (kp - key) );
1202 rc = unique_search ( op,
1206 &op->o_bd->be_nsuffix[0],
1211 if ( rc != SLAP_CB_CONTINUE ) break;
1213 if ( rc != SLAP_CB_CONTINUE ) break;
1226 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1227 unique_data *private = (unique_data *) on->on_bi.bi_private;
1228 unique_domain *domains = private->domains;
1229 unique_domain *legacy = private->legacy;
1230 unique_domain *domain;
1231 Operation nop = *op;
1234 struct berval bv[2];
1235 int rc = SLAP_CB_CONTINUE;
1237 Debug(LDAP_DEBUG_TRACE, "==> unique_modrdn <%s> <%s>\n",
1238 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
1240 for ( domain = legacy ? legacy : domains;
1242 domain = domain->next )
1244 unique_domain_uri *uri;
1247 for ( uri = domain->uri;
1254 && !dnIsSuffix( &op->o_req_ndn, uri->ndn )
1255 && (!op->orr_nnewSup
1256 || !dnIsSuffix( op->orr_nnewSup, uri->ndn )))
1259 if ( ldap_bv2rdn_x ( &op->oq_modrdn.rs_newrdn,
1261 (char **)&rs->sr_text,
1262 LDAP_DN_FORMAT_LDAP,
1263 op->o_tmpmemctx ) ) {
1264 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1265 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1266 "unknown type(s) used in RDN");
1271 rc = SLAP_CB_CONTINUE;
1272 for ( i=0; newrdn[i]; i++) {
1273 AttributeDescription *ad = NULL;
1274 if ( slap_bv2ad( &newrdn[i]->la_attr, &ad, &rs->sr_text )) {
1275 ldap_rdnfree_x( newrdn, op->o_tmpmemctx );
1276 rs->sr_err = LDAP_INVALID_SYNTAX;
1277 send_ldap_result( op, rs );
1281 newrdn[i]->la_private = ad;
1283 if ( rc != SLAP_CB_CONTINUE ) break;
1285 bv[1].bv_val = NULL;
1288 for ( i=0; newrdn[i]; i++ ) {
1289 bv[0] = newrdn[i]->la_value;
1290 ks += count_filter_len ( domain,
1292 newrdn[i]->la_private,
1296 /* skip this domain if it isn't involved */
1297 if ( !ks ) continue;
1299 if ( uri->filter && uri->filter->bv_len )
1300 ks += uri->filter->bv_len + STRLENOF ("(&)");
1301 kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1303 if ( uri->filter && uri->filter->bv_len ) {
1304 len = snprintf(kp, ks, "(&%s", uri->filter->bv_val);
1305 assert( len >= 0 && len < ks );
1308 len = snprintf(kp, ks - (kp - key), "(|");
1309 assert( len >= 0 && len < ks - (kp - key) );
1312 for ( i=0; newrdn[i]; i++) {
1313 bv[0] = newrdn[i]->la_value;
1314 kp = build_filter ( domain,
1316 newrdn[i]->la_private,
1323 len = snprintf(kp, ks - (kp - key), ")");
1324 assert( len >= 0 && len < ks - (kp - key) );
1326 if ( uri->filter && uri->filter->bv_len ) {
1327 len = snprintf (kp, ks - (kp - key), ")");
1328 assert( len >= 0 && len < ks - (kp - key) );
1332 rc = unique_search ( op,
1336 &op->o_bd->be_nsuffix[0],
1341 if ( rc != SLAP_CB_CONTINUE ) break;
1343 if ( rc != SLAP_CB_CONTINUE ) break;
1350 ** init_module is last so the symbols resolve "for free" --
1351 ** it expects to be called automagically during dynamic module initialization
1359 /* statically declared just after the #includes at top */
1360 memset (&unique, 0, sizeof(unique));
1362 unique.on_bi.bi_type = "unique";
1363 unique.on_bi.bi_db_init = unique_db_init;
1364 unique.on_bi.bi_db_destroy = unique_db_destroy;
1365 unique.on_bi.bi_db_open = unique_open;
1366 unique.on_bi.bi_db_close = unique_close;
1367 unique.on_bi.bi_op_add = unique_add;
1368 unique.on_bi.bi_op_modify = unique_modify;
1369 unique.on_bi.bi_op_modrdn = unique_modrdn;
1371 unique.on_bi.bi_cf_ocs = uniqueocs;
1372 rc = config_register_schema( uniquecfg, uniqueocs );
1373 if ( rc ) return rc;
1375 return(overlay_register(&unique));
1378 #if SLAPD_OVER_UNIQUE == SLAPD_MOD_DYNAMIC && defined(PIC)
1379 int init_module(int argc, char *argv[]) {
1380 return unique_initialize();
1384 #endif /* SLAPD_OVER_UNIQUE */