vger/main.c

75 lines
1.8 KiB
C
Raw Normal View History

#include "vger.c"
2020-12-01 22:39:05 +00:00
int
main(int argc, char **argv)
{
2022-08-18 08:54:13 +00:00
char request[GEMINI_REQUEST_MAX] = {'\0'};
char user[_SC_LOGIN_NAME_MAX] = {'\0'};
char hostname[GEMINI_REQUEST_MAX] = {'\0'};
char path[GEMINI_REQUEST_MAX] = {'\0'};
char query[GEMINI_REQUEST_MAX] = {'\0'};
char chroot_dir[PATH_MAX] = DEFAULT_CHROOT;
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
2022-08-18 20:23:59 +00:00
* query : after a ? in cgi : gemini://...?query
*/
while ((option = getopt(argc, argv, ":d:l:m:u:c:vi")) != -1) {
switch (option) {
case 'd':
esnprintf(chroot_dir, sizeof(chroot_dir), "%s", optarg);
break;
case 'l':
esnprintf(lang, sizeof(lang), "lang=%s", optarg);
break;
case 'm':
esnprintf(default_mime, sizeof(default_mime), "%s", optarg);
break;
2020-12-03 21:59:39 +00:00
case 'u':
esnprintf(user, sizeof(user), "%s", optarg);
break;
case 'c':
esnprintf(cgi_dir, sizeof(cgi_dir), "%s", optarg);
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
2022-08-18 08:54:13 +00:00
read_request(request);
split_request(request, hostname, path, query);
2022-08-18 09:06:37 +00:00
set_path(path, sizeof(path), virtualhost, hostname);
/* 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);
2022-08-18 12:12:38 +00:00
/* *** from here, cgi didn't run *** */
2021-10-21 09:41:22 +00:00
2022-08-18 12:12:38 +00:00
/* check if path available */
check_path(path, sizeof(path), virtualhost, strlen(hostname));
/* regular file to stdout */
2022-08-18 20:23:59 +00:00
display_file(path);
stop(EXIT_SUCCESS, NULL);
2020-12-01 22:39:05 +00:00
}