2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2002-2005 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>
38 * server start time (should we use a struct timeval also in slapd?
40 static struct timeval base_time;
41 ldap_pvt_thread_mutex_t slapi_hn_mutex;
42 ldap_pvt_thread_mutex_t slapi_time_mutex;
45 ldap_pvt_thread_mutex_t mutex;
48 struct slapi_condvar {
49 ldap_pvt_thread_cond_t cond;
50 ldap_pvt_thread_mutex_t mutex;
53 static int checkBVString(const struct berval *bv)
57 for ( i = 0; i < bv->bv_len; i++ ) {
58 if ( bv->bv_val[i] == '\0' )
61 if ( bv->bv_val[i] != '\0' )
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 ) {
113 Slapi_Entry *e = NULL;
116 pTmpS = slapi_ch_strdup( s );
117 if ( pTmpS != NULL ) {
118 e = str2entry( pTmpS );
119 slapi_ch_free( (void **)&pTmpS );
132 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
133 ret = entry2str( e, len );
134 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
140 slapi_entry_get_dn( Slapi_Entry *e )
142 return e->e_name.bv_val;
146 slapi_x_entry_get_id( Slapi_Entry *e )
156 struct berval dn = BER_BVNULL;
159 dn.bv_len = strlen( ldn );
161 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname, NULL );
165 slapi_entry_dup( Slapi_Entry *e )
167 return entry_dup( e );
171 slapi_entry_attr_delete(
175 AttributeDescription *ad = NULL;
178 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
179 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
182 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
183 return 0; /* attribute is deleted */
185 return -1; /* something went wrong */
190 slapi_entry_alloc( void )
192 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
196 slapi_entry_free( Slapi_Entry *e )
203 slapi_entry_attr_merge(
206 struct berval **vals )
208 AttributeDescription *ad = NULL;
213 rc = bvptr2obj( vals, &bv );
214 if ( rc != LDAP_SUCCESS ) {
218 rc = slap_str2ad( type, &ad, &text );
219 if ( rc != LDAP_SUCCESS ) {
223 rc = attr_merge_normalize_one( e, ad, bv, NULL );
230 slapi_entry_attr_find(
235 AttributeDescription *ad = NULL;
239 rc = slap_str2ad( type, &ad, &text );
240 if ( rc != LDAP_SUCCESS ) {
244 *attr = attr_find( e->e_attrs, ad );
245 if ( *attr == NULL ) {
253 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
255 AttributeDescription *ad = NULL;
260 rc = slap_str2ad( type, &ad, &text );
261 if ( rc != LDAP_SUCCESS ) {
265 attr = attr_find( e->e_attrs, ad );
266 if ( attr == NULL ) {
270 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
273 p = slapi_value_get_string( &attr->a_vals[0] );
275 return slapi_ch_strdup( (char *)p );
283 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
285 AttributeDescription *ad = NULL;
290 rc = slap_str2ad( type, &ad, &text );
291 if ( rc != LDAP_SUCCESS ) {
295 attr = attr_find( e->e_attrs, ad );
296 if ( attr == NULL ) {
300 return slapi_value_get_int( attr->a_vals );
304 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
306 AttributeDescription *ad = NULL;
311 rc = slap_str2ad( type, &ad, &text );
312 if ( rc != LDAP_SUCCESS ) {
316 attr = attr_find( e->e_attrs, ad );
317 if ( attr == NULL ) {
321 return slapi_value_get_long( attr->a_vals );
325 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
327 AttributeDescription *ad = NULL;
332 rc = slap_str2ad( type, &ad, &text );
333 if ( rc != LDAP_SUCCESS ) {
337 attr = attr_find( e->e_attrs, ad );
338 if ( attr == NULL ) {
342 return slapi_value_get_uint( attr->a_vals );
346 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
348 AttributeDescription *ad = NULL;
353 rc = slap_str2ad( type, &ad, &text );
354 if ( rc != LDAP_SUCCESS ) {
358 attr = attr_find( e->e_attrs, ad );
359 if ( attr == NULL ) {
363 return slapi_value_get_ulong( attr->a_vals );
367 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
370 AttributeDescription *ad = NULL;
375 rc = slap_str2ad( type, &ad, &text );
376 if ( rc != LDAP_SUCCESS ) {
380 attr = attr_find( e->e_attrs, ad );
381 if ( attr == NULL ) {
385 bv.bv_val = (char *)value;
386 bv.bv_len = strlen( value );
388 return ( slapi_attr_value_find( attr, &bv ) != -1 );
392 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
394 AttributeDescription *ad = NULL;
399 rc = slap_str2ad( type, &ad, &text );
400 if ( rc != LDAP_SUCCESS ) {
404 attr_delete ( &e->e_attrs, ad );
405 if ( value != NULL ) {
406 bv.bv_val = (char *)value;
407 bv.bv_len = strlen(value);
408 attr_merge_normalize_one( e, ad, &bv, NULL );
413 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
417 snprintf( buf, sizeof( buf ), "%d", l );
418 slapi_entry_attr_set_charptr( e, type, buf );
422 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
426 snprintf( buf, sizeof( buf ), "%u", l );
427 slapi_entry_attr_set_charptr( e, type, buf );
431 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
435 snprintf( buf, sizeof( buf ), "%ld", l );
436 slapi_entry_attr_set_charptr( e, type, buf );
440 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
444 snprintf( buf, sizeof( buf ), "%lu", l );
445 slapi_entry_attr_set_charptr( e, type, buf );
449 slapi_is_rootdse( const char *dn )
451 return ( dn == NULL || dn[0] == '\0' );
455 slapi_entry_has_children( const Slapi_Entry *e )
458 int hasSubordinates = 0;
460 pb = slapi_pblock_new();
461 slapi_int_connection_init_pb( pb, LDAP_REQ_SEARCH );
463 slapi_pblock_set( pb, SLAPI_TARGET_DN, slapi_entry_get_dn( (Entry *)e ) );
465 pb->pop->o_bd = select_backend( (struct berval *)&e->e_nname, 0, 0 );
466 if ( pb->pop->o_bd != NULL ) {
467 pb->pop->o_bd->be_has_subordinates( pb->pop, (Entry *)e, &hasSubordinates );
470 slapi_pblock_destroy( pb );
472 return ( hasSubordinates == LDAP_COMPARE_TRUE );
476 * Return approximate size of the entry rounded to the nearest
477 * 1K. Only the size of the attribute values are counted in the
478 * Sun implementation.
480 * http://docs.sun.com/source/816-6701-10/funcref.html#1017388
482 size_t slapi_entry_size(Slapi_Entry *e)
488 for ( size = 0, a = e->e_attrs; a != NULL; a = a->a_next ) {
489 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
490 size += a->a_vals[i].bv_len + 1;
495 size -= (size % 1024);
501 * Add values to entry.
504 * LDAP_SUCCESS Values added to entry
505 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
506 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
509 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
514 char textbuf[SLAP_TEXT_BUFLEN];
516 mod.sm_op = LDAP_MOD_ADD;
519 mod.sm_type.bv_val = (char *)type;
520 mod.sm_type.bv_len = strlen( type );
522 rc = slap_str2ad( type, &mod.sm_desc, &text );
523 if ( rc != LDAP_SUCCESS ) {
527 if ( vals == NULL ) {
528 /* Apparently vals can be NULL
529 * FIXME: sm_values = NULL ? */
530 mod.sm_values = (BerVarray)ch_malloc( sizeof(struct berval) );
531 mod.sm_values->bv_val = NULL;
534 rc = bvptr2obj( vals, &mod.sm_values );
535 if ( rc != LDAP_SUCCESS ) {
536 return LDAP_CONSTRAINT_VIOLATION;
539 mod.sm_nvalues = NULL;
541 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
543 slapi_ch_free( (void **)&mod.sm_values );
545 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
549 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
551 return slapi_entry_add_values( e, type, vals );
555 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
557 AttributeDescription *ad = NULL;
561 rc = slap_str2ad( type, &ad, &text );
562 if ( rc != LDAP_SUCCESS ) {
566 return attr_merge_normalize( e, ad, *vs, NULL );
570 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
575 char textbuf[SLAP_TEXT_BUFLEN];
577 mod.sm_op = LDAP_MOD_DELETE;
580 mod.sm_type.bv_val = (char *)type;
581 mod.sm_type.bv_len = strlen( type );
583 if ( vals == NULL ) {
584 /* If vals is NULL, this is a NOOP. */
588 rc = slap_str2ad( type, &mod.sm_desc, &text );
589 if ( rc != LDAP_SUCCESS ) {
593 if ( vals[0] == NULL ) {
594 /* SLAPI doco says LDAP_OPERATIONS_ERROR but LDAP_OTHER is better */
595 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
598 rc = bvptr2obj( vals, &mod.sm_values );
599 if ( rc != LDAP_SUCCESS ) {
600 return LDAP_CONSTRAINT_VIOLATION;
602 mod.sm_nvalues = NULL;
604 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
606 slapi_ch_free( (void **)&mod.sm_values );
612 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
614 return slapi_entry_delete_values( e, type, vals );
618 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
620 return slapi_entry_attr_merge( e, (char *)type, vals );
624 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
626 AttributeDescription *ad = NULL;
630 rc = slap_str2ad( type, &ad, &text );
631 if ( rc != LDAP_SUCCESS ) {
635 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
636 if ( rc != LDAP_SUCCESS ) {
644 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
648 val.bv_val = (char *)value;
649 val.bv_len = strlen( value );
651 return slapi_entry_add_value( e, type, &val );
655 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
657 Slapi_Value *vals[2];
660 val.bv_val = (char *)value;
661 val.bv_len = strlen( value );
665 return slapi_entry_delete_values_sv( e, type, vals );
670 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
672 return slapi_entry_attr_merge( e, (char *)type, vals );
676 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
684 return ( *attr != NULL ) ? 0 : -1;
688 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
694 if ( prevattr == NULL ) {
698 *attr = prevattr->a_next;
700 return ( *attr != NULL ) ? 0 : -1;
704 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
706 AttributeDescription *ad = NULL;
711 rc = slap_str2ad( type, &ad, &text );
712 if ( rc != LDAP_SUCCESS ) {
716 attr_delete( &e->e_attrs, ad );
718 rc = bvptr2obj( vals, &bv );
719 if ( rc != LDAP_SUCCESS ) {
723 rc = attr_merge_normalize( e, ad, bv, NULL );
724 slapi_ch_free( (void **)&bv );
725 if ( rc != LDAP_SUCCESS ) {
733 * FIXME -- The caller must free the allocated memory.
734 * In Netscape they do not have to.
737 slapi_attr_get_values(
739 struct berval ***vals )
744 if ( attr == NULL ) {
748 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
752 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
753 for ( j = 0; j < i; j++ ) {
754 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
758 *vals = (struct berval **)bv;
764 slapi_dn_normalize( char *dn )
769 assert( dn != NULL );
772 bdn.bv_len = strlen( dn );
774 if ( dnPretty( NULL, &bdn, &pdn, NULL ) != LDAP_SUCCESS ) {
782 slapi_dn_normalize_case( char *dn )
787 assert( dn != NULL );
790 bdn.bv_len = strlen( dn );
792 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
804 struct berval bdn, ndn;
805 struct berval bsuffix, nsuffix;
808 assert( dn != NULL );
809 assert( suffix != NULL );
812 bdn.bv_len = strlen( dn );
814 bsuffix.bv_val = suffix;
815 bsuffix.bv_len = strlen( suffix );
817 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
821 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
824 slapi_ch_free( (void **)&ndn.bv_val );
828 rc = dnIsSuffix( &ndn, &nsuffix );
830 slapi_ch_free( (void **)&ndn.bv_val );
831 slapi_ch_free( (void **)&nsuffix.bv_val );
838 const char *parentdn,
839 const char *childdn )
841 struct berval assertedParentDN, normalizedAssertedParentDN;
842 struct berval childDN, normalizedChildDN;
843 struct berval normalizedParentDN;
846 assert( parentdn != NULL );
847 assert( childdn != NULL );
849 assertedParentDN.bv_val = (char *)parentdn;
850 assertedParentDN.bv_len = strlen( parentdn );
852 if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
853 &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
858 childDN.bv_val = (char *)childdn;
859 childDN.bv_len = strlen( childdn );
861 if ( dnNormalize( 0, NULL, NULL, &childDN,
862 &normalizedChildDN, NULL ) != LDAP_SUCCESS )
864 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
868 dnParent( &normalizedChildDN, &normalizedParentDN );
870 if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
871 &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
876 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
877 slapi_ch_free( (void **)&normalizedChildDN.bv_val );
879 return ( match == 0 );
883 * Returns DN of the parent entry, or NULL if the DN is
884 * an empty string or NULL, or has no parent.
887 slapi_dn_parent( const char *_dn )
889 struct berval dn, prettyDN;
890 struct berval parentDN;
896 dn.bv_val = (char *)_dn;
897 dn.bv_len = strlen( _dn );
899 if ( dn.bv_len == 0 ) {
903 if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
907 dnParent( &prettyDN, &parentDN ); /* in-place */
909 slapi_ch_free( (void **)&prettyDN.bv_val );
911 if ( parentDN.bv_len == 0 ) {
915 return slapi_ch_strdup( parentDN.bv_val );
919 * Returns DN of the parent entry; or NULL if the DN is
920 * an empty string, if the DN has no parent, or if the
921 * DN is the suffix of the backend database
923 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *_dn )
926 struct berval dn, prettyDN;
927 struct berval normalizedDN, parentDN;
929 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
932 dn.bv_val = (char *)_dn;
933 dn.bv_len = strlen( _dn );
935 if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
939 if ( be != NULL && be_issuffix( be, &normalizedDN ) ) {
940 slapi_ch_free( (void **)&prettyDN.bv_val );
941 slapi_ch_free( (void **)&normalizedDN.bv_val );
945 dnParent( &prettyDN, &parentDN );
947 slapi_ch_free( (void **)&prettyDN.bv_val );
948 slapi_ch_free( (void **)&normalizedDN.bv_val );
950 if ( parentDN.bv_len == 0 ) {
954 return slapi_ch_strdup( parentDN.bv_val );
958 slapi_dn_ignore_case( char *dn )
960 return slapi_dn_normalize_case( dn );
964 slapi_ch_malloc( unsigned long size )
966 return ch_malloc( size );
970 slapi_ch_free( void **ptr )
972 if ( ptr == NULL || *ptr == NULL )
979 slapi_ch_free_string( char **ptr )
981 slapi_ch_free( (void **)ptr );
985 slapi_ch_array_free( char **arrayp )
989 if ( arrayp != NULL ) {
990 for ( p = arrayp; *p != NULL; p++ ) {
991 slapi_ch_free( (void **)p );
993 slapi_ch_free( (void **)&arrayp );
998 slapi_ch_bvdup(const struct berval *v)
1002 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
1003 bv->bv_len = v->bv_len;
1004 bv->bv_val = slapi_ch_malloc( bv->bv_len );
1005 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1011 slapi_ch_bvecdup(const struct berval **v)
1020 for ( i = 0; v[i] != NULL; i++ )
1023 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1025 for ( i = 0; v[i] != NULL; i++ ) {
1026 rv[i] = slapi_ch_bvdup( v[i] );
1035 unsigned long nelem,
1036 unsigned long size )
1038 return ch_calloc( nelem, size );
1044 unsigned long size )
1046 return ch_realloc( block, size );
1050 slapi_ch_strdup( char *s )
1052 return ch_strdup( (const char *)s );
1056 slapi_ch_stlen( char *s )
1058 return strlen( (const char *)s );
1062 slapi_control_present(
1063 LDAPControl **controls,
1065 struct berval **val,
1079 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1080 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1085 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1087 *val = &controls[i]->ldctl_value;
1092 *iscritical = controls[i]->ldctl_iscritical;
1102 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1103 unsigned long *slapi_mask)
1105 *slapi_mask = SLAPI_OPERATION_NONE;
1107 if ( slap_mask & SLAP_CTRL_ABANDON )
1108 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1110 if ( slap_mask & SLAP_CTRL_ADD )
1111 *slapi_mask |= SLAPI_OPERATION_ADD;
1113 if ( slap_mask & SLAP_CTRL_BIND )
1114 *slapi_mask |= SLAPI_OPERATION_BIND;
1116 if ( slap_mask & SLAP_CTRL_COMPARE )
1117 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1119 if ( slap_mask & SLAP_CTRL_DELETE )
1120 *slapi_mask |= SLAPI_OPERATION_DELETE;
1122 if ( slap_mask & SLAP_CTRL_MODIFY )
1123 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1125 if ( slap_mask & SLAP_CTRL_RENAME )
1126 *slapi_mask |= SLAPI_OPERATION_MODDN;
1128 if ( slap_mask & SLAP_CTRL_SEARCH )
1129 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1131 if ( slap_mask & SLAP_CTRL_UNBIND )
1132 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1136 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1137 slap_mask_t *slap_mask)
1141 if ( slapi_mask & SLAPI_OPERATION_BIND )
1142 *slap_mask |= SLAP_CTRL_BIND;
1144 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1145 *slap_mask |= SLAP_CTRL_UNBIND;
1147 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1148 *slap_mask |= SLAP_CTRL_SEARCH;
1150 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1151 *slap_mask |= SLAP_CTRL_MODIFY;
1153 if ( slapi_mask & SLAPI_OPERATION_ADD )
1154 *slap_mask |= SLAP_CTRL_ADD;
1156 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1157 *slap_mask |= SLAP_CTRL_DELETE;
1159 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1160 *slap_mask |= SLAP_CTRL_RENAME;
1162 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1163 *slap_mask |= SLAP_CTRL_COMPARE;
1165 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1166 *slap_mask |= SLAP_CTRL_ABANDON;
1168 *slap_mask |= SLAP_CTRL_GLOBAL;
1172 slapi_int_parse_control(
1177 /* Plugins must deal with controls themselves. */
1179 return LDAP_SUCCESS;
1183 slapi_register_supported_control(
1185 unsigned long controlops )
1187 slap_mask_t controlmask;
1189 slapiControlOp2SlapControlMask( controlops, &controlmask );
1191 register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL );
1195 slapi_get_supported_controls(
1197 unsigned long **ctrlopsp )
1201 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1202 if ( rc != LDAP_SUCCESS ) {
1206 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1207 /* In place, naughty. */
1208 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1211 return LDAP_SUCCESS;
1215 slapi_dup_control( LDAPControl *ctrl )
1219 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1220 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1221 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1222 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1228 slapi_register_supported_saslmechanism( char *mechanism )
1230 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1231 slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1232 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1236 slapi_get_supported_saslmechanisms( void )
1238 /* FIXME -- can not get the saslmechanism without a connection. */
1239 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1240 "can not get the SASL mechanism list "
1241 "without a connection\n" );
1246 slapi_get_supported_extended_ops( void )
1249 char **ppExtOpOID = NULL;
1252 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1256 for ( j = 0; slapi_int_get_supported_extop( j ) != NULL; j++ ) {
1261 if ( numExtOps == 0 ) {
1265 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1266 for ( k = 0; k < i; k++ ) {
1269 bv = get_supported_extop( k );
1270 assert( bv != NULL );
1272 ppExtOpOID[ k ] = bv->bv_val;
1275 for ( ; k < j; k++ ) {
1278 bv = slapi_int_get_supported_extop( k );
1279 assert( bv != NULL );
1281 ppExtOpOID[ i + k ] = bv->bv_val;
1283 ppExtOpOID[ i + k ] = NULL;
1289 slapi_send_ldap_result(
1295 struct berval **urls )
1299 assert( pb->pop != NULL );
1304 rs->sr_matched = matched;
1308 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1309 send_ldap_sasl( pb->pop, rs );
1310 } else if ( rs->sr_rspoid != NULL ) {
1311 send_ldap_extended( pb->pop, rs );
1313 if ( pb->pop->o_tag == LDAP_REQ_SEARCH )
1314 rs->sr_nentries = nentries;
1316 send_ldap_result( pb->pop, rs );
1321 slapi_send_ldap_search_entry(
1324 LDAPControl **ectrls,
1328 SlapReply rs = { REP_SEARCH };
1330 AttributeName *an = NULL;
1334 assert( pb->pop != NULL );
1336 if ( attrs != NULL ) {
1337 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1343 an = (AttributeName *) slapi_ch_malloc( (i+1) * sizeof(AttributeName) );
1344 for ( i = 0; attrs[i] != NULL; i++ ) {
1345 an[i].an_name.bv_val = attrs[i];
1346 an[i].an_name.bv_len = strlen( attrs[i] );
1347 an[i].an_desc = NULL;
1348 rs.sr_err = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text );
1349 if ( rs.sr_err != LDAP_SUCCESS) {
1350 slapi_ch_free( (void **)&an );
1354 an[i].an_name.bv_len = 0;
1355 an[i].an_name.bv_val = NULL;
1358 rs.sr_err = LDAP_SUCCESS;
1359 rs.sr_matched = NULL;
1362 rs.sr_ctrls = ectrls;
1364 rs.sr_operational_attrs = NULL;
1369 rc = send_search_entry( pb->pop, &rs );
1371 slapi_ch_free( (void **)&an );
1377 slapi_send_ldap_search_reference(
1380 struct berval **references,
1381 LDAPControl **ectrls,
1382 struct berval **v2refs
1385 SlapReply rs = { REP_SEARCHREF };
1388 rs.sr_err = LDAP_SUCCESS;
1389 rs.sr_matched = NULL;
1392 rc = bvptr2obj( references, &rs.sr_ref );
1393 if ( rc != LDAP_SUCCESS ) {
1397 rs.sr_ctrls = ectrls;
1399 rs.sr_operational_attrs = NULL;
1402 if ( v2refs != NULL ) {
1403 rc = bvptr2obj( v2refs, &rs.sr_v2ref );
1404 if ( rc != LDAP_SUCCESS ) {
1405 slapi_ch_free( (void **)&rs.sr_ref );
1412 rc = send_search_reference( pb->pop, &rs );
1414 slapi_ch_free( (void **)&rs.sr_ref );
1415 slapi_ch_free( (void **)&rs.sr_v2ref );
1421 slapi_str2filter( char *str )
1423 return str2filter( str );
1435 slapi_filter_dup( Slapi_Filter *filter )
1439 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1441 f->f_choice = filter->f_choice;
1443 switch ( f->f_choice ) {
1444 case LDAP_FILTER_AND:
1445 case LDAP_FILTER_NOT:
1446 case LDAP_FILTER_OR: {
1447 Filter *pFilter, **ppF;
1449 for ( pFilter = filter->f_list, ppF = &f->f_list;
1451 pFilter = pFilter->f_next, ppF = &f->f_next )
1453 *ppF = slapi_filter_dup( pFilter );
1459 case LDAP_FILTER_PRESENT:
1460 f->f_desc = filter->f_desc;
1462 case LDAP_FILTER_EQUALITY:
1463 case LDAP_FILTER_GE:
1464 case LDAP_FILTER_LE:
1465 case LDAP_FILTER_APPROX:
1466 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1467 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1468 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1470 case LDAP_FILTER_EXT:
1471 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1472 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1473 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1474 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1475 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1476 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1478 case LDAP_FILTER_SUBSTRINGS: {
1481 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1482 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1483 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1484 if ( filter->f_sub_any != NULL ) {
1485 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1487 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1488 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1489 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1491 f->f_sub_any[i].bv_val = NULL;
1493 f->f_sub_any = NULL;
1495 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1498 case SLAPD_FILTER_COMPUTED:
1499 f->f_result = filter->f_result;
1502 slapi_ch_free( (void **)&f );
1511 slapi_filter_get_choice( Slapi_Filter *f )
1525 slapi_filter_get_ava(
1528 struct berval **bval )
1531 int rc = LDAP_SUCCESS;
1533 assert( type != NULL );
1534 assert( bval != NULL );
1539 ftype = f->f_choice;
1540 if ( ftype == LDAP_FILTER_EQUALITY
1541 || ftype == LDAP_FILTER_GE
1542 || ftype == LDAP_FILTER_LE
1543 || ftype == LDAP_FILTER_APPROX ) {
1545 * According to the SLAPI Reference Manual these are
1548 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1549 *bval = &f->f_un.f_un_ava->aa_value;
1550 } else { /* filter type not supported */
1558 slapi_filter_list_first( Slapi_Filter *f )
1566 ftype = f->f_choice;
1567 if ( ftype == LDAP_FILTER_AND
1568 || ftype == LDAP_FILTER_OR
1569 || ftype == LDAP_FILTER_NOT ) {
1570 return (Slapi_Filter *)f->f_list;
1577 slapi_filter_list_next(
1579 Slapi_Filter *fprev )
1587 ftype = f->f_choice;
1588 if ( ftype == LDAP_FILTER_AND
1589 || ftype == LDAP_FILTER_OR
1590 || ftype == LDAP_FILTER_NOT )
1592 return fprev->f_next;
1599 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1605 switch ( f->f_choice ) {
1606 case LDAP_FILTER_GE:
1607 case LDAP_FILTER_LE:
1608 case LDAP_FILTER_EQUALITY:
1609 case LDAP_FILTER_APPROX:
1610 *type = f->f_av_desc->ad_cname.bv_val;
1612 case LDAP_FILTER_SUBSTRINGS:
1613 *type = f->f_sub_desc->ad_cname.bv_val;
1615 case LDAP_FILTER_PRESENT:
1616 *type = f->f_desc->ad_cname.bv_val;
1618 case LDAP_FILTER_EXT:
1619 *type = f->f_mr_desc->ad_cname.bv_val;
1622 /* Complex filters need not apply. */
1631 slapi_x_filter_set_attribute_type( Slapi_Filter *f, const char *type )
1633 AttributeDescription **adp, *ad = NULL;
1641 switch ( f->f_choice ) {
1642 case LDAP_FILTER_GE:
1643 case LDAP_FILTER_LE:
1644 case LDAP_FILTER_EQUALITY:
1645 case LDAP_FILTER_APPROX:
1646 adp = &f->f_av_desc;
1648 case LDAP_FILTER_SUBSTRINGS:
1649 adp = &f->f_sub_desc;
1651 case LDAP_FILTER_PRESENT:
1654 case LDAP_FILTER_EXT:
1655 adp = &f->f_mr_desc;
1658 /* Complex filters need not apply. */
1662 rc = slap_str2ad( type, &ad, &text );
1663 if ( rc == LDAP_SUCCESS )
1666 return ( rc == LDAP_SUCCESS ) ? 0 : -1;
1670 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1671 char ***any, char **final )
1675 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1680 * The caller shouldn't free but we can't return an
1681 * array of char *s from an array of bervals without
1682 * allocating memory, so we may as well be consistent.
1685 *type = f->f_sub_desc->ad_cname.bv_val;
1686 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1687 if ( f->f_sub_any != NULL ) {
1688 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1690 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1691 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1692 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1698 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1704 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1706 Slapi_Filter *f = NULL;
1708 if ( ftype == LDAP_FILTER_AND ||
1709 ftype == LDAP_FILTER_OR ||
1710 ftype == LDAP_FILTER_NOT )
1712 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1713 f->f_choice = ftype;
1715 f->f_list->f_next = f2;
1723 slapi_x_filter_append( int ftype,
1724 Slapi_Filter **pContainingFilter, /* NULL on first call */
1725 Slapi_Filter **pNextFilter,
1726 Slapi_Filter *filterToAppend )
1728 if ( ftype == LDAP_FILTER_AND ||
1729 ftype == LDAP_FILTER_OR ||
1730 ftype == LDAP_FILTER_NOT )
1732 if ( *pContainingFilter == NULL ) {
1733 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
1734 (*pContainingFilter)->f_choice = ftype;
1735 (*pContainingFilter)->f_list = filterToAppend;
1736 (*pContainingFilter)->f_next = NULL;
1738 if ( (*pContainingFilter)->f_choice != ftype ) {
1742 (*pNextFilter)->f_next = filterToAppend;
1744 *pNextFilter = filterToAppend;
1752 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1759 /* spec says return zero if no filter. */
1763 if ( verify_access ) {
1764 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
1766 return LDAP_PARAM_ERROR;
1772 * According to acl.c it is safe to call test_filter() with
1775 rc = test_filter( op, e, f );
1777 case LDAP_COMPARE_TRUE:
1780 case LDAP_COMPARE_FALSE:
1782 case SLAPD_COMPARE_UNDEFINED:
1785 case LDAP_PROTOCOL_ERROR:
1786 /* filter type unknown: spec says return -1 */
1795 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
1797 return slapi_filter_test( NULL, e, f, 0 );
1801 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
1803 switch ( f->f_choice ) {
1804 case LDAP_FILTER_AND:
1805 case LDAP_FILTER_NOT:
1806 case LDAP_FILTER_OR: {
1810 * FIXME: altering f; should we use a temporary?
1812 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1813 rc = slapi_filter_apply( f, fn, arg, error_code );
1817 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
1823 case LDAP_FILTER_EQUALITY:
1824 case LDAP_FILTER_SUBSTRINGS:
1825 case LDAP_FILTER_GE:
1826 case LDAP_FILTER_LE:
1827 case LDAP_FILTER_PRESENT:
1828 case LDAP_FILTER_APPROX:
1829 case LDAP_FILTER_EXT:
1830 *error_code = fn( f, arg );
1833 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1836 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
1837 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
1846 struct berval **vals,
1850 * FIXME: what's the point?
1855 #define MAX_HOSTNAME 512
1858 slapi_get_hostname( void )
1861 static int been_here = 0;
1862 static char *static_hn = NULL;
1864 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1866 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1867 if ( static_hn == NULL) {
1868 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
1869 "Cannot allocate memory for hostname\n" );
1871 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1876 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1877 slapi_log_error( SLAPI_LOG_FATAL,
1879 "can't get hostname\n" );
1880 slapi_ch_free( (void **)&static_hn );
1882 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1891 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1893 hn = ch_strdup( static_hn );
1899 * FIXME: this should go in an appropriate header ...
1901 extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist );
1910 int rc = LDAP_SUCCESS;
1913 va_start( arglist, fmt );
1914 rc = slapi_int_log_error( severity, subsystem, fmt, arglist );
1922 slapi_timer_current_time( void )
1924 static int first_time = 1;
1925 #if !defined (_WIN32)
1929 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1932 gettimeofday( &base_time, NULL );
1934 gettimeofday( &now, NULL );
1935 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1936 (now.tv_usec - base_time.tv_usec);
1937 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1943 return (slap_get_time() - starttime) * 1000000;
1950 performance_counter_present = QueryPerformanceCounter( &base_time );
1951 QueryPerformanceFrequency( &performance_freq );
1954 if ( !performance_counter_present )
1957 QueryPerformanceCounter( &now );
1958 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1966 slapi_timer_get_time( char *label )
1968 unsigned long start = slapi_timer_current_time();
1969 printf("%10ld %10d usec %s\n", start, 0, label);
1977 slapi_timer_elapsed_time(
1979 unsigned long start )
1981 unsigned long stop = slapi_timer_current_time();
1982 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
1986 slapi_free_search_results_internal( Slapi_PBlock *pb )
1988 Slapi_Entry **entries;
1989 int k = 0, nEnt = 0;
1991 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
1992 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
1993 if ( nEnt == 0 || entries == NULL ) {
1997 for ( k = 0; k < nEnt; k++ ) {
1998 slapi_entry_free( entries[k] );
2002 slapi_ch_free( (void **)&entries );
2005 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2007 Connection *conn = NULL;
2010 rc = slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2011 if ( rc != PBLOCK_SUCCESS || conn == NULL ) {
2016 *isSSL = conn->c_is_tls;
2021 return LDAP_SUCCESS;
2025 * DS 5.x compatability API follow
2028 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2033 return LDAP_PARAM_ERROR;
2035 at = attr->a_desc->ad_type;
2037 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2039 if ( is_at_single_value( at ) )
2040 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2041 if ( is_at_operational( at ) )
2042 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2043 if ( is_at_obsolete( at ) )
2044 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2045 if ( is_at_collective( at ) )
2046 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2047 if ( is_at_no_user_mod( at ) )
2048 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2050 return LDAP_SUCCESS;
2053 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2055 unsigned long flags;
2057 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2059 return (flags & flag) ? 1 : 0;
2062 Slapi_Attr *slapi_attr_new( void )
2066 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2071 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2074 AttributeDescription *ad = NULL;
2076 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2089 void slapi_attr_free( Slapi_Attr **a )
2095 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2097 return attr_dup( (Slapi_Attr *)attr );
2100 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2103 struct berval *nvalp;
2105 AttributeDescription *desc = a->a_desc;
2107 if ( desc->ad_type->sat_equality &&
2108 desc->ad_type->sat_equality->smr_normalize ) {
2109 rc = (*desc->ad_type->sat_equality->smr_normalize)(
2110 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2111 desc->ad_type->sat_syntax,
2112 desc->ad_type->sat_equality,
2113 (Slapi_Value *)v, &nval, NULL );
2114 if ( rc != LDAP_SUCCESS ) {
2122 rc = value_add_one( &a->a_vals, (Slapi_Value *)v );
2123 if ( rc == 0 && nvalp != NULL ) {
2124 rc = value_add_one( &a->a_nvals, nvalp );
2126 a->a_nvals = a->a_vals;
2129 if ( nvalp != NULL ) {
2130 slapi_ch_free_string( &nval.bv_val );
2136 int slapi_attr_type2plugin( const char *type, void **pi )
2143 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2145 if ( attr == NULL ) {
2146 return LDAP_PARAM_ERROR;
2149 *type = attr->a_desc->ad_cname.bv_val;
2151 return LDAP_SUCCESS;
2154 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2156 if ( attr == NULL ) {
2157 return LDAP_PARAM_ERROR;
2159 *oidp = attr->a_desc->ad_type->sat_oid;
2161 return LDAP_SUCCESS;
2164 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2171 mr = a->a_desc->ad_type->sat_equality;
2172 rc = value_match( &ret, a->a_desc, mr,
2173 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2174 (struct berval *)v1, (void *)v2, &text );
2175 if ( rc != LDAP_SUCCESS )
2178 return ( ret == 0 ) ? 0 : -1;
2181 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2190 if ( a ->a_vals == NULL ) {
2193 mr = a->a_desc->ad_type->sat_equality;
2194 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2195 rc = value_match( &ret, a->a_desc, mr,
2196 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2197 if ( rc != LDAP_SUCCESS ) {
2207 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2209 AttributeDescription *a1 = NULL;
2210 AttributeDescription *a2 = NULL;
2214 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2218 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2222 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2223 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2224 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2227 case SLAPI_TYPE_CMP_EXACT:
2228 ret = ad_cmp( a1, a2 );
2230 case SLAPI_TYPE_CMP_BASE:
2231 ret = ad_base_cmp( a1, a2 );
2233 case SLAPI_TYPE_CMP_SUBTYPE:
2234 ret = is_ad_subtype( a2, a2 );
2244 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2246 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2249 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2251 return slapi_valueset_first_value( &a->a_vals, v );
2254 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2256 return slapi_valueset_next_value( &a->a_vals, hint, v );
2259 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2261 *numValues = slapi_valueset_count( &a->a_vals );
2266 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2268 *vs = &((Slapi_Attr *)a)->a_vals;
2273 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2275 return slapi_attr_get_values( a, vals );
2278 char *slapi_attr_syntax_normalize( const char *s )
2280 AttributeDescription *ad = NULL;
2283 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2287 return ad->ad_cname.bv_val;
2290 Slapi_Value *slapi_value_new( void )
2294 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2299 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2301 return ber_dupbv( NULL, (struct berval *)bval );
2304 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2306 return slapi_value_new_berval( v );
2309 Slapi_Value *slapi_value_new_string(const char *s)
2313 bv.bv_val = (char *)s;
2314 bv.bv_len = strlen( s );
2316 return slapi_value_new_berval( &bv );
2319 Slapi_Value *slapi_value_init(Slapi_Value *val)
2327 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2329 return ber_dupbv( v, bval );
2332 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2334 v->bv_val = slapi_ch_strdup( (char *)s );
2335 v->bv_len = strlen( s );
2340 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2342 return slapi_value_new_value( v );
2345 void slapi_value_free(Slapi_Value **value)
2347 if ( value == NULL ) {
2351 if ( (*value) != NULL ) {
2352 slapi_ch_free( (void **)&(*value)->bv_val );
2353 slapi_ch_free( (void **)value );
2357 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2362 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2364 if ( value == NULL ) {
2367 if ( value->bv_val != NULL ) {
2368 slapi_ch_free( (void **)&value->bv_val );
2370 slapi_value_init_berval( value, (struct berval *)bval );
2375 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2377 if ( value == NULL ) {
2380 return slapi_value_set_berval( value, vfrom );
2383 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2385 if ( value == NULL ) {
2388 if ( value->bv_val != NULL ) {
2389 slapi_ch_free( (void **)&value->bv_val );
2391 value->bv_val = slapi_ch_malloc( len );
2392 value->bv_len = len;
2393 AC_MEMCPY( value->bv_val, val, len );
2398 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2400 if ( value == NULL ) {
2403 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2407 int slapi_value_set_int(Slapi_Value *value, int intVal)
2411 snprintf( buf, sizeof( buf ), "%d", intVal );
2413 return slapi_value_set_string( value, buf );
2416 const char *slapi_value_get_string(const Slapi_Value *value)
2418 if ( value == NULL ) return NULL;
2419 if ( value->bv_val == NULL ) return NULL;
2420 if ( !checkBVString( value ) ) return NULL;
2422 return value->bv_val;
2425 int slapi_value_get_int(const Slapi_Value *value)
2427 if ( value == NULL ) return 0;
2428 if ( value->bv_val == NULL ) return 0;
2429 if ( !checkBVString( value ) ) return 0;
2431 return (int)strtol( value->bv_val, NULL, 10 );
2434 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2436 if ( value == NULL ) return 0;
2437 if ( value->bv_val == NULL ) return 0;
2438 if ( !checkBVString( value ) ) return 0;
2440 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2443 long slapi_value_get_long(const Slapi_Value *value)
2445 if ( value == NULL ) return 0;
2446 if ( value->bv_val == NULL ) return 0;
2447 if ( !checkBVString( value ) ) return 0;
2449 return strtol( value->bv_val, NULL, 10 );
2452 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2454 if ( value == NULL ) return 0;
2455 if ( value->bv_val == NULL ) return 0;
2456 if ( !checkBVString( value ) ) return 0;
2458 return strtoul( value->bv_val, NULL, 10 );
2461 size_t slapi_value_get_length(const Slapi_Value *value)
2463 if ( value == NULL )
2466 return (size_t) value->bv_len;
2469 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2471 return slapi_attr_value_cmp( a, v1, v2 );
2474 /* A ValueSet is a container for a BerVarray. */
2475 Slapi_ValueSet *slapi_valueset_new( void )
2479 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2485 void slapi_valueset_free(Slapi_ValueSet *vs)
2490 ber_bvarray_free( vp );
2491 slapi_ch_free( (void **)&vp );
2497 void slapi_valueset_init(Slapi_ValueSet *vs)
2499 if ( vs != NULL && *vs == NULL ) {
2500 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2501 (*vs)->bv_val = NULL;
2506 void slapi_valueset_done(Slapi_ValueSet *vs)
2513 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
2515 slapi_ch_free( (void **)&vp->bv_val );
2517 /* but don't free *vs or vs */
2520 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2524 ber_dupbv( &bv, (Slapi_Value *)addval );
2525 ber_bvarray_add( vs, &bv );
2528 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2530 return slapi_valueset_next_value( vs, 0, v );
2533 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2543 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2553 int slapi_valueset_count( const Slapi_ValueSet *vs )
2563 for ( i = 0; vp[i].bv_val != NULL; i++ )
2570 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2574 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2575 slapi_valueset_add_value( vs1, vp );
2579 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
2580 struct berval *val, int access )
2583 slap_access_t slap_access;
2584 AttributeDescription *ad = NULL;
2587 rc = slap_str2ad( attr, &ad, &text );
2588 if ( rc != LDAP_SUCCESS ) {
2593 * Whilst the SLAPI access types are arranged as a bitmask, the
2594 * documentation indicates that they are to be used separately.
2596 switch ( access & SLAPI_ACL_ALL ) {
2597 case SLAPI_ACL_COMPARE:
2598 slap_access = ACL_COMPARE;
2600 case SLAPI_ACL_SEARCH:
2601 slap_access = ACL_SEARCH;
2603 case SLAPI_ACL_READ:
2604 slap_access = ACL_READ;
2606 case SLAPI_ACL_WRITE:
2607 slap_access = ACL_WRITE;
2609 case SLAPI_ACL_DELETE:
2610 slap_access = ACL_WDEL;
2613 slap_access = ACL_WADD;
2615 case SLAPI_ACL_SELF: /* not documented */
2616 case SLAPI_ACL_PROXY: /* not documented */
2618 return LDAP_INSUFFICIENT_ACCESS;
2622 assert( pb->pop != NULL );
2624 if ( access_allowed( pb->pop, e, ad, val, slap_access, NULL ) ) {
2625 return LDAP_SUCCESS;
2628 return LDAP_INSUFFICIENT_ACCESS;
2631 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
2634 int rc = LDAP_SUCCESS;
2637 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
2638 return LDAP_PARAM_ERROR;
2641 ml = slapi_int_ldapmods2modifications( mods, NULL );
2646 if ( rc == LDAP_SUCCESS ) {
2647 rc = acl_check_modlist( op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2650 /* Careful when freeing the modlist because it has pointers into the mods array. */
2651 slapi_int_mods_free( ml );
2657 * Synthesise an LDAPMod array from a Modifications list to pass
2658 * to SLAPI. This synthesis is destructive and as such the
2659 * Modifications list may not be used after calling this
2662 * This function must also be called before slap_mods_check().
2664 LDAPMod **slapi_int_modifications2ldapmods(
2665 Modifications **pmodlist,
2669 Modifications *ml, *modlist;
2670 LDAPMod **mods, *modp;
2673 modlist = *pmodlist;
2675 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
2678 mods = (LDAPMod **)slap_sl_malloc( (i + 1) * sizeof(LDAPMod *), memctx );
2680 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
2681 mods[i] = (LDAPMod *)slap_sl_malloc( sizeof(LDAPMod), memctx );
2683 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
2685 /* Take ownership of original type. */
2686 modp->mod_type = ml->sml_type.bv_val;
2687 ml->sml_type.bv_val = NULL;
2689 if ( ml->sml_values != NULL ) {
2690 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
2692 modp->mod_bvalues = (struct berval **)slap_sl_malloc( (j + 1) *
2693 sizeof(struct berval *), memctx );
2694 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
2695 /* Take ownership of original values. */
2696 modp->mod_bvalues[j] = (struct berval *)slap_sl_malloc(
2697 sizeof(struct berval), memctx );
2698 modp->mod_bvalues[j]->bv_len = ml->sml_values[j].bv_len;
2699 modp->mod_bvalues[j]->bv_val = ml->sml_values[j].bv_val;
2700 ml->sml_values[j].bv_len = 0;
2701 ml->sml_values[j].bv_val = NULL;
2703 modp->mod_bvalues[j] = NULL;
2705 modp->mod_bvalues = NULL;
2712 slap_mods_free( modlist );
2719 * Convert a potentially modified array of LDAPMods back to a
2720 * Modification list.
2722 * The returned Modification list contains pointers into the
2723 * LDAPMods array; the latter MUST be freed with
2724 * slapi_int_free_ldapmods() (see below).
2726 Modifications *slapi_int_ldapmods2modifications ( LDAPMod **mods, void *memctx )
2728 Modifications *modlist = NULL, **modtail;
2731 if ( mods == NULL ) {
2737 for( modp = mods; *modp != NULL; modp++ ) {
2741 struct berval **bvp;
2743 AttributeDescription *ad = NULL;
2745 /* Don't initialize attribute type if mods_check() is going to be called */
2746 if ( slap_str2ad( (*modp)->mod_type, &ad, &text ) != LDAP_SUCCESS )
2749 mod = (Modifications *) slap_sl_malloc( sizeof(Modifications), memctx );
2750 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
2752 mod->sml_type.bv_val = (*modp)->mod_type;
2753 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
2755 mod->sml_next = NULL;
2757 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
2758 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
2761 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
2766 mod->sml_values = NULL;
2768 mod->sml_values = (BerVarray) slap_sl_malloc( (i + 1) * sizeof(struct berval), memctx );
2770 /* NB: This implicitly trusts a plugin to return valid modifications. */
2771 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
2772 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
2773 mod->sml_values[i].bv_val = (*bvp)->bv_val;
2774 mod->sml_values[i].bv_len = (*bvp)->bv_len;
2777 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
2778 mod->sml_values[i].bv_val = *p;
2779 mod->sml_values[i].bv_len = strlen( *p );
2782 mod->sml_values[i].bv_val = NULL;
2783 mod->sml_values[i].bv_len = 0;
2785 mod->sml_nvalues = NULL;
2788 modtail = &mod->sml_next;
2795 slapi_int_mods_free( Modifications *ml )
2797 Modifications *next;
2799 for ( ; ml != NULL; ml = next ) {
2800 next = ml->sml_next;
2802 /* Don't free unnormalized values */
2803 if ( ml->sml_nvalues != NULL ) {
2804 ber_bvarray_free( ml->sml_nvalues );
2805 ml->sml_nvalues = NULL;
2807 slapi_ch_free((void **)&ml->sml_values);
2808 slapi_ch_free((void **)&ml);
2813 * This function only frees the parts of the mods array that
2814 * are not shared with the Modification list that was created
2815 * by slapi_int_ldapmods2modifications().
2819 slapi_int_free_ldapmods ( LDAPMod **mods )
2826 for ( i = 0; mods[i] != NULL; i++ ) {
2828 * Don't free values themselves; they're owned by the
2829 * Modification list. Do free the containing array.
2831 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
2832 if ( mods[i]->mod_bvalues != NULL ) {
2833 for ( j = 0; mods[i]->mod_bvalues[j] != NULL; j++ )
2834 slapi_ch_free( (void **)&mods[i]->mod_bvalues[j] );
2835 slapi_ch_free( (void **)&mods[i]->mod_bvalues );
2838 slapi_ch_free( (void **)&mods[i]->mod_values );
2840 /* Don't free type, for same reasons. */
2841 slapi_ch_free( (void **)&mods[i] );
2843 slapi_ch_free( (void **)&mods );
2847 * Sun ONE DS 5.x computed attribute support. Computed attributes
2848 * allow for dynamically generated operational attributes, a very
2849 * useful thing indeed.
2853 * For some reason Sun don't use the normal plugin mechanism
2854 * registration path to register an "evaluator" function (an
2855 * "evaluator" is responsible for adding computed attributes;
2856 * the nomenclature is somewhat confusing).
2858 * As such slapi_compute_add_evaluator() registers the
2859 * function directly.
2861 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
2863 Slapi_PBlock *pPlugin = NULL;
2866 pPlugin = slapi_pblock_new();
2867 if ( pPlugin == NULL ) {
2868 rc = LDAP_NO_MEMORY;
2872 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
2873 if ( rc != LDAP_SUCCESS ) {
2877 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
2878 if ( rc != LDAP_SUCCESS ) {
2882 rc = slapi_int_register_plugin( frontendDB, pPlugin );
2889 if ( rc != LDAP_SUCCESS ) {
2890 if ( pPlugin != NULL ) {
2891 slapi_pblock_destroy( pPlugin );
2900 * See notes above regarding slapi_compute_add_evaluator().
2902 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
2904 Slapi_PBlock *pPlugin = NULL;
2907 pPlugin = slapi_pblock_new();
2908 if ( pPlugin == NULL ) {
2909 rc = LDAP_NO_MEMORY;
2913 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
2914 if ( rc != LDAP_SUCCESS ) {
2918 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
2919 if ( rc != LDAP_SUCCESS ) {
2923 rc = slapi_int_register_plugin( frontendDB, pPlugin );
2930 if ( rc != LDAP_SUCCESS ) {
2931 if ( pPlugin != NULL ) {
2932 slapi_pblock_destroy( pPlugin );
2941 * Call compute evaluators
2943 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
2946 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
2948 rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
2949 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
2950 /* Nothing to do; front-end should ignore. */
2954 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
2956 * -1: no attribute matched requested type
2957 * 0: one attribute matched
2958 * >0: error happened
2960 rc = (*pGetPlugin)( c, type, e, outputfn );
2966 slapi_ch_free( (void **)&tmpPlugin );
2971 int compute_rewrite_search_filter(Slapi_PBlock *pb)
2976 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
2981 return slapi_int_call_plugins( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
2985 * New API to provide the plugin with access to the search
2986 * pblock. Have informed Sun DS team.
2988 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
2993 if ( c->cac_pb == NULL )
3001 Slapi_Mutex *slapi_new_mutex( void )
3005 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3006 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3007 slapi_ch_free( (void **)&m );
3014 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3016 if ( mutex != NULL ) {
3017 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3018 slapi_ch_free( (void **)&mutex);
3022 void slapi_lock_mutex( Slapi_Mutex *mutex )
3024 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3027 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3029 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3032 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3036 if ( mutex == NULL ) {
3040 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3041 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3042 slapi_ch_free( (void **)&cv );
3046 cv->mutex = mutex->mutex;
3051 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3053 if ( cvar != NULL ) {
3054 ldap_pvt_thread_cond_destroy( &cvar->cond );
3055 slapi_ch_free( (void **)&cvar );
3059 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3061 if ( cvar == NULL ) {
3065 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3068 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3070 if ( cvar == NULL ) {
3075 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3078 return ldap_pvt_thread_cond_signal( &cvar->cond );
3081 int slapi_int_access_allowed( Operation *op,
3083 AttributeDescription *desc,
3085 slap_access_t access,
3086 AccessControlState *state )
3088 int rc, slap_access = 0;
3089 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3092 pb = SLAPI_OPERATION_PBLOCK( op );
3094 /* internal operation */
3100 slap_access |= SLAPI_ACL_COMPARE;
3103 slap_access |= SLAPI_ACL_SEARCH;
3106 slap_access |= SLAPI_ACL_READ;
3109 slap_access |= SLAPI_ACL_WRITE;
3112 slap_access |= SLAPI_ACL_DELETE;
3115 slap_access |= SLAPI_ACL_ADD;
3121 rc = slapi_int_get_plugins( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
3122 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3123 /* nothing to do; allowed access */
3127 rc = 1; /* default allow policy */
3129 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3134 rc = (*pGetPlugin)( pb, entry, desc->ad_cname.bv_val,
3135 val, slap_access, (void *)state );
3141 slapi_ch_free( (void **)&tmpPlugin );
3147 * There is no documentation for this.
3149 int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
3161 rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
3162 if ( rc != LDAP_SUCCESS ) {
3166 if ( lrdn[1] != NULL ) {
3167 return -1; /* not single valued */
3172 *type = slapi_ch_strdup( ava->la_attr.bv_val );
3173 ber_dupbv( bv, &ava->la_value );
3180 char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
3182 struct berval new_dn, parent_dn, newrdn;
3184 new_dn.bv_val = NULL;
3186 parent_dn.bv_val = (char *)dn;
3187 parent_dn.bv_len = strlen( dn );
3189 newrdn.bv_val = (char *)rdn;
3190 newrdn.bv_len = strlen( rdn );
3192 build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
3194 return new_dn.bv_val;
3197 int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
3201 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
3202 size_t textlen = sizeof textbuf;
3203 int rc = LDAP_SUCCESS;
3205 be = select_backend( &e->e_nname, 0, 0 );
3207 rc = entry_schema_check( be, e, NULL, 0,
3208 &text, textbuf, textlen );
3210 return ( rc == LDAP_SUCCESS ) ? 0 : 1;
3213 int slapi_entry_rdn_values_present( const Slapi_Entry *e )
3217 int i = 0, match = 0;
3219 rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
3220 if ( rc != LDAP_SUCCESS ) {
3224 if ( dn[0] != NULL ) {
3225 LDAPRDN rdn = dn[0];
3227 for ( i = 0; rdn[i] != NULL; i++ ) {
3228 LDAPAVA *ava = &rdn[0][i];
3229 Slapi_Attr *a = NULL;
3231 if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
3232 slapi_attr_value_find( a, &ava->la_value ) == 0 )
3239 return ( i == match );
3242 int slapi_entry_add_rdn_values( Slapi_Entry *e )
3247 rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
3248 if ( rc != LDAP_SUCCESS ) {
3252 if ( dn[0] != NULL ) {
3253 LDAPRDN rdn = dn[0];
3254 struct berval *vals[2];
3256 for ( i = 0; rdn[i] != NULL; i++ ) {
3257 LDAPAVA *ava = &rdn[0][i];
3258 Slapi_Attr *a = NULL;
3260 if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
3261 slapi_attr_value_find( a, &ava->la_value ) == 0 )
3264 vals[0] = &ava->la_value;
3267 slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
3273 return LDAP_SUCCESS;
3276 const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
3280 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
3281 if ( attr == NULL ) {
3285 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
3286 return slapi_value_get_string( &attr->a_vals[0] );
3292 void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
3296 attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
3298 bv.bv_val = uniqueid;
3299 bv.bv_len = strlen( uniqueid );
3300 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
3303 LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
3310 size = sizeof("ldap:///");
3313 size += strlen( ldaphost );
3314 if ( ldapport != 0 )
3317 url = slapi_ch_malloc( size );
3319 if ( ldapport != 0 ) {
3320 sprintf( url, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
3322 sprintf( url, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
3325 rc = ldap_initialize( &ld, url );
3327 slapi_ch_free_string( &url );
3329 return ( rc == LDAP_SUCCESS ) ? ld : NULL;
3332 void slapi_ldap_unbind( LDAP *ld )
3337 int slapi_x_backend_get_flags( const Slapi_Backend *be, unsigned long *flags )
3340 return LDAP_PARAM_ERROR;
3342 *flags = SLAP_DBFLAGS(be);
3344 return LDAP_SUCCESS;
3348 slapi_int_count_controls( LDAPControl **ctrls )
3352 if ( ctrls == NULL )
3355 for ( i = 0; ctrls[i] != NULL; i++ )
3361 #endif /* LDAP_SLAPI */