From 0ec0db6ccfb60925dbd72f371b195193f89f03fc Mon Sep 17 00:00:00 2001 From: swirl Date: Sat, 25 Sep 2021 18:14:16 -0400 Subject: [PATCH] add compile option to disable custom links --- Makefile | 5 +++-- README.md | 2 +- main.c | 9 +++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 363e26e..1b06fb4 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,9 @@ systemd_dir=${DESTDIR}${confdir}/systemd/system nginx_dir=${DESTDIR}${confdir}/nginx bindir=${DESTDIR}${prefix}/bin -CC := gcc -CFLAGS := -O2 +DISABLE_CUSTOM_LINKS ?= 0 +CC ?= gcc +CFLAGS := -O2 -DDISABLE_CUSTOM_LINKS=${DISABLE_CUSTOM_LINKS} ${CFLAGS} BIN := pacebin diff --git a/README.md b/README.md index 0a222dc..5b3118c 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ To build this project, you'll need a libc implementation (only tested with glibc git clone https://git.swurl.xyz/swirl/pacebin && cd pacebin ``` -2. Compile: +2. Now, you need to compile. When compiling, you can optionally choose to disable the ability to use custom links for pastes. This can easily be done by setting `DISABLE_CUSTOM_LINKS` to 1; i.e. `make DISABLE_CUSTOM_LINKS=1`. Defaults to 0, enabling them. ```bash make ``` diff --git a/main.c b/main.c index 0f16285..5694d68 100644 --- a/main.c +++ b/main.c @@ -104,6 +104,10 @@ void trim(char *str) { memmove(str, _str, len + 1); } +#if DISABLE_CUSTOM_LINKS == 1 +void handle_post(struct mg_connection *nc, char *content, char *host) { + char *short_link = gen_random_link(); +#else void handle_post(struct mg_connection *nc, char *content, char *host, char *link) { char *short_link; if (strlen(link) == 0) { @@ -117,6 +121,7 @@ void handle_post(struct mg_connection *nc, char *content, char *host, char *link if (paste_exists(short_link)) { return mg_http_reply(nc, 500, "", "a paste named %s already exists", short_link); } +#endif FILE *url = get_paste_file(short_link, "w+"); fputs(content, url); @@ -165,7 +170,11 @@ static void ev_handler(struct mg_connection *nc, int ev, void *p, void *f) { char *body = strdup(hm->body.ptr); if (strncmp(hm->method.ptr, "POST", hm->method.len) == 0) { +#if DISABLE_CUSTOM_LINKS == 1 + handle_post(nc, body, host); // FIXME: return 400 on bad Content-Type +#else handle_post(nc, body, host, uri); // FIXME: return 400 on bad Content-Type +#endif } else if (strncmp(hm->method.ptr, "DELETE", hm->method.len) == 0) { handle_delete(nc, uri, body); } else if (strncmp(hm->method.ptr, "GET", hm->method.len) == 0) {