]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/api.c
1a1c32ab9368de151b744e13f24807e70771e550
[openldap] / servers / slapd / back-sql / api.c
1 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
2  *
3  * Copyright 1999-2004 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                         rc = ( *ba->ba_dn2odbc )( op, rs, &bv );
98
99                         if ( rc ) {
100                                 return rc;
101                         }
102                 }
103         }
104
105         *dn = bv;
106
107         return 0;
108 }
109
110 int
111 backsql_api_odbc2dn( Operation *op, SlapReply *rs, struct berval *dn )
112 {
113         backsql_info    *bi = (backsql_info *)op->o_bd->be_private;
114         backsql_api     *ba;
115         int             rc;
116         struct berval   bv;
117
118         ba = bi->sql_api;
119
120         if ( ba == NULL ) {
121                 return 0;
122         }
123
124         ber_dupbv( &bv, dn );
125
126         for ( ; ba; ba = ba->ba_next ) {
127                 if ( ba->ba_dn2odbc ) {
128                         rc = ( *ba->ba_odbc2dn )( op, rs, &bv );
129
130                         if ( rc ) {
131                                 return rc;
132                         }
133                 }
134         }
135
136         *dn = bv;
137
138         return 0;
139 }
140