From ca46b7062a1efbce050218c47f10360bba89bf35 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Sat, 25 Sep 2021 22:18:24 -0400 Subject: Clean up up (ugh) somewhat again --- www/temp.causal.agency/up.c | 88 ++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 46 deletions(-) diff --git a/www/temp.causal.agency/up.c b/www/temp.causal.agency/up.c index 9fca922a..91e37c0f 100644 --- a/www/temp.causal.agency/up.c +++ b/www/temp.causal.agency/up.c @@ -33,8 +33,6 @@ #include #include -static int dir = -1; - static const char *Page = "up"; static const struct kvalid Key = { NULL, "file" }; @@ -49,6 +47,7 @@ static enum kcgi_err fail(struct kreq *req, enum khttp http) { || khttp_printf(req, "%s\n", khttps[http]); } +static int dir = -1; static const char *upload(const char *ext, void *ptr, size_t len) { static char name[256]; snprintf( @@ -137,48 +136,70 @@ static enum kcgi_err handle(struct kreq *req) { } } -static void cgi(void) { +int main(int argc, char *argv[]) { + int error; + const char *path = (argc > 1 ? argv[1] : "."); + dir = open(path, O_DIRECTORY); + if (dir < 0) err(EX_NOINPUT, "%s", path); + +#ifdef __OpenBSD__ + error = unveil(path, "wc"); + if (error) err(EX_OSERR, "unveil"); +#endif + +#ifdef __FreeBSD__ + cap_rights_t rights; + cap_rights_init(&rights, CAP_LOOKUP, CAP_CREATE, CAP_PWRITE); + error = cap_rights_limit(dir, &rights); + if (error) err(EX_OSERR, "cap_rights_limit"); +#endif + + if (!khttp_fcgi_test()) { #ifdef __OpenBSD__ - if (pledge("stdio wpath cpath proc", NULL)) err(EX_OSERR, "pledge"); + error = pledge("stdio wpath cpath proc", NULL); + if (error) err(EX_OSERR, "pledge"); #endif - struct kreq req; - enum kcgi_err error = khttp_parse(&req, &Key, 1, &Page, 1, 0); - if (error) errx(EX_PROTOCOL, "khttp_parse: %s", kcgi_strerror(error)); + struct kreq req; + error = khttp_parse(&req, &Key, 1, &Page, 1, 0); + if (error) errx(EX_PROTOCOL, "khttp_parse: %s", kcgi_strerror(error)); #ifdef __OpenBSD__ - if (pledge("stdio wpath cpath", NULL)) err(EX_OSERR, "pledge"); + error = pledge("stdio wpath cpath", NULL); + if (error) err(EX_OSERR, "pledge"); #endif #ifdef __FreeBSD__ - if (cap_enter()) err(EX_OSERR, "cap_enter"); + error = cap_enter(); + if (error) err(EX_OSERR, "cap_enter"); #endif - error = handle(&req); - if (error) errx(EX_PROTOCOL, "%s", kcgi_strerror(error)); - khttp_free(&req); -} + error = handle(&req); + if (error) errx(EX_PROTOCOL, "%s", kcgi_strerror(error)); + khttp_free(&req); + return EX_OK; + } -static void fcgi(void) { #ifdef __OpenBSD__ - if (pledge("stdio wpath cpath unix sendfd recvfd proc", NULL)) { - err(EX_OSERR, "pledge"); - } + error = pledge("stdio wpath cpath unix sendfd recvfd proc", NULL); + if (error) err(EX_OSERR, "pledge"); #endif struct kfcgi *fcgi; - enum kcgi_err error = khttp_fcgi_init(&fcgi, &Key, 1, &Page, 1, 0); + error = khttp_fcgi_init(&fcgi, &Key, 1, &Page, 1, 0); if (error) errx(EX_CONFIG, "khttp_fcgi_init: %s", kcgi_strerror(error)); #ifdef __OpenBSD__ - if (pledge("stdio wpath cpath recvfd", NULL)) err(EX_OSERR, "pledge"); + error = pledge("stdio wpath cpath recvfd", NULL); + if (error) err(EX_OSERR, "pledge"); #endif #ifdef __FreeBSD__ - if (cap_enter()) err(EX_OSERR, "cap_enter"); + error = cap_enter(); + if (error) err(EX_OSERR, "cap_enter"); #endif for ( struct kreq req; - KCGI_OK == (error = khttp_fcgi_parse(fcgi, &req)); + !(error = khttp_fcgi_parse(fcgi, &req)); khttp_free(&req) ) { error = handle(&req); @@ -189,28 +210,3 @@ static void fcgi(void) { } khttp_fcgi_free(fcgi); } - -int main(int argc, char *argv[]) { - int error; - const char *path = (argc > 1 ? argv[1] : "."); - dir = open(path, O_DIRECTORY); - if (dir < 0) err(EX_NOINPUT, "%s", path); - -#ifdef __OpenBSD__ - error = unveil(path, "wc"); - if (error) err(EX_OSERR, "unveil"); -#endif - -#ifdef __FreeBSD__ - cap_rights_t rights; - cap_rights_init(&rights, CAP_LOOKUP, CAP_CREATE, CAP_PWRITE); - error = cap_rights_limit(dir, &rights); - if (error) err(EX_OSERR, "cap_rights_limit"); -#endif - - if (khttp_fcgi_test()) { - fcgi(); - } else { - cgi(); - } -} -- cgit 1.4.1