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.
14 #include "slapi_common.h"
16 #include <ac/string.h>
25 struct berval *ns_get_supported_extop( int );
28 #include <sys/systeminfo.h>
34 * server start time (should we use a struct timeval also in slapd?
36 static struct timeval base_time;
37 ldap_pvt_thread_mutex_t slapi_hn_mutex;
38 ldap_pvt_thread_mutex_t slapi_time_mutex;
41 * This function converts an array of pointers to berval objects to
42 * an array of berval objects.
47 struct berval **bvptr,
50 int rc = LDAP_SUCCESS;
54 if ( bvptr == NULL || *bvptr == NULL ) {
58 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
62 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
63 if ( tmpberval == NULL ) {
64 return LDAP_NO_MEMORY;
67 for ( i = 0; bvptr[i] != NULL; i++ ) {
68 tmpberval[i].bv_val = bvptr[i]->bv_val;
69 tmpberval[i].bv_len = bvptr[i]->bv_len;
72 if ( rc == LDAP_SUCCESS ) {
84 #if defined(LDAP_SLAPI)
85 Slapi_Entry *e = NULL;
88 pTmpS = slapi_ch_strdup( s );
89 if ( pTmpS != NULL ) {
90 e = str2entry( pTmpS );
91 slapi_ch_free( (void **)&pTmpS );
95 #else /* !defined(LDAP_SLAPI) */
97 #endif /* !defined(LDAP_SLAPI) */
105 #if defined(LDAP_SLAPI)
108 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
109 ret = entry2str( e, len );
110 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
113 #else /* !defined(LDAP_SLAPI) */
115 #endif /* !defined(LDAP_SLAPI) */
119 slapi_entry_get_dn( Slapi_Entry *e )
121 #if defined(LDAP_SLAPI)
122 return e->e_name.bv_val;
123 #else /* !defined(LDAP_SLAPI) */
125 #endif /* !defined(LDAP_SLAPI) */
133 #if defined(LDAP_SLAPI)
134 struct berval dn = { 0, NULL };
137 dn.bv_len = strlen( ldn );
139 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
140 #endif /* defined(LDAP_SLAPI) */
144 slapi_entry_dup( Slapi_Entry *e )
146 #if defined(LDAP_SLAPI)
151 tmp = slapi_entry2str( e, &len );
153 return (Slapi_Entry *)NULL;
156 tmpEnt = (Slapi_Entry *)str2entry( tmp );
157 if ( tmpEnt == NULL ) {
158 slapi_ch_free( (void **)&tmp );
159 return (Slapi_Entry *)NULL;
163 slapi_ch_free( (void **)&tmp );
167 #else /* !defined(LDAP_SLAPI) */
169 #endif /* !defined(LDAP_SLAPI) */
173 slapi_entry_attr_delete(
177 #if defined(LDAP_SLAPI)
178 AttributeDescription *ad;
181 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
182 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
185 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
186 return 0; /* attribute is deleted */
188 return -1; /* something went wrong */
190 #else /* !defined(LDAP_SLAPI) */
192 #endif /* !defined(LDAP_SLAPI) */
196 slapi_entry_alloc( void )
198 #if defined(LDAP_SLAPI)
199 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
200 #else /* !defined(LDAP_SLAPI) */
202 #endif /* !defined(LDAP_SLAPI) */
206 slapi_entry_free( Slapi_Entry *e )
208 #if defined(LDAP_SLAPI)
210 #endif /* defined(LDAP_SLAPI) */
214 slapi_entry_attr_merge(
217 struct berval **vals )
219 #if defined(LDAP_SLAPI)
220 AttributeDescription *ad;
225 rc = bvptr2obj( vals, &bv );
226 if ( rc != LDAP_SUCCESS ) {
230 rc = slap_str2ad( type, &ad, &text );
231 if ( rc != LDAP_SUCCESS ) {
235 rc = attr_merge( e, ad, bv );
239 #else /* !defined(LDAP_SLAPI) */
241 #endif /* !defined(LDAP_SLAPI) */
245 slapi_entry_attr_find(
250 #if defined(LDAP_SLAPI)
251 AttributeDescription *ad;
255 rc = slap_str2ad( type, &ad, &text );
256 if ( rc != LDAP_SUCCESS ) {
260 *attr = attr_find( e->e_attrs, ad );
261 if ( *attr == NULL ) {
266 #else /* !defined(LDAP_SLAPI) */
268 #endif /* !defined(LDAP_SLAPI) */
272 * FIXME -- The caller must free the allocated memory.
273 * In Netscape they do not have to.
276 slapi_attr_get_values(
278 struct berval ***vals )
280 #if defined(LDAP_SLAPI)
284 if ( attr == NULL ) {
288 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
292 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
293 for ( j = 0; j < i; j++ ) {
294 bv[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
295 bv[j]->bv_val = ch_strdup( attr->a_vals[j].bv_val );
296 bv[j]->bv_len = attr->a_vals[j].bv_len;
300 *vals = (struct berval **)bv;
303 #else /* !defined(LDAP_SLAPI) */
305 #endif /* !defined(LDAP_SLAPI) */
309 slapi_dn_normalize( char *dn )
311 #if defined(LDAP_SLAPI)
315 assert( dn != NULL );
318 bdn.bv_len = strlen( dn );
320 dnNormalize2( NULL, &bdn, &ndn );
323 * FIXME: ain't it safe to set dn = ndn.bv_val ?
325 dn = ch_strdup( ndn.bv_val );
326 ch_free( ndn.bv_val );
329 #else /* !defined(LDAP_SLAPI) */
331 #endif /* !defined(LDAP_SLAPI) */
335 * FIXME: this function is dangerous and should be deprecated;
336 * DN normalization is a lot more than lower-casing, and BTW
337 * OpenLDAP's DN normalization for case insensitive attributes
338 * is already lower case
341 slapi_dn_normalize_case( char *dn )
343 #if defined(LDAP_SLAPI)
344 slapi_dn_normalize( dn );
345 ldap_pvt_str2lower( dn );
348 #else /* defined(LDAP_SLAPI) */
350 #endif /* defined(LDAP_SLAPI) */
358 #if defined(LDAP_SLAPI)
359 struct berval bdn, ndn;
360 struct berval bsuffix, nsuffix;
362 assert( dn != NULL );
363 assert( suffix != NULL );
366 bdn.bv_len = strlen( dn );
368 bsuffix.bv_val = suffix;
369 bsuffix.bv_len = strlen( suffix );
371 dnNormalize2( NULL, &bdn, &ndn );
372 dnNormalize2( NULL, &bsuffix, &nsuffix );
374 return dnIsSuffix( &ndn, &nsuffix );
375 #else /* !defined(LDAP_SLAPI) */
377 #endif /* !defined(LDAP_SLAPI) */
381 slapi_dn_ignore_case( char *dn )
383 #if defined(LDAP_SLAPI)
384 return slapi_dn_normalize_case( dn );
385 #else /* !defined(LDAP_SLAPI) */
387 #endif /* !defined(LDAP_SLAPI) */
391 slapi_ch_malloc( unsigned long size )
393 #if defined(LDAP_SLAPI)
394 return ch_malloc( size );
395 #else /* !defined(LDAP_SLAPI) */
397 #endif /* !defined(LDAP_SLAPI) */
401 slapi_ch_free( void **ptr )
403 #if defined(LDAP_SLAPI)
406 #endif /* defined(LDAP_SLAPI) */
414 #if defined(LDAP_SLAPI)
415 return ch_calloc( nelem, size );
416 #else /* !defined(LDAP_SLAPI) */
418 #endif /* !defined(LDAP_SLAPI) */
426 #if defined(LDAP_SLAPI)
427 return ch_realloc( block, size );
428 #else /* !defined(LDAP_SLAPI) */
430 #endif /* !defined(LDAP_SLAPI) */
434 slapi_ch_strdup( char *s )
436 #if defined(LDAP_SLAPI)
437 return ch_strdup( (const char *)s );
438 #else /* !defined(LDAP_SLAPI) */
440 #endif /* !defined(LDAP_SLAPI) */
444 slapi_ch_stlen( char *s )
446 #if defined(LDAP_SLAPI)
447 return strlen( (const char *)s );
448 #else /* !defined(LDAP_SLAPI) */
450 #endif /* !defined(LDAP_SLAPI) */
454 slapi_control_present(
455 LDAPControl **controls,
460 #if defined(LDAP_SLAPI)
472 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
473 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
478 if ( controls[i]->ldctl_value.bv_len != 0 ) {
480 * FIXME: according to 6.1 specification,
481 * "The val output parameter is set
482 * to point into the controls array.
483 * A copy of the control value is
487 struct berval *pTmpBval;
489 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
490 if ( pTmpBval == NULL ) {
493 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
494 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
498 slapi_ch_free( (void **)&pTmpBval );
504 *val = &controls[i]->ldctl_value;
509 *iscritical = controls[i]->ldctl_iscritical;
516 #else /* !defined(LDAP_SLAPI) */
518 #endif /* !defined(LDAP_SLAPI) */
522 slapi_register_supported_control(
524 unsigned long controlops )
526 #if defined(LDAP_SLAPI)
527 /* FIXME -- can not add controls to openLDAP dynamically */
528 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_CONTROLS",
529 "can not add controls to openLDAP dynamically\n" );
530 #endif /* defined(LDAP_SLAPI) */
534 slapi_get_supported_controls(
536 unsigned long **ctrlopsp )
538 #if defined(LDAP_SLAPI)
542 unsigned long *masks = NULL;
544 for (n = 0; get_supported_ctrl( n ) != NULL; n++) {
556 oids = (char **)slapi_ch_malloc( (n + 1) * sizeof(char *) );
557 if ( oids == NULL ) {
562 masks = (unsigned long *)slapi_ch_malloc( n * sizeof(int) );
563 if ( masks == NULL ) {
568 for ( i = 0; i < n; i++ ) {
570 * FIXME: Netscape's specification says nothing about
571 * memory; should we copy the OIDs or return pointers
572 * to internal values? In OpenLDAP the latter is safe
573 * since we do not allow to register coltrols runtime
575 oids[ i ] = ch_strdup( get_supported_ctrl( i ) );
576 if ( oids[ i ] == NULL ) {
580 masks[ i ] = (unsigned long)get_supported_ctrl_mask( i );
588 if ( rc != LDAP_SUCCESS ) {
589 for ( i = 0; oids != NULL && oids[ i ] != NULL; i++ ) {
590 ch_free( oids[ i ] );
597 #else /* !defined(LDAP_SLAPI) */
599 #endif /* !defined(LDAP_SLAPI) */
603 slapi_register_supported_saslmechanism( char *mechanism )
605 #if defined(LDAP_SLAPI)
606 /* FIXME -- can not add saslmechanism to openLDAP dynamically */
607 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
608 "can not add saslmechanism to openLDAP dynamically\n" );
609 #endif /* defined(LDAP_SLAPI) */
613 slapi_get_supported_saslmechanisms( void )
615 #if defined(LDAP_SLAPI)
616 /* FIXME -- can not get the saslmechanism wihtout a connection. */
617 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
618 "can not get the saslmechanism "
619 "wihtout a connection\n" );
621 #else /* defined(LDAP_SLAPI) */
623 #endif /* defined(LDAP_SLAPI) */
627 slapi_get_supported_extended_ops( void )
629 #if defined(LDAP_SLAPI)
631 char **ppExtOpOID = NULL;
634 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
638 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
643 if ( numExtOps == 0 ) {
647 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
648 for ( k = 0; k < i; k++ ) {
651 bv = get_supported_extop( k );
652 assert( bv != NULL );
654 ppExtOpOID[ k ] = bv->bv_val;
657 for ( ; k < j; k++ ) {
660 bv = ns_get_supported_extop( k );
661 assert( bv != NULL );
663 ppExtOpOID[ i + k ] = bv->bv_val;
665 ppExtOpOID[ i + k ] = NULL;
668 #else /* !defined(LDAP_SLAPI) */
670 #endif /* !defined(LDAP_SLAPI) */
674 slapi_send_ldap_result(
680 struct berval **urls )
682 #if defined(LDAP_SLAPI)
687 struct berval *extValue = NULL;
690 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
691 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
692 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
693 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, &s );
694 rc = LDAP_SASL_BIND_IN_PROGRESS;
695 send_ldap_sasl( conn, op, rc, NULL, NULL, NULL, NULL, s );
699 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
700 if ( extOID != NULL ) {
701 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &extValue );
702 slapi_send_ldap_extended_response( conn, op, err, extOID,
707 send_ldap_result( conn, op, err, matched, text, NULL, NULL );
708 #endif /* defined(LDAP_SLAPI) */
712 slapi_send_ldap_search_entry(
715 LDAPControl **ectrls,
719 #if defined(LDAP_SLAPI)
726 AttributeName *an = NULL;
729 for ( i = 0; attrs[ i ] != NULL; i++ ) {
734 an = (AttributeName *) ch_malloc( i * sizeof(AttributeName) );
735 for ( i = 0; attrs[i] != NULL; i++ ) {
736 an[i].an_name.bv_val = ch_strdup( attrs[i] );
737 an[i].an_name.bv_len = strlen( attrs[i] );
738 an[i].an_desc = NULL;
739 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
744 if ( ( rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) ||
745 ( rc = slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&pConn) != 0 ) ||
746 ( rc = slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp) != 0 ) ) {
749 rc = send_search_entry( be, pConn, pOp, e, an, attrsonly, NULL );
754 #else /* !defined(LDAP_SLAPI) */
756 #endif /* !defined(LDAP_SLAPI) */
761 slapi_str2filter( char *str )
763 #if defined(LDAP_SLAPI)
764 return str2filter( str );
765 #else /* !defined(LDAP_SLAPI) */
767 #endif /* !defined(LDAP_SLAPI) */
775 #if defined(LDAP_SLAPI)
777 #endif /* defined(LDAP_SLAPI) */
781 slapi_filter_get_choice( Slapi_Filter *f )
783 #if defined(LDAP_SLAPI)
793 #else /* !defined(LDAP_SLAPI) */
794 return -1; /* invalid filter type */
795 #endif /* !defined(LDAP_SLAPI) */
799 slapi_filter_get_ava(
802 struct berval **bval )
804 #if defined(LDAP_SLAPI)
806 int rc = LDAP_SUCCESS;
808 assert( type != NULL );
809 assert( bval != NULL );
815 if ( ftype == LDAP_FILTER_EQUALITY
816 || ftype == LDAP_FILTER_GE
817 || ftype == LDAP_FILTER_LE
818 || ftype == LDAP_FILTER_APPROX ) {
819 *type = slapi_ch_strdup( f->f_un.f_un_ava->aa_desc->ad_cname.bv_val );
820 if ( *type == NULL ) {
825 *bval = (struct berval *)slapi_ch_malloc( sizeof(struct berval) );
826 if ( *bval == NULL ) {
831 (*bval)->bv_len = f->f_un.f_un_ava->aa_value.bv_len;
832 (*bval)->bv_val = slapi_ch_strdup( f->f_un.f_un_ava->aa_value.bv_val );
833 if ( (*bval)->bv_val == NULL ) {
837 } else { /* filter type not supported */
842 if ( rc != LDAP_SUCCESS ) {
855 #else /* !defined(LDAP_SLAPI) */
857 #endif /* !defined(LDAP_SLAPI) */
861 slapi_filter_list_first( Slapi_Filter *f )
863 #if defined(LDAP_SLAPI)
871 if ( ftype == LDAP_FILTER_AND
872 || ftype == LDAP_FILTER_OR
873 || ftype == LDAP_FILTER_NOT ) {
874 return (Slapi_Filter *)f->f_and;
878 #else /* !defined(LDAP_SLAPI) */
880 #endif /* !defined(LDAP_SLAPI) */
884 slapi_filter_list_next(
886 Slapi_Filter *fprev )
888 #if defined(LDAP_SLAPI)
896 if ( ftype == LDAP_FILTER_AND
897 || ftype == LDAP_FILTER_OR
898 || ftype == LDAP_FILTER_NOT ) {
899 if ( f->f_and == fprev ) {
900 return f->f_and->f_next;
905 #else /* !defined(LDAP_SLAPI) */
907 #endif /* !defined(LDAP_SLAPI) */
911 slapi_send_ldap_extended_response(
916 struct berval *response )
918 #if defined(LDAP_SLAPI)
919 send_ldap_extended( conn,op, errornum, NULL, NULL, NULL,
920 respName,response, NULL );
922 #else /* !defined(LDAP_SLAPI) */
924 #endif /* !defined(LDAP_SLAPI) */
929 struct berval **vals,
932 #if defined(LDAP_SLAPI)
934 * FIXME: what's the point?
937 #else /* !defined(LDAP_SLAPI) */
939 #endif /* !defined(LDAP_SLAPI) */
943 slapi_get_hostname( void )
945 #if defined(LDAP_SLAPI)
949 * FIXME: I'd prefer a different check ...
952 hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
954 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
955 "can't malloc memory for hostname\n" );
958 } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
959 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
960 "can't get hostname\n" );
961 slapi_ch_free( (void **)&hn );
965 static int been_here = 0;
966 static char *static_hn = NULL;
968 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
970 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
971 if ( static_hn == NULL) {
972 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
973 "can't malloc memory for hostname\n" );
975 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
980 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
981 slapi_log_error( SLAPI_LOG_FATAL,
983 "can't get hostname\n" );
984 slapi_ch_free( (void **)&static_hn );
986 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
995 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
997 hn = ch_strdup( static_hn );
1001 #else /* !defined(LDAP_SLAPI) */
1003 #endif /* !defined(LDAP_SLAPI) */
1007 * FIXME: this should go in an appropriate header ...
1009 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
1018 #if defined(LDAP_SLAPI)
1019 int rc = LDAP_SUCCESS;
1022 va_start( arglist, fmt );
1023 rc = vLogError( severity, subsystem, fmt, arglist );
1027 #else /* !defined(LDAP_SLAPI) */
1029 #endif /* !defined(LDAP_SLAPI) */
1034 slapi_timer_current_time( void )
1036 #if defined(LDAP_SLAPI)
1037 static int first_time = 1;
1038 #if !defined (_WIN32)
1042 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1045 gettimeofday( &base_time, NULL );
1047 gettimeofday( &now, NULL );
1048 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1049 (now.tv_usec - base_time.tv_usec);
1050 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1056 return (slap_get_time() - starttime) * 1000000;
1063 performance_counter_present = QueryPerformanceCounter( &base_time );
1064 QueryPerformanceFrequency( &performance_freq );
1067 if ( !performance_counter_present )
1070 QueryPerformanceCounter( &now );
1071 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1073 #else /* !defined(LDAP_SLAPI) */
1075 #endif /* !defined(LDAP_SLAPI) */
1082 slapi_timer_get_time( char *label )
1084 #if defined(LDAP_SLAPI)
1085 unsigned long start = slapi_timer_current_time();
1086 printf("%10ld %10ld usec %s\n", start, 0, label);
1088 #else /* !defined(LDAP_SLAPI) */
1090 #endif /* !defined(LDAP_SLAPI) */
1097 slapi_timer_elapsed_time(
1099 unsigned long start )
1101 #if defined(LDAP_SLAPI)
1102 unsigned long stop = slapi_timer_current_time();
1103 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
1104 #endif /* defined(LDAP_SLAPI) */
1108 slapi_free_search_results_internal( Slapi_PBlock *pb )
1110 #if defined(LDAP_SLAPI)
1111 Slapi_Entry **entries;
1112 int k = 0, nEnt = 0;
1114 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
1115 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
1120 if ( entries == NULL ) {
1124 for ( k = 0; k < nEnt; k++ ) {
1125 slapi_entry_free( entries[k] );
1128 slapi_ch_free( (void **)&entries );
1129 #endif /* defined(LDAP_SLAPI) */
1133 * Internal API to prime a Slapi_PBlock with a Backend.
1135 int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
1137 #if defined(LDAP_SLAPI)
1140 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
1141 if ( rc != LDAP_SUCCESS )
1144 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
1145 if ( rc != LDAP_SUCCESS )
1148 return LDAP_SUCCESS;
1151 #endif /* defined(LDAP_SLAPI) */
1154 #if defined(LDAP_SLAPI)
1156 * If oldStyle is TRUE, then a value suitable for setting to
1157 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
1158 * (pointer to static storage).
1160 * If oldStyle is FALSE, then a value suitable for setting to
1161 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
1162 * a pointer to allocated memory and will include the SASL
1163 * mechanism (if any).
1165 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
1170 switch ( authz->sai_method ) {
1171 case LDAP_AUTH_SASL:
1173 authType = SLAPD_AUTH_SASL;
1175 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
1176 authType = slapi_ch_malloc( len );
1177 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
1180 case LDAP_AUTH_SIMPLE:
1181 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
1183 case LDAP_AUTH_NONE:
1184 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
1190 if ( is_tls && authType == NULL ) {
1191 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
1199 * Internal API to prime a Slapi_PBlock with a Connection.
1201 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
1203 #if defined(LDAP_SLAPI)
1207 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
1208 if ( rc != LDAP_SUCCESS )
1211 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
1212 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
1213 if ( rc != LDAP_SUCCESS )
1217 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
1218 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
1219 if ( rc != LDAP_SUCCESS )
1223 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
1224 if ( rc != LDAP_SUCCESS )
1227 /* Returns pointer to static string */
1228 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
1229 if ( connAuthType != NULL ) {
1230 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
1231 if ( rc != LDAP_SUCCESS )
1235 /* Returns pointer to allocated string */
1236 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
1237 if ( connAuthType != NULL ) {
1238 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
1239 if ( rc != LDAP_SUCCESS )
1243 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
1244 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
1245 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
1246 if ( rc != LDAP_SUCCESS )
1251 #endif /* defined(LDAP_SLAPI) */
1255 * Internal API to prime a Slapi_PBlock with an Operation.
1257 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
1259 #if defined(LDAP_SLAPI)
1266 if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
1270 isRoot = be_isroot( be, &op->o_ndn );
1271 isUpdateDn = be_isupdate( be, &op->o_ndn );
1274 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
1275 if ( rc != LDAP_SUCCESS )
1278 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
1279 if ( rc != LDAP_SUCCESS )
1282 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
1283 if ( rc != LDAP_SUCCESS )
1286 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
1287 if ( rc != LDAP_SUCCESS )
1290 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
1291 if ( rc != LDAP_SUCCESS )
1294 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
1295 if ( rc != LDAP_SUCCESS )
1298 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
1299 if ( rc != LDAP_SUCCESS)
1302 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
1303 if ( rc != LDAP_SUCCESS )
1306 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
1307 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
1308 /* Not quite sure what the point of this is. */
1309 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
1310 if ( rc != LDAP_SUCCESS )
1314 return LDAP_SUCCESS;
1320 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
1322 #if defined( LDAP_SLAPI )
1325 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1326 *isSSL = conn->c_is_tls;
1328 return LDAP_SUCCESS;
1331 #endif /* defined(LDAP_SLAPI) */
1335 * DS 5.x compatability API follow
1338 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
1340 #if defined( LDAP_SLAPI )
1344 return LDAP_PARAM_ERROR;
1346 at = attr->a_desc->ad_type;
1348 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
1350 if ( is_at_single_value( at ) )
1351 *flags |= SLAPI_ATTR_FLAG_SINGLE;
1352 if ( is_at_operational( at ) )
1353 *flags |= SLAPI_ATTR_FLAG_OPATTR;
1354 if ( is_at_obsolete( at ) )
1355 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
1356 if ( is_at_collective( at ) )
1357 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
1358 if ( is_at_no_user_mod( at ) )
1359 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
1361 return LDAP_SUCCESS;
1364 #endif /* defined(LDAP_SLAPI) */
1367 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
1369 #if defined( LDAP_SLAPI )
1370 unsigned long flags;
1372 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
1374 return (flags & flag) ? 1 : 0;
1377 #endif /* defined(LDAP_SLAPI) */
1380 Slapi_Attr *slapi_attr_new( void )
1385 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
1393 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
1397 AttributeDescription *ad;
1399 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
1414 void slapi_attr_free( Slapi_Attr **a )
1422 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
1425 return attr_dup( (Slapi_Attr *)attr );
1431 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
1434 return value_add_one( &a->a_vals, (Slapi_Value *)v );
1440 int slapi_attr_type2plugin( const char *type, void **pi )
1447 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
1450 if ( attr == NULL ) {
1451 return LDAP_PARAM_ERROR;
1454 *type = attr->a_desc->ad_cname.bv_val;
1456 return LDAP_SUCCESS;
1462 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
1465 if ( attr == NULL ) {
1466 return LDAP_PARAM_ERROR;
1468 *oidp = attr->a_desc->ad_type->sat_oid;
1470 return LDAP_SUCCESS;
1476 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
1484 mr = a->a_desc->ad_type->sat_equality;
1485 rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
1486 (struct berval *)v1, (void *)v2, &text );
1487 if ( rc != LDAP_SUCCESS )
1490 return ( ret == 0 ) ? 0 : -1;
1496 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
1506 mr = a->a_desc->ad_type->sat_equality;
1507 for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
1508 rc = value_match( &ret, a->a_desc, mr,
1509 SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
1510 if ( rc != LDAP_SUCCESS ) {
1521 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
1524 AttributeDescription *a1;
1525 AttributeDescription *a2;
1529 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
1533 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
1537 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
1538 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
1539 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
1542 case SLAPI_TYPE_CMP_EXACT:
1543 ret = ad_cmp( a1, a2 );
1545 case SLAPI_TYPE_CMP_BASE:
1546 ret = ad_base_cmp( a1, a2 );
1548 case SLAPI_TYPE_CMP_SUBTYPE:
1549 ret = is_ad_subtype( a2, a2 );
1562 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
1565 return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
1571 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
1574 return slapi_valueset_first_value( &a->a_vals, v );
1580 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
1583 return slapi_valueset_next_value( &a->a_vals, hint, v );
1589 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
1592 *numValues = slapi_valueset_count( &a->a_vals );
1600 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
1603 *vs = &((Slapi_Attr *)a)->a_vals;
1611 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
1614 return slapi_attr_get_values( a, vals );
1620 char *slapi_attr_syntax_normalize( const char *s )
1623 AttributeDescription *ad;
1626 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
1630 return ad->ad_cname.bv_val;
1636 Slapi_Value *slapi_value_new( void )
1641 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
1649 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
1652 return ber_dupbv( NULL, (struct berval *)bval );
1658 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
1661 return slapi_value_new_berval( v );
1667 Slapi_Value *slapi_value_new_string(const char *s)
1672 bv.bv_val = (char *)s;
1673 bv.bv_len = strlen( s );
1675 return slapi_value_new_berval( &bv );
1681 Slapi_Value *slapi_value_init(Slapi_Value *val)
1693 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
1696 return ber_dupbv( v, bval );
1702 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
1705 v->bv_val = slapi_ch_strdup( (char *)s );
1706 v->bv_len = strlen( s );
1714 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
1717 return slapi_value_new_value( v );
1723 void slapi_value_free(Slapi_Value **value)
1726 if ( value == NULL ) {
1729 if ( *value != NULL ) {
1732 slapi_ch_free( (void **)&v->bv_val );
1733 slapi_ch_free( (void **)&v );
1738 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
1747 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
1750 if ( value == NULL ) {
1753 if ( value->bv_val != NULL ) {
1754 slapi_ch_free( (void **)&value->bv_val );
1756 slapi_value_init_berval( value, (struct berval *)bval );
1764 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
1767 if ( value == NULL ) {
1770 return slapi_value_set_berval( value, vfrom );
1776 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
1779 if ( value == NULL ) {
1782 if ( value->bv_val != NULL ) {
1783 slapi_ch_free( (void **)&value->bv_val );
1785 value->bv_val = slapi_ch_malloc( len );
1786 value->bv_len = len;
1787 AC_MEMCPY( value->bv_val, val, len );
1795 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
1798 if ( value == NULL ) {
1801 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
1808 int slapi_value_set_int(Slapi_Value *value, int intVal)
1813 snprintf( buf, sizeof( buf ), "%d", intVal );
1815 return slapi_value_set_string( value, buf );
1821 const char *slapi_value_get_string(const Slapi_Value *value)
1824 if ( value == NULL ) {
1827 return value->bv_val;
1834 static int checkBVString(const struct berval *bv)
1838 for ( i = 0; i < bv->bv_len; i++ ) {
1839 if ( bv->bv_val[i] == '\0' )
1842 if ( bv->bv_val[i] != '\0' )
1849 int slapi_value_get_int(const Slapi_Value *value)
1852 if ( value == NULL ) return 0;
1853 if ( !checkBVString( value ) ) return 0;
1855 return (int)strtol( value->bv_val, NULL, 10 );
1861 unsigned int slapi_value_get_uint(const Slapi_Value *value)
1864 if ( value == NULL ) return 0;
1865 if ( !checkBVString( value ) ) return 0;
1867 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
1873 long slapi_value_get_long(const Slapi_Value *value)
1876 if ( value == NULL ) return 0;
1877 if ( !checkBVString( value ) ) return 0;
1879 return strtol( value->bv_val, NULL, 10 );
1885 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
1888 if ( value == NULL ) return 0;
1889 if ( !checkBVString( value ) ) return 0;
1891 return strtoul( value->bv_val, NULL, 10 );
1897 size_t slapi_value_get_length(const Slapi_Value *value)
1900 if ( value == NULL )
1903 return (size_t) value->bv_len;
1909 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
1912 return slapi_attr_value_cmp( a, v1, v2 );
1918 /* A ValueSet is a container for a BerVarray. */
1919 Slapi_ValueSet *slapi_valueset_new( void )
1924 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
1933 void slapi_valueset_free(Slapi_ValueSet *vs)
1937 ber_bvarray_free( *vs );
1943 void slapi_valueset_init(Slapi_ValueSet *vs)
1946 if ( vs != NULL && *vs == NULL ) {
1947 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
1948 (*vs)->bv_val = NULL;
1954 void slapi_valueset_done(Slapi_ValueSet *vs)
1962 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
1964 slapi_ch_free( (void **)&vp->bv_val );
1966 /* but don't free *vs or vs */
1970 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
1973 ber_bvarray_add( vs, (Slapi_Value *)addval );
1977 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
1980 return slapi_valueset_next_value( vs, 0, v );
1986 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
1997 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2008 int slapi_valueset_count( const Slapi_ValueSet *vs )
2019 for ( i = 0; vp[i].bv_val != NULL; i++ )
2029 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2034 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2035 slapi_valueset_add_value( vs1, vp );