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