From 27549119efcb50da8aa918ee07310f3a805b50ea Mon Sep 17 00:00:00 2001 From: prx Date: Thu, 18 Aug 2022 13:59:54 +0200 Subject: [PATCH] simplify and fix redirections after regex --- main.c | 9 +++++++-- vger.c | 24 ++++++++++-------------- vger.h | 2 +- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/main.c b/main.c index de26cef..fb2dc0b 100644 --- a/main.c +++ b/main.c @@ -58,6 +58,7 @@ main(int argc, char **argv) read_request(request); split_request(request, hostname, path, query); set_path(path, sizeof(path), virtualhost, hostname); + //get_query(path, query, sizeof(query)); /* percent decode */ @@ -69,16 +70,20 @@ main(int argc, char **argv) if (do_cgi(chroot_dir, cgi_dir, path, hostname, query) == 0) stop(EXIT_SUCCESS, NULL); + syslog(LOG_DAEMON, "path:%s", path); /* *** from here, cgi didn't run *** * check if path available */ - check_path(path, sizeof(path), hostname, virtualhost); + check_path(path, sizeof(path), virtualhost, strlen(hostname)); /* split dir and filename */ split_dir_file(path, dir, sizeof(dir), file, sizeof(file)); + syslog(LOG_DAEMON, "dir:%s", dir); + syslog(LOG_DAEMON, "file:%s", file); /* go to dir */ - echdir(dir); + if (*dir) + echdir(dir); /* regular file to stdout */ display_file(file); diff --git a/vger.c b/vger.c index 0142091..79a842c 100644 --- a/vger.c +++ b/vger.c @@ -384,6 +384,9 @@ set_path(char *path, size_t pathsiz, int virtualhost, const char *hostname) { char tmp[GEMINI_REQUEST_MAX] = {'\0'}; + if (strlen(path) == 0) /* this is root dir */ + estrlcpy(path, "./", pathsiz); + /* path is in a subdir named hostname */ if (virtualhost) { estrlcpy(tmp, hostname, sizeof(tmp)); @@ -396,7 +399,7 @@ set_path(char *path, size_t pathsiz, int virtualhost, const char *hostname) } void -check_path(char *path, size_t pathsiz, const char *hstnm, int virtualhost) +check_path(char *path, size_t pathsiz, int virtualhost, size_t hstnm_o) { struct stat sb = {0}; char tmp[PATH_MAX] = {'\0'}; @@ -416,17 +419,13 @@ check_path(char *path, size_t pathsiz, const char *hstnm, int virtualhost) /* check if dir path end with "/" */ if (path[strlen(path) - 1] != '/') { /* redirect to the dir with appropriate ending '/' */ - - /* remove leading '.' for redirection*/ - if (virtualhost) /* remove ./host.name */ - memmove(path, path+2+strlen(hstnm), - strlen(path + 2) + strlen(hstnm) + 1); + estrlcpy(tmp, "/", sizeof(tmp)); + if (virtualhost) /* skip hostname */ + estrlcat(tmp, path+hstnm_o+1, sizeof(tmp)); else - memmove(path, path+1, - strlen(path + 1) + 1); /* +1 for \0 */ - - estrlcat(path, "/", pathsiz); - status(31, "%s", path); + estrlcat(tmp, path, sizeof(tmp)); + estrlcat(tmp, "/", sizeof(tmp)); + status(31, "%s", tmp); stop(EXIT_SUCCESS, NULL); } /* check if DEFAULT_INDEX exists in directory */ @@ -502,9 +501,6 @@ split_request(const char *request, char *hostname, char *path, char *query) getsubexp(request, match[1], hostname); getsubexp(request, match[2], path); getsubexp(request, match[3], query); - syslog(LOG_DAEMON, "hostname:%s", hostname); - syslog(LOG_DAEMON, "path:%s", path); - syslog(LOG_DAEMON, "query:%s", query); regfree(&greg); } diff --git a/vger.h b/vger.h index c9e79bc..b9d71c0 100644 --- a/vger.h +++ b/vger.h @@ -42,7 +42,7 @@ static char _request[GEMINI_REQUEST_MAX] = {'\0'}; ssize_t autoindex(const char *); void cgi(const char *); char * read_request(char *); -void check_path(char *, size_t, const char *, int); +void check_path(char *, size_t, int, size_t); ssize_t display_file(const char *); int do_cgi(const char *, const char *, const char *, const char *, const char *); void drop_privileges(const char *, const char *, const char *);