From fdbbd73010b02f42479c8b1c515113993950f0b2 Mon Sep 17 00:00:00 2001 From: Tom MTT Date: Sun, 11 Dec 2022 00:41:39 +0100 Subject: [PATCH 1/7] fix(feuille.c): check validity of sockets in accept loop --- feuille.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/feuille.c b/feuille.c index 55a1d7f..9847718 100644 --- a/feuille.c +++ b/feuille.c @@ -93,6 +93,12 @@ void accept_loop(int server) /* accept loop */ int connection; while ((connection = accept_connection(server))) { + /* check if the socket is invalid */ + if (connection == -1) { + error("error while accepting incoming connection: %s", strerror(errno)); + continue; + } + verbose(1, "--- new incoming connection. connection ID: %d:%d ---", pid, time(0)); unsigned long paste_size = 0; From 3c356481e1c91300e409876a545ca2a971a40806 Mon Sep 17 00:00:00 2001 From: Tom MTT Date: Sun, 11 Dec 2022 00:42:30 +0100 Subject: [PATCH 2/7] fix: remove useless newlines --- feuille.c | 1 - server.c | 1 - 2 files changed, 2 deletions(-) diff --git a/feuille.c b/feuille.c index 9847718..a81e82e 100644 --- a/feuille.c +++ b/feuille.c @@ -348,7 +348,6 @@ int main(int argc, char *argv[]) gid = user->pw_gid; } else { - puts(""); syslog(LOG_WARNING, "running as non-root user."); syslog(LOG_WARNING, "`chroot' and user switching have been disabled."); puts(""); diff --git a/server.c b/server.c index c26384c..3fd7d23 100644 --- a/server.c +++ b/server.c @@ -110,7 +110,6 @@ int initialize_server() return -1; #else if (ipv6_only == 0) { - puts(""); syslog(LOG_WARNING, "dual-stack mode is disabled on OpenBSD."); syslog(LOG_WARNING, "feuille will only listen on the `::' IPv6 address."); puts(""); From 63319d752546149eb53c4b327be5b9682b3c8f06 Mon Sep 17 00:00:00 2001 From: Tom MTT Date: Sun, 11 Dec 2022 00:44:50 +0100 Subject: [PATCH 3/7] fix(server.c): better dual-stack mode handling --- server.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/server.c b/server.c index 3fd7d23..2c1a196 100644 --- a/server.c +++ b/server.c @@ -103,18 +103,14 @@ int initialize_server() if (setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0) return -1; -#ifndef __OpenBSD__ /* Enable dual-stack mode on supported platforms */ verbose(3, " IPV6_V6ONLY..."); - if (setsockopt(server, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6_only, sizeof(ipv6_only)) < 0) - return -1; -#else - if (ipv6_only == 0) { - syslog(LOG_WARNING, "dual-stack mode is disabled on OpenBSD."); + + if (setsockopt(server, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6_only, sizeof(ipv6_only)) < 0) { + syslog(LOG_WARNING, "dual-stack mode is disabled on your platform."); syslog(LOG_WARNING, "feuille will only listen on the `::' IPv6 address."); puts(""); } -#endif /* bind address and port */ verbose(3, "binding address on the socket..."); From 16f516d6ceb12f557724a675098b0a7192226d6e Mon Sep 17 00:00:00 2001 From: Tom MTT Date: Sun, 11 Dec 2022 01:59:08 +0100 Subject: [PATCH 4/7] feat: cosmopolitan libc support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added support for cosmopolitan libc. Simply do `make COSMO=yes` and you'll get feuille.com, a working αcτµαlly pδrταblε εxεcµταblε pastebin server. + version bump. --- .gitignore | 4 ++++ Makefile | 57 +++++++++++++++++++++++++++++++++++++----------------- arg.h | 3 +++ bin.c | 2 ++ config.mk | 29 ++++++++++++++++++++------- feuille.1 | 2 +- feuille.c | 5 +++++ server.c | 9 ++++++++- util.c | 2 ++ 9 files changed, 86 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 654d37f..ad9f5f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ *.o +cosmopolitan + feuille +feuille.com + cgi/feuille.cgi diff --git a/Makefile b/Makefile index b3b57ed..63f1195 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,13 @@ include config.mk -TARGET = feuille +TARGET = feuille.com +TARGET$(COSMO) = feuille + SRC = feuille.c util.c server.c bin.c OBJ = $(SRC:%.c=%.o) -all: $(TARGET) $(TARGET).1 cgi +all: $(TARGET) feuille.1 cgi run: $(TARGET) ./$(TARGET) @@ -18,10 +20,10 @@ clean: @rm -f $(OBJ) distclean: - @printf "%-8s $(TARGET) $(OBJ)\n" "rm" - @rm -f $(TARGET) $(OBJ) + @printf "%-8s feuille feuille.com $(OBJ)\n" "rm" + @rm -f feuille feuille.com $(OBJ) -install: $(TARGET) $(TARGET).1 +install: $(TARGET) feuille.1 @echo "installing executable file to $(PREFIX)/bin" @mkdir -p "$(PREFIX)/bin" @cp -f $(TARGET) "$(PREFIX)/bin" @@ -29,34 +31,53 @@ install: $(TARGET) $(TARGET).1 @echo "installing manpage to $(MAN)/man1" @mkdir -p $(MAN)/man1 - @cp -f $(TARGET).1 $(MAN)/man1 - @chmod 644 $(MAN)/man1/$(TARGET).1 + @cp -f feuille.1 $(MAN)/man1 + @chmod 644 $(MAN)/man1/feuille.1 -uninstall: $(PREFIX)/bin/$(TARGET) $(MAN)/man1/$(TARGET).1 +uninstall: $(PREFIX)/bin/$(TARGET) $(MAN)/man1/feuille.1 @echo "removing executable file from $(PREFIX)/bin" @rm -f "$(PREFIX)/bin/$(TARGET)" @echo "removing manpage from $(MAN)/man1" - @rm -f $(MAN)/man1/$(TARGET).1 + @rm -f $(MAN)/man1/feuille.1 -$(TARGET): $(OBJ) - @printf "%-8s $(OBJ) -o $@\n" "$(CC)" - @$(CC) $(LDFLAGS) $(OBJ) -o $@ +feuille.1: feuille.1.md config.mk + @printf "%-8s feuille.1.md -o feuille.1\n" "pandoc" + @sed "s/{VERSION}/$(VERSION)/g" feuille.1.md | pandoc -s -t man -o feuille.1 -$(TARGET).1: $(TARGET).1.md config.mk - @printf "%-8s $(TARGET).1.md -o $@\n" "pandoc" - @sed "s/{VERSION}/$(VERSION)/g" $(TARGET).1.md | pandoc -s -t man -o $@ +feuille: $(OBJ) + @printf "%-8s $(OBJ) -o feuille\n" "$(CC)" + @$(CC) $(OBJ) -o feuille $(LDFLAGS) +# cosmopolitan libc +feuille.com: cosmopolitan feuille + @printf "%-8s feuille -o feuille.com\n" "objcopy" + @objcopy -S -O binary feuille feuille.com + +cosmopolitan: + @if [ ! -d cosmopolitan ]; then \ + printf "%-8s https://justine.lol/cosmopolitan/cosmopolitan-amalgamation-2.2.zip\n" "curl" ;\ + curl -sO "https://justine.lol/cosmopolitan/cosmopolitan-amalgamation-2.2.zip" ;\ + \ + printf "%-8s cosmopolitan-amalgamation-2.2.zip\n" "unzip" ;\ + unzip -qf cosmopolitan-amalgamation-2.2.zip -d cosmopolitan ;\ + \ + rm -rf cosmopolitan-amalgamation-* ;\ + fi + +# CGI script ADDR = 127.0.0.1 PORT = 9999 cgi: cgi/feuille.cgi cgi/feuille.cgi: cgi/feuille.cgi.c - @printf "%-8s cgi/feuille.cgi.c -o $@\n" "$(CC)" - @$(CC) $(CFLAGS) $(LDFLAGS) -static -DADDR=\"$(ADDR)\" -DPORT=$(PORT) cgi/feuille.cgi.c -o $@ + @printf "%-8s cgi/feuille.cgi.c -o cgi/feuille.cgi\n" "$(CC)" + @$(CC) cgi/feuille.cgi.c -o cgi/feuille.cgi -std=c99 -O3 -static -Wall -Wextra \ + -DADDR=\"$(ADDR)\" -DPORT=$(PORT) \ + $(INCS) $(LIBS) .SUFFIXES: .c .o .c.o: @printf "%-8s $<\n" "$(CC)" - @$(CC) $(CFLAGS) -c $< + @$(CC) -c $< $(CFLAGS) diff --git a/arg.h b/arg.h index 2130a20..9c4d1bb 100644 --- a/arg.h +++ b/arg.h @@ -34,8 +34,11 @@ #ifndef _ARG_H_ #define _ARG_H_ +#ifndef COSMOPOLITAN #include #include +#endif + extern char *argv0; /* use main(int argc, char *argv[]) */ diff --git a/bin.c b/bin.c index a927f78..ee97280 100644 --- a/bin.c +++ b/bin.c @@ -13,11 +13,13 @@ #include "bin.h" +#ifndef COSMOPOLITAN #include /* for errno */ #include /* for NULL, fclose, fopen, fputs, snprintf, FILE */ #include /* for calloc, free, malloc, rand, realloc */ #include /* for strlen */ #include /* for access, F_OK */ +#endif #include "feuille.h" /* for Settings, settings */ diff --git a/config.mk b/config.mk index 62d505f..80b04ea 100644 --- a/config.mk +++ b/config.mk @@ -1,5 +1,5 @@ # feuille version -VERSION = 1.19.5 +VERSION = 2.0.0 # paths (customize them to fit your system) PREFIX = /usr/local @@ -17,13 +17,28 @@ LIBS = -L/usr/lib -lc # compiler CC = cc -# debug build -CFLAGS = -g -std=c99 -Wall -Wextra -Wpedantic -Wno-sign-compare -DVERSION=\"$(VERSION)\" -DDEBUG $(INCS) -LDFLAGS = -g $(LIBS) +# cosmopolitan libc flags +CCFLAGS = -std=c99 -nostdinc -fno-pie -fno-omit-frame-pointer \ + -mno-tls-direct-seg-refs -mno-red-zone \ + -DVERSION=\"$(VERSION)\" -DCOSMOPOLITAN \ + -I. -include cosmopolitan/cosmopolitan.h -# release build -CFLAGS$(DEBUG) = -std=c99 -Wall -Wextra -Wno-sign-compare -DVERSION=\"$(VERSION)\" -O3 $(INCS) -LDFLAGS$(DEBUG) = -s $(LIBS) +CLDFLAGS = -std=c99 -static -nostdlib -fno-pie -fno-omit-frame-pointer \ + -mno-tls-direct-seg-refs -mno-red-zone \ + -fuse-ld=bfd -Wl,-T,cosmopolitan/ape.lds -Wl,--gc-sections \ + cosmopolitan/crt.o cosmopolitan/ape-no-modify-self.o cosmopolitan/cosmopolitan.a + +# standard libc flags +CCFLAGS$(COSMO) = -std=c99 -DVERSION=\"$(VERSION)\" $(INCS) +CLDFLAGS$(COSMO) = $(LIBS) + +# debug flags +CFLAGS = -g -Wall -Wextra -Wno-sign-compare -DDEBUG $(CCFLAGS) +LDFLAGS = -g $(CLDFLAGS) + +# release flags +CFLAGS$(DEBUG) = -O3 -Wall -Wextra -Wno-sign-compare $(CCFLAGS) +LDFLAGS$(DEBUG) = -s $(CLDFLAGS) # static build (uncomment) #LD_FLAGS += -static diff --git a/feuille.1 b/feuille.1 index ed874a1..0d7bb17 100644 --- a/feuille.1 +++ b/feuille.1 @@ -14,7 +14,7 @@ . ftr VB CB . ftr VBI CBI .\} -.TH "feuille" "1" "November 2022" "feuille 1.19.5" "" +.TH "feuille" "1" "November 2022" "feuille 2.0.0" "" .hy .SH NAME .PP diff --git a/feuille.c b/feuille.c index a81e82e..843bde9 100644 --- a/feuille.c +++ b/feuille.c @@ -15,6 +15,7 @@ #include "feuille.h" +#ifndef COSMOPOLITAN #include /* for errno, ERANGE, EAGAIN, EFBIG, ENOENT */ #include /* for initgroups */ #include /* for USHRT_MAX, ULONG_MAX, CHAR_MAX, PATH_MAX, UCHA... */ @@ -29,6 +30,7 @@ #include /* for syslog, openlog, LOG_WARNING, LOG_NDELAY, LOG_... */ #include /* for time */ #include /* for getuid, access, chdir, chown, chroot, close */ +#endif #include "arg.h" /* for EARGF, ARGBEGIN, ARGEND */ #include "bin.h" /* for create_url, generate_id, write_paste */ @@ -387,8 +389,11 @@ int main(int argc, char *argv[]) if (setgid(gid) != 0 || getgid() != gid) die(1, "could not switch to group for user `%s'.\n", settings.user); + #ifndef COSMOPOLITAN + /* initgroups doesn't work on cosmopolitan libc yet */ if (initgroups(settings.user, gid) != 0) die(1, "could not initialize other groups for user `%s'.\n", settings.user); + #endif /* switching user */ if (setuid(uid) != 0 || getuid() != uid) diff --git a/server.c b/server.c index 2c1a196..a01f535 100644 --- a/server.c +++ b/server.c @@ -13,6 +13,7 @@ #include "server.h" +#ifndef COSMOPOLITAN #include /* for inet_pton */ #include /* for errno, EAGAIN, EFBIG, ENOENT */ #include /* for htons, sockaddr_in, sockaddr_in6, IPPROTO_IPV6 */ @@ -24,6 +25,7 @@ #include /* for setsockopt, bind, socket, SOL_SOCKET, AF_INET */ #include /* for timeval */ #include /* for close */ +#endif #include "feuille.h" /* for Settings, settings */ #include "util.h" /* for verbose */ @@ -136,9 +138,14 @@ int initialize_server() */ int accept_connection(int socket) { + /* only needed for cosmopolitan libc */ + /* we don't do anything with this struct yet */ + struct sockaddr_in address; + int addrlen = sizeof(address); + /* accept the connection */ /* TODO: maybe retrieve IP address for logging? *maybe* */ - int connection = accept(socket, (struct sockaddr *)NULL, NULL); + int connection = accept(socket, (struct sockaddr*)&address, (socklen_t*)&addrlen); /* set the timeout for the connection */ struct timeval timeout = { settings.timeout, 0 }; diff --git a/util.c b/util.c index aef31f5..7cabb91 100644 --- a/util.c +++ b/util.c @@ -15,11 +15,13 @@ #include "util.h" +#ifndef COSMOPOLITAN #include /* for va_end, va_list, va_start */ #include /* for snprintf, vsnprintf, vfprintf, BUFSIZ, stderr */ #include /* for exit */ #include /* for syslog, LOG_DEBUG, LOG_ERR */ #include /* for getpid */ +#endif #include "feuille.h" /* for Settings, settings */ From 0ccd538c97dc2835cd689745f2c0501963a0fff8 Mon Sep 17 00:00:00 2001 From: Tom MTT Date: Sun, 11 Dec 2022 02:10:36 +0100 Subject: [PATCH 5/7] style(Makefile): padding --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 63f1195..b5878a6 100644 --- a/Makefile +++ b/Makefile @@ -7,8 +7,8 @@ include config.mk TARGET = feuille.com TARGET$(COSMO) = feuille -SRC = feuille.c util.c server.c bin.c -OBJ = $(SRC:%.c=%.o) +SRC = feuille.c util.c server.c bin.c +OBJ = $(SRC:%.c=%.o) all: $(TARGET) feuille.1 cgi From de2c58196c69ff4f3341fc46ec3d56a2737b4c83 Mon Sep 17 00:00:00 2001 From: Tom MTT Date: Sun, 11 Dec 2022 02:12:30 +0100 Subject: [PATCH 6/7] fix(feuille.c): pledge if using cosmopolitan libc --- config.mk | 2 +- feuille.1 | 2 +- feuille.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config.mk b/config.mk index 80b04ea..fe5e177 100644 --- a/config.mk +++ b/config.mk @@ -1,5 +1,5 @@ # feuille version -VERSION = 2.0.0 +VERSION = 2.0.1 # paths (customize them to fit your system) PREFIX = /usr/local diff --git a/feuille.1 b/feuille.1 index 0d7bb17..596d7f3 100644 --- a/feuille.1 +++ b/feuille.1 @@ -14,7 +14,7 @@ . ftr VB CB . ftr VBI CBI .\} -.TH "feuille" "1" "November 2022" "feuille 2.0.0" "" +.TH "feuille" "1" "November 2022" "feuille 2.0.1" "" .hy .SH NAME .PP diff --git a/feuille.c b/feuille.c index 843bde9..27d2b94 100644 --- a/feuille.c +++ b/feuille.c @@ -400,7 +400,7 @@ int main(int argc, char *argv[]) die(1, "could not switch to user `%s'.\n", settings.user); } -#ifdef __OpenBSD__ +#if defined __OpenBSD__ || defined COSMOPOLITAN /* OpenBSD-only security measures */ pledge("proc stdio rpath wpath cpath inet", "stdio rpath wpath cpath inet"); #endif From 20fe93c3ca74eef6f045fed55ffc22604d91e830 Mon Sep 17 00:00:00 2001 From: Tom MTT Date: Sun, 11 Dec 2022 02:14:51 +0100 Subject: [PATCH 7/7] feat(README.md): cosmopolitan libc "tutorial" --- README.md | 10 +++++++++- config.mk | 2 +- feuille.1 | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d26ee11..195edcf 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,7 @@ That sould be it. Have fun! * Works on nearly all POSIX-compliant OSes * Can be run in the background and as a service * IPv6-enabled +* Now with 100% more (Cosmopolitan libc)[http://justine.lol/cosmopolitan/] support! ## Installation @@ -194,6 +195,13 @@ $ make $ sudo make install ``` +You can also build using the +(Cosmopolitan libc)[http://justine.lol/cosmopolitan/], which will +make an executable capable of running on Linux, OpenBSD, FreeBSD, +Mac... out of the box. To do so, build with the `COSMO` flag. +It will produce `feuille` and `feuille.com`, the former being the +debug binary, and the former the portable one. + If you wish to make a debug build, you can set `DEBUG` to whatever comes to your mind. @@ -201,7 +209,7 @@ You can also set `CC` to the compiler of your liking, like `clang` or `pcc`. ```console -$ make DEBUG=yes CC=clang +$ make COSMO=y DEBUG=y CC=clang ``` In order to compile CGI script(s), run: diff --git a/config.mk b/config.mk index fe5e177..d6b25f7 100644 --- a/config.mk +++ b/config.mk @@ -1,5 +1,5 @@ # feuille version -VERSION = 2.0.1 +VERSION = 2.1.0 # paths (customize them to fit your system) PREFIX = /usr/local diff --git a/feuille.1 b/feuille.1 index 596d7f3..f90ddb6 100644 --- a/feuille.1 +++ b/feuille.1 @@ -14,7 +14,7 @@ . ftr VB CB . ftr VBI CBI .\} -.TH "feuille" "1" "November 2022" "feuille 2.0.1" "" +.TH "feuille" "1" "November 2022" "feuille 2.1.0" "" .hy .SH NAME .PP