version 1.74, 2014/07/18 14:46:25 |
version 1.79, 2014/07/21 22:33:01 |
Line 71 static void pg_noresult(const struct req *, const ch |
|
Line 71 static void pg_noresult(const struct req *, const ch |
|
static void pg_search(const struct req *); |
static void pg_search(const struct req *); |
static void pg_searchres(const struct req *, |
static void pg_searchres(const struct req *, |
struct manpage *, size_t); |
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_html(int, const char *); |
static void resp_begin_http(int, const char *); |
static void resp_begin_http(int, const char *); |
static void resp_end_html(void); |
static void resp_end_html(void); |
Line 79 static void resp_searchform(const struct req *); |
|
Line 79 static void resp_searchform(const struct req *); |
|
static void resp_show(const struct req *, const char *); |
static void resp_show(const struct req *, const char *); |
|
|
static const char *scriptname; /* CGI script name */ |
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 int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9}; |
static const char *const sec_numbers[] = { |
static const char *const sec_numbers[] = { |
Line 467 resp_searchform(const struct req *req) |
|
Line 466 resp_searchform(const struct req *req) |
|
puts("<!-- End search form. //-->"); |
puts("<!-- End search form. //-->"); |
} |
} |
|
|
|
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 |
static void |
pg_index(const struct req *req) |
pg_index(const struct req *req) |
{ |
{ |
Line 524 pg_searchres(const struct req *req, struct manpage *r, |
|
Line 549 pg_searchres(const struct req *req, struct manpage *r, |
|
int prio, priouse; |
int prio, priouse; |
char sec; |
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 (1 == sz) { |
/* |
/* |
* If we have just one result, then jump there now |
* If we have just one result, then jump there now |
Line 531 pg_searchres(const struct req *req, struct manpage *r, |
|
Line 565 pg_searchres(const struct req *req, struct manpage *r, |
|
*/ |
*/ |
printf("Status: 303 See Other\r\n"); |
printf("Status: 303 See Other\r\n"); |
printf("Location: http://%s%s/%s/%s?", |
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); |
http_printquery(req); |
printf("\r\n" |
printf("\r\n" |
"Content-Type: text/html; charset=utf-8\r\n" |
"Content-Type: text/html; charset=utf-8\r\n" |
Line 751 format(const struct req *req, const char *file) |
|
Line 785 format(const struct req *req, const char *file) |
|
return; |
return; |
} |
} |
|
|
snprintf(opts, sizeof(opts), |
snprintf(opts, sizeof(opts), "fragment,man=%s?" |
"fragment,man=%s?query=%%N&sec=%%S", |
"manpath=%s&query=%%N&sec=%%S&arch=%s", |
scriptname); |
scriptname, req->q.manpath, |
|
req->q.arch ? req->q.arch : ""); |
|
|
mparse_result(mp, &mdoc, &man, NULL); |
mparse_result(mp, &mdoc, &man, NULL); |
if (NULL == man && NULL == mdoc) { |
if (NULL == man && NULL == mdoc) { |
Line 778 format(const struct req *req, const char *file) |
|
Line 813 format(const struct req *req, const char *file) |
|
static void |
static void |
resp_show(const struct req *req, const char *file) |
resp_show(const struct req *req, const char *file) |
{ |
{ |
if ('.' == file[0] || '/' == file[1]) |
|
|
if ('.' == file[0] && '/' == file[1]) |
file += 2; |
file += 2; |
|
|
if ('c' == *file) |
if ('c' == *file) |
Line 788 resp_show(const struct req *req, const char *file) |
|
Line 824 resp_show(const struct req *req, const char *file) |
|
} |
} |
|
|
static void |
static void |
pg_show(const struct req *req, const char *path) |
pg_show(struct req *req, const char *path) |
{ |
{ |
char *sub; |
char *sub; |
|
|
Line 799 pg_show(const struct req *req, const char *path) |
|
Line 835 pg_show(const struct req *req, const char *path) |
|
} |
} |
*sub++ = '\0'; |
*sub++ = '\0'; |
|
|
|
if ( ! validate_manpath(req, path)) { |
|
pg_error_badrequest( |
|
"You specified an invalid manpath."); |
|
return; |
|
} |
|
|
/* |
/* |
* Begin by chdir()ing into the manpath. |
* Begin by chdir()ing into the manpath. |
* This way we can pick up the database files, which are |
* This way we can pick up the database files, which are |
Line 806 pg_show(const struct req *req, const char *path) |
|
Line 848 pg_show(const struct req *req, const char *path) |
|
*/ |
*/ |
|
|
if (-1 == chdir(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( |
pg_error_badrequest( |
"You specified an invalid manpath."); |
"You specified an invalid manual file."); |
return; |
return; |
} |
} |
|
|
|
if (strcmp(path, "mandoc")) |
|
req->q.manpath = path; |
|
|
resp_begin_html(200, NULL); |
resp_begin_html(200, NULL); |
resp_searchform(req); |
resp_searchform(req); |
resp_show(req, sub); |
resp_show(req, sub); |
Line 835 pg_search(const struct req *req) |
|
Line 887 pg_search(const struct req *req) |
|
*/ |
*/ |
|
|
if (-1 == (chdir(req->q.manpath))) { |
if (-1 == (chdir(req->q.manpath))) { |
pg_error_badrequest( |
fprintf(stderr, "chdir %s: %s\n", |
"You specified an invalid manpath."); |
req->q.manpath, strerror(errno)); |
|
pg_error_internal(); |
return; |
return; |
} |
} |
|
|
|
|
if (NULL == (scriptname = getenv("SCRIPT_NAME"))) |
if (NULL == (scriptname = getenv("SCRIPT_NAME"))) |
scriptname = ""; |
scriptname = ""; |
|
|
if (NULL == (httphost = getenv("HTTP_HOST"))) |
|
httphost = "localhost"; |
|
|
|
/* |
/* |
* First we change directory into the MAN_DIR so that |
* First we change directory into the MAN_DIR so that |
* subsequent scanning for manpath directories is rooted |
* subsequent scanning for manpath directories is rooted |
|
|
|
|
if (NULL != (querystring = getenv("QUERY_STRING"))) |
if (NULL != (querystring = getenv("QUERY_STRING"))) |
http_parse(&req, querystring); |
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. */ |
/* Dispatch to the three different pages. */ |
|
|