]> git.sur5r.net Git - openldap/blob - libraries/libldap/options.c
18b8fa6a2cf73255e109d2dd10fe61f8e1371ed8
[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) {
116                         /* bad param */
117                         break;
118                 } 
119
120                 * (int *) outvalue = ld->ld_version;
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                 if(ld == NULL) {
130                         /* bad param */
131                         break;
132                 } 
133                 * (char **) outvalue = ld->ld_host;
134                 return 0;
135
136         case LDAP_OPT_ERROR_NUMBER:
137                 if(ld == NULL) {
138                         /* bad param */
139                         break;
140                 } 
141                 * (int *) outvalue = ld->ld_errno;
142                 return 0;
143
144         case LDAP_OPT_ERROR_STRING:
145                 /* not yet supported */
146                 if(ld == NULL) {
147                         /* bad param */
148                         break;
149                 } 
150                 break;
151
152         default:
153                 /* bad param */
154                 break;
155         }
156
157         return -1;
158 }
159
160 int
161 ldap_set_option(
162         LDAP    *ld,
163         int             option,
164         void    *invalue)
165 {
166         struct ldapoptions *lo;
167
168         if(!openldap_ldap_initialized) {
169                 openldap_ldap_initialize();
170         }
171
172         if(invalue == NULL) {
173                 /* no place to set from */
174                 return -1;
175         }
176
177         if(ld == NULL) {
178                 lo = &openldap_ldap_global_options;
179         } else {
180                 lo = &ld->ld_options;
181         }
182
183         switch(option) {
184         case LDAP_OPT_API_INFO:
185         case LDAP_OPT_DESC:
186                 /* READ ONLY */
187                 break;
188
189         case LDAP_OPT_DEREF:
190                 lo->ldo_deref = * (int *) invalue;
191                 return 0;
192
193         case LDAP_OPT_SIZELIMIT:
194                 lo->ldo_sizelimit = * (int *) invalue;
195                 return 0;
196
197         case LDAP_OPT_TIMELIMIT:
198                 lo->ldo_timelimit = * (int *) invalue;
199                 return 0;
200
201         case LDAP_OPT_REFERRALS:
202                 if((int) invalue == (int) LDAP_OPT_ON) {
203                         LDAP_BOOL_SET(lo, LDAP_BOOL_REFERRALS);
204                 } else {
205                         LDAP_BOOL_CLR(lo, LDAP_BOOL_REFERRALS);
206                 }
207                 return 0;
208
209         case LDAP_OPT_RESTART:
210                 if((int) invalue == (int) LDAP_OPT_ON) {
211                         LDAP_BOOL_SET(lo, LDAP_BOOL_RESTART);
212                 } else {
213                         LDAP_BOOL_CLR(lo, LDAP_BOOL_RESTART);
214                 }
215                 return 0;
216
217         case LDAP_OPT_PROTOCOL_VERSION: {
218                         int vers = * (int *) invalue;
219                         if (vers < LDAP_VERSION_MIN || vers > LDAP_VERSION_MAX) {
220                                 /* not supported */
221                                 break;
222                         }
223                         ld->ld_version = vers;
224                 } return 0;
225
226         case LDAP_OPT_SERVER_CONTROLS:
227         case LDAP_OPT_CLIENT_CONTROLS:
228         case LDAP_OPT_HOST_NAME:
229         case LDAP_OPT_ERROR_NUMBER:
230         case LDAP_OPT_ERROR_STRING:
231                 /* not yet supported */
232                 break;
233         default:
234                 /* bad param */
235                 break;
236         }
237         return -1;
238 }