1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/* libguestfs
* Copyright (C) 2012 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Handle temporary directories.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libintl.h>
#include <unistd.h>
#include "ignore-value.h"
#include "guestfs.h"
#include "guestfs-internal.h"
#include "guestfs-internal-actions.h"
/**
* We need to make all tmpdir paths absolute because lots of places in
* the code assume this. Do it at the time we set the path or read
* the environment variable (L<https://bugzilla.redhat.com/882417>).
*
* The C<ctxstr> parameter is a string displayed in error messages
* giving the context of the operation (eg. name of environment
* variable being used, or API function being called).
*/
static int
set_abs_path (guestfs_h *g, const char *ctxstr,
const char *tmpdir, char **tmpdir_ret)
{
char *ret;
struct stat statbuf;
/* Free the old path, and set it to NULL so that if we fail below
* we don't end up with a pointer to freed memory.
*/
free (*tmpdir_ret);
*tmpdir_ret = NULL;
if (tmpdir == NULL)
return 0;
ret = realpath (tmpdir, NULL);
if (ret == NULL) {
perrorf (g, _("converting path to absolute path: %s: %s: realpath"),
ctxstr, tmpdir);
return -1;
}
if (stat (ret, &statbuf) == -1) {
perrorf (g, "%s: %s: %s: stat",
_("setting temporary directory"), ctxstr, tmpdir);
return -1;
}
if (!S_ISDIR (statbuf.st_mode)) {
error (g, _("%s: %s: ‘%s’ is not a directory"),
_("setting temporary directory"), ctxstr, tmpdir);
return -1;
}
*tmpdir_ret = ret;
return 0;
}
int
guestfs_int_set_env_tmpdir (guestfs_h *g, const char *envname,
const char *tmpdir)
{
return set_abs_path (g, envname, tmpdir, &g->env_tmpdir);
}
int
guestfs_int_set_env_runtimedir (guestfs_h *g, const char *envname,
const char *runtimedir)
{
return set_abs_path (g, envname, runtimedir, &g->env_runtimedir);
}
int
guestfs_impl_set_tmpdir (guestfs_h *g, const char *tmpdir)
{
return set_abs_path (g, "set_tmpdir", tmpdir, &g->int_tmpdir);
}
/**
* Implements the C<guestfs_get_tmpdir> API.
*
* Note this actually calculates the tmpdir, so it never returns
* C<NULL>.
*/
char *
guestfs_impl_get_tmpdir (guestfs_h *g)
{
const char *str;
if (g->int_tmpdir)
str = g->int_tmpdir;
else if (g->env_tmpdir)
str = g->env_tmpdir;
else
str = "/tmp";
return safe_strdup (g, str);
}
int
guestfs_impl_set_cachedir (guestfs_h *g, const char *cachedir)
{
return set_abs_path (g, "set_cachedir", cachedir, &g->int_cachedir);
}
/**
* Implements the C<guestfs_get_cachedir> API.
*
* Note this actually calculates the cachedir, so it never returns C<NULL>.
*/
char *
guestfs_impl_get_cachedir (guestfs_h *g)
{
const char *str;
if (g->int_cachedir)
str = g->int_cachedir;
else if (g->env_tmpdir)
str = g->env_tmpdir;
else
str = "/var/tmp";
return safe_strdup (g, str);
}
/**
* Implements the C<guestfs_get_sockdir> API.
*
* Note this actually calculates the sockdir, so it never returns
* C<NULL>.
*/
char *
guestfs_impl_get_sockdir (guestfs_h *g)
{
const char *str;
uid_t euid = geteuid ();
if (euid == 0) {
/* Use /tmp exclusively for root, as otherwise qemu (running as
* qemu.qemu when launched by libvirt) will not be able to access
* the directory.
*/
str = "/tmp";
} else {
if (g->env_runtimedir)
str = g->env_runtimedir;
else
str = "/tmp";
}
return safe_strdup (g, str);
}
static int
lazy_make_tmpdir (guestfs_h *g,
char *(*getdir) (guestfs_h *g), int is_runtime_dir,
char **dest)
{
if (!*dest) {
CLEANUP_FREE char *tmpdir = getdir (g);
char *tmppath = safe_asprintf (g, "%s/libguestfsXXXXXX", tmpdir);
if (mkdtemp (tmppath) == NULL) {
int bad_runtime_dir = is_runtime_dir && errno == EACCES &&
STRPREFIX (tmpdir, "/run/user/");
if (!bad_runtime_dir)
perrorf (g, _("%s: cannot create temporary directory"), tmppath);
else
error (g, _("%s: cannot create temporary directory. It may be that $XDG_RUNTIME_DIR is pointing to a directory which we cannot write to, for example if you used ‘su [user]’ to change to this user account (see https://bugzilla.redhat.com/967509). You can correct this by adjusting XDG_RUNTIME_DIR and possibly creating /run/user/%d with the right ownership."),
tmppath, (int) geteuid ());
free (tmppath);
return -1;
}
/* Allow qemu (which may be running as qemu.qemu) to read in this
* temporary directory; we are storing either sockets, or temporary
* disks which qemu needs to access to. (RHBZ#610880).
* We do this only for root, as for normal users qemu will be run
* under the same user.
*/
if (geteuid () == 0 && chmod (tmppath, 0755) == -1) {
perrorf (g, "chmod: %s", tmppath);
free (tmppath);
return -1;
}
*dest = tmppath;
}
return 0;
}
/**
* The C<g-E<gt>tmpdir> (per-handle temporary directory) is not
* created when the handle is created. Instead we create it lazily
* before the first time it is used, or during launch.
*/
int
guestfs_int_lazy_make_tmpdir (guestfs_h *g)
{
return lazy_make_tmpdir (g, guestfs_get_tmpdir, 0, &g->tmpdir);
}
int
guestfs_int_lazy_make_sockdir (guestfs_h *g)
{
return lazy_make_tmpdir (g, guestfs_get_sockdir, 1, &g->sockdir);
}
/**
* Generate unique temporary paths for temporary files.
*
* Returns a unique path or NULL on error.
*/
char *
guestfs_int_make_temp_path (guestfs_h *g,
const char *name, const char *extension)
{
int ret = 0;
ret = guestfs_int_lazy_make_tmpdir (g);
if (ret < 0)
return NULL;
return safe_asprintf (g, "%s/%s%d%s%s",
g->tmpdir, name, ++g->unique,
extension ? "." : "",
extension ? extension : "");
}
/**
* Create the supermin appliance directory under cachedir, if it does
* not exist.
*
* Sanity-check that the permissions on the cachedir are safe, in case
* it has been pre-created maliciously or tampered with.
*
* Returns the directory name which the caller must free.
*/
char *
guestfs_int_lazy_make_supermin_appliance_dir (guestfs_h *g)
{
CLEANUP_FREE char *tmpdir = guestfs_get_cachedir (g);
char *ret = NULL;
struct stat statbuf;
uid_t uid = geteuid ();
ret = safe_asprintf (g, "%s/.guestfs-%ju", tmpdir, (uintmax_t) uid);
ignore_value (mkdir (ret, 0755));
ignore_value (chmod (ret, 0755)); /* RHBZ#921292 */
/* See if the cache directory exists and passes some simple checks
* to make sure it has not been tampered with.
*/
if (lstat (ret, &statbuf) == -1) {
perrorf (g, _("stat: %s"), ret);
free (ret);
return NULL;
}
if (statbuf.st_uid != uid) {
error (g, _("security: cached appliance %s is not owned by UID %ju"),
ret, (uintmax_t) uid);
free (ret);
return NULL;
}
if (!S_ISDIR (statbuf.st_mode)) {
error (g, _("security: cached appliance %s is not a directory (mode %o)"),
ret, statbuf.st_mode);
free (ret);
return NULL;
}
if ((statbuf.st_mode & 0022) != 0) {
error (g, _("security: cached appliance %s is writable by group or other (mode %o)"),
ret, statbuf.st_mode);
free (ret);
return NULL;
}
/* "Touch" the directory. */
ignore_value (utimes (ret, NULL));
return ret;
}
/**
* Recursively remove a temporary directory. If removal fails, just
* return (it's a temporary directory so it'll eventually be cleaned
* up by a temp cleaner).
*
* This is implemented using C<rm -rf> because that's simpler and
* safer.
*/
void
guestfs_int_recursive_remove_dir (guestfs_h *g, const char *dir)
{
CLEANUP_CMD_CLOSE struct command *cmd = guestfs_int_new_command (g);
guestfs_int_cmd_add_arg (cmd, "rm");
guestfs_int_cmd_add_arg (cmd, "-rf");
guestfs_int_cmd_add_arg (cmd, dir);
ignore_value (guestfs_int_cmd_run (cmd));
}
void
guestfs_int_remove_tmpdir (guestfs_h *g)
{
if (g->tmpdir)
guestfs_int_recursive_remove_dir (g, g->tmpdir);
}
void
guestfs_int_remove_sockdir (guestfs_h *g)
{
if (g->sockdir)
guestfs_int_recursive_remove_dir (g, g->sockdir);
}
|