]> git.sur5r.net Git - openldap/blob - servers/slapd/extended.c
Const'ification
[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 static int extop_callback(
65         Connection *conn, Operation *op,
66         int msg, int arg, void *argp);
67
68 char *
69 get_supported_extop (int index)
70 {
71         extop_list_t *ext;
72
73         /* linear scan is slow, but this way doesn't force a
74          * big change on root_dse.c, where this routine is used.
75          */
76         for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) ;
77         if (ext == NULL)
78                 return(NULL);
79         return(ext->oid);
80 }
81
82 int
83 do_extended(
84     Connection  *conn,
85     Operation   *op
86 )
87 {
88         int rc = LDAP_SUCCESS;
89         char* reqoid;
90         struct berval *reqdata;
91         ber_tag_t tag;
92         ber_len_t len;
93         extop_list_t *ext;
94         const char *text;
95         struct berval **refs;
96         char *rspoid;
97         struct berval *rspdata;
98         LDAPControl **rspctrls;
99
100         Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
101
102         reqoid = NULL;
103         reqdata = NULL;
104
105         if( op->o_protocol < LDAP_VERSION3 ) {
106                 Debug( LDAP_DEBUG_ANY, "do_extended: protocol version (%d) too low\n",
107                         op->o_protocol, 0 ,0 );
108                 send_ldap_disconnect( conn, op,
109                         LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
110                 rc = -1;
111                 goto done;
112         }
113
114         if ( ber_scanf( op->o_ber, "{a" /*}*/, &reqoid ) == LBER_ERROR ) {
115                 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
116                 send_ldap_disconnect( conn, op,
117                         LDAP_PROTOCOL_ERROR, "decoding error" );
118                 rc = -1;
119                 goto done;
120         }
121
122         if( !(ext = find_extop(supp_ext_list, reqoid)) ) {
123                 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
124                         reqoid, 0 ,0 );
125                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
126                         NULL, "unsupported extended operation", NULL, NULL );
127                 goto done;
128         }
129
130         tag = ber_peek_tag( op->o_ber, &len );
131         
132         if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
133                 if( ber_scanf( op->o_ber, "O", &reqdata ) == LBER_ERROR ) {
134                         Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
135                         send_ldap_disconnect( conn, op,
136                                 LDAP_PROTOCOL_ERROR, "decoding error" );
137                         rc = -1;
138                         goto done;
139                 }
140         }
141
142         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
143                 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
144                 return rc;
145         } 
146
147         Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", reqoid, 0 ,0 );
148
149         rspoid = NULL;
150         rspdata = NULL;
151         rspctrls = NULL;
152         text = NULL;
153         refs = NULL;
154
155         rc = (ext->ext_main)( extop_callback, conn, op,
156                 reqoid, reqdata,
157                 &rspoid, &rspdata, &rspctrls, &text, &refs );
158
159         if( rc != SLAPD_ABANDON ) {
160                 if (rc == LDAP_REFERRAL) {
161                         refs = default_referral;
162                 }
163
164                 send_ldap_extended( conn, op, rc, NULL, text, refs,
165                         rspoid, rspdata, rspctrls );
166         }
167
168         if ( rspoid != NULL ) {
169                 free( rspoid );
170         }
171
172         if ( rspdata != NULL )
173                 ber_bvfree( rspdata );
174
175 done:
176         if ( reqdata != NULL ) {
177                 ber_bvfree( reqdata );
178         }
179         if ( reqoid != NULL ) {
180                 free( reqoid );
181         }
182
183         return rc;
184 }
185
186 int
187 load_extop(
188         const char *ext_oid,
189         SLAP_EXTOP_MAIN_FN ext_main )
190 {
191         extop_list_t *ext;
192
193         if( ext_oid == NULL || *ext_oid == '\0' ) return -1; 
194         if(!ext_main) return -1; 
195
196         ext = ch_calloc(1, sizeof(extop_list_t));
197         if (ext == NULL)
198                 return(-1);
199
200         ext->oid = ch_strdup( ext_oid );
201         if (ext->oid == NULL) {
202                 free(ext);
203                 return(-1);
204         }
205
206         ext->ext_main = ext_main;
207         ext->next = supp_ext_list;
208
209         supp_ext_list = ext;
210
211         return(0);
212 }
213
214 int
215 extops_init (void)
216 {
217         int i;
218
219         for (i = 0; builtin_extops[i].oid != NULL; i++) {
220                 load_extop(builtin_extops[i].oid, builtin_extops[i].ext_main);
221         }
222         return(0);
223 }
224
225 int
226 extops_kill (void)
227 {
228         extop_list_t *ext;
229
230         /* we allocated the memory, so we have to free it, too. */
231         while ((ext = supp_ext_list) != NULL) {
232                 supp_ext_list = ext->next;
233                 if (ext->oid != NULL)
234                         ch_free(ext->oid);
235                 ch_free(ext);
236         }
237         return(0);
238 }
239
240 static extop_list_t *
241 find_extop( extop_list_t *list, char *oid )
242 {
243         extop_list_t *ext;
244
245         for (ext = list; ext; ext = ext->next) {
246                 if (strcmp(ext->oid, oid) == 0)
247                         return(ext);
248         }
249         return(NULL);
250 }
251
252 int
253 extop_callback(
254         Connection *conn, Operation *op,
255         int msg, int arg, void *argp)
256 {
257         if (argp == NULL)
258                 return(-1);
259
260         switch (msg) {
261         case SLAPD_EXTOP_GETVERSION:
262                 *(int *)argp = 1;
263                 return(0);
264
265         case SLAPD_EXTOP_GETPROTO:
266                 *(int *)argp = op->o_protocol;
267                 return(0);
268         
269         case SLAPD_EXTOP_GETAUTH:
270                 *(int *)argp = op->o_authtype;
271                 return(0);
272         
273         case SLAPD_EXTOP_GETDN:
274                 *(char **)argp = op->o_dn;
275                 return(0);
276         
277         case SLAPD_EXTOP_GETCLIENT:
278                 if (conn->c_peer_domain != NULL && *conn->c_peer_domain != 0) {
279                         *(char **)argp = conn->c_peer_domain;
280                         return(0);
281                 }
282                 if (conn->c_peer_name != NULL && *conn->c_peer_name != 0) {
283                         *(char **)argp = conn->c_peer_name;
284                         return(0);
285                 }
286                 break;
287         
288         default:
289                 break;
290         }
291         return(-1);
292 }