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