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