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