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/>.
26 #include "source3/include/client.h"
27 #include "source3/libsmb/proto.h"
28 #include "libsmbclient.h"
29 #include "libsmb_internal.h"
30 #include "../libcli/smb/smbXcli_base.h"
33 * Routine to open() a file ...
37 SMBC_open_ctx(SMBCCTX *context,
45 char *password = NULL;
46 char *workgroup = NULL;
48 char *targetpath = NULL;
49 struct cli_state *targetcli = NULL;
51 SMBCFILE *file = NULL;
54 NTSTATUS status = NT_STATUS_OBJECT_PATH_INVALID;
55 struct cli_credentials *creds = NULL;
56 TALLOC_CTX *frame = talloc_stackframe();
57 bool smb311_posix_saved;
59 if (!context || !context->internal->initialized) {
61 errno = EINVAL; /* Best I can think of ... */
71 if (SMBC_parse_path(frame,
87 if (!user || user[0] == (char)0) {
88 user = talloc_strdup(frame, smbc_getUser(context));
96 srv = SMBC_server(frame, context, True,
97 server, port, share, &workgroup, &user, &password);
104 if (errno == EPERM) {
107 return NULL; /* SMBC_server sets errno */
110 /* Hmmm, the test for a directory is suspect here ... FIXME */
112 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
113 file = smbc_getFunctionOpendir(context)(context, fname);
116 errno = cli_status_to_errno(status);
121 file = SMB_CALLOC_ARRAY(SMBCFILE, 1);
128 creds = context->internal->creds;
129 /*d_printf(">>>open: resolving %s\n", path);*/
130 status = cli_resolve_path(
131 context->internal->mem_ctx,
134 srv->cli, path, &targetcli, &targetpath);
135 if (!NT_STATUS_IS_OK(status)) {
136 d_printf("Could not resolve %s\n", path);
142 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
145 * Indicate to cli_smb2_create_fnum_send() that we want file
146 * handles with posix extensions.
149 smb311_posix_saved = targetcli->smb2.client_smb311_posix;
150 targetcli->smb2.client_smb311_posix =
151 smbc_getOptionPosixExtensions(context) &&
152 (smbXcli_conn_protocol(targetcli->conn) >= PROTOCOL_SMB3_11) &&
153 smbXcli_conn_have_posix(targetcli->conn);
156 * Random error that the O_PATH if-block will never return
158 status = NT_STATUS_LDAP(0);
161 if (flags & O_PATH) {
162 if ((flags & ~O_PATH) != 0) {
168 status = cli_ntcreate(
170 targetpath, /* fname */
172 SEC_FILE_READ_ATTRIBUTE | SEC_FILE_READ_EA |
173 SEC_STD_READ_CONTROL, /* DesiredAccess
175 0, /* FileAttributes */
176 FILE_SHARE_READ | FILE_SHARE_WRITE |
177 FILE_SHARE_DELETE, /* ShareAccess */
178 FILE_OPEN, /* CreateDisposition */
179 0x0, /* CreateOptions */
180 0x0, /* SecurityFlags */
185 if (NT_STATUS_EQUAL(status, NT_STATUS_LDAP(0))) {
186 status = cli_open(targetcli,
189 context->internal->share_mode,
193 targetcli->smb2.client_smb311_posix = smb311_posix_saved;
195 if (!NT_STATUS_IS_OK(status)) {
197 /* Handle the error ... */
201 errno = cli_status_to_errno(status);
205 /* Fill in file struct */
208 file->fname = SMB_STRDUP(fname);
213 * targetcli is either equal to srv->cli or
214 * is a subsidiary DFS connection. Either way
215 * file->cli_fd belongs to it so we must cache
216 * it for read/write/close, not re-resolve each time.
217 * Re-resolving is both slow and incorrect.
219 file->targetcli = targetcli;
221 DLIST_ADD(context->internal->files, file);
224 * If the file was opened in O_APPEND mode, all write
225 * operations should be appended to the file. To do that,
226 * though, using this protocol, would require a getattrE()
227 * call for each and every write, to determine where the end
228 * of the file is. (There does not appear to be an append flag
229 * in the protocol.) Rather than add all of that overhead of
230 * retrieving the current end-of-file offset prior to each
231 * write operation, we'll assume that most append operations
232 * will continuously write, so we'll just set the offset to
233 * the end of the file now and hope that's adequate.
235 * Note to self: If this proves inadequate, and O_APPEND
236 * should, in some cases, be forced for each write, add a
237 * field in the context options structure, for
238 * "strict_append_mode" which would select between the current
239 * behavior (if FALSE) or issuing a getattrE() prior to each
240 * write and forcing the write to the end of the file (if
241 * TRUE). Adding that capability will likely require adding
242 * an "append" flag into the _SMBCFILE structure to track
243 * whether a file was opened in O_APPEND mode. -- djl
245 if (flags & O_APPEND) {
246 if (SMBC_lseek_ctx(context, file, 0, SEEK_END) < 0) {
247 (void) SMBC_close_ctx(context, file);
259 * Routine to create a file
263 SMBC_creat_ctx(SMBCCTX *context,
267 return SMBC_open_ctx(context, path,
268 O_WRONLY | O_CREAT | O_TRUNC, mode);
272 * Routine to read() a file ...
276 SMBC_read_ctx(SMBCCTX *context,
282 TALLOC_CTX *frame = talloc_stackframe();
288 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
289 * appears to pass file->offset (which is type off_t) differently than
290 * a local variable of type off_t. Using local variable "offset" in
291 * the call to cli_read() instead of file->offset fixes a problem
292 * retrieving data at an offset greater than 4GB.
296 if (!context || !context->internal->initialized) {
302 DEBUG(4, ("smbc_read(%p, %zu)\n", file, count));
304 if (!SMBC_dlist_contains(context->internal->files, file)) {
310 offset = file->offset;
312 /* Check that the buffer exists ... */
320 status = cli_read(file->targetcli, file->cli_fd, (char *)buf, offset,
322 if (!NT_STATUS_IS_OK(status)) {
324 errno = cli_status_to_errno(status);
330 DEBUG(4, (" --> %zu\n", ret));
333 return ret; /* Success, ret bytes of data ... */
337 SMBC_splice_ctx(SMBCCTX *context,
341 int (*splice_cb)(off_t n, void *priv),
345 TALLOC_CTX *frame = talloc_stackframe();
348 if (!context || !context->internal->initialized) {
354 if (!SMBC_dlist_contains(context->internal->files, srcfile)) {
360 if (!SMBC_dlist_contains(context->internal->files, dstfile)) {
366 status = cli_splice(srcfile->targetcli, dstfile->targetcli,
367 srcfile->cli_fd, dstfile->cli_fd,
368 count, srcfile->offset, dstfile->offset, &written,
370 if (!NT_STATUS_IS_OK(status)) {
372 errno = cli_status_to_errno(status);
376 srcfile->offset += written;
377 dstfile->offset += written;
384 * Routine to write() a file ...
388 SMBC_write_ctx(SMBCCTX *context,
394 TALLOC_CTX *frame = talloc_stackframe();
397 /* First check all pointers before dereferencing them */
399 if (!context || !context->internal->initialized) {
405 if (!SMBC_dlist_contains(context->internal->files, file)) {
411 /* Check that the buffer exists ... */
419 offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */
421 status = cli_writeall(file->targetcli, file->cli_fd,
422 0, (const uint8_t *)buf, offset, count, NULL);
423 if (!NT_STATUS_IS_OK(status)) {
425 errno = map_errno_from_nt_status(status);
429 file->offset += count;
432 return count; /* Success, 0 bytes of data ... */
436 * Routine to close() a file ...
440 SMBC_close_ctx(SMBCCTX *context,
443 TALLOC_CTX *frame = talloc_stackframe();
446 if (!context || !context->internal->initialized) {
452 if (!SMBC_dlist_contains(context->internal->files, file)) {
461 return smbc_getFunctionClosedir(context)(context, file);
464 status = cli_close(file->targetcli, file->cli_fd);
465 if (!NT_STATUS_IS_OK(status)) {
467 DEBUG(3, ("cli_close failed on %s. purging server.\n",
469 /* Deallocate slot and remove the server
470 * from the server cache if unused */
472 DLIST_REMOVE(context->internal->files, file);
473 SAFE_FREE(file->fname);
475 smbc_getFunctionRemoveUnusedServer(context)(context, srv);
477 errno = cli_status_to_errno(status);
481 DLIST_REMOVE(context->internal->files, file);
482 SAFE_FREE(file->fname);
489 * Get info from an SMB server on a file. Use a qpathinfo call first
490 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
493 SMBC_getatr(SMBCCTX * context,
498 char *fixedpath = NULL;
499 char *targetpath = NULL;
500 struct cli_state *targetcli = NULL;
503 struct timespec create_time_ts = {0};
504 struct timespec access_time_ts = {0};
505 struct timespec write_time_ts = {0};
506 struct timespec change_time_ts = {0};
507 struct timespec w_time_ts = {0};
508 time_t write_time = 0;
510 mode_t mode = S_IFREG;
511 struct cli_credentials *creds = NULL;
512 TALLOC_CTX *frame = talloc_stackframe();
513 NTSTATUS status = NT_STATUS_ACCESS_DENIED;
515 if (!context || !context->internal->initialized) {
517 return NT_STATUS_INVALID_PARAMETER;
520 /* path fixup for . and .. */
521 if (ISDOT(path) || ISDOTDOT(path)) {
522 fixedpath = talloc_strdup(frame, "\\");
525 return NT_STATUS_NO_MEMORY;
528 fixedpath = talloc_strdup(frame, path);
531 return NT_STATUS_NO_MEMORY;
533 trim_string(fixedpath, NULL, "\\..");
534 trim_string(fixedpath, NULL, "\\.");
536 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
538 creds = context->internal->creds;
540 status = cli_resolve_path(context->internal->mem_ctx,
544 &targetcli, &targetpath);
545 if (!NT_STATUS_IS_OK(status)) {
546 d_printf("Couldn't resolve %s\n", path);
548 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
551 if (!srv->no_pathinfo2) {
552 bool not_supported_error = false;
553 status = cli_qpathinfo2(targetcli,
563 if (NT_STATUS_IS_OK(status)) {
566 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL) ||
567 NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
568 not_supported_error = true;
570 if (!not_supported_error) {
571 /* "Normal error". Just return it to caller. */
577 srv->no_pathinfo2 = True;
579 if (!srv->no_pathinfo3) {
580 bool not_supported_error = false;
581 status = cli_qpathinfo3(targetcli,
590 if (NT_STATUS_IS_OK(status)) {
593 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL) ||
594 NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
595 not_supported_error = true;
597 if (!not_supported_error) {
598 /* "Normal error". Just return it to caller. */
604 srv->no_pathinfo3 = True;
606 /* if this is NT then don't bother with the getatr */
607 if (smb1cli_conn_capabilities(targetcli->conn) & CAP_NT_SMBS) {
611 status = cli_getatr(targetcli, targetpath, &attr, &size, &write_time);
612 if (!NT_STATUS_IS_OK(status)) {
615 w_time_ts = convert_time_t_to_timespec(write_time);
616 access_time_ts = change_time_ts = write_time_ts = w_time_ts;
629 if ((context->internal->posix_extensions) && (mode != S_IFREG)) {
630 sb->st_mode = (sb->st_mode & ~S_IFMT) | mode;
637 srv->no_pathinfo2 = False;
638 srv->no_pathinfo3 = False;
645 * Set file info on an SMB server. Use setpathinfo call first. If that
646 * fails, use setattrE..
648 * Access and modification time parameters are always used and must be
649 * provided. Create time, if zero, will be determined from the actual create
650 * time of the file. If non-zero, the create time will be set as well.
652 * "attr" (attributes) parameter may be set to -1 if it is not to be set.
655 SMBC_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
656 struct timespec create_time,
657 struct timespec access_time,
658 struct timespec write_time,
659 struct timespec change_time,
663 uint32_t lattr = (uint32_t)attr;
665 TALLOC_CTX *frame = talloc_stackframe();
667 if (attr == (uint16_t)-1) {
669 * External ABI only passes in
670 * 16-bits of attribute. Make
671 * sure we correctly map to
672 * (uint32_t)-1 meaning don't
673 * change attributes if attr was
674 * passed in as 16-bit -1.
676 lattr = (uint32_t)-1;
681 * First, try setpathinfo (if qpathinfo succeeded), for it is the
682 * modern function for "new code" to be using, and it works given a
683 * filename rather than requiring that the file be opened to have its
684 * attributes manipulated.
686 if (srv->no_pathinfo ||
687 !NT_STATUS_IS_OK(cli_setpathinfo_ext(srv->cli, path,
695 * setpathinfo is not supported; go to plan B.
697 * cli_setatr() does not work on win98, and it also doesn't
698 * support setting the access time (only the modification
699 * time), so in all cases, we open the specified file and use
700 * cli_setattrE() which should work on all OS versions, and
701 * supports both times.
704 /* Don't try {q,set}pathinfo() again, with this server */
705 srv->no_pathinfo = True;
708 status = cli_open(srv->cli, path, O_RDWR, DENY_NONE, &fd);
709 if (!NT_STATUS_IS_OK(status)) {
711 errno = cli_status_to_errno(status);
715 /* Set the new attributes */
716 status = cli_setattrE(
724 cli_close(srv->cli, fd);
727 * Unfortunately, setattrE() doesn't have a provision for
728 * setting the access attr (attributes). We'll have to try
729 * cli_setatr() for that, and with only this parameter, it
730 * seems to work on win98.
732 if (NT_STATUS_IS_OK(status) && attr != (uint16_t) -1) {
733 status = cli_setatr(srv->cli, path, (uint32_t)attr, 0);
736 if (!NT_STATUS_IS_OK(status)) {
738 errno = cli_status_to_errno(status);
748 * A routine to lseek() a file
752 SMBC_lseek_ctx(SMBCCTX *context,
758 TALLOC_CTX *frame = talloc_stackframe();
760 if (!context || !context->internal->initialized) {
766 if (!SMBC_dlist_contains(context->internal->files, file)) {
775 return -1; /* Can't lseek a dir ... */
780 file->offset = offset;
783 file->offset += offset;
786 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
787 file->targetcli, file->cli_fd, NULL,
788 &size, NULL, NULL, NULL, NULL,
794 file->offset = size + offset;
807 * Routine to truncate a file given by its file descriptor, to a specified size
811 SMBC_ftruncate_ctx(SMBCCTX *context,
816 TALLOC_CTX *frame = talloc_stackframe();
818 if (!context || !context->internal->initialized) {
824 if (!SMBC_dlist_contains(context->internal->files, file)) {
836 if (!NT_STATUS_IS_OK(cli_ftruncate(file->targetcli, file->cli_fd, (uint64_t)size))) {