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 )
169 return entry_dup( e );
170 #else /* LDAP_SLAPI */
172 #endif /* LDAP_SLAPI */
176 slapi_entry_attr_delete(
181 AttributeDescription *ad = NULL;
184 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
185 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
188 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
189 return 0; /* attribute is deleted */
191 return -1; /* something went wrong */
193 #else /* LDAP_SLAPI */
195 #endif /* LDAP_SLAPI */
199 slapi_entry_alloc( void )
202 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
203 #else /* LDAP_SLAPI */
205 #endif /* LDAP_SLAPI */
209 slapi_entry_free( Slapi_Entry *e )
213 #endif /* LDAP_SLAPI */
217 slapi_entry_attr_merge(
220 struct berval **vals )
223 AttributeDescription *ad = NULL;
228 rc = bvptr2obj( vals, &bv );
229 if ( rc != LDAP_SUCCESS ) {
233 rc = slap_str2ad( type, &ad, &text );
234 if ( rc != LDAP_SUCCESS ) {
238 rc = attr_merge_normalize_one( e, ad, bv, NULL );
242 #else /* LDAP_SLAPI */
244 #endif /* LDAP_SLAPI */
248 slapi_entry_attr_find(
254 AttributeDescription *ad = NULL;
258 rc = slap_str2ad( type, &ad, &text );
259 if ( rc != LDAP_SUCCESS ) {
263 *attr = attr_find( e->e_attrs, ad );
264 if ( *attr == NULL ) {
269 #else /* LDAP_SLAPI */
271 #endif /* LDAP_SLAPI */
275 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
278 AttributeDescription *ad = NULL;
283 rc = slap_str2ad( type, &ad, &text );
284 if ( rc != LDAP_SUCCESS ) {
288 attr = attr_find( e->e_attrs, ad );
289 if ( attr == NULL ) {
293 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
294 return slapi_ch_strdup( attr->a_vals[0].bv_val );
304 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
307 AttributeDescription *ad = NULL;
312 rc = slap_str2ad( type, &ad, &text );
313 if ( rc != LDAP_SUCCESS ) {
317 attr = attr_find( e->e_attrs, ad );
318 if ( attr == NULL ) {
322 return slapi_value_get_int( attr->a_vals );
329 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
332 AttributeDescription *ad = NULL;
337 rc = slap_str2ad( type, &ad, &text );
338 if ( rc != LDAP_SUCCESS ) {
342 attr = attr_find( e->e_attrs, ad );
343 if ( attr == NULL ) {
347 return slapi_value_get_long( attr->a_vals );
354 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
357 AttributeDescription *ad = NULL;
362 rc = slap_str2ad( type, &ad, &text );
363 if ( rc != LDAP_SUCCESS ) {
367 attr = attr_find( e->e_attrs, ad );
368 if ( attr == NULL ) {
372 return slapi_value_get_uint( attr->a_vals );
379 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
382 AttributeDescription *ad = NULL;
387 rc = slap_str2ad( type, &ad, &text );
388 if ( rc != LDAP_SUCCESS ) {
392 attr = attr_find( e->e_attrs, ad );
393 if ( attr == NULL ) {
397 return slapi_value_get_ulong( attr->a_vals );
404 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
408 AttributeDescription *ad = NULL;
413 rc = slap_str2ad( type, &ad, &text );
414 if ( rc != LDAP_SUCCESS ) {
418 attr = attr_find( e->e_attrs, ad );
419 if ( attr == NULL ) {
423 bv.bv_val = (char *)value;
424 bv.bv_len = strlen( value );
426 return ( slapi_attr_value_find( attr, &bv ) != -1 );
433 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
436 AttributeDescription *ad = NULL;
441 rc = slap_str2ad( type, &ad, &text );
442 if ( rc != LDAP_SUCCESS ) {
446 attr_delete ( &e->e_attrs, ad );
447 if ( value != NULL ) {
448 bv.bv_val = (char *)value;
449 bv.bv_len = strlen(value);
450 attr_merge_normalize_one( e, ad, &bv, NULL );
452 #endif /* LDAP_SLAPI */
456 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
461 snprintf( buf, sizeof( buf ), "%d", l );
462 slapi_entry_attr_set_charptr( e, type, buf );
463 #endif /* LDAP_SLAPI */
467 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
472 snprintf( buf, sizeof( buf ), "%u", l );
473 slapi_entry_attr_set_charptr( e, type, buf );
474 #endif /* LDAP_SLAPI */
478 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
483 snprintf( buf, sizeof( buf ), "%ld", l );
484 slapi_entry_attr_set_charptr( e, type, buf );
485 #endif /* LDAP_SLAPI */
489 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
494 snprintf( buf, sizeof( buf ), "%lu", l );
495 slapi_entry_attr_set_charptr( e, type, buf );
496 #endif /* LDAP_SLAPI */
500 slapi_is_rootdse( const char *dn )
503 return ( dn == NULL || dn[0] == '\0' );
510 * Add values to entry.
513 * LDAP_SUCCESS Values added to entry
514 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
515 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
518 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
524 char textbuf[SLAP_TEXT_BUFLEN];
526 mod.sm_op = LDAP_MOD_ADD;
528 mod.sm_type.bv_val = (char *)type;
529 mod.sm_type.bv_len = strlen( type );
531 rc = slap_str2ad( type, &mod.sm_desc, &text );
532 if ( rc != LDAP_SUCCESS ) {
536 if ( vals == NULL ) {
537 /* Apparently vals can be NULL
538 * FIXME: sm_bvalues = NULL ? */
539 mod.sm_bvalues = (BerVarray)ch_malloc( sizeof(struct berval) );
540 mod.sm_bvalues->bv_val = NULL;
543 rc = bvptr2obj( vals, &mod.sm_bvalues );
544 if ( rc != LDAP_SUCCESS ) {
545 return LDAP_CONSTRAINT_VIOLATION;
548 mod.sm_nvalues = NULL;
550 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
552 ch_free( mod.sm_bvalues );
554 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
557 #endif /* LDAP_SLAPI */
561 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
564 return slapi_entry_add_values( e, type, vals );
567 #endif /* LDAP_SLAPI */
571 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
574 AttributeDescription *ad = NULL;
578 rc = slap_str2ad( type, &ad, &text );
579 if ( rc != LDAP_SUCCESS ) {
583 return attr_merge_normalize( e, ad, *vs, NULL );
586 #endif /* LDAP_SLAPI */
590 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
596 char textbuf[SLAP_TEXT_BUFLEN];
598 mod.sm_op = LDAP_MOD_DELETE;
600 mod.sm_type.bv_val = (char *)type;
601 mod.sm_type.bv_len = strlen( type );
603 if ( vals == NULL ) {
604 /* If vals is NULL, this is a NOOP. */
608 rc = slap_str2ad( type, &mod.sm_desc, &text );
609 if ( rc != LDAP_SUCCESS ) {
613 if ( vals[0] == NULL ) {
614 /* SLAPI doco says LDAP_OPERATIONS_ERROR but LDAP_OTHER is better */
615 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
618 rc = bvptr2obj( vals, &mod.sm_bvalues );
619 if ( rc != LDAP_SUCCESS ) {
620 return LDAP_CONSTRAINT_VIOLATION;
622 mod.sm_nvalues = NULL;
624 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
626 ch_free( mod.sm_bvalues );
631 #endif /* LDAP_SLAPI */
635 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
638 return slapi_entry_delete_values( e, type, vals );
641 #endif /* LDAP_SLAPI */
645 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
648 return slapi_entry_attr_merge( e, (char *)type, vals );
651 #endif /* LDAP_SLAPI */
655 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
658 AttributeDescription *ad = NULL;
662 rc = slap_str2ad( type, &ad, &text );
663 if ( rc != LDAP_SUCCESS ) {
667 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
668 if ( rc != LDAP_SUCCESS ) {
675 #endif /* LDAP_SLAPI */
679 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
684 val.bv_val = (char *)value;
685 val.bv_len = strlen( value );
687 return slapi_entry_add_value( e, type, &val );
690 #endif /* LDAP_SLAPI */
694 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
697 Slapi_Value *vals[2];
700 val.bv_val = (char *)value;
701 val.bv_len = strlen( value );
705 return slapi_entry_delete_values_sv( e, type, vals );
708 #endif /* LDAP_SLAPI */
713 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
716 return slapi_entry_attr_merge( e, (char *)type, vals );
723 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
732 return ( *attr != NULL ) ? 0 : -1;
739 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
746 if ( prevattr == NULL ) {
750 *attr = prevattr->a_next;
752 return ( *attr != NULL ) ? 0 : -1;
759 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
762 AttributeDescription *ad = NULL;
767 rc = slap_str2ad( type, &ad, &text );
768 if ( rc != LDAP_SUCCESS ) {
772 attr_delete( &e->e_attrs, ad );
774 rc = bvptr2obj( vals, &bv );
775 if ( rc != LDAP_SUCCESS ) {
779 rc = attr_merge_normalize( e, ad, bv, NULL );
780 slapi_ch_free( (void **)&bv );
781 if ( rc != LDAP_SUCCESS ) {
788 #endif /* LDAP_SLAPI */
792 * FIXME -- The caller must free the allocated memory.
793 * In Netscape they do not have to.
796 slapi_attr_get_values(
798 struct berval ***vals )
804 if ( attr == NULL ) {
808 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
812 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
813 for ( j = 0; j < i; j++ ) {
814 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
818 *vals = (struct berval **)bv;
821 #else /* LDAP_SLAPI */
823 #endif /* LDAP_SLAPI */
827 slapi_dn_normalize( char *dn )
833 assert( dn != NULL );
836 bdn.bv_len = strlen( dn );
838 if ( dnPretty( NULL, &bdn, &pdn, NULL ) != LDAP_SUCCESS ) {
843 #else /* LDAP_SLAPI */
845 #endif /* LDAP_SLAPI */
849 slapi_dn_normalize_case( char *dn )
855 assert( dn != NULL );
858 bdn.bv_len = strlen( dn );
860 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
865 #else /* LDAP_SLAPI */
867 #endif /* LDAP_SLAPI */
876 struct berval bdn, ndn;
877 struct berval bsuffix, nsuffix;
880 assert( dn != NULL );
881 assert( suffix != NULL );
884 bdn.bv_len = strlen( dn );
886 bsuffix.bv_val = suffix;
887 bsuffix.bv_len = strlen( suffix );
889 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
893 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
896 slapi_ch_free( (void **)&ndn.bv_val );
900 rc = dnIsSuffix( &ndn, &nsuffix );
902 slapi_ch_free( (void **)&ndn.bv_val );
903 slapi_ch_free( (void **)&nsuffix.bv_val );
906 #else /* LDAP_SLAPI */
908 #endif /* LDAP_SLAPI */
913 const char *parentdn,
914 const char *childdn )
917 struct berval assertedParentDN, normalizedAssertedParentDN;
918 struct berval childDN, normalizedChildDN;
919 struct berval normalizedParentDN;
922 assert( parentdn != NULL );
923 assert( childdn != NULL );
925 assertedParentDN.bv_val = (char *)parentdn;
926 assertedParentDN.bv_len = strlen( parentdn );
928 if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
929 &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
934 childDN.bv_val = (char *)childdn;
935 childDN.bv_len = strlen( childdn );
937 if ( dnNormalize( 0, NULL, NULL, &childDN,
938 &normalizedChildDN, NULL ) != LDAP_SUCCESS )
940 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
944 dnParent( &normalizedChildDN, &normalizedParentDN );
946 if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
947 &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
952 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
953 slapi_ch_free( (void **)&normalizedChildDN.bv_val );
955 return ( match == 0 );
958 #endif /* LDAP_SLAPI */
962 * Returns DN of the parent entry, or NULL if the DN is
963 * an empty string or NULL, or has no parent.
966 slapi_dn_parent( const char *_dn )
969 struct berval dn, prettyDN;
970 struct berval parentDN;
976 dn.bv_val = (char *)_dn;
977 dn.bv_len = strlen( _dn );
979 if ( dn.bv_len == 0 ) {
983 if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
987 dnParent( &prettyDN, &parentDN ); /* in-place */
989 slapi_ch_free( (void **)&prettyDN.bv_val );
991 if ( parentDN.bv_len == 0 ) {
995 return slapi_ch_strdup( parentDN.bv_val );
998 #endif /* LDAP_SLAPI */
1002 * Returns DN of the parent entry; or NULL if the DN is
1003 * an empty string, if the DN has no parent, or if the
1004 * DN is the suffix of the backend database
1006 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *_dn )
1010 struct berval dn, prettyDN;
1011 struct berval normalizedDN, parentDN;
1013 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
1016 dn.bv_val = (char *)_dn;
1017 dn.bv_len = strlen( _dn );
1019 if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
1023 if ( be != NULL && be_issuffix( be, &normalizedDN ) ) {
1024 slapi_ch_free( (void **)&prettyDN.bv_val );
1025 slapi_ch_free( (void **)&normalizedDN.bv_val );
1029 dnParent( &prettyDN, &parentDN );
1031 slapi_ch_free( (void **)&prettyDN.bv_val );
1032 slapi_ch_free( (void **)&normalizedDN.bv_val );
1034 if ( parentDN.bv_len == 0 ) {
1038 return slapi_ch_strdup( parentDN.bv_val );
1041 #endif /* LDAP_SLAPI */
1045 slapi_dn_ignore_case( char *dn )
1048 return slapi_dn_normalize_case( dn );
1049 #else /* LDAP_SLAPI */
1051 #endif /* LDAP_SLAPI */
1055 slapi_ch_malloc( unsigned long size )
1058 return ch_malloc( size );
1059 #else /* LDAP_SLAPI */
1061 #endif /* LDAP_SLAPI */
1065 slapi_ch_free( void **ptr )
1070 #endif /* LDAP_SLAPI */
1074 slapi_ch_free_string( char **ptr )
1077 slapi_ch_free( (void **)ptr );
1078 #endif /* LDAP_SLAPI */
1082 slapi_ch_array_free( char **arrayp )
1087 if ( arrayp != NULL ) {
1088 for ( p = arrayp; *p != NULL; p++ ) {
1089 slapi_ch_free( (void **)p );
1091 slapi_ch_free( (void **)&arrayp );
1097 slapi_ch_bvdup(const struct berval *v)
1102 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
1103 bv->bv_len = v->bv_len;
1104 bv->bv_val = slapi_ch_malloc( bv->bv_len );
1105 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1114 slapi_ch_bvecdup(const struct berval **v)
1124 for ( i = 0; v[i] != NULL; i++ )
1127 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1129 for ( i = 0; v[i] != NULL; i++ ) {
1130 rv[i] = slapi_ch_bvdup( v[i] );
1142 unsigned long nelem,
1143 unsigned long size )
1146 return ch_calloc( nelem, size );
1147 #else /* LDAP_SLAPI */
1149 #endif /* LDAP_SLAPI */
1155 unsigned long size )
1158 return ch_realloc( block, size );
1159 #else /* LDAP_SLAPI */
1161 #endif /* LDAP_SLAPI */
1165 slapi_ch_strdup( char *s )
1168 return ch_strdup( (const char *)s );
1169 #else /* LDAP_SLAPI */
1171 #endif /* LDAP_SLAPI */
1175 slapi_ch_stlen( char *s )
1178 return strlen( (const char *)s );
1179 #else /* LDAP_SLAPI */
1181 #endif /* LDAP_SLAPI */
1185 slapi_control_present(
1186 LDAPControl **controls,
1188 struct berval **val,
1203 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1204 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1209 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1211 * FIXME: according to 6.1 specification,
1212 * "The val output parameter is set
1213 * to point into the controls array.
1214 * A copy of the control value is
1218 struct berval *pTmpBval;
1220 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
1221 if ( pTmpBval == NULL ) {
1224 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
1225 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
1229 slapi_ch_free( (void **)&pTmpBval );
1235 *val = &controls[i]->ldctl_value;
1240 *iscritical = controls[i]->ldctl_iscritical;
1247 #else /* LDAP_SLAPI */
1249 #endif /* LDAP_SLAPI */
1254 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1255 unsigned long *slapi_mask)
1257 *slapi_mask = SLAPI_OPERATION_NONE;
1259 if ( slap_mask & SLAP_CTRL_ABANDON )
1260 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1262 if ( slap_mask & SLAP_CTRL_ADD )
1263 *slapi_mask |= SLAPI_OPERATION_ADD;
1265 if ( slap_mask & SLAP_CTRL_BIND )
1266 *slapi_mask |= SLAPI_OPERATION_BIND;
1268 if ( slap_mask & SLAP_CTRL_COMPARE )
1269 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1271 if ( slap_mask & SLAP_CTRL_DELETE )
1272 *slapi_mask |= SLAPI_OPERATION_DELETE;
1274 if ( slap_mask & SLAP_CTRL_MODIFY )
1275 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1277 if ( slap_mask & SLAP_CTRL_RENAME )
1278 *slapi_mask |= SLAPI_OPERATION_MODDN;
1280 if ( slap_mask & SLAP_CTRL_SEARCH )
1281 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1283 if ( slap_mask & SLAP_CTRL_UNBIND )
1284 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1288 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1289 slap_mask_t *slap_mask)
1293 if ( slapi_mask & SLAPI_OPERATION_BIND )
1294 *slap_mask |= SLAP_CTRL_BIND;
1296 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1297 *slap_mask |= SLAP_CTRL_UNBIND;
1299 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1300 *slap_mask |= SLAP_CTRL_SEARCH;
1302 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1303 *slap_mask |= SLAP_CTRL_MODIFY;
1305 if ( slapi_mask & SLAPI_OPERATION_ADD )
1306 *slap_mask |= SLAP_CTRL_ADD;
1308 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1309 *slap_mask |= SLAP_CTRL_DELETE;
1311 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1312 *slap_mask |= SLAP_CTRL_RENAME;
1314 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1315 *slap_mask |= SLAP_CTRL_COMPARE;
1317 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1318 *slap_mask |= SLAP_CTRL_ABANDON;
1320 *slap_mask |= SLAP_CTRL_FRONTEND;
1329 /* Plugins must deal with controls themselves. */
1331 return LDAP_SUCCESS;
1333 #endif /* LDAP_SLAPI */
1336 slapi_register_supported_control(
1338 unsigned long controlops )
1341 slap_mask_t controlmask;
1343 slapiControlOp2SlapControlMask( controlops, &controlmask );
1345 register_supported_control( controloid, controlmask, NULL, parseSlapiControl );
1346 #endif /* LDAP_SLAPI */
1350 slapi_get_supported_controls(
1352 unsigned long **ctrlopsp )
1357 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1358 if ( rc != LDAP_SUCCESS ) {
1362 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1363 /* In place, naughty. */
1364 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1367 return LDAP_SUCCESS;
1368 #else /* LDAP_SLAPI */
1370 #endif /* LDAP_SLAPI */
1374 slapi_dup_control( LDAPControl *ctrl )
1379 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1380 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1381 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1382 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1387 #endif /* LDAP_SLAPI */
1391 slapi_register_supported_saslmechanism( char *mechanism )
1394 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1395 slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1396 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1397 #endif /* LDAP_SLAPI */
1401 slapi_get_supported_saslmechanisms( void )
1404 /* FIXME -- can not get the saslmechanism without a connection. */
1405 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1406 "can not get the SASL mechanism list "
1407 "without a connection\n" );
1409 #else /* LDAP_SLAPI */
1411 #endif /* LDAP_SLAPI */
1415 slapi_get_supported_extended_ops( void )
1419 char **ppExtOpOID = NULL;
1422 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1426 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
1431 if ( numExtOps == 0 ) {
1435 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1436 for ( k = 0; k < i; k++ ) {
1439 bv = get_supported_extop( k );
1440 assert( bv != NULL );
1442 ppExtOpOID[ k ] = bv->bv_val;
1445 for ( ; k < j; k++ ) {
1448 bv = ns_get_supported_extop( k );
1449 assert( bv != NULL );
1451 ppExtOpOID[ i + k ] = bv->bv_val;
1453 ppExtOpOID[ i + k ] = NULL;
1456 #else /* LDAP_SLAPI */
1458 #endif /* LDAP_SLAPI */
1462 slapi_send_ldap_result(
1468 struct berval **urls )
1473 char *extOID = NULL;
1474 struct berval *extValue = NULL;
1476 SlapReply rs = { REP_RESULT };
1478 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1481 rs.sr_matched = matched;
1486 slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
1488 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1489 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
1490 send_ldap_sasl( op, &rs );
1494 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1495 if ( extOID != NULL ) {
1496 rs.sr_rspoid = extOID;
1497 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
1498 send_ldap_extended( op, &rs );
1502 if (op->o_tag == LDAP_REQ_SEARCH)
1503 rs.sr_nentries = nentries;
1505 send_ldap_result( op, &rs );
1506 #endif /* LDAP_SLAPI */
1510 slapi_send_ldap_search_entry(
1513 LDAPControl **ectrls,
1519 SlapReply rs = { REP_RESULT };
1521 AttributeName *an = NULL;
1524 if ( attrs != NULL ) {
1525 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1533 an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
1534 for ( i = 0; attrs[i] != NULL; i++ ) {
1535 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1536 an[i].an_name.bv_len = strlen( attrs[i] );
1537 an[i].an_desc = NULL;
1538 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1541 an[i].an_name.bv_len = 0;
1542 an[i].an_name.bv_val = NULL;
1545 rs.sr_err = LDAP_SUCCESS;
1546 rs.sr_matched = NULL;
1549 rs.sr_ctrls = ectrls;
1554 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1558 return send_search_entry( pOp, &rs );
1559 #else /* LDAP_SLAPI */
1561 #endif /* LDAP_SLAPI */
1565 slapi_send_ldap_search_reference(
1568 struct berval **references,
1569 LDAPControl **ectrls,
1570 struct berval **v2refs
1575 SlapReply rs = { REP_SEARCHREF };
1578 rs.sr_err = LDAP_SUCCESS;
1579 rs.sr_matched = NULL;
1582 rc = bvptr2obj( references, &rs.sr_ref );
1583 if ( rc != LDAP_SUCCESS ) {
1587 rs.sr_ctrls = ectrls;
1591 if ( v2refs != NULL ) {
1592 rc = bvptr2obj( v2refs, &rs.sr_v2ref );
1593 if ( rc != LDAP_SUCCESS ) {
1594 slapi_ch_free( (void **)&rs.sr_ref );
1601 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1605 rc = send_search_reference( pOp, &rs );
1607 if ( rs.sr_ref != NULL )
1608 slapi_ch_free( (void **)&rs.sr_ref );
1610 if ( rs.sr_v2ref != NULL )
1611 slapi_ch_free( (void **)&rs.sr_v2ref );
1616 #endif /* LDAP_SLAPI */
1620 slapi_str2filter( char *str )
1623 return str2filter( str );
1624 #else /* LDAP_SLAPI */
1626 #endif /* LDAP_SLAPI */
1636 #endif /* LDAP_SLAPI */
1640 slapi_filter_dup( Slapi_Filter *filter )
1645 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1647 f->f_choice = filter->f_choice;
1649 switch ( f->f_choice ) {
1650 case LDAP_FILTER_AND:
1651 case LDAP_FILTER_NOT:
1652 case LDAP_FILTER_OR: {
1653 Filter *pFilter, **ppF;
1655 for ( pFilter = filter->f_list, ppF = &f->f_list;
1657 pFilter = pFilter->f_next, ppF = &f->f_next )
1659 *ppF = slapi_filter_dup( pFilter );
1665 case LDAP_FILTER_PRESENT:
1666 f->f_desc = filter->f_desc;
1668 case LDAP_FILTER_EQUALITY:
1669 case LDAP_FILTER_GE:
1670 case LDAP_FILTER_LE:
1671 case LDAP_FILTER_APPROX:
1672 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1673 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1674 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1676 case LDAP_FILTER_EXT:
1677 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1678 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1679 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1680 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1681 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1682 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1684 case LDAP_FILTER_SUBSTRINGS: {
1687 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1688 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1689 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1690 if ( filter->f_sub_any != NULL ) {
1691 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1693 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1694 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1695 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1697 f->f_sub_any[i].bv_val = NULL;
1699 f->f_sub_any = NULL;
1701 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1704 case SLAPD_FILTER_COMPUTED:
1705 f->f_result = filter->f_result;
1708 slapi_ch_free( (void **)&f );
1716 #endif /* LDAP_SLAPI */
1720 slapi_filter_get_choice( Slapi_Filter *f )
1732 #else /* LDAP_SLAPI */
1733 return -1; /* invalid filter type */
1734 #endif /* LDAP_SLAPI */
1738 slapi_filter_get_ava(
1741 struct berval **bval )
1745 int rc = LDAP_SUCCESS;
1747 assert( type != NULL );
1748 assert( bval != NULL );
1753 ftype = f->f_choice;
1754 if ( ftype == LDAP_FILTER_EQUALITY
1755 || ftype == LDAP_FILTER_GE
1756 || ftype == LDAP_FILTER_LE
1757 || ftype == LDAP_FILTER_APPROX ) {
1759 * According to the SLAPI Reference Manual these are
1762 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1763 *bval = &f->f_un.f_un_ava->aa_value;
1764 } else { /* filter type not supported */
1769 #else /* LDAP_SLAPI */
1771 #endif /* LDAP_SLAPI */
1775 slapi_filter_list_first( Slapi_Filter *f )
1784 ftype = f->f_choice;
1785 if ( ftype == LDAP_FILTER_AND
1786 || ftype == LDAP_FILTER_OR
1787 || ftype == LDAP_FILTER_NOT ) {
1788 return (Slapi_Filter *)f->f_list;
1792 #else /* LDAP_SLAPI */
1794 #endif /* LDAP_SLAPI */
1798 slapi_filter_list_next(
1800 Slapi_Filter *fprev )
1809 ftype = f->f_choice;
1810 if ( ftype == LDAP_FILTER_AND
1811 || ftype == LDAP_FILTER_OR
1812 || ftype == LDAP_FILTER_NOT )
1814 return fprev->f_next;
1818 #else /* LDAP_SLAPI */
1820 #endif /* LDAP_SLAPI */
1824 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1831 switch ( f->f_choice ) {
1832 case LDAP_FILTER_GE:
1833 case LDAP_FILTER_LE:
1834 case LDAP_FILTER_EQUALITY:
1835 case LDAP_FILTER_APPROX:
1836 *type = f->f_av_desc->ad_cname.bv_val;
1838 case LDAP_FILTER_SUBSTRINGS:
1839 *type = f->f_sub_desc->ad_cname.bv_val;
1841 case LDAP_FILTER_PRESENT:
1842 *type = f->f_desc->ad_cname.bv_val;
1844 case LDAP_FILTER_EXT:
1845 *type = f->f_mr_desc->ad_cname.bv_val;
1848 /* Complex filters need not apply. */
1856 #endif /* LDAP_SLAPI */
1860 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1861 char ***any, char **final )
1866 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1871 * The caller shouldn't free but we can't return an
1872 * array of char *s from an array of bervals without
1873 * allocating memory, so we may as well be consistent.
1876 *type = f->f_sub_desc->ad_cname.bv_val;
1877 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1878 if ( f->f_sub_any != NULL ) {
1879 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1881 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1882 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1883 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1889 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1894 #endif /* LDAP_SLAPI */
1898 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1901 Slapi_Filter *f = NULL;
1903 if ( ftype == LDAP_FILTER_AND ||
1904 ftype == LDAP_FILTER_OR ||
1905 ftype == LDAP_FILTER_NOT )
1907 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1908 f->f_choice = ftype;
1910 f->f_list->f_next = f2;
1917 #endif /* LDAP_SLAPI */
1921 slapi_x_filter_append( int ftype,
1922 Slapi_Filter **pContainingFilter, /* NULL on first call */
1923 Slapi_Filter **pNextFilter,
1924 Slapi_Filter *filterToAppend )
1927 if ( ftype == LDAP_FILTER_AND ||
1928 ftype == LDAP_FILTER_OR ||
1929 ftype == LDAP_FILTER_NOT )
1931 if ( *pContainingFilter == NULL ) {
1932 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
1933 (*pContainingFilter)->f_choice = ftype;
1934 (*pContainingFilter)->f_list = filterToAppend;
1935 (*pContainingFilter)->f_next = NULL;
1937 if ( (*pContainingFilter)->f_choice != ftype ) {
1941 (*pNextFilter)->f_next = filterToAppend;
1943 *pNextFilter = filterToAppend;
1947 #endif /* LDAP_SLAPI */
1952 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1960 /* spec says return zero if no filter. */
1964 if ( verify_access ) {
1965 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
1967 return LDAP_PARAM_ERROR;
1973 * According to acl.c it is safe to call test_filter() with
1976 rc = test_filter( op, e, f );
1978 case LDAP_COMPARE_TRUE:
1981 case LDAP_COMPARE_FALSE:
1983 case SLAPD_COMPARE_UNDEFINED:
1986 case LDAP_PROTOCOL_ERROR:
1987 /* filter type unknown: spec says return -1 */
1995 #endif /* LDAP_SLAPI */
1999 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
2002 return slapi_filter_test( NULL, e, f, 0 );
2009 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
2012 switch ( f->f_choice ) {
2013 case LDAP_FILTER_AND:
2014 case LDAP_FILTER_NOT:
2015 case LDAP_FILTER_OR: {
2019 * FIXME: altering f; should we use a temporary?
2021 for ( f = f->f_list; f != NULL; f = f->f_next ) {
2022 rc = slapi_filter_apply( f, fn, arg, error_code );
2026 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
2032 case LDAP_FILTER_EQUALITY:
2033 case LDAP_FILTER_SUBSTRINGS:
2034 case LDAP_FILTER_GE:
2035 case LDAP_FILTER_LE:
2036 case LDAP_FILTER_PRESENT:
2037 case LDAP_FILTER_APPROX:
2038 case LDAP_FILTER_EXT:
2039 *error_code = fn( f, arg );
2042 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2045 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
2046 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
2052 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2054 #endif /* LDAP_SLAPI */
2058 slapi_send_ldap_extended_response(
2063 struct berval *response )
2068 rs.sr_err = errornum;
2069 rs.sr_matched = NULL;
2073 rs.sr_rspoid = respName;
2074 rs.sr_rspdata = response;
2076 send_ldap_extended( op, &rs );
2078 return LDAP_SUCCESS;
2079 #else /* LDAP_SLAPI */
2081 #endif /* LDAP_SLAPI */
2086 struct berval **vals,
2091 * FIXME: what's the point?
2094 #else /* LDAP_SLAPI */
2096 #endif /* LDAP_SLAPI */
2099 #define MAX_HOSTNAME 512
2102 slapi_get_hostname( void )
2106 static int been_here = 0;
2107 static char *static_hn = NULL;
2109 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
2111 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
2112 if ( static_hn == NULL) {
2113 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
2114 "Cannot allocate memory for hostname\n" );
2116 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2121 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
2122 slapi_log_error( SLAPI_LOG_FATAL,
2124 "can't get hostname\n" );
2125 slapi_ch_free( (void **)&static_hn );
2127 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2136 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2138 hn = ch_strdup( static_hn );
2141 #else /* LDAP_SLAPI */
2143 #endif /* LDAP_SLAPI */
2147 * FIXME: this should go in an appropriate header ...
2149 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
2159 int rc = LDAP_SUCCESS;
2162 va_start( arglist, fmt );
2163 rc = vLogError( severity, subsystem, fmt, arglist );
2167 #else /* LDAP_SLAPI */
2169 #endif /* LDAP_SLAPI */
2174 slapi_timer_current_time( void )
2177 static int first_time = 1;
2178 #if !defined (_WIN32)
2182 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
2185 gettimeofday( &base_time, NULL );
2187 gettimeofday( &now, NULL );
2188 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
2189 (now.tv_usec - base_time.tv_usec);
2190 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
2196 return (slap_get_time() - starttime) * 1000000;
2203 performance_counter_present = QueryPerformanceCounter( &base_time );
2204 QueryPerformanceFrequency( &performance_freq );
2207 if ( !performance_counter_present )
2210 QueryPerformanceCounter( &now );
2211 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2213 #else /* LDAP_SLAPI */
2215 #endif /* LDAP_SLAPI */
2222 slapi_timer_get_time( char *label )
2225 unsigned long start = slapi_timer_current_time();
2226 printf("%10ld %10ld usec %s\n", start, 0, label);
2228 #else /* LDAP_SLAPI */
2230 #endif /* LDAP_SLAPI */
2237 slapi_timer_elapsed_time(
2239 unsigned long start )
2242 unsigned long stop = slapi_timer_current_time();
2243 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2244 #endif /* LDAP_SLAPI */
2248 slapi_free_search_results_internal( Slapi_PBlock *pb )
2251 Slapi_Entry **entries;
2252 int k = 0, nEnt = 0;
2254 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2255 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2260 if ( entries == NULL ) {
2264 for ( k = 0; k < nEnt; k++ ) {
2265 slapi_entry_free( entries[k] );
2268 slapi_ch_free( (void **)&entries );
2269 #endif /* LDAP_SLAPI */
2274 * Internal API to prime a Slapi_PBlock with a Backend.
2276 static int initBackendPB( Slapi_PBlock *pb, Backend *be )
2280 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2281 if ( rc != LDAP_SUCCESS )
2285 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2286 if ( rc != LDAP_SUCCESS )
2290 return LDAP_SUCCESS;
2294 * If oldStyle is TRUE, then a value suitable for setting to
2295 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
2296 * (pointer to static storage).
2298 * If oldStyle is FALSE, then a value suitable for setting to
2299 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2300 * a pointer to allocated memory and will include the SASL
2301 * mechanism (if any).
2303 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2308 switch ( authz->sai_method ) {
2309 case LDAP_AUTH_SASL:
2311 authType = SLAPD_AUTH_SASL;
2313 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2314 authType = slapi_ch_malloc( len );
2315 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2318 case LDAP_AUTH_SIMPLE:
2319 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2321 case LDAP_AUTH_NONE:
2322 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2328 if ( is_tls && authType == NULL ) {
2329 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2336 * Internal API to prime a Slapi_PBlock with a Connection.
2338 static int initConnectionPB( Slapi_PBlock *pb, Connection *conn )
2343 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2344 if ( rc != LDAP_SUCCESS )
2347 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2348 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2349 if ( rc != LDAP_SUCCESS )
2351 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2352 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2353 if ( rc != LDAP_SUCCESS )
2357 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2358 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2359 if ( rc != LDAP_SUCCESS )
2361 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2362 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2363 if ( rc != LDAP_SUCCESS )
2367 #ifdef LDAP_CONNECTIONLESS
2368 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2369 if ( rc != LDAP_SUCCESS )
2373 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2374 if ( rc != LDAP_SUCCESS )
2377 /* Returns pointer to static string */
2378 connAuthType = Authorization2AuthType( &conn->c_authz,
2385 if ( connAuthType != NULL ) {
2386 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2387 if ( rc != LDAP_SUCCESS )
2391 /* Returns pointer to allocated string */
2392 connAuthType = Authorization2AuthType( &conn->c_authz,
2399 if ( connAuthType != NULL ) {
2400 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2401 /* slapi_pblock_set dups this itself */
2402 slapi_ch_free( (void **)&connAuthType );
2403 if ( rc != LDAP_SUCCESS )
2407 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
2408 /* slapi_pblock_set dups this itself */
2409 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)conn->c_authz.sai_dn.bv_val);
2410 if ( rc != LDAP_SUCCESS )
2414 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SSF, (void *)conn->c_ssf);
2415 if ( rc != LDAP_SUCCESS )
2418 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SASL_CONTEXT,
2419 ( conn->c_sasl_authctx != NULL ? conn->c_sasl_authctx :
2420 conn->c_sasl_sockctx ) );
2421 if ( rc != LDAP_SUCCESS )
2426 #endif /* LDAP_SLAPI */
2429 * Internal API to prime a Slapi_PBlock with an Operation.
2431 int slapi_x_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
2439 if ( op->o_bd != NULL ) {
2440 isRoot = be_isroot( op->o_bd, &op->o_ndn );
2441 isUpdateDn = be_isupdate( op->o_bd, &op->o_ndn );
2444 rc = initBackendPB( pb, op->o_bd );
2445 if ( rc != LDAP_SUCCESS )
2448 rc = initConnectionPB( pb, op->o_conn );
2449 if ( rc != LDAP_SUCCESS )
2452 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2453 if ( rc != LDAP_SUCCESS )
2456 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2457 if ( rc != LDAP_SUCCESS )
2460 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2461 if ( rc != LDAP_SUCCESS )
2464 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2465 if ( rc != LDAP_SUCCESS )
2468 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2469 if ( rc != LDAP_SUCCESS )
2472 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2473 if ( rc != LDAP_SUCCESS )
2476 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2477 if ( rc != LDAP_SUCCESS)
2480 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2481 if ( rc != LDAP_SUCCESS )
2484 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2485 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
2486 /* Not quite sure what the point of this is. */
2487 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
2488 if ( rc != LDAP_SUCCESS )
2492 return LDAP_SUCCESS;
2498 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2503 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2505 *isSSL = conn->c_is_tls;
2510 return LDAP_SUCCESS;
2513 #endif /* LDAP_SLAPI */
2517 * DS 5.x compatability API follow
2520 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2526 return LDAP_PARAM_ERROR;
2528 at = attr->a_desc->ad_type;
2530 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2532 if ( is_at_single_value( at ) )
2533 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2534 if ( is_at_operational( at ) )
2535 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2536 if ( is_at_obsolete( at ) )
2537 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2538 if ( is_at_collective( at ) )
2539 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2540 if ( is_at_no_user_mod( at ) )
2541 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2543 return LDAP_SUCCESS;
2546 #endif /* LDAP_SLAPI */
2549 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2552 unsigned long flags;
2554 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2556 return (flags & flag) ? 1 : 0;
2559 #endif /* LDAP_SLAPI */
2562 Slapi_Attr *slapi_attr_new( void )
2567 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2575 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2579 AttributeDescription *ad = NULL;
2581 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2597 void slapi_attr_free( Slapi_Attr **a )
2605 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2608 return attr_dup( (Slapi_Attr *)attr );
2614 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2618 * FIXME: here we may lose alignment between a_vals/a_nvals
2620 return value_add_one( &a->a_vals, (Slapi_Value *)v );
2626 int slapi_attr_type2plugin( const char *type, void **pi )
2633 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2636 if ( attr == NULL ) {
2637 return LDAP_PARAM_ERROR;
2640 *type = attr->a_desc->ad_cname.bv_val;
2642 return LDAP_SUCCESS;
2648 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2651 if ( attr == NULL ) {
2652 return LDAP_PARAM_ERROR;
2654 *oidp = attr->a_desc->ad_type->sat_oid;
2656 return LDAP_SUCCESS;
2662 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2670 mr = a->a_desc->ad_type->sat_equality;
2671 rc = value_match( &ret, a->a_desc, mr,
2672 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2673 (struct berval *)v1, (void *)v2, &text );
2674 if ( rc != LDAP_SUCCESS )
2677 return ( ret == 0 ) ? 0 : -1;
2683 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2693 if ( a ->a_vals == NULL ) {
2696 mr = a->a_desc->ad_type->sat_equality;
2697 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2698 rc = value_match( &ret, a->a_desc, mr,
2699 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2700 if ( rc != LDAP_SUCCESS ) {
2707 #endif /* LDAP_SLAPI */
2711 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2714 AttributeDescription *a1 = NULL;
2715 AttributeDescription *a2 = NULL;
2719 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2723 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2727 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2728 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2729 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2732 case SLAPI_TYPE_CMP_EXACT:
2733 ret = ad_cmp( a1, a2 );
2735 case SLAPI_TYPE_CMP_BASE:
2736 ret = ad_base_cmp( a1, a2 );
2738 case SLAPI_TYPE_CMP_SUBTYPE:
2739 ret = is_ad_subtype( a2, a2 );
2752 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2755 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2761 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2764 return slapi_valueset_first_value( &a->a_vals, v );
2770 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2773 return slapi_valueset_next_value( &a->a_vals, hint, v );
2779 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2782 *numValues = slapi_valueset_count( &a->a_vals );
2790 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2793 *vs = &((Slapi_Attr *)a)->a_vals;
2801 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2804 return slapi_attr_get_values( a, vals );
2810 char *slapi_attr_syntax_normalize( const char *s )
2813 AttributeDescription *ad = NULL;
2816 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2820 return ad->ad_cname.bv_val;
2826 Slapi_Value *slapi_value_new( void )
2831 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2839 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2842 return ber_dupbv( NULL, (struct berval *)bval );
2848 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2851 return slapi_value_new_berval( v );
2857 Slapi_Value *slapi_value_new_string(const char *s)
2862 bv.bv_val = (char *)s;
2863 bv.bv_len = strlen( s );
2865 return slapi_value_new_berval( &bv );
2871 Slapi_Value *slapi_value_init(Slapi_Value *val)
2883 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2886 return ber_dupbv( v, bval );
2892 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2895 v->bv_val = slapi_ch_strdup( (char *)s );
2896 v->bv_len = strlen( s );
2904 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2907 return slapi_value_new_value( v );
2913 void slapi_value_free(Slapi_Value **value)
2916 if ( value == NULL ) {
2920 if ( (*value) != NULL ) {
2921 slapi_ch_free( (void **)&(*value)->bv_val );
2922 slapi_ch_free( (void **)value );
2927 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2936 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2939 if ( value == NULL ) {
2942 if ( value->bv_val != NULL ) {
2943 slapi_ch_free( (void **)&value->bv_val );
2945 slapi_value_init_berval( value, (struct berval *)bval );
2953 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2956 if ( value == NULL ) {
2959 return slapi_value_set_berval( value, vfrom );
2965 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2968 if ( value == NULL ) {
2971 if ( value->bv_val != NULL ) {
2972 slapi_ch_free( (void **)&value->bv_val );
2974 value->bv_val = slapi_ch_malloc( len );
2975 value->bv_len = len;
2976 AC_MEMCPY( value->bv_val, val, len );
2984 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2987 if ( value == NULL ) {
2990 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2997 int slapi_value_set_int(Slapi_Value *value, int intVal)
3002 snprintf( buf, sizeof( buf ), "%d", intVal );
3004 return slapi_value_set_string( value, buf );
3010 const char *slapi_value_get_string(const Slapi_Value *value)
3013 if ( value == NULL ) {
3016 return value->bv_val;
3023 static int checkBVString(const struct berval *bv)
3027 for ( i = 0; i < bv->bv_len; i++ ) {
3028 if ( bv->bv_val[i] == '\0' )
3031 if ( bv->bv_val[i] != '\0' )
3038 int slapi_value_get_int(const Slapi_Value *value)
3041 if ( value == NULL ) return 0;
3042 if ( value->bv_val == NULL ) return 0;
3043 if ( !checkBVString( value ) ) return 0;
3045 return (int)strtol( value->bv_val, NULL, 10 );
3051 unsigned int slapi_value_get_uint(const Slapi_Value *value)
3054 if ( value == NULL ) return 0;
3055 if ( value->bv_val == NULL ) return 0;
3056 if ( !checkBVString( value ) ) return 0;
3058 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
3064 long slapi_value_get_long(const Slapi_Value *value)
3067 if ( value == NULL ) return 0;
3068 if ( value->bv_val == NULL ) return 0;
3069 if ( !checkBVString( value ) ) return 0;
3071 return strtol( value->bv_val, NULL, 10 );
3077 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
3080 if ( value == NULL ) return 0;
3081 if ( value->bv_val == NULL ) return 0;
3082 if ( !checkBVString( value ) ) return 0;
3084 return strtoul( value->bv_val, NULL, 10 );
3090 size_t slapi_value_get_length(const Slapi_Value *value)
3093 if ( value == NULL )
3096 return (size_t) value->bv_len;
3102 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
3105 return slapi_attr_value_cmp( a, v1, v2 );
3111 /* A ValueSet is a container for a BerVarray. */
3112 Slapi_ValueSet *slapi_valueset_new( void )
3117 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
3126 void slapi_valueset_free(Slapi_ValueSet *vs)
3132 ber_bvarray_free( vp );
3133 slapi_ch_free( (void **)&vp );
3140 void slapi_valueset_init(Slapi_ValueSet *vs)
3143 if ( vs != NULL && *vs == NULL ) {
3144 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
3145 (*vs)->bv_val = NULL;
3151 void slapi_valueset_done(Slapi_ValueSet *vs)
3159 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
3161 slapi_ch_free( (void **)&vp->bv_val );
3163 /* but don't free *vs or vs */
3167 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
3172 ber_dupbv( &bv, (Slapi_Value *)addval );
3173 ber_bvarray_add( vs, &bv );
3177 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
3180 return slapi_valueset_next_value( vs, 0, v );
3186 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
3197 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
3208 int slapi_valueset_count( const Slapi_ValueSet *vs )
3219 for ( i = 0; vp[i].bv_val != NULL; i++ )
3229 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
3234 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
3235 slapi_valueset_add_value( vs1, vp );
3240 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
3241 struct berval *val, int access )
3248 slap_access_t slap_access;
3249 AttributeDescription *ad = NULL;
3252 ret = slap_str2ad( attr, &ad, &text );
3253 if ( ret != LDAP_SUCCESS ) {
3257 switch ( access & SLAPI_ACL_ALL ) {
3258 case SLAPI_ACL_COMPARE:
3259 slap_access = ACL_COMPARE;
3261 case SLAPI_ACL_SEARCH:
3262 slap_access = ACL_SEARCH;
3264 case SLAPI_ACL_READ:
3265 slap_access = ACL_READ;
3267 case SLAPI_ACL_WRITE:
3268 case SLAPI_ACL_DELETE:
3270 case SLAPI_ACL_SELF:
3271 slap_access = ACL_WRITE;
3274 return LDAP_INSUFFICIENT_ACCESS;
3278 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3279 return LDAP_PARAM_ERROR;
3282 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3283 return LDAP_PARAM_ERROR;
3286 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3287 return LDAP_PARAM_ERROR;
3290 ret = access_allowed( op, e, ad, val, slap_access, NULL );
3292 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3294 return LDAP_UNWILLING_TO_PERFORM;
3298 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
3302 int rc = LDAP_SUCCESS;
3303 Modifications *ml, *mp;
3305 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3306 return LDAP_PARAM_ERROR;
3309 ml = slapi_x_ldapmods2modifications( mods );
3314 for ( mp = ml; mp != NULL; mp = mp->sml_next ) {
3315 rc = slap_bv2ad( &mp->sml_type, &mp->sml_desc, (const char **)errbuf );
3316 if ( rc != LDAP_SUCCESS ) {
3321 if ( rc == LDAP_SUCCESS ) {
3322 rc = acl_check_modlist( op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3325 /* Careful when freeing the modlist because it has pointers into the mods array. */
3326 for ( ; ml != NULL; ml = mp ) {
3329 /* just free the containing array */
3330 slapi_ch_free( (void **)&ml->sml_bvalues );
3331 slapi_ch_free( (void **)&ml );
3336 return LDAP_UNWILLING_TO_PERFORM;
3341 * Synthesise an LDAPMod array from a Modifications list to pass
3342 * to SLAPI. This synthesis is destructive and as such the
3343 * Modifications list may not be used after calling this
3346 * This function must also be called before slap_mods_check().
3348 LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
3351 Modifications *ml, *modlist;
3352 LDAPMod **mods, *modp;
3355 modlist = *pmodlist;
3357 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3360 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3362 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3363 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3365 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3367 /* Take ownership of original type. */
3368 modp->mod_type = ml->sml_type.bv_val;
3369 ml->sml_type.bv_val = NULL;
3371 if ( ml->sml_bvalues != NULL ) {
3372 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
3374 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3375 sizeof(struct berval *) );
3376 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
3377 /* Take ownership of original values. */
3378 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3379 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
3380 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
3381 ml->sml_bvalues[j].bv_len = 0;
3382 ml->sml_bvalues[j].bv_val = NULL;
3384 modp->mod_bvalues[j] = NULL;
3386 modp->mod_bvalues = NULL;
3393 slap_mods_free( modlist );
3403 * Convert a potentially modified array of LDAPMods back to a
3404 * Modification list.
3406 * The returned Modification list contains pointers into the
3407 * LDAPMods array; the latter MUST be freed with
3408 * slapi_x_free_ldapmods() (see below).
3410 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
3413 Modifications *modlist = NULL, **modtail;
3418 for( modp = mods; *modp != NULL; modp++ ) {
3422 struct berval **bvp;
3424 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3425 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3426 mod->sml_type.bv_val = (*modp)->mod_type;
3427 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3428 mod->sml_desc = NULL;
3429 mod->sml_next = NULL;
3431 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3432 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3435 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3440 mod->sml_bvalues = NULL;
3442 mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3444 /* NB: This implicitly trusts a plugin to return valid modifications. */
3445 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3446 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3447 mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
3448 mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
3451 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3452 mod->sml_bvalues[i].bv_val = *p;
3453 mod->sml_bvalues[i].bv_len = strlen( *p );
3456 mod->sml_bvalues[i].bv_val = NULL;
3458 mod->sml_nvalues = NULL;
3461 modtail = &mod->sml_next;
3471 * This function only frees the parts of the mods array that
3472 * are not shared with the Modification list that was created
3473 * by slapi_x_ldapmods2modifications().
3476 void slapi_x_free_ldapmods (LDAPMod **mods)
3484 for ( i = 0; mods[i] != NULL; i++ ) {
3486 * Don't free values themselves; they're owned by the
3487 * Modification list. Do free the containing array.
3489 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3490 for ( j = 0; mods[i]->mod_bvalues != NULL && mods[i]->mod_bvalues[j] != NULL; j++ ) {
3491 ch_free( mods[i]->mod_bvalues[j] );
3493 ch_free( mods[i]->mod_bvalues );
3495 ch_free( mods[i]->mod_values );
3497 /* Don't free type, for same reasons. */
3501 #endif /* LDAP_SLAPI */
3505 * Sun ONE DS 5.x computed attribute support. Computed attributes
3506 * allow for dynamically generated operational attributes, a very
3507 * useful thing indeed.
3511 * Write the computed attribute to a BerElement. Complementary
3512 * functions need to be defined for anything that replaces
3513 * op->o_callback->sc_sendentry, if you wish to make computed
3514 * attributes available to it.
3516 int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
3519 Operation *op = NULL;
3521 AttributeDescription *desc = NULL;
3537 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
3538 if ( rc != 0 || op == NULL ) {
3542 ber = (BerElement *)c->cac_private;
3545 if ( c->cac_attrs == NULL ) {
3546 /* All attrs request, skip operational attributes */
3547 if ( is_at_operational( desc->ad_type ) ) {
3551 /* Specific attrs requested */
3552 if ( is_at_operational( desc->ad_type ) ) {
3553 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3557 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3563 if ( !access_allowed( op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
3564 slapi_log_error( SLAPI_LOG_ACL, "slapi_x_compute_output_ber",
3565 "acl: access to attribute %s not allowed\n",
3566 desc->ad_cname.bv_val );
3570 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
3572 slapi_log_error( SLAPI_LOG_BER, "slapi_x_compute_output_ber",
3573 "ber_printf failed\n");
3577 if ( !c->cac_attrsonly ) {
3578 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
3579 if ( !access_allowed( op, e,
3580 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
3581 slapi_log_error( SLAPI_LOG_ACL, "slapi_x_compute_output_ber",
3583 "acl: access to %s, value %d not allowed\n",
3584 op->o_connid, desc->ad_cname.bv_val, i );
3588 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
3589 slapi_log_error( SLAPI_LOG_BER, "slapi_x_compute_output_ber",
3590 "ber_printf failed\n");
3596 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
3597 slapi_log_error( SLAPI_LOG_BER, "slapi_x_compute_output_ber",
3598 "ber_printf failed\n" );
3609 * For some reason Sun don't use the normal plugin mechanism
3610 * registration path to register an "evaluator" function (an
3611 * "evaluator" is responsible for adding computed attributes;
3612 * the nomenclature is somewhat confusing).
3614 * As such slapi_compute_add_evaluator() registers the
3615 * function directly.
3617 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3620 Slapi_PBlock *pPlugin = NULL;
3623 pPlugin = slapi_pblock_new();
3624 if ( pPlugin == NULL ) {
3625 rc = LDAP_NO_MEMORY;
3629 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3630 if ( rc != LDAP_SUCCESS ) {
3634 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3635 if ( rc != LDAP_SUCCESS ) {
3639 rc = insertPlugin( NULL, pPlugin );
3646 if ( rc != LDAP_SUCCESS ) {
3647 if ( pPlugin != NULL ) {
3648 slapi_pblock_destroy( pPlugin );
3656 #endif /* LDAP_SLAPI */
3660 * See notes above regarding slapi_compute_add_evaluator().
3662 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3665 Slapi_PBlock *pPlugin = NULL;
3668 pPlugin = slapi_pblock_new();
3669 if ( pPlugin == NULL ) {
3670 rc = LDAP_NO_MEMORY;
3674 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3675 if ( rc != LDAP_SUCCESS ) {
3679 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3680 if ( rc != LDAP_SUCCESS ) {
3684 rc = insertPlugin( NULL, pPlugin );
3691 if ( rc != LDAP_SUCCESS ) {
3692 if ( pPlugin != NULL ) {
3693 slapi_pblock_destroy( pPlugin );
3701 #endif /* LDAP_SLAPI */
3705 * Call compute evaluators
3707 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3711 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3713 rc = getAllPluginFuncs( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3714 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3715 /* Nothing to do; front-end should ignore. */
3719 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3721 * -1: no attribute matched requested type
3722 * 0: one attribute matched
3723 * >0: error happened
3725 rc = (*pGetPlugin)( c, type, e, outputfn );
3731 slapi_ch_free( (void **)&tmpPlugin );
3736 #endif /* LDAP_SLAPI */
3739 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3745 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3750 return doPluginFNs( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3753 #endif /* LDAP_SLAPI */
3757 * New API to provide the plugin with access to the search
3758 * pblock. Have informed Sun DS team.
3760 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3766 if ( c->cac_pb == NULL )
3774 #endif /* LDAP_SLAPI */
3777 Slapi_Mutex *slapi_new_mutex( void )
3782 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3783 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3784 slapi_ch_free( (void **)&m );
3794 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3797 if ( mutex != NULL ) {
3798 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3799 slapi_ch_free( (void **)&mutex);
3804 void slapi_lock_mutex( Slapi_Mutex *mutex )
3807 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3811 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3814 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3820 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3825 if ( mutex == NULL ) {
3829 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3830 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3831 slapi_ch_free( (void **)&cv );
3835 /* XXX struct copy */
3836 cv->mutex = mutex->mutex;
3844 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3847 if ( cvar != NULL ) {
3848 ldap_pvt_thread_cond_destroy( &cvar->cond );
3849 slapi_ch_free( (void **)&cvar );
3854 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3857 if ( cvar == NULL ) {
3861 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3867 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3870 if ( cvar == NULL ) {
3875 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3878 return ldap_pvt_thread_cond_signal( &cvar->cond );
3884 int slapi_x_access_allowed( Operation *op,
3886 AttributeDescription *desc,
3888 slap_access_t access,
3889 AccessControlState *state )
3892 int rc, slap_access = 0;
3893 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3895 if ( op->o_pb == NULL ) {
3896 /* internal operation */
3902 slap_access |= SLAPI_ACL_ADD | SLAPI_ACL_DELETE | SLAPI_ACL_WRITE;
3905 slap_access |= SLAPI_ACL_READ;
3908 slap_access |= SLAPI_ACL_SEARCH;
3911 slap_access = ACL_COMPARE;
3917 rc = getAllPluginFuncs( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
3918 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3919 /* nothing to do; allowed access */
3923 slapi_x_pblock_set_operation( op->o_pb, op );
3925 rc = 1; /* default allow policy */
3927 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3932 rc = (*pGetPlugin)( op->o_pb, entry, desc->ad_cname.bv_val,
3933 val, slap_access, (void *)state );
3939 slapi_ch_free( (void **)&tmpPlugin );
3944 #endif /* LDAP_SLAPI */