2020-12-04 08:59:39 +11:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pwd.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-04 08:59:39 +11:00
|
|
|
#include <sys/stat.h>
|
2020-12-02 09:39:05 +11:00
|
|
|
#include <unistd.h>
|
2020-12-03 05:59:52 +11:00
|
|
|
#include "mimes.c"
|
2020-12-02 09:39:05 +11:00
|
|
|
|
|
|
|
#define BUFF_LEN_1 1000
|
|
|
|
#define BUFF_LEN_2 1025
|
|
|
|
#define BUFF_LEN_3 1024
|
|
|
|
#define GEMINI_PART 9
|
2020-12-03 04:07:10 +11:00
|
|
|
#define DEFAULT_LANG "en"
|
|
|
|
#define DEFAULT_CHROOT "/var/gemini/"
|
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 *);
|
|
|
|
void get_file_mime(const char *, char *, const ssize_t);
|
2020-12-02 23:32:39 +11:00
|
|
|
int main (int, char **);
|
2020-12-02 09:39:05 +11:00
|
|
|
|
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
|
|
|
status(const int code, const char *file_mime, const char *lang)
|
2020-12-02 09:39:05 +11:00
|
|
|
{
|
2020-12-03 05:59:52 +11:00
|
|
|
printf("%i %s; lang=%s\r\n",
|
|
|
|
code, file_mime, lang);
|
2020-12-02 09:39:05 +11:00
|
|
|
}
|
|
|
|
|
2020-12-03 05:59:52 +11:00
|
|
|
void
|
|
|
|
get_file_mime(const char *path, char *type, const ssize_t type_size)
|
|
|
|
{
|
|
|
|
char *extension;
|
|
|
|
|
|
|
|
extension = strrchr(path, '.');
|
|
|
|
|
|
|
|
/* look for the MIME in the database */
|
2020-12-04 09:03:08 +11:00
|
|
|
for (int i = 0; i < sizeof(database) / sizeof(struct mimes); i++) {
|
|
|
|
if (strcmp(database[i].extension, extension + 1) == 0) {
|
|
|
|
strlcpy(type, database[i].type, type_size);
|
2020-12-03 05:59:52 +11:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if no MIME have been found, set a default one */
|
|
|
|
if (strlen(type) == 0)
|
|
|
|
strlcpy(type, "text/gemini", type_size);
|
|
|
|
}
|
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-02 23:32:39 +11:00
|
|
|
size_t buflen = BUFF_LEN_1;
|
|
|
|
char *buffer[BUFF_LEN_1];
|
2020-12-03 05:59:52 +11:00
|
|
|
char extension[10];
|
|
|
|
char file_mime[50];
|
2020-12-02 23:32:39 +11:00
|
|
|
ssize_t nread;
|
|
|
|
struct stat sb;
|
2020-12-03 05:59:52 +11:00
|
|
|
FILE *fd;
|
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 */
|
|
|
|
stat(path, &sb);
|
2020-12-02 09:39:05 +11:00
|
|
|
|
2020-12-03 05:59:52 +11:00
|
|
|
/* open the file requested */
|
|
|
|
fd = fopen(path, "r");
|
2020-12-02 09:39:05 +11:00
|
|
|
|
|
|
|
if (fd != NULL && S_ISDIR(sb.st_mode) != 1) {
|
|
|
|
|
2020-12-03 05:59:52 +11:00
|
|
|
get_file_mime(path, file_mime, sizeof(file_mime));
|
|
|
|
|
2020-12-02 23:32:39 +11:00
|
|
|
/* check if directory */
|
2020-12-03 05:59:52 +11:00
|
|
|
status(20, file_mime, lang);
|
2020-12-02 09:39:05 +11:00
|
|
|
|
|
|
|
/* read the file and write it to stdout */
|
|
|
|
while ((nread = fread(buffer, sizeof(char), buflen, fd)) != 0)
|
|
|
|
fwrite(buffer, sizeof(char), nread, stdout);
|
|
|
|
fclose(fd);
|
|
|
|
} else {
|
2020-12-04 09:03:08 +11:00
|
|
|
/* return an error code and no content */
|
2020-12-03 05:59:52 +11:00
|
|
|
status(40, "text/gemini", lang);
|
2020-12-02 09:39:05 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2020-12-02 23:32:39 +11:00
|
|
|
char buffer [BUFF_LEN_2];
|
|
|
|
char request [BUFF_LEN_2];
|
|
|
|
char hostname [BUFF_LEN_2];
|
|
|
|
char file [BUFF_LEN_2];
|
2020-12-03 05:59:52 +11:00
|
|
|
char path [BUFF_LEN_2] = DEFAULT_CHROOT;
|
|
|
|
char lang [3] = DEFAULT_LANG;
|
2020-12-04 08:59:39 +11:00
|
|
|
char user [_SC_LOGIN_NAME_MAX];
|
2020-12-03 01:31:21 +11:00
|
|
|
int virtualhost = 0;
|
2020-12-02 23:32:39 +11:00
|
|
|
int option;
|
2020-12-03 01:31:21 +11:00
|
|
|
int start_with_gemini;
|
2020-12-04 09:03:08 +11:00
|
|
|
struct passwd *pw;
|
2020-12-02 23:32:39 +11:00
|
|
|
char *pos;
|
|
|
|
|
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':
|
|
|
|
strlcpy(path, optarg, sizeof(path));
|
|
|
|
break;
|
2020-12-03 01:31:21 +11:00
|
|
|
case 'v':
|
|
|
|
virtualhost = 1;
|
|
|
|
break;
|
2020-12-03 04:07:10 +11:00
|
|
|
case 'l':
|
|
|
|
strlcpy(lang, optarg, sizeof(lang));
|
|
|
|
break;
|
2020-12-04 08:59:39 +11:00
|
|
|
case 'u':
|
|
|
|
strlcpy(user, optarg, sizeof(user));
|
|
|
|
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
|
|
|
/*
|
|
|
|
* 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) {
|
|
|
|
/* is root? */
|
|
|
|
if (getuid() != 0)
|
|
|
|
err(1, "chroot requires root user");
|
|
|
|
|
|
|
|
/* search user uid from name */
|
|
|
|
if ((pw = getpwnam(user)) == NULL)
|
|
|
|
err(1, "finding user");
|
|
|
|
|
|
|
|
/* chroot worked? */
|
|
|
|
if (chroot(path) != 0)
|
|
|
|
err(1, "chroot");
|
|
|
|
|
|
|
|
/* drop privileges */
|
|
|
|
if (setuid(pw->pw_uid) != 0)
|
|
|
|
err(1, "Can't drop privileges");
|
|
|
|
}
|
2020-12-02 09:41:55 +11:00
|
|
|
#ifdef __OpenBSD__
|
2020-12-03 05:59:52 +11:00
|
|
|
/*
|
|
|
|
* prevent access to files other than the one in path
|
|
|
|
*/
|
2020-12-02 09:39:05 +11:00
|
|
|
if (unveil(path, "r") == -1)
|
|
|
|
err(1, "unveil");
|
|
|
|
|
2020-12-03 05:59:52 +11:00
|
|
|
/*
|
|
|
|
* prevent system calls other than requirement for fread file and
|
|
|
|
* write to stdio
|
|
|
|
*/
|
2020-12-02 09:39:05 +11:00
|
|
|
if (pledge("stdio rpath", NULL) == -1)
|
|
|
|
err(1, "pledge");
|
2020-12-02 09:41:55 +11:00
|
|
|
#endif
|
2020-12-02 09:39:05 +11:00
|
|
|
|
|
|
|
/*
|
|
|
|
* read 1024 chars from stdin
|
|
|
|
* to get the request
|
|
|
|
*/
|
|
|
|
fgets(request, BUFF_LEN_3, stdin);
|
|
|
|
|
|
|
|
/* remove \r\n at the end of string */
|
|
|
|
pos = strchr(request, '\r');
|
2020-12-02 23:32:39 +11:00
|
|
|
if (pos != NULL)
|
|
|
|
strlcpy(pos, "\0", 1);
|
2020-12-02 09:39:05 +11:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check if the beginning of the request starts with
|
|
|
|
* gemini://
|
|
|
|
*/
|
2020-12-03 01:31:21 +11:00
|
|
|
start_with_gemini = strncmp(request, "gemini://", 9);
|
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 */
|
|
|
|
printf("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);
|
|
|
|
}
|
|
|
|
/* remove the gemini:// part */
|
|
|
|
strlcpy(buffer, request + GEMINI_PART, sizeof(buffer) - GEMINI_PART);
|
|
|
|
strlcpy(request, buffer, sizeof(request));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-02 09:39:05 +11:00
|
|
|
for (int i = 0; i < sizeof(request); i++) {
|
|
|
|
if (*pos == request[i]) {
|
|
|
|
position = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* separate hostname and uri */
|
|
|
|
if (position != -1) {
|
2020-12-03 01:31:21 +11:00
|
|
|
strlcpy(hostname, request, position + 1);
|
2020-12-02 09:39:05 +11:00
|
|
|
strlcpy(file, request + position + 1, sizeof(request));
|
|
|
|
|
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-02 09:39:05 +11:00
|
|
|
strlcpy(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 {
|
|
|
|
puts("error undefined");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* there are no slash / in the request
|
|
|
|
* -2 to remove \r\n
|
|
|
|
*/
|
|
|
|
strlcpy(hostname, request, sizeof(hostname));
|
|
|
|
strlcpy(file, "/index.gmi", 11);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|