2021-01-10 19:30:35 +11:00
|
|
|
#include <sys/types.h>
|
2021-01-14 23:31:51 +11:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
2020-12-05 05:31:13 +11:00
|
|
|
|
2021-01-10 19:30:35 +11:00
|
|
|
#include <dirent.h>
|
2020-12-04 08:59:39 +11:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2021-01-14 23:31:51 +11:00
|
|
|
#include <fcntl.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"
|
2021-01-10 19:30:35 +11:00
|
|
|
#include "opts.h"
|
2021-01-14 23:31:51 +11:00
|
|
|
#include "utils.h"
|
2020-12-02 09:39:05 +11:00
|
|
|
|
|
|
|
#define GEMINI_PART 9
|
2020-12-10 07:08:09 +11:00
|
|
|
#define GEMINI_REQUEST_MAX 1024 /* see https://gemini.circumlunar.space/docs/specification.html */
|
|
|
|
|
2020-12-02 09:39:05 +11:00
|
|
|
|
|
|
|
|
2021-01-10 19:30:35 +11:00
|
|
|
void autoindex(const char *);
|
2021-01-14 23:31:51 +11:00
|
|
|
void cgi(const char *cgicmd);
|
2021-01-10 19:30:35 +11:00
|
|
|
void display_file(const char *);
|
|
|
|
void status(const int, const char *);
|
|
|
|
void status_redirect(const int, const char *);
|
2020-12-05 05:08:36 +11:00
|
|
|
void drop_privileges(const char *, const char *);
|
2020-12-10 07:12:25 +11:00
|
|
|
|
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) {
|
2021-01-14 23:31:51 +11:00
|
|
|
errlog("chroot requires program to be run as root");
|
2020-12-05 05:08:36 +11:00
|
|
|
}
|
|
|
|
/* search user uid from name */
|
|
|
|
if ((pw = getpwnam(user)) == NULL) {
|
2021-01-14 23:31:51 +11:00
|
|
|
errlog("the user %s can't be found on the system", user);
|
2020-12-05 05:08:36 +11:00
|
|
|
}
|
|
|
|
/* chroot worked? */
|
2020-12-06 21:37:32 +11:00
|
|
|
if (chroot(path) != 0) {
|
2021-01-14 23:31:51 +11:00
|
|
|
errlog("the chroot_dir %s can't be used for chroot", path);
|
2020-12-05 05:08:36 +11:00
|
|
|
}
|
2020-12-10 00:20:10 +11:00
|
|
|
chrooted = 1;
|
2020-12-05 05:39:16 +11:00
|
|
|
if (chdir("/") == -1) {
|
2021-01-14 23:31:51 +11:00
|
|
|
errlog("failed to chdir(\"/\")");
|
2020-12-05 05:39:16 +11:00
|
|
|
}
|
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)) {
|
2021-01-14 23:31:51 +11:00
|
|
|
errlog("dropping privileges to user %s (uid=%i) failed",
|
2020-12-05 05:08:36 +11:00
|
|
|
user, pw->pw_uid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#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
|
|
|
}
|
2021-02-01 08:05:48 +11:00
|
|
|
/* permission to execute what's inside cgipath */
|
2021-01-14 23:31:51 +11:00
|
|
|
if (strlen(cgibin) > 0) {
|
2021-02-01 08:05:48 +11:00
|
|
|
/* first, build the full path of cgi (not in chroot) */
|
2021-01-14 23:31:51 +11:00
|
|
|
char cgifullpath[PATH_MAX] = {'\0'};
|
|
|
|
estrlcpy(cgifullpath, path, sizeof(cgifullpath));
|
|
|
|
estrlcat(cgifullpath, cgibin, sizeof(cgifullpath));
|
2021-02-01 08:05:48 +11:00
|
|
|
|
2021-01-14 23:31:51 +11:00
|
|
|
eunveil(cgifullpath, "rx");
|
|
|
|
}
|
2021-02-01 08:05:48 +11:00
|
|
|
/* forbid more unveil */
|
2021-02-01 07:21:15 +11:00
|
|
|
eunveil(NULL, NULL);
|
|
|
|
|
2020-12-05 05:08:36 +11:00
|
|
|
/*
|
|
|
|
* prevent system calls other parsing queryfor fread file and
|
|
|
|
* write to stdio
|
|
|
|
*/
|
2021-01-14 23:31:51 +11:00
|
|
|
if (strlen(cgibin) > 0) {
|
2021-02-01 08:05:48 +11:00
|
|
|
/* cgi need execlp() (exec) and fork() (proc) */
|
2021-01-14 23:31:51 +11:00
|
|
|
epledge("stdio rpath exec proc", NULL);
|
|
|
|
} else {
|
|
|
|
epledge("stdio rpath", NULL);
|
2020-12-05 05:08:36 +11:00
|
|
|
}
|
|
|
|
#endif
|
2020-12-02 09:39:05 +11:00
|
|
|
}
|
|
|
|
|
2020-12-03 05:59:52 +11:00
|
|
|
void
|
2021-01-10 19:30:35 +11:00
|
|
|
status(const int code, const char *file_mime)
|
2020-12-03 05:59:52 +11:00
|
|
|
{
|
2021-01-10 19:30:35 +11:00
|
|
|
printf("%i %s; %s\r\n",
|
2020-12-05 05:08:36 +11:00
|
|
|
code, file_mime, lang);
|
2020-12-03 05:59:52 +11:00
|
|
|
}
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2021-01-02 07:00:40 +11:00
|
|
|
void
|
|
|
|
status_redirect(const int code, const char *url)
|
|
|
|
{
|
|
|
|
printf("%i %s\r\n",
|
|
|
|
code, url);
|
|
|
|
}
|
|
|
|
|
2020-12-02 09:39:05 +11:00
|
|
|
void
|
2021-01-10 19:30:35 +11:00
|
|
|
display_file(const char *uri)
|
2020-12-02 09:39:05 +11:00
|
|
|
{
|
2020-12-10 22:49:01 +11:00
|
|
|
FILE *fd = NULL;
|
|
|
|
struct stat sb = {0};
|
|
|
|
ssize_t nread = 0;
|
2020-12-06 02:58:26 +11:00
|
|
|
const char *file_mime;
|
2021-01-10 19:30:35 +11:00
|
|
|
char *buffer[BUFSIZ];
|
|
|
|
char target[FILENAME_MAX] = {'\0'};
|
|
|
|
char fp[PATH_MAX] = {'\0'};
|
2021-02-01 08:05:48 +11:00
|
|
|
char tmp[PATH_MAX] = {'\0'}; /* used to build temporary path */
|
2021-01-10 19:30:35 +11:00
|
|
|
|
|
|
|
/* build file path inside chroot */
|
|
|
|
estrlcpy(fp, chroot_dir, sizeof(fp));
|
|
|
|
estrlcat(fp, uri, sizeof(fp));
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2021-01-02 07:00:40 +11:00
|
|
|
/* this is to check if path exists and obtain metadata later */
|
2021-01-10 19:30:35 +11:00
|
|
|
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-02 07:00:40 +11:00
|
|
|
}
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2020-12-05 05:43:29 +11:00
|
|
|
/* check if directory */
|
2021-01-07 07:37:38 +11:00
|
|
|
if (S_ISDIR(sb.st_mode) != 0) {
|
2021-01-10 19:30:35 +11:00
|
|
|
if (fp[strlen(fp) -1 ] != '/') {
|
|
|
|
/* no ending "/", redirect to "path/" */
|
2021-02-01 08:05:48 +11:00
|
|
|
estrlcpy(tmp, uri, sizeof(tmp));
|
|
|
|
estrlcat(tmp, "/", sizeof(tmp));
|
|
|
|
status_redirect(31, tmp);
|
2021-01-10 19:30:35 +11:00
|
|
|
return;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* there is a leading "/", display index.gmi */
|
2021-02-01 08:05:48 +11:00
|
|
|
estrlcpy(tmp, fp, sizeof(tmp));
|
|
|
|
estrlcat(tmp, "index.gmi", sizeof(tmp));
|
2021-01-10 19:30:35 +11:00
|
|
|
|
|
|
|
/* check if index.gmi exists or show autoindex */
|
2021-02-01 08:05:48 +11:00
|
|
|
if (stat(tmp, &sb) == 0) {
|
|
|
|
estrlcpy(fp, tmp, sizeof(fp));
|
2021-01-10 19:30:35 +11:00
|
|
|
} else if (doautoidx != 0) {
|
|
|
|
autoindex(fp);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2021-01-02 07:00:40 +11:00
|
|
|
/* open the file requested */
|
2021-01-10 19:30:35 +11:00
|
|
|
if ((fd = fopen(fp, "r")) == NULL) { goto err; }
|
2021-01-02 07:00:40 +11:00
|
|
|
|
2021-01-10 19:30:35 +11:00
|
|
|
file_mime = get_file_mime(fp, default_mime);
|
2020-12-03 05:59:52 +11:00
|
|
|
|
2021-01-10 19:30:35 +11:00
|
|
|
status(20, file_mime);
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2021-02-01 08:05:48 +11:00
|
|
|
/* read the file byte after byte in buffer and write it to stdout */
|
2021-01-14 23:31:51 +11:00
|
|
|
while ((nread = fread(buffer, 1, sizeof(buffer), fd)) != 0)
|
|
|
|
fwrite(buffer, 1, nread, stdout);
|
2021-01-10 19:30:35 +11:00
|
|
|
goto closefd;
|
|
|
|
syslog(LOG_DAEMON, "path served %s", fp);
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2020-12-05 05:43:29 +11:00
|
|
|
return;
|
2021-01-07 07:37:38 +11:00
|
|
|
|
2020-12-06 00:44:59 +11:00
|
|
|
err:
|
2020-12-05 05:43:29 +11:00
|
|
|
/* return an error code and no content */
|
2021-01-10 19:30:35 +11:00
|
|
|
status(51, "text/gemini");
|
|
|
|
syslog(LOG_DAEMON, "path invalid %s", fp);
|
2020-12-10 22:49:01 +11:00
|
|
|
goto closefd;
|
|
|
|
|
2021-01-02 07:00:40 +11:00
|
|
|
redirect:
|
2021-01-10 19:30:35 +11:00
|
|
|
/* read symbolic link target to redirect */
|
|
|
|
if (readlink(fp, target, FILENAME_MAX) == -1) {
|
|
|
|
goto err;
|
2021-01-02 07:00:40 +11:00
|
|
|
}
|
|
|
|
|
2021-01-10 19:30:35 +11:00
|
|
|
status_redirect(30, target);
|
|
|
|
syslog(LOG_DAEMON, "redirection from %s to %s", fp, target);
|
2021-01-02 07:00:40 +11:00
|
|
|
|
2020-12-10 22:49:01 +11:00
|
|
|
closefd:
|
2021-01-10 19:30:35 +11:00
|
|
|
if (S_ISREG(sb.st_mode) != 0) {
|
|
|
|
fclose(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
autoindex(const char *path)
|
|
|
|
{
|
2021-01-15 00:30:11 +11:00
|
|
|
int n = 0;
|
2021-02-01 08:05:48 +11:00
|
|
|
char *pos = NULL;
|
|
|
|
struct dirent **namelist; /* this must be freed at last */
|
2021-01-10 19:30:35 +11:00
|
|
|
|
|
|
|
|
|
|
|
syslog(LOG_DAEMON, "autoindex: %s", path);
|
|
|
|
|
|
|
|
status(20, "text/gemini");
|
|
|
|
|
2021-02-01 08:05:48 +11:00
|
|
|
/* display link to parent */
|
|
|
|
char parent[PATH_MAX] = {'\0'};
|
|
|
|
/* parent is "path" without chroot_dir */
|
|
|
|
estrlcpy(parent, path+strlen(chroot_dir), sizeof(parent));
|
|
|
|
/* remove ending '/' */
|
|
|
|
while (parent[strlen(parent)-1] == '/') {
|
|
|
|
parent[strlen(parent)-1] = '\0';
|
|
|
|
}
|
|
|
|
/* remove last part after '/' */
|
|
|
|
pos = strrchr(parent, '/');
|
|
|
|
if (pos != NULL) {
|
|
|
|
pos[1] = '\0'; /* at worse, parent is now "/" */
|
|
|
|
}
|
|
|
|
printf("=> %s ../\n", parent);
|
|
|
|
|
|
|
|
/* use alphasort to always have the same order on every system */
|
2021-01-15 00:30:11 +11:00
|
|
|
if ((n = scandir(path, &namelist, NULL, alphasort)) < 0) {
|
|
|
|
status(51, "text/gemini");
|
|
|
|
errlog("Can't scan %s", path);
|
|
|
|
} else {
|
|
|
|
for(int j = 0; j < n; j++) {
|
2021-02-01 08:05:48 +11:00
|
|
|
/* skip self and parent */
|
2021-01-31 23:59:24 +11:00
|
|
|
if ((strcmp(namelist[j]->d_name, ".") == 0) ||
|
|
|
|
(strcmp(namelist[j]->d_name, "..") == 0)) {
|
2021-01-15 00:30:11 +11:00
|
|
|
continue;
|
|
|
|
}
|
2021-02-01 08:05:48 +11:00
|
|
|
/* add "/" at the end of a directory path */
|
2021-01-15 00:30:11 +11:00
|
|
|
if (namelist[j]->d_type == DT_DIR) {
|
|
|
|
printf("=> ./%s/ %s/\n", namelist[j]->d_name, namelist[j]->d_name);
|
|
|
|
} else {
|
|
|
|
printf("=> ./%s %s\n", namelist[j]->d_name, namelist[j]->d_name);
|
|
|
|
}
|
|
|
|
free(namelist[j]);
|
2021-01-10 19:30:35 +11:00
|
|
|
}
|
2021-01-15 00:30:11 +11:00
|
|
|
free(namelist);
|
2021-01-10 19:30:35 +11:00
|
|
|
}
|
2020-12-02 09:39:05 +11:00
|
|
|
}
|
|
|
|
|
2021-01-14 23:31:51 +11:00
|
|
|
void
|
|
|
|
cgi(const char *cgicmd)
|
|
|
|
{
|
|
|
|
|
|
|
|
int pipedes[2] = {0};
|
|
|
|
pid_t pid;
|
|
|
|
|
2021-02-01 08:05:48 +11:00
|
|
|
/* get a pipe to get stdout */
|
2021-01-14 23:31:51 +11:00
|
|
|
if (pipe(pipedes) != 0) {
|
2021-01-15 00:30:11 +11:00
|
|
|
status(42, "text/gemini");
|
2021-01-14 23:31:51 +11:00
|
|
|
err(1, "pipe failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
|
|
|
|
if (pid < 0) {
|
|
|
|
close(pipedes[0]);
|
|
|
|
close(pipedes[1]);
|
2021-01-15 00:30:11 +11:00
|
|
|
status(42, "text/gemini");
|
2021-01-14 23:31:51 +11:00
|
|
|
err(1, "fork failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid > 0) { /* parent */
|
|
|
|
char buf[3];
|
|
|
|
size_t nread = 0;
|
|
|
|
FILE *output = NULL;
|
|
|
|
|
|
|
|
close(pipedes[1]); /* make sure entry is closed so fread() gets EOF */
|
|
|
|
|
|
|
|
/* use fread/fwrite because are buffered */
|
|
|
|
output = fdopen(pipedes[0], "r");
|
|
|
|
if (output == NULL) {
|
2021-01-15 00:30:11 +11:00
|
|
|
status(42, "text/gemini");
|
2021-01-14 23:31:51 +11:00
|
|
|
err(1, "fdopen failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read pipe output */
|
|
|
|
while ((nread = fread(buf, 1, sizeof(buf), output)) != 0) {
|
|
|
|
fwrite(buf, 1, nread, stdout);
|
|
|
|
}
|
|
|
|
close(pipedes[0]);
|
|
|
|
fclose(output);
|
|
|
|
|
|
|
|
wait(NULL); /* wait for child to terminate */
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
} else if (pid == 0) { /* child */
|
|
|
|
dup2(pipedes[1], STDOUT_FILENO); /* set pipe output equal to stdout */
|
|
|
|
close(pipedes[1]); /* no need this file descriptor : it is now stdout */
|
|
|
|
execlp(cgicmd, cgicmd, NULL);
|
|
|
|
/* if execlp is ok, this will never be reached */
|
2021-01-15 00:30:11 +11:00
|
|
|
status(42, "text/gemini");
|
2021-01-14 23:31:51 +11:00
|
|
|
errlog("error when trying to execlp %s", cgicmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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'};
|
2021-01-10 19:30:35 +11:00
|
|
|
char uri [PATH_MAX] = {'\0'};
|
2020-12-06 00:44:59 +11:00
|
|
|
char user [_SC_LOGIN_NAME_MAX] = "";
|
2021-02-01 07:21:15 +11:00
|
|
|
char query[PATH_MAX] = {'\0'};
|
2020-12-03 01:31:21 +11:00
|
|
|
int virtualhost = 0;
|
2020-12-09 07:48:35 +11:00
|
|
|
int option = 0;
|
2020-12-09 07:48:21 +11:00
|
|
|
char *pos = NULL;
|
2020-12-02 23:32:39 +11:00
|
|
|
|
2021-01-14 23:31:51 +11:00
|
|
|
while ((option = getopt(argc, argv, ":d:l:m:u:c:vi")) != -1) {
|
2020-12-02 23:32:39 +11:00
|
|
|
switch (option) {
|
|
|
|
case 'd':
|
2021-01-10 19:30:35 +11:00
|
|
|
estrlcpy(chroot_dir, optarg, sizeof(chroot_dir));
|
2020-12-03 01:31:21 +11:00
|
|
|
break;
|
2020-12-03 04:07:10 +11:00
|
|
|
case 'l':
|
2021-01-10 19:30:35 +11:00
|
|
|
estrlcpy(lang, "lang=", sizeof(lang));
|
|
|
|
estrlcat(lang, optarg, sizeof(lang));
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
estrlcpy(default_mime, optarg, sizeof(default_mime));
|
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));
|
2021-01-10 19:30:35 +11:00
|
|
|
break;
|
2021-01-14 23:31:51 +11:00
|
|
|
case 'c':
|
|
|
|
estrlcpy(cgibin, optarg, sizeof(cgibin));
|
|
|
|
break;
|
2021-01-10 19:30:35 +11:00
|
|
|
case 'v':
|
|
|
|
virtualhost = 1;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
doautoidx = 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
|
|
|
*/
|
2021-01-10 19:30:35 +11:00
|
|
|
drop_privileges(user, chroot_dir);
|
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
|
|
|
|
*/
|
2021-01-10 19:30:35 +11:00
|
|
|
if (fgets(request, GEMINI_REQUEST_MAX, stdin) == NULL) {
|
|
|
|
status(59, "request is too long (1024 max)");
|
2021-01-14 23:31:51 +11:00
|
|
|
errlog("request is too long (1024 max): %s", request);
|
2020-12-10 07:08:09 +11:00
|
|
|
}
|
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-10 22:49:01 +11:00
|
|
|
if (strncmp(request, "gemini://", GEMINI_PART) != 0) {
|
2020-12-02 09:39:05 +11:00
|
|
|
/* error code url malformed */
|
2021-01-14 23:31:51 +11:00
|
|
|
errlog("request «%s» doesn't match gemini://",
|
2020-12-10 22:49:01 +11:00
|
|
|
request);
|
2020-12-02 09:39:05 +11:00
|
|
|
}
|
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 */
|
2021-01-31 23:59:24 +11:00
|
|
|
memmove(request, request + GEMINI_PART, strlen(request) +1 - GEMINI_PART);
|
|
|
|
|
|
|
|
/* remove all "/.." for safety reasons */
|
|
|
|
while ((pos = strstr(request, "/..")) != NULL ) {
|
|
|
|
memmove(request, pos+3, strlen(pos) +1 - 3); /* "/.." = 3 */
|
|
|
|
}
|
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 */
|
|
|
|
/* separate hostname and uri */
|
2021-01-10 19:30:35 +11:00
|
|
|
estrlcpy(uri, pos, strlen(pos)+1);
|
2020-12-10 22:49:01 +11:00
|
|
|
/* just keep hostname in request */
|
|
|
|
pos[0] = '\0';
|
2020-12-02 09:39:05 +11:00
|
|
|
}
|
2021-01-02 02:08:51 +11: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 */
|
2020-12-10 22:49:01 +11:00
|
|
|
estrlcpy(hostname, request, sizeof(hostname));
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2021-02-01 07:21:15 +11:00
|
|
|
/* look for "?" if any to set query for cgi, or remove it*/
|
|
|
|
pos = strchr(uri, '?');
|
|
|
|
if (pos != NULL) {
|
|
|
|
estrlcpy(query, pos+1, sizeof(query));
|
|
|
|
esetenv("QUERY_STRING", query, 1);
|
|
|
|
pos[0] = '\0';
|
|
|
|
}
|
|
|
|
|
2020-12-03 01:31:21 +11:00
|
|
|
/*
|
2021-01-10 19:30:35 +11:00
|
|
|
* if virtualhost feature is actived looking under the chroot_path +
|
2020-12-03 01:31:21 +11:00
|
|
|
* hostname directory gemini://foobar/hello will look for
|
2021-01-10 19:30:35 +11:00
|
|
|
* chroot_path/foobar/hello
|
2020-12-03 01:31:21 +11:00
|
|
|
*/
|
|
|
|
if (virtualhost) {
|
2021-01-10 19:30:35 +11:00
|
|
|
if (strlen(uri) == 0) {
|
|
|
|
estrlcpy(uri, "/index.gmi", sizeof(uri));
|
|
|
|
}
|
2021-02-01 07:21:15 +11:00
|
|
|
char tmp[PATH_MAX] = {'\0'};
|
|
|
|
estrlcpy(tmp, hostname, sizeof(tmp));
|
|
|
|
estrlcat(tmp, uri, sizeof(tmp));
|
|
|
|
estrlcpy(uri, tmp, sizeof(uri));
|
2020-12-03 01:31:21 +11:00
|
|
|
}
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2021-01-14 23:31:51 +11:00
|
|
|
/* check if uri is cgibin */
|
2021-01-31 23:59:24 +11:00
|
|
|
if ((strlen(cgibin) > 0) &&
|
|
|
|
(strncmp(uri, cgibin, strlen(cgibin)) == 0)) {
|
2021-02-01 07:21:15 +11:00
|
|
|
|
2021-02-01 08:05:48 +11:00
|
|
|
/* cgipath with chroot_dir at the beginning */
|
2021-01-14 23:31:51 +11:00
|
|
|
char cgipath[PATH_MAX] = {'\0'};
|
|
|
|
estrlcpy(cgipath, chroot_dir, sizeof(cgipath));
|
|
|
|
estrlcat(cgipath, uri, sizeof(cgipath));
|
|
|
|
/* set env variables for CGI */
|
|
|
|
/* see https://lists.orbitalfox.eu/archives/gemini/2020/000315.html */
|
|
|
|
esetenv("GATEWAY_INTERFACE", "CGI/1.1", 1);
|
|
|
|
esetenv("SERVER_PROTOCOL", "GEMINI", 1);
|
|
|
|
esetenv("SERVER_SOFTWARE", "vger/1", 1);
|
|
|
|
|
|
|
|
/* look for an extension to find PATH_INFO */
|
|
|
|
pos = strrchr(cgipath, '.');
|
|
|
|
if (pos != NULL) {
|
|
|
|
/* found a dot */
|
|
|
|
pos = strchr(pos, '/');
|
|
|
|
if (pos != NULL) {
|
|
|
|
setenv("PATH_INFO", pos, 1);
|
|
|
|
pos[0] = '\0'; /* keep only script name */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
esetenv("SCRIPT_NAME", cgipath, 1);
|
|
|
|
esetenv("SERVER_NAME", hostname, 1);
|
|
|
|
|
|
|
|
cgi(cgipath);
|
|
|
|
|
|
|
|
} else {
|
2021-01-31 23:59:24 +11:00
|
|
|
//TODO: percent decoding here
|
2021-01-14 23:31:51 +11:00
|
|
|
/* open file and send it to stdout */
|
|
|
|
display_file(uri);
|
|
|
|
}
|
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
|
|
|
}
|