2 * The Shibboleth License, Version 1.
4 * University Corporation for Advanced Internet Development, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
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.
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
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.
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.
51 * shar.cpp -- the SHAR "main" code. All the functionality is elsewhere
52 * (in case you want to turn this into a library later).
54 * Created By: Derek Atkins <derek@ihtfp.com>
59 // eventually we might be able to support autoconf via cygwin...
60 #if defined (_MSC_VER) || defined(__BORLANDC__)
61 # include "config_win32.h"
68 #include <sys/select.h>
75 #include "shar-utils.h"
76 #include <log4cpp/Category.hh>
80 using namespace shibboleth;
81 using namespace shibtarget;
82 using namespace log4cpp;
85 # define FD_SETSIZE 1024
88 extern "C" void shibrpc_prog_2(struct svc_req* rqstp, register SVCXPRT* transp);
90 // Declare a "MemoryListener" that our server methods will forward their work to.
91 IListener* g_MemoryListener = NULL;
94 const char* shar_config = NULL;
95 const char* shar_schemadir = NULL;
96 bool shar_checkonly = false;
97 static int unlink_socket = 0;
99 static bool new_connection(IListener::ShibSocket& listener, const Iterator<ShibRPCProtocols>& protos)
101 IListener::ShibSocket sock;
103 // Accept the connection.
104 if (!ShibTargetConfig::getConfig().getINI()->getListener()->accept(listener, sock))
107 // We throw away the result because the children manage themselves...
109 new SharChild(sock,protos);
112 saml::NDC ndc("new_connection");
113 Category& log=Category::getInstance("shibd");
114 log.crit("error starting new child thread to service request");
120 static void shar_svc_run(IListener::ShibSocket& listener, const Iterator<ShibRPCProtocols>& protos)
123 saml::NDC ndc("shar_svc_run");
125 Category& log=Category::getInstance("shibd");
130 FD_SET(listener, &readfds);
131 struct timeval tv = { 0, 0 };
134 switch (select(listener + 1, &readfds, 0, 0, &tv)) {
140 if (errno == EINTR) continue;
141 SHARUtils::log_error();
142 log.error("select() on main listener socket failed");
149 if (!new_connection(listener, protos))
150 log.crit("new_connection failed");
153 log.info("shar_svc_run ended");
158 int real_main(int preinit)
160 static IListener::ShibSocket sock;
161 ShibRPCProtocols protos[1] = {
162 { SHIBRPC_PROG, SHIBRPC_VERS_2, shibrpc_prog_2 }
165 ShibTargetConfig& conf=ShibTargetConfig::getConfig();
168 // initialize the shib-target library
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)
180 shar_config=getenv("SHIBCONFIG");
182 shar_schemadir=getenv("SHIBSCHEMAS");
184 shar_schemadir=SHIB_SCHEMAS;
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");
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");
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) {
203 fprintf(stderr, "MemoryListener plugin failed to load");
208 const IListener* listener=conf.getINI()->getListener();
210 // Create the SHAR listener socket
211 if (!listener->create(sock)) {
212 delete g_MemoryListener;
217 // Bind to the proper port
218 if (!listener->bind(sock)) {
219 delete g_MemoryListener;
224 // Initialize the SHAR Utilitites
229 if (!shar_checkonly) {
230 shar_svc_run(sock, ArrayIterator<ShibRPCProtocols>(protos,1));
231 fprintf(stdout,"shar_svc_run returned\n");
233 // Finalize the SHAR, close all clients
235 conf.getINI()->getListener()->close(sock);
238 delete g_MemoryListener;
240 fprintf(stdout, "shibd shutdown complete\n");
247 static void term_handler(int arg)
252 static int setup_signals(void)
254 NDC ndc("setup_signals");
257 memset(&sa, 0, sizeof (sa));
258 sa.sa_handler = SIG_IGN;
259 sa.sa_flags = SA_RESTART;
261 if (sigaction(SIGPIPE, &sa, NULL) < 0) {
262 SHARUtils::log_error();
266 memset(&sa, 0, sizeof (sa));
267 sa.sa_handler = term_handler;
268 sa.sa_flags = SA_RESTART;
270 if (sigaction(SIGHUP, &sa, NULL) < 0) {
271 SHARUtils::log_error();
274 if (sigaction(SIGINT, &sa, NULL) < 0) {
275 SHARUtils::log_error();
278 if (sigaction(SIGQUIT, &sa, NULL) < 0) {
279 SHARUtils::log_error();
282 if (sigaction(SIGTERM, &sa, NULL) < 0) {
283 SHARUtils::log_error();
289 static void usage(char* whoami)
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");
300 static int parse_args(int argc, char* argv[])
304 while ((opt = getopt(argc, argv, "c:d:fth")) > 0) {
310 shar_schemadir=optarg;
325 int main(int argc, char *argv[])
327 IListener::ShibSocket sock;
328 ShibRPCProtocols protos[] = {
329 { SHIBRPC_PROG, SHIBRPC_VERS_2, shibrpc_prog_2 }
332 if (setup_signals() != 0)
335 if (parse_args(argc, argv) != 0)
339 shar_config=getenv("SHIBCONFIG");
341 shar_schemadir=getenv("SHIBSCHEMAS");
343 shar_schemadir=SHIB_SCHEMAS;
345 shar_config=SHIB_CONFIG;
347 // initialize the shib-target library
348 ShibTargetConfig& conf=ShibTargetConfig::getConfig();
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)
359 if (!conf.init(shar_schemadir) || !conf.load(shar_config)) {
360 fprintf(stderr, "configuration is invalid, check console for specific problems\n");
365 fprintf(stderr, "overall configuration is loadable, check console for non-fatal problems\n");
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) {
373 fprintf(stderr, "MemoryListener plugin failed to load");
378 const IListener* listener=conf.getINI()->getListener();
380 // Create the SHAR listener socket
381 if (!listener->create(sock)) {
382 delete g_MemoryListener;
387 // Bind to the proper port
388 if (!listener->bind(sock, unlink_socket==1)) {
389 delete g_MemoryListener;
394 // Initialize the SHAR Utilitites
398 shar_svc_run(sock, ArrayIterator<ShibRPCProtocols>(protos,1));
400 /* Finalize the SHAR, close all clients */
402 fprintf(stderr, "shar utils finalized\n");
404 listener->close(sock);
405 fprintf(stderr, "shib socket closed\n");
409 fprintf(stderr, "shibd shutdown complete\n");