examples/VFS: fix skel_transparent.c in reference to shadow_copy changes
[Samba/gebeck_regimport.git] / source3 / libsmb / libsmb_file.c
blobc822eab79850dfe43ee73124cd666686613571b5
1 /*
2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003-2008
9 Copyright (C) Jeremy Allison 2007, 2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "libsmb/libsmb.h"
27 #include "libsmbclient.h"
28 #include "libsmb_internal.h"
32 * Routine to open() a file ...
35 SMBCFILE *
36 SMBC_open_ctx(SMBCCTX *context,
37 const char *fname,
38 int flags,
39 mode_t mode)
41 char *server = NULL;
42 char *share = NULL;
43 char *user = NULL;
44 char *password = NULL;
45 char *workgroup = NULL;
46 char *path = NULL;
47 char *targetpath = NULL;
48 struct cli_state *targetcli = NULL;
49 SMBCSRV *srv = NULL;
50 SMBCFILE *file = NULL;
51 uint16_t fd;
52 NTSTATUS status = NT_STATUS_OBJECT_PATH_INVALID;
53 TALLOC_CTX *frame = talloc_stackframe();
55 if (!context || !context->internal->initialized) {
56 errno = EINVAL; /* Best I can think of ... */
57 TALLOC_FREE(frame);
58 return NULL;
61 if (!fname) {
62 errno = EINVAL;
63 TALLOC_FREE(frame);
64 return NULL;
67 if (SMBC_parse_path(frame,
68 context,
69 fname,
70 &workgroup,
71 &server,
72 &share,
73 &path,
74 &user,
75 &password,
76 NULL)) {
77 errno = EINVAL;
78 TALLOC_FREE(frame);
79 return NULL;
82 if (!user || user[0] == (char)0) {
83 user = talloc_strdup(frame, smbc_getUser(context));
84 if (!user) {
85 errno = ENOMEM;
86 TALLOC_FREE(frame);
87 return NULL;
91 srv = SMBC_server(frame, context, True,
92 server, share, &workgroup, &user, &password);
93 if (!srv) {
94 if (errno == EPERM) errno = EACCES;
95 TALLOC_FREE(frame);
96 return NULL; /* SMBC_server sets errno */
99 /* Hmmm, the test for a directory is suspect here ... FIXME */
101 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
102 status = NT_STATUS_OBJECT_PATH_INVALID;
103 } else {
104 file = SMB_MALLOC_P(SMBCFILE);
105 if (!file) {
106 errno = ENOMEM;
107 TALLOC_FREE(frame);
108 return NULL;
111 ZERO_STRUCTP(file);
113 /*d_printf(">>>open: resolving %s\n", path);*/
114 if (!cli_resolve_path(frame, "", context->internal->auth_info,
115 srv->cli, path,
116 &targetcli, &targetpath)) {
117 d_printf("Could not resolve %s\n", path);
118 errno = ENOENT;
119 SAFE_FREE(file);
120 TALLOC_FREE(frame);
121 return NULL;
123 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
125 status = cli_open(targetcli, targetpath, flags,
126 context->internal->share_mode, &fd);
127 if (!NT_STATUS_IS_OK(status)) {
129 /* Handle the error ... */
131 SAFE_FREE(file);
132 errno = SMBC_errno(context, targetcli);
133 TALLOC_FREE(frame);
134 return NULL;
137 /* Fill in file struct */
139 file->cli_fd = fd;
140 file->fname = SMB_STRDUP(fname);
141 file->srv = srv;
142 file->offset = 0;
143 file->file = True;
145 DLIST_ADD(context->internal->files, file);
148 * If the file was opened in O_APPEND mode, all write
149 * operations should be appended to the file. To do that,
150 * though, using this protocol, would require a getattrE()
151 * call for each and every write, to determine where the end
152 * of the file is. (There does not appear to be an append flag
153 * in the protocol.) Rather than add all of that overhead of
154 * retrieving the current end-of-file offset prior to each
155 * write operation, we'll assume that most append operations
156 * will continuously write, so we'll just set the offset to
157 * the end of the file now and hope that's adequate.
159 * Note to self: If this proves inadequate, and O_APPEND
160 * should, in some cases, be forced for each write, add a
161 * field in the context options structure, for
162 * "strict_append_mode" which would select between the current
163 * behavior (if FALSE) or issuing a getattrE() prior to each
164 * write and forcing the write to the end of the file (if
165 * TRUE). Adding that capability will likely require adding
166 * an "append" flag into the _SMBCFILE structure to track
167 * whether a file was opened in O_APPEND mode. -- djl
169 if (flags & O_APPEND) {
170 if (SMBC_lseek_ctx(context, file, 0, SEEK_END) < 0) {
171 (void) SMBC_close_ctx(context, file);
172 errno = ENXIO;
173 TALLOC_FREE(frame);
174 return NULL;
178 TALLOC_FREE(frame);
179 return file;
182 /* Check if opendir needed ... */
184 if (!NT_STATUS_IS_OK(status)) {
185 int eno = 0;
187 eno = SMBC_errno(context, srv->cli);
188 file = smbc_getFunctionOpendir(context)(context, fname);
189 if (!file) errno = eno;
190 TALLOC_FREE(frame);
191 return file;
194 errno = EINVAL; /* FIXME, correct errno ? */
195 TALLOC_FREE(frame);
196 return NULL;
200 * Routine to create a file
203 SMBCFILE *
204 SMBC_creat_ctx(SMBCCTX *context,
205 const char *path,
206 mode_t mode)
208 if (!context || !context->internal->initialized) {
209 errno = EINVAL;
210 return NULL;
213 return SMBC_open_ctx(context, path,
214 O_WRONLY | O_CREAT | O_TRUNC, mode);
218 * Routine to read() a file ...
221 ssize_t
222 SMBC_read_ctx(SMBCCTX *context,
223 SMBCFILE *file,
224 void *buf,
225 size_t count)
227 int ret;
228 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
229 char *path = NULL;
230 char *targetpath = NULL;
231 struct cli_state *targetcli = NULL;
232 TALLOC_CTX *frame = talloc_stackframe();
235 * offset:
237 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
238 * appears to pass file->offset (which is type off_t) differently than
239 * a local variable of type off_t. Using local variable "offset" in
240 * the call to cli_read() instead of file->offset fixes a problem
241 * retrieving data at an offset greater than 4GB.
243 off_t offset;
245 if (!context || !context->internal->initialized) {
246 errno = EINVAL;
247 TALLOC_FREE(frame);
248 return -1;
251 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
253 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
254 errno = EBADF;
255 TALLOC_FREE(frame);
256 return -1;
259 offset = file->offset;
261 /* Check that the buffer exists ... */
263 if (buf == NULL) {
264 errno = EINVAL;
265 TALLOC_FREE(frame);
266 return -1;
269 /*d_printf(">>>read: parsing %s\n", file->fname);*/
270 if (SMBC_parse_path(frame,
271 context,
272 file->fname,
273 NULL,
274 &server,
275 &share,
276 &path,
277 &user,
278 &password,
279 NULL)) {
280 errno = EINVAL;
281 TALLOC_FREE(frame);
282 return -1;
285 /*d_printf(">>>read: resolving %s\n", path);*/
286 if (!cli_resolve_path(frame, "", context->internal->auth_info,
287 file->srv->cli, path,
288 &targetcli, &targetpath)) {
289 d_printf("Could not resolve %s\n", path);
290 errno = ENOENT;
291 TALLOC_FREE(frame);
292 return -1;
294 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
296 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
298 if (ret < 0) {
299 errno = SMBC_errno(context, targetcli);
300 TALLOC_FREE(frame);
301 return -1;
304 file->offset += ret;
306 DEBUG(4, (" --> %d\n", ret));
308 TALLOC_FREE(frame);
309 return ret; /* Success, ret bytes of data ... */
313 * Routine to write() a file ...
316 ssize_t
317 SMBC_write_ctx(SMBCCTX *context,
318 SMBCFILE *file,
319 const void *buf,
320 size_t count)
322 off_t offset;
323 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
324 char *path = NULL;
325 char *targetpath = NULL;
326 struct cli_state *targetcli = NULL;
327 TALLOC_CTX *frame = talloc_stackframe();
328 NTSTATUS status;
330 /* First check all pointers before dereferencing them */
332 if (!context || !context->internal->initialized) {
333 errno = EINVAL;
334 TALLOC_FREE(frame);
335 return -1;
338 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
339 errno = EBADF;
340 TALLOC_FREE(frame);
341 return -1;
344 /* Check that the buffer exists ... */
346 if (buf == NULL) {
347 errno = EINVAL;
348 TALLOC_FREE(frame);
349 return -1;
352 offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */
354 /*d_printf(">>>write: parsing %s\n", file->fname);*/
355 if (SMBC_parse_path(frame,
356 context,
357 file->fname,
358 NULL,
359 &server,
360 &share,
361 &path,
362 &user,
363 &password,
364 NULL)) {
365 errno = EINVAL;
366 TALLOC_FREE(frame);
367 return -1;
370 /*d_printf(">>>write: resolving %s\n", path);*/
371 if (!cli_resolve_path(frame, "", context->internal->auth_info,
372 file->srv->cli, path,
373 &targetcli, &targetpath)) {
374 d_printf("Could not resolve %s\n", path);
375 errno = ENOENT;
376 TALLOC_FREE(frame);
377 return -1;
379 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
381 status = cli_writeall(targetcli, file->cli_fd,
382 0, (const uint8_t *)buf, offset, count, NULL);
383 if (!NT_STATUS_IS_OK(status)) {
384 errno = map_errno_from_nt_status(status);
385 TALLOC_FREE(frame);
386 return -1;
389 file->offset += count;
391 TALLOC_FREE(frame);
392 return count; /* Success, 0 bytes of data ... */
396 * Routine to close() a file ...
400 SMBC_close_ctx(SMBCCTX *context,
401 SMBCFILE *file)
403 SMBCSRV *srv;
404 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
405 char *path = NULL;
406 char *targetpath = NULL;
407 struct cli_state *targetcli = NULL;
408 TALLOC_CTX *frame = talloc_stackframe();
410 if (!context || !context->internal->initialized) {
411 errno = EINVAL;
412 TALLOC_FREE(frame);
413 return -1;
416 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
417 errno = EBADF;
418 TALLOC_FREE(frame);
419 return -1;
422 /* IS a dir ... */
423 if (!file->file) {
424 TALLOC_FREE(frame);
425 return smbc_getFunctionClosedir(context)(context, file);
428 /*d_printf(">>>close: parsing %s\n", file->fname);*/
429 if (SMBC_parse_path(frame,
430 context,
431 file->fname,
432 NULL,
433 &server,
434 &share,
435 &path,
436 &user,
437 &password,
438 NULL)) {
439 errno = EINVAL;
440 TALLOC_FREE(frame);
441 return -1;
444 /*d_printf(">>>close: resolving %s\n", path);*/
445 if (!cli_resolve_path(frame, "", context->internal->auth_info,
446 file->srv->cli, path,
447 &targetcli, &targetpath)) {
448 d_printf("Could not resolve %s\n", path);
449 errno = ENOENT;
450 TALLOC_FREE(frame);
451 return -1;
453 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
455 if (!NT_STATUS_IS_OK(cli_close(targetcli, file->cli_fd))) {
456 DEBUG(3, ("cli_close failed on %s. purging server.\n",
457 file->fname));
458 /* Deallocate slot and remove the server
459 * from the server cache if unused */
460 errno = SMBC_errno(context, targetcli);
461 srv = file->srv;
462 DLIST_REMOVE(context->internal->files, file);
463 SAFE_FREE(file->fname);
464 SAFE_FREE(file);
465 smbc_getFunctionRemoveUnusedServer(context)(context, srv);
466 TALLOC_FREE(frame);
467 return -1;
470 DLIST_REMOVE(context->internal->files, file);
471 SAFE_FREE(file->fname);
472 SAFE_FREE(file);
473 TALLOC_FREE(frame);
474 return 0;
478 * Get info from an SMB server on a file. Use a qpathinfo call first
479 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
481 bool
482 SMBC_getatr(SMBCCTX * context,
483 SMBCSRV *srv,
484 const char *path,
485 uint16 *mode,
486 SMB_OFF_T *size,
487 struct timespec *create_time_ts,
488 struct timespec *access_time_ts,
489 struct timespec *write_time_ts,
490 struct timespec *change_time_ts,
491 SMB_INO_T *ino)
493 char *fixedpath = NULL;
494 char *targetpath = NULL;
495 struct cli_state *targetcli = NULL;
496 time_t write_time;
497 TALLOC_CTX *frame = talloc_stackframe();
499 if (!context || !context->internal->initialized) {
500 errno = EINVAL;
501 TALLOC_FREE(frame);
502 return False;
505 /* path fixup for . and .. */
506 if (strequal(path, ".") || strequal(path, "..")) {
507 fixedpath = talloc_strdup(frame, "\\");
508 if (!fixedpath) {
509 errno = ENOMEM;
510 TALLOC_FREE(frame);
511 return False;
513 } else {
514 fixedpath = talloc_strdup(frame, path);
515 if (!fixedpath) {
516 errno = ENOMEM;
517 TALLOC_FREE(frame);
518 return False;
520 trim_string(fixedpath, NULL, "\\..");
521 trim_string(fixedpath, NULL, "\\.");
523 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
525 if (!cli_resolve_path(frame, "", context->internal->auth_info,
526 srv->cli, fixedpath,
527 &targetcli, &targetpath)) {
528 d_printf("Couldn't resolve %s\n", path);
529 errno = ENOENT;
530 TALLOC_FREE(frame);
531 return False;
534 if (!srv->no_pathinfo2 &&
535 NT_STATUS_IS_OK(cli_qpathinfo2(targetcli, targetpath,
536 create_time_ts,
537 access_time_ts,
538 write_time_ts,
539 change_time_ts,
540 size, mode, ino))) {
541 TALLOC_FREE(frame);
542 return True;
545 /* if this is NT then don't bother with the getatr */
546 if (targetcli->capabilities & CAP_NT_SMBS) {
547 errno = EPERM;
548 TALLOC_FREE(frame);
549 return False;
552 if (NT_STATUS_IS_OK(cli_getatr(targetcli, targetpath, mode, size, &write_time))) {
553 struct timespec w_time_ts;
555 w_time_ts = convert_time_t_to_timespec(write_time);
556 if (write_time_ts != NULL) {
557 *write_time_ts = w_time_ts;
559 if (create_time_ts != NULL) {
560 *create_time_ts = w_time_ts;
562 if (access_time_ts != NULL) {
563 *access_time_ts = w_time_ts;
565 if (change_time_ts != NULL) {
566 *change_time_ts = w_time_ts;
568 srv->no_pathinfo2 = True;
569 TALLOC_FREE(frame);
570 return True;
573 errno = EPERM;
574 TALLOC_FREE(frame);
575 return False;
579 * Set file info on an SMB server. Use setpathinfo call first. If that
580 * fails, use setattrE..
582 * Access and modification time parameters are always used and must be
583 * provided. Create time, if zero, will be determined from the actual create
584 * time of the file. If non-zero, the create time will be set as well.
586 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
588 bool
589 SMBC_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
590 time_t create_time,
591 time_t access_time,
592 time_t write_time,
593 time_t change_time,
594 uint16 mode)
596 uint16_t fd;
597 int ret;
598 TALLOC_CTX *frame = talloc_stackframe();
601 * First, try setpathinfo (if qpathinfo succeeded), for it is the
602 * modern function for "new code" to be using, and it works given a
603 * filename rather than requiring that the file be opened to have its
604 * attributes manipulated.
606 if (srv->no_pathinfo ||
607 !NT_STATUS_IS_OK(cli_setpathinfo_basic(srv->cli, path,
608 create_time,
609 access_time,
610 write_time,
611 change_time,
612 mode))) {
615 * setpathinfo is not supported; go to plan B.
617 * cli_setatr() does not work on win98, and it also doesn't
618 * support setting the access time (only the modification
619 * time), so in all cases, we open the specified file and use
620 * cli_setattrE() which should work on all OS versions, and
621 * supports both times.
624 /* Don't try {q,set}pathinfo() again, with this server */
625 srv->no_pathinfo = True;
627 /* Open the file */
628 if (!NT_STATUS_IS_OK(cli_open(srv->cli, path, O_RDWR, DENY_NONE, &fd))) {
629 errno = SMBC_errno(context, srv->cli);
630 TALLOC_FREE(frame);
631 return -1;
634 /* Set the new attributes */
635 ret = NT_STATUS_IS_OK(cli_setattrE(srv->cli, fd,
636 change_time,
637 access_time,
638 write_time));
640 /* Close the file */
641 cli_close(srv->cli, fd);
644 * Unfortunately, setattrE() doesn't have a provision for
645 * setting the access mode (attributes). We'll have to try
646 * cli_setatr() for that, and with only this parameter, it
647 * seems to work on win98.
649 if (ret && mode != (uint16) -1) {
650 ret = NT_STATUS_IS_OK(cli_setatr(srv->cli, path, mode, 0));
653 if (! ret) {
654 errno = SMBC_errno(context, srv->cli);
655 TALLOC_FREE(frame);
656 return False;
660 TALLOC_FREE(frame);
661 return True;
665 * A routine to lseek() a file
668 off_t
669 SMBC_lseek_ctx(SMBCCTX *context,
670 SMBCFILE *file,
671 off_t offset,
672 int whence)
674 SMB_OFF_T size;
675 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
676 char *path = NULL;
677 char *targetpath = NULL;
678 struct cli_state *targetcli = NULL;
679 TALLOC_CTX *frame = talloc_stackframe();
681 if (!context || !context->internal->initialized) {
682 errno = EINVAL;
683 TALLOC_FREE(frame);
684 return -1;
687 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
688 errno = EBADF;
689 TALLOC_FREE(frame);
690 return -1;
693 if (!file->file) {
694 errno = EINVAL;
695 TALLOC_FREE(frame);
696 return -1; /* Can't lseek a dir ... */
699 switch (whence) {
700 case SEEK_SET:
701 file->offset = offset;
702 break;
703 case SEEK_CUR:
704 file->offset += offset;
705 break;
706 case SEEK_END:
707 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
708 if (SMBC_parse_path(frame,
709 context,
710 file->fname,
711 NULL,
712 &server,
713 &share,
714 &path,
715 &user,
716 &password,
717 NULL)) {
718 errno = EINVAL;
719 TALLOC_FREE(frame);
720 return -1;
723 /*d_printf(">>>lseek: resolving %s\n", path);*/
724 if (!cli_resolve_path(frame, "", context->internal->auth_info,
725 file->srv->cli, path,
726 &targetcli, &targetpath)) {
727 d_printf("Could not resolve %s\n", path);
728 errno = ENOENT;
729 TALLOC_FREE(frame);
730 return -1;
733 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
734 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
735 targetcli, file->cli_fd, NULL,
736 &size, NULL, NULL, NULL, NULL,
737 NULL))) {
738 SMB_OFF_T b_size = size;
739 if (!NT_STATUS_IS_OK(cli_getattrE(targetcli, file->cli_fd,
740 NULL, &b_size, NULL, NULL, NULL))) {
741 errno = EINVAL;
742 TALLOC_FREE(frame);
743 return -1;
744 } else
745 size = b_size;
747 file->offset = size + offset;
748 break;
749 default:
750 errno = EINVAL;
751 break;
754 TALLOC_FREE(frame);
755 return file->offset;
760 * Routine to truncate a file given by its file descriptor, to a specified size
764 SMBC_ftruncate_ctx(SMBCCTX *context,
765 SMBCFILE *file,
766 off_t length)
768 SMB_OFF_T size = length;
769 char *server = NULL;
770 char *share = NULL;
771 char *user = NULL;
772 char *password = NULL;
773 char *path = NULL;
774 char *targetpath = NULL;
775 struct cli_state *targetcli = NULL;
776 TALLOC_CTX *frame = talloc_stackframe();
778 if (!context || !context->internal->initialized) {
779 errno = EINVAL;
780 TALLOC_FREE(frame);
781 return -1;
784 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
785 errno = EBADF;
786 TALLOC_FREE(frame);
787 return -1;
790 if (!file->file) {
791 errno = EINVAL;
792 TALLOC_FREE(frame);
793 return -1;
796 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
797 if (SMBC_parse_path(frame,
798 context,
799 file->fname,
800 NULL,
801 &server,
802 &share,
803 &path,
804 &user,
805 &password,
806 NULL)) {
807 errno = EINVAL;
808 TALLOC_FREE(frame);
809 return -1;
812 /*d_printf(">>>fstat: resolving %s\n", path);*/
813 if (!cli_resolve_path(frame, "", context->internal->auth_info,
814 file->srv->cli, path,
815 &targetcli, &targetpath)) {
816 d_printf("Could not resolve %s\n", path);
817 errno = ENOENT;
818 TALLOC_FREE(frame);
819 return -1;
821 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
823 if (!NT_STATUS_IS_OK(cli_ftruncate(targetcli, file->cli_fd, (uint64_t)size))) {
824 errno = EINVAL;
825 TALLOC_FREE(frame);
826 return -1;
829 TALLOC_FREE(frame);
830 return 0;