Compare commits

..

No commits in common. "20fe93c3ca74eef6f045fed55ffc22604d91e830" and "3922c5cabbc2f0be505544203023293509e39338" have entirely different histories.

10 changed files with 40 additions and 107 deletions

4
.gitignore vendored
View file

@ -1,8 +1,4 @@
*.o *.o
cosmopolitan
feuille feuille
feuille.com
cgi/feuille.cgi cgi/feuille.cgi

View file

@ -4,13 +4,11 @@
include config.mk include config.mk
TARGET = feuille.com TARGET = feuille
TARGET$(COSMO) = feuille
SRC = feuille.c util.c server.c bin.c SRC = feuille.c util.c server.c bin.c
OBJ = $(SRC:%.c=%.o) OBJ = $(SRC:%.c=%.o)
all: $(TARGET) feuille.1 cgi all: $(TARGET) $(TARGET).1 cgi
run: $(TARGET) run: $(TARGET)
./$(TARGET) ./$(TARGET)
@ -20,10 +18,10 @@ clean:
@rm -f $(OBJ) @rm -f $(OBJ)
distclean: distclean:
@printf "%-8s feuille feuille.com $(OBJ)\n" "rm" @printf "%-8s $(TARGET) $(OBJ)\n" "rm"
@rm -f feuille feuille.com $(OBJ) @rm -f $(TARGET) $(OBJ)
install: $(TARGET) feuille.1 install: $(TARGET) $(TARGET).1
@echo "installing executable file to $(PREFIX)/bin" @echo "installing executable file to $(PREFIX)/bin"
@mkdir -p "$(PREFIX)/bin" @mkdir -p "$(PREFIX)/bin"
@cp -f $(TARGET) "$(PREFIX)/bin" @cp -f $(TARGET) "$(PREFIX)/bin"
@ -31,53 +29,34 @@ install: $(TARGET) feuille.1
@echo "installing manpage to $(MAN)/man1" @echo "installing manpage to $(MAN)/man1"
@mkdir -p $(MAN)/man1 @mkdir -p $(MAN)/man1
@cp -f feuille.1 $(MAN)/man1 @cp -f $(TARGET).1 $(MAN)/man1
@chmod 644 $(MAN)/man1/feuille.1 @chmod 644 $(MAN)/man1/$(TARGET).1
uninstall: $(PREFIX)/bin/$(TARGET) $(MAN)/man1/feuille.1 uninstall: $(PREFIX)/bin/$(TARGET) $(MAN)/man1/$(TARGET).1
@echo "removing executable file from $(PREFIX)/bin" @echo "removing executable file from $(PREFIX)/bin"
@rm -f "$(PREFIX)/bin/$(TARGET)" @rm -f "$(PREFIX)/bin/$(TARGET)"
@echo "removing manpage from $(MAN)/man1" @echo "removing manpage from $(MAN)/man1"
@rm -f $(MAN)/man1/feuille.1 @rm -f $(MAN)/man1/$(TARGET).1
feuille.1: feuille.1.md config.mk $(TARGET): $(OBJ)
@printf "%-8s feuille.1.md -o feuille.1\n" "pandoc" @printf "%-8s $(OBJ) -o $@\n" "$(CC)"
@sed "s/{VERSION}/$(VERSION)/g" feuille.1.md | pandoc -s -t man -o feuille.1 @$(CC) $(LDFLAGS) $(OBJ) -o $@
feuille: $(OBJ) $(TARGET).1: $(TARGET).1.md config.mk
@printf "%-8s $(OBJ) -o feuille\n" "$(CC)" @printf "%-8s $(TARGET).1.md -o $@\n" "pandoc"
@$(CC) $(OBJ) -o feuille $(LDFLAGS) @sed "s/{VERSION}/$(VERSION)/g" $(TARGET).1.md | pandoc -s -t man -o $@
# 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 ADDR = 127.0.0.1
PORT = 9999 PORT = 9999
cgi: cgi/feuille.cgi cgi: cgi/feuille.cgi
cgi/feuille.cgi: cgi/feuille.cgi.c cgi/feuille.cgi: cgi/feuille.cgi.c
@printf "%-8s cgi/feuille.cgi.c -o cgi/feuille.cgi\n" "$(CC)" @printf "%-8s cgi/feuille.cgi.c -o $@\n" "$(CC)"
@$(CC) cgi/feuille.cgi.c -o cgi/feuille.cgi -std=c99 -O3 -static -Wall -Wextra \ @$(CC) $(CFLAGS) $(LDFLAGS) -static -DADDR=\"$(ADDR)\" -DPORT=$(PORT) cgi/feuille.cgi.c -o $@
-DADDR=\"$(ADDR)\" -DPORT=$(PORT) \
$(INCS) $(LIBS)
.SUFFIXES: .c .o .SUFFIXES: .c .o
.c.o: .c.o:
@printf "%-8s $<\n" "$(CC)" @printf "%-8s $<\n" "$(CC)"
@$(CC) -c $< $(CFLAGS) @$(CC) $(CFLAGS) -c $<

