2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2002-2006 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 return str2entry( s );
124 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
125 s = entry2str( e, len );
127 ret = slapi_ch_strdup( s );
128 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
134 slapi_entry_get_dn( Slapi_Entry *e )
136 return e->e_name.bv_val;
140 slapi_x_entry_get_id( Slapi_Entry *e )
146 slapi_int_dn_pretty( struct berval *in, struct berval *out )
148 Syntax *syntax = slap_schema.si_syn_distinguishedName;
150 assert( syntax != NULL );
152 return (syntax->ssyn_pretty)( syntax, in, out, NULL );
156 slapi_int_dn_normalize( struct berval *in, struct berval *out )
158 MatchingRule *mr = slap_schema.si_mr_distinguishedNameMatch;
159 Syntax *syntax = slap_schema.si_syn_distinguishedName;
161 assert( mr != NULL );
163 return (mr->smr_normalize)( 0, syntax, mr, in, out, NULL );
171 struct berval dn = BER_BVNULL;
174 dn.bv_len = strlen( ldn );
176 slapi_int_dn_pretty( &dn, &e->e_name );
177 slapi_int_dn_normalize( &dn, &e->e_nname );
181 slapi_entry_dup( Slapi_Entry *e )
183 return entry_dup( e );
187 slapi_entry_attr_delete(
191 AttributeDescription *ad = NULL;
194 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
195 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
198 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
199 return 0; /* attribute is deleted */
201 return -1; /* something went wrong */
206 slapi_entry_alloc( void )
208 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
212 slapi_entry_free( Slapi_Entry *e )
219 slapi_entry_attr_merge(
222 struct berval **vals )
224 AttributeDescription *ad = NULL;
229 rc = slap_str2ad( type, &ad, &text );
230 if ( rc != LDAP_SUCCESS ) {
234 rc = bvptr2obj( vals, &bv );
235 if ( rc != LDAP_SUCCESS ) {
239 rc = attr_merge_normalize_one( e, ad, bv, NULL );
246 slapi_entry_attr_find(
251 AttributeDescription *ad = NULL;
255 rc = slap_str2ad( type, &ad, &text );
256 if ( rc != LDAP_SUCCESS ) {
260 *attr = attr_find( e->e_attrs, ad );
261 if ( *attr == NULL ) {
269 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
271 AttributeDescription *ad = NULL;
276 rc = slap_str2ad( type, &ad, &text );
277 if ( rc != LDAP_SUCCESS ) {
281 attr = attr_find( e->e_attrs, ad );
282 if ( attr == NULL ) {
286 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
289 p = slapi_value_get_string( &attr->a_vals[0] );
291 return slapi_ch_strdup( p );
299 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
301 AttributeDescription *ad = NULL;
306 rc = slap_str2ad( type, &ad, &text );
307 if ( rc != LDAP_SUCCESS ) {
311 attr = attr_find( e->e_attrs, ad );
312 if ( attr == NULL ) {
316 return slapi_value_get_int( attr->a_vals );
320 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
322 AttributeDescription *ad = NULL;
327 rc = slap_str2ad( type, &ad, &text );
328 if ( rc != LDAP_SUCCESS ) {
332 attr = attr_find( e->e_attrs, ad );
333 if ( attr == NULL ) {
337 return slapi_value_get_long( attr->a_vals );
341 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
343 AttributeDescription *ad = NULL;
348 rc = slap_str2ad( type, &ad, &text );
349 if ( rc != LDAP_SUCCESS ) {
353 attr = attr_find( e->e_attrs, ad );
354 if ( attr == NULL ) {
358 return slapi_value_get_uint( attr->a_vals );
362 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
364 AttributeDescription *ad = NULL;
369 rc = slap_str2ad( type, &ad, &text );
370 if ( rc != LDAP_SUCCESS ) {
374 attr = attr_find( e->e_attrs, ad );
375 if ( attr == NULL ) {
379 return slapi_value_get_ulong( attr->a_vals );
383 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
386 AttributeDescription *ad = NULL;
391 rc = slap_str2ad( type, &ad, &text );
392 if ( rc != LDAP_SUCCESS ) {
396 attr = attr_find( e->e_attrs, ad );
397 if ( attr == NULL ) {
401 bv.bv_val = (char *)value;
402 bv.bv_len = strlen( value );
404 return ( slapi_attr_value_find( attr, &bv ) != -1 );
408 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
410 AttributeDescription *ad = NULL;
415 rc = slap_str2ad( type, &ad, &text );
416 if ( rc != LDAP_SUCCESS ) {
420 attr_delete ( &e->e_attrs, ad );
421 if ( value != NULL ) {
422 bv.bv_val = (char *)value;
423 bv.bv_len = strlen(value);
424 attr_merge_normalize_one( e, ad, &bv, NULL );
429 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
433 snprintf( buf, sizeof( buf ), "%d", l );
434 slapi_entry_attr_set_charptr( e, type, buf );
438 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
442 snprintf( buf, sizeof( buf ), "%u", l );
443 slapi_entry_attr_set_charptr( e, type, buf );
447 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
451 snprintf( buf, sizeof( buf ), "%ld", l );
452 slapi_entry_attr_set_charptr( e, type, buf );
456 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
460 snprintf( buf, sizeof( buf ), "%lu", l );
461 slapi_entry_attr_set_charptr( e, type, buf );
465 slapi_is_rootdse( const char *dn )
467 return ( dn == NULL || dn[0] == '\0' );
471 slapi_entry_has_children( const Slapi_Entry *e )
474 int hasSubordinates = 0;
476 pb = slapi_pblock_new();
477 slapi_int_connection_init_pb( pb, LDAP_REQ_SEARCH );
479 slapi_pblock_set( pb, SLAPI_TARGET_DN, slapi_entry_get_dn( (Entry *)e ) );
481 pb->pb_op->o_bd = select_backend( (struct berval *)&e->e_nname, 0, 0 );
482 if ( pb->pb_op->o_bd != NULL ) {
483 pb->pb_op->o_bd->be_has_subordinates( pb->pb_op, (Entry *)e, &hasSubordinates );
486 slapi_pblock_destroy( pb );
488 return ( hasSubordinates == LDAP_COMPARE_TRUE );
492 * Return approximate size of the entry rounded to the nearest
493 * 1K. Only the size of the attribute values are counted in the
494 * Sun implementation.
496 * http://docs.sun.com/source/816-6701-10/funcref.html#1017388
498 size_t slapi_entry_size(Slapi_Entry *e)
504 for ( size = 0, a = e->e_attrs; a != NULL; a = a->a_next ) {
505 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
506 size += a->a_vals[i].bv_len + 1;
511 size -= (size % 1024);
517 * Add values to entry.
520 * LDAP_SUCCESS Values added to entry
521 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
522 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
525 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
530 char textbuf[SLAP_TEXT_BUFLEN];
532 mod.sm_op = LDAP_MOD_ADD;
535 mod.sm_type.bv_val = (char *)type;
536 mod.sm_type.bv_len = strlen( type );
538 rc = slap_str2ad( type, &mod.sm_desc, &text );
539 if ( rc != LDAP_SUCCESS ) {
543 if ( vals == NULL ) {
544 /* Apparently vals can be NULL
545 * FIXME: sm_values = NULL ? */
546 mod.sm_values = (BerVarray)ch_malloc( sizeof(struct berval) );
547 mod.sm_values->bv_val = NULL;
550 rc = bvptr2obj( vals, &mod.sm_values );
551 if ( rc != LDAP_SUCCESS ) {
552 return LDAP_CONSTRAINT_VIOLATION;
555 mod.sm_nvalues = NULL;
557 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
559 slapi_ch_free( (void **)&mod.sm_values );
561 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
565 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
567 return slapi_entry_add_values( e, type, vals );
571 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
573 AttributeDescription *ad = NULL;
577 rc = slap_str2ad( type, &ad, &text );
578 if ( rc != LDAP_SUCCESS ) {
582 return attr_merge_normalize( e, ad, *vs, NULL );
586 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
591 char textbuf[SLAP_TEXT_BUFLEN];
593 mod.sm_op = LDAP_MOD_DELETE;
596 mod.sm_type.bv_val = (char *)type;
597 mod.sm_type.bv_len = strlen( type );
599 if ( vals == NULL ) {
600 /* If vals is NULL, this is a NOOP. */
604 rc = slap_str2ad( type, &mod.sm_desc, &text );
605 if ( rc != LDAP_SUCCESS ) {
609 if ( vals[0] == NULL ) {
610 /* SLAPI doco says LDApb_opERATIONS_ERROR but LDAP_OTHER is better */
611 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
614 rc = bvptr2obj( vals, &mod.sm_values );
615 if ( rc != LDAP_SUCCESS ) {
616 return LDAP_CONSTRAINT_VIOLATION;
618 mod.sm_nvalues = NULL;
620 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
622 slapi_ch_free( (void **)&mod.sm_values );
628 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
630 return slapi_entry_delete_values( e, type, vals );
634 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
636 return slapi_entry_attr_merge( e, (char *)type, vals );
640 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
642 AttributeDescription *ad = NULL;
646 rc = slap_str2ad( type, &ad, &text );
647 if ( rc != LDAP_SUCCESS ) {
651 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
652 if ( rc != LDAP_SUCCESS ) {
660 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
664 val.bv_val = (char *)value;
665 val.bv_len = strlen( value );
667 return slapi_entry_add_value( e, type, &val );
671 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
673 Slapi_Value *vals[2];
676 val.bv_val = (char *)value;
677 val.bv_len = strlen( value );
681 return slapi_entry_delete_values_sv( e, type, vals );
685 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
687 return slapi_entry_attr_merge( e, (char *)type, vals );
691 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
699 return ( *attr != NULL ) ? 0 : -1;
703 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
709 if ( prevattr == NULL ) {
713 *attr = prevattr->a_next;
715 return ( *attr != NULL ) ? 0 : -1;
719 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
721 AttributeDescription *ad = NULL;
726 rc = slap_str2ad( type, &ad, &text );
727 if ( rc != LDAP_SUCCESS ) {
731 attr_delete( &e->e_attrs, ad );
733 rc = bvptr2obj( vals, &bv );
734 if ( rc != LDAP_SUCCESS ) {
738 rc = attr_merge_normalize( e, ad, bv, NULL );
739 slapi_ch_free( (void **)&bv );
740 if ( rc != LDAP_SUCCESS ) {
748 * FIXME -- The caller must free the allocated memory.
749 * In Netscape they do not have to.
752 slapi_attr_get_values(
754 struct berval ***vals )
759 if ( attr == NULL ) {
763 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
767 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
768 for ( j = 0; j < i; j++ ) {
769 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
773 *vals = (struct berval **)bv;
779 slapi_dn_normalize( char *dn )
784 assert( dn != NULL );
787 bdn.bv_len = strlen( dn );
789 if ( slapi_int_dn_pretty( &bdn, &pdn ) != LDAP_SUCCESS ) {
797 slapi_dn_normalize_case( char *dn )
802 assert( dn != NULL );
805 bdn.bv_len = strlen( dn );
807 if ( slapi_int_dn_normalize( &bdn, &ndn ) != LDAP_SUCCESS ) {
819 struct berval bdn, ndn;
820 struct berval bsuffix, nsuffix;
823 assert( dn != NULL );
824 assert( suffix != NULL );
827 bdn.bv_len = strlen( dn );
829 bsuffix.bv_val = suffix;
830 bsuffix.bv_len = strlen( suffix );
832 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
836 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
839 slapi_ch_free( (void **)&ndn.bv_val );
843 rc = dnIsSuffix( &ndn, &nsuffix );
845 slapi_ch_free( (void **)&ndn.bv_val );
846 slapi_ch_free( (void **)&nsuffix.bv_val );
853 const char *parentdn,
854 const char *childdn )
856 struct berval assertedParentDN, normalizedAssertedParentDN;
857 struct berval childDN, normalizedChildDN;
858 struct berval normalizedParentDN;
861 assert( parentdn != NULL );
862 assert( childdn != NULL );
864 assertedParentDN.bv_val = (char *)parentdn;
865 assertedParentDN.bv_len = strlen( parentdn );
867 if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
868 &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
873 childDN.bv_val = (char *)childdn;
874 childDN.bv_len = strlen( childdn );
876 if ( dnNormalize( 0, NULL, NULL, &childDN,
877 &normalizedChildDN, NULL ) != LDAP_SUCCESS )
879 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
883 dnParent( &normalizedChildDN, &normalizedParentDN );
885 if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
886 &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
891 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
892 slapi_ch_free( (void **)&normalizedChildDN.bv_val );
894 return ( match == 0 );
898 * Returns DN of the parent entry, or NULL if the DN is
899 * an empty string or NULL, or has no parent.
902 slapi_dn_parent( const char *_dn )
904 struct berval dn, prettyDN;
905 struct berval parentDN;
912 dn.bv_val = (char *)_dn;
913 dn.bv_len = strlen( _dn );
915 if ( dn.bv_len == 0 ) {
919 if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
923 dnParent( &prettyDN, &parentDN ); /* in-place */
925 if ( parentDN.bv_len == 0 ) {
926 slapi_ch_free_string( &prettyDN.bv_val );
930 ret = slapi_ch_strdup( parentDN.bv_val );
931 slapi_ch_free_string( &prettyDN.bv_val );
936 int slapi_dn_isbesuffix( Slapi_PBlock *pb, char *ldn )
941 if ( slapi_is_rootdse( ldn ) ) {
945 /* according to spec should already be normalized */
946 ndn.bv_len = strlen( ldn );
949 be = select_backend( &pb->pb_op->o_req_ndn, 0, 0 );
954 return be_issuffix( be, &ndn );
958 * Returns DN of the parent entry; or NULL if the DN is
959 * an empty string, if the DN has no parent, or if the
960 * DN is the suffix of the backend database
962 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *ldn )
965 struct berval dn, prettyDN;
966 struct berval normalizedDN, parentDN;
973 PBLOCK_ASSERT_OP( pb, 0 );
975 if ( slapi_is_rootdse( ldn ) ) {
979 dn.bv_val = (char *)ldn;
980 dn.bv_len = strlen( ldn );
982 if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
986 be = select_backend( &pb->pb_op->o_req_ndn, 0, 0 );
988 if ( be == NULL || be_issuffix( be, &normalizedDN ) == 0 ) {
989 dnParent( &prettyDN, &parentDN );
991 if ( parentDN.bv_len != 0 )
992 parent = slapi_ch_strdup( parentDN.bv_val );
995 slapi_ch_free_string( &prettyDN.bv_val );
996 slapi_ch_free_string( &normalizedDN.bv_val );
1002 slapi_dn_ignore_case( char *dn )
1004 return slapi_dn_normalize_case( dn );
1008 slapi_ch_malloc( unsigned long size )
1010 return ch_malloc( size );
1014 slapi_ch_free( void **ptr )
1016 if ( ptr == NULL || *ptr == NULL )
1023 slapi_ch_free_string( char **ptr )
1025 slapi_ch_free( (void **)ptr );
1029 slapi_ch_array_free( char **arrayp )
1033 if ( arrayp != NULL ) {
1034 for ( p = arrayp; *p != NULL; p++ ) {
1035 slapi_ch_free( (void **)p );
1037 slapi_ch_free( (void **)&arrayp );
1042 slapi_ch_bvdup(const struct berval *v)
1046 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
1047 bv->bv_len = v->bv_len;
1048 bv->bv_val = slapi_ch_malloc( bv->bv_len );
1049 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1055 slapi_ch_bvecdup(const struct berval **v)
1064 for ( i = 0; v[i] != NULL; i++ )
1067 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1069 for ( i = 0; v[i] != NULL; i++ ) {
1070 rv[i] = slapi_ch_bvdup( v[i] );
1079 unsigned long nelem,
1080 unsigned long size )
1082 return ch_calloc( nelem, size );
1088 unsigned long size )
1090 return ch_realloc( block, size );
1094 slapi_ch_strdup( const char *s )
1096 return ch_strdup( s );
1100 slapi_ch_stlen( const char *s )
1106 slapi_control_present(
1107 LDAPControl **controls,
1109 struct berval **val,
1123 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1124 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1129 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1131 *val = &controls[i]->ldctl_value;
1136 *iscritical = controls[i]->ldctl_iscritical;
1146 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1147 unsigned long *slapi_mask)
1149 *slapi_mask = SLAPI_OPERATION_NONE;
1151 if ( slap_mask & SLAP_CTRL_ABANDON )
1152 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1154 if ( slap_mask & SLAP_CTRL_ADD )
1155 *slapi_mask |= SLAPI_OPERATION_ADD;
1157 if ( slap_mask & SLAP_CTRL_BIND )
1158 *slapi_mask |= SLAPI_OPERATION_BIND;
1160 if ( slap_mask & SLAP_CTRL_COMPARE )
1161 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1163 if ( slap_mask & SLAP_CTRL_DELETE )
1164 *slapi_mask |= SLAPI_OPERATION_DELETE;
1166 if ( slap_mask & SLAP_CTRL_MODIFY )
1167 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1169 if ( slap_mask & SLAP_CTRL_RENAME )
1170 *slapi_mask |= SLAPI_OPERATION_MODDN;
1172 if ( slap_mask & SLAP_CTRL_SEARCH )
1173 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1175 if ( slap_mask & SLAP_CTRL_UNBIND )
1176 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1180 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1181 slap_mask_t *slap_mask)
1185 if ( slapi_mask & SLAPI_OPERATION_BIND )
1186 *slap_mask |= SLAP_CTRL_BIND;
1188 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1189 *slap_mask |= SLAP_CTRL_UNBIND;
1191 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1192 *slap_mask |= SLAP_CTRL_SEARCH;
1194 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1195 *slap_mask |= SLAP_CTRL_MODIFY;
1197 if ( slapi_mask & SLAPI_OPERATION_ADD )
1198 *slap_mask |= SLAP_CTRL_ADD;
1200 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1201 *slap_mask |= SLAP_CTRL_DELETE;
1203 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1204 *slap_mask |= SLAP_CTRL_RENAME;
1206 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1207 *slap_mask |= SLAP_CTRL_COMPARE;
1209 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1210 *slap_mask |= SLAP_CTRL_ABANDON;
1212 *slap_mask |= SLAP_CTRL_GLOBAL;
1216 slapi_int_parse_control(
1221 /* Plugins must deal with controls themselves. */
1223 return LDAP_SUCCESS;
1227 slapi_register_supported_control(
1229 unsigned long controlops )
1231 slap_mask_t controlmask;
1233 slapiControlOp2SlapControlMask( controlops, &controlmask );
1235 register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL );
1239 slapi_get_supported_controls(
1241 unsigned long **ctrlopsp )
1245 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1246 if ( rc != LDAP_SUCCESS ) {
1250 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1251 /* In place, naughty. */
1252 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1255 return LDAP_SUCCESS;
1259 slapi_dup_control( LDAPControl *ctrl )
1263 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1264 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1265 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1266 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1272 slapi_register_supported_saslmechanism( char *mechanism )
1274 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1275 slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1276 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1280 slapi_get_supported_saslmechanisms( void )
1282 /* FIXME -- can not get the saslmechanism without a connection. */
1283 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1284 "can not get the SASL mechanism list "
1285 "without a connection\n" );
1290 slapi_get_supported_extended_ops( void )
1293 char **ppExtOpOID = NULL;
1296 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1300 for ( j = 0; slapi_int_get_supported_extop( j ) != NULL; j++ ) {
1305 if ( numExtOps == 0 ) {
1309 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1310 for ( k = 0; k < i; k++ ) {
1313 bv = get_supported_extop( k );
1314 assert( bv != NULL );
1316 ppExtOpOID[ k ] = bv->bv_val;
1319 for ( ; k < j; k++ ) {
1322 bv = slapi_int_get_supported_extop( k );
1323 assert( bv != NULL );
1325 ppExtOpOID[ i + k ] = bv->bv_val;
1327 ppExtOpOID[ i + k ] = NULL;
1333 slapi_send_ldap_result(
1339 struct berval **urls )
1343 PBLOCK_ASSERT_OP( pb, 0 );
1348 rs->sr_matched = matched;
1352 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1353 send_ldap_sasl( pb->pb_op, rs );
1354 } else if ( rs->sr_rspoid != NULL ) {
1355 send_ldap_extended( pb->pb_op, rs );
1357 if ( pb->pb_op->o_tag == LDAP_REQ_SEARCH )
1358 rs->sr_nentries = nentries;
1360 send_ldap_result( pb->pb_op, rs );
1365 slapi_send_ldap_search_entry(
1368 LDAPControl **ectrls,
1372 SlapReply rs = { REP_SEARCH };
1374 AttributeName *an = NULL;
1378 assert( pb->pb_op != NULL );
1380 if ( attrs != NULL ) {
1381 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1387 an = (AttributeName *) slapi_ch_malloc( (i+1) * sizeof(AttributeName) );
1388 for ( i = 0; attrs[i] != NULL; i++ ) {
1389 an[i].an_name.bv_val = attrs[i];
1390 an[i].an_name.bv_len = strlen( attrs[i] );
1391 an[i].an_desc = NULL;
1392 rs.sr_err = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text );
1393 if ( rs.sr_err != LDAP_SUCCESS) {
1394 slapi_ch_free( (void **)&an );
1398 an[i].an_name.bv_len = 0;
1399 an[i].an_name.bv_val = NULL;
1402 rs.sr_err = LDAP_SUCCESS;
1403 rs.sr_matched = NULL;
1406 rs.sr_ctrls = ectrls;
1408 rs.sr_operational_attrs = NULL;
1413 rc = send_search_entry( pb->pb_op, &rs );
1415 slapi_ch_free( (void **)&an );
1421 slapi_send_ldap_search_reference(
1424 struct berval **references,
1425 LDAPControl **ectrls,
1426 struct berval **v2refs
1429 SlapReply rs = { REP_SEARCHREF };
1432 rs.sr_err = LDAP_SUCCESS;
1433 rs.sr_matched = NULL;
1436 rc = bvptr2obj( references, &rs.sr_ref );
1437 if ( rc != LDAP_SUCCESS ) {
1441 rs.sr_ctrls = ectrls;
1443 rs.sr_operational_attrs = NULL;
1446 if ( v2refs != NULL ) {
1447 rc = bvptr2obj( v2refs, &rs.sr_v2ref );
1448 if ( rc != LDAP_SUCCESS ) {
1449 slapi_ch_free( (void **)&rs.sr_ref );
1456 rc = send_search_reference( pb->pb_op, &rs );
1458 slapi_ch_free( (void **)&rs.sr_ref );
1459 slapi_ch_free( (void **)&rs.sr_v2ref );
1465 slapi_str2filter( char *str )
1467 return str2filter( str );
1479 slapi_filter_dup( Slapi_Filter *filter )
1483 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1485 f->f_choice = filter->f_choice;
1487 switch ( f->f_choice ) {
1488 case LDAP_FILTER_AND:
1489 case LDAP_FILTER_NOT:
1490 case LDAP_FILTER_OR: {
1491 Filter *pFilter, **ppF;
1493 for ( pFilter = filter->f_list, ppF = &f->f_list;
1495 pFilter = pFilter->f_next, ppF = &f->f_next )
1497 *ppF = slapi_filter_dup( pFilter );
1503 case LDAP_FILTER_PRESENT:
1504 f->f_desc = filter->f_desc;
1506 case LDAP_FILTER_EQUALITY:
1507 case LDAP_FILTER_GE:
1508 case LDAP_FILTER_LE:
1509 case LDAP_FILTER_APPROX:
1510 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1511 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1512 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1514 case LDAP_FILTER_EXT:
1515 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1516 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1517 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1518 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1519 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1520 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1522 case LDAP_FILTER_SUBSTRINGS: {
1525 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1526 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1527 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1528 if ( filter->f_sub_any != NULL ) {
1529 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1531 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1532 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1533 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1535 f->f_sub_any[i].bv_val = NULL;
1537 f->f_sub_any = NULL;
1539 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1542 case SLAPD_FILTER_COMPUTED:
1543 f->f_result = filter->f_result;
1546 slapi_ch_free( (void **)&f );
1555 slapi_filter_get_choice( Slapi_Filter *f )
1569 slapi_filter_get_ava(
1572 struct berval **bval )
1575 int rc = LDAP_SUCCESS;
1577 assert( type != NULL );
1578 assert( bval != NULL );
1583 ftype = f->f_choice;
1584 if ( ftype == LDAP_FILTER_EQUALITY
1585 || ftype == LDAP_FILTER_GE
1586 || ftype == LDAP_FILTER_LE
1587 || ftype == LDAP_FILTER_APPROX ) {
1589 * According to the SLAPI Reference Manual these are
1592 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1593 *bval = &f->f_un.f_un_ava->aa_value;
1594 } else { /* filter type not supported */
1602 slapi_filter_list_first( Slapi_Filter *f )
1610 ftype = f->f_choice;
1611 if ( ftype == LDAP_FILTER_AND
1612 || ftype == LDAP_FILTER_OR
1613 || ftype == LDAP_FILTER_NOT ) {
1614 return (Slapi_Filter *)f->f_list;
1621 slapi_filter_list_next(
1623 Slapi_Filter *fprev )
1631 ftype = f->f_choice;
1632 if ( ftype == LDAP_FILTER_AND
1633 || ftype == LDAP_FILTER_OR
1634 || ftype == LDAP_FILTER_NOT )
1636 return fprev->f_next;
1643 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1649 switch ( f->f_choice ) {
1650 case LDAP_FILTER_GE:
1651 case LDAP_FILTER_LE:
1652 case LDAP_FILTER_EQUALITY:
1653 case LDAP_FILTER_APPROX:
1654 *type = f->f_av_desc->ad_cname.bv_val;
1656 case LDAP_FILTER_SUBSTRINGS:
1657 *type = f->f_sub_desc->ad_cname.bv_val;
1659 case LDAP_FILTER_PRESENT:
1660 *type = f->f_desc->ad_cname.bv_val;
1662 case LDAP_FILTER_EXT:
1663 *type = f->f_mr_desc->ad_cname.bv_val;
1666 /* Complex filters need not apply. */
1675 slapi_x_filter_set_attribute_type( Slapi_Filter *f, const char *type )
1677 AttributeDescription **adp, *ad = NULL;
1685 switch ( f->f_choice ) {
1686 case LDAP_FILTER_GE:
1687 case LDAP_FILTER_LE:
1688 case LDAP_FILTER_EQUALITY:
1689 case LDAP_FILTER_APPROX:
1690 adp = &f->f_av_desc;
1692 case LDAP_FILTER_SUBSTRINGS:
1693 adp = &f->f_sub_desc;
1695 case LDAP_FILTER_PRESENT:
1698 case LDAP_FILTER_EXT:
1699 adp = &f->f_mr_desc;
1702 /* Complex filters need not apply. */
1706 rc = slap_str2ad( type, &ad, &text );
1707 if ( rc == LDAP_SUCCESS )
1710 return ( rc == LDAP_SUCCESS ) ? 0 : -1;
1714 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1715 char ***any, char **final )
1719 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1724 * The caller shouldn't free but we can't return an
1725 * array of char *s from an array of bervals without
1726 * allocating memory, so we may as well be consistent.
1729 *type = f->f_sub_desc->ad_cname.bv_val;
1730 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1731 if ( f->f_sub_any != NULL ) {
1732 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1734 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1735 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1736 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1742 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1748 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1750 Slapi_Filter *f = NULL;
1752 if ( ftype == LDAP_FILTER_AND ||
1753 ftype == LDAP_FILTER_OR ||
1754 ftype == LDAP_FILTER_NOT )
1756 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1757 f->f_choice = ftype;
1759 f->f_list->f_next = f2;
1767 slapi_x_filter_append( int ftype,
1768 Slapi_Filter **pContainingFilter, /* NULL on first call */
1769 Slapi_Filter **pNextFilter,
1770 Slapi_Filter *filterToAppend )
1772 if ( ftype == LDAP_FILTER_AND ||
1773 ftype == LDAP_FILTER_OR ||
1774 ftype == LDAP_FILTER_NOT )
1776 if ( *pContainingFilter == NULL ) {
1777 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
1778 (*pContainingFilter)->f_choice = ftype;
1779 (*pContainingFilter)->f_list = filterToAppend;
1780 (*pContainingFilter)->f_next = NULL;
1782 if ( (*pContainingFilter)->f_choice != ftype ) {
1786 (*pNextFilter)->f_next = filterToAppend;
1788 *pNextFilter = filterToAppend;
1796 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1803 /* spec says return zero if no filter. */
1807 if ( verify_access ) {
1810 return LDAP_PARAM_ERROR;
1816 * According to acl.c it is safe to call test_filter() with
1819 rc = test_filter( op, e, f );
1821 case LDAP_COMPARE_TRUE:
1824 case LDAP_COMPARE_FALSE:
1826 case SLAPD_COMPARE_UNDEFINED:
1829 case LDAP_PROTOCOL_ERROR:
1830 /* filter type unknown: spec says return -1 */
1839 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
1841 return slapi_filter_test( NULL, e, f, 0 );
1845 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
1847 switch ( f->f_choice ) {
1848 case LDAP_FILTER_AND:
1849 case LDAP_FILTER_NOT:
1850 case LDAP_FILTER_OR: {
1854 * FIXME: altering f; should we use a temporary?
1856 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1857 rc = slapi_filter_apply( f, fn, arg, error_code );
1861 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
1867 case LDAP_FILTER_EQUALITY:
1868 case LDAP_FILTER_SUBSTRINGS:
1869 case LDAP_FILTER_GE:
1870 case LDAP_FILTER_LE:
1871 case LDAP_FILTER_PRESENT:
1872 case LDAP_FILTER_APPROX:
1873 case LDAP_FILTER_EXT:
1874 *error_code = fn( f, arg );
1877 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1880 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
1881 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
1890 struct berval **vals,
1894 * FIXME: what's the point?
1899 #define MAX_HOSTNAME 512
1902 slapi_get_hostname( void )
1905 static int been_here = 0;
1906 static char *static_hn = NULL;
1908 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1910 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1911 if ( static_hn == NULL) {
1912 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
1913 "Cannot allocate memory for hostname\n" );
1915 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1920 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1921 slapi_log_error( SLAPI_LOG_FATAL,
1923 "can't get hostname\n" );
1924 slapi_ch_free( (void **)&static_hn );
1926 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1935 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1937 hn = ch_strdup( static_hn );
1943 * FIXME: this should go in an appropriate header ...
1945 extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist );
1954 int rc = LDAP_SUCCESS;
1957 va_start( arglist, fmt );
1958 rc = slapi_int_log_error( severity, subsystem, fmt, arglist );
1966 slapi_timer_current_time( void )
1968 static int first_time = 1;
1969 #if !defined (_WIN32)
1973 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1976 gettimeofday( &base_time, NULL );
1978 gettimeofday( &now, NULL );
1979 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1980 (now.tv_usec - base_time.tv_usec);
1981 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1987 return (slap_get_time() - starttime) * 1000000;
1994 performance_counter_present = QueryPerformanceCounter( &base_time );
1995 QueryPerformanceFrequency( &performance_freq );
1998 if ( !performance_counter_present )
2001 QueryPerformanceCounter( &now );
2002 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2010 slapi_timer_get_time( char *label )
2012 unsigned long start = slapi_timer_current_time();
2013 printf("%10ld %10d usec %s\n", start, 0, label);
2021 slapi_timer_elapsed_time(
2023 unsigned long start )
2025 unsigned long stop = slapi_timer_current_time();
2026 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2030 slapi_free_search_results_internal( Slapi_PBlock *pb )
2032 Slapi_Entry **entries;
2033 int k = 0, nEnt = 0;
2035 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2036 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2037 if ( nEnt == 0 || entries == NULL ) {
2041 for ( k = 0; k < nEnt; k++ ) {
2042 slapi_entry_free( entries[k] );
2046 slapi_ch_free( (void **)&entries );
2049 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2052 return LDAP_PARAM_ERROR;
2054 if ( pb->pb_conn == NULL )
2055 return LDAP_PARAM_ERROR;
2058 *isSSL = pb->pb_conn->c_is_tls;
2063 return LDAP_SUCCESS;
2067 * DS 5.x compatability API follow
2070 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2075 return LDAP_PARAM_ERROR;
2077 at = attr->a_desc->ad_type;
2079 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2081 if ( is_at_single_value( at ) )
2082 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2083 if ( is_at_operational( at ) )
2084 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2085 if ( is_at_obsolete( at ) )
2086 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2087 if ( is_at_collective( at ) )
2088 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2089 if ( is_at_no_user_mod( at ) )
2090 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2092 return LDAP_SUCCESS;
2095 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2097 unsigned long flags;
2099 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2101 return (flags & flag) ? 1 : 0;
2104 Slapi_Attr *slapi_attr_new( void )
2108 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2113 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2116 AttributeDescription *ad = NULL;
2118 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2131 void slapi_attr_free( Slapi_Attr **a )
2137 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2139 return attr_dup( (Slapi_Attr *)attr );
2142 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2145 struct berval *nvalp;
2147 AttributeDescription *desc = a->a_desc;
2149 if ( desc->ad_type->sat_equality &&
2150 desc->ad_type->sat_equality->smr_normalize ) {
2151 rc = (*desc->ad_type->sat_equality->smr_normalize)(
2152 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2153 desc->ad_type->sat_syntax,
2154 desc->ad_type->sat_equality,
2155 (Slapi_Value *)v, &nval, NULL );
2156 if ( rc != LDAP_SUCCESS ) {
2164 rc = value_add_one( &a->a_vals, (Slapi_Value *)v );
2165 if ( rc == 0 && nvalp != NULL ) {
2166 rc = value_add_one( &a->a_nvals, nvalp );
2168 a->a_nvals = a->a_vals;
2171 if ( nvalp != NULL ) {
2172 slapi_ch_free_string( &nval.bv_val );
2178 int slapi_attr_type2plugin( const char *type, void **pi )
2185 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2187 if ( attr == NULL ) {
2188 return LDAP_PARAM_ERROR;
2191 *type = attr->a_desc->ad_cname.bv_val;
2193 return LDAP_SUCCESS;
2196 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2198 if ( attr == NULL ) {
2199 return LDAP_PARAM_ERROR;
2201 *oidp = attr->a_desc->ad_type->sat_oid;
2203 return LDAP_SUCCESS;
2206 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2213 mr = a->a_desc->ad_type->sat_equality;
2214 rc = value_match( &ret, a->a_desc, mr,
2215 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2216 (struct berval *)v1, (void *)v2, &text );
2217 if ( rc != LDAP_SUCCESS )
2220 return ( ret == 0 ) ? 0 : -1;
2223 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2232 if ( a ->a_vals == NULL ) {
2235 mr = a->a_desc->ad_type->sat_equality;
2236 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2237 rc = value_match( &ret, a->a_desc, mr,
2238 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2239 if ( rc != LDAP_SUCCESS ) {
2249 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2251 AttributeDescription *a1 = NULL;
2252 AttributeDescription *a2 = NULL;
2256 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2260 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2264 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2265 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2266 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2269 case SLAPI_TYPE_CMP_EXACT:
2270 ret = ad_cmp( a1, a2 );
2272 case SLAPI_TYPE_CMP_BASE:
2273 ret = ad_base_cmp( a1, a2 );
2275 case SLAPI_TYPE_CMP_SUBTYPE:
2276 ret = is_ad_subtype( a2, a2 );
2286 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2288 return ( slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT ) == 0 );
2291 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2293 return slapi_valueset_first_value( &a->a_vals, v );
2296 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2298 return slapi_valueset_next_value( &a->a_vals, hint, v );
2301 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2303 *numValues = slapi_valueset_count( &a->a_vals );
2308 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2310 *vs = &((Slapi_Attr *)a)->a_vals;
2315 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2317 return slapi_attr_get_values( a, vals );
2320 char *slapi_attr_syntax_normalize( const char *s )
2322 AttributeDescription *ad = NULL;
2325 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2329 return ad->ad_cname.bv_val;
2332 Slapi_Value *slapi_value_new( void )
2336 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2341 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2343 return ber_dupbv( NULL, (struct berval *)bval );
2346 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2348 return slapi_value_new_berval( v );
2351 Slapi_Value *slapi_value_new_string(const char *s)
2355 bv.bv_val = (char *)s;
2356 bv.bv_len = strlen( s );
2358 return slapi_value_new_berval( &bv );
2361 Slapi_Value *slapi_value_init(Slapi_Value *val)
2369 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2371 return ber_dupbv( v, bval );
2374 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2376 v->bv_val = slapi_ch_strdup( s );
2377 v->bv_len = strlen( s );
2382 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2384 return slapi_value_new_value( v );
2387 void slapi_value_free(Slapi_Value **value)
2389 if ( value == NULL ) {
2393 if ( (*value) != NULL ) {
2394 slapi_ch_free( (void **)&(*value)->bv_val );
2395 slapi_ch_free( (void **)value );
2399 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2404 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2406 if ( value == NULL ) {
2409 if ( value->bv_val != NULL ) {
2410 slapi_ch_free( (void **)&value->bv_val );
2412 slapi_value_init_berval( value, (struct berval *)bval );
2417 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2419 if ( value == NULL ) {
2422 return slapi_value_set_berval( value, vfrom );
2425 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2427 if ( value == NULL ) {
2430 if ( value->bv_val != NULL ) {
2431 slapi_ch_free( (void **)&value->bv_val );
2433 value->bv_val = slapi_ch_malloc( len );
2434 value->bv_len = len;
2435 AC_MEMCPY( value->bv_val, val, len );
2440 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2442 if ( value == NULL ) {
2445 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2449 int slapi_value_set_int(Slapi_Value *value, int intVal)
2453 snprintf( buf, sizeof( buf ), "%d", intVal );
2455 return slapi_value_set_string( value, buf );
2458 const char *slapi_value_get_string(const Slapi_Value *value)
2460 if ( value == NULL ) return NULL;
2461 if ( value->bv_val == NULL ) return NULL;
2462 if ( !checkBVString( value ) ) return NULL;
2464 return value->bv_val;
2467 int slapi_value_get_int(const Slapi_Value *value)
2469 if ( value == NULL ) return 0;
2470 if ( value->bv_val == NULL ) return 0;
2471 if ( !checkBVString( value ) ) return 0;
2473 return (int)strtol( value->bv_val, NULL, 10 );
2476 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2478 if ( value == NULL ) return 0;
2479 if ( value->bv_val == NULL ) return 0;
2480 if ( !checkBVString( value ) ) return 0;
2482 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2485 long slapi_value_get_long(const Slapi_Value *value)
2487 if ( value == NULL ) return 0;
2488 if ( value->bv_val == NULL ) return 0;
2489 if ( !checkBVString( value ) ) return 0;
2491 return strtol( value->bv_val, NULL, 10 );
2494 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2496 if ( value == NULL ) return 0;
2497 if ( value->bv_val == NULL ) return 0;
2498 if ( !checkBVString( value ) ) return 0;
2500 return strtoul( value->bv_val, NULL, 10 );
2503 size_t slapi_value_get_length(const Slapi_Value *value)
2505 if ( value == NULL )
2508 return (size_t) value->bv_len;
2511 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2513 return slapi_attr_value_cmp( a, v1, v2 );
2516 /* A ValueSet is a container for a BerVarray. */
2517 Slapi_ValueSet *slapi_valueset_new( void )
2521 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2527 void slapi_valueset_free(Slapi_ValueSet *vs)
2532 ber_bvarray_free( vp );
2533 slapi_ch_free( (void **)&vp );
2539 void slapi_valueset_init(Slapi_ValueSet *vs)
2541 if ( vs != NULL && *vs == NULL ) {
2542 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2543 (*vs)->bv_val = NULL;
2548 void slapi_valueset_done(Slapi_ValueSet *vs)
2555 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
2557 slapi_ch_free( (void **)&vp->bv_val );
2559 /* but don't free *vs or vs */
2562 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2566 ber_dupbv( &bv, (Slapi_Value *)addval );
2567 ber_bvarray_add( vs, &bv );
2570 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2572 return slapi_valueset_next_value( vs, 0, v );
2575 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2585 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2595 int slapi_valueset_count( const Slapi_ValueSet *vs )
2605 for ( i = 0; vp[i].bv_val != NULL; i++ )
2612 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2616 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2617 slapi_valueset_add_value( vs1, vp );
2621 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
2622 struct berval *val, int access )
2625 slap_access_t slap_access;
2626 AttributeDescription *ad = NULL;
2629 rc = slap_str2ad( attr, &ad, &text );
2630 if ( rc != LDAP_SUCCESS ) {
2635 * Whilst the SLAPI access types are arranged as a bitmask, the
2636 * documentation indicates that they are to be used separately.
2638 switch ( access & SLAPI_ACL_ALL ) {
2639 case SLAPI_ACL_COMPARE:
2640 slap_access = ACL_COMPARE;
2642 case SLAPI_ACL_SEARCH:
2643 slap_access = ACL_SEARCH;
2645 case SLAPI_ACL_READ:
2646 slap_access = ACL_READ;
2648 case SLAPI_ACL_WRITE:
2649 slap_access = ACL_WRITE;
2651 case SLAPI_ACL_DELETE:
2652 slap_access = ACL_WDEL;
2655 slap_access = ACL_WADD;
2657 case SLAPI_ACL_SELF: /* not documented */
2658 case SLAPI_ACL_PROXY: /* not documented */
2660 return LDAP_INSUFFICIENT_ACCESS;
2664 assert( pb->pb_op != NULL );
2666 if ( access_allowed( pb->pb_op, e, ad, val, slap_access, NULL ) ) {
2667 return LDAP_SUCCESS;
2670 return LDAP_INSUFFICIENT_ACCESS;
2673 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
2675 int rc = LDAP_SUCCESS;
2678 if ( pb == NULL || pb->pb_op == NULL )
2679 return LDAP_PARAM_ERROR;
2681 ml = slapi_int_ldapmods2modifications( mods );
2686 if ( rc == LDAP_SUCCESS ) {
2687 rc = acl_check_modlist( pb->pb_op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2690 slap_mods_free( ml, 1 );
2696 * Synthesise an LDAPMod array from a Modifications list to pass
2699 LDAPMod **slapi_int_modifications2ldapmods( Modifications *modlist )
2702 LDAPMod **mods, *modp;
2705 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
2708 mods = (LDAPMod **)slapi_ch_malloc( (i + 1) * sizeof(LDAPMod *) );
2710 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
2711 mods[i] = (LDAPMod *)slapi_ch_malloc( sizeof(LDAPMod) );
2713 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
2714 if ( BER_BVISNULL( &ml->sml_type ) ) {
2715 /* may happen for internally generated mods */
2716 assert( ml->sml_desc != NULL );
2717 modp->mod_type = slapi_ch_strdup( ml->sml_desc->ad_cname.bv_val );
2719 modp->mod_type = slapi_ch_strdup( ml->sml_type.bv_val );
2720 BER_BVZERO( &ml->sml_type );
2723 if ( ml->sml_values != NULL ) {
2724 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
2726 modp->mod_bvalues = (struct berval **)slapi_ch_malloc( (j + 1) *
2727 sizeof(struct berval *) );
2728 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
2729 modp->mod_bvalues[j] = (struct berval *)slapi_ch_malloc(
2730 sizeof(struct berval) );
2731 ber_dupbv( modp->mod_bvalues[j], &ml->sml_values[j] );
2733 modp->mod_bvalues[j] = NULL;
2735 modp->mod_bvalues = NULL;
2746 * Convert a potentially modified array of LDAPMods back to a
2747 * Modification list. Unfortunately the values need to be
2748 * duplicated because slap_mods_check() will try to free them
2749 * before prettying (and we can't easily get out of calling
2750 * slap_mods_check() because we need normalized values).
2752 Modifications *slapi_int_ldapmods2modifications ( LDAPMod **mods )
2754 Modifications *modlist = NULL, **modtail;
2756 char textbuf[SLAP_TEXT_BUFLEN];
2759 if ( mods == NULL ) {
2765 for ( modp = mods; *modp != NULL; modp++ ) {
2767 LDAPMod *lmod = *modp;
2770 AttributeDescription *ad = NULL;
2772 if ( slap_str2ad( lmod->mod_type, &ad, &text ) != LDAP_SUCCESS ) {
2776 mod = (Modifications *) slapi_ch_malloc( sizeof(Modifications) );
2777 mod->sml_op = lmod->mod_op & ~(LDAP_MOD_BVALUES);
2779 mod->sml_type = ad->ad_cname;
2781 mod->sml_next = NULL;
2784 if ( lmod->mod_op & LDAP_MOD_BVALUES ) {
2785 if ( lmod->mod_bvalues != NULL ) {
2786 while ( lmod->mod_bvalues[i] != NULL )
2790 if ( lmod->mod_values != NULL ) {
2791 while ( lmod->mod_values[i] != NULL )
2797 mod->sml_values = NULL;
2799 mod->sml_values = (BerVarray) slapi_ch_malloc( (i + 1) * sizeof(struct berval) );
2801 /* NB: This implicitly trusts a plugin to return valid modifications. */
2802 if ( lmod->mod_op & LDAP_MOD_BVALUES ) {
2803 for ( i = 0; lmod->mod_bvalues[i] != NULL; i++ ) {
2804 ber_dupbv( &mod->sml_values[i], lmod->mod_bvalues[i] );
2807 for ( i = 0; lmod->mod_values[i] != NULL; i++ ) {
2808 mod->sml_values[i].bv_val = slapi_ch_strdup( lmod->mod_values[i] );
2809 mod->sml_values[i].bv_len = strlen( lmod->mod_values[i] );
2812 mod->sml_values[i].bv_val = NULL;
2813 mod->sml_values[i].bv_len = 0;
2815 mod->sml_nvalues = NULL;
2818 modtail = &mod->sml_next;
2821 if ( slap_mods_check( modlist, &text, textbuf, sizeof( textbuf ), NULL ) != LDAP_SUCCESS ) {
2822 slap_mods_free( modlist, 1 );
2830 * Sun ONE DS 5.x computed attribute support. Computed attributes
2831 * allow for dynamically generated operational attributes, a very
2832 * useful thing indeed.
2836 * For some reason Sun don't use the normal plugin mechanism
2837 * registration path to register an "evaluator" function (an
2838 * "evaluator" is responsible for adding computed attributes;
2839 * the nomenclature is somewhat confusing).
2841 * As such slapi_compute_add_evaluator() registers the
2842 * function directly.
2844 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
2846 Slapi_PBlock *pPlugin = NULL;
2848 int type = SLAPI_PLUGIN_OBJECT;
2850 pPlugin = slapi_pblock_new();
2851 if ( pPlugin == NULL ) {
2852 rc = LDAP_NO_MEMORY;
2856 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
2857 if ( rc != LDAP_SUCCESS ) {
2861 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
2862 if ( rc != LDAP_SUCCESS ) {
2866 rc = slapi_int_register_plugin( frontendDB, pPlugin );
2873 if ( rc != LDAP_SUCCESS ) {
2874 if ( pPlugin != NULL ) {
2875 slapi_pblock_destroy( pPlugin );
2884 * See notes above regarding slapi_compute_add_evaluator().
2886 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
2888 Slapi_PBlock *pPlugin = NULL;
2890 int type = SLAPI_PLUGIN_OBJECT;
2892 pPlugin = slapi_pblock_new();
2893 if ( pPlugin == NULL ) {
2894 rc = LDAP_NO_MEMORY;
2898 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
2899 if ( rc != LDAP_SUCCESS ) {
2903 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
2904 if ( rc != LDAP_SUCCESS ) {
2908 rc = slapi_int_register_plugin( frontendDB, pPlugin );
2915 if ( rc != LDAP_SUCCESS ) {
2916 if ( pPlugin != NULL ) {
2917 slapi_pblock_destroy( pPlugin );
2926 * Call compute evaluators
2928 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
2931 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
2933 rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
2934 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
2935 /* Nothing to do; front-end should ignore. */
2939 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
2941 * -1: no attribute matched requested type
2942 * 0: one attribute matched
2943 * >0: error happened
2945 rc = (*pGetPlugin)( c, type, e, outputfn );
2951 slapi_ch_free( (void **)&tmpPlugin );
2957 compute_rewrite_search_filter( Slapi_PBlock *pb )
2959 if ( pb == NULL || pb->pb_op == NULL )
2960 return LDAP_PARAM_ERROR;
2962 return slapi_int_call_plugins( pb->pb_op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
2966 * New API to provide the plugin with access to the search
2967 * pblock. Have informed Sun DS team.
2970 slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
2975 if ( c->cac_pb == NULL )
2983 Slapi_Mutex *slapi_new_mutex( void )
2987 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
2988 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
2989 slapi_ch_free( (void **)&m );
2996 void slapi_destroy_mutex( Slapi_Mutex *mutex )
2998 if ( mutex != NULL ) {
2999 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3000 slapi_ch_free( (void **)&mutex);
3004 void slapi_lock_mutex( Slapi_Mutex *mutex )
3006 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3009 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3011 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3014 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3018 if ( mutex == NULL ) {
3022 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3023 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3024 slapi_ch_free( (void **)&cv );
3028 cv->mutex = mutex->mutex;
3033 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3035 if ( cvar != NULL ) {
3036 ldap_pvt_thread_cond_destroy( &cvar->cond );
3037 slapi_ch_free( (void **)&cvar );
3041 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3043 if ( cvar == NULL ) {
3047 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3050 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3052 if ( cvar == NULL ) {
3057 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3060 return ldap_pvt_thread_cond_signal( &cvar->cond );
3063 int slapi_int_access_allowed( Operation *op,
3065 AttributeDescription *desc,
3067 slap_access_t access,
3068 AccessControlState *state )
3070 int rc, slap_access = 0;
3071 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3074 pb = SLAPI_OPERATION_PBLOCK( op );
3076 /* internal operation */
3082 slap_access |= SLAPI_ACL_COMPARE;
3085 slap_access |= SLAPI_ACL_SEARCH;
3088 slap_access |= SLAPI_ACL_READ;
3091 slap_access |= SLAPI_ACL_WRITE;
3094 slap_access |= SLAPI_ACL_DELETE;
3097 slap_access |= SLAPI_ACL_ADD;
3103 rc = slapi_int_get_plugins( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
3104 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3105 /* nothing to do; allowed access */
3109 rc = 1; /* default allow policy */
3111 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3116 rc = (*pGetPlugin)( pb, entry, desc->ad_cname.bv_val,
3117 val, slap_access, (void *)state );
3123 slapi_ch_free( (void **)&tmpPlugin );
3129 * There is no documentation for this.
3131 int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
3143 rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
3144 if ( rc != LDAP_SUCCESS ) {
3148 if ( lrdn[1] != NULL ) {
3149 return -1; /* not single valued */
3154 *type = slapi_ch_strdup( ava->la_attr.bv_val );
3155 ber_dupbv( bv, &ava->la_value );
3162 char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
3164 struct berval new_dn, parent_dn, newrdn;
3166 new_dn.bv_val = NULL;
3168 parent_dn.bv_val = (char *)dn;
3169 parent_dn.bv_len = strlen( dn );
3171 newrdn.bv_val = (char *)rdn;
3172 newrdn.bv_len = strlen( rdn );
3174 build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
3176 return new_dn.bv_val;
3179 int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
3183 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
3184 size_t textlen = sizeof textbuf;
3185 int rc = LDAP_SUCCESS;
3187 PBLOCK_ASSERT_OP( pb, 0 );
3189 be_orig = pb->pb_op->o_bd;
3191 pb->pb_op->o_bd = select_backend( &e->e_nname, 0, 0 );
3192 if ( pb->pb_op->o_bd != NULL ) {
3193 rc = entry_schema_check( pb->pb_op, e, NULL, 0,
3194 &text, textbuf, textlen );
3196 pb->pb_op->o_bd = be_orig;
3198 return ( rc == LDAP_SUCCESS ) ? 0 : 1;
3201 int slapi_entry_rdn_values_present( const Slapi_Entry *e )
3205 int i = 0, match = 0;
3207 rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
3208 if ( rc != LDAP_SUCCESS ) {
3212 if ( dn[0] != NULL ) {
3213 LDAPRDN rdn = dn[0];
3215 for ( i = 0; rdn[i] != NULL; i++ ) {
3216 LDAPAVA *ava = &rdn[0][i];
3217 Slapi_Attr *a = NULL;
3219 if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
3220 slapi_attr_value_find( a, &ava->la_value ) == 0 )
3227 return ( i == match );
3230 int slapi_entry_add_rdn_values( Slapi_Entry *e )
3235 rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
3236 if ( rc != LDAP_SUCCESS ) {
3240 if ( dn[0] != NULL ) {
3241 LDAPRDN rdn = dn[0];
3242 struct berval *vals[2];
3244 for ( i = 0; rdn[i] != NULL; i++ ) {
3245 LDAPAVA *ava = &rdn[0][i];
3246 Slapi_Attr *a = NULL;
3248 if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
3249 slapi_attr_value_find( a, &ava->la_value ) == 0 )
3252 vals[0] = &ava->la_value;
3255 slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
3261 return LDAP_SUCCESS;
3264 const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
3268 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
3269 if ( attr == NULL ) {
3273 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
3274 return slapi_value_get_string( &attr->a_vals[0] );
3280 void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
3284 attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
3286 bv.bv_val = uniqueid;
3287 bv.bv_len = strlen( uniqueid );
3288 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
3291 LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
3298 size = sizeof("ldap:///");
3301 size += strlen( ldaphost );
3302 if ( ldapport != 0 )
3305 url = slapi_ch_malloc( size );
3307 if ( ldapport != 0 ) {
3308 sprintf( url, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
3310 sprintf( url, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
3313 rc = ldap_initialize( &ld, url );
3315 slapi_ch_free_string( &url );
3317 return ( rc == LDAP_SUCCESS ) ? ld : NULL;
3320 void slapi_ldap_unbind( LDAP *ld )
3322 ldap_unbind_ext_s( ld, NULL, NULL );
3325 int slapi_x_backend_get_flags( const Slapi_Backend *be, unsigned long *flags )
3328 return LDAP_PARAM_ERROR;
3330 *flags = SLAP_DBFLAGS(be);
3332 return LDAP_SUCCESS;
3336 slapi_int_count_controls( LDAPControl **ctrls )
3340 if ( ctrls == NULL )
3343 for ( i = 0; ctrls[i] != NULL; i++ )
3350 slapi_op_abandoned( Slapi_PBlock *pb )
3352 if ( pb->pb_op == NULL )
3355 return ( pb->pb_op->o_abandon );
3359 slapi_op_type_to_string(unsigned long type)
3364 case SLAPI_OPERATION_BIND:
3367 case SLAPI_OPERATION_UNBIND:
3370 case SLAPI_OPERATION_SEARCH:
3373 case SLAPI_OPERATION_MODIFY:
3376 case SLAPI_OPERATION_ADD:
3379 case SLAPI_OPERATION_DELETE:
3382 case SLAPI_OPERATION_MODDN:
3385 case SLAPI_OPERATION_COMPARE:
3388 case SLAPI_OPERATION_ABANDON:
3391 case SLAPI_OPERATION_EXTENDED:
3395 str = "unknown operation type";
3402 slapi_op_get_type(Slapi_Operation * op)
3406 switch ( op->o_tag ) {
3408 type = SLAPI_OPERATION_BIND;
3410 case LDAP_REQ_UNBIND:
3411 type = SLAPI_OPERATION_UNBIND;
3413 case LDAP_REQ_SEARCH:
3414 type = SLAPI_OPERATION_SEARCH;
3416 case LDAP_REQ_MODIFY:
3417 type = SLAPI_OPERATION_MODIFY;
3420 type = SLAPI_OPERATION_ADD;
3422 case LDAP_REQ_DELETE:
3423 type = SLAPI_OPERATION_DELETE;
3425 case LDAP_REQ_MODRDN:
3426 type = SLAPI_OPERATION_MODDN;
3428 case LDAP_REQ_COMPARE:
3429 type = SLAPI_OPERATION_COMPARE;
3431 case LDAP_REQ_ABANDON:
3432 type = SLAPI_OPERATION_ABANDON;
3434 case LDAP_REQ_EXTENDED:
3435 type = SLAPI_OPERATION_EXTENDED;
3438 type = SLAPI_OPERATION_NONE;
3444 void slapi_be_set_readonly( Slapi_Backend *be, int readonly )
3450 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
3452 be->be_restrictops &= ~(SLAP_RESTRICT_OP_WRITES);
3455 int slapi_be_get_readonly( Slapi_Backend *be )
3460 return ( (be->be_restrictops & SLAP_RESTRICT_OP_WRITES) == SLAP_RESTRICT_OP_WRITES );
3463 const char *slapi_x_be_get_updatedn( Slapi_Backend *be )
3468 return be->be_update_ndn.bv_val;
3471 Slapi_Backend *slapi_be_select( const Slapi_DN *sdn )
3475 slapi_sdn_get_ndn( sdn );
3477 be = select_backend( (struct berval *)&sdn->ndn, 0, 0 );
3484 slapi_operation_set_flag(Slapi_Operation *op, unsigned long flag)
3489 slapi_operation_clear_flag(Slapi_Operation *op, unsigned long flag)
3494 slapi_operation_is_flag_set(Slapi_Operation *op, unsigned long flag)
3499 #endif /* LDAP_SLAPI */