b7a69567f67585182ca2299877b3d0cf94c6e732
[shibboleth/sp.git] / shibd / shibd.cpp
1 /*\r
2  *  Copyright 2001-2007 Internet2\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /*\r
18  * shar.cpp -- the shibd "main" code.  All the functionality is elsewhere\r
19  *\r
20  * Created By:  Derek Atkins <derek@ihtfp.com>\r
21  *\r
22  * $Id: shar.cpp 2164 2007-02-11 05:26:18 +0000 (Sun, 11 Feb 2007) cantor $\r
23  */\r
24 \r
25 \r
26 // eventually we might be able to support autoconf via cygwin...\r
27 #if defined (_MSC_VER) || defined(__BORLANDC__)\r
28 # include "config_win32.h"\r
29 #else\r
30 # include "config.h"\r
31 #endif\r
32 \r
33 #ifdef WIN32\r
34 # define _CRT_NONSTDC_NO_DEPRECATE 1\r
35 # define _CRT_SECURE_NO_DEPRECATE 1\r
36 #endif\r
37 \r
38 #include <shibsp/SPConfig.h>\r
39 \r
40 #ifdef HAVE_UNISTD_H\r
41 #include <unistd.h>\r
42 #include <sys/select.h>\r
43 #endif\r
44 \r
45 #include <stdio.h>\r
46 #include <signal.h>\r
47 #include <shibsp/ServiceProvider.h>\r
48 #include <shibsp/remoting/ListenerService.h>\r
49 #include <xercesc/util/XMLUniDefs.hpp>\r
50 #include <xmltooling/XMLToolingConfig.h>\r
51 #include <xmltooling/util/XMLConstants.h>\r
52 #include <xmltooling/util/XMLHelper.h>\r
53 \r
54 using namespace shibsp;\r
55 using namespace xmltooling;\r
56 using namespace std;\r
57 \r
58 bool shibd_shutdown = false;\r
59 const char* shar_config = NULL;\r
60 const char* shar_schemadir = NULL;\r
61 const char* shar_prefix = NULL;\r
62 bool shar_checkonly = false;\r
63 bool shar_version = false;\r
64 static int unlink_socket = 0;\r
65 const char* pidfile = NULL;\r
66 \r
67 #ifdef WIN32\r
68 \r
69 //#include <CRTDBG.H>\r
70 \r
71 #define nNoMansLandSize 4\r
72 typedef struct _CrtMemBlockHeader\r
73 {\r
74         struct _CrtMemBlockHeader * pBlockHeaderNext;\r
75         struct _CrtMemBlockHeader * pBlockHeaderPrev;\r
76         char *                      szFileName;\r
77         int                         nLine;\r
78         size_t                      nDataSize;\r
79         int                         nBlockUse;\r
80         long                        lRequest;\r
81         unsigned char               gap[nNoMansLandSize];\r
82         /* followed by:\r
83          *  unsigned char           data[nDataSize];\r
84          *  unsigned char           anotherGap[nNoMansLandSize];\r
85          */\r
86 } _CrtMemBlockHeader;\r
87 \r
88 /*\r
89 int MyAllocHook(int nAllocType, void *pvData,\r
90       size_t nSize, int nBlockUse, long lRequest,\r
91       const unsigned char * szFileName, int nLine)\r
92 {\r
93     if ( nBlockUse == _CRT_BLOCK )\r
94       return( TRUE );\r
95     if (nAllocType == _HOOK_FREE) {\r
96         _CrtMemBlockHeader* ptr = (_CrtMemBlockHeader*)(((_CrtMemBlockHeader *)pvData)-1);\r
97         if (ptr->nDataSize == 8192)\r
98             fprintf(stderr,"free  request %u size %u\n", ptr->lRequest, ptr->nDataSize);\r
99     }\r
100     else if (nAllocType == _HOOK_ALLOC && nSize == 8192)\r
101         fprintf(stderr,"%s request %u size %u\n", ((nAllocType == _HOOK_ALLOC) ? "alloc" : "realloc"), lRequest, nSize);\r
102     return (TRUE);\r
103 }\r
104 */\r
105 \r
106 int real_main(int preinit)\r
107 {\r
108     SPConfig& conf=SPConfig::getConfig();\r
109     if (preinit) {\r
110 \r
111         // Initialize the SP library.\r
112         conf.setFeatures(\r
113             SPConfig::Listener |\r
114             SPConfig::Caching |\r
115             SPConfig::Metadata |\r
116             SPConfig::Trust |\r
117             SPConfig::Credentials |\r
118             SPConfig::AttributeResolution |\r
119             SPConfig::Handlers |\r
120             SPConfig::OutOfProcess |\r
121             (shar_checkonly ? SPConfig::RequestMapping : SPConfig::Logging)\r
122             );\r
123         if (!conf.init(shar_schemadir, shar_prefix)) {\r
124             fprintf(stderr, "configuration is invalid, see console for specific problems\n");\r
125             return -1;\r
126         }\r
127 \r
128         if (!conf.instantiate(shar_config)) {\r
129             fprintf(stderr, "configuration is invalid, check console for specific problems\n");\r
130             conf.term();\r
131             return -2;\r
132         }\r
133 \r
134         // If just a test run, bail.\r
135         if (shar_checkonly) {\r
136             fprintf(stdout, "overall configuration is loadable, check console for non-fatal problems\n");\r
137             return 0;\r
138         }\r
139     }\r
140     else {\r
141 \r
142         //_CrtSetAllocHook(MyAllocHook);\r
143 \r
144         // Run the listener\r
145         if (!shar_checkonly) {\r
146 \r
147             // Run the listener.\r
148             if (!conf.getServiceProvider()->getListenerService()->run(&shibd_shutdown)) {\r
149                 fprintf(stderr, "listener failed to enter listen loop\n");\r
150                 return -3;\r
151             }\r
152         }\r
153 \r
154         conf.term();\r
155     }\r
156     return 0;\r
157 }\r
158 \r
159 #else\r
160 \r
161 static void term_handler(int arg)\r
162 {\r
163     shibd_shutdown = true;\r
164 }\r
165 \r
166 static int setup_signals(void)\r
167 {\r
168     struct sigaction sa;\r
169     memset(&sa, 0, sizeof (sa));\r
170     sa.sa_handler = SIG_IGN;\r
171     sa.sa_flags = SA_RESTART;\r
172 \r
173     if (sigaction(SIGPIPE, &sa, NULL) < 0) {\r
174         return -1;\r
175     }\r
176 \r
177     memset(&sa, 0, sizeof (sa));\r
178     sa.sa_handler = term_handler;\r
179     sa.sa_flags = SA_RESTART;\r
180 \r
181     if (sigaction(SIGHUP, &sa, NULL) < 0) {\r
182         return -1;\r
183     }\r
184     if (sigaction(SIGINT, &sa, NULL) < 0) {\r
185         return -1;\r
186     }\r
187     if (sigaction(SIGQUIT, &sa, NULL) < 0) {\r
188         return -1;\r
189     }\r
190     if (sigaction(SIGTERM, &sa, NULL) < 0) {\r
191         return -1;\r
192     }\r
193     return 0;\r
194 }\r
195 \r
196 static void usage(char* whoami)\r
197 {\r
198     fprintf(stderr, "usage: %s [-dcxtfpvh]\n", whoami);\r
199     fprintf(stderr, "  -d\tinstallation prefix to use.\n");\r
200     fprintf(stderr, "  -c\tconfig file to use.\n");\r
201     fprintf(stderr, "  -x\tXML schema catalogs to use.\n");\r
202     fprintf(stderr, "  -t\tcheck configuration file for problems.\n");\r
203     fprintf(stderr, "  -f\tforce removal of listener socket.\n");\r
204     fprintf(stderr, "  -p\tpid file to use.\n");\r
205     fprintf(stderr, "  -v\tprint software version.\n");\r
206     fprintf(stderr, "  -h\tprint this help message.\n");\r
207     exit(1);\r
208 }\r
209 \r
210 static int parse_args(int argc, char* argv[])\r
211 {\r
212     int opt;\r
213 \r
214     while ((opt = getopt(argc, argv, "d:c:x:p:ftvh")) > 0) {\r
215         switch (opt) {\r
216             case 'd':\r
217                 shar_prefix=optarg;\r
218                 break;\r
219             case 'c':\r
220                 shar_config=optarg;\r
221                 break;\r
222             case 'x':\r
223                 shar_schemadir=optarg;\r
224                 break;\r
225             case 'f':\r
226                 unlink_socket = 1;\r
227                 break;\r
228             case 't':\r
229                 shar_checkonly=true;\r
230                 break;\r
231             case 'v':\r
232                 shar_version=true;\r
233                 break;\r
234             case 'p':\r
235                 pidfile=optarg;\r
236                 break;\r
237             default:\r
238                 return -1;\r
239         }\r
240     }\r
241     return 0;\r
242 }\r
243 \r
244 int main(int argc, char *argv[])\r
245 {\r
246     if (parse_args(argc, argv) != 0)\r
247         usage(argv[0]);\r
248     else if (shar_version) {\r
249         fprintf(stdout, PACKAGE_STRING"\n");\r
250         return 0;\r
251     }\r
252 \r
253     if (setup_signals() != 0)\r
254         return -1;\r
255 \r
256     // initialize the shib-target library\r
257     SPConfig& conf=SPConfig::getConfig();\r
258     conf.setFeatures(\r
259         SPConfig::Listener |\r
260         SPConfig::Caching |\r
261         SPConfig::Metadata |\r
262         SPConfig::Trust |\r
263         SPConfig::Credentials |\r
264         SPConfig::AttributeResolution |\r
265         SPConfig::Handlers |\r
266         SPConfig::OutOfProcess |\r
267         (shar_checkonly ? SPConfig::RequestMapping : SPConfig::Logging)\r
268         );\r
269     if (!conf.init(shar_schemadir, shar_prefix)) {\r
270         fprintf(stderr, "configuration is invalid, check console for specific problems\n");\r
271         return -1;\r
272     }\r
273 \r
274     if (!conf.instantiate(shar_config)) {\r
275         fprintf(stderr, "configuration is invalid, check console for specific problems\n");\r
276         conf.term();\r
277         return -2;\r
278     }\r
279 \r
280     if (shar_checkonly)\r
281         fprintf(stderr, "overall configuration is loadable, check console for non-fatal problems\n");\r
282     else {\r
283 \r
284         // Write the pid file\r
285         if (pidfile) {\r
286             FILE* pidf = fopen(pidfile, "w");\r
287             if (pidf) {\r
288                 fprintf(pidf, "%d\n", getpid());\r
289                 fclose(pidf);\r
290             } else {\r
291                 perror(pidfile);  // keep running though\r
292             }\r
293         }\r
294 \r
295         // Run the listener\r
296         if (!conf.getServiceProvider()->getListenerService()->run(&shibd_shutdown)) {\r
297             fprintf(stderr, "listener failed to enter listen loop\n");\r
298             return -3;\r
299         }\r
300     }\r
301 \r
302     conf.term();\r
303     if (pidfile)\r
304         unlink(pidfile);\r
305     return 0;\r
306 }\r
307 \r
308 #endif\r