1 | /* |
1 | /* |
2 | util.C -- process management and other utility functions |
2 | util.C -- process management and other utility functions |
|
|
3 | Copyright (C) 2003-2011 Marc Lehmann <gvpe@schmorp.de> |
3 | |
4 | |
4 | Some of these are taken from tinc, see the AUTHORS file. |
5 | Some of these are taken from tinc, see the AUTHORS file. |
5 | |
6 | |
|
|
7 | This file is part of GVPE. |
|
|
8 | |
6 | This program is free software; you can redistribute it and/or modify |
9 | GVPE is free software; you can redistribute it and/or modify it |
7 | it under the terms of the GNU General Public License as published by |
10 | under the terms of the GNU General Public License as published by the |
8 | the Free Software Foundation; either version 2 of the License, or |
11 | Free Software Foundation; either version 3 of the License, or (at your |
9 | (at your option) any later version. |
12 | option) any later version. |
10 | |
13 | |
11 | This program is distributed in the hope that it will be useful, |
14 | This program is distributed in the hope that it will be useful, but |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
14 | GNU General Public License for more details. |
17 | Public License for more details. |
15 | |
18 | |
16 | You should have received a copy of the GNU General Public License |
19 | You should have received a copy of the GNU General Public License along |
17 | along with this program; if not, write to the Free Software |
20 | with this program; if not, see <http://www.gnu.org/licenses/>. |
18 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 | |
|
|
22 | Additional permission under GNU GPL version 3 section 7 |
|
|
23 | |
|
|
24 | If you modify this Program, or any covered work, by linking or |
|
|
25 | combining it with the OpenSSL project's OpenSSL library (or a modified |
|
|
26 | version of that library), containing parts covered by the terms of the |
|
|
27 | OpenSSL or SSLeay licenses, the licensors of this Program grant you |
|
|
28 | additional permission to convey the resulting work. Corresponding |
|
|
29 | Source for a non-source form of such a combination shall include the |
|
|
30 | source code for the parts of OpenSSL used as well as that of the |
|
|
31 | covered work. |
19 | */ |
32 | */ |
20 | |
33 | |
21 | #include "config.h" |
34 | #include "config.h" |
22 | |
35 | |
23 | #include <cstdio> |
36 | #include <cstdio> |
|
|
37 | #include <cstdlib> |
24 | #include <cstring> |
38 | #include <cstring> |
|
|
39 | |
|
|
40 | #include <queue> |
25 | |
41 | |
26 | #include <errno.h> |
42 | #include <errno.h> |
27 | #include <signal.h> |
43 | #include <signal.h> |
28 | #include <sys/types.h> |
44 | #include <sys/types.h> |
29 | #include <sys/wait.h> |
45 | #include <sys/wait.h> |
30 | #include <unistd.h> |
46 | #include <unistd.h> |
31 | #include <time.h> |
47 | #include <time.h> |
32 | #include <sys/socket.h> |
|
|
33 | #include <netinet/in.h> |
|
|
34 | #include <arpa/inet.h> |
|
|
35 | |
48 | |
|
|
49 | #if ENABLE_PTHREADS |
|
|
50 | # include <pthread.h> |
|
|
51 | #endif |
36 | |
52 | |
37 | #include <sys/mman.h> |
53 | #include <openssl/rand.h> |
|
|
54 | |
|
|
55 | #include "netcompat.h" |
38 | |
56 | |
39 | #include "gettext.h" |
57 | #include "gettext.h" |
40 | #include "pidfile.h" |
58 | #include "pidfile.h" |
41 | #include "dropin.h" |
59 | #include "dropin.h" |
42 | |
60 | |
… | |
… | |
48 | int |
66 | int |
49 | write_pidfile (void) |
67 | write_pidfile (void) |
50 | { |
68 | { |
51 | int pid; |
69 | int pid; |
52 | |
70 | |
53 | pid = check_pid (pidfilename); |
71 | pid = check_pid (conf.pidfilename); |
54 | |
72 | |
55 | if (pid) |
73 | if (pid) |
56 | { |
74 | { |
57 | fprintf (stderr, _("A vped is already running with pid %d.\n"), pid); |
75 | fprintf (stderr, _("A gvpe daemon is already running with pid %d.\n"), pid); |
58 | return 1; |
76 | return 1; |
59 | } |
77 | } |
60 | |
78 | |
61 | /* if it's locked, write-protected, or whatever */ |
79 | /* if it's locked, write-protected, or whatever */ |
62 | if (!write_pid (pidfilename)) |
80 | if (!write_pid (conf.pidfilename)) |
63 | return 1; |
81 | return 1; |
64 | |
82 | |
65 | return 0; |
83 | return 0; |
66 | } |
84 | } |
67 | |
85 | |
68 | int |
86 | int |
69 | kill_other (int signal) |
87 | kill_other (int signal) |
70 | { |
88 | { |
71 | int pid; |
89 | int pid; |
72 | |
90 | |
73 | pid = read_pid (pidfilename); |
91 | pid = read_pid (conf.pidfilename); |
74 | |
92 | |
75 | if (!pid) |
93 | if (!pid) |
76 | { |
94 | { |
77 | fprintf (stderr, _("No other vped is running.\n")); |
95 | fprintf (stderr, _("No other gvpe daemon is running.\n")); |
78 | return 1; |
96 | return 1; |
79 | } |
97 | } |
80 | |
98 | |
81 | errno = 0; /* No error, sometimes errno is only changed on error */ |
99 | errno = 0; /* No error, sometimes errno is only changed on error */ |
82 | |
100 | |
83 | /* ESRCH is returned when no process with that pid is found */ |
101 | /* ESRCH is returned when no process with that pid is found */ |
84 | if (kill (pid, signal) && errno == ESRCH) |
102 | if (kill (pid, signal) && errno == ESRCH) |
85 | { |
103 | { |
86 | fprintf (stderr, _("The vped is no longer running. ")); |
104 | fprintf (stderr, _("The gvpe daemon is no longer running. ")); |
87 | |
105 | |
88 | fprintf (stderr, _("Removing stale lock file.\n")); |
106 | fprintf (stderr, _("Removing stale lock file.\n")); |
89 | remove_pid (pidfilename); |
107 | remove_pid (conf.pidfilename); |
90 | } |
108 | } |
91 | |
109 | |
92 | return 0; |
110 | return 0; |
93 | } |
111 | } |
94 | |
112 | |
… | |
… | |
114 | return -1; |
132 | return -1; |
115 | } |
133 | } |
116 | |
134 | |
117 | /* Now UPDATE the pid in the pidfile, because we changed it... */ |
135 | /* Now UPDATE the pid in the pidfile, because we changed it... */ |
118 | |
136 | |
119 | if (!write_pid (pidfilename)) |
137 | if (!write_pid (conf.pidfilename)) |
120 | return -1; |
138 | return -1; |
121 | |
139 | |
122 | log_to (LOGTO_SYSLOG); |
140 | log_to (LOGTO_SYSLOG); |
123 | } |
141 | } |
124 | else |
142 | else |
125 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
143 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
126 | |
144 | |
127 | slog (L_INFO, _("vped %s (%s %s) starting"), VERSION, __DATE__, __TIME__); |
145 | slog (L_INFO, _("gvpe daemon %s (%s %s) starting up."), VERSION, __DATE__, __TIME__); |
128 | |
146 | |
129 | return 0; |
147 | return 0; |
130 | } |
148 | } |
131 | |
149 | |
132 | void |
150 | /*****************************************************************************/ |
133 | make_names (void) |
|
|
134 | { |
|
|
135 | if (!pidfilename) |
|
|
136 | pidfilename = LOCALSTATEDIR "/run/vped.pid"; |
|
|
137 | |
151 | |
138 | if (!confbase) |
152 | pid_t |
139 | asprintf (&confbase, "%s/vpe", CONFDIR); |
|
|
140 | } |
|
|
141 | |
|
|
142 | void run_script (const run_script_cb &cb, bool wait) |
153 | run_script (const run_script_cb &cb, bool wait) |
143 | { |
154 | { |
144 | int pid; |
155 | sigset_t oldset; |
145 | |
156 | |
146 | if ((pid = fork ()) == 0) |
157 | if (wait) |
|
|
158 | { |
|
|
159 | sigset_t sigchld; |
|
|
160 | sigemptyset (&sigchld); |
|
|
161 | sigaddset (&sigchld, SIGCHLD); |
|
|
162 | sigprocmask (SIG_BLOCK, &sigchld, &oldset); |
147 | { |
163 | } |
148 | char *filename; |
164 | |
149 | asprintf (&filename, "%s/%s", confbase, cb()); |
165 | pid_t pid = fork (); |
150 | execl (filename, filename, (char *) 0); |
166 | |
151 | exit (255); |
167 | if (pid == 0) |
|
|
168 | { |
|
|
169 | sigprocmask (SIG_SETMASK, &oldset, 0); |
|
|
170 | |
|
|
171 | execl ("/bin/sh", "/bin/sh", "-c", cb (), (char *) 0); |
|
|
172 | exit (EXIT_FAILURE); |
152 | } |
173 | } |
153 | else if (pid > 0) |
174 | else if (pid > 0) |
154 | { |
175 | { |
155 | if (wait) |
176 | if (wait) |
156 | { |
177 | { |
|
|
178 | int status; |
157 | waitpid (pid, 0, 0); |
179 | int res = waitpid (pid, &status, 0); |
158 | /* TODO: check status */ |
180 | |
|
|
181 | sigprocmask (SIG_SETMASK, &oldset, 0); |
|
|
182 | |
|
|
183 | if (res < 0) |
|
|
184 | { |
|
|
185 | slog (L_WARN, _("waiting for an external command failed: %s."), |
|
|
186 | strerror (errno)); |
|
|
187 | return 0; |
|
|
188 | } |
|
|
189 | else if (!WIFEXITED (status) || WEXITSTATUS (status) != EXIT_SUCCESS) |
|
|
190 | { |
|
|
191 | slog (L_WARN, _("external command returned with exit status %d (%04x)."), |
|
|
192 | WEXITSTATUS (status), status); |
|
|
193 | return 0; |
|
|
194 | } |
159 | } |
195 | } |
160 | } |
196 | } |
|
|
197 | else |
|
|
198 | { |
|
|
199 | slog (L_ERR, _("unable to fork, exiting: %s"), strerror (errno)); |
|
|
200 | exit (EXIT_FAILURE); |
|
|
201 | } |
|
|
202 | |
|
|
203 | return pid; |
161 | } |
204 | } |
|
|
205 | |
|
|
206 | /*****************************************************************************/ |
|
|
207 | |
|
|
208 | #if 0 /* not yet used */ |
|
|
209 | |
|
|
210 | #if ENABLE_PTHREADS |
|
|
211 | struct async_cb |
|
|
212 | { |
|
|
213 | callback<void ()> work_cb; |
|
|
214 | callback<void ()> done_cb; |
|
|
215 | }; |
|
|
216 | |
|
|
217 | static ev::async async_done_w; |
|
|
218 | static std::queue< callback<void ()> > async_q; |
|
|
219 | |
|
|
220 | static callback<void ()> work_cb; |
|
|
221 | |
|
|
222 | static void * |
|
|
223 | async_exec (void *) |
|
|
224 | { |
|
|
225 | work_cb (); |
|
|
226 | async_done_w.send (); |
|
|
227 | |
|
|
228 | return 0; |
|
|
229 | } |
|
|
230 | |
|
|
231 | static void |
|
|
232 | async_q_next () |
|
|
233 | { |
|
|
234 | work_cb = async_q.front (); async_q.pop (); |
|
|
235 | |
|
|
236 | sigset_t fullsigset, oldsigset; |
|
|
237 | pthread_attr_t attr; |
|
|
238 | pthread_t tid; |
|
|
239 | |
|
|
240 | pthread_attr_init (&attr); |
|
|
241 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
|
|
242 | //pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN); |
|
|
243 | sigfillset (&fullsigset); |
|
|
244 | pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); |
|
|
245 | |
|
|
246 | if (pthread_create (&tid, &attr, async_exec, 0)) |
|
|
247 | async_exec (0); |
|
|
248 | |
|
|
249 | pthread_sigmask (SIG_SETMASK, &oldsigset, 0); |
|
|
250 | pthread_attr_destroy (&attr); |
|
|
251 | } |
|
|
252 | |
|
|
253 | namespace { |
|
|
254 | void |
|
|
255 | async_done (ev::async &w, int revents) |
|
|
256 | { |
|
|
257 | callback<void ()> done_cb = async_q.front (); async_q.pop (); |
|
|
258 | |
|
|
259 | if (async_q.empty ()) |
|
|
260 | async_done_w.stop (); |
|
|
261 | else |
|
|
262 | async_q_next (); |
|
|
263 | |
|
|
264 | done_cb (); |
|
|
265 | } |
|
|
266 | }; |
|
|
267 | |
|
|
268 | void |
|
|
269 | async (callback<void ()> work_cb, callback<void ()> done_cb) |
|
|
270 | { |
|
|
271 | bool was_empty = async_q.empty (); |
|
|
272 | |
|
|
273 | async_q.push (work_cb); |
|
|
274 | async_q.push (done_cb); |
|
|
275 | |
|
|
276 | if (was_empty) |
|
|
277 | { |
|
|
278 | async_done_w.set<async_done> (); |
|
|
279 | async_done_w.start (); |
|
|
280 | async_q_next (); |
|
|
281 | } |
|
|
282 | } |
|
|
283 | |
|
|
284 | #else |
|
|
285 | |
|
|
286 | void |
|
|
287 | async (callback<void ()> work_cb, callback<void ()> done_cb) |
|
|
288 | { |
|
|
289 | work_cb (); |
|
|
290 | done_cb (); |
|
|
291 | } |
|
|
292 | |
|
|
293 | #endif |
|
|
294 | |
|
|
295 | #endif |
|
|
296 | |
|
|
297 | /*****************************************************************************/ |
|
|
298 | |
|
|
299 | void hexdump (const char *header, void *data, int len) |
|
|
300 | { |
|
|
301 | u8 *p = (u8 *)data; |
|
|
302 | |
|
|
303 | printf ("%s:", header); |
|
|
304 | |
|
|
305 | while (len--) |
|
|
306 | printf (" %02x", *p++); |
|
|
307 | |
|
|
308 | printf ("\n"); |
|
|
309 | } |
|
|
310 | |
|
|
311 | /*****************************************************************************/ |
162 | |
312 | |
163 | #if ENABLE_HTTP_PROXY |
313 | #if ENABLE_HTTP_PROXY |
164 | // works like strdup |
314 | // works like strdup |
165 | u8 * |
315 | u8 * |
166 | base64_encode (const u8 *data, unsigned int len) |
316 | base64_encode (const u8 *data, unsigned int len) |
… | |
… | |
206 | |
356 | |
207 | return res; |
357 | return res; |
208 | } |
358 | } |
209 | #endif |
359 | #endif |
210 | |
360 | |
|
|
361 | bool |
|
|
362 | slow_memeq (const void *a, const void *b, int len) |
|
|
363 | { |
|
|
364 | volatile const u8 *pa = (const u8 *)a; |
|
|
365 | volatile const u8 *pb = (const u8 *)b; |
|
|
366 | u8 diff = 0; |
|
|
367 | |
|
|
368 | while (len--) |
|
|
369 | diff |= *pa++ ^ *pb++; |
|
|
370 | |
|
|
371 | return !diff; |
|
|
372 | } |
|
|
373 | |
|
|
374 | void |
|
|
375 | id2mac (unsigned int id, void *m) |
|
|
376 | { |
|
|
377 | mac &p = *(mac *)m; |
|
|
378 | |
|
|
379 | if (id) |
|
|
380 | { |
|
|
381 | p[0] = 0xfe; |
|
|
382 | p[1] = 0xfd; |
|
|
383 | p[2] = 0x80; |
|
|
384 | p[3] = 0x00; |
|
|
385 | p[4] = id >> 8; |
|
|
386 | p[5] = id; |
|
|
387 | } |
|
|
388 | else |
|
|
389 | { |
|
|
390 | p[0] = 0xff; |
|
|
391 | p[1] = 0xff; |
|
|
392 | p[2] = 0xff; |
|
|
393 | p[3] = 0xff; |
|
|
394 | p[4] = 0xff; |
|
|
395 | p[5] = 0xff; |
|
|
396 | } |
|
|
397 | } |
|
|
398 | |
|
|
399 | /*****************************************************************************/ |
|
|
400 | |
|
|
401 | void rand_fill (void *data, int len) |
|
|
402 | { |
|
|
403 | int l = RAND_bytes ((unsigned char *)data, len); |
|
|
404 | |
|
|
405 | if (l > 0) |
|
|
406 | return; |
|
|
407 | else if (l == 0) |
|
|
408 | slog (L_WARN, _("Not enough random entropy to generate secure keys. Using weaker pseudo-random session keys.")); |
|
|
409 | else |
|
|
410 | fatal (_("RAND_bytes failed, aborting.")); |
|
|
411 | } |
|
|
412 | |