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