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 */
926 slapi_dn_ignore_case( char *dn )
929 return slapi_dn_normalize_case( dn );
930 #else /* LDAP_SLAPI */
932 #endif /* LDAP_SLAPI */
936 slapi_ch_malloc( unsigned long size )
939 return ch_malloc( size );
940 #else /* LDAP_SLAPI */
942 #endif /* LDAP_SLAPI */
946 slapi_ch_free( void **ptr )
951 #endif /* LDAP_SLAPI */
955 slapi_ch_free_string( char **ptr )
958 slapi_ch_free( (void **)ptr );
959 #endif /* LDAP_SLAPI */
963 slapi_ch_array_free( char **arrayp )
968 if ( arrayp != NULL ) {
969 for ( p = arrayp; *p != NULL; p++ ) {
970 slapi_ch_free( (void **)p );
972 slapi_ch_free( (void **)&arrayp );
978 slapi_ch_bvdup(const struct berval *v)
983 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
984 bv->bv_len = v->bv_len;
985 bv->bv_val = slapi_ch_malloc( bv->bv_len );
986 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
995 slapi_ch_bvecdup(const struct berval **v)
1005 for ( i = 0; v[i] != NULL; i++ )
1008 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1010 for ( i = 0; v[i] != NULL; i++ ) {
1011 rv[i] = slapi_ch_bvdup( v[i] );
1023 unsigned long nelem,
1024 unsigned long size )
1027 return ch_calloc( nelem, size );
1028 #else /* LDAP_SLAPI */
1030 #endif /* LDAP_SLAPI */
1036 unsigned long size )
1039 return ch_realloc( block, size );
1040 #else /* LDAP_SLAPI */
1042 #endif /* LDAP_SLAPI */
1046 slapi_ch_strdup( char *s )
1049 return ch_strdup( (const char *)s );
1050 #else /* LDAP_SLAPI */
1052 #endif /* LDAP_SLAPI */
1056 slapi_ch_stlen( char *s )
1059 return strlen( (const char *)s );
1060 #else /* LDAP_SLAPI */
1062 #endif /* LDAP_SLAPI */
1066 slapi_control_present(
1067 LDAPControl **controls,
1069 struct berval **val,
1084 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1085 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1090 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1092 * FIXME: according to 6.1 specification,
1093 * "The val output parameter is set
1094 * to point into the controls array.
1095 * A copy of the control value is
1099 struct berval *pTmpBval;
1101 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
1102 if ( pTmpBval == NULL ) {
1105 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
1106 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
1110 slapi_ch_free( (void **)&pTmpBval );
1116 *val = &controls[i]->ldctl_value;
1121 *iscritical = controls[i]->ldctl_iscritical;
1128 #else /* LDAP_SLAPI */
1130 #endif /* LDAP_SLAPI */
1135 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1136 unsigned long *slapi_mask)
1138 *slapi_mask = SLAPI_OPERATION_NONE;
1140 if ( slap_mask & SLAP_CTRL_ABANDON )
1141 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1143 if ( slap_mask & SLAP_CTRL_ADD )
1144 *slapi_mask |= SLAPI_OPERATION_ADD;
1146 if ( slap_mask & SLAP_CTRL_BIND )
1147 *slapi_mask |= SLAPI_OPERATION_BIND;
1149 if ( slap_mask & SLAP_CTRL_COMPARE )
1150 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1152 if ( slap_mask & SLAP_CTRL_DELETE )
1153 *slapi_mask |= SLAPI_OPERATION_DELETE;
1155 if ( slap_mask & SLAP_CTRL_MODIFY )
1156 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1158 if ( slap_mask & SLAP_CTRL_RENAME )
1159 *slapi_mask |= SLAPI_OPERATION_MODDN;
1161 if ( slap_mask & SLAP_CTRL_SEARCH )
1162 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1164 if ( slap_mask & SLAP_CTRL_UNBIND )
1165 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1169 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1170 slap_mask_t *slap_mask)
1174 if ( slapi_mask & SLAPI_OPERATION_BIND )
1175 *slap_mask |= SLAP_CTRL_BIND;
1177 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1178 *slap_mask |= SLAP_CTRL_UNBIND;
1180 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1181 *slap_mask |= SLAP_CTRL_SEARCH;
1183 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1184 *slap_mask |= SLAP_CTRL_MODIFY;
1186 if ( slapi_mask & SLAPI_OPERATION_ADD )
1187 *slap_mask |= SLAP_CTRL_ADD;
1189 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1190 *slap_mask |= SLAP_CTRL_DELETE;
1192 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1193 *slap_mask |= SLAP_CTRL_RENAME;
1195 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1196 *slap_mask |= SLAP_CTRL_COMPARE;
1198 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1199 *slap_mask |= SLAP_CTRL_ABANDON;
1201 *slap_mask |= SLAP_CTRL_FRONTEND;
1210 /* Plugins must deal with controls themselves. */
1212 return LDAP_SUCCESS;
1214 #endif /* LDAP_SLAPI */
1217 slapi_register_supported_control(
1219 unsigned long controlops )
1222 slap_mask_t controlmask;
1224 slapiControlOp2SlapControlMask( controlops, &controlmask );
1226 register_supported_control( controloid, controlmask, NULL, parseSlapiControl );
1227 #endif /* LDAP_SLAPI */
1231 slapi_get_supported_controls(
1233 unsigned long **ctrlopsp )
1238 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1239 if ( rc != LDAP_SUCCESS ) {
1243 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1244 /* In place, naughty. */
1245 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1248 return LDAP_SUCCESS;
1249 #else /* LDAP_SLAPI */
1251 #endif /* LDAP_SLAPI */
1255 slapi_dup_control( LDAPControl *ctrl )
1260 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1261 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1262 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1263 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1268 #endif /* LDAP_SLAPI */
1272 slapi_register_supported_saslmechanism( char *mechanism )
1275 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1276 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
1277 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1278 #endif /* LDAP_SLAPI */
1282 slapi_get_supported_saslmechanisms( void )
1285 /* FIXME -- can not get the saslmechanism without a connection. */
1286 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
1287 "can not get the SASL mechanism list "
1288 "without a connection\n" );
1290 #else /* LDAP_SLAPI */
1292 #endif /* LDAP_SLAPI */
1296 slapi_get_supported_extended_ops( void )
1300 char **ppExtOpOID = NULL;
1303 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1307 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
1312 if ( numExtOps == 0 ) {
1316 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1317 for ( k = 0; k < i; k++ ) {
1320 bv = get_supported_extop( k );
1321 assert( bv != NULL );
1323 ppExtOpOID[ k ] = bv->bv_val;
1326 for ( ; k < j; k++ ) {
1329 bv = ns_get_supported_extop( k );
1330 assert( bv != NULL );
1332 ppExtOpOID[ i + k ] = bv->bv_val;
1334 ppExtOpOID[ i + k ] = NULL;
1337 #else /* LDAP_SLAPI */
1339 #endif /* LDAP_SLAPI */
1343 slapi_send_ldap_result(
1349 struct berval **urls )
1354 char *extOID = NULL;
1355 struct berval *extValue = NULL;
1357 SlapReply rs = { REP_RESULT };
1359 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1362 rs.sr_matched = matched;
1367 slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
1369 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1370 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
1371 send_ldap_sasl( op, &rs );
1375 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1376 if ( extOID != NULL ) {
1377 rs.sr_rspoid = extOID;
1378 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
1379 send_ldap_extended( op, &rs );
1383 if (op->o_tag == LDAP_REQ_SEARCH)
1384 rs.sr_nentries = nentries;
1386 send_ldap_result( op, &rs );
1387 #endif /* LDAP_SLAPI */
1391 slapi_send_ldap_search_entry(
1394 LDAPControl **ectrls,
1400 SlapReply rs = { REP_RESULT };
1402 AttributeName *an = NULL;
1405 if ( attrs != NULL ) {
1406 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1414 an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
1415 for ( i = 0; attrs[i] != NULL; i++ ) {
1416 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1417 an[i].an_name.bv_len = strlen( attrs[i] );
1418 an[i].an_desc = NULL;
1419 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1422 an[i].an_name.bv_len = 0;
1423 an[i].an_name.bv_val = NULL;
1426 rs.sr_err = LDAP_SUCCESS;
1427 rs.sr_matched = NULL;
1430 rs.sr_ctrls = ectrls;
1435 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1439 return send_search_entry( pOp, &rs );
1440 #else /* LDAP_SLAPI */
1442 #endif /* LDAP_SLAPI */
1447 slapi_str2filter( char *str )
1450 return str2filter( str );
1451 #else /* LDAP_SLAPI */
1453 #endif /* LDAP_SLAPI */
1463 #endif /* LDAP_SLAPI */
1467 slapi_filter_dup( Slapi_Filter *filter )
1472 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1474 f->f_choice = filter->f_choice;
1476 switch ( f->f_choice ) {
1477 case LDAP_FILTER_AND:
1478 case LDAP_FILTER_NOT:
1479 case LDAP_FILTER_OR: {
1480 Filter *pFilter, **ppF;
1482 for ( pFilter = filter->f_list, ppF = &f->f_list;
1484 pFilter = pFilter->f_next, ppF = &f->f_next )
1486 *ppF = slapi_filter_dup( pFilter );
1492 case LDAP_FILTER_PRESENT:
1493 f->f_desc = filter->f_desc;
1495 case LDAP_FILTER_EQUALITY:
1496 case LDAP_FILTER_GE:
1497 case LDAP_FILTER_LE:
1498 case LDAP_FILTER_APPROX:
1499 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1500 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1501 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1503 case LDAP_FILTER_EXT:
1504 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1505 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1506 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1507 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1508 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1509 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1511 case LDAP_FILTER_SUBSTRINGS: {
1514 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1515 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1516 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1517 if ( filter->f_sub_any != NULL ) {
1518 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1520 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1521 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1522 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1524 f->f_sub_any[i].bv_val = NULL;
1526 f->f_sub_any = NULL;
1528 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1531 case SLAPD_FILTER_COMPUTED:
1532 f->f_result = filter->f_result;
1535 slapi_ch_free( (void **)&f );
1543 #endif /* LDAP_SLAPI */
1547 slapi_filter_get_choice( Slapi_Filter *f )
1559 #else /* LDAP_SLAPI */
1560 return -1; /* invalid filter type */
1561 #endif /* LDAP_SLAPI */
1565 slapi_filter_get_ava(
1568 struct berval **bval )
1572 int rc = LDAP_SUCCESS;
1574 assert( type != NULL );
1575 assert( bval != NULL );
1580 ftype = f->f_choice;
1581 if ( ftype == LDAP_FILTER_EQUALITY
1582 || ftype == LDAP_FILTER_GE
1583 || ftype == LDAP_FILTER_LE
1584 || ftype == LDAP_FILTER_APPROX ) {
1586 * According to the SLAPI Reference Manual these are
1589 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1590 *bval = &f->f_un.f_un_ava->aa_value;
1591 } else { /* filter type not supported */
1596 #else /* LDAP_SLAPI */
1598 #endif /* LDAP_SLAPI */
1602 slapi_filter_list_first( Slapi_Filter *f )
1611 ftype = f->f_choice;
1612 if ( ftype == LDAP_FILTER_AND
1613 || ftype == LDAP_FILTER_OR
1614 || ftype == LDAP_FILTER_NOT ) {
1615 return (Slapi_Filter *)f->f_list;
1619 #else /* LDAP_SLAPI */
1621 #endif /* LDAP_SLAPI */
1625 slapi_filter_list_next(
1627 Slapi_Filter *fprev )
1636 ftype = f->f_choice;
1637 if ( ftype == LDAP_FILTER_AND
1638 || ftype == LDAP_FILTER_OR
1639 || ftype == LDAP_FILTER_NOT )
1641 return fprev->f_next;
1645 #else /* LDAP_SLAPI */
1647 #endif /* LDAP_SLAPI */
1651 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1658 switch ( f->f_choice ) {
1659 case LDAP_FILTER_GE:
1660 case LDAP_FILTER_LE:
1661 case LDAP_FILTER_EQUALITY:
1662 case LDAP_FILTER_APPROX:
1663 *type = f->f_av_desc->ad_cname.bv_val;
1665 case LDAP_FILTER_SUBSTRINGS:
1666 *type = f->f_sub_desc->ad_cname.bv_val;
1668 case LDAP_FILTER_PRESENT:
1669 *type = f->f_desc->ad_cname.bv_val;
1671 case LDAP_FILTER_EXT:
1672 *type = f->f_mr_desc->ad_cname.bv_val;
1675 /* Complex filters need not apply. */
1683 #endif /* LDAP_SLAPI */
1687 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1688 char ***any, char **final )
1693 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1698 * The caller shouldn't free but we can't return an
1699 * array of char *s from an array of bervals without
1700 * allocating memory, so we may as well be consistent.
1703 *type = f->f_sub_desc->ad_cname.bv_val;
1704 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1705 if ( f->f_sub_any != NULL ) {
1706 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1708 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1709 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1710 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1716 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1721 #endif /* LDAP_SLAPI */
1725 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1728 Slapi_Filter *f = NULL;
1730 if ( ftype == LDAP_FILTER_AND ||
1731 ftype == LDAP_FILTER_OR ||
1732 ftype == LDAP_FILTER_NOT )
1734 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1735 f->f_choice = ftype;
1737 f->f_list->f_next = f2;
1744 #endif /* LDAP_SLAPI */
1748 slapi_x_filter_append( int ftype,
1749 Slapi_Filter **pContainingFilter, /* NULL on first call */
1750 Slapi_Filter **pNextFilter,
1751 Slapi_Filter *filterToAppend )
1754 if ( ftype == LDAP_FILTER_AND ||
1755 ftype == LDAP_FILTER_OR ||
1756 ftype == LDAP_FILTER_NOT )
1758 if ( *pContainingFilter == NULL ) {
1759 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
1760 (*pContainingFilter)->f_choice = ftype;
1761 (*pContainingFilter)->f_list = filterToAppend;
1762 (*pContainingFilter)->f_next = NULL;
1764 if ( (*pContainingFilter)->f_choice != ftype ) {
1768 (*pNextFilter)->f_next = filterToAppend;
1770 *pNextFilter = filterToAppend;
1774 #endif /* LDAP_SLAPI */
1779 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1787 /* spec says return zero if no filter. */
1791 if ( verify_access ) {
1792 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
1794 return LDAP_PARAM_ERROR;
1800 * According to acl.c it is safe to call test_filter() with
1803 rc = test_filter( op, e, f );
1805 case LDAP_COMPARE_TRUE:
1808 case LDAP_COMPARE_FALSE:
1810 case SLAPD_COMPARE_UNDEFINED:
1813 case LDAP_PROTOCOL_ERROR:
1814 /* filter type unknown: spec says return -1 */
1822 #endif /* LDAP_SLAPI */
1826 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
1829 return slapi_filter_test( NULL, e, f, 0 );
1836 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
1839 switch ( f->f_choice ) {
1840 case LDAP_FILTER_AND:
1841 case LDAP_FILTER_NOT:
1842 case LDAP_FILTER_OR: {
1846 * FIXME: altering f; should we use a temporary?
1848 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1849 rc = slapi_filter_apply( f, fn, arg, error_code );
1853 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
1859 case LDAP_FILTER_EQUALITY:
1860 case LDAP_FILTER_SUBSTRINGS:
1861 case LDAP_FILTER_GE:
1862 case LDAP_FILTER_LE:
1863 case LDAP_FILTER_PRESENT:
1864 case LDAP_FILTER_APPROX:
1865 case LDAP_FILTER_EXT:
1866 *error_code = fn( f, arg );
1869 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1872 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
1873 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
1879 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1881 #endif /* LDAP_SLAPI */
1885 slapi_send_ldap_extended_response(
1890 struct berval *response )
1895 rs.sr_err = errornum;
1896 rs.sr_matched = NULL;
1900 rs.sr_rspoid = respName;
1901 rs.sr_rspdata = response;
1903 send_ldap_extended( op, &rs );
1905 return LDAP_SUCCESS;
1906 #else /* LDAP_SLAPI */
1908 #endif /* LDAP_SLAPI */
1913 struct berval **vals,
1918 * FIXME: what's the point?
1921 #else /* LDAP_SLAPI */
1923 #endif /* LDAP_SLAPI */
1926 #define MAX_HOSTNAME 512
1929 slapi_get_hostname( void )
1935 * FIXME: I'd prefer a different check ...
1938 hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1940 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1941 "can't malloc memory for hostname\n" );
1944 } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
1945 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1946 "can't get hostname\n" );
1947 slapi_ch_free( (void **)&hn );
1951 static int been_here = 0;
1952 static char *static_hn = NULL;
1954 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1956 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1957 if ( static_hn == NULL) {
1958 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1959 "can't malloc memory for hostname\n" );
1961 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1966 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1967 slapi_log_error( SLAPI_LOG_FATAL,
1969 "can't get hostname\n" );
1970 slapi_ch_free( (void **)&static_hn );
1972 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1981 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1983 hn = ch_strdup( static_hn );
1984 #endif /* !_SPARC */
1987 #else /* LDAP_SLAPI */
1989 #endif /* LDAP_SLAPI */
1993 * FIXME: this should go in an appropriate header ...
1995 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
2005 int rc = LDAP_SUCCESS;
2008 va_start( arglist, fmt );
2009 rc = vLogError( severity, subsystem, fmt, arglist );
2013 #else /* LDAP_SLAPI */
2015 #endif /* LDAP_SLAPI */
2020 slapi_timer_current_time( void )
2023 static int first_time = 1;
2024 #if !defined (_WIN32)
2028 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
2031 gettimeofday( &base_time, NULL );
2033 gettimeofday( &now, NULL );
2034 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
2035 (now.tv_usec - base_time.tv_usec);
2036 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
2042 return (slap_get_time() - starttime) * 1000000;
2049 performance_counter_present = QueryPerformanceCounter( &base_time );
2050 QueryPerformanceFrequency( &performance_freq );
2053 if ( !performance_counter_present )
2056 QueryPerformanceCounter( &now );
2057 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2059 #else /* LDAP_SLAPI */
2061 #endif /* LDAP_SLAPI */
2068 slapi_timer_get_time( char *label )
2071 unsigned long start = slapi_timer_current_time();
2072 printf("%10ld %10ld usec %s\n", start, 0, label);
2074 #else /* LDAP_SLAPI */
2076 #endif /* LDAP_SLAPI */
2083 slapi_timer_elapsed_time(
2085 unsigned long start )
2088 unsigned long stop = slapi_timer_current_time();
2089 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2090 #endif /* LDAP_SLAPI */
2094 slapi_free_search_results_internal( Slapi_PBlock *pb )
2097 Slapi_Entry **entries;
2098 int k = 0, nEnt = 0;
2100 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2101 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2106 if ( entries == NULL ) {
2110 for ( k = 0; k < nEnt; k++ ) {
2111 slapi_entry_free( entries[k] );
2114 slapi_ch_free( (void **)&entries );
2115 #endif /* LDAP_SLAPI */
2120 * Internal API to prime a Slapi_PBlock with a Backend.
2122 static int initBackendPB( Slapi_PBlock *pb, Backend *be )
2126 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2127 if ( rc != LDAP_SUCCESS )
2131 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2132 if ( rc != LDAP_SUCCESS )
2136 return LDAP_SUCCESS;
2140 * If oldStyle is TRUE, then a value suitable for setting to
2141 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
2142 * (pointer to static storage).
2144 * If oldStyle is FALSE, then a value suitable for setting to
2145 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2146 * a pointer to allocated memory and will include the SASL
2147 * mechanism (if any).
2149 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2154 switch ( authz->sai_method ) {
2155 case LDAP_AUTH_SASL:
2157 authType = SLAPD_AUTH_SASL;
2159 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2160 authType = slapi_ch_malloc( len );
2161 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2164 case LDAP_AUTH_SIMPLE:
2165 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2167 case LDAP_AUTH_NONE:
2168 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2174 if ( is_tls && authType == NULL ) {
2175 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2182 * Internal API to prime a Slapi_PBlock with a Connection.
2184 static int initConnectionPB( Slapi_PBlock *pb, Connection *conn )
2189 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2190 if ( rc != LDAP_SUCCESS )
2193 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2194 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2195 if ( rc != LDAP_SUCCESS )
2197 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2198 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2199 if ( rc != LDAP_SUCCESS )
2203 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2204 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2205 if ( rc != LDAP_SUCCESS )
2207 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2208 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2209 if ( rc != LDAP_SUCCESS )
2213 #ifdef LDAP_CONNECTIONLESS
2214 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2215 if ( rc != LDAP_SUCCESS )
2219 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2220 if ( rc != LDAP_SUCCESS )
2223 /* Returns pointer to static string */
2224 connAuthType = Authorization2AuthType( &conn->c_authz,
2231 if ( connAuthType != NULL ) {
2232 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2233 if ( rc != LDAP_SUCCESS )
2237 /* Returns pointer to allocated string */
2238 connAuthType = Authorization2AuthType( &conn->c_authz,
2245 if ( connAuthType != NULL ) {
2246 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2247 if ( rc != LDAP_SUCCESS )
2251 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
2252 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
2253 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
2254 if ( rc != LDAP_SUCCESS )
2258 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SSF, (void *)conn->c_ssf);
2259 if ( rc != LDAP_SUCCESS )
2262 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SASL_CONTEXT,
2263 ( conn->c_sasl_authctx != NULL ? conn->c_sasl_authctx :
2264 conn->c_sasl_sockctx ) );
2265 if ( rc != LDAP_SUCCESS )
2270 #endif /* LDAP_SLAPI */
2273 * Internal API to prime a Slapi_PBlock with an Operation.
2275 int slapi_x_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
2283 if ( op->o_bd != NULL ) {
2284 isRoot = be_isroot( op->o_bd, &op->o_ndn );
2285 isUpdateDn = be_isupdate( op->o_bd, &op->o_ndn );
2288 rc = initBackendPB( pb, op->o_bd );
2289 if ( rc != LDAP_SUCCESS )
2292 rc = initConnectionPB( pb, op->o_conn );
2293 if ( rc != LDAP_SUCCESS )
2296 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2297 if ( rc != LDAP_SUCCESS )
2300 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2301 if ( rc != LDAP_SUCCESS )
2304 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2305 if ( rc != LDAP_SUCCESS )
2308 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2309 if ( rc != LDAP_SUCCESS )
2312 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2313 if ( rc != LDAP_SUCCESS )
2316 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2317 if ( rc != LDAP_SUCCESS )
2320 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2321 if ( rc != LDAP_SUCCESS)
2324 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2325 if ( rc != LDAP_SUCCESS )
2328 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2329 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
2330 /* Not quite sure what the point of this is. */
2331 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
2332 if ( rc != LDAP_SUCCESS )
2336 return LDAP_SUCCESS;
2342 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2347 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2349 *isSSL = conn->c_is_tls;
2354 return LDAP_SUCCESS;
2357 #endif /* LDAP_SLAPI */
2361 * DS 5.x compatability API follow
2364 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2370 return LDAP_PARAM_ERROR;
2372 at = attr->a_desc->ad_type;
2374 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2376 if ( is_at_single_value( at ) )
2377 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2378 if ( is_at_operational( at ) )
2379 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2380 if ( is_at_obsolete( at ) )
2381 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2382 if ( is_at_collective( at ) )
2383 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2384 if ( is_at_no_user_mod( at ) )
2385 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2387 return LDAP_SUCCESS;
2390 #endif /* LDAP_SLAPI */
2393 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2396 unsigned long flags;
2398 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2400 return (flags & flag) ? 1 : 0;
2403 #endif /* LDAP_SLAPI */
2406 Slapi_Attr *slapi_attr_new( void )
2411 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2419 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2423 AttributeDescription *ad = NULL;
2425 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2441 void slapi_attr_free( Slapi_Attr **a )
2449 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2452 return attr_dup( (Slapi_Attr *)attr );
2458 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2462 * FIXME: here we may lose alignment between a_vals/a_nvals
2464 return value_add_one( &a->a_vals, (Slapi_Value *)v );
2470 int slapi_attr_type2plugin( const char *type, void **pi )
2477 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2480 if ( attr == NULL ) {
2481 return LDAP_PARAM_ERROR;
2484 *type = attr->a_desc->ad_cname.bv_val;
2486 return LDAP_SUCCESS;
2492 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2495 if ( attr == NULL ) {
2496 return LDAP_PARAM_ERROR;
2498 *oidp = attr->a_desc->ad_type->sat_oid;
2500 return LDAP_SUCCESS;
2506 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2514 mr = a->a_desc->ad_type->sat_equality;
2515 rc = value_match( &ret, a->a_desc, mr,
2516 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2517 (struct berval *)v1, (void *)v2, &text );
2518 if ( rc != LDAP_SUCCESS )
2521 return ( ret == 0 ) ? 0 : -1;
2527 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2537 if ( a ->a_vals == NULL ) {
2540 mr = a->a_desc->ad_type->sat_equality;
2541 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2542 rc = value_match( &ret, a->a_desc, mr,
2543 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2544 if ( rc != LDAP_SUCCESS ) {
2551 #endif /* LDAP_SLAPI */
2555 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2558 AttributeDescription *a1 = NULL;
2559 AttributeDescription *a2 = NULL;
2563 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2567 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2571 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2572 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2573 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2576 case SLAPI_TYPE_CMP_EXACT:
2577 ret = ad_cmp( a1, a2 );
2579 case SLAPI_TYPE_CMP_BASE:
2580 ret = ad_base_cmp( a1, a2 );
2582 case SLAPI_TYPE_CMP_SUBTYPE:
2583 ret = is_ad_subtype( a2, a2 );
2596 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2599 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2605 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2608 return slapi_valueset_first_value( &a->a_vals, v );
2614 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2617 return slapi_valueset_next_value( &a->a_vals, hint, v );
2623 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2626 *numValues = slapi_valueset_count( &a->a_vals );
2634 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2637 *vs = &((Slapi_Attr *)a)->a_vals;
2645 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2648 return slapi_attr_get_values( a, vals );
2654 char *slapi_attr_syntax_normalize( const char *s )
2657 AttributeDescription *ad = NULL;
2660 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2664 return ad->ad_cname.bv_val;
2670 Slapi_Value *slapi_value_new( void )
2675 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2683 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2686 return ber_dupbv( NULL, (struct berval *)bval );
2692 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2695 return slapi_value_new_berval( v );
2701 Slapi_Value *slapi_value_new_string(const char *s)
2706 bv.bv_val = (char *)s;
2707 bv.bv_len = strlen( s );
2709 return slapi_value_new_berval( &bv );
2715 Slapi_Value *slapi_value_init(Slapi_Value *val)
2727 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2730 return ber_dupbv( v, bval );
2736 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2739 v->bv_val = slapi_ch_strdup( (char *)s );
2740 v->bv_len = strlen( s );
2748 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2751 return slapi_value_new_value( v );
2757 void slapi_value_free(Slapi_Value **value)
2760 if ( value == NULL ) {
2764 if ( (*value) != NULL ) {
2765 slapi_ch_free( (void **)&(*value)->bv_val );
2766 slapi_ch_free( (void **)value );
2771 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2780 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2783 if ( value == NULL ) {
2786 if ( value->bv_val != NULL ) {
2787 slapi_ch_free( (void **)&value->bv_val );
2789 slapi_value_init_berval( value, (struct berval *)bval );
2797 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2800 if ( value == NULL ) {
2803 return slapi_value_set_berval( value, vfrom );
2809 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2812 if ( value == NULL ) {
2815 if ( value->bv_val != NULL ) {
2816 slapi_ch_free( (void **)&value->bv_val );
2818 value->bv_val = slapi_ch_malloc( len );
2819 value->bv_len = len;
2820 AC_MEMCPY( value->bv_val, val, len );
2828 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2831 if ( value == NULL ) {
2834 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2841 int slapi_value_set_int(Slapi_Value *value, int intVal)
2846 snprintf( buf, sizeof( buf ), "%d", intVal );
2848 return slapi_value_set_string( value, buf );
2854 const char *slapi_value_get_string(const Slapi_Value *value)
2857 if ( value == NULL ) {
2860 return value->bv_val;
2867 static int checkBVString(const struct berval *bv)
2871 for ( i = 0; i < bv->bv_len; i++ ) {
2872 if ( bv->bv_val[i] == '\0' )
2875 if ( bv->bv_val[i] != '\0' )
2882 int slapi_value_get_int(const Slapi_Value *value)
2885 if ( value == NULL ) return 0;
2886 if ( value->bv_val == NULL ) return 0;
2887 if ( !checkBVString( value ) ) return 0;
2889 return (int)strtol( value->bv_val, NULL, 10 );
2895 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2898 if ( value == NULL ) return 0;
2899 if ( value->bv_val == NULL ) return 0;
2900 if ( !checkBVString( value ) ) return 0;
2902 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2908 long slapi_value_get_long(const Slapi_Value *value)
2911 if ( value == NULL ) return 0;
2912 if ( value->bv_val == NULL ) return 0;
2913 if ( !checkBVString( value ) ) return 0;
2915 return strtol( value->bv_val, NULL, 10 );
2921 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2924 if ( value == NULL ) return 0;
2925 if ( value->bv_val == NULL ) return 0;
2926 if ( !checkBVString( value ) ) return 0;
2928 return strtoul( value->bv_val, NULL, 10 );
2934 size_t slapi_value_get_length(const Slapi_Value *value)
2937 if ( value == NULL )
2940 return (size_t) value->bv_len;
2946 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2949 return slapi_attr_value_cmp( a, v1, v2 );
2955 /* A ValueSet is a container for a BerVarray. */
2956 Slapi_ValueSet *slapi_valueset_new( void )
2961 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2970 void slapi_valueset_free(Slapi_ValueSet *vs)
2976 ber_bvarray_free( vp );
2977 slapi_ch_free( (void **)&vp );
2984 void slapi_valueset_init(Slapi_ValueSet *vs)
2987 if ( vs != NULL && *vs == NULL ) {
2988 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2989 (*vs)->bv_val = NULL;
2995 void slapi_valueset_done(Slapi_ValueSet *vs)
3003 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
3005 slapi_ch_free( (void **)&vp->bv_val );
3007 /* but don't free *vs or vs */
3011 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
3016 ber_dupbv( &bv, (Slapi_Value *)addval );
3017 ber_bvarray_add( vs, &bv );
3021 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
3024 return slapi_valueset_next_value( vs, 0, v );
3030 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
3041 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
3052 int slapi_valueset_count( const Slapi_ValueSet *vs )
3063 for ( i = 0; vp[i].bv_val != NULL; i++ )
3073 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
3078 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
3079 slapi_valueset_add_value( vs1, vp );
3084 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
3085 struct berval *val, int access )
3092 slap_access_t slap_access;
3093 AttributeDescription *ad = NULL;
3096 ret = slap_str2ad( attr, &ad, &text );
3097 if ( ret != LDAP_SUCCESS ) {
3101 switch ( access & SLAPI_ACL_ALL ) {
3102 case SLAPI_ACL_COMPARE:
3103 slap_access = ACL_COMPARE;
3105 case SLAPI_ACL_SEARCH:
3106 slap_access = ACL_SEARCH;
3108 case SLAPI_ACL_READ:
3109 slap_access = ACL_READ;
3111 case SLAPI_ACL_WRITE:
3112 case SLAPI_ACL_DELETE:
3114 case SLAPI_ACL_SELF:
3115 slap_access = ACL_WRITE;
3118 return LDAP_INSUFFICIENT_ACCESS;
3122 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3123 return LDAP_PARAM_ERROR;
3126 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3127 return LDAP_PARAM_ERROR;
3130 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3131 return LDAP_PARAM_ERROR;
3134 ret = access_allowed( op, e, ad, val, slap_access, NULL );
3136 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3138 return LDAP_UNWILLING_TO_PERFORM;
3142 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
3146 int rc = LDAP_SUCCESS;
3147 Modifications *ml, *mp;
3149 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3150 return LDAP_PARAM_ERROR;
3153 ml = slapi_x_ldapmods2modifications( mods );
3158 for ( mp = ml; mp != NULL; mp = mp->sml_next ) {
3159 rc = slap_bv2ad( &mp->sml_type, &mp->sml_desc, (const char **)errbuf );
3160 if ( rc != LDAP_SUCCESS ) {
3165 if ( rc == LDAP_SUCCESS ) {
3166 rc = acl_check_modlist( op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3169 /* Careful when freeing the modlist because it has pointers into the mods array. */
3170 for ( ; ml != NULL; ml = mp ) {
3173 /* just free the containing array */
3174 slapi_ch_free( (void **)&ml->sml_bvalues );
3175 slapi_ch_free( (void **)&ml );
3180 return LDAP_UNWILLING_TO_PERFORM;
3185 * Synthesise an LDAPMod array from a Modifications list to pass
3186 * to SLAPI. This synthesis is destructive and as such the
3187 * Modifications list may not be used after calling this
3190 * This function must also be called before slap_mods_check().
3192 LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
3195 Modifications *ml, *modlist;
3196 LDAPMod **mods, *modp;
3199 modlist = *pmodlist;
3201 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3204 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3206 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3207 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3209 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3211 /* Take ownership of original type. */
3212 modp->mod_type = ml->sml_type.bv_val;
3213 ml->sml_type.bv_val = NULL;
3215 if ( ml->sml_bvalues != NULL ) {
3216 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
3218 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3219 sizeof(struct berval *) );
3220 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
3221 /* Take ownership of original values. */
3222 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3223 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
3224 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
3225 ml->sml_bvalues[j].bv_len = 0;
3226 ml->sml_bvalues[j].bv_val = NULL;
3228 modp->mod_bvalues[j] = NULL;
3230 modp->mod_bvalues = NULL;
3237 slap_mods_free( modlist );
3247 * Convert a potentially modified array of LDAPMods back to a
3248 * Modification list.
3250 * The returned Modification list contains pointers into the
3251 * LDAPMods array; the latter MUST be freed with
3252 * slapi_x_free_ldapmods() (see below).
3254 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
3257 Modifications *modlist = NULL, **modtail;
3262 for( modp = mods; *modp != NULL; modp++ ) {
3266 struct berval **bvp;
3268 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3269 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3270 mod->sml_type.bv_val = (*modp)->mod_type;
3271 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3272 mod->sml_desc = NULL;
3273 mod->sml_next = NULL;
3275 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3276 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3279 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3284 mod->sml_bvalues = NULL;
3286 mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3288 /* NB: This implicitly trusts a plugin to return valid modifications. */
3289 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3290 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3291 mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
3292 mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
3295 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3296 mod->sml_bvalues[i].bv_val = *p;
3297 mod->sml_bvalues[i].bv_len = strlen( *p );
3300 mod->sml_bvalues[i].bv_val = NULL;
3302 mod->sml_nvalues = NULL;
3305 modtail = &mod->sml_next;
3315 * This function only frees the parts of the mods array that
3316 * are not shared with the Modification list that was created
3317 * by slapi_x_ldapmods2modifications().
3320 void slapi_x_free_ldapmods (LDAPMod **mods)
3328 for ( i = 0; mods[i] != NULL; i++ ) {
3330 * Don't free values themselves; they're owned by the
3331 * Modification list. Do free the containing array.
3333 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3334 for ( j = 0; mods[i]->mod_bvalues != NULL && mods[i]->mod_bvalues[j] != NULL; j++ ) {
3335 ch_free( mods[i]->mod_bvalues[j] );
3337 ch_free( mods[i]->mod_bvalues );
3339 ch_free( mods[i]->mod_values );
3341 /* Don't free type, for same reasons. */
3345 #endif /* LDAP_SLAPI */
3349 * Sun ONE DS 5.x computed attribute support. Computed attributes
3350 * allow for dynamically generated operational attributes, a very
3351 * useful thing indeed.
3355 * Write the computed attribute to a BerElement. Complementary
3356 * functions need to be defined for anything that replaces
3357 * op->o_callback->sc_sendentry, if you wish to make computed
3358 * attributes available to it.
3360 int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
3363 Operation *op = NULL;
3365 AttributeDescription *desc = NULL;
3381 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
3382 if ( rc != 0 || op == NULL ) {
3386 ber = (BerElement *)c->cac_private;
3389 if ( c->cac_attrs == NULL ) {
3390 /* All attrs request, skip operational attributes */
3391 if ( is_at_operational( desc->ad_type ) ) {
3395 /* Specific attrs requested */
3396 if ( is_at_operational( desc->ad_type ) ) {
3397 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3401 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3407 if ( !access_allowed( op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
3408 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
3409 "acl: access to attribute %s not allowed\n",
3410 desc->ad_cname.bv_val );
3414 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
3416 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3417 "ber_printf failed\n");
3421 if ( !c->cac_attrsonly ) {
3422 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
3423 if ( !access_allowed( op, e,
3424 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
3425 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
3426 "slapi_x_compute_output_ber: conn %lu "
3427 "acl: access to %s, value %d not allowed\n",
3428 op->o_connid, desc->ad_cname.bv_val, i );
3432 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
3433 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3434 "ber_printf failed\n");
3440 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
3441 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3442 "ber_printf failed\n" );
3453 * For some reason Sun don't use the normal plugin mechanism
3454 * registration path to register an "evaluator" function (an
3455 * "evaluator" is responsible for adding computed attributes;
3456 * the nomenclature is somewhat confusing).
3458 * As such slapi_compute_add_evaluator() registers the
3459 * function directly.
3461 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3464 Slapi_PBlock *pPlugin = NULL;
3467 pPlugin = slapi_pblock_new();
3468 if ( pPlugin == NULL ) {
3469 rc = LDAP_NO_MEMORY;
3473 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3474 if ( rc != LDAP_SUCCESS ) {
3478 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3479 if ( rc != LDAP_SUCCESS ) {
3483 rc = insertPlugin( NULL, pPlugin );
3490 if ( rc != LDAP_SUCCESS ) {
3491 if ( pPlugin != NULL ) {
3492 slapi_pblock_destroy( pPlugin );
3500 #endif /* LDAP_SLAPI */
3504 * See notes above regarding slapi_compute_add_evaluator().
3506 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3509 Slapi_PBlock *pPlugin = NULL;
3512 pPlugin = slapi_pblock_new();
3513 if ( pPlugin == NULL ) {
3514 rc = LDAP_NO_MEMORY;
3518 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3519 if ( rc != LDAP_SUCCESS ) {
3523 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3524 if ( rc != LDAP_SUCCESS ) {
3528 rc = insertPlugin( NULL, pPlugin );
3535 if ( rc != LDAP_SUCCESS ) {
3536 if ( pPlugin != NULL ) {
3537 slapi_pblock_destroy( pPlugin );
3545 #endif /* LDAP_SLAPI */
3549 * Call compute evaluators
3551 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3555 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3557 rc = getAllPluginFuncs( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3558 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3559 /* Nothing to do; front-end should ignore. */
3563 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3565 * -1: no attribute matched requested type
3566 * 0: one attribute matched
3567 * >0: error happened
3569 rc = (*pGetPlugin)( c, type, e, outputfn );
3575 slapi_ch_free( (void **)&tmpPlugin );
3580 #endif /* LDAP_SLAPI */
3583 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3589 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3594 return doPluginFNs( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3597 #endif /* LDAP_SLAPI */
3601 * New API to provide the plugin with access to the search
3602 * pblock. Have informed Sun DS team.
3604 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3610 if ( c->cac_pb == NULL )
3618 #endif /* LDAP_SLAPI */
3621 Slapi_Mutex *slapi_new_mutex( void )
3626 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3627 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3628 slapi_ch_free( (void **)&m );
3638 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3641 if ( mutex != NULL ) {
3642 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3643 slapi_ch_free( (void **)&mutex);
3648 void slapi_lock_mutex( Slapi_Mutex *mutex )
3651 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3655 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3658 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3664 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3669 if ( mutex == NULL ) {
3673 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3674 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3675 slapi_ch_free( (void **)&cv );
3679 /* XXX struct copy */
3680 cv->mutex = mutex->mutex;
3688 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3691 if ( cvar != NULL ) {
3692 ldap_pvt_thread_cond_destroy( &cvar->cond );
3693 slapi_ch_free( (void **)&cvar );
3698 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3701 if ( cvar == NULL ) {
3705 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3711 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3714 if ( cvar == NULL ) {
3719 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3722 return ldap_pvt_thread_cond_signal( &cvar->cond );
3728 int slapi_x_access_allowed( Operation *op,
3730 AttributeDescription *desc,
3732 slap_access_t access,
3733 AccessControlState *state )
3736 int rc, slap_access = 0;
3737 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3739 if ( op->o_pb == NULL ) {
3740 /* internal operation */
3744 slapi_x_pblock_set_operation( op->o_pb, op );
3748 slap_access |= SLAPI_ACL_ADD | SLAPI_ACL_DELETE | SLAPI_ACL_WRITE;
3751 slap_access |= SLAPI_ACL_READ;
3754 slap_access |= SLAPI_ACL_SEARCH;
3757 slap_access = ACL_COMPARE;
3763 rc = getAllPluginFuncs( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
3764 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3765 /* nothing to do; allowed access */
3769 rc = 1; /* default allow policy */
3771 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3776 rc = (*pGetPlugin)( op->o_pb, entry, desc->ad_cname.bv_val,
3777 val, slap_access, (void *)state );
3783 slapi_ch_free( (void **)&tmpPlugin );
3788 #endif /* LDAP_SLAPI */