summaryrefslogtreecommitdiff
path: root/hscript/script.cc
blob: 20a5c9d1393e2b72f28de086d4ae6d7e649f6c35 (plain) (blame)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
 * script.cc - Implementation of the Script class
 * 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 <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <set>

#include "script.hh"
#include "disk.hh"
#include "meta.hh"
#include "network.hh"
#include "user.hh"

#include "util/output.hh"

#define LINE_MAX 512


typedef Horizon::Keys::Key *(*key_parse_fn)(std::string, int, int*, int*);

const std::map<std::string, key_parse_fn> valid_keys = {
    {"network", &Horizon::Keys::Network::parseFromData},
    {"hostname", &Horizon::Keys::Hostname::parseFromData},
    {"pkginstall", &Horizon::Keys::PkgInstall::parseFromData},
    {"rootpw", &Horizon::Keys::RootPassphrase::parseFromData},

    {"language", &Horizon::Keys::Language::parseFromData},
    {"keymap", &Horizon::Keys::Keymap::parseFromData},
    {"firmware", &Horizon::Keys::Firmware::parseFromData},
    {"timezone", &Horizon::Keys::Timezone::parseFromData},
    {"repository", &Horizon::Keys::Repository::parseFromData},
    {"signingkey", &Horizon::Keys::SigningKey::parseFromData},

    {"netaddress", &Horizon::Keys::NetAddress::parseFromData},
    {"nameserver", &Horizon::Keys::Nameserver::parseFromData},
    {"netssid", &Horizon::Keys::NetSSID::parseFromData},

    {"username", &Horizon::Keys::Username::parseFromData},
    {"useralias", &Horizon::Keys::UserAlias::parseFromData},
    {"userpw", &Horizon::Keys::UserPassphrase::parseFromData},
    {"usericon", &Horizon::Keys::UserIcon::parseFromData},
    {"usergroups", &Horizon::Keys::UserGroups::parseFromData},

    {"diskid", &Horizon::Keys::DiskId::parseFromData},
    {"disklabel", &Horizon::Keys::DiskLabel::parseFromData},
    {"partition", &Horizon::Keys::Partition::parseFromData},
    {"lvm_pv", &Horizon::Keys::LVMPhysical::parseFromData},
    {"lvm_vg", &Horizon::Keys::LVMGroup::parseFromData},
    {"lvm_lv", &Horizon::Keys::LVMVolume::parseFromData},
    {"encrypt", &Horizon::Keys::Encrypt::parseFromData},
    {"fs", &Horizon::Keys::Filesystem::parseFromData},
    {"mount", &Horizon::Keys::Mount::parseFromData}
};


namespace Horizon {

struct Script::ScriptPrivate {
    /*! Determines whether or not to enable networking. */
    std::unique_ptr<Horizon::Keys::Network> network;
    /*! The target system's hostname. */
    std::unique_ptr<Horizon::Keys::Hostname> hostname;
    /*! The packages to install to the target system. */
    std::set<std::string> packages;
    /*! The root shadow line. */
    std::unique_ptr<Horizon::Keys::RootPassphrase> rootpw;
    /*! Target system's mountpoints. */
    std::vector< std::unique_ptr<Horizon::Keys::Mount> > mounts;

    /*! Network addressing configuration */
    std::vector< std::unique_ptr<Horizon::Keys::NetAddress> > addresses;

    /*! APK repositories */
    std::vector< std::unique_ptr<Horizon::Keys::Repository> > repos;

