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>
36 * server start time (should we use a struct timeval also in slapd?
38 static struct timeval base_time;
39 ldap_pvt_thread_mutex_t slapi_hn_mutex;
40 ldap_pvt_thread_mutex_t slapi_time_mutex;
43 ldap_pvt_thread_mutex_t mutex;
46 struct slapi_condvar {
47 ldap_pvt_thread_cond_t cond;
48 ldap_pvt_thread_mutex_t mutex;
52 static int checkBVString(const struct berval *bv)
56 for ( i = 0; i < bv->bv_len; i++ ) {
57 if ( bv->bv_val[i] == '\0' )
60 if ( bv->bv_val[i] != '\0' )
65 #endif /* LDAP_SLAPI */
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 ) {
114 Slapi_Entry *e = NULL;
117 pTmpS = slapi_ch_strdup( s );
118 if ( pTmpS != NULL ) {
119 e = str2entry( pTmpS );
120 slapi_ch_free( (void **)&pTmpS );
126 #endif /* LDAP_SLAPI */
137 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
138 ret = entry2str( e, len );
139 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
142 #else /* LDAP_SLAPI */
144 #endif /* LDAP_SLAPI */
148 slapi_entry_get_dn( Slapi_Entry *e )
151 return e->e_name.bv_val;
152 #else /* LDAP_SLAPI */
154 #endif /* LDAP_SLAPI */
158 slapi_x_entry_get_id( Slapi_Entry *e )
164 #endif /* LDAP_SLAPI */
173 struct berval dn = BER_BVNULL;
176 dn.bv_len = strlen( ldn );
178 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname, NULL );
179 #endif /* LDAP_SLAPI */
183 slapi_entry_dup( Slapi_Entry *e )
186 return entry_dup( e );
187 #else /* LDAP_SLAPI */
189 #endif /* LDAP_SLAPI */
193 slapi_entry_attr_delete(
198 AttributeDescription *ad = NULL;
201 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
202 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
205 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
206 return 0; /* attribute is deleted */
208 return -1; /* something went wrong */
210 #else /* LDAP_SLAPI */
212 #endif /* LDAP_SLAPI */
216 slapi_entry_alloc( void )
219 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
220 #else /* LDAP_SLAPI */
222 #endif /* LDAP_SLAPI */
226 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;
542 pb = slapi_pblock_new();
544 slapi_pblock_set( pb, SLAPI_TARGET_DN, slapi_entry_get_dn( (Entry *)e ) );
546 rc = slapi_int_connection_init( pb, LDAP_REQ_SEARCH, &conn );
547 if ( rc != LDAP_SUCCESS ) {
548 slapi_pblock_destroy( pb );
552 op = conn->c_pending_ops.stqh_first;
553 op->o_bd = select_backend( &e->e_nname, 0, 0 );
554 if ( op->o_bd != NULL ) {
555 op->o_bd->be_has_subordinates( op, (Entry *)e, &hasSubordinates );
558 slapi_int_connection_destroy( &conn );
559 slapi_pblock_destroy( pb );
561 return ( hasSubordinates == LDAP_COMPARE_TRUE );
568 * Return approximate size of the entry rounded to the nearest
569 * 1K. Only the size of the attribute values are counted in the
570 * Sun implementation.
572 * http://docs.sun.com/source/816-6701-10/funcref.html#1017388
574 size_t slapi_entry_size(Slapi_Entry *e)
581 for ( size = 0, a = e->e_attrs; a != NULL; a = a->a_next ) {
582 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
583 size += a->a_vals[i].bv_len + 1;
588 size -= (size % 1024);
593 #endif /* LDAP_SLAPI */
597 * Add values to entry.
600 * LDAP_SUCCESS Values added to entry
601 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
602 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
605 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
611 char textbuf[SLAP_TEXT_BUFLEN];
613 mod.sm_op = LDAP_MOD_ADD;
616 mod.sm_type.bv_val = (char *)type;
617 mod.sm_type.bv_len = strlen( type );
619 rc = slap_str2ad( type, &mod.sm_desc, &text );
620 if ( rc != LDAP_SUCCESS ) {
624 if ( vals == NULL ) {
625 /* Apparently vals can be NULL
626 * FIXME: sm_values = NULL ? */
627 mod.sm_values = (BerVarray)ch_malloc( sizeof(struct berval) );
628 mod.sm_values->bv_val = NULL;
631 rc = bvptr2obj( vals, &mod.sm_values );
632 if ( rc != LDAP_SUCCESS ) {
633 return LDAP_CONSTRAINT_VIOLATION;
636 mod.sm_nvalues = NULL;
638 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
640 ch_free( mod.sm_values );
642 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
645 #endif /* LDAP_SLAPI */
649 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
652 return slapi_entry_add_values( e, type, vals );
655 #endif /* LDAP_SLAPI */
659 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
662 AttributeDescription *ad = NULL;
666 rc = slap_str2ad( type, &ad, &text );
667 if ( rc != LDAP_SUCCESS ) {
671 return attr_merge_normalize( e, ad, *vs, NULL );
674 #endif /* LDAP_SLAPI */
678 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
684 char textbuf[SLAP_TEXT_BUFLEN];
686 mod.sm_op = LDAP_MOD_DELETE;
689 mod.sm_type.bv_val = (char *)type;
690 mod.sm_type.bv_len = strlen( type );
692 if ( vals == NULL ) {
693 /* If vals is NULL, this is a NOOP. */
697 rc = slap_str2ad( type, &mod.sm_desc, &text );
698 if ( rc != LDAP_SUCCESS ) {
702 if ( vals[0] == NULL ) {
703 /* SLAPI doco says LDAP_OPERATIONS_ERROR but LDAP_OTHER is better */
704 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
707 rc = bvptr2obj( vals, &mod.sm_values );
708 if ( rc != LDAP_SUCCESS ) {
709 return LDAP_CONSTRAINT_VIOLATION;
711 mod.sm_nvalues = NULL;
713 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
715 ch_free( mod.sm_values );
720 #endif /* LDAP_SLAPI */
724 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
727 return slapi_entry_delete_values( e, type, vals );
730 #endif /* LDAP_SLAPI */
734 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
737 return slapi_entry_attr_merge( e, (char *)type, vals );
740 #endif /* LDAP_SLAPI */
744 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
747 AttributeDescription *ad = NULL;
751 rc = slap_str2ad( type, &ad, &text );
752 if ( rc != LDAP_SUCCESS ) {
756 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
757 if ( rc != LDAP_SUCCESS ) {
764 #endif /* LDAP_SLAPI */
768 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
773 val.bv_val = (char *)value;
774 val.bv_len = strlen( value );
776 return slapi_entry_add_value( e, type, &val );
779 #endif /* LDAP_SLAPI */
783 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
786 Slapi_Value *vals[2];
789 val.bv_val = (char *)value;
790 val.bv_len = strlen( value );
794 return slapi_entry_delete_values_sv( e, type, vals );
797 #endif /* LDAP_SLAPI */
802 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
805 return slapi_entry_attr_merge( e, (char *)type, vals );
812 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
821 return ( *attr != NULL ) ? 0 : -1;
828 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
835 if ( prevattr == NULL ) {
839 *attr = prevattr->a_next;
841 return ( *attr != NULL ) ? 0 : -1;
848 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
851 AttributeDescription *ad = NULL;
856 rc = slap_str2ad( type, &ad, &text );
857 if ( rc != LDAP_SUCCESS ) {
861 attr_delete( &e->e_attrs, ad );
863 rc = bvptr2obj( vals, &bv );
864 if ( rc != LDAP_SUCCESS ) {
868 rc = attr_merge_normalize( e, ad, bv, NULL );
869 slapi_ch_free( (void **)&bv );
870 if ( rc != LDAP_SUCCESS ) {
877 #endif /* LDAP_SLAPI */
881 * FIXME -- The caller must free the allocated memory.
882 * In Netscape they do not have to.
885 slapi_attr_get_values(
887 struct berval ***vals )
893 if ( attr == NULL ) {
897 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
901 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
902 for ( j = 0; j < i; j++ ) {
903 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
907 *vals = (struct berval **)bv;
910 #else /* LDAP_SLAPI */
912 #endif /* LDAP_SLAPI */
916 slapi_dn_normalize( char *dn )
922 assert( dn != NULL );
925 bdn.bv_len = strlen( dn );
927 if ( dnPretty( NULL, &bdn, &pdn, NULL ) != LDAP_SUCCESS ) {
932 #else /* LDAP_SLAPI */
934 #endif /* LDAP_SLAPI */
938 slapi_dn_normalize_case( char *dn )
944 assert( dn != NULL );
947 bdn.bv_len = strlen( dn );
949 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
954 #else /* LDAP_SLAPI */
956 #endif /* LDAP_SLAPI */
965 struct berval bdn, ndn;
966 struct berval bsuffix, nsuffix;
969 assert( dn != NULL );
970 assert( suffix != NULL );
973 bdn.bv_len = strlen( dn );
975 bsuffix.bv_val = suffix;
976 bsuffix.bv_len = strlen( suffix );
978 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
982 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
985 slapi_ch_free( (void **)&ndn.bv_val );
989 rc = dnIsSuffix( &ndn, &nsuffix );
991 slapi_ch_free( (void **)&ndn.bv_val );
992 slapi_ch_free( (void **)&nsuffix.bv_val );
995 #else /* LDAP_SLAPI */
997 #endif /* LDAP_SLAPI */
1002 const char *parentdn,
1003 const char *childdn )
1006 struct berval assertedParentDN, normalizedAssertedParentDN;
1007 struct berval childDN, normalizedChildDN;
1008 struct berval normalizedParentDN;
1011 assert( parentdn != NULL );
1012 assert( childdn != NULL );
1014 assertedParentDN.bv_val = (char *)parentdn;
1015 assertedParentDN.bv_len = strlen( parentdn );
1017 if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
1018 &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
1023 childDN.bv_val = (char *)childdn;
1024 childDN.bv_len = strlen( childdn );
1026 if ( dnNormalize( 0, NULL, NULL, &childDN,
1027 &normalizedChildDN, NULL ) != LDAP_SUCCESS )
1029 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
1033 dnParent( &normalizedChildDN, &normalizedParentDN );
1035 if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
1036 &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
1041 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
1042 slapi_ch_free( (void **)&normalizedChildDN.bv_val );
1044 return ( match == 0 );
1047 #endif /* LDAP_SLAPI */
1051 * Returns DN of the parent entry, or NULL if the DN is
1052 * an empty string or NULL, or has no parent.
1055 slapi_dn_parent( const char *_dn )
1058 struct berval dn, prettyDN;
1059 struct berval parentDN;
1061 if ( _dn == NULL ) {
1065 dn.bv_val = (char *)_dn;
1066 dn.bv_len = strlen( _dn );
1068 if ( dn.bv_len == 0 ) {
1072 if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
1076 dnParent( &prettyDN, &parentDN ); /* in-place */
1078 slapi_ch_free( (void **)&prettyDN.bv_val );
1080 if ( parentDN.bv_len == 0 ) {
1084 return slapi_ch_strdup( parentDN.bv_val );
1087 #endif /* LDAP_SLAPI */
1091 * Returns DN of the parent entry; or NULL if the DN is
1092 * an empty string, if the DN has no parent, or if the
1093 * DN is the suffix of the backend database
1095 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *_dn )
1099 struct berval dn, prettyDN;
1100 struct berval normalizedDN, parentDN;
1102 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
1105 dn.bv_val = (char *)_dn;
1106 dn.bv_len = strlen( _dn );
1108 if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
1112 if ( be != NULL && be_issuffix( be, &normalizedDN ) ) {
1113 slapi_ch_free( (void **)&prettyDN.bv_val );
1114 slapi_ch_free( (void **)&normalizedDN.bv_val );
1118 dnParent( &prettyDN, &parentDN );
1120 slapi_ch_free( (void **)&prettyDN.bv_val );
1121 slapi_ch_free( (void **)&normalizedDN.bv_val );
1123 if ( parentDN.bv_len == 0 ) {
1127 return slapi_ch_strdup( parentDN.bv_val );
1130 #endif /* LDAP_SLAPI */
1134 slapi_dn_ignore_case( char *dn )
1137 return slapi_dn_normalize_case( dn );
1138 #else /* LDAP_SLAPI */
1140 #endif /* LDAP_SLAPI */
1144 slapi_ch_malloc( unsigned long size )
1147 return ch_malloc( size );
1148 #else /* LDAP_SLAPI */
1150 #endif /* LDAP_SLAPI */
1154 slapi_ch_free( void **ptr )
1157 if ( ptr == NULL || *ptr == NULL )
1161 #endif /* LDAP_SLAPI */
1165 slapi_ch_free_string( char **ptr )
1168 slapi_ch_free( (void **)ptr );
1169 #endif /* LDAP_SLAPI */
1173 slapi_ch_array_free( char **arrayp )
1178 if ( arrayp != NULL ) {
1179 for ( p = arrayp; *p != NULL; p++ ) {
1180 slapi_ch_free( (void **)p );
1182 slapi_ch_free( (void **)&arrayp );
1188 slapi_ch_bvdup(const struct berval *v)
1193 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
1194 bv->bv_len = v->bv_len;
1195 bv->bv_val = slapi_ch_malloc( bv->bv_len );
1196 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1205 slapi_ch_bvecdup(const struct berval **v)
1215 for ( i = 0; v[i] != NULL; i++ )
1218 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1220 for ( i = 0; v[i] != NULL; i++ ) {
1221 rv[i] = slapi_ch_bvdup( v[i] );
1233 unsigned long nelem,
1234 unsigned long size )
1237 return ch_calloc( nelem, size );
1238 #else /* LDAP_SLAPI */
1240 #endif /* LDAP_SLAPI */
1246 unsigned long size )
1249 return ch_realloc( block, size );
1250 #else /* LDAP_SLAPI */
1252 #endif /* LDAP_SLAPI */
1256 slapi_ch_strdup( char *s )
1259 return ch_strdup( (const char *)s );
1260 #else /* LDAP_SLAPI */
1262 #endif /* LDAP_SLAPI */
1266 slapi_ch_stlen( char *s )
1269 return strlen( (const char *)s );
1270 #else /* LDAP_SLAPI */
1272 #endif /* LDAP_SLAPI */
1276 slapi_control_present(
1277 LDAPControl **controls,
1279 struct berval **val,
1294 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1295 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1300 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1302 * FIXME: according to 6.1 specification,
1303 * "The val output parameter is set
1304 * to point into the controls array.
1305 * A copy of the control value is
1309 struct berval *pTmpBval;
1311 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
1312 if ( pTmpBval == NULL ) {
1315 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
1316 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
1320 slapi_ch_free( (void **)&pTmpBval );
1326 *val = &controls[i]->ldctl_value;
1331 *iscritical = controls[i]->ldctl_iscritical;
1338 #else /* LDAP_SLAPI */
1340 #endif /* LDAP_SLAPI */
1345 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1346 unsigned long *slapi_mask)
1348 *slapi_mask = SLAPI_OPERATION_NONE;
1350 if ( slap_mask & SLAP_CTRL_ABANDON )
1351 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1353 if ( slap_mask & SLAP_CTRL_ADD )
1354 *slapi_mask |= SLAPI_OPERATION_ADD;
1356 if ( slap_mask & SLAP_CTRL_BIND )
1357 *slapi_mask |= SLAPI_OPERATION_BIND;
1359 if ( slap_mask & SLAP_CTRL_COMPARE )
1360 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1362 if ( slap_mask & SLAP_CTRL_DELETE )
1363 *slapi_mask |= SLAPI_OPERATION_DELETE;
1365 if ( slap_mask & SLAP_CTRL_MODIFY )
1366 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1368 if ( slap_mask & SLAP_CTRL_RENAME )
1369 *slapi_mask |= SLAPI_OPERATION_MODDN;
1371 if ( slap_mask & SLAP_CTRL_SEARCH )
1372 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1374 if ( slap_mask & SLAP_CTRL_UNBIND )
1375 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1379 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1380 slap_mask_t *slap_mask)
1384 if ( slapi_mask & SLAPI_OPERATION_BIND )
1385 *slap_mask |= SLAP_CTRL_BIND;
1387 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1388 *slap_mask |= SLAP_CTRL_UNBIND;
1390 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1391 *slap_mask |= SLAP_CTRL_SEARCH;
1393 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1394 *slap_mask |= SLAP_CTRL_MODIFY;
1396 if ( slapi_mask & SLAPI_OPERATION_ADD )
1397 *slap_mask |= SLAP_CTRL_ADD;
1399 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1400 *slap_mask |= SLAP_CTRL_DELETE;
1402 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1403 *slap_mask |= SLAP_CTRL_RENAME;
1405 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1406 *slap_mask |= SLAP_CTRL_COMPARE;
1408 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1409 *slap_mask |= SLAP_CTRL_ABANDON;
1411 *slap_mask |= SLAP_CTRL_GLOBAL;
1415 slapi_int_parse_control(
1420 /* Plugins must deal with controls themselves. */
1422 return LDAP_SUCCESS;
1424 #endif /* LDAP_SLAPI */
1427 slapi_register_supported_control(
1429 unsigned long controlops )
1432 slap_mask_t controlmask;
1434 slapiControlOp2SlapControlMask( controlops, &controlmask );
1436 register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL );
1437 #endif /* LDAP_SLAPI */
1441 slapi_get_supported_controls(
1443 unsigned long **ctrlopsp )
1448 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1449 if ( rc != LDAP_SUCCESS ) {
1453 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1454 /* In place, naughty. */
1455 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1458 return LDAP_SUCCESS;
1459 #else /* LDAP_SLAPI */
1461 #endif /* LDAP_SLAPI */
1465 slapi_dup_control( LDAPControl *ctrl )
1470 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1471 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1472 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1473 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1478 #endif /* LDAP_SLAPI */
1482 slapi_register_supported_saslmechanism( char *mechanism )
1485 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1486 slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1487 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1488 #endif /* LDAP_SLAPI */
1492 slapi_get_supported_saslmechanisms( void )
1495 /* FIXME -- can not get the saslmechanism without a connection. */
1496 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1497 "can not get the SASL mechanism list "
1498 "without a connection\n" );
1500 #else /* LDAP_SLAPI */
1502 #endif /* LDAP_SLAPI */
1506 slapi_get_supported_extended_ops( void )
1510 char **ppExtOpOID = NULL;
1513 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1517 for ( j = 0; slapi_int_get_supported_extop( j ) != NULL; j++ ) {
1522 if ( numExtOps == 0 ) {
1526 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1527 for ( k = 0; k < i; k++ ) {
1530 bv = get_supported_extop( k );
1531 assert( bv != NULL );
1533 ppExtOpOID[ k ] = bv->bv_val;
1536 for ( ; k < j; k++ ) {
1539 bv = slapi_int_get_supported_extop( k );
1540 assert( bv != NULL );
1542 ppExtOpOID[ i + k ] = bv->bv_val;
1544 ppExtOpOID[ i + k ] = NULL;
1547 #else /* LDAP_SLAPI */
1549 #endif /* LDAP_SLAPI */
1553 slapi_send_ldap_result(
1559 struct berval **urls )
1563 char *extOID = NULL;
1564 SlapReply rs = { REP_RESULT };
1566 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1569 rs.sr_matched = matched;
1574 slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
1576 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1577 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
1578 send_ldap_sasl( op, &rs );
1582 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1583 if ( extOID != NULL ) {
1584 rs.sr_rspoid = extOID;
1585 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
1586 send_ldap_extended( op, &rs );
1590 if ( op->o_tag == LDAP_REQ_SEARCH )
1591 rs.sr_nentries = nentries;
1593 send_ldap_result( op, &rs );
1594 #endif /* LDAP_SLAPI */
1598 slapi_send_ldap_search_entry(
1601 LDAPControl **ectrls,
1607 SlapReply rs = { REP_RESULT };
1609 AttributeName *an = NULL;
1612 if ( attrs != NULL ) {
1613 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1621 an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
1622 for ( i = 0; attrs[i] != NULL; i++ ) {
1623 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1624 an[i].an_name.bv_len = strlen( attrs[i] );
1625 an[i].an_desc = NULL;
1626 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1629 an[i].an_name.bv_len = 0;
1630 an[i].an_name.bv_val = NULL;
1633 rs.sr_err = LDAP_SUCCESS;
1634 rs.sr_matched = NULL;
1637 rs.sr_ctrls = ectrls;
1639 rs.sr_operational_attrs = NULL;
1644 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1648 return send_search_entry( pOp, &rs );
1649 #else /* LDAP_SLAPI */
1651 #endif /* LDAP_SLAPI */
1655 slapi_send_ldap_search_reference(
1658 struct berval **references,
1659 LDAPControl **ectrls,
1660 struct berval **v2refs
1665 SlapReply rs = { REP_SEARCHREF };
1668 rs.sr_err = LDAP_SUCCESS;
1669 rs.sr_matched = NULL;
1672 rc = bvptr2obj( references, &rs.sr_ref );
1673 if ( rc != LDAP_SUCCESS ) {
1677 rs.sr_ctrls = ectrls;
1679 rs.sr_operational_attrs = NULL;
1682 if ( v2refs != NULL ) {
1683 rc = bvptr2obj( v2refs, &rs.sr_v2ref );
1684 if ( rc != LDAP_SUCCESS ) {
1685 slapi_ch_free( (void **)&rs.sr_ref );
1692 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1696 rc = send_search_reference( pOp, &rs );
1698 if ( rs.sr_ref != NULL )
1699 slapi_ch_free( (void **)&rs.sr_ref );
1701 if ( rs.sr_v2ref != NULL )
1702 slapi_ch_free( (void **)&rs.sr_v2ref );
1707 #endif /* LDAP_SLAPI */
1711 slapi_str2filter( char *str )
1714 return str2filter( str );
1715 #else /* LDAP_SLAPI */
1717 #endif /* LDAP_SLAPI */
1727 #endif /* LDAP_SLAPI */
1731 slapi_filter_dup( Slapi_Filter *filter )
1736 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1738 f->f_choice = filter->f_choice;
1740 switch ( f->f_choice ) {
1741 case LDAP_FILTER_AND:
1742 case LDAP_FILTER_NOT:
1743 case LDAP_FILTER_OR: {
1744 Filter *pFilter, **ppF;
1746 for ( pFilter = filter->f_list, ppF = &f->f_list;
1748 pFilter = pFilter->f_next, ppF = &f->f_next )
1750 *ppF = slapi_filter_dup( pFilter );
1756 case LDAP_FILTER_PRESENT:
1757 f->f_desc = filter->f_desc;
1759 case LDAP_FILTER_EQUALITY:
1760 case LDAP_FILTER_GE:
1761 case LDAP_FILTER_LE:
1762 case LDAP_FILTER_APPROX:
1763 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1764 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1765 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1767 case LDAP_FILTER_EXT:
1768 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1769 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1770 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1771 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1772 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1773 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1775 case LDAP_FILTER_SUBSTRINGS: {
1778 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1779 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1780 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1781 if ( filter->f_sub_any != NULL ) {
1782 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1784 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1785 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1786 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1788 f->f_sub_any[i].bv_val = NULL;
1790 f->f_sub_any = NULL;
1792 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1795 case SLAPD_FILTER_COMPUTED:
1796 f->f_result = filter->f_result;
1799 slapi_ch_free( (void **)&f );
1807 #endif /* LDAP_SLAPI */
1811 slapi_filter_get_choice( Slapi_Filter *f )
1823 #else /* LDAP_SLAPI */
1824 return -1; /* invalid filter type */
1825 #endif /* LDAP_SLAPI */
1829 slapi_filter_get_ava(
1832 struct berval **bval )
1836 int rc = LDAP_SUCCESS;
1838 assert( type != NULL );
1839 assert( bval != NULL );
1844 ftype = f->f_choice;
1845 if ( ftype == LDAP_FILTER_EQUALITY
1846 || ftype == LDAP_FILTER_GE
1847 || ftype == LDAP_FILTER_LE
1848 || ftype == LDAP_FILTER_APPROX ) {
1850 * According to the SLAPI Reference Manual these are
1853 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1854 *bval = &f->f_un.f_un_ava->aa_value;
1855 } else { /* filter type not supported */
1860 #else /* LDAP_SLAPI */
1862 #endif /* LDAP_SLAPI */
1866 slapi_filter_list_first( Slapi_Filter *f )
1875 ftype = f->f_choice;
1876 if ( ftype == LDAP_FILTER_AND
1877 || ftype == LDAP_FILTER_OR
1878 || ftype == LDAP_FILTER_NOT ) {
1879 return (Slapi_Filter *)f->f_list;
1883 #else /* LDAP_SLAPI */
1885 #endif /* LDAP_SLAPI */
1889 slapi_filter_list_next(
1891 Slapi_Filter *fprev )
1900 ftype = f->f_choice;
1901 if ( ftype == LDAP_FILTER_AND
1902 || ftype == LDAP_FILTER_OR
1903 || ftype == LDAP_FILTER_NOT )
1905 return fprev->f_next;
1909 #else /* LDAP_SLAPI */
1911 #endif /* LDAP_SLAPI */
1915 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1922 switch ( f->f_choice ) {
1923 case LDAP_FILTER_GE:
1924 case LDAP_FILTER_LE:
1925 case LDAP_FILTER_EQUALITY:
1926 case LDAP_FILTER_APPROX:
1927 *type = f->f_av_desc->ad_cname.bv_val;
1929 case LDAP_FILTER_SUBSTRINGS:
1930 *type = f->f_sub_desc->ad_cname.bv_val;
1932 case LDAP_FILTER_PRESENT:
1933 *type = f->f_desc->ad_cname.bv_val;
1935 case LDAP_FILTER_EXT:
1936 *type = f->f_mr_desc->ad_cname.bv_val;
1939 /* Complex filters need not apply. */
1947 #endif /* LDAP_SLAPI */
1951 slapi_x_filter_set_attribute_type( Slapi_Filter *f, const char *type )
1954 AttributeDescription **adp, *ad = NULL;
1962 switch ( f->f_choice ) {
1963 case LDAP_FILTER_GE:
1964 case LDAP_FILTER_LE:
1965 case LDAP_FILTER_EQUALITY:
1966 case LDAP_FILTER_APPROX:
1967 adp = &f->f_av_desc;
1969 case LDAP_FILTER_SUBSTRINGS:
1970 adp = &f->f_sub_desc;
1972 case LDAP_FILTER_PRESENT:
1975 case LDAP_FILTER_EXT:
1976 adp = &f->f_mr_desc;
1979 /* Complex filters need not apply. */
1983 rc = slap_str2ad( type, &ad, &text );
1984 if ( rc == LDAP_SUCCESS )
1987 return ( rc == LDAP_SUCCESS ) ? 0 : -1;
1990 #endif /* LDAP_SLAPI */
1994 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1995 char ***any, char **final )
2000 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
2005 * The caller shouldn't free but we can't return an
2006 * array of char *s from an array of bervals without
2007 * allocating memory, so we may as well be consistent.
2010 *type = f->f_sub_desc->ad_cname.bv_val;
2011 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
2012 if ( f->f_sub_any != NULL ) {
2013 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
2015 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
2016 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
2017 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
2023 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
2028 #endif /* LDAP_SLAPI */
2032 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
2035 Slapi_Filter *f = NULL;
2037 if ( ftype == LDAP_FILTER_AND ||
2038 ftype == LDAP_FILTER_OR ||
2039 ftype == LDAP_FILTER_NOT )
2041 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
2042 f->f_choice = ftype;
2044 f->f_list->f_next = f2;
2051 #endif /* LDAP_SLAPI */
2055 slapi_x_filter_append( int ftype,
2056 Slapi_Filter **pContainingFilter, /* NULL on first call */
2057 Slapi_Filter **pNextFilter,
2058 Slapi_Filter *filterToAppend )
2061 if ( ftype == LDAP_FILTER_AND ||
2062 ftype == LDAP_FILTER_OR ||
2063 ftype == LDAP_FILTER_NOT )
2065 if ( *pContainingFilter == NULL ) {
2066 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
2067 (*pContainingFilter)->f_choice = ftype;
2068 (*pContainingFilter)->f_list = filterToAppend;
2069 (*pContainingFilter)->f_next = NULL;
2071 if ( (*pContainingFilter)->f_choice != ftype ) {
2075 (*pNextFilter)->f_next = filterToAppend;
2077 *pNextFilter = filterToAppend;
2081 #endif /* LDAP_SLAPI */
2086 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
2094 /* spec says return zero if no filter. */
2098 if ( verify_access ) {
2099 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
2101 return LDAP_PARAM_ERROR;
2107 * According to acl.c it is safe to call test_filter() with
2110 rc = test_filter( op, e, f );
2112 case LDAP_COMPARE_TRUE:
2115 case LDAP_COMPARE_FALSE:
2117 case SLAPD_COMPARE_UNDEFINED:
2120 case LDAP_PROTOCOL_ERROR:
2121 /* filter type unknown: spec says return -1 */
2129 #endif /* LDAP_SLAPI */
2133 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
2136 return slapi_filter_test( NULL, e, f, 0 );
2143 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
2146 switch ( f->f_choice ) {
2147 case LDAP_FILTER_AND:
2148 case LDAP_FILTER_NOT:
2149 case LDAP_FILTER_OR: {
2153 * FIXME: altering f; should we use a temporary?
2155 for ( f = f->f_list; f != NULL; f = f->f_next ) {
2156 rc = slapi_filter_apply( f, fn, arg, error_code );
2160 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
2166 case LDAP_FILTER_EQUALITY:
2167 case LDAP_FILTER_SUBSTRINGS:
2168 case LDAP_FILTER_GE:
2169 case LDAP_FILTER_LE:
2170 case LDAP_FILTER_PRESENT:
2171 case LDAP_FILTER_APPROX:
2172 case LDAP_FILTER_EXT:
2173 *error_code = fn( f, arg );
2176 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2179 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
2180 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
2186 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2188 #endif /* LDAP_SLAPI */
2192 slapi_send_ldap_extended_response(
2197 struct berval *response )
2202 rs.sr_err = errornum;
2203 rs.sr_matched = NULL;
2207 rs.sr_rspoid = respName;
2208 rs.sr_rspdata = response;
2210 send_ldap_extended( op, &rs );
2212 return LDAP_SUCCESS;
2213 #else /* LDAP_SLAPI */
2215 #endif /* LDAP_SLAPI */
2220 struct berval **vals,
2225 * FIXME: what's the point?
2228 #else /* LDAP_SLAPI */
2230 #endif /* LDAP_SLAPI */
2233 #define MAX_HOSTNAME 512
2236 slapi_get_hostname( void )
2240 static int been_here = 0;
2241 static char *static_hn = NULL;
2243 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
2245 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
2246 if ( static_hn == NULL) {
2247 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
2248 "Cannot allocate memory for hostname\n" );
2250 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2255 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
2256 slapi_log_error( SLAPI_LOG_FATAL,
2258 "can't get hostname\n" );
2259 slapi_ch_free( (void **)&static_hn );
2261 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2270 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2272 hn = ch_strdup( static_hn );
2275 #else /* LDAP_SLAPI */
2277 #endif /* LDAP_SLAPI */
2281 * FIXME: this should go in an appropriate header ...
2283 extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist );
2293 int rc = LDAP_SUCCESS;
2296 va_start( arglist, fmt );
2297 rc = slapi_int_log_error( severity, subsystem, fmt, arglist );
2301 #else /* LDAP_SLAPI */
2303 #endif /* LDAP_SLAPI */
2308 slapi_timer_current_time( void )
2311 static int first_time = 1;
2312 #if !defined (_WIN32)
2316 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
2319 gettimeofday( &base_time, NULL );
2321 gettimeofday( &now, NULL );
2322 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
2323 (now.tv_usec - base_time.tv_usec);
2324 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
2330 return (slap_get_time() - starttime) * 1000000;
2337 performance_counter_present = QueryPerformanceCounter( &base_time );
2338 QueryPerformanceFrequency( &performance_freq );
2341 if ( !performance_counter_present )
2344 QueryPerformanceCounter( &now );
2345 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2347 #else /* LDAP_SLAPI */
2349 #endif /* LDAP_SLAPI */
2356 slapi_timer_get_time( char *label )
2359 unsigned long start = slapi_timer_current_time();
2360 printf("%10ld %10d usec %s\n", start, 0, label);
2362 #else /* LDAP_SLAPI */
2364 #endif /* LDAP_SLAPI */
2371 slapi_timer_elapsed_time(
2373 unsigned long start )
2376 unsigned long stop = slapi_timer_current_time();
2377 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2378 #endif /* LDAP_SLAPI */
2382 slapi_free_search_results_internal( Slapi_PBlock *pb )
2385 Slapi_Entry **entries;
2386 int k = 0, nEnt = 0;
2388 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2389 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2394 if ( entries == NULL ) {
2398 for ( k = 0; k < nEnt; k++ ) {
2399 slapi_entry_free( entries[k] );
2402 slapi_ch_free( (void **)&entries );
2403 #endif /* LDAP_SLAPI */
2408 * Internal API to prime a Slapi_PBlock with a Backend.
2410 static int slapi_int_pblock_set_backend( Slapi_PBlock *pb, Backend *be )
2412 slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2415 slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2422 * If oldStyle is TRUE, then a value suitable for setting to
2423 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
2424 * (pointer to static storage).
2426 * If oldStyle is FALSE, then a value suitable for setting to
2427 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2428 * a pointer to allocated memory and will include the SASL
2429 * mechanism (if any).
2431 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2436 switch ( authz->sai_method ) {
2437 case LDAP_AUTH_SASL:
2439 authType = SLAPD_AUTH_SASL;
2441 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2442 authType = slapi_ch_malloc( len );
2443 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2446 case LDAP_AUTH_SIMPLE:
2447 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2449 case LDAP_AUTH_NONE:
2450 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2456 if ( is_tls && authType == NULL ) {
2457 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2464 * Internal API to prime a Slapi_PBlock with a Connection.
2466 static int slapi_int_pblock_set_connection( Slapi_PBlock *pb, Connection *conn )
2470 slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2472 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2473 slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2474 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2475 slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2478 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2479 slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2480 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2481 slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2484 #ifdef LDAP_CONNECTIONLESS
2485 slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2488 slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2490 /* Returns pointer to static string */
2491 connAuthType = Authorization2AuthType( &conn->c_authz,
2498 if ( connAuthType != NULL ) {
2499 slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2502 /* Returns pointer to allocated string */
2503 connAuthType = Authorization2AuthType( &conn->c_authz,
2510 if ( connAuthType != NULL ) {
2511 slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2512 /* slapi_pblock_set dups this itself */
2513 slapi_ch_free( (void **)&connAuthType );
2516 if ( conn->c_authz.sai_method != LDAP_AUTH_NONE &&
2517 conn->c_authz.sai_dn.bv_val != NULL ) {
2518 /* slapi_pblock_set dups this itself */
2519 slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)conn->c_authz.sai_dn.bv_val);
2522 slapi_pblock_set(pb, SLAPI_X_CONN_SSF, (void *)conn->c_ssf);
2524 slapi_pblock_set(pb, SLAPI_X_CONN_SASL_CONTEXT,
2525 ( conn->c_sasl_authctx != NULL ? conn->c_sasl_authctx :
2526 conn->c_sasl_sockctx ) );
2530 #endif /* LDAP_SLAPI */
2533 * Internal API to prime a Slapi_PBlock with an Operation.
2535 int slapi_int_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
2541 void *existingOp = NULL;
2546 if ( op->o_req_ndn.bv_len ) {
2547 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
2548 if ( op->o_bd != NULL ) {
2549 isRoot = be_isroot( op );
2550 isUpdateDn = be_isupdate( op );
2554 /* This should only be called once per operation */
2555 slapi_pblock_get( pb, SLAPI_OPERATION, &existingOp );
2556 assert( existingOp == NULL );
2558 slapi_int_pblock_set_backend( pb, op->o_bd );
2559 slapi_int_pblock_set_connection( pb, op->o_conn );
2561 slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2562 slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2563 slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2564 slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2565 slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2566 slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2567 slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2568 slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2569 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)get_manageDSAit( op ) );
2570 slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2571 if ( opAuthType != NULL )
2572 slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
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 rc = slap_str2ad( attr, &ad, &text );
3350 if ( rc != LDAP_SUCCESS ) {
3355 * Whilst the SLAPI access types are arranged as a bitmask, the
3356 * documentation indicates that they are to be used separately.
3358 switch ( access & SLAPI_ACL_ALL ) {
3359 case SLAPI_ACL_COMPARE:
3360 slap_access = ACL_COMPARE;
3362 case SLAPI_ACL_SEARCH:
3363 slap_access = ACL_SEARCH;
3365 case SLAPI_ACL_READ:
3366 slap_access = ACL_READ;
3368 case SLAPI_ACL_WRITE:
3369 slap_access = ACL_WRITE;
3371 case SLAPI_ACL_DELETE:
3372 slap_access = ACL_WDEL;
3375 slap_access = ACL_WADD;
3377 case SLAPI_ACL_SELF: /* not documented */
3378 case SLAPI_ACL_PROXY: /* not documented */
3380 return LDAP_INSUFFICIENT_ACCESS;
3384 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3385 return LDAP_PARAM_ERROR;
3388 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3389 return LDAP_PARAM_ERROR;
3392 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3393 return LDAP_PARAM_ERROR;
3396 if ( access_allowed( op, e, ad, val, slap_access, NULL ) ) {
3397 return LDAP_SUCCESS;
3400 return LDAP_INSUFFICIENT_ACCESS;
3402 return LDAP_UNWILLING_TO_PERFORM;
3406 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
3410 int rc = LDAP_SUCCESS;
3411 Modifications *ml, *mp;
3413 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3414 return LDAP_PARAM_ERROR;
3417 ml = slapi_int_ldapmods2modifications( mods );
3422 if ( rc == LDAP_SUCCESS ) {
3423 rc = acl_check_modlist( op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3426 /* Careful when freeing the modlist because it has pointers into the mods array. */
3427 for ( ; ml != NULL; ml = mp ) {
3430 /* just free the containing array */
3431 slapi_ch_free( (void **)&ml->sml_values );
3432 slapi_ch_free( (void **)&ml );
3437 return LDAP_UNWILLING_TO_PERFORM;
3442 * Synthesise an LDAPMod array from a Modifications list to pass
3443 * to SLAPI. This synthesis is destructive and as such the
3444 * Modifications list may not be used after calling this
3447 * This function must also be called before slap_mods_check().
3449 LDAPMod **slapi_int_modifications2ldapmods(Modifications **pmodlist)
3452 Modifications *ml, *modlist;
3453 LDAPMod **mods, *modp;
3456 modlist = *pmodlist;
3458 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3461 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3463 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3464 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3466 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3468 /* Take ownership of original type. */
3469 modp->mod_type = ml->sml_type.bv_val;
3470 ml->sml_type.bv_val = NULL;
3472 if ( ml->sml_values != NULL ) {
3473 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
3475 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3476 sizeof(struct berval *) );
3477 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
3478 /* Take ownership of original values. */
3479 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3480 modp->mod_bvalues[j]->bv_len = ml->sml_values[j].bv_len;
3481 modp->mod_bvalues[j]->bv_val = ml->sml_values[j].bv_val;
3482 ml->sml_values[j].bv_len = 0;
3483 ml->sml_values[j].bv_val = NULL;
3485 modp->mod_bvalues[j] = NULL;
3487 modp->mod_bvalues = NULL;
3494 slap_mods_free( modlist );
3504 * Convert a potentially modified array of LDAPMods back to a
3505 * Modification list.
3507 * The returned Modification list contains pointers into the
3508 * LDAPMods array; the latter MUST be freed with
3509 * slapi_int_free_ldapmods() (see below).
3511 Modifications *slapi_int_ldapmods2modifications ( LDAPMod **mods )
3514 Modifications *modlist = NULL, **modtail;
3517 if ( mods == NULL ) {
3523 for( modp = mods; *modp != NULL; modp++ ) {
3527 struct berval **bvp;
3529 AttributeDescription *ad = NULL;
3531 /* Don't initialize attribute type if mods_check() is going to be called */
3532 if ( slap_str2ad( (*modp)->mod_type, &ad, &text ) != LDAP_SUCCESS )
3535 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3536 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3538 mod->sml_type.bv_val = (*modp)->mod_type;
3539 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3541 mod->sml_next = NULL;
3543 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3544 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3547 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3552 mod->sml_values = NULL;
3554 mod->sml_values = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3556 /* NB: This implicitly trusts a plugin to return valid modifications. */
3557 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3558 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3559 mod->sml_values[i].bv_val = (*bvp)->bv_val;
3560 mod->sml_values[i].bv_len = (*bvp)->bv_len;
3563 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3564 mod->sml_values[i].bv_val = *p;
3565 mod->sml_values[i].bv_len = strlen( *p );
3568 mod->sml_values[i].bv_val = NULL;
3569 mod->sml_values[i].bv_len = 0;
3571 mod->sml_nvalues = NULL;
3574 modtail = &mod->sml_next;
3584 * This function only frees the parts of the mods array that
3585 * are not shared with the Modification list that was created
3586 * by slapi_int_ldapmods2modifications().
3589 void slapi_int_free_ldapmods (LDAPMod **mods)
3597 for ( i = 0; mods[i] != NULL; i++ ) {
3599 * Don't free values themselves; they're owned by the
3600 * Modification list. Do free the containing array.
3602 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3603 for ( j = 0; mods[i]->mod_values != NULL && mods[i]->mod_values[j] != NULL; j++ ) {
3604 ch_free( mods[i]->mod_values[j] );
3606 ch_free( mods[i]->mod_values );
3608 ch_free( mods[i]->mod_values );
3610 /* Don't free type, for same reasons. */
3614 #endif /* LDAP_SLAPI */
3618 * Sun ONE DS 5.x computed attribute support. Computed attributes
3619 * allow for dynamically generated operational attributes, a very
3620 * useful thing indeed.
3624 * For some reason Sun don't use the normal plugin mechanism
3625 * registration path to register an "evaluator" function (an
3626 * "evaluator" is responsible for adding computed attributes;
3627 * the nomenclature is somewhat confusing).
3629 * As such slapi_compute_add_evaluator() registers the
3630 * function directly.
3632 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3635 Slapi_PBlock *pPlugin = NULL;
3638 pPlugin = slapi_pblock_new();
3639 if ( pPlugin == NULL ) {
3640 rc = LDAP_NO_MEMORY;
3644 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3645 if ( rc != LDAP_SUCCESS ) {
3649 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3650 if ( rc != LDAP_SUCCESS ) {
3654 rc = slapi_int_register_plugin( frontendDB, pPlugin );
3661 if ( rc != LDAP_SUCCESS ) {
3662 if ( pPlugin != NULL ) {
3663 slapi_pblock_destroy( pPlugin );
3671 #endif /* LDAP_SLAPI */
3675 * See notes above regarding slapi_compute_add_evaluator().
3677 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3680 Slapi_PBlock *pPlugin = NULL;
3683 pPlugin = slapi_pblock_new();
3684 if ( pPlugin == NULL ) {
3685 rc = LDAP_NO_MEMORY;
3689 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3690 if ( rc != LDAP_SUCCESS ) {
3694 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3695 if ( rc != LDAP_SUCCESS ) {
3699 rc = slapi_int_register_plugin( frontendDB, pPlugin );
3706 if ( rc != LDAP_SUCCESS ) {
3707 if ( pPlugin != NULL ) {
3708 slapi_pblock_destroy( pPlugin );
3716 #endif /* LDAP_SLAPI */
3720 * Call compute evaluators
3722 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3726 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3728 rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3729 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3730 /* Nothing to do; front-end should ignore. */
3734 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3736 * -1: no attribute matched requested type
3737 * 0: one attribute matched
3738 * >0: error happened
3740 rc = (*pGetPlugin)( c, type, e, outputfn );
3746 slapi_ch_free( (void **)&tmpPlugin );
3751 #endif /* LDAP_SLAPI */
3754 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3760 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3765 return slapi_int_call_plugins( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3768 #endif /* LDAP_SLAPI */
3772 * New API to provide the plugin with access to the search
3773 * pblock. Have informed Sun DS team.
3775 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3781 if ( c->cac_pb == NULL )
3789 #endif /* LDAP_SLAPI */
3792 Slapi_Mutex *slapi_new_mutex( void )
3797 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3798 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3799 slapi_ch_free( (void **)&m );
3809 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3812 if ( mutex != NULL ) {
3813 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3814 slapi_ch_free( (void **)&mutex);
3819 void slapi_lock_mutex( Slapi_Mutex *mutex )
3822 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3826 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3829 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3835 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3840 if ( mutex == NULL ) {
3844 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3845 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3846 slapi_ch_free( (void **)&cv );
3850 /* XXX struct copy */
3851 cv->mutex = mutex->mutex;
3859 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3862 if ( cvar != NULL ) {
3863 ldap_pvt_thread_cond_destroy( &cvar->cond );
3864 slapi_ch_free( (void **)&cvar );
3869 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3872 if ( cvar == NULL ) {
3876 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3882 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3885 if ( cvar == NULL ) {
3890 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3893 return ldap_pvt_thread_cond_signal( &cvar->cond );
3899 int slapi_int_access_allowed( Operation *op,
3901 AttributeDescription *desc,
3903 slap_access_t access,
3904 AccessControlState *state )
3907 int rc, slap_access = 0;
3908 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3911 pb = SLAPI_OPERATION_PBLOCK( op );
3913 /* internal operation */
3919 slap_access |= SLAPI_ACL_COMPARE;
3922 slap_access |= SLAPI_ACL_SEARCH;
3925 slap_access |= SLAPI_ACL_READ;
3928 slap_access |= SLAPI_ACL_WRITE;
3931 slap_access |= SLAPI_ACL_DELETE;
3934 slap_access |= SLAPI_ACL_ADD;
3940 rc = slapi_int_get_plugins( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
3941 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3942 /* nothing to do; allowed access */
3946 rc = 1; /* default allow policy */
3948 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3953 rc = (*pGetPlugin)( pb, entry, desc->ad_cname.bv_val,
3954 val, slap_access, (void *)state );
3960 slapi_ch_free( (void **)&tmpPlugin );
3965 #endif /* LDAP_SLAPI */
3969 * There is no documentation for this.
3971 int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
3984 rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
3985 if ( rc != LDAP_SUCCESS ) {
3989 if ( lrdn[1] != NULL ) {
3990 return -1; /* not single valued */
3995 *type = slapi_ch_strdup( ava->la_attr.bv_val );
3996 ber_dupbv( bv, &ava->la_value );
4003 #endif /* LDAP_SLAPI */
4006 char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
4009 struct berval new_dn, parent_dn, newrdn;
4011 new_dn.bv_val = NULL;
4013 parent_dn.bv_val = (char *)dn;
4014 parent_dn.bv_len = strlen( dn );
4016 newrdn.bv_val = (char *)rdn;
4017 newrdn.bv_len = strlen( rdn );
4019 build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
4021 return new_dn.bv_val;
4024 #endif /* LDAP_SLAPI */
4027 int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
4032 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
4033 size_t textlen = sizeof textbuf;
4036 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
4039 rc = entry_schema_check( be, e, NULL, 0,
4040 &text, textbuf, textlen );
4042 return ( rc == LDAP_SUCCESS ) ? 0 : 1;
4045 #endif /* LDAP_SLAPI */
4048 int slapi_entry_rdn_values_present( const Slapi_Entry *e )
4053 int i = 0, match = 0;
4055 rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
4056 if ( rc != LDAP_SUCCESS ) {
4060 if ( dn[0] != NULL ) {
4061 LDAPRDN rdn = dn[0];
4063 for ( i = 0; rdn[i] != NULL; i++ ) {
4064 LDAPAVA *ava = &rdn[0][i];
4065 Slapi_Attr *a = NULL;
4067 if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
4068 slapi_attr_value_find( a, &ava->la_value ) == 0 )
4075 return ( i == match );
4078 #endif /* LDAP_SLAPI */
4081 int slapi_entry_add_rdn_values( Slapi_Entry *e )
4087 rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
4088 if ( rc != LDAP_SUCCESS ) {
4092 if ( dn[0] != NULL ) {
4093 LDAPRDN rdn = dn[0];
4094 struct berval *vals[2];
4096 for ( i = 0; rdn[i] != NULL; i++ ) {
4097 LDAPAVA *ava = &rdn[0][i];
4098 Slapi_Attr *a = NULL;
4100 if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
4101 slapi_attr_value_find( a, &ava->la_value ) == 0 )
4104 vals[0] = &ava->la_value;
4107 slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
4113 return LDAP_SUCCESS;
4116 #endif /* LDAP_SLAPI */
4119 const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
4124 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
4125 if ( attr == NULL ) {
4129 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
4130 return slapi_value_get_string( &attr->a_vals[0] );
4132 #endif /* LDAP_SLAPI */
4137 void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
4142 attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
4144 bv.bv_val = uniqueid;
4145 bv.bv_len = strlen( uniqueid );
4146 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
4147 #endif /* LDAP_SLAPI */
4150 LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
4158 size = sizeof("ldap:///");
4161 size += strlen( ldaphost );
4162 if ( ldapport != 0 )
4165 url = slapi_ch_malloc( size );
4167 if ( ldapport != 0 ) {
4168 sprintf( url, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
4170 sprintf( url, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
4173 rc = ldap_initialize( &ld, url );
4175 slapi_ch_free_string( &url );
4177 return ( rc == LDAP_SUCCESS ) ? ld : NULL;
4180 #endif /* LDAP_SLAPI */
4183 void slapi_ldap_unbind( LDAP *ld )
4187 #endif /* LDAP_SLAPI */
4190 int slapi_x_backend_get_flags( const Slapi_Backend *be, unsigned long *flags )
4194 return LDAP_PARAM_ERROR;
4196 *flags = SLAP_DBFLAGS(be);
4198 return LDAP_SUCCESS;
4201 #endif /* LDAP_SLAPI */