💾 Archived View for celehner.com › 2021 › 07 › oldcomp › popcorn.c captured on 2023-09-08 at 16:05:21.

View Raw

More Information

⬅️ Previous capture (2021-11-30)

-=-=-=-=-=-=-

/*
popcorn.c - WIP port of https://github.com/the-maldridge/PopCorn to C
Copyright 2021 Charles E. Lehner

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <sodium.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

const char *server = "localhost";
const char *port = "8080";
const char *uuid_path = "/etc/popcorn/uuid";
const char *interval = "86400";

int usage(int status) {
	fputs("Usage: popcorn [--server <server>] [--port <port>]\n"
	      "               [--uuid-path <uuid_path>] [--interval <interval>]\n",
		status == 0 ? stdout : stderr);
	exit(status);
}

const char *get_uuid() {
	static char id[96];
	size_t sz;
	struct stat st;
	FILE *file;
	int rc = stat(uuid_path, &st);
	if (rc == 0) {
		// TODO: support larger id file
		if (st.st_size != sizeof id) {
			errno = EMSGSIZE;
			return NULL;
		}
		file = fopen(uuid_path, "r");
		if (file == NULL) return NULL;
		sz = fread(id, sizeof id, 1, file);
		fclose(file);
		if (sz < 1) return NULL;
	} else if (errno == ENOENT) {
		int randfd = open("/dev/urandom", O_RDONLY);
		if (randfd < 0) return NULL;
		ssize_t ssz = read(randfd, id, sizeof id);
		close(randfd);
		if (ssz < (ssize_t)sizeof id) return NULL;
		mode_t oldmask = umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
		file = fopen(uuid_path, "w");
		umask(oldmask);
		if (file == NULL) return NULL;
		sz = fwrite(id, sizeof id, 1, file);
		fclose(file);
		if (sz < 1) return NULL;
	} else {
		return NULL;
	}
	return id;
}

int main(int argc, char *argv[]) {
	int i;
	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "--server")) {
			if (++i >= argc) return usage(1);
			else server = argv[i];
		} else if (!strcmp(argv[i], "--port")) {
			if (++i >= argc) return usage(1);
			else port = argv[i];
		} else if (!strcmp(argv[i], "--uuid-path")) {
			if (++i >= argc) return usage(1);
			else uuid_path = argv[i];
		} else if (!strcmp(argv[i], "--interval")) {
			if (++i >= argc) return usage(1);
			else interval = argv[i];
		} else {
			return usage(1);
		}
	}
	const char *machine_id = get_uuid();
	if (machine_id == NULL) err(1, "get_uuid");
	char hash[32];
	int rc = crypto_hash_sha256((unsigned char *)hash, (unsigned char *)machine_id, 96);
	if (rc < 0) errx(1, "failed to hash machine id");
	warnx("id %02x%02x%02x%02x", hash[0], hash[1], hash[2], hash[3]);
}