version 1.74, 2014/07/18 14:46:25 |
version 1.81, 2014/07/24 08:26:57 |
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 423 resp_searchform(const struct req *req) |
|
Line 422 resp_searchform(const struct req *req) |
|
|
|
/* Write architecture selector. */ |
/* Write architecture selector. */ |
|
|
puts("<SELECT NAME=\"arch\">"); |
printf( "<SELECT NAME=\"arch\">\n" |
|
"<OPTION VALUE=\"default\""); |
|
if (NULL == req->q.arch) |
|
printf(" SELECTED"); |
|
puts(">All Architectures</OPTION>"); |
for (i = 0; i < arch_MAX; i++) { |
for (i = 0; i < arch_MAX; i++) { |
printf("<OPTION VALUE=\"%s\"", arch_names[i]); |
printf("<OPTION VALUE=\"%s\"", arch_names[i]); |
if (NULL != req->q.arch && |
if (NULL != req->q.arch && |
Line 467 resp_searchform(const struct req *req) |
|
Line 470 resp_searchform(const struct req *req) |
|
puts("<!-- End search form. //-->"); |
puts("<!-- End search form. //-->"); |
} |
} |
|
|
|
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; |
|
|
|
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 520 pg_error_internal(void) |
|
Line 563 pg_error_internal(void) |
|
static void |
static void |
pg_searchres(const struct req *req, struct manpage *r, size_t sz) |
pg_searchres(const struct req *req, struct manpage *r, size_t sz) |
{ |
{ |
|
char *arch, *archend; |
size_t i, iuse, isec; |
size_t i, iuse, isec; |
|
int archprio, archpriouse; |
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 585 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 574 pg_searchres(const struct req *req, struct manpage *r, |
|
Line 628 pg_searchres(const struct req *req, struct manpage *r, |
|
puts("<HR>"); |
puts("<HR>"); |
iuse = 0; |
iuse = 0; |
priouse = 10; |
priouse = 10; |
|
archpriouse = 3; |
for (i = 0; i < sz; i++) { |
for (i = 0; i < sz; i++) { |
isec = strcspn(r[i].file, "123456789"); |
isec = strcspn(r[i].file, "123456789"); |
sec = r[i].file[isec]; |
sec = r[i].file[isec]; |
if ('\0' == sec) |
if ('\0' == sec) |
continue; |
continue; |
prio = sec_prios[sec - '1']; |
prio = sec_prios[sec - '1']; |
|
if (NULL == req->q.arch) { |
|
archprio = |
|
(NULL == (arch = strchr( |
|
r[i].file + isec, '/'))) ? 3 : |
|
(NULL == (archend = strchr( |
|
arch + 1, '/'))) ? 0 : |
|
strncmp(arch, "amd64/", |
|
archend - arch) ? 2 : 1; |
|
if (archprio < archpriouse) { |
|
archpriouse = archprio; |
|
priouse = prio; |
|
iuse = i; |
|
continue; |
|
} |
|
if (archprio > archpriouse) |
|
continue; |
|
} |
if (prio >= priouse) |
if (prio >= priouse) |
continue; |
continue; |
priouse = prio; |
priouse = prio; |
Line 751 format(const struct req *req, const char *file) |
|
Line 823 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 851 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 862 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 873 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 886 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 925 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"))) |
if ( ! validate_urifrag(scriptname)) { |
httphost = "localhost"; |
fprintf(stderr, "unsafe SCRIPT_NAME \"%s\"\n", |
|
scriptname); |
|
pg_error_internal(); |
|
return(EXIT_FAILURE); |
|
} |
|
|
/* |
/* |
* First we change directory into the MAN_DIR so that |
* First we change directory into the MAN_DIR so that |
|
|
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); |
|
} |
|
|
|
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. */ |
/* Dispatch to the three different pages. */ |
|
|
path = getenv("PATH_INFO"); |
path = getenv("PATH_INFO"); |
Line 982 pathgen(struct req *req) |
|
Line 1089 pathgen(struct req *req) |
|
dpsz--; |
dpsz--; |
req->p = mandoc_realloc(req->p, |
req->p = mandoc_realloc(req->p, |
(req->psz + 1) * sizeof(char *)); |
(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 ) { |
if ( req->p == NULL ) { |