2 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright IBM Corp. 1997,2002
7 * Use of this source code is subject to the terms of The OpenLDAP Public
8 * License (version 2.7 or later).
9 * No trademarks of the IBM Corporation are to be used to identify, endorse
10 * or promote any products derived from this code without the prior
11 * written consent of IBM
14 * Portions (C) Copyright PADL Software Pty Ltd. 2003
15 * Redistribution and use in source and binary forms are permitted
16 * provided that this notice is preserved and that due credit is
17 * given to PADL Software Pty Ltd. This software is provided ``as is''
18 * without express or implied warranty.
23 #include <ac/string.h>
24 #include <ac/stdarg.h>
26 #include <ac/unistd.h>
33 #include <sys/systeminfo.h>
39 * server start time (should we use a struct timeval also in slapd?
41 static struct timeval base_time;
42 ldap_pvt_thread_mutex_t slapi_hn_mutex;
43 ldap_pvt_thread_mutex_t slapi_time_mutex;
46 ldap_pvt_thread_mutex_t mutex;
49 struct slapi_condvar {
50 ldap_pvt_thread_cond_t cond;
51 ldap_pvt_thread_mutex_t mutex;
55 * This function converts an array of pointers to berval objects to
56 * an array of berval objects.
61 struct berval **bvptr,
64 int rc = LDAP_SUCCESS;
68 if ( bvptr == NULL || *bvptr == NULL ) {
72 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
76 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
77 if ( tmpberval == NULL ) {
78 return LDAP_NO_MEMORY;
81 for ( i = 0; bvptr[i] != NULL; i++ ) {
82 tmpberval[i].bv_val = bvptr[i]->bv_val;
83 tmpberval[i].bv_len = bvptr[i]->bv_len;
85 tmpberval[i].bv_val = NULL;
86 tmpberval[i].bv_len = 0;
88 if ( rc == LDAP_SUCCESS ) {
101 Slapi_Entry *e = NULL;
104 pTmpS = slapi_ch_strdup( s );
105 if ( pTmpS != NULL ) {
106 e = str2entry( pTmpS );
107 slapi_ch_free( (void **)&pTmpS );
113 #endif /* LDAP_SLAPI */
124 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
125 ret = entry2str( e, len );
126 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
129 #else /* LDAP_SLAPI */
131 #endif /* LDAP_SLAPI */
135 slapi_entry_get_dn( Slapi_Entry *e )
138 return e->e_name.bv_val;
139 #else /* LDAP_SLAPI */
141 #endif /* LDAP_SLAPI */
145 slapi_x_entry_get_id( Slapi_Entry *e )
151 #endif /* LDAP_SLAPI */
160 struct berval dn = { 0, NULL };
163 dn.bv_len = strlen( ldn );
165 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname, NULL );
166 #endif /* LDAP_SLAPI */
170 slapi_entry_dup( Slapi_Entry *e )
177 tmp = slapi_entry2str( e, &len );
179 return (Slapi_Entry *)NULL;
182 tmpEnt = (Slapi_Entry *)str2entry( tmp );
183 if ( tmpEnt == NULL ) {
184 slapi_ch_free( (void **)&tmp );
185 return (Slapi_Entry *)NULL;
189 slapi_ch_free( (void **)&tmp );
193 #else /* LDAP_SLAPI */
195 #endif /* LDAP_SLAPI */
199 slapi_entry_attr_delete(
204 AttributeDescription *ad = NULL;
207 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
208 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
211 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
212 return 0; /* attribute is deleted */
214 return -1; /* something went wrong */
216 #else /* LDAP_SLAPI */
218 #endif /* LDAP_SLAPI */
222 slapi_entry_alloc( void )
225 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
226 #else /* LDAP_SLAPI */
228 #endif /* LDAP_SLAPI */
232 slapi_entry_free( Slapi_Entry *e )
236 #endif /* LDAP_SLAPI */
240 slapi_entry_attr_merge(
243 struct berval **vals )
246 AttributeDescription *ad = NULL;
251 rc = bvptr2obj( vals, &bv );
252 if ( rc != LDAP_SUCCESS ) {
256 rc = slap_str2ad( type, &ad, &text );
257 if ( rc != LDAP_SUCCESS ) {
261 rc = attr_merge_normalize_one( e, ad, bv, NULL );
265 #else /* LDAP_SLAPI */
267 #endif /* LDAP_SLAPI */
271 slapi_entry_attr_find(
277 AttributeDescription *ad = NULL;
281 rc = slap_str2ad( type, &ad, &text );
282 if ( rc != LDAP_SUCCESS ) {
286 *attr = attr_find( e->e_attrs, ad );
287 if ( *attr == NULL ) {
292 #else /* LDAP_SLAPI */
294 #endif /* LDAP_SLAPI */
298 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
301 AttributeDescription *ad = NULL;
306 rc = slap_str2ad( type, &ad, &text );
307 if ( rc != LDAP_SUCCESS ) {
311 attr = attr_find( e->e_attrs, ad );
312 if ( attr == NULL ) {
316 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
317 return slapi_ch_strdup( attr->a_vals[0].bv_val );
327 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
330 AttributeDescription *ad = NULL;
335 rc = slap_str2ad( type, &ad, &text );
336 if ( rc != LDAP_SUCCESS ) {
340 attr = attr_find( e->e_attrs, ad );
341 if ( attr == NULL ) {
345 return slapi_value_get_int( attr->a_vals );
352 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
355 AttributeDescription *ad = NULL;
360 rc = slap_str2ad( type, &ad, &text );
361 if ( rc != LDAP_SUCCESS ) {
365 attr = attr_find( e->e_attrs, ad );
366 if ( attr == NULL ) {
370 return slapi_value_get_long( attr->a_vals );
377 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
380 AttributeDescription *ad = NULL;
385 rc = slap_str2ad( type, &ad, &text );
386 if ( rc != LDAP_SUCCESS ) {
390 attr = attr_find( e->e_attrs, ad );
391 if ( attr == NULL ) {
395 return slapi_value_get_uint( attr->a_vals );
402 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
405 AttributeDescription *ad = NULL;
410 rc = slap_str2ad( type, &ad, &text );
411 if ( rc != LDAP_SUCCESS ) {
415 attr = attr_find( e->e_attrs, ad );
416 if ( attr == NULL ) {
420 return slapi_value_get_ulong( attr->a_vals );
427 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
431 AttributeDescription *ad = NULL;
436 rc = slap_str2ad( type, &ad, &text );
437 if ( rc != LDAP_SUCCESS ) {
441 attr = attr_find( e->e_attrs, ad );
442 if ( attr == NULL ) {
446 bv.bv_val = (char *)value;
447 bv.bv_len = strlen( value );
449 return ( slapi_attr_value_find( attr, &bv ) != -1 );
456 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
459 AttributeDescription *ad = NULL;
464 rc = slap_str2ad( type, &ad, &text );
465 if ( rc != LDAP_SUCCESS ) {
469 attr_delete ( &e->e_attrs, ad );
470 if ( value != NULL ) {
471 bv.bv_val = (char *)value;
472 bv.bv_len = strlen(value);
473 attr_merge_normalize_one( e, ad, &bv, NULL );
475 #endif /* LDAP_SLAPI */
479 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
484 snprintf( buf, sizeof( buf ), "%d", l );
485 slapi_entry_attr_set_charptr( e, type, buf );
486 #endif /* LDAP_SLAPI */
490 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
495 snprintf( buf, sizeof( buf ), "%u", l );
496 slapi_entry_attr_set_charptr( e, type, buf );
497 #endif /* LDAP_SLAPI */
501 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
506 snprintf( buf, sizeof( buf ), "%ld", l );
507 slapi_entry_attr_set_charptr( e, type, buf );
508 #endif /* LDAP_SLAPI */
512 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
517 snprintf( buf, sizeof( buf ), "%lu", l );
518 slapi_entry_attr_set_charptr( e, type, buf );
519 #endif /* LDAP_SLAPI */
523 slapi_is_rootdse( const char *dn )
526 return ( dn == NULL || dn[0] == '\0' );
533 * Add values to entry.
536 * LDAP_SUCCESS Values added to entry
537 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
538 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
541 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
547 char textbuf[SLAP_TEXT_BUFLEN];
549 mod.sm_op = LDAP_MOD_ADD;
551 mod.sm_type.bv_val = (char *)type;
552 mod.sm_type.bv_len = strlen( type );
554 rc = slap_str2ad( type, &mod.sm_desc, &text );
555 if ( rc != LDAP_SUCCESS ) {
559 if ( vals == NULL ) {
560 /* Apparently vals can be NULL
561 * FIXME: sm_bvalues = NULL ? */
562 mod.sm_bvalues = (BerVarray)ch_malloc( sizeof(struct berval) );
563 mod.sm_bvalues->bv_val = NULL;
566 rc = bvptr2obj( vals, &mod.sm_bvalues );
567 if ( rc != LDAP_SUCCESS ) {
568 return LDAP_CONSTRAINT_VIOLATION;
571 mod.sm_nvalues = NULL;
573 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
575 ch_free( mod.sm_bvalues );
577 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
580 #endif /* LDAP_SLAPI */
584 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
587 return slapi_entry_add_values( e, type, vals );
590 #endif /* LDAP_SLAPI */
594 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
597 AttributeDescription *ad = NULL;
601 rc = slap_str2ad( type, &ad, &text );
602 if ( rc != LDAP_SUCCESS ) {
606 return attr_merge_normalize( e, ad, *vs, NULL );
609 #endif /* LDAP_SLAPI */
613 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
619 char textbuf[SLAP_TEXT_BUFLEN];
621 mod.sm_op = LDAP_MOD_DELETE;
623 mod.sm_type.bv_val = (char *)type;
624 mod.sm_type.bv_len = strlen( type );
626 if ( vals == NULL ) {
627 /* If vals is NULL, this is a NOOP. */
631 rc = slap_str2ad( type, &mod.sm_desc, &text );
632 if ( rc != LDAP_SUCCESS ) {
636 if ( vals[0] == NULL ) {
637 /* SLAPI doco says LDAP_OPERATIONS_ERROR but LDAP_OTHER is better */
638 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
641 rc = bvptr2obj( vals, &mod.sm_bvalues );
642 if ( rc != LDAP_SUCCESS ) {
643 return LDAP_CONSTRAINT_VIOLATION;
645 mod.sm_nvalues = NULL;
647 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
649 ch_free( mod.sm_bvalues );
654 #endif /* LDAP_SLAPI */
658 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
661 return slapi_entry_delete_values( e, type, vals );
664 #endif /* LDAP_SLAPI */
668 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
671 return slapi_entry_attr_merge( e, (char *)type, vals );
674 #endif /* LDAP_SLAPI */
678 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
681 AttributeDescription *ad = NULL;
685 rc = slap_str2ad( type, &ad, &text );
686 if ( rc != LDAP_SUCCESS ) {
690 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
691 if ( rc != LDAP_SUCCESS ) {
698 #endif /* LDAP_SLAPI */
702 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
707 val.bv_val = (char *)value;
708 val.bv_len = strlen( value );
710 return slapi_entry_add_value( e, type, &val );
713 #endif /* LDAP_SLAPI */
717 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
720 Slapi_Value *vals[2];
723 val.bv_val = (char *)value;
724 val.bv_len = strlen( value );
728 return slapi_entry_delete_values_sv( e, type, vals );
731 #endif /* LDAP_SLAPI */
736 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
739 return slapi_entry_attr_merge( e, (char *)type, vals );
746 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
755 return ( *attr != NULL ) ? 0 : -1;
762 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
769 if ( prevattr == NULL ) {
773 *attr = prevattr->a_next;
775 return ( *attr != NULL ) ? 0 : -1;
782 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
785 AttributeDescription *ad = NULL;
790 rc = slap_str2ad( type, &ad, &text );
791 if ( rc != LDAP_SUCCESS ) {
795 attr_delete( &e->e_attrs, ad );
797 rc = bvptr2obj( vals, &bv );
798 if ( rc != LDAP_SUCCESS ) {
802 rc = attr_merge_normalize( e, ad, bv, NULL );
803 slapi_ch_free( (void **)&bv );
804 if ( rc != LDAP_SUCCESS ) {
811 #endif /* LDAP_SLAPI */
815 * FIXME -- The caller must free the allocated memory.
816 * In Netscape they do not have to.
819 slapi_attr_get_values(
821 struct berval ***vals )
827 if ( attr == NULL ) {
831 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
835 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
836 for ( j = 0; j < i; j++ ) {
837 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
841 *vals = (struct berval **)bv;
844 #else /* LDAP_SLAPI */
846 #endif /* LDAP_SLAPI */
850 slapi_dn_normalize( char *dn )
856 assert( dn != NULL );
859 bdn.bv_len = strlen( dn );
861 if ( dnPretty( NULL, &bdn, &pdn, NULL ) != LDAP_SUCCESS ) {
866 #else /* LDAP_SLAPI */
868 #endif /* LDAP_SLAPI */
872 slapi_dn_normalize_case( char *dn )
878 assert( dn != NULL );
881 bdn.bv_len = strlen( dn );
883 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
888 #else /* LDAP_SLAPI */
890 #endif /* LDAP_SLAPI */
899 struct berval bdn, ndn;
900 struct berval bsuffix, nsuffix;
903 assert( dn != NULL );
904 assert( suffix != NULL );
907 bdn.bv_len = strlen( dn );
909 bsuffix.bv_val = suffix;
910 bsuffix.bv_len = strlen( suffix );
912 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
916 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
919 slapi_ch_free( (void **)&ndn.bv_val );
923 rc = dnIsSuffix( &ndn, &nsuffix );
925 slapi_ch_free( (void **)&ndn.bv_val );
926 slapi_ch_free( (void **)&nsuffix.bv_val );
929 #else /* LDAP_SLAPI */
931 #endif /* LDAP_SLAPI */
935 slapi_dn_ignore_case( char *dn )
938 return slapi_dn_normalize_case( dn );
939 #else /* LDAP_SLAPI */
941 #endif /* LDAP_SLAPI */
945 slapi_ch_malloc( unsigned long size )
948 return ch_malloc( size );
949 #else /* LDAP_SLAPI */
951 #endif /* LDAP_SLAPI */
955 slapi_ch_free( void **ptr )
960 #endif /* LDAP_SLAPI */
964 slapi_ch_free_string( char **ptr )
967 slapi_ch_free( (void **)ptr );
968 #endif /* LDAP_SLAPI */
972 slapi_ch_array_free( char **arrayp )
977 if ( arrayp != NULL ) {
978 for ( p = arrayp; *p != NULL; p++ ) {
979 slapi_ch_free( (void **)p );
981 slapi_ch_free( (void **)&arrayp );
987 slapi_ch_bvdup(const struct berval *v)
992 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
993 bv->bv_len = v->bv_len;
994 bv->bv_val = slapi_ch_malloc( bv->bv_len );
995 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1004 slapi_ch_bvecdup(const struct berval **v)
1014 for ( i = 0; v[i] != NULL; i++ )
1017 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1019 for ( i = 0; v[i] != NULL; i++ ) {
1020 rv[i] = slapi_ch_bvdup( v[i] );
1032 unsigned long nelem,
1033 unsigned long size )
1036 return ch_calloc( nelem, size );
1037 #else /* LDAP_SLAPI */
1039 #endif /* LDAP_SLAPI */
1045 unsigned long size )
1048 return ch_realloc( block, size );
1049 #else /* LDAP_SLAPI */
1051 #endif /* LDAP_SLAPI */
1055 slapi_ch_strdup( char *s )
1058 return ch_strdup( (const char *)s );
1059 #else /* LDAP_SLAPI */
1061 #endif /* LDAP_SLAPI */
1065 slapi_ch_stlen( char *s )
1068 return strlen( (const char *)s );
1069 #else /* LDAP_SLAPI */
1071 #endif /* LDAP_SLAPI */
1075 slapi_control_present(
1076 LDAPControl **controls,
1078 struct berval **val,
1093 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1094 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1099 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1101 * FIXME: according to 6.1 specification,
1102 * "The val output parameter is set
1103 * to point into the controls array.
1104 * A copy of the control value is
1108 struct berval *pTmpBval;
1110 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
1111 if ( pTmpBval == NULL ) {
1114 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
1115 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
1119 slapi_ch_free( (void **)&pTmpBval );
1125 *val = &controls[i]->ldctl_value;
1130 *iscritical = controls[i]->ldctl_iscritical;
1137 #else /* LDAP_SLAPI */
1139 #endif /* LDAP_SLAPI */
1144 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1145 unsigned long *slapi_mask)
1147 *slapi_mask = SLAPI_OPERATION_NONE;
1149 if ( slap_mask & SLAP_CTRL_ABANDON )
1150 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1152 if ( slap_mask & SLAP_CTRL_ADD )
1153 *slapi_mask |= SLAPI_OPERATION_ADD;
1155 if ( slap_mask & SLAP_CTRL_BIND )
1156 *slapi_mask |= SLAPI_OPERATION_BIND;
1158 if ( slap_mask & SLAP_CTRL_COMPARE )
1159 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1161 if ( slap_mask & SLAP_CTRL_DELETE )
1162 *slapi_mask |= SLAPI_OPERATION_DELETE;
1164 if ( slap_mask & SLAP_CTRL_MODIFY )
1165 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1167 if ( slap_mask & SLAP_CTRL_RENAME )
1168 *slapi_mask |= SLAPI_OPERATION_MODDN;
1170 if ( slap_mask & SLAP_CTRL_SEARCH )
1171 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1173 if ( slap_mask & SLAP_CTRL_UNBIND )
1174 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1178 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1179 slap_mask_t *slap_mask)
1183 if ( slapi_mask & SLAPI_OPERATION_BIND )
1184 *slap_mask |= SLAP_CTRL_BIND;
1186 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1187 *slap_mask |= SLAP_CTRL_UNBIND;
1189 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1190 *slap_mask |= SLAP_CTRL_SEARCH;
1192 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1193 *slap_mask |= SLAP_CTRL_MODIFY;
1195 if ( slapi_mask & SLAPI_OPERATION_ADD )
1196 *slap_mask |= SLAP_CTRL_ADD;
1198 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1199 *slap_mask |= SLAP_CTRL_DELETE;
1201 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1202 *slap_mask |= SLAP_CTRL_RENAME;
1204 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1205 *slap_mask |= SLAP_CTRL_COMPARE;
1207 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1208 *slap_mask |= SLAP_CTRL_ABANDON;
1210 *slap_mask |= SLAP_CTRL_FRONTEND;
1219 /* Plugins must deal with controls themselves. */
1221 return LDAP_SUCCESS;
1223 #endif /* LDAP_SLAPI */
1226 slapi_register_supported_control(
1228 unsigned long controlops )
1231 slap_mask_t controlmask;
1233 slapiControlOp2SlapControlMask( controlops, &controlmask );
1235 register_supported_control( controloid, controlmask, NULL, parseSlapiControl );
1236 #endif /* LDAP_SLAPI */
1240 slapi_get_supported_controls(
1242 unsigned long **ctrlopsp )
1247 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1248 if ( rc != LDAP_SUCCESS ) {
1252 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1253 /* In place, naughty. */
1254 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1257 return LDAP_SUCCESS;
1258 #else /* LDAP_SLAPI */
1260 #endif /* LDAP_SLAPI */
1264 slapi_dup_control( LDAPControl *ctrl )
1269 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1270 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1271 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1272 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1277 #endif /* LDAP_SLAPI */
1281 slapi_register_supported_saslmechanism( char *mechanism )
1284 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1285 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
1286 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1287 #endif /* LDAP_SLAPI */
1291 slapi_get_supported_saslmechanisms( void )
1294 /* FIXME -- can not get the saslmechanism without a connection. */
1295 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
1296 "can not get the SASL mechanism list "
1297 "without a connection\n" );
1299 #else /* LDAP_SLAPI */
1301 #endif /* LDAP_SLAPI */
1305 slapi_get_supported_extended_ops( void )
1309 char **ppExtOpOID = NULL;
1312 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1316 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
1321 if ( numExtOps == 0 ) {
1325 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1326 for ( k = 0; k < i; k++ ) {
1329 bv = get_supported_extop( k );
1330 assert( bv != NULL );
1332 ppExtOpOID[ k ] = bv->bv_val;
1335 for ( ; k < j; k++ ) {
1338 bv = ns_get_supported_extop( k );
1339 assert( bv != NULL );
1341 ppExtOpOID[ i + k ] = bv->bv_val;
1343 ppExtOpOID[ i + k ] = NULL;
1346 #else /* LDAP_SLAPI */
1348 #endif /* LDAP_SLAPI */
1352 slapi_send_ldap_result(
1358 struct berval **urls )
1363 char *extOID = NULL;
1364 struct berval *extValue = NULL;
1366 SlapReply rs = { REP_RESULT };
1368 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1371 rs.sr_matched = matched;
1376 slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
1378 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1379 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
1380 send_ldap_sasl( op, &rs );
1384 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1385 if ( extOID != NULL ) {
1386 rs.sr_rspoid = extOID;
1387 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
1388 send_ldap_extended( op, &rs );
1392 if (op->o_tag == LDAP_REQ_SEARCH)
1393 rs.sr_nentries = nentries;
1395 send_ldap_result( op, &rs );
1396 #endif /* LDAP_SLAPI */
1400 slapi_send_ldap_search_entry(
1403 LDAPControl **ectrls,
1409 SlapReply rs = { REP_RESULT };
1411 AttributeName *an = NULL;
1414 if ( attrs != NULL ) {
1415 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1423 an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
1424 for ( i = 0; attrs[i] != NULL; i++ ) {
1425 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1426 an[i].an_name.bv_len = strlen( attrs[i] );
1427 an[i].an_desc = NULL;
1428 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1431 an[i].an_name.bv_len = 0;
1432 an[i].an_name.bv_val = NULL;
1435 rs.sr_err = LDAP_SUCCESS;
1436 rs.sr_matched = NULL;
1439 rs.sr_ctrls = ectrls;
1444 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1448 return send_search_entry( pOp, &rs );
1449 #else /* LDAP_SLAPI */
1451 #endif /* LDAP_SLAPI */
1456 slapi_str2filter( char *str )
1459 return str2filter( str );
1460 #else /* LDAP_SLAPI */
1462 #endif /* LDAP_SLAPI */
1472 #endif /* LDAP_SLAPI */
1476 slapi_filter_dup( Slapi_Filter *filter )
1481 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1483 f->f_choice = filter->f_choice;
1485 switch ( f->f_choice ) {
1486 case LDAP_FILTER_AND:
1487 case LDAP_FILTER_NOT:
1488 case LDAP_FILTER_OR: {
1489 Filter *pFilter, **ppF;
1491 for ( pFilter = filter->f_list, ppF = &f->f_list;
1493 pFilter = pFilter->f_next, ppF = &f->f_next )
1495 *ppF = slapi_filter_dup( pFilter );
1501 case LDAP_FILTER_PRESENT:
1502 f->f_desc = filter->f_desc;
1504 case LDAP_FILTER_EQUALITY:
1505 case LDAP_FILTER_GE:
1506 case LDAP_FILTER_LE:
1507 case LDAP_FILTER_APPROX:
1508 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1509 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1510 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1512 case LDAP_FILTER_EXT:
1513 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1514 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1515 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1516 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1517 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1518 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1520 case LDAP_FILTER_SUBSTRINGS: {
1523 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1524 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1525 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1526 if ( filter->f_sub_any != NULL ) {
1527 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1529 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1530 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1531 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1533 f->f_sub_any[i].bv_val = NULL;
1535 f->f_sub_any = NULL;
1537 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1540 case SLAPD_FILTER_COMPUTED:
1541 f->f_result = filter->f_result;
1544 slapi_ch_free( (void **)&f );
1552 #endif /* LDAP_SLAPI */
1556 slapi_filter_get_choice( Slapi_Filter *f )
1568 #else /* LDAP_SLAPI */
1569 return -1; /* invalid filter type */
1570 #endif /* LDAP_SLAPI */
1574 slapi_filter_get_ava(
1577 struct berval **bval )
1581 int rc = LDAP_SUCCESS;
1583 assert( type != NULL );
1584 assert( bval != NULL );
1589 ftype = f->f_choice;
1590 if ( ftype == LDAP_FILTER_EQUALITY
1591 || ftype == LDAP_FILTER_GE
1592 || ftype == LDAP_FILTER_LE
1593 || ftype == LDAP_FILTER_APPROX ) {
1595 * According to the SLAPI Reference Manual these are
1598 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1599 *bval = &f->f_un.f_un_ava->aa_value;
1600 } else { /* filter type not supported */
1605 #else /* LDAP_SLAPI */
1607 #endif /* LDAP_SLAPI */
1611 slapi_filter_list_first( Slapi_Filter *f )
1620 ftype = f->f_choice;
1621 if ( ftype == LDAP_FILTER_AND
1622 || ftype == LDAP_FILTER_OR
1623 || ftype == LDAP_FILTER_NOT ) {
1624 return (Slapi_Filter *)f->f_list;
1628 #else /* LDAP_SLAPI */
1630 #endif /* LDAP_SLAPI */
1634 slapi_filter_list_next(
1636 Slapi_Filter *fprev )
1645 ftype = f->f_choice;
1646 if ( ftype == LDAP_FILTER_AND
1647 || ftype == LDAP_FILTER_OR
1648 || ftype == LDAP_FILTER_NOT )
1650 return fprev->f_next;
1654 #else /* LDAP_SLAPI */
1656 #endif /* LDAP_SLAPI */
1660 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1667 switch ( f->f_choice ) {
1668 case LDAP_FILTER_GE:
1669 case LDAP_FILTER_LE:
1670 case LDAP_FILTER_EQUALITY:
1671 case LDAP_FILTER_APPROX:
1672 *type = f->f_av_desc->ad_cname.bv_val;
1674 case LDAP_FILTER_SUBSTRINGS:
1675 *type = f->f_sub_desc->ad_cname.bv_val;
1677 case LDAP_FILTER_PRESENT:
1678 *type = f->f_desc->ad_cname.bv_val;
1680 case LDAP_FILTER_EXT:
1681 *type = f->f_mr_desc->ad_cname.bv_val;
1684 /* Complex filters need not apply. */
1692 #endif /* LDAP_SLAPI */
1696 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1697 char ***any, char **final )
1702 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1707 * The caller shouldn't free but we can't return an
1708 * array of char *s from an array of bervals without
1709 * allocating memory, so we may as well be consistent.
1712 *type = f->f_sub_desc->ad_cname.bv_val;
1713 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1714 if ( f->f_sub_any != NULL ) {
1715 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1717 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1718 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1719 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1725 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1730 #endif /* LDAP_SLAPI */
1734 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1737 Slapi_Filter *f = NULL;
1739 if ( ftype == LDAP_FILTER_AND ||
1740 ftype == LDAP_FILTER_OR ||
1741 ftype == LDAP_FILTER_NOT )
1743 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1744 f->f_choice = ftype;
1746 f->f_list->f_next = f2;
1753 #endif /* LDAP_SLAPI */
1757 slapi_x_filter_append( int ftype,
1758 Slapi_Filter **pContainingFilter, /* NULL on first call */
1759 Slapi_Filter **pNextFilter,
1760 Slapi_Filter *filterToAppend )
1763 if ( ftype == LDAP_FILTER_AND ||
1764 ftype == LDAP_FILTER_OR ||
1765 ftype == LDAP_FILTER_NOT )
1767 if ( *pContainingFilter == NULL ) {
1768 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
1769 (*pContainingFilter)->f_choice = ftype;
1770 (*pContainingFilter)->f_list = filterToAppend;
1771 (*pContainingFilter)->f_next = NULL;
1773 if ( (*pContainingFilter)->f_choice != ftype ) {
1777 (*pNextFilter)->f_next = filterToAppend;
1779 *pNextFilter = filterToAppend;
1783 #endif /* LDAP_SLAPI */
1788 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1796 /* spec says return zero if no filter. */
1800 if ( verify_access ) {
1801 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
1803 return LDAP_PARAM_ERROR;
1809 * According to acl.c it is safe to call test_filter() with
1812 rc = test_filter( op, e, f );
1814 case LDAP_COMPARE_TRUE:
1817 case LDAP_COMPARE_FALSE:
1819 case SLAPD_COMPARE_UNDEFINED:
1822 case LDAP_PROTOCOL_ERROR:
1823 /* filter type unknown: spec says return -1 */
1831 #endif /* LDAP_SLAPI */
1835 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
1838 return slapi_filter_test( NULL, e, f, 0 );
1845 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
1848 switch ( f->f_choice ) {
1849 case LDAP_FILTER_AND:
1850 case LDAP_FILTER_NOT:
1851 case LDAP_FILTER_OR: {
1855 * FIXME: altering f; should we use a temporary?
1857 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1858 rc = slapi_filter_apply( f, fn, arg, error_code );
1862 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
1868 case LDAP_FILTER_EQUALITY:
1869 case LDAP_FILTER_SUBSTRINGS:
1870 case LDAP_FILTER_GE:
1871 case LDAP_FILTER_LE:
1872 case LDAP_FILTER_PRESENT:
1873 case LDAP_FILTER_APPROX:
1874 case LDAP_FILTER_EXT:
1875 *error_code = fn( f, arg );
1878 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1881 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
1882 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
1888 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1890 #endif /* LDAP_SLAPI */
1894 slapi_send_ldap_extended_response(
1899 struct berval *response )
1904 rs.sr_err = errornum;
1905 rs.sr_matched = NULL;
1909 rs.sr_rspoid = respName;
1910 rs.sr_rspdata = response;
1912 send_ldap_extended( op, &rs );
1914 return LDAP_SUCCESS;
1915 #else /* LDAP_SLAPI */
1917 #endif /* LDAP_SLAPI */
1922 struct berval **vals,
1927 * FIXME: what's the point?
1930 #else /* LDAP_SLAPI */
1932 #endif /* LDAP_SLAPI */
1935 #define MAX_HOSTNAME 512
1938 slapi_get_hostname( void )
1944 * FIXME: I'd prefer a different check ...
1947 hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1949 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1950 "can't malloc memory for hostname\n" );
1953 } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
1954 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1955 "can't get hostname\n" );
1956 slapi_ch_free( (void **)&hn );
1960 static int been_here = 0;
1961 static char *static_hn = NULL;
1963 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1965 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1966 if ( static_hn == NULL) {
1967 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1968 "can't malloc memory for hostname\n" );
1970 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1975 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1976 slapi_log_error( SLAPI_LOG_FATAL,
1978 "can't get hostname\n" );
1979 slapi_ch_free( (void **)&static_hn );
1981 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1990 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1992 hn = ch_strdup( static_hn );
1993 #endif /* !_SPARC */
1996 #else /* LDAP_SLAPI */
1998 #endif /* LDAP_SLAPI */
2002 * FIXME: this should go in an appropriate header ...
2004 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
2014 int rc = LDAP_SUCCESS;
2017 va_start( arglist, fmt );
2018 rc = vLogError( severity, subsystem, fmt, arglist );
2022 #else /* LDAP_SLAPI */
2024 #endif /* LDAP_SLAPI */
2029 slapi_timer_current_time( void )
2032 static int first_time = 1;
2033 #if !defined (_WIN32)
2037 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
2040 gettimeofday( &base_time, NULL );
2042 gettimeofday( &now, NULL );
2043 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
2044 (now.tv_usec - base_time.tv_usec);
2045 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
2051 return (slap_get_time() - starttime) * 1000000;
2058 performance_counter_present = QueryPerformanceCounter( &base_time );
2059 QueryPerformanceFrequency( &performance_freq );
2062 if ( !performance_counter_present )
2065 QueryPerformanceCounter( &now );
2066 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2068 #else /* LDAP_SLAPI */
2070 #endif /* LDAP_SLAPI */
2077 slapi_timer_get_time( char *label )
2080 unsigned long start = slapi_timer_current_time();
2081 printf("%10ld %10ld usec %s\n", start, 0, label);
2083 #else /* LDAP_SLAPI */
2085 #endif /* LDAP_SLAPI */
2092 slapi_timer_elapsed_time(
2094 unsigned long start )
2097 unsigned long stop = slapi_timer_current_time();
2098 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2099 #endif /* LDAP_SLAPI */
2103 slapi_free_search_results_internal( Slapi_PBlock *pb )
2106 Slapi_Entry **entries;
2107 int k = 0, nEnt = 0;
2109 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2110 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2115 if ( entries == NULL ) {
2119 for ( k = 0; k < nEnt; k++ ) {
2120 slapi_entry_free( entries[k] );
2123 slapi_ch_free( (void **)&entries );
2124 #endif /* LDAP_SLAPI */
2129 * Internal API to prime a Slapi_PBlock with a Backend.
2131 static int initBackendPB( Slapi_PBlock *pb, Backend *be )
2135 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2136 if ( rc != LDAP_SUCCESS )
2140 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2141 if ( rc != LDAP_SUCCESS )
2145 return LDAP_SUCCESS;
2149 * If oldStyle is TRUE, then a value suitable for setting to
2150 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
2151 * (pointer to static storage).
2153 * If oldStyle is FALSE, then a value suitable for setting to
2154 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2155 * a pointer to allocated memory and will include the SASL
2156 * mechanism (if any).
2158 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2163 switch ( authz->sai_method ) {
2164 case LDAP_AUTH_SASL:
2166 authType = SLAPD_AUTH_SASL;
2168 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2169 authType = slapi_ch_malloc( len );
2170 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2173 case LDAP_AUTH_SIMPLE:
2174 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2176 case LDAP_AUTH_NONE:
2177 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2183 if ( is_tls && authType == NULL ) {
2184 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2191 * Internal API to prime a Slapi_PBlock with a Connection.
2193 static int initConnectionPB( Slapi_PBlock *pb, Connection *conn )
2198 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2199 if ( rc != LDAP_SUCCESS )
2202 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2203 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2204 if ( rc != LDAP_SUCCESS )
2206 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2207 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2208 if ( rc != LDAP_SUCCESS )
2212 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2213 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2214 if ( rc != LDAP_SUCCESS )
2216 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2217 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2218 if ( rc != LDAP_SUCCESS )
2222 #ifdef LDAP_CONNECTIONLESS
2223 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2224 if ( rc != LDAP_SUCCESS )
2228 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2229 if ( rc != LDAP_SUCCESS )
2232 /* Returns pointer to static string */
2233 connAuthType = Authorization2AuthType( &conn->c_authz,
2240 if ( connAuthType != NULL ) {
2241 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2242 if ( rc != LDAP_SUCCESS )
2246 /* Returns pointer to allocated string */
2247 connAuthType = Authorization2AuthType( &conn->c_authz,
2254 if ( connAuthType != NULL ) {
2255 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2256 if ( rc != LDAP_SUCCESS )
2260 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
2261 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
2262 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
2263 if ( rc != LDAP_SUCCESS )
2269 #endif /* LDAP_SLAPI */
2272 * Internal API to prime a Slapi_PBlock with an Operation.
2274 int slapi_x_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
2282 if ( op->o_bd != NULL ) {
2283 isRoot = be_isroot( op->o_bd, &op->o_ndn );
2284 isUpdateDn = be_isupdate( op->o_bd, &op->o_ndn );
2287 rc = initBackendPB( pb, op->o_bd );
2288 if ( rc != LDAP_SUCCESS )
2291 rc = initConnectionPB( pb, op->o_conn );
2292 if ( rc != LDAP_SUCCESS )
2295 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2296 if ( rc != LDAP_SUCCESS )
2299 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2300 if ( rc != LDAP_SUCCESS )
2303 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2304 if ( rc != LDAP_SUCCESS )
2307 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2308 if ( rc != LDAP_SUCCESS )
2311 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2312 if ( rc != LDAP_SUCCESS )
2315 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2316 if ( rc != LDAP_SUCCESS )
2319 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2320 if ( rc != LDAP_SUCCESS)
2323 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2324 if ( rc != LDAP_SUCCESS )
2327 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2328 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
2329 /* Not quite sure what the point of this is. */
2330 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
2331 if ( rc != LDAP_SUCCESS )
2335 return LDAP_SUCCESS;
2341 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2346 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2348 *isSSL = conn->c_is_tls;
2353 return LDAP_SUCCESS;
2356 #endif /* LDAP_SLAPI */
2360 * DS 5.x compatability API follow
2363 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2369 return LDAP_PARAM_ERROR;
2371 at = attr->a_desc->ad_type;
2373 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2375 if ( is_at_single_value( at ) )
2376 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2377 if ( is_at_operational( at ) )
2378 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2379 if ( is_at_obsolete( at ) )
2380 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2381 if ( is_at_collective( at ) )
2382 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2383 if ( is_at_no_user_mod( at ) )
2384 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2386 return LDAP_SUCCESS;
2389 #endif /* LDAP_SLAPI */
2392 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2395 unsigned long flags;
2397 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2399 return (flags & flag) ? 1 : 0;
2402 #endif /* LDAP_SLAPI */
2405 Slapi_Attr *slapi_attr_new( void )
2410 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2418 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2422 AttributeDescription *ad = NULL;
2424 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2440 void slapi_attr_free( Slapi_Attr **a )
2448 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2451 return attr_dup( (Slapi_Attr *)attr );
2457 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2461 * FIXME: here we may lose alignment between a_vals/a_nvals
2463 return value_add_one( &a->a_vals, (Slapi_Value *)v );
2469 int slapi_attr_type2plugin( const char *type, void **pi )
2476 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2479 if ( attr == NULL ) {
2480 return LDAP_PARAM_ERROR;
2483 *type = attr->a_desc->ad_cname.bv_val;
2485 return LDAP_SUCCESS;
2491 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2494 if ( attr == NULL ) {
2495 return LDAP_PARAM_ERROR;
2497 *oidp = attr->a_desc->ad_type->sat_oid;
2499 return LDAP_SUCCESS;
2505 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2513 mr = a->a_desc->ad_type->sat_equality;
2514 rc = value_match( &ret, a->a_desc, mr,
2515 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2516 (struct berval *)v1, (void *)v2, &text );
2517 if ( rc != LDAP_SUCCESS )
2520 return ( ret == 0 ) ? 0 : -1;
2526 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2536 if ( a ->a_vals == NULL ) {
2539 mr = a->a_desc->ad_type->sat_equality;
2540 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2541 rc = value_match( &ret, a->a_desc, mr,
2542 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2543 if ( rc != LDAP_SUCCESS ) {
2550 #endif /* LDAP_SLAPI */
2554 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2557 AttributeDescription *a1 = NULL;
2558 AttributeDescription *a2 = NULL;
2562 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2566 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2570 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2571 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2572 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2575 case SLAPI_TYPE_CMP_EXACT:
2576 ret = ad_cmp( a1, a2 );
2578 case SLAPI_TYPE_CMP_BASE:
2579 ret = ad_base_cmp( a1, a2 );
2581 case SLAPI_TYPE_CMP_SUBTYPE:
2582 ret = is_ad_subtype( a2, a2 );
2595 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2598 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2604 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2607 return slapi_valueset_first_value( &a->a_vals, v );
2613 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2616 return slapi_valueset_next_value( &a->a_vals, hint, v );
2622 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2625 *numValues = slapi_valueset_count( &a->a_vals );
2633 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2636 *vs = &((Slapi_Attr *)a)->a_vals;
2644 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2647 return slapi_attr_get_values( a, vals );
2653 char *slapi_attr_syntax_normalize( const char *s )
2656 AttributeDescription *ad = NULL;
2659 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2663 return ad->ad_cname.bv_val;
2669 Slapi_Value *slapi_value_new( void )
2674 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2682 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2685 return ber_dupbv( NULL, (struct berval *)bval );
2691 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2694 return slapi_value_new_berval( v );
2700 Slapi_Value *slapi_value_new_string(const char *s)
2705 bv.bv_val = (char *)s;
2706 bv.bv_len = strlen( s );
2708 return slapi_value_new_berval( &bv );
2714 Slapi_Value *slapi_value_init(Slapi_Value *val)
2726 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2729 return ber_dupbv( v, bval );
2735 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2738 v->bv_val = slapi_ch_strdup( (char *)s );
2739 v->bv_len = strlen( s );
2747 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2750 return slapi_value_new_value( v );
2756 void slapi_value_free(Slapi_Value **value)
2759 if ( value == NULL ) {
2763 if ( (*value) != NULL ) {
2764 slapi_ch_free( (void **)&(*value)->bv_val );
2765 slapi_ch_free( (void **)value );
2770 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2779 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2782 if ( value == NULL ) {
2785 if ( value->bv_val != NULL ) {
2786 slapi_ch_free( (void **)&value->bv_val );
2788 slapi_value_init_berval( value, (struct berval *)bval );
2796 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2799 if ( value == NULL ) {
2802 return slapi_value_set_berval( value, vfrom );
2808 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2811 if ( value == NULL ) {
2814 if ( value->bv_val != NULL ) {
2815 slapi_ch_free( (void **)&value->bv_val );
2817 value->bv_val = slapi_ch_malloc( len );
2818 value->bv_len = len;
2819 AC_MEMCPY( value->bv_val, val, len );
2827 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2830 if ( value == NULL ) {
2833 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2840 int slapi_value_set_int(Slapi_Value *value, int intVal)
2845 snprintf( buf, sizeof( buf ), "%d", intVal );
2847 return slapi_value_set_string( value, buf );
2853 const char *slapi_value_get_string(const Slapi_Value *value)
2856 if ( value == NULL ) {
2859 return value->bv_val;
2866 static int checkBVString(const struct berval *bv)
2870 for ( i = 0; i < bv->bv_len; i++ ) {
2871 if ( bv->bv_val[i] == '\0' )
2874 if ( bv->bv_val[i] != '\0' )
2881 int slapi_value_get_int(const Slapi_Value *value)
2884 if ( value == NULL ) return 0;
2885 if ( value->bv_val == NULL ) return 0;
2886 if ( !checkBVString( value ) ) return 0;
2888 return (int)strtol( value->bv_val, NULL, 10 );
2894 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2897 if ( value == NULL ) return 0;
2898 if ( value->bv_val == NULL ) return 0;
2899 if ( !checkBVString( value ) ) return 0;
2901 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2907 long slapi_value_get_long(const Slapi_Value *value)
2910 if ( value == NULL ) return 0;
2911 if ( value->bv_val == NULL ) return 0;
2912 if ( !checkBVString( value ) ) return 0;
2914 return strtol( value->bv_val, NULL, 10 );
2920 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2923 if ( value == NULL ) return 0;
2924 if ( value->bv_val == NULL ) return 0;
2925 if ( !checkBVString( value ) ) return 0;
2927 return strtoul( value->bv_val, NULL, 10 );
2933 size_t slapi_value_get_length(const Slapi_Value *value)
2936 if ( value == NULL )
2939 return (size_t) value->bv_len;
2945 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2948 return slapi_attr_value_cmp( a, v1, v2 );
2954 /* A ValueSet is a container for a BerVarray. */
2955 Slapi_ValueSet *slapi_valueset_new( void )
2960 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2969 void slapi_valueset_free(Slapi_ValueSet *vs)
2975 ber_bvarray_free( vp );
2976 slapi_ch_free( (void **)&vp );
2983 void slapi_valueset_init(Slapi_ValueSet *vs)
2986 if ( vs != NULL && *vs == NULL ) {
2987 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2988 (*vs)->bv_val = NULL;
2994 void slapi_valueset_done(Slapi_ValueSet *vs)
3002 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
3004 slapi_ch_free( (void **)&vp->bv_val );
3006 /* but don't free *vs or vs */
3010 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
3015 ber_dupbv( &bv, (Slapi_Value *)addval );
3016 ber_bvarray_add( vs, &bv );
3020 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
3023 return slapi_valueset_next_value( vs, 0, v );
3029 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
3040 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
3051 int slapi_valueset_count( const Slapi_ValueSet *vs )
3062 for ( i = 0; vp[i].bv_val != NULL; i++ )
3072 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
3077 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
3078 slapi_valueset_add_value( vs1, vp );
3083 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
3084 struct berval *val, int access )
3091 slap_access_t slap_access;
3092 AttributeDescription *ad = NULL;
3095 ret = slap_str2ad( attr, &ad, &text );
3096 if ( ret != LDAP_SUCCESS ) {
3100 switch ( access & SLAPI_ACL_ALL ) {
3101 case SLAPI_ACL_COMPARE:
3102 slap_access = ACL_COMPARE;
3104 case SLAPI_ACL_SEARCH:
3105 slap_access = ACL_SEARCH;
3107 case SLAPI_ACL_READ:
3108 slap_access = ACL_READ;
3110 case SLAPI_ACL_WRITE:
3111 case SLAPI_ACL_DELETE:
3113 case SLAPI_ACL_SELF:
3114 slap_access = ACL_WRITE;
3117 return LDAP_INSUFFICIENT_ACCESS;
3121 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3122 return LDAP_PARAM_ERROR;
3125 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3126 return LDAP_PARAM_ERROR;
3129 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3130 return LDAP_PARAM_ERROR;
3133 ret = access_allowed( be, conn, op, e, desc, val, slap_access, NULL );
3135 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3137 return LDAP_UNWILLING_TO_PERFORM;
3141 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
3147 Modifications *next;
3149 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3150 return LDAP_PARAM_ERROR;
3153 ml = slapi_x_ldapmods2modifications( mods );
3158 ret = acl_check_modlist( op, e, ml );
3160 /* Careful when freeing the modlist because it has pointers into the mods array. */
3161 for ( ; ml != NULL; ml = next ) {
3162 next = ml->sml_next;
3164 /* just free the containing array */
3165 slapi_ch_free( (void **)&ml->sml_bvalues );
3166 slapi_ch_free( (void **)&ml );
3169 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3171 return LDAP_UNWILLING_TO_PERFORM;
3176 * Synthesise an LDAPMod array from a Modifications list to pass
3177 * to SLAPI. This synthesis is destructive and as such the
3178 * Modifications list may not be used after calling this
3181 * This function must also be called before slap_mods_check().
3183 LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
3186 Modifications *ml, *modlist;
3187 LDAPMod **mods, *modp;
3190 modlist = *pmodlist;
3192 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3195 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3197 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3198 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3200 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3202 /* Take ownership of original type. */
3203 modp->mod_type = ml->sml_type.bv_val;
3204 ml->sml_type.bv_val = NULL;
3206 if ( ml->sml_bvalues != NULL ) {
3207 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
3209 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3210 sizeof(struct berval *) );
3211 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
3212 /* Take ownership of original values. */
3213 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3214 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
3215 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
3216 ml->sml_bvalues[j].bv_len = 0;
3217 ml->sml_bvalues[j].bv_val = NULL;
3219 modp->mod_bvalues[j] = NULL;
3221 modp->mod_bvalues = NULL;
3228 slap_mods_free( modlist );
3238 * Convert a potentially modified array of LDAPMods back to a
3239 * Modification list.
3241 * The returned Modification list contains pointers into the
3242 * LDAPMods array; the latter MUST be freed with
3243 * slapi_x_free_ldapmods() (see below).
3245 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
3248 Modifications *modlist = NULL, **modtail;
3253 for( modp = mods; *modp != NULL; modp++ ) {
3257 struct berval **bvp;
3259 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3260 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3261 mod->sml_type.bv_val = (*modp)->mod_type;
3262 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3263 mod->sml_desc = NULL;
3264 mod->sml_next = NULL;
3266 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3267 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3270 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3275 mod->sml_bvalues = NULL;
3277 mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3279 /* NB: This implicitly trusts a plugin to return valid modifications. */
3280 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3281 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3282 mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
3283 mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
3286 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3287 mod->sml_bvalues[i].bv_val = *p;
3288 mod->sml_bvalues[i].bv_len = strlen( *p );
3291 mod->sml_bvalues[i].bv_val = NULL;
3293 mod->sml_nvalues = NULL;
3296 modtail = &mod->sml_next;
3306 * This function only frees the parts of the mods array that
3307 * are not shared with the Modification list that was created
3308 * by slapi_x_ldapmods2modifications().
3311 void slapi_x_free_ldapmods (LDAPMod **mods)
3319 for ( i = 0; mods[i] != NULL; i++ ) {
3321 * Don't free values themselves; they're owned by the
3322 * Modification list. Do free the containing array.
3324 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3325 for ( j = 0; mods[i]->mod_bvalues != NULL && mods[i]->mod_bvalues[j] != NULL; j++ ) {
3326 ch_free( mods[i]->mod_bvalues[j] );
3328 ch_free( mods[i]->mod_bvalues );
3330 ch_free( mods[i]->mod_values );
3332 /* Don't free type, for same reasons. */
3336 #endif /* LDAP_SLAPI */
3340 * Sun ONE DS 5.x computed attribute support. Computed attributes
3341 * allow for dynamically generated operational attributes, a very
3342 * useful thing indeed.
3346 * Write the computed attribute to a BerElement. Complementary
3347 * functions need to be defined for anything that replaces
3348 * op->o_callback->sc_sendentry, if you wish to make computed
3349 * attributes available to it.
3351 int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
3354 Operation *op = NULL;
3356 AttributeDescription *desc = NULL;
3372 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
3373 if ( rc != 0 || op == NULL ) {
3377 ber = (BerElement *)c->cac_private;
3380 if ( c->cac_attrs == NULL ) {
3381 /* All attrs request, skip operational attributes */
3382 if ( is_at_operational( desc->ad_type ) ) {
3386 /* Specific attrs requested */
3387 if ( is_at_operational( desc->ad_type ) ) {
3388 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3392 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3398 if ( !access_allowed( op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
3399 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
3400 "acl: access to attribute %s not allowed\n",
3401 desc->ad_cname.bv_val );
3405 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
3407 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3408 "ber_printf failed\n");
3412 if ( !c->cac_attrsonly ) {
3413 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
3414 if ( !access_allowed( op, e,
3415 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
3416 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
3417 "slapi_x_compute_output_ber: conn %lu "
3418 "acl: access to %s, value %d not allowed\n",
3419 op->o_connid, desc->ad_cname.bv_val, i );
3423 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
3424 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3425 "ber_printf failed\n");
3431 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
3432 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3433 "ber_printf failed\n" );
3444 * For some reason Sun don't use the normal plugin mechanism
3445 * registration path to register an "evaluator" function (an
3446 * "evaluator" is responsible for adding computed attributes;
3447 * the nomenclature is somewhat confusing).
3449 * As such slapi_compute_add_evaluator() registers the
3450 * function directly.
3452 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3455 Slapi_PBlock *pPlugin = NULL;
3458 pPlugin = slapi_pblock_new();
3459 if ( pPlugin == NULL ) {
3460 rc = LDAP_NO_MEMORY;
3464 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3465 if ( rc != LDAP_SUCCESS ) {
3469 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3470 if ( rc != LDAP_SUCCESS ) {
3474 rc = insertPlugin( NULL, pPlugin );
3481 if ( rc != LDAP_SUCCESS ) {
3482 if ( pPlugin != NULL ) {
3483 slapi_pblock_destroy( pPlugin );
3491 #endif /* LDAP_SLAPI */
3495 * See notes above regarding slapi_compute_add_evaluator().
3497 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3500 Slapi_PBlock *pPlugin = NULL;
3503 pPlugin = slapi_pblock_new();
3504 if ( pPlugin == NULL ) {
3505 rc = LDAP_NO_MEMORY;
3509 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3510 if ( rc != LDAP_SUCCESS ) {
3514 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3515 if ( rc != LDAP_SUCCESS ) {
3519 rc = insertPlugin( NULL, pPlugin );
3526 if ( rc != LDAP_SUCCESS ) {
3527 if ( pPlugin != NULL ) {
3528 slapi_pblock_destroy( pPlugin );
3536 #endif /* LDAP_SLAPI */
3540 * Call compute evaluators
3542 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3546 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3548 rc = getAllPluginFuncs( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3549 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3550 /* Nothing to do; front-end should ignore. */
3554 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3556 * -1: no attribute matched requested type
3557 * 0: one attribute matched
3558 * >0: error happened
3560 rc = (*pGetPlugin)( c, type, e, outputfn );
3566 slapi_ch_free( (void **)&tmpPlugin );
3571 #endif /* LDAP_SLAPI */
3574 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3580 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3585 return doPluginFNs( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3588 #endif /* LDAP_SLAPI */
3592 * New API to provide the plugin with access to the search
3593 * pblock. Have informed Sun DS team.
3595 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3601 if ( c->cac_pb == NULL )
3609 #endif /* LDAP_SLAPI */
3612 Slapi_Mutex *slapi_new_mutex( void )
3617 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3618 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3619 slapi_ch_free( (void **)&m );
3629 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3632 if ( mutex != NULL ) {
3633 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3634 slapi_ch_free( (void **)&mutex);
3639 void slapi_lock_mutex( Slapi_Mutex *mutex )
3642 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3646 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3649 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3655 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3660 if ( mutex == NULL ) {
3664 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3665 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3666 slapi_ch_free( (void **)&cv );
3670 /* XXX struct copy */
3671 cv->mutex = mutex->mutex;
3679 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3682 if ( cvar != NULL ) {
3683 ldap_pvt_thread_cond_destroy( &cvar->cond );
3684 slapi_ch_free( (void **)&cvar );
3689 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3692 if ( cvar == NULL ) {
3696 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3702 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3705 if ( cvar == NULL ) {
3710 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3713 return ldap_pvt_thread_cond_signal( &cvar->cond );