]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
cleanup
[openldap] / libraries / libldap / init.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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 <limits.h>
17
18 #include "ldap-int.h"
19 #include "ldap_defaults.h"
20
21 struct ldapoptions ldap_int_global_options =
22         { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };  
23
24 #define ATTR_NONE       0
25 #define ATTR_BOOL       1
26 #define ATTR_INT        2
27 #define ATTR_KV         3
28 #define ATTR_STRING     4
29 #define ATTR_URIS       5
30
31 #define ATTR_SASL       6
32 #define ATTR_TLS        7
33
34 struct ol_keyvalue {
35         const char *            key;
36         int                     value;
37 };
38
39 static const struct ol_keyvalue deref_kv[] = {
40         {"never", LDAP_DEREF_NEVER},
41         {"searching", LDAP_DEREF_SEARCHING},
42         {"finding", LDAP_DEREF_FINDING},
43         {"always", LDAP_DEREF_ALWAYS},
44         {NULL, 0}
45 };
46
47 static const struct ol_attribute {
48         int                     useronly;
49         int                     type;
50         const char *    name;
51         const void *    data;
52         size_t          offset;
53 } attrs[] = {
54         {0, ATTR_KV,            "DEREF",        deref_kv, /* or &deref_kv[0] */
55                 offsetof(struct ldapoptions, ldo_deref)},
56         {0, ATTR_INT,           "SIZELIMIT",    NULL,
57                 offsetof(struct ldapoptions, ldo_sizelimit)},
58         {0, ATTR_INT,           "TIMELIMIT",    NULL,
59                 offsetof(struct ldapoptions, ldo_timelimit)},
60         {1, ATTR_STRING,        "BINDDN",               NULL,
61                 offsetof(struct ldapoptions, ldo_defbinddn)},
62         {0, ATTR_STRING,        "BASE",                 NULL,
63                 offsetof(struct ldapoptions, ldo_defbase)},
64         {0, ATTR_INT,           "PORT",                 NULL,           /* deprecated */
65                 offsetof(struct ldapoptions, ldo_defport)},
66         {0, ATTR_URIS,          "HOST",                 NULL,   1},     /* deprecated */
67         {0, ATTR_URIS,          "URI",                  NULL,   0}, /* replaces HOST/URI */
68         {0, ATTR_BOOL,          "REFERRALS",    NULL,   LDAP_BOOL_REFERRALS},
69         {0, ATTR_BOOL,          "RESTART",              NULL,   LDAP_BOOL_RESTART},
70
71 #ifdef HAVE_CYRUS_SASL
72         {1, ATTR_STRING,        "SASL_MECH",            NULL,
73                 offsetof(struct ldapoptions, ldo_def_sasl_mech)},
74         {1, ATTR_STRING,        "SASL_REALM",           NULL,
75                 offsetof(struct ldapoptions, ldo_def_sasl_realm)},
76         {1, ATTR_STRING,        "SASL_AUTHCID",         NULL,
77                 offsetof(struct ldapoptions, ldo_def_sasl_authcid)},
78         {1, ATTR_STRING,        "SASL_AUTHZID",         NULL,
79                 offsetof(struct ldapoptions, ldo_def_sasl_authzid)},
80         {0, ATTR_SASL,          "SASL_SECPROPS",        NULL,   LDAP_OPT_X_SASL_SECPROPS},
81 #endif
82
83 #ifdef HAVE_TLS
84         {0, ATTR_TLS,           "TLS",                  NULL,   LDAP_OPT_X_TLS},
85         {1, ATTR_TLS,           "TLS_CERT",             NULL,   LDAP_OPT_X_TLS_CERTFILE},
86         {1, ATTR_TLS,           "TLS_KEY",              NULL,   LDAP_OPT_X_TLS_KEYFILE},
87         {0, ATTR_TLS,           "TLS_CACERT",   NULL,   LDAP_OPT_X_TLS_CACERTFILE},
88         {0, ATTR_TLS,           "TLS_CACERTDIR",NULL,   LDAP_OPT_X_TLS_CACERTDIR},
89         {0, ATTR_TLS,           "TLS_REQCERT",  NULL,   LDAP_OPT_X_TLS_REQUIRE_CERT},
90         {0, ATTR_TLS,           "TLS_RANDFILE", NULL,   LDAP_OPT_X_TLS_RANDOM_FILE},
91 #endif
92
93         {0, ATTR_NONE,          NULL,           NULL,   0}
94 };
95
96 #define MAX_LDAP_ATTR_LEN  sizeof("TLS_CACERTDIR")
97 #define MAX_LDAP_ENV_PREFIX_LEN 8
98
99 static void openldap_ldap_init_w_conf(
100         const char *file, int userconf )
101 {
102         char linebuf[128];
103         FILE *fp;
104         int i;
105         char *cmd, *opt;
106         char *start, *end;
107         struct ldapoptions *gopts;
108
109         if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
110                 return;                 /* Could not allocate mem for global options */
111         }
112
113         if (file == NULL) {
114                 /* no file name */
115                 return;
116         }
117
118         Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
119
120         fp = fopen(file, "r");
121         if(fp == NULL) {
122                 /* could not open file */
123                 return;
124         }
125
126         Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
127
128         while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
129                 /* skip lines starting with '#' */
130                 if(*start == '#') continue;
131
132                 /* trim leading white space */
133                 while((*start != '\0') && isspace((unsigned char) *start))
134                         start++;
135
136                 /* anything left? */
137                 if(*start == '\0') continue;
138
139                 /* trim trailing white space */
140                 end = &start[strlen(start)-1];
141                 while(isspace((unsigned char)*end)) end--;
142                 end[1] = '\0';
143
144                 /* anything left? */
145                 if(*start == '\0') continue;
146                 
147
148                 /* parse the command */
149                 cmd=start;
150                 while((*start != '\0') && !isspace((unsigned char)*start)) {
151                         start++;
152                 }
153                 if(*start == '\0') {
154                         /* command has no argument */
155                         continue;
156                 } 
157
158                 *start++ = '\0';
159
160                 /* we must have some whitespace to skip */
161                 while(isspace((unsigned char)*start)) start++;
162                 opt = start;
163
164                 for(i=0; attrs[i].type != ATTR_NONE; i++) {
165                         void *p;
166
167                         if( !userconf && attrs[i].useronly ) {
168                                 continue;
169                         }
170
171                         if(strcasecmp(cmd, attrs[i].name) != 0) {
172                                 continue;
173                         }
174
175                         switch(attrs[i].type) {
176                         case ATTR_BOOL:
177                                 if((strcasecmp(opt, "on") == 0) 
178                                         || (strcasecmp(opt, "yes") == 0)
179                                         || (strcasecmp(opt, "true") == 0))
180                                 {
181                                         LDAP_BOOL_SET(gopts, attrs[i].offset);
182
183                                 } else {
184                                         LDAP_BOOL_CLR(gopts, attrs[i].offset);
185                                 }
186
187                                 break;
188
189                         case ATTR_INT:
190                                 p = &((char *) gopts)[attrs[i].offset];
191                                 * (int*) p = atoi(opt);
192                                 break;
193
194                         case ATTR_KV: {
195                                         const struct ol_keyvalue *kv;
196
197                                         for(kv = attrs[i].data;
198                                                 kv->key != NULL;
199                                                 kv++) {
200
201                                                 if(strcasecmp(opt, kv->key) == 0) {
202                                                         p = &((char *) gopts)[attrs[i].offset];
203                                                         * (int*) p = kv->value;
204                                                         break;
205                                                 }
206                                         }
207                                 } break;
208
209                         case ATTR_STRING:
210                                 p = &((char *) gopts)[attrs[i].offset];
211                                 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
212                                 * (char**) p = LDAP_STRDUP(opt);
213                                 break;
214                         case ATTR_URIS:
215                                 if (attrs[i].offset == 0) {
216                                         ldap_set_option( NULL, LDAP_OPT_URI, opt );
217                                 } else {
218                                         ldap_set_option( NULL, LDAP_OPT_HOST_NAME, opt );
219                                 }
220                                 break;
221                         case ATTR_SASL:
222 #ifdef HAVE_CYRUS_SASL
223                                 ldap_int_sasl_config( gopts, attrs[i].offset, opt );
224 #endif
225                                 break;
226                         case ATTR_TLS:
227 #ifdef HAVE_TLS
228                                 ldap_int_tls_config( gopts, attrs[i].offset, opt );
229 #endif
230                                 break;
231                         }
232
233                         break;
234                 }
235         }
236
237         fclose(fp);
238 }
239
240 static void openldap_ldap_init_w_sysconf(const char *file)
241 {
242         openldap_ldap_init_w_conf( file, 0 );
243 }
244
245 static void openldap_ldap_init_w_userconf(const char *file)
246 {
247         char *home;
248         char *path = NULL;
249
250         if (file == NULL) {
251                 /* no file name */
252                 return;
253         }
254
255         home = getenv("HOME");
256
257         if (home != NULL) {
258                 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
259                       home, 0, 0);
260                 path = LDAP_MALLOC(strlen(home) + strlen(file) + 3);
261         } else {
262                 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
263                       0, 0, 0);
264         }
265
266         if(home != NULL && path != NULL) {
267                 /* we assume UNIX path syntax is used... */
268
269                 /* try ~/file */
270                 sprintf(path, "%s/%s", home, file);
271                 openldap_ldap_init_w_conf(path, 1);
272
273                 /* try ~/.file */
274                 sprintf(path, "%s/.%s", home, file);
275                 openldap_ldap_init_w_conf(path, 1);
276         }
277
278         if(path != NULL) {
279                 LDAP_FREE(path);
280         }
281
282         /* try file */
283         openldap_ldap_init_w_conf(file, 1);
284 }
285
286 static void openldap_ldap_init_w_env(
287                 struct ldapoptions *gopts,
288                 const char *prefix)
289 {
290         char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
291         int len;
292         int i;
293         void *p;
294         char *value;
295
296         if (prefix == NULL) {
297                 prefix = LDAP_ENV_PREFIX;
298         }
299
300         strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
301         buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
302         len = strlen(buf);
303
304         for(i=0; attrs[i].type != ATTR_NONE; i++) {
305                 strcpy(&buf[len], attrs[i].name);
306                 value = getenv(buf);
307
308                 if(value == NULL) {
309                         continue;
310                 }
311
312                 switch(attrs[i].type) {
313                 case ATTR_BOOL:
314                         if((strcasecmp(value, "on") == 0) 
315                                 || (strcasecmp(value, "yes") == 0)
316                                 || (strcasecmp(value, "true") == 0))
317                         {
318                                 LDAP_BOOL_SET(gopts, attrs[i].offset);
319
320                         } else {
321                                 LDAP_BOOL_CLR(gopts, attrs[i].offset);
322                         }
323                         break;
324
325                 case ATTR_INT:
326                         p = &((char *) gopts)[attrs[i].offset];
327                         * (int*) p = atoi(value);
328                         break;
329
330                 case ATTR_KV: {
331                                 const struct ol_keyvalue *kv;
332
333                                 for(kv = attrs[i].data;
334                                         kv->key != NULL;
335                                         kv++) {
336
337                                         if(strcasecmp(value, kv->key) == 0) {
338                                                 p = &((char *) gopts)[attrs[i].offset];
339                                                 * (int*) p = kv->value;
340                                                 break;
341                                         }
342                                 }
343                         } break;
344
345                 case ATTR_STRING:
346                         p = &((char *) gopts)[attrs[i].offset];
347                         if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
348                         if (*value == '\0') {
349                                 * (char**) p = NULL;
350                         } else {
351                                 * (char**) p = LDAP_STRDUP(value);
352                         }
353                         break;
354                 case ATTR_URIS:
355                         if (attrs[i].offset == 0) {
356                                 ldap_set_option( NULL, LDAP_OPT_URI, value );
357                         } else {
358                                 ldap_set_option( NULL, LDAP_OPT_HOST_NAME, value );
359                         }
360                         break;
361                 case ATTR_SASL:
362 #ifdef HAVE_CYRUS_SASL
363                         ldap_int_sasl_config( gopts, attrs[i].offset, value );
364 #endif                          
365                         break;
366                 case ATTR_TLS:
367 #ifdef HAVE_TLS
368                         ldap_int_tls_config( gopts, attrs[i].offset, value );
369 #endif                          
370                         break;
371                 }
372         }
373 }
374
375 /* 
376  * Initialize the global options structure with default values.
377  */
378 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
379 {
380         if (dbglvl)
381             gopts->ldo_debug = *dbglvl;
382         else
383                 gopts->ldo_debug = 0;
384
385         gopts->ldo_version   = LDAP_VERSION2;
386         gopts->ldo_deref     = LDAP_DEREF_NEVER;
387         gopts->ldo_timelimit = LDAP_NO_LIMIT;
388         gopts->ldo_sizelimit = LDAP_NO_LIMIT;
389
390         gopts->ldo_tm_api = (struct timeval *)NULL;
391         gopts->ldo_tm_net = (struct timeval *)NULL;
392
393         /* ldo_defludp is leaked, we should have an at_exit() handler
394          * to free this and whatever else needs to cleaned up. 
395          */
396         ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
397         gopts->ldo_defport = LDAP_PORT;
398
399         gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
400         gopts->ldo_rebindproc = NULL;
401
402         LDAP_BOOL_ZERO(gopts);
403
404         LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
405
406 #ifdef HAVE_CYRUS_SASL
407         gopts->ldo_def_sasl_mech = NULL;
408         gopts->ldo_def_sasl_realm = NULL;
409         gopts->ldo_def_sasl_authcid = NULL;
410         gopts->ldo_def_sasl_authzid = NULL;
411
412         memset( &gopts->ldo_sasl_secprops, '\0', sizeof(gopts->ldo_sasl_secprops) );
413
414         gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
415         gopts->ldo_sasl_secprops.maxbufsize = 65536;
416         gopts->ldo_sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
417 #endif
418
419 #ifdef HAVE_TLS
420         gopts->ldo_tls_ctx = NULL;
421 #endif
422
423         gopts->ldo_valid = LDAP_INITIALIZED;
424
425         return;
426 }
427
428 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
429         || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
430 char * ldap_int_hostname = NULL;
431 #endif
432
433 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
434 {
435         if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
436                 return;
437         }
438
439 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
440         || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
441         ldap_int_hostname = ldap_pvt_get_fqdn( ldap_int_hostname );
442 #endif
443
444         ldap_int_utils_init();
445
446         if ( ldap_int_tblsize == 0 )
447                 ldap_int_ip_init();
448
449         ldap_int_initialize_global_options(gopts, NULL);
450
451         if( getenv("LDAPNOINIT") != NULL ) {
452                 return;
453         }
454
455 #ifdef HAVE_CYRUS_SASL
456         {
457                 /* set authentication identity to current user name */
458                 char *user = getenv("USER");
459
460                 if( user == NULL ) user = getenv("USERNAME");
461                 if( user == NULL ) user = getenv("LOGNAME");
462
463                 if( user != NULL ) {
464                         /* this value is leaked, need at_exit() handler */
465                         gopts->ldo_def_sasl_authcid = LDAP_STRDUP( user );
466                 }
467     }
468 #endif
469
470         openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
471         openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
472
473         {
474                 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
475
476                 if( altfile != NULL ) {
477                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
478                               LDAP_ENV_PREFIX "CONF", altfile, 0);
479                         openldap_ldap_init_w_sysconf( altfile );
480                 }
481                 else
482                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
483                               LDAP_ENV_PREFIX "CONF", 0, 0);
484         }
485
486         {
487                 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
488
489                 if( altfile != NULL ) {
490                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
491                               LDAP_ENV_PREFIX "RC", altfile, 0);
492                         openldap_ldap_init_w_userconf( altfile );
493                 }
494                 else
495                         Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
496                               LDAP_ENV_PREFIX "RC", 0, 0);
497         }
498
499         openldap_ldap_init_w_env(gopts, NULL);
500
501         ldap_int_sasl_init();
502 }