1 /*
2 * Copyright (C) 2018 Rafael Ostertag
3 *
4 * This file is part of YAPET.
5 *
6 * YAPET is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
9 * version.
10 *
11 * YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * YAPET. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Additional permission under GNU GPL version 3 section 7
20 *
21 * If you modify this program, or any covered work, by linking or combining it
22 * with the OpenSSL project's OpenSSL library (or a modified version of that
23 * library), containing parts covered by the terms of the OpenSSL or SSLeay
24 * licenses, Rafael Ostertag grants you additional permission to convey the
25 * resulting work. Corresponding Source for a non-source form of such a
26 * combination shall include the source code for the parts of OpenSSL used as
27 * well as that of the covered work.
28 */
29
30 #include <cstdio>
31
32 #include "consts.h"
33 #include "fileerror.hh"
34 #include "fileutils.hh"
35 #include "intl.h"
36 #include "yapetfile.hh"
37
38 using namespace yapet;
39
YapetFile(const std::string & filename,bool create,bool secure)40 YapetFile::YapetFile(const std::string& filename, bool create, bool secure)
41 : _rawFile{filename}, _create{create}, _secure{secure} {}
42
YapetFile(YapetFile && other)43 YapetFile::YapetFile(YapetFile&& other)
44 : _rawFile{std::move(other._rawFile)},
45 _create{other._create},
46 _secure{other._secure} {}
47
operator =(YapetFile && other)48 YapetFile& YapetFile::operator=(YapetFile&& other) {
49 if (&other == this) {
50 return *this;
51 }
52
53 _rawFile = std::move(other._rawFile);
54 _create = other._create;
55 _secure = other._secure;
56
57 return *this;
58 }
59
~YapetFile()60 YapetFile::~YapetFile() {}
61
openRawFile()62 void YapetFile::openRawFile() {
63 if (_create) {
64 _rawFile.openNew();
65 setSecurePermissionsAndOwner(_rawFile.filename());
66 } else {
67 if (_secure && !hasSecurePermissions(_rawFile.filename())) {
68 char msg[YAPET::Consts::EXCEPTION_MESSAGE_BUFFER_SIZE];
69 std::snprintf(msg, YAPET::Consts::EXCEPTION_MESSAGE_BUFFER_SIZE,
70 _("'%s' has insecure permissions"),
71 _rawFile.filename().c_str());
72 throw FileInsecureError{msg};
73 }
74
75 _rawFile.openExisting();
76 }
77 }