2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2002-2004 The OpenLDAP Foundation.
5 * Portions Copyright 1997,2002-2003 IBM Corporation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by IBM Corporation for use in
18 * IBM products and subsequently ported to OpenLDAP Software by
19 * Steve Omrani. Additional significant contributors include:
25 #include <ac/string.h>
26 #include <ac/stdarg.h>
28 #include <ac/unistd.h>
37 * server start time (should we use a struct timeval also in slapd?
39 static struct timeval base_time;
40 ldap_pvt_thread_mutex_t slapi_hn_mutex;
41 ldap_pvt_thread_mutex_t slapi_time_mutex;
44 ldap_pvt_thread_mutex_t mutex;
47 struct slapi_condvar {
48 ldap_pvt_thread_cond_t cond;
49 ldap_pvt_thread_mutex_t mutex;
53 static int checkBVString(const struct berval *bv)
57 for ( i = 0; i < bv->bv_len; i++ ) {
58 if ( bv->bv_val[i] == '\0' )
61 if ( bv->bv_val[i] != '\0' )
66 #endif /* LDAP_SLAPI */
69 * This function converts an array of pointers to berval objects to
70 * an array of berval objects.
75 struct berval **bvptr,
78 int rc = LDAP_SUCCESS;
82 if ( bvptr == NULL || *bvptr == NULL ) {
86 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
90 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
91 if ( tmpberval == NULL ) {
92 return LDAP_NO_MEMORY;
95 for ( i = 0; bvptr[i] != NULL; i++ ) {
96 tmpberval[i].bv_val = bvptr[i]->bv_val;
97 tmpberval[i].bv_len = bvptr[i]->bv_len;
99 tmpberval[i].bv_val = NULL;
100 tmpberval[i].bv_len = 0;
102 if ( rc == LDAP_SUCCESS ) {
115 Slapi_Entry *e = NULL;
118 pTmpS = slapi_ch_strdup( s );
119 if ( pTmpS != NULL ) {
120 e = str2entry( pTmpS );
121 slapi_ch_free( (void **)&pTmpS );
127 #endif /* LDAP_SLAPI */
138 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
139 ret = entry2str( e, len );
140 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
143 #else /* LDAP_SLAPI */
145 #endif /* LDAP_SLAPI */
149 slapi_entry_get_dn( Slapi_Entry *e )
152 return e->e_name.bv_val;
153 #else /* LDAP_SLAPI */
155 #endif /* LDAP_SLAPI */
159 slapi_x_entry_get_id( Slapi_Entry *e )
165 #endif /* LDAP_SLAPI */
174 struct berval dn = { 0, NULL };
177 dn.bv_len = strlen( ldn );
179 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname, NULL );
180 #endif /* LDAP_SLAPI */
184 slapi_entry_dup( Slapi_Entry *e )
187 return entry_dup( e );
188 #else /* LDAP_SLAPI */
190 #endif /* LDAP_SLAPI */
194 slapi_entry_attr_delete(
199 AttributeDescription *ad = NULL;
202 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
203 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
206 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
207 return 0; /* attribute is deleted */
209 return -1; /* something went wrong */
211 #else /* LDAP_SLAPI */
213 #endif /* LDAP_SLAPI */
217 slapi_entry_alloc( void )
220 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
221 #else /* LDAP_SLAPI */
223 #endif /* LDAP_SLAPI */
227 slapi_entry_free( Slapi_Entry *e )
231 #endif /* LDAP_SLAPI */
235 slapi_entry_attr_merge(
238 struct berval **vals )
241 AttributeDescription *ad = NULL;
246 rc = bvptr2obj( vals, &bv );
247 if ( rc != LDAP_SUCCESS ) {
251 rc = slap_str2ad( type, &ad, &text );
252 if ( rc != LDAP_SUCCESS ) {
256 rc = attr_merge_normalize_one( e, ad, bv, NULL );
260 #else /* LDAP_SLAPI */
262 #endif /* LDAP_SLAPI */
266 slapi_entry_attr_find(
272 AttributeDescription *ad = NULL;
276 rc = slap_str2ad( type, &ad, &text );
277 if ( rc != LDAP_SUCCESS ) {
281 *attr = attr_find( e->e_attrs, ad );
282 if ( *attr == NULL ) {
287 #else /* LDAP_SLAPI */
289 #endif /* LDAP_SLAPI */
293 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
296 AttributeDescription *ad = NULL;
301 rc = slap_str2ad( type, &ad, &text );
302 if ( rc != LDAP_SUCCESS ) {
306 attr = attr_find( e->e_attrs, ad );
307 if ( attr == NULL ) {
311 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
314 p = slapi_value_get_string( &attr->a_vals[0] );
316 return slapi_ch_strdup( (char *)p );
327 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
330 AttributeDescription *ad = NULL;
335 rc = slap_str2ad( type, &ad, &text );
336 if ( rc != LDAP_SUCCESS ) {
340 attr = attr_find( e->e_attrs, ad );
341 if ( attr == NULL ) {
345 return slapi_value_get_int( attr->a_vals );
352 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
355 AttributeDescription *ad = NULL;
360 rc = slap_str2ad( type, &ad, &text );
361 if ( rc != LDAP_SUCCESS ) {
365 attr = attr_find( e->e_attrs, ad );
366 if ( attr == NULL ) {
370 return slapi_value_get_long( attr->a_vals );
377 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
380 AttributeDescription *ad = NULL;
385 rc = slap_str2ad( type, &ad, &text );
386 if ( rc != LDAP_SUCCESS ) {
390 attr = attr_find( e->e_attrs, ad );
391 if ( attr == NULL ) {
395 return slapi_value_get_uint( attr->a_vals );
402 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
405 AttributeDescription *ad = NULL;
410 rc = slap_str2ad( type, &ad, &text );
411 if ( rc != LDAP_SUCCESS ) {
415 attr = attr_find( e->e_attrs, ad );
416 if ( attr == NULL ) {
420 return slapi_value_get_ulong( attr->a_vals );
427 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
431 AttributeDescription *ad = NULL;
436 rc = slap_str2ad( type, &ad, &text );
437 if ( rc != LDAP_SUCCESS ) {
441 attr = attr_find( e->e_attrs, ad );
442 if ( attr == NULL ) {
446 bv.bv_val = (char *)value;
447 bv.bv_len = strlen( value );
449 return ( slapi_attr_value_find( attr, &bv ) != -1 );
456 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
459 AttributeDescription *ad = NULL;
464 rc = slap_str2ad( type, &ad, &text );
465 if ( rc != LDAP_SUCCESS ) {
469 attr_delete ( &e->e_attrs, ad );
470 if ( value != NULL ) {
471 bv.bv_val = (char *)value;
472 bv.bv_len = strlen(value);
473 attr_merge_normalize_one( e, ad, &bv, NULL );
475 #endif /* LDAP_SLAPI */
479 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
484 snprintf( buf, sizeof( buf ), "%d", l );
485 slapi_entry_attr_set_charptr( e, type, buf );
486 #endif /* LDAP_SLAPI */
490 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
495 snprintf( buf, sizeof( buf ), "%u", l );
496 slapi_entry_attr_set_charptr( e, type, buf );
497 #endif /* LDAP_SLAPI */
501 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
506 snprintf( buf, sizeof( buf ), "%ld", l );
507 slapi_entry_attr_set_charptr( e, type, buf );
508 #endif /* LDAP_SLAPI */
512 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
517 snprintf( buf, sizeof( buf ), "%lu", l );
518 slapi_entry_attr_set_charptr( e, type, buf );
519 #endif /* LDAP_SLAPI */
523 slapi_is_rootdse( const char *dn )
526 return ( dn == NULL || dn[0] == '\0' );
533 * Add values to entry.
536 * LDAP_SUCCESS Values added to entry
537 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
538 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
541 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
547 char textbuf[SLAP_TEXT_BUFLEN];
549 mod.sm_op = LDAP_MOD_ADD;
551 mod.sm_type.bv_val = (char *)type;
552 mod.sm_type.bv_len = strlen( type );
554 rc = slap_str2ad( type, &mod.sm_desc, &text );
555 if ( rc != LDAP_SUCCESS ) {
559 if ( vals == NULL ) {
560 /* Apparently vals can be NULL
561 * FIXME: sm_bvalues = NULL ? */
562 mod.sm_bvalues = (BerVarray)ch_malloc( sizeof(struct berval) );
563 mod.sm_bvalues->bv_val = NULL;
566 rc = bvptr2obj( vals, &mod.sm_bvalues );
567 if ( rc != LDAP_SUCCESS ) {
568 return LDAP_CONSTRAINT_VIOLATION;
571 mod.sm_nvalues = NULL;
573 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
575 ch_free( mod.sm_bvalues );
577 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
580 #endif /* LDAP_SLAPI */
584 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
587 return slapi_entry_add_values( e, type, vals );
590 #endif /* LDAP_SLAPI */
594 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
597 AttributeDescription *ad = NULL;
601 rc = slap_str2ad( type, &ad, &text );
602 if ( rc != LDAP_SUCCESS ) {
606 return attr_merge_normalize( e, ad, *vs, NULL );
609 #endif /* LDAP_SLAPI */
613 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
619 char textbuf[SLAP_TEXT_BUFLEN];
621 mod.sm_op = LDAP_MOD_DELETE;
623 mod.sm_type.bv_val = (char *)type;
624 mod.sm_type.bv_len = strlen( type );
626 if ( vals == NULL ) {
627 /* If vals is NULL, this is a NOOP. */
631 rc = slap_str2ad( type, &mod.sm_desc, &text );
632 if ( rc != LDAP_SUCCESS ) {
636 if ( vals[0] == NULL ) {
637 /* SLAPI doco says LDAP_OPERATIONS_ERROR but LDAP_OTHER is better */
638 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
641 rc = bvptr2obj( vals, &mod.sm_bvalues );
642 if ( rc != LDAP_SUCCESS ) {
643 return LDAP_CONSTRAINT_VIOLATION;
645 mod.sm_nvalues = NULL;
647 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
649 ch_free( mod.sm_bvalues );
654 #endif /* LDAP_SLAPI */
658 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
661 return slapi_entry_delete_values( e, type, vals );
664 #endif /* LDAP_SLAPI */
668 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
671 return slapi_entry_attr_merge( e, (char *)type, vals );
674 #endif /* LDAP_SLAPI */
678 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
681 AttributeDescription *ad = NULL;
685 rc = slap_str2ad( type, &ad, &text );
686 if ( rc != LDAP_SUCCESS ) {
690 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
691 if ( rc != LDAP_SUCCESS ) {
698 #endif /* LDAP_SLAPI */
702 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
707 val.bv_val = (char *)value;
708 val.bv_len = strlen( value );
710 return slapi_entry_add_value( e, type, &val );
713 #endif /* LDAP_SLAPI */
717 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
720 Slapi_Value *vals[2];
723 val.bv_val = (char *)value;
724 val.bv_len = strlen( value );
728 return slapi_entry_delete_values_sv( e, type, vals );
731 #endif /* LDAP_SLAPI */
736 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
739 return slapi_entry_attr_merge( e, (char *)type, vals );
746 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
755 return ( *attr != NULL ) ? 0 : -1;
762 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
769 if ( prevattr == NULL ) {
773 *attr = prevattr->a_next;
775 return ( *attr != NULL ) ? 0 : -1;
782 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
785 AttributeDescription *ad = NULL;
790 rc = slap_str2ad( type, &ad, &text );
791 if ( rc != LDAP_SUCCESS ) {
795 attr_delete( &e->e_attrs, ad );
797 rc = bvptr2obj( vals, &bv );
798 if ( rc != LDAP_SUCCESS ) {
802 rc = attr_merge_normalize( e, ad, bv, NULL );
803 slapi_ch_free( (void **)&bv );
804 if ( rc != LDAP_SUCCESS ) {
811 #endif /* LDAP_SLAPI */
815 * FIXME -- The caller must free the allocated memory.
816 * In Netscape they do not have to.
819 slapi_attr_get_values(
821 struct berval ***vals )
827 if ( attr == NULL ) {
831 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
835 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
836 for ( j = 0; j < i; j++ ) {
837 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
841 *vals = (struct berval **)bv;
844 #else /* LDAP_SLAPI */
846 #endif /* LDAP_SLAPI */
850 slapi_dn_normalize( char *dn )
856 assert( dn != NULL );
859 bdn.bv_len = strlen( dn );
861 if ( dnPretty( NULL, &bdn, &pdn, NULL ) != LDAP_SUCCESS ) {
866 #else /* LDAP_SLAPI */
868 #endif /* LDAP_SLAPI */
872 slapi_dn_normalize_case( char *dn )
878 assert( dn != NULL );
881 bdn.bv_len = strlen( dn );
883 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
888 #else /* LDAP_SLAPI */
890 #endif /* LDAP_SLAPI */
899 struct berval bdn, ndn;
900 struct berval bsuffix, nsuffix;
903 assert( dn != NULL );
904 assert( suffix != NULL );
907 bdn.bv_len = strlen( dn );
909 bsuffix.bv_val = suffix;
910 bsuffix.bv_len = strlen( suffix );
912 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
916 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
919 slapi_ch_free( (void **)&ndn.bv_val );
923 rc = dnIsSuffix( &ndn, &nsuffix );
925 slapi_ch_free( (void **)&ndn.bv_val );
926 slapi_ch_free( (void **)&nsuffix.bv_val );
929 #else /* LDAP_SLAPI */
931 #endif /* LDAP_SLAPI */
936 const char *parentdn,
937 const char *childdn )
940 struct berval assertedParentDN, normalizedAssertedParentDN;
941 struct berval childDN, normalizedChildDN;
942 struct berval normalizedParentDN;
945 assert( parentdn != NULL );
946 assert( childdn != NULL );
948 assertedParentDN.bv_val = (char *)parentdn;
949 assertedParentDN.bv_len = strlen( parentdn );
951 if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
952 &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
957 childDN.bv_val = (char *)childdn;
958 childDN.bv_len = strlen( childdn );
960 if ( dnNormalize( 0, NULL, NULL, &childDN,
961 &normalizedChildDN, NULL ) != LDAP_SUCCESS )
963 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
967 dnParent( &normalizedChildDN, &normalizedParentDN );
969 if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
970 &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
975 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
976 slapi_ch_free( (void **)&normalizedChildDN.bv_val );
978 return ( match == 0 );
981 #endif /* LDAP_SLAPI */
985 * Returns DN of the parent entry, or NULL if the DN is
986 * an empty string or NULL, or has no parent.
989 slapi_dn_parent( const char *_dn )
992 struct berval dn, prettyDN;
993 struct berval parentDN;
999 dn.bv_val = (char *)_dn;
1000 dn.bv_len = strlen( _dn );
1002 if ( dn.bv_len == 0 ) {
1006 if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
1010 dnParent( &prettyDN, &parentDN ); /* in-place */
1012 slapi_ch_free( (void **)&prettyDN.bv_val );
1014 if ( parentDN.bv_len == 0 ) {
1018 return slapi_ch_strdup( parentDN.bv_val );
1021 #endif /* LDAP_SLAPI */
1025 * Returns DN of the parent entry; or NULL if the DN is
1026 * an empty string, if the DN has no parent, or if the
1027 * DN is the suffix of the backend database
1029 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *_dn )
1033 struct berval dn, prettyDN;
1034 struct berval normalizedDN, parentDN;
1036 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
1039 dn.bv_val = (char *)_dn;
1040 dn.bv_len = strlen( _dn );
1042 if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
1046 if ( be != NULL && be_issuffix( be, &normalizedDN ) ) {
1047 slapi_ch_free( (void **)&prettyDN.bv_val );
1048 slapi_ch_free( (void **)&normalizedDN.bv_val );
1052 dnParent( &prettyDN, &parentDN );
1054 slapi_ch_free( (void **)&prettyDN.bv_val );
1055 slapi_ch_free( (void **)&normalizedDN.bv_val );
1057 if ( parentDN.bv_len == 0 ) {
1061 return slapi_ch_strdup( parentDN.bv_val );
1064 #endif /* LDAP_SLAPI */
1068 slapi_dn_ignore_case( char *dn )
1071 return slapi_dn_normalize_case( dn );
1072 #else /* LDAP_SLAPI */
1074 #endif /* LDAP_SLAPI */
1078 slapi_ch_malloc( unsigned long size )
1081 return ch_malloc( size );
1082 #else /* LDAP_SLAPI */
1084 #endif /* LDAP_SLAPI */
1088 slapi_ch_free( void **ptr )
1093 #endif /* LDAP_SLAPI */
1097 slapi_ch_free_string( char **ptr )
1100 slapi_ch_free( (void **)ptr );
1101 #endif /* LDAP_SLAPI */
1105 slapi_ch_array_free( char **arrayp )
1110 if ( arrayp != NULL ) {
1111 for ( p = arrayp; *p != NULL; p++ ) {
1112 slapi_ch_free( (void **)p );
1114 slapi_ch_free( (void **)&arrayp );
1120 slapi_ch_bvdup(const struct berval *v)
1125 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
1126 bv->bv_len = v->bv_len;
1127 bv->bv_val = slapi_ch_malloc( bv->bv_len );
1128 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1137 slapi_ch_bvecdup(const struct berval **v)
1147 for ( i = 0; v[i] != NULL; i++ )
1150 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1152 for ( i = 0; v[i] != NULL; i++ ) {
1153 rv[i] = slapi_ch_bvdup( v[i] );
1165 unsigned long nelem,
1166 unsigned long size )
1169 return ch_calloc( nelem, size );
1170 #else /* LDAP_SLAPI */
1172 #endif /* LDAP_SLAPI */
1178 unsigned long size )
1181 return ch_realloc( block, size );
1182 #else /* LDAP_SLAPI */
1184 #endif /* LDAP_SLAPI */
1188 slapi_ch_strdup( char *s )
1191 return ch_strdup( (const char *)s );
1192 #else /* LDAP_SLAPI */
1194 #endif /* LDAP_SLAPI */
1198 slapi_ch_stlen( char *s )
1201 return strlen( (const char *)s );
1202 #else /* LDAP_SLAPI */
1204 #endif /* LDAP_SLAPI */
1208 slapi_control_present(
1209 LDAPControl **controls,
1211 struct berval **val,
1226 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1227 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1232 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1234 * FIXME: according to 6.1 specification,
1235 * "The val output parameter is set
1236 * to point into the controls array.
1237 * A copy of the control value is
1241 struct berval *pTmpBval;
1243 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
1244 if ( pTmpBval == NULL ) {
1247 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
1248 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
1252 slapi_ch_free( (void **)&pTmpBval );
1258 *val = &controls[i]->ldctl_value;
1263 *iscritical = controls[i]->ldctl_iscritical;
1270 #else /* LDAP_SLAPI */
1272 #endif /* LDAP_SLAPI */
1277 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1278 unsigned long *slapi_mask)
1280 *slapi_mask = SLAPI_OPERATION_NONE;
1282 if ( slap_mask & SLAP_CTRL_ABANDON )
1283 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1285 if ( slap_mask & SLAP_CTRL_ADD )
1286 *slapi_mask |= SLAPI_OPERATION_ADD;
1288 if ( slap_mask & SLAP_CTRL_BIND )
1289 *slapi_mask |= SLAPI_OPERATION_BIND;
1291 if ( slap_mask & SLAP_CTRL_COMPARE )
1292 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1294 if ( slap_mask & SLAP_CTRL_DELETE )
1295 *slapi_mask |= SLAPI_OPERATION_DELETE;
1297 if ( slap_mask & SLAP_CTRL_MODIFY )
1298 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1300 if ( slap_mask & SLAP_CTRL_RENAME )
1301 *slapi_mask |= SLAPI_OPERATION_MODDN;
1303 if ( slap_mask & SLAP_CTRL_SEARCH )
1304 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1306 if ( slap_mask & SLAP_CTRL_UNBIND )
1307 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1311 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1312 slap_mask_t *slap_mask)
1316 if ( slapi_mask & SLAPI_OPERATION_BIND )
1317 *slap_mask |= SLAP_CTRL_BIND;
1319 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1320 *slap_mask |= SLAP_CTRL_UNBIND;
1322 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1323 *slap_mask |= SLAP_CTRL_SEARCH;
1325 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1326 *slap_mask |= SLAP_CTRL_MODIFY;
1328 if ( slapi_mask & SLAPI_OPERATION_ADD )
1329 *slap_mask |= SLAP_CTRL_ADD;
1331 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1332 *slap_mask |= SLAP_CTRL_DELETE;
1334 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1335 *slap_mask |= SLAP_CTRL_RENAME;
1337 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1338 *slap_mask |= SLAP_CTRL_COMPARE;
1340 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1341 *slap_mask |= SLAP_CTRL_ABANDON;
1343 *slap_mask |= SLAP_CTRL_FRONTEND;
1347 slapi_int_parse_control(
1352 /* Plugins must deal with controls themselves. */
1354 return LDAP_SUCCESS;
1356 #endif /* LDAP_SLAPI */
1359 slapi_register_supported_control(
1361 unsigned long controlops )
1364 slap_mask_t controlmask;
1366 slapiControlOp2SlapControlMask( controlops, &controlmask );
1368 register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control );
1369 #endif /* LDAP_SLAPI */
1373 slapi_get_supported_controls(
1375 unsigned long **ctrlopsp )
1380 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1381 if ( rc != LDAP_SUCCESS ) {
1385 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1386 /* In place, naughty. */
1387 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1390 return LDAP_SUCCESS;
1391 #else /* LDAP_SLAPI */
1393 #endif /* LDAP_SLAPI */
1397 slapi_dup_control( LDAPControl *ctrl )
1402 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1403 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1404 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1405 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1410 #endif /* LDAP_SLAPI */
1414 slapi_register_supported_saslmechanism( char *mechanism )
1417 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1418 slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1419 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1420 #endif /* LDAP_SLAPI */
1424 slapi_get_supported_saslmechanisms( void )
1427 /* FIXME -- can not get the saslmechanism without a connection. */
1428 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1429 "can not get the SASL mechanism list "
1430 "without a connection\n" );
1432 #else /* LDAP_SLAPI */
1434 #endif /* LDAP_SLAPI */
1438 slapi_get_supported_extended_ops( void )
1442 char **ppExtOpOID = NULL;
1445 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1449 for ( j = 0; slapi_int_get_supported_extop( j ) != NULL; j++ ) {
1454 if ( numExtOps == 0 ) {
1458 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1459 for ( k = 0; k < i; k++ ) {
1462 bv = get_supported_extop( k );
1463 assert( bv != NULL );
1465 ppExtOpOID[ k ] = bv->bv_val;
1468 for ( ; k < j; k++ ) {
1471 bv = slapi_int_get_supported_extop( k );
1472 assert( bv != NULL );
1474 ppExtOpOID[ i + k ] = bv->bv_val;
1476 ppExtOpOID[ i + k ] = NULL;
1479 #else /* LDAP_SLAPI */
1481 #endif /* LDAP_SLAPI */
1485 slapi_send_ldap_result(
1491 struct berval **urls )
1496 char *extOID = NULL;
1497 struct berval *extValue = NULL;
1499 SlapReply rs = { REP_RESULT };
1501 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1504 rs.sr_matched = matched;
1509 slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
1511 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1512 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
1513 send_ldap_sasl( op, &rs );
1517 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1518 if ( extOID != NULL ) {
1519 rs.sr_rspoid = extOID;
1520 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
1521 send_ldap_extended( op, &rs );
1525 if (op->o_tag == LDAP_REQ_SEARCH)
1526 rs.sr_nentries = nentries;
1528 send_ldap_result( op, &rs );
1529 #endif /* LDAP_SLAPI */
1533 slapi_send_ldap_search_entry(
1536 LDAPControl **ectrls,
1542 SlapReply rs = { REP_RESULT };
1544 AttributeName *an = NULL;
1547 if ( attrs != NULL ) {
1548 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1556 an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
1557 for ( i = 0; attrs[i] != NULL; i++ ) {
1558 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1559 an[i].an_name.bv_len = strlen( attrs[i] );
1560 an[i].an_desc = NULL;
1561 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1564 an[i].an_name.bv_len = 0;
1565 an[i].an_name.bv_val = NULL;
1568 rs.sr_err = LDAP_SUCCESS;
1569 rs.sr_matched = NULL;
1572 rs.sr_ctrls = ectrls;
1577 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1581 return send_search_entry( pOp, &rs );
1582 #else /* LDAP_SLAPI */
1584 #endif /* LDAP_SLAPI */
1588 slapi_send_ldap_search_reference(
1591 struct berval **references,
1592 LDAPControl **ectrls,
1593 struct berval **v2refs
1598 SlapReply rs = { REP_SEARCHREF };
1601 rs.sr_err = LDAP_SUCCESS;
1602 rs.sr_matched = NULL;
1605 rc = bvptr2obj( references, &rs.sr_ref );
1606 if ( rc != LDAP_SUCCESS ) {
1610 rs.sr_ctrls = ectrls;
1614 if ( v2refs != NULL ) {
1615 rc = bvptr2obj( v2refs, &rs.sr_v2ref );
1616 if ( rc != LDAP_SUCCESS ) {
1617 slapi_ch_free( (void **)&rs.sr_ref );
1624 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1628 rc = send_search_reference( pOp, &rs );
1630 if ( rs.sr_ref != NULL )
1631 slapi_ch_free( (void **)&rs.sr_ref );
1633 if ( rs.sr_v2ref != NULL )
1634 slapi_ch_free( (void **)&rs.sr_v2ref );
1639 #endif /* LDAP_SLAPI */
1643 slapi_str2filter( char *str )
1646 return str2filter( str );
1647 #else /* LDAP_SLAPI */
1649 #endif /* LDAP_SLAPI */
1659 #endif /* LDAP_SLAPI */
1663 slapi_filter_dup( Slapi_Filter *filter )
1668 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1670 f->f_choice = filter->f_choice;
1672 switch ( f->f_choice ) {
1673 case LDAP_FILTER_AND:
1674 case LDAP_FILTER_NOT:
1675 case LDAP_FILTER_OR: {
1676 Filter *pFilter, **ppF;
1678 for ( pFilter = filter->f_list, ppF = &f->f_list;
1680 pFilter = pFilter->f_next, ppF = &f->f_next )
1682 *ppF = slapi_filter_dup( pFilter );
1688 case LDAP_FILTER_PRESENT:
1689 f->f_desc = filter->f_desc;
1691 case LDAP_FILTER_EQUALITY:
1692 case LDAP_FILTER_GE:
1693 case LDAP_FILTER_LE:
1694 case LDAP_FILTER_APPROX:
1695 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1696 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1697 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1699 case LDAP_FILTER_EXT:
1700 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1701 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1702 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1703 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1704 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1705 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1707 case LDAP_FILTER_SUBSTRINGS: {
1710 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1711 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1712 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1713 if ( filter->f_sub_any != NULL ) {
1714 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1716 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1717 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1718 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1720 f->f_sub_any[i].bv_val = NULL;
1722 f->f_sub_any = NULL;
1724 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1727 case SLAPD_FILTER_COMPUTED:
1728 f->f_result = filter->f_result;
1731 slapi_ch_free( (void **)&f );
1739 #endif /* LDAP_SLAPI */
1743 slapi_filter_get_choice( Slapi_Filter *f )
1755 #else /* LDAP_SLAPI */
1756 return -1; /* invalid filter type */
1757 #endif /* LDAP_SLAPI */
1761 slapi_filter_get_ava(
1764 struct berval **bval )
1768 int rc = LDAP_SUCCESS;
1770 assert( type != NULL );
1771 assert( bval != NULL );
1776 ftype = f->f_choice;
1777 if ( ftype == LDAP_FILTER_EQUALITY
1778 || ftype == LDAP_FILTER_GE
1779 || ftype == LDAP_FILTER_LE
1780 || ftype == LDAP_FILTER_APPROX ) {
1782 * According to the SLAPI Reference Manual these are
1785 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1786 *bval = &f->f_un.f_un_ava->aa_value;
1787 } else { /* filter type not supported */
1792 #else /* LDAP_SLAPI */
1794 #endif /* LDAP_SLAPI */
1798 slapi_filter_list_first( Slapi_Filter *f )
1807 ftype = f->f_choice;
1808 if ( ftype == LDAP_FILTER_AND
1809 || ftype == LDAP_FILTER_OR
1810 || ftype == LDAP_FILTER_NOT ) {
1811 return (Slapi_Filter *)f->f_list;
1815 #else /* LDAP_SLAPI */
1817 #endif /* LDAP_SLAPI */
1821 slapi_filter_list_next(
1823 Slapi_Filter *fprev )
1832 ftype = f->f_choice;
1833 if ( ftype == LDAP_FILTER_AND
1834 || ftype == LDAP_FILTER_OR
1835 || ftype == LDAP_FILTER_NOT )
1837 return fprev->f_next;
1841 #else /* LDAP_SLAPI */
1843 #endif /* LDAP_SLAPI */
1847 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1854 switch ( f->f_choice ) {
1855 case LDAP_FILTER_GE:
1856 case LDAP_FILTER_LE:
1857 case LDAP_FILTER_EQUALITY:
1858 case LDAP_FILTER_APPROX:
1859 *type = f->f_av_desc->ad_cname.bv_val;
1861 case LDAP_FILTER_SUBSTRINGS:
1862 *type = f->f_sub_desc->ad_cname.bv_val;
1864 case LDAP_FILTER_PRESENT:
1865 *type = f->f_desc->ad_cname.bv_val;
1867 case LDAP_FILTER_EXT:
1868 *type = f->f_mr_desc->ad_cname.bv_val;
1871 /* Complex filters need not apply. */
1879 #endif /* LDAP_SLAPI */
1883 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1884 char ***any, char **final )
1889 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1894 * The caller shouldn't free but we can't return an
1895 * array of char *s from an array of bervals without
1896 * allocating memory, so we may as well be consistent.
1899 *type = f->f_sub_desc->ad_cname.bv_val;
1900 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1901 if ( f->f_sub_any != NULL ) {
1902 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1904 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1905 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1906 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1912 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1917 #endif /* LDAP_SLAPI */
1921 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1924 Slapi_Filter *f = NULL;
1926 if ( ftype == LDAP_FILTER_AND ||
1927 ftype == LDAP_FILTER_OR ||
1928 ftype == LDAP_FILTER_NOT )
1930 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1931 f->f_choice = ftype;
1933 f->f_list->f_next = f2;
1940 #endif /* LDAP_SLAPI */
1944 slapi_x_filter_append( int ftype,
1945 Slapi_Filter **pContainingFilter, /* NULL on first call */
1946 Slapi_Filter **pNextFilter,
1947 Slapi_Filter *filterToAppend )
1950 if ( ftype == LDAP_FILTER_AND ||
1951 ftype == LDAP_FILTER_OR ||
1952 ftype == LDAP_FILTER_NOT )
1954 if ( *pContainingFilter == NULL ) {
1955 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
1956 (*pContainingFilter)->f_choice = ftype;
1957 (*pContainingFilter)->f_list = filterToAppend;
1958 (*pContainingFilter)->f_next = NULL;
1960 if ( (*pContainingFilter)->f_choice != ftype ) {
1964 (*pNextFilter)->f_next = filterToAppend;
1966 *pNextFilter = filterToAppend;
1970 #endif /* LDAP_SLAPI */
1975 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1983 /* spec says return zero if no filter. */
1987 if ( verify_access ) {
1988 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
1990 return LDAP_PARAM_ERROR;
1996 * According to acl.c it is safe to call test_filter() with
1999 rc = test_filter( op, e, f );
2001 case LDAP_COMPARE_TRUE:
2004 case LDAP_COMPARE_FALSE:
2006 case SLAPD_COMPARE_UNDEFINED:
2009 case LDAP_PROTOCOL_ERROR:
2010 /* filter type unknown: spec says return -1 */
2018 #endif /* LDAP_SLAPI */
2022 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
2025 return slapi_filter_test( NULL, e, f, 0 );
2032 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
2035 switch ( f->f_choice ) {
2036 case LDAP_FILTER_AND:
2037 case LDAP_FILTER_NOT:
2038 case LDAP_FILTER_OR: {
2042 * FIXME: altering f; should we use a temporary?
2044 for ( f = f->f_list; f != NULL; f = f->f_next ) {
2045 rc = slapi_filter_apply( f, fn, arg, error_code );
2049 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
2055 case LDAP_FILTER_EQUALITY:
2056 case LDAP_FILTER_SUBSTRINGS:
2057 case LDAP_FILTER_GE:
2058 case LDAP_FILTER_LE:
2059 case LDAP_FILTER_PRESENT:
2060 case LDAP_FILTER_APPROX:
2061 case LDAP_FILTER_EXT:
2062 *error_code = fn( f, arg );
2065 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2068 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
2069 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
2075 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2077 #endif /* LDAP_SLAPI */
2081 slapi_send_ldap_extended_response(
2086 struct berval *response )
2091 rs.sr_err = errornum;
2092 rs.sr_matched = NULL;
2096 rs.sr_rspoid = respName;
2097 rs.sr_rspdata = response;
2099 send_ldap_extended( op, &rs );
2101 return LDAP_SUCCESS;
2102 #else /* LDAP_SLAPI */
2104 #endif /* LDAP_SLAPI */
2109 struct berval **vals,
2114 * FIXME: what's the point?
2117 #else /* LDAP_SLAPI */
2119 #endif /* LDAP_SLAPI */
2122 #define MAX_HOSTNAME 512
2125 slapi_get_hostname( void )
2129 static int been_here = 0;
2130 static char *static_hn = NULL;
2132 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
2134 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
2135 if ( static_hn == NULL) {
2136 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
2137 "Cannot allocate memory for hostname\n" );
2139 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2144 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
2145 slapi_log_error( SLAPI_LOG_FATAL,
2147 "can't get hostname\n" );
2148 slapi_ch_free( (void **)&static_hn );
2150 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2159 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2161 hn = ch_strdup( static_hn );
2164 #else /* LDAP_SLAPI */
2166 #endif /* LDAP_SLAPI */
2170 * FIXME: this should go in an appropriate header ...
2172 extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist );
2182 int rc = LDAP_SUCCESS;
2185 va_start( arglist, fmt );
2186 rc = slapi_int_log_error( severity, subsystem, fmt, arglist );
2190 #else /* LDAP_SLAPI */
2192 #endif /* LDAP_SLAPI */
2197 slapi_timer_current_time( void )
2200 static int first_time = 1;
2201 #if !defined (_WIN32)
2205 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
2208 gettimeofday( &base_time, NULL );
2210 gettimeofday( &now, NULL );
2211 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
2212 (now.tv_usec - base_time.tv_usec);
2213 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
2219 return (slap_get_time() - starttime) * 1000000;
2226 performance_counter_present = QueryPerformanceCounter( &base_time );
2227 QueryPerformanceFrequency( &performance_freq );
2230 if ( !performance_counter_present )
2233 QueryPerformanceCounter( &now );
2234 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2236 #else /* LDAP_SLAPI */
2238 #endif /* LDAP_SLAPI */
2245 slapi_timer_get_time( char *label )
2248 unsigned long start = slapi_timer_current_time();
2249 printf("%10ld %10ld usec %s\n", start, 0, label);
2251 #else /* LDAP_SLAPI */
2253 #endif /* LDAP_SLAPI */
2260 slapi_timer_elapsed_time(
2262 unsigned long start )
2265 unsigned long stop = slapi_timer_current_time();
2266 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2267 #endif /* LDAP_SLAPI */
2271 slapi_free_search_results_internal( Slapi_PBlock *pb )
2274 Slapi_Entry **entries;
2275 int k = 0, nEnt = 0;
2277 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2278 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2283 if ( entries == NULL ) {
2287 for ( k = 0; k < nEnt; k++ ) {
2288 slapi_entry_free( entries[k] );
2291 slapi_ch_free( (void **)&entries );
2292 #endif /* LDAP_SLAPI */
2297 * Internal API to prime a Slapi_PBlock with a Backend.
2299 static int slapi_int_pblock_set_backend( Slapi_PBlock *pb, Backend *be )
2303 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2304 if ( rc != LDAP_SUCCESS )
2308 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2309 if ( rc != LDAP_SUCCESS )
2313 return LDAP_SUCCESS;
2317 * If oldStyle is TRUE, then a value suitable for setting to
2318 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
2319 * (pointer to static storage).
2321 * If oldStyle is FALSE, then a value suitable for setting to
2322 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2323 * a pointer to allocated memory and will include the SASL
2324 * mechanism (if any).
2326 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2331 switch ( authz->sai_method ) {
2332 case LDAP_AUTH_SASL:
2334 authType = SLAPD_AUTH_SASL;
2336 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2337 authType = slapi_ch_malloc( len );
2338 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2341 case LDAP_AUTH_SIMPLE:
2342 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2344 case LDAP_AUTH_NONE:
2345 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2351 if ( is_tls && authType == NULL ) {
2352 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2359 * Internal API to prime a Slapi_PBlock with a Connection.
2361 static int slapi_int_pblock_set_connection( Slapi_PBlock *pb, Connection *conn )
2366 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2367 if ( rc != LDAP_SUCCESS )
2370 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2371 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2372 if ( rc != LDAP_SUCCESS )
2374 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2375 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2376 if ( rc != LDAP_SUCCESS )
2380 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2381 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2382 if ( rc != LDAP_SUCCESS )
2384 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2385 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2386 if ( rc != LDAP_SUCCESS )
2390 #ifdef LDAP_CONNECTIONLESS
2391 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2392 if ( rc != LDAP_SUCCESS )
2396 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2397 if ( rc != LDAP_SUCCESS )
2400 /* Returns pointer to static string */
2401 connAuthType = Authorization2AuthType( &conn->c_authz,
2408 if ( connAuthType != NULL ) {
2409 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2410 if ( rc != LDAP_SUCCESS )
2414 /* Returns pointer to allocated string */
2415 connAuthType = Authorization2AuthType( &conn->c_authz,
2422 if ( connAuthType != NULL ) {
2423 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2424 /* slapi_pblock_set dups this itself */
2425 slapi_ch_free( (void **)&connAuthType );
2426 if ( rc != LDAP_SUCCESS )
2430 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
2431 /* slapi_pblock_set dups this itself */
2432 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)conn->c_authz.sai_dn.bv_val);
2433 if ( rc != LDAP_SUCCESS )
2437 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SSF, (void *)conn->c_ssf);
2438 if ( rc != LDAP_SUCCESS )
2441 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SASL_CONTEXT,
2442 ( conn->c_sasl_authctx != NULL ? conn->c_sasl_authctx :
2443 conn->c_sasl_sockctx ) );
2444 if ( rc != LDAP_SUCCESS )
2449 #endif /* LDAP_SLAPI */
2452 * Internal API to prime a Slapi_PBlock with an Operation.
2454 int slapi_int_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
2462 if ( op->o_bd != NULL ) {
2463 isRoot = be_isroot( op->o_bd, &op->o_ndn );
2464 isUpdateDn = be_isupdate( op->o_bd, &op->o_ndn );
2467 rc = slapi_int_pblock_set_backend( pb, op->o_bd );
2468 if ( rc != LDAP_SUCCESS )
2471 rc = slapi_int_pblock_set_connection( pb, op->o_conn );
2472 if ( rc != LDAP_SUCCESS )
2475 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2476 if ( rc != LDAP_SUCCESS )
2479 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2480 if ( rc != LDAP_SUCCESS )
2483 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2484 if ( rc != LDAP_SUCCESS )
2487 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2488 if ( rc != LDAP_SUCCESS )
2491 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2492 if ( rc != LDAP_SUCCESS )
2495 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2496 if ( rc != LDAP_SUCCESS )
2499 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2500 if ( rc != LDAP_SUCCESS)
2503 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2504 if ( rc != LDAP_SUCCESS )
2507 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2508 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
2509 /* Not quite sure what the point of this is. */
2510 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
2511 if ( rc != LDAP_SUCCESS )
2515 return LDAP_SUCCESS;
2521 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2526 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2528 *isSSL = conn->c_is_tls;
2533 return LDAP_SUCCESS;
2536 #endif /* LDAP_SLAPI */
2540 * DS 5.x compatability API follow
2543 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2549 return LDAP_PARAM_ERROR;
2551 at = attr->a_desc->ad_type;
2553 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2555 if ( is_at_single_value( at ) )
2556 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2557 if ( is_at_operational( at ) )
2558 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2559 if ( is_at_obsolete( at ) )
2560 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2561 if ( is_at_collective( at ) )
2562 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2563 if ( is_at_no_user_mod( at ) )
2564 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2566 return LDAP_SUCCESS;
2569 #endif /* LDAP_SLAPI */
2572 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2575 unsigned long flags;
2577 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2579 return (flags & flag) ? 1 : 0;
2582 #endif /* LDAP_SLAPI */
2585 Slapi_Attr *slapi_attr_new( void )
2590 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2598 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2602 AttributeDescription *ad = NULL;
2604 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2620 void slapi_attr_free( Slapi_Attr **a )
2628 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2631 return attr_dup( (Slapi_Attr *)attr );
2637 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2641 * FIXME: here we may lose alignment between a_vals/a_nvals
2643 return value_add_one( &a->a_vals, (Slapi_Value *)v );
2649 int slapi_attr_type2plugin( const char *type, void **pi )
2656 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2659 if ( attr == NULL ) {
2660 return LDAP_PARAM_ERROR;
2663 *type = attr->a_desc->ad_cname.bv_val;
2665 return LDAP_SUCCESS;
2671 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2674 if ( attr == NULL ) {
2675 return LDAP_PARAM_ERROR;
2677 *oidp = attr->a_desc->ad_type->sat_oid;
2679 return LDAP_SUCCESS;
2685 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2693 mr = a->a_desc->ad_type->sat_equality;
2694 rc = value_match( &ret, a->a_desc, mr,
2695 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2696 (struct berval *)v1, (void *)v2, &text );
2697 if ( rc != LDAP_SUCCESS )
2700 return ( ret == 0 ) ? 0 : -1;
2706 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2716 if ( a ->a_vals == NULL ) {
2719 mr = a->a_desc->ad_type->sat_equality;
2720 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2721 rc = value_match( &ret, a->a_desc, mr,
2722 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2723 if ( rc != LDAP_SUCCESS ) {
2730 #endif /* LDAP_SLAPI */
2734 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2737 AttributeDescription *a1 = NULL;
2738 AttributeDescription *a2 = NULL;
2742 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2746 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2750 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2751 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2752 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2755 case SLAPI_TYPE_CMP_EXACT:
2756 ret = ad_cmp( a1, a2 );
2758 case SLAPI_TYPE_CMP_BASE:
2759 ret = ad_base_cmp( a1, a2 );
2761 case SLAPI_TYPE_CMP_SUBTYPE:
2762 ret = is_ad_subtype( a2, a2 );
2775 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2778 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2784 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2787 return slapi_valueset_first_value( &a->a_vals, v );
2793 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2796 return slapi_valueset_next_value( &a->a_vals, hint, v );
2802 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2805 *numValues = slapi_valueset_count( &a->a_vals );
2813 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2816 *vs = &((Slapi_Attr *)a)->a_vals;
2824 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2827 return slapi_attr_get_values( a, vals );
2833 char *slapi_attr_syntax_normalize( const char *s )
2836 AttributeDescription *ad = NULL;
2839 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2843 return ad->ad_cname.bv_val;
2849 Slapi_Value *slapi_value_new( void )
2854 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2862 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2865 return ber_dupbv( NULL, (struct berval *)bval );
2871 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2874 return slapi_value_new_berval( v );
2880 Slapi_Value *slapi_value_new_string(const char *s)
2885 bv.bv_val = (char *)s;
2886 bv.bv_len = strlen( s );
2888 return slapi_value_new_berval( &bv );
2894 Slapi_Value *slapi_value_init(Slapi_Value *val)
2906 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2909 return ber_dupbv( v, bval );
2915 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2918 v->bv_val = slapi_ch_strdup( (char *)s );
2919 v->bv_len = strlen( s );
2927 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2930 return slapi_value_new_value( v );
2936 void slapi_value_free(Slapi_Value **value)
2939 if ( value == NULL ) {
2943 if ( (*value) != NULL ) {
2944 slapi_ch_free( (void **)&(*value)->bv_val );
2945 slapi_ch_free( (void **)value );
2950 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2959 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2962 if ( value == NULL ) {
2965 if ( value->bv_val != NULL ) {
2966 slapi_ch_free( (void **)&value->bv_val );
2968 slapi_value_init_berval( value, (struct berval *)bval );
2976 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2979 if ( value == NULL ) {
2982 return slapi_value_set_berval( value, vfrom );
2988 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2991 if ( value == NULL ) {
2994 if ( value->bv_val != NULL ) {
2995 slapi_ch_free( (void **)&value->bv_val );
2997 value->bv_val = slapi_ch_malloc( len );
2998 value->bv_len = len;
2999 AC_MEMCPY( value->bv_val, val, len );
3007 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
3010 if ( value == NULL ) {
3013 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
3020 int slapi_value_set_int(Slapi_Value *value, int intVal)
3025 snprintf( buf, sizeof( buf ), "%d", intVal );
3027 return slapi_value_set_string( value, buf );
3033 const char *slapi_value_get_string(const Slapi_Value *value)
3036 if ( value == NULL ) return NULL;
3037 if ( value->bv_val == NULL ) return NULL;
3038 if ( !checkBVString( value ) ) return NULL;
3040 return value->bv_val;
3046 int slapi_value_get_int(const Slapi_Value *value)
3049 if ( value == NULL ) return 0;
3050 if ( value->bv_val == NULL ) return 0;
3051 if ( !checkBVString( value ) ) return 0;
3053 return (int)strtol( value->bv_val, NULL, 10 );
3059 unsigned int slapi_value_get_uint(const Slapi_Value *value)
3062 if ( value == NULL ) return 0;
3063 if ( value->bv_val == NULL ) return 0;
3064 if ( !checkBVString( value ) ) return 0;
3066 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
3072 long slapi_value_get_long(const Slapi_Value *value)
3075 if ( value == NULL ) return 0;
3076 if ( value->bv_val == NULL ) return 0;
3077 if ( !checkBVString( value ) ) return 0;
3079 return strtol( value->bv_val, NULL, 10 );
3085 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
3088 if ( value == NULL ) return 0;
3089 if ( value->bv_val == NULL ) return 0;
3090 if ( !checkBVString( value ) ) return 0;
3092 return strtoul( value->bv_val, NULL, 10 );
3098 size_t slapi_value_get_length(const Slapi_Value *value)
3101 if ( value == NULL )
3104 return (size_t) value->bv_len;
3110 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
3113 return slapi_attr_value_cmp( a, v1, v2 );
3119 /* A ValueSet is a container for a BerVarray. */
3120 Slapi_ValueSet *slapi_valueset_new( void )
3125 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
3134 void slapi_valueset_free(Slapi_ValueSet *vs)
3140 ber_bvarray_free( vp );
3141 slapi_ch_free( (void **)&vp );
3148 void slapi_valueset_init(Slapi_ValueSet *vs)
3151 if ( vs != NULL && *vs == NULL ) {
3152 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
3153 (*vs)->bv_val = NULL;
3159 void slapi_valueset_done(Slapi_ValueSet *vs)
3167 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
3169 slapi_ch_free( (void **)&vp->bv_val );
3171 /* but don't free *vs or vs */
3175 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
3180 ber_dupbv( &bv, (Slapi_Value *)addval );
3181 ber_bvarray_add( vs, &bv );
3185 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
3188 return slapi_valueset_next_value( vs, 0, v );
3194 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
3205 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
3216 int slapi_valueset_count( const Slapi_ValueSet *vs )
3227 for ( i = 0; vp[i].bv_val != NULL; i++ )
3237 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
3242 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
3243 slapi_valueset_add_value( vs1, vp );
3248 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
3249 struct berval *val, int access )
3256 slap_access_t slap_access;
3257 AttributeDescription *ad = NULL;
3260 ret = slap_str2ad( attr, &ad, &text );
3261 if ( ret != LDAP_SUCCESS ) {
3265 switch ( access & SLAPI_ACL_ALL ) {
3266 case SLAPI_ACL_COMPARE:
3267 slap_access = ACL_COMPARE;
3269 case SLAPI_ACL_SEARCH:
3270 slap_access = ACL_SEARCH;
3272 case SLAPI_ACL_READ:
3273 slap_access = ACL_READ;
3275 case SLAPI_ACL_WRITE:
3276 case SLAPI_ACL_DELETE:
3278 case SLAPI_ACL_SELF:
3279 slap_access = ACL_WRITE;
3282 return LDAP_INSUFFICIENT_ACCESS;
3286 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3287 return LDAP_PARAM_ERROR;
3290 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3291 return LDAP_PARAM_ERROR;
3294 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3295 return LDAP_PARAM_ERROR;
3298 ret = access_allowed( op, e, ad, val, slap_access, NULL );
3300 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3302 return LDAP_UNWILLING_TO_PERFORM;
3306 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
3310 int rc = LDAP_SUCCESS;
3311 Modifications *ml, *mp;
3313 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3314 return LDAP_PARAM_ERROR;
3317 ml = slapi_int_ldapmods2modifications( mods );
3322 for ( mp = ml; mp != NULL; mp = mp->sml_next ) {
3323 rc = slap_bv2ad( &mp->sml_type, &mp->sml_desc, (const char **)errbuf );
3324 if ( rc != LDAP_SUCCESS ) {
3329 if ( rc == LDAP_SUCCESS ) {
3330 rc = acl_check_modlist( op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3333 /* Careful when freeing the modlist because it has pointers into the mods array. */
3334 for ( ; ml != NULL; ml = mp ) {
3337 /* just free the containing array */
3338 slapi_ch_free( (void **)&ml->sml_bvalues );
3339 slapi_ch_free( (void **)&ml );
3344 return LDAP_UNWILLING_TO_PERFORM;
3349 * Synthesise an LDAPMod array from a Modifications list to pass
3350 * to SLAPI. This synthesis is destructive and as such the
3351 * Modifications list may not be used after calling this
3354 * This function must also be called before slap_mods_check().
3356 LDAPMod **slapi_int_modifications2ldapmods(Modifications **pmodlist)
3359 Modifications *ml, *modlist;
3360 LDAPMod **mods, *modp;
3363 modlist = *pmodlist;
3365 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3368 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3370 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3371 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3373 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3375 /* Take ownership of original type. */
3376 modp->mod_type = ml->sml_type.bv_val;
3377 ml->sml_type.bv_val = NULL;
3379 if ( ml->sml_bvalues != NULL ) {
3380 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
3382 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3383 sizeof(struct berval *) );
3384 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
3385 /* Take ownership of original values. */
3386 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3387 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
3388 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
3389 ml->sml_bvalues[j].bv_len = 0;
3390 ml->sml_bvalues[j].bv_val = NULL;
3392 modp->mod_bvalues[j] = NULL;
3394 modp->mod_bvalues = NULL;
3401 slap_mods_free( modlist );
3411 * Convert a potentially modified array of LDAPMods back to a
3412 * Modification list.
3414 * The returned Modification list contains pointers into the
3415 * LDAPMods array; the latter MUST be freed with
3416 * slapi_int_free_ldapmods() (see below).
3418 Modifications *slapi_int_ldapmods2modifications (LDAPMod **mods)
3421 Modifications *modlist = NULL, **modtail;
3426 for( modp = mods; *modp != NULL; modp++ ) {
3430 struct berval **bvp;
3432 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3433 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3434 mod->sml_type.bv_val = (*modp)->mod_type;
3435 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3436 mod->sml_desc = NULL;
3437 mod->sml_next = NULL;
3439 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3440 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3443 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3448 mod->sml_bvalues = NULL;
3450 mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3452 /* NB: This implicitly trusts a plugin to return valid modifications. */
3453 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3454 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3455 mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
3456 mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
3459 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3460 mod->sml_bvalues[i].bv_val = *p;
3461 mod->sml_bvalues[i].bv_len = strlen( *p );
3464 mod->sml_bvalues[i].bv_val = NULL;
3465 mod->sml_bvalues[i].bv_len = 0;
3467 mod->sml_nvalues = NULL;
3470 modtail = &mod->sml_next;
3480 * This function only frees the parts of the mods array that
3481 * are not shared with the Modification list that was created
3482 * by slapi_int_ldapmods2modifications().
3485 void slapi_int_free_ldapmods (LDAPMod **mods)
3493 for ( i = 0; mods[i] != NULL; i++ ) {
3495 * Don't free values themselves; they're owned by the
3496 * Modification list. Do free the containing array.
3498 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3499 for ( j = 0; mods[i]->mod_bvalues != NULL && mods[i]->mod_bvalues[j] != NULL; j++ ) {
3500 ch_free( mods[i]->mod_bvalues[j] );
3502 ch_free( mods[i]->mod_bvalues );
3504 ch_free( mods[i]->mod_values );
3506 /* Don't free type, for same reasons. */
3510 #endif /* LDAP_SLAPI */
3514 * Sun ONE DS 5.x computed attribute support. Computed attributes
3515 * allow for dynamically generated operational attributes, a very
3516 * useful thing indeed.
3520 * Write the computed attribute to a BerElement. Complementary
3521 * functions need to be defined for anything that replaces
3522 * op->o_callback->sc_sendentry, if you wish to make computed
3523 * attributes available to it.
3525 int slapi_int_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
3528 Operation *op = NULL;
3530 AttributeDescription *desc = NULL;
3546 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
3547 if ( rc != 0 || op == NULL ) {
3551 ber = (BerElement *)c->cac_private;
3554 if ( c->cac_attrs == NULL ) {
3555 /* All attrs request, skip operational attributes */
3556 if ( is_at_operational( desc->ad_type ) ) {
3560 /* Specific attrs requested */
3561 if ( is_at_operational( desc->ad_type ) ) {
3562 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3566 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3572 if ( !access_allowed( op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
3573 slapi_log_error( SLAPI_LOG_ACL, "slapi_int_compute_output_ber",
3574 "acl: access to attribute %s not allowed\n",
3575 desc->ad_cname.bv_val );
3579 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
3581 slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
3582 "ber_printf failed\n");
3586 if ( !c->cac_attrsonly ) {
3587 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
3588 if ( !access_allowed( op, e,
3589 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
3590 slapi_log_error( SLAPI_LOG_ACL, "slapi_int_compute_output_ber",
3592 "acl: access to %s, value %d not allowed\n",
3593 op->o_connid, desc->ad_cname.bv_val, i );
3597 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
3598 slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
3599 "ber_printf failed\n");
3605 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
3606 slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
3607 "ber_printf failed\n" );
3618 * For some reason Sun don't use the normal plugin mechanism
3619 * registration path to register an "evaluator" function (an
3620 * "evaluator" is responsible for adding computed attributes;
3621 * the nomenclature is somewhat confusing).
3623 * As such slapi_compute_add_evaluator() registers the
3624 * function directly.
3626 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3629 Slapi_PBlock *pPlugin = NULL;
3632 pPlugin = slapi_pblock_new();
3633 if ( pPlugin == NULL ) {
3634 rc = LDAP_NO_MEMORY;
3638 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3639 if ( rc != LDAP_SUCCESS ) {
3643 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3644 if ( rc != LDAP_SUCCESS ) {
3648 rc = slapi_int_register_plugin( NULL, pPlugin );
3655 if ( rc != LDAP_SUCCESS ) {
3656 if ( pPlugin != NULL ) {
3657 slapi_pblock_destroy( pPlugin );
3665 #endif /* LDAP_SLAPI */
3669 * See notes above regarding slapi_compute_add_evaluator().
3671 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3674 Slapi_PBlock *pPlugin = NULL;
3677 pPlugin = slapi_pblock_new();
3678 if ( pPlugin == NULL ) {
3679 rc = LDAP_NO_MEMORY;
3683 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3684 if ( rc != LDAP_SUCCESS ) {
3688 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3689 if ( rc != LDAP_SUCCESS ) {
3693 rc = slapi_int_register_plugin( NULL, pPlugin );
3700 if ( rc != LDAP_SUCCESS ) {
3701 if ( pPlugin != NULL ) {
3702 slapi_pblock_destroy( pPlugin );
3710 #endif /* LDAP_SLAPI */
3714 * Call compute evaluators
3716 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3720 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3722 rc = slapi_int_get_plugins( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3723 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3724 /* Nothing to do; front-end should ignore. */
3728 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3730 * -1: no attribute matched requested type
3731 * 0: one attribute matched
3732 * >0: error happened
3734 rc = (*pGetPlugin)( c, type, e, outputfn );
3740 slapi_ch_free( (void **)&tmpPlugin );
3745 #endif /* LDAP_SLAPI */
3748 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3754 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3759 return slapi_int_call_plugins( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3762 #endif /* LDAP_SLAPI */
3766 * New API to provide the plugin with access to the search
3767 * pblock. Have informed Sun DS team.
3769 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3775 if ( c->cac_pb == NULL )
3783 #endif /* LDAP_SLAPI */
3786 Slapi_Mutex *slapi_new_mutex( void )
3791 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3792 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3793 slapi_ch_free( (void **)&m );
3803 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3806 if ( mutex != NULL ) {
3807 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3808 slapi_ch_free( (void **)&mutex);
3813 void slapi_lock_mutex( Slapi_Mutex *mutex )
3816 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3820 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3823 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3829 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3834 if ( mutex == NULL ) {
3838 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3839 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3840 slapi_ch_free( (void **)&cv );
3844 /* XXX struct copy */
3845 cv->mutex = mutex->mutex;
3853 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3856 if ( cvar != NULL ) {
3857 ldap_pvt_thread_cond_destroy( &cvar->cond );
3858 slapi_ch_free( (void **)&cvar );
3863 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3866 if ( cvar == NULL ) {
3870 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3876 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3879 if ( cvar == NULL ) {
3884 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3887 return ldap_pvt_thread_cond_signal( &cvar->cond );
3893 int slapi_int_access_allowed( Operation *op,
3895 AttributeDescription *desc,
3897 slap_access_t access,
3898 AccessControlState *state )
3901 int rc, slap_access = 0;
3902 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3904 if ( op->o_pb == NULL ) {
3905 /* internal operation */
3911 slap_access |= SLAPI_ACL_ADD | SLAPI_ACL_DELETE | SLAPI_ACL_WRITE;
3914 slap_access |= SLAPI_ACL_READ;
3917 slap_access |= SLAPI_ACL_SEARCH;
3920 slap_access = ACL_COMPARE;
3926 rc = slapi_int_get_plugins( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
3927 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3928 /* nothing to do; allowed access */
3932 slapi_int_pblock_set_operation( op->o_pb, op );
3934 rc = 1; /* default allow policy */
3936 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3941 rc = (*pGetPlugin)( op->o_pb, entry, desc->ad_cname.bv_val,
3942 val, slap_access, (void *)state );
3948 slapi_ch_free( (void **)&tmpPlugin );
3953 #endif /* LDAP_SLAPI */