]> git.sur5r.net Git - openldap/blob - libraries/libldap/options.c
Remove extern declarations of library functions from source.c.
[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] = strdup(features[i]);
70                                 }
71
72                                 info->ldapai_extensions[i] = NULL;
73                         }
74
75                         info->ldapai_vendor_name = strdup(LDAP_VENDOR_NAME);
76                         info->ldapai_vendor_version = LDAP_VENDOR_VERSION;
77
78                         return 0;
79                 } break;
80
81         case LDAP_OPT_DESC:
82                 if(ld == NULL) {
83                         /* bad param */
84                         break;
85                 } 
86
87                 * (int *) outvalue = ld->ld_sb.sb_sd;
88                 return 0;
89
90         case LDAP_OPT_DEREF:
91                 * (int *) outvalue = lo->ldo_deref;
92                 return 0;
93
94         case LDAP_OPT_SIZELIMIT:
95                 * (int *) outvalue = lo->ldo_sizelimit;
96                 return 0;
97
98         case LDAP_OPT_TIMELIMIT:
99                 * (int *) outvalue = lo->ldo_timelimit;
100                 return 0;
101
102         case LDAP_OPT_REFERRALS:
103                 * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_REFERRALS);
104                 return 0;
105                 
106         case LDAP_OPT_RESTART:
107                 * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_RESTART);
108                 return 0;
109
110         case LDAP_OPT_DNS:      /* LDAPv2 */
111                 * (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_DNS);
112                 return 0;
113
114         case LDAP_OPT_PROTOCOL_VERSION:
115                 if ((ld != NULL) && ld->ld_version) {
116                         * (int *) outvalue = ld->ld_version;
117                 } else { 
118                         * (int *) outvalue = lo->ldo_version;
119                 }
120                 return 0;
121
122         case LDAP_OPT_SERVER_CONTROLS:
123         case LDAP_OPT_CLIENT_CONTROLS:
124                 /* not yet supported */
125                 break;
126
127         case LDAP_OPT_HOST_NAME:
128                 /*
129                  * draft-ietf-ldapext-ldap-c-api-01 doesn't state
130                  * whether client to have to free host names or no,
131                  * we do
132                  */
133
134                 * (char **) outvalue = strdup(lo->ldo_defhost);
135                 return 0;
136
137         case LDAP_OPT_ERROR_NUMBER:
138                 if(ld == NULL) {
139                         /* bad param */
140                         break;
141                 } 
142                 * (int *) outvalue = ld->ld_errno;
143                 return 0;
144
145         case LDAP_OPT_ERROR_STRING:
146                 /* not yet supported */
147                 if(ld == NULL) {
148                         /* bad param */
149                         break;
150                 } 
151
152                 /*
153                  * draft-ietf-ldapext-ldap-c-api-01 doesn't require
154                  *      the client to have to free error strings, we do
155                  */
156
157                 if( ld->ld_error == NULL ) {
158                         * (char **) outvalue = NULL;
159                 } else {
160                         * (char **) outvalue = strdup(ld->ld_error);
161                 }
162                 break;
163
164         default:
165                 /* bad param */
166                 break;
167         }
168
169         return -1;
170 }
171
172 int
173 ldap_set_option(
174         LDAP    *ld,
175         int             option,
176         void    *invalue)
177 {
178         struct ldapoptions *lo;
179
180         if(!openldap_ldap_initialized) {
181                 openldap_ldap_initialize();
182         }
183
184         if(invalue == NULL) {
185                 /* no place to set from */
186                 return -1;
187         }
188
189         if(ld == NULL) {
190                 lo = &openldap_ldap_global_options;
191         } else {
192                 lo = &ld->ld_options;
193         }
194
195         switch(option) {
196         case LDAP_OPT_API_INFO:
197         case LDAP_OPT_DESC:
198                 /* READ ONLY */
199                 break;
200
201         case LDAP_OPT_DEREF:
202                 lo->ldo_deref = * (int *) invalue;
203                 return 0;
204
205         case LDAP_OPT_SIZELIMIT:
206                 lo->ldo_sizelimit = * (int *) invalue;
207                 return 0;
208
209         case LDAP_OPT_TIMELIMIT:
210                 lo->ldo_timelimit = * (int *) invalue;
211                 return 0;
212
213         case LDAP_OPT_REFERRALS:
214                 if((int) invalue == (int) LDAP_OPT_ON) {
215                         LDAP_BOOL_SET(lo, LDAP_BOOL_REFERRALS);
216                 } else {
217                         LDAP_BOOL_CLR(lo, LDAP_BOOL_REFERRALS);
218                 }
219                 return 0;
220
221         case LDAP_OPT_RESTART:
222                 if((int) invalue == (int) LDAP_OPT_ON) {
223                         LDAP_BOOL_SET(lo, LDAP_BOOL_RESTART);
224                 } else {
225                         LDAP_BOOL_CLR(lo, LDAP_BOOL_RESTART);
226                 }
227                 return 0;
228
229         case LDAP_OPT_PROTOCOL_VERSION: {
230                         int vers = * (int *) invalue;
231                         if (vers < LDAP_VERSION_MIN || vers > LDAP_VERSION_MAX) {
232                                 /* not supported */
233                                 break;
234                         }
235                         ld->ld_version = vers;
236                 } return 0;
237
238         case LDAP_OPT_SERVER_CONTROLS:
239         case LDAP_OPT_CLIENT_CONTROLS:
240                 /* not yet supported */
241                 break;
242
243         case LDAP_OPT_HOST_NAME: {
244                         char* host = * (char **) invalue;
245
246                         if(lo->ldo_defhost != NULL) {
247                                 free(lo->ldo_defhost);
248                                 lo->ldo_defhost = NULL;
249                         }
250
251                         if(host != NULL) {
252                                 lo->ldo_defhost = strdup(host);
253                                 return 0;
254                         }
255
256                         if(ld == NULL) {
257                                 /*
258                                  * must want global default returned
259                                  * to initial condition.
260                                  */
261                                 lo->ldo_defhost = strdup("localhost");
262
263                         } else {
264                                 /*
265                                  * must want the session default
266                                  *   updated to the current global default
267                                  */
268                                 lo->ldo_defhost = strdup(
269                                         openldap_ldap_global_options.ldo_defhost);
270                         }
271                 } return 0;
272
273         case LDAP_OPT_ERROR_NUMBER: {
274                         int err = * (int *) invalue;
275
276                         if (err != 0 ) {
277                                 /* not supported */
278                                 /* we only allow ld_errno to be cleared. */
279                                 break;
280                         }
281
282                         if(ld == NULL) {
283                                 /* need a struct ldap */
284                                 break;
285                         }
286
287                         ld->ld_errno = err;
288                 } return 0;
289
290         case LDAP_OPT_ERROR_STRING: {
291                         char* err = * (char **) invalue;
292
293                         if (err != NULL ) {
294                                 /* not supported */
295                                 /* we only allow ld_error to be cleared. */
296                                 break;
297                         }
298
299                         if(ld == NULL) {
300                                 /* need a struct ldap */
301                                 break;
302                         }
303
304                         ld->ld_error = err;
305                 } return 0;
306
307         default:
308                 /* bad param */
309                 break;
310         }
311         return -1;
312 }