metaproxy 1.22.1
filter_cgi.cpp
Go to the documentation of this file.
1/* This file is part of Metaproxy.
2 Copyright (C) Index Data
3
4Metaproxy is free software; you can redistribute it and/or modify it under
5the terms of the GNU General Public License as published by the Free
6Software Foundation; either version 2, or (at your option) any later
7version.
8
9Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or
11FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include "filter_cgi.hpp"
20#include <metaproxy/package.hpp>
21#include <metaproxy/util.hpp>
22#include "gduutil.hpp"
23#include <yaz/zgdu.h>
24#include <yaz/log.h>
25
26#include <unistd.h>
27#include <fcntl.h>
28#include <signal.h>
29#include <sys/wait.h>
30#include <yaz/poll.h>
31#include <sstream>
32
33#include "config.hpp"
34
35namespace mp = metaproxy_1;
36namespace yf = mp::filter;
37
38namespace metaproxy_1 {
39 namespace filter {
40 class CGI::Exec {
41 friend class Rep;
42 friend class CGI;
43 std::string path;
44 std::string program;
45 };
46 class CGI::Rep {
47 friend class CGI;
48 std::list<CGI::Exec> exec_map;
49 std::map<std::string,std::string> env_map;
50 std::map<pid_t,pid_t> children;
51 boost::mutex m_mutex;
52 std::string documentroot;
53 void child(Z_HTTP_Request *, const CGI::Exec *);
54 public:
55 ~Rep();
56 };
57 }
58}
59
60yf::CGI::CGI() : m_p(new Rep)
61{
62
63}
64
65yf::CGI::Rep::~Rep()
66{
67 std::map<pid_t,pid_t>::const_iterator it;
68 boost::mutex::scoped_lock lock(m_mutex);
69
70 for (it = children.begin(); it != children.end(); it++)
71 kill(it->second, SIGTERM);
72}
73
74yf::CGI::~CGI()
75{
76}
77
78void yf::CGI::Rep::child(Z_HTTP_Request *hreq, const CGI::Exec *it)
79{
80 const char *path_cstr = hreq->path;
81 std::string path(path_cstr);
82 const char *program_cstr = it->program.c_str();
83 std::string script_name(path, 0, it->path.length());
84 std::string rest(path, it->path.length());
85 std::string query_string;
86 std::string path_info;
87 size_t qpos = rest.find('?');
88 if (qpos == std::string::npos)
89 path_info = rest;
90 else
91 {
92 query_string.assign(rest, qpos + 1, std::string::npos);
93 path_info.assign(rest, 0, qpos);
94 }
95 setenv("REQUEST_METHOD", hreq->method, 1);
96 setenv("REQUEST_URI", path_cstr, 1);
97 setenv("SCRIPT_NAME", script_name.c_str(), 1);
98 setenv("PATH_INFO", path_info.c_str(), 1);
99 setenv("QUERY_STRING", query_string.c_str(), 1);
100 const char *v;
101 v = z_HTTP_header_lookup(hreq->headers, "Cookie");
102 if (v)
103 setenv("HTTP_COOKIE", v, 1);
104 v = z_HTTP_header_lookup(hreq->headers, "User-Agent");
105 if (v)
106 setenv("HTTP_USER_AGENT", v, 1);
107 v = z_HTTP_header_lookup(hreq->headers, "Accept");
108 if (v)
109 setenv("HTTP_ACCEPT", v, 1);
110 v = z_HTTP_header_lookup(hreq->headers, "Accept-Encoding");
111 if (v)
112 setenv("HTTP_ACCEPT_ENCODING", v, 1);
113 setenv("DOCUMENT_ROOT", documentroot.c_str(), 1);
114 setenv("GATEWAY_INTERFACE", "CGI/1.1", 1);
115
116 v = z_HTTP_header_lookup(hreq->headers, "Content-Type");
117 if (v)
118 {
119 char tmp[40];
120 sprintf(tmp, "%d", hreq->content_len);
121 setenv("CONTENT_LENGTH", tmp, 1);
122 setenv("CONTENT_TYPE", v, 1);
123 }
124 // apply user-defined environment
125 std::map<std::string,std::string>::const_iterator it_e;
126 for (it_e = env_map.begin();
127 it_e != env_map.end(); it_e++)
128 setenv(it_e->first.c_str(), it_e->second.c_str(), 1);
129 // change directory to configuration root
130 // then to CGI program directory (could be relative)
131 int r = chdir(documentroot.c_str());
132 if (r == -1)
133 {
134 yaz_log(YLOG_LOG, "CGI chdir(%s) failed: %s",
135 documentroot.c_str(), strerror(errno));
136 exit(1);
137 }
138 char *program = xstrdup(program_cstr);
139 char *cp = strrchr(program, '/');
140 if (cp)
141 {
142 *cp++ = '\0';
143 r = chdir(program);
144 if (r == -1)
145 {
146 yaz_log(YLOG_FATAL, "CGI chdir(%s) failed: %s",
147 program, strerror(errno));
148 exit(1);
149 }
150 }
151 else
152 cp = program;
153 r = execl(cp, cp, (char *) 0);
154 if (r == -1)
155 {
156 yaz_log(YLOG_FATAL, "CGI execl(%s) failed: %s",
157 program, strerror(errno));
158 exit(1);
159 }
160 exit(0);
161}
162
163
164void yf::CGI::process(mp::Package &package) const
165{
166 Z_GDU *zgdu_req = package.request().get();
167 Z_GDU *zgdu_res = 0;
168
169 if (!zgdu_req || zgdu_req->which != Z_GDU_HTTP_Request)
170 {
171 package.move();
172 return;
173 }
174 std::list<CGI::Exec>::const_iterator it;
175 metaproxy_1::odr odr;
176 Z_HTTP_Request *hreq = zgdu_req->u.HTTP_Request;
177 const char *path_cstr = hreq->path;
178 for (it = m_p->exec_map.begin(); it != m_p->exec_map.end(); it++)
179 {
180 if (strncmp(it->path.c_str(), path_cstr, it->path.length()) == 0)
181 {
182 int fds_response[2];
183 int r = pipe(fds_response);
184 if (r == -1)
185 {
186 zgdu_res = odr.create_HTTP_Response(
187 package.session(), hreq, 400);
188 package.response() = zgdu_res;
189 continue;
190 }
191 int fds_request[2];
192 r = pipe(fds_request);
193 if (r == -1)
194 {
195 zgdu_res = odr.create_HTTP_Response(
196 package.session(), hreq, 400);
197 package.response() = zgdu_res;
198 close(fds_response[0]);
199 close(fds_response[1]);
200 continue;
201 }
202
203 int status;
204 pid_t pid = ::fork();
205 switch (pid)
206 {
207 case 0: /* child */
208 /* POSTed content */
209 close(0);
210 r = dup(fds_request[0]);
211 if (r == -1)
212 {
213 zgdu_res = odr.create_HTTP_Response(
214 package.session(), hreq, 500);
215 package.response() = zgdu_res;
216 exit(1);
217 }
218 close(fds_request[1]);
219 /* response */
220 close(1);
221 close(fds_response[0]);
222 r = dup(fds_response[1]);
223 if (r == -1)
224 {
225 zgdu_res = odr.create_HTTP_Response(
226 package.session(), hreq, 500);
227 package.response() = zgdu_res;
228 exit(1);
229 }
230 m_p->child(hreq, &(*it));
231 break;
232 case -1: /* error */
233 close(fds_request[0]);
234 close(fds_request[1]);
235 close(fds_response[0]);
236 close(fds_response[1]);
237 zgdu_res = odr.create_HTTP_Response(
238 package.session(), hreq, 400);
239 package.response() = zgdu_res;
240 break;
241 default: /* parent */
242 close(fds_response[1]);
243 close(fds_request[0]);
244 if (pid)
245 {
246 boost::mutex::scoped_lock lock(m_p->m_mutex);
247 m_p->children[pid] = pid;
248 }
249 WRBUF w = wrbuf_alloc();
250 wrbuf_puts(w, "HTTP/1.1 200 OK\r\n");
251 fcntl(fds_response[0], F_SETFL, O_NONBLOCK);
252 fcntl(fds_request[1], F_SETFL, O_NONBLOCK);
253 int no_write = 0;
254 while (1)
255 {
256 int num = 1;
257 struct yaz_poll_fd fds[2];
258 fds[0].fd = fds_response[0];
259 fds[0].input_mask = yaz_poll_read;
260 if (no_write < hreq->content_len)
261 {
262 fds[1].fd = fds_request[1];
263 fds[1].input_mask = yaz_poll_write;
264 num = 2;
265 }
266 int r = yaz_poll(fds, num, 60, 0);
267 if (r <= 0)
268 break;
269 if (fds[0].output_mask & (yaz_poll_read|yaz_poll_except))
270 {
271 char buf[512];
272 ssize_t rd = read(fds_response[0], buf, sizeof buf);
273 if (rd <= 0)
274 break;
275 wrbuf_write(w, buf, rd);
276 }
277 if (num == 2 && fds[1].output_mask & yaz_poll_write)
278 {
279 ssize_t wd = write(fds_request[1],
280 hreq->content_buf + no_write,
281 hreq->content_len - no_write);
282 if (wd <= 0)
283 break;
284 no_write += wd;
285 }
286 }
287 close(fds_request[1]);
288 close(fds_response[0]);
289 waitpid(pid, &status, 0);
290
291 if (pid)
292 {
293 boost::mutex::scoped_lock lock(m_p->m_mutex);
294 m_p->children.erase(pid);
295 }
296 ODR dec = odr_createmem(ODR_DECODE);
297 odr_setbuf(dec, wrbuf_buf(w), wrbuf_len(w), 0);
298 r = z_GDU(dec, &zgdu_res, 0, 0);
299 if (r && zgdu_res)
300 {
301 package.response() = zgdu_res;
302 }
303 else
304 {
305 zgdu_res = odr.create_HTTP_Response(
306 package.session(), zgdu_req->u.HTTP_Request, 400);
307 Z_HTTP_Response *hres = zgdu_res->u.HTTP_Response;
308 z_HTTP_header_add(odr, &hres->headers,
309 "Content-Type", "text/plain");
310 hres->content_buf =
311 odr_strdup(odr, "Invalid script from script");
312 hres->content_len = strlen(hres->content_buf);
313 }
314 package.response() = zgdu_res;
315 odr_destroy(dec);
316 wrbuf_destroy(w);
317 break;
318 }
319 return;
320 }
321 }
322 package.move();
323}
324
325void yf::CGI::configure(const xmlNode *ptr, bool test_only, const char *path)
326{
327 yaz_log(YLOG_LOG, "cgi::configure path=%s", path);
328 for (ptr = ptr->children; ptr; ptr = ptr->next)
329 {
330 if (ptr->type != XML_ELEMENT_NODE)
331 continue;
332 if (!strcmp((const char *) ptr->name, "map"))
333 {
334 CGI::Exec exec;
335
336 const struct _xmlAttr *attr;
337 for (attr = ptr->properties; attr; attr = attr->next)
338 {
339 if (!strcmp((const char *) attr->name, "path"))
340 exec.path = mp::xml::get_text(attr->children);
341 else if (!strcmp((const char *) attr->name, "exec"))
342 exec.program = mp::xml::get_text(attr->children);
343 else
344 throw mp::filter::FilterException
345 ("Bad attribute "
346 + std::string((const char *) attr->name)
347 + " in cgi section");
348 }
349 m_p->exec_map.push_back(exec);
350 }
351 else if (!strcmp((const char *) ptr->name, "env"))
352 {
353 std::string name, value;
354
355 const struct _xmlAttr *attr;
356 for (attr = ptr->properties; attr; attr = attr->next)
357 {
358 if (!strcmp((const char *) attr->name, "name"))
359 name = mp::xml::get_text(attr->children);
360 else if (!strcmp((const char *) attr->name, "value"))
361 value = mp::xml::get_text(attr->children);
362 else
363 throw mp::filter::FilterException
364 ("Bad attribute "
365 + std::string((const char *) attr->name)
366 + " in cgi section");
367 }
368 if (name.length() > 0)
369 m_p->env_map[name] = value;
370 }
371 else if (!strcmp((const char *) ptr->name, "documentroot"))
372 {
373 m_p->documentroot = path;
374 }
375 else
376 {
377 throw mp::filter::FilterException("Bad element "
378 + std::string((const char *)
379 ptr->name));
380 }
381 }
382 if (m_p->documentroot.length() == 0)
383 m_p->documentroot = ".";
384}
385
386static mp::filter::Base* filter_creator()
387{
388 return new mp::filter::CGI;
389}
390
391extern "C" {
392 struct metaproxy_1_filter_struct metaproxy_1_filter_cgi = {
393 0,
394 "cgi",
396 };
397}
398
399
400/*
401 * Local variables:
402 * c-basic-offset: 4
403 * c-file-style: "Stroustrup"
404 * indent-tabs-mode: nil
405 * End:
406 * vim: shiftwidth=4 tabstop=8 expandtab
407 */
408
std::map< std::string, std::string > env_map
void child(Z_HTTP_Request *, const CGI::Exec *)
std::map< pid_t, pid_t > children
std::list< CGI::Exec > exec_map
static mp::filter::Base * filter_creator()
struct metaproxy_1_filter_struct metaproxy_1_filter_cgi