Close files in CGI child to close bind sock
[metaproxy-moved-to-github.git] / src / filter_cgi.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2010 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 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 <sys/wait.h>
28 #include <sstream>
29
30 #include "config.hpp"
31
32 namespace mp = metaproxy_1;
33 namespace yf = mp::filter;
34
35 namespace metaproxy_1 {
36     namespace filter {
37         class CGI::Exec {
38             friend class Rep;
39             friend class CGI;
40             std::string path;
41             std::string program;
42         };
43         class CGI::Rep {
44             friend class CGI;
45             std::list<CGI::Exec> exec_map;
46         };
47     }
48 }
49
50 yf::CGI::CGI() : m_p(new Rep)
51 {
52 }
53
54 yf::CGI::~CGI()
55 {  // must have a destructor because of boost::scoped_ptr
56 }
57
58 void yf::CGI::process(mp::Package &package) const
59 {
60     Z_GDU *zgdu_req = package.request().get();
61     Z_GDU *zgdu_res = 0;
62     
63     if (!zgdu_req)
64         return;
65     
66     if (zgdu_req->which != Z_GDU_HTTP_Request)
67     {
68         package.move();
69         return;
70     }
71     std::string path_info;
72     std::string query_string;
73     const char *path = zgdu_req->u.HTTP_Request->path;
74     yaz_log(YLOG_LOG, "path=%s", path);
75     const char *p_cp = strchr(path, '?');
76     if (p_cp)
77     {
78         path_info.assign(path, p_cp - path);
79         query_string.assign(p_cp+1);
80     }
81     else
82         path_info.assign(path);
83
84     std::list<CGI::Exec>::const_iterator it;
85     metaproxy_1::odr odr;
86     for (it = m_p->exec_map.begin(); it != m_p->exec_map.end(); it++)
87     {
88         if (it->path.compare(path_info) == 0)
89         {
90             int r;
91             pid_t pid;
92             int status;
93             int fd;
94             
95             pid = ::fork();
96             switch (pid)
97             {
98             case 0: /* child */
99                 for (fd = 3; fd <= 1023; fd++)
100                     close(fd);
101                 setenv("PATH_INFO", path_info.c_str(), 1);
102                 setenv("QUERY_STRING", query_string.c_str(), 1);
103                 r = execl(it->program.c_str(), it->program.c_str(), (char *) 0);
104                 if (r == -1)
105                     exit(1);
106                 exit(0);
107                 break;
108             case -1: /* error */
109                 zgdu_res = odr.create_HTTP_Response(
110                     package.session(), zgdu_req->u.HTTP_Request, 400);
111                 package.response() = zgdu_res;
112                 break;
113             default: /* parent */
114                 waitpid(pid, &status, 0);
115
116                 zgdu_res = odr.create_HTTP_Response(
117                     package.session(), zgdu_req->u.HTTP_Request, 200);
118                 package.response() = zgdu_res;
119                 break;
120             }
121             return;
122         }
123     }
124     package.move();
125 }
126
127 void yf::CGI::configure(const xmlNode *ptr, bool test_only)
128 {
129     for (ptr = ptr->children; ptr; ptr = ptr->next)
130     {
131         if (ptr->type != XML_ELEMENT_NODE)
132             continue;
133         if (!strcmp((const char *) ptr->name, "map"))
134         {
135             CGI::Exec exec;
136
137             const struct _xmlAttr *attr;
138             for (attr = ptr->properties; attr; attr = attr->next)
139             {
140                 if (!strcmp((const char *) attr->name,  "path"))
141                     exec.path = mp::xml::get_text(attr->children);
142                 else if (!strcmp((const char *) attr->name, "exec"))
143                     exec.program = mp::xml::get_text(attr->children);
144                 else
145                     throw mp::filter::FilterException
146                         ("Bad attribute " 
147                          + std::string((const char *) attr->name)
148                          + " in cgi section");
149             }
150             m_p->exec_map.push_back(exec);
151         }
152         else
153         {
154             throw mp::filter::FilterException("Bad element " 
155                                                + std::string((const char *)
156                                                              ptr->name));
157         }
158     }
159 }
160
161 static mp::filter::Base* filter_creator()
162 {
163     return new mp::filter::CGI;
164 }
165
166 extern "C" {
167     struct metaproxy_1_filter_struct metaproxy_1_filter_cgi = {
168         0,
169         "cgi",
170         filter_creator
171     };
172 }
173
174
175 /*
176  * Local variables:
177  * c-basic-offset: 4
178  * c-file-style: "Stroustrup"
179  * indent-tabs-mode: nil
180  * End:
181  * vim: shiftwidth=4 tabstop=8 expandtab
182  */
183