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>
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 "can not add controls to openLDAP dynamically\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 "can not add saslmechanism to openLDAP dynamically\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_send_ldap_extended_response(
1218 struct berval *response )
1220 #if defined(LDAP_SLAPI)
1221 send_ldap_extended( conn,op, errornum, NULL, NULL, NULL,
1222 respName,response, NULL );
1223 return LDAP_SUCCESS;
1224 #else /* !defined(LDAP_SLAPI) */
1226 #endif /* !defined(LDAP_SLAPI) */
1231 struct berval **vals,
1234 #if defined(LDAP_SLAPI)
1236 * FIXME: what's the point?
1239 #else /* !defined(LDAP_SLAPI) */
1241 #endif /* !defined(LDAP_SLAPI) */
1245 slapi_get_hostname( void )
1247 #if defined(LDAP_SLAPI)
1251 * FIXME: I'd prefer a different check ...
1254 hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1256 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1257 "can't malloc memory for hostname\n" );
1260 } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
1261 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1262 "can't get hostname\n" );
1263 slapi_ch_free( (void **)&hn );
1267 static int been_here = 0;
1268 static char *static_hn = NULL;
1270 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1272 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1273 if ( static_hn == NULL) {
1274 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1275 "can't malloc memory for hostname\n" );
1277 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1282 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1283 slapi_log_error( SLAPI_LOG_FATAL,
1285 "can't get hostname\n" );
1286 slapi_ch_free( (void **)&static_hn );
1288 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1297 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1299 hn = ch_strdup( static_hn );
1300 #endif /* !_SPARC */
1303 #else /* !defined(LDAP_SLAPI) */
1305 #endif /* !defined(LDAP_SLAPI) */
1309 * FIXME: this should go in an appropriate header ...
1311 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
1320 #if defined(LDAP_SLAPI)
1321 int rc = LDAP_SUCCESS;
1324 va_start( arglist, fmt );
1325 rc = vLogError( severity, subsystem, fmt, arglist );
1329 #else /* !defined(LDAP_SLAPI) */
1331 #endif /* !defined(LDAP_SLAPI) */
1336 slapi_timer_current_time( void )
1338 #if defined(LDAP_SLAPI)
1339 static int first_time = 1;
1340 #if !defined (_WIN32)
1344 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1347 gettimeofday( &base_time, NULL );
1349 gettimeofday( &now, NULL );
1350 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1351 (now.tv_usec - base_time.tv_usec);
1352 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1358 return (slap_get_time() - starttime) * 1000000;
1365 performance_counter_present = QueryPerformanceCounter( &base_time );
1366 QueryPerformanceFrequency( &performance_freq );
1369 if ( !performance_counter_present )
1372 QueryPerformanceCounter( &now );
1373 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1375 #else /* !defined(LDAP_SLAPI) */
1377 #endif /* !defined(LDAP_SLAPI) */
1384 slapi_timer_get_time( char *label )
1386 #if defined(LDAP_SLAPI)
1387 unsigned long start = slapi_timer_current_time();
1388 printf("%10ld %10ld usec %s\n", start, 0, label);
1390 #else /* !defined(LDAP_SLAPI) */
1392 #endif /* !defined(LDAP_SLAPI) */
1399 slapi_timer_elapsed_time(
1401 unsigned long start )
1403 #if defined(LDAP_SLAPI)
1404 unsigned long stop = slapi_timer_current_time();
1405 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
1406 #endif /* defined(LDAP_SLAPI) */
1410 slapi_free_search_results_internal( Slapi_PBlock *pb )
1412 #if defined(LDAP_SLAPI)
1413 Slapi_Entry **entries;
1414 int k = 0, nEnt = 0;
1416 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
1417 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
1422 if ( entries == NULL ) {
1426 for ( k = 0; k < nEnt; k++ ) {
1427 slapi_entry_free( entries[k] );
1430 slapi_ch_free( (void **)&entries );
1431 #endif /* defined(LDAP_SLAPI) */
1435 * Internal API to prime a Slapi_PBlock with a Backend.
1437 int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
1439 #if defined(LDAP_SLAPI)
1442 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
1443 if ( rc != LDAP_SUCCESS )
1447 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
1448 if ( rc != LDAP_SUCCESS )
1452 return LDAP_SUCCESS;
1455 #endif /* defined(LDAP_SLAPI) */
1458 #if defined(LDAP_SLAPI)
1460 * If oldStyle is TRUE, then a value suitable for setting to
1461 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
1462 * (pointer to static storage).
1464 * If oldStyle is FALSE, then a value suitable for setting to
1465 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
1466 * a pointer to allocated memory and will include the SASL
1467 * mechanism (if any).
1469 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
1474 switch ( authz->sai_method ) {
1475 case LDAP_AUTH_SASL:
1477 authType = SLAPD_AUTH_SASL;
1479 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
1480 authType = slapi_ch_malloc( len );
1481 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
1484 case LDAP_AUTH_SIMPLE:
1485 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
1487 case LDAP_AUTH_NONE:
1488 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
1494 if ( is_tls && authType == NULL ) {
1495 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
1503 * Internal API to prime a Slapi_PBlock with a Connection.
1505 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
1507 #if defined(LDAP_SLAPI)
1511 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
1512 if ( rc != LDAP_SUCCESS )
1515 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
1516 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
1517 if ( rc != LDAP_SUCCESS )
1519 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
1520 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
1521 if ( rc != LDAP_SUCCESS )
1525 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
1526 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
1527 if ( rc != LDAP_SUCCESS )
1529 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
1530 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
1531 if ( rc != LDAP_SUCCESS )
1535 #ifdef LDAP_CONNECTIONLESS
1536 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
1537 if ( rc != LDAP_SUCCESS )
1541 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
1542 if ( rc != LDAP_SUCCESS )
1545 /* Returns pointer to static string */
1546 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
1547 if ( connAuthType != NULL ) {
1548 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
1549 if ( rc != LDAP_SUCCESS )
1553 /* Returns pointer to allocated string */
1554 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
1555 if ( connAuthType != NULL ) {
1556 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
1557 if ( rc != LDAP_SUCCESS )
1561 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
1562 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
1563 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
1564 if ( rc != LDAP_SUCCESS )
1571 #endif /* defined(LDAP_SLAPI) */
1575 * Internal API to prime a Slapi_PBlock with an Operation.
1577 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
1579 #if defined(LDAP_SLAPI)
1586 if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
1590 isRoot = be_isroot( be, &op->o_ndn );
1591 isUpdateDn = be_isupdate( be, &op->o_ndn );
1594 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
1595 if ( rc != LDAP_SUCCESS )
1598 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
1599 if ( rc != LDAP_SUCCESS )
1602 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
1603 if ( rc != LDAP_SUCCESS )
1606 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
1607 if ( rc != LDAP_SUCCESS )
1610 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
1611 if ( rc != LDAP_SUCCESS )
1614 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
1615 if ( rc != LDAP_SUCCESS )
1618 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
1619 if ( rc != LDAP_SUCCESS)
1622 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
1623 if ( rc != LDAP_SUCCESS )
1626 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
1627 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
1628 /* Not quite sure what the point of this is. */
1629 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
1630 if ( rc != LDAP_SUCCESS )
1634 return LDAP_SUCCESS;
1640 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
1642 #if defined( LDAP_SLAPI )
1645 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1646 *isSSL = conn->c_is_tls;
1648 return LDAP_SUCCESS;
1651 #endif /* defined(LDAP_SLAPI) */
1655 * DS 5.x compatability API follow
1658 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
1660 #if defined( LDAP_SLAPI )
1664 return LDAP_PARAM_ERROR;
1666 at = attr->a_desc->ad_type;
1668 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
1670 if ( is_at_single_value( at ) )
1671 *flags |= SLAPI_ATTR_FLAG_SINGLE;
1672 if ( is_at_operational( at ) )
1673 *flags |= SLAPI_ATTR_FLAG_OPATTR;
1674 if ( is_at_obsolete( at ) )
1675 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
1676 if ( is_at_collective( at ) )
1677 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
1678 if ( is_at_no_user_mod( at ) )
1679 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
1681 return LDAP_SUCCESS;
1684 #endif /* defined(LDAP_SLAPI) */
1687 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
1689 #if defined( LDAP_SLAPI )
1690 unsigned long flags;
1692 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
1694 return (flags & flag) ? 1 : 0;
1697 #endif /* defined(LDAP_SLAPI) */
1700 Slapi_Attr *slapi_attr_new( void )
1705 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
1713 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
1717 AttributeDescription *ad = NULL;
1719 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
1734 void slapi_attr_free( Slapi_Attr **a )
1742 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
1745 return attr_dup( (Slapi_Attr *)attr );
1751 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
1754 return value_add_one( &a->a_vals, (Slapi_Value *)v );
1760 int slapi_attr_type2plugin( const char *type, void **pi )
1767 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
1770 if ( attr == NULL ) {
1771 return LDAP_PARAM_ERROR;
1774 *type = attr->a_desc->ad_cname.bv_val;
1776 return LDAP_SUCCESS;
1782 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
1785 if ( attr == NULL ) {
1786 return LDAP_PARAM_ERROR;
1788 *oidp = attr->a_desc->ad_type->sat_oid;
1790 return LDAP_SUCCESS;
1796 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
1804 mr = a->a_desc->ad_type->sat_equality;
1805 rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
1806 (struct berval *)v1, (void *)v2, &text );
1807 if ( rc != LDAP_SUCCESS )
1810 return ( ret == 0 ) ? 0 : -1;
1816 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
1826 mr = a->a_desc->ad_type->sat_equality;
1827 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
1828 rc = value_match( &ret, a->a_desc, mr,
1829 SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
1830 if ( rc != LDAP_SUCCESS ) {
1841 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
1844 AttributeDescription *a1 = NULL;
1845 AttributeDescription *a2 = NULL;
1849 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
1853 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
1857 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
1858 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
1859 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
1862 case SLAPI_TYPE_CMP_EXACT:
1863 ret = ad_cmp( a1, a2 );
1865 case SLAPI_TYPE_CMP_BASE:
1866 ret = ad_base_cmp( a1, a2 );
1868 case SLAPI_TYPE_CMP_SUBTYPE:
1869 ret = is_ad_subtype( a2, a2 );
1882 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
1885 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
1891 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
1894 return slapi_valueset_first_value( &a->a_vals, v );
1900 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
1903 return slapi_valueset_next_value( &a->a_vals, hint, v );
1909 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
1912 *numValues = slapi_valueset_count( &a->a_vals );
1920 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
1923 *vs = &((Slapi_Attr *)a)->a_vals;
1931 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
1934 return slapi_attr_get_values( a, vals );
1940 char *slapi_attr_syntax_normalize( const char *s )
1943 AttributeDescription *ad = NULL;
1946 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
1950 return ad->ad_cname.bv_val;
1956 Slapi_Value *slapi_value_new( void )
1961 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
1969 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
1972 return ber_dupbv( NULL, (struct berval *)bval );
1978 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
1981 return slapi_value_new_berval( v );
1987 Slapi_Value *slapi_value_new_string(const char *s)
1992 bv.bv_val = (char *)s;
1993 bv.bv_len = strlen( s );
1995 return slapi_value_new_berval( &bv );
2001 Slapi_Value *slapi_value_init(Slapi_Value *val)
2013 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2016 return ber_dupbv( v, bval );
2022 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2025 v->bv_val = slapi_ch_strdup( (char *)s );
2026 v->bv_len = strlen( s );
2034 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2037 return slapi_value_new_value( v );
2043 void slapi_value_free(Slapi_Value **value)
2046 if ( value == NULL ) {
2050 if ( (*value) != NULL ) {
2051 slapi_ch_free( (void **)&(*value)->bv_val );
2052 slapi_ch_free( (void **)value );
2057 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2066 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2069 if ( value == NULL ) {
2072 if ( value->bv_val != NULL ) {
2073 slapi_ch_free( (void **)&value->bv_val );
2075 slapi_value_init_berval( value, (struct berval *)bval );
2083 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2086 if ( value == NULL ) {
2089 return slapi_value_set_berval( value, vfrom );
2095 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2098 if ( value == NULL ) {
2101 if ( value->bv_val != NULL ) {
2102 slapi_ch_free( (void **)&value->bv_val );
2104 value->bv_val = slapi_ch_malloc( len );
2105 value->bv_len = len;
2106 AC_MEMCPY( value->bv_val, val, len );
2114 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2117 if ( value == NULL ) {
2120 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2127 int slapi_value_set_int(Slapi_Value *value, int intVal)
2132 snprintf( buf, sizeof( buf ), "%d", intVal );
2134 return slapi_value_set_string( value, buf );
2140 const char *slapi_value_get_string(const Slapi_Value *value)
2143 if ( value == NULL ) {
2146 return value->bv_val;
2153 static int checkBVString(const struct berval *bv)
2157 for ( i = 0; i < bv->bv_len; i++ ) {
2158 if ( bv->bv_val[i] == '\0' )
2161 if ( bv->bv_val[i] != '\0' )
2168 int slapi_value_get_int(const Slapi_Value *value)
2171 if ( value == NULL ) return 0;
2172 if ( value->bv_val == NULL ) return 0;
2173 if ( !checkBVString( value ) ) return 0;
2175 return (int)strtol( value->bv_val, NULL, 10 );
2181 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2184 if ( value == NULL ) return 0;
2185 if ( value->bv_val == NULL ) return 0;
2186 if ( !checkBVString( value ) ) return 0;
2188 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2194 long slapi_value_get_long(const Slapi_Value *value)
2197 if ( value == NULL ) return 0;
2198 if ( value->bv_val == NULL ) return 0;
2199 if ( !checkBVString( value ) ) return 0;
2201 return strtol( value->bv_val, NULL, 10 );
2207 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2210 if ( value == NULL ) return 0;
2211 if ( value->bv_val == NULL ) return 0;
2212 if ( !checkBVString( value ) ) return 0;
2214 return strtoul( value->bv_val, NULL, 10 );
2220 size_t slapi_value_get_length(const Slapi_Value *value)
2223 if ( value == NULL )
2226 return (size_t) value->bv_len;
2232 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2235 return slapi_attr_value_cmp( a, v1, v2 );
2241 /* A ValueSet is a container for a BerVarray. */
2242 Slapi_ValueSet *slapi_valueset_new( void )
2247 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2256 void slapi_valueset_free(Slapi_ValueSet *vs)
2262 ber_bvarray_free( vp );
2263 slapi_ch_free( (void **)&vp );
2270 void slapi_valueset_init(Slapi_ValueSet *vs)
2273 if ( vs != NULL && *vs == NULL ) {
2274 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2275 (*vs)->bv_val = NULL;
2281 void slapi_valueset_done(Slapi_ValueSet *vs)
2289 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
2291 slapi_ch_free( (void **)&vp->bv_val );
2293 /* but don't free *vs or vs */
2297 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2302 ber_dupbv( &bv, (Slapi_Value *)addval );
2303 ber_bvarray_add( vs, &bv );
2307 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2310 return slapi_valueset_next_value( vs, 0, v );
2316 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2327 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2338 int slapi_valueset_count( const Slapi_ValueSet *vs )
2349 for ( i = 0; vp[i].bv_val != NULL; i++ )
2359 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2364 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2365 slapi_valueset_add_value( vs1, vp );
2370 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
2371 struct berval *val, int access )
2378 slap_access_t slap_access;
2379 AttributeDescription *ad = NULL;
2382 ret = slap_str2ad( attr, &ad, &text );
2383 if ( ret != LDAP_SUCCESS ) {
2387 switch ( access & SLAPI_ACL_ALL ) {
2388 case SLAPI_ACL_COMPARE:
2389 slap_access = ACL_COMPARE;
2391 case SLAPI_ACL_SEARCH:
2392 slap_access = ACL_SEARCH;
2394 case SLAPI_ACL_READ:
2395 slap_access = ACL_READ;
2397 case SLAPI_ACL_WRITE:
2398 case SLAPI_ACL_DELETE:
2400 case SLAPI_ACL_SELF:
2401 slap_access = ACL_WRITE;
2404 return LDAP_INSUFFICIENT_ACCESS;
2408 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2409 return LDAP_PARAM_ERROR;
2412 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
2413 return LDAP_PARAM_ERROR;
2416 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
2417 return LDAP_PARAM_ERROR;
2420 ret = access_allowed( be, conn, op, e, desc, val, slap_access, NULL );
2422 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2424 return LDAP_UNWILLING_TO_PERFORM;
2428 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
2436 Modifications *next;
2438 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2439 return LDAP_PARAM_ERROR;
2442 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
2443 return LDAP_PARAM_ERROR;
2446 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
2447 return LDAP_PARAM_ERROR;
2450 ml = slapi_x_ldapmods2modifications( mods );
2455 ret = acl_check_modlist( be, conn, op, e, ml );
2457 /* Careful when freeing the modlist because it has pointers into the mods array. */
2458 for ( ; ml != NULL; ml = next ) {
2459 next = ml->sml_next;
2461 /* just free the containing array */
2462 slapi_ch_free( (void **)&ml->sml_bvalues );
2463 slapi_ch_free( (void **)&ml );
2466 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2468 return LDAP_UNWILLING_TO_PERFORM;
2473 * Synthesise an LDAPMod array from a Modifications list to pass
2474 * to SLAPI. This synthesis is destructive and as such the
2475 * Modifications list may not be used after calling this
2478 * This function must also be called before slap_mods_check().
2480 LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
2483 Modifications *ml, *modlist;
2484 LDAPMod **mods, *modp;
2487 modlist = *pmodlist;
2489 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
2492 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
2494 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
2496 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
2498 /* Take ownership of original type. */
2499 modp->mod_type = ml->sml_type.bv_val;
2500 ml->sml_type.bv_val = NULL;
2502 if ( ml->sml_bvalues != NULL ) {
2503 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
2505 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
2506 sizeof(struct berval *) );
2507 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
2508 /* Take ownership of original values. */
2509 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
2510 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
2511 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
2512 ml->sml_bvalues[j].bv_len = 0;
2513 ml->sml_bvalues[j].bv_val = NULL;
2515 modp->mod_bvalues[j] = NULL;
2517 modp->mod_bvalues = NULL;
2524 slap_mods_free( modlist );
2534 * Convert a potentially modified array of LDAPMods back to a
2535 * Modification list.
2537 * The returned Modification list contains pointers into the
2538 * LDAPMods array; the latter MUST be freed with
2539 * slapi_x_free_ldapmods() (see below).
2541 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
2544 Modifications *modlist, **modtail;
2549 for( modp = mods; *modp != NULL; modp++ ) {
2553 struct berval **bvp;
2555 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
2556 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
2557 mod->sml_type.bv_val = (*modp)->mod_type;
2558 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
2559 mod->sml_desc = NULL;
2560 mod->sml_next = NULL;
2562 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
2563 for( i = 0, bvp = (*modp)->mod_bvalues; *bvp != NULL; bvp++, i++ )
2566 for( i = 0, p = (*modp)->mod_values; *p != NULL; p++, i++ )
2570 mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
2572 /* NB: This implicitly trusts a plugin to return valid modifications. */
2573 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
2574 for( i = 0, bvp = (*modp)->mod_bvalues; *bvp != NULL; bvp++, i++ ) {
2575 mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
2576 mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
2579 for( i = 0, p = (*modp)->mod_values; *p != NULL; p++, i++ ) {
2580 mod->sml_bvalues[i].bv_val = *p;
2581 mod->sml_bvalues[i].bv_len = strlen( *p );
2584 mod->sml_bvalues[i].bv_val = NULL;
2587 modtail = &mod->sml_next;
2597 * This function only frees the parts of the mods array that
2598 * are not shared with the Modification list that was created
2599 * by slapi_x_ldapmods2modifications().
2602 void slapi_x_free_ldapmods (LDAPMod **mods)
2610 for ( i = 0; mods[i] != NULL; i++ ) {
2612 * Don't free values themselves; they're owned by the
2613 * Modification list. Do free the containing array.
2615 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
2616 for ( j = 0; mods[i]->mod_bvalues[j] != NULL; j++ ) {
2617 ch_free( mods[i]->mod_bvalues[j] );
2619 ch_free( mods[i]->mod_bvalues );
2621 ch_free( mods[i]->mod_values );
2623 /* Don't free type, for same reasons. */
2627 #endif /* LDAP_SLAPI */
2631 * Sun ONE DS 5.x computed attribute support. Computed attributes
2632 * allow for dynamically generated operational attributes, a very
2633 * useful thing indeed.
2637 * Write the computed attribute to a BerElement. Complementary
2638 * functions need to be defined for anything that replaces
2639 * op->o_callback->sc_sendentry, if you wish to make computed
2640 * attributes available to it.
2642 int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
2646 Connection *conn = NULL;
2647 Operation *op = NULL;
2649 AttributeDescription *desc;
2665 rc = slapi_pblock_get( c->cac_pb, SLAPI_BACKEND, (void *)&be );
2667 be = NULL; /* no backend for root DSE */
2670 rc = slapi_pblock_get( c->cac_pb, SLAPI_CONNECTION, (void *)&conn );
2671 if ( rc != 0 || conn == NULL ) {
2675 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
2676 if ( rc != 0 || op == NULL ) {
2680 ber = (BerElement *)c->cac_private;
2683 if ( c->cac_attrs == NULL ) {
2684 /* All attrs request, skip operational attributes */
2685 if ( is_at_operational( desc->ad_type ) ) {
2689 /* Specific attrs requested */
2690 if ( is_at_operational( desc->ad_type ) ) {
2691 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
2695 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
2701 if ( !access_allowed( be, conn, op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
2702 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
2703 "acl: access to attribute %s not allowed\n",
2704 desc->ad_cname.bv_val );
2708 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
2710 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
2711 "ber_printf failed\n");
2715 if ( !c->cac_attrsonly ) {
2716 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
2717 if ( !access_allowed( be, conn, op, e,
2718 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
2719 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
2720 "slapi_x_compute_output_ber: conn %lu "
2721 "acl: access to %s, value %d not allowed\n",
2722 op->o_connid, desc->ad_cname.bv_val, i );
2726 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
2727 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
2728 "ber_printf failed\n");
2734 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
2735 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
2736 "ber_printf failed\n" );
2747 * For some reason Sun don't use the normal plugin mechanism
2748 * registration path to register an "evaluator" function (an
2749 * "evaluator" is responsible for adding computed attributes;
2750 * the nomenclature is somewhat confusing).
2752 * As such slapi_compute_add_evaluator() registers the
2753 * function directly.
2755 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
2758 Slapi_PBlock *pPlugin = NULL;
2761 pPlugin = slapi_pblock_new();
2762 if ( pPlugin == NULL ) {
2763 rc = LDAP_NO_MEMORY;
2767 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
2768 if ( rc != LDAP_SUCCESS ) {
2772 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
2773 if ( rc != LDAP_SUCCESS ) {
2777 rc = insertPlugin( NULL, pPlugin );
2784 if ( rc != LDAP_SUCCESS ) {
2785 if ( pPlugin != NULL ) {
2786 slapi_pblock_destroy( pPlugin );
2794 #endif /* LDAP_SLAPI */
2798 * See notes above regarding slapi_compute_add_evaluator().
2800 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
2803 Slapi_PBlock *pPlugin = NULL;
2806 pPlugin = slapi_pblock_new();
2807 if ( pPlugin == NULL ) {
2808 rc = LDAP_NO_MEMORY;
2812 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
2813 if ( rc != LDAP_SUCCESS ) {
2817 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
2818 if ( rc != LDAP_SUCCESS ) {
2822 rc = insertPlugin( NULL, pPlugin );
2829 if ( rc != LDAP_SUCCESS ) {
2830 if ( pPlugin != NULL ) {
2831 slapi_pblock_destroy( pPlugin );
2839 #endif /* LDAP_SLAPI */
2843 * Call compute evaluators
2845 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
2849 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
2851 rc = getAllPluginFuncs( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
2852 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
2853 /* Nothing to do; front-end should ignore. */
2857 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
2859 * -1: no attribute matched requested type
2860 * 0: one attribute matched
2861 * >0: error happened
2863 rc = (*pGetPlugin)( c, type, e, outputfn );
2869 slapi_ch_free( (void **)&tmpPlugin );
2874 #endif /* LDAP_SLAPI */
2877 int compute_rewrite_search_filter(Slapi_PBlock *pb)
2883 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
2888 return doPluginFNs( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
2891 #endif /* LDAP_SLAPI */
2895 * New API to provide the plugin with access to the search
2896 * pblock. Have informed Sun DS team.
2898 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
2904 if ( c->cac_pb == NULL )
2912 #endif /* LDAP_SLAPI */
2915 Slapi_Mutex *slapi_new_mutex( void )
2920 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
2921 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
2922 slapi_ch_free( (void **)&m );
2932 void slapi_destroy_mutex( Slapi_Mutex *mutex )
2935 if ( mutex != NULL ) {
2936 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
2937 slapi_ch_free( (void **)&mutex);
2942 void slapi_lock_mutex( Slapi_Mutex *mutex )
2945 ldap_pvt_thread_mutex_lock( &mutex->mutex );
2949 int slapi_unlock_mutex( Slapi_Mutex *mutex )
2952 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
2958 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
2963 if ( mutex == NULL ) {
2967 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
2968 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
2969 slapi_ch_free( (void **)&cv );
2973 /* XXX struct copy */
2974 cv->mutex = mutex->mutex;
2982 void slapi_destroy_condvar( Slapi_CondVar *cvar )
2985 if ( cvar != NULL ) {
2986 ldap_pvt_thread_cond_destroy( &cvar->cond );
2987 slapi_ch_free( (void **)&cvar );
2992 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
2995 if ( cvar == NULL ) {
2999 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3005 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3008 if ( cvar == NULL ) {
3013 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3016 return ldap_pvt_thread_cond_signal( &cvar->cond );