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