View file

@ -159,7 +159,6 @@ That sould be it. Have fun!
* Works on nearly all POSIX-compliant OSes * Works on nearly all POSIX-compliant OSes
* Can be run in the background and as a service * Can be run in the background and as a service
* IPv6-enabled * IPv6-enabled
* Now with 100% more (Cosmopolitan libc)[http://justine.lol/cosmopolitan/] support!
## Installation ## Installation
@ -195,13 +194,6 @@ $ make
$ sudo make install $ 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 If you wish to make a debug build, you can set `DEBUG` to whatever
comes to your mind. comes to your mind.
@ -209,7 +201,7 @@ You can also set `CC` to the compiler of your liking, like `clang` or
`pcc`. `pcc`.
```console ```console
$ make COSMO=y DEBUG=y CC=clang $ make DEBUG=yes CC=clang
``` ```
In order to compile CGI script(s), run: In order to compile CGI script(s), run:

3
arg.h
View file

@ -34,11 +34,8 @@
#ifndef _ARG_H_ #ifndef _ARG_H_
#define _ARG_H_ #define _ARG_H_
#ifndef COSMOPOLITAN
#include <stdlib.h> #include <stdlib.h>
#include <stddef.h> #include <stddef.h>
#endif
extern char *argv0; extern char *argv0;
/* use main(int argc, char *argv[]) */ /* use main(int argc, char *argv[]) */

2
bin.c
View file

@ -13,13 +13,11 @@
#include "bin.h" #include "bin.h"
#ifndef COSMOPOLITAN
#include <errno.h> /* for errno */ #include <errno.h> /* for errno */
#include <stdio.h> /* for NULL, fclose, fopen, fputs, snprintf, FILE */ #include <stdio.h> /* for NULL, fclose, fopen, fputs, snprintf, FILE */
#include <stdlib.h> /* for calloc, free, malloc, rand, realloc */ #include <stdlib.h> /* for calloc, free, malloc, rand, realloc */
#include <string.h> /* for strlen */ #include <string.h> /* for strlen */
#include <unistd.h> /* for access, F_OK */ #include <unistd.h> /* for access, F_OK */
#endif
#include "feuille.h" /* for Settings, settings */ #include "feuille.h" /* for Settings, settings */

View file

@ -1,5 +1,5 @@
# feuille version # feuille version
VERSION = 2.1.0 VERSION = 1.19.5
# paths (customize them to fit your system) # paths (customize them to fit your system)
PREFIX = /usr/local PREFIX = /usr/local
@ -17,28 +17,13 @@ LIBS = -L/usr/lib -lc
# compiler # compiler
CC = cc CC = cc
# cosmopolitan libc flags # debug build
CCFLAGS = -std=c99 -nostdinc -fno-pie -fno-omit-frame-pointer \ CFLAGS = -g -std=c99 -Wall -Wextra -Wpedantic -Wno-sign-compare -DVERSION=\"$(VERSION)\" -DDEBUG $(INCS)
-mno-tls-direct-seg-refs -mno-red-zone \ LDFLAGS = -g $(LIBS)
-DVERSION=\"$(VERSION)\" -DCOSMOPOLITAN \
-I. -include cosmopolitan/cosmopolitan.h
CLDFLAGS = -std=c99 -static -nostdlib -fno-pie -fno-omit-frame-pointer \ # release build
-mno-tls-direct-seg-refs -mno-red-zone \ CFLAGS$(DEBUG) = -std=c99 -Wall -Wextra -Wno-sign-compare -DVERSION=\"$(VERSION)\" -O3 $(INCS)
-fuse-ld=bfd -Wl,-T,cosmopolitan/ape.lds -Wl,--gc-sections \ LDFLAGS$(DEBUG) = -s $(LIBS)
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) # static build (uncomment)
#LD_FLAGS += -static #LD_FLAGS += -static

