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 "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 ...
36 SMBC_open_ctx(SMBCCTX *context,
44 char *password = NULL;
45 char *workgroup = NULL;
47 char *targetpath = NULL;
48 struct cli_state *targetcli = NULL;
50 SMBCFILE *file = NULL;
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 ... */
68 if (SMBC_parse_path(frame,
84 if (!user || user[0] == (char)0) {
85 user = talloc_strdup(frame, smbc_getUser(context));
93 srv = SMBC_server(frame, context, True,
94 server, port, share, &workgroup, &user, &password);
96 if (errno == EPERM) errno = EACCES;
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;
106 struct cli_credentials *creds = NULL;
108 file = SMB_MALLOC_P(SMBCFILE);
117 creds = get_cmdline_auth_info_creds(
118 context->internal->auth_info);
119 /*d_printf(">>>open: resolving %s\n", path);*/
120 status = cli_resolve_path(
123 srv->cli, path, &targetcli, &targetpath);
124 if (!NT_STATUS_IS_OK(status)) {
125 d_printf("Could not resolve %s\n", path);
131 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
133 status = cli_open(targetcli, targetpath, flags,
134 context->internal->share_mode, &fd);
135 if (!NT_STATUS_IS_OK(status)) {
137 /* Handle the error ... */
140 errno = SMBC_errno(context, targetcli);
145 /* Fill in file struct */
148 file->fname = SMB_STRDUP(fname);
153 * targetcli is either equal to srv->cli or
154 * is a subsidiary DFS connection. Either way
155 * file->cli_fd belongs to it so we must cache
156 * it for read/write/close, not re-resolve each time.
157 * Re-resolving is both slow and incorrect.
159 file->targetcli = targetcli;
161 DLIST_ADD(context->internal->files, file);
164 * If the file was opened in O_APPEND mode, all write
165 * operations should be appended to the file. To do that,
166 * though, using this protocol, would require a getattrE()
167 * call for each and every write, to determine where the end
168 * of the file is. (There does not appear to be an append flag
169 * in the protocol.) Rather than add all of that overhead of
170 * retrieving the current end-of-file offset prior to each
171 * write operation, we'll assume that most append operations
172 * will continuously write, so we'll just set the offset to
173 * the end of the file now and hope that's adequate.
175 * Note to self: If this proves inadequate, and O_APPEND
176 * should, in some cases, be forced for each write, add a
177 * field in the context options structure, for
178 * "strict_append_mode" which would select between the current
179 * behavior (if FALSE) or issuing a getattrE() prior to each
180 * write and forcing the write to the end of the file (if
181 * TRUE). Adding that capability will likely require adding
182 * an "append" flag into the _SMBCFILE structure to track
183 * whether a file was opened in O_APPEND mode. -- djl
185 if (flags & O_APPEND) {
186 if (SMBC_lseek_ctx(context, file, 0, SEEK_END) < 0) {
187 (void) SMBC_close_ctx(context, file);
198 /* Check if opendir needed ... */
200 if (!NT_STATUS_IS_OK(status)) {
203 eno = SMBC_errno(context, srv->cli);
204 file = smbc_getFunctionOpendir(context)(context, fname);
205 if (!file) errno = eno;
210 errno = EINVAL; /* FIXME, correct errno ? */
216 * Routine to create a file
220 SMBC_creat_ctx(SMBCCTX *context,
224 if (!context || !context->internal->initialized) {
229 return SMBC_open_ctx(context, path,
230 O_WRONLY | O_CREAT | O_TRUNC, mode);
234 * Routine to read() a file ...
238 SMBC_read_ctx(SMBCCTX *context,
244 TALLOC_CTX *frame = talloc_stackframe();
250 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
251 * appears to pass file->offset (which is type off_t) differently than
252 * a local variable of type off_t. Using local variable "offset" in
253 * the call to cli_read() instead of file->offset fixes a problem
254 * retrieving data at an offset greater than 4GB.
258 if (!context || !context->internal->initialized) {
264 DEBUG(4, ("smbc_read(%p, %zu)\n", file, count));
266 if (!SMBC_dlist_contains(context->internal->files, file)) {
272 offset = file->offset;
274 /* Check that the buffer exists ... */
282 status = cli_read(file->targetcli, file->cli_fd, (char *)buf, offset,
284 if (!NT_STATUS_IS_OK(status)) {
285 errno = SMBC_errno(context, file->targetcli);
292 DEBUG(4, (" --> %zu\n", ret));
295 return ret; /* Success, ret bytes of data ... */
299 SMBC_splice_ctx(SMBCCTX *context,
303 int (*splice_cb)(off_t n, void *priv),
307 TALLOC_CTX *frame = talloc_stackframe();
310 if (!context || !context->internal->initialized) {
316 if (!SMBC_dlist_contains(context->internal->files, srcfile)) {
322 if (!SMBC_dlist_contains(context->internal->files, dstfile)) {
328 status = cli_splice(srcfile->targetcli, dstfile->targetcli,
329 srcfile->cli_fd, dstfile->cli_fd,
330 count, srcfile->offset, dstfile->offset, &written,
332 if (!NT_STATUS_IS_OK(status)) {
333 errno = SMBC_errno(context, srcfile->targetcli);
338 srcfile->offset += written;
339 dstfile->offset += written;
346 * Routine to write() a file ...
350 SMBC_write_ctx(SMBCCTX *context,
356 TALLOC_CTX *frame = talloc_stackframe();
359 /* First check all pointers before dereferencing them */
361 if (!context || !context->internal->initialized) {
367 if (!SMBC_dlist_contains(context->internal->files, file)) {
373 /* Check that the buffer exists ... */
381 offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */
383 status = cli_writeall(file->targetcli, file->cli_fd,
384 0, (const uint8_t *)buf, offset, count, NULL);
385 if (!NT_STATUS_IS_OK(status)) {
386 errno = map_errno_from_nt_status(status);
391 file->offset += count;
394 return count; /* Success, 0 bytes of data ... */
398 * Routine to close() a file ...
402 SMBC_close_ctx(SMBCCTX *context,
405 TALLOC_CTX *frame = talloc_stackframe();
407 if (!context || !context->internal->initialized) {
413 if (!SMBC_dlist_contains(context->internal->files, file)) {
422 return smbc_getFunctionClosedir(context)(context, file);
425 if (!NT_STATUS_IS_OK(cli_close(file->targetcli, file->cli_fd))) {
427 DEBUG(3, ("cli_close failed on %s. purging server.\n",
429 /* Deallocate slot and remove the server
430 * from the server cache if unused */
431 errno = SMBC_errno(context, file->targetcli);
433 DLIST_REMOVE(context->internal->files, file);
434 SAFE_FREE(file->fname);
436 smbc_getFunctionRemoveUnusedServer(context)(context, srv);
441 DLIST_REMOVE(context->internal->files, file);
442 SAFE_FREE(file->fname);
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
453 SMBC_getatr(SMBCCTX * context,
458 char *fixedpath = NULL;
459 char *targetpath = NULL;
460 struct cli_state *targetcli = NULL;
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;
469 struct cli_credentials *creds = NULL;
470 TALLOC_CTX *frame = talloc_stackframe();
473 if (!context || !context->internal->initialized) {
479 /* path fixup for . and .. */
480 if (ISDOT(path) || ISDOTDOT(path)) {
481 fixedpath = talloc_strdup(frame, "\\");
488 fixedpath = talloc_strdup(frame, path);
494 trim_string(fixedpath, NULL, "\\..");
495 trim_string(fixedpath, NULL, "\\.");
497 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
499 creds = get_cmdline_auth_info_creds(context->internal->auth_info);
501 status = cli_resolve_path(frame, "",
504 &targetcli, &targetpath);
505 if (!NT_STATUS_IS_OK(status)) {
506 d_printf("Couldn't resolve %s\n", path);
512 if (!srv->no_pathinfo2) {
513 status = cli_qpathinfo2(targetcli,
522 if (NT_STATUS_IS_OK(status)) {
527 srv->no_pathinfo2 = True;
529 if (!srv->no_pathinfo3) {
530 status = cli_qpathinfo3(targetcli,
539 if (NT_STATUS_IS_OK(status)) {
544 srv->no_pathinfo3 = True;
546 /* if this is NT then don't bother with the getatr */
547 if (smb1cli_conn_capabilities(targetcli->conn) & CAP_NT_SMBS) {
551 status = cli_getatr(targetcli, targetpath, &attr, &size, &write_time);
552 if (NT_STATUS_IS_OK(status)) {
553 struct timespec w_time_ts =
554 convert_time_t_to_timespec(write_time);
556 access_time_ts = change_time_ts = write_time_ts = w_time_ts;
576 srv->no_pathinfo2 = False;
577 srv->no_pathinfo3 = False;
585 * Set file info on an SMB server. Use setpathinfo call first. If that
586 * fails, use setattrE..
588 * Access and modification time parameters are always used and must be
589 * provided. Create time, if zero, will be determined from the actual create
590 * time of the file. If non-zero, the create time will be set as well.
592 * "attr" (attributes) parameter may be set to -1 if it is not to be set.
595 SMBC_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
596 struct timespec create_time,
597 struct timespec access_time,
598 struct timespec write_time,
599 struct timespec change_time,
604 uint32_t lattr = (uint32_t)attr;
605 TALLOC_CTX *frame = talloc_stackframe();
607 if (attr == (uint16_t)-1) {
609 * External ABI only passes in
610 * 16-bits of attribute. Make
611 * sure we correctly map to
612 * (uint32_t)-1 meaning don't
613 * change attributes if attr was
614 * passed in as 16-bit -1.
616 lattr = (uint32_t)-1;
621 * First, try setpathinfo (if qpathinfo succeeded), for it is the
622 * modern function for "new code" to be using, and it works given a
623 * filename rather than requiring that the file be opened to have its
624 * attributes manipulated.
626 if (srv->no_pathinfo ||
627 !NT_STATUS_IS_OK(cli_setpathinfo_ext(srv->cli, path,
635 * setpathinfo is not supported; go to plan B.
637 * cli_setatr() does not work on win98, and it also doesn't
638 * support setting the access time (only the modification
639 * time), so in all cases, we open the specified file and use
640 * cli_setattrE() which should work on all OS versions, and
641 * supports both times.
644 /* Don't try {q,set}pathinfo() again, with this server */
645 srv->no_pathinfo = True;
648 if (!NT_STATUS_IS_OK(cli_open(srv->cli, path, O_RDWR, DENY_NONE, &fd))) {
649 errno = SMBC_errno(context, srv->cli);
654 /* Set the new attributes */
655 ret = NT_STATUS_IS_OK(cli_setattrE(srv->cli, fd,
661 cli_close(srv->cli, fd);
664 * Unfortunately, setattrE() doesn't have a provision for
665 * setting the access attr (attributes). We'll have to try
666 * cli_setatr() for that, and with only this parameter, it
667 * seems to work on win98.
669 if (ret && attr != (uint16_t) -1) {
670 ret = NT_STATUS_IS_OK(cli_setatr(srv->cli, path, (uint32_t)attr, 0));
674 errno = SMBC_errno(context, srv->cli);
685 * A routine to lseek() a file
689 SMBC_lseek_ctx(SMBCCTX *context,
695 TALLOC_CTX *frame = talloc_stackframe();
697 if (!context || !context->internal->initialized) {
703 if (!SMBC_dlist_contains(context->internal->files, file)) {
712 return -1; /* Can't lseek a dir ... */
717 file->offset = offset;
720 file->offset += offset;
723 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
724 file->targetcli, file->cli_fd, NULL,
725 &size, NULL, NULL, NULL, NULL,
731 file->offset = size + offset;
744 * Routine to truncate a file given by its file descriptor, to a specified size
748 SMBC_ftruncate_ctx(SMBCCTX *context,
753 TALLOC_CTX *frame = talloc_stackframe();
755 if (!context || !context->internal->initialized) {
761 if (!SMBC_dlist_contains(context->internal->files, file)) {
773 if (!NT_STATUS_IS_OK(cli_ftruncate(file->targetcli, file->cli_fd, (uint64_t)size))) {