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