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