2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2002-2005 The OpenLDAP Foundation.
5 * Portions Copyright 1997,2002-2003 IBM Corporation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by IBM Corporation for use in
18 * IBM products and subsequently ported to OpenLDAP Software by
19 * Steve Omrani. Additional significant contributors include:
25 #include <ac/string.h>
26 #include <ac/stdarg.h>
28 #include <ac/unistd.h>
38 * server start time (should we use a struct timeval also in slapd?
40 static struct timeval base_time;
41 ldap_pvt_thread_mutex_t slapi_hn_mutex;
42 ldap_pvt_thread_mutex_t slapi_time_mutex;
45 ldap_pvt_thread_mutex_t mutex;
48 struct slapi_condvar {
49 ldap_pvt_thread_cond_t cond;
50 ldap_pvt_thread_mutex_t mutex;
53 static int checkBVString(const struct berval *bv)
57 for ( i = 0; i < bv->bv_len; i++ ) {
58 if ( bv->bv_val[i] == '\0' )
61 if ( bv->bv_val[i] != '\0' )
68 * This function converts an array of pointers to berval objects to
69 * an array of berval objects.
74 struct berval **bvptr,
77 int rc = LDAP_SUCCESS;
81 if ( bvptr == NULL || *bvptr == NULL ) {
85 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
89 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
90 if ( tmpberval == NULL ) {
91 return LDAP_NO_MEMORY;
94 for ( i = 0; bvptr[i] != NULL; i++ ) {
95 tmpberval[i].bv_val = bvptr[i]->bv_val;
96 tmpberval[i].bv_len = bvptr[i]->bv_len;
98 tmpberval[i].bv_val = NULL;
99 tmpberval[i].bv_len = 0;
101 if ( rc == LDAP_SUCCESS ) {
113 Slapi_Entry *e = NULL;
116 pTmpS = slapi_ch_strdup( s );
117 if ( pTmpS != NULL ) {
118 e = str2entry( pTmpS );
119 slapi_ch_free( (void **)&pTmpS );
132 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
133 ret = entry2str( e, len );
134 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
140 slapi_entry_get_dn( Slapi_Entry *e )
142 return e->e_name.bv_val;
146 slapi_x_entry_get_id( Slapi_Entry *e )
152 slapi_int_dn_pretty( struct berval *in, struct berval *out )
154 Syntax *syntax = slap_schema.si_syn_distinguishedName;
156 assert( syntax != NULL );
158 return (syntax->ssyn_pretty)( syntax, in, out, NULL );
162 slapi_int_dn_normalize( struct berval *in, struct berval *out )
164 MatchingRule *mr = slap_schema.si_mr_distinguishedNameMatch;
165 Syntax *syntax = slap_schema.si_syn_distinguishedName;
167 assert( mr != NULL );
169 return (mr->smr_normalize)( 0, syntax, mr, in, out, NULL );
177 struct berval dn = BER_BVNULL;
180 dn.bv_len = strlen( ldn );
182 slapi_int_dn_pretty( &dn, &e->e_name );
183 slapi_int_dn_normalize( &dn, &e->e_nname );
187 slapi_entry_dup( Slapi_Entry *e )
189 return entry_dup( e );
193 slapi_entry_attr_delete(
197 AttributeDescription *ad = NULL;
200 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
201 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
204 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
205 return 0; /* attribute is deleted */
207 return -1; /* something went wrong */
212 slapi_entry_alloc( void )
214 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
218 slapi_entry_free( Slapi_Entry *e )
225 slapi_entry_attr_merge(
228 struct berval **vals )
230 AttributeDescription *ad = NULL;
235 rc = bvptr2obj( vals, &bv );
236 if ( rc != LDAP_SUCCESS ) {
240 rc = slap_str2ad( type, &ad, &text );
241 if ( rc != LDAP_SUCCESS ) {
245 rc = attr_merge_normalize_one( e, ad, bv, NULL );
252 slapi_entry_attr_find(
257 AttributeDescription *ad = NULL;
261 rc = slap_str2ad( type, &ad, &text );
262 if ( rc != LDAP_SUCCESS ) {
266 *attr = attr_find( e->e_attrs, ad );
267 if ( *attr == NULL ) {
275 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
277 AttributeDescription *ad = NULL;
282 rc = slap_str2ad( type, &ad, &text );
283 if ( rc != LDAP_SUCCESS ) {
287 attr = attr_find( e->e_attrs, ad );
288 if ( attr == NULL ) {
292 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
295 p = slapi_value_get_string( &attr->a_vals[0] );
297 return slapi_ch_strdup( p );
305 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
307 AttributeDescription *ad = NULL;
312 rc = slap_str2ad( type, &ad, &text );
313 if ( rc != LDAP_SUCCESS ) {
317 attr = attr_find( e->e_attrs, ad );
318 if ( attr == NULL ) {
322 return slapi_value_get_int( attr->a_vals );
326 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
328 AttributeDescription *ad = NULL;
333 rc = slap_str2ad( type, &ad, &text );
334 if ( rc != LDAP_SUCCESS ) {
338 attr = attr_find( e->e_attrs, ad );
339 if ( attr == NULL ) {
343 return slapi_value_get_long( attr->a_vals );
347 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
349 AttributeDescription *ad = NULL;
354 rc = slap_str2ad( type, &ad, &text );
355 if ( rc != LDAP_SUCCESS ) {
359 attr = attr_find( e->e_attrs, ad );
360 if ( attr == NULL ) {
364 return slapi_value_get_uint( attr->a_vals );
368 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
370 AttributeDescription *ad = NULL;
375 rc = slap_str2ad( type, &ad, &text );
376 if ( rc != LDAP_SUCCESS ) {
380 attr = attr_find( e->e_attrs, ad );
381 if ( attr == NULL ) {
385 return slapi_value_get_ulong( attr->a_vals );
389 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
392 AttributeDescription *ad = NULL;
397 rc = slap_str2ad( type, &ad, &text );
398 if ( rc != LDAP_SUCCESS ) {
402 attr = attr_find( e->e_attrs, ad );
403 if ( attr == NULL ) {
407 bv.bv_val = (char *)value;
408 bv.bv_len = strlen( value );
410 return ( slapi_attr_value_find( attr, &bv ) != -1 );
414 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
416 AttributeDescription *ad = NULL;
421 rc = slap_str2ad( type, &ad, &text );
422 if ( rc != LDAP_SUCCESS ) {
426 attr_delete ( &e->e_attrs, ad );
427 if ( value != NULL ) {
428 bv.bv_val = (char *)value;
429 bv.bv_len = strlen(value);
430 attr_merge_normalize_one( e, ad, &bv, NULL );
435 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
439 snprintf( buf, sizeof( buf ), "%d", l );
440 slapi_entry_attr_set_charptr( e, type, buf );
444 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
448 snprintf( buf, sizeof( buf ), "%u", l );
449 slapi_entry_attr_set_charptr( e, type, buf );
453 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
457 snprintf( buf, sizeof( buf ), "%ld", l );
458 slapi_entry_attr_set_charptr( e, type, buf );
462 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
466 snprintf( buf, sizeof( buf ), "%lu", l );
467 slapi_entry_attr_set_charptr( e, type, buf );
471 slapi_is_rootdse( const char *dn )
473 return ( dn == NULL || dn[0] == '\0' );
477 slapi_entry_has_children( const Slapi_Entry *e )
480 int hasSubordinates = 0;
482 pb = slapi_pblock_new();
483 slapi_int_connection_init_pb( pb, LDAP_REQ_SEARCH );
485 slapi_pblock_set( pb, SLAPI_TARGET_DN, slapi_entry_get_dn( (Entry *)e ) );
487 pb->pb_op->o_bd = select_backend( (struct berval *)&e->e_nname, 0, 0 );
488 if ( pb->pb_op->o_bd != NULL ) {
489 pb->pb_op->o_bd->be_has_subordinates( pb->pb_op, (Entry *)e, &hasSubordinates );
492 slapi_pblock_destroy( pb );
494 return ( hasSubordinates == LDAP_COMPARE_TRUE );
498 * Return approximate size of the entry rounded to the nearest
499 * 1K. Only the size of the attribute values are counted in the
500 * Sun implementation.
502 * http://docs.sun.com/source/816-6701-10/funcref.html#1017388
504 size_t slapi_entry_size(Slapi_Entry *e)
510 for ( size = 0, a = e->e_attrs; a != NULL; a = a->a_next ) {
511 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
512 size += a->a_vals[i].bv_len + 1;
517 size -= (size % 1024);
523 * Add values to entry.
526 * LDAP_SUCCESS Values added to entry
527 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
528 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
531 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
536 char textbuf[SLAP_TEXT_BUFLEN];
538 mod.sm_op = LDAP_MOD_ADD;
541 mod.sm_type.bv_val = (char *)type;
542 mod.sm_type.bv_len = strlen( type );
544 rc = slap_str2ad( type, &mod.sm_desc, &text );
545 if ( rc != LDAP_SUCCESS ) {
549 if ( vals == NULL ) {
550 /* Apparently vals can be NULL
551 * FIXME: sm_values = NULL ? */
552 mod.sm_values = (BerVarray)ch_malloc( sizeof(struct berval) );
553 mod.sm_values->bv_val = NULL;
556 rc = bvptr2obj( vals, &mod.sm_values );
557 if ( rc != LDAP_SUCCESS ) {
558 return LDAP_CONSTRAINT_VIOLATION;
561 mod.sm_nvalues = NULL;
563 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
565 slapi_ch_free( (void **)&mod.sm_values );
567 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
571 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
573 return slapi_entry_add_values( e, type, vals );
577 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
579 AttributeDescription *ad = NULL;
583 rc = slap_str2ad( type, &ad, &text );
584 if ( rc != LDAP_SUCCESS ) {
588 return attr_merge_normalize( e, ad, *vs, NULL );
592 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
597 char textbuf[SLAP_TEXT_BUFLEN];
599 mod.sm_op = LDAP_MOD_DELETE;
602 mod.sm_type.bv_val = (char *)type;
603 mod.sm_type.bv_len = strlen( type );
605 if ( vals == NULL ) {
606 /* If vals is NULL, this is a NOOP. */
610 rc = slap_str2ad( type, &mod.sm_desc, &text );
611 if ( rc != LDAP_SUCCESS ) {
615 if ( vals[0] == NULL ) {
616 /* SLAPI doco says LDApb_opERATIONS_ERROR but LDAP_OTHER is better */
617 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
620 rc = bvptr2obj( vals, &mod.sm_values );
621 if ( rc != LDAP_SUCCESS ) {
622 return LDAP_CONSTRAINT_VIOLATION;
624 mod.sm_nvalues = NULL;
626 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
628 slapi_ch_free( (void **)&mod.sm_values );
634 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
636 return slapi_entry_delete_values( e, type, vals );
640 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
642 return slapi_entry_attr_merge( e, (char *)type, vals );
646 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
648 AttributeDescription *ad = NULL;
652 rc = slap_str2ad( type, &ad, &text );
653 if ( rc != LDAP_SUCCESS ) {
657 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
658 if ( rc != LDAP_SUCCESS ) {
666 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
670 val.bv_val = (char *)value;
671 val.bv_len = strlen( value );
673 return slapi_entry_add_value( e, type, &val );
677 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
679 Slapi_Value *vals[2];
682 val.bv_val = (char *)value;
683 val.bv_len = strlen( value );
687 return slapi_entry_delete_values_sv( e, type, vals );
691 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
693 return slapi_entry_attr_merge( e, (char *)type, vals );
697 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
705 return ( *attr != NULL ) ? 0 : -1;
709 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
715 if ( prevattr == NULL ) {
719 *attr = prevattr->a_next;
721 return ( *attr != NULL ) ? 0 : -1;
725 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
727 AttributeDescription *ad = NULL;
732 rc = slap_str2ad( type, &ad, &text );
733 if ( rc != LDAP_SUCCESS ) {
737 attr_delete( &e->e_attrs, ad );
739 rc = bvptr2obj( vals, &bv );
740 if ( rc != LDAP_SUCCESS ) {
744 rc = attr_merge_normalize( e, ad, bv, NULL );
745 slapi_ch_free( (void **)&bv );
746 if ( rc != LDAP_SUCCESS ) {
754 * FIXME -- The caller must free the allocated memory.
755 * In Netscape they do not have to.
758 slapi_attr_get_values(
760 struct berval ***vals )
765 if ( attr == NULL ) {
769 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
773 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
774 for ( j = 0; j < i; j++ ) {
775 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
779 *vals = (struct berval **)bv;
785 slapi_dn_normalize( char *dn )
790 assert( dn != NULL );
793 bdn.bv_len = strlen( dn );
795 if ( slapi_int_dn_pretty( &bdn, &pdn ) != LDAP_SUCCESS ) {
803 slapi_dn_normalize_case( char *dn )
808 assert( dn != NULL );
811 bdn.bv_len = strlen( dn );
813 if ( slapi_int_dn_normalize( &bdn, &ndn ) != LDAP_SUCCESS ) {
825 struct berval bdn, ndn;
826 struct berval bsuffix, nsuffix;
829 assert( dn != NULL );
830 assert( suffix != NULL );
833 bdn.bv_len = strlen( dn );
835 bsuffix.bv_val = suffix;
836 bsuffix.bv_len = strlen( suffix );
838 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
842 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
845 slapi_ch_free( (void **)&ndn.bv_val );
849 rc = dnIsSuffix( &ndn, &nsuffix );
851 slapi_ch_free( (void **)&ndn.bv_val );
852 slapi_ch_free( (void **)&nsuffix.bv_val );
859 const char *parentdn,
860 const char *childdn )
862 struct berval assertedParentDN, normalizedAssertedParentDN;
863 struct berval childDN, normalizedChildDN;
864 struct berval normalizedParentDN;
867 assert( parentdn != NULL );
868 assert( childdn != NULL );
870 assertedParentDN.bv_val = (char *)parentdn;
871 assertedParentDN.bv_len = strlen( parentdn );
873 if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
874 &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
879 childDN.bv_val = (char *)childdn;
880 childDN.bv_len = strlen( childdn );
882 if ( dnNormalize( 0, NULL, NULL, &childDN,
883 &normalizedChildDN, NULL ) != LDAP_SUCCESS )
885 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
889 dnParent( &normalizedChildDN, &normalizedParentDN );
891 if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
892 &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
897 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
898 slapi_ch_free( (void **)&normalizedChildDN.bv_val );
900 return ( match == 0 );
904 * Returns DN of the parent entry, or NULL if the DN is
905 * an empty string or NULL, or has no parent.
908 slapi_dn_parent( const char *_dn )
910 struct berval dn, prettyDN;
911 struct berval parentDN;
917 dn.bv_val = (char *)_dn;
918 dn.bv_len = strlen( _dn );
920 if ( dn.bv_len == 0 ) {
924 if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
928 dnParent( &prettyDN, &parentDN ); /* in-place */
930 slapi_ch_free( (void **)&prettyDN.bv_val );
932 if ( parentDN.bv_len == 0 ) {
936 return slapi_ch_strdup( parentDN.bv_val );
939 int slapi_dn_isbesuffix( Slapi_PBlock *pb, char *ldn )
944 if ( slapi_is_rootdse( ldn ) ) {
948 /* according to spec should already be normalized */
949 ndn.bv_len = strlen( ldn );
952 be = select_backend( &pb->pb_op->o_req_ndn, 0, 0 );
957 return be_issuffix( be, &ndn );
961 * Returns DN of the parent entry; or NULL if the DN is
962 * an empty string, if the DN has no parent, or if the
963 * DN is the suffix of the backend database
965 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *ldn )
968 struct berval dn, prettyDN;
969 struct berval normalizedDN, parentDN;
976 PBLOCK_ASSERT_OP( pb, 0 );
978 if ( slapi_is_rootdse( ldn ) ) {
982 dn.bv_val = (char *)ldn;
983 dn.bv_len = strlen( ldn );
985 if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
989 be = select_backend( &pb->pb_op->o_req_ndn, 0, 0 );
991 if ( be == NULL || be_issuffix( be, &normalizedDN ) == 0 ) {
992 dnParent( &prettyDN, &parentDN );
994 if ( parentDN.bv_len != 0 )
995 parent = slapi_ch_strdup( parentDN.bv_val );
998 slapi_ch_free_string( &prettyDN.bv_val );
999 slapi_ch_free_string( &normalizedDN.bv_val );
1005 slapi_dn_ignore_case( char *dn )
1007 return slapi_dn_normalize_case( dn );
1011 slapi_ch_malloc( unsigned long size )
1013 return ch_malloc( size );
1017 slapi_ch_free( void **ptr )
1019 if ( ptr == NULL || *ptr == NULL )
1026 slapi_ch_free_string( char **ptr )
1028 slapi_ch_free( (void **)ptr );
1032 slapi_ch_array_free( char **arrayp )
1036 if ( arrayp != NULL ) {
1037 for ( p = arrayp; *p != NULL; p++ ) {
1038 slapi_ch_free( (void **)p );
1040 slapi_ch_free( (void **)&arrayp );
1045 slapi_ch_bvdup(const struct berval *v)
1049 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
1050 bv->bv_len = v->bv_len;
1051 bv->bv_val = slapi_ch_malloc( bv->bv_len );
1052 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1058 slapi_ch_bvecdup(const struct berval **v)
1067 for ( i = 0; v[i] != NULL; i++ )
1070 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1072 for ( i = 0; v[i] != NULL; i++ ) {
1073 rv[i] = slapi_ch_bvdup( v[i] );
1082 unsigned long nelem,
1083 unsigned long size )
1085 return ch_calloc( nelem, size );
1091 unsigned long size )
1093 return ch_realloc( block, size );
1097 slapi_ch_strdup( const char *s )
1099 return ch_strdup( s );
1103 slapi_ch_stlen( const char *s )
1109 slapi_control_present(
1110 LDAPControl **controls,
1112 struct berval **val,
1126 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1127 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1132 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1134 *val = &controls[i]->ldctl_value;
1139 *iscritical = controls[i]->ldctl_iscritical;
1149 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1150 unsigned long *slapi_mask)
1152 *slapi_mask = SLAPI_OPERATION_NONE;
1154 if ( slap_mask & SLAP_CTRL_ABANDON )
1155 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1157 if ( slap_mask & SLAP_CTRL_ADD )
1158 *slapi_mask |= SLAPI_OPERATION_ADD;
1160 if ( slap_mask & SLAP_CTRL_BIND )
1161 *slapi_mask |= SLAPI_OPERATION_BIND;
1163 if ( slap_mask & SLAP_CTRL_COMPARE )
1164 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1166 if ( slap_mask & SLAP_CTRL_DELETE )
1167 *slapi_mask |= SLAPI_OPERATION_DELETE;
1169 if ( slap_mask & SLAP_CTRL_MODIFY )
1170 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1172 if ( slap_mask & SLAP_CTRL_RENAME )
1173 *slapi_mask |= SLAPI_OPERATION_MODDN;
1175 if ( slap_mask & SLAP_CTRL_SEARCH )
1176 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1178 if ( slap_mask & SLAP_CTRL_UNBIND )
1179 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1183 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1184 slap_mask_t *slap_mask)
1188 if ( slapi_mask & SLAPI_OPERATION_BIND )
1189 *slap_mask |= SLAP_CTRL_BIND;
1191 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1192 *slap_mask |= SLAP_CTRL_UNBIND;
1194 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1195 *slap_mask |= SLAP_CTRL_SEARCH;
1197 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1198 *slap_mask |= SLAP_CTRL_MODIFY;
1200 if ( slapi_mask & SLAPI_OPERATION_ADD )
1201 *slap_mask |= SLAP_CTRL_ADD;
1203 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1204 *slap_mask |= SLAP_CTRL_DELETE;
1206 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1207 *slap_mask |= SLAP_CTRL_RENAME;
1209 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1210 *slap_mask |= SLAP_CTRL_COMPARE;
1212 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1213 *slap_mask |= SLAP_CTRL_ABANDON;
1215 *slap_mask |= SLAP_CTRL_GLOBAL;
1219 slapi_int_parse_control(
1224 /* Plugins must deal with controls themselves. */
1226 return LDAP_SUCCESS;
1230 slapi_register_supported_control(
1232 unsigned long controlops )
1234 slap_mask_t controlmask;
1236 slapiControlOp2SlapControlMask( controlops, &controlmask );
1238 register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL );
1242 slapi_get_supported_controls(
1244 unsigned long **ctrlopsp )
1248 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1249 if ( rc != LDAP_SUCCESS ) {
1253 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1254 /* In place, naughty. */
1255 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1258 return LDAP_SUCCESS;
1262 slapi_dup_control( LDAPControl *ctrl )
1266 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1267 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1268 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1269 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1275 slapi_register_supported_saslmechanism( char *mechanism )
1277 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1278 slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1279 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1283 slapi_get_supported_saslmechanisms( void )
1285 /* FIXME -- can not get the saslmechanism without a connection. */
1286 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1287 "can not get the SASL mechanism list "
1288 "without a connection\n" );
1293 slapi_get_supported_extended_ops( void )
1296 char **ppExtOpOID = NULL;
1299 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1303 for ( j = 0; slapi_int_get_supported_extop( j ) != NULL; j++ ) {
1308 if ( numExtOps == 0 ) {
1312 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1313 for ( k = 0; k < i; k++ ) {
1316 bv = get_supported_extop( k );
1317 assert( bv != NULL );
1319 ppExtOpOID[ k ] = bv->bv_val;
1322 for ( ; k < j; k++ ) {
1325 bv = slapi_int_get_supported_extop( k );
1326 assert( bv != NULL );
1328 ppExtOpOID[ i + k ] = bv->bv_val;
1330 ppExtOpOID[ i + k ] = NULL;
1336 slapi_send_ldap_result(
1342 struct berval **urls )
1346 PBLOCK_ASSERT_OP( pb, 0 );
1351 rs->sr_matched = matched;
1355 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1356 send_ldap_sasl( pb->pb_op, rs );
1357 } else if ( rs->sr_rspoid != NULL ) {
1358 send_ldap_extended( pb->pb_op, rs );
1360 if ( pb->pb_op->o_tag == LDAP_REQ_SEARCH )
1361 rs->sr_nentries = nentries;
1363 send_ldap_result( pb->pb_op, rs );
1368 slapi_send_ldap_search_entry(
1371 LDAPControl **ectrls,
1375 SlapReply rs = { REP_SEARCH };
1377 AttributeName *an = NULL;
1381 assert( pb->pb_op != NULL );
1383 if ( attrs != NULL ) {
1384 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1390 an = (AttributeName *) slapi_ch_malloc( (i+1) * sizeof(AttributeName) );
1391 for ( i = 0; attrs[i] != NULL; i++ ) {
1392 an[i].an_name.bv_val = attrs[i];
1393 an[i].an_name.bv_len = strlen( attrs[i] );
1394 an[i].an_desc = NULL;
1395 rs.sr_err = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text );
1396 if ( rs.sr_err != LDAP_SUCCESS) {
1397 slapi_ch_free( (void **)&an );
1401 an[i].an_name.bv_len = 0;
1402 an[i].an_name.bv_val = NULL;
1405 rs.sr_err = LDAP_SUCCESS;
1406 rs.sr_matched = NULL;
1409 rs.sr_ctrls = ectrls;
1411 rs.sr_operational_attrs = NULL;
1416 rc = send_search_entry( pb->pb_op, &rs );
1418 slapi_ch_free( (void **)&an );
1424 slapi_send_ldap_search_reference(
1427 struct berval **references,
1428 LDAPControl **ectrls,
1429 struct berval **v2refs
1432 SlapReply rs = { REP_SEARCHREF };
1435 rs.sr_err = LDAP_SUCCESS;
1436 rs.sr_matched = NULL;
1439 rc = bvptr2obj( references, &rs.sr_ref );
1440 if ( rc != LDAP_SUCCESS ) {
1444 rs.sr_ctrls = ectrls;
1446 rs.sr_operational_attrs = NULL;
1449 if ( v2refs != NULL ) {
1450 rc = bvptr2obj( v2refs, &rs.sr_v2ref );
1451 if ( rc != LDAP_SUCCESS ) {
1452 slapi_ch_free( (void **)&rs.sr_ref );
1459 rc = send_search_reference( pb->pb_op, &rs );
1461 slapi_ch_free( (void **)&rs.sr_ref );
1462 slapi_ch_free( (void **)&rs.sr_v2ref );
1468 slapi_str2filter( char *str )
1470 return str2filter( str );
1482 slapi_filter_dup( Slapi_Filter *filter )
1486 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1488 f->f_choice = filter->f_choice;
1490 switch ( f->f_choice ) {
1491 case LDAP_FILTER_AND:
1492 case LDAP_FILTER_NOT:
1493 case LDAP_FILTER_OR: {
1494 Filter *pFilter, **ppF;
1496 for ( pFilter = filter->f_list, ppF = &f->f_list;
1498 pFilter = pFilter->f_next, ppF = &f->f_next )
1500 *ppF = slapi_filter_dup( pFilter );
1506 case LDAP_FILTER_PRESENT:
1507 f->f_desc = filter->f_desc;
1509 case LDAP_FILTER_EQUALITY:
1510 case LDAP_FILTER_GE:
1511 case LDAP_FILTER_LE:
1512 case LDAP_FILTER_APPROX:
1513 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1514 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1515 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1517 case LDAP_FILTER_EXT:
1518 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1519 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1520 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1521 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1522 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1523 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1525 case LDAP_FILTER_SUBSTRINGS: {
1528 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1529 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1530 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1531 if ( filter->f_sub_any != NULL ) {
1532 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1534 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1535 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1536 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1538 f->f_sub_any[i].bv_val = NULL;
1540 f->f_sub_any = NULL;
1542 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1545 case SLAPD_FILTER_COMPUTED:
1546 f->f_result = filter->f_result;
1549 slapi_ch_free( (void **)&f );
1558 slapi_filter_get_choice( Slapi_Filter *f )
1572 slapi_filter_get_ava(
1575 struct berval **bval )
1578 int rc = LDAP_SUCCESS;
1580 assert( type != NULL );
1581 assert( bval != NULL );
1586 ftype = f->f_choice;
1587 if ( ftype == LDAP_FILTER_EQUALITY
1588 || ftype == LDAP_FILTER_GE
1589 || ftype == LDAP_FILTER_LE
1590 || ftype == LDAP_FILTER_APPROX ) {
1592 * According to the SLAPI Reference Manual these are
1595 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1596 *bval = &f->f_un.f_un_ava->aa_value;
1597 } else { /* filter type not supported */
1605 slapi_filter_list_first( Slapi_Filter *f )
1613 ftype = f->f_choice;
1614 if ( ftype == LDAP_FILTER_AND
1615 || ftype == LDAP_FILTER_OR
1616 || ftype == LDAP_FILTER_NOT ) {
1617 return (Slapi_Filter *)f->f_list;
1624 slapi_filter_list_next(
1626 Slapi_Filter *fprev )
1634 ftype = f->f_choice;
1635 if ( ftype == LDAP_FILTER_AND
1636 || ftype == LDAP_FILTER_OR
1637 || ftype == LDAP_FILTER_NOT )
1639 return fprev->f_next;
1646 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1652 switch ( f->f_choice ) {
1653 case LDAP_FILTER_GE:
1654 case LDAP_FILTER_LE:
1655 case LDAP_FILTER_EQUALITY:
1656 case LDAP_FILTER_APPROX:
1657 *type = f->f_av_desc->ad_cname.bv_val;
1659 case LDAP_FILTER_SUBSTRINGS:
1660 *type = f->f_sub_desc->ad_cname.bv_val;
1662 case LDAP_FILTER_PRESENT:
1663 *type = f->f_desc->ad_cname.bv_val;
1665 case LDAP_FILTER_EXT:
1666 *type = f->f_mr_desc->ad_cname.bv_val;
1669 /* Complex filters need not apply. */
1678 slapi_x_filter_set_attribute_type( Slapi_Filter *f, const char *type )
1680 AttributeDescription **adp, *ad = NULL;
1688 switch ( f->f_choice ) {
1689 case LDAP_FILTER_GE:
1690 case LDAP_FILTER_LE:
1691 case LDAP_FILTER_EQUALITY:
1692 case LDAP_FILTER_APPROX:
1693 adp = &f->f_av_desc;
1695 case LDAP_FILTER_SUBSTRINGS:
1696 adp = &f->f_sub_desc;
1698 case LDAP_FILTER_PRESENT:
1701 case LDAP_FILTER_EXT:
1702 adp = &f->f_mr_desc;
1705 /* Complex filters need not apply. */
1709 rc = slap_str2ad( type, &ad, &text );
1710 if ( rc == LDAP_SUCCESS )
1713 return ( rc == LDAP_SUCCESS ) ? 0 : -1;
1717 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1718 char ***any, char **final )
1722 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1727 * The caller shouldn't free but we can't return an
1728 * array of char *s from an array of bervals without
1729 * allocating memory, so we may as well be consistent.
1732 *type = f->f_sub_desc->ad_cname.bv_val;
1733 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1734 if ( f->f_sub_any != NULL ) {
1735 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1737 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1738 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1739 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1745 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1751 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1753 Slapi_Filter *f = NULL;
1755 if ( ftype == LDAP_FILTER_AND ||
1756 ftype == LDAP_FILTER_OR ||
1757 ftype == LDAP_FILTER_NOT )
1759 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1760 f->f_choice = ftype;
1762 f->f_list->f_next = f2;
1770 slapi_x_filter_append( int ftype,
1771 Slapi_Filter **pContainingFilter, /* NULL on first call */
1772 Slapi_Filter **pNextFilter,
1773 Slapi_Filter *filterToAppend )
1775 if ( ftype == LDAP_FILTER_AND ||
1776 ftype == LDAP_FILTER_OR ||
1777 ftype == LDAP_FILTER_NOT )
1779 if ( *pContainingFilter == NULL ) {
1780 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
1781 (*pContainingFilter)->f_choice = ftype;
1782 (*pContainingFilter)->f_list = filterToAppend;
1783 (*pContainingFilter)->f_next = NULL;
1785 if ( (*pContainingFilter)->f_choice != ftype ) {
1789 (*pNextFilter)->f_next = filterToAppend;
1791 *pNextFilter = filterToAppend;
1799 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1806 /* spec says return zero if no filter. */
1810 if ( verify_access ) {
1813 return LDAP_PARAM_ERROR;
1819 * According to acl.c it is safe to call test_filter() with
1822 rc = test_filter( op, e, f );
1824 case LDAP_COMPARE_TRUE:
1827 case LDAP_COMPARE_FALSE:
1829 case SLAPD_COMPARE_UNDEFINED:
1832 case LDAP_PROTOCOL_ERROR:
1833 /* filter type unknown: spec says return -1 */
1842 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
1844 return slapi_filter_test( NULL, e, f, 0 );
1848 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
1850 switch ( f->f_choice ) {
1851 case LDAP_FILTER_AND:
1852 case LDAP_FILTER_NOT:
1853 case LDAP_FILTER_OR: {
1857 * FIXME: altering f; should we use a temporary?
1859 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1860 rc = slapi_filter_apply( f, fn, arg, error_code );
1864 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
1870 case LDAP_FILTER_EQUALITY:
1871 case LDAP_FILTER_SUBSTRINGS:
1872 case LDAP_FILTER_GE:
1873 case LDAP_FILTER_LE:
1874 case LDAP_FILTER_PRESENT:
1875 case LDAP_FILTER_APPROX:
1876 case LDAP_FILTER_EXT:
1877 *error_code = fn( f, arg );
1880 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1883 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
1884 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
1893 struct berval **vals,
1897 * FIXME: what's the point?
1902 #define MAX_HOSTNAME 512
1905 slapi_get_hostname( void )
1908 static int been_here = 0;
1909 static char *static_hn = NULL;
1911 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1913 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1914 if ( static_hn == NULL) {
1915 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
1916 "Cannot allocate memory for hostname\n" );
1918 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1923 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1924 slapi_log_error( SLAPI_LOG_FATAL,
1926 "can't get hostname\n" );
1927 slapi_ch_free( (void **)&static_hn );
1929 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1938 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1940 hn = ch_strdup( static_hn );
1946 * FIXME: this should go in an appropriate header ...
1948 extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist );
1957 int rc = LDAP_SUCCESS;
1960 va_start( arglist, fmt );
1961 rc = slapi_int_log_error( severity, subsystem, fmt, arglist );
1969 slapi_timer_current_time( void )
1971 static int first_time = 1;
1972 #if !defined (_WIN32)
1976 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1979 gettimeofday( &base_time, NULL );
1981 gettimeofday( &now, NULL );
1982 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1983 (now.tv_usec - base_time.tv_usec);
1984 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1990 return (slap_get_time() - starttime) * 1000000;
1997 performance_counter_present = QueryPerformanceCounter( &base_time );
1998 QueryPerformanceFrequency( &performance_freq );
2001 if ( !performance_counter_present )
2004 QueryPerformanceCounter( &now );
2005 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2013 slapi_timer_get_time( char *label )
2015 unsigned long start = slapi_timer_current_time();
2016 printf("%10ld %10d usec %s\n", start, 0, label);
2024 slapi_timer_elapsed_time(
2026 unsigned long start )
2028 unsigned long stop = slapi_timer_current_time();
2029 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2033 slapi_free_search_results_internal( Slapi_PBlock *pb )
2035 Slapi_Entry **entries;
2036 int k = 0, nEnt = 0;
2038 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2039 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2040 if ( nEnt == 0 || entries == NULL ) {
2044 for ( k = 0; k < nEnt; k++ ) {
2045 slapi_entry_free( entries[k] );
2049 slapi_ch_free( (void **)&entries );
2052 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2055 return LDAP_PARAM_ERROR;
2057 if ( pb->pb_conn == NULL )
2058 return LDAP_PARAM_ERROR;
2061 *isSSL = pb->pb_conn->c_is_tls;
2066 return LDAP_SUCCESS;
2070 * DS 5.x compatability API follow
2073 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2078 return LDAP_PARAM_ERROR;
2080 at = attr->a_desc->ad_type;
2082 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2084 if ( is_at_single_value( at ) )
2085 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2086 if ( is_at_operational( at ) )
2087 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2088 if ( is_at_obsolete( at ) )
2089 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2090 if ( is_at_collective( at ) )
2091 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2092 if ( is_at_no_user_mod( at ) )
2093 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2095 return LDAP_SUCCESS;
2098 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2100 unsigned long flags;
2102 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2104 return (flags & flag) ? 1 : 0;
2107 Slapi_Attr *slapi_attr_new( void )
2111 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2116 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2119 AttributeDescription *ad = NULL;
2121 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2134 void slapi_attr_free( Slapi_Attr **a )
2140 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2142 return attr_dup( (Slapi_Attr *)attr );
2145 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2148 struct berval *nvalp;
2150 AttributeDescription *desc = a->a_desc;
2152 if ( desc->ad_type->sat_equality &&
2153 desc->ad_type->sat_equality->smr_normalize ) {
2154 rc = (*desc->ad_type->sat_equality->smr_normalize)(
2155 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2156 desc->ad_type->sat_syntax,
2157 desc->ad_type->sat_equality,
2158 (Slapi_Value *)v, &nval, NULL );
2159 if ( rc != LDAP_SUCCESS ) {
2167 rc = value_add_one( &a->a_vals, (Slapi_Value *)v );
2168 if ( rc == 0 && nvalp != NULL ) {
2169 rc = value_add_one( &a->a_nvals, nvalp );
2171 a->a_nvals = a->a_vals;
2174 if ( nvalp != NULL ) {
2175 slapi_ch_free_string( &nval.bv_val );
2181 int slapi_attr_type2plugin( const char *type, void **pi )
2188 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2190 if ( attr == NULL ) {
2191 return LDAP_PARAM_ERROR;
2194 *type = attr->a_desc->ad_cname.bv_val;
2196 return LDAP_SUCCESS;
2199 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2201 if ( attr == NULL ) {
2202 return LDAP_PARAM_ERROR;
2204 *oidp = attr->a_desc->ad_type->sat_oid;
2206 return LDAP_SUCCESS;
2209 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2216 mr = a->a_desc->ad_type->sat_equality;
2217 rc = value_match( &ret, a->a_desc, mr,
2218 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2219 (struct berval *)v1, (void *)v2, &text );
2220 if ( rc != LDAP_SUCCESS )
2223 return ( ret == 0 ) ? 0 : -1;
2226 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2235 if ( a ->a_vals == NULL ) {
2238 mr = a->a_desc->ad_type->sat_equality;
2239 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2240 rc = value_match( &ret, a->a_desc, mr,
2241 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2242 if ( rc != LDAP_SUCCESS ) {
2252 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2254 AttributeDescription *a1 = NULL;
2255 AttributeDescription *a2 = NULL;
2259 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2263 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2267 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2268 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2269 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2272 case SLAPI_TYPE_CMP_EXACT:
2273 ret = ad_cmp( a1, a2 );
2275 case SLAPI_TYPE_CMP_BASE:
2276 ret = ad_base_cmp( a1, a2 );
2278 case SLAPI_TYPE_CMP_SUBTYPE:
2279 ret = is_ad_subtype( a2, a2 );
2289 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2291 return ( slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT ) == 0 );
2294 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2296 return slapi_valueset_first_value( &a->a_vals, v );
2299 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2301 return slapi_valueset_next_value( &a->a_vals, hint, v );
2304 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2306 *numValues = slapi_valueset_count( &a->a_vals );
2311 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2313 *vs = &((Slapi_Attr *)a)->a_vals;
2318 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2320 return slapi_attr_get_values( a, vals );
2323 char *slapi_attr_syntax_normalize( const char *s )
2325 AttributeDescription *ad = NULL;
2328 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2332 return ad->ad_cname.bv_val;
2335 Slapi_Value *slapi_value_new( void )
2339 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2344 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2346 return ber_dupbv( NULL, (struct berval *)bval );
2349 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2351 return slapi_value_new_berval( v );
2354 Slapi_Value *slapi_value_new_string(const char *s)
2358 bv.bv_val = (char *)s;
2359 bv.bv_len = strlen( s );
2361 return slapi_value_new_berval( &bv );
2364 Slapi_Value *slapi_value_init(Slapi_Value *val)
2372 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2374 return ber_dupbv( v, bval );
2377 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2379 v->bv_val = slapi_ch_strdup( s );
2380 v->bv_len = strlen( s );
2385 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2387 return slapi_value_new_value( v );
2390 void slapi_value_free(Slapi_Value **value)
2392 if ( value == NULL ) {
2396 if ( (*value) != NULL ) {
2397 slapi_ch_free( (void **)&(*value)->bv_val );
2398 slapi_ch_free( (void **)value );
2402 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2407 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2409 if ( value == NULL ) {
2412 if ( value->bv_val != NULL ) {
2413 slapi_ch_free( (void **)&value->bv_val );
2415 slapi_value_init_berval( value, (struct berval *)bval );
2420 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2422 if ( value == NULL ) {
2425 return slapi_value_set_berval( value, vfrom );
2428 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2430 if ( value == NULL ) {
2433 if ( value->bv_val != NULL ) {
2434 slapi_ch_free( (void **)&value->bv_val );
2436 value->bv_val = slapi_ch_malloc( len );
2437 value->bv_len = len;
2438 AC_MEMCPY( value->bv_val, val, len );
2443 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2445 if ( value == NULL ) {
2448 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2452 int slapi_value_set_int(Slapi_Value *value, int intVal)
2456 snprintf( buf, sizeof( buf ), "%d", intVal );
2458 return slapi_value_set_string( value, buf );
2461 const char *slapi_value_get_string(const Slapi_Value *value)
2463 if ( value == NULL ) return NULL;
2464 if ( value->bv_val == NULL ) return NULL;
2465 if ( !checkBVString( value ) ) return NULL;
2467 return value->bv_val;
2470 int slapi_value_get_int(const Slapi_Value *value)
2472 if ( value == NULL ) return 0;
2473 if ( value->bv_val == NULL ) return 0;
2474 if ( !checkBVString( value ) ) return 0;
2476 return (int)strtol( value->bv_val, NULL, 10 );
2479 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2481 if ( value == NULL ) return 0;
2482 if ( value->bv_val == NULL ) return 0;
2483 if ( !checkBVString( value ) ) return 0;
2485 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2488 long slapi_value_get_long(const Slapi_Value *value)
2490 if ( value == NULL ) return 0;
2491 if ( value->bv_val == NULL ) return 0;
2492 if ( !checkBVString( value ) ) return 0;
2494 return strtol( value->bv_val, NULL, 10 );
2497 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2499 if ( value == NULL ) return 0;
2500 if ( value->bv_val == NULL ) return 0;
2501 if ( !checkBVString( value ) ) return 0;
2503 return strtoul( value->bv_val, NULL, 10 );
2506 size_t slapi_value_get_length(const Slapi_Value *value)
2508 if ( value == NULL )
2511 return (size_t) value->bv_len;
2514 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2516 return slapi_attr_value_cmp( a, v1, v2 );
2519 /* A ValueSet is a container for a BerVarray. */
2520 Slapi_ValueSet *slapi_valueset_new( void )
2524 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2530 void slapi_valueset_free(Slapi_ValueSet *vs)
2535 ber_bvarray_free( vp );
2536 slapi_ch_free( (void **)&vp );
2542 void slapi_valueset_init(Slapi_ValueSet *vs)
2544 if ( vs != NULL && *vs == NULL ) {
2545 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2546 (*vs)->bv_val = NULL;
2551 void slapi_valueset_done(Slapi_ValueSet *vs)
2558 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
2560 slapi_ch_free( (void **)&vp->bv_val );
2562 /* but don't free *vs or vs */
2565 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2569 ber_dupbv( &bv, (Slapi_Value *)addval );
2570 ber_bvarray_add( vs, &bv );
2573 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2575 return slapi_valueset_next_value( vs, 0, v );
2578 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2588 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2598 int slapi_valueset_count( const Slapi_ValueSet *vs )
2608 for ( i = 0; vp[i].bv_val != NULL; i++ )
2615 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2619 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2620 slapi_valueset_add_value( vs1, vp );
2624 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
2625 struct berval *val, int access )
2628 slap_access_t slap_access;
2629 AttributeDescription *ad = NULL;
2632 rc = slap_str2ad( attr, &ad, &text );
2633 if ( rc != LDAP_SUCCESS ) {
2638 * Whilst the SLAPI access types are arranged as a bitmask, the
2639 * documentation indicates that they are to be used separately.
2641 switch ( access & SLAPI_ACL_ALL ) {
2642 case SLAPI_ACL_COMPARE:
2643 slap_access = ACL_COMPARE;
2645 case SLAPI_ACL_SEARCH:
2646 slap_access = ACL_SEARCH;
2648 case SLAPI_ACL_READ:
2649 slap_access = ACL_READ;
2651 case SLAPI_ACL_WRITE:
2652 slap_access = ACL_WRITE;
2654 case SLAPI_ACL_DELETE:
2655 slap_access = ACL_WDEL;
2658 slap_access = ACL_WADD;
2660 case SLAPI_ACL_SELF: /* not documented */
2661 case SLAPI_ACL_PROXY: /* not documented */
2663 return LDAP_INSUFFICIENT_ACCESS;
2667 assert( pb->pb_op != NULL );
2669 if ( access_allowed( pb->pb_op, e, ad, val, slap_access, NULL ) ) {
2670 return LDAP_SUCCESS;
2673 return LDAP_INSUFFICIENT_ACCESS;
2676 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
2678 int rc = LDAP_SUCCESS;
2681 if ( pb == NULL || pb->pb_op == NULL )
2682 return LDAP_PARAM_ERROR;
2684 ml = slapi_int_ldapmods2modifications( mods );
2689 if ( rc == LDAP_SUCCESS ) {
2690 rc = acl_check_modlist( pb->pb_op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2693 slap_mods_free( ml, 1 );
2699 * Synthesise an LDAPMod array from a Modifications list to pass
2700 * to SLAPI. This synthesis is destructive and as such the
2701 * Modifications list may not be used after calling this
2704 * This function must also be called before slap_mods_check().
2706 LDAPMod **slapi_int_modifications2ldapmods( Modifications **pmodlist )
2708 Modifications *ml, *modlist;
2709 LDAPMod **mods, *modp;
2712 modlist = *pmodlist;
2714 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
2717 mods = (LDAPMod **)slapi_ch_malloc( (i + 1) * sizeof(LDAPMod *) );
2719 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
2720 mods[i] = (LDAPMod *)slapi_ch_malloc( sizeof(LDAPMod) );
2722 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
2723 if ( BER_BVISNULL( &ml->sml_type ) ) {
2724 /* may happen for internally generated mods */
2725 assert( ml->sml_desc != NULL );
2726 modp->mod_type = slapi_ch_strdup( ml->sml_desc->ad_cname.bv_val );
2728 modp->mod_type = slapi_ch_strdup( ml->sml_type.bv_val );
2729 BER_BVZERO( &ml->sml_type );
2732 if ( ml->sml_values != NULL ) {
2733 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
2735 modp->mod_bvalues = (struct berval **)slapi_ch_malloc( (j + 1) *
2736 sizeof(struct berval *) );
2737 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
2738 modp->mod_bvalues[j] = (struct berval *)slapi_ch_malloc(
2739 sizeof(struct berval) );
2740 /* Take ownership of original values. */
2741 modp->mod_bvalues[j]->bv_len = ml->sml_values[j].bv_len;
2742 modp->mod_bvalues[j]->bv_val = ml->sml_values[j].bv_val;
2743 ml->sml_values[j].bv_len = 0;
2744 ml->sml_values[j].bv_val = NULL;
2746 modp->mod_bvalues[j] = NULL;
2747 slapi_ch_free( (void **)&ml->sml_values );
2749 modp->mod_bvalues = NULL;
2756 slap_mods_free( modlist, 1 );
2763 * Convert a potentially modified array of LDAPMods back to a
2764 * Modification list. Unfortunately the values need to be
2765 * duplicated because slap_mods_check() will try to free them
2766 * before prettying (and we can't easily get out of calling
2767 * slap_mods_check() because we need normalized values).
2769 Modifications *slapi_int_ldapmods2modifications ( LDAPMod **mods )
2771 Modifications *modlist = NULL, **modtail;
2773 char textbuf[SLAP_TEXT_BUFLEN];
2776 if ( mods == NULL ) {
2782 for ( modp = mods; *modp != NULL; modp++ ) {
2784 LDAPMod *lmod = *modp;
2787 AttributeDescription *ad = NULL;
2789 if ( slap_str2ad( lmod->mod_type, &ad, &text ) != LDAP_SUCCESS ) {
2793 mod = (Modifications *) slapi_ch_malloc( sizeof(Modifications) );
2794 mod->sml_op = lmod->mod_op & ~(LDAP_MOD_BVALUES);
2796 mod->sml_type = ad->ad_cname;
2798 mod->sml_next = NULL;
2801 if ( lmod->mod_op & LDAP_MOD_BVALUES ) {
2802 if ( lmod->mod_bvalues != NULL ) {
2803 while ( lmod->mod_bvalues[i] != NULL )
2807 if ( lmod->mod_values != NULL ) {
2808 while ( lmod->mod_values[i] != NULL )
2814 mod->sml_values = NULL;
2816 mod->sml_values = (BerVarray) slapi_ch_malloc( (i + 1) * sizeof(struct berval) );
2818 /* NB: This implicitly trusts a plugin to return valid modifications. */
2819 if ( lmod->mod_op & LDAP_MOD_BVALUES ) {
2820 for ( i = 0; lmod->mod_bvalues[i] != NULL; i++ ) {
2821 ber_dupbv( &mod->sml_values[i], lmod->mod_bvalues[i] );
2824 for ( i = 0; lmod->mod_values[i] != NULL; i++ ) {
2825 mod->sml_values[i].bv_val = slapi_ch_strdup( lmod->mod_values[i] );
2826 mod->sml_values[i].bv_len = strlen( lmod->mod_values[i] );
2829 mod->sml_values[i].bv_val = NULL;
2830 mod->sml_values[i].bv_len = 0;
2832 mod->sml_nvalues = NULL;
2835 modtail = &mod->sml_next;
2838 if ( slap_mods_check( modlist, &text, textbuf, sizeof( textbuf ), NULL ) != LDAP_SUCCESS ) {
2839 slap_mods_free( modlist, 1 );
2847 * Sun ONE DS 5.x computed attribute support. Computed attributes
2848 * allow for dynamically generated operational attributes, a very
2849 * useful thing indeed.
2853 * For some reason Sun don't use the normal plugin mechanism
2854 * registration path to register an "evaluator" function (an
2855 * "evaluator" is responsible for adding computed attributes;
2856 * the nomenclature is somewhat confusing).
2858 * As such slapi_compute_add_evaluator() registers the
2859 * function directly.
2861 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
2863 Slapi_PBlock *pPlugin = NULL;
2865 int type = SLAPI_PLUGIN_OBJECT;
2867 pPlugin = slapi_pblock_new();
2868 if ( pPlugin == NULL ) {
2869 rc = LDAP_NO_MEMORY;
2873 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
2874 if ( rc != LDAP_SUCCESS ) {
2878 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
2879 if ( rc != LDAP_SUCCESS ) {
2883 rc = slapi_int_register_plugin( frontendDB, pPlugin );
2890 if ( rc != LDAP_SUCCESS ) {
2891 if ( pPlugin != NULL ) {
2892 slapi_pblock_destroy( pPlugin );
2901 * See notes above regarding slapi_compute_add_evaluator().
2903 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
2905 Slapi_PBlock *pPlugin = NULL;
2907 int type = SLAPI_PLUGIN_OBJECT;
2909 pPlugin = slapi_pblock_new();
2910 if ( pPlugin == NULL ) {
2911 rc = LDAP_NO_MEMORY;
2915 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
2916 if ( rc != LDAP_SUCCESS ) {
2920 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
2921 if ( rc != LDAP_SUCCESS ) {
2925 rc = slapi_int_register_plugin( frontendDB, pPlugin );
2932 if ( rc != LDAP_SUCCESS ) {
2933 if ( pPlugin != NULL ) {
2934 slapi_pblock_destroy( pPlugin );
2943 * Call compute evaluators
2945 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
2948 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
2950 rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
2951 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
2952 /* Nothing to do; front-end should ignore. */
2956 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
2958 * -1: no attribute matched requested type
2959 * 0: one attribute matched
2960 * >0: error happened
2962 rc = (*pGetPlugin)( c, type, e, outputfn );
2968 slapi_ch_free( (void **)&tmpPlugin );
2974 compute_rewrite_search_filter( Slapi_PBlock *pb )
2976 if ( pb == NULL || pb->pb_op == NULL )
2977 return LDAP_PARAM_ERROR;
2979 return slapi_int_call_plugins( pb->pb_op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
2983 * New API to provide the plugin with access to the search
2984 * pblock. Have informed Sun DS team.
2987 slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
2992 if ( c->cac_pb == NULL )
3000 Slapi_Mutex *slapi_new_mutex( void )
3004 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3005 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3006 slapi_ch_free( (void **)&m );
3013 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3015 if ( mutex != NULL ) {
3016 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3017 slapi_ch_free( (void **)&mutex);
3021 void slapi_lock_mutex( Slapi_Mutex *mutex )
3023 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3026 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3028 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3031 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3035 if ( mutex == NULL ) {
3039 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3040 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3041 slapi_ch_free( (void **)&cv );
3045 cv->mutex = mutex->mutex;
3050 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3052 if ( cvar != NULL ) {
3053 ldap_pvt_thread_cond_destroy( &cvar->cond );
3054 slapi_ch_free( (void **)&cvar );
3058 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3060 if ( cvar == NULL ) {
3064 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3067 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3069 if ( cvar == NULL ) {
3074 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3077 return ldap_pvt_thread_cond_signal( &cvar->cond );
3080 int slapi_int_access_allowed( Operation *op,
3082 AttributeDescription *desc,
3084 slap_access_t access,
3085 AccessControlState *state )
3087 int rc, slap_access = 0;
3088 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3091 pb = SLAPI_OPERATION_PBLOCK( op );
3093 /* internal operation */
3099 slap_access |= SLAPI_ACL_COMPARE;
3102 slap_access |= SLAPI_ACL_SEARCH;
3105 slap_access |= SLAPI_ACL_READ;
3108 slap_access |= SLAPI_ACL_WRITE;
3111 slap_access |= SLAPI_ACL_DELETE;
3114 slap_access |= SLAPI_ACL_ADD;
3120 rc = slapi_int_get_plugins( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
3121 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3122 /* nothing to do; allowed access */
3126 rc = 1; /* default allow policy */
3128 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3133 rc = (*pGetPlugin)( pb, entry, desc->ad_cname.bv_val,
3134 val, slap_access, (void *)state );
3140 slapi_ch_free( (void **)&tmpPlugin );
3146 * There is no documentation for this.
3148 int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
3160 rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
3161 if ( rc != LDAP_SUCCESS ) {
3165 if ( lrdn[1] != NULL ) {
3166 return -1; /* not single valued */
3171 *type = slapi_ch_strdup( ava->la_attr.bv_val );
3172 ber_dupbv( bv, &ava->la_value );
3179 char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
3181 struct berval new_dn, parent_dn, newrdn;
3183 new_dn.bv_val = NULL;
3185 parent_dn.bv_val = (char *)dn;
3186 parent_dn.bv_len = strlen( dn );
3188 newrdn.bv_val = (char *)rdn;
3189 newrdn.bv_len = strlen( rdn );
3191 build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
3193 return new_dn.bv_val;
3196 int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
3200 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
3201 size_t textlen = sizeof textbuf;
3202 int rc = LDAP_SUCCESS;
3204 PBLOCK_ASSERT_OP( pb, 0 );
3206 be_orig = pb->pb_op->o_bd;
3208 pb->pb_op->o_bd = select_backend( &e->e_nname, 0, 0 );
3209 if ( pb->pb_op->o_bd != NULL ) {
3210 rc = entry_schema_check( pb->pb_op, e, NULL, 0,
3211 &text, textbuf, textlen );
3213 pb->pb_op->o_bd = be_orig;
3215 return ( rc == LDAP_SUCCESS ) ? 0 : 1;
3218 int slapi_entry_rdn_values_present( const Slapi_Entry *e )
3222 int i = 0, match = 0;
3224 rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
3225 if ( rc != LDAP_SUCCESS ) {
3229 if ( dn[0] != NULL ) {
3230 LDAPRDN rdn = dn[0];
3232 for ( i = 0; rdn[i] != NULL; i++ ) {
3233 LDAPAVA *ava = &rdn[0][i];
3234 Slapi_Attr *a = NULL;
3236 if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
3237 slapi_attr_value_find( a, &ava->la_value ) == 0 )
3244 return ( i == match );
3247 int slapi_entry_add_rdn_values( Slapi_Entry *e )
3252 rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
3253 if ( rc != LDAP_SUCCESS ) {
3257 if ( dn[0] != NULL ) {
3258 LDAPRDN rdn = dn[0];
3259 struct berval *vals[2];
3261 for ( i = 0; rdn[i] != NULL; i++ ) {
3262 LDAPAVA *ava = &rdn[0][i];
3263 Slapi_Attr *a = NULL;
3265 if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
3266 slapi_attr_value_find( a, &ava->la_value ) == 0 )
3269 vals[0] = &ava->la_value;
3272 slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
3278 return LDAP_SUCCESS;
3281 const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
3285 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
3286 if ( attr == NULL ) {
3290 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
3291 return slapi_value_get_string( &attr->a_vals[0] );
3297 void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
3301 attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
3303 bv.bv_val = uniqueid;
3304 bv.bv_len = strlen( uniqueid );
3305 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
3308 LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
3315 size = sizeof("ldap:///");
3318 size += strlen( ldaphost );
3319 if ( ldapport != 0 )
3322 url = slapi_ch_malloc( size );
3324 if ( ldapport != 0 ) {
3325 sprintf( url, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
3327 sprintf( url, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
3330 rc = ldap_initialize( &ld, url );
3332 slapi_ch_free_string( &url );
3334 return ( rc == LDAP_SUCCESS ) ? ld : NULL;
3337 void slapi_ldap_unbind( LDAP *ld )
3339 ldap_unbind_ext_s( ld, NULL, NULL );
3342 int slapi_x_backend_get_flags( const Slapi_Backend *be, unsigned long *flags )
3345 return LDAP_PARAM_ERROR;
3347 *flags = SLAP_DBFLAGS(be);
3349 return LDAP_SUCCESS;
3353 slapi_int_count_controls( LDAPControl **ctrls )
3357 if ( ctrls == NULL )
3360 for ( i = 0; ctrls[i] != NULL; i++ )
3367 slapi_op_abandoned( Slapi_PBlock *pb )
3369 if ( pb->pb_op == NULL )
3372 return ( pb->pb_op->o_abandon );
3376 slapi_op_type_to_string(unsigned long type)
3381 case SLAPI_OPERATION_BIND:
3384 case SLAPI_OPERATION_UNBIND:
3387 case SLAPI_OPERATION_SEARCH:
3390 case SLAPI_OPERATION_MODIFY:
3393 case SLAPI_OPERATION_ADD:
3396 case SLAPI_OPERATION_DELETE:
3399 case SLAPI_OPERATION_MODDN:
3402 case SLAPI_OPERATION_COMPARE:
3405 case SLAPI_OPERATION_ABANDON:
3408 case SLAPI_OPERATION_EXTENDED:
3412 str = "unknown operation type";
3419 slapi_op_get_type(Slapi_Operation * op)
3423 switch ( op->o_tag ) {
3425 type = SLAPI_OPERATION_BIND;
3427 case LDAP_REQ_UNBIND:
3428 type = SLAPI_OPERATION_UNBIND;
3430 case LDAP_REQ_SEARCH:
3431 type = SLAPI_OPERATION_SEARCH;
3433 case LDAP_REQ_MODIFY:
3434 type = SLAPI_OPERATION_MODIFY;
3437 type = SLAPI_OPERATION_ADD;
3439 case LDAP_REQ_DELETE:
3440 type = SLAPI_OPERATION_DELETE;
3442 case LDAP_REQ_MODRDN:
3443 type = SLAPI_OPERATION_MODDN;
3445 case LDAP_REQ_COMPARE:
3446 type = SLAPI_OPERATION_COMPARE;
3448 case LDAP_REQ_ABANDON:
3449 type = SLAPI_OPERATION_ABANDON;
3451 case LDAP_REQ_EXTENDED:
3452 type = SLAPI_OPERATION_EXTENDED;
3455 type = SLAPI_OPERATION_NONE;
3461 void slapi_be_set_readonly( Slapi_Backend *be, int readonly )
3467 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
3469 be->be_restrictops &= ~(SLAP_RESTRICT_OP_WRITES);
3472 int slapi_be_get_readonly( Slapi_Backend *be )
3477 return ( (be->be_restrictops & SLAP_RESTRICT_OP_WRITES) == SLAP_RESTRICT_OP_WRITES );
3480 const char *slapi_x_be_get_updatedn( Slapi_Backend *be )
3485 return be->be_update_ndn.bv_val;
3488 Slapi_Backend *slapi_be_select( const Slapi_DN *sdn )
3492 slapi_sdn_get_ndn( sdn );
3494 be = select_backend( (struct berval *)&sdn->ndn, 0, 0 );
3501 slapi_operation_set_flag(Slapi_Operation *op, unsigned long flag)
3506 slapi_operation_clear_flag(Slapi_Operation *op, unsigned long flag)
3511 slapi_operation_is_flag_set(Slapi_Operation *op, unsigned long flag)
3516 #endif /* LDAP_SLAPI */