=================================================================== RCS file: /cvs/mandoc/cgi.c,v retrieving revision 1.132 retrieving revision 1.136 diff -u -p -r1.132 -r1.136 --- mandoc/cgi.c 2016/05/28 13:40:48 1.132 +++ mandoc/cgi.c 2016/07/31 23:37:23 1.136 @@ -1,4 +1,4 @@ -/* $Id: cgi.c,v 1.132 2016/05/28 13:40:48 schwarze Exp $ */ +/* $Id: cgi.c,v 1.136 2016/07/31 23:37:23 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2014, 2015, 2016 Ingo Schwarze @@ -470,9 +470,6 @@ validate_manpath(const struct req *req, const char* ma { size_t i; - if ( ! strcmp(manpath, "mandoc")) - return 1; - for (i = 0; i < req->psz; i++) if ( ! strcmp(manpath, req->p[i])) return 1; @@ -499,9 +496,9 @@ pg_index(const struct req *req) resp_searchform(req, FOCUS_QUERY); printf("

\n" "This web interface is documented in the\n" - "man.cgi\n" + "man.cgi(8)\n" "manual, and the\n" - "apropos\n" + "apropos(1)\n" "manual explains the query syntax.\n" "

\n", scriptname, *scriptname == '\0' ? "" : "/", @@ -803,7 +800,8 @@ resp_format(const struct req *req, const char *file) } mchars_alloc(); - mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_BADARG, NULL, req->q.manpath); + mp = mparse_alloc(MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1, + MANDOCLEVEL_BADARG, NULL, req->q.manpath); mparse_readfd(mp, fd, file); close(fd); @@ -884,13 +882,8 @@ pg_show(struct req *req, const char *fullpath) free(manpath); return; } + free(manpath); - if (strcmp(manpath, "mandoc")) { - free(req->q.manpath); - req->q.manpath = manpath; - } else - free(manpath); - if ( ! validate_filename(file)) { pg_error_badrequest( "You specified an invalid manual file."); @@ -1026,7 +1019,7 @@ main(void) if (*path != '\0') { parse_path_info(&req, path); - if (access(path, F_OK) == -1) + if (req.q.manpath == NULL || access(path, F_OK) == -1) path = ""; } else if ((querystring = getenv("QUERY_STRING")) != NULL) parse_query_string(&req, querystring); @@ -1072,11 +1065,13 @@ main(void) static void parse_path_info(struct req *req, const char *path) { - char *dir; + char *dir[4]; + int i; req->isquery = 0; req->q.equal = 1; req->q.manpath = mandoc_strdup(path); + req->q.arch = NULL; /* Mandatory manual page name. */ if ((req->q.query = strrchr(req->q.manpath, '/')) == NULL) { @@ -1095,27 +1090,50 @@ parse_path_info(struct req *req, const char *path) } /* Handle the case of name[.section] only. */ - if (req->q.manpath == NULL) { - req->q.arch = NULL; + if (req->q.manpath == NULL) return; - } req->q.query = mandoc_strdup(req->q.query); - /* Optional architecture. */ - dir = strrchr(req->q.manpath, '/'); - if (dir != NULL && strncmp(dir + 1, "man", 3) != 0) { - *dir++ = '\0'; - req->q.arch = mandoc_strdup(dir); - dir = strrchr(req->q.manpath, '/'); - } else - req->q.arch = NULL; + /* Split directory components. */ + dir[i = 0] = req->q.manpath; + while ((dir[i + 1] = strchr(dir[i], '/')) != NULL) { + if (++i == 3) { + pg_error_badrequest( + "You specified too many directory components."); + exit(EXIT_FAILURE); + } + *dir[i]++ = '\0'; + } - /* Optional directory name. */ - if (dir != NULL && strncmp(dir + 1, "man", 3) == 0) { - *dir++ = '\0'; + /* Optional manpath. */ + if ((i = validate_manpath(req, req->q.manpath)) == 0) + req->q.manpath = NULL; + else if (dir[1] == NULL) + return; + + /* Optional section. */ + if (strncmp(dir[i], "man", 3) == 0) { free(req->q.sec); - req->q.sec = mandoc_strdup(dir + 3); + req->q.sec = mandoc_strdup(dir[i++] + 3); } + if (dir[i] == NULL) { + if (req->q.manpath == NULL) + free(dir[0]); + return; + } + if (dir[i + 1] != NULL) { + pg_error_badrequest( + "You specified an invalid directory component."); + exit(EXIT_FAILURE); + } + + /* Optional architecture. */ + if (i) { + req->q.arch = mandoc_strdup(dir[i]); + if (req->q.manpath == NULL) + free(dir[0]); + } else + req->q.arch = dir[0]; } /*