Make the parameter globals a bit more unique.
[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 static int unlink_socket = 0;
94
95 static bool new_connection(IListener::ShibSocket& listener, const Iterator<ShibRPCProtocols>& protos)
96 {
97     IListener::ShibSocket sock;
98
99     // Accept the connection.
100     if (!ShibTargetConfig::getConfig().getINI()->getListener()->accept(listener, sock))
101         return false;
102
103     // We throw away the result because the children manage themselves...
104     new SharChild(sock,protos);
105     return true;
106 }
107
108 static void shar_svc_run(IListener::ShibSocket& listener, const Iterator<ShibRPCProtocols>& protos)
109 {
110     NDC ndc("shar_svc_run");
111     Category& log=Category::getInstance("SHAR");
112
113     while (shar_run) {
114         fd_set readfds;
115         FD_ZERO(&readfds);
116         FD_SET(listener, &readfds);
117         struct timeval tv = { 0, 0 };
118         tv.tv_sec = 5;
119     
120         switch (select(FD_SETSIZE, &readfds, 0, 0, &tv)) {
121             case -1:
122                 if (errno == EINTR) continue;
123                 SHARUtils::log_error();
124                 return;
125         
126             case 0:
127                 continue;
128         
129             default:
130                 if (!new_connection(listener, protos))
131                     log.error("new_connection failed");
132         }
133     }
134     log.info("shar_svc_run ended");
135 }
136
137 #ifdef WIN32
138
139 int real_main(int preinit)
140 {
141     static IListener::ShibSocket sock;
142     ShibRPCProtocols protos[1] = {
143         { SHIBRPC_PROG, SHIBRPC_VERS_1, shibrpc_prog_1 }
144     };
145
146     ShibTargetConfig& conf=ShibTargetConfig::getConfig();
147     if (preinit) {
148
149         // initialize the shib-target library
150         conf.setFeatures(
151             ShibTargetConfig::Listener |
152             ShibTargetConfig::SessionCache |
153             ShibTargetConfig::Metadata |
154             ShibTargetConfig::Trust |
155             ShibTargetConfig::Credentials |
156             ShibTargetConfig::AAP |
157             ShibTargetConfig::SHARExtensions
158             );
159         if (!shar_config)
160             shar_config=getenv("SHIBCONFIG");
161         if (!shar_schemadir)
162             shar_schemadir=getenv("SHIBSCHEMAS");
163         if (!shar_schemadir)
164             shar_schemadir=SHIB_SCHEMAS;
165         if (!shar_config)
166             shar_config=SHIB_CONFIG;
167         if (!conf.init(shar_schemadir,shar_config))
168             return -2;
169
170         const IListener* listener=conf.getINI()->getListener();
171         
172         // Create the SHAR listener socket
173         if (!listener->create(sock))
174             return -3;
175
176         // Bind to the proper port
177         if (!listener->bind(sock))
178             return -4;
179
180         // Initialize the SHAR Utilitites
181         SHARUtils::init();
182     }
183     else {
184         // Run the listener
185         shar_svc_run(sock, ArrayIterator<ShibRPCProtocols>(protos,1));
186         fprintf(stderr,"shar_svc_run returned\n");
187
188         // Finalize the SHAR, close all clients
189         SHARUtils::fini();
190         conf.getINI()->getListener()->close(sock);
191         conf.shutdown();
192     }
193     return 0;
194 }
195
196 #else
197
198 static void term_handler(int arg)
199 {
200     shar_run = 0;
201 }
202
203 static int setup_signals(void)
204 {
205     NDC ndc("setup_signals");
206     
207     struct sigaction sa;
208     memset(&sa, 0, sizeof (sa));
209     sa.sa_handler = SIG_IGN;
210     sa.sa_flags = SA_RESTART;
211
212     if (sigaction(SIGPIPE, &sa, NULL) < 0) {
213         SHARUtils::log_error();
214         return -1;
215     }
216
217     memset(&sa, 0, sizeof (sa));
218     sa.sa_handler = term_handler;
219     sa.sa_flags = SA_RESTART;
220
221     if (sigaction(SIGHUP, &sa, NULL) < 0) {
222         SHARUtils::log_error();
223         return -1;
224     }
225     if (sigaction(SIGINT, &sa, NULL) < 0) {
226         SHARUtils::log_error();
227         return -1;
228     }
229     if (sigaction(SIGQUIT, &sa, NULL) < 0) {
230         SHARUtils::log_error();
231         return -1;
232     }
233     if (sigaction(SIGTERM, &sa, NULL) < 0) {
234         SHARUtils::log_error();
235         return -1;
236     }
237     return 0;
238 }
239
240 static void usage(char* whoami)
241 {
242     fprintf(stderr, "usage: %s [-f]\n", whoami);
243     fprintf(stderr, "  -c\tconfig file to use.\n");
244     fprintf(stderr, "  -d\tschema directory to use.\n");
245     fprintf(stderr, "  -f\tforce removal of listener socket.\n");
246     fprintf(stderr, "  -h\tprint this help message.\n");
247     exit(1);
248 }
249
250 static int parse_args(int argc, char* argv[])
251 {
252     int opt;
253
254     while ((opt = getopt(argc, argv, "cdfFh")) > 0) {
255         switch (opt) {
256             case 'c':
257                 shar_config=optarg;
258                 break;
259             case 'd':
260                 shar_schemadir=optarg;
261                 break;
262             case 'f':
263                 unlink_socket = 1;
264                 break;
265             default:
266                 return -1;
267         }
268     }
269     return 0;
270 }
271
272 int main(int argc, char *argv[])
273 {
274     IListener::ShibSocket sock;
275     ShibRPCProtocols protos[] = {
276         { SHIBRPC_PROG, SHIBRPC_VERS_1, shibrpc_prog_1 }
277     };
278
279     if (setup_signals() != 0)
280         return -1;
281
282     if (parse_args(argc, argv) != 0)
283         usage(argv[0]);
284
285     if (!shar_config)
286         shar_config=getenv("SHIBCONFIG");
287     if (!shar_schemadir)
288         shar_schemadir=getenv("SHIBSCHEMAS");
289     if (!shar_schemadir)
290         shar_schemadir=SHIB_SCHEMAS;
291     if (!shar_config)
292         shar_config=SHIB_CONFIG;
293
294     // initialize the shib-target library
295     ShibTargetConfig& conf=ShibTargetConfig::getConfig();
296     conf.setFeatures(
297         ShibTargetConfig::Listener |
298         ShibTargetConfig::SessionCache |
299         ShibTargetConfig::Metadata |
300         ShibTargetConfig::Trust |
301         ShibTargetConfig::Credentials |
302         ShibTargetConfig::AAP |
303         ShibTargetConfig::SHARExtensions
304         );
305     if (!conf.init(shar_schemadir,shar_config))
306         return -2;
307
308     const IListener* listener=conf.getINI()->getListener();
309     
310     // Create the SHAR listener socket
311     if (!listener->create(sock))
312         return -3;
313
314     // Bind to the proper port
315     if (!listener->bind(sock, unlink_socket==1))
316         return -4;
317
318     // Initialize the SHAR Utilitites
319     SHARUtils::init();
320
321     // Run the listener
322     shar_svc_run(sock, ArrayIterator<ShibRPCProtocols>(protos,1));
323
324     /* Finalize the SHAR, close all clients */
325     SHARUtils::fini();
326     fprintf(stderr, "shar utils finalized\n");
327
328     listener->close(sock);
329     fprintf(stderr, "shib socket closed\n");
330
331     conf.shutdown();
332     fprintf(stderr, "shar finished.  bye bye.\n");
333     return 0;
334 }
335
336 #endif