]> git.sur5r.net Git - openldap/blob - servers/slapd/extended.c
ACIs almost entirely factored out of slapd
[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 #ifdef HAVE_TLS
73         { &slap_EXOP_START_TLS, 0, starttls_extop },
74 #endif
75         { NULL, 0, NULL }
76 };
77
78
79 static struct extop_list *find_extop(
80         struct extop_list *list, struct berval *oid );
81
82 struct berval *
83 get_supported_extop (int index)
84 {
85         struct extop_list *ext;
86
87         /* linear scan is slow, but this way doesn't force a
88          * big change on root_dse.c, where this routine is used.
89          */
90         for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) {
91                 ; /* empty */
92         }
93
94         if (ext == NULL) return NULL;
95
96         return &ext->oid;
97 }
98
99
100 int exop_root_dse_info( Entry *e )
101 {
102         AttributeDescription *ad_supportedExtension
103                 = slap_schema.si_ad_supportedExtension;
104         struct berval vals[2];
105         struct extop_list *ext;
106
107         vals[1].bv_val = NULL;
108         vals[1].bv_len = 0;
109
110         for (ext = supp_ext_list; ext != NULL; ext = ext->next) {
111                 if( ext->flags & SLAP_EXOP_HIDE ) continue;
112
113                 vals[0] = ext->oid;
114
115                 if( attr_merge( e, ad_supportedExtension, vals, NULL ) ) {
116                         return LDAP_OTHER;
117                 }
118         }
119
120         return LDAP_SUCCESS;
121 }
122
123 int
124 do_extended(
125     Operation   *op,
126     SlapReply   *rs
127 )
128 {
129         struct berval reqdata = {0, NULL};
130         ber_tag_t tag;
131         ber_len_t len;
132
133         Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
134
135         if( op->o_protocol < LDAP_VERSION3 ) {
136                 Debug( LDAP_DEBUG_ANY,
137                         "do_extended: protocol version (%d) too low\n",
138                         op->o_protocol, 0 ,0 );
139                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
140                 rs->sr_err = SLAPD_DISCONNECT;
141                 goto done;
142         }
143
144         if ( ber_scanf( op->o_ber, "{m" /*}*/, &op->ore_reqoid ) == LBER_ERROR ) {
145                 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
146                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
147                 rs->sr_err = SLAPD_DISCONNECT;
148                 goto done;
149         }
150
151         tag = ber_peek_tag( op->o_ber, &len );
152         
153         if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
154                 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) {
155                         Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
156                         send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
157                         rs->sr_err = SLAPD_DISCONNECT;
158                         goto done;
159                 }
160         }
161
162         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
163                 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
164                 return rs->sr_err;
165         } 
166
167         /* check for controls inappropriate for all extended operations */
168         if( get_manageDSAit( op ) == SLAP_CONTROL_CRITICAL ) {
169                 Statslog( LDAP_DEBUG_STATS, "%s EXT oid=%s\n",
170                     op->o_log_prefix, op->ore_reqoid.bv_val, 0, 0, 0 );
171                 send_ldap_error( op, rs,
172                         LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
173                         "manageDSAit control inappropriate" );
174                 goto done;
175         }
176
177         /* FIXME: temporary? */
178         if ( reqdata.bv_val ) {
179                 op->ore_reqdata = &reqdata;
180         }
181
182         op->o_bd = frontendDB;
183         rs->sr_err = frontendDB->be_extended( op, rs );
184
185 done:
186         return rs->sr_err;
187 }
188
189 int
190 fe_extended( Operation *op, SlapReply *rs )
191 {
192         struct extop_list       *ext = NULL;
193         struct berval           reqdata = BER_BVNULL;
194
195         if (op->ore_reqdata) {
196                 reqdata = *op->ore_reqdata;
197         }
198
199         if( !(ext = find_extop(supp_ext_list, &op->ore_reqoid )))
200         {
201                 Statslog( LDAP_DEBUG_STATS, "%s EXT oid=%s\n",
202                     op->o_log_prefix, op->ore_reqoid.bv_val, 0, 0, 0 );
203                 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
204                         op->ore_reqoid.bv_val, 0 ,0 );
205                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
206                         "unsupported extended operation" );
207                 goto done;
208         }
209
210         op->ore_flags = ext->flags;
211
212         Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n",
213                 op->ore_reqoid.bv_val, 0 ,0 );
214
215         { /* start of OpenLDAP extended operation */
216                 rs->sr_err = (ext->ext_main)( op, rs );
217
218                 if( rs->sr_err != SLAPD_ABANDON ) {
219                         if ( rs->sr_err == LDAP_REFERRAL && rs->sr_ref == NULL ) {
220                                 rs->sr_ref = referral_rewrite( default_referral,
221                                         NULL, NULL, LDAP_SCOPE_DEFAULT );
222                                 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
223                                 if ( !rs->sr_ref ) {
224                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
225                                         rs->sr_text = "referral missing";
226                                 }
227                         }
228
229                         if ( op->o_bd == NULL )
230                                 op->o_bd = frontendDB;
231                         send_ldap_extended( op, rs );
232
233                         if ( rs->sr_ref != default_referral ) {
234                                 ber_bvarray_free( rs->sr_ref );
235                                 rs->sr_ref = NULL;
236                         }
237                 }
238
239                 if ( rs->sr_rspoid != NULL ) {
240                         free( (char *)rs->sr_rspoid );
241                 }
242
243                 if ( rs->sr_rspdata != NULL ) {
244                         ber_bvfree( rs->sr_rspdata );
245                 }
246         } /* end of OpenLDAP extended operation */
247
248 done:;
249         return rs->sr_err;
250 }
251
252 int
253 load_extop(
254         struct berval *ext_oid,
255         slap_mask_t ext_flags,
256         SLAP_EXTOP_MAIN_FN *ext_main )
257 {
258         struct extop_list *ext;
259
260         if( ext_oid == NULL || ext_oid->bv_val == NULL ||
261                 ext_oid->bv_val[0] == '\0' || ext_oid->bv_len == 0 ) return -1; 
262         if(!ext_main) return -1; 
263
264         ext = ch_calloc(1, sizeof(struct extop_list) + ext_oid->bv_len + 1);
265         if (ext == NULL)
266                 return(-1);
267
268         ext->flags = ext_flags;
269
270         ext->oid.bv_val = (char *)(ext + 1);
271         AC_MEMCPY( ext->oid.bv_val, ext_oid->bv_val, ext_oid->bv_len );
272         ext->oid.bv_len = ext_oid->bv_len;
273         ext->oid.bv_val[ext->oid.bv_len] = '\0';
274
275         ext->ext_main = ext_main;
276         ext->next = supp_ext_list;
277
278         supp_ext_list = ext;
279
280         return(0);
281 }
282
283 int
284 extops_init (void)
285 {
286         int i;
287
288         for (i = 0; builtin_extops[i].oid != NULL; i++) {
289                 load_extop((struct berval *)builtin_extops[i].oid,
290                         builtin_extops[i].flags,
291                         builtin_extops[i].ext_main);
292         }
293         return(0);
294 }
295
296 int
297 extops_kill (void)
298 {
299         struct extop_list *ext;
300
301         /* we allocated the memory, so we have to free it, too. */
302         while ((ext = supp_ext_list) != NULL) {
303                 supp_ext_list = ext->next;
304                 ch_free(ext);
305         }
306         return(0);
307 }
308
309 static struct extop_list *
310 find_extop( struct extop_list *list, struct berval *oid )
311 {
312         struct extop_list *ext;
313
314         for (ext = list; ext; ext = ext->next) {
315                 if (bvmatch(&ext->oid, oid))
316                         return(ext);
317         }
318         return(NULL);
319 }
320
321
322 static int
323 whoami_extop (
324         Operation *op,
325         SlapReply *rs )
326 {
327         struct berval *bv;
328
329         if ( op->ore_reqdata != NULL ) {
330                 /* no request data should be provided */
331                 rs->sr_text = "no request data expected";
332                 return LDAP_PROTOCOL_ERROR;
333         }
334
335         Statslog( LDAP_DEBUG_STATS, "%s WHOAMI\n",
336             op->o_log_prefix, 0, 0, 0, 0 );
337
338         op->o_bd = op->o_conn->c_authz_backend;
339         if( backend_check_restrictions( op, rs,
340                 (struct berval *)&slap_EXOP_WHOAMI ) != LDAP_SUCCESS ) {
341                 return rs->sr_err;
342         }
343
344         bv = (struct berval *) ch_malloc( sizeof(struct berval) );
345         if( op->o_dn.bv_len ) {
346                 bv->bv_len = op->o_dn.bv_len + STRLENOF( "dn:" );
347                 bv->bv_val = ch_malloc( bv->bv_len + 1 );
348                 AC_MEMCPY( bv->bv_val, "dn:", STRLENOF( "dn:" ) );
349                 AC_MEMCPY( &bv->bv_val[STRLENOF( "dn:" )], op->o_dn.bv_val,
350                         op->o_dn.bv_len );
351                 bv->bv_val[bv->bv_len] = '\0';
352
353         } else {
354                 bv->bv_len = 0;
355                 bv->bv_val = NULL;
356         }
357
358         rs->sr_rspdata = bv;
359         return LDAP_SUCCESS;
360 }