nbos

nbos

https://git.tonybtw.com/nbos.git git://git.tonybtw.com/nbos.git
2,476 bytes raw
1
#include "validate.h"
2
3
#include <stdio.h>
4
#include <string.h>
5
6
static bool is_known_bootloader(const char *name) {
7
    return strcmp(name, "limine")   == 0
8
        || strcmp(name, "grub")     == 0
9
        || strcmp(name, "syslinux") == 0;
10
}
11
12
/**
13
 * validate() - Run static checks on the system config.
14
 * @cfg: Configuration to inspect.
15
 *
16
 * Covers what resolve() does not: hostname presence, bootloader
17
 * recognition, non-null kernel/shell pointers, and uniqueness of user
18
 * and service names. Pkg references (kernel, shells, declared pkgs)
19
 * are all closure roots — resolve() auto-discovers transitive deps,
20
 * so no "is in the package list" check is needed. Errors are printed
21
 * to stderr.
22
 *
23
 * Return: true if every check passes.
24
 */
25
bool validate(const system_cfg *cfg) {
26
    bool ok = true;
27
28
    if (cfg->hostname == nullptr || cfg->hostname[0] == '\0') {
29
        fprintf(stderr, "nb: hostname is empty\n");
30
        ok = false;
31
    }
32
33
    if (!is_known_bootloader(cfg->boot.bootloader)) {
34
        fprintf(stderr, "nb: unknown bootloader '%s'\n", cfg->boot.bootloader);
35
        ok = false;
36
    }
37
38
    if (cfg->boot.kernel == nullptr) {
39
        fprintf(stderr, "nb: boot.kernel is null\n");
40
        ok = false;
41
    }
42
43
    for (size_t i = 0; i < cfg->users.len; i++) {
44
        const user *u = &cfg->users.data[i];
45
46
        if (u->name == nullptr || u->name[0] == '\0') {
47
            fprintf(stderr, "nb: user[%zu] has empty name\n", i);
48
            ok = false;
49
            continue;
50
        }
51
52
        if (u->shell == nullptr) {
53
            fprintf(stderr, "nb: user '%s' has null shell\n", u->name);
54
            ok = false;
55
        }
56
57
        for (size_t j = i + 1; j < cfg->users.len; j++) {
58
            if (strcmp(u->name, cfg->users.data[j].name) == 0) {
59
                fprintf(stderr, "nb: duplicate user name '%s'\n", u->name);
60
                ok = false;
61
            }
62
        }
63
    }
64
65
    for (size_t i = 0; i < cfg->services.len; i++) {
66
        const service *s = &cfg->services.data[i];
67
68
        if (s->name == nullptr || s->name[0] == '\0') {
69
            fprintf(stderr, "nb: service[%zu] has empty name\n", i);
70
            ok = false;
71
            continue;
72
        }
73
74
        for (size_t j = i + 1; j < cfg->services.len; j++) {
75
            if (strcmp(s->name, cfg->services.data[j].name) == 0) {
76
                fprintf(stderr, "nb: duplicate service name '%s'\n", s->name);
77
                ok = false;
78
            }
79
        }
80
    }
81
82
    return ok;
83
}