    /*! Store +key_obj+ representing the key +key_name+.
     * @param key_name      The name of the key that is being stored.
     * @param key_obj       The Key object associated with the key.
     * @param errors        Output parameter: if given, incremented on error.
     * @param warnings      Output parameter: if given, incremented on warning.
     * @param opts          Script parsing options.
     */
    bool store_key(const std::string key_name, Keys::Key *key_obj, int lineno,
                   int *errors, int *warnings, ScriptOptions opts) {
#define DUPLICATE_ERROR(OBJ, KEY, OLD_VAL) \
    std::string err_str("previous value was ");\
    err_str += OLD_VAL;\
    err_str += " at installfile:" + std::to_string(OBJ->lineno());\
    if(errors) *errors += 1;\
    output_error("installfile:" + std::to_string(lineno),\
                 "duplicate value for key '" + KEY + "'", err_str);

        if(key_name == "network") {
            if(this->network) {
                DUPLICATE_ERROR(this->network, std::string("network"),
                                this->network->test() ? "true" : "false")
                return false;
            }
            std::unique_ptr<Keys::Network> net(
                        dynamic_cast<Keys::Network *>(key_obj)
            );
            this->network = std::move(net);
            return true;
        } else if(key_name == "hostname") {
            if(this->hostname) {
                DUPLICATE_ERROR(this->hostname, std::string("hostname"),
                                this->hostname->value())
                return false;
            }
            std::unique_ptr<Keys::Hostname> name(
                        dynamic_cast<Keys::Hostname *>(key_obj)
            );
            this->hostname = std::move(name);
            return true;
        } else if(key_name == "pkginstall") {
            Keys::PkgInstall *install = dynamic_cast<Keys::PkgInstall *>(key_obj);
            for(auto &pkg : install->packages()) {
                if(opts.test(StrictMode) && packages.find(pkg) != packages.end()) {
                    if(warnings) *warnings += 1;
                    output_warning("installfile:" + std::to_string(lineno),
                                   "package '" + pkg + "' has already been specified",
                                   "");
                    continue;
                }
                packages.insert(pkg);
            }
            delete install;
            return true;
        } else if(key_name == "rootpw") {
            if(this->rootpw) {
                DUPLICATE_ERROR(this->rootpw, std::string("rootpw"),
                                "an encrypted passphrase")
                return false;
            }
            std::unique_ptr<Keys::RootPassphrase> name(
                        dynamic_cast<Keys::RootPassphrase *>(key_obj)
            );
            this->rootpw = std::move(name);
            return true;
        } else if(key_name == "mount") {
            std::unique_ptr<Keys::Mount> mount(
                        dynamic_cast<Keys::Mount *>(key_obj)
            );
            this->mounts.push_back(std::move(mount));
            return true;
        } else if(key_name == "netaddress") {
            std::unique_ptr<Keys::NetAddress> addr(
                        dynamic_cast<Keys::NetAddress *>(key_obj)
            );
            this->addresses.push_back(std::move(addr));
            return true;
        } else if(key_name == "repository") {
            std::unique_ptr<Keys::Repository> repo(
                        dynamic_cast<Keys::Repository *>(key_obj)
            );
            this->repos.push_back(std::move(repo));
            return true;
        } else {
            return false;
        }
#undef DUPLICATE_ERROR
    }
};


Script::Script() {
    internal = new ScriptPrivate;
}

Script::~Script() {
    delete internal;
}

const Script *Script::load(const std::string path, const ScriptOptions opts) {
    std::ifstream file(path);
    if(!file) {
        output_error(path, "Cannot open installfile", "");
        return nullptr;
    }

    return Script::load(file, opts);
}

#define PARSER_ERROR(err_str) \
    errors++;\
    output_error("installfile:" + std::to_string(lineno),\
                 err_str, "");\
    if(!opts.test(ScriptOptionFlags::KeepGoing)) {\
        break;\
    }

#define PARSER_WARNING(warn_str) \
    warnings++;\
    output_warning("installfile:" + std::to_string(lineno),\
                   warn_str, "");

const Script *Script::load(std::istream &sstream, const ScriptOptions opts) {
    using namespace Horizon::Keys;
    Script *the_script = new Script;

    int lineno = 0;
    char nextline[LINE_MAX];
    const std::string delim(" \t");
    int errors = 0, warnings = 0;

    while(sstream.getline(nextline, sizeof(nextline))) {
        lineno++;
        if(nextline[0] == '#') {
            /* This is a comment line; ignore it. */
            continue;
        }

        const std::string line(nextline);
        std::string key;
        std::string::size_type start, key_end, value_begin;
        start = line.find_first_not_of(delim);
        if(start == std::string::npos) {
            /* This is a blank line; ignore it. */
            continue;
        }

        key_end = line.find_first_of(delim, start);
        value_begin = line.find_first_not_of(delim, key_end);
        key = line.substr(start, (key_end - start));
        if(key_end == std::string::npos || value_begin == std::string::npos) {
            /* Key without value */
            PARSER_ERROR("key '" + key + "' has no value")
            continue;
        }

        /* Normalise key to lower-case */
        std::transform(key.begin(), key.end(), key.begin(), ::tolower);

        if(valid_keys.find(key) == valid_keys.end()) {
            /* Invalid key */
            if(opts.test(StrictMode)) {
                PARSER_ERROR("key '" + key + "' is not defined")
            } else {
                PARSER_WARNING("key '" + key + "' is not defined")
            }
            continue;
        }

        Key *key_obj = valid_keys.at(key)(line.substr(value_begin), lineno,
                                          &errors, &warnings);
        if(!key_obj) {
            PARSER_ERROR("value for key '" + key + "' was invalid")
            continue;
        }

        if(!the_script->internal->store_key(key, key_obj, lineno, &errors,
                                            &warnings, opts)) {
            PARSER_ERROR("stopping due to prior errors")
            continue;
        }
    }

    if(sstream.fail() && !sstream.eof()) {
        output_error("installfile:" + std::to_string(lineno + 1),
                     "line exceeds maximum length",
                     "Maximum line length is " + std::to_string(LINE_MAX));
        errors++;
    }

    if(sstream.bad() && !sstream.eof()) {
        output_error("installfile:" + std::to_string(lineno),
                     "I/O error while reading installfile", "");
        errors++;
    }

    /* Ensure all required keys are present. */
#define MISSING_ERROR(key) \
    output_error("installfile:" + std::to_string(lineno),\
                 "expected value for key '" + std::string(key) + "'",\
                 "this key is required");\
    errors++;

    if(errors == 0) {
        if(!the_script->internal->network) {
            MISSING_ERROR("network")
        }
        if(!the_script->internal->hostname) {
            MISSING_ERROR("hostname")
        }
        if(the_script->internal->packages.size() == 0) {
            MISSING_ERROR("pkginstall")
        }
        if(!the_script->internal->rootpw) {
            MISSING_ERROR("rootpw")
        }
        if(the_script->internal->mounts.size() == 0) {
            MISSING_ERROR("mount")
        }
    }
#undef MISSING_ERROR

    output_log("parser", "0", "installfile",
               std::to_string(errors) + " error(s), " +
               std::to_string(warnings) + " warning(s).", "");

    if(errors > 0) {
        delete the_script;
        return nullptr;
    } else {
        the_script->opts = opts;
        return the_script;
    }
}

bool Script::validate() const {
    int failures = 0;
    std::set<std::string> seen_mounts;
    std::map<const std::string, int> seen_iface;

    if(!this->internal->network->validate(this->opts)) failures++;
    if(!this->internal->hostname->validate(this->opts)) failures++;
    if(!this->internal->rootpw->validate(this->opts)) failures++;

    for(auto &mount : this->internal->mounts) {
        if(!mount->validate(this->opts)) {
            failures++;
            continue;
        }

        /* Runner.Validate.mount.Unique */
        if(seen_mounts.find(mount->mountpoint()) != seen_mounts.end()) {
            failures++;
            output_error("installfile:" + std::to_string(mount->lineno()),
                         "mount: mountpoint " + mount->mountpoint() +
                         " has already been specified; " + mount->device() +
                         " is a duplicate");
        }
        seen_mounts.insert(mount->mountpoint());
        if(this->opts.test(InstallEnvironment)) {
            /* TODO: Runner.Validate.mount.Block for not-yet-created devs. */
        }
    }

    /* Runner.Validate.mount.Root */
    if(seen_mounts.find("/") == seen_mounts.end()) {
        failures++;
        output_error("installfile:0", "mount: no root mount specified");
    }

    /* Runner.Validate.network.netaddress */
    if(this->internal->network->test() &&
       this->internal->addresses.size() == 0) {
        failures++;
        output_error("installfile:0",
                     "networking requested but no 'netaddress' defined",
                     "You need to specify at least one address to enable "
                     "networking.");
    }

    for(auto &address : this->internal->addresses) {
        if(!address->validate(this->opts)) {
            failures++;
        }

        /* Runner.Validate.network.netaddress.Count */
        if(seen_iface.find(address->iface()) == seen_iface.end()) {
            seen_iface.insert(std::make_pair(address->iface(), 1));
        } else {
            seen_iface[address->iface()] += 1;
            if(seen_iface[address->iface()] > 255) {
                failures++;
                output_error("installfile:" + std::to_string(address->lineno()),
                             "netaddress: interface '" + address->iface() +
                             "' has too many addresses assigned");
            }
        }
    }

    if(this->internal->repos.size() == 0) {
        Keys::Repository *sys_key = dynamic_cast<Keys::Repository *>(
            Horizon::Keys::Repository::parseFromData(
                "https://distfiles.adelielinux.org/adelie/stable/system", 0,
                nullptr, nullptr
            )
        );
        if(!sys_key) {
            output_error("internal", "failed to create default system repository");
            return false;
        }
        std::unique_ptr<Keys::Repository> sys_repo(sys_key);
        this->internal->repos.push_back(std::move(sys_repo));
        Keys::Repository *user_key = dynamic_cast<Keys::Repository *>(
            Horizon::Keys::Repository::parseFromData(
                "https://distfiles.adelielinux.org/adelie/stable/user", 0,
                nullptr, nullptr
            )
        );
        if(!user_key) {
            output_error("internal", "failed to create default user repository");
            return false;
        }
        std::unique_ptr<Keys::Repository> user_repo(user_key);
        this->internal->repos.push_back(std::move(user_repo));
    }

    for(auto &repo : this->internal->repos) {
        if(!repo->validate(this->opts)) {
            failures++;
        }
    }

    if(this->internal->repos.size() > 10) {
        failures++;
        output_error("installfile:" +
                     std::to_string(this->internal->repos[11]->lineno()),
                     "repository: too many repositories specified",
                     "You may only specify up to 10 repositories.");
    }

    output_log("validator", "0", "installfile",
               std::to_string(failures) + " failure(s).", "");
    return (failures == 0);
}

bool Script::execute() const {
    bool success;

    /* Runner.Execute.Verify */
    output_step_start("validate");
    success = this->validate();
    output_step_end("validate");
    if(!success) {
        /* Runner.Execute.Verify.Failure */
        output_error("validator", "The HorizonScript failed validation",
                     "Check the output from the validator.");
        return false;
    }

#define EXECUTE_FAILURE(phase) \
    output_error(phase, "The HorizonScript failed to execute",\
                 "Check the log file for more details.")

    /**************** DISK SETUP ****************/
    output_step_start("disk");
    /* Sort by mountpoint.
     * This ensures that any subdirectory mounts come after their parent. */
    std::sort(this->internal->mounts.begin(), this->internal->mounts.end(),
              [](std::unique_ptr<Keys::Mount> const &e1,
                 std::unique_ptr<Keys::Mount> const &e2) {
        return e1->mountpoint() < e2->mountpoint();
    });
    for(auto &mount : this->internal->mounts) {
        if(!mount->execute(opts)) {
            EXECUTE_FAILURE("disk");
            return false;
        }
    }
    output_step_end("disk");

    /**************** PRE PACKAGE METADATA ****************/
    output_step_start("pre-metadata");
    if(!this->internal->hostname->execute(opts)) {
        EXECUTE_FAILURE("pre-metadata");
        return false;
    }
    output_step_end("pre-metadata");

    /**************** PKGDB ****************/
    output_step_start("pkgdb");
    for(auto &repo : this->internal->repos) {
        if(!repo->execute(opts)) {
            EXECUTE_FAILURE("pkgdb");
            return false;
        }
    }

    /* Runner.Execute.pkginstall.APKDB */
    output_info("internal", "initialising APK");
    if(opts.test(Simulate)) {
        std::cout << "apk --root /target --initdb add" << std::endl;
    } else {
        if(system("apk --root /target --initdb add") != 0) {
            EXECUTE_FAILURE("pkgdb");
            return false;
        }
    }

    /* Runner.Execute.pkginstall */
    output_info("internal", "installing packages to target");
    std::ostringstream pkg_list;
    for(auto &pkg : this->internal->packages) {
        pkg_list << pkg << " ";
    }
    if(opts.test(Simulate)) {
        std::cout << "apk --root /target update" << std::endl;
        std::cout << "apk --root /target add " << pkg_list.str() << std::endl;
    } else {
        if(system("apk --root /target update") != 0) {
            EXECUTE_FAILURE("pkgdb");
            return false;
        }
        std::string apk_invoc = "apk --root /target add " + pkg_list.str();
        if(system(apk_invoc.c_str()) != 0) {
            EXECUTE_FAILURE("pkgdb");
            return false;
        }
    }

    output_step_end("pkgdb");

    /**************** POST PACKAGE METADATA ****************/
    output_step_start("post-metadata");
    if(!this->internal->rootpw->execute(opts)) {
        EXECUTE_FAILURE("post-metadata");
        return false;
    }
    output_step_end("post-metadata");
    return true;
}

}