summaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c162
1 files changed, 159 insertions, 3 deletions
diff --git a/src/main.c b/src/main.c
index 7d4acb2..bf833b9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,11 +1,167 @@
#include <assert.h>
+#include <linux/limits.h>
+#include <stdnoreturn.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
#include <string.h>
#include <stdio.h>
+#include <uuid/uuid.h>
+#include <getopt.h>
+#include "config.h"
#include "transport.h"
+#include "log.h"
+
+static void help(FILE *f) {
+ fprintf(f, "This is where the help would go\n");
+}
+
+enum {
+ OPT_CODE = 1000,
+};
+
+#define CODE_MAX 8
+
+int main(int argc, char **argv) {
+ char code[CODE_MAX+1] = {};
+
+ static const struct option long_options[] = {
+ {"code", required_argument, NULL, OPT_CODE},
+ {"help", no_argument, NULL, 'h'},
+ {},
+ };
+
+ int option;
+
+ while((option = getopt_long(argc, argv, "h", long_options, NULL)) != -1) {
+ switch(option) {
+ case OPT_CODE:
+ if(strlen(optarg) != CODE_MAX) {
+ fprintf(stderr, "Bad Code Format\n");
+ help(stderr);
+ exit(1);
+ }
+ strncpy(code, optarg, CODE_MAX);
+ break;
+ case 'h':
+ help(stdout);
+ break;
+ default:
+ help(stderr);
+ exit(1);
+ }
+ }
+
+ char *config_base = getenv("XDG_DATA_HOME");
+ char *config_mid = "/";
+ if(config_base == NULL) {
+ config_base = getenv("HOME");
+ config_mid = "/.local/share/";
+ }
+
+ char data_dir[PATH_MAX];
+ snprintf(data_dir, PATH_MAX-1, "%s%slibrm", config_base, config_mid);
+ TRACE("Config Dir %s", data_dir);
+
+ mkdir(data_dir, 0700);
+ int data_fd = open(data_dir, O_DIRECTORY);
+ if(data_fd < 0) {
+ FATAL("data dir could not be opened");
+ }
+
+ bool new = false;
+ int runtime_fd = openat(data_fd, "runtime", O_RDWR | O_CLOEXEC, 0600);
+ if(runtime_fd < 0) {
+ runtime_fd = openat(data_fd, "runtime", O_RDWR | O_CREAT | O_EXCL, 0600);
+ if(runtime_fd < 0) {
+ FATAL("open failed");
+ }
+ new = true;
+ }
+
+ FILE *runtime_file = fdopen(runtime_fd, "r+");
+ if(new) {
+ TRACE("No config file found, create one");
+ if(rm_config_bootstrap(runtime_file) != RC_SUCCESS) {
+ FATAL("config bootstrap failed");
+ }
+ }
+
+ struct RMDevice device = {};
+
+ fseek(runtime_file, 0, SEEK_SET);
+ rm_device_load(runtime_file, &device);
+
+ if(strlen(device.id) < 36) {
+ TRACE("Generate device id");
+ uuid_t uuid;
+ uuid_generate_random(uuid);
+ uuid_unparse_upper(uuid, device.id);
+
+ {
+ fseek(runtime_file, 0, SEEK_SET);
+ int newfd = openat(data_fd, "runtime.new", O_RDWR | O_CREAT | O_EXCL, 0600);
+ if(newfd < 0) {
+ FATAL("openat failed");
+ }
+
+ FILE *fnew = fdopen(newfd, "r+");
+ if(fnew == NULL) {
+ FATAL("fdopen failed");
+ }
+
+ if(fflush(fnew) != 0) {
+ FATAL("fflush fail");
+ }
+
+ if(rm_device_save(runtime_file, &device, fnew) != RC_SUCCESS) {
+ FATAL("saving config failed");
+ };
+ if(renameat(data_fd, "runtime.new", data_fd, "runtime") != 0) {
+ FATAL("renameat failed");
+ }
+ if(fsync(data_fd) != 0) {
+ FATAL("fsync failed");
+ }
+ }
+ }
+
+ if(device.deviceToken == NULL && code[0] == 0) {
+ FATAL("--code is required when enrolling the device the first time");
+ }
+
+ rm_device_open(&device, code);
+
+ {
+ fseek(runtime_file, 0, SEEK_SET);
+ int newfd = openat(data_fd, "runtime.new", O_RDWR | O_CREAT | O_EXCL, 0600);
+ if(newfd < 0) {
+ FATAL("openat failed");
+ }
+
+ FILE *fnew = fdopen(newfd, "r+");
+ if(fnew == NULL) {
+ FATAL("fdopen failed");
+ }
+
+ if(fflush(fnew) != 0) {
+ FATAL("fflush fail");
+ }
+
+ if(rm_device_save(runtime_file, &device, fnew) != RC_SUCCESS) {
+ FATAL("saving config failed");
+ };
+ if(renameat(data_fd, "runtime.new", data_fd, "runtime") != 0) {
+ FATAL("renameat failed");
+ }
+ if(fsync(data_fd) != 0) {
+ FATAL("fsync failed");
+ }
+ }
-int main() {
struct SizedBlock body = {0};
- transport_request(GET_ROOT_URL, NULL, NULL, &body);
- printf("Body fetched: %ld bytes", strlen(body.memory));
+ transport_request(GET_ROOT_URL, HTTP_GET, NULL, NULL, &body);
+ TRACE("Body fetched: %ld bytes", strlen(body.memory));
}