1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/*
* meta.cc - Implementation of the Key classes for system metadata
* libhscript, the HorizonScript library for
* Project Horizon
*
* Copyright (c) 2019 Adélie Linux and contributors. All rights reserved.
* This code is licensed under the AGPL 3.0 license, as noted in the
* LICENSE-code file in the root directory of this repository.
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
#include <assert.h>
#include <fstream>
#include <regex>
#ifdef HAS_INSTALL_ENV
# include <unistd.h>
#endif /* HAS_INSTALL_ENV */
#include "meta.hh"
#include "util/output.hh"
using namespace Horizon::Keys;
Key *Hostname::parseFromData(const std::string &data, int lineno, int *errors,
int *) {
std::regex valid_re("[A-Za-z0-9-_.]*");
if(!std::regex_match(data, valid_re)) {
if(errors) *errors += 1;
output_error("installfile:" + std::to_string(lineno),
"hostname: expected machine or DNS name",
"'" + data + "' is not a valid hostname");
return nullptr;
}
return new Hostname(lineno, data);
}
bool Hostname::validate(ScriptOptions) const {
/* Validate that the name is a valid machine or DNS name */
bool any_failure = false;
std::string::size_type last_dot = 0, next_dot = 0;
if(!isalpha(this->_value[0])) {
any_failure = true;
output_error("installfile:" + std::to_string(this->lineno()),
"hostname: must start with alphabetical character");
}
if(this->_value.size() > 320) {
any_failure = true;
output_error("installfile:" + std::to_string(this->lineno()),
"hostname: value too long",
"valid host names must be less than 320 characters");
}
do {
next_dot = this->_value.find_first_of('.', next_dot + 1);
if(next_dot == std::string::npos) {
next_dot = this->_value.size();
}
if(next_dot - last_dot > 64) {
any_failure = true;
output_error("installfile:" + std::to_string(this->lineno()),
"hostname: component too long",
"each component must be less than 64 characters");
}
last_dot = next_dot;
} while(next_dot != this->_value.size());
return !any_failure;
}
bool Hostname::execute(ScriptOptions opts) const {
/* Set the hostname of the target computer */
std::string actual;
std::string::size_type dot = this->_value.find_first_of('.');
if(this->_value.size() > 64) {
/* Linux has a nodename limit of 64 characters.
* That's fine, because we have a limit of 64 chars per segment.
* Assuming a dot is present, just chop at the first dot. */
assert(dot <= 64);
actual = this->_value.substr(0, dot);
} else {
actual = this->_value;
}
/* Runner.Execute.hostname. */
output_info("installfile:" + std::to_string(this->lineno()),
"hostname: set hostname to '" + actual + "'");
if(opts.test(Simulate)) {
std::cout << "hostname " << actual << std::endl;
}
#ifdef HAS_INSTALL_ENV
else {
if(sethostname(actual.c_str(), actual.size()) == -1) {
output_error("installfile:" + std::to_string(this->lineno()),
"hostname: failed to set host name",
std::string(strerror(errno)));
return false;
}
}
#endif /* HAS_INSTALL_ENV */
/* Runner.Execute.hostname.Write. */
output_info("installfile:" + std::to_string(this->lineno()),
"hostname: write '" + actual + "' to /etc/hostname");
if(opts.test(Simulate)) {
std::cout << "printf '%s' " << actual << " > /target/etc/hostname"
<< std::endl;
}
#ifdef HAS_INSTALL_ENV
else {
std::ofstream hostname_f("/target/etc/hostname");
if(!hostname_f) {
output_error("installfile:" + std::to_string(this->lineno()),
"hostname: could not open /etc/hostname for writing");
return false;
}
hostname_f << actual;
}
#endif /* HAS_INSTALL_ENV */
/* The second condition ensures that it isn't a single dot that simply
* terminates the nodename. */
if(dot != std::string::npos && this->_value.length() > dot + 1) {
const std::string domain(this->_value.substr(dot + 1));
output_info("installfile:" + std::to_string(this->lineno()),
"hostname: set domain name '" + domain + "'");
if(opts.test(Simulate)) {
std::cout << "printf 'dns_domain_lo=\"" << domain
<< "\"\\" << "n' >> /target/etc/conf.d/net" << std::endl;
}
#ifdef HAS_INSTALL_ENV
else {
std::ofstream net_conf_f("/target/etc/conf.d/net");
if(!net_conf_f) {
output_error("installfile:" + std::to_string(this->lineno()),
"hostname: could not open /etc/conf.d/net for "
"writing");
return false;
}
net_conf_f << "dns_domain_lo\"" << domain << "\"" << std::endl;
}
#endif /* HAS_INSTALL_ENV */
}
return true;
}
static std::regex valid_pkg("[0-9A-Za-z+_.-]*((>?<|[<>]?=|[~>])[0-9A-Za-z-_.]+)?");
Key *PkgInstall::parseFromData(const std::string &data, int lineno, int *errors,
int *warnings) {
std::string next_pkg;
std::istringstream stream(data);
std::set<std::string> all_pkgs;
while(stream >> next_pkg) {
if(!std::regex_match(next_pkg, valid_pkg)) {
if(errors) *errors += 1;
output_error("installfile:" + std::to_string(lineno),
"pkginstall: expected package name",
"'" + next_pkg + "' is not a valid package or atom");
return nullptr;
}
if(all_pkgs.find(next_pkg) != all_pkgs.end()) {
if(warnings) *warnings += 1;
output_warning("installfile:" + std::to_string(lineno),
"pkginstall: package '" + next_pkg +
"' is already in the target package set");
continue;
}
all_pkgs.insert(next_pkg);
}
return new PkgInstall(lineno, all_pkgs);
}
Key *Firmware::parseFromData(const std::string &data, int lineno, int *errors,
int *warnings) {
bool value;
if(!BooleanKey::parse(data, "installfile:" + std::to_string(lineno),
"firmware", &value)) {
if(errors) *errors += 1;
return nullptr;
}
if(value) {
#ifdef NON_LIBRE_FIRMWARE
output_warning("installfile:" + std::to_string(lineno),
"firmware: You have requested non-libre firmware. "
"This may cause security issues, system instability, "
"and many other issues. You should not enable this "
"option unless your system absolutely requires it.");
#else
if(errors) *errors += 1;
output_error("installfile:" + std::to_string(lineno),
"firmware: You have requested non-libre firmware, "
"but this version of Horizon does not support "
"non-libre firmware.", "Installation cannot proceed.");
return nullptr;
#endif
}
return new Firmware(lineno, value);
}
bool Firmware::execute(ScriptOptions) const {
/* By itself, this does nothing. */
return true;
}
/* LCOV_EXCL_START */
bool PkgInstall::validate(ScriptOptions) const {
/* Any validation errors would have occurred above. */
return true;
}
bool PkgInstall::execute(ScriptOptions) const {
/* Package installation is handled in Script::execute. */
return true;
}
/* LCOV_EXCL_STOP */
Key *Repository::parseFromData(const std::string &data, int lineno, int *errors,
int *) {
if(data.empty() || (data[0] != '/' && data.compare(0, 4, "http"))) {
if(errors) *errors += 1;
output_error("installfile:" + std::to_string(lineno),
"repository: must be absolute path or HTTP(S) URL");
return nullptr;
}
return new Repository(lineno, data);
}
bool Repository::validate(ScriptOptions) const {
/* TODO XXX: Ensure URL is accessible if networking is available */
return true;
}
bool Repository::execute(ScriptOptions opts) const {
/* Runner.Execute.repository. */
output_info("installfile:" + std::to_string(this->lineno()),
"repository: write '" + this->value() +
"' to /etc/apk/repositories");
if(opts.test(Simulate)) {
std::cout << "echo '" << this->value() <<
"' >> /target/etc/apk/repositories" << std::endl;
return true;
}
#ifdef HAS_INSTALL_ENV
std::ofstream repo_f("/target/etc/apk/repositories",
std::ios_base::ate);
if(!repo_f) {
output_error("installfile:" + std::to_string(this->lineno()),
"repository: could not open /etc/apk/repositories "
"for writing");
return false;
}
repo_f << this->value() << std::endl;
return true;
#else
return false;
#endif /* HAS_INSTALL_ENV */
}
|