]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
049274d54a090c8bade0efb490cd3c9341827d7e
[openldap] / libraries / libldap / init.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 #include <ac/ctype.h>
9 #include <ac/time.h>
10
11 #include "ldap-int.h"
12 #include "ldapconfig.h"
13
14 struct ldapoptions openldap_ldap_global_options; 
15
16 #undef gopts
17 #define gopts openldap_ldap_global_options
18
19 int     openldap_ldap_initialized = 0;
20
21 #define ATTR_NONE       0
22 #define ATTR_BOOL       1
23 #define ATTR_INT        2
24 #define ATTR_KV         3
25 #define ATTR_STRING     4
26
27 struct ol_keyvalue {
28         char*           key;
29         int                     value;
30 };
31
32 static struct ol_keyvalue deref_kv[] = {
33         {"never", LDAP_DEREF_NEVER},
34         {"searching", LDAP_DEREF_SEARCHING},
35         {"finding", LDAP_DEREF_FINDING},
36         {"always", LDAP_DEREF_ALWAYS},
37         {NULL, 0}
38 };
39
40 static struct ol_attribute {
41         int                     type;
42         char*           name;
43         void*           data;
44         size_t          offset;
45 } attrs[] = {
46         {ATTR_KV,               "DEREF",        deref_kv, /* or &deref_kv[0] */
47                 offsetof(struct ldapoptions, ldo_deref)},
48         {ATTR_INT,              "SIZELIMIT",    NULL,
49                 offsetof(struct ldapoptions, ldo_sizelimit)},
50         {ATTR_INT,              "TIMELIMIT",    NULL,
51                 offsetof(struct ldapoptions, ldo_timelimit)},
52         {ATTR_STRING,   "BASE",                 NULL,
53                 offsetof(struct ldapoptions, ldo_defbase)},
54         {ATTR_STRING,   "HOST",                 NULL,
55                 offsetof(struct ldapoptions, ldo_defhost)},
56         {ATTR_INT,              "PORT",                 NULL,
57                 offsetof(struct ldapoptions, ldo_defport)},
58         {ATTR_BOOL,             "REFERRALS",    (void *) LDAP_BOOL_REFERRALS, 0},
59         {ATTR_BOOL,             "RESTART",              (void *) LDAP_BOOL_RESTART, 0},
60         {ATTR_BOOL,             "DNS",                  (void *) LDAP_BOOL_DNS, 0},
61         {ATTR_NONE,             NULL,           NULL,   0}
62 };
63
64 #define MAX_LDAP_ATTR_LEN  sizeof("SIZELIMIT")
65 #define MAX_LDAP_ENV_PREFIX_LEN 8
66
67 static void openldap_ldap_init_w_conf(const char *file)
68 {
69         char linebuf[128];
70         FILE *fp;
71         int i;
72         char *cmd, *opt;
73         char *start, *end;
74
75         fp = fopen(file, "r");
76         if(fp == NULL) {
77                 /* could not open file */
78                 return;
79         }
80
81         while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
82                 /* skip lines starting with '#' */
83                 if(*start == '#') continue;
84
85                 /* trim leading white space */
86                 while((*start != '\0') && isspace(*start)) start++;
87
88                 /* anything left? */
89                 if(*start == '\0') continue;
90
91                 /* trim trailing white space */
92                 end = &start[strlen(start)-1];
93                 while(isspace(*end)) end--;
94                 end[1] = '\0';
95
96                 /* anything left? */
97                 if(*start == '\0') continue;
98                 
99
100                 /* parse the command */
101                 cmd=start;
102                 while((*start != '\0') && !isspace(*start)) {
103                         start++;
104                 }
105                 if(*start == '\0') {
106                         /* command has no argument */
107                         continue;
108                 } 
109
110                 *start++ = '\0';
111
112                 /* we must have some non-whitespace to skip */
113                 while(isspace(*start)) start++;
114                 opt = start;
115
116                 for(i=0; attrs[i].type != ATTR_NONE; i++) {
117                         void *p;
118
119                         if(strcasecmp(cmd, attrs[i].name) != 0) {
120                                 continue;
121                         }
122
123                         p = &((char *) &gopts)[attrs[i].offset];
124
125                         switch(attrs[i].type) {
126                         case ATTR_BOOL:
127                                 if((strcasecmp(opt, "on") == 0) 
128                                         || (strcasecmp(opt, "yes") == 0)
129                                         || (strcasecmp(opt, "true") == 0))
130                                 {
131                                         LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
132
133                                 } else {
134                                         LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
135                                 }
136
137                                 break;
138
139                         case ATTR_INT:
140                                 * (int*) p = atoi(opt);
141                                 break;
142
143                         case ATTR_KV: {
144                                         struct ol_keyvalue *kv;
145
146                                         for(kv = (struct ol_keyvalue *) attrs[i].data;
147                                                 kv->key != NULL;
148                                                 kv++) {
149
150                                                 if(strcasecmp(opt, kv->key) == 0) {
151                                                         * (int*) p = kv->value;
152                                                         break;
153                                                 }
154                                         }
155                                 } break;
156
157                         case ATTR_STRING:
158                                 if (* (char**) p != NULL) free(* (char**) p);
159                                 * (char**) p = strdup(opt);
160                                 break;
161                         }
162                 }
163         }
164 }
165
166 static void openldap_ldap_init_w_userconf(const char *file)
167 {
168         char *home = getenv("HOME");
169         char *path;
170         
171         if (home != NULL) {
172                 path = malloc(strlen(home) + strlen(file) + 3);
173         } else {
174                 path = malloc(strlen(file) + 3);
175         }
176
177
178         if(home != NULL && path != NULL) {
179                 /* try ~/file */
180                 sprintf(path, "%s/%s", home, file);
181                 openldap_ldap_init_w_conf(path);
182
183                 /* try ~/.file */
184                 sprintf(path, "%s/.%s", home, file);
185                 openldap_ldap_init_w_conf(path);
186         }
187
188         /* try file */
189         openldap_ldap_init_w_conf(file);
190
191         if(path == NULL) {
192                 /* try .file */
193                 sprintf(path, ".%s", file);
194                 openldap_ldap_init_w_conf(path);
195         }
196 }
197
198 static void openldap_ldap_init_w_env(const char *prefix)
199 {
200         char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
201         int len;
202         int i;
203         void *p;
204         char *value;
205
206         if (prefix == NULL) {
207                 prefix = DEFAULT_LDAP_ENV_PREFIX;
208         }
209
210         strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
211         buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
212         len = strlen(buf);
213
214         for(i=0; attrs[i].type != ATTR_NONE; i++) {
215                 strcpy(&buf[len], attrs[i].name);
216                 value = getenv(buf);
217
218                 if(value == NULL) {
219                         continue;
220                 }
221
222                 p = &((char *) &gopts)[attrs[i].offset];
223
224                 switch(attrs[i].type) {
225                 case ATTR_BOOL:
226                         if((strcasecmp(value, "on") == 0) 
227                                 || (strcasecmp(value, "yes") == 0)
228                                 || (strcasecmp(value, "true") == 0))
229                         {
230                                 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
231
232                         } else {
233                                 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
234                         }
235                         break;
236
237                 case ATTR_INT:
238                         * (int*) p = atoi(value);
239                         break;
240
241                 case ATTR_KV: {
242                                 struct ol_keyvalue *kv;
243
244                                 for(kv = (struct ol_keyvalue *) attrs[i].data;
245                                         kv->key != NULL;
246                                         kv++) {
247
248                                         if(strcasecmp(value, kv->key) == 0) {
249                                                 * (int*) p = kv->value;
250                                                 break;
251                                         }
252                                 }
253                         } break;
254
255                 case ATTR_STRING:
256                         if (* (char**) p != NULL) free(* (char**) p);
257                         if (*value == '\0') {
258                                 * (char**) p = NULL;
259                         } else {
260                                 * (char**) p = strdup(value);
261                         }
262                         break;
263                 }
264         }
265 }
266
267 void openldap_ldap_initialize( void )
268 {
269         if ( openldap_ldap_initialized ) {
270                 return;
271         }
272
273         gopts.ldo_version =     LDAP_VERSION2;
274         gopts.ldo_deref =       LDAP_DEREF_NEVER;
275         gopts.ldo_timelimit = LDAP_NO_LIMIT;
276         gopts.ldo_sizelimit = LDAP_NO_LIMIT;
277
278         gopts.ldo_defhost = strdup("localhost");
279         gopts.ldo_defport = LDAP_PORT;
280
281         gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
282
283         LDAP_BOOL_ZERO(&gopts);
284
285 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || \
286         LDAP_VERSION_MAX > LDAP_VERSION2
287         LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
288 #endif
289
290         openldap_ldap_init_w_conf(DEFAULT_LDAP_CONF_FILE);
291         openldap_ldap_init_w_userconf(DEFAULT_LDAP_USERRC_FILE);
292         openldap_ldap_init_w_env(NULL);
293
294         openldap_ldap_initialized = 1;
295 }