vger/main.c

75 lines
1.8 KiB
C

#include "vger.c"
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 path[GEMINI_REQUEST_MAX] = {'\0'};
char query[GEMINI_REQUEST_MAX] = {'\0'};
char chroot_dir[PATH_MAX] = DEFAULT_CHROOT;
int option = 0;
int virtualhost = 0;
/*
* 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 : 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;
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;
break;
}
}
/*
* do chroot if an user is supplied
*/
drop_privileges(user, chroot_dir, cgi_dir);
read_request(request);
split_request(request, hostname, path, query);
set_path(path, sizeof(path), virtualhost, hostname);
/* percent decode */
uridecode(query);
uridecode(path);
/* is it cgi ? */
if (*cgi_dir)
if (do_cgi(chroot_dir, cgi_dir, path, hostname, query) == 0)
stop(EXIT_SUCCESS, NULL);
/* *** from here, cgi didn't run *** */
/* check if path available */
check_path(path, sizeof(path), virtualhost, strlen(hostname));
/* regular file to stdout */
display_file(path);
stop(EXIT_SUCCESS, NULL);
}