// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2022 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see .
// This class will read a standard OpenVPN config file that might contain
// references to other files, and it will merge the included files into the
// config file, using inline configuration syntax, to produce a single,
// unified config file.
#ifndef OPENVPN_OPTIONS_MERGE_H
#define OPENVPN_OPTIONS_MERGE_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace openvpn {
class ProfileMerge
{
// internal flags
enum
{
F_MAY_INCLUDE_KEY_DIRECTION = (1 << 0),
F_PKCS12 = (1 << 1),
F_HTTP_PROXY = (1 << 2),
};
// limits
enum
{
MAX_FN_LIST_SIZE = 16,
};
public:
OPENVPN_EXCEPTION(merge_error);
// public status values
enum Status
{
MERGE_UNDEFINED,
MERGE_SUCCESS,
MERGE_EXCEPTION,
MERGE_OVPN_EXT_FAIL,
MERGE_OVPN_FILE_FAIL,
MERGE_REF_FAIL,
MERGE_MULTIPLE_REF_FAIL,
};
// merge status
Status status() const
{
return status_;
}
const std::string &error() const
{
return error_;
}
// merge path basename
const std::string &basename() const
{
return basename_;
}
// final unified profile
const std::string &profile_content() const
{
return profile_content_;
}
// list of all reference paths successfully read
const std::vector &ref_path_list() const
{
return ref_succeed_list_;
}
// merge status as a string
const char *status_string() const
{
switch (status_)
{
case MERGE_UNDEFINED:
return "MERGE_UNDEFINED";
case MERGE_SUCCESS:
return "MERGE_SUCCESS";
case MERGE_EXCEPTION:
return "MERGE_EXCEPTION";
case MERGE_OVPN_EXT_FAIL:
return "MERGE_OVPN_EXT_FAIL";
case MERGE_OVPN_FILE_FAIL:
return "MERGE_OVPN_FILE_FAIL";
case MERGE_REF_FAIL:
return "MERGE_REF_FAIL";
case MERGE_MULTIPLE_REF_FAIL:
return "MERGE_MULTIPLE_REF_FAIL";
default:
return "MERGE_?";
}
}
// allow following of external file references
enum Follow
{
FOLLOW_NONE,
FOLLOW_PARTIAL,
FOLLOW_FULL,
};
ProfileMerge(const std::string &profile_path,
const std::string &profile_ext,
const std::string &profile_dir_override,
const Follow follow_references,
const size_t max_line_len,
const size_t max_size)
: status_(MERGE_UNDEFINED)
{
try
{
size_t total_size = 0;
// read the profile
std::string orig_profile_content;
std::string profile_dir;
try
{
profile_dir = !profile_dir_override.empty() ? profile_dir_override : path::dirname(profile_path);
basename_ = path::basename(profile_path);
const std::string ext = path::ext(basename_);
if (profile_ext.empty() || string::strcasecmp(ext, profile_ext) == 0)
{
orig_profile_content = read_text_utf8(profile_path, max_size);
total_size = orig_profile_content.size();
}
else
{
status_ = MERGE_OVPN_EXT_FAIL;
error_ = std::string("ERR_PROFILE_NO_OVPN_EXTENSION: ") + basename_;
return;
}
}
catch (const file_is_binary &e)
{
status_ = MERGE_OVPN_FILE_FAIL;
error_ = std::string("ERR_PROFILE_FILE_IS_BINARY: ") + e.what();
return;
}
catch (const file_too_large &e)
{
status_ = MERGE_OVPN_FILE_FAIL;
error_ = std::string("ERR_PROFILE_FILE_TOO_LARGE: ") + e.what();
return;
}
catch (const std::exception &e)
{
status_ = MERGE_OVPN_FILE_FAIL;
error_ = std::string("ERR_PROFILE_GENERIC: ") + e.what();
return;
}
// expand the profile
expand_profile(orig_profile_content, profile_dir, follow_references, max_line_len, max_size, total_size);
}
catch (const std::exception &e)
{
status_ = MERGE_EXCEPTION;
error_ = std::string("ERR_PROFILE_GENERIC: ") + e.what();
}
}
static std::string merge(const std::string &profile_path,
const std::string &profile_ext,
const std::string &profile_dir_override,
const Follow follow_references,
const size_t max_line_len,
const size_t max_size)
{
const ProfileMerge pm(profile_path,
profile_ext,
profile_dir_override,
follow_references,
max_line_len,
max_size);
if (pm.status() == ProfileMerge::MERGE_SUCCESS)
return pm.profile_content();
else
OPENVPN_THROW(merge_error, pm.status_string() << ": " << pm.error());
}
protected:
ProfileMerge()
: status_(MERGE_UNDEFINED)
{
}
void expand_profile(const std::string &orig_profile_content,
const std::string &profile_dir,
const Follow follow_references,
const size_t max_line_len,
const size_t max_size,
size_t total_size)
{
if (total_size > max_size)
{
status_ = MERGE_EXCEPTION;
error_ = "ERR_PROFILE_FILE_TOO_LARGE: file too large";
return;
}
status_ = MERGE_SUCCESS;
SplitLines in(orig_profile_content, max_line_len);
bool in_multiline = false;
bool opaque_multiline = false;
Option multiline;
profile_content_.reserve(orig_profile_content.length());
while (in(true))
{
if (in.line_overflow())
{
status_ = MERGE_EXCEPTION;
error_ = "ERR_PROFILE_LINE_TOO_LONG: line too long";
return;
}
const std::string &line = in.line_ref();
bool echo = true;
if (in_multiline)
{
if (OptionList::is_close_tag(line, multiline.ref(0)))
{
multiline.clear();
in_multiline = false;
opaque_multiline = false;
}
}
else if (!OptionList::ignore_line(line))
{
Option opt = Split::by_space