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>
33 #include <sys/systeminfo.h>
39 * server start time (should we use a struct timeval also in slapd?
41 static struct timeval base_time;
42 ldap_pvt_thread_mutex_t slapi_hn_mutex;
43 ldap_pvt_thread_mutex_t slapi_time_mutex;
46 ldap_pvt_thread_mutex_t mutex;
49 struct slapi_condvar {
50 ldap_pvt_thread_cond_t cond;
51 ldap_pvt_thread_mutex_t mutex;
55 * This function converts an array of pointers to berval objects to
56 * an array of berval objects.
61 struct berval **bvptr,
64 int rc = LDAP_SUCCESS;
68 if ( bvptr == NULL || *bvptr == NULL ) {
72 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
76 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
77 if ( tmpberval == NULL ) {
78 return LDAP_NO_MEMORY;
81 for ( i = 0; bvptr[i] != NULL; i++ ) {
82 tmpberval[i].bv_val = bvptr[i]->bv_val;
83 tmpberval[i].bv_len = bvptr[i]->bv_len;
85 tmpberval[i].bv_val = NULL;
86 tmpberval[i].bv_len = 0;
88 if ( rc == LDAP_SUCCESS ) {
101 Slapi_Entry *e = NULL;
104 pTmpS = slapi_ch_strdup( s );
105 if ( pTmpS != NULL ) {
106 e = str2entry( pTmpS );
107 slapi_ch_free( (void **)&pTmpS );
113 #endif /* LDAP_SLAPI */
124 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
125 ret = entry2str( e, len );
126 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
129 #else /* LDAP_SLAPI */
131 #endif /* LDAP_SLAPI */
135 slapi_entry_get_dn( Slapi_Entry *e )
138 return e->e_name.bv_val;
139 #else /* LDAP_SLAPI */
141 #endif /* LDAP_SLAPI */
145 slapi_x_entry_get_id( Slapi_Entry *e )
151 #endif /* LDAP_SLAPI */
160 struct berval dn = { 0, NULL };
163 dn.bv_len = strlen( ldn );
165 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname, NULL );
166 #endif /* LDAP_SLAPI */
170 slapi_entry_dup( Slapi_Entry *e )
175 ret = (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(*ret) );
178 ber_dupbv( &ret->e_name, &e->e_name );
179 ber_dupbv( &ret->e_nname, &e->e_nname );
180 ret->e_attrs = attrs_dup( e->e_attrs );
181 ret->e_ocflags = e->e_ocflags;
182 ber_dupbv( &ret->e_bv, &e->e_bv );
183 ret->e_private = NULL;
184 #else /* LDAP_SLAPI */
186 #endif /* LDAP_SLAPI */
190 slapi_entry_attr_delete(
195 AttributeDescription *ad = NULL;
198 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
199 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
202 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
203 return 0; /* attribute is deleted */
205 return -1; /* something went wrong */
207 #else /* LDAP_SLAPI */
209 #endif /* LDAP_SLAPI */
213 slapi_entry_alloc( void )
216 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
217 #else /* LDAP_SLAPI */
219 #endif /* LDAP_SLAPI */
223 slapi_entry_free( Slapi_Entry *e )
227 #endif /* LDAP_SLAPI */
231 slapi_entry_attr_merge(
234 struct berval **vals )
237 AttributeDescription *ad = NULL;
242 rc = bvptr2obj( vals, &bv );
243 if ( rc != LDAP_SUCCESS ) {
247 rc = slap_str2ad( type, &ad, &text );
248 if ( rc != LDAP_SUCCESS ) {
252 rc = attr_merge_normalize_one( e, ad, bv, NULL );
256 #else /* LDAP_SLAPI */
258 #endif /* LDAP_SLAPI */
262 slapi_entry_attr_find(
268 AttributeDescription *ad = NULL;
272 rc = slap_str2ad( type, &ad, &text );
273 if ( rc != LDAP_SUCCESS ) {
277 *attr = attr_find( e->e_attrs, ad );
278 if ( *attr == NULL ) {
283 #else /* LDAP_SLAPI */
285 #endif /* LDAP_SLAPI */
289 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
292 AttributeDescription *ad = NULL;
297 rc = slap_str2ad( type, &ad, &text );
298 if ( rc != LDAP_SUCCESS ) {
302 attr = attr_find( e->e_attrs, ad );
303 if ( attr == NULL ) {
307 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
308 return slapi_ch_strdup( attr->a_vals[0].bv_val );
318 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
321 AttributeDescription *ad = NULL;
326 rc = slap_str2ad( type, &ad, &text );
327 if ( rc != LDAP_SUCCESS ) {
331 attr = attr_find( e->e_attrs, ad );
332 if ( attr == NULL ) {
336 return slapi_value_get_int( attr->a_vals );
343 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
346 AttributeDescription *ad = NULL;
351 rc = slap_str2ad( type, &ad, &text );
352 if ( rc != LDAP_SUCCESS ) {
356 attr = attr_find( e->e_attrs, ad );
357 if ( attr == NULL ) {
361 return slapi_value_get_long( attr->a_vals );
368 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
371 AttributeDescription *ad = NULL;
376 rc = slap_str2ad( type, &ad, &text );
377 if ( rc != LDAP_SUCCESS ) {
381 attr = attr_find( e->e_attrs, ad );
382 if ( attr == NULL ) {
386 return slapi_value_get_uint( attr->a_vals );
393 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
396 AttributeDescription *ad = NULL;
401 rc = slap_str2ad( type, &ad, &text );
402 if ( rc != LDAP_SUCCESS ) {
406 attr = attr_find( e->e_attrs, ad );
407 if ( attr == NULL ) {
411 return slapi_value_get_ulong( attr->a_vals );
418 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
422 AttributeDescription *ad = NULL;
427 rc = slap_str2ad( type, &ad, &text );
428 if ( rc != LDAP_SUCCESS ) {
432 attr = attr_find( e->e_attrs, ad );
433 if ( attr == NULL ) {
437 bv.bv_val = (char *)value;
438 bv.bv_len = strlen( value );
440 return ( slapi_attr_value_find( attr, &bv ) != -1 );
447 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
450 AttributeDescription *ad = NULL;
455 rc = slap_str2ad( type, &ad, &text );
456 if ( rc != LDAP_SUCCESS ) {
460 attr_delete ( &e->e_attrs, ad );
461 if ( value != NULL ) {
462 bv.bv_val = (char *)value;
463 bv.bv_len = strlen(value);
464 attr_merge_normalize_one( e, ad, &bv, NULL );
466 #endif /* LDAP_SLAPI */
470 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
475 snprintf( buf, sizeof( buf ), "%d", l );
476 slapi_entry_attr_set_charptr( e, type, buf );
477 #endif /* LDAP_SLAPI */
481 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
486 snprintf( buf, sizeof( buf ), "%u", l );
487 slapi_entry_attr_set_charptr( e, type, buf );
488 #endif /* LDAP_SLAPI */
492 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
497 snprintf( buf, sizeof( buf ), "%ld", l );
498 slapi_entry_attr_set_charptr( e, type, buf );
499 #endif /* LDAP_SLAPI */
503 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
508 snprintf( buf, sizeof( buf ), "%lu", l );
509 slapi_entry_attr_set_charptr( e, type, buf );
510 #endif /* LDAP_SLAPI */
514 slapi_is_rootdse( const char *dn )
517 return ( dn == NULL || dn[0] == '\0' );
524 * Add values to entry.
527 * LDAP_SUCCESS Values added to entry
528 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
529 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
532 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
538 char textbuf[SLAP_TEXT_BUFLEN];
540 mod.sm_op = LDAP_MOD_ADD;
542 mod.sm_type.bv_val = (char *)type;
543 mod.sm_type.bv_len = strlen( type );
545 rc = slap_str2ad( type, &mod.sm_desc, &text );
546 if ( rc != LDAP_SUCCESS ) {
550 if ( vals == NULL ) {
551 /* Apparently vals can be NULL
552 * FIXME: sm_bvalues = NULL ? */
553 mod.sm_bvalues = (BerVarray)ch_malloc( sizeof(struct berval) );
554 mod.sm_bvalues->bv_val = NULL;
557 rc = bvptr2obj( vals, &mod.sm_bvalues );
558 if ( rc != LDAP_SUCCESS ) {
559 return LDAP_CONSTRAINT_VIOLATION;
562 mod.sm_nvalues = NULL;
564 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
566 ch_free( mod.sm_bvalues );
568 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
571 #endif /* LDAP_SLAPI */
575 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
578 return slapi_entry_add_values( e, type, vals );
581 #endif /* LDAP_SLAPI */
585 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
588 AttributeDescription *ad = NULL;
592 rc = slap_str2ad( type, &ad, &text );
593 if ( rc != LDAP_SUCCESS ) {
597 return attr_merge_normalize( e, ad, *vs, NULL );
600 #endif /* LDAP_SLAPI */
604 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
610 char textbuf[SLAP_TEXT_BUFLEN];
612 mod.sm_op = LDAP_MOD_DELETE;
614 mod.sm_type.bv_val = (char *)type;
615 mod.sm_type.bv_len = strlen( type );
617 if ( vals == NULL ) {
618 /* If vals is NULL, this is a NOOP. */
622 rc = slap_str2ad( type, &mod.sm_desc, &text );
623 if ( rc != LDAP_SUCCESS ) {
627 if ( vals[0] == NULL ) {
628 /* SLAPI doco says LDAP_OPERATIONS_ERROR but LDAP_OTHER is better */
629 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
632 rc = bvptr2obj( vals, &mod.sm_bvalues );
633 if ( rc != LDAP_SUCCESS ) {
634 return LDAP_CONSTRAINT_VIOLATION;
636 mod.sm_nvalues = NULL;
638 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
640 ch_free( mod.sm_bvalues );
645 #endif /* LDAP_SLAPI */
649 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
652 return slapi_entry_delete_values( e, type, vals );
655 #endif /* LDAP_SLAPI */
659 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
662 return slapi_entry_attr_merge( e, (char *)type, vals );
665 #endif /* LDAP_SLAPI */
669 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
672 AttributeDescription *ad = NULL;
676 rc = slap_str2ad( type, &ad, &text );
677 if ( rc != LDAP_SUCCESS ) {
681 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
682 if ( rc != LDAP_SUCCESS ) {
689 #endif /* LDAP_SLAPI */
693 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
698 val.bv_val = (char *)value;
699 val.bv_len = strlen( value );
701 return slapi_entry_add_value( e, type, &val );
704 #endif /* LDAP_SLAPI */
708 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
711 Slapi_Value *vals[2];
714 val.bv_val = (char *)value;
715 val.bv_len = strlen( value );
719 return slapi_entry_delete_values_sv( e, type, vals );
722 #endif /* LDAP_SLAPI */
727 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
730 return slapi_entry_attr_merge( e, (char *)type, vals );
737 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
746 return ( *attr != NULL ) ? 0 : -1;
753 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
760 if ( prevattr == NULL ) {
764 *attr = prevattr->a_next;
766 return ( *attr != NULL ) ? 0 : -1;
773 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
776 AttributeDescription *ad = NULL;
781 rc = slap_str2ad( type, &ad, &text );
782 if ( rc != LDAP_SUCCESS ) {
786 attr_delete( &e->e_attrs, ad );
788 rc = bvptr2obj( vals, &bv );
789 if ( rc != LDAP_SUCCESS ) {
793 rc = attr_merge_normalize( e, ad, bv, NULL );
794 slapi_ch_free( (void **)&bv );
795 if ( rc != LDAP_SUCCESS ) {
802 #endif /* LDAP_SLAPI */
806 * FIXME -- The caller must free the allocated memory.
807 * In Netscape they do not have to.
810 slapi_attr_get_values(
812 struct berval ***vals )
818 if ( attr == NULL ) {
822 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
826 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
827 for ( j = 0; j < i; j++ ) {
828 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
832 *vals = (struct berval **)bv;
835 #else /* LDAP_SLAPI */
837 #endif /* LDAP_SLAPI */
841 slapi_dn_normalize( char *dn )
847 assert( dn != NULL );
850 bdn.bv_len = strlen( dn );
852 if ( dnPretty( NULL, &bdn, &pdn, NULL ) != LDAP_SUCCESS ) {
857 #else /* LDAP_SLAPI */
859 #endif /* LDAP_SLAPI */
863 slapi_dn_normalize_case( char *dn )
869 assert( dn != NULL );
872 bdn.bv_len = strlen( dn );
874 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
879 #else /* LDAP_SLAPI */
881 #endif /* LDAP_SLAPI */
890 struct berval bdn, ndn;
891 struct berval bsuffix, nsuffix;
894 assert( dn != NULL );
895 assert( suffix != NULL );
898 bdn.bv_len = strlen( dn );
900 bsuffix.bv_val = suffix;
901 bsuffix.bv_len = strlen( suffix );
903 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
907 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
910 slapi_ch_free( (void **)&ndn.bv_val );
914 rc = dnIsSuffix( &ndn, &nsuffix );
916 slapi_ch_free( (void **)&ndn.bv_val );
917 slapi_ch_free( (void **)&nsuffix.bv_val );
920 #else /* LDAP_SLAPI */
922 #endif /* LDAP_SLAPI */
927 const char *parentdn,
928 const char *childdn )
931 struct berval assertedParentDN, normalizedAssertedParentDN;
932 struct berval childDN, normalizedChildDN;
933 struct berval normalizedParentDN;
936 assert( parentdn != NULL );
937 assert( childdn != NULL );
939 assertedParentDN.bv_val = (char *)parentdn;
940 assertedParentDN.bv_len = strlen( parentdn );
942 if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
943 &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
948 childDN.bv_val = (char *)childdn;
949 childDN.bv_len = strlen( childdn );
951 if ( dnNormalize( 0, NULL, NULL, &childDN,
952 &normalizedChildDN, NULL ) != LDAP_SUCCESS )
954 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
958 dnParent( &normalizedChildDN, &normalizedParentDN );
960 if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
961 &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
966 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
967 slapi_ch_free( (void **)&normalizedChildDN.bv_val );
969 return ( match == 0 );
972 #endif /* LDAP_SLAPI */
976 * Returns DN of the parent entry, or NULL if the DN is
977 * an empty string or NULL, or has no parent.
980 slapi_dn_parent( const char *_dn )
983 struct berval dn, prettyDN;
984 struct berval parentDN;
990 dn.bv_val = (char *)_dn;
991 dn.bv_len = strlen( _dn );
993 if ( dn.bv_len == 0 ) {
997 if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
1001 dnParent( &prettyDN, &parentDN ); /* in-place */
1003 slapi_ch_free( (void **)&prettyDN.bv_val );
1005 if ( parentDN.bv_len == 0 ) {
1009 return slapi_ch_strdup( parentDN.bv_val );
1012 #endif /* LDAP_SLAPI */
1016 * Returns DN of the parent entry; or NULL if the DN is
1017 * an empty string, if the DN has no parent, or if the
1018 * DN is the suffix of the backend database
1020 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *_dn )
1024 struct berval dn, prettyDN;
1025 struct berval normalizedDN, parentDN;
1027 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
1030 dn.bv_val = (char *)_dn;
1031 dn.bv_len = strlen( _dn );
1033 if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
1037 if ( be != NULL && be_issuffix( be, &normalizedDN ) ) {
1038 slapi_ch_free( (void **)&prettyDN.bv_val );
1039 slapi_ch_free( (void **)&normalizedDN.bv_val );
1043 dnParent( &prettyDN, &parentDN );
1045 slapi_ch_free( (void **)&prettyDN.bv_val );
1046 slapi_ch_free( (void **)&normalizedDN.bv_val );
1048 if ( parentDN.bv_len == 0 ) {
1052 return slapi_ch_strdup( parentDN.bv_val );
1055 #endif /* LDAP_SLAPI */
1059 slapi_dn_ignore_case( char *dn )
1062 return slapi_dn_normalize_case( dn );
1063 #else /* LDAP_SLAPI */
1065 #endif /* LDAP_SLAPI */
1069 slapi_ch_malloc( unsigned long size )
1072 return ch_malloc( size );
1073 #else /* LDAP_SLAPI */
1075 #endif /* LDAP_SLAPI */
1079 slapi_ch_free( void **ptr )
1084 #endif /* LDAP_SLAPI */
1088 slapi_ch_free_string( char **ptr )
1091 slapi_ch_free( (void **)ptr );
1092 #endif /* LDAP_SLAPI */
1096 slapi_ch_array_free( char **arrayp )
1101 if ( arrayp != NULL ) {
1102 for ( p = arrayp; *p != NULL; p++ ) {
1103 slapi_ch_free( (void **)p );
1105 slapi_ch_free( (void **)&arrayp );
1111 slapi_ch_bvdup(const struct berval *v)
1116 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
1117 bv->bv_len = v->bv_len;
1118 bv->bv_val = slapi_ch_malloc( bv->bv_len );
1119 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1128 slapi_ch_bvecdup(const struct berval **v)
1138 for ( i = 0; v[i] != NULL; i++ )
1141 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1143 for ( i = 0; v[i] != NULL; i++ ) {
1144 rv[i] = slapi_ch_bvdup( v[i] );
1156 unsigned long nelem,
1157 unsigned long size )
1160 return ch_calloc( nelem, size );
1161 #else /* LDAP_SLAPI */
1163 #endif /* LDAP_SLAPI */
1169 unsigned long size )
1172 return ch_realloc( block, size );
1173 #else /* LDAP_SLAPI */
1175 #endif /* LDAP_SLAPI */
1179 slapi_ch_strdup( char *s )
1182 return ch_strdup( (const char *)s );
1183 #else /* LDAP_SLAPI */
1185 #endif /* LDAP_SLAPI */
1189 slapi_ch_stlen( char *s )
1192 return strlen( (const char *)s );
1193 #else /* LDAP_SLAPI */
1195 #endif /* LDAP_SLAPI */
1199 slapi_control_present(
1200 LDAPControl **controls,
1202 struct berval **val,
1217 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1218 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1223 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1225 * FIXME: according to 6.1 specification,
1226 * "The val output parameter is set
1227 * to point into the controls array.
1228 * A copy of the control value is
1232 struct berval *pTmpBval;
1234 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
1235 if ( pTmpBval == NULL ) {
1238 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
1239 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
1243 slapi_ch_free( (void **)&pTmpBval );
1249 *val = &controls[i]->ldctl_value;
1254 *iscritical = controls[i]->ldctl_iscritical;
1261 #else /* LDAP_SLAPI */
1263 #endif /* LDAP_SLAPI */
1268 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1269 unsigned long *slapi_mask)
1271 *slapi_mask = SLAPI_OPERATION_NONE;
1273 if ( slap_mask & SLAP_CTRL_ABANDON )
1274 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1276 if ( slap_mask & SLAP_CTRL_ADD )
1277 *slapi_mask |= SLAPI_OPERATION_ADD;
1279 if ( slap_mask & SLAP_CTRL_BIND )
1280 *slapi_mask |= SLAPI_OPERATION_BIND;
1282 if ( slap_mask & SLAP_CTRL_COMPARE )
1283 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1285 if ( slap_mask & SLAP_CTRL_DELETE )
1286 *slapi_mask |= SLAPI_OPERATION_DELETE;
1288 if ( slap_mask & SLAP_CTRL_MODIFY )
1289 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1291 if ( slap_mask & SLAP_CTRL_RENAME )
1292 *slapi_mask |= SLAPI_OPERATION_MODDN;
1294 if ( slap_mask & SLAP_CTRL_SEARCH )
1295 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1297 if ( slap_mask & SLAP_CTRL_UNBIND )
1298 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1302 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1303 slap_mask_t *slap_mask)
1307 if ( slapi_mask & SLAPI_OPERATION_BIND )
1308 *slap_mask |= SLAP_CTRL_BIND;
1310 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1311 *slap_mask |= SLAP_CTRL_UNBIND;
1313 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1314 *slap_mask |= SLAP_CTRL_SEARCH;
1316 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1317 *slap_mask |= SLAP_CTRL_MODIFY;
1319 if ( slapi_mask & SLAPI_OPERATION_ADD )
1320 *slap_mask |= SLAP_CTRL_ADD;
1322 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1323 *slap_mask |= SLAP_CTRL_DELETE;
1325 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1326 *slap_mask |= SLAP_CTRL_RENAME;
1328 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1329 *slap_mask |= SLAP_CTRL_COMPARE;
1331 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1332 *slap_mask |= SLAP_CTRL_ABANDON;
1334 *slap_mask |= SLAP_CTRL_FRONTEND;
1343 /* Plugins must deal with controls themselves. */
1345 return LDAP_SUCCESS;
1347 #endif /* LDAP_SLAPI */
1350 slapi_register_supported_control(
1352 unsigned long controlops )
1355 slap_mask_t controlmask;
1357 slapiControlOp2SlapControlMask( controlops, &controlmask );
1359 register_supported_control( controloid, controlmask, NULL, parseSlapiControl );
1360 #endif /* LDAP_SLAPI */
1364 slapi_get_supported_controls(
1366 unsigned long **ctrlopsp )
1371 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1372 if ( rc != LDAP_SUCCESS ) {
1376 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1377 /* In place, naughty. */
1378 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1381 return LDAP_SUCCESS;
1382 #else /* LDAP_SLAPI */
1384 #endif /* LDAP_SLAPI */
1388 slapi_dup_control( LDAPControl *ctrl )
1393 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1394 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1395 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1396 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1401 #endif /* LDAP_SLAPI */
1405 slapi_register_supported_saslmechanism( char *mechanism )
1408 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1409 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
1410 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1411 #endif /* LDAP_SLAPI */
1415 slapi_get_supported_saslmechanisms( void )
1418 /* FIXME -- can not get the saslmechanism without a connection. */
1419 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
1420 "can not get the SASL mechanism list "
1421 "without a connection\n" );
1423 #else /* LDAP_SLAPI */
1425 #endif /* LDAP_SLAPI */
1429 slapi_get_supported_extended_ops( void )
1433 char **ppExtOpOID = NULL;
1436 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1440 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
1445 if ( numExtOps == 0 ) {
1449 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1450 for ( k = 0; k < i; k++ ) {
1453 bv = get_supported_extop( k );
1454 assert( bv != NULL );
1456 ppExtOpOID[ k ] = bv->bv_val;
1459 for ( ; k < j; k++ ) {
1462 bv = ns_get_supported_extop( k );
1463 assert( bv != NULL );
1465 ppExtOpOID[ i + k ] = bv->bv_val;
1467 ppExtOpOID[ i + k ] = NULL;
1470 #else /* LDAP_SLAPI */
1472 #endif /* LDAP_SLAPI */
1476 slapi_send_ldap_result(
1482 struct berval **urls )
1487 char *extOID = NULL;
1488 struct berval *extValue = NULL;
1490 SlapReply rs = { REP_RESULT };
1492 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1495 rs.sr_matched = matched;
1500 slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
1502 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1503 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
1504 send_ldap_sasl( op, &rs );
1508 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1509 if ( extOID != NULL ) {
1510 rs.sr_rspoid = extOID;
1511 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
1512 send_ldap_extended( op, &rs );
1516 if (op->o_tag == LDAP_REQ_SEARCH)
1517 rs.sr_nentries = nentries;
1519 send_ldap_result( op, &rs );
1520 #endif /* LDAP_SLAPI */
1524 slapi_send_ldap_search_entry(
1527 LDAPControl **ectrls,
1533 SlapReply rs = { REP_RESULT };
1535 AttributeName *an = NULL;
1538 if ( attrs != NULL ) {
1539 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1547 an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
1548 for ( i = 0; attrs[i] != NULL; i++ ) {
1549 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1550 an[i].an_name.bv_len = strlen( attrs[i] );
1551 an[i].an_desc = NULL;
1552 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1555 an[i].an_name.bv_len = 0;
1556 an[i].an_name.bv_val = NULL;
1559 rs.sr_err = LDAP_SUCCESS;
1560 rs.sr_matched = NULL;
1563 rs.sr_ctrls = ectrls;
1568 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1572 return send_search_entry( pOp, &rs );
1573 #else /* LDAP_SLAPI */
1575 #endif /* LDAP_SLAPI */
1579 slapi_send_ldap_search_reference(
1582 struct berval **references,
1583 LDAPControl **ectrls,
1584 struct berval **v2refs
1589 SlapReply rs = { REP_SEARCHREF };
1592 rs.sr_err = LDAP_SUCCESS;
1593 rs.sr_matched = NULL;
1596 rc = bvptr2obj( references, &rs.sr_ref );
1597 if ( rc != LDAP_SUCCESS ) {
1601 rs.sr_ctrls = ectrls;
1605 if ( v2refs != NULL ) {
1606 rc = bvptr2obj( v2refs, &rs.sr_v2ref );
1607 if ( rc != LDAP_SUCCESS ) {
1608 slapi_ch_free( (void **)&rs.sr_ref );
1615 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1619 rc = send_search_reference( pOp, &rs );
1621 if ( rs.sr_ref != NULL )
1622 slapi_ch_free( (void **)&rs.sr_ref );
1624 if ( rs.sr_v2ref != NULL )
1625 slapi_ch_free( (void **)&rs.sr_v2ref );
1630 #endif /* LDAP_SLAPI */
1634 slapi_str2filter( char *str )
1637 return str2filter( str );
1638 #else /* LDAP_SLAPI */
1640 #endif /* LDAP_SLAPI */
1650 #endif /* LDAP_SLAPI */
1654 slapi_filter_dup( Slapi_Filter *filter )
1659 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1661 f->f_choice = filter->f_choice;
1663 switch ( f->f_choice ) {
1664 case LDAP_FILTER_AND:
1665 case LDAP_FILTER_NOT:
1666 case LDAP_FILTER_OR: {
1667 Filter *pFilter, **ppF;
1669 for ( pFilter = filter->f_list, ppF = &f->f_list;
1671 pFilter = pFilter->f_next, ppF = &f->f_next )
1673 *ppF = slapi_filter_dup( pFilter );
1679 case LDAP_FILTER_PRESENT:
1680 f->f_desc = filter->f_desc;
1682 case LDAP_FILTER_EQUALITY:
1683 case LDAP_FILTER_GE:
1684 case LDAP_FILTER_LE:
1685 case LDAP_FILTER_APPROX:
1686 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1687 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1688 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1690 case LDAP_FILTER_EXT:
1691 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1692 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1693 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1694 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1695 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1696 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1698 case LDAP_FILTER_SUBSTRINGS: {
1701 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1702 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1703 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1704 if ( filter->f_sub_any != NULL ) {
1705 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1707 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1708 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1709 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1711 f->f_sub_any[i].bv_val = NULL;
1713 f->f_sub_any = NULL;
1715 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1718 case SLAPD_FILTER_COMPUTED:
1719 f->f_result = filter->f_result;
1722 slapi_ch_free( (void **)&f );
1730 #endif /* LDAP_SLAPI */
1734 slapi_filter_get_choice( Slapi_Filter *f )
1746 #else /* LDAP_SLAPI */
1747 return -1; /* invalid filter type */
1748 #endif /* LDAP_SLAPI */
1752 slapi_filter_get_ava(
1755 struct berval **bval )
1759 int rc = LDAP_SUCCESS;
1761 assert( type != NULL );
1762 assert( bval != NULL );
1767 ftype = f->f_choice;
1768 if ( ftype == LDAP_FILTER_EQUALITY
1769 || ftype == LDAP_FILTER_GE
1770 || ftype == LDAP_FILTER_LE
1771 || ftype == LDAP_FILTER_APPROX ) {
1773 * According to the SLAPI Reference Manual these are
1776 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1777 *bval = &f->f_un.f_un_ava->aa_value;
1778 } else { /* filter type not supported */
1783 #else /* LDAP_SLAPI */
1785 #endif /* LDAP_SLAPI */
1789 slapi_filter_list_first( Slapi_Filter *f )
1798 ftype = f->f_choice;
1799 if ( ftype == LDAP_FILTER_AND
1800 || ftype == LDAP_FILTER_OR
1801 || ftype == LDAP_FILTER_NOT ) {
1802 return (Slapi_Filter *)f->f_list;
1806 #else /* LDAP_SLAPI */
1808 #endif /* LDAP_SLAPI */
1812 slapi_filter_list_next(
1814 Slapi_Filter *fprev )
1823 ftype = f->f_choice;
1824 if ( ftype == LDAP_FILTER_AND
1825 || ftype == LDAP_FILTER_OR
1826 || ftype == LDAP_FILTER_NOT )
1828 return fprev->f_next;
1832 #else /* LDAP_SLAPI */
1834 #endif /* LDAP_SLAPI */
1838 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1845 switch ( f->f_choice ) {
1846 case LDAP_FILTER_GE:
1847 case LDAP_FILTER_LE:
1848 case LDAP_FILTER_EQUALITY:
1849 case LDAP_FILTER_APPROX:
1850 *type = f->f_av_desc->ad_cname.bv_val;
1852 case LDAP_FILTER_SUBSTRINGS:
1853 *type = f->f_sub_desc->ad_cname.bv_val;
1855 case LDAP_FILTER_PRESENT:
1856 *type = f->f_desc->ad_cname.bv_val;
1858 case LDAP_FILTER_EXT:
1859 *type = f->f_mr_desc->ad_cname.bv_val;
1862 /* Complex filters need not apply. */
1870 #endif /* LDAP_SLAPI */
1874 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1875 char ***any, char **final )
1880 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1885 * The caller shouldn't free but we can't return an
1886 * array of char *s from an array of bervals without
1887 * allocating memory, so we may as well be consistent.
1890 *type = f->f_sub_desc->ad_cname.bv_val;
1891 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1892 if ( f->f_sub_any != NULL ) {
1893 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1895 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1896 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1897 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1903 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1908 #endif /* LDAP_SLAPI */
1912 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1915 Slapi_Filter *f = NULL;
1917 if ( ftype == LDAP_FILTER_AND ||
1918 ftype == LDAP_FILTER_OR ||
1919 ftype == LDAP_FILTER_NOT )
1921 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1922 f->f_choice = ftype;
1924 f->f_list->f_next = f2;
1931 #endif /* LDAP_SLAPI */
1935 slapi_x_filter_append( int ftype,
1936 Slapi_Filter **pContainingFilter, /* NULL on first call */
1937 Slapi_Filter **pNextFilter,
1938 Slapi_Filter *filterToAppend )
1941 if ( ftype == LDAP_FILTER_AND ||
1942 ftype == LDAP_FILTER_OR ||
1943 ftype == LDAP_FILTER_NOT )
1945 if ( *pContainingFilter == NULL ) {
1946 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
1947 (*pContainingFilter)->f_choice = ftype;
1948 (*pContainingFilter)->f_list = filterToAppend;
1949 (*pContainingFilter)->f_next = NULL;
1951 if ( (*pContainingFilter)->f_choice != ftype ) {
1955 (*pNextFilter)->f_next = filterToAppend;
1957 *pNextFilter = filterToAppend;
1961 #endif /* LDAP_SLAPI */
1966 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1974 /* spec says return zero if no filter. */
1978 if ( verify_access ) {
1979 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
1981 return LDAP_PARAM_ERROR;
1987 * According to acl.c it is safe to call test_filter() with
1990 rc = test_filter( op, e, f );
1992 case LDAP_COMPARE_TRUE:
1995 case LDAP_COMPARE_FALSE:
1997 case SLAPD_COMPARE_UNDEFINED:
2000 case LDAP_PROTOCOL_ERROR:
2001 /* filter type unknown: spec says return -1 */
2009 #endif /* LDAP_SLAPI */
2013 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
2016 return slapi_filter_test( NULL, e, f, 0 );
2023 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
2026 switch ( f->f_choice ) {
2027 case LDAP_FILTER_AND:
2028 case LDAP_FILTER_NOT:
2029 case LDAP_FILTER_OR: {
2033 * FIXME: altering f; should we use a temporary?
2035 for ( f = f->f_list; f != NULL; f = f->f_next ) {
2036 rc = slapi_filter_apply( f, fn, arg, error_code );
2040 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
2046 case LDAP_FILTER_EQUALITY:
2047 case LDAP_FILTER_SUBSTRINGS:
2048 case LDAP_FILTER_GE:
2049 case LDAP_FILTER_LE:
2050 case LDAP_FILTER_PRESENT:
2051 case LDAP_FILTER_APPROX:
2052 case LDAP_FILTER_EXT:
2053 *error_code = fn( f, arg );
2056 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2059 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
2060 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
2066 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2068 #endif /* LDAP_SLAPI */
2072 slapi_send_ldap_extended_response(
2077 struct berval *response )
2082 rs.sr_err = errornum;
2083 rs.sr_matched = NULL;
2087 rs.sr_rspoid = respName;
2088 rs.sr_rspdata = response;
2090 send_ldap_extended( op, &rs );
2092 return LDAP_SUCCESS;
2093 #else /* LDAP_SLAPI */
2095 #endif /* LDAP_SLAPI */
2100 struct berval **vals,
2105 * FIXME: what's the point?
2108 #else /* LDAP_SLAPI */
2110 #endif /* LDAP_SLAPI */
2113 #define MAX_HOSTNAME 512
2116 slapi_get_hostname( void )
2122 * FIXME: I'd prefer a different check ...
2125 hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
2127 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
2128 "can't malloc memory for hostname\n" );
2131 } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
2132 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
2133 "can't get hostname\n" );
2134 slapi_ch_free( (void **)&hn );
2138 static int been_here = 0;
2139 static char *static_hn = NULL;
2141 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
2143 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
2144 if ( static_hn == NULL) {
2145 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
2146 "can't malloc memory for hostname\n" );
2148 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2153 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
2154 slapi_log_error( SLAPI_LOG_FATAL,
2156 "can't get hostname\n" );
2157 slapi_ch_free( (void **)&static_hn );
2159 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2168 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2170 hn = ch_strdup( static_hn );
2171 #endif /* !_SPARC */
2174 #else /* LDAP_SLAPI */
2176 #endif /* LDAP_SLAPI */
2180 * FIXME: this should go in an appropriate header ...
2182 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
2192 int rc = LDAP_SUCCESS;
2195 va_start( arglist, fmt );
2196 rc = vLogError( severity, subsystem, fmt, arglist );
2200 #else /* LDAP_SLAPI */
2202 #endif /* LDAP_SLAPI */
2207 slapi_timer_current_time( void )
2210 static int first_time = 1;
2211 #if !defined (_WIN32)
2215 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
2218 gettimeofday( &base_time, NULL );
2220 gettimeofday( &now, NULL );
2221 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
2222 (now.tv_usec - base_time.tv_usec);
2223 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
2229 return (slap_get_time() - starttime) * 1000000;
2236 performance_counter_present = QueryPerformanceCounter( &base_time );
2237 QueryPerformanceFrequency( &performance_freq );
2240 if ( !performance_counter_present )
2243 QueryPerformanceCounter( &now );
2244 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2246 #else /* LDAP_SLAPI */
2248 #endif /* LDAP_SLAPI */
2255 slapi_timer_get_time( char *label )
2258 unsigned long start = slapi_timer_current_time();
2259 printf("%10ld %10ld usec %s\n", start, 0, label);
2261 #else /* LDAP_SLAPI */
2263 #endif /* LDAP_SLAPI */
2270 slapi_timer_elapsed_time(
2272 unsigned long start )
2275 unsigned long stop = slapi_timer_current_time();
2276 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2277 #endif /* LDAP_SLAPI */
2281 slapi_free_search_results_internal( Slapi_PBlock *pb )
2284 Slapi_Entry **entries;
2285 int k = 0, nEnt = 0;
2287 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2288 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2293 if ( entries == NULL ) {
2297 for ( k = 0; k < nEnt; k++ ) {
2298 slapi_entry_free( entries[k] );
2301 slapi_ch_free( (void **)&entries );
2302 #endif /* LDAP_SLAPI */
2307 * Internal API to prime a Slapi_PBlock with a Backend.
2309 static int initBackendPB( Slapi_PBlock *pb, Backend *be )
2313 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2314 if ( rc != LDAP_SUCCESS )
2318 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2319 if ( rc != LDAP_SUCCESS )
2323 return LDAP_SUCCESS;
2327 * If oldStyle is TRUE, then a value suitable for setting to
2328 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
2329 * (pointer to static storage).
2331 * If oldStyle is FALSE, then a value suitable for setting to
2332 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2333 * a pointer to allocated memory and will include the SASL
2334 * mechanism (if any).
2336 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2341 switch ( authz->sai_method ) {
2342 case LDAP_AUTH_SASL:
2344 authType = SLAPD_AUTH_SASL;
2346 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2347 authType = slapi_ch_malloc( len );
2348 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2351 case LDAP_AUTH_SIMPLE:
2352 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2354 case LDAP_AUTH_NONE:
2355 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2361 if ( is_tls && authType == NULL ) {
2362 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2369 * Internal API to prime a Slapi_PBlock with a Connection.
2371 static int initConnectionPB( Slapi_PBlock *pb, Connection *conn )
2376 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2377 if ( rc != LDAP_SUCCESS )
2380 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2381 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2382 if ( rc != LDAP_SUCCESS )
2384 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2385 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2386 if ( rc != LDAP_SUCCESS )
2390 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2391 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2392 if ( rc != LDAP_SUCCESS )
2394 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2395 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2396 if ( rc != LDAP_SUCCESS )
2400 #ifdef LDAP_CONNECTIONLESS
2401 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2402 if ( rc != LDAP_SUCCESS )
2406 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2407 if ( rc != LDAP_SUCCESS )
2410 /* Returns pointer to static string */
2411 connAuthType = Authorization2AuthType( &conn->c_authz,
2418 if ( connAuthType != NULL ) {
2419 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2420 if ( rc != LDAP_SUCCESS )
2424 /* Returns pointer to allocated string */
2425 connAuthType = Authorization2AuthType( &conn->c_authz,
2432 if ( connAuthType != NULL ) {
2433 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2434 /* slapi_pblock_set dups this itself */
2435 slapi_ch_free( (void **)&connAuthType );
2436 if ( rc != LDAP_SUCCESS )
2440 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
2441 /* slapi_pblock_set dups this itself */
2442 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)conn->c_authz.sai_dn.bv_val);
2443 if ( rc != LDAP_SUCCESS )
2447 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SSF, (void *)conn->c_ssf);
2448 if ( rc != LDAP_SUCCESS )
2451 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SASL_CONTEXT,
2452 ( conn->c_sasl_authctx != NULL ? conn->c_sasl_authctx :
2453 conn->c_sasl_sockctx ) );
2454 if ( rc != LDAP_SUCCESS )
2459 #endif /* LDAP_SLAPI */
2462 * Internal API to prime a Slapi_PBlock with an Operation.
2464 int slapi_x_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
2472 if ( op->o_bd != NULL ) {
2473 isRoot = be_isroot( op->o_bd, &op->o_ndn );
2474 isUpdateDn = be_isupdate( op->o_bd, &op->o_ndn );
2477 rc = initBackendPB( pb, op->o_bd );
2478 if ( rc != LDAP_SUCCESS )
2481 rc = initConnectionPB( pb, op->o_conn );
2482 if ( rc != LDAP_SUCCESS )
2485 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2486 if ( rc != LDAP_SUCCESS )
2489 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2490 if ( rc != LDAP_SUCCESS )
2493 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2494 if ( rc != LDAP_SUCCESS )
2497 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2498 if ( rc != LDAP_SUCCESS )
2501 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2502 if ( rc != LDAP_SUCCESS )
2505 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2506 if ( rc != LDAP_SUCCESS )
2509 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2510 if ( rc != LDAP_SUCCESS)
2513 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2514 if ( rc != LDAP_SUCCESS )
2517 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2518 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
2519 /* Not quite sure what the point of this is. */
2520 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
2521 if ( rc != LDAP_SUCCESS )
2525 return LDAP_SUCCESS;
2531 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2536 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2538 *isSSL = conn->c_is_tls;
2543 return LDAP_SUCCESS;
2546 #endif /* LDAP_SLAPI */
2550 * DS 5.x compatability API follow
2553 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2559 return LDAP_PARAM_ERROR;
2561 at = attr->a_desc->ad_type;
2563 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2565 if ( is_at_single_value( at ) )
2566 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2567 if ( is_at_operational( at ) )
2568 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2569 if ( is_at_obsolete( at ) )
2570 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2571 if ( is_at_collective( at ) )
2572 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2573 if ( is_at_no_user_mod( at ) )
2574 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2576 return LDAP_SUCCESS;
2579 #endif /* LDAP_SLAPI */
2582 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2585 unsigned long flags;
2587 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2589 return (flags & flag) ? 1 : 0;
2592 #endif /* LDAP_SLAPI */
2595 Slapi_Attr *slapi_attr_new( void )
2600 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2608 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2612 AttributeDescription *ad = NULL;
2614 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2630 void slapi_attr_free( Slapi_Attr **a )
2638 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2641 return attr_dup( (Slapi_Attr *)attr );
2647 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2651 * FIXME: here we may lose alignment between a_vals/a_nvals
2653 return value_add_one( &a->a_vals, (Slapi_Value *)v );
2659 int slapi_attr_type2plugin( const char *type, void **pi )
2666 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2669 if ( attr == NULL ) {
2670 return LDAP_PARAM_ERROR;
2673 *type = attr->a_desc->ad_cname.bv_val;
2675 return LDAP_SUCCESS;
2681 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2684 if ( attr == NULL ) {
2685 return LDAP_PARAM_ERROR;
2687 *oidp = attr->a_desc->ad_type->sat_oid;
2689 return LDAP_SUCCESS;
2695 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2703 mr = a->a_desc->ad_type->sat_equality;
2704 rc = value_match( &ret, a->a_desc, mr,
2705 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2706 (struct berval *)v1, (void *)v2, &text );
2707 if ( rc != LDAP_SUCCESS )
2710 return ( ret == 0 ) ? 0 : -1;
2716 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2726 if ( a ->a_vals == NULL ) {
2729 mr = a->a_desc->ad_type->sat_equality;
2730 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2731 rc = value_match( &ret, a->a_desc, mr,
2732 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2733 if ( rc != LDAP_SUCCESS ) {
2740 #endif /* LDAP_SLAPI */
2744 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2747 AttributeDescription *a1 = NULL;
2748 AttributeDescription *a2 = NULL;
2752 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2756 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2760 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2761 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2762 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2765 case SLAPI_TYPE_CMP_EXACT:
2766 ret = ad_cmp( a1, a2 );
2768 case SLAPI_TYPE_CMP_BASE:
2769 ret = ad_base_cmp( a1, a2 );
2771 case SLAPI_TYPE_CMP_SUBTYPE:
2772 ret = is_ad_subtype( a2, a2 );
2785 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2788 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2794 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2797 return slapi_valueset_first_value( &a->a_vals, v );
2803 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2806 return slapi_valueset_next_value( &a->a_vals, hint, v );
2812 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2815 *numValues = slapi_valueset_count( &a->a_vals );
2823 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2826 *vs = &((Slapi_Attr *)a)->a_vals;
2834 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2837 return slapi_attr_get_values( a, vals );
2843 char *slapi_attr_syntax_normalize( const char *s )
2846 AttributeDescription *ad = NULL;
2849 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2853 return ad->ad_cname.bv_val;
2859 Slapi_Value *slapi_value_new( void )
2864 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2872 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2875 return ber_dupbv( NULL, (struct berval *)bval );
2881 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2884 return slapi_value_new_berval( v );
2890 Slapi_Value *slapi_value_new_string(const char *s)
2895 bv.bv_val = (char *)s;
2896 bv.bv_len = strlen( s );
2898 return slapi_value_new_berval( &bv );
2904 Slapi_Value *slapi_value_init(Slapi_Value *val)
2916 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2919 return ber_dupbv( v, bval );
2925 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2928 v->bv_val = slapi_ch_strdup( (char *)s );
2929 v->bv_len = strlen( s );
2937 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2940 return slapi_value_new_value( v );
2946 void slapi_value_free(Slapi_Value **value)
2949 if ( value == NULL ) {
2953 if ( (*value) != NULL ) {
2954 slapi_ch_free( (void **)&(*value)->bv_val );
2955 slapi_ch_free( (void **)value );
2960 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2969 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2972 if ( value == NULL ) {
2975 if ( value->bv_val != NULL ) {
2976 slapi_ch_free( (void **)&value->bv_val );
2978 slapi_value_init_berval( value, (struct berval *)bval );
2986 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2989 if ( value == NULL ) {
2992 return slapi_value_set_berval( value, vfrom );
2998 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
3001 if ( value == NULL ) {
3004 if ( value->bv_val != NULL ) {
3005 slapi_ch_free( (void **)&value->bv_val );
3007 value->bv_val = slapi_ch_malloc( len );
3008 value->bv_len = len;
3009 AC_MEMCPY( value->bv_val, val, len );
3017 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
3020 if ( value == NULL ) {
3023 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
3030 int slapi_value_set_int(Slapi_Value *value, int intVal)
3035 snprintf( buf, sizeof( buf ), "%d", intVal );
3037 return slapi_value_set_string( value, buf );
3043 const char *slapi_value_get_string(const Slapi_Value *value)
3046 if ( value == NULL ) {
3049 return value->bv_val;
3056 static int checkBVString(const struct berval *bv)
3060 for ( i = 0; i < bv->bv_len; i++ ) {
3061 if ( bv->bv_val[i] == '\0' )
3064 if ( bv->bv_val[i] != '\0' )
3071 int slapi_value_get_int(const Slapi_Value *value)
3074 if ( value == NULL ) return 0;
3075 if ( value->bv_val == NULL ) return 0;
3076 if ( !checkBVString( value ) ) return 0;
3078 return (int)strtol( value->bv_val, NULL, 10 );
3084 unsigned int slapi_value_get_uint(const Slapi_Value *value)
3087 if ( value == NULL ) return 0;
3088 if ( value->bv_val == NULL ) return 0;
3089 if ( !checkBVString( value ) ) return 0;
3091 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
3097 long slapi_value_get_long(const Slapi_Value *value)
3100 if ( value == NULL ) return 0;
3101 if ( value->bv_val == NULL ) return 0;
3102 if ( !checkBVString( value ) ) return 0;
3104 return strtol( value->bv_val, NULL, 10 );
3110 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
3113 if ( value == NULL ) return 0;
3114 if ( value->bv_val == NULL ) return 0;
3115 if ( !checkBVString( value ) ) return 0;
3117 return strtoul( value->bv_val, NULL, 10 );
3123 size_t slapi_value_get_length(const Slapi_Value *value)
3126 if ( value == NULL )
3129 return (size_t) value->bv_len;
3135 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
3138 return slapi_attr_value_cmp( a, v1, v2 );
3144 /* A ValueSet is a container for a BerVarray. */
3145 Slapi_ValueSet *slapi_valueset_new( void )
3150 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
3159 void slapi_valueset_free(Slapi_ValueSet *vs)
3165 ber_bvarray_free( vp );
3166 slapi_ch_free( (void **)&vp );
3173 void slapi_valueset_init(Slapi_ValueSet *vs)
3176 if ( vs != NULL && *vs == NULL ) {
3177 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
3178 (*vs)->bv_val = NULL;
3184 void slapi_valueset_done(Slapi_ValueSet *vs)
3192 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
3194 slapi_ch_free( (void **)&vp->bv_val );
3196 /* but don't free *vs or vs */
3200 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
3205 ber_dupbv( &bv, (Slapi_Value *)addval );
3206 ber_bvarray_add( vs, &bv );
3210 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
3213 return slapi_valueset_next_value( vs, 0, v );
3219 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
3230 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
3241 int slapi_valueset_count( const Slapi_ValueSet *vs )
3252 for ( i = 0; vp[i].bv_val != NULL; i++ )
3262 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
3267 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
3268 slapi_valueset_add_value( vs1, vp );
3273 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
3274 struct berval *val, int access )
3281 slap_access_t slap_access;
3282 AttributeDescription *ad = NULL;
3285 ret = slap_str2ad( attr, &ad, &text );
3286 if ( ret != LDAP_SUCCESS ) {
3290 switch ( access & SLAPI_ACL_ALL ) {
3291 case SLAPI_ACL_COMPARE:
3292 slap_access = ACL_COMPARE;
3294 case SLAPI_ACL_SEARCH:
3295 slap_access = ACL_SEARCH;
3297 case SLAPI_ACL_READ:
3298 slap_access = ACL_READ;
3300 case SLAPI_ACL_WRITE:
3301 case SLAPI_ACL_DELETE:
3303 case SLAPI_ACL_SELF:
3304 slap_access = ACL_WRITE;
3307 return LDAP_INSUFFICIENT_ACCESS;
3311 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3312 return LDAP_PARAM_ERROR;
3315 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3316 return LDAP_PARAM_ERROR;
3319 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3320 return LDAP_PARAM_ERROR;
3323 ret = access_allowed( op, e, ad, val, slap_access, NULL );
3325 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3327 return LDAP_UNWILLING_TO_PERFORM;
3331 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
3335 int rc = LDAP_SUCCESS;
3336 Modifications *ml, *mp;
3338 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3339 return LDAP_PARAM_ERROR;
3342 ml = slapi_x_ldapmods2modifications( mods );
3347 for ( mp = ml; mp != NULL; mp = mp->sml_next ) {
3348 rc = slap_bv2ad( &mp->sml_type, &mp->sml_desc, (const char **)errbuf );
3349 if ( rc != LDAP_SUCCESS ) {
3354 if ( rc == LDAP_SUCCESS ) {
3355 rc = acl_check_modlist( op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3358 /* Careful when freeing the modlist because it has pointers into the mods array. */
3359 for ( ; ml != NULL; ml = mp ) {
3362 /* just free the containing array */
3363 slapi_ch_free( (void **)&ml->sml_bvalues );
3364 slapi_ch_free( (void **)&ml );
3369 return LDAP_UNWILLING_TO_PERFORM;
3374 * Synthesise an LDAPMod array from a Modifications list to pass
3375 * to SLAPI. This synthesis is destructive and as such the
3376 * Modifications list may not be used after calling this
3379 * This function must also be called before slap_mods_check().
3381 LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
3384 Modifications *ml, *modlist;
3385 LDAPMod **mods, *modp;
3388 modlist = *pmodlist;
3390 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3393 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3395 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3396 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3398 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3400 /* Take ownership of original type. */
3401 modp->mod_type = ml->sml_type.bv_val;
3402 ml->sml_type.bv_val = NULL;
3404 if ( ml->sml_bvalues != NULL ) {
3405 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
3407 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3408 sizeof(struct berval *) );
3409 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
3410 /* Take ownership of original values. */
3411 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3412 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
3413 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
3414 ml->sml_bvalues[j].bv_len = 0;
3415 ml->sml_bvalues[j].bv_val = NULL;
3417 modp->mod_bvalues[j] = NULL;
3419 modp->mod_bvalues = NULL;
3426 slap_mods_free( modlist );
3436 * Convert a potentially modified array of LDAPMods back to a
3437 * Modification list.
3439 * The returned Modification list contains pointers into the
3440 * LDAPMods array; the latter MUST be freed with
3441 * slapi_x_free_ldapmods() (see below).
3443 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
3446 Modifications *modlist = NULL, **modtail;
3451 for( modp = mods; *modp != NULL; modp++ ) {
3455 struct berval **bvp;
3457 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3458 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3459 mod->sml_type.bv_val = (*modp)->mod_type;
3460 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3461 mod->sml_desc = NULL;
3462 mod->sml_next = NULL;
3464 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3465 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3468 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3473 mod->sml_bvalues = NULL;
3475 mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3477 /* NB: This implicitly trusts a plugin to return valid modifications. */
3478 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3479 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3480 mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
3481 mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
3484 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3485 mod->sml_bvalues[i].bv_val = *p;
3486 mod->sml_bvalues[i].bv_len = strlen( *p );
3489 mod->sml_bvalues[i].bv_val = NULL;
3491 mod->sml_nvalues = NULL;
3494 modtail = &mod->sml_next;
3504 * This function only frees the parts of the mods array that
3505 * are not shared with the Modification list that was created
3506 * by slapi_x_ldapmods2modifications().
3509 void slapi_x_free_ldapmods (LDAPMod **mods)
3517 for ( i = 0; mods[i] != NULL; i++ ) {
3519 * Don't free values themselves; they're owned by the
3520 * Modification list. Do free the containing array.
3522 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3523 for ( j = 0; mods[i]->mod_bvalues != NULL && mods[i]->mod_bvalues[j] != NULL; j++ ) {
3524 ch_free( mods[i]->mod_bvalues[j] );
3526 ch_free( mods[i]->mod_bvalues );
3528 ch_free( mods[i]->mod_values );
3530 /* Don't free type, for same reasons. */
3534 #endif /* LDAP_SLAPI */
3538 * Sun ONE DS 5.x computed attribute support. Computed attributes
3539 * allow for dynamically generated operational attributes, a very
3540 * useful thing indeed.
3544 * Write the computed attribute to a BerElement. Complementary
3545 * functions need to be defined for anything that replaces
3546 * op->o_callback->sc_sendentry, if you wish to make computed
3547 * attributes available to it.
3549 int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
3552 Operation *op = NULL;
3554 AttributeDescription *desc = NULL;
3570 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
3571 if ( rc != 0 || op == NULL ) {
3575 ber = (BerElement *)c->cac_private;
3578 if ( c->cac_attrs == NULL ) {
3579 /* All attrs request, skip operational attributes */
3580 if ( is_at_operational( desc->ad_type ) ) {
3584 /* Specific attrs requested */
3585 if ( is_at_operational( desc->ad_type ) ) {
3586 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3590 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3596 if ( !access_allowed( op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
3597 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
3598 "acl: access to attribute %s not allowed\n",
3599 desc->ad_cname.bv_val );
3603 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
3605 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3606 "ber_printf failed\n");
3610 if ( !c->cac_attrsonly ) {
3611 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
3612 if ( !access_allowed( op, e,
3613 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
3614 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
3615 "slapi_x_compute_output_ber: conn %lu "
3616 "acl: access to %s, value %d not allowed\n",
3617 op->o_connid, desc->ad_cname.bv_val, i );
3621 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
3622 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3623 "ber_printf failed\n");
3629 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
3630 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3631 "ber_printf failed\n" );
3642 * For some reason Sun don't use the normal plugin mechanism
3643 * registration path to register an "evaluator" function (an
3644 * "evaluator" is responsible for adding computed attributes;
3645 * the nomenclature is somewhat confusing).
3647 * As such slapi_compute_add_evaluator() registers the
3648 * function directly.
3650 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3653 Slapi_PBlock *pPlugin = NULL;
3656 pPlugin = slapi_pblock_new();
3657 if ( pPlugin == NULL ) {
3658 rc = LDAP_NO_MEMORY;
3662 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3663 if ( rc != LDAP_SUCCESS ) {
3667 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3668 if ( rc != LDAP_SUCCESS ) {
3672 rc = insertPlugin( NULL, pPlugin );
3679 if ( rc != LDAP_SUCCESS ) {
3680 if ( pPlugin != NULL ) {
3681 slapi_pblock_destroy( pPlugin );
3689 #endif /* LDAP_SLAPI */
3693 * See notes above regarding slapi_compute_add_evaluator().
3695 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3698 Slapi_PBlock *pPlugin = NULL;
3701 pPlugin = slapi_pblock_new();
3702 if ( pPlugin == NULL ) {
3703 rc = LDAP_NO_MEMORY;
3707 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3708 if ( rc != LDAP_SUCCESS ) {
3712 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3713 if ( rc != LDAP_SUCCESS ) {
3717 rc = insertPlugin( NULL, pPlugin );
3724 if ( rc != LDAP_SUCCESS ) {
3725 if ( pPlugin != NULL ) {
3726 slapi_pblock_destroy( pPlugin );
3734 #endif /* LDAP_SLAPI */
3738 * Call compute evaluators
3740 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3744 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3746 rc = getAllPluginFuncs( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3747 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3748 /* Nothing to do; front-end should ignore. */
3752 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3754 * -1: no attribute matched requested type
3755 * 0: one attribute matched
3756 * >0: error happened
3758 rc = (*pGetPlugin)( c, type, e, outputfn );
3764 slapi_ch_free( (void **)&tmpPlugin );
3769 #endif /* LDAP_SLAPI */
3772 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3778 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3783 return doPluginFNs( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3786 #endif /* LDAP_SLAPI */
3790 * New API to provide the plugin with access to the search
3791 * pblock. Have informed Sun DS team.
3793 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3799 if ( c->cac_pb == NULL )
3807 #endif /* LDAP_SLAPI */
3810 Slapi_Mutex *slapi_new_mutex( void )
3815 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3816 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3817 slapi_ch_free( (void **)&m );
3827 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3830 if ( mutex != NULL ) {
3831 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3832 slapi_ch_free( (void **)&mutex);
3837 void slapi_lock_mutex( Slapi_Mutex *mutex )
3840 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3844 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3847 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3853 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3858 if ( mutex == NULL ) {
3862 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3863 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3864 slapi_ch_free( (void **)&cv );
3868 /* XXX struct copy */
3869 cv->mutex = mutex->mutex;
3877 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3880 if ( cvar != NULL ) {
3881 ldap_pvt_thread_cond_destroy( &cvar->cond );
3882 slapi_ch_free( (void **)&cvar );
3887 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3890 if ( cvar == NULL ) {
3894 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3900 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3903 if ( cvar == NULL ) {
3908 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3911 return ldap_pvt_thread_cond_signal( &cvar->cond );
3917 int slapi_x_access_allowed( Operation *op,
3919 AttributeDescription *desc,
3921 slap_access_t access,
3922 AccessControlState *state )
3925 int rc, slap_access = 0;
3926 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3928 if ( op->o_pb == NULL ) {
3929 /* internal operation */
3935 slap_access |= SLAPI_ACL_ADD | SLAPI_ACL_DELETE | SLAPI_ACL_WRITE;
3938 slap_access |= SLAPI_ACL_READ;
3941 slap_access |= SLAPI_ACL_SEARCH;
3944 slap_access = ACL_COMPARE;
3950 rc = getAllPluginFuncs( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
3951 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3952 /* nothing to do; allowed access */
3956 slapi_x_pblock_set_operation( op->o_pb, op );
3958 rc = 1; /* default allow policy */
3960 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3965 rc = (*pGetPlugin)( op->o_pb, entry, desc->ad_cname.bv_val,
3966 val, slap_access, (void *)state );
3972 slapi_ch_free( (void **)&tmpPlugin );
3977 #endif /* LDAP_SLAPI */