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>
36 * server start time (should we use a struct timeval also in slapd?
38 static struct timeval base_time;
39 ldap_pvt_thread_mutex_t slapi_hn_mutex;
40 ldap_pvt_thread_mutex_t slapi_time_mutex;
43 ldap_pvt_thread_mutex_t mutex;
46 struct slapi_condvar {
47 ldap_pvt_thread_cond_t cond;
48 ldap_pvt_thread_mutex_t mutex;
52 static int checkBVString(const struct berval *bv)
56 for ( i = 0; i < bv->bv_len; i++ ) {
57 if ( bv->bv_val[i] == '\0' )
60 if ( bv->bv_val[i] != '\0' )
65 #endif /* LDAP_SLAPI */
68 * This function converts an array of pointers to berval objects to
69 * an array of berval objects.
74 struct berval **bvptr,
77 int rc = LDAP_SUCCESS;
81 if ( bvptr == NULL || *bvptr == NULL ) {
85 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
89 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
90 if ( tmpberval == NULL ) {
91 return LDAP_NO_MEMORY;
94 for ( i = 0; bvptr[i] != NULL; i++ ) {
95 tmpberval[i].bv_val = bvptr[i]->bv_val;
96 tmpberval[i].bv_len = bvptr[i]->bv_len;
98 tmpberval[i].bv_val = NULL;
99 tmpberval[i].bv_len = 0;
101 if ( rc == LDAP_SUCCESS ) {
114 Slapi_Entry *e = NULL;
117 pTmpS = slapi_ch_strdup( s );
118 if ( pTmpS != NULL ) {
119 e = str2entry( pTmpS );
120 slapi_ch_free( (void **)&pTmpS );
126 #endif /* LDAP_SLAPI */
137 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
138 ret = entry2str( e, len );
139 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
142 #else /* LDAP_SLAPI */
144 #endif /* LDAP_SLAPI */
148 slapi_entry_get_dn( Slapi_Entry *e )
151 return e->e_name.bv_val;
152 #else /* LDAP_SLAPI */
154 #endif /* LDAP_SLAPI */
158 slapi_x_entry_get_id( Slapi_Entry *e )
164 #endif /* LDAP_SLAPI */
173 struct berval dn = BER_BVNULL;
176 dn.bv_len = strlen( ldn );
178 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname, NULL );
179 #endif /* LDAP_SLAPI */
183 slapi_entry_dup( Slapi_Entry *e )
186 return entry_dup( e );
187 #else /* LDAP_SLAPI */
189 #endif /* LDAP_SLAPI */
193 slapi_entry_attr_delete(
198 AttributeDescription *ad = NULL;
201 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
202 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
205 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
206 return 0; /* attribute is deleted */
208 return -1; /* something went wrong */
210 #else /* LDAP_SLAPI */
212 #endif /* LDAP_SLAPI */
216 slapi_entry_alloc( void )
219 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
220 #else /* LDAP_SLAPI */
222 #endif /* LDAP_SLAPI */
226 slapi_entry_free( Slapi_Entry *e )
230 #endif /* LDAP_SLAPI */
234 slapi_entry_attr_merge(
237 struct berval **vals )
240 AttributeDescription *ad = NULL;
245 rc = bvptr2obj( vals, &bv );
246 if ( rc != LDAP_SUCCESS ) {
250 rc = slap_str2ad( type, &ad, &text );
251 if ( rc != LDAP_SUCCESS ) {
255 rc = attr_merge_normalize_one( e, ad, bv, NULL );
259 #else /* LDAP_SLAPI */
261 #endif /* LDAP_SLAPI */
265 slapi_entry_attr_find(
271 AttributeDescription *ad = NULL;
275 rc = slap_str2ad( type, &ad, &text );
276 if ( rc != LDAP_SUCCESS ) {
280 *attr = attr_find( e->e_attrs, ad );
281 if ( *attr == NULL ) {
286 #else /* LDAP_SLAPI */
288 #endif /* LDAP_SLAPI */
292 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
295 AttributeDescription *ad = NULL;
300 rc = slap_str2ad( type, &ad, &text );
301 if ( rc != LDAP_SUCCESS ) {
305 attr = attr_find( e->e_attrs, ad );
306 if ( attr == NULL ) {
310 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
313 p = slapi_value_get_string( &attr->a_vals[0] );
315 return slapi_ch_strdup( (char *)p );
326 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
329 AttributeDescription *ad = NULL;
334 rc = slap_str2ad( type, &ad, &text );
335 if ( rc != LDAP_SUCCESS ) {
339 attr = attr_find( e->e_attrs, ad );
340 if ( attr == NULL ) {
344 return slapi_value_get_int( attr->a_vals );
351 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
354 AttributeDescription *ad = NULL;
359 rc = slap_str2ad( type, &ad, &text );
360 if ( rc != LDAP_SUCCESS ) {
364 attr = attr_find( e->e_attrs, ad );
365 if ( attr == NULL ) {
369 return slapi_value_get_long( attr->a_vals );
376 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
379 AttributeDescription *ad = NULL;
384 rc = slap_str2ad( type, &ad, &text );
385 if ( rc != LDAP_SUCCESS ) {
389 attr = attr_find( e->e_attrs, ad );
390 if ( attr == NULL ) {
394 return slapi_value_get_uint( attr->a_vals );
401 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
404 AttributeDescription *ad = NULL;
409 rc = slap_str2ad( type, &ad, &text );
410 if ( rc != LDAP_SUCCESS ) {
414 attr = attr_find( e->e_attrs, ad );
415 if ( attr == NULL ) {
419 return slapi_value_get_ulong( attr->a_vals );
426 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
430 AttributeDescription *ad = NULL;
435 rc = slap_str2ad( type, &ad, &text );
436 if ( rc != LDAP_SUCCESS ) {
440 attr = attr_find( e->e_attrs, ad );
441 if ( attr == NULL ) {
445 bv.bv_val = (char *)value;
446 bv.bv_len = strlen( value );
448 return ( slapi_attr_value_find( attr, &bv ) != -1 );
455 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
458 AttributeDescription *ad = NULL;
463 rc = slap_str2ad( type, &ad, &text );
464 if ( rc != LDAP_SUCCESS ) {
468 attr_delete ( &e->e_attrs, ad );
469 if ( value != NULL ) {
470 bv.bv_val = (char *)value;
471 bv.bv_len = strlen(value);
472 attr_merge_normalize_one( e, ad, &bv, NULL );
474 #endif /* LDAP_SLAPI */
478 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
483 snprintf( buf, sizeof( buf ), "%d", l );
484 slapi_entry_attr_set_charptr( e, type, buf );
485 #endif /* LDAP_SLAPI */
489 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
494 snprintf( buf, sizeof( buf ), "%u", l );
495 slapi_entry_attr_set_charptr( e, type, buf );
496 #endif /* LDAP_SLAPI */
500 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
505 snprintf( buf, sizeof( buf ), "%ld", l );
506 slapi_entry_attr_set_charptr( e, type, buf );
507 #endif /* LDAP_SLAPI */
511 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
516 snprintf( buf, sizeof( buf ), "%lu", l );
517 slapi_entry_attr_set_charptr( e, type, buf );
518 #endif /* LDAP_SLAPI */
522 slapi_is_rootdse( const char *dn )
525 return ( dn == NULL || dn[0] == '\0' );
532 slapi_entry_has_children(const Slapi_Entry *e)
537 int hasSubordinates = 0;
539 pConn = slapi_int_init_connection( NULL, LDAP_REQ_SEARCH );
540 if ( pConn == NULL ) {
544 op = (Operation *)pConn->c_pending_ops.stqh_first;
545 op->o_bd = select_backend( (struct berval *)&e->e_nname, 0, 0 );
546 if ( op->o_bd == NULL ) {
550 op->o_bd->be_has_subordinates( op, (Entry *)e, &hasSubordinates );
552 slapi_int_connection_destroy( &pConn );
554 return ( hasSubordinates == LDAP_COMPARE_TRUE );
561 * Return approximate size of the entry rounded to the nearest
562 * 1K. Only the size of the attribute values are counted in the
563 * Sun implementation.
565 * http://docs.sun.com/source/816-6701-10/funcref.html#1017388
567 size_t slapi_entry_size(Slapi_Entry *e)
574 for ( size = 0, a = e->e_attrs; a != NULL; a = a->a_next ) {
575 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
576 size += a->a_vals[i].bv_len + 1;
581 size -= (size % 1024);
586 #endif /* LDAP_SLAPI */
590 * Add values to entry.
593 * LDAP_SUCCESS Values added to entry
594 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
595 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
598 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
604 char textbuf[SLAP_TEXT_BUFLEN];
606 mod.sm_op = LDAP_MOD_ADD;
609 mod.sm_type.bv_val = (char *)type;
610 mod.sm_type.bv_len = strlen( type );
612 rc = slap_str2ad( type, &mod.sm_desc, &text );
613 if ( rc != LDAP_SUCCESS ) {
617 if ( vals == NULL ) {
618 /* Apparently vals can be NULL
619 * FIXME: sm_values = NULL ? */
620 mod.sm_values = (BerVarray)ch_malloc( sizeof(struct berval) );
621 mod.sm_values->bv_val = NULL;
624 rc = bvptr2obj( vals, &mod.sm_values );
625 if ( rc != LDAP_SUCCESS ) {
626 return LDAP_CONSTRAINT_VIOLATION;
629 mod.sm_nvalues = NULL;
631 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
633 ch_free( mod.sm_values );
635 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
638 #endif /* LDAP_SLAPI */
642 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
645 return slapi_entry_add_values( e, type, vals );
648 #endif /* LDAP_SLAPI */
652 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
655 AttributeDescription *ad = NULL;
659 rc = slap_str2ad( type, &ad, &text );
660 if ( rc != LDAP_SUCCESS ) {
664 return attr_merge_normalize( e, ad, *vs, NULL );
667 #endif /* LDAP_SLAPI */
671 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
677 char textbuf[SLAP_TEXT_BUFLEN];
679 mod.sm_op = LDAP_MOD_DELETE;
682 mod.sm_type.bv_val = (char *)type;
683 mod.sm_type.bv_len = strlen( type );
685 if ( vals == NULL ) {
686 /* If vals is NULL, this is a NOOP. */
690 rc = slap_str2ad( type, &mod.sm_desc, &text );
691 if ( rc != LDAP_SUCCESS ) {
695 if ( vals[0] == NULL ) {
696 /* SLAPI doco says LDAP_OPERATIONS_ERROR but LDAP_OTHER is better */
697 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
700 rc = bvptr2obj( vals, &mod.sm_values );
701 if ( rc != LDAP_SUCCESS ) {
702 return LDAP_CONSTRAINT_VIOLATION;
704 mod.sm_nvalues = NULL;
706 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
708 ch_free( mod.sm_values );
713 #endif /* LDAP_SLAPI */
717 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
720 return slapi_entry_delete_values( e, type, vals );
723 #endif /* LDAP_SLAPI */
727 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
730 return slapi_entry_attr_merge( e, (char *)type, vals );
733 #endif /* LDAP_SLAPI */
737 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
740 AttributeDescription *ad = NULL;
744 rc = slap_str2ad( type, &ad, &text );
745 if ( rc != LDAP_SUCCESS ) {
749 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
750 if ( rc != LDAP_SUCCESS ) {
757 #endif /* LDAP_SLAPI */
761 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
766 val.bv_val = (char *)value;
767 val.bv_len = strlen( value );
769 return slapi_entry_add_value( e, type, &val );
772 #endif /* LDAP_SLAPI */
776 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
779 Slapi_Value *vals[2];
782 val.bv_val = (char *)value;
783 val.bv_len = strlen( value );
787 return slapi_entry_delete_values_sv( e, type, vals );
790 #endif /* LDAP_SLAPI */
795 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
798 return slapi_entry_attr_merge( e, (char *)type, vals );
805 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
814 return ( *attr != NULL ) ? 0 : -1;
821 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
828 if ( prevattr == NULL ) {
832 *attr = prevattr->a_next;
834 return ( *attr != NULL ) ? 0 : -1;
841 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
844 AttributeDescription *ad = NULL;
849 rc = slap_str2ad( type, &ad, &text );
850 if ( rc != LDAP_SUCCESS ) {
854 attr_delete( &e->e_attrs, ad );
856 rc = bvptr2obj( vals, &bv );
857 if ( rc != LDAP_SUCCESS ) {
861 rc = attr_merge_normalize( e, ad, bv, NULL );
862 slapi_ch_free( (void **)&bv );
863 if ( rc != LDAP_SUCCESS ) {
870 #endif /* LDAP_SLAPI */
874 * FIXME -- The caller must free the allocated memory.
875 * In Netscape they do not have to.
878 slapi_attr_get_values(
880 struct berval ***vals )
886 if ( attr == NULL ) {
890 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
894 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
895 for ( j = 0; j < i; j++ ) {
896 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
900 *vals = (struct berval **)bv;
903 #else /* LDAP_SLAPI */
905 #endif /* LDAP_SLAPI */
909 slapi_dn_normalize( char *dn )
915 assert( dn != NULL );
918 bdn.bv_len = strlen( dn );
920 if ( dnPretty( NULL, &bdn, &pdn, NULL ) != LDAP_SUCCESS ) {
925 #else /* LDAP_SLAPI */
927 #endif /* LDAP_SLAPI */
931 slapi_dn_normalize_case( char *dn )
937 assert( dn != NULL );
940 bdn.bv_len = strlen( dn );
942 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
947 #else /* LDAP_SLAPI */
949 #endif /* LDAP_SLAPI */
958 struct berval bdn, ndn;
959 struct berval bsuffix, nsuffix;
962 assert( dn != NULL );
963 assert( suffix != NULL );
966 bdn.bv_len = strlen( dn );
968 bsuffix.bv_val = suffix;
969 bsuffix.bv_len = strlen( suffix );
971 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
975 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
978 slapi_ch_free( (void **)&ndn.bv_val );
982 rc = dnIsSuffix( &ndn, &nsuffix );
984 slapi_ch_free( (void **)&ndn.bv_val );
985 slapi_ch_free( (void **)&nsuffix.bv_val );
988 #else /* LDAP_SLAPI */
990 #endif /* LDAP_SLAPI */
995 const char *parentdn,
996 const char *childdn )
999 struct berval assertedParentDN, normalizedAssertedParentDN;
1000 struct berval childDN, normalizedChildDN;
1001 struct berval normalizedParentDN;
1004 assert( parentdn != NULL );
1005 assert( childdn != NULL );
1007 assertedParentDN.bv_val = (char *)parentdn;
1008 assertedParentDN.bv_len = strlen( parentdn );
1010 if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
1011 &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
1016 childDN.bv_val = (char *)childdn;
1017 childDN.bv_len = strlen( childdn );
1019 if ( dnNormalize( 0, NULL, NULL, &childDN,
1020 &normalizedChildDN, NULL ) != LDAP_SUCCESS )
1022 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
1026 dnParent( &normalizedChildDN, &normalizedParentDN );
1028 if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
1029 &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
1034 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
1035 slapi_ch_free( (void **)&normalizedChildDN.bv_val );
1037 return ( match == 0 );
1040 #endif /* LDAP_SLAPI */
1044 * Returns DN of the parent entry, or NULL if the DN is
1045 * an empty string or NULL, or has no parent.
1048 slapi_dn_parent( const char *_dn )
1051 struct berval dn, prettyDN;
1052 struct berval parentDN;
1054 if ( _dn == NULL ) {
1058 dn.bv_val = (char *)_dn;
1059 dn.bv_len = strlen( _dn );
1061 if ( dn.bv_len == 0 ) {
1065 if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
1069 dnParent( &prettyDN, &parentDN ); /* in-place */
1071 slapi_ch_free( (void **)&prettyDN.bv_val );
1073 if ( parentDN.bv_len == 0 ) {
1077 return slapi_ch_strdup( parentDN.bv_val );
1080 #endif /* LDAP_SLAPI */
1084 * Returns DN of the parent entry; or NULL if the DN is
1085 * an empty string, if the DN has no parent, or if the
1086 * DN is the suffix of the backend database
1088 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *_dn )
1092 struct berval dn, prettyDN;
1093 struct berval normalizedDN, parentDN;
1095 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
1098 dn.bv_val = (char *)_dn;
1099 dn.bv_len = strlen( _dn );
1101 if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
1105 if ( be != NULL && be_issuffix( be, &normalizedDN ) ) {
1106 slapi_ch_free( (void **)&prettyDN.bv_val );
1107 slapi_ch_free( (void **)&normalizedDN.bv_val );
1111 dnParent( &prettyDN, &parentDN );
1113 slapi_ch_free( (void **)&prettyDN.bv_val );
1114 slapi_ch_free( (void **)&normalizedDN.bv_val );
1116 if ( parentDN.bv_len == 0 ) {
1120 return slapi_ch_strdup( parentDN.bv_val );
1123 #endif /* LDAP_SLAPI */
1127 slapi_dn_ignore_case( char *dn )
1130 return slapi_dn_normalize_case( dn );
1131 #else /* LDAP_SLAPI */
1133 #endif /* LDAP_SLAPI */
1137 slapi_ch_malloc( unsigned long size )
1140 return ch_malloc( size );
1141 #else /* LDAP_SLAPI */
1143 #endif /* LDAP_SLAPI */
1147 slapi_ch_free( void **ptr )
1152 #endif /* LDAP_SLAPI */
1156 slapi_ch_free_string( char **ptr )
1159 slapi_ch_free( (void **)ptr );
1160 #endif /* LDAP_SLAPI */
1164 slapi_ch_array_free( char **arrayp )
1169 if ( arrayp != NULL ) {
1170 for ( p = arrayp; *p != NULL; p++ ) {
1171 slapi_ch_free( (void **)p );
1173 slapi_ch_free( (void **)&arrayp );
1179 slapi_ch_bvdup(const struct berval *v)
1184 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
1185 bv->bv_len = v->bv_len;
1186 bv->bv_val = slapi_ch_malloc( bv->bv_len );
1187 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1196 slapi_ch_bvecdup(const struct berval **v)
1206 for ( i = 0; v[i] != NULL; i++ )
1209 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1211 for ( i = 0; v[i] != NULL; i++ ) {
1212 rv[i] = slapi_ch_bvdup( v[i] );
1224 unsigned long nelem,
1225 unsigned long size )
1228 return ch_calloc( nelem, size );
1229 #else /* LDAP_SLAPI */
1231 #endif /* LDAP_SLAPI */
1237 unsigned long size )
1240 return ch_realloc( block, size );
1241 #else /* LDAP_SLAPI */
1243 #endif /* LDAP_SLAPI */
1247 slapi_ch_strdup( char *s )
1250 return ch_strdup( (const char *)s );
1251 #else /* LDAP_SLAPI */
1253 #endif /* LDAP_SLAPI */
1257 slapi_ch_stlen( char *s )
1260 return strlen( (const char *)s );
1261 #else /* LDAP_SLAPI */
1263 #endif /* LDAP_SLAPI */
1267 slapi_control_present(
1268 LDAPControl **controls,
1270 struct berval **val,
1285 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1286 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1291 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1293 * FIXME: according to 6.1 specification,
1294 * "The val output parameter is set
1295 * to point into the controls array.
1296 * A copy of the control value is
1300 struct berval *pTmpBval;
1302 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
1303 if ( pTmpBval == NULL ) {
1306 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
1307 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
1311 slapi_ch_free( (void **)&pTmpBval );
1317 *val = &controls[i]->ldctl_value;
1322 *iscritical = controls[i]->ldctl_iscritical;
1329 #else /* LDAP_SLAPI */
1331 #endif /* LDAP_SLAPI */
1336 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1337 unsigned long *slapi_mask)
1339 *slapi_mask = SLAPI_OPERATION_NONE;
1341 if ( slap_mask & SLAP_CTRL_ABANDON )
1342 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1344 if ( slap_mask & SLAP_CTRL_ADD )
1345 *slapi_mask |= SLAPI_OPERATION_ADD;
1347 if ( slap_mask & SLAP_CTRL_BIND )
1348 *slapi_mask |= SLAPI_OPERATION_BIND;
1350 if ( slap_mask & SLAP_CTRL_COMPARE )
1351 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1353 if ( slap_mask & SLAP_CTRL_DELETE )
1354 *slapi_mask |= SLAPI_OPERATION_DELETE;
1356 if ( slap_mask & SLAP_CTRL_MODIFY )
1357 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1359 if ( slap_mask & SLAP_CTRL_RENAME )
1360 *slapi_mask |= SLAPI_OPERATION_MODDN;
1362 if ( slap_mask & SLAP_CTRL_SEARCH )
1363 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1365 if ( slap_mask & SLAP_CTRL_UNBIND )
1366 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1370 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1371 slap_mask_t *slap_mask)
1375 if ( slapi_mask & SLAPI_OPERATION_BIND )
1376 *slap_mask |= SLAP_CTRL_BIND;
1378 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1379 *slap_mask |= SLAP_CTRL_UNBIND;
1381 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1382 *slap_mask |= SLAP_CTRL_SEARCH;
1384 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1385 *slap_mask |= SLAP_CTRL_MODIFY;
1387 if ( slapi_mask & SLAPI_OPERATION_ADD )
1388 *slap_mask |= SLAP_CTRL_ADD;
1390 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1391 *slap_mask |= SLAP_CTRL_DELETE;
1393 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1394 *slap_mask |= SLAP_CTRL_RENAME;
1396 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1397 *slap_mask |= SLAP_CTRL_COMPARE;
1399 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1400 *slap_mask |= SLAP_CTRL_ABANDON;
1402 *slap_mask |= SLAP_CTRL_GLOBAL;
1406 slapi_int_parse_control(
1411 /* Plugins must deal with controls themselves. */
1413 return LDAP_SUCCESS;
1415 #endif /* LDAP_SLAPI */
1418 slapi_register_supported_control(
1420 unsigned long controlops )
1423 slap_mask_t controlmask;
1425 slapiControlOp2SlapControlMask( controlops, &controlmask );
1427 register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL );
1428 #endif /* LDAP_SLAPI */
1432 slapi_get_supported_controls(
1434 unsigned long **ctrlopsp )
1439 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1440 if ( rc != LDAP_SUCCESS ) {
1444 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1445 /* In place, naughty. */
1446 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1449 return LDAP_SUCCESS;
1450 #else /* LDAP_SLAPI */
1452 #endif /* LDAP_SLAPI */
1456 slapi_dup_control( LDAPControl *ctrl )
1461 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1462 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1463 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1464 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1469 #endif /* LDAP_SLAPI */
1473 slapi_register_supported_saslmechanism( char *mechanism )
1476 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1477 slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1478 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1479 #endif /* LDAP_SLAPI */
1483 slapi_get_supported_saslmechanisms( void )
1486 /* FIXME -- can not get the saslmechanism without a connection. */
1487 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1488 "can not get the SASL mechanism list "
1489 "without a connection\n" );
1491 #else /* LDAP_SLAPI */
1493 #endif /* LDAP_SLAPI */
1497 slapi_get_supported_extended_ops( void )
1501 char **ppExtOpOID = NULL;
1504 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1508 for ( j = 0; slapi_int_get_supported_extop( j ) != NULL; j++ ) {
1513 if ( numExtOps == 0 ) {
1517 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1518 for ( k = 0; k < i; k++ ) {
1521 bv = get_supported_extop( k );
1522 assert( bv != NULL );
1524 ppExtOpOID[ k ] = bv->bv_val;
1527 for ( ; k < j; k++ ) {
1530 bv = slapi_int_get_supported_extop( k );
1531 assert( bv != NULL );
1533 ppExtOpOID[ i + k ] = bv->bv_val;
1535 ppExtOpOID[ i + k ] = NULL;
1538 #else /* LDAP_SLAPI */
1540 #endif /* LDAP_SLAPI */
1544 slapi_send_ldap_result(
1550 struct berval **urls )
1554 char *extOID = NULL;
1555 SlapReply rs = { REP_RESULT };
1557 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1560 rs.sr_matched = matched;
1565 slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
1567 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1568 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
1569 send_ldap_sasl( op, &rs );
1573 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1574 if ( extOID != NULL ) {
1575 rs.sr_rspoid = extOID;
1576 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
1577 send_ldap_extended( op, &rs );
1581 if (op->o_tag == LDAP_REQ_SEARCH)
1582 rs.sr_nentries = nentries;
1584 send_ldap_result( op, &rs );
1585 #endif /* LDAP_SLAPI */
1589 slapi_send_ldap_search_entry(
1592 LDAPControl **ectrls,
1598 SlapReply rs = { REP_RESULT };
1600 AttributeName *an = NULL;
1603 if ( attrs != NULL ) {
1604 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1612 an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
1613 for ( i = 0; attrs[i] != NULL; i++ ) {
1614 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1615 an[i].an_name.bv_len = strlen( attrs[i] );
1616 an[i].an_desc = NULL;
1617 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1620 an[i].an_name.bv_len = 0;
1621 an[i].an_name.bv_val = NULL;
1624 rs.sr_err = LDAP_SUCCESS;
1625 rs.sr_matched = NULL;
1628 rs.sr_ctrls = ectrls;
1630 rs.sr_operational_attrs = NULL;
1635 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1639 return send_search_entry( pOp, &rs );
1640 #else /* LDAP_SLAPI */
1642 #endif /* LDAP_SLAPI */
1646 slapi_send_ldap_search_reference(
1649 struct berval **references,
1650 LDAPControl **ectrls,
1651 struct berval **v2refs
1656 SlapReply rs = { REP_SEARCHREF };
1659 rs.sr_err = LDAP_SUCCESS;
1660 rs.sr_matched = NULL;
1663 rc = bvptr2obj( references, &rs.sr_ref );
1664 if ( rc != LDAP_SUCCESS ) {
1668 rs.sr_ctrls = ectrls;
1670 rs.sr_operational_attrs = NULL;
1673 if ( v2refs != NULL ) {
1674 rc = bvptr2obj( v2refs, &rs.sr_v2ref );
1675 if ( rc != LDAP_SUCCESS ) {
1676 slapi_ch_free( (void **)&rs.sr_ref );
1683 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1687 rc = send_search_reference( pOp, &rs );
1689 if ( rs.sr_ref != NULL )
1690 slapi_ch_free( (void **)&rs.sr_ref );
1692 if ( rs.sr_v2ref != NULL )
1693 slapi_ch_free( (void **)&rs.sr_v2ref );
1698 #endif /* LDAP_SLAPI */
1702 slapi_str2filter( char *str )
1705 return str2filter( str );
1706 #else /* LDAP_SLAPI */
1708 #endif /* LDAP_SLAPI */
1718 #endif /* LDAP_SLAPI */
1722 slapi_filter_dup( Slapi_Filter *filter )
1727 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1729 f->f_choice = filter->f_choice;
1731 switch ( f->f_choice ) {
1732 case LDAP_FILTER_AND:
1733 case LDAP_FILTER_NOT:
1734 case LDAP_FILTER_OR: {
1735 Filter *pFilter, **ppF;
1737 for ( pFilter = filter->f_list, ppF = &f->f_list;
1739 pFilter = pFilter->f_next, ppF = &f->f_next )
1741 *ppF = slapi_filter_dup( pFilter );
1747 case LDAP_FILTER_PRESENT:
1748 f->f_desc = filter->f_desc;
1750 case LDAP_FILTER_EQUALITY:
1751 case LDAP_FILTER_GE:
1752 case LDAP_FILTER_LE:
1753 case LDAP_FILTER_APPROX:
1754 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1755 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1756 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1758 case LDAP_FILTER_EXT:
1759 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1760 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1761 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1762 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1763 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1764 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1766 case LDAP_FILTER_SUBSTRINGS: {
1769 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1770 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1771 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1772 if ( filter->f_sub_any != NULL ) {
1773 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1775 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1776 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1777 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1779 f->f_sub_any[i].bv_val = NULL;
1781 f->f_sub_any = NULL;
1783 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1786 case SLAPD_FILTER_COMPUTED:
1787 f->f_result = filter->f_result;
1790 slapi_ch_free( (void **)&f );
1798 #endif /* LDAP_SLAPI */
1802 slapi_filter_get_choice( Slapi_Filter *f )
1814 #else /* LDAP_SLAPI */
1815 return -1; /* invalid filter type */
1816 #endif /* LDAP_SLAPI */
1820 slapi_filter_get_ava(
1823 struct berval **bval )
1827 int rc = LDAP_SUCCESS;
1829 assert( type != NULL );
1830 assert( bval != NULL );
1835 ftype = f->f_choice;
1836 if ( ftype == LDAP_FILTER_EQUALITY
1837 || ftype == LDAP_FILTER_GE
1838 || ftype == LDAP_FILTER_LE
1839 || ftype == LDAP_FILTER_APPROX ) {
1841 * According to the SLAPI Reference Manual these are
1844 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1845 *bval = &f->f_un.f_un_ava->aa_value;
1846 } else { /* filter type not supported */
1851 #else /* LDAP_SLAPI */
1853 #endif /* LDAP_SLAPI */
1857 slapi_filter_list_first( Slapi_Filter *f )
1866 ftype = f->f_choice;
1867 if ( ftype == LDAP_FILTER_AND
1868 || ftype == LDAP_FILTER_OR
1869 || ftype == LDAP_FILTER_NOT ) {
1870 return (Slapi_Filter *)f->f_list;
1874 #else /* LDAP_SLAPI */
1876 #endif /* LDAP_SLAPI */
1880 slapi_filter_list_next(
1882 Slapi_Filter *fprev )
1891 ftype = f->f_choice;
1892 if ( ftype == LDAP_FILTER_AND
1893 || ftype == LDAP_FILTER_OR
1894 || ftype == LDAP_FILTER_NOT )
1896 return fprev->f_next;
1900 #else /* LDAP_SLAPI */
1902 #endif /* LDAP_SLAPI */
1906 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1913 switch ( f->f_choice ) {
1914 case LDAP_FILTER_GE:
1915 case LDAP_FILTER_LE:
1916 case LDAP_FILTER_EQUALITY:
1917 case LDAP_FILTER_APPROX:
1918 *type = f->f_av_desc->ad_cname.bv_val;
1920 case LDAP_FILTER_SUBSTRINGS:
1921 *type = f->f_sub_desc->ad_cname.bv_val;
1923 case LDAP_FILTER_PRESENT:
1924 *type = f->f_desc->ad_cname.bv_val;
1926 case LDAP_FILTER_EXT:
1927 *type = f->f_mr_desc->ad_cname.bv_val;
1930 /* Complex filters need not apply. */
1938 #endif /* LDAP_SLAPI */
1942 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1943 char ***any, char **final )
1948 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1953 * The caller shouldn't free but we can't return an
1954 * array of char *s from an array of bervals without
1955 * allocating memory, so we may as well be consistent.
1958 *type = f->f_sub_desc->ad_cname.bv_val;
1959 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1960 if ( f->f_sub_any != NULL ) {
1961 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1963 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1964 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1965 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1971 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1976 #endif /* LDAP_SLAPI */
1980 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1983 Slapi_Filter *f = NULL;
1985 if ( ftype == LDAP_FILTER_AND ||
1986 ftype == LDAP_FILTER_OR ||
1987 ftype == LDAP_FILTER_NOT )
1989 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1990 f->f_choice = ftype;
1992 f->f_list->f_next = f2;
1999 #endif /* LDAP_SLAPI */
2003 slapi_x_filter_append( int ftype,
2004 Slapi_Filter **pContainingFilter, /* NULL on first call */
2005 Slapi_Filter **pNextFilter,
2006 Slapi_Filter *filterToAppend )
2009 if ( ftype == LDAP_FILTER_AND ||
2010 ftype == LDAP_FILTER_OR ||
2011 ftype == LDAP_FILTER_NOT )
2013 if ( *pContainingFilter == NULL ) {
2014 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
2015 (*pContainingFilter)->f_choice = ftype;
2016 (*pContainingFilter)->f_list = filterToAppend;
2017 (*pContainingFilter)->f_next = NULL;
2019 if ( (*pContainingFilter)->f_choice != ftype ) {
2023 (*pNextFilter)->f_next = filterToAppend;
2025 *pNextFilter = filterToAppend;
2029 #endif /* LDAP_SLAPI */
2034 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
2042 /* spec says return zero if no filter. */
2046 if ( verify_access ) {
2047 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
2049 return LDAP_PARAM_ERROR;
2055 * According to acl.c it is safe to call test_filter() with
2058 rc = test_filter( op, e, f );
2060 case LDAP_COMPARE_TRUE:
2063 case LDAP_COMPARE_FALSE:
2065 case SLAPD_COMPARE_UNDEFINED:
2068 case LDAP_PROTOCOL_ERROR:
2069 /* filter type unknown: spec says return -1 */
2077 #endif /* LDAP_SLAPI */
2081 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
2084 return slapi_filter_test( NULL, e, f, 0 );
2091 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
2094 switch ( f->f_choice ) {
2095 case LDAP_FILTER_AND:
2096 case LDAP_FILTER_NOT:
2097 case LDAP_FILTER_OR: {
2101 * FIXME: altering f; should we use a temporary?
2103 for ( f = f->f_list; f != NULL; f = f->f_next ) {
2104 rc = slapi_filter_apply( f, fn, arg, error_code );
2108 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
2114 case LDAP_FILTER_EQUALITY:
2115 case LDAP_FILTER_SUBSTRINGS:
2116 case LDAP_FILTER_GE:
2117 case LDAP_FILTER_LE:
2118 case LDAP_FILTER_PRESENT:
2119 case LDAP_FILTER_APPROX:
2120 case LDAP_FILTER_EXT:
2121 *error_code = fn( f, arg );
2124 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2127 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
2128 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
2134 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2136 #endif /* LDAP_SLAPI */
2140 slapi_send_ldap_extended_response(
2145 struct berval *response )
2150 rs.sr_err = errornum;
2151 rs.sr_matched = NULL;
2155 rs.sr_rspoid = respName;
2156 rs.sr_rspdata = response;
2158 send_ldap_extended( op, &rs );
2160 return LDAP_SUCCESS;
2161 #else /* LDAP_SLAPI */
2163 #endif /* LDAP_SLAPI */
2168 struct berval **vals,
2173 * FIXME: what's the point?
2176 #else /* LDAP_SLAPI */
2178 #endif /* LDAP_SLAPI */
2181 #define MAX_HOSTNAME 512
2184 slapi_get_hostname( void )
2188 static int been_here = 0;
2189 static char *static_hn = NULL;
2191 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
2193 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
2194 if ( static_hn == NULL) {
2195 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
2196 "Cannot allocate memory for hostname\n" );
2198 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2203 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
2204 slapi_log_error( SLAPI_LOG_FATAL,
2206 "can't get hostname\n" );
2207 slapi_ch_free( (void **)&static_hn );
2209 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2218 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2220 hn = ch_strdup( static_hn );
2223 #else /* LDAP_SLAPI */
2225 #endif /* LDAP_SLAPI */
2229 * FIXME: this should go in an appropriate header ...
2231 extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist );
2241 int rc = LDAP_SUCCESS;
2244 va_start( arglist, fmt );
2245 rc = slapi_int_log_error( severity, subsystem, fmt, arglist );
2249 #else /* LDAP_SLAPI */
2251 #endif /* LDAP_SLAPI */
2256 slapi_timer_current_time( void )
2259 static int first_time = 1;
2260 #if !defined (_WIN32)
2264 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
2267 gettimeofday( &base_time, NULL );
2269 gettimeofday( &now, NULL );
2270 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
2271 (now.tv_usec - base_time.tv_usec);
2272 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
2278 return (slap_get_time() - starttime) * 1000000;
2285 performance_counter_present = QueryPerformanceCounter( &base_time );
2286 QueryPerformanceFrequency( &performance_freq );
2289 if ( !performance_counter_present )
2292 QueryPerformanceCounter( &now );
2293 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2295 #else /* LDAP_SLAPI */
2297 #endif /* LDAP_SLAPI */
2304 slapi_timer_get_time( char *label )
2307 unsigned long start = slapi_timer_current_time();
2308 printf("%10ld %10d usec %s\n", start, 0, label);
2310 #else /* LDAP_SLAPI */
2312 #endif /* LDAP_SLAPI */
2319 slapi_timer_elapsed_time(
2321 unsigned long start )
2324 unsigned long stop = slapi_timer_current_time();
2325 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2326 #endif /* LDAP_SLAPI */
2330 slapi_free_search_results_internal( Slapi_PBlock *pb )
2333 Slapi_Entry **entries;
2334 int k = 0, nEnt = 0;
2336 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2337 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2342 if ( entries == NULL ) {
2346 for ( k = 0; k < nEnt; k++ ) {
2347 slapi_entry_free( entries[k] );
2350 slapi_ch_free( (void **)&entries );
2351 #endif /* LDAP_SLAPI */
2356 * Internal API to prime a Slapi_PBlock with a Backend.
2358 static int slapi_int_pblock_set_backend( Slapi_PBlock *pb, Backend *be )
2362 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2363 if ( rc != LDAP_SUCCESS )
2367 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2368 if ( rc != LDAP_SUCCESS )
2372 return LDAP_SUCCESS;
2376 * If oldStyle is TRUE, then a value suitable for setting to
2377 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
2378 * (pointer to static storage).
2380 * If oldStyle is FALSE, then a value suitable for setting to
2381 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2382 * a pointer to allocated memory and will include the SASL
2383 * mechanism (if any).
2385 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2390 switch ( authz->sai_method ) {
2391 case LDAP_AUTH_SASL:
2393 authType = SLAPD_AUTH_SASL;
2395 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2396 authType = slapi_ch_malloc( len );
2397 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2400 case LDAP_AUTH_SIMPLE:
2401 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2403 case LDAP_AUTH_NONE:
2404 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2410 if ( is_tls && authType == NULL ) {
2411 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2418 * Internal API to prime a Slapi_PBlock with a Connection.
2420 static int slapi_int_pblock_set_connection( Slapi_PBlock *pb, Connection *conn )
2425 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2426 if ( rc != LDAP_SUCCESS )
2429 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2430 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2431 if ( rc != LDAP_SUCCESS )
2433 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2434 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2435 if ( rc != LDAP_SUCCESS )
2439 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2440 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2441 if ( rc != LDAP_SUCCESS )
2443 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2444 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2445 if ( rc != LDAP_SUCCESS )
2449 #ifdef LDAP_CONNECTIONLESS
2450 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2451 if ( rc != LDAP_SUCCESS )
2455 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2456 if ( rc != LDAP_SUCCESS )
2459 /* Returns pointer to static string */
2460 connAuthType = Authorization2AuthType( &conn->c_authz,
2467 if ( connAuthType != NULL ) {
2468 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2469 if ( rc != LDAP_SUCCESS )
2473 /* Returns pointer to allocated string */
2474 connAuthType = Authorization2AuthType( &conn->c_authz,
2481 if ( connAuthType != NULL ) {
2482 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2483 /* slapi_pblock_set dups this itself */
2484 slapi_ch_free( (void **)&connAuthType );
2485 if ( rc != LDAP_SUCCESS )
2489 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
2490 /* slapi_pblock_set dups this itself */
2491 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)conn->c_authz.sai_dn.bv_val);
2492 if ( rc != LDAP_SUCCESS )
2496 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SSF, (void *)conn->c_ssf);
2497 if ( rc != LDAP_SUCCESS )
2500 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SASL_CONTEXT,
2501 ( conn->c_sasl_authctx != NULL ? conn->c_sasl_authctx :
2502 conn->c_sasl_sockctx ) );
2503 if ( rc != LDAP_SUCCESS )
2508 #endif /* LDAP_SLAPI */
2511 * Internal API to prime a Slapi_PBlock with an Operation.
2513 int slapi_int_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
2521 if ( op->o_bd != NULL ) {
2522 isRoot = be_isroot( op );
2523 isUpdateDn = be_isupdate( op );
2526 rc = slapi_int_pblock_set_backend( pb, op->o_bd );
2527 if ( rc != LDAP_SUCCESS )
2530 rc = slapi_int_pblock_set_connection( pb, op->o_conn );
2531 if ( rc != LDAP_SUCCESS )
2534 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2535 if ( rc != LDAP_SUCCESS )
2538 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2539 if ( rc != LDAP_SUCCESS )
2542 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2543 if ( rc != LDAP_SUCCESS )
2546 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2547 if ( rc != LDAP_SUCCESS )
2550 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2551 if ( rc != LDAP_SUCCESS )
2554 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2555 if ( rc != LDAP_SUCCESS )
2558 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2559 if ( rc != LDAP_SUCCESS)
2562 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2563 if ( rc != LDAP_SUCCESS )
2566 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2567 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
2568 /* Not quite sure what the point of this is. */
2569 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
2570 if ( rc != LDAP_SUCCESS )
2574 return LDAP_SUCCESS;
2580 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2585 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2587 *isSSL = conn->c_is_tls;
2592 return LDAP_SUCCESS;
2595 #endif /* LDAP_SLAPI */
2599 * DS 5.x compatability API follow
2602 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2608 return LDAP_PARAM_ERROR;
2610 at = attr->a_desc->ad_type;
2612 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2614 if ( is_at_single_value( at ) )
2615 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2616 if ( is_at_operational( at ) )
2617 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2618 if ( is_at_obsolete( at ) )
2619 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2620 if ( is_at_collective( at ) )
2621 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2622 if ( is_at_no_user_mod( at ) )
2623 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2625 return LDAP_SUCCESS;
2628 #endif /* LDAP_SLAPI */
2631 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2634 unsigned long flags;
2636 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2638 return (flags & flag) ? 1 : 0;
2641 #endif /* LDAP_SLAPI */
2644 Slapi_Attr *slapi_attr_new( void )
2649 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2657 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2661 AttributeDescription *ad = NULL;
2663 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2679 void slapi_attr_free( Slapi_Attr **a )
2687 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2690 return attr_dup( (Slapi_Attr *)attr );
2696 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2700 struct berval *nvalp;
2702 AttributeDescription *desc = a->a_desc;
2704 if ( desc->ad_type->sat_equality &&
2705 desc->ad_type->sat_equality->smr_normalize ) {
2706 rc = (*desc->ad_type->sat_equality->smr_normalize)(
2707 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2708 desc->ad_type->sat_syntax,
2709 desc->ad_type->sat_equality,
2710 (Slapi_Value *)v, &nval, NULL );
2711 if ( rc != LDAP_SUCCESS ) {
2719 rc = value_add_one( &a->a_vals, (Slapi_Value *)v );
2720 if ( rc == 0 && nvalp != NULL ) {
2721 rc = value_add_one( &a->a_nvals, nvalp );
2723 a->a_nvals = a->a_vals;
2726 if ( nvalp != NULL ) {
2727 slapi_ch_free_string( &nval.bv_val );
2736 int slapi_attr_type2plugin( const char *type, void **pi )
2743 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2746 if ( attr == NULL ) {
2747 return LDAP_PARAM_ERROR;
2750 *type = attr->a_desc->ad_cname.bv_val;
2752 return LDAP_SUCCESS;
2758 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2761 if ( attr == NULL ) {
2762 return LDAP_PARAM_ERROR;
2764 *oidp = attr->a_desc->ad_type->sat_oid;
2766 return LDAP_SUCCESS;
2772 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2780 mr = a->a_desc->ad_type->sat_equality;
2781 rc = value_match( &ret, a->a_desc, mr,
2782 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2783 (struct berval *)v1, (void *)v2, &text );
2784 if ( rc != LDAP_SUCCESS )
2787 return ( ret == 0 ) ? 0 : -1;
2793 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2803 if ( a ->a_vals == NULL ) {
2806 mr = a->a_desc->ad_type->sat_equality;
2807 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2808 rc = value_match( &ret, a->a_desc, mr,
2809 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2810 if ( rc != LDAP_SUCCESS ) {
2817 #endif /* LDAP_SLAPI */
2821 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2824 AttributeDescription *a1 = NULL;
2825 AttributeDescription *a2 = NULL;
2829 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2833 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2837 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2838 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2839 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2842 case SLAPI_TYPE_CMP_EXACT:
2843 ret = ad_cmp( a1, a2 );
2845 case SLAPI_TYPE_CMP_BASE:
2846 ret = ad_base_cmp( a1, a2 );
2848 case SLAPI_TYPE_CMP_SUBTYPE:
2849 ret = is_ad_subtype( a2, a2 );
2862 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2865 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2871 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2874 return slapi_valueset_first_value( &a->a_vals, v );
2880 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2883 return slapi_valueset_next_value( &a->a_vals, hint, v );
2889 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2892 *numValues = slapi_valueset_count( &a->a_vals );
2900 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2903 *vs = &((Slapi_Attr *)a)->a_vals;
2911 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2914 return slapi_attr_get_values( a, vals );
2920 char *slapi_attr_syntax_normalize( const char *s )
2923 AttributeDescription *ad = NULL;
2926 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2930 return ad->ad_cname.bv_val;
2936 Slapi_Value *slapi_value_new( void )
2941 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2949 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2952 return ber_dupbv( NULL, (struct berval *)bval );
2958 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2961 return slapi_value_new_berval( v );
2967 Slapi_Value *slapi_value_new_string(const char *s)
2972 bv.bv_val = (char *)s;
2973 bv.bv_len = strlen( s );
2975 return slapi_value_new_berval( &bv );
2981 Slapi_Value *slapi_value_init(Slapi_Value *val)
2993 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2996 return ber_dupbv( v, bval );
3002 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
3005 v->bv_val = slapi_ch_strdup( (char *)s );
3006 v->bv_len = strlen( s );
3014 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
3017 return slapi_value_new_value( v );
3023 void slapi_value_free(Slapi_Value **value)
3026 if ( value == NULL ) {
3030 if ( (*value) != NULL ) {
3031 slapi_ch_free( (void **)&(*value)->bv_val );
3032 slapi_ch_free( (void **)value );
3037 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
3046 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
3049 if ( value == NULL ) {
3052 if ( value->bv_val != NULL ) {
3053 slapi_ch_free( (void **)&value->bv_val );
3055 slapi_value_init_berval( value, (struct berval *)bval );
3063 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
3066 if ( value == NULL ) {
3069 return slapi_value_set_berval( value, vfrom );
3075 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
3078 if ( value == NULL ) {
3081 if ( value->bv_val != NULL ) {
3082 slapi_ch_free( (void **)&value->bv_val );
3084 value->bv_val = slapi_ch_malloc( len );
3085 value->bv_len = len;
3086 AC_MEMCPY( value->bv_val, val, len );
3094 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
3097 if ( value == NULL ) {
3100 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
3107 int slapi_value_set_int(Slapi_Value *value, int intVal)
3112 snprintf( buf, sizeof( buf ), "%d", intVal );
3114 return slapi_value_set_string( value, buf );
3120 const char *slapi_value_get_string(const Slapi_Value *value)
3123 if ( value == NULL ) return NULL;
3124 if ( value->bv_val == NULL ) return NULL;
3125 if ( !checkBVString( value ) ) return NULL;
3127 return value->bv_val;
3133 int slapi_value_get_int(const Slapi_Value *value)
3136 if ( value == NULL ) return 0;
3137 if ( value->bv_val == NULL ) return 0;
3138 if ( !checkBVString( value ) ) return 0;
3140 return (int)strtol( value->bv_val, NULL, 10 );
3146 unsigned int slapi_value_get_uint(const Slapi_Value *value)
3149 if ( value == NULL ) return 0;
3150 if ( value->bv_val == NULL ) return 0;
3151 if ( !checkBVString( value ) ) return 0;
3153 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
3159 long slapi_value_get_long(const Slapi_Value *value)
3162 if ( value == NULL ) return 0;
3163 if ( value->bv_val == NULL ) return 0;
3164 if ( !checkBVString( value ) ) return 0;
3166 return strtol( value->bv_val, NULL, 10 );
3172 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
3175 if ( value == NULL ) return 0;
3176 if ( value->bv_val == NULL ) return 0;
3177 if ( !checkBVString( value ) ) return 0;
3179 return strtoul( value->bv_val, NULL, 10 );
3185 size_t slapi_value_get_length(const Slapi_Value *value)
3188 if ( value == NULL )
3191 return (size_t) value->bv_len;
3197 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
3200 return slapi_attr_value_cmp( a, v1, v2 );
3206 /* A ValueSet is a container for a BerVarray. */
3207 Slapi_ValueSet *slapi_valueset_new( void )
3212 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
3221 void slapi_valueset_free(Slapi_ValueSet *vs)
3227 ber_bvarray_free( vp );
3228 slapi_ch_free( (void **)&vp );
3235 void slapi_valueset_init(Slapi_ValueSet *vs)
3238 if ( vs != NULL && *vs == NULL ) {
3239 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
3240 (*vs)->bv_val = NULL;
3246 void slapi_valueset_done(Slapi_ValueSet *vs)
3254 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
3256 slapi_ch_free( (void **)&vp->bv_val );
3258 /* but don't free *vs or vs */
3262 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
3267 ber_dupbv( &bv, (Slapi_Value *)addval );
3268 ber_bvarray_add( vs, &bv );
3272 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
3275 return slapi_valueset_next_value( vs, 0, v );
3281 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
3292 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
3303 int slapi_valueset_count( const Slapi_ValueSet *vs )
3314 for ( i = 0; vp[i].bv_val != NULL; i++ )
3324 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
3329 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
3330 slapi_valueset_add_value( vs1, vp );
3335 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
3336 struct berval *val, int access )
3343 slap_access_t slap_access;
3344 AttributeDescription *ad = NULL;
3347 ret = slap_str2ad( attr, &ad, &text );
3348 if ( ret != LDAP_SUCCESS ) {
3352 switch ( access & SLAPI_ACL_ALL ) {
3353 case SLAPI_ACL_COMPARE:
3354 slap_access = ACL_COMPARE;
3356 case SLAPI_ACL_SEARCH:
3357 slap_access = ACL_SEARCH;
3359 case SLAPI_ACL_READ:
3360 slap_access = ACL_READ;
3362 case SLAPI_ACL_WRITE:
3363 case SLAPI_ACL_DELETE:
3365 case SLAPI_ACL_SELF:
3366 /* FIXME: handle ACL_WADD/ACL_WDEL */
3367 slap_access = ACL_WRITE;
3370 return LDAP_INSUFFICIENT_ACCESS;
3374 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3375 return LDAP_PARAM_ERROR;
3378 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3379 return LDAP_PARAM_ERROR;
3382 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3383 return LDAP_PARAM_ERROR;
3386 ret = access_allowed( op, e, ad, val, slap_access, NULL );
3388 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3390 return LDAP_UNWILLING_TO_PERFORM;
3394 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
3398 int rc = LDAP_SUCCESS;
3399 Modifications *ml, *mp;
3401 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3402 return LDAP_PARAM_ERROR;
3405 ml = slapi_int_ldapmods2modifications( mods );
3410 for ( mp = ml; mp != NULL; mp = mp->sml_next ) {
3411 rc = slap_bv2ad( &mp->sml_type, &mp->sml_desc, (const char **)errbuf );
3412 if ( rc != LDAP_SUCCESS ) {
3417 if ( rc == LDAP_SUCCESS ) {
3418 rc = acl_check_modlist( op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3421 /* Careful when freeing the modlist because it has pointers into the mods array. */
3422 for ( ; ml != NULL; ml = mp ) {
3425 /* just free the containing array */
3426 slapi_ch_free( (void **)&ml->sml_values );
3427 slapi_ch_free( (void **)&ml );
3432 return LDAP_UNWILLING_TO_PERFORM;
3437 * Synthesise an LDAPMod array from a Modifications list to pass
3438 * to SLAPI. This synthesis is destructive and as such the
3439 * Modifications list may not be used after calling this
3442 * This function must also be called before slap_mods_check().
3444 LDAPMod **slapi_int_modifications2ldapmods(Modifications **pmodlist)
3447 Modifications *ml, *modlist;
3448 LDAPMod **mods, *modp;
3451 modlist = *pmodlist;
3453 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3456 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3458 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3459 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3461 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3463 /* Take ownership of original type. */
3464 modp->mod_type = ml->sml_type.bv_val;
3465 ml->sml_type.bv_val = NULL;
3467 if ( ml->sml_values != NULL ) {
3468 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
3470 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3471 sizeof(struct berval *) );
3472 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
3473 /* Take ownership of original values. */
3474 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3475 modp->mod_bvalues[j]->bv_len = ml->sml_values[j].bv_len;
3476 modp->mod_bvalues[j]->bv_val = ml->sml_values[j].bv_val;
3477 ml->sml_values[j].bv_len = 0;
3478 ml->sml_values[j].bv_val = NULL;
3480 modp->mod_bvalues[j] = NULL;
3482 modp->mod_bvalues = NULL;
3489 slap_mods_free( modlist );
3499 * Convert a potentially modified array of LDAPMods back to a
3500 * Modification list.
3502 * The returned Modification list contains pointers into the
3503 * LDAPMods array; the latter MUST be freed with
3504 * slapi_int_free_ldapmods() (see below).
3506 Modifications *slapi_int_ldapmods2modifications (LDAPMod **mods)
3509 Modifications *modlist = NULL, **modtail;
3512 if ( mods == NULL ) {
3518 for( modp = mods; *modp != NULL; modp++ ) {
3522 struct berval **bvp;
3524 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3525 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3527 mod->sml_type.bv_val = (*modp)->mod_type;
3528 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3529 mod->sml_desc = NULL;
3530 mod->sml_next = NULL;
3532 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3533 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3536 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3541 mod->sml_values = NULL;
3543 mod->sml_values = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3545 /* NB: This implicitly trusts a plugin to return valid modifications. */
3546 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3547 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3548 mod->sml_values[i].bv_val = (*bvp)->bv_val;
3549 mod->sml_values[i].bv_len = (*bvp)->bv_len;
3552 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3553 mod->sml_values[i].bv_val = *p;
3554 mod->sml_values[i].bv_len = strlen( *p );
3557 mod->sml_values[i].bv_val = NULL;
3558 mod->sml_values[i].bv_len = 0;
3560 mod->sml_nvalues = NULL;
3563 modtail = &mod->sml_next;
3573 * This function only frees the parts of the mods array that
3574 * are not shared with the Modification list that was created
3575 * by slapi_int_ldapmods2modifications().
3578 void slapi_int_free_ldapmods (LDAPMod **mods)
3586 for ( i = 0; mods[i] != NULL; i++ ) {
3588 * Don't free values themselves; they're owned by the
3589 * Modification list. Do free the containing array.
3591 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3592 for ( j = 0; mods[i]->mod_values != NULL && mods[i]->mod_values[j] != NULL; j++ ) {
3593 ch_free( mods[i]->mod_values[j] );
3595 ch_free( mods[i]->mod_values );
3597 ch_free( mods[i]->mod_values );
3599 /* Don't free type, for same reasons. */
3603 #endif /* LDAP_SLAPI */
3607 * Sun ONE DS 5.x computed attribute support. Computed attributes
3608 * allow for dynamically generated operational attributes, a very
3609 * useful thing indeed.
3613 * Write the computed attribute to a BerElement. Complementary
3614 * functions need to be defined for anything that replaces
3615 * op->o_callback->sc_sendentry, if you wish to make computed
3616 * attributes available to it.
3618 int slapi_int_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
3621 Operation *op = NULL;
3623 AttributeDescription *desc = NULL;
3639 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
3640 if ( rc != 0 || op == NULL ) {
3644 ber = (BerElement *)c->cac_private;
3647 if ( c->cac_attrs == NULL ) {
3648 /* All attrs request, skip operational attributes */
3649 if ( is_at_operational( desc->ad_type ) ) {
3653 /* Specific attrs requested */
3654 if ( is_at_operational( desc->ad_type ) ) {
3655 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3659 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3665 if ( !access_allowed( op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
3666 slapi_log_error( SLAPI_LOG_ACL, "slapi_int_compute_output_ber",
3667 "acl: access to attribute %s not allowed\n",
3668 desc->ad_cname.bv_val );
3672 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
3674 slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
3675 "ber_printf failed\n");
3679 if ( !c->cac_attrsonly && a->a_vals != NULL ) {
3680 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
3681 if ( !access_allowed( op, e,
3682 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
3683 slapi_log_error( SLAPI_LOG_ACL, "slapi_int_compute_output_ber",
3685 "acl: access to %s, value %d not allowed\n",
3686 op->o_connid, desc->ad_cname.bv_val, i );
3690 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
3691 slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
3692 "ber_printf failed\n");
3698 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
3699 slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
3700 "ber_printf failed\n" );
3711 * For some reason Sun don't use the normal plugin mechanism
3712 * registration path to register an "evaluator" function (an
3713 * "evaluator" is responsible for adding computed attributes;
3714 * the nomenclature is somewhat confusing).
3716 * As such slapi_compute_add_evaluator() registers the
3717 * function directly.
3719 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3722 Slapi_PBlock *pPlugin = NULL;
3725 pPlugin = slapi_pblock_new();
3726 if ( pPlugin == NULL ) {
3727 rc = LDAP_NO_MEMORY;
3731 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3732 if ( rc != LDAP_SUCCESS ) {
3736 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3737 if ( rc != LDAP_SUCCESS ) {
3741 rc = slapi_int_register_plugin( NULL, pPlugin );
3748 if ( rc != LDAP_SUCCESS ) {
3749 if ( pPlugin != NULL ) {
3750 slapi_pblock_destroy( pPlugin );
3758 #endif /* LDAP_SLAPI */
3762 * See notes above regarding slapi_compute_add_evaluator().
3764 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3767 Slapi_PBlock *pPlugin = NULL;
3770 pPlugin = slapi_pblock_new();
3771 if ( pPlugin == NULL ) {
3772 rc = LDAP_NO_MEMORY;
3776 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3777 if ( rc != LDAP_SUCCESS ) {
3781 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3782 if ( rc != LDAP_SUCCESS ) {
3786 rc = slapi_int_register_plugin( NULL, pPlugin );
3793 if ( rc != LDAP_SUCCESS ) {
3794 if ( pPlugin != NULL ) {
3795 slapi_pblock_destroy( pPlugin );
3803 #endif /* LDAP_SLAPI */
3807 * Call compute evaluators
3809 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3813 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3815 rc = slapi_int_get_plugins( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3816 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3817 /* Nothing to do; front-end should ignore. */
3821 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3823 * -1: no attribute matched requested type
3824 * 0: one attribute matched
3825 * >0: error happened
3827 rc = (*pGetPlugin)( c, type, e, outputfn );
3833 slapi_ch_free( (void **)&tmpPlugin );
3838 #endif /* LDAP_SLAPI */
3841 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3847 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3852 return slapi_int_call_plugins( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3855 #endif /* LDAP_SLAPI */
3859 * New API to provide the plugin with access to the search
3860 * pblock. Have informed Sun DS team.
3862 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3868 if ( c->cac_pb == NULL )
3876 #endif /* LDAP_SLAPI */
3879 Slapi_Mutex *slapi_new_mutex( void )
3884 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3885 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3886 slapi_ch_free( (void **)&m );
3896 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3899 if ( mutex != NULL ) {
3900 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3901 slapi_ch_free( (void **)&mutex);
3906 void slapi_lock_mutex( Slapi_Mutex *mutex )
3909 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3913 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3916 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3922 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3927 if ( mutex == NULL ) {
3931 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3932 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3933 slapi_ch_free( (void **)&cv );
3937 /* XXX struct copy */
3938 cv->mutex = mutex->mutex;
3946 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3949 if ( cvar != NULL ) {
3950 ldap_pvt_thread_cond_destroy( &cvar->cond );
3951 slapi_ch_free( (void **)&cvar );
3956 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3959 if ( cvar == NULL ) {
3963 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3969 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3972 if ( cvar == NULL ) {
3977 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3980 return ldap_pvt_thread_cond_signal( &cvar->cond );
3986 int slapi_int_access_allowed( Operation *op,
3988 AttributeDescription *desc,
3990 slap_access_t access,
3991 AccessControlState *state )
3994 int rc, slap_access = 0;
3995 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3997 if ( op->o_pb == NULL ) {
3998 /* internal operation */
4004 /* FIXME: handle ACL_WADD/ACL_WDEL */
4005 slap_access |= SLAPI_ACL_ADD | SLAPI_ACL_DELETE | SLAPI_ACL_WRITE;
4008 slap_access |= SLAPI_ACL_READ;
4011 slap_access |= SLAPI_ACL_SEARCH;
4014 slap_access = ACL_COMPARE;
4020 rc = slapi_int_get_plugins( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
4021 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
4022 /* nothing to do; allowed access */
4026 slapi_int_pblock_set_operation( op->o_pb, op );
4028 rc = 1; /* default allow policy */
4030 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
4035 rc = (*pGetPlugin)( op->o_pb, entry, desc->ad_cname.bv_val,
4036 val, slap_access, (void *)state );
4042 slapi_ch_free( (void **)&tmpPlugin );
4047 #endif /* LDAP_SLAPI */
4051 * There is no documentation for this.
4053 int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
4066 rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
4067 if ( rc != LDAP_SUCCESS ) {
4071 if ( lrdn[1] != NULL ) {
4072 return -1; /* not single valued */
4077 *type = slapi_ch_strdup( ava->la_attr.bv_val );
4078 ber_dupbv( bv, &ava->la_value );
4085 #endif /* LDAP_SLAPI */
4088 char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
4091 struct berval new_dn, parent_dn, newrdn;
4093 new_dn.bv_val = NULL;
4095 parent_dn.bv_val = (char *)dn;
4096 parent_dn.bv_len = strlen( dn );
4098 newrdn.bv_val = (char *)rdn;
4099 newrdn.bv_len = strlen( rdn );
4101 build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
4103 return new_dn.bv_val;
4106 #endif /* LDAP_SLAPI */
4109 int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
4114 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
4115 size_t textlen = sizeof textbuf;
4118 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
4121 rc = entry_schema_check( be, e, NULL, 0,
4122 &text, textbuf, textlen );
4124 return ( rc == LDAP_SUCCESS ) ? 0 : 1;
4127 #endif /* LDAP_SLAPI */
4130 int slapi_entry_rdn_values_present( const Slapi_Entry *e )
4135 int i = 0, match = 0;
4137 rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
4138 if ( rc != LDAP_SUCCESS ) {
4142 if ( dn[0] != NULL ) {
4143 LDAPRDN rdn = dn[0];
4145 for ( i = 0; rdn[i] != NULL; i++ ) {
4146 LDAPAVA *ava = &rdn[0][i];
4147 Slapi_Attr *a = NULL;
4149 if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
4150 slapi_attr_value_find( a, &ava->la_value ) == 0 )
4157 return ( i == match );
4160 #endif /* LDAP_SLAPI */
4163 int slapi_entry_add_rdn_values( Slapi_Entry *e )
4169 rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
4170 if ( rc != LDAP_SUCCESS ) {
4174 if ( dn[0] != NULL ) {
4175 LDAPRDN rdn = dn[0];
4176 struct berval *vals[2];
4178 for ( i = 0; rdn[i] != NULL; i++ ) {
4179 LDAPAVA *ava = &rdn[0][i];
4180 Slapi_Attr *a = NULL;
4182 if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
4183 slapi_attr_value_find( a, &ava->la_value ) == 0 )
4186 vals[0] = &ava->la_value;
4189 slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
4195 return LDAP_SUCCESS;
4198 #endif /* LDAP_SLAPI */
4201 const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
4206 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
4207 if ( attr == NULL ) {
4211 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
4212 return slapi_value_get_string( &attr->a_vals[0] );
4214 #endif /* LDAP_SLAPI */
4219 void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
4224 attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
4226 bv.bv_val = uniqueid;
4227 bv.bv_len = strlen( uniqueid );
4228 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
4229 #endif /* LDAP_SLAPI */
4232 LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
4240 size = sizeof("ldap:///");
4243 size += strlen( ldaphost );
4244 if ( ldapport != 0 )
4247 url = slapi_ch_malloc( size );
4249 if ( ldapport != 0 ) {
4250 sprintf( url, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
4252 sprintf( url, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
4255 rc = ldap_initialize( &ld, url );
4257 slapi_ch_free_string( &url );
4259 return ( rc == LDAP_SUCCESS ) ? ld : NULL;
4262 #endif /* LDAP_SLAPI */
4265 void slapi_ldap_unbind( LDAP *ld )
4269 #endif /* LDAP_SLAPI */
4272 int slapi_x_backend_get_flags( const Slapi_Backend *be, unsigned long *flags )
4276 return LDAP_PARAM_ERROR;
4278 *flags = SLAP_DBFLAGS(be);
4280 return LDAP_SUCCESS;
4283 #endif /* LDAP_SLAPI */