2004-04-10 17:33:55 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2018-03-22 23:35:24 +08:00
|
|
|
* Copyright 1999-2018 The OpenLDAP Foundation.
|
2004-04-10 17:33:55 +08:00
|
|
|
* Portions Copyright 1999 Dmitry Kovalev.
|
2005-01-03 18:51:59 +08:00
|
|
|
* Portions Copyright 2004 Pierangelo Masarati.
|
2004-04-10 17:33:55 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
/* ACKNOWLEDGEMENTS:
|
|
|
|
* This work was initially developed by Dmitry Kovalev for inclusion
|
2005-01-03 18:51:59 +08:00
|
|
|
* by OpenLDAP Software. Additional significant contributors include
|
|
|
|
* Pierangelo Masarati.
|
2004-04-10 17:33:55 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include "ac/string.h"
|
|
|
|
|
|
|
|
#include "slap.h"
|
|
|
|
#include "proto-sql.h"
|
|
|
|
|
|
|
|
static backsql_api *backsqlapi;
|
|
|
|
|
|
|
|
int
|
2005-01-06 00:23:00 +08:00
|
|
|
backsql_api_config( backsql_info *bi, const char *name, int argc, char *argv[] )
|
2004-04-10 17:33:55 +08:00
|
|
|
{
|
|
|
|
backsql_api *ba;
|
|
|
|
|
2005-07-18 14:22:33 +08:00
|
|
|
assert( bi != NULL );
|
|
|
|
assert( name != NULL );
|
2004-04-10 17:33:55 +08:00
|
|
|
|
|
|
|
for ( ba = backsqlapi; ba; ba = ba->ba_next ) {
|
|
|
|
if ( strcasecmp( name, ba->ba_name ) == 0 ) {
|
|
|
|
backsql_api *ba2;
|
|
|
|
|
|
|
|
ba2 = ch_malloc( sizeof( backsql_api ) );
|
|
|
|
*ba2 = *ba;
|
2005-01-06 00:23:00 +08:00
|
|
|
|
|
|
|
if ( ba2->ba_config ) {
|
|
|
|
if ( ( *ba2->ba_config )( ba2, argc, argv ) ) {
|
|
|
|
ch_free( ba2 );
|
|
|
|
return 1;
|
|
|
|
}
|
2011-11-05 13:00:44 +08:00
|
|
|
ba2->ba_argc = argc;
|
|
|
|
if ( argc ) {
|
|
|
|
int i;
|
|
|
|
ba2->ba_argv = ch_malloc( argc * sizeof(char *));
|
|
|
|
for ( i=0; i<argc; i++ )
|
|
|
|
ba2->ba_argv[i] = ch_strdup( argv[i] );
|
|
|
|
}
|
2005-01-06 00:23:00 +08:00
|
|
|
}
|
|
|
|
|
2004-10-02 20:05:42 +08:00
|
|
|
ba2->ba_next = bi->sql_api;
|
|
|
|
bi->sql_api = ba2;
|
2004-04-10 17:33:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-01-06 00:23:00 +08:00
|
|
|
int
|
|
|
|
backsql_api_destroy( backsql_info *bi )
|
|
|
|
{
|
|
|
|
backsql_api *ba;
|
|
|
|
|
2005-07-18 14:22:33 +08:00
|
|
|
assert( bi != NULL );
|
2005-01-06 00:23:00 +08:00
|
|
|
|
|
|
|
ba = bi->sql_api;
|
|
|
|
|
|
|
|
if ( ba == NULL ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( ; ba; ba = ba->ba_next ) {
|
|
|
|
if ( ba->ba_destroy ) {
|
|
|
|
(void)( *ba->ba_destroy )( ba );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-10 17:33:55 +08:00
|
|
|
int
|
|
|
|
backsql_api_register( backsql_api *ba )
|
|
|
|
{
|
|
|
|
backsql_api *ba2;
|
|
|
|
|
2005-07-18 14:22:33 +08:00
|
|
|
assert( ba != NULL );
|
2005-01-06 00:23:00 +08:00
|
|
|
assert( ba->ba_private == NULL );
|
2004-04-10 17:33:55 +08:00
|
|
|
|
|
|
|
if ( ba->ba_name == NULL ) {
|
|
|
|
fprintf( stderr, "API module has no name\n" );
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( ba2 = backsqlapi; ba2; ba2 = ba2->ba_next ) {
|
|
|
|
if ( strcasecmp( ba->ba_name, ba2->ba_name ) == 0 ) {
|
|
|
|
fprintf( stderr, "API module \"%s\" already defined\n", ba->ba_name );
|
2004-12-02 07:16:21 +08:00
|
|
|
exit( EXIT_FAILURE );
|
2004-04-10 17:33:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ba->ba_next = backsqlapi;
|
|
|
|
backsqlapi = ba;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
backsql_api_dn2odbc( Operation *op, SlapReply *rs, struct berval *dn )
|
|
|
|
{
|
2004-10-02 20:05:42 +08:00
|
|
|
backsql_info *bi = (backsql_info *)op->o_bd->be_private;
|
2004-04-10 17:33:55 +08:00
|
|
|
backsql_api *ba;
|
|
|
|
int rc;
|
|
|
|
struct berval bv;
|
|
|
|
|
2004-10-02 20:05:42 +08:00
|
|
|
ba = bi->sql_api;
|
2004-04-10 17:33:55 +08:00
|
|
|
|
|
|
|
if ( ba == NULL ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ber_dupbv( &bv, dn );
|
|
|
|
|
|
|
|
for ( ; ba; ba = ba->ba_next ) {
|
|
|
|
if ( ba->ba_dn2odbc ) {
|
2004-12-03 06:59:55 +08:00
|
|
|
/*
|
|
|
|
* The dn2odbc() helper is supposed to rewrite
|
|
|
|
* the contents of bv, freeing the original value
|
|
|
|
* with ch_free() if required and replacing it
|
|
|
|
* with a newly allocated one using ch_malloc()
|
|
|
|
* or companion functions.
|
|
|
|
*
|
|
|
|
* NOTE: it is supposed to __always__ free
|
|
|
|
* the value of bv in case of error, and reset
|
|
|
|
* it with BER_BVZERO() .
|
|
|
|
*/
|
2004-04-10 17:33:55 +08:00
|
|
|
rc = ( *ba->ba_dn2odbc )( op, rs, &bv );
|
|
|
|
|
|
|
|
if ( rc ) {
|
2004-12-03 06:59:55 +08:00
|
|
|
/* in case of error, dn2odbc() must cleanup */
|
|
|
|
assert( BER_BVISNULL( &bv ) );
|
|
|
|
|
2004-04-10 17:33:55 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-03 06:59:55 +08:00
|
|
|
assert( !BER_BVISNULL( &bv ) );
|
|
|
|
|
2004-04-10 17:33:55 +08:00
|
|
|
*dn = bv;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
backsql_api_odbc2dn( Operation *op, SlapReply *rs, struct berval *dn )
|
|
|
|
{
|
2004-10-02 20:05:42 +08:00
|
|
|
backsql_info *bi = (backsql_info *)op->o_bd->be_private;
|
2004-04-10 17:33:55 +08:00
|
|
|
backsql_api *ba;
|
|
|
|
int rc;
|
|
|
|
struct berval bv;
|
|
|
|
|
2004-10-02 20:05:42 +08:00
|
|
|
ba = bi->sql_api;
|
2004-04-10 17:33:55 +08:00
|
|
|
|
|
|
|
if ( ba == NULL ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ber_dupbv( &bv, dn );
|
|
|
|
|
|
|
|
for ( ; ba; ba = ba->ba_next ) {
|
|
|
|
if ( ba->ba_dn2odbc ) {
|
|
|
|
rc = ( *ba->ba_odbc2dn )( op, rs, &bv );
|
2004-12-03 06:59:55 +08:00
|
|
|
/*
|
|
|
|
* The odbc2dn() helper is supposed to rewrite
|
|
|
|
* the contents of bv, freeing the original value
|
|
|
|
* with ch_free() if required and replacing it
|
|
|
|
* with a newly allocated one using ch_malloc()
|
|
|
|
* or companion functions.
|
|
|
|
*
|
|
|
|
* NOTE: it is supposed to __always__ free
|
|
|
|
* the value of bv in case of error, and reset
|
|
|
|
* it with BER_BVZERO() .
|
|
|
|
*/
|
2004-04-10 17:33:55 +08:00
|
|
|
if ( rc ) {
|
2004-12-03 06:59:55 +08:00
|
|
|
/* in case of error, odbc2dn() must cleanup */
|
|
|
|
assert( BER_BVISNULL( &bv ) );
|
|
|
|
|
2004-04-10 17:33:55 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-03 06:59:55 +08:00
|
|
|
assert( !BER_BVISNULL( &bv ) );
|
|
|
|
|
2004-04-10 17:33:55 +08:00
|
|
|
*dn = bv;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|