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;
540 SlapReply rs = { REP_RESULT };
542 pb = slapi_pblock_new();
544 slapi_pblock_set( pb, SLAPI_TARGET_DN, slapi_entry_get_dn((Entry *)e) );
546 rc = slapi_int_init_connection( pb, &rs, LDAP_REQ_SEARCH, &conn );
547 if ( rc != LDAP_SUCCESS ) {
548 slapi_pblock_destroy( pb );
552 op->o_bd->be_has_subordinates( op, (Entry *)e, &hasSubordinates );
554 slapi_int_connection_destroy( &conn );
555 slapi_pblock_destroy( pb );
557 return ( hasSubordinates == LDAP_COMPARE_TRUE );
564 * Return approximate size of the entry rounded to the nearest
565 * 1K. Only the size of the attribute values are counted in the
566 * Sun implementation.
568 * http://docs.sun.com/source/816-6701-10/funcref.html#1017388
570 size_t slapi_entry_size(Slapi_Entry *e)
577 for ( size = 0, a = e->e_attrs; a != NULL; a = a->a_next ) {
578 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
579 size += a->a_vals[i].bv_len + 1;
584 size -= (size % 1024);
589 #endif /* LDAP_SLAPI */
593 * Add values to entry.
596 * LDAP_SUCCESS Values added to entry
597 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
598 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
601 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
607 char textbuf[SLAP_TEXT_BUFLEN];
609 mod.sm_op = LDAP_MOD_ADD;
612 mod.sm_type.bv_val = (char *)type;
613 mod.sm_type.bv_len = strlen( type );
615 rc = slap_str2ad( type, &mod.sm_desc, &text );
616 if ( rc != LDAP_SUCCESS ) {
620 if ( vals == NULL ) {
621 /* Apparently vals can be NULL
622 * FIXME: sm_values = NULL ? */
623 mod.sm_values = (BerVarray)ch_malloc( sizeof(struct berval) );
624 mod.sm_values->bv_val = NULL;
627 rc = bvptr2obj( vals, &mod.sm_values );
628 if ( rc != LDAP_SUCCESS ) {
629 return LDAP_CONSTRAINT_VIOLATION;
632 mod.sm_nvalues = NULL;
634 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
636 ch_free( mod.sm_values );
638 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
641 #endif /* LDAP_SLAPI */
645 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
648 return slapi_entry_add_values( e, type, vals );
651 #endif /* LDAP_SLAPI */
655 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
658 AttributeDescription *ad = NULL;
662 rc = slap_str2ad( type, &ad, &text );
663 if ( rc != LDAP_SUCCESS ) {
667 return attr_merge_normalize( e, ad, *vs, NULL );
670 #endif /* LDAP_SLAPI */
674 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
680 char textbuf[SLAP_TEXT_BUFLEN];
682 mod.sm_op = LDAP_MOD_DELETE;
685 mod.sm_type.bv_val = (char *)type;
686 mod.sm_type.bv_len = strlen( type );
688 if ( vals == NULL ) {
689 /* If vals is NULL, this is a NOOP. */
693 rc = slap_str2ad( type, &mod.sm_desc, &text );
694 if ( rc != LDAP_SUCCESS ) {
698 if ( vals[0] == NULL ) {
699 /* SLAPI doco says LDAP_OPERATIONS_ERROR but LDAP_OTHER is better */
700 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
703 rc = bvptr2obj( vals, &mod.sm_values );
704 if ( rc != LDAP_SUCCESS ) {
705 return LDAP_CONSTRAINT_VIOLATION;
707 mod.sm_nvalues = NULL;
709 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
711 ch_free( mod.sm_values );
716 #endif /* LDAP_SLAPI */
720 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
723 return slapi_entry_delete_values( e, type, vals );
726 #endif /* LDAP_SLAPI */
730 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
733 return slapi_entry_attr_merge( e, (char *)type, vals );
736 #endif /* LDAP_SLAPI */
740 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
743 AttributeDescription *ad = NULL;
747 rc = slap_str2ad( type, &ad, &text );
748 if ( rc != LDAP_SUCCESS ) {
752 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
753 if ( rc != LDAP_SUCCESS ) {
760 #endif /* LDAP_SLAPI */
764 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
769 val.bv_val = (char *)value;
770 val.bv_len = strlen( value );
772 return slapi_entry_add_value( e, type, &val );
775 #endif /* LDAP_SLAPI */
779 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
782 Slapi_Value *vals[2];
785 val.bv_val = (char *)value;
786 val.bv_len = strlen( value );
790 return slapi_entry_delete_values_sv( e, type, vals );
793 #endif /* LDAP_SLAPI */
798 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
801 return slapi_entry_attr_merge( e, (char *)type, vals );
808 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
817 return ( *attr != NULL ) ? 0 : -1;
824 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
831 if ( prevattr == NULL ) {
835 *attr = prevattr->a_next;
837 return ( *attr != NULL ) ? 0 : -1;
844 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
847 AttributeDescription *ad = NULL;
852 rc = slap_str2ad( type, &ad, &text );
853 if ( rc != LDAP_SUCCESS ) {
857 attr_delete( &e->e_attrs, ad );
859 rc = bvptr2obj( vals, &bv );
860 if ( rc != LDAP_SUCCESS ) {
864 rc = attr_merge_normalize( e, ad, bv, NULL );
865 slapi_ch_free( (void **)&bv );
866 if ( rc != LDAP_SUCCESS ) {
873 #endif /* LDAP_SLAPI */
877 * FIXME -- The caller must free the allocated memory.
878 * In Netscape they do not have to.
881 slapi_attr_get_values(
883 struct berval ***vals )
889 if ( attr == NULL ) {
893 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
897 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
898 for ( j = 0; j < i; j++ ) {
899 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
903 *vals = (struct berval **)bv;
906 #else /* LDAP_SLAPI */
908 #endif /* LDAP_SLAPI */
912 slapi_dn_normalize( char *dn )
918 assert( dn != NULL );
921 bdn.bv_len = strlen( dn );
923 if ( dnPretty( NULL, &bdn, &pdn, NULL ) != LDAP_SUCCESS ) {
928 #else /* LDAP_SLAPI */
930 #endif /* LDAP_SLAPI */
934 slapi_dn_normalize_case( char *dn )
940 assert( dn != NULL );
943 bdn.bv_len = strlen( dn );
945 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
950 #else /* LDAP_SLAPI */
952 #endif /* LDAP_SLAPI */
961 struct berval bdn, ndn;
962 struct berval bsuffix, nsuffix;
965 assert( dn != NULL );
966 assert( suffix != NULL );
969 bdn.bv_len = strlen( dn );
971 bsuffix.bv_val = suffix;
972 bsuffix.bv_len = strlen( suffix );
974 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
978 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
981 slapi_ch_free( (void **)&ndn.bv_val );
985 rc = dnIsSuffix( &ndn, &nsuffix );
987 slapi_ch_free( (void **)&ndn.bv_val );
988 slapi_ch_free( (void **)&nsuffix.bv_val );
991 #else /* LDAP_SLAPI */
993 #endif /* LDAP_SLAPI */
998 const char *parentdn,
999 const char *childdn )
1002 struct berval assertedParentDN, normalizedAssertedParentDN;
1003 struct berval childDN, normalizedChildDN;
1004 struct berval normalizedParentDN;
1007 assert( parentdn != NULL );
1008 assert( childdn != NULL );
1010 assertedParentDN.bv_val = (char *)parentdn;
1011 assertedParentDN.bv_len = strlen( parentdn );
1013 if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
1014 &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
1019 childDN.bv_val = (char *)childdn;
1020 childDN.bv_len = strlen( childdn );
1022 if ( dnNormalize( 0, NULL, NULL, &childDN,
1023 &normalizedChildDN, NULL ) != LDAP_SUCCESS )
1025 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
1029 dnParent( &normalizedChildDN, &normalizedParentDN );
1031 if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
1032 &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
1037 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
1038 slapi_ch_free( (void **)&normalizedChildDN.bv_val );
1040 return ( match == 0 );
1043 #endif /* LDAP_SLAPI */
1047 * Returns DN of the parent entry, or NULL if the DN is
1048 * an empty string or NULL, or has no parent.
1051 slapi_dn_parent( const char *_dn )
1054 struct berval dn, prettyDN;
1055 struct berval parentDN;
1057 if ( _dn == NULL ) {
1061 dn.bv_val = (char *)_dn;
1062 dn.bv_len = strlen( _dn );
1064 if ( dn.bv_len == 0 ) {
1068 if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
1072 dnParent( &prettyDN, &parentDN ); /* in-place */
1074 slapi_ch_free( (void **)&prettyDN.bv_val );
1076 if ( parentDN.bv_len == 0 ) {
1080 return slapi_ch_strdup( parentDN.bv_val );
1083 #endif /* LDAP_SLAPI */
1087 * Returns DN of the parent entry; or NULL if the DN is
1088 * an empty string, if the DN has no parent, or if the
1089 * DN is the suffix of the backend database
1091 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *_dn )
1095 struct berval dn, prettyDN;
1096 struct berval normalizedDN, parentDN;
1098 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
1101 dn.bv_val = (char *)_dn;
1102 dn.bv_len = strlen( _dn );
1104 if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
1108 if ( be != NULL && be_issuffix( be, &normalizedDN ) ) {
1109 slapi_ch_free( (void **)&prettyDN.bv_val );
1110 slapi_ch_free( (void **)&normalizedDN.bv_val );
1114 dnParent( &prettyDN, &parentDN );
1116 slapi_ch_free( (void **)&prettyDN.bv_val );
1117 slapi_ch_free( (void **)&normalizedDN.bv_val );
1119 if ( parentDN.bv_len == 0 ) {
1123 return slapi_ch_strdup( parentDN.bv_val );
1126 #endif /* LDAP_SLAPI */
1130 slapi_dn_ignore_case( char *dn )
1133 return slapi_dn_normalize_case( dn );
1134 #else /* LDAP_SLAPI */
1136 #endif /* LDAP_SLAPI */
1140 slapi_ch_malloc( unsigned long size )
1143 return ch_malloc( size );
1144 #else /* LDAP_SLAPI */
1146 #endif /* LDAP_SLAPI */
1150 slapi_ch_free( void **ptr )
1155 #endif /* LDAP_SLAPI */
1159 slapi_ch_free_string( char **ptr )
1162 slapi_ch_free( (void **)ptr );
1163 #endif /* LDAP_SLAPI */
1167 slapi_ch_array_free( char **arrayp )
1172 if ( arrayp != NULL ) {
1173 for ( p = arrayp; *p != NULL; p++ ) {
1174 slapi_ch_free( (void **)p );
1176 slapi_ch_free( (void **)&arrayp );
1182 slapi_ch_bvdup(const struct berval *v)
1187 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
1188 bv->bv_len = v->bv_len;
1189 bv->bv_val = slapi_ch_malloc( bv->bv_len );
1190 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1199 slapi_ch_bvecdup(const struct berval **v)
1209 for ( i = 0; v[i] != NULL; i++ )
1212 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1214 for ( i = 0; v[i] != NULL; i++ ) {
1215 rv[i] = slapi_ch_bvdup( v[i] );
1227 unsigned long nelem,
1228 unsigned long size )
1231 return ch_calloc( nelem, size );
1232 #else /* LDAP_SLAPI */
1234 #endif /* LDAP_SLAPI */
1240 unsigned long size )
1243 return ch_realloc( block, size );
1244 #else /* LDAP_SLAPI */
1246 #endif /* LDAP_SLAPI */
1250 slapi_ch_strdup( char *s )
1253 return ch_strdup( (const char *)s );
1254 #else /* LDAP_SLAPI */
1256 #endif /* LDAP_SLAPI */
1260 slapi_ch_stlen( char *s )
1263 return strlen( (const char *)s );
1264 #else /* LDAP_SLAPI */
1266 #endif /* LDAP_SLAPI */
1270 slapi_control_present(
1271 LDAPControl **controls,
1273 struct berval **val,
1288 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1289 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1294 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1296 * FIXME: according to 6.1 specification,
1297 * "The val output parameter is set
1298 * to point into the controls array.
1299 * A copy of the control value is
1303 struct berval *pTmpBval;
1305 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
1306 if ( pTmpBval == NULL ) {
1309 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
1310 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
1314 slapi_ch_free( (void **)&pTmpBval );
1320 *val = &controls[i]->ldctl_value;
1325 *iscritical = controls[i]->ldctl_iscritical;
1332 #else /* LDAP_SLAPI */
1334 #endif /* LDAP_SLAPI */
1339 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1340 unsigned long *slapi_mask)
1342 *slapi_mask = SLAPI_OPERATION_NONE;
1344 if ( slap_mask & SLAP_CTRL_ABANDON )
1345 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1347 if ( slap_mask & SLAP_CTRL_ADD )
1348 *slapi_mask |= SLAPI_OPERATION_ADD;
1350 if ( slap_mask & SLAP_CTRL_BIND )
1351 *slapi_mask |= SLAPI_OPERATION_BIND;
1353 if ( slap_mask & SLAP_CTRL_COMPARE )
1354 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1356 if ( slap_mask & SLAP_CTRL_DELETE )
1357 *slapi_mask |= SLAPI_OPERATION_DELETE;
1359 if ( slap_mask & SLAP_CTRL_MODIFY )
1360 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1362 if ( slap_mask & SLAP_CTRL_RENAME )
1363 *slapi_mask |= SLAPI_OPERATION_MODDN;
1365 if ( slap_mask & SLAP_CTRL_SEARCH )
1366 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1368 if ( slap_mask & SLAP_CTRL_UNBIND )
1369 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1373 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1374 slap_mask_t *slap_mask)
1378 if ( slapi_mask & SLAPI_OPERATION_BIND )
1379 *slap_mask |= SLAP_CTRL_BIND;
1381 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1382 *slap_mask |= SLAP_CTRL_UNBIND;
1384 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1385 *slap_mask |= SLAP_CTRL_SEARCH;
1387 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1388 *slap_mask |= SLAP_CTRL_MODIFY;
1390 if ( slapi_mask & SLAPI_OPERATION_ADD )
1391 *slap_mask |= SLAP_CTRL_ADD;
1393 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1394 *slap_mask |= SLAP_CTRL_DELETE;
1396 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1397 *slap_mask |= SLAP_CTRL_RENAME;
1399 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1400 *slap_mask |= SLAP_CTRL_COMPARE;
1402 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1403 *slap_mask |= SLAP_CTRL_ABANDON;
1405 *slap_mask |= SLAP_CTRL_GLOBAL;
1409 slapi_int_parse_control(
1414 /* Plugins must deal with controls themselves. */
1416 return LDAP_SUCCESS;
1418 #endif /* LDAP_SLAPI */
1421 slapi_register_supported_control(
1423 unsigned long controlops )
1426 slap_mask_t controlmask;
1428 slapiControlOp2SlapControlMask( controlops, &controlmask );
1430 register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL );
1431 #endif /* LDAP_SLAPI */
1435 slapi_get_supported_controls(
1437 unsigned long **ctrlopsp )
1442 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1443 if ( rc != LDAP_SUCCESS ) {
1447 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1448 /* In place, naughty. */
1449 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1452 return LDAP_SUCCESS;
1453 #else /* LDAP_SLAPI */
1455 #endif /* LDAP_SLAPI */
1459 slapi_dup_control( LDAPControl *ctrl )
1464 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1465 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1466 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1467 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1472 #endif /* LDAP_SLAPI */
1476 slapi_register_supported_saslmechanism( char *mechanism )
1479 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1480 slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1481 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1482 #endif /* LDAP_SLAPI */
1486 slapi_get_supported_saslmechanisms( void )
1489 /* FIXME -- can not get the saslmechanism without a connection. */
1490 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1491 "can not get the SASL mechanism list "
1492 "without a connection\n" );
1494 #else /* LDAP_SLAPI */
1496 #endif /* LDAP_SLAPI */
1500 slapi_get_supported_extended_ops( void )
1504 char **ppExtOpOID = NULL;
1507 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1511 for ( j = 0; slapi_int_get_supported_extop( j ) != NULL; j++ ) {
1516 if ( numExtOps == 0 ) {
1520 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1521 for ( k = 0; k < i; k++ ) {
1524 bv = get_supported_extop( k );
1525 assert( bv != NULL );
1527 ppExtOpOID[ k ] = bv->bv_val;
1530 for ( ; k < j; k++ ) {
1533 bv = slapi_int_get_supported_extop( k );
1534 assert( bv != NULL );
1536 ppExtOpOID[ i + k ] = bv->bv_val;
1538 ppExtOpOID[ i + k ] = NULL;
1541 #else /* LDAP_SLAPI */
1543 #endif /* LDAP_SLAPI */
1547 slapi_send_ldap_result(
1553 struct berval **urls )
1557 char *extOID = NULL;
1558 SlapReply rs = { REP_RESULT };
1560 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1563 rs.sr_matched = matched;
1568 slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
1570 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1571 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
1572 send_ldap_sasl( op, &rs );
1576 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1577 if ( extOID != NULL ) {
1578 rs.sr_rspoid = extOID;
1579 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
1580 send_ldap_extended( op, &rs );
1584 if ( op->o_tag == LDAP_REQ_SEARCH )
1585 rs.sr_nentries = nentries;
1587 send_ldap_result( op, &rs );
1588 #endif /* LDAP_SLAPI */
1592 slapi_send_ldap_search_entry(
1595 LDAPControl **ectrls,
1601 SlapReply rs = { REP_RESULT };
1603 AttributeName *an = NULL;
1606 if ( attrs != NULL ) {
1607 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1615 an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
1616 for ( i = 0; attrs[i] != NULL; i++ ) {
1617 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1618 an[i].an_name.bv_len = strlen( attrs[i] );
1619 an[i].an_desc = NULL;
1620 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1623 an[i].an_name.bv_len = 0;
1624 an[i].an_name.bv_val = NULL;
1627 rs.sr_err = LDAP_SUCCESS;
1628 rs.sr_matched = NULL;
1631 rs.sr_ctrls = ectrls;
1633 rs.sr_operational_attrs = NULL;
1638 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1642 return send_search_entry( pOp, &rs );
1643 #else /* LDAP_SLAPI */
1645 #endif /* LDAP_SLAPI */
1649 slapi_send_ldap_search_reference(
1652 struct berval **references,
1653 LDAPControl **ectrls,
1654 struct berval **v2refs
1659 SlapReply rs = { REP_SEARCHREF };
1662 rs.sr_err = LDAP_SUCCESS;
1663 rs.sr_matched = NULL;
1666 rc = bvptr2obj( references, &rs.sr_ref );
1667 if ( rc != LDAP_SUCCESS ) {
1671 rs.sr_ctrls = ectrls;
1673 rs.sr_operational_attrs = NULL;
1676 if ( v2refs != NULL ) {
1677 rc = bvptr2obj( v2refs, &rs.sr_v2ref );
1678 if ( rc != LDAP_SUCCESS ) {
1679 slapi_ch_free( (void **)&rs.sr_ref );
1686 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1690 rc = send_search_reference( pOp, &rs );
1692 if ( rs.sr_ref != NULL )
1693 slapi_ch_free( (void **)&rs.sr_ref );
1695 if ( rs.sr_v2ref != NULL )
1696 slapi_ch_free( (void **)&rs.sr_v2ref );
1701 #endif /* LDAP_SLAPI */
1705 slapi_str2filter( char *str )
1708 return str2filter( str );
1709 #else /* LDAP_SLAPI */
1711 #endif /* LDAP_SLAPI */
1721 #endif /* LDAP_SLAPI */
1725 slapi_filter_dup( Slapi_Filter *filter )
1730 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1732 f->f_choice = filter->f_choice;
1734 switch ( f->f_choice ) {
1735 case LDAP_FILTER_AND:
1736 case LDAP_FILTER_NOT:
1737 case LDAP_FILTER_OR: {
1738 Filter *pFilter, **ppF;
1740 for ( pFilter = filter->f_list, ppF = &f->f_list;
1742 pFilter = pFilter->f_next, ppF = &f->f_next )
1744 *ppF = slapi_filter_dup( pFilter );
1750 case LDAP_FILTER_PRESENT:
1751 f->f_desc = filter->f_desc;
1753 case LDAP_FILTER_EQUALITY:
1754 case LDAP_FILTER_GE:
1755 case LDAP_FILTER_LE:
1756 case LDAP_FILTER_APPROX:
1757 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1758 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1759 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1761 case LDAP_FILTER_EXT:
1762 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1763 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1764 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1765 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1766 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1767 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1769 case LDAP_FILTER_SUBSTRINGS: {
1772 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1773 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1774 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1775 if ( filter->f_sub_any != NULL ) {
1776 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1778 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1779 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1780 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1782 f->f_sub_any[i].bv_val = NULL;
1784 f->f_sub_any = NULL;
1786 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1789 case SLAPD_FILTER_COMPUTED:
1790 f->f_result = filter->f_result;
1793 slapi_ch_free( (void **)&f );
1801 #endif /* LDAP_SLAPI */
1805 slapi_filter_get_choice( Slapi_Filter *f )
1817 #else /* LDAP_SLAPI */
1818 return -1; /* invalid filter type */
1819 #endif /* LDAP_SLAPI */
1823 slapi_filter_get_ava(
1826 struct berval **bval )
1830 int rc = LDAP_SUCCESS;
1832 assert( type != NULL );
1833 assert( bval != NULL );
1838 ftype = f->f_choice;
1839 if ( ftype == LDAP_FILTER_EQUALITY
1840 || ftype == LDAP_FILTER_GE
1841 || ftype == LDAP_FILTER_LE
1842 || ftype == LDAP_FILTER_APPROX ) {
1844 * According to the SLAPI Reference Manual these are
1847 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1848 *bval = &f->f_un.f_un_ava->aa_value;
1849 } else { /* filter type not supported */
1854 #else /* LDAP_SLAPI */
1856 #endif /* LDAP_SLAPI */
1860 slapi_filter_list_first( Slapi_Filter *f )
1869 ftype = f->f_choice;
1870 if ( ftype == LDAP_FILTER_AND
1871 || ftype == LDAP_FILTER_OR
1872 || ftype == LDAP_FILTER_NOT ) {
1873 return (Slapi_Filter *)f->f_list;
1877 #else /* LDAP_SLAPI */
1879 #endif /* LDAP_SLAPI */
1883 slapi_filter_list_next(
1885 Slapi_Filter *fprev )
1894 ftype = f->f_choice;
1895 if ( ftype == LDAP_FILTER_AND
1896 || ftype == LDAP_FILTER_OR
1897 || ftype == LDAP_FILTER_NOT )
1899 return fprev->f_next;
1903 #else /* LDAP_SLAPI */
1905 #endif /* LDAP_SLAPI */
1909 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1916 switch ( f->f_choice ) {
1917 case LDAP_FILTER_GE:
1918 case LDAP_FILTER_LE:
1919 case LDAP_FILTER_EQUALITY:
1920 case LDAP_FILTER_APPROX:
1921 *type = f->f_av_desc->ad_cname.bv_val;
1923 case LDAP_FILTER_SUBSTRINGS:
1924 *type = f->f_sub_desc->ad_cname.bv_val;
1926 case LDAP_FILTER_PRESENT:
1927 *type = f->f_desc->ad_cname.bv_val;
1929 case LDAP_FILTER_EXT:
1930 *type = f->f_mr_desc->ad_cname.bv_val;
1933 /* Complex filters need not apply. */
1941 #endif /* LDAP_SLAPI */
1945 slapi_x_filter_set_attribute_type( Slapi_Filter *f, const char *type )
1948 AttributeDescription **adp, *ad = NULL;
1956 switch ( f->f_choice ) {
1957 case LDAP_FILTER_GE:
1958 case LDAP_FILTER_LE:
1959 case LDAP_FILTER_EQUALITY:
1960 case LDAP_FILTER_APPROX:
1961 adp = &f->f_av_desc;
1963 case LDAP_FILTER_SUBSTRINGS:
1964 adp = &f->f_sub_desc;
1966 case LDAP_FILTER_PRESENT:
1969 case LDAP_FILTER_EXT:
1970 adp = &f->f_mr_desc;
1973 /* Complex filters need not apply. */
1977 rc = slap_str2ad( type, &ad, &text );
1978 if ( rc == LDAP_SUCCESS )
1981 return ( rc == LDAP_SUCCESS ) ? 0 : -1;
1984 #endif /* LDAP_SLAPI */
1988 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1989 char ***any, char **final )
1994 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1999 * The caller shouldn't free but we can't return an
2000 * array of char *s from an array of bervals without
2001 * allocating memory, so we may as well be consistent.
2004 *type = f->f_sub_desc->ad_cname.bv_val;
2005 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
2006 if ( f->f_sub_any != NULL ) {
2007 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
2009 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
2010 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
2011 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
2017 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
2022 #endif /* LDAP_SLAPI */
2026 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
2029 Slapi_Filter *f = NULL;
2031 if ( ftype == LDAP_FILTER_AND ||
2032 ftype == LDAP_FILTER_OR ||
2033 ftype == LDAP_FILTER_NOT )
2035 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
2036 f->f_choice = ftype;
2038 f->f_list->f_next = f2;
2045 #endif /* LDAP_SLAPI */
2049 slapi_x_filter_append( int ftype,
2050 Slapi_Filter **pContainingFilter, /* NULL on first call */
2051 Slapi_Filter **pNextFilter,
2052 Slapi_Filter *filterToAppend )
2055 if ( ftype == LDAP_FILTER_AND ||
2056 ftype == LDAP_FILTER_OR ||
2057 ftype == LDAP_FILTER_NOT )
2059 if ( *pContainingFilter == NULL ) {
2060 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
2061 (*pContainingFilter)->f_choice = ftype;
2062 (*pContainingFilter)->f_list = filterToAppend;
2063 (*pContainingFilter)->f_next = NULL;
2065 if ( (*pContainingFilter)->f_choice != ftype ) {
2069 (*pNextFilter)->f_next = filterToAppend;
2071 *pNextFilter = filterToAppend;
2075 #endif /* LDAP_SLAPI */
2080 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
2088 /* spec says return zero if no filter. */
2092 if ( verify_access ) {
2093 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
2095 return LDAP_PARAM_ERROR;
2101 * According to acl.c it is safe to call test_filter() with
2104 rc = test_filter( op, e, f );
2106 case LDAP_COMPARE_TRUE:
2109 case LDAP_COMPARE_FALSE:
2111 case SLAPD_COMPARE_UNDEFINED:
2114 case LDAP_PROTOCOL_ERROR:
2115 /* filter type unknown: spec says return -1 */
2123 #endif /* LDAP_SLAPI */
2127 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
2130 return slapi_filter_test( NULL, e, f, 0 );
2137 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
2140 switch ( f->f_choice ) {
2141 case LDAP_FILTER_AND:
2142 case LDAP_FILTER_NOT:
2143 case LDAP_FILTER_OR: {
2147 * FIXME: altering f; should we use a temporary?
2149 for ( f = f->f_list; f != NULL; f = f->f_next ) {
2150 rc = slapi_filter_apply( f, fn, arg, error_code );
2154 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
2160 case LDAP_FILTER_EQUALITY:
2161 case LDAP_FILTER_SUBSTRINGS:
2162 case LDAP_FILTER_GE:
2163 case LDAP_FILTER_LE:
2164 case LDAP_FILTER_PRESENT:
2165 case LDAP_FILTER_APPROX:
2166 case LDAP_FILTER_EXT:
2167 *error_code = fn( f, arg );
2170 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2173 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
2174 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
2180 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2182 #endif /* LDAP_SLAPI */
2186 slapi_send_ldap_extended_response(
2191 struct berval *response )
2196 rs.sr_err = errornum;
2197 rs.sr_matched = NULL;
2201 rs.sr_rspoid = respName;
2202 rs.sr_rspdata = response;
2204 send_ldap_extended( op, &rs );
2206 return LDAP_SUCCESS;
2207 #else /* LDAP_SLAPI */
2209 #endif /* LDAP_SLAPI */
2214 struct berval **vals,
2219 * FIXME: what's the point?
2222 #else /* LDAP_SLAPI */
2224 #endif /* LDAP_SLAPI */
2227 #define MAX_HOSTNAME 512
2230 slapi_get_hostname( void )
2234 static int been_here = 0;
2235 static char *static_hn = NULL;
2237 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
2239 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
2240 if ( static_hn == NULL) {
2241 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
2242 "Cannot allocate memory for hostname\n" );
2244 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2249 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
2250 slapi_log_error( SLAPI_LOG_FATAL,
2252 "can't get hostname\n" );
2253 slapi_ch_free( (void **)&static_hn );
2255 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2264 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2266 hn = ch_strdup( static_hn );
2269 #else /* LDAP_SLAPI */
2271 #endif /* LDAP_SLAPI */
2275 * FIXME: this should go in an appropriate header ...
2277 extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist );
2287 int rc = LDAP_SUCCESS;
2290 va_start( arglist, fmt );
2291 rc = slapi_int_log_error( severity, subsystem, fmt, arglist );
2295 #else /* LDAP_SLAPI */
2297 #endif /* LDAP_SLAPI */
2302 slapi_timer_current_time( void )
2305 static int first_time = 1;
2306 #if !defined (_WIN32)
2310 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
2313 gettimeofday( &base_time, NULL );
2315 gettimeofday( &now, NULL );
2316 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
2317 (now.tv_usec - base_time.tv_usec);
2318 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
2324 return (slap_get_time() - starttime) * 1000000;
2331 performance_counter_present = QueryPerformanceCounter( &base_time );
2332 QueryPerformanceFrequency( &performance_freq );
2335 if ( !performance_counter_present )
2338 QueryPerformanceCounter( &now );
2339 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2341 #else /* LDAP_SLAPI */
2343 #endif /* LDAP_SLAPI */
2350 slapi_timer_get_time( char *label )
2353 unsigned long start = slapi_timer_current_time();
2354 printf("%10ld %10d usec %s\n", start, 0, label);
2356 #else /* LDAP_SLAPI */
2358 #endif /* LDAP_SLAPI */
2365 slapi_timer_elapsed_time(
2367 unsigned long start )
2370 unsigned long stop = slapi_timer_current_time();
2371 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2372 #endif /* LDAP_SLAPI */
2376 slapi_free_search_results_internal( Slapi_PBlock *pb )
2379 Slapi_Entry **entries;
2380 int k = 0, nEnt = 0;
2382 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2383 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2388 if ( entries == NULL ) {
2392 for ( k = 0; k < nEnt; k++ ) {
2393 slapi_entry_free( entries[k] );
2396 slapi_ch_free( (void **)&entries );
2397 #endif /* LDAP_SLAPI */
2402 * Internal API to prime a Slapi_PBlock with a Backend.
2404 static int slapi_int_pblock_set_backend( Slapi_PBlock *pb, Backend *be )
2408 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2409 if ( rc != LDAP_SUCCESS )
2413 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2414 if ( rc != LDAP_SUCCESS )
2418 return LDAP_SUCCESS;
2422 * If oldStyle is TRUE, then a value suitable for setting to
2423 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
2424 * (pointer to static storage).
2426 * If oldStyle is FALSE, then a value suitable for setting to
2427 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2428 * a pointer to allocated memory and will include the SASL
2429 * mechanism (if any).
2431 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2436 switch ( authz->sai_method ) {
2437 case LDAP_AUTH_SASL:
2439 authType = SLAPD_AUTH_SASL;
2441 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2442 authType = slapi_ch_malloc( len );
2443 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2446 case LDAP_AUTH_SIMPLE:
2447 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2449 case LDAP_AUTH_NONE:
2450 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2456 if ( is_tls && authType == NULL ) {
2457 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2464 * Internal API to prime a Slapi_PBlock with a Connection.
2466 static int slapi_int_pblock_set_connection( Slapi_PBlock *pb, Connection *conn )
2471 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2472 if ( rc != LDAP_SUCCESS ) {
2476 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2477 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2478 if ( rc != LDAP_SUCCESS )
2480 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2481 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2482 if ( rc != LDAP_SUCCESS )
2486 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2487 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2488 if ( rc != LDAP_SUCCESS )
2490 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2491 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2492 if ( rc != LDAP_SUCCESS )
2496 #ifdef LDAP_CONNECTIONLESS
2497 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2498 if ( rc != LDAP_SUCCESS )
2502 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2503 if ( rc != LDAP_SUCCESS )
2506 /* Returns pointer to static string */
2507 connAuthType = Authorization2AuthType( &conn->c_authz,
2514 if ( connAuthType != NULL ) {
2515 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2516 if ( rc != LDAP_SUCCESS )
2520 /* Returns pointer to allocated string */
2521 connAuthType = Authorization2AuthType( &conn->c_authz,
2528 if ( connAuthType != NULL ) {
2529 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2530 /* slapi_pblock_set dups this itself */
2531 slapi_ch_free( (void **)&connAuthType );
2532 if ( rc != LDAP_SUCCESS )
2536 if ( conn->c_authz.sai_method != LDAP_AUTH_NONE &&
2537 conn->c_authz.sai_dn.bv_val != NULL ) {
2538 /* slapi_pblock_set dups this itself */
2539 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)conn->c_authz.sai_dn.bv_val);
2540 if ( rc != LDAP_SUCCESS )
2544 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SSF, (void *)conn->c_ssf);
2545 if ( rc != LDAP_SUCCESS )
2548 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SASL_CONTEXT,
2549 ( conn->c_sasl_authctx != NULL ? conn->c_sasl_authctx :
2550 conn->c_sasl_sockctx ) );
2551 if ( rc != LDAP_SUCCESS )
2556 #endif /* LDAP_SLAPI */
2559 * Internal API to prime a Slapi_PBlock with an Operation.
2561 int slapi_int_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
2568 void *existingOp = NULL;
2570 if ( op->o_bd != NULL ) {
2571 isRoot = be_isroot( op );
2572 isUpdateDn = be_isupdate( op );
2575 /* This should only be called once per operation */
2576 slapi_pblock_get( pb, SLAPI_OPERATION, &existingOp );
2577 assert( existingOp == NULL );
2579 rc = slapi_int_pblock_set_backend( pb, op->o_bd );
2580 if ( rc != LDAP_SUCCESS )
2583 rc = slapi_int_pblock_set_connection( pb, op->o_conn );
2584 if ( rc != LDAP_SUCCESS )
2587 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2588 if ( rc != LDAP_SUCCESS )
2591 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2592 if ( rc != LDAP_SUCCESS )
2595 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2596 if ( rc != LDAP_SUCCESS )
2599 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2600 if ( rc != LDAP_SUCCESS )
2603 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2604 if ( rc != LDAP_SUCCESS )
2607 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2608 if ( rc != LDAP_SUCCESS )
2611 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2612 if ( rc != LDAP_SUCCESS)
2615 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2616 if ( rc != LDAP_SUCCESS )
2619 rc = slapi_pblock_set( pb, SLAPI_TARGET_DN, (void *)op->o_req_dn.bv_val );
2620 if ( rc != LDAP_SUCCESS )
2623 rc = slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)get_manageDSAit( op ) );
2624 if ( rc != LDAP_SUCCESS )
2627 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2628 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
2629 /* Not quite sure what the point of this is. */
2630 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
2631 if ( rc != LDAP_SUCCESS )
2635 return LDAP_SUCCESS;
2641 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2646 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2648 *isSSL = conn->c_is_tls;
2653 return LDAP_SUCCESS;
2656 #endif /* LDAP_SLAPI */
2660 * DS 5.x compatability API follow
2663 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2669 return LDAP_PARAM_ERROR;
2671 at = attr->a_desc->ad_type;
2673 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2675 if ( is_at_single_value( at ) )
2676 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2677 if ( is_at_operational( at ) )
2678 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2679 if ( is_at_obsolete( at ) )
2680 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2681 if ( is_at_collective( at ) )
2682 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2683 if ( is_at_no_user_mod( at ) )
2684 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2686 return LDAP_SUCCESS;
2689 #endif /* LDAP_SLAPI */
2692 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2695 unsigned long flags;
2697 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2699 return (flags & flag) ? 1 : 0;
2702 #endif /* LDAP_SLAPI */
2705 Slapi_Attr *slapi_attr_new( void )
2710 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2718 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2722 AttributeDescription *ad = NULL;
2724 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2740 void slapi_attr_free( Slapi_Attr **a )
2748 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2751 return attr_dup( (Slapi_Attr *)attr );
2757 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2761 struct berval *nvalp;
2763 AttributeDescription *desc = a->a_desc;
2765 if ( desc->ad_type->sat_equality &&
2766 desc->ad_type->sat_equality->smr_normalize ) {
2767 rc = (*desc->ad_type->sat_equality->smr_normalize)(
2768 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2769 desc->ad_type->sat_syntax,
2770 desc->ad_type->sat_equality,
2771 (Slapi_Value *)v, &nval, NULL );
2772 if ( rc != LDAP_SUCCESS ) {
2780 rc = value_add_one( &a->a_vals, (Slapi_Value *)v );
2781 if ( rc == 0 && nvalp != NULL ) {
2782 rc = value_add_one( &a->a_nvals, nvalp );
2784 a->a_nvals = a->a_vals;
2787 if ( nvalp != NULL ) {
2788 slapi_ch_free_string( &nval.bv_val );
2797 int slapi_attr_type2plugin( const char *type, void **pi )
2804 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2807 if ( attr == NULL ) {
2808 return LDAP_PARAM_ERROR;
2811 *type = attr->a_desc->ad_cname.bv_val;
2813 return LDAP_SUCCESS;
2819 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2822 if ( attr == NULL ) {
2823 return LDAP_PARAM_ERROR;
2825 *oidp = attr->a_desc->ad_type->sat_oid;
2827 return LDAP_SUCCESS;
2833 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2841 mr = a->a_desc->ad_type->sat_equality;
2842 rc = value_match( &ret, a->a_desc, mr,
2843 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2844 (struct berval *)v1, (void *)v2, &text );
2845 if ( rc != LDAP_SUCCESS )
2848 return ( ret == 0 ) ? 0 : -1;
2854 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2864 if ( a ->a_vals == NULL ) {
2867 mr = a->a_desc->ad_type->sat_equality;
2868 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2869 rc = value_match( &ret, a->a_desc, mr,
2870 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2871 if ( rc != LDAP_SUCCESS ) {
2878 #endif /* LDAP_SLAPI */
2882 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2885 AttributeDescription *a1 = NULL;
2886 AttributeDescription *a2 = NULL;
2890 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2894 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2898 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2899 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2900 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2903 case SLAPI_TYPE_CMP_EXACT:
2904 ret = ad_cmp( a1, a2 );
2906 case SLAPI_TYPE_CMP_BASE:
2907 ret = ad_base_cmp( a1, a2 );
2909 case SLAPI_TYPE_CMP_SUBTYPE:
2910 ret = is_ad_subtype( a2, a2 );
2923 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2926 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2932 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2935 return slapi_valueset_first_value( &a->a_vals, v );
2941 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2944 return slapi_valueset_next_value( &a->a_vals, hint, v );
2950 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2953 *numValues = slapi_valueset_count( &a->a_vals );
2961 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2964 *vs = &((Slapi_Attr *)a)->a_vals;
2972 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2975 return slapi_attr_get_values( a, vals );
2981 char *slapi_attr_syntax_normalize( const char *s )
2984 AttributeDescription *ad = NULL;
2987 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2991 return ad->ad_cname.bv_val;
2997 Slapi_Value *slapi_value_new( void )
3002 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
3010 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
3013 return ber_dupbv( NULL, (struct berval *)bval );
3019 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
3022 return slapi_value_new_berval( v );
3028 Slapi_Value *slapi_value_new_string(const char *s)
3033 bv.bv_val = (char *)s;
3034 bv.bv_len = strlen( s );
3036 return slapi_value_new_berval( &bv );
3042 Slapi_Value *slapi_value_init(Slapi_Value *val)
3054 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
3057 return ber_dupbv( v, bval );
3063 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
3066 v->bv_val = slapi_ch_strdup( (char *)s );
3067 v->bv_len = strlen( s );
3075 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
3078 return slapi_value_new_value( v );
3084 void slapi_value_free(Slapi_Value **value)
3087 if ( value == NULL ) {
3091 if ( (*value) != NULL ) {
3092 slapi_ch_free( (void **)&(*value)->bv_val );
3093 slapi_ch_free( (void **)value );
3098 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
3107 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
3110 if ( value == NULL ) {
3113 if ( value->bv_val != NULL ) {
3114 slapi_ch_free( (void **)&value->bv_val );
3116 slapi_value_init_berval( value, (struct berval *)bval );
3124 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
3127 if ( value == NULL ) {
3130 return slapi_value_set_berval( value, vfrom );
3136 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
3139 if ( value == NULL ) {
3142 if ( value->bv_val != NULL ) {
3143 slapi_ch_free( (void **)&value->bv_val );
3145 value->bv_val = slapi_ch_malloc( len );
3146 value->bv_len = len;
3147 AC_MEMCPY( value->bv_val, val, len );
3155 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
3158 if ( value == NULL ) {
3161 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
3168 int slapi_value_set_int(Slapi_Value *value, int intVal)
3173 snprintf( buf, sizeof( buf ), "%d", intVal );
3175 return slapi_value_set_string( value, buf );
3181 const char *slapi_value_get_string(const Slapi_Value *value)
3184 if ( value == NULL ) return NULL;
3185 if ( value->bv_val == NULL ) return NULL;
3186 if ( !checkBVString( value ) ) return NULL;
3188 return value->bv_val;
3194 int slapi_value_get_int(const Slapi_Value *value)
3197 if ( value == NULL ) return 0;
3198 if ( value->bv_val == NULL ) return 0;
3199 if ( !checkBVString( value ) ) return 0;
3201 return (int)strtol( value->bv_val, NULL, 10 );
3207 unsigned int slapi_value_get_uint(const Slapi_Value *value)
3210 if ( value == NULL ) return 0;
3211 if ( value->bv_val == NULL ) return 0;
3212 if ( !checkBVString( value ) ) return 0;
3214 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
3220 long slapi_value_get_long(const Slapi_Value *value)
3223 if ( value == NULL ) return 0;
3224 if ( value->bv_val == NULL ) return 0;
3225 if ( !checkBVString( value ) ) return 0;
3227 return strtol( value->bv_val, NULL, 10 );
3233 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
3236 if ( value == NULL ) return 0;
3237 if ( value->bv_val == NULL ) return 0;
3238 if ( !checkBVString( value ) ) return 0;
3240 return strtoul( value->bv_val, NULL, 10 );
3246 size_t slapi_value_get_length(const Slapi_Value *value)
3249 if ( value == NULL )
3252 return (size_t) value->bv_len;
3258 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
3261 return slapi_attr_value_cmp( a, v1, v2 );
3267 /* A ValueSet is a container for a BerVarray. */
3268 Slapi_ValueSet *slapi_valueset_new( void )
3273 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
3282 void slapi_valueset_free(Slapi_ValueSet *vs)
3288 ber_bvarray_free( vp );
3289 slapi_ch_free( (void **)&vp );
3296 void slapi_valueset_init(Slapi_ValueSet *vs)
3299 if ( vs != NULL && *vs == NULL ) {
3300 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
3301 (*vs)->bv_val = NULL;
3307 void slapi_valueset_done(Slapi_ValueSet *vs)
3315 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
3317 slapi_ch_free( (void **)&vp->bv_val );
3319 /* but don't free *vs or vs */
3323 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
3328 ber_dupbv( &bv, (Slapi_Value *)addval );
3329 ber_bvarray_add( vs, &bv );
3333 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
3336 return slapi_valueset_next_value( vs, 0, v );
3342 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
3353 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
3364 int slapi_valueset_count( const Slapi_ValueSet *vs )
3375 for ( i = 0; vp[i].bv_val != NULL; i++ )
3385 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
3390 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
3391 slapi_valueset_add_value( vs1, vp );
3396 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
3397 struct berval *val, int access )
3404 slap_access_t slap_access;
3405 AttributeDescription *ad = NULL;
3408 rc = slap_str2ad( attr, &ad, &text );
3409 if ( rc != LDAP_SUCCESS ) {
3414 * Whilst the SLAPI access types are arranged as a bitmask, the
3415 * documentation indicates that they are to be used separately.
3417 switch ( access & SLAPI_ACL_ALL ) {
3418 case SLAPI_ACL_COMPARE:
3419 slap_access = ACL_COMPARE;
3421 case SLAPI_ACL_SEARCH:
3422 slap_access = ACL_SEARCH;
3424 case SLAPI_ACL_READ:
3425 slap_access = ACL_READ;
3427 case SLAPI_ACL_WRITE:
3428 slap_access = ACL_WRITE;
3430 case SLAPI_ACL_DELETE:
3431 slap_access = ACL_WDEL;
3434 slap_access = ACL_WADD;
3436 case SLAPI_ACL_SELF: /* not documented */
3437 case SLAPI_ACL_PROXY: /* not documented */
3439 return LDAP_INSUFFICIENT_ACCESS;
3443 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3444 return LDAP_PARAM_ERROR;
3447 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3448 return LDAP_PARAM_ERROR;
3451 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3452 return LDAP_PARAM_ERROR;
3455 if ( access_allowed( op, e, ad, val, slap_access, NULL ) ) {
3456 return LDAP_SUCCESS;
3459 return LDAP_INSUFFICIENT_ACCESS;
3461 return LDAP_UNWILLING_TO_PERFORM;
3465 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
3469 int rc = LDAP_SUCCESS;
3470 Modifications *ml, *mp;
3472 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3473 return LDAP_PARAM_ERROR;
3476 ml = slapi_int_ldapmods2modifications( mods );
3481 if ( rc == LDAP_SUCCESS ) {
3482 rc = acl_check_modlist( op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3485 /* Careful when freeing the modlist because it has pointers into the mods array. */
3486 for ( ; ml != NULL; ml = mp ) {
3489 /* just free the containing array */
3490 slapi_ch_free( (void **)&ml->sml_values );
3491 slapi_ch_free( (void **)&ml );
3496 return LDAP_UNWILLING_TO_PERFORM;
3501 * Synthesise an LDAPMod array from a Modifications list to pass
3502 * to SLAPI. This synthesis is destructive and as such the
3503 * Modifications list may not be used after calling this
3506 * This function must also be called before slap_mods_check().
3508 LDAPMod **slapi_int_modifications2ldapmods(Modifications **pmodlist)
3511 Modifications *ml, *modlist;
3512 LDAPMod **mods, *modp;
3515 modlist = *pmodlist;
3517 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3520 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3522 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3523 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3525 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3527 /* Take ownership of original type. */
3528 modp->mod_type = ml->sml_type.bv_val;
3529 ml->sml_type.bv_val = NULL;
3531 if ( ml->sml_values != NULL ) {
3532 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
3534 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3535 sizeof(struct berval *) );
3536 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
3537 /* Take ownership of original values. */
3538 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3539 modp->mod_bvalues[j]->bv_len = ml->sml_values[j].bv_len;
3540 modp->mod_bvalues[j]->bv_val = ml->sml_values[j].bv_val;
3541 ml->sml_values[j].bv_len = 0;
3542 ml->sml_values[j].bv_val = NULL;
3544 modp->mod_bvalues[j] = NULL;
3546 modp->mod_bvalues = NULL;
3553 slap_mods_free( modlist );
3563 * Convert a potentially modified array of LDAPMods back to a
3564 * Modification list.
3566 * The returned Modification list contains pointers into the
3567 * LDAPMods array; the latter MUST be freed with
3568 * slapi_int_free_ldapmods() (see below).
3570 Modifications *slapi_int_ldapmods2modifications (LDAPMod **mods)
3573 Modifications *modlist = NULL, **modtail;
3576 if ( mods == NULL ) {
3582 for( modp = mods; *modp != NULL; modp++ ) {
3586 struct berval **bvp;
3588 AttributeDescription *ad = NULL;
3590 if ( slap_str2ad( (*modp)->mod_type, &ad, &text ) != LDAP_SUCCESS )
3593 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3594 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3596 mod->sml_type.bv_val = (*modp)->mod_type;
3597 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3599 mod->sml_next = NULL;
3601 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3602 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3605 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3610 mod->sml_values = NULL;
3612 mod->sml_values = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3614 /* NB: This implicitly trusts a plugin to return valid modifications. */
3615 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3616 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3617 mod->sml_values[i].bv_val = (*bvp)->bv_val;
3618 mod->sml_values[i].bv_len = (*bvp)->bv_len;
3621 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3622 mod->sml_values[i].bv_val = *p;
3623 mod->sml_values[i].bv_len = strlen( *p );
3626 mod->sml_values[i].bv_val = NULL;
3627 mod->sml_values[i].bv_len = 0;
3629 mod->sml_nvalues = NULL;
3632 modtail = &mod->sml_next;
3642 * This function only frees the parts of the mods array that
3643 * are not shared with the Modification list that was created
3644 * by slapi_int_ldapmods2modifications().
3647 void slapi_int_free_ldapmods (LDAPMod **mods)
3655 for ( i = 0; mods[i] != NULL; i++ ) {
3657 * Don't free values themselves; they're owned by the
3658 * Modification list. Do free the containing array.
3660 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3661 for ( j = 0; mods[i]->mod_values != NULL && mods[i]->mod_values[j] != NULL; j++ ) {
3662 ch_free( mods[i]->mod_values[j] );
3664 ch_free( mods[i]->mod_values );
3666 ch_free( mods[i]->mod_values );
3668 /* Don't free type, for same reasons. */
3672 #endif /* LDAP_SLAPI */
3676 * Sun ONE DS 5.x computed attribute support. Computed attributes
3677 * allow for dynamically generated operational attributes, a very
3678 * useful thing indeed.
3682 * For some reason Sun don't use the normal plugin mechanism
3683 * registration path to register an "evaluator" function (an
3684 * "evaluator" is responsible for adding computed attributes;
3685 * the nomenclature is somewhat confusing).
3687 * As such slapi_compute_add_evaluator() registers the
3688 * function directly.
3690 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3693 Slapi_PBlock *pPlugin = NULL;
3696 pPlugin = slapi_pblock_new();
3697 if ( pPlugin == NULL ) {
3698 rc = LDAP_NO_MEMORY;
3702 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3703 if ( rc != LDAP_SUCCESS ) {
3707 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3708 if ( rc != LDAP_SUCCESS ) {
3712 rc = slapi_int_register_plugin( frontendDB, pPlugin );
3719 if ( rc != LDAP_SUCCESS ) {
3720 if ( pPlugin != NULL ) {
3721 slapi_pblock_destroy( pPlugin );
3729 #endif /* LDAP_SLAPI */
3733 * See notes above regarding slapi_compute_add_evaluator().
3735 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3738 Slapi_PBlock *pPlugin = NULL;
3741 pPlugin = slapi_pblock_new();
3742 if ( pPlugin == NULL ) {
3743 rc = LDAP_NO_MEMORY;
3747 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3748 if ( rc != LDAP_SUCCESS ) {
3752 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3753 if ( rc != LDAP_SUCCESS ) {
3757 rc = slapi_int_register_plugin( frontendDB, pPlugin );
3764 if ( rc != LDAP_SUCCESS ) {
3765 if ( pPlugin != NULL ) {
3766 slapi_pblock_destroy( pPlugin );
3774 #endif /* LDAP_SLAPI */
3778 * Call compute evaluators
3780 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3784 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3786 rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3787 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3788 /* Nothing to do; front-end should ignore. */
3792 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3794 * -1: no attribute matched requested type
3795 * 0: one attribute matched
3796 * >0: error happened
3798 rc = (*pGetPlugin)( c, type, e, outputfn );
3804 slapi_ch_free( (void **)&tmpPlugin );
3809 #endif /* LDAP_SLAPI */
3812 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3818 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3823 return slapi_int_call_plugins( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3826 #endif /* LDAP_SLAPI */
3830 * New API to provide the plugin with access to the search
3831 * pblock. Have informed Sun DS team.
3833 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3839 if ( c->cac_pb == NULL )
3847 #endif /* LDAP_SLAPI */
3850 Slapi_Mutex *slapi_new_mutex( void )
3855 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3856 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3857 slapi_ch_free( (void **)&m );
3867 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3870 if ( mutex != NULL ) {
3871 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3872 slapi_ch_free( (void **)&mutex);
3877 void slapi_lock_mutex( Slapi_Mutex *mutex )
3880 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3884 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3887 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3893 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3898 if ( mutex == NULL ) {
3902 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3903 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3904 slapi_ch_free( (void **)&cv );
3908 /* XXX struct copy */
3909 cv->mutex = mutex->mutex;
3917 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3920 if ( cvar != NULL ) {
3921 ldap_pvt_thread_cond_destroy( &cvar->cond );
3922 slapi_ch_free( (void **)&cvar );
3927 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3930 if ( cvar == NULL ) {
3934 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3940 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3943 if ( cvar == NULL ) {
3948 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3951 return ldap_pvt_thread_cond_signal( &cvar->cond );
3957 int slapi_int_access_allowed( Operation *op,
3959 AttributeDescription *desc,
3961 slap_access_t access,
3962 AccessControlState *state )
3965 int rc, slap_access = 0;
3966 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3968 if ( op->o_pb == NULL ) {
3969 /* internal operation */
3975 slap_access |= SLAPI_ACL_COMPARE;
3978 slap_access |= SLAPI_ACL_SEARCH;
3981 slap_access |= SLAPI_ACL_READ;
3984 slap_access |= SLAPI_ACL_WRITE;
3987 slap_access |= SLAPI_ACL_DELETE;
3990 slap_access |= SLAPI_ACL_ADD;
3996 rc = slapi_int_get_plugins( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
3997 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3998 /* nothing to do; allowed access */
4002 rc = 1; /* default allow policy */
4004 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
4009 rc = (*pGetPlugin)( op->o_pb, entry, desc->ad_cname.bv_val,
4010 val, slap_access, (void *)state );
4016 slapi_ch_free( (void **)&tmpPlugin );
4021 #endif /* LDAP_SLAPI */
4025 * There is no documentation for this.
4027 int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
4040 rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
4041 if ( rc != LDAP_SUCCESS ) {
4045 if ( lrdn[1] != NULL ) {
4046 return -1; /* not single valued */
4051 *type = slapi_ch_strdup( ava->la_attr.bv_val );
4052 ber_dupbv( bv, &ava->la_value );
4059 #endif /* LDAP_SLAPI */
4062 char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
4065 struct berval new_dn, parent_dn, newrdn;
4067 new_dn.bv_val = NULL;
4069 parent_dn.bv_val = (char *)dn;
4070 parent_dn.bv_len = strlen( dn );
4072 newrdn.bv_val = (char *)rdn;
4073 newrdn.bv_len = strlen( rdn );
4075 build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
4077 return new_dn.bv_val;
4080 #endif /* LDAP_SLAPI */
4083 int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
4088 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
4089 size_t textlen = sizeof textbuf;
4092 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
4095 rc = entry_schema_check( be, e, NULL, 0,
4096 &text, textbuf, textlen );
4098 return ( rc == LDAP_SUCCESS ) ? 0 : 1;
4101 #endif /* LDAP_SLAPI */
4104 int slapi_entry_rdn_values_present( const Slapi_Entry *e )
4109 int i = 0, match = 0;
4111 rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
4112 if ( rc != LDAP_SUCCESS ) {
4116 if ( dn[0] != NULL ) {
4117 LDAPRDN rdn = dn[0];
4119 for ( i = 0; rdn[i] != NULL; i++ ) {
4120 LDAPAVA *ava = &rdn[0][i];
4121 Slapi_Attr *a = NULL;
4123 if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
4124 slapi_attr_value_find( a, &ava->la_value ) == 0 )
4131 return ( i == match );
4134 #endif /* LDAP_SLAPI */
4137 int slapi_entry_add_rdn_values( Slapi_Entry *e )
4143 rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
4144 if ( rc != LDAP_SUCCESS ) {
4148 if ( dn[0] != NULL ) {
4149 LDAPRDN rdn = dn[0];
4150 struct berval *vals[2];
4152 for ( i = 0; rdn[i] != NULL; i++ ) {
4153 LDAPAVA *ava = &rdn[0][i];
4154 Slapi_Attr *a = NULL;
4156 if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
4157 slapi_attr_value_find( a, &ava->la_value ) == 0 )
4160 vals[0] = &ava->la_value;
4163 slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
4169 return LDAP_SUCCESS;
4172 #endif /* LDAP_SLAPI */
4175 const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
4180 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
4181 if ( attr == NULL ) {
4185 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
4186 return slapi_value_get_string( &attr->a_vals[0] );
4188 #endif /* LDAP_SLAPI */
4193 void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
4198 attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
4200 bv.bv_val = uniqueid;
4201 bv.bv_len = strlen( uniqueid );
4202 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
4203 #endif /* LDAP_SLAPI */
4206 LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
4214 size = sizeof("ldap:///");
4217 size += strlen( ldaphost );
4218 if ( ldapport != 0 )
4221 url = slapi_ch_malloc( size );
4223 if ( ldapport != 0 ) {
4224 sprintf( url, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
4226 sprintf( url, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
4229 rc = ldap_initialize( &ld, url );
4231 slapi_ch_free_string( &url );
4233 return ( rc == LDAP_SUCCESS ) ? ld : NULL;
4236 #endif /* LDAP_SLAPI */
4239 void slapi_ldap_unbind( LDAP *ld )
4243 #endif /* LDAP_SLAPI */
4246 int slapi_x_backend_get_flags( const Slapi_Backend *be, unsigned long *flags )
4250 return LDAP_PARAM_ERROR;
4252 *flags = SLAP_DBFLAGS(be);
4254 return LDAP_SUCCESS;
4257 #endif /* LDAP_SLAPI */