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.h" |
---|
22 | #include "sbuild-chroot-directory.h" |
---|
23 | #include "sbuild-chroot-plain.h" |
---|
24 | #include "sbuild-chroot-file.h" |
---|
25 | #ifdef SBUILD_FEATURE_BLOCKDEV |
---|
26 | #include "sbuild-chroot-block-device.h" |
---|
27 | #endif // SBUILD_FEATURE_BLOCKDEV |
---|
28 | #ifdef SBUILD_FEATURE_LOOPBACK |
---|
29 | #include "sbuild-chroot-loopback.h" |
---|
30 | #endif // SBUILD_FEATURE_LOOPBACK |
---|
31 | #ifdef SBUILD_FEATURE_LVMSNAP |
---|
32 | #include "sbuild-chroot-lvm-snapshot.h" |
---|
33 | #endif // SBUILD_FEATURE_LVMSNAP |
---|
34 | #include "sbuild-chroot-facet.h" |
---|
35 | #include "sbuild-chroot-facet-personality.h" |
---|
36 | #include "sbuild-chroot-facet-session.h" |
---|
37 | #include "sbuild-chroot-facet-session-clonable.h" |
---|
38 | #include "sbuild-lock.h" |
---|
39 | |
---|
40 | #include <cerrno> |
---|
41 | #include <map> |
---|
42 | #include <utility> |
---|
43 | |
---|
44 | #include <ext/stdio_filebuf.h> |
---|
45 | |
---|
46 | #include <sys/types.h> |
---|
47 | #include <sys/stat.h> |
---|
48 | #include <fcntl.h> |
---|
49 | |
---|
50 | #include <boost/format.hpp> |
---|
51 | |
---|
52 | using boost::format; |
---|
53 | using namespace sbuild; |
---|
54 | |
---|
55 | namespace |
---|
56 | { |
---|
57 | |
---|
58 | typedef std::pair<sbuild::chroot::error_code,const char *> emap; |
---|
59 | |
---|
60 | /** |
---|
61 | * This is a list of the supported error codes. It's used to |
---|
62 | * construct the real error codes map. |
---|
63 | */ |
---|
64 | emap init_errors[] = |
---|
65 | { |
---|
66 | emap(sbuild::chroot::CHROOT_CREATE, N_("Chroot creation failed")), |
---|
67 | emap(sbuild::chroot::CHROOT_DEVICE, N_("Device name not set")), |
---|
68 | // TRANSLATORS: %1% = chroot type name |
---|
69 | emap(sbuild::chroot::CHROOT_TYPE, N_("Unknown chroot type '%1%'")), |
---|
70 | emap(sbuild::chroot::DEVICE_ABS, N_("Device must have an absolute path")), |
---|
71 | emap(sbuild::chroot::DEVICE_LOCK, N_("Failed to lock device")), |
---|
72 | emap(sbuild::chroot::DEVICE_NOTBLOCK, N_("File is not a block device")), |
---|
73 | emap(sbuild::chroot::DEVICE_UNLOCK, N_("Failed to unlock device")), |
---|
74 | emap(sbuild::chroot::DIRECTORY_ABS, N_("Directory must have an absolute path")), |
---|
75 | emap(sbuild::chroot::FACET_INVALID, N_("Attempt to add object which is not a facet")), |
---|
76 | emap(sbuild::chroot::FACET_PRESENT, N_("Attempt to add facet which is already in use")), |
---|
77 | emap(sbuild::chroot::FILE_ABS, N_("File must have an absolute path")), |
---|
78 | emap(sbuild::chroot::FILE_LOCK, N_("Failed to acquire file lock")), |
---|
79 | emap(sbuild::chroot::FILE_NOTREG, N_("File is not a regular file")), |
---|
80 | emap(sbuild::chroot::FILE_OWNER, N_("File is not owned by user root")), |
---|
81 | emap(sbuild::chroot::FILE_PERMS, N_("File has write permissions for others")), |
---|
82 | emap(sbuild::chroot::FILE_UNLOCK, N_("Failed to discard file lock")), |
---|
83 | emap(sbuild::chroot::LOCATION_ABS, N_("Location must have an absolute path")), |
---|
84 | // TRANSLATORS: unlink refers to the C function which removes a file |
---|
85 | emap(sbuild::chroot::SESSION_UNLINK, N_("Failed to unlink session file")), |
---|
86 | emap(sbuild::chroot::SESSION_WRITE, N_("Failed to write session file")) |
---|
87 | }; |
---|
88 | |
---|
89 | } |
---|
90 | |
---|
91 | template<> |
---|
92 | error<sbuild::chroot::error_code>::map_type |
---|
93 | error<sbuild::chroot::error_code>::error_strings |
---|
94 | (init_errors, |
---|
95 | init_errors + (sizeof(init_errors) / sizeof(init_errors[0]))); |
---|
96 | |
---|
97 | sbuild::chroot::chroot (): |
---|
98 | name(), |
---|
99 | session_id(), |
---|
100 | description(), |
---|
101 | priority(0), |
---|
102 | users(), |
---|
103 | groups(), |
---|
104 | root_users(), |
---|
105 | root_groups(), |
---|
106 | aliases(), |
---|
107 | environment_filter(SBUILD_DEFAULT_ENVIRONMENT_FILTER), |
---|
108 | mount_location(), |
---|
109 | original(true), |
---|
110 | run_setup_scripts(true), |
---|
111 | script_config("script-defaults"), |
---|
112 | command_prefix(), |
---|
113 | facets() |
---|
114 | { |
---|
115 | add_facet(sbuild::chroot_facet_personality::create()); |
---|
116 | add_facet(sbuild::chroot_facet_session_clonable::create()); |
---|
117 | } |
---|
118 | |
---|
119 | sbuild::chroot::chroot (const chroot& rhs): |
---|
120 | name(rhs.name), |
---|
121 | session_id(rhs.session_id), |
---|
122 | description(rhs.description), |
---|
123 | priority(rhs.priority), |
---|
124 | users(rhs.users), |
---|
125 | groups(rhs.groups), |
---|
126 | root_users(rhs.root_users), |
---|
127 | root_groups(rhs.root_groups), |
---|
128 | aliases(rhs.aliases), |
---|
129 | environment_filter(rhs.environment_filter), |
---|
130 | mount_location(rhs.mount_location), |
---|
131 | original(rhs.original), |
---|
132 | run_setup_scripts(rhs.run_setup_scripts), |
---|
133 | script_config(rhs.script_config), |
---|
134 | command_prefix(rhs.command_prefix), |
---|
135 | facets() |
---|
136 | { |
---|
137 | /// @todo Use internal version of add_facet to add chroot pointer. |
---|
138 | for (facet_list::const_iterator pos = rhs.facets.begin(); |
---|
139 | pos != rhs.facets.end(); |
---|
140 | ++pos) |
---|
141 | { |
---|
142 | facet_ptr fp = (*pos)->clone(); |
---|
143 | fp->set_chroot(*this); |
---|
144 | facets.push_back(fp); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | sbuild::chroot::~chroot () |
---|
149 | { |
---|
150 | } |
---|
151 | |
---|
152 | sbuild::chroot::ptr |
---|
153 | sbuild::chroot::create (std::string const& type) |
---|
154 | { |
---|
155 | chroot *new_chroot = 0; |
---|
156 | |
---|
157 | if (type == "directory") |
---|
158 | new_chroot = new chroot_directory(); |
---|
159 | else if (type == "plain") |
---|
160 | new_chroot = new chroot_plain(); |
---|
161 | else if (type == "file") |
---|
162 | new_chroot = new chroot_file(); |
---|
163 | #ifdef SBUILD_FEATURE_BLOCKDEV |
---|
164 | else if (type == "block-device") |
---|
165 | new_chroot = new chroot_block_device(); |
---|
166 | #endif // SBUILD_FEATURE_BLOCKDEV |
---|
167 | #ifdef SBUILD_FEATURE_LOOPBACK |
---|
168 | else if (type == "loopback") |
---|
169 | new_chroot = new chroot_loopback(); |
---|
170 | #endif // SBUILD_FEATURE_LOOPBACK |
---|
171 | #ifdef SBUILD_FEATURE_LVMSNAP |
---|
172 | else if (type == "lvm-snapshot") |
---|
173 | new_chroot = new chroot_lvm_snapshot(); |
---|
174 | #endif // SBUILD_FEATURE_LVMSNAP |
---|
175 | else |
---|
176 | throw error(type, CHROOT_TYPE); |
---|
177 | |
---|
178 | if (new_chroot == 0) |
---|
179 | throw error(CHROOT_CREATE); |
---|
180 | |
---|
181 | return ptr(new_chroot); |
---|
182 | } |
---|
183 | |
---|
184 | std::string const& |
---|
185 | sbuild::chroot::get_name () const |
---|
186 | { |
---|
187 | return this->name; |
---|
188 | } |
---|
189 | |
---|
190 | void |
---|
191 | sbuild::chroot::set_name (std::string const& name) |
---|
192 | { |
---|
193 | this->name = name; |
---|
194 | } |
---|
195 | |
---|
196 | std::string const& |
---|
197 | sbuild::chroot::get_session_id () const |
---|
198 | { |
---|
199 | return this->session_id; |
---|
200 | } |
---|
201 | |
---|
202 | void |
---|
203 | sbuild::chroot::set_session_id (std::string const& session_id) |
---|
204 | { |
---|
205 | this->session_id = session_id; |
---|
206 | set_name(session_id); |
---|
207 | set_aliases(string_list()); |
---|
208 | } |
---|
209 | |
---|
210 | std::string const& |
---|
211 | sbuild::chroot::get_keyfile_name () const |
---|
212 | { |
---|
213 | if (get_active()) |
---|
214 | return this->session_id; |
---|
215 | else |
---|
216 | return this->name; |
---|
217 | } |
---|
218 | |
---|
219 | std::string const& |
---|
220 | sbuild::chroot::get_description () const |
---|
221 | { |
---|
222 | return this->description; |
---|
223 | } |
---|
224 | |
---|
225 | void |
---|
226 | sbuild::chroot::set_description (std::string const& description) |
---|
227 | { |
---|
228 | this->description = description; |
---|
229 | } |
---|
230 | |
---|
231 | std::string const& |
---|
232 | sbuild::chroot::get_mount_location () const |
---|
233 | { |
---|
234 | return this->mount_location; |
---|
235 | } |
---|
236 | |
---|
237 | void |
---|
238 | sbuild::chroot::set_mount_location (std::string const& location) |
---|
239 | { |
---|
240 | if (!location.empty() && !is_absname(location)) |
---|
241 | throw error(location, LOCATION_ABS); |
---|
242 | this->mount_location = location; |
---|
243 | } |
---|
244 | |
---|
245 | unsigned int |
---|
246 | sbuild::chroot::get_priority () const |
---|
247 | { |
---|
248 | return this->priority; |
---|
249 | } |
---|
250 | |
---|
251 | void |
---|
252 | sbuild::chroot::set_priority (unsigned int priority) |
---|
253 | { |
---|
254 | this->priority = priority; |
---|
255 | } |
---|
256 | |
---|
257 | string_list const& |
---|
258 | sbuild::chroot::get_users () const |
---|
259 | { |
---|
260 | return this->users; |
---|
261 | } |
---|
262 | |
---|
263 | void |
---|
264 | sbuild::chroot::set_users (string_list const& users) |
---|
265 | { |
---|
266 | this->users = users; |
---|
267 | } |
---|
268 | |
---|
269 | string_list const& |
---|
270 | sbuild::chroot::get_groups () const |
---|
271 | { |
---|
272 | return this->groups; |
---|
273 | } |
---|
274 | |
---|
275 | void |
---|
276 | sbuild::chroot::set_groups (string_list const& groups) |
---|
277 | { |
---|
278 | this->groups = groups; |
---|
279 | } |
---|
280 | |
---|
281 | string_list const& |
---|
282 | sbuild::chroot::get_root_users () const |
---|
283 | { |
---|
284 | return this->root_users; |
---|
285 | } |
---|
286 | |
---|
287 | void |
---|
288 | sbuild::chroot::set_root_users (string_list const& users) |
---|
289 | { |
---|
290 | this->root_users = users; |
---|
291 | } |
---|
292 | |
---|
293 | string_list const& |
---|
294 | sbuild::chroot::get_root_groups () const |
---|
295 | { |
---|
296 | return this->root_groups; |
---|
297 | } |
---|
298 | |
---|
299 | void |
---|
300 | sbuild::chroot::set_root_groups (string_list const& groups) |
---|
301 | { |
---|
302 | this->root_groups = groups; |
---|
303 | } |
---|
304 | |
---|
305 | string_list const& |
---|
306 | sbuild::chroot::get_aliases () const |
---|
307 | { |
---|
308 | return this->aliases; |
---|
309 | } |
---|
310 | |
---|
311 | void |
---|
312 | sbuild::chroot::set_aliases (string_list const& aliases) |
---|
313 | { |
---|
314 | this->aliases = aliases; |
---|
315 | } |
---|
316 | |
---|
317 | regex const& |
---|
318 | sbuild::chroot::get_environment_filter () const |
---|
319 | { |
---|
320 | return this->environment_filter; |
---|
321 | } |
---|
322 | |
---|
323 | void |
---|
324 | sbuild::chroot::set_environment_filter (regex const& environment_filter) |
---|
325 | { |
---|
326 | this->environment_filter = environment_filter; |
---|
327 | } |
---|
328 | |
---|
329 | bool |
---|
330 | sbuild::chroot::get_active () const |
---|
331 | { |
---|
332 | chroot_facet_session::const_ptr psess(get_facet<chroot_facet_session>()); |
---|
333 | |
---|
334 | return (psess) ? true : false; |
---|
335 | } |
---|
336 | |
---|
337 | bool |
---|
338 | sbuild::chroot::get_original () const |
---|
339 | { |
---|
340 | return this->original; |
---|
341 | } |
---|
342 | |
---|
343 | void |
---|
344 | sbuild::chroot::set_original (bool original) |
---|
345 | { |
---|
346 | this->original = original; |
---|
347 | } |
---|
348 | |
---|
349 | bool |
---|
350 | sbuild::chroot::get_run_setup_scripts () const |
---|
351 | { |
---|
352 | return this->run_setup_scripts; |
---|
353 | } |
---|
354 | |
---|
355 | void |
---|
356 | sbuild::chroot::set_run_setup_scripts (bool run_setup_scripts) |
---|
357 | { |
---|
358 | this->run_setup_scripts = run_setup_scripts; |
---|
359 | } |
---|
360 | |
---|
361 | std::string const& |
---|
362 | sbuild::chroot::get_script_config () const |
---|
363 | { |
---|
364 | return this->script_config; |
---|
365 | } |
---|
366 | |
---|
367 | void |
---|
368 | sbuild::chroot::set_script_config (std::string const& script_config) |
---|
369 | { |
---|
370 | this->script_config = script_config; |
---|
371 | } |
---|
372 | |
---|
373 | string_list const& |
---|
374 | sbuild::chroot::get_command_prefix () const |
---|
375 | { |
---|
376 | return this->command_prefix; |
---|
377 | } |
---|
378 | |
---|
379 | void |
---|
380 | sbuild::chroot::set_command_prefix (string_list const& command_prefix) |
---|
381 | { |
---|
382 | this->command_prefix = command_prefix; |
---|
383 | } |
---|
384 | |
---|
385 | string_list |
---|
386 | sbuild::chroot::list_facets () const |
---|
387 | { |
---|
388 | string_list fnames; |
---|
389 | |
---|
390 | for (facet_list::const_iterator pos = facets.begin(); |
---|
391 | pos != facets.end(); |
---|
392 | ++pos) |
---|
393 | { |
---|
394 | fnames.push_back((*pos)->get_name()); |
---|
395 | } |
---|
396 | |
---|
397 | return fnames; |
---|
398 | } |
---|
399 | |
---|
400 | void |
---|
401 | sbuild::chroot::setup_env (environment& env) const |
---|
402 | { |
---|
403 | setup_env(*this, env); |
---|
404 | |
---|
405 | for (facet_list::const_iterator pos = facets.begin(); |
---|
406 | pos != facets.end(); |
---|
407 | ++pos) |
---|
408 | { |
---|
409 | (*pos)->setup_env(*this, env); |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | void |
---|
414 | sbuild::chroot::setup_env (chroot const& chroot, |
---|
415 | environment& env) const |
---|
416 | { |
---|
417 | env.add("CHROOT_TYPE", chroot.get_chroot_type()); |
---|
418 | env.add("CHROOT_NAME", chroot.get_name()); |
---|
419 | // env.add("CHROOT_SESSION_NAME", chroot.get_keyfile_name()); |
---|
420 | env.add("CHROOT_DESCRIPTION", chroot.get_description()); |
---|
421 | env.add("CHROOT_MOUNT_LOCATION", chroot.get_mount_location()); |
---|
422 | env.add("CHROOT_PATH", chroot.get_path()); |
---|
423 | env.add("CHROOT_SCRIPT_CONFIG", normalname(std::string(PACKAGE_SYSCONF_DIR) + '/' + chroot.get_script_config())); |
---|
424 | env.add("CHROOT_SESSION_CREATE", |
---|
425 | static_cast<bool>(chroot.get_session_flags() & SESSION_CREATE)); |
---|
426 | env.add("CHROOT_SESSION_CLONE", |
---|
427 | static_cast<bool>(chroot.get_session_flags() & SESSION_CLONE)); |
---|
428 | env.add("CHROOT_SESSION_PURGE", |
---|
429 | static_cast<bool>(chroot.get_session_flags() & SESSION_PURGE)); |
---|
430 | } |
---|
431 | |
---|
432 | void |
---|
433 | sbuild::chroot::setup_session_info (bool start) |
---|
434 | { |
---|
435 | /* Create or unlink session information. */ |
---|
436 | std::string file = std::string(SCHROOT_SESSION_DIR) + "/" + get_name(); |
---|
437 | |
---|
438 | if (start) |
---|
439 | { |
---|
440 | int fd = open(file.c_str(), O_CREAT|O_EXCL|O_WRONLY, 0664); |
---|
441 | if (fd < 0) |
---|
442 | throw error(file, SESSION_WRITE, strerror(errno)); |
---|
443 | |
---|
444 | // Create a stream buffer from the file descriptor. The fd will |
---|
445 | // be closed when the buffer is destroyed. |
---|
446 | __gnu_cxx::stdio_filebuf<char> fdbuf(fd, std::ios::out); |
---|
447 | std::ostream output(&fdbuf); |
---|
448 | output.imbue(std::locale::classic()); |
---|
449 | |
---|
450 | sbuild::file_lock lock(fd); |
---|
451 | try |
---|
452 | { |
---|
453 | lock.set_lock(lock::LOCK_EXCLUSIVE, 2); |
---|
454 | } |
---|
455 | catch (lock::error const& e) |
---|
456 | { |
---|
457 | throw error(file, FILE_LOCK, e); |
---|
458 | } |
---|
459 | |
---|
460 | keyfile details; |
---|
461 | get_keyfile(details); |
---|
462 | output << details; |
---|
463 | |
---|
464 | try |
---|
465 | { |
---|
466 | lock.unset_lock(); |
---|
467 | } |
---|
468 | catch (lock::error const& e) |
---|
469 | { |
---|
470 | throw error(file, FILE_UNLOCK, e); |
---|
471 | } |
---|
472 | } |
---|
473 | else /* start == false */ |
---|
474 | { |
---|
475 | if (unlink(file.c_str()) != 0) |
---|
476 | throw error(file, SESSION_UNLINK, strerror(errno)); |
---|
477 | } |
---|
478 | } |
---|
479 | |
---|
480 | void |
---|
481 | sbuild::chroot::lock (setup_type type) |
---|
482 | { |
---|
483 | setup_lock(type, true, 0); |
---|
484 | } |
---|
485 | |
---|
486 | void |
---|
487 | sbuild::chroot::unlock (setup_type type, |
---|
488 | int status) |
---|
489 | { |
---|
490 | setup_lock(type, false, status); |
---|
491 | } |
---|
492 | |
---|
493 | sbuild::chroot::session_flags |
---|
494 | sbuild::chroot::get_session_flags () const |
---|
495 | { |
---|
496 | session_flags flags = get_session_flags(*this); |
---|
497 | |
---|
498 | for (facet_list::const_iterator pos = facets.begin(); |
---|
499 | pos != facets.end(); |
---|
500 | ++pos) |
---|
501 | { |
---|
502 | flags = flags | (*pos)->get_session_flags(*this); |
---|
503 | } |
---|
504 | |
---|
505 | return flags; |
---|
506 | } |
---|
507 | |
---|
508 | void |
---|
509 | sbuild::chroot::get_details (format_detail& detail) const |
---|
510 | { |
---|
511 | get_details(*this, detail); |
---|
512 | |
---|
513 | for (facet_list::const_iterator pos = facets.begin(); |
---|
514 | pos != facets.end(); |
---|
515 | ++pos) |
---|
516 | { |
---|
517 | (*pos)->get_details(*this, detail); |
---|
518 | } |
---|
519 | } |
---|
520 | |
---|
521 | void |
---|
522 | sbuild::chroot::get_details (chroot const& chroot, |
---|
523 | format_detail& detail) const |
---|
524 | { |
---|
525 | detail.add(_("Name"), chroot.get_name()); |
---|
526 | |
---|
527 | detail |
---|
528 | .add(_("Description"), chroot.get_description()) |
---|
529 | .add(_("Type"), chroot.get_chroot_type()) |
---|
530 | .add(_("Priority"), chroot.get_priority()) |
---|
531 | .add(_("Users"), chroot.get_users()) |
---|
532 | .add(_("Groups"), chroot.get_groups()) |
---|
533 | .add(_("Root Users"), chroot.get_root_users()) |
---|
534 | .add(_("Root Groups"), chroot.get_root_groups()) |
---|
535 | .add(_("Aliases"), chroot.get_aliases()) |
---|
536 | .add(_("Environment Filter"), chroot.get_environment_filter()) |
---|
537 | .add(_("Run Setup Scripts"), chroot.get_run_setup_scripts()) |
---|
538 | .add(_("Script Configuration"), chroot.get_script_config()) |
---|
539 | .add(_("Session Managed"), |
---|
540 | static_cast<bool>(chroot.get_session_flags() & chroot::SESSION_CREATE)) |
---|
541 | .add(_("Session Cloned"), |
---|
542 | static_cast<bool>(chroot.get_session_flags() & chroot::SESSION_CLONE)) |
---|
543 | .add(_("Session Purged"), |
---|
544 | static_cast<bool>(chroot.get_session_flags() & chroot::SESSION_PURGE)); |
---|
545 | |
---|
546 | if (!chroot.get_command_prefix().empty()) |
---|
547 | detail.add(_("Command Prefix"), chroot.get_command_prefix()); |
---|
548 | |
---|
549 | /* Non user-settable properties are listed last. */ |
---|
550 | if (!chroot.get_mount_location().empty()) |
---|
551 | detail.add(_("Mount Location"), chroot.get_mount_location()); |
---|
552 | if (!chroot.get_path().empty()) |
---|
553 | detail.add(_("Path"), chroot.get_path()); |
---|
554 | } |
---|
555 | |
---|
556 | void |
---|
557 | sbuild::chroot::print_details (std::ostream& stream) const |
---|
558 | { |
---|
559 | format_detail fmt((get_active() == true ? _("Session") : _("Chroot")), |
---|
560 | stream.getloc()); |
---|
561 | |
---|
562 | get_details(fmt); |
---|
563 | |
---|
564 | stream << fmt; |
---|
565 | } |
---|
566 | |
---|
567 | void |
---|
568 | sbuild::chroot::get_keyfile (keyfile& keyfile) const |
---|
569 | { |
---|
570 | get_keyfile(*this, keyfile); |
---|
571 | |
---|
572 | for (facet_list::const_iterator pos = facets.begin(); |
---|
573 | pos != facets.end(); |
---|
574 | ++pos) |
---|
575 | { |
---|
576 | (*pos)->get_keyfile(*this, keyfile); |
---|
577 | } |
---|
578 | } |
---|
579 | |
---|
580 | void |
---|
581 | sbuild::chroot::get_keyfile (chroot const& chroot, |
---|
582 | keyfile& keyfile) const |
---|
583 | { |
---|
584 | keyfile.remove_group(chroot.get_keyfile_name()); |
---|
585 | |
---|
586 | if (get_active()) |
---|
587 | keyfile::set_object_value(chroot, &chroot::get_name, |
---|
588 | keyfile, chroot.get_keyfile_name(), |
---|
589 | "name"); |
---|
590 | |
---|
591 | keyfile::set_object_value(chroot, &chroot::get_chroot_type, |
---|
592 | keyfile, chroot.get_keyfile_name(), |
---|
593 | "type"); |
---|
594 | |
---|
595 | keyfile::set_object_value(chroot, &chroot::get_script_config, |
---|
596 | keyfile, chroot.get_keyfile_name(), |
---|
597 | "script-config"); |
---|
598 | |
---|
599 | keyfile::set_object_value(chroot, &chroot::get_priority, |
---|
600 | keyfile, chroot.get_keyfile_name(), |
---|
601 | "priority"); |
---|
602 | |
---|
603 | keyfile::set_object_list_value(chroot, &chroot::get_aliases, |
---|
604 | keyfile, chroot.get_keyfile_name(), |
---|
605 | "aliases"); |
---|
606 | |
---|
607 | keyfile::set_object_value(chroot, &chroot::get_environment_filter, |
---|
608 | keyfile, chroot.get_keyfile_name(), |
---|
609 | "environment-filter"); |
---|
610 | |
---|
611 | keyfile::set_object_value(chroot, &chroot::get_description, |
---|
612 | keyfile, chroot.get_keyfile_name(), |
---|
613 | "description"); |
---|
614 | |
---|
615 | keyfile::set_object_list_value(chroot, &chroot::get_users, |
---|
616 | keyfile, chroot.get_keyfile_name(), |
---|
617 | "users"); |
---|
618 | |
---|
619 | keyfile::set_object_list_value(chroot, &chroot::get_groups, |
---|
620 | keyfile, chroot.get_keyfile_name(), |
---|
621 | "groups"); |
---|
622 | |
---|
623 | keyfile::set_object_list_value(chroot, &chroot::get_root_users, |
---|
624 | keyfile, chroot.get_keyfile_name(), |
---|
625 | "root-users"); |
---|
626 | |
---|
627 | keyfile::set_object_list_value(chroot, &chroot::get_root_groups, |
---|
628 | keyfile, chroot.get_keyfile_name(), |
---|
629 | "root-groups"); |
---|
630 | |
---|
631 | if (get_active()) |
---|
632 | keyfile::set_object_value(chroot, &chroot::get_mount_location, |
---|
633 | keyfile, chroot.get_keyfile_name(), |
---|
634 | "mount-location"); |
---|
635 | |
---|
636 | keyfile::set_object_list_value(chroot, &chroot::get_command_prefix, |
---|
637 | keyfile, chroot.get_keyfile_name(), |
---|
638 | "command-prefix"); |
---|
639 | |
---|
640 | } |
---|
641 | |
---|
642 | void |
---|
643 | sbuild::chroot::set_keyfile (keyfile const& keyfile, |
---|
644 | string_list& used_keys) |
---|
645 | { |
---|
646 | set_keyfile(*this, keyfile, used_keys); |
---|
647 | |
---|
648 | for (facet_list::const_iterator pos = facets.begin(); |
---|
649 | pos != facets.end(); |
---|
650 | ++pos) |
---|
651 | { |
---|
652 | (*pos)->set_keyfile(*this, keyfile, used_keys); |
---|
653 | } |
---|
654 | } |
---|
655 | |
---|
656 | void |
---|
657 | sbuild::chroot::set_keyfile (chroot& chroot, |
---|
658 | keyfile const& keyfile, |
---|
659 | string_list& used_keys) |
---|
660 | { |
---|
661 | // Null method for obsolete keys. |
---|
662 | void (sbuild::chroot::* nullmethod)(bool) = 0; |
---|
663 | |
---|
664 | // Keys which are used elsewhere, but should be counted as "used". |
---|
665 | used_keys.push_back("type"); |
---|
666 | |
---|
667 | string_list keys = keyfile.get_keys(chroot.get_keyfile_name()); |
---|
668 | for (string_list::const_iterator pos = keys.begin(); |
---|
669 | pos != keys.end(); |
---|
670 | ++pos) |
---|
671 | { |
---|
672 | static regex description_keys("^description\\[.*\\]$"); |
---|
673 | if (regex_search(*pos, description_keys)) |
---|
674 | used_keys.push_back(*pos); |
---|
675 | } |
---|
676 | |
---|
677 | keyfile::get_object_value(chroot, nullmethod, |
---|
678 | keyfile, chroot.get_keyfile_name(), |
---|
679 | "active", |
---|
680 | keyfile::PRIORITY_DEPRECATED); |
---|
681 | used_keys.push_back("active"); |
---|
682 | |
---|
683 | // Setup scripts are run depending on the chroot type in use, and is |
---|
684 | // no longer user-configurable. They need to run for all types |
---|
685 | // except "plain". |
---|
686 | keyfile::get_object_value(chroot, nullmethod, |
---|
687 | keyfile, chroot.get_keyfile_name(), |
---|
688 | "run-setup-scripts", |
---|
689 | keyfile::PRIORITY_DEPRECATED); |
---|
690 | used_keys.push_back("run-setup-scripts"); |
---|
691 | |
---|
692 | // Exec scripts have been removed, so these two calls do nothing |
---|
693 | // except to warn the user that the options are no longer used. |
---|
694 | keyfile::get_object_value(chroot, nullmethod, |
---|
695 | keyfile, chroot.get_keyfile_name(), |
---|
696 | "run-session-scripts", |
---|
697 | keyfile::PRIORITY_OBSOLETE); |
---|
698 | used_keys.push_back("run-session-scripts"); |
---|
699 | keyfile::get_object_value(chroot, nullmethod, |
---|
700 | keyfile, chroot.get_keyfile_name(), |
---|
701 | "run-exec-scripts", |
---|
702 | keyfile::PRIORITY_DEPRECATED); |
---|
703 | used_keys.push_back("run-exec-scripts"); |
---|
704 | |
---|
705 | keyfile::get_object_value(chroot, &chroot::set_script_config, |
---|
706 | keyfile, chroot.get_keyfile_name(), |
---|
707 | "script-config", |
---|
708 | keyfile::PRIORITY_OPTIONAL); |
---|
709 | used_keys.push_back("script-config"); |
---|
710 | |
---|
711 | keyfile::get_object_value(chroot, &chroot::set_priority, |
---|
712 | keyfile, chroot.get_keyfile_name(), |
---|
713 | "priority", |
---|
714 | keyfile::PRIORITY_OPTIONAL); |
---|
715 | used_keys.push_back("priority"); |
---|
716 | |
---|
717 | keyfile::get_object_list_value(chroot, &chroot::set_aliases, |
---|
718 | keyfile, chroot.get_keyfile_name(), |
---|
719 | "aliases", |
---|
720 | keyfile::PRIORITY_OPTIONAL); |
---|
721 | used_keys.push_back("aliases"); |
---|
722 | |
---|
723 | keyfile::get_object_value(chroot, &chroot::set_environment_filter, |
---|
724 | keyfile, chroot.get_keyfile_name(), |
---|
725 | "environment-filter", |
---|
726 | keyfile::PRIORITY_OPTIONAL); |
---|
727 | used_keys.push_back("environment-filter"); |
---|
728 | |
---|
729 | keyfile::get_object_value(chroot, &chroot::set_description, |
---|
730 | keyfile, chroot.get_keyfile_name(), |
---|
731 | "description", |
---|
732 | keyfile::PRIORITY_OPTIONAL); |
---|
733 | used_keys.push_back("description"); |
---|
734 | |
---|
735 | keyfile::get_object_list_value(chroot, &chroot::set_users, |
---|
736 | keyfile, chroot.get_keyfile_name(), |
---|
737 | "users", |
---|
738 | keyfile::PRIORITY_OPTIONAL); |
---|
739 | used_keys.push_back("users"); |
---|
740 | |
---|
741 | keyfile::get_object_list_value(chroot, &chroot::set_groups, |
---|
742 | keyfile, chroot.get_keyfile_name(), |
---|
743 | "groups", |
---|
744 | keyfile::PRIORITY_OPTIONAL); |
---|
745 | used_keys.push_back("groups"); |
---|
746 | |
---|
747 | keyfile::get_object_list_value(chroot, &chroot::set_root_users, |
---|
748 | keyfile, chroot.get_keyfile_name(), |
---|
749 | "root-users", |
---|
750 | keyfile::PRIORITY_OPTIONAL); |
---|
751 | used_keys.push_back("root-users"); |
---|
752 | |
---|
753 | keyfile::get_object_list_value(chroot, &chroot::set_root_groups, |
---|
754 | keyfile, chroot.get_keyfile_name(), |
---|
755 | "root-groups", |
---|
756 | keyfile::PRIORITY_OPTIONAL); |
---|
757 | used_keys.push_back("root-groups"); |
---|
758 | |
---|
759 | keyfile::get_object_value(chroot, &chroot::set_mount_location, |
---|
760 | keyfile, chroot.get_keyfile_name(), |
---|
761 | "mount-location", |
---|
762 | get_active() ? |
---|
763 | keyfile::PRIORITY_REQUIRED : |
---|
764 | keyfile::PRIORITY_DISALLOWED); |
---|
765 | used_keys.push_back("mount-location"); |
---|
766 | |
---|
767 | keyfile::get_object_value(chroot, &chroot::set_name, |
---|
768 | keyfile, chroot.get_keyfile_name(), |
---|
769 | "name", |
---|
770 | get_active() ? |
---|
771 | keyfile::PRIORITY_OPTIONAL : |
---|
772 | keyfile::PRIORITY_DISALLOWED); |
---|
773 | used_keys.push_back("name"); |
---|
774 | |
---|
775 | keyfile::get_object_list_value(chroot, &chroot::set_command_prefix, |
---|
776 | keyfile, chroot.get_keyfile_name(), |
---|
777 | "command-prefix", |
---|
778 | keyfile::PRIORITY_OPTIONAL); |
---|
779 | used_keys.push_back("command-prefix"); |
---|
780 | } |
---|