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