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;
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;
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;
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) */
431 #if defined(LDAP_SLAPI)
432 return ch_calloc( nelem, size );
433 #else /* !defined(LDAP_SLAPI) */
435 #endif /* !defined(LDAP_SLAPI) */
443 #if defined(LDAP_SLAPI)
444 return ch_realloc( block, size );
445 #else /* !defined(LDAP_SLAPI) */
447 #endif /* !defined(LDAP_SLAPI) */
451 slapi_ch_strdup( char *s )
453 #if defined(LDAP_SLAPI)
454 return ch_strdup( (const char *)s );
455 #else /* !defined(LDAP_SLAPI) */
457 #endif /* !defined(LDAP_SLAPI) */
461 slapi_ch_stlen( char *s )
463 #if defined(LDAP_SLAPI)
464 return strlen( (const char *)s );
465 #else /* !defined(LDAP_SLAPI) */
467 #endif /* !defined(LDAP_SLAPI) */
471 slapi_control_present(
472 LDAPControl **controls,
477 #if defined(LDAP_SLAPI)
489 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
490 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
495 if ( controls[i]->ldctl_value.bv_len != 0 ) {
497 * FIXME: according to 6.1 specification,
498 * "The val output parameter is set
499 * to point into the controls array.
500 * A copy of the control value is
504 struct berval *pTmpBval;
506 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
507 if ( pTmpBval == NULL ) {
510 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
511 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
515 slapi_ch_free( (void **)&pTmpBval );
521 *val = &controls[i]->ldctl_value;
526 *iscritical = controls[i]->ldctl_iscritical;
533 #else /* !defined(LDAP_SLAPI) */
535 #endif /* !defined(LDAP_SLAPI) */
539 slapi_register_supported_control(
541 unsigned long controlops )
543 #if defined(LDAP_SLAPI)
544 /* FIXME -- can not add controls to openLDAP dynamically */
545 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_CONTROLS",
546 "can not add controls to openLDAP dynamically\n" );
547 #endif /* defined(LDAP_SLAPI) */
551 slapi_get_supported_controls(
553 unsigned long **ctrlopsp )
555 #if defined(LDAP_SLAPI)
559 unsigned long *masks = NULL;
561 for (n = 0; get_supported_ctrl( n ) != NULL; n++) {
573 oids = (char **)slapi_ch_malloc( (n + 1) * sizeof(char *) );
574 if ( oids == NULL ) {
579 masks = (unsigned long *)slapi_ch_malloc( n * sizeof(int) );
580 if ( masks == NULL ) {
585 for ( i = 0; i < n; i++ ) {
587 * FIXME: Netscape's specification says nothing about
588 * memory; should we copy the OIDs or return pointers
589 * to internal values? In OpenLDAP the latter is safe
590 * since we do not allow to register coltrols runtime
592 oids[ i ] = ch_strdup( get_supported_ctrl( i ) );
593 if ( oids[ i ] == NULL ) {
597 masks[ i ] = (unsigned long)get_supported_ctrl_mask( i );
605 if ( rc != LDAP_SUCCESS ) {
606 for ( i = 0; oids != NULL && oids[ i ] != NULL; i++ ) {
607 ch_free( oids[ i ] );
614 #else /* !defined(LDAP_SLAPI) */
616 #endif /* !defined(LDAP_SLAPI) */
620 slapi_register_supported_saslmechanism( char *mechanism )
622 #if defined(LDAP_SLAPI)
623 /* FIXME -- can not add saslmechanism to openLDAP dynamically */
624 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
625 "can not add saslmechanism to openLDAP dynamically\n" );
626 #endif /* defined(LDAP_SLAPI) */
630 slapi_get_supported_saslmechanisms( void )
632 #if defined(LDAP_SLAPI)
633 /* FIXME -- can not get the saslmechanism wihtout a connection. */
634 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
635 "can not get the saslmechanism "
636 "wihtout a connection\n" );
638 #else /* defined(LDAP_SLAPI) */
640 #endif /* defined(LDAP_SLAPI) */
644 slapi_get_supported_extended_ops( void )
646 #if defined(LDAP_SLAPI)
648 char **ppExtOpOID = NULL;
651 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
655 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
660 if ( numExtOps == 0 ) {
664 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
665 for ( k = 0; k < i; k++ ) {
668 bv = get_supported_extop( k );
669 assert( bv != NULL );
671 ppExtOpOID[ k ] = bv->bv_val;
674 for ( ; k < j; k++ ) {
677 bv = ns_get_supported_extop( k );
678 assert( bv != NULL );
680 ppExtOpOID[ i + k ] = bv->bv_val;
682 ppExtOpOID[ i + k ] = NULL;
685 #else /* !defined(LDAP_SLAPI) */
687 #endif /* !defined(LDAP_SLAPI) */
691 slapi_send_ldap_result(
697 struct berval **urls )
699 #if defined(LDAP_SLAPI)
704 struct berval *extValue = NULL;
707 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
708 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
709 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
710 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, &s );
711 rc = LDAP_SASL_BIND_IN_PROGRESS;
712 send_ldap_sasl( conn, op, rc, NULL, NULL, NULL, NULL, s );
716 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
717 if ( extOID != NULL ) {
718 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &extValue );
719 slapi_send_ldap_extended_response( conn, op, err, extOID,
724 send_ldap_result( conn, op, err, matched, text, NULL, NULL );
725 #endif /* defined(LDAP_SLAPI) */
729 slapi_send_ldap_search_entry(
732 LDAPControl **ectrls,
736 #if defined(LDAP_SLAPI)
743 AttributeName *an = NULL;
746 for ( i = 0; attrs[ i ] != NULL; i++ ) {
751 an = (AttributeName *) ch_malloc( i * sizeof(AttributeName) );
752 for ( i = 0; attrs[i] != NULL; i++ ) {
753 an[i].an_name.bv_val = ch_strdup( attrs[i] );
754 an[i].an_name.bv_len = strlen( attrs[i] );
755 an[i].an_desc = NULL;
756 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
761 if ( ( rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) ||
762 ( rc = slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&pConn) != 0 ) ||
763 ( rc = slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp) != 0 ) ) {
766 rc = send_search_entry( be, pConn, pOp, e, an, attrsonly, NULL );
771 #else /* !defined(LDAP_SLAPI) */
773 #endif /* !defined(LDAP_SLAPI) */
778 slapi_str2filter( char *str )
780 #if defined(LDAP_SLAPI)
781 return str2filter( str );
782 #else /* !defined(LDAP_SLAPI) */
784 #endif /* !defined(LDAP_SLAPI) */
792 #if defined(LDAP_SLAPI)
794 #endif /* defined(LDAP_SLAPI) */
798 slapi_filter_get_choice( Slapi_Filter *f )
800 #if defined(LDAP_SLAPI)
810 #else /* !defined(LDAP_SLAPI) */
811 return -1; /* invalid filter type */
812 #endif /* !defined(LDAP_SLAPI) */
816 slapi_filter_get_ava(
819 struct berval **bval )
821 #if defined(LDAP_SLAPI)
823 int rc = LDAP_SUCCESS;
825 assert( type != NULL );
826 assert( bval != NULL );
832 if ( ftype == LDAP_FILTER_EQUALITY
833 || ftype == LDAP_FILTER_GE
834 || ftype == LDAP_FILTER_LE
835 || ftype == LDAP_FILTER_APPROX ) {
836 *type = slapi_ch_strdup( f->f_un.f_un_ava->aa_desc->ad_cname.bv_val );
837 if ( *type == NULL ) {
842 *bval = (struct berval *)slapi_ch_malloc( sizeof(struct berval) );
843 if ( *bval == NULL ) {
848 (*bval)->bv_len = f->f_un.f_un_ava->aa_value.bv_len;
849 (*bval)->bv_val = slapi_ch_strdup( f->f_un.f_un_ava->aa_value.bv_val );
850 if ( (*bval)->bv_val == NULL ) {
854 } else { /* filter type not supported */
859 if ( rc != LDAP_SUCCESS ) {
872 #else /* !defined(LDAP_SLAPI) */
874 #endif /* !defined(LDAP_SLAPI) */
878 slapi_filter_list_first( Slapi_Filter *f )
880 #if defined(LDAP_SLAPI)
888 if ( ftype == LDAP_FILTER_AND
889 || ftype == LDAP_FILTER_OR
890 || ftype == LDAP_FILTER_NOT ) {
891 return (Slapi_Filter *)f->f_and;
895 #else /* !defined(LDAP_SLAPI) */
897 #endif /* !defined(LDAP_SLAPI) */
901 slapi_filter_list_next(
903 Slapi_Filter *fprev )
905 #if defined(LDAP_SLAPI)
913 if ( ftype == LDAP_FILTER_AND
914 || ftype == LDAP_FILTER_OR
915 || ftype == LDAP_FILTER_NOT ) {
916 if ( f->f_and == fprev ) {
917 return f->f_and->f_next;
922 #else /* !defined(LDAP_SLAPI) */
924 #endif /* !defined(LDAP_SLAPI) */
928 slapi_send_ldap_extended_response(
933 struct berval *response )
935 #if defined(LDAP_SLAPI)
936 send_ldap_extended( conn,op, errornum, NULL, NULL, NULL,
937 respName,response, NULL );
939 #else /* !defined(LDAP_SLAPI) */
941 #endif /* !defined(LDAP_SLAPI) */
946 struct berval **vals,
949 #if defined(LDAP_SLAPI)
951 * FIXME: what's the point?
954 #else /* !defined(LDAP_SLAPI) */
956 #endif /* !defined(LDAP_SLAPI) */
960 slapi_get_hostname( void )
962 #if defined(LDAP_SLAPI)
966 * FIXME: I'd prefer a different check ...
969 hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
971 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
972 "can't malloc memory for hostname\n" );
975 } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
976 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
977 "can't get hostname\n" );
978 slapi_ch_free( (void **)&hn );
982 static int been_here = 0;
983 static char *static_hn = NULL;
985 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
987 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
988 if ( static_hn == NULL) {
989 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
990 "can't malloc memory for hostname\n" );
992 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
997 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
998 slapi_log_error( SLAPI_LOG_FATAL,
1000 "can't get hostname\n" );
1001 slapi_ch_free( (void **)&static_hn );
1003 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1012 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1014 hn = ch_strdup( static_hn );
1015 #endif /* !_SPARC */
1018 #else /* !defined(LDAP_SLAPI) */
1020 #endif /* !defined(LDAP_SLAPI) */
1024 * FIXME: this should go in an appropriate header ...
1026 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
1035 #if defined(LDAP_SLAPI)
1036 int rc = LDAP_SUCCESS;
1039 va_start( arglist, fmt );
1040 rc = vLogError( severity, subsystem, fmt, arglist );
1044 #else /* !defined(LDAP_SLAPI) */
1046 #endif /* !defined(LDAP_SLAPI) */
1051 slapi_timer_current_time( void )
1053 #if defined(LDAP_SLAPI)
1054 static int first_time = 1;
1055 #if !defined (_WIN32)
1059 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1062 gettimeofday( &base_time, NULL );
1064 gettimeofday( &now, NULL );
1065 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1066 (now.tv_usec - base_time.tv_usec);
1067 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1073 return (slap_get_time() - starttime) * 1000000;
1080 performance_counter_present = QueryPerformanceCounter( &base_time );
1081 QueryPerformanceFrequency( &performance_freq );
1084 if ( !performance_counter_present )
1087 QueryPerformanceCounter( &now );
1088 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1090 #else /* !defined(LDAP_SLAPI) */
1092 #endif /* !defined(LDAP_SLAPI) */
1099 slapi_timer_get_time( char *label )
1101 #if defined(LDAP_SLAPI)
1102 unsigned long start = slapi_timer_current_time();
1103 printf("%10ld %10ld usec %s\n", start, 0, label);
1105 #else /* !defined(LDAP_SLAPI) */
1107 #endif /* !defined(LDAP_SLAPI) */
1114 slapi_timer_elapsed_time(
1116 unsigned long start )
1118 #if defined(LDAP_SLAPI)
1119 unsigned long stop = slapi_timer_current_time();
1120 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
1121 #endif /* defined(LDAP_SLAPI) */
1125 slapi_free_search_results_internal( Slapi_PBlock *pb )
1127 #if defined(LDAP_SLAPI)
1128 Slapi_Entry **entries;
1129 int k = 0, nEnt = 0;
1131 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
1132 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
1137 if ( entries == NULL ) {
1141 for ( k = 0; k < nEnt; k++ ) {
1142 slapi_entry_free( entries[k] );
1145 slapi_ch_free( (void **)&entries );
1146 #endif /* defined(LDAP_SLAPI) */
1150 * Internal API to prime a Slapi_PBlock with a Backend.
1152 int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
1154 #if defined(LDAP_SLAPI)
1157 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
1158 if ( rc != LDAP_SUCCESS )
1161 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
1162 if ( rc != LDAP_SUCCESS )
1165 return LDAP_SUCCESS;
1168 #endif /* defined(LDAP_SLAPI) */
1171 #if defined(LDAP_SLAPI)
1173 * If oldStyle is TRUE, then a value suitable for setting to
1174 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
1175 * (pointer to static storage).
1177 * If oldStyle is FALSE, then a value suitable for setting to
1178 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
1179 * a pointer to allocated memory and will include the SASL
1180 * mechanism (if any).
1182 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
1187 switch ( authz->sai_method ) {
1188 case LDAP_AUTH_SASL:
1190 authType = SLAPD_AUTH_SASL;
1192 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
1193 authType = slapi_ch_malloc( len );
1194 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
1197 case LDAP_AUTH_SIMPLE:
1198 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
1200 case LDAP_AUTH_NONE:
1201 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
1207 if ( is_tls && authType == NULL ) {
1208 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
1216 * Internal API to prime a Slapi_PBlock with a Connection.
1218 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
1220 #if defined(LDAP_SLAPI)
1224 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
1225 if ( rc != LDAP_SUCCESS )
1228 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
1229 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
1230 if ( rc != LDAP_SUCCESS )
1234 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
1235 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
1236 if ( rc != LDAP_SUCCESS )
1240 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
1241 if ( rc != LDAP_SUCCESS )
1244 /* Returns pointer to static string */
1245 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
1246 if ( connAuthType != NULL ) {
1247 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
1248 if ( rc != LDAP_SUCCESS )
1252 /* Returns pointer to allocated string */
1253 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
1254 if ( connAuthType != NULL ) {
1255 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
1256 if ( rc != LDAP_SUCCESS )
1260 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
1261 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
1262 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
1263 if ( rc != LDAP_SUCCESS )
1270 #endif /* defined(LDAP_SLAPI) */
1274 * Internal API to prime a Slapi_PBlock with an Operation.
1276 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
1278 #if defined(LDAP_SLAPI)
1285 if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
1289 isRoot = be_isroot( be, &op->o_ndn );
1290 isUpdateDn = be_isupdate( be, &op->o_ndn );
1293 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
1294 if ( rc != LDAP_SUCCESS )
1297 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
1298 if ( rc != LDAP_SUCCESS )
1301 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
1302 if ( rc != LDAP_SUCCESS )
1305 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
1306 if ( rc != LDAP_SUCCESS )
1309 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
1310 if ( rc != LDAP_SUCCESS )
1313 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
1314 if ( rc != LDAP_SUCCESS )
1317 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
1318 if ( rc != LDAP_SUCCESS)
1321 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
1322 if ( rc != LDAP_SUCCESS )
1325 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
1326 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
1327 /* Not quite sure what the point of this is. */
1328 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
1329 if ( rc != LDAP_SUCCESS )
1333 return LDAP_SUCCESS;
1339 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
1341 #if defined( LDAP_SLAPI )
1344 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1345 *isSSL = conn->c_is_tls;
1347 return LDAP_SUCCESS;
1350 #endif /* defined(LDAP_SLAPI) */
1354 * DS 5.x compatability API follow
1357 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
1359 #if defined( LDAP_SLAPI )
1363 return LDAP_PARAM_ERROR;
1365 at = attr->a_desc->ad_type;
1367 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
1369 if ( is_at_single_value( at ) )
1370 *flags |= SLAPI_ATTR_FLAG_SINGLE;
1371 if ( is_at_operational( at ) )
1372 *flags |= SLAPI_ATTR_FLAG_OPATTR;
1373 if ( is_at_obsolete( at ) )
1374 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
1375 if ( is_at_collective( at ) )
1376 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
1377 if ( is_at_no_user_mod( at ) )
1378 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
1380 return LDAP_SUCCESS;
1383 #endif /* defined(LDAP_SLAPI) */
1386 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
1388 #if defined( LDAP_SLAPI )
1389 unsigned long flags;
1391 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
1393 return (flags & flag) ? 1 : 0;
1396 #endif /* defined(LDAP_SLAPI) */
1399 Slapi_Attr *slapi_attr_new( void )
1404 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
1412 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
1416 AttributeDescription *ad;
1418 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
1433 void slapi_attr_free( Slapi_Attr **a )
1441 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
1444 return attr_dup( (Slapi_Attr *)attr );
1450 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
1453 return value_add_one( &a->a_vals, (Slapi_Value *)v );
1459 int slapi_attr_type2plugin( const char *type, void **pi )
1466 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
1469 if ( attr == NULL ) {
1470 return LDAP_PARAM_ERROR;
1473 *type = attr->a_desc->ad_cname.bv_val;
1475 return LDAP_SUCCESS;
1481 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
1484 if ( attr == NULL ) {
1485 return LDAP_PARAM_ERROR;
1487 *oidp = attr->a_desc->ad_type->sat_oid;
1489 return LDAP_SUCCESS;
1495 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
1503 mr = a->a_desc->ad_type->sat_equality;
1504 rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
1505 (struct berval *)v1, (void *)v2, &text );
1506 if ( rc != LDAP_SUCCESS )
1509 return ( ret == 0 ) ? 0 : -1;
1515 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
1525 mr = a->a_desc->ad_type->sat_equality;
1526 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
1527 rc = value_match( &ret, a->a_desc, mr,
1528 SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
1529 if ( rc != LDAP_SUCCESS ) {
1540 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
1543 AttributeDescription *a1;
1544 AttributeDescription *a2;
1548 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
1552 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
1556 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
1557 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
1558 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
1561 case SLAPI_TYPE_CMP_EXACT:
1562 ret = ad_cmp( a1, a2 );
1564 case SLAPI_TYPE_CMP_BASE:
1565 ret = ad_base_cmp( a1, a2 );
1567 case SLAPI_TYPE_CMP_SUBTYPE:
1568 ret = is_ad_subtype( a2, a2 );
1581 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
1584 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
1590 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
1593 return slapi_valueset_first_value( &a->a_vals, v );
1599 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
1602 return slapi_valueset_next_value( &a->a_vals, hint, v );
1608 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
1611 *numValues = slapi_valueset_count( &a->a_vals );
1619 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
1622 *vs = &((Slapi_Attr *)a)->a_vals;
1630 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
1633 return slapi_attr_get_values( a, vals );
1639 char *slapi_attr_syntax_normalize( const char *s )
1642 AttributeDescription *ad;
1645 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
1649 return ad->ad_cname.bv_val;
1655 Slapi_Value *slapi_value_new( void )
1660 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
1668 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
1671 return ber_dupbv( NULL, (struct berval *)bval );
1677 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
1680 return slapi_value_new_berval( v );
1686 Slapi_Value *slapi_value_new_string(const char *s)
1691 bv.bv_val = (char *)s;
1692 bv.bv_len = strlen( s );
1694 return slapi_value_new_berval( &bv );
1700 Slapi_Value *slapi_value_init(Slapi_Value *val)
1712 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
1715 return ber_dupbv( v, bval );
1721 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
1724 v->bv_val = slapi_ch_strdup( (char *)s );
1725 v->bv_len = strlen( s );
1733 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
1736 return slapi_value_new_value( v );
1742 void slapi_value_free(Slapi_Value **value)
1745 if ( value == NULL ) {
1748 if ( *value != NULL ) {
1751 slapi_ch_free( (void **)&v->bv_val );
1752 slapi_ch_free( (void **)&v );
1757 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
1766 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
1769 if ( value == NULL ) {
1772 if ( value->bv_val != NULL ) {
1773 slapi_ch_free( (void **)&value->bv_val );
1775 slapi_value_init_berval( value, (struct berval *)bval );
1783 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
1786 if ( value == NULL ) {
1789 return slapi_value_set_berval( value, vfrom );
1795 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
1798 if ( value == NULL ) {
1801 if ( value->bv_val != NULL ) {
1802 slapi_ch_free( (void **)&value->bv_val );
1804 value->bv_val = slapi_ch_malloc( len );
1805 value->bv_len = len;
1806 AC_MEMCPY( value->bv_val, val, len );
1814 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
1817 if ( value == NULL ) {
1820 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
1827 int slapi_value_set_int(Slapi_Value *value, int intVal)
1832 snprintf( buf, sizeof( buf ), "%d", intVal );
1834 return slapi_value_set_string( value, buf );
1840 const char *slapi_value_get_string(const Slapi_Value *value)
1843 if ( value == NULL ) {
1846 return value->bv_val;
1853 static int checkBVString(const struct berval *bv)
1857 for ( i = 0; i < bv->bv_len; i++ ) {
1858 if ( bv->bv_val[i] == '\0' )
1861 if ( bv->bv_val[i] != '\0' )
1868 int slapi_value_get_int(const Slapi_Value *value)
1871 if ( value == NULL ) return 0;
1872 if ( !checkBVString( value ) ) return 0;
1874 return (int)strtol( value->bv_val, NULL, 10 );
1880 unsigned int slapi_value_get_uint(const Slapi_Value *value)
1883 if ( value == NULL ) return 0;
1884 if ( !checkBVString( value ) ) return 0;
1886 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
1892 long slapi_value_get_long(const Slapi_Value *value)
1895 if ( value == NULL ) return 0;
1896 if ( !checkBVString( value ) ) return 0;
1898 return strtol( value->bv_val, NULL, 10 );
1904 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
1907 if ( value == NULL ) return 0;
1908 if ( !checkBVString( value ) ) return 0;
1910 return strtoul( value->bv_val, NULL, 10 );
1916 size_t slapi_value_get_length(const Slapi_Value *value)
1919 if ( value == NULL )
1922 return (size_t) value->bv_len;
1928 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
1931 return slapi_attr_value_cmp( a, v1, v2 );
1937 /* A ValueSet is a container for a BerVarray. */
1938 Slapi_ValueSet *slapi_valueset_new( void )
1943 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
1952 void slapi_valueset_free(Slapi_ValueSet *vs)
1958 ber_bvarray_free( vp );
1959 slapi_ch_free( (void **)&vp );
1966 void slapi_valueset_init(Slapi_ValueSet *vs)
1969 if ( vs != NULL && *vs == NULL ) {
1970 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
1971 (*vs)->bv_val = NULL;
1977 void slapi_valueset_done(Slapi_ValueSet *vs)
1985 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
1987 slapi_ch_free( (void **)&vp->bv_val );
1989 /* but don't free *vs or vs */
1993 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
1996 ber_bvarray_add( vs, (Slapi_Value *)addval );
2000 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2003 return slapi_valueset_next_value( vs, 0, v );
2009 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2020 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2031 int slapi_valueset_count( const Slapi_ValueSet *vs )
2042 for ( i = 0; vp[i].bv_val != NULL; i++ )
2052 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2057 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2058 slapi_valueset_add_value( vs1, vp );
2064 * Attribute sets are an OpenLDAP extension for the
2065 * virtual operational attribute coalescing plugin
2067 Slapi_AttrSet *slapi_x_attrset_new( void )
2073 * Like a Slapi_ValueSet, a Slapi_AttrSet is a container
2074 * for objects: we need this because it may be initially
2077 a = (Slapi_AttrSet *)slapi_ch_malloc( sizeof( *a ) );
2086 Slapi_AttrSet *slapi_x_attrset_init( Slapi_AttrSet *as, Slapi_Attr *a )
2098 void slapi_x_attrset_free( Slapi_AttrSet **pAs )
2101 Slapi_AttrSet *as = *pAs;
2105 slapi_ch_free( (void **)pAs );
2110 Slapi_AttrSet *slapi_x_attrset_dup( Slapi_AttrSet *as )
2113 Slapi_AttrSet *newAs = slapi_x_attrset_new();
2116 *newAs = attrs_dup( *as );
2124 int slapi_x_attrset_add_attr( Slapi_AttrSet *as, Slapi_Attr *a )
2127 Slapi_Attr *nextAttr;
2129 if ( as == NULL || a == NULL )
2132 if ( *as == NULL ) {
2133 /* First attribute */
2137 /* Non-first attribute */
2138 nextAttr = (*as)->a_next;
2142 a->a_next = nextAttr;
2150 int slapi_x_attrset_add_attr_copy( Slapi_AttrSet *as, Slapi_Attr *a )
2155 adup = slapi_attr_dup( a );
2156 return slapi_x_attrset_add_attr( as, adup );
2162 int slapi_x_attrset_find( Slapi_AttrSet *as, const char *type, Slapi_Attr **attr )
2165 AttributeDescription *ad;
2168 if ( as == NULL || *as == NULL ) {
2172 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2175 *attr = attrs_find( *as, ad );
2176 return ( *attr == NULL ) ? -1 : 0;
2182 int slapi_x_attrset_merge( Slapi_AttrSet *as, const char *type, Slapi_ValueSet *vals )
2185 AttributeDescription *ad;
2189 if ( vals == NULL || *vals == NULL ) {
2190 /* Must have something to add. */
2194 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2198 for ( a = as; *a != NULL; a = &(*a)->a_next ) {
2199 if ( ad_cmp( (*a)->a_desc, ad ) == 0 ) {
2205 *a = (Slapi_Attr *) slapi_ch_malloc( sizeof(Attribute) );
2207 (*a)->a_vals = NULL;
2208 (*a)->a_next = NULL;
2212 return value_add ( &(*a)->a_vals, *vals );
2218 int slapi_x_attrset_merge_bervals( Slapi_AttrSet *as, const char *type, struct berval **vals )
2224 if ( bvptr2obj( vals, &vp ) != LDAP_SUCCESS ) {
2227 rc = slapi_x_attrset_merge( as, type, &vp );
2228 slapi_ch_free( (void **)&vp );
2236 int slapi_x_attrset_delete( Slapi_AttrSet *as, const char *type )
2239 AttributeDescription *ad;
2246 if ( *as == NULL ) {
2250 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2254 if ( attr_delete( as, ad ) != LDAP_SUCCESS ) {