]> git.sur5r.net Git - openldap/blob - servers/slapd/controls.c
09267a8bb2fa43c3bb63a23c48c1017fa00ee9e4
[openldap] / servers / slapd / controls.c
1 /* 
2  * Copyright 1999 The OpenLDAP Foundation.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted only
6  * as authorized by the OpenLDAP Public License.  A copy of this
7  * license is available at http://www.OpenLDAP.org/license.html or
8  * in file LICENSE in the top-level directory of the distribution.
9  */
10 #include "portable.h"
11
12 #include <stdio.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16
17 #include "../../libraries/liblber/lber-int.h"
18
19 char *supportedControls[] = {
20         LDAP_CONTROL_MANAGEDSAIT,
21         NULL
22 };
23
24 int get_ctrls(
25         Connection *conn,
26         Operation *op,
27         int sendres )
28 {
29         int nctrls;
30         ber_tag_t tag;
31         ber_len_t len;
32         char *opaque;
33         BerElement *ber = op->o_ber;
34         LDAPControl ***ctrls = &op->o_ctrls;
35         int rc = LDAP_SUCCESS;
36         char *errmsg = NULL;
37
38         len = ber_pvt_ber_remaining(ber);
39
40         if( len == 0) {
41                 /* no controls */
42                 rc = LDAP_SUCCESS;
43                 goto return_results;
44         }
45
46         if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
47                 if( tag == LBER_ERROR ) {
48                         rc = -1;
49                         errmsg = "unexpected data in PDU";
50                 }
51
52                 goto return_results;
53         }
54
55         Debug( LDAP_DEBUG_TRACE, "=> get_ctrls\n", 0, 0, 0 );
56
57         if( op->o_protocol < LDAP_VERSION3 ) {
58                 rc = -1;
59                 errmsg = "controls require LDAPv3";
60                 goto return_results;
61         }
62
63         /* set through each element */
64         nctrls = 0;
65         *ctrls = ch_malloc( 1 * sizeof(LDAPControl *) );
66
67 #if 0
68         if( *ctrls == NULL ) {
69                 rc = LDAP_NO_MEMORY;
70                 errmsg = "no memory";
71                 goto return_results;
72         }
73 #endif
74
75         ctrls[nctrls] = NULL;
76
77         for( tag = ber_first_element( ber, &len, &opaque );
78                 tag != LBER_ERROR;
79                 tag = ber_next_element( ber, &len, opaque ) )
80         {
81                 LDAPControl *tctrl;
82                 LDAPControl **tctrls;
83
84                 tctrl = ch_calloc( 1, sizeof(LDAPControl) );
85                 tctrl->ldctl_oid = NULL;
86                 tctrl->ldctl_value.bv_val = NULL;
87
88                 /* allocate pointer space for current controls (nctrls)
89                  * + this control + extra NULL
90                  */
91                 tctrls = (tctrl == NULL) ? NULL :
92                         ch_realloc(*ctrls, (nctrls+2) * sizeof(LDAPControl *));
93
94 #if 0
95                 if( tctrls == NULL ) {
96                         /* one of the above allocation failed */
97
98                         if( tctrl != NULL ) {
99                                 ch_free( tctrl );
100                         }
101
102                         ldap_controls_free(*ctrls);
103                         *ctrls = NULL;
104
105                         rc = LDAP_NO_MEMORY;
106                         errmsg = "no memory";
107                         goto return_results;
108                 }
109 #endif
110
111                 tctrls[nctrls++] = tctrl;
112                 tctrls[nctrls] = NULL;
113
114                 tag = ber_scanf( ber, "{a" /*}*/, &tctrl->ldctl_oid );
115
116                 if( tag == LBER_ERROR ) {
117                         Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
118                                 0, 0, 0 );
119                         *ctrls = NULL;
120                         ldap_controls_free( tctrls );
121                         rc = -1;
122                         errmsg = "decoding controls error";
123                         goto return_results;
124                 }
125
126                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: %s\n",
127                         tctrl->ldctl_oid, 0, 0 );
128
129                 tag = ber_peek_tag( ber, &len );
130
131                 if( tag == LBER_BOOLEAN ) {
132                         ber_int_t crit;
133                         tag = ber_scanf( ber, "b", &crit );
134
135                         if( tag == LBER_ERROR ) {
136                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
137                                         0, 0, 0 );
138                                 *ctrls = NULL;
139                                 ldap_controls_free( tctrls );
140                                 rc = -1;
141                                 errmsg = "decoding controls error";
142                                 goto return_results;
143                         }
144
145                         tctrl->ldctl_iscritical = crit ? (char) 0 : (char) ~0;
146                         tag = ber_peek_tag( ber, &len );
147                 }
148
149                 if( tag == LBER_OCTETSTRING ) {
150                         tag = ber_scanf( ber, "o", &tctrl->ldctl_value );
151
152                         if( tag == LBER_ERROR ) {
153                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get value failed.\n",
154                                         0, 0, 0 );
155                                 *ctrls = NULL;
156                                 ldap_controls_free( tctrls );
157                                 rc = -1;
158                                 errmsg = "decoding controls error";
159                                 goto return_results;
160                         }
161                 }
162
163                 if( tctrl->ldctl_iscritical &&
164                         !charray_inlist( supportedControls, tctrl->ldctl_oid ) )
165                 {
166                         rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
167                         errmsg = "critical extension is unavailable ";
168                         goto return_results;
169                 }
170
171                 *ctrls = tctrls;
172         }
173
174 return_results:
175         Debug( LDAP_DEBUG_TRACE, "<= get_ctrls: %d %d %s\n",
176                 nctrls, rc, errmsg ? errmsg : "");
177
178         if( sendres && rc != LDAP_SUCCESS ) {
179                 if( rc == -1 ) {
180                         send_ldap_disconnect( conn, op, rc, errmsg );
181                 } else {
182                         send_ldap_result( conn, op, rc,
183                                 NULL, errmsg, NULL, NULL );
184                 }
185         }
186
187         return rc;
188 }
189
190
191 int get_manageDSAit( Operation *op )
192 {
193         int i;
194         if( op == NULL || op->o_ctrls == NULL ) {
195                 return 0;
196         }
197
198         for( i=0; op->o_ctrls[i] != NULL; i++ ) {
199                 if( strcmp( LDAP_CONTROL_MANAGEDSAIT, op->o_ctrls[i]->ldctl_oid )
200                         == 0 )
201                 {
202                         return 1;
203                 }
204         }
205
206         return 0;
207 }