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
|
/* libguestfs - the guestfsd daemon
* Copyright (C) 2009-2012 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <dirent.h>
#include "cloexec.h"
#include "daemon.h"
#include "optgroups.h"
#include "actions.h"
/* On Windows, NAME_MAX is not defined. */
#ifndef NAME_MAX
#define NAME_MAX FILENAME_MAX
#endif
#ifdef HAVE_REALPATH
int
optgroup_realpath_available (void)
{
return 1;
}
char *
do_realpath (const char *path)
{
char *ret;
CHROOT_IN;
ret = realpath (path, NULL);
CHROOT_OUT;
if (ret == NULL) {
reply_with_perror ("%s", path);
return NULL;
}
return ret; /* caller frees */
}
#else /* !HAVE_REALPATH */
int
optgroup_realpath_available (void)
{
return 0;
}
char *
do_realpath (const char *path)
{
abort ();
}
#endif /* !HAVE_REALPATH */
static int find_path_element (int fd_cwd, char *name, size_t *name_len_ret);
char *
do_case_sensitive_path (const char *path)
{
char ret[PATH_MAX+1] = "/";
size_t next = 1;
int fd_cwd;
/* 'fd_cwd' here is a surrogate for the current working directory, so
* that we don't have to actually call chdir(2).
*/
fd_cwd = open (sysroot, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
if (fd_cwd == -1) {
reply_with_perror ("%s", sysroot);
return NULL;
}
/* First character is a '/'. Take each subsequent path element
* and follow it.
*/
while (*path) {
size_t i = strcspn (path, "/");
if (i == 0) {
path++;
continue;
}
if ((i == 1 && path[0] == '.') ||
(i == 2 && path[0] == '.' && path[1] == '.')) {
reply_with_error ("path contained . or .. elements");
goto error;
}
if (i > NAME_MAX) {
reply_with_error ("path element too long");
goto error;
}
char name[NAME_MAX+1];
memcpy (name, path, i);
name[i] = '\0';
/* Skip to next element in path (for the next loop iteration). */
path += i;
/* Read the current directory looking (case insensitively) for
* this element of the path. This replaces 'name' with the
* correct case version.
*/
if (find_path_element (fd_cwd, name, &i) == -1)
goto error;
/* Add the real name of this path element to the return value. */
if (next > 1)
ret[next++] = '/';
if (next + i >= PATH_MAX) {
reply_with_error ("final path too long");
goto error;
}
strcpy (&ret[next], name);
next += i;
/* Is it a directory? Try going into it. */
int fd2 = openat (fd_cwd, name, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
int err = errno;
close (fd_cwd);
fd_cwd = fd2;
errno = err;
if (fd_cwd == -1) {
/* ENOTDIR is OK provided we've reached the end of the path. */
if (errno != ENOTDIR) {
reply_with_perror ("openat: %s", name);
goto error;
}
if (*path) {
reply_with_error ("%s: non-directory element in path", name);
goto error;
}
}
}
if (fd_cwd >= 0)
close (fd_cwd);
ret[next] = '\0';
char *retp = strdup (ret);
if (retp == NULL) {
reply_with_perror ("strdup");
return NULL;
}
return retp; /* caller frees */
error:
if (fd_cwd >= 0)
close (fd_cwd);
return NULL;
}
/* 'fd_cwd' is a file descriptor pointing to an open directory.
* 'name' is a buffer of NAME_MAX+1 characters in size which initially
* contains the path element to search for.
*
* We search the directory looking for a path element that case
* insensitively matches 'name' and update the 'name' buffer.
*
* If this is successful, return 0. If it fails, reply with an error
* and return -1.
*/
static int
find_path_element (int fd_cwd, char *name, size_t *name_len_ret)
{
int fd2;
DIR *dir;
struct dirent *d;
fd2 = dup_cloexec (fd_cwd); /* because closedir will close it */
if (fd2 == -1) {
reply_with_perror ("dup");
return -1;
}
dir = fdopendir (fd2);
if (dir == NULL) {
reply_with_perror ("opendir");
close (fd2);
return -1;
}
for (;;) {
errno = 0;
d = readdir (dir);
if (d == NULL)
break;
if (STRCASEEQ (d->d_name, name))
break;
}
if (d == NULL && errno != 0) {
reply_with_perror ("readdir");
closedir (dir);
return -1;
}
if (d == NULL) {
reply_with_error ("%s: no file or directory found with this name", name);
closedir (dir);
return -1;
}
*name_len_ret = strlen (d->d_name);
if (*name_len_ret > NAME_MAX) {
/* Should never happen? */
reply_with_error ("path element longer than NAME_MAX");
closedir (dir);
return -1;
}
/* Safe because name is a buffer of NAME_MAX+1 characters. */
strcpy (name, d->d_name);
/* NB: closedir frees the structure associated with 'd', so we must
* do this last.
*/
if (closedir (dir) == -1) {
reply_with_perror ("closedir");
return -1;
}
return 0;
}
|