]> git.sur5r.net Git - openldap/blob - libraries/libldap/controls.c
rename ldap_pvt_init_utils() to ldap_int_utils_init() and provide
[openldap] / libraries / libldap / controls.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*
6  * LDAP controls
7  */
8
9 #include "portable.h"
10
11 #include <stdlib.h>
12
13 #include <ac/time.h>
14 #include <ac/string.h>
15
16 #include "ldap-int.h"
17
18
19 /*
20  * ldap_int_put_controls
21  */
22
23 int ldap_int_put_controls(
24         LDAP *ld,
25         LDAPControl **ctrls,
26         BerElement *ber )
27 {
28         LDAPControl **c;
29
30         assert( ld != NULL );
31         assert( ber != NULL );
32
33         if( ctrls == NULL ) {
34                 /* use default server controls */
35                 ctrls = ld->ld_sctrls;
36         }
37
38         if( ctrls == NULL || *ctrls == NULL ) {
39                 return LDAP_SUCCESS;
40         }
41
42         if ( ld->ld_version < LDAP_VERSION3 ) {
43                 /* LDAPv2 doesn't support controls,
44                  * error if any control is critical
45                  */
46                 for( c = ctrls ; *c != NULL; c++ ) {
47                         if( (*c)->ldctl_iscritical ) {
48                                 ld->ld_errno = LDAP_NOT_SUPPORTED;
49                                 return ld->ld_errno;
50                         }
51                 }
52
53                 return LDAP_SUCCESS;
54         }
55
56         /* Controls are encoded as a sequence of sequences */
57         if( ber_printf( ber, "t{", LDAP_TAG_CONTROLS ) == -1 ) {
58                 ld->ld_errno = LDAP_ENCODING_ERROR;
59                 return ld->ld_errno;
60         }
61
62         for( c = ctrls ; *c != NULL; c++ ) {
63                 if ( ber_printf( ber, "{s",
64                         (*c)->ldctl_oid ) == -1 )
65                 {
66                         ld->ld_errno = LDAP_ENCODING_ERROR;
67                         return ld->ld_errno;
68                 }
69
70                 if( (*c)->ldctl_iscritical /* only if true */
71                         &&  ( ber_printf( ber, "b",
72                                 (*c)->ldctl_iscritical ) == -1 ) )
73                 {
74                         ld->ld_errno = LDAP_ENCODING_ERROR;
75                         return ld->ld_errno;
76                 }
77
78                 if( (*c)->ldctl_value.bv_val != NULL /* only if we have a value */
79                         &&  ( ber_printf( ber, "O",
80                                 &((*c)->ldctl_value) ) == -1 ) )
81                 {
82                         ld->ld_errno = LDAP_ENCODING_ERROR;
83                         return ld->ld_errno;
84                 }
85
86
87                 if( ber_printf( ber, "}" ) == -1 ) {
88                         ld->ld_errno = LDAP_ENCODING_ERROR;
89                         return ld->ld_errno;
90                 }
91         }
92
93
94         if( ber_printf( ber, "}" ) == -1 ) {
95                 ld->ld_errno = LDAP_ENCODING_ERROR;
96                 return ld->ld_errno;
97         }
98
99         return LDAP_SUCCESS;
100 }
101
102 int ldap_int_get_controls LDAP_P((
103         BerElement *be,
104         LDAPControl ***ctrls ))
105 {
106         assert( be != NULL );
107         assert( ctrls != NULL );
108
109         **ctrls = NULL;
110
111         return LDAP_NOT_SUPPORTED;
112 }
113
114 /*
115  * Free a LDAPControl
116  */
117 void
118 ldap_control_free( LDAPControl *c )
119 {
120         if ( c != NULL ) {
121                 if( c->ldctl_oid != NULL) {
122                         free( c->ldctl_oid );
123                 }
124
125                 if( c->ldctl_value.bv_val != NULL ) {
126                         free( c->ldctl_value.bv_val );
127                 }
128
129                 free( c );
130         }
131 }
132
133 /*
134  * Free an array of LDAPControl's
135  */
136 void
137 ldap_controls_free( LDAPControl **controls )
138 {
139         if ( controls != NULL ) {
140                 LDAPControl *c;
141
142                 for(c = *controls; c != NULL; c++) {
143                         ldap_control_free( c );
144                 }
145
146                 free( controls );
147         }
148 }
149
150 /*
151  * Duplicate an array of LDAPControl
152  */
153 LDAPControl **ldap_controls_dup( const LDAPControl **controls )
154 {
155         LDAPControl **new;
156         int i;
157
158         if ( controls == NULL ) {
159                 return NULL;
160         }
161
162         /* count the controls */
163         for(i=0; controls[i] != NULL; i++) /* empty */ ;
164
165         if( i < 1 ) {
166                 /* no controls to duplicate */
167                 return NULL;
168         }
169
170         new = (LDAPControl **) malloc( i * sizeof(LDAPControl *) );
171
172         if( new == NULL ) {
173                 /* memory allocation failure */
174                 return NULL;
175         }
176
177         /* duplicate the controls */
178         for(i=0; controls[i] != NULL; i++) {
179                 new[i] = ldap_control_dup( controls[i] );
180
181                 if( new[i] == NULL ) {
182                         ldap_controls_free( new );
183                         return NULL;
184                 }
185         }
186
187         new[i] = NULL;
188
189         return new;
190 }
191
192 /*
193  * Duplicate a LDAPControl
194  */
195 LDAPControl *ldap_control_dup( const LDAPControl *c )
196 {
197         LDAPControl *new;
198
199         if ( c == NULL ) {
200                 return NULL;
201         }
202
203         new = (LDAPControl *) malloc( sizeof(LDAPControl) );
204
205         if( new == NULL ) {
206                 return NULL;
207         }
208
209         if( c->ldctl_oid != NULL ) {
210                 new->ldctl_oid = strdup( c->ldctl_oid );
211
212                 if(new->ldctl_oid == NULL) {
213                         free( new );
214                         return NULL;
215                 }
216
217         } else {
218                 new->ldctl_oid = NULL;
219         }
220
221         if( c->ldctl_value.bv_len > 0 ) {
222                 new->ldctl_value.bv_val = (char *) malloc( c->ldctl_value.bv_len );
223
224                 if(new->ldctl_value.bv_val == NULL) {
225                         if(new->ldctl_oid != NULL) {
226                                 free( new->ldctl_oid );
227                         }
228                         free( new );
229                         return NULL;
230                 }
231                 
232                 SAFEMEMCPY( new->ldctl_value.bv_val, c->ldctl_value.bv_val, 
233                         c->ldctl_value.bv_len );
234
235                 new->ldctl_value.bv_len = c->ldctl_value.bv_len;
236
237         } else {
238                 new->ldctl_value.bv_len = 0;
239                 new->ldctl_value.bv_val = NULL;
240         }
241
242         new->ldctl_iscritical = c->ldctl_iscritical;
243         return new;
244 }