Exemplo n.º 1
0
struct sql_connection *db_sql_init(const char *config_path, bool userdb)
{
	struct sql_connection *conn;
	const char *error;
	pool_t pool;

	conn = sql_conn_find(config_path);
	if (conn != NULL) {
		if (userdb)
			conn->userdb_used = TRUE;
		conn->refcount++;
		return conn;
	}

	if (*config_path == '\0')
		i_fatal("sql: Configuration file path not given");

	pool = pool_alloconly_create("sql_connection", 1024);
	conn = p_new(pool, struct sql_connection, 1);
	conn->pool = pool;
	conn->userdb_used = userdb;

	conn->refcount = 1;

	conn->config_path = p_strdup(pool, config_path);
	conn->set = default_sql_settings;
	if (!settings_read_nosection(config_path, parse_setting, conn, &error))
		i_fatal("sql %s: %s", config_path, error);

	if (conn->set.password_query == default_sql_settings.password_query)
		conn->default_password_query = TRUE;
	if (conn->set.user_query == default_sql_settings.user_query)
		conn->default_user_query = TRUE;
	if (conn->set.update_query == default_sql_settings.update_query)
		conn->default_update_query = TRUE;
	if (conn->set.iterate_query == default_sql_settings.iterate_query)
		conn->default_iterate_query = TRUE;

	if (conn->set.driver == NULL) {
		i_fatal("sql: driver not set in configuration file %s",
			config_path);
	}
	if (conn->set.connect == NULL) {
		i_fatal("sql: connect string not set in configuration file %s",
			config_path);
	}
	conn->db = sql_init(conn->set.driver, conn->set.connect);

	conn->next = connections;
	connections = conn;
	return conn;
}
Exemplo n.º 2
0
struct dict_connection *db_dict_init(const char *config_path)
{
	struct dict_connection *conn;
	const char *error;
	pool_t pool;

	conn = dict_conn_find(config_path);
	if (conn != NULL) {
		conn->refcount++;
		return conn;
	}

	if (*config_path == '\0')
		i_fatal("dict: Configuration file path not given");

	pool = pool_alloconly_create("dict_connection", 1024);
	conn = p_new(pool, struct dict_connection, 1);
	conn->pool = pool;

	conn->refcount = 1;

	conn->config_path = p_strdup(pool, config_path);
	conn->set = default_dict_settings;
	if (!settings_read_nosection(config_path, parse_setting, conn, &error))
		i_fatal("dict %s: %s", config_path, error);

	if (conn->set.uri == NULL)
		i_fatal("dict %s: Empty uri setting", config_path);
	if (strcmp(conn->set.value_format, "json") != 0) {
		i_fatal("dict %s: Unsupported value_format %s in ",
			config_path, conn->set.value_format);
	}
	if (dict_init(conn->set.uri, DICT_DATA_TYPE_STRING, "",
		      global_auth_settings->base_dir, &conn->dict, &error) < 0)
		i_fatal("dict %s: Failed to init dict: %s", config_path, error);

	conn->next = connections;
	connections = conn;
	return conn;
}
int sieve_ldap_storage_read_settings
(struct sieve_ldap_storage *lstorage, const char *config_path)
{
    struct sieve_storage *storage = &lstorage->storage;
    const char *str, *error;
    struct stat st;

    if ( stat(config_path, &st) < 0 ) {
        sieve_storage_sys_error(storage,
                                "Failed to read LDAP storage config: "
                                "stat(%s) failed: %m", config_path);
        return -1;
    }

    lstorage->set = default_settings;
    lstorage->set_mtime = st.st_mtime;

    if (!settings_read_nosection
            (config_path, parse_setting, lstorage, &error)) {
        sieve_storage_set_critical(storage,
                                   "Failed to read LDAP storage config `%s': %s",
                                   config_path, error);
        return -1;
    }

    if (lstorage->set.base == NULL) {
        sieve_storage_set_critical(storage,
                                   "Invalid LDAP storage config `%s': "
                                   "No search base given", config_path);
        return -1;
    }

    if (lstorage->set.uris == NULL && lstorage->set.hosts == NULL) {
        sieve_storage_set_critical(storage,
                                   "Invalid LDAP storage config `%s': "
                                   "No uris or hosts set", config_path);
        return -1;
    }

    if (*lstorage->set.ldaprc_path != '\0') {
        str = getenv("LDAPRC");
        if (str != NULL && strcmp(str, lstorage->set.ldaprc_path) != 0) {
            sieve_storage_set_critical(storage,
                                       "Invalid LDAP storage config `%s': "
                                       "Multiple different ldaprc_path settings not allowed "
                                       "(%s and %s)", config_path, str, lstorage->set.ldaprc_path);
            return -1;
        }
        env_put(t_strconcat("LDAPRC=", lstorage->set.ldaprc_path, NULL));
    }

    if ( ldap_deref_from_str
            (lstorage->set.deref, &lstorage->set.ldap_deref) < 0 ) {
        sieve_storage_set_critical(storage,
                                   "Invalid LDAP storage config `%s': "
                                   "Invalid deref option `%s'",
                                   config_path, lstorage->set.deref);;
    }

    if ( ldap_scope_from_str
            (lstorage->set.scope, &lstorage->set.ldap_scope) < 0 ) {
        sieve_storage_set_critical(storage,
                                   "Invalid LDAP storage config `%s': "
                                   "Invalid scope option `%s'",
                                   config_path, lstorage->set.scope);;
    }

#ifdef OPENLDAP_TLS_OPTIONS
    if ( lstorage->set.tls_require_cert != NULL &&
            ldap_tls_require_cert_from_str(lstorage->set.tls_require_cert,
                                           &lstorage->set.ldap_tls_require_cert) < 0) {
        sieve_storage_set_critical(storage,
                                   "Invalid LDAP storage config `%s': "
                                   "Invalid tls_require_cert option `%s'",
                                   config_path, lstorage->set.tls_require_cert);
    }
#endif
    return 0;
}