2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2002-2010 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>
39 * server start time (should we use a struct timeval also in slapd?
41 static struct timeval base_time;
42 ldap_pvt_thread_mutex_t slapi_hn_mutex;
43 ldap_pvt_thread_mutex_t slapi_time_mutex;
46 ldap_pvt_thread_mutex_t mutex;
49 struct slapi_condvar {
50 ldap_pvt_thread_cond_t cond;
51 ldap_pvt_thread_mutex_t mutex;
54 static int checkBVString(const struct berval *bv)
58 for ( i = 0; i < bv->bv_len; i++ ) {
59 if ( bv->bv_val[i] == '\0' )
62 if ( bv->bv_val[i] != '\0' )
69 * This function converts an array of pointers to berval objects to
70 * an array of berval objects.
75 struct berval **bvptr,
79 int rc = LDAP_SUCCESS;
83 if ( bvptr == NULL || *bvptr == NULL ) {
87 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
93 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
94 if ( tmpberval == NULL ) {
95 return LDAP_NO_MEMORY;
98 for ( i = 0; bvptr[i] != NULL; i++ ) {
99 tmpberval[i].bv_val = bvptr[i]->bv_val;
100 tmpberval[i].bv_len = bvptr[i]->bv_len;
102 tmpberval[i].bv_val = NULL;
103 tmpberval[i].bv_len = 0;
105 if ( rc == LDAP_SUCCESS ) {
117 return str2entry( s );
128 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
129 s = entry2str( e, len );
131 ret = slapi_ch_strdup( s );
132 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
138 slapi_entry_get_dn( Slapi_Entry *e )
140 return e->e_name.bv_val;
144 slapi_x_entry_get_id( Slapi_Entry *e )
150 slapi_int_dn_pretty( struct berval *in, struct berval *out )
152 Syntax *syntax = slap_schema.si_syn_distinguishedName;
154 assert( syntax != NULL );
156 return (syntax->ssyn_pretty)( syntax, in, out, NULL );
160 slapi_int_dn_normalize( struct berval *in, struct berval *out )
162 MatchingRule *mr = slap_schema.si_mr_distinguishedNameMatch;
163 Syntax *syntax = slap_schema.si_syn_distinguishedName;
165 assert( mr != NULL );
167 return (mr->smr_normalize)( 0, syntax, mr, in, out, NULL );
175 struct berval dn = BER_BVNULL;
178 dn.bv_len = strlen( ldn );
180 slapi_int_dn_pretty( &dn, &e->e_name );
181 slapi_int_dn_normalize( &dn, &e->e_nname );
185 slapi_entry_dup( Slapi_Entry *e )
187 return entry_dup( e );
191 slapi_entry_attr_delete(
195 AttributeDescription *ad = NULL;
198 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
199 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
202 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
203 return 0; /* attribute is deleted */
205 return -1; /* something went wrong */
210 slapi_entry_alloc( void )
212 return (Slapi_Entry *)entry_alloc();
216 slapi_entry_free( Slapi_Entry *e )
223 slapi_entry_attr_merge(
226 struct berval **vals )
228 AttributeDescription *ad = NULL;
233 rc = slap_str2ad( type, &ad, &text );
234 if ( rc != LDAP_SUCCESS ) {
238 rc = bvptr2obj( vals, &bv, NULL );
239 if ( rc != LDAP_SUCCESS ) {
243 rc = attr_merge_normalize( e, ad, bv, NULL );
250 slapi_entry_attr_find(
255 AttributeDescription *ad = NULL;
259 rc = slap_str2ad( type, &ad, &text );
260 if ( rc != LDAP_SUCCESS ) {
264 *attr = attr_find( e->e_attrs, ad );
265 if ( *attr == NULL ) {
273 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
275 AttributeDescription *ad = NULL;
280 rc = slap_str2ad( type, &ad, &text );
281 if ( rc != LDAP_SUCCESS ) {
285 attr = attr_find( e->e_attrs, ad );
286 if ( attr == NULL ) {
290 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
293 p = slapi_value_get_string( &attr->a_vals[0] );
295 return slapi_ch_strdup( p );
303 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
305 AttributeDescription *ad = NULL;
310 rc = slap_str2ad( type, &ad, &text );
311 if ( rc != LDAP_SUCCESS ) {
315 attr = attr_find( e->e_attrs, ad );
316 if ( attr == NULL ) {
320 return slapi_value_get_int( attr->a_vals );
324 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
326 AttributeDescription *ad = NULL;
331 rc = slap_str2ad( type, &ad, &text );
332 if ( rc != LDAP_SUCCESS ) {
336 attr = attr_find( e->e_attrs, ad );
337 if ( attr == NULL ) {
341 return slapi_value_get_long( attr->a_vals );
345 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
347 AttributeDescription *ad = NULL;
352 rc = slap_str2ad( type, &ad, &text );
353 if ( rc != LDAP_SUCCESS ) {
357 attr = attr_find( e->e_attrs, ad );
358 if ( attr == NULL ) {
362 return slapi_value_get_uint( attr->a_vals );
366 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
368 AttributeDescription *ad = NULL;
373 rc = slap_str2ad( type, &ad, &text );
374 if ( rc != LDAP_SUCCESS ) {
378 attr = attr_find( e->e_attrs, ad );
379 if ( attr == NULL ) {
383 return slapi_value_get_ulong( attr->a_vals );
387 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
390 AttributeDescription *ad = NULL;
395 rc = slap_str2ad( type, &ad, &text );
396 if ( rc != LDAP_SUCCESS ) {
400 attr = attr_find( e->e_attrs, ad );
401 if ( attr == NULL ) {
405 bv.bv_val = (char *)value;
406 bv.bv_len = strlen( value );
408 return ( slapi_attr_value_find( attr, &bv ) != -1 );
412 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
414 AttributeDescription *ad = NULL;
419 rc = slap_str2ad( type, &ad, &text );
420 if ( rc != LDAP_SUCCESS ) {
424 attr_delete ( &e->e_attrs, ad );
425 if ( value != NULL ) {
426 bv.bv_val = (char *)value;
427 bv.bv_len = strlen(value);
428 attr_merge_normalize_one( e, ad, &bv, NULL );
433 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
437 snprintf( buf, sizeof( buf ), "%d", l );
438 slapi_entry_attr_set_charptr( e, type, buf );
442 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
446 snprintf( buf, sizeof( buf ), "%u", l );
447 slapi_entry_attr_set_charptr( e, type, buf );
451 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
455 snprintf( buf, sizeof( buf ), "%ld", l );
456 slapi_entry_attr_set_charptr( e, type, buf );
460 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
464 snprintf( buf, sizeof( buf ), "%lu", l );
465 slapi_entry_attr_set_charptr( e, type, buf );
469 slapi_is_rootdse( const char *dn )
471 return ( dn == NULL || dn[0] == '\0' );
475 slapi_entry_has_children( const Slapi_Entry *e )
478 Backend *be = select_backend( (struct berval *)&e->e_nname, 0 );
479 int rc, hasSubordinates = 0;
481 if ( be == NULL || be->be_has_subordinates == 0 ) {
485 pb = slapi_pblock_new();
489 slapi_int_connection_init_pb( pb, LDAP_REQ_SEARCH );
491 rc = slapi_pblock_set( pb, SLAPI_TARGET_DN, slapi_entry_get_dn(
493 if ( rc == LDAP_SUCCESS ) {
494 pb->pb_op->o_bd = be;
495 rc = be->be_has_subordinates( pb->pb_op, (Entry *) e,
499 slapi_pblock_destroy( pb );
501 return ( rc == LDAP_SUCCESS && hasSubordinates == LDAP_COMPARE_TRUE );
505 * Return approximate size of the entry rounded to the nearest
506 * 1K. Only the size of the attribute values are counted in the
507 * Sun implementation.
509 * http://docs.sun.com/source/816-6701-10/funcref.html#1017388
511 size_t slapi_entry_size(Slapi_Entry *e)
517 for ( size = 0, a = e->e_attrs; a != NULL; a = a->a_next ) {
518 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
519 size += a->a_vals[i].bv_len + 1;
524 size -= (size % 1024);
530 * Add values to entry.
533 * LDAP_SUCCESS Values added to entry
534 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
535 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
538 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
543 char textbuf[SLAP_TEXT_BUFLEN];
545 mod.sm_op = LDAP_MOD_ADD;
548 mod.sm_type.bv_val = (char *)type;
549 mod.sm_type.bv_len = strlen( type );
551 rc = slap_str2ad( type, &mod.sm_desc, &text );
552 if ( rc != LDAP_SUCCESS ) {
556 if ( vals == NULL ) {
557 /* Apparently vals can be NULL
558 * FIXME: sm_values = NULL ? */
559 mod.sm_values = (BerVarray)ch_malloc( sizeof(struct berval) );
560 mod.sm_values->bv_val = NULL;
564 rc = bvptr2obj( vals, &mod.sm_values, &mod.sm_numvals );
565 if ( rc != LDAP_SUCCESS ) {
566 return LDAP_CONSTRAINT_VIOLATION;
569 mod.sm_nvalues = NULL;
571 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
573 slapi_ch_free( (void **)&mod.sm_values );
575 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
579 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
581 return slapi_entry_add_values( e, type, vals );
585 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
587 AttributeDescription *ad = NULL;
591 rc = slap_str2ad( type, &ad, &text );
592 if ( rc != LDAP_SUCCESS ) {
596 return attr_merge_normalize( e, ad, *vs, NULL );
600 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
605 char textbuf[SLAP_TEXT_BUFLEN];
607 mod.sm_op = LDAP_MOD_DELETE;
610 mod.sm_type.bv_val = (char *)type;
611 mod.sm_type.bv_len = strlen( type );
613 if ( vals == NULL ) {
614 /* If vals is NULL, this is a NOOP. */
618 rc = slap_str2ad( type, &mod.sm_desc, &text );
619 if ( rc != LDAP_SUCCESS ) {
623 if ( vals[0] == NULL ) {
624 /* SLAPI doco says LDApb_opERATIONS_ERROR but LDAP_OTHER is better */
625 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
628 rc = bvptr2obj( vals, &mod.sm_values, &mod.sm_numvals );
629 if ( rc != LDAP_SUCCESS ) {
630 return LDAP_CONSTRAINT_VIOLATION;
632 mod.sm_nvalues = NULL;
634 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
636 slapi_ch_free( (void **)&mod.sm_values );
642 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
644 return slapi_entry_delete_values( e, type, vals );
648 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
650 return slapi_entry_attr_merge( e, (char *)type, vals );
654 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
656 AttributeDescription *ad = NULL;
660 rc = slap_str2ad( type, &ad, &text );
661 if ( rc != LDAP_SUCCESS ) {
665 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
666 if ( rc != LDAP_SUCCESS ) {
674 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
678 val.bv_val = (char *)value;
679 val.bv_len = strlen( value );
681 return slapi_entry_add_value( e, type, &val );
685 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
687 Slapi_Value *vals[2];
690 val.bv_val = (char *)value;
691 val.bv_len = strlen( value );
695 return slapi_entry_delete_values_sv( e, type, vals );
699 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
701 return slapi_entry_attr_merge( e, (char *)type, vals );
705 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
713 return ( *attr != NULL ) ? 0 : -1;
717 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
723 if ( prevattr == NULL ) {
727 *attr = prevattr->a_next;
729 return ( *attr != NULL ) ? 0 : -1;
733 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
735 AttributeDescription *ad = NULL;
740 rc = slap_str2ad( type, &ad, &text );
741 if ( rc != LDAP_SUCCESS ) {
745 attr_delete( &e->e_attrs, ad );
747 rc = bvptr2obj( vals, &bv, NULL );
748 if ( rc != LDAP_SUCCESS ) {
752 rc = attr_merge_normalize( e, ad, bv, NULL );
753 slapi_ch_free( (void **)&bv );
754 if ( rc != LDAP_SUCCESS ) {
762 * FIXME -- The caller must free the allocated memory.
763 * In Netscape they do not have to.
766 slapi_attr_get_values(
768 struct berval ***vals )
773 if ( attr == NULL ) {
777 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
781 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
782 for ( j = 0; j < i; j++ ) {
783 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
787 *vals = (struct berval **)bv;
793 slapi_dn_normalize( char *dn )
798 assert( dn != NULL );
801 bdn.bv_len = strlen( dn );
803 if ( slapi_int_dn_pretty( &bdn, &pdn ) != LDAP_SUCCESS ) {
811 slapi_dn_normalize_case( char *dn )
816 assert( dn != NULL );
819 bdn.bv_len = strlen( dn );
821 if ( slapi_int_dn_normalize( &bdn, &ndn ) != LDAP_SUCCESS ) {
833 struct berval bdn, ndn;
834 struct berval bsuffix, nsuffix;
837 assert( dn != NULL );
838 assert( suffix != NULL );
841 bdn.bv_len = strlen( dn );
843 bsuffix.bv_val = suffix;
844 bsuffix.bv_len = strlen( suffix );
846 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
850 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
853 slapi_ch_free( (void **)&ndn.bv_val );
857 rc = dnIsSuffix( &ndn, &nsuffix );
859 slapi_ch_free( (void **)&ndn.bv_val );
860 slapi_ch_free( (void **)&nsuffix.bv_val );
867 const char *parentdn,
868 const char *childdn )
870 struct berval assertedParentDN, normalizedAssertedParentDN;
871 struct berval childDN, normalizedChildDN;
872 struct berval normalizedParentDN;
875 assert( parentdn != NULL );
876 assert( childdn != NULL );
878 assertedParentDN.bv_val = (char *)parentdn;
879 assertedParentDN.bv_len = strlen( parentdn );
881 if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
882 &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
887 childDN.bv_val = (char *)childdn;
888 childDN.bv_len = strlen( childdn );
890 if ( dnNormalize( 0, NULL, NULL, &childDN,
891 &normalizedChildDN, NULL ) != LDAP_SUCCESS )
893 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
897 dnParent( &normalizedChildDN, &normalizedParentDN );
899 if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
900 &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
905 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
906 slapi_ch_free( (void **)&normalizedChildDN.bv_val );
908 return ( match == 0 );
912 * Returns DN of the parent entry, or NULL if the DN is
913 * an empty string or NULL, or has no parent.
916 slapi_dn_parent( const char *_dn )
918 struct berval dn, prettyDN;
919 struct berval parentDN;
926 dn.bv_val = (char *)_dn;
927 dn.bv_len = strlen( _dn );
929 if ( dn.bv_len == 0 ) {
933 if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
937 dnParent( &prettyDN, &parentDN ); /* in-place */
939 if ( parentDN.bv_len == 0 ) {
940 slapi_ch_free_string( &prettyDN.bv_val );
944 ret = slapi_ch_strdup( parentDN.bv_val );
945 slapi_ch_free_string( &prettyDN.bv_val );
950 int slapi_dn_isbesuffix( Slapi_PBlock *pb, char *ldn )
955 if ( slapi_is_rootdse( ldn ) ) {
959 /* according to spec should already be normalized */
960 ndn.bv_len = strlen( ldn );
963 be = select_backend( &pb->pb_op->o_req_ndn, 0 );
968 return be_issuffix( be, &ndn );
972 * Returns DN of the parent entry; or NULL if the DN is
973 * an empty string, if the DN has no parent, or if the
974 * DN is the suffix of the backend database
976 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *ldn )
979 struct berval dn, prettyDN;
980 struct berval normalizedDN, parentDN;
987 PBLOCK_ASSERT_OP( pb, 0 );
989 if ( slapi_is_rootdse( ldn ) ) {
993 dn.bv_val = (char *)ldn;
994 dn.bv_len = strlen( ldn );
996 if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
1000 be = select_backend( &pb->pb_op->o_req_ndn, 0 );
1002 if ( be == NULL || be_issuffix( be, &normalizedDN ) == 0 ) {
1003 dnParent( &prettyDN, &parentDN );
1005 if ( parentDN.bv_len != 0 )
1006 parent = slapi_ch_strdup( parentDN.bv_val );
1009 slapi_ch_free_string( &prettyDN.bv_val );
1010 slapi_ch_free_string( &normalizedDN.bv_val );
1016 slapi_dn_ignore_case( char *dn )
1018 return slapi_dn_normalize_case( dn );
1022 slapi_ch_malloc( unsigned long size )
1024 return ch_malloc( size );
1028 slapi_ch_free( void **ptr )
1030 if ( ptr == NULL || *ptr == NULL )
1037 slapi_ch_free_string( char **ptr )
1039 slapi_ch_free( (void **)ptr );
1043 slapi_ch_array_free( char **arrayp )
1047 if ( arrayp != NULL ) {
1048 for ( p = arrayp; *p != NULL; p++ ) {
1049 slapi_ch_free( (void **)p );
1051 slapi_ch_free( (void **)&arrayp );
1056 slapi_ch_bvdup(const struct berval *v)
1058 return ber_dupbv(NULL, (struct berval *)v);
1062 slapi_ch_bvecdup(const struct berval **v)
1071 for ( i = 0; v[i] != NULL; i++ )
1074 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1076 for ( i = 0; v[i] != NULL; i++ ) {
1077 rv[i] = slapi_ch_bvdup( v[i] );
1086 unsigned long nelem,
1087 unsigned long size )
1089 return ch_calloc( nelem, size );
1095 unsigned long size )
1097 return ch_realloc( block, size );
1101 slapi_ch_strdup( const char *s )
1103 return ch_strdup( s );
1107 slapi_ch_stlen( const char *s )
1113 slapi_control_present(
1114 LDAPControl **controls,
1116 struct berval **val,
1130 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1131 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1136 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1138 *val = &controls[i]->ldctl_value;
1143 *iscritical = controls[i]->ldctl_iscritical;
1153 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1154 unsigned long *slapi_mask)
1156 *slapi_mask = SLAPI_OPERATION_NONE;
1158 if ( slap_mask & SLAP_CTRL_ABANDON )
1159 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1161 if ( slap_mask & SLAP_CTRL_ADD )
1162 *slapi_mask |= SLAPI_OPERATION_ADD;
1164 if ( slap_mask & SLAP_CTRL_BIND )
1165 *slapi_mask |= SLAPI_OPERATION_BIND;
1167 if ( slap_mask & SLAP_CTRL_COMPARE )
1168 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1170 if ( slap_mask & SLAP_CTRL_DELETE )
1171 *slapi_mask |= SLAPI_OPERATION_DELETE;
1173 if ( slap_mask & SLAP_CTRL_MODIFY )
1174 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1176 if ( slap_mask & SLAP_CTRL_RENAME )
1177 *slapi_mask |= SLAPI_OPERATION_MODDN;
1179 if ( slap_mask & SLAP_CTRL_SEARCH )
1180 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1182 if ( slap_mask & SLAP_CTRL_UNBIND )
1183 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1187 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1188 slap_mask_t *slap_mask)
1192 if ( slapi_mask & SLAPI_OPERATION_BIND )
1193 *slap_mask |= SLAP_CTRL_BIND;
1195 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1196 *slap_mask |= SLAP_CTRL_UNBIND;
1198 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1199 *slap_mask |= SLAP_CTRL_SEARCH;
1201 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1202 *slap_mask |= SLAP_CTRL_MODIFY;
1204 if ( slapi_mask & SLAPI_OPERATION_ADD )
1205 *slap_mask |= SLAP_CTRL_ADD;
1207 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1208 *slap_mask |= SLAP_CTRL_DELETE;
1210 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1211 *slap_mask |= SLAP_CTRL_RENAME;
1213 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1214 *slap_mask |= SLAP_CTRL_COMPARE;
1216 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1217 *slap_mask |= SLAP_CTRL_ABANDON;
1219 *slap_mask |= SLAP_CTRL_GLOBAL;
1223 slapi_int_parse_control(
1228 /* Plugins must deal with controls themselves. */
1230 return LDAP_SUCCESS;
1234 slapi_register_supported_control(
1236 unsigned long controlops )
1238 slap_mask_t controlmask;
1240 slapiControlOp2SlapControlMask( controlops, &controlmask );
1242 register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL );
1246 slapi_get_supported_controls(
1248 unsigned long **ctrlopsp )
1252 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1253 if ( rc != LDAP_SUCCESS ) {
1257 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1258 /* In place, naughty. */
1259 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1262 return LDAP_SUCCESS;
1266 slapi_dup_control( LDAPControl *ctrl )
1270 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1271 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1272 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1273 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1279 slapi_register_supported_saslmechanism( char *mechanism )
1281 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1282 slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1283 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1287 slapi_get_supported_saslmechanisms( void )
1289 /* FIXME -- can not get the saslmechanism without a connection. */
1290 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1291 "can not get the SASL mechanism list "
1292 "without a connection\n" );
1297 slapi_get_supported_extended_ops( void )
1300 char **ppExtOpOID = NULL;
1303 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1307 for ( j = 0; slapi_int_get_supported_extop( j ) != NULL; j++ ) {
1312 if ( numExtOps == 0 ) {
1316 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1317 for ( k = 0; k < i; k++ ) {
1320 bv = get_supported_extop( k );
1321 assert( bv != NULL );
1323 ppExtOpOID[ k ] = bv->bv_val;
1326 for ( ; k < j; k++ ) {
1329 bv = slapi_int_get_supported_extop( k );
1330 assert( bv != NULL );
1332 ppExtOpOID[ i + k ] = bv->bv_val;
1334 ppExtOpOID[ i + k ] = NULL;
1340 slapi_send_ldap_result(
1346 struct berval **urls )
1350 PBLOCK_ASSERT_OP( pb, 0 );
1355 rs->sr_matched = matched;
1359 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1360 send_ldap_sasl( pb->pb_op, rs );
1361 } else if ( rs->sr_rspoid != NULL ) {
1362 send_ldap_extended( pb->pb_op, rs );
1364 if ( pb->pb_op->o_tag == LDAP_REQ_SEARCH )
1365 rs->sr_nentries = nentries;
1367 bvptr2obj( urls, &rs->sr_ref, NULL );
1369 send_ldap_result( pb->pb_op, rs );
1372 slapi_ch_free( (void **)&rs->sr_ref );
1377 slapi_send_ldap_search_entry(
1380 LDAPControl **ectrls,
1384 SlapReply rs = { REP_SEARCH };
1386 AttributeName *an = NULL;
1390 assert( pb->pb_op != NULL );
1392 if ( attrs != NULL ) {
1393 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1399 an = (AttributeName *) slapi_ch_calloc( i + 1, sizeof(AttributeName) );
1400 for ( i = 0; attrs[i] != NULL; i++ ) {
1401 an[j].an_name.bv_val = attrs[i];
1402 an[j].an_name.bv_len = strlen( attrs[i] );
1403 an[j].an_desc = NULL;
1404 if ( slap_bv2ad( &an[j].an_name, &an[j].an_desc, &text ) == LDAP_SUCCESS) {
1408 an[j].an_name.bv_len = 0;
1409 an[j].an_name.bv_val = NULL;
1412 rs.sr_err = LDAP_SUCCESS;
1413 rs.sr_matched = NULL;
1416 rs.sr_ctrls = ectrls;
1418 rs.sr_operational_attrs = NULL;
1423 rc = send_search_entry( pb->pb_op, &rs );
1425 slapi_ch_free( (void **)&an );
1431 slapi_send_ldap_search_reference(
1434 struct berval **references,
1435 LDAPControl **ectrls,
1436 struct berval **v2refs
1439 SlapReply rs = { REP_SEARCHREF };
1442 rs.sr_err = LDAP_SUCCESS;
1443 rs.sr_matched = NULL;
1446 rc = bvptr2obj( references, &rs.sr_ref, NULL );
1447 if ( rc != LDAP_SUCCESS ) {
1451 rs.sr_ctrls = ectrls;
1453 rs.sr_operational_attrs = NULL;
1456 if ( v2refs != NULL ) {
1457 rc = bvptr2obj( v2refs, &rs.sr_v2ref, NULL );
1458 if ( rc != LDAP_SUCCESS ) {
1459 slapi_ch_free( (void **)&rs.sr_ref );
1466 rc = send_search_reference( pb->pb_op, &rs );
1468 slapi_ch_free( (void **)&rs.sr_ref );
1469 slapi_ch_free( (void **)&rs.sr_v2ref );
1475 slapi_str2filter( char *str )
1477 return str2filter( str );
1489 slapi_filter_dup( Slapi_Filter *filter )
1491 return filter_dup( filter, NULL );
1495 slapi_filter_get_choice( Slapi_Filter *f )
1509 slapi_filter_get_ava(
1512 struct berval **bval )
1515 int rc = LDAP_SUCCESS;
1517 assert( type != NULL );
1518 assert( bval != NULL );
1523 ftype = f->f_choice;
1524 if ( ftype == LDAP_FILTER_EQUALITY
1525 || ftype == LDAP_FILTER_GE
1526 || ftype == LDAP_FILTER_LE
1527 || ftype == LDAP_FILTER_APPROX ) {
1529 * According to the SLAPI Reference Manual these are
1532 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1533 *bval = &f->f_un.f_un_ava->aa_value;
1534 } else { /* filter type not supported */
1542 slapi_filter_list_first( Slapi_Filter *f )
1550 ftype = f->f_choice;
1551 if ( ftype == LDAP_FILTER_AND
1552 || ftype == LDAP_FILTER_OR
1553 || ftype == LDAP_FILTER_NOT ) {
1554 return (Slapi_Filter *)f->f_list;
1561 slapi_filter_list_next(
1563 Slapi_Filter *fprev )
1571 ftype = f->f_choice;
1572 if ( ftype == LDAP_FILTER_AND
1573 || ftype == LDAP_FILTER_OR
1574 || ftype == LDAP_FILTER_NOT )
1576 return fprev->f_next;
1583 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1589 switch ( f->f_choice ) {
1590 case LDAP_FILTER_GE:
1591 case LDAP_FILTER_LE:
1592 case LDAP_FILTER_EQUALITY:
1593 case LDAP_FILTER_APPROX:
1594 *type = f->f_av_desc->ad_cname.bv_val;
1596 case LDAP_FILTER_SUBSTRINGS:
1597 *type = f->f_sub_desc->ad_cname.bv_val;
1599 case LDAP_FILTER_PRESENT:
1600 *type = f->f_desc->ad_cname.bv_val;
1602 case LDAP_FILTER_EXT:
1603 *type = f->f_mr_desc->ad_cname.bv_val;
1606 /* Complex filters need not apply. */
1615 slapi_x_filter_set_attribute_type( Slapi_Filter *f, const char *type )
1617 AttributeDescription **adp, *ad = NULL;
1625 switch ( f->f_choice ) {
1626 case LDAP_FILTER_GE:
1627 case LDAP_FILTER_LE:
1628 case LDAP_FILTER_EQUALITY:
1629 case LDAP_FILTER_APPROX:
1630 adp = &f->f_av_desc;
1632 case LDAP_FILTER_SUBSTRINGS:
1633 adp = &f->f_sub_desc;
1635 case LDAP_FILTER_PRESENT:
1638 case LDAP_FILTER_EXT:
1639 adp = &f->f_mr_desc;
1642 /* Complex filters need not apply. */
1646 rc = slap_str2ad( type, &ad, &text );
1647 if ( rc == LDAP_SUCCESS )
1650 return ( rc == LDAP_SUCCESS ) ? 0 : -1;
1654 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1655 char ***any, char **final )
1659 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1664 * The caller shouldn't free but we can't return an
1665 * array of char *s from an array of bervals without
1666 * allocating memory, so we may as well be consistent.
1669 *type = f->f_sub_desc->ad_cname.bv_val;
1670 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1671 if ( f->f_sub_any != NULL ) {
1672 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1674 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1675 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1676 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1682 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1688 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1690 Slapi_Filter *f = NULL;
1692 if ( ftype == LDAP_FILTER_AND ||
1693 ftype == LDAP_FILTER_OR ||
1694 ftype == LDAP_FILTER_NOT )
1696 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1697 f->f_choice = ftype;
1699 f->f_list->f_next = f2;
1707 slapi_x_filter_append( int ftype,
1708 Slapi_Filter **pContainingFilter, /* NULL on first call */
1709 Slapi_Filter **pNextFilter,
1710 Slapi_Filter *filterToAppend )
1712 if ( ftype == LDAP_FILTER_AND ||
1713 ftype == LDAP_FILTER_OR ||
1714 ftype == LDAP_FILTER_NOT )
1716 if ( *pContainingFilter == NULL ) {
1717 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
1718 (*pContainingFilter)->f_choice = ftype;
1719 (*pContainingFilter)->f_list = filterToAppend;
1720 (*pContainingFilter)->f_next = NULL;
1722 if ( (*pContainingFilter)->f_choice != ftype ) {
1726 (*pNextFilter)->f_next = filterToAppend;
1728 *pNextFilter = filterToAppend;
1736 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1743 /* spec says return zero if no filter. */
1747 if ( verify_access ) {
1750 return LDAP_PARAM_ERROR;
1756 * According to acl.c it is safe to call test_filter() with
1759 rc = test_filter( op, e, f );
1761 case LDAP_COMPARE_TRUE:
1764 case LDAP_COMPARE_FALSE:
1766 case SLAPD_COMPARE_UNDEFINED:
1769 case LDAP_PROTOCOL_ERROR:
1770 /* filter type unknown: spec says return -1 */
1779 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
1781 return slapi_filter_test( NULL, e, f, 0 );
1785 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
1787 switch ( f->f_choice ) {
1788 case LDAP_FILTER_AND:
1789 case LDAP_FILTER_NOT:
1790 case LDAP_FILTER_OR: {
1794 * FIXME: altering f; should we use a temporary?
1796 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1797 rc = slapi_filter_apply( f, fn, arg, error_code );
1801 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
1807 case LDAP_FILTER_EQUALITY:
1808 case LDAP_FILTER_SUBSTRINGS:
1809 case LDAP_FILTER_GE:
1810 case LDAP_FILTER_LE:
1811 case LDAP_FILTER_PRESENT:
1812 case LDAP_FILTER_APPROX:
1813 case LDAP_FILTER_EXT:
1814 *error_code = fn( f, arg );
1817 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1820 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
1821 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
1830 struct berval **vals,
1835 if( ( vals == NULL ) || ( v == NULL ) )
1838 for ( i = 0; vals[i] != NULL; i++ ) {
1839 if ( !lutil_passwd( vals[i], v, NULL, NULL ) )
1846 #define MAX_HOSTNAME 512
1849 slapi_get_hostname( void )
1852 static int been_here = 0;
1853 static char *static_hn = NULL;
1855 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1857 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1858 if ( static_hn == NULL) {
1859 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
1860 "Cannot allocate memory for hostname\n" );
1862 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1867 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1868 slapi_log_error( SLAPI_LOG_FATAL,
1870 "can't get hostname\n" );
1871 slapi_ch_free( (void **)&static_hn );
1873 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1882 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1884 hn = ch_strdup( static_hn );
1890 * FIXME: this should go in an appropriate header ...
1892 extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist );
1901 int rc = LDAP_SUCCESS;
1904 va_start( arglist, fmt );
1905 rc = slapi_int_log_error( severity, subsystem, fmt, arglist );
1913 slapi_timer_current_time( void )
1915 static int first_time = 1;
1916 #if !defined (_WIN32)
1920 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1923 gettimeofday( &base_time, NULL );
1925 gettimeofday( &now, NULL );
1926 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1927 (now.tv_usec - base_time.tv_usec);
1928 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1934 return (slap_get_time() - starttime) * 1000000;
1941 performance_counter_present = QueryPerformanceCounter( &base_time );
1942 QueryPerformanceFrequency( &performance_freq );
1945 if ( !performance_counter_present )
1948 QueryPerformanceCounter( &now );
1949 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1957 slapi_timer_get_time( char *label )
1959 unsigned long start = slapi_timer_current_time();
1960 printf("%10ld %10d usec %s\n", start, 0, label);
1968 slapi_timer_elapsed_time(
1970 unsigned long start )
1972 unsigned long stop = slapi_timer_current_time();
1973 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
1977 slapi_free_search_results_internal( Slapi_PBlock *pb )
1979 Slapi_Entry **entries;
1980 int k = 0, nEnt = 0;
1982 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
1983 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
1984 if ( nEnt == 0 || entries == NULL ) {
1988 for ( k = 0; k < nEnt; k++ ) {
1989 slapi_entry_free( entries[k] );
1993 slapi_ch_free( (void **)&entries );
1996 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
1999 return LDAP_PARAM_ERROR;
2001 if ( pb->pb_conn == NULL )
2002 return LDAP_PARAM_ERROR;
2005 *isSSL = pb->pb_conn->c_is_tls;
2010 return LDAP_SUCCESS;
2014 * DS 5.x compatability API follow
2017 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2022 return LDAP_PARAM_ERROR;
2024 at = attr->a_desc->ad_type;
2026 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2028 if ( is_at_single_value( at ) )
2029 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2030 if ( is_at_operational( at ) )
2031 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2032 if ( is_at_obsolete( at ) )
2033 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2034 if ( is_at_collective( at ) )
2035 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2036 if ( is_at_no_user_mod( at ) )
2037 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2039 return LDAP_SUCCESS;
2042 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2044 unsigned long flags;
2046 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2048 return (flags & flag) ? 1 : 0;
2051 Slapi_Attr *slapi_attr_new( void )
2055 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2060 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2063 AttributeDescription *ad = NULL;
2065 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2078 void slapi_attr_free( Slapi_Attr **a )
2084 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2086 return attr_dup( (Slapi_Attr *)attr );
2089 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2092 struct berval *nvalp;
2094 AttributeDescription *desc = a->a_desc;
2096 if ( desc->ad_type->sat_equality &&
2097 desc->ad_type->sat_equality->smr_normalize ) {
2098 rc = (*desc->ad_type->sat_equality->smr_normalize)(
2099 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2100 desc->ad_type->sat_syntax,
2101 desc->ad_type->sat_equality,
2102 (Slapi_Value *)v, &nval, NULL );
2103 if ( rc != LDAP_SUCCESS ) {
2111 rc = value_add_one( &a->a_vals, (Slapi_Value *)v );
2112 if ( rc == 0 && nvalp != NULL ) {
2113 rc = value_add_one( &a->a_nvals, nvalp );
2115 a->a_nvals = a->a_vals;
2118 if ( nvalp != NULL ) {
2119 slapi_ch_free_string( &nval.bv_val );
2125 int slapi_attr_type2plugin( const char *type, void **pi )
2132 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2134 if ( attr == NULL ) {
2135 return LDAP_PARAM_ERROR;
2138 *type = attr->a_desc->ad_cname.bv_val;
2140 return LDAP_SUCCESS;
2143 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2145 if ( attr == NULL ) {
2146 return LDAP_PARAM_ERROR;
2148 *oidp = attr->a_desc->ad_type->sat_oid;
2150 return LDAP_SUCCESS;
2153 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2160 mr = a->a_desc->ad_type->sat_equality;
2161 rc = value_match( &ret, a->a_desc, mr,
2162 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2163 (struct berval *)v1, (void *)v2, &text );
2164 if ( rc != LDAP_SUCCESS )
2167 return ( ret == 0 ) ? 0 : -1;
2170 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2174 if ( a ->a_vals == NULL ) {
2177 rc = attr_valfind( (Attribute *)a, SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, v,
2179 return rc == 0 ? 0 : -1;
2182 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2184 AttributeDescription *a1 = NULL;
2185 AttributeDescription *a2 = NULL;
2189 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2193 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2197 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2198 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2199 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2202 case SLAPI_TYPE_CMP_EXACT:
2203 ret = ad_cmp( a1, a2 );
2205 case SLAPI_TYPE_CMP_BASE:
2206 ret = ad_base_cmp( a1, a2 );
2208 case SLAPI_TYPE_CMP_SUBTYPE:
2209 ret = is_ad_subtype( a2, a2 );
2219 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2221 return ( slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT ) == 0 );
2224 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2226 return slapi_valueset_first_value( &a->a_vals, v );
2229 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2231 return slapi_valueset_next_value( &a->a_vals, hint, v );
2234 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2236 *numValues = slapi_valueset_count( &a->a_vals );
2241 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2243 *vs = &((Slapi_Attr *)a)->a_vals;
2248 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2250 return slapi_attr_get_values( a, vals );
2253 char *slapi_attr_syntax_normalize( const char *s )
2255 AttributeDescription *ad = NULL;
2258 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2262 return ad->ad_cname.bv_val;
2265 Slapi_Value *slapi_value_new( void )
2269 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2274 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2276 return ber_dupbv( NULL, (struct berval *)bval );
2279 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2281 return slapi_value_new_berval( v );
2284 Slapi_Value *slapi_value_new_string(const char *s)
2288 bv.bv_val = (char *)s;
2289 bv.bv_len = strlen( s );
2291 return slapi_value_new_berval( &bv );
2294 Slapi_Value *slapi_value_init(Slapi_Value *val)
2302 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2304 return ber_dupbv( v, bval );
2307 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2309 v->bv_val = slapi_ch_strdup( s );
2310 v->bv_len = strlen( s );
2315 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2317 return slapi_value_new_value( v );
2320 void slapi_value_free(Slapi_Value **value)
2322 if ( value == NULL ) {
2326 if ( (*value) != NULL ) {
2327 slapi_ch_free( (void **)&(*value)->bv_val );
2328 slapi_ch_free( (void **)value );
2332 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2337 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2339 if ( value == NULL ) {
2342 if ( value->bv_val != NULL ) {
2343 slapi_ch_free( (void **)&value->bv_val );
2345 slapi_value_init_berval( value, (struct berval *)bval );
2350 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2352 if ( value == NULL ) {
2355 return slapi_value_set_berval( value, vfrom );
2358 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2360 if ( value == NULL ) {
2363 if ( value->bv_val != NULL ) {
2364 slapi_ch_free( (void **)&value->bv_val );
2366 value->bv_val = slapi_ch_malloc( len );
2367 value->bv_len = len;
2368 AC_MEMCPY( value->bv_val, val, len );
2373 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2375 if ( value == NULL ) {
2378 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2382 int slapi_value_set_int(Slapi_Value *value, int intVal)
2386 snprintf( buf, sizeof( buf ), "%d", intVal );
2388 return slapi_value_set_string( value, buf );
2391 const char *slapi_value_get_string(const Slapi_Value *value)
2393 if ( value == NULL ) return NULL;
2394 if ( value->bv_val == NULL ) return NULL;
2395 if ( !checkBVString( value ) ) return NULL;
2397 return value->bv_val;
2400 int slapi_value_get_int(const Slapi_Value *value)
2402 if ( value == NULL ) return 0;
2403 if ( value->bv_val == NULL ) return 0;
2404 if ( !checkBVString( value ) ) return 0;
2406 return (int)strtol( value->bv_val, NULL, 10 );
2409 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2411 if ( value == NULL ) return 0;
2412 if ( value->bv_val == NULL ) return 0;
2413 if ( !checkBVString( value ) ) return 0;
2415 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2418 long slapi_value_get_long(const Slapi_Value *value)
2420 if ( value == NULL ) return 0;
2421 if ( value->bv_val == NULL ) return 0;
2422 if ( !checkBVString( value ) ) return 0;
2424 return strtol( value->bv_val, NULL, 10 );
2427 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2429 if ( value == NULL ) return 0;
2430 if ( value->bv_val == NULL ) return 0;
2431 if ( !checkBVString( value ) ) return 0;
2433 return strtoul( value->bv_val, NULL, 10 );
2436 size_t slapi_value_get_length(const Slapi_Value *value)
2438 if ( value == NULL )
2441 return (size_t) value->bv_len;
2444 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2446 return slapi_attr_value_cmp( a, v1, v2 );
2449 /* A ValueSet is a container for a BerVarray. */
2450 Slapi_ValueSet *slapi_valueset_new( void )
2454 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2460 void slapi_valueset_free(Slapi_ValueSet *vs)
2465 ber_bvarray_free( vp );
2468 slapi_ch_free( (void **)&vp );
2472 void slapi_valueset_init(Slapi_ValueSet *vs)
2474 if ( vs != NULL && *vs == NULL ) {
2475 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2476 (*vs)->bv_val = NULL;
2481 void slapi_valueset_done(Slapi_ValueSet *vs)
2488 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
2490 slapi_ch_free( (void **)&vp->bv_val );
2492 /* but don't free *vs or vs */
2495 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2499 ber_dupbv( &bv, (Slapi_Value *)addval );
2500 ber_bvarray_add( vs, &bv );
2503 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2505 return slapi_valueset_next_value( vs, 0, v );
2508 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2518 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2528 int slapi_valueset_count( const Slapi_ValueSet *vs )
2541 for ( i = 0; vp[i].bv_val != NULL; i++ )
2548 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2552 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2553 slapi_valueset_add_value( vs1, vp );
2557 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
2558 struct berval *val, int access )
2561 slap_access_t slap_access;
2562 AttributeDescription *ad = NULL;
2565 rc = slap_str2ad( attr, &ad, &text );
2566 if ( rc != LDAP_SUCCESS ) {
2571 * Whilst the SLAPI access types are arranged as a bitmask, the
2572 * documentation indicates that they are to be used separately.
2574 switch ( access & SLAPI_ACL_ALL ) {
2575 case SLAPI_ACL_COMPARE:
2576 slap_access = ACL_COMPARE;
2578 case SLAPI_ACL_SEARCH:
2579 slap_access = ACL_SEARCH;
2581 case SLAPI_ACL_READ:
2582 slap_access = ACL_READ;
2584 case SLAPI_ACL_WRITE:
2585 slap_access = ACL_WRITE;
2587 case SLAPI_ACL_DELETE:
2588 slap_access = ACL_WDEL;
2591 slap_access = ACL_WADD;
2593 case SLAPI_ACL_SELF: /* not documented */
2594 case SLAPI_ACL_PROXY: /* not documented */
2596 return LDAP_INSUFFICIENT_ACCESS;
2600 assert( pb->pb_op != NULL );
2602 if ( access_allowed( pb->pb_op, e, ad, val, slap_access, NULL ) ) {
2603 return LDAP_SUCCESS;
2606 return LDAP_INSUFFICIENT_ACCESS;
2609 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
2611 int rc = LDAP_SUCCESS;
2614 if ( pb == NULL || pb->pb_op == NULL )
2615 return LDAP_PARAM_ERROR;
2617 ml = slapi_int_ldapmods2modifications( pb->pb_op, mods );
2622 if ( rc == LDAP_SUCCESS ) {
2623 rc = acl_check_modlist( pb->pb_op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2626 slap_mods_free( ml, 1 );
2632 * Synthesise an LDAPMod array from a Modifications list to pass
2635 LDAPMod **slapi_int_modifications2ldapmods( Modifications *modlist )
2638 LDAPMod **mods, *modp;
2641 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
2644 mods = (LDAPMod **)slapi_ch_malloc( (i + 1) * sizeof(LDAPMod *) );
2646 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
2647 mods[i] = (LDAPMod *)slapi_ch_malloc( sizeof(LDAPMod) );
2649 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
2650 if ( BER_BVISNULL( &ml->sml_type ) ) {
2651 /* may happen for internally generated mods */
2652 assert( ml->sml_desc != NULL );
2653 modp->mod_type = slapi_ch_strdup( ml->sml_desc->ad_cname.bv_val );
2655 modp->mod_type = slapi_ch_strdup( ml->sml_type.bv_val );
2658 if ( ml->sml_values != NULL ) {
2659 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
2661 modp->mod_bvalues = (struct berval **)slapi_ch_malloc( (j + 1) *
2662 sizeof(struct berval *) );
2663 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
2664 modp->mod_bvalues[j] = (struct berval *)slapi_ch_malloc(
2665 sizeof(struct berval) );
2666 ber_dupbv( modp->mod_bvalues[j], &ml->sml_values[j] );
2668 modp->mod_bvalues[j] = NULL;
2670 modp->mod_bvalues = NULL;
2681 * Convert a potentially modified array of LDAPMods back to a
2682 * Modification list. Unfortunately the values need to be
2683 * duplicated because slap_mods_check() will try to free them
2684 * before prettying (and we can't easily get out of calling
2685 * slap_mods_check() because we need normalized values).
2687 Modifications *slapi_int_ldapmods2modifications ( Operation *op, LDAPMod **mods )
2689 Modifications *modlist = NULL, **modtail;
2691 char textbuf[SLAP_TEXT_BUFLEN];
2694 if ( mods == NULL ) {
2700 for ( modp = mods; *modp != NULL; modp++ ) {
2702 LDAPMod *lmod = *modp;
2705 AttributeDescription *ad = NULL;
2707 if ( slap_str2ad( lmod->mod_type, &ad, &text ) != LDAP_SUCCESS ) {
2711 mod = (Modifications *) slapi_ch_malloc( sizeof(Modifications) );
2712 mod->sml_op = lmod->mod_op & ~(LDAP_MOD_BVALUES);
2714 mod->sml_type = ad->ad_cname;
2716 mod->sml_next = NULL;
2719 if ( lmod->mod_op & LDAP_MOD_BVALUES ) {
2720 if ( lmod->mod_bvalues != NULL ) {
2721 while ( lmod->mod_bvalues[i] != NULL )
2725 if ( lmod->mod_values != NULL ) {
2726 while ( lmod->mod_values[i] != NULL )
2730 mod->sml_numvals = i;
2733 mod->sml_values = NULL;
2735 mod->sml_values = (BerVarray) slapi_ch_malloc( (i + 1) * sizeof(struct berval) );
2737 /* NB: This implicitly trusts a plugin to return valid modifications. */
2738 if ( lmod->mod_op & LDAP_MOD_BVALUES ) {
2739 for ( i = 0; lmod->mod_bvalues[i] != NULL; i++ ) {
2740 ber_dupbv( &mod->sml_values[i], lmod->mod_bvalues[i] );
2743 for ( i = 0; lmod->mod_values[i] != NULL; i++ ) {
2744 mod->sml_values[i].bv_val = slapi_ch_strdup( lmod->mod_values[i] );
2745 mod->sml_values[i].bv_len = strlen( lmod->mod_values[i] );
2748 mod->sml_values[i].bv_val = NULL;
2749 mod->sml_values[i].bv_len = 0;
2751 mod->sml_nvalues = NULL;
2754 modtail = &mod->sml_next;
2757 if ( slap_mods_check( op, modlist, &text, textbuf, sizeof( textbuf ), NULL ) != LDAP_SUCCESS ) {
2758 slap_mods_free( modlist, 1 );
2766 * Sun ONE DS 5.x computed attribute support. Computed attributes
2767 * allow for dynamically generated operational attributes, a very
2768 * useful thing indeed.
2772 * For some reason Sun don't use the normal plugin mechanism
2773 * registration path to register an "evaluator" function (an
2774 * "evaluator" is responsible for adding computed attributes;
2775 * the nomenclature is somewhat confusing).
2777 * As such slapi_compute_add_evaluator() registers the
2778 * function directly.
2780 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
2782 Slapi_PBlock *pPlugin = NULL;
2784 int type = SLAPI_PLUGIN_OBJECT;
2786 pPlugin = slapi_pblock_new();
2787 if ( pPlugin == NULL ) {
2788 rc = LDAP_NO_MEMORY;
2792 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
2793 if ( rc != LDAP_SUCCESS ) {
2797 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
2798 if ( rc != LDAP_SUCCESS ) {
2802 rc = slapi_int_register_plugin( frontendDB, pPlugin );
2809 if ( rc != LDAP_SUCCESS ) {
2810 if ( pPlugin != NULL ) {
2811 slapi_pblock_destroy( pPlugin );
2820 * See notes above regarding slapi_compute_add_evaluator().
2822 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
2824 Slapi_PBlock *pPlugin = NULL;
2826 int type = SLAPI_PLUGIN_OBJECT;
2828 pPlugin = slapi_pblock_new();
2829 if ( pPlugin == NULL ) {
2830 rc = LDAP_NO_MEMORY;
2834 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
2835 if ( rc != LDAP_SUCCESS ) {
2839 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
2840 if ( rc != LDAP_SUCCESS ) {
2844 rc = slapi_int_register_plugin( frontendDB, pPlugin );
2851 if ( rc != LDAP_SUCCESS ) {
2852 if ( pPlugin != NULL ) {
2853 slapi_pblock_destroy( pPlugin );
2862 * Call compute evaluators
2864 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
2867 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
2869 rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
2870 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
2871 /* Nothing to do; front-end should ignore. */
2875 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
2877 * -1: no attribute matched requested type
2878 * 0: one attribute matched
2879 * >0: error happened
2881 rc = (*pGetPlugin)( c, type, e, outputfn );
2887 slapi_ch_free( (void **)&tmpPlugin );
2893 compute_rewrite_search_filter( Slapi_PBlock *pb )
2895 if ( pb == NULL || pb->pb_op == NULL )
2896 return LDAP_PARAM_ERROR;
2898 return slapi_int_call_plugins( pb->pb_op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
2902 * New API to provide the plugin with access to the search
2903 * pblock. Have informed Sun DS team.
2906 slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
2911 if ( c->cac_pb == NULL )
2919 Slapi_Mutex *slapi_new_mutex( void )
2923 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
2924 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
2925 slapi_ch_free( (void **)&m );
2932 void slapi_destroy_mutex( Slapi_Mutex *mutex )
2934 if ( mutex != NULL ) {
2935 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
2936 slapi_ch_free( (void **)&mutex);
2940 void slapi_lock_mutex( Slapi_Mutex *mutex )
2942 ldap_pvt_thread_mutex_lock( &mutex->mutex );
2945 int slapi_unlock_mutex( Slapi_Mutex *mutex )
2947 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
2950 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
2954 if ( mutex == NULL ) {
2958 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
2959 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
2960 slapi_ch_free( (void **)&cv );
2964 cv->mutex = mutex->mutex;
2969 void slapi_destroy_condvar( Slapi_CondVar *cvar )
2971 if ( cvar != NULL ) {
2972 ldap_pvt_thread_cond_destroy( &cvar->cond );
2973 slapi_ch_free( (void **)&cvar );
2977 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
2979 if ( cvar == NULL ) {
2983 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
2986 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
2988 if ( cvar == NULL ) {
2993 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
2996 return ldap_pvt_thread_cond_signal( &cvar->cond );
2999 int slapi_int_access_allowed( Operation *op,
3001 AttributeDescription *desc,
3003 slap_access_t access,
3004 AccessControlState *state )
3006 int rc, slap_access = 0;
3007 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3010 pb = SLAPI_OPERATION_PBLOCK( op );
3012 /* internal operation */
3018 slap_access |= SLAPI_ACL_COMPARE;
3021 slap_access |= SLAPI_ACL_SEARCH;
3024 slap_access |= SLAPI_ACL_READ;
3027 slap_access |= SLAPI_ACL_WRITE;
3030 slap_access |= SLAPI_ACL_DELETE;
3033 slap_access |= SLAPI_ACL_ADD;
3039 rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
3040 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3041 /* nothing to do; allowed access */
3045 rc = 1; /* default allow policy */
3047 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3052 rc = (*pGetPlugin)( pb, entry, desc->ad_cname.bv_val,
3053 val, slap_access, (void *)state );
3059 slapi_ch_free( (void **)&tmpPlugin );
3065 * There is no documentation for this.
3067 int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
3079 rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
3080 if ( rc != LDAP_SUCCESS ) {
3084 if ( lrdn[1] != NULL ) {
3085 return -1; /* not single valued */
3090 *type = slapi_ch_strdup( ava->la_attr.bv_val );
3091 ber_dupbv( bv, &ava->la_value );
3098 char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
3100 struct berval new_dn, parent_dn, newrdn;
3102 new_dn.bv_val = NULL;
3104 parent_dn.bv_val = (char *)dn;
3105 parent_dn.bv_len = strlen( dn );
3107 newrdn.bv_val = (char *)rdn;
3108 newrdn.bv_len = strlen( rdn );
3110 build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
3112 return new_dn.bv_val;
3115 int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
3119 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
3120 size_t textlen = sizeof textbuf;
3121 int rc = LDAP_SUCCESS;
3123 PBLOCK_ASSERT_OP( pb, 0 );
3125 be_orig = pb->pb_op->o_bd;
3127 pb->pb_op->o_bd = select_backend( &e->e_nname, 0 );
3128 if ( pb->pb_op->o_bd != NULL ) {
3129 rc = entry_schema_check( pb->pb_op, e, NULL, 0, 0, NULL,
3130 &text, textbuf, textlen );
3132 pb->pb_op->o_bd = be_orig;
3134 return ( rc == LDAP_SUCCESS ) ? 0 : 1;
3137 int slapi_entry_rdn_values_present( const Slapi_Entry *e )
3141 int i = 0, match = 0;
3143 rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
3144 if ( rc != LDAP_SUCCESS ) {
3148 if ( dn[0] != NULL ) {
3149 LDAPRDN rdn = dn[0];
3151 for ( i = 0; rdn[i] != NULL; i++ ) {
3152 LDAPAVA *ava = &rdn[0][i];
3153 Slapi_Attr *a = NULL;
3155 if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
3156 slapi_attr_value_find( a, &ava->la_value ) == 0 )
3163 return ( i == match );
3166 int slapi_entry_add_rdn_values( Slapi_Entry *e )
3171 rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
3172 if ( rc != LDAP_SUCCESS ) {
3176 if ( dn[0] != NULL ) {
3177 LDAPRDN rdn = dn[0];
3178 struct berval *vals[2];
3180 for ( i = 0; rdn[i] != NULL; i++ ) {
3181 LDAPAVA *ava = &rdn[0][i];
3182 Slapi_Attr *a = NULL;
3184 if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
3185 slapi_attr_value_find( a, &ava->la_value ) == 0 )
3188 vals[0] = &ava->la_value;
3191 slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
3197 return LDAP_SUCCESS;
3200 const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
3204 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
3205 if ( attr == NULL ) {
3209 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
3210 return slapi_value_get_string( &attr->a_vals[0] );
3216 void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
3220 attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
3222 bv.bv_val = uniqueid;
3223 bv.bv_len = strlen( uniqueid );
3224 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
3227 LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
3234 size = sizeof("ldap:///");
3238 size += strlen( ldaphost );
3239 if ( ldapport != 0 ) {
3243 url = slapi_ch_malloc( size );
3245 if ( ldapport != 0 ) {
3246 rc = snprintf( url, size, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
3248 rc = snprintf( url, size, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
3251 if ( rc > 0 && (size_t) rc < size ) {
3252 rc = ldap_initialize( &ld, url );
3257 slapi_ch_free_string( &url );
3259 return ( rc == LDAP_SUCCESS ) ? ld : NULL;
3262 void slapi_ldap_unbind( LDAP *ld )
3264 ldap_unbind_ext_s( ld, NULL, NULL );
3267 int slapi_x_backend_get_flags( const Slapi_Backend *be, unsigned long *flags )
3270 return LDAP_PARAM_ERROR;
3272 *flags = SLAP_DBFLAGS(be);
3274 return LDAP_SUCCESS;
3278 slapi_int_count_controls( LDAPControl **ctrls )
3282 if ( ctrls == NULL )
3285 for ( i = 0; ctrls[i] != NULL; i++ )
3292 slapi_op_abandoned( Slapi_PBlock *pb )
3294 if ( pb->pb_op == NULL )
3297 return ( pb->pb_op->o_abandon );
3301 slapi_op_type_to_string(unsigned long type)
3306 case SLAPI_OPERATION_BIND:
3309 case SLAPI_OPERATION_UNBIND:
3312 case SLAPI_OPERATION_SEARCH:
3315 case SLAPI_OPERATION_MODIFY:
3318 case SLAPI_OPERATION_ADD:
3321 case SLAPI_OPERATION_DELETE:
3324 case SLAPI_OPERATION_MODDN:
3327 case SLAPI_OPERATION_COMPARE:
3330 case SLAPI_OPERATION_ABANDON:
3333 case SLAPI_OPERATION_EXTENDED:
3337 str = "unknown operation type";
3344 slapi_op_get_type(Slapi_Operation * op)
3348 switch ( op->o_tag ) {
3350 type = SLAPI_OPERATION_BIND;
3352 case LDAP_REQ_UNBIND:
3353 type = SLAPI_OPERATION_UNBIND;
3355 case LDAP_REQ_SEARCH:
3356 type = SLAPI_OPERATION_SEARCH;
3358 case LDAP_REQ_MODIFY:
3359 type = SLAPI_OPERATION_MODIFY;
3362 type = SLAPI_OPERATION_ADD;
3364 case LDAP_REQ_DELETE:
3365 type = SLAPI_OPERATION_DELETE;
3367 case LDAP_REQ_MODRDN:
3368 type = SLAPI_OPERATION_MODDN;
3370 case LDAP_REQ_COMPARE:
3371 type = SLAPI_OPERATION_COMPARE;
3373 case LDAP_REQ_ABANDON:
3374 type = SLAPI_OPERATION_ABANDON;
3376 case LDAP_REQ_EXTENDED:
3377 type = SLAPI_OPERATION_EXTENDED;
3380 type = SLAPI_OPERATION_NONE;
3386 void slapi_be_set_readonly( Slapi_Backend *be, int readonly )
3392 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
3394 be->be_restrictops &= ~(SLAP_RESTRICT_OP_WRITES);
3397 int slapi_be_get_readonly( Slapi_Backend *be )
3402 return ( (be->be_restrictops & SLAP_RESTRICT_OP_WRITES) == SLAP_RESTRICT_OP_WRITES );
3405 const char *slapi_x_be_get_updatedn( Slapi_Backend *be )
3410 return be->be_update_ndn.bv_val;
3413 Slapi_Backend *slapi_be_select( const Slapi_DN *sdn )
3417 slapi_sdn_get_ndn( sdn );
3419 be = select_backend( (struct berval *)&sdn->ndn, 0 );
3426 slapi_operation_set_flag(Slapi_Operation *op, unsigned long flag)
3431 slapi_operation_clear_flag(Slapi_Operation *op, unsigned long flag)
3436 slapi_operation_is_flag_set(Slapi_Operation *op, unsigned long flag)
3441 #endif /* LDAP_SLAPI */