WHATSNEW: Update changes since 3.3.0.
[Samba.git] / source / libsmb / libsmb_file.c
blobe162aaa16b75a6c4fc534114590b3bac2b8c13f0
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 "libsmbclient.h"
27 #include "libsmb_internal.h"
31 * Routine to open() a file ...
34 SMBCFILE *
35 SMBC_open_ctx(SMBCCTX *context,
36 const char *fname,
37 int flags,
38 mode_t mode)
40 char *server = NULL;
41 char *share = NULL;
42 char *user = NULL;
43 char *password = NULL;
44 char *workgroup = NULL;
45 char *path = NULL;
46 char *targetpath = NULL;
47 struct cli_state *targetcli = NULL;
48 SMBCSRV *srv = NULL;
49 SMBCFILE *file = NULL;
50 int fd;
51 TALLOC_CTX *frame = talloc_stackframe();
53 if (!context || !context->internal->initialized) {
55 errno = EINVAL; /* Best I can think of ... */
56 TALLOC_FREE(frame);
57 return NULL;
61 if (!fname) {
63 errno = EINVAL;
64 TALLOC_FREE(frame);
65 return NULL;
69 if (SMBC_parse_path(frame,
70 context,
71 fname,
72 &workgroup,
73 &server,
74 &share,
75 &path,
76 &user,
77 &password,
78 NULL)) {
79 errno = EINVAL;
80 TALLOC_FREE(frame);
81 return NULL;
84 if (!user || user[0] == (char)0) {
85 user = talloc_strdup(frame, smbc_getUser(context));
86 if (!user) {
87 errno = ENOMEM;
88 TALLOC_FREE(frame);
89 return NULL;
93 srv = SMBC_server(frame, context, True,
94 server, share, &workgroup, &user, &password);
96 if (!srv) {
97 if (errno == EPERM) errno = EACCES;
98 TALLOC_FREE(frame);
99 return NULL; /* SMBC_server sets errno */
102 /* Hmmm, the test for a directory is suspect here ... FIXME */
104 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
105 fd = -1;
106 } else {
107 file = SMB_MALLOC_P(SMBCFILE);
109 if (!file) {
110 errno = ENOMEM;
111 TALLOC_FREE(frame);
112 return NULL;
115 ZERO_STRUCTP(file);
117 /*d_printf(">>>open: resolving %s\n", path);*/
118 if (!cli_resolve_path(frame, "", srv->cli, path,
119 &targetcli, &targetpath)) {
120 d_printf("Could not resolve %s\n", path);
121 SAFE_FREE(file);
122 TALLOC_FREE(frame);
123 return NULL;
125 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
127 if ((fd = cli_open(targetcli, targetpath, flags,
128 context->internal->share_mode)) < 0) {
130 /* Handle the error ... */
132 SAFE_FREE(file);
133 errno = SMBC_errno(context, targetcli);
134 TALLOC_FREE(frame);
135 return NULL;
139 /* Fill in file struct */
141 file->cli_fd = fd;
142 file->fname = SMB_STRDUP(fname);
143 file->srv = srv;
144 file->offset = 0;
145 file->file = True;
147 DLIST_ADD(context->internal->files, file);
150 * If the file was opened in O_APPEND mode, all write
151 * operations should be appended to the file. To do that,
152 * though, using this protocol, would require a getattrE()
153 * call for each and every write, to determine where the end
154 * of the file is. (There does not appear to be an append flag
155 * in the protocol.) Rather than add all of that overhead of
156 * retrieving the current end-of-file offset prior to each
157 * write operation, we'll assume that most append operations
158 * will continuously write, so we'll just set the offset to
159 * the end of the file now and hope that's adequate.
161 * Note to self: If this proves inadequate, and O_APPEND
162 * should, in some cases, be forced for each write, add a
163 * field in the context options structure, for
164 * "strict_append_mode" which would select between the current
165 * behavior (if FALSE) or issuing a getattrE() prior to each
166 * write and forcing the write to the end of the file (if
167 * TRUE). Adding that capability will likely require adding
168 * an "append" flag into the _SMBCFILE structure to track
169 * whether a file was opened in O_APPEND mode. -- djl
171 if (flags & O_APPEND) {
172 if (SMBC_lseek_ctx(context, file, 0, SEEK_END) < 0) {
173 (void) SMBC_close_ctx(context, file);
174 errno = ENXIO;
175 TALLOC_FREE(frame);
176 return NULL;
180 TALLOC_FREE(frame);
181 return file;
185 /* Check if opendir needed ... */
187 if (fd == -1) {
188 int eno = 0;
190 eno = SMBC_errno(context, srv->cli);
191 file = smbc_getFunctionOpendir(context)(context, fname);
192 if (!file) errno = eno;
193 TALLOC_FREE(frame);
194 return file;
198 errno = EINVAL; /* FIXME, correct errno ? */
199 TALLOC_FREE(frame);
200 return NULL;
205 * Routine to create a file
208 SMBCFILE *
209 SMBC_creat_ctx(SMBCCTX *context,
210 const char *path,
211 mode_t mode)
214 if (!context || !context->internal->initialized) {
216 errno = EINVAL;
217 return NULL;
221 return SMBC_open_ctx(context, path,
222 O_WRONLY | O_CREAT | O_TRUNC, mode);
226 * Routine to read() a file ...
229 ssize_t
230 SMBC_read_ctx(SMBCCTX *context,
231 SMBCFILE *file,
232 void *buf,
233 size_t count)
235 int ret;
236 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
237 char *path = NULL;
238 char *targetpath = NULL;
239 struct cli_state *targetcli = NULL;
240 TALLOC_CTX *frame = talloc_stackframe();
243 * offset:
245 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
246 * appears to pass file->offset (which is type off_t) differently than
247 * a local variable of type off_t. Using local variable "offset" in
248 * the call to cli_read() instead of file->offset fixes a problem
249 * retrieving data at an offset greater than 4GB.
251 off_t offset;
253 if (!context || !context->internal->initialized) {
255 errno = EINVAL;
256 TALLOC_FREE(frame);
257 return -1;
261 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
263 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
264 errno = EBADF;
265 TALLOC_FREE(frame);
266 return -1;
270 offset = file->offset;
272 /* Check that the buffer exists ... */
274 if (buf == NULL) {
275 errno = EINVAL;
276 TALLOC_FREE(frame);
277 return -1;
281 /*d_printf(">>>read: parsing %s\n", file->fname);*/
282 if (SMBC_parse_path(frame,
283 context,
284 file->fname,
285 NULL,
286 &server,
287 &share,
288 &path,
289 &user,
290 &password,
291 NULL)) {
292 errno = EINVAL;
293 TALLOC_FREE(frame);
294 return -1;
297 /*d_printf(">>>read: resolving %s\n", path);*/
298 if (!cli_resolve_path(frame, "", file->srv->cli, path,
299 &targetcli, &targetpath)) {
300 d_printf("Could not resolve %s\n", path);
301 TALLOC_FREE(frame);
302 return -1;
304 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
306 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
308 if (ret < 0) {
310 errno = SMBC_errno(context, targetcli);
311 TALLOC_FREE(frame);
312 return -1;
316 file->offset += ret;
318 DEBUG(4, (" --> %d\n", ret));
320 TALLOC_FREE(frame);
321 return ret; /* Success, ret bytes of data ... */
326 * Routine to write() a file ...
329 ssize_t
330 SMBC_write_ctx(SMBCCTX *context,
331 SMBCFILE *file,
332 const void *buf,
333 size_t count)
335 int ret;
336 off_t offset;
337 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
338 char *path = NULL;
339 char *targetpath = NULL;
340 struct cli_state *targetcli = NULL;
341 TALLOC_CTX *frame = talloc_stackframe();
343 /* First check all pointers before dereferencing them */
345 if (!context || !context->internal->initialized) {
347 errno = EINVAL;
348 TALLOC_FREE(frame);
349 return -1;
353 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
354 errno = EBADF;
355 TALLOC_FREE(frame);
356 return -1;
359 /* Check that the buffer exists ... */
361 if (buf == NULL) {
362 errno = EINVAL;
363 TALLOC_FREE(frame);
364 return -1;
368 offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */
370 /*d_printf(">>>write: parsing %s\n", file->fname);*/
371 if (SMBC_parse_path(frame,
372 context,
373 file->fname,
374 NULL,
375 &server,
376 &share,
377 &path,
378 &user,
379 &password,
380 NULL)) {
381 errno = EINVAL;
382 TALLOC_FREE(frame);
383 return -1;
386 /*d_printf(">>>write: resolving %s\n", path);*/
387 if (!cli_resolve_path(frame, "", file->srv->cli, path,
388 &targetcli, &targetpath)) {
389 d_printf("Could not resolve %s\n", path);
390 TALLOC_FREE(frame);
391 return -1;
393 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
395 ret = cli_write(targetcli, file->cli_fd,
396 0, (char *)buf, offset, count);
398 if (ret <= 0) {
399 errno = SMBC_errno(context, targetcli);
400 TALLOC_FREE(frame);
401 return -1;
405 file->offset += ret;
407 TALLOC_FREE(frame);
408 return ret; /* Success, 0 bytes of data ... */
412 * Routine to close() a file ...
416 SMBC_close_ctx(SMBCCTX *context,
417 SMBCFILE *file)
419 SMBCSRV *srv;
420 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
421 char *path = NULL;
422 char *targetpath = NULL;
423 struct cli_state *targetcli = NULL;
424 TALLOC_CTX *frame = talloc_stackframe();
426 if (!context || !context->internal->initialized) {
428 errno = EINVAL;
429 TALLOC_FREE(frame);
430 return -1;
433 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
434 errno = EBADF;
435 TALLOC_FREE(frame);
436 return -1;
439 /* IS a dir ... */
440 if (!file->file) {
441 TALLOC_FREE(frame);
442 return smbc_getFunctionClosedir(context)(context, file);
445 /*d_printf(">>>close: parsing %s\n", file->fname);*/
446 if (SMBC_parse_path(frame,
447 context,
448 file->fname,
449 NULL,
450 &server,
451 &share,
452 &path,
453 &user,
454 &password,
455 NULL)) {
456 errno = EINVAL;
457 TALLOC_FREE(frame);
458 return -1;
461 /*d_printf(">>>close: resolving %s\n", path);*/
462 if (!cli_resolve_path(frame, "", file->srv->cli, path,
463 &targetcli, &targetpath)) {
464 d_printf("Could not resolve %s\n", path);
465 TALLOC_FREE(frame);
466 return -1;
468 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
470 if (!cli_close(targetcli, file->cli_fd)) {
472 DEBUG(3, ("cli_close failed on %s. purging server.\n",
473 file->fname));
474 /* Deallocate slot and remove the server
475 * from the server cache if unused */
476 errno = SMBC_errno(context, targetcli);
477 srv = file->srv;
478 DLIST_REMOVE(context->internal->files, file);
479 SAFE_FREE(file->fname);
480 SAFE_FREE(file);
481 smbc_getFunctionRemoveUnusedServer(context)(context, srv);
482 TALLOC_FREE(frame);
483 return -1;
487 DLIST_REMOVE(context->internal->files, file);
488 SAFE_FREE(file->fname);
489 SAFE_FREE(file);
490 TALLOC_FREE(frame);
492 return 0;
496 * Get info from an SMB server on a file. Use a qpathinfo call first
497 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
499 bool
500 SMBC_getatr(SMBCCTX * context,
501 SMBCSRV *srv,
502 char *path,
503 uint16 *mode,
504 SMB_OFF_T *size,
505 struct timespec *create_time_ts,
506 struct timespec *access_time_ts,
507 struct timespec *write_time_ts,
508 struct timespec *change_time_ts,
509 SMB_INO_T *ino)
511 char *fixedpath = NULL;
512 char *targetpath = NULL;
513 struct cli_state *targetcli = NULL;
514 time_t write_time;
515 TALLOC_CTX *frame = talloc_stackframe();
517 if (!context || !context->internal->initialized) {
519 errno = EINVAL;
520 TALLOC_FREE(frame);
521 return false;
524 /* path fixup for . and .. */
525 if (strequal(path, ".") || strequal(path, "..")) {
526 fixedpath = talloc_strdup(frame, "\\");
527 if (!fixedpath) {
528 errno = ENOMEM;
529 TALLOC_FREE(frame);
530 return false;
532 } else {
533 fixedpath = talloc_strdup(frame, path);
534 if (!fixedpath) {
535 errno = ENOMEM;
536 TALLOC_FREE(frame);
537 return false;
539 trim_string(fixedpath, NULL, "\\..");
540 trim_string(fixedpath, NULL, "\\.");
542 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
544 if (!cli_resolve_path(frame, "", srv->cli, fixedpath,
545 &targetcli, &targetpath)) {
546 d_printf("Couldn't resolve %s\n", path);
547 TALLOC_FREE(frame);
548 return False;
551 if (!srv->no_pathinfo2 &&
552 cli_qpathinfo2(targetcli, targetpath,
553 create_time_ts,
554 access_time_ts,
555 write_time_ts,
556 change_time_ts,
557 size, mode, ino)) {
558 TALLOC_FREE(frame);
559 return True;
562 /* if this is NT then don't bother with the getatr */
563 if (targetcli->capabilities & CAP_NT_SMBS) {
564 errno = EPERM;
565 TALLOC_FREE(frame);
566 return False;
569 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
571 struct timespec w_time_ts;
573 w_time_ts = convert_time_t_to_timespec(write_time);
575 if (write_time_ts != NULL) {
576 *write_time_ts = w_time_ts;
579 if (create_time_ts != NULL) {
580 *create_time_ts = w_time_ts;
583 if (access_time_ts != NULL) {
584 *access_time_ts = w_time_ts;
587 if (change_time_ts != NULL) {
588 *change_time_ts = w_time_ts;
591 srv->no_pathinfo2 = True;
592 TALLOC_FREE(frame);
593 return True;
596 errno = EPERM;
597 TALLOC_FREE(frame);
598 return False;
603 * Set file info on an SMB server. Use setpathinfo call first. If that
604 * fails, use setattrE..
606 * Access and modification time parameters are always used and must be
607 * provided. Create time, if zero, will be determined from the actual create
608 * time of the file. If non-zero, the create time will be set as well.
610 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
612 bool
613 SMBC_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
614 time_t create_time,
615 time_t access_time,
616 time_t write_time,
617 time_t change_time,
618 uint16 mode)
620 int fd;
621 int ret;
622 TALLOC_CTX *frame = talloc_stackframe();
625 * First, try setpathinfo (if qpathinfo succeeded), for it is the
626 * modern function for "new code" to be using, and it works given a
627 * filename rather than requiring that the file be opened to have its
628 * attributes manipulated.
630 if (srv->no_pathinfo ||
631 ! cli_setpathinfo(srv->cli, path,
632 create_time,
633 access_time,
634 write_time,
635 change_time,
636 mode)) {
639 * setpathinfo is not supported; go to plan B.
641 * cli_setatr() does not work on win98, and it also doesn't
642 * support setting the access time (only the modification
643 * time), so in all cases, we open the specified file and use
644 * cli_setattrE() which should work on all OS versions, and
645 * supports both times.
648 /* Don't try {q,set}pathinfo() again, with this server */
649 srv->no_pathinfo = True;
651 /* Open the file */
652 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
654 errno = SMBC_errno(context, srv->cli);
655 TALLOC_FREE(frame);
656 return -1;
659 /* Set the new attributes */
660 ret = cli_setattrE(srv->cli, fd,
661 change_time,
662 access_time,
663 write_time);
665 /* Close the file */
666 cli_close(srv->cli, fd);
669 * Unfortunately, setattrE() doesn't have a provision for
670 * setting the access mode (attributes). We'll have to try
671 * cli_setatr() for that, and with only this parameter, it
672 * seems to work on win98.
674 if (ret && mode != (uint16) -1) {
675 ret = cli_setatr(srv->cli, path, mode, 0);
678 if (! ret) {
679 errno = SMBC_errno(context, srv->cli);
680 TALLOC_FREE(frame);
681 return False;
685 TALLOC_FREE(frame);
686 return True;
690 * A routine to lseek() a file
693 off_t
694 SMBC_lseek_ctx(SMBCCTX *context,
695 SMBCFILE *file,
696 off_t offset,
697 int whence)
699 SMB_OFF_T size;
700 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
701 char *path = NULL;
702 char *targetpath = NULL;
703 struct cli_state *targetcli = NULL;
704 TALLOC_CTX *frame = talloc_stackframe();
706 if (!context || !context->internal->initialized) {
708 errno = EINVAL;
709 TALLOC_FREE(frame);
710 return -1;
713 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
715 errno = EBADF;
716 TALLOC_FREE(frame);
717 return -1;
721 if (!file->file) {
723 errno = EINVAL;
724 TALLOC_FREE(frame);
725 return -1; /* Can't lseek a dir ... */
729 switch (whence) {
730 case SEEK_SET:
731 file->offset = offset;
732 break;
734 case SEEK_CUR:
735 file->offset += offset;
736 break;
738 case SEEK_END:
739 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
740 if (SMBC_parse_path(frame,
741 context,
742 file->fname,
743 NULL,
744 &server,
745 &share,
746 &path,
747 &user,
748 &password,
749 NULL)) {
750 errno = EINVAL;
751 TALLOC_FREE(frame);
752 return -1;
755 /*d_printf(">>>lseek: resolving %s\n", path);*/
756 if (!cli_resolve_path(frame, "", file->srv->cli, path,
757 &targetcli, &targetpath)) {
758 d_printf("Could not resolve %s\n", path);
759 TALLOC_FREE(frame);
760 return -1;
762 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
764 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
765 &size, NULL, NULL, NULL, NULL, NULL))
767 SMB_OFF_T b_size = size;
768 if (!cli_getattrE(targetcli, file->cli_fd,
769 NULL, &b_size, NULL, NULL, NULL))
771 errno = EINVAL;
772 TALLOC_FREE(frame);
773 return -1;
774 } else
775 size = b_size;
777 file->offset = size + offset;
778 break;
780 default:
781 errno = EINVAL;
782 break;
786 TALLOC_FREE(frame);
787 return file->offset;
793 * Routine to truncate a file given by its file descriptor, to a specified size
797 SMBC_ftruncate_ctx(SMBCCTX *context,
798 SMBCFILE *file,
799 off_t length)
801 SMB_OFF_T size = length;
802 char *server = NULL;
803 char *share = NULL;
804 char *user = NULL;
805 char *password = NULL;
806 char *path = NULL;
807 char *targetpath = NULL;
808 struct cli_state *targetcli = NULL;
809 TALLOC_CTX *frame = talloc_stackframe();
811 if (!context || !context->internal->initialized) {
813 errno = EINVAL;
814 TALLOC_FREE(frame);
815 return -1;
818 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
819 errno = EBADF;
820 TALLOC_FREE(frame);
821 return -1;
824 if (!file->file) {
825 errno = EINVAL;
826 TALLOC_FREE(frame);
827 return -1;
830 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
831 if (SMBC_parse_path(frame,
832 context,
833 file->fname,
834 NULL,
835 &server,
836 &share,
837 &path,
838 &user,
839 &password,
840 NULL)) {
841 errno = EINVAL;
842 TALLOC_FREE(frame);
843 return -1;
846 /*d_printf(">>>fstat: resolving %s\n", path);*/
847 if (!cli_resolve_path(frame, "", file->srv->cli, path,
848 &targetcli, &targetpath)) {
849 d_printf("Could not resolve %s\n", path);
850 TALLOC_FREE(frame);
851 return -1;
853 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
855 if (!cli_ftruncate(targetcli, file->cli_fd, size)) {
856 errno = EINVAL;
857 TALLOC_FREE(frame);
858 return -1;
861 TALLOC_FREE(frame);
862 return 0;