]> git.sur5r.net Git - openldap/blob - servers/slapd/extended.c
52eb15a7a7ac6cf3dd78faf2b7603b4e7dd1fca5
[openldap] / servers / slapd / extended.c
1 /* $OpenLDAP$ */
2 /* 
3  * Copyright 1999 The OpenLDAP Foundation.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted only
7  * as authorized by the OpenLDAP Public License.  A copy of this
8  * license is available at http://www.OpenLDAP.org/license.html or
9  * in file LICENSE in the top-level directory of the distribution.
10  */
11
12 /*
13  * LDAPv3 Extended Operation Request
14  *      ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
15  *              requestName      [0] LDAPOID,
16  *              requestValue     [1] OCTET STRING OPTIONAL
17  *      }
18  *
19  * LDAPv3 Extended Operation Response
20  *      ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
21  *              COMPONENTS OF LDAPResult,
22  *              responseName     [10] LDAPOID OPTIONAL,
23  *              response         [11] OCTET STRING OPTIONAL
24  *      }
25  *
26  */
27
28 #include "portable.h"
29
30 #include <stdio.h>
31 #include <ac/socket.h>
32
33 #include "slap.h"
34
35 #ifdef SLAPD_EXTERNAL_EXTENSIONS
36
37 typedef struct extensions_cookie_t {
38     Connection  *conn;
39     Operation   *op;
40 } extensions_cookie_t;
41
42 #define MAX_OID_LENGTH  128
43
44 typedef struct extensions_list_t {
45         struct extensions_list_t *next;
46         char *oid;
47         int (*ext_main)(int (*)(), void *, char *reqoid, struct berval *reqdata, char **rspoid, struct berval *rspdata, char **text);
48 } extensions_list_t;
49
50 extensions_list_t *supp_ext_list = NULL;
51
52 #define MAX_SUPP_EXT_TRACKED    64
53 int supp_ext_count = 0;
54 char *supportedExtensions[MAX_SUPP_EXT_TRACKED] = { NULL };
55
56 extensions_list_t *find_extension (extensions_list_t *list, char *oid);
57 int extensions_callback (extensions_cookie_t *cookie, int msg, int arg, void *argp);
58 #else
59
60 char *supportedExtensions[] = {
61         NULL
62 };
63 #endif
64
65
66 int
67 do_extended(
68     Connection  *conn,
69     Operation   *op
70 )
71 {
72         int rc = LDAP_SUCCESS;
73         char* reqoid ;
74         struct berval reqdata;
75         ber_tag_t tag;
76         ber_len_t len;
77 #ifdef SLAPD_EXTERNAL_EXTENSIONS
78         extensions_list_t *ext;
79         char *rspoid, *text;
80         struct berval rspdata;
81         extensions_cookie_t cookie;
82 #endif
83
84         Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
85
86         reqoid = NULL;
87         reqdata.bv_val = NULL;
88
89         if( op->o_protocol < LDAP_VERSION3 ) {
90                 Debug( LDAP_DEBUG_ANY, "do_extended: protocol version (%d) too low\n",
91                         op->o_protocol, 0 ,0 );
92                 send_ldap_disconnect( conn, op,
93                         LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
94                 rc = -1;
95                 goto done;
96         }
97
98         if ( ber_scanf( op->o_ber, "{a" /*}*/, &reqoid ) == LBER_ERROR ) {
99                 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
100                 send_ldap_disconnect( conn, op,
101                         LDAP_PROTOCOL_ERROR, "decoding error" );
102                 rc = -1;
103                 goto done;
104         }
105
106 #ifdef SLAPD_EXTERNAL_EXTENSIONS
107         if( !(ext = find_extension(supp_ext_list, reqoid)) )
108 #else
109         if( !charray_inlist( supportedExtensions, reqoid ) )
110 #endif
111         {
112                 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
113                         reqoid, 0 ,0 );
114                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
115                         NULL, "unsuppored extended operation", NULL, NULL );
116                 goto done;
117         }
118
119         tag = ber_peek_tag( op->o_ber, &len );
120         
121         if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
122                 if( ber_scanf( op->o_ber, "o", &reqdata ) == LBER_ERROR ) {
123                         Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
124                         send_ldap_disconnect( conn, op,
125                                 LDAP_PROTOCOL_ERROR, "decoding error" );
126                         rc = -1;
127                         goto done;
128                 }
129         }
130
131         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
132                 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
133                 return rc;
134         } 
135
136         Debug( LDAP_DEBUG_ARGS, "do_extended: oid \"%s\"\n", reqoid, 0 ,0 );
137
138 #ifdef SLAPD_EXTERNAL_EXTENSIONS
139         cookie.conn = conn;
140         cookie.op = op;
141         rspoid = NULL;
142         rspdata.bv_len = 0;
143         rspdata.bv_val = NULL;
144         text = NULL;
145         rc = (ext->ext_main)(extensions_callback, &cookie, reqoid, &reqdata, &rspoid, &rspdata, &text);
146
147         send_ldap_extended(conn, op, rc, NULL, text, rspoid, rspdata.bv_val ? &rspdata : NULL);
148
149         if (rspoid != NULL)
150                 free(rspoid);
151         if ( rspdata.bv_val != NULL )
152                 free(rspdata.bv_val);
153         if ( text != NULL )
154                 free(text);
155
156 #else
157         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
158                 NULL, "unsupported extended operation", NULL, NULL );
159
160 #endif
161
162 done:
163         if ( reqoid != NULL ) {
164                 free( reqoid );
165         }
166         if ( reqdata.bv_val != NULL ) {
167                 free( reqdata.bv_val );
168         }
169
170         return rc;
171 }
172
173 #ifdef SLAPD_EXTERNAL_EXTENSIONS
174
175 int
176 load_extension (
177         const void *module,
178         const char *file_name
179 )
180 {
181         extensions_list_t *ext;
182         int (*ext_getoid)(int index, char *oid, int blen);
183         int rc;
184
185         ext = ch_calloc(1, sizeof(extensions_list_t));
186         if (ext == NULL)
187                 return(-1);
188
189         ext->oid = ch_malloc(MAX_OID_LENGTH);
190         if (ext->oid == NULL) {
191                 free(ext);
192                 return(-1);
193         }
194
195         ext->ext_main = module_resolve(module, "ext_main");
196         if (ext->ext_main == NULL) {
197                 free(ext->oid);
198                 free(ext);
199                 return(-1);
200         }
201
202         ext_getoid = module_resolve(module, "ext_getoid");
203         if (ext_getoid == NULL) {
204                 free(ext->oid);
205                 free(ext);
206                 return(-1);
207         }
208         rc = (ext_getoid)(0, ext->oid, MAX_OID_LENGTH);
209         if (rc != 0) {
210                 free(ext->oid);
211                 free(ext);
212                 return(rc);
213         }
214         if (*ext->oid == 0) {
215                 free(ext->oid);
216                 free(ext);
217                 return(-1);
218         }
219
220         /* supportedExtensions must be maintained for the root DSE.
221          * Unfortunately, this global var is declared extern char *[],
222          * which means it cannot grow dynamically.  So, for now it is
223          * a char *[n], and only (n-1) oids are tracked.  In the off
224          * chance that this is too few, the extensions will still be
225          * loaded, but not reported in root DSE info.  To increase
226          * the maximum, change MAX_SUPP_EXT_TRACKED and recompile or
227          * fix root_dse.c to use something other than a static array.
228          */
229         if (supp_ext_count < (MAX_SUPP_EXT_TRACKED - 1)) {
230                 supportedExtensions[supp_ext_count++] = ch_strdup(ext->oid);
231                 supportedExtensions[supp_ext_count] = NULL;
232         }
233
234         ext->next = supp_ext_list;
235         supp_ext_list = ext;
236         return(0);
237 }
238
239 extensions_list_t *
240 find_extension (extensions_list_t *list, char *oid)
241 {
242         extensions_list_t *ext;
243
244         for (ext = list; ext; ext = ext->next) {
245                 if (strcmp(ext->oid, oid) == 0)
246                         return(ext);
247         }
248         return(NULL);
249 }
250
251 int
252 extensions_callback (extensions_cookie_t *cookie, int msg, int arg, void *argp)
253 {
254         if (cookie == NULL)
255                 return(-1);
256
257         if (argp == NULL)
258                 return(-1);
259
260         switch (msg) {
261         case 0:         /* SLAPD_EXT_GETVERSION */
262                 *(int *)argp = 1;
263                 return(0);
264
265         case 1:         /* SLAPD_EXT_GETPROTO */
266                 *(int *)argp = cookie->op->o_protocol;
267                 return(0);
268         
269         case 2:         /* SLAPD_EXT_GETAUTH */
270                 *(int *)argp = cookie->op->o_authtype;
271                 return(0);
272         
273         case 3:         /* SLAPD_EXT_GETDN */
274                 *(char **)argp = cookie->op->o_dn;
275                 return(0);
276         
277         case 4:         /* SLAPD_EXT_GETCLIENT */
278                 if (cookie->conn->c_peer_domain != NULL && *cookie->conn->c_peer_domain != 0) {
279                         *(char **)argp = cookie->conn->c_peer_domain;
280                         return(0);
281                 }
282                 if (cookie->conn->c_peer_name != NULL && *cookie->conn->c_peer_name != 0) {
283                         *(char **)argp = cookie->conn->c_peer_name;
284                         return(0);
285                 }
286                 break;
287         
288         default:
289                 break;
290         }
291         return(-1);
292 }
293
294 #endif
295