iconvert tab to space in README - libgcgi - REST library for Gopher Err bitreich.org 70 hgit clone git://bitreich.org/libgcgi git://hg6vgqziawt5s4dj.onion/libgcgi URL:git://bitreich.org/libgcgi git://hg6vgqziawt5s4dj.onion/libgcgi bitreich.org 70 1Log /scm/libgcgi/log.gph bitreich.org 70 1Files /scm/libgcgi/files.gph bitreich.org 70 1Refs /scm/libgcgi/refs.gph bitreich.org 70 1Tags /scm/libgcgi/tag bitreich.org 70 1README /scm/libgcgi/file/README.gph bitreich.org 70 1LICENSE /scm/libgcgi/file/LICENSE.gph bitreich.org 70 i--- Err bitreich.org 70 1commit 7a737dac78b68760118454337c2caeb6906fd90c /scm/libgcgi/commit/7a737dac78b68760118454337c2caeb6906fd90c.gph bitreich.org 70 1parent b3f118ffbf6488c5e8897be2fa8e225d5149c394 /scm/libgcgi/commit/b3f118ffbf6488c5e8897be2fa8e225d5149c394.gph bitreich.org 70 hAuthor: Josuah Demangeon URL:mailto:me@josuah.net bitreich.org 70 iDate: Wed, 3 Aug 2022 23:59:08 +0200 Err bitreich.org 70 i Err bitreich.org 70 iconvert tab to space in README Err bitreich.org 70 i Err bitreich.org 70 iDiffstat: Err bitreich.org 70 i M Makefile | 2 +- Err bitreich.org 70 i M README | 146 ++++++++++++++++---------------- Err bitreich.org 70 i M index.c | 15 +++++++++++++++ Err bitreich.org 70 i Err bitreich.org 70 i3 files changed, 89 insertions(+), 74 deletions(-) Err bitreich.org 70 i--- Err bitreich.org 70 1diff --git a/Makefile b/Makefile /scm/libgcgi/file/Makefile.gph bitreich.org 70 i@@ -8,7 +8,7 @@ clean: Err bitreich.org 70 i rm -f *.o index.cgi Err bitreich.org 70 i Err bitreich.org 70 i README: libgcgi.3 Err bitreich.org 70 i- mandoc -Tutf8 libgcgi.3 | col -b | sed '1h; $$g' >$@ Err bitreich.org 70 i+ mandoc -Tutf8 libgcgi.3 | col -b | expand | sed '1h; $$g' >$@ Err bitreich.org 70 i Err bitreich.org 70 i index.cgi: index.c libgcgi.c libgcgi.h Err bitreich.org 70 i ${CC} ${LDFLAGS} ${CFLAGS} -o $@ index.c libgcgi.c ${LIBS} Err bitreich.org 70 1diff --git a/README b/README /scm/libgcgi/file/README.gph bitreich.org 70 i@@ -1,4 +1,4 @@ Err bitreich.org 70 i-LIBGCGI(3) Library Functions Manual LIBGCGI(3) Err bitreich.org 70 i+LIBGCGI(3) Library Functions Manual LIBGCGI(3) Err bitreich.org 70 i Err bitreich.org 70 i NAME Err bitreich.org 70 i gcgi_handle_request, gcgi_fatal, gcgi_template, gcgi_set_var, Err bitreich.org 70 i@@ -42,7 +42,7 @@ SYNOPSIS Err bitreich.org 70 i Err bitreich.org 70 i DESCRIPTION Err bitreich.org 70 i This library is a C wrapper around the geomyidae(8) new CGI interface, Err bitreich.org 70 i- which permits REST applications to be written for Gopher. In this mode, Err bitreich.org 70 i+ which permits REST applications to be written for Gopher. In this mode, Err bitreich.org 70 i geomyidae(8) directs all requests to a single binary in charge of Err bitreich.org 70 i handling all paths, rather than trying to serve a file. Err bitreich.org 70 i Err bitreich.org 70 i@@ -51,8 +51,8 @@ DESCRIPTION Err bitreich.org 70 i appropriate handler depending on the query path. Err bitreich.org 70 i Err bitreich.org 70 i struct gcgi_handler { Err bitreich.org 70 i- char const *glob; Err bitreich.org 70 i- void (*fn)(char **matches); Err bitreich.org 70 i+ char const *glob; Err bitreich.org 70 i+ void (*fn)(char **matches); Err bitreich.org 70 i }; Err bitreich.org 70 i Err bitreich.org 70 i The glob is a string against which the path (everything in the query Err bitreich.org 70 i@@ -63,103 +63,103 @@ DESCRIPTION Err bitreich.org 70 i glob. Err bitreich.org 70 i Err bitreich.org 70 i void gcgi_handle_request(struct gcgi_handler h[], int argc, char **argv) Err bitreich.org 70 i- Given an array of handlers h, call the first function pointer Err bitreich.org 70 i- that matches. argc and argv should be set to the program ones to Err bitreich.org 70 i- extract the arguments given by geomyidae(8). The h struct is an Err bitreich.org 70 i- array of struct gcgi_handler: Err bitreich.org 70 i+ Given an array of handlers h, call the first function pointer Err bitreich.org 70 i+ that matches. argc and argv should be set to the program ones to Err bitreich.org 70 i+ extract the arguments given by geomyidae(8). The h struct is an Err bitreich.org 70 i+ array of struct gcgi_handler: Err bitreich.org 70 i Err bitreich.org 70 i Content Generation Err bitreich.org 70 i According to geomyidae(8) behavior, the output format will be: Err bitreich.org 70 i- • a raw gophermap if the binary is “index.cgi”, Err bitreich.org 70 i- • a geomyidae(8) ‘gph’ format if the binary is “index.dcgi”. Err bitreich.org 70 i+ • a raw gophermap if the binary is “index.cgi”, Err bitreich.org 70 i+ • a geomyidae(8) ‘gph’ format if the binary is “index.dcgi”. Err bitreich.org 70 i Err bitreich.org 70 i void gcgi_fatal(char *fmt, ...) Err bitreich.org 70 i- Prints an error message formatted by fmt and exit(3) the program Err bitreich.org 70 i- with status 1. Err bitreich.org 70 i+ Prints an error message formatted by fmt and exit(3) the program Err bitreich.org 70 i+ with status 1. Err bitreich.org 70 i Err bitreich.org 70 i void gcgi_template(char const *path, struct gcgi_var_list *vars) Err bitreich.org 70 i- Format the template at path replacing every occurence of Err bitreich.org 70 i- “{{key}}” by the matching value by searching in vars. Err bitreich.org 70 i+ Format the template at path replacing every occurence of Err bitreich.org 70 i+ “{{key}}” by the matching value by searching in vars. Err bitreich.org 70 i Err bitreich.org 70 i void gcgi_print_gophermap(char type, char *desc, char *path, char *host, Err bitreich.org 70 i- char *port) Err bitreich.org 70 i- Print a gophermap entry line with type, desc, path, host, port to Err bitreich.org 70 i- be set to the chosen value as described in RFC 1436. Both host Err bitreich.org 70 i- and port are NULL, default values will be used. Err bitreich.org 70 i+ char *port) Err bitreich.org 70 i+ Print a gophermap entry line with type, desc, path, host, port to Err bitreich.org 70 i+ be set to the chosen value as described in RFC 1436. Both host Err bitreich.org 70 i+ and port are NULL, default values will be used. Err bitreich.org 70 i Err bitreich.org 70 i Err bitreich.org 70 i void gcgi_print_gph(char type, char *desc, char *path, char *host, char Err bitreich.org 70 i- *port) Err bitreich.org 70 i- Print a gph entry line with type, desc, path, host, port to be Err bitreich.org 70 i- set to the chosen value as described in geomyidae(8) manual page. Err bitreich.org 70 i- If host or port are NULL, default values will be used. Err bitreich.org 70 i+ *port) Err bitreich.org 70 i+ Print a gph entry line with type, desc, path, host, port to be Err bitreich.org 70 i+ set to the chosen value as described in geomyidae(8) manual page. Err bitreich.org 70 i+ If host or port are NULL, default values will be used. Err bitreich.org 70 i Err bitreich.org 70 i Variable List Handling Err bitreich.org 70 i A common data format is used for handling lists of variables: Err bitreich.org 70 i- • For parsing a simple text-based database format and writing it back. Err bitreich.org 70 i- • For storing the parsed query string in gcgi_gopher_query. Err bitreich.org 70 i- • For passing variables to expand in the templates. Err bitreich.org 70 i+ • For parsing a simple text-based database format and writing it back. Err bitreich.org 70 i+ • For storing the parsed query string in gcgi_gopher_query. Err bitreich.org 70 i+ • For passing variables to expand in the templates. Err bitreich.org 70 i Err bitreich.org 70 i void gcgi_set_var(struct gcgi_var_list *vars, char *key, char *val) Err bitreich.org 70 i- Overwrite with val the value of a variable matching key of vars. Err bitreich.org 70 i- The key and val buffers are not duplicated, and must remain valid Err bitreich.org 70 i- at all time they need to be accessible, such as through Err bitreich.org 70 i- gcgi_get_var(). Err bitreich.org 70 i+ Overwrite with val the value of a variable matching key of vars. Err bitreich.org 70 i+ The key and val buffers are not duplicated, and must remain valid Err bitreich.org 70 i+ at all time they need to be accessible, such as through Err bitreich.org 70 i+ gcgi_get_var(). Err bitreich.org 70 i Err bitreich.org 70 i char * gcgi_get_var(struct gcgi_var_list *vars, char *key) Err bitreich.org 70 i- Get the value of the variable of vars matching key or NULL if Err bitreich.org 70 i- none match. Err bitreich.org 70 i+ Get the value of the variable of vars matching key or NULL if Err bitreich.org 70 i+ none match. Err bitreich.org 70 i Err bitreich.org 70 i void gcgi_free_var_list(struct gcgi_var_list *vars) Err bitreich.org 70 i- Free memory used by a list of variable. This only frees the Err bitreich.org 70 i- memory allocated by this library. Err bitreich.org 70 i+ Free memory used by a list of variable. This only frees the Err bitreich.org 70 i+ memory allocated by this library. Err bitreich.org 70 i Err bitreich.org 70 i void gcgi_read_var_list(struct gcgi_var_list *vars, char *path) Err bitreich.org 70 i- Store all variables from path onto variables in vars. The file Err bitreich.org 70 i- format is similar to RFC822 messages or HTTP headers: Err bitreich.org 70 i- • One line per variable, with a key=value format. Err bitreich.org 70 i- • The key is everything at the beginning of the line until the Err bitreich.org 70 i- occurence of “:”. Err bitreich.org 70 i- • The value is everything after “: ”. Err bitreich.org 70 i- • After the list of variables, an empty line declares the body Err bitreich.org 70 i- of the message, which continues until the end and is stored in Err bitreich.org 70 i- a “text” key. Err bitreich.org 70 i+ Store all variables from path onto variables in vars. The file Err bitreich.org 70 i+ format is similar to RFC822 messages or HTTP headers: Err bitreich.org 70 i+ • One line per variable, with a key=value format. Err bitreich.org 70 i+ • The key is everything at the beginning of the line until the Err bitreich.org 70 i+ occurence of “:”. Err bitreich.org 70 i+ • The value is everything after “: ”. Err bitreich.org 70 i+ • After the list of variables, an empty line declares the body Err bitreich.org 70 i+ of the message, which continues until the end and is stored in Err bitreich.org 70 i+ a “text” key. Err bitreich.org 70 i Err bitreich.org 70 i int gcgi_write_var_list(struct gcgi_var_list *vars, char *path) Err bitreich.org 70 i- Encode the variable list vars into a new file at path. A Err bitreich.org 70 i- temporary file will be created in the meantime, and the Err bitreich.org 70 i- replacement will be atomic so that no partial write can occur. Err bitreich.org 70 i- The “text” special key will be turned into the body of the Err bitreich.org 70 i- message after an empty line instead of a variable on its own Err bitreich.org 70 i- line. Err bitreich.org 70 i+ Encode the variable list vars into a new file at path. A Err bitreich.org 70 i+ temporary file will be created in the meantime, and the Err bitreich.org 70 i+ replacement will be atomic so that no partial write can occur. Err bitreich.org 70 i+ The “text” special key will be turned into the body of the Err bitreich.org 70 i+ message after an empty line instead of a variable on its own Err bitreich.org 70 i+ line. Err bitreich.org 70 i Err bitreich.org 70 i Global Variables Err bitreich.org 70 i These variables are filled with the components of the query. They will Err bitreich.org 70 i only be valid after handle_request() is called. Err bitreich.org 70 i Err bitreich.org 70 i char *gcgi_gopher_search Err bitreich.org 70 i- From argv[1], this is the search string, passed after a tab in Err bitreich.org 70 i- the gopher protocol for item type “7”. Err bitreich.org 70 i+ From argv[1], this is the search string, passed after a tab in Err bitreich.org 70 i+ the gopher protocol for item type “7”. Err bitreich.org 70 i Err bitreich.org 70 i char *gcgi_gopher_path Err bitreich.org 70 i- From argv[2], this is the query path. It is the full query Err bitreich.org 70 i- without the search string and with the query string removed. Err bitreich.org 70 i+ From argv[2], this is the query path. It is the full query Err bitreich.org 70 i+ without the search string and with the query string removed. Err bitreich.org 70 i Err bitreich.org 70 i struct gcgi_var_list gcgi_gopher_query Err bitreich.org 70 i- From argv[2], this is the query string stored as a key-value Err bitreich.org 70 i- gcgi_var_list. It is extracted from the part of the query after Err bitreich.org 70 i- the “”?, usually formated as Err bitreich.org 70 i- “?key1=value1&key2=value2&key3=value3” Err bitreich.org 70 i+ From argv[2], this is the query string stored as a key-value Err bitreich.org 70 i+ gcgi_var_list. It is extracted from the part of the query after Err bitreich.org 70 i+ the “”?, usually formated as Err bitreich.org 70 i+ “?key1=value1&key2=value2&key3=value3” Err bitreich.org 70 i Err bitreich.org 70 i char *gcgi_gopher_host Err bitreich.org 70 i- From argv[3], this is the current host name configured in Err bitreich.org 70 i- geomyidae(8). It is what to use as a ‘host’ in links printed Err bitreich.org 70 i- out. Err bitreich.org 70 i+ From argv[3], this is the current host name configured in Err bitreich.org 70 i+ geomyidae(8). It is what to use as a ‘host’ in links printed Err bitreich.org 70 i+ out. Err bitreich.org 70 i Err bitreich.org 70 i char *gcgi_gopher_port Err bitreich.org 70 i- From argv[4], this is the current port number configured in Err bitreich.org 70 i- geomyidae(8). It is what to use as a ‘port’ in links printed Err bitreich.org 70 i- out. Err bitreich.org 70 i+ From argv[4], this is the current port number configured in Err bitreich.org 70 i+ geomyidae(8). It is what to use as a ‘port’ in links printed Err bitreich.org 70 i+ out. Err bitreich.org 70 i Err bitreich.org 70 i EXAMPLES Err bitreich.org 70 i #include "libgcgi.h" Err bitreich.org 70 i@@ -167,25 +167,25 @@ EXAMPLES Err bitreich.org 70 i /* implementation of each handler here */ Err bitreich.org 70 i Err bitreich.org 70 i static struct gcgi_handler handlers[] = { Err bitreich.org 70 i- { "/", page_home }, Err bitreich.org 70 i- { "/song", page_song_list }, Err bitreich.org 70 i- { "/song/*", page_song_item }, Err bitreich.org 70 i- { "*", page_not_found }, Err bitreich.org 70 i- { NULL, NULL }, Err bitreich.org 70 i+ { "/", page_home }, Err bitreich.org 70 i+ { "/song", page_song_list }, Err bitreich.org 70 i+ { "/song/*", page_song_item }, Err bitreich.org 70 i+ { "*", page_not_found }, Err bitreich.org 70 i+ { NULL, NULL }, Err bitreich.org 70 i }; Err bitreich.org 70 i Err bitreich.org 70 i int Err bitreich.org 70 i main(int argc, char **argv) Err bitreich.org 70 i { Err bitreich.org 70 i- /* privilege dropping, chroot and/or syscall restriction here */ Err bitreich.org 70 i+ /* privilege dropping, chroot and/or syscall restriction here */ Err bitreich.org 70 i Err bitreich.org 70 i- gcgi_handle_request(handlers, argv, argc); Err bitreich.org 70 i- return 0; Err bitreich.org 70 i+ gcgi_handle_request(handlers, argv, argc); Err bitreich.org 70 i+ return 0; Err bitreich.org 70 i } Err bitreich.org 70 i Err bitreich.org 70 i ENVIRONMENT VARIABLES Err bitreich.org 70 i libgcgi does not use environment variable, but the application code can Err bitreich.org 70 i- make use of them. The environment variables applied to geomyidae(8) will Err bitreich.org 70 i+ make use of them. The environment variables applied to geomyidae(8) will Err bitreich.org 70 i be inherited and accessible. Err bitreich.org 70 i Err bitreich.org 70 i BUGS Err bitreich.org 70 i@@ -198,7 +198,7 @@ CAVEATS Err bitreich.org 70 i The Gopher protocol is not designed for file upload. A dedicated file Err bitreich.org 70 i upload protocol such as SFTP or FTP may be used instead. Err bitreich.org 70 i Err bitreich.org 70 i- The Gopher protocol is not designed for dynamic scripting. A dedicated Err bitreich.org 70 i+ The Gopher protocol is not designed for dynamic scripting. A dedicated Err bitreich.org 70 i remote interface protocol such as SSH or telnet may be used instead. Err bitreich.org 70 i Err bitreich.org 70 i SEE ALSO Err bitreich.org 70 i@@ -208,4 +208,4 @@ AUTHORS Err bitreich.org 70 i Josuah Demangeon Err bitreich.org 70 i gopher://bitreich.org: The Bitreich Project Err bitreich.org 70 i Err bitreich.org 70 i-LIBGCGI(3) Library Functions Manual LIBGCGI(3) Err bitreich.org 70 i+LIBGCGI(3) Library Functions Manual LIBGCGI(3) Err bitreich.org 70 1diff --git a/index.c b/index.c /scm/libgcgi/file/index.c.gph bitreich.org 70 i@@ -34,6 +34,21 @@ main(int argc, char **argv) Err bitreich.org 70 i gcgi_fatal("unveil failed: %s", strerror(errno)); Err bitreich.org 70 i if (pledge("stdio rpath wpath cpath", NULL) == -1) Err bitreich.org 70 i gcgi_fatal("pledge failed: %s", strerror(errno)); Err bitreich.org 70 i+#elif defined(__linux__) Err bitreich.org 70 i+ scmp_filter_ctx ctx; Err bitreich.org 70 i+ if (chroot(".") == -1) Err bitreich.org 70 i+ gcgi_fatal("chroot failed"); Err bitreich.org 70 i+ if ((ctx = seccomp_init(SCMP_ACT_KILL)) == NULL) Err bitreich.org 70 i+ gcgi_fatal("seccomp_init failed: %s", strerror(errno)); Err bitreich.org 70 i+ if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 0) < 0 Err bitreich.org 70 i+ || seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit), 0) < 0 Err bitreich.org 70 i+ || seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(close), 0) < 0 Err bitreich.org 70 i+ || seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0) < 0 Err bitreich.org 70 i+ || seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1, Err bitreich.org 70 i+ SCMP_A0(SCMP_CMP_EQ, 0)) < 0) Err bitreich.org 70 i+ gcgi_fatal("seccomp_rule_add failed"); Err bitreich.org 70 i+ if (seccomp_load(ctx) < 0) Err bitreich.org 70 i+ gcgi_fatal("seccomp_load failed: %s", strerror(errno)); Err bitreich.org 70 i #else Err bitreich.org 70 i #warning "no syscall restriction enabled" Err bitreich.org 70 i #endif Err bitreich.org 70 .