]> git.sur5r.net Git - openldap/blob - libraries/libldap/options.c
Add strdup.c from -llutil, renamed to ldap_strdup() and always used.
[openldap] / libraries / libldap / options.c
1 #include "portable.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include <ac/socket.h>
7 #include <ac/string.h>
8
9 #include "ldap-int.h"
10
11 static const char* features[] = {
12 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
13         "X_OPENLDAP_V2_DNS",
14 #endif
15 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
16         "X_OPENLDAP_V2_REFERRALS",
17 #endif
18         NULL
19 };
20
21 int
22 ldap_get_option(
23         LDAP    *ld,
24         int             option,
25         void    *outvalue)
26 {
27         struct ldapoptions *lo;
28
29         if(!openldap_ldap_initialized) {
30                 openldap_ldap_initialize();
31         }
32
33         if(outvalue == NULL) {
34                 /* no place to get to */
35                 return -1;
36         }
37
38         if(ld == NULL) {
39                 lo = &openldap_ldap_global_options;
40         } else {
41                 lo = &ld->ld_options;
42         }
43
44         switch(option) {
45         case LDAP_OPT_API_INFO: {
46                         struct ldapapiinfo *info = (struct ldapapiinfo *) outvalue;
47
48                         if(info == NULL) {
49                                 /* outvalue must point to an apiinfo structure */
50                                 return -1;
51                         }
52
53                         if(info->ldapai_info_version != LDAP_API_INFO_VERSION) {
54                                 /* api info version mismatch */
55                                 info->ldapai_info_version = LDAP_API_INFO_VERSION;
56                                 return -1;
57                         }
58
59                         info->ldapai_api_version = LDAP_API_VERSION;
60                         info->ldapai_api_version = LDAP_API_VERSION;
61                         info->ldapai_protocol_version = LDAP_VERSION_MAX;
62                         if(features[0] == NULL) {
63                                 info->ldapai_extensions = NULL;
64                         } else {
65                                 int i;
66                                 info->ldapai_extensions = malloc(sizeof(features));
67
68                                 for(i=0; features[i] != NULL; i++) {
69                                         info->ldapai_extensions[i] =
70                                                 ldap_strdup(features[i]);
71                                 }
72
73                                 info->ldapai_extensions[i] = NULL;
74                         }
75
76                         info->ldapai_vendor_name = ldap_strdup(LDAP_VENDOR_NAME);
77                         info->ldapai_vendor_version = LDAP_VENDOR_VERSION;
78
79                         return 0;
80                 } break;
81
82         case LDAP_OPT_DESC:
83                 if(ld == NULL) {
84                         /* bad param */
85                         break;
86                 } 
87
88                 * (int *) outvalue = ld->ld_sb.sb_sd;
89                 return 0;
90
91         case LDAP_OPT_DEREF:
92                 * (int *) outvalue = lo->ldo_deref;
93                 return 0;
94
95         case LDAP_OPT_SIZELIMIT:
96                 * (int *) outvalue = lo->ldo_sizelimit;
97                 return 0;
98
99         case LDAP_OPT_TIMELIMIT:
100                 * (int *) outvalue = lo->ldo_timelimit;
101                 return 0;
102
103         case LDAP_OPT_REFERRALS:
104                 * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_REFERRALS);
105                 return 0;
106                 
107         case LDAP_OPT_RESTART:
108                 * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_RESTART);
109                 return 0;
110
111         case LDAP_OPT_DNS:      /* LDAPv2 */
112                 * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_DNS);
113                 return 0;
114
115         case LDAP_OPT_PROTOCOL_VERSION:
116                 if ((ld != NULL) && ld->ld_version) {
117                         * (int *) outvalue = ld->ld_version;
118                 } else { 
119                         * (int *) outvalue = lo->ldo_version;
120                 }
121                 return 0;
122
123         case LDAP_OPT_SERVER_CONTROLS:
124         case LDAP_OPT_CLIENT_CONTROLS:
125                 /* not yet supported */
126                 break;
127
128         case LDAP_OPT_HOST_NAME:
129                 /*
130                  * draft-ietf-ldapext-ldap-c-api-01 doesn't state
131                  * whether client to have to free host names or no,
132                  * we do
133                  */
134
135                 * (char **) outvalue = ldap_strdup(lo->ldo_defhost);
136                 return 0;
137
138         case LDAP_OPT_ERROR_NUMBER:
139                 if(ld == NULL) {
140                         /* bad param */
141                         break;
142                 } 
143                 * (int *) outvalue = ld->ld_errno;
144                 return 0;
145
146         case LDAP_OPT_ERROR_STRING:
147                 /* not yet supported */
148                 if(ld == NULL) {
149                         /* bad param */
150                         break;
151                 } 
152
153                 /*
154                  * draft-ietf-ldapext-ldap-c-api-01 doesn't require
155                  *      the client to have to free error strings, we do
156                  */
157
158                 if( ld->ld_error == NULL ) {
159                         * (char **) outvalue = NULL;
160                 } else {
161                         * (char **) outvalue = ldap_strdup(ld->ld_error);
162                 }
163                 break;
164
165         default:
166                 /* bad param */
167                 break;
168         }
169
170         return -1;
171 }
172
173 int
174 ldap_set_option(
175         LDAP    *ld,
176         int             option,
177         void    *invalue)
178 {
179         struct ldapoptions *lo;
180
181         if(!openldap_ldap_initialized) {
182                 openldap_ldap_initialize();
183         }
184
185         if(invalue == NULL) {
186                 /* no place to set from */
187                 return -1;
188         }
189
190         if(ld == NULL) {
191                 lo = &openldap_ldap_global_options;
192         } else {
193                 lo = &ld->ld_options;
194         }
195
196         switch(option) {
197         case LDAP_OPT_API_INFO:
198         case LDAP_OPT_DESC:
199                 /* READ ONLY */
200                 break;
201
202         case LDAP_OPT_DEREF:
203                 lo->ldo_deref = * (int *) invalue;
204                 return 0;
205
206         case LDAP_OPT_SIZELIMIT:
207                 lo->ldo_sizelimit = * (int *) invalue;
208                 return 0;
209
210         case LDAP_OPT_TIMELIMIT:
211                 lo->ldo_timelimit = * (int *) invalue;
212                 return 0;
213
214         case LDAP_OPT_REFERRALS:
215                 if((int) invalue == (int) LDAP_OPT_ON) {
216                         LDAP_BOOL_SET(lo, LDAP_BOOL_REFERRALS);
217                 } else {
218                         LDAP_BOOL_CLR(lo, LDAP_BOOL_REFERRALS);
219                 }
220                 return 0;
221
222         case LDAP_OPT_RESTART:
223                 if((int) invalue == (int) LDAP_OPT_ON) {
224                         LDAP_BOOL_SET(lo, LDAP_BOOL_RESTART);
225                 } else {
226                         LDAP_BOOL_CLR(lo, LDAP_BOOL_RESTART);
227                 }
228                 return 0;
229
230         case LDAP_OPT_PROTOCOL_VERSION: {
231                         int vers = * (int *) invalue;
232                         if (vers < LDAP_VERSION_MIN || vers > LDAP_VERSION_MAX) {
233                                 /* not supported */
234                                 break;
235                         }
236                         ld->ld_version = vers;
237                 } return 0;
238
239         case LDAP_OPT_SERVER_CONTROLS:
240         case LDAP_OPT_CLIENT_CONTROLS:
241                 /* not yet supported */
242                 break;
243
244         case LDAP_OPT_HOST_NAME: {
245                         char* host = * (char **) invalue;
246
247                         if(lo->ldo_defhost != NULL) {
248                                 free(lo->ldo_defhost);
249                                 lo->ldo_defhost = NULL;
250                         }
251
252                         if(host != NULL) {
253                                 lo->ldo_defhost = ldap_strdup(host);
254                                 return 0;
255                         }
256
257                         if(ld == NULL) {
258                                 /*
259                                  * must want global default returned
260                                  * to initial condition.
261                                  */
262                                 lo->ldo_defhost = ldap_strdup("localhost");
263
264                         } else {
265                                 /*
266                                  * must want the session default
267                                  *   updated to the current global default
268                                  */
269                                 lo->ldo_defhost = ldap_strdup(
270                                         openldap_ldap_global_options.ldo_defhost);
271                         }
272                 } return 0;
273
274         case LDAP_OPT_ERROR_NUMBER: {
275                         int err = * (int *) invalue;
276
277                         if (err != 0 ) {
278                                 /* not supported */
279                                 /* we only allow ld_errno to be cleared. */
280                                 break;
281                         }
282
283                         if(ld == NULL) {
284                                 /* need a struct ldap */
285                                 break;
286                         }
287
288                         ld->ld_errno = err;
289                 } return 0;
290
291         case LDAP_OPT_ERROR_STRING: {
292                         char* err = * (char **) invalue;
293
294                         if (err != NULL ) {
295                                 /* not supported */
296                                 /* we only allow ld_error to be cleared. */
297                                 break;
298                         }
299
300                         if(ld == NULL) {
301                                 /* need a struct ldap */
302                                 break;
303                         }
304
305                         ld->ld_error = err;
306                 } return 0;
307
308         default:
309                 /* bad param */
310                 break;
311         }
312         return -1;
313 }