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