]> git.sur5r.net Git - openldap/blob - libraries/libldap/extended.c
Move connectionless connection init to startup (may not be in the
[openldap] / libraries / libldap / extended.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (C) The Internet Society (1997).
16  * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
17  */
18
19 /*
20  * LDAPv3 Extended Operation Request
21  *      ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
22  *              requestName      [0] LDAPOID,
23  *              requestValue     [1] OCTET STRING OPTIONAL
24  *      }
25  *
26  * LDAPv3 Extended Operation Response
27  *      ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
28  *              COMPONENTS OF LDAPResult,
29  *              responseName     [10] LDAPOID OPTIONAL,
30  *              response         [11] OCTET STRING OPTIONAL
31  *      }
32  *
33  */
34
35 #include "portable.h"
36
37 #include <stdio.h>
38 #include <ac/stdlib.h>
39
40 #include <ac/socket.h>
41 #include <ac/string.h>
42 #include <ac/time.h>
43
44 #include "ldap-int.h"
45 #include "ldap_log.h"
46
47 int
48 ldap_extended_operation(
49         LDAP                    *ld,
50         LDAP_CONST char *reqoid,
51         struct berval   *reqdata,
52         LDAPControl             **sctrls,
53         LDAPControl             **cctrls,
54         int                             *msgidp )
55 {
56         BerElement *ber;
57         int rc;
58         ber_int_t id;
59
60         Debug( LDAP_DEBUG_TRACE, "ldap_extended_operation\n", 0, 0, 0 );
61
62         assert( ld != NULL );
63         assert( LDAP_VALID( ld ) );
64         assert( reqoid != NULL && *reqoid != '\0' );
65         assert( msgidp != NULL );
66
67         /* must be version 3 (or greater) */
68         if ( ld->ld_version < LDAP_VERSION3 ) {
69                 ld->ld_errno = LDAP_NOT_SUPPORTED;
70                 return( ld->ld_errno );
71         }
72
73         /* create a message to send */
74         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
75                 ld->ld_errno = LDAP_NO_MEMORY;
76                 return( ld->ld_errno );
77         }
78
79         LDAP_NEXT_MSGID( ld, id );
80         if ( reqdata != NULL ) {
81                 rc = ber_printf( ber, "{it{tstON}", /* '}' */
82                         id, LDAP_REQ_EXTENDED,
83                         LDAP_TAG_EXOP_REQ_OID, reqoid,
84                         LDAP_TAG_EXOP_REQ_VALUE, reqdata );
85
86         } else {
87                 rc = ber_printf( ber, "{it{tsN}", /* '}' */
88                         id, LDAP_REQ_EXTENDED,
89                         LDAP_TAG_EXOP_REQ_OID, reqoid );
90         }
91
92         if( rc == -1 ) {
93                 ld->ld_errno = LDAP_ENCODING_ERROR;
94                 ber_free( ber, 1 );
95                 return( ld->ld_errno );
96         }
97
98         /* Put Server Controls */
99         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
100                 ber_free( ber, 1 );
101                 return ld->ld_errno;
102         }
103
104         if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
105                 ld->ld_errno = LDAP_ENCODING_ERROR;
106                 ber_free( ber, 1 );
107                 return( ld->ld_errno );
108         }
109
110         /* send the message */
111         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_EXTENDED, NULL, ber, id );
112
113         return( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
114 }
115
116 int
117 ldap_extended_operation_s(
118         LDAP                    *ld,
119         LDAP_CONST char *reqoid,
120         struct berval   *reqdata,
121         LDAPControl             **sctrls,
122         LDAPControl             **cctrls,
123         char                    **retoidp,
124         struct berval   **retdatap )
125 {
126     int     rc;
127     int     msgid;
128     LDAPMessage *res;
129
130         Debug( LDAP_DEBUG_TRACE, "ldap_extended_operation_s\n", 0, 0, 0 );
131
132         assert( ld != NULL );
133         assert( LDAP_VALID( ld ) );
134         assert( reqoid != NULL && *reqoid != '\0' );
135
136     rc = ldap_extended_operation( ld, reqoid, reqdata,
137                 sctrls, cctrls, &msgid );
138         
139     if ( rc != LDAP_SUCCESS ) {
140         return( rc );
141         }
142  
143     if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 ) {
144         return( ld->ld_errno );
145         }
146
147         if ( retoidp != NULL ) *retoidp = NULL;
148         if ( retdatap != NULL ) *retdatap = NULL;
149
150         rc = ldap_parse_extended_result( ld, res, retoidp, retdatap, 0 );
151
152         if( rc != LDAP_SUCCESS ) {
153                 ldap_msgfree( res );
154                 return rc;
155         }
156
157     return( ldap_result2error( ld, res, 1 ) );
158 }
159
160 /* Parse an extended result */
161 int
162 ldap_parse_extended_result (
163         LDAP                    *ld,
164         LDAPMessage             *res,
165         char                    **retoidp,
166         struct berval   **retdatap,
167         int                             freeit )
168 {
169         BerElement *ber;
170         ber_tag_t rc;
171         ber_tag_t tag;
172         ber_len_t len;
173         struct berval *resdata;
174         ber_int_t errcode;
175         char *resoid;
176
177         assert( ld != NULL );
178         assert( LDAP_VALID( ld ) );
179         assert( res != NULL );
180
181         Debug( LDAP_DEBUG_TRACE, "ldap_parse_extended_result\n", 0, 0, 0 );
182
183         if( ld->ld_version < LDAP_VERSION3 ) {
184                 ld->ld_errno = LDAP_NOT_SUPPORTED;
185                 return ld->ld_errno;
186         }
187
188         if( res->lm_msgtype != LDAP_RES_EXTENDED ) {
189                 ld->ld_errno = LDAP_PARAM_ERROR;
190                 return ld->ld_errno;
191         }
192
193         if( retoidp != NULL ) *retoidp = NULL;
194         if( retdatap != NULL ) *retdatap = NULL;
195
196         if ( ld->ld_error ) {
197                 LDAP_FREE( ld->ld_error );
198                 ld->ld_error = NULL;
199         }
200
201         if ( ld->ld_matched ) {
202                 LDAP_FREE( ld->ld_matched );
203                 ld->ld_matched = NULL;
204         }
205
206         ber = ber_dup( res->lm_ber );
207
208         if ( ber == NULL ) {
209                 ld->ld_errno = LDAP_NO_MEMORY;
210                 return ld->ld_errno;
211         }
212
213         rc = ber_scanf( ber, "{eaa" /*}*/, &errcode,
214                 &ld->ld_matched, &ld->ld_error );
215
216         if( rc == LBER_ERROR ) {
217                 ld->ld_errno = LDAP_DECODING_ERROR;
218                 ber_free( ber, 0 );
219                 return ld->ld_errno;
220         }
221
222         resoid = NULL;
223         resdata = NULL;
224
225         tag = ber_peek_tag( ber, &len );
226
227         if( tag == LDAP_TAG_REFERRAL ) {
228                 /* skip over referral */
229                 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
230                         ld->ld_errno = LDAP_DECODING_ERROR;
231                         ber_free( ber, 0 );
232                         return ld->ld_errno;
233                 }
234
235                 tag = ber_peek_tag( ber, &len );
236         }
237
238         if( tag == LDAP_TAG_EXOP_RES_OID ) {
239                 /* we have a resoid */
240                 if( ber_scanf( ber, "a", &resoid ) == LBER_ERROR ) {
241                         ld->ld_errno = LDAP_DECODING_ERROR;
242                         ber_free( ber, 0 );
243                         return ld->ld_errno;
244                 }
245
246                 tag = ber_peek_tag( ber, &len );
247         }
248
249         if( tag == LDAP_TAG_EXOP_RES_VALUE ) {
250                 /* we have a resdata */
251                 if( ber_scanf( ber, "O", &resdata ) == LBER_ERROR ) {
252                         ld->ld_errno = LDAP_DECODING_ERROR;
253                         ber_free( ber, 0 );
254                         if( resoid != NULL ) LDAP_FREE( resoid );
255                         return ld->ld_errno;
256                 }
257         }
258
259         ber_free( ber, 0 );
260
261         if( retoidp != NULL ) {
262                 *retoidp = resoid;
263         } else {
264                 LDAP_FREE( resoid );
265         }
266
267         if( retdatap != NULL ) {
268                 *retdatap = resdata;
269         } else {
270                 ber_bvfree( resdata );
271         }
272
273         ld->ld_errno = errcode;
274
275         if( freeit ) {
276                 ldap_msgfree( res );
277         }
278
279         return LDAP_SUCCESS;
280 }
281
282
283 /* Parse an extended partial */
284 int
285 ldap_parse_intermediate (
286         LDAP                    *ld,
287         LDAPMessage             *res,
288         char                    **retoidp,
289         struct berval   **retdatap,
290         LDAPControl             ***serverctrls,
291         int                             freeit )
292 {
293         BerElement *ber;
294         ber_tag_t rc;
295         ber_tag_t tag;
296         ber_len_t len;
297         struct berval *resdata;
298         char *resoid;
299
300         assert( ld != NULL );
301         assert( LDAP_VALID( ld ) );
302         assert( res != NULL );
303
304         Debug( LDAP_DEBUG_TRACE, "ldap_parse_intermediate\n", 0, 0, 0 );
305
306         if( ld->ld_version < LDAP_VERSION3 ) {
307                 ld->ld_errno = LDAP_NOT_SUPPORTED;
308                 return ld->ld_errno;
309         }
310
311         if( res->lm_msgtype != LDAP_RES_INTERMEDIATE ) {
312                 ld->ld_errno = LDAP_PARAM_ERROR;
313                 return ld->ld_errno;
314         }
315
316         if( retoidp != NULL ) *retoidp = NULL;
317         if( retdatap != NULL ) *retdatap = NULL;
318
319         ber = ber_dup( res->lm_ber );
320
321         if ( ber == NULL ) {
322                 ld->ld_errno = LDAP_NO_MEMORY;
323                 return ld->ld_errno;
324         }
325
326         rc = ber_scanf( ber, "{" /*}*/ );
327
328         if( rc == LBER_ERROR ) {
329                 ld->ld_errno = LDAP_DECODING_ERROR;
330                 ber_free( ber, 0 );
331                 return ld->ld_errno;
332         }
333
334         resoid = NULL;
335         resdata = NULL;
336
337         tag = ber_peek_tag( ber, &len );
338
339         /*
340          * NOTE: accept intermediate and extended response tag values
341          * as older versions of slapd(8) incorrectly used extended
342          * response tags.
343          * Should be removed when 2.2 is moved to Historic.
344          */
345         if( tag == LDAP_TAG_IM_RES_OID || tag == LDAP_TAG_EXOP_RES_OID ) {
346                 /* we have a resoid */
347                 if( ber_scanf( ber, "a", &resoid ) == LBER_ERROR ) {
348                         ld->ld_errno = LDAP_DECODING_ERROR;
349                         ber_free( ber, 0 );
350                         return ld->ld_errno;
351                 }
352
353                 tag = ber_peek_tag( ber, &len );
354         }
355
356         if( tag == LDAP_TAG_IM_RES_VALUE || tag == LDAP_TAG_EXOP_RES_VALUE ) {
357                 /* we have a resdata */
358                 if( ber_scanf( ber, "O", &resdata ) == LBER_ERROR ) {
359                         ld->ld_errno = LDAP_DECODING_ERROR;
360                         ber_free( ber, 0 );
361                         if( resoid != NULL ) LDAP_FREE( resoid );
362                         return ld->ld_errno;
363                 }
364         }
365
366         if ( serverctrls == NULL ) {
367                 rc = LDAP_SUCCESS;
368                 goto free_and_return;
369         }
370
371         if ( ber_scanf( ber, /*{*/ "}" ) == LBER_ERROR ) {
372                 rc = LDAP_DECODING_ERROR;
373                 goto free_and_return;
374         }
375
376         rc = ldap_pvt_get_controls( ber, serverctrls );
377
378 free_and_return:
379         ber_free( ber, 0 );
380
381         if( retoidp != NULL ) {
382                 *retoidp = resoid;
383         } else {
384                 LDAP_FREE( resoid );
385         }
386
387         if( retdatap != NULL ) {
388                 *retdatap = resdata;
389         } else {
390                 ber_bvfree( resdata );
391         }
392
393         if( freeit ) {
394                 ldap_msgfree( res );
395         }
396
397         return LDAP_SUCCESS;
398 }
399