s4:dsdb:tests: Also pass tests if asserted identity is present
[Samba.git] / source3 / libsmb / libsmb_file.c
blobfa301b9fa1872eb948f54e31d957331344db3a7d
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"
29 #include "../libcli/smb/smbXcli_base.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 uint16_t port = 0;
53 NTSTATUS status = NT_STATUS_OBJECT_PATH_INVALID;
54 TALLOC_CTX *frame = talloc_stackframe();
56 if (!context || !context->internal->initialized) {
57 errno = EINVAL; /* Best I can think of ... */
58 TALLOC_FREE(frame);
59 return NULL;
62 if (!fname) {
63 errno = EINVAL;
64 TALLOC_FREE(frame);
65 return NULL;
68 if (SMBC_parse_path(frame,
69 context,
70 fname,
71 &workgroup,
72 &server,
73 &port,
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, port, share, &workgroup, &user, &password);
95 if (!srv) {
96 if (errno == EPERM) errno = EACCES;
97 TALLOC_FREE(frame);
98 return NULL; /* SMBC_server sets errno */
101 /* Hmmm, the test for a directory is suspect here ... FIXME */
103 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
104 status = NT_STATUS_OBJECT_PATH_INVALID;
105 } else {
106 struct cli_credentials *creds = NULL;
108 file = SMB_MALLOC_P(SMBCFILE);
109 if (!file) {
110 errno = ENOMEM;
111 TALLOC_FREE(frame);
112 return NULL;
115 ZERO_STRUCTP(file);
117 creds = context->internal->creds;
118 /*d_printf(">>>open: resolving %s\n", path);*/
119 status = cli_resolve_path(
120 frame, "",
121 creds,
122 srv->cli, path, &targetcli, &targetpath);
123 if (!NT_STATUS_IS_OK(status)) {
124 d_printf("Could not resolve %s\n", path);
125 errno = ENOENT;
126 SAFE_FREE(file);
127 TALLOC_FREE(frame);
128 return NULL;
130 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
132 status = cli_open(targetcli, targetpath, flags,
133 context->internal->share_mode, &fd);
134 if (!NT_STATUS_IS_OK(status)) {
136 /* Handle the error ... */
138 SAFE_FREE(file);
139 TALLOC_FREE(frame);
140 errno = cli_status_to_errno(status);
141 return NULL;
144 /* Fill in file struct */
146 file->cli_fd = fd;
147 file->fname = SMB_STRDUP(fname);
148 file->srv = srv;
149 file->offset = 0;
150 file->file = True;
152 * targetcli is either equal to srv->cli or
153 * is a subsidiary DFS connection. Either way
154 * file->cli_fd belongs to it so we must cache
155 * it for read/write/close, not re-resolve each time.
156 * Re-resolving is both slow and incorrect.
158 file->targetcli = targetcli;
160 DLIST_ADD(context->internal->files, file);
163 * If the file was opened in O_APPEND mode, all write
164 * operations should be appended to the file. To do that,
165 * though, using this protocol, would require a getattrE()
166 * call for each and every write, to determine where the end
167 * of the file is. (There does not appear to be an append flag
168 * in the protocol.) Rather than add all of that overhead of
169 * retrieving the current end-of-file offset prior to each
170 * write operation, we'll assume that most append operations
171 * will continuously write, so we'll just set the offset to
172 * the end of the file now and hope that's adequate.
174 * Note to self: If this proves inadequate, and O_APPEND
175 * should, in some cases, be forced for each write, add a
176 * field in the context options structure, for
177 * "strict_append_mode" which would select between the current
178 * behavior (if FALSE) or issuing a getattrE() prior to each
179 * write and forcing the write to the end of the file (if
180 * TRUE). Adding that capability will likely require adding
181 * an "append" flag into the _SMBCFILE structure to track
182 * whether a file was opened in O_APPEND mode. -- djl
184 if (flags & O_APPEND) {
185 if (SMBC_lseek_ctx(context, file, 0, SEEK_END) < 0) {
186 (void) SMBC_close_ctx(context, file);
187 errno = ENXIO;
188 TALLOC_FREE(frame);
189 return NULL;
193 TALLOC_FREE(frame);
194 return file;
197 /* Check if opendir needed ... */
199 if (!NT_STATUS_IS_OK(status)) {
200 file = smbc_getFunctionOpendir(context)(context, fname);
201 TALLOC_FREE(frame);
202 if (file == NULL) {
203 errno = cli_status_to_errno(status);
205 return file;
208 errno = EINVAL; /* FIXME, correct errno ? */
209 TALLOC_FREE(frame);
210 return NULL;
214 * Routine to create a file
217 SMBCFILE *
218 SMBC_creat_ctx(SMBCCTX *context,
219 const char *path,
220 mode_t mode)
222 if (!context || !context->internal->initialized) {
223 errno = EINVAL;
224 return NULL;
227 return SMBC_open_ctx(context, path,
228 O_WRONLY | O_CREAT | O_TRUNC, mode);
232 * Routine to read() a file ...
235 ssize_t
236 SMBC_read_ctx(SMBCCTX *context,
237 SMBCFILE *file,
238 void *buf,
239 size_t count)
241 size_t ret;
242 TALLOC_CTX *frame = talloc_stackframe();
243 NTSTATUS status;
246 * offset:
248 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
249 * appears to pass file->offset (which is type off_t) differently than
250 * a local variable of type off_t. Using local variable "offset" in
251 * the call to cli_read() instead of file->offset fixes a problem
252 * retrieving data at an offset greater than 4GB.
254 off_t offset;
256 if (!context || !context->internal->initialized) {
257 errno = EINVAL;
258 TALLOC_FREE(frame);
259 return -1;
262 DEBUG(4, ("smbc_read(%p, %zu)\n", file, count));
264 if (!SMBC_dlist_contains(context->internal->files, file)) {
265 errno = EBADF;
266 TALLOC_FREE(frame);
267 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;
280 status = cli_read(file->targetcli, file->cli_fd, (char *)buf, offset,
281 count, &ret);
282 if (!NT_STATUS_IS_OK(status)) {
283 TALLOC_FREE(frame);
284 errno = cli_status_to_errno(status);
285 return -1;
288 file->offset += ret;
290 DEBUG(4, (" --> %zu\n", ret));
292 TALLOC_FREE(frame);
293 return ret; /* Success, ret bytes of data ... */
296 off_t
297 SMBC_splice_ctx(SMBCCTX *context,
298 SMBCFILE *srcfile,
299 SMBCFILE *dstfile,
300 off_t count,
301 int (*splice_cb)(off_t n, void *priv),
302 void *priv)
304 off_t written = 0;
305 TALLOC_CTX *frame = talloc_stackframe();
306 NTSTATUS status;
308 if (!context || !context->internal->initialized) {
309 errno = EINVAL;
310 TALLOC_FREE(frame);
311 return -1;
314 if (!SMBC_dlist_contains(context->internal->files, srcfile)) {
315 errno = EBADF;
316 TALLOC_FREE(frame);
317 return -1;
320 if (!SMBC_dlist_contains(context->internal->files, dstfile)) {
321 errno = EBADF;
322 TALLOC_FREE(frame);
323 return -1;
326 status = cli_splice(srcfile->targetcli, dstfile->targetcli,
327 srcfile->cli_fd, dstfile->cli_fd,
328 count, srcfile->offset, dstfile->offset, &written,
329 splice_cb, priv);
330 if (!NT_STATUS_IS_OK(status)) {
331 TALLOC_FREE(frame);
332 errno = cli_status_to_errno(status);
333 return -1;
336 srcfile->offset += written;
337 dstfile->offset += written;
339 TALLOC_FREE(frame);
340 return written;
344 * Routine to write() a file ...
347 ssize_t
348 SMBC_write_ctx(SMBCCTX *context,
349 SMBCFILE *file,
350 const void *buf,
351 size_t count)
353 off_t offset;
354 TALLOC_CTX *frame = talloc_stackframe();
355 NTSTATUS status;
357 /* First check all pointers before dereferencing them */
359 if (!context || !context->internal->initialized) {
360 errno = EINVAL;
361 TALLOC_FREE(frame);
362 return -1;
365 if (!SMBC_dlist_contains(context->internal->files, file)) {
366 errno = EBADF;
367 TALLOC_FREE(frame);
368 return -1;
371 /* Check that the buffer exists ... */
373 if (buf == NULL) {
374 errno = EINVAL;
375 TALLOC_FREE(frame);
376 return -1;
379 offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */
381 status = cli_writeall(file->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 TALLOC_CTX *frame = talloc_stackframe();
404 NTSTATUS status;
406 if (!context || !context->internal->initialized) {
407 errno = EINVAL;
408 TALLOC_FREE(frame);
409 return -1;
412 if (!SMBC_dlist_contains(context->internal->files, file)) {
413 errno = EBADF;
414 TALLOC_FREE(frame);
415 return -1;
418 /* IS a dir ... */
419 if (!file->file) {
420 TALLOC_FREE(frame);
421 return smbc_getFunctionClosedir(context)(context, file);
424 status = cli_close(file->targetcli, file->cli_fd);
425 if (!NT_STATUS_IS_OK(status)) {
426 SMBCSRV *srv;
427 DEBUG(3, ("cli_close failed on %s. purging server.\n",
428 file->fname));
429 /* Deallocate slot and remove the server
430 * from the server cache if unused */
431 srv = file->srv;
432 DLIST_REMOVE(context->internal->files, file);
433 SAFE_FREE(file->fname);
434 SAFE_FREE(file);
435 smbc_getFunctionRemoveUnusedServer(context)(context, srv);
436 TALLOC_FREE(frame);
437 errno = cli_status_to_errno(status);
438 return -1;
441 DLIST_REMOVE(context->internal->files, file);
442 SAFE_FREE(file->fname);
443 SAFE_FREE(file);
444 TALLOC_FREE(frame);
445 return 0;
449 * Get info from an SMB server on a file. Use a qpathinfo call first
450 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
452 NTSTATUS
453 SMBC_getatr(SMBCCTX * context,
454 SMBCSRV *srv,
455 const char *path,
456 struct stat *sb)
458 char *fixedpath = NULL;
459 char *targetpath = NULL;
460 struct cli_state *targetcli = NULL;
461 uint32_t attr = 0;
462 off_t size = 0;
463 struct timespec create_time_ts = {0};
464 struct timespec access_time_ts = {0};
465 struct timespec write_time_ts = {0};
466 struct timespec change_time_ts = {0};
467 time_t write_time = 0;
468 SMB_INO_T ino = 0;
469 struct cli_credentials *creds = NULL;
470 TALLOC_CTX *frame = talloc_stackframe();
471 NTSTATUS status;
473 if (!context || !context->internal->initialized) {
474 TALLOC_FREE(frame);
475 return NT_STATUS_INVALID_PARAMETER;
478 /* path fixup for . and .. */
479 if (ISDOT(path) || ISDOTDOT(path)) {
480 fixedpath = talloc_strdup(frame, "\\");
481 if (!fixedpath) {
482 TALLOC_FREE(frame);
483 return NT_STATUS_NO_MEMORY;
485 } else {
486 fixedpath = talloc_strdup(frame, path);
487 if (!fixedpath) {
488 TALLOC_FREE(frame);
489 return NT_STATUS_NO_MEMORY;
491 trim_string(fixedpath, NULL, "\\..");
492 trim_string(fixedpath, NULL, "\\.");
494 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
496 creds = context->internal->creds;
498 status = cli_resolve_path(frame, "",
499 creds,
500 srv->cli, fixedpath,
501 &targetcli, &targetpath);
502 if (!NT_STATUS_IS_OK(status)) {
503 d_printf("Couldn't resolve %s\n", path);
504 TALLOC_FREE(frame);
505 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
508 if (!srv->no_pathinfo2) {
509 status = cli_qpathinfo2(targetcli,
510 targetpath,
511 &create_time_ts,
512 &access_time_ts,
513 &write_time_ts,
514 &change_time_ts,
515 &size,
516 &attr,
517 &ino);
518 if (NT_STATUS_IS_OK(status)) {
519 goto setup_stat;
523 srv->no_pathinfo2 = True;
525 if (!srv->no_pathinfo3) {
526 status = cli_qpathinfo3(targetcli,
527 targetpath,
528 &create_time_ts,
529 &access_time_ts,
530 &write_time_ts,
531 &change_time_ts,
532 &size,
533 &attr,
534 &ino);
535 if (NT_STATUS_IS_OK(status)) {
536 goto setup_stat;
540 srv->no_pathinfo3 = True;
542 /* if this is NT then don't bother with the getatr */
543 if (smb1cli_conn_capabilities(targetcli->conn) & CAP_NT_SMBS) {
544 goto all_failed;
547 status = cli_getatr(targetcli, targetpath, &attr, &size, &write_time);
548 if (NT_STATUS_IS_OK(status)) {
549 struct timespec w_time_ts =
550 convert_time_t_to_timespec(write_time);
552 access_time_ts = change_time_ts = write_time_ts = w_time_ts;
554 goto setup_stat;
557 setup_stat:
558 setup_stat(sb,
559 path,
560 size,
561 attr,
562 ino,
563 srv->dev,
564 access_time_ts,
565 change_time_ts,
566 write_time_ts);
568 TALLOC_FREE(frame);
569 return NT_STATUS_OK;
571 all_failed:
572 srv->no_pathinfo2 = False;
573 srv->no_pathinfo3 = False;
575 TALLOC_FREE(frame);
576 return NT_STATUS_ACCESS_DENIED;
580 * Set file info on an SMB server. Use setpathinfo call first. If that
581 * fails, use setattrE..
583 * Access and modification time parameters are always used and must be
584 * provided. Create time, if zero, will be determined from the actual create
585 * time of the file. If non-zero, the create time will be set as well.
587 * "attr" (attributes) parameter may be set to -1 if it is not to be set.
589 bool
590 SMBC_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
591 struct timespec create_time,
592 struct timespec access_time,
593 struct timespec write_time,
594 struct timespec change_time,
595 uint16_t attr)
597 uint16_t fd;
598 uint32_t lattr = (uint32_t)attr;
599 NTSTATUS status;
600 TALLOC_CTX *frame = talloc_stackframe();
602 if (attr == (uint16_t)-1) {
604 * External ABI only passes in
605 * 16-bits of attribute. Make
606 * sure we correctly map to
607 * (uint32_t)-1 meaning don't
608 * change attributes if attr was
609 * passed in as 16-bit -1.
611 lattr = (uint32_t)-1;
616 * First, try setpathinfo (if qpathinfo succeeded), for it is the
617 * modern function for "new code" to be using, and it works given a
618 * filename rather than requiring that the file be opened to have its
619 * attributes manipulated.
621 if (srv->no_pathinfo ||
622 !NT_STATUS_IS_OK(cli_setpathinfo_ext(srv->cli, path,
623 create_time,
624 access_time,
625 write_time,
626 change_time,
627 lattr))) {
630 * setpathinfo is not supported; go to plan B.
632 * cli_setatr() does not work on win98, and it also doesn't
633 * support setting the access time (only the modification
634 * time), so in all cases, we open the specified file and use
635 * cli_setattrE() which should work on all OS versions, and
636 * supports both times.
639 /* Don't try {q,set}pathinfo() again, with this server */
640 srv->no_pathinfo = True;
642 /* Open the file */
643 status = cli_open(srv->cli, path, O_RDWR, DENY_NONE, &fd);
644 if (!NT_STATUS_IS_OK(status)) {
645 TALLOC_FREE(frame);
646 errno = cli_status_to_errno(status);
647 return False;
650 /* Set the new attributes */
651 status = cli_setattrE(
652 srv->cli,
654 change_time.tv_sec,
655 access_time.tv_sec,
656 write_time.tv_sec);
658 /* Close the file */
659 cli_close(srv->cli, fd);
662 * Unfortunately, setattrE() doesn't have a provision for
663 * setting the access attr (attributes). We'll have to try
664 * cli_setatr() for that, and with only this parameter, it
665 * seems to work on win98.
667 if (NT_STATUS_IS_OK(status) && attr != (uint16_t) -1) {
668 status = cli_setatr(srv->cli, path, (uint32_t)attr, 0);
671 if (!NT_STATUS_IS_OK(status)) {
672 TALLOC_FREE(frame);
673 errno = cli_status_to_errno(status);
674 return False;
678 TALLOC_FREE(frame);
679 return True;
683 * A routine to lseek() a file
686 off_t
687 SMBC_lseek_ctx(SMBCCTX *context,
688 SMBCFILE *file,
689 off_t offset,
690 int whence)
692 off_t size;
693 TALLOC_CTX *frame = talloc_stackframe();
695 if (!context || !context->internal->initialized) {
696 errno = EINVAL;
697 TALLOC_FREE(frame);
698 return -1;
701 if (!SMBC_dlist_contains(context->internal->files, file)) {
702 errno = EBADF;
703 TALLOC_FREE(frame);
704 return -1;
707 if (!file->file) {
708 errno = EINVAL;
709 TALLOC_FREE(frame);
710 return -1; /* Can't lseek a dir ... */
713 switch (whence) {
714 case SEEK_SET:
715 file->offset = offset;
716 break;
717 case SEEK_CUR:
718 file->offset += offset;
719 break;
720 case SEEK_END:
721 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
722 file->targetcli, file->cli_fd, NULL,
723 &size, NULL, NULL, NULL, NULL,
724 NULL))) {
725 errno = EINVAL;
726 TALLOC_FREE(frame);
727 return -1;
729 file->offset = size + offset;
730 break;
731 default:
732 errno = EINVAL;
733 break;
736 TALLOC_FREE(frame);
737 return file->offset;
742 * Routine to truncate a file given by its file descriptor, to a specified size
746 SMBC_ftruncate_ctx(SMBCCTX *context,
747 SMBCFILE *file,
748 off_t length)
750 off_t size = length;
751 TALLOC_CTX *frame = talloc_stackframe();
753 if (!context || !context->internal->initialized) {
754 errno = EINVAL;
755 TALLOC_FREE(frame);
756 return -1;
759 if (!SMBC_dlist_contains(context->internal->files, file)) {
760 errno = EBADF;
761 TALLOC_FREE(frame);
762 return -1;
765 if (!file->file) {
766 errno = EINVAL;
767 TALLOC_FREE(frame);
768 return -1;
771 if (!NT_STATUS_IS_OK(cli_ftruncate(file->targetcli, file->cli_fd, (uint64_t)size))) {
772 errno = EINVAL;
773 TALLOC_FREE(frame);
774 return -1;
777 TALLOC_FREE(frame);
778 return 0;