]> git.sur5r.net Git - openldap/blob - servers/slapd/extended.c
Sync with HEAD
[openldap] / servers / slapd / extended.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 The OpenLDAP Foundation.
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
16 /*
17  * LDAPv3 Extended Operation Request
18  *      ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
19  *              requestName      [0] LDAPOID,
20  *              requestValue     [1] OCTET STRING OPTIONAL
21  *      }
22  *
23  * LDAPv3 Extended Operation Response
24  *      ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
25  *              COMPONENTS OF LDAPResult,
26  *              responseName     [10] LDAPOID OPTIONAL,
27  *              response         [11] OCTET STRING OPTIONAL
28  *      }
29  *
30  */
31
32 #include "portable.h"
33
34 #include <stdio.h>
35
36 #include <ac/socket.h>
37 #include <ac/string.h>
38
39 #include "slap.h"
40 #include "lber_pvt.h"
41
42 #define UNSUPPORTED_EXOP "unsupported extended operation"
43
44
45 static struct extop_list {
46         struct extop_list *next;
47         struct berval oid;
48         slap_mask_t flags;
49         SLAP_EXTOP_MAIN_FN *ext_main;
50 } *supp_ext_list = NULL;
51
52 static SLAP_EXTOP_MAIN_FN whoami_extop;
53
54 /* This list of built-in extops is for extops that are not part
55  * of backends or in external modules.  Essentially, this is
56  * just a way to get built-in extops onto the extop list without
57  * having a separate init routine for each built-in extop.
58  */
59 const struct berval slap_EXOP_CANCEL = BER_BVC(LDAP_EXOP_X_CANCEL);
60 const struct berval slap_EXOP_WHOAMI = BER_BVC(LDAP_EXOP_X_WHO_AM_I);
61 const struct berval slap_EXOP_MODIFY_PASSWD = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
62 const struct berval slap_EXOP_START_TLS = BER_BVC(LDAP_EXOP_START_TLS);
63
64 static struct {
65         const struct berval *oid;
66         slap_mask_t flags;
67         SLAP_EXTOP_MAIN_FN *ext_main;
68 } builtin_extops[] = {
69         { &slap_EXOP_CANCEL, SLAP_EXOP_HIDE, cancel_extop },
70         { &slap_EXOP_WHOAMI, 0, whoami_extop },
71         { &slap_EXOP_MODIFY_PASSWD, SLAP_EXOP_WRITES, passwd_extop },
72         { NULL, 0, NULL }
73 };
74
75
76 static struct extop_list *find_extop(
77         struct extop_list *list, struct berval *oid );
78
79 struct berval *
80 get_supported_extop (int index)
81 {
82         struct extop_list *ext;
83
84         /* linear scan is slow, but this way doesn't force a
85          * big change on root_dse.c, where this routine is used.
86          */
87         for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) {
88                 ; /* empty */
89         }
90
91         if (ext == NULL) return NULL;
92
93         return &ext->oid;
94 }
95
96
97 int exop_root_dse_info( Entry *e )
98 {
99         AttributeDescription *ad_supportedExtension
100                 = slap_schema.si_ad_supportedExtension;
101         struct berval vals[2];
102         struct extop_list *ext;
103
104         vals[1].bv_val = NULL;
105         vals[1].bv_len = 0;
106
107         for (ext = supp_ext_list; ext != NULL; ext = ext->next) {
108                 if( ext->flags & SLAP_EXOP_HIDE ) continue;
109
110                 vals[0] = ext->oid;
111
112                 if( attr_merge( e, ad_supportedExtension, vals, NULL ) ) {
113                         return LDAP_OTHER;
114                 }
115         }
116
117         return LDAP_SUCCESS;
118 }
119
120 int
121 do_extended(
122     Operation   *op,
123     SlapReply   *rs
124 )
125 {
126         struct berval reqdata = {0, NULL};
127         ber_tag_t tag;
128         ber_len_t len;
129
130         Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
131
132         if( op->o_protocol < LDAP_VERSION3 ) {
133                 Debug( LDAP_DEBUG_ANY,
134                         "do_extended: protocol version (%d) too low\n",
135                         op->o_protocol, 0 ,0 );
136                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
137                 rs->sr_err = SLAPD_DISCONNECT;
138                 goto done;
139         }
140
141         if ( ber_scanf( op->o_ber, "{m" /*}*/, &op->ore_reqoid ) == LBER_ERROR ) {
142                 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
143                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
144                 rs->sr_err = SLAPD_DISCONNECT;
145                 goto done;
146         }
147
148         tag = ber_peek_tag( op->o_ber, &len );
149         
150         if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
151                 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) {
152                         Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
153                         send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
154                         rs->sr_err = SLAPD_DISCONNECT;
155                         goto done;
156                 }
157         }
158
159         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
160                 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
161                 return rs->sr_err;
162         } 
163
164         /* check for controls inappropriate for all extended operations */
165         if( get_manageDSAit( op ) == SLAP_CONTROL_CRITICAL ) {
166                 Statslog( LDAP_DEBUG_STATS, "%s EXT oid=%s\n",
167                     op->o_log_prefix, op->ore_reqoid.bv_val, 0, 0, 0 );
168                 send_ldap_error( op, rs,
169                         LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
170                         "manageDSAit control inappropriate" );
171                 goto done;
172         }
173
174         /* FIXME: temporary? */
175         if ( reqdata.bv_val ) {
176                 op->ore_reqdata = &reqdata;
177         }
178
179         op->o_bd = frontendDB;
180         rs->sr_err = frontendDB->be_extended( op, rs );
181
182 done:
183         return rs->sr_err;
184 }
185
186 int
187 fe_extended( Operation *op, SlapReply *rs )
188 {
189         struct extop_list       *ext = NULL;
190         struct berval           reqdata = BER_BVNULL;
191
192         if (op->ore_reqdata) {
193                 reqdata = *op->ore_reqdata;
194         }
195
196         if( !(ext = find_extop(supp_ext_list, &op->ore_reqoid )))
197         {
198                 Statslog( LDAP_DEBUG_STATS, "%s EXT oid=%s\n",
199                     op->o_log_prefix, op->ore_reqoid.bv_val, 0, 0, 0 );
200                 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
201                         op->ore_reqoid.bv_val, 0 ,0 );
202                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
203                         "unsupported extended operation" );
204                 goto done;
205         }
206
207         op->ore_flags = ext->flags;
208
209         Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n",
210                 op->ore_reqoid.bv_val, 0 ,0 );
211
212         { /* start of OpenLDAP extended operation */
213                 rs->sr_err = (ext->ext_main)( op, rs );
214
215                 if( rs->sr_err != SLAPD_ABANDON ) {
216                         if ( rs->sr_err == LDAP_REFERRAL && rs->sr_ref == NULL ) {
217                                 rs->sr_ref = referral_rewrite( default_referral,
218                                         NULL, NULL, LDAP_SCOPE_DEFAULT );
219                                 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
220                                 if ( !rs->sr_ref ) {
221                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
222                                         rs->sr_text = "referral missing";
223                                 }
224                         }
225
226                         if ( op->o_bd == NULL )
227                                 op->o_bd = frontendDB;
228                         send_ldap_extended( op, rs );
229
230                         if ( rs->sr_ref != default_referral ) {
231                                 ber_bvarray_free( rs->sr_ref );
232                                 rs->sr_ref = NULL;
233                         }
234                 }
235
236                 if ( rs->sr_rspoid != NULL ) {
237                         free( (char *)rs->sr_rspoid );
238                 }
239
240                 if ( rs->sr_rspdata != NULL ) {
241                         ber_bvfree( rs->sr_rspdata );
242                 }
243         } /* end of OpenLDAP extended operation */
244
245 done:;
246         return rs->sr_err;
247 }
248
249 int
250 load_extop(
251         struct berval *ext_oid,
252         slap_mask_t ext_flags,
253         SLAP_EXTOP_MAIN_FN *ext_main )
254 {
255         struct extop_list *ext;
256
257         if( ext_oid == NULL || ext_oid->bv_val == NULL ||
258                 ext_oid->bv_val[0] == '\0' || ext_oid->bv_len == 0 ) return -1; 
259         if(!ext_main) return -1; 
260
261         ext = ch_calloc(1, sizeof(struct extop_list) + ext_oid->bv_len + 1);
262         if (ext == NULL)
263                 return(-1);
264
265         ext->flags = ext_flags;
266
267         ext->oid.bv_val = (char *)(ext + 1);
268         AC_MEMCPY( ext->oid.bv_val, ext_oid->bv_val, ext_oid->bv_len );
269         ext->oid.bv_len = ext_oid->bv_len;
270         ext->oid.bv_val[ext->oid.bv_len] = '\0';
271
272         ext->ext_main = ext_main;
273         ext->next = supp_ext_list;
274
275         supp_ext_list = ext;
276
277         return(0);
278 }
279
280 int
281 extops_init (void)
282 {
283         int i;
284
285         for (i = 0; builtin_extops[i].oid != NULL; i++) {
286                 load_extop((struct berval *)builtin_extops[i].oid,
287                         builtin_extops[i].flags,
288                         builtin_extops[i].ext_main);
289         }
290         return(0);
291 }
292
293 int
294 extops_kill (void)
295 {
296         struct extop_list *ext;
297
298         /* we allocated the memory, so we have to free it, too. */
299         while ((ext = supp_ext_list) != NULL) {
300                 supp_ext_list = ext->next;
301                 ch_free(ext);
302         }
303         return(0);
304 }
305
306 static struct extop_list *
307 find_extop( struct extop_list *list, struct berval *oid )
308 {
309         struct extop_list *ext;
310
311         for (ext = list; ext; ext = ext->next) {
312                 if (bvmatch(&ext->oid, oid))
313                         return(ext);
314         }
315         return(NULL);
316 }
317
318
319 static int
320 whoami_extop (
321         Operation *op,
322         SlapReply *rs )
323 {
324         struct berval *bv;
325
326         if ( op->ore_reqdata != NULL ) {
327                 /* no request data should be provided */
328                 rs->sr_text = "no request data expected";
329                 return LDAP_PROTOCOL_ERROR;
330         }
331
332         Statslog( LDAP_DEBUG_STATS, "%s WHOAMI\n",
333             op->o_log_prefix, 0, 0, 0, 0 );
334
335         op->o_bd = op->o_conn->c_authz_backend;
336         if( backend_check_restrictions( op, rs,
337                 (struct berval *)&slap_EXOP_WHOAMI ) != LDAP_SUCCESS ) {
338                 return rs->sr_err;
339         }
340
341         bv = (struct berval *) ch_malloc( sizeof(struct berval) );
342         if( op->o_dn.bv_len ) {
343                 bv->bv_len = op->o_dn.bv_len + STRLENOF( "dn:" );
344                 bv->bv_val = ch_malloc( bv->bv_len + 1 );
345                 AC_MEMCPY( bv->bv_val, "dn:", STRLENOF( "dn:" ) );
346                 AC_MEMCPY( &bv->bv_val[STRLENOF( "dn:" )], op->o_dn.bv_val,
347                         op->o_dn.bv_len );
348                 bv->bv_val[bv->bv_len] = '\0';
349
350         } else {
351                 bv->bv_len = 0;
352                 bv->bv_val = NULL;
353         }
354
355         rs->sr_rspdata = bv;
356         return LDAP_SUCCESS;
357 }