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) */
140 #if defined(LDAP_SLAPI)
141 struct berval dn = { 0, NULL };
144 dn.bv_len = strlen( ldn );
146 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
147 #endif /* defined(LDAP_SLAPI) */
151 slapi_entry_dup( Slapi_Entry *e )
153 #if defined(LDAP_SLAPI)
158 tmp = slapi_entry2str( e, &len );
160 return (Slapi_Entry *)NULL;
163 tmpEnt = (Slapi_Entry *)str2entry( tmp );
164 if ( tmpEnt == NULL ) {
165 slapi_ch_free( (void **)&tmp );
166 return (Slapi_Entry *)NULL;
170 slapi_ch_free( (void **)&tmp );
174 #else /* !defined(LDAP_SLAPI) */
176 #endif /* !defined(LDAP_SLAPI) */
180 slapi_entry_attr_delete(
184 #if defined(LDAP_SLAPI)
185 AttributeDescription *ad;
188 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
189 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
192 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
193 return 0; /* attribute is deleted */
195 return -1; /* something went wrong */
197 #else /* !defined(LDAP_SLAPI) */
199 #endif /* !defined(LDAP_SLAPI) */
203 slapi_entry_alloc( void )
205 #if defined(LDAP_SLAPI)
206 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
207 #else /* !defined(LDAP_SLAPI) */
209 #endif /* !defined(LDAP_SLAPI) */
213 slapi_entry_free( Slapi_Entry *e )
215 #if defined(LDAP_SLAPI)
217 #endif /* defined(LDAP_SLAPI) */
221 slapi_entry_attr_merge(
224 struct berval **vals )
226 #if defined(LDAP_SLAPI)
227 AttributeDescription *ad;
232 rc = bvptr2obj( vals, &bv );
233 if ( rc != LDAP_SUCCESS ) {
237 rc = slap_str2ad( type, &ad, &text );
238 if ( rc != LDAP_SUCCESS ) {
242 rc = attr_merge( e, ad, bv );
246 #else /* !defined(LDAP_SLAPI) */
248 #endif /* !defined(LDAP_SLAPI) */
252 slapi_entry_attr_find(
257 #if defined(LDAP_SLAPI)
258 AttributeDescription *ad;
262 rc = slap_str2ad( type, &ad, &text );
263 if ( rc != LDAP_SUCCESS ) {
267 *attr = attr_find( e->e_attrs, ad );
268 if ( *attr == NULL ) {
273 #else /* !defined(LDAP_SLAPI) */
275 #endif /* !defined(LDAP_SLAPI) */
279 * FIXME -- The caller must free the allocated memory.
280 * In Netscape they do not have to.
283 slapi_attr_get_values(
285 struct berval ***vals )
287 #if defined(LDAP_SLAPI)
291 if ( attr == NULL ) {
295 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
299 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
300 for ( j = 0; j < i; j++ ) {
301 bv[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
302 bv[j]->bv_val = ch_strdup( attr->a_vals[j].bv_val );
303 bv[j]->bv_len = attr->a_vals[j].bv_len;
307 *vals = (struct berval **)bv;
310 #else /* !defined(LDAP_SLAPI) */
312 #endif /* !defined(LDAP_SLAPI) */
316 slapi_dn_normalize( char *dn )
318 #if defined(LDAP_SLAPI)
322 assert( dn != NULL );
325 bdn.bv_len = strlen( dn );
327 dnNormalize2( NULL, &bdn, &ndn );
330 * FIXME: ain't it safe to set dn = ndn.bv_val ?
332 dn = ch_strdup( ndn.bv_val );
333 ch_free( ndn.bv_val );
336 #else /* !defined(LDAP_SLAPI) */
338 #endif /* !defined(LDAP_SLAPI) */
342 * FIXME: this function is dangerous and should be deprecated;
343 * DN normalization is a lot more than lower-casing, and BTW
344 * OpenLDAP's DN normalization for case insensitive attributes
345 * is already lower case
348 slapi_dn_normalize_case( char *dn )
350 #if defined(LDAP_SLAPI)
351 slapi_dn_normalize( dn );
352 ldap_pvt_str2lower( dn );
355 #else /* defined(LDAP_SLAPI) */
357 #endif /* defined(LDAP_SLAPI) */
365 #if defined(LDAP_SLAPI)
366 struct berval bdn, ndn;
367 struct berval bsuffix, nsuffix;
369 assert( dn != NULL );
370 assert( suffix != NULL );
373 bdn.bv_len = strlen( dn );
375 bsuffix.bv_val = suffix;
376 bsuffix.bv_len = strlen( suffix );
378 dnNormalize2( NULL, &bdn, &ndn );
379 dnNormalize2( NULL, &bsuffix, &nsuffix );
381 return dnIsSuffix( &ndn, &nsuffix );
382 #else /* !defined(LDAP_SLAPI) */
384 #endif /* !defined(LDAP_SLAPI) */
388 slapi_dn_ignore_case( char *dn )
390 #if defined(LDAP_SLAPI)
391 return slapi_dn_normalize_case( dn );
392 #else /* !defined(LDAP_SLAPI) */
394 #endif /* !defined(LDAP_SLAPI) */
398 slapi_ch_malloc( unsigned long size )
400 #if defined(LDAP_SLAPI)
401 return ch_malloc( size );
402 #else /* !defined(LDAP_SLAPI) */
404 #endif /* !defined(LDAP_SLAPI) */
408 slapi_ch_free( void **ptr )
410 #if defined(LDAP_SLAPI)
413 #endif /* defined(LDAP_SLAPI) */
421 #if defined(LDAP_SLAPI)
422 return ch_calloc( nelem, size );
423 #else /* !defined(LDAP_SLAPI) */
425 #endif /* !defined(LDAP_SLAPI) */
433 #if defined(LDAP_SLAPI)
434 return ch_realloc( block, size );
435 #else /* !defined(LDAP_SLAPI) */
437 #endif /* !defined(LDAP_SLAPI) */
441 slapi_ch_strdup( char *s )
443 #if defined(LDAP_SLAPI)
444 return ch_strdup( (const char *)s );
445 #else /* !defined(LDAP_SLAPI) */
447 #endif /* !defined(LDAP_SLAPI) */
451 slapi_ch_stlen( char *s )
453 #if defined(LDAP_SLAPI)
454 return strlen( (const char *)s );
455 #else /* !defined(LDAP_SLAPI) */
457 #endif /* !defined(LDAP_SLAPI) */
461 slapi_control_present(
462 LDAPControl **controls,
467 #if defined(LDAP_SLAPI)
479 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
480 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
485 if ( controls[i]->ldctl_value.bv_len != 0 ) {
487 * FIXME: according to 6.1 specification,
488 * "The val output parameter is set
489 * to point into the controls array.
490 * A copy of the control value is
494 struct berval *pTmpBval;
496 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
497 if ( pTmpBval == NULL ) {
500 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
501 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
505 slapi_ch_free( (void **)&pTmpBval );
511 *val = &controls[i]->ldctl_value;
516 *iscritical = controls[i]->ldctl_iscritical;
523 #else /* !defined(LDAP_SLAPI) */
525 #endif /* !defined(LDAP_SLAPI) */
529 slapi_register_supported_control(
531 unsigned long controlops )
533 #if defined(LDAP_SLAPI)
534 /* FIXME -- can not add controls to openLDAP dynamically */
535 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_CONTROLS",
536 "can not add controls to openLDAP dynamically\n" );
537 #endif /* defined(LDAP_SLAPI) */
541 slapi_get_supported_controls(
543 unsigned long **ctrlopsp )
545 #if defined(LDAP_SLAPI)
549 unsigned long *masks = NULL;
551 for (n = 0; get_supported_ctrl( n ) != NULL; n++) {
563 oids = (char **)slapi_ch_malloc( (n + 1) * sizeof(char *) );
564 if ( oids == NULL ) {
569 masks = (unsigned long *)slapi_ch_malloc( n * sizeof(int) );
570 if ( masks == NULL ) {
575 for ( i = 0; i < n; i++ ) {
577 * FIXME: Netscape's specification says nothing about
578 * memory; should we copy the OIDs or return pointers
579 * to internal values? In OpenLDAP the latter is safe
580 * since we do not allow to register coltrols runtime
582 oids[ i ] = ch_strdup( get_supported_ctrl( i ) );
583 if ( oids[ i ] == NULL ) {
587 masks[ i ] = (unsigned long)get_supported_ctrl_mask( i );
595 if ( rc != LDAP_SUCCESS ) {
596 for ( i = 0; oids != NULL && oids[ i ] != NULL; i++ ) {
597 ch_free( oids[ i ] );
604 #else /* !defined(LDAP_SLAPI) */
606 #endif /* !defined(LDAP_SLAPI) */
610 slapi_register_supported_saslmechanism( char *mechanism )
612 #if defined(LDAP_SLAPI)
613 /* FIXME -- can not add saslmechanism to openLDAP dynamically */
614 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
615 "can not add saslmechanism to openLDAP dynamically\n" );
616 #endif /* defined(LDAP_SLAPI) */
620 slapi_get_supported_saslmechanisms( void )
622 #if defined(LDAP_SLAPI)
623 /* FIXME -- can not get the saslmechanism wihtout a connection. */
624 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
625 "can not get the saslmechanism "
626 "wihtout a connection\n" );
628 #else /* defined(LDAP_SLAPI) */
630 #endif /* defined(LDAP_SLAPI) */
634 slapi_get_supported_extended_ops( void )
636 #if defined(LDAP_SLAPI)
638 char **ppExtOpOID = NULL;
641 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
645 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
650 if ( numExtOps == 0 ) {
654 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
655 for ( k = 0; k < i; k++ ) {
658 bv = get_supported_extop( k );
659 assert( bv != NULL );
661 ppExtOpOID[ k ] = bv->bv_val;
664 for ( ; k < j; k++ ) {
667 bv = ns_get_supported_extop( k );
668 assert( bv != NULL );
670 ppExtOpOID[ i + k ] = bv->bv_val;
672 ppExtOpOID[ i + k ] = NULL;
675 #else /* !defined(LDAP_SLAPI) */
677 #endif /* !defined(LDAP_SLAPI) */
681 slapi_send_ldap_result(
687 struct berval **urls )
689 #if defined(LDAP_SLAPI)
694 struct berval *extValue = NULL;
697 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
698 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
699 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
700 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, &s );
701 rc = LDAP_SASL_BIND_IN_PROGRESS;
702 send_ldap_sasl( conn, op, rc, NULL, NULL, NULL, NULL, s );
706 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
707 if ( extOID != NULL ) {
708 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &extValue );
709 slapi_send_ldap_extended_response( conn, op, err, extOID,
714 send_ldap_result( conn, op, err, matched, text, NULL, NULL );
715 #endif /* defined(LDAP_SLAPI) */
719 slapi_send_ldap_search_entry(
722 LDAPControl **ectrls,
726 #if defined(LDAP_SLAPI)
733 AttributeName *an = NULL;
736 for ( i = 0; attrs[ i ] != NULL; i++ ) {
741 an = (AttributeName *) ch_malloc( i * sizeof(AttributeName) );
742 for ( i = 0; attrs[i] != NULL; i++ ) {
743 an[i].an_name.bv_val = ch_strdup( attrs[i] );
744 an[i].an_name.bv_len = strlen( attrs[i] );
745 an[i].an_desc = NULL;
746 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
751 if ( ( rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) ||
752 ( rc = slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&pConn) != 0 ) ||
753 ( rc = slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp) != 0 ) ) {
756 rc = send_search_entry( be, pConn, pOp, e, an, attrsonly, NULL );
761 #else /* !defined(LDAP_SLAPI) */
763 #endif /* !defined(LDAP_SLAPI) */
768 slapi_str2filter( char *str )
770 #if defined(LDAP_SLAPI)
771 return str2filter( str );
772 #else /* !defined(LDAP_SLAPI) */
774 #endif /* !defined(LDAP_SLAPI) */
782 #if defined(LDAP_SLAPI)
784 #endif /* defined(LDAP_SLAPI) */
788 slapi_filter_get_choice( Slapi_Filter *f )
790 #if defined(LDAP_SLAPI)
800 #else /* !defined(LDAP_SLAPI) */
801 return -1; /* invalid filter type */
802 #endif /* !defined(LDAP_SLAPI) */
806 slapi_filter_get_ava(
809 struct berval **bval )
811 #if defined(LDAP_SLAPI)
813 int rc = LDAP_SUCCESS;
815 assert( type != NULL );
816 assert( bval != NULL );
822 if ( ftype == LDAP_FILTER_EQUALITY
823 || ftype == LDAP_FILTER_GE
824 || ftype == LDAP_FILTER_LE
825 || ftype == LDAP_FILTER_APPROX ) {
826 *type = slapi_ch_strdup( f->f_un.f_un_ava->aa_desc->ad_cname.bv_val );
827 if ( *type == NULL ) {
832 *bval = (struct berval *)slapi_ch_malloc( sizeof(struct berval) );
833 if ( *bval == NULL ) {
838 (*bval)->bv_len = f->f_un.f_un_ava->aa_value.bv_len;
839 (*bval)->bv_val = slapi_ch_strdup( f->f_un.f_un_ava->aa_value.bv_val );
840 if ( (*bval)->bv_val == NULL ) {
844 } else { /* filter type not supported */
849 if ( rc != LDAP_SUCCESS ) {
862 #else /* !defined(LDAP_SLAPI) */
864 #endif /* !defined(LDAP_SLAPI) */
868 slapi_filter_list_first( Slapi_Filter *f )
870 #if defined(LDAP_SLAPI)
878 if ( ftype == LDAP_FILTER_AND
879 || ftype == LDAP_FILTER_OR
880 || ftype == LDAP_FILTER_NOT ) {
881 return (Slapi_Filter *)f->f_and;
885 #else /* !defined(LDAP_SLAPI) */
887 #endif /* !defined(LDAP_SLAPI) */
891 slapi_filter_list_next(
893 Slapi_Filter *fprev )
895 #if defined(LDAP_SLAPI)
903 if ( ftype == LDAP_FILTER_AND
904 || ftype == LDAP_FILTER_OR
905 || ftype == LDAP_FILTER_NOT ) {
906 if ( f->f_and == fprev ) {
907 return f->f_and->f_next;
912 #else /* !defined(LDAP_SLAPI) */
914 #endif /* !defined(LDAP_SLAPI) */
918 slapi_send_ldap_extended_response(
923 struct berval *response )
925 #if defined(LDAP_SLAPI)
926 send_ldap_extended( conn,op, errornum, NULL, NULL, NULL,
927 respName,response, NULL );
929 #else /* !defined(LDAP_SLAPI) */
931 #endif /* !defined(LDAP_SLAPI) */
936 struct berval **vals,
939 #if defined(LDAP_SLAPI)
941 * FIXME: what's the point?
944 #else /* !defined(LDAP_SLAPI) */
946 #endif /* !defined(LDAP_SLAPI) */
950 slapi_get_hostname( void )
952 #if defined(LDAP_SLAPI)
956 * FIXME: I'd prefer a different check ...
959 hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
961 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
962 "can't malloc memory for hostname\n" );
965 } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
966 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
967 "can't get hostname\n" );
968 slapi_ch_free( (void **)&hn );
972 static int been_here = 0;
973 static char *static_hn = NULL;
975 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
977 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
978 if ( static_hn == NULL) {
979 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
980 "can't malloc memory for hostname\n" );
982 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
987 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
988 slapi_log_error( SLAPI_LOG_FATAL,
990 "can't get hostname\n" );
991 slapi_ch_free( (void **)&static_hn );
993 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1002 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1004 hn = ch_strdup( static_hn );
1005 #endif /* !_SPARC */
1008 #else /* !defined(LDAP_SLAPI) */
1010 #endif /* !defined(LDAP_SLAPI) */
1014 * FIXME: this should go in an appropriate header ...
1016 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
1025 #if defined(LDAP_SLAPI)
1026 int rc = LDAP_SUCCESS;
1029 va_start( arglist, fmt );
1030 rc = vLogError( severity, subsystem, fmt, arglist );
1034 #else /* !defined(LDAP_SLAPI) */
1036 #endif /* !defined(LDAP_SLAPI) */
1041 slapi_timer_current_time( void )
1043 #if defined(LDAP_SLAPI)
1044 static int first_time = 1;
1045 #if !defined (_WIN32)
1049 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1052 gettimeofday( &base_time, NULL );
1054 gettimeofday( &now, NULL );
1055 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1056 (now.tv_usec - base_time.tv_usec);
1057 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1063 return (slap_get_time() - starttime) * 1000000;
1070 performance_counter_present = QueryPerformanceCounter( &base_time );
1071 QueryPerformanceFrequency( &performance_freq );
1074 if ( !performance_counter_present )
1077 QueryPerformanceCounter( &now );
1078 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1080 #else /* !defined(LDAP_SLAPI) */
1082 #endif /* !defined(LDAP_SLAPI) */
1089 slapi_timer_get_time( char *label )
1091 #if defined(LDAP_SLAPI)
1092 unsigned long start = slapi_timer_current_time();
1093 printf("%10ld %10ld usec %s\n", start, 0, label);
1095 #else /* !defined(LDAP_SLAPI) */
1097 #endif /* !defined(LDAP_SLAPI) */
1104 slapi_timer_elapsed_time(
1106 unsigned long start )
1108 #if defined(LDAP_SLAPI)
1109 unsigned long stop = slapi_timer_current_time();
1110 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
1111 #endif /* defined(LDAP_SLAPI) */
1115 slapi_free_search_results_internal( Slapi_PBlock *pb )
1117 #if defined(LDAP_SLAPI)
1118 Slapi_Entry **entries;
1119 int k = 0, nEnt = 0;
1121 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
1122 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
1127 if ( entries == NULL ) {
1131 for ( k = 0; k < nEnt; k++ ) {
1132 slapi_entry_free( entries[k] );
1135 slapi_ch_free( (void **)&entries );
1136 #endif /* defined(LDAP_SLAPI) */
1140 * Internal API to prime a Slapi_PBlock with a Backend.
1142 int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
1144 #if defined(LDAP_SLAPI)
1147 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
1148 if ( rc != LDAP_SUCCESS )
1151 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
1152 if ( rc != LDAP_SUCCESS )
1155 return LDAP_SUCCESS;
1158 #endif /* defined(LDAP_SLAPI) */
1161 #if defined(LDAP_SLAPI)
1163 * If oldStyle is TRUE, then a value suitable for setting to
1164 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
1165 * (pointer to static storage).
1167 * If oldStyle is FALSE, then a value suitable for setting to
1168 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
1169 * a pointer to allocated memory and will include the SASL
1170 * mechanism (if any).
1172 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
1177 switch ( authz->sai_method ) {
1178 case LDAP_AUTH_SASL:
1180 authType = SLAPD_AUTH_SASL;
1182 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
1183 authType = slapi_ch_malloc( len );
1184 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
1187 case LDAP_AUTH_SIMPLE:
1188 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
1190 case LDAP_AUTH_NONE:
1191 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
1197 if ( is_tls && authType == NULL ) {
1198 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
1206 * Internal API to prime a Slapi_PBlock with a Connection.
1208 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
1210 #if defined(LDAP_SLAPI)
1214 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
1215 if ( rc != LDAP_SUCCESS )
1218 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
1219 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
1220 if ( rc != LDAP_SUCCESS )
1224 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
1225 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
1226 if ( rc != LDAP_SUCCESS )
1230 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
1231 if ( rc != LDAP_SUCCESS )
1234 /* Returns pointer to static string */
1235 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
1236 if ( connAuthType != NULL ) {
1237 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
1238 if ( rc != LDAP_SUCCESS )
1242 /* Returns pointer to allocated string */
1243 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
1244 if ( connAuthType != NULL ) {
1245 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
1246 if ( rc != LDAP_SUCCESS )
1250 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
1251 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
1252 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
1253 if ( rc != LDAP_SUCCESS )
1260 #endif /* defined(LDAP_SLAPI) */
1264 * Internal API to prime a Slapi_PBlock with an Operation.
1266 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
1268 #if defined(LDAP_SLAPI)
1275 if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
1279 isRoot = be_isroot( be, &op->o_ndn );
1280 isUpdateDn = be_isupdate( be, &op->o_ndn );
1283 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
1284 if ( rc != LDAP_SUCCESS )
1287 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
1288 if ( rc != LDAP_SUCCESS )
1291 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
1292 if ( rc != LDAP_SUCCESS )
1295 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
1296 if ( rc != LDAP_SUCCESS )
1299 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
1300 if ( rc != LDAP_SUCCESS )
1303 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
1304 if ( rc != LDAP_SUCCESS )
1307 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
1308 if ( rc != LDAP_SUCCESS)
1311 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
1312 if ( rc != LDAP_SUCCESS )
1315 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
1316 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
1317 /* Not quite sure what the point of this is. */
1318 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
1319 if ( rc != LDAP_SUCCESS )
1323 return LDAP_SUCCESS;
1329 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
1331 #if defined( LDAP_SLAPI )
1334 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1335 *isSSL = conn->c_is_tls;
1337 return LDAP_SUCCESS;
1340 #endif /* defined(LDAP_SLAPI) */
1344 * DS 5.x compatability API follow
1347 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
1349 #if defined( LDAP_SLAPI )
1353 return LDAP_PARAM_ERROR;
1355 at = attr->a_desc->ad_type;
1357 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
1359 if ( is_at_single_value( at ) )
1360 *flags |= SLAPI_ATTR_FLAG_SINGLE;
1361 if ( is_at_operational( at ) )
1362 *flags |= SLAPI_ATTR_FLAG_OPATTR;
1363 if ( is_at_obsolete( at ) )
1364 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
1365 if ( is_at_collective( at ) )
1366 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
1367 if ( is_at_no_user_mod( at ) )
1368 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
1370 return LDAP_SUCCESS;
1373 #endif /* defined(LDAP_SLAPI) */
1376 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
1378 #if defined( LDAP_SLAPI )
1379 unsigned long flags;
1381 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
1383 return (flags & flag) ? 1 : 0;
1386 #endif /* defined(LDAP_SLAPI) */
1389 Slapi_Attr *slapi_attr_new( void )
1394 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
1402 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
1406 AttributeDescription *ad;
1408 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
1423 void slapi_attr_free( Slapi_Attr **a )
1431 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
1434 return attr_dup( (Slapi_Attr *)attr );
1440 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
1443 return value_add_one( &a->a_vals, (Slapi_Value *)v );
1449 int slapi_attr_type2plugin( const char *type, void **pi )
1456 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
1459 if ( attr == NULL ) {
1460 return LDAP_PARAM_ERROR;
1463 *type = attr->a_desc->ad_cname.bv_val;
1465 return LDAP_SUCCESS;
1471 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
1474 if ( attr == NULL ) {
1475 return LDAP_PARAM_ERROR;
1477 *oidp = attr->a_desc->ad_type->sat_oid;
1479 return LDAP_SUCCESS;
1485 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
1493 mr = a->a_desc->ad_type->sat_equality;
1494 rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
1495 (struct berval *)v1, (void *)v2, &text );
1496 if ( rc != LDAP_SUCCESS )
1499 return ( ret == 0 ) ? 0 : -1;
1505 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
1515 mr = a->a_desc->ad_type->sat_equality;
1516 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
1517 rc = value_match( &ret, a->a_desc, mr,
1518 SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
1519 if ( rc != LDAP_SUCCESS ) {
1530 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
1533 AttributeDescription *a1;
1534 AttributeDescription *a2;
1538 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
1542 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
1546 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
1547 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
1548 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
1551 case SLAPI_TYPE_CMP_EXACT:
1552 ret = ad_cmp( a1, a2 );
1554 case SLAPI_TYPE_CMP_BASE:
1555 ret = ad_base_cmp( a1, a2 );
1557 case SLAPI_TYPE_CMP_SUBTYPE:
1558 ret = is_ad_subtype( a2, a2 );
1571 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
1574 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
1580 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
1583 return slapi_valueset_first_value( &a->a_vals, v );
1589 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
1592 return slapi_valueset_next_value( &a->a_vals, hint, v );
1598 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
1601 *numValues = slapi_valueset_count( &a->a_vals );
1609 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
1612 *vs = &((Slapi_Attr *)a)->a_vals;
1620 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
1623 return slapi_attr_get_values( a, vals );
1629 char *slapi_attr_syntax_normalize( const char *s )
1632 AttributeDescription *ad;
1635 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
1639 return ad->ad_cname.bv_val;
1645 Slapi_Value *slapi_value_new( void )
1650 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
1658 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
1661 return ber_dupbv( NULL, (struct berval *)bval );
1667 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
1670 return slapi_value_new_berval( v );
1676 Slapi_Value *slapi_value_new_string(const char *s)
1681 bv.bv_val = (char *)s;
1682 bv.bv_len = strlen( s );
1684 return slapi_value_new_berval( &bv );
1690 Slapi_Value *slapi_value_init(Slapi_Value *val)
1702 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
1705 return ber_dupbv( v, bval );
1711 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
1714 v->bv_val = slapi_ch_strdup( (char *)s );
1715 v->bv_len = strlen( s );
1723 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
1726 return slapi_value_new_value( v );
1732 void slapi_value_free(Slapi_Value **value)
1735 if ( value == NULL ) {
1738 if ( *value != NULL ) {
1741 slapi_ch_free( (void **)&v->bv_val );
1742 slapi_ch_free( (void **)&v );
1747 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
1756 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
1759 if ( value == NULL ) {
1762 if ( value->bv_val != NULL ) {
1763 slapi_ch_free( (void **)&value->bv_val );
1765 slapi_value_init_berval( value, (struct berval *)bval );
1773 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
1776 if ( value == NULL ) {
1779 return slapi_value_set_berval( value, vfrom );
1785 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
1788 if ( value == NULL ) {
1791 if ( value->bv_val != NULL ) {
1792 slapi_ch_free( (void **)&value->bv_val );
1794 value->bv_val = slapi_ch_malloc( len );
1795 value->bv_len = len;
1796 AC_MEMCPY( value->bv_val, val, len );
1804 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
1807 if ( value == NULL ) {
1810 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
1817 int slapi_value_set_int(Slapi_Value *value, int intVal)
1822 snprintf( buf, sizeof( buf ), "%d", intVal );
1824 return slapi_value_set_string( value, buf );
1830 const char *slapi_value_get_string(const Slapi_Value *value)
1833 if ( value == NULL ) {
1836 return value->bv_val;
1843 static int checkBVString(const struct berval *bv)
1847 for ( i = 0; i < bv->bv_len; i++ ) {
1848 if ( bv->bv_val[i] == '\0' )
1851 if ( bv->bv_val[i] != '\0' )
1858 int slapi_value_get_int(const Slapi_Value *value)
1861 if ( value == NULL ) return 0;
1862 if ( !checkBVString( value ) ) return 0;
1864 return (int)strtol( value->bv_val, NULL, 10 );
1870 unsigned int slapi_value_get_uint(const Slapi_Value *value)
1873 if ( value == NULL ) return 0;
1874 if ( !checkBVString( value ) ) return 0;
1876 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
1882 long slapi_value_get_long(const Slapi_Value *value)
1885 if ( value == NULL ) return 0;
1886 if ( !checkBVString( value ) ) return 0;
1888 return strtol( value->bv_val, NULL, 10 );
1894 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
1897 if ( value == NULL ) return 0;
1898 if ( !checkBVString( value ) ) return 0;
1900 return strtoul( value->bv_val, NULL, 10 );
1906 size_t slapi_value_get_length(const Slapi_Value *value)
1909 if ( value == NULL )
1912 return (size_t) value->bv_len;
1918 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
1921 return slapi_attr_value_cmp( a, v1, v2 );
1927 /* A ValueSet is a container for a BerVarray. */
1928 Slapi_ValueSet *slapi_valueset_new( void )
1933 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
1942 void slapi_valueset_free(Slapi_ValueSet *vs)
1948 ber_bvarray_free( vp );
1949 slapi_ch_free( (void **)&vp );
1956 void slapi_valueset_init(Slapi_ValueSet *vs)
1959 if ( vs != NULL && *vs == NULL ) {
1960 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
1961 (*vs)->bv_val = NULL;
1967 void slapi_valueset_done(Slapi_ValueSet *vs)
1975 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
1977 slapi_ch_free( (void **)&vp->bv_val );
1979 /* but don't free *vs or vs */
1983 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
1986 ber_bvarray_add( vs, (Slapi_Value *)addval );
1990 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
1993 return slapi_valueset_next_value( vs, 0, v );
1999 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2010 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2021 int slapi_valueset_count( const Slapi_ValueSet *vs )
2032 for ( i = 0; vp[i].bv_val != NULL; i++ )
2042 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2047 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2048 slapi_valueset_add_value( vs1, vp );
2054 * Attribute sets are an OpenLDAP extension for the
2055 * virtual operational attribute coalescing plugin
2057 Slapi_AttrSet *slapi_x_attrset_new( void )
2063 * Like a Slapi_ValueSet, a Slapi_AttrSet is a container
2064 * for objects: we need this because it may be initially
2067 a = (Slapi_AttrSet *)slapi_ch_malloc( sizeof( *a ) );
2076 Slapi_AttrSet *slapi_x_attrset_init( Slapi_AttrSet *as, Slapi_Attr *a )
2088 void slapi_x_attrset_free( Slapi_AttrSet **pAs )
2091 Slapi_AttrSet *as = *pAs;
2095 slapi_ch_free( (void **)&as );
2101 Slapi_AttrSet *slapi_x_attrset_dup( Slapi_AttrSet *as )
2104 Slapi_AttrSet *newAs = slapi_x_attrset_new();
2107 *newAs = attrs_dup( *as );
2115 int slapi_x_attrset_add_attr( Slapi_AttrSet *as, Slapi_Attr *a )
2118 Slapi_Attr *nextAttr;
2120 if ( as == NULL || a == NULL )
2123 if ( *as == NULL ) {
2124 /* First attribute */
2128 /* Non-first attribute */
2129 nextAttr = (*as)->a_next;
2133 a->a_next = nextAttr;
2141 int slapi_x_attrset_add_attr_copy( Slapi_AttrSet *as, Slapi_Attr *a )
2146 adup = slapi_attr_dup( a );
2147 return slapi_x_attrset_add_attr( as, adup );
2153 int slapi_x_attrset_find( Slapi_AttrSet *as, const char *type, Slapi_Attr **attr )
2156 AttributeDescription *ad;
2159 if ( as == NULL || *as == NULL ) {
2163 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2166 *attr = attrs_find( *as, ad );
2167 return ( *attr == NULL ) ? -1 : 0;
2173 int slapi_x_attrset_merge( Slapi_AttrSet *as, const char *type, Slapi_ValueSet *vals )
2176 AttributeDescription *ad;
2180 if ( vals == NULL || *vals == NULL ) {
2181 /* Must have something to add. */
2185 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2189 for ( a = as; *a != NULL; a = &(*a)->a_next ) {
2190 if ( ad_cmp( (*a)->a_desc, ad ) == 0 ) {
2196 *a = (Slapi_Attr *) slapi_ch_malloc( sizeof(Attribute) );
2198 (*a)->a_vals = NULL;
2199 (*a)->a_next = NULL;
2203 return value_add ( &(*a)->a_vals, *vals );
2209 int slapi_x_attrset_merge_bervals( Slapi_AttrSet *as, const char *type, struct berval **vals )
2215 if ( bvptr2obj( vals, &vp ) != LDAP_SUCCESS ) {
2218 rc = slapi_x_attrset_merge( as, type, &vp );
2219 slapi_ch_free( (void **)&vp );
2227 int slapi_x_attrset_delete( Slapi_AttrSet *as, const char *type )
2230 AttributeDescription *ad;
2237 if ( *as == NULL ) {
2241 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2245 if ( attr_delete( as, ad ) != LDAP_SUCCESS ) {