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>
32 #include <sys/systeminfo.h>
38 * server start time (should we use a struct timeval also in slapd?
40 static struct timeval base_time;
41 ldap_pvt_thread_mutex_t slapi_hn_mutex;
42 ldap_pvt_thread_mutex_t slapi_time_mutex;
45 ldap_pvt_thread_mutex_t mutex;
48 struct slapi_condvar {
49 ldap_pvt_thread_cond_t cond;
50 ldap_pvt_thread_mutex_t mutex;
54 * This function converts an array of pointers to berval objects to
55 * an array of berval objects.
60 struct berval **bvptr,
63 int rc = LDAP_SUCCESS;
67 if ( bvptr == NULL || *bvptr == NULL ) {
71 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
75 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
76 if ( tmpberval == NULL ) {
77 return LDAP_NO_MEMORY;
80 for ( i = 0; bvptr[i] != NULL; i++ ) {
81 tmpberval[i].bv_val = bvptr[i]->bv_val;
82 tmpberval[i].bv_len = bvptr[i]->bv_len;
84 tmpberval[i].bv_val = NULL;
85 tmpberval[i].bv_len = 0;
87 if ( rc == LDAP_SUCCESS ) {
100 Slapi_Entry *e = NULL;
103 pTmpS = slapi_ch_strdup( s );
104 if ( pTmpS != NULL ) {
105 e = str2entry( pTmpS );
106 slapi_ch_free( (void **)&pTmpS );
112 #endif /* LDAP_SLAPI */
123 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
124 ret = entry2str( e, len );
125 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
128 #else /* LDAP_SLAPI */
130 #endif /* LDAP_SLAPI */
134 slapi_entry_get_dn( Slapi_Entry *e )
137 return e->e_name.bv_val;
138 #else /* LDAP_SLAPI */
140 #endif /* LDAP_SLAPI */
144 slapi_x_entry_get_id( Slapi_Entry *e )
150 #endif /* LDAP_SLAPI */
159 struct berval dn = { 0, NULL };
162 dn.bv_len = strlen( ldn );
164 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
165 #endif /* LDAP_SLAPI */
169 slapi_entry_dup( Slapi_Entry *e )
176 tmp = slapi_entry2str( e, &len );
178 return (Slapi_Entry *)NULL;
181 tmpEnt = (Slapi_Entry *)str2entry( tmp );
182 if ( tmpEnt == NULL ) {
183 slapi_ch_free( (void **)&tmp );
184 return (Slapi_Entry *)NULL;
188 slapi_ch_free( (void **)&tmp );
192 #else /* LDAP_SLAPI */
194 #endif /* LDAP_SLAPI */
198 slapi_entry_attr_delete(
203 AttributeDescription *ad = NULL;
206 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
207 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
210 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
211 return 0; /* attribute is deleted */
213 return -1; /* something went wrong */
215 #else /* LDAP_SLAPI */
217 #endif /* LDAP_SLAPI */
221 slapi_entry_alloc( void )
224 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
225 #else /* LDAP_SLAPI */
227 #endif /* LDAP_SLAPI */
231 slapi_entry_free( Slapi_Entry *e )
235 #endif /* LDAP_SLAPI */
239 slapi_entry_attr_merge(
242 struct berval **vals )
245 AttributeDescription *ad = NULL;
250 rc = bvptr2obj( vals, &bv );
251 if ( rc != LDAP_SUCCESS ) {
255 rc = slap_str2ad( type, &ad, &text );
256 if ( rc != LDAP_SUCCESS ) {
260 rc = attr_mergeit( e, ad, bv );
264 #else /* LDAP_SLAPI */
266 #endif /* LDAP_SLAPI */
270 slapi_entry_attr_find(
276 AttributeDescription *ad = NULL;
280 rc = slap_str2ad( type, &ad, &text );
281 if ( rc != LDAP_SUCCESS ) {
285 *attr = attr_find( e->e_attrs, ad );
286 if ( *attr == NULL ) {
291 #else /* LDAP_SLAPI */
293 #endif /* LDAP_SLAPI */
297 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
300 AttributeDescription *ad = NULL;
305 rc = slap_str2ad( type, &ad, &text );
306 if ( rc != LDAP_SUCCESS ) {
310 attr = attr_find( e->e_attrs, ad );
311 if ( attr == NULL ) {
315 if ( attr->a_vals != NULL && attr->a_vals[0].bv_val != NULL ) {
316 return slapi_ch_strdup( attr->a_vals[0].bv_val );
326 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
329 AttributeDescription *ad = NULL;
334 rc = slap_str2ad( type, &ad, &text );
335 if ( rc != LDAP_SUCCESS ) {
339 attr = attr_find( e->e_attrs, ad );
340 if ( attr == NULL ) {
344 return slapi_value_get_int( attr->a_vals );
351 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
354 AttributeDescription *ad = NULL;
359 rc = slap_str2ad( type, &ad, &text );
360 if ( rc != LDAP_SUCCESS ) {
364 attr = attr_find( e->e_attrs, ad );
365 if ( attr == NULL ) {
369 return slapi_value_get_long( attr->a_vals );
376 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
379 AttributeDescription *ad = NULL;
384 rc = slap_str2ad( type, &ad, &text );
385 if ( rc != LDAP_SUCCESS ) {
389 attr = attr_find( e->e_attrs, ad );
390 if ( attr == NULL ) {
394 return slapi_value_get_uint( attr->a_vals );
401 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
404 AttributeDescription *ad = NULL;
409 rc = slap_str2ad( type, &ad, &text );
410 if ( rc != LDAP_SUCCESS ) {
414 attr = attr_find( e->e_attrs, ad );
415 if ( attr == NULL ) {
419 return slapi_value_get_ulong( attr->a_vals );
426 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
430 AttributeDescription *ad = NULL;
435 rc = slap_str2ad( type, &ad, &text );
436 if ( rc != LDAP_SUCCESS ) {
440 attr = attr_find( e->e_attrs, ad );
441 if ( attr == NULL ) {
445 bv.bv_val = (char *)value;
446 bv.bv_len = strlen( value );
448 return ( slapi_attr_value_find( attr, &bv ) != -1 );
455 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
458 AttributeDescription *ad = NULL;
463 rc = slap_str2ad( type, &ad, &text );
464 if ( rc != LDAP_SUCCESS ) {
468 attr_delete ( &e->e_attrs, ad );
469 if ( value != NULL ) {
470 bv.bv_val = (char *)value;
471 bv.bv_len = strlen(value);
472 attr_mergeit_one( e, ad, &bv );
474 #endif /* LDAP_SLAPI */
478 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
483 snprintf( buf, sizeof( buf ), "%d", l );
484 slapi_entry_attr_set_charptr( e, type, buf );
485 #endif /* LDAP_SLAPI */
489 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
494 snprintf( buf, sizeof( buf ), "%u", l );
495 slapi_entry_attr_set_charptr( e, type, buf );
496 #endif /* LDAP_SLAPI */
500 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
505 snprintf( buf, sizeof( buf ), "%ld", l );
506 slapi_entry_attr_set_charptr( e, type, buf );
507 #endif /* LDAP_SLAPI */
511 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
516 snprintf( buf, sizeof( buf ), "%lu", l );
517 slapi_entry_attr_set_charptr( e, type, buf );
518 #endif /* LDAP_SLAPI */
522 slapi_is_rootdse( const char *dn )
525 return ( dn == NULL || dn[0] == '\0' );
532 * Add values to entry.
535 * LDAP_SUCCESS Values added to entry
536 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
537 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
540 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
546 char textbuf[SLAP_TEXT_BUFLEN];
548 mod.sm_op = LDAP_MOD_ADD;
550 mod.sm_type.bv_val = (char *)type;
551 mod.sm_type.bv_len = strlen( type );
553 rc = slap_str2ad( type, &mod.sm_desc, &text );
554 if ( rc != LDAP_SUCCESS ) {
558 if ( vals == NULL ) {
559 /* Apparently vals can be NULL */
560 mod.sm_bvalues = (BerVarray)ch_malloc( sizeof(struct berval) );
561 mod.sm_bvalues->bv_val = NULL;
563 rc = bvptr2obj( vals, &mod.sm_bvalues );
564 if ( rc != LDAP_SUCCESS ) {
565 return LDAP_CONSTRAINT_VIOLATION;
569 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
571 ch_free( mod.sm_bvalues );
573 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
576 #endif /* LDAP_SLAPI */
580 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
583 return slapi_entry_add_values( e, type, vals );
586 #endif /* LDAP_SLAPI */
590 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
593 AttributeDescription *ad = NULL;
597 rc = slap_str2ad( type, &ad, &text );
598 if ( rc != LDAP_SUCCESS ) {
602 return attr_mergeit( e, ad, *vs );
605 #endif /* LDAP_SLAPI */
609 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
615 char textbuf[SLAP_TEXT_BUFLEN];
617 mod.sm_op = LDAP_MOD_DELETE;
619 mod.sm_type.bv_val = (char *)type;
620 mod.sm_type.bv_len = strlen( type );
622 if ( vals == NULL ) {
623 /* If vals is NULL, this is a NOOP. */
627 rc = slap_str2ad( type, &mod.sm_desc, &text );
628 if ( rc != LDAP_SUCCESS ) {
632 if ( vals[0] == NULL ) {
633 /* SLAPI doco says LDAP_OPERATIONS_ERROR */
634 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OPERATIONS_ERROR : LDAP_SUCCESS;
637 rc = bvptr2obj( vals, &mod.sm_bvalues );
638 if ( rc != LDAP_SUCCESS ) {
639 return LDAP_CONSTRAINT_VIOLATION;
642 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
644 ch_free( mod.sm_bvalues );
649 #endif /* LDAP_SLAPI */
653 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
656 return slapi_entry_delete_values( e, type, vals );
659 #endif /* LDAP_SLAPI */
663 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
666 return slapi_entry_attr_merge( e, (char *)type, vals );
669 #endif /* LDAP_SLAPI */
673 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
676 AttributeDescription *ad = NULL;
680 rc = slap_str2ad( type, &ad, &text );
681 if ( rc != LDAP_SUCCESS ) {
685 rc = attr_mergeit_one( e, ad, (Slapi_Value *)value );
686 if ( rc != LDAP_SUCCESS ) {
693 #endif /* LDAP_SLAPI */
697 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
702 val.bv_val = (char *)value;
703 val.bv_len = strlen( value );
705 return slapi_entry_add_value( e, type, &val );
708 #endif /* LDAP_SLAPI */
712 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
715 Slapi_Value *vals[2];
718 val.bv_val = (char *)value;
719 val.bv_len = strlen( value );
723 return slapi_entry_delete_values_sv( e, type, vals );
726 #endif /* LDAP_SLAPI */
731 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
734 return slapi_entry_attr_merge( e, (char *)type, vals );
741 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
750 return ( *attr != NULL ) ? 0 : -1;
757 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
764 if ( prevattr == NULL ) {
768 *attr = prevattr->a_next;
770 return ( *attr != NULL ) ? 0 : -1;
777 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
780 AttributeDescription *ad = NULL;
785 rc = slap_str2ad( type, &ad, &text );
786 if ( rc != LDAP_SUCCESS ) {
790 attr_delete( &e->e_attrs, ad );
792 rc = bvptr2obj( vals, &bv );
793 if ( rc != LDAP_SUCCESS ) {
797 rc = attr_mergeit( e, ad, bv );
798 slapi_ch_free( (void **)&bv );
799 if ( rc != LDAP_SUCCESS ) {
806 #endif /* LDAP_SLAPI */
810 * FIXME -- The caller must free the allocated memory.
811 * In Netscape they do not have to.
814 slapi_attr_get_values(
816 struct berval ***vals )
822 if ( attr == NULL ) {
826 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
830 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
831 for ( j = 0; j < i; j++ ) {
832 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
836 *vals = (struct berval **)bv;
839 #else /* LDAP_SLAPI */
841 #endif /* LDAP_SLAPI */
845 slapi_dn_normalize( char *dn )
851 assert( dn != NULL );
854 bdn.bv_len = strlen( dn );
856 dnNormalize2( NULL, &bdn, &ndn );
859 * FIXME: ain't it safe to set dn = ndn.bv_val ?
861 dn = ch_strdup( ndn.bv_val );
862 ch_free( ndn.bv_val );
865 #else /* LDAP_SLAPI */
867 #endif /* LDAP_SLAPI */
871 * FIXME: this function is dangerous and should be deprecated;
872 * DN normalization is a lot more than lower-casing, and BTW
873 * OpenLDAP's DN normalization for case insensitive attributes
874 * is already lower case
877 slapi_dn_normalize_case( char *dn )
880 slapi_dn_normalize( dn );
881 ldap_pvt_str2lower( dn );
884 #else /* LDAP_SLAPI */
886 #endif /* LDAP_SLAPI */
895 struct berval bdn, ndn;
896 struct berval bsuffix, nsuffix;
898 assert( dn != NULL );
899 assert( suffix != NULL );
902 bdn.bv_len = strlen( dn );
904 bsuffix.bv_val = suffix;
905 bsuffix.bv_len = strlen( suffix );
907 dnNormalize2( NULL, &bdn, &ndn );
908 dnNormalize2( NULL, &bsuffix, &nsuffix );
910 return dnIsSuffix( &ndn, &nsuffix );
911 #else /* LDAP_SLAPI */
913 #endif /* LDAP_SLAPI */
917 slapi_dn_ignore_case( char *dn )
920 return slapi_dn_normalize_case( dn );
921 #else /* LDAP_SLAPI */
923 #endif /* LDAP_SLAPI */
927 slapi_ch_malloc( unsigned long size )
930 return ch_malloc( size );
931 #else /* LDAP_SLAPI */
933 #endif /* LDAP_SLAPI */
937 slapi_ch_free( void **ptr )
942 #endif /* LDAP_SLAPI */
946 slapi_ch_free_string( char **ptr )
949 slapi_ch_free( (void **)ptr );
950 #endif /* LDAP_SLAPI */
954 slapi_ch_array_free( char **arrayp )
959 if ( arrayp != NULL ) {
960 for ( p = arrayp; *p != NULL; p++ ) {
961 slapi_ch_free( (void **)p );
963 slapi_ch_free( (void **)&arrayp );
969 slapi_ch_bvdup(const struct berval *v)
974 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
975 bv->bv_len = v->bv_len;
976 bv->bv_val = slapi_ch_malloc( bv->bv_len );
977 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
986 slapi_ch_bvecdup(const struct berval **v)
996 for ( i = 0; v[i] != NULL; i++ )
999 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1001 for ( i = 0; v[i] != NULL; i++ ) {
1002 rv[i] = slapi_ch_bvdup( v[i] );
1014 unsigned long nelem,
1015 unsigned long size )
1018 return ch_calloc( nelem, size );
1019 #else /* LDAP_SLAPI */
1021 #endif /* LDAP_SLAPI */
1027 unsigned long size )
1030 return ch_realloc( block, size );
1031 #else /* LDAP_SLAPI */
1033 #endif /* LDAP_SLAPI */
1037 slapi_ch_strdup( char *s )
1040 return ch_strdup( (const char *)s );
1041 #else /* LDAP_SLAPI */
1043 #endif /* LDAP_SLAPI */
1047 slapi_ch_stlen( char *s )
1050 return strlen( (const char *)s );
1051 #else /* LDAP_SLAPI */
1053 #endif /* LDAP_SLAPI */
1057 slapi_control_present(
1058 LDAPControl **controls,
1060 struct berval **val,
1075 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1076 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1081 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1083 * FIXME: according to 6.1 specification,
1084 * "The val output parameter is set
1085 * to point into the controls array.
1086 * A copy of the control value is
1090 struct berval *pTmpBval;
1092 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
1093 if ( pTmpBval == NULL ) {
1096 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
1097 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
1101 slapi_ch_free( (void **)&pTmpBval );
1107 *val = &controls[i]->ldctl_value;
1112 *iscritical = controls[i]->ldctl_iscritical;
1119 #else /* LDAP_SLAPI */
1121 #endif /* LDAP_SLAPI */
1125 slapi_register_supported_control(
1127 unsigned long controlops )
1130 /* FIXME -- can not add controls to OpenLDAP dynamically */
1131 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_CONTROLS",
1132 "OpenLDAP does not support dynamic registration of LDAP controls\n" );
1133 #endif /* LDAP_SLAPI */
1137 slapi_get_supported_controls(
1139 unsigned long **ctrlopsp )
1145 unsigned long *masks = NULL;
1147 for (n = 0; get_supported_ctrl( n ) != NULL; n++) {
1155 return LDAP_SUCCESS;
1159 oids = (char **)slapi_ch_malloc( (n + 1) * sizeof(char *) );
1160 if ( oids == NULL ) {
1161 rc = LDAP_NO_MEMORY;
1165 masks = (unsigned long *)slapi_ch_malloc( n * sizeof(int) );
1166 if ( masks == NULL ) {
1167 rc = LDAP_NO_MEMORY;
1171 for ( i = 0; i < n; i++ ) {
1173 * FIXME: Netscape's specification says nothing about
1174 * memory; should we copy the OIDs or return pointers
1175 * to internal values? In OpenLDAP the latter is safe
1176 * since we do not allow to register coltrols runtime
1178 oids[ i ] = ch_strdup( get_supported_ctrl( i ) );
1179 if ( oids[ i ] == NULL ) {
1180 rc = LDAP_NO_MEMORY;
1183 masks[ i ] = (unsigned long)get_supported_ctrl_mask( i );
1188 return LDAP_SUCCESS;
1191 if ( rc != LDAP_SUCCESS ) {
1192 for ( i = 0; oids != NULL && oids[ i ] != NULL; i++ ) {
1193 ch_free( oids[ i ] );
1200 #else /* LDAP_SLAPI */
1202 #endif /* LDAP_SLAPI */
1206 slapi_register_supported_saslmechanism( char *mechanism )
1209 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1210 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
1211 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1212 #endif /* LDAP_SLAPI */
1216 slapi_get_supported_saslmechanisms( void )
1219 /* FIXME -- can not get the saslmechanism wihtout a connection. */
1220 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
1221 "can not get the saslmechanism "
1222 "wihtout a connection\n" );
1224 #else /* LDAP_SLAPI */
1226 #endif /* LDAP_SLAPI */
1230 slapi_get_supported_extended_ops( void )
1234 char **ppExtOpOID = NULL;
1237 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1241 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
1246 if ( numExtOps == 0 ) {
1250 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1251 for ( k = 0; k < i; k++ ) {
1254 bv = get_supported_extop( k );
1255 assert( bv != NULL );
1257 ppExtOpOID[ k ] = bv->bv_val;
1260 for ( ; k < j; k++ ) {
1263 bv = ns_get_supported_extop( k );
1264 assert( bv != NULL );
1266 ppExtOpOID[ i + k ] = bv->bv_val;
1268 ppExtOpOID[ i + k ] = NULL;
1271 #else /* LDAP_SLAPI */
1273 #endif /* LDAP_SLAPI */
1277 slapi_send_ldap_result(
1283 struct berval **urls )
1289 char *extOID = NULL;
1290 struct berval *extValue = NULL;
1293 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1294 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1295 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1296 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, &s );
1297 rc = LDAP_SASL_BIND_IN_PROGRESS;
1298 send_ldap_sasl( conn, op, rc, NULL, NULL, NULL, NULL, s );
1302 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1303 if ( extOID != NULL ) {
1304 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &extValue );
1305 slapi_send_ldap_extended_response( conn, op, err, extOID,
1310 send_ldap_result( conn, op, err, matched, text, NULL, NULL );
1311 #endif /* LDAP_SLAPI */
1315 slapi_send_ldap_search_entry(
1318 LDAPControl **ectrls,
1329 AttributeName *an = NULL;
1332 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1337 an = (AttributeName *) ch_malloc( i * sizeof(AttributeName) );
1338 for ( i = 0; attrs[i] != NULL; i++ ) {
1339 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1340 an[i].an_name.bv_len = strlen( attrs[i] );
1341 an[i].an_desc = NULL;
1342 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1347 if ( ( rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) ||
1348 ( rc = slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&pConn) != 0 ) ||
1349 ( rc = slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp) != 0 ) ) {
1352 rc = send_search_entry( be, pConn, pOp, e, an, attrsonly, NULL );
1357 #else /* LDAP_SLAPI */
1359 #endif /* LDAP_SLAPI */
1364 slapi_str2filter( char *str )
1367 return str2filter( str );
1368 #else /* LDAP_SLAPI */
1370 #endif /* LDAP_SLAPI */
1380 #endif /* LDAP_SLAPI */
1384 slapi_filter_dup( Slapi_Filter *filter )
1389 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1391 f->f_choice = filter->f_choice;
1393 switch ( f->f_choice ) {
1394 case LDAP_FILTER_AND:
1395 case LDAP_FILTER_NOT:
1396 case LDAP_FILTER_OR: {
1397 Filter *pFilter, **ppF;
1399 for ( pFilter = filter->f_list, ppF = &f->f_list;
1401 pFilter = pFilter->f_next, ppF = &f->f_next )
1403 *ppF = slapi_filter_dup( pFilter );
1409 case LDAP_FILTER_PRESENT:
1410 f->f_desc = filter->f_desc;
1412 case LDAP_FILTER_EQUALITY:
1413 case LDAP_FILTER_GE:
1414 case LDAP_FILTER_LE:
1415 case LDAP_FILTER_APPROX:
1416 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1417 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1418 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1420 case LDAP_FILTER_EXT:
1421 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1422 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1423 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1424 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1425 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1426 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1427 case LDAP_FILTER_SUBSTRINGS: {
1430 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1431 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1432 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1434 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1435 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1436 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1438 f->f_sub_any[i].bv_val = NULL;
1439 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1442 case SLAPD_FILTER_COMPUTED:
1443 f->f_result = filter->f_result;
1446 slapi_ch_free( (void **)&f );
1454 #endif /* LDAP_SLAPI */
1458 slapi_filter_get_choice( Slapi_Filter *f )
1470 #else /* LDAP_SLAPI */
1471 return -1; /* invalid filter type */
1472 #endif /* LDAP_SLAPI */
1476 slapi_filter_get_ava(
1479 struct berval **bval )
1483 int rc = LDAP_SUCCESS;
1485 assert( type != NULL );
1486 assert( bval != NULL );
1491 ftype = f->f_choice;
1492 if ( ftype == LDAP_FILTER_EQUALITY
1493 || ftype == LDAP_FILTER_GE
1494 || ftype == LDAP_FILTER_LE
1495 || ftype == LDAP_FILTER_APPROX ) {
1497 * According to the SLAPI Reference Manual these are
1500 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1501 *bval = &f->f_un.f_un_ava->aa_value;
1502 } else { /* filter type not supported */
1507 #else /* LDAP_SLAPI */
1509 #endif /* LDAP_SLAPI */
1513 slapi_filter_list_first( Slapi_Filter *f )
1522 ftype = f->f_choice;
1523 if ( ftype == LDAP_FILTER_AND
1524 || ftype == LDAP_FILTER_OR
1525 || ftype == LDAP_FILTER_NOT ) {
1526 return (Slapi_Filter *)f->f_and;
1530 #else /* LDAP_SLAPI */
1532 #endif /* LDAP_SLAPI */
1536 slapi_filter_list_next(
1538 Slapi_Filter *fprev )
1547 ftype = f->f_choice;
1548 if ( ftype == LDAP_FILTER_AND
1549 || ftype == LDAP_FILTER_OR
1550 || ftype == LDAP_FILTER_NOT )
1552 return fprev->f_next;
1556 #else /* LDAP_SLAPI */
1558 #endif /* LDAP_SLAPI */
1562 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1569 switch ( f->f_choice ) {
1570 case LDAP_FILTER_GE:
1571 case LDAP_FILTER_LE:
1572 case LDAP_FILTER_EQUALITY:
1573 case LDAP_FILTER_APPROX:
1574 *type = f->f_av_desc->ad_cname.bv_val;
1576 case LDAP_FILTER_SUBSTRINGS:
1577 *type = f->f_sub_desc->ad_cname.bv_val;
1579 case LDAP_FILTER_PRESENT:
1580 *type = f->f_desc->ad_cname.bv_val;
1582 case LDAP_FILTER_EXT:
1583 *type = f->f_mr_desc->ad_cname.bv_val;
1586 /* Complex filters need not apply. */
1594 #endif /* LDAP_SLAPI */
1598 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1599 char ***any, char **final )
1604 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1609 * The caller shouldn't free but we can't return an
1610 * array of char *s from an array of bervals without
1611 * allocating memory, so we may as well be consistent.
1614 *type = f->f_sub_desc->ad_cname.bv_val;
1615 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1616 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1618 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1619 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1620 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1623 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1628 #endif /* LDAP_SLAPI */
1632 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2)
1635 Slapi_Filter *f = NULL;
1637 if ( ftype == LDAP_FILTER_AND ||
1638 ftype == LDAP_FILTER_OR ||
1639 ftype == LDAP_FILTER_NOT )
1641 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1642 f->f_choice = ftype;
1650 #endif /* LDAP_SLAPI */
1654 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1664 /* spec says return zero if no filter. */
1668 if ( verify_access ) {
1669 (void) slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be);
1670 rc = slapi_pblock_get(pb, SLAPI_CONNECTION, (void *)&conn);
1672 return LDAP_PARAM_ERROR;
1674 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
1676 return LDAP_PARAM_ERROR;
1683 * According to acl.c it is safe to call test_filter() with
1686 rc = test_filter( be, conn, op, e, f );
1688 case LDAP_COMPARE_TRUE:
1691 case LDAP_COMPARE_FALSE:
1693 case SLAPD_COMPARE_UNDEFINED:
1696 case LDAP_PROTOCOL_ERROR:
1697 /* filter type unknown: spec says return -1 */
1705 #endif /* LDAP_SLAPI */
1709 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
1712 return slapi_filter_test( NULL, e, f, 0 );
1719 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
1722 switch ( f->f_choice ) {
1723 case LDAP_FILTER_AND:
1724 case LDAP_FILTER_NOT:
1725 case LDAP_FILTER_OR: {
1729 * FIXME: altering f; should we use a temporary?
1731 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1732 rc = slapi_filter_apply( f, fn, arg, error_code );
1736 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
1742 case LDAP_FILTER_EQUALITY:
1743 case LDAP_FILTER_SUBSTRINGS:
1744 case LDAP_FILTER_GE:
1745 case LDAP_FILTER_LE:
1746 case LDAP_FILTER_PRESENT:
1747 case LDAP_FILTER_APPROX:
1748 case LDAP_FILTER_EXT:
1749 *error_code = fn( f, arg );
1752 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1755 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
1756 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
1762 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1764 #endif /* LDAP_SLAPI */
1768 slapi_send_ldap_extended_response(
1773 struct berval *response )
1776 send_ldap_extended( conn,op, errornum, NULL, NULL, NULL,
1777 respName,response, NULL );
1778 return LDAP_SUCCESS;
1779 #else /* LDAP_SLAPI */
1781 #endif /* LDAP_SLAPI */
1786 struct berval **vals,
1791 * FIXME: what's the point?
1794 #else /* LDAP_SLAPI */
1796 #endif /* LDAP_SLAPI */
1799 #define MAX_HOSTNAME 512
1802 slapi_get_hostname( void )
1808 * FIXME: I'd prefer a different check ...
1811 hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1813 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1814 "can't malloc memory for hostname\n" );
1817 } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
1818 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1819 "can't get hostname\n" );
1820 slapi_ch_free( (void **)&hn );
1824 static int been_here = 0;
1825 static char *static_hn = NULL;
1827 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1829 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1830 if ( static_hn == NULL) {
1831 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1832 "can't malloc memory for hostname\n" );
1834 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1839 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1840 slapi_log_error( SLAPI_LOG_FATAL,
1842 "can't get hostname\n" );
1843 slapi_ch_free( (void **)&static_hn );
1845 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1854 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1856 hn = ch_strdup( static_hn );
1857 #endif /* !_SPARC */
1860 #else /* LDAP_SLAPI */
1862 #endif /* LDAP_SLAPI */
1866 * FIXME: this should go in an appropriate header ...
1868 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
1878 int rc = LDAP_SUCCESS;
1881 va_start( arglist, fmt );
1882 rc = vLogError( severity, subsystem, fmt, arglist );
1886 #else /* LDAP_SLAPI */
1888 #endif /* LDAP_SLAPI */
1893 slapi_timer_current_time( void )
1896 static int first_time = 1;
1897 #if !defined (_WIN32)
1901 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1904 gettimeofday( &base_time, NULL );
1906 gettimeofday( &now, NULL );
1907 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1908 (now.tv_usec - base_time.tv_usec);
1909 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1915 return (slap_get_time() - starttime) * 1000000;
1922 performance_counter_present = QueryPerformanceCounter( &base_time );
1923 QueryPerformanceFrequency( &performance_freq );
1926 if ( !performance_counter_present )
1929 QueryPerformanceCounter( &now );
1930 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1932 #else /* LDAP_SLAPI */
1934 #endif /* LDAP_SLAPI */
1941 slapi_timer_get_time( char *label )
1944 unsigned long start = slapi_timer_current_time();
1945 printf("%10ld %10ld usec %s\n", start, 0, label);
1947 #else /* LDAP_SLAPI */
1949 #endif /* LDAP_SLAPI */
1956 slapi_timer_elapsed_time(
1958 unsigned long start )
1961 unsigned long stop = slapi_timer_current_time();
1962 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
1963 #endif /* LDAP_SLAPI */
1967 slapi_free_search_results_internal( Slapi_PBlock *pb )
1970 Slapi_Entry **entries;
1971 int k = 0, nEnt = 0;
1973 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
1974 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
1979 if ( entries == NULL ) {
1983 for ( k = 0; k < nEnt; k++ ) {
1984 slapi_entry_free( entries[k] );
1987 slapi_ch_free( (void **)&entries );
1988 #endif /* LDAP_SLAPI */
1992 * Internal API to prime a Slapi_PBlock with a Backend.
1994 int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
1999 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2000 if ( rc != LDAP_SUCCESS )
2004 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2005 if ( rc != LDAP_SUCCESS )
2009 return LDAP_SUCCESS;
2012 #endif /* LDAP_SLAPI */
2017 * If oldStyle is TRUE, then a value suitable for setting to
2018 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
2019 * (pointer to static storage).
2021 * If oldStyle is FALSE, then a value suitable for setting to
2022 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2023 * a pointer to allocated memory and will include the SASL
2024 * mechanism (if any).
2026 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2031 switch ( authz->sai_method ) {
2032 case LDAP_AUTH_SASL:
2034 authType = SLAPD_AUTH_SASL;
2036 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2037 authType = slapi_ch_malloc( len );
2038 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2041 case LDAP_AUTH_SIMPLE:
2042 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2044 case LDAP_AUTH_NONE:
2045 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2051 if ( is_tls && authType == NULL ) {
2052 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2060 * Internal API to prime a Slapi_PBlock with a Connection.
2062 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
2068 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2069 if ( rc != LDAP_SUCCESS )
2072 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2073 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2074 if ( rc != LDAP_SUCCESS )
2076 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2077 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2078 if ( rc != LDAP_SUCCESS )
2082 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2083 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2084 if ( rc != LDAP_SUCCESS )
2086 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2087 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2088 if ( rc != LDAP_SUCCESS )
2092 #ifdef LDAP_CONNECTIONLESS
2093 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2094 if ( rc != LDAP_SUCCESS )
2098 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2099 if ( rc != LDAP_SUCCESS )
2102 /* Returns pointer to static string */
2103 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
2104 if ( connAuthType != NULL ) {
2105 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2106 if ( rc != LDAP_SUCCESS )
2110 /* Returns pointer to allocated string */
2111 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
2112 if ( connAuthType != NULL ) {
2113 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2114 if ( rc != LDAP_SUCCESS )
2118 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
2119 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
2120 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
2121 if ( rc != LDAP_SUCCESS )
2128 #endif /* LDAP_SLAPI */
2132 * Internal API to prime a Slapi_PBlock with an Operation.
2134 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
2143 if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2147 isRoot = be_isroot( be, &op->o_ndn );
2148 isUpdateDn = be_isupdate( be, &op->o_ndn );
2151 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2152 if ( rc != LDAP_SUCCESS )
2155 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2156 if ( rc != LDAP_SUCCESS )
2159 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2160 if ( rc != LDAP_SUCCESS )
2163 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2164 if ( rc != LDAP_SUCCESS )
2167 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2168 if ( rc != LDAP_SUCCESS )
2171 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2172 if ( rc != LDAP_SUCCESS )
2175 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2176 if ( rc != LDAP_SUCCESS)
2179 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2180 if ( rc != LDAP_SUCCESS )
2183 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2184 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
2185 /* Not quite sure what the point of this is. */
2186 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
2187 if ( rc != LDAP_SUCCESS )
2191 return LDAP_SUCCESS;
2197 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2202 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2203 *isSSL = conn->c_is_tls;
2205 return LDAP_SUCCESS;
2208 #endif /* LDAP_SLAPI */
2212 * DS 5.x compatability API follow
2215 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2221 return LDAP_PARAM_ERROR;
2223 at = attr->a_desc->ad_type;
2225 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2227 if ( is_at_single_value( at ) )
2228 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2229 if ( is_at_operational( at ) )
2230 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2231 if ( is_at_obsolete( at ) )
2232 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2233 if ( is_at_collective( at ) )
2234 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2235 if ( is_at_no_user_mod( at ) )
2236 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2238 return LDAP_SUCCESS;
2241 #endif /* LDAP_SLAPI */
2244 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2247 unsigned long flags;
2249 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2251 return (flags & flag) ? 1 : 0;
2254 #endif /* LDAP_SLAPI */
2257 Slapi_Attr *slapi_attr_new( void )
2262 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2270 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2274 AttributeDescription *ad = NULL;
2276 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2291 void slapi_attr_free( Slapi_Attr **a )
2299 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2302 return attr_dup( (Slapi_Attr *)attr );
2308 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2311 return value_add_one( &a->a_vals, (Slapi_Value *)v );
2317 int slapi_attr_type2plugin( const char *type, void **pi )
2324 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2327 if ( attr == NULL ) {
2328 return LDAP_PARAM_ERROR;
2331 *type = attr->a_desc->ad_cname.bv_val;
2333 return LDAP_SUCCESS;
2339 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2342 if ( attr == NULL ) {
2343 return LDAP_PARAM_ERROR;
2345 *oidp = attr->a_desc->ad_type->sat_oid;
2347 return LDAP_SUCCESS;
2353 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2361 mr = a->a_desc->ad_type->sat_equality;
2362 rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
2363 (struct berval *)v1, (void *)v2, &text );
2364 if ( rc != LDAP_SUCCESS )
2367 return ( ret == 0 ) ? 0 : -1;
2373 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2383 mr = a->a_desc->ad_type->sat_equality;
2384 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2385 rc = value_match( &ret, a->a_desc, mr,
2386 SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
2387 if ( rc != LDAP_SUCCESS ) {
2398 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2401 AttributeDescription *a1 = NULL;
2402 AttributeDescription *a2 = NULL;
2406 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2410 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2414 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2415 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2416 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2419 case SLAPI_TYPE_CMP_EXACT:
2420 ret = ad_cmp( a1, a2 );
2422 case SLAPI_TYPE_CMP_BASE:
2423 ret = ad_base_cmp( a1, a2 );
2425 case SLAPI_TYPE_CMP_SUBTYPE:
2426 ret = is_ad_subtype( a2, a2 );
2439 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2442 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2448 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2451 return slapi_valueset_first_value( &a->a_vals, v );
2457 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2460 return slapi_valueset_next_value( &a->a_vals, hint, v );
2466 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2469 *numValues = slapi_valueset_count( &a->a_vals );
2477 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2480 *vs = &((Slapi_Attr *)a)->a_vals;
2488 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2491 return slapi_attr_get_values( a, vals );
2497 char *slapi_attr_syntax_normalize( const char *s )
2500 AttributeDescription *ad = NULL;
2503 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2507 return ad->ad_cname.bv_val;
2513 Slapi_Value *slapi_value_new( void )
2518 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2526 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2529 return ber_dupbv( NULL, (struct berval *)bval );
2535 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2538 return slapi_value_new_berval( v );
2544 Slapi_Value *slapi_value_new_string(const char *s)
2549 bv.bv_val = (char *)s;
2550 bv.bv_len = strlen( s );
2552 return slapi_value_new_berval( &bv );
2558 Slapi_Value *slapi_value_init(Slapi_Value *val)
2570 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2573 return ber_dupbv( v, bval );
2579 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2582 v->bv_val = slapi_ch_strdup( (char *)s );
2583 v->bv_len = strlen( s );
2591 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2594 return slapi_value_new_value( v );
2600 void slapi_value_free(Slapi_Value **value)
2603 if ( value == NULL ) {
2607 if ( (*value) != NULL ) {
2608 slapi_ch_free( (void **)&(*value)->bv_val );
2609 slapi_ch_free( (void **)value );
2614 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2623 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2626 if ( value == NULL ) {
2629 if ( value->bv_val != NULL ) {
2630 slapi_ch_free( (void **)&value->bv_val );
2632 slapi_value_init_berval( value, (struct berval *)bval );
2640 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2643 if ( value == NULL ) {
2646 return slapi_value_set_berval( value, vfrom );
2652 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2655 if ( value == NULL ) {
2658 if ( value->bv_val != NULL ) {
2659 slapi_ch_free( (void **)&value->bv_val );
2661 value->bv_val = slapi_ch_malloc( len );
2662 value->bv_len = len;
2663 AC_MEMCPY( value->bv_val, val, len );
2671 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2674 if ( value == NULL ) {
2677 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2684 int slapi_value_set_int(Slapi_Value *value, int intVal)
2689 snprintf( buf, sizeof( buf ), "%d", intVal );
2691 return slapi_value_set_string( value, buf );
2697 const char *slapi_value_get_string(const Slapi_Value *value)
2700 if ( value == NULL ) {
2703 return value->bv_val;
2710 static int checkBVString(const struct berval *bv)
2714 for ( i = 0; i < bv->bv_len; i++ ) {
2715 if ( bv->bv_val[i] == '\0' )
2718 if ( bv->bv_val[i] != '\0' )
2725 int slapi_value_get_int(const Slapi_Value *value)
2728 if ( value == NULL ) return 0;
2729 if ( value->bv_val == NULL ) return 0;
2730 if ( !checkBVString( value ) ) return 0;
2732 return (int)strtol( value->bv_val, NULL, 10 );
2738 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2741 if ( value == NULL ) return 0;
2742 if ( value->bv_val == NULL ) return 0;
2743 if ( !checkBVString( value ) ) return 0;
2745 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2751 long slapi_value_get_long(const Slapi_Value *value)
2754 if ( value == NULL ) return 0;
2755 if ( value->bv_val == NULL ) return 0;
2756 if ( !checkBVString( value ) ) return 0;
2758 return strtol( value->bv_val, NULL, 10 );
2764 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2767 if ( value == NULL ) return 0;
2768 if ( value->bv_val == NULL ) return 0;
2769 if ( !checkBVString( value ) ) return 0;
2771 return strtoul( value->bv_val, NULL, 10 );
2777 size_t slapi_value_get_length(const Slapi_Value *value)
2780 if ( value == NULL )
2783 return (size_t) value->bv_len;
2789 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2792 return slapi_attr_value_cmp( a, v1, v2 );
2798 /* A ValueSet is a container for a BerVarray. */
2799 Slapi_ValueSet *slapi_valueset_new( void )
2804 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2813 void slapi_valueset_free(Slapi_ValueSet *vs)
2819 ber_bvarray_free( vp );
2820 slapi_ch_free( (void **)&vp );
2827 void slapi_valueset_init(Slapi_ValueSet *vs)
2830 if ( vs != NULL && *vs == NULL ) {
2831 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2832 (*vs)->bv_val = NULL;
2838 void slapi_valueset_done(Slapi_ValueSet *vs)
2846 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
2848 slapi_ch_free( (void **)&vp->bv_val );
2850 /* but don't free *vs or vs */
2854 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2859 ber_dupbv( &bv, (Slapi_Value *)addval );
2860 ber_bvarray_add( vs, &bv );
2864 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2867 return slapi_valueset_next_value( vs, 0, v );
2873 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2884 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2895 int slapi_valueset_count( const Slapi_ValueSet *vs )
2906 for ( i = 0; vp[i].bv_val != NULL; i++ )
2916 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2921 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2922 slapi_valueset_add_value( vs1, vp );
2927 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
2928 struct berval *val, int access )
2935 slap_access_t slap_access;
2936 AttributeDescription *ad = NULL;
2939 ret = slap_str2ad( attr, &ad, &text );
2940 if ( ret != LDAP_SUCCESS ) {
2944 switch ( access & SLAPI_ACL_ALL ) {
2945 case SLAPI_ACL_COMPARE:
2946 slap_access = ACL_COMPARE;
2948 case SLAPI_ACL_SEARCH:
2949 slap_access = ACL_SEARCH;
2951 case SLAPI_ACL_READ:
2952 slap_access = ACL_READ;
2954 case SLAPI_ACL_WRITE:
2955 case SLAPI_ACL_DELETE:
2957 case SLAPI_ACL_SELF:
2958 slap_access = ACL_WRITE;
2961 return LDAP_INSUFFICIENT_ACCESS;
2965 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2966 return LDAP_PARAM_ERROR;
2969 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
2970 return LDAP_PARAM_ERROR;
2973 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
2974 return LDAP_PARAM_ERROR;
2977 ret = access_allowed( be, conn, op, e, desc, val, slap_access, NULL );
2979 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2981 return LDAP_UNWILLING_TO_PERFORM;
2985 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
2993 Modifications *next;
2995 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2996 return LDAP_PARAM_ERROR;
2999 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3000 return LDAP_PARAM_ERROR;
3003 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3004 return LDAP_PARAM_ERROR;
3007 ml = slapi_x_ldapmods2modifications( mods );
3012 ret = acl_check_modlist( be, conn, op, e, ml );
3014 /* Careful when freeing the modlist because it has pointers into the mods array. */
3015 for ( ; ml != NULL; ml = next ) {
3016 next = ml->sml_next;
3018 /* just free the containing array */
3019 slapi_ch_free( (void **)&ml->sml_bvalues );
3020 slapi_ch_free( (void **)&ml );
3023 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3025 return LDAP_UNWILLING_TO_PERFORM;
3030 * Synthesise an LDAPMod array from a Modifications list to pass
3031 * to SLAPI. This synthesis is destructive and as such the
3032 * Modifications list may not be used after calling this
3035 * This function must also be called before slap_mods_check().
3037 LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
3040 Modifications *ml, *modlist;
3041 LDAPMod **mods, *modp;
3044 modlist = *pmodlist;
3046 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3049 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3051 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3052 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3054 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3056 /* Take ownership of original type. */
3057 modp->mod_type = ml->sml_type.bv_val;
3058 ml->sml_type.bv_val = NULL;
3060 if ( ml->sml_bvalues != NULL ) {
3061 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
3063 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3064 sizeof(struct berval *) );
3065 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
3066 /* Take ownership of original values. */
3067 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3068 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
3069 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
3070 ml->sml_bvalues[j].bv_len = 0;
3071 ml->sml_bvalues[j].bv_val = NULL;
3073 modp->mod_bvalues[j] = NULL;
3075 modp->mod_bvalues = NULL;
3082 slap_mods_free( modlist );
3092 * Convert a potentially modified array of LDAPMods back to a
3093 * Modification list.
3095 * The returned Modification list contains pointers into the
3096 * LDAPMods array; the latter MUST be freed with
3097 * slapi_x_free_ldapmods() (see below).
3099 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
3102 Modifications *modlist, **modtail;
3107 for( modp = mods; *modp != NULL; modp++ ) {
3111 struct berval **bvp;
3113 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3114 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3115 mod->sml_type.bv_val = (*modp)->mod_type;
3116 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3117 mod->sml_desc = NULL;
3118 mod->sml_next = NULL;
3120 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3121 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3124 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3129 mod->sml_bvalues = NULL;
3131 mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3133 /* NB: This implicitly trusts a plugin to return valid modifications. */
3134 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3135 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3136 mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
3137 mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
3140 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3141 mod->sml_bvalues[i].bv_val = *p;
3142 mod->sml_bvalues[i].bv_len = strlen( *p );
3145 mod->sml_bvalues[i].bv_val = NULL;
3149 modtail = &mod->sml_next;
3159 * This function only frees the parts of the mods array that
3160 * are not shared with the Modification list that was created
3161 * by slapi_x_ldapmods2modifications().
3164 void slapi_x_free_ldapmods (LDAPMod **mods)
3172 for ( i = 0; mods[i] != NULL; i++ ) {
3174 * Don't free values themselves; they're owned by the
3175 * Modification list. Do free the containing array.
3177 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3178 for ( j = 0; mods[i]->mod_bvalues != NULL && mods[i]->mod_bvalues[j] != NULL; j++ ) {
3179 ch_free( mods[i]->mod_bvalues[j] );
3181 ch_free( mods[i]->mod_bvalues );
3183 ch_free( mods[i]->mod_values );
3185 /* Don't free type, for same reasons. */
3189 #endif /* LDAP_SLAPI */
3193 * Sun ONE DS 5.x computed attribute support. Computed attributes
3194 * allow for dynamically generated operational attributes, a very
3195 * useful thing indeed.
3199 * Write the computed attribute to a BerElement. Complementary
3200 * functions need to be defined for anything that replaces
3201 * op->o_callback->sc_sendentry, if you wish to make computed
3202 * attributes available to it.
3204 int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
3208 Connection *conn = NULL;
3209 Operation *op = NULL;
3211 AttributeDescription *desc = NULL;
3227 rc = slapi_pblock_get( c->cac_pb, SLAPI_BACKEND, (void *)&be );
3229 be = NULL; /* no backend for root DSE */
3232 rc = slapi_pblock_get( c->cac_pb, SLAPI_CONNECTION, (void *)&conn );
3233 if ( rc != 0 || conn == NULL ) {
3237 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
3238 if ( rc != 0 || op == NULL ) {
3242 ber = (BerElement *)c->cac_private;
3245 if ( c->cac_attrs == NULL ) {
3246 /* All attrs request, skip operational attributes */
3247 if ( is_at_operational( desc->ad_type ) ) {
3251 /* Specific attrs requested */
3252 if ( is_at_operational( desc->ad_type ) ) {
3253 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3257 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3263 if ( !access_allowed( be, conn, op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
3264 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
3265 "acl: access to attribute %s not allowed\n",
3266 desc->ad_cname.bv_val );
3270 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
3272 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3273 "ber_printf failed\n");
3277 if ( !c->cac_attrsonly ) {
3278 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
3279 if ( !access_allowed( be, conn, op, e,
3280 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
3281 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
3282 "slapi_x_compute_output_ber: conn %lu "
3283 "acl: access to %s, value %d not allowed\n",
3284 op->o_connid, desc->ad_cname.bv_val, i );
3288 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
3289 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3290 "ber_printf failed\n");
3296 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
3297 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3298 "ber_printf failed\n" );
3309 * For some reason Sun don't use the normal plugin mechanism
3310 * registration path to register an "evaluator" function (an
3311 * "evaluator" is responsible for adding computed attributes;
3312 * the nomenclature is somewhat confusing).
3314 * As such slapi_compute_add_evaluator() registers the
3315 * function directly.
3317 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3320 Slapi_PBlock *pPlugin = NULL;
3323 pPlugin = slapi_pblock_new();
3324 if ( pPlugin == NULL ) {
3325 rc = LDAP_NO_MEMORY;
3329 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3330 if ( rc != LDAP_SUCCESS ) {
3334 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3335 if ( rc != LDAP_SUCCESS ) {
3339 rc = insertPlugin( NULL, pPlugin );
3346 if ( rc != LDAP_SUCCESS ) {
3347 if ( pPlugin != NULL ) {
3348 slapi_pblock_destroy( pPlugin );
3356 #endif /* LDAP_SLAPI */
3360 * See notes above regarding slapi_compute_add_evaluator().
3362 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3365 Slapi_PBlock *pPlugin = NULL;
3368 pPlugin = slapi_pblock_new();
3369 if ( pPlugin == NULL ) {
3370 rc = LDAP_NO_MEMORY;
3374 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3375 if ( rc != LDAP_SUCCESS ) {
3379 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3380 if ( rc != LDAP_SUCCESS ) {
3384 rc = insertPlugin( NULL, pPlugin );
3391 if ( rc != LDAP_SUCCESS ) {
3392 if ( pPlugin != NULL ) {
3393 slapi_pblock_destroy( pPlugin );
3401 #endif /* LDAP_SLAPI */
3405 * Call compute evaluators
3407 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3411 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3413 rc = getAllPluginFuncs( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3414 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3415 /* Nothing to do; front-end should ignore. */
3419 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3421 * -1: no attribute matched requested type
3422 * 0: one attribute matched
3423 * >0: error happened
3425 rc = (*pGetPlugin)( c, type, e, outputfn );
3431 slapi_ch_free( (void **)&tmpPlugin );
3436 #endif /* LDAP_SLAPI */
3439 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3445 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3450 return doPluginFNs( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3453 #endif /* LDAP_SLAPI */
3457 * New API to provide the plugin with access to the search
3458 * pblock. Have informed Sun DS team.
3460 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3466 if ( c->cac_pb == NULL )
3474 #endif /* LDAP_SLAPI */
3477 Slapi_Mutex *slapi_new_mutex( void )
3482 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3483 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3484 slapi_ch_free( (void **)&m );
3494 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3497 if ( mutex != NULL ) {
3498 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3499 slapi_ch_free( (void **)&mutex);
3504 void slapi_lock_mutex( Slapi_Mutex *mutex )
3507 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3511 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3514 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3520 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3525 if ( mutex == NULL ) {
3529 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3530 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3531 slapi_ch_free( (void **)&cv );
3535 /* XXX struct copy */
3536 cv->mutex = mutex->mutex;
3544 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3547 if ( cvar != NULL ) {
3548 ldap_pvt_thread_cond_destroy( &cvar->cond );
3549 slapi_ch_free( (void **)&cvar );
3554 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3557 if ( cvar == NULL ) {
3561 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3567 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3570 if ( cvar == NULL ) {
3575 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3578 return ldap_pvt_thread_cond_signal( &cvar->cond );