1 | /*
|
---|
2 | * Store streams in xattrs
|
---|
3 | *
|
---|
4 | * Copyright (C) Volker Lendecke, 2008
|
---|
5 | *
|
---|
6 | * Partly based on James Peach's Darwin module, which is
|
---|
7 | *
|
---|
8 | * Copyright (C) James Peach 2006-2007
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 3 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "includes.h"
|
---|
25 |
|
---|
26 | #undef DBGC_CLASS
|
---|
27 | #define DBGC_CLASS DBGC_VFS
|
---|
28 |
|
---|
29 | struct stream_io {
|
---|
30 | char *base;
|
---|
31 | char *xattr_name;
|
---|
32 | void *fsp_name_ptr;
|
---|
33 | files_struct *fsp;
|
---|
34 | vfs_handle_struct *handle;
|
---|
35 | };
|
---|
36 |
|
---|
37 | static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
|
---|
38 | {
|
---|
39 | struct MD5Context ctx;
|
---|
40 | unsigned char hash[16];
|
---|
41 | SMB_INO_T result;
|
---|
42 | char *upper_sname;
|
---|
43 |
|
---|
44 | DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
|
---|
45 | (unsigned long)sbuf->st_dev,
|
---|
46 | (unsigned long)sbuf->st_ino, sname));
|
---|
47 |
|
---|
48 | upper_sname = talloc_strdup_upper(talloc_tos(), sname);
|
---|
49 | SMB_ASSERT(upper_sname != NULL);
|
---|
50 |
|
---|
51 | MD5Init(&ctx);
|
---|
52 | MD5Update(&ctx, (unsigned char *)&(sbuf->st_dev),
|
---|
53 | sizeof(sbuf->st_dev));
|
---|
54 | MD5Update(&ctx, (unsigned char *)&(sbuf->st_ino),
|
---|
55 | sizeof(sbuf->st_ino));
|
---|
56 | MD5Update(&ctx, (unsigned char *)upper_sname,
|
---|
57 | talloc_get_size(upper_sname)-1);
|
---|
58 | MD5Final(hash, &ctx);
|
---|
59 |
|
---|
60 | TALLOC_FREE(upper_sname);
|
---|
61 |
|
---|
62 | /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
|
---|
63 | memcpy(&result, hash, sizeof(result));
|
---|
64 |
|
---|
65 | DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
|
---|
66 |
|
---|
67 | return result;
|
---|
68 | }
|
---|
69 |
|
---|
70 | static ssize_t get_xattr_size(connection_struct *conn,
|
---|
71 | files_struct *fsp,
|
---|
72 | const char *fname,
|
---|
73 | const char *xattr_name)
|
---|
74 | {
|
---|
75 | NTSTATUS status;
|
---|
76 | struct ea_struct ea;
|
---|
77 | ssize_t result;
|
---|
78 |
|
---|
79 | status = get_ea_value(talloc_tos(), conn, fsp, fname,
|
---|
80 | xattr_name, &ea);
|
---|
81 |
|
---|
82 | if (!NT_STATUS_IS_OK(status)) {
|
---|
83 | return -1;
|
---|
84 | }
|
---|
85 |
|
---|
86 | result = ea.value.length-1;
|
---|
87 | TALLOC_FREE(ea.value.data);
|
---|
88 | return result;
|
---|
89 | }
|
---|
90 |
|
---|
91 | static bool streams_xattr_recheck(struct stream_io *sio)
|
---|
92 | {
|
---|
93 | NTSTATUS status;
|
---|
94 | char *base = NULL;
|
---|
95 | char *sname = NULL;
|
---|
96 | char *xattr_name = NULL;
|
---|
97 |
|
---|
98 | if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
|
---|
99 | return true;
|
---|
100 | }
|
---|
101 |
|
---|
102 | status = split_ntfs_stream_name(talloc_tos(), sio->fsp->fsp_name,
|
---|
103 | &base, &sname);
|
---|
104 | if (!NT_STATUS_IS_OK(status)) {
|
---|
105 | return false;
|
---|
106 | }
|
---|
107 |
|
---|
108 | if (sname == NULL) {
|
---|
109 | /* how can this happen */
|
---|
110 | errno = EINVAL;
|
---|
111 | return false;
|
---|
112 | }
|
---|
113 |
|
---|
114 | xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
|
---|
115 | SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
|
---|
116 | if (xattr_name == NULL) {
|
---|
117 | return false;
|
---|
118 | }
|
---|
119 |
|
---|
120 | TALLOC_FREE(sio->xattr_name);
|
---|
121 | TALLOC_FREE(sio->base);
|
---|
122 | sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
|
---|
123 | xattr_name);
|
---|
124 | sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
|
---|
125 | base);
|
---|
126 | sio->fsp_name_ptr = sio->fsp->fsp_name;
|
---|
127 |
|
---|
128 | if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
|
---|
129 | return false;
|
---|
130 | }
|
---|
131 |
|
---|
132 | return true;
|
---|
133 | }
|
---|
134 |
|
---|
135 | static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
|
---|
136 | SMB_STRUCT_STAT *sbuf)
|
---|
137 | {
|
---|
138 | int ret = -1;
|
---|
139 | struct stream_io *io = (struct stream_io *)
|
---|
140 | VFS_FETCH_FSP_EXTENSION(handle, fsp);
|
---|
141 |
|
---|
142 | DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
|
---|
143 |
|
---|
144 | if (io == NULL || fsp->base_fsp == NULL) {
|
---|
145 | return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
|
---|
146 | }
|
---|
147 |
|
---|
148 | if (!streams_xattr_recheck(io)) {
|
---|
149 | return -1;
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (lp_posix_pathnames()) {
|
---|
153 | ret = SMB_VFS_LSTAT(handle->conn, io->base, sbuf);
|
---|
154 | } else {
|
---|
155 | ret = SMB_VFS_STAT(handle->conn, io->base, sbuf);
|
---|
156 | }
|
---|
157 |
|
---|
158 | if (ret == -1) {
|
---|
159 | return -1;
|
---|
160 | }
|
---|
161 |
|
---|
162 | sbuf->st_size = get_xattr_size(handle->conn, fsp->base_fsp,
|
---|
163 | io->base, io->xattr_name);
|
---|
164 | if (sbuf->st_size == -1) {
|
---|
165 | return -1;
|
---|
166 | }
|
---|
167 |
|
---|
168 | DEBUG(10, ("sbuf->st_size = %d\n", (int)sbuf->st_size));
|
---|
169 |
|
---|
170 | sbuf->st_ino = stream_inode(sbuf, io->xattr_name);
|
---|
171 | sbuf->st_mode &= ~S_IFMT;
|
---|
172 | sbuf->st_mode |= S_IFREG;
|
---|
173 | sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
|
---|
174 |
|
---|
175 | return 0;
|
---|
176 | }
|
---|
177 |
|
---|
178 | static int streams_xattr_stat(vfs_handle_struct *handle, const char *fname,
|
---|
179 | SMB_STRUCT_STAT *sbuf)
|
---|
180 | {
|
---|
181 | NTSTATUS status;
|
---|
182 | char *base = NULL, *sname = NULL;
|
---|
183 | int result = -1;
|
---|
184 | char *xattr_name;
|
---|
185 |
|
---|
186 | if (!is_ntfs_stream_name(fname)) {
|
---|
187 | return SMB_VFS_NEXT_STAT(handle, fname, sbuf);
|
---|
188 | }
|
---|
189 |
|
---|
190 | status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
|
---|
191 | if (!NT_STATUS_IS_OK(status)) {
|
---|
192 | errno = EINVAL;
|
---|
193 | return -1;
|
---|
194 | }
|
---|
195 |
|
---|
196 | if (sname == NULL){
|
---|
197 | return SMB_VFS_NEXT_STAT(handle, base, sbuf);
|
---|
198 | }
|
---|
199 |
|
---|
200 | if (SMB_VFS_STAT(handle->conn, base, sbuf) == -1) {
|
---|
201 | goto fail;
|
---|
202 | }
|
---|
203 |
|
---|
204 | xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
|
---|
205 | SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
|
---|
206 | if (xattr_name == NULL) {
|
---|
207 | errno = ENOMEM;
|
---|
208 | goto fail;
|
---|
209 | }
|
---|
210 |
|
---|
211 | sbuf->st_size = get_xattr_size(handle->conn, NULL, base, xattr_name);
|
---|
212 | if (sbuf->st_size == -1) {
|
---|
213 | errno = ENOENT;
|
---|
214 | goto fail;
|
---|
215 | }
|
---|
216 |
|
---|
217 | sbuf->st_ino = stream_inode(sbuf, xattr_name);
|
---|
218 | sbuf->st_mode &= ~S_IFMT;
|
---|
219 | sbuf->st_mode |= S_IFREG;
|
---|
220 | sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
|
---|
221 |
|
---|
222 | result = 0;
|
---|
223 | fail:
|
---|
224 | TALLOC_FREE(base);
|
---|
225 | TALLOC_FREE(sname);
|
---|
226 | return result;
|
---|
227 | }
|
---|
228 |
|
---|
229 | static int streams_xattr_lstat(vfs_handle_struct *handle, const char *fname,
|
---|
230 | SMB_STRUCT_STAT *sbuf)
|
---|
231 | {
|
---|
232 | NTSTATUS status;
|
---|
233 | char *base, *sname;
|
---|
234 | int result = -1;
|
---|
235 | char *xattr_name;
|
---|
236 |
|
---|
237 | if (!is_ntfs_stream_name(fname)) {
|
---|
238 | return SMB_VFS_NEXT_LSTAT(handle, fname, sbuf);
|
---|
239 | }
|
---|
240 |
|
---|
241 | status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
|
---|
242 | if (!NT_STATUS_IS_OK(status)) {
|
---|
243 | errno = EINVAL;
|
---|
244 | goto fail;
|
---|
245 | }
|
---|
246 |
|
---|
247 | if (sname == NULL){
|
---|
248 | return SMB_VFS_NEXT_LSTAT(handle, base, sbuf);
|
---|
249 | }
|
---|
250 |
|
---|
251 | if (SMB_VFS_LSTAT(handle->conn, base, sbuf) == -1) {
|
---|
252 | goto fail;
|
---|
253 | }
|
---|
254 |
|
---|
255 | xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
|
---|
256 | SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
|
---|
257 | if (xattr_name == NULL) {
|
---|
258 | errno = ENOMEM;
|
---|
259 | goto fail;
|
---|
260 | }
|
---|
261 |
|
---|
262 | sbuf->st_size = get_xattr_size(handle->conn, NULL, base, xattr_name);
|
---|
263 | if (sbuf->st_size == -1) {
|
---|
264 | errno = ENOENT;
|
---|
265 | goto fail;
|
---|
266 | }
|
---|
267 |
|
---|
268 | sbuf->st_ino = stream_inode(sbuf, xattr_name);
|
---|
269 | sbuf->st_mode &= ~S_IFMT;
|
---|
270 | sbuf->st_mode |= S_IFREG;
|
---|
271 | sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
|
---|
272 |
|
---|
273 | result = 0;
|
---|
274 | fail:
|
---|
275 | TALLOC_FREE(base);
|
---|
276 | TALLOC_FREE(sname);
|
---|
277 | return result;
|
---|
278 | }
|
---|
279 |
|
---|
280 | static int streams_xattr_open(vfs_handle_struct *handle, const char *fname,
|
---|
281 | files_struct *fsp, int flags, mode_t mode)
|
---|
282 | {
|
---|
283 | TALLOC_CTX *frame;
|
---|
284 | NTSTATUS status;
|
---|
285 | struct stream_io *sio;
|
---|
286 | char *base, *sname;
|
---|
287 | struct ea_struct ea;
|
---|
288 | char *xattr_name;
|
---|
289 | int baseflags;
|
---|
290 | int hostfd = -1;
|
---|
291 |
|
---|
292 | DEBUG(10, ("streams_xattr_open called for %s\n", fname));
|
---|
293 |
|
---|
294 | if (!is_ntfs_stream_name(fname)) {
|
---|
295 | return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
|
---|
296 | }
|
---|
297 |
|
---|
298 | frame = talloc_stackframe();
|
---|
299 |
|
---|
300 | status = split_ntfs_stream_name(talloc_tos(), fname,
|
---|
301 | &base, &sname);
|
---|
302 | if (!NT_STATUS_IS_OK(status)) {
|
---|
303 | errno = EINVAL;
|
---|
304 | goto fail;
|
---|
305 | }
|
---|
306 |
|
---|
307 | if (sname == NULL) {
|
---|
308 | hostfd = SMB_VFS_NEXT_OPEN(handle, base, fsp, flags, mode);
|
---|
309 | talloc_free(frame);
|
---|
310 | return hostfd;
|
---|
311 | }
|
---|
312 |
|
---|
313 | xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
|
---|
314 | SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
|
---|
315 | if (xattr_name == NULL) {
|
---|
316 | errno = ENOMEM;
|
---|
317 | goto fail;
|
---|
318 | }
|
---|
319 |
|
---|
320 | /*
|
---|
321 | * We use baseflags to turn off nasty side-effects when opening the
|
---|
322 | * underlying file.
|
---|
323 | */
|
---|
324 | baseflags = flags;
|
---|
325 | baseflags &= ~O_TRUNC;
|
---|
326 | baseflags &= ~O_EXCL;
|
---|
327 | baseflags &= ~O_CREAT;
|
---|
328 |
|
---|
329 | hostfd = SMB_VFS_OPEN(handle->conn, base, fsp, baseflags, mode);
|
---|
330 |
|
---|
331 | /* It is legit to open a stream on a directory, but the base
|
---|
332 | * fd has to be read-only.
|
---|
333 | */
|
---|
334 | if ((hostfd == -1) && (errno == EISDIR)) {
|
---|
335 | baseflags &= ~O_ACCMODE;
|
---|
336 | baseflags |= O_RDONLY;
|
---|
337 | hostfd = SMB_VFS_OPEN(handle->conn, fname, fsp, baseflags,
|
---|
338 | mode);
|
---|
339 | }
|
---|
340 |
|
---|
341 | if (hostfd == -1) {
|
---|
342 | goto fail;
|
---|
343 | }
|
---|
344 |
|
---|
345 | status = get_ea_value(talloc_tos(), handle->conn, NULL, base,
|
---|
346 | xattr_name, &ea);
|
---|
347 |
|
---|
348 | DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
|
---|
349 |
|
---|
350 | if (!NT_STATUS_IS_OK(status)
|
---|
351 | && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
|
---|
352 | /*
|
---|
353 | * The base file is not there. This is an error even if we got
|
---|
354 | * O_CREAT, the higher levels should have created the base
|
---|
355 | * file for us.
|
---|
356 | */
|
---|
357 | DEBUG(10, ("streams_xattr_open: base file %s not around, "
|
---|
358 | "returning ENOENT\n", base));
|
---|
359 | errno = ENOENT;
|
---|
360 | goto fail;
|
---|
361 | }
|
---|
362 |
|
---|
363 | if (!NT_STATUS_IS_OK(status)) {
|
---|
364 | /*
|
---|
365 | * The attribute does not exist
|
---|
366 | */
|
---|
367 |
|
---|
368 | if (flags & O_CREAT) {
|
---|
369 | /*
|
---|
370 | * Darn, xattrs need at least 1 byte
|
---|
371 | */
|
---|
372 | char null = '\0';
|
---|
373 |
|
---|
374 | DEBUG(10, ("creating attribute %s on file %s\n",
|
---|
375 | xattr_name, base));
|
---|
376 |
|
---|
377 | if (fsp->base_fsp->fh->fd != -1) {
|
---|
378 | if (SMB_VFS_FSETXATTR(
|
---|
379 | fsp->base_fsp, xattr_name,
|
---|
380 | &null, sizeof(null),
|
---|
381 | flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
|
---|
382 | goto fail;
|
---|
383 | }
|
---|
384 | } else {
|
---|
385 | if (SMB_VFS_SETXATTR(
|
---|
386 | handle->conn, base, xattr_name,
|
---|
387 | &null, sizeof(null),
|
---|
388 | flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
|
---|
389 | goto fail;
|
---|
390 | }
|
---|
391 | }
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | if (flags & O_TRUNC) {
|
---|
396 | char null = '\0';
|
---|
397 | if (fsp->base_fsp->fh->fd != -1) {
|
---|
398 | if (SMB_VFS_FSETXATTR(
|
---|
399 | fsp->base_fsp, xattr_name,
|
---|
400 | &null, sizeof(null),
|
---|
401 | flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
|
---|
402 | goto fail;
|
---|
403 | }
|
---|
404 | } else {
|
---|
405 | if (SMB_VFS_SETXATTR(
|
---|
406 | handle->conn, base, xattr_name,
|
---|
407 | &null, sizeof(null),
|
---|
408 | flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
|
---|
409 | goto fail;
|
---|
410 | }
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
|
---|
415 | struct stream_io);
|
---|
416 | if (sio == NULL) {
|
---|
417 | errno = ENOMEM;
|
---|
418 | goto fail;
|
---|
419 | }
|
---|
420 |
|
---|
421 | sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
|
---|
422 | xattr_name);
|
---|
423 | sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
|
---|
424 | base);
|
---|
425 | sio->fsp_name_ptr = fsp->fsp_name;
|
---|
426 | sio->handle = handle;
|
---|
427 | sio->fsp = fsp;
|
---|
428 |
|
---|
429 | if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
|
---|
430 | errno = ENOMEM;
|
---|
431 | goto fail;
|
---|
432 | }
|
---|
433 |
|
---|
434 | TALLOC_FREE(frame);
|
---|
435 | return hostfd;
|
---|
436 |
|
---|
437 | fail:
|
---|
438 | if (hostfd >= 0) {
|
---|
439 | /*
|
---|
440 | * BUGBUGBUG -- we would need to call fd_close_posix here, but
|
---|
441 | * we don't have a full fsp yet
|
---|
442 | */
|
---|
443 | SMB_VFS_CLOSE(fsp);
|
---|
444 | }
|
---|
445 |
|
---|
446 | TALLOC_FREE(frame);
|
---|
447 | return -1;
|
---|
448 | }
|
---|
449 |
|
---|
450 | static int streams_xattr_unlink(vfs_handle_struct *handle, const char *fname)
|
---|
451 | {
|
---|
452 | NTSTATUS status;
|
---|
453 | char *base = NULL;
|
---|
454 | char *sname = NULL;
|
---|
455 | int ret = -1;
|
---|
456 | char *xattr_name;
|
---|
457 |
|
---|
458 | if (!is_ntfs_stream_name(fname)) {
|
---|
459 | return SMB_VFS_NEXT_UNLINK(handle, fname);
|
---|
460 | }
|
---|
461 |
|
---|
462 | status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
|
---|
463 | if (!NT_STATUS_IS_OK(status)) {
|
---|
464 | errno = EINVAL;
|
---|
465 | goto fail;
|
---|
466 | }
|
---|
467 |
|
---|
468 | if (sname == NULL){
|
---|
469 | return SMB_VFS_NEXT_UNLINK(handle, base);
|
---|
470 | }
|
---|
471 |
|
---|
472 | xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
|
---|
473 | SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
|
---|
474 | if (xattr_name == NULL) {
|
---|
475 | errno = ENOMEM;
|
---|
476 | goto fail;
|
---|
477 | }
|
---|
478 |
|
---|
479 | ret = SMB_VFS_REMOVEXATTR(handle->conn, base, xattr_name);
|
---|
480 |
|
---|
481 | if ((ret == -1) && (errno == ENOATTR)) {
|
---|
482 | errno = ENOENT;
|
---|
483 | goto fail;
|
---|
484 | }
|
---|
485 |
|
---|
486 | ret = 0;
|
---|
487 |
|
---|
488 | fail:
|
---|
489 | TALLOC_FREE(base);
|
---|
490 | TALLOC_FREE(sname);
|
---|
491 | return ret;
|
---|
492 | }
|
---|
493 |
|
---|
494 | static int streams_xattr_rename(vfs_handle_struct *handle,
|
---|
495 | const char *oldname,
|
---|
496 | const char *newname)
|
---|
497 | {
|
---|
498 | NTSTATUS status;
|
---|
499 | TALLOC_CTX *frame = NULL;
|
---|
500 | char *obase;
|
---|
501 | char *ostream;
|
---|
502 | char *nbase;
|
---|
503 | char *nstream;
|
---|
504 | const char *base;
|
---|
505 | int ret = -1;
|
---|
506 | char *oxattr_name;
|
---|
507 | char *nxattr_name;
|
---|
508 | bool o_is_stream;
|
---|
509 | bool n_is_stream;
|
---|
510 | ssize_t oret;
|
---|
511 | ssize_t nret;
|
---|
512 | struct ea_struct ea;
|
---|
513 |
|
---|
514 | o_is_stream = is_ntfs_stream_name(oldname);
|
---|
515 | n_is_stream = is_ntfs_stream_name(newname);
|
---|
516 |
|
---|
517 | if (!o_is_stream && !n_is_stream) {
|
---|
518 | return SMB_VFS_NEXT_RENAME(handle, oldname, newname);
|
---|
519 | }
|
---|
520 |
|
---|
521 | frame = talloc_stackframe();
|
---|
522 | if (!frame) {
|
---|
523 | goto fail;
|
---|
524 | }
|
---|
525 |
|
---|
526 | status = split_ntfs_stream_name(talloc_tos(), oldname, &obase, &ostream);
|
---|
527 | if (!NT_STATUS_IS_OK(status)) {
|
---|
528 | errno = EINVAL;
|
---|
529 | goto fail;
|
---|
530 | }
|
---|
531 |
|
---|
532 | status = split_ntfs_stream_name(talloc_tos(), newname, &nbase, &nstream);
|
---|
533 | if (!NT_STATUS_IS_OK(status)) {
|
---|
534 | errno = EINVAL;
|
---|
535 | goto fail;
|
---|
536 | }
|
---|
537 |
|
---|
538 | /*TODO: maybe call SMB_VFS_NEXT_RENAME() both streams are NULL (::$DATA) */
|
---|
539 | if (ostream == NULL) {
|
---|
540 | errno = ENOSYS;
|
---|
541 | goto fail;
|
---|
542 | }
|
---|
543 |
|
---|
544 | if (nstream == NULL) {
|
---|
545 | errno = ENOSYS;
|
---|
546 | goto fail;
|
---|
547 | }
|
---|
548 |
|
---|
549 | if (StrCaseCmp(ostream, nstream) == 0) {
|
---|
550 | goto done;
|
---|
551 | }
|
---|
552 |
|
---|
553 | base = obase;
|
---|
554 |
|
---|
555 | oxattr_name = talloc_asprintf(talloc_tos(), "%s%s",
|
---|
556 | SAMBA_XATTR_DOSSTREAM_PREFIX, ostream);
|
---|
557 | if (oxattr_name == NULL) {
|
---|
558 | errno = ENOMEM;
|
---|
559 | goto fail;
|
---|
560 | }
|
---|
561 |
|
---|
562 | nxattr_name = talloc_asprintf(talloc_tos(), "%s%s",
|
---|
563 | SAMBA_XATTR_DOSSTREAM_PREFIX, nstream);
|
---|
564 | if (nxattr_name == NULL) {
|
---|
565 | errno = ENOMEM;
|
---|
566 | goto fail;
|
---|
567 | }
|
---|
568 |
|
---|
569 | /* read the old stream */
|
---|
570 | status = get_ea_value(talloc_tos(), handle->conn, NULL,
|
---|
571 | base, oxattr_name, &ea);
|
---|
572 | if (!NT_STATUS_IS_OK(status)) {
|
---|
573 | errno = ENOENT;
|
---|
574 | goto fail;
|
---|
575 | }
|
---|
576 |
|
---|
577 | /* (over)write the new stream */
|
---|
578 | nret = SMB_VFS_SETXATTR(handle->conn, base, nxattr_name,
|
---|
579 | ea.value.data, ea.value.length, 0);
|
---|
580 | if (nret < 0) {
|
---|
581 | if (errno == ENOATTR) {
|
---|
582 | errno = ENOENT;
|
---|
583 | }
|
---|
584 | goto fail;
|
---|
585 | }
|
---|
586 |
|
---|
587 | /* remove the old stream */
|
---|
588 | oret = SMB_VFS_REMOVEXATTR(handle->conn, base, oxattr_name);
|
---|
589 | if (oret < 0) {
|
---|
590 | if (errno == ENOATTR) {
|
---|
591 | errno = ENOENT;
|
---|
592 | }
|
---|
593 | goto fail;
|
---|
594 | }
|
---|
595 |
|
---|
596 | done:
|
---|
597 | errno = 0;
|
---|
598 | ret = 0;
|
---|
599 | fail:
|
---|
600 | TALLOC_FREE(frame);
|
---|
601 | return ret;
|
---|
602 | }
|
---|
603 |
|
---|
604 | static NTSTATUS walk_xattr_streams(connection_struct *conn, files_struct *fsp,
|
---|
605 | const char *fname,
|
---|
606 | bool (*fn)(struct ea_struct *ea,
|
---|
607 | void *private_data),
|
---|
608 | void *private_data)
|
---|
609 | {
|
---|
610 | NTSTATUS status;
|
---|
611 | char **names;
|
---|
612 | size_t i, num_names;
|
---|
613 | size_t prefix_len = strlen(SAMBA_XATTR_DOSSTREAM_PREFIX);
|
---|
614 |
|
---|
615 | status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
|
---|
616 | &names, &num_names);
|
---|
617 | if (!NT_STATUS_IS_OK(status)) {
|
---|
618 | return status;
|
---|
619 | }
|
---|
620 |
|
---|
621 | for (i=0; i<num_names; i++) {
|
---|
622 | struct ea_struct ea;
|
---|
623 |
|
---|
624 | if (strncmp(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
|
---|
625 | prefix_len) != 0) {
|
---|
626 | continue;
|
---|
627 | }
|
---|
628 |
|
---|
629 | status = get_ea_value(names, conn, fsp, fname, names[i], &ea);
|
---|
630 | if (!NT_STATUS_IS_OK(status)) {
|
---|
631 | DEBUG(10, ("Could not get ea %s for file %s: %s\n",
|
---|
632 | names[i], fname, nt_errstr(status)));
|
---|
633 | continue;
|
---|
634 | }
|
---|
635 |
|
---|
636 | ea.name = talloc_asprintf(ea.value.data, ":%s",
|
---|
637 | names[i] + prefix_len);
|
---|
638 | if (ea.name == NULL) {
|
---|
639 | DEBUG(0, ("talloc failed\n"));
|
---|
640 | continue;
|
---|
641 | }
|
---|
642 |
|
---|
643 | if (!fn(&ea, private_data)) {
|
---|
644 | TALLOC_FREE(ea.value.data);
|
---|
645 | return NT_STATUS_OK;
|
---|
646 | }
|
---|
647 |
|
---|
648 | TALLOC_FREE(ea.value.data);
|
---|
649 | }
|
---|
650 |
|
---|
651 | TALLOC_FREE(names);
|
---|
652 | return NT_STATUS_OK;
|
---|
653 | }
|
---|
654 |
|
---|
655 | static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
|
---|
656 | struct stream_struct **streams,
|
---|
657 | const char *name, SMB_OFF_T size,
|
---|
658 | SMB_OFF_T alloc_size)
|
---|
659 | {
|
---|
660 | struct stream_struct *tmp;
|
---|
661 |
|
---|
662 | tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
|
---|
663 | (*num_streams)+1);
|
---|
664 | if (tmp == NULL) {
|
---|
665 | return false;
|
---|
666 | }
|
---|
667 |
|
---|
668 | tmp[*num_streams].name = talloc_strdup(tmp, name);
|
---|
669 | if (tmp[*num_streams].name == NULL) {
|
---|
670 | return false;
|
---|
671 | }
|
---|
672 |
|
---|
673 | tmp[*num_streams].size = size;
|
---|
674 | tmp[*num_streams].alloc_size = alloc_size;
|
---|
675 |
|
---|
676 | *streams = tmp;
|
---|
677 | *num_streams += 1;
|
---|
678 | return true;
|
---|
679 | }
|
---|
680 |
|
---|
681 | struct streaminfo_state {
|
---|
682 | TALLOC_CTX *mem_ctx;
|
---|
683 | vfs_handle_struct *handle;
|
---|
684 | unsigned int num_streams;
|
---|
685 | struct stream_struct *streams;
|
---|
686 | NTSTATUS status;
|
---|
687 | };
|
---|
688 |
|
---|
689 | static bool collect_one_stream(struct ea_struct *ea, void *private_data)
|
---|
690 | {
|
---|
691 | struct streaminfo_state *state =
|
---|
692 | (struct streaminfo_state *)private_data;
|
---|
693 |
|
---|
694 | if (!add_one_stream(state->mem_ctx,
|
---|
695 | &state->num_streams, &state->streams,
|
---|
696 | ea->name, ea->value.length-1,
|
---|
697 | smb_roundup(state->handle->conn,
|
---|
698 | ea->value.length-1))) {
|
---|
699 | state->status = NT_STATUS_NO_MEMORY;
|
---|
700 | return false;
|
---|
701 | }
|
---|
702 |
|
---|
703 | return true;
|
---|
704 | }
|
---|
705 |
|
---|
706 | static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
|
---|
707 | struct files_struct *fsp,
|
---|
708 | const char *fname,
|
---|
709 | TALLOC_CTX *mem_ctx,
|
---|
710 | unsigned int *pnum_streams,
|
---|
711 | struct stream_struct **pstreams)
|
---|
712 | {
|
---|
713 | SMB_STRUCT_STAT sbuf;
|
---|
714 | int ret;
|
---|
715 | NTSTATUS status;
|
---|
716 | struct streaminfo_state state;
|
---|
717 |
|
---|
718 | if ((fsp != NULL) && (fsp->fh->fd != -1)) {
|
---|
719 | if (is_ntfs_stream_name(fsp->fsp_name)) {
|
---|
720 | return NT_STATUS_INVALID_PARAMETER;
|
---|
721 | }
|
---|
722 | ret = SMB_VFS_FSTAT(fsp, &sbuf);
|
---|
723 | }
|
---|
724 | else {
|
---|
725 | if (is_ntfs_stream_name(fname)) {
|
---|
726 | return NT_STATUS_INVALID_PARAMETER;
|
---|
727 | }
|
---|
728 | if (lp_posix_pathnames()) {
|
---|
729 | ret = SMB_VFS_LSTAT(handle->conn, fname, &sbuf);
|
---|
730 | } else {
|
---|
731 | ret = SMB_VFS_STAT(handle->conn, fname, &sbuf);
|
---|
732 | }
|
---|
733 | }
|
---|
734 |
|
---|
735 | if (ret == -1) {
|
---|
736 | return map_nt_error_from_unix(errno);
|
---|
737 | }
|
---|
738 |
|
---|
739 | state.streams = NULL;
|
---|
740 | state.num_streams = 0;
|
---|
741 |
|
---|
742 | if (!S_ISDIR(sbuf.st_mode)) {
|
---|
743 | if (!add_one_stream(mem_ctx,
|
---|
744 | &state.num_streams, &state.streams,
|
---|
745 | "::$DATA", sbuf.st_size,
|
---|
746 | get_allocation_size(handle->conn, fsp,
|
---|
747 | &sbuf))) {
|
---|
748 | return NT_STATUS_NO_MEMORY;
|
---|
749 | }
|
---|
750 | }
|
---|
751 |
|
---|
752 | state.mem_ctx = mem_ctx;
|
---|
753 | state.handle = handle;
|
---|
754 | state.status = NT_STATUS_OK;
|
---|
755 |
|
---|
756 | status = walk_xattr_streams(handle->conn, fsp, fname,
|
---|
757 | collect_one_stream, &state);
|
---|
758 |
|
---|
759 | if (!NT_STATUS_IS_OK(status)) {
|
---|
760 | TALLOC_FREE(state.streams);
|
---|
761 | return status;
|
---|
762 | }
|
---|
763 |
|
---|
764 | if (!NT_STATUS_IS_OK(state.status)) {
|
---|
765 | TALLOC_FREE(state.streams);
|
---|
766 | return state.status;
|
---|
767 | }
|
---|
768 |
|
---|
769 | *pnum_streams = state.num_streams;
|
---|
770 | *pstreams = state.streams;
|
---|
771 | return NT_STATUS_OK;
|
---|
772 | }
|
---|
773 |
|
---|
774 | static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle)
|
---|
775 | {
|
---|
776 | return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
|
---|
777 | }
|
---|
778 |
|
---|
779 | static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
|
---|
780 | files_struct *fsp, const void *data,
|
---|
781 | size_t n, SMB_OFF_T offset)
|
---|
782 | {
|
---|
783 | struct stream_io *sio =
|
---|
784 | (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
|
---|
785 | struct ea_struct ea;
|
---|
786 | NTSTATUS status;
|
---|
787 | int ret;
|
---|
788 |
|
---|
789 | DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
|
---|
790 |
|
---|
791 | if (sio == NULL) {
|
---|
792 | return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
|
---|
793 | }
|
---|
794 |
|
---|
795 | if (!streams_xattr_recheck(sio)) {
|
---|
796 | return -1;
|
---|
797 | }
|
---|
798 |
|
---|
799 | status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
|
---|
800 | sio->base, sio->xattr_name, &ea);
|
---|
801 | if (!NT_STATUS_IS_OK(status)) {
|
---|
802 | return -1;
|
---|
803 | }
|
---|
804 |
|
---|
805 | if ((offset + n) > ea.value.length-1) {
|
---|
806 | uint8 *tmp;
|
---|
807 |
|
---|
808 | tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
|
---|
809 | offset + n + 1);
|
---|
810 |
|
---|
811 | if (tmp == NULL) {
|
---|
812 | TALLOC_FREE(ea.value.data);
|
---|
813 | errno = ENOMEM;
|
---|
814 | return -1;
|
---|
815 | }
|
---|
816 | ea.value.data = tmp;
|
---|
817 | ea.value.length = offset + n + 1;
|
---|
818 | ea.value.data[offset+n] = 0;
|
---|
819 | }
|
---|
820 |
|
---|
821 | memcpy(ea.value.data + offset, data, n);
|
---|
822 |
|
---|
823 | if (fsp->base_fsp->fh->fd != -1) {
|
---|
824 | ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
|
---|
825 | sio->xattr_name,
|
---|
826 | ea.value.data, ea.value.length, 0);
|
---|
827 | } else {
|
---|
828 | ret = SMB_VFS_SETXATTR(fsp->conn, fsp->base_fsp->fsp_name,
|
---|
829 | sio->xattr_name,
|
---|
830 | ea.value.data, ea.value.length, 0);
|
---|
831 | }
|
---|
832 | TALLOC_FREE(ea.value.data);
|
---|
833 |
|
---|
834 | if (ret == -1) {
|
---|
835 | return -1;
|
---|
836 | }
|
---|
837 |
|
---|
838 | return n;
|
---|
839 | }
|
---|
840 |
|
---|
841 | static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
|
---|
842 | files_struct *fsp, void *data,
|
---|
843 | size_t n, SMB_OFF_T offset)
|
---|
844 | {
|
---|
845 | struct stream_io *sio =
|
---|
846 | (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
|
---|
847 | struct ea_struct ea;
|
---|
848 | NTSTATUS status;
|
---|
849 | size_t length, overlap;
|
---|
850 |
|
---|
851 | if (sio == NULL) {
|
---|
852 | return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
|
---|
853 | }
|
---|
854 |
|
---|
855 | if (!streams_xattr_recheck(sio)) {
|
---|
856 | return -1;
|
---|
857 | }
|
---|
858 |
|
---|
859 | status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
|
---|
860 | sio->base, sio->xattr_name, &ea);
|
---|
861 | if (!NT_STATUS_IS_OK(status)) {
|
---|
862 | return -1;
|
---|
863 | }
|
---|
864 |
|
---|
865 | length = ea.value.length-1;
|
---|
866 |
|
---|
867 | /* Attempt to read past EOF. */
|
---|
868 | if (length <= offset) {
|
---|
869 | errno = EINVAL;
|
---|
870 | return -1;
|
---|
871 | }
|
---|
872 |
|
---|
873 | overlap = (offset + n) > length ? (length - offset) : n;
|
---|
874 | memcpy(data, ea.value.data + offset, overlap);
|
---|
875 |
|
---|
876 | TALLOC_FREE(ea.value.data);
|
---|
877 | return overlap;
|
---|
878 | }
|
---|
879 |
|
---|
880 | static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
|
---|
881 | struct files_struct *fsp,
|
---|
882 | SMB_OFF_T offset)
|
---|
883 | {
|
---|
884 | int ret;
|
---|
885 | uint8 *tmp;
|
---|
886 | struct ea_struct ea;
|
---|
887 | NTSTATUS status;
|
---|
888 | struct stream_io *sio =
|
---|
889 | (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
|
---|
890 |
|
---|
891 | DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
|
---|
892 | fsp->fsp_name,
|
---|
893 | (double)offset ));
|
---|
894 |
|
---|
895 | if (sio == NULL) {
|
---|
896 | return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
|
---|
897 | }
|
---|
898 |
|
---|
899 | if (!streams_xattr_recheck(sio)) {
|
---|
900 | return -1;
|
---|
901 | }
|
---|
902 |
|
---|
903 | status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
|
---|
904 | sio->base, sio->xattr_name, &ea);
|
---|
905 | if (!NT_STATUS_IS_OK(status)) {
|
---|
906 | return -1;
|
---|
907 | }
|
---|
908 |
|
---|
909 | tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
|
---|
910 | offset + 1);
|
---|
911 |
|
---|
912 | if (tmp == NULL) {
|
---|
913 | TALLOC_FREE(ea.value.data);
|
---|
914 | errno = ENOMEM;
|
---|
915 | return -1;
|
---|
916 | }
|
---|
917 |
|
---|
918 | /* Did we expand ? */
|
---|
919 | if (ea.value.length < offset + 1) {
|
---|
920 | memset(&tmp[ea.value.length], '\0',
|
---|
921 | offset + 1 - ea.value.length);
|
---|
922 | }
|
---|
923 |
|
---|
924 | ea.value.data = tmp;
|
---|
925 | ea.value.length = offset + 1;
|
---|
926 | ea.value.data[offset] = 0;
|
---|
927 |
|
---|
928 | if (fsp->base_fsp->fh->fd != -1) {
|
---|
929 | ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
|
---|
930 | sio->xattr_name,
|
---|
931 | ea.value.data, ea.value.length, 0);
|
---|
932 | } else {
|
---|
933 | ret = SMB_VFS_SETXATTR(fsp->conn, fsp->base_fsp->fsp_name,
|
---|
934 | sio->xattr_name,
|
---|
935 | ea.value.data, ea.value.length, 0);
|
---|
936 | }
|
---|
937 |
|
---|
938 | TALLOC_FREE(ea.value.data);
|
---|
939 |
|
---|
940 | if (ret == -1) {
|
---|
941 | return -1;
|
---|
942 | }
|
---|
943 |
|
---|
944 | return 0;
|
---|
945 | }
|
---|
946 |
|
---|
947 | /* VFS operations structure */
|
---|
948 |
|
---|
949 | static vfs_op_tuple streams_xattr_ops[] = {
|
---|
950 | {SMB_VFS_OP(streams_xattr_fs_capabilities), SMB_VFS_OP_FS_CAPABILITIES,
|
---|
951 | SMB_VFS_LAYER_TRANSPARENT},
|
---|
952 | {SMB_VFS_OP(streams_xattr_open), SMB_VFS_OP_OPEN,
|
---|
953 | SMB_VFS_LAYER_TRANSPARENT},
|
---|
954 | {SMB_VFS_OP(streams_xattr_stat), SMB_VFS_OP_STAT,
|
---|
955 | SMB_VFS_LAYER_TRANSPARENT},
|
---|
956 | {SMB_VFS_OP(streams_xattr_fstat), SMB_VFS_OP_FSTAT,
|
---|
957 | SMB_VFS_LAYER_TRANSPARENT},
|
---|
958 | {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
|
---|
959 | SMB_VFS_LAYER_TRANSPARENT},
|
---|
960 | {SMB_VFS_OP(streams_xattr_pread), SMB_VFS_OP_PREAD,
|
---|
961 | SMB_VFS_LAYER_TRANSPARENT},
|
---|
962 | {SMB_VFS_OP(streams_xattr_pwrite), SMB_VFS_OP_PWRITE,
|
---|
963 | SMB_VFS_LAYER_TRANSPARENT},
|
---|
964 | {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
|
---|
965 | SMB_VFS_LAYER_TRANSPARENT},
|
---|
966 | {SMB_VFS_OP(streams_xattr_unlink), SMB_VFS_OP_UNLINK,
|
---|
967 | SMB_VFS_LAYER_TRANSPARENT},
|
---|
968 | {SMB_VFS_OP(streams_xattr_rename), SMB_VFS_OP_RENAME,
|
---|
969 | SMB_VFS_LAYER_TRANSPARENT},
|
---|
970 | {SMB_VFS_OP(streams_xattr_ftruncate), SMB_VFS_OP_FTRUNCATE,
|
---|
971 | SMB_VFS_LAYER_TRANSPARENT},
|
---|
972 | {SMB_VFS_OP(streams_xattr_streaminfo), SMB_VFS_OP_STREAMINFO,
|
---|
973 | SMB_VFS_LAYER_OPAQUE},
|
---|
974 | {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
|
---|
975 | };
|
---|
976 |
|
---|
977 | NTSTATUS vfs_streams_xattr_init(void);
|
---|
978 | NTSTATUS vfs_streams_xattr_init(void)
|
---|
979 | {
|
---|
980 | return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
|
---|
981 | streams_xattr_ops);
|
---|
982 | }
|
---|