2 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * (C) Copyright IBM Corp. 1997,2002
7 * Redistribution and use in source and binary forms are permitted
8 * provided that this notice is preserved and that due credit is
9 * given to IBM Corporation. This software is provided ``as is''
10 * without express or implied warranty.
13 * Portions (C) Copyright PADL Software Pty Ltd.
14 * Redistribution and use in source and binary forms are permitted
15 * provided that this notice is preserved and that due credit is
16 * given to PADL Software Pty Ltd. This software is provided ``as is''
17 * without express or implied warranty.
21 #include "slapi_common.h"
23 #include <ac/string.h>
32 struct berval *ns_get_supported_extop( int );
35 #include <sys/systeminfo.h>
41 * server start time (should we use a struct timeval also in slapd?
43 static struct timeval base_time;
44 ldap_pvt_thread_mutex_t slapi_hn_mutex;
45 ldap_pvt_thread_mutex_t slapi_time_mutex;
48 * This function converts an array of pointers to berval objects to
49 * an array of berval objects.
54 struct berval **bvptr,
57 int rc = LDAP_SUCCESS;
61 if ( bvptr == NULL || *bvptr == NULL ) {
65 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
69 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
70 if ( tmpberval == NULL ) {
71 return LDAP_NO_MEMORY;
74 for ( i = 0; bvptr[i] != NULL; i++ ) {
75 tmpberval[i].bv_val = bvptr[i]->bv_val;
76 tmpberval[i].bv_len = bvptr[i]->bv_len;
79 if ( rc == LDAP_SUCCESS ) {
91 #if defined(LDAP_SLAPI)
92 Slapi_Entry *e = NULL;
95 pTmpS = slapi_ch_strdup( s );
96 if ( pTmpS != NULL ) {
97 e = str2entry( pTmpS );
98 slapi_ch_free( (void **)&pTmpS );
102 #else /* !defined(LDAP_SLAPI) */
104 #endif /* !defined(LDAP_SLAPI) */
112 #if defined(LDAP_SLAPI)
115 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
116 ret = entry2str( e, len );
117 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
120 #else /* !defined(LDAP_SLAPI) */
122 #endif /* !defined(LDAP_SLAPI) */
126 slapi_entry_get_dn( Slapi_Entry *e )
128 #if defined(LDAP_SLAPI)
129 return e->e_name.bv_val;
130 #else /* !defined(LDAP_SLAPI) */
132 #endif /* !defined(LDAP_SLAPI) */
136 slapi_x_entry_get_id( Slapi_Entry *e )
138 #if defined(LDAP_SLAPI)
142 #endif /* !defined(LDAP_SLAPI) */
150 #if defined(LDAP_SLAPI)
151 struct berval dn = { 0, NULL };
154 dn.bv_len = strlen( ldn );
156 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
157 #endif /* defined(LDAP_SLAPI) */
161 slapi_entry_dup( Slapi_Entry *e )
163 #if defined(LDAP_SLAPI)
168 tmp = slapi_entry2str( e, &len );
170 return (Slapi_Entry *)NULL;
173 tmpEnt = (Slapi_Entry *)str2entry( tmp );
174 if ( tmpEnt == NULL ) {
175 slapi_ch_free( (void **)&tmp );
176 return (Slapi_Entry *)NULL;
180 slapi_ch_free( (void **)&tmp );
184 #else /* !defined(LDAP_SLAPI) */
186 #endif /* !defined(LDAP_SLAPI) */
190 slapi_entry_attr_delete(
194 #if defined(LDAP_SLAPI)
195 AttributeDescription *ad = NULL;
198 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
199 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
202 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
203 return 0; /* attribute is deleted */
205 return -1; /* something went wrong */
207 #else /* !defined(LDAP_SLAPI) */
209 #endif /* !defined(LDAP_SLAPI) */
213 slapi_entry_alloc( void )
215 #if defined(LDAP_SLAPI)
216 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
217 #else /* !defined(LDAP_SLAPI) */
219 #endif /* !defined(LDAP_SLAPI) */
223 slapi_entry_free( Slapi_Entry *e )
225 #if defined(LDAP_SLAPI)
227 #endif /* defined(LDAP_SLAPI) */
231 slapi_entry_attr_merge(
234 struct berval **vals )
236 #if defined(LDAP_SLAPI)
237 AttributeDescription *ad = NULL;
242 rc = bvptr2obj( vals, &bv );
243 if ( rc != LDAP_SUCCESS ) {
247 rc = slap_str2ad( type, &ad, &text );
248 if ( rc != LDAP_SUCCESS ) {
252 rc = attr_merge( e, ad, bv );
256 #else /* !defined(LDAP_SLAPI) */
258 #endif /* !defined(LDAP_SLAPI) */
262 slapi_entry_attr_find(
267 #if defined(LDAP_SLAPI)
268 AttributeDescription *ad = NULL;
272 rc = slap_str2ad( type, &ad, &text );
273 if ( rc != LDAP_SUCCESS ) {
277 *attr = attr_find( e->e_attrs, ad );
278 if ( *attr == NULL ) {
283 #else /* !defined(LDAP_SLAPI) */
285 #endif /* !defined(LDAP_SLAPI) */
289 * FIXME -- The caller must free the allocated memory.
290 * In Netscape they do not have to.
293 slapi_attr_get_values(
295 struct berval ***vals )
297 #if defined(LDAP_SLAPI)
301 if ( attr == NULL ) {
305 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
309 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
310 for ( j = 0; j < i; j++ ) {
311 bv[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
312 bv[j]->bv_val = ch_strdup( attr->a_vals[j].bv_val );
313 bv[j]->bv_len = attr->a_vals[j].bv_len;
317 *vals = (struct berval **)bv;
320 #else /* !defined(LDAP_SLAPI) */
322 #endif /* !defined(LDAP_SLAPI) */
326 slapi_dn_normalize( char *dn )
328 #if defined(LDAP_SLAPI)
332 assert( dn != NULL );
335 bdn.bv_len = strlen( dn );
337 dnNormalize2( NULL, &bdn, &ndn );
340 * FIXME: ain't it safe to set dn = ndn.bv_val ?
342 dn = ch_strdup( ndn.bv_val );
343 ch_free( ndn.bv_val );
346 #else /* !defined(LDAP_SLAPI) */
348 #endif /* !defined(LDAP_SLAPI) */
352 * FIXME: this function is dangerous and should be deprecated;
353 * DN normalization is a lot more than lower-casing, and BTW
354 * OpenLDAP's DN normalization for case insensitive attributes
355 * is already lower case
358 slapi_dn_normalize_case( char *dn )
360 #if defined(LDAP_SLAPI)
361 slapi_dn_normalize( dn );
362 ldap_pvt_str2lower( dn );
365 #else /* defined(LDAP_SLAPI) */
367 #endif /* defined(LDAP_SLAPI) */
375 #if defined(LDAP_SLAPI)
376 struct berval bdn, ndn;
377 struct berval bsuffix, nsuffix;
379 assert( dn != NULL );
380 assert( suffix != NULL );
383 bdn.bv_len = strlen( dn );
385 bsuffix.bv_val = suffix;
386 bsuffix.bv_len = strlen( suffix );
388 dnNormalize2( NULL, &bdn, &ndn );
389 dnNormalize2( NULL, &bsuffix, &nsuffix );
391 return dnIsSuffix( &ndn, &nsuffix );
392 #else /* !defined(LDAP_SLAPI) */
394 #endif /* !defined(LDAP_SLAPI) */
398 slapi_dn_ignore_case( char *dn )
400 #if defined(LDAP_SLAPI)
401 return slapi_dn_normalize_case( dn );
402 #else /* !defined(LDAP_SLAPI) */
404 #endif /* !defined(LDAP_SLAPI) */
408 slapi_ch_malloc( unsigned long size )
410 #if defined(LDAP_SLAPI)
411 return ch_malloc( size );
412 #else /* !defined(LDAP_SLAPI) */
414 #endif /* !defined(LDAP_SLAPI) */
418 slapi_ch_free( void **ptr )
420 #if defined(LDAP_SLAPI)
423 #endif /* defined(LDAP_SLAPI) */
427 slapi_ch_free_string( char **ptr )
429 #if defined(LDAP_SLAPI)
430 slapi_ch_free( (void **)ptr );
431 #endif /* defined(LDAP_SLAPI) */
439 #if defined(LDAP_SLAPI)
440 return ch_calloc( nelem, size );
441 #else /* !defined(LDAP_SLAPI) */
443 #endif /* !defined(LDAP_SLAPI) */
451 #if defined(LDAP_SLAPI)
452 return ch_realloc( block, size );
453 #else /* !defined(LDAP_SLAPI) */
455 #endif /* !defined(LDAP_SLAPI) */
459 slapi_ch_strdup( char *s )
461 #if defined(LDAP_SLAPI)
462 return ch_strdup( (const char *)s );
463 #else /* !defined(LDAP_SLAPI) */
465 #endif /* !defined(LDAP_SLAPI) */
469 slapi_ch_stlen( char *s )
471 #if defined(LDAP_SLAPI)
472 return strlen( (const char *)s );
473 #else /* !defined(LDAP_SLAPI) */
475 #endif /* !defined(LDAP_SLAPI) */
479 slapi_control_present(
480 LDAPControl **controls,
485 #if defined(LDAP_SLAPI)
497 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
498 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
503 if ( controls[i]->ldctl_value.bv_len != 0 ) {
505 * FIXME: according to 6.1 specification,
506 * "The val output parameter is set
507 * to point into the controls array.
508 * A copy of the control value is
512 struct berval *pTmpBval;
514 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
515 if ( pTmpBval == NULL ) {
518 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
519 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
523 slapi_ch_free( (void **)&pTmpBval );
529 *val = &controls[i]->ldctl_value;
534 *iscritical = controls[i]->ldctl_iscritical;
541 #else /* !defined(LDAP_SLAPI) */
543 #endif /* !defined(LDAP_SLAPI) */
547 slapi_register_supported_control(
549 unsigned long controlops )
551 #if defined(LDAP_SLAPI)
552 /* FIXME -- can not add controls to openLDAP dynamically */
553 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_CONTROLS",
554 "can not add controls to openLDAP dynamically\n" );
555 #endif /* defined(LDAP_SLAPI) */
559 slapi_get_supported_controls(
561 unsigned long **ctrlopsp )
563 #if defined(LDAP_SLAPI)
567 unsigned long *masks = NULL;
569 for (n = 0; get_supported_ctrl( n ) != NULL; n++) {
581 oids = (char **)slapi_ch_malloc( (n + 1) * sizeof(char *) );
582 if ( oids == NULL ) {
587 masks = (unsigned long *)slapi_ch_malloc( n * sizeof(int) );
588 if ( masks == NULL ) {
593 for ( i = 0; i < n; i++ ) {
595 * FIXME: Netscape's specification says nothing about
596 * memory; should we copy the OIDs or return pointers
597 * to internal values? In OpenLDAP the latter is safe
598 * since we do not allow to register coltrols runtime
600 oids[ i ] = ch_strdup( get_supported_ctrl( i ) );
601 if ( oids[ i ] == NULL ) {
605 masks[ i ] = (unsigned long)get_supported_ctrl_mask( i );
613 if ( rc != LDAP_SUCCESS ) {
614 for ( i = 0; oids != NULL && oids[ i ] != NULL; i++ ) {
615 ch_free( oids[ i ] );
622 #else /* !defined(LDAP_SLAPI) */
624 #endif /* !defined(LDAP_SLAPI) */
628 slapi_register_supported_saslmechanism( char *mechanism )
630 #if defined(LDAP_SLAPI)
631 /* FIXME -- can not add saslmechanism to openLDAP dynamically */
632 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
633 "can not add saslmechanism to openLDAP dynamically\n" );
634 #endif /* defined(LDAP_SLAPI) */
638 slapi_get_supported_saslmechanisms( void )
640 #if defined(LDAP_SLAPI)
641 /* FIXME -- can not get the saslmechanism wihtout a connection. */
642 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
643 "can not get the saslmechanism "
644 "wihtout a connection\n" );
646 #else /* defined(LDAP_SLAPI) */
648 #endif /* defined(LDAP_SLAPI) */
652 slapi_get_supported_extended_ops( void )
654 #if defined(LDAP_SLAPI)
656 char **ppExtOpOID = NULL;
659 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
663 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
668 if ( numExtOps == 0 ) {
672 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
673 for ( k = 0; k < i; k++ ) {
676 bv = get_supported_extop( k );
677 assert( bv != NULL );
679 ppExtOpOID[ k ] = bv->bv_val;
682 for ( ; k < j; k++ ) {
685 bv = ns_get_supported_extop( k );
686 assert( bv != NULL );
688 ppExtOpOID[ i + k ] = bv->bv_val;
690 ppExtOpOID[ i + k ] = NULL;
693 #else /* !defined(LDAP_SLAPI) */
695 #endif /* !defined(LDAP_SLAPI) */
699 slapi_send_ldap_result(
705 struct berval **urls )
707 #if defined(LDAP_SLAPI)
712 struct berval *extValue = NULL;
715 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
716 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
717 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
718 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, &s );
719 rc = LDAP_SASL_BIND_IN_PROGRESS;
720 send_ldap_sasl( conn, op, rc, NULL, NULL, NULL, NULL, s );
724 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
725 if ( extOID != NULL ) {
726 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &extValue );
727 slapi_send_ldap_extended_response( conn, op, err, extOID,
732 send_ldap_result( conn, op, err, matched, text, NULL, NULL );
733 #endif /* defined(LDAP_SLAPI) */
737 slapi_send_ldap_search_entry(
740 LDAPControl **ectrls,
744 #if defined(LDAP_SLAPI)
751 AttributeName *an = NULL;
754 for ( i = 0; attrs[ i ] != NULL; i++ ) {
759 an = (AttributeName *) ch_malloc( i * sizeof(AttributeName) );
760 for ( i = 0; attrs[i] != NULL; i++ ) {
761 an[i].an_name.bv_val = ch_strdup( attrs[i] );
762 an[i].an_name.bv_len = strlen( attrs[i] );
763 an[i].an_desc = NULL;
764 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
769 if ( ( rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) ||
770 ( rc = slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&pConn) != 0 ) ||
771 ( rc = slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp) != 0 ) ) {
774 rc = send_search_entry( be, pConn, pOp, e, an, attrsonly, NULL );
779 #else /* !defined(LDAP_SLAPI) */
781 #endif /* !defined(LDAP_SLAPI) */
786 slapi_str2filter( char *str )
788 #if defined(LDAP_SLAPI)
789 return str2filter( str );
790 #else /* !defined(LDAP_SLAPI) */
792 #endif /* !defined(LDAP_SLAPI) */
800 #if defined(LDAP_SLAPI)
802 #endif /* defined(LDAP_SLAPI) */
806 slapi_filter_get_choice( Slapi_Filter *f )
808 #if defined(LDAP_SLAPI)
818 #else /* !defined(LDAP_SLAPI) */
819 return -1; /* invalid filter type */
820 #endif /* !defined(LDAP_SLAPI) */
824 slapi_filter_get_ava(
827 struct berval **bval )
829 #if defined(LDAP_SLAPI)
831 int rc = LDAP_SUCCESS;
833 assert( type != NULL );
834 assert( bval != NULL );
840 if ( ftype == LDAP_FILTER_EQUALITY
841 || ftype == LDAP_FILTER_GE
842 || ftype == LDAP_FILTER_LE
843 || ftype == LDAP_FILTER_APPROX ) {
844 *type = slapi_ch_strdup( f->f_un.f_un_ava->aa_desc->ad_cname.bv_val );
845 if ( *type == NULL ) {
850 *bval = (struct berval *)slapi_ch_malloc( sizeof(struct berval) );
851 if ( *bval == NULL ) {
856 (*bval)->bv_len = f->f_un.f_un_ava->aa_value.bv_len;
857 (*bval)->bv_val = slapi_ch_strdup( f->f_un.f_un_ava->aa_value.bv_val );
858 if ( (*bval)->bv_val == NULL ) {
862 } else { /* filter type not supported */
867 if ( rc != LDAP_SUCCESS ) {
880 #else /* !defined(LDAP_SLAPI) */
882 #endif /* !defined(LDAP_SLAPI) */
886 slapi_filter_list_first( Slapi_Filter *f )
888 #if defined(LDAP_SLAPI)
896 if ( ftype == LDAP_FILTER_AND
897 || ftype == LDAP_FILTER_OR
898 || ftype == LDAP_FILTER_NOT ) {
899 return (Slapi_Filter *)f->f_and;
903 #else /* !defined(LDAP_SLAPI) */
905 #endif /* !defined(LDAP_SLAPI) */
909 slapi_filter_list_next(
911 Slapi_Filter *fprev )
913 #if defined(LDAP_SLAPI)
921 if ( ftype == LDAP_FILTER_AND
922 || ftype == LDAP_FILTER_OR
923 || ftype == LDAP_FILTER_NOT ) {
924 if ( f->f_and == fprev ) {
925 return f->f_and->f_next;
930 #else /* !defined(LDAP_SLAPI) */
932 #endif /* !defined(LDAP_SLAPI) */
936 slapi_send_ldap_extended_response(
941 struct berval *response )
943 #if defined(LDAP_SLAPI)
944 send_ldap_extended( conn,op, errornum, NULL, NULL, NULL,
945 respName,response, NULL );
947 #else /* !defined(LDAP_SLAPI) */
949 #endif /* !defined(LDAP_SLAPI) */
954 struct berval **vals,
957 #if defined(LDAP_SLAPI)
959 * FIXME: what's the point?
962 #else /* !defined(LDAP_SLAPI) */
964 #endif /* !defined(LDAP_SLAPI) */
968 slapi_get_hostname( void )
970 #if defined(LDAP_SLAPI)
974 * FIXME: I'd prefer a different check ...
977 hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
979 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
980 "can't malloc memory for hostname\n" );
983 } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
984 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
985 "can't get hostname\n" );
986 slapi_ch_free( (void **)&hn );
990 static int been_here = 0;
991 static char *static_hn = NULL;
993 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
995 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
996 if ( static_hn == NULL) {
997 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
998 "can't malloc memory for hostname\n" );
1000 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1005 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1006 slapi_log_error( SLAPI_LOG_FATAL,
1008 "can't get hostname\n" );
1009 slapi_ch_free( (void **)&static_hn );
1011 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1020 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1022 hn = ch_strdup( static_hn );
1023 #endif /* !_SPARC */
1026 #else /* !defined(LDAP_SLAPI) */
1028 #endif /* !defined(LDAP_SLAPI) */
1032 * FIXME: this should go in an appropriate header ...
1034 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
1043 #if defined(LDAP_SLAPI)
1044 int rc = LDAP_SUCCESS;
1047 va_start( arglist, fmt );
1048 rc = vLogError( severity, subsystem, fmt, arglist );
1052 #else /* !defined(LDAP_SLAPI) */
1054 #endif /* !defined(LDAP_SLAPI) */
1059 slapi_timer_current_time( void )
1061 #if defined(LDAP_SLAPI)
1062 static int first_time = 1;
1063 #if !defined (_WIN32)
1067 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1070 gettimeofday( &base_time, NULL );
1072 gettimeofday( &now, NULL );
1073 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1074 (now.tv_usec - base_time.tv_usec);
1075 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1081 return (slap_get_time() - starttime) * 1000000;
1088 performance_counter_present = QueryPerformanceCounter( &base_time );
1089 QueryPerformanceFrequency( &performance_freq );
1092 if ( !performance_counter_present )
1095 QueryPerformanceCounter( &now );
1096 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1098 #else /* !defined(LDAP_SLAPI) */
1100 #endif /* !defined(LDAP_SLAPI) */
1107 slapi_timer_get_time( char *label )
1109 #if defined(LDAP_SLAPI)
1110 unsigned long start = slapi_timer_current_time();
1111 printf("%10ld %10ld usec %s\n", start, 0, label);
1113 #else /* !defined(LDAP_SLAPI) */
1115 #endif /* !defined(LDAP_SLAPI) */
1122 slapi_timer_elapsed_time(
1124 unsigned long start )
1126 #if defined(LDAP_SLAPI)
1127 unsigned long stop = slapi_timer_current_time();
1128 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
1129 #endif /* defined(LDAP_SLAPI) */
1133 slapi_free_search_results_internal( Slapi_PBlock *pb )
1135 #if defined(LDAP_SLAPI)
1136 Slapi_Entry **entries;
1137 int k = 0, nEnt = 0;
1139 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
1140 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
1145 if ( entries == NULL ) {
1149 for ( k = 0; k < nEnt; k++ ) {
1150 slapi_entry_free( entries[k] );
1153 slapi_ch_free( (void **)&entries );
1154 #endif /* defined(LDAP_SLAPI) */
1158 * Internal API to prime a Slapi_PBlock with a Backend.
1160 int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
1162 #if defined(LDAP_SLAPI)
1165 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
1166 if ( rc != LDAP_SUCCESS )
1170 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
1171 if ( rc != LDAP_SUCCESS )
1175 return LDAP_SUCCESS;
1178 #endif /* defined(LDAP_SLAPI) */
1181 #if defined(LDAP_SLAPI)
1183 * If oldStyle is TRUE, then a value suitable for setting to
1184 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
1185 * (pointer to static storage).
1187 * If oldStyle is FALSE, then a value suitable for setting to
1188 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
1189 * a pointer to allocated memory and will include the SASL
1190 * mechanism (if any).
1192 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
1197 switch ( authz->sai_method ) {
1198 case LDAP_AUTH_SASL:
1200 authType = SLAPD_AUTH_SASL;
1202 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
1203 authType = slapi_ch_malloc( len );
1204 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
1207 case LDAP_AUTH_SIMPLE:
1208 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
1210 case LDAP_AUTH_NONE:
1211 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
1217 if ( is_tls && authType == NULL ) {
1218 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
1226 * Internal API to prime a Slapi_PBlock with a Connection.
1228 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
1230 #if defined(LDAP_SLAPI)
1234 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
1235 if ( rc != LDAP_SUCCESS )
1238 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
1239 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
1240 if ( rc != LDAP_SUCCESS )
1244 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
1245 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
1246 if ( rc != LDAP_SUCCESS )
1250 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
1251 if ( rc != LDAP_SUCCESS )
1254 /* Returns pointer to static string */
1255 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
1256 if ( connAuthType != NULL ) {
1257 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
1258 if ( rc != LDAP_SUCCESS )
1262 /* Returns pointer to allocated string */
1263 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
1264 if ( connAuthType != NULL ) {
1265 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
1266 if ( rc != LDAP_SUCCESS )
1270 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
1271 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
1272 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
1273 if ( rc != LDAP_SUCCESS )
1280 #endif /* defined(LDAP_SLAPI) */
1284 * Internal API to prime a Slapi_PBlock with an Operation.
1286 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
1288 #if defined(LDAP_SLAPI)
1295 if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
1299 isRoot = be_isroot( be, &op->o_ndn );
1300 isUpdateDn = be_isupdate( be, &op->o_ndn );
1303 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
1304 if ( rc != LDAP_SUCCESS )
1307 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
1308 if ( rc != LDAP_SUCCESS )
1311 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
1312 if ( rc != LDAP_SUCCESS )
1315 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
1316 if ( rc != LDAP_SUCCESS )
1319 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
1320 if ( rc != LDAP_SUCCESS )
1323 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
1324 if ( rc != LDAP_SUCCESS )
1327 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
1328 if ( rc != LDAP_SUCCESS)
1331 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
1332 if ( rc != LDAP_SUCCESS )
1335 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
1336 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
1337 /* Not quite sure what the point of this is. */
1338 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
1339 if ( rc != LDAP_SUCCESS )
1343 return LDAP_SUCCESS;
1349 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
1351 #if defined( LDAP_SLAPI )
1354 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1355 *isSSL = conn->c_is_tls;
1357 return LDAP_SUCCESS;
1360 #endif /* defined(LDAP_SLAPI) */
1364 * DS 5.x compatability API follow
1367 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
1369 #if defined( LDAP_SLAPI )
1373 return LDAP_PARAM_ERROR;
1375 at = attr->a_desc->ad_type;
1377 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
1379 if ( is_at_single_value( at ) )
1380 *flags |= SLAPI_ATTR_FLAG_SINGLE;
1381 if ( is_at_operational( at ) )
1382 *flags |= SLAPI_ATTR_FLAG_OPATTR;
1383 if ( is_at_obsolete( at ) )
1384 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
1385 if ( is_at_collective( at ) )
1386 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
1387 if ( is_at_no_user_mod( at ) )
1388 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
1390 return LDAP_SUCCESS;
1393 #endif /* defined(LDAP_SLAPI) */
1396 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
1398 #if defined( LDAP_SLAPI )
1399 unsigned long flags;
1401 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
1403 return (flags & flag) ? 1 : 0;
1406 #endif /* defined(LDAP_SLAPI) */
1409 Slapi_Attr *slapi_attr_new( void )
1414 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
1422 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
1426 AttributeDescription *ad = NULL;
1428 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
1443 void slapi_attr_free( Slapi_Attr **a )
1451 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
1454 return attr_dup( (Slapi_Attr *)attr );
1460 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
1463 return value_add_one( &a->a_vals, (Slapi_Value *)v );
1469 int slapi_attr_type2plugin( const char *type, void **pi )
1476 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
1479 if ( attr == NULL ) {
1480 return LDAP_PARAM_ERROR;
1483 *type = attr->a_desc->ad_cname.bv_val;
1485 return LDAP_SUCCESS;
1491 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
1494 if ( attr == NULL ) {
1495 return LDAP_PARAM_ERROR;
1497 *oidp = attr->a_desc->ad_type->sat_oid;
1499 return LDAP_SUCCESS;
1505 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
1513 mr = a->a_desc->ad_type->sat_equality;
1514 rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
1515 (struct berval *)v1, (void *)v2, &text );
1516 if ( rc != LDAP_SUCCESS )
1519 return ( ret == 0 ) ? 0 : -1;
1525 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
1535 mr = a->a_desc->ad_type->sat_equality;
1536 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
1537 rc = value_match( &ret, a->a_desc, mr,
1538 SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
1539 if ( rc != LDAP_SUCCESS ) {
1550 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
1553 AttributeDescription *a1 = NULL;
1554 AttributeDescription *a2 = NULL;
1558 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
1562 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
1566 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
1567 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
1568 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
1571 case SLAPI_TYPE_CMP_EXACT:
1572 ret = ad_cmp( a1, a2 );
1574 case SLAPI_TYPE_CMP_BASE:
1575 ret = ad_base_cmp( a1, a2 );
1577 case SLAPI_TYPE_CMP_SUBTYPE:
1578 ret = is_ad_subtype( a2, a2 );
1591 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
1594 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
1600 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
1603 return slapi_valueset_first_value( &a->a_vals, v );
1609 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
1612 return slapi_valueset_next_value( &a->a_vals, hint, v );
1618 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
1621 *numValues = slapi_valueset_count( &a->a_vals );
1629 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
1632 *vs = &((Slapi_Attr *)a)->a_vals;
1640 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
1643 return slapi_attr_get_values( a, vals );
1649 char *slapi_attr_syntax_normalize( const char *s )
1652 AttributeDescription *ad = NULL;
1655 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
1659 return ad->ad_cname.bv_val;
1665 Slapi_Value *slapi_value_new( void )
1670 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
1678 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
1681 return ber_dupbv( NULL, (struct berval *)bval );
1687 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
1690 return slapi_value_new_berval( v );
1696 Slapi_Value *slapi_value_new_string(const char *s)
1701 bv.bv_val = (char *)s;
1702 bv.bv_len = strlen( s );
1704 return slapi_value_new_berval( &bv );
1710 Slapi_Value *slapi_value_init(Slapi_Value *val)
1722 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
1725 return ber_dupbv( v, bval );
1731 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
1734 v->bv_val = slapi_ch_strdup( (char *)s );
1735 v->bv_len = strlen( s );
1743 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
1746 return slapi_value_new_value( v );
1752 void slapi_value_free(Slapi_Value **value)
1755 if ( value == NULL ) {
1759 if ( (*value) != NULL ) {
1760 slapi_ch_free( (void **)&(*value)->bv_val );
1761 slapi_ch_free( (void **)value );
1766 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
1775 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
1778 if ( value == NULL ) {
1781 if ( value->bv_val != NULL ) {
1782 slapi_ch_free( (void **)&value->bv_val );
1784 slapi_value_init_berval( value, (struct berval *)bval );
1792 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
1795 if ( value == NULL ) {
1798 return slapi_value_set_berval( value, vfrom );
1804 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
1807 if ( value == NULL ) {
1810 if ( value->bv_val != NULL ) {
1811 slapi_ch_free( (void **)&value->bv_val );
1813 value->bv_val = slapi_ch_malloc( len );
1814 value->bv_len = len;
1815 AC_MEMCPY( value->bv_val, val, len );
1823 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
1826 if ( value == NULL ) {
1829 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
1836 int slapi_value_set_int(Slapi_Value *value, int intVal)
1841 snprintf( buf, sizeof( buf ), "%d", intVal );
1843 return slapi_value_set_string( value, buf );
1849 const char *slapi_value_get_string(const Slapi_Value *value)
1852 if ( value == NULL ) {
1855 return value->bv_val;
1862 static int checkBVString(const struct berval *bv)
1866 for ( i = 0; i < bv->bv_len; i++ ) {
1867 if ( bv->bv_val[i] == '\0' )
1870 if ( bv->bv_val[i] != '\0' )
1877 int slapi_value_get_int(const Slapi_Value *value)
1880 if ( value == NULL ) return 0;
1881 if ( !checkBVString( value ) ) return 0;
1883 return (int)strtol( value->bv_val, NULL, 10 );
1889 unsigned int slapi_value_get_uint(const Slapi_Value *value)
1892 if ( value == NULL ) return 0;
1893 if ( !checkBVString( value ) ) return 0;
1895 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
1901 long slapi_value_get_long(const Slapi_Value *value)
1904 if ( value == NULL ) return 0;
1905 if ( !checkBVString( value ) ) return 0;
1907 return strtol( value->bv_val, NULL, 10 );
1913 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
1916 if ( value == NULL ) return 0;
1917 if ( !checkBVString( value ) ) return 0;
1919 return strtoul( value->bv_val, NULL, 10 );
1925 size_t slapi_value_get_length(const Slapi_Value *value)
1928 if ( value == NULL )
1931 return (size_t) value->bv_len;
1937 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
1940 return slapi_attr_value_cmp( a, v1, v2 );
1946 /* A ValueSet is a container for a BerVarray. */
1947 Slapi_ValueSet *slapi_valueset_new( void )
1952 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
1961 void slapi_valueset_free(Slapi_ValueSet *vs)
1967 ber_bvarray_free( vp );
1968 slapi_ch_free( (void **)&vp );
1975 void slapi_valueset_init(Slapi_ValueSet *vs)
1978 if ( vs != NULL && *vs == NULL ) {
1979 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
1980 (*vs)->bv_val = NULL;
1986 void slapi_valueset_done(Slapi_ValueSet *vs)
1994 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
1996 slapi_ch_free( (void **)&vp->bv_val );
1998 /* but don't free *vs or vs */
2002 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2007 ber_dupbv( &bv, (Slapi_Value *)addval );
2008 ber_bvarray_add( vs, &bv );
2012 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2015 return slapi_valueset_next_value( vs, 0, v );
2021 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2032 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2043 int slapi_valueset_count( const Slapi_ValueSet *vs )
2054 for ( i = 0; vp[i].bv_val != NULL; i++ )
2064 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2069 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2070 slapi_valueset_add_value( vs1, vp );
2075 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
2076 struct berval *val, int access )
2083 slap_access_t slap_access;
2084 AttributeDescription *ad = NULL;
2087 ret = slap_str2ad( attr, &ad, &text );
2088 if ( ret != LDAP_SUCCESS ) {
2092 switch ( access & SLAPI_ACL_ALL ) {
2093 case SLAPI_ACL_COMPARE:
2094 slap_access = ACL_COMPARE;
2096 case SLAPI_ACL_SEARCH:
2097 slap_access = ACL_SEARCH;
2099 case SLAPI_ACL_READ:
2100 slap_access = ACL_READ;
2102 case SLAPI_ACL_WRITE:
2103 case SLAPI_ACL_DELETE:
2105 case SLAPI_ACL_SELF:
2106 slap_access = ACL_WRITE;
2109 return LDAP_INSUFFICIENT_ACCESS;
2113 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2114 return LDAP_PARAM_ERROR;
2117 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
2118 return LDAP_PARAM_ERROR;
2121 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
2122 return LDAP_PARAM_ERROR;
2125 ret = access_allowed( be, conn, op, e, desc, val, slap_access, NULL );
2127 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2129 return LDAP_UNWILLING_TO_PERFORM;
2133 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
2141 Modifications *next;
2143 if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2144 return LDAP_PARAM_ERROR;
2147 if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
2148 return LDAP_PARAM_ERROR;
2151 if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
2152 return LDAP_PARAM_ERROR;
2155 ml = slapi_x_ldapmods2modifications( mods );
2160 ret = acl_check_modlist( be, conn, op, e, ml );
2162 /* Careful when freeing the modlist because it has pointers into the mods array. */
2163 for ( ; ml != NULL; ml = next ) {
2164 next = ml->sml_next;
2166 /* just free the containing array */
2167 slapi_ch_free( (void **)&ml->sml_bvalues );
2168 slapi_ch_free( (void **)&ml );
2171 return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2173 return LDAP_UNWILLING_TO_PERFORM;
2178 * Attribute sets are an OpenLDAP extension for the
2179 * virtual operational attribute coalescing plugin
2181 * Subject to going away very soon; do not use
2183 Slapi_AttrSet *slapi_x_attrset_new( void )
2189 * Like a Slapi_ValueSet, a Slapi_AttrSet is a container
2190 * for objects: we need this because it may be initially
2193 a = (Slapi_AttrSet *)slapi_ch_malloc( sizeof( *a ) );
2202 Slapi_AttrSet *slapi_x_attrset_init( Slapi_AttrSet *as, Slapi_Attr *a )
2214 void slapi_x_attrset_free( Slapi_AttrSet **pAs )
2217 Slapi_AttrSet *as = *pAs;
2221 slapi_ch_free( (void **)pAs );
2226 Slapi_AttrSet *slapi_x_attrset_dup( Slapi_AttrSet *as )
2229 Slapi_AttrSet *newAs = slapi_x_attrset_new();
2232 *newAs = attrs_dup( *as );
2240 int slapi_x_attrset_add_attr( Slapi_AttrSet *as, Slapi_Attr *a )
2243 Slapi_Attr *nextAttr;
2245 if ( as == NULL || a == NULL )
2248 if ( *as == NULL ) {
2249 /* First attribute */
2253 /* Non-first attribute */
2254 nextAttr = (*as)->a_next;
2258 a->a_next = nextAttr;
2266 int slapi_x_attrset_add_attr_copy( Slapi_AttrSet *as, Slapi_Attr *a )
2271 adup = slapi_attr_dup( a );
2272 return slapi_x_attrset_add_attr( as, adup );
2278 int slapi_x_attrset_find( Slapi_AttrSet *as, const char *type, Slapi_Attr **attr )
2281 AttributeDescription *ad = NULL;
2284 if ( as == NULL || *as == NULL ) {
2288 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2291 *attr = attrs_find( *as, ad );
2292 return ( *attr == NULL ) ? -1 : 0;
2298 int slapi_x_attrset_merge( Slapi_AttrSet *as, const char *type, Slapi_ValueSet *vals )
2301 AttributeDescription *ad = NULL;
2305 if ( vals == NULL || *vals == NULL ) {
2306 /* Must have something to add. */
2310 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2314 for ( a = as; *a != NULL; a = &(*a)->a_next ) {
2315 if ( ad_cmp( (*a)->a_desc, ad ) == 0 ) {
2321 *a = (Slapi_Attr *) slapi_ch_malloc( sizeof(Attribute) );
2323 (*a)->a_vals = NULL;
2324 (*a)->a_next = NULL;
2328 return value_add ( &(*a)->a_vals, *vals );
2334 int slapi_x_attrset_merge_bervals( Slapi_AttrSet *as, const char *type, struct berval **vals )
2340 if ( bvptr2obj( vals, &vp ) != LDAP_SUCCESS ) {
2343 rc = slapi_x_attrset_merge( as, type, &vp );
2344 slapi_ch_free( (void **)&vp );
2352 int slapi_x_attrset_delete( Slapi_AttrSet *as, const char *type )
2355 AttributeDescription *ad = NULL;
2362 if ( *as == NULL ) {
2366 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2370 if ( attr_delete( as, ad ) != LDAP_SUCCESS ) {
2381 * Synthesise an LDAPMod array from a Modifications list to pass
2382 * to SLAPI. This synthesis is destructive and as such the
2383 * Modifications list may not be used after calling this
2386 * This function must also be called before slap_mods_check().
2388 LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
2391 Modifications *ml, *modlist;
2392 LDAPMod **mods, *modp;
2395 modlist = *pmodlist;
2397 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
2400 mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
2402 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
2404 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
2406 /* Take ownership of original type. */
2407 modp->mod_type = ml->sml_type.bv_val;
2408 ml->sml_type.bv_val = NULL;
2410 if ( ml->sml_bvalues != NULL ) {
2411 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
2413 modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
2414 sizeof(struct berval *) );
2415 for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
2416 /* Take ownership of original values. */
2417 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
2418 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
2419 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
2420 ml->sml_bvalues[j].bv_len = 0;
2421 ml->sml_bvalues[j].bv_val = NULL;
2423 modp->mod_bvalues[j] = NULL;
2425 modp->mod_bvalues = NULL;
2432 slap_mods_free( modlist );
2442 * Convert a potentially modified array of LDAPMods back to a
2443 * Modification list.
2445 * The returned Modification list contains pointers into the
2446 * LDAPMods array; the latter MUST be freed with
2447 * slapi_x_free_ldapmods() (see below).
2449 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
2452 Modifications *modlist, **modtail;
2457 for( modp = mods; *modp != NULL; modp++ ) {
2461 struct berval **bvp;
2463 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
2464 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
2465 mod->sml_type.bv_val = (*modp)->mod_type;
2466 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
2467 mod->sml_desc = NULL;
2468 mod->sml_next = NULL;
2470 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
2471 for( i = 0, bvp = (*modp)->mod_bvalues; *bvp != NULL; bvp++, i++ )
2474 for( i = 0, p = (*modp)->mod_values; *p != NULL; p++, i++ )
2478 mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
2480 /* NB: This implicitly trusts a plugin to return valid modifications. */
2481 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
2482 for( i = 0, bvp = (*modp)->mod_bvalues; *bvp != NULL; bvp++, i++ ) {
2483 mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
2484 mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
2487 for( i = 0, p = (*modp)->mod_values; *p != NULL; p++, i++ ) {
2488 mod->sml_bvalues[i].bv_val = *p;
2489 mod->sml_bvalues[i].bv_len = strlen( *p );
2492 mod->sml_bvalues[i].bv_val = NULL;
2495 modtail = &mod->sml_next;
2505 * This function only frees the parts of the mods array that
2506 * are not shared with the Modification list that was created
2507 * by slapi_x_ldapmods2modifications().
2510 void slapi_x_free_ldapmods (LDAPMod **mods)
2518 for ( i = 0; mods[i] != NULL; i++ ) {
2520 * Don't free values themselves; they're owned by the
2521 * Modification list. Do free the containing array.
2523 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
2524 for ( j = 0; mods[i]->mod_bvalues[j] != NULL; j++ ) {
2525 ch_free( mods[i]->mod_bvalues[j] );
2527 ch_free( mods[i]->mod_bvalues );
2529 ch_free( mods[i]->mod_values );
2531 /* Don't free type, for same reasons. */
2535 #endif /* LDAP_SLAPI */