=================================================================== RCS file: /cvs/mandoc/cgi.c,v retrieving revision 1.73 retrieving revision 1.78 diff -u -p -r1.73 -r1.78 --- mandoc/cgi.c 2014/07/13 15:38:36 1.73 +++ mandoc/cgi.c 2014/07/21 15:45:17 1.78 @@ -1,4 +1,4 @@ -/* $Id: cgi.c,v 1.73 2014/07/13 15:38:36 schwarze Exp $ */ +/* $Id: cgi.c,v 1.78 2014/07/21 15:45:17 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2014 Ingo Schwarze @@ -79,7 +79,6 @@ static void resp_searchform(const struct req *); static void resp_show(const struct req *, const char *); static const char *scriptname; /* CGI script name */ -static const char *httphost; /* hostname used in the URIs */ static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9}; static const char *const sec_numbers[] = { @@ -467,6 +466,32 @@ resp_searchform(const struct req *req) puts(""); } +static int +validate_manpath(const struct req *req, const char* manpath) +{ + size_t i; + + if ( ! strcmp(manpath, "mandoc")) + return(1); + + for (i = 0; i < req->psz; i++) + if ( ! strcmp(manpath, req->p[i])) + return(1); + + return(0); +} + +static int +validate_filename(const char *file) +{ + + if ('.' == file[0] && '/' == file[1]) + file += 2; + + return ( ! (strstr(file, "../") || strstr(file, "/..") || + (strncmp(file, "man", 3) && strncmp(file, "cat", 3)))); +} + static void pg_index(const struct req *req) { @@ -524,6 +549,15 @@ pg_searchres(const struct req *req, struct manpage *r, int prio, priouse; char sec; + for (i = 0; i < sz; i++) { + if (validate_filename(r[i].file)) + continue; + fprintf(stderr, "invalid filename %s in %s database\n", + r[i].file, req->q.manpath); + pg_error_internal(); + return; + } + if (1 == sz) { /* * If we have just one result, then jump there now @@ -531,7 +565,7 @@ pg_searchres(const struct req *req, struct manpage *r, */ printf("Status: 303 See Other\r\n"); printf("Location: http://%s%s/%s/%s?", - httphost, scriptname, req->q.manpath, r[0].file); + HTTP_HOST, scriptname, req->q.manpath, r[0].file); http_printquery(req); printf("\r\n" "Content-Type: text/html; charset=utf-8\r\n" @@ -778,7 +812,8 @@ format(const struct req *req, const char *file) static void resp_show(const struct req *req, const char *file) { - if ('.' == file[0] || '/' == file[1]) + + if ('.' == file[0] && '/' == file[1]) file += 2; if ('c' == *file) @@ -799,6 +834,12 @@ pg_show(const struct req *req, const char *path) } *sub++ = '\0'; + if ( ! validate_manpath(req, path)) { + pg_error_badrequest( + "You specified an invalid manpath."); + return; + } + /* * Begin by chdir()ing into the manpath. * This way we can pick up the database files, which are @@ -806,8 +847,15 @@ pg_show(const struct req *req, const char *path) */ if (-1 == chdir(path)) { + fprintf(stderr, "chdir %s: %s\n", + path, strerror(errno)); + pg_error_internal(); + return; + } + + if ( ! validate_filename(sub)) { pg_error_badrequest( - "You specified an invalid manpath."); + "You specified an invalid manual file."); return; } @@ -835,8 +883,9 @@ pg_search(const struct req *req) */ if (-1 == (chdir(req->q.manpath))) { - pg_error_badrequest( - "You specified an invalid manpath."); + fprintf(stderr, "chdir %s: %s\n", + req->q.manpath, strerror(errno)); + pg_error_internal(); return; } @@ -907,9 +956,6 @@ main(void) if (NULL == (scriptname = getenv("SCRIPT_NAME"))) scriptname = ""; - if (NULL == (httphost = getenv("HTTP_HOST"))) - httphost = "localhost"; - /* * First we change directory into the MAN_DIR so that * subsequent scanning for manpath directories is rooted @@ -931,6 +977,12 @@ main(void) if (NULL != (querystring = getenv("QUERY_STRING"))) http_parse(&req, querystring); + if ( ! validate_manpath(&req, req.q.manpath)) { + pg_error_badrequest( + "You specified an invalid manpath."); + return(EXIT_FAILURE); + } + /* Dispatch to the three different pages. */ path = getenv("PATH_INFO"); @@ -970,8 +1022,12 @@ pathgen(struct req *req) char *dp; size_t dpsz; - if (NULL == (fp = fopen("manpath.conf", "r"))) - return; + if (NULL == (fp = fopen("manpath.conf", "r"))) { + fprintf(stderr, "%s/manpath.conf: %s\n", + MAN_DIR, strerror(errno)); + pg_error_internal(); + exit(EXIT_FAILURE); + } while (NULL != (dp = fgetln(fp, &dpsz))) { if ('\n' == dp[dpsz - 1]) @@ -979,5 +1035,11 @@ pathgen(struct req *req) req->p = mandoc_realloc(req->p, (req->psz + 1) * sizeof(char *)); req->p[req->psz++] = mandoc_strndup(dp, dpsz); + } + + if ( req->p == NULL ) { + fprintf(stderr, "%s/manpath.conf is empty\n", MAN_DIR); + pg_error_internal(); + exit(EXIT_FAILURE); } }