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