vger/main.c

372 lines
8.3 KiB
C
Raw Normal View History

2020-12-04 18:31:13 +00:00
#include <sys/stat.h>
#include <sys/types.h>
2020-12-04 18:31:13 +00:00
#include <dirent.h>
2020-12-03 21:59:39 +00:00
#include <err.h>
#include <errno.h>
#include <limits.h>
2020-12-03 21:59:39 +00:00
#include <pwd.h>
2020-12-04 17:55:31 +00:00
#include <stdarg.h>
2020-12-01 22:39:05 +00:00
#include <stdio.h>
2020-12-03 21:59:39 +00:00
#include <stdlib.h>
2020-12-01 22:39:05 +00:00
#include <string.h>
2020-12-04 17:55:31 +00:00
#include <syslog.h>
2020-12-01 22:39:05 +00:00
#include <unistd.h>
2020-12-04 18:29:44 +00:00
#include "mimes.h"
#include "opts.h"
2020-12-01 22:39:05 +00:00
#define GEMINI_PART 9
#define GEMINI_REQUEST_MAX 1024 /* see https://gemini.circumlunar.space/docs/specification.html */
2020-12-01 22:39:05 +00:00
void autoindex(const char *);
void display_file(const char *);
void status(const int, const char *);
void status_redirect(const int, const char *);
void drop_privileges(const char *, const char *);
void eunveil(const char *, const char *);
size_t estrlcat(char *, const char *, size_t);
size_t estrlcpy(char *, const char *, size_t);
void
eunveil(const char *path, const char *permissions)
{
if (unveil(path, permissions) == -1) {
syslog(LOG_DAEMON, "unveil on %s failed", path);
err(1, "unveil");
}
}
2020-12-04 17:55:31 +00:00
size_t
estrlcpy(char *dst, const char *src, size_t dstsize)
{
size_t n = 0;
n = strlcpy(dst, src, dstsize);
if (n >= dstsize) {
err(1, "strlcyp failed for %s = %s", dst, src);
}
return n;
}
2020-12-09 20:12:25 +00:00
size_t
estrlcat(char *dst, const char *src, size_t dstsize)
{
size_t size;
if ((size = strlcat(dst, src, dstsize)) >= dstsize)
err(1, "strlcat on %s + %s", dst, src);
2020-12-09 20:12:25 +00:00
return size;
}
2020-12-01 22:39:05 +00:00
void
drop_privileges(const char *user, const char *path)
2020-12-01 22:39:05 +00:00
{
struct passwd *pw;
int chrooted = 0;
2020-12-05 13:44:59 +00:00
/*
* use chroot() if an user is specified requires root user to be
* running the program to run chroot() and then drop privileges
*/
if (strlen(user) > 0) {
2020-12-05 13:44:59 +00:00
/* is root? */
if (getuid() != 0) {
syslog(LOG_DAEMON, "chroot requires program to be run as root");
2020-12-04 18:31:42 +00:00
errx(1, "chroot requires root user");
}
/* search user uid from name */
if ((pw = getpwnam(user)) == NULL) {
syslog(LOG_DAEMON, "the user %s can't be found on the system", user);
err(1, "finding user");
}
/* chroot worked? */
if (chroot(path) != 0) {
syslog(LOG_DAEMON, "the chroot_dir %s can't be used for chroot", path);
err(1, "chroot");
}
chrooted = 1;
2020-12-04 18:39:16 +00:00
if (chdir("/") == -1) {
syslog(LOG_DAEMON, "failed to chdir(\"/\")");
err(1, "chdir");
}
/* drop privileges */
2020-12-04 18:39:16 +00:00
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) {
syslog(LOG_DAEMON, "dropping privileges to user %s (uid=%i) failed",
user, pw->pw_uid);
err(1, "Can't drop privileges");
}
}
#ifdef __OpenBSD__
/*
* prevent access to files other than the one in path
*/
if (chrooted) {
eunveil("/", "r");
} else {
eunveil(path, "r");
}
/*
* prevent system calls other parsing queryfor fread file and
* write to stdio
*/
if (pledge("stdio rpath", NULL) == -1) {
syslog(LOG_DAEMON, "pledge call failed");
err(1, "pledge");
}
#endif
2020-12-01 22:39:05 +00:00
}
2020-12-02 18:59:52 +00:00
void
status(const int code, const char *file_mime)
2020-12-02 18:59:52 +00:00
{
printf("%i %s; %s\r\n",
code, file_mime, lang);
2020-12-02 18:59:52 +00:00
}
2020-12-01 22:39:05 +00:00
2021-01-01 20:00:40 +00:00
void
status_redirect(const int code, const char *url)
{
printf("%i %s\r\n",
code, url);
}
2020-12-01 22:39:05 +00:00
void
display_file(const char *uri)
2020-12-01 22:39:05 +00:00
{
FILE *fd = NULL;
struct stat sb = {0};
ssize_t nread = 0;
const char *file_mime;
char *buffer[BUFSIZ];
char target[FILENAME_MAX] = {'\0'};
char fp[PATH_MAX] = {'\0'};
/* build file path inside chroot */
estrlcpy(fp, chroot_dir, sizeof(fp));
estrlcat(fp, uri, sizeof(fp));
2020-12-01 22:39:05 +00:00
2021-01-01 20:00:40 +00:00
/* this is to check if path exists and obtain metadata later */
if (stat(fp, &sb) == -1) {
/* check if fp is a symbolic link
* if so, redirect using its target */
if (lstat(fp, &sb) != -1 && S_ISLNK(sb.st_mode) == 1)
goto redirect;
else
goto err;
2021-01-01 20:00:40 +00:00
}
2020-12-01 22:39:05 +00:00
/* check if directory */
if (S_ISDIR(sb.st_mode) != 0) {
if (fp[strlen(fp) -1 ] != '/') {
/* no ending "/", redirect to "path/" */
char new_uri[PATH_MAX] = {'\0'};
estrlcpy(new_uri, uri, sizeof(fp));
estrlcat(new_uri, "/", sizeof(fp));
status_redirect(31, new_uri);
return;
} else {
/* there is a leading "/", display index.gmi */
char index_path[PATH_MAX] = {'\0'};
estrlcpy(index_path, fp, sizeof(index_path));
estrlcat(index_path, "index.gmi", sizeof(index_path));
/* check if index.gmi exists or show autoindex */
if (stat(index_path, &sb) == 0) {
estrlcpy(fp, index_path, sizeof(fp));
} else if (doautoidx != 0) {
autoindex(fp);
return;
} else {
goto err;
}
}
}
2020-12-01 22:39:05 +00:00
2021-01-01 20:00:40 +00:00
/* open the file requested */
if ((fd = fopen(fp, "r")) == NULL) { goto err; }
2021-01-01 20:00:40 +00:00
file_mime = get_file_mime(fp, default_mime);
2020-12-02 18:59:52 +00:00
status(20, file_mime);
2020-12-01 22:39:05 +00:00
/* read the file and write it to stdout */
while ((nread = fread(buffer, sizeof(char), sizeof(buffer), fd)) != 0)
fwrite(buffer, sizeof(char), nread, stdout);
goto closefd;
syslog(LOG_DAEMON, "path served %s", fp);
2020-12-01 22:39:05 +00:00
return;
2020-12-05 13:44:59 +00:00
err:
/* return an error code and no content */
status(51, "text/gemini");
syslog(LOG_DAEMON, "path invalid %s", fp);
goto closefd;
2021-01-01 20:00:40 +00:00
redirect:
/* read symbolic link target to redirect */
if (readlink(fp, target, FILENAME_MAX) == -1) {
goto err;
2021-01-01 20:00:40 +00:00
}
status_redirect(30, target);
syslog(LOG_DAEMON, "redirection from %s to %s", fp, target);
2021-01-01 20:00:40 +00:00
closefd:
if (S_ISREG(sb.st_mode) != 0) {
fclose(fd);
}
}
void
autoindex(const char *path)
{
struct dirent *dp;
DIR *fd;
if (!(fd = opendir(path))) {
err(1,"opendir '%s':", path);
}
syslog(LOG_DAEMON, "autoindex: %s", path);
status(20, "text/gemini");
/* TODO : add ending / in name if directory */
while ((dp = readdir(fd))) {
/* skip self */
if (!strcmp(dp->d_name, ".")) {
continue;
}
if (dp->d_type == DT_DIR) {
printf("=> ./%s/ %s/\n", dp->d_name, dp->d_name);
} else {
printf("=> ./%s %s\n", dp->d_name, dp->d_name);
}
}
closedir(fd);
2020-12-01 22:39:05 +00:00
}
int
main(int argc, char **argv)
{
char request [GEMINI_REQUEST_MAX] = {'\0'};
char hostname [GEMINI_REQUEST_MAX] = {'\0'};
char uri [PATH_MAX] = {'\0'};
2020-12-05 13:44:59 +00:00
char user [_SC_LOGIN_NAME_MAX] = "";
int virtualhost = 0;
2020-12-08 20:48:35 +00:00
int option = 0;
2020-12-08 20:48:21 +00:00
char *pos = NULL;
while ((option = getopt(argc, argv, ":d:l:m:u:vi")) != -1) {
switch (option) {
case 'd':
estrlcpy(chroot_dir, optarg, sizeof(chroot_dir));
break;
case 'l':
estrlcpy(lang, "lang=", sizeof(lang));
estrlcat(lang, optarg, sizeof(lang));
break;
case 'm':
estrlcpy(default_mime, optarg, sizeof(default_mime));
break;
2020-12-03 21:59:39 +00:00
case 'u':
estrlcpy(user, optarg, sizeof(user));
break;
case 'v':
virtualhost = 1;
break;
case 'i':
doautoidx = 1;
2020-12-03 21:59:39 +00:00
break;
}
2020-12-01 22:39:05 +00:00
}
2020-12-02 18:59:52 +00:00
2020-12-03 21:59:39 +00:00
/*
* do chroot if an user is supplied run pledge/unveil if OpenBSD
2020-12-03 21:59:39 +00:00
*/
drop_privileges(user, chroot_dir);
2020-12-05 13:44:59 +00:00
2020-12-01 22:39:05 +00:00
/*
* read 1024 chars from stdin
* to get the request
*/
if (fgets(request, GEMINI_REQUEST_MAX, stdin) == NULL) {
status(59, "request is too long (1024 max)");
syslog(LOG_DAEMON, "request is too long (1024 max): %s", request);
exit(1);
}
2020-12-01 22:39:05 +00:00
/* remove \r\n at the end of string */
pos = strchr(request, '\r');
if (pos != NULL)
2020-12-04 17:55:31 +00:00
*pos = '\0';
2020-12-01 22:39:05 +00:00
/*
* check if the beginning of the request starts with
* gemini://
*/
if (strncmp(request, "gemini://", GEMINI_PART) != 0) {
2020-12-01 22:39:05 +00:00
/* error code url malformed */
syslog(LOG_DAEMON, "request «%s» doesn't match gemini://",
request);
2020-12-01 22:39:05 +00:00
exit(1);
}
2020-12-04 17:55:31 +00:00
syslog(LOG_DAEMON, "request %s", request);
2020-12-01 22:39:05 +00:00
/* remove the gemini:// part */
memmove(request, request + GEMINI_PART, sizeof(request) - GEMINI_PART);
2020-12-01 22:39:05 +00:00
/*
* look for the first / after the hostname
* in order to split hostname and uri
*/
pos = strchr(request, '/');
if (pos != NULL) {
/* if there is a / found */
/* separate hostname and uri */
estrlcpy(uri, pos, strlen(pos)+1);
/* just keep hostname in request */
pos[0] = '\0';
2020-12-01 22:39:05 +00:00
}
/* check if client added :port at end of request */
pos = strchr(request, ':');
if (pos != NULL) {
/* end string at :*/
pos[0] = '\0';
}
/* copy hostname from request */
estrlcpy(hostname, request, sizeof(hostname));
2020-12-01 22:39:05 +00:00
/*
* if virtualhost feature is actived looking under the chroot_path +
* hostname directory gemini://foobar/hello will look for
* chroot_path/foobar/hello
*/
if (virtualhost) {
if (strlen(uri) == 0) {
estrlcpy(uri, "/index.gmi", sizeof(uri));
}
char new_uri[PATH_MAX] = {'\0'};
estrlcpy(new_uri, hostname, sizeof(new_uri));
estrlcat(new_uri, uri, sizeof(new_uri));
estrlcpy(uri, new_uri, sizeof(uri));
}
2020-12-01 22:39:05 +00:00
/* open file and send it to stdout */
display_file(uri);
2020-12-01 22:39:05 +00:00
return (0);
2020-12-01 22:39:05 +00:00
}