=================================================================== RCS file: /cvs/mandoc/cgi.c,v retrieving revision 1.78 retrieving revision 1.80 diff -u -p -r1.78 -r1.80 --- mandoc/cgi.c 2014/07/21 15:45:17 1.78 +++ mandoc/cgi.c 2014/07/22 18:14:13 1.80 @@ -1,4 +1,4 @@ -/* $Id: cgi.c,v 1.78 2014/07/21 15:45:17 schwarze Exp $ */ +/* $Id: cgi.c,v 1.80 2014/07/22 18:14:13 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2014 Ingo Schwarze @@ -71,7 +71,7 @@ static void pg_noresult(const struct req *, const ch static void pg_search(const struct req *); static void pg_searchres(const struct req *, struct manpage *, size_t); -static void pg_show(const struct req *, const char *); +static void pg_show(struct req *, const char *); static void resp_begin_html(int, const char *); static void resp_begin_http(int, const char *); static void resp_end_html(void); @@ -467,6 +467,20 @@ resp_searchform(const struct req *req) } static int +validate_urifrag(const char *frag) +{ + + while ('\0' != *frag) { + if ( ! (isalnum((unsigned char)*frag) || + '-' == *frag || '.' == *frag || + '/' == *frag || '_' == *frag)) + return(0); + frag++; + } + return(1); +} + +static int validate_manpath(const struct req *req, const char* manpath) { size_t i; @@ -785,9 +799,10 @@ format(const struct req *req, const char *file) return; } - snprintf(opts, sizeof(opts), - "fragment,man=%s?query=%%N&sec=%%S", - scriptname); + snprintf(opts, sizeof(opts), "fragment,man=%s?" + "manpath=%s&query=%%N&sec=%%S&arch=%s", + scriptname, req->q.manpath, + req->q.arch ? req->q.arch : ""); mparse_result(mp, &mdoc, &man, NULL); if (NULL == man && NULL == mdoc) { @@ -823,7 +838,7 @@ resp_show(const struct req *req, const char *file) } static void -pg_show(const struct req *req, const char *path) +pg_show(struct req *req, const char *path) { char *sub; @@ -859,6 +874,9 @@ pg_show(const struct req *req, const char *path) return; } + if (strcmp(path, "mandoc")) + req->q.manpath = path; + resp_begin_html(200, NULL); resp_searchform(req); resp_show(req, sub); @@ -956,6 +974,13 @@ main(void) if (NULL == (scriptname = getenv("SCRIPT_NAME"))) scriptname = ""; + if ( ! validate_urifrag(scriptname)) { + fprintf(stderr, "unsafe SCRIPT_NAME \"%s\"\n", + scriptname); + pg_error_internal(); + return(EXIT_FAILURE); + } + /* * First we change directory into the MAN_DIR so that * subsequent scanning for manpath directories is rooted @@ -983,6 +1008,12 @@ main(void) return(EXIT_FAILURE); } + if ( ! (NULL == req.q.arch || validate_urifrag(req.q.arch))) { + pg_error_badrequest( + "You specified an invalid architecture."); + return(EXIT_FAILURE); + } + /* Dispatch to the three different pages. */ path = getenv("PATH_INFO"); @@ -1034,7 +1065,20 @@ pathgen(struct req *req) dpsz--; req->p = mandoc_realloc(req->p, (req->psz + 1) * sizeof(char *)); - req->p[req->psz++] = mandoc_strndup(dp, dpsz); + dp = mandoc_strndup(dp, dpsz); + if ( ! validate_urifrag(dp)) { + fprintf(stderr, "%s/manpath.conf contains " + "unsafe path \"%s\"\n", MAN_DIR, dp); + pg_error_internal(); + exit(EXIT_FAILURE); + } + if (NULL != strchr(dp, '/')) { + fprintf(stderr, "%s/manpath.conf contains " + "path with slash \"%s\"\n", MAN_DIR, dp); + pg_error_internal(); + exit(EXIT_FAILURE); + } + req->p[req->psz++] = dp; } if ( req->p == NULL ) {