Fix Unix side
[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_2(struct svc_req* rqstp, register SVCXPRT* transp);
89
90 // Declare a "MemoryListener" that our server methods will forward their work to.
91 IListener* g_MemoryListener = NULL;
92
93 int shar_run = 1;
94 const char* shar_config = NULL;
95 const char* shar_schemadir = NULL;
96 bool shar_checkonly = false;
97 static int unlink_socket = 0;
98
99 static bool new_connection(IListener::ShibSocket& listener, const Iterator<ShibRPCProtocols>& protos)
100 {
101     IListener::ShibSocket sock;
102
103     // Accept the connection.
104     if (!ShibTargetConfig::getConfig().getINI()->getListener()->accept(listener, sock))
105         return false;
106
107     // We throw away the result because the children manage themselves...
108     try {
109         new SharChild(sock,protos);
110     }
111     catch (...) {
112         saml::NDC ndc("new_connection");
113         Category& log=Category::getInstance("shibd");
114         log.crit("error starting new child thread to service request");
115         return false;
116     }
117     return true;
118 }
119
120 static void shar_svc_run(IListener::ShibSocket& listener, const Iterator<ShibRPCProtocols>& protos)
121 {
122 #ifdef _DEBUG
123     saml::NDC ndc("shar_svc_run");
124 #endif
125     Category& log=Category::getInstance("shibd");
126
127     while (shar_run) {
128         fd_set readfds;
129         FD_ZERO(&readfds);
130         FD_SET(listener, &readfds);
131         struct timeval tv = { 0, 0 };
132         tv.tv_sec = 5;
133     
134         switch (select(listener + 1, &readfds, 0, 0, &tv)) {
135 #ifdef WIN32
136             case SOCKET_ERROR:
137 #else
138             case -1:
139 #endif
140                 if (errno == EINTR) continue;
141                 SHARUtils::log_error();
142                 log.error("select() on main listener socket failed");
143                 return;
144         
145             case 0:
146                 continue;
147         
148             default:
149                 if (!new_connection(listener, protos))
150                     log.crit("new_connection failed");
151         }
152     }
153     log.info("shar_svc_run ended");
154 }
155
156 #ifdef WIN32
157
158 int real_main(int preinit)
159 {
160     static IListener::ShibSocket sock;
161     ShibRPCProtocols protos[1] = {
162         { SHIBRPC_PROG, SHIBRPC_VERS_2, shibrpc_prog_2 }
163     };
164
165     ShibTargetConfig& conf=ShibTargetConfig::getConfig();
166     if (preinit) {
167
168         // initialize the shib-target library
169         conf.setFeatures(
170             ShibTargetConfig::Listener |
171             ShibTargetConfig::Caching |
172             ShibTargetConfig::Metadata |
173             ShibTargetConfig::Trust |
174             ShibTargetConfig::Credentials |
175             ShibTargetConfig::AAP |
176             ShibTargetConfig::GlobalExtensions |
177             (shar_checkonly ? (ShibTargetConfig::LocalExtensions | ShibTargetConfig::RequestMapper) : ShibTargetConfig::Logging)
178             );
179         if (!shar_config)
180             shar_config=getenv("SHIBCONFIG");
181         if (!shar_schemadir)
182             shar_schemadir=getenv("SHIBSCHEMAS");
183         if (!shar_schemadir)
184             shar_schemadir=SHIB_SCHEMAS;
185         if (!shar_config)
186             shar_config=SHIB_CONFIG;
187         if (!conf.init(shar_schemadir) || !conf.load(shar_config)) {
188             fprintf(stderr, "configuration is invalid, see console for specific problems\n");
189             return -2;
190         }
191
192         // If just a test run, bail.
193         if (shar_checkonly) {
194             fprintf(stdout, "overall configuration is loadable, check console for non-fatal problems\n");
195             return 0;
196         }
197         
198         // Build an internal "listener" to handle the work.
199         IPlugIn* plugin=SAMLConfig::getConfig().getPlugMgr().newPlugin(shibtarget::XML::MemoryListenerType,NULL);
200         g_MemoryListener=dynamic_cast<IListener*>(plugin);
201         if (!g_MemoryListener) {
202             delete plugin;
203             fprintf(stderr, "MemoryListener plugin failed to load");
204             conf.shutdown();
205             return -3;
206         }
207
208         const IListener* listener=conf.getINI()->getListener();
209         
210         // Create the SHAR listener socket
211         if (!listener->create(sock)) {
212             delete g_MemoryListener;
213             conf.shutdown();
214             return -4;
215         }
216
217         // Bind to the proper port
218         if (!listener->bind(sock)) {
219             delete g_MemoryListener;
220             conf.shutdown();
221             return -5;
222         }
223
224         // Initialize the SHAR Utilitites
225         SHARUtils::init();
226     }
227     else {
228         // Run the listener
229         if (!shar_checkonly) {
230             shar_svc_run(sock, ArrayIterator<ShibRPCProtocols>(protos,1));
231             fprintf(stdout,"shar_svc_run returned\n");
232
233             // Finalize the SHAR, close all clients
234             SHARUtils::fini();
235             conf.getINI()->getListener()->close(sock);
236         }
237
238         delete g_MemoryListener;
239         conf.shutdown();
240         fprintf(stdout, "shibd shutdown complete\n");
241     }
242     return 0;
243 }
244
245 #else
246
247 static void term_handler(int arg)
248 {
249     shar_run = 0;
250 }
251
252 static int setup_signals(void)
253 {
254     NDC ndc("setup_signals");
255     
256     struct sigaction sa;
257     memset(&sa, 0, sizeof (sa));
258     sa.sa_handler = SIG_IGN;
259     sa.sa_flags = SA_RESTART;
260
261     if (sigaction(SIGPIPE, &sa, NULL) < 0) {
262         SHARUtils::log_error();
263         return -1;
264     }
265
266     memset(&sa, 0, sizeof (sa));
267     sa.sa_handler = term_handler;
268     sa.sa_flags = SA_RESTART;
269
270     if (sigaction(SIGHUP, &sa, NULL) < 0) {
271         SHARUtils::log_error();
272         return -1;
273     }
274     if (sigaction(SIGINT, &sa, NULL) < 0) {
275         SHARUtils::log_error();
276         return -1;
277     }
278     if (sigaction(SIGQUIT, &sa, NULL) < 0) {
279         SHARUtils::log_error();
280         return -1;
281     }
282     if (sigaction(SIGTERM, &sa, NULL) < 0) {
283         SHARUtils::log_error();
284         return -1;
285     }
286     return 0;
287 }
288
289 static void usage(char* whoami)
290 {
291     fprintf(stderr, "usage: %s [-fcdt]\n", whoami);
292     fprintf(stderr, "  -c\tconfig file to use.\n");
293     fprintf(stderr, "  -d\tschema directory to use.\n");
294     fprintf(stderr, "  -t\tcheck configuration file for problems.\n");
295     fprintf(stderr, "  -f\tforce removal of listener socket.\n");
296     fprintf(stderr, "  -h\tprint this help message.\n");
297     exit(1);
298 }
299
300 static int parse_args(int argc, char* argv[])
301 {
302     int opt;
303
304     while ((opt = getopt(argc, argv, "c:d:fth")) > 0) {
305         switch (opt) {
306             case 'c':
307                 shar_config=optarg;
308                 break;
309             case 'd':
310                 shar_schemadir=optarg;
311                 break;
312             case 'f':
313                 unlink_socket = 1;
314                 break;
315             case 't':
316                 shar_checkonly=true;
317                 break;
318             default:
319                 return -1;
320         }
321     }
322     return 0;
323 }
324
325 int main(int argc, char *argv[])
326 {
327     IListener::ShibSocket sock;
328     ShibRPCProtocols protos[] = {
329         { SHIBRPC_PROG, SHIBRPC_VERS_2, shibrpc_prog_2 }
330     };
331
332     if (setup_signals() != 0)
333         return -1;
334
335     if (parse_args(argc, argv) != 0)
336         usage(argv[0]);
337
338     if (!shar_config)
339         shar_config=getenv("SHIBCONFIG");
340     if (!shar_schemadir)
341         shar_schemadir=getenv("SHIBSCHEMAS");
342     if (!shar_schemadir)
343         shar_schemadir=SHIB_SCHEMAS;
344     if (!shar_config)
345         shar_config=SHIB_CONFIG;
346
347     // initialize the shib-target library
348     ShibTargetConfig& conf=ShibTargetConfig::getConfig();
349     conf.setFeatures(
350         ShibTargetConfig::Listener |
351         ShibTargetConfig::Caching |
352         ShibTargetConfig::Metadata |
353         ShibTargetConfig::Trust |
354         ShibTargetConfig::Credentials |
355         ShibTargetConfig::AAP |
356         ShibTargetConfig::GlobalExtensions |
357         (shar_checkonly ? (ShibTargetConfig::LocalExtensions | ShibTargetConfig::RequestMapper) : ShibTargetConfig::Logging)
358         );
359     if (!conf.init(shar_schemadir) || !conf.load(shar_config)) {
360         fprintf(stderr, "configuration is invalid, check console for specific problems\n");
361         return -2;
362     }
363
364     if (shar_checkonly)
365         fprintf(stderr, "overall configuration is loadable, check console for non-fatal problems\n");
366     else {
367
368         // Build an internal "listener" to handle the work.
369         IPlugIn* plugin=SAMLConfig::getConfig().getPlugMgr().newPlugin(shibtarget::XML::MemoryListenerType,NULL);
370         g_MemoryListener=dynamic_cast<IListener*>(plugin);
371         if (!g_MemoryListener) {
372             delete plugin;
373             fprintf(stderr, "MemoryListener plugin failed to load");
374             conf.shutdown();
375             return -3;
376         }
377
378         const IListener* listener=conf.getINI()->getListener();
379         
380         // Create the SHAR listener socket
381         if (!listener->create(sock)) {
382             delete g_MemoryListener;
383             conf.shutdown();
384             return -4;
385         }
386     
387         // Bind to the proper port
388         if (!listener->bind(sock, unlink_socket==1)) {
389             delete g_MemoryListener;
390             conf.shutdown();
391             return -5;
392         }
393     
394         // Initialize the SHAR Utilitites
395         SHARUtils::init();
396     
397         // Run the listener
398         shar_svc_run(sock, ArrayIterator<ShibRPCProtocols>(protos,1));
399     
400         /* Finalize the SHAR, close all clients */
401         SHARUtils::fini();
402         fprintf(stderr, "shar utils finalized\n");
403     
404         listener->close(sock);
405         fprintf(stderr, "shib socket closed\n");
406     }
407
408     conf.shutdown();
409     fprintf(stderr, "shibd shutdown complete\n");
410     return 0;
411 }
412
413 #endif