Debian Codegraph @

src/HTMLTemplate.cpp

raw
1 //Implements the HTMLTemplate class, for displaying template-based banned pages to clients
2
3 // For all support, instructions and copyright go to:
4 // http://e2guardian.org/
5 // Released under the GPL v2, with the OpenSSL exception described in the README file.
6
7 // INCLUDES
8
9 #ifdef HAVE_CONFIG_H
10 #include "e2config.h"
11 #endif
12 #include "HTMLTemplate.hpp"
13 #include "RegExp.hpp"
14 #include "String.hpp"
15 #include "OptionContainer.hpp"
16 #include "Logger.hpp"
17
18 #include <cstdlib>
19 #include <cstdio>
20 #include <unistd.h>
21 #include <iostream>
22 #include <fstream>
23
24 // GLOBALS
25
26 extern OptionContainer o;
27
28 // IMPLEMENTATION
29
30 // wipe the loaded template
31 void HTMLTemplate::reset()
32 {
33 html.clear();
34 }
35
36 // push a line onto our string list
37 void HTMLTemplate::push(String s)
38 {
39 if (s.length() > 0) {
40 html.push_back(s);
41 }
42 }
43
44 // read in HTML template and find URL, reason, category etc. placeholders
45 bool HTMLTemplate::readTemplateFile(const char *filename, const char *placeholders)
46 {
47 std::string linebuffer;
48 RegExp re;
49 // compile regexp for matching supported placeholders
50 // allow optional custom placeholder string
51 re.comp(placeholders ? placeholders : "-URL-|-REASONGIVEN-|-REASONLOGGED-|-USER-|-IP-|-HOST-|-FILTERGROUP-|-RAWFILTERGROUP-|-BYPASS-|-CATEGORIES-|-SHORTURL-|-SERVERIP-|-EXTFLAGS-|-SERVERNAME-");
52 RegResult Rre;
53 unsigned int offset;
54 String result;
55 String line;
56 std::ifstream templatefile(filename, std::ios::in); // e2guardian.conf
57 if (!templatefile.good()) {
58 E2LOGGER_error("error reading HTML template file: ", filename);
59 return false;
60 }
61 while (!templatefile.eof()) {
62 std::getline(templatefile, linebuffer);
63 line = linebuffer.c_str();
64 // look for placeholders
65 re.match(line.toCharArray(),Rre);
66 while (Rre.numberOfMatches() > 0) {
67 // whenever we find one, push the text before it onto the list, then the placeholder, then the text after it
68 offset = Rre.offset(0);
69 result = Rre.result(0).c_str();
70 if (offset > 0) {
71 push(line.subString(0, offset));
72 push(result);
73 line = line.subString(offset + result.length(), line.length() - offset - result.length());
74 } else {
75 push(result);
76 line = line.subString(result.length(), line.length() - result.length());
77 }
78 re.match(line.toCharArray(),Rre);
79 }
80 // if any text remains, or we didn't find a placeholder, push the remainder of the line
81 if (line.length() > 0) {
82 push(line);
83 }
84 }
85 if (html.size() < 1) {
86 E2LOGGER_error("Unable to parse template file: ", filename);
87 return false;
88 }
89
90 // add space as last line - to make safe i+1 and size -1 logic in display_hb
91 line = " ";
92 push(line);
93
94 templatefile.close();
95 return true;
96 }
97
98 // encode quotes and angle brackets using URL encoding to prevent XSS in the block page
99 void makeURLSafe(String &url)
100 {
101 url.replaceall("'", "%27");
102 url.replaceall("\"", "%22");
103 url.replaceall("<", "%3C");
104 url.replaceall(">", "%3E");
105 }
106
107 void HTMLTemplate::display_hb(String &ebody, String *url, std::string &reason, std::string &logreason, std::string &categories,
108 std::string *user, std::string *ip, std::string *host, int filtergroup, String grpname, String &hashed , String &localip, String &extflags) {
109
110 DEBUG_debug("Displaying TEMPLATE. url:", url, " reason:", reason, " logreason:", logreason);
111
112 String line;
113 bool newline;
114 unsigned int sz = html.size();
115 if (sz < 1) //template is empty
116 return;
117 sz--; //ignore last space line added by read function
118 String safeurl(*url); // Take a copy of the URL so we can encode it to stop XSS
119 bool safe = false;
120 String servername("");
121 servername = o.net.server_name;
122 for (unsigned int i = 0; i < sz; i++) {
123 // preserve newlines from original file
124 newline = false;
125 line = html[i];
126 DEBUG_debug("Displaying TEMPLATE: ", line);
127
128 // Take care SSLMITM negotiation error
129 if (line.length() < 1){
130 ebody += "\n";
131 E2LOGGER_error("Corrupted TEMPLATE returns: ", url->c_str());
132 break;
133 }
134
135 // look for placeholders (split onto their own line by readTemplateFile) and replace them
136 if (line == "-URL-") {
137 if (!safe) {
138 makeURLSafe(safeurl);
139 safe = true;
140 }
141 line = safeurl;
142 } else if (line == "-SHORTURL-") {
143 if (!safe) {
144 makeURLSafe(safeurl);
145 safe = true;
146 }
147 line = safeurl;
148 if (line.length() > 41) {
149 line = line.subString(0, 40);
150 line += "...";
151 }
152 } else if (line == "-SERVERIP-") {
153 line = localip;
154 } else if (line == "-REASONGIVEN-") {
155 String safereason = reason;
156 makeURLSafe(safereason);
157 line = safereason;
158 } else if (line == "-REASONLOGGED-") {
159 line = logreason;
160 } else if (line == "-SERVERNAME-") {
161 line = servername;
162 } else if (line == "-USER-") {
163 String safeuser(*user);
164 makeURLSafe(safeuser);
165 line = safeuser;
166 } else if (line == "-IP-") {
167 line = *ip;
168 } else if (line == "-HOST-") {
169 if (host == NULL) {
170 line = "";
171 } else {
172 line = *host;
173 }
174 } else if (line == "-FILTERGROUP-") {
175 line = grpname;
176 } else if (line == "-RAWFILTERGROUP-") {
177 line = String(filtergroup + 1);
178 } else if (line == "-CATEGORIES-") {
179 if (categories.length() > 0) {
180 line = categories;
181 } else {
182 line = "N/A";
183 }
184 } else if (line == "-BYPASS-") {
185 if (hashed.length() > 0) {
186 if (!safe) {
187 makeURLSafe(safeurl);
188 safe = true;
189 }
190 line = safeurl;
191 if (!(url->after("://").contains("/"))) {
192 line += "/";
193 }
194 if (url->contains("?")) {
195 line += "&" + hashed;
196 } else {
197 line += "?" + hashed;
198 }
199 } else {
200 line = "";
201 }
202 } else if (line == "-EXTFLAGS-") {
203 line = extflags;
204 } else {
205 // if this line wasn't a placeholder, and neither is the
206 // next line, then output a newline, thus preserving line breaks
207 // from the original template file.
208 if (html[i + 1][0] != '-') {
209 newline = true;
210 }
211 }
212 if (line.length() > 0) {
213 ebody += line;
214 }
215 if (newline) {
216 ebody += "\n";
217 }
218 }
219 //ebody += html[sz].toCharArray(); - no longer needed as html[sz] is added as padding
220 //ebody += "\n";
221 }
222
223 #ifdef NOTDEF
224 // fill in placeholders with the given information and send the resulting page to the client
225 // only useful if you used the default set of placeholders
226 void HTMLTemplate::display(Socket *s, String *url, std::string &reason, std::string &logreason, std::string &categories,
227 std::string *user, std::string *ip, std::string *host, int filtergroup, String grpname, String &hashed)
228 {
229 String ebody;
230 String localip = s->getLocalIP();
231 display_hb(ebody, url, reason, logreason, categories,
232 user, ip, host, filtergroup, grpname, hashed, localip);
233 s->writeString(ebody.toCharArray());
234 }
235 #endif