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