]> git.sur5r.net Git - openldap/blob - servers/slapd/extended.c
935fcdc8a8ffb41c4ac8e96dfe9785acf7bbfa6b
[openldap] / servers / slapd / extended.c
1 /* $OpenLDAP$ */
2 /* 
3  * Copyright 1999-2003 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
32 #include <ac/socket.h>
33 #include <ac/string.h>
34
35 #include "slap.h"
36 #include "lber_pvt.h"
37
38 #ifdef LDAP_SLAPI
39 #include "slapi.h"
40 #endif
41
42 #define UNSUPPORTED_EXTENDEDOP "unsupported extended operation"
43
44 static struct extop_list {
45         struct extop_list *next;
46         struct berval oid;
47         SLAP_EXTOP_MAIN_FN *ext_main;
48 } *supp_ext_list = NULL;
49
50 static SLAP_EXTOP_MAIN_FN whoami_extop;
51
52 /* this list of built-in extops is for extops that are not part
53  * of backends or in external modules.  essentially, this is
54  * just a way to get built-in extops onto the extop list without
55  * having a separate init routine for each built-in extop.
56  */
57 #ifdef LDAP_EXOP_X_CANCEL
58 const struct berval slap_EXOP_CANCEL = BER_BVC(LDAP_EXOP_X_CANCEL);
59 #endif
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_EXTOP_MAIN_FN *ext_main;
67 } builtin_extops[] = {
68 #ifdef LDAP_EXOP_X_CANCEL
69         { &slap_EXOP_CANCEL, cancel_extop },
70 #endif
71         { &slap_EXOP_WHOAMI, whoami_extop },
72         { &slap_EXOP_MODIFY_PASSWD, passwd_extop },
73 #ifdef HAVE_TLS
74         { &slap_EXOP_START_TLS, starttls_extop },
75 #endif
76         { NULL, NULL }
77 };
78
79
80 static struct extop_list *find_extop(
81         struct extop_list *list, struct berval *oid );
82
83 struct berval *
84 get_supported_extop (int index)
85 {
86         struct extop_list *ext;
87
88         /* linear scan is slow, but this way doesn't force a
89          * big change on root_dse.c, where this routine is used.
90          */
91         for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) {
92                 ; /* empty */
93         }
94
95         if (ext == NULL) return NULL;
96
97         return &ext->oid ;
98 }
99
100 int
101 do_extended(
102     Operation   *op,
103     SlapReply   *rs
104 )
105 {
106         struct berval reqdata = {0, NULL};
107         ber_tag_t tag;
108         ber_len_t len;
109         struct extop_list *ext = NULL;
110
111 #if defined(LDAP_SLAPI) 
112         Slapi_PBlock    *pb = op->o_pb;
113         SLAPI_FUNC      funcAddr = NULL;
114         int             extop_rc;
115         int             msg_sent = FALSE;
116 #endif /* defined(LDAP_SLAPI) */
117
118 #ifdef NEW_LOGGING
119         LDAP_LOG( OPERATION, ENTRY, "do_extended: conn %d\n", op->o_connid, 0, 0 );
120 #else
121         Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
122 #endif
123
124         if( op->o_protocol < LDAP_VERSION3 ) {
125 #ifdef NEW_LOGGING
126                 LDAP_LOG( OPERATION, ERR, 
127                         "do_extended: protocol version (%d) too low.\n", op->o_protocol, 0, 0 );
128 #else
129                 Debug( LDAP_DEBUG_ANY,
130                         "do_extended: protocol version (%d) too low\n",
131                         op->o_protocol, 0 ,0 );
132 #endif
133                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
134                 rs->sr_err = -1;
135                 goto done;
136         }
137
138         if ( ber_scanf( op->o_ber, "{m" /*}*/, &op->oq_extended.rs_reqoid ) == LBER_ERROR ) {
139 #ifdef NEW_LOGGING
140                 LDAP_LOG( OPERATION, ERR, "do_extended: conn %d  ber_scanf failed\n", 
141                         op->o_connid, 0, 0 );
142 #else
143                 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
144 #endif
145                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
146                 rs->sr_err = -1;
147                 goto done;
148         }
149
150 #ifdef LDAP_SLAPI
151         getPluginFunc( &op->oq_extended.rs_reqoid, &funcAddr ); /* NS-SLAPI extended operation */
152         if( !funcAddr && !(ext = find_extop(supp_ext_list, &op->oq_extended.rs_reqoid )))
153 #else
154         if( !(ext = find_extop(supp_ext_list, &op->oq_extended.rs_reqoid )))
155 #endif
156         {
157 #ifdef NEW_LOGGING
158                 LDAP_LOG( OPERATION, ERR, 
159                         "do_extended: conn %d  unsupported operation \"%s\"\n",
160                         op->o_connid, op->oq_extended.rs_reqoid.bv_val, 0 );
161 #else
162                 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
163                         op->oq_extended.rs_reqoid.bv_val, 0 ,0 );
164 #endif
165                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
166                         "unsupported extended operation" );
167                 goto done;
168         }
169
170         tag = ber_peek_tag( op->o_ber, &len );
171         
172         if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
173                 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) {
174 #ifdef NEW_LOGGING
175                         LDAP_LOG( OPERATION, ERR, 
176                                 "do_extended: conn %d  ber_scanf failed\n", 
177                                 op->o_connid, 0, 0 );
178 #else
179                         Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
180 #endif
181                         send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
182                         rs->sr_err = -1;
183                         goto done;
184                 }
185         }
186
187         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
188 #ifdef NEW_LOGGING
189                 LDAP_LOG( OPERATION, ERR, 
190                         "do_extended: conn %d  get_ctrls failed\n", op->o_connid, 0, 0 );
191 #else
192                 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
193 #endif
194                 return rs->sr_err;
195         } 
196
197         /* check for controls inappropriate for all extended operations */
198         if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
199                 send_ldap_error( op, rs,
200                         LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
201                         "manageDSAit control inappropriate" );
202                 goto done;
203         }
204
205 #ifdef NEW_LOGGING
206         LDAP_LOG( OPERATION, DETAIL1, 
207                 "do_extended: conn %d  oid=%s\n.", op->o_connid, op->oq_extended.rs_reqoid.bv_val, 0 );
208 #else
209         Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", op->oq_extended.rs_reqoid.bv_val, 0 ,0 );
210 #endif
211
212 #if defined(LDAP_SLAPI)
213         if (ext != NULL) { /* OpenLDAP extended operation */
214 #endif /* defined(LDAP_SLAPI) */
215
216                 if (reqdata.bv_val) op->oq_extended.rs_reqdata = &reqdata;
217                 rs->sr_err = (ext->ext_main)( op, rs );
218
219                 if( rs->sr_err != SLAPD_ABANDON ) {
220                         if ( rs->sr_err == LDAP_REFERRAL && rs->sr_ref == NULL ) {
221                                 rs->sr_ref = referral_rewrite( default_referral,
222                                         NULL, NULL, LDAP_SCOPE_DEFAULT );
223                         }
224
225                         send_ldap_extended( op, rs );
226
227                         ber_bvarray_free( rs->sr_ref );
228                 }
229
230                 if ( rs->sr_rspoid != NULL ) {
231                         free( (char *)rs->sr_rspoid );
232                 }
233
234                 if ( rs->sr_rspdata != NULL ) {
235                         ber_bvfree( rs->sr_rspdata );
236                 }
237
238 #if defined( LDAP_SLAPI )
239                 goto done;  /* end of OpenLDAP extended operation */
240
241         } else { /* start of Netscape extended operation */
242                 rs->sr_err = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_OID,
243                                 (void *)op->oq_extended.rs_reqoid.bv_val);
244                 if ( rs->sr_err != LDAP_SUCCESS ) {
245                         rs->sr_err = LDAP_OTHER;
246                         goto done;
247                 }
248
249                 rs->sr_err = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_VALUE,
250                                 (void *)&reqdata);
251                 if ( rs->sr_err != LDAP_SUCCESS ) {
252                         rs->sr_err = LDAP_OTHER;
253                         goto done;
254                 }
255
256                 rs->sr_err = slapi_x_pblock_set_operation( pb, op );
257                 if ( rs->sr_err != LDAP_SUCCESS ) {
258                         rs->sr_err = LDAP_OTHER;
259                         goto done;
260                 }
261
262                 extop_rc = (*funcAddr)( pb );
263                 if ( extop_rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
264                         msg_sent = TRUE;
265
266                 } else if ( extop_rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
267                         rs->sr_err = LDAP_PROTOCOL_ERROR;
268                         rs->sr_text = UNSUPPORTED_EXTENDEDOP;
269
270                 } else {
271                         rs->sr_err = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID,
272                                         &rs->sr_rspoid);
273                         if ( rs->sr_err != LDAP_SUCCESS ) {
274                                 goto done2;
275                         }
276
277                         rs->sr_err = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE,
278                                         &rs->sr_rspdata);
279                         if ( rs->sr_err != LDAP_SUCCESS ) {
280                                 goto done2;
281                         }
282
283                         rs->sr_err = extop_rc;
284                         send_ldap_extended( op, rs );
285                         msg_sent = TRUE;
286                 }
287
288 done2:;
289                 if ( rs->sr_err != LDAP_SUCCESS && msg_sent == FALSE ) {
290                         send_ldap_result( op, rs );
291                 }
292
293                 if ( rs->sr_rspoid != NULL ) {
294                         ch_free( (char *)rs->sr_rspoid );
295                 }
296
297                 if ( rs->sr_rspdata != NULL ) {
298                         ber_bvfree( rs->sr_rspdata );
299                 }
300
301         } /* end of Netscape extended operation */
302 #endif /* defined( LDAP_SLAPI ) */
303
304 done:
305         return rs->sr_err;
306 }
307
308 int
309 load_extop(
310         struct berval *ext_oid,
311         SLAP_EXTOP_MAIN_FN *ext_main )
312 {
313         struct extop_list *ext;
314
315         if( ext_oid == NULL || ext_oid->bv_val == NULL ||
316                 ext_oid->bv_val[0] == '\0' || ext_oid->bv_len == 0 ) return -1; 
317         if(!ext_main) return -1; 
318
319         ext = ch_calloc(1, sizeof(struct extop_list) + ext_oid->bv_len + 1);
320         if (ext == NULL)
321                 return(-1);
322
323         ext->oid.bv_val = (char *)(ext + 1);
324         AC_MEMCPY( ext->oid.bv_val, ext_oid->bv_val, ext_oid->bv_len );
325         ext->oid.bv_len = ext_oid->bv_len;
326         ext->oid.bv_val[ext->oid.bv_len] = '\0';
327
328         ext->ext_main = ext_main;
329         ext->next = supp_ext_list;
330
331         supp_ext_list = ext;
332
333         return(0);
334 }
335
336 int
337 extops_init (void)
338 {
339         int i;
340
341         for (i = 0; builtin_extops[i].oid != NULL; i++) {
342                 load_extop((struct berval *)builtin_extops[i].oid, builtin_extops[i].ext_main);
343         }
344         return(0);
345 }
346
347 int
348 extops_kill (void)
349 {
350         struct extop_list *ext;
351
352         /* we allocated the memory, so we have to free it, too. */
353         while ((ext = supp_ext_list) != NULL) {
354                 supp_ext_list = ext->next;
355                 ch_free(ext);
356         }
357         return(0);
358 }
359
360 static struct extop_list *
361 find_extop( struct extop_list *list, struct berval *oid )
362 {
363         struct extop_list *ext;
364
365         for (ext = list; ext; ext = ext->next) {
366                 if (bvmatch(&ext->oid, oid))
367                         return(ext);
368         }
369         return(NULL);
370 }
371
372
373 static int
374 whoami_extop (
375         Operation *op,
376         SlapReply *rs )
377 {
378         struct berval *bv;
379
380         if ( op->oq_extended.rs_reqdata != NULL ) {
381                 /* no request data should be provided */
382                 rs->sr_text = "no request data expected";
383                 return LDAP_PROTOCOL_ERROR;
384         }
385
386         op->o_bd = op->o_conn->c_authz_backend;
387         if( backend_check_restrictions( op, rs,
388                 (struct berval *)&slap_EXOP_WHOAMI ) != LDAP_SUCCESS ) {
389                 return rs->sr_err;
390         }
391
392         bv = (struct berval *) ch_malloc( sizeof(struct berval) );
393         if( op->o_dn.bv_len ) {
394                 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
395                 bv->bv_val = ch_malloc( bv->bv_len + 1 );
396                 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
397                 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
398                         op->o_dn.bv_len );
399                 bv->bv_val[bv->bv_len] = '\0';
400
401         } else {
402                 bv->bv_len = 0;
403                 bv->bv_val = NULL;
404         }
405
406         rs->sr_rspdata = bv;
407         return LDAP_SUCCESS;
408 }