2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2002-2003 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>
37 * server start time (should we use a struct timeval also in slapd?
39 static struct timeval base_time;
40 ldap_pvt_thread_mutex_t slapi_hn_mutex;
41 ldap_pvt_thread_mutex_t slapi_time_mutex;
44 ldap_pvt_thread_mutex_t mutex;
47 struct slapi_condvar {
48 ldap_pvt_thread_cond_t cond;
49 ldap_pvt_thread_mutex_t mutex;
53 * This function converts an array of pointers to berval objects to
54 * an array of berval objects.
59 struct berval **bvptr,
62 int rc = LDAP_SUCCESS;
66 if ( bvptr == NULL || *bvptr == NULL ) {
70 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
74 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
75 if ( tmpberval == NULL ) {
76 return LDAP_NO_MEMORY;
79 for ( i = 0; bvptr[i] != NULL; i++ ) {
80 tmpberval[i].bv_val = bvptr[i]->bv_val;
81 tmpberval[i].bv_len = bvptr[i]->bv_len;
83 tmpberval[i].bv_val = NULL;
84 tmpberval[i].bv_len = 0;
86 if ( rc == LDAP_SUCCESS ) {
99 Slapi_Entry *e = NULL;
102 pTmpS = slapi_ch_strdup( s );
103 if ( pTmpS != NULL ) {
104 e = str2entry( pTmpS );
105 slapi_ch_free( (void **)&pTmpS );
111 #endif /* LDAP_SLAPI */
122 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
123 ret = entry2str( e, len );
124 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
127 #else /* LDAP_SLAPI */
129 #endif /* LDAP_SLAPI */
133 slapi_entry_get_dn( Slapi_Entry *e )
136 return e->e_name.bv_val;
137 #else /* LDAP_SLAPI */
139 #endif /* LDAP_SLAPI */
143 slapi_x_entry_get_id( Slapi_Entry *e )
149 #endif /* LDAP_SLAPI */
158 struct berval dn = { 0, NULL };
161 dn.bv_len = strlen( ldn );
163 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname, NULL );
164 #endif /* LDAP_SLAPI */
168 slapi_entry_dup( Slapi_Entry *e )
171 return entry_dup( e );
172 #else /* LDAP_SLAPI */
174 #endif /* LDAP_SLAPI */
178 slapi_entry_attr_delete(
183 AttributeDescription *ad = NULL;
186 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
187 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
190 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
191 return 0; /* attribute is deleted */
193 return -1; /* something went wrong */
195 #else /* LDAP_SLAPI */
197 #endif /* LDAP_SLAPI */
201 slapi_entry_alloc( void )
204 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
205 #else /* LDAP_SLAPI */
207 #endif /* LDAP_SLAPI */
211 slapi_entry_free( Slapi_Entry *e )
215 #endif /* LDAP_SLAPI */
219 slapi_entry_attr_merge(
222 struct berval **vals )
225 AttributeDescription *ad = NULL;
230 rc = bvptr2obj( vals, &bv );
231 if ( rc != LDAP_SUCCESS ) {
235 rc = slap_str2ad( type, &ad, &text );
236 if ( rc != LDAP_SUCCESS ) {
240 rc = attr_merge_normalize_one( e, ad, bv, NULL );
244 #else /* LDAP_SLAPI */
246 #endif /* LDAP_SLAPI */
250 slapi_entry_attr_find(
256 AttributeDescription *ad = NULL;
260 rc = slap_str2ad( type, &ad, &text );
261 if ( rc != LDAP_SUCCESS ) {
265 *attr = attr_find( e->e_attrs, ad );
266 if ( *attr == NULL ) {
271 #else /* LDAP_SLAPI */
273 #endif /* LDAP_SLAPI */
277 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
280 AttributeDescription *ad = NULL;
285 rc = slap_str2ad( type, &ad, &text );
286 if ( rc != LDAP_SUCCESS ) {
290 attr = attr_find( e->e_attrs, ad );
291 if ( attr == NULL ) {
295 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
296 return slapi_ch_strdup( attr->a_vals[0].bv_val );
306 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
309 AttributeDescription *ad = NULL;
314 rc = slap_str2ad( type, &ad, &text );
315 if ( rc != LDAP_SUCCESS ) {
319 attr = attr_find( e->e_attrs, ad );
320 if ( attr == NULL ) {
324 return slapi_value_get_int( attr->a_vals );
331 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
334 AttributeDescription *ad = NULL;
339 rc = slap_str2ad( type, &ad, &text );
340 if ( rc != LDAP_SUCCESS ) {
344 attr = attr_find( e->e_attrs, ad );
345 if ( attr == NULL ) {
349 return slapi_value_get_long( attr->a_vals );
356 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
359 AttributeDescription *ad = NULL;
364 rc = slap_str2ad( type, &ad, &text );
365 if ( rc != LDAP_SUCCESS ) {
369 attr = attr_find( e->e_attrs, ad );
370 if ( attr == NULL ) {
374 return slapi_value_get_uint( attr->a_vals );
381 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
384 AttributeDescription *ad = NULL;
389 rc = slap_str2ad( type, &ad, &text );
390 if ( rc != LDAP_SUCCESS ) {
394 attr = attr_find( e->e_attrs, ad );
395 if ( attr == NULL ) {
399 return slapi_value_get_ulong( attr->a_vals );
406 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
410 AttributeDescription *ad = NULL;
415 rc = slap_str2ad( type, &ad, &text );
416 if ( rc != LDAP_SUCCESS ) {
420 attr = attr_find( e->e_attrs, ad );
421 if ( attr == NULL ) {
425 bv.bv_val = (char *)value;
426 bv.bv_len = strlen( value );
428 return ( slapi_attr_value_find( attr, &bv ) != -1 );
435 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
438 AttributeDescription *ad = NULL;
443 rc = slap_str2ad( type, &ad, &text );
444 if ( rc != LDAP_SUCCESS ) {
448 attr_delete ( &e->e_attrs, ad );
449 if ( value != NULL ) {
450 bv.bv_val = (char *)value;
451 bv.bv_len = strlen(value);
452 attr_merge_normalize_one( e, ad, &bv, NULL );
454 #endif /* LDAP_SLAPI */
458 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
463 snprintf( buf, sizeof( buf ), "%d", l );
464 slapi_entry_attr_set_charptr( e, type, buf );
465 #endif /* LDAP_SLAPI */
469 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
474 snprintf( buf, sizeof( buf ), "%u", l );
475 slapi_entry_attr_set_charptr( e, type, buf );
476 #endif /* LDAP_SLAPI */
480 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
485 snprintf( buf, sizeof( buf ), "%ld", l );
486 slapi_entry_attr_set_charptr( e, type, buf );
487 #endif /* LDAP_SLAPI */
491 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
496 snprintf( buf, sizeof( buf ), "%lu", l );
497 slapi_entry_attr_set_charptr( e, type, buf );
498 #endif /* LDAP_SLAPI */
502 slapi_is_rootdse( const char *dn )
505 return ( dn == NULL || dn[0] == '\0' );
512 * Add values to entry.
515 * LDAP_SUCCESS Values added to entry
516 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
517 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
520 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
526 char textbuf[SLAP_TEXT_BUFLEN];
528 mod.sm_op = LDAP_MOD_ADD;
530 mod.sm_type.bv_val = (char *)type;
531 mod.sm_type.bv_len = strlen( type );
533 rc = slap_str2ad( type, &mod.sm_desc, &text );
534 if ( rc != LDAP_SUCCESS ) {
538 if ( vals == NULL ) {
539 /* Apparently vals can be NULL
540 * FIXME: sm_bvalues = NULL ? */
541 mod.sm_bvalues = (BerVarray)ch_malloc( sizeof(struct berval) );
542 mod.sm_bvalues->bv_val = NULL;
545 rc = bvptr2obj( vals, &mod.sm_bvalues );
546 if ( rc != LDAP_SUCCESS ) {
547 return LDAP_CONSTRAINT_VIOLATION;
550 mod.sm_nvalues = NULL;
552 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
554 ch_free( mod.sm_bvalues );
556 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
559 #endif /* LDAP_SLAPI */
563 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
566 return slapi_entry_add_values( e, type, vals );
569 #endif /* LDAP_SLAPI */
573 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
576 AttributeDescription *ad = NULL;
580 rc = slap_str2ad( type, &ad, &text );
581 if ( rc != LDAP_SUCCESS ) {
585 return attr_merge_normalize( e, ad, *vs, NULL );
588 #endif /* LDAP_SLAPI */
592 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
598 char textbuf[SLAP_TEXT_BUFLEN];
600 mod.sm_op = LDAP_MOD_DELETE;
602 mod.sm_type.bv_val = (char *)type;
603 mod.sm_type.bv_len = strlen( type );
605 if ( vals == NULL ) {
606 /* If vals is NULL, this is a NOOP. */
610 rc = slap_str2ad( type, &mod.sm_desc, &text );
611 if ( rc != LDAP_SUCCESS ) {
615 if ( vals[0] == NULL ) {
616 /* SLAPI doco says LDAP_OPERATIONS_ERROR but LDAP_OTHER is better */
617 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
620 rc = bvptr2obj( vals, &mod.sm_bvalues );
621 if ( rc != LDAP_SUCCESS ) {
622 return LDAP_CONSTRAINT_VIOLATION;
624 mod.sm_nvalues = NULL;
626 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
628 ch_free( mod.sm_bvalues );
633 #endif /* LDAP_SLAPI */
637 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
640 return slapi_entry_delete_values( e, type, vals );
643 #endif /* LDAP_SLAPI */
647 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
650 return slapi_entry_attr_merge( e, (char *)type, vals );
653 #endif /* LDAP_SLAPI */
657 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
660 AttributeDescription *ad = NULL;
664 rc = slap_str2ad( type, &ad, &text );
665 if ( rc != LDAP_SUCCESS ) {
669 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
670 if ( rc != LDAP_SUCCESS ) {
677 #endif /* LDAP_SLAPI */
681 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
686 val.bv_val = (char *)value;
687 val.bv_len = strlen( value );
689 return slapi_entry_add_value( e, type, &val );
692 #endif /* LDAP_SLAPI */
696 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
699 Slapi_Value *vals[2];
702 val.bv_val = (char *)value;
703 val.bv_len = strlen( value );
707 return slapi_entry_delete_values_sv( e, type, vals );
710 #endif /* LDAP_SLAPI */
715 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
718 return slapi_entry_attr_merge( e, (char *)type, vals );
725 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
734 return ( *attr != NULL ) ? 0 : -1;
741 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
748 if ( prevattr == NULL ) {
752 *attr = prevattr->a_next;
754 return ( *attr != NULL ) ? 0 : -1;
761 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
764 AttributeDescription *ad = NULL;
769 rc = slap_str2ad( type, &ad, &text );
770 if ( rc != LDAP_SUCCESS ) {
774 attr_delete( &e->e_attrs, ad );
776 rc = bvptr2obj( vals, &bv );
777 if ( rc != LDAP_SUCCESS ) {
781 rc = attr_merge_normalize( e, ad, bv, NULL );
782 slapi_ch_free( (void **)&bv );
783 if ( rc != LDAP_SUCCESS ) {
790 #endif /* LDAP_SLAPI */
794 * FIXME -- The caller must free the allocated memory.
795 * In Netscape they do not have to.
798 slapi_attr_get_values(
800 struct berval ***vals )
806 if ( attr == NULL ) {
810 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
814 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
815 for ( j = 0; j < i; j++ ) {
816 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
820 *vals = (struct berval **)bv;
823 #else /* LDAP_SLAPI */
825 #endif /* LDAP_SLAPI */
829 slapi_dn_normalize( char *dn )
835 assert( dn != NULL );
838 bdn.bv_len = strlen( dn );
840 if ( dnPretty( NULL, &bdn, &pdn, NULL ) != LDAP_SUCCESS ) {
845 #else /* LDAP_SLAPI */
847 #endif /* LDAP_SLAPI */
851 slapi_dn_normalize_case( char *dn )
857 assert( dn != NULL );
860 bdn.bv_len = strlen( dn );
862 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
867 #else /* LDAP_SLAPI */
869 #endif /* LDAP_SLAPI */
878 struct berval bdn, ndn;
879 struct berval bsuffix, nsuffix;
882 assert( dn != NULL );
883 assert( suffix != NULL );
886 bdn.bv_len = strlen( dn );
888 bsuffix.bv_val = suffix;
889 bsuffix.bv_len = strlen( suffix );
891 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
895 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
898 slapi_ch_free( (void **)&ndn.bv_val );
902 rc = dnIsSuffix( &ndn, &nsuffix );
904 slapi_ch_free( (void **)&ndn.bv_val );
905 slapi_ch_free( (void **)&nsuffix.bv_val );
908 #else /* LDAP_SLAPI */
910 #endif /* LDAP_SLAPI */
915 const char *parentdn,
916 const char *childdn )
919 struct berval assertedParentDN, normalizedAssertedParentDN;
920 struct berval childDN, normalizedChildDN;
921 struct berval normalizedParentDN;
924 assert( parentdn != NULL );
925 assert( childdn != NULL );
927 assertedParentDN.bv_val = (char *)parentdn;
928 assertedParentDN.bv_len = strlen( parentdn );
930 if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
931 &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
936 childDN.bv_val = (char *)childdn;
937 childDN.bv_len = strlen( childdn );
939 if ( dnNormalize( 0, NULL, NULL, &childDN,
940 &normalizedChildDN, NULL ) != LDAP_SUCCESS )
942 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
946 dnParent( &normalizedChildDN, &normalizedParentDN );
948 if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
949 &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
954 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
955 slapi_ch_free( (void **)&normalizedChildDN.bv_val );
957 return ( match == 0 );
960 #endif /* LDAP_SLAPI */
964 * Returns DN of the parent entry, or NULL if the DN is
965 * an empty string or NULL, or has no parent.
968 slapi_dn_parent( const char *_dn )
971 struct berval dn, prettyDN;
972 struct berval parentDN;
978 dn.bv_val = (char *)_dn;
979 dn.bv_len = strlen( _dn );
981 if ( dn.bv_len == 0 ) {
985 if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
989 dnParent( &prettyDN, &parentDN ); /* in-place */
991 slapi_ch_free( (void **)&prettyDN.bv_val );
993 if ( parentDN.bv_len == 0 ) {
997 return slapi_ch_strdup( parentDN.bv_val );
1000 #endif /* LDAP_SLAPI */
1004 * Returns DN of the parent entry; or NULL if the DN is
1005 * an empty string, if the DN has no parent, or if the
1006 * DN is the suffix of the backend database
1008 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *_dn )
1012 struct berval dn, prettyDN;
1013 struct berval normalizedDN, parentDN;
1015 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
1018 dn.bv_val = (char *)_dn;
1019 dn.bv_len = strlen( _dn );
1021 if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
1025 if ( be != NULL && be_issuffix( be, &normalizedDN ) ) {
1026 slapi_ch_free( (void **)&prettyDN.bv_val );
1027 slapi_ch_free( (void **)&normalizedDN.bv_val );
1031 dnParent( &prettyDN, &parentDN );
1033 slapi_ch_free( (void **)&prettyDN.bv_val );
1034 slapi_ch_free( (void **)&normalizedDN.bv_val );
1036 if ( parentDN.bv_len == 0 ) {
1040 return slapi_ch_strdup( parentDN.bv_val );
1043 #endif /* LDAP_SLAPI */
1047 slapi_dn_ignore_case( char *dn )
1050 return slapi_dn_normalize_case( dn );
1051 #else /* LDAP_SLAPI */
1053 #endif /* LDAP_SLAPI */
1057 slapi_ch_malloc( unsigned long size )
1060 return ch_malloc( size );
1061 #else /* LDAP_SLAPI */
1063 #endif /* LDAP_SLAPI */
1067 slapi_ch_free( void **ptr )
1072 #endif /* LDAP_SLAPI */
1076 slapi_ch_free_string( char **ptr )
1079 slapi_ch_free( (void **)ptr );
1080 #endif /* LDAP_SLAPI */
1084 slapi_ch_array_free( char **arrayp )
1089 if ( arrayp != NULL ) {
1090 for ( p = arrayp; *p != NULL; p++ ) {
1091 slapi_ch_free( (void **)p );
1093 slapi_ch_free( (void **)&arrayp );
1099 slapi_ch_bvdup(const struct berval *v)
1104 bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
1105 bv->bv_len = v->bv_len;
1106 bv->bv_val = slapi_ch_malloc( bv->bv_len );
1107 AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1116 slapi_ch_bvecdup(const struct berval **v)
1126 for ( i = 0; v[i] != NULL; i++ )
1129 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1131 for ( i = 0; v[i] != NULL; i++ ) {
1132 rv[i] = slapi_ch_bvdup( v[i] );
1144 unsigned long nelem,
1145 unsigned long size )
1148 return ch_calloc( nelem, size );
1149 #else /* LDAP_SLAPI */
1151 #endif /* LDAP_SLAPI */
1157 unsigned long size )
1160 return ch_realloc( block, size );
1161 #else /* LDAP_SLAPI */
1163 #endif /* LDAP_SLAPI */
1167 slapi_ch_strdup( char *s )
1170 return ch_strdup( (const char *)s );
1171 #else /* LDAP_SLAPI */
1173 #endif /* LDAP_SLAPI */
1177 slapi_ch_stlen( char *s )
1180 return strlen( (const char *)s );
1181 #else /* LDAP_SLAPI */
1183 #endif /* LDAP_SLAPI */
1187 slapi_control_present(
1188 LDAPControl **controls,
1190 struct berval **val,
1205 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1206 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1211 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1213 * FIXME: according to 6.1 specification,
1214 * "The val output parameter is set
1215 * to point into the controls array.
1216 * A copy of the control value is
1220 struct berval *pTmpBval;
1222 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
1223 if ( pTmpBval == NULL ) {
1226 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
1227 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
1231 slapi_ch_free( (void **)&pTmpBval );
1237 *val = &controls[i]->ldctl_value;
1242 *iscritical = controls[i]->ldctl_iscritical;
1249 #else /* LDAP_SLAPI */
1251 #endif /* LDAP_SLAPI */
1256 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1257 unsigned long *slapi_mask)
1259 *slapi_mask = SLAPI_OPERATION_NONE;
1261 if ( slap_mask & SLAP_CTRL_ABANDON )
1262 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1264 if ( slap_mask & SLAP_CTRL_ADD )
1265 *slapi_mask |= SLAPI_OPERATION_ADD;
1267 if ( slap_mask & SLAP_CTRL_BIND )
1268 *slapi_mask |= SLAPI_OPERATION_BIND;
1270 if ( slap_mask & SLAP_CTRL_COMPARE )
1271 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1273 if ( slap_mask & SLAP_CTRL_DELETE )
1274 *slapi_mask |= SLAPI_OPERATION_DELETE;
1276 if ( slap_mask & SLAP_CTRL_MODIFY )
1277 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1279 if ( slap_mask & SLAP_CTRL_RENAME )
1280 *slapi_mask |= SLAPI_OPERATION_MODDN;
1282 if ( slap_mask & SLAP_CTRL_SEARCH )
1283 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1285 if ( slap_mask & SLAP_CTRL_UNBIND )
1286 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1290 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1291 slap_mask_t *slap_mask)
1295 if ( slapi_mask & SLAPI_OPERATION_BIND )
1296 *slap_mask |= SLAP_CTRL_BIND;
1298 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1299 *slap_mask |= SLAP_CTRL_UNBIND;
1301 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1302 *slap_mask |= SLAP_CTRL_SEARCH;
1304 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1305 *slap_mask |= SLAP_CTRL_MODIFY;
1307 if ( slapi_mask & SLAPI_OPERATION_ADD )
1308 *slap_mask |= SLAP_CTRL_ADD;
1310 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1311 *slap_mask |= SLAP_CTRL_DELETE;
1313 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1314 *slap_mask |= SLAP_CTRL_RENAME;
1316 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1317 *slap_mask |= SLAP_CTRL_COMPARE;
1319 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1320 *slap_mask |= SLAP_CTRL_ABANDON;
1322 *slap_mask |= SLAP_CTRL_FRONTEND;
1331 /* Plugins must deal with controls themselves. */
1333 return LDAP_SUCCESS;
1335 #endif /* LDAP_SLAPI */
1338 slapi_register_supported_control(
1340 unsigned long controlops )
1343 slap_mask_t controlmask;
1345 slapiControlOp2SlapControlMask( controlops, &controlmask );
1347 register_supported_control( controloid, controlmask, NULL, parseSlapiControl );
1348 #endif /* LDAP_SLAPI */
1352 slapi_get_supported_controls(
1354 unsigned long **ctrlopsp )
1359 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1360 if ( rc != LDAP_SUCCESS ) {
1364 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1365 /* In place, naughty. */
1366 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1369 return LDAP_SUCCESS;
1370 #else /* LDAP_SLAPI */
1372 #endif /* LDAP_SLAPI */
1376 slapi_dup_control( LDAPControl *ctrl )
1381 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1382 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1383 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1384 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1389 #endif /* LDAP_SLAPI */
1393 slapi_register_supported_saslmechanism( char *mechanism )
1396 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1397 slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1398 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1399 #endif /* LDAP_SLAPI */
1403 slapi_get_supported_saslmechanisms( void )
1406 /* FIXME -- can not get the saslmechanism without a connection. */
1407 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1408 "can not get the SASL mechanism list "
1409 "without a connection\n" );
1411 #else /* LDAP_SLAPI */
1413 #endif /* LDAP_SLAPI */
1417 slapi_get_supported_extended_ops( void )
1421 char **ppExtOpOID = NULL;
1424 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1428 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
1433 if ( numExtOps == 0 ) {
1437 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1438 for ( k = 0; k < i; k++ ) {
1441 bv = get_supported_extop( k );
1442 assert( bv != NULL );
1444 ppExtOpOID[ k ] = bv->bv_val;
1447 for ( ; k < j; k++ ) {
1450 bv = ns_get_supported_extop( k );
1451 assert( bv != NULL );
1453 ppExtOpOID[ i + k ] = bv->bv_val;
1455 ppExtOpOID[ i + k ] = NULL;
1458 #else /* LDAP_SLAPI */
1460 #endif /* LDAP_SLAPI */
1464 slapi_send_ldap_result(
1470 struct berval **urls )
1475 char *extOID = NULL;
1476 struct berval *extValue = NULL;
1478 SlapReply rs = { REP_RESULT };
1480 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1483 rs.sr_matched = matched;
1488 slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
1490 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1491 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
1492 send_ldap_sasl( op, &rs );
1496 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1497 if ( extOID != NULL ) {
1498 rs.sr_rspoid = extOID;
1499 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
1500 send_ldap_extended( op, &rs );
1504 if (op->o_tag == LDAP_REQ_SEARCH)
1505 rs.sr_nentries = nentries;
1507 send_ldap_result( op, &rs );
1508 #endif /* LDAP_SLAPI */
1512 slapi_send_ldap_search_entry(
1515 LDAPControl **ectrls,
1521 SlapReply rs = { REP_RESULT };
1523 AttributeName *an = NULL;
1526 if ( attrs != NULL ) {
1527 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1535 an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
1536 for ( i = 0; attrs[i] != NULL; i++ ) {
1537 an[i].an_name.bv_val = ch_strdup( attrs[i] );
1538 an[i].an_name.bv_len = strlen( attrs[i] );
1539 an[i].an_desc = NULL;
1540 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1543 an[i].an_name.bv_len = 0;
1544 an[i].an_name.bv_val = NULL;
1547 rs.sr_err = LDAP_SUCCESS;
1548 rs.sr_matched = NULL;
1551 rs.sr_ctrls = ectrls;
1556 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1560 return send_search_entry( pOp, &rs );
1561 #else /* LDAP_SLAPI */
1563 #endif /* LDAP_SLAPI */
1567 slapi_send_ldap_search_reference(
1570 struct berval **references,
1571 LDAPControl **ectrls,
1572 struct berval **v2refs
1577 SlapReply rs = { REP_SEARCHREF };
1580 rs.sr_err = LDAP_SUCCESS;
1581 rs.sr_matched = NULL;
1584 rc = bvptr2obj( references, &rs.sr_ref );
1585 if ( rc != LDAP_SUCCESS ) {
1589 rs.sr_ctrls = ectrls;
1593 if ( v2refs != NULL ) {
1594 rc = bvptr2obj( v2refs, &rs.sr_v2ref );
1595 if ( rc != LDAP_SUCCESS ) {
1596 slapi_ch_free( (void **)&rs.sr_ref );
1603 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
1607 rc = send_search_reference( pOp, &rs );
1609 if ( rs.sr_ref != NULL )
1610 slapi_ch_free( (void **)&rs.sr_ref );
1612 if ( rs.sr_v2ref != NULL )
1613 slapi_ch_free( (void **)&rs.sr_v2ref );
1618 #endif /* LDAP_SLAPI */
1622 slapi_str2filter( char *str )
1625 return str2filter( str );
1626 #else /* LDAP_SLAPI */
1628 #endif /* LDAP_SLAPI */
1638 #endif /* LDAP_SLAPI */
1642 slapi_filter_dup( Slapi_Filter *filter )
1647 f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1649 f->f_choice = filter->f_choice;
1651 switch ( f->f_choice ) {
1652 case LDAP_FILTER_AND:
1653 case LDAP_FILTER_NOT:
1654 case LDAP_FILTER_OR: {
1655 Filter *pFilter, **ppF;
1657 for ( pFilter = filter->f_list, ppF = &f->f_list;
1659 pFilter = pFilter->f_next, ppF = &f->f_next )
1661 *ppF = slapi_filter_dup( pFilter );
1667 case LDAP_FILTER_PRESENT:
1668 f->f_desc = filter->f_desc;
1670 case LDAP_FILTER_EQUALITY:
1671 case LDAP_FILTER_GE:
1672 case LDAP_FILTER_LE:
1673 case LDAP_FILTER_APPROX:
1674 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1675 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1676 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1678 case LDAP_FILTER_EXT:
1679 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1680 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1681 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1682 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1683 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1684 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1686 case LDAP_FILTER_SUBSTRINGS: {
1689 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1690 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1691 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1692 if ( filter->f_sub_any != NULL ) {
1693 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1695 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1696 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1697 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1699 f->f_sub_any[i].bv_val = NULL;
1701 f->f_sub_any = NULL;
1703 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1706 case SLAPD_FILTER_COMPUTED:
1707 f->f_result = filter->f_result;
1710 slapi_ch_free( (void **)&f );
1718 #endif /* LDAP_SLAPI */
1722 slapi_filter_get_choice( Slapi_Filter *f )
1734 #else /* LDAP_SLAPI */
1735 return -1; /* invalid filter type */
1736 #endif /* LDAP_SLAPI */
1740 slapi_filter_get_ava(
1743 struct berval **bval )
1747 int rc = LDAP_SUCCESS;
1749 assert( type != NULL );
1750 assert( bval != NULL );
1755 ftype = f->f_choice;
1756 if ( ftype == LDAP_FILTER_EQUALITY
1757 || ftype == LDAP_FILTER_GE
1758 || ftype == LDAP_FILTER_LE
1759 || ftype == LDAP_FILTER_APPROX ) {
1761 * According to the SLAPI Reference Manual these are
1764 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1765 *bval = &f->f_un.f_un_ava->aa_value;
1766 } else { /* filter type not supported */
1771 #else /* LDAP_SLAPI */
1773 #endif /* LDAP_SLAPI */
1777 slapi_filter_list_first( Slapi_Filter *f )
1786 ftype = f->f_choice;
1787 if ( ftype == LDAP_FILTER_AND
1788 || ftype == LDAP_FILTER_OR
1789 || ftype == LDAP_FILTER_NOT ) {
1790 return (Slapi_Filter *)f->f_list;
1794 #else /* LDAP_SLAPI */
1796 #endif /* LDAP_SLAPI */
1800 slapi_filter_list_next(
1802 Slapi_Filter *fprev )
1811 ftype = f->f_choice;
1812 if ( ftype == LDAP_FILTER_AND
1813 || ftype == LDAP_FILTER_OR
1814 || ftype == LDAP_FILTER_NOT )
1816 return fprev->f_next;
1820 #else /* LDAP_SLAPI */
1822 #endif /* LDAP_SLAPI */
1826 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1833 switch ( f->f_choice ) {
1834 case LDAP_FILTER_GE:
1835 case LDAP_FILTER_LE:
1836 case LDAP_FILTER_EQUALITY:
1837 case LDAP_FILTER_APPROX:
1838 *type = f->f_av_desc->ad_cname.bv_val;
1840 case LDAP_FILTER_SUBSTRINGS:
1841 *type = f->f_sub_desc->ad_cname.bv_val;
1843 case LDAP_FILTER_PRESENT:
1844 *type = f->f_desc->ad_cname.bv_val;
1846 case LDAP_FILTER_EXT:
1847 *type = f->f_mr_desc->ad_cname.bv_val;
1850 /* Complex filters need not apply. */
1858 #endif /* LDAP_SLAPI */
1862 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1863 char ***any, char **final )
1868 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1873 * The caller shouldn't free but we can't return an
1874 * array of char *s from an array of bervals without
1875 * allocating memory, so we may as well be consistent.
1878 *type = f->f_sub_desc->ad_cname.bv_val;
1879 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1880 if ( f->f_sub_any != NULL ) {
1881 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1883 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1884 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1885 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1891 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1896 #endif /* LDAP_SLAPI */
1900 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1903 Slapi_Filter *f = NULL;
1905 if ( ftype == LDAP_FILTER_AND ||
1906 ftype == LDAP_FILTER_OR ||
1907 ftype == LDAP_FILTER_NOT )
1909 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1910 f->f_choice = ftype;
1912 f->f_list->f_next = f2;
1919 #endif /* LDAP_SLAPI */
1923 slapi_x_filter_append( int ftype,
1924 Slapi_Filter **pContainingFilter, /* NULL on first call */
1925 Slapi_Filter **pNextFilter,
1926 Slapi_Filter *filterToAppend )
1929 if ( ftype == LDAP_FILTER_AND ||
1930 ftype == LDAP_FILTER_OR ||
1931 ftype == LDAP_FILTER_NOT )
1933 if ( *pContainingFilter == NULL ) {
1934 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
1935 (*pContainingFilter)->f_choice = ftype;
1936 (*pContainingFilter)->f_list = filterToAppend;
1937 (*pContainingFilter)->f_next = NULL;
1939 if ( (*pContainingFilter)->f_choice != ftype ) {
1943 (*pNextFilter)->f_next = filterToAppend;
1945 *pNextFilter = filterToAppend;
1949 #endif /* LDAP_SLAPI */
1954 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1962 /* spec says return zero if no filter. */
1966 if ( verify_access ) {
1967 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
1969 return LDAP_PARAM_ERROR;
1975 * According to acl.c it is safe to call test_filter() with
1978 rc = test_filter( op, e, f );
1980 case LDAP_COMPARE_TRUE:
1983 case LDAP_COMPARE_FALSE:
1985 case SLAPD_COMPARE_UNDEFINED:
1988 case LDAP_PROTOCOL_ERROR:
1989 /* filter type unknown: spec says return -1 */
1997 #endif /* LDAP_SLAPI */
2001 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
2004 return slapi_filter_test( NULL, e, f, 0 );
2011 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
2014 switch ( f->f_choice ) {
2015 case LDAP_FILTER_AND:
2016 case LDAP_FILTER_NOT:
2017 case LDAP_FILTER_OR: {
2021 * FIXME: altering f; should we use a temporary?
2023 for ( f = f->f_list; f != NULL; f = f->f_next ) {
2024 rc = slapi_filter_apply( f, fn, arg, error_code );
2028 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
2034 case LDAP_FILTER_EQUALITY:
2035 case LDAP_FILTER_SUBSTRINGS:
2036 case LDAP_FILTER_GE:
2037 case LDAP_FILTER_LE:
2038 case LDAP_FILTER_PRESENT:
2039 case LDAP_FILTER_APPROX:
2040 case LDAP_FILTER_EXT:
2041 *error_code = fn( f, arg );
2044 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2047 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
2048 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
2054 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
2056 #endif /* LDAP_SLAPI */
2060 slapi_send_ldap_extended_response(
2065 struct berval *response )
2070 rs.sr_err = errornum;
2071 rs.sr_matched = NULL;
2075 rs.sr_rspoid = respName;
2076 rs.sr_rspdata = response;
2078 send_ldap_extended( op, &rs );
2080 return LDAP_SUCCESS;
2081 #else /* LDAP_SLAPI */
2083 #endif /* LDAP_SLAPI */
2088 struct berval **vals,
2093 * FIXME: what's the point?
2096 #else /* LDAP_SLAPI */
2098 #endif /* LDAP_SLAPI */
2101 #define MAX_HOSTNAME 512
2104 slapi_get_hostname( void )
2108 static int been_here = 0;
2109 static char *static_hn = NULL;
2111 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
2113 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
2114 if ( static_hn == NULL) {
2115 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
2116 "Cannot allocate memory for hostname\n" );
2118 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2123 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
2124 slapi_log_error( SLAPI_LOG_FATAL,
2126 "can't get hostname\n" );
2127 slapi_ch_free( (void **)&static_hn );
2129 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2138 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
2140 hn = ch_strdup( static_hn );
2143 #else /* LDAP_SLAPI */
2145 #endif /* LDAP_SLAPI */
2149 * FIXME: this should go in an appropriate header ...
2151 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
2161 int rc = LDAP_SUCCESS;
2164 va_start( arglist, fmt );
2165 rc = vLogError( severity, subsystem, fmt, arglist );
2169 #else /* LDAP_SLAPI */
2171 #endif /* LDAP_SLAPI */
2176 slapi_timer_current_time( void )
2179 static int first_time = 1;
2180 #if !defined (_WIN32)
2184 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
2187 gettimeofday( &base_time, NULL );
2189 gettimeofday( &now, NULL );
2190 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
2191 (now.tv_usec - base_time.tv_usec);
2192 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
2198 return (slap_get_time() - starttime) * 1000000;
2205 performance_counter_present = QueryPerformanceCounter( &base_time );
2206 QueryPerformanceFrequency( &performance_freq );
2209 if ( !performance_counter_present )
2212 QueryPerformanceCounter( &now );
2213 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2215 #else /* LDAP_SLAPI */
2217 #endif /* LDAP_SLAPI */
2224 slapi_timer_get_time( char *label )
2227 unsigned long start = slapi_timer_current_time();
2228 printf("%10ld %10ld usec %s\n", start, 0, label);
2230 #else /* LDAP_SLAPI */
2232 #endif /* LDAP_SLAPI */
2239 slapi_timer_elapsed_time(
2241 unsigned long start )
2244 unsigned long stop = slapi_timer_current_time();
2245 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2246 #endif /* LDAP_SLAPI */
2250 slapi_free_search_results_internal( Slapi_PBlock *pb )
2253 Slapi_Entry **entries;
2254 int k = 0, nEnt = 0;
2256 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2257 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2262 if ( entries == NULL ) {
2266 for ( k = 0; k < nEnt; k++ ) {
2267 slapi_entry_free( entries[k] );
2270 slapi_ch_free( (void **)&entries );
2271 #endif /* LDAP_SLAPI */
2276 * Internal API to prime a Slapi_PBlock with a Backend.
2278 static int initBackendPB( Slapi_PBlock *pb, Backend *be )
2282 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2283 if ( rc != LDAP_SUCCESS )
2287 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2288 if ( rc != LDAP_SUCCESS )
2292 return LDAP_SUCCESS;
2296 * If oldStyle is TRUE, then a value suitable for setting to
2297 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
2298 * (pointer to static storage).
2300 * If oldStyle is FALSE, then a value suitable for setting to
2301 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2302 * a pointer to allocated memory and will include the SASL
2303 * mechanism (if any).
2305 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2310 switch ( authz->sai_method ) {
2311 case LDAP_AUTH_SASL:
2313 authType = SLAPD_AUTH_SASL;
2315 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2316 authType = slapi_ch_malloc( len );
2317 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2320 case LDAP_AUTH_SIMPLE:
2321 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2323 case LDAP_AUTH_NONE:
2324 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2330 if ( is_tls && authType == NULL ) {
2331 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2338 * Internal API to prime a Slapi_PBlock with a Connection.
2340 static int initConnectionPB( Slapi_PBlock *pb, Connection *conn )
2345 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2346 if ( rc != LDAP_SUCCESS )
2349 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2350 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2351 if ( rc != LDAP_SUCCESS )
2353 } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2354 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2355 if ( rc != LDAP_SUCCESS )
2359 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2360 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2361 if ( rc != LDAP_SUCCESS )
2363 } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2364 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2365 if ( rc != LDAP_SUCCESS )
2369 #ifdef LDAP_CONNECTIONLESS
2370 rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2371 if ( rc != LDAP_SUCCESS )
2375 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2376 if ( rc != LDAP_SUCCESS )
2379 /* Returns pointer to static string */
2380 connAuthType = Authorization2AuthType( &conn->c_authz,
2387 if ( connAuthType != NULL ) {
2388 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2389 if ( rc != LDAP_SUCCESS )
2393 /* Returns pointer to allocated string */
2394 connAuthType = Authorization2AuthType( &conn->c_authz,
2401 if ( connAuthType != NULL ) {
2402 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2403 /* slapi_pblock_set dups this itself */
2404 slapi_ch_free( (void **)&connAuthType );
2405 if ( rc != LDAP_SUCCESS )
2409 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
2410 /* slapi_pblock_set dups this itself */
2411 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)conn->c_authz.sai_dn.bv_val);
2412 if ( rc != LDAP_SUCCESS )
2416 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SSF, (void *)conn->c_ssf);
2417 if ( rc != LDAP_SUCCESS )
2420 rc = slapi_pblock_set(pb, SLAPI_X_CONN_SASL_CONTEXT,
2421 ( conn->c_sasl_authctx != NULL ? conn->c_sasl_authctx :
2422 conn->c_sasl_sockctx ) );
2423 if ( rc != LDAP_SUCCESS )
2428 #endif /* LDAP_SLAPI */
2431 * Internal API to prime a Slapi_PBlock with an Operation.
2433 int slapi_x_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
2441 if ( op->o_bd != NULL ) {
2442 isRoot = be_isroot( op->o_bd, &op->o_ndn );
2443 isUpdateDn = be_isupdate( op->o_bd, &op->o_ndn );
2446 rc = initBackendPB( pb, op->o_bd );
2447 if ( rc != LDAP_SUCCESS )
2450 rc = initConnectionPB( pb, op->o_conn );
2451 if ( rc != LDAP_SUCCESS )
2454 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2455 if ( rc != LDAP_SUCCESS )
2458 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2459 if ( rc != LDAP_SUCCESS )
2462 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2463 if ( rc != LDAP_SUCCESS )
2466 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2467 if ( rc != LDAP_SUCCESS )
2470 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2471 if ( rc != LDAP_SUCCESS )
2474 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2475 if ( rc != LDAP_SUCCESS )
2478 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2479 if ( rc != LDAP_SUCCESS)
2482 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2483 if ( rc != LDAP_SUCCESS )
2486 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2487 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
2488 /* Not quite sure what the point of this is. */
2489 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
2490 if ( rc != LDAP_SUCCESS )
2494 return LDAP_SUCCESS;
2500 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2505 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2507 *isSSL = conn->c_is_tls;
2512 return LDAP_SUCCESS;
2515 #endif /* LDAP_SLAPI */
2519 * DS 5.x compatability API follow
2522 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2528 return LDAP_PARAM_ERROR;
2530 at = attr->a_desc->ad_type;
2532 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2534 if ( is_at_single_value( at ) )
2535 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2536 if ( is_at_operational( at ) )
2537 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2538 if ( is_at_obsolete( at ) )
2539 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2540 if ( is_at_collective( at ) )
2541 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2542 if ( is_at_no_user_mod( at ) )
2543 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2545 return LDAP_SUCCESS;
2548 #endif /* LDAP_SLAPI */
2551 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2554 unsigned long flags;
2556 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2558 return (flags & flag) ? 1 : 0;
2561 #endif /* LDAP_SLAPI */
2564 Slapi_Attr *slapi_attr_new( void )
2569 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2577 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2581 AttributeDescription *ad = NULL;
2583 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2599 void slapi_attr_free( Slapi_Attr **a )
2607 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2610 return attr_dup( (Slapi_Attr *)attr );
2616 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2620 * FIXME: here we may lose alignment between a_vals/a_nvals
2622 return value_add_one( &a->a_vals, (Slapi_Value *)v );
2628 int slapi_attr_type2plugin( const char *type, void **pi )
2635 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2638 if ( attr == NULL ) {
2639 return LDAP_PARAM_ERROR;
2642 *type = attr->a_desc->ad_cname.bv_val;
2644 return LDAP_SUCCESS;
2650 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2653 if ( attr == NULL ) {
2654 return LDAP_PARAM_ERROR;
2656 *oidp = attr->a_desc->ad_type->sat_oid;
2658 return LDAP_SUCCESS;
2664 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2672 mr = a->a_desc->ad_type->sat_equality;
2673 rc = value_match( &ret, a->a_desc, mr,
2674 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2675 (struct berval *)v1, (void *)v2, &text );
2676 if ( rc != LDAP_SUCCESS )
2679 return ( ret == 0 ) ? 0 : -1;
2685 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2695 if ( a ->a_vals == NULL ) {
2698 mr = a->a_desc->ad_type->sat_equality;
2699 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2700 rc = value_match( &ret, a->a_desc, mr,
2701 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2702 if ( rc != LDAP_SUCCESS ) {
2709 #endif /* LDAP_SLAPI */
2713 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2716 AttributeDescription *a1 = NULL;
2717 AttributeDescription *a2 = NULL;
2721 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2725 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2729 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2730 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2731 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2734 case SLAPI_TYPE_CMP_EXACT:
2735 ret = ad_cmp( a1, a2 );
2737 case SLAPI_TYPE_CMP_BASE:
2738 ret = ad_base_cmp( a1, a2 );
2740 case SLAPI_TYPE_CMP_SUBTYPE:
2741 ret = is_ad_subtype( a2, a2 );
2754 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2757 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2763 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2766 return slapi_valueset_first_value( &a->a_vals, v );
2772 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2775 return slapi_valueset_next_value( &a->a_vals, hint, v );
2781 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2784 *numValues = slapi_valueset_count( &a->a_vals );
2792 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2795 *vs = &((Slapi_Attr *)a)->a_vals;
2803 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2806 return slapi_attr_get_values( a, vals );
2812 char *slapi_attr_syntax_normalize( const char *s )
2815 AttributeDescription *ad = NULL;
2818 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2822 return ad->ad_cname.bv_val;
2828 Slapi_Value *slapi_value_new( void )
2833 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2841 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2844 return ber_dupbv( NULL, (struct berval *)bval );
2850 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2853 return slapi_value_new_berval( v );
2859 Slapi_Value *slapi_value_new_string(const char *s)
2864 bv.bv_val = (char *)s;
2865 bv.bv_len = strlen( s );
2867 return slapi_value_new_berval( &bv );
2873 Slapi_Value *slapi_value_init(Slapi_Value *val)
2885 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2888 return ber_dupbv( v, bval );
2894 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2897 v->bv_val = slapi_ch_strdup( (char *)s );
2898 v->bv_len = strlen( s );
2906 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2909 return slapi_value_new_value( v );
2915 void slapi_value_free(Slapi_Value **value)
2918 if ( value == NULL ) {
2922 if ( (*value) != NULL ) {
2923 slapi_ch_free( (void **)&(*value)->bv_val );
2924 slapi_ch_free( (void **)value );
2929 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2938 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2941 if ( value == NULL ) {
2944 if ( value->bv_val != NULL ) {
2945 slapi_ch_free( (void **)&value->bv_val );
2947 slapi_value_init_berval( value, (struct berval *)bval );
2955 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2958 if ( value == NULL ) {
2961 return slapi_value_set_berval( value, vfrom );
2967 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2970 if ( value == NULL ) {
2973 if ( value->bv_val != NULL ) {
2974 slapi_ch_free( (void **)&value->bv_val );
2976 value->bv_val = slapi_ch_malloc( len );
2977 value->bv_len = len;
2978 AC_MEMCPY( value->bv_val, val, len );
2986 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2989 if ( value == NULL ) {
2992 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2999 int slapi_value_set_int(Slapi_Value *value, int intVal)
3004 snprintf( buf, sizeof( buf ), "%d", intVal );
3006 return slapi_value_set_string( value, buf );
3012 const char *slapi_value_get_string(const Slapi_Value *value)
3015 if ( value == NULL ) {
3018 return value->bv_val;
3025 static int checkBVString(const struct berval *bv)
3029 for ( i = 0; i < bv->bv_len; i++ ) {
3030 if ( bv->bv_val[i] == '\0' )
3033 if ( bv->bv_val[i] != '\0' )
3040 int slapi_value_get_int(const Slapi_Value *value)
3043 if ( value == NULL ) return 0;
3044 if ( value->bv_val == NULL ) return 0;
3045 if ( !checkBVString( value ) ) return 0;
3047 return (int)strtol( value->bv_val, NULL, 10 );
3053 unsigned int slapi_value_get_uint(const Slapi_Value *value)
3056 if ( value == NULL ) return 0;
3057 if ( value->bv_val == NULL ) return 0;
3058 if ( !checkBVString( value ) ) return 0;
3060 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
3066 long slapi_value_get_long(const Slapi_Value *value)
3069 if ( value == NULL ) return 0;
3070 if ( value->bv_val == NULL ) return 0;
3071 if ( !checkBVString( value ) ) return 0;
3073 return strtol( value->bv_val, NULL, 10 );
3079 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
3082 if ( value == NULL ) return 0;
3083 if ( value->bv_val == NULL ) return 0;
3084 if ( !checkBVString( value ) ) return 0;
3086 return strtoul( value->bv_val, NULL, 10 );
3092 size_t slapi_value_get_length(const Slapi_Value *value)
3095 if ( value == NULL )
3098 return (size_t) value->bv_len;
3104 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
3107 return slapi_attr_value_cmp( a, v1, v2 );
3113 /* A ValueSet is a container for a BerVarray. */
3114 Slapi_ValueSet *slapi_valueset_new( void )
3119 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
3128 void slapi_valueset_free(Slapi_ValueSet *vs)
3134 ber_bvarray_free( vp );
3135 slapi_ch_free( (void **)&vp );
3142 void slapi_valueset_init(Slapi_ValueSet *vs)
3145 if ( vs != NULL && *vs == NULL ) {
3146 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
3147 (*vs)->bv_val = NULL;
3153 void slapi_valueset_done(Slapi_ValueSet *vs)
3161 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
3163 slapi_ch_free( (void **)&vp->bv_val );
3165 /* but don't free *vs or vs */
3169 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
3174 ber_dupbv( &bv, (Slapi_Value *)addval );
3175 ber_bvarray_add( vs, &bv );
3179 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
3182 return slapi_valueset_next_value( vs, 0, v );
3188 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
3199 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
3210 int slapi_valueset_count( const Slapi_ValueSet *vs )
3221 for ( i = 0; vp[i].bv_val != NULL; i++ )
3231 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
3236 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
3237 slapi_valueset_add_value( vs1, vp );
3242 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
3243 struct berval *val, int access )
3250 slap_access_t slap_access;
3251 AttributeDescription *ad = NULL;
3254 ret = slap_str2ad( attr, &ad, &text );
3255 if ( ret != LDAP_SUCCESS ) {
3259 switch ( access & SLAPI_ACL_ALL ) {
3260 case SLAPI_ACL_COMPARE:
3261 slap_access = ACL_COMPARE;
3263 case SLAPI_ACL_SEARCH:
3264 slap_access = ACL_SEARCH;
3266 case SLAPI_ACL_READ:
3267 slap_access = ACL_READ;
3269 case SLAPI_ACL_WRITE:
3270 case SLAPI_ACL_DELETE:
3272 case SLAPI_ACL_SELF:
3273 slap_access = ACL_WRITE;
3276 return LDAP_INSUFFICIENT_ACCESS;
3280 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3281 return LDAP_PARAM_ERROR;
3284 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3285 return LDAP_PARAM_ERROR;
3288 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3289 return LDAP_PARAM_ERROR;
3292 ret = access_allowed( op, e, ad, val, slap_access, NULL );
3294 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3296 return LDAP_UNWILLING_TO_PERFORM;
3300 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
3304 int rc = LDAP_SUCCESS;
3305 Modifications *ml, *mp;
3307 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3308 return LDAP_PARAM_ERROR;
3311 ml = slapi_x_ldapmods2modifications( mods );
3316 for ( mp = ml; mp != NULL; mp = mp->sml_next ) {
3317 rc = slap_bv2ad( &mp->sml_type, &mp->sml_desc, (const char **)errbuf );
3318 if ( rc != LDAP_SUCCESS ) {
3323 if ( rc == LDAP_SUCCESS ) {
3324 rc = acl_check_modlist( op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3327 /* Careful when freeing the modlist because it has pointers into the mods array. */
3328 for ( ; ml != NULL; ml = mp ) {
3331 /* just free the containing array */
3332 slapi_ch_free( (void **)&ml->sml_bvalues );
3333 slapi_ch_free( (void **)&ml );
3338 return LDAP_UNWILLING_TO_PERFORM;
3343 * Synthesise an LDAPMod array from a Modifications list to pass
3344 * to SLAPI. This synthesis is destructive and as such the
3345 * Modifications list may not be used after calling this
3348 * This function must also be called before slap_mods_check().
3350 LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
3353 Modifications *ml, *modlist;
3354 LDAPMod **mods, *modp;
3357 modlist = *pmodlist;
3359 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3362 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3364 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3365 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3367 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3369 /* Take ownership of original type. */
3370 modp->mod_type = ml->sml_type.bv_val;
3371 ml->sml_type.bv_val = NULL;
3373 if ( ml->sml_bvalues != NULL ) {
3374 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
3376 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3377 sizeof(struct berval *) );
3378 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
3379 /* Take ownership of original values. */
3380 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3381 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
3382 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
3383 ml->sml_bvalues[j].bv_len = 0;
3384 ml->sml_bvalues[j].bv_val = NULL;
3386 modp->mod_bvalues[j] = NULL;
3388 modp->mod_bvalues = NULL;
3395 slap_mods_free( modlist );
3405 * Convert a potentially modified array of LDAPMods back to a
3406 * Modification list.
3408 * The returned Modification list contains pointers into the
3409 * LDAPMods array; the latter MUST be freed with
3410 * slapi_x_free_ldapmods() (see below).
3412 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
3415 Modifications *modlist = NULL, **modtail;
3420 for( modp = mods; *modp != NULL; modp++ ) {
3424 struct berval **bvp;
3426 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3427 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3428 mod->sml_type.bv_val = (*modp)->mod_type;
3429 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3430 mod->sml_desc = NULL;
3431 mod->sml_next = NULL;
3433 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3434 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3437 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3442 mod->sml_bvalues = NULL;
3444 mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3446 /* NB: This implicitly trusts a plugin to return valid modifications. */
3447 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3448 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3449 mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
3450 mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
3453 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3454 mod->sml_bvalues[i].bv_val = *p;
3455 mod->sml_bvalues[i].bv_len = strlen( *p );
3458 mod->sml_bvalues[i].bv_val = NULL;
3460 mod->sml_nvalues = NULL;
3463 modtail = &mod->sml_next;
3473 * This function only frees the parts of the mods array that
3474 * are not shared with the Modification list that was created
3475 * by slapi_x_ldapmods2modifications().
3478 void slapi_x_free_ldapmods (LDAPMod **mods)
3486 for ( i = 0; mods[i] != NULL; i++ ) {
3488 * Don't free values themselves; they're owned by the
3489 * Modification list. Do free the containing array.
3491 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3492 for ( j = 0; mods[i]->mod_bvalues != NULL && mods[i]->mod_bvalues[j] != NULL; j++ ) {
3493 ch_free( mods[i]->mod_bvalues[j] );
3495 ch_free( mods[i]->mod_bvalues );
3497 ch_free( mods[i]->mod_values );
3499 /* Don't free type, for same reasons. */
3503 #endif /* LDAP_SLAPI */
3507 * Sun ONE DS 5.x computed attribute support. Computed attributes
3508 * allow for dynamically generated operational attributes, a very
3509 * useful thing indeed.
3513 * Write the computed attribute to a BerElement. Complementary
3514 * functions need to be defined for anything that replaces
3515 * op->o_callback->sc_sendentry, if you wish to make computed
3516 * attributes available to it.
3518 int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
3521 Operation *op = NULL;
3523 AttributeDescription *desc = NULL;
3539 rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
3540 if ( rc != 0 || op == NULL ) {
3544 ber = (BerElement *)c->cac_private;
3547 if ( c->cac_attrs == NULL ) {
3548 /* All attrs request, skip operational attributes */
3549 if ( is_at_operational( desc->ad_type ) ) {
3553 /* Specific attrs requested */
3554 if ( is_at_operational( desc->ad_type ) ) {
3555 if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3559 if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3565 if ( !access_allowed( op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
3566 slapi_log_error( SLAPI_LOG_ACL, "slapi_x_compute_output_ber",
3567 "acl: access to attribute %s not allowed\n",
3568 desc->ad_cname.bv_val );
3572 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
3574 slapi_log_error( SLAPI_LOG_BER, "slapi_x_compute_output_ber",
3575 "ber_printf failed\n");
3579 if ( !c->cac_attrsonly ) {
3580 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
3581 if ( !access_allowed( op, e,
3582 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
3583 slapi_log_error( SLAPI_LOG_ACL, "slapi_x_compute_output_ber",
3585 "acl: access to %s, value %d not allowed\n",
3586 op->o_connid, desc->ad_cname.bv_val, i );
3590 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
3591 slapi_log_error( SLAPI_LOG_BER, "slapi_x_compute_output_ber",
3592 "ber_printf failed\n");
3598 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
3599 slapi_log_error( SLAPI_LOG_BER, "slapi_x_compute_output_ber",
3600 "ber_printf failed\n" );
3611 * For some reason Sun don't use the normal plugin mechanism
3612 * registration path to register an "evaluator" function (an
3613 * "evaluator" is responsible for adding computed attributes;
3614 * the nomenclature is somewhat confusing).
3616 * As such slapi_compute_add_evaluator() registers the
3617 * function directly.
3619 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3622 Slapi_PBlock *pPlugin = NULL;
3625 pPlugin = slapi_pblock_new();
3626 if ( pPlugin == NULL ) {
3627 rc = LDAP_NO_MEMORY;
3631 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3632 if ( rc != LDAP_SUCCESS ) {
3636 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3637 if ( rc != LDAP_SUCCESS ) {
3641 rc = insertPlugin( NULL, pPlugin );
3648 if ( rc != LDAP_SUCCESS ) {
3649 if ( pPlugin != NULL ) {
3650 slapi_pblock_destroy( pPlugin );
3658 #endif /* LDAP_SLAPI */
3662 * See notes above regarding slapi_compute_add_evaluator().
3664 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3667 Slapi_PBlock *pPlugin = NULL;
3670 pPlugin = slapi_pblock_new();
3671 if ( pPlugin == NULL ) {
3672 rc = LDAP_NO_MEMORY;
3676 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3677 if ( rc != LDAP_SUCCESS ) {
3681 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3682 if ( rc != LDAP_SUCCESS ) {
3686 rc = insertPlugin( NULL, pPlugin );
3693 if ( rc != LDAP_SUCCESS ) {
3694 if ( pPlugin != NULL ) {
3695 slapi_pblock_destroy( pPlugin );
3703 #endif /* LDAP_SLAPI */
3707 * Call compute evaluators
3709 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3713 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3715 rc = getAllPluginFuncs( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3716 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3717 /* Nothing to do; front-end should ignore. */
3721 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3723 * -1: no attribute matched requested type
3724 * 0: one attribute matched
3725 * >0: error happened
3727 rc = (*pGetPlugin)( c, type, e, outputfn );
3733 slapi_ch_free( (void **)&tmpPlugin );
3738 #endif /* LDAP_SLAPI */
3741 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3747 rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3752 return doPluginFNs( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3755 #endif /* LDAP_SLAPI */
3759 * New API to provide the plugin with access to the search
3760 * pblock. Have informed Sun DS team.
3762 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3768 if ( c->cac_pb == NULL )
3776 #endif /* LDAP_SLAPI */
3779 Slapi_Mutex *slapi_new_mutex( void )
3784 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3785 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3786 slapi_ch_free( (void **)&m );
3796 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3799 if ( mutex != NULL ) {
3800 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3801 slapi_ch_free( (void **)&mutex);
3806 void slapi_lock_mutex( Slapi_Mutex *mutex )
3809 ldap_pvt_thread_mutex_lock( &mutex->mutex );
3813 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3816 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3822 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3827 if ( mutex == NULL ) {
3831 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3832 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3833 slapi_ch_free( (void **)&cv );
3837 /* XXX struct copy */
3838 cv->mutex = mutex->mutex;
3846 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3849 if ( cvar != NULL ) {
3850 ldap_pvt_thread_cond_destroy( &cvar->cond );
3851 slapi_ch_free( (void **)&cvar );
3856 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3859 if ( cvar == NULL ) {
3863 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3869 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3872 if ( cvar == NULL ) {
3877 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3880 return ldap_pvt_thread_cond_signal( &cvar->cond );
3886 int slapi_x_access_allowed( Operation *op,
3888 AttributeDescription *desc,
3890 slap_access_t access,
3891 AccessControlState *state )
3894 int rc, slap_access = 0;
3895 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3897 if ( op->o_pb == NULL ) {
3898 /* internal operation */
3904 slap_access |= SLAPI_ACL_ADD | SLAPI_ACL_DELETE | SLAPI_ACL_WRITE;
3907 slap_access |= SLAPI_ACL_READ;
3910 slap_access |= SLAPI_ACL_SEARCH;
3913 slap_access = ACL_COMPARE;
3919 rc = getAllPluginFuncs( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
3920 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3921 /* nothing to do; allowed access */
3925 slapi_x_pblock_set_operation( op->o_pb, op );
3927 rc = 1; /* default allow policy */
3929 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3934 rc = (*pGetPlugin)( op->o_pb, entry, desc->ad_cname.bv_val,
3935 val, slap_access, (void *)state );
3941 slapi_ch_free( (void **)&tmpPlugin );
3946 #endif /* LDAP_SLAPI */