]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/slapi_utils.c
1803dabe40aa0b47847775ae5c28823dc525c5cb
[openldap] / servers / slapd / slapi / slapi_utils.c
1 /*
2  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*
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.
11  */
12 /*
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.
18  */
19
20 #include "portable.h"
21 #include "slapi_common.h"
22
23 #include <ac/string.h>
24
25 #include <slap.h>
26 #include <slapi.h>
27 #include <stdarg.h>
28 #include <ctype.h>
29 #include <unistd.h>
30 #include <ldap_pvt.h>
31
32 struct berval *ns_get_supported_extop( int );
33
34 #ifdef _SPARC  
35 #include <sys/systeminfo.h>
36 #endif
37
38 #include <netdb.h>
39
40 /*
41  * server start time (should we use a struct timeval also in slapd?
42  */
43 static struct                   timeval base_time;
44 ldap_pvt_thread_mutex_t         slapi_hn_mutex;
45 ldap_pvt_thread_mutex_t         slapi_time_mutex;
46
47 /*
48  * This function converts an array of pointers to berval objects to
49  * an array of berval objects.
50  */
51
52 int
53 bvptr2obj(
54         struct berval   **bvptr, 
55         BerVarray       *bvobj )
56 {
57         int             rc = LDAP_SUCCESS;
58         int             i;
59         BerVarray       tmpberval;
60
61         if ( bvptr == NULL || *bvptr == NULL ) {
62                 return LDAP_OTHER;
63         }
64
65         for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
66                 ; /* EMPTY */
67         }
68
69         tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
70         if ( tmpberval == NULL ) {
71                 return LDAP_NO_MEMORY;
72         } 
73
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;
77         }
78
79         if ( rc == LDAP_SUCCESS ) {
80                 *bvobj = tmpberval;
81         }
82
83         return rc;
84 }
85
86 Slapi_Entry *
87 slapi_str2entry(
88         char            *s, 
89         int             check_dup )
90 {
91 #if defined(LDAP_SLAPI)
92         Slapi_Entry     *e = NULL;
93         char            *pTmpS;
94
95         pTmpS = slapi_ch_strdup( s );
96         if ( pTmpS != NULL ) {
97                 e = str2entry( pTmpS ); 
98                 slapi_ch_free( (void **)&pTmpS );
99         }
100
101         return e;
102 #else /* !defined(LDAP_SLAPI) */
103         return NULL;
104 #endif /* !defined(LDAP_SLAPI) */
105 }
106
107 char *
108 slapi_entry2str(
109         Slapi_Entry     *e, 
110         int             *len ) 
111 {
112 #if defined(LDAP_SLAPI)
113         char            *ret;
114
115         ldap_pvt_thread_mutex_lock( &entry2str_mutex );
116         ret = entry2str( e, len );
117         ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
118
119         return ret;
120 #else /* !defined(LDAP_SLAPI) */
121         return NULL;
122 #endif /* !defined(LDAP_SLAPI) */
123 }
124
125 char *
126 slapi_entry_get_dn( Slapi_Entry *e ) 
127 {
128 #if defined(LDAP_SLAPI)
129         return e->e_name.bv_val;
130 #else /* !defined(LDAP_SLAPI) */
131         return NULL;
132 #endif /* !defined(LDAP_SLAPI) */
133 }
134
135 int
136 slapi_x_entry_get_id( Slapi_Entry *e )
137 {
138 #if defined(LDAP_SLAPI)
139         return e->e_id;
140 #else
141         return NOID;
142 #endif /* !defined(LDAP_SLAPI) */
143 }
144
145 void 
146 slapi_entry_set_dn(
147         Slapi_Entry     *e, 
148         char            *ldn )
149 {
150 #if defined(LDAP_SLAPI)
151         struct berval   dn = { 0, NULL };
152
153         dn.bv_val = ldn;
154         dn.bv_len = strlen( ldn );
155
156         dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
157 #endif /* defined(LDAP_SLAPI) */
158 }
159
160 Slapi_Entry *
161 slapi_entry_dup( Slapi_Entry *e ) 
162 {
163 #if defined(LDAP_SLAPI)
164         char            *tmp = NULL;
165         Slapi_Entry     *tmpEnt;
166         int             len = 0;
167         
168         tmp = slapi_entry2str( e, &len );
169         if ( tmp == NULL ) {
170                 return (Slapi_Entry *)NULL;
171         }
172
173         tmpEnt = (Slapi_Entry *)str2entry( tmp );
174         if ( tmpEnt == NULL ) { 
175                 slapi_ch_free( (void **)&tmp );
176                 return (Slapi_Entry *)NULL;
177         }
178         
179         if (tmp != NULL) {
180                 slapi_ch_free( (void **)&tmp );
181         }
182
183         return tmpEnt;
184 #else /* !defined(LDAP_SLAPI) */
185         return NULL;
186 #endif /* !defined(LDAP_SLAPI) */
187 }
188
189 int 
190 slapi_entry_attr_delete(
191         Slapi_Entry     *e,             
192         char            *type ) 
193 {
194 #if defined(LDAP_SLAPI)
195         AttributeDescription    *ad;
196         const char              *text;
197
198         if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
199                 return 1;       /* LDAP_NO_SUCH_ATTRIBUTE */
200         }
201
202         if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
203                 return 0;       /* attribute is deleted */
204         } else {
205                 return -1;      /* something went wrong */
206         }
207 #else /* !defined(LDAP_SLAPI) */
208         return -1;
209 #endif /* !defined(LDAP_SLAPI) */
210 }
211
212 Slapi_Entry *
213 slapi_entry_alloc( void ) 
214 {
215 #if defined(LDAP_SLAPI)
216         return (Slapi_Entry *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
217 #else /* !defined(LDAP_SLAPI) */
218         return NULL;
219 #endif /* !defined(LDAP_SLAPI) */
220 }
221
222 void 
223 slapi_entry_free( Slapi_Entry *e ) 
224 {
225 #if defined(LDAP_SLAPI)
226         entry_free( e );
227 #endif /* defined(LDAP_SLAPI) */
228 }
229
230 int 
231 slapi_entry_attr_merge(
232         Slapi_Entry     *e, 
233         char            *type, 
234         struct berval   **vals ) 
235 {
236 #if defined(LDAP_SLAPI)
237         AttributeDescription    *ad;
238         const char              *text;
239         BerVarray               bv;
240         int                     rc;
241
242         rc = bvptr2obj( vals, &bv );
243         if ( rc != LDAP_SUCCESS ) {
244                 return -1;
245         }
246         
247         rc = slap_str2ad( type, &ad, &text );
248         if ( rc != LDAP_SUCCESS ) {
249                 return -1;
250         }
251         
252         rc = attr_merge( e, ad, bv );
253         ch_free( bv );
254
255         return rc;
256 #else /* !defined(LDAP_SLAPI) */
257         return -1;
258 #endif /* !defined(LDAP_SLAPI) */
259 }
260
261 int
262 slapi_entry_attr_find(
263         Slapi_Entry     *e, 
264         char            *type, 
265         Slapi_Attr      **attr ) 
266 {
267 #if defined(LDAP_SLAPI)
268         AttributeDescription    *ad;
269         const char              *text;
270         int                     rc;
271
272         rc = slap_str2ad( type, &ad, &text );
273         if ( rc != LDAP_SUCCESS ) {
274                 return -1;
275         }
276
277         *attr = attr_find( e->e_attrs, ad );
278         if ( *attr == NULL ) {
279                 return -1;
280         }
281
282         return 0;
283 #else /* !defined(LDAP_SLAPI) */
284         return -1;
285 #endif /* !defined(LDAP_SLAPI) */
286 }
287
288 /* 
289  * FIXME -- The caller must free the allocated memory. 
290  * In Netscape they do not have to.
291  */
292 int 
293 slapi_attr_get_values(
294         Slapi_Attr      *attr, 
295         struct berval   ***vals ) 
296 {
297 #if defined(LDAP_SLAPI)
298         int             i, j;
299         struct berval   **bv;
300
301         if ( attr == NULL ) {
302                 return 1;
303         }
304
305         for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
306                 ; /* EMPTY */
307         }
308
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;
314         }
315         bv[j] = NULL;
316         
317         *vals = (struct berval **)bv;
318
319         return 0;
320 #else /* !defined(LDAP_SLAPI) */
321         return -1;
322 #endif /* !defined(LDAP_SLAPI) */
323 }
324
325 char *
326 slapi_dn_normalize( char *dn ) 
327 {
328 #if defined(LDAP_SLAPI)
329         struct berval   bdn;
330         struct berval   ndn;
331
332         assert( dn != NULL );
333         
334         bdn.bv_val = dn;
335         bdn.bv_len = strlen( dn );
336
337         dnNormalize2( NULL, &bdn, &ndn );
338
339         /*
340          * FIXME: ain't it safe to set dn = ndn.bv_val ?
341          */
342         dn = ch_strdup( ndn.bv_val );
343         ch_free( ndn.bv_val );
344         
345         return dn;
346 #else /* !defined(LDAP_SLAPI) */
347         return NULL;
348 #endif /* !defined(LDAP_SLAPI) */
349 }
350
351 /*
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
356  */
357 char *
358 slapi_dn_normalize_case( char *dn ) 
359 {
360 #if defined(LDAP_SLAPI)
361         slapi_dn_normalize( dn );
362         ldap_pvt_str2lower( dn );
363
364         return dn;
365 #else /* defined(LDAP_SLAPI) */
366         return NULL;
367 #endif /* defined(LDAP_SLAPI) */
368 }
369
370 int 
371 slapi_dn_issuffix(
372         char            *dn, 
373         char            *suffix )
374 {
375 #if defined(LDAP_SLAPI)
376         struct berval   bdn, ndn;
377         struct berval   bsuffix, nsuffix;
378
379         assert( dn != NULL );
380         assert( suffix != NULL );
381
382         bdn.bv_val = dn;
383         bdn.bv_len = strlen( dn );
384
385         bsuffix.bv_val = suffix;
386         bsuffix.bv_len = strlen( suffix );
387
388         dnNormalize2( NULL, &bdn, &ndn );
389         dnNormalize2( NULL, &bsuffix, &nsuffix );
390
391         return dnIsSuffix( &ndn, &nsuffix );
392 #else /* !defined(LDAP_SLAPI) */
393         return 0;
394 #endif /* !defined(LDAP_SLAPI) */
395 }
396
397 char *
398 slapi_dn_ignore_case( char *dn )
399 {       
400 #if defined(LDAP_SLAPI)
401         return slapi_dn_normalize_case( dn );
402 #else /* !defined(LDAP_SLAPI) */
403         return NULL;
404 #endif /* !defined(LDAP_SLAPI) */
405 }
406
407 char *
408 slapi_ch_malloc( unsigned long size ) 
409 {
410 #if defined(LDAP_SLAPI)
411         return ch_malloc( size );       
412 #else /* !defined(LDAP_SLAPI) */
413         return NULL;
414 #endif /* !defined(LDAP_SLAPI) */
415 }
416
417 void 
418 slapi_ch_free( void **ptr ) 
419 {
420 #if defined(LDAP_SLAPI)
421         ch_free( *ptr );
422         *ptr = NULL;
423 #endif /* defined(LDAP_SLAPI) */
424 }
425
426 char *
427 slapi_ch_calloc(
428         unsigned long nelem, 
429         unsigned long size ) 
430 {
431 #if defined(LDAP_SLAPI)
432         return ch_calloc( nelem, size );
433 #else /* !defined(LDAP_SLAPI) */
434         return NULL;
435 #endif /* !defined(LDAP_SLAPI) */
436 }
437
438 char *
439 slapi_ch_realloc(
440         char *block, 
441         unsigned long size ) 
442 {
443 #if defined(LDAP_SLAPI)
444         return ch_realloc( block, size );
445 #else /* !defined(LDAP_SLAPI) */
446         return NULL;
447 #endif /* !defined(LDAP_SLAPI) */
448 }
449
450 char *
451 slapi_ch_strdup( char *s ) 
452 {
453 #if defined(LDAP_SLAPI)
454         return ch_strdup( (const char *)s );
455 #else /* !defined(LDAP_SLAPI) */
456         return NULL;
457 #endif /* !defined(LDAP_SLAPI) */
458 }
459
460 size_t
461 slapi_ch_stlen( char *s ) 
462 {
463 #if defined(LDAP_SLAPI)
464         return strlen( (const char *)s );
465 #else /* !defined(LDAP_SLAPI) */
466         return 0;
467 #endif /* !defined(LDAP_SLAPI) */
468 }
469
470 int 
471 slapi_control_present(
472         LDAPControl     **controls, 
473         char            *oid, 
474         struct berval   **val, 
475         int             *iscritical ) 
476 {
477 #if defined(LDAP_SLAPI)
478         int             i;
479         int             rc = 0;
480
481         if ( val ) {
482                 *val = NULL;
483         }
484         
485         if ( iscritical ) {
486                 *iscritical = 0;
487         }
488         
489         for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
490                 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
491                         continue;
492                 }
493
494                 rc = 1;
495                 if ( controls[i]->ldctl_value.bv_len != 0 ) {
496                         /*
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
501                          *    not made."
502                          */
503 #if 0
504                         struct berval   *pTmpBval;
505
506                         pTmpBval = (struct berval *)slapi_ch_malloc( sizeof(struct berval));
507                         if ( pTmpBval == NULL ) {
508                                 rc = 0;
509                         } else {
510                                 pTmpBval->bv_len = controls[i]->ldctl_value.bv_len;
511                                 pTmpBval->bv_val = controls[i]->ldctl_value.bv_val;
512                                 if ( val ) {
513                                         *val = pTmpBval;
514                                 } else {
515                                         slapi_ch_free( (void **)&pTmpBval );
516                                         rc = 0;
517                                 }
518                         }
519 #endif /* 0 */
520                         if ( val ) {
521                                 *val = &controls[i]->ldctl_value;
522                         }
523                 }
524
525                 if ( iscritical ) {
526                         *iscritical = controls[i]->ldctl_iscritical;
527                 }
528
529                 break;
530         }
531
532         return rc;
533 #else /* !defined(LDAP_SLAPI) */
534         return 0;
535 #endif /* !defined(LDAP_SLAPI) */
536 }
537
538 void 
539 slapi_register_supported_control(
540         char            *controloid, 
541         unsigned long   controlops )
542 {
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) */
548 }
549
550 int 
551 slapi_get_supported_controls(
552         char            ***ctrloidsp, 
553         unsigned long   **ctrlopsp ) 
554 {
555 #if defined(LDAP_SLAPI)
556         int             i, n;
557         int             rc = 1;
558         char            **oids = NULL;
559         unsigned long   *masks = NULL;
560
561         for (n = 0; get_supported_ctrl( n ) != NULL; n++) {
562                 ; /* count them */
563         }
564         
565         if ( n == 0 ) {
566                 /* no controls */
567                 *ctrloidsp = NULL;
568                 *ctrlopsp = NULL;
569                 return LDAP_SUCCESS;
570         }
571
572
573         oids = (char **)slapi_ch_malloc( (n + 1) * sizeof(char *) );
574         if ( oids == NULL ) {
575                 rc = LDAP_NO_MEMORY;
576                 goto error_return;
577         }
578
579         masks = (unsigned long *)slapi_ch_malloc( n * sizeof(int) );
580         if ( masks == NULL ) {
581                 rc = LDAP_NO_MEMORY;
582                 goto error_return;
583         }
584
585         for ( i = 0; i < n; i++ ) {
586                 /*
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
591                  */
592                 oids[ i ] = ch_strdup( get_supported_ctrl( i ) );
593                 if ( oids[ i ] == NULL ) {
594                         rc = LDAP_NO_MEMORY;
595                         goto error_return;
596                 }
597                 masks[ i ] = (unsigned long)get_supported_ctrl_mask( i );
598         }
599
600         *ctrloidsp = oids;
601         *ctrlopsp = masks;
602         return LDAP_SUCCESS;
603
604 error_return:
605         if ( rc != LDAP_SUCCESS ) {
606                 for ( i = 0; oids != NULL && oids[ i ] != NULL; i++ ) {
607                         ch_free( oids[ i ] );
608                 }
609                 ch_free( oids );
610                 ch_free( masks );
611         }
612
613         return rc;
614 #else /* !defined(LDAP_SLAPI) */
615         return 1;
616 #endif /* !defined(LDAP_SLAPI) */
617 }
618
619 void 
620 slapi_register_supported_saslmechanism( char *mechanism )
621 {
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) */
627 }
628
629 char **
630 slapi_get_supported_saslmechanisms( void )
631 {
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" );
637         return NULL;
638 #else /* defined(LDAP_SLAPI) */
639         return NULL;
640 #endif /* defined(LDAP_SLAPI) */
641 }
642
643 char **
644 slapi_get_supported_extended_ops( void )
645 {
646 #if defined(LDAP_SLAPI)
647         int             i, j, k;
648         char            **ppExtOpOID = NULL;
649         int             numExtOps = 0;
650
651         for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
652                 ;
653         }
654         
655         for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
656                 ;
657         }
658
659         numExtOps = i + j;
660         if ( numExtOps == 0 ) {
661                 return NULL;
662         }
663
664         ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
665         for ( k = 0; k < i; k++ ) {
666                 struct berval   *bv;
667
668                 bv = get_supported_extop( k );
669                 assert( bv != NULL );
670
671                 ppExtOpOID[ k ] = bv->bv_val;
672         }
673         
674         for ( ; k < j; k++ ) {
675                 struct berval   *bv;
676
677                 bv = ns_get_supported_extop( k );
678                 assert( bv != NULL );
679
680                 ppExtOpOID[ i + k ] = bv->bv_val;
681         }
682         ppExtOpOID[ i + k ] = NULL;
683
684         return ppExtOpOID;
685 #else /* !defined(LDAP_SLAPI) */
686         return NULL;
687 #endif /* !defined(LDAP_SLAPI) */
688 }
689
690 void 
691 slapi_send_ldap_result(
692         Slapi_PBlock    *pb, 
693         int             err, 
694         char            *matched, 
695         char            *text, 
696         int             nentries, 
697         struct berval   **urls ) 
698 {
699 #if defined(LDAP_SLAPI)
700         Connection      *conn;
701         Operation       *op;
702         struct berval   *s;
703         char            *extOID = NULL;
704         struct berval   *extValue = NULL;
705         int             rc;
706
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 );
713                 return;
714         }
715
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,
720                                 extValue );
721                 return;
722         }
723
724         send_ldap_result( conn, op, err, matched, text, NULL, NULL );
725 #endif /* defined(LDAP_SLAPI) */
726 }
727
728 int 
729 slapi_send_ldap_search_entry(
730         Slapi_PBlock    *pb, 
731         Slapi_Entry     *e, 
732         LDAPControl     **ectrls, 
733         char            **attrs, 
734         int             attrsonly )
735 {
736 #if defined(LDAP_SLAPI)
737         Backend         *be;
738         Connection      *pConn;
739         Operation       *pOp;
740         int             rc;
741
742         int             i;
743         AttributeName   *an = NULL;
744         const char      *text;
745
746         for ( i = 0; attrs[ i ] != NULL; i++ ) {
747                 ; /* empty */
748         }
749
750         if ( i > 0 ) {
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)
757                                 return -1;
758                 }
759         }
760
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 ) ) {
764                 rc = LDAP_OTHER;
765         } else {
766                 rc = send_search_entry( be, pConn, pOp, e, an, attrsonly, NULL );
767         }
768
769         return rc;
770
771 #else /* !defined(LDAP_SLAPI) */
772         return -1;
773 #endif /* !defined(LDAP_SLAPI) */
774 }
775
776
777 Slapi_Filter *
778 slapi_str2filter( char *str ) 
779 {
780 #if defined(LDAP_SLAPI)
781         return str2filter( str );
782 #else /* !defined(LDAP_SLAPI) */
783         return NULL;
784 #endif /* !defined(LDAP_SLAPI) */
785 }
786
787 void 
788 slapi_filter_free(
789         Slapi_Filter    *f, 
790         int             recurse ) 
791 {
792 #if defined(LDAP_SLAPI)
793         filter_free( f );
794 #endif /* defined(LDAP_SLAPI) */
795 }
796
797 int 
798 slapi_filter_get_choice( Slapi_Filter *f )
799 {
800 #if defined(LDAP_SLAPI)
801         int             rc;
802
803         if ( f != NULL ) {
804                 rc = f->f_choice;
805         } else {
806                 rc = 0;
807         }
808
809         return rc;
810 #else /* !defined(LDAP_SLAPI) */
811         return -1;              /* invalid filter type */
812 #endif /* !defined(LDAP_SLAPI) */
813 }
814
815 int 
816 slapi_filter_get_ava(
817         Slapi_Filter    *f, 
818         char            **type, 
819         struct berval   **bval )
820 {
821 #if defined(LDAP_SLAPI)
822         int             ftype;
823         int             rc = LDAP_SUCCESS;
824
825         assert( type != NULL );
826         assert( bval != NULL );
827
828         *type = NULL;
829         *bval = NULL;
830
831         ftype = f->f_choice;
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 ) {
838                         rc = LDAP_NO_MEMORY;
839                         goto done;
840                 }
841
842                 *bval = (struct berval *)slapi_ch_malloc( sizeof(struct berval) );
843                 if ( *bval == NULL ) {
844                         rc = LDAP_NO_MEMORY;
845                         goto done;
846                 }
847
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 ) {
851                         rc = LDAP_NO_MEMORY;
852                         goto done;
853                 }
854         } else { /* filter type not supported */
855                 rc = -1;
856         }
857
858 done:
859         if ( rc != LDAP_SUCCESS ) {
860                 if ( *bval ) {
861                         ch_free( *bval );
862                         *bval = NULL;
863                 }
864
865                 if ( *type ) {
866                         ch_free( *type );
867                         *type = NULL;
868                 }
869         }
870
871         return rc;
872 #else /* !defined(LDAP_SLAPI) */
873         return -1;
874 #endif /* !defined(LDAP_SLAPI) */
875 }
876
877 Slapi_Filter *
878 slapi_filter_list_first( Slapi_Filter *f )
879 {
880 #if defined(LDAP_SLAPI)
881         int             ftype;
882
883         if ( f == NULL ) {
884                 return NULL;
885         }
886
887         ftype = f->f_choice;
888         if ( ftype == LDAP_FILTER_AND
889                         || ftype == LDAP_FILTER_OR
890                         || ftype == LDAP_FILTER_NOT ) {
891                 return (Slapi_Filter *)f->f_and;
892         } else {
893                 return NULL;
894         }
895 #else /* !defined(LDAP_SLAPI) */
896         return NULL;
897 #endif /* !defined(LDAP_SLAPI) */
898 }
899
900 Slapi_Filter *
901 slapi_filter_list_next(
902         Slapi_Filter    *f, 
903         Slapi_Filter    *fprev )
904 {
905 #if defined(LDAP_SLAPI)
906         int             ftype;
907
908         if ( f == NULL ) {
909                 return NULL;
910         }
911
912         ftype = f->f_choice;
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;
918                 }
919         }
920
921         return NULL;
922 #else /* !defined(LDAP_SLAPI) */
923         return NULL;
924 #endif /* !defined(LDAP_SLAPI) */
925 }
926
927 int 
928 slapi_send_ldap_extended_response(
929         Connection      *conn, 
930         Operation       *op,
931         int             errornum, 
932         char            *respName,
933         struct berval   *response )
934 {
935 #if defined(LDAP_SLAPI)
936         send_ldap_extended( conn,op, errornum, NULL, NULL, NULL,
937                         respName,response, NULL );
938         return LDAP_SUCCESS;
939 #else /* !defined(LDAP_SLAPI) */
940         return -1;
941 #endif /* !defined(LDAP_SLAPI) */
942 }
943
944 int 
945 slapi_pw_find(
946         struct berval   **vals, 
947         struct berval   *v ) 
948 {
949 #if defined(LDAP_SLAPI)
950         /*
951          * FIXME: what's the point?
952          */
953         return 1;
954 #else /* !defined(LDAP_SLAPI) */
955         return 1;
956 #endif /* !defined(LDAP_SLAPI) */
957 }
958              
959 char *
960 slapi_get_hostname( void ) 
961 {
962 #if defined(LDAP_SLAPI)
963         char            *hn = NULL;
964
965         /*
966          * FIXME: I'd prefer a different check ...
967          */
968 #if defined _SPARC 
969         hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
970         if ( hn == NULL) {
971                 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
972                                 "can't malloc memory for hostname\n" );
973                 hn = NULL;
974                 
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 );
979                 hn = NULL;
980         }
981 #else /* !_SPARC */
982         static int      been_here = 0;   
983         static char     *static_hn = NULL;
984
985         ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
986         if ( !been_here ) {
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" );
991                         static_hn = NULL;
992                         ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
993
994                         return hn;
995                         
996                 } else { 
997                         if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
998                                 slapi_log_error( SLAPI_LOG_FATAL,
999                                                 "SLAPI_SYSINFO",
1000                                                 "can't get hostname\n" );
1001                                 slapi_ch_free( (void **)&static_hn );
1002                                 static_hn = NULL;
1003                                 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1004
1005                                 return hn;
1006
1007                         } else {
1008                                 been_here = 1;
1009                         }
1010                 }
1011         }
1012         ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1013         
1014         hn = ch_strdup( static_hn );
1015 #endif /* !_SPARC */
1016
1017         return hn;
1018 #else /* !defined(LDAP_SLAPI) */
1019         return NULL;
1020 #endif /* !defined(LDAP_SLAPI) */
1021 }
1022
1023 /*
1024  * FIXME: this should go in an appropriate header ...
1025  */
1026 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
1027
1028 int 
1029 slapi_log_error(
1030         int             severity, 
1031         char            *subsystem, 
1032         char            *fmt, 
1033         ... ) 
1034 {
1035 #if defined(LDAP_SLAPI)
1036         int             rc = LDAP_SUCCESS;
1037         va_list         arglist;
1038
1039         va_start( arglist, fmt );
1040         rc = vLogError( severity, subsystem, fmt, arglist );
1041         va_end( arglist );
1042
1043         return rc;
1044 #else /* !defined(LDAP_SLAPI) */
1045         return -1;
1046 #endif /* !defined(LDAP_SLAPI) */
1047 }
1048
1049
1050 unsigned long
1051 slapi_timer_current_time( void ) 
1052 {
1053 #if defined(LDAP_SLAPI)
1054         static int      first_time = 1;
1055 #if !defined (_WIN32)
1056         struct timeval  now;
1057         unsigned long   ret;
1058
1059         ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1060         if (first_time) {
1061                 first_time = 0;
1062                 gettimeofday( &base_time, NULL );
1063         }
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 );
1068
1069         return ret;
1070
1071         /*
1072          * Ain't it better?
1073         return (slap_get_time() - starttime) * 1000000;
1074          */
1075 #else /* _WIN32 */
1076         LARGE_INTEGER now;
1077
1078         if ( first_time ) {
1079                 first_time = 0;
1080                 performance_counter_present = QueryPerformanceCounter( &base_time );
1081                 QueryPerformanceFrequency( &performance_freq );
1082         }
1083
1084         if ( !performance_counter_present )
1085              return 0;
1086
1087         QueryPerformanceCounter( &now );
1088         return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1089 #endif /* _WIN32 */
1090 #else /* !defined(LDAP_SLAPI) */
1091         return 0;
1092 #endif /* !defined(LDAP_SLAPI) */
1093 }
1094
1095 /*
1096  * FIXME ?
1097  */
1098 unsigned long
1099 slapi_timer_get_time( char *label ) 
1100 {
1101 #if defined(LDAP_SLAPI)
1102         unsigned long start = slapi_timer_current_time();
1103         printf("%10ld %10ld usec %s\n", start, 0, label);
1104         return start;
1105 #else /* !defined(LDAP_SLAPI) */
1106         return 0;
1107 #endif /* !defined(LDAP_SLAPI) */
1108 }
1109
1110 /*
1111  * FIXME ?
1112  */
1113 void
1114 slapi_timer_elapsed_time(
1115         char *label,
1116         unsigned long start ) 
1117 {
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) */
1122 }
1123
1124 void
1125 slapi_free_search_results_internal( Slapi_PBlock *pb ) 
1126 {
1127 #if defined(LDAP_SLAPI)
1128         Slapi_Entry     **entries;
1129         int             k = 0, nEnt = 0;
1130
1131         slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
1132         slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
1133         if ( nEnt == 0 ) {
1134                 return;
1135         }
1136         
1137         if ( entries == NULL ) {
1138                 return;
1139         }
1140         
1141         for ( k = 0; k < nEnt; k++ ) {
1142                 slapi_entry_free( entries[k] );
1143         }
1144         
1145         slapi_ch_free( (void **)&entries );
1146 #endif /* defined(LDAP_SLAPI) */
1147 }
1148
1149 /*
1150  * Internal API to prime a Slapi_PBlock with a Backend.
1151  */
1152 int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
1153 {
1154 #if defined(LDAP_SLAPI)
1155         int rc;
1156
1157         rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
1158         if ( rc != LDAP_SUCCESS )
1159                 return rc;
1160
1161         rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
1162         if ( rc != LDAP_SUCCESS )
1163                 return rc;
1164
1165         return LDAP_SUCCESS;
1166 #else
1167         return -1;
1168 #endif /* defined(LDAP_SLAPI) */
1169 }
1170
1171 #if defined(LDAP_SLAPI)
1172 /*
1173  * If oldStyle is TRUE, then a value suitable for setting to
1174  * the deprecated SLAPI_CONN_AUTHTYPE value is returned 
1175  * (pointer to static storage).
1176  *
1177  * If oldStyle is FALSE, then a value suitable for setting to
1178  * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
1179  * a pointer to allocated memory and will include the SASL
1180  * mechanism (if any).
1181  */
1182 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
1183 {
1184         size_t len;
1185         char *authType;
1186
1187         switch ( authz->sai_method ) {
1188         case LDAP_AUTH_SASL:
1189                 if ( oldStyle ) {
1190                         authType = SLAPD_AUTH_SASL;
1191                 } else {
1192                         len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
1193                         authType = slapi_ch_malloc( len );
1194                         snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
1195                 }
1196                 break;
1197         case LDAP_AUTH_SIMPLE:
1198                 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
1199                 break;
1200         case LDAP_AUTH_NONE:
1201                 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
1202                 break;
1203         default:
1204                 authType = NULL;
1205                 break;
1206         }
1207         if ( is_tls && authType == NULL ) {
1208                 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
1209         }
1210
1211         return authType;
1212 }
1213 #endif
1214
1215 /*
1216  * Internal API to prime a Slapi_PBlock with a Connection.
1217  */
1218 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
1219 {
1220 #if defined(LDAP_SLAPI)
1221         char *connAuthType;
1222         int rc;
1223
1224         rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
1225         if ( rc != LDAP_SUCCESS )
1226                 return rc;
1227
1228         if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
1229                 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
1230                 if ( rc != LDAP_SUCCESS )
1231                         return rc;
1232         }
1233
1234         if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
1235                 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
1236                 if ( rc != LDAP_SUCCESS )
1237                         return rc;
1238         }
1239
1240         rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
1241         if ( rc != LDAP_SUCCESS )
1242                 return rc;
1243
1244         /* Returns pointer to static string */
1245         connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
1246         if ( connAuthType != NULL ) {
1247                 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
1248                 if ( rc != LDAP_SUCCESS )
1249                         return rc;
1250         }
1251
1252         /* Returns pointer to allocated string */
1253         connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
1254         if ( connAuthType != NULL ) {
1255                 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
1256                 if ( rc != LDAP_SUCCESS )
1257                         return rc;
1258         }
1259
1260         if ( conn->c_authz.sai_dn.bv_val != NULL ) {
1261                 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
1262                 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
1263                 if ( rc != LDAP_SUCCESS )
1264                         return rc;
1265         }
1266
1267         return rc;
1268 #else
1269         return -1;
1270 #endif /* defined(LDAP_SLAPI) */
1271 }
1272
1273 /*
1274  * Internal API to prime a Slapi_PBlock with an Operation.
1275  */
1276 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
1277 {
1278 #if defined(LDAP_SLAPI)
1279         int isRoot = 0;
1280         int isUpdateDn = 0;
1281         int rc;
1282         Backend *be;
1283         char *opAuthType;
1284
1285         if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
1286                 be = NULL;
1287         }
1288         if (be != NULL) {
1289                 isRoot = be_isroot( be, &op->o_ndn );
1290                 isUpdateDn = be_isupdate( be, &op->o_ndn );
1291         }
1292                 
1293         rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
1294         if ( rc != LDAP_SUCCESS )
1295                 return rc;
1296
1297         rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
1298         if ( rc != LDAP_SUCCESS )
1299                 return rc;
1300
1301         rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
1302         if ( rc != LDAP_SUCCESS )
1303                 return rc;
1304
1305         rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
1306         if ( rc != LDAP_SUCCESS )
1307                 return rc;
1308
1309         rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
1310         if ( rc != LDAP_SUCCESS )
1311                 return rc;
1312
1313         rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
1314         if ( rc != LDAP_SUCCESS )
1315                 return rc;
1316
1317         rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
1318         if ( rc != LDAP_SUCCESS)
1319                 return rc;
1320
1321         rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
1322         if ( rc != LDAP_SUCCESS )
1323                 return rc;
1324
1325         rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
1326         if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
1327                 /* Not quite sure what the point of this is. */
1328                 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
1329                 if ( rc != LDAP_SUCCESS )
1330                         return rc;
1331         }
1332
1333         return LDAP_SUCCESS;
1334 #else
1335         return -1;
1336 #endif
1337 }
1338
1339 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
1340 {
1341 #if defined( LDAP_SLAPI )
1342         Connection *conn;
1343
1344         slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1345         *isSSL = conn->c_is_tls;
1346
1347         return LDAP_SUCCESS;
1348 #else
1349         return -1;
1350 #endif /* defined(LDAP_SLAPI) */
1351 }
1352
1353 /*
1354  * DS 5.x compatability API follow
1355  */
1356
1357 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
1358 {
1359 #if defined( LDAP_SLAPI )
1360         AttributeType *at;
1361
1362         if ( attr == NULL )
1363                 return LDAP_PARAM_ERROR;
1364
1365         at = attr->a_desc->ad_type;
1366
1367         *flags = SLAPI_ATTR_FLAG_STD_ATTR;
1368
1369         if ( is_at_single_value( at ) )
1370                 *flags |= SLAPI_ATTR_FLAG_SINGLE;
1371         if ( is_at_operational( at ) )
1372                 *flags |= SLAPI_ATTR_FLAG_OPATTR;
1373         if ( is_at_obsolete( at ) )
1374                 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
1375         if ( is_at_collective( at ) )
1376                 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
1377         if ( is_at_no_user_mod( at ) )
1378                 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
1379
1380         return LDAP_SUCCESS;
1381 #else
1382         return -1;
1383 #endif /* defined(LDAP_SLAPI) */
1384 }
1385
1386 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
1387 {
1388 #if defined( LDAP_SLAPI )
1389         unsigned long flags;
1390
1391         if ( slapi_attr_get_flags( attr, &flags ) != 0 )
1392                 return 0;
1393         return (flags & flag) ? 1 : 0;
1394 #else
1395         return 0;
1396 #endif /* defined(LDAP_SLAPI) */
1397 }
1398
1399 Slapi_Attr *slapi_attr_new( void )
1400 {
1401 #ifdef LDAP_SLAPI
1402         Attribute *ad;
1403
1404         ad = (Attribute  *)slapi_ch_calloc( 1, sizeof(*ad) );
1405
1406         return ad;
1407 #else
1408         return NULL;
1409 #endif
1410 }
1411
1412 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
1413 {
1414 #ifdef LDAP_SLAPI
1415         const char *text;
1416         AttributeDescription *ad;
1417
1418         if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
1419                 return NULL;
1420         }
1421
1422         a->a_desc = ad;
1423         a->a_vals = NULL;
1424         a->a_next = NULL;
1425         a->a_flags = 0;
1426
1427         return a;
1428 #else
1429         return NULL;
1430 #endif
1431 }
1432
1433 void slapi_attr_free( Slapi_Attr **a )
1434 {
1435 #ifdef LDAP_SLAPI
1436         attr_free( *a );
1437         *a = NULL;
1438 #endif
1439 }
1440
1441 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
1442 {
1443 #ifdef LDAP_SLAPI
1444         return attr_dup( (Slapi_Attr *)attr );
1445 #else
1446         return NULL;
1447 #endif
1448 }
1449
1450 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
1451 {
1452 #ifdef LDAP_SLAPI
1453         return value_add_one( &a->a_vals, (Slapi_Value *)v );
1454 #else
1455         return -1;
1456 #endif
1457 }
1458
1459 int slapi_attr_type2plugin( const char *type, void **pi )
1460 {
1461         *pi = NULL;
1462
1463         return LDAP_OTHER;
1464 }
1465
1466 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
1467 {
1468 #ifdef LDAP_SLAPI
1469         if ( attr == NULL ) {
1470                 return LDAP_PARAM_ERROR;
1471         }
1472
1473         *type = attr->a_desc->ad_cname.bv_val;
1474
1475         return LDAP_SUCCESS;
1476 #else
1477         return -1;
1478 #endif
1479 }
1480
1481 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
1482 {
1483 #ifdef LDAP_SLAPI
1484         if ( attr == NULL ) {
1485                 return LDAP_PARAM_ERROR;
1486         }
1487         *oidp = attr->a_desc->ad_type->sat_oid;
1488
1489         return LDAP_SUCCESS;
1490 #else
1491         return -1;
1492 #endif
1493 }
1494
1495 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
1496 {
1497 #ifdef LDAP_SLAPI
1498         MatchingRule *mr;
1499         int ret;
1500         int rc;
1501         const char *text;
1502
1503         mr = a->a_desc->ad_type->sat_equality;
1504         rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
1505                 (struct berval *)v1, (void *)v2, &text );
1506         if ( rc != LDAP_SUCCESS ) 
1507                 return -1;
1508
1509         return ( ret == 0 ) ? 0 : -1;
1510 #else
1511         return -1;
1512 #endif
1513 }
1514
1515 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
1516 {
1517 #ifdef LDAP_SLAPI
1518         MatchingRule *mr;
1519         struct berval *bv;
1520         int j;
1521         const char *text;
1522         int rc;
1523         int ret;
1524
1525         mr = a->a_desc->ad_type->sat_equality;
1526         for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
1527                 rc = value_match( &ret, a->a_desc, mr,
1528                         SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
1529                 if ( rc != LDAP_SUCCESS ) {
1530                         return -1;
1531                 }
1532                 if ( ret == 0 ) {
1533                         return 0;
1534                 }
1535         }
1536 #endif
1537         return -1;
1538 }
1539
1540 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
1541 {
1542 #ifdef LDAP_SLAPI
1543         AttributeDescription *a1;
1544         AttributeDescription *a2;
1545         const char *text;
1546         int ret;
1547
1548         if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
1549                 return -1;
1550         }
1551
1552         if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
1553                 return 1;
1554         }
1555
1556 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
1557         ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
1558                 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
1559
1560         switch ( opt ) {
1561         case SLAPI_TYPE_CMP_EXACT:
1562                 ret = ad_cmp( a1, a2 );
1563                 break;
1564         case SLAPI_TYPE_CMP_BASE:
1565                 ret = ad_base_cmp( a1, a2 );
1566                 break;
1567         case SLAPI_TYPE_CMP_SUBTYPE:
1568                 ret = is_ad_subtype( a2, a2 );
1569                 break;
1570         default:
1571                 ret = -1;
1572                 break;
1573         }
1574
1575         return ret;
1576 #else
1577         return -1;
1578 #endif
1579 }
1580
1581 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
1582 {
1583 #ifdef LDAP_SLAPI
1584         return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
1585 #else
1586         return -1;
1587 #endif
1588 }
1589
1590 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
1591 {
1592 #ifdef LDAP_SLAPI
1593         return slapi_valueset_first_value( &a->a_vals, v );
1594 #else
1595         return -1;
1596 #endif
1597 }
1598
1599 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
1600 {
1601 #ifdef LDAP_SLAPI
1602         return slapi_valueset_next_value( &a->a_vals, hint, v );
1603 #else
1604         return -1;
1605 #endif
1606 }
1607
1608 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
1609 {
1610 #ifdef LDAP_SLAPI
1611         *numValues = slapi_valueset_count( &a->a_vals );
1612
1613         return 0;
1614 #else
1615         return -1;
1616 #endif
1617 }
1618
1619 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
1620 {
1621 #ifdef LDAP_SLAPI
1622         *vs = &((Slapi_Attr *)a)->a_vals;
1623
1624         return 0;
1625 #else
1626         return -1;
1627 #endif
1628 }
1629
1630 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
1631 {
1632 #ifdef LDAP_SLAPI
1633         return slapi_attr_get_values( a, vals );
1634 #else
1635         return -1;
1636 #endif
1637 }
1638
1639 char *slapi_attr_syntax_normalize( const char *s )
1640 {
1641 #ifdef LDAP_SLAPI
1642         AttributeDescription *ad;
1643         const char *text;
1644
1645         if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
1646                 return NULL;
1647         }
1648
1649         return ad->ad_cname.bv_val;
1650 #else
1651         return -1;
1652 #endif
1653 }
1654
1655 Slapi_Value *slapi_value_new( void )
1656 {
1657 #ifdef LDAP_SLAPI
1658         struct berval *bv;
1659
1660         bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
1661
1662         return bv;
1663 #else
1664         return NULL;
1665 #endif
1666 }
1667
1668 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
1669 {
1670 #ifdef LDAP_SLAPI
1671         return ber_dupbv( NULL, (struct berval *)bval );
1672 #else
1673         return NULL;
1674 #endif
1675 }
1676
1677 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
1678 {
1679 #ifdef LDAP_SLAPI
1680         return slapi_value_new_berval( v );
1681 #else
1682         return NULL;
1683 #endif
1684 }
1685
1686 Slapi_Value *slapi_value_new_string(const char *s)
1687 {
1688 #ifdef LDAP_SLAPI
1689         struct berval bv;
1690
1691         bv.bv_val = (char *)s;
1692         bv.bv_len = strlen( s );
1693
1694         return slapi_value_new_berval( &bv );
1695 #else
1696         return NULL;
1697 #endif
1698 }
1699
1700 Slapi_Value *slapi_value_init(Slapi_Value *val)
1701 {
1702 #ifdef LDAP_SLAPI
1703         val->bv_val = NULL;
1704         val->bv_len = 0;
1705
1706         return val;
1707 #else
1708         return NULL;
1709 #endif
1710 }
1711
1712 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
1713 {
1714 #ifdef LDAP_SLAPI
1715         return ber_dupbv( v, bval );
1716 #else
1717         return NULL;
1718 #endif
1719 }
1720
1721 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
1722 {
1723 #ifdef LDAP_SLAPI
1724         v->bv_val = slapi_ch_strdup( (char *)s );
1725         v->bv_len = strlen( s );
1726
1727         return v;
1728 #else
1729         return NULL;
1730 #endif
1731 }
1732
1733 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
1734 {
1735 #ifdef LDAP_SLAPI
1736         return slapi_value_new_value( v );
1737 #else
1738         return NULL;
1739 #endif
1740 }
1741
1742 void slapi_value_free(Slapi_Value **value)
1743 {
1744 #ifdef LDAP_SLAPI       
1745         if ( value == NULL ) {
1746                 return;
1747         }
1748         if ( *value != NULL ) {
1749                 Slapi_Value *v;
1750
1751                 slapi_ch_free( (void **)&v->bv_val );
1752                 slapi_ch_free( (void **)&v );
1753         }
1754 #endif
1755 }
1756
1757 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
1758 {
1759 #ifdef LDAP_SLAPI
1760         return value;
1761 #else
1762         return NULL;
1763 #endif
1764 }
1765
1766 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
1767 {
1768 #ifdef LDAP_SLAPI
1769         if ( value == NULL ) {
1770                 return NULL;
1771         }
1772         if ( value->bv_val != NULL ) {
1773                 slapi_ch_free( (void **)&value->bv_val );
1774         }
1775         slapi_value_init_berval( value, (struct berval *)bval );
1776
1777         return value;
1778 #else
1779         return NULL;
1780 #endif
1781 }
1782
1783 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
1784 {
1785 #ifdef LDAP_SLAPI
1786         if ( value == NULL ) {
1787                 return NULL;
1788         }
1789         return slapi_value_set_berval( value, vfrom );
1790 #else
1791         return NULL;
1792 #endif
1793 }
1794
1795 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
1796 {
1797 #ifdef LDAP_SLAPI
1798         if ( value == NULL ) {
1799                 return NULL;
1800         }
1801         if ( value->bv_val != NULL ) {
1802                 slapi_ch_free( (void **)&value->bv_val );
1803         }
1804         value->bv_val = slapi_ch_malloc( len );
1805         value->bv_len = len;
1806         AC_MEMCPY( value->bv_val, val, len );
1807
1808         return value;
1809 #else
1810         return NULL;
1811 #endif
1812 }
1813
1814 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
1815 {
1816 #ifdef LDAP_SLAPI
1817         if ( value == NULL ) {
1818                 return -1;
1819         }
1820         slapi_value_set( value, (void *)strVal, strlen( strVal ) );
1821         return 0;
1822 #else
1823         return NULL;
1824 #endif
1825 }
1826
1827 int slapi_value_set_int(Slapi_Value *value, int intVal)
1828 {
1829 #ifdef LDAP_SLAPI
1830         char buf[64];
1831
1832         snprintf( buf, sizeof( buf ), "%d", intVal );
1833
1834         return slapi_value_set_string( value, buf );
1835 #else
1836         return -1;
1837 #endif
1838 }
1839
1840 const char *slapi_value_get_string(const Slapi_Value *value)
1841 {
1842 #ifdef LDAP_SLAPI
1843         if ( value == NULL ) {
1844                 return NULL;
1845         }
1846         return value->bv_val;
1847 #else
1848         return NULL;
1849 #endif
1850 }
1851
1852 #ifdef LDAP_SLAPI
1853 static int checkBVString(const struct berval *bv)
1854 {
1855         int i;
1856
1857         for ( i = 0; i < bv->bv_len; i++ ) {
1858                 if ( bv->bv_val[i] == '\0' )
1859                         return 0;
1860         }
1861         if ( bv->bv_val[i] != '\0' )
1862                 return 0;
1863
1864         return 1;
1865 }
1866 #endif
1867
1868 int slapi_value_get_int(const Slapi_Value *value)
1869 {
1870 #ifdef LDAP_SLAPI
1871         if ( value == NULL ) return 0;
1872         if ( !checkBVString( value ) ) return 0;
1873
1874         return (int)strtol( value->bv_val, NULL, 10 );
1875 #else
1876         return NULL;
1877 #endif
1878 }
1879
1880 unsigned int slapi_value_get_uint(const Slapi_Value *value)
1881 {
1882 #ifdef LDAP_SLAPI
1883         if ( value == NULL ) return 0;
1884         if ( !checkBVString( value ) ) return 0;
1885
1886         return (unsigned int)strtoul( value->bv_val, NULL, 10 );
1887 #else
1888         return NULL;
1889 #endif
1890 }
1891
1892 long slapi_value_get_long(const Slapi_Value *value)
1893 {
1894 #ifdef LDAP_SLAPI
1895         if ( value == NULL ) return 0;
1896         if ( !checkBVString( value ) ) return 0;
1897
1898         return strtol( value->bv_val, NULL, 10 );
1899 #else
1900         return NULL;
1901 #endif
1902 }
1903
1904 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
1905 {
1906 #ifdef LDAP_SLAPI
1907         if ( value == NULL ) return 0;
1908         if ( !checkBVString( value ) ) return 0;
1909
1910         return strtoul( value->bv_val, NULL, 10 );
1911 #else
1912         return NULL;
1913 #endif
1914 }
1915
1916 size_t slapi_value_get_length(const Slapi_Value *value)
1917 {
1918 #ifdef LDAP_SLAPI
1919         if ( value == NULL )
1920                 return 0;
1921
1922         return (size_t) value->bv_len;
1923 #else
1924         return 0;
1925 #endif
1926 }
1927
1928 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
1929 {
1930 #ifdef LDAP_SLAPI
1931         return slapi_attr_value_cmp( a, v1, v2 );
1932 #else
1933         return -1;
1934 #endif
1935 }
1936
1937 /* A ValueSet is a container for a BerVarray. */
1938 Slapi_ValueSet *slapi_valueset_new( void )
1939 {
1940 #ifdef LDAP_SLAPI
1941         Slapi_ValueSet *vs;
1942
1943         vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
1944         *vs = NULL;
1945
1946         return vs;
1947 #else
1948         return NULL;
1949 #endif
1950 }
1951
1952 void slapi_valueset_free(Slapi_ValueSet *vs)
1953 {
1954 #ifdef LDAP_SLAPI
1955         if ( vs != NULL ) {
1956                 BerVarray vp = *vs;
1957
1958                 ber_bvarray_free( vp );
1959                 slapi_ch_free( (void **)&vp );
1960
1961                 *vs = NULL;
1962         }
1963 #endif
1964 }
1965
1966 void slapi_valueset_init(Slapi_ValueSet *vs)
1967 {
1968 #ifdef LDAP_SLAPI
1969         if ( vs != NULL && *vs == NULL ) {
1970                 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
1971                 (*vs)->bv_val = NULL;
1972                 (*vs)->bv_len = 0;
1973         }
1974 #endif
1975 }
1976
1977 void slapi_valueset_done(Slapi_ValueSet *vs)
1978 {
1979 #ifdef LDAP_SLAPI
1980         BerVarray vp;
1981
1982         if ( vs == NULL )
1983                 return;
1984
1985         for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
1986                 vp->bv_len = 0;
1987                 slapi_ch_free( (void **)&vp->bv_val );
1988         }
1989         /* but don't free *vs or vs */
1990 #endif
1991 }
1992
1993 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
1994 {
1995 #ifdef LDAP_SLAPI
1996         ber_bvarray_add( vs, (Slapi_Value *)addval );
1997 #endif
1998 }
1999
2000 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2001 {
2002 #ifdef LDAP_SLAPI
2003         return slapi_valueset_next_value( vs, 0, v );
2004 #else
2005         return -1;
2006 #endif
2007 }
2008
2009 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2010 {
2011 #ifdef LDAP_SLAPI
2012         int i;
2013         BerVarray vp;
2014
2015         if ( vs == NULL )
2016                 return -1;
2017
2018         vp = *vs;
2019
2020         for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2021                 if ( i == index ) {
2022                         *v = &vp[i];
2023                         return index + 1;
2024                 }
2025         }
2026 #endif
2027
2028         return -1;
2029 }
2030
2031 int slapi_valueset_count( const Slapi_ValueSet *vs )
2032 {
2033 #ifdef LDAP_SLAPI
2034         int i;
2035         BerVarray vp;
2036
2037         if ( vs == NULL )
2038                 return 0;
2039
2040         vp = *vs;
2041
2042         for ( i = 0; vp[i].bv_val != NULL; i++ )
2043                 ;
2044
2045         return i;
2046 #else
2047         return 0;
2048 #endif
2049
2050 }
2051
2052 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2053 {
2054 #ifdef LDAP_SLAPI
2055         BerVarray vp;
2056
2057         for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2058                 slapi_valueset_add_value( vs1, vp );
2059         }
2060 #endif
2061 }
2062
2063 /*
2064  * Attribute sets are an OpenLDAP extension for the 
2065  * virtual operational attribute coalescing plugin 
2066  */
2067 Slapi_AttrSet *slapi_x_attrset_new( void )
2068 {
2069 #ifdef LDAP_SLAPI
2070         Slapi_AttrSet *a;
2071
2072         /*
2073          * Like a Slapi_ValueSet, a Slapi_AttrSet is a container
2074          * for objects: we need this because it may be initially
2075          * empty.
2076          */
2077         a = (Slapi_AttrSet *)slapi_ch_malloc( sizeof( *a ) );
2078         *a = NULL;
2079
2080         return a;
2081 #else
2082         return NULL;
2083 #endif
2084 }
2085
2086 Slapi_AttrSet *slapi_x_attrset_init( Slapi_AttrSet *as, Slapi_Attr *a )
2087 {
2088 #ifdef LDAP_SLAPI
2089         *as = a;
2090         a->a_next = NULL;
2091
2092         return as;
2093 #else
2094         return NULL;
2095 #endif
2096 }
2097
2098 void slapi_x_attrset_free( Slapi_AttrSet **pAs )
2099 {
2100 #ifdef LDAP_SLAPI
2101         Slapi_AttrSet *as = *pAs;
2102
2103         if ( as != NULL ) {
2104                 attrs_free( *as );
2105                 slapi_ch_free( (void **)pAs );
2106         }
2107 #endif
2108 }
2109
2110 Slapi_AttrSet *slapi_x_attrset_dup( Slapi_AttrSet *as )
2111 {
2112 #ifdef LDAP_SLAPI
2113         Slapi_AttrSet *newAs = slapi_x_attrset_new();
2114         
2115         if ( *as != NULL )
2116                 *newAs = attrs_dup( *as );
2117
2118         return newAs;
2119 #else
2120         return NULL;
2121 #endif
2122 }
2123
2124 int slapi_x_attrset_add_attr( Slapi_AttrSet *as, Slapi_Attr *a )
2125 {
2126 #ifdef LDAP_SLAPI
2127         Slapi_Attr *nextAttr;
2128
2129         if ( as == NULL || a == NULL )
2130                 return -1;
2131
2132         if ( *as == NULL ) {
2133                 /* First attribute */
2134                 nextAttr = NULL;
2135                 (*as) = a;
2136         } else {
2137                 /* Non-first attribute */
2138                 nextAttr = (*as)->a_next;
2139                 (*as)->a_next = a;
2140         }
2141
2142         a->a_next = nextAttr;
2143
2144         return 0;
2145 #else
2146         return -1;
2147 #endif
2148 }
2149
2150 int slapi_x_attrset_add_attr_copy( Slapi_AttrSet *as, Slapi_Attr *a )
2151 {
2152 #ifdef LDAP_SLAPI
2153         Slapi_Attr *adup;
2154
2155         adup = slapi_attr_dup( a );
2156         return slapi_x_attrset_add_attr( as, adup );
2157 #else
2158         return -1;
2159 #endif
2160 }
2161
2162 int slapi_x_attrset_find( Slapi_AttrSet *as, const char *type, Slapi_Attr **attr )
2163 {
2164 #ifdef LDAP_SLAPI
2165         AttributeDescription *ad;
2166         const char *text;
2167
2168         if ( as == NULL || *as == NULL ) {
2169                 return -1;
2170         }
2171
2172         if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2173                 return -1;
2174         }
2175         *attr = attrs_find( *as, ad );
2176         return ( *attr == NULL ) ? -1 : 0;
2177 #else
2178         return -1;
2179 #endif
2180 }
2181
2182 int slapi_x_attrset_merge( Slapi_AttrSet *as, const char *type, Slapi_ValueSet *vals )
2183 {
2184 #ifdef LDAP_SLAPI
2185         AttributeDescription *ad;
2186         Slapi_AttrSet *a;
2187         const char *text;
2188
2189         if ( vals == NULL || *vals == NULL ) {
2190                 /* Must have something to add. */
2191                 return -1;
2192         }
2193
2194         if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2195                 return -1;
2196         }
2197
2198         for ( a = as; *a != NULL; a = &(*a)->a_next ) {
2199                 if ( ad_cmp( (*a)->a_desc, ad ) == 0 ) {
2200                         break;
2201                 }
2202         }
2203
2204         if ( *a == NULL ) {
2205                 *a = (Slapi_Attr *) slapi_ch_malloc( sizeof(Attribute) );
2206                 (*a)->a_desc = ad;
2207                 (*a)->a_vals = NULL;
2208                 (*a)->a_next = NULL;
2209                 (*a)->a_flags = 0;
2210         }
2211
2212         return value_add ( &(*a)->a_vals, *vals );
2213 #else
2214         return -1;
2215 #endif
2216 }
2217
2218 int slapi_x_attrset_merge_bervals( Slapi_AttrSet *as, const char *type, struct berval **vals )
2219 {
2220 #ifdef LDAP_SLAPI
2221         BerVarray vp;
2222         int rc;
2223
2224         if ( bvptr2obj( vals, &vp ) != LDAP_SUCCESS ) {
2225                 return -1;
2226         }
2227         rc = slapi_x_attrset_merge( as, type, &vp );
2228         slapi_ch_free( (void **)&vp );
2229
2230         return rc;
2231 #else
2232         return -1;
2233 #endif
2234 }
2235
2236 int slapi_x_attrset_delete( Slapi_AttrSet *as, const char *type )
2237 {
2238 #ifdef LDAP_SLAPI
2239         AttributeDescription *ad;
2240         const char *text;
2241
2242         if ( as == NULL ) {
2243                 return -1;
2244         }
2245
2246         if ( *as == NULL ) {
2247                 return -1;
2248         }
2249
2250         if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2251                 return -1;
2252         }
2253
2254         if ( attr_delete( as, ad ) != LDAP_SUCCESS ) {
2255                 return -1;
2256         }
2257
2258         return 0;
2259 #else
2260         return -1;
2261 #endif
2262 }