| 1 | /*
|
|---|
| 2 | * Store streams in a separate subdirectory
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (C) Volker Lendecke, 2007
|
|---|
| 5 | *
|
|---|
| 6 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 7 | * it under the terms of the GNU General Public License as published by
|
|---|
| 8 | * the Free Software Foundation; either version 3 of the License, or
|
|---|
| 9 | * (at your option) any later version.
|
|---|
| 10 | *
|
|---|
| 11 | * This program is distributed in the hope that it will be useful,
|
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | * GNU General Public License for more details.
|
|---|
| 15 | *
|
|---|
| 16 | * You should have received a copy of the GNU General Public License
|
|---|
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | #include "includes.h"
|
|---|
| 21 |
|
|---|
| 22 | #undef DBGC_CLASS
|
|---|
| 23 | #define DBGC_CLASS DBGC_VFS
|
|---|
| 24 |
|
|---|
| 25 | /*
|
|---|
| 26 | * Excerpt from a mail from tridge:
|
|---|
| 27 | *
|
|---|
| 28 | * Volker, what I'm thinking of is this:
|
|---|
| 29 | * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream1
|
|---|
| 30 | * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream2
|
|---|
| 31 | *
|
|---|
| 32 | * where XX/YY is a 2 level hash based on the fsid/inode. "aaaa.bbbb"
|
|---|
| 33 | * is the fsid/inode. "namedstreamX" is a file named after the stream
|
|---|
| 34 | * name.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | static uint32_t hash_fn(DATA_BLOB key)
|
|---|
| 38 | {
|
|---|
| 39 | uint32_t value; /* Used to compute the hash value. */
|
|---|
| 40 | uint32_t i; /* Used to cycle through random values. */
|
|---|
| 41 |
|
|---|
| 42 | /* Set the initial value from the key size. */
|
|---|
| 43 | for (value = 0x238F13AF * key.length, i=0; i < key.length; i++)
|
|---|
| 44 | value = (value + (key.data[i] << (i*5 % 24)));
|
|---|
| 45 |
|
|---|
| 46 | return (1103515243 * value + 12345);
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | /*
|
|---|
| 50 | * With the hashing scheme based on the inode we need to protect against
|
|---|
| 51 | * streams showing up on files with re-used inodes. This can happen if we
|
|---|
| 52 | * create a stream directory from within Samba, and a local process or NFS
|
|---|
| 53 | * client deletes the file without deleting the streams directory. When the
|
|---|
| 54 | * inode is re-used and the stream directory is still around, the streams in
|
|---|
| 55 | * there would be show up as belonging to the new file.
|
|---|
| 56 | *
|
|---|
| 57 | * There are several workarounds for this, probably the easiest one is on
|
|---|
| 58 | * systems which have a true birthtime stat element: When the file has a later
|
|---|
| 59 | * birthtime than the streams directory, then we have to recreate the
|
|---|
| 60 | * directory.
|
|---|
| 61 | *
|
|---|
| 62 | * The other workaround is to somehow mark the file as generated by Samba with
|
|---|
| 63 | * something that a NFS client would not do. The closest one is a special
|
|---|
| 64 | * xattr value being set. On systems which do not support xattrs, it might be
|
|---|
| 65 | * an option to put in a special ACL entry for a non-existing group.
|
|---|
| 66 | */
|
|---|
| 67 |
|
|---|
| 68 | #define SAMBA_XATTR_MARKER "user.SAMBA_STREAMS"
|
|---|
| 69 |
|
|---|
| 70 | static bool file_is_valid(vfs_handle_struct *handle, const char *path,
|
|---|
| 71 | bool check_valid)
|
|---|
| 72 | {
|
|---|
| 73 | char buf;
|
|---|
| 74 |
|
|---|
| 75 | if (!check_valid) {
|
|---|
| 76 | return true;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | DEBUG(10, ("file_is_valid (%s) called\n", path));
|
|---|
| 80 |
|
|---|
| 81 | if (SMB_VFS_NEXT_GETXATTR(handle, path, SAMBA_XATTR_MARKER,
|
|---|
| 82 | &buf, sizeof(buf)) != sizeof(buf)) {
|
|---|
| 83 | DEBUG(10, ("GETXATTR failed: %s\n", strerror(errno)));
|
|---|
| 84 | return false;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | if (buf != '1') {
|
|---|
| 88 | DEBUG(10, ("got wrong buffer content: '%c'\n", buf));
|
|---|
| 89 | return false;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | return true;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | static bool mark_file_valid(vfs_handle_struct *handle, const char *path,
|
|---|
| 96 | bool check_valid)
|
|---|
| 97 | {
|
|---|
| 98 | char buf = '1';
|
|---|
| 99 | int ret;
|
|---|
| 100 |
|
|---|
| 101 | if (!check_valid) {
|
|---|
| 102 | return true;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | DEBUG(10, ("marking file %s as valid\n", path));
|
|---|
| 106 |
|
|---|
| 107 | ret = SMB_VFS_NEXT_SETXATTR(handle, path, SAMBA_XATTR_MARKER,
|
|---|
| 108 | &buf, sizeof(buf), 0);
|
|---|
| 109 |
|
|---|
| 110 | if (ret == -1) {
|
|---|
| 111 | DEBUG(10, ("SETXATTR failed: %s\n", strerror(errno)));
|
|---|
| 112 | return false;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | return true;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | static char *stream_dir(vfs_handle_struct *handle, const char *base_path,
|
|---|
| 119 | const SMB_STRUCT_STAT *base_sbuf, bool create_it)
|
|---|
| 120 | {
|
|---|
| 121 | uint32_t hash;
|
|---|
| 122 | char *result = NULL;
|
|---|
| 123 | SMB_STRUCT_STAT sbuf;
|
|---|
| 124 | uint8_t first, second;
|
|---|
| 125 | char *tmp;
|
|---|
| 126 | char *id_hex;
|
|---|
| 127 | struct file_id id;
|
|---|
| 128 | uint8 id_buf[16];
|
|---|
| 129 | bool check_valid;
|
|---|
| 130 | const char *rootdir;
|
|---|
| 131 |
|
|---|
| 132 | check_valid = lp_parm_bool(SNUM(handle->conn),
|
|---|
| 133 | "streams_depot", "check_valid", true);
|
|---|
| 134 |
|
|---|
| 135 | tmp = talloc_asprintf(talloc_tos(), "%s/.streams", handle->conn->connectpath);
|
|---|
| 136 |
|
|---|
| 137 | if (tmp == NULL) {
|
|---|
| 138 | errno = ENOMEM;
|
|---|
| 139 | goto fail;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | rootdir = lp_parm_const_string(
|
|---|
| 143 | SNUM(handle->conn), "streams_depot", "directory",
|
|---|
| 144 | tmp);
|
|---|
| 145 |
|
|---|
| 146 | if (base_sbuf == NULL) {
|
|---|
| 147 | if (SMB_VFS_NEXT_STAT(handle, base_path, &sbuf) == -1) {
|
|---|
| 148 | /*
|
|---|
| 149 | * base file is not there
|
|---|
| 150 | */
|
|---|
| 151 | goto fail;
|
|---|
| 152 | }
|
|---|
| 153 | base_sbuf = &sbuf;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | id = SMB_VFS_FILE_ID_CREATE(handle->conn, base_sbuf->st_dev,
|
|---|
| 157 | base_sbuf->st_ino);
|
|---|
| 158 |
|
|---|
| 159 | push_file_id_16((char *)id_buf, &id);
|
|---|
| 160 |
|
|---|
| 161 | hash = hash_fn(data_blob_const(id_buf, sizeof(id_buf)));
|
|---|
| 162 |
|
|---|
| 163 | first = hash & 0xff;
|
|---|
| 164 | second = (hash >> 8) & 0xff;
|
|---|
| 165 |
|
|---|
| 166 | id_hex = hex_encode(talloc_tos(), id_buf, sizeof(id_buf));
|
|---|
| 167 |
|
|---|
| 168 | if (id_hex == NULL) {
|
|---|
| 169 | errno = ENOMEM;
|
|---|
| 170 | goto fail;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | result = talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir,
|
|---|
| 174 | first, second, id_hex);
|
|---|
| 175 |
|
|---|
| 176 | TALLOC_FREE(id_hex);
|
|---|
| 177 |
|
|---|
| 178 | if (result == NULL) {
|
|---|
| 179 | errno = ENOMEM;
|
|---|
| 180 | return NULL;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | if (SMB_VFS_NEXT_STAT(handle, result, &sbuf) == 0) {
|
|---|
| 184 | char *newname;
|
|---|
| 185 |
|
|---|
| 186 | if (!S_ISDIR(sbuf.st_mode)) {
|
|---|
| 187 | errno = EINVAL;
|
|---|
| 188 | goto fail;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | if (file_is_valid(handle, base_path, check_valid)) {
|
|---|
| 192 | return result;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /*
|
|---|
| 196 | * Someone has recreated a file under an existing inode
|
|---|
| 197 | * without deleting the streams directory. For now, just move
|
|---|
| 198 | * it away.
|
|---|
| 199 | */
|
|---|
| 200 |
|
|---|
| 201 | again:
|
|---|
| 202 | newname = talloc_asprintf(talloc_tos(), "lost-%lu", random());
|
|---|
| 203 | if (newname == NULL) {
|
|---|
| 204 | errno = ENOMEM;
|
|---|
| 205 | goto fail;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | if (SMB_VFS_NEXT_RENAME(handle, result, newname) == -1) {
|
|---|
| 209 | if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
|
|---|
| 210 | TALLOC_FREE(newname);
|
|---|
| 211 | goto again;
|
|---|
| 212 | }
|
|---|
| 213 | goto fail;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | TALLOC_FREE(newname);
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | if (!create_it) {
|
|---|
| 220 | errno = ENOENT;
|
|---|
| 221 | goto fail;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | if ((SMB_VFS_NEXT_MKDIR(handle, rootdir, 0755) != 0)
|
|---|
| 225 | && (errno != EEXIST)) {
|
|---|
| 226 | goto fail;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
|
|---|
| 230 | if (tmp == NULL) {
|
|---|
| 231 | errno = ENOMEM;
|
|---|
| 232 | goto fail;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
|
|---|
| 236 | && (errno != EEXIST)) {
|
|---|
| 237 | goto fail;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | TALLOC_FREE(tmp);
|
|---|
| 241 |
|
|---|
| 242 | tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
|
|---|
| 243 | second);
|
|---|
| 244 | if (tmp == NULL) {
|
|---|
| 245 | errno = ENOMEM;
|
|---|
| 246 | goto fail;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
|
|---|
| 250 | && (errno != EEXIST)) {
|
|---|
| 251 | goto fail;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | TALLOC_FREE(tmp);
|
|---|
| 255 |
|
|---|
| 256 | if ((SMB_VFS_NEXT_MKDIR(handle, result, 0755) != 0)
|
|---|
| 257 | && (errno != EEXIST)) {
|
|---|
| 258 | goto fail;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | if (!mark_file_valid(handle, base_path, check_valid)) {
|
|---|
| 262 | goto fail;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | return result;
|
|---|
| 266 |
|
|---|
| 267 | fail:
|
|---|
| 268 | TALLOC_FREE(result);
|
|---|
| 269 | return NULL;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | static char *stream_name(vfs_handle_struct *handle, const char *fname,
|
|---|
| 273 | bool create_dir)
|
|---|
| 274 | {
|
|---|
| 275 | char *base = NULL;
|
|---|
| 276 | char *sname = NULL;
|
|---|
| 277 | char *id_hex = NULL;
|
|---|
| 278 | char *dirname, *stream_fname;
|
|---|
| 279 |
|
|---|
| 280 | if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), fname,
|
|---|
| 281 | &base, &sname))) {
|
|---|
| 282 | DEBUG(10, ("split_ntfs_stream_name failed\n"));
|
|---|
| 283 | errno = ENOMEM;
|
|---|
| 284 | goto fail;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | /* if it's the ::$DATA stream just return the base file name */
|
|---|
| 288 | if (!sname) {
|
|---|
| 289 | return base;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | dirname = stream_dir(handle, base, NULL, create_dir);
|
|---|
| 293 |
|
|---|
| 294 | if (dirname == NULL) {
|
|---|
| 295 | goto fail;
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | stream_fname = talloc_asprintf(talloc_tos(), "%s/:%s", dirname, sname);
|
|---|
| 299 |
|
|---|
| 300 | if (stream_fname == NULL) {
|
|---|
| 301 | errno = ENOMEM;
|
|---|
| 302 | goto fail;
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | DEBUG(10, ("stream filename = %s\n", stream_fname));
|
|---|
| 306 |
|
|---|
| 307 | TALLOC_FREE(base);
|
|---|
| 308 | TALLOC_FREE(sname);
|
|---|
| 309 | TALLOC_FREE(id_hex);
|
|---|
| 310 |
|
|---|
| 311 | return stream_fname;
|
|---|
| 312 |
|
|---|
| 313 | fail:
|
|---|
| 314 | DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
|
|---|
| 315 | TALLOC_FREE(base);
|
|---|
| 316 | TALLOC_FREE(sname);
|
|---|
| 317 | TALLOC_FREE(id_hex);
|
|---|
| 318 | return NULL;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | static NTSTATUS walk_streams(vfs_handle_struct *handle,
|
|---|
| 322 | const char *fname,
|
|---|
| 323 | const SMB_STRUCT_STAT *sbuf,
|
|---|
| 324 | char **pdirname,
|
|---|
| 325 | bool (*fn)(const char *dirname,
|
|---|
| 326 | const char *dirent,
|
|---|
| 327 | void *private_data),
|
|---|
| 328 | void *private_data)
|
|---|
| 329 | {
|
|---|
| 330 | char *dirname;
|
|---|
| 331 | SMB_STRUCT_DIR *dirhandle = NULL;
|
|---|
| 332 | char *dirent;
|
|---|
| 333 |
|
|---|
| 334 | dirname = stream_dir(handle, fname, sbuf, false);
|
|---|
| 335 |
|
|---|
| 336 | if (dirname == NULL) {
|
|---|
| 337 | if (errno == ENOENT) {
|
|---|
| 338 | /*
|
|---|
| 339 | * no stream around
|
|---|
| 340 | */
|
|---|
| 341 | return NT_STATUS_OK;
|
|---|
| 342 | }
|
|---|
| 343 | return map_nt_error_from_unix(errno);
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
|
|---|
| 347 |
|
|---|
| 348 | dirhandle = SMB_VFS_NEXT_OPENDIR(handle, dirname, NULL, 0);
|
|---|
| 349 |
|
|---|
| 350 | if (dirhandle == NULL) {
|
|---|
| 351 | TALLOC_FREE(dirname);
|
|---|
| 352 | return map_nt_error_from_unix(errno);
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | while ((dirent = vfs_readdirname(handle->conn, dirhandle)) != NULL) {
|
|---|
| 356 |
|
|---|
| 357 | if (ISDOT(dirent) || ISDOTDOT(dirent)) {
|
|---|
| 358 | continue;
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | DEBUG(10, ("walk_streams: dirent=%s\n", dirent));
|
|---|
| 362 |
|
|---|
| 363 | if (!fn(dirname, dirent, private_data)) {
|
|---|
| 364 | break;
|
|---|
| 365 | }
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | SMB_VFS_NEXT_CLOSEDIR(handle, dirhandle);
|
|---|
| 369 |
|
|---|
| 370 | if (pdirname != NULL) {
|
|---|
| 371 | *pdirname = dirname;
|
|---|
| 372 | }
|
|---|
| 373 | else {
|
|---|
| 374 | TALLOC_FREE(dirname);
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | return NT_STATUS_OK;
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | static int streams_depot_stat(vfs_handle_struct *handle, const char *fname,
|
|---|
| 381 | SMB_STRUCT_STAT *sbuf)
|
|---|
| 382 | {
|
|---|
| 383 | char *stream_fname;
|
|---|
| 384 | int ret = -1;
|
|---|
| 385 |
|
|---|
| 386 | DEBUG(10, ("streams_depot_stat called for [%s]\n", fname));
|
|---|
| 387 |
|
|---|
| 388 | if (!is_ntfs_stream_name(fname)) {
|
|---|
| 389 | return SMB_VFS_NEXT_STAT(handle, fname, sbuf);
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | stream_fname = stream_name(handle, fname, false);
|
|---|
| 393 | if (stream_fname == NULL) {
|
|---|
| 394 | goto done;
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | ret = SMB_VFS_NEXT_STAT(handle, stream_fname, sbuf);
|
|---|
| 398 |
|
|---|
| 399 | done:
|
|---|
| 400 | TALLOC_FREE(stream_fname);
|
|---|
| 401 | return ret;
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | static int streams_depot_lstat(vfs_handle_struct *handle, const char *fname,
|
|---|
| 405 | SMB_STRUCT_STAT *sbuf)
|
|---|
| 406 | {
|
|---|
| 407 | char *stream_fname;
|
|---|
| 408 | int ret = -1;
|
|---|
| 409 |
|
|---|
| 410 | if (!is_ntfs_stream_name(fname)) {
|
|---|
| 411 | return SMB_VFS_NEXT_LSTAT(handle, fname, sbuf);
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | stream_fname = stream_name(handle, fname, false);
|
|---|
| 415 | if (stream_fname == NULL) {
|
|---|
| 416 | goto done;
|
|---|
| 417 | }
|
|---|
| 418 |
|
|---|
| 419 | ret = SMB_VFS_NEXT_LSTAT(handle, stream_fname, sbuf);
|
|---|
| 420 |
|
|---|
| 421 | done:
|
|---|
| 422 | TALLOC_FREE(stream_fname);
|
|---|
| 423 | return ret;
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | static int streams_depot_open(vfs_handle_struct *handle, const char *fname,
|
|---|
| 427 | files_struct *fsp, int flags, mode_t mode)
|
|---|
| 428 | {
|
|---|
| 429 | TALLOC_CTX *frame;
|
|---|
| 430 | char *base = NULL;
|
|---|
| 431 | char *sname = NULL;
|
|---|
| 432 | SMB_STRUCT_STAT base_sbuf;
|
|---|
| 433 | char *stream_fname;
|
|---|
| 434 | int ret = -1;
|
|---|
| 435 |
|
|---|
| 436 | if (!is_ntfs_stream_name(fname)) {
|
|---|
| 437 | return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | frame = talloc_stackframe();
|
|---|
| 441 |
|
|---|
| 442 | if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), fname,
|
|---|
| 443 | &base, &sname))) {
|
|---|
| 444 | errno = ENOMEM;
|
|---|
| 445 | goto done;
|
|---|
| 446 | }
|
|---|
| 447 |
|
|---|
| 448 | if (!sname) {
|
|---|
| 449 | ret = SMB_VFS_NEXT_OPEN(handle, base, fsp, flags, mode);
|
|---|
| 450 | goto done;
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | ret = SMB_VFS_NEXT_STAT(handle, base, &base_sbuf);
|
|---|
| 454 |
|
|---|
| 455 | if (ret == -1) {
|
|---|
| 456 | goto done;
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | TALLOC_FREE(base);
|
|---|
| 460 |
|
|---|
| 461 | stream_fname = stream_name(handle, fname, true);
|
|---|
| 462 | if (stream_fname == NULL) {
|
|---|
| 463 | goto done;
|
|---|
| 464 | }
|
|---|
| 465 |
|
|---|
| 466 | ret = SMB_VFS_NEXT_OPEN(handle, stream_fname, fsp, flags, mode);
|
|---|
| 467 |
|
|---|
| 468 | done:
|
|---|
| 469 | TALLOC_FREE(frame);
|
|---|
| 470 | return ret;
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | static int streams_depot_unlink(vfs_handle_struct *handle, const char *fname)
|
|---|
| 474 | {
|
|---|
| 475 | int ret = -1;
|
|---|
| 476 | SMB_STRUCT_STAT sbuf;
|
|---|
| 477 |
|
|---|
| 478 | DEBUG(10, ("streams_depot_unlink called for %s\n", fname));
|
|---|
| 479 |
|
|---|
| 480 | if (is_ntfs_stream_name(fname)) {
|
|---|
| 481 | char *stream_fname;
|
|---|
| 482 |
|
|---|
| 483 | stream_fname = stream_name(handle, fname, false);
|
|---|
| 484 | if (stream_fname == NULL) {
|
|---|
| 485 | return -1;
|
|---|
| 486 | }
|
|---|
| 487 |
|
|---|
| 488 | ret = SMB_VFS_NEXT_UNLINK(handle, stream_fname);
|
|---|
| 489 |
|
|---|
| 490 | TALLOC_FREE(stream_fname);
|
|---|
| 491 | return ret;
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | /*
|
|---|
| 495 | * We potentially need to delete the per-inode streams directory
|
|---|
| 496 | */
|
|---|
| 497 |
|
|---|
| 498 | if (SMB_VFS_NEXT_STAT(handle, fname, &sbuf) == -1) {
|
|---|
| 499 | return -1;
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | if (sbuf.st_nlink == 1) {
|
|---|
| 503 | char *dirname = stream_dir(handle, fname, &sbuf, false);
|
|---|
| 504 |
|
|---|
| 505 | if (dirname != NULL) {
|
|---|
| 506 | SMB_VFS_NEXT_RMDIR(handle, dirname);
|
|---|
| 507 | }
|
|---|
| 508 | TALLOC_FREE(dirname);
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 | return SMB_VFS_NEXT_UNLINK(handle, fname);
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| 514 | static int streams_depot_rename(vfs_handle_struct *handle,
|
|---|
| 515 | const char *oldname,
|
|---|
| 516 | const char *newname)
|
|---|
| 517 | {
|
|---|
| 518 | TALLOC_CTX *frame = NULL;
|
|---|
| 519 | int ret = -1;
|
|---|
| 520 | bool old_is_stream;
|
|---|
| 521 | bool new_is_stream;
|
|---|
| 522 | char *obase = NULL;
|
|---|
| 523 | char *osname = NULL;
|
|---|
| 524 | char *nbase = NULL;
|
|---|
| 525 | char *nsname = NULL;
|
|---|
| 526 | char *ostream_fname = NULL;
|
|---|
| 527 | char *nstream_fname = NULL;
|
|---|
| 528 | char *newname_full = NULL;
|
|---|
| 529 |
|
|---|
| 530 | DEBUG(10, ("streams_depot_rename called for %s => %s\n",
|
|---|
| 531 | oldname, newname));
|
|---|
| 532 |
|
|---|
| 533 | old_is_stream = is_ntfs_stream_name(oldname);
|
|---|
| 534 | new_is_stream = is_ntfs_stream_name(newname);
|
|---|
| 535 |
|
|---|
| 536 | if (!old_is_stream && !new_is_stream) {
|
|---|
| 537 | return SMB_VFS_NEXT_RENAME(handle, oldname, newname);
|
|---|
| 538 | }
|
|---|
| 539 |
|
|---|
| 540 | frame = talloc_stackframe();
|
|---|
| 541 |
|
|---|
| 542 | if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), oldname,
|
|---|
| 543 | &obase, &osname))) {
|
|---|
| 544 | errno = ENOMEM;
|
|---|
| 545 | goto done;
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), newname,
|
|---|
| 549 | &nbase, &nsname))) {
|
|---|
| 550 | errno = ENOMEM;
|
|---|
| 551 | goto done;
|
|---|
| 552 | }
|
|---|
| 553 |
|
|---|
| 554 | /* for now don't allow renames from or to the default stream */
|
|---|
| 555 | if (!osname || !nsname) {
|
|---|
| 556 | errno = ENOSYS;
|
|---|
| 557 | goto done;
|
|---|
| 558 | }
|
|---|
| 559 |
|
|---|
| 560 | ostream_fname = stream_name(handle, oldname, false);
|
|---|
| 561 | if (ostream_fname == NULL) {
|
|---|
| 562 | return -1;
|
|---|
| 563 | }
|
|---|
| 564 |
|
|---|
| 565 | /*
|
|---|
| 566 | * Handle passing in a stream name without the base file. This is
|
|---|
| 567 | * exercised by the NTRENAME streams rename path.
|
|---|
| 568 | */
|
|---|
| 569 | if (StrCaseCmp(nbase, "./") == 0) {
|
|---|
| 570 | newname_full = talloc_asprintf(talloc_tos(), "%s:%s", obase,
|
|---|
| 571 | nsname);
|
|---|
| 572 | if (newname_full == NULL) {
|
|---|
| 573 | errno = ENOMEM;
|
|---|
| 574 | goto done;
|
|---|
| 575 | }
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | nstream_fname = stream_name(handle,
|
|---|
| 579 | newname_full ? newname_full : newname,
|
|---|
| 580 | false);
|
|---|
| 581 | if (nstream_fname == NULL) {
|
|---|
| 582 | return -1;
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| 585 | ret = SMB_VFS_NEXT_RENAME(handle, ostream_fname, nstream_fname);
|
|---|
| 586 |
|
|---|
| 587 | done:
|
|---|
| 588 | TALLOC_FREE(frame);
|
|---|
| 589 | return ret;
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
|
|---|
| 593 | struct stream_struct **streams,
|
|---|
| 594 | const char *name, SMB_OFF_T size,
|
|---|
| 595 | SMB_OFF_T alloc_size)
|
|---|
| 596 | {
|
|---|
| 597 | struct stream_struct *tmp;
|
|---|
| 598 |
|
|---|
| 599 | tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
|
|---|
| 600 | (*num_streams)+1);
|
|---|
| 601 | if (tmp == NULL) {
|
|---|
| 602 | return false;
|
|---|
| 603 | }
|
|---|
| 604 |
|
|---|
| 605 | tmp[*num_streams].name = talloc_strdup(tmp, name);
|
|---|
| 606 | if (tmp[*num_streams].name == NULL) {
|
|---|
| 607 | return false;
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | tmp[*num_streams].size = size;
|
|---|
| 611 | tmp[*num_streams].alloc_size = alloc_size;
|
|---|
| 612 |
|
|---|
| 613 | *streams = tmp;
|
|---|
| 614 | *num_streams += 1;
|
|---|
| 615 | return true;
|
|---|
| 616 | }
|
|---|
| 617 |
|
|---|
| 618 | struct streaminfo_state {
|
|---|
| 619 | TALLOC_CTX *mem_ctx;
|
|---|
| 620 | vfs_handle_struct *handle;
|
|---|
| 621 | unsigned int num_streams;
|
|---|
| 622 | struct stream_struct *streams;
|
|---|
| 623 | NTSTATUS status;
|
|---|
| 624 | };
|
|---|
| 625 |
|
|---|
| 626 | static bool collect_one_stream(const char *dirname,
|
|---|
| 627 | const char *dirent,
|
|---|
| 628 | void *private_data)
|
|---|
| 629 | {
|
|---|
| 630 | struct streaminfo_state *state =
|
|---|
| 631 | (struct streaminfo_state *)private_data;
|
|---|
| 632 | char *full_sname;
|
|---|
| 633 | SMB_STRUCT_STAT sbuf;
|
|---|
| 634 |
|
|---|
| 635 | if (asprintf(&full_sname, "%s/%s", dirname, dirent) == -1) {
|
|---|
| 636 | state->status = NT_STATUS_NO_MEMORY;
|
|---|
| 637 | return false;
|
|---|
| 638 | }
|
|---|
| 639 | if (SMB_VFS_NEXT_STAT(state->handle, full_sname, &sbuf) == -1) {
|
|---|
| 640 | DEBUG(10, ("Could not stat %s: %s\n", full_sname,
|
|---|
| 641 | strerror(errno)));
|
|---|
| 642 | SAFE_FREE(full_sname);
|
|---|
| 643 | return true;
|
|---|
| 644 | }
|
|---|
| 645 |
|
|---|
| 646 | SAFE_FREE(full_sname);
|
|---|
| 647 |
|
|---|
| 648 | if (!add_one_stream(state->mem_ctx,
|
|---|
| 649 | &state->num_streams, &state->streams,
|
|---|
| 650 | dirent, sbuf.st_size,
|
|---|
| 651 | get_allocation_size(
|
|---|
| 652 | state->handle->conn, NULL, &sbuf))) {
|
|---|
| 653 | state->status = NT_STATUS_NO_MEMORY;
|
|---|
| 654 | return false;
|
|---|
| 655 | }
|
|---|
| 656 |
|
|---|
| 657 | return true;
|
|---|
| 658 | }
|
|---|
| 659 |
|
|---|
| 660 | static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
|
|---|
| 661 | struct files_struct *fsp,
|
|---|
| 662 | const char *fname,
|
|---|
| 663 | TALLOC_CTX *mem_ctx,
|
|---|
| 664 | unsigned int *pnum_streams,
|
|---|
| 665 | struct stream_struct **pstreams)
|
|---|
| 666 | {
|
|---|
| 667 | SMB_STRUCT_STAT sbuf;
|
|---|
| 668 | int ret;
|
|---|
| 669 | NTSTATUS status;
|
|---|
| 670 | struct streaminfo_state state;
|
|---|
| 671 |
|
|---|
| 672 | if ((fsp != NULL) && (fsp->fh->fd != -1)) {
|
|---|
| 673 | if (is_ntfs_stream_name(fsp->fsp_name)) {
|
|---|
| 674 | return NT_STATUS_INVALID_PARAMETER;
|
|---|
| 675 | }
|
|---|
| 676 | ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf);
|
|---|
| 677 | }
|
|---|
| 678 | else {
|
|---|
| 679 | if (is_ntfs_stream_name(fname)) {
|
|---|
| 680 | return NT_STATUS_INVALID_PARAMETER;
|
|---|
| 681 | }
|
|---|
| 682 | ret = SMB_VFS_NEXT_STAT(handle, fname, &sbuf);
|
|---|
| 683 | }
|
|---|
| 684 |
|
|---|
| 685 | if (ret == -1) {
|
|---|
| 686 | return map_nt_error_from_unix(errno);
|
|---|
| 687 | }
|
|---|
| 688 |
|
|---|
| 689 | state.streams = NULL;
|
|---|
| 690 | state.num_streams = 0;
|
|---|
| 691 |
|
|---|
| 692 | if (!S_ISDIR(sbuf.st_mode)) {
|
|---|
| 693 | if (!add_one_stream(mem_ctx,
|
|---|
| 694 | &state.num_streams, &state.streams,
|
|---|
| 695 | "::$DATA", sbuf.st_size,
|
|---|
| 696 | get_allocation_size(handle->conn, fsp,
|
|---|
| 697 | &sbuf))) {
|
|---|
| 698 | return NT_STATUS_NO_MEMORY;
|
|---|
| 699 | }
|
|---|
| 700 | }
|
|---|
| 701 |
|
|---|
| 702 | state.mem_ctx = mem_ctx;
|
|---|
| 703 | state.handle = handle;
|
|---|
| 704 | state.status = NT_STATUS_OK;
|
|---|
| 705 |
|
|---|
| 706 | status = walk_streams(handle, fname, &sbuf, NULL, collect_one_stream,
|
|---|
| 707 | &state);
|
|---|
| 708 |
|
|---|
| 709 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 710 | TALLOC_FREE(state.streams);
|
|---|
| 711 | return status;
|
|---|
| 712 | }
|
|---|
| 713 |
|
|---|
| 714 | if (!NT_STATUS_IS_OK(state.status)) {
|
|---|
| 715 | TALLOC_FREE(state.streams);
|
|---|
| 716 | return state.status;
|
|---|
| 717 | }
|
|---|
| 718 |
|
|---|
| 719 | *pnum_streams = state.num_streams;
|
|---|
| 720 | *pstreams = state.streams;
|
|---|
| 721 | return NT_STATUS_OK;
|
|---|
| 722 | }
|
|---|
| 723 |
|
|---|
| 724 | static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle)
|
|---|
| 725 | {
|
|---|
| 726 | return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
|
|---|
| 727 | }
|
|---|
| 728 |
|
|---|
| 729 | /* VFS operations structure */
|
|---|
| 730 |
|
|---|
| 731 | static vfs_op_tuple streams_depot_ops[] = {
|
|---|
| 732 | {SMB_VFS_OP(streams_depot_fs_capabilities), SMB_VFS_OP_FS_CAPABILITIES,
|
|---|
| 733 | SMB_VFS_LAYER_TRANSPARENT},
|
|---|
| 734 | {SMB_VFS_OP(streams_depot_open), SMB_VFS_OP_OPEN,
|
|---|
| 735 | SMB_VFS_LAYER_TRANSPARENT},
|
|---|
| 736 | {SMB_VFS_OP(streams_depot_stat), SMB_VFS_OP_STAT,
|
|---|
| 737 | SMB_VFS_LAYER_TRANSPARENT},
|
|---|
| 738 | {SMB_VFS_OP(streams_depot_lstat), SMB_VFS_OP_LSTAT,
|
|---|
| 739 | SMB_VFS_LAYER_TRANSPARENT},
|
|---|
| 740 | {SMB_VFS_OP(streams_depot_unlink), SMB_VFS_OP_UNLINK,
|
|---|
| 741 | SMB_VFS_LAYER_TRANSPARENT},
|
|---|
| 742 | {SMB_VFS_OP(streams_depot_rename), SMB_VFS_OP_RENAME,
|
|---|
| 743 | SMB_VFS_LAYER_TRANSPARENT},
|
|---|
| 744 | {SMB_VFS_OP(streams_depot_streaminfo), SMB_VFS_OP_STREAMINFO,
|
|---|
| 745 | SMB_VFS_LAYER_OPAQUE},
|
|---|
| 746 | {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
|
|---|
| 747 | };
|
|---|
| 748 |
|
|---|
| 749 | NTSTATUS vfs_streams_depot_init(void);
|
|---|
| 750 | NTSTATUS vfs_streams_depot_init(void)
|
|---|
| 751 | {
|
|---|
| 752 | return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
|
|---|
| 753 | streams_depot_ops);
|
|---|
| 754 | }
|
|---|