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