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