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