2020-12-05 05:31:13 +11:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2020-12-04 08:59:39 +11:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2020-12-10 07:08:09 +11:00
|
|
|
#include <limits.h>
|
2020-12-04 08:59:39 +11:00
|
|
|
#include <pwd.h>
|
2020-12-05 04:55:31 +11:00
|
|
|
#include <stdarg.h>
|
2020-12-02 09:39:05 +11:00
|
|
|
#include <stdio.h>
|
2020-12-04 08:59:39 +11:00
|
|
|
#include <stdlib.h>
|
2020-12-02 09:39:05 +11:00
|
|
|
#include <string.h>
|
2020-12-05 04:55:31 +11:00
|
|
|
#include <syslog.h>
|
2020-12-02 09:39:05 +11:00
|
|
|
#include <unistd.h>
|
2020-12-05 05:29:44 +11:00
|
|
|
|
|
|
|
#include "mimes.h"
|
2020-12-02 09:39:05 +11:00
|
|
|
|
|
|
|
#define GEMINI_PART 9
|
2020-12-03 04:07:10 +11:00
|
|
|
#define DEFAULT_LANG "en"
|
2020-12-10 07:08:09 +11:00
|
|
|
#define DEFAULT_CHROOT "/var/gemini"
|
|
|
|
#define GEMINI_REQUEST_MAX 1024 /* see https://gemini.circumlunar.space/docs/specification.html */
|
|
|
|
|
2020-12-02 09:39:05 +11:00
|
|
|
|
|
|
|
|
2020-12-03 05:59:52 +11:00
|
|
|
void display_file(const char *, const char *);
|
|
|
|
void status (const int, const char *, const char *);
|
2020-12-05 05:08:36 +11:00
|
|
|
void drop_privileges(const char *, const char *);
|
2020-12-10 00:20:10 +11:00
|
|
|
void eunveil(const char *path, const char *permissions);
|
2020-12-10 07:08:09 +11:00
|
|
|
size_t estrlcpy(char *dst, const char *src, size_t dstsize);
|
2020-12-10 00:20:10 +11:00
|
|
|
|
|
|
|
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-05 04:55:31 +11:00
|
|
|
|
2020-12-10 07:08:09 +11: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, "estrlcpy failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2020-12-02 09:39:05 +11:00
|
|
|
void
|
2020-12-05 05:08:36 +11:00
|
|
|
drop_privileges(const char *user, const char *path)
|
2020-12-02 09:39:05 +11:00
|
|
|
{
|
2020-12-05 05:08:36 +11:00
|
|
|
struct passwd *pw;
|
2020-12-10 00:20:10 +11:00
|
|
|
int chrooted = 0;
|
2020-12-06 00:44:59 +11:00
|
|
|
|
2020-12-05 05:08:36 +11: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-06 00:44:59 +11:00
|
|
|
|
2020-12-05 05:08:36 +11:00
|
|
|
/* is root? */
|
|
|
|
if (getuid() != 0) {
|
|
|
|
syslog(LOG_DAEMON, "chroot requires program to be run as root");
|
2020-12-05 05:31:42 +11:00
|
|
|
errx(1, "chroot requires root user");
|
2020-12-05 05:08:36 +11:00
|
|
|
}
|
|
|
|
/* 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? */
|
2020-12-06 21:37:32 +11:00
|
|
|
if (chroot(path) != 0) {
|
|
|
|
syslog(LOG_DAEMON, "the chroot_dir %s can't be used for chroot", path);
|
2020-12-05 05:08:36 +11:00
|
|
|
err(1, "chroot");
|
|
|
|
}
|
2020-12-10 00:20:10 +11:00
|
|
|
chrooted = 1;
|
2020-12-05 05:39:16 +11:00
|
|
|
if (chdir("/") == -1) {
|
|
|
|
syslog(LOG_DAEMON, "failed to chdir(\"/\")");
|
|
|
|
err(1, "chdir");
|
|
|
|
}
|
2020-12-05 05:08:36 +11:00
|
|
|
/* drop privileges */
|
2020-12-05 05:39:16 +11: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)) {
|
2020-12-05 05:08:36 +11:00
|
|
|
syslog(LOG_DAEMON, "dropping privileges to user %s (uid=%i) failed",
|
|
|
|
user, pw->pw_uid);
|
|
|
|
err(1, "Can't drop privileges");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef __OpenBSD__
|
|
|
|
/*
|
2020-12-10 00:20:10 +11:00
|
|
|
* prevent access to files other than the one in path
|
|
|
|
*/
|
|
|
|
if (chrooted) {
|
|
|
|
eunveil("/", "r");
|
|
|
|
} else {
|
|
|
|
eunveil(path, "r");
|
2020-12-05 05:08:36 +11:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 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-02 09:39:05 +11:00
|
|
|
}
|
|
|
|
|
2020-12-03 05:59:52 +11:00
|
|
|
void
|
2020-12-05 05:08:36 +11:00
|
|
|
status(const int code, const char *file_mime, const char *lang)
|
2020-12-03 05:59:52 +11:00
|
|
|
{
|
2020-12-05 05:08:36 +11:00
|
|
|
printf("%i %s; lang=%s\r\n",
|
|
|
|
code, file_mime, lang);
|
2020-12-03 05:59:52 +11:00
|
|
|
}
|
2020-12-02 09:39:05 +11:00
|
|
|
|
|
|
|
void
|
2020-12-03 05:59:52 +11:00
|
|
|
display_file(const char *path, const char *lang)
|
2020-12-02 09:39:05 +11:00
|
|
|
{
|
2020-12-06 02:58:26 +11:00
|
|
|
FILE *fd;
|
|
|
|
struct stat sb;
|
|
|
|
ssize_t nread;
|
2020-12-06 21:27:43 +11:00
|
|
|
char *buffer[BUFSIZ];
|
2020-12-06 02:58:26 +11:00
|
|
|
const char *file_mime;
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2020-12-02 23:32:39 +11:00
|
|
|
/* this is to check if path is a directory */
|
2020-12-05 05:43:29 +11:00
|
|
|
if (stat(path, &sb) == -1)
|
|
|
|
goto err;
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2020-12-03 05:59:52 +11:00
|
|
|
/* open the file requested */
|
2020-12-05 05:43:29 +11:00
|
|
|
if ((fd = fopen(path, "r")) == NULL)
|
|
|
|
goto err;
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2020-12-05 05:43:29 +11:00
|
|
|
/* check if directory */
|
|
|
|
if (S_ISDIR(sb.st_mode) == 1)
|
|
|
|
goto err;
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2020-12-06 02:58:26 +11:00
|
|
|
file_mime = get_file_mime(path);
|
2020-12-03 05:59:52 +11:00
|
|
|
|
2020-12-05 05:43:29 +11:00
|
|
|
status(20, file_mime, lang);
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2020-12-05 05:43:29 +11:00
|
|
|
/* read the file and write it to stdout */
|
2020-12-06 21:27:43 +11:00
|
|
|
while ((nread = fread(buffer, sizeof(char), sizeof(buffer), fd)) != 0)
|
2020-12-05 05:43:29 +11:00
|
|
|
fwrite(buffer, sizeof(char), nread, stdout);
|
|
|
|
fclose(fd);
|
|
|
|
syslog(LOG_DAEMON, "path served %s", path);
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2020-12-05 05:43:29 +11:00
|
|
|
return;
|
2020-12-06 00:44:59 +11:00
|
|
|
err:
|
2020-12-05 05:43:29 +11:00
|
|
|
/* return an error code and no content */
|
|
|
|
status(40, "text/gemini", lang);
|
|
|
|
syslog(LOG_DAEMON, "path invalid %s", path);
|
2020-12-02 09:39:05 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2020-12-10 07:08:09 +11:00
|
|
|
char request [GEMINI_REQUEST_MAX] = {'\0'};
|
|
|
|
char hostname [GEMINI_REQUEST_MAX] = {'\0'};
|
|
|
|
char file [GEMINI_REQUEST_MAX] = {'\0'};
|
|
|
|
char path [GEMINI_REQUEST_MAX] = DEFAULT_CHROOT;
|
2020-12-03 05:59:52 +11:00
|
|
|
char lang [3] = DEFAULT_LANG;
|
2020-12-06 00:44:59 +11:00
|
|
|
char user [_SC_LOGIN_NAME_MAX] = "";
|
2020-12-03 01:31:21 +11:00
|
|
|
int virtualhost = 0;
|
2020-12-09 07:48:35 +11:00
|
|
|
int option = 0;
|
2020-12-06 00:44:59 +11:00
|
|
|
int chroot = 0;
|
2020-12-09 07:51:56 +11:00
|
|
|
int start_with_gemini = 0;
|
2020-12-09 07:48:21 +11:00
|
|
|
char *pos = NULL;
|
2020-12-02 23:32:39 +11:00
|
|
|
|
2020-12-04 08:59:39 +11:00
|
|
|
while ((option = getopt(argc, argv, ":d:l:u:v")) != -1) {
|
2020-12-02 23:32:39 +11:00
|
|
|
switch (option) {
|
|
|
|
case 'd':
|
2020-12-10 07:08:09 +11:00
|
|
|
estrlcpy(path, optarg, sizeof(path));
|
2020-12-02 23:32:39 +11:00
|
|
|
break;
|
2020-12-03 01:31:21 +11:00
|
|
|
case 'v':
|
|
|
|
virtualhost = 1;
|
|
|
|
break;
|
2020-12-03 04:07:10 +11:00
|
|
|
case 'l':
|
2020-12-10 07:08:09 +11:00
|
|
|
estrlcpy(lang, optarg, sizeof(lang));
|
2020-12-03 04:07:10 +11:00
|
|
|
break;
|
2020-12-04 08:59:39 +11:00
|
|
|
case 'u':
|
2020-12-10 07:08:09 +11:00
|
|
|
estrlcpy(user, optarg, sizeof(user));
|
2020-12-06 00:44:59 +11:00
|
|
|
chroot = 1;
|
2020-12-04 08:59:39 +11:00
|
|
|
break;
|
2020-12-02 23:32:39 +11:00
|
|
|
}
|
2020-12-02 09:39:05 +11:00
|
|
|
}
|
2020-12-03 05:59:52 +11:00
|
|
|
|
2020-12-04 08:59:39 +11:00
|
|
|
/*
|
2020-12-05 05:08:36 +11:00
|
|
|
* do chroot if an user is supplied run pledge/unveil if OpenBSD
|
2020-12-04 08:59:39 +11:00
|
|
|
*/
|
2020-12-05 05:08:36 +11:00
|
|
|
drop_privileges(user, path);
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2020-12-06 00:44:59 +11:00
|
|
|
/* change basedir to / to build the filepath if we use chroot */
|
|
|
|
if (chroot == 1)
|
2020-12-10 07:08:09 +11:00
|
|
|
estrlcpy(path, "/", sizeof(path));
|
2020-12-06 00:44:59 +11:00
|
|
|
|
2020-12-02 09:39:05 +11:00
|
|
|
/*
|
|
|
|
* read 1024 chars from stdin
|
|
|
|
* to get the request
|
|
|
|
*/
|
2020-12-10 07:08:09 +11:00
|
|
|
if (fgets(request, GEMINI_REQUEST_MAX, stdin) == NULL){
|
|
|
|
/*TODO : add error code 5x */
|
|
|
|
syslog(LOG_DAEMON, "request is too long (1024 max): %s", request);
|
|
|
|
exit(1);
|
|
|
|
}
|
2020-12-02 09:39:05 +11:00
|
|
|
|
|
|
|
/* remove \r\n at the end of string */
|
|
|
|
pos = strchr(request, '\r');
|
2020-12-02 23:32:39 +11:00
|
|
|
if (pos != NULL)
|
2020-12-05 04:55:31 +11:00
|
|
|
*pos = '\0';
|
2020-12-02 09:39:05 +11:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check if the beginning of the request starts with
|
|
|
|
* gemini://
|
|
|
|
*/
|
2020-12-09 23:53:21 +11:00
|
|
|
start_with_gemini = strncmp(request, "gemini://", GEMINI_PART);
|
2020-12-02 09:39:05 +11:00
|
|
|
|
|
|
|
/* the request must start with gemini:// */
|
2020-12-02 23:32:39 +11:00
|
|
|
if (start_with_gemini != 0) {
|
2020-12-02 09:39:05 +11:00
|
|
|
/* error code url malformed */
|
2020-12-05 04:55:31 +11:00
|
|
|
syslog(LOG_DAEMON, "request «%s» doesn't match gemini:// at index %i",
|
2020-12-02 23:32:39 +11:00
|
|
|
request, start_with_gemini);
|
2020-12-02 09:39:05 +11:00
|
|
|
exit(1);
|
|
|
|
}
|
2020-12-05 04:55:31 +11:00
|
|
|
syslog(LOG_DAEMON, "request %s", request);
|
|
|
|
|
2020-12-02 09:39:05 +11:00
|
|
|
/* remove the gemini:// part */
|
2020-12-10 07:08:09 +11:00
|
|
|
memmove(request, request + GEMINI_PART, sizeof(request) - GEMINI_PART);
|
2020-12-02 09:39:05 +11: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 */
|
2020-12-02 23:32:39 +11:00
|
|
|
int position = -1;
|
2020-12-09 18:31:18 +11:00
|
|
|
for (size_t i = 0; i < sizeof(request); i++) {
|
2020-12-02 09:39:05 +11:00
|
|
|
if (*pos == request[i]) {
|
|
|
|
position = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* separate hostname and uri */
|
|
|
|
if (position != -1) {
|
2020-12-10 07:08:09 +11:00
|
|
|
/*estrlcpy(hostname, request - position + 1, sizeof(request - position + 1));*/
|
|
|
|
estrlcpy(file, request+position+1, sizeof(request+position+1));
|
|
|
|
request[position +1] = '\0'; /* this is faster than strlcpy above*/
|
|
|
|
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2020-12-02 23:32:39 +11:00
|
|
|
/*
|
|
|
|
* use a default file if no file are requested this
|
|
|
|
* can happen in two cases gemini://hostname/
|
|
|
|
* gemini://hostname/directory/
|
|
|
|
*/
|
|
|
|
if (strlen(file) == 0)
|
2020-12-10 07:08:09 +11:00
|
|
|
estrlcpy(file, "/index.gmi", 11);
|
2020-12-02 23:32:39 +11:00
|
|
|
if (file[strlen(file) - 1] == '/')
|
|
|
|
strlcat(file, "index.gmi", sizeof(file));
|
2020-12-02 09:39:05 +11:00
|
|
|
|
|
|
|
} else {
|
2020-12-05 04:55:31 +11:00
|
|
|
syslog(LOG_DAEMON, "unknown situation after parsing query");
|
2020-12-02 09:39:05 +11:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* there are no slash / in the request
|
|
|
|
* -2 to remove \r\n
|
|
|
|
*/
|
2020-12-10 07:08:09 +11:00
|
|
|
estrlcpy(hostname, request, sizeof(hostname));
|
|
|
|
estrlcpy(file, "/index.gmi", 11);
|
2020-12-02 09:39:05 +11:00
|
|
|
}
|
|
|
|
|
2020-12-03 01:31:21 +11:00
|
|
|
/*
|
|
|
|
* if virtualhost feature is actived looking under the default path +
|
|
|
|
* hostname directory gemini://foobar/hello will look for
|
|
|
|
* path/foobar/hello
|
|
|
|
*/
|
|
|
|
if (virtualhost) {
|
|
|
|
strlcat(path, hostname, sizeof(path));
|
|
|
|
strlcat(path, "/", sizeof(path));
|
|
|
|
}
|
2020-12-02 09:39:05 +11:00
|
|
|
/* add the base dir to the file requested */
|
|
|
|
strlcat(path, file, sizeof(path));
|
|
|
|
|
|
|
|
/* open file and send it to stdout */
|
2020-12-03 04:07:10 +11:00
|
|
|
display_file(path, lang);
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2020-12-02 23:32:39 +11:00
|
|
|
return (0);
|
2020-12-02 09:39:05 +11:00
|
|
|
}
|