vger/main.c

89 lines
2.3 KiB
C
Raw Normal View History

#include "vger.c"
2020-12-01 22:39:05 +00:00
int
main(int argc, char **argv)
{
char request[GEMINI_REQUEST_MAX] = {'\0'};
char user[_SC_LOGIN_NAME_MAX] = {'\0'};
char hostname[GEMINI_REQUEST_MAX] = {'\0'};
char query[PATH_MAX] = {'\0'};
char path[PATH_MAX] = {'\0'};
char chroot_dir[PATH_MAX] = DEFAULT_CHROOT;
char file[FILENAME_MAX] = DEFAULT_INDEX;
char dir[PATH_MAX] = {'\0'};
int option = 0;
int virtualhost = 0;
/*
2021-10-21 09:41:22 +00:00
* request : contain the whole request from client : gemini://...\r\n
* user : username, used in drop_privileges()
* hostname : extracted from hostname. used with virtualhosts and cgi SERVER_NAME
* query : file requested in cgi : gemini://...?query
* file : file basename to display. Emtpy is a directory has been requested
* dir : directory requested. vger will chdir() in to find file
* pos : used to parse request and split into interesting parts
*/
while ((option = getopt(argc, argv, ":d:l:m:u:c: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 'c':
estrlcpy(cgi_dir, optarg, sizeof(cgi_dir));
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
2020-12-03 21:59:39 +00:00
*/
drop_privileges(user, chroot_dir, cgi_dir);
2020-12-05 13:44:59 +00:00
check_request(request);
get_hostname(request, hostname, sizeof(hostname));
get_path(request, path, sizeof(path), virtualhost, hostname);
get_query(path, query, sizeof(query));
/* percent decode */
uridecode(query);
uridecode(path);
2021-10-21 09:41:22 +00:00
/* is it cgi ? */
if (*cgi_dir)
if (do_cgi(chroot_dir, cgi_dir, path, hostname, query) == 0)
stop(EXIT_SUCCESS, NULL);
2021-10-21 09:41:22 +00:00
/* *** from here, cgi didn't run ***
* check if path available
*/
check_path(path, sizeof(path), hostname, virtualhost);
/* split dir and filename */
get_dir_file(path, dir, sizeof(dir), file, sizeof(file));
/* go to dir */
echdir(dir);
/* regular file to stdout */
display_file(file);
stop(EXIT_SUCCESS, NULL);
2020-12-01 22:39:05 +00:00
}