1 /* unique.c - attribute uniqueness module */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2004-2008 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;
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 ch_free ( uri->dn.bv_val );
142 ch_free ( uri->ndn.bv_val );
143 ch_free ( uri->filter.bv_val );
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 ch_free ( domain->domain_spec.bv_val );
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, 0, &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) {
241 Filter *f = str2filter( url_desc->lud_filter );
244 snprintf( c->cr_msg, sizeof( c->cr_msg ),
245 "unique: bad filter");
249 /* make sure the strfilter is in normal form (ITS#5581) */
250 filter2bv( f, &uri->filter );
252 ptr = strstr( uri->filter.bv_val, "(?=" /*)*/ );
253 if ( ptr != NULL && ptr <= ( uri->filter.bv_val - STRLENOF( "(?=" /*)*/ ) + uri->filter.bv_len ) )
255 snprintf( c->cr_msg, sizeof( c->cr_msg ),
256 "unique: bad filter");
265 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
266 "%s: %s\n", c->log, c->cr_msg, 0 );
267 unique_free_domain_uri ( uri );
274 unique_new_domain_uri_basic ( unique_domain_uri **urip,
277 LDAPURLDesc *url_desc = NULL;
280 rc = ldap_url_parse ( UNIQUE_DEFAULT_URI, &url_desc );
282 rc = unique_new_domain_uri ( urip, url_desc, c );
283 ldap_free_urldesc ( url_desc );
287 /* if *domain is non-null, it's pushed down the stack.
288 * note that the entire stack is freed if there is an error,
289 * so build added domains in a separate stack before adding them
291 * domain_specs look like
293 * [strict ][ignore ]uri[[ uri]...]
294 * e.g. "ldap:///ou=foo,o=bar?uid?sub ldap:///ou=baz,o=bar?uid?sub"
295 * "strict ldap:///ou=accounts,o=bar?uid,uidNumber?one"
298 * so finally strictness is per-domain
299 * but so is ignore-state, and that would be better as a per-url thing
302 unique_new_domain ( unique_domain **domainp,
307 int rc = LDAP_SUCCESS;
309 unique_domain * domain;
310 LDAPURLDesc *url_desc, *url_descs = NULL;
312 Debug(LDAP_DEBUG_TRACE, "==> unique_new_domain <%s>\n",
315 domain = ch_calloc ( 1, sizeof (unique_domain) );
316 ber_str2bv( domain_spec, 0, 1, &domain->domain_spec );
318 uri_start = domain_spec;
319 if ( strncasecmp ( uri_start, "ignore ",
320 STRLENOF( "ignore " ) ) == 0 ) {
322 uri_start += STRLENOF( "ignore " );
324 if ( strncasecmp ( uri_start, "strict ",
325 STRLENOF( "strict " ) ) == 0 ) {
327 uri_start += STRLENOF( "strict " );
329 && strncasecmp ( uri_start, "ignore ",
330 STRLENOF( "ignore " ) ) == 0 ) {
332 uri_start += STRLENOF( "ignore " );
335 rc = ldap_url_parselist_ext ( &url_descs, uri_start, " ", 0 );
337 snprintf( c->cr_msg, sizeof( c->cr_msg ),
338 "<%s> invalid ldap urilist",
344 for ( url_desc = url_descs;
346 url_desc = url_descs->lud_next ) {
347 rc = unique_new_domain_uri ( &domain->uri,
358 if ( url_descs ) ldap_free_urldesc ( url_descs );
359 domain->next = *domainp;
362 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
363 "%s: %s\n", c->log, c->cr_msg, 0 );
364 unique_free_domain ( domain );
371 unique_cf_base( ConfigArgs *c )
373 BackendDB *be = (BackendDB *)c->be;
374 slap_overinst *on = (slap_overinst *)c->bi;
375 unique_data *private = (unique_data *) on->on_bi.bi_private;
376 unique_domain *domains = private->domains;
377 unique_domain *legacy = private->legacy;
378 int rc = ARG_BAD_CONF;
381 case SLAP_CONFIG_EMIT:
383 if ( legacy && legacy->uri && legacy->uri->dn.bv_val ) {
384 rc = value_add_one ( &c->rvalue_vals,
387 rc = value_add_one ( &c->rvalue_nvals,
392 case LDAP_MOD_DELETE:
393 assert ( legacy && legacy->uri && legacy->uri->dn.bv_val );
395 ch_free ( legacy->uri->dn.bv_val );
396 ch_free ( legacy->uri->ndn.bv_val );
397 BER_BVZERO( &legacy->uri->dn );
398 BER_BVZERO( &legacy->uri->ndn );
399 if ( !legacy->uri->attrs ) {
400 unique_free_domain_uri ( legacy->uri );
403 if ( !legacy->uri && !private->legacy_strict_set ) {
404 unique_free_domain ( legacy );
405 private->legacy = legacy = NULL;
409 case SLAP_CONFIG_ADD:
411 snprintf( c->cr_msg, sizeof( c->cr_msg ),
412 "cannot set legacy attrs when URIs are present" );
413 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
414 c->cr_msg, NULL, NULL );
418 if ( be->be_nsuffix == NULL ) {
419 snprintf( c->cr_msg, sizeof( c->cr_msg ),
420 "suffix must be set" );
421 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
422 c->cr_msg, NULL, NULL );
426 if ( !dnIsSuffix ( &c->value_ndn,
427 &be->be_nsuffix[0] ) ) {
428 snprintf( c->cr_msg, sizeof( c->cr_msg ),
429 "dn is not a suffix of backend base" );
430 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
431 c->cr_msg, NULL, NULL );
436 unique_new_domain ( &private->legacy,
439 legacy = private->legacy;
442 unique_new_domain_uri_basic ( &legacy->uri, c );
443 ch_free ( legacy->uri->dn.bv_val );
444 ch_free ( legacy->uri->ndn.bv_val );
445 legacy->uri->dn = c->value_dn;
446 legacy->uri->ndn = c->value_ndn;
457 unique_cf_attrs( ConfigArgs *c )
459 slap_overinst *on = (slap_overinst *)c->bi;
460 unique_data *private = (unique_data *) on->on_bi.bi_private;
461 unique_domain *domains = private->domains;
462 unique_domain *legacy = private->legacy;
463 unique_attrs *new_attrs = NULL;
464 unique_attrs *attr, *next_attr, *reverse_attrs;
465 unique_attrs **attrp;
466 int rc = ARG_BAD_CONF;
470 case SLAP_CONFIG_EMIT:
472 && (c->type == UNIQUE_IGNORE) == legacy->ignore
474 for ( attr = legacy->uri->attrs;
477 value_add_one( &c->rvalue_vals,
478 &attr->attr->ad_cname );
481 case LDAP_MOD_DELETE:
483 && (c->type == UNIQUE_IGNORE) == legacy->ignore
485 && legacy->uri->attrs) {
486 if ( c->valx < 0 ) { /* delete all */
487 for ( attr = legacy->uri->attrs;
490 next_attr = attr->next;
493 legacy->uri->attrs = NULL;
494 } else { /* delete by index */
495 attrp = &legacy->uri->attrs;
496 for ( i=0; i < c->valx; ++i )
497 attrp = &(*attrp)->next;
502 if ( !legacy->uri->attrs
503 && !legacy->uri->dn.bv_val ) {
504 unique_free_domain_uri ( legacy->uri );
507 if ( !legacy->uri && !private->legacy_strict_set ) {
508 unique_free_domain ( legacy );
509 private->legacy = legacy = NULL;
515 case SLAP_CONFIG_ADD:
517 snprintf( c->cr_msg, sizeof( c->cr_msg ),
518 "cannot set legacy attrs when URIs are present" );
519 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
520 c->cr_msg, NULL, NULL );
526 && legacy->uri->attrs
527 && (c->type == UNIQUE_IGNORE) != legacy->ignore ) {
528 snprintf( c->cr_msg, sizeof( c->cr_msg ),
529 "cannot set both attrs and ignore-attrs" );
530 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
531 c->cr_msg, NULL, NULL );
536 unique_new_domain ( &private->legacy,
539 legacy = private->legacy;
542 unique_new_domain_uri_basic ( &legacy->uri, c );
544 for ( i=1; c->argv[i]; ++i ) {
545 AttributeDescription * ad = NULL;
547 if ( slap_str2ad ( c->argv[i], &ad, &text )
550 attr = ch_calloc ( 1,
551 sizeof ( unique_attrs ) );
553 attr->next = new_attrs;
556 snprintf( c->cr_msg, sizeof( c->cr_msg ),
557 "unique: attribute: %s: %s",
559 for ( attr = new_attrs;
562 next_attr = attr->next;
571 /* (nconc legacy->uri->attrs (nreverse new_attrs)) */
572 reverse_attrs = NULL;
573 for ( attr = new_attrs;
576 next_attr = attr->next;
577 attr->next = reverse_attrs;
578 reverse_attrs = attr;
580 for ( attrp = &legacy->uri->attrs;
582 attrp = &(*attrp)->next ) ;
583 *attrp = reverse_attrs;
585 legacy->ignore = ( c->type == UNIQUE_IGNORE );
592 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
593 "%s: %s\n", c->log, c->cr_msg, 0 );
599 unique_cf_strict( ConfigArgs *c )
601 slap_overinst *on = (slap_overinst *)c->bi;
602 unique_data *private = (unique_data *) on->on_bi.bi_private;
603 unique_domain *domains = private->domains;
604 unique_domain *legacy = private->legacy;
605 int rc = ARG_BAD_CONF;
608 case SLAP_CONFIG_EMIT:
609 /* We process the boolean manually instead of using
610 * ARG_ON_OFF so that we can three-state it;
611 * olcUniqueStrict is either TRUE, FALSE, or missing,
612 * and missing is necessary to add olcUniqueURIs...
614 if ( private->legacy_strict_set ) {
616 bv.bv_val = legacy->strict ? "TRUE" : "FALSE";
617 bv.bv_len = legacy->strict ?
620 value_add_one ( &c->rvalue_vals, &bv );
624 case LDAP_MOD_DELETE:
627 if ( ! legacy->uri ) {
628 unique_free_domain ( legacy );
629 private->legacy = NULL;
632 private->legacy_strict_set = 0;
636 case SLAP_CONFIG_ADD:
638 snprintf( c->cr_msg, sizeof( c->cr_msg ),
639 "cannot set legacy attrs when URIs are present" );
640 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
641 c->cr_msg, NULL, NULL );
646 unique_new_domain ( &private->legacy,
649 legacy = private->legacy;
651 /* ... not using ARG_ON_OFF makes this necessary too */
652 assert ( c->argc == 2 );
653 legacy->strict = (strcasecmp ( c->argv[1], "TRUE" ) == 0);
654 private->legacy_strict_set = 1;
665 unique_cf_uri( ConfigArgs *c )
667 slap_overinst *on = (slap_overinst *)c->bi;
668 unique_data *private = (unique_data *) on->on_bi.bi_private;
669 unique_domain *domains = private->domains;
670 unique_domain *legacy = private->legacy;
671 unique_domain *domain = NULL, **domainp = NULL;
672 int rc = ARG_BAD_CONF;
676 case SLAP_CONFIG_EMIT:
677 for ( domain = domains;
679 domain = domain->next ) {
680 rc = value_add_one ( &c->rvalue_vals,
681 &domain->domain_spec );
685 case LDAP_MOD_DELETE:
686 if ( c->valx < 0 ) { /* delete them all! */
687 unique_free_domain ( domains );
688 private->domains = NULL;
689 } else { /* delete just one */
690 domainp = &private->domains;
691 for ( i=0; i < c->valx && *domainp; ++i )
692 domainp = &(*domainp)->next;
694 /* If *domainp is null, we walked off the end
695 * of the list. This happens when back-config
696 * and the overlay are out-of-sync, like when
697 * rejecting changes before ITS#4752 gets
700 * This should never happen, but will appear
701 * if you backport this version of
702 * slapo-unique without the config-undo fixes
704 * test024 Will hit this case in such a
707 assert (*domainp != NULL);
710 *domainp = domain->next;
712 unique_free_domain ( domain );
717 case SLAP_CONFIG_ADD: /* fallthrough */
720 snprintf( c->cr_msg, sizeof( c->cr_msg ),
721 "cannot set Uri when legacy attrs are present" );
722 Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
723 c->cr_msg, NULL, NULL );
728 if ( c->line ) rc = unique_new_domain ( &domain, c->line, c );
729 else rc = unique_new_domain ( &domain, c->argv[1], c );
731 assert ( domain->next == NULL );
732 for ( domainp = &private->domains;
734 domainp = &(*domainp)->next ) ;
747 ** allocate new unique_data;
748 ** initialize, copy basedn;
749 ** store in on_bi.bi_private;
759 slap_overinst *on = (slap_overinst *)be->bd_info;
760 unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
762 Debug(LDAP_DEBUG_TRACE, "==> unique_db_init\n", 0, 0, 0);
764 *privatep = ch_calloc ( 1, sizeof ( unique_data ) );
775 slap_overinst *on = (slap_overinst *)be->bd_info;
776 unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
777 unique_data *private = *privatep;
779 Debug(LDAP_DEBUG_TRACE, "==> unique_db_destroy\n", 0, 0, 0);
782 unique_domain *domains = private->domains;
783 unique_domain *legacy = private->legacy;
785 unique_free_domain ( domains );
786 unique_free_domain ( legacy );
800 Debug(LDAP_DEBUG_TRACE, "unique_open: overlay initialized\n", 0, 0, 0);
807 ** Leave unique_data but wipe out config
817 slap_overinst *on = (slap_overinst *) be->bd_info;
818 unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
819 unique_data *private = *privatep;
821 Debug(LDAP_DEBUG_TRACE, "==> unique_close\n", 0, 0, 0);
824 unique_domain *domains = private->domains;
825 unique_domain *legacy = private->legacy;
827 unique_free_domain ( domains );
828 unique_free_domain ( legacy );
829 memset ( private, 0, sizeof ( unique_data ) );
838 ** if this is a REP_SEARCH, count++;
842 static int count_attr_cb(
849 /* because you never know */
850 if(!op || !rs) return(0);
852 /* Only search entries are interesting */
853 if(rs->sr_type != REP_SEARCH) return(0);
855 uc = op->o_callback->sc_private;
857 /* Ignore the current entry */
858 if ( dn_match( uc->ndn, &rs->sr_entry->e_nname )) return(0);
860 Debug(LDAP_DEBUG_TRACE, "==> count_attr_cb <%s>\n",
861 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "UNKNOWN_DN", 0, 0);
868 /* count the length of one attribute ad
869 * (and all of its values b)
870 * in the proposed filter
874 unique_domain *domain,
875 unique_domain_uri *uri,
876 AttributeDescription *ad,
884 while ( !is_at_operational( ad->ad_type ) ) {
886 for ( attr = uri->attrs; attr; attr = attr->next ) {
887 if ( ad == attr->attr ) {
891 if ( ( domain->ignore && attr )
892 || (!domain->ignore && !attr )) {
896 if ( b && b[0].bv_val ) {
897 for (i = 0; b[i].bv_val; i++ ) {
898 /* note: make room for filter escaping... */
899 ks += ( 3 * b[i].bv_len ) + ad->ad_cname.bv_len + STRLENOF( "(=)" );
901 } else if ( domain->strict ) {
902 ks += ad->ad_cname.bv_len + STRLENOF( "(=*)" ); /* (attr=*) */
912 unique_domain *domain,
913 unique_domain_uri *uri,
914 AttributeDescription *ad,
924 while ( !is_at_operational( ad->ad_type ) ) {
926 for ( attr = uri->attrs; attr; attr = attr->next ) {
927 if ( ad == attr->attr ) {
931 if ( ( domain->ignore && attr )
932 || (!domain->ignore && !attr )) {
936 if ( b && b[0].bv_val ) {
937 for ( i = 0; b[i].bv_val; i++ ) {
941 ldap_bv2escaped_filter_value_x( &b[i], &bv, 1, ctx );
942 len = snprintf( kp, ks, "(%s=%s)", ad->ad_cname.bv_val, bv.bv_val );
943 assert( len >= 0 && len < ks );
945 if ( bv.bv_val != b[i].bv_val ) {
946 ber_memfree_x( bv.bv_val, ctx );
949 } else if ( domain->strict ) {
951 len = snprintf( kp, ks, "(%s=*)", ad->ad_cname.bv_val );
952 assert( len >= 0 && len < ks );
970 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
971 SlapReply nrs = { REP_RESULT };
972 slap_callback cb = { NULL, NULL, NULL, NULL }; /* XXX */
973 unique_counter uq = { NULL, 0 };
976 Debug(LDAP_DEBUG_TRACE, "==> unique_search %s\n", key, 0, 0);
978 nop->ors_filter = str2filter_x(nop, key->bv_val);
979 if(nop->ors_filter == NULL) {
980 op->o_bd->bd_info = (BackendInfo *) on->on_info;
981 send_ldap_error(op, rs, LDAP_OTHER,
982 "unique_search invalid filter");
986 nop->ors_filterstr = *key;
988 cb.sc_response = (slap_response*)count_attr_cb;
990 nop->o_callback = &cb;
991 nop->o_tag = LDAP_REQ_SEARCH;
992 nop->ors_scope = scope;
993 nop->ors_deref = LDAP_DEREF_NEVER;
994 nop->ors_limit = NULL;
995 nop->ors_slimit = SLAP_NO_LIMIT;
996 nop->ors_tlimit = SLAP_NO_LIMIT;
997 nop->ors_attrs = slap_anlist_no_attrs;
998 nop->ors_attrsonly = 1;
1000 uq.ndn = &op->o_req_ndn;
1002 nop->o_req_ndn = *dn;
1003 nop->o_ndn = op->o_bd->be_rootndn;
1005 nop->o_bd = on->on_info->oi_origdb;
1006 rc = nop->o_bd->be_search(nop, &nrs);
1007 filter_free_x(nop, nop->ors_filter);
1008 op->o_tmpfree( key->bv_val, op->o_tmpmemctx );
1010 if(rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT) {
1011 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1012 send_ldap_error(op, rs, rc, "unique_search failed");
1016 Debug(LDAP_DEBUG_TRACE, "=> unique_search found %d records\n", uq.count, 0, 0);
1019 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1020 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
1021 "some attributes not unique");
1025 return(SLAP_CB_CONTINUE);
1034 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1035 unique_data *private = (unique_data *) on->on_bi.bi_private;
1036 unique_domain *domains = private->domains;
1037 unique_domain *legacy = private->legacy;
1038 unique_domain *domain;
1039 Operation nop = *op;
1042 struct berval bvkey;
1043 int rc = SLAP_CB_CONTINUE;
1045 Debug(LDAP_DEBUG_TRACE, "==> unique_add <%s>\n",
1046 op->o_req_dn.bv_val, 0, 0);
1048 for ( domain = legacy ? legacy : domains;
1050 domain = domain->next )
1052 unique_domain_uri *uri;
1053 int ks = STRLENOF("(|)");
1055 for ( uri = domain->uri;
1061 if ( uri->ndn.bv_val
1062 && !dnIsSuffix( &op->o_req_ndn, &uri->ndn ))
1065 if(!(a = op->ora_e->e_attrs)) {
1066 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1067 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1068 "unique_add() got null op.ora_e.e_attrs");
1073 for(; a; a = a->a_next) {
1074 ks += count_filter_len ( domain,
1081 /* skip this domain-uri if it isn't involved */
1082 if ( !ks ) continue;
1084 /* terminating NUL */
1087 if ( uri->filter.bv_val && uri->filter.bv_len )
1088 ks += uri->filter.bv_len + STRLENOF ("(&)");
1089 kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1091 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1092 len = snprintf (kp, ks, "(&%s", uri->filter.bv_val);
1093 assert( len >= 0 && len < ks );
1096 len = snprintf(kp, ks - (kp - key), "(|");
1097 assert( len >= 0 && len < ks - (kp - key) );
1100 for(a = op->ora_e->e_attrs; a; a = a->a_next)
1101 kp = build_filter(domain,
1109 len = snprintf(kp, ks - (kp - key), ")");
1110 assert( len >= 0 && len < ks - (kp - key) );
1112 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1113 len = snprintf(kp, ks - (kp - key), ")");
1114 assert( len >= 0 && len < ks - (kp - key) );
1118 bvkey.bv_len = kp - key;
1120 rc = unique_search ( op,
1124 &op->o_bd->be_nsuffix[0],
1129 if ( rc != SLAP_CB_CONTINUE ) break;
1131 if ( rc != SLAP_CB_CONTINUE ) break;
1144 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1145 unique_data *private = (unique_data *) on->on_bi.bi_private;
1146 unique_domain *domains = private->domains;
1147 unique_domain *legacy = private->legacy;
1148 unique_domain *domain;
1149 Operation nop = *op;
1152 struct berval bvkey;
1153 int rc = SLAP_CB_CONTINUE;
1155 Debug(LDAP_DEBUG_TRACE, "==> unique_modify <%s>\n",
1156 op->o_req_dn.bv_val, 0, 0);
1158 for ( domain = legacy ? legacy : domains;
1160 domain = domain->next )
1162 unique_domain_uri *uri;
1163 int ks = STRLENOF("(|)");
1165 for ( uri = domain->uri;
1171 if ( uri->ndn.bv_val
1172 && !dnIsSuffix( &op->o_req_ndn, &uri->ndn ))
1175 if ( !(m = op->orm_modlist) ) {
1176 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1177 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1178 "unique_modify() got null op.orm_modlist");
1183 for ( ; m; m = m->sml_next)
1184 if ( (m->sml_op & LDAP_MOD_OP)
1185 != LDAP_MOD_DELETE )
1186 ks += count_filter_len
1192 /* skip this domain-uri if it isn't involved */
1193 if ( !ks ) continue;
1195 /* terminating NUL */
1198 if ( uri->filter.bv_val && uri->filter.bv_len )
1199 ks += uri->filter.bv_len + STRLENOF ("(&)");
1200 kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1202 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1203 len = snprintf(kp, ks, "(&%s", uri->filter.bv_val);
1204 assert( len >= 0 && len < ks );
1207 len = snprintf(kp, ks - (kp - key), "(|");
1208 assert( len >= 0 && len < ks - (kp - key) );
1211 for(m = op->orm_modlist; m; m = m->sml_next)
1212 if ( (m->sml_op & LDAP_MOD_OP)
1213 != LDAP_MOD_DELETE )
1214 kp = build_filter ( domain,
1222 len = snprintf(kp, ks - (kp - key), ")");
1223 assert( len >= 0 && len < ks - (kp - key) );
1225 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1226 len = snprintf (kp, ks - (kp - key), ")");
1227 assert( len >= 0 && len < ks - (kp - key) );
1231 bvkey.bv_len = kp - key;
1233 rc = unique_search ( op,
1237 &op->o_bd->be_nsuffix[0],
1242 if ( rc != SLAP_CB_CONTINUE ) break;
1244 if ( rc != SLAP_CB_CONTINUE ) break;
1257 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1258 unique_data *private = (unique_data *) on->on_bi.bi_private;
1259 unique_domain *domains = private->domains;
1260 unique_domain *legacy = private->legacy;
1261 unique_domain *domain;
1262 Operation nop = *op;
1264 struct berval bvkey;
1266 struct berval bv[2];
1267 int rc = SLAP_CB_CONTINUE;
1269 Debug(LDAP_DEBUG_TRACE, "==> unique_modrdn <%s> <%s>\n",
1270 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
1272 for ( domain = legacy ? legacy : domains;
1274 domain = domain->next )
1276 unique_domain_uri *uri;
1277 int ks = STRLENOF("(|)");
1279 for ( uri = domain->uri;
1285 if ( uri->ndn.bv_val
1286 && !dnIsSuffix( &op->o_req_ndn, &uri->ndn )
1287 && (!op->orr_nnewSup
1288 || !dnIsSuffix( op->orr_nnewSup, &uri->ndn )))
1291 if ( ldap_bv2rdn_x ( &op->oq_modrdn.rs_newrdn,
1293 (char **)&rs->sr_text,
1294 LDAP_DN_FORMAT_LDAP,
1295 op->o_tmpmemctx ) ) {
1296 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1297 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1298 "unknown type(s) used in RDN");
1303 rc = SLAP_CB_CONTINUE;
1304 for ( i=0; newrdn[i]; i++) {
1305 AttributeDescription *ad = NULL;
1306 if ( slap_bv2ad( &newrdn[i]->la_attr, &ad, &rs->sr_text )) {
1307 ldap_rdnfree_x( newrdn, op->o_tmpmemctx );
1308 rs->sr_err = LDAP_INVALID_SYNTAX;
1309 send_ldap_result( op, rs );
1313 newrdn[i]->la_private = ad;
1315 if ( rc != SLAP_CB_CONTINUE ) break;
1317 bv[1].bv_val = NULL;
1320 for ( i=0; newrdn[i]; i++ ) {
1321 bv[0] = newrdn[i]->la_value;
1322 ks += count_filter_len ( domain,
1324 newrdn[i]->la_private,
1328 /* skip this domain if it isn't involved */
1329 if ( !ks ) continue;
1331 /* terminating NUL */
1334 if ( uri->filter.bv_val && uri->filter.bv_len )
1335 ks += uri->filter.bv_len + STRLENOF ("(&)");
1336 kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1338 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1339 len = snprintf(kp, ks, "(&%s", uri->filter.bv_val);
1340 assert( len >= 0 && len < ks );
1343 len = snprintf(kp, ks - (kp - key), "(|");
1344 assert( len >= 0 && len < ks - (kp - key) );
1347 for ( i=0; newrdn[i]; i++) {
1348 bv[0] = newrdn[i]->la_value;
1349 kp = build_filter ( domain,
1351 newrdn[i]->la_private,
1358 len = snprintf(kp, ks - (kp - key), ")");
1359 assert( len >= 0 && len < ks - (kp - key) );
1361 if ( uri->filter.bv_val && uri->filter.bv_len ) {
1362 len = snprintf (kp, ks - (kp - key), ")");
1363 assert( len >= 0 && len < ks - (kp - key) );
1367 bvkey.bv_len = kp - key;
1369 rc = unique_search ( op,
1373 &op->o_bd->be_nsuffix[0],
1378 if ( rc != SLAP_CB_CONTINUE ) break;
1380 if ( rc != SLAP_CB_CONTINUE ) break;
1387 ** init_module is last so the symbols resolve "for free" --
1388 ** it expects to be called automagically during dynamic module initialization
1396 /* statically declared just after the #includes at top */
1397 memset (&unique, 0, sizeof(unique));
1399 unique.on_bi.bi_type = "unique";
1400 unique.on_bi.bi_db_init = unique_db_init;
1401 unique.on_bi.bi_db_destroy = unique_db_destroy;
1402 unique.on_bi.bi_db_open = unique_open;
1403 unique.on_bi.bi_db_close = unique_close;
1404 unique.on_bi.bi_op_add = unique_add;
1405 unique.on_bi.bi_op_modify = unique_modify;
1406 unique.on_bi.bi_op_modrdn = unique_modrdn;
1408 unique.on_bi.bi_cf_ocs = uniqueocs;
1409 rc = config_register_schema( uniquecfg, uniqueocs );
1410 if ( rc ) return rc;
1412 return(overlay_register(&unique));
1415 #if SLAPD_OVER_UNIQUE == SLAPD_MOD_DYNAMIC && defined(PIC)
1416 int init_module(int argc, char *argv[]) {
1417 return unique_initialize();
1421 #endif /* SLAPD_OVER_UNIQUE */