]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/slapi_utils.c
4887da032b3255fe0b4d69e4eae0813745587db6
[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 = NULL;
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 = NULL;
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 = NULL;
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         if ( be != NULL ) {
1162                 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
1163                 if ( rc != LDAP_SUCCESS )
1164                         return rc;
1165         }
1166
1167         return LDAP_SUCCESS;
1168 #else
1169         return -1;
1170 #endif /* defined(LDAP_SLAPI) */
1171 }
1172
1173 #if defined(LDAP_SLAPI)
1174 /*
1175  * If oldStyle is TRUE, then a value suitable for setting to
1176  * the deprecated SLAPI_CONN_AUTHTYPE value is returned 
1177  * (pointer to static storage).
1178  *
1179  * If oldStyle is FALSE, then a value suitable for setting to
1180  * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
1181  * a pointer to allocated memory and will include the SASL
1182  * mechanism (if any).
1183  */
1184 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
1185 {
1186         size_t len;
1187         char *authType;
1188
1189         switch ( authz->sai_method ) {
1190         case LDAP_AUTH_SASL:
1191                 if ( oldStyle ) {
1192                         authType = SLAPD_AUTH_SASL;
1193                 } else {
1194                         len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
1195                         authType = slapi_ch_malloc( len );
1196                         snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
1197                 }
1198                 break;
1199         case LDAP_AUTH_SIMPLE:
1200                 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
1201                 break;
1202         case LDAP_AUTH_NONE:
1203                 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
1204                 break;
1205         default:
1206                 authType = NULL;
1207                 break;
1208         }
1209         if ( is_tls && authType == NULL ) {
1210                 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
1211         }
1212
1213         return authType;
1214 }
1215 #endif
1216
1217 /*
1218  * Internal API to prime a Slapi_PBlock with a Connection.
1219  */
1220 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
1221 {
1222 #if defined(LDAP_SLAPI)
1223         char *connAuthType;
1224         int rc;
1225
1226         rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
1227         if ( rc != LDAP_SUCCESS )
1228                 return rc;
1229
1230         if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
1231                 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
1232                 if ( rc != LDAP_SUCCESS )
1233                         return rc;
1234         }
1235
1236         if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
1237                 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
1238                 if ( rc != LDAP_SUCCESS )
1239                         return rc;
1240         }
1241
1242         rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
1243         if ( rc != LDAP_SUCCESS )
1244                 return rc;
1245
1246         /* Returns pointer to static string */
1247         connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
1248         if ( connAuthType != NULL ) {
1249                 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
1250                 if ( rc != LDAP_SUCCESS )
1251                         return rc;
1252         }
1253
1254         /* Returns pointer to allocated string */
1255         connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
1256         if ( connAuthType != NULL ) {
1257                 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
1258                 if ( rc != LDAP_SUCCESS )
1259                         return rc;
1260         }
1261
1262         if ( conn->c_authz.sai_dn.bv_val != NULL ) {
1263                 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
1264                 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
1265                 if ( rc != LDAP_SUCCESS )
1266                         return rc;
1267         }
1268
1269         return rc;
1270 #else
1271         return -1;
1272 #endif /* defined(LDAP_SLAPI) */
1273 }
1274
1275 /*
1276  * Internal API to prime a Slapi_PBlock with an Operation.
1277  */
1278 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
1279 {
1280 #if defined(LDAP_SLAPI)
1281         int isRoot = 0;
1282         int isUpdateDn = 0;
1283         int rc;
1284         Backend *be;
1285         char *opAuthType;
1286
1287         if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
1288                 be = NULL;
1289         }
1290         if (be != NULL) {
1291                 isRoot = be_isroot( be, &op->o_ndn );
1292                 isUpdateDn = be_isupdate( be, &op->o_ndn );
1293         }
1294                 
1295         rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
1296         if ( rc != LDAP_SUCCESS )
1297                 return rc;
1298
1299         rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
1300         if ( rc != LDAP_SUCCESS )
1301                 return rc;
1302
1303         rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
1304         if ( rc != LDAP_SUCCESS )
1305                 return rc;
1306
1307         rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
1308         if ( rc != LDAP_SUCCESS )
1309                 return rc;
1310
1311         rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
1312         if ( rc != LDAP_SUCCESS )
1313                 return rc;
1314
1315         rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
1316         if ( rc != LDAP_SUCCESS )
1317                 return rc;
1318
1319         rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
1320         if ( rc != LDAP_SUCCESS)
1321                 return rc;
1322
1323         rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
1324         if ( rc != LDAP_SUCCESS )
1325                 return rc;
1326
1327         rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
1328         if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
1329                 /* Not quite sure what the point of this is. */
1330                 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
1331                 if ( rc != LDAP_SUCCESS )
1332                         return rc;
1333         }
1334
1335         return LDAP_SUCCESS;
1336 #else
1337         return -1;
1338 #endif
1339 }
1340
1341 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
1342 {
1343 #if defined( LDAP_SLAPI )
1344         Connection *conn;
1345
1346         slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1347         *isSSL = conn->c_is_tls;
1348
1349         return LDAP_SUCCESS;
1350 #else
1351         return -1;
1352 #endif /* defined(LDAP_SLAPI) */
1353 }
1354
1355 /*
1356  * DS 5.x compatability API follow
1357  */
1358
1359 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
1360 {
1361 #if defined( LDAP_SLAPI )
1362         AttributeType *at;
1363
1364         if ( attr == NULL )
1365                 return LDAP_PARAM_ERROR;
1366
1367         at = attr->a_desc->ad_type;
1368
1369         *flags = SLAPI_ATTR_FLAG_STD_ATTR;
1370
1371         if ( is_at_single_value( at ) )
1372                 *flags |= SLAPI_ATTR_FLAG_SINGLE;
1373         if ( is_at_operational( at ) )
1374                 *flags |= SLAPI_ATTR_FLAG_OPATTR;
1375         if ( is_at_obsolete( at ) )
1376                 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
1377         if ( is_at_collective( at ) )
1378                 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
1379         if ( is_at_no_user_mod( at ) )
1380                 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
1381
1382         return LDAP_SUCCESS;
1383 #else
1384         return -1;
1385 #endif /* defined(LDAP_SLAPI) */
1386 }
1387
1388 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
1389 {
1390 #if defined( LDAP_SLAPI )
1391         unsigned long flags;
1392
1393         if ( slapi_attr_get_flags( attr, &flags ) != 0 )
1394                 return 0;
1395         return (flags & flag) ? 1 : 0;
1396 #else
1397         return 0;
1398 #endif /* defined(LDAP_SLAPI) */
1399 }
1400
1401 Slapi_Attr *slapi_attr_new( void )
1402 {
1403 #ifdef LDAP_SLAPI
1404         Attribute *ad;
1405
1406         ad = (Attribute  *)slapi_ch_calloc( 1, sizeof(*ad) );
1407
1408         return ad;
1409 #else
1410         return NULL;
1411 #endif
1412 }
1413
1414 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
1415 {
1416 #ifdef LDAP_SLAPI
1417         const char *text;
1418         AttributeDescription *ad = NULL;
1419
1420         if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
1421                 return NULL;
1422         }
1423
1424         a->a_desc = ad;
1425         a->a_vals = NULL;
1426         a->a_next = NULL;
1427         a->a_flags = 0;
1428
1429         return a;
1430 #else
1431         return NULL;
1432 #endif
1433 }
1434
1435 void slapi_attr_free( Slapi_Attr **a )
1436 {
1437 #ifdef LDAP_SLAPI
1438         attr_free( *a );
1439         *a = NULL;
1440 #endif
1441 }
1442
1443 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
1444 {
1445 #ifdef LDAP_SLAPI
1446         return attr_dup( (Slapi_Attr *)attr );
1447 #else
1448         return NULL;
1449 #endif
1450 }
1451
1452 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
1453 {
1454 #ifdef LDAP_SLAPI
1455         return value_add_one( &a->a_vals, (Slapi_Value *)v );
1456 #else
1457         return -1;
1458 #endif
1459 }
1460
1461 int slapi_attr_type2plugin( const char *type, void **pi )
1462 {
1463         *pi = NULL;
1464
1465         return LDAP_OTHER;
1466 }
1467
1468 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
1469 {
1470 #ifdef LDAP_SLAPI
1471         if ( attr == NULL ) {
1472                 return LDAP_PARAM_ERROR;
1473         }
1474
1475         *type = attr->a_desc->ad_cname.bv_val;
1476
1477         return LDAP_SUCCESS;
1478 #else
1479         return -1;
1480 #endif
1481 }
1482
1483 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
1484 {
1485 #ifdef LDAP_SLAPI
1486         if ( attr == NULL ) {
1487                 return LDAP_PARAM_ERROR;
1488         }
1489         *oidp = attr->a_desc->ad_type->sat_oid;
1490
1491         return LDAP_SUCCESS;
1492 #else
1493         return -1;
1494 #endif
1495 }
1496
1497 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
1498 {
1499 #ifdef LDAP_SLAPI
1500         MatchingRule *mr;
1501         int ret;
1502         int rc;
1503         const char *text;
1504
1505         mr = a->a_desc->ad_type->sat_equality;
1506         rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
1507                 (struct berval *)v1, (void *)v2, &text );
1508         if ( rc != LDAP_SUCCESS ) 
1509                 return -1;
1510
1511         return ( ret == 0 ) ? 0 : -1;
1512 #else
1513         return -1;
1514 #endif
1515 }
1516
1517 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
1518 {
1519 #ifdef LDAP_SLAPI
1520         MatchingRule *mr;
1521         struct berval *bv;
1522         int j;
1523         const char *text;
1524         int rc;
1525         int ret;
1526
1527         mr = a->a_desc->ad_type->sat_equality;
1528         for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
1529                 rc = value_match( &ret, a->a_desc, mr,
1530                         SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
1531                 if ( rc != LDAP_SUCCESS ) {
1532                         return -1;
1533                 }
1534                 if ( ret == 0 ) {
1535                         return 0;
1536                 }
1537         }
1538 #endif
1539         return -1;
1540 }
1541
1542 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
1543 {
1544 #ifdef LDAP_SLAPI
1545         AttributeDescription *a1 = NULL;
1546         AttributeDescription *a2 = NULL;
1547         const char *text;
1548         int ret;
1549
1550         if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
1551                 return -1;
1552         }
1553
1554         if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
1555                 return 1;
1556         }
1557
1558 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
1559         ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
1560                 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
1561
1562         switch ( opt ) {
1563         case SLAPI_TYPE_CMP_EXACT:
1564                 ret = ad_cmp( a1, a2 );
1565                 break;
1566         case SLAPI_TYPE_CMP_BASE:
1567                 ret = ad_base_cmp( a1, a2 );
1568                 break;
1569         case SLAPI_TYPE_CMP_SUBTYPE:
1570                 ret = is_ad_subtype( a2, a2 );
1571                 break;
1572         default:
1573                 ret = -1;
1574                 break;
1575         }
1576
1577         return ret;
1578 #else
1579         return -1;
1580 #endif
1581 }
1582
1583 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
1584 {
1585 #ifdef LDAP_SLAPI
1586         return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
1587 #else
1588         return -1;
1589 #endif
1590 }
1591
1592 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
1593 {
1594 #ifdef LDAP_SLAPI
1595         return slapi_valueset_first_value( &a->a_vals, v );
1596 #else
1597         return -1;
1598 #endif
1599 }
1600
1601 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
1602 {
1603 #ifdef LDAP_SLAPI
1604         return slapi_valueset_next_value( &a->a_vals, hint, v );
1605 #else
1606         return -1;
1607 #endif
1608 }
1609
1610 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
1611 {
1612 #ifdef LDAP_SLAPI
1613         *numValues = slapi_valueset_count( &a->a_vals );
1614
1615         return 0;
1616 #else
1617         return -1;
1618 #endif
1619 }
1620
1621 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
1622 {
1623 #ifdef LDAP_SLAPI
1624         *vs = &((Slapi_Attr *)a)->a_vals;
1625
1626         return 0;
1627 #else
1628         return -1;
1629 #endif
1630 }
1631
1632 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
1633 {
1634 #ifdef LDAP_SLAPI
1635         return slapi_attr_get_values( a, vals );
1636 #else
1637         return -1;
1638 #endif
1639 }
1640
1641 char *slapi_attr_syntax_normalize( const char *s )
1642 {
1643 #ifdef LDAP_SLAPI
1644         AttributeDescription *ad = NULL;
1645         const char *text;
1646
1647         if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
1648                 return NULL;
1649         }
1650
1651         return ad->ad_cname.bv_val;
1652 #else
1653         return -1;
1654 #endif
1655 }
1656
1657 Slapi_Value *slapi_value_new( void )
1658 {
1659 #ifdef LDAP_SLAPI
1660         struct berval *bv;
1661
1662         bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
1663
1664         return bv;
1665 #else
1666         return NULL;
1667 #endif
1668 }
1669
1670 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
1671 {
1672 #ifdef LDAP_SLAPI
1673         return ber_dupbv( NULL, (struct berval *)bval );
1674 #else
1675         return NULL;
1676 #endif
1677 }
1678
1679 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
1680 {
1681 #ifdef LDAP_SLAPI
1682         return slapi_value_new_berval( v );
1683 #else
1684         return NULL;
1685 #endif
1686 }
1687
1688 Slapi_Value *slapi_value_new_string(const char *s)
1689 {
1690 #ifdef LDAP_SLAPI
1691         struct berval bv;
1692
1693         bv.bv_val = (char *)s;
1694         bv.bv_len = strlen( s );
1695
1696         return slapi_value_new_berval( &bv );
1697 #else
1698         return NULL;
1699 #endif
1700 }
1701
1702 Slapi_Value *slapi_value_init(Slapi_Value *val)
1703 {
1704 #ifdef LDAP_SLAPI
1705         val->bv_val = NULL;
1706         val->bv_len = 0;
1707
1708         return val;
1709 #else
1710         return NULL;
1711 #endif
1712 }
1713
1714 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
1715 {
1716 #ifdef LDAP_SLAPI
1717         return ber_dupbv( v, bval );
1718 #else
1719         return NULL;
1720 #endif
1721 }
1722
1723 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
1724 {
1725 #ifdef LDAP_SLAPI
1726         v->bv_val = slapi_ch_strdup( (char *)s );
1727         v->bv_len = strlen( s );
1728
1729         return v;
1730 #else
1731         return NULL;
1732 #endif
1733 }
1734
1735 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
1736 {
1737 #ifdef LDAP_SLAPI
1738         return slapi_value_new_value( v );
1739 #else
1740         return NULL;
1741 #endif
1742 }
1743
1744 void slapi_value_free(Slapi_Value **value)
1745 {
1746 #ifdef LDAP_SLAPI       
1747         if ( value == NULL ) {
1748                 return;
1749         }
1750
1751         if ( (*value) != NULL ) {
1752                 slapi_ch_free( (void **)&(*value)->bv_val );
1753                 slapi_ch_free( (void **)value );
1754         }
1755 #endif
1756 }
1757
1758 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
1759 {
1760 #ifdef LDAP_SLAPI
1761         return value;
1762 #else
1763         return NULL;
1764 #endif
1765 }
1766
1767 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
1768 {
1769 #ifdef LDAP_SLAPI
1770         if ( value == NULL ) {
1771                 return NULL;
1772         }
1773         if ( value->bv_val != NULL ) {
1774                 slapi_ch_free( (void **)&value->bv_val );
1775         }
1776         slapi_value_init_berval( value, (struct berval *)bval );
1777
1778         return value;
1779 #else
1780         return NULL;
1781 #endif
1782 }
1783
1784 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
1785 {
1786 #ifdef LDAP_SLAPI
1787         if ( value == NULL ) {
1788                 return NULL;
1789         }
1790         return slapi_value_set_berval( value, vfrom );
1791 #else
1792         return NULL;
1793 #endif
1794 }
1795
1796 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
1797 {
1798 #ifdef LDAP_SLAPI
1799         if ( value == NULL ) {
1800                 return NULL;
1801         }
1802         if ( value->bv_val != NULL ) {
1803                 slapi_ch_free( (void **)&value->bv_val );
1804         }
1805         value->bv_val = slapi_ch_malloc( len );
1806         value->bv_len = len;
1807         AC_MEMCPY( value->bv_val, val, len );
1808
1809         return value;
1810 #else
1811         return NULL;
1812 #endif
1813 }
1814
1815 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
1816 {
1817 #ifdef LDAP_SLAPI
1818         if ( value == NULL ) {
1819                 return -1;
1820         }
1821         slapi_value_set( value, (void *)strVal, strlen( strVal ) );
1822         return 0;
1823 #else
1824         return NULL;
1825 #endif
1826 }
1827
1828 int slapi_value_set_int(Slapi_Value *value, int intVal)
1829 {
1830 #ifdef LDAP_SLAPI
1831         char buf[64];
1832
1833         snprintf( buf, sizeof( buf ), "%d", intVal );
1834
1835         return slapi_value_set_string( value, buf );
1836 #else
1837         return -1;
1838 #endif
1839 }
1840
1841 const char *slapi_value_get_string(const Slapi_Value *value)
1842 {
1843 #ifdef LDAP_SLAPI
1844         if ( value == NULL ) {
1845                 return NULL;
1846         }
1847         return value->bv_val;
1848 #else
1849         return NULL;
1850 #endif
1851 }
1852
1853 #ifdef LDAP_SLAPI
1854 static int checkBVString(const struct berval *bv)
1855 {
1856         int i;
1857
1858         for ( i = 0; i < bv->bv_len; i++ ) {
1859                 if ( bv->bv_val[i] == '\0' )
1860                         return 0;
1861         }
1862         if ( bv->bv_val[i] != '\0' )
1863                 return 0;
1864
1865         return 1;
1866 }
1867 #endif
1868
1869 int slapi_value_get_int(const Slapi_Value *value)
1870 {
1871 #ifdef LDAP_SLAPI
1872         if ( value == NULL ) return 0;
1873         if ( !checkBVString( value ) ) return 0;
1874
1875         return (int)strtol( value->bv_val, NULL, 10 );
1876 #else
1877         return NULL;
1878 #endif
1879 }
1880
1881 unsigned int slapi_value_get_uint(const Slapi_Value *value)
1882 {
1883 #ifdef LDAP_SLAPI
1884         if ( value == NULL ) return 0;
1885         if ( !checkBVString( value ) ) return 0;
1886
1887         return (unsigned int)strtoul( value->bv_val, NULL, 10 );
1888 #else
1889         return NULL;
1890 #endif
1891 }
1892
1893 long slapi_value_get_long(const Slapi_Value *value)
1894 {
1895 #ifdef LDAP_SLAPI
1896         if ( value == NULL ) return 0;
1897         if ( !checkBVString( value ) ) return 0;
1898
1899         return strtol( value->bv_val, NULL, 10 );
1900 #else
1901         return NULL;
1902 #endif
1903 }
1904
1905 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
1906 {
1907 #ifdef LDAP_SLAPI
1908         if ( value == NULL ) return 0;
1909         if ( !checkBVString( value ) ) return 0;
1910
1911         return strtoul( value->bv_val, NULL, 10 );
1912 #else
1913         return NULL;
1914 #endif
1915 }
1916
1917 size_t slapi_value_get_length(const Slapi_Value *value)
1918 {
1919 #ifdef LDAP_SLAPI
1920         if ( value == NULL )
1921                 return 0;
1922
1923         return (size_t) value->bv_len;
1924 #else
1925         return 0;
1926 #endif
1927 }
1928
1929 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
1930 {
1931 #ifdef LDAP_SLAPI
1932         return slapi_attr_value_cmp( a, v1, v2 );
1933 #else
1934         return -1;
1935 #endif
1936 }
1937
1938 /* A ValueSet is a container for a BerVarray. */
1939 Slapi_ValueSet *slapi_valueset_new( void )
1940 {
1941 #ifdef LDAP_SLAPI
1942         Slapi_ValueSet *vs;
1943
1944         vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
1945         *vs = NULL;
1946
1947         return vs;
1948 #else
1949         return NULL;
1950 #endif
1951 }
1952
1953 void slapi_valueset_free(Slapi_ValueSet *vs)
1954 {
1955 #ifdef LDAP_SLAPI
1956         if ( vs != NULL ) {
1957                 BerVarray vp = *vs;
1958
1959                 ber_bvarray_free( vp );
1960                 slapi_ch_free( (void **)&vp );
1961
1962                 *vs = NULL;
1963         }
1964 #endif
1965 }
1966
1967 void slapi_valueset_init(Slapi_ValueSet *vs)
1968 {
1969 #ifdef LDAP_SLAPI
1970         if ( vs != NULL && *vs == NULL ) {
1971                 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
1972                 (*vs)->bv_val = NULL;
1973                 (*vs)->bv_len = 0;
1974         }
1975 #endif
1976 }
1977
1978 void slapi_valueset_done(Slapi_ValueSet *vs)
1979 {
1980 #ifdef LDAP_SLAPI
1981         BerVarray vp;
1982
1983         if ( vs == NULL )
1984                 return;
1985
1986         for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
1987                 vp->bv_len = 0;
1988                 slapi_ch_free( (void **)&vp->bv_val );
1989         }
1990         /* but don't free *vs or vs */
1991 #endif
1992 }
1993
1994 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
1995 {
1996 #ifdef LDAP_SLAPI
1997         struct berval bv;
1998
1999         ber_dupbv( &bv, (Slapi_Value *)addval );
2000         ber_bvarray_add( vs, &bv );
2001 #endif
2002 }
2003
2004 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2005 {
2006 #ifdef LDAP_SLAPI
2007         return slapi_valueset_next_value( vs, 0, v );
2008 #else
2009         return -1;
2010 #endif
2011 }
2012
2013 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2014 {
2015 #ifdef LDAP_SLAPI
2016         int i;
2017         BerVarray vp;
2018
2019         if ( vs == NULL )
2020                 return -1;
2021
2022         vp = *vs;
2023
2024         for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2025                 if ( i == index ) {
2026                         *v = &vp[i];
2027                         return index + 1;
2028                 }
2029         }
2030 #endif
2031
2032         return -1;
2033 }
2034
2035 int slapi_valueset_count( const Slapi_ValueSet *vs )
2036 {
2037 #ifdef LDAP_SLAPI
2038         int i;
2039         BerVarray vp;
2040
2041         if ( vs == NULL )
2042                 return 0;
2043
2044         vp = *vs;
2045
2046         for ( i = 0; vp[i].bv_val != NULL; i++ )
2047                 ;
2048
2049         return i;
2050 #else
2051         return 0;
2052 #endif
2053
2054 }
2055
2056 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2057 {
2058 #ifdef LDAP_SLAPI
2059         BerVarray vp;
2060
2061         for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2062                 slapi_valueset_add_value( vs1, vp );
2063         }
2064 #endif
2065 }
2066
2067 /*
2068  * Attribute sets are an OpenLDAP extension for the 
2069  * virtual operational attribute coalescing plugin 
2070  */
2071 Slapi_AttrSet *slapi_x_attrset_new( void )
2072 {
2073 #ifdef LDAP_SLAPI
2074         Slapi_AttrSet *a;
2075
2076         /*
2077          * Like a Slapi_ValueSet, a Slapi_AttrSet is a container
2078          * for objects: we need this because it may be initially
2079          * empty.
2080          */
2081         a = (Slapi_AttrSet *)slapi_ch_malloc( sizeof( *a ) );
2082         *a = NULL;
2083
2084         return a;
2085 #else
2086         return NULL;
2087 #endif
2088 }
2089
2090 Slapi_AttrSet *slapi_x_attrset_init( Slapi_AttrSet *as, Slapi_Attr *a )
2091 {
2092 #ifdef LDAP_SLAPI
2093         *as = a;
2094         a->a_next = NULL;
2095
2096         return as;
2097 #else
2098         return NULL;
2099 #endif
2100 }
2101
2102 void slapi_x_attrset_free( Slapi_AttrSet **pAs )
2103 {
2104 #ifdef LDAP_SLAPI
2105         Slapi_AttrSet *as = *pAs;
2106
2107         if ( as != NULL ) {
2108                 attrs_free( *as );
2109                 slapi_ch_free( (void **)pAs );
2110         }
2111 #endif
2112 }
2113
2114 Slapi_AttrSet *slapi_x_attrset_dup( Slapi_AttrSet *as )
2115 {
2116 #ifdef LDAP_SLAPI
2117         Slapi_AttrSet *newAs = slapi_x_attrset_new();
2118         
2119         if ( *as != NULL )
2120                 *newAs = attrs_dup( *as );
2121
2122         return newAs;
2123 #else
2124         return NULL;
2125 #endif
2126 }
2127
2128 int slapi_x_attrset_add_attr( Slapi_AttrSet *as, Slapi_Attr *a )
2129 {
2130 #ifdef LDAP_SLAPI
2131         Slapi_Attr *nextAttr;
2132
2133         if ( as == NULL || a == NULL )
2134                 return -1;
2135
2136         if ( *as == NULL ) {
2137                 /* First attribute */
2138                 nextAttr = NULL;
2139                 (*as) = a;
2140         } else {
2141                 /* Non-first attribute */
2142                 nextAttr = (*as)->a_next;
2143                 (*as)->a_next = a;
2144         }
2145
2146         a->a_next = nextAttr;
2147
2148         return 0;
2149 #else
2150         return -1;
2151 #endif
2152 }
2153
2154 int slapi_x_attrset_add_attr_copy( Slapi_AttrSet *as, Slapi_Attr *a )
2155 {
2156 #ifdef LDAP_SLAPI
2157         Slapi_Attr *adup;
2158
2159         adup = slapi_attr_dup( a );
2160         return slapi_x_attrset_add_attr( as, adup );
2161 #else
2162         return -1;
2163 #endif
2164 }
2165
2166 int slapi_x_attrset_find( Slapi_AttrSet *as, const char *type, Slapi_Attr **attr )
2167 {
2168 #ifdef LDAP_SLAPI
2169         AttributeDescription *ad = NULL;
2170         const char *text;
2171
2172         if ( as == NULL || *as == NULL ) {
2173                 return -1;
2174         }
2175
2176         if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2177                 return -1;
2178         }
2179         *attr = attrs_find( *as, ad );
2180         return ( *attr == NULL ) ? -1 : 0;
2181 #else
2182         return -1;
2183 #endif
2184 }
2185
2186 int slapi_x_attrset_merge( Slapi_AttrSet *as, const char *type, Slapi_ValueSet *vals )
2187 {
2188 #ifdef LDAP_SLAPI
2189         AttributeDescription *ad = NULL;
2190         Slapi_AttrSet *a;
2191         const char *text;
2192
2193         if ( vals == NULL || *vals == NULL ) {
2194                 /* Must have something to add. */
2195                 return -1;
2196         }
2197
2198         if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2199                 return -1;
2200         }
2201
2202         for ( a = as; *a != NULL; a = &(*a)->a_next ) {
2203                 if ( ad_cmp( (*a)->a_desc, ad ) == 0 ) {
2204                         break;
2205                 }
2206         }
2207
2208         if ( *a == NULL ) {
2209                 *a = (Slapi_Attr *) slapi_ch_malloc( sizeof(Attribute) );
2210                 (*a)->a_desc = ad;
2211                 (*a)->a_vals = NULL;
2212                 (*a)->a_next = NULL;
2213                 (*a)->a_flags = 0;
2214         }
2215
2216         return value_add ( &(*a)->a_vals, *vals );
2217 #else
2218         return -1;
2219 #endif
2220 }
2221
2222 int slapi_x_attrset_merge_bervals( Slapi_AttrSet *as, const char *type, struct berval **vals )
2223 {
2224 #ifdef LDAP_SLAPI
2225         BerVarray vp;
2226         int rc;
2227
2228         if ( bvptr2obj( vals, &vp ) != LDAP_SUCCESS ) {
2229                 return -1;
2230         }
2231         rc = slapi_x_attrset_merge( as, type, &vp );
2232         slapi_ch_free( (void **)&vp );
2233
2234         return rc;
2235 #else
2236         return -1;
2237 #endif
2238 }
2239
2240 int slapi_x_attrset_delete( Slapi_AttrSet *as, const char *type )
2241 {
2242 #ifdef LDAP_SLAPI
2243         AttributeDescription *ad = NULL;
2244         const char *text;
2245
2246         if ( as == NULL ) {
2247                 return -1;
2248         }
2249
2250         if ( *as == NULL ) {
2251                 return -1;
2252         }
2253
2254         if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2255                 return -1;
2256         }
2257
2258         if ( attr_delete( as, ad ) != LDAP_SUCCESS ) {
2259                 return -1;
2260         }
2261
2262         return 0;
2263 #else
2264         return -1;
2265 #endif
2266 }