]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
Fix NULL pointer deref bugs
[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 #ifdef HAVE_CYRUS_SASL
79         {0, ATTR_INT,           "SASL_MINSSF",  NULL,
80                 offsetof(struct ldapoptions, ldo_sasl_minssf)},
81         {0, ATTR_INT,           "SASL_MAXSSF",  NULL,
82                 offsetof(struct ldapoptions, ldo_sasl_maxssf)},
83 #endif
84         {0, ATTR_NONE,          NULL,           NULL,   0}
85 };
86
87 #define MAX_LDAP_ATTR_LEN  sizeof("TLS_CACERTDIR")
88 #define MAX_LDAP_ENV_PREFIX_LEN 8
89
90 static void openldap_ldap_init_w_conf(
91         const char *file, int userconf )
92 {
93         char linebuf[128];
94         FILE *fp;
95         int i;
96         char *cmd, *opt;
97         char *start, *end;
98
99         if (file == NULL) {
100                 /* no file name */
101                 return;
102         }
103
104         fp = fopen(file, "r");
105         if(fp == NULL) {
106                 /* could not open file */
107                 return;
108         }
109
110         while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
111                 /* skip lines starting with '#' */
112                 if(*start == '#') continue;
113
114                 /* trim leading white space */
115                 while((*start != '\0') && isspace((unsigned char) *start))
116                         start++;
117
118                 /* anything left? */
119                 if(*start == '\0') continue;
120
121                 /* trim trailing white space */
122                 end = &start[strlen(start)-1];
123                 while(isspace((unsigned char)*end)) end--;
124                 end[1] = '\0';
125
126                 /* anything left? */
127                 if(*start == '\0') continue;
128                 
129
130                 /* parse the command */
131                 cmd=start;
132                 while((*start != '\0') && !isspace((unsigned char)*start)) {
133                         start++;
134                 }
135                 if(*start == '\0') {
136                         /* command has no argument */
137                         continue;
138                 } 
139
140                 *start++ = '\0';
141
142                 /* we must have some whitespace to skip */
143                 while(isspace((unsigned char)*start)) start++;
144                 opt = start;
145
146                 for(i=0; attrs[i].type != ATTR_NONE; i++) {
147                         void *p;
148
149                         if( !userconf && attrs[i].useronly ) {
150                                 continue;
151                         }
152
153                         if(strcasecmp(cmd, attrs[i].name) != 0) {
154                                 continue;
155                         }
156
157                         switch(attrs[i].type) {
158                         case ATTR_BOOL:
159                                 if((strcasecmp(opt, "on") == 0) 
160                                         || (strcasecmp(opt, "yes") == 0)
161                                         || (strcasecmp(opt, "true") == 0))
162                                 {
163                                         LDAP_BOOL_SET(&gopts, attrs[i].offset);
164
165                                 } else {
166                                         LDAP_BOOL_CLR(&gopts, attrs[i].offset);
167                                 }
168
169                                 break;
170
171                         case ATTR_INT:
172                                 p = &((char *) &gopts)[attrs[i].offset];
173                                 * (int*) p = atoi(opt);
174                                 break;
175
176                         case ATTR_KV: {
177                                         const struct ol_keyvalue *kv;
178
179                                         for(kv = attrs[i].data;
180                                                 kv->key != NULL;
181                                                 kv++) {
182
183                                                 if(strcasecmp(opt, kv->key) == 0) {
184                                                         p = &((char *) &gopts)[attrs[i].offset];
185                                                         * (int*) p = kv->value;
186                                                         break;
187                                                 }
188                                         }
189                                 } break;
190
191                         case ATTR_STRING:
192                                 p = &((char *) &gopts)[attrs[i].offset];
193                                 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
194                                 * (char**) p = LDAP_STRDUP(opt);
195                                 break;
196                         case ATTR_TLS:
197 #ifdef HAVE_TLS
198                                 ldap_pvt_tls_config( &gopts, attrs[i].offset, opt );
199 #endif
200                                 break;
201                         case ATTR_URIS:
202                                 if (attrs[i].offset == 0) {
203                                         ldap_set_option( NULL, LDAP_OPT_URI, opt );
204                                 } else {
205                                         ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
206                                 }
207                                 break;
208                         }
209                 }
210         }
211
212         fclose(fp);
213 }
214
215 static void openldap_ldap_init_w_sysconf(const char *file)
216 {
217         openldap_ldap_init_w_conf( file, 0 );
218 }
219
220 static void openldap_ldap_init_w_userconf(const char *file)
221 {
222         char *home;
223         char *path;
224
225         if (file == NULL) {
226                 /* no file name */
227                 return;
228         }
229
230         home = getenv("HOME");
231
232         if (home != NULL) {
233                 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
234         } else {
235                 path = LDAP_MALLOC(strlen(file) + 3);
236         }
237
238         if(home != NULL && path != NULL) {
239                 /* we assume UNIX path syntax is used... */
240
241                 /* try ~/file */
242                 sprintf(path, "%s/%s", home, file);
243                 openldap_ldap_init_w_conf(path, 1);
244
245                 /* try ~/.file */
246                 sprintf(path, "%s/.%s", home, file);
247                 openldap_ldap_init_w_conf(path, 1);
248         }
249
250         if(path != NULL) {
251                 LDAP_FREE(path);
252         }
253
254         /* try file */
255         openldap_ldap_init_w_conf(file, 1);
256 }
257
258 static void openldap_ldap_init_w_env(const char *prefix)
259 {
260         char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
261         int len;
262         int i;
263         void *p;
264         char *value;
265
266         if (prefix == NULL) {
267                 prefix = LDAP_ENV_PREFIX;
268         }
269
270         strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
271         buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
272         len = strlen(buf);
273
274         for(i=0; attrs[i].type != ATTR_NONE; i++) {
275                 strcpy(&buf[len], attrs[i].name);
276                 value = getenv(buf);
277
278                 if(value == NULL) {
279                         continue;
280                 }
281
282                 switch(attrs[i].type) {
283                 case ATTR_BOOL:
284                         if((strcasecmp(value, "on") == 0) 
285                                 || (strcasecmp(value, "yes") == 0)
286                                 || (strcasecmp(value, "true") == 0))
287                         {
288                                 LDAP_BOOL_SET(&gopts, attrs[i].offset);
289
290                         } else {
291                                 LDAP_BOOL_CLR(&gopts, attrs[i].offset);
292                         }
293                         break;
294
295                 case ATTR_INT:
296                         p = &((char *) &gopts)[attrs[i].offset];
297                         * (int*) p = atoi(value);
298                         break;
299
300                 case ATTR_KV: {
301                                 const struct ol_keyvalue *kv;
302
303                                 for(kv = attrs[i].data;
304                                         kv->key != NULL;
305                                         kv++) {
306
307                                         if(strcasecmp(value, kv->key) == 0) {
308                                                 p = &((char *) &gopts)[attrs[i].offset];
309                                                 * (int*) p = kv->value;
310                                                 break;
311                                         }
312                                 }
313                         } break;
314
315                 case ATTR_STRING:
316                         p = &((char *) &gopts)[attrs[i].offset];
317                         if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
318                         if (*value == '\0') {
319                                 * (char**) p = NULL;
320                         } else {
321                                 * (char**) p = LDAP_STRDUP(value);
322                         }
323                         break;
324                 case ATTR_TLS:
325 #ifdef HAVE_TLS
326                         ldap_pvt_tls_config( &gopts, attrs[i].offset, value );
327 #endif                          
328                         break;
329                 case ATTR_URIS:
330                         if (attrs[i].offset == 0) {
331                                 ldap_set_option( NULL, LDAP_OPT_URI, value );
332                         } else {
333                                 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
334                         }
335                         break;
336                 }
337         }
338 }
339
340 void ldap_int_initialize( void )
341 {
342         if ( gopts.ldo_valid == LDAP_INITIALIZED ) {
343                 return;
344         }
345
346         ldap_int_utils_init();
347
348 #ifdef HAVE_TLS
349         ldap_pvt_tls_init();
350 #endif
351
352 #ifdef HAVE_CYRUS_SASL
353         ldap_pvt_sasl_init();
354 #endif
355
356         if ( ldap_int_tblsize == 0 )
357                 ldap_int_ip_init();
358
359         gopts.ldo_debug = 0;
360
361         gopts.ldo_version =     LDAP_VERSION2;
362         gopts.ldo_deref =       LDAP_DEREF_NEVER;
363         gopts.ldo_timelimit = LDAP_NO_LIMIT;
364         gopts.ldo_sizelimit = LDAP_NO_LIMIT;
365
366         gopts.ldo_tm_api = (struct timeval *)NULL;
367         gopts.ldo_tm_net = (struct timeval *)NULL;
368
369         /* ldo_defludp is leaked, we should have an at_exit() handler
370          * to free this and whatever else needs to cleaned up. 
371          */
372         ldap_url_parselist(&gopts.ldo_defludp, "ldap://localhost/");
373         gopts.ldo_defport = LDAP_PORT;
374
375         gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
376
377         LDAP_BOOL_ZERO(&gopts);
378
379         LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
380
381 #ifdef HAVE_TLS
382         gopts.ldo_tls_ctx = NULL;
383 #endif
384 #ifdef HAVE_CYRUS_SASL
385         gopts.ldo_sasl_minssf = 0;
386         gopts.ldo_sasl_maxssf = INT_MAX;
387 #endif
388
389         gopts.ldo_valid = LDAP_INITIALIZED;
390
391         if( getenv("LDAPNOINIT") != NULL ) {
392                 return;
393         }
394
395         openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
396         openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
397
398         {
399                 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
400
401                 if( altfile != NULL ) {
402                         openldap_ldap_init_w_sysconf( altfile );
403                 }
404         }
405
406         {
407                 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
408
409                 if( altfile != NULL ) {
410                         openldap_ldap_init_w_userconf( altfile );
411                 }
412         }
413
414         openldap_ldap_init_w_env(NULL);
415 }