Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / back-sql / api.c
blob823623b17d2e03306d8fcc193e6ce0476b8b3927
1 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 * Copyright 1999-2008 The OpenLDAP Foundation.
4 * Portions Copyright 1999 Dmitry Kovalev.
5 * Portions Copyright 2004 Pierangelo Masarati.
6 * All rights reserved.
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.
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>.
16 /* ACKNOWLEDGEMENTS:
17 * This work was initially developed by Dmitry Kovalev for inclusion
18 * by OpenLDAP Software. Additional significant contributors include
19 * Pierangelo Masarati.
22 #include "portable.h"
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include "ac/string.h"
28 #include "slap.h"
29 #include "proto-sql.h"
31 static backsql_api *backsqlapi;
33 int
34 backsql_api_config( backsql_info *bi, const char *name, int argc, char *argv[] )
36 backsql_api *ba;
38 assert( bi != NULL );
39 assert( name != NULL );
41 for ( ba = backsqlapi; ba; ba = ba->ba_next ) {
42 if ( strcasecmp( name, ba->ba_name ) == 0 ) {
43 backsql_api *ba2;
45 ba2 = ch_malloc( sizeof( backsql_api ) );
46 *ba2 = *ba;
48 if ( ba2->ba_config ) {
49 if ( ( *ba2->ba_config )( ba2, argc, argv ) ) {
50 ch_free( ba2 );
51 return 1;
55 ba2->ba_next = bi->sql_api;
56 bi->sql_api = ba2;
57 return 0;
61 return 1;
64 int
65 backsql_api_destroy( backsql_info *bi )
67 backsql_api *ba;
69 assert( bi != NULL );
71 ba = bi->sql_api;
73 if ( ba == NULL ) {
74 return 0;
77 for ( ; ba; ba = ba->ba_next ) {
78 if ( ba->ba_destroy ) {
79 (void)( *ba->ba_destroy )( ba );
83 return 0;
86 int
87 backsql_api_register( backsql_api *ba )
89 backsql_api *ba2;
91 assert( ba != NULL );
92 assert( ba->ba_private == NULL );
94 if ( ba->ba_name == NULL ) {
95 fprintf( stderr, "API module has no name\n" );
96 exit(EXIT_FAILURE);
99 for ( ba2 = backsqlapi; ba2; ba2 = ba2->ba_next ) {
100 if ( strcasecmp( ba->ba_name, ba2->ba_name ) == 0 ) {
101 fprintf( stderr, "API module \"%s\" already defined\n", ba->ba_name );
102 exit( EXIT_FAILURE );
106 ba->ba_next = backsqlapi;
107 backsqlapi = ba;
109 return 0;
113 backsql_api_dn2odbc( Operation *op, SlapReply *rs, struct berval *dn )
115 backsql_info *bi = (backsql_info *)op->o_bd->be_private;
116 backsql_api *ba;
117 int rc;
118 struct berval bv;
120 ba = bi->sql_api;
122 if ( ba == NULL ) {
123 return 0;
126 ber_dupbv( &bv, dn );
128 for ( ; ba; ba = ba->ba_next ) {
129 if ( ba->ba_dn2odbc ) {
131 * The dn2odbc() helper is supposed to rewrite
132 * the contents of bv, freeing the original value
133 * with ch_free() if required and replacing it
134 * with a newly allocated one using ch_malloc()
135 * or companion functions.
137 * NOTE: it is supposed to __always__ free
138 * the value of bv in case of error, and reset
139 * it with BER_BVZERO() .
141 rc = ( *ba->ba_dn2odbc )( op, rs, &bv );
143 if ( rc ) {
144 /* in case of error, dn2odbc() must cleanup */
145 assert( BER_BVISNULL( &bv ) );
147 return rc;
152 assert( !BER_BVISNULL( &bv ) );
154 *dn = bv;
156 return 0;
160 backsql_api_odbc2dn( Operation *op, SlapReply *rs, struct berval *dn )
162 backsql_info *bi = (backsql_info *)op->o_bd->be_private;
163 backsql_api *ba;
164 int rc;
165 struct berval bv;
167 ba = bi->sql_api;
169 if ( ba == NULL ) {
170 return 0;
173 ber_dupbv( &bv, dn );
175 for ( ; ba; ba = ba->ba_next ) {
176 if ( ba->ba_dn2odbc ) {
177 rc = ( *ba->ba_odbc2dn )( op, rs, &bv );
179 * The odbc2dn() helper is supposed to rewrite
180 * the contents of bv, freeing the original value
181 * with ch_free() if required and replacing it
182 * with a newly allocated one using ch_malloc()
183 * or companion functions.
185 * NOTE: it is supposed to __always__ free
186 * the value of bv in case of error, and reset
187 * it with BER_BVZERO() .
189 if ( rc ) {
190 /* in case of error, odbc2dn() must cleanup */
191 assert( BER_BVISNULL( &bv ) );
193 return rc;
198 assert( !BER_BVISNULL( &bv ) );
200 *dn = bv;
202 return 0;