2 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * (C) Copyright IBM Corp. 1997,2002
7 * Redistribution and use in source and binary forms are permitted
8 * provided that this notice is preserved and that due credit is
9 * given to IBM Corporation. This software is provided ``as is''
10 * without express or implied warranty.
13 * Portions (C) Copyright PADL Software Pty Ltd. 2003
14 * Redistribution and use in source and binary forms are permitted
15 * provided that this notice is preserved and that due credit is
16 * given to PADL Software Pty Ltd. This software is provided ``as is''
17 * without express or implied warranty.
22 #include <ac/string.h>
23 #include <ac/stdarg.h>
25 #include <ac/unistd.h>
31 struct berval *ns_get_supported_extop( int );
34 #include <sys/systeminfo.h>
40 * server start time (should we use a struct timeval also in slapd?
42 static struct timeval base_time;
43 ldap_pvt_thread_mutex_t slapi_hn_mutex;
44 ldap_pvt_thread_mutex_t slapi_time_mutex;
47 ldap_pvt_thread_mutex_t mutex;
50 struct slapi_condvar {
51 ldap_pvt_thread_cond_t cond;
52 ldap_pvt_thread_mutex_t mutex;
56 * This function converts an array of pointers to berval objects to
57 * an array of berval objects.
62 struct berval **bvptr,
65 int rc = LDAP_SUCCESS;
69 if ( bvptr == NULL || *bvptr == NULL ) {
73 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
77 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
78 if ( tmpberval == NULL ) {
79 return LDAP_NO_MEMORY;
82 for ( i = 0; bvptr[i] != NULL; i++ ) {
83 tmpberval[i].bv_val = bvptr[i]->bv_val;
84 tmpberval[i].bv_len = bvptr[i]->bv_len;
86 tmpberval[i].bv_val = NULL;
87 tmpberval[i].bv_len = 0;
89 if ( rc == LDAP_SUCCESS ) {
102 Slapi_Entry *e = NULL;
105 pTmpS = slapi_ch_strdup( s );
106 if ( pTmpS != NULL ) {
107 e = str2entry( pTmpS );
108 slapi_ch_free( (void **)&pTmpS );
114 #endif /* LDAP_SLAPI */
125 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
126 ret = entry2str( e, len );
127 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
130 #else /* LDAP_SLAPI */
132 #endif /* LDAP_SLAPI */
136 slapi_entry_get_dn( Slapi_Entry *e )
139 return e->e_name.bv_val;
140 #else /* LDAP_SLAPI */
142 #endif /* LDAP_SLAPI */
146 slapi_x_entry_get_id( Slapi_Entry *e )
152 #endif /* LDAP_SLAPI */
161 struct berval dn = { 0, NULL };
164 dn.bv_len = strlen( ldn );
166 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
167 #endif /* LDAP_SLAPI */
171 slapi_entry_dup( Slapi_Entry *e )
178 tmp = slapi_entry2str( e, &len );
180 return (Slapi_Entry *)NULL;
183 tmpEnt = (Slapi_Entry *)str2entry( tmp );
184 if ( tmpEnt == NULL ) {
185 slapi_ch_free( (void **)&tmp );
186 return (Slapi_Entry *)NULL;
190 slapi_ch_free( (void **)&tmp );
194 #else /* LDAP_SLAPI */
196 #endif /* LDAP_SLAPI */
200 slapi_entry_attr_delete(
205 AttributeDescription *ad = NULL;
208 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
209 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
212 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
213 return 0; /* attribute is deleted */
215 return -1; /* something went wrong */
217 #else /* LDAP_SLAPI */
219 #endif /* LDAP_SLAPI */
223 slapi_entry_alloc( void )
226 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
227 #else /* LDAP_SLAPI */
229 #endif /* LDAP_SLAPI */
233 slapi_entry_free( Slapi_Entry *e )
237 #endif /* LDAP_SLAPI */
241 slapi_entry_attr_merge(
244 struct berval **vals )
247 AttributeDescription *ad = NULL;
252 rc = bvptr2obj( vals, &bv );
253 if ( rc != LDAP_SUCCESS ) {
257 rc = slap_str2ad( type, &ad, &text );
258 if ( rc != LDAP_SUCCESS ) {
262 rc = attr_merge( e, ad, bv );
266 #else /* LDAP_SLAPI */
268 #endif /* LDAP_SLAPI */
272 slapi_entry_attr_find(
278 AttributeDescription *ad = NULL;
282 rc = slap_str2ad( type, &ad, &text );
283 if ( rc != LDAP_SUCCESS ) {
287 *attr = attr_find( e->e_attrs, ad );
288 if ( *attr == NULL ) {
293 #else /* LDAP_SLAPI */
295 #endif /* LDAP_SLAPI */
299 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
302 AttributeDescription *ad = NULL;
307 rc = slap_str2ad( type, &ad, &text );
308 if ( rc != LDAP_SUCCESS ) {
312 attr = attr_find( e->e_attrs, ad );
313 if ( attr == NULL ) {
317 if ( attr->a_vals != NULL && attr->a_vals[0].bv_val != NULL ) {
318 return slapi_ch_strdup( attr->a_vals[0].bv_val );
328 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
331 AttributeDescription *ad = NULL;
336 rc = slap_str2ad( type, &ad, &text );
337 if ( rc != LDAP_SUCCESS ) {
341 attr = attr_find( e->e_attrs, ad );
342 if ( attr == NULL ) {
346 return slapi_value_get_int( attr->a_vals );
353 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
356 AttributeDescription *ad = NULL;
361 rc = slap_str2ad( type, &ad, &text );
362 if ( rc != LDAP_SUCCESS ) {
366 attr = attr_find( e->e_attrs, ad );
367 if ( attr == NULL ) {
371 return slapi_value_get_long( attr->a_vals );
378 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
381 AttributeDescription *ad = NULL;
386 rc = slap_str2ad( type, &ad, &text );
387 if ( rc != LDAP_SUCCESS ) {
391 attr = attr_find( e->e_attrs, ad );
392 if ( attr == NULL ) {
396 return slapi_value_get_uint( attr->a_vals );
403 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
406 AttributeDescription *ad = NULL;
411 rc = slap_str2ad( type, &ad, &text );
412 if ( rc != LDAP_SUCCESS ) {
416 attr = attr_find( e->e_attrs, ad );
417 if ( attr == NULL ) {
421 return slapi_value_get_ulong( attr->a_vals );
428 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
432 AttributeDescription *ad = NULL;
437 rc = slap_str2ad( type, &ad, &text );
438 if ( rc != LDAP_SUCCESS ) {
442 attr = attr_find( e->e_attrs, ad );
443 if ( attr == NULL ) {
447 bv.bv_val = (char *)value;
448 bv.bv_len = strlen( value );
450 return ( slapi_attr_value_find( attr, &bv ) != -1 );
457 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
460 AttributeDescription *ad = NULL;
465 rc = slap_str2ad( type, &ad, &text );
466 if ( rc != LDAP_SUCCESS ) {
470 attr_delete ( &e->e_attrs, ad );
471 if ( value != NULL ) {
472 bv.bv_val = (char *)value;
473 bv.bv_len = strlen(value);
474 attr_merge_one ( e, ad, &bv );
476 #endif /* LDAP_SLAPI */
480 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
485 snprintf( buf, sizeof( buf ), "%d", l );
486 slapi_entry_attr_set_charptr( e, type, buf );
487 #endif /* LDAP_SLAPI */
491 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
496 snprintf( buf, sizeof( buf ), "%u", l );
497 slapi_entry_attr_set_charptr( e, type, buf );
498 #endif /* LDAP_SLAPI */
502 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
507 snprintf( buf, sizeof( buf ), "%ld", l );
508 slapi_entry_attr_set_charptr( e, type, buf );
509 #endif /* LDAP_SLAPI */
513 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
518 snprintf( buf, sizeof( buf ), "%lu", l );
519 slapi_entry_attr_set_charptr( e, type, buf );
520 #endif /* LDAP_SLAPI */
524 slapi_is_rootdse( const char *dn )
527 return ( dn == NULL || dn[0] == '\0' );
534 * Add values to entry.
537 * LDAP_SUCCESS Values added to entry
538 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
539 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
542 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
548 char textbuf[SLAP_TEXT_BUFLEN];
550 mod.sm_op = LDAP_MOD_ADD;
552 mod.sm_type.bv_val = (char *)type;
553 mod.sm_type.bv_len = strlen( type );
555 rc = slap_str2ad( type, &mod.sm_desc, &text );
556 if ( rc != LDAP_SUCCESS ) {
560 if ( vals == NULL ) {
561 /* Apparently vals can be NULL */
562 mod.sm_bvalues = (BerVarray)ch_malloc( sizeof(struct berval) );
563 mod.sm_bvalues->bv_val = NULL;
565 rc = bvptr2obj( vals, &mod.sm_bvalues );
566 if ( rc != LDAP_SUCCESS ) {
567 return LDAP_CONSTRAINT_VIOLATION;
571 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
573 ch_free( mod.sm_bvalues );
575 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
578 #endif /* LDAP_SLAPI */
582 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
585 return slapi_entry_add_values( e, type, vals );
588 #endif /* LDAP_SLAPI */
592 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
595 AttributeDescription *ad = NULL;
599 rc = slap_str2ad( type, &ad, &text );
600 if ( rc != LDAP_SUCCESS ) {
604 return attr_merge( e, ad, *vs );
607 #endif /* LDAP_SLAPI */
611 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
617 char textbuf[SLAP_TEXT_BUFLEN];
619 mod.sm_op = LDAP_MOD_DELETE;
621 mod.sm_type.bv_val = (char *)type;
622 mod.sm_type.bv_len = strlen( type );
624 if ( vals == NULL ) {
625 /* If vals is NULL, this is a NOOP. */
629 rc = slap_str2ad( type, &mod.sm_desc, &text );
630 if ( rc != LDAP_SUCCESS ) {
634 if ( vals[0] == NULL ) {
635 /* SLAPI doco says LDAP_OPERATIONS_ERROR */
636 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OPERATIONS_ERROR : LDAP_SUCCESS;
639 rc = bvptr2obj( vals, &mod.sm_bvalues );
640 if ( rc != LDAP_SUCCESS ) {
641 return LDAP_CONSTRAINT_VIOLATION;
644 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
646 ch_free( mod.sm_bvalues );
651 #endif /* LDAP_SLAPI */
655 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
658 return slapi_entry_delete_values( e, type, vals );
661 #endif /* LDAP_SLAPI */
665 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
668 return slapi_entry_attr_merge( e, (char *)type, vals );
671 #endif /* LDAP_SLAPI */
675 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
678 AttributeDescription *ad = NULL;
682 rc = slap_str2ad( type, &ad, &text );
683 if ( rc != LDAP_SUCCESS ) {
687 rc = attr_merge_one( e, ad, (Slapi_Value *)value );
688 if ( rc != LDAP_SUCCESS ) {
695 #endif /* LDAP_SLAPI */
699 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
704 val.bv_val = (char *)value;
705 val.bv_len = strlen( value );
707 return slapi_entry_add_value( e, type, &val );
710 #endif /* LDAP_SLAPI */
714 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
717 Slapi_Value *vals[2];
720 val.bv_val = (char *)value;
721 val.bv_len = strlen( value );
725 return slapi_entry_delete_values_sv( e, type, vals );
728 #endif /* LDAP_SLAPI */
733 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
736 return slapi_entry_attr_merge( e, (char *)type, vals );
743 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
752 return ( *attr != NULL ) ? 0 : -1;
759 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
766 if ( prevattr == NULL ) {
770 *attr = prevattr->a_next;
772 return ( *attr != NULL ) ? 0 : -1;
779 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
782 AttributeDescription *ad = NULL;
787 rc = slap_str2ad( type, &ad, &text );
788 if ( rc != LDAP_SUCCESS ) {
792 attr_delete( &e->e_attrs, ad );
794 rc = bvptr2obj( vals, &bv );
795 if ( rc != LDAP_SUCCESS ) {
799 rc = attr_merge( e, ad, bv );
800 slapi_ch_free( (void **)&bv );
801 if ( rc != LDAP_SUCCESS ) {
808 #endif /* LDAP_SLAPI */
812 * FIXME -- The caller must free the allocated memory.
813 * In Netscape they do not have to.
816 slapi_attr_get_values(
818 struct berval ***vals )
824 if ( attr == NULL ) {
828 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
832 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
833 for ( j = 0; j < i; j++ ) {
834 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
838 *vals = (struct berval **)bv;
841 #else /* LDAP_SLAPI */
843 #endif /* LDAP_SLAPI */
847 slapi_dn_normalize( char *dn )
853 assert( dn != NULL );
856 bdn.bv_len = strlen( dn );
858 dnNormalize2( NULL, &bdn, &ndn );
861 * FIXME: ain't it safe to set dn = ndn.bv_val ?
863 dn = ch_strdup( ndn.bv_val );
864 ch_free( ndn.bv_val );
867 #else /* LDAP_SLAPI */
869 #endif /* LDAP_SLAPI */
873 * FIXME: this function is dangerous and should be deprecated;
874 * DN normalization is a lot more than lower-casing, and BTW
875 * OpenLDAP's DN normalization for case insensitive attributes
876 * is already lower case
879 slapi_dn_normalize_case( char *dn )
882 slapi_dn_normalize( dn );
883 ldap_pvt_str2lower( dn );
886 #else /* LDAP_SLAPI */
888 #endif /* LDAP_SLAPI */
897 struct berval bdn, ndn;
898 struct berval bsuffix, nsuffix;
900 assert( dn != NULL );
901 assert( suffix != NULL );
904 bdn.bv_len = strlen( dn );
906 bsuffix.bv_val = suffix;
907 bsuffix.bv_len = strlen( suffix );
909 dnNormalize2( NULL, &bdn, &ndn );
910 dnNormalize2( NULL, &bsuffix, &nsuffix );
912 return dnIsSuffix( &ndn, &nsuffix );
913 #else /* LDAP_SLAPI */
915 #endif /* LDAP_SLAPI */
919 slapi_dn_ignore_case( char *dn )
922 return slapi_dn_normalize_case( dn );
923 #else /* LDAP_SLAPI */
925 #endif /* LDAP_SLAPI */
929 slapi_ch_malloc( unsigned long size )
932 return ch_malloc( size );
933 #else /* LDAP_SLAPI */
935 #endif /* LDAP_SLAPI */
939 slapi_ch_free( void **ptr )
944 #endif /* LDAP_SLAPI */
948 slapi_ch_free_string( char **ptr )
951 slapi_ch_free( (void **)ptr );
952 #endif /* LDAP_SLAPI */
956 slapi_ch_array_free( char **arrayp )
961 if ( arrayp != NULL ) {
962 for ( p = arrayp; *p != NULL; p++ ) {
963 slapi_ch_free( (void **)p );
965 slapi_ch_free( (void **)&arrayp );
971 slapi_ch_bvdup(const struct berval *v)
976 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
977 bv->bv_len = v->bv_len;
978 bv->bv_val = slapi_ch_malloc( bv->bv_len );
979 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
988 slapi_ch_bvecdup(const struct berval **v)
998 for ( i = 0; v[i] != NULL; i++ )
1001 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1003 for ( i = 0; v[i] != NULL; i++ ) {
1004 rv[i] = slapi_ch_bvdup( v[i] );
1016 unsigned long nelem,
1017 unsigned long size )
1020 return ch_calloc( nelem, size );
1021 #else /* LDAP_SLAPI */
1023 #endif /* LDAP_SLAPI */
1029 unsigned long size )
1032 return ch_realloc( block, size );
1033 #else /* LDAP_SLAPI */
1035 #endif /* LDAP_SLAPI */
1039 slapi_ch_strdup( char *s )
1042 return ch_strdup( (const char *)s );
1043 #else /* LDAP_SLAPI */
1045 #endif /* LDAP_SLAPI */
1049 slapi_ch_stlen( char *s )
1052 return strlen( (const char *)s );
1053 #else /* LDAP_SLAPI */
1055 #endif /* LDAP_SLAPI */
1059 slapi_control_present(
1060 LDAPControl **controls,
1062 struct berval **val,
1077 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1078 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1083 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1085 * FIXME: according to 6.1 specification,
1086 * "The val output parameter is set
1087 * to point into the controls array.
1088 * A copy of the control value is
1092 struct berval *pTmpBval;
1094 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
1095 if ( pTmpBval == NULL ) {
1098 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
1099 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
1103 slapi_ch_free( (void **)&pTmpBval );
1109 *val = &controls[i]->ldctl_value;
1114 *iscritical = controls[i]->ldctl_iscritical;
1121 #else /* LDAP_SLAPI */
1123 #endif /* LDAP_SLAPI */
1127 slapi_register_supported_control(
1129 unsigned long controlops )
1132 /* FIXME -- can not add controls to OpenLDAP dynamically */
1133 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_CONTROLS",
1134 "OpenLDAP does not support dynamic registration of LDAP controls\n" );
1135 #endif /* LDAP_SLAPI */
1139 slapi_get_supported_controls(
1141 unsigned long **ctrlopsp )
1147 unsigned long *masks = NULL;
1149 for (n = 0; get_supported_ctrl( n ) != NULL; n++) {
1157 return LDAP_SUCCESS;
1161 oids = (char **)slapi_ch_malloc( (n + 1) * sizeof(char *) );
1162 if ( oids == NULL ) {
1163 rc = LDAP_NO_MEMORY;
1167 masks = (unsigned long *)slapi_ch_malloc( n * sizeof(int) );
1168 if ( masks == NULL ) {
1169 rc = LDAP_NO_MEMORY;
1173 for ( i = 0; i < n; i++ ) {
1175 * FIXME: Netscape's specification says nothing about
1176 * memory; should we copy the OIDs or return pointers
1177 * to internal values? In OpenLDAP the latter is safe
1178 * since we do not allow to register coltrols runtime
1180 oids[ i ] = ch_strdup( get_supported_ctrl( i ) );
1181 if ( oids[ i ] == NULL ) {
1182 rc = LDAP_NO_MEMORY;
1185 masks[ i ] = (unsigned long)get_supported_ctrl_mask( i );
1190 return LDAP_SUCCESS;
1193 if ( rc != LDAP_SUCCESS ) {
1194 for ( i = 0; oids != NULL && oids[ i ] != NULL; i++ ) {
1195 ch_free( oids[ i ] );
1202 #else /* LDAP_SLAPI */
1204 #endif /* LDAP_SLAPI */
1208 slapi_register_supported_saslmechanism( char *mechanism )
1211 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1212 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
1213 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1214 #endif /* LDAP_SLAPI */
1218 slapi_get_supported_saslmechanisms( void )
1221 /* FIXME -- can not get the saslmechanism wihtout a connection. */
1222 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
1223 "can not get the saslmechanism "
1224 "wihtout a connection\n" );
1226 #else /* LDAP_SLAPI */
1228 #endif /* LDAP_SLAPI */
1232 slapi_get_supported_extended_ops( void )
1236 char **ppExtOpOID = NULL;
1239 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1243 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
1248 if ( numExtOps == 0 ) {
1252 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1253 for ( k = 0; k < i; k++ ) {
1256 bv = get_supported_extop( k );
1257 assert( bv != NULL );
1259 ppExtOpOID[ k ] = bv->bv_val;
1262 for ( ; k < j; k++ ) {
1265 bv = ns_get_supported_extop( k );
1266 assert( bv != NULL );
1268 ppExtOpOID[ i + k ] = bv->bv_val;
1270 ppExtOpOID[ i + k ] = NULL;
1273 #else /* LDAP_SLAPI */
1275 #endif /* LDAP_SLAPI */
1279 slapi_send_ldap_result(
1285 struct berval **urls )
1291 char *extOID = NULL;
1292 struct berval *extValue = NULL;
1295 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1296 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1297 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1298 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, &s );
1299 rc = LDAP_SASL_BIND_IN_PROGRESS;
1300 send_ldap_sasl( conn, op, rc, NULL, NULL, NULL, NULL, s );
1304 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1305 if ( extOID != NULL ) {
1306 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &extValue );
1307 slapi_send_ldap_extended_response( conn, op, err, extOID,
1312 send_ldap_result( conn, op, err, matched, text, NULL, NULL );
1313 #endif /* LDAP_SLAPI */
1317 slapi_send_ldap_search_entry(
1320 LDAPControl **ectrls,
1331 AttributeName *an = NULL;
1334 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1339 an = (AttributeName *) ch_malloc( i * sizeof(AttributeName) );
1340 for ( i = 0; attrs[i] != NULL; i++ ) {
1341 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1342 an[i].an_name.bv_len = strlen( attrs[i] );
1343 an[i].an_desc = NULL;
1344 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1349 if ( ( rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) ||
1350 ( rc = slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&pConn) != 0 ) ||
1351 ( rc = slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp) != 0 ) ) {
1354 rc = send_search_entry( be, pConn, pOp, e, an, attrsonly, NULL );
1359 #else /* LDAP_SLAPI */
1361 #endif /* LDAP_SLAPI */
1366 slapi_str2filter( char *str )
1369 return str2filter( str );
1370 #else /* LDAP_SLAPI */
1372 #endif /* LDAP_SLAPI */
1382 #endif /* LDAP_SLAPI */
1386 slapi_filter_dup( Slapi_Filter *filter )
1391 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1393 f->f_choice = filter->f_choice;
1395 switch ( f->f_choice ) {
1396 case LDAP_FILTER_AND:
1397 case LDAP_FILTER_NOT:
1398 case LDAP_FILTER_OR: {
1399 Filter *pFilter, **ppF;
1401 for ( pFilter = filter->f_list, ppF = &f->f_list;
1403 pFilter = pFilter->f_next, ppF = &f->f_next )
1405 *ppF = slapi_filter_dup( pFilter );
1411 case LDAP_FILTER_PRESENT:
1412 f->f_desc = filter->f_desc;
1414 case LDAP_FILTER_EQUALITY:
1415 case LDAP_FILTER_GE:
1416 case LDAP_FILTER_LE:
1417 case LDAP_FILTER_APPROX:
1418 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1419 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1420 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1422 case LDAP_FILTER_EXT:
1423 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1424 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1425 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1426 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1427 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1428 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1429 case LDAP_FILTER_SUBSTRINGS: {
1432 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1433 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1434 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1436 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1437 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1438 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1440 f->f_sub_any[i].bv_val = NULL;
1441 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1444 case SLAPD_FILTER_COMPUTED:
1445 f->f_result = filter->f_result;
1448 slapi_ch_free( (void **)&f );
1456 #endif /* LDAP_SLAPI */
1460 slapi_filter_get_choice( Slapi_Filter *f )
1472 #else /* LDAP_SLAPI */
1473 return -1; /* invalid filter type */
1474 #endif /* LDAP_SLAPI */
1478 slapi_filter_get_ava(
1481 struct berval **bval )
1485 int rc = LDAP_SUCCESS;
1487 assert( type != NULL );
1488 assert( bval != NULL );
1493 ftype = f->f_choice;
1494 if ( ftype == LDAP_FILTER_EQUALITY
1495 || ftype == LDAP_FILTER_GE
1496 || ftype == LDAP_FILTER_LE
1497 || ftype == LDAP_FILTER_APPROX ) {
1499 * According to the SLAPI Reference Manual these are
1502 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1503 *bval = &f->f_un.f_un_ava->aa_value;
1504 } else { /* filter type not supported */
1509 #else /* LDAP_SLAPI */
1511 #endif /* LDAP_SLAPI */
1515 slapi_filter_list_first( Slapi_Filter *f )
1524 ftype = f->f_choice;
1525 if ( ftype == LDAP_FILTER_AND
1526 || ftype == LDAP_FILTER_OR
1527 || ftype == LDAP_FILTER_NOT ) {
1528 return (Slapi_Filter *)f->f_and;
1532 #else /* LDAP_SLAPI */
1534 #endif /* LDAP_SLAPI */
1538 slapi_filter_list_next(
1540 Slapi_Filter *fprev )
1549 ftype = f->f_choice;
1550 if ( ftype == LDAP_FILTER_AND
1551 || ftype == LDAP_FILTER_OR
1552 || ftype == LDAP_FILTER_NOT )
1554 return fprev->f_next;
1558 #else /* LDAP_SLAPI */
1560 #endif /* LDAP_SLAPI */
1564 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1571 switch ( f->f_choice ) {
1572 case LDAP_FILTER_GE:
1573 case LDAP_FILTER_LE:
1574 case LDAP_FILTER_EQUALITY:
1575 case LDAP_FILTER_APPROX:
1576 *type = f->f_av_desc->ad_cname.bv_val;
1578 case LDAP_FILTER_SUBSTRINGS:
1579 *type = f->f_sub_desc->ad_cname.bv_val;
1581 case LDAP_FILTER_PRESENT:
1582 *type = f->f_desc->ad_cname.bv_val;
1584 case LDAP_FILTER_EXT:
1585 *type = f->f_mr_desc->ad_cname.bv_val;
1588 /* Complex filters need not apply. */
1596 #endif /* LDAP_SLAPI */
1600 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1601 char ***any, char **final )
1606 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1611 * The caller shouldn't free but we can't return an
1612 * array of char *s from an array of bervals without
1613 * allocating memory, so we may as well be consistent.
1616 *type = f->f_sub_desc->ad_cname.bv_val;
1617 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1618 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1620 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1621 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1622 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1625 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1630 #endif /* LDAP_SLAPI */
1634 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2)
1637 Slapi_Filter *f = NULL;
1639 if ( ftype == LDAP_FILTER_AND ||
1640 ftype == LDAP_FILTER_OR ||
1641 ftype == LDAP_FILTER_NOT )
1643 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1644 f->f_choice = ftype;
1652 #endif /* LDAP_SLAPI */
1656 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1666 /* spec says return zero if no filter. */
1670 if ( verify_access ) {
1671 (void) slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be);
1672 rc = slapi_pblock_get(pb, SLAPI_CONNECTION, (void *)&conn);
1674 return LDAP_PARAM_ERROR;
1676 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
1678 return LDAP_PARAM_ERROR;
1685 * According to acl.c it is safe to call test_filter() with
1688 rc = test_filter( be, conn, op, e, f );
1690 case LDAP_COMPARE_TRUE:
1693 case LDAP_COMPARE_FALSE:
1695 case SLAPD_COMPARE_UNDEFINED:
1698 case LDAP_PROTOCOL_ERROR:
1699 /* filter type unknown: spec says return -1 */
1707 #endif /* LDAP_SLAPI */
1711 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
1714 return slapi_filter_test( NULL, e, f, 0 );
1721 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
1724 switch ( f->f_choice ) {
1725 case LDAP_FILTER_AND:
1726 case LDAP_FILTER_NOT:
1727 case LDAP_FILTER_OR: {
1731 * FIXME: altering f; should we use a temporary?
1733 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1734 rc = slapi_filter_apply( f, fn, arg, error_code );
1738 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
1744 case LDAP_FILTER_EQUALITY:
1745 case LDAP_FILTER_SUBSTRINGS:
1746 case LDAP_FILTER_GE:
1747 case LDAP_FILTER_LE:
1748 case LDAP_FILTER_PRESENT:
1749 case LDAP_FILTER_APPROX:
1750 case LDAP_FILTER_EXT:
1751 *error_code = fn( f, arg );
1754 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1757 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
1758 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
1764 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1766 #endif /* LDAP_SLAPI */
1770 slapi_send_ldap_extended_response(
1775 struct berval *response )
1778 send_ldap_extended( conn,op, errornum, NULL, NULL, NULL,
1779 respName,response, NULL );
1780 return LDAP_SUCCESS;
1781 #else /* LDAP_SLAPI */
1783 #endif /* LDAP_SLAPI */
1788 struct berval **vals,
1793 * FIXME: what's the point?
1796 #else /* LDAP_SLAPI */
1798 #endif /* LDAP_SLAPI */
1801 #define MAX_HOSTNAME 512
1804 slapi_get_hostname( void )
1810 * FIXME: I'd prefer a different check ...
1813 hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1815 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1816 "can't malloc memory for hostname\n" );
1819 } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
1820 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1821 "can't get hostname\n" );
1822 slapi_ch_free( (void **)&hn );
1826 static int been_here = 0;
1827 static char *static_hn = NULL;
1829 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1831 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1832 if ( static_hn == NULL) {
1833 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1834 "can't malloc memory for hostname\n" );
1836 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1841 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1842 slapi_log_error( SLAPI_LOG_FATAL,
1844 "can't get hostname\n" );
1845 slapi_ch_free( (void **)&static_hn );
1847 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1856 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1858 hn = ch_strdup( static_hn );
1859 #endif /* !_SPARC */
1862 #else /* LDAP_SLAPI */
1864 #endif /* LDAP_SLAPI */
1868 * FIXME: this should go in an appropriate header ...
1870 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
1880 int rc = LDAP_SUCCESS;
1883 va_start( arglist, fmt );
1884 rc = vLogError( severity, subsystem, fmt, arglist );
1888 #else /* LDAP_SLAPI */
1890 #endif /* LDAP_SLAPI */
1895 slapi_timer_current_time( void )
1898 static int first_time = 1;
1899 #if !defined (_WIN32)
1903 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1906 gettimeofday( &base_time, NULL );
1908 gettimeofday( &now, NULL );
1909 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1910 (now.tv_usec - base_time.tv_usec);
1911 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1917 return (slap_get_time() - starttime) * 1000000;
1924 performance_counter_present = QueryPerformanceCounter( &base_time );
1925 QueryPerformanceFrequency( &performance_freq );
1928 if ( !performance_counter_present )
1931 QueryPerformanceCounter( &now );
1932 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1934 #else /* LDAP_SLAPI */
1936 #endif /* LDAP_SLAPI */
1943 slapi_timer_get_time( char *label )
1946 unsigned long start = slapi_timer_current_time();
1947 printf("%10ld %10ld usec %s\n", start, 0, label);
1949 #else /* LDAP_SLAPI */
1951 #endif /* LDAP_SLAPI */
1958 slapi_timer_elapsed_time(
1960 unsigned long start )
1963 unsigned long stop = slapi_timer_current_time();
1964 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
1965 #endif /* LDAP_SLAPI */
1969 slapi_free_search_results_internal( Slapi_PBlock *pb )
1972 Slapi_Entry **entries;
1973 int k = 0, nEnt = 0;
1975 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
1976 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
1981 if ( entries == NULL ) {
1985 for ( k = 0; k < nEnt; k++ ) {
1986 slapi_entry_free( entries[k] );
1989 slapi_ch_free( (void **)&entries );
1990 #endif /* LDAP_SLAPI */
1994 * Internal API to prime a Slapi_PBlock with a Backend.
1996 int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
2001 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2002 if ( rc != LDAP_SUCCESS )
2006 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2007 if ( rc != LDAP_SUCCESS )
2011 return LDAP_SUCCESS;
2014 #endif /* LDAP_SLAPI */
2019 * If oldStyle is TRUE, then a value suitable for setting to
2020 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
2021 * (pointer to static storage).
2023 * If oldStyle is FALSE, then a value suitable for setting to
2024 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2025 * a pointer to allocated memory and will include the SASL
2026 * mechanism (if any).
2028 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2033 switch ( authz->sai_method ) {
2034 case LDAP_AUTH_SASL:
2036 authType = SLAPD_AUTH_SASL;
2038 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2039 authType = slapi_ch_malloc( len );
2040 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2043 case LDAP_AUTH_SIMPLE:
2044 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2046 case LDAP_AUTH_NONE:
2047 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2053 if ( is_tls && authType == NULL ) {
2054 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2062 * Internal API to prime a Slapi_PBlock with a Connection.
2064 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
2070 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2071 if ( rc != LDAP_SUCCESS )
2074 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2075 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2076 if ( rc != LDAP_SUCCESS )
2078 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2079 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2080 if ( rc != LDAP_SUCCESS )
2084 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2085 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2086 if ( rc != LDAP_SUCCESS )
2088 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2089 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2090 if ( rc != LDAP_SUCCESS )
2094 #ifdef LDAP_CONNECTIONLESS
2095 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2096 if ( rc != LDAP_SUCCESS )
2100 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2101 if ( rc != LDAP_SUCCESS )
2104 /* Returns pointer to static string */
2105 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
2106 if ( connAuthType != NULL ) {
2107 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2108 if ( rc != LDAP_SUCCESS )
2112 /* Returns pointer to allocated string */
2113 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
2114 if ( connAuthType != NULL ) {
2115 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2116 if ( rc != LDAP_SUCCESS )
2120 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
2121 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
2122 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
2123 if ( rc != LDAP_SUCCESS )
2130 #endif /* LDAP_SLAPI */
2134 * Internal API to prime a Slapi_PBlock with an Operation.
2136 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
2145 if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2149 isRoot = be_isroot( be, &op->o_ndn );
2150 isUpdateDn = be_isupdate( be, &op->o_ndn );
2153 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2154 if ( rc != LDAP_SUCCESS )
2157 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2158 if ( rc != LDAP_SUCCESS )
2161 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2162 if ( rc != LDAP_SUCCESS )
2165 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2166 if ( rc != LDAP_SUCCESS )
2169 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2170 if ( rc != LDAP_SUCCESS )
2173 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2174 if ( rc != LDAP_SUCCESS )
2177 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2178 if ( rc != LDAP_SUCCESS)
2181 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2182 if ( rc != LDAP_SUCCESS )
2185 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2186 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
2187 /* Not quite sure what the point of this is. */
2188 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
2189 if ( rc != LDAP_SUCCESS )
2193 return LDAP_SUCCESS;
2199 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2204 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2205 *isSSL = conn->c_is_tls;
2207 return LDAP_SUCCESS;
2210 #endif /* LDAP_SLAPI */
2214 * DS 5.x compatability API follow
2217 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2223 return LDAP_PARAM_ERROR;
2225 at = attr->a_desc->ad_type;
2227 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2229 if ( is_at_single_value( at ) )
2230 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2231 if ( is_at_operational( at ) )
2232 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2233 if ( is_at_obsolete( at ) )
2234 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2235 if ( is_at_collective( at ) )
2236 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2237 if ( is_at_no_user_mod( at ) )
2238 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2240 return LDAP_SUCCESS;
2243 #endif /* LDAP_SLAPI */
2246 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2249 unsigned long flags;
2251 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2253 return (flags & flag) ? 1 : 0;
2256 #endif /* LDAP_SLAPI */
2259 Slapi_Attr *slapi_attr_new( void )
2264 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2272 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2276 AttributeDescription *ad = NULL;
2278 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2293 void slapi_attr_free( Slapi_Attr **a )
2301 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2304 return attr_dup( (Slapi_Attr *)attr );
2310 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2313 return value_add_one( &a->a_vals, (Slapi_Value *)v );
2319 int slapi_attr_type2plugin( const char *type, void **pi )
2326 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2329 if ( attr == NULL ) {
2330 return LDAP_PARAM_ERROR;
2333 *type = attr->a_desc->ad_cname.bv_val;
2335 return LDAP_SUCCESS;
2341 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2344 if ( attr == NULL ) {
2345 return LDAP_PARAM_ERROR;
2347 *oidp = attr->a_desc->ad_type->sat_oid;
2349 return LDAP_SUCCESS;
2355 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2363 mr = a->a_desc->ad_type->sat_equality;
2364 rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
2365 (struct berval *)v1, (void *)v2, &text );
2366 if ( rc != LDAP_SUCCESS )
2369 return ( ret == 0 ) ? 0 : -1;
2375 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2385 mr = a->a_desc->ad_type->sat_equality;
2386 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2387 rc = value_match( &ret, a->a_desc, mr,
2388 SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
2389 if ( rc != LDAP_SUCCESS ) {
2400 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2403 AttributeDescription *a1 = NULL;
2404 AttributeDescription *a2 = NULL;
2408 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2412 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2416 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2417 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2418 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2421 case SLAPI_TYPE_CMP_EXACT:
2422 ret = ad_cmp( a1, a2 );
2424 case SLAPI_TYPE_CMP_BASE:
2425 ret = ad_base_cmp( a1, a2 );
2427 case SLAPI_TYPE_CMP_SUBTYPE:
2428 ret = is_ad_subtype( a2, a2 );
2441 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2444 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2450 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2453 return slapi_valueset_first_value( &a->a_vals, v );
2459 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2462 return slapi_valueset_next_value( &a->a_vals, hint, v );
2468 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2471 *numValues = slapi_valueset_count( &a->a_vals );
2479 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2482 *vs = &((Slapi_Attr *)a)->a_vals;
2490 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2493 return slapi_attr_get_values( a, vals );
2499 char *slapi_attr_syntax_normalize( const char *s )
2502 AttributeDescription *ad = NULL;
2505 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2509 return ad->ad_cname.bv_val;
2515 Slapi_Value *slapi_value_new( void )
2520 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2528 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2531 return ber_dupbv( NULL, (struct berval *)bval );
2537 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2540 return slapi_value_new_berval( v );
2546 Slapi_Value *slapi_value_new_string(const char *s)
2551 bv.bv_val = (char *)s;
2552 bv.bv_len = strlen( s );
2554 return slapi_value_new_berval( &bv );
2560 Slapi_Value *slapi_value_init(Slapi_Value *val)
2572 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2575 return ber_dupbv( v, bval );
2581 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2584 v->bv_val = slapi_ch_strdup( (char *)s );
2585 v->bv_len = strlen( s );
2593 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2596 return slapi_value_new_value( v );
2602 void slapi_value_free(Slapi_Value **value)
2605 if ( value == NULL ) {
2609 if ( (*value) != NULL ) {
2610 slapi_ch_free( (void **)&(*value)->bv_val );
2611 slapi_ch_free( (void **)value );
2616 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2625 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2628 if ( value == NULL ) {
2631 if ( value->bv_val != NULL ) {
2632 slapi_ch_free( (void **)&value->bv_val );
2634 slapi_value_init_berval( value, (struct berval *)bval );
2642 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2645 if ( value == NULL ) {
2648 return slapi_value_set_berval( value, vfrom );
2654 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2657 if ( value == NULL ) {
2660 if ( value->bv_val != NULL ) {
2661 slapi_ch_free( (void **)&value->bv_val );
2663 value->bv_val = slapi_ch_malloc( len );
2664 value->bv_len = len;
2665 AC_MEMCPY( value->bv_val, val, len );
2673 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2676 if ( value == NULL ) {
2679 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2686 int slapi_value_set_int(Slapi_Value *value, int intVal)
2691 snprintf( buf, sizeof( buf ), "%d", intVal );
2693 return slapi_value_set_string( value, buf );
2699 const char *slapi_value_get_string(const Slapi_Value *value)
2702 if ( value == NULL ) {
2705 return value->bv_val;
2712 static int checkBVString(const struct berval *bv)
2716 for ( i = 0; i < bv->bv_len; i++ ) {
2717 if ( bv->bv_val[i] == '\0' )
2720 if ( bv->bv_val[i] != '\0' )
2727 int slapi_value_get_int(const Slapi_Value *value)
2730 if ( value == NULL ) return 0;
2731 if ( value->bv_val == NULL ) return 0;
2732 if ( !checkBVString( value ) ) return 0;
2734 return (int)strtol( value->bv_val, NULL, 10 );
2740 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2743 if ( value == NULL ) return 0;
2744 if ( value->bv_val == NULL ) return 0;
2745 if ( !checkBVString( value ) ) return 0;
2747 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2753 long slapi_value_get_long(const Slapi_Value *value)
2756 if ( value == NULL ) return 0;
2757 if ( value->bv_val == NULL ) return 0;
2758 if ( !checkBVString( value ) ) return 0;
2760 return strtol( value->bv_val, NULL, 10 );
2766 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2769 if ( value == NULL ) return 0;
2770 if ( value->bv_val == NULL ) return 0;
2771 if ( !checkBVString( value ) ) return 0;
2773 return strtoul( value->bv_val, NULL, 10 );
2779 size_t slapi_value_get_length(const Slapi_Value *value)
2782 if ( value == NULL )
2785 return (size_t) value->bv_len;
2791 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2794 return slapi_attr_value_cmp( a, v1, v2 );
2800 /* A ValueSet is a container for a BerVarray. */
2801 Slapi_ValueSet *slapi_valueset_new( void )
2806 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2815 void slapi_valueset_free(Slapi_ValueSet *vs)
2821 ber_bvarray_free( vp );
2822 slapi_ch_free( (void **)&vp );
2829 void slapi_valueset_init(Slapi_ValueSet *vs)
2832 if ( vs != NULL && *vs == NULL ) {
2833 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2834 (*vs)->bv_val = NULL;
2840 void slapi_valueset_done(Slapi_ValueSet *vs)
2848 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
2850 slapi_ch_free( (void **)&vp->bv_val );
2852 /* but don't free *vs or vs */
2856 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2861 ber_dupbv( &bv, (Slapi_Value *)addval );
2862 ber_bvarray_add( vs, &bv );
2866 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2869 return slapi_valueset_next_value( vs, 0, v );
2875 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2886 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2897 int slapi_valueset_count( const Slapi_ValueSet *vs )
2908 for ( i = 0; vp[i].bv_val != NULL; i++ )
2918 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2923 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2924 slapi_valueset_add_value( vs1, vp );
2929 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
2930 struct berval *val, int access )
2937 slap_access_t slap_access;
2938 AttributeDescription *ad = NULL;
2941 ret = slap_str2ad( attr, &ad, &text );
2942 if ( ret != LDAP_SUCCESS ) {
2946 switch ( access & SLAPI_ACL_ALL ) {
2947 case SLAPI_ACL_COMPARE:
2948 slap_access = ACL_COMPARE;
2950 case SLAPI_ACL_SEARCH:
2951 slap_access = ACL_SEARCH;
2953 case SLAPI_ACL_READ:
2954 slap_access = ACL_READ;
2956 case SLAPI_ACL_WRITE:
2957 case SLAPI_ACL_DELETE:
2959 case SLAPI_ACL_SELF:
2960 slap_access = ACL_WRITE;
2963 return LDAP_INSUFFICIENT_ACCESS;
2967 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2968 return LDAP_PARAM_ERROR;
2971 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
2972 return LDAP_PARAM_ERROR;
2975 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
2976 return LDAP_PARAM_ERROR;
2979 ret = access_allowed( be, conn, op, e, desc, val, slap_access, NULL );
2981 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2983 return LDAP_UNWILLING_TO_PERFORM;
2987 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
2995 Modifications *next;
2997 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2998 return LDAP_PARAM_ERROR;
3001 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3002 return LDAP_PARAM_ERROR;
3005 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3006 return LDAP_PARAM_ERROR;
3009 ml = slapi_x_ldapmods2modifications( mods );
3014 ret = acl_check_modlist( be, conn, op, e, ml );
3016 /* Careful when freeing the modlist because it has pointers into the mods array. */
3017 for ( ; ml != NULL; ml = next ) {
3018 next = ml->sml_next;
3020 /* just free the containing array */
3021 slapi_ch_free( (void **)&ml->sml_bvalues );
3022 slapi_ch_free( (void **)&ml );
3025 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3027 return LDAP_UNWILLING_TO_PERFORM;
3032 * Synthesise an LDAPMod array from a Modifications list to pass
3033 * to SLAPI. This synthesis is destructive and as such the
3034 * Modifications list may not be used after calling this
3037 * This function must also be called before slap_mods_check().
3039 LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
3042 Modifications *ml, *modlist;
3043 LDAPMod **mods, *modp;
3046 modlist = *pmodlist;
3048 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3051 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3053 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3054 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3056 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3058 /* Take ownership of original type. */
3059 modp->mod_type = ml->sml_type.bv_val;
3060 ml->sml_type.bv_val = NULL;
3062 if ( ml->sml_bvalues != NULL ) {
3063 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
3065 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3066 sizeof(struct berval *) );
3067 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
3068 /* Take ownership of original values. */
3069 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3070 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
3071 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
3072 ml->sml_bvalues[j].bv_len = 0;
3073 ml->sml_bvalues[j].bv_val = NULL;
3075 modp->mod_bvalues[j] = NULL;
3077 modp->mod_bvalues = NULL;
3084 slap_mods_free( modlist );
3094 * Convert a potentially modified array of LDAPMods back to a
3095 * Modification list.
3097 * The returned Modification list contains pointers into the
3098 * LDAPMods array; the latter MUST be freed with
3099 * slapi_x_free_ldapmods() (see below).
3101 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
3104 Modifications *modlist, **modtail;
3109 for( modp = mods; *modp != NULL; modp++ ) {
3113 struct berval **bvp;
3115 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3116 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3117 mod->sml_type.bv_val = (*modp)->mod_type;
3118 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3119 mod->sml_desc = NULL;
3120 mod->sml_next = NULL;
3122 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3123 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3126 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3131 mod->sml_bvalues = NULL;
3133 mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3135 /* NB: This implicitly trusts a plugin to return valid modifications. */
3136 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3137 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3138 mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
3139 mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
3142 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3143 mod->sml_bvalues[i].bv_val = *p;
3144 mod->sml_bvalues[i].bv_len = strlen( *p );
3147 mod->sml_bvalues[i].bv_val = NULL;
3151 modtail = &mod->sml_next;
3161 * This function only frees the parts of the mods array that
3162 * are not shared with the Modification list that was created
3163 * by slapi_x_ldapmods2modifications().
3166 void slapi_x_free_ldapmods (LDAPMod **mods)
3174 for ( i = 0; mods[i] != NULL; i++ ) {
3176 * Don't free values themselves; they're owned by the
3177 * Modification list. Do free the containing array.
3179 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3180 for ( j = 0; mods[i]->mod_bvalues != NULL && mods[i]->mod_bvalues[j] != NULL; j++ ) {
3181 ch_free( mods[i]->mod_bvalues[j] );
3183 ch_free( mods[i]->mod_bvalues );
3185 ch_free( mods[i]->mod_values );
3187 /* Don't free type, for same reasons. */
3191 #endif /* LDAP_SLAPI */
3195 * Sun ONE DS 5.x computed attribute support. Computed attributes
3196 * allow for dynamically generated operational attributes, a very
3197 * useful thing indeed.
3201 * Write the computed attribute to a BerElement. Complementary
3202 * functions need to be defined for anything that replaces
3203 * op->o_callback->sc_sendentry, if you wish to make computed
3204 * attributes available to it.
3206 int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
3210 Connection *conn = NULL;
3211 Operation *op = NULL;
3213 AttributeDescription *desc = NULL;
3229 rc = slapi_pblock_get( c->cac_pb, SLAPI_BACKEND, (void *)&be );
3231 be = NULL; /* no backend for root DSE */
3234 rc = slapi_pblock_get( c->cac_pb, SLAPI_CONNECTION, (void *)&conn );
3235 if ( rc != 0 || conn == NULL ) {
3239 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
3240 if ( rc != 0 || op == NULL ) {
3244 ber = (BerElement *)c->cac_private;
3247 if ( c->cac_attrs == NULL ) {
3248 /* All attrs request, skip operational attributes */
3249 if ( is_at_operational( desc->ad_type ) ) {
3253 /* Specific attrs requested */
3254 if ( is_at_operational( desc->ad_type ) ) {
3255 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3259 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3265 if ( !access_allowed( be, conn, op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
3266 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
3267 "acl: access to attribute %s not allowed\n",
3268 desc->ad_cname.bv_val );
3272 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
3274 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3275 "ber_printf failed\n");
3279 if ( !c->cac_attrsonly ) {
3280 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
3281 if ( !access_allowed( be, conn, op, e,
3282 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
3283 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
3284 "slapi_x_compute_output_ber: conn %lu "
3285 "acl: access to %s, value %d not allowed\n",
3286 op->o_connid, desc->ad_cname.bv_val, i );
3290 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
3291 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3292 "ber_printf failed\n");
3298 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
3299 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3300 "ber_printf failed\n" );
3311 * For some reason Sun don't use the normal plugin mechanism
3312 * registration path to register an "evaluator" function (an
3313 * "evaluator" is responsible for adding computed attributes;
3314 * the nomenclature is somewhat confusing).
3316 * As such slapi_compute_add_evaluator() registers the
3317 * function directly.
3319 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3322 Slapi_PBlock *pPlugin = NULL;
3325 pPlugin = slapi_pblock_new();
3326 if ( pPlugin == NULL ) {
3327 rc = LDAP_NO_MEMORY;
3331 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3332 if ( rc != LDAP_SUCCESS ) {
3336 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3337 if ( rc != LDAP_SUCCESS ) {
3341 rc = insertPlugin( NULL, pPlugin );
3348 if ( rc != LDAP_SUCCESS ) {
3349 if ( pPlugin != NULL ) {
3350 slapi_pblock_destroy( pPlugin );
3358 #endif /* LDAP_SLAPI */
3362 * See notes above regarding slapi_compute_add_evaluator().
3364 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3367 Slapi_PBlock *pPlugin = NULL;
3370 pPlugin = slapi_pblock_new();
3371 if ( pPlugin == NULL ) {
3372 rc = LDAP_NO_MEMORY;
3376 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3377 if ( rc != LDAP_SUCCESS ) {
3381 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3382 if ( rc != LDAP_SUCCESS ) {
3386 rc = insertPlugin( NULL, pPlugin );
3393 if ( rc != LDAP_SUCCESS ) {
3394 if ( pPlugin != NULL ) {
3395 slapi_pblock_destroy( pPlugin );
3403 #endif /* LDAP_SLAPI */
3407 * Call compute evaluators
3409 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3413 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3415 rc = getAllPluginFuncs( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3416 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3417 /* Nothing to do; front-end should ignore. */
3421 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3423 * -1: no attribute matched requested type
3424 * 0: one attribute matched
3425 * >0: error happened
3427 rc = (*pGetPlugin)( c, type, e, outputfn );
3433 slapi_ch_free( (void **)&tmpPlugin );
3438 #endif /* LDAP_SLAPI */
3441 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3447 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3452 return doPluginFNs( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3455 #endif /* LDAP_SLAPI */
3459 * New API to provide the plugin with access to the search
3460 * pblock. Have informed Sun DS team.
3462 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3468 if ( c->cac_pb == NULL )
3476 #endif /* LDAP_SLAPI */
3479 Slapi_Mutex *slapi_new_mutex( void )
3484 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3485 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3486 slapi_ch_free( (void **)&m );
3496 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3499 if ( mutex != NULL ) {
3500 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3501 slapi_ch_free( (void **)&mutex);
3506 void slapi_lock_mutex( Slapi_Mutex *mutex )
3509 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3513 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3516 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3522 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3527 if ( mutex == NULL ) {
3531 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3532 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3533 slapi_ch_free( (void **)&cv );
3537 /* XXX struct copy */
3538 cv->mutex = mutex->mutex;
3546 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3549 if ( cvar != NULL ) {
3550 ldap_pvt_thread_cond_destroy( &cvar->cond );
3551 slapi_ch_free( (void **)&cvar );
3556 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3559 if ( cvar == NULL ) {
3563 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3569 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3572 if ( cvar == NULL ) {
3577 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3580 return ldap_pvt_thread_cond_signal( &cvar->cond );