1 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 * Copyright 1999-2014 The OpenLDAP Foundation.
4 * Portions Copyright 1999 Dmitry Kovalev.
5 * Portions Copyright 2004 Pierangelo Masarati.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
17 * This work was initially developed by Dmitry Kovalev for inclusion
18 * by OpenLDAP Software. Additional significant contributors include
19 * Pierangelo Masarati.
25 #include <sys/types.h>
26 #include "ac/string.h"
29 #include "proto-sql.h"
31 static backsql_api *backsqlapi;
34 backsql_api_config( backsql_info *bi, const char *name, int argc, char *argv[] )
39 assert( name != NULL );
41 for ( ba = backsqlapi; ba; ba = ba->ba_next ) {
42 if ( strcasecmp( name, ba->ba_name ) == 0 ) {
45 ba2 = ch_malloc( sizeof( backsql_api ) );
48 if ( ba2->ba_config ) {
49 if ( ( *ba2->ba_config )( ba2, argc, argv ) ) {
56 ba2->ba_argv = ch_malloc( argc * sizeof(char *));
57 for ( i=0; i<argc; i++ )
58 ba2->ba_argv[i] = ch_strdup( argv[i] );
62 ba2->ba_next = bi->sql_api;
72 backsql_api_destroy( backsql_info *bi )
84 for ( ; ba; ba = ba->ba_next ) {
85 if ( ba->ba_destroy ) {
86 (void)( *ba->ba_destroy )( ba );
94 backsql_api_register( backsql_api *ba )
99 assert( ba->ba_private == NULL );
101 if ( ba->ba_name == NULL ) {
102 fprintf( stderr, "API module has no name\n" );
106 for ( ba2 = backsqlapi; ba2; ba2 = ba2->ba_next ) {
107 if ( strcasecmp( ba->ba_name, ba2->ba_name ) == 0 ) {
108 fprintf( stderr, "API module \"%s\" already defined\n", ba->ba_name );
109 exit( EXIT_FAILURE );
113 ba->ba_next = backsqlapi;
120 backsql_api_dn2odbc( Operation *op, SlapReply *rs, struct berval *dn )
122 backsql_info *bi = (backsql_info *)op->o_bd->be_private;
133 ber_dupbv( &bv, dn );
135 for ( ; ba; ba = ba->ba_next ) {
136 if ( ba->ba_dn2odbc ) {
138 * The dn2odbc() helper is supposed to rewrite
139 * the contents of bv, freeing the original value
140 * with ch_free() if required and replacing it
141 * with a newly allocated one using ch_malloc()
142 * or companion functions.
144 * NOTE: it is supposed to __always__ free
145 * the value of bv in case of error, and reset
146 * it with BER_BVZERO() .
148 rc = ( *ba->ba_dn2odbc )( op, rs, &bv );
151 /* in case of error, dn2odbc() must cleanup */
152 assert( BER_BVISNULL( &bv ) );
159 assert( !BER_BVISNULL( &bv ) );
167 backsql_api_odbc2dn( Operation *op, SlapReply *rs, struct berval *dn )
169 backsql_info *bi = (backsql_info *)op->o_bd->be_private;
180 ber_dupbv( &bv, dn );
182 for ( ; ba; ba = ba->ba_next ) {
183 if ( ba->ba_dn2odbc ) {
184 rc = ( *ba->ba_odbc2dn )( op, rs, &bv );
186 * The odbc2dn() helper is supposed to rewrite
187 * the contents of bv, freeing the original value
188 * with ch_free() if required and replacing it
189 * with a newly allocated one using ch_malloc()
190 * or companion functions.
192 * NOTE: it is supposed to __always__ free
193 * the value of bv in case of error, and reset
194 * it with BER_BVZERO() .
197 /* in case of error, odbc2dn() must cleanup */
198 assert( BER_BVISNULL( &bv ) );
205 assert( !BER_BVISNULL( &bv ) );