2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2018 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>
22 #include <ac/unistd.h>
25 #include <ac/socket.h>
26 #include <ac/string.h>
35 #include "ldap_defaults.h"
38 struct ldapoptions ldap_int_global_options =
39 { LDAP_UNINITIALIZED, LDAP_DEBUG_NONE
41 LDAP_LDO_CONNECTIONLESS_NULLARG
44 LDAP_LDO_GSSAPI_NULLARG
45 LDAP_LDO_MUTEX_NULLARG };
58 #define ATTR_OPT_INT 9
60 #define ATTR_GSSAPI 10
67 static const struct ol_keyvalue deref_kv[] = {
68 {"never", LDAP_DEREF_NEVER},
69 {"searching", LDAP_DEREF_SEARCHING},
70 {"finding", LDAP_DEREF_FINDING},
71 {"always", LDAP_DEREF_ALWAYS},
75 static const struct ol_attribute {
82 {0, ATTR_OPT_TV, "TIMEOUT", NULL, LDAP_OPT_TIMEOUT},
83 {0, ATTR_OPT_TV, "NETWORK_TIMEOUT", NULL, LDAP_OPT_NETWORK_TIMEOUT},
84 {0, ATTR_OPT_INT, "VERSION", NULL, LDAP_OPT_PROTOCOL_VERSION},
85 {0, ATTR_KV, "DEREF", deref_kv, /* or &deref_kv[0] */
86 offsetof(struct ldapoptions, ldo_deref)},
87 {0, ATTR_INT, "SIZELIMIT", NULL,
88 offsetof(struct ldapoptions, ldo_sizelimit)},
89 {0, ATTR_INT, "TIMELIMIT", NULL,
90 offsetof(struct ldapoptions, ldo_timelimit)},
91 {1, ATTR_STRING, "BINDDN", NULL,
92 offsetof(struct ldapoptions, ldo_defbinddn)},
93 {0, ATTR_STRING, "BASE", NULL,
94 offsetof(struct ldapoptions, ldo_defbase)},
95 {0, ATTR_INT, "PORT", NULL, /* deprecated */
96 offsetof(struct ldapoptions, ldo_defport)},
97 {0, ATTR_OPTION, "HOST", NULL, LDAP_OPT_HOST_NAME}, /* deprecated */
98 {0, ATTR_OPTION, "URI", NULL, LDAP_OPT_URI}, /* replaces HOST/PORT */
99 {0, ATTR_BOOL, "REFERRALS", NULL, LDAP_BOOL_REFERRALS},
101 /* This should only be allowed via ldap_set_option(3) */
102 {0, ATTR_BOOL, "RESTART", NULL, LDAP_BOOL_RESTART},
105 #ifdef HAVE_CYRUS_SASL
106 {0, ATTR_STRING, "SASL_MECH", NULL,
107 offsetof(struct ldapoptions, ldo_def_sasl_mech)},
108 {0, ATTR_STRING, "SASL_REALM", NULL,
109 offsetof(struct ldapoptions, ldo_def_sasl_realm)},
110 {1, ATTR_STRING, "SASL_AUTHCID", NULL,
111 offsetof(struct ldapoptions, ldo_def_sasl_authcid)},
112 {1, ATTR_STRING, "SASL_AUTHZID", NULL,
113 offsetof(struct ldapoptions, ldo_def_sasl_authzid)},
114 {0, ATTR_SASL, "SASL_SECPROPS", NULL, LDAP_OPT_X_SASL_SECPROPS},
115 {0, ATTR_BOOL, "SASL_NOCANON", NULL, LDAP_BOOL_SASL_NOCANON},
119 {0, ATTR_GSSAPI,"GSSAPI_SIGN", NULL, LDAP_OPT_SIGN},
120 {0, ATTR_GSSAPI,"GSSAPI_ENCRYPT", NULL, LDAP_OPT_ENCRYPT},
121 {0, ATTR_GSSAPI,"GSSAPI_ALLOW_REMOTE_PRINCIPAL",NULL, LDAP_OPT_X_GSSAPI_ALLOW_REMOTE_PRINCIPAL},
125 {1, ATTR_TLS, "TLS_CERT", NULL, LDAP_OPT_X_TLS_CERTFILE},
126 {1, ATTR_TLS, "TLS_KEY", NULL, LDAP_OPT_X_TLS_KEYFILE},
127 {0, ATTR_TLS, "TLS_CACERT", NULL, LDAP_OPT_X_TLS_CACERTFILE},
128 {0, ATTR_TLS, "TLS_CACERTDIR", NULL, LDAP_OPT_X_TLS_CACERTDIR},
129 {0, ATTR_TLS, "TLS_REQCERT", NULL, LDAP_OPT_X_TLS_REQUIRE_CERT},
130 {0, ATTR_TLS, "TLS_RANDFILE", NULL, LDAP_OPT_X_TLS_RANDOM_FILE},
131 {0, ATTR_TLS, "TLS_CIPHER_SUITE", NULL, LDAP_OPT_X_TLS_CIPHER_SUITE},
132 {0, ATTR_TLS, "TLS_PROTOCOL_MIN", NULL, LDAP_OPT_X_TLS_PROTOCOL_MIN},
133 {0, ATTR_TLS, "TLS_PEERKEY_HASH", NULL, LDAP_OPT_X_TLS_PEERKEY_HASH},
135 #ifdef HAVE_OPENSSL_CRL
136 {0, ATTR_TLS, "TLS_CRLCHECK", NULL, LDAP_OPT_X_TLS_CRLCHECK},
139 {0, ATTR_TLS, "TLS_CRLFILE", NULL, LDAP_OPT_X_TLS_CRLFILE},
144 {0, ATTR_NONE, NULL, NULL, 0}
147 #define MAX_LDAP_ATTR_LEN sizeof("GSSAPI_ALLOW_REMOTE_PRINCIPAL")
148 #define MAX_LDAP_ENV_PREFIX_LEN 8
151 ldap_int_conf_option(
152 struct ldapoptions *gopts,
153 char *cmd, char *opt, int userconf )
157 for(i=0; attrs[i].type != ATTR_NONE; i++) {
160 if( !userconf && attrs[i].useronly ) {
164 if(strcasecmp(cmd, attrs[i].name) != 0) {
168 switch(attrs[i].type) {
170 if((strcasecmp(opt, "on") == 0)
171 || (strcasecmp(opt, "yes") == 0)
172 || (strcasecmp(opt, "true") == 0))
174 LDAP_BOOL_SET(gopts, attrs[i].offset);
177 LDAP_BOOL_CLR(gopts, attrs[i].offset);
185 p = &((char *) gopts)[attrs[i].offset];
186 l = strtol( opt, &next, 10 );
187 if ( next != opt && next[ 0 ] == '\0' ) {
193 const struct ol_keyvalue *kv;
195 for(kv = attrs[i].data;
199 if(strcasecmp(opt, kv->key) == 0) {
200 p = &((char *) gopts)[attrs[i].offset];
201 * (int*) p = kv->value;
208 p = &((char *) gopts)[attrs[i].offset];
209 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
210 * (char**) p = LDAP_STRDUP(opt);
213 ldap_set_option( NULL, attrs[i].offset, opt );
216 #ifdef HAVE_CYRUS_SASL
217 ldap_int_sasl_config( gopts, attrs[i].offset, opt );
222 ldap_int_gssapi_config( gopts, attrs[i].offset, opt );
227 ldap_pvt_tls_config( NULL, attrs[i].offset, opt );
234 tv.tv_sec = strtol( opt, &next, 10 );
235 if ( next != opt && next[ 0 ] == '\0' && tv.tv_sec > 0 ) {
236 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&tv );
242 l = strtol( opt, &next, 10 );
243 if ( next != opt && next[ 0 ] == '\0' && l > 0 && (long)((int)l) == l ) {
245 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&v );
253 if ( attrs[i].type == ATTR_NONE ) {
254 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_tls_config: "
255 "unknown option '%s'",
264 ldap_pvt_conf_option(
265 char *cmd, char *opt, int userconf )
267 struct ldapoptions *gopts;
268 int rc = LDAP_OPT_ERROR;
270 /* Get pointer to global option structure */
271 gopts = LDAP_INT_GLOBAL_OPT();
273 return LDAP_NO_MEMORY;
276 if ( gopts->ldo_valid != LDAP_INITIALIZED ) {
277 ldap_int_initialize(gopts, NULL);
278 if ( gopts->ldo_valid != LDAP_INITIALIZED )
279 return LDAP_LOCAL_ERROR;
282 return ldap_int_conf_option( gopts, cmd, opt, userconf );
285 static void openldap_ldap_init_w_conf(
286 const char *file, int userconf )
288 char linebuf[ AC_LINE_MAX ];
293 struct ldapoptions *gopts;
295 if ((gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
296 return; /* Could not allocate mem for global options */
304 Debug(LDAP_DEBUG_TRACE, "ldap_init: trying %s\n", file, 0, 0);
306 fp = fopen(file, "r");
308 /* could not open file */
312 Debug(LDAP_DEBUG_TRACE, "ldap_init: using %s\n", file, 0, 0);
314 while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
315 /* skip lines starting with '#' */
316 if(*start == '#') continue;
318 /* trim leading white space */
319 while((*start != '\0') && isspace((unsigned char) *start))
323 if(*start == '\0') continue;
325 /* trim trailing white space */
326 end = &start[strlen(start)-1];
327 while(isspace((unsigned char)*end)) end--;
331 if(*start == '\0') continue;
334 /* parse the command */
336 while((*start != '\0') && !isspace((unsigned char)*start)) {
340 /* command has no argument */
346 /* we must have some whitespace to skip */
347 while(isspace((unsigned char)*start)) start++;
350 ldap_int_conf_option( gopts, cmd, opt, userconf );
356 static void openldap_ldap_init_w_sysconf(const char *file)
358 openldap_ldap_init_w_conf( file, 0 );
361 static void openldap_ldap_init_w_userconf(const char *file)
371 home = getenv("HOME");
374 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is %s\n",
376 path = LDAP_MALLOC(strlen(home) + strlen(file) + sizeof( LDAP_DIRSEP "."));
378 Debug(LDAP_DEBUG_TRACE, "ldap_init: HOME env is NULL\n",
382 if(home != NULL && path != NULL) {
383 /* we assume UNIX path syntax is used... */
386 sprintf(path, "%s" LDAP_DIRSEP "%s", home, file);
387 openldap_ldap_init_w_conf(path, 1);
390 sprintf(path, "%s" LDAP_DIRSEP ".%s", home, file);
391 openldap_ldap_init_w_conf(path, 1);
399 openldap_ldap_init_w_conf(file, 1);
402 static void openldap_ldap_init_w_env(
403 struct ldapoptions *gopts,
406 char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
412 if (prefix == NULL) {
413 prefix = LDAP_ENV_PREFIX;
416 strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
417 buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
420 for(i=0; attrs[i].type != ATTR_NONE; i++) {
421 strcpy(&buf[len], attrs[i].name);
428 switch(attrs[i].type) {
430 if((strcasecmp(value, "on") == 0)
431 || (strcasecmp(value, "yes") == 0)
432 || (strcasecmp(value, "true") == 0))
434 LDAP_BOOL_SET(gopts, attrs[i].offset);
437 LDAP_BOOL_CLR(gopts, attrs[i].offset);
442 p = &((char *) gopts)[attrs[i].offset];
443 * (int*) p = atoi(value);
447 const struct ol_keyvalue *kv;
449 for(kv = attrs[i].data;
453 if(strcasecmp(value, kv->key) == 0) {
454 p = &((char *) gopts)[attrs[i].offset];
455 * (int*) p = kv->value;
462 p = &((char *) gopts)[attrs[i].offset];
463 if (* (char**) p != NULL) LDAP_FREE(* (char**) p);
464 if (*value == '\0') {
467 * (char**) p = LDAP_STRDUP(value);
471 ldap_set_option( NULL, attrs[i].offset, value );
474 #ifdef HAVE_CYRUS_SASL
475 ldap_int_sasl_config( gopts, attrs[i].offset, value );
480 ldap_int_gssapi_config( gopts, attrs[i].offset, value );
485 ldap_pvt_tls_config( NULL, attrs[i].offset, value );
492 tv.tv_sec = strtol( value, &next, 10 );
493 if ( next != value && next[ 0 ] == '\0' && tv.tv_sec > 0 ) {
494 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&tv );
500 l = strtol( value, &next, 10 );
501 if ( next != value && next[ 0 ] == '\0' && l > 0 && (long)((int)l) == l ) {
503 (void)ldap_set_option( NULL, attrs[i].offset, (const void *)&v );
510 #if defined(__GNUC__)
511 /* Declare this function as a destructor so that it will automatically be
512 * invoked either at program exit (if libldap is a static library) or
513 * at unload time (if libldap is a dynamic library).
515 * Sorry, don't know how to handle this for non-GCC environments.
517 static void ldap_int_destroy_global_options(void)
518 __attribute__ ((destructor));
522 ldap_int_destroy_global_options(void)
524 struct ldapoptions *gopts = LDAP_INT_GLOBAL_OPT();
529 gopts->ldo_valid = LDAP_UNINITIALIZED;
531 if ( gopts->ldo_defludp ) {
532 ldap_free_urllist( gopts->ldo_defludp );
533 gopts->ldo_defludp = NULL;
535 #if defined(HAVE_WINSOCK) || defined(HAVE_WINSOCK2)
539 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
540 if ( ldap_int_hostname ) {
541 LDAP_FREE( ldap_int_hostname );
542 ldap_int_hostname = NULL;
545 #ifdef HAVE_CYRUS_SASL
546 if ( gopts->ldo_def_sasl_authcid ) {
547 LDAP_FREE( gopts->ldo_def_sasl_authcid );
548 gopts->ldo_def_sasl_authcid = NULL;
552 ldap_int_tls_destroy( gopts );
557 * Initialize the global options structure with default values.
559 void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl )
561 #ifdef LDAP_R_COMPILE
562 LDAP_PVT_MUTEX_FIRSTCREATE(gopts->ldo_mutex);
564 LDAP_MUTEX_LOCK( &gopts->ldo_mutex );
565 if (gopts->ldo_valid == LDAP_INITIALIZED) {
566 /* someone else got here first */
567 LDAP_MUTEX_UNLOCK( &gopts->ldo_mutex );
571 gopts->ldo_debug = *dbglvl;
573 gopts->ldo_debug = 0;
575 gopts->ldo_version = LDAP_VERSION2;
576 gopts->ldo_deref = LDAP_DEREF_NEVER;
577 gopts->ldo_timelimit = LDAP_NO_LIMIT;
578 gopts->ldo_sizelimit = LDAP_NO_LIMIT;
580 gopts->ldo_tm_api.tv_sec = -1;
581 gopts->ldo_tm_net.tv_sec = -1;
583 /* ldo_defludp will be freed by the termination handler
585 ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/");
586 gopts->ldo_defport = LDAP_PORT;
587 #if !defined(__GNUC__) && !defined(PIC)
588 /* Do this only for a static library, and only if we can't
589 * arrange for it to be executed as a library destructor
591 atexit(ldap_int_destroy_global_options);
594 gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
595 gopts->ldo_rebind_proc = NULL;
596 gopts->ldo_rebind_params = NULL;
598 LDAP_BOOL_ZERO(gopts);
600 LDAP_BOOL_SET(gopts, LDAP_BOOL_REFERRALS);
602 #ifdef LDAP_CONNECTIONLESS
603 gopts->ldo_peer = NULL;
604 gopts->ldo_cldapdn = NULL;
605 gopts->ldo_is_udp = 0;
608 #ifdef HAVE_CYRUS_SASL
609 gopts->ldo_def_sasl_mech = NULL;
610 gopts->ldo_def_sasl_realm = NULL;
611 gopts->ldo_def_sasl_authcid = NULL;
612 gopts->ldo_def_sasl_authzid = NULL;
614 memset( &gopts->ldo_sasl_secprops,
615 '\0', sizeof(gopts->ldo_sasl_secprops) );
617 gopts->ldo_sasl_secprops.max_ssf = INT_MAX;
618 gopts->ldo_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
619 gopts->ldo_sasl_secprops.security_flags =
620 SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
624 gopts->ldo_tls_connect_cb = NULL;
625 gopts->ldo_tls_connect_arg = NULL;
626 gopts->ldo_tls_require_cert = LDAP_OPT_X_TLS_DEMAND;
628 gopts->ldo_keepalive_probes = 0;
629 gopts->ldo_keepalive_interval = 0;
630 gopts->ldo_keepalive_idle = 0;
632 gopts->ldo_valid = LDAP_INITIALIZED;
633 LDAP_MUTEX_UNLOCK( &gopts->ldo_mutex );
637 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
638 char * ldap_int_hostname = NULL;
641 #ifdef LDAP_R_COMPILE
642 int ldap_int_stackguard;
645 void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl )
647 if ( gopts->ldo_valid == LDAP_INITIALIZED ) {
651 ldap_int_error_init();
653 ldap_int_utils_init();
656 { WORD wVersionRequested;
659 wVersionRequested = MAKEWORD( 2, 0 );
660 if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
661 /* Tell the user that we couldn't find a usable */
666 /* Confirm that the WinSock DLL supports 2.0.*/
667 /* Note that if the DLL supports versions greater */
668 /* than 2.0 in addition to 2.0, it will still return */
669 /* 2.0 in wVersion since that is the version we */
672 if ( LOBYTE( wsaData.wVersion ) != 2 ||
673 HIBYTE( wsaData.wVersion ) != 0 )
675 /* Tell the user that we couldn't find a usable */
680 } /* The WinSock DLL is acceptable. Proceed. */
681 #elif defined(HAVE_WINSOCK)
683 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
689 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
690 LDAP_MUTEX_LOCK( &ldap_int_hostname_mutex );
692 char *name = ldap_int_hostname;
694 ldap_int_hostname = ldap_pvt_get_fqdn( name );
696 if ( name != NULL && name != ldap_int_hostname ) {
700 LDAP_MUTEX_UNLOCK( &ldap_int_hostname_mutex );
704 if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
707 #ifdef HAVE_CYRUS_SASL
708 if ( ldap_int_sasl_init() != 0 ) {
713 ldap_int_initialize_global_options(gopts, dbglvl);
715 if( getenv("LDAPNOINIT") != NULL ) {
719 #ifdef LDAP_R_COMPILE
720 if( getenv("LDAPSTACKGUARD") != NULL ) {
721 ldap_int_stackguard = 1;
725 #ifdef HAVE_CYRUS_SASL
727 /* set authentication identity to current user name */
728 char *user = getenv("USER");
730 if( user == NULL ) user = getenv("USERNAME");
731 if( user == NULL ) user = getenv("LOGNAME");
734 gopts->ldo_def_sasl_authcid = LDAP_STRDUP( user );
739 openldap_ldap_init_w_sysconf(LDAP_CONF_FILE);
742 if ( geteuid() != getuid() )
746 openldap_ldap_init_w_userconf(LDAP_USERRC_FILE);
749 char *altfile = getenv(LDAP_ENV_PREFIX "CONF");
751 if( altfile != NULL ) {
752 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
753 LDAP_ENV_PREFIX "CONF", altfile, 0);
754 openldap_ldap_init_w_sysconf( altfile );
757 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
758 LDAP_ENV_PREFIX "CONF", 0, 0);
762 char *altfile = getenv(LDAP_ENV_PREFIX "RC");
764 if( altfile != NULL ) {
765 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is %s\n",
766 LDAP_ENV_PREFIX "RC", altfile, 0);
767 openldap_ldap_init_w_userconf( altfile );
770 Debug(LDAP_DEBUG_TRACE, "ldap_init: %s env is NULL\n",
771 LDAP_ENV_PREFIX "RC", 0, 0);
774 openldap_ldap_init_w_env(gopts, NULL);