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