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_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
469 AttributeDescription *ad;
474 rc = slap_str2ad( type, &ad, &text );
475 if ( rc != LDAP_SUCCESS ) {
479 attr_delete( &e->e_attrs, ad );
481 rc = bvptr2obj( vals, &bv );
482 if ( rc != LDAP_SUCCESS ) {
486 rc = attr_merge( e, ad, bv );
487 slapi_ch_free( (void **)&bv );
488 if ( rc != LDAP_SUCCESS ) {
495 #endif /* LDAP_SLAPI */
499 * FIXME -- The caller must free the allocated memory.
500 * In Netscape they do not have to.
503 slapi_attr_get_values(
505 struct berval ***vals )
507 #if defined(LDAP_SLAPI)
511 if ( attr == NULL ) {
515 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
519 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
520 for ( j = 0; j < i; j++ ) {
521 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
525 *vals = (struct berval **)bv;
528 #else /* !defined(LDAP_SLAPI) */
530 #endif /* !defined(LDAP_SLAPI) */
534 slapi_dn_normalize( char *dn )
536 #if defined(LDAP_SLAPI)
540 assert( dn != NULL );
543 bdn.bv_len = strlen( dn );
545 dnNormalize2( NULL, &bdn, &ndn );
548 * FIXME: ain't it safe to set dn = ndn.bv_val ?
550 dn = ch_strdup( ndn.bv_val );
551 ch_free( ndn.bv_val );
554 #else /* !defined(LDAP_SLAPI) */
556 #endif /* !defined(LDAP_SLAPI) */
560 * FIXME: this function is dangerous and should be deprecated;
561 * DN normalization is a lot more than lower-casing, and BTW
562 * OpenLDAP's DN normalization for case insensitive attributes
563 * is already lower case
566 slapi_dn_normalize_case( char *dn )
568 #if defined(LDAP_SLAPI)
569 slapi_dn_normalize( dn );
570 ldap_pvt_str2lower( dn );
573 #else /* defined(LDAP_SLAPI) */
575 #endif /* defined(LDAP_SLAPI) */
583 #if defined(LDAP_SLAPI)
584 struct berval bdn, ndn;
585 struct berval bsuffix, nsuffix;
587 assert( dn != NULL );
588 assert( suffix != NULL );
591 bdn.bv_len = strlen( dn );
593 bsuffix.bv_val = suffix;
594 bsuffix.bv_len = strlen( suffix );
596 dnNormalize2( NULL, &bdn, &ndn );
597 dnNormalize2( NULL, &bsuffix, &nsuffix );
599 return dnIsSuffix( &ndn, &nsuffix );
600 #else /* !defined(LDAP_SLAPI) */
602 #endif /* !defined(LDAP_SLAPI) */
606 slapi_dn_ignore_case( char *dn )
608 #if defined(LDAP_SLAPI)
609 return slapi_dn_normalize_case( dn );
610 #else /* !defined(LDAP_SLAPI) */
612 #endif /* !defined(LDAP_SLAPI) */
616 slapi_ch_malloc( unsigned long size )
618 #if defined(LDAP_SLAPI)
619 return ch_malloc( size );
620 #else /* !defined(LDAP_SLAPI) */
622 #endif /* !defined(LDAP_SLAPI) */
626 slapi_ch_free( void **ptr )
628 #if defined(LDAP_SLAPI)
631 #endif /* defined(LDAP_SLAPI) */
635 slapi_ch_free_string( char **ptr )
637 #if defined(LDAP_SLAPI)
638 slapi_ch_free( (void **)ptr );
639 #endif /* defined(LDAP_SLAPI) */
643 slapi_ch_array_free( char **arrayp )
648 if ( arrayp != NULL ) {
649 for ( p = arrayp; *p != NULL; p++ ) {
650 slapi_ch_free( (void **)p );
652 slapi_ch_free( (void **)&arrayp );
658 slapi_ch_bvdup(const struct berval *v)
663 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
664 bv->bv_len = v->bv_len;
665 bv->bv_val = slapi_ch_malloc( bv->bv_len );
666 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
675 slapi_ch_bvecdup(const struct berval **v)
685 for ( i = 0; v[i] != NULL; i++ )
688 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
690 for ( i = 0; v[i] != NULL; i++ ) {
691 rv[i] = slapi_ch_bvdup( v[i] );
706 #if defined(LDAP_SLAPI)
707 return ch_calloc( nelem, size );
708 #else /* !defined(LDAP_SLAPI) */
710 #endif /* !defined(LDAP_SLAPI) */
718 #if defined(LDAP_SLAPI)
719 return ch_realloc( block, size );
720 #else /* !defined(LDAP_SLAPI) */
722 #endif /* !defined(LDAP_SLAPI) */
726 slapi_ch_strdup( char *s )
728 #if defined(LDAP_SLAPI)
729 return ch_strdup( (const char *)s );
730 #else /* !defined(LDAP_SLAPI) */
732 #endif /* !defined(LDAP_SLAPI) */
736 slapi_ch_stlen( char *s )
738 #if defined(LDAP_SLAPI)
739 return strlen( (const char *)s );
740 #else /* !defined(LDAP_SLAPI) */
742 #endif /* !defined(LDAP_SLAPI) */
746 slapi_control_present(
747 LDAPControl **controls,
752 #if defined(LDAP_SLAPI)
764 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
765 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
770 if ( controls[i]->ldctl_value.bv_len != 0 ) {
772 * FIXME: according to 6.1 specification,
773 * "The val output parameter is set
774 * to point into the controls array.
775 * A copy of the control value is
779 struct berval *pTmpBval;
781 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
782 if ( pTmpBval == NULL ) {
785 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
786 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
790 slapi_ch_free( (void **)&pTmpBval );
796 *val = &controls[i]->ldctl_value;
801 *iscritical = controls[i]->ldctl_iscritical;
808 #else /* !defined(LDAP_SLAPI) */
810 #endif /* !defined(LDAP_SLAPI) */
814 slapi_register_supported_control(
816 unsigned long controlops )
818 #if defined(LDAP_SLAPI)
819 /* FIXME -- can not add controls to openLDAP dynamically */
820 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_CONTROLS",
821 "can not add controls to openLDAP dynamically\n" );
822 #endif /* defined(LDAP_SLAPI) */
826 slapi_get_supported_controls(
828 unsigned long **ctrlopsp )
830 #if defined(LDAP_SLAPI)
834 unsigned long *masks = NULL;
836 for (n = 0; get_supported_ctrl( n ) != NULL; n++) {
848 oids = (char **)slapi_ch_malloc( (n + 1) * sizeof(char *) );
849 if ( oids == NULL ) {
854 masks = (unsigned long *)slapi_ch_malloc( n * sizeof(int) );
855 if ( masks == NULL ) {
860 for ( i = 0; i < n; i++ ) {
862 * FIXME: Netscape's specification says nothing about
863 * memory; should we copy the OIDs or return pointers
864 * to internal values? In OpenLDAP the latter is safe
865 * since we do not allow to register coltrols runtime
867 oids[ i ] = ch_strdup( get_supported_ctrl( i ) );
868 if ( oids[ i ] == NULL ) {
872 masks[ i ] = (unsigned long)get_supported_ctrl_mask( i );
880 if ( rc != LDAP_SUCCESS ) {
881 for ( i = 0; oids != NULL && oids[ i ] != NULL; i++ ) {
882 ch_free( oids[ i ] );
889 #else /* !defined(LDAP_SLAPI) */
891 #endif /* !defined(LDAP_SLAPI) */
895 slapi_register_supported_saslmechanism( char *mechanism )
897 #if defined(LDAP_SLAPI)
898 /* FIXME -- can not add saslmechanism to openLDAP dynamically */
899 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
900 "can not add saslmechanism to openLDAP dynamically\n" );
901 #endif /* defined(LDAP_SLAPI) */
905 slapi_get_supported_saslmechanisms( void )
907 #if defined(LDAP_SLAPI)
908 /* FIXME -- can not get the saslmechanism wihtout a connection. */
909 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
910 "can not get the saslmechanism "
911 "wihtout a connection\n" );
913 #else /* defined(LDAP_SLAPI) */
915 #endif /* defined(LDAP_SLAPI) */
919 slapi_get_supported_extended_ops( void )
921 #if defined(LDAP_SLAPI)
923 char **ppExtOpOID = NULL;
926 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
930 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
935 if ( numExtOps == 0 ) {
939 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
940 for ( k = 0; k < i; k++ ) {
943 bv = get_supported_extop( k );
944 assert( bv != NULL );
946 ppExtOpOID[ k ] = bv->bv_val;
949 for ( ; k < j; k++ ) {
952 bv = ns_get_supported_extop( k );
953 assert( bv != NULL );
955 ppExtOpOID[ i + k ] = bv->bv_val;
957 ppExtOpOID[ i + k ] = NULL;
960 #else /* !defined(LDAP_SLAPI) */
962 #endif /* !defined(LDAP_SLAPI) */
966 slapi_send_ldap_result(
972 struct berval **urls )
974 #if defined(LDAP_SLAPI)
979 struct berval *extValue = NULL;
982 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
983 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
984 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
985 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, &s );
986 rc = LDAP_SASL_BIND_IN_PROGRESS;
987 send_ldap_sasl( conn, op, rc, NULL, NULL, NULL, NULL, s );
991 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
992 if ( extOID != NULL ) {
993 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &extValue );
994 slapi_send_ldap_extended_response( conn, op, err, extOID,
999 send_ldap_result( conn, op, err, matched, text, NULL, NULL );
1000 #endif /* defined(LDAP_SLAPI) */
1004 slapi_send_ldap_search_entry(
1007 LDAPControl **ectrls,
1011 #if defined(LDAP_SLAPI)
1018 AttributeName *an = NULL;
1021 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1026 an = (AttributeName *) ch_malloc( i * sizeof(AttributeName) );
1027 for ( i = 0; attrs[i] != NULL; i++ ) {
1028 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1029 an[i].an_name.bv_len = strlen( attrs[i] );
1030 an[i].an_desc = NULL;
1031 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1036 if ( ( rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) ||
1037 ( rc = slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&pConn) != 0 ) ||
1038 ( rc = slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp) != 0 ) ) {
1041 rc = send_search_entry( be, pConn, pOp, e, an, attrsonly, NULL );
1046 #else /* !defined(LDAP_SLAPI) */
1048 #endif /* !defined(LDAP_SLAPI) */
1053 slapi_str2filter( char *str )
1055 #if defined(LDAP_SLAPI)
1056 return str2filter( str );
1057 #else /* !defined(LDAP_SLAPI) */
1059 #endif /* !defined(LDAP_SLAPI) */
1067 #if defined(LDAP_SLAPI)
1069 #endif /* defined(LDAP_SLAPI) */
1073 slapi_filter_get_choice( Slapi_Filter *f )
1075 #if defined(LDAP_SLAPI)
1085 #else /* !defined(LDAP_SLAPI) */
1086 return -1; /* invalid filter type */
1087 #endif /* !defined(LDAP_SLAPI) */
1091 slapi_filter_get_ava(
1094 struct berval **bval )
1096 #if defined(LDAP_SLAPI)
1098 int rc = LDAP_SUCCESS;
1100 assert( type != NULL );
1101 assert( bval != NULL );
1106 ftype = f->f_choice;
1107 if ( ftype == LDAP_FILTER_EQUALITY
1108 || ftype == LDAP_FILTER_GE
1109 || ftype == LDAP_FILTER_LE
1110 || ftype == LDAP_FILTER_APPROX ) {
1112 * According to the SLAPI Reference Manual these are
1115 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1116 *bval = &f->f_un.f_un_ava->aa_value;
1117 } else { /* filter type not supported */
1122 #else /* !defined(LDAP_SLAPI) */
1124 #endif /* !defined(LDAP_SLAPI) */
1128 slapi_filter_list_first( Slapi_Filter *f )
1130 #if defined(LDAP_SLAPI)
1137 ftype = f->f_choice;
1138 if ( ftype == LDAP_FILTER_AND
1139 || ftype == LDAP_FILTER_OR
1140 || ftype == LDAP_FILTER_NOT ) {
1141 return (Slapi_Filter *)f->f_and;
1145 #else /* !defined(LDAP_SLAPI) */
1147 #endif /* !defined(LDAP_SLAPI) */
1151 slapi_filter_list_next(
1153 Slapi_Filter *fprev )
1155 #if defined(LDAP_SLAPI)
1162 ftype = f->f_choice;
1163 if ( ftype == LDAP_FILTER_AND
1164 || ftype == LDAP_FILTER_OR
1165 || ftype == LDAP_FILTER_NOT ) {
1166 if ( f->f_and == fprev ) {
1167 return f->f_and->f_next;
1172 #else /* !defined(LDAP_SLAPI) */
1174 #endif /* !defined(LDAP_SLAPI) */
1178 slapi_send_ldap_extended_response(
1183 struct berval *response )
1185 #if defined(LDAP_SLAPI)
1186 send_ldap_extended( conn,op, errornum, NULL, NULL, NULL,
1187 respName,response, NULL );
1188 return LDAP_SUCCESS;
1189 #else /* !defined(LDAP_SLAPI) */
1191 #endif /* !defined(LDAP_SLAPI) */
1196 struct berval **vals,
1199 #if defined(LDAP_SLAPI)
1201 * FIXME: what's the point?
1204 #else /* !defined(LDAP_SLAPI) */
1206 #endif /* !defined(LDAP_SLAPI) */
1210 slapi_get_hostname( void )
1212 #if defined(LDAP_SLAPI)
1216 * FIXME: I'd prefer a different check ...
1219 hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1221 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1222 "can't malloc memory for hostname\n" );
1225 } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
1226 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1227 "can't get hostname\n" );
1228 slapi_ch_free( (void **)&hn );
1232 static int been_here = 0;
1233 static char *static_hn = NULL;
1235 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1237 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1238 if ( static_hn == NULL) {
1239 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1240 "can't malloc memory for hostname\n" );
1242 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1247 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1248 slapi_log_error( SLAPI_LOG_FATAL,
1250 "can't get hostname\n" );
1251 slapi_ch_free( (void **)&static_hn );
1253 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1262 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1264 hn = ch_strdup( static_hn );
1265 #endif /* !_SPARC */
1268 #else /* !defined(LDAP_SLAPI) */
1270 #endif /* !defined(LDAP_SLAPI) */
1274 * FIXME: this should go in an appropriate header ...
1276 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
1285 #if defined(LDAP_SLAPI)
1286 int rc = LDAP_SUCCESS;
1289 va_start( arglist, fmt );
1290 rc = vLogError( severity, subsystem, fmt, arglist );
1294 #else /* !defined(LDAP_SLAPI) */
1296 #endif /* !defined(LDAP_SLAPI) */
1301 slapi_timer_current_time( void )
1303 #if defined(LDAP_SLAPI)
1304 static int first_time = 1;
1305 #if !defined (_WIN32)
1309 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1312 gettimeofday( &base_time, NULL );
1314 gettimeofday( &now, NULL );
1315 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1316 (now.tv_usec - base_time.tv_usec);
1317 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1323 return (slap_get_time() - starttime) * 1000000;
1330 performance_counter_present = QueryPerformanceCounter( &base_time );
1331 QueryPerformanceFrequency( &performance_freq );
1334 if ( !performance_counter_present )
1337 QueryPerformanceCounter( &now );
1338 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1340 #else /* !defined(LDAP_SLAPI) */
1342 #endif /* !defined(LDAP_SLAPI) */
1349 slapi_timer_get_time( char *label )
1351 #if defined(LDAP_SLAPI)
1352 unsigned long start = slapi_timer_current_time();
1353 printf("%10ld %10ld usec %s\n", start, 0, label);
1355 #else /* !defined(LDAP_SLAPI) */
1357 #endif /* !defined(LDAP_SLAPI) */
1364 slapi_timer_elapsed_time(
1366 unsigned long start )
1368 #if defined(LDAP_SLAPI)
1369 unsigned long stop = slapi_timer_current_time();
1370 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
1371 #endif /* defined(LDAP_SLAPI) */
1375 slapi_free_search_results_internal( Slapi_PBlock *pb )
1377 #if defined(LDAP_SLAPI)
1378 Slapi_Entry **entries;
1379 int k = 0, nEnt = 0;
1381 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
1382 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
1387 if ( entries == NULL ) {
1391 for ( k = 0; k < nEnt; k++ ) {
1392 slapi_entry_free( entries[k] );
1395 slapi_ch_free( (void **)&entries );
1396 #endif /* defined(LDAP_SLAPI) */
1400 * Internal API to prime a Slapi_PBlock with a Backend.
1402 int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
1404 #if defined(LDAP_SLAPI)
1407 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
1408 if ( rc != LDAP_SUCCESS )
1412 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
1413 if ( rc != LDAP_SUCCESS )
1417 return LDAP_SUCCESS;
1420 #endif /* defined(LDAP_SLAPI) */
1423 #if defined(LDAP_SLAPI)
1425 * If oldStyle is TRUE, then a value suitable for setting to
1426 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
1427 * (pointer to static storage).
1429 * If oldStyle is FALSE, then a value suitable for setting to
1430 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
1431 * a pointer to allocated memory and will include the SASL
1432 * mechanism (if any).
1434 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
1439 switch ( authz->sai_method ) {
1440 case LDAP_AUTH_SASL:
1442 authType = SLAPD_AUTH_SASL;
1444 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
1445 authType = slapi_ch_malloc( len );
1446 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
1449 case LDAP_AUTH_SIMPLE:
1450 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
1452 case LDAP_AUTH_NONE:
1453 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
1459 if ( is_tls && authType == NULL ) {
1460 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
1468 * Internal API to prime a Slapi_PBlock with a Connection.
1470 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
1472 #if defined(LDAP_SLAPI)
1476 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
1477 if ( rc != LDAP_SUCCESS )
1480 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
1481 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
1482 if ( rc != LDAP_SUCCESS )
1484 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
1485 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
1486 if ( rc != LDAP_SUCCESS )
1490 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
1491 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
1492 if ( rc != LDAP_SUCCESS )
1494 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
1495 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
1496 if ( rc != LDAP_SUCCESS )
1500 #ifdef LDAP_CONNECTIONLESS
1501 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
1502 if ( rc != LDAP_SUCCESS )
1506 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
1507 if ( rc != LDAP_SUCCESS )
1510 /* Returns pointer to static string */
1511 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
1512 if ( connAuthType != NULL ) {
1513 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
1514 if ( rc != LDAP_SUCCESS )
1518 /* Returns pointer to allocated string */
1519 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
1520 if ( connAuthType != NULL ) {
1521 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
1522 if ( rc != LDAP_SUCCESS )
1526 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
1527 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
1528 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
1529 if ( rc != LDAP_SUCCESS )
1536 #endif /* defined(LDAP_SLAPI) */
1540 * Internal API to prime a Slapi_PBlock with an Operation.
1542 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
1544 #if defined(LDAP_SLAPI)
1551 if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
1555 isRoot = be_isroot( be, &op->o_ndn );
1556 isUpdateDn = be_isupdate( be, &op->o_ndn );
1559 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
1560 if ( rc != LDAP_SUCCESS )
1563 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
1564 if ( rc != LDAP_SUCCESS )
1567 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
1568 if ( rc != LDAP_SUCCESS )
1571 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
1572 if ( rc != LDAP_SUCCESS )
1575 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
1576 if ( rc != LDAP_SUCCESS )
1579 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
1580 if ( rc != LDAP_SUCCESS )
1583 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
1584 if ( rc != LDAP_SUCCESS)
1587 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
1588 if ( rc != LDAP_SUCCESS )
1591 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
1592 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
1593 /* Not quite sure what the point of this is. */
1594 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
1595 if ( rc != LDAP_SUCCESS )
1599 return LDAP_SUCCESS;
1605 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
1607 #if defined( LDAP_SLAPI )
1610 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1611 *isSSL = conn->c_is_tls;
1613 return LDAP_SUCCESS;
1616 #endif /* defined(LDAP_SLAPI) */
1620 * DS 5.x compatability API follow
1623 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
1625 #if defined( LDAP_SLAPI )
1629 return LDAP_PARAM_ERROR;
1631 at = attr->a_desc->ad_type;
1633 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
1635 if ( is_at_single_value( at ) )
1636 *flags |= SLAPI_ATTR_FLAG_SINGLE;
1637 if ( is_at_operational( at ) )
1638 *flags |= SLAPI_ATTR_FLAG_OPATTR;
1639 if ( is_at_obsolete( at ) )
1640 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
1641 if ( is_at_collective( at ) )
1642 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
1643 if ( is_at_no_user_mod( at ) )
1644 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
1646 return LDAP_SUCCESS;
1649 #endif /* defined(LDAP_SLAPI) */
1652 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
1654 #if defined( LDAP_SLAPI )
1655 unsigned long flags;
1657 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
1659 return (flags & flag) ? 1 : 0;
1662 #endif /* defined(LDAP_SLAPI) */
1665 Slapi_Attr *slapi_attr_new( void )
1670 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
1678 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
1682 AttributeDescription *ad = NULL;
1684 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
1699 void slapi_attr_free( Slapi_Attr **a )
1707 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
1710 return attr_dup( (Slapi_Attr *)attr );
1716 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
1719 return value_add_one( &a->a_vals, (Slapi_Value *)v );
1725 int slapi_attr_type2plugin( const char *type, void **pi )
1732 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
1735 if ( attr == NULL ) {
1736 return LDAP_PARAM_ERROR;
1739 *type = attr->a_desc->ad_cname.bv_val;
1741 return LDAP_SUCCESS;
1747 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
1750 if ( attr == NULL ) {
1751 return LDAP_PARAM_ERROR;
1753 *oidp = attr->a_desc->ad_type->sat_oid;
1755 return LDAP_SUCCESS;
1761 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
1769 mr = a->a_desc->ad_type->sat_equality;
1770 rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
1771 (struct berval *)v1, (void *)v2, &text );
1772 if ( rc != LDAP_SUCCESS )
1775 return ( ret == 0 ) ? 0 : -1;
1781 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
1791 mr = a->a_desc->ad_type->sat_equality;
1792 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
1793 rc = value_match( &ret, a->a_desc, mr,
1794 SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
1795 if ( rc != LDAP_SUCCESS ) {
1806 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
1809 AttributeDescription *a1 = NULL;
1810 AttributeDescription *a2 = NULL;
1814 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
1818 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
1822 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
1823 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
1824 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
1827 case SLAPI_TYPE_CMP_EXACT:
1828 ret = ad_cmp( a1, a2 );
1830 case SLAPI_TYPE_CMP_BASE:
1831 ret = ad_base_cmp( a1, a2 );
1833 case SLAPI_TYPE_CMP_SUBTYPE:
1834 ret = is_ad_subtype( a2, a2 );
1847 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
1850 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
1856 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
1859 return slapi_valueset_first_value( &a->a_vals, v );
1865 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
1868 return slapi_valueset_next_value( &a->a_vals, hint, v );
1874 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
1877 *numValues = slapi_valueset_count( &a->a_vals );
1885 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
1888 *vs = &((Slapi_Attr *)a)->a_vals;
1896 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
1899 return slapi_attr_get_values( a, vals );
1905 char *slapi_attr_syntax_normalize( const char *s )
1908 AttributeDescription *ad = NULL;
1911 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
1915 return ad->ad_cname.bv_val;
1921 Slapi_Value *slapi_value_new( void )
1926 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
1934 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
1937 return ber_dupbv( NULL, (struct berval *)bval );
1943 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
1946 return slapi_value_new_berval( v );
1952 Slapi_Value *slapi_value_new_string(const char *s)
1957 bv.bv_val = (char *)s;
1958 bv.bv_len = strlen( s );
1960 return slapi_value_new_berval( &bv );
1966 Slapi_Value *slapi_value_init(Slapi_Value *val)
1978 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
1981 return ber_dupbv( v, bval );
1987 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
1990 v->bv_val = slapi_ch_strdup( (char *)s );
1991 v->bv_len = strlen( s );
1999 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2002 return slapi_value_new_value( v );
2008 void slapi_value_free(Slapi_Value **value)
2011 if ( value == NULL ) {
2015 if ( (*value) != NULL ) {
2016 slapi_ch_free( (void **)&(*value)->bv_val );
2017 slapi_ch_free( (void **)value );
2022 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2031 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2034 if ( value == NULL ) {
2037 if ( value->bv_val != NULL ) {
2038 slapi_ch_free( (void **)&value->bv_val );
2040 slapi_value_init_berval( value, (struct berval *)bval );
2048 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2051 if ( value == NULL ) {
2054 return slapi_value_set_berval( value, vfrom );
2060 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2063 if ( value == NULL ) {
2066 if ( value->bv_val != NULL ) {
2067 slapi_ch_free( (void **)&value->bv_val );
2069 value->bv_val = slapi_ch_malloc( len );
2070 value->bv_len = len;
2071 AC_MEMCPY( value->bv_val, val, len );
2079 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2082 if ( value == NULL ) {
2085 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2092 int slapi_value_set_int(Slapi_Value *value, int intVal)
2097 snprintf( buf, sizeof( buf ), "%d", intVal );
2099 return slapi_value_set_string( value, buf );
2105 const char *slapi_value_get_string(const Slapi_Value *value)
2108 if ( value == NULL ) {
2111 return value->bv_val;
2118 static int checkBVString(const struct berval *bv)
2122 for ( i = 0; i < bv->bv_len; i++ ) {
2123 if ( bv->bv_val[i] == '\0' )
2126 if ( bv->bv_val[i] != '\0' )
2133 int slapi_value_get_int(const Slapi_Value *value)
2136 if ( value == NULL ) return 0;
2137 if ( value->bv_val == NULL ) return 0;
2138 if ( !checkBVString( value ) ) return 0;
2140 return (int)strtol( value->bv_val, NULL, 10 );
2146 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2149 if ( value == NULL ) return 0;
2150 if ( value->bv_val == NULL ) return 0;
2151 if ( !checkBVString( value ) ) return 0;
2153 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2159 long slapi_value_get_long(const Slapi_Value *value)
2162 if ( value == NULL ) return 0;
2163 if ( value->bv_val == NULL ) return 0;
2164 if ( !checkBVString( value ) ) return 0;
2166 return strtol( value->bv_val, NULL, 10 );
2172 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2175 if ( value == NULL ) return 0;
2176 if ( value->bv_val == NULL ) return 0;
2177 if ( !checkBVString( value ) ) return 0;
2179 return strtoul( value->bv_val, NULL, 10 );
2185 size_t slapi_value_get_length(const Slapi_Value *value)
2188 if ( value == NULL )
2191 return (size_t) value->bv_len;
2197 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2200 return slapi_attr_value_cmp( a, v1, v2 );
2206 /* A ValueSet is a container for a BerVarray. */
2207 Slapi_ValueSet *slapi_valueset_new( void )
2212 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2221 void slapi_valueset_free(Slapi_ValueSet *vs)
2227 ber_bvarray_free( vp );
2228 slapi_ch_free( (void **)&vp );
2235 void slapi_valueset_init(Slapi_ValueSet *vs)
2238 if ( vs != NULL && *vs == NULL ) {
2239 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2240 (*vs)->bv_val = NULL;
2246 void slapi_valueset_done(Slapi_ValueSet *vs)
2254 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
2256 slapi_ch_free( (void **)&vp->bv_val );
2258 /* but don't free *vs or vs */
2262 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2267 ber_dupbv( &bv, (Slapi_Value *)addval );
2268 ber_bvarray_add( vs, &bv );
2272 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2275 return slapi_valueset_next_value( vs, 0, v );
2281 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2292 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2303 int slapi_valueset_count( const Slapi_ValueSet *vs )
2314 for ( i = 0; vp[i].bv_val != NULL; i++ )
2324 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2329 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2330 slapi_valueset_add_value( vs1, vp );
2335 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
2336 struct berval *val, int access )
2343 slap_access_t slap_access;
2344 AttributeDescription *ad = NULL;
2347 ret = slap_str2ad( attr, &ad, &text );
2348 if ( ret != LDAP_SUCCESS ) {
2352 switch ( access & SLAPI_ACL_ALL ) {
2353 case SLAPI_ACL_COMPARE:
2354 slap_access = ACL_COMPARE;
2356 case SLAPI_ACL_SEARCH:
2357 slap_access = ACL_SEARCH;
2359 case SLAPI_ACL_READ:
2360 slap_access = ACL_READ;
2362 case SLAPI_ACL_WRITE:
2363 case SLAPI_ACL_DELETE:
2365 case SLAPI_ACL_SELF:
2366 slap_access = ACL_WRITE;
2369 return LDAP_INSUFFICIENT_ACCESS;
2373 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2374 return LDAP_PARAM_ERROR;
2377 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
2378 return LDAP_PARAM_ERROR;
2381 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
2382 return LDAP_PARAM_ERROR;
2385 ret = access_allowed( be, conn, op, e, desc, val, slap_access, NULL );
2387 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2389 return LDAP_UNWILLING_TO_PERFORM;
2393 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
2401 Modifications *next;
2403 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2404 return LDAP_PARAM_ERROR;
2407 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
2408 return LDAP_PARAM_ERROR;
2411 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
2412 return LDAP_PARAM_ERROR;
2415 ml = slapi_x_ldapmods2modifications( mods );
2420 ret = acl_check_modlist( be, conn, op, e, ml );
2422 /* Careful when freeing the modlist because it has pointers into the mods array. */
2423 for ( ; ml != NULL; ml = next ) {
2424 next = ml->sml_next;
2426 /* just free the containing array */
2427 slapi_ch_free( (void **)&ml->sml_bvalues );
2428 slapi_ch_free( (void **)&ml );
2431 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2433 return LDAP_UNWILLING_TO_PERFORM;
2438 * Synthesise an LDAPMod array from a Modifications list to pass
2439 * to SLAPI. This synthesis is destructive and as such the
2440 * Modifications list may not be used after calling this
2443 * This function must also be called before slap_mods_check().
2445 LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
2448 Modifications *ml, *modlist;
2449 LDAPMod **mods, *modp;
2452 modlist = *pmodlist;
2454 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
2457 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
2459 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
2461 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
2463 /* Take ownership of original type. */
2464 modp->mod_type = ml->sml_type.bv_val;
2465 ml->sml_type.bv_val = NULL;
2467 if ( ml->sml_bvalues != NULL ) {
2468 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
2470 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
2471 sizeof(struct berval *) );
2472 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
2473 /* Take ownership of original values. */
2474 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
2475 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
2476 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
2477 ml->sml_bvalues[j].bv_len = 0;
2478 ml->sml_bvalues[j].bv_val = NULL;
2480 modp->mod_bvalues[j] = NULL;
2482 modp->mod_bvalues = NULL;
2489 slap_mods_free( modlist );
2499 * Convert a potentially modified array of LDAPMods back to a
2500 * Modification list.
2502 * The returned Modification list contains pointers into the
2503 * LDAPMods array; the latter MUST be freed with
2504 * slapi_x_free_ldapmods() (see below).
2506 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
2509 Modifications *modlist, **modtail;
2514 for( modp = mods; *modp != NULL; modp++ ) {
2518 struct berval **bvp;
2520 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
2521 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
2522 mod->sml_type.bv_val = (*modp)->mod_type;
2523 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
2524 mod->sml_desc = NULL;
2525 mod->sml_next = NULL;
2527 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
2528 for( i = 0, bvp = (*modp)->mod_bvalues; *bvp != NULL; bvp++, i++ )
2531 for( i = 0, p = (*modp)->mod_values; *p != NULL; p++, i++ )
2535 mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
2537 /* NB: This implicitly trusts a plugin to return valid modifications. */
2538 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
2539 for( i = 0, bvp = (*modp)->mod_bvalues; *bvp != NULL; bvp++, i++ ) {
2540 mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
2541 mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
2544 for( i = 0, p = (*modp)->mod_values; *p != NULL; p++, i++ ) {
2545 mod->sml_bvalues[i].bv_val = *p;
2546 mod->sml_bvalues[i].bv_len = strlen( *p );
2549 mod->sml_bvalues[i].bv_val = NULL;
2552 modtail = &mod->sml_next;
2562 * This function only frees the parts of the mods array that
2563 * are not shared with the Modification list that was created
2564 * by slapi_x_ldapmods2modifications().
2567 void slapi_x_free_ldapmods (LDAPMod **mods)
2575 for ( i = 0; mods[i] != NULL; i++ ) {
2577 * Don't free values themselves; they're owned by the
2578 * Modification list. Do free the containing array.
2580 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
2581 for ( j = 0; mods[i]->mod_bvalues[j] != NULL; j++ ) {
2582 ch_free( mods[i]->mod_bvalues[j] );
2584 ch_free( mods[i]->mod_bvalues );
2586 ch_free( mods[i]->mod_values );
2588 /* Don't free type, for same reasons. */
2592 #endif /* LDAP_SLAPI */
2596 * Sun ONE DS 5.x computed attribute support. Computed attributes
2597 * allow for dynamically generated operational attributes, a very
2598 * useful thing indeed.
2602 * Write the computed attribute to a BerElement. Complementary
2603 * functions need to be defined for anything that replaces
2604 * op->o_callback->sc_sendentry, if you wish to make computed
2605 * attributes available to it.
2607 int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
2611 Connection *conn = NULL;
2612 Operation *op = NULL;
2614 AttributeDescription *desc;
2630 rc = slapi_pblock_get( c->cac_pb, SLAPI_BACKEND, (void *)&be );
2632 be = NULL; /* no backend for root DSE */
2635 rc = slapi_pblock_get( c->cac_pb, SLAPI_CONNECTION, (void *)&conn );
2636 if ( rc != 0 || conn == NULL ) {
2640 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
2641 if ( rc != 0 || op == NULL ) {
2645 ber = (BerElement *)c->cac_private;
2648 if ( c->cac_attrs == NULL ) {
2649 /* All attrs request, skip operational attributes */
2650 if ( is_at_operational( desc->ad_type ) ) {
2654 /* Specific attrs requested */
2655 if ( is_at_operational( desc->ad_type ) ) {
2656 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
2660 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
2666 if ( !access_allowed( be, conn, op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
2667 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
2668 "acl: access to attribute %s not allowed\n",
2669 desc->ad_cname.bv_val );
2673 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
2675 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
2676 "ber_printf failed\n");
2680 if ( !c->cac_attrsonly ) {
2681 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
2682 if ( !access_allowed( be, conn, op, e,
2683 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
2684 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
2685 "slapi_x_compute_output_ber: conn %lu "
2686 "acl: access to %s, value %d not allowed\n",
2687 op->o_connid, desc->ad_cname.bv_val, i );
2691 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
2692 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
2693 "ber_printf failed\n");
2699 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
2700 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
2701 "ber_printf failed\n" );
2712 * For some reason Sun don't use the normal plugin mechanism
2713 * registration path to register an "evaluator" function (an
2714 * "evaluator" is responsible for adding computed attributes;
2715 * the nomenclature is somewhat confusing).
2717 * As such slapi_compute_add_evaluator() registers the
2718 * function directly.
2720 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
2723 Slapi_PBlock *pPlugin = NULL;
2726 pPlugin = slapi_pblock_new();
2727 if ( pPlugin == NULL ) {
2728 rc = LDAP_NO_MEMORY;
2732 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
2733 if ( rc != LDAP_SUCCESS ) {
2737 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
2738 if ( rc != LDAP_SUCCESS ) {
2742 rc = insertPlugin( NULL, pPlugin );
2749 if ( rc != LDAP_SUCCESS ) {
2750 if ( pPlugin != NULL ) {
2751 slapi_pblock_destroy( pPlugin );
2759 #endif /* LDAP_SLAPI */
2763 * See notes above regarding slapi_compute_add_evaluator().
2765 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
2768 Slapi_PBlock *pPlugin = NULL;
2771 pPlugin = slapi_pblock_new();
2772 if ( pPlugin == NULL ) {
2773 rc = LDAP_NO_MEMORY;
2777 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
2778 if ( rc != LDAP_SUCCESS ) {
2782 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
2783 if ( rc != LDAP_SUCCESS ) {
2787 rc = insertPlugin( NULL, pPlugin );
2794 if ( rc != LDAP_SUCCESS ) {
2795 if ( pPlugin != NULL ) {
2796 slapi_pblock_destroy( pPlugin );
2804 #endif /* LDAP_SLAPI */
2808 * Call compute evaluators
2810 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
2814 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
2816 rc = getAllPluginFuncs( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
2817 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
2818 /* Nothing to do; front-end should ignore. */
2822 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
2824 * -1: no attribute matched requested type
2825 * 0: one attribute matched
2826 * >0: error happened
2828 rc = (*pGetPlugin)( c, type, e, outputfn );
2834 slapi_ch_free( (void **)&tmpPlugin );
2839 #endif /* LDAP_SLAPI */
2842 int compute_rewrite_search_filter(Slapi_PBlock *pb)
2848 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
2853 return doPluginFNs( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
2856 #endif /* LDAP_SLAPI */
2860 * New API to provide the plugin with access to the search
2861 * pblock. Have informed Sun DS team.
2863 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
2869 if ( c->cac_pb == NULL )
2877 #endif /* LDAP_SLAPI */
2880 Slapi_Mutex *slapi_new_mutex( void )
2885 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
2886 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
2887 slapi_ch_free( (void **)&m );
2897 void slapi_destroy_mutex( Slapi_Mutex *mutex )
2900 if ( mutex != NULL ) {
2901 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
2902 slapi_ch_free( (void **)&mutex);
2907 void slapi_lock_mutex( Slapi_Mutex *mutex )
2910 ldap_pvt_thread_mutex_lock( &mutex->mutex );
2914 int slapi_unlock_mutex( Slapi_Mutex *mutex )
2917 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
2923 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
2928 if ( mutex == NULL ) {
2932 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
2933 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
2934 slapi_ch_free( (void **)&cv );
2938 /* XXX struct copy */
2939 cv->mutex = mutex->mutex;
2947 void slapi_destroy_condvar( Slapi_CondVar *cvar )
2950 if ( cvar != NULL ) {
2951 ldap_pvt_thread_cond_destroy( &cvar->cond );
2952 slapi_ch_free( (void **)&cvar );
2957 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
2960 if ( cvar == NULL ) {
2964 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
2970 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
2973 if ( cvar == NULL ) {
2978 return ldap_pvt_thread_cond_braodcast( &cvar->cond );
2981 return ldap_pvt_thread_cond_signal( &cvar->cond );