Commit Diff


commit - c8b6c180338b8a10ffb61a0c8169fef130b144d3
commit + 940f7cb79743357f8ba073c8d63c3681ed468bd2
blob - 338a860e245c0825b3f31415366d9daf45e0fec7
blob + 20824e63b2355b7204e92a3d14dd59a15808a677
--- Makefile
+++ Makefile
@@ -29,6 +29,7 @@ install: all
 	$(INSTALL) -d -m 755 -o root $(MAN)/man8
 	$(INSTALL) -d -m 750 -o root $(CONFDIR)
 	$(INSTALL) -d -m 755 -o root $(GEMINIDIR)
+	$(INSTALL) -d -m 755 -o _twind -g _twind $(GEMINIDIR)/logs
 	$(INSTALL) -m 644 -o root twind.8 $(MAN)/man8
 	$(INSTALL) -m 755 -o root twind $(SBIN)
 
blob - c8df8c1cde601d1c43bd25de8299c5ea99716b6e
blob + e58dc10e82e04522b731ca41ee0724ed0f3c4697
--- log.c
+++ log.c
@@ -18,16 +18,23 @@
 
 #define _GNU_SOURCE
 
+#include <sys/types.h>
+#include <sys/stat.h>
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
 #include <syslog.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <time.h>
 
 #include "log.h"
+#include "twind.h"
 
+#define MAXLOGLINE 1024
+
 static int		 debug;
 static int		 verbose;
 static const char	*log_procname;
@@ -199,3 +206,43 @@ fatalx(const char *emsg, ...)
 	va_end(ap);
 	exit(1);
 }
+
+void
+open_twind_logs(void)
+{
+	if ((access_fd = open(_PATH_TWIND_ACCESS_LOG, O_WRONLY|O_APPEND|O_CREAT, 0644))
+		== -1)
+		fatalx("Cannot open access log: %s", _PATH_TWIND_ACCESS_LOG);
+
+	if ((error_fd = open(_PATH_TWIND_ERROR_LOG, O_WRONLY|O_APPEND|O_CREAT, 0644))
+		== -1)
+		fatalx("Cannot open error log: %s", _PATH_TWIND_ACCESS_LOG);
+
+	return;
+}
+
+void
+user_log(int target, const char *fmt, ...)
+{
+	char buffer[MAXLOGLINE];
+	va_list ap;
+
+	memset(buffer, 0, MAXLOGLINE);
+
+
+	va_start(ap, fmt);
+	snprintf(buffer, MAXLOGLINE, fmt, ap);
+	switch(target) {
+		case 0:
+			vdprintf(access_fd, fmt, ap);
+			dprintf(access_fd, "\n");
+			break;
+		case 1:
+			vdprintf(error_fd, fmt, ap);
+			dprintf(error_fd, "\n");
+			break;
+		default:
+			log_warn("Non-existent user log target");
+	}
+	va_end(ap);
+}
blob - 6d100a48988fc5bde78793af99c23abcf20445f5
blob + 3ca04f696d7d58f1ffe9601d9823930f8e087773
--- twind.c
+++ twind.c
@@ -153,6 +153,8 @@ main(int argc, char *argv[])
 
 	log_init(debug_flag, LOG_DAEMON);
 	log_setverbose(verbose_flag);
+
+	open_twind_logs();
 
 #ifdef __OpenBSD__
 	if (pledge("stdio inet dns proc rpath", NULL) == -1)
@@ -357,6 +359,8 @@ main_request_handler(void *argp)
 			log_debug("Cannot get MIME type for %s", ext);
 	}
 
+	user_log(0, "Request for %s", finalpath);
+
 	if (send_response(ssl_peer, STATUS_SUCCESS, finalpath, mime) < 0) {
 		log_warn("Sending response to client failed");
 		return NULL;
blob - cb93f4a30e0baa7e6ad993f3793bb5d472bc9836
blob + 02ba8c54aa665df12147f2d2bd588f5f7ce04ddd
--- twind.h
+++ twind.h
@@ -17,11 +17,18 @@
 #ifndef _TWIND_H
 #define _TWIND_H
 
+#include <netinet/in.h>
+
 #include <openssl/ssl.h>
 
 #define VERSION "2021.a"
 #define MAXREQLEN 1025
+#define _PATH_TWIND_ACCESS_LOG "logs/access.log"
+#define _PATH_TWIND_ERROR_LOG "logs/error.log"
 
+int access_fd;
+int error_fd;
+
 /* gemini.c */
 int check_gemini_file(const char *);
 int send_response(SSL*, int, const char *, const char *);
@@ -39,6 +46,10 @@ void* xmalloc(size_t);
 char* xstrdup(const char *);
 size_t strlcpy(char *, const char *, size_t);
 
+/* log.c */
+void open_twind_logs(void);
+void user_log(int, const char *, ...);
+
 enum status_codes {
 	STATUS_INPUT = 10,
 	STATUS_SENSITIVE_INPUT = 11,
@@ -60,4 +71,8 @@ enum status_codes {
 	STATUS_CERT_NOT_VALID = 62,
 };
 
+struct client_connection {
+	SSL *ssl_peer;
+	char client_addr[INET6_ADDRSTRLEN];
+};
 #endif