View file

@ -14,7 +14,7 @@
. ftr VB CB . ftr VB CB
. ftr VBI CBI . ftr VBI CBI
.\} .\}
.TH "feuille" "1" "November 2022" "feuille 2.1.0" "" .TH "feuille" "1" "November 2022" "feuille 1.19.5" ""
.hy .hy
.SH NAME .SH NAME
.PP .PP

View file

@ -15,7 +15,6 @@
#include "feuille.h" #include "feuille.h"
#ifndef COSMOPOLITAN
#include <errno.h> /* for errno, ERANGE, EAGAIN, EFBIG, ENOENT */ #include <errno.h> /* for errno, ERANGE, EAGAIN, EFBIG, ENOENT */
#include <grp.h> /* for initgroups */ #include <grp.h> /* for initgroups */
#include <limits.h> /* for USHRT_MAX, ULONG_MAX, CHAR_MAX, PATH_MAX, UCHA... */ #include <limits.h> /* for USHRT_MAX, ULONG_MAX, CHAR_MAX, PATH_MAX, UCHA... */
@ -30,7 +29,6 @@
#include <syslog.h> /* for syslog, openlog, LOG_WARNING, LOG_NDELAY, LOG_... */ #include <syslog.h> /* for syslog, openlog, LOG_WARNING, LOG_NDELAY, LOG_... */
#include <time.h> /* for time */ #include <time.h> /* for time */
#include <unistd.h> /* for getuid, access, chdir, chown, chroot, close */ #include <unistd.h> /* for getuid, access, chdir, chown, chroot, close */
#endif
#include "arg.h" /* for EARGF, ARGBEGIN, ARGEND */ #include "arg.h" /* for EARGF, ARGBEGIN, ARGEND */
#include "bin.h" /* for create_url, generate_id, write_paste */ #include "bin.h" /* for create_url, generate_id, write_paste */
@ -95,12 +93,6 @@ void accept_loop(int server)
/* accept loop */ /* accept loop */
int connection; int connection;
while ((connection = accept_connection(server))) { 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)); verbose(1, "--- new incoming connection. connection ID: %d:%d ---", pid, time(0));
unsigned long paste_size = 0; unsigned long paste_size = 0;
@ -350,6 +342,7 @@ int main(int argc, char *argv[])
gid = user->pw_gid; gid = user->pw_gid;
} else { } else {
puts("");
syslog(LOG_WARNING, "running as non-root user."); syslog(LOG_WARNING, "running as non-root user.");
syslog(LOG_WARNING, "`chroot' and user switching have been disabled."); syslog(LOG_WARNING, "`chroot' and user switching have been disabled.");
puts(""); puts("");
@ -389,18 +382,15 @@ int main(int argc, char *argv[])
if (setgid(gid) != 0 || getgid() != gid) if (setgid(gid) != 0 || getgid() != gid)
die(1, "could not switch to group for user `%s'.\n", settings.user); 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) if (initgroups(settings.user, gid) != 0)
die(1, "could not initialize other groups for user `%s'.\n", settings.user); die(1, "could not initialize other groups for user `%s'.\n", settings.user);
#endif
/* switching user */ /* switching user */
if (setuid(uid) != 0 || getuid() != uid) if (setuid(uid) != 0 || getuid() != uid)
die(1, "could not switch to user `%s'.\n", settings.user); die(1, "could not switch to user `%s'.\n", settings.user);
} }
#if defined __OpenBSD__ || defined COSMOPOLITAN #ifdef __OpenBSD__
/* OpenBSD-only security measures */ /* OpenBSD-only security measures */
pledge("proc stdio rpath wpath cpath inet", "stdio rpath wpath cpath inet"); pledge("proc stdio rpath wpath cpath inet", "stdio rpath wpath cpath inet");
#endif #endif

