2014-07-21 05:22:06 +02:00
|
|
|
// 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.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2020-03-04 20:48:54 +01:00
|
|
|
// Copyright (C) 2012-2020 OpenVPN Inc.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2017-12-21 21:42:20 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License Version 3
|
2014-07-21 05:22:06 +02:00
|
|
|
// as published by the Free Software Foundation.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// 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
|
2017-12-21 21:42:20 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2014-07-21 05:22:06 +02:00
|
|
|
//
|
2017-12-21 21:42:20 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2014-07-21 05:22:06 +02:00
|
|
|
// along with this program in the COPYING file.
|
|
|
|
// If not, see <http://www.gnu.org/licenses/>.
|
2012-08-24 23:13:42 +02:00
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// Basic file-handling methods.
|
|
|
|
|
2011-10-06 19:22:37 +02:00
|
|
|
#ifndef OPENVPN_COMMON_FILE_H
|
|
|
|
#define OPENVPN_COMMON_FILE_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
2016-03-15 00:13:19 +01:00
|
|
|
#include <iostream>
|
2015-04-24 01:55:07 +02:00
|
|
|
#include <cstdint> // for std::uint64_t
|
2012-11-12 02:52:03 +01:00
|
|
|
|
2011-10-06 19:22:37 +02:00
|
|
|
#include <openvpn/common/exception.hpp>
|
2012-11-12 02:52:03 +01:00
|
|
|
#include <openvpn/common/unicode.hpp>
|
2011-11-26 12:33:39 +01:00
|
|
|
#include <openvpn/buffer/buffer.hpp>
|
2015-01-13 02:34:10 +01:00
|
|
|
#include <openvpn/buffer/bufstr.hpp>
|
2015-09-09 07:16:46 +02:00
|
|
|
#include <openvpn/buffer/buflist.hpp>
|
2011-10-06 19:22:37 +02:00
|
|
|
|
2019-04-30 14:58:40 +02:00
|
|
|
#if defined(OPENVPN_PLATFORM_WIN)
|
|
|
|
#include <openvpn/win/unicode.hpp>
|
|
|
|
#endif
|
|
|
|
|
2020-02-10 15:33:00 +01:00
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
#include <filesystem>
|
|
|
|
#endif
|
|
|
|
|
2011-10-06 19:22:37 +02:00
|
|
|
namespace openvpn {
|
|
|
|
|
2012-11-12 02:52:03 +01:00
|
|
|
OPENVPN_UNTAGGED_EXCEPTION(file_exception);
|
|
|
|
OPENVPN_UNTAGGED_EXCEPTION_INHERIT(file_exception, open_file_error);
|
|
|
|
OPENVPN_UNTAGGED_EXCEPTION_INHERIT(file_exception, file_too_large);
|
|
|
|
OPENVPN_UNTAGGED_EXCEPTION_INHERIT(file_exception, file_is_binary);
|
|
|
|
OPENVPN_UNTAGGED_EXCEPTION_INHERIT(file_exception, file_not_utf8);
|
2011-11-26 12:33:39 +01:00
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// Read text from file via stream approach that doesn't require that we
|
|
|
|
// establish the length of the file in advance.
|
2012-11-12 02:52:03 +01:00
|
|
|
inline std::string read_text_simple(const std::string& filename)
|
2011-10-06 19:22:37 +02:00
|
|
|
{
|
2011-10-07 07:47:49 +02:00
|
|
|
std::ifstream ifs(filename.c_str());
|
2011-10-06 19:22:37 +02:00
|
|
|
if (!ifs)
|
2014-08-12 02:02:21 +02:00
|
|
|
OPENVPN_THROW(open_file_error, "cannot open for read: " << filename);
|
2011-10-06 19:22:37 +02:00
|
|
|
const std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
|
2011-10-07 07:47:49 +02:00
|
|
|
if (!ifs)
|
2012-11-12 02:52:03 +01:00
|
|
|
OPENVPN_THROW(open_file_error, "cannot read: " << filename);
|
2011-10-06 19:22:37 +02:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// Read a file (may be text or binary).
|
2012-11-12 02:52:03 +01:00
|
|
|
inline BufferPtr read_binary(const std::string& filename,
|
2015-04-24 01:55:07 +02:00
|
|
|
const std::uint64_t max_size = 0,
|
2012-11-12 02:52:03 +01:00
|
|
|
const unsigned int buffer_flags = 0)
|
2011-11-26 12:33:39 +01:00
|
|
|
{
|
2019-04-30 14:58:40 +02:00
|
|
|
#if defined(OPENVPN_PLATFORM_WIN)
|
|
|
|
Win::UTF16 filenamew(Win::utf16(filename));
|
2020-02-10 15:33:00 +01:00
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
std::filesystem::path path(filenamew.get());
|
|
|
|
std::ifstream ifs(path, std::ios::binary);
|
|
|
|
#elif _MSC_VER
|
2019-04-30 14:58:40 +02:00
|
|
|
std::ifstream ifs(filenamew.get(), std::ios::binary);
|
|
|
|
#else
|
2011-11-26 12:33:39 +01:00
|
|
|
std::ifstream ifs(filename.c_str(), std::ios::binary);
|
2020-02-10 15:33:00 +01:00
|
|
|
#endif // __cplusplus
|
|
|
|
#else
|
|
|
|
std::ifstream ifs(filename.c_str(), std::ios::binary);
|
|
|
|
#endif // OPENVPN_PLATFORM_WIN
|
|
|
|
|
2011-11-26 12:33:39 +01:00
|
|
|
if (!ifs)
|
2014-08-12 02:02:21 +02:00
|
|
|
OPENVPN_THROW(open_file_error, "cannot open for read: " << filename);
|
2011-11-26 12:33:39 +01:00
|
|
|
|
|
|
|
// get length of file
|
|
|
|
ifs.seekg (0, std::ios::end);
|
|
|
|
const std::streamsize length = ifs.tellg();
|
2015-04-24 01:55:07 +02:00
|
|
|
if (max_size && std::uint64_t(length) > max_size)
|
2012-11-12 02:52:03 +01:00
|
|
|
OPENVPN_THROW(file_too_large, "file too large [" << length << '/' << max_size << "]: " << filename);
|
2011-11-26 12:33:39 +01:00
|
|
|
ifs.seekg (0, std::ios::beg);
|
|
|
|
|
|
|
|
// allocate buffer
|
2012-01-25 09:32:27 +01:00
|
|
|
BufferPtr b = new BufferAllocated(size_t(length), buffer_flags | BufferAllocated::ARRAY);
|
2011-11-26 12:33:39 +01:00
|
|
|
|
|
|
|
// read data
|
|
|
|
ifs.read((char *)b->data(), length);
|
|
|
|
|
|
|
|
// check for errors
|
|
|
|
if (ifs.gcount() != length)
|
2012-11-12 02:52:03 +01:00
|
|
|
OPENVPN_THROW(open_file_error, "read length inconsistency: " << filename);
|
2011-11-26 12:33:39 +01:00
|
|
|
if (!ifs)
|
2012-11-12 02:52:03 +01:00
|
|
|
OPENVPN_THROW(open_file_error, "cannot read: " << filename);
|
2011-11-26 12:33:39 +01:00
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2015-09-09 07:16:46 +02:00
|
|
|
// Read a file (may be text or binary) without seeking to determine
|
|
|
|
// its length.
|
|
|
|
inline BufferPtr read_binary_linear(const std::string& filename,
|
|
|
|
const std::uint64_t max_size = 0,
|
|
|
|
const size_t block_size = 1024)
|
|
|
|
{
|
|
|
|
std::ifstream ifs(filename.c_str(), std::ios::binary);
|
|
|
|
if (!ifs)
|
|
|
|
OPENVPN_THROW(open_file_error, "cannot open for read: " << filename);
|
|
|
|
|
|
|
|
BufferList buflist;
|
|
|
|
std::streamsize total_size = 0;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
BufferPtr b = new BufferAllocated(block_size, 0);
|
|
|
|
ifs.read((char *)b->data(), b->remaining());
|
|
|
|
const std::streamsize size = ifs.gcount();
|
|
|
|
if (size)
|
|
|
|
{
|
|
|
|
b->set_size(size);
|
|
|
|
total_size += size;
|
|
|
|
if (max_size && std::uint64_t(total_size) > max_size)
|
|
|
|
OPENVPN_THROW(file_too_large, "file too large [" << total_size << '/' << max_size << "]: " << filename);
|
|
|
|
buflist.push_back(std::move(b));
|
|
|
|
}
|
|
|
|
if (ifs.eof())
|
|
|
|
break;
|
|
|
|
if (!ifs)
|
|
|
|
OPENVPN_THROW(open_file_error, "cannot read: " << filename);
|
|
|
|
}
|
2015-09-18 19:14:03 +02:00
|
|
|
return buflist.join();
|
2015-09-09 07:16:46 +02:00
|
|
|
}
|
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// Read a text file as a std::string, throw error if file is binary
|
2015-04-24 01:55:07 +02:00
|
|
|
inline std::string read_text(const std::string& filename, const std::uint64_t max_size = 0)
|
2012-11-12 02:52:03 +01:00
|
|
|
{
|
|
|
|
BufferPtr bp = read_binary(filename, max_size);
|
|
|
|
if (bp->contains_null())
|
|
|
|
OPENVPN_THROW(file_is_binary, "file is binary: " << filename);
|
|
|
|
return std::string((const char *)bp->c_data(), bp->size());
|
|
|
|
}
|
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// Read a UTF-8 file as a std::string, throw errors if file is binary or malformed UTF-8
|
2015-04-24 01:55:07 +02:00
|
|
|
inline std::string read_text_utf8(const std::string& filename, const std::uint64_t max_size = 0)
|
2012-08-12 02:32:15 +02:00
|
|
|
{
|
2012-11-12 02:52:03 +01:00
|
|
|
BufferPtr bp = read_binary(filename, max_size);
|
2013-01-30 14:36:53 +01:00
|
|
|
|
|
|
|
// check if binary
|
2012-11-12 02:52:03 +01:00
|
|
|
if (bp->contains_null())
|
|
|
|
OPENVPN_THROW(file_is_binary, "file is binary: " << filename);
|
2013-01-30 14:36:53 +01:00
|
|
|
|
|
|
|
// remove Windows UTF-8 BOM if present
|
|
|
|
if (bp->size() >= 3)
|
|
|
|
{
|
|
|
|
const unsigned char *data = bp->c_data();
|
|
|
|
if (data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF)
|
|
|
|
bp->advance(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify that file is valid UTF-8
|
2015-12-23 03:10:53 +01:00
|
|
|
if (!Unicode::is_valid_utf8_uchar_buf(bp->c_data(), bp->size()))
|
2012-11-12 02:52:03 +01:00
|
|
|
OPENVPN_THROW(file_not_utf8, "file is not UTF8: " << filename);
|
2013-01-30 14:36:53 +01:00
|
|
|
|
2012-08-12 02:32:15 +02:00
|
|
|
return std::string((const char *)bp->c_data(), bp->size());
|
|
|
|
}
|
2014-08-12 02:02:21 +02:00
|
|
|
|
2016-03-15 00:13:19 +01:00
|
|
|
// Read multi-line string from stdin
|
|
|
|
inline std::string read_stdin()
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(std::cin, line))
|
|
|
|
{
|
|
|
|
ret += line;
|
|
|
|
ret += '\n';
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-12 02:02:21 +02:00
|
|
|
// Write binary buffer to file
|
|
|
|
inline void write_binary(const std::string& filename, const Buffer& buf)
|
|
|
|
{
|
|
|
|
std::ofstream ofs(filename.c_str(), std::ios::binary);
|
|
|
|
if (!ofs)
|
|
|
|
OPENVPN_THROW(open_file_error, "cannot open for write: " << filename);
|
|
|
|
ofs.write((const char *)buf.c_data(), buf.size());
|
|
|
|
if (!ofs)
|
|
|
|
OPENVPN_THROW(open_file_error, "cannot write: " << filename);
|
|
|
|
}
|
|
|
|
|
2015-10-16 08:37:00 +02:00
|
|
|
// Write binary buffer list to file
|
|
|
|
template <typename BUFLIST>
|
|
|
|
inline void write_binary_list(const std::string& filename, const BUFLIST& buflist)
|
|
|
|
{
|
|
|
|
std::ofstream ofs(filename.c_str(), std::ios::binary);
|
|
|
|
if (!ofs)
|
|
|
|
OPENVPN_THROW(open_file_error, "cannot open for write: " << filename);
|
|
|
|
for (auto &buf : buflist)
|
|
|
|
{
|
|
|
|
ofs.write((const char *)buf->c_data(), buf->size());
|
|
|
|
if (!ofs)
|
|
|
|
OPENVPN_THROW(open_file_error, "cannot write: " << filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 02:34:10 +01:00
|
|
|
// Write std::string to file
|
|
|
|
inline void write_string(const std::string& filename, const std::string& str)
|
|
|
|
{
|
|
|
|
BufferPtr buf = buf_from_string(str);
|
|
|
|
write_binary(filename, *buf);
|
|
|
|
}
|
|
|
|
|
2011-10-06 19:22:37 +02:00
|
|
|
} // namespace openvpn
|
|
|
|
|
|
|
|
#endif // OPENVPN_COMMON_FILE_H
|