2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2002-2004 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>
37 * server start time (should we use a struct timeval also in slapd?
39 static struct timeval base_time;
40 ldap_pvt_thread_mutex_t slapi_hn_mutex;
41 ldap_pvt_thread_mutex_t slapi_time_mutex;
44 ldap_pvt_thread_mutex_t mutex;
47 struct slapi_condvar {
48 ldap_pvt_thread_cond_t cond;
49 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' )
66 #endif /* LDAP_SLAPI */
69 * This function converts an array of pointers to berval objects to
70 * an array of berval objects.
75 struct berval **bvptr,
78 int rc = LDAP_SUCCESS;
82 if ( bvptr == NULL || *bvptr == NULL ) {
86 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
90 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
91 if ( tmpberval == NULL ) {
92 return LDAP_NO_MEMORY;
95 for ( i = 0; bvptr[i] != NULL; i++ ) {
96 tmpberval[i].bv_val = bvptr[i]->bv_val;
97 tmpberval[i].bv_len = bvptr[i]->bv_len;
99 tmpberval[i].bv_val = NULL;
100 tmpberval[i].bv_len = 0;
102 if ( rc == LDAP_SUCCESS ) {
115 Slapi_Entry *e = NULL;
118 pTmpS = slapi_ch_strdup( s );
119 if ( pTmpS != NULL ) {
120 e = str2entry( pTmpS );
121 slapi_ch_free( (void **)&pTmpS );
127 #endif /* LDAP_SLAPI */
138 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
139 ret = entry2str( e, len );
140 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
143 #else /* LDAP_SLAPI */
145 #endif /* LDAP_SLAPI */
149 slapi_entry_get_dn( Slapi_Entry *e )
152 return e->e_name.bv_val;
153 #else /* LDAP_SLAPI */
155 #endif /* LDAP_SLAPI */
159 slapi_x_entry_get_id( Slapi_Entry *e )
165 #endif /* LDAP_SLAPI */
174 struct berval dn = BER_BVNULL;
177 dn.bv_len = strlen( ldn );
179 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname, NULL );
180 #endif /* LDAP_SLAPI */
184 slapi_entry_dup( Slapi_Entry *e )
187 return entry_dup( e );
188 #else /* LDAP_SLAPI */
190 #endif /* LDAP_SLAPI */
194 slapi_entry_attr_delete(
199 AttributeDescription *ad = NULL;
202 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
203 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
206 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
207 return 0; /* attribute is deleted */
209 return -1; /* something went wrong */
211 #else /* LDAP_SLAPI */
213 #endif /* LDAP_SLAPI */
217 slapi_entry_alloc( void )
220 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
221 #else /* LDAP_SLAPI */
223 #endif /* LDAP_SLAPI */
227 slapi_entry_free( Slapi_Entry *e )
231 #endif /* LDAP_SLAPI */
235 slapi_entry_attr_merge(
238 struct berval **vals )
241 AttributeDescription *ad = NULL;
246 rc = bvptr2obj( vals, &bv );
247 if ( rc != LDAP_SUCCESS ) {
251 rc = slap_str2ad( type, &ad, &text );
252 if ( rc != LDAP_SUCCESS ) {
256 rc = attr_merge_normalize_one( e, ad, bv, NULL );
260 #else /* LDAP_SLAPI */
262 #endif /* LDAP_SLAPI */
266 slapi_entry_attr_find(
272 AttributeDescription *ad = NULL;
276 rc = slap_str2ad( type, &ad, &text );
277 if ( rc != LDAP_SUCCESS ) {
281 *attr = attr_find( e->e_attrs, ad );
282 if ( *attr == NULL ) {
287 #else /* LDAP_SLAPI */
289 #endif /* LDAP_SLAPI */
293 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
296 AttributeDescription *ad = NULL;
301 rc = slap_str2ad( type, &ad, &text );
302 if ( rc != LDAP_SUCCESS ) {
306 attr = attr_find( e->e_attrs, ad );
307 if ( attr == NULL ) {
311 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
314 p = slapi_value_get_string( &attr->a_vals[0] );
316 return slapi_ch_strdup( (char *)p );
327 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
330 AttributeDescription *ad = NULL;
335 rc = slap_str2ad( type, &ad, &text );
336 if ( rc != LDAP_SUCCESS ) {
340 attr = attr_find( e->e_attrs, ad );
341 if ( attr == NULL ) {
345 return slapi_value_get_int( attr->a_vals );
352 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
355 AttributeDescription *ad = NULL;
360 rc = slap_str2ad( type, &ad, &text );
361 if ( rc != LDAP_SUCCESS ) {
365 attr = attr_find( e->e_attrs, ad );
366 if ( attr == NULL ) {
370 return slapi_value_get_long( attr->a_vals );
377 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
380 AttributeDescription *ad = NULL;
385 rc = slap_str2ad( type, &ad, &text );
386 if ( rc != LDAP_SUCCESS ) {
390 attr = attr_find( e->e_attrs, ad );
391 if ( attr == NULL ) {
395 return slapi_value_get_uint( attr->a_vals );
402 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
405 AttributeDescription *ad = NULL;
410 rc = slap_str2ad( type, &ad, &text );
411 if ( rc != LDAP_SUCCESS ) {
415 attr = attr_find( e->e_attrs, ad );
416 if ( attr == NULL ) {
420 return slapi_value_get_ulong( attr->a_vals );
427 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
431 AttributeDescription *ad = NULL;
436 rc = slap_str2ad( type, &ad, &text );
437 if ( rc != LDAP_SUCCESS ) {
441 attr = attr_find( e->e_attrs, ad );
442 if ( attr == NULL ) {
446 bv.bv_val = (char *)value;
447 bv.bv_len = strlen( value );
449 return ( slapi_attr_value_find( attr, &bv ) != -1 );
456 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
459 AttributeDescription *ad = NULL;
464 rc = slap_str2ad( type, &ad, &text );
465 if ( rc != LDAP_SUCCESS ) {
469 attr_delete ( &e->e_attrs, ad );
470 if ( value != NULL ) {
471 bv.bv_val = (char *)value;
472 bv.bv_len = strlen(value);
473 attr_merge_normalize_one( e, ad, &bv, NULL );
475 #endif /* LDAP_SLAPI */
479 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
484 snprintf( buf, sizeof( buf ), "%d", l );
485 slapi_entry_attr_set_charptr( e, type, buf );
486 #endif /* LDAP_SLAPI */
490 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
495 snprintf( buf, sizeof( buf ), "%u", l );
496 slapi_entry_attr_set_charptr( e, type, buf );
497 #endif /* LDAP_SLAPI */
501 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
506 snprintf( buf, sizeof( buf ), "%ld", l );
507 slapi_entry_attr_set_charptr( e, type, buf );
508 #endif /* LDAP_SLAPI */
512 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
517 snprintf( buf, sizeof( buf ), "%lu", l );
518 slapi_entry_attr_set_charptr( e, type, buf );
519 #endif /* LDAP_SLAPI */
523 slapi_is_rootdse( const char *dn )
526 return ( dn == NULL || dn[0] == '\0' );
533 slapi_entry_has_children(const Slapi_Entry *e)
538 int hasSubordinates = 0;
540 pConn = slapi_int_init_connection( NULL, LDAP_REQ_SEARCH );
541 if ( pConn == NULL ) {
545 op = (Operation *)pConn->c_pending_ops.stqh_first;
546 op->o_bd = select_backend( (struct berval *)&e->e_nname, 0, 0 );
547 if ( op->o_bd == NULL ) {
551 op->o_bd->be_has_subordinates( op, (Entry *)e, &hasSubordinates );
553 slapi_int_connection_destroy( &pConn );
555 return ( hasSubordinates == LDAP_COMPARE_TRUE );
562 * Return approximate size of the entry rounded to the nearest
563 * 1K. Only the size of the attribute values are counted in the
564 * Sun implementation.
566 * http://docs.sun.com/source/816-6701-10/funcref.html#1017388
568 size_t slapi_entry_size(Slapi_Entry *e)
575 for ( size = 0, a = e->e_attrs; a != NULL; a = a->a_next ) {
576 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
577 size += a->a_vals[i].bv_len + 1;
582 size -= (size % 1024);
587 #endif /* LDAP_SLAPI */
591 * Add values to entry.
594 * LDAP_SUCCESS Values added to entry
595 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
596 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
599 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
605 char textbuf[SLAP_TEXT_BUFLEN];
607 mod.sm_op = LDAP_MOD_ADD;
609 mod.sm_type.bv_val = (char *)type;
610 mod.sm_type.bv_len = strlen( type );
612 rc = slap_str2ad( type, &mod.sm_desc, &text );
613 if ( rc != LDAP_SUCCESS ) {
617 if ( vals == NULL ) {
618 /* Apparently vals can be NULL
619 * FIXME: sm_values = NULL ? */
620 mod.sm_values = (BerVarray)ch_malloc( sizeof(struct berval) );
621 mod.sm_values->bv_val = NULL;
624 rc = bvptr2obj( vals, &mod.sm_values );
625 if ( rc != LDAP_SUCCESS ) {
626 return LDAP_CONSTRAINT_VIOLATION;
629 mod.sm_nvalues = NULL;
631 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
633 ch_free( mod.sm_values );
635 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
638 #endif /* LDAP_SLAPI */
642 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
645 return slapi_entry_add_values( e, type, vals );
648 #endif /* LDAP_SLAPI */
652 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
655 AttributeDescription *ad = NULL;
659 rc = slap_str2ad( type, &ad, &text );
660 if ( rc != LDAP_SUCCESS ) {
664 return attr_merge_normalize( e, ad, *vs, NULL );
667 #endif /* LDAP_SLAPI */
671 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
677 char textbuf[SLAP_TEXT_BUFLEN];
679 mod.sm_op = LDAP_MOD_DELETE;
681 mod.sm_type.bv_val = (char *)type;
682 mod.sm_type.bv_len = strlen( type );
684 if ( vals == NULL ) {
685 /* If vals is NULL, this is a NOOP. */
689 rc = slap_str2ad( type, &mod.sm_desc, &text );
690 if ( rc != LDAP_SUCCESS ) {
694 if ( vals[0] == NULL ) {
695 /* SLAPI doco says LDAP_OPERATIONS_ERROR but LDAP_OTHER is better */
696 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
699 rc = bvptr2obj( vals, &mod.sm_values );
700 if ( rc != LDAP_SUCCESS ) {
701 return LDAP_CONSTRAINT_VIOLATION;
703 mod.sm_nvalues = NULL;
705 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
707 ch_free( mod.sm_values );
712 #endif /* LDAP_SLAPI */
716 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
719 return slapi_entry_delete_values( e, type, vals );
722 #endif /* LDAP_SLAPI */
726 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
729 return slapi_entry_attr_merge( e, (char *)type, vals );
732 #endif /* LDAP_SLAPI */
736 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
739 AttributeDescription *ad = NULL;
743 rc = slap_str2ad( type, &ad, &text );
744 if ( rc != LDAP_SUCCESS ) {
748 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
749 if ( rc != LDAP_SUCCESS ) {
756 #endif /* LDAP_SLAPI */
760 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
765 val.bv_val = (char *)value;
766 val.bv_len = strlen( value );
768 return slapi_entry_add_value( e, type, &val );
771 #endif /* LDAP_SLAPI */
775 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
778 Slapi_Value *vals[2];
781 val.bv_val = (char *)value;
782 val.bv_len = strlen( value );
786 return slapi_entry_delete_values_sv( e, type, vals );
789 #endif /* LDAP_SLAPI */
794 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
797 return slapi_entry_attr_merge( e, (char *)type, vals );
804 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
813 return ( *attr != NULL ) ? 0 : -1;
820 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
827 if ( prevattr == NULL ) {
831 *attr = prevattr->a_next;
833 return ( *attr != NULL ) ? 0 : -1;
840 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
843 AttributeDescription *ad = NULL;
848 rc = slap_str2ad( type, &ad, &text );
849 if ( rc != LDAP_SUCCESS ) {
853 attr_delete( &e->e_attrs, ad );
855 rc = bvptr2obj( vals, &bv );
856 if ( rc != LDAP_SUCCESS ) {
860 rc = attr_merge_normalize( e, ad, bv, NULL );
861 slapi_ch_free( (void **)&bv );
862 if ( rc != LDAP_SUCCESS ) {
869 #endif /* LDAP_SLAPI */
873 * FIXME -- The caller must free the allocated memory.
874 * In Netscape they do not have to.
877 slapi_attr_get_values(
879 struct berval ***vals )
885 if ( attr == NULL ) {
889 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
893 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
894 for ( j = 0; j < i; j++ ) {
895 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
899 *vals = (struct berval **)bv;
902 #else /* LDAP_SLAPI */
904 #endif /* LDAP_SLAPI */
908 slapi_dn_normalize( char *dn )
914 assert( dn != NULL );
917 bdn.bv_len = strlen( dn );
919 if ( dnPretty( NULL, &bdn, &pdn, NULL ) != LDAP_SUCCESS ) {
924 #else /* LDAP_SLAPI */
926 #endif /* LDAP_SLAPI */
930 slapi_dn_normalize_case( char *dn )
936 assert( dn != NULL );
939 bdn.bv_len = strlen( dn );
941 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
946 #else /* LDAP_SLAPI */
948 #endif /* LDAP_SLAPI */
957 struct berval bdn, ndn;
958 struct berval bsuffix, nsuffix;
961 assert( dn != NULL );
962 assert( suffix != NULL );
965 bdn.bv_len = strlen( dn );
967 bsuffix.bv_val = suffix;
968 bsuffix.bv_len = strlen( suffix );
970 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
974 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
977 slapi_ch_free( (void **)&ndn.bv_val );
981 rc = dnIsSuffix( &ndn, &nsuffix );
983 slapi_ch_free( (void **)&ndn.bv_val );
984 slapi_ch_free( (void **)&nsuffix.bv_val );
987 #else /* LDAP_SLAPI */
989 #endif /* LDAP_SLAPI */
994 const char *parentdn,
995 const char *childdn )
998 struct berval assertedParentDN, normalizedAssertedParentDN;
999 struct berval childDN, normalizedChildDN;
1000 struct berval normalizedParentDN;
1003 assert( parentdn != NULL );
1004 assert( childdn != NULL );
1006 assertedParentDN.bv_val = (char *)parentdn;
1007 assertedParentDN.bv_len = strlen( parentdn );
1009 if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
1010 &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
1015 childDN.bv_val = (char *)childdn;
1016 childDN.bv_len = strlen( childdn );
1018 if ( dnNormalize( 0, NULL, NULL, &childDN,
1019 &normalizedChildDN, NULL ) != LDAP_SUCCESS )
1021 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
1025 dnParent( &normalizedChildDN, &normalizedParentDN );
1027 if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
1028 &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
1033 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
1034 slapi_ch_free( (void **)&normalizedChildDN.bv_val );
1036 return ( match == 0 );
1039 #endif /* LDAP_SLAPI */
1043 * Returns DN of the parent entry, or NULL if the DN is
1044 * an empty string or NULL, or has no parent.
1047 slapi_dn_parent( const char *_dn )
1050 struct berval dn, prettyDN;
1051 struct berval parentDN;
1053 if ( _dn == NULL ) {
1057 dn.bv_val = (char *)_dn;
1058 dn.bv_len = strlen( _dn );
1060 if ( dn.bv_len == 0 ) {
1064 if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
1068 dnParent( &prettyDN, &parentDN ); /* in-place */
1070 slapi_ch_free( (void **)&prettyDN.bv_val );
1072 if ( parentDN.bv_len == 0 ) {
1076 return slapi_ch_strdup( parentDN.bv_val );
1079 #endif /* LDAP_SLAPI */
1083 * Returns DN of the parent entry; or NULL if the DN is
1084 * an empty string, if the DN has no parent, or if the
1085 * DN is the suffix of the backend database
1087 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *_dn )
1091 struct berval dn, prettyDN;
1092 struct berval normalizedDN, parentDN;
1094 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
1097 dn.bv_val = (char *)_dn;
1098 dn.bv_len = strlen( _dn );
1100 if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
1104 if ( be != NULL && be_issuffix( be, &normalizedDN ) ) {
1105 slapi_ch_free( (void **)&prettyDN.bv_val );
1106 slapi_ch_free( (void **)&normalizedDN.bv_val );
1110 dnParent( &prettyDN, &parentDN );
1112 slapi_ch_free( (void **)&prettyDN.bv_val );
1113 slapi_ch_free( (void **)&normalizedDN.bv_val );
1115 if ( parentDN.bv_len == 0 ) {
1119 return slapi_ch_strdup( parentDN.bv_val );
1122 #endif /* LDAP_SLAPI */
1126 slapi_dn_ignore_case( char *dn )
1129 return slapi_dn_normalize_case( dn );
1130 #else /* LDAP_SLAPI */
1132 #endif /* LDAP_SLAPI */
1136 slapi_ch_malloc( unsigned long size )
1139 return ch_malloc( size );
1140 #else /* LDAP_SLAPI */
1142 #endif /* LDAP_SLAPI */
1146 slapi_ch_free( void **ptr )
1151 #endif /* LDAP_SLAPI */
1155 slapi_ch_free_string( char **ptr )
1158 slapi_ch_free( (void **)ptr );
1159 #endif /* LDAP_SLAPI */
1163 slapi_ch_array_free( char **arrayp )
1168 if ( arrayp != NULL ) {
1169 for ( p = arrayp; *p != NULL; p++ ) {
1170 slapi_ch_free( (void **)p );
1172 slapi_ch_free( (void **)&arrayp );
1178 slapi_ch_bvdup(const struct berval *v)
1183 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
1184 bv->bv_len = v->bv_len;
1185 bv->bv_val = slapi_ch_malloc( bv->bv_len );
1186 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1195 slapi_ch_bvecdup(const struct berval **v)
1205 for ( i = 0; v[i] != NULL; i++ )
1208 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1210 for ( i = 0; v[i] != NULL; i++ ) {
1211 rv[i] = slapi_ch_bvdup( v[i] );
1223 unsigned long nelem,
1224 unsigned long size )
1227 return ch_calloc( nelem, size );
1228 #else /* LDAP_SLAPI */
1230 #endif /* LDAP_SLAPI */
1236 unsigned long size )
1239 return ch_realloc( block, size );
1240 #else /* LDAP_SLAPI */
1242 #endif /* LDAP_SLAPI */
1246 slapi_ch_strdup( char *s )
1249 return ch_strdup( (const char *)s );
1250 #else /* LDAP_SLAPI */
1252 #endif /* LDAP_SLAPI */
1256 slapi_ch_stlen( char *s )
1259 return strlen( (const char *)s );
1260 #else /* LDAP_SLAPI */
1262 #endif /* LDAP_SLAPI */
1266 slapi_control_present(
1267 LDAPControl **controls,
1269 struct berval **val,
1284 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1285 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1290 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1292 * FIXME: according to 6.1 specification,
1293 * "The val output parameter is set
1294 * to point into the controls array.
1295 * A copy of the control value is
1299 struct berval *pTmpBval;
1301 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
1302 if ( pTmpBval == NULL ) {
1305 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
1306 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
1310 slapi_ch_free( (void **)&pTmpBval );
1316 *val = &controls[i]->ldctl_value;
1321 *iscritical = controls[i]->ldctl_iscritical;
1328 #else /* LDAP_SLAPI */
1330 #endif /* LDAP_SLAPI */
1335 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1336 unsigned long *slapi_mask)
1338 *slapi_mask = SLAPI_OPERATION_NONE;
1340 if ( slap_mask & SLAP_CTRL_ABANDON )
1341 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1343 if ( slap_mask & SLAP_CTRL_ADD )
1344 *slapi_mask |= SLAPI_OPERATION_ADD;
1346 if ( slap_mask & SLAP_CTRL_BIND )
1347 *slapi_mask |= SLAPI_OPERATION_BIND;
1349 if ( slap_mask & SLAP_CTRL_COMPARE )
1350 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1352 if ( slap_mask & SLAP_CTRL_DELETE )
1353 *slapi_mask |= SLAPI_OPERATION_DELETE;
1355 if ( slap_mask & SLAP_CTRL_MODIFY )
1356 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1358 if ( slap_mask & SLAP_CTRL_RENAME )
1359 *slapi_mask |= SLAPI_OPERATION_MODDN;
1361 if ( slap_mask & SLAP_CTRL_SEARCH )
1362 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1364 if ( slap_mask & SLAP_CTRL_UNBIND )
1365 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1369 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1370 slap_mask_t *slap_mask)
1374 if ( slapi_mask & SLAPI_OPERATION_BIND )
1375 *slap_mask |= SLAP_CTRL_BIND;
1377 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1378 *slap_mask |= SLAP_CTRL_UNBIND;
1380 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1381 *slap_mask |= SLAP_CTRL_SEARCH;
1383 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1384 *slap_mask |= SLAP_CTRL_MODIFY;
1386 if ( slapi_mask & SLAPI_OPERATION_ADD )
1387 *slap_mask |= SLAP_CTRL_ADD;
1389 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1390 *slap_mask |= SLAP_CTRL_DELETE;
1392 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1393 *slap_mask |= SLAP_CTRL_RENAME;
1395 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1396 *slap_mask |= SLAP_CTRL_COMPARE;
1398 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1399 *slap_mask |= SLAP_CTRL_ABANDON;
1401 *slap_mask |= SLAP_CTRL_FRONTEND;
1405 slapi_int_parse_control(
1410 /* Plugins must deal with controls themselves. */
1412 return LDAP_SUCCESS;
1414 #endif /* LDAP_SLAPI */
1417 slapi_register_supported_control(
1419 unsigned long controlops )
1422 slap_mask_t controlmask;
1424 slapiControlOp2SlapControlMask( controlops, &controlmask );
1426 register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control );
1427 #endif /* LDAP_SLAPI */
1431 slapi_get_supported_controls(
1433 unsigned long **ctrlopsp )
1438 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1439 if ( rc != LDAP_SUCCESS ) {
1443 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1444 /* In place, naughty. */
1445 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1448 return LDAP_SUCCESS;
1449 #else /* LDAP_SLAPI */
1451 #endif /* LDAP_SLAPI */
1455 slapi_dup_control( LDAPControl *ctrl )
1460 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1461 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1462 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1463 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1468 #endif /* LDAP_SLAPI */
1472 slapi_register_supported_saslmechanism( char *mechanism )
1475 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1476 slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1477 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1478 #endif /* LDAP_SLAPI */
1482 slapi_get_supported_saslmechanisms( void )
1485 /* FIXME -- can not get the saslmechanism without a connection. */
1486 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1487 "can not get the SASL mechanism list "
1488 "without a connection\n" );
1490 #else /* LDAP_SLAPI */
1492 #endif /* LDAP_SLAPI */
1496 slapi_get_supported_extended_ops( void )
1500 char **ppExtOpOID = NULL;
1503 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1507 for ( j = 0; slapi_int_get_supported_extop( j ) != NULL; j++ ) {
1512 if ( numExtOps == 0 ) {
1516 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1517 for ( k = 0; k < i; k++ ) {
1520 bv = get_supported_extop( k );
1521 assert( bv != NULL );
1523 ppExtOpOID[ k ] = bv->bv_val;
1526 for ( ; k < j; k++ ) {
1529 bv = slapi_int_get_supported_extop( k );
1530 assert( bv != NULL );
1532 ppExtOpOID[ i + k ] = bv->bv_val;
1534 ppExtOpOID[ i + k ] = NULL;
1537 #else /* LDAP_SLAPI */
1539 #endif /* LDAP_SLAPI */
1543 slapi_send_ldap_result(
1549 struct berval **urls )
1554 char *extOID = NULL;
1555 struct berval *extValue = NULL;
1557 SlapReply rs = { REP_RESULT };
1559 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1562 rs.sr_matched = matched;
1567 slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
1569 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1570 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
1571 send_ldap_sasl( op, &rs );
1575 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1576 if ( extOID != NULL ) {
1577 rs.sr_rspoid = extOID;
1578 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
1579 send_ldap_extended( op, &rs );
1583 if (op->o_tag == LDAP_REQ_SEARCH)
1584 rs.sr_nentries = nentries;
1586 send_ldap_result( op, &rs );
1587 #endif /* LDAP_SLAPI */
1591 slapi_send_ldap_search_entry(
1594 LDAPControl **ectrls,
1600 SlapReply rs = { REP_RESULT };
1602 AttributeName *an = NULL;
1605 if ( attrs != NULL ) {
1606 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1614 an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
1615 for ( i = 0; attrs[i] != NULL; i++ ) {
1616 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1617 an[i].an_name.bv_len = strlen( attrs[i] );
1618 an[i].an_desc = NULL;
1619 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1622 an[i].an_name.bv_len = 0;
1623 an[i].an_name.bv_val = NULL;
1626 rs.sr_err = LDAP_SUCCESS;
1627 rs.sr_matched = NULL;
1630 rs.sr_ctrls = ectrls;
1632 rs.sr_operational_attrs = NULL;
1637 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1641 return send_search_entry( pOp, &rs );
1642 #else /* LDAP_SLAPI */
1644 #endif /* LDAP_SLAPI */
1648 slapi_send_ldap_search_reference(
1651 struct berval **references,
1652 LDAPControl **ectrls,
1653 struct berval **v2refs
1658 SlapReply rs = { REP_SEARCHREF };
1661 rs.sr_err = LDAP_SUCCESS;
1662 rs.sr_matched = NULL;
1665 rc = bvptr2obj( references, &rs.sr_ref );
1666 if ( rc != LDAP_SUCCESS ) {
1670 rs.sr_ctrls = ectrls;
1672 rs.sr_operational_attrs = NULL;
1675 if ( v2refs != NULL ) {
1676 rc = bvptr2obj( v2refs, &rs.sr_v2ref );
1677 if ( rc != LDAP_SUCCESS ) {
1678 slapi_ch_free( (void **)&rs.sr_ref );
1685 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1689 rc = send_search_reference( pOp, &rs );
1691 if ( rs.sr_ref != NULL )
1692 slapi_ch_free( (void **)&rs.sr_ref );
1694 if ( rs.sr_v2ref != NULL )
1695 slapi_ch_free( (void **)&rs.sr_v2ref );
1700 #endif /* LDAP_SLAPI */
1704 slapi_str2filter( char *str )
1707 return str2filter( str );
1708 #else /* LDAP_SLAPI */
1710 #endif /* LDAP_SLAPI */
1720 #endif /* LDAP_SLAPI */
1724 slapi_filter_dup( Slapi_Filter *filter )
1729 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1731 f->f_choice = filter->f_choice;
1733 switch ( f->f_choice ) {
1734 case LDAP_FILTER_AND:
1735 case LDAP_FILTER_NOT:
1736 case LDAP_FILTER_OR: {
1737 Filter *pFilter, **ppF;
1739 for ( pFilter = filter->f_list, ppF = &f->f_list;
1741 pFilter = pFilter->f_next, ppF = &f->f_next )
1743 *ppF = slapi_filter_dup( pFilter );
1749 case LDAP_FILTER_PRESENT:
1750 f->f_desc = filter->f_desc;
1752 case LDAP_FILTER_EQUALITY:
1753 case LDAP_FILTER_GE:
1754 case LDAP_FILTER_LE:
1755 case LDAP_FILTER_APPROX:
1756 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1757 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1758 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1760 case LDAP_FILTER_EXT:
1761 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1762 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1763 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1764 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1765 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1766 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1768 case LDAP_FILTER_SUBSTRINGS: {
1771 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1772 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1773 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1774 if ( filter->f_sub_any != NULL ) {
1775 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1777 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1778 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1779 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1781 f->f_sub_any[i].bv_val = NULL;
1783 f->f_sub_any = NULL;
1785 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1788 case SLAPD_FILTER_COMPUTED:
1789 f->f_result = filter->f_result;
1792 slapi_ch_free( (void **)&f );
1800 #endif /* LDAP_SLAPI */
1804 slapi_filter_get_choice( Slapi_Filter *f )
1816 #else /* LDAP_SLAPI */
1817 return -1; /* invalid filter type */
1818 #endif /* LDAP_SLAPI */
1822 slapi_filter_get_ava(
1825 struct berval **bval )
1829 int rc = LDAP_SUCCESS;
1831 assert( type != NULL );
1832 assert( bval != NULL );
1837 ftype = f->f_choice;
1838 if ( ftype == LDAP_FILTER_EQUALITY
1839 || ftype == LDAP_FILTER_GE
1840 || ftype == LDAP_FILTER_LE
1841 || ftype == LDAP_FILTER_APPROX ) {
1843 * According to the SLAPI Reference Manual these are
1846 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1847 *bval = &f->f_un.f_un_ava->aa_value;
1848 } else { /* filter type not supported */
1853 #else /* LDAP_SLAPI */
1855 #endif /* LDAP_SLAPI */
1859 slapi_filter_list_first( Slapi_Filter *f )
1868 ftype = f->f_choice;
1869 if ( ftype == LDAP_FILTER_AND
1870 || ftype == LDAP_FILTER_OR
1871 || ftype == LDAP_FILTER_NOT ) {
1872 return (Slapi_Filter *)f->f_list;
1876 #else /* LDAP_SLAPI */
1878 #endif /* LDAP_SLAPI */
1882 slapi_filter_list_next(
1884 Slapi_Filter *fprev )
1893 ftype = f->f_choice;
1894 if ( ftype == LDAP_FILTER_AND
1895 || ftype == LDAP_FILTER_OR
1896 || ftype == LDAP_FILTER_NOT )
1898 return fprev->f_next;
1902 #else /* LDAP_SLAPI */
1904 #endif /* LDAP_SLAPI */
1908 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1915 switch ( f->f_choice ) {
1916 case LDAP_FILTER_GE:
1917 case LDAP_FILTER_LE:
1918 case LDAP_FILTER_EQUALITY:
1919 case LDAP_FILTER_APPROX:
1920 *type = f->f_av_desc->ad_cname.bv_val;
1922 case LDAP_FILTER_SUBSTRINGS:
1923 *type = f->f_sub_desc->ad_cname.bv_val;
1925 case LDAP_FILTER_PRESENT:
1926 *type = f->f_desc->ad_cname.bv_val;
1928 case LDAP_FILTER_EXT:
1929 *type = f->f_mr_desc->ad_cname.bv_val;
1932 /* Complex filters need not apply. */
1940 #endif /* LDAP_SLAPI */
1944 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1945 char ***any, char **final )
1950 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1955 * The caller shouldn't free but we can't return an
1956 * array of char *s from an array of bervals without
1957 * allocating memory, so we may as well be consistent.
1960 *type = f->f_sub_desc->ad_cname.bv_val;
1961 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1962 if ( f->f_sub_any != NULL ) {
1963 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1965 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1966 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1967 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1973 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1978 #endif /* LDAP_SLAPI */
1982 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1985 Slapi_Filter *f = NULL;
1987 if ( ftype == LDAP_FILTER_AND ||
1988 ftype == LDAP_FILTER_OR ||
1989 ftype == LDAP_FILTER_NOT )
1991 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1992 f->f_choice = ftype;
1994 f->f_list->f_next = f2;
2001 #endif /* LDAP_SLAPI */
2005 slapi_x_filter_append( int ftype,
2006 Slapi_Filter **pContainingFilter, /* NULL on first call */
2007 Slapi_Filter **pNextFilter,
2008 Slapi_Filter *filterToAppend )
2011 if ( ftype == LDAP_FILTER_AND ||
2012 ftype == LDAP_FILTER_OR ||
2013 ftype == LDAP_FILTER_NOT )
2015 if ( *pContainingFilter == NULL ) {
2016 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
2017 (*pContainingFilter)->f_choice = ftype;
2018 (*pContainingFilter)->f_list = filterToAppend;
2019 (*pContainingFilter)->f_next = NULL;
2021 if ( (*pContainingFilter)->f_choice != ftype ) {
2025 (*pNextFilter)->f_next = filterToAppend;
2027 *pNextFilter = filterToAppend;
2031 #endif /* LDAP_SLAPI */
2036 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
2044 /* spec says return zero if no filter. */
2048 if ( verify_access ) {
2049 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
2051 return LDAP_PARAM_ERROR;
2057 * According to acl.c it is safe to call test_filter() with
2060 rc = test_filter( op, e, f );
2062 case LDAP_COMPARE_TRUE:
2065 case LDAP_COMPARE_FALSE:
2067 case SLAPD_COMPARE_UNDEFINED:
2070 case LDAP_PROTOCOL_ERROR:
2071 /* filter type unknown: spec says return -1 */
2079 #endif /* LDAP_SLAPI */
2083 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
2086 return slapi_filter_test( NULL, e, f, 0 );
2093 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
2096 switch ( f->f_choice ) {
2097 case LDAP_FILTER_AND:
2098 case LDAP_FILTER_NOT:
2099 case LDAP_FILTER_OR: {
2103 * FIXME: altering f; should we use a temporary?
2105 for ( f = f->f_list; f != NULL; f = f->f_next ) {
2106 rc = slapi_filter_apply( f, fn, arg, error_code );
2110 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
2116 case LDAP_FILTER_EQUALITY:
2117 case LDAP_FILTER_SUBSTRINGS:
2118 case LDAP_FILTER_GE:
2119 case LDAP_FILTER_LE:
2120 case LDAP_FILTER_PRESENT:
2121 case LDAP_FILTER_APPROX:
2122 case LDAP_FILTER_EXT:
2123 *error_code = fn( f, arg );
2126 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2129 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
2130 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
2136 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2138 #endif /* LDAP_SLAPI */
2142 slapi_send_ldap_extended_response(
2147 struct berval *response )
2152 rs.sr_err = errornum;
2153 rs.sr_matched = NULL;
2157 rs.sr_rspoid = respName;
2158 rs.sr_rspdata = response;
2160 send_ldap_extended( op, &rs );
2162 return LDAP_SUCCESS;
2163 #else /* LDAP_SLAPI */
2165 #endif /* LDAP_SLAPI */
2170 struct berval **vals,
2175 * FIXME: what's the point?
2178 #else /* LDAP_SLAPI */
2180 #endif /* LDAP_SLAPI */
2183 #define MAX_HOSTNAME 512
2186 slapi_get_hostname( void )
2190 static int been_here = 0;
2191 static char *static_hn = NULL;
2193 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
2195 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
2196 if ( static_hn == NULL) {
2197 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
2198 "Cannot allocate memory for hostname\n" );
2200 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2205 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
2206 slapi_log_error( SLAPI_LOG_FATAL,
2208 "can't get hostname\n" );
2209 slapi_ch_free( (void **)&static_hn );
2211 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2220 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2222 hn = ch_strdup( static_hn );
2225 #else /* LDAP_SLAPI */
2227 #endif /* LDAP_SLAPI */
2231 * FIXME: this should go in an appropriate header ...
2233 extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist );
2243 int rc = LDAP_SUCCESS;
2246 va_start( arglist, fmt );
2247 rc = slapi_int_log_error( severity, subsystem, fmt, arglist );
2251 #else /* LDAP_SLAPI */
2253 #endif /* LDAP_SLAPI */
2258 slapi_timer_current_time( void )
2261 static int first_time = 1;
2262 #if !defined (_WIN32)
2266 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
2269 gettimeofday( &base_time, NULL );
2271 gettimeofday( &now, NULL );
2272 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
2273 (now.tv_usec - base_time.tv_usec);
2274 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
2280 return (slap_get_time() - starttime) * 1000000;
2287 performance_counter_present = QueryPerformanceCounter( &base_time );
2288 QueryPerformanceFrequency( &performance_freq );
2291 if ( !performance_counter_present )
2294 QueryPerformanceCounter( &now );
2295 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2297 #else /* LDAP_SLAPI */
2299 #endif /* LDAP_SLAPI */
2306 slapi_timer_get_time( char *label )
2309 unsigned long start = slapi_timer_current_time();
2310 printf("%10ld %10ld usec %s\n", start, 0, label);
2312 #else /* LDAP_SLAPI */
2314 #endif /* LDAP_SLAPI */
2321 slapi_timer_elapsed_time(
2323 unsigned long start )
2326 unsigned long stop = slapi_timer_current_time();
2327 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2328 #endif /* LDAP_SLAPI */
2332 slapi_free_search_results_internal( Slapi_PBlock *pb )
2335 Slapi_Entry **entries;
2336 int k = 0, nEnt = 0;
2338 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2339 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2344 if ( entries == NULL ) {
2348 for ( k = 0; k < nEnt; k++ ) {
2349 slapi_entry_free( entries[k] );
2352 slapi_ch_free( (void **)&entries );
2353 #endif /* LDAP_SLAPI */
2358 * Internal API to prime a Slapi_PBlock with a Backend.
2360 static int slapi_int_pblock_set_backend( Slapi_PBlock *pb, Backend *be )
2364 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2365 if ( rc != LDAP_SUCCESS )
2369 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2370 if ( rc != LDAP_SUCCESS )
2374 return LDAP_SUCCESS;
2378 * If oldStyle is TRUE, then a value suitable for setting to
2379 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
2380 * (pointer to static storage).
2382 * If oldStyle is FALSE, then a value suitable for setting to
2383 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2384 * a pointer to allocated memory and will include the SASL
2385 * mechanism (if any).
2387 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2392 switch ( authz->sai_method ) {
2393 case LDAP_AUTH_SASL:
2395 authType = SLAPD_AUTH_SASL;
2397 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2398 authType = slapi_ch_malloc( len );
2399 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2402 case LDAP_AUTH_SIMPLE:
2403 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2405 case LDAP_AUTH_NONE:
2406 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2412 if ( is_tls && authType == NULL ) {
2413 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2420 * Internal API to prime a Slapi_PBlock with a Connection.
2422 static int slapi_int_pblock_set_connection( Slapi_PBlock *pb, Connection *conn )
2427 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2428 if ( rc != LDAP_SUCCESS )
2431 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2432 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2433 if ( rc != LDAP_SUCCESS )
2435 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2436 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2437 if ( rc != LDAP_SUCCESS )
2441 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2442 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2443 if ( rc != LDAP_SUCCESS )
2445 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2446 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2447 if ( rc != LDAP_SUCCESS )
2451 #ifdef LDAP_CONNECTIONLESS
2452 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2453 if ( rc != LDAP_SUCCESS )
2457 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2458 if ( rc != LDAP_SUCCESS )
2461 /* Returns pointer to static string */
2462 connAuthType = Authorization2AuthType( &conn->c_authz,
2469 if ( connAuthType != NULL ) {
2470 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2471 if ( rc != LDAP_SUCCESS )
2475 /* Returns pointer to allocated string */
2476 connAuthType = Authorization2AuthType( &conn->c_authz,
2483 if ( connAuthType != NULL ) {
2484 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2485 /* slapi_pblock_set dups this itself */
2486 slapi_ch_free( (void **)&connAuthType );
2487 if ( rc != LDAP_SUCCESS )
2491 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
2492 /* slapi_pblock_set dups this itself */
2493 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)conn->c_authz.sai_dn.bv_val);
2494 if ( rc != LDAP_SUCCESS )
2498 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SSF, (void *)conn->c_ssf);
2499 if ( rc != LDAP_SUCCESS )
2502 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SASL_CONTEXT,
2503 ( conn->c_sasl_authctx != NULL ? conn->c_sasl_authctx :
2504 conn->c_sasl_sockctx ) );
2505 if ( rc != LDAP_SUCCESS )
2510 #endif /* LDAP_SLAPI */
2513 * Internal API to prime a Slapi_PBlock with an Operation.
2515 int slapi_int_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
2523 if ( op->o_bd != NULL ) {
2524 isRoot = be_isroot( op );
2525 isUpdateDn = be_isupdate( op );
2528 rc = slapi_int_pblock_set_backend( pb, op->o_bd );
2529 if ( rc != LDAP_SUCCESS )
2532 rc = slapi_int_pblock_set_connection( pb, op->o_conn );
2533 if ( rc != LDAP_SUCCESS )
2536 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2537 if ( rc != LDAP_SUCCESS )
2540 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2541 if ( rc != LDAP_SUCCESS )
2544 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2545 if ( rc != LDAP_SUCCESS )
2548 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2549 if ( rc != LDAP_SUCCESS )
2552 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2553 if ( rc != LDAP_SUCCESS )
2556 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2557 if ( rc != LDAP_SUCCESS )
2560 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2561 if ( rc != LDAP_SUCCESS)
2564 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2565 if ( rc != LDAP_SUCCESS )
2568 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2569 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
2570 /* Not quite sure what the point of this is. */
2571 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
2572 if ( rc != LDAP_SUCCESS )
2576 return LDAP_SUCCESS;
2582 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2587 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2589 *isSSL = conn->c_is_tls;
2594 return LDAP_SUCCESS;
2597 #endif /* LDAP_SLAPI */
2601 * DS 5.x compatability API follow
2604 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2610 return LDAP_PARAM_ERROR;
2612 at = attr->a_desc->ad_type;
2614 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2616 if ( is_at_single_value( at ) )
2617 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2618 if ( is_at_operational( at ) )
2619 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2620 if ( is_at_obsolete( at ) )
2621 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2622 if ( is_at_collective( at ) )
2623 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2624 if ( is_at_no_user_mod( at ) )
2625 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2627 return LDAP_SUCCESS;
2630 #endif /* LDAP_SLAPI */
2633 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2636 unsigned long flags;
2638 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2640 return (flags & flag) ? 1 : 0;
2643 #endif /* LDAP_SLAPI */
2646 Slapi_Attr *slapi_attr_new( void )
2651 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2659 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2663 AttributeDescription *ad = NULL;
2665 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2681 void slapi_attr_free( Slapi_Attr **a )
2689 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2692 return attr_dup( (Slapi_Attr *)attr );
2698 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2702 struct berval *nvalp;
2704 AttributeDescription *desc = a->a_desc;
2706 if ( desc->ad_type->sat_equality &&
2707 desc->ad_type->sat_equality->smr_normalize ) {
2708 rc = (*desc->ad_type->sat_equality->smr_normalize)(
2709 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2710 desc->ad_type->sat_syntax,
2711 desc->ad_type->sat_equality,
2712 (Slapi_Value *)v, &nval, NULL );
2713 if ( rc != LDAP_SUCCESS ) {
2721 rc = value_add_one( &a->a_vals, (Slapi_Value *)v );
2722 if ( rc == 0 && nvalp != NULL ) {
2723 rc = value_add_one( &a->a_nvals, nvalp );
2725 a->a_nvals = a->a_vals;
2728 if ( nvalp != NULL ) {
2729 slapi_ch_free_string( &nval.bv_val );
2738 int slapi_attr_type2plugin( const char *type, void **pi )
2745 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2748 if ( attr == NULL ) {
2749 return LDAP_PARAM_ERROR;
2752 *type = attr->a_desc->ad_cname.bv_val;
2754 return LDAP_SUCCESS;
2760 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2763 if ( attr == NULL ) {
2764 return LDAP_PARAM_ERROR;
2766 *oidp = attr->a_desc->ad_type->sat_oid;
2768 return LDAP_SUCCESS;
2774 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2782 mr = a->a_desc->ad_type->sat_equality;
2783 rc = value_match( &ret, a->a_desc, mr,
2784 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2785 (struct berval *)v1, (void *)v2, &text );
2786 if ( rc != LDAP_SUCCESS )
2789 return ( ret == 0 ) ? 0 : -1;
2795 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2805 if ( a ->a_vals == NULL ) {
2808 mr = a->a_desc->ad_type->sat_equality;
2809 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2810 rc = value_match( &ret, a->a_desc, mr,
2811 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2812 if ( rc != LDAP_SUCCESS ) {
2819 #endif /* LDAP_SLAPI */
2823 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2826 AttributeDescription *a1 = NULL;
2827 AttributeDescription *a2 = NULL;
2831 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2835 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2839 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2840 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2841 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2844 case SLAPI_TYPE_CMP_EXACT:
2845 ret = ad_cmp( a1, a2 );
2847 case SLAPI_TYPE_CMP_BASE:
2848 ret = ad_base_cmp( a1, a2 );
2850 case SLAPI_TYPE_CMP_SUBTYPE:
2851 ret = is_ad_subtype( a2, a2 );
2864 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2867 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2873 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2876 return slapi_valueset_first_value( &a->a_vals, v );
2882 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2885 return slapi_valueset_next_value( &a->a_vals, hint, v );
2891 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2894 *numValues = slapi_valueset_count( &a->a_vals );
2902 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2905 *vs = &((Slapi_Attr *)a)->a_vals;
2913 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2916 return slapi_attr_get_values( a, vals );
2922 char *slapi_attr_syntax_normalize( const char *s )
2925 AttributeDescription *ad = NULL;
2928 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2932 return ad->ad_cname.bv_val;
2938 Slapi_Value *slapi_value_new( void )
2943 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2951 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2954 return ber_dupbv( NULL, (struct berval *)bval );
2960 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2963 return slapi_value_new_berval( v );
2969 Slapi_Value *slapi_value_new_string(const char *s)
2974 bv.bv_val = (char *)s;
2975 bv.bv_len = strlen( s );
2977 return slapi_value_new_berval( &bv );
2983 Slapi_Value *slapi_value_init(Slapi_Value *val)
2995 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2998 return ber_dupbv( v, bval );
3004 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
3007 v->bv_val = slapi_ch_strdup( (char *)s );
3008 v->bv_len = strlen( s );
3016 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
3019 return slapi_value_new_value( v );
3025 void slapi_value_free(Slapi_Value **value)
3028 if ( value == NULL ) {
3032 if ( (*value) != NULL ) {
3033 slapi_ch_free( (void **)&(*value)->bv_val );
3034 slapi_ch_free( (void **)value );
3039 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
3048 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
3051 if ( value == NULL ) {
3054 if ( value->bv_val != NULL ) {
3055 slapi_ch_free( (void **)&value->bv_val );
3057 slapi_value_init_berval( value, (struct berval *)bval );
3065 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
3068 if ( value == NULL ) {
3071 return slapi_value_set_berval( value, vfrom );
3077 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
3080 if ( value == NULL ) {
3083 if ( value->bv_val != NULL ) {
3084 slapi_ch_free( (void **)&value->bv_val );
3086 value->bv_val = slapi_ch_malloc( len );
3087 value->bv_len = len;
3088 AC_MEMCPY( value->bv_val, val, len );
3096 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
3099 if ( value == NULL ) {
3102 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
3109 int slapi_value_set_int(Slapi_Value *value, int intVal)
3114 snprintf( buf, sizeof( buf ), "%d", intVal );
3116 return slapi_value_set_string( value, buf );
3122 const char *slapi_value_get_string(const Slapi_Value *value)
3125 if ( value == NULL ) return NULL;
3126 if ( value->bv_val == NULL ) return NULL;
3127 if ( !checkBVString( value ) ) return NULL;
3129 return value->bv_val;
3135 int slapi_value_get_int(const Slapi_Value *value)
3138 if ( value == NULL ) return 0;
3139 if ( value->bv_val == NULL ) return 0;
3140 if ( !checkBVString( value ) ) return 0;
3142 return (int)strtol( value->bv_val, NULL, 10 );
3148 unsigned int slapi_value_get_uint(const Slapi_Value *value)
3151 if ( value == NULL ) return 0;
3152 if ( value->bv_val == NULL ) return 0;
3153 if ( !checkBVString( value ) ) return 0;
3155 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
3161 long slapi_value_get_long(const Slapi_Value *value)
3164 if ( value == NULL ) return 0;
3165 if ( value->bv_val == NULL ) return 0;
3166 if ( !checkBVString( value ) ) return 0;
3168 return strtol( value->bv_val, NULL, 10 );
3174 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
3177 if ( value == NULL ) return 0;
3178 if ( value->bv_val == NULL ) return 0;
3179 if ( !checkBVString( value ) ) return 0;
3181 return strtoul( value->bv_val, NULL, 10 );
3187 size_t slapi_value_get_length(const Slapi_Value *value)
3190 if ( value == NULL )
3193 return (size_t) value->bv_len;
3199 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
3202 return slapi_attr_value_cmp( a, v1, v2 );
3208 /* A ValueSet is a container for a BerVarray. */
3209 Slapi_ValueSet *slapi_valueset_new( void )
3214 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
3223 void slapi_valueset_free(Slapi_ValueSet *vs)
3229 ber_bvarray_free( vp );
3230 slapi_ch_free( (void **)&vp );
3237 void slapi_valueset_init(Slapi_ValueSet *vs)
3240 if ( vs != NULL && *vs == NULL ) {
3241 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
3242 (*vs)->bv_val = NULL;
3248 void slapi_valueset_done(Slapi_ValueSet *vs)
3256 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
3258 slapi_ch_free( (void **)&vp->bv_val );
3260 /* but don't free *vs or vs */
3264 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
3269 ber_dupbv( &bv, (Slapi_Value *)addval );
3270 ber_bvarray_add( vs, &bv );
3274 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
3277 return slapi_valueset_next_value( vs, 0, v );
3283 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
3294 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
3305 int slapi_valueset_count( const Slapi_ValueSet *vs )
3316 for ( i = 0; vp[i].bv_val != NULL; i++ )
3326 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
3331 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
3332 slapi_valueset_add_value( vs1, vp );
3337 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
3338 struct berval *val, int access )
3345 slap_access_t slap_access;
3346 AttributeDescription *ad = NULL;
3349 ret = slap_str2ad( attr, &ad, &text );
3350 if ( ret != LDAP_SUCCESS ) {
3354 switch ( access & SLAPI_ACL_ALL ) {
3355 case SLAPI_ACL_COMPARE:
3356 slap_access = ACL_COMPARE;
3358 case SLAPI_ACL_SEARCH:
3359 slap_access = ACL_SEARCH;
3361 case SLAPI_ACL_READ:
3362 slap_access = ACL_READ;
3364 case SLAPI_ACL_WRITE:
3365 case SLAPI_ACL_DELETE:
3367 case SLAPI_ACL_SELF:
3368 slap_access = ACL_WRITE;
3371 return LDAP_INSUFFICIENT_ACCESS;
3375 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3376 return LDAP_PARAM_ERROR;
3379 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3380 return LDAP_PARAM_ERROR;
3383 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3384 return LDAP_PARAM_ERROR;
3387 ret = access_allowed( op, e, ad, val, slap_access, NULL );
3389 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3391 return LDAP_UNWILLING_TO_PERFORM;
3395 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
3399 int rc = LDAP_SUCCESS;
3400 Modifications *ml, *mp;
3402 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3403 return LDAP_PARAM_ERROR;
3406 ml = slapi_int_ldapmods2modifications( mods );
3411 for ( mp = ml; mp != NULL; mp = mp->sml_next ) {
3412 rc = slap_bv2ad( &mp->sml_type, &mp->sml_desc, (const char **)errbuf );
3413 if ( rc != LDAP_SUCCESS ) {
3418 if ( rc == LDAP_SUCCESS ) {
3419 rc = acl_check_modlist( op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3422 /* Careful when freeing the modlist because it has pointers into the mods array. */
3423 for ( ; ml != NULL; ml = mp ) {
3426 /* just free the containing array */
3427 slapi_ch_free( (void **)&ml->sml_values );
3428 slapi_ch_free( (void **)&ml );
3433 return LDAP_UNWILLING_TO_PERFORM;
3438 * Synthesise an LDAPMod array from a Modifications list to pass
3439 * to SLAPI. This synthesis is destructive and as such the
3440 * Modifications list may not be used after calling this
3443 * This function must also be called before slap_mods_check().
3445 LDAPMod **slapi_int_modifications2ldapmods(Modifications **pmodlist)
3448 Modifications *ml, *modlist;
3449 LDAPMod **mods, *modp;
3452 modlist = *pmodlist;
3454 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3457 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3459 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3460 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3462 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3464 /* Take ownership of original type. */
3465 modp->mod_type = ml->sml_type.bv_val;
3466 ml->sml_type.bv_val = NULL;
3468 if ( ml->sml_values != NULL ) {
3469 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
3471 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3472 sizeof(struct berval *) );
3473 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
3474 /* Take ownership of original values. */
3475 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3476 modp->mod_bvalues[j]->bv_len = ml->sml_values[j].bv_len;
3477 modp->mod_bvalues[j]->bv_val = ml->sml_values[j].bv_val;
3478 ml->sml_values[j].bv_len = 0;
3479 ml->sml_values[j].bv_val = NULL;
3481 modp->mod_bvalues[j] = NULL;
3483 modp->mod_bvalues = NULL;
3490 slap_mods_free( modlist );
3500 * Convert a potentially modified array of LDAPMods back to a
3501 * Modification list.
3503 * The returned Modification list contains pointers into the
3504 * LDAPMods array; the latter MUST be freed with
3505 * slapi_int_free_ldapmods() (see below).
3507 Modifications *slapi_int_ldapmods2modifications (LDAPMod **mods)
3510 Modifications *modlist = NULL, **modtail;
3515 for( modp = mods; *modp != NULL; modp++ ) {
3519 struct berval **bvp;
3521 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3522 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3523 mod->sml_type.bv_val = (*modp)->mod_type;
3524 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3525 mod->sml_desc = NULL;
3526 mod->sml_next = NULL;
3528 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3529 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3532 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3537 mod->sml_values = NULL;
3539 mod->sml_values = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3541 /* NB: This implicitly trusts a plugin to return valid modifications. */
3542 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3543 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3544 mod->sml_values[i].bv_val = (*bvp)->bv_val;
3545 mod->sml_values[i].bv_len = (*bvp)->bv_len;
3548 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3549 mod->sml_values[i].bv_val = *p;
3550 mod->sml_values[i].bv_len = strlen( *p );
3553 mod->sml_values[i].bv_val = NULL;
3554 mod->sml_values[i].bv_len = 0;
3556 mod->sml_nvalues = NULL;
3559 modtail = &mod->sml_next;
3569 * This function only frees the parts of the mods array that
3570 * are not shared with the Modification list that was created
3571 * by slapi_int_ldapmods2modifications().
3574 void slapi_int_free_ldapmods (LDAPMod **mods)
3582 for ( i = 0; mods[i] != NULL; i++ ) {
3584 * Don't free values themselves; they're owned by the
3585 * Modification list. Do free the containing array.
3587 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3588 for ( j = 0; mods[i]->mod_values != NULL && mods[i]->mod_values[j] != NULL; j++ ) {
3589 ch_free( mods[i]->mod_values[j] );
3591 ch_free( mods[i]->mod_values );
3593 ch_free( mods[i]->mod_values );
3595 /* Don't free type, for same reasons. */
3599 #endif /* LDAP_SLAPI */
3603 * Sun ONE DS 5.x computed attribute support. Computed attributes
3604 * allow for dynamically generated operational attributes, a very
3605 * useful thing indeed.
3609 * Write the computed attribute to a BerElement. Complementary
3610 * functions need to be defined for anything that replaces
3611 * op->o_callback->sc_sendentry, if you wish to make computed
3612 * attributes available to it.
3614 int slapi_int_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
3617 Operation *op = NULL;
3619 AttributeDescription *desc = NULL;
3635 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
3636 if ( rc != 0 || op == NULL ) {
3640 ber = (BerElement *)c->cac_private;
3643 if ( c->cac_attrs == NULL ) {
3644 /* All attrs request, skip operational attributes */
3645 if ( is_at_operational( desc->ad_type ) ) {
3649 /* Specific attrs requested */
3650 if ( is_at_operational( desc->ad_type ) ) {
3651 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3655 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3661 if ( !access_allowed( op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
3662 slapi_log_error( SLAPI_LOG_ACL, "slapi_int_compute_output_ber",
3663 "acl: access to attribute %s not allowed\n",
3664 desc->ad_cname.bv_val );
3668 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
3670 slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
3671 "ber_printf failed\n");
3675 if ( !c->cac_attrsonly && a->a_vals != NULL ) {
3676 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
3677 if ( !access_allowed( op, e,
3678 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
3679 slapi_log_error( SLAPI_LOG_ACL, "slapi_int_compute_output_ber",
3681 "acl: access to %s, value %d not allowed\n",
3682 op->o_connid, desc->ad_cname.bv_val, i );
3686 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
3687 slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
3688 "ber_printf failed\n");
3694 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
3695 slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
3696 "ber_printf failed\n" );
3707 * For some reason Sun don't use the normal plugin mechanism
3708 * registration path to register an "evaluator" function (an
3709 * "evaluator" is responsible for adding computed attributes;
3710 * the nomenclature is somewhat confusing).
3712 * As such slapi_compute_add_evaluator() registers the
3713 * function directly.
3715 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3718 Slapi_PBlock *pPlugin = NULL;
3721 pPlugin = slapi_pblock_new();
3722 if ( pPlugin == NULL ) {
3723 rc = LDAP_NO_MEMORY;
3727 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3728 if ( rc != LDAP_SUCCESS ) {
3732 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3733 if ( rc != LDAP_SUCCESS ) {
3737 rc = slapi_int_register_plugin( NULL, pPlugin );
3744 if ( rc != LDAP_SUCCESS ) {
3745 if ( pPlugin != NULL ) {
3746 slapi_pblock_destroy( pPlugin );
3754 #endif /* LDAP_SLAPI */
3758 * See notes above regarding slapi_compute_add_evaluator().
3760 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3763 Slapi_PBlock *pPlugin = NULL;
3766 pPlugin = slapi_pblock_new();
3767 if ( pPlugin == NULL ) {
3768 rc = LDAP_NO_MEMORY;
3772 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3773 if ( rc != LDAP_SUCCESS ) {
3777 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3778 if ( rc != LDAP_SUCCESS ) {
3782 rc = slapi_int_register_plugin( NULL, pPlugin );
3789 if ( rc != LDAP_SUCCESS ) {
3790 if ( pPlugin != NULL ) {
3791 slapi_pblock_destroy( pPlugin );
3799 #endif /* LDAP_SLAPI */
3803 * Call compute evaluators
3805 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3809 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3811 rc = slapi_int_get_plugins( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3812 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3813 /* Nothing to do; front-end should ignore. */
3817 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3819 * -1: no attribute matched requested type
3820 * 0: one attribute matched
3821 * >0: error happened
3823 rc = (*pGetPlugin)( c, type, e, outputfn );
3829 slapi_ch_free( (void **)&tmpPlugin );
3834 #endif /* LDAP_SLAPI */
3837 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3843 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3848 return slapi_int_call_plugins( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3851 #endif /* LDAP_SLAPI */
3855 * New API to provide the plugin with access to the search
3856 * pblock. Have informed Sun DS team.
3858 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3864 if ( c->cac_pb == NULL )
3872 #endif /* LDAP_SLAPI */
3875 Slapi_Mutex *slapi_new_mutex( void )
3880 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3881 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3882 slapi_ch_free( (void **)&m );
3892 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3895 if ( mutex != NULL ) {
3896 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3897 slapi_ch_free( (void **)&mutex);
3902 void slapi_lock_mutex( Slapi_Mutex *mutex )
3905 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3909 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3912 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3918 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3923 if ( mutex == NULL ) {
3927 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3928 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3929 slapi_ch_free( (void **)&cv );
3933 /* XXX struct copy */
3934 cv->mutex = mutex->mutex;
3942 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3945 if ( cvar != NULL ) {
3946 ldap_pvt_thread_cond_destroy( &cvar->cond );
3947 slapi_ch_free( (void **)&cvar );
3952 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3955 if ( cvar == NULL ) {
3959 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3965 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3968 if ( cvar == NULL ) {
3973 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3976 return ldap_pvt_thread_cond_signal( &cvar->cond );
3982 int slapi_int_access_allowed( Operation *op,
3984 AttributeDescription *desc,
3986 slap_access_t access,
3987 AccessControlState *state )
3990 int rc, slap_access = 0;
3991 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3993 if ( op->o_pb == NULL ) {
3994 /* internal operation */
4000 slap_access |= SLAPI_ACL_ADD | SLAPI_ACL_DELETE | SLAPI_ACL_WRITE;
4003 slap_access |= SLAPI_ACL_READ;
4006 slap_access |= SLAPI_ACL_SEARCH;
4009 slap_access = ACL_COMPARE;
4015 rc = slapi_int_get_plugins( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
4016 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
4017 /* nothing to do; allowed access */
4021 slapi_int_pblock_set_operation( op->o_pb, op );
4023 rc = 1; /* default allow policy */
4025 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
4030 rc = (*pGetPlugin)( op->o_pb, entry, desc->ad_cname.bv_val,
4031 val, slap_access, (void *)state );
4037 slapi_ch_free( (void **)&tmpPlugin );
4042 #endif /* LDAP_SLAPI */
4046 * There is no documentation for this.
4048 int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
4061 rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
4062 if ( rc != LDAP_SUCCESS ) {
4066 if ( lrdn[1] != NULL ) {
4067 return -1; /* not single valued */
4072 *type = slapi_ch_strdup( ava->la_attr.bv_val );
4073 ber_dupbv( bv, &ava->la_value );
4080 #endif /* LDAP_SLAPI */
4083 char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
4086 struct berval new_dn, parent_dn, newrdn;
4088 new_dn.bv_val = NULL;
4090 parent_dn.bv_val = (char *)dn;
4091 parent_dn.bv_len = strlen( dn );
4093 newrdn.bv_val = (char *)rdn;
4094 newrdn.bv_len = strlen( rdn );
4096 build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
4098 return new_dn.bv_val;
4101 #endif /* LDAP_SLAPI */
4104 int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
4109 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
4110 size_t textlen = sizeof textbuf;
4113 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
4116 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
4118 return ( rc == LDAP_SUCCESS ) ? 0 : 1;
4121 #endif /* LDAP_SLAPI */
4124 int slapi_entry_rdn_values_present( const Slapi_Entry *e )
4129 int i = 0, match = 0;
4131 rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
4132 if ( rc != LDAP_SUCCESS ) {
4136 if ( dn[0] != NULL ) {
4137 LDAPRDN rdn = dn[0];
4139 for ( i = 0; rdn[i] != NULL; i++ ) {
4140 LDAPAVA *ava = &rdn[0][i];
4141 Slapi_Attr *a = NULL;
4143 if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
4144 slapi_attr_value_find( a, &ava->la_value ) == 0 )
4151 return ( i == match );
4154 #endif /* LDAP_SLAPI */
4157 int slapi_entry_add_rdn_values( Slapi_Entry *e )
4163 rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
4164 if ( rc != LDAP_SUCCESS ) {
4168 if ( dn[0] != NULL ) {
4169 LDAPRDN rdn = dn[0];
4170 struct berval *vals[2];
4172 for ( i = 0; rdn[i] != NULL; i++ ) {
4173 LDAPAVA *ava = &rdn[0][i];
4174 Slapi_Attr *a = NULL;
4176 if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
4177 slapi_attr_value_find( a, &ava->la_value ) == 0 )
4180 vals[0] = &ava->la_value;
4183 slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
4189 return LDAP_SUCCESS;
4192 #endif /* LDAP_SLAPI */
4195 const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
4199 const char *uniqueid;
4201 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
4202 if ( attr == NULL ) {
4206 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
4207 return slapi_value_get_string( &attr->a_vals[0] );
4209 #endif /* LDAP_SLAPI */
4214 void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
4219 attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
4221 bv.bv_val = uniqueid;
4222 bv.bv_len = strlen( uniqueid );
4223 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
4224 #endif /* LDAP_SLAPI */
4227 LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
4235 size = sizeof("ldap:///");
4238 size += strlen( ldaphost );
4239 if ( ldapport != 0 )
4242 url = slapi_ch_malloc( size );
4244 if ( ldapport != 0 ) {
4245 sprintf( url, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
4247 sprintf( url, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
4250 rc = ldap_initialize( &ld, url );
4252 slapi_ch_free_string( &url );
4254 return ( rc == LDAP_SUCCESS ) ? ld : NULL;
4257 #endif /* LDAP_SLAPI */
4260 void slapi_ldap_unbind( LDAP *ld )
4264 #endif /* LDAP_SLAPI */