2 * Copyright (C) 2010 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/select.h>
35 #ifdef HAVE_SYS_TYPES_H
36 #include <sys/types.h>
40 #include "guestfs-internal.h"
41 #include "guestfs-internal-actions.h"
42 #include "guestfs_protocol.h"
44 static const char *kernel_name = "vmlinuz." host_cpu;
45 static const char *initrd_name = "initramfs." host_cpu ".img";
47 static int find_path (guestfs_h *g, int (*pred) (guestfs_h *g, const char *pelem, void *data), void *data, char **pelem);
48 static int dir_contains_file (const char *dir, const char *file);
49 static int dir_contains_files (const char *dir, ...);
50 static int contains_supermin_appliance (guestfs_h *g, const char *path, void *data);
51 static int contains_ordinary_appliance (guestfs_h *g, const char *path, void *data);
52 static char *calculate_supermin_checksum (guestfs_h *g, const char *supermin_path);
53 static int check_for_cached_appliance (guestfs_h *g, const char *supermin_path, const char *checksum, char **kernel, char **initrd, char **appliance);
54 static int build_supermin_appliance (guestfs_h *g, const char *supermin_path, const char *checksum, char **kernel, char **initrd, char **appliance);
55 static int run_supermin_helper (guestfs_h *g, const char *supermin_path, const char *cachedir, size_t cdlen);
57 /* Locate or build the appliance.
59 * This function locates or builds the appliance as necessary,
60 * handling the supermin appliance, caching of supermin-built
61 * appliances, or using an ordinary appliance.
63 * The return value is 0 = good, -1 = error. Returned in '*kernel'
64 * will be the name of the kernel to use, '*initrd' the name of the
65 * initrd, '*appliance' the name of the ext2 root filesystem.
66 * '*appliance' can be NULL, meaning that we are using an ordinary
67 * (non-ext2) appliance. All three strings must be freed by the
68 * caller. However the referenced files themselves must not be
71 * The process is as follows:
73 * (1) Look for the first element of g->path which contains a
74 * supermin appliance skeleton. If no element has this, skip
75 * straight to step (5).
76 * (2) Calculate the checksum of this supermin appliance.
77 * (3) Check whether $TMPDIR/$checksum/ directory exists, contains
78 * a cached appliance, and passes basic security checks. If so,
79 * return this appliance.
80 * (4) Try to build the supermin appliance into $TMPDIR/$checksum/.
81 * If this is successful, return it.
82 * (5) Check each element of g->path, looking for an ordinary appliance.
83 * If one is found, return it.
86 guestfs___build_appliance (guestfs_h *g,
87 char **kernel, char **initrd, char **appliance)
93 r = find_path (g, contains_supermin_appliance, NULL, &supermin_path);
98 /* Step (2): calculate checksum. */
99 char *checksum = calculate_supermin_checksum (g, supermin_path);
101 /* Step (3): cached appliance exists? */
102 r = check_for_cached_appliance (g, supermin_path, checksum,
103 kernel, initrd, appliance);
105 free (supermin_path);
107 return r == 1 ? 0 : -1;
110 /* Step (4): build supermin appliance. */
111 r = build_supermin_appliance (g, supermin_path, checksum,
112 kernel, initrd, appliance);
113 free (supermin_path);
117 free (supermin_path);
122 r = find_path (g, contains_ordinary_appliance, NULL, &path);
127 size_t len = strlen (path);
128 *kernel = safe_malloc (g, len + strlen (kernel_name) + 2);
129 *initrd = safe_malloc (g, len + strlen (initrd_name) + 2);
130 sprintf (*kernel, "%s/%s", path, kernel_name);
131 sprintf (*initrd, "%s/%s", path, initrd_name);
138 error (g, _("cannot find any suitable libguestfs supermin or ordinary appliance on LIBGUESTFS_PATH (search path: %s)"),
144 contains_supermin_appliance (guestfs_h *g, const char *path, void *data)
146 return dir_contains_files (path, "supermin.d", "kmod.whitelist", NULL);
150 contains_ordinary_appliance (guestfs_h *g, const char *path, void *data)
152 return dir_contains_files (path, kernel_name, initrd_name, NULL);
155 /* supermin_path is a path which is known to contain a supermin
156 * appliance. Using febootstrap-supermin-helper -f checksum calculate
157 * the checksum so we can see if it is cached.
160 calculate_supermin_checksum (guestfs_h *g, const char *supermin_path)
162 size_t len = 2 * strlen (supermin_path) + 256;
164 int pass_u_g_args = getuid () != geteuid () || getgid () != getegid ();
168 "febootstrap-supermin-helper%s "
172 g->verbose ? " --verbose" : "",
176 "febootstrap-supermin-helper%s "
182 g->verbose ? " --verbose" : "",
183 geteuid (), getegid (),
187 guestfs___print_timestamped_message (g, "%s", cmd);
189 /* Errors here are non-fatal, so we don't need to call error(). */
190 FILE *pp = popen (cmd, "r");
195 if (fgets (checksum, sizeof checksum, pp) == NULL) {
200 if (pclose (pp) == -1) {
205 len = strlen (checksum);
207 if (len < 16) { /* sanity check */
208 fprintf (stderr, "libguestfs: internal error: febootstrap-supermin-helper -f checksum returned a short string\n");
212 if (len > 0 && checksum[len-1] == '\n')
213 checksum[--len] = '\0';
215 return safe_strndup (g, checksum, len);
218 /* Check for cached appliance in $TMPDIR/$checksum. Check it exists
219 * and passes some basic security checks.
222 * 1 = exists, and passes
224 * -1 = error which should abort the whole launch process
227 security_check_cache_file (guestfs_h *g, const char *filename,
228 const struct stat *statbuf)
230 uid_t uid = geteuid ();
232 if (statbuf->st_uid != uid) {
233 error (g, ("libguestfs cached appliance %s is not owned by UID %d\n"),
238 if ((statbuf->st_mode & 0022) != 0) {
239 error (g, ("libguestfs cached appliance %s is writable by group or other (mode %o)\n"),
240 filename, statbuf->st_mode);
248 check_for_cached_appliance (guestfs_h *g,
249 const char *supermin_path, const char *checksum,
250 char **kernel, char **initrd, char **appliance)
252 const char *tmpdir = guestfs_tmpdir ();
254 size_t len = strlen (tmpdir) + strlen (checksum) + 10;
256 snprintf (cachedir, len, "%s/guestfs.%s", tmpdir, checksum);
258 /* Touch the directory to prevent it being deleting in a rare race
259 * between us doing the checks and a tmp cleaner running. Note this
260 * doesn't create the directory, and we ignore any error.
262 (void) utime (cachedir, NULL);
264 /* See if the cache directory exists and passes some simple checks
265 * to make sure it has not been tampered with. Note that geteuid()
266 * forms a part of the checksum.
269 if (lstat (cachedir, &statbuf) == -1)
272 if (security_check_cache_file (g, cachedir, &statbuf) == -1)
277 *kernel = safe_malloc (g, len + 8 /* / + "kernel" + \0 */);
278 *initrd = safe_malloc (g, len + 8 /* / + "initrd" + \0 */);
279 *appliance = safe_malloc (g, len + 6 /* / + "root" + \0 */);
280 sprintf (*kernel, "%s/kernel", cachedir);
281 sprintf (*initrd, "%s/initrd", cachedir);
282 sprintf (*appliance, "%s/root", cachedir);
284 /* Touch the files to prevent them being deleted, and to bring the
285 * cache up to date. Note this doesn't create the files.
287 (void) utime (*kernel, NULL);
289 /* NB. *kernel is a symlink, so we want to check the kernel, not the
290 * link (stat, not lstat). We don't do a security check on the
291 * kernel since it's always under /boot.
293 if (stat (*kernel, &statbuf) == -1) {
298 (void) utime (*initrd, NULL);
300 if (lstat (*initrd, &statbuf) == -1) {
305 if (security_check_cache_file (g, *initrd, &statbuf) == -1) {
310 (void) utime (*appliance, NULL);
312 if (lstat (*appliance, &statbuf) == -1) {
317 if (security_check_cache_file (g, *appliance, &statbuf) == -1) {
332 /* Build supermin appliance from supermin_path to $TMPDIR/$checksum.
336 * -1 = error (aborts launch)
339 build_supermin_appliance (guestfs_h *g,
340 const char *supermin_path, const char *checksum,
341 char **kernel, char **initrd, char **appliance)
344 guestfs___print_timestamped_message (g, "begin building supermin appliance");
346 const char *tmpdir = guestfs_tmpdir ();
348 size_t tmpcdlen = strlen (tmpdir) + 16;
349 char tmpcd[tmpcdlen];
350 snprintf (tmpcd, tmpcdlen, "%s/guestfs.XXXXXX", tmpdir);
352 if (NULL == mkdtemp (tmpcd)) {
353 error (g, _("failed to create temporary cache directory: %m"));
358 guestfs___print_timestamped_message (g, "run febootstrap-supermin-helper");
360 int r = run_supermin_helper (g, supermin_path, tmpcd, tmpcdlen);
365 guestfs___print_timestamped_message (g, "finished building supermin appliance");
367 size_t cdlen = strlen (tmpdir) + strlen (checksum) + 10;
368 char cachedir[cdlen];
369 snprintf (cachedir, cdlen, "%s/guestfs.%s", tmpdir, checksum);
371 /* Make the temporary directory world readable */
372 if (chmod (tmpcd, 0755) == -1) {
373 error (g, "chmod %s: %m", tmpcd);
376 /* Try to rename the temporary directory to its non-temporary name */
377 if (rename (tmpcd, cachedir) == -1) {
378 /* If the cache directory now exists, we may have been racing with another
379 * libguestfs process. Check the new directory and use it if it's valid. */
380 if (errno == ENOTEMPTY || errno == EEXIST) {
381 /* Appliance cache consists of 2 files and a symlink in the cache
382 * directory. Delete them first. */
383 DIR *dir = opendir (tmpcd);
385 error (g, "opendir %s: %m", tmpcd);
389 int fd = dirfd (dir);
391 error (g, "dirfd: %m");
396 struct dirent *dirent;
399 dirent = readdir (dir);
401 if (dirent == NULL) {
405 /* Check that dirent is a file so we don't try to delete . and .. */
407 if (fstatat (fd, dirent->d_name, &st, AT_SYMLINK_NOFOLLOW) == -1) {
408 error (g, "fstatat %s: %m", dirent->d_name);
412 if (!S_ISDIR(st.st_mode)) {
413 if (unlinkat (fd, dirent->d_name, 0) == -1) {
414 error (g, "unlinkat %s: %m", dirent->d_name);
422 error (g, "readdir %s: %m", tmpcd);
429 /* Delete the temporary cache directory itself. */
430 if (rmdir (tmpcd) == -1) {
431 error (g, "rmdir %s: %m", tmpcd);
435 /* Check the new cache directory, and return it if valid */
436 return check_for_cached_appliance (g, supermin_path, checksum,
437 kernel, initrd, appliance);
441 error (g, _("error renaming temporary cache directory: %m"));
446 *kernel = safe_malloc (g, cdlen + 8 /* / + "kernel" + \0 */);
447 *initrd = safe_malloc (g, cdlen + 8 /* / + "initrd" + \0 */);
448 *appliance = safe_malloc (g, cdlen + 6 /* / + "root" + \0 */);
449 sprintf (*kernel, "%s/kernel", cachedir);
450 sprintf (*initrd, "%s/initrd", cachedir);
451 sprintf (*appliance, "%s/root", cachedir);
456 /* Run febootstrap-supermin-helper and tell it to generate the
460 run_supermin_helper (guestfs_h *g, const char *supermin_path,
461 const char *cachedir, size_t cdlen)
463 size_t pathlen = strlen (supermin_path);
465 const char *argv[30];
469 snprintf (uid, sizeof uid, "%i", geteuid ());
471 snprintf (gid, sizeof gid, "%i", getegid ());
472 char supermin_d[pathlen + 32];
473 snprintf (supermin_d, pathlen + 32, "%s/supermin.d", supermin_path);
474 char kernel[cdlen + 32];
475 snprintf (kernel, cdlen + 32, "%s/kernel", cachedir);
476 char initrd[cdlen + 32];
477 snprintf (initrd, cdlen + 32, "%s/initrd", cachedir);
478 char root[cdlen + 32];
479 snprintf (root, cdlen + 32, "%s/root", cachedir);
481 int pass_u_g_args = getuid () != geteuid () || getgid () != getegid ();
483 argv[i++] = "febootstrap-supermin-helper";
485 argv[i++] = "--verbose";
494 argv[i++] = supermin_d;
495 argv[i++] = host_cpu;
507 if (pid > 0) { /* Parent. */
509 guestfs___print_timestamped_argv (g, argv);
512 if (waitpid (pid, &status, 0) == -1) {
513 perrorf (g, "waitpid");
516 if (!WIFEXITED (status) || WEXITSTATUS (status) != 0) {
517 error (g, _("external command failed, see earlier error messages"));
525 /* Set a sensible umask in the subprocess, so kernel and initrd
526 * output files are world-readable (RHBZ#610880).
530 execvp ("febootstrap-supermin-helper", (char * const *) argv);
532 _exit (EXIT_FAILURE);
535 /* Search elements of g->path, returning the first path element which
536 * matches the predicate function 'pred'.
538 * Function 'pred' must return a true or false value. If it returns
539 * -1 then the entire search is aborted.
542 * 1 = a path element matched, it is returned in *pelem_ret and must be
543 * freed by the caller,
544 * 0 = no path element matched, *pelem_ret is set to NULL, or
545 * -1 = error which aborts the launch process
548 find_path (guestfs_h *g,
549 int (*pred) (guestfs_h *g, const char *pelem, void *data),
555 const char *pelem = g->path;
557 /* Note that if g->path is an empty string, we want to check the
558 * current directory (for backwards compatibility with
559 * libguestfs < 1.5.4).
562 len = strcspn (pelem, ":");
564 /* Empty element or "." means current directory. */
566 *pelem_ret = safe_strdup (g, ".");
568 *pelem_ret = safe_strndup (g, pelem, len);
570 r = pred (g, *pelem_ret, data);
576 if (r != 0) /* predicate matched */
581 if (pelem[len] == ':')
587 /* Predicate didn't match on any path element. */
592 /* Returns true iff file is contained in dir. */
594 dir_contains_file (const char *dir, const char *file)
596 size_t dirlen = strlen (dir);
597 size_t filelen = strlen (file);
598 size_t len = dirlen + filelen + 2;
601 snprintf (path, len, "%s/%s", dir, file);
602 return access (path, F_OK) == 0;
605 /* Returns true iff every listed file is contained in 'dir'. */
607 dir_contains_files (const char *dir, ...)
612 va_start (args, dir);
613 while ((file = va_arg (args, const char *)) != NULL) {
614 if (!dir_contains_file (dir, file)) {