summaryrefslogtreecommitdiff
path: root/system/mandoc/cgi-gz.patch
blob: ac506b797398313bc950708897703430fdccd86a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Handle the case where some of the entries in the manpath are gzipped by
removing the .gz extension and allowing mparse_open to add it back and
mark the page as gzipped. This is part of the documented contract for
mparse_open, see mandoc(3).

--- mandoc-1.14.5/cgi.c	2019-03-10 04:56:43.000000000 -0500
+++ mandoc-1.14.5/cgi.c	2020-06-05 15:54:05.681060852 -0500
@@ -93,7 +93,7 @@ static	void		 resp_searchform(const stru
 static	void		 resp_show(const struct req *, const char *);
 static	void		 set_query_attr(char **, char **);
 static	int		 validate_arch(const char *);
-static	int		 validate_filename(const char *);
+static	int		 validate_filename(char *);
 static	int		 validate_manpath(const struct req *, const char *);
 static	int		 validate_urifrag(const char *);
 
@@ -517,8 +509,13 @@ validate_arch(const char *arch)
 }
 
 static int
-validate_filename(const char *file)
+validate_filename(char *file)
 {
+	char *cp;
+
+	cp = strrchr(file, '.');
+	if (cp != NULL && ! strcmp(cp + 1, "gz"))
+		*cp = '\0';
 
 	if ('.' == file[0] && '/' == file[1])
 		file += 2;
@@ -854,14 +862,15 @@ resp_format(const struct req *req, const
 	int		 fd;
 	int		 usepath;
 
-	if (-1 == (fd = open(file, O_RDONLY, 0))) {
-		puts("<p>You specified an invalid manual file.</p>");
-		return;
-	}
-
 	mchars_alloc();
 	mp = mparse_alloc(MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1 |
 	    MPARSE_VALIDATE, MANDOC_OS_OTHER, req->q.manpath);
+
+	if (-1 == (fd = mparse_open(mp, file))) {
+		puts("<p>You specified an invalid manual file.</p>");
+		goto out;
+	}
+
 	mparse_readfd(mp, fd, file);
 	close(fd);
 	meta = mparse_result(mp);
@@ -882,10 +891,11 @@ resp_format(const struct req *req, const
 		html_man(vp, meta);
 
 	html_free(vp);
-	mparse_free(mp);
-	mchars_free();
 	free(conf.man);
 	free(conf.style);
+out:
+	mparse_free(mp);
+	mchars_free();
 }
 
 static void
@@ -904,8 +914,8 @@ resp_show(const struct req *req, const c
 static void
 pg_show(struct req *req, const char *fullpath)
 {
-	char		*manpath;
-	const char	*file;
+	char	*manpath;
+	char	*file;
 
 	if ((file = strchr(fullpath, '/')) == NULL) {
 		pg_error_badrequest(