Avoid proxy for bypass_socket, enable_udp_decap
[strongswan.git] / src / charon-nm / charon-nm.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <stdio.h>
17 #include <syslog.h>
18 #include <signal.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21
22 #include <hydra.h>
23 #include <daemon.h>
24
25 #include <library.h>
26 #include <utils/backtrace.h>
27 #include <threading/thread.h>
28
29 #include <nm/nm_backend.h>
30
31 /**
32 * Hook in library for debugging messages
33 */
34 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
35
36 /**
37 * Simple logging hook for library logs, using syslog output
38 */
39 static void dbg_syslog(debug_t group, level_t level, char *fmt, ...)
40 {
41 if (level <= 1)
42 {
43 char buffer[8192], groupstr[4];
44 va_list args;
45
46 va_start(args, fmt);
47 /* write in memory buffer first */
48 vsnprintf(buffer, sizeof(buffer), fmt, args);
49 /* cache group name */
50 snprintf(groupstr, sizeof(groupstr), "%N", debug_names, group);
51 syslog(LOG_DAEMON|LOG_INFO, "00[%s] %s", groupstr, buffer);
52 va_end(args);
53 }
54 }
55
56 /**
57 * Run the daemon and handle unix signals
58 */
59 static void run()
60 {
61 sigset_t set;
62
63 /* handle SIGINT and SIGTERM in this handler */
64 sigemptyset(&set);
65 sigaddset(&set, SIGINT);
66 sigaddset(&set, SIGTERM);
67 sigprocmask(SIG_BLOCK, &set, NULL);
68
69 while (TRUE)
70 {
71 int sig;
72 int error;
73
74 error = sigwait(&set, &sig);
75 if (error)
76 {
77 DBG1(DBG_DMN, "error %d while waiting for a signal", error);
78 return;
79 }
80 switch (sig)
81 {
82 case SIGINT:
83 {
84 DBG1(DBG_DMN, "signal of type SIGINT received. Shutting down");
85 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
86 return;
87 }
88 case SIGTERM:
89 {
90 DBG1(DBG_DMN, "signal of type SIGTERM received. Shutting down");
91 charon->bus->alert(charon->bus, ALERT_SHUTDOWN_SIGNAL, sig);
92 return;
93 }
94 default:
95 {
96 DBG1(DBG_DMN, "unknown signal %d received. Ignored", sig);
97 break;
98 }
99 }
100 }
101 }
102
103 /**
104 * Handle SIGSEGV/SIGILL signals raised by threads
105 */
106 static void segv_handler(int signal)
107 {
108 backtrace_t *backtrace;
109
110 DBG1(DBG_DMN, "thread %u received %d", thread_current_id(), signal);
111 backtrace = backtrace_create(2);
112 backtrace->log(backtrace, stderr, TRUE);
113 backtrace->destroy(backtrace);
114
115 DBG1(DBG_DMN, "killing ourself, received critical signal");
116 abort();
117 }
118
119 /**
120 * Lookup UID and GID
121 */
122 static bool lookup_uid_gid()
123 {
124 #ifdef IPSEC_USER
125 if (!charon->caps->resolve_uid(charon->caps, IPSEC_USER))
126 {
127 return FALSE;
128 }
129 #endif
130 #ifdef IPSEC_GROUP
131 if (!charon->caps->resolve_gid(charon->caps, IPSEC_GROUP))
132 {
133 return FALSE;
134 }
135 #endif
136 return TRUE;
137 }
138
139 /**
140 * Main function, starts NetworkManager backend.
141 */
142 int main(int argc, char *argv[])
143 {
144 struct sigaction action;
145 int status = SS_RC_INITIALIZATION_FAILED;
146
147 /* logging for library during initialization, as we have no bus yet */
148 dbg = dbg_syslog;
149
150 /* initialize library */
151 if (!library_init(NULL))
152 {
153 library_deinit();
154 exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
155 }
156
157 if (lib->integrity &&
158 !lib->integrity->check_file(lib->integrity, "charon-nm", argv[0]))
159 {
160 dbg_syslog(DBG_DMN, 1, "integrity check of charon-nm failed");
161 library_deinit();
162 exit(SS_RC_DAEMON_INTEGRITY);
163 }
164
165 if (!libhydra_init("charon-nm"))
166 {
167 dbg_syslog(DBG_DMN, 1, "initialization failed - aborting charon-nm");
168 libhydra_deinit();
169 library_deinit();
170 exit(SS_RC_INITIALIZATION_FAILED);
171 }
172
173 if (!libcharon_init("charon-nm"))
174 {
175 dbg_syslog(DBG_DMN, 1, "initialization failed - aborting charon-nm");
176 goto deinit;
177 }
178
179 if (!lookup_uid_gid())
180 {
181 dbg_syslog(DBG_DMN, 1, "invalid uid/gid - aborting charon-nm");
182 goto deinit;
183 }
184
185 /* make sure we log to the DAEMON facility by default */
186 lib->settings->set_int(lib->settings, "charon-nm.syslog.daemon.default",
187 lib->settings->get_int(lib->settings,
188 "charon-nm.syslog.daemon.default", 1));
189 charon->load_loggers(charon, NULL, FALSE);
190
191 /* use random ports to avoid conflicts with regular charon */
192 lib->settings->set_int(lib->settings, "charon-nm.port", 0);
193 lib->settings->set_int(lib->settings, "charon-nm.port_natt_t", 0);
194
195 DBG1(DBG_DMN, "Starting charon NetworkManager backend (strongSwan "VERSION")");
196 if (lib->integrity)
197 {
198 DBG1(DBG_DMN, "integrity tests enabled:");
199 DBG1(DBG_DMN, "lib 'libstrongswan': passed file and segment integrity tests");
200 DBG1(DBG_DMN, "lib 'libhydra': passed file and segment integrity tests");
201 DBG1(DBG_DMN, "lib 'libcharon': passed file and segment integrity tests");
202 DBG1(DBG_DMN, "daemon 'charon-nm': passed file integrity test");
203 }
204
205 /* register NM backend to be loaded with plugins */
206 nm_backend_register();
207
208 /* initialize daemon */
209 if (!charon->initialize(charon,
210 lib->settings->get_str(lib->settings, "charon-nm.load", PLUGINS)))
211 {
212 DBG1(DBG_DMN, "initialization failed - aborting charon-nm");
213 goto deinit;
214 }
215
216 if (!charon->caps->drop(charon->caps))
217 {
218 DBG1(DBG_DMN, "capability dropping failed - aborting charon-nm");
219 goto deinit;
220 }
221
222 /* add handler for SEGV and ILL,
223 * INT and TERM are handled by sigwait() in run() */
224 action.sa_handler = segv_handler;
225 action.sa_flags = 0;
226 sigemptyset(&action.sa_mask);
227 sigaddset(&action.sa_mask, SIGINT);
228 sigaddset(&action.sa_mask, SIGTERM);
229 sigaction(SIGSEGV, &action, NULL);
230 sigaction(SIGILL, &action, NULL);
231 sigaction(SIGBUS, &action, NULL);
232 action.sa_handler = SIG_IGN;
233 sigaction(SIGPIPE, &action, NULL);
234
235 pthread_sigmask(SIG_SETMASK, &action.sa_mask, NULL);
236
237 /* start daemon (i.e. the threads in the thread-pool) */
238 charon->start(charon);
239
240 /* main thread goes to run loop */
241 run();
242
243 status = 0;
244
245 deinit:
246 libcharon_deinit();
247 libhydra_deinit();
248 library_deinit();
249 return status;
250 }
251