Replacing the original charon with a small wrapper around libcharon.
[strongswan.git] / src / charon / charon.c
1 /*
2 * Copyright (C) 2006-2010 Tobias Brunner
3 * Copyright (C) 2005-2009 Martin Willi
4 * Copyright (C) 2006 Daniel Roethlisberger
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include <stdio.h>
20 #ifdef HAVE_PRCTL
21 #include <sys/prctl.h>
22 #endif
23 #define _POSIX_PTHREAD_SEMANTICS /* for two param sigwait on OpenSolaris */
24 #include <signal.h>
25 #undef _POSIX_PTHREAD_SEMANTICS
26 #include <pthread.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include <getopt.h>
31 #include <pwd.h>
32 #include <grp.h>
33
34 #include <daemon.h>
35
36 #include <library.h>
37 #include <utils/backtrace.h>
38 #include <threading/thread.h>
39
40 /**
41 * PID file, in which charon stores its process id
42 */
43 #define PID_FILE IPSEC_PIDDIR "/charon.pid"
44
45 /**
46 * hook in library for debugging messages
47 */
48 extern void (*dbg) (int level, char *fmt, ...);
49
50 /**
51 * Logging hook for library logs, using stderr output
52 */
53 static void dbg_stderr(int level, char *fmt, ...)
54 {
55 va_list args;
56
57 if (level <= 1)
58 {
59 va_start(args, fmt);
60 fprintf(stderr, "00[LIB] ");
61 vfprintf(stderr, fmt, args);
62 fprintf(stderr, "\n");
63 va_end(args);
64 }
65 }
66
67 /**
68 * Run the daemon and handle unix signals
69 */
70 static void run()
71 {
72 sigset_t set;
73
74 /* handle SIGINT, SIGHUP ans SIGTERM in this handler */
75 sigemptyset(&set);
76 sigaddset(&set, SIGINT);
77 sigaddset(&set, SIGHUP);
78 sigaddset(&set, SIGTERM);
79 sigprocmask(SIG_BLOCK, &set, NULL);
80
81 while (TRUE)
82 {
83 int sig;
84 int error;
85
86 error = sigwait(&set, &sig);
87 if (error)
88 {
89 DBG1(DBG_DMN, "error %d while waiting for a signal", error);
90 return;
91 }
92 switch (sig)
93 {
94 case SIGHUP:
95 {
96 DBG1(DBG_DMN, "signal of type SIGHUP received. Ignored");
97 break;
98 }
99 case SIGINT:
100 {
101 DBG1(DBG_DMN, "signal of type SIGINT received. Shutting down");
102 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
103 return;
104 }
105 case SIGTERM:
106 {
107 DBG1(DBG_DMN, "signal of type SIGTERM received. Shutting down");
108 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
109 return;
110 }
111 default:
112 {
113 DBG1(DBG_DMN, "unknown signal %d received. Ignored", sig);
114 break;
115 }
116 }
117 }
118 }
119
120 /**
121 * drop daemon capabilities
122 */
123 static bool drop_capabilities()
124 {
125 #ifdef HAVE_PRCTL
126 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
127 #endif
128
129 if (setgid(charon->gid) != 0)
130 {
131 DBG1(DBG_DMN, "change to unprivileged group failed");
132 return FALSE;
133 }
134 if (setuid(charon->uid) != 0)
135 {
136 DBG1(DBG_DMN, "change to unprivileged user failed");
137 return FALSE;
138 }
139 if (!charon->drop_capabilities(charon))
140 {
141 DBG1(DBG_DMN, "unable to drop daemon capabilities");
142 return FALSE;
143 }
144 return TRUE;
145 }
146
147 /**
148 * lookup UID and GID
149 */
150 static bool lookup_uid_gid()
151 {
152 #ifdef IPSEC_USER
153 {
154 char buf[1024];
155 struct passwd passwd, *pwp;
156
157 if (getpwnam_r(IPSEC_USER, &passwd, buf, sizeof(buf), &pwp) != 0 ||
158 pwp == NULL)
159 {
160 DBG1(DBG_DMN, "resolving user '"IPSEC_USER"' failed");
161 return FALSE;
162 }
163 charon->uid = pwp->pw_uid;
164 }
165 #endif
166 #ifdef IPSEC_GROUP
167 {
168 char buf[1024];
169 struct group group, *grp;
170
171 if (getgrnam_r(IPSEC_GROUP, &group, buf, sizeof(buf), &grp) != 0 ||
172 grp == NULL)
173 {
174 DBG1(DBG_DMN, "resolving group '"IPSEC_GROUP"' failed");
175 return FALSE;
176 }
177 charon->gid = grp->gr_gid;
178 }
179 #endif
180 return TRUE;
181 }
182
183 /**
184 * Handle SIGSEGV/SIGILL signals raised by threads
185 */
186 static void segv_handler(int signal)
187 {
188 backtrace_t *backtrace;
189
190 DBG1(DBG_DMN, "thread %u received %d", thread_current_id(), signal);
191 backtrace = backtrace_create(2);
192 backtrace->log(backtrace, stderr);
193 backtrace->destroy(backtrace);
194
195 DBG1(DBG_DMN, "killing ourself, received critical signal");
196 abort();
197 }
198
199 /**
200 * Check/create PID file, return TRUE if already running
201 */
202 static bool check_pidfile()
203 {
204 struct stat stb;
205 FILE *file;
206
207 if (stat(PID_FILE, &stb) == 0)
208 {
209 file = fopen(PID_FILE, "r");
210 if (file)
211 {
212 char buf[64];
213 pid_t pid = 0;
214
215 memset(buf, 0, sizeof(buf));
216 if (fread(buf, 1, sizeof(buf), file))
217 {
218 pid = atoi(buf);
219 }
220 fclose(file);
221 if (pid && kill(pid, 0) == 0)
222 { /* such a process is running */
223 return TRUE;
224 }
225 }
226 DBG1(DBG_DMN, "removing pidfile '"PID_FILE"', process not running");
227 unlink(PID_FILE);
228 }
229
230 /* create new pidfile */
231 file = fopen(PID_FILE, "w");
232 if (file)
233 {
234 fprintf(file, "%d\n", getpid());
235 ignore_result(fchown(fileno(file), charon->uid, charon->gid));
236 fclose(file);
237 }
238 return FALSE;
239 }
240
241 /**
242 * print command line usage and exit
243 */
244 static void usage(const char *msg)
245 {
246 if (msg != NULL && *msg != '\0')
247 {
248 fprintf(stderr, "%s\n", msg);
249 }
250 fprintf(stderr, "Usage: charon\n"
251 " [--help]\n"
252 " [--version]\n"
253 " [--use-syslog]\n"
254 " [--debug-<type> <level>]\n"
255 " <type>: log context type (dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib)\n"
256 " <level>: log verbosity (-1 = silent, 0 = audit, 1 = control,\n"
257 " 2 = controlmore, 3 = raw, 4 = private)\n"
258 "\n"
259 );
260 exit(msg == NULL? 0 : 1);
261 }
262
263 /**
264 * Main function, starts the daemon.
265 */
266 int main(int argc, char *argv[])
267 {
268 struct sigaction action;
269 bool use_syslog = FALSE;
270 level_t levels[DBG_MAX];
271 int group;
272
273 /* logging for library during initialization, as we have no bus yet */
274 dbg = dbg_stderr;
275
276 /* initialize library */
277 if (!library_init(NULL))
278 {
279 library_deinit();
280 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
281 }
282
283 if (lib->integrity &&
284 !lib->integrity->check_file(lib->integrity, "charon", argv[0]))
285 {
286 dbg_stderr(1, "integrity check of charon failed");
287 library_deinit();
288 exit(SS_RC_DAEMON_INTEGRITY);
289 }
290
291 if (!libcharon_init())
292 {
293 dbg_stderr(1, "initialization failed - aborting charon");
294 libcharon_deinit();
295 library_deinit();
296 exit(SS_RC_INITIALIZATION_FAILED);
297 }
298
299 /* use CTRL loglevel for default */
300 for (group = 0; group < DBG_MAX; group++)
301 {
302 levels[group] = LEVEL_CTRL;
303 }
304
305 /* handle arguments */
306 for (;;)
307 {
308 struct option long_opts[] = {
309 { "help", no_argument, NULL, 'h' },
310 { "version", no_argument, NULL, 'v' },
311 { "use-syslog", no_argument, NULL, 'l' },
312 /* TODO: handle "debug-all" */
313 { "debug-dmn", required_argument, &group, DBG_DMN },
314 { "debug-mgr", required_argument, &group, DBG_MGR },
315 { "debug-ike", required_argument, &group, DBG_IKE },
316 { "debug-chd", required_argument, &group, DBG_CHD },
317 { "debug-job", required_argument, &group, DBG_JOB },
318 { "debug-cfg", required_argument, &group, DBG_CFG },
319 { "debug-knl", required_argument, &group, DBG_KNL },
320 { "debug-net", required_argument, &group, DBG_NET },
321 { "debug-enc", required_argument, &group, DBG_ENC },
322 { "debug-lib", required_argument, &group, DBG_LIB },
323 { 0,0,0,0 }
324 };
325
326 int c = getopt_long(argc, argv, "", long_opts, NULL);
327 switch (c)
328 {
329 case EOF:
330 break;
331 case 'h':
332 usage(NULL);
333 break;
334 case 'v':
335 printf("Linux strongSwan %s\n", VERSION);
336 exit(0);
337 case 'l':
338 use_syslog = TRUE;
339 continue;
340 case 0:
341 /* option is in group */
342 levels[group] = atoi(optarg);
343 continue;
344 default:
345 usage("");
346 break;
347 }
348 break;
349 }
350
351 if (!lookup_uid_gid())
352 {
353 dbg_stderr(1, "invalid uid/gid - aborting charon");
354 libcharon_deinit();
355 library_deinit();
356 exit(SS_RC_INITIALIZATION_FAILED);
357 }
358
359 /* initialize daemon */
360 if (!charon->initialize(charon, use_syslog, levels))
361 {
362 DBG1(DBG_DMN, "initialization failed - aborting charon");
363 libcharon_deinit();
364 library_deinit();
365 exit(SS_RC_INITIALIZATION_FAILED);
366 }
367
368 if (check_pidfile())
369 {
370 DBG1(DBG_DMN, "charon already running (\""PID_FILE"\" exists)");
371 libcharon_deinit();
372 library_deinit();
373 exit(-1);
374 }
375
376 if (!drop_capabilities())
377 {
378 DBG1(DBG_DMN, "capability dropping failed - aborting charon");
379 libcharon_deinit();
380 library_deinit();
381 exit(SS_RC_INITIALIZATION_FAILED);
382 }
383
384 /* add handler for SEGV and ILL,
385 * INT, TERM and HUP are handled by sigwait() in run() */
386 action.sa_handler = segv_handler;
387 action.sa_flags = 0;
388 sigemptyset(&action.sa_mask);
389 sigaddset(&action.sa_mask, SIGINT);
390 sigaddset(&action.sa_mask, SIGTERM);
391 sigaddset(&action.sa_mask, SIGHUP);
392 sigaction(SIGSEGV, &action, NULL);
393 sigaction(SIGILL, &action, NULL);
394 sigaction(SIGBUS, &action, NULL);
395 action.sa_handler = SIG_IGN;
396 sigaction(SIGPIPE, &action, NULL);
397
398 pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
399
400 /* start daemon (i.e. the threads in the thread-pool) */
401 charon->start(charon);
402
403 /* main thread goes to run loop */
404 run();
405
406 /* normal termination, cleanup and exit */
407 libcharon_deinit();
408 library_deinit();
409
410 unlink(PID_FILE);
411
412 return 0;
413 }
414