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, with or without
16 * modification, are permitted provided that this notice is preserved
17 * and that due credit is given to PADL Software Pty Ltd. This software
18 * is provided ``as is'' without express or implied warranty.
23 #include <ac/string.h>
24 #include <ac/stdarg.h>
26 #include <ac/unistd.h>
35 * server start time (should we use a struct timeval also in slapd?
37 static struct timeval base_time;
38 ldap_pvt_thread_mutex_t slapi_hn_mutex;
39 ldap_pvt_thread_mutex_t slapi_time_mutex;
42 ldap_pvt_thread_mutex_t mutex;
45 struct slapi_condvar {
46 ldap_pvt_thread_cond_t cond;
47 ldap_pvt_thread_mutex_t mutex;
51 * This function converts an array of pointers to berval objects to
52 * an array of berval objects.
57 struct berval **bvptr,
60 int rc = LDAP_SUCCESS;
64 if ( bvptr == NULL || *bvptr == NULL ) {
68 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
72 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
73 if ( tmpberval == NULL ) {
74 return LDAP_NO_MEMORY;
77 for ( i = 0; bvptr[i] != NULL; i++ ) {
78 tmpberval[i].bv_val = bvptr[i]->bv_val;
79 tmpberval[i].bv_len = bvptr[i]->bv_len;
81 tmpberval[i].bv_val = NULL;
82 tmpberval[i].bv_len = 0;
84 if ( rc == LDAP_SUCCESS ) {
97 Slapi_Entry *e = NULL;
100 pTmpS = slapi_ch_strdup( s );
101 if ( pTmpS != NULL ) {
102 e = str2entry( pTmpS );
103 slapi_ch_free( (void **)&pTmpS );
109 #endif /* LDAP_SLAPI */
120 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
121 ret = entry2str( e, len );
122 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
125 #else /* LDAP_SLAPI */
127 #endif /* LDAP_SLAPI */
131 slapi_entry_get_dn( Slapi_Entry *e )
134 return e->e_name.bv_val;
135 #else /* LDAP_SLAPI */
137 #endif /* LDAP_SLAPI */
141 slapi_x_entry_get_id( Slapi_Entry *e )
147 #endif /* LDAP_SLAPI */
156 struct berval dn = { 0, NULL };
159 dn.bv_len = strlen( ldn );
161 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname, NULL );
162 #endif /* LDAP_SLAPI */
166 slapi_entry_dup( Slapi_Entry *e )
171 ret = (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(*ret) );
174 ber_dupbv( &ret->e_name, &e->e_name );
175 ber_dupbv( &ret->e_nname, &e->e_nname );
176 ret->e_attrs = attrs_dup( e->e_attrs );
177 ret->e_ocflags = e->e_ocflags;
178 ber_dupbv( &ret->e_bv, &e->e_bv );
179 ret->e_private = NULL;
180 #else /* LDAP_SLAPI */
182 #endif /* LDAP_SLAPI */
186 slapi_entry_attr_delete(
191 AttributeDescription *ad = NULL;
194 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
195 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
198 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
199 return 0; /* attribute is deleted */
201 return -1; /* something went wrong */
203 #else /* LDAP_SLAPI */
205 #endif /* LDAP_SLAPI */
209 slapi_entry_alloc( void )
212 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
213 #else /* LDAP_SLAPI */
215 #endif /* LDAP_SLAPI */
219 slapi_entry_free( Slapi_Entry *e )
223 #endif /* LDAP_SLAPI */
227 slapi_entry_attr_merge(
230 struct berval **vals )
233 AttributeDescription *ad = NULL;
238 rc = bvptr2obj( vals, &bv );
239 if ( rc != LDAP_SUCCESS ) {
243 rc = slap_str2ad( type, &ad, &text );
244 if ( rc != LDAP_SUCCESS ) {
248 rc = attr_merge_normalize_one( e, ad, bv, NULL );
252 #else /* LDAP_SLAPI */
254 #endif /* LDAP_SLAPI */
258 slapi_entry_attr_find(
264 AttributeDescription *ad = NULL;
268 rc = slap_str2ad( type, &ad, &text );
269 if ( rc != LDAP_SUCCESS ) {
273 *attr = attr_find( e->e_attrs, ad );
274 if ( *attr == NULL ) {
279 #else /* LDAP_SLAPI */
281 #endif /* LDAP_SLAPI */
285 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
288 AttributeDescription *ad = NULL;
293 rc = slap_str2ad( type, &ad, &text );
294 if ( rc != LDAP_SUCCESS ) {
298 attr = attr_find( e->e_attrs, ad );
299 if ( attr == NULL ) {
303 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
304 return slapi_ch_strdup( attr->a_vals[0].bv_val );
314 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
317 AttributeDescription *ad = NULL;
322 rc = slap_str2ad( type, &ad, &text );
323 if ( rc != LDAP_SUCCESS ) {
327 attr = attr_find( e->e_attrs, ad );
328 if ( attr == NULL ) {
332 return slapi_value_get_int( attr->a_vals );
339 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
342 AttributeDescription *ad = NULL;
347 rc = slap_str2ad( type, &ad, &text );
348 if ( rc != LDAP_SUCCESS ) {
352 attr = attr_find( e->e_attrs, ad );
353 if ( attr == NULL ) {
357 return slapi_value_get_long( attr->a_vals );
364 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
367 AttributeDescription *ad = NULL;
372 rc = slap_str2ad( type, &ad, &text );
373 if ( rc != LDAP_SUCCESS ) {
377 attr = attr_find( e->e_attrs, ad );
378 if ( attr == NULL ) {
382 return slapi_value_get_uint( attr->a_vals );
389 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
392 AttributeDescription *ad = NULL;
397 rc = slap_str2ad( type, &ad, &text );
398 if ( rc != LDAP_SUCCESS ) {
402 attr = attr_find( e->e_attrs, ad );
403 if ( attr == NULL ) {
407 return slapi_value_get_ulong( attr->a_vals );
414 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
418 AttributeDescription *ad = NULL;
423 rc = slap_str2ad( type, &ad, &text );
424 if ( rc != LDAP_SUCCESS ) {
428 attr = attr_find( e->e_attrs, ad );
429 if ( attr == NULL ) {
433 bv.bv_val = (char *)value;
434 bv.bv_len = strlen( value );
436 return ( slapi_attr_value_find( attr, &bv ) != -1 );
443 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
446 AttributeDescription *ad = NULL;
451 rc = slap_str2ad( type, &ad, &text );
452 if ( rc != LDAP_SUCCESS ) {
456 attr_delete ( &e->e_attrs, ad );
457 if ( value != NULL ) {
458 bv.bv_val = (char *)value;
459 bv.bv_len = strlen(value);
460 attr_merge_normalize_one( e, ad, &bv, NULL );
462 #endif /* LDAP_SLAPI */
466 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
471 snprintf( buf, sizeof( buf ), "%d", l );
472 slapi_entry_attr_set_charptr( e, type, buf );
473 #endif /* LDAP_SLAPI */
477 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
482 snprintf( buf, sizeof( buf ), "%u", l );
483 slapi_entry_attr_set_charptr( e, type, buf );
484 #endif /* LDAP_SLAPI */
488 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
493 snprintf( buf, sizeof( buf ), "%ld", l );
494 slapi_entry_attr_set_charptr( e, type, buf );
495 #endif /* LDAP_SLAPI */
499 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
504 snprintf( buf, sizeof( buf ), "%lu", l );
505 slapi_entry_attr_set_charptr( e, type, buf );
506 #endif /* LDAP_SLAPI */
510 slapi_is_rootdse( const char *dn )
513 return ( dn == NULL || dn[0] == '\0' );
520 * Add values to entry.
523 * LDAP_SUCCESS Values added to entry
524 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
525 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
528 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
534 char textbuf[SLAP_TEXT_BUFLEN];
536 mod.sm_op = LDAP_MOD_ADD;
538 mod.sm_type.bv_val = (char *)type;
539 mod.sm_type.bv_len = strlen( type );
541 rc = slap_str2ad( type, &mod.sm_desc, &text );
542 if ( rc != LDAP_SUCCESS ) {
546 if ( vals == NULL ) {
547 /* Apparently vals can be NULL
548 * FIXME: sm_bvalues = NULL ? */
549 mod.sm_bvalues = (BerVarray)ch_malloc( sizeof(struct berval) );
550 mod.sm_bvalues->bv_val = NULL;
553 rc = bvptr2obj( vals, &mod.sm_bvalues );
554 if ( rc != LDAP_SUCCESS ) {
555 return LDAP_CONSTRAINT_VIOLATION;
558 mod.sm_nvalues = NULL;
560 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
562 ch_free( mod.sm_bvalues );
564 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
567 #endif /* LDAP_SLAPI */
571 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
574 return slapi_entry_add_values( e, type, vals );
577 #endif /* LDAP_SLAPI */
581 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
584 AttributeDescription *ad = NULL;
588 rc = slap_str2ad( type, &ad, &text );
589 if ( rc != LDAP_SUCCESS ) {
593 return attr_merge_normalize( e, ad, *vs, NULL );
596 #endif /* LDAP_SLAPI */
600 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
606 char textbuf[SLAP_TEXT_BUFLEN];
608 mod.sm_op = LDAP_MOD_DELETE;
610 mod.sm_type.bv_val = (char *)type;
611 mod.sm_type.bv_len = strlen( type );
613 if ( vals == NULL ) {
614 /* If vals is NULL, this is a NOOP. */
618 rc = slap_str2ad( type, &mod.sm_desc, &text );
619 if ( rc != LDAP_SUCCESS ) {
623 if ( vals[0] == NULL ) {
624 /* SLAPI doco says LDAP_OPERATIONS_ERROR but LDAP_OTHER is better */
625 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
628 rc = bvptr2obj( vals, &mod.sm_bvalues );
629 if ( rc != LDAP_SUCCESS ) {
630 return LDAP_CONSTRAINT_VIOLATION;
632 mod.sm_nvalues = NULL;
634 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
636 ch_free( mod.sm_bvalues );
641 #endif /* LDAP_SLAPI */
645 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
648 return slapi_entry_delete_values( e, type, vals );
651 #endif /* LDAP_SLAPI */
655 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
658 return slapi_entry_attr_merge( e, (char *)type, vals );
661 #endif /* LDAP_SLAPI */
665 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
668 AttributeDescription *ad = NULL;
672 rc = slap_str2ad( type, &ad, &text );
673 if ( rc != LDAP_SUCCESS ) {
677 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
678 if ( rc != LDAP_SUCCESS ) {
685 #endif /* LDAP_SLAPI */
689 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
694 val.bv_val = (char *)value;
695 val.bv_len = strlen( value );
697 return slapi_entry_add_value( e, type, &val );
700 #endif /* LDAP_SLAPI */
704 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
707 Slapi_Value *vals[2];
710 val.bv_val = (char *)value;
711 val.bv_len = strlen( value );
715 return slapi_entry_delete_values_sv( e, type, vals );
718 #endif /* LDAP_SLAPI */
723 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
726 return slapi_entry_attr_merge( e, (char *)type, vals );
733 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
742 return ( *attr != NULL ) ? 0 : -1;
749 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
756 if ( prevattr == NULL ) {
760 *attr = prevattr->a_next;
762 return ( *attr != NULL ) ? 0 : -1;
769 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
772 AttributeDescription *ad = NULL;
777 rc = slap_str2ad( type, &ad, &text );
778 if ( rc != LDAP_SUCCESS ) {
782 attr_delete( &e->e_attrs, ad );
784 rc = bvptr2obj( vals, &bv );
785 if ( rc != LDAP_SUCCESS ) {
789 rc = attr_merge_normalize( e, ad, bv, NULL );
790 slapi_ch_free( (void **)&bv );
791 if ( rc != LDAP_SUCCESS ) {
798 #endif /* LDAP_SLAPI */
802 * FIXME -- The caller must free the allocated memory.
803 * In Netscape they do not have to.
806 slapi_attr_get_values(
808 struct berval ***vals )
814 if ( attr == NULL ) {
818 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
822 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
823 for ( j = 0; j < i; j++ ) {
824 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
828 *vals = (struct berval **)bv;
831 #else /* LDAP_SLAPI */
833 #endif /* LDAP_SLAPI */
837 slapi_dn_normalize( char *dn )
843 assert( dn != NULL );
846 bdn.bv_len = strlen( dn );
848 if ( dnPretty( NULL, &bdn, &pdn, NULL ) != LDAP_SUCCESS ) {
853 #else /* LDAP_SLAPI */
855 #endif /* LDAP_SLAPI */
859 slapi_dn_normalize_case( char *dn )
865 assert( dn != NULL );
868 bdn.bv_len = strlen( dn );
870 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
875 #else /* LDAP_SLAPI */
877 #endif /* LDAP_SLAPI */
886 struct berval bdn, ndn;
887 struct berval bsuffix, nsuffix;
890 assert( dn != NULL );
891 assert( suffix != NULL );
894 bdn.bv_len = strlen( dn );
896 bsuffix.bv_val = suffix;
897 bsuffix.bv_len = strlen( suffix );
899 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
903 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
906 slapi_ch_free( (void **)&ndn.bv_val );
910 rc = dnIsSuffix( &ndn, &nsuffix );
912 slapi_ch_free( (void **)&ndn.bv_val );
913 slapi_ch_free( (void **)&nsuffix.bv_val );
916 #else /* LDAP_SLAPI */
918 #endif /* LDAP_SLAPI */
923 const char *parentdn,
924 const char *childdn )
927 struct berval assertedParentDN, normalizedAssertedParentDN;
928 struct berval childDN, normalizedChildDN;
929 struct berval normalizedParentDN;
932 assert( parentdn != NULL );
933 assert( childdn != NULL );
935 assertedParentDN.bv_val = (char *)parentdn;
936 assertedParentDN.bv_len = strlen( parentdn );
938 if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
939 &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
944 childDN.bv_val = (char *)childdn;
945 childDN.bv_len = strlen( childdn );
947 if ( dnNormalize( 0, NULL, NULL, &childDN,
948 &normalizedChildDN, NULL ) != LDAP_SUCCESS )
950 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
954 dnParent( &normalizedChildDN, &normalizedParentDN );
956 if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
957 &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
962 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
963 slapi_ch_free( (void **)&normalizedChildDN.bv_val );
965 return ( match == 0 );
968 #endif /* LDAP_SLAPI */
972 * Returns DN of the parent entry, or NULL if the DN is
973 * an empty string or NULL, or has no parent.
976 slapi_dn_parent( const char *_dn )
979 struct berval dn, prettyDN;
980 struct berval parentDN;
986 dn.bv_val = (char *)_dn;
987 dn.bv_len = strlen( _dn );
989 if ( dn.bv_len == 0 ) {
993 if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
997 dnParent( &prettyDN, &parentDN ); /* in-place */
999 slapi_ch_free( (void **)&prettyDN.bv_val );
1001 if ( parentDN.bv_len == 0 ) {
1005 return slapi_ch_strdup( parentDN.bv_val );
1008 #endif /* LDAP_SLAPI */
1012 * Returns DN of the parent entry; or NULL if the DN is
1013 * an empty string, if the DN has no parent, or if the
1014 * DN is the suffix of the backend database
1016 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *_dn )
1020 struct berval dn, prettyDN;
1021 struct berval normalizedDN, parentDN;
1023 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
1026 dn.bv_val = (char *)_dn;
1027 dn.bv_len = strlen( _dn );
1029 if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
1033 if ( be != NULL && be_issuffix( be, &normalizedDN ) ) {
1034 slapi_ch_free( (void **)&prettyDN.bv_val );
1035 slapi_ch_free( (void **)&normalizedDN.bv_val );
1039 dnParent( &prettyDN, &parentDN );
1041 slapi_ch_free( (void **)&prettyDN.bv_val );
1042 slapi_ch_free( (void **)&normalizedDN.bv_val );
1044 if ( parentDN.bv_len == 0 ) {
1048 return slapi_ch_strdup( parentDN.bv_val );
1051 #endif /* LDAP_SLAPI */
1055 slapi_dn_ignore_case( char *dn )
1058 return slapi_dn_normalize_case( dn );
1059 #else /* LDAP_SLAPI */
1061 #endif /* LDAP_SLAPI */
1065 slapi_ch_malloc( unsigned long size )
1068 return ch_malloc( size );
1069 #else /* LDAP_SLAPI */
1071 #endif /* LDAP_SLAPI */
1075 slapi_ch_free( void **ptr )
1080 #endif /* LDAP_SLAPI */
1084 slapi_ch_free_string( char **ptr )
1087 slapi_ch_free( (void **)ptr );
1088 #endif /* LDAP_SLAPI */
1092 slapi_ch_array_free( char **arrayp )
1097 if ( arrayp != NULL ) {
1098 for ( p = arrayp; *p != NULL; p++ ) {
1099 slapi_ch_free( (void **)p );
1101 slapi_ch_free( (void **)&arrayp );
1107 slapi_ch_bvdup(const struct berval *v)
1112 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
1113 bv->bv_len = v->bv_len;
1114 bv->bv_val = slapi_ch_malloc( bv->bv_len );
1115 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1124 slapi_ch_bvecdup(const struct berval **v)
1134 for ( i = 0; v[i] != NULL; i++ )
1137 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1139 for ( i = 0; v[i] != NULL; i++ ) {
1140 rv[i] = slapi_ch_bvdup( v[i] );
1152 unsigned long nelem,
1153 unsigned long size )
1156 return ch_calloc( nelem, size );
1157 #else /* LDAP_SLAPI */
1159 #endif /* LDAP_SLAPI */
1165 unsigned long size )
1168 return ch_realloc( block, size );
1169 #else /* LDAP_SLAPI */
1171 #endif /* LDAP_SLAPI */
1175 slapi_ch_strdup( char *s )
1178 return ch_strdup( (const char *)s );
1179 #else /* LDAP_SLAPI */
1181 #endif /* LDAP_SLAPI */
1185 slapi_ch_stlen( char *s )
1188 return strlen( (const char *)s );
1189 #else /* LDAP_SLAPI */
1191 #endif /* LDAP_SLAPI */
1195 slapi_control_present(
1196 LDAPControl **controls,
1198 struct berval **val,
1213 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1214 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1219 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1221 * FIXME: according to 6.1 specification,
1222 * "The val output parameter is set
1223 * to point into the controls array.
1224 * A copy of the control value is
1228 struct berval *pTmpBval;
1230 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
1231 if ( pTmpBval == NULL ) {
1234 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
1235 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
1239 slapi_ch_free( (void **)&pTmpBval );
1245 *val = &controls[i]->ldctl_value;
1250 *iscritical = controls[i]->ldctl_iscritical;
1257 #else /* LDAP_SLAPI */
1259 #endif /* LDAP_SLAPI */
1264 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1265 unsigned long *slapi_mask)
1267 *slapi_mask = SLAPI_OPERATION_NONE;
1269 if ( slap_mask & SLAP_CTRL_ABANDON )
1270 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1272 if ( slap_mask & SLAP_CTRL_ADD )
1273 *slapi_mask |= SLAPI_OPERATION_ADD;
1275 if ( slap_mask & SLAP_CTRL_BIND )
1276 *slapi_mask |= SLAPI_OPERATION_BIND;
1278 if ( slap_mask & SLAP_CTRL_COMPARE )
1279 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1281 if ( slap_mask & SLAP_CTRL_DELETE )
1282 *slapi_mask |= SLAPI_OPERATION_DELETE;
1284 if ( slap_mask & SLAP_CTRL_MODIFY )
1285 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1287 if ( slap_mask & SLAP_CTRL_RENAME )
1288 *slapi_mask |= SLAPI_OPERATION_MODDN;
1290 if ( slap_mask & SLAP_CTRL_SEARCH )
1291 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1293 if ( slap_mask & SLAP_CTRL_UNBIND )
1294 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1298 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1299 slap_mask_t *slap_mask)
1303 if ( slapi_mask & SLAPI_OPERATION_BIND )
1304 *slap_mask |= SLAP_CTRL_BIND;
1306 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1307 *slap_mask |= SLAP_CTRL_UNBIND;
1309 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1310 *slap_mask |= SLAP_CTRL_SEARCH;
1312 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1313 *slap_mask |= SLAP_CTRL_MODIFY;
1315 if ( slapi_mask & SLAPI_OPERATION_ADD )
1316 *slap_mask |= SLAP_CTRL_ADD;
1318 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1319 *slap_mask |= SLAP_CTRL_DELETE;
1321 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1322 *slap_mask |= SLAP_CTRL_RENAME;
1324 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1325 *slap_mask |= SLAP_CTRL_COMPARE;
1327 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1328 *slap_mask |= SLAP_CTRL_ABANDON;
1330 *slap_mask |= SLAP_CTRL_FRONTEND;
1339 /* Plugins must deal with controls themselves. */
1341 return LDAP_SUCCESS;
1343 #endif /* LDAP_SLAPI */
1346 slapi_register_supported_control(
1348 unsigned long controlops )
1351 slap_mask_t controlmask;
1353 slapiControlOp2SlapControlMask( controlops, &controlmask );
1355 register_supported_control( controloid, controlmask, NULL, parseSlapiControl );
1356 #endif /* LDAP_SLAPI */
1360 slapi_get_supported_controls(
1362 unsigned long **ctrlopsp )
1367 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1368 if ( rc != LDAP_SUCCESS ) {
1372 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1373 /* In place, naughty. */
1374 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1377 return LDAP_SUCCESS;
1378 #else /* LDAP_SLAPI */
1380 #endif /* LDAP_SLAPI */
1384 slapi_dup_control( LDAPControl *ctrl )
1389 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1390 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1391 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1392 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1397 #endif /* LDAP_SLAPI */
1401 slapi_register_supported_saslmechanism( char *mechanism )
1404 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1405 slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1406 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1407 #endif /* LDAP_SLAPI */
1411 slapi_get_supported_saslmechanisms( void )
1414 /* FIXME -- can not get the saslmechanism without a connection. */
1415 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1416 "can not get the SASL mechanism list "
1417 "without a connection\n" );
1419 #else /* LDAP_SLAPI */
1421 #endif /* LDAP_SLAPI */
1425 slapi_get_supported_extended_ops( void )
1429 char **ppExtOpOID = NULL;
1432 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1436 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
1441 if ( numExtOps == 0 ) {
1445 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1446 for ( k = 0; k < i; k++ ) {
1449 bv = get_supported_extop( k );
1450 assert( bv != NULL );
1452 ppExtOpOID[ k ] = bv->bv_val;
1455 for ( ; k < j; k++ ) {
1458 bv = ns_get_supported_extop( k );
1459 assert( bv != NULL );
1461 ppExtOpOID[ i + k ] = bv->bv_val;
1463 ppExtOpOID[ i + k ] = NULL;
1466 #else /* LDAP_SLAPI */
1468 #endif /* LDAP_SLAPI */
1472 slapi_send_ldap_result(
1478 struct berval **urls )
1483 char *extOID = NULL;
1484 struct berval *extValue = NULL;
1486 SlapReply rs = { REP_RESULT };
1488 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1491 rs.sr_matched = matched;
1496 slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
1498 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1499 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
1500 send_ldap_sasl( op, &rs );
1504 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1505 if ( extOID != NULL ) {
1506 rs.sr_rspoid = extOID;
1507 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
1508 send_ldap_extended( op, &rs );
1512 if (op->o_tag == LDAP_REQ_SEARCH)
1513 rs.sr_nentries = nentries;
1515 send_ldap_result( op, &rs );
1516 #endif /* LDAP_SLAPI */
1520 slapi_send_ldap_search_entry(
1523 LDAPControl **ectrls,
1529 SlapReply rs = { REP_RESULT };
1531 AttributeName *an = NULL;
1534 if ( attrs != NULL ) {
1535 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1543 an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
1544 for ( i = 0; attrs[i] != NULL; i++ ) {
1545 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1546 an[i].an_name.bv_len = strlen( attrs[i] );
1547 an[i].an_desc = NULL;
1548 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1551 an[i].an_name.bv_len = 0;
1552 an[i].an_name.bv_val = NULL;
1555 rs.sr_err = LDAP_SUCCESS;
1556 rs.sr_matched = NULL;
1559 rs.sr_ctrls = ectrls;
1564 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1568 return send_search_entry( pOp, &rs );
1569 #else /* LDAP_SLAPI */
1571 #endif /* LDAP_SLAPI */
1575 slapi_send_ldap_search_reference(
1578 struct berval **references,
1579 LDAPControl **ectrls,
1580 struct berval **v2refs
1585 SlapReply rs = { REP_SEARCHREF };
1588 rs.sr_err = LDAP_SUCCESS;
1589 rs.sr_matched = NULL;
1592 rc = bvptr2obj( references, &rs.sr_ref );
1593 if ( rc != LDAP_SUCCESS ) {
1597 rs.sr_ctrls = ectrls;
1601 if ( v2refs != NULL ) {
1602 rc = bvptr2obj( v2refs, &rs.sr_v2ref );
1603 if ( rc != LDAP_SUCCESS ) {
1604 slapi_ch_free( (void **)&rs.sr_ref );
1611 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1615 rc = send_search_reference( pOp, &rs );
1617 if ( rs.sr_ref != NULL )
1618 slapi_ch_free( (void **)&rs.sr_ref );
1620 if ( rs.sr_v2ref != NULL )
1621 slapi_ch_free( (void **)&rs.sr_v2ref );
1626 #endif /* LDAP_SLAPI */
1630 slapi_str2filter( char *str )
1633 return str2filter( str );
1634 #else /* LDAP_SLAPI */
1636 #endif /* LDAP_SLAPI */
1646 #endif /* LDAP_SLAPI */
1650 slapi_filter_dup( Slapi_Filter *filter )
1655 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1657 f->f_choice = filter->f_choice;
1659 switch ( f->f_choice ) {
1660 case LDAP_FILTER_AND:
1661 case LDAP_FILTER_NOT:
1662 case LDAP_FILTER_OR: {
1663 Filter *pFilter, **ppF;
1665 for ( pFilter = filter->f_list, ppF = &f->f_list;
1667 pFilter = pFilter->f_next, ppF = &f->f_next )
1669 *ppF = slapi_filter_dup( pFilter );
1675 case LDAP_FILTER_PRESENT:
1676 f->f_desc = filter->f_desc;
1678 case LDAP_FILTER_EQUALITY:
1679 case LDAP_FILTER_GE:
1680 case LDAP_FILTER_LE:
1681 case LDAP_FILTER_APPROX:
1682 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1683 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1684 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1686 case LDAP_FILTER_EXT:
1687 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1688 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1689 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1690 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1691 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1692 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1694 case LDAP_FILTER_SUBSTRINGS: {
1697 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1698 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1699 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1700 if ( filter->f_sub_any != NULL ) {
1701 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1703 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1704 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1705 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1707 f->f_sub_any[i].bv_val = NULL;
1709 f->f_sub_any = NULL;
1711 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1714 case SLAPD_FILTER_COMPUTED:
1715 f->f_result = filter->f_result;
1718 slapi_ch_free( (void **)&f );
1726 #endif /* LDAP_SLAPI */
1730 slapi_filter_get_choice( Slapi_Filter *f )
1742 #else /* LDAP_SLAPI */
1743 return -1; /* invalid filter type */
1744 #endif /* LDAP_SLAPI */
1748 slapi_filter_get_ava(
1751 struct berval **bval )
1755 int rc = LDAP_SUCCESS;
1757 assert( type != NULL );
1758 assert( bval != NULL );
1763 ftype = f->f_choice;
1764 if ( ftype == LDAP_FILTER_EQUALITY
1765 || ftype == LDAP_FILTER_GE
1766 || ftype == LDAP_FILTER_LE
1767 || ftype == LDAP_FILTER_APPROX ) {
1769 * According to the SLAPI Reference Manual these are
1772 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1773 *bval = &f->f_un.f_un_ava->aa_value;
1774 } else { /* filter type not supported */
1779 #else /* LDAP_SLAPI */
1781 #endif /* LDAP_SLAPI */
1785 slapi_filter_list_first( Slapi_Filter *f )
1794 ftype = f->f_choice;
1795 if ( ftype == LDAP_FILTER_AND
1796 || ftype == LDAP_FILTER_OR
1797 || ftype == LDAP_FILTER_NOT ) {
1798 return (Slapi_Filter *)f->f_list;
1802 #else /* LDAP_SLAPI */
1804 #endif /* LDAP_SLAPI */
1808 slapi_filter_list_next(
1810 Slapi_Filter *fprev )
1819 ftype = f->f_choice;
1820 if ( ftype == LDAP_FILTER_AND
1821 || ftype == LDAP_FILTER_OR
1822 || ftype == LDAP_FILTER_NOT )
1824 return fprev->f_next;
1828 #else /* LDAP_SLAPI */
1830 #endif /* LDAP_SLAPI */
1834 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1841 switch ( f->f_choice ) {
1842 case LDAP_FILTER_GE:
1843 case LDAP_FILTER_LE:
1844 case LDAP_FILTER_EQUALITY:
1845 case LDAP_FILTER_APPROX:
1846 *type = f->f_av_desc->ad_cname.bv_val;
1848 case LDAP_FILTER_SUBSTRINGS:
1849 *type = f->f_sub_desc->ad_cname.bv_val;
1851 case LDAP_FILTER_PRESENT:
1852 *type = f->f_desc->ad_cname.bv_val;
1854 case LDAP_FILTER_EXT:
1855 *type = f->f_mr_desc->ad_cname.bv_val;
1858 /* Complex filters need not apply. */
1866 #endif /* LDAP_SLAPI */
1870 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1871 char ***any, char **final )
1876 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1881 * The caller shouldn't free but we can't return an
1882 * array of char *s from an array of bervals without
1883 * allocating memory, so we may as well be consistent.
1886 *type = f->f_sub_desc->ad_cname.bv_val;
1887 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1888 if ( f->f_sub_any != NULL ) {
1889 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1891 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1892 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1893 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1899 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1904 #endif /* LDAP_SLAPI */
1908 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1911 Slapi_Filter *f = NULL;
1913 if ( ftype == LDAP_FILTER_AND ||
1914 ftype == LDAP_FILTER_OR ||
1915 ftype == LDAP_FILTER_NOT )
1917 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1918 f->f_choice = ftype;
1920 f->f_list->f_next = f2;
1927 #endif /* LDAP_SLAPI */
1931 slapi_x_filter_append( int ftype,
1932 Slapi_Filter **pContainingFilter, /* NULL on first call */
1933 Slapi_Filter **pNextFilter,
1934 Slapi_Filter *filterToAppend )
1937 if ( ftype == LDAP_FILTER_AND ||
1938 ftype == LDAP_FILTER_OR ||
1939 ftype == LDAP_FILTER_NOT )
1941 if ( *pContainingFilter == NULL ) {
1942 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
1943 (*pContainingFilter)->f_choice = ftype;
1944 (*pContainingFilter)->f_list = filterToAppend;
1945 (*pContainingFilter)->f_next = NULL;
1947 if ( (*pContainingFilter)->f_choice != ftype ) {
1951 (*pNextFilter)->f_next = filterToAppend;
1953 *pNextFilter = filterToAppend;
1957 #endif /* LDAP_SLAPI */
1962 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1970 /* spec says return zero if no filter. */
1974 if ( verify_access ) {
1975 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
1977 return LDAP_PARAM_ERROR;
1983 * According to acl.c it is safe to call test_filter() with
1986 rc = test_filter( op, e, f );
1988 case LDAP_COMPARE_TRUE:
1991 case LDAP_COMPARE_FALSE:
1993 case SLAPD_COMPARE_UNDEFINED:
1996 case LDAP_PROTOCOL_ERROR:
1997 /* filter type unknown: spec says return -1 */
2005 #endif /* LDAP_SLAPI */
2009 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
2012 return slapi_filter_test( NULL, e, f, 0 );
2019 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
2022 switch ( f->f_choice ) {
2023 case LDAP_FILTER_AND:
2024 case LDAP_FILTER_NOT:
2025 case LDAP_FILTER_OR: {
2029 * FIXME: altering f; should we use a temporary?
2031 for ( f = f->f_list; f != NULL; f = f->f_next ) {
2032 rc = slapi_filter_apply( f, fn, arg, error_code );
2036 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
2042 case LDAP_FILTER_EQUALITY:
2043 case LDAP_FILTER_SUBSTRINGS:
2044 case LDAP_FILTER_GE:
2045 case LDAP_FILTER_LE:
2046 case LDAP_FILTER_PRESENT:
2047 case LDAP_FILTER_APPROX:
2048 case LDAP_FILTER_EXT:
2049 *error_code = fn( f, arg );
2052 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2055 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
2056 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
2062 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2064 #endif /* LDAP_SLAPI */
2068 slapi_send_ldap_extended_response(
2073 struct berval *response )
2078 rs.sr_err = errornum;
2079 rs.sr_matched = NULL;
2083 rs.sr_rspoid = respName;
2084 rs.sr_rspdata = response;
2086 send_ldap_extended( op, &rs );
2088 return LDAP_SUCCESS;
2089 #else /* LDAP_SLAPI */
2091 #endif /* LDAP_SLAPI */
2096 struct berval **vals,
2101 * FIXME: what's the point?
2104 #else /* LDAP_SLAPI */
2106 #endif /* LDAP_SLAPI */
2109 #define MAX_HOSTNAME 512
2112 slapi_get_hostname( void )
2116 static int been_here = 0;
2117 static char *static_hn = NULL;
2119 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
2121 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
2122 if ( static_hn == NULL) {
2123 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
2124 "Cannot allocate memory for hostname\n" );
2126 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2131 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
2132 slapi_log_error( SLAPI_LOG_FATAL,
2134 "can't get hostname\n" );
2135 slapi_ch_free( (void **)&static_hn );
2137 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2146 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2148 hn = ch_strdup( static_hn );
2151 #else /* LDAP_SLAPI */
2153 #endif /* LDAP_SLAPI */
2157 * FIXME: this should go in an appropriate header ...
2159 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
2169 int rc = LDAP_SUCCESS;
2172 va_start( arglist, fmt );
2173 rc = vLogError( severity, subsystem, fmt, arglist );
2177 #else /* LDAP_SLAPI */
2179 #endif /* LDAP_SLAPI */
2184 slapi_timer_current_time( void )
2187 static int first_time = 1;
2188 #if !defined (_WIN32)
2192 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
2195 gettimeofday( &base_time, NULL );
2197 gettimeofday( &now, NULL );
2198 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
2199 (now.tv_usec - base_time.tv_usec);
2200 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
2206 return (slap_get_time() - starttime) * 1000000;
2213 performance_counter_present = QueryPerformanceCounter( &base_time );
2214 QueryPerformanceFrequency( &performance_freq );
2217 if ( !performance_counter_present )
2220 QueryPerformanceCounter( &now );
2221 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2223 #else /* LDAP_SLAPI */
2225 #endif /* LDAP_SLAPI */
2232 slapi_timer_get_time( char *label )
2235 unsigned long start = slapi_timer_current_time();
2236 printf("%10ld %10ld usec %s\n", start, 0, label);
2238 #else /* LDAP_SLAPI */
2240 #endif /* LDAP_SLAPI */
2247 slapi_timer_elapsed_time(
2249 unsigned long start )
2252 unsigned long stop = slapi_timer_current_time();
2253 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2254 #endif /* LDAP_SLAPI */
2258 slapi_free_search_results_internal( Slapi_PBlock *pb )
2261 Slapi_Entry **entries;
2262 int k = 0, nEnt = 0;
2264 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2265 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2270 if ( entries == NULL ) {
2274 for ( k = 0; k < nEnt; k++ ) {
2275 slapi_entry_free( entries[k] );
2278 slapi_ch_free( (void **)&entries );
2279 #endif /* LDAP_SLAPI */
2284 * Internal API to prime a Slapi_PBlock with a Backend.
2286 static int initBackendPB( Slapi_PBlock *pb, Backend *be )
2290 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2291 if ( rc != LDAP_SUCCESS )
2295 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2296 if ( rc != LDAP_SUCCESS )
2300 return LDAP_SUCCESS;
2304 * If oldStyle is TRUE, then a value suitable for setting to
2305 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
2306 * (pointer to static storage).
2308 * If oldStyle is FALSE, then a value suitable for setting to
2309 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2310 * a pointer to allocated memory and will include the SASL
2311 * mechanism (if any).
2313 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2318 switch ( authz->sai_method ) {
2319 case LDAP_AUTH_SASL:
2321 authType = SLAPD_AUTH_SASL;
2323 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2324 authType = slapi_ch_malloc( len );
2325 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2328 case LDAP_AUTH_SIMPLE:
2329 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2331 case LDAP_AUTH_NONE:
2332 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2338 if ( is_tls && authType == NULL ) {
2339 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2346 * Internal API to prime a Slapi_PBlock with a Connection.
2348 static int initConnectionPB( Slapi_PBlock *pb, Connection *conn )
2353 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2354 if ( rc != LDAP_SUCCESS )
2357 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2358 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2359 if ( rc != LDAP_SUCCESS )
2361 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2362 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2363 if ( rc != LDAP_SUCCESS )
2367 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2368 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2369 if ( rc != LDAP_SUCCESS )
2371 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2372 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2373 if ( rc != LDAP_SUCCESS )
2377 #ifdef LDAP_CONNECTIONLESS
2378 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2379 if ( rc != LDAP_SUCCESS )
2383 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2384 if ( rc != LDAP_SUCCESS )
2387 /* Returns pointer to static string */
2388 connAuthType = Authorization2AuthType( &conn->c_authz,
2395 if ( connAuthType != NULL ) {
2396 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2397 if ( rc != LDAP_SUCCESS )
2401 /* Returns pointer to allocated string */
2402 connAuthType = Authorization2AuthType( &conn->c_authz,
2409 if ( connAuthType != NULL ) {
2410 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2411 /* slapi_pblock_set dups this itself */
2412 slapi_ch_free( (void **)&connAuthType );
2413 if ( rc != LDAP_SUCCESS )
2417 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
2418 /* slapi_pblock_set dups this itself */
2419 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)conn->c_authz.sai_dn.bv_val);
2420 if ( rc != LDAP_SUCCESS )
2424 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SSF, (void *)conn->c_ssf);
2425 if ( rc != LDAP_SUCCESS )
2428 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SASL_CONTEXT,
2429 ( conn->c_sasl_authctx != NULL ? conn->c_sasl_authctx :
2430 conn->c_sasl_sockctx ) );
2431 if ( rc != LDAP_SUCCESS )
2436 #endif /* LDAP_SLAPI */
2439 * Internal API to prime a Slapi_PBlock with an Operation.
2441 int slapi_x_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
2449 if ( op->o_bd != NULL ) {
2450 isRoot = be_isroot( op->o_bd, &op->o_ndn );
2451 isUpdateDn = be_isupdate( op->o_bd, &op->o_ndn );
2454 rc = initBackendPB( pb, op->o_bd );
2455 if ( rc != LDAP_SUCCESS )
2458 rc = initConnectionPB( pb, op->o_conn );
2459 if ( rc != LDAP_SUCCESS )
2462 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2463 if ( rc != LDAP_SUCCESS )
2466 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2467 if ( rc != LDAP_SUCCESS )
2470 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2471 if ( rc != LDAP_SUCCESS )
2474 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2475 if ( rc != LDAP_SUCCESS )
2478 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2479 if ( rc != LDAP_SUCCESS )
2482 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2483 if ( rc != LDAP_SUCCESS )
2486 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2487 if ( rc != LDAP_SUCCESS)
2490 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2491 if ( rc != LDAP_SUCCESS )
2494 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2495 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
2496 /* Not quite sure what the point of this is. */
2497 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
2498 if ( rc != LDAP_SUCCESS )
2502 return LDAP_SUCCESS;
2508 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2513 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2515 *isSSL = conn->c_is_tls;
2520 return LDAP_SUCCESS;
2523 #endif /* LDAP_SLAPI */
2527 * DS 5.x compatability API follow
2530 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2536 return LDAP_PARAM_ERROR;
2538 at = attr->a_desc->ad_type;
2540 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2542 if ( is_at_single_value( at ) )
2543 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2544 if ( is_at_operational( at ) )
2545 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2546 if ( is_at_obsolete( at ) )
2547 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2548 if ( is_at_collective( at ) )
2549 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2550 if ( is_at_no_user_mod( at ) )
2551 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2553 return LDAP_SUCCESS;
2556 #endif /* LDAP_SLAPI */
2559 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2562 unsigned long flags;
2564 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2566 return (flags & flag) ? 1 : 0;
2569 #endif /* LDAP_SLAPI */
2572 Slapi_Attr *slapi_attr_new( void )
2577 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2585 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2589 AttributeDescription *ad = NULL;
2591 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2607 void slapi_attr_free( Slapi_Attr **a )
2615 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2618 return attr_dup( (Slapi_Attr *)attr );
2624 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2628 * FIXME: here we may lose alignment between a_vals/a_nvals
2630 return value_add_one( &a->a_vals, (Slapi_Value *)v );
2636 int slapi_attr_type2plugin( const char *type, void **pi )
2643 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2646 if ( attr == NULL ) {
2647 return LDAP_PARAM_ERROR;
2650 *type = attr->a_desc->ad_cname.bv_val;
2652 return LDAP_SUCCESS;
2658 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2661 if ( attr == NULL ) {
2662 return LDAP_PARAM_ERROR;
2664 *oidp = attr->a_desc->ad_type->sat_oid;
2666 return LDAP_SUCCESS;
2672 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2680 mr = a->a_desc->ad_type->sat_equality;
2681 rc = value_match( &ret, a->a_desc, mr,
2682 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2683 (struct berval *)v1, (void *)v2, &text );
2684 if ( rc != LDAP_SUCCESS )
2687 return ( ret == 0 ) ? 0 : -1;
2693 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2703 if ( a ->a_vals == NULL ) {
2706 mr = a->a_desc->ad_type->sat_equality;
2707 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2708 rc = value_match( &ret, a->a_desc, mr,
2709 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2710 if ( rc != LDAP_SUCCESS ) {
2717 #endif /* LDAP_SLAPI */
2721 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2724 AttributeDescription *a1 = NULL;
2725 AttributeDescription *a2 = NULL;
2729 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2733 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2737 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2738 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2739 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2742 case SLAPI_TYPE_CMP_EXACT:
2743 ret = ad_cmp( a1, a2 );
2745 case SLAPI_TYPE_CMP_BASE:
2746 ret = ad_base_cmp( a1, a2 );
2748 case SLAPI_TYPE_CMP_SUBTYPE:
2749 ret = is_ad_subtype( a2, a2 );
2762 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2765 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2771 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2774 return slapi_valueset_first_value( &a->a_vals, v );
2780 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2783 return slapi_valueset_next_value( &a->a_vals, hint, v );
2789 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2792 *numValues = slapi_valueset_count( &a->a_vals );
2800 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2803 *vs = &((Slapi_Attr *)a)->a_vals;
2811 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2814 return slapi_attr_get_values( a, vals );
2820 char *slapi_attr_syntax_normalize( const char *s )
2823 AttributeDescription *ad = NULL;
2826 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2830 return ad->ad_cname.bv_val;
2836 Slapi_Value *slapi_value_new( void )
2841 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2849 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2852 return ber_dupbv( NULL, (struct berval *)bval );
2858 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2861 return slapi_value_new_berval( v );
2867 Slapi_Value *slapi_value_new_string(const char *s)
2872 bv.bv_val = (char *)s;
2873 bv.bv_len = strlen( s );
2875 return slapi_value_new_berval( &bv );
2881 Slapi_Value *slapi_value_init(Slapi_Value *val)
2893 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2896 return ber_dupbv( v, bval );
2902 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2905 v->bv_val = slapi_ch_strdup( (char *)s );
2906 v->bv_len = strlen( s );
2914 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2917 return slapi_value_new_value( v );
2923 void slapi_value_free(Slapi_Value **value)
2926 if ( value == NULL ) {
2930 if ( (*value) != NULL ) {
2931 slapi_ch_free( (void **)&(*value)->bv_val );
2932 slapi_ch_free( (void **)value );
2937 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2946 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2949 if ( value == NULL ) {
2952 if ( value->bv_val != NULL ) {
2953 slapi_ch_free( (void **)&value->bv_val );
2955 slapi_value_init_berval( value, (struct berval *)bval );
2963 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2966 if ( value == NULL ) {
2969 return slapi_value_set_berval( value, vfrom );
2975 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2978 if ( value == NULL ) {
2981 if ( value->bv_val != NULL ) {
2982 slapi_ch_free( (void **)&value->bv_val );
2984 value->bv_val = slapi_ch_malloc( len );
2985 value->bv_len = len;
2986 AC_MEMCPY( value->bv_val, val, len );
2994 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2997 if ( value == NULL ) {
3000 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
3007 int slapi_value_set_int(Slapi_Value *value, int intVal)
3012 snprintf( buf, sizeof( buf ), "%d", intVal );
3014 return slapi_value_set_string( value, buf );
3020 const char *slapi_value_get_string(const Slapi_Value *value)
3023 if ( value == NULL ) {
3026 return value->bv_val;
3033 static int checkBVString(const struct berval *bv)
3037 for ( i = 0; i < bv->bv_len; i++ ) {
3038 if ( bv->bv_val[i] == '\0' )
3041 if ( bv->bv_val[i] != '\0' )
3048 int slapi_value_get_int(const Slapi_Value *value)
3051 if ( value == NULL ) return 0;
3052 if ( value->bv_val == NULL ) return 0;
3053 if ( !checkBVString( value ) ) return 0;
3055 return (int)strtol( value->bv_val, NULL, 10 );
3061 unsigned int slapi_value_get_uint(const Slapi_Value *value)
3064 if ( value == NULL ) return 0;
3065 if ( value->bv_val == NULL ) return 0;
3066 if ( !checkBVString( value ) ) return 0;
3068 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
3074 long slapi_value_get_long(const Slapi_Value *value)
3077 if ( value == NULL ) return 0;
3078 if ( value->bv_val == NULL ) return 0;
3079 if ( !checkBVString( value ) ) return 0;
3081 return strtol( value->bv_val, NULL, 10 );
3087 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
3090 if ( value == NULL ) return 0;
3091 if ( value->bv_val == NULL ) return 0;
3092 if ( !checkBVString( value ) ) return 0;
3094 return strtoul( value->bv_val, NULL, 10 );
3100 size_t slapi_value_get_length(const Slapi_Value *value)
3103 if ( value == NULL )
3106 return (size_t) value->bv_len;
3112 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
3115 return slapi_attr_value_cmp( a, v1, v2 );
3121 /* A ValueSet is a container for a BerVarray. */
3122 Slapi_ValueSet *slapi_valueset_new( void )
3127 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
3136 void slapi_valueset_free(Slapi_ValueSet *vs)
3142 ber_bvarray_free( vp );
3143 slapi_ch_free( (void **)&vp );
3150 void slapi_valueset_init(Slapi_ValueSet *vs)
3153 if ( vs != NULL && *vs == NULL ) {
3154 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
3155 (*vs)->bv_val = NULL;
3161 void slapi_valueset_done(Slapi_ValueSet *vs)
3169 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
3171 slapi_ch_free( (void **)&vp->bv_val );
3173 /* but don't free *vs or vs */
3177 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
3182 ber_dupbv( &bv, (Slapi_Value *)addval );
3183 ber_bvarray_add( vs, &bv );
3187 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
3190 return slapi_valueset_next_value( vs, 0, v );
3196 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
3207 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
3218 int slapi_valueset_count( const Slapi_ValueSet *vs )
3229 for ( i = 0; vp[i].bv_val != NULL; i++ )
3239 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
3244 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
3245 slapi_valueset_add_value( vs1, vp );
3250 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
3251 struct berval *val, int access )
3258 slap_access_t slap_access;
3259 AttributeDescription *ad = NULL;
3262 ret = slap_str2ad( attr, &ad, &text );
3263 if ( ret != LDAP_SUCCESS ) {
3267 switch ( access & SLAPI_ACL_ALL ) {
3268 case SLAPI_ACL_COMPARE:
3269 slap_access = ACL_COMPARE;
3271 case SLAPI_ACL_SEARCH:
3272 slap_access = ACL_SEARCH;
3274 case SLAPI_ACL_READ:
3275 slap_access = ACL_READ;
3277 case SLAPI_ACL_WRITE:
3278 case SLAPI_ACL_DELETE:
3280 case SLAPI_ACL_SELF:
3281 slap_access = ACL_WRITE;
3284 return LDAP_INSUFFICIENT_ACCESS;
3288 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3289 return LDAP_PARAM_ERROR;
3292 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3293 return LDAP_PARAM_ERROR;
3296 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3297 return LDAP_PARAM_ERROR;
3300 ret = access_allowed( op, e, ad, val, slap_access, NULL );
3302 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3304 return LDAP_UNWILLING_TO_PERFORM;
3308 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
3312 int rc = LDAP_SUCCESS;
3313 Modifications *ml, *mp;
3315 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3316 return LDAP_PARAM_ERROR;
3319 ml = slapi_x_ldapmods2modifications( mods );
3324 for ( mp = ml; mp != NULL; mp = mp->sml_next ) {
3325 rc = slap_bv2ad( &mp->sml_type, &mp->sml_desc, (const char **)errbuf );
3326 if ( rc != LDAP_SUCCESS ) {
3331 if ( rc == LDAP_SUCCESS ) {
3332 rc = acl_check_modlist( op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3335 /* Careful when freeing the modlist because it has pointers into the mods array. */
3336 for ( ; ml != NULL; ml = mp ) {
3339 /* just free the containing array */
3340 slapi_ch_free( (void **)&ml->sml_bvalues );
3341 slapi_ch_free( (void **)&ml );
3346 return LDAP_UNWILLING_TO_PERFORM;
3351 * Synthesise an LDAPMod array from a Modifications list to pass
3352 * to SLAPI. This synthesis is destructive and as such the
3353 * Modifications list may not be used after calling this
3356 * This function must also be called before slap_mods_check().
3358 LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
3361 Modifications *ml, *modlist;
3362 LDAPMod **mods, *modp;
3365 modlist = *pmodlist;
3367 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3370 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3372 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3373 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3375 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3377 /* Take ownership of original type. */
3378 modp->mod_type = ml->sml_type.bv_val;
3379 ml->sml_type.bv_val = NULL;
3381 if ( ml->sml_bvalues != NULL ) {
3382 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
3384 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3385 sizeof(struct berval *) );
3386 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
3387 /* Take ownership of original values. */
3388 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3389 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
3390 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
3391 ml->sml_bvalues[j].bv_len = 0;
3392 ml->sml_bvalues[j].bv_val = NULL;
3394 modp->mod_bvalues[j] = NULL;
3396 modp->mod_bvalues = NULL;
3403 slap_mods_free( modlist );
3413 * Convert a potentially modified array of LDAPMods back to a
3414 * Modification list.
3416 * The returned Modification list contains pointers into the
3417 * LDAPMods array; the latter MUST be freed with
3418 * slapi_x_free_ldapmods() (see below).
3420 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
3423 Modifications *modlist = NULL, **modtail;
3428 for( modp = mods; *modp != NULL; modp++ ) {
3432 struct berval **bvp;
3434 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3435 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3436 mod->sml_type.bv_val = (*modp)->mod_type;
3437 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3438 mod->sml_desc = NULL;
3439 mod->sml_next = NULL;
3441 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3442 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3445 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3450 mod->sml_bvalues = NULL;
3452 mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3454 /* NB: This implicitly trusts a plugin to return valid modifications. */
3455 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3456 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3457 mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
3458 mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
3461 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3462 mod->sml_bvalues[i].bv_val = *p;
3463 mod->sml_bvalues[i].bv_len = strlen( *p );
3466 mod->sml_bvalues[i].bv_val = NULL;
3468 mod->sml_nvalues = NULL;
3471 modtail = &mod->sml_next;
3481 * This function only frees the parts of the mods array that
3482 * are not shared with the Modification list that was created
3483 * by slapi_x_ldapmods2modifications().
3486 void slapi_x_free_ldapmods (LDAPMod **mods)
3494 for ( i = 0; mods[i] != NULL; i++ ) {
3496 * Don't free values themselves; they're owned by the
3497 * Modification list. Do free the containing array.
3499 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3500 for ( j = 0; mods[i]->mod_bvalues != NULL && mods[i]->mod_bvalues[j] != NULL; j++ ) {
3501 ch_free( mods[i]->mod_bvalues[j] );
3503 ch_free( mods[i]->mod_bvalues );
3505 ch_free( mods[i]->mod_values );
3507 /* Don't free type, for same reasons. */
3511 #endif /* LDAP_SLAPI */
3515 * Sun ONE DS 5.x computed attribute support. Computed attributes
3516 * allow for dynamically generated operational attributes, a very
3517 * useful thing indeed.
3521 * Write the computed attribute to a BerElement. Complementary
3522 * functions need to be defined for anything that replaces
3523 * op->o_callback->sc_sendentry, if you wish to make computed
3524 * attributes available to it.
3526 int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
3529 Operation *op = NULL;
3531 AttributeDescription *desc = NULL;
3547 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
3548 if ( rc != 0 || op == NULL ) {
3552 ber = (BerElement *)c->cac_private;
3555 if ( c->cac_attrs == NULL ) {
3556 /* All attrs request, skip operational attributes */
3557 if ( is_at_operational( desc->ad_type ) ) {
3561 /* Specific attrs requested */
3562 if ( is_at_operational( desc->ad_type ) ) {
3563 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3567 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3573 if ( !access_allowed( op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
3574 slapi_log_error( SLAPI_LOG_ACL, "slapi_x_compute_output_ber",
3575 "acl: access to attribute %s not allowed\n",
3576 desc->ad_cname.bv_val );
3580 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
3582 slapi_log_error( SLAPI_LOG_BER, "slapi_x_compute_output_ber",
3583 "ber_printf failed\n");
3587 if ( !c->cac_attrsonly ) {
3588 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
3589 if ( !access_allowed( op, e,
3590 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
3591 slapi_log_error( SLAPI_LOG_ACL, "slapi_x_compute_output_ber",
3593 "acl: access to %s, value %d not allowed\n",
3594 op->o_connid, desc->ad_cname.bv_val, i );
3598 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
3599 slapi_log_error( SLAPI_LOG_BER, "slapi_x_compute_output_ber",
3600 "ber_printf failed\n");
3606 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
3607 slapi_log_error( SLAPI_LOG_BER, "slapi_x_compute_output_ber",
3608 "ber_printf failed\n" );
3619 * For some reason Sun don't use the normal plugin mechanism
3620 * registration path to register an "evaluator" function (an
3621 * "evaluator" is responsible for adding computed attributes;
3622 * the nomenclature is somewhat confusing).
3624 * As such slapi_compute_add_evaluator() registers the
3625 * function directly.
3627 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3630 Slapi_PBlock *pPlugin = NULL;
3633 pPlugin = slapi_pblock_new();
3634 if ( pPlugin == NULL ) {
3635 rc = LDAP_NO_MEMORY;
3639 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3640 if ( rc != LDAP_SUCCESS ) {
3644 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3645 if ( rc != LDAP_SUCCESS ) {
3649 rc = insertPlugin( NULL, pPlugin );
3656 if ( rc != LDAP_SUCCESS ) {
3657 if ( pPlugin != NULL ) {
3658 slapi_pblock_destroy( pPlugin );
3666 #endif /* LDAP_SLAPI */
3670 * See notes above regarding slapi_compute_add_evaluator().
3672 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3675 Slapi_PBlock *pPlugin = NULL;
3678 pPlugin = slapi_pblock_new();
3679 if ( pPlugin == NULL ) {
3680 rc = LDAP_NO_MEMORY;
3684 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3685 if ( rc != LDAP_SUCCESS ) {
3689 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3690 if ( rc != LDAP_SUCCESS ) {
3694 rc = insertPlugin( NULL, pPlugin );
3701 if ( rc != LDAP_SUCCESS ) {
3702 if ( pPlugin != NULL ) {
3703 slapi_pblock_destroy( pPlugin );
3711 #endif /* LDAP_SLAPI */
3715 * Call compute evaluators
3717 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3721 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3723 rc = getAllPluginFuncs( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3724 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3725 /* Nothing to do; front-end should ignore. */
3729 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3731 * -1: no attribute matched requested type
3732 * 0: one attribute matched
3733 * >0: error happened
3735 rc = (*pGetPlugin)( c, type, e, outputfn );
3741 slapi_ch_free( (void **)&tmpPlugin );
3746 #endif /* LDAP_SLAPI */
3749 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3755 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3760 return doPluginFNs( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3763 #endif /* LDAP_SLAPI */
3767 * New API to provide the plugin with access to the search
3768 * pblock. Have informed Sun DS team.
3770 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3776 if ( c->cac_pb == NULL )
3784 #endif /* LDAP_SLAPI */
3787 Slapi_Mutex *slapi_new_mutex( void )
3792 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3793 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3794 slapi_ch_free( (void **)&m );
3804 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3807 if ( mutex != NULL ) {
3808 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3809 slapi_ch_free( (void **)&mutex);
3814 void slapi_lock_mutex( Slapi_Mutex *mutex )
3817 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3821 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3824 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3830 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3835 if ( mutex == NULL ) {
3839 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3840 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3841 slapi_ch_free( (void **)&cv );
3845 /* XXX struct copy */
3846 cv->mutex = mutex->mutex;
3854 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3857 if ( cvar != NULL ) {
3858 ldap_pvt_thread_cond_destroy( &cvar->cond );
3859 slapi_ch_free( (void **)&cvar );
3864 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3867 if ( cvar == NULL ) {
3871 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3877 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3880 if ( cvar == NULL ) {
3885 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3888 return ldap_pvt_thread_cond_signal( &cvar->cond );
3894 int slapi_x_access_allowed( Operation *op,
3896 AttributeDescription *desc,
3898 slap_access_t access,
3899 AccessControlState *state )
3902 int rc, slap_access = 0;
3903 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3905 if ( op->o_pb == NULL ) {
3906 /* internal operation */
3912 slap_access |= SLAPI_ACL_ADD | SLAPI_ACL_DELETE | SLAPI_ACL_WRITE;
3915 slap_access |= SLAPI_ACL_READ;
3918 slap_access |= SLAPI_ACL_SEARCH;
3921 slap_access = ACL_COMPARE;
3927 rc = getAllPluginFuncs( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
3928 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3929 /* nothing to do; allowed access */
3933 slapi_x_pblock_set_operation( op->o_pb, op );
3935 rc = 1; /* default allow policy */
3937 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3942 rc = (*pGetPlugin)( op->o_pb, entry, desc->ad_cname.bv_val,
3943 val, slap_access, (void *)state );
3949 slapi_ch_free( (void **)&tmpPlugin );
3954 #endif /* LDAP_SLAPI */