]> git.sur5r.net Git - openldap/blob - servers/slapd/extended.c
Switch to openldap-data
[openldap] / servers / slapd / extended.c
1 /* $OpenLDAP$ */
2 /* 
3  * Copyright 1999-2002 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 #include <ac/string.h>
33
34 #include "slap.h"
35
36 static struct extop_list {
37         struct extop_list *next;
38         char *oid;
39         SLAP_EXTOP_MAIN_FN *ext_main;
40 } *supp_ext_list = NULL;
41
42 static SLAP_EXTOP_MAIN_FN whoami_extop;
43
44 /* this list of built-in extops is for extops that are not part
45  * of backends or in external modules.  essentially, this is
46  * just a way to get built-in extops onto the extop list without
47  * having a separate init routine for each built-in extop.
48  */
49 static struct {
50         char *oid;
51         SLAP_EXTOP_MAIN_FN *ext_main;
52 } builtin_extops[] = {
53 #ifdef HAVE_TLS
54         { LDAP_EXOP_START_TLS, starttls_extop },
55 #endif
56         { LDAP_EXOP_MODIFY_PASSWD, passwd_extop },
57         { LDAP_EXOP_X_WHO_AM_I, whoami_extop },
58         { NULL, NULL }
59 };
60
61
62 static struct extop_list *find_extop(
63         struct extop_list *list, char *oid );
64
65 char *
66 get_supported_extop (int index)
67 {
68         struct extop_list *ext;
69
70         /* linear scan is slow, but this way doesn't force a
71          * big change on root_dse.c, where this routine is used.
72          */
73         for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) {
74                 ; /* empty */
75         }
76
77         if (ext == NULL) return NULL;
78
79         return ext->oid ;
80 }
81
82 int
83 do_extended(
84     Connection  *conn,
85     Operation   *op
86 )
87 {
88         int rc = LDAP_SUCCESS;
89         char* reqoid;
90         struct berval *reqdata;
91         ber_tag_t tag;
92         ber_len_t len;
93         struct extop_list *ext;
94         const char *text;
95         BerVarray refs;
96         char *rspoid;
97         struct berval *rspdata;
98         LDAPControl **rspctrls;
99
100 #ifdef NEW_LOGGING
101         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
102                 "do_extended: conn %d\n", conn->c_connid ));
103 #else
104         Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
105 #endif
106         reqoid = NULL;
107         reqdata = NULL;
108
109         if( op->o_protocol < LDAP_VERSION3 ) {
110 #ifdef NEW_LOGGING
111                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
112                         "do_extended: protocol version (%d) too low.\n",
113                         op->o_protocol ));
114 #else
115                 Debug( LDAP_DEBUG_ANY,
116                         "do_extended: protocol version (%d) too low\n",
117                         op->o_protocol, 0 ,0 );
118 #endif
119                 send_ldap_disconnect( conn, op,
120                         LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
121                 rc = -1;
122                 goto done;
123         }
124
125         if ( ber_scanf( op->o_ber, "{a" /*}*/, &reqoid ) == LBER_ERROR ) {
126 #ifdef NEW_LOGGING
127                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
128                         "do_extended: conn %d  ber_scanf failed\n", conn->c_connid ));
129 #else
130                 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
131 #endif
132                 send_ldap_disconnect( conn, op,
133                         LDAP_PROTOCOL_ERROR, "decoding error" );
134                 rc = -1;
135                 goto done;
136         }
137
138         if( !(ext = find_extop(supp_ext_list, reqoid)) ) {
139 #ifdef NEW_LOGGING
140                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
141                         "do_extended: conn %d  unsupported operation \"%s\"\n",
142                         conn->c_connid, reqoid ));
143 #else
144                 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
145                         reqoid, 0 ,0 );
146 #endif
147                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
148                         NULL, "unsupported extended operation", NULL, NULL );
149                 goto done;
150         }
151
152         tag = ber_peek_tag( op->o_ber, &len );
153         
154         if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
155                 if( ber_scanf( op->o_ber, "O", &reqdata ) == LBER_ERROR ) {
156 #ifdef NEW_LOGGING
157                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
158                                 "do_extended: conn %d  ber_scanf failed\n", conn->c_connid ));
159 #else
160                         Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
161 #endif
162                         send_ldap_disconnect( conn, op,
163                                 LDAP_PROTOCOL_ERROR, "decoding error" );
164                         rc = -1;
165                         goto done;
166                 }
167         }
168
169         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
170 #ifdef NEW_LOGGING
171                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
172                         "do_extended: conn %d  get_ctrls failed\n", conn->c_connid ));
173 #else
174                 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
175 #endif
176                 return rc;
177         } 
178
179         /* check for controls inappropriate for all extended operations */
180         if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
181                 send_ldap_result( conn, op,
182                         rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
183                         NULL, "manageDSAit control inappropriate",
184                         NULL, NULL );
185                 goto done;
186         }
187
188 #ifdef NEW_LOGGING
189         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
190                 "do_extended: conn %d  oid=%d\n.", conn->c_connid, reqoid ));
191 #else
192         Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", reqoid, 0 ,0 );
193 #endif
194
195         rspoid = NULL;
196         rspdata = NULL;
197         rspctrls = NULL;
198         text = NULL;
199         refs = NULL;
200
201         rc = (ext->ext_main)( conn, op,
202                 reqoid, reqdata,
203                 &rspoid, &rspdata, &rspctrls, &text, &refs );
204
205         if( rc != SLAPD_ABANDON ) {
206                 if ( rc == LDAP_REFERRAL && refs == NULL ) {
207                         refs = referral_rewrite( default_referral,
208                                 NULL, NULL, LDAP_SCOPE_DEFAULT );
209                 }
210
211                 send_ldap_extended( conn, op, rc, NULL, text, refs,
212                         rspoid, rspdata, rspctrls );
213
214                 ber_bvarray_free( refs );
215         }
216
217         if ( rspoid != NULL ) {
218                 free( rspoid );
219         }
220
221         if ( rspdata != NULL ) {
222                 ber_bvfree( rspdata );
223         }
224
225 done:
226         if ( reqdata != NULL ) {
227                 ber_bvfree( reqdata );
228         }
229         if ( reqoid != NULL ) {
230                 free( reqoid );
231         }
232
233         return rc;
234 }
235
236 int
237 load_extop(
238         const char *ext_oid,
239         SLAP_EXTOP_MAIN_FN *ext_main )
240 {
241         struct extop_list *ext;
242
243         if( ext_oid == NULL || *ext_oid == '\0' ) return -1; 
244         if(!ext_main) return -1; 
245
246         ext = ch_calloc(1, sizeof(struct extop_list));
247         if (ext == NULL)
248                 return(-1);
249
250         ext->oid = ch_strdup( ext_oid );
251         if (ext->oid == NULL) {
252                 free(ext);
253                 return(-1);
254         }
255
256         ext->ext_main = ext_main;
257         ext->next = supp_ext_list;
258
259         supp_ext_list = ext;
260
261         return(0);
262 }
263
264 int
265 extops_init (void)
266 {
267         int i;
268
269         for (i = 0; builtin_extops[i].oid != NULL; i++) {
270                 load_extop(builtin_extops[i].oid, builtin_extops[i].ext_main);
271         }
272         return(0);
273 }
274
275 int
276 extops_kill (void)
277 {
278         struct extop_list *ext;
279
280         /* we allocated the memory, so we have to free it, too. */
281         while ((ext = supp_ext_list) != NULL) {
282                 supp_ext_list = ext->next;
283                 if (ext->oid != NULL)
284                         ch_free(ext->oid);
285                 ch_free(ext);
286         }
287         return(0);
288 }
289
290 static struct extop_list *
291 find_extop( struct extop_list *list, char *oid )
292 {
293         struct extop_list *ext;
294
295         for (ext = list; ext; ext = ext->next) {
296                 if (strcmp(ext->oid, oid) == 0)
297                         return(ext);
298         }
299         return(NULL);
300 }
301
302
303 int
304 whoami_extop (
305         Connection *conn,
306         Operation *op,
307         const char * reqoid,
308         struct berval * reqdata,
309         char ** rspoid,
310         struct berval ** rspdata,
311         LDAPControl ***rspctrls,
312         const char ** text,
313         BerVarray * refs )
314 {
315         struct berval *bv;
316
317         if ( reqdata != NULL ) {
318                 /* no request data should be provided */
319                 *text = "no request data expected";
320                 return LDAP_PROTOCOL_ERROR;
321         }
322
323         bv = (struct berval *) ch_malloc( sizeof(struct berval) );
324         if( op->o_dn.bv_len ) {
325                 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
326                 bv->bv_val = ch_malloc( bv->bv_len + 1 );
327                 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
328                 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
329                         op->o_dn.bv_len );
330                 bv->bv_val[bv->bv_len] = '\0';
331
332         } else {
333                 bv->bv_len = 0;
334                 bv->bv_val = NULL;
335         }
336
337         *rspdata = bv;
338         return LDAP_SUCCESS;
339 }