]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
5628eea1e7cb04b24f6331d7b77f139509de8e45
[openldap] / libraries / libldap / init.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <stdio.h>
9 #include <ac/stdlib.h>
10
11 #include <ac/socket.h>
12 #include <ac/string.h>
13 #include <ac/ctype.h>
14 #include <ac/time.h>
15
16 #include "ldap-int.h"
17 #include "ldap_defaults.h"
18
19 struct ldapoptions ldap_int_global_options =
20         { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };  
21
22 #undef gopts
23 #define gopts ldap_int_global_options
24
25 #define ATTR_NONE       0
26 #define ATTR_BOOL       1
27 #define ATTR_INT        2
28 #define ATTR_KV         3
29 #define ATTR_STRING     4
30 #define ATTR_TLS        5
31 #define ATTR_URIS       6
32
33 struct ol_keyvalue {
34         const char *            key;
35         int                     value;
36 };
37
38 static const struct ol_keyvalue deref_kv[] = {
39         {"never", LDAP_DEREF_NEVER},
40         {"searching", LDAP_DEREF_SEARCHING},
41         {"finding", LDAP_DEREF_FINDING},
42         {"always", LDAP_DEREF_ALWAYS},
43         {NULL, 0}
44 };
45
46 static const struct ol_attribute {
47         int                     useronly;
48         int                     type;
49         const char *    name;
50         const void *    data;
51         size_t          offset;
52 } attrs[] = {
53         {0, ATTR_KV,            "DEREF",        deref_kv, /* or &deref_kv[0] */
54                 offsetof(struct ldapoptions, ldo_deref)},
55         {0, ATTR_INT,           "SIZELIMIT",    NULL,
56                 offsetof(struct ldapoptions, ldo_sizelimit)},
57         {0, ATTR_INT,           "TIMELIMIT",    NULL,
58                 offsetof(struct ldapoptions, ldo_timelimit)},
59         {1, ATTR_STRING,        "BINDDN",               NULL,
60                 offsetof(struct ldapoptions, ldo_defbinddn)},
61         {0, ATTR_STRING,        "BASE",                 NULL,
62                 offsetof(struct ldapoptions, ldo_defbase)},
63         {0, ATTR_INT,           "PORT",                 NULL,
64                 offsetof(struct ldapoptions, ldo_defport)},
65         /* **** keep this around for backward compatibility */
66         {0, ATTR_URIS,          "HOST",                 NULL,   1},
67         /* **** */
68         {0, ATTR_URIS,          "URI",                  NULL,   0},
69         {0, ATTR_BOOL,          "REFERRALS",    NULL,   LDAP_BOOL_REFERRALS},
70         {0, ATTR_BOOL,          "RESTART",              NULL,   LDAP_BOOL_RESTART},
71         {0, ATTR_BOOL,          "DNS",                  NULL,   LDAP_BOOL_DNS},
72         {0, ATTR_TLS,           "TLS",                  NULL,   LDAP_OPT_X_TLS},
73         {0, ATTR_TLS,           "TLS_CERT",             NULL,   LDAP_OPT_X_TLS_CERTFILE},
74         {0, ATTR_TLS,           "TLS_KEY",              NULL,   LDAP_OPT_X_TLS_KEYFILE},
75         {0, ATTR_TLS,           "TLS_CACERT",   NULL,   LDAP_OPT_X_TLS_CACERTFILE},
76         {0, ATTR_TLS,           "TLS_CACERTDIR",NULL,   LDAP_OPT_X_TLS_CACERTDIR},
77         {0, ATTR_TLS,           "TLS_REQCERT",  NULL,   LDAP_OPT_X_TLS_REQUIRE_CERT},
78         {0, ATTR_NONE,          NULL,           NULL,   0}
79 };
80
81 #define MAX_LDAP_ATTR_LEN  sizeof("TLS_CACERTDIR")
82 #define MAX_LDAP_ENV_PREFIX_LEN 8
83
84 static void openldap_ldap_init_w_conf(
85         const char *file, int userconf )
86 {
87         char linebuf[128];
88         FILE *fp;
89         int i;
90         char *cmd, *opt;
91         char *start, *end;
92
93         if (file == NULL) {
94                 /* no file name */
95                 return;
96         }
97
98         fp = fopen(file, "r");
99         if(fp == NULL) {
100                 /* could not open file */
101                 return;
102         }
103
104         while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
105                 /* skip lines starting with '#' */
106                 if(*start == '#') continue;
107
108                 /* trim leading white space */
109                 while((*start != '\0') && isspace((unsigned char) *start))
110                         start++;
111
112                 /* anything left? */
113                 if(*start == '\0') continue;
114
115                 /* trim trailing white space */
116                 end = &start[strlen(start)-1];
117                 while(isspace((unsigned char)*end)) end--;
118                 end[1] = '\0';
119
120                 /* anything left? */
121                 if(*start == '\0') continue;
122                 
123
124                 /* parse the command */
125                 cmd=start;
126                 while((*start != '\0') && !isspace((unsigned char)*start)) {
127                         start++;
128                 }
129                 if(*start == '\0') {
130                         /* command has no argument */
131                         continue;
132                 } 
133
134                 *start++ = '\0';
135
136                 /* we must have some non-whitespace to skip */
137                 while(isspace((unsigned char)*start)) start++;
138                 opt = start;
139
140                 for(i=0; attrs[i].type != ATTR_NONE; i++) {
141                         void *p;
142
143                         if( !userconf && !attrs[i].useronly ) {
144                                 continue;
145                         }
146
147                         if(strcasecmp(cmd, attrs[i].name) != 0) {
148                                 continue;
149                         }
150
151                         switch(attrs[i].type) {
152                         case ATTR_BOOL:
153                                 if((strcasecmp(opt, "on") == 0) 
154                                         || (strcasecmp(opt, "yes") == 0)
155                                         || (strcasecmp(opt, "true") == 0))
156                                 {
157                                         LDAP_BOOL_SET(&gopts, attrs[i].offset);
158
159                                 } else {
160                                         LDAP_BOOL_CLR(&gopts, attrs[i].offset);
161                                 }
162
163                                 break;
164
165                         case ATTR_INT:
166                                 p = &((char *) &gopts)[attrs[i].offset];
167                                 * (int*) p = atoi(opt);
168                                 break;
169
170                         case ATTR_KV: {
171                                         const struct ol_keyvalue *kv;
172
173                                         for(kv = attrs[i].data;
174                                                 kv->key != NULL;
175                                                 kv++) {
176
177                                                 if(strcasecmp(opt, kv->key) == 0) {
178                                                         p = &((char *) &gopts)[attrs[i].offset];
179                                                         * (int*) p = kv->value;
180                                                         break;
181                                                 }
182                                         }
183                                 } break;
184
185                         case ATTR_STRING:
186                                 p = &((char *) &gopts)[attrs[i].offset];
187                                 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
188                                 * (char**) p = LDAP_STRDUP(opt);
189                                 break;
190                         case ATTR_TLS:
191 #ifdef HAVE_TLS
192                                 ldap_pvt_tls_config( &gopts, attrs[i].offset, opt );
193 #endif
194                                 break;
195                         case ATTR_URIS:
196                                 if (attrs[i].offset == 0) {
197                                         ldap_set_option( NULL, LDAP_OPT_URI, opt );
198                                 } else {
199                                         ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
200                                 }
201                                 break;
202                         }
203                 }
204         }
205
206         fclose(fp);
207 }
208
209 static void openldap_ldap_init_w_sysconf(const char *file)
210 {
211         openldap_ldap_init_w_conf( file, 0 );
212 }
213
214 static void openldap_ldap_init_w_userconf(const char *file)
215 {
216         char *home;
217         char *path;
218
219         if (file == NULL) {
220                 /* no file name */
221                 return;
222         }
223
224         home = getenv("HOME");
225
226         if (home != NULL) {
227                 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
228         } else {
229                 path = LDAP_MALLOC(strlen(file) + 3);
230         }
231
232         if(home != NULL && path != NULL) {
233                 /* we assume UNIX path syntax is used... */
234
235                 /* try ~/file */
236                 sprintf(path, "%s/%s", home, file);
237                 openldap_ldap_init_w_conf(path, 1);
238
239                 /* try ~/.file */
240                 sprintf(path, "%s/.%s", home, file);
241                 openldap_ldap_init_w_conf(path, 1);
242         }
243
244         if(path != NULL) {
245                 LDAP_FREE(path);
246         }
247
248         /* try file */
249         openldap_ldap_init_w_conf(file, 1);
250 }
251
252 static void openldap_ldap_init_w_env(const char *prefix)
253 {
254         char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
255         int len;
256         int i;
257         void *p;
258         char *value;
259
260         if (prefix == NULL) {
261                 prefix = LDAP_ENV_PREFIX;
262         }
263
264         strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
265         buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
266         len = strlen(buf);
267
268         for(i=0; attrs[i].type != ATTR_NONE; i++) {
269                 strcpy(&buf[len], attrs[i].name);
270                 value = getenv(buf);
271
272                 if(value == NULL) {
273                         continue;
274                 }
275
276                 switch(attrs[i].type) {
277                 case ATTR_BOOL:
278                         if((strcasecmp(value, "on") == 0) 
279                                 || (strcasecmp(value, "yes") == 0)
280                                 || (strcasecmp(value, "true") == 0))
281                         {
282                                 LDAP_BOOL_SET(&gopts, attrs[i].offset);
283
284                         } else {
285                                 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
286                         }
287                         break;
288
289                 case ATTR_INT:
290                         p = &((char *) &gopts)[attrs[i].offset];
291                         * (int*) p = atoi(value);
292                         break;
293
294                 case ATTR_KV: {
295                                 const struct ol_keyvalue *kv;
296
297                                 for(kv = attrs[i].data;
298                                         kv->key != NULL;
299                                         kv++) {
300
301                                         if(strcasecmp(value, kv->key) == 0) {
302                                                 p = &((char *) &gopts)[attrs[i].offset];
303                                                 * (int*) p = kv->value;
304                                                 break;
305                                         }
306                                 }
307                         } break;
308
309                 case ATTR_STRING:
310                         p = &((char *) &gopts)[attrs[i].offset];
311                         if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
312                         if (*value == '\0') {
313                                 * (char**) p = NULL;
314                         } else {
315                                 * (char**) p = LDAP_STRDUP(value);
316                         }
317                         break;
318                 case ATTR_TLS:
319 #ifdef HAVE_TLS
320                         ldap_pvt_tls_config( &gopts, attrs[i].offset, value );
321 #endif                          
322                         break;
323                 case ATTR_URIS:
324                         if (attrs[i].offset == 0) {
325                                 ldap_set_option( NULL, LDAP_OPT_URI, value );
326                         } else {
327                                 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
328                         }
329                         break;
330                 }
331         }
332 }
333
334 void ldap_int_initialize( void )
335 {
336         if ( gopts.ldo_valid == LDAP_INITIALIZED ) {
337                 return;
338         }
339
340         ldap_int_utils_init();
341
342 #ifdef HAVE_TLS
343         ldap_pvt_tls_init();
344 #endif
345
346 #ifdef HAVE_CYRUS_SASL
347         ldap_pvt_sasl_init();
348 #endif
349
350         if ( ldap_int_tblsize == 0 )
351                 ldap_int_ip_init();
352
353         gopts.ldo_debug = 0;
354
355         gopts.ldo_version =     LDAP_VERSION2;
356         gopts.ldo_deref =       LDAP_DEREF_NEVER;
357         gopts.ldo_timelimit = LDAP_NO_LIMIT;
358         gopts.ldo_sizelimit = LDAP_NO_LIMIT;
359
360         gopts.ldo_tm_api = (struct timeval *)NULL;
361         gopts.ldo_tm_net = (struct timeval *)NULL;
362
363         ldap_url_parselist(&gopts.ldo_defludp, "ldap://localhost/");
364         gopts.ldo_defport = LDAP_PORT;
365
366         gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
367
368         LDAP_BOOL_ZERO(&gopts);
369
370         LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
371
372 #ifdef HAVE_TLS
373         gopts.ldo_tls_ctx = NULL;
374 #endif
375
376         gopts.ldo_valid = LDAP_INITIALIZED;
377
378         if( getenv("LDAPNOINIT") != NULL ) {
379                 return;
380         }
381
382         openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
383         openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
384
385         {
386                 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
387
388                 if( altfile != NULL ) {
389                         openldap_ldap_init_w_sysconf( altfile );
390                 }
391         }
392
393         {
394                 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
395
396                 if( altfile != NULL ) {
397                         openldap_ldap_init_w_userconf( altfile );
398                 }
399         }
400
401         openldap_ldap_init_w_env(NULL);
402 }