2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2002-2004 The OpenLDAP Foundation.
5 * Portions Copyright 1997,2002-2003 IBM Corporation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by IBM Corporation for use in
18 * IBM products and subsequently ported to OpenLDAP Software by
19 * Steve Omrani. Additional significant contributors include:
25 #include <ac/string.h>
26 #include <ac/stdarg.h>
28 #include <ac/unistd.h>
36 * server start time (should we use a struct timeval also in slapd?
38 static struct timeval base_time;
39 ldap_pvt_thread_mutex_t slapi_hn_mutex;
40 ldap_pvt_thread_mutex_t slapi_time_mutex;
43 ldap_pvt_thread_mutex_t mutex;
46 struct slapi_condvar {
47 ldap_pvt_thread_cond_t cond;
48 ldap_pvt_thread_mutex_t mutex;
52 static int checkBVString(const struct berval *bv)
56 for ( i = 0; i < bv->bv_len; i++ ) {
57 if ( bv->bv_val[i] == '\0' )
60 if ( bv->bv_val[i] != '\0' )
65 #endif /* LDAP_SLAPI */
68 * This function converts an array of pointers to berval objects to
69 * an array of berval objects.
74 struct berval **bvptr,
77 int rc = LDAP_SUCCESS;
81 if ( bvptr == NULL || *bvptr == NULL ) {
85 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
89 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
90 if ( tmpberval == NULL ) {
91 return LDAP_NO_MEMORY;
94 for ( i = 0; bvptr[i] != NULL; i++ ) {
95 tmpberval[i].bv_val = bvptr[i]->bv_val;
96 tmpberval[i].bv_len = bvptr[i]->bv_len;
98 tmpberval[i].bv_val = NULL;
99 tmpberval[i].bv_len = 0;
101 if ( rc == LDAP_SUCCESS ) {
114 Slapi_Entry *e = NULL;
117 pTmpS = slapi_ch_strdup( s );
118 if ( pTmpS != NULL ) {
119 e = str2entry( pTmpS );
120 slapi_ch_free( (void **)&pTmpS );
126 #endif /* LDAP_SLAPI */
137 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
138 ret = entry2str( e, len );
139 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
142 #else /* LDAP_SLAPI */
144 #endif /* LDAP_SLAPI */
148 slapi_entry_get_dn( Slapi_Entry *e )
151 return e->e_name.bv_val;
152 #else /* LDAP_SLAPI */
154 #endif /* LDAP_SLAPI */
158 slapi_x_entry_get_id( Slapi_Entry *e )
164 #endif /* LDAP_SLAPI */
173 struct berval dn = BER_BVNULL;
176 dn.bv_len = strlen( ldn );
178 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname, NULL );
179 #endif /* LDAP_SLAPI */
183 slapi_entry_dup( Slapi_Entry *e )
186 return entry_dup( e );
187 #else /* LDAP_SLAPI */
189 #endif /* LDAP_SLAPI */
193 slapi_entry_attr_delete(
198 AttributeDescription *ad = NULL;
201 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
202 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
205 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
206 return 0; /* attribute is deleted */
208 return -1; /* something went wrong */
210 #else /* LDAP_SLAPI */
212 #endif /* LDAP_SLAPI */
216 slapi_entry_alloc( void )
219 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
220 #else /* LDAP_SLAPI */
222 #endif /* LDAP_SLAPI */
226 slapi_entry_free( Slapi_Entry *e )
230 #endif /* LDAP_SLAPI */
234 slapi_entry_attr_merge(
237 struct berval **vals )
240 AttributeDescription *ad = NULL;
245 rc = bvptr2obj( vals, &bv );
246 if ( rc != LDAP_SUCCESS ) {
250 rc = slap_str2ad( type, &ad, &text );
251 if ( rc != LDAP_SUCCESS ) {
255 rc = attr_merge_normalize_one( e, ad, bv, NULL );
259 #else /* LDAP_SLAPI */
261 #endif /* LDAP_SLAPI */
265 slapi_entry_attr_find(
271 AttributeDescription *ad = NULL;
275 rc = slap_str2ad( type, &ad, &text );
276 if ( rc != LDAP_SUCCESS ) {
280 *attr = attr_find( e->e_attrs, ad );
281 if ( *attr == NULL ) {
286 #else /* LDAP_SLAPI */
288 #endif /* LDAP_SLAPI */
292 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
295 AttributeDescription *ad = NULL;
300 rc = slap_str2ad( type, &ad, &text );
301 if ( rc != LDAP_SUCCESS ) {
305 attr = attr_find( e->e_attrs, ad );
306 if ( attr == NULL ) {
310 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
313 p = slapi_value_get_string( &attr->a_vals[0] );
315 return slapi_ch_strdup( (char *)p );
326 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
329 AttributeDescription *ad = NULL;
334 rc = slap_str2ad( type, &ad, &text );
335 if ( rc != LDAP_SUCCESS ) {
339 attr = attr_find( e->e_attrs, ad );
340 if ( attr == NULL ) {
344 return slapi_value_get_int( attr->a_vals );
351 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
354 AttributeDescription *ad = NULL;
359 rc = slap_str2ad( type, &ad, &text );
360 if ( rc != LDAP_SUCCESS ) {
364 attr = attr_find( e->e_attrs, ad );
365 if ( attr == NULL ) {
369 return slapi_value_get_long( attr->a_vals );
376 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
379 AttributeDescription *ad = NULL;
384 rc = slap_str2ad( type, &ad, &text );
385 if ( rc != LDAP_SUCCESS ) {
389 attr = attr_find( e->e_attrs, ad );
390 if ( attr == NULL ) {
394 return slapi_value_get_uint( attr->a_vals );
401 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
404 AttributeDescription *ad = NULL;
409 rc = slap_str2ad( type, &ad, &text );
410 if ( rc != LDAP_SUCCESS ) {
414 attr = attr_find( e->e_attrs, ad );
415 if ( attr == NULL ) {
419 return slapi_value_get_ulong( attr->a_vals );
426 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
430 AttributeDescription *ad = NULL;
435 rc = slap_str2ad( type, &ad, &text );
436 if ( rc != LDAP_SUCCESS ) {
440 attr = attr_find( e->e_attrs, ad );
441 if ( attr == NULL ) {
445 bv.bv_val = (char *)value;
446 bv.bv_len = strlen( value );
448 return ( slapi_attr_value_find( attr, &bv ) != -1 );
455 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
458 AttributeDescription *ad = NULL;
463 rc = slap_str2ad( type, &ad, &text );
464 if ( rc != LDAP_SUCCESS ) {
468 attr_delete ( &e->e_attrs, ad );
469 if ( value != NULL ) {
470 bv.bv_val = (char *)value;
471 bv.bv_len = strlen(value);
472 attr_merge_normalize_one( e, ad, &bv, NULL );
474 #endif /* LDAP_SLAPI */
478 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
483 snprintf( buf, sizeof( buf ), "%d", l );
484 slapi_entry_attr_set_charptr( e, type, buf );
485 #endif /* LDAP_SLAPI */
489 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
494 snprintf( buf, sizeof( buf ), "%u", l );
495 slapi_entry_attr_set_charptr( e, type, buf );
496 #endif /* LDAP_SLAPI */
500 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
505 snprintf( buf, sizeof( buf ), "%ld", l );
506 slapi_entry_attr_set_charptr( e, type, buf );
507 #endif /* LDAP_SLAPI */
511 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
516 snprintf( buf, sizeof( buf ), "%lu", l );
517 slapi_entry_attr_set_charptr( e, type, buf );
518 #endif /* LDAP_SLAPI */
522 slapi_is_rootdse( const char *dn )
525 return ( dn == NULL || dn[0] == '\0' );
532 slapi_entry_has_children(const Slapi_Entry *e)
537 int hasSubordinates = 0;
539 pConn = slapi_int_init_connection( NULL, LDAP_REQ_SEARCH );
540 if ( pConn == NULL ) {
544 op = (Operation *)pConn->c_pending_ops.stqh_first;
545 op->o_bd = select_backend( (struct berval *)&e->e_nname, 0, 0 );
546 if ( op->o_bd == NULL ) {
550 op->o_bd->be_has_subordinates( op, (Entry *)e, &hasSubordinates );
552 slapi_int_connection_destroy( &pConn );
554 return ( hasSubordinates == LDAP_COMPARE_TRUE );
561 * Return approximate size of the entry rounded to the nearest
562 * 1K. Only the size of the attribute values are counted in the
563 * Sun implementation.
565 * http://docs.sun.com/source/816-6701-10/funcref.html#1017388
567 size_t slapi_entry_size(Slapi_Entry *e)
574 for ( size = 0, a = e->e_attrs; a != NULL; a = a->a_next ) {
575 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
576 size += a->a_vals[i].bv_len + 1;
581 size -= (size % 1024);
586 #endif /* LDAP_SLAPI */
590 * Add values to entry.
593 * LDAP_SUCCESS Values added to entry
594 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
595 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
598 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
604 char textbuf[SLAP_TEXT_BUFLEN];
606 mod.sm_op = LDAP_MOD_ADD;
608 mod.sm_type.bv_val = (char *)type;
609 mod.sm_type.bv_len = strlen( type );
611 rc = slap_str2ad( type, &mod.sm_desc, &text );
612 if ( rc != LDAP_SUCCESS ) {
616 if ( vals == NULL ) {
617 /* Apparently vals can be NULL
618 * FIXME: sm_values = NULL ? */
619 mod.sm_values = (BerVarray)ch_malloc( sizeof(struct berval) );
620 mod.sm_values->bv_val = NULL;
623 rc = bvptr2obj( vals, &mod.sm_values );
624 if ( rc != LDAP_SUCCESS ) {
625 return LDAP_CONSTRAINT_VIOLATION;
628 mod.sm_nvalues = NULL;
630 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
632 ch_free( mod.sm_values );
634 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
637 #endif /* LDAP_SLAPI */
641 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
644 return slapi_entry_add_values( e, type, vals );
647 #endif /* LDAP_SLAPI */
651 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
654 AttributeDescription *ad = NULL;
658 rc = slap_str2ad( type, &ad, &text );
659 if ( rc != LDAP_SUCCESS ) {
663 return attr_merge_normalize( e, ad, *vs, NULL );
666 #endif /* LDAP_SLAPI */
670 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
676 char textbuf[SLAP_TEXT_BUFLEN];
678 mod.sm_op = LDAP_MOD_DELETE;
680 mod.sm_type.bv_val = (char *)type;
681 mod.sm_type.bv_len = strlen( type );
683 if ( vals == NULL ) {
684 /* If vals is NULL, this is a NOOP. */
688 rc = slap_str2ad( type, &mod.sm_desc, &text );
689 if ( rc != LDAP_SUCCESS ) {
693 if ( vals[0] == NULL ) {
694 /* SLAPI doco says LDAP_OPERATIONS_ERROR but LDAP_OTHER is better */
695 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
698 rc = bvptr2obj( vals, &mod.sm_values );
699 if ( rc != LDAP_SUCCESS ) {
700 return LDAP_CONSTRAINT_VIOLATION;
702 mod.sm_nvalues = NULL;
704 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
706 ch_free( mod.sm_values );
711 #endif /* LDAP_SLAPI */
715 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
718 return slapi_entry_delete_values( e, type, vals );
721 #endif /* LDAP_SLAPI */
725 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
728 return slapi_entry_attr_merge( e, (char *)type, vals );
731 #endif /* LDAP_SLAPI */
735 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
738 AttributeDescription *ad = NULL;
742 rc = slap_str2ad( type, &ad, &text );
743 if ( rc != LDAP_SUCCESS ) {
747 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
748 if ( rc != LDAP_SUCCESS ) {
755 #endif /* LDAP_SLAPI */
759 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
764 val.bv_val = (char *)value;
765 val.bv_len = strlen( value );
767 return slapi_entry_add_value( e, type, &val );
770 #endif /* LDAP_SLAPI */
774 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
777 Slapi_Value *vals[2];
780 val.bv_val = (char *)value;
781 val.bv_len = strlen( value );
785 return slapi_entry_delete_values_sv( e, type, vals );
788 #endif /* LDAP_SLAPI */
793 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
796 return slapi_entry_attr_merge( e, (char *)type, vals );
803 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
812 return ( *attr != NULL ) ? 0 : -1;
819 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
826 if ( prevattr == NULL ) {
830 *attr = prevattr->a_next;
832 return ( *attr != NULL ) ? 0 : -1;
839 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
842 AttributeDescription *ad = NULL;
847 rc = slap_str2ad( type, &ad, &text );
848 if ( rc != LDAP_SUCCESS ) {
852 attr_delete( &e->e_attrs, ad );
854 rc = bvptr2obj( vals, &bv );
855 if ( rc != LDAP_SUCCESS ) {
859 rc = attr_merge_normalize( e, ad, bv, NULL );
860 slapi_ch_free( (void **)&bv );
861 if ( rc != LDAP_SUCCESS ) {
868 #endif /* LDAP_SLAPI */
872 * FIXME -- The caller must free the allocated memory.
873 * In Netscape they do not have to.
876 slapi_attr_get_values(
878 struct berval ***vals )
884 if ( attr == NULL ) {
888 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
892 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
893 for ( j = 0; j < i; j++ ) {
894 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
898 *vals = (struct berval **)bv;
901 #else /* LDAP_SLAPI */
903 #endif /* LDAP_SLAPI */
907 slapi_dn_normalize( char *dn )
913 assert( dn != NULL );
916 bdn.bv_len = strlen( dn );
918 if ( dnPretty( NULL, &bdn, &pdn, NULL ) != LDAP_SUCCESS ) {
923 #else /* LDAP_SLAPI */
925 #endif /* LDAP_SLAPI */
929 slapi_dn_normalize_case( char *dn )
935 assert( dn != NULL );
938 bdn.bv_len = strlen( dn );
940 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
945 #else /* LDAP_SLAPI */
947 #endif /* LDAP_SLAPI */
956 struct berval bdn, ndn;
957 struct berval bsuffix, nsuffix;
960 assert( dn != NULL );
961 assert( suffix != NULL );
964 bdn.bv_len = strlen( dn );
966 bsuffix.bv_val = suffix;
967 bsuffix.bv_len = strlen( suffix );
969 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
973 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
976 slapi_ch_free( (void **)&ndn.bv_val );
980 rc = dnIsSuffix( &ndn, &nsuffix );
982 slapi_ch_free( (void **)&ndn.bv_val );
983 slapi_ch_free( (void **)&nsuffix.bv_val );
986 #else /* LDAP_SLAPI */
988 #endif /* LDAP_SLAPI */
993 const char *parentdn,
994 const char *childdn )
997 struct berval assertedParentDN, normalizedAssertedParentDN;
998 struct berval childDN, normalizedChildDN;
999 struct berval normalizedParentDN;
1002 assert( parentdn != NULL );
1003 assert( childdn != NULL );
1005 assertedParentDN.bv_val = (char *)parentdn;
1006 assertedParentDN.bv_len = strlen( parentdn );
1008 if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
1009 &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
1014 childDN.bv_val = (char *)childdn;
1015 childDN.bv_len = strlen( childdn );
1017 if ( dnNormalize( 0, NULL, NULL, &childDN,
1018 &normalizedChildDN, NULL ) != LDAP_SUCCESS )
1020 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
1024 dnParent( &normalizedChildDN, &normalizedParentDN );
1026 if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
1027 &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
1032 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
1033 slapi_ch_free( (void **)&normalizedChildDN.bv_val );
1035 return ( match == 0 );
1038 #endif /* LDAP_SLAPI */
1042 * Returns DN of the parent entry, or NULL if the DN is
1043 * an empty string or NULL, or has no parent.
1046 slapi_dn_parent( const char *_dn )
1049 struct berval dn, prettyDN;
1050 struct berval parentDN;
1052 if ( _dn == NULL ) {
1056 dn.bv_val = (char *)_dn;
1057 dn.bv_len = strlen( _dn );
1059 if ( dn.bv_len == 0 ) {
1063 if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
1067 dnParent( &prettyDN, &parentDN ); /* in-place */
1069 slapi_ch_free( (void **)&prettyDN.bv_val );
1071 if ( parentDN.bv_len == 0 ) {
1075 return slapi_ch_strdup( parentDN.bv_val );
1078 #endif /* LDAP_SLAPI */
1082 * Returns DN of the parent entry; or NULL if the DN is
1083 * an empty string, if the DN has no parent, or if the
1084 * DN is the suffix of the backend database
1086 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *_dn )
1090 struct berval dn, prettyDN;
1091 struct berval normalizedDN, parentDN;
1093 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
1096 dn.bv_val = (char *)_dn;
1097 dn.bv_len = strlen( _dn );
1099 if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
1103 if ( be != NULL && be_issuffix( be, &normalizedDN ) ) {
1104 slapi_ch_free( (void **)&prettyDN.bv_val );
1105 slapi_ch_free( (void **)&normalizedDN.bv_val );
1109 dnParent( &prettyDN, &parentDN );
1111 slapi_ch_free( (void **)&prettyDN.bv_val );
1112 slapi_ch_free( (void **)&normalizedDN.bv_val );
1114 if ( parentDN.bv_len == 0 ) {
1118 return slapi_ch_strdup( parentDN.bv_val );
1121 #endif /* LDAP_SLAPI */
1125 slapi_dn_ignore_case( char *dn )
1128 return slapi_dn_normalize_case( dn );
1129 #else /* LDAP_SLAPI */
1131 #endif /* LDAP_SLAPI */
1135 slapi_ch_malloc( unsigned long size )
1138 return ch_malloc( size );
1139 #else /* LDAP_SLAPI */
1141 #endif /* LDAP_SLAPI */
1145 slapi_ch_free( void **ptr )
1150 #endif /* LDAP_SLAPI */
1154 slapi_ch_free_string( char **ptr )
1157 slapi_ch_free( (void **)ptr );
1158 #endif /* LDAP_SLAPI */
1162 slapi_ch_array_free( char **arrayp )
1167 if ( arrayp != NULL ) {
1168 for ( p = arrayp; *p != NULL; p++ ) {
1169 slapi_ch_free( (void **)p );
1171 slapi_ch_free( (void **)&arrayp );
1177 slapi_ch_bvdup(const struct berval *v)
1182 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
1183 bv->bv_len = v->bv_len;
1184 bv->bv_val = slapi_ch_malloc( bv->bv_len );
1185 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1194 slapi_ch_bvecdup(const struct berval **v)
1204 for ( i = 0; v[i] != NULL; i++ )
1207 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1209 for ( i = 0; v[i] != NULL; i++ ) {
1210 rv[i] = slapi_ch_bvdup( v[i] );
1222 unsigned long nelem,
1223 unsigned long size )
1226 return ch_calloc( nelem, size );
1227 #else /* LDAP_SLAPI */
1229 #endif /* LDAP_SLAPI */
1235 unsigned long size )
1238 return ch_realloc( block, size );
1239 #else /* LDAP_SLAPI */
1241 #endif /* LDAP_SLAPI */
1245 slapi_ch_strdup( char *s )
1248 return ch_strdup( (const char *)s );
1249 #else /* LDAP_SLAPI */
1251 #endif /* LDAP_SLAPI */
1255 slapi_ch_stlen( char *s )
1258 return strlen( (const char *)s );
1259 #else /* LDAP_SLAPI */
1261 #endif /* LDAP_SLAPI */
1265 slapi_control_present(
1266 LDAPControl **controls,
1268 struct berval **val,
1283 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1284 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1289 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1291 * FIXME: according to 6.1 specification,
1292 * "The val output parameter is set
1293 * to point into the controls array.
1294 * A copy of the control value is
1298 struct berval *pTmpBval;
1300 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
1301 if ( pTmpBval == NULL ) {
1304 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
1305 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
1309 slapi_ch_free( (void **)&pTmpBval );
1315 *val = &controls[i]->ldctl_value;
1320 *iscritical = controls[i]->ldctl_iscritical;
1327 #else /* LDAP_SLAPI */
1329 #endif /* LDAP_SLAPI */
1334 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1335 unsigned long *slapi_mask)
1337 *slapi_mask = SLAPI_OPERATION_NONE;
1339 if ( slap_mask & SLAP_CTRL_ABANDON )
1340 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1342 if ( slap_mask & SLAP_CTRL_ADD )
1343 *slapi_mask |= SLAPI_OPERATION_ADD;
1345 if ( slap_mask & SLAP_CTRL_BIND )
1346 *slapi_mask |= SLAPI_OPERATION_BIND;
1348 if ( slap_mask & SLAP_CTRL_COMPARE )
1349 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1351 if ( slap_mask & SLAP_CTRL_DELETE )
1352 *slapi_mask |= SLAPI_OPERATION_DELETE;
1354 if ( slap_mask & SLAP_CTRL_MODIFY )
1355 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1357 if ( slap_mask & SLAP_CTRL_RENAME )
1358 *slapi_mask |= SLAPI_OPERATION_MODDN;
1360 if ( slap_mask & SLAP_CTRL_SEARCH )
1361 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1363 if ( slap_mask & SLAP_CTRL_UNBIND )
1364 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1368 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1369 slap_mask_t *slap_mask)
1373 if ( slapi_mask & SLAPI_OPERATION_BIND )
1374 *slap_mask |= SLAP_CTRL_BIND;
1376 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1377 *slap_mask |= SLAP_CTRL_UNBIND;
1379 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1380 *slap_mask |= SLAP_CTRL_SEARCH;
1382 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1383 *slap_mask |= SLAP_CTRL_MODIFY;
1385 if ( slapi_mask & SLAPI_OPERATION_ADD )
1386 *slap_mask |= SLAP_CTRL_ADD;
1388 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1389 *slap_mask |= SLAP_CTRL_DELETE;
1391 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1392 *slap_mask |= SLAP_CTRL_RENAME;
1394 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1395 *slap_mask |= SLAP_CTRL_COMPARE;
1397 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1398 *slap_mask |= SLAP_CTRL_ABANDON;
1400 *slap_mask |= SLAP_CTRL_FRONTEND;
1404 slapi_int_parse_control(
1409 /* Plugins must deal with controls themselves. */
1411 return LDAP_SUCCESS;
1413 #endif /* LDAP_SLAPI */
1416 slapi_register_supported_control(
1418 unsigned long controlops )
1421 slap_mask_t controlmask;
1423 slapiControlOp2SlapControlMask( controlops, &controlmask );
1425 register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL );
1426 #endif /* LDAP_SLAPI */
1430 slapi_get_supported_controls(
1432 unsigned long **ctrlopsp )
1437 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1438 if ( rc != LDAP_SUCCESS ) {
1442 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1443 /* In place, naughty. */
1444 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1447 return LDAP_SUCCESS;
1448 #else /* LDAP_SLAPI */
1450 #endif /* LDAP_SLAPI */
1454 slapi_dup_control( LDAPControl *ctrl )
1459 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1460 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1461 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1462 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1467 #endif /* LDAP_SLAPI */
1471 slapi_register_supported_saslmechanism( char *mechanism )
1474 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1475 slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1476 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1477 #endif /* LDAP_SLAPI */
1481 slapi_get_supported_saslmechanisms( void )
1484 /* FIXME -- can not get the saslmechanism without a connection. */
1485 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1486 "can not get the SASL mechanism list "
1487 "without a connection\n" );
1489 #else /* LDAP_SLAPI */
1491 #endif /* LDAP_SLAPI */
1495 slapi_get_supported_extended_ops( void )
1499 char **ppExtOpOID = NULL;
1502 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1506 for ( j = 0; slapi_int_get_supported_extop( j ) != NULL; j++ ) {
1511 if ( numExtOps == 0 ) {
1515 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1516 for ( k = 0; k < i; k++ ) {
1519 bv = get_supported_extop( k );
1520 assert( bv != NULL );
1522 ppExtOpOID[ k ] = bv->bv_val;
1525 for ( ; k < j; k++ ) {
1528 bv = slapi_int_get_supported_extop( k );
1529 assert( bv != NULL );
1531 ppExtOpOID[ i + k ] = bv->bv_val;
1533 ppExtOpOID[ i + k ] = NULL;
1536 #else /* LDAP_SLAPI */
1538 #endif /* LDAP_SLAPI */
1542 slapi_send_ldap_result(
1548 struct berval **urls )
1553 char *extOID = NULL;
1554 struct berval *extValue = NULL;
1556 SlapReply rs = { REP_RESULT };
1558 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1561 rs.sr_matched = matched;
1566 slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
1568 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1569 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
1570 send_ldap_sasl( op, &rs );
1574 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1575 if ( extOID != NULL ) {
1576 rs.sr_rspoid = extOID;
1577 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
1578 send_ldap_extended( op, &rs );
1582 if (op->o_tag == LDAP_REQ_SEARCH)
1583 rs.sr_nentries = nentries;
1585 send_ldap_result( op, &rs );
1586 #endif /* LDAP_SLAPI */
1590 slapi_send_ldap_search_entry(
1593 LDAPControl **ectrls,
1599 SlapReply rs = { REP_RESULT };
1601 AttributeName *an = NULL;
1604 if ( attrs != NULL ) {
1605 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1613 an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
1614 for ( i = 0; attrs[i] != NULL; i++ ) {
1615 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1616 an[i].an_name.bv_len = strlen( attrs[i] );
1617 an[i].an_desc = NULL;
1618 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1621 an[i].an_name.bv_len = 0;
1622 an[i].an_name.bv_val = NULL;
1625 rs.sr_err = LDAP_SUCCESS;
1626 rs.sr_matched = NULL;
1629 rs.sr_ctrls = ectrls;
1631 rs.sr_operational_attrs = NULL;
1636 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1640 return send_search_entry( pOp, &rs );
1641 #else /* LDAP_SLAPI */
1643 #endif /* LDAP_SLAPI */
1647 slapi_send_ldap_search_reference(
1650 struct berval **references,
1651 LDAPControl **ectrls,
1652 struct berval **v2refs
1657 SlapReply rs = { REP_SEARCHREF };
1660 rs.sr_err = LDAP_SUCCESS;
1661 rs.sr_matched = NULL;
1664 rc = bvptr2obj( references, &rs.sr_ref );
1665 if ( rc != LDAP_SUCCESS ) {
1669 rs.sr_ctrls = ectrls;
1671 rs.sr_operational_attrs = NULL;
1674 if ( v2refs != NULL ) {
1675 rc = bvptr2obj( v2refs, &rs.sr_v2ref );
1676 if ( rc != LDAP_SUCCESS ) {
1677 slapi_ch_free( (void **)&rs.sr_ref );
1684 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1688 rc = send_search_reference( pOp, &rs );
1690 if ( rs.sr_ref != NULL )
1691 slapi_ch_free( (void **)&rs.sr_ref );
1693 if ( rs.sr_v2ref != NULL )
1694 slapi_ch_free( (void **)&rs.sr_v2ref );
1699 #endif /* LDAP_SLAPI */
1703 slapi_str2filter( char *str )
1706 return str2filter( str );
1707 #else /* LDAP_SLAPI */
1709 #endif /* LDAP_SLAPI */
1719 #endif /* LDAP_SLAPI */
1723 slapi_filter_dup( Slapi_Filter *filter )
1728 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1730 f->f_choice = filter->f_choice;
1732 switch ( f->f_choice ) {
1733 case LDAP_FILTER_AND:
1734 case LDAP_FILTER_NOT:
1735 case LDAP_FILTER_OR: {
1736 Filter *pFilter, **ppF;
1738 for ( pFilter = filter->f_list, ppF = &f->f_list;
1740 pFilter = pFilter->f_next, ppF = &f->f_next )
1742 *ppF = slapi_filter_dup( pFilter );
1748 case LDAP_FILTER_PRESENT:
1749 f->f_desc = filter->f_desc;
1751 case LDAP_FILTER_EQUALITY:
1752 case LDAP_FILTER_GE:
1753 case LDAP_FILTER_LE:
1754 case LDAP_FILTER_APPROX:
1755 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1756 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1757 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1759 case LDAP_FILTER_EXT:
1760 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1761 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1762 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1763 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1764 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1765 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1767 case LDAP_FILTER_SUBSTRINGS: {
1770 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1771 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1772 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1773 if ( filter->f_sub_any != NULL ) {
1774 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1776 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1777 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1778 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1780 f->f_sub_any[i].bv_val = NULL;
1782 f->f_sub_any = NULL;
1784 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1787 case SLAPD_FILTER_COMPUTED:
1788 f->f_result = filter->f_result;
1791 slapi_ch_free( (void **)&f );
1799 #endif /* LDAP_SLAPI */
1803 slapi_filter_get_choice( Slapi_Filter *f )
1815 #else /* LDAP_SLAPI */
1816 return -1; /* invalid filter type */
1817 #endif /* LDAP_SLAPI */
1821 slapi_filter_get_ava(
1824 struct berval **bval )
1828 int rc = LDAP_SUCCESS;
1830 assert( type != NULL );
1831 assert( bval != NULL );
1836 ftype = f->f_choice;
1837 if ( ftype == LDAP_FILTER_EQUALITY
1838 || ftype == LDAP_FILTER_GE
1839 || ftype == LDAP_FILTER_LE
1840 || ftype == LDAP_FILTER_APPROX ) {
1842 * According to the SLAPI Reference Manual these are
1845 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1846 *bval = &f->f_un.f_un_ava->aa_value;
1847 } else { /* filter type not supported */
1852 #else /* LDAP_SLAPI */
1854 #endif /* LDAP_SLAPI */
1858 slapi_filter_list_first( Slapi_Filter *f )
1867 ftype = f->f_choice;
1868 if ( ftype == LDAP_FILTER_AND
1869 || ftype == LDAP_FILTER_OR
1870 || ftype == LDAP_FILTER_NOT ) {
1871 return (Slapi_Filter *)f->f_list;
1875 #else /* LDAP_SLAPI */
1877 #endif /* LDAP_SLAPI */
1881 slapi_filter_list_next(
1883 Slapi_Filter *fprev )
1892 ftype = f->f_choice;
1893 if ( ftype == LDAP_FILTER_AND
1894 || ftype == LDAP_FILTER_OR
1895 || ftype == LDAP_FILTER_NOT )
1897 return fprev->f_next;
1901 #else /* LDAP_SLAPI */
1903 #endif /* LDAP_SLAPI */
1907 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1914 switch ( f->f_choice ) {
1915 case LDAP_FILTER_GE:
1916 case LDAP_FILTER_LE:
1917 case LDAP_FILTER_EQUALITY:
1918 case LDAP_FILTER_APPROX:
1919 *type = f->f_av_desc->ad_cname.bv_val;
1921 case LDAP_FILTER_SUBSTRINGS:
1922 *type = f->f_sub_desc->ad_cname.bv_val;
1924 case LDAP_FILTER_PRESENT:
1925 *type = f->f_desc->ad_cname.bv_val;
1927 case LDAP_FILTER_EXT:
1928 *type = f->f_mr_desc->ad_cname.bv_val;
1931 /* Complex filters need not apply. */
1939 #endif /* LDAP_SLAPI */
1943 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1944 char ***any, char **final )
1949 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1954 * The caller shouldn't free but we can't return an
1955 * array of char *s from an array of bervals without
1956 * allocating memory, so we may as well be consistent.
1959 *type = f->f_sub_desc->ad_cname.bv_val;
1960 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1961 if ( f->f_sub_any != NULL ) {
1962 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1964 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1965 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1966 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1972 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1977 #endif /* LDAP_SLAPI */
1981 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1984 Slapi_Filter *f = NULL;
1986 if ( ftype == LDAP_FILTER_AND ||
1987 ftype == LDAP_FILTER_OR ||
1988 ftype == LDAP_FILTER_NOT )
1990 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1991 f->f_choice = ftype;
1993 f->f_list->f_next = f2;
2000 #endif /* LDAP_SLAPI */
2004 slapi_x_filter_append( int ftype,
2005 Slapi_Filter **pContainingFilter, /* NULL on first call */
2006 Slapi_Filter **pNextFilter,
2007 Slapi_Filter *filterToAppend )
2010 if ( ftype == LDAP_FILTER_AND ||
2011 ftype == LDAP_FILTER_OR ||
2012 ftype == LDAP_FILTER_NOT )
2014 if ( *pContainingFilter == NULL ) {
2015 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
2016 (*pContainingFilter)->f_choice = ftype;
2017 (*pContainingFilter)->f_list = filterToAppend;
2018 (*pContainingFilter)->f_next = NULL;
2020 if ( (*pContainingFilter)->f_choice != ftype ) {
2024 (*pNextFilter)->f_next = filterToAppend;
2026 *pNextFilter = filterToAppend;
2030 #endif /* LDAP_SLAPI */
2035 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
2043 /* spec says return zero if no filter. */
2047 if ( verify_access ) {
2048 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
2050 return LDAP_PARAM_ERROR;
2056 * According to acl.c it is safe to call test_filter() with
2059 rc = test_filter( op, e, f );
2061 case LDAP_COMPARE_TRUE:
2064 case LDAP_COMPARE_FALSE:
2066 case SLAPD_COMPARE_UNDEFINED:
2069 case LDAP_PROTOCOL_ERROR:
2070 /* filter type unknown: spec says return -1 */
2078 #endif /* LDAP_SLAPI */
2082 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
2085 return slapi_filter_test( NULL, e, f, 0 );
2092 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
2095 switch ( f->f_choice ) {
2096 case LDAP_FILTER_AND:
2097 case LDAP_FILTER_NOT:
2098 case LDAP_FILTER_OR: {
2102 * FIXME: altering f; should we use a temporary?
2104 for ( f = f->f_list; f != NULL; f = f->f_next ) {
2105 rc = slapi_filter_apply( f, fn, arg, error_code );
2109 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
2115 case LDAP_FILTER_EQUALITY:
2116 case LDAP_FILTER_SUBSTRINGS:
2117 case LDAP_FILTER_GE:
2118 case LDAP_FILTER_LE:
2119 case LDAP_FILTER_PRESENT:
2120 case LDAP_FILTER_APPROX:
2121 case LDAP_FILTER_EXT:
2122 *error_code = fn( f, arg );
2125 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2128 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
2129 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
2135 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2137 #endif /* LDAP_SLAPI */
2141 slapi_send_ldap_extended_response(
2146 struct berval *response )
2151 rs.sr_err = errornum;
2152 rs.sr_matched = NULL;
2156 rs.sr_rspoid = respName;
2157 rs.sr_rspdata = response;
2159 send_ldap_extended( op, &rs );
2161 return LDAP_SUCCESS;
2162 #else /* LDAP_SLAPI */
2164 #endif /* LDAP_SLAPI */
2169 struct berval **vals,
2174 * FIXME: what's the point?
2177 #else /* LDAP_SLAPI */
2179 #endif /* LDAP_SLAPI */
2182 #define MAX_HOSTNAME 512
2185 slapi_get_hostname( void )
2189 static int been_here = 0;
2190 static char *static_hn = NULL;
2192 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
2194 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
2195 if ( static_hn == NULL) {
2196 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
2197 "Cannot allocate memory for hostname\n" );
2199 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2204 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
2205 slapi_log_error( SLAPI_LOG_FATAL,
2207 "can't get hostname\n" );
2208 slapi_ch_free( (void **)&static_hn );
2210 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2219 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2221 hn = ch_strdup( static_hn );
2224 #else /* LDAP_SLAPI */
2226 #endif /* LDAP_SLAPI */
2230 * FIXME: this should go in an appropriate header ...
2232 extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist );
2242 int rc = LDAP_SUCCESS;
2245 va_start( arglist, fmt );
2246 rc = slapi_int_log_error( severity, subsystem, fmt, arglist );
2250 #else /* LDAP_SLAPI */
2252 #endif /* LDAP_SLAPI */
2257 slapi_timer_current_time( void )
2260 static int first_time = 1;
2261 #if !defined (_WIN32)
2265 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
2268 gettimeofday( &base_time, NULL );
2270 gettimeofday( &now, NULL );
2271 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
2272 (now.tv_usec - base_time.tv_usec);
2273 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
2279 return (slap_get_time() - starttime) * 1000000;
2286 performance_counter_present = QueryPerformanceCounter( &base_time );
2287 QueryPerformanceFrequency( &performance_freq );
2290 if ( !performance_counter_present )
2293 QueryPerformanceCounter( &now );
2294 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2296 #else /* LDAP_SLAPI */
2298 #endif /* LDAP_SLAPI */
2305 slapi_timer_get_time( char *label )
2308 unsigned long start = slapi_timer_current_time();
2309 printf("%10ld %10ld usec %s\n", start, 0, label);
2311 #else /* LDAP_SLAPI */
2313 #endif /* LDAP_SLAPI */
2320 slapi_timer_elapsed_time(
2322 unsigned long start )
2325 unsigned long stop = slapi_timer_current_time();
2326 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2327 #endif /* LDAP_SLAPI */
2331 slapi_free_search_results_internal( Slapi_PBlock *pb )
2334 Slapi_Entry **entries;
2335 int k = 0, nEnt = 0;
2337 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2338 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2343 if ( entries == NULL ) {
2347 for ( k = 0; k < nEnt; k++ ) {
2348 slapi_entry_free( entries[k] );
2351 slapi_ch_free( (void **)&entries );
2352 #endif /* LDAP_SLAPI */
2357 * Internal API to prime a Slapi_PBlock with a Backend.
2359 static int slapi_int_pblock_set_backend( Slapi_PBlock *pb, Backend *be )
2363 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2364 if ( rc != LDAP_SUCCESS )
2368 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2369 if ( rc != LDAP_SUCCESS )
2373 return LDAP_SUCCESS;
2377 * If oldStyle is TRUE, then a value suitable for setting to
2378 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
2379 * (pointer to static storage).
2381 * If oldStyle is FALSE, then a value suitable for setting to
2382 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2383 * a pointer to allocated memory and will include the SASL
2384 * mechanism (if any).
2386 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2391 switch ( authz->sai_method ) {
2392 case LDAP_AUTH_SASL:
2394 authType = SLAPD_AUTH_SASL;
2396 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2397 authType = slapi_ch_malloc( len );
2398 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2401 case LDAP_AUTH_SIMPLE:
2402 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2404 case LDAP_AUTH_NONE:
2405 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2411 if ( is_tls && authType == NULL ) {
2412 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2419 * Internal API to prime a Slapi_PBlock with a Connection.
2421 static int slapi_int_pblock_set_connection( Slapi_PBlock *pb, Connection *conn )
2426 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2427 if ( rc != LDAP_SUCCESS )
2430 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2431 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2432 if ( rc != LDAP_SUCCESS )
2434 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2435 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2436 if ( rc != LDAP_SUCCESS )
2440 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2441 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2442 if ( rc != LDAP_SUCCESS )
2444 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2445 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2446 if ( rc != LDAP_SUCCESS )
2450 #ifdef LDAP_CONNECTIONLESS
2451 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2452 if ( rc != LDAP_SUCCESS )
2456 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2457 if ( rc != LDAP_SUCCESS )
2460 /* Returns pointer to static string */
2461 connAuthType = Authorization2AuthType( &conn->c_authz,
2468 if ( connAuthType != NULL ) {
2469 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2470 if ( rc != LDAP_SUCCESS )
2474 /* Returns pointer to allocated string */
2475 connAuthType = Authorization2AuthType( &conn->c_authz,
2482 if ( connAuthType != NULL ) {
2483 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2484 /* slapi_pblock_set dups this itself */
2485 slapi_ch_free( (void **)&connAuthType );
2486 if ( rc != LDAP_SUCCESS )
2490 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
2491 /* slapi_pblock_set dups this itself */
2492 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)conn->c_authz.sai_dn.bv_val);
2493 if ( rc != LDAP_SUCCESS )
2497 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SSF, (void *)conn->c_ssf);
2498 if ( rc != LDAP_SUCCESS )
2501 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SASL_CONTEXT,
2502 ( conn->c_sasl_authctx != NULL ? conn->c_sasl_authctx :
2503 conn->c_sasl_sockctx ) );
2504 if ( rc != LDAP_SUCCESS )
2509 #endif /* LDAP_SLAPI */
2512 * Internal API to prime a Slapi_PBlock with an Operation.
2514 int slapi_int_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
2522 if ( op->o_bd != NULL ) {
2523 isRoot = be_isroot( op );
2524 isUpdateDn = be_isupdate( op );
2527 rc = slapi_int_pblock_set_backend( pb, op->o_bd );
2528 if ( rc != LDAP_SUCCESS )
2531 rc = slapi_int_pblock_set_connection( pb, op->o_conn );
2532 if ( rc != LDAP_SUCCESS )
2535 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2536 if ( rc != LDAP_SUCCESS )
2539 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2540 if ( rc != LDAP_SUCCESS )
2543 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2544 if ( rc != LDAP_SUCCESS )
2547 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2548 if ( rc != LDAP_SUCCESS )
2551 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2552 if ( rc != LDAP_SUCCESS )
2555 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2556 if ( rc != LDAP_SUCCESS )
2559 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2560 if ( rc != LDAP_SUCCESS)
2563 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2564 if ( rc != LDAP_SUCCESS )
2567 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2568 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
2569 /* Not quite sure what the point of this is. */
2570 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
2571 if ( rc != LDAP_SUCCESS )
2575 return LDAP_SUCCESS;
2581 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2586 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2588 *isSSL = conn->c_is_tls;
2593 return LDAP_SUCCESS;
2596 #endif /* LDAP_SLAPI */
2600 * DS 5.x compatability API follow
2603 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2609 return LDAP_PARAM_ERROR;
2611 at = attr->a_desc->ad_type;
2613 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2615 if ( is_at_single_value( at ) )
2616 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2617 if ( is_at_operational( at ) )
2618 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2619 if ( is_at_obsolete( at ) )
2620 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2621 if ( is_at_collective( at ) )
2622 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2623 if ( is_at_no_user_mod( at ) )
2624 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2626 return LDAP_SUCCESS;
2629 #endif /* LDAP_SLAPI */
2632 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2635 unsigned long flags;
2637 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2639 return (flags & flag) ? 1 : 0;
2642 #endif /* LDAP_SLAPI */
2645 Slapi_Attr *slapi_attr_new( void )
2650 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2658 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2662 AttributeDescription *ad = NULL;
2664 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2680 void slapi_attr_free( Slapi_Attr **a )
2688 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2691 return attr_dup( (Slapi_Attr *)attr );
2697 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2701 struct berval *nvalp;
2703 AttributeDescription *desc = a->a_desc;
2705 if ( desc->ad_type->sat_equality &&
2706 desc->ad_type->sat_equality->smr_normalize ) {
2707 rc = (*desc->ad_type->sat_equality->smr_normalize)(
2708 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2709 desc->ad_type->sat_syntax,
2710 desc->ad_type->sat_equality,
2711 (Slapi_Value *)v, &nval, NULL );
2712 if ( rc != LDAP_SUCCESS ) {
2720 rc = value_add_one( &a->a_vals, (Slapi_Value *)v );
2721 if ( rc == 0 && nvalp != NULL ) {
2722 rc = value_add_one( &a->a_nvals, nvalp );
2724 a->a_nvals = a->a_vals;
2727 if ( nvalp != NULL ) {
2728 slapi_ch_free_string( &nval.bv_val );
2737 int slapi_attr_type2plugin( const char *type, void **pi )
2744 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2747 if ( attr == NULL ) {
2748 return LDAP_PARAM_ERROR;
2751 *type = attr->a_desc->ad_cname.bv_val;
2753 return LDAP_SUCCESS;
2759 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2762 if ( attr == NULL ) {
2763 return LDAP_PARAM_ERROR;
2765 *oidp = attr->a_desc->ad_type->sat_oid;
2767 return LDAP_SUCCESS;
2773 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2781 mr = a->a_desc->ad_type->sat_equality;
2782 rc = value_match( &ret, a->a_desc, mr,
2783 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2784 (struct berval *)v1, (void *)v2, &text );
2785 if ( rc != LDAP_SUCCESS )
2788 return ( ret == 0 ) ? 0 : -1;
2794 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2804 if ( a ->a_vals == NULL ) {
2807 mr = a->a_desc->ad_type->sat_equality;
2808 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2809 rc = value_match( &ret, a->a_desc, mr,
2810 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2811 if ( rc != LDAP_SUCCESS ) {
2818 #endif /* LDAP_SLAPI */
2822 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2825 AttributeDescription *a1 = NULL;
2826 AttributeDescription *a2 = NULL;
2830 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2834 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2838 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2839 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2840 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2843 case SLAPI_TYPE_CMP_EXACT:
2844 ret = ad_cmp( a1, a2 );
2846 case SLAPI_TYPE_CMP_BASE:
2847 ret = ad_base_cmp( a1, a2 );
2849 case SLAPI_TYPE_CMP_SUBTYPE:
2850 ret = is_ad_subtype( a2, a2 );
2863 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2866 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2872 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2875 return slapi_valueset_first_value( &a->a_vals, v );
2881 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2884 return slapi_valueset_next_value( &a->a_vals, hint, v );
2890 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2893 *numValues = slapi_valueset_count( &a->a_vals );
2901 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2904 *vs = &((Slapi_Attr *)a)->a_vals;
2912 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2915 return slapi_attr_get_values( a, vals );
2921 char *slapi_attr_syntax_normalize( const char *s )
2924 AttributeDescription *ad = NULL;
2927 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2931 return ad->ad_cname.bv_val;
2937 Slapi_Value *slapi_value_new( void )
2942 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2950 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2953 return ber_dupbv( NULL, (struct berval *)bval );
2959 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2962 return slapi_value_new_berval( v );
2968 Slapi_Value *slapi_value_new_string(const char *s)
2973 bv.bv_val = (char *)s;
2974 bv.bv_len = strlen( s );
2976 return slapi_value_new_berval( &bv );
2982 Slapi_Value *slapi_value_init(Slapi_Value *val)
2994 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2997 return ber_dupbv( v, bval );
3003 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
3006 v->bv_val = slapi_ch_strdup( (char *)s );
3007 v->bv_len = strlen( s );
3015 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
3018 return slapi_value_new_value( v );
3024 void slapi_value_free(Slapi_Value **value)
3027 if ( value == NULL ) {
3031 if ( (*value) != NULL ) {
3032 slapi_ch_free( (void **)&(*value)->bv_val );
3033 slapi_ch_free( (void **)value );
3038 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
3047 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
3050 if ( value == NULL ) {
3053 if ( value->bv_val != NULL ) {
3054 slapi_ch_free( (void **)&value->bv_val );
3056 slapi_value_init_berval( value, (struct berval *)bval );
3064 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
3067 if ( value == NULL ) {
3070 return slapi_value_set_berval( value, vfrom );
3076 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
3079 if ( value == NULL ) {
3082 if ( value->bv_val != NULL ) {
3083 slapi_ch_free( (void **)&value->bv_val );
3085 value->bv_val = slapi_ch_malloc( len );
3086 value->bv_len = len;
3087 AC_MEMCPY( value->bv_val, val, len );
3095 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
3098 if ( value == NULL ) {
3101 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
3108 int slapi_value_set_int(Slapi_Value *value, int intVal)
3113 snprintf( buf, sizeof( buf ), "%d", intVal );
3115 return slapi_value_set_string( value, buf );
3121 const char *slapi_value_get_string(const Slapi_Value *value)
3124 if ( value == NULL ) return NULL;
3125 if ( value->bv_val == NULL ) return NULL;
3126 if ( !checkBVString( value ) ) return NULL;
3128 return value->bv_val;
3134 int slapi_value_get_int(const Slapi_Value *value)
3137 if ( value == NULL ) return 0;
3138 if ( value->bv_val == NULL ) return 0;
3139 if ( !checkBVString( value ) ) return 0;
3141 return (int)strtol( value->bv_val, NULL, 10 );
3147 unsigned int slapi_value_get_uint(const Slapi_Value *value)
3150 if ( value == NULL ) return 0;
3151 if ( value->bv_val == NULL ) return 0;
3152 if ( !checkBVString( value ) ) return 0;
3154 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
3160 long slapi_value_get_long(const Slapi_Value *value)
3163 if ( value == NULL ) return 0;
3164 if ( value->bv_val == NULL ) return 0;
3165 if ( !checkBVString( value ) ) return 0;
3167 return strtol( value->bv_val, NULL, 10 );
3173 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
3176 if ( value == NULL ) return 0;
3177 if ( value->bv_val == NULL ) return 0;
3178 if ( !checkBVString( value ) ) return 0;
3180 return strtoul( value->bv_val, NULL, 10 );
3186 size_t slapi_value_get_length(const Slapi_Value *value)
3189 if ( value == NULL )
3192 return (size_t) value->bv_len;
3198 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
3201 return slapi_attr_value_cmp( a, v1, v2 );
3207 /* A ValueSet is a container for a BerVarray. */
3208 Slapi_ValueSet *slapi_valueset_new( void )
3213 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
3222 void slapi_valueset_free(Slapi_ValueSet *vs)
3228 ber_bvarray_free( vp );
3229 slapi_ch_free( (void **)&vp );
3236 void slapi_valueset_init(Slapi_ValueSet *vs)
3239 if ( vs != NULL && *vs == NULL ) {
3240 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
3241 (*vs)->bv_val = NULL;
3247 void slapi_valueset_done(Slapi_ValueSet *vs)
3255 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
3257 slapi_ch_free( (void **)&vp->bv_val );
3259 /* but don't free *vs or vs */
3263 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
3268 ber_dupbv( &bv, (Slapi_Value *)addval );
3269 ber_bvarray_add( vs, &bv );
3273 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
3276 return slapi_valueset_next_value( vs, 0, v );
3282 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
3293 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
3304 int slapi_valueset_count( const Slapi_ValueSet *vs )
3315 for ( i = 0; vp[i].bv_val != NULL; i++ )
3325 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
3330 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
3331 slapi_valueset_add_value( vs1, vp );
3336 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
3337 struct berval *val, int access )
3344 slap_access_t slap_access;
3345 AttributeDescription *ad = NULL;
3348 ret = slap_str2ad( attr, &ad, &text );
3349 if ( ret != LDAP_SUCCESS ) {
3353 switch ( access & SLAPI_ACL_ALL ) {
3354 case SLAPI_ACL_COMPARE:
3355 slap_access = ACL_COMPARE;
3357 case SLAPI_ACL_SEARCH:
3358 slap_access = ACL_SEARCH;
3360 case SLAPI_ACL_READ:
3361 slap_access = ACL_READ;
3363 case SLAPI_ACL_WRITE:
3364 case SLAPI_ACL_DELETE:
3366 case SLAPI_ACL_SELF:
3367 slap_access = ACL_WRITE;
3370 return LDAP_INSUFFICIENT_ACCESS;
3374 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3375 return LDAP_PARAM_ERROR;
3378 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3379 return LDAP_PARAM_ERROR;
3382 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3383 return LDAP_PARAM_ERROR;
3386 ret = access_allowed( op, e, ad, val, slap_access, NULL );
3388 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3390 return LDAP_UNWILLING_TO_PERFORM;
3394 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
3398 int rc = LDAP_SUCCESS;
3399 Modifications *ml, *mp;
3401 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3402 return LDAP_PARAM_ERROR;
3405 ml = slapi_int_ldapmods2modifications( mods );
3410 for ( mp = ml; mp != NULL; mp = mp->sml_next ) {
3411 rc = slap_bv2ad( &mp->sml_type, &mp->sml_desc, (const char **)errbuf );
3412 if ( rc != LDAP_SUCCESS ) {
3417 if ( rc == LDAP_SUCCESS ) {
3418 rc = acl_check_modlist( op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3421 /* Careful when freeing the modlist because it has pointers into the mods array. */
3422 for ( ; ml != NULL; ml = mp ) {
3425 /* just free the containing array */
3426 slapi_ch_free( (void **)&ml->sml_values );
3427 slapi_ch_free( (void **)&ml );
3432 return LDAP_UNWILLING_TO_PERFORM;
3437 * Synthesise an LDAPMod array from a Modifications list to pass
3438 * to SLAPI. This synthesis is destructive and as such the
3439 * Modifications list may not be used after calling this
3442 * This function must also be called before slap_mods_check().
3444 LDAPMod **slapi_int_modifications2ldapmods(Modifications **pmodlist)
3447 Modifications *ml, *modlist;
3448 LDAPMod **mods, *modp;
3451 modlist = *pmodlist;
3453 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3456 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3458 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3459 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3461 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3463 /* Take ownership of original type. */
3464 modp->mod_type = ml->sml_type.bv_val;
3465 ml->sml_type.bv_val = NULL;
3467 if ( ml->sml_values != NULL ) {
3468 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
3470 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3471 sizeof(struct berval *) );
3472 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
3473 /* Take ownership of original values. */
3474 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3475 modp->mod_bvalues[j]->bv_len = ml->sml_values[j].bv_len;
3476 modp->mod_bvalues[j]->bv_val = ml->sml_values[j].bv_val;
3477 ml->sml_values[j].bv_len = 0;
3478 ml->sml_values[j].bv_val = NULL;
3480 modp->mod_bvalues[j] = NULL;
3482 modp->mod_bvalues = NULL;
3489 slap_mods_free( modlist );
3499 * Convert a potentially modified array of LDAPMods back to a
3500 * Modification list.
3502 * The returned Modification list contains pointers into the
3503 * LDAPMods array; the latter MUST be freed with
3504 * slapi_int_free_ldapmods() (see below).
3506 Modifications *slapi_int_ldapmods2modifications (LDAPMod **mods)
3509 Modifications *modlist = NULL, **modtail;
3512 if ( mods == NULL ) {
3518 for( modp = mods; *modp != NULL; modp++ ) {
3522 struct berval **bvp;
3524 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3525 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3526 mod->sml_type.bv_val = (*modp)->mod_type;
3527 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3528 mod->sml_desc = NULL;
3529 mod->sml_next = NULL;
3531 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3532 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3535 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3540 mod->sml_values = NULL;
3542 mod->sml_values = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3544 /* NB: This implicitly trusts a plugin to return valid modifications. */
3545 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3546 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3547 mod->sml_values[i].bv_val = (*bvp)->bv_val;
3548 mod->sml_values[i].bv_len = (*bvp)->bv_len;
3551 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3552 mod->sml_values[i].bv_val = *p;
3553 mod->sml_values[i].bv_len = strlen( *p );
3556 mod->sml_values[i].bv_val = NULL;
3557 mod->sml_values[i].bv_len = 0;
3559 mod->sml_nvalues = NULL;
3562 modtail = &mod->sml_next;
3572 * This function only frees the parts of the mods array that
3573 * are not shared with the Modification list that was created
3574 * by slapi_int_ldapmods2modifications().
3577 void slapi_int_free_ldapmods (LDAPMod **mods)
3585 for ( i = 0; mods[i] != NULL; i++ ) {
3587 * Don't free values themselves; they're owned by the
3588 * Modification list. Do free the containing array.
3590 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3591 for ( j = 0; mods[i]->mod_values != NULL && mods[i]->mod_values[j] != NULL; j++ ) {
3592 ch_free( mods[i]->mod_values[j] );
3594 ch_free( mods[i]->mod_values );
3596 ch_free( mods[i]->mod_values );
3598 /* Don't free type, for same reasons. */
3602 #endif /* LDAP_SLAPI */
3606 * Sun ONE DS 5.x computed attribute support. Computed attributes
3607 * allow for dynamically generated operational attributes, a very
3608 * useful thing indeed.
3612 * Write the computed attribute to a BerElement. Complementary
3613 * functions need to be defined for anything that replaces
3614 * op->o_callback->sc_sendentry, if you wish to make computed
3615 * attributes available to it.
3617 int slapi_int_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
3620 Operation *op = NULL;
3622 AttributeDescription *desc = NULL;
3638 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
3639 if ( rc != 0 || op == NULL ) {
3643 ber = (BerElement *)c->cac_private;
3646 if ( c->cac_attrs == NULL ) {
3647 /* All attrs request, skip operational attributes */
3648 if ( is_at_operational( desc->ad_type ) ) {
3652 /* Specific attrs requested */
3653 if ( is_at_operational( desc->ad_type ) ) {
3654 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3658 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3664 if ( !access_allowed( op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
3665 slapi_log_error( SLAPI_LOG_ACL, "slapi_int_compute_output_ber",
3666 "acl: access to attribute %s not allowed\n",
3667 desc->ad_cname.bv_val );
3671 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
3673 slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
3674 "ber_printf failed\n");
3678 if ( !c->cac_attrsonly && a->a_vals != NULL ) {
3679 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
3680 if ( !access_allowed( op, e,
3681 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
3682 slapi_log_error( SLAPI_LOG_ACL, "slapi_int_compute_output_ber",
3684 "acl: access to %s, value %d not allowed\n",
3685 op->o_connid, desc->ad_cname.bv_val, i );
3689 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
3690 slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
3691 "ber_printf failed\n");
3697 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
3698 slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
3699 "ber_printf failed\n" );
3710 * For some reason Sun don't use the normal plugin mechanism
3711 * registration path to register an "evaluator" function (an
3712 * "evaluator" is responsible for adding computed attributes;
3713 * the nomenclature is somewhat confusing).
3715 * As such slapi_compute_add_evaluator() registers the
3716 * function directly.
3718 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3721 Slapi_PBlock *pPlugin = NULL;
3724 pPlugin = slapi_pblock_new();
3725 if ( pPlugin == NULL ) {
3726 rc = LDAP_NO_MEMORY;
3730 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3731 if ( rc != LDAP_SUCCESS ) {
3735 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3736 if ( rc != LDAP_SUCCESS ) {
3740 rc = slapi_int_register_plugin( NULL, pPlugin );
3747 if ( rc != LDAP_SUCCESS ) {
3748 if ( pPlugin != NULL ) {
3749 slapi_pblock_destroy( pPlugin );
3757 #endif /* LDAP_SLAPI */
3761 * See notes above regarding slapi_compute_add_evaluator().
3763 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3766 Slapi_PBlock *pPlugin = NULL;
3769 pPlugin = slapi_pblock_new();
3770 if ( pPlugin == NULL ) {
3771 rc = LDAP_NO_MEMORY;
3775 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3776 if ( rc != LDAP_SUCCESS ) {
3780 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3781 if ( rc != LDAP_SUCCESS ) {
3785 rc = slapi_int_register_plugin( NULL, pPlugin );
3792 if ( rc != LDAP_SUCCESS ) {
3793 if ( pPlugin != NULL ) {
3794 slapi_pblock_destroy( pPlugin );
3802 #endif /* LDAP_SLAPI */
3806 * Call compute evaluators
3808 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3812 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3814 rc = slapi_int_get_plugins( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3815 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3816 /* Nothing to do; front-end should ignore. */
3820 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3822 * -1: no attribute matched requested type
3823 * 0: one attribute matched
3824 * >0: error happened
3826 rc = (*pGetPlugin)( c, type, e, outputfn );
3832 slapi_ch_free( (void **)&tmpPlugin );
3837 #endif /* LDAP_SLAPI */
3840 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3846 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3851 return slapi_int_call_plugins( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3854 #endif /* LDAP_SLAPI */
3858 * New API to provide the plugin with access to the search
3859 * pblock. Have informed Sun DS team.
3861 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3867 if ( c->cac_pb == NULL )
3875 #endif /* LDAP_SLAPI */
3878 Slapi_Mutex *slapi_new_mutex( void )
3883 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3884 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3885 slapi_ch_free( (void **)&m );
3895 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3898 if ( mutex != NULL ) {
3899 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3900 slapi_ch_free( (void **)&mutex);
3905 void slapi_lock_mutex( Slapi_Mutex *mutex )
3908 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3912 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3915 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3921 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3926 if ( mutex == NULL ) {
3930 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3931 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3932 slapi_ch_free( (void **)&cv );
3936 /* XXX struct copy */
3937 cv->mutex = mutex->mutex;
3945 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3948 if ( cvar != NULL ) {
3949 ldap_pvt_thread_cond_destroy( &cvar->cond );
3950 slapi_ch_free( (void **)&cvar );
3955 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3958 if ( cvar == NULL ) {
3962 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3968 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3971 if ( cvar == NULL ) {
3976 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3979 return ldap_pvt_thread_cond_signal( &cvar->cond );
3985 int slapi_int_access_allowed( Operation *op,
3987 AttributeDescription *desc,
3989 slap_access_t access,
3990 AccessControlState *state )
3993 int rc, slap_access = 0;
3994 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3996 if ( op->o_pb == NULL ) {
3997 /* internal operation */
4003 slap_access |= SLAPI_ACL_ADD | SLAPI_ACL_DELETE | SLAPI_ACL_WRITE;
4006 slap_access |= SLAPI_ACL_READ;
4009 slap_access |= SLAPI_ACL_SEARCH;
4012 slap_access = ACL_COMPARE;
4018 rc = slapi_int_get_plugins( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
4019 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
4020 /* nothing to do; allowed access */
4024 slapi_int_pblock_set_operation( op->o_pb, op );
4026 rc = 1; /* default allow policy */
4028 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
4033 rc = (*pGetPlugin)( op->o_pb, entry, desc->ad_cname.bv_val,
4034 val, slap_access, (void *)state );
4040 slapi_ch_free( (void **)&tmpPlugin );
4045 #endif /* LDAP_SLAPI */
4049 * There is no documentation for this.
4051 int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
4064 rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
4065 if ( rc != LDAP_SUCCESS ) {
4069 if ( lrdn[1] != NULL ) {
4070 return -1; /* not single valued */
4075 *type = slapi_ch_strdup( ava->la_attr.bv_val );
4076 ber_dupbv( bv, &ava->la_value );
4083 #endif /* LDAP_SLAPI */
4086 char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
4089 struct berval new_dn, parent_dn, newrdn;
4091 new_dn.bv_val = NULL;
4093 parent_dn.bv_val = (char *)dn;
4094 parent_dn.bv_len = strlen( dn );
4096 newrdn.bv_val = (char *)rdn;
4097 newrdn.bv_len = strlen( rdn );
4099 build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
4101 return new_dn.bv_val;
4104 #endif /* LDAP_SLAPI */
4107 int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
4112 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
4113 size_t textlen = sizeof textbuf;
4116 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
4119 rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
4121 return ( rc == LDAP_SUCCESS ) ? 0 : 1;
4124 #endif /* LDAP_SLAPI */
4127 int slapi_entry_rdn_values_present( const Slapi_Entry *e )
4132 int i = 0, match = 0;
4134 rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
4135 if ( rc != LDAP_SUCCESS ) {
4139 if ( dn[0] != NULL ) {
4140 LDAPRDN rdn = dn[0];
4142 for ( i = 0; rdn[i] != NULL; i++ ) {
4143 LDAPAVA *ava = &rdn[0][i];
4144 Slapi_Attr *a = NULL;
4146 if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
4147 slapi_attr_value_find( a, &ava->la_value ) == 0 )
4154 return ( i == match );
4157 #endif /* LDAP_SLAPI */
4160 int slapi_entry_add_rdn_values( Slapi_Entry *e )
4166 rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
4167 if ( rc != LDAP_SUCCESS ) {
4171 if ( dn[0] != NULL ) {
4172 LDAPRDN rdn = dn[0];
4173 struct berval *vals[2];
4175 for ( i = 0; rdn[i] != NULL; i++ ) {
4176 LDAPAVA *ava = &rdn[0][i];
4177 Slapi_Attr *a = NULL;
4179 if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
4180 slapi_attr_value_find( a, &ava->la_value ) == 0 )
4183 vals[0] = &ava->la_value;
4186 slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
4192 return LDAP_SUCCESS;
4195 #endif /* LDAP_SLAPI */
4198 const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
4202 const char *uniqueid;
4204 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
4205 if ( attr == NULL ) {
4209 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
4210 return slapi_value_get_string( &attr->a_vals[0] );
4212 #endif /* LDAP_SLAPI */
4217 void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
4222 attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
4224 bv.bv_val = uniqueid;
4225 bv.bv_len = strlen( uniqueid );
4226 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
4227 #endif /* LDAP_SLAPI */
4230 LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
4238 size = sizeof("ldap:///");
4241 size += strlen( ldaphost );
4242 if ( ldapport != 0 )
4245 url = slapi_ch_malloc( size );
4247 if ( ldapport != 0 ) {
4248 sprintf( url, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
4250 sprintf( url, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
4253 rc = ldap_initialize( &ld, url );
4255 slapi_ch_free_string( &url );
4257 return ( rc == LDAP_SUCCESS ) ? ld : NULL;
4260 #endif /* LDAP_SLAPI */
4263 void slapi_ldap_unbind( LDAP *ld )
4267 #endif /* LDAP_SLAPI */