]>
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> |
25e870b5 | 38 | #include <linux/fs.h> |
5cb5a6ff LP |
39 | |
40 | #include "execute.h" | |
41 | #include "strv.h" | |
42 | #include "macro.h" | |
43 | #include "util.h" | |
acbb0225 | 44 | #include "log.h" |
9eba9da4 | 45 | #include "ioprio.h" |
94f04347 | 46 | #include "securebits.h" |
8e274523 | 47 | #include "cgroup.h" |
15ae422b | 48 | #include "namespace.h" |
5cb5a6ff | 49 | |
02a51aba LP |
50 | /* This assumes there is a 'tty' group */ |
51 | #define TTY_MODE 0620 | |
52 | ||
034c6ed7 LP |
53 | static int shift_fds(int fds[], unsigned n_fds) { |
54 | int start, restart_from; | |
55 | ||
56 | if (n_fds <= 0) | |
57 | return 0; | |
58 | ||
a0d40ac5 LP |
59 | /* Modifies the fds array! (sorts it) */ |
60 | ||
034c6ed7 LP |
61 | assert(fds); |
62 | ||
63 | start = 0; | |
64 | for (;;) { | |
65 | int i; | |
66 | ||
67 | restart_from = -1; | |
68 | ||
69 | for (i = start; i < (int) n_fds; i++) { | |
70 | int nfd; | |
71 | ||
72 | /* Already at right index? */ | |
73 | if (fds[i] == i+3) | |
74 | continue; | |
75 | ||
76 | if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0) | |
77 | return -errno; | |
78 | ||
e1f5e051 | 79 | close_nointr_nofail(fds[i]); |
034c6ed7 LP |
80 | fds[i] = nfd; |
81 | ||
82 | /* Hmm, the fd we wanted isn't free? Then | |
83 | * let's remember that and try again from here*/ | |
84 | if (nfd != i+3 && restart_from < 0) | |
85 | restart_from = i; | |
86 | } | |
87 | ||
88 | if (restart_from < 0) | |
89 | break; | |
90 | ||
91 | start = restart_from; | |
92 | } | |
93 | ||
94 | return 0; | |
95 | } | |
96 | ||
c2748801 | 97 | static int flags_fds(const int fds[], unsigned n_fds, bool nonblock) { |
47a71eed | 98 | unsigned i; |
e2c76839 | 99 | int r; |
47a71eed LP |
100 | |
101 | if (n_fds <= 0) | |
102 | return 0; | |
103 | ||
104 | assert(fds); | |
105 | ||
451a074f | 106 | /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags */ |
47a71eed LP |
107 | |
108 | for (i = 0; i < n_fds; i++) { | |
47a71eed | 109 | |
e2c76839 LP |
110 | if ((r = fd_nonblock(fds[i], nonblock)) < 0) |
111 | return r; | |
47a71eed | 112 | |
451a074f LP |
113 | /* We unconditionally drop FD_CLOEXEC from the fds, |
114 | * since after all we want to pass these fds to our | |
115 | * children */ | |
47a71eed | 116 | |
e2c76839 LP |
117 | if ((r = fd_cloexec(fds[i], false)) < 0) |
118 | return r; | |
47a71eed LP |
119 | } |
120 | ||
121 | return 0; | |
122 | } | |
123 | ||
80876c20 LP |
124 | static const char *tty_path(const ExecContext *context) { |
125 | assert(context); | |
126 | ||
127 | if (context->tty_path) | |
128 | return context->tty_path; | |
129 | ||
130 | return "/dev/console"; | |
131 | } | |
132 | ||
133 | static int open_null_as(int flags, int nfd) { | |
134 | int fd, r; | |
071830ff | 135 | |
80876c20 | 136 | assert(nfd >= 0); |
071830ff | 137 | |
80876c20 | 138 | if ((fd = open("/dev/null", flags|O_NOCTTY)) < 0) |
071830ff LP |
139 | return -errno; |
140 | ||
80876c20 LP |
141 | if (fd != nfd) { |
142 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
e1f5e051 | 143 | close_nointr_nofail(fd); |
80876c20 LP |
144 | } else |
145 | r = nfd; | |
071830ff | 146 | |
80876c20 | 147 | return r; |
071830ff LP |
148 | } |
149 | ||
80876c20 LP |
150 | static int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, int nfd) { |
151 | int fd, r; | |
152 | union { | |
153 | struct sockaddr sa; | |
154 | struct sockaddr_un un; | |
155 | } sa; | |
071830ff LP |
156 | |
157 | assert(context); | |
80876c20 LP |
158 | assert(output < _EXEC_OUTPUT_MAX); |
159 | assert(ident); | |
160 | assert(nfd >= 0); | |
071830ff | 161 | |
80876c20 LP |
162 | if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) |
163 | return -errno; | |
071830ff | 164 | |
80876c20 LP |
165 | zero(sa); |
166 | sa.sa.sa_family = AF_UNIX; | |
167 | strncpy(sa.un.sun_path+1, LOGGER_SOCKET, sizeof(sa.un.sun_path)-1); | |
071830ff | 168 | |
80876c20 LP |
169 | if (connect(fd, &sa.sa, sizeof(sa)) < 0) { |
170 | close_nointr_nofail(fd); | |
171 | return -errno; | |
172 | } | |
071830ff | 173 | |
80876c20 LP |
174 | if (shutdown(fd, SHUT_RD) < 0) { |
175 | close_nointr_nofail(fd); | |
176 | return -errno; | |
177 | } | |
071830ff | 178 | |
80876c20 LP |
179 | /* We speak a very simple protocol between log server |
180 | * and client: one line for the log destination (kmsg | |
181 | * or syslog), followed by the priority field, | |
182 | * followed by the process name. Since we replaced | |
183 | * stdin/stderr we simple use stdio to write to | |
184 | * it. Note that we use stderr, to minimize buffer | |
185 | * flushing issues. */ | |
186 | ||
187 | dprintf(fd, | |
188 | "%s\n" | |
189 | "%i\n" | |
4f4a1dbf LP |
190 | "%s\n" |
191 | "%i\n", | |
9a6bca7a | 192 | output == EXEC_OUTPUT_KMSG ? "kmsg" : "syslog", |
80876c20 | 193 | context->syslog_priority, |
4f4a1dbf LP |
194 | context->syslog_identifier ? context->syslog_identifier : ident, |
195 | !context->syslog_no_prefix); | |
80876c20 LP |
196 | |
197 | if (fd != nfd) { | |
198 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
e1f5e051 | 199 | close_nointr_nofail(fd); |
80876c20 LP |
200 | } else |
201 | r = nfd; | |
071830ff | 202 | |
80876c20 LP |
203 | return r; |
204 | } | |
205 | static int open_terminal_as(const char *path, mode_t mode, int nfd) { | |
206 | int fd, r; | |
071830ff | 207 | |
80876c20 LP |
208 | assert(path); |
209 | assert(nfd >= 0); | |
071830ff | 210 | |
80876c20 LP |
211 | if ((fd = open_terminal(path, mode | O_NOCTTY)) < 0) |
212 | return fd; | |
071830ff | 213 | |
80876c20 LP |
214 | if (fd != nfd) { |
215 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
216 | close_nointr_nofail(fd); | |
217 | } else | |
218 | r = nfd; | |
071830ff | 219 | |
80876c20 LP |
220 | return r; |
221 | } | |
071830ff | 222 | |
80876c20 LP |
223 | static bool is_terminal_input(ExecInput i) { |
224 | return | |
225 | i == EXEC_INPUT_TTY || | |
226 | i == EXEC_INPUT_TTY_FORCE || | |
227 | i == EXEC_INPUT_TTY_FAIL; | |
228 | } | |
071830ff | 229 | |
03fd9c49 | 230 | static int fixup_input(ExecInput std_input, int socket_fd) { |
071830ff | 231 | |
03fd9c49 | 232 | if (std_input == EXEC_INPUT_SOCKET && socket_fd < 0) |
4f2d528d LP |
233 | return EXEC_INPUT_NULL; |
234 | ||
03fd9c49 | 235 | return std_input; |
4f2d528d LP |
236 | } |
237 | ||
03fd9c49 | 238 | static int fixup_output(ExecOutput std_output, int socket_fd) { |
4f2d528d | 239 | |
03fd9c49 | 240 | if (std_output == EXEC_OUTPUT_SOCKET && socket_fd < 0) |
4f2d528d LP |
241 | return EXEC_OUTPUT_INHERIT; |
242 | ||
03fd9c49 | 243 | return std_output; |
4f2d528d LP |
244 | } |
245 | ||
246 | static int setup_input(const ExecContext *context, int socket_fd) { | |
247 | ExecInput i; | |
248 | ||
249 | assert(context); | |
250 | ||
03fd9c49 | 251 | i = fixup_input(context->std_input, socket_fd); |
4f2d528d LP |
252 | |
253 | switch (i) { | |
071830ff | 254 | |
80876c20 LP |
255 | case EXEC_INPUT_NULL: |
256 | return open_null_as(O_RDONLY, STDIN_FILENO); | |
257 | ||
258 | case EXEC_INPUT_TTY: | |
259 | case EXEC_INPUT_TTY_FORCE: | |
260 | case EXEC_INPUT_TTY_FAIL: { | |
261 | int fd, r; | |
071830ff | 262 | |
80876c20 LP |
263 | if ((fd = acquire_terminal( |
264 | tty_path(context), | |
4f2d528d | 265 | i == EXEC_INPUT_TTY_FAIL, |
21de3988 LP |
266 | i == EXEC_INPUT_TTY_FORCE, |
267 | false)) < 0) | |
80876c20 LP |
268 | return fd; |
269 | ||
270 | if (fd != STDIN_FILENO) { | |
271 | r = dup2(fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO; | |
071830ff | 272 | close_nointr_nofail(fd); |
80876c20 LP |
273 | } else |
274 | r = STDIN_FILENO; | |
275 | ||
276 | return r; | |
277 | } | |
278 | ||
4f2d528d LP |
279 | case EXEC_INPUT_SOCKET: |
280 | return dup2(socket_fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO; | |
281 | ||
80876c20 LP |
282 | default: |
283 | assert_not_reached("Unknown input type"); | |
284 | } | |
285 | } | |
286 | ||
4f2d528d LP |
287 | static int setup_output(const ExecContext *context, int socket_fd, const char *ident) { |
288 | ExecOutput o; | |
289 | ExecInput i; | |
290 | ||
80876c20 LP |
291 | assert(context); |
292 | assert(ident); | |
293 | ||
03fd9c49 LP |
294 | i = fixup_input(context->std_input, socket_fd); |
295 | o = fixup_output(context->std_output, socket_fd); | |
4f2d528d | 296 | |
80876c20 LP |
297 | /* This expects the input is already set up */ |
298 | ||
4f2d528d | 299 | switch (o) { |
071830ff | 300 | |
80876c20 LP |
301 | case EXEC_OUTPUT_INHERIT: |
302 | ||
303 | /* If the input is connected to a terminal, inherit that... */ | |
ff876e28 | 304 | if (i != EXEC_INPUT_NULL) |
80876c20 | 305 | return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO; |
071830ff | 306 | |
ff876e28 | 307 | /* For PID 1 stdout is always connected to /dev/null, |
dec5d552 LP |
308 | * hence reopen the console if out parent is PID1. */ |
309 | if (getppid() == 1) | |
ff876e28 LP |
310 | return open_terminal_as(tty_path(context), O_WRONLY, STDOUT_FILENO); |
311 | ||
312 | return STDOUT_FILENO; | |
80876c20 LP |
313 | |
314 | case EXEC_OUTPUT_NULL: | |
315 | return open_null_as(O_WRONLY, STDOUT_FILENO); | |
316 | ||
4f2d528d LP |
317 | case EXEC_OUTPUT_TTY: |
318 | if (is_terminal_input(i)) | |
80876c20 LP |
319 | return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO; |
320 | ||
321 | /* We don't reset the terminal if this is just about output */ | |
322 | return open_terminal_as(tty_path(context), O_WRONLY, STDOUT_FILENO); | |
94f04347 | 323 | |
80876c20 | 324 | case EXEC_OUTPUT_SYSLOG: |
9a6bca7a | 325 | case EXEC_OUTPUT_KMSG: |
4f2d528d LP |
326 | return connect_logger_as(context, o, ident, STDOUT_FILENO); |
327 | ||
328 | case EXEC_OUTPUT_SOCKET: | |
329 | assert(socket_fd >= 0); | |
330 | return dup2(socket_fd, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO; | |
80876c20 | 331 | |
94f04347 LP |
332 | default: |
333 | assert_not_reached("Unknown output type"); | |
071830ff | 334 | } |
94f04347 LP |
335 | } |
336 | ||
4f2d528d LP |
337 | static int setup_error(const ExecContext *context, int socket_fd, const char *ident) { |
338 | ExecOutput o, e; | |
339 | ExecInput i; | |
340 | ||
94f04347 | 341 | assert(context); |
02a51aba | 342 | assert(ident); |
071830ff | 343 | |
03fd9c49 LP |
344 | i = fixup_input(context->std_input, socket_fd); |
345 | o = fixup_output(context->std_output, socket_fd); | |
346 | e = fixup_output(context->std_error, socket_fd); | |
4f2d528d | 347 | |
80876c20 | 348 | /* This expects the input and output are already set up */ |
94f04347 | 349 | |
80876c20 LP |
350 | /* Don't change the stderr file descriptor if we inherit all |
351 | * the way and are not on a tty */ | |
4f2d528d LP |
352 | if (e == EXEC_OUTPUT_INHERIT && |
353 | o == EXEC_OUTPUT_INHERIT && | |
ff876e28 | 354 | i != EXEC_INPUT_NULL && |
dec5d552 | 355 | getppid () != 1) |
80876c20 | 356 | return STDERR_FILENO; |
94f04347 | 357 | |
80876c20 | 358 | /* Duplicate form stdout if possible */ |
4f2d528d | 359 | if (e == o || e == EXEC_OUTPUT_INHERIT) |
80876c20 | 360 | return dup2(STDOUT_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO; |
94f04347 | 361 | |
4f2d528d | 362 | switch (e) { |
80876c20 LP |
363 | |
364 | case EXEC_OUTPUT_NULL: | |
365 | return open_null_as(O_WRONLY, STDERR_FILENO); | |
366 | ||
367 | case EXEC_OUTPUT_TTY: | |
4f2d528d | 368 | if (is_terminal_input(i)) |
80876c20 LP |
369 | return dup2(STDIN_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO; |
370 | ||
371 | /* We don't reset the terminal if this is just about output */ | |
372 | return open_terminal_as(tty_path(context), O_WRONLY, STDERR_FILENO); | |
373 | ||
374 | case EXEC_OUTPUT_SYSLOG: | |
9a6bca7a | 375 | case EXEC_OUTPUT_KMSG: |
4f2d528d LP |
376 | return connect_logger_as(context, e, ident, STDERR_FILENO); |
377 | ||
378 | case EXEC_OUTPUT_SOCKET: | |
379 | assert(socket_fd >= 0); | |
380 | return dup2(socket_fd, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO; | |
94f04347 LP |
381 | |
382 | default: | |
80876c20 | 383 | assert_not_reached("Unknown error type"); |
94f04347 | 384 | } |
071830ff LP |
385 | } |
386 | ||
02a51aba LP |
387 | static int chown_terminal(int fd, uid_t uid) { |
388 | struct stat st; | |
389 | ||
390 | assert(fd >= 0); | |
02a51aba LP |
391 | |
392 | /* This might fail. What matters are the results. */ | |
bab45044 LP |
393 | (void) fchown(fd, uid, -1); |
394 | (void) fchmod(fd, TTY_MODE); | |
02a51aba LP |
395 | |
396 | if (fstat(fd, &st) < 0) | |
397 | return -errno; | |
398 | ||
d8b4e2e9 | 399 | if (st.st_uid != uid || (st.st_mode & 0777) != TTY_MODE) |
02a51aba LP |
400 | return -EPERM; |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
80876c20 LP |
405 | static int setup_confirm_stdio(const ExecContext *context, |
406 | int *_saved_stdin, | |
407 | int *_saved_stdout) { | |
408 | int fd = -1, saved_stdin, saved_stdout = -1, r; | |
409 | ||
410 | assert(context); | |
411 | assert(_saved_stdin); | |
412 | assert(_saved_stdout); | |
413 | ||
414 | /* This returns positive EXIT_xxx return values instead of | |
415 | * negative errno style values! */ | |
416 | ||
417 | if ((saved_stdin = fcntl(STDIN_FILENO, F_DUPFD, 3)) < 0) | |
418 | return EXIT_STDIN; | |
419 | ||
420 | if ((saved_stdout = fcntl(STDOUT_FILENO, F_DUPFD, 3)) < 0) { | |
421 | r = EXIT_STDOUT; | |
422 | goto fail; | |
423 | } | |
424 | ||
425 | if ((fd = acquire_terminal( | |
426 | tty_path(context), | |
427 | context->std_input == EXEC_INPUT_TTY_FAIL, | |
21de3988 LP |
428 | context->std_input == EXEC_INPUT_TTY_FORCE, |
429 | false)) < 0) { | |
80876c20 LP |
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 | ||
2608882f | 470 | static int restore_confirm_stdio(const ExecContext *context, |
80876c20 LP |
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, |
1137a57c | 726 | char **environment, |
81a2b7ce LP |
727 | bool apply_permissions, |
728 | bool apply_chroot, | |
80876c20 | 729 | bool confirm_spawn, |
8e274523 | 730 | CGroupBonding *cgroup_bondings, |
81a2b7ce LP |
731 | pid_t *ret) { |
732 | ||
034c6ed7 | 733 | pid_t pid; |
8e274523 | 734 | int r; |
1a63a750 | 735 | char *line; |
4f2d528d | 736 | int socket_fd; |
034c6ed7 | 737 | |
5cb5a6ff LP |
738 | assert(command); |
739 | assert(context); | |
740 | assert(ret); | |
034c6ed7 LP |
741 | assert(fds || n_fds <= 0); |
742 | ||
4f2d528d LP |
743 | if (context->std_input == EXEC_INPUT_SOCKET || |
744 | context->std_output == EXEC_OUTPUT_SOCKET || | |
745 | context->std_error == EXEC_OUTPUT_SOCKET) { | |
746 | ||
747 | if (n_fds != 1) | |
748 | return -EINVAL; | |
749 | ||
750 | socket_fd = fds[0]; | |
751 | ||
752 | fds = NULL; | |
753 | n_fds = 0; | |
754 | } else | |
755 | socket_fd = -1; | |
756 | ||
9e2f7c11 LP |
757 | if (!argv) |
758 | argv = command->argv; | |
759 | ||
760 | if (!(line = exec_command_line(argv))) | |
1a63a750 LP |
761 | return -ENOMEM; |
762 | ||
763 | log_debug("About to execute: %s", line); | |
764 | free(line); | |
acbb0225 | 765 | |
8e274523 LP |
766 | if (cgroup_bondings) |
767 | if ((r = cgroup_bonding_realize_list(cgroup_bondings))) | |
768 | return r; | |
769 | ||
034c6ed7 LP |
770 | if ((pid = fork()) < 0) |
771 | return -errno; | |
772 | ||
773 | if (pid == 0) { | |
8e274523 | 774 | int i; |
309bff19 | 775 | sigset_t ss; |
81a2b7ce LP |
776 | const char *username = NULL, *home = NULL; |
777 | uid_t uid = (uid_t) -1; | |
778 | gid_t gid = (gid_t) -1; | |
779 | char **our_env = NULL, **final_env = NULL; | |
780 | unsigned n_env = 0; | |
80876c20 LP |
781 | int saved_stdout = -1, saved_stdin = -1; |
782 | bool keep_stdout = false, keep_stdin = false; | |
309bff19 | 783 | |
034c6ed7 | 784 | /* child */ |
5cb5a6ff | 785 | |
1b91d3e8 LP |
786 | /* We reset exactly these signals, since they are the |
787 | * only ones we set to SIG_IGN in the main daemon. All | |
788 | * others we leave untouched because we set them to | |
789 | * SIG_DFL or a valid handler initially, both of which | |
790 | * will be demoted to SIG_DFL. */ | |
791 | default_signals(SIGNALS_CRASH_HANDLER, | |
9a34ec5f | 792 | SIGNALS_IGNORE, -1); |
7b683879 | 793 | |
309bff19 LP |
794 | if (sigemptyset(&ss) < 0 || |
795 | sigprocmask(SIG_SETMASK, &ss, NULL) < 0) { | |
796 | r = EXIT_SIGNAL_MASK; | |
797 | goto fail; | |
798 | } | |
799 | ||
8d567588 LP |
800 | if (!context->no_setsid) |
801 | if (setsid() < 0) { | |
802 | r = EXIT_SETSID; | |
803 | goto fail; | |
804 | } | |
80876c20 | 805 | |
80876c20 LP |
806 | if (confirm_spawn) { |
807 | char response; | |
808 | ||
809 | /* Set up terminal for the question */ | |
810 | if ((r = setup_confirm_stdio(context, | |
811 | &saved_stdin, &saved_stdout))) | |
812 | goto fail; | |
813 | ||
814 | /* Now ask the question. */ | |
9e2f7c11 | 815 | if (!(line = exec_command_line(argv))) { |
80876c20 | 816 | r = EXIT_MEMORY; |
ee2b4894 LP |
817 | goto fail; |
818 | } | |
80876c20 LP |
819 | |
820 | r = ask(&response, "yns", "Execute %s? [Yes, No, Skip] ", line); | |
821 | free(line); | |
822 | ||
823 | if (r < 0 || response == 'n') { | |
824 | r = EXIT_CONFIRM; | |
825 | goto fail; | |
826 | } else if (response == 's') { | |
827 | r = 0; | |
ee2b4894 LP |
828 | goto fail; |
829 | } | |
80876c20 LP |
830 | |
831 | /* Release terminal for the question */ | |
2608882f | 832 | if ((r = restore_confirm_stdio(context, |
80876c20 LP |
833 | &saved_stdin, &saved_stdout, |
834 | &keep_stdin, &keep_stdout))) | |
835 | goto fail; | |
034c6ed7 LP |
836 | } |
837 | ||
80876c20 | 838 | if (!keep_stdin) |
4f2d528d | 839 | if (setup_input(context, socket_fd) < 0) { |
80876c20 LP |
840 | r = EXIT_STDIN; |
841 | goto fail; | |
842 | } | |
9eba9da4 | 843 | |
80876c20 | 844 | if (!keep_stdout) |
4f2d528d | 845 | if (setup_output(context, socket_fd, file_name_from_path(command->path)) < 0) { |
80876c20 LP |
846 | r = EXIT_STDOUT; |
847 | goto fail; | |
848 | } | |
94f04347 | 849 | |
4f2d528d | 850 | if (setup_error(context, socket_fd, file_name_from_path(command->path)) < 0) { |
80876c20 | 851 | r = EXIT_STDERR; |
071830ff LP |
852 | goto fail; |
853 | } | |
854 | ||
8e274523 LP |
855 | if (cgroup_bondings) |
856 | if ((r = cgroup_bonding_install_list(cgroup_bondings, 0)) < 0) { | |
857 | r = EXIT_CGROUP; | |
858 | goto fail; | |
859 | } | |
860 | ||
fb33a393 LP |
861 | if (context->oom_adjust_set) { |
862 | char t[16]; | |
034c6ed7 | 863 | |
fb33a393 LP |
864 | snprintf(t, sizeof(t), "%i", context->oom_adjust); |
865 | char_array_0(t); | |
034c6ed7 | 866 | |
fb33a393 LP |
867 | if (write_one_line_file("/proc/self/oom_adj", t) < 0) { |
868 | r = EXIT_OOM_ADJUST; | |
869 | goto fail; | |
870 | } | |
034c6ed7 LP |
871 | } |
872 | ||
fb33a393 LP |
873 | if (context->nice_set) |
874 | if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) { | |
875 | r = EXIT_NICE; | |
876 | goto fail; | |
877 | } | |
878 | ||
94f04347 LP |
879 | if (context->cpu_sched_set) { |
880 | struct sched_param param; | |
881 | ||
882 | zero(param); | |
883 | param.sched_priority = context->cpu_sched_priority; | |
884 | ||
38b48754 LP |
885 | if (sched_setscheduler(0, context->cpu_sched_policy | |
886 | (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), ¶m) < 0) { | |
94f04347 LP |
887 | r = EXIT_SETSCHEDULER; |
888 | goto fail; | |
889 | } | |
890 | } | |
891 | ||
892 | if (context->cpu_affinity_set) | |
893 | if (sched_setaffinity(0, sizeof(context->cpu_affinity), &context->cpu_affinity) < 0) { | |
894 | r = EXIT_CPUAFFINITY; | |
895 | goto fail; | |
896 | } | |
897 | ||
9eba9da4 LP |
898 | if (context->ioprio_set) |
899 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) { | |
900 | r = EXIT_IOPRIO; | |
901 | goto fail; | |
902 | } | |
903 | ||
94f04347 LP |
904 | if (context->timer_slack_ns_set) |
905 | if (prctl(PR_SET_TIMERSLACK, context->timer_slack_ns_set) < 0) { | |
906 | r = EXIT_TIMERSLACK; | |
907 | goto fail; | |
908 | } | |
909 | ||
15ae422b LP |
910 | if (strv_length(context->read_write_dirs) > 0 || |
911 | strv_length(context->read_only_dirs) > 0 || | |
912 | strv_length(context->inaccessible_dirs) > 0 || | |
913 | context->mount_flags != MS_SHARED || | |
914 | context->private_tmp) | |
915 | if ((r = setup_namespace( | |
916 | context->read_write_dirs, | |
917 | context->read_only_dirs, | |
918 | context->inaccessible_dirs, | |
919 | context->private_tmp, | |
920 | context->mount_flags)) < 0) | |
921 | goto fail; | |
922 | ||
81a2b7ce LP |
923 | if (context->user) { |
924 | username = context->user; | |
925 | if (get_user_creds(&username, &uid, &gid, &home) < 0) { | |
926 | r = EXIT_USER; | |
927 | goto fail; | |
928 | } | |
d8b4e2e9 LP |
929 | |
930 | if (is_terminal_input(context->std_input)) | |
931 | if (chown_terminal(STDIN_FILENO, uid) < 0) { | |
932 | r = EXIT_STDIN; | |
933 | goto fail; | |
934 | } | |
81a2b7ce LP |
935 | } |
936 | ||
937 | if (apply_permissions) | |
938 | if (enforce_groups(context, username, uid) < 0) { | |
939 | r = EXIT_GROUP; | |
940 | goto fail; | |
941 | } | |
942 | ||
15ae422b LP |
943 | umask(context->umask); |
944 | ||
81a2b7ce LP |
945 | if (apply_chroot) { |
946 | if (context->root_directory) | |
947 | if (chroot(context->root_directory) < 0) { | |
948 | r = EXIT_CHROOT; | |
949 | goto fail; | |
950 | } | |
951 | ||
952 | if (chdir(context->working_directory ? context->working_directory : "/") < 0) { | |
953 | r = EXIT_CHDIR; | |
954 | goto fail; | |
955 | } | |
956 | } else { | |
957 | ||
958 | char *d; | |
959 | ||
960 | if (asprintf(&d, "%s/%s", | |
961 | context->root_directory ? context->root_directory : "", | |
962 | context->working_directory ? context->working_directory : "") < 0) { | |
963 | r = EXIT_MEMORY; | |
964 | goto fail; | |
965 | } | |
966 | ||
967 | if (chdir(d) < 0) { | |
968 | free(d); | |
969 | r = EXIT_CHDIR; | |
970 | goto fail; | |
971 | } | |
972 | ||
973 | free(d); | |
974 | } | |
975 | ||
a0d40ac5 | 976 | if (close_all_fds(fds, n_fds) < 0 || |
47a71eed | 977 | shift_fds(fds, n_fds) < 0 || |
451a074f | 978 | flags_fds(fds, n_fds, context->non_blocking) < 0) { |
034c6ed7 LP |
979 | r = EXIT_FDS; |
980 | goto fail; | |
981 | } | |
982 | ||
81a2b7ce | 983 | if (apply_permissions) { |
034c6ed7 | 984 | |
81a2b7ce LP |
985 | for (i = 0; i < RLIMIT_NLIMITS; i++) { |
986 | if (!context->rlimit[i]) | |
987 | continue; | |
988 | ||
989 | if (setrlimit(i, context->rlimit[i]) < 0) { | |
990 | r = EXIT_LIMITS; | |
991 | goto fail; | |
992 | } | |
034c6ed7 | 993 | } |
034c6ed7 | 994 | |
81a2b7ce LP |
995 | if (context->user) |
996 | if (enforce_user(context, uid) < 0) { | |
997 | r = EXIT_USER; | |
998 | goto fail; | |
999 | } | |
1000 | ||
693ced48 LP |
1001 | /* PR_GET_SECUREBITS is not priviliged, while |
1002 | * PR_SET_SECUREBITS is. So to suppress | |
1003 | * potential EPERMs we'll try not to call | |
1004 | * PR_SET_SECUREBITS unless necessary. */ | |
1005 | if (prctl(PR_GET_SECUREBITS) != context->secure_bits) | |
1006 | if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) { | |
1007 | r = EXIT_SECUREBITS; | |
1008 | goto fail; | |
1009 | } | |
81a2b7ce LP |
1010 | |
1011 | if (context->capabilities) | |
1012 | if (cap_set_proc(context->capabilities) < 0) { | |
1013 | r = EXIT_CAPABILITIES; | |
1014 | goto fail; | |
1015 | } | |
94f04347 LP |
1016 | } |
1017 | ||
81a2b7ce LP |
1018 | if (!(our_env = new0(char*, 6))) { |
1019 | r = EXIT_MEMORY; | |
1020 | goto fail; | |
1021 | } | |
034c6ed7 | 1022 | |
81a2b7ce LP |
1023 | if (n_fds > 0) |
1024 | if (asprintf(our_env + n_env++, "LISTEN_PID=%llu", (unsigned long long) getpid()) < 0 || | |
1025 | asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0) { | |
1026 | r = EXIT_MEMORY; | |
1027 | goto fail; | |
1028 | } | |
034c6ed7 | 1029 | |
81a2b7ce LP |
1030 | if (home) |
1031 | if (asprintf(our_env + n_env++, "HOME=%s", home) < 0) { | |
1032 | r = EXIT_MEMORY; | |
1033 | goto fail; | |
1034 | } | |
034c6ed7 | 1035 | |
81a2b7ce LP |
1036 | if (username) |
1037 | if (asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 || | |
1038 | asprintf(our_env + n_env++, "USER=%s", username) < 0) { | |
1039 | r = EXIT_MEMORY; | |
1040 | goto fail; | |
1041 | } | |
034c6ed7 | 1042 | |
4e85aff4 LP |
1043 | assert(n_env <= 6); |
1044 | ||
1137a57c | 1045 | if (!(final_env = strv_env_merge(environment, our_env, context->environment, NULL))) { |
81a2b7ce LP |
1046 | r = EXIT_MEMORY; |
1047 | goto fail; | |
1048 | } | |
034c6ed7 | 1049 | |
9e2f7c11 | 1050 | execve(command->path, argv, final_env); |
034c6ed7 LP |
1051 | r = EXIT_EXEC; |
1052 | ||
1053 | fail: | |
81a2b7ce LP |
1054 | strv_free(our_env); |
1055 | strv_free(final_env); | |
1056 | ||
80876c20 LP |
1057 | if (saved_stdin >= 0) |
1058 | close_nointr_nofail(saved_stdin); | |
1059 | ||
1060 | if (saved_stdout >= 0) | |
1061 | close_nointr_nofail(saved_stdout); | |
1062 | ||
034c6ed7 LP |
1063 | _exit(r); |
1064 | } | |
1065 | ||
80876c20 LP |
1066 | /* We add the new process to the cgroup both in the child (so |
1067 | * that we can be sure that no user code is ever executed | |
1068 | * outside of the cgroup) and in the parent (so that we can be | |
1069 | * sure that when we kill the cgroup the process will be | |
1070 | * killed too). */ | |
1071 | if (cgroup_bondings) | |
4e85aff4 | 1072 | cgroup_bonding_install_list(cgroup_bondings, pid); |
2da3263a | 1073 | |
81a2b7ce | 1074 | log_debug("Forked %s as %llu", command->path, (unsigned long long) pid); |
2da3263a | 1075 | |
9fb86720 LP |
1076 | command->exec_status.pid = pid; |
1077 | command->exec_status.start_timestamp = now(CLOCK_REALTIME); | |
1078 | ||
034c6ed7 | 1079 | *ret = pid; |
5cb5a6ff LP |
1080 | return 0; |
1081 | } | |
1082 | ||
034c6ed7 LP |
1083 | void exec_context_init(ExecContext *c) { |
1084 | assert(c); | |
1085 | ||
1086 | c->umask = 0002; | |
9eba9da4 | 1087 | c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0); |
94f04347 | 1088 | c->cpu_sched_policy = SCHED_OTHER; |
071830ff | 1089 | c->syslog_priority = LOG_DAEMON|LOG_INFO; |
15ae422b | 1090 | c->mount_flags = MS_SHARED; |
034c6ed7 LP |
1091 | } |
1092 | ||
1093 | void exec_context_done(ExecContext *c) { | |
5cb5a6ff LP |
1094 | unsigned l; |
1095 | ||
1096 | assert(c); | |
1097 | ||
1098 | strv_free(c->environment); | |
034c6ed7 | 1099 | c->environment = NULL; |
5cb5a6ff | 1100 | |
034c6ed7 | 1101 | for (l = 0; l < ELEMENTSOF(c->rlimit); l++) { |
5cb5a6ff | 1102 | free(c->rlimit[l]); |
034c6ed7 LP |
1103 | c->rlimit[l] = NULL; |
1104 | } | |
1105 | ||
9eba9da4 LP |
1106 | free(c->working_directory); |
1107 | c->working_directory = NULL; | |
1108 | free(c->root_directory); | |
1109 | c->root_directory = NULL; | |
5cb5a6ff | 1110 | |
80876c20 LP |
1111 | free(c->tty_path); |
1112 | c->tty_path = NULL; | |
1113 | ||
071830ff LP |
1114 | free(c->syslog_identifier); |
1115 | c->syslog_identifier = NULL; | |
1116 | ||
5cb5a6ff | 1117 | free(c->user); |
034c6ed7 LP |
1118 | c->user = NULL; |
1119 | ||
5cb5a6ff | 1120 | free(c->group); |
034c6ed7 LP |
1121 | c->group = NULL; |
1122 | ||
1123 | strv_free(c->supplementary_groups); | |
1124 | c->supplementary_groups = NULL; | |
94f04347 LP |
1125 | |
1126 | if (c->capabilities) { | |
1127 | cap_free(c->capabilities); | |
1128 | c->capabilities = NULL; | |
1129 | } | |
15ae422b LP |
1130 | |
1131 | strv_free(c->read_only_dirs); | |
1132 | c->read_only_dirs = NULL; | |
1133 | ||
1134 | strv_free(c->read_write_dirs); | |
1135 | c->read_write_dirs = NULL; | |
1136 | ||
1137 | strv_free(c->inaccessible_dirs); | |
1138 | c->inaccessible_dirs = NULL; | |
5cb5a6ff LP |
1139 | } |
1140 | ||
43d0fcbd LP |
1141 | void exec_command_done(ExecCommand *c) { |
1142 | assert(c); | |
1143 | ||
1144 | free(c->path); | |
1145 | c->path = NULL; | |
1146 | ||
1147 | strv_free(c->argv); | |
1148 | c->argv = NULL; | |
1149 | } | |
1150 | ||
1151 | void exec_command_done_array(ExecCommand *c, unsigned n) { | |
1152 | unsigned i; | |
1153 | ||
1154 | for (i = 0; i < n; i++) | |
1155 | exec_command_done(c+i); | |
1156 | } | |
1157 | ||
5cb5a6ff LP |
1158 | void exec_command_free_list(ExecCommand *c) { |
1159 | ExecCommand *i; | |
1160 | ||
1161 | while ((i = c)) { | |
034c6ed7 | 1162 | LIST_REMOVE(ExecCommand, command, c, i); |
43d0fcbd | 1163 | exec_command_done(i); |
5cb5a6ff LP |
1164 | free(i); |
1165 | } | |
1166 | } | |
1167 | ||
034c6ed7 LP |
1168 | void exec_command_free_array(ExecCommand **c, unsigned n) { |
1169 | unsigned i; | |
1170 | ||
1171 | for (i = 0; i < n; i++) { | |
1172 | exec_command_free_list(c[i]); | |
1173 | c[i] = NULL; | |
1174 | } | |
1175 | } | |
1176 | ||
15ae422b LP |
1177 | static void strv_fprintf(FILE *f, char **l) { |
1178 | char **g; | |
1179 | ||
1180 | assert(f); | |
1181 | ||
1182 | STRV_FOREACH(g, l) | |
1183 | fprintf(f, " %s", *g); | |
1184 | } | |
1185 | ||
5cb5a6ff | 1186 | void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) { |
94f04347 LP |
1187 | char ** e; |
1188 | unsigned i; | |
9eba9da4 | 1189 | |
5cb5a6ff LP |
1190 | assert(c); |
1191 | assert(f); | |
1192 | ||
1193 | if (!prefix) | |
1194 | prefix = ""; | |
1195 | ||
1196 | fprintf(f, | |
94f04347 LP |
1197 | "%sUMask: %04o\n" |
1198 | "%sWorkingDirectory: %s\n" | |
451a074f | 1199 | "%sRootDirectory: %s\n" |
15ae422b LP |
1200 | "%sNonBlocking: %s\n" |
1201 | "%sPrivateTmp: %s\n", | |
5cb5a6ff | 1202 | prefix, c->umask, |
9eba9da4 | 1203 | prefix, c->working_directory ? c->working_directory : "/", |
451a074f | 1204 | prefix, c->root_directory ? c->root_directory : "/", |
15ae422b LP |
1205 | prefix, yes_no(c->non_blocking), |
1206 | prefix, yes_no(c->private_tmp)); | |
fb33a393 | 1207 | |
94f04347 LP |
1208 | if (c->environment) |
1209 | for (e = c->environment; *e; e++) | |
1210 | fprintf(f, "%sEnvironment: %s\n", prefix, *e); | |
1211 | ||
fb33a393 LP |
1212 | if (c->nice_set) |
1213 | fprintf(f, | |
1214 | "%sNice: %i\n", | |
1215 | prefix, c->nice); | |
1216 | ||
1217 | if (c->oom_adjust_set) | |
1218 | fprintf(f, | |
1219 | "%sOOMAdjust: %i\n", | |
1220 | prefix, c->oom_adjust); | |
9eba9da4 | 1221 | |
94f04347 LP |
1222 | for (i = 0; i < RLIM_NLIMITS; i++) |
1223 | if (c->rlimit[i]) | |
ea430986 | 1224 | fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max); |
94f04347 | 1225 | |
9eba9da4 LP |
1226 | if (c->ioprio_set) |
1227 | fprintf(f, | |
1228 | "%sIOSchedulingClass: %s\n" | |
1229 | "%sIOPriority: %i\n", | |
94f04347 | 1230 | prefix, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c->ioprio)), |
9eba9da4 | 1231 | prefix, (int) IOPRIO_PRIO_DATA(c->ioprio)); |
94f04347 LP |
1232 | |
1233 | if (c->cpu_sched_set) | |
1234 | fprintf(f, | |
1235 | "%sCPUSchedulingPolicy: %s\n" | |
38b48754 LP |
1236 | "%sCPUSchedulingPriority: %i\n" |
1237 | "%sCPUSchedulingResetOnFork: %s\n", | |
94f04347 | 1238 | prefix, sched_policy_to_string(c->cpu_sched_policy), |
38b48754 LP |
1239 | prefix, c->cpu_sched_priority, |
1240 | prefix, yes_no(c->cpu_sched_reset_on_fork)); | |
94f04347 LP |
1241 | |
1242 | if (c->cpu_affinity_set) { | |
1243 | fprintf(f, "%sCPUAffinity:", prefix); | |
1244 | for (i = 0; i < CPU_SETSIZE; i++) | |
1245 | if (CPU_ISSET(i, &c->cpu_affinity)) | |
1246 | fprintf(f, " %i", i); | |
1247 | fputs("\n", f); | |
1248 | } | |
1249 | ||
1250 | if (c->timer_slack_ns_set) | |
1251 | fprintf(f, "%sTimerSlackNS: %lu\n", prefix, c->timer_slack_ns); | |
1252 | ||
1253 | fprintf(f, | |
80876c20 LP |
1254 | "%sStandardInput: %s\n" |
1255 | "%sStandardOutput: %s\n" | |
1256 | "%sStandardError: %s\n", | |
1257 | prefix, exec_input_to_string(c->std_input), | |
1258 | prefix, exec_output_to_string(c->std_output), | |
1259 | prefix, exec_output_to_string(c->std_error)); | |
1260 | ||
1261 | if (c->tty_path) | |
1262 | fprintf(f, | |
1263 | "%sTTYPath: %s\n", | |
1264 | prefix, c->tty_path); | |
94f04347 | 1265 | |
9a6bca7a LP |
1266 | if (c->std_output == EXEC_OUTPUT_SYSLOG || c->std_output == EXEC_OUTPUT_KMSG || |
1267 | c->std_error == EXEC_OUTPUT_SYSLOG || c->std_error == EXEC_OUTPUT_KMSG) | |
94f04347 LP |
1268 | fprintf(f, |
1269 | "%sSyslogFacility: %s\n" | |
1270 | "%sSyslogLevel: %s\n", | |
1271 | prefix, log_facility_to_string(LOG_FAC(c->syslog_priority)), | |
1272 | prefix, log_level_to_string(LOG_PRI(c->syslog_priority))); | |
1273 | ||
1274 | if (c->capabilities) { | |
1275 | char *t; | |
1276 | if ((t = cap_to_text(c->capabilities, NULL))) { | |
1277 | fprintf(f, "%sCapabilities: %s\n", | |
1278 | prefix, t); | |
1279 | cap_free(t); | |
1280 | } | |
1281 | } | |
1282 | ||
1283 | if (c->secure_bits) | |
1284 | fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n", | |
1285 | prefix, | |
1286 | (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "", | |
1287 | (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "", | |
1288 | (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "", | |
1289 | (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "", | |
1290 | (c->secure_bits & SECURE_NOROOT) ? " noroot" : "", | |
1291 | (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : ""); | |
1292 | ||
1293 | if (c->capability_bounding_set_drop) { | |
1294 | fprintf(f, "%sCapabilityBoundingSetDrop:", prefix); | |
1295 | ||
1296 | for (i = 0; i <= CAP_LAST_CAP; i++) | |
1297 | if (c->capability_bounding_set_drop & (1 << i)) { | |
1298 | char *t; | |
1299 | ||
1300 | if ((t = cap_to_name(i))) { | |
1301 | fprintf(f, " %s", t); | |
1302 | free(t); | |
1303 | } | |
1304 | } | |
1305 | ||
1306 | fputs("\n", f); | |
1307 | } | |
1308 | ||
1309 | if (c->user) | |
1310 | fprintf(f, "%sUser: %s", prefix, c->user); | |
1311 | if (c->group) | |
1312 | fprintf(f, "%sGroup: %s", prefix, c->group); | |
1313 | ||
15ae422b | 1314 | if (strv_length(c->supplementary_groups) > 0) { |
94f04347 | 1315 | fprintf(f, "%sSupplementaryGroups:", prefix); |
15ae422b LP |
1316 | strv_fprintf(f, c->supplementary_groups); |
1317 | fputs("\n", f); | |
1318 | } | |
94f04347 | 1319 | |
15ae422b LP |
1320 | if (strv_length(c->read_write_dirs) > 0) { |
1321 | fprintf(f, "%sReadWriteDirs:", prefix); | |
1322 | strv_fprintf(f, c->read_write_dirs); | |
1323 | fputs("\n", f); | |
1324 | } | |
1325 | ||
1326 | if (strv_length(c->read_only_dirs) > 0) { | |
1327 | fprintf(f, "%sReadOnlyDirs:", prefix); | |
1328 | strv_fprintf(f, c->read_only_dirs); | |
1329 | fputs("\n", f); | |
1330 | } | |
94f04347 | 1331 | |
15ae422b LP |
1332 | if (strv_length(c->inaccessible_dirs) > 0) { |
1333 | fprintf(f, "%sInaccessibleDirs:", prefix); | |
1334 | strv_fprintf(f, c->inaccessible_dirs); | |
94f04347 LP |
1335 | fputs("\n", f); |
1336 | } | |
5cb5a6ff LP |
1337 | } |
1338 | ||
034c6ed7 LP |
1339 | void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) { |
1340 | assert(s); | |
5cb5a6ff | 1341 | |
034c6ed7 | 1342 | s->pid = pid; |
9fb86720 LP |
1343 | s->exit_timestamp = now(CLOCK_REALTIME); |
1344 | ||
034c6ed7 LP |
1345 | s->code = code; |
1346 | s->status = status; | |
9fb86720 LP |
1347 | } |
1348 | ||
1349 | void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) { | |
1350 | char buf[FORMAT_TIMESTAMP_MAX]; | |
1351 | ||
1352 | assert(s); | |
1353 | assert(f); | |
1354 | ||
1355 | if (!prefix) | |
1356 | prefix = ""; | |
1357 | ||
1358 | if (s->pid <= 0) | |
1359 | return; | |
1360 | ||
1361 | fprintf(f, | |
1362 | "%sPID: %llu\n", | |
1363 | prefix, (unsigned long long) s->pid); | |
1364 | ||
1365 | if (s->start_timestamp > 0) | |
1366 | fprintf(f, | |
1367 | "%sStart Timestamp: %s\n", | |
1368 | prefix, format_timestamp(buf, sizeof(buf), s->start_timestamp)); | |
1369 | ||
1370 | if (s->exit_timestamp > 0) | |
1371 | fprintf(f, | |
1372 | "%sExit Timestamp: %s\n" | |
1373 | "%sExit Code: %s\n" | |
1374 | "%sExit Status: %i\n", | |
1375 | prefix, format_timestamp(buf, sizeof(buf), s->exit_timestamp), | |
1376 | prefix, sigchld_code_to_string(s->code), | |
1377 | prefix, s->status); | |
5cb5a6ff | 1378 | } |
44d8db9e | 1379 | |
9e2f7c11 | 1380 | char *exec_command_line(char **argv) { |
44d8db9e LP |
1381 | size_t k; |
1382 | char *n, *p, **a; | |
1383 | bool first = true; | |
1384 | ||
9e2f7c11 | 1385 | assert(argv); |
44d8db9e | 1386 | |
9164977d | 1387 | k = 1; |
9e2f7c11 | 1388 | STRV_FOREACH(a, argv) |
44d8db9e LP |
1389 | k += strlen(*a)+3; |
1390 | ||
1391 | if (!(n = new(char, k))) | |
1392 | return NULL; | |
1393 | ||
1394 | p = n; | |
9e2f7c11 | 1395 | STRV_FOREACH(a, argv) { |
44d8db9e LP |
1396 | |
1397 | if (!first) | |
1398 | *(p++) = ' '; | |
1399 | else | |
1400 | first = false; | |
1401 | ||
1402 | if (strpbrk(*a, WHITESPACE)) { | |
1403 | *(p++) = '\''; | |
1404 | p = stpcpy(p, *a); | |
1405 | *(p++) = '\''; | |
1406 | } else | |
1407 | p = stpcpy(p, *a); | |
1408 | ||
1409 | } | |
1410 | ||
9164977d LP |
1411 | *p = 0; |
1412 | ||
44d8db9e LP |
1413 | /* FIXME: this doesn't really handle arguments that have |
1414 | * spaces and ticks in them */ | |
1415 | ||
1416 | return n; | |
1417 | } | |
1418 | ||
1419 | void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) { | |
9fb86720 LP |
1420 | char *p2; |
1421 | const char *prefix2; | |
1422 | ||
44d8db9e LP |
1423 | char *cmd; |
1424 | ||
1425 | assert(c); | |
1426 | assert(f); | |
1427 | ||
1428 | if (!prefix) | |
1429 | prefix = ""; | |
9fb86720 LP |
1430 | p2 = strappend(prefix, "\t"); |
1431 | prefix2 = p2 ? p2 : prefix; | |
44d8db9e | 1432 | |
9e2f7c11 | 1433 | cmd = exec_command_line(c->argv); |
44d8db9e LP |
1434 | |
1435 | fprintf(f, | |
1436 | "%sCommand Line: %s\n", | |
1437 | prefix, cmd ? cmd : strerror(ENOMEM)); | |
1438 | ||
1439 | free(cmd); | |
9fb86720 LP |
1440 | |
1441 | exec_status_dump(&c->exec_status, f, prefix2); | |
1442 | ||
1443 | free(p2); | |
44d8db9e LP |
1444 | } |
1445 | ||
1446 | void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) { | |
1447 | assert(f); | |
1448 | ||
1449 | if (!prefix) | |
1450 | prefix = ""; | |
1451 | ||
1452 | LIST_FOREACH(command, c, c) | |
1453 | exec_command_dump(c, f, prefix); | |
1454 | } | |
94f04347 | 1455 | |
a6a80b4f LP |
1456 | void exec_command_append_list(ExecCommand **l, ExecCommand *e) { |
1457 | ExecCommand *end; | |
1458 | ||
1459 | assert(l); | |
1460 | assert(e); | |
1461 | ||
1462 | if (*l) { | |
1463 | /* It's kinda important that we keep the order here */ | |
1464 | LIST_FIND_TAIL(ExecCommand, command, *l, end); | |
1465 | LIST_INSERT_AFTER(ExecCommand, command, *l, end, e); | |
1466 | } else | |
1467 | *l = e; | |
1468 | } | |
1469 | ||
26fd040d LP |
1470 | int exec_command_set(ExecCommand *c, const char *path, ...) { |
1471 | va_list ap; | |
1472 | char **l, *p; | |
1473 | ||
1474 | assert(c); | |
1475 | assert(path); | |
1476 | ||
1477 | va_start(ap, path); | |
1478 | l = strv_new_ap(path, ap); | |
1479 | va_end(ap); | |
1480 | ||
1481 | if (!l) | |
1482 | return -ENOMEM; | |
1483 | ||
1484 | if (!(p = strdup(path))) { | |
1485 | strv_free(l); | |
1486 | return -ENOMEM; | |
1487 | } | |
1488 | ||
1489 | free(c->path); | |
1490 | c->path = p; | |
1491 | ||
1492 | strv_free(c->argv); | |
1493 | c->argv = l; | |
1494 | ||
1495 | return 0; | |
1496 | } | |
1497 | ||
80876c20 | 1498 | const char* exit_status_to_string(ExitStatus status) { |
b8d3418f LP |
1499 | |
1500 | /* We cast to int here, so that -Wenum doesn't complain that | |
1501 | * EXIT_SUCCESS/EXIT_FAILURE aren't in the enum */ | |
1502 | ||
1503 | switch ((int) status) { | |
80876c20 LP |
1504 | |
1505 | case EXIT_SUCCESS: | |
1506 | return "SUCCESS"; | |
1507 | ||
1508 | case EXIT_FAILURE: | |
1509 | return "FAILURE"; | |
1510 | ||
1511 | case EXIT_INVALIDARGUMENT: | |
1512 | return "INVALIDARGUMENT"; | |
1513 | ||
1514 | case EXIT_NOTIMPLEMENTED: | |
1515 | return "NOTIMPLEMENTED"; | |
1516 | ||
1517 | case EXIT_NOPERMISSION: | |
1518 | return "NOPERMISSION"; | |
1519 | ||
1520 | case EXIT_NOTINSTALLED: | |
1521 | return "NOTINSSTALLED"; | |
1522 | ||
1523 | case EXIT_NOTCONFIGURED: | |
1524 | return "NOTCONFIGURED"; | |
1525 | ||
1526 | case EXIT_NOTRUNNING: | |
1527 | return "NOTRUNNING"; | |
1528 | ||
1529 | case EXIT_CHDIR: | |
1530 | return "CHDIR"; | |
1531 | ||
1532 | case EXIT_NICE: | |
1533 | return "NICE"; | |
1534 | ||
1535 | case EXIT_FDS: | |
1536 | return "FDS"; | |
1537 | ||
1538 | case EXIT_EXEC: | |
1539 | return "EXEC"; | |
1540 | ||
1541 | case EXIT_MEMORY: | |
1542 | return "MEMORY"; | |
1543 | ||
1544 | case EXIT_LIMITS: | |
1545 | return "LIMITS"; | |
1546 | ||
1547 | case EXIT_OOM_ADJUST: | |
1548 | return "OOM_ADJUST"; | |
1549 | ||
1550 | case EXIT_SIGNAL_MASK: | |
1551 | return "SIGNAL_MASK"; | |
1552 | ||
1553 | case EXIT_STDIN: | |
1554 | return "STDIN"; | |
1555 | ||
1556 | case EXIT_STDOUT: | |
1557 | return "STDOUT"; | |
1558 | ||
1559 | case EXIT_CHROOT: | |
1560 | return "CHROOT"; | |
1561 | ||
1562 | case EXIT_IOPRIO: | |
1563 | return "IOPRIO"; | |
1564 | ||
1565 | case EXIT_TIMERSLACK: | |
1566 | return "TIMERSLACK"; | |
1567 | ||
1568 | case EXIT_SECUREBITS: | |
1569 | return "SECUREBITS"; | |
1570 | ||
1571 | case EXIT_SETSCHEDULER: | |
1572 | return "SETSCHEDULER"; | |
1573 | ||
1574 | case EXIT_CPUAFFINITY: | |
1575 | return "CPUAFFINITY"; | |
1576 | ||
1577 | case EXIT_GROUP: | |
1578 | return "GROUP"; | |
1579 | ||
1580 | case EXIT_USER: | |
1581 | return "USER"; | |
1582 | ||
1583 | case EXIT_CAPABILITIES: | |
1584 | return "CAPABILITIES"; | |
1585 | ||
1586 | case EXIT_CGROUP: | |
1587 | return "CGROUP"; | |
1588 | ||
1589 | case EXIT_SETSID: | |
1590 | return "SETSID"; | |
1591 | ||
1592 | case EXIT_CONFIRM: | |
1593 | return "CONFIRM"; | |
1594 | ||
1595 | case EXIT_STDERR: | |
1596 | return "STDERR"; | |
1597 | ||
1598 | default: | |
1599 | return NULL; | |
1600 | } | |
1601 | } | |
1602 | ||
1603 | static const char* const exec_input_table[_EXEC_INPUT_MAX] = { | |
1604 | [EXEC_INPUT_NULL] = "null", | |
1605 | [EXEC_INPUT_TTY] = "tty", | |
1606 | [EXEC_INPUT_TTY_FORCE] = "tty-force", | |
4f2d528d LP |
1607 | [EXEC_INPUT_TTY_FAIL] = "tty-fail", |
1608 | [EXEC_INPUT_SOCKET] = "socket" | |
80876c20 LP |
1609 | }; |
1610 | ||
94f04347 | 1611 | static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = { |
80876c20 | 1612 | [EXEC_OUTPUT_INHERIT] = "inherit", |
94f04347 | 1613 | [EXEC_OUTPUT_NULL] = "null", |
80876c20 | 1614 | [EXEC_OUTPUT_TTY] = "tty", |
94f04347 | 1615 | [EXEC_OUTPUT_SYSLOG] = "syslog", |
9a6bca7a | 1616 | [EXEC_OUTPUT_KMSG] = "kmsg", |
4f2d528d | 1617 | [EXEC_OUTPUT_SOCKET] = "socket" |
94f04347 LP |
1618 | }; |
1619 | ||
1620 | DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput); | |
1621 | ||
94f04347 | 1622 | DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput); |