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