diff --git a/include/Mcde/McdeSys.h b/include/Mcde/McdeSys.h index 2feb301b..cebb1260 100644 --- a/include/Mcde/McdeSys.h +++ b/include/Mcde/McdeSys.h @@ -16,7 +16,7 @@ * * Status: lib/McdeSys implements mcde_sys_backend, mcde_sys_error, * mcde_sys_info, mcde_sys_power, mcde_sys_interfaces, the sound, Bluetooth, - * battery, time zone, sharing, the network browser, + * battery, time zone, sharing, the network browser, disks, * user and software update functions (common.c, net.c, sound.c, users.c, * netbsd.c, linux.c); the network setup and the host name setter are the * contract still. @@ -243,6 +243,45 @@ int mcde_sys_net_mount(const char *url, const char *mountpoint, /* disconnect what is mounted at MOUNTPOINT */ int mcde_sys_net_unmount(const char *mountpoint); +/* + * Disks: the drives and their partitions (NetBSD: wedges), where they are + * mounted, and what can be done to them. NAME is the system's: wd0, dk1, + * sd0 / nvme0n1, sda1. Changing a disk needs root. + */ +typedef struct { + char name[32]; + char parent[32]; /* the drive of a partition; "" for a drive */ + char model[96]; /* a drive's model, or "" */ + char fstype[32]; /* ffs, msdos, ext2fs, swap, cd9660 ...; "" */ + char label[96]; /* the partition's or volume's name, or "" */ + unsigned long long bytes; + int removable; /* a stick, a card, a CD */ + int system; /* holds / or swap: never erased */ + char mountpoint[256]; /* where mounted, or "" */ + unsigned long long used, avail; /* when mounted */ +} McdeSysDisk; + +/* the drives, each followed by its partitions; how many */ +int mcde_sys_disks(McdeSysDisk *d, size_t max); +/* mount partition NAME on /media/LABEL (made); where, into MOUNTPOINT */ +int mcde_sys_disk_mount(const char *name, char *mountpoint, size_t len); +int mcde_sys_disk_unmount(const char *name); +/* check partition NAME without changing it (fsck -n); its report into LOG */ +int mcde_sys_disk_check(const char *name, char *log, size_t len); +/* erase the whole drive NAME: one partition, FSTYPE ("ffs", "msdos"; + * Linux also "ext4") named LABEL; the new partition's name into PART. + * Refused for a drive with a system partition or one that is mounted. */ +int mcde_sys_disk_erase(const char *name, const char *fstype, + const char *label, char *part, size_t len); + +/* + * The permissions of the system's files: compared with the list the system + * keeps of how they should be, and set back to it (REPAIR, root). What + * differs -- or what was set back -- goes into LOG, a line each; how many + * lines. NetBSD: mtree(8) against /etc/mtree/set.* (every installed set). + */ +int mcde_sys_permissions(int repair, char *log, size_t len); + /* * A process: the command line it was started with, as one line a shell * reads back (each argument in single quotes). For the front panel, which diff --git a/lib/McdeSys/Makefile.am b/lib/McdeSys/Makefile.am index 9acb9664..dfbe67aa 100644 --- a/lib/McdeSys/Makefile.am +++ b/lib/McdeSys/Makefile.am @@ -7,12 +7,12 @@ lib_LTLIBRARIES = libMcdeSys.la libMcdeSys_la_CPPFLAGS = -I$(top_srcdir)/include libMcdeSys_la_SOURCES = common.c net.c sound.c users.c share.c netmount.c private.h if NETBSD -libMcdeSys_la_SOURCES += netbsd.c +libMcdeSys_la_SOURCES += netbsd.c disk_netbsd.c # crypt(3), for the password hash; proplib(3), for the battery; libdns_sd # (base), for the computers on the network (netbsd.c) libMcdeSys_la_LIBADD = -lcrypt -lprop -ldns_sd else -libMcdeSys_la_SOURCES += linux.c +libMcdeSys_la_SOURCES += linux.c disk_linux.c endif libMcdeSys_la_LDFLAGS = -version-info 1:0:0 diff --git a/lib/McdeSys/disk_linux.c b/lib/McdeSys/disk_linux.c new file mode 100644 index 00000000..63a004d0 --- /dev/null +++ b/lib/McdeSys/disk_linux.c @@ -0,0 +1,274 @@ +/* + * disk_linux.c -- MCDE: the disks of a Linux system (McdeSys) + * + * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. + * + * lsblk(8) -P -b (KEY="value" pairs, sizes in bytes) for the drives and + * partitions; udisksctl(1) mounts and unmounts where it is there (as the + * user, with the system's permission), else mount(8); fsck(8) -n looks; + * erasing is sfdisk(8) (one GPT partition) and mkfs.ext4 or mkfs.vfat. + * Loop devices, LVM and crypto layers are not drives of their own here. + */ + +#define _POSIX_C_SOURCE 200809L + +#include +#include +#include +#include +#include + +#include + +#include "private.h" + +static const char *const lsblkPaths[] = { "/usr/bin/lsblk", "/bin/lsblk", + NULL }; +static const char *const udisksPaths[] = { "/usr/bin/udisksctl", NULL }; + +/* the value of KEY="..." in LINE */ +static void +Pair(const char *line, const char *key, char *buf, size_t len) +{ + char pat[32]; + const char *p, *e; + + buf[0] = '\0'; + snprintf(pat, sizeof pat, " %s=\"", key); + if (strncmp(line, pat + 1, strlen(pat + 1)) == 0) + p = line + strlen(pat + 1); + else if ((p = strstr(line, pat)) != NULL) + p += strlen(pat); + else + return; + if ((e = strchr(p, '"')) == NULL) + return; + snprintf(buf, len, "%.*s", (int) (e - p), p); +} + +int +mcde_sys_disks(McdeSysDisk *d, size_t max) +{ + static char out[65536]; + const char *lb = mcde_sys_which(lsblkPaths); + char *argv[] = { NULL, "-P", "-b", "-o", + "NAME,PKNAME,TYPE,SIZE,MODEL,FSTYPE,LABEL,MOUNTPOINT,RM", NULL }; + char *line, *last = NULL, type[16], size[32], rm[8]; + size_t n = 0; + + if (lb == NULL) + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "lsblk is not there."); + argv[0] = (char *) lb; + if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK) + return -1; + for (line = strtok_r(out, "\n", &last); line != NULL && n < max; + line = strtok_r(NULL, "\n", &last)) { + McdeSysDisk *x = &d[n]; + + Pair(line, "TYPE", type, sizeof type); + if (strcmp(type, "disk") != 0 && strcmp(type, "part") != 0 && + strcmp(type, "rom") != 0) + continue; + memset(x, 0, sizeof *x); + Pair(line, "NAME", x->name, sizeof x->name); + if (strcmp(type, "part") == 0) + Pair(line, "PKNAME", x->parent, sizeof x->parent); + Pair(line, "MODEL", x->model, sizeof x->model); + Pair(line, "FSTYPE", x->fstype, sizeof x->fstype); + Pair(line, "LABEL", x->label, sizeof x->label); + Pair(line, "MOUNTPOINT", x->mountpoint, sizeof x->mountpoint); + Pair(line, "SIZE", size, sizeof size); + Pair(line, "RM", rm, sizeof rm); + x->bytes = strtoull(size, NULL, 10); + x->removable = strcmp(rm, "1") == 0 || strcmp(type, "rom") == 0; + if (strcmp(x->mountpoint, "[SWAP]") == 0) { + x->mountpoint[0] = '\0'; + x->system = 1; + } + if (strcmp(x->mountpoint, "/") == 0 || + strcmp(x->mountpoint, "/boot") == 0 || + strcmp(x->mountpoint, "/boot/efi") == 0 || + strcmp(x->mountpoint, "/usr") == 0 || + strcmp(x->mountpoint, "/home") == 0 || + strcmp(x->fstype, "crypto_LUKS") == 0 || + strcmp(x->fstype, "LVM2_member") == 0) + x->system = 1; + if (x->mountpoint[0] != '\0') { + struct statvfs sv; + + if (statvfs(x->mountpoint, &sv) == 0) { + x->used = (unsigned long long) (sv.f_blocks - + sv.f_bfree) * sv.f_frsize; + x->avail = (unsigned long long) sv.f_bavail * + sv.f_frsize; + } + } + /* a system partition makes its drive the system's */ + if (x->system && x->parent[0] != '\0') { + size_t k; + + for (k = 0; k < n; k++) + if (strcmp(d[k].name, x->parent) == 0) + d[k].system = 1; + } + n++; + } + return (int) n; +} + +static int +Find(const char *name, McdeSysDisk *out) +{ + static McdeSysDisk all[64]; + int n = mcde_sys_disks(all, 64), i; + + for (i = 0; i < n; i++) + if (strcmp(all[i].name, name) == 0) { + *out = all[i]; + return 1; + } + return 0; +} + +int +mcde_sys_disk_mount(const char *name, char *mountpoint, size_t len) +{ + McdeSysDisk d; + char dev[64], out[512]; + const char *ud = mcde_sys_which(udisksPaths); + + if (!Find(name, &d)) + return mcde_sys_fail(MCDE_SYS_ENOENT, "There is no disk %s.", + name); + if (d.mountpoint[0] != '\0') { + snprintf(mountpoint, len, "%s", d.mountpoint); + return MCDE_SYS_OK; + } + if (ud == NULL) + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "udisksctl is not " + "there."); + snprintf(dev, sizeof dev, "/dev/%s", name); + { + char *argv[] = { (char *) ud, "mount", "-b", dev, NULL }; + + if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK) + return MCDE_SYS_EFAIL; + } + /* "Mounted /dev/sdb1 at /media/user/STICK" */ + if (!Find(name, &d) || d.mountpoint[0] == '\0') + return mcde_sys_fail(MCDE_SYS_EFAIL, "%s", out); + snprintf(mountpoint, len, "%s", d.mountpoint); + return MCDE_SYS_OK; +} + +int +mcde_sys_disk_unmount(const char *name) +{ + McdeSysDisk d; + char dev[64]; + const char *ud = mcde_sys_which(udisksPaths); + + if (!Find(name, &d) || d.mountpoint[0] == '\0') + return mcde_sys_fail(MCDE_SYS_ENOENT, "%s is not mounted.", + name); + if (d.system) + return mcde_sys_fail(MCDE_SYS_EPERM, "%s is the system's.", + name); + snprintf(dev, sizeof dev, "/dev/%s", name); + if (ud != NULL) { + char *argv[] = { (char *) ud, "unmount", "-b", dev, NULL }; + + return mcde_sys_run(argv); + } else { + char *argv[] = { "/bin/umount", d.mountpoint, NULL }; + + return mcde_sys_run(argv); + } +} + +int +mcde_sys_disk_check(const char *name, char *log, size_t len) +{ + McdeSysDisk d; + char dev[64]; + char *argv[] = { "/sbin/fsck", "-n", NULL, NULL }; + + log[0] = '\0'; + if (!Find(name, &d) || d.fstype[0] == '\0' || + strcmp(d.fstype, "swap") == 0) + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "%s cannot be checked.", + name); + snprintf(dev, sizeof dev, "/dev/%s", name); + argv[2] = dev; + return mcde_sys_capture(argv, log, len); +} + +int +mcde_sys_disk_erase(const char *name, const char *fstype, const char *label, + char *part, size_t len) +{ + static McdeSysDisk all[64]; + char dev[64], pdev[80]; + int n, i; + + if (geteuid() != 0) + return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator " + "can erase a disk."); + if (label == NULL || label[0] == '\0' || strlen(label) > 11 || + strspn(label, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789_-") != strlen(label)) + return mcde_sys_fail(MCDE_SYS_EINVAL, "A name has letters, " + "digits, - and _, at most 11."); + n = mcde_sys_disks(all, 64); + for (i = 0; i < n; i++) + if (strcmp(all[i].name, name) == 0) + break; + if (i == n || all[i].parent[0] != '\0') + return mcde_sys_fail(MCDE_SYS_EINVAL, "%s is no drive.", name); + if (all[i].system) + return mcde_sys_fail(MCDE_SYS_EPERM, "%s holds the system and " + "is not erased.", name); + for (i = 0; i < n; i++) + if ((strcmp(all[i].name, name) == 0 || + strcmp(all[i].parent, name) == 0) && + all[i].mountpoint[0] != '\0') + return mcde_sys_fail(MCDE_SYS_EPERM, "%s is mounted on " + "%s: unmount it first.", all[i].name, + all[i].mountpoint); + snprintf(dev, sizeof dev, "/dev/%s", name); + { + char *argv[] = { "/sbin/sfdisk", "--quiet", dev, NULL }; + + /* one GPT partition over the whole drive (sfdisk(8)) */ + if (mcde_sys_run_input(argv, "label: gpt\n,,L\n") != MCDE_SYS_OK) + return MCDE_SYS_EFAIL; + } + /* sda -> sda1, nvme0n1 -> nvme0n1p1 */ + snprintf(part, len, "%s%s1", name, + name[strlen(name) - 1] >= '0' && name[strlen(name) - 1] <= '9' ? + "p" : ""); + snprintf(pdev, sizeof pdev, "/dev/%s", part); + if (strcmp(fstype, "msdos") == 0) { + char *argv[] = { "/sbin/mkfs.vfat", "-F", "32", "-n", + (char *) label, pdev, NULL }; + + return mcde_sys_run(argv); + } else { + char *argv[] = { "/sbin/mkfs.ext4", "-q", "-L", (char *) label, + pdev, NULL }; + + return mcde_sys_run(argv); + } +} + +/* ---- the permissions: Linux distributions keep no list of how their + * files' permissions should be (dpkg --verify checks contents) */ +int +mcde_sys_permissions(int repair, char *log, size_t len) +{ + (void) repair; + if (len > 0) + log[0] = '\0'; + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "This system keeps no list of " + "how its files' permissions should be."); +} diff --git a/lib/McdeSys/disk_netbsd.c b/lib/McdeSys/disk_netbsd.c new file mode 100644 index 00000000..b8096527 --- /dev/null +++ b/lib/McdeSys/disk_netbsd.c @@ -0,0 +1,485 @@ +/* + * disk_netbsd.c -- MCDE: the disks of a NetBSD system (McdeSys) + * + * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. + * + * NetBSD's own tools and calls (docs/reference/disks.txt in the Makronix + * tree): the drives from sysctl hw.disknames, their size from dkctl(8) + * getgeometry, their partitions -- wedges, made from GPT or MBR by the + * kernel -- from dkctl listwedges, a drive's model from atactl(8) + * identify; what is mounted from getmntinfo(3), the swap from swapctl(8) + * -l. A drive that holds /, /usr, /var, /home or swap is the system's and + * is never erased. Erasing is gpt(8) destroy, create and add, dkctl + * makewedges, then newfs(8) -O 2 or newfs_msdos(8) -F 32. + */ + +#define _NETBSD_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "private.h" + +static char dkctl[] = "/sbin/dkctl"; +static char gptp[] = "/sbin/gpt"; + +static unsigned long long +Number(const char *s) +{ + return strtoull(s, NULL, 10); +} + +/* "/dev/rwd0: 68719476736 bytes in 134217728 blocks of 512 bytes" */ +static void +Geometry(const char *name, unsigned long long *bytes, unsigned long *bsize) +{ + char out[512], *p; + char *argv[] = { dkctl, (char *) name, "getgeometry", NULL }; + + *bytes = 0; + *bsize = 512; + if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK || + (p = strchr(out, ':')) == NULL) + return; + *bytes = Number(p + 1); + if ((p = strstr(out, "blocks of ")) != NULL) + *bsize = (unsigned long) Number(p + 10); +} + +static void +Model(const char *name, char *buf, size_t len) +{ + char out[2048], *p, *e; + char *argv[] = { "/sbin/atactl", (char *) name, "identify", NULL }; + + buf[0] = '\0'; + if (strncmp(name, "cd", 2) == 0) { + snprintf(buf, len, "CD/DVD drive"); + return; + } + if (strncmp(name, "wd", 2) != 0 || + mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK || + (p = strstr(out, "Model: ")) == NULL) + return; + p += 7; + if ((e = strstr(p, ", Rev")) == NULL) + e = strchr(p, '\n'); + snprintf(buf, len, "%.*s", (int) (e != NULL ? e - p : (long) strlen(p)), + p); +} + +/* where every device is mounted, and the swap */ +static void +MountOf(const char *name, McdeSysDisk *d) +{ + struct statvfs *mnt; + char dev[64]; + int n, i; + + snprintf(dev, sizeof dev, "/dev/%s", name); + n = getmntinfo(&mnt, MNT_NOWAIT); + for (i = 0; i < n; i++) + if (strcmp(mnt[i].f_mntfromname, dev) == 0) { + snprintf(d->mountpoint, sizeof d->mountpoint, "%s", + mnt[i].f_mntonname); + d->used = (unsigned long long) (mnt[i].f_blocks - + mnt[i].f_bfree) * mnt[i].f_frsize; + d->avail = (unsigned long long) mnt[i].f_bavail * + mnt[i].f_frsize; + if (strcmp(d->mountpoint, "/") == 0 || + strcmp(d->mountpoint, "/usr") == 0 || + strcmp(d->mountpoint, "/var") == 0 || + strcmp(d->mountpoint, "/home") == 0) + d->system = 1; + return; + } +} + +static int +IsSwap(const char *name) +{ + char out[2048], dev[64]; + char *argv[] = { "/sbin/swapctl", "-l", NULL }; + + snprintf(dev, sizeof dev, "/dev/%s ", name); + return mcde_sys_capture(argv, out, sizeof out) == MCDE_SYS_OK && + strstr(out, dev) != NULL; +} + +int +mcde_sys_disks(McdeSysDisk *d, size_t max) +{ + char names[1024], *tok, *last = NULL; + size_t len = sizeof names, n = 0; + + if (sysctlbyname("hw.disknames", names, &len, NULL, 0) != 0) + return mcde_sys_fail(MCDE_SYS_EFAIL, "No list of disks " + "(hw.disknames)."); + for (tok = strtok_r(names, " ", &last); tok != NULL && n < max; + tok = strtok_r(NULL, " ", &last)) { + McdeSysDisk *drive = &d[n]; + unsigned long bsize; + char out[4096], *line, *l2 = NULL; + char *argv[] = { dkctl, tok, "listwedges", NULL }; + size_t first; + + /* the wedges come with their drive; floppies are left out */ + if (strncmp(tok, "dk", 2) == 0 || strncmp(tok, "fd", 2) == 0) + continue; + memset(drive, 0, sizeof *drive); + snprintf(drive->name, sizeof drive->name, "%s", tok); + Geometry(tok, &drive->bytes, &bsize); + Model(tok, drive->model, sizeof drive->model); + drive->removable = strncmp(tok, "sd", 2) == 0 || + strncmp(tok, "cd", 2) == 0; + if (strncmp(tok, "cd", 2) == 0 && drive->bytes > 0) + snprintf(drive->fstype, sizeof drive->fstype, "cd9660"); + first = ++n; + if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK) + continue; + /* "dk0: LABEL, 113246080 blocks at 64, type: ffs" */ + for (line = strtok_r(out, "\n", &l2); line != NULL && n < max; + line = strtok_r(NULL, "\n", &l2)) { + McdeSysDisk *p = &d[n]; + char *colon = strchr(line, ':'), *comma, *type; + + if (strncmp(line, "dk", 2) != 0 || colon == NULL || + (comma = strrchr(line, ',')) == NULL) + continue; + memset(p, 0, sizeof *p); + snprintf(p->name, sizeof p->name, "%.*s", + (int) (colon - line), line); + snprintf(p->parent, sizeof p->parent, "%s", tok); + { + /* the label: up to the comma before the count */ + char *c2 = strstr(colon, " blocks at "); + char *lab_end = c2; + + while (lab_end != NULL && lab_end > colon && + *lab_end != ',') + lab_end--; + if (lab_end != NULL && lab_end > colon + 2) + snprintf(p->label, sizeof p->label, + "%.*s", (int) (lab_end - colon - 2), + colon + 2); + if (c2 != NULL) { + char *b = c2; + + while (b > colon && isdigit((unsigned + char) b[-1])) + b--; + p->bytes = Number(b) * bsize; + } + } + if ((type = strstr(comma, "type: ")) != NULL) + snprintf(p->fstype, sizeof p->fstype, "%s", + type + 6); + /* a GPT label of only a UUID says nothing */ + if (strlen(p->label) == 36 && p->label[8] == '-') + p->label[0] = '\0'; + MountOf(p->name, p); + if (strcmp(p->fstype, "swap") == 0 && IsSwap(p->name)) + p->system = 1; + if (p->system) + drive->system = 1; + n++; + } + (void) first; + MountOf(tok, drive); /* a CD, or a disk without wedges */ + } + return (int) n; +} + +static int +Find(const char *name, McdeSysDisk *out) +{ + static McdeSysDisk all[64]; + int n = mcde_sys_disks(all, 64), i; + + for (i = 0; i < n; i++) + if (strcmp(all[i].name, name) == 0) { + *out = all[i]; + return 1; + } + return 0; +} + +int +mcde_sys_disk_mount(const char *name, char *mountpoint, size_t len) +{ + McdeSysDisk d; + char dev[64], mp[256], prog[64]; + const char *base; + char *argv[5]; + size_t i; + + if (!Find(name, &d)) + return mcde_sys_fail(MCDE_SYS_ENOENT, "There is no disk %s.", + name); + if (d.mountpoint[0] != '\0') { + snprintf(mountpoint, len, "%s", d.mountpoint); + return MCDE_SYS_OK; + } + if (strcmp(d.fstype, "ffs") != 0 && strcmp(d.fstype, "msdos") != 0 && + strcmp(d.fstype, "ext2fs") != 0 && strcmp(d.fstype, "cd9660") != 0 && + strcmp(d.fstype, "ntfs") != 0) + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "A %s partition cannot " + "be mounted.", d.fstype[0] != '\0' ? d.fstype : "blank"); + snprintf(prog, sizeof prog, "/sbin/mount_%s", d.fstype); + snprintf(dev, sizeof dev, strcmp(d.fstype, "cd9660") == 0 ? + "/dev/%sa" : "/dev/%s", name); + /* /media/LABEL, or /media/NAME: letters, digits and - only */ + base = d.label[0] != '\0' ? d.label : name; + snprintf(mp, sizeof mp, "/media/"); + for (i = 0; base[i] != '\0' && strlen(mp) + 2 < sizeof mp; i++) { + size_t l = strlen(mp); + + mp[l] = isalnum((unsigned char) base[i]) ? base[i] : '-'; + mp[l + 1] = '\0'; + } + (void) mkdir("/media", 0755); + (void) mkdir(mp, 0755); + argv[0] = prog; + argv[1] = dev; + argv[2] = mp; + argv[3] = NULL; + if (mcde_sys_run(argv) != MCDE_SYS_OK) + return MCDE_SYS_EFAIL; + snprintf(mountpoint, len, "%s", mp); + return MCDE_SYS_OK; +} + +int +mcde_sys_disk_unmount(const char *name) +{ + McdeSysDisk d; + char *argv[3]; + + if (!Find(name, &d) || d.mountpoint[0] == '\0') + return mcde_sys_fail(MCDE_SYS_ENOENT, "%s is not mounted.", + name); + if (d.system) + return mcde_sys_fail(MCDE_SYS_EPERM, "%s is the system's.", + name); + argv[0] = "/sbin/umount"; + argv[1] = d.mountpoint; + argv[2] = NULL; + return mcde_sys_run(argv); +} + +int +mcde_sys_disk_check(const char *name, char *log, size_t len) +{ + McdeSysDisk d; + char prog[64], dev[64]; + char *argv[4]; + + log[0] = '\0'; + if (!Find(name, &d)) + return mcde_sys_fail(MCDE_SYS_ENOENT, "There is no disk %s.", + name); + if (strcmp(d.fstype, "ffs") != 0 && strcmp(d.fstype, "msdos") != 0 && + strcmp(d.fstype, "ext2fs") != 0) + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "A %s partition cannot " + "be checked.", d.fstype[0] != '\0' ? d.fstype : "blank"); + snprintf(prog, sizeof prog, "/sbin/fsck_%s", d.fstype); + snprintf(dev, sizeof dev, "/dev/r%s", name); + argv[0] = prog; + argv[1] = "-n"; /* look, change nothing (fsck_ffs(8)) */ + argv[2] = dev; + argv[3] = NULL; + return mcde_sys_capture(argv, log, len); +} + +int +mcde_sys_disk_erase(const char *name, const char *fstype, const char *label, + char *part, size_t len) +{ + static McdeSysDisk all[64]; + char out[2048], rdev[64], *p; + const char *gtype; + int n, i; + + if (geteuid() != 0) + return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator " + "can erase a disk."); + if (strcmp(fstype, "ffs") == 0) + gtype = "ffs"; + else if (strcmp(fstype, "msdos") == 0) + gtype = "windows"; + else + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "No %s here.", fstype); + if (label == NULL || label[0] == '\0' || strlen(label) > 32 || + strspn(label, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789 _-") != strlen(label)) + return mcde_sys_fail(MCDE_SYS_EINVAL, "A name has letters, " + "digits, blanks, - and _, at most 32."); + n = mcde_sys_disks(all, 64); + for (i = 0; i < n; i++) + if (strcmp(all[i].name, name) == 0) + break; + if (i == n || all[i].parent[0] != '\0') + return mcde_sys_fail(MCDE_SYS_EINVAL, "%s is no drive.", name); + if (all[i].system) + return mcde_sys_fail(MCDE_SYS_EPERM, "%s holds the system and " + "is not erased.", name); + for (i = 0; i < n; i++) + if ((strcmp(all[i].name, name) == 0 || + strcmp(all[i].parent, name) == 0) && + all[i].mountpoint[0] != '\0') + return mcde_sys_fail(MCDE_SYS_EPERM, "%s is mounted on " + "%s: unmount it first.", all[i].name, + all[i].mountpoint); + { + char *destroy[] = { gptp, "destroy", (char *) name, NULL }; + char *create[] = { gptp, "create", "-f", (char *) name, NULL }; + char *add[] = { gptp, "add", "-t", (char *) gtype, "-l", + (char *) label, (char *) name, NULL }; + char *make[] = { dkctl, (char *) name, "makewedges", NULL }; + char *list[] = { dkctl, (char *) name, "listwedges", NULL }; + + (void) mcde_sys_run(destroy); /* none there: that is fine */ + if (mcde_sys_run(create) != MCDE_SYS_OK || + mcde_sys_run(add) != MCDE_SYS_OK || + mcde_sys_run(make) != MCDE_SYS_OK || + mcde_sys_capture(list, out, sizeof out) != MCDE_SYS_OK) + return MCDE_SYS_EFAIL; + } + if ((p = strstr(out, "\ndk")) == NULL) + return mcde_sys_fail(MCDE_SYS_EFAIL, "The new partition did " + "not appear."); + p++; + snprintf(part, len, "%.*s", (int) strcspn(p, ":"), p); + snprintf(rdev, sizeof rdev, "/dev/r%s", part); + if (strcmp(fstype, "ffs") == 0) { + char *newfs[] = { "/sbin/newfs", "-O", "2", rdev, NULL }; + + return mcde_sys_run(newfs); + } else { + char *newfs[] = { "/sbin/newfs_msdos", "-F", "32", "-L", + (char *) label, rdev, NULL }; + + return mcde_sys_run(newfs); + } +} + +/* ---- the permissions: mtree(8) against the specification sysinst left + * for every installed set, /etc/mtree/set.NAME -- files not in the set left + * alone (-e), -U to set them back (docs/reference/permissions.txt). Only + * owner, group and mode count: -k did not keep mtree from comparing the + * sizes and checksums the specification names (every changed file in /etc + * and every log was reported, measured), so the specification is copied + * with only type, uname, gname, mode and link. */ +#include + +static int +Narrow(const char *from, const char *to) +{ + FILE *in = fopen(from, "r"), *out; + char line[4096]; + + if (in == NULL) + return 0; + if ((out = fopen(to, "w")) == NULL) { + fclose(in); + return 0; + } + while (fgets(line, sizeof line, in) != NULL) { + char *tok, *last = NULL; + int first = 1; + + line[strcspn(line, "\n")] = '\0'; + if (line[0] == '#' || line[0] == '\0') { + fprintf(out, "%s\n", line); + continue; + } + for (tok = strtok_r(line, " \t", &last); tok != NULL; + tok = strtok_r(NULL, " \t", &last)) { + /* the path (or /set, ..) and the keywords that count */ + if (first || strncmp(tok, "type=", 5) == 0 || + strncmp(tok, "uname=", 6) == 0 || + strncmp(tok, "gname=", 6) == 0 || + strncmp(tok, "mode=", 5) == 0 || + strncmp(tok, "link=", 5) == 0) + fprintf(out, "%s%s", first ? "" : " ", tok); + first = 0; + } + fputc('\n', out); + } + fclose(in); + return fclose(out) == 0; +} + +int +mcde_sys_permissions(int repair, char *log, size_t len) +{ + DIR *dir = opendir("/etc/mtree"); + struct dirent *de; + static char out[65536]; + size_t used = 0; + int lines = 0; + + log[0] = '\0'; + if (dir == NULL) + return mcde_sys_fail(MCDE_SYS_ENOENT, "There is no /etc/mtree."); + if (repair && geteuid() != 0) { + closedir(dir); + return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator " + "can repair permissions."); + } + while ((de = readdir(dir)) != NULL) { + char full[300], spec[64], *line, *last = NULL; + char *argv[10]; + int k = 0, fd; + + if (strncmp(de->d_name, "set.", 4) != 0) + continue; + snprintf(full, sizeof full, "/etc/mtree/%s", de->d_name); + snprintf(spec, sizeof spec, "/tmp/mcde-mtree.XXXXXX"); + if ((fd = mkstemp(spec)) < 0) + continue; + close(fd); + if (!Narrow(full, spec)) { + unlink(spec); + continue; + } + argv[k++] = "/usr/sbin/mtree"; + argv[k++] = "-e"; + if (repair) + argv[k++] = "-U"; + argv[k++] = "-f"; + argv[k++] = spec; + argv[k++] = "-p"; + argv[k++] = "/"; + argv[k] = NULL; + /* mtree ends with a non-zero status when something differs: + * its report is what is wanted either way */ + (void) mcde_sys_capture(argv, out, sizeof out); + unlink(spec); + for (line = strtok_r(out, "\n", &last); line != NULL; + line = strtok_r(NULL, "\n", &last)) { + /* the report's own lines that name a file, indented + * lines tell what differed */ + if (line[0] == '\0') + continue; + if (used + strlen(line) + 2 < len) { + used += (size_t) snprintf(log + used, + len - used, "%s\n", line); + } + if (line[0] != '\t' && line[0] != ' ') + lines++; + } + } + closedir(dir); + return lines; +}