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