chiark / gitweb /
importd: add new bus calls for importing local tar and raw images
[elogind.git] / src / import / import-common.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2015 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 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   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/prctl.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25
26 #include "util.h"
27 #include "btrfs-util.h"
28 #include "capability.h"
29 #include "import-common.h"
30
31 int import_make_read_only_fd(int fd) {
32         int r;
33
34         assert(fd >= 0);
35
36         /* First, let's make this a read-only subvolume if it refers
37          * to a subvolume */
38         r = btrfs_subvol_set_read_only_fd(fd, true);
39         if (r == -ENOTTY || r == -ENOTDIR || r == -EINVAL) {
40                 struct stat st;
41
42                 /* This doesn't refer to a subvolume, or the file
43                  * system isn't even btrfs. In that, case fall back to
44                  * chmod()ing */
45
46                 r = fstat(fd, &st);
47                 if (r < 0)
48                         return log_error_errno(errno, "Failed to stat temporary image: %m");
49
50                 /* Drop "w" flag */
51                 if (fchmod(fd, st.st_mode & 07555) < 0)
52                         return log_error_errno(errno, "Failed to chmod() final image: %m");
53
54                 return 0;
55
56         } else if (r < 0)
57                 return log_error_errno(r, "Failed to make subvolume read-only: %m");
58
59         return 0;
60 }
61
62 int import_make_read_only(const char *path) {
63         _cleanup_close_ int fd = 1;
64
65         fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
66         if (fd < 0)
67                 return log_error_errno(errno, "Failed to open %s: %m", path);
68
69         return import_make_read_only_fd(fd);
70 }
71
72 int import_fork_tar(const char *path, pid_t *ret) {
73         _cleanup_close_pair_ int pipefd[2] = { -1, -1 };
74         pid_t pid;
75         int r;
76
77         assert(path);
78         assert(ret);
79
80         if (pipe2(pipefd, O_CLOEXEC) < 0)
81                 return log_error_errno(errno, "Failed to create pipe for tar: %m");
82
83         pid = fork();
84         if (pid < 0)
85                 return log_error_errno(errno, "Failed to fork off tar: %m");
86
87         if (pid == 0) {
88                 int null_fd;
89                 uint64_t retain =
90                         (1ULL << CAP_CHOWN) |
91                         (1ULL << CAP_FOWNER) |
92                         (1ULL << CAP_FSETID) |
93                         (1ULL << CAP_MKNOD) |
94                         (1ULL << CAP_SETFCAP) |
95                         (1ULL << CAP_DAC_OVERRIDE);
96
97                 /* Child */
98
99                 reset_all_signal_handlers();
100                 reset_signal_mask();
101                 assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
102
103                 pipefd[1] = safe_close(pipefd[1]);
104
105                 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
106                         log_error_errno(errno, "Failed to dup2() fd: %m");
107                         _exit(EXIT_FAILURE);
108                 }
109
110                 if (pipefd[0] != STDIN_FILENO)
111                         pipefd[0] = safe_close(pipefd[0]);
112
113                 null_fd = open("/dev/null", O_WRONLY|O_NOCTTY);
114                 if (null_fd < 0) {
115                         log_error_errno(errno, "Failed to open /dev/null: %m");
116                         _exit(EXIT_FAILURE);
117                 }
118
119                 if (dup2(null_fd, STDOUT_FILENO) != STDOUT_FILENO) {
120                         log_error_errno(errno, "Failed to dup2() fd: %m");
121                         _exit(EXIT_FAILURE);
122                 }
123
124                 if (null_fd != STDOUT_FILENO)
125                         null_fd = safe_close(null_fd);
126
127                 fd_cloexec(STDIN_FILENO, false);
128                 fd_cloexec(STDOUT_FILENO, false);
129                 fd_cloexec(STDERR_FILENO, false);
130
131                 if (unshare(CLONE_NEWNET) < 0)
132                         log_error_errno(errno, "Failed to lock tar into network namespace, ignoring: %m");
133
134                 r = capability_bounding_set_drop(~retain, true);
135                 if (r < 0)
136                         log_error_errno(r, "Failed to drop capabilities, ignoring: %m");
137
138                 execlp("tar", "tar", "--numeric-owner", "-C", path, "-px", NULL);
139                 log_error_errno(errno, "Failed to execute tar: %m");
140                 _exit(EXIT_FAILURE);
141         }
142
143         pipefd[0] = safe_close(pipefd[0]);
144         r = pipefd[1];
145         pipefd[1] = -1;
146
147         *ret = pid;
148
149         return r;
150 }