]>
Commit | Line | Data |
---|---|---|
5cb5a6ff LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
5cb5a6ff | 22 | #include <assert.h> |
034c6ed7 LP |
23 | #include <dirent.h> |
24 | #include <errno.h> | |
25 | #include <fcntl.h> | |
26 | #include <unistd.h> | |
44d8db9e | 27 | #include <string.h> |
309bff19 | 28 | #include <signal.h> |
071830ff LP |
29 | #include <sys/socket.h> |
30 | #include <sys/un.h> | |
94f04347 | 31 | #include <sys/prctl.h> |
66ee3769 | 32 | #include <linux/sched.h> |
451a074f LP |
33 | #include <sys/types.h> |
34 | #include <sys/stat.h> | |
81a2b7ce LP |
35 | #include <grp.h> |
36 | #include <pwd.h> | |
15ae422b | 37 | #include <sys/mount.h> |
5cb5a6ff LP |
38 | |
39 | #include "execute.h" | |
40 | #include "strv.h" | |
41 | #include "macro.h" | |
42 | #include "util.h" | |
acbb0225 | 43 | #include "log.h" |
9eba9da4 | 44 | #include "ioprio.h" |
94f04347 | 45 | #include "securebits.h" |
8e274523 | 46 | #include "cgroup.h" |
15ae422b | 47 | #include "namespace.h" |
5cb5a6ff | 48 | |
02a51aba LP |
49 | /* This assumes there is a 'tty' group */ |
50 | #define TTY_MODE 0620 | |
51 | ||
034c6ed7 LP |
52 | static int shift_fds(int fds[], unsigned n_fds) { |
53 | int start, restart_from; | |
54 | ||
55 | if (n_fds <= 0) | |
56 | return 0; | |
57 | ||
a0d40ac5 LP |
58 | /* Modifies the fds array! (sorts it) */ |
59 | ||
034c6ed7 LP |
60 | assert(fds); |
61 | ||
62 | start = 0; | |
63 | for (;;) { | |
64 | int i; | |
65 | ||
66 | restart_from = -1; | |
67 | ||
68 | for (i = start; i < (int) n_fds; i++) { | |
69 | int nfd; | |
70 | ||
71 | /* Already at right index? */ | |
72 | if (fds[i] == i+3) | |
73 | continue; | |
74 | ||
75 | if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0) | |
76 | return -errno; | |
77 | ||
e1f5e051 | 78 | close_nointr_nofail(fds[i]); |
034c6ed7 LP |
79 | fds[i] = nfd; |
80 | ||
81 | /* Hmm, the fd we wanted isn't free? Then | |
82 | * let's remember that and try again from here*/ | |
83 | if (nfd != i+3 && restart_from < 0) | |
84 | restart_from = i; | |
85 | } | |
86 | ||
87 | if (restart_from < 0) | |
88 | break; | |
89 | ||
90 | start = restart_from; | |
91 | } | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
c2748801 | 96 | static int flags_fds(const int fds[], unsigned n_fds, bool nonblock) { |
47a71eed | 97 | unsigned i; |
e2c76839 | 98 | int r; |
47a71eed LP |
99 | |
100 | if (n_fds <= 0) | |
101 | return 0; | |
102 | ||
103 | assert(fds); | |
104 | ||
451a074f | 105 | /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags */ |
47a71eed LP |
106 | |
107 | for (i = 0; i < n_fds; i++) { | |
47a71eed | 108 | |
e2c76839 LP |
109 | if ((r = fd_nonblock(fds[i], nonblock)) < 0) |
110 | return r; | |
47a71eed | 111 | |
451a074f LP |
112 | /* We unconditionally drop FD_CLOEXEC from the fds, |
113 | * since after all we want to pass these fds to our | |
114 | * children */ | |
47a71eed | 115 | |
e2c76839 LP |
116 | if ((r = fd_cloexec(fds[i], false)) < 0) |
117 | return r; | |
47a71eed LP |
118 | } |
119 | ||
120 | return 0; | |
121 | } | |
122 | ||
80876c20 LP |
123 | static const char *tty_path(const ExecContext *context) { |
124 | assert(context); | |
125 | ||
126 | if (context->tty_path) | |
127 | return context->tty_path; | |
128 | ||
129 | return "/dev/console"; | |
130 | } | |
131 | ||
132 | static int open_null_as(int flags, int nfd) { | |
133 | int fd, r; | |
071830ff | 134 | |
80876c20 | 135 | assert(nfd >= 0); |
071830ff | 136 | |
80876c20 | 137 | if ((fd = open("/dev/null", flags|O_NOCTTY)) < 0) |
071830ff LP |
138 | return -errno; |
139 | ||
80876c20 LP |
140 | if (fd != nfd) { |
141 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
e1f5e051 | 142 | close_nointr_nofail(fd); |
80876c20 LP |
143 | } else |
144 | r = nfd; | |
071830ff | 145 | |
80876c20 | 146 | return r; |
071830ff LP |
147 | } |
148 | ||
80876c20 LP |
149 | static int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, int nfd) { |
150 | int fd, r; | |
151 | union { | |
152 | struct sockaddr sa; | |
153 | struct sockaddr_un un; | |
154 | } sa; | |
071830ff LP |
155 | |
156 | assert(context); | |
80876c20 LP |
157 | assert(output < _EXEC_OUTPUT_MAX); |
158 | assert(ident); | |
159 | assert(nfd >= 0); | |
071830ff | 160 | |
80876c20 LP |
161 | if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) |
162 | return -errno; | |
071830ff | 163 | |
80876c20 LP |
164 | zero(sa); |
165 | sa.sa.sa_family = AF_UNIX; | |
166 | strncpy(sa.un.sun_path+1, LOGGER_SOCKET, sizeof(sa.un.sun_path)-1); | |
071830ff | 167 | |
80876c20 LP |
168 | if (connect(fd, &sa.sa, sizeof(sa)) < 0) { |
169 | close_nointr_nofail(fd); | |
170 | return -errno; | |
171 | } | |
071830ff | 172 | |
80876c20 LP |
173 | if (shutdown(fd, SHUT_RD) < 0) { |
174 | close_nointr_nofail(fd); | |
175 | return -errno; | |
176 | } | |
071830ff | 177 | |
80876c20 LP |
178 | /* We speak a very simple protocol between log server |
179 | * and client: one line for the log destination (kmsg | |
180 | * or syslog), followed by the priority field, | |
181 | * followed by the process name. Since we replaced | |
182 | * stdin/stderr we simple use stdio to write to | |
183 | * it. Note that we use stderr, to minimize buffer | |
184 | * flushing issues. */ | |
185 | ||
186 | dprintf(fd, | |
187 | "%s\n" | |
188 | "%i\n" | |
189 | "%s\n", | |
190 | output == EXEC_OUTPUT_KERNEL ? "kmsg" : "syslog", | |
191 | context->syslog_priority, | |
192 | context->syslog_identifier ? context->syslog_identifier : ident); | |
193 | ||
194 | if (fd != nfd) { | |
195 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
e1f5e051 | 196 | close_nointr_nofail(fd); |
80876c20 LP |
197 | } else |
198 | r = nfd; | |
071830ff | 199 | |
80876c20 LP |
200 | return r; |
201 | } | |
202 | static int open_terminal_as(const char *path, mode_t mode, int nfd) { | |
203 | int fd, r; | |
071830ff | 204 | |
80876c20 LP |
205 | assert(path); |
206 | assert(nfd >= 0); | |
071830ff | 207 | |
80876c20 LP |
208 | if ((fd = open_terminal(path, mode | O_NOCTTY)) < 0) |
209 | return fd; | |
071830ff | 210 | |
80876c20 LP |
211 | if (fd != nfd) { |
212 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
213 | close_nointr_nofail(fd); | |
214 | } else | |
215 | r = nfd; | |
071830ff | 216 | |
80876c20 LP |
217 | return r; |
218 | } | |
071830ff | 219 | |
80876c20 LP |
220 | static bool is_terminal_input(ExecInput i) { |
221 | return | |
222 | i == EXEC_INPUT_TTY || | |
223 | i == EXEC_INPUT_TTY_FORCE || | |
224 | i == EXEC_INPUT_TTY_FAIL; | |
225 | } | |
071830ff | 226 | |
4f2d528d | 227 | static int fixup_input(const ExecContext *context, int socket_fd) { |
80876c20 | 228 | assert(context); |
071830ff | 229 | |
4f2d528d LP |
230 | if (socket_fd < 0 && context->std_input == EXEC_INPUT_SOCKET) |
231 | return EXEC_INPUT_NULL; | |
232 | ||
233 | return context->std_input; | |
234 | } | |
235 | ||
236 | static int fixup_output(const ExecContext *context, int socket_fd) { | |
237 | assert(context); | |
238 | ||
239 | if (socket_fd < 0 && context->std_output == EXEC_OUTPUT_SOCKET) | |
240 | return EXEC_OUTPUT_INHERIT; | |
241 | ||
242 | return context->std_output; | |
243 | } | |
244 | ||
245 | static int fixup_error(const ExecContext *context, int socket_fd) { | |
246 | assert(context); | |
247 | ||
248 | if (socket_fd < 0 && context->std_error == EXEC_OUTPUT_SOCKET) | |
249 | return EXEC_OUTPUT_INHERIT; | |
250 | ||
251 | return context->std_error; | |
252 | } | |
253 | ||
254 | static int setup_input(const ExecContext *context, int socket_fd) { | |
255 | ExecInput i; | |
256 | ||
257 | assert(context); | |
258 | ||
259 | i = fixup_input(context, socket_fd); | |
260 | ||
261 | switch (i) { | |
071830ff | 262 | |
80876c20 LP |
263 | case EXEC_INPUT_NULL: |
264 | return open_null_as(O_RDONLY, STDIN_FILENO); | |
265 | ||
266 | case EXEC_INPUT_TTY: | |
267 | case EXEC_INPUT_TTY_FORCE: | |
268 | case EXEC_INPUT_TTY_FAIL: { | |
269 | int fd, r; | |
071830ff | 270 | |
80876c20 LP |
271 | if ((fd = acquire_terminal( |
272 | tty_path(context), | |
4f2d528d LP |
273 | i == EXEC_INPUT_TTY_FAIL, |
274 | i == EXEC_INPUT_TTY_FORCE)) < 0) | |
80876c20 LP |
275 | return fd; |
276 | ||
277 | if (fd != STDIN_FILENO) { | |
278 | r = dup2(fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO; | |
071830ff | 279 | close_nointr_nofail(fd); |
80876c20 LP |
280 | } else |
281 | r = STDIN_FILENO; | |
282 | ||
283 | return r; | |
284 | } | |
285 | ||
4f2d528d LP |
286 | case EXEC_INPUT_SOCKET: |
287 | return dup2(socket_fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO; | |
288 | ||
80876c20 LP |
289 | default: |
290 | assert_not_reached("Unknown input type"); | |
291 | } | |
292 | } | |
293 | ||
4f2d528d LP |
294 | static int setup_output(const ExecContext *context, int socket_fd, const char *ident) { |
295 | ExecOutput o; | |
296 | ExecInput i; | |
297 | ||
80876c20 LP |
298 | assert(context); |
299 | assert(ident); | |
300 | ||
4f2d528d LP |
301 | i = fixup_input(context, socket_fd); |
302 | o = fixup_output(context, socket_fd); | |
303 | ||
80876c20 LP |
304 | /* This expects the input is already set up */ |
305 | ||
4f2d528d | 306 | switch (o) { |
071830ff | 307 | |
80876c20 LP |
308 | case EXEC_OUTPUT_INHERIT: |
309 | ||
310 | /* If the input is connected to a terminal, inherit that... */ | |
4f2d528d | 311 | if (is_terminal_input(i) || i == EXEC_INPUT_SOCKET) |
80876c20 | 312 | return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO; |
071830ff | 313 | |
4f2d528d | 314 | return STDIN_FILENO; |
80876c20 LP |
315 | |
316 | case EXEC_OUTPUT_NULL: | |
317 | return open_null_as(O_WRONLY, STDOUT_FILENO); | |
318 | ||
4f2d528d LP |
319 | case EXEC_OUTPUT_TTY: |
320 | if (is_terminal_input(i)) | |
80876c20 LP |
321 | return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO; |
322 | ||
323 | /* We don't reset the terminal if this is just about output */ | |
324 | return open_terminal_as(tty_path(context), O_WRONLY, STDOUT_FILENO); | |
94f04347 | 325 | |
80876c20 LP |
326 | case EXEC_OUTPUT_SYSLOG: |
327 | case EXEC_OUTPUT_KERNEL: | |
4f2d528d LP |
328 | return connect_logger_as(context, o, ident, STDOUT_FILENO); |
329 | ||
330 | case EXEC_OUTPUT_SOCKET: | |
331 | assert(socket_fd >= 0); | |
332 | return dup2(socket_fd, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO; | |
80876c20 | 333 | |
94f04347 LP |
334 | default: |
335 | assert_not_reached("Unknown output type"); | |
071830ff | 336 | } |
94f04347 LP |
337 | } |
338 | ||
4f2d528d LP |
339 | static int setup_error(const ExecContext *context, int socket_fd, const char *ident) { |
340 | ExecOutput o, e; | |
341 | ExecInput i; | |
342 | ||
94f04347 | 343 | assert(context); |
02a51aba | 344 | assert(ident); |
071830ff | 345 | |
4f2d528d LP |
346 | i = fixup_input(context, socket_fd); |
347 | o = fixup_output(context, socket_fd); | |
348 | e = fixup_error(context, socket_fd); | |
349 | ||
80876c20 | 350 | /* This expects the input and output are already set up */ |
94f04347 | 351 | |
80876c20 LP |
352 | /* Don't change the stderr file descriptor if we inherit all |
353 | * the way and are not on a tty */ | |
4f2d528d LP |
354 | if (e == EXEC_OUTPUT_INHERIT && |
355 | o == EXEC_OUTPUT_INHERIT && | |
356 | !is_terminal_input(i)) | |
80876c20 | 357 | return STDERR_FILENO; |
94f04347 | 358 | |
80876c20 | 359 | /* Duplicate form stdout if possible */ |
4f2d528d | 360 | if (e == o || e == EXEC_OUTPUT_INHERIT) |
80876c20 | 361 | return dup2(STDOUT_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO; |
94f04347 | 362 | |
4f2d528d | 363 | switch (e) { |
80876c20 LP |
364 | |
365 | case EXEC_OUTPUT_NULL: | |
366 | return open_null_as(O_WRONLY, STDERR_FILENO); | |
367 | ||
368 | case EXEC_OUTPUT_TTY: | |
4f2d528d | 369 | if (is_terminal_input(i)) |
80876c20 LP |
370 | return dup2(STDIN_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO; |
371 | ||
372 | /* We don't reset the terminal if this is just about output */ | |
373 | return open_terminal_as(tty_path(context), O_WRONLY, STDERR_FILENO); | |
374 | ||
375 | case EXEC_OUTPUT_SYSLOG: | |
376 | case EXEC_OUTPUT_KERNEL: | |
4f2d528d LP |
377 | return connect_logger_as(context, e, ident, STDERR_FILENO); |
378 | ||
379 | case EXEC_OUTPUT_SOCKET: | |
380 | assert(socket_fd >= 0); | |
381 | return dup2(socket_fd, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO; | |
94f04347 LP |
382 | |
383 | default: | |
80876c20 | 384 | assert_not_reached("Unknown error type"); |
94f04347 | 385 | } |
071830ff LP |
386 | } |
387 | ||
02a51aba LP |
388 | static int chown_terminal(int fd, uid_t uid) { |
389 | struct stat st; | |
390 | ||
391 | assert(fd >= 0); | |
02a51aba LP |
392 | |
393 | /* This might fail. What matters are the results. */ | |
394 | fchown(fd, uid, -1); | |
395 | fchmod(fd, TTY_MODE); | |
396 | ||
397 | if (fstat(fd, &st) < 0) | |
398 | return -errno; | |
399 | ||
d8b4e2e9 | 400 | if (st.st_uid != uid || (st.st_mode & 0777) != TTY_MODE) |
02a51aba LP |
401 | return -EPERM; |
402 | ||
403 | return 0; | |
404 | } | |
405 | ||
80876c20 LP |
406 | static int setup_confirm_stdio(const ExecContext *context, |
407 | int *_saved_stdin, | |
408 | int *_saved_stdout) { | |
409 | int fd = -1, saved_stdin, saved_stdout = -1, r; | |
410 | ||
411 | assert(context); | |
412 | assert(_saved_stdin); | |
413 | assert(_saved_stdout); | |
414 | ||
415 | /* This returns positive EXIT_xxx return values instead of | |
416 | * negative errno style values! */ | |
417 | ||
418 | if ((saved_stdin = fcntl(STDIN_FILENO, F_DUPFD, 3)) < 0) | |
419 | return EXIT_STDIN; | |
420 | ||
421 | if ((saved_stdout = fcntl(STDOUT_FILENO, F_DUPFD, 3)) < 0) { | |
422 | r = EXIT_STDOUT; | |
423 | goto fail; | |
424 | } | |
425 | ||
426 | if ((fd = acquire_terminal( | |
427 | tty_path(context), | |
428 | context->std_input == EXEC_INPUT_TTY_FAIL, | |
429 | context->std_input == EXEC_INPUT_TTY_FORCE)) < 0) { | |
430 | r = EXIT_STDIN; | |
431 | goto fail; | |
432 | } | |
433 | ||
02a51aba LP |
434 | if (chown_terminal(fd, getuid()) < 0) { |
435 | r = EXIT_STDIN; | |
436 | goto fail; | |
437 | } | |
438 | ||
80876c20 LP |
439 | if (dup2(fd, STDIN_FILENO) < 0) { |
440 | r = EXIT_STDIN; | |
441 | goto fail; | |
442 | } | |
443 | ||
444 | if (dup2(fd, STDOUT_FILENO) < 0) { | |
445 | r = EXIT_STDOUT; | |
446 | goto fail; | |
447 | } | |
448 | ||
449 | if (fd >= 2) | |
450 | close_nointr_nofail(fd); | |
451 | ||
452 | *_saved_stdin = saved_stdin; | |
453 | *_saved_stdout = saved_stdout; | |
454 | ||
455 | return 0; | |
456 | ||
457 | fail: | |
458 | if (saved_stdout >= 0) | |
459 | close_nointr_nofail(saved_stdout); | |
460 | ||
461 | if (saved_stdin >= 0) | |
462 | close_nointr_nofail(saved_stdin); | |
463 | ||
464 | if (fd >= 0) | |
465 | close_nointr_nofail(fd); | |
466 | ||
467 | return r; | |
468 | } | |
469 | ||
470 | static int restore_conform_stdio(const ExecContext *context, | |
471 | int *saved_stdin, | |
472 | int *saved_stdout, | |
473 | bool *keep_stdin, | |
474 | bool *keep_stdout) { | |
475 | ||
476 | assert(context); | |
477 | assert(saved_stdin); | |
478 | assert(*saved_stdin >= 0); | |
479 | assert(saved_stdout); | |
480 | assert(*saved_stdout >= 0); | |
481 | ||
482 | /* This returns positive EXIT_xxx return values instead of | |
483 | * negative errno style values! */ | |
484 | ||
485 | if (is_terminal_input(context->std_input)) { | |
486 | ||
487 | /* The service wants terminal input. */ | |
488 | ||
489 | *keep_stdin = true; | |
490 | *keep_stdout = | |
491 | context->std_output == EXEC_OUTPUT_INHERIT || | |
492 | context->std_output == EXEC_OUTPUT_TTY; | |
493 | ||
494 | } else { | |
495 | /* If the service doesn't want a controlling terminal, | |
496 | * then we need to get rid entirely of what we have | |
497 | * already. */ | |
498 | ||
499 | if (release_terminal() < 0) | |
500 | return EXIT_STDIN; | |
501 | ||
502 | if (dup2(*saved_stdin, STDIN_FILENO) < 0) | |
503 | return EXIT_STDIN; | |
504 | ||
505 | if (dup2(*saved_stdout, STDOUT_FILENO) < 0) | |
506 | return EXIT_STDOUT; | |
507 | ||
508 | *keep_stdout = *keep_stdin = false; | |
509 | } | |
510 | ||
511 | return 0; | |
512 | } | |
513 | ||
81a2b7ce LP |
514 | static int get_group_creds(const char *groupname, gid_t *gid) { |
515 | struct group *g; | |
516 | unsigned long lu; | |
517 | ||
518 | assert(groupname); | |
519 | assert(gid); | |
520 | ||
521 | /* We enforce some special rules for gid=0: in order to avoid | |
522 | * NSS lookups for root we hardcode its data. */ | |
523 | ||
524 | if (streq(groupname, "root") || streq(groupname, "0")) { | |
525 | *gid = 0; | |
526 | return 0; | |
527 | } | |
528 | ||
529 | if (safe_atolu(groupname, &lu) >= 0) { | |
530 | errno = 0; | |
531 | g = getgrgid((gid_t) lu); | |
532 | } else { | |
533 | errno = 0; | |
534 | g = getgrnam(groupname); | |
535 | } | |
536 | ||
537 | if (!g) | |
538 | return errno != 0 ? -errno : -ESRCH; | |
539 | ||
540 | *gid = g->gr_gid; | |
541 | return 0; | |
542 | } | |
543 | ||
544 | static int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) { | |
545 | struct passwd *p; | |
546 | unsigned long lu; | |
547 | ||
548 | assert(username); | |
549 | assert(*username); | |
550 | assert(uid); | |
551 | assert(gid); | |
552 | assert(home); | |
553 | ||
554 | /* We enforce some special rules for uid=0: in order to avoid | |
555 | * NSS lookups for root we hardcode its data. */ | |
556 | ||
557 | if (streq(*username, "root") || streq(*username, "0")) { | |
558 | *username = "root"; | |
559 | *uid = 0; | |
560 | *gid = 0; | |
561 | *home = "/root"; | |
562 | return 0; | |
563 | } | |
564 | ||
565 | if (safe_atolu(*username, &lu) >= 0) { | |
566 | errno = 0; | |
567 | p = getpwuid((uid_t) lu); | |
568 | ||
569 | /* If there are multiple users with the same id, make | |
570 | * sure to leave $USER to the configured value instead | |
571 | * of the first occurence in the database. However if | |
572 | * the uid was configured by a numeric uid, then let's | |
573 | * pick the real username from /etc/passwd. */ | |
574 | if (*username && p) | |
575 | *username = p->pw_name; | |
576 | } else { | |
577 | errno = 0; | |
578 | p = getpwnam(*username); | |
579 | } | |
580 | ||
581 | if (!p) | |
582 | return errno != 0 ? -errno : -ESRCH; | |
583 | ||
584 | *uid = p->pw_uid; | |
585 | *gid = p->pw_gid; | |
586 | *home = p->pw_dir; | |
587 | return 0; | |
588 | } | |
589 | ||
590 | static int enforce_groups(const ExecContext *context, const char *username, gid_t gid) { | |
591 | bool keep_groups = false; | |
592 | int r; | |
593 | ||
594 | assert(context); | |
595 | ||
596 | /* Lookup and ser GID and supplementary group list. Here too | |
597 | * we avoid NSS lookups for gid=0. */ | |
598 | ||
599 | if (context->group || username) { | |
600 | ||
601 | if (context->group) | |
602 | if ((r = get_group_creds(context->group, &gid)) < 0) | |
603 | return r; | |
604 | ||
605 | /* First step, initialize groups from /etc/groups */ | |
606 | if (username && gid != 0) { | |
607 | if (initgroups(username, gid) < 0) | |
608 | return -errno; | |
609 | ||
610 | keep_groups = true; | |
611 | } | |
612 | ||
613 | /* Second step, set our gids */ | |
614 | if (setresgid(gid, gid, gid) < 0) | |
615 | return -errno; | |
616 | } | |
617 | ||
618 | if (context->supplementary_groups) { | |
619 | int ngroups_max, k; | |
620 | gid_t *gids; | |
621 | char **i; | |
622 | ||
623 | /* Final step, initialize any manually set supplementary groups */ | |
624 | ngroups_max = (int) sysconf(_SC_NGROUPS_MAX); | |
625 | ||
626 | if (!(gids = new(gid_t, ngroups_max))) | |
627 | return -ENOMEM; | |
628 | ||
629 | if (keep_groups) { | |
630 | if ((k = getgroups(ngroups_max, gids)) < 0) { | |
631 | free(gids); | |
632 | return -errno; | |
633 | } | |
634 | } else | |
635 | k = 0; | |
636 | ||
637 | STRV_FOREACH(i, context->supplementary_groups) { | |
638 | ||
639 | if (k >= ngroups_max) { | |
640 | free(gids); | |
641 | return -E2BIG; | |
642 | } | |
643 | ||
644 | if ((r = get_group_creds(*i, gids+k)) < 0) { | |
645 | free(gids); | |
646 | return r; | |
647 | } | |
648 | ||
649 | k++; | |
650 | } | |
651 | ||
652 | if (setgroups(k, gids) < 0) { | |
653 | free(gids); | |
654 | return -errno; | |
655 | } | |
656 | ||
657 | free(gids); | |
658 | } | |
659 | ||
660 | return 0; | |
661 | } | |
662 | ||
663 | static int enforce_user(const ExecContext *context, uid_t uid) { | |
664 | int r; | |
665 | assert(context); | |
666 | ||
667 | /* Sets (but doesn't lookup) the uid and make sure we keep the | |
668 | * capabilities while doing so. */ | |
669 | ||
670 | if (context->capabilities) { | |
671 | cap_t d; | |
672 | static const cap_value_t bits[] = { | |
673 | CAP_SETUID, /* Necessary so that we can run setresuid() below */ | |
674 | CAP_SETPCAP /* Necessary so that we can set PR_SET_SECUREBITS later on */ | |
675 | }; | |
676 | ||
677 | /* First step: If we need to keep capabilities but | |
678 | * drop privileges we need to make sure we keep our | |
679 | * caps, whiel we drop priviliges. */ | |
693ced48 LP |
680 | if (uid != 0) { |
681 | int sb = context->secure_bits|SECURE_KEEP_CAPS; | |
682 | ||
683 | if (prctl(PR_GET_SECUREBITS) != sb) | |
684 | if (prctl(PR_SET_SECUREBITS, sb) < 0) | |
685 | return -errno; | |
686 | } | |
81a2b7ce LP |
687 | |
688 | /* Second step: set the capabilites. This will reduce | |
689 | * the capabilities to the minimum we need. */ | |
690 | ||
691 | if (!(d = cap_dup(context->capabilities))) | |
692 | return -errno; | |
693 | ||
694 | if (cap_set_flag(d, CAP_EFFECTIVE, ELEMENTSOF(bits), bits, CAP_SET) < 0 || | |
695 | cap_set_flag(d, CAP_PERMITTED, ELEMENTSOF(bits), bits, CAP_SET) < 0) { | |
696 | r = -errno; | |
697 | cap_free(d); | |
698 | return r; | |
699 | } | |
700 | ||
701 | if (cap_set_proc(d) < 0) { | |
702 | r = -errno; | |
703 | cap_free(d); | |
704 | return r; | |
705 | } | |
706 | ||
707 | cap_free(d); | |
708 | } | |
709 | ||
710 | /* Third step: actually set the uids */ | |
711 | if (setresuid(uid, uid, uid) < 0) | |
712 | return -errno; | |
713 | ||
714 | /* At this point we should have all necessary capabilities but | |
715 | are otherwise a normal user. However, the caps might got | |
716 | corrupted due to the setresuid() so we need clean them up | |
717 | later. This is done outside of this call. */ | |
718 | ||
719 | return 0; | |
720 | } | |
721 | ||
9fb86720 | 722 | int exec_spawn(ExecCommand *command, |
9e2f7c11 | 723 | char **argv, |
81a2b7ce | 724 | const ExecContext *context, |
c2748801 | 725 | int fds[], unsigned n_fds, |
81a2b7ce LP |
726 | bool apply_permissions, |
727 | bool apply_chroot, | |
80876c20 | 728 | bool confirm_spawn, |
8e274523 | 729 | CGroupBonding *cgroup_bondings, |
81a2b7ce LP |
730 | pid_t *ret) { |
731 | ||
034c6ed7 | 732 | pid_t pid; |
8e274523 | 733 | int r; |
1a63a750 | 734 | char *line; |
4f2d528d | 735 | int socket_fd; |
034c6ed7 | 736 | |
5cb5a6ff LP |
737 | assert(command); |
738 | assert(context); | |
739 | assert(ret); | |
034c6ed7 LP |
740 | assert(fds || n_fds <= 0); |
741 | ||
4f2d528d LP |
742 | if (context->std_input == EXEC_INPUT_SOCKET || |
743 | context->std_output == EXEC_OUTPUT_SOCKET || | |
744 | context->std_error == EXEC_OUTPUT_SOCKET) { | |
745 | ||
746 | if (n_fds != 1) | |
747 | return -EINVAL; | |
748 | ||
749 | socket_fd = fds[0]; | |
750 | ||
751 | fds = NULL; | |
752 | n_fds = 0; | |
753 | } else | |
754 | socket_fd = -1; | |
755 | ||
9e2f7c11 LP |
756 | if (!argv) |
757 | argv = command->argv; | |
758 | ||
759 | if (!(line = exec_command_line(argv))) | |
1a63a750 LP |
760 | return -ENOMEM; |
761 | ||
762 | log_debug("About to execute: %s", line); | |
763 | free(line); | |
acbb0225 | 764 | |
8e274523 LP |
765 | if (cgroup_bondings) |
766 | if ((r = cgroup_bonding_realize_list(cgroup_bondings))) | |
767 | return r; | |
768 | ||
034c6ed7 LP |
769 | if ((pid = fork()) < 0) |
770 | return -errno; | |
771 | ||
772 | if (pid == 0) { | |
8e274523 | 773 | int i; |
309bff19 | 774 | sigset_t ss; |
81a2b7ce LP |
775 | const char *username = NULL, *home = NULL; |
776 | uid_t uid = (uid_t) -1; | |
777 | gid_t gid = (gid_t) -1; | |
778 | char **our_env = NULL, **final_env = NULL; | |
779 | unsigned n_env = 0; | |
80876c20 LP |
780 | int saved_stdout = -1, saved_stdin = -1; |
781 | bool keep_stdout = false, keep_stdin = false; | |
309bff19 | 782 | |
034c6ed7 | 783 | /* child */ |
5cb5a6ff | 784 | |
7b683879 LP |
785 | reset_all_signal_handlers(); |
786 | ||
309bff19 LP |
787 | if (sigemptyset(&ss) < 0 || |
788 | sigprocmask(SIG_SETMASK, &ss, NULL) < 0) { | |
789 | r = EXIT_SIGNAL_MASK; | |
790 | goto fail; | |
791 | } | |
792 | ||
8d567588 LP |
793 | if (!context->no_setsid) |
794 | if (setsid() < 0) { | |
795 | r = EXIT_SETSID; | |
796 | goto fail; | |
797 | } | |
80876c20 | 798 | |
80876c20 LP |
799 | if (confirm_spawn) { |
800 | char response; | |
801 | ||
802 | /* Set up terminal for the question */ | |
803 | if ((r = setup_confirm_stdio(context, | |
804 | &saved_stdin, &saved_stdout))) | |
805 | goto fail; | |
806 | ||
807 | /* Now ask the question. */ | |
9e2f7c11 | 808 | if (!(line = exec_command_line(argv))) { |
80876c20 | 809 | r = EXIT_MEMORY; |
ee2b4894 LP |
810 | goto fail; |
811 | } | |
80876c20 LP |
812 | |
813 | r = ask(&response, "yns", "Execute %s? [Yes, No, Skip] ", line); | |
814 | free(line); | |
815 | ||
816 | if (r < 0 || response == 'n') { | |
817 | r = EXIT_CONFIRM; | |
818 | goto fail; | |
819 | } else if (response == 's') { | |
820 | r = 0; | |
ee2b4894 LP |
821 | goto fail; |
822 | } | |
80876c20 LP |
823 | |
824 | /* Release terminal for the question */ | |
825 | if ((r = restore_conform_stdio(context, | |
826 | &saved_stdin, &saved_stdout, | |
827 | &keep_stdin, &keep_stdout))) | |
828 | goto fail; | |
034c6ed7 LP |
829 | } |
830 | ||
80876c20 | 831 | if (!keep_stdin) |
4f2d528d | 832 | if (setup_input(context, socket_fd) < 0) { |
80876c20 LP |
833 | r = EXIT_STDIN; |
834 | goto fail; | |
835 | } | |
9eba9da4 | 836 | |
80876c20 | 837 | if (!keep_stdout) |
4f2d528d | 838 | if (setup_output(context, socket_fd, file_name_from_path(command->path)) < 0) { |
80876c20 LP |
839 | r = EXIT_STDOUT; |
840 | goto fail; | |
841 | } | |
94f04347 | 842 | |
4f2d528d | 843 | if (setup_error(context, socket_fd, file_name_from_path(command->path)) < 0) { |
80876c20 | 844 | r = EXIT_STDERR; |
071830ff LP |
845 | goto fail; |
846 | } | |
847 | ||
8e274523 LP |
848 | if (cgroup_bondings) |
849 | if ((r = cgroup_bonding_install_list(cgroup_bondings, 0)) < 0) { | |
850 | r = EXIT_CGROUP; | |
851 | goto fail; | |
852 | } | |
853 | ||
fb33a393 LP |
854 | if (context->oom_adjust_set) { |
855 | char t[16]; | |
034c6ed7 | 856 | |
fb33a393 LP |
857 | snprintf(t, sizeof(t), "%i", context->oom_adjust); |
858 | char_array_0(t); | |
034c6ed7 | 859 | |
fb33a393 LP |
860 | if (write_one_line_file("/proc/self/oom_adj", t) < 0) { |
861 | r = EXIT_OOM_ADJUST; | |
862 | goto fail; | |
863 | } | |
034c6ed7 LP |
864 | } |
865 | ||
fb33a393 LP |
866 | if (context->nice_set) |
867 | if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) { | |
868 | r = EXIT_NICE; | |
869 | goto fail; | |
870 | } | |
871 | ||
94f04347 LP |
872 | if (context->cpu_sched_set) { |
873 | struct sched_param param; | |
874 | ||
875 | zero(param); | |
876 | param.sched_priority = context->cpu_sched_priority; | |
877 | ||
38b48754 LP |
878 | if (sched_setscheduler(0, context->cpu_sched_policy | |
879 | (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), ¶m) < 0) { | |
94f04347 LP |
880 | r = EXIT_SETSCHEDULER; |
881 | goto fail; | |
882 | } | |
883 | } | |
884 | ||
885 | if (context->cpu_affinity_set) | |
886 | if (sched_setaffinity(0, sizeof(context->cpu_affinity), &context->cpu_affinity) < 0) { | |
887 | r = EXIT_CPUAFFINITY; | |
888 | goto fail; | |
889 | } | |
890 | ||
9eba9da4 LP |
891 | if (context->ioprio_set) |
892 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) { | |
893 | r = EXIT_IOPRIO; | |
894 | goto fail; | |
895 | } | |
896 | ||
94f04347 LP |
897 | if (context->timer_slack_ns_set) |
898 | if (prctl(PR_SET_TIMERSLACK, context->timer_slack_ns_set) < 0) { | |
899 | r = EXIT_TIMERSLACK; | |
900 | goto fail; | |
901 | } | |
902 | ||
15ae422b LP |
903 | if (strv_length(context->read_write_dirs) > 0 || |
904 | strv_length(context->read_only_dirs) > 0 || | |
905 | strv_length(context->inaccessible_dirs) > 0 || | |
906 | context->mount_flags != MS_SHARED || | |
907 | context->private_tmp) | |
908 | if ((r = setup_namespace( | |
909 | context->read_write_dirs, | |
910 | context->read_only_dirs, | |
911 | context->inaccessible_dirs, | |
912 | context->private_tmp, | |
913 | context->mount_flags)) < 0) | |
914 | goto fail; | |
915 | ||
81a2b7ce LP |
916 | if (context->user) { |
917 | username = context->user; | |
918 | if (get_user_creds(&username, &uid, &gid, &home) < 0) { | |
919 | r = EXIT_USER; | |
920 | goto fail; | |
921 | } | |
d8b4e2e9 LP |
922 | |
923 | if (is_terminal_input(context->std_input)) | |
924 | if (chown_terminal(STDIN_FILENO, uid) < 0) { | |
925 | r = EXIT_STDIN; | |
926 | goto fail; | |
927 | } | |
81a2b7ce LP |
928 | } |
929 | ||
930 | if (apply_permissions) | |
931 | if (enforce_groups(context, username, uid) < 0) { | |
932 | r = EXIT_GROUP; | |
933 | goto fail; | |
934 | } | |
935 | ||
15ae422b LP |
936 | umask(context->umask); |
937 | ||
81a2b7ce LP |
938 | if (apply_chroot) { |
939 | if (context->root_directory) | |
940 | if (chroot(context->root_directory) < 0) { | |
941 | r = EXIT_CHROOT; | |
942 | goto fail; | |
943 | } | |
944 | ||
945 | if (chdir(context->working_directory ? context->working_directory : "/") < 0) { | |
946 | r = EXIT_CHDIR; | |
947 | goto fail; | |
948 | } | |
949 | } else { | |
950 | ||
951 | char *d; | |
952 | ||
953 | if (asprintf(&d, "%s/%s", | |
954 | context->root_directory ? context->root_directory : "", | |
955 | context->working_directory ? context->working_directory : "") < 0) { | |
956 | r = EXIT_MEMORY; | |
957 | goto fail; | |
958 | } | |
959 | ||
960 | if (chdir(d) < 0) { | |
961 | free(d); | |
962 | r = EXIT_CHDIR; | |
963 | goto fail; | |
964 | } | |
965 | ||
966 | free(d); | |
967 | } | |
968 | ||
a0d40ac5 | 969 | if (close_all_fds(fds, n_fds) < 0 || |
47a71eed | 970 | shift_fds(fds, n_fds) < 0 || |
451a074f | 971 | flags_fds(fds, n_fds, context->non_blocking) < 0) { |
034c6ed7 LP |
972 | r = EXIT_FDS; |
973 | goto fail; | |
974 | } | |
975 | ||
81a2b7ce | 976 | if (apply_permissions) { |
034c6ed7 | 977 | |
81a2b7ce LP |
978 | for (i = 0; i < RLIMIT_NLIMITS; i++) { |
979 | if (!context->rlimit[i]) | |
980 | continue; | |
981 | ||
982 | if (setrlimit(i, context->rlimit[i]) < 0) { | |
983 | r = EXIT_LIMITS; | |
984 | goto fail; | |
985 | } | |
034c6ed7 | 986 | } |
034c6ed7 | 987 | |
81a2b7ce LP |
988 | if (context->user) |
989 | if (enforce_user(context, uid) < 0) { | |
990 | r = EXIT_USER; | |
991 | goto fail; | |
992 | } | |
993 | ||
693ced48 LP |
994 | /* PR_GET_SECUREBITS is not priviliged, while |
995 | * PR_SET_SECUREBITS is. So to suppress | |
996 | * potential EPERMs we'll try not to call | |
997 | * PR_SET_SECUREBITS unless necessary. */ | |
998 | if (prctl(PR_GET_SECUREBITS) != context->secure_bits) | |
999 | if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) { | |
1000 | r = EXIT_SECUREBITS; | |
1001 | goto fail; | |
1002 | } | |
81a2b7ce LP |
1003 | |
1004 | if (context->capabilities) | |
1005 | if (cap_set_proc(context->capabilities) < 0) { | |
1006 | r = EXIT_CAPABILITIES; | |
1007 | goto fail; | |
1008 | } | |
94f04347 LP |
1009 | } |
1010 | ||
81a2b7ce LP |
1011 | if (!(our_env = new0(char*, 6))) { |
1012 | r = EXIT_MEMORY; | |
1013 | goto fail; | |
1014 | } | |
034c6ed7 | 1015 | |
81a2b7ce LP |
1016 | if (n_fds > 0) |
1017 | if (asprintf(our_env + n_env++, "LISTEN_PID=%llu", (unsigned long long) getpid()) < 0 || | |
1018 | asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0) { | |
1019 | r = EXIT_MEMORY; | |
1020 | goto fail; | |
1021 | } | |
034c6ed7 | 1022 | |
81a2b7ce LP |
1023 | if (home) |
1024 | if (asprintf(our_env + n_env++, "HOME=%s", home) < 0) { | |
1025 | r = EXIT_MEMORY; | |
1026 | goto fail; | |
1027 | } | |
034c6ed7 | 1028 | |
81a2b7ce LP |
1029 | if (username) |
1030 | if (asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 || | |
1031 | asprintf(our_env + n_env++, "USER=%s", username) < 0) { | |
1032 | r = EXIT_MEMORY; | |
1033 | goto fail; | |
1034 | } | |
034c6ed7 | 1035 | |
81a2b7ce LP |
1036 | if (!(final_env = strv_env_merge(environ, our_env, context->environment, NULL))) { |
1037 | r = EXIT_MEMORY; | |
1038 | goto fail; | |
1039 | } | |
034c6ed7 | 1040 | |
9e2f7c11 | 1041 | execve(command->path, argv, final_env); |
034c6ed7 LP |
1042 | r = EXIT_EXEC; |
1043 | ||
1044 | fail: | |
81a2b7ce LP |
1045 | strv_free(our_env); |
1046 | strv_free(final_env); | |
1047 | ||
80876c20 LP |
1048 | if (saved_stdin >= 0) |
1049 | close_nointr_nofail(saved_stdin); | |
1050 | ||
1051 | if (saved_stdout >= 0) | |
1052 | close_nointr_nofail(saved_stdout); | |
1053 | ||
034c6ed7 LP |
1054 | _exit(r); |
1055 | } | |
1056 | ||
80876c20 LP |
1057 | /* We add the new process to the cgroup both in the child (so |
1058 | * that we can be sure that no user code is ever executed | |
1059 | * outside of the cgroup) and in the parent (so that we can be | |
1060 | * sure that when we kill the cgroup the process will be | |
1061 | * killed too). */ | |
1062 | if (cgroup_bondings) | |
1063 | if ((r = cgroup_bonding_install_list(cgroup_bondings, pid)) < 0) { | |
1064 | r = EXIT_CGROUP; | |
1065 | goto fail; | |
1066 | } | |
2da3263a | 1067 | |
81a2b7ce | 1068 | log_debug("Forked %s as %llu", command->path, (unsigned long long) pid); |
2da3263a | 1069 | |
9fb86720 LP |
1070 | command->exec_status.pid = pid; |
1071 | command->exec_status.start_timestamp = now(CLOCK_REALTIME); | |
1072 | ||
034c6ed7 | 1073 | *ret = pid; |
5cb5a6ff LP |
1074 | return 0; |
1075 | } | |
1076 | ||
034c6ed7 LP |
1077 | void exec_context_init(ExecContext *c) { |
1078 | assert(c); | |
1079 | ||
1080 | c->umask = 0002; | |
9eba9da4 | 1081 | c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0); |
94f04347 | 1082 | c->cpu_sched_policy = SCHED_OTHER; |
071830ff | 1083 | c->syslog_priority = LOG_DAEMON|LOG_INFO; |
15ae422b | 1084 | c->mount_flags = MS_SHARED; |
034c6ed7 LP |
1085 | } |
1086 | ||
1087 | void exec_context_done(ExecContext *c) { | |
5cb5a6ff LP |
1088 | unsigned l; |
1089 | ||
1090 | assert(c); | |
1091 | ||
1092 | strv_free(c->environment); | |
034c6ed7 | 1093 | c->environment = NULL; |
5cb5a6ff | 1094 | |
034c6ed7 | 1095 | for (l = 0; l < ELEMENTSOF(c->rlimit); l++) { |
5cb5a6ff | 1096 | free(c->rlimit[l]); |
034c6ed7 LP |
1097 | c->rlimit[l] = NULL; |
1098 | } | |
1099 | ||
9eba9da4 LP |
1100 | free(c->working_directory); |
1101 | c->working_directory = NULL; | |
1102 | free(c->root_directory); | |
1103 | c->root_directory = NULL; | |
5cb5a6ff | 1104 | |
80876c20 LP |
1105 | free(c->tty_path); |
1106 | c->tty_path = NULL; | |
1107 | ||
071830ff LP |
1108 | free(c->syslog_identifier); |
1109 | c->syslog_identifier = NULL; | |
1110 | ||
5cb5a6ff | 1111 | free(c->user); |
034c6ed7 LP |
1112 | c->user = NULL; |
1113 | ||
5cb5a6ff | 1114 | free(c->group); |
034c6ed7 LP |
1115 | c->group = NULL; |
1116 | ||
1117 | strv_free(c->supplementary_groups); | |
1118 | c->supplementary_groups = NULL; | |
94f04347 LP |
1119 | |
1120 | if (c->capabilities) { | |
1121 | cap_free(c->capabilities); | |
1122 | c->capabilities = NULL; | |
1123 | } | |
15ae422b LP |
1124 | |
1125 | strv_free(c->read_only_dirs); | |
1126 | c->read_only_dirs = NULL; | |
1127 | ||
1128 | strv_free(c->read_write_dirs); | |
1129 | c->read_write_dirs = NULL; | |
1130 | ||
1131 | strv_free(c->inaccessible_dirs); | |
1132 | c->inaccessible_dirs = NULL; | |
5cb5a6ff LP |
1133 | } |
1134 | ||
43d0fcbd LP |
1135 | void exec_command_done(ExecCommand *c) { |
1136 | assert(c); | |
1137 | ||
1138 | free(c->path); | |
1139 | c->path = NULL; | |
1140 | ||
1141 | strv_free(c->argv); | |
1142 | c->argv = NULL; | |
1143 | } | |
1144 | ||
1145 | void exec_command_done_array(ExecCommand *c, unsigned n) { | |
1146 | unsigned i; | |
1147 | ||
1148 | for (i = 0; i < n; i++) | |
1149 | exec_command_done(c+i); | |
1150 | } | |
1151 | ||
5cb5a6ff LP |
1152 | void exec_command_free_list(ExecCommand *c) { |
1153 | ExecCommand *i; | |
1154 | ||
1155 | while ((i = c)) { | |
034c6ed7 | 1156 | LIST_REMOVE(ExecCommand, command, c, i); |
43d0fcbd | 1157 | exec_command_done(i); |
5cb5a6ff LP |
1158 | free(i); |
1159 | } | |
1160 | } | |
1161 | ||
034c6ed7 LP |
1162 | void exec_command_free_array(ExecCommand **c, unsigned n) { |
1163 | unsigned i; | |
1164 | ||
1165 | for (i = 0; i < n; i++) { | |
1166 | exec_command_free_list(c[i]); | |
1167 | c[i] = NULL; | |
1168 | } | |
1169 | } | |
1170 | ||
15ae422b LP |
1171 | static void strv_fprintf(FILE *f, char **l) { |
1172 | char **g; | |
1173 | ||
1174 | assert(f); | |
1175 | ||
1176 | STRV_FOREACH(g, l) | |
1177 | fprintf(f, " %s", *g); | |
1178 | } | |
1179 | ||
5cb5a6ff | 1180 | void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) { |
94f04347 LP |
1181 | char ** e; |
1182 | unsigned i; | |
9eba9da4 | 1183 | |
5cb5a6ff LP |
1184 | assert(c); |
1185 | assert(f); | |
1186 | ||
1187 | if (!prefix) | |
1188 | prefix = ""; | |
1189 | ||
1190 | fprintf(f, | |
94f04347 LP |
1191 | "%sUMask: %04o\n" |
1192 | "%sWorkingDirectory: %s\n" | |
451a074f | 1193 | "%sRootDirectory: %s\n" |
15ae422b LP |
1194 | "%sNonBlocking: %s\n" |
1195 | "%sPrivateTmp: %s\n", | |
5cb5a6ff | 1196 | prefix, c->umask, |
9eba9da4 | 1197 | prefix, c->working_directory ? c->working_directory : "/", |
451a074f | 1198 | prefix, c->root_directory ? c->root_directory : "/", |
15ae422b LP |
1199 | prefix, yes_no(c->non_blocking), |
1200 | prefix, yes_no(c->private_tmp)); | |
fb33a393 | 1201 | |
94f04347 LP |
1202 | if (c->environment) |
1203 | for (e = c->environment; *e; e++) | |
1204 | fprintf(f, "%sEnvironment: %s\n", prefix, *e); | |
1205 | ||
fb33a393 LP |
1206 | if (c->nice_set) |
1207 | fprintf(f, | |
1208 | "%sNice: %i\n", | |
1209 | prefix, c->nice); | |
1210 | ||
1211 | if (c->oom_adjust_set) | |
1212 | fprintf(f, | |
1213 | "%sOOMAdjust: %i\n", | |
1214 | prefix, c->oom_adjust); | |
9eba9da4 | 1215 | |
94f04347 LP |
1216 | for (i = 0; i < RLIM_NLIMITS; i++) |
1217 | if (c->rlimit[i]) | |
ea430986 | 1218 | fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max); |
94f04347 | 1219 | |
9eba9da4 LP |
1220 | if (c->ioprio_set) |
1221 | fprintf(f, | |
1222 | "%sIOSchedulingClass: %s\n" | |
1223 | "%sIOPriority: %i\n", | |
94f04347 | 1224 | prefix, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c->ioprio)), |
9eba9da4 | 1225 | prefix, (int) IOPRIO_PRIO_DATA(c->ioprio)); |
94f04347 LP |
1226 | |
1227 | if (c->cpu_sched_set) | |
1228 | fprintf(f, | |
1229 | "%sCPUSchedulingPolicy: %s\n" | |
38b48754 LP |
1230 | "%sCPUSchedulingPriority: %i\n" |
1231 | "%sCPUSchedulingResetOnFork: %s\n", | |
94f04347 | 1232 | prefix, sched_policy_to_string(c->cpu_sched_policy), |
38b48754 LP |
1233 | prefix, c->cpu_sched_priority, |
1234 | prefix, yes_no(c->cpu_sched_reset_on_fork)); | |
94f04347 LP |
1235 | |
1236 | if (c->cpu_affinity_set) { | |
1237 | fprintf(f, "%sCPUAffinity:", prefix); | |
1238 | for (i = 0; i < CPU_SETSIZE; i++) | |
1239 | if (CPU_ISSET(i, &c->cpu_affinity)) | |
1240 | fprintf(f, " %i", i); | |
1241 | fputs("\n", f); | |
1242 | } | |
1243 | ||
1244 | if (c->timer_slack_ns_set) | |
1245 | fprintf(f, "%sTimerSlackNS: %lu\n", prefix, c->timer_slack_ns); | |
1246 | ||
1247 | fprintf(f, | |
80876c20 LP |
1248 | "%sStandardInput: %s\n" |
1249 | "%sStandardOutput: %s\n" | |
1250 | "%sStandardError: %s\n", | |
1251 | prefix, exec_input_to_string(c->std_input), | |
1252 | prefix, exec_output_to_string(c->std_output), | |
1253 | prefix, exec_output_to_string(c->std_error)); | |
1254 | ||
1255 | if (c->tty_path) | |
1256 | fprintf(f, | |
1257 | "%sTTYPath: %s\n", | |
1258 | prefix, c->tty_path); | |
94f04347 | 1259 | |
80876c20 LP |
1260 | if (c->std_output == EXEC_OUTPUT_SYSLOG || c->std_output == EXEC_OUTPUT_KERNEL || |
1261 | c->std_error == EXEC_OUTPUT_SYSLOG || c->std_error == EXEC_OUTPUT_KERNEL) | |
94f04347 LP |
1262 | fprintf(f, |
1263 | "%sSyslogFacility: %s\n" | |
1264 | "%sSyslogLevel: %s\n", | |
1265 | prefix, log_facility_to_string(LOG_FAC(c->syslog_priority)), | |
1266 | prefix, log_level_to_string(LOG_PRI(c->syslog_priority))); | |
1267 | ||
1268 | if (c->capabilities) { | |
1269 | char *t; | |
1270 | if ((t = cap_to_text(c->capabilities, NULL))) { | |
1271 | fprintf(f, "%sCapabilities: %s\n", | |
1272 | prefix, t); | |
1273 | cap_free(t); | |
1274 | } | |
1275 | } | |
1276 | ||
1277 | if (c->secure_bits) | |
1278 | fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n", | |
1279 | prefix, | |
1280 | (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "", | |
1281 | (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "", | |
1282 | (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "", | |
1283 | (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "", | |
1284 | (c->secure_bits & SECURE_NOROOT) ? " noroot" : "", | |
1285 | (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : ""); | |
1286 | ||
1287 | if (c->capability_bounding_set_drop) { | |
1288 | fprintf(f, "%sCapabilityBoundingSetDrop:", prefix); | |
1289 | ||
1290 | for (i = 0; i <= CAP_LAST_CAP; i++) | |
1291 | if (c->capability_bounding_set_drop & (1 << i)) { | |
1292 | char *t; | |
1293 | ||
1294 | if ((t = cap_to_name(i))) { | |
1295 | fprintf(f, " %s", t); | |
1296 | free(t); | |
1297 | } | |
1298 | } | |
1299 | ||
1300 | fputs("\n", f); | |
1301 | } | |
1302 | ||
1303 | if (c->user) | |
1304 | fprintf(f, "%sUser: %s", prefix, c->user); | |
1305 | if (c->group) | |
1306 | fprintf(f, "%sGroup: %s", prefix, c->group); | |
1307 | ||
15ae422b | 1308 | if (strv_length(c->supplementary_groups) > 0) { |
94f04347 | 1309 | fprintf(f, "%sSupplementaryGroups:", prefix); |
15ae422b LP |
1310 | strv_fprintf(f, c->supplementary_groups); |
1311 | fputs("\n", f); | |
1312 | } | |
94f04347 | 1313 | |
15ae422b LP |
1314 | if (strv_length(c->read_write_dirs) > 0) { |
1315 | fprintf(f, "%sReadWriteDirs:", prefix); | |
1316 | strv_fprintf(f, c->read_write_dirs); | |
1317 | fputs("\n", f); | |
1318 | } | |
1319 | ||
1320 | if (strv_length(c->read_only_dirs) > 0) { | |
1321 | fprintf(f, "%sReadOnlyDirs:", prefix); | |
1322 | strv_fprintf(f, c->read_only_dirs); | |
1323 | fputs("\n", f); | |
1324 | } | |
94f04347 | 1325 | |
15ae422b LP |
1326 | if (strv_length(c->inaccessible_dirs) > 0) { |
1327 | fprintf(f, "%sInaccessibleDirs:", prefix); | |
1328 | strv_fprintf(f, c->inaccessible_dirs); | |
94f04347 LP |
1329 | fputs("\n", f); |
1330 | } | |
5cb5a6ff LP |
1331 | } |
1332 | ||
034c6ed7 LP |
1333 | void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) { |
1334 | assert(s); | |
5cb5a6ff | 1335 | |
034c6ed7 | 1336 | s->pid = pid; |
9fb86720 LP |
1337 | s->exit_timestamp = now(CLOCK_REALTIME); |
1338 | ||
034c6ed7 LP |
1339 | s->code = code; |
1340 | s->status = status; | |
9fb86720 LP |
1341 | } |
1342 | ||
1343 | void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) { | |
1344 | char buf[FORMAT_TIMESTAMP_MAX]; | |
1345 | ||
1346 | assert(s); | |
1347 | assert(f); | |
1348 | ||
1349 | if (!prefix) | |
1350 | prefix = ""; | |
1351 | ||
1352 | if (s->pid <= 0) | |
1353 | return; | |
1354 | ||
1355 | fprintf(f, | |
1356 | "%sPID: %llu\n", | |
1357 | prefix, (unsigned long long) s->pid); | |
1358 | ||
1359 | if (s->start_timestamp > 0) | |
1360 | fprintf(f, | |
1361 | "%sStart Timestamp: %s\n", | |
1362 | prefix, format_timestamp(buf, sizeof(buf), s->start_timestamp)); | |
1363 | ||
1364 | if (s->exit_timestamp > 0) | |
1365 | fprintf(f, | |
1366 | "%sExit Timestamp: %s\n" | |
1367 | "%sExit Code: %s\n" | |
1368 | "%sExit Status: %i\n", | |
1369 | prefix, format_timestamp(buf, sizeof(buf), s->exit_timestamp), | |
1370 | prefix, sigchld_code_to_string(s->code), | |
1371 | prefix, s->status); | |
5cb5a6ff | 1372 | } |
44d8db9e | 1373 | |
9e2f7c11 | 1374 | char *exec_command_line(char **argv) { |
44d8db9e LP |
1375 | size_t k; |
1376 | char *n, *p, **a; | |
1377 | bool first = true; | |
1378 | ||
9e2f7c11 | 1379 | assert(argv); |
44d8db9e | 1380 | |
9164977d | 1381 | k = 1; |
9e2f7c11 | 1382 | STRV_FOREACH(a, argv) |
44d8db9e LP |
1383 | k += strlen(*a)+3; |
1384 | ||
1385 | if (!(n = new(char, k))) | |
1386 | return NULL; | |
1387 | ||
1388 | p = n; | |
9e2f7c11 | 1389 | STRV_FOREACH(a, argv) { |
44d8db9e LP |
1390 | |
1391 | if (!first) | |
1392 | *(p++) = ' '; | |
1393 | else | |
1394 | first = false; | |
1395 | ||
1396 | if (strpbrk(*a, WHITESPACE)) { | |
1397 | *(p++) = '\''; | |
1398 | p = stpcpy(p, *a); | |
1399 | *(p++) = '\''; | |
1400 | } else | |
1401 | p = stpcpy(p, *a); | |
1402 | ||
1403 | } | |
1404 | ||
9164977d LP |
1405 | *p = 0; |
1406 | ||
44d8db9e LP |
1407 | /* FIXME: this doesn't really handle arguments that have |
1408 | * spaces and ticks in them */ | |
1409 | ||
1410 | return n; | |
1411 | } | |
1412 | ||
1413 | void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) { | |
9fb86720 LP |
1414 | char *p2; |
1415 | const char *prefix2; | |
1416 | ||
44d8db9e LP |
1417 | char *cmd; |
1418 | ||
1419 | assert(c); | |
1420 | assert(f); | |
1421 | ||
1422 | if (!prefix) | |
1423 | prefix = ""; | |
9fb86720 LP |
1424 | p2 = strappend(prefix, "\t"); |
1425 | prefix2 = p2 ? p2 : prefix; | |
44d8db9e | 1426 | |
9e2f7c11 | 1427 | cmd = exec_command_line(c->argv); |
44d8db9e LP |
1428 | |
1429 | fprintf(f, | |
1430 | "%sCommand Line: %s\n", | |
1431 | prefix, cmd ? cmd : strerror(ENOMEM)); | |
1432 | ||
1433 | free(cmd); | |
9fb86720 LP |
1434 | |
1435 | exec_status_dump(&c->exec_status, f, prefix2); | |
1436 | ||
1437 | free(p2); | |
44d8db9e LP |
1438 | } |
1439 | ||
1440 | void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) { | |
1441 | assert(f); | |
1442 | ||
1443 | if (!prefix) | |
1444 | prefix = ""; | |
1445 | ||
1446 | LIST_FOREACH(command, c, c) | |
1447 | exec_command_dump(c, f, prefix); | |
1448 | } | |
94f04347 | 1449 | |
a6a80b4f LP |
1450 | void exec_command_append_list(ExecCommand **l, ExecCommand *e) { |
1451 | ExecCommand *end; | |
1452 | ||
1453 | assert(l); | |
1454 | assert(e); | |
1455 | ||
1456 | if (*l) { | |
1457 | /* It's kinda important that we keep the order here */ | |
1458 | LIST_FIND_TAIL(ExecCommand, command, *l, end); | |
1459 | LIST_INSERT_AFTER(ExecCommand, command, *l, end, e); | |
1460 | } else | |
1461 | *l = e; | |
1462 | } | |
1463 | ||
26fd040d LP |
1464 | int exec_command_set(ExecCommand *c, const char *path, ...) { |
1465 | va_list ap; | |
1466 | char **l, *p; | |
1467 | ||
1468 | assert(c); | |
1469 | assert(path); | |
1470 | ||
1471 | va_start(ap, path); | |
1472 | l = strv_new_ap(path, ap); | |
1473 | va_end(ap); | |
1474 | ||
1475 | if (!l) | |
1476 | return -ENOMEM; | |
1477 | ||
1478 | if (!(p = strdup(path))) { | |
1479 | strv_free(l); | |
1480 | return -ENOMEM; | |
1481 | } | |
1482 | ||
1483 | free(c->path); | |
1484 | c->path = p; | |
1485 | ||
1486 | strv_free(c->argv); | |
1487 | c->argv = l; | |
1488 | ||
1489 | return 0; | |
1490 | } | |
1491 | ||
80876c20 | 1492 | const char* exit_status_to_string(ExitStatus status) { |
b8d3418f LP |
1493 | |
1494 | /* We cast to int here, so that -Wenum doesn't complain that | |
1495 | * EXIT_SUCCESS/EXIT_FAILURE aren't in the enum */ | |
1496 | ||
1497 | switch ((int) status) { | |
80876c20 LP |
1498 | |
1499 | case EXIT_SUCCESS: | |
1500 | return "SUCCESS"; | |
1501 | ||
1502 | case EXIT_FAILURE: | |
1503 | return "FAILURE"; | |
1504 | ||
1505 | case EXIT_INVALIDARGUMENT: | |
1506 | return "INVALIDARGUMENT"; | |
1507 | ||
1508 | case EXIT_NOTIMPLEMENTED: | |
1509 | return "NOTIMPLEMENTED"; | |
1510 | ||
1511 | case EXIT_NOPERMISSION: | |
1512 | return "NOPERMISSION"; | |
1513 | ||
1514 | case EXIT_NOTINSTALLED: | |
1515 | return "NOTINSSTALLED"; | |
1516 | ||
1517 | case EXIT_NOTCONFIGURED: | |
1518 | return "NOTCONFIGURED"; | |
1519 | ||
1520 | case EXIT_NOTRUNNING: | |
1521 | return "NOTRUNNING"; | |
1522 | ||
1523 | case EXIT_CHDIR: | |
1524 | return "CHDIR"; | |
1525 | ||
1526 | case EXIT_NICE: | |
1527 | return "NICE"; | |
1528 | ||
1529 | case EXIT_FDS: | |
1530 | return "FDS"; | |
1531 | ||
1532 | case EXIT_EXEC: | |
1533 | return "EXEC"; | |
1534 | ||
1535 | case EXIT_MEMORY: | |
1536 | return "MEMORY"; | |
1537 | ||
1538 | case EXIT_LIMITS: | |
1539 | return "LIMITS"; | |
1540 | ||
1541 | case EXIT_OOM_ADJUST: | |
1542 | return "OOM_ADJUST"; | |
1543 | ||
1544 | case EXIT_SIGNAL_MASK: | |
1545 | return "SIGNAL_MASK"; | |
1546 | ||
1547 | case EXIT_STDIN: | |
1548 | return "STDIN"; | |
1549 | ||
1550 | case EXIT_STDOUT: | |
1551 | return "STDOUT"; | |
1552 | ||
1553 | case EXIT_CHROOT: | |
1554 | return "CHROOT"; | |
1555 | ||
1556 | case EXIT_IOPRIO: | |
1557 | return "IOPRIO"; | |
1558 | ||
1559 | case EXIT_TIMERSLACK: | |
1560 | return "TIMERSLACK"; | |
1561 | ||
1562 | case EXIT_SECUREBITS: | |
1563 | return "SECUREBITS"; | |
1564 | ||
1565 | case EXIT_SETSCHEDULER: | |
1566 | return "SETSCHEDULER"; | |
1567 | ||
1568 | case EXIT_CPUAFFINITY: | |
1569 | return "CPUAFFINITY"; | |
1570 | ||
1571 | case EXIT_GROUP: | |
1572 | return "GROUP"; | |
1573 | ||
1574 | case EXIT_USER: | |
1575 | return "USER"; | |
1576 | ||
1577 | case EXIT_CAPABILITIES: | |
1578 | return "CAPABILITIES"; | |
1579 | ||
1580 | case EXIT_CGROUP: | |
1581 | return "CGROUP"; | |
1582 | ||
1583 | case EXIT_SETSID: | |
1584 | return "SETSID"; | |
1585 | ||
1586 | case EXIT_CONFIRM: | |
1587 | return "CONFIRM"; | |
1588 | ||
1589 | case EXIT_STDERR: | |
1590 | return "STDERR"; | |
1591 | ||
1592 | default: | |
1593 | return NULL; | |
1594 | } | |
1595 | } | |
1596 | ||
1597 | static const char* const exec_input_table[_EXEC_INPUT_MAX] = { | |
1598 | [EXEC_INPUT_NULL] = "null", | |
1599 | [EXEC_INPUT_TTY] = "tty", | |
1600 | [EXEC_INPUT_TTY_FORCE] = "tty-force", | |
4f2d528d LP |
1601 | [EXEC_INPUT_TTY_FAIL] = "tty-fail", |
1602 | [EXEC_INPUT_SOCKET] = "socket" | |
80876c20 LP |
1603 | }; |
1604 | ||
94f04347 | 1605 | static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = { |
80876c20 | 1606 | [EXEC_OUTPUT_INHERIT] = "inherit", |
94f04347 | 1607 | [EXEC_OUTPUT_NULL] = "null", |
80876c20 | 1608 | [EXEC_OUTPUT_TTY] = "tty", |
94f04347 | 1609 | [EXEC_OUTPUT_SYSLOG] = "syslog", |
4f2d528d LP |
1610 | [EXEC_OUTPUT_KERNEL] = "kernel", |
1611 | [EXEC_OUTPUT_SOCKET] = "socket" | |
94f04347 LP |
1612 | }; |
1613 | ||
1614 | DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput); | |
1615 | ||
94f04347 | 1616 | DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput); |