improve the example and prepare libseccomp in the example - libgcgi - REST library for Gopher HTML git clone git://bitreich.org/libgcgi git://hg6vgqziawt5s4dj.onion/libgcgi DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit 39674c36f9dbf2a2f4be5fa2f8df9894c1ed75be DIR parent fd61b070718453c8d57daa40f620d89e75cb2d78 HTML Author: Josuah Demangeon <me@josuah.net> Date: Wed, 3 Aug 2022 18:39:18 +0200 improve the example and prepare libseccomp in the example Diffstat: M Makefile | 7 ++++--- M db/vars | 2 +- M gph/page_not_found.gph | 3 ++- M index.c | 37 ++++++++++++++----------------- 4 files changed, 24 insertions(+), 25 deletions(-) --- DIR diff --git a/Makefile b/Makefile @@ -1,5 +1,6 @@ -LDFLAGS = -static -CFLAGS = -D_POSIX_C_SOURCE=200809L -g -pedantic -std=c99 -Wall -Wextra +LDFLAGS = +#LIBS = -lseccomp #<- uncomment on Linux +CFLAGS = -D_POSIX_C_SOURCE=200809L -D_GNU_SOURCE -g -pedantic -std=c99 -Wall -Wextra all: index.cgi @@ -10,4 +11,4 @@ README: libgcgi.3 mandoc -Tutf8 libgcgi.3 | col -b | sed '1h; $$g' >$@ index.cgi: index.c libgcgi.c libgcgi.h - ${CC} ${LDFLAGS} ${CFLAGS} -o $@ index.c libgcgi.c + ${CC} ${LDFLAGS} ${CFLAGS} -o $@ index.c libgcgi.c ${LIBS} DIR diff --git a/db/vars b/db/vars @@ -1 +1 @@ -name: world +mail: gopher@example.com DIR diff --git a/gph/page_not_found.gph b/gph/page_not_found.gph @@ -1 +1,2 @@ -Hello {{name}}! +Sorry, I could not find {{page}}. +If it looks like an error, you can let us know that "{{page}}" is not working by sending us an email at {{mail}}. DIR diff --git a/index.c b/index.c @@ -1,31 +1,27 @@ +#include <errno.h> #include <stddef.h> -#include <unistd.h> #include <stdio.h> +#include <string.h> +#include <unistd.h> -#include "libgcgi.h" - -#ifndef __OpenBSD__ -#define pledge(p1,p2) 0 -#define unveil(p1,p2) 0 +#ifdef __linux__ +#include <seccomp.h> #endif +#include "libgcgi.h" + static void -error_page_not_found(char **matches) +page_not_found(char **matches) { struct gcgi_var_list vars = {0}; - char *var; gcgi_read_var_list(&vars, "db/vars"); - - printf("sorry, I could not find %s\n", matches[0]); - if ((var = gcgi_get_var(&gcgi_gopher_query, "var")) != NULL) - printf("I got the $var though! -> '%s'\n", var); - - gcgi_template("gph/error_page_not_found.gph", &vars); + gcgi_set_var(&vars, "page", matches[0]); + gcgi_template("gph/page_not_found.gph", &vars); } static struct gcgi_handler handlers[] = { - { "*", error_page_not_found }, + { "*", page_not_found }, { NULL, NULL }, }; @@ -33,13 +29,14 @@ int main(int argc, char **argv) { - /* restrict allowed paths */ +#if defined(__OpenBSD__) if (unveil("gph", "r") == -1 || unveil("db", "rwc") == -1) - gcgi_fatal("unveil failed"); - - /* restrict allowed system calls */ + gcgi_fatal("unveil failed: %s", strerror(errno)); if (pledge("stdio rpath wpath cpath", NULL) == -1) - gcgi_fatal("pledge failed"); + gcgi_fatal("pledge failed: %s", strerror(errno)); +#else +#warning "no syscall restriction enabled" +#endif /* handle the request with the handlers */ gcgi_handle_request(handlers, argv, argc);