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