Skip to content
pi-c Artifact [b99868d3a6]
Login

Artifact [b99868d3a6]

Artifact b99868d3a6c27d65c9a128ebae72d056a0f886b76b5bb20506e3d6f7668c0a9a:


/* The host platform (see platform.h). */
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include <sys/random.h>

#include "platform.h"

static int64_t host_wall_ns(void) {
    struct timespec ts;
    if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
        return -1;
    }
    return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
}

static uint64_t host_mono_ns(void) {
    struct timespec ts;
    (void)clock_gettime(CLOCK_MONOTONIC, &ts);
    return (uint64_t)ts.tv_sec * 1000000000u + (uint64_t)ts.tv_nsec;
}

static int host_entropy(void *buf, size_t n) { return getentropy(buf, n); }

static int host_lock(const char *path, int shared) {
    int fd = open(path, O_RDWR | O_CREAT | O_CLOEXEC, 0600);
    if (fd < 0) {
        return -1;
    }
    if (flock(fd, (shared ? LOCK_SH : LOCK_EX) | LOCK_NB) != 0) {
        int e = errno;
        close(fd);
        errno = e;
        return -2;
    }
    return fd;
}

static void host_unlock(int handle) { close(handle); }

/* ---- host only: the terminal ---------------------------------------- */

/* While echo is off: the terminal's settings, so a signal can put them back.
 * SIGINT and SIGTERM restore and re-raise (the default then ends pic);
 * SIGTSTP restores and stops, and on the way back (or on a SIGCONT after an
 * outside stop) echo goes off again. */
static volatile sig_atomic_t tty_fd = -1;
static struct termios tty_old, tty_quiet;
static const int tty_signals[4] = {SIGINT, SIGTERM, SIGTSTP, SIGCONT};
static struct sigaction tty_saved[4];

static void tty_on_signal(int sig) {
    int e = errno;
    if (tty_fd >= 0) {
        if (sig == SIGCONT) {
            (void)tcsetattr(tty_fd, TCSANOW, &tty_quiet);
        } else {
            sigset_t one;
            struct sigaction dfl;
            (void)tcsetattr(tty_fd, TCSANOW, &tty_old);
            memset(&dfl, 0, sizeof dfl);
            dfl.sa_handler = SIG_DFL;
            sigemptyset(&dfl.sa_mask);
            (void)sigaction(sig, &dfl, NULL);
            sigemptyset(&one);
            sigaddset(&one, sig);
            (void)raise(sig);
            (void)sigprocmask(SIG_UNBLOCK, &one, NULL); /* delivered here: dies, or stops until SIGCONT */
            if (sig == SIGTSTP) { /* continued: back to the read, echo off again */
                struct sigaction mine;
                memset(&mine, 0, sizeof mine);
                mine.sa_handler = tty_on_signal;
                sigemptyset(&mine.sa_mask);
                (void)sigaction(SIGTSTP, &mine, NULL);
                (void)tcsetattr(tty_fd, TCSANOW, &tty_quiet);
            }
        }
    }
    errno = e;
}

long pic_tty_secret(const char *prompt, char *buf, size_t cap, char *err, size_t errcap) {
    int fd = open("/dev/tty", O_RDWR | O_NOCTTY | O_CLOEXEC);
    struct termios old, quiet;
    struct sigaction mine;
    size_t n = 0;
    long rc = -1;
    if (fd < 0) {
        (void)snprintf(err, errcap, "no terminal to ask on");
        return -1;
    }
    if (tcgetattr(fd, &old) != 0) {
        (void)snprintf(err, errcap, "no terminal to ask on");
        close(fd);
        return -1;
    }
    quiet = old;
    quiet.c_lflag &= ~(tcflag_t)(ECHO | ECHOE | ECHOK | ECHONL);
    quiet.c_lflag |= ICANON;
    tty_old = old;
    tty_quiet = quiet;
    tty_fd = fd;
    memset(&mine, 0, sizeof mine);
    mine.sa_handler = tty_on_signal; /* no SA_RESTART: the read sees EINTR and goes on */
    sigemptyset(&mine.sa_mask);
    for (int i = 0; i < 4; i++) {
        (void)sigaction(tty_signals[i], &mine, &tty_saved[i]);
    }
    /* Echo off before the prompt shows, so nothing typed after it can echo;
     * input typed before it is dropped. */
    if (tcsetattr(fd, TCSAFLUSH, &quiet) != 0) {
        (void)snprintf(err, errcap, "cannot turn the terminal's echo off");
        tty_fd = -1;
        for (int i = 0; i < 4; i++) {
            (void)sigaction(tty_signals[i], &tty_saved[i], NULL);
        }
        close(fd);
        return -1;
    }
    (void)write(fd, prompt, strlen(prompt));
    for (;;) {
        char c;
        ssize_t got = read(fd, &c, 1);
        if (got < 0 && errno == EINTR) {
            continue;
        }
        if (got <= 0) {
            (void)snprintf(err, errcap, "no answer was typed");
            break;
        }
        if (c == '\n' || c == '\r') {
            buf[n] = '\0';
            rc = (long)n;
            break;
        }
        if (n + 1 >= cap) {
            (void)snprintf(err, errcap, "the answer is longer than %zu bytes", cap - 1);
            break;
        }
        buf[n++] = c;
    }
    (void)tcsetattr(fd, TCSAFLUSH, &old);
    tty_fd = -1;
    for (int i = 0; i < 4; i++) {
        (void)sigaction(tty_signals[i], &tty_saved[i], NULL);
    }
    (void)write(fd, "\n", 1);
    close(fd);
    if (rc < 0) {
        volatile char *v = buf;
        for (size_t i = 0; i < cap; i++) {
            v[i] = 0;
        }
    }
    return rc;
}

/* The passphrase, asked on the controlling terminal (twice for a new
 * store). A dev build (PIC_FAULTS) also takes it from PIC_TEST_PASSPHRASE so
 * the suite can run without a terminal; a release build never reads it. */
static int host_secret_key(int fresh, unsigned char *buf, size_t cap, size_t *len, int *stretch, char *err,
                           size_t errcap) {
    char again[512];
    long n;
    *stretch = 1;
#ifdef PIC_FAULTS
    {
        const char *t = getenv("PIC_TEST_PASSPHRASE");
        if (t) {
            size_t k = strlen(t);
            if (k == 0 || k > cap) {
                (void)snprintf(err, errcap, "PIC_TEST_PASSPHRASE is empty or too long");
                return -1;
            }
            memcpy(buf, t, k);
            *len = k;
            return 0;
        }
    }
#endif
    n = pic_tty_secret(fresh ? "New passphrase for pic's credential store: " : "Passphrase for pic's credential store: ",
                       (char *)buf, cap, err, errcap);
    if (n < 0) {
        return -1;
    }
    if (n == 0) {
        (void)snprintf(err, errcap, "the passphrase is empty");
        return -1;
    }
    if (fresh) {
        long m = pic_tty_secret("Same passphrase again: ", again, sizeof again, err, errcap);
        int same = m == n && memcmp(again, buf, (size_t)n) == 0;
        volatile char *v = again;
        for (size_t i = 0; i < sizeof again; i++) {
            v[i] = 0;
        }
        if (m < 0) {
            return -1;
        }
        if (!same) {
            (void)snprintf(err, errcap, "the two passphrases differ");
            return -1;
        }
    }
    *len = (size_t)n;
    return 0;
}

static const pic_platform host = {
    host_wall_ns, host_mono_ns, host_entropy, host_lock, host_unlock, pic_input_read_host, pic_input_list_host, NULL,
    host_secret_key,
};

static const pic_platform *current = &host;

const pic_platform *pic_platform_host(void) { return &host; }
const pic_platform *pic_platform_get(void) { return current; }
void pic_platform_set(const pic_platform *p) { current = p ? p : &host; }