2 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * (C) Copyright IBM Corp. 1997,2002
7 * Redistribution and use in source and binary forms are permitted
8 * provided that this notice is preserved and that due credit is
9 * given to IBM Corporation. This software is provided ``as is''
10 * without express or implied warranty.
13 * Portions (C) Copyright PADL Software Pty Ltd.
14 * Redistribution and use in source and binary forms are permitted
15 * provided that this notice is preserved and that due credit is
16 * given to PADL Software Pty Ltd. This software is provided ``as is''
17 * without express or implied warranty.
21 #include "slapi_common.h"
23 #include <ac/string.h>
27 #include <ac/stdarg.h>
29 #include <ac/unistd.h>
32 struct berval *ns_get_supported_extop( int );
35 #include <sys/systeminfo.h>
41 * server start time (should we use a struct timeval also in slapd?
43 static struct timeval base_time;
44 ldap_pvt_thread_mutex_t slapi_hn_mutex;
45 ldap_pvt_thread_mutex_t slapi_time_mutex;
48 ldap_pvt_thread_mutex_t mutex;
51 struct slapi_condvar {
52 ldap_pvt_thread_cond_t cond;
53 ldap_pvt_thread_mutex_t mutex;
57 * This function converts an array of pointers to berval objects to
58 * an array of berval objects.
63 struct berval **bvptr,
66 int rc = LDAP_SUCCESS;
70 if ( bvptr == NULL || *bvptr == NULL ) {
74 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
78 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
79 if ( tmpberval == NULL ) {
80 return LDAP_NO_MEMORY;
83 for ( i = 0; bvptr[i] != NULL; i++ ) {
84 tmpberval[i].bv_val = bvptr[i]->bv_val;
85 tmpberval[i].bv_len = bvptr[i]->bv_len;
88 if ( rc == LDAP_SUCCESS ) {
100 #if defined(LDAP_SLAPI)
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 );
111 #else /* !defined(LDAP_SLAPI) */
113 #endif /* !defined(LDAP_SLAPI) */
121 #if defined(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 /* !defined(LDAP_SLAPI) */
131 #endif /* !defined(LDAP_SLAPI) */
135 slapi_entry_get_dn( Slapi_Entry *e )
137 #if defined(LDAP_SLAPI)
138 return e->e_name.bv_val;
139 #else /* !defined(LDAP_SLAPI) */
141 #endif /* !defined(LDAP_SLAPI) */
145 slapi_x_entry_get_id( Slapi_Entry *e )
147 #if defined(LDAP_SLAPI)
151 #endif /* !defined(LDAP_SLAPI) */
159 #if defined(LDAP_SLAPI)
160 struct berval dn = { 0, NULL };
163 dn.bv_len = strlen( ldn );
165 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
166 #endif /* defined(LDAP_SLAPI) */
170 slapi_entry_dup( Slapi_Entry *e )
172 #if defined(LDAP_SLAPI)
177 tmp = slapi_entry2str( e, &len );
179 return (Slapi_Entry *)NULL;
182 tmpEnt = (Slapi_Entry *)str2entry( tmp );
183 if ( tmpEnt == NULL ) {
184 slapi_ch_free( (void **)&tmp );
185 return (Slapi_Entry *)NULL;
189 slapi_ch_free( (void **)&tmp );
193 #else /* !defined(LDAP_SLAPI) */
195 #endif /* !defined(LDAP_SLAPI) */
199 slapi_entry_attr_delete(
203 #if defined(LDAP_SLAPI)
204 AttributeDescription *ad = NULL;
207 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
208 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
211 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
212 return 0; /* attribute is deleted */
214 return -1; /* something went wrong */
216 #else /* !defined(LDAP_SLAPI) */
218 #endif /* !defined(LDAP_SLAPI) */
222 slapi_entry_alloc( void )
224 #if defined(LDAP_SLAPI)
225 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
226 #else /* !defined(LDAP_SLAPI) */
228 #endif /* !defined(LDAP_SLAPI) */
232 slapi_entry_free( Slapi_Entry *e )
234 #if defined(LDAP_SLAPI)
236 #endif /* defined(LDAP_SLAPI) */
240 slapi_entry_attr_merge(
243 struct berval **vals )
245 #if defined(LDAP_SLAPI)
246 AttributeDescription *ad = NULL;
251 rc = bvptr2obj( vals, &bv );
252 if ( rc != LDAP_SUCCESS ) {
256 rc = slap_str2ad( type, &ad, &text );
257 if ( rc != LDAP_SUCCESS ) {
261 rc = attr_merge( e, ad, bv );
265 #else /* !defined(LDAP_SLAPI) */
267 #endif /* !defined(LDAP_SLAPI) */
271 slapi_entry_attr_find(
276 #if defined(LDAP_SLAPI)
277 AttributeDescription *ad = NULL;
281 rc = slap_str2ad( type, &ad, &text );
282 if ( rc != LDAP_SUCCESS ) {
286 *attr = attr_find( e->e_attrs, ad );
287 if ( *attr == NULL ) {
292 #else /* !defined(LDAP_SLAPI) */
294 #endif /* !defined(LDAP_SLAPI) */
298 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
301 AttributeDescription *ad = NULL;
306 rc = slap_str2ad( type, &ad, &text );
307 if ( rc != LDAP_SUCCESS ) {
311 attr = attr_find( e->e_attrs, ad );
312 if ( attr == NULL ) {
316 if ( attr->a_vals != NULL && attr->a_vals[0].bv_val != NULL ) {
317 return slapi_ch_strdup( attr->a_vals[0].bv_val );
327 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
330 AttributeDescription *ad = NULL;
335 rc = slap_str2ad( type, &ad, &text );
336 if ( rc != LDAP_SUCCESS ) {
340 attr = attr_find( e->e_attrs, ad );
341 if ( attr == NULL ) {
345 return slapi_value_get_int( attr->a_vals );
352 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
355 AttributeDescription *ad = NULL;
360 rc = slap_str2ad( type, &ad, &text );
361 if ( rc != LDAP_SUCCESS ) {
365 attr = attr_find( e->e_attrs, ad );
366 if ( attr == NULL ) {
370 return slapi_value_get_long( attr->a_vals );
377 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
380 AttributeDescription *ad = NULL;
385 rc = slap_str2ad( type, &ad, &text );
386 if ( rc != LDAP_SUCCESS ) {
390 attr = attr_find( e->e_attrs, ad );
391 if ( attr == NULL ) {
395 return slapi_value_get_uint( attr->a_vals );
402 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
405 AttributeDescription *ad = NULL;
410 rc = slap_str2ad( type, &ad, &text );
411 if ( rc != LDAP_SUCCESS ) {
415 attr = attr_find( e->e_attrs, ad );
416 if ( attr == NULL ) {
420 return slapi_value_get_ulong( attr->a_vals );
427 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
431 AttributeDescription *ad;
436 rc = slap_str2ad( type, &ad, &text );
437 if ( rc != LDAP_SUCCESS ) {
441 attr = attr_find( e->e_attrs, ad );
442 if ( attr == NULL ) {
446 bv.bv_val = (char *)value;
447 bv.bv_len = strlen( value );
449 return slapi_attr_value_find( attr, &bv );
456 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
459 return slapi_entry_attr_merge( e, (char *)type, vals );
466 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
475 return ( *attr != NULL ) ? 0 : -1;
482 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
489 if ( prevattr == NULL ) {
493 *attr = prevattr->a_next;
495 return ( *attr != NULL ) ? 0 : -1;
502 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
505 AttributeDescription *ad;
510 rc = slap_str2ad( type, &ad, &text );
511 if ( rc != LDAP_SUCCESS ) {
515 attr_delete( &e->e_attrs, ad );
517 rc = bvptr2obj( vals, &bv );
518 if ( rc != LDAP_SUCCESS ) {
522 rc = attr_merge( e, ad, bv );
523 slapi_ch_free( (void **)&bv );
524 if ( rc != LDAP_SUCCESS ) {
531 #endif /* LDAP_SLAPI */
535 * FIXME -- The caller must free the allocated memory.
536 * In Netscape they do not have to.
539 slapi_attr_get_values(
541 struct berval ***vals )
543 #if defined(LDAP_SLAPI)
547 if ( attr == NULL ) {
551 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
555 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
556 for ( j = 0; j < i; j++ ) {
557 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
561 *vals = (struct berval **)bv;
564 #else /* !defined(LDAP_SLAPI) */
566 #endif /* !defined(LDAP_SLAPI) */
570 slapi_dn_normalize( char *dn )
572 #if defined(LDAP_SLAPI)
576 assert( dn != NULL );
579 bdn.bv_len = strlen( dn );
581 dnNormalize2( NULL, &bdn, &ndn );
584 * FIXME: ain't it safe to set dn = ndn.bv_val ?
586 dn = ch_strdup( ndn.bv_val );
587 ch_free( ndn.bv_val );
590 #else /* !defined(LDAP_SLAPI) */
592 #endif /* !defined(LDAP_SLAPI) */
596 * FIXME: this function is dangerous and should be deprecated;
597 * DN normalization is a lot more than lower-casing, and BTW
598 * OpenLDAP's DN normalization for case insensitive attributes
599 * is already lower case
602 slapi_dn_normalize_case( char *dn )
604 #if defined(LDAP_SLAPI)
605 slapi_dn_normalize( dn );
606 ldap_pvt_str2lower( dn );
609 #else /* defined(LDAP_SLAPI) */
611 #endif /* defined(LDAP_SLAPI) */
619 #if defined(LDAP_SLAPI)
620 struct berval bdn, ndn;
621 struct berval bsuffix, nsuffix;
623 assert( dn != NULL );
624 assert( suffix != NULL );
627 bdn.bv_len = strlen( dn );
629 bsuffix.bv_val = suffix;
630 bsuffix.bv_len = strlen( suffix );
632 dnNormalize2( NULL, &bdn, &ndn );
633 dnNormalize2( NULL, &bsuffix, &nsuffix );
635 return dnIsSuffix( &ndn, &nsuffix );
636 #else /* !defined(LDAP_SLAPI) */
638 #endif /* !defined(LDAP_SLAPI) */
642 slapi_dn_ignore_case( char *dn )
644 #if defined(LDAP_SLAPI)
645 return slapi_dn_normalize_case( dn );
646 #else /* !defined(LDAP_SLAPI) */
648 #endif /* !defined(LDAP_SLAPI) */
652 slapi_ch_malloc( unsigned long size )
654 #if defined(LDAP_SLAPI)
655 return ch_malloc( size );
656 #else /* !defined(LDAP_SLAPI) */
658 #endif /* !defined(LDAP_SLAPI) */
662 slapi_ch_free( void **ptr )
664 #if defined(LDAP_SLAPI)
667 #endif /* defined(LDAP_SLAPI) */
671 slapi_ch_free_string( char **ptr )
673 #if defined(LDAP_SLAPI)
674 slapi_ch_free( (void **)ptr );
675 #endif /* defined(LDAP_SLAPI) */
679 slapi_ch_array_free( char **arrayp )
684 if ( arrayp != NULL ) {
685 for ( p = arrayp; *p != NULL; p++ ) {
686 slapi_ch_free( (void **)p );
688 slapi_ch_free( (void **)&arrayp );
694 slapi_ch_bvdup(const struct berval *v)
699 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
700 bv->bv_len = v->bv_len;
701 bv->bv_val = slapi_ch_malloc( bv->bv_len );
702 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
711 slapi_ch_bvecdup(const struct berval **v)
721 for ( i = 0; v[i] != NULL; i++ )
724 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
726 for ( i = 0; v[i] != NULL; i++ ) {
727 rv[i] = slapi_ch_bvdup( v[i] );
742 #if defined(LDAP_SLAPI)
743 return ch_calloc( nelem, size );
744 #else /* !defined(LDAP_SLAPI) */
746 #endif /* !defined(LDAP_SLAPI) */
754 #if defined(LDAP_SLAPI)
755 return ch_realloc( block, size );
756 #else /* !defined(LDAP_SLAPI) */
758 #endif /* !defined(LDAP_SLAPI) */
762 slapi_ch_strdup( char *s )
764 #if defined(LDAP_SLAPI)
765 return ch_strdup( (const char *)s );
766 #else /* !defined(LDAP_SLAPI) */
768 #endif /* !defined(LDAP_SLAPI) */
772 slapi_ch_stlen( char *s )
774 #if defined(LDAP_SLAPI)
775 return strlen( (const char *)s );
776 #else /* !defined(LDAP_SLAPI) */
778 #endif /* !defined(LDAP_SLAPI) */
782 slapi_control_present(
783 LDAPControl **controls,
788 #if defined(LDAP_SLAPI)
800 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
801 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
806 if ( controls[i]->ldctl_value.bv_len != 0 ) {
808 * FIXME: according to 6.1 specification,
809 * "The val output parameter is set
810 * to point into the controls array.
811 * A copy of the control value is
815 struct berval *pTmpBval;
817 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
818 if ( pTmpBval == NULL ) {
821 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
822 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
826 slapi_ch_free( (void **)&pTmpBval );
832 *val = &controls[i]->ldctl_value;
837 *iscritical = controls[i]->ldctl_iscritical;
844 #else /* !defined(LDAP_SLAPI) */
846 #endif /* !defined(LDAP_SLAPI) */
850 slapi_register_supported_control(
852 unsigned long controlops )
854 #if defined(LDAP_SLAPI)
855 /* FIXME -- can not add controls to OpenLDAP dynamically */
856 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_CONTROLS",
857 "OpenLDAP does not support dynamic registration of LDAP controls\n" );
858 #endif /* defined(LDAP_SLAPI) */
862 slapi_get_supported_controls(
864 unsigned long **ctrlopsp )
866 #if defined(LDAP_SLAPI)
870 unsigned long *masks = NULL;
872 for (n = 0; get_supported_ctrl( n ) != NULL; n++) {
884 oids = (char **)slapi_ch_malloc( (n + 1) * sizeof(char *) );
885 if ( oids == NULL ) {
890 masks = (unsigned long *)slapi_ch_malloc( n * sizeof(int) );
891 if ( masks == NULL ) {
896 for ( i = 0; i < n; i++ ) {
898 * FIXME: Netscape's specification says nothing about
899 * memory; should we copy the OIDs or return pointers
900 * to internal values? In OpenLDAP the latter is safe
901 * since we do not allow to register coltrols runtime
903 oids[ i ] = ch_strdup( get_supported_ctrl( i ) );
904 if ( oids[ i ] == NULL ) {
908 masks[ i ] = (unsigned long)get_supported_ctrl_mask( i );
916 if ( rc != LDAP_SUCCESS ) {
917 for ( i = 0; oids != NULL && oids[ i ] != NULL; i++ ) {
918 ch_free( oids[ i ] );
925 #else /* !defined(LDAP_SLAPI) */
927 #endif /* !defined(LDAP_SLAPI) */
931 slapi_register_supported_saslmechanism( char *mechanism )
933 #if defined(LDAP_SLAPI)
934 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
935 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
936 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
937 #endif /* defined(LDAP_SLAPI) */
941 slapi_get_supported_saslmechanisms( void )
943 #if defined(LDAP_SLAPI)
944 /* FIXME -- can not get the saslmechanism wihtout a connection. */
945 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
946 "can not get the saslmechanism "
947 "wihtout a connection\n" );
949 #else /* defined(LDAP_SLAPI) */
951 #endif /* defined(LDAP_SLAPI) */
955 slapi_get_supported_extended_ops( void )
957 #if defined(LDAP_SLAPI)
959 char **ppExtOpOID = NULL;
962 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
966 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
971 if ( numExtOps == 0 ) {
975 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
976 for ( k = 0; k < i; k++ ) {
979 bv = get_supported_extop( k );
980 assert( bv != NULL );
982 ppExtOpOID[ k ] = bv->bv_val;
985 for ( ; k < j; k++ ) {
988 bv = ns_get_supported_extop( k );
989 assert( bv != NULL );
991 ppExtOpOID[ i + k ] = bv->bv_val;
993 ppExtOpOID[ i + k ] = NULL;
996 #else /* !defined(LDAP_SLAPI) */
998 #endif /* !defined(LDAP_SLAPI) */
1002 slapi_send_ldap_result(
1008 struct berval **urls )
1010 #if defined(LDAP_SLAPI)
1014 char *extOID = NULL;
1015 struct berval *extValue = NULL;
1018 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1019 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1020 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1021 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, &s );
1022 rc = LDAP_SASL_BIND_IN_PROGRESS;
1023 send_ldap_sasl( conn, op, rc, NULL, NULL, NULL, NULL, s );
1027 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1028 if ( extOID != NULL ) {
1029 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &extValue );
1030 slapi_send_ldap_extended_response( conn, op, err, extOID,
1035 send_ldap_result( conn, op, err, matched, text, NULL, NULL );
1036 #endif /* defined(LDAP_SLAPI) */
1040 slapi_send_ldap_search_entry(
1043 LDAPControl **ectrls,
1047 #if defined(LDAP_SLAPI)
1054 AttributeName *an = NULL;
1057 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1062 an = (AttributeName *) ch_malloc( i * sizeof(AttributeName) );
1063 for ( i = 0; attrs[i] != NULL; i++ ) {
1064 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1065 an[i].an_name.bv_len = strlen( attrs[i] );
1066 an[i].an_desc = NULL;
1067 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1072 if ( ( rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) ||
1073 ( rc = slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&pConn) != 0 ) ||
1074 ( rc = slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp) != 0 ) ) {
1077 rc = send_search_entry( be, pConn, pOp, e, an, attrsonly, NULL );
1082 #else /* !defined(LDAP_SLAPI) */
1084 #endif /* !defined(LDAP_SLAPI) */
1089 slapi_str2filter( char *str )
1091 #if defined(LDAP_SLAPI)
1092 return str2filter( str );
1093 #else /* !defined(LDAP_SLAPI) */
1095 #endif /* !defined(LDAP_SLAPI) */
1103 #if defined(LDAP_SLAPI)
1105 #endif /* defined(LDAP_SLAPI) */
1109 slapi_filter_get_choice( Slapi_Filter *f )
1111 #if defined(LDAP_SLAPI)
1121 #else /* !defined(LDAP_SLAPI) */
1122 return -1; /* invalid filter type */
1123 #endif /* !defined(LDAP_SLAPI) */
1127 slapi_filter_get_ava(
1130 struct berval **bval )
1132 #if defined(LDAP_SLAPI)
1134 int rc = LDAP_SUCCESS;
1136 assert( type != NULL );
1137 assert( bval != NULL );
1142 ftype = f->f_choice;
1143 if ( ftype == LDAP_FILTER_EQUALITY
1144 || ftype == LDAP_FILTER_GE
1145 || ftype == LDAP_FILTER_LE
1146 || ftype == LDAP_FILTER_APPROX ) {
1148 * According to the SLAPI Reference Manual these are
1151 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1152 *bval = &f->f_un.f_un_ava->aa_value;
1153 } else { /* filter type not supported */
1158 #else /* !defined(LDAP_SLAPI) */
1160 #endif /* !defined(LDAP_SLAPI) */
1164 slapi_filter_list_first( Slapi_Filter *f )
1166 #if defined(LDAP_SLAPI)
1173 ftype = f->f_choice;
1174 if ( ftype == LDAP_FILTER_AND
1175 || ftype == LDAP_FILTER_OR
1176 || ftype == LDAP_FILTER_NOT ) {
1177 return (Slapi_Filter *)f->f_and;
1181 #else /* !defined(LDAP_SLAPI) */
1183 #endif /* !defined(LDAP_SLAPI) */
1187 slapi_filter_list_next(
1189 Slapi_Filter *fprev )
1191 #if defined(LDAP_SLAPI)
1198 ftype = f->f_choice;
1199 if ( ftype == LDAP_FILTER_AND
1200 || ftype == LDAP_FILTER_OR
1201 || ftype == LDAP_FILTER_NOT )
1203 return fprev->f_next;
1207 #else /* !defined(LDAP_SLAPI) */
1209 #endif /* !defined(LDAP_SLAPI) */
1213 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1220 switch ( f->f_choice ) {
1221 case LDAP_FILTER_GE:
1222 case LDAP_FILTER_LE:
1223 case LDAP_FILTER_EQUALITY:
1224 case LDAP_FILTER_APPROX:
1225 *type = f->f_av_desc->ad_cname.bv_val;
1227 case LDAP_FILTER_SUBSTRINGS:
1228 *type = f->f_sub_desc->ad_cname.bv_val;
1230 case LDAP_FILTER_PRESENT:
1231 *type = f->f_desc->ad_cname.bv_val;
1233 case LDAP_FILTER_EXT:
1234 *type = f->f_mr_desc->ad_cname.bv_val;
1237 /* Complex filters need not apply. */
1245 #endif /* LDAP_SLAPI */
1249 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1250 char ***any, char **final )
1255 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1260 * The caller shouldn't free but we can't return an
1261 * array of char *s from an array of bervals without
1262 * allocating memory, so we may as well be consistent.
1265 *type = f->f_sub_desc->ad_cname.bv_val;
1266 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1267 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1269 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1270 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1271 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1274 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1279 #endif /* LDAP_SLAPI */
1283 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2)
1286 Slapi_Filter *f = NULL;
1288 if ( ftype == LDAP_FILTER_AND ||
1289 ftype == LDAP_FILTER_OR ||
1290 ftype == LDAP_FILTER_NOT )
1292 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1293 f->f_choice = ftype;
1301 #endif /* LDAP_SLAPI */
1305 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1315 /* spec says return zero if no filter. */
1319 if ( verify_access ) {
1320 (void) slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be);
1321 rc = slapi_pblock_get(pb, SLAPI_CONNECTION, (void *)&conn);
1323 return LDAP_PARAM_ERROR;
1325 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
1327 return LDAP_PARAM_ERROR;
1334 * According to acl.c it is safe to call test_filter() with
1337 rc = test_filter( be, conn, op, e, f );
1339 case LDAP_COMPARE_TRUE:
1342 case LDAP_COMPARE_FALSE:
1344 case SLAPD_COMPARE_UNDEFINED:
1347 case LDAP_PROTOCOL_ERROR:
1348 /* filter type unknown: spec says return -1 */
1356 #endif /* LDAP_SLAPI */
1360 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
1363 return slapi_filter_test( NULL, e, f, 0 );
1370 slapi_send_ldap_extended_response(
1375 struct berval *response )
1377 #if defined(LDAP_SLAPI)
1378 send_ldap_extended( conn,op, errornum, NULL, NULL, NULL,
1379 respName,response, NULL );
1380 return LDAP_SUCCESS;
1381 #else /* !defined(LDAP_SLAPI) */
1383 #endif /* !defined(LDAP_SLAPI) */
1388 struct berval **vals,
1391 #if defined(LDAP_SLAPI)
1393 * FIXME: what's the point?
1396 #else /* !defined(LDAP_SLAPI) */
1398 #endif /* !defined(LDAP_SLAPI) */
1402 slapi_get_hostname( void )
1404 #if defined(LDAP_SLAPI)
1408 * FIXME: I'd prefer a different check ...
1411 hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1413 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1414 "can't malloc memory for hostname\n" );
1417 } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
1418 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1419 "can't get hostname\n" );
1420 slapi_ch_free( (void **)&hn );
1424 static int been_here = 0;
1425 static char *static_hn = NULL;
1427 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1429 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1430 if ( static_hn == NULL) {
1431 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1432 "can't malloc memory for hostname\n" );
1434 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1439 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1440 slapi_log_error( SLAPI_LOG_FATAL,
1442 "can't get hostname\n" );
1443 slapi_ch_free( (void **)&static_hn );
1445 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1454 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1456 hn = ch_strdup( static_hn );
1457 #endif /* !_SPARC */
1460 #else /* !defined(LDAP_SLAPI) */
1462 #endif /* !defined(LDAP_SLAPI) */
1466 * FIXME: this should go in an appropriate header ...
1468 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
1477 #if defined(LDAP_SLAPI)
1478 int rc = LDAP_SUCCESS;
1481 va_start( arglist, fmt );
1482 rc = vLogError( severity, subsystem, fmt, arglist );
1486 #else /* !defined(LDAP_SLAPI) */
1488 #endif /* !defined(LDAP_SLAPI) */
1493 slapi_timer_current_time( void )
1495 #if defined(LDAP_SLAPI)
1496 static int first_time = 1;
1497 #if !defined (_WIN32)
1501 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1504 gettimeofday( &base_time, NULL );
1506 gettimeofday( &now, NULL );
1507 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1508 (now.tv_usec - base_time.tv_usec);
1509 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1515 return (slap_get_time() - starttime) * 1000000;
1522 performance_counter_present = QueryPerformanceCounter( &base_time );
1523 QueryPerformanceFrequency( &performance_freq );
1526 if ( !performance_counter_present )
1529 QueryPerformanceCounter( &now );
1530 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1532 #else /* !defined(LDAP_SLAPI) */
1534 #endif /* !defined(LDAP_SLAPI) */
1541 slapi_timer_get_time( char *label )
1543 #if defined(LDAP_SLAPI)
1544 unsigned long start = slapi_timer_current_time();
1545 printf("%10ld %10ld usec %s\n", start, 0, label);
1547 #else /* !defined(LDAP_SLAPI) */
1549 #endif /* !defined(LDAP_SLAPI) */
1556 slapi_timer_elapsed_time(
1558 unsigned long start )
1560 #if defined(LDAP_SLAPI)
1561 unsigned long stop = slapi_timer_current_time();
1562 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
1563 #endif /* defined(LDAP_SLAPI) */
1567 slapi_free_search_results_internal( Slapi_PBlock *pb )
1569 #if defined(LDAP_SLAPI)
1570 Slapi_Entry **entries;
1571 int k = 0, nEnt = 0;
1573 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
1574 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
1579 if ( entries == NULL ) {
1583 for ( k = 0; k < nEnt; k++ ) {
1584 slapi_entry_free( entries[k] );
1587 slapi_ch_free( (void **)&entries );
1588 #endif /* defined(LDAP_SLAPI) */
1592 * Internal API to prime a Slapi_PBlock with a Backend.
1594 int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
1596 #if defined(LDAP_SLAPI)
1599 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
1600 if ( rc != LDAP_SUCCESS )
1604 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
1605 if ( rc != LDAP_SUCCESS )
1609 return LDAP_SUCCESS;
1612 #endif /* defined(LDAP_SLAPI) */
1615 #if defined(LDAP_SLAPI)
1617 * If oldStyle is TRUE, then a value suitable for setting to
1618 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
1619 * (pointer to static storage).
1621 * If oldStyle is FALSE, then a value suitable for setting to
1622 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
1623 * a pointer to allocated memory and will include the SASL
1624 * mechanism (if any).
1626 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
1631 switch ( authz->sai_method ) {
1632 case LDAP_AUTH_SASL:
1634 authType = SLAPD_AUTH_SASL;
1636 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
1637 authType = slapi_ch_malloc( len );
1638 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
1641 case LDAP_AUTH_SIMPLE:
1642 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
1644 case LDAP_AUTH_NONE:
1645 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
1651 if ( is_tls && authType == NULL ) {
1652 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
1660 * Internal API to prime a Slapi_PBlock with a Connection.
1662 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
1664 #if defined(LDAP_SLAPI)
1668 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
1669 if ( rc != LDAP_SUCCESS )
1672 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
1673 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
1674 if ( rc != LDAP_SUCCESS )
1676 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
1677 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
1678 if ( rc != LDAP_SUCCESS )
1682 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
1683 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
1684 if ( rc != LDAP_SUCCESS )
1686 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
1687 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
1688 if ( rc != LDAP_SUCCESS )
1692 #ifdef LDAP_CONNECTIONLESS
1693 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
1694 if ( rc != LDAP_SUCCESS )
1698 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
1699 if ( rc != LDAP_SUCCESS )
1702 /* Returns pointer to static string */
1703 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
1704 if ( connAuthType != NULL ) {
1705 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
1706 if ( rc != LDAP_SUCCESS )
1710 /* Returns pointer to allocated string */
1711 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
1712 if ( connAuthType != NULL ) {
1713 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
1714 if ( rc != LDAP_SUCCESS )
1718 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
1719 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
1720 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
1721 if ( rc != LDAP_SUCCESS )
1728 #endif /* defined(LDAP_SLAPI) */
1732 * Internal API to prime a Slapi_PBlock with an Operation.
1734 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
1736 #if defined(LDAP_SLAPI)
1743 if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
1747 isRoot = be_isroot( be, &op->o_ndn );
1748 isUpdateDn = be_isupdate( be, &op->o_ndn );
1751 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
1752 if ( rc != LDAP_SUCCESS )
1755 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
1756 if ( rc != LDAP_SUCCESS )
1759 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
1760 if ( rc != LDAP_SUCCESS )
1763 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
1764 if ( rc != LDAP_SUCCESS )
1767 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
1768 if ( rc != LDAP_SUCCESS )
1771 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
1772 if ( rc != LDAP_SUCCESS )
1775 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
1776 if ( rc != LDAP_SUCCESS)
1779 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
1780 if ( rc != LDAP_SUCCESS )
1783 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
1784 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
1785 /* Not quite sure what the point of this is. */
1786 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
1787 if ( rc != LDAP_SUCCESS )
1791 return LDAP_SUCCESS;
1797 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
1799 #if defined( LDAP_SLAPI )
1802 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1803 *isSSL = conn->c_is_tls;
1805 return LDAP_SUCCESS;
1808 #endif /* defined(LDAP_SLAPI) */
1812 * DS 5.x compatability API follow
1815 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
1817 #if defined( LDAP_SLAPI )
1821 return LDAP_PARAM_ERROR;
1823 at = attr->a_desc->ad_type;
1825 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
1827 if ( is_at_single_value( at ) )
1828 *flags |= SLAPI_ATTR_FLAG_SINGLE;
1829 if ( is_at_operational( at ) )
1830 *flags |= SLAPI_ATTR_FLAG_OPATTR;
1831 if ( is_at_obsolete( at ) )
1832 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
1833 if ( is_at_collective( at ) )
1834 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
1835 if ( is_at_no_user_mod( at ) )
1836 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
1838 return LDAP_SUCCESS;
1841 #endif /* defined(LDAP_SLAPI) */
1844 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
1846 #if defined( LDAP_SLAPI )
1847 unsigned long flags;
1849 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
1851 return (flags & flag) ? 1 : 0;
1854 #endif /* defined(LDAP_SLAPI) */
1857 Slapi_Attr *slapi_attr_new( void )
1862 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
1870 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
1874 AttributeDescription *ad = NULL;
1876 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
1891 void slapi_attr_free( Slapi_Attr **a )
1899 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
1902 return attr_dup( (Slapi_Attr *)attr );
1908 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
1911 return value_add_one( &a->a_vals, (Slapi_Value *)v );
1917 int slapi_attr_type2plugin( const char *type, void **pi )
1924 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
1927 if ( attr == NULL ) {
1928 return LDAP_PARAM_ERROR;
1931 *type = attr->a_desc->ad_cname.bv_val;
1933 return LDAP_SUCCESS;
1939 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
1942 if ( attr == NULL ) {
1943 return LDAP_PARAM_ERROR;
1945 *oidp = attr->a_desc->ad_type->sat_oid;
1947 return LDAP_SUCCESS;
1953 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
1961 mr = a->a_desc->ad_type->sat_equality;
1962 rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
1963 (struct berval *)v1, (void *)v2, &text );
1964 if ( rc != LDAP_SUCCESS )
1967 return ( ret == 0 ) ? 0 : -1;
1973 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
1983 mr = a->a_desc->ad_type->sat_equality;
1984 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
1985 rc = value_match( &ret, a->a_desc, mr,
1986 SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
1987 if ( rc != LDAP_SUCCESS ) {
1998 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2001 AttributeDescription *a1 = NULL;
2002 AttributeDescription *a2 = NULL;
2006 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2010 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2014 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2015 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2016 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2019 case SLAPI_TYPE_CMP_EXACT:
2020 ret = ad_cmp( a1, a2 );
2022 case SLAPI_TYPE_CMP_BASE:
2023 ret = ad_base_cmp( a1, a2 );
2025 case SLAPI_TYPE_CMP_SUBTYPE:
2026 ret = is_ad_subtype( a2, a2 );
2039 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2042 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2048 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2051 return slapi_valueset_first_value( &a->a_vals, v );
2057 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2060 return slapi_valueset_next_value( &a->a_vals, hint, v );
2066 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2069 *numValues = slapi_valueset_count( &a->a_vals );
2077 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2080 *vs = &((Slapi_Attr *)a)->a_vals;
2088 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2091 return slapi_attr_get_values( a, vals );
2097 char *slapi_attr_syntax_normalize( const char *s )
2100 AttributeDescription *ad = NULL;
2103 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2107 return ad->ad_cname.bv_val;
2113 Slapi_Value *slapi_value_new( void )
2118 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2126 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2129 return ber_dupbv( NULL, (struct berval *)bval );
2135 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2138 return slapi_value_new_berval( v );
2144 Slapi_Value *slapi_value_new_string(const char *s)
2149 bv.bv_val = (char *)s;
2150 bv.bv_len = strlen( s );
2152 return slapi_value_new_berval( &bv );
2158 Slapi_Value *slapi_value_init(Slapi_Value *val)
2170 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2173 return ber_dupbv( v, bval );
2179 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2182 v->bv_val = slapi_ch_strdup( (char *)s );
2183 v->bv_len = strlen( s );
2191 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2194 return slapi_value_new_value( v );
2200 void slapi_value_free(Slapi_Value **value)
2203 if ( value == NULL ) {
2207 if ( (*value) != NULL ) {
2208 slapi_ch_free( (void **)&(*value)->bv_val );
2209 slapi_ch_free( (void **)value );
2214 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2223 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2226 if ( value == NULL ) {
2229 if ( value->bv_val != NULL ) {
2230 slapi_ch_free( (void **)&value->bv_val );
2232 slapi_value_init_berval( value, (struct berval *)bval );
2240 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2243 if ( value == NULL ) {
2246 return slapi_value_set_berval( value, vfrom );
2252 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2255 if ( value == NULL ) {
2258 if ( value->bv_val != NULL ) {
2259 slapi_ch_free( (void **)&value->bv_val );
2261 value->bv_val = slapi_ch_malloc( len );
2262 value->bv_len = len;
2263 AC_MEMCPY( value->bv_val, val, len );
2271 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2274 if ( value == NULL ) {
2277 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2284 int slapi_value_set_int(Slapi_Value *value, int intVal)
2289 snprintf( buf, sizeof( buf ), "%d", intVal );
2291 return slapi_value_set_string( value, buf );
2297 const char *slapi_value_get_string(const Slapi_Value *value)
2300 if ( value == NULL ) {
2303 return value->bv_val;
2310 static int checkBVString(const struct berval *bv)
2314 for ( i = 0; i < bv->bv_len; i++ ) {
2315 if ( bv->bv_val[i] == '\0' )
2318 if ( bv->bv_val[i] != '\0' )
2325 int slapi_value_get_int(const Slapi_Value *value)
2328 if ( value == NULL ) return 0;
2329 if ( value->bv_val == NULL ) return 0;
2330 if ( !checkBVString( value ) ) return 0;
2332 return (int)strtol( value->bv_val, NULL, 10 );
2338 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2341 if ( value == NULL ) return 0;
2342 if ( value->bv_val == NULL ) return 0;
2343 if ( !checkBVString( value ) ) return 0;
2345 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2351 long slapi_value_get_long(const Slapi_Value *value)
2354 if ( value == NULL ) return 0;
2355 if ( value->bv_val == NULL ) return 0;
2356 if ( !checkBVString( value ) ) return 0;
2358 return strtol( value->bv_val, NULL, 10 );
2364 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2367 if ( value == NULL ) return 0;
2368 if ( value->bv_val == NULL ) return 0;
2369 if ( !checkBVString( value ) ) return 0;
2371 return strtoul( value->bv_val, NULL, 10 );
2377 size_t slapi_value_get_length(const Slapi_Value *value)
2380 if ( value == NULL )
2383 return (size_t) value->bv_len;
2389 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2392 return slapi_attr_value_cmp( a, v1, v2 );
2398 /* A ValueSet is a container for a BerVarray. */
2399 Slapi_ValueSet *slapi_valueset_new( void )
2404 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2413 void slapi_valueset_free(Slapi_ValueSet *vs)
2419 ber_bvarray_free( vp );
2420 slapi_ch_free( (void **)&vp );
2427 void slapi_valueset_init(Slapi_ValueSet *vs)
2430 if ( vs != NULL && *vs == NULL ) {
2431 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2432 (*vs)->bv_val = NULL;
2438 void slapi_valueset_done(Slapi_ValueSet *vs)
2446 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
2448 slapi_ch_free( (void **)&vp->bv_val );
2450 /* but don't free *vs or vs */
2454 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2459 ber_dupbv( &bv, (Slapi_Value *)addval );
2460 ber_bvarray_add( vs, &bv );
2464 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2467 return slapi_valueset_next_value( vs, 0, v );
2473 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2484 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2495 int slapi_valueset_count( const Slapi_ValueSet *vs )
2506 for ( i = 0; vp[i].bv_val != NULL; i++ )
2516 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2521 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2522 slapi_valueset_add_value( vs1, vp );
2527 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
2528 struct berval *val, int access )
2535 slap_access_t slap_access;
2536 AttributeDescription *ad = NULL;
2539 ret = slap_str2ad( attr, &ad, &text );
2540 if ( ret != LDAP_SUCCESS ) {
2544 switch ( access & SLAPI_ACL_ALL ) {
2545 case SLAPI_ACL_COMPARE:
2546 slap_access = ACL_COMPARE;
2548 case SLAPI_ACL_SEARCH:
2549 slap_access = ACL_SEARCH;
2551 case SLAPI_ACL_READ:
2552 slap_access = ACL_READ;
2554 case SLAPI_ACL_WRITE:
2555 case SLAPI_ACL_DELETE:
2557 case SLAPI_ACL_SELF:
2558 slap_access = ACL_WRITE;
2561 return LDAP_INSUFFICIENT_ACCESS;
2565 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2566 return LDAP_PARAM_ERROR;
2569 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
2570 return LDAP_PARAM_ERROR;
2573 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
2574 return LDAP_PARAM_ERROR;
2577 ret = access_allowed( be, conn, op, e, desc, val, slap_access, NULL );
2579 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2581 return LDAP_UNWILLING_TO_PERFORM;
2585 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
2593 Modifications *next;
2595 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2596 return LDAP_PARAM_ERROR;
2599 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
2600 return LDAP_PARAM_ERROR;
2603 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
2604 return LDAP_PARAM_ERROR;
2607 ml = slapi_x_ldapmods2modifications( mods );
2612 ret = acl_check_modlist( be, conn, op, e, ml );
2614 /* Careful when freeing the modlist because it has pointers into the mods array. */
2615 for ( ; ml != NULL; ml = next ) {
2616 next = ml->sml_next;
2618 /* just free the containing array */
2619 slapi_ch_free( (void **)&ml->sml_bvalues );
2620 slapi_ch_free( (void **)&ml );
2623 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2625 return LDAP_UNWILLING_TO_PERFORM;
2630 * Synthesise an LDAPMod array from a Modifications list to pass
2631 * to SLAPI. This synthesis is destructive and as such the
2632 * Modifications list may not be used after calling this
2635 * This function must also be called before slap_mods_check().
2637 LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
2640 Modifications *ml, *modlist;
2641 LDAPMod **mods, *modp;
2644 modlist = *pmodlist;
2646 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
2649 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
2651 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
2652 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
2654 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
2656 /* Take ownership of original type. */
2657 modp->mod_type = ml->sml_type.bv_val;
2658 ml->sml_type.bv_val = NULL;
2660 if ( ml->sml_bvalues != NULL ) {
2661 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
2663 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
2664 sizeof(struct berval *) );
2665 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
2666 /* Take ownership of original values. */
2667 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
2668 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
2669 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
2670 ml->sml_bvalues[j].bv_len = 0;
2671 ml->sml_bvalues[j].bv_val = NULL;
2673 modp->mod_bvalues[j] = NULL;
2675 modp->mod_bvalues = NULL;
2682 slap_mods_free( modlist );
2692 * Convert a potentially modified array of LDAPMods back to a
2693 * Modification list.
2695 * The returned Modification list contains pointers into the
2696 * LDAPMods array; the latter MUST be freed with
2697 * slapi_x_free_ldapmods() (see below).
2699 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
2702 Modifications *modlist, **modtail;
2707 for( modp = mods; *modp != NULL; modp++ ) {
2711 struct berval **bvp;
2713 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
2714 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
2715 mod->sml_type.bv_val = (*modp)->mod_type;
2716 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
2717 mod->sml_desc = NULL;
2718 mod->sml_next = NULL;
2720 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
2721 for( i = 0, bvp = (*modp)->mod_bvalues; *bvp != NULL; bvp++, i++ )
2724 for( i = 0, p = (*modp)->mod_values; *p != NULL; p++, i++ )
2728 mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
2730 /* NB: This implicitly trusts a plugin to return valid modifications. */
2731 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
2732 for( i = 0, bvp = (*modp)->mod_bvalues; *bvp != NULL; bvp++, i++ ) {
2733 mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
2734 mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
2737 for( i = 0, p = (*modp)->mod_values; *p != NULL; p++, i++ ) {
2738 mod->sml_bvalues[i].bv_val = *p;
2739 mod->sml_bvalues[i].bv_len = strlen( *p );
2742 mod->sml_bvalues[i].bv_val = NULL;
2745 modtail = &mod->sml_next;
2755 * This function only frees the parts of the mods array that
2756 * are not shared with the Modification list that was created
2757 * by slapi_x_ldapmods2modifications().
2760 void slapi_x_free_ldapmods (LDAPMod **mods)
2768 for ( i = 0; mods[i] != NULL; i++ ) {
2770 * Don't free values themselves; they're owned by the
2771 * Modification list. Do free the containing array.
2773 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
2774 for ( j = 0; mods[i]->mod_bvalues[j] != NULL; j++ ) {
2775 ch_free( mods[i]->mod_bvalues[j] );
2777 ch_free( mods[i]->mod_bvalues );
2779 ch_free( mods[i]->mod_values );
2781 /* Don't free type, for same reasons. */
2785 #endif /* LDAP_SLAPI */
2789 * Sun ONE DS 5.x computed attribute support. Computed attributes
2790 * allow for dynamically generated operational attributes, a very
2791 * useful thing indeed.
2795 * Write the computed attribute to a BerElement. Complementary
2796 * functions need to be defined for anything that replaces
2797 * op->o_callback->sc_sendentry, if you wish to make computed
2798 * attributes available to it.
2800 int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
2804 Connection *conn = NULL;
2805 Operation *op = NULL;
2807 AttributeDescription *desc;
2823 rc = slapi_pblock_get( c->cac_pb, SLAPI_BACKEND, (void *)&be );
2825 be = NULL; /* no backend for root DSE */
2828 rc = slapi_pblock_get( c->cac_pb, SLAPI_CONNECTION, (void *)&conn );
2829 if ( rc != 0 || conn == NULL ) {
2833 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
2834 if ( rc != 0 || op == NULL ) {
2838 ber = (BerElement *)c->cac_private;
2841 if ( c->cac_attrs == NULL ) {
2842 /* All attrs request, skip operational attributes */
2843 if ( is_at_operational( desc->ad_type ) ) {
2847 /* Specific attrs requested */
2848 if ( is_at_operational( desc->ad_type ) ) {
2849 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
2853 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
2859 if ( !access_allowed( be, conn, op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
2860 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
2861 "acl: access to attribute %s not allowed\n",
2862 desc->ad_cname.bv_val );
2866 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
2868 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
2869 "ber_printf failed\n");
2873 if ( !c->cac_attrsonly ) {
2874 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
2875 if ( !access_allowed( be, conn, op, e,
2876 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
2877 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
2878 "slapi_x_compute_output_ber: conn %lu "
2879 "acl: access to %s, value %d not allowed\n",
2880 op->o_connid, desc->ad_cname.bv_val, i );
2884 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
2885 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
2886 "ber_printf failed\n");
2892 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
2893 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
2894 "ber_printf failed\n" );
2905 * For some reason Sun don't use the normal plugin mechanism
2906 * registration path to register an "evaluator" function (an
2907 * "evaluator" is responsible for adding computed attributes;
2908 * the nomenclature is somewhat confusing).
2910 * As such slapi_compute_add_evaluator() registers the
2911 * function directly.
2913 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
2916 Slapi_PBlock *pPlugin = NULL;
2919 pPlugin = slapi_pblock_new();
2920 if ( pPlugin == NULL ) {
2921 rc = LDAP_NO_MEMORY;
2925 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
2926 if ( rc != LDAP_SUCCESS ) {
2930 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
2931 if ( rc != LDAP_SUCCESS ) {
2935 rc = insertPlugin( NULL, pPlugin );
2942 if ( rc != LDAP_SUCCESS ) {
2943 if ( pPlugin != NULL ) {
2944 slapi_pblock_destroy( pPlugin );
2952 #endif /* LDAP_SLAPI */
2956 * See notes above regarding slapi_compute_add_evaluator().
2958 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
2961 Slapi_PBlock *pPlugin = NULL;
2964 pPlugin = slapi_pblock_new();
2965 if ( pPlugin == NULL ) {
2966 rc = LDAP_NO_MEMORY;
2970 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
2971 if ( rc != LDAP_SUCCESS ) {
2975 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
2976 if ( rc != LDAP_SUCCESS ) {
2980 rc = insertPlugin( NULL, pPlugin );
2987 if ( rc != LDAP_SUCCESS ) {
2988 if ( pPlugin != NULL ) {
2989 slapi_pblock_destroy( pPlugin );
2997 #endif /* LDAP_SLAPI */
3001 * Call compute evaluators
3003 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3007 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3009 rc = getAllPluginFuncs( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3010 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3011 /* Nothing to do; front-end should ignore. */
3015 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3017 * -1: no attribute matched requested type
3018 * 0: one attribute matched
3019 * >0: error happened
3021 rc = (*pGetPlugin)( c, type, e, outputfn );
3027 slapi_ch_free( (void **)&tmpPlugin );
3032 #endif /* LDAP_SLAPI */
3035 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3041 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3046 return doPluginFNs( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3049 #endif /* LDAP_SLAPI */
3053 * New API to provide the plugin with access to the search
3054 * pblock. Have informed Sun DS team.
3056 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3062 if ( c->cac_pb == NULL )
3070 #endif /* LDAP_SLAPI */
3073 Slapi_Mutex *slapi_new_mutex( void )
3078 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3079 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3080 slapi_ch_free( (void **)&m );
3090 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3093 if ( mutex != NULL ) {
3094 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3095 slapi_ch_free( (void **)&mutex);
3100 void slapi_lock_mutex( Slapi_Mutex *mutex )
3103 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3107 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3110 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3116 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3121 if ( mutex == NULL ) {
3125 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3126 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3127 slapi_ch_free( (void **)&cv );
3131 /* XXX struct copy */
3132 cv->mutex = mutex->mutex;
3140 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3143 if ( cvar != NULL ) {
3144 ldap_pvt_thread_cond_destroy( &cvar->cond );
3145 slapi_ch_free( (void **)&cvar );
3150 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3153 if ( cvar == NULL ) {
3157 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3163 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3166 if ( cvar == NULL ) {
3171 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3174 return ldap_pvt_thread_cond_signal( &cvar->cond );