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