X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=servers%2Fslapd%2Fslapi%2Fslapi_utils.c;h=d3fa90ab745c84f0e1290c5fece7aa6f3699eee8;hb=71827d002de71fe7ff6a88f0ccfa9b71f8240935;hp=2af2e49f1dabfc2784fdc2161d6a748f0f9d5acc;hpb=053672c6c0b4a2ceb45f90b177c7d06e7b66e40d;p=openldap diff --git a/servers/slapd/slapi/slapi_utils.c b/servers/slapd/slapi/slapi_utils.c index 2af2e49f1d..d3fa90ab74 100644 --- a/servers/slapd/slapi/slapi_utils.c +++ b/servers/slapd/slapi/slapi_utils.c @@ -1,42 +1,39 @@ -/* - * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved. - * COPYING RESTRICTIONS APPLY, see COPYRIGHT file - */ -/* - * (C) Copyright IBM Corp. 1997,2002 - * Redistribution and use in source and binary forms are permitted - * provided that this notice is preserved and that due credit is - * given to IBM Corporation. This software is provided ``as is'' - * without express or implied warranty. +/* $OpenLDAP$ */ +/* This work is part of OpenLDAP Software . + * + * Copyright 2002-2006 The OpenLDAP Foundation. + * Portions Copyright 1997,2002-2003 IBM Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted only as authorized by the OpenLDAP + * Public License. + * + * A copy of this license is available in the file LICENSE in the + * top-level directory of the distribution or, alternatively, at + * . */ -/* - * Portions (C) Copyright PADL Software Pty Ltd. - * Redistribution and use in source and binary forms are permitted - * provided that this notice is preserved and that due credit is - * given to PADL Software Pty Ltd. This software is provided ``as is'' - * without express or implied warranty. +/* ACKNOWLEDGEMENTS: + * This work was initially developed by IBM Corporation for use in + * IBM products and subsequently ported to OpenLDAP Software by + * Steve Omrani. Additional significant contributors include: + * Luke Howard */ #include "portable.h" -#include "slapi_common.h" #include +#include +#include +#include #include #include -#include -#include -#include -#include - -struct berval *ns_get_supported_extop( int ); - -#ifdef _SPARC -#include -#endif #include +#ifdef LDAP_SLAPI + /* * server start time (should we use a struct timeval also in slapd? */ @@ -44,6 +41,29 @@ static struct timeval base_time; ldap_pvt_thread_mutex_t slapi_hn_mutex; ldap_pvt_thread_mutex_t slapi_time_mutex; +struct slapi_mutex { + ldap_pvt_thread_mutex_t mutex; +}; + +struct slapi_condvar { + ldap_pvt_thread_cond_t cond; + ldap_pvt_thread_mutex_t mutex; +}; + +static int checkBVString(const struct berval *bv) +{ + int i; + + for ( i = 0; i < bv->bv_len; i++ ) { + if ( bv->bv_val[i] == '\0' ) + return 0; + } + if ( bv->bv_val[i] != '\0' ) + return 0; + + return 1; +} + /* * This function converts an array of pointers to berval objects to * an array of berval objects. @@ -75,6 +95,8 @@ bvptr2obj( tmpberval[i].bv_val = bvptr[i]->bv_val; tmpberval[i].bv_len = bvptr[i]->bv_len; } + tmpberval[i].bv_val = NULL; + tmpberval[i].bv_len = 0; if ( rc == LDAP_SUCCESS ) { *bvobj = tmpberval; @@ -86,22 +108,9 @@ bvptr2obj( Slapi_Entry * slapi_str2entry( char *s, - int check_dup ) + int flags ) { -#if defined(LDAP_SLAPI) - Slapi_Entry *e = NULL; - char *pTmpS; - - pTmpS = slapi_ch_strdup( s ); - if ( pTmpS != NULL ) { - e = str2entry( pTmpS ); - slapi_ch_free( (void **)&pTmpS ); - } - - return e; -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ + return str2entry( s ); } char * @@ -109,27 +118,49 @@ slapi_entry2str( Slapi_Entry *e, int *len ) { -#if defined(LDAP_SLAPI) - char *ret; + char *ret = NULL; + char *s; ldap_pvt_thread_mutex_lock( &entry2str_mutex ); - ret = entry2str( e, len ); + s = entry2str( e, len ); + if ( s != NULL ) + ret = slapi_ch_strdup( s ); ldap_pvt_thread_mutex_unlock( &entry2str_mutex ); return ret; -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ } char * slapi_entry_get_dn( Slapi_Entry *e ) { -#if defined(LDAP_SLAPI) return e->e_name.bv_val; -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ +} + +int +slapi_x_entry_get_id( Slapi_Entry *e ) +{ + return e->e_id; +} + +static int +slapi_int_dn_pretty( struct berval *in, struct berval *out ) +{ + Syntax *syntax = slap_schema.si_syn_distinguishedName; + + assert( syntax != NULL ); + + return (syntax->ssyn_pretty)( syntax, in, out, NULL ); +} + +static int +slapi_int_dn_normalize( struct berval *in, struct berval *out ) +{ + MatchingRule *mr = slap_schema.si_mr_distinguishedNameMatch; + Syntax *syntax = slap_schema.si_syn_distinguishedName; + + assert( mr != NULL ); + + return (mr->smr_normalize)( 0, syntax, mr, in, out, NULL ); } void @@ -137,43 +168,19 @@ slapi_entry_set_dn( Slapi_Entry *e, char *ldn ) { -#if defined(LDAP_SLAPI) - struct berval dn = { 0, NULL }; + struct berval dn = BER_BVNULL; dn.bv_val = ldn; dn.bv_len = strlen( ldn ); - dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname ); -#endif /* defined(LDAP_SLAPI) */ + slapi_int_dn_pretty( &dn, &e->e_name ); + slapi_int_dn_normalize( &dn, &e->e_nname ); } Slapi_Entry * slapi_entry_dup( Slapi_Entry *e ) { -#if defined(LDAP_SLAPI) - char *tmp = NULL; - Slapi_Entry *tmpEnt; - int len = 0; - - tmp = slapi_entry2str( e, &len ); - if ( tmp == NULL ) { - return (Slapi_Entry *)NULL; - } - - tmpEnt = (Slapi_Entry *)str2entry( tmp ); - if ( tmpEnt == NULL ) { - slapi_ch_free( (void **)&tmp ); - return (Slapi_Entry *)NULL; - } - - if (tmp != NULL) { - slapi_ch_free( (void **)&tmp ); - } - - return tmpEnt; -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ + return entry_dup( e ); } int @@ -181,8 +188,7 @@ slapi_entry_attr_delete( Slapi_Entry *e, char *type ) { -#if defined(LDAP_SLAPI) - AttributeDescription *ad; + AttributeDescription *ad = NULL; const char *text; if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) { @@ -194,27 +200,19 @@ slapi_entry_attr_delete( } else { return -1; /* something went wrong */ } -#else /* !defined(LDAP_SLAPI) */ - return -1; -#endif /* !defined(LDAP_SLAPI) */ } Slapi_Entry * slapi_entry_alloc( void ) { -#if defined(LDAP_SLAPI) return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) ); -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ } void slapi_entry_free( Slapi_Entry *e ) { -#if defined(LDAP_SLAPI) - entry_free( e ); -#endif /* defined(LDAP_SLAPI) */ + if ( e != NULL ) + entry_free( e ); } int @@ -223,29 +221,25 @@ slapi_entry_attr_merge( char *type, struct berval **vals ) { -#if defined(LDAP_SLAPI) - AttributeDescription *ad; + AttributeDescription *ad = NULL; const char *text; BerVarray bv; int rc; - rc = bvptr2obj( vals, &bv ); + rc = slap_str2ad( type, &ad, &text ); if ( rc != LDAP_SUCCESS ) { return -1; } - rc = slap_str2ad( type, &ad, &text ); + rc = bvptr2obj( vals, &bv ); if ( rc != LDAP_SUCCESS ) { return -1; } - rc = attr_merge( e, ad, bv ); + rc = attr_merge_normalize_one( e, ad, bv, NULL ); ch_free( bv ); return rc; -#else /* !defined(LDAP_SLAPI) */ - return -1; -#endif /* !defined(LDAP_SLAPI) */ } int @@ -254,8 +248,7 @@ slapi_entry_attr_find( char *type, Slapi_Attr **attr ) { -#if defined(LDAP_SLAPI) - AttributeDescription *ad; + AttributeDescription *ad = NULL; const char *text; int rc; @@ -270,1984 +263,3166 @@ slapi_entry_attr_find( } return 0; -#else /* !defined(LDAP_SLAPI) */ - return -1; -#endif /* !defined(LDAP_SLAPI) */ } -/* - * FIXME -- The caller must free the allocated memory. - * In Netscape they do not have to. - */ -int -slapi_attr_get_values( - Slapi_Attr *attr, - struct berval ***vals ) +char * +slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type ) { -#if defined(LDAP_SLAPI) - int i, j; - struct berval **bv; + AttributeDescription *ad = NULL; + const char *text; + int rc; + Attribute *attr; - if ( attr == NULL ) { - return 1; + rc = slap_str2ad( type, &ad, &text ); + if ( rc != LDAP_SUCCESS ) { + return NULL; } - for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) { - ; /* EMPTY */ + attr = attr_find( e->e_attrs, ad ); + if ( attr == NULL ) { + return NULL; } - bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) ); - for ( j = 0; j < i; j++ ) { - bv[j] = (struct berval *)ch_malloc( sizeof(struct berval) ); - bv[j]->bv_val = ch_strdup( attr->a_vals[j].bv_val ); - bv[j]->bv_len = attr->a_vals[j].bv_len; + if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) { + const char *p; + + p = slapi_value_get_string( &attr->a_vals[0] ); + if ( p != NULL ) { + return slapi_ch_strdup( p ); + } } - bv[j] = NULL; - - *vals = (struct berval **)bv; - return 0; -#else /* !defined(LDAP_SLAPI) */ - return -1; -#endif /* !defined(LDAP_SLAPI) */ + return NULL; } -char * -slapi_dn_normalize( char *dn ) +int +slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type ) { -#if defined(LDAP_SLAPI) - struct berval bdn; - struct berval ndn; + AttributeDescription *ad = NULL; + const char *text; + int rc; + Attribute *attr; - assert( dn != NULL ); - - bdn.bv_val = dn; - bdn.bv_len = strlen( dn ); + rc = slap_str2ad( type, &ad, &text ); + if ( rc != LDAP_SUCCESS ) { + return 0; + } - dnNormalize2( NULL, &bdn, &ndn ); + attr = attr_find( e->e_attrs, ad ); + if ( attr == NULL ) { + return 0; + } - /* - * FIXME: ain't it safe to set dn = ndn.bv_val ? - */ - dn = ch_strdup( ndn.bv_val ); - ch_free( ndn.bv_val ); - - return dn; -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ + return slapi_value_get_int( attr->a_vals ); } -/* - * FIXME: this function is dangerous and should be deprecated; - * DN normalization is a lot more than lower-casing, and BTW - * OpenLDAP's DN normalization for case insensitive attributes - * is already lower case - */ -char * -slapi_dn_normalize_case( char *dn ) +long +slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type ) { -#if defined(LDAP_SLAPI) - slapi_dn_normalize( dn ); - ldap_pvt_str2lower( dn ); + AttributeDescription *ad = NULL; + const char *text; + int rc; + Attribute *attr; - return dn; -#else /* defined(LDAP_SLAPI) */ - return NULL; -#endif /* defined(LDAP_SLAPI) */ + rc = slap_str2ad( type, &ad, &text ); + if ( rc != LDAP_SUCCESS ) { + return 0; + } + + attr = attr_find( e->e_attrs, ad ); + if ( attr == NULL ) { + return 0; + } + + return slapi_value_get_long( attr->a_vals ); } -int -slapi_dn_issuffix( - char *dn, - char *suffix ) +unsigned int +slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type ) { -#if defined(LDAP_SLAPI) - struct berval bdn, ndn; - struct berval bsuffix, nsuffix; + AttributeDescription *ad = NULL; + const char *text; + int rc; + Attribute *attr; - assert( dn != NULL ); - assert( suffix != NULL ); + rc = slap_str2ad( type, &ad, &text ); + if ( rc != LDAP_SUCCESS ) { + return 0; + } - bdn.bv_val = dn; - bdn.bv_len = strlen( dn ); + attr = attr_find( e->e_attrs, ad ); + if ( attr == NULL ) { + return 0; + } - bsuffix.bv_val = suffix; - bsuffix.bv_len = strlen( suffix ); + return slapi_value_get_uint( attr->a_vals ); +} + +unsigned long +slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type ) +{ + AttributeDescription *ad = NULL; + const char *text; + int rc; + Attribute *attr; - dnNormalize2( NULL, &bdn, &ndn ); - dnNormalize2( NULL, &bsuffix, &nsuffix ); + rc = slap_str2ad( type, &ad, &text ); + if ( rc != LDAP_SUCCESS ) { + return 0; + } - return dnIsSuffix( &ndn, &nsuffix ); -#else /* !defined(LDAP_SLAPI) */ - return 0; -#endif /* !defined(LDAP_SLAPI) */ + attr = attr_find( e->e_attrs, ad ); + if ( attr == NULL ) { + return 0; + } + + return slapi_value_get_ulong( attr->a_vals ); } -char * -slapi_dn_ignore_case( char *dn ) -{ -#if defined(LDAP_SLAPI) - return slapi_dn_normalize_case( dn ); -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ +int +slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value ) +{ + struct berval bv; + AttributeDescription *ad = NULL; + const char *text; + int rc; + Attribute *attr; + + rc = slap_str2ad( type, &ad, &text ); + if ( rc != LDAP_SUCCESS ) { + return 0; + } + + attr = attr_find( e->e_attrs, ad ); + if ( attr == NULL ) { + return 0; + } + + bv.bv_val = (char *)value; + bv.bv_len = strlen( value ); + + return ( slapi_attr_value_find( attr, &bv ) != -1 ); } -char * -slapi_ch_malloc( unsigned long size ) +void +slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value) { -#if defined(LDAP_SLAPI) - return ch_malloc( size ); -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ + AttributeDescription *ad = NULL; + const char *text; + int rc; + struct berval bv; + + rc = slap_str2ad( type, &ad, &text ); + if ( rc != LDAP_SUCCESS ) { + return; + } + + attr_delete ( &e->e_attrs, ad ); + if ( value != NULL ) { + bv.bv_val = (char *)value; + bv.bv_len = strlen(value); + attr_merge_normalize_one( e, ad, &bv, NULL ); + } } -void -slapi_ch_free( void **ptr ) +void +slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l) { -#if defined(LDAP_SLAPI) - ch_free( *ptr ); - *ptr = NULL; -#endif /* defined(LDAP_SLAPI) */ + char buf[64]; + + snprintf( buf, sizeof( buf ), "%d", l ); + slapi_entry_attr_set_charptr( e, type, buf ); } -char * -slapi_ch_calloc( - unsigned long nelem, - unsigned long size ) +void +slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l) { -#if defined(LDAP_SLAPI) - return ch_calloc( nelem, size ); -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ + char buf[64]; + + snprintf( buf, sizeof( buf ), "%u", l ); + slapi_entry_attr_set_charptr( e, type, buf ); } -char * -slapi_ch_realloc( - char *block, - unsigned long size ) +void +slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l) { -#if defined(LDAP_SLAPI) - return ch_realloc( block, size ); -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ + char buf[64]; + + snprintf( buf, sizeof( buf ), "%ld", l ); + slapi_entry_attr_set_charptr( e, type, buf ); } -char * -slapi_ch_strdup( char *s ) +void +slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l) { -#if defined(LDAP_SLAPI) - return ch_strdup( (const char *)s ); -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ + char buf[64]; + + snprintf( buf, sizeof( buf ), "%lu", l ); + slapi_entry_attr_set_charptr( e, type, buf ); } -size_t -slapi_ch_stlen( char *s ) +int +slapi_is_rootdse( const char *dn ) { -#if defined(LDAP_SLAPI) - return strlen( (const char *)s ); -#else /* !defined(LDAP_SLAPI) */ - return 0; -#endif /* !defined(LDAP_SLAPI) */ + return ( dn == NULL || dn[0] == '\0' ); } -int -slapi_control_present( - LDAPControl **controls, - char *oid, - struct berval **val, - int *iscritical ) +int +slapi_entry_has_children( const Slapi_Entry *e ) { -#if defined(LDAP_SLAPI) - int i; - int rc = 0; + Slapi_PBlock *pb; + int hasSubordinates = 0; - if ( val ) { - *val = NULL; - } - - if ( iscritical ) { - *iscritical = 0; + pb = slapi_pblock_new(); + slapi_int_connection_init_pb( pb, LDAP_REQ_SEARCH ); + + slapi_pblock_set( pb, SLAPI_TARGET_DN, slapi_entry_get_dn( (Entry *)e ) ); + + pb->pb_op->o_bd = select_backend( (struct berval *)&e->e_nname, 0, 0 ); + if ( pb->pb_op->o_bd != NULL ) { + pb->pb_op->o_bd->be_has_subordinates( pb->pb_op, (Entry *)e, &hasSubordinates ); } - - for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) { - if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) { - continue; - } - rc = 1; - if ( controls[i]->ldctl_value.bv_len != 0 ) { - /* - * FIXME: according to 6.1 specification, - * "The val output parameter is set - * to point into the controls array. - * A copy of the control value is - * not made." - */ -#if 0 - struct berval *pTmpBval; + slapi_pblock_destroy( pb ); - pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval)); - if ( pTmpBval == NULL ) { - rc = 0; - } else { - pTmpBval->bv_len = controls[i]->ldctl_value.bv_len; - pTmpBval->bv_val = controls[i]->ldctl_value.bv_val; - if ( val ) { - *val = pTmpBval; - } else { - slapi_ch_free( (void **)&pTmpBval ); - rc = 0; - } - } -#endif /* 0 */ - if ( val ) { - *val = &controls[i]->ldctl_value; - } - } + return ( hasSubordinates == LDAP_COMPARE_TRUE ); +} - if ( iscritical ) { - *iscritical = controls[i]->ldctl_iscritical; - } +/* + * Return approximate size of the entry rounded to the nearest + * 1K. Only the size of the attribute values are counted in the + * Sun implementation. + * + * http://docs.sun.com/source/816-6701-10/funcref.html#1017388 + */ +size_t slapi_entry_size(Slapi_Entry *e) +{ + size_t size; + Attribute *a; + int i; - break; + for ( size = 0, a = e->e_attrs; a != NULL; a = a->a_next ) { + for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) { + size += a->a_vals[i].bv_len + 1; + } } - return rc; -#else /* !defined(LDAP_SLAPI) */ - return 0; -#endif /* !defined(LDAP_SLAPI) */ -} + size += 1023; + size -= (size % 1024); -void -slapi_register_supported_control( - char *controloid, - unsigned long controlops ) -{ -#if defined(LDAP_SLAPI) - /* FIXME -- can not add controls to openLDAP dynamically */ - slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_CONTROLS", - "can not add controls to openLDAP dynamically\n" ); -#endif /* defined(LDAP_SLAPI) */ + return size; } -int -slapi_get_supported_controls( - char ***ctrloidsp, - unsigned long **ctrlopsp ) +/* + * Add values to entry. + * + * Returns: + * LDAP_SUCCESS Values added to entry + * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already + * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec) + */ +int +slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals ) { -#if defined(LDAP_SLAPI) - int i, n; - int rc = 1; - char **oids = NULL; - unsigned long *masks = NULL; - - for (n = 0; get_supported_ctrl( n ) != NULL; n++) { - ; /* count them */ - } - - if ( n == 0 ) { - /* no controls */ - *ctrloidsp = NULL; - *ctrlopsp = NULL; - return LDAP_SUCCESS; - } + Modification mod; + const char *text; + int rc; + char textbuf[SLAP_TEXT_BUFLEN]; + mod.sm_op = LDAP_MOD_ADD; + mod.sm_flags = 0; + mod.sm_desc = NULL; + mod.sm_type.bv_val = (char *)type; + mod.sm_type.bv_len = strlen( type ); - oids = (char **)slapi_ch_malloc( (n + 1) * sizeof(char *) ); - if ( oids == NULL ) { - rc = LDAP_NO_MEMORY; - goto error_return; + rc = slap_str2ad( type, &mod.sm_desc, &text ); + if ( rc != LDAP_SUCCESS ) { + return rc; } - masks = (unsigned long *)slapi_ch_malloc( n * sizeof(int) ); - if ( masks == NULL ) { - rc = LDAP_NO_MEMORY; - goto error_return; - } + if ( vals == NULL ) { + /* Apparently vals can be NULL + * FIXME: sm_values = NULL ? */ + mod.sm_values = (BerVarray)ch_malloc( sizeof(struct berval) ); + mod.sm_values->bv_val = NULL; - for ( i = 0; i < n; i++ ) { - /* - * FIXME: Netscape's specification says nothing about - * memory; should we copy the OIDs or return pointers - * to internal values? In OpenLDAP the latter is safe - * since we do not allow to register coltrols runtime - */ - oids[ i ] = ch_strdup( get_supported_ctrl( i ) ); - if ( oids[ i ] == NULL ) { - rc = LDAP_NO_MEMORY; - goto error_return; + } else { + rc = bvptr2obj( vals, &mod.sm_values ); + if ( rc != LDAP_SUCCESS ) { + return LDAP_CONSTRAINT_VIOLATION; } - masks[ i ] = (unsigned long)get_supported_ctrl_mask( i ); } + mod.sm_nvalues = NULL; - *ctrloidsp = oids; - *ctrlopsp = masks; - return LDAP_SUCCESS; + rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) ); -error_return: - if ( rc != LDAP_SUCCESS ) { - for ( i = 0; oids != NULL && oids[ i ] != NULL; i++ ) { - ch_free( oids[ i ] ); - } - ch_free( oids ); - ch_free( masks ); - } + slapi_ch_free( (void **)&mod.sm_values ); - return rc; -#else /* !defined(LDAP_SLAPI) */ - return 1; -#endif /* !defined(LDAP_SLAPI) */ + return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION; } -void -slapi_register_supported_saslmechanism( char *mechanism ) +int +slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals ) { -#if defined(LDAP_SLAPI) - /* FIXME -- can not add saslmechanism to openLDAP dynamically */ - slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL", - "can not add saslmechanism to openLDAP dynamically\n" ); -#endif /* defined(LDAP_SLAPI) */ + return slapi_entry_add_values( e, type, vals ); } -char ** -slapi_get_supported_saslmechanisms( void ) +int +slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs) { -#if defined(LDAP_SLAPI) - /* FIXME -- can not get the saslmechanism wihtout a connection. */ - slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL", - "can not get the saslmechanism " - "wihtout a connection\n" ); - return NULL; -#else /* defined(LDAP_SLAPI) */ - return NULL; -#endif /* defined(LDAP_SLAPI) */ + AttributeDescription *ad = NULL; + const char *text; + int rc; + + rc = slap_str2ad( type, &ad, &text ); + if ( rc != LDAP_SUCCESS ) { + return -1; + } + + return attr_merge_normalize( e, ad, *vs, NULL ); } -char ** -slapi_get_supported_extended_ops( void ) +int +slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals ) { -#if defined(LDAP_SLAPI) - int i, j, k; - char **ppExtOpOID = NULL; - int numExtOps = 0; + Modification mod; + const char *text; + int rc; + char textbuf[SLAP_TEXT_BUFLEN]; - for ( i = 0; get_supported_extop( i ) != NULL; i++ ) { - ; + mod.sm_op = LDAP_MOD_DELETE; + mod.sm_flags = 0; + mod.sm_desc = NULL; + mod.sm_type.bv_val = (char *)type; + mod.sm_type.bv_len = strlen( type ); + + if ( vals == NULL ) { + /* If vals is NULL, this is a NOOP. */ + return LDAP_SUCCESS; } - for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) { - ; + rc = slap_str2ad( type, &mod.sm_desc, &text ); + if ( rc != LDAP_SUCCESS ) { + return rc; } - numExtOps = i + j; - if ( numExtOps == 0 ) { - return NULL; + if ( vals[0] == NULL ) { + /* SLAPI doco says LDApb_opERATIONS_ERROR but LDAP_OTHER is better */ + return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS; } - ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) ); - for ( k = 0; k < i; k++ ) { - struct berval *bv; + rc = bvptr2obj( vals, &mod.sm_values ); + if ( rc != LDAP_SUCCESS ) { + return LDAP_CONSTRAINT_VIOLATION; + } + mod.sm_nvalues = NULL; - bv = get_supported_extop( k ); - assert( bv != NULL ); + rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) ); - ppExtOpOID[ k ] = bv->bv_val; - } - - for ( ; k < j; k++ ) { - struct berval *bv; + slapi_ch_free( (void **)&mod.sm_values ); - bv = ns_get_supported_extop( k ); - assert( bv != NULL ); + return rc; +} - ppExtOpOID[ i + k ] = bv->bv_val; - } - ppExtOpOID[ i + k ] = NULL; +int +slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals ) +{ + return slapi_entry_delete_values( e, type, vals ); +} - return ppExtOpOID; -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ +int +slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals ) +{ + return slapi_entry_attr_merge( e, (char *)type, vals ); } -void -slapi_send_ldap_result( - Slapi_PBlock *pb, - int err, - char *matched, - char *text, - int nentries, - struct berval **urls ) +int +slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value) { -#if defined(LDAP_SLAPI) - Connection *conn; - Operation *op; - struct berval *s; - char *extOID = NULL; - struct berval *extValue = NULL; - int rc; + AttributeDescription *ad = NULL; + int rc; + const char *text; - slapi_pblock_get( pb, SLAPI_CONNECTION, &conn ); - slapi_pblock_get( pb, SLAPI_OPERATION, &op ); - if ( err == LDAP_SASL_BIND_IN_PROGRESS ) { - slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, &s ); - rc = LDAP_SASL_BIND_IN_PROGRESS; - send_ldap_sasl( conn, op, rc, NULL, NULL, NULL, NULL, s ); - return; + rc = slap_str2ad( type, &ad, &text ); + if ( rc != LDAP_SUCCESS ) { + return -1; } - slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID ); - if ( extOID != NULL ) { - slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &extValue ); - slapi_send_ldap_extended_response( conn, op, err, extOID, - extValue ); - return; + rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL ); + if ( rc != LDAP_SUCCESS ) { + return -1; } - send_ldap_result( conn, op, err, matched, text, NULL, NULL ); -#endif /* defined(LDAP_SLAPI) */ + return 0; } -int -slapi_send_ldap_search_entry( - Slapi_PBlock *pb, - Slapi_Entry *e, - LDAPControl **ectrls, - char **attrs, - int attrsonly ) +int +slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value) { -#if defined(LDAP_SLAPI) - Backend *be; - Connection *pConn; - Operation *pOp; - int rc; - - int i; - AttributeName *an = NULL; - const char *text; + Slapi_Value val; - for ( i = 0; attrs[ i ] != NULL; i++ ) { - ; /* empty */ - } + val.bv_val = (char *)value; + val.bv_len = strlen( value ); - if ( i > 0 ) { - an = (AttributeName *) ch_malloc( i * sizeof(AttributeName) ); - for ( i = 0; attrs[i] != NULL; i++ ) { - an[i].an_name.bv_val = ch_strdup( attrs[i] ); - an[i].an_name.bv_len = strlen( attrs[i] ); - an[i].an_desc = NULL; - if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS) - return -1; - } - } + return slapi_entry_add_value( e, type, &val ); +} - if ( ( rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) || - ( rc = slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&pConn) != 0 ) || - ( rc = slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp) != 0 ) ) { - rc = LDAP_OTHER; - } else { - rc = send_search_entry( be, pConn, pOp, e, an, attrsonly, NULL ); - } +int +slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value) +{ + Slapi_Value *vals[2]; + Slapi_Value val; - return rc; + val.bv_val = (char *)value; + val.bv_len = strlen( value ); + vals[0] = &val; + vals[1] = NULL; -#else /* !defined(LDAP_SLAPI) */ - return -1; -#endif /* !defined(LDAP_SLAPI) */ + return slapi_entry_delete_values_sv( e, type, vals ); } - -Slapi_Filter * -slapi_str2filter( char *str ) +int +slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals ) { -#if defined(LDAP_SLAPI) - return str2filter( str ); -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ + return slapi_entry_attr_merge( e, (char *)type, vals ); } -void -slapi_filter_free( - Slapi_Filter *f, - int recurse ) +int +slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr ) { -#if defined(LDAP_SLAPI) - filter_free( f ); -#endif /* defined(LDAP_SLAPI) */ + if ( e == NULL ) { + return -1; + } + + *attr = e->e_attrs; + + return ( *attr != NULL ) ? 0 : -1; } -int -slapi_filter_get_choice( Slapi_Filter *f ) +int +slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr ) { -#if defined(LDAP_SLAPI) - int rc; + if ( e == NULL ) { + return -1; + } - if ( f != NULL ) { - rc = f->f_choice; - } else { - rc = 0; + if ( prevattr == NULL ) { + return -1; } - return rc; -#else /* !defined(LDAP_SLAPI) */ - return -1; /* invalid filter type */ -#endif /* !defined(LDAP_SLAPI) */ + *attr = prevattr->a_next; + + return ( *attr != NULL ) ? 0 : -1; } -int -slapi_filter_get_ava( - Slapi_Filter *f, - char **type, - struct berval **bval ) +int +slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals ) { -#if defined(LDAP_SLAPI) - int ftype; - int rc = LDAP_SUCCESS; - - assert( type != NULL ); - assert( bval != NULL ); - - *type = NULL; - *bval = NULL; - - ftype = f->f_choice; - if ( ftype == LDAP_FILTER_EQUALITY - || ftype == LDAP_FILTER_GE - || ftype == LDAP_FILTER_LE - || ftype == LDAP_FILTER_APPROX ) { - *type = slapi_ch_strdup( f->f_un.f_un_ava->aa_desc->ad_cname.bv_val ); - if ( *type == NULL ) { - rc = LDAP_NO_MEMORY; - goto done; - } + AttributeDescription *ad = NULL; + const char *text; + int rc; + BerVarray bv; + + rc = slap_str2ad( type, &ad, &text ); + if ( rc != LDAP_SUCCESS ) { + return 0; + } - *bval = (struct berval *)slapi_ch_malloc( sizeof(struct berval) ); - if ( *bval == NULL ) { - rc = LDAP_NO_MEMORY; - goto done; - } + attr_delete( &e->e_attrs, ad ); - (*bval)->bv_len = f->f_un.f_un_ava->aa_value.bv_len; - (*bval)->bv_val = slapi_ch_strdup( f->f_un.f_un_ava->aa_value.bv_val ); - if ( (*bval)->bv_val == NULL ) { - rc = LDAP_NO_MEMORY; - goto done; - } - } else { /* filter type not supported */ - rc = -1; + rc = bvptr2obj( vals, &bv ); + if ( rc != LDAP_SUCCESS ) { + return -1; } - -done: + + rc = attr_merge_normalize( e, ad, bv, NULL ); + slapi_ch_free( (void **)&bv ); if ( rc != LDAP_SUCCESS ) { - if ( *bval ) { - ch_free( *bval ); - *bval = NULL; - } - - if ( *type ) { - ch_free( *type ); - *type = NULL; - } + return -1; } - - return rc; -#else /* !defined(LDAP_SLAPI) */ - return -1; -#endif /* !defined(LDAP_SLAPI) */ + + return 0; } -Slapi_Filter * -slapi_filter_list_first( Slapi_Filter *f ) +/* + * FIXME -- The caller must free the allocated memory. + * In Netscape they do not have to. + */ +int +slapi_attr_get_values( + Slapi_Attr *attr, + struct berval ***vals ) { -#if defined(LDAP_SLAPI) - int ftype; - - if ( f == NULL ) { - return NULL; + int i, j; + struct berval **bv; + + if ( attr == NULL ) { + return 1; } - ftype = f->f_choice; - if ( ftype == LDAP_FILTER_AND - || ftype == LDAP_FILTER_OR - || ftype == LDAP_FILTER_NOT ) { - return (Slapi_Filter *)f->f_and; - } else { - return NULL; + for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) { + ; /* EMPTY */ } -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ + + bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) ); + for ( j = 0; j < i; j++ ) { + bv[j] = ber_dupbv( NULL, &attr->a_vals[j] ); + } + bv[j] = NULL; + + *vals = (struct berval **)bv; + + return 0; } -Slapi_Filter * -slapi_filter_list_next( - Slapi_Filter *f, - Slapi_Filter *fprev ) +char * +slapi_dn_normalize( char *dn ) { -#if defined(LDAP_SLAPI) - int ftype; + struct berval bdn; + struct berval pdn; - if ( f == NULL ) { - return NULL; - } + assert( dn != NULL ); + + bdn.bv_val = dn; + bdn.bv_len = strlen( dn ); - ftype = f->f_choice; - if ( ftype == LDAP_FILTER_AND - || ftype == LDAP_FILTER_OR - || ftype == LDAP_FILTER_NOT ) { - if ( f->f_and == fprev ) { - return f->f_and->f_next; - } + if ( slapi_int_dn_pretty( &bdn, &pdn ) != LDAP_SUCCESS ) { + return NULL; } - return NULL; -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ + return pdn.bv_val; } -int -slapi_send_ldap_extended_response( - Connection *conn, - Operation *op, - int errornum, - char *respName, - struct berval *response ) -{ -#if defined(LDAP_SLAPI) - send_ldap_extended( conn,op, errornum, NULL, NULL, NULL, - respName,response, NULL ); - return LDAP_SUCCESS; -#else /* !defined(LDAP_SLAPI) */ - return -1; -#endif /* !defined(LDAP_SLAPI) */ +char * +slapi_dn_normalize_case( char *dn ) +{ + struct berval bdn; + struct berval ndn; + + assert( dn != NULL ); + + bdn.bv_val = dn; + bdn.bv_len = strlen( dn ); + + if ( slapi_int_dn_normalize( &bdn, &ndn ) != LDAP_SUCCESS ) { + return NULL; + } + + return ndn.bv_val; } int -slapi_pw_find( - struct berval **vals, - struct berval *v ) -{ -#if defined(LDAP_SLAPI) - /* - * FIXME: what's the point? - */ - return 1; -#else /* !defined(LDAP_SLAPI) */ - return 1; -#endif /* !defined(LDAP_SLAPI) */ -} - -char * -slapi_get_hostname( void ) +slapi_dn_issuffix( + char *dn, + char *suffix ) { -#if defined(LDAP_SLAPI) - char *hn = NULL; - - /* - * FIXME: I'd prefer a different check ... - */ -#if defined _SPARC - hn = (char *)slapi_ch_malloc( MAX_HOSTNAME ); - if ( hn == NULL) { - slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO", - "can't malloc memory for hostname\n" ); - hn = NULL; - - } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) { - slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO", - "can't get hostname\n" ); - slapi_ch_free( (void **)&hn ); - hn = NULL; - } -#else /* !_SPARC */ - static int been_here = 0; - static char *static_hn = NULL; + struct berval bdn, ndn; + struct berval bsuffix, nsuffix; + int rc; - ldap_pvt_thread_mutex_lock( &slapi_hn_mutex ); - if ( !been_here ) { - static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME ); - if ( static_hn == NULL) { - slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO", - "can't malloc memory for hostname\n" ); - static_hn = NULL; - ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex ); + assert( dn != NULL ); + assert( suffix != NULL ); - return hn; - - } else { - if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) { - slapi_log_error( SLAPI_LOG_FATAL, - "SLAPI_SYSINFO", - "can't get hostname\n" ); - slapi_ch_free( (void **)&static_hn ); - static_hn = NULL; - ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex ); + bdn.bv_val = dn; + bdn.bv_len = strlen( dn ); - return hn; + bsuffix.bv_val = suffix; + bsuffix.bv_len = strlen( suffix ); - } else { - been_here = 1; - } - } + if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) { + return 0; } - ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex ); - - hn = ch_strdup( static_hn ); -#endif /* !_SPARC */ - - return hn; -#else /* !defined(LDAP_SLAPI) */ - return NULL; -#endif /* !defined(LDAP_SLAPI) */ -} -/* - * FIXME: this should go in an appropriate header ... - */ -extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist ); + if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL ) + != LDAP_SUCCESS ) + { + slapi_ch_free( (void **)&ndn.bv_val ); + return 0; + } -int -slapi_log_error( - int severity, - char *subsystem, - char *fmt, - ... ) -{ -#if defined(LDAP_SLAPI) - int rc = LDAP_SUCCESS; - va_list arglist; + rc = dnIsSuffix( &ndn, &nsuffix ); - va_start( arglist, fmt ); - rc = vLogError( severity, subsystem, fmt, arglist ); - va_end( arglist ); + slapi_ch_free( (void **)&ndn.bv_val ); + slapi_ch_free( (void **)&nsuffix.bv_val ); return rc; -#else /* !defined(LDAP_SLAPI) */ - return -1; -#endif /* !defined(LDAP_SLAPI) */ } - -unsigned long -slapi_timer_current_time( void ) +int +slapi_dn_isparent( + const char *parentdn, + const char *childdn ) { -#if defined(LDAP_SLAPI) - static int first_time = 1; -#if !defined (_WIN32) - struct timeval now; - unsigned long ret; + struct berval assertedParentDN, normalizedAssertedParentDN; + struct berval childDN, normalizedChildDN; + struct berval normalizedParentDN; + int match; - ldap_pvt_thread_mutex_lock( &slapi_time_mutex ); - if (first_time) { - first_time = 0; - gettimeofday( &base_time, NULL ); + assert( parentdn != NULL ); + assert( childdn != NULL ); + + assertedParentDN.bv_val = (char *)parentdn; + assertedParentDN.bv_len = strlen( parentdn ); + + if ( dnNormalize( 0, NULL, NULL, &assertedParentDN, + &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS ) + { + return 0; } - gettimeofday( &now, NULL ); - ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 + - (now.tv_usec - base_time.tv_usec); - ldap_pvt_thread_mutex_unlock( &slapi_time_mutex ); - return ret; + childDN.bv_val = (char *)childdn; + childDN.bv_len = strlen( childdn ); - /* - * Ain't it better? - return (slap_get_time() - starttime) * 1000000; - */ -#else /* _WIN32 */ - LARGE_INTEGER now; + if ( dnNormalize( 0, NULL, NULL, &childDN, + &normalizedChildDN, NULL ) != LDAP_SUCCESS ) + { + slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val ); + return 0; + } - if ( first_time ) { - first_time = 0; - performance_counter_present = QueryPerformanceCounter( &base_time ); - QueryPerformanceFrequency( &performance_freq ); + dnParent( &normalizedChildDN, &normalizedParentDN ); + + if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL, + &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS ) + { + match = -1; } - if ( !performance_counter_present ) - return 0; + slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val ); + slapi_ch_free( (void **)&normalizedChildDN.bv_val ); - QueryPerformanceCounter( &now ); - return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart; -#endif /* _WIN32 */ -#else /* !defined(LDAP_SLAPI) */ - return 0; -#endif /* !defined(LDAP_SLAPI) */ + return ( match == 0 ); } /* - * FIXME ? + * Returns DN of the parent entry, or NULL if the DN is + * an empty string or NULL, or has no parent. */ -unsigned long -slapi_timer_get_time( char *label ) +char * +slapi_dn_parent( const char *_dn ) { -#if defined(LDAP_SLAPI) - unsigned long start = slapi_timer_current_time(); - printf("%10ld %10ld usec %s\n", start, 0, label); - return start; -#else /* !defined(LDAP_SLAPI) */ - return 0; -#endif /* !defined(LDAP_SLAPI) */ -} + struct berval dn, prettyDN; + struct berval parentDN; + char *ret; -/* - * FIXME ? - */ -void -slapi_timer_elapsed_time( - char *label, - unsigned long start ) -{ -#if defined(LDAP_SLAPI) - unsigned long stop = slapi_timer_current_time(); - printf ("%10ld %10ld usec %s\n", stop, stop - start, label); -#endif /* defined(LDAP_SLAPI) */ -} + if ( _dn == NULL ) { + return NULL; + } -void -slapi_free_search_results_internal( Slapi_PBlock *pb ) -{ -#if defined(LDAP_SLAPI) - Slapi_Entry **entries; - int k = 0, nEnt = 0; + dn.bv_val = (char *)_dn; + dn.bv_len = strlen( _dn ); - slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt ); - slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries ); - if ( nEnt == 0 ) { - return; - } - - if ( entries == NULL ) { - return; + if ( dn.bv_len == 0 ) { + return NULL; } - - for ( k = 0; k < nEnt; k++ ) { - slapi_entry_free( entries[k] ); + + if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) { + return NULL; } - - slapi_ch_free( (void **)&entries ); -#endif /* defined(LDAP_SLAPI) */ -} -/* - * Internal API to prime a Slapi_PBlock with a Backend. - */ -int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be ) -{ -#if defined(LDAP_SLAPI) - int rc; + dnParent( &prettyDN, &parentDN ); /* in-place */ - rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be ); - if ( rc != LDAP_SUCCESS ) - return rc; + if ( parentDN.bv_len == 0 ) { + slapi_ch_free_string( &prettyDN.bv_val ); + return NULL; + } - rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type ); - if ( rc != LDAP_SUCCESS ) - return rc; + ret = slapi_ch_strdup( parentDN.bv_val ); + slapi_ch_free_string( &prettyDN.bv_val ); - return LDAP_SUCCESS; -#else - return -1; -#endif /* defined(LDAP_SLAPI) */ + return ret; } -#if defined(LDAP_SLAPI) -/* - * If oldStyle is TRUE, then a value suitable for setting to - * the deprecated SLAPI_CONN_AUTHTYPE value is returned - * (pointer to static storage). - * - * If oldStyle is FALSE, then a value suitable for setting to - * the new SLAPI_CONN_AUTHMETHOD will be returned, which is - * a pointer to allocated memory and will include the SASL - * mechanism (if any). - */ -static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle ) +int slapi_dn_isbesuffix( Slapi_PBlock *pb, char *ldn ) { - size_t len; - char *authType; + struct berval ndn; + Backend *be; - switch ( authz->sai_method ) { - case LDAP_AUTH_SASL: - if ( oldStyle ) { - authType = SLAPD_AUTH_SASL; - } else { - len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len; - authType = slapi_ch_malloc( len ); - snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val ); - } - break; - case LDAP_AUTH_SIMPLE: - authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE ); - break; - case LDAP_AUTH_NONE: - authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE ); - break; - default: - authType = NULL; - break; + if ( slapi_is_rootdse( ldn ) ) { + return 0; } - if ( is_tls && authType == NULL ) { - authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL ); + + /* according to spec should already be normalized */ + ndn.bv_len = strlen( ldn ); + ndn.bv_val = ldn; + + be = select_backend( &pb->pb_op->o_req_ndn, 0, 0 ); + if ( be == NULL ) { + return 0; } - return authType; + return be_issuffix( be, &ndn ); } -#endif /* - * Internal API to prime a Slapi_PBlock with a Connection. + * Returns DN of the parent entry; or NULL if the DN is + * an empty string, if the DN has no parent, or if the + * DN is the suffix of the backend database */ -int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn ) +char *slapi_dn_beparent( Slapi_PBlock *pb, const char *ldn ) { -#if defined(LDAP_SLAPI) - char *connAuthType; - int rc; - - rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn ); - if ( rc != LDAP_SUCCESS ) - return rc; + Backend *be; + struct berval dn, prettyDN; + struct berval normalizedDN, parentDN; + char *parent = NULL; - if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) { - rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] ); - if ( rc != LDAP_SUCCESS ) - return rc; + if ( pb == NULL ) { + return NULL; } - if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) { - rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] ); - if ( rc != LDAP_SUCCESS ) - return rc; + PBLOCK_ASSERT_OP( pb, 0 ); + + if ( slapi_is_rootdse( ldn ) ) { + return NULL; } - rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid ); - if ( rc != LDAP_SUCCESS ) - return rc; + dn.bv_val = (char *)ldn; + dn.bv_len = strlen( ldn ); - /* Returns pointer to static string */ - connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 ); - if ( connAuthType != NULL ) { - rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType); - if ( rc != LDAP_SUCCESS ) - return rc; + if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) { + return NULL; } - /* Returns pointer to allocated string */ - connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 ); - if ( connAuthType != NULL ) { - rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType); - if ( rc != LDAP_SUCCESS ) - return rc; - } + be = select_backend( &pb->pb_op->o_req_ndn, 0, 0 ); - if ( conn->c_authz.sai_dn.bv_val != NULL ) { - char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val); - rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn); - if ( rc != LDAP_SUCCESS ) - return rc; + if ( be == NULL || be_issuffix( be, &normalizedDN ) == 0 ) { + dnParent( &prettyDN, &parentDN ); + + if ( parentDN.bv_len != 0 ) + parent = slapi_ch_strdup( parentDN.bv_val ); } - return rc; -#else - return -1; -#endif /* defined(LDAP_SLAPI) */ + slapi_ch_free_string( &prettyDN.bv_val ); + slapi_ch_free_string( &normalizedDN.bv_val ); + + return parent; } -/* - * Internal API to prime a Slapi_PBlock with an Operation. - */ -int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op ) +char * +slapi_dn_ignore_case( char *dn ) +{ + return slapi_dn_normalize_case( dn ); +} + +char * +slapi_ch_malloc( unsigned long size ) { -#if defined(LDAP_SLAPI) - int isRoot = 0; - int isUpdateDn = 0; - int rc; - Backend *be; - char *opAuthType; + return ch_malloc( size ); +} - if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) { - be = NULL; - } - if (be != NULL) { - isRoot = be_isroot( be, &op->o_ndn ); - isUpdateDn = be_isupdate( be, &op->o_ndn ); - } - - rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op ); - if ( rc != LDAP_SUCCESS ) - return rc; +void +slapi_ch_free( void **ptr ) +{ + if ( ptr == NULL || *ptr == NULL ) + return; + ch_free( *ptr ); + *ptr = NULL; +} - rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time ); - if ( rc != LDAP_SUCCESS ) - return rc; +void +slapi_ch_free_string( char **ptr ) +{ + slapi_ch_free( (void **)ptr ); +} - rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid ); - if ( rc != LDAP_SUCCESS ) - return rc; +void +slapi_ch_array_free( char **arrayp ) +{ + char **p; - rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag ); - if ( rc != LDAP_SUCCESS ) - return rc; + if ( arrayp != NULL ) { + for ( p = arrayp; *p != NULL; p++ ) { + slapi_ch_free( (void **)p ); + } + slapi_ch_free( (void **)&arrayp ); + } +} - rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot ); - if ( rc != LDAP_SUCCESS ) - return rc; +struct berval * +slapi_ch_bvdup(const struct berval *v) +{ + return ber_dupbv(NULL, (struct berval *)v); +} - rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn ); - if ( rc != LDAP_SUCCESS ) - return rc; +struct berval ** +slapi_ch_bvecdup(const struct berval **v) +{ + int i; + struct berval **rv; - rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls ); - if ( rc != LDAP_SUCCESS) - return rc; + if ( v == NULL ) { + return NULL; + } - rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val ); - if ( rc != LDAP_SUCCESS ) - return rc; + for ( i = 0; v[i] != NULL; i++ ) + ; - rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType ); - if ( rc == LDAP_SUCCESS && opAuthType != NULL ) { - /* Not quite sure what the point of this is. */ - rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType ); - if ( rc != LDAP_SUCCESS ) - return rc; + rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) ); + + for ( i = 0; v[i] != NULL; i++ ) { + rv[i] = slapi_ch_bvdup( v[i] ); } + rv[i] = NULL; - return LDAP_SUCCESS; -#else - return -1; -#endif + return rv; } -int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL ) +char * +slapi_ch_calloc( + unsigned long nelem, + unsigned long size ) { -#if defined( LDAP_SLAPI ) - Connection *conn; + return ch_calloc( nelem, size ); +} - slapi_pblock_get( pb, SLAPI_CONNECTION, &conn ); - *isSSL = conn->c_is_tls; +char * +slapi_ch_realloc( + char *block, + unsigned long size ) +{ + return ch_realloc( block, size ); +} - return LDAP_SUCCESS; -#else - return -1; -#endif /* defined(LDAP_SLAPI) */ +char * +slapi_ch_strdup( const char *s ) +{ + return ch_strdup( s ); } -/* - * DS 5.x compatability API follow - */ +size_t +slapi_ch_stlen( const char *s ) +{ + return strlen( s ); +} -int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags ) +int +slapi_control_present( + LDAPControl **controls, + char *oid, + struct berval **val, + int *iscritical ) { -#if defined( LDAP_SLAPI ) - AttributeType *at; + int i; + int rc = 0; - if ( attr == NULL ) - return LDAP_PARAM_ERROR; + if ( val ) { + *val = NULL; + } + + if ( iscritical ) { + *iscritical = 0; + } + + for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) { + if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) { + continue; + } - at = attr->a_desc->ad_type; + rc = 1; + if ( controls[i]->ldctl_value.bv_len != 0 ) { + if ( val ) { + *val = &controls[i]->ldctl_value; + } + } - *flags = SLAPI_ATTR_FLAG_STD_ATTR; + if ( iscritical ) { + *iscritical = controls[i]->ldctl_iscritical; + } - if ( is_at_single_value( at ) ) - *flags |= SLAPI_ATTR_FLAG_SINGLE; - if ( is_at_operational( at ) ) - *flags |= SLAPI_ATTR_FLAG_OPATTR; - if ( is_at_obsolete( at ) ) - *flags |= SLAPI_ATTR_FLAG_OBSOLETE; - if ( is_at_collective( at ) ) - *flags |= SLAPI_ATTR_FLAG_COLLECTIVE; - if ( is_at_no_user_mod( at ) ) - *flags |= SLAPI_ATTR_FLAG_NOUSERMOD; + break; + } - return LDAP_SUCCESS; -#else - return -1; -#endif /* defined(LDAP_SLAPI) */ + return rc; } -int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag ) +static void +slapControlMask2SlapiControlOp(slap_mask_t slap_mask, + unsigned long *slapi_mask) { -#if defined( LDAP_SLAPI ) - unsigned long flags; + *slapi_mask = SLAPI_OPERATION_NONE; - if ( slapi_attr_get_flags( attr, &flags ) != 0 ) - return 0; - return (flags & flag) ? 1 : 0; -#else - return 0; -#endif /* defined(LDAP_SLAPI) */ -} + if ( slap_mask & SLAP_CTRL_ABANDON ) + *slapi_mask |= SLAPI_OPERATION_ABANDON; -Slapi_Attr *slapi_attr_new( void ) -{ -#ifdef LDAP_SLAPI - Attribute *ad; + if ( slap_mask & SLAP_CTRL_ADD ) + *slapi_mask |= SLAPI_OPERATION_ADD; - ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) ); + if ( slap_mask & SLAP_CTRL_BIND ) + *slapi_mask |= SLAPI_OPERATION_BIND; - return ad; -#else - return NULL; -#endif -} + if ( slap_mask & SLAP_CTRL_COMPARE ) + *slapi_mask |= SLAPI_OPERATION_COMPARE; -Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type ) -{ -#ifdef LDAP_SLAPI - const char *text; - AttributeDescription *ad; + if ( slap_mask & SLAP_CTRL_DELETE ) + *slapi_mask |= SLAPI_OPERATION_DELETE; - if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) { - return NULL; - } + if ( slap_mask & SLAP_CTRL_MODIFY ) + *slapi_mask |= SLAPI_OPERATION_MODIFY; - a->a_desc = ad; - a->a_vals = NULL; - a->a_next = NULL; - a->a_flags = 0; + if ( slap_mask & SLAP_CTRL_RENAME ) + *slapi_mask |= SLAPI_OPERATION_MODDN; - return a; -#else - return NULL; -#endif -} + if ( slap_mask & SLAP_CTRL_SEARCH ) + *slapi_mask |= SLAPI_OPERATION_SEARCH; -void slapi_attr_free( Slapi_Attr **a ) -{ -#ifdef LDAP_SLAPI - attr_free( *a ); - *a = NULL; -#endif + if ( slap_mask & SLAP_CTRL_UNBIND ) + *slapi_mask |= SLAPI_OPERATION_UNBIND; } -Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr ) +static void +slapiControlOp2SlapControlMask(unsigned long slapi_mask, + slap_mask_t *slap_mask) { -#ifdef LDAP_SLAPI - return attr_dup( (Slapi_Attr *)attr ); -#else - return NULL; -#endif -} + *slap_mask = 0; -int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v ) -{ -#ifdef LDAP_SLAPI - return value_add_one( &a->a_vals, (Slapi_Value *)v ); -#else - return -1; -#endif + if ( slapi_mask & SLAPI_OPERATION_BIND ) + *slap_mask |= SLAP_CTRL_BIND; + + if ( slapi_mask & SLAPI_OPERATION_UNBIND ) + *slap_mask |= SLAP_CTRL_UNBIND; + + if ( slapi_mask & SLAPI_OPERATION_SEARCH ) + *slap_mask |= SLAP_CTRL_SEARCH; + + if ( slapi_mask & SLAPI_OPERATION_MODIFY ) + *slap_mask |= SLAP_CTRL_MODIFY; + + if ( slapi_mask & SLAPI_OPERATION_ADD ) + *slap_mask |= SLAP_CTRL_ADD; + + if ( slapi_mask & SLAPI_OPERATION_DELETE ) + *slap_mask |= SLAP_CTRL_DELETE; + + if ( slapi_mask & SLAPI_OPERATION_MODDN ) + *slap_mask |= SLAP_CTRL_RENAME; + + if ( slapi_mask & SLAPI_OPERATION_COMPARE ) + *slap_mask |= SLAP_CTRL_COMPARE; + + if ( slapi_mask & SLAPI_OPERATION_ABANDON ) + *slap_mask |= SLAP_CTRL_ABANDON; + + *slap_mask |= SLAP_CTRL_GLOBAL; } -int slapi_attr_type2plugin( const char *type, void **pi ) +static int +slapi_int_parse_control( + Operation *op, + SlapReply *rs, + LDAPControl *ctrl ) { - *pi = NULL; + /* Plugins must deal with controls themselves. */ - return LDAP_OTHER; + return LDAP_SUCCESS; } -int slapi_attr_get_type( const Slapi_Attr *attr, char **type ) +void +slapi_register_supported_control( + char *controloid, + unsigned long controlops ) { -#ifdef LDAP_SLAPI - if ( attr == NULL ) { - return LDAP_PARAM_ERROR; - } + slap_mask_t controlmask; - *type = attr->a_desc->ad_cname.bv_val; + slapiControlOp2SlapControlMask( controlops, &controlmask ); - return LDAP_SUCCESS; -#else - return -1; -#endif + register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL ); } -int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp ) +int +slapi_get_supported_controls( + char ***ctrloidsp, + unsigned long **ctrlopsp ) { -#ifdef LDAP_SLAPI - if ( attr == NULL ) { - return LDAP_PARAM_ERROR; + int i, rc; + + rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp ); + if ( rc != LDAP_SUCCESS ) { + return rc; + } + + for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) { + /* In place, naughty. */ + slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) ); } - *oidp = attr->a_desc->ad_type->sat_oid; return LDAP_SUCCESS; -#else - return -1; -#endif } -int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 ) +LDAPControl * +slapi_dup_control( LDAPControl *ctrl ) { -#ifdef LDAP_SLAPI - MatchingRule *mr; - int ret; - int rc; - const char *text; + LDAPControl *ret; - mr = a->a_desc->ad_type->sat_equality; - rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH, - (struct berval *)v1, (void *)v2, &text ); - if ( rc != LDAP_SUCCESS ) - return -1; + ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) ); + ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid ); + ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value ); + ret->ldctl_iscritical = ctrl->ldctl_iscritical; - return ( ret == 0 ) ? 0 : -1; -#else - return -1; -#endif + return ret; } -int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v ) +void +slapi_register_supported_saslmechanism( char *mechanism ) { -#ifdef LDAP_SLAPI - MatchingRule *mr; - struct berval *bv; - int j; + /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */ + slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism", + "OpenLDAP does not support dynamic registration of SASL mechanisms\n" ); +} + +char ** +slapi_get_supported_saslmechanisms( void ) +{ + /* FIXME -- can not get the saslmechanism without a connection. */ + slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms", + "can not get the SASL mechanism list " + "without a connection\n" ); + return NULL; +} + +char ** +slapi_get_supported_extended_ops( void ) +{ + int i, j, k; + char **ppExtOpOID = NULL; + int numExtOps = 0; + + for ( i = 0; get_supported_extop( i ) != NULL; i++ ) { + ; + } + + for ( j = 0; slapi_int_get_supported_extop( j ) != NULL; j++ ) { + ; + } + + numExtOps = i + j; + if ( numExtOps == 0 ) { + return NULL; + } + + ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) ); + for ( k = 0; k < i; k++ ) { + struct berval *bv; + + bv = get_supported_extop( k ); + assert( bv != NULL ); + + ppExtOpOID[ k ] = bv->bv_val; + } + + for ( ; k < j; k++ ) { + struct berval *bv; + + bv = slapi_int_get_supported_extop( k ); + assert( bv != NULL ); + + ppExtOpOID[ i + k ] = bv->bv_val; + } + ppExtOpOID[ i + k ] = NULL; + + return ppExtOpOID; +} + +void +slapi_send_ldap_result( + Slapi_PBlock *pb, + int err, + char *matched, + char *text, + int nentries, + struct berval **urls ) +{ + SlapReply *rs; + + PBLOCK_ASSERT_OP( pb, 0 ); + + rs = pb->pb_rs; + + rs->sr_err = err; + rs->sr_matched = matched; + rs->sr_text = text; + rs->sr_ref = NULL; + + if ( err == LDAP_SASL_BIND_IN_PROGRESS ) { + send_ldap_sasl( pb->pb_op, rs ); + } else if ( rs->sr_rspoid != NULL ) { + send_ldap_extended( pb->pb_op, rs ); + } else { + if ( pb->pb_op->o_tag == LDAP_REQ_SEARCH ) + rs->sr_nentries = nentries; + if ( urls != NULL ) + bvptr2obj( urls, &rs->sr_ref ); + + send_ldap_result( pb->pb_op, rs ); + + if ( urls != NULL ) + slapi_ch_free( (void **)&rs->sr_ref ); + } +} + +int +slapi_send_ldap_search_entry( + Slapi_PBlock *pb, + Slapi_Entry *e, + LDAPControl **ectrls, + char **attrs, + int attrsonly ) +{ + SlapReply rs = { REP_SEARCH }; + int i = 0, j = 0; + AttributeName *an = NULL; + const char *text; + int rc; + + assert( pb->pb_op != NULL ); + + if ( attrs != NULL ) { + for ( i = 0; attrs[ i ] != NULL; i++ ) { + ; /* empty */ + } + } + + if ( i ) { + an = (AttributeName *) slapi_ch_calloc( i + 1, sizeof(AttributeName) ); + for ( i = 0; attrs[i] != NULL; i++ ) { + an[j].an_name.bv_val = attrs[i]; + an[j].an_name.bv_len = strlen( attrs[i] ); + an[j].an_desc = NULL; + if ( slap_bv2ad( &an[j].an_name, &an[j].an_desc, &text ) == LDAP_SUCCESS) { + j++; + } + } + an[j].an_name.bv_len = 0; + an[j].an_name.bv_val = NULL; + } + + rs.sr_err = LDAP_SUCCESS; + rs.sr_matched = NULL; + rs.sr_text = NULL; + rs.sr_ref = NULL; + rs.sr_ctrls = ectrls; + rs.sr_attrs = an; + rs.sr_operational_attrs = NULL; + rs.sr_entry = e; + rs.sr_v2ref = NULL; + rs.sr_flags = 0; + + rc = send_search_entry( pb->pb_op, &rs ); + + slapi_ch_free( (void **)&an ); + + return rc; +} + +int +slapi_send_ldap_search_reference( + Slapi_PBlock *pb, + Slapi_Entry *e, + struct berval **references, + LDAPControl **ectrls, + struct berval **v2refs + ) +{ + SlapReply rs = { REP_SEARCHREF }; + int rc; + + rs.sr_err = LDAP_SUCCESS; + rs.sr_matched = NULL; + rs.sr_text = NULL; + + rc = bvptr2obj( references, &rs.sr_ref ); + if ( rc != LDAP_SUCCESS ) { + return rc; + } + + rs.sr_ctrls = ectrls; + rs.sr_attrs = NULL; + rs.sr_operational_attrs = NULL; + rs.sr_entry = e; + + if ( v2refs != NULL ) { + rc = bvptr2obj( v2refs, &rs.sr_v2ref ); + if ( rc != LDAP_SUCCESS ) { + slapi_ch_free( (void **)&rs.sr_ref ); + return rc; + } + } else { + rs.sr_v2ref = NULL; + } + + rc = send_search_reference( pb->pb_op, &rs ); + + slapi_ch_free( (void **)&rs.sr_ref ); + slapi_ch_free( (void **)&rs.sr_v2ref ); + + return rc; +} + +Slapi_Filter * +slapi_str2filter( char *str ) +{ + return str2filter( str ); +} + +void +slapi_filter_free( + Slapi_Filter *f, + int recurse ) +{ + filter_free( f ); +} + +Slapi_Filter * +slapi_filter_dup( Slapi_Filter *filter ) +{ + return filter_dup( filter, NULL ); +} + +int +slapi_filter_get_choice( Slapi_Filter *f ) +{ + int rc; + + if ( f != NULL ) { + rc = f->f_choice; + } else { + rc = 0; + } + + return rc; +} + +int +slapi_filter_get_ava( + Slapi_Filter *f, + char **type, + struct berval **bval ) +{ + int ftype; + int rc = LDAP_SUCCESS; + + assert( type != NULL ); + assert( bval != NULL ); + + *type = NULL; + *bval = NULL; + + ftype = f->f_choice; + if ( ftype == LDAP_FILTER_EQUALITY + || ftype == LDAP_FILTER_GE + || ftype == LDAP_FILTER_LE + || ftype == LDAP_FILTER_APPROX ) { + /* + * According to the SLAPI Reference Manual these are + * not duplicated. + */ + *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val; + *bval = &f->f_un.f_un_ava->aa_value; + } else { /* filter type not supported */ + rc = -1; + } + + return rc; +} + +Slapi_Filter * +slapi_filter_list_first( Slapi_Filter *f ) +{ + int ftype; + + if ( f == NULL ) { + return NULL; + } + + ftype = f->f_choice; + if ( ftype == LDAP_FILTER_AND + || ftype == LDAP_FILTER_OR + || ftype == LDAP_FILTER_NOT ) { + return (Slapi_Filter *)f->f_list; + } else { + return NULL; + } +} + +Slapi_Filter * +slapi_filter_list_next( + Slapi_Filter *f, + Slapi_Filter *fprev ) +{ + int ftype; + + if ( f == NULL ) { + return NULL; + } + + ftype = f->f_choice; + if ( ftype == LDAP_FILTER_AND + || ftype == LDAP_FILTER_OR + || ftype == LDAP_FILTER_NOT ) + { + return fprev->f_next; + } + + return NULL; +} + +int +slapi_filter_get_attribute_type( Slapi_Filter *f, char **type ) +{ + if ( f == NULL ) { + return -1; + } + + switch ( f->f_choice ) { + case LDAP_FILTER_GE: + case LDAP_FILTER_LE: + case LDAP_FILTER_EQUALITY: + case LDAP_FILTER_APPROX: + *type = f->f_av_desc->ad_cname.bv_val; + break; + case LDAP_FILTER_SUBSTRINGS: + *type = f->f_sub_desc->ad_cname.bv_val; + break; + case LDAP_FILTER_PRESENT: + *type = f->f_desc->ad_cname.bv_val; + break; + case LDAP_FILTER_EXT: + *type = f->f_mr_desc->ad_cname.bv_val; + break; + default: + /* Complex filters need not apply. */ + *type = NULL; + return -1; + } + + return 0; +} + +int +slapi_x_filter_set_attribute_type( Slapi_Filter *f, const char *type ) +{ + AttributeDescription **adp, *ad = NULL; const char *text; int rc; - int ret; - mr = a->a_desc->ad_type->sat_equality; - for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) { - rc = value_match( &ret, a->a_desc, mr, - SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text ); - if ( rc != LDAP_SUCCESS ) { - return -1; - } - if ( ret == 0 ) { - return 0; - } + if ( f == NULL ) { + return -1; + } + + switch ( f->f_choice ) { + case LDAP_FILTER_GE: + case LDAP_FILTER_LE: + case LDAP_FILTER_EQUALITY: + case LDAP_FILTER_APPROX: + adp = &f->f_av_desc; + break; + case LDAP_FILTER_SUBSTRINGS: + adp = &f->f_sub_desc; + break; + case LDAP_FILTER_PRESENT: + adp = &f->f_desc; + break; + case LDAP_FILTER_EXT: + adp = &f->f_mr_desc; + break; + default: + /* Complex filters need not apply. */ + return -1; + } + + rc = slap_str2ad( type, &ad, &text ); + if ( rc == LDAP_SUCCESS ) + *adp = ad; + + return ( rc == LDAP_SUCCESS ) ? 0 : -1; +} + +int +slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial, + char ***any, char **final ) +{ + int i; + + if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) { + return -1; + } + + /* + * The caller shouldn't free but we can't return an + * array of char *s from an array of bervals without + * allocating memory, so we may as well be consistent. + * XXX + */ + *type = f->f_sub_desc->ad_cname.bv_val; + *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL; + if ( f->f_sub_any != NULL ) { + for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) + ; + *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) ); + for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) { + (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val); + } + (*any)[i] = NULL; + } else { + *any = NULL; + } + *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL; + + return 0; +} + +Slapi_Filter * +slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 ) +{ + Slapi_Filter *f = NULL; + + if ( ftype == LDAP_FILTER_AND || + ftype == LDAP_FILTER_OR || + ftype == LDAP_FILTER_NOT ) + { + f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) ); + f->f_choice = ftype; + f->f_list = f1; + f->f_list->f_next = f2; + f->f_next = NULL; + } + + return f; +} + +int +slapi_x_filter_append( int ftype, + Slapi_Filter **pContainingFilter, /* NULL on first call */ + Slapi_Filter **pNextFilter, + Slapi_Filter *filterToAppend ) +{ + if ( ftype == LDAP_FILTER_AND || + ftype == LDAP_FILTER_OR || + ftype == LDAP_FILTER_NOT ) + { + if ( *pContainingFilter == NULL ) { + *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) ); + (*pContainingFilter)->f_choice = ftype; + (*pContainingFilter)->f_list = filterToAppend; + (*pContainingFilter)->f_next = NULL; + } else { + if ( (*pContainingFilter)->f_choice != ftype ) { + /* Sanity check */ + return -1; + } + (*pNextFilter)->f_next = filterToAppend; + } + *pNextFilter = filterToAppend; + + return 0; + } + return -1; +} + +int +slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f, + int verify_access ) +{ + Operation *op; + int rc; + + if ( f == NULL ) { + /* spec says return zero if no filter. */ + return 0; + } + + if ( verify_access ) { + op = pb->pb_op; + if ( op == NULL ) + return LDAP_PARAM_ERROR; + } else { + op = NULL; + } + + /* + * According to acl.c it is safe to call test_filter() with + * NULL arguments... + */ + rc = test_filter( op, e, f ); + switch (rc) { + case LDAP_COMPARE_TRUE: + rc = 0; + break; + case LDAP_COMPARE_FALSE: + break; + case SLAPD_COMPARE_UNDEFINED: + rc = LDAP_OTHER; + break; + case LDAP_PROTOCOL_ERROR: + /* filter type unknown: spec says return -1 */ + rc = -1; + break; + } + + return rc; +} + +int +slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f) +{ + return slapi_filter_test( NULL, e, f, 0 ); +} + +int +slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code ) +{ + switch ( f->f_choice ) { + case LDAP_FILTER_AND: + case LDAP_FILTER_NOT: + case LDAP_FILTER_OR: { + int rc; + + /* + * FIXME: altering f; should we use a temporary? + */ + for ( f = f->f_list; f != NULL; f = f->f_next ) { + rc = slapi_filter_apply( f, fn, arg, error_code ); + if ( rc != 0 ) { + return rc; + } + if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) { + break; + } + } + break; + } + case LDAP_FILTER_EQUALITY: + case LDAP_FILTER_SUBSTRINGS: + case LDAP_FILTER_GE: + case LDAP_FILTER_LE: + case LDAP_FILTER_PRESENT: + case LDAP_FILTER_APPROX: + case LDAP_FILTER_EXT: + *error_code = fn( f, arg ); + break; + default: + *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE; + } + + if ( *error_code == SLAPI_FILTER_SCAN_NOMORE || + *error_code == SLAPI_FILTER_SCAN_CONTINUE ) { + return 0; + } + + return -1; +} + +int +slapi_pw_find( + struct berval **vals, + struct berval *v ) +{ + /* + * FIXME: what's the point? + */ + return 1; +} + +#define MAX_HOSTNAME 512 + +char * +slapi_get_hostname( void ) +{ + char *hn = NULL; + static int been_here = 0; + static char *static_hn = NULL; + + ldap_pvt_thread_mutex_lock( &slapi_hn_mutex ); + if ( !been_here ) { + static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME ); + if ( static_hn == NULL) { + slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname", + "Cannot allocate memory for hostname\n" ); + static_hn = NULL; + ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex ); + + return hn; + + } else { + if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) { + slapi_log_error( SLAPI_LOG_FATAL, + "SLAPI", + "can't get hostname\n" ); + slapi_ch_free( (void **)&static_hn ); + static_hn = NULL; + ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex ); + + return hn; + + } else { + been_here = 1; + } + } + } + ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex ); + + hn = ch_strdup( static_hn ); + + return hn; +} + +/* + * FIXME: this should go in an appropriate header ... + */ +extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist ); + +int +slapi_log_error( + int severity, + char *subsystem, + char *fmt, + ... ) +{ + int rc = LDAP_SUCCESS; + va_list arglist; + + va_start( arglist, fmt ); + rc = slapi_int_log_error( severity, subsystem, fmt, arglist ); + va_end( arglist ); + + return rc; +} + + +unsigned long +slapi_timer_current_time( void ) +{ + static int first_time = 1; +#if !defined (_WIN32) + struct timeval now; + unsigned long ret; + + ldap_pvt_thread_mutex_lock( &slapi_time_mutex ); + if (first_time) { + first_time = 0; + gettimeofday( &base_time, NULL ); + } + gettimeofday( &now, NULL ); + ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 + + (now.tv_usec - base_time.tv_usec); + ldap_pvt_thread_mutex_unlock( &slapi_time_mutex ); + + return ret; + + /* + * Ain't it better? + return (slap_get_time() - starttime) * 1000000; + */ +#else /* _WIN32 */ + LARGE_INTEGER now; + + if ( first_time ) { + first_time = 0; + performance_counter_present = QueryPerformanceCounter( &base_time ); + QueryPerformanceFrequency( &performance_freq ); + } + + if ( !performance_counter_present ) + return 0; + + QueryPerformanceCounter( &now ); + return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart; +#endif /* _WIN32 */ +} + +/* + * FIXME ? + */ +unsigned long +slapi_timer_get_time( char *label ) +{ + unsigned long start = slapi_timer_current_time(); + printf("%10ld %10d usec %s\n", start, 0, label); + return start; +} + +/* + * FIXME ? + */ +void +slapi_timer_elapsed_time( + char *label, + unsigned long start ) +{ + unsigned long stop = slapi_timer_current_time(); + printf ("%10ld %10ld usec %s\n", stop, stop - start, label); +} + +void +slapi_free_search_results_internal( Slapi_PBlock *pb ) +{ + Slapi_Entry **entries; + int k = 0, nEnt = 0; + + slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt ); + slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries ); + if ( nEnt == 0 || entries == NULL ) { + return; + } + + for ( k = 0; k < nEnt; k++ ) { + slapi_entry_free( entries[k] ); + entries[k] = NULL; + } + + slapi_ch_free( (void **)&entries ); +} + +int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL ) +{ + if ( pb == NULL ) + return LDAP_PARAM_ERROR; + + if ( pb->pb_conn == NULL ) + return LDAP_PARAM_ERROR; + +#ifdef HAVE_TLS + *isSSL = pb->pb_conn->c_is_tls; +#else + *isSSL = 0; +#endif + + return LDAP_SUCCESS; +} + +/* + * DS 5.x compatability API follow + */ + +int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags ) +{ + AttributeType *at; + + if ( attr == NULL ) + return LDAP_PARAM_ERROR; + + at = attr->a_desc->ad_type; + + *flags = SLAPI_ATTR_FLAG_STD_ATTR; + + if ( is_at_single_value( at ) ) + *flags |= SLAPI_ATTR_FLAG_SINGLE; + if ( is_at_operational( at ) ) + *flags |= SLAPI_ATTR_FLAG_OPATTR; + if ( is_at_obsolete( at ) ) + *flags |= SLAPI_ATTR_FLAG_OBSOLETE; + if ( is_at_collective( at ) ) + *flags |= SLAPI_ATTR_FLAG_COLLECTIVE; + if ( is_at_no_user_mod( at ) ) + *flags |= SLAPI_ATTR_FLAG_NOUSERMOD; + + return LDAP_SUCCESS; +} + +int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag ) +{ + unsigned long flags; + + if ( slapi_attr_get_flags( attr, &flags ) != 0 ) + return 0; + return (flags & flag) ? 1 : 0; +} + +Slapi_Attr *slapi_attr_new( void ) +{ + Attribute *ad; + + ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) ); + + return ad; +} + +Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type ) +{ + const char *text; + AttributeDescription *ad = NULL; + + if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) { + return NULL; + } + + a->a_desc = ad; + a->a_vals = NULL; + a->a_nvals = NULL; + a->a_next = NULL; + a->a_flags = 0; + + return a; +} + +void slapi_attr_free( Slapi_Attr **a ) +{ + attr_free( *a ); + *a = NULL; +} + +Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr ) +{ + return attr_dup( (Slapi_Attr *)attr ); +} + +int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v ) +{ + struct berval nval; + struct berval *nvalp; + int rc; + AttributeDescription *desc = a->a_desc; + + if ( desc->ad_type->sat_equality && + desc->ad_type->sat_equality->smr_normalize ) { + rc = (*desc->ad_type->sat_equality->smr_normalize)( + SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX, + desc->ad_type->sat_syntax, + desc->ad_type->sat_equality, + (Slapi_Value *)v, &nval, NULL ); + if ( rc != LDAP_SUCCESS ) { + return rc; + } + nvalp = &nval; + } else { + nvalp = NULL; + } + + rc = value_add_one( &a->a_vals, (Slapi_Value *)v ); + if ( rc == 0 && nvalp != NULL ) { + rc = value_add_one( &a->a_nvals, nvalp ); + } else { + a->a_nvals = a->a_vals; + } + + if ( nvalp != NULL ) { + slapi_ch_free_string( &nval.bv_val ); + } + + return rc; +} + +int slapi_attr_type2plugin( const char *type, void **pi ) +{ + *pi = NULL; + + return LDAP_OTHER; +} + +int slapi_attr_get_type( const Slapi_Attr *attr, char **type ) +{ + if ( attr == NULL ) { + return LDAP_PARAM_ERROR; + } + + *type = attr->a_desc->ad_cname.bv_val; + + return LDAP_SUCCESS; +} + +int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp ) +{ + if ( attr == NULL ) { + return LDAP_PARAM_ERROR; + } + *oidp = attr->a_desc->ad_type->sat_oid; + + return LDAP_SUCCESS; +} + +int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 ) +{ + MatchingRule *mr; + int ret; + int rc; + const char *text; + + mr = a->a_desc->ad_type->sat_equality; + rc = value_match( &ret, a->a_desc, mr, + SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, + (struct berval *)v1, (void *)v2, &text ); + if ( rc != LDAP_SUCCESS ) + return -1; + + return ( ret == 0 ) ? 0 : -1; +} + +int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v ) +{ + MatchingRule *mr; + struct berval *bv; + int j; + const char *text; + int rc; + int ret; + + if ( a ->a_vals == NULL ) { + return -1; + } + mr = a->a_desc->ad_type->sat_equality; + for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) { + rc = value_match( &ret, a->a_desc, mr, + SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text ); + if ( rc != LDAP_SUCCESS ) { + return -1; + } + if ( ret == 0 ) { + return 0; + } + } + return -1; +} + +int slapi_attr_type_cmp( const char *t1, const char *t2, int opt ) +{ + AttributeDescription *a1 = NULL; + AttributeDescription *a2 = NULL; + const char *text; + int ret; + + if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) { + return -1; + } + + if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) { + return 1; + } + +#define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \ + ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \ + ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val ))) + + switch ( opt ) { + case SLAPI_TYPE_CMP_EXACT: + ret = ad_cmp( a1, a2 ); + break; + case SLAPI_TYPE_CMP_BASE: + ret = ad_base_cmp( a1, a2 ); + break; + case SLAPI_TYPE_CMP_SUBTYPE: + ret = is_ad_subtype( a2, a2 ); + break; + default: + ret = -1; + break; + } + + return ret; +} + +int slapi_attr_types_equivalent( const char *t1, const char *t2 ) +{ + return ( slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT ) == 0 ); +} + +int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v ) +{ + return slapi_valueset_first_value( &a->a_vals, v ); +} + +int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v ) +{ + return slapi_valueset_next_value( &a->a_vals, hint, v ); +} + +int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues ) +{ + *numValues = slapi_valueset_count( &a->a_vals ); + + return 0; +} + +int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs ) +{ + *vs = &((Slapi_Attr *)a)->a_vals; + + return 0; +} + +int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals ) +{ + return slapi_attr_get_values( a, vals ); +} + +char *slapi_attr_syntax_normalize( const char *s ) +{ + AttributeDescription *ad = NULL; + const char *text; + + if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) { + return NULL; + } + + return ad->ad_cname.bv_val; +} + +Slapi_Value *slapi_value_new( void ) +{ + struct berval *bv; + + bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) ); + + return bv; +} + +Slapi_Value *slapi_value_new_berval(const struct berval *bval) +{ + return ber_dupbv( NULL, (struct berval *)bval ); +} + +Slapi_Value *slapi_value_new_value(const Slapi_Value *v) +{ + return slapi_value_new_berval( v ); +} + +Slapi_Value *slapi_value_new_string(const char *s) +{ + struct berval bv; + + bv.bv_val = (char *)s; + bv.bv_len = strlen( s ); + + return slapi_value_new_berval( &bv ); +} + +Slapi_Value *slapi_value_init(Slapi_Value *val) +{ + val->bv_val = NULL; + val->bv_len = 0; + + return val; +} + +Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval) +{ + return ber_dupbv( v, bval ); +} + +Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s) +{ + v->bv_val = slapi_ch_strdup( s ); + v->bv_len = strlen( s ); + + return v; +} + +Slapi_Value *slapi_value_dup(const Slapi_Value *v) +{ + return slapi_value_new_value( v ); +} + +void slapi_value_free(Slapi_Value **value) +{ + if ( value == NULL ) { + return; + } + + if ( (*value) != NULL ) { + slapi_ch_free( (void **)&(*value)->bv_val ); + slapi_ch_free( (void **)value ); + } +} + +const struct berval *slapi_value_get_berval( const Slapi_Value *value ) +{ + return value; +} + +Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval ) +{ + if ( value == NULL ) { + return NULL; + } + if ( value->bv_val != NULL ) { + slapi_ch_free( (void **)&value->bv_val ); + } + slapi_value_init_berval( value, (struct berval *)bval ); + + return value; +} + +Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom) +{ + if ( value == NULL ) { + return NULL; + } + return slapi_value_set_berval( value, vfrom ); +} + +Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len) +{ + if ( value == NULL ) { + return NULL; + } + if ( value->bv_val != NULL ) { + slapi_ch_free( (void **)&value->bv_val ); + } + value->bv_val = slapi_ch_malloc( len ); + value->bv_len = len; + AC_MEMCPY( value->bv_val, val, len ); + + return value; +} + +int slapi_value_set_string(Slapi_Value *value, const char *strVal) +{ + if ( value == NULL ) { + return -1; + } + slapi_value_set( value, (void *)strVal, strlen( strVal ) ); + return 0; +} + +int slapi_value_set_int(Slapi_Value *value, int intVal) +{ + char buf[64]; + + snprintf( buf, sizeof( buf ), "%d", intVal ); + + return slapi_value_set_string( value, buf ); +} + +const char *slapi_value_get_string(const Slapi_Value *value) +{ + if ( value == NULL ) return NULL; + if ( value->bv_val == NULL ) return NULL; + if ( !checkBVString( value ) ) return NULL; + + return value->bv_val; +} + +int slapi_value_get_int(const Slapi_Value *value) +{ + if ( value == NULL ) return 0; + if ( value->bv_val == NULL ) return 0; + if ( !checkBVString( value ) ) return 0; + + return (int)strtol( value->bv_val, NULL, 10 ); +} + +unsigned int slapi_value_get_uint(const Slapi_Value *value) +{ + if ( value == NULL ) return 0; + if ( value->bv_val == NULL ) return 0; + if ( !checkBVString( value ) ) return 0; + + return (unsigned int)strtoul( value->bv_val, NULL, 10 ); +} + +long slapi_value_get_long(const Slapi_Value *value) +{ + if ( value == NULL ) return 0; + if ( value->bv_val == NULL ) return 0; + if ( !checkBVString( value ) ) return 0; + + return strtol( value->bv_val, NULL, 10 ); +} + +unsigned long slapi_value_get_ulong(const Slapi_Value *value) +{ + if ( value == NULL ) return 0; + if ( value->bv_val == NULL ) return 0; + if ( !checkBVString( value ) ) return 0; + + return strtoul( value->bv_val, NULL, 10 ); +} + +size_t slapi_value_get_length(const Slapi_Value *value) +{ + if ( value == NULL ) + return 0; + + return (size_t) value->bv_len; +} + +int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2) +{ + return slapi_attr_value_cmp( a, v1, v2 ); +} + +/* A ValueSet is a container for a BerVarray. */ +Slapi_ValueSet *slapi_valueset_new( void ) +{ + Slapi_ValueSet *vs; + + vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) ); + *vs = NULL; + + return vs; +} + +void slapi_valueset_free(Slapi_ValueSet *vs) +{ + if ( vs != NULL ) { + BerVarray vp = *vs; + + ber_bvarray_free( vp ); + vp = NULL; + + slapi_ch_free( (void **)&vp ); + } +} + +void slapi_valueset_init(Slapi_ValueSet *vs) +{ + if ( vs != NULL && *vs == NULL ) { + *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) ); + (*vs)->bv_val = NULL; + (*vs)->bv_len = 0; + } +} + +void slapi_valueset_done(Slapi_ValueSet *vs) +{ + BerVarray vp; + + if ( vs == NULL ) + return; + + for ( vp = *vs; vp->bv_val != NULL; vp++ ) { + vp->bv_len = 0; + slapi_ch_free( (void **)&vp->bv_val ); } -#endif - return -1; + /* but don't free *vs or vs */ } -int slapi_attr_type_cmp( const char *t1, const char *t2, int opt ) +void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval) { -#ifdef LDAP_SLAPI - AttributeDescription *a1; - AttributeDescription *a2; - const char *text; - int ret; + struct berval bv; - if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) { + ber_dupbv( &bv, (Slapi_Value *)addval ); + ber_bvarray_add( vs, &bv ); +} + +int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v ) +{ + return slapi_valueset_next_value( vs, 0, v ); +} + +int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v) +{ + int i; + BerVarray vp; + + if ( vs == NULL ) return -1; + + vp = *vs; + + for ( i = 0; vp[i].bv_val != NULL; i++ ) { + if ( i == index ) { + *v = &vp[i]; + return index + 1; + } } - if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) { - return 1; + return -1; +} + +int slapi_valueset_count( const Slapi_ValueSet *vs ) +{ + int i; + BerVarray vp; + + if ( vs == NULL ) + return 0; + + vp = *vs; + + if ( vp == NULL ) + return 0; + + for ( i = 0; vp[i].bv_val != NULL; i++ ) + ; + + return i; + +} + +void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2) +{ + BerVarray vp; + + for ( vp = *vs2; vp->bv_val != NULL; vp++ ) { + slapi_valueset_add_value( vs1, vp ); } +} -#define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \ - ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \ - ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val ))) +int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr, + struct berval *val, int access ) +{ + int rc; + slap_access_t slap_access; + AttributeDescription *ad = NULL; + const char *text; - switch ( opt ) { - case SLAPI_TYPE_CMP_EXACT: - ret = ad_cmp( a1, a2 ); + rc = slap_str2ad( attr, &ad, &text ); + if ( rc != LDAP_SUCCESS ) { + return rc; + } + + /* + * Whilst the SLAPI access types are arranged as a bitmask, the + * documentation indicates that they are to be used separately. + */ + switch ( access & SLAPI_ACL_ALL ) { + case SLAPI_ACL_COMPARE: + slap_access = ACL_COMPARE; break; - case SLAPI_TYPE_CMP_BASE: - ret = ad_base_cmp( a1, a2 ); + case SLAPI_ACL_SEARCH: + slap_access = ACL_SEARCH; break; - case SLAPI_TYPE_CMP_SUBTYPE: - ret = is_ad_subtype( a2, a2 ); + case SLAPI_ACL_READ: + slap_access = ACL_READ; + break; + case SLAPI_ACL_WRITE: + slap_access = ACL_WRITE; + break; + case SLAPI_ACL_DELETE: + slap_access = ACL_WDEL; + break; + case SLAPI_ACL_ADD: + slap_access = ACL_WADD; break; + case SLAPI_ACL_SELF: /* not documented */ + case SLAPI_ACL_PROXY: /* not documented */ default: - ret = -1; + return LDAP_INSUFFICIENT_ACCESS; break; } - return ret; -#else - return -1; -#endif -} + assert( pb->pb_op != NULL ); -int slapi_attr_types_equivalent( const char *t1, const char *t2 ) -{ -#ifdef LDAP_SLAPI - return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT ); -#else - return -1; -#endif + if ( access_allowed( pb->pb_op, e, ad, val, slap_access, NULL ) ) { + return LDAP_SUCCESS; + } + + return LDAP_INSUFFICIENT_ACCESS; } -int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v ) +int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf) { -#ifdef LDAP_SLAPI - return slapi_valueset_first_value( &a->a_vals, v ); -#else - return -1; -#endif + int rc = LDAP_SUCCESS; + Modifications *ml; + + if ( pb == NULL || pb->pb_op == NULL ) + return LDAP_PARAM_ERROR; + + ml = slapi_int_ldapmods2modifications( mods ); + if ( ml == NULL ) { + return LDAP_OTHER; + } + + if ( rc == LDAP_SUCCESS ) { + rc = acl_check_modlist( pb->pb_op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS; + } + + slap_mods_free( ml, 1 ); + + return rc; } -int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v ) +/* + * Synthesise an LDAPMod array from a Modifications list to pass + * to SLAPI. + */ +LDAPMod **slapi_int_modifications2ldapmods( Modifications *modlist ) { -#ifdef LDAP_SLAPI - return slapi_valueset_next_value( &a->a_vals, hint, v ); -#else - return -1; -#endif + Modifications *ml; + LDAPMod **mods, *modp; + int i, j; + + for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next ) + ; + + mods = (LDAPMod **)slapi_ch_malloc( (i + 1) * sizeof(LDAPMod *) ); + + for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) { + mods[i] = (LDAPMod *)slapi_ch_malloc( sizeof(LDAPMod) ); + modp = mods[i]; + modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES; + if ( BER_BVISNULL( &ml->sml_type ) ) { + /* may happen for internally generated mods */ + assert( ml->sml_desc != NULL ); + modp->mod_type = slapi_ch_strdup( ml->sml_desc->ad_cname.bv_val ); + } else { + modp->mod_type = slapi_ch_strdup( ml->sml_type.bv_val ); + } + + if ( ml->sml_values != NULL ) { + for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) + ; + modp->mod_bvalues = (struct berval **)slapi_ch_malloc( (j + 1) * + sizeof(struct berval *) ); + for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) { + modp->mod_bvalues[j] = (struct berval *)slapi_ch_malloc( + sizeof(struct berval) ); + ber_dupbv( modp->mod_bvalues[j], &ml->sml_values[j] ); + } + modp->mod_bvalues[j] = NULL; + } else { + modp->mod_bvalues = NULL; + } + i++; + } + + mods[i] = NULL; + + return mods; } -int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues ) +/* + * Convert a potentially modified array of LDAPMods back to a + * Modification list. Unfortunately the values need to be + * duplicated because slap_mods_check() will try to free them + * before prettying (and we can't easily get out of calling + * slap_mods_check() because we need normalized values). + */ +Modifications *slapi_int_ldapmods2modifications ( LDAPMod **mods ) { -#ifdef LDAP_SLAPI - *numValues = slapi_valueset_count( &a->a_vals ); + Modifications *modlist = NULL, **modtail; + LDAPMod **modp; + char textbuf[SLAP_TEXT_BUFLEN]; + const char *text; - return 0; -#else - return -1; -#endif + if ( mods == NULL ) { + return NULL; + } + + modtail = &modlist; + + for ( modp = mods; *modp != NULL; modp++ ) { + Modifications *mod; + LDAPMod *lmod = *modp; + int i; + const char *text; + AttributeDescription *ad = NULL; + + if ( slap_str2ad( lmod->mod_type, &ad, &text ) != LDAP_SUCCESS ) { + continue; + } + + mod = (Modifications *) slapi_ch_malloc( sizeof(Modifications) ); + mod->sml_op = lmod->mod_op & ~(LDAP_MOD_BVALUES); + mod->sml_flags = 0; + mod->sml_type = ad->ad_cname; + mod->sml_desc = ad; + mod->sml_next = NULL; + + i = 0; + if ( lmod->mod_op & LDAP_MOD_BVALUES ) { + if ( lmod->mod_bvalues != NULL ) { + while ( lmod->mod_bvalues[i] != NULL ) + i++; + } + } else { + if ( lmod->mod_values != NULL ) { + while ( lmod->mod_values[i] != NULL ) + i++; + } + } + + if ( i == 0 ) { + mod->sml_values = NULL; + } else { + mod->sml_values = (BerVarray) slapi_ch_malloc( (i + 1) * sizeof(struct berval) ); + + /* NB: This implicitly trusts a plugin to return valid modifications. */ + if ( lmod->mod_op & LDAP_MOD_BVALUES ) { + for ( i = 0; lmod->mod_bvalues[i] != NULL; i++ ) { + ber_dupbv( &mod->sml_values[i], lmod->mod_bvalues[i] ); + } + } else { + for ( i = 0; lmod->mod_values[i] != NULL; i++ ) { + mod->sml_values[i].bv_val = slapi_ch_strdup( lmod->mod_values[i] ); + mod->sml_values[i].bv_len = strlen( lmod->mod_values[i] ); + } + } + mod->sml_values[i].bv_val = NULL; + mod->sml_values[i].bv_len = 0; + } + mod->sml_nvalues = NULL; + + *modtail = mod; + modtail = &mod->sml_next; + } + + if ( slap_mods_check( modlist, &text, textbuf, sizeof( textbuf ), NULL ) != LDAP_SUCCESS ) { + slap_mods_free( modlist, 1 ); + modlist = NULL; + } + + return modlist; } -int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs ) +/* + * Sun ONE DS 5.x computed attribute support. Computed attributes + * allow for dynamically generated operational attributes, a very + * useful thing indeed. + */ + +/* + * For some reason Sun don't use the normal plugin mechanism + * registration path to register an "evaluator" function (an + * "evaluator" is responsible for adding computed attributes; + * the nomenclature is somewhat confusing). + * + * As such slapi_compute_add_evaluator() registers the + * function directly. + */ +int slapi_compute_add_evaluator(slapi_compute_callback_t function) { -#ifdef LDAP_SLAPI - *vs = &((Slapi_Attr *)a)->a_vals; + Slapi_PBlock *pPlugin = NULL; + int rc; + int type = SLAPI_PLUGIN_OBJECT; + + pPlugin = slapi_pblock_new(); + if ( pPlugin == NULL ) { + rc = LDAP_NO_MEMORY; + goto done; + } + + rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type ); + if ( rc != LDAP_SUCCESS ) { + goto done; + } + + rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function ); + if ( rc != LDAP_SUCCESS ) { + goto done; + } + + rc = slapi_int_register_plugin( frontendDB, pPlugin ); + if ( rc != 0 ) { + rc = LDAP_OTHER; + goto done; + } + +done: + if ( rc != LDAP_SUCCESS ) { + if ( pPlugin != NULL ) { + slapi_pblock_destroy( pPlugin ); + } + return -1; + } return 0; -#else - return -1; -#endif } -int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals ) +/* + * See notes above regarding slapi_compute_add_evaluator(). + */ +int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function) { -#ifdef LDAP_SLAPI - return slapi_attr_get_values( a, vals ); -#else - return -1; -#endif -} + Slapi_PBlock *pPlugin = NULL; + int rc; + int type = SLAPI_PLUGIN_OBJECT; -char *slapi_attr_syntax_normalize( const char *s ) -{ -#ifdef LDAP_SLAPI - AttributeDescription *ad; - const char *text; + pPlugin = slapi_pblock_new(); + if ( pPlugin == NULL ) { + rc = LDAP_NO_MEMORY; + goto done; + } - if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) { - return NULL; + rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type ); + if ( rc != LDAP_SUCCESS ) { + goto done; } - return ad->ad_cname.bv_val; -#else - return -1; -#endif -} + rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function ); + if ( rc != LDAP_SUCCESS ) { + goto done; + } -Slapi_Value *slapi_value_new( void ) -{ -#ifdef LDAP_SLAPI - struct berval *bv; + rc = slapi_int_register_plugin( frontendDB, pPlugin ); + if ( rc != 0 ) { + rc = LDAP_OTHER; + goto done; + } - bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) ); +done: + if ( rc != LDAP_SUCCESS ) { + if ( pPlugin != NULL ) { + slapi_pblock_destroy( pPlugin ); + } + return -1; + } - return bv; -#else - return NULL; -#endif + return 0; } -Slapi_Value *slapi_value_new_berval(const struct berval *bval) +/* + * Call compute evaluators + */ +int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn) { -#ifdef LDAP_SLAPI - return ber_dupbv( NULL, (struct berval *)bval ); -#else - return NULL; -#endif -} + int rc = 0; + slapi_compute_callback_t *pGetPlugin, *tmpPlugin; -Slapi_Value *slapi_value_new_value(const Slapi_Value *v) -{ -#ifdef LDAP_SLAPI - return slapi_value_new_berval( v ); -#else - return NULL; -#endif -} + rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin ); + if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) { + /* Nothing to do; front-end should ignore. */ + return 0; + } -Slapi_Value *slapi_value_new_string(const char *s) -{ -#ifdef LDAP_SLAPI - struct berval bv; + for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) { + /* + * -1: no attribute matched requested type + * 0: one attribute matched + * >0: error happened + */ + rc = (*pGetPlugin)( c, type, e, outputfn ); + if ( rc > 0 ) { + break; + } + } - bv.bv_val = (char *)s; - bv.bv_len = strlen( s ); + slapi_ch_free( (void **)&tmpPlugin ); - return slapi_value_new_berval( &bv ); -#else - return NULL; -#endif + return rc; } -Slapi_Value *slapi_value_init(Slapi_Value *val) +int +compute_rewrite_search_filter( Slapi_PBlock *pb ) { -#ifdef LDAP_SLAPI - val->bv_val = NULL; - val->bv_len = 0; + if ( pb == NULL || pb->pb_op == NULL ) + return LDAP_PARAM_ERROR; - return val; -#else - return NULL; -#endif + return slapi_int_call_plugins( pb->pb_op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb ); } -Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval) +/* + * New API to provide the plugin with access to the search + * pblock. Have informed Sun DS team. + */ +int +slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb) { -#ifdef LDAP_SLAPI - return ber_dupbv( v, bval ); -#else - return NULL; -#endif -} + if ( c == NULL ) + return -1; -Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s) -{ -#ifdef LDAP_SLAPI - v->bv_val = slapi_ch_strdup( (char *)s ); - v->bv_len = strlen( s ); + if ( c->cac_pb == NULL ) + return -1; - return v; -#else - return NULL; -#endif + *pb = c->cac_pb; + + return 0; } -Slapi_Value *slapi_value_dup(const Slapi_Value *v) +Slapi_Mutex *slapi_new_mutex( void ) { -#ifdef LDAP_SLAPI - return slapi_value_new_value( v ); -#else - return NULL; -#endif + Slapi_Mutex *m; + + m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) ); + if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) { + slapi_ch_free( (void **)&m ); + return NULL; + } + + return m; } -void slapi_value_free(Slapi_Value **value) +void slapi_destroy_mutex( Slapi_Mutex *mutex ) { -#ifdef LDAP_SLAPI - if ( value == NULL ) { - return; + if ( mutex != NULL ) { + ldap_pvt_thread_mutex_destroy( &mutex->mutex ); + slapi_ch_free( (void **)&mutex); } - if ( *value != NULL ) { - Slapi_Value *v; +} - slapi_ch_free( (void **)&v->bv_val ); - slapi_ch_free( (void **)&v ); - } -#endif +void slapi_lock_mutex( Slapi_Mutex *mutex ) +{ + ldap_pvt_thread_mutex_lock( &mutex->mutex ); } -const struct berval *slapi_value_get_berval( const Slapi_Value *value ) +int slapi_unlock_mutex( Slapi_Mutex *mutex ) { -#ifdef LDAP_SLAPI - return value; -#else - return NULL; -#endif + return ldap_pvt_thread_mutex_unlock( &mutex->mutex ); } -Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval ) +Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex ) { -#ifdef LDAP_SLAPI - if ( value == NULL ) { + Slapi_CondVar *cv; + + if ( mutex == NULL ) { return NULL; } - if ( value->bv_val != NULL ) { - slapi_ch_free( (void **)&value->bv_val ); + + cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) ); + if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) { + slapi_ch_free( (void **)&cv ); + return NULL; } - slapi_value_init_berval( value, (struct berval *)bval ); - return value; -#else - return NULL; -#endif + cv->mutex = mutex->mutex; + + return cv; } -Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom) +void slapi_destroy_condvar( Slapi_CondVar *cvar ) { -#ifdef LDAP_SLAPI - if ( value == NULL ) { - return NULL; + if ( cvar != NULL ) { + ldap_pvt_thread_cond_destroy( &cvar->cond ); + slapi_ch_free( (void **)&cvar ); } - return slapi_value_set_berval( value, vfrom ); -#else - return NULL; -#endif } -Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len) +int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout ) { -#ifdef LDAP_SLAPI - if ( value == NULL ) { - return NULL; - } - if ( value->bv_val != NULL ) { - slapi_ch_free( (void **)&value->bv_val ); + if ( cvar == NULL ) { + return -1; } - value->bv_val = slapi_ch_malloc( len ); - value->bv_len = len; - AC_MEMCPY( value->bv_val, val, len ); - return value; -#else - return NULL; -#endif + return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex ); } -int slapi_value_set_string(Slapi_Value *value, const char *strVal) +int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all ) { -#ifdef LDAP_SLAPI - if ( value == NULL ) { + if ( cvar == NULL ) { return -1; } - slapi_value_set( value, (void *)strVal, strlen( strVal ) ); - return 0; -#else - return NULL; -#endif -} - -int slapi_value_set_int(Slapi_Value *value, int intVal) -{ -#ifdef LDAP_SLAPI - char buf[64]; - snprintf( buf, sizeof( buf ), "%d", intVal ); + if ( notify_all ) { + return ldap_pvt_thread_cond_broadcast( &cvar->cond ); + } - return slapi_value_set_string( value, buf ); -#else - return -1; -#endif + return ldap_pvt_thread_cond_signal( &cvar->cond ); } -const char *slapi_value_get_string(const Slapi_Value *value) +int slapi_int_access_allowed( Operation *op, + Entry *entry, + AttributeDescription *desc, + struct berval *val, + slap_access_t access, + AccessControlState *state ) { -#ifdef LDAP_SLAPI - if ( value == NULL ) { - return NULL; + int rc, slap_access = 0; + slapi_acl_callback_t *pGetPlugin, *tmpPlugin; + Slapi_PBlock *pb; + + pb = SLAPI_OPERATION_PBLOCK( op ); + if ( pb == NULL ) { + /* internal operation */ + return 1; } - return value->bv_val; -#else - return NULL; -#endif -} -#ifdef LDAP_SLAPI -static int checkBVString(const struct berval *bv) -{ - int i; + switch ( access ) { + case ACL_COMPARE: + slap_access |= SLAPI_ACL_COMPARE; + break; + case ACL_SEARCH: + slap_access |= SLAPI_ACL_SEARCH; + break; + case ACL_READ: + slap_access |= SLAPI_ACL_READ; + break; + case ACL_WRITE: + slap_access |= SLAPI_ACL_WRITE; + break; + case ACL_WDEL: + slap_access |= SLAPI_ACL_DELETE; + break; + case ACL_WADD: + slap_access |= SLAPI_ACL_ADD; + break; + default: + break; + } - for ( i = 0; i < bv->bv_len; i++ ) { - if ( bv->bv_val[i] == '\0' ) - return 0; + rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin ); + if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) { + /* nothing to do; allowed access */ + return 1; } - if ( bv->bv_val[i] != '\0' ) - return 0; - return 1; -} -#endif + rc = 1; /* default allow policy */ -int slapi_value_get_int(const Slapi_Value *value) -{ -#ifdef LDAP_SLAPI - if ( value == NULL ) return 0; - if ( !checkBVString( value ) ) return 0; + for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) { + /* + * 0 access denied + * 1 access granted + */ + rc = (*pGetPlugin)( pb, entry, desc->ad_cname.bv_val, + val, slap_access, (void *)state ); + if ( rc == 0 ) { + break; + } + } - return (int)strtol( value->bv_val, NULL, 10 ); -#else - return NULL; -#endif + slapi_ch_free( (void **)&tmpPlugin ); + + return rc; } -unsigned int slapi_value_get_uint(const Slapi_Value *value) +/* + * There is no documentation for this. + */ +int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv ) { -#ifdef LDAP_SLAPI - if ( value == NULL ) return 0; - if ( !checkBVString( value ) ) return 0; + LDAPRDN lrdn; + LDAPAVA *ava; + int rc; + char *p; - return (unsigned int)strtoul( value->bv_val, NULL, 10 ); -#else - return NULL; -#endif -} + *type = NULL; -long slapi_value_get_long(const Slapi_Value *value) -{ -#ifdef LDAP_SLAPI - if ( value == NULL ) return 0; - if ( !checkBVString( value ) ) return 0; + bv->bv_len = 0; + bv->bv_val = NULL; - return strtol( value->bv_val, NULL, 10 ); -#else - return NULL; -#endif -} + rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 ); + if ( rc != LDAP_SUCCESS ) { + return -1; + } -unsigned long slapi_value_get_ulong(const Slapi_Value *value) -{ -#ifdef LDAP_SLAPI - if ( value == NULL ) return 0; - if ( !checkBVString( value ) ) return 0; + if ( lrdn[1] != NULL ) { + return -1; /* not single valued */ + } - return strtoul( value->bv_val, NULL, 10 ); -#else - return NULL; -#endif -} + ava = lrdn[0]; -size_t slapi_value_get_length(const Slapi_Value *value) -{ -#ifdef LDAP_SLAPI - if ( value == NULL ) - return 0; + *type = slapi_ch_strdup( ava->la_attr.bv_val ); + ber_dupbv( bv, &ava->la_value ); - return (size_t) value->bv_len; -#else - return 0; -#endif -} + ldap_rdnfree(lrdn); -int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2) -{ -#ifdef LDAP_SLAPI - return slapi_attr_value_cmp( a, v1, v2 ); -#else - return -1; -#endif + return 0; } -/* A ValueSet is a container for a BerVarray. */ -Slapi_ValueSet *slapi_valueset_new( void ) +char *slapi_dn_plus_rdn( const char *dn, const char *rdn ) { -#ifdef LDAP_SLAPI - Slapi_ValueSet *vs; + struct berval new_dn, parent_dn, newrdn; - vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) ); - *vs = NULL; + new_dn.bv_val = NULL; - return vs; -#else - return NULL; -#endif -} + parent_dn.bv_val = (char *)dn; + parent_dn.bv_len = strlen( dn ); -void slapi_valueset_free(Slapi_ValueSet *vs) -{ -#ifdef LDAP_SLAPI - if ( vs != NULL ) { - BerVarray vp = *vs; + newrdn.bv_val = (char *)rdn; + newrdn.bv_len = strlen( rdn ); - ber_bvarray_free( vp ); - slapi_ch_free( (void **)&vp ); + build_new_dn( &new_dn, &parent_dn, &newrdn, NULL ); - *vs = NULL; - } -#endif + return new_dn.bv_val; } -void slapi_valueset_init(Slapi_ValueSet *vs) +int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e ) { -#ifdef LDAP_SLAPI - if ( vs != NULL && *vs == NULL ) { - *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) ); - (*vs)->bv_val = NULL; - (*vs)->bv_len = 0; - } -#endif -} + Backend *be_orig; + const char *text; + char textbuf[SLAP_TEXT_BUFLEN] = { '\0' }; + size_t textlen = sizeof textbuf; + int rc = LDAP_SUCCESS; -void slapi_valueset_done(Slapi_ValueSet *vs) -{ -#ifdef LDAP_SLAPI - BerVarray vp; + PBLOCK_ASSERT_OP( pb, 0 ); - if ( vs == NULL ) - return; + be_orig = pb->pb_op->o_bd; - for ( vp = *vs; vp->bv_val != NULL; vp++ ) { - vp->bv_len = 0; - slapi_ch_free( (void **)&vp->bv_val ); + pb->pb_op->o_bd = select_backend( &e->e_nname, 0, 0 ); + if ( pb->pb_op->o_bd != NULL ) { + rc = entry_schema_check( pb->pb_op, e, NULL, 0, + &text, textbuf, textlen ); } - /* but don't free *vs or vs */ -#endif -} + pb->pb_op->o_bd = be_orig; -void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval) -{ -#ifdef LDAP_SLAPI - ber_bvarray_add( vs, (Slapi_Value *)addval ); -#endif + return ( rc == LDAP_SUCCESS ) ? 0 : 1; } -int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v ) +int slapi_entry_rdn_values_present( const Slapi_Entry *e ) { -#ifdef LDAP_SLAPI - return slapi_valueset_next_value( vs, 0, v ); -#else - return -1; -#endif -} + LDAPDN dn; + int rc; + int i = 0, match = 0; -int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v) -{ -#ifdef LDAP_SLAPI - int i; - BerVarray vp; + rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 ); + if ( rc != LDAP_SUCCESS ) { + return 0; + } - if ( vs == NULL ) - return -1; + if ( dn[0] != NULL ) { + LDAPRDN rdn = dn[0]; - vp = *vs; + for ( i = 0; rdn[i] != NULL; i++ ) { + LDAPAVA *ava = &rdn[0][i]; + Slapi_Attr *a = NULL; - for ( i = 0; vp[i].bv_val != NULL; i++ ) { - if ( i == index ) { - *v = &vp[i]; - return index + 1; + if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 && + slapi_attr_value_find( a, &ava->la_value ) == 0 ) + match++; } } -#endif - return -1; + ldap_dnfree( dn ); + + return ( i == match ); } -int slapi_valueset_count( const Slapi_ValueSet *vs ) +int slapi_entry_add_rdn_values( Slapi_Entry *e ) { -#ifdef LDAP_SLAPI - int i; - BerVarray vp; + LDAPDN dn; + int i, rc; - if ( vs == NULL ) - return 0; - - vp = *vs; + rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 ); + if ( rc != LDAP_SUCCESS ) { + return rc; + } - for ( i = 0; vp[i].bv_val != NULL; i++ ) - ; + if ( dn[0] != NULL ) { + LDAPRDN rdn = dn[0]; + struct berval *vals[2]; - return i; -#else - return 0; -#endif + for ( i = 0; rdn[i] != NULL; i++ ) { + LDAPAVA *ava = &rdn[0][i]; + Slapi_Attr *a = NULL; -} + if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 && + slapi_attr_value_find( a, &ava->la_value ) == 0 ) + continue; -void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2) -{ -#ifdef LDAP_SLAPI - BerVarray vp; + vals[0] = &ava->la_value; + vals[1] = NULL; - for ( vp = *vs2; vp->bv_val != NULL; vp++ ) { - slapi_valueset_add_value( vs1, vp ); + slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals ); + } } -#endif + + ldap_dnfree( dn ); + + return LDAP_SUCCESS; } -/* - * Attribute sets are an OpenLDAP extension for the - * virtual operational attribute coalescing plugin - */ -Slapi_AttrSet *slapi_x_attrset_new( void ) +const char *slapi_entry_get_uniqueid( const Slapi_Entry *e ) { -#ifdef LDAP_SLAPI - Slapi_AttrSet *a; + Attribute *attr; - /* - * Like a Slapi_ValueSet, a Slapi_AttrSet is a container - * for objects: we need this because it may be initially - * empty. - */ - a = (Slapi_AttrSet *)slapi_ch_malloc( sizeof( *a ) ); - *a = NULL; + attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID ); + if ( attr == NULL ) { + return NULL; + } + + if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) { + return slapi_value_get_string( &attr->a_vals[0] ); + } - return a; -#else return NULL; -#endif } -Slapi_AttrSet *slapi_x_attrset_init( Slapi_AttrSet *as, Slapi_Attr *a ) +void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid ) { -#ifdef LDAP_SLAPI - *as = a; - a->a_next = NULL; + struct berval bv; - return as; -#else - return NULL; -#endif + attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID ); + + bv.bv_val = uniqueid; + bv.bv_len = strlen( uniqueid ); + attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL ); } -void slapi_x_attrset_free( Slapi_AttrSet **pAs ) +LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared ) { -#ifdef LDAP_SLAPI - Slapi_AttrSet *as = *pAs; + LDAP *ld; + char *url; + size_t size; + int rc; - if ( as != NULL ) { - attrs_free( *as ); - slapi_ch_free( (void **)&as ); - *pAs = NULL; + size = sizeof("ldap:///"); + if ( secure ) + size++; + size += strlen( ldaphost ); + if ( ldapport != 0 ) + size += 32; + + url = slapi_ch_malloc( size ); + + if ( ldapport != 0 ) { + sprintf( url, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport ); + } else { + sprintf( url, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost ); } -#endif + + rc = ldap_initialize( &ld, url ); + + slapi_ch_free_string( &url ); + + return ( rc == LDAP_SUCCESS ) ? ld : NULL; } -Slapi_AttrSet *slapi_x_attrset_dup( Slapi_AttrSet *as ) +void slapi_ldap_unbind( LDAP *ld ) { -#ifdef LDAP_SLAPI - Slapi_AttrSet *newAs = slapi_x_attrset_new(); - - if ( *as != NULL ) - *newAs = attrs_dup( *as ); - - return newAs; -#else - return NULL; -#endif + ldap_unbind_ext_s( ld, NULL, NULL ); } -int slapi_x_attrset_add_attr( Slapi_AttrSet *as, Slapi_Attr *a ) +int slapi_x_backend_get_flags( const Slapi_Backend *be, unsigned long *flags ) { -#ifdef LDAP_SLAPI - Slapi_Attr *nextAttr; + if ( be == NULL ) + return LDAP_PARAM_ERROR; - if ( as == NULL || a == NULL ) - return -1; + *flags = SLAP_DBFLAGS(be); - if ( *as == NULL ) { - /* First attribute */ - nextAttr = NULL; - (*as) = a; - } else { - /* Non-first attribute */ - nextAttr = (*as)->a_next; - (*as)->a_next = a; - } + return LDAP_SUCCESS; +} - a->a_next = nextAttr; +int +slapi_int_count_controls( LDAPControl **ctrls ) +{ + size_t i; - return 0; -#else - return -1; -#endif + if ( ctrls == NULL ) + return 0; + + for ( i = 0; ctrls[i] != NULL; i++ ) + ; + + return i; } -int slapi_x_attrset_add_attr_copy( Slapi_AttrSet *as, Slapi_Attr *a ) +int +slapi_op_abandoned( Slapi_PBlock *pb ) { -#ifdef LDAP_SLAPI - Slapi_Attr *adup; + if ( pb->pb_op == NULL ) + return 0; - adup = slapi_attr_dup( a ); - return slapi_x_attrset_add_attr( as, adup ); -#else - return -1; -#endif + return ( pb->pb_op->o_abandon ); } -int slapi_x_attrset_find( Slapi_AttrSet *as, const char *type, Slapi_Attr **attr ) +char * +slapi_op_type_to_string(unsigned long type) { -#ifdef LDAP_SLAPI - AttributeDescription *ad; - const char *text; + char *str; - if ( as == NULL || *as == NULL ) { - return -1; - } - - if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) { - return -1; + switch (type) { + case SLAPI_OPERATION_BIND: + str = "bind"; + break; + case SLAPI_OPERATION_UNBIND: + str = "unbind"; + break; + case SLAPI_OPERATION_SEARCH: + str = "search"; + break; + case SLAPI_OPERATION_MODIFY: + str = "modify"; + break; + case SLAPI_OPERATION_ADD: + str = "add"; + break; + case SLAPI_OPERATION_DELETE: + str = "delete"; + break; + case SLAPI_OPERATION_MODDN: + str = "modrdn"; + break; + case SLAPI_OPERATION_COMPARE: + str = "compare"; + break; + case SLAPI_OPERATION_ABANDON: + str = "abandon"; + break; + case SLAPI_OPERATION_EXTENDED: + str = "extended"; + break; + default: + str = "unknown operation type"; + break; } - *attr = attrs_find( *as, ad ); - return ( *attr == NULL ) ? -1 : 0; -#else - return -1; -#endif + return str; } -int slapi_x_attrset_merge( Slapi_AttrSet *as, const char *type, Slapi_ValueSet *vals ) +unsigned long +slapi_op_get_type(Slapi_Operation * op) { -#ifdef LDAP_SLAPI - AttributeDescription *ad; - Slapi_AttrSet *a; - const char *text; + unsigned long type; - if ( vals == NULL || *vals == NULL ) { - /* Must have something to add. */ - return -1; + switch ( op->o_tag ) { + case LDAP_REQ_BIND: + type = SLAPI_OPERATION_BIND; + break; + case LDAP_REQ_UNBIND: + type = SLAPI_OPERATION_UNBIND; + break; + case LDAP_REQ_SEARCH: + type = SLAPI_OPERATION_SEARCH; + break; + case LDAP_REQ_MODIFY: + type = SLAPI_OPERATION_MODIFY; + break; + case LDAP_REQ_ADD: + type = SLAPI_OPERATION_ADD; + break; + case LDAP_REQ_DELETE: + type = SLAPI_OPERATION_DELETE; + break; + case LDAP_REQ_MODRDN: + type = SLAPI_OPERATION_MODDN; + break; + case LDAP_REQ_COMPARE: + type = SLAPI_OPERATION_COMPARE; + break; + case LDAP_REQ_ABANDON: + type = SLAPI_OPERATION_ABANDON; + break; + case LDAP_REQ_EXTENDED: + type = SLAPI_OPERATION_EXTENDED; + break; + default: + type = SLAPI_OPERATION_NONE; + break; } + return type; +} - if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) { - return -1; - } +void slapi_be_set_readonly( Slapi_Backend *be, int readonly ) +{ + if ( be == NULL ) + return; - for ( a = as; *a != NULL; a = &(*a)->a_next ) { - if ( ad_cmp( (*a)->a_desc, ad ) == 0 ) { - break; - } - } + if ( readonly ) + be->be_restrictops |= SLAP_RESTRICT_OP_WRITES; + else + be->be_restrictops &= ~(SLAP_RESTRICT_OP_WRITES); +} - if ( *a == NULL ) { - *a = (Slapi_Attr *) slapi_ch_malloc( sizeof(Attribute) ); - (*a)->a_desc = ad; - (*a)->a_vals = NULL; - (*a)->a_next = NULL; - (*a)->a_flags = 0; - } +int slapi_be_get_readonly( Slapi_Backend *be ) +{ + if ( be == NULL ) + return 0; - return value_add ( &(*a)->a_vals, *vals ); -#else - return -1; -#endif + return ( (be->be_restrictops & SLAP_RESTRICT_OP_WRITES) == SLAP_RESTRICT_OP_WRITES ); } -int slapi_x_attrset_merge_bervals( Slapi_AttrSet *as, const char *type, struct berval **vals ) +const char *slapi_x_be_get_updatedn( Slapi_Backend *be ) { -#ifdef LDAP_SLAPI - BerVarray vp; - int rc; - - if ( bvptr2obj( vals, &vp ) != LDAP_SUCCESS ) { - return -1; - } - rc = slapi_x_attrset_merge( as, type, &vp ); - slapi_ch_free( (void **)&vp ); + if ( be == NULL ) + return NULL; - return rc; -#else - return -1; -#endif + return be->be_update_ndn.bv_val; } -int slapi_x_attrset_delete( Slapi_AttrSet *as, const char *type ) +Slapi_Backend *slapi_be_select( const Slapi_DN *sdn ) { -#ifdef LDAP_SLAPI - AttributeDescription *ad; - const char *text; + Slapi_Backend *be; - if ( as == NULL ) { - return -1; - } + slapi_sdn_get_ndn( sdn ); - if ( *as == NULL ) { - return -1; - } + be = select_backend( (struct berval *)&sdn->ndn, 0, 0 ); - if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) { - return -1; - } + return be; +} - if ( attr_delete( as, ad ) != LDAP_SUCCESS ) { - return -1; - } +#if 0 +void +slapi_operation_set_flag(Slapi_Operation *op, unsigned long flag) +{ +} - return 0; -#else - return -1; -#endif +void +slapi_operation_clear_flag(Slapi_Operation *op, unsigned long flag) +{ +} + +int +slapi_operation_is_flag_set(Slapi_Operation *op, unsigned long flag) +{ } +#endif + +#endif /* LDAP_SLAPI */ +