]> git.sur5r.net Git - openldap/blob - servers/slapd/extended.c
Fix previous commit
[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
30 #include <stdio.h>
31 #include <ac/socket.h>
32
33 #include "slap.h"
34
35 static struct extop_list {
36         struct extop_list *next;
37         char *oid;
38         SLAP_EXTOP_MAIN_FN *ext_main;
39 } *supp_ext_list = NULL;
40
41 /* this list of built-in extops is for extops that are not part
42  * of backends or in external modules.  essentially, this is
43  * just a way to get built-in extops onto the extop list without
44  * having a separate init routine for each built-in extop.
45  */
46 static struct {
47         char *oid;
48         SLAP_EXTOP_MAIN_FN *ext_main;
49 } builtin_extops[] = {
50 #ifdef HAVE_TLS
51         { LDAP_EXOP_START_TLS, starttls_extop },
52 #endif
53         { LDAP_EXOP_X_MODIFY_PASSWD, passwd_extop },
54         { NULL, NULL }
55 };
56
57
58 static struct extop_list *find_extop(
59         struct extop_list *list, char *oid );
60
61 char *
62 get_supported_extop (int index)
63 {
64         struct extop_list *ext;
65
66         /* linear scan is slow, but this way doesn't force a
67          * big change on root_dse.c, where this routine is used.
68          */
69         for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) {
70                 ; /* empty */
71         }
72
73         if (ext == NULL) return NULL;
74
75         return ext->oid ;
76 }
77
78 int
79 do_extended(
80     Connection  *conn,
81     Operation   *op
82 )
83 {
84         int rc = LDAP_SUCCESS;
85         char* reqoid;
86         struct berval *reqdata;
87         ber_tag_t tag;
88         ber_len_t len;
89         struct extop_list *ext;
90         const char *text;
91         BerVarray refs;
92         char *rspoid;
93         struct berval *rspdata;
94         LDAPControl **rspctrls;
95
96 #ifdef NEW_LOGGING
97         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
98                 "do_extended: conn %d\n", conn->c_connid ));
99 #else
100         Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
101 #endif
102         reqoid = NULL;
103         reqdata = NULL;
104
105         if( op->o_protocol < LDAP_VERSION3 ) {
106 #ifdef NEW_LOGGING
107                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
108                         "do_extended: protocol version (%d) too low.\n",
109                         op->o_protocol ));
110 #else
111                 Debug( LDAP_DEBUG_ANY,
112                         "do_extended: protocol version (%d) too low\n",
113                         op->o_protocol, 0 ,0 );
114 #endif
115                 send_ldap_disconnect( conn, op,
116                         LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
117                 rc = -1;
118                 goto done;
119         }
120
121         if ( ber_scanf( op->o_ber, "{a" /*}*/, &reqoid ) == LBER_ERROR ) {
122 #ifdef NEW_LOGGING
123                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
124                         "do_extended: conn %d  ber_scanf failed\n", conn->c_connid ));
125 #else
126                 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
127 #endif
128                 send_ldap_disconnect( conn, op,
129                         LDAP_PROTOCOL_ERROR, "decoding error" );
130                 rc = -1;
131                 goto done;
132         }
133
134         if( !(ext = find_extop(supp_ext_list, reqoid)) ) {
135 #ifdef NEW_LOGGING
136                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
137                         "do_extended: conn %d  unsupported operation \"%s\"\n",
138                         conn->c_connid, reqoid ));
139 #else
140                 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
141                         reqoid, 0 ,0 );
142 #endif
143                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
144                         NULL, "unsupported extended operation", NULL, NULL );
145                 goto done;
146         }
147
148         tag = ber_peek_tag( op->o_ber, &len );
149         
150         if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
151                 if( ber_scanf( op->o_ber, "O", &reqdata ) == LBER_ERROR ) {
152 #ifdef NEW_LOGGING
153                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
154                                 "do_extended: conn %d  ber_scanf failed\n", conn->c_connid ));
155 #else
156                         Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
157 #endif
158                         send_ldap_disconnect( conn, op,
159                                 LDAP_PROTOCOL_ERROR, "decoding error" );
160                         rc = -1;
161                         goto done;
162                 }
163         }
164
165         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
166 #ifdef NEW_LOGGING
167                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
168                         "do_extended: conn %d  get_ctrls failed\n", conn->c_connid ));
169 #else
170                 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
171 #endif
172                 return rc;
173         } 
174
175         /* check for controls inappropriate for all extended operations */
176         if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
177                 send_ldap_result( conn, op,
178                         rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
179                         NULL, "manageDSAit control inappropriate",
180                         NULL, NULL );
181                 goto done;
182         }
183
184 #ifdef NEW_LOGGING
185         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
186                 "do_extended: conn %d  oid=%d\n.", conn->c_connid, reqoid ));
187 #else
188         Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", reqoid, 0 ,0 );
189 #endif
190
191         rspoid = NULL;
192         rspdata = NULL;
193         rspctrls = NULL;
194         text = NULL;
195         refs = NULL;
196
197         rc = (ext->ext_main)( conn, op,
198                 reqoid, reqdata,
199                 &rspoid, &rspdata, &rspctrls, &text, &refs );
200
201         if( rc != SLAPD_ABANDON ) {
202                 if ( rc == LDAP_REFERRAL && refs == NULL ) {
203                         refs = referral_rewrite( default_referral,
204                                 NULL, NULL, LDAP_SCOPE_DEFAULT );
205                 }
206
207                 send_ldap_extended( conn, op, rc, NULL, text, refs,
208                         rspoid, rspdata, rspctrls );
209
210                 ber_bvarray_free( refs );
211         }
212
213         if ( rspoid != NULL ) {
214                 free( rspoid );
215         }
216
217         if ( rspdata != NULL )
218                 ber_bvfree( rspdata );
219
220 done:
221         if ( reqdata != NULL ) {
222                 ber_bvfree( reqdata );
223         }
224         if ( reqoid != NULL ) {
225                 free( reqoid );
226         }
227
228         return rc;
229 }
230
231 int
232 load_extop(
233         const char *ext_oid,
234         SLAP_EXTOP_MAIN_FN *ext_main )
235 {
236         struct extop_list *ext;
237
238         if( ext_oid == NULL || *ext_oid == '\0' ) return -1; 
239         if(!ext_main) return -1; 
240
241         ext = ch_calloc(1, sizeof(struct extop_list));
242         if (ext == NULL)
243                 return(-1);
244
245         ext->oid = ch_strdup( ext_oid );
246         if (ext->oid == NULL) {
247                 free(ext);
248                 return(-1);
249         }
250
251         ext->ext_main = ext_main;
252         ext->next = supp_ext_list;
253
254         supp_ext_list = ext;
255
256         return(0);
257 }
258
259 int
260 extops_init (void)
261 {
262         int i;
263
264         for (i = 0; builtin_extops[i].oid != NULL; i++) {
265                 load_extop(builtin_extops[i].oid, builtin_extops[i].ext_main);
266         }
267         return(0);
268 }
269
270 int
271 extops_kill (void)
272 {
273         struct extop_list *ext;
274
275         /* we allocated the memory, so we have to free it, too. */
276         while ((ext = supp_ext_list) != NULL) {
277                 supp_ext_list = ext->next;
278                 if (ext->oid != NULL)
279                         ch_free(ext->oid);
280                 ch_free(ext);
281         }
282         return(0);
283 }
284
285 static struct extop_list *
286 find_extop( struct extop_list *list, char *oid )
287 {
288         struct extop_list *ext;
289
290         for (ext = list; ext; ext = ext->next) {
291                 if (strcmp(ext->oid, oid) == 0)
292                         return(ext);
293         }
294         return(NULL);
295 }