missed a variable name
[shibboleth/cpp-sp.git] / shar / shar.cpp
1 /*
2  * The Shibboleth License, Version 1.
3  * Copyright (c) 2002
4  * University Corporation for Advanced Internet Development, Inc.
5  * All rights reserved
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution, if any, must include
17  * the following acknowledgment: "This product includes software developed by
18  * the University Corporation for Advanced Internet Development
19  * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement
20  * may appear in the software itself, if and wherever such third-party
21  * acknowledgments normally appear.
22  *
23  * Neither the name of Shibboleth nor the names of its contributors, nor
24  * Internet2, nor the University Corporation for Advanced Internet Development,
25  * Inc., nor UCAID may be used to endorse or promote products derived from this
26  * software without specific prior written permission. For written permission,
27  * please contact shibboleth@shibboleth.org
28  *
29  * Products derived from this software may not be called Shibboleth, Internet2,
30  * UCAID, or the University Corporation for Advanced Internet Development, nor
31  * may Shibboleth appear in their name, without prior written permission of the
32  * University Corporation for Advanced Internet Development.
33  *
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
36  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
38  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
39  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
40  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
41  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT,
42  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49
50 /*
51  * shar.cpp -- the SHAR "main" code.  All the functionality is elsewhere
52  *           (in case you want to turn this into a library later).
53  *
54  * Created By:  Derek Atkins <derek@ihtfp.com>
55  *
56  * $Id$
57  */
58
59 // eventually we might be able to support autoconf via cygwin...
60 #if defined (_MSC_VER) || defined(__BORLANDC__)
61 # include "config_win32.h"
62 #else
63 # include "config.h"
64 #endif
65
66 #ifdef HAVE_UNISTD_H
67 #include <unistd.h>
68 #include <sys/select.h>
69 #endif
70
71 #include <stdio.h>
72 #include <errno.h>
73 #include <signal.h>
74
75 #include "shar-utils.h"
76 #include <log4cpp/Category.hh>
77
78 using namespace std;
79 using namespace saml;
80 using namespace shibboleth;
81 using namespace shibtarget;
82 using namespace log4cpp;
83
84 #ifndef FD_SETSIZE
85 # define FD_SETSIZE 1024
86 #endif
87
88 extern "C" void shibrpc_prog_1(struct svc_req* rqstp, register SVCXPRT* transp);
89
90 int shar_run = 1;
91 const char* shar_config = NULL;
92 const char* shar_schemadir = NULL;
93 bool shar_checkonly = false;
94 static int unlink_socket = 0;
95
96 static bool new_connection(IListener::ShibSocket& listener, const Iterator<ShibRPCProtocols>& protos)
97 {
98     IListener::ShibSocket sock;
99
100     // Accept the connection.
101     if (!ShibTargetConfig::getConfig().getINI()->getListener()->accept(listener, sock))
102         return false;
103
104     // We throw away the result because the children manage themselves...
105     new SharChild(sock,protos);
106     return true;
107 }
108
109 static void shar_svc_run(IListener::ShibSocket& listener, const Iterator<ShibRPCProtocols>& protos)
110 {
111     NDC ndc("shar_svc_run");
112     Category& log=Category::getInstance("SHAR");
113
114     while (shar_run) {
115         fd_set readfds;
116         FD_ZERO(&readfds);
117         FD_SET(listener, &readfds);
118         struct timeval tv = { 0, 0 };
119         tv.tv_sec = 5;
120     
121         switch (select(FD_SETSIZE, &readfds, 0, 0, &tv)) {
122             case -1:
123                 if (errno == EINTR) continue;
124                 SHARUtils::log_error();
125                 return;
126         
127             case 0:
128                 continue;
129         
130             default:
131                 if (!new_connection(listener, protos))
132                     log.error("new_connection failed");
133         }
134     }
135     log.info("shar_svc_run ended");
136 }
137
138 #ifdef WIN32
139
140 int real_main(int preinit)
141 {
142     static IListener::ShibSocket sock;
143     ShibRPCProtocols protos[1] = {
144         { SHIBRPC_PROG, SHIBRPC_VERS_1, shibrpc_prog_1 }
145     };
146
147     ShibTargetConfig& conf=ShibTargetConfig::getConfig();
148     if (preinit) {
149
150         // initialize the shib-target library
151         conf.setFeatures(
152             ShibTargetConfig::Listener |
153             ShibTargetConfig::SessionCache |
154             ShibTargetConfig::Metadata |
155             ShibTargetConfig::Trust |
156             ShibTargetConfig::Credentials |
157             ShibTargetConfig::AAP |
158             ShibTargetConfig::SHARExtensions |
159             (shar_checkonly ? (ShibTargetConfig::SHIREExtensions | ShibTargetConfig::RequestMapper) : 0)
160             );
161         if (!shar_config)
162             shar_config=getenv("SHIBCONFIG");
163         if (!shar_schemadir)
164             shar_schemadir=getenv("SHIBSCHEMAS");
165         if (!shar_schemadir)
166             shar_schemadir=SHIB_SCHEMAS;
167         if (!shar_config)
168             shar_config=SHIB_CONFIG;
169         if (!conf.init(shar_schemadir,shar_config)) {
170             fprintf(stderr, "configuration is invalid, check log for specific problems\n");
171             return -2;
172         }
173
174         // If just a test run, bail.
175         if (shar_checkonly) {
176             fprintf(stderr, "overall configuration is loadable, check log for non-fatal problems\n");
177             return 0;
178         }
179
180         const IListener* listener=conf.getINI()->getListener();
181         
182         // Create the SHAR listener socket
183         if (!listener->create(sock))
184             return -3;
185
186         // Bind to the proper port
187         if (!listener->bind(sock))
188             return -4;
189
190         // Initialize the SHAR Utilitites
191         SHARUtils::init();
192     }
193     else {
194         // Run the listener
195         if (!shar_checkonly) {
196             shar_svc_run(sock, ArrayIterator<ShibRPCProtocols>(protos,1));
197             fprintf(stderr,"shar_svc_run returned\n");
198
199             // Finalize the SHAR, close all clients
200             SHARUtils::fini();
201             conf.getINI()->getListener()->close(sock);
202         }
203
204         conf.shutdown();
205         fprintf(stderr, "shar shutdown complete\n");
206     }
207     return 0;
208 }
209
210 #else
211
212 static void term_handler(int arg)
213 {
214     shar_run = 0;
215 }
216
217 static int setup_signals(void)
218 {
219     NDC ndc("setup_signals");
220     
221     struct sigaction sa;
222     memset(&sa, 0, sizeof (sa));
223     sa.sa_handler = SIG_IGN;
224     sa.sa_flags = SA_RESTART;
225
226     if (sigaction(SIGPIPE, &sa, NULL) < 0) {
227         SHARUtils::log_error();
228         return -1;
229     }
230
231     memset(&sa, 0, sizeof (sa));
232     sa.sa_handler = term_handler;
233     sa.sa_flags = SA_RESTART;
234
235     if (sigaction(SIGHUP, &sa, NULL) < 0) {
236         SHARUtils::log_error();
237         return -1;
238     }
239     if (sigaction(SIGINT, &sa, NULL) < 0) {
240         SHARUtils::log_error();
241         return -1;
242     }
243     if (sigaction(SIGQUIT, &sa, NULL) < 0) {
244         SHARUtils::log_error();
245         return -1;
246     }
247     if (sigaction(SIGTERM, &sa, NULL) < 0) {
248         SHARUtils::log_error();
249         return -1;
250     }
251     return 0;
252 }
253
254 static void usage(char* whoami)
255 {
256     fprintf(stderr, "usage: %s [-fcdt]\n", whoami);
257     fprintf(stderr, "  -c\tconfig file to use.\n");
258     fprintf(stderr, "  -d\tschema directory to use.\n");
259     fprintf(stderr, "  -t\tcheck configuration file for problems.\n");
260     fprintf(stderr, "  -f\tforce removal of listener socket.\n");
261     fprintf(stderr, "  -h\tprint this help message.\n");
262     exit(1);
263 }
264
265 static int parse_args(int argc, char* argv[])
266 {
267     int opt;
268
269     while ((opt = getopt(argc, argv, "cdfFh")) > 0) {
270         switch (opt) {
271             case 'c':
272                 shar_config=optarg;
273                 break;
274             case 'd':
275                 shar_schemadir=optarg;
276                 break;
277             case 'f':
278                 unlink_socket = 1;
279                 break;
280             case 't':
281                 shar_checkonly=true;
282                 break;
283             default:
284                 return -1;
285         }
286     }
287     return 0;
288 }
289
290 int main(int argc, char *argv[])
291 {
292     IListener::ShibSocket sock;
293     ShibRPCProtocols protos[] = {
294         { SHIBRPC_PROG, SHIBRPC_VERS_1, shibrpc_prog_1 }
295     };
296
297     if (setup_signals() != 0)
298         return -1;
299
300     if (parse_args(argc, argv) != 0)
301         usage(argv[0]);
302
303     if (!shar_config)
304         shar_config=getenv("SHIBCONFIG");
305     if (!shar_schemadir)
306         shar_schemadir=getenv("SHIBSCHEMAS");
307     if (!shar_schemadir)
308         shar_schemadir=SHIB_SCHEMAS;
309     if (!shar_config)
310         shar_config=SHIB_CONFIG;
311
312     // initialize the shib-target library
313     ShibTargetConfig& conf=ShibTargetConfig::getConfig();
314     conf.setFeatures(
315         ShibTargetConfig::Listener |
316         ShibTargetConfig::SessionCache |
317         ShibTargetConfig::Metadata |
318         ShibTargetConfig::Trust |
319         ShibTargetConfig::Credentials |
320         ShibTargetConfig::AAP |
321         ShibTargetConfig::SHARExtensions |
322         (shar_checkonly ? (ShibTargetConfig::SHIREExtensions | ShibTargetConfig::RequestMapper) : 0)
323         );
324     if (!conf.init(shar_schemadir,shar_config)) {
325         fprintf(stderr, "configuration is invalid, check log for specific problems\n");
326         return -2;
327     }
328
329     if (shar_checkonly)
330         fprintf(stderr, "overall configuration is loadable, check log for non-fatal problems\n");
331     else {
332         const IListener* listener=conf.getINI()->getListener();
333         
334         // Create the SHAR listener socket
335         if (!listener->create(sock))
336             return -3;
337     
338         // Bind to the proper port
339         if (!listener->bind(sock, unlink_socket==1))
340             return -4;
341     
342         // Initialize the SHAR Utilitites
343         SHARUtils::init();
344     
345         // Run the listener
346         shar_svc_run(sock, ArrayIterator<ShibRPCProtocols>(protos,1));
347     
348         /* Finalize the SHAR, close all clients */
349         SHARUtils::fini();
350         fprintf(stderr, "shar utils finalized\n");
351     
352         listener->close(sock);
353         fprintf(stderr, "shib socket closed\n");
354     }
355     
356     conf.shutdown();
357     fprintf(stderr, "shar shutdown complete\n");
358     return 0;
359 }
360
361 #endif