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) */
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 )
1162 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
1163 if ( rc != LDAP_SUCCESS )
1167 return LDAP_SUCCESS;
1170 #endif /* defined(LDAP_SLAPI) */
1173 #if defined(LDAP_SLAPI)
1175 * If oldStyle is TRUE, then a value suitable for setting to
1176 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
1177 * (pointer to static storage).
1179 * If oldStyle is FALSE, then a value suitable for setting to
1180 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
1181 * a pointer to allocated memory and will include the SASL
1182 * mechanism (if any).
1184 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
1189 switch ( authz->sai_method ) {
1190 case LDAP_AUTH_SASL:
1192 authType = SLAPD_AUTH_SASL;
1194 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
1195 authType = slapi_ch_malloc( len );
1196 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
1199 case LDAP_AUTH_SIMPLE:
1200 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
1202 case LDAP_AUTH_NONE:
1203 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
1209 if ( is_tls && authType == NULL ) {
1210 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
1218 * Internal API to prime a Slapi_PBlock with a Connection.
1220 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
1222 #if defined(LDAP_SLAPI)
1226 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
1227 if ( rc != LDAP_SUCCESS )
1230 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
1231 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
1232 if ( rc != LDAP_SUCCESS )
1236 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
1237 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
1238 if ( rc != LDAP_SUCCESS )
1242 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
1243 if ( rc != LDAP_SUCCESS )
1246 /* Returns pointer to static string */
1247 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
1248 if ( connAuthType != NULL ) {
1249 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
1250 if ( rc != LDAP_SUCCESS )
1254 /* Returns pointer to allocated string */
1255 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
1256 if ( connAuthType != NULL ) {
1257 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
1258 if ( rc != LDAP_SUCCESS )
1262 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
1263 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
1264 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
1265 if ( rc != LDAP_SUCCESS )
1272 #endif /* defined(LDAP_SLAPI) */
1276 * Internal API to prime a Slapi_PBlock with an Operation.
1278 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
1280 #if defined(LDAP_SLAPI)
1287 if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
1291 isRoot = be_isroot( be, &op->o_ndn );
1292 isUpdateDn = be_isupdate( be, &op->o_ndn );
1295 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
1296 if ( rc != LDAP_SUCCESS )
1299 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
1300 if ( rc != LDAP_SUCCESS )
1303 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
1304 if ( rc != LDAP_SUCCESS )
1307 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
1308 if ( rc != LDAP_SUCCESS )
1311 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
1312 if ( rc != LDAP_SUCCESS )
1315 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
1316 if ( rc != LDAP_SUCCESS )
1319 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
1320 if ( rc != LDAP_SUCCESS)
1323 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
1324 if ( rc != LDAP_SUCCESS )
1327 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
1328 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
1329 /* Not quite sure what the point of this is. */
1330 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
1331 if ( rc != LDAP_SUCCESS )
1335 return LDAP_SUCCESS;
1341 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
1343 #if defined( LDAP_SLAPI )
1346 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1347 *isSSL = conn->c_is_tls;
1349 return LDAP_SUCCESS;
1352 #endif /* defined(LDAP_SLAPI) */
1356 * DS 5.x compatability API follow
1359 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
1361 #if defined( LDAP_SLAPI )
1365 return LDAP_PARAM_ERROR;
1367 at = attr->a_desc->ad_type;
1369 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
1371 if ( is_at_single_value( at ) )
1372 *flags |= SLAPI_ATTR_FLAG_SINGLE;
1373 if ( is_at_operational( at ) )
1374 *flags |= SLAPI_ATTR_FLAG_OPATTR;
1375 if ( is_at_obsolete( at ) )
1376 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
1377 if ( is_at_collective( at ) )
1378 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
1379 if ( is_at_no_user_mod( at ) )
1380 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
1382 return LDAP_SUCCESS;
1385 #endif /* defined(LDAP_SLAPI) */
1388 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
1390 #if defined( LDAP_SLAPI )
1391 unsigned long flags;
1393 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
1395 return (flags & flag) ? 1 : 0;
1398 #endif /* defined(LDAP_SLAPI) */
1401 Slapi_Attr *slapi_attr_new( void )
1406 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
1414 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
1418 AttributeDescription *ad = NULL;
1420 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
1435 void slapi_attr_free( Slapi_Attr **a )
1443 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
1446 return attr_dup( (Slapi_Attr *)attr );
1452 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
1455 return value_add_one( &a->a_vals, (Slapi_Value *)v );
1461 int slapi_attr_type2plugin( const char *type, void **pi )
1468 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
1471 if ( attr == NULL ) {
1472 return LDAP_PARAM_ERROR;
1475 *type = attr->a_desc->ad_cname.bv_val;
1477 return LDAP_SUCCESS;
1483 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
1486 if ( attr == NULL ) {
1487 return LDAP_PARAM_ERROR;
1489 *oidp = attr->a_desc->ad_type->sat_oid;
1491 return LDAP_SUCCESS;
1497 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
1505 mr = a->a_desc->ad_type->sat_equality;
1506 rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
1507 (struct berval *)v1, (void *)v2, &text );
1508 if ( rc != LDAP_SUCCESS )
1511 return ( ret == 0 ) ? 0 : -1;
1517 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
1527 mr = a->a_desc->ad_type->sat_equality;
1528 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
1529 rc = value_match( &ret, a->a_desc, mr,
1530 SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
1531 if ( rc != LDAP_SUCCESS ) {
1542 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
1545 AttributeDescription *a1 = NULL;
1546 AttributeDescription *a2 = NULL;
1550 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
1554 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
1558 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
1559 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
1560 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
1563 case SLAPI_TYPE_CMP_EXACT:
1564 ret = ad_cmp( a1, a2 );
1566 case SLAPI_TYPE_CMP_BASE:
1567 ret = ad_base_cmp( a1, a2 );
1569 case SLAPI_TYPE_CMP_SUBTYPE:
1570 ret = is_ad_subtype( a2, a2 );
1583 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
1586 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
1592 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
1595 return slapi_valueset_first_value( &a->a_vals, v );
1601 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
1604 return slapi_valueset_next_value( &a->a_vals, hint, v );
1610 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
1613 *numValues = slapi_valueset_count( &a->a_vals );
1621 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
1624 *vs = &((Slapi_Attr *)a)->a_vals;
1632 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
1635 return slapi_attr_get_values( a, vals );
1641 char *slapi_attr_syntax_normalize( const char *s )
1644 AttributeDescription *ad = NULL;
1647 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
1651 return ad->ad_cname.bv_val;
1657 Slapi_Value *slapi_value_new( void )
1662 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
1670 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
1673 return ber_dupbv( NULL, (struct berval *)bval );
1679 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
1682 return slapi_value_new_berval( v );
1688 Slapi_Value *slapi_value_new_string(const char *s)
1693 bv.bv_val = (char *)s;
1694 bv.bv_len = strlen( s );
1696 return slapi_value_new_berval( &bv );
1702 Slapi_Value *slapi_value_init(Slapi_Value *val)
1714 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
1717 return ber_dupbv( v, bval );
1723 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
1726 v->bv_val = slapi_ch_strdup( (char *)s );
1727 v->bv_len = strlen( s );
1735 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
1738 return slapi_value_new_value( v );
1744 void slapi_value_free(Slapi_Value **value)
1747 if ( value == NULL ) {
1751 if ( (*value) != NULL ) {
1752 slapi_ch_free( (void **)&(*value)->bv_val );
1753 slapi_ch_free( (void **)value );
1758 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
1767 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
1770 if ( value == NULL ) {
1773 if ( value->bv_val != NULL ) {
1774 slapi_ch_free( (void **)&value->bv_val );
1776 slapi_value_init_berval( value, (struct berval *)bval );
1784 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
1787 if ( value == NULL ) {
1790 return slapi_value_set_berval( value, vfrom );
1796 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
1799 if ( value == NULL ) {
1802 if ( value->bv_val != NULL ) {
1803 slapi_ch_free( (void **)&value->bv_val );
1805 value->bv_val = slapi_ch_malloc( len );
1806 value->bv_len = len;
1807 AC_MEMCPY( value->bv_val, val, len );
1815 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
1818 if ( value == NULL ) {
1821 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
1828 int slapi_value_set_int(Slapi_Value *value, int intVal)
1833 snprintf( buf, sizeof( buf ), "%d", intVal );
1835 return slapi_value_set_string( value, buf );
1841 const char *slapi_value_get_string(const Slapi_Value *value)
1844 if ( value == NULL ) {
1847 return value->bv_val;
1854 static int checkBVString(const struct berval *bv)
1858 for ( i = 0; i < bv->bv_len; i++ ) {
1859 if ( bv->bv_val[i] == '\0' )
1862 if ( bv->bv_val[i] != '\0' )
1869 int slapi_value_get_int(const Slapi_Value *value)
1872 if ( value == NULL ) return 0;
1873 if ( !checkBVString( value ) ) return 0;
1875 return (int)strtol( value->bv_val, NULL, 10 );
1881 unsigned int slapi_value_get_uint(const Slapi_Value *value)
1884 if ( value == NULL ) return 0;
1885 if ( !checkBVString( value ) ) return 0;
1887 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
1893 long slapi_value_get_long(const Slapi_Value *value)
1896 if ( value == NULL ) return 0;
1897 if ( !checkBVString( value ) ) return 0;
1899 return strtol( value->bv_val, NULL, 10 );
1905 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
1908 if ( value == NULL ) return 0;
1909 if ( !checkBVString( value ) ) return 0;
1911 return strtoul( value->bv_val, NULL, 10 );
1917 size_t slapi_value_get_length(const Slapi_Value *value)
1920 if ( value == NULL )
1923 return (size_t) value->bv_len;
1929 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
1932 return slapi_attr_value_cmp( a, v1, v2 );
1938 /* A ValueSet is a container for a BerVarray. */
1939 Slapi_ValueSet *slapi_valueset_new( void )
1944 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
1953 void slapi_valueset_free(Slapi_ValueSet *vs)
1959 ber_bvarray_free( vp );
1960 slapi_ch_free( (void **)&vp );
1967 void slapi_valueset_init(Slapi_ValueSet *vs)
1970 if ( vs != NULL && *vs == NULL ) {
1971 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
1972 (*vs)->bv_val = NULL;
1978 void slapi_valueset_done(Slapi_ValueSet *vs)
1986 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
1988 slapi_ch_free( (void **)&vp->bv_val );
1990 /* but don't free *vs or vs */
1994 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
1999 ber_dupbv( &bv, (Slapi_Value *)addval );
2000 ber_bvarray_add( vs, &bv );
2004 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2007 return slapi_valueset_next_value( vs, 0, v );
2013 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2024 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2035 int slapi_valueset_count( const Slapi_ValueSet *vs )
2046 for ( i = 0; vp[i].bv_val != NULL; i++ )
2056 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2061 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2062 slapi_valueset_add_value( vs1, vp );
2068 * Attribute sets are an OpenLDAP extension for the
2069 * virtual operational attribute coalescing plugin
2071 Slapi_AttrSet *slapi_x_attrset_new( void )
2077 * Like a Slapi_ValueSet, a Slapi_AttrSet is a container
2078 * for objects: we need this because it may be initially
2081 a = (Slapi_AttrSet *)slapi_ch_malloc( sizeof( *a ) );
2090 Slapi_AttrSet *slapi_x_attrset_init( Slapi_AttrSet *as, Slapi_Attr *a )
2102 void slapi_x_attrset_free( Slapi_AttrSet **pAs )
2105 Slapi_AttrSet *as = *pAs;
2109 slapi_ch_free( (void **)pAs );
2114 Slapi_AttrSet *slapi_x_attrset_dup( Slapi_AttrSet *as )
2117 Slapi_AttrSet *newAs = slapi_x_attrset_new();
2120 *newAs = attrs_dup( *as );
2128 int slapi_x_attrset_add_attr( Slapi_AttrSet *as, Slapi_Attr *a )
2131 Slapi_Attr *nextAttr;
2133 if ( as == NULL || a == NULL )
2136 if ( *as == NULL ) {
2137 /* First attribute */
2141 /* Non-first attribute */
2142 nextAttr = (*as)->a_next;
2146 a->a_next = nextAttr;
2154 int slapi_x_attrset_add_attr_copy( Slapi_AttrSet *as, Slapi_Attr *a )
2159 adup = slapi_attr_dup( a );
2160 return slapi_x_attrset_add_attr( as, adup );
2166 int slapi_x_attrset_find( Slapi_AttrSet *as, const char *type, Slapi_Attr **attr )
2169 AttributeDescription *ad = NULL;
2172 if ( as == NULL || *as == NULL ) {
2176 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2179 *attr = attrs_find( *as, ad );
2180 return ( *attr == NULL ) ? -1 : 0;
2186 int slapi_x_attrset_merge( Slapi_AttrSet *as, const char *type, Slapi_ValueSet *vals )
2189 AttributeDescription *ad = NULL;
2193 if ( vals == NULL || *vals == NULL ) {
2194 /* Must have something to add. */
2198 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2202 for ( a = as; *a != NULL; a = &(*a)->a_next ) {
2203 if ( ad_cmp( (*a)->a_desc, ad ) == 0 ) {
2209 *a = (Slapi_Attr *) slapi_ch_malloc( sizeof(Attribute) );
2211 (*a)->a_vals = NULL;
2212 (*a)->a_next = NULL;
2216 return value_add ( &(*a)->a_vals, *vals );
2222 int slapi_x_attrset_merge_bervals( Slapi_AttrSet *as, const char *type, struct berval **vals )
2228 if ( bvptr2obj( vals, &vp ) != LDAP_SUCCESS ) {
2231 rc = slapi_x_attrset_merge( as, type, &vp );
2232 slapi_ch_free( (void **)&vp );
2240 int slapi_x_attrset_delete( Slapi_AttrSet *as, const char *type )
2243 AttributeDescription *ad = NULL;
2250 if ( *as == NULL ) {
2254 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2258 if ( attr_delete( as, ad ) != LDAP_SUCCESS ) {