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