View file

@ -13,7 +13,6 @@
#include "server.h" #include "server.h"
#ifndef COSMOPOLITAN
#include <arpa/inet.h> /* for inet_pton */ #include <arpa/inet.h> /* for inet_pton */
#include <errno.h> /* for errno, EAGAIN, EFBIG, ENOENT */ #include <errno.h> /* for errno, EAGAIN, EFBIG, ENOENT */
#include <netinet/in.h> /* for htons, sockaddr_in, sockaddr_in6, IPPROTO_IPV6 */ #include <netinet/in.h> /* for htons, sockaddr_in, sockaddr_in6, IPPROTO_IPV6 */
@ -25,7 +24,6 @@
#include <sys/socket.h> /* for setsockopt, bind, socket, SOL_SOCKET, AF_INET */ #include <sys/socket.h> /* for setsockopt, bind, socket, SOL_SOCKET, AF_INET */
#include <sys/time.h> /* for timeval */ #include <sys/time.h> /* for timeval */
#include <unistd.h> /* for close */ #include <unistd.h> /* for close */
#endif
#include "feuille.h" /* for Settings, settings */ #include "feuille.h" /* for Settings, settings */
#include "util.h" /* for verbose */ #include "util.h" /* for verbose */
@ -105,14 +103,19 @@ int initialize_server()
if (setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0) if (setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0)
return -1; return -1;
#ifndef __OpenBSD__
/* Enable dual-stack mode on supported platforms */ /* Enable dual-stack mode on supported platforms */
verbose(3, " IPV6_V6ONLY..."); verbose(3, " IPV6_V6ONLY...");
if (setsockopt(server, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6_only, sizeof(ipv6_only)) < 0)
if (setsockopt(server, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6_only, sizeof(ipv6_only)) < 0) { return -1;
syslog(LOG_WARNING, "dual-stack mode is disabled on your platform."); #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."); syslog(LOG_WARNING, "feuille will only listen on the `::' IPv6 address.");
puts(""); puts("");
} }
#endif
/* bind address and port */ /* bind address and port */
verbose(3, "binding address on the socket..."); verbose(3, "binding address on the socket...");
@ -138,14 +141,9 @@ int initialize_server()
*/ */
int accept_connection(int socket) 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 */ /* accept the connection */
/* TODO: maybe retrieve IP address for logging? *maybe* */ /* TODO: maybe retrieve IP address for logging? *maybe* */
int connection = accept(socket, (struct sockaddr*)&address, (socklen_t*)&addrlen); int connection = accept(socket, (struct sockaddr *)NULL, NULL);
/* set the timeout for the connection */ /* set the timeout for the connection */
struct timeval timeout = { settings.timeout, 0 }; struct timeval timeout = { settings.timeout, 0 };

2
util.c
View file

@ -15,13 +15,11 @@
#include "util.h" #include "util.h"
#ifndef COSMOPOLITAN
#include <stdarg.h> /* for va_end, va_list, va_start */ #include <stdarg.h> /* for va_end, va_list, va_start */
#include <stdio.h> /* for snprintf, vsnprintf, vfprintf, BUFSIZ, stderr */ #include <stdio.h> /* for snprintf, vsnprintf, vfprintf, BUFSIZ, stderr */
#include <stdlib.h> /* for exit */ #include <stdlib.h> /* for exit */
#include <syslog.h> /* for syslog, LOG_DEBUG, LOG_ERR */ #include <syslog.h> /* for syslog, LOG_DEBUG, LOG_ERR */
#include <unistd.h> /* for getpid */ #include <unistd.h> /* for getpid */
#endif
#include "feuille.h" /* for Settings, settings */ #include "feuille.h" /* for Settings, settings */