2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
19 #include <ac/stdlib.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
31 #include "ldap_defaults.h"
33 struct ldapoptions ldap_int_global_options =
34 { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };
51 static const struct ol_keyvalue deref_kv[] = {
52 {"never", LDAP_DEREF_NEVER},
53 {"searching", LDAP_DEREF_SEARCHING},
54 {"finding", LDAP_DEREF_FINDING},
55 {"always", LDAP_DEREF_ALWAYS},
59 static const struct ol_attribute {
66 {0, ATTR_KV, "DEREF", deref_kv, /* or &deref_kv[0] */
67 offsetof(struct ldapoptions, ldo_deref)},
68 {0, ATTR_INT, "SIZELIMIT", NULL,
69 offsetof(struct ldapoptions, ldo_sizelimit)},
70 {0, ATTR_INT, "TIMELIMIT", NULL,
71 offsetof(struct ldapoptions, ldo_timelimit)},
72 {1, ATTR_STRING, "BINDDN", NULL,
73 offsetof(struct ldapoptions, ldo_defbinddn)},
74 {0, ATTR_STRING, "BASE", NULL,
75 offsetof(struct ldapoptions, ldo_defbase)},
76 {0, ATTR_INT, "PORT", NULL, /* deprecated */
77 offsetof(struct ldapoptions, ldo_defport)},
78 {0, ATTR_OPTION, "HOST", NULL, LDAP_OPT_HOST_NAME}, /* deprecated */
79 {0, ATTR_OPTION, "URI", NULL, LDAP_OPT_URI}, /* replaces HOST/PORT */
80 {0, ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
81 {0, ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
83 #ifdef HAVE_CYRUS_SASL
84 {1, ATTR_STRING, "SASL_MECH", NULL,
85 offsetof(struct ldapoptions, ldo_def_sasl_mech)},
86 {1, ATTR_STRING, "SASL_REALM", NULL,
87 offsetof(struct ldapoptions, ldo_def_sasl_realm)},
88 {1, ATTR_STRING, "SASL_AUTHCID", NULL,
89 offsetof(struct ldapoptions, ldo_def_sasl_authcid)},
90 {1, ATTR_STRING, "SASL_AUTHZID", NULL,
91 offsetof(struct ldapoptions, ldo_def_sasl_authzid)},
92 {0, ATTR_SASL, "SASL_SECPROPS", NULL, LDAP_OPT_X_SASL_SECPROPS},
96 {1, ATTR_TLS, "TLS_CERT", NULL, LDAP_OPT_X_TLS_CERTFILE},
97 {1, ATTR_TLS, "TLS_KEY", NULL, LDAP_OPT_X_TLS_KEYFILE},
98 {0, ATTR_TLS, "TLS_CACERT", NULL, LDAP_OPT_X_TLS_CACERTFILE},
99 {0, ATTR_TLS, "TLS_CACERTDIR", NULL, LDAP_OPT_X_TLS_CACERTDIR},
100 {0, ATTR_TLS, "TLS_REQCERT", NULL, LDAP_OPT_X_TLS_REQUIRE_CERT},
101 {0, ATTR_TLS, "TLS_RANDFILE", NULL, LDAP_OPT_X_TLS_RANDOM_FILE},
102 {0, ATTR_TLS, "TLS_CIPHER_SUITE", NULL, LDAP_OPT_X_TLS_CIPHER_SUITE},
104 #ifdef HAVE_OPENSSL_CRL
105 {0, ATTR_TLS, "TLS_CRLCHECK", NULL, LDAP_OPT_X_TLS_CRLCHECK},
110 {0, ATTR_NONE, NULL, NULL, 0}
113 #define MAX_LDAP_ATTR_LEN sizeof("TLS_CIPHER_SUITE")
114 #define MAX_LDAP_ENV_PREFIX_LEN 8
116 static void openldap_ldap_init_w_conf(
117 const char *file, int userconf )
124 struct ldapoptions *gopts;
126 if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
127 return; /* Could not allocate mem for global options */
135 Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
137 fp = fopen(file, "r");
139 /* could not open file */
143 Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
145 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
146 /* skip lines starting with '#' */
147 if(*start == '#') continue;
149 /* trim leading white space */
150 while((*start != '\0') && isspace((unsigned char) *start))
154 if(*start == '\0') continue;
156 /* trim trailing white space */
157 end = &start[strlen(start)-1];
158 while(isspace((unsigned char)*end)) end--;
162 if(*start == '\0') continue;
165 /* parse the command */
167 while((*start != '\0') && !isspace((unsigned char)*start)) {
171 /* command has no argument */
177 /* we must have some whitespace to skip */
178 while(isspace((unsigned char)*start)) start++;
181 for(i=0; attrs[i].type != ATTR_NONE; i++) {
184 if( !userconf && attrs[i].useronly ) {
188 if(strcasecmp(cmd, attrs[i].name) != 0) {
192 switch(attrs[i].type) {
194 if((strcasecmp(opt, "on") == 0)
195 || (strcasecmp(opt, "yes") == 0)
196 || (strcasecmp(opt, "true") == 0))
198 LDAP_BOOL_SET(gopts, attrs[i].offset);
201 LDAP_BOOL_CLR(gopts, attrs[i].offset);
207 p = &((char *) gopts)[attrs[i].offset];
208 * (int*) p = atoi(opt);
212 const struct ol_keyvalue *kv;
214 for(kv = attrs[i].data;
218 if(strcasecmp(opt, kv->key) == 0) {
219 p = &((char *) gopts)[attrs[i].offset];
220 * (int*) p = kv->value;
227 p = &((char *) gopts)[attrs[i].offset];
228 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
229 * (char**) p = LDAP_STRDUP(opt);
232 ldap_set_option( NULL, attrs[i].offset, opt );
235 #ifdef HAVE_CYRUS_SASL
236 ldap_int_sasl_config( gopts, attrs[i].offset, opt );
241 ldap_int_tls_config( NULL, attrs[i].offset, opt );
253 static void openldap_ldap_init_w_sysconf(const char *file)
255 openldap_ldap_init_w_conf( file, 0 );
258 static void openldap_ldap_init_w_userconf(const char *file)
268 home = getenv("HOME");
271 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
273 path = LDAP_MALLOC(strlen(home) + strlen(file) + sizeof( LDAP_DIRSEP "."));
275 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
279 if(home != NULL && path != NULL) {
280 /* we assume UNIX path syntax is used... */
283 sprintf(path, "%s" LDAP_DIRSEP "%s", home, file);
284 openldap_ldap_init_w_conf(path, 1);
287 sprintf(path, "%s" LDAP_DIRSEP ".%s", home, file);
288 openldap_ldap_init_w_conf(path, 1);
296 openldap_ldap_init_w_conf(file, 1);
299 static void openldap_ldap_init_w_env(
300 struct ldapoptions *gopts,
303 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
309 if (prefix == NULL) {
310 prefix = LDAP_ENV_PREFIX;
313 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
314 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
317 for(i=0; attrs[i].type != ATTR_NONE; i++) {
318 strcpy(&buf[len], attrs[i].name);
325 switch(attrs[i].type) {
327 if((strcasecmp(value, "on") == 0)
328 || (strcasecmp(value, "yes") == 0)
329 || (strcasecmp(value, "true") == 0))
331 LDAP_BOOL_SET(gopts, attrs[i].offset);
334 LDAP_BOOL_CLR(gopts, attrs[i].offset);
339 p = &((char *) gopts)[attrs[i].offset];
340 * (int*) p = atoi(value);
344 const struct ol_keyvalue *kv;
346 for(kv = attrs[i].data;
350 if(strcasecmp(value, kv->key) == 0) {
351 p = &((char *) gopts)[attrs[i].offset];
352 * (int*) p = kv->value;
359 p = &((char *) gopts)[attrs[i].offset];
360 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
361 if (*value == '\0') {
364 * (char**) p = LDAP_STRDUP(value);
368 ldap_set_option( NULL, attrs[i].offset, value );
371 #ifdef HAVE_CYRUS_SASL
372 ldap_int_sasl_config( gopts, attrs[i].offset, value );
377 ldap_int_tls_config( NULL, attrs[i].offset, value );
384 #if defined(__GNUC__)
385 /* Declare this function as a destructor so that it will automatically be
386 * invoked either at program exit (if libldap is a static library) or
387 * at unload time (if libldap is a dynamic library).
389 * Sorry, don't know how to handle this for non-GCC environments.
391 static void ldap_int_destroy_global_options(void)
392 __attribute__ ((destructor));
396 ldap_int_destroy_global_options(void)
398 struct ldapoptions *gopts = LDAP_INT_GLOBAL_OPT();
403 gopts->ldo_valid = LDAP_UNINITIALIZED;
405 if ( gopts->ldo_defludp ) {
406 ldap_free_urllist( gopts->ldo_defludp );
407 gopts->ldo_defludp = NULL;
409 #if defined(HAVE_WINSOCK) || defined(HAVE_WINSOCK2)
413 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
414 || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
415 if ( ldap_int_hostname ) {
416 LDAP_FREE( ldap_int_hostname );
417 ldap_int_hostname = NULL;
423 * Initialize the global options structure with default values.
425 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
428 gopts->ldo_debug = *dbglvl;
430 gopts->ldo_debug = 0;
432 gopts->ldo_version = LDAP_VERSION2;
433 gopts->ldo_deref = LDAP_DEREF_NEVER;
434 gopts->ldo_timelimit = LDAP_NO_LIMIT;
435 gopts->ldo_sizelimit = LDAP_NO_LIMIT;
437 gopts->ldo_tm_api = (struct timeval *)NULL;
438 gopts->ldo_tm_net = (struct timeval *)NULL;
440 /* ldo_defludp will be freed by the termination handler
442 ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
443 gopts->ldo_defport = LDAP_PORT;
444 #if !defined(__GNUC__) && !defined(PIC)
445 /* Do this only for a static library, and only if we can't
446 * arrange for it to be executed as a library destructor
448 atexit(ldap_int_destroy_global_options);
451 gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
452 gopts->ldo_rebind_proc = NULL;
453 gopts->ldo_rebind_params = NULL;
455 LDAP_BOOL_ZERO(gopts);
457 LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
459 #ifdef LDAP_CONNECTIONLESS
460 gopts->ldo_peer = NULL;
461 gopts->ldo_cldapdn = NULL;
462 gopts->ldo_is_udp = 0;
465 #ifdef HAVE_CYRUS_SASL
466 gopts->ldo_def_sasl_mech = NULL;
467 gopts->ldo_def_sasl_realm = NULL;
468 gopts->ldo_def_sasl_authcid = NULL;
469 gopts->ldo_def_sasl_authzid = NULL;
471 memset( &gopts->ldo_sasl_secprops,
472 '\0', sizeof(gopts->ldo_sasl_secprops) );
474 gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
475 gopts->ldo_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
476 gopts->ldo_sasl_secprops.security_flags =
477 SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
481 gopts->ldo_tls_connect_cb = NULL;
482 gopts->ldo_tls_connect_arg = NULL;
485 gopts->ldo_valid = LDAP_INITIALIZED;
489 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
490 || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
491 char * ldap_int_hostname = NULL;
494 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
496 if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
500 ldap_int_error_init();
502 ldap_int_utils_init();
505 { WORD wVersionRequested;
508 wVersionRequested = MAKEWORD( 2, 0 );
509 if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
510 /* Tell the user that we couldn't find a usable */
515 /* Confirm that the WinSock DLL supports 2.0.*/
516 /* Note that if the DLL supports versions greater */
517 /* than 2.0 in addition to 2.0, it will still return */
518 /* 2.0 in wVersion since that is the version we */
521 if ( LOBYTE( wsaData.wVersion ) != 2 ||
522 HIBYTE( wsaData.wVersion ) != 0 )
524 /* Tell the user that we couldn't find a usable */
529 } /* The WinSock DLL is acceptable. Proceed. */
532 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
538 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \
539 || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
541 char *name = ldap_int_hostname;
543 ldap_int_hostname = ldap_pvt_get_fqdn( name );
545 if ( name != NULL && name != ldap_int_hostname ) {
552 if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
555 ldap_int_initialize_global_options(gopts, NULL);
557 if( getenv("LDAPNOINIT") != NULL ) {
561 #ifdef HAVE_CYRUS_SASL
563 /* set authentication identity to current user name */
564 char *user = getenv("USER");
566 if( user == NULL ) user = getenv("USERNAME");
567 if( user == NULL ) user = getenv("LOGNAME");
570 gopts->ldo_def_sasl_authcid = user;
575 openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
576 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
579 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
581 if( altfile != NULL ) {
582 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
583 LDAP_ENV_PREFIX "CONF", altfile, 0);
584 openldap_ldap_init_w_sysconf( altfile );
587 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
588 LDAP_ENV_PREFIX "CONF", 0, 0);
592 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
594 if( altfile != NULL ) {
595 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
596 LDAP_ENV_PREFIX "RC", altfile, 0);
597 openldap_ldap_init_w_userconf( altfile );
600 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
601 LDAP_ENV_PREFIX "RC", 0, 0);
604 openldap_ldap_init_w_env(gopts, NULL);