1 | /* Copyright © 2005-2008 Roger Leigh <rleigh@debian.org> |
---|
2 | * |
---|
3 | * schroot is free software: you can redistribute it and/or modify it |
---|
4 | * under the terms of the GNU General Public License as published by |
---|
5 | * the Free Software Foundation, either version 3 of the License, or |
---|
6 | * (at your option) any later version. |
---|
7 | * |
---|
8 | * schroot is distributed in the hope that it will be useful, but |
---|
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
11 | * General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU General Public License |
---|
14 | * along with this program. If not, see |
---|
15 | * <http://www.gnu.org/licenses/>. |
---|
16 | * |
---|
17 | *********************************************************************/ |
---|
18 | |
---|
19 | #include <config.h> |
---|
20 | |
---|
21 | #include "sbuild-chroot-loopback.h" |
---|
22 | #include "sbuild-chroot-facet-session-clonable.h" |
---|
23 | #include "sbuild-chroot-facet-mountable.h" |
---|
24 | #ifdef SBUILD_FEATURE_UNION |
---|
25 | #include "sbuild-chroot-facet-union.h" |
---|
26 | #endif // SBUILD_FEATURE_UNION |
---|
27 | #include "sbuild-format-detail.h" |
---|
28 | #include "sbuild-lock.h" |
---|
29 | #include "sbuild-util.h" |
---|
30 | |
---|
31 | #include <cassert> |
---|
32 | #include <cerrno> |
---|
33 | #include <cstring> |
---|
34 | |
---|
35 | #include <boost/format.hpp> |
---|
36 | |
---|
37 | using boost::format; |
---|
38 | using namespace sbuild; |
---|
39 | |
---|
40 | chroot_loopback::chroot_loopback (): |
---|
41 | chroot(), |
---|
42 | file() |
---|
43 | { |
---|
44 | add_facet(sbuild::chroot_facet_mountable::create()); |
---|
45 | #ifdef SBUILD_FEATURE_UNION |
---|
46 | add_facet(sbuild::chroot_facet_union::create()); |
---|
47 | #endif // SBUILD_FEATURE_UNION |
---|
48 | } |
---|
49 | |
---|
50 | chroot_loopback::~chroot_loopback () |
---|
51 | { |
---|
52 | } |
---|
53 | |
---|
54 | chroot_loopback::chroot_loopback (const chroot_loopback& rhs): |
---|
55 | chroot(rhs), |
---|
56 | file(rhs.file) |
---|
57 | { |
---|
58 | } |
---|
59 | |
---|
60 | sbuild::chroot::ptr |
---|
61 | chroot_loopback::clone () const |
---|
62 | { |
---|
63 | return ptr(new chroot_loopback(*this)); |
---|
64 | } |
---|
65 | |
---|
66 | sbuild::chroot::ptr |
---|
67 | chroot_loopback::clone_session (std::string const& session_id, |
---|
68 | std::string const& user, |
---|
69 | bool root) const |
---|
70 | { |
---|
71 | chroot_facet_session_clonable::const_ptr psess |
---|
72 | (get_facet<chroot_facet_session_clonable>()); |
---|
73 | assert(psess); |
---|
74 | |
---|
75 | ptr session(new chroot_loopback(*this)); |
---|
76 | psess->clone_session_setup(session, session_id, user, root); |
---|
77 | |
---|
78 | return session; |
---|
79 | } |
---|
80 | |
---|
81 | sbuild::chroot::ptr |
---|
82 | chroot_loopback::clone_source () const |
---|
83 | { |
---|
84 | ptr clone; |
---|
85 | |
---|
86 | #ifdef SBUILD_FEATURE_UNION |
---|
87 | chroot_facet_union::const_ptr puni(get_facet<chroot_facet_union>()); |
---|
88 | assert(puni); |
---|
89 | |
---|
90 | if (puni->get_union_configured()) { |
---|
91 | clone = ptr(new chroot_loopback(*this)); |
---|
92 | puni->clone_source_setup(clone); |
---|
93 | } |
---|
94 | #endif // SBUILD_FEATURE_UNION |
---|
95 | |
---|
96 | return clone; |
---|
97 | } |
---|
98 | |
---|
99 | std::string const& |
---|
100 | chroot_loopback::get_file () const |
---|
101 | { |
---|
102 | return this->file; |
---|
103 | } |
---|
104 | |
---|
105 | void |
---|
106 | chroot_loopback::set_file (std::string const& file) |
---|
107 | { |
---|
108 | if (!is_absname(file)) |
---|
109 | throw error(file, FILE_ABS); |
---|
110 | |
---|
111 | this->file = file; |
---|
112 | |
---|
113 | chroot_facet_mountable::ptr pmnt |
---|
114 | (get_facet<chroot_facet_mountable>()); |
---|
115 | pmnt->set_mount_device(this->file); |
---|
116 | } |
---|
117 | |
---|
118 | std::string |
---|
119 | chroot_loopback::get_path () const |
---|
120 | { |
---|
121 | chroot_facet_mountable::const_ptr pmnt |
---|
122 | (get_facet<chroot_facet_mountable>()); |
---|
123 | |
---|
124 | std::string path(get_mount_location()); |
---|
125 | |
---|
126 | if (pmnt) |
---|
127 | path += pmnt->get_location(); |
---|
128 | |
---|
129 | return path; |
---|
130 | } |
---|
131 | |
---|
132 | std::string const& |
---|
133 | chroot_loopback::get_chroot_type () const |
---|
134 | { |
---|
135 | static const std::string type("loopback"); |
---|
136 | |
---|
137 | return type; |
---|
138 | } |
---|
139 | |
---|
140 | void |
---|
141 | chroot_loopback::setup_env (chroot const& chroot, |
---|
142 | environment& env) const |
---|
143 | { |
---|
144 | chroot::setup_env(chroot, env); |
---|
145 | |
---|
146 | env.add("CHROOT_FILE", get_file()); |
---|
147 | } |
---|
148 | |
---|
149 | void |
---|
150 | chroot_loopback::setup_lock (chroot::setup_type type, |
---|
151 | bool lock, |
---|
152 | int status) |
---|
153 | { |
---|
154 | // Check ownership and permissions. |
---|
155 | if (type == SETUP_START && lock == true) |
---|
156 | { |
---|
157 | stat file_status(this->file); |
---|
158 | |
---|
159 | // NOTE: taken from chroot_config::check_security. |
---|
160 | if (file_status.uid() != 0) |
---|
161 | throw error(this->file, FILE_OWNER); |
---|
162 | if (file_status.check_mode(stat::PERM_OTHER_WRITE)) |
---|
163 | throw error(this->file, FILE_PERMS); |
---|
164 | if (!file_status.is_regular()) |
---|
165 | throw error(this->file, FILE_NOTREG); |
---|
166 | } |
---|
167 | |
---|
168 | /* By default, loopback chroots do no locking. */ |
---|
169 | #ifdef SBUILD_FEATURE_UNION |
---|
170 | /** |
---|
171 | * By default, loopback chroots do no locking, but can create sessions |
---|
172 | * using filesystem unions. |
---|
173 | */ |
---|
174 | chroot_facet_union::const_ptr puni(get_facet<chroot_facet_union>()); |
---|
175 | assert(puni); |
---|
176 | |
---|
177 | if (puni->get_union_configured() && |
---|
178 | ((type == SETUP_START && lock == true) || |
---|
179 | (type == SETUP_STOP && lock == false && status == 0))) |
---|
180 | { |
---|
181 | bool start = (type == SETUP_START); |
---|
182 | setup_session_info(start); |
---|
183 | } |
---|
184 | #endif |
---|
185 | } |
---|
186 | |
---|
187 | sbuild::chroot::session_flags |
---|
188 | chroot_loopback::get_session_flags (chroot const& chroot) const |
---|
189 | { |
---|
190 | return chroot::SESSION_NOFLAGS; |
---|
191 | } |
---|
192 | |
---|
193 | void |
---|
194 | chroot_loopback::get_details (chroot const& chroot, |
---|
195 | format_detail& detail) const |
---|
196 | { |
---|
197 | chroot::get_details(chroot, detail); |
---|
198 | |
---|
199 | if (!this->file.empty()) |
---|
200 | detail.add(_("File"), get_file()); |
---|
201 | } |
---|
202 | |
---|
203 | void |
---|
204 | chroot_loopback::get_keyfile (chroot const& chroot, |
---|
205 | keyfile& keyfile) const |
---|
206 | { |
---|
207 | chroot::get_keyfile(chroot, keyfile); |
---|
208 | |
---|
209 | keyfile::set_object_value(*this, &chroot_loopback::get_file, |
---|
210 | keyfile, get_keyfile_name(), "file"); |
---|
211 | } |
---|
212 | |
---|
213 | void |
---|
214 | chroot_loopback::set_keyfile (chroot& chroot, |
---|
215 | keyfile const& keyfile, |
---|
216 | string_list& used_keys) |
---|
217 | { |
---|
218 | chroot::set_keyfile(chroot, keyfile, used_keys); |
---|
219 | |
---|
220 | keyfile::get_object_value(*this, &chroot_loopback::set_file, |
---|
221 | keyfile, get_keyfile_name(), "file", |
---|
222 | keyfile::PRIORITY_REQUIRED); |
---|
223 | used_keys.push_back("file"); |
---|
224 | } |
---|