2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 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"
34 struct ldapoptions ldap_int_global_options =
35 { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE };
48 #define ATTR_OPT_INT 9
50 #define ATTR_GSSAPI 10
57 static const struct ol_keyvalue deref_kv[] = {
58 {"never", LDAP_DEREF_NEVER},
59 {"searching", LDAP_DEREF_SEARCHING},
60 {"finding", LDAP_DEREF_FINDING},
61 {"always", LDAP_DEREF_ALWAYS},
65 static const struct ol_attribute {
72 {0, ATTR_OPT_TV, "TIMEOUT", NULL, LDAP_OPT_TIMEOUT},
73 {0, ATTR_OPT_TV, "NETWORK_TIMEOUT", NULL, LDAP_OPT_NETWORK_TIMEOUT},
74 {0, ATTR_OPT_INT, "VERSION", NULL, LDAP_OPT_PROTOCOL_VERSION},
75 {0, ATTR_KV, "DEREF", deref_kv, /* or &deref_kv[0] */
76 offsetof(struct ldapoptions, ldo_deref)},
77 {0, ATTR_INT, "SIZELIMIT", NULL,
78 offsetof(struct ldapoptions, ldo_sizelimit)},
79 {0, ATTR_INT, "TIMELIMIT", NULL,
80 offsetof(struct ldapoptions, ldo_timelimit)},
81 {1, ATTR_STRING, "BINDDN", NULL,
82 offsetof(struct ldapoptions, ldo_defbinddn)},
83 {0, ATTR_STRING, "BASE", NULL,
84 offsetof(struct ldapoptions, ldo_defbase)},
85 {0, ATTR_INT, "PORT", NULL, /* deprecated */
86 offsetof(struct ldapoptions, ldo_defport)},
87 {0, ATTR_OPTION, "HOST", NULL, LDAP_OPT_HOST_NAME}, /* deprecated */
88 {0, ATTR_OPTION, "URI", NULL, LDAP_OPT_URI}, /* replaces HOST/PORT */
89 {0, ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
91 /* This should only be allowed via ldap_set_option(3) */
92 {0, ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
95 #ifdef HAVE_CYRUS_SASL
96 {0, ATTR_STRING, "SASL_MECH", NULL,
97 offsetof(struct ldapoptions, ldo_def_sasl_mech)},
98 {0, ATTR_STRING, "SASL_REALM", NULL,
99 offsetof(struct ldapoptions, ldo_def_sasl_realm)},
100 {1, ATTR_STRING, "SASL_AUTHCID", NULL,
101 offsetof(struct ldapoptions, ldo_def_sasl_authcid)},
102 {1, ATTR_STRING, "SASL_AUTHZID", NULL,
103 offsetof(struct ldapoptions, ldo_def_sasl_authzid)},
104 {0, ATTR_SASL, "SASL_SECPROPS", NULL, LDAP_OPT_X_SASL_SECPROPS},
108 {0, ATTR_GSSAPI,"GSSAPI_SIGN", NULL, LDAP_OPT_SIGN},
109 {0, ATTR_GSSAPI,"GSSAPI_ENCRYPT", NULL, LDAP_OPT_ENCRYPT},
110 {0, ATTR_GSSAPI,"GSSAPI_ALLOW_REMOTE_PRINCIPAL",NULL, LDAP_OPT_X_GSSAPI_ALLOW_REMOTE_PRINCIPAL},
114 {1, ATTR_TLS, "TLS_CERT", NULL, LDAP_OPT_X_TLS_CERTFILE},
115 {1, ATTR_TLS, "TLS_KEY", NULL, LDAP_OPT_X_TLS_KEYFILE},
116 {0, ATTR_TLS, "TLS_CACERT", NULL, LDAP_OPT_X_TLS_CACERTFILE},
117 {0, ATTR_TLS, "TLS_CACERTDIR", NULL, LDAP_OPT_X_TLS_CACERTDIR},
118 {0, ATTR_TLS, "TLS_REQCERT", NULL, LDAP_OPT_X_TLS_REQUIRE_CERT},
119 {0, ATTR_TLS, "TLS_RANDFILE", NULL, LDAP_OPT_X_TLS_RANDOM_FILE},
120 {0, ATTR_TLS, "TLS_CIPHER_SUITE", NULL, LDAP_OPT_X_TLS_CIPHER_SUITE},
122 #ifdef HAVE_OPENSSL_CRL
123 {0, ATTR_TLS, "TLS_CRLCHECK", NULL, LDAP_OPT_X_TLS_CRLCHECK},
126 {0, ATTR_TLS, "TLS_CRLFILE", NULL, LDAP_OPT_X_TLS_CRLFILE},
131 {0, ATTR_NONE, NULL, NULL, 0}
134 #define MAX_LDAP_ATTR_LEN sizeof("GSSAPI_ALLOW_REMOTE_PRINCIPAL")
135 #define MAX_LDAP_ENV_PREFIX_LEN 8
137 static void openldap_ldap_init_w_conf(
138 const char *file, int userconf )
140 char linebuf[ AC_LINE_MAX ];
145 struct ldapoptions *gopts;
147 if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
148 return; /* Could not allocate mem for global options */
156 Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
158 fp = fopen(file, "r");
160 /* could not open file */
164 Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
166 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
167 /* skip lines starting with '#' */
168 if(*start == '#') continue;
170 /* trim leading white space */
171 while((*start != '\0') && isspace((unsigned char) *start))
175 if(*start == '\0') continue;
177 /* trim trailing white space */
178 end = &start[strlen(start)-1];
179 while(isspace((unsigned char)*end)) end--;
183 if(*start == '\0') continue;
186 /* parse the command */
188 while((*start != '\0') && !isspace((unsigned char)*start)) {
192 /* command has no argument */
198 /* we must have some whitespace to skip */
199 while(isspace((unsigned char)*start)) start++;
202 for(i=0; attrs[i].type != ATTR_NONE; i++) {
205 if( !userconf && attrs[i].useronly ) {
209 if(strcasecmp(cmd, attrs[i].name) != 0) {
213 switch(attrs[i].type) {
215 if((strcasecmp(opt, "on") == 0)
216 || (strcasecmp(opt, "yes") == 0)
217 || (strcasecmp(opt, "true") == 0))
219 LDAP_BOOL_SET(gopts, attrs[i].offset);
222 LDAP_BOOL_CLR(gopts, attrs[i].offset);
230 p = &((char *) gopts)[attrs[i].offset];
231 l = strtol( opt, &next, 10 );
232 if ( next != opt && next[ 0 ] == '\0' ) {
238 const struct ol_keyvalue *kv;
240 for(kv = attrs[i].data;
244 if(strcasecmp(opt, kv->key) == 0) {
245 p = &((char *) gopts)[attrs[i].offset];
246 * (int*) p = kv->value;
253 p = &((char *) gopts)[attrs[i].offset];
254 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
255 * (char**) p = LDAP_STRDUP(opt);
258 ldap_set_option( NULL, attrs[i].offset, opt );
261 #ifdef HAVE_CYRUS_SASL
262 ldap_int_sasl_config( gopts, attrs[i].offset, opt );
267 ldap_int_gssapi_config( gopts, attrs[i].offset, opt );
272 ldap_int_tls_config( NULL, attrs[i].offset, opt );
279 tv.tv_sec = strtol( opt, &next, 10 );
280 if ( next != opt && next[ 0 ] == '\0' && tv.tv_sec > 0 ) {
281 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&tv );
287 l = strtol( opt, &next, 10 );
288 if ( next != opt && next[ 0 ] == '\0' && l > 0 && (long)((int)l) == l ) {
290 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&v );
302 static void openldap_ldap_init_w_sysconf(const char *file)
304 openldap_ldap_init_w_conf( file, 0 );
307 static void openldap_ldap_init_w_userconf(const char *file)
317 home = getenv("HOME");
320 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
322 path = LDAP_MALLOC(strlen(home) + strlen(file) + sizeof( LDAP_DIRSEP "."));
324 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
328 if(home != NULL && path != NULL) {
329 /* we assume UNIX path syntax is used... */
332 sprintf(path, "%s" LDAP_DIRSEP "%s", home, file);
333 openldap_ldap_init_w_conf(path, 1);
336 sprintf(path, "%s" LDAP_DIRSEP ".%s", home, file);
337 openldap_ldap_init_w_conf(path, 1);
345 openldap_ldap_init_w_conf(file, 1);
348 static void openldap_ldap_init_w_env(
349 struct ldapoptions *gopts,
352 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
358 if (prefix == NULL) {
359 prefix = LDAP_ENV_PREFIX;
362 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
363 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
366 for(i=0; attrs[i].type != ATTR_NONE; i++) {
367 strcpy(&buf[len], attrs[i].name);
374 switch(attrs[i].type) {
376 if((strcasecmp(value, "on") == 0)
377 || (strcasecmp(value, "yes") == 0)
378 || (strcasecmp(value, "true") == 0))
380 LDAP_BOOL_SET(gopts, attrs[i].offset);
383 LDAP_BOOL_CLR(gopts, attrs[i].offset);
388 p = &((char *) gopts)[attrs[i].offset];
389 * (int*) p = atoi(value);
393 const struct ol_keyvalue *kv;
395 for(kv = attrs[i].data;
399 if(strcasecmp(value, kv->key) == 0) {
400 p = &((char *) gopts)[attrs[i].offset];
401 * (int*) p = kv->value;
408 p = &((char *) gopts)[attrs[i].offset];
409 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
410 if (*value == '\0') {
413 * (char**) p = LDAP_STRDUP(value);
417 ldap_set_option( NULL, attrs[i].offset, value );
420 #ifdef HAVE_CYRUS_SASL
421 ldap_int_sasl_config( gopts, attrs[i].offset, value );
426 ldap_int_tls_config( NULL, attrs[i].offset, value );
433 #if defined(__GNUC__)
434 /* Declare this function as a destructor so that it will automatically be
435 * invoked either at program exit (if libldap is a static library) or
436 * at unload time (if libldap is a dynamic library).
438 * Sorry, don't know how to handle this for non-GCC environments.
440 static void ldap_int_destroy_global_options(void)
441 __attribute__ ((destructor));
445 ldap_int_destroy_global_options(void)
447 struct ldapoptions *gopts = LDAP_INT_GLOBAL_OPT();
452 gopts->ldo_valid = LDAP_UNINITIALIZED;
454 if ( gopts->ldo_defludp ) {
455 ldap_free_urllist( gopts->ldo_defludp );
456 gopts->ldo_defludp = NULL;
458 #if defined(HAVE_WINSOCK) || defined(HAVE_WINSOCK2)
462 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
463 if ( ldap_int_hostname ) {
464 LDAP_FREE( ldap_int_hostname );
465 ldap_int_hostname = NULL;
468 #ifdef HAVE_CYRUS_SASL
469 if ( gopts->ldo_def_sasl_authcid ) {
470 LDAP_FREE( gopts->ldo_def_sasl_authcid );
471 gopts->ldo_def_sasl_authcid = NULL;
475 ldap_int_tls_destroy( gopts );
480 * Initialize the global options structure with default values.
482 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
485 gopts->ldo_debug = *dbglvl;
487 gopts->ldo_debug = 0;
489 gopts->ldo_version = LDAP_VERSION2;
490 gopts->ldo_deref = LDAP_DEREF_NEVER;
491 gopts->ldo_timelimit = LDAP_NO_LIMIT;
492 gopts->ldo_sizelimit = LDAP_NO_LIMIT;
494 gopts->ldo_tm_api.tv_sec = -1;
495 gopts->ldo_tm_net.tv_sec = -1;
497 /* ldo_defludp will be freed by the termination handler
499 ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
500 gopts->ldo_defport = LDAP_PORT;
501 #if !defined(__GNUC__) && !defined(PIC)
502 /* Do this only for a static library, and only if we can't
503 * arrange for it to be executed as a library destructor
505 atexit(ldap_int_destroy_global_options);
508 gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
509 gopts->ldo_rebind_proc = NULL;
510 gopts->ldo_rebind_params = NULL;
512 LDAP_BOOL_ZERO(gopts);
514 LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
516 #ifdef LDAP_CONNECTIONLESS
517 gopts->ldo_peer = NULL;
518 gopts->ldo_cldapdn = NULL;
519 gopts->ldo_is_udp = 0;
522 #ifdef HAVE_CYRUS_SASL
523 gopts->ldo_def_sasl_mech = NULL;
524 gopts->ldo_def_sasl_realm = NULL;
525 gopts->ldo_def_sasl_authcid = NULL;
526 gopts->ldo_def_sasl_authzid = NULL;
528 memset( &gopts->ldo_sasl_secprops,
529 '\0', sizeof(gopts->ldo_sasl_secprops) );
531 gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
532 gopts->ldo_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
533 gopts->ldo_sasl_secprops.security_flags =
534 SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
538 gopts->ldo_tls_connect_cb = NULL;
539 gopts->ldo_tls_connect_arg = NULL;
540 gopts->ldo_tls_require_cert = LDAP_OPT_X_TLS_DEMAND;
543 gopts->ldo_valid = LDAP_INITIALIZED;
547 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
548 char * ldap_int_hostname = NULL;
551 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
553 if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
557 ldap_int_error_init();
559 ldap_int_utils_init();
562 { WORD wVersionRequested;
565 wVersionRequested = MAKEWORD( 2, 0 );
566 if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
567 /* Tell the user that we couldn't find a usable */
572 /* Confirm that the WinSock DLL supports 2.0.*/
573 /* Note that if the DLL supports versions greater */
574 /* than 2.0 in addition to 2.0, it will still return */
575 /* 2.0 in wVersion since that is the version we */
578 if ( LOBYTE( wsaData.wVersion ) != 2 ||
579 HIBYTE( wsaData.wVersion ) != 0 )
581 /* Tell the user that we couldn't find a usable */
586 } /* The WinSock DLL is acceptable. Proceed. */
589 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
595 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
597 char *name = ldap_int_hostname;
599 ldap_int_hostname = ldap_pvt_get_fqdn( name );
601 if ( name != NULL && name != ldap_int_hostname ) {
608 if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
611 ldap_int_initialize_global_options(gopts, NULL);
613 if( getenv("LDAPNOINIT") != NULL ) {
617 #ifdef HAVE_CYRUS_SASL
619 /* set authentication identity to current user name */
620 char *user = getenv("USER");
622 if( user == NULL ) user = getenv("USERNAME");
623 if( user == NULL ) user = getenv("LOGNAME");
626 gopts->ldo_def_sasl_authcid = LDAP_STRDUP( user );
631 openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
632 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
635 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
637 if( altfile != NULL ) {
638 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
639 LDAP_ENV_PREFIX "CONF", altfile, 0);
640 openldap_ldap_init_w_sysconf( altfile );
643 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
644 LDAP_ENV_PREFIX "CONF", 0, 0);
648 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
650 if( altfile != NULL ) {
651 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
652 LDAP_ENV_PREFIX "RC", altfile, 0);
653 openldap_ldap_init_w_userconf( altfile );
656 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
657 LDAP_ENV_PREFIX "RC", 0, 0);
660 openldap_ldap_init_w_env(gopts, NULL);