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