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 * FIXME: sm_bvalues = NULL ? */
561 mod.sm_bvalues = (BerVarray)ch_malloc( sizeof(struct berval) );
562 mod.sm_bvalues->bv_val = NULL;
565 rc = bvptr2obj( vals, &mod.sm_bvalues );
566 if ( rc != LDAP_SUCCESS ) {
567 return LDAP_CONSTRAINT_VIOLATION;
571 mod.sm_nvalues = NULL;
574 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
576 ch_free( mod.sm_bvalues );
578 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
581 #endif /* LDAP_SLAPI */
585 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
588 return slapi_entry_add_values( e, type, vals );
591 #endif /* LDAP_SLAPI */
595 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
598 AttributeDescription *ad = NULL;
602 rc = slap_str2ad( type, &ad, &text );
603 if ( rc != LDAP_SUCCESS ) {
607 return attr_mergeit( e, ad, *vs );
610 #endif /* LDAP_SLAPI */
614 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
620 char textbuf[SLAP_TEXT_BUFLEN];
622 mod.sm_op = LDAP_MOD_DELETE;
624 mod.sm_type.bv_val = (char *)type;
625 mod.sm_type.bv_len = strlen( type );
627 if ( vals == NULL ) {
628 /* If vals is NULL, this is a NOOP. */
632 rc = slap_str2ad( type, &mod.sm_desc, &text );
633 if ( rc != LDAP_SUCCESS ) {
637 if ( vals[0] == NULL ) {
638 /* SLAPI doco says LDAP_OPERATIONS_ERROR but LDAP_OTHER is better */
639 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
642 rc = bvptr2obj( vals, &mod.sm_bvalues );
643 if ( rc != LDAP_SUCCESS ) {
644 return LDAP_CONSTRAINT_VIOLATION;
647 mod.sm_nvalues = NULL;
650 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
652 ch_free( mod.sm_bvalues );
657 #endif /* LDAP_SLAPI */
661 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
664 return slapi_entry_delete_values( e, type, vals );
667 #endif /* LDAP_SLAPI */
671 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
674 return slapi_entry_attr_merge( e, (char *)type, vals );
677 #endif /* LDAP_SLAPI */
681 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
684 AttributeDescription *ad = NULL;
688 rc = slap_str2ad( type, &ad, &text );
689 if ( rc != LDAP_SUCCESS ) {
693 rc = attr_mergeit_one( e, ad, (Slapi_Value *)value );
694 if ( rc != LDAP_SUCCESS ) {
701 #endif /* LDAP_SLAPI */
705 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
710 val.bv_val = (char *)value;
711 val.bv_len = strlen( value );
713 return slapi_entry_add_value( e, type, &val );
716 #endif /* LDAP_SLAPI */
720 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
723 Slapi_Value *vals[2];
726 val.bv_val = (char *)value;
727 val.bv_len = strlen( value );
731 return slapi_entry_delete_values_sv( e, type, vals );
734 #endif /* LDAP_SLAPI */
739 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
742 return slapi_entry_attr_merge( e, (char *)type, vals );
749 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
758 return ( *attr != NULL ) ? 0 : -1;
765 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
772 if ( prevattr == NULL ) {
776 *attr = prevattr->a_next;
778 return ( *attr != NULL ) ? 0 : -1;
785 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
788 AttributeDescription *ad = NULL;
793 rc = slap_str2ad( type, &ad, &text );
794 if ( rc != LDAP_SUCCESS ) {
798 attr_delete( &e->e_attrs, ad );
800 rc = bvptr2obj( vals, &bv );
801 if ( rc != LDAP_SUCCESS ) {
805 rc = attr_mergeit( e, ad, bv );
806 slapi_ch_free( (void **)&bv );
807 if ( rc != LDAP_SUCCESS ) {
814 #endif /* LDAP_SLAPI */
818 * FIXME -- The caller must free the allocated memory.
819 * In Netscape they do not have to.
822 slapi_attr_get_values(
824 struct berval ***vals )
830 if ( attr == NULL ) {
834 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
838 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
839 for ( j = 0; j < i; j++ ) {
840 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
844 *vals = (struct berval **)bv;
847 #else /* LDAP_SLAPI */
849 #endif /* LDAP_SLAPI */
853 slapi_dn_normalize( char *dn )
859 assert( dn != NULL );
862 bdn.bv_len = strlen( dn );
864 dnNormalize2( NULL, &bdn, &ndn );
867 * FIXME: ain't it safe to set dn = ndn.bv_val ?
869 dn = ch_strdup( ndn.bv_val );
870 ch_free( ndn.bv_val );
873 #else /* LDAP_SLAPI */
875 #endif /* LDAP_SLAPI */
879 * FIXME: this function is dangerous and should be deprecated;
880 * DN normalization is a lot more than lower-casing, and BTW
881 * OpenLDAP's DN normalization for case insensitive attributes
882 * is already lower case
885 slapi_dn_normalize_case( char *dn )
888 slapi_dn_normalize( dn );
889 ldap_pvt_str2lower( dn );
892 #else /* LDAP_SLAPI */
894 #endif /* LDAP_SLAPI */
903 struct berval bdn, ndn;
904 struct berval bsuffix, nsuffix;
906 assert( dn != NULL );
907 assert( suffix != NULL );
910 bdn.bv_len = strlen( dn );
912 bsuffix.bv_val = suffix;
913 bsuffix.bv_len = strlen( suffix );
915 dnNormalize2( NULL, &bdn, &ndn );
916 dnNormalize2( NULL, &bsuffix, &nsuffix );
918 return dnIsSuffix( &ndn, &nsuffix );
919 #else /* LDAP_SLAPI */
921 #endif /* LDAP_SLAPI */
925 slapi_dn_ignore_case( char *dn )
928 return slapi_dn_normalize_case( dn );
929 #else /* LDAP_SLAPI */
931 #endif /* LDAP_SLAPI */
935 slapi_ch_malloc( unsigned long size )
938 return ch_malloc( size );
939 #else /* LDAP_SLAPI */
941 #endif /* LDAP_SLAPI */
945 slapi_ch_free( void **ptr )
950 #endif /* LDAP_SLAPI */
954 slapi_ch_free_string( char **ptr )
957 slapi_ch_free( (void **)ptr );
958 #endif /* LDAP_SLAPI */
962 slapi_ch_array_free( char **arrayp )
967 if ( arrayp != NULL ) {
968 for ( p = arrayp; *p != NULL; p++ ) {
969 slapi_ch_free( (void **)p );
971 slapi_ch_free( (void **)&arrayp );
977 slapi_ch_bvdup(const struct berval *v)
982 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
983 bv->bv_len = v->bv_len;
984 bv->bv_val = slapi_ch_malloc( bv->bv_len );
985 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
994 slapi_ch_bvecdup(const struct berval **v)
1004 for ( i = 0; v[i] != NULL; i++ )
1007 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1009 for ( i = 0; v[i] != NULL; i++ ) {
1010 rv[i] = slapi_ch_bvdup( v[i] );
1022 unsigned long nelem,
1023 unsigned long size )
1026 return ch_calloc( nelem, size );
1027 #else /* LDAP_SLAPI */
1029 #endif /* LDAP_SLAPI */
1035 unsigned long size )
1038 return ch_realloc( block, size );
1039 #else /* LDAP_SLAPI */
1041 #endif /* LDAP_SLAPI */
1045 slapi_ch_strdup( char *s )
1048 return ch_strdup( (const char *)s );
1049 #else /* LDAP_SLAPI */
1051 #endif /* LDAP_SLAPI */
1055 slapi_ch_stlen( char *s )
1058 return strlen( (const char *)s );
1059 #else /* LDAP_SLAPI */
1061 #endif /* LDAP_SLAPI */
1065 slapi_control_present(
1066 LDAPControl **controls,
1068 struct berval **val,
1083 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1084 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1089 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1091 * FIXME: according to 6.1 specification,
1092 * "The val output parameter is set
1093 * to point into the controls array.
1094 * A copy of the control value is
1098 struct berval *pTmpBval;
1100 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
1101 if ( pTmpBval == NULL ) {
1104 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
1105 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
1109 slapi_ch_free( (void **)&pTmpBval );
1115 *val = &controls[i]->ldctl_value;
1120 *iscritical = controls[i]->ldctl_iscritical;
1127 #else /* LDAP_SLAPI */
1129 #endif /* LDAP_SLAPI */
1134 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1135 unsigned long *slapi_mask)
1137 *slapi_mask = SLAPI_OPERATION_NONE;
1139 if ( slap_mask & SLAP_CTRL_ABANDON )
1140 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1142 if ( slap_mask & SLAP_CTRL_ADD )
1143 *slapi_mask |= SLAPI_OPERATION_ADD;
1145 if ( slap_mask & SLAP_CTRL_BIND )
1146 *slapi_mask |= SLAPI_OPERATION_BIND;
1148 if ( slap_mask & SLAP_CTRL_COMPARE )
1149 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1151 if ( slap_mask & SLAP_CTRL_DELETE )
1152 *slapi_mask |= SLAPI_OPERATION_DELETE;
1154 if ( slap_mask & SLAP_CTRL_MODIFY )
1155 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1157 if ( slap_mask & SLAP_CTRL_RENAME )
1158 *slapi_mask |= SLAPI_OPERATION_MODDN;
1160 if ( slap_mask & SLAP_CTRL_SEARCH )
1161 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1163 if ( slap_mask & SLAP_CTRL_UNBIND )
1164 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1168 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1169 slap_mask_t *slap_mask)
1173 if ( slapi_mask & SLAPI_OPERATION_BIND )
1174 *slap_mask |= SLAP_CTRL_BIND;
1176 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1177 *slap_mask |= SLAP_CTRL_UNBIND;
1179 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1180 *slap_mask |= SLAP_CTRL_SEARCH;
1182 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1183 *slap_mask |= SLAP_CTRL_MODIFY;
1185 if ( slapi_mask & SLAPI_OPERATION_ADD )
1186 *slap_mask |= SLAP_CTRL_ADD;
1188 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1189 *slap_mask |= SLAP_CTRL_DELETE;
1191 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1192 *slap_mask |= SLAP_CTRL_RENAME;
1194 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1195 *slap_mask |= SLAP_CTRL_COMPARE;
1197 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1198 *slap_mask |= SLAP_CTRL_ABANDON;
1200 *slap_mask |= SLAP_CTRL_FRONTEND;
1210 /* Plugins must deal with controls themselves. */
1212 return LDAP_SUCCESS;
1214 #endif /* LDAP_SLAPI */
1217 slapi_register_supported_control(
1219 unsigned long controlops )
1222 slap_mask_t controlmask;
1224 slapiControlOp2SlapControlMask( controlops, &controlmask );
1226 register_supported_control( controloid, controlmask, NULL, parseSlapiControl );
1227 #endif /* LDAP_SLAPI */
1231 slapi_get_supported_controls(
1233 unsigned long **ctrlopsp )
1238 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1239 if ( rc != LDAP_SUCCESS ) {
1243 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1244 /* In place, naughty. */
1245 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1248 return LDAP_SUCCESS;
1249 #else /* LDAP_SLAPI */
1251 #endif /* LDAP_SLAPI */
1255 slapi_dup_control( LDAPControl *ctrl )
1260 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1261 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1262 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1263 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1268 #endif /* LDAP_SLAPI */
1272 slapi_register_supported_saslmechanism( char *mechanism )
1275 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1276 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
1277 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1278 #endif /* LDAP_SLAPI */
1282 slapi_get_supported_saslmechanisms( void )
1285 /* FIXME -- can not get the saslmechanism without a connection. */
1286 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
1287 "can not get the SASL mechanism list "
1288 "without a connection\n" );
1290 #else /* LDAP_SLAPI */
1292 #endif /* LDAP_SLAPI */
1296 slapi_get_supported_extended_ops( void )
1300 char **ppExtOpOID = NULL;
1303 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1307 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
1312 if ( numExtOps == 0 ) {
1316 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1317 for ( k = 0; k < i; k++ ) {
1320 bv = get_supported_extop( k );
1321 assert( bv != NULL );
1323 ppExtOpOID[ k ] = bv->bv_val;
1326 for ( ; k < j; k++ ) {
1329 bv = ns_get_supported_extop( k );
1330 assert( bv != NULL );
1332 ppExtOpOID[ i + k ] = bv->bv_val;
1334 ppExtOpOID[ i + k ] = NULL;
1337 #else /* LDAP_SLAPI */
1339 #endif /* LDAP_SLAPI */
1343 slapi_send_ldap_result(
1349 struct berval **urls )
1355 char *extOID = NULL;
1356 struct berval *extValue = NULL;
1359 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1360 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1361 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1362 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, &s );
1363 rc = LDAP_SASL_BIND_IN_PROGRESS;
1364 send_ldap_sasl( conn, op, rc, NULL, NULL, NULL, NULL, s );
1368 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1369 if ( extOID != NULL ) {
1370 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &extValue );
1371 slapi_send_ldap_extended_response( conn, op, err, extOID,
1376 send_ldap_result( conn, op, err, matched, text, NULL, NULL );
1377 #endif /* LDAP_SLAPI */
1381 slapi_send_ldap_search_entry(
1384 LDAPControl **ectrls,
1395 AttributeName *an = NULL;
1398 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1403 an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
1404 for ( i = 0; attrs[i] != NULL; i++ ) {
1405 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1406 an[i].an_name.bv_len = strlen( attrs[i] );
1407 an[i].an_desc = NULL;
1408 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1411 an[i].an_name.bv_len = 0;
1412 an[i].an_name.bv_val = NULL;
1415 if ( ( rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) ||
1416 ( rc = slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&pConn) != 0 ) ||
1417 ( rc = slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp) != 0 ) ) {
1420 rc = send_search_entry( be, pConn, pOp, e, an, attrsonly, NULL );
1425 #else /* LDAP_SLAPI */
1427 #endif /* LDAP_SLAPI */
1432 slapi_str2filter( char *str )
1435 return str2filter( str );
1436 #else /* LDAP_SLAPI */
1438 #endif /* LDAP_SLAPI */
1448 #endif /* LDAP_SLAPI */
1452 slapi_filter_dup( Slapi_Filter *filter )
1457 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1459 f->f_choice = filter->f_choice;
1461 switch ( f->f_choice ) {
1462 case LDAP_FILTER_AND:
1463 case LDAP_FILTER_NOT:
1464 case LDAP_FILTER_OR: {
1465 Filter *pFilter, **ppF;
1467 for ( pFilter = filter->f_list, ppF = &f->f_list;
1469 pFilter = pFilter->f_next, ppF = &f->f_next )
1471 *ppF = slapi_filter_dup( pFilter );
1477 case LDAP_FILTER_PRESENT:
1478 f->f_desc = filter->f_desc;
1480 case LDAP_FILTER_EQUALITY:
1481 case LDAP_FILTER_GE:
1482 case LDAP_FILTER_LE:
1483 case LDAP_FILTER_APPROX:
1484 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1485 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1486 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1488 case LDAP_FILTER_EXT:
1489 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1490 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1491 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1492 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1493 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1494 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1495 case LDAP_FILTER_SUBSTRINGS: {
1498 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1499 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1500 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1502 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1503 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1504 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1506 f->f_sub_any[i].bv_val = NULL;
1507 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1510 case SLAPD_FILTER_COMPUTED:
1511 f->f_result = filter->f_result;
1514 slapi_ch_free( (void **)&f );
1522 #endif /* LDAP_SLAPI */
1526 slapi_filter_get_choice( Slapi_Filter *f )
1538 #else /* LDAP_SLAPI */
1539 return -1; /* invalid filter type */
1540 #endif /* LDAP_SLAPI */
1544 slapi_filter_get_ava(
1547 struct berval **bval )
1551 int rc = LDAP_SUCCESS;
1553 assert( type != NULL );
1554 assert( bval != NULL );
1559 ftype = f->f_choice;
1560 if ( ftype == LDAP_FILTER_EQUALITY
1561 || ftype == LDAP_FILTER_GE
1562 || ftype == LDAP_FILTER_LE
1563 || ftype == LDAP_FILTER_APPROX ) {
1565 * According to the SLAPI Reference Manual these are
1568 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1569 *bval = &f->f_un.f_un_ava->aa_value;
1570 } else { /* filter type not supported */
1575 #else /* LDAP_SLAPI */
1577 #endif /* LDAP_SLAPI */
1581 slapi_filter_list_first( Slapi_Filter *f )
1590 ftype = f->f_choice;
1591 if ( ftype == LDAP_FILTER_AND
1592 || ftype == LDAP_FILTER_OR
1593 || ftype == LDAP_FILTER_NOT ) {
1594 return (Slapi_Filter *)f->f_and;
1598 #else /* LDAP_SLAPI */
1600 #endif /* LDAP_SLAPI */
1604 slapi_filter_list_next(
1606 Slapi_Filter *fprev )
1615 ftype = f->f_choice;
1616 if ( ftype == LDAP_FILTER_AND
1617 || ftype == LDAP_FILTER_OR
1618 || ftype == LDAP_FILTER_NOT )
1620 return fprev->f_next;
1624 #else /* LDAP_SLAPI */
1626 #endif /* LDAP_SLAPI */
1630 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1637 switch ( f->f_choice ) {
1638 case LDAP_FILTER_GE:
1639 case LDAP_FILTER_LE:
1640 case LDAP_FILTER_EQUALITY:
1641 case LDAP_FILTER_APPROX:
1642 *type = f->f_av_desc->ad_cname.bv_val;
1644 case LDAP_FILTER_SUBSTRINGS:
1645 *type = f->f_sub_desc->ad_cname.bv_val;
1647 case LDAP_FILTER_PRESENT:
1648 *type = f->f_desc->ad_cname.bv_val;
1650 case LDAP_FILTER_EXT:
1651 *type = f->f_mr_desc->ad_cname.bv_val;
1654 /* Complex filters need not apply. */
1662 #endif /* LDAP_SLAPI */
1666 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1667 char ***any, char **final )
1672 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1677 * The caller shouldn't free but we can't return an
1678 * array of char *s from an array of bervals without
1679 * allocating memory, so we may as well be consistent.
1682 *type = f->f_sub_desc->ad_cname.bv_val;
1683 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1684 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1686 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1687 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1688 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1691 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1696 #endif /* LDAP_SLAPI */
1700 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2)
1703 Slapi_Filter *f = NULL;
1705 if ( ftype == LDAP_FILTER_AND ||
1706 ftype == LDAP_FILTER_OR ||
1707 ftype == LDAP_FILTER_NOT )
1709 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1710 f->f_choice = ftype;
1718 #endif /* LDAP_SLAPI */
1722 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1732 /* spec says return zero if no filter. */
1736 if ( verify_access ) {
1737 (void) slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be);
1738 rc = slapi_pblock_get(pb, SLAPI_CONNECTION, (void *)&conn);
1740 return LDAP_PARAM_ERROR;
1742 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
1744 return LDAP_PARAM_ERROR;
1751 * According to acl.c it is safe to call test_filter() with
1754 rc = test_filter( be, conn, op, e, f );
1756 case LDAP_COMPARE_TRUE:
1759 case LDAP_COMPARE_FALSE:
1761 case SLAPD_COMPARE_UNDEFINED:
1764 case LDAP_PROTOCOL_ERROR:
1765 /* filter type unknown: spec says return -1 */
1773 #endif /* LDAP_SLAPI */
1777 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
1780 return slapi_filter_test( NULL, e, f, 0 );
1787 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
1790 switch ( f->f_choice ) {
1791 case LDAP_FILTER_AND:
1792 case LDAP_FILTER_NOT:
1793 case LDAP_FILTER_OR: {
1797 * FIXME: altering f; should we use a temporary?
1799 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1800 rc = slapi_filter_apply( f, fn, arg, error_code );
1804 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
1810 case LDAP_FILTER_EQUALITY:
1811 case LDAP_FILTER_SUBSTRINGS:
1812 case LDAP_FILTER_GE:
1813 case LDAP_FILTER_LE:
1814 case LDAP_FILTER_PRESENT:
1815 case LDAP_FILTER_APPROX:
1816 case LDAP_FILTER_EXT:
1817 *error_code = fn( f, arg );
1820 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1823 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
1824 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
1830 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1832 #endif /* LDAP_SLAPI */
1836 slapi_send_ldap_extended_response(
1841 struct berval *response )
1844 send_ldap_extended( conn,op, errornum, NULL, NULL, NULL,
1845 respName,response, NULL );
1846 return LDAP_SUCCESS;
1847 #else /* LDAP_SLAPI */
1849 #endif /* LDAP_SLAPI */
1854 struct berval **vals,
1859 * FIXME: what's the point?
1862 #else /* LDAP_SLAPI */
1864 #endif /* LDAP_SLAPI */
1867 #define MAX_HOSTNAME 512
1870 slapi_get_hostname( void )
1876 * FIXME: I'd prefer a different check ...
1879 hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1881 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1882 "can't malloc memory for hostname\n" );
1885 } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
1886 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1887 "can't get hostname\n" );
1888 slapi_ch_free( (void **)&hn );
1892 static int been_here = 0;
1893 static char *static_hn = NULL;
1895 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1897 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1898 if ( static_hn == NULL) {
1899 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1900 "can't malloc memory for hostname\n" );
1902 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1907 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1908 slapi_log_error( SLAPI_LOG_FATAL,
1910 "can't get hostname\n" );
1911 slapi_ch_free( (void **)&static_hn );
1913 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1922 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1924 hn = ch_strdup( static_hn );
1925 #endif /* !_SPARC */
1928 #else /* LDAP_SLAPI */
1930 #endif /* LDAP_SLAPI */
1934 * FIXME: this should go in an appropriate header ...
1936 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
1946 int rc = LDAP_SUCCESS;
1949 va_start( arglist, fmt );
1950 rc = vLogError( severity, subsystem, fmt, arglist );
1954 #else /* LDAP_SLAPI */
1956 #endif /* LDAP_SLAPI */
1961 slapi_timer_current_time( void )
1964 static int first_time = 1;
1965 #if !defined (_WIN32)
1969 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1972 gettimeofday( &base_time, NULL );
1974 gettimeofday( &now, NULL );
1975 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1976 (now.tv_usec - base_time.tv_usec);
1977 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1983 return (slap_get_time() - starttime) * 1000000;
1990 performance_counter_present = QueryPerformanceCounter( &base_time );
1991 QueryPerformanceFrequency( &performance_freq );
1994 if ( !performance_counter_present )
1997 QueryPerformanceCounter( &now );
1998 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2000 #else /* LDAP_SLAPI */
2002 #endif /* LDAP_SLAPI */
2009 slapi_timer_get_time( char *label )
2012 unsigned long start = slapi_timer_current_time();
2013 printf("%10ld %10ld usec %s\n", start, 0, label);
2015 #else /* LDAP_SLAPI */
2017 #endif /* LDAP_SLAPI */
2024 slapi_timer_elapsed_time(
2026 unsigned long start )
2029 unsigned long stop = slapi_timer_current_time();
2030 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2031 #endif /* LDAP_SLAPI */
2035 slapi_free_search_results_internal( Slapi_PBlock *pb )
2038 Slapi_Entry **entries;
2039 int k = 0, nEnt = 0;
2041 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2042 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2047 if ( entries == NULL ) {
2051 for ( k = 0; k < nEnt; k++ ) {
2052 slapi_entry_free( entries[k] );
2055 slapi_ch_free( (void **)&entries );
2056 #endif /* LDAP_SLAPI */
2060 * Internal API to prime a Slapi_PBlock with a Backend.
2062 int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
2067 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2068 if ( rc != LDAP_SUCCESS )
2072 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2073 if ( rc != LDAP_SUCCESS )
2077 return LDAP_SUCCESS;
2080 #endif /* LDAP_SLAPI */
2085 * If oldStyle is TRUE, then a value suitable for setting to
2086 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
2087 * (pointer to static storage).
2089 * If oldStyle is FALSE, then a value suitable for setting to
2090 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2091 * a pointer to allocated memory and will include the SASL
2092 * mechanism (if any).
2094 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2099 switch ( authz->sai_method ) {
2100 case LDAP_AUTH_SASL:
2102 authType = SLAPD_AUTH_SASL;
2104 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2105 authType = slapi_ch_malloc( len );
2106 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2109 case LDAP_AUTH_SIMPLE:
2110 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2112 case LDAP_AUTH_NONE:
2113 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2119 if ( is_tls && authType == NULL ) {
2120 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2128 * Internal API to prime a Slapi_PBlock with a Connection.
2130 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
2136 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2137 if ( rc != LDAP_SUCCESS )
2140 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2141 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2142 if ( rc != LDAP_SUCCESS )
2144 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2145 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2146 if ( rc != LDAP_SUCCESS )
2150 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2151 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2152 if ( rc != LDAP_SUCCESS )
2154 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2155 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2156 if ( rc != LDAP_SUCCESS )
2160 #ifdef LDAP_CONNECTIONLESS
2161 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2162 if ( rc != LDAP_SUCCESS )
2166 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2167 if ( rc != LDAP_SUCCESS )
2170 /* Returns pointer to static string */
2171 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
2172 if ( connAuthType != NULL ) {
2173 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2174 if ( rc != LDAP_SUCCESS )
2178 /* Returns pointer to allocated string */
2179 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
2180 if ( connAuthType != NULL ) {
2181 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2182 if ( rc != LDAP_SUCCESS )
2186 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
2187 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
2188 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
2189 if ( rc != LDAP_SUCCESS )
2196 #endif /* LDAP_SLAPI */
2200 * Internal API to prime a Slapi_PBlock with an Operation.
2202 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
2211 if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2215 isRoot = be_isroot( be, &op->o_ndn );
2216 isUpdateDn = be_isupdate( be, &op->o_ndn );
2219 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2220 if ( rc != LDAP_SUCCESS )
2223 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2224 if ( rc != LDAP_SUCCESS )
2227 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2228 if ( rc != LDAP_SUCCESS )
2231 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2232 if ( rc != LDAP_SUCCESS )
2235 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2236 if ( rc != LDAP_SUCCESS )
2239 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2240 if ( rc != LDAP_SUCCESS )
2243 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2244 if ( rc != LDAP_SUCCESS)
2247 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2248 if ( rc != LDAP_SUCCESS )
2251 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2252 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
2253 /* Not quite sure what the point of this is. */
2254 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
2255 if ( rc != LDAP_SUCCESS )
2259 return LDAP_SUCCESS;
2265 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2270 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2271 *isSSL = conn->c_is_tls;
2273 return LDAP_SUCCESS;
2276 #endif /* LDAP_SLAPI */
2280 * DS 5.x compatability API follow
2283 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2289 return LDAP_PARAM_ERROR;
2291 at = attr->a_desc->ad_type;
2293 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2295 if ( is_at_single_value( at ) )
2296 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2297 if ( is_at_operational( at ) )
2298 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2299 if ( is_at_obsolete( at ) )
2300 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2301 if ( is_at_collective( at ) )
2302 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2303 if ( is_at_no_user_mod( at ) )
2304 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2306 return LDAP_SUCCESS;
2309 #endif /* LDAP_SLAPI */
2312 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2315 unsigned long flags;
2317 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2319 return (flags & flag) ? 1 : 0;
2322 #endif /* LDAP_SLAPI */
2325 Slapi_Attr *slapi_attr_new( void )
2330 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2338 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2342 AttributeDescription *ad = NULL;
2344 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2362 void slapi_attr_free( Slapi_Attr **a )
2370 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2373 return attr_dup( (Slapi_Attr *)attr );
2379 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2384 * FIXME: here we may lose alignment between a_vals/a_nvals
2387 return value_add_one( &a->a_vals, (Slapi_Value *)v );
2393 int slapi_attr_type2plugin( const char *type, void **pi )
2400 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2403 if ( attr == NULL ) {
2404 return LDAP_PARAM_ERROR;
2407 *type = attr->a_desc->ad_cname.bv_val;
2409 return LDAP_SUCCESS;
2415 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2418 if ( attr == NULL ) {
2419 return LDAP_PARAM_ERROR;
2421 *oidp = attr->a_desc->ad_type->sat_oid;
2423 return LDAP_SUCCESS;
2429 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2437 mr = a->a_desc->ad_type->sat_equality;
2438 rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
2439 (struct berval *)v1, (void *)v2, &text );
2440 if ( rc != LDAP_SUCCESS )
2443 return ( ret == 0 ) ? 0 : -1;
2449 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2459 mr = a->a_desc->ad_type->sat_equality;
2460 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2461 rc = value_match( &ret, a->a_desc, mr,
2462 SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
2463 if ( rc != LDAP_SUCCESS ) {
2474 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2477 AttributeDescription *a1 = NULL;
2478 AttributeDescription *a2 = NULL;
2482 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2486 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2490 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2491 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2492 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2495 case SLAPI_TYPE_CMP_EXACT:
2496 ret = ad_cmp( a1, a2 );
2498 case SLAPI_TYPE_CMP_BASE:
2499 ret = ad_base_cmp( a1, a2 );
2501 case SLAPI_TYPE_CMP_SUBTYPE:
2502 ret = is_ad_subtype( a2, a2 );
2515 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2518 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2524 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2527 return slapi_valueset_first_value( &a->a_vals, v );
2533 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2536 return slapi_valueset_next_value( &a->a_vals, hint, v );
2542 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2545 *numValues = slapi_valueset_count( &a->a_vals );
2553 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2556 *vs = &((Slapi_Attr *)a)->a_vals;
2564 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2567 return slapi_attr_get_values( a, vals );
2573 char *slapi_attr_syntax_normalize( const char *s )
2576 AttributeDescription *ad = NULL;
2579 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2583 return ad->ad_cname.bv_val;
2589 Slapi_Value *slapi_value_new( void )
2594 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2602 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2605 return ber_dupbv( NULL, (struct berval *)bval );
2611 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2614 return slapi_value_new_berval( v );
2620 Slapi_Value *slapi_value_new_string(const char *s)
2625 bv.bv_val = (char *)s;
2626 bv.bv_len = strlen( s );
2628 return slapi_value_new_berval( &bv );
2634 Slapi_Value *slapi_value_init(Slapi_Value *val)
2646 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2649 return ber_dupbv( v, bval );
2655 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2658 v->bv_val = slapi_ch_strdup( (char *)s );
2659 v->bv_len = strlen( s );
2667 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2670 return slapi_value_new_value( v );
2676 void slapi_value_free(Slapi_Value **value)
2679 if ( value == NULL ) {
2683 if ( (*value) != NULL ) {
2684 slapi_ch_free( (void **)&(*value)->bv_val );
2685 slapi_ch_free( (void **)value );
2690 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2699 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2702 if ( value == NULL ) {
2705 if ( value->bv_val != NULL ) {
2706 slapi_ch_free( (void **)&value->bv_val );
2708 slapi_value_init_berval( value, (struct berval *)bval );
2716 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2719 if ( value == NULL ) {
2722 return slapi_value_set_berval( value, vfrom );
2728 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2731 if ( value == NULL ) {
2734 if ( value->bv_val != NULL ) {
2735 slapi_ch_free( (void **)&value->bv_val );
2737 value->bv_val = slapi_ch_malloc( len );
2738 value->bv_len = len;
2739 AC_MEMCPY( value->bv_val, val, len );
2747 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2750 if ( value == NULL ) {
2753 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2760 int slapi_value_set_int(Slapi_Value *value, int intVal)
2765 snprintf( buf, sizeof( buf ), "%d", intVal );
2767 return slapi_value_set_string( value, buf );
2773 const char *slapi_value_get_string(const Slapi_Value *value)
2776 if ( value == NULL ) {
2779 return value->bv_val;
2786 static int checkBVString(const struct berval *bv)
2790 for ( i = 0; i < bv->bv_len; i++ ) {
2791 if ( bv->bv_val[i] == '\0' )
2794 if ( bv->bv_val[i] != '\0' )
2801 int slapi_value_get_int(const Slapi_Value *value)
2804 if ( value == NULL ) return 0;
2805 if ( value->bv_val == NULL ) return 0;
2806 if ( !checkBVString( value ) ) return 0;
2808 return (int)strtol( value->bv_val, NULL, 10 );
2814 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2817 if ( value == NULL ) return 0;
2818 if ( value->bv_val == NULL ) return 0;
2819 if ( !checkBVString( value ) ) return 0;
2821 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2827 long slapi_value_get_long(const Slapi_Value *value)
2830 if ( value == NULL ) return 0;
2831 if ( value->bv_val == NULL ) return 0;
2832 if ( !checkBVString( value ) ) return 0;
2834 return strtol( value->bv_val, NULL, 10 );
2840 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2843 if ( value == NULL ) return 0;
2844 if ( value->bv_val == NULL ) return 0;
2845 if ( !checkBVString( value ) ) return 0;
2847 return strtoul( value->bv_val, NULL, 10 );
2853 size_t slapi_value_get_length(const Slapi_Value *value)
2856 if ( value == NULL )
2859 return (size_t) value->bv_len;
2865 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2868 return slapi_attr_value_cmp( a, v1, v2 );
2874 /* A ValueSet is a container for a BerVarray. */
2875 Slapi_ValueSet *slapi_valueset_new( void )
2880 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2889 void slapi_valueset_free(Slapi_ValueSet *vs)
2895 ber_bvarray_free( vp );
2896 slapi_ch_free( (void **)&vp );
2903 void slapi_valueset_init(Slapi_ValueSet *vs)
2906 if ( vs != NULL && *vs == NULL ) {
2907 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2908 (*vs)->bv_val = NULL;
2914 void slapi_valueset_done(Slapi_ValueSet *vs)
2922 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
2924 slapi_ch_free( (void **)&vp->bv_val );
2926 /* but don't free *vs or vs */
2930 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2935 ber_dupbv( &bv, (Slapi_Value *)addval );
2936 ber_bvarray_add( vs, &bv );
2940 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2943 return slapi_valueset_next_value( vs, 0, v );
2949 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2960 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2971 int slapi_valueset_count( const Slapi_ValueSet *vs )
2982 for ( i = 0; vp[i].bv_val != NULL; i++ )
2992 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2997 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2998 slapi_valueset_add_value( vs1, vp );
3003 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
3004 struct berval *val, int access )
3011 slap_access_t slap_access;
3012 AttributeDescription *ad = NULL;
3015 ret = slap_str2ad( attr, &ad, &text );
3016 if ( ret != LDAP_SUCCESS ) {
3020 switch ( access & SLAPI_ACL_ALL ) {
3021 case SLAPI_ACL_COMPARE:
3022 slap_access = ACL_COMPARE;
3024 case SLAPI_ACL_SEARCH:
3025 slap_access = ACL_SEARCH;
3027 case SLAPI_ACL_READ:
3028 slap_access = ACL_READ;
3030 case SLAPI_ACL_WRITE:
3031 case SLAPI_ACL_DELETE:
3033 case SLAPI_ACL_SELF:
3034 slap_access = ACL_WRITE;
3037 return LDAP_INSUFFICIENT_ACCESS;
3041 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3042 return LDAP_PARAM_ERROR;
3045 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3046 return LDAP_PARAM_ERROR;
3049 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3050 return LDAP_PARAM_ERROR;
3053 ret = access_allowed( be, conn, op, e, desc, val, slap_access, NULL );
3055 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3057 return LDAP_UNWILLING_TO_PERFORM;
3061 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
3069 Modifications *next;
3071 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3072 return LDAP_PARAM_ERROR;
3075 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3076 return LDAP_PARAM_ERROR;
3079 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3080 return LDAP_PARAM_ERROR;
3083 ml = slapi_x_ldapmods2modifications( mods );
3088 ret = acl_check_modlist( be, conn, op, e, ml );
3090 /* Careful when freeing the modlist because it has pointers into the mods array. */
3091 for ( ; ml != NULL; ml = next ) {
3092 next = ml->sml_next;
3094 /* just free the containing array */
3095 slapi_ch_free( (void **)&ml->sml_bvalues );
3096 slapi_ch_free( (void **)&ml );
3099 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3101 return LDAP_UNWILLING_TO_PERFORM;
3106 * Synthesise an LDAPMod array from a Modifications list to pass
3107 * to SLAPI. This synthesis is destructive and as such the
3108 * Modifications list may not be used after calling this
3111 * This function must also be called before slap_mods_check().
3113 LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
3116 Modifications *ml, *modlist;
3117 LDAPMod **mods, *modp;
3120 modlist = *pmodlist;
3122 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3125 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3127 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3128 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3130 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3132 /* Take ownership of original type. */
3133 modp->mod_type = ml->sml_type.bv_val;
3134 ml->sml_type.bv_val = NULL;
3136 if ( ml->sml_bvalues != NULL ) {
3137 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
3139 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3140 sizeof(struct berval *) );
3141 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
3142 /* Take ownership of original values. */
3143 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3144 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
3145 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
3146 ml->sml_bvalues[j].bv_len = 0;
3147 ml->sml_bvalues[j].bv_val = NULL;
3149 modp->mod_bvalues[j] = NULL;
3151 modp->mod_bvalues = NULL;
3158 slap_mods_free( modlist );
3168 * Convert a potentially modified array of LDAPMods back to a
3169 * Modification list.
3171 * The returned Modification list contains pointers into the
3172 * LDAPMods array; the latter MUST be freed with
3173 * slapi_x_free_ldapmods() (see below).
3175 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
3178 Modifications *modlist, **modtail;
3183 for( modp = mods; *modp != NULL; modp++ ) {
3187 struct berval **bvp;
3189 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3190 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3191 mod->sml_type.bv_val = (*modp)->mod_type;
3192 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3193 mod->sml_desc = NULL;
3194 mod->sml_next = NULL;
3196 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3197 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3200 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3205 mod->sml_bvalues = NULL;
3207 mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3209 /* NB: This implicitly trusts a plugin to return valid modifications. */
3210 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3211 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3212 mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
3213 mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
3216 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3217 mod->sml_bvalues[i].bv_val = *p;
3218 mod->sml_bvalues[i].bv_len = strlen( *p );
3221 mod->sml_bvalues[i].bv_val = NULL;
3224 mod->sml_nvalues = NULL;
3228 modtail = &mod->sml_next;
3238 * This function only frees the parts of the mods array that
3239 * are not shared with the Modification list that was created
3240 * by slapi_x_ldapmods2modifications().
3243 void slapi_x_free_ldapmods (LDAPMod **mods)
3251 for ( i = 0; mods[i] != NULL; i++ ) {
3253 * Don't free values themselves; they're owned by the
3254 * Modification list. Do free the containing array.
3256 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3257 for ( j = 0; mods[i]->mod_bvalues != NULL && mods[i]->mod_bvalues[j] != NULL; j++ ) {
3258 ch_free( mods[i]->mod_bvalues[j] );
3260 ch_free( mods[i]->mod_bvalues );
3262 ch_free( mods[i]->mod_values );
3264 /* Don't free type, for same reasons. */
3268 #endif /* LDAP_SLAPI */
3272 * Sun ONE DS 5.x computed attribute support. Computed attributes
3273 * allow for dynamically generated operational attributes, a very
3274 * useful thing indeed.
3278 * Write the computed attribute to a BerElement. Complementary
3279 * functions need to be defined for anything that replaces
3280 * op->o_callback->sc_sendentry, if you wish to make computed
3281 * attributes available to it.
3283 int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
3287 Connection *conn = NULL;
3288 Operation *op = NULL;
3290 AttributeDescription *desc = NULL;
3306 rc = slapi_pblock_get( c->cac_pb, SLAPI_BACKEND, (void *)&be );
3308 be = NULL; /* no backend for root DSE */
3311 rc = slapi_pblock_get( c->cac_pb, SLAPI_CONNECTION, (void *)&conn );
3312 if ( rc != 0 || conn == NULL ) {
3316 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
3317 if ( rc != 0 || op == NULL ) {
3321 ber = (BerElement *)c->cac_private;
3324 if ( c->cac_attrs == NULL ) {
3325 /* All attrs request, skip operational attributes */
3326 if ( is_at_operational( desc->ad_type ) ) {
3330 /* Specific attrs requested */
3331 if ( is_at_operational( desc->ad_type ) ) {
3332 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3336 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3342 if ( !access_allowed( be, conn, op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
3343 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
3344 "acl: access to attribute %s not allowed\n",
3345 desc->ad_cname.bv_val );
3349 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
3351 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3352 "ber_printf failed\n");
3356 if ( !c->cac_attrsonly ) {
3357 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
3358 if ( !access_allowed( be, conn, op, e,
3359 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
3360 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
3361 "slapi_x_compute_output_ber: conn %lu "
3362 "acl: access to %s, value %d not allowed\n",
3363 op->o_connid, desc->ad_cname.bv_val, i );
3367 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
3368 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3369 "ber_printf failed\n");
3375 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
3376 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3377 "ber_printf failed\n" );
3388 * For some reason Sun don't use the normal plugin mechanism
3389 * registration path to register an "evaluator" function (an
3390 * "evaluator" is responsible for adding computed attributes;
3391 * the nomenclature is somewhat confusing).
3393 * As such slapi_compute_add_evaluator() registers the
3394 * function directly.
3396 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3399 Slapi_PBlock *pPlugin = NULL;
3402 pPlugin = slapi_pblock_new();
3403 if ( pPlugin == NULL ) {
3404 rc = LDAP_NO_MEMORY;
3408 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3409 if ( rc != LDAP_SUCCESS ) {
3413 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3414 if ( rc != LDAP_SUCCESS ) {
3418 rc = insertPlugin( NULL, pPlugin );
3425 if ( rc != LDAP_SUCCESS ) {
3426 if ( pPlugin != NULL ) {
3427 slapi_pblock_destroy( pPlugin );
3435 #endif /* LDAP_SLAPI */
3439 * See notes above regarding slapi_compute_add_evaluator().
3441 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3444 Slapi_PBlock *pPlugin = NULL;
3447 pPlugin = slapi_pblock_new();
3448 if ( pPlugin == NULL ) {
3449 rc = LDAP_NO_MEMORY;
3453 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3454 if ( rc != LDAP_SUCCESS ) {
3458 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3459 if ( rc != LDAP_SUCCESS ) {
3463 rc = insertPlugin( NULL, pPlugin );
3470 if ( rc != LDAP_SUCCESS ) {
3471 if ( pPlugin != NULL ) {
3472 slapi_pblock_destroy( pPlugin );
3480 #endif /* LDAP_SLAPI */
3484 * Call compute evaluators
3486 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3490 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3492 rc = getAllPluginFuncs( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3493 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3494 /* Nothing to do; front-end should ignore. */
3498 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3500 * -1: no attribute matched requested type
3501 * 0: one attribute matched
3502 * >0: error happened
3504 rc = (*pGetPlugin)( c, type, e, outputfn );
3510 slapi_ch_free( (void **)&tmpPlugin );
3515 #endif /* LDAP_SLAPI */
3518 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3524 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3529 return doPluginFNs( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3532 #endif /* LDAP_SLAPI */
3536 * New API to provide the plugin with access to the search
3537 * pblock. Have informed Sun DS team.
3539 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3545 if ( c->cac_pb == NULL )
3553 #endif /* LDAP_SLAPI */
3556 Slapi_Mutex *slapi_new_mutex( void )
3561 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3562 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3563 slapi_ch_free( (void **)&m );
3573 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3576 if ( mutex != NULL ) {
3577 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3578 slapi_ch_free( (void **)&mutex);
3583 void slapi_lock_mutex( Slapi_Mutex *mutex )
3586 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3590 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3593 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3599 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3604 if ( mutex == NULL ) {
3608 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3609 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3610 slapi_ch_free( (void **)&cv );
3614 /* XXX struct copy */
3615 cv->mutex = mutex->mutex;
3623 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3626 if ( cvar != NULL ) {
3627 ldap_pvt_thread_cond_destroy( &cvar->cond );
3628 slapi_ch_free( (void **)&cvar );
3633 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3636 if ( cvar == NULL ) {
3640 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3646 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3649 if ( cvar == NULL ) {
3654 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3657 return ldap_pvt_thread_cond_signal( &cvar->cond );