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"
24 struct berval *ns_get_supported_extop( int );
27 #include <sys/systeminfo.h>
33 * server start time (should we use a struct timeval also in slapd?
35 static struct timeval base_time;
36 ldap_pvt_thread_mutex_t slapi_hn_mutex;
37 ldap_pvt_thread_mutex_t slapi_time_mutex;
40 * This function converts an array of pointers to berval objects to
41 * an array of berval objects.
46 struct berval **bvptr,
49 int rc = LDAP_SUCCESS;
53 if ( bvptr == NULL || *bvptr == NULL ) {
57 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
61 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
62 if ( tmpberval == NULL ) {
63 return LDAP_NO_MEMORY;
66 for ( i = 0; bvptr[i] != NULL; i++ ) {
67 tmpberval[i].bv_val = bvptr[i]->bv_val;
68 tmpberval[i].bv_len = bvptr[i]->bv_len;
71 if ( rc == LDAP_SUCCESS ) {
83 #if defined(LDAP_SLAPI)
84 Slapi_Entry *e = NULL;
87 pTmpS = slapi_ch_strdup( s );
88 if ( pTmpS != NULL ) {
89 e = str2entry( pTmpS );
90 slapi_ch_free( (void **)&pTmpS );
94 #else /* !defined(LDAP_SLAPI) */
96 #endif /* !defined(LDAP_SLAPI) */
104 #if defined(LDAP_SLAPI)
107 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
108 ret = entry2str( e, len );
109 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
112 #else /* !defined(LDAP_SLAPI) */
114 #endif /* !defined(LDAP_SLAPI) */
118 slapi_entry_get_dn( Slapi_Entry *e )
120 #if defined(LDAP_SLAPI)
121 return e->e_name.bv_val;
122 #else /* !defined(LDAP_SLAPI) */
124 #endif /* !defined(LDAP_SLAPI) */
132 #if defined(LDAP_SLAPI)
133 struct berval dn = { 0, NULL };
136 dn.bv_len = strlen( ldn );
138 dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
139 #endif /* defined(LDAP_SLAPI) */
143 slapi_entry_dup( Slapi_Entry *e )
145 #if defined(LDAP_SLAPI)
150 tmp = slapi_entry2str( e, &len );
152 return (Slapi_Entry *)NULL;
155 tmpEnt = (Slapi_Entry *)str2entry( tmp );
156 if ( tmpEnt == NULL ) {
157 slapi_ch_free( (void **)&tmp );
158 return (Slapi_Entry *)NULL;
162 slapi_ch_free( (void **)&tmp );
166 #else /* !defined(LDAP_SLAPI) */
168 #endif /* !defined(LDAP_SLAPI) */
172 slapi_entry_attr_delete(
176 #if defined(LDAP_SLAPI)
177 AttributeDescription *ad;
180 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
181 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
184 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
185 return 0; /* attribute is deleted */
187 return -1; /* something went wrong */
189 #else /* !defined(LDAP_SLAPI) */
191 #endif /* !defined(LDAP_SLAPI) */
195 slapi_entry_alloc( void )
197 #if defined(LDAP_SLAPI)
198 return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
199 #else /* !defined(LDAP_SLAPI) */
201 #endif /* !defined(LDAP_SLAPI) */
205 slapi_entry_free( Slapi_Entry *e )
207 #if defined(LDAP_SLAPI)
209 #endif /* defined(LDAP_SLAPI) */
213 slapi_entry_attr_merge(
216 struct berval **vals )
218 #if defined(LDAP_SLAPI)
219 AttributeDescription *ad;
224 rc = bvptr2obj( vals, &bv );
225 if ( rc != LDAP_SUCCESS ) {
229 rc = slap_str2ad( type, &ad, &text );
230 if ( rc != LDAP_SUCCESS ) {
234 rc = attr_merge( e, ad, bv );
238 #else /* !defined(LDAP_SLAPI) */
240 #endif /* !defined(LDAP_SLAPI) */
244 slapi_entry_attr_find(
249 #if defined(LDAP_SLAPI)
250 AttributeDescription *ad;
254 rc = slap_str2ad( type, &ad, &text );
255 if ( rc != LDAP_SUCCESS ) {
259 *attr = attr_find( e->e_attrs, ad );
260 if ( *attr == NULL ) {
265 #else /* !defined(LDAP_SLAPI) */
267 #endif /* !defined(LDAP_SLAPI) */
271 * FIXME -- The caller must free the allocated memory.
272 * In Netscape they do not have to.
275 slapi_attr_get_values(
277 struct berval ***vals )
279 #if defined(LDAP_SLAPI)
283 if ( attr == NULL ) {
287 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
291 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
292 for ( j = 0; j < i; j++ ) {
293 bv[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
294 bv[j]->bv_val = ch_strdup( attr->a_vals[j].bv_val );
295 bv[j]->bv_len = attr->a_vals[j].bv_len;
299 *vals = (struct berval **)bv;
302 #else /* !defined(LDAP_SLAPI) */
304 #endif /* !defined(LDAP_SLAPI) */
308 slapi_dn_normalize( char *dn )
310 #if defined(LDAP_SLAPI)
314 assert( dn != NULL );
317 bdn.bv_len = strlen( dn );
319 dnNormalize2( NULL, &bdn, &ndn );
322 * FIXME: ain't it safe to set dn = ndn.bv_val ?
324 dn = ch_strdup( ndn.bv_val );
325 ch_free( ndn.bv_val );
328 #else /* !defined(LDAP_SLAPI) */
330 #endif /* !defined(LDAP_SLAPI) */
334 * FIXME: this function is dangerous and should be deprecated;
335 * DN normalization is a lot more than lower-casing, and BTW
336 * OpenLDAP's DN normalization for case insensitive attributes
337 * is already lower case
340 slapi_dn_normalize_case( char *dn )
342 #if defined(LDAP_SLAPI)
343 slapi_dn_normalize( dn );
344 ldap_pvt_str2lower( dn );
347 #else /* defined(LDAP_SLAPI) */
349 #endif /* defined(LDAP_SLAPI) */
357 #if defined(LDAP_SLAPI)
358 struct berval bdn, ndn;
359 struct berval bsuffix, nsuffix;
361 assert( dn != NULL );
362 assert( suffix != NULL );
365 bdn.bv_len = strlen( dn );
367 bsuffix.bv_val = suffix;
368 bsuffix.bv_len = strlen( suffix );
370 dnNormalize2( NULL, &bdn, &ndn );
371 dnNormalize2( NULL, &bsuffix, &nsuffix );
373 return dnIsSuffix( &ndn, &nsuffix );
374 #else /* !defined(LDAP_SLAPI) */
376 #endif /* !defined(LDAP_SLAPI) */
380 slapi_dn_ignore_case( char *dn )
382 #if defined(LDAP_SLAPI)
383 return slapi_dn_normalize_case( dn );
384 #else /* !defined(LDAP_SLAPI) */
386 #endif /* !defined(LDAP_SLAPI) */
390 slapi_ch_malloc( unsigned long size )
392 #if defined(LDAP_SLAPI)
393 return ch_malloc( size );
394 #else /* !defined(LDAP_SLAPI) */
396 #endif /* !defined(LDAP_SLAPI) */
400 slapi_ch_free( void **ptr )
402 #if defined(LDAP_SLAPI)
405 #endif /* defined(LDAP_SLAPI) */
413 #if defined(LDAP_SLAPI)
414 return ch_calloc( nelem, size );
415 #else /* !defined(LDAP_SLAPI) */
417 #endif /* !defined(LDAP_SLAPI) */
425 #if defined(LDAP_SLAPI)
426 return ch_realloc( block, size );
427 #else /* !defined(LDAP_SLAPI) */
429 #endif /* !defined(LDAP_SLAPI) */
433 slapi_ch_strdup( char *s )
435 #if defined(LDAP_SLAPI)
436 return ch_strdup( (const char *)s );
437 #else /* !defined(LDAP_SLAPI) */
439 #endif /* !defined(LDAP_SLAPI) */
443 slapi_ch_stlen( char *s )
445 #if defined(LDAP_SLAPI)
446 return strlen( (const char *)s );
447 #else /* !defined(LDAP_SLAPI) */
449 #endif /* !defined(LDAP_SLAPI) */
453 slapi_control_present(
454 LDAPControl **controls,
459 #if defined(LDAP_SLAPI)
471 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
472 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
477 if ( controls[i]->ldctl_value.bv_len != 0 ) {
479 * FIXME: according to 6.1 specification,
480 * "The val output parameter is set
481 * to point into the controls array.
482 * A copy of the control value is
486 struct berval *pTmpBval;
488 pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
489 if ( pTmpBval == NULL ) {
492 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
493 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
497 slapi_ch_free( (void **)&pTmpBval );
503 *val = &controls[i]->ldctl_value;
508 *iscritical = controls[i]->ldctl_iscritical;
515 #else /* !defined(LDAP_SLAPI) */
517 #endif /* !defined(LDAP_SLAPI) */
521 slapi_register_supported_control(
523 unsigned long controlops )
525 #if defined(LDAP_SLAPI)
526 /* FIXME -- can not add controls to openLDAP dynamically */
527 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_CONTROLS",
528 "can not add controls to openLDAP dynamically\n" );
529 #endif /* defined(LDAP_SLAPI) */
533 slapi_get_supported_controls(
535 unsigned long **ctrlopsp )
537 #if defined(LDAP_SLAPI)
541 unsigned long *masks = NULL;
543 for (n = 0; get_supported_ctrl( n ) != NULL; n++) {
555 oids = (char **)slapi_ch_malloc( (n + 1) * sizeof(char *) );
556 if ( oids == NULL ) {
561 masks = (unsigned long *)slapi_ch_malloc( n * sizeof(int) );
562 if ( masks == NULL ) {
567 for ( i = 0; i < n; i++ ) {
569 * FIXME: Netscape's specification says nothing about
570 * memory; should we copy the OIDs or return pointers
571 * to internal values? In OpenLDAP the latter is safe
572 * since we do not allow to register coltrols runtime
574 oids[ i ] = ch_strdup( get_supported_ctrl( i ) );
575 if ( oids[ i ] == NULL ) {
579 masks[ i ] = (unsigned long)get_supported_ctrl_mask( i );
587 if ( rc != LDAP_SUCCESS ) {
588 for ( i = 0; oids != NULL && oids[ i ] != NULL; i++ ) {
589 ch_free( oids[ i ] );
596 #else /* !defined(LDAP_SLAPI) */
598 #endif /* !defined(LDAP_SLAPI) */
602 slapi_register_supported_saslmechanism( char *mechanism )
604 #if defined(LDAP_SLAPI)
605 /* FIXME -- can not add saslmechanism to openLDAP dynamically */
606 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
607 "can not add saslmechanism to openLDAP dynamically\n" );
608 #endif /* defined(LDAP_SLAPI) */
612 slapi_get_supported_saslmechanisms( void )
614 #if defined(LDAP_SLAPI)
615 /* FIXME -- can not get the saslmechanism wihtout a connection. */
616 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
617 "can not get the saslmechanism "
618 "wihtout a connection\n" );
620 #else /* defined(LDAP_SLAPI) */
622 #endif /* defined(LDAP_SLAPI) */
626 slapi_get_supported_extended_ops( void )
628 #if defined(LDAP_SLAPI)
630 char **ppExtOpOID = NULL;
633 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
637 for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
642 if ( numExtOps == 0 ) {
646 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
647 for ( k = 0; k < i; k++ ) {
650 bv = get_supported_extop( k );
651 assert( bv != NULL );
653 ppExtOpOID[ k ] = bv->bv_val;
656 for ( ; k < j; k++ ) {
659 bv = ns_get_supported_extop( k );
660 assert( bv != NULL );
662 ppExtOpOID[ i + k ] = bv->bv_val;
664 ppExtOpOID[ i + k ] = NULL;
667 #else /* !defined(LDAP_SLAPI) */
669 #endif /* !defined(LDAP_SLAPI) */
673 slapi_send_ldap_result(
679 struct berval **urls )
681 #if defined(LDAP_SLAPI)
686 struct berval *extValue = NULL;
689 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
690 slapi_pblock_get( pb, SLAPI_OPERATION, &op );
691 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
692 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, &s );
693 rc = LDAP_SASL_BIND_IN_PROGRESS;
694 send_ldap_sasl( conn, op, rc, NULL, NULL, NULL, NULL, s );
698 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
699 if ( extOID != NULL ) {
700 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &extValue );
701 slapi_send_ldap_extended_response( conn, op, err, extOID,
706 send_ldap_result( conn, op, err, matched, text, NULL, NULL );
707 #endif /* defined(LDAP_SLAPI) */
711 slapi_send_ldap_search_entry(
714 LDAPControl **ectrls,
718 #if defined(LDAP_SLAPI)
725 AttributeName *an = NULL;
728 for ( i = 0; attrs[ i ] != NULL; i++ ) {
733 an = (AttributeName *) ch_malloc( i * sizeof(AttributeName) );
734 for ( i = 0; attrs[i] != NULL; i++ ) {
735 an[i].an_name.bv_val = ch_strdup( attrs[i] );
736 an[i].an_name.bv_len = strlen( attrs[i] );
737 an[i].an_desc = NULL;
738 if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
743 if ( ( rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) ||
744 ( rc = slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&pConn) != 0 ) ||
745 ( rc = slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp) != 0 ) ) {
748 rc = send_search_entry( be, pConn, pOp, e, an, attrsonly, NULL );
753 #else /* !defined(LDAP_SLAPI) */
755 #endif /* !defined(LDAP_SLAPI) */
760 slapi_str2filter( char *str )
762 #if defined(LDAP_SLAPI)
763 return str2filter( str );
764 #else /* !defined(LDAP_SLAPI) */
766 #endif /* !defined(LDAP_SLAPI) */
774 #if defined(LDAP_SLAPI)
776 #endif /* defined(LDAP_SLAPI) */
780 slapi_filter_get_choice( Slapi_Filter *f )
782 #if defined(LDAP_SLAPI)
792 #else /* !defined(LDAP_SLAPI) */
793 return -1; /* invalid filter type */
794 #endif /* !defined(LDAP_SLAPI) */
798 slapi_filter_get_ava(
801 struct berval **bval )
803 #if defined(LDAP_SLAPI)
805 int rc = LDAP_SUCCESS;
807 assert( type != NULL );
808 assert( bval != NULL );
814 if ( ftype == LDAP_FILTER_EQUALITY
815 || ftype == LDAP_FILTER_GE
816 || ftype == LDAP_FILTER_LE
817 || ftype == LDAP_FILTER_APPROX ) {
818 *type = slapi_ch_strdup( f->f_un.f_un_ava->aa_desc->ad_cname.bv_val );
819 if ( *type == NULL ) {
824 *bval = (struct berval *)slapi_ch_malloc( sizeof(struct berval) );
825 if ( *bval == NULL ) {
830 (*bval)->bv_len = f->f_un.f_un_ava->aa_value.bv_len;
831 (*bval)->bv_val = slapi_ch_strdup( f->f_un.f_un_ava->aa_value.bv_val );
832 if ( (*bval)->bv_val == NULL ) {
836 } else { /* filter type not supported */
841 if ( rc != LDAP_SUCCESS ) {
854 #else /* !defined(LDAP_SLAPI) */
856 #endif /* !defined(LDAP_SLAPI) */
860 slapi_filter_list_first( Slapi_Filter *f )
862 #if defined(LDAP_SLAPI)
870 if ( ftype == LDAP_FILTER_AND
871 || ftype == LDAP_FILTER_OR
872 || ftype == LDAP_FILTER_NOT ) {
873 return (Slapi_Filter *)f->f_and;
877 #else /* !defined(LDAP_SLAPI) */
879 #endif /* !defined(LDAP_SLAPI) */
883 slapi_filter_list_next(
885 Slapi_Filter *fprev )
887 #if defined(LDAP_SLAPI)
895 if ( ftype == LDAP_FILTER_AND
896 || ftype == LDAP_FILTER_OR
897 || ftype == LDAP_FILTER_NOT ) {
898 if ( f->f_and == fprev ) {
899 return f->f_and->f_next;
904 #else /* !defined(LDAP_SLAPI) */
906 #endif /* !defined(LDAP_SLAPI) */
910 slapi_send_ldap_extended_response(
915 struct berval *response )
917 #if defined(LDAP_SLAPI)
918 send_ldap_extended( conn,op, errornum, NULL, NULL, NULL,
919 respName,response, NULL );
921 #else /* !defined(LDAP_SLAPI) */
923 #endif /* !defined(LDAP_SLAPI) */
928 struct berval **vals,
931 #if defined(LDAP_SLAPI)
933 * FIXME: what's the point?
936 #else /* !defined(LDAP_SLAPI) */
938 #endif /* !defined(LDAP_SLAPI) */
942 slapi_get_hostname( void )
944 #if defined(LDAP_SLAPI)
948 * FIXME: I'd prefer a different check ...
951 hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
953 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
954 "can't malloc memory for hostname\n" );
957 } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
958 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
959 "can't get hostname\n" );
960 slapi_ch_free( (void **)&hn );
964 static int been_here = 0;
965 static char *static_hn = NULL;
967 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
969 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
970 if ( static_hn == NULL) {
971 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
972 "can't malloc memory for hostname\n" );
974 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
979 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
980 slapi_log_error( SLAPI_LOG_FATAL,
982 "can't get hostname\n" );
983 slapi_ch_free( (void **)&static_hn );
985 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
994 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
996 hn = ch_strdup( static_hn );
1000 #else /* !defined(LDAP_SLAPI) */
1002 #endif /* !defined(LDAP_SLAPI) */
1006 * FIXME: this should go in an appropriate header ...
1008 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
1017 #if defined(LDAP_SLAPI)
1018 int rc = LDAP_SUCCESS;
1021 va_start( arglist, fmt );
1022 rc = vLogError( severity, subsystem, fmt, arglist );
1026 #else /* !defined(LDAP_SLAPI) */
1028 #endif /* !defined(LDAP_SLAPI) */
1033 slapi_timer_current_time( void )
1035 #if defined(LDAP_SLAPI)
1036 static int first_time = 1;
1037 #if !defined (_WIN32)
1041 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1044 gettimeofday( &base_time, NULL );
1046 gettimeofday( &now, NULL );
1047 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1048 (now.tv_usec - base_time.tv_usec);
1049 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1055 return (slap_get_time() - starttime) * 1000000;
1062 performance_counter_present = QueryPerformanceCounter( &base_time );
1063 QueryPerformanceFrequency( &performance_freq );
1066 if ( !performance_counter_present )
1069 QueryPerformanceCounter( &now );
1070 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1072 #else /* !defined(LDAP_SLAPI) */
1074 #endif /* !defined(LDAP_SLAPI) */
1081 slapi_timer_get_time( char *label )
1083 #if defined(LDAP_SLAPI)
1084 unsigned long start = slapi_timer_current_time();
1085 printf("%10ld %10ld usec %s\n", start, 0, label);
1087 #else /* !defined(LDAP_SLAPI) */
1089 #endif /* !defined(LDAP_SLAPI) */
1096 slapi_timer_elapsed_time(
1098 unsigned long start )
1100 #if defined(LDAP_SLAPI)
1101 unsigned long stop = slapi_timer_current_time();
1102 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
1103 #endif /* defined(LDAP_SLAPI) */
1107 slapi_free_search_results_internal( Slapi_PBlock *pb )
1109 #if defined(LDAP_SLAPI)
1110 Slapi_Entry **entries;
1111 int k = 0, nEnt = 0;
1113 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
1114 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
1119 if ( entries == NULL ) {
1123 for ( k = 0; k < nEnt; k++ ) {
1124 slapi_entry_free( entries[k] );
1127 slapi_ch_free( (void **)&entries );
1128 #endif /* defined(LDAP_SLAPI) */
1132 * Internal API to prime a Slapi_PBlock with a Backend.
1134 int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
1136 #if defined(LDAP_SLAPI)
1139 rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
1140 if ( rc != LDAP_SUCCESS )
1143 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
1144 if ( rc != LDAP_SUCCESS )
1147 return LDAP_SUCCESS;
1150 #endif /* defined(LDAP_SLAPI) */
1153 #if defined(LDAP_SLAPI)
1155 * If oldStyle is TRUE, then a value suitable for setting to
1156 * the deprecated SLAPI_CONN_AUTHTYPE value is returned
1157 * (pointer to static storage).
1159 * If oldStyle is FALSE, then a value suitable for setting to
1160 * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
1161 * a pointer to allocated memory and will include the SASL
1162 * mechanism (if any).
1164 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
1169 switch ( authz->sai_method ) {
1170 case LDAP_AUTH_SASL:
1172 authType = SLAPD_AUTH_SASL;
1174 len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
1175 authType = slapi_ch_malloc( len );
1176 snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
1179 case LDAP_AUTH_SIMPLE:
1180 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
1182 case LDAP_AUTH_NONE:
1183 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
1189 if ( is_tls && authType == NULL ) {
1190 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
1198 * Internal API to prime a Slapi_PBlock with a Connection.
1200 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
1202 #if defined(LDAP_SLAPI)
1206 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
1207 if ( rc != LDAP_SUCCESS )
1210 if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
1211 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
1212 if ( rc != LDAP_SUCCESS )
1216 if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
1217 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
1218 if ( rc != LDAP_SUCCESS )
1222 rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
1223 if ( rc != LDAP_SUCCESS )
1226 /* Returns pointer to static string */
1227 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
1228 if ( connAuthType != NULL ) {
1229 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
1230 if ( rc != LDAP_SUCCESS )
1234 /* Returns pointer to allocated string */
1235 connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
1236 if ( connAuthType != NULL ) {
1237 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
1238 if ( rc != LDAP_SUCCESS )
1242 if ( conn->c_authz.sai_dn.bv_val != NULL ) {
1243 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
1244 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
1245 if ( rc != LDAP_SUCCESS )
1250 #endif /* defined(LDAP_SLAPI) */
1254 * Internal API to prime a Slapi_PBlock with an Operation.
1256 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
1258 #if defined(LDAP_SLAPI)
1265 if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
1269 isRoot = be_isroot( be, &op->o_ndn );
1270 isUpdateDn = be_isupdate( be, &op->o_ndn );
1273 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
1274 if ( rc != LDAP_SUCCESS )
1277 rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
1278 if ( rc != LDAP_SUCCESS )
1281 rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
1282 if ( rc != LDAP_SUCCESS )
1285 rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
1286 if ( rc != LDAP_SUCCESS )
1289 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
1290 if ( rc != LDAP_SUCCESS )
1293 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
1294 if ( rc != LDAP_SUCCESS )
1297 rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
1298 if ( rc != LDAP_SUCCESS)
1301 rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
1302 if ( rc != LDAP_SUCCESS )
1305 rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
1306 if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
1307 /* Not quite sure what the point of this is. */
1308 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
1309 if ( rc != LDAP_SUCCESS )
1313 return LDAP_SUCCESS;
1319 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
1321 #if defined( LDAP_SLAPI )
1324 slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1325 *isSSL = conn->c_is_tls;
1327 return LDAP_SUCCESS;
1330 #endif /* defined(LDAP_SLAPI) */