]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/api.c
763956b0498450cf570848eee9e76c0e7f8b5bef
[openldap] / servers / slapd / back-sql / api.c
1 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
2  *
3  * Copyright 1999-2005 The OpenLDAP Foundation.
4  * Portions Copyright 1999 Dmitry Kovalev.
5  * Portions Copyright 2004 Pierangelo Masarati.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Dmitry Kovalev for inclusion
18  * by OpenLDAP Software.  Additional significant contributors include
19  * Pierangelo Masarati.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include "ac/string.h"
27
28 #include "slap.h"
29 #include "proto-sql.h"
30
31 static backsql_api *backsqlapi;
32
33 int
34 backsql_api_config( backsql_info *bi, const char *name )
35 {
36         backsql_api     *ba;
37
38         assert( bi );
39         assert( name );
40
41         for ( ba = backsqlapi; ba; ba = ba->ba_next ) {
42                 if ( strcasecmp( name, ba->ba_name ) == 0 ) {
43                         backsql_api     *ba2;
44
45                         ba2 = ch_malloc( sizeof( backsql_api ) );
46                         *ba2 = *ba;
47                         ba2->ba_next = bi->sql_api;
48                         bi->sql_api = ba2;
49                         return 0;
50                 }
51         }
52
53         return 1;
54 }
55
56 int
57 backsql_api_register( backsql_api *ba )
58 {
59         backsql_api     *ba2;
60
61         assert( ba );
62
63         if ( ba->ba_name == NULL ) {
64                 fprintf( stderr, "API module has no name\n" );
65                 exit(EXIT_FAILURE);
66         }
67
68         for ( ba2 = backsqlapi; ba2; ba2 = ba2->ba_next ) {
69                 if ( strcasecmp( ba->ba_name, ba2->ba_name ) == 0 ) {
70                         fprintf( stderr, "API module \"%s\" already defined\n", ba->ba_name );
71                         exit( EXIT_FAILURE );
72                 }
73         }
74
75         ba->ba_next = backsqlapi;
76         backsqlapi = ba;
77
78         return 0;
79 }
80
81 int
82 backsql_api_dn2odbc( Operation *op, SlapReply *rs, struct berval *dn )
83 {
84         backsql_info    *bi = (backsql_info *)op->o_bd->be_private;
85         backsql_api     *ba;
86         int             rc;
87         struct berval   bv;
88
89         ba = bi->sql_api;
90
91         if ( ba == NULL ) {
92                 return 0;
93         }
94
95         ber_dupbv( &bv, dn );
96
97         for ( ; ba; ba = ba->ba_next ) {
98                 if ( ba->ba_dn2odbc ) {
99                         /*
100                          * The dn2odbc() helper is supposed to rewrite
101                          * the contents of bv, freeing the original value
102                          * with ch_free() if required and replacing it 
103                          * with a newly allocated one using ch_malloc() 
104                          * or companion functions.
105                          *
106                          * NOTE: it is supposed to __always__ free
107                          * the value of bv in case of error, and reset
108                          * it with BER_BVZERO() .
109                          */
110                         rc = ( *ba->ba_dn2odbc )( op, rs, &bv );
111
112                         if ( rc ) {
113                                 /* in case of error, dn2odbc() must cleanup */
114                                 assert( BER_BVISNULL( &bv ) );
115
116                                 return rc;
117                         }
118                 }
119         }
120
121         assert( !BER_BVISNULL( &bv ) );
122
123         *dn = bv;
124
125         return 0;
126 }
127
128 int
129 backsql_api_odbc2dn( Operation *op, SlapReply *rs, struct berval *dn )
130 {
131         backsql_info    *bi = (backsql_info *)op->o_bd->be_private;
132         backsql_api     *ba;
133         int             rc;
134         struct berval   bv;
135
136         ba = bi->sql_api;
137
138         if ( ba == NULL ) {
139                 return 0;
140         }
141
142         ber_dupbv( &bv, dn );
143
144         for ( ; ba; ba = ba->ba_next ) {
145                 if ( ba->ba_dn2odbc ) {
146                         rc = ( *ba->ba_odbc2dn )( op, rs, &bv );
147                         /*
148                          * The odbc2dn() helper is supposed to rewrite
149                          * the contents of bv, freeing the original value
150                          * with ch_free() if required and replacing it 
151                          * with a newly allocated one using ch_malloc() 
152                          * or companion functions.
153                          *
154                          * NOTE: it is supposed to __always__ free
155                          * the value of bv in case of error, and reset
156                          * it with BER_BVZERO() .
157                          */
158                         if ( rc ) {
159                                 /* in case of error, odbc2dn() must cleanup */
160                                 assert( BER_BVISNULL( &bv ) );
161
162                                 return rc;
163                         }
164                 }
165         }
166
167         assert( !BER_BVISNULL( &bv ) );
168
169         *dn = bv;
170
171         return 0;
172 }
173