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 file = SMB_MALLOC_P(SMBCFILE);
115 /*d_printf(">>>open: resolving %s\n", path);*/
116 status = cli_resolve_path(
117 frame, "", context->internal->auth_info,
118 srv->cli, path, &targetcli, &targetpath);
119 if (!NT_STATUS_IS_OK(status)) {
120 d_printf("Could not resolve %s\n", path);
126 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
128 status = cli_open(targetcli, targetpath, flags,
129 context->internal->share_mode, &fd);
130 if (!NT_STATUS_IS_OK(status)) {
132 /* Handle the error ... */
135 errno = SMBC_errno(context, targetcli);
140 /* Fill in file struct */
143 file->fname = SMB_STRDUP(fname);
148 DLIST_ADD(context->internal->files, file);
151 * If the file was opened in O_APPEND mode, all write
152 * operations should be appended to the file. To do that,
153 * though, using this protocol, would require a getattrE()
154 * call for each and every write, to determine where the end
155 * of the file is. (There does not appear to be an append flag
156 * in the protocol.) Rather than add all of that overhead of
157 * retrieving the current end-of-file offset prior to each
158 * write operation, we'll assume that most append operations
159 * will continuously write, so we'll just set the offset to
160 * the end of the file now and hope that's adequate.
162 * Note to self: If this proves inadequate, and O_APPEND
163 * should, in some cases, be forced for each write, add a
164 * field in the context options structure, for
165 * "strict_append_mode" which would select between the current
166 * behavior (if FALSE) or issuing a getattrE() prior to each
167 * write and forcing the write to the end of the file (if
168 * TRUE). Adding that capability will likely require adding
169 * an "append" flag into the _SMBCFILE structure to track
170 * whether a file was opened in O_APPEND mode. -- djl
172 if (flags & O_APPEND) {
173 if (SMBC_lseek_ctx(context, file, 0, SEEK_END) < 0) {
174 (void) SMBC_close_ctx(context, file);
185 /* Check if opendir needed ... */
187 if (!NT_STATUS_IS_OK(status)) {
190 eno = SMBC_errno(context, srv->cli);
191 file = smbc_getFunctionOpendir(context)(context, fname);
192 if (!file) errno = eno;
197 errno = EINVAL; /* FIXME, correct errno ? */
203 * Routine to create a file
207 SMBC_creat_ctx(SMBCCTX *context,
211 if (!context || !context->internal->initialized) {
216 return SMBC_open_ctx(context, path,
217 O_WRONLY | O_CREAT | O_TRUNC, mode);
221 * Routine to read() a file ...
225 SMBC_read_ctx(SMBCCTX *context,
231 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
233 char *targetpath = NULL;
234 struct cli_state *targetcli = NULL;
236 TALLOC_CTX *frame = talloc_stackframe();
242 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
243 * appears to pass file->offset (which is type off_t) differently than
244 * a local variable of type off_t. Using local variable "offset" in
245 * the call to cli_read() instead of file->offset fixes a problem
246 * retrieving data at an offset greater than 4GB.
250 if (!context || !context->internal->initialized) {
256 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
258 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
264 offset = file->offset;
266 /* Check that the buffer exists ... */
274 /*d_printf(">>>read: parsing %s\n", file->fname);*/
275 if (SMBC_parse_path(frame,
291 /*d_printf(">>>read: resolving %s\n", path);*/
292 status = cli_resolve_path(frame, "", context->internal->auth_info,
293 file->srv->cli, path,
294 &targetcli, &targetpath);
295 if (!NT_STATUS_IS_OK(status)) {
296 d_printf("Could not resolve %s\n", path);
301 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
303 status = cli_read(targetcli, file->cli_fd, (char *)buf, offset,
305 if (!NT_STATUS_IS_OK(status)) {
306 errno = SMBC_errno(context, targetcli);
313 DEBUG(4, (" --> %ld\n", (unsigned long)ret));
316 return ret; /* Success, ret bytes of data ... */
320 SMBC_splice_ctx(SMBCCTX *context,
324 int (*splice_cb)(off_t n, void *priv),
328 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
330 char *targetpath = NULL;
331 struct cli_state *srccli = NULL;
332 struct cli_state *dstcli = NULL;
334 TALLOC_CTX *frame = talloc_stackframe();
337 if (!context || !context->internal->initialized) {
344 !SMBC_dlist_contains(context->internal->files, srcfile))
352 !SMBC_dlist_contains(context->internal->files, dstfile))
359 if (SMBC_parse_path(frame,
375 status = cli_resolve_path(frame, "", context->internal->auth_info,
376 srcfile->srv->cli, path,
377 &srccli, &targetpath);
378 if (!NT_STATUS_IS_OK(status)) {
379 d_printf("Could not resolve %s\n", path);
385 if (SMBC_parse_path(frame,
401 status = cli_resolve_path(frame, "", context->internal->auth_info,
402 dstfile->srv->cli, path,
403 &dstcli, &targetpath);
404 if (!NT_STATUS_IS_OK(status)) {
405 d_printf("Could not resolve %s\n", path);
411 status = cli_splice(srccli, dstcli,
412 srcfile->cli_fd, dstfile->cli_fd,
413 count, srcfile->offset, dstfile->offset, &written,
415 if (!NT_STATUS_IS_OK(status)) {
416 errno = SMBC_errno(context, srccli);
421 srcfile->offset += written;
422 dstfile->offset += written;
429 * Routine to write() a file ...
433 SMBC_write_ctx(SMBCCTX *context,
439 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
441 char *targetpath = NULL;
442 struct cli_state *targetcli = NULL;
444 TALLOC_CTX *frame = talloc_stackframe();
447 /* First check all pointers before dereferencing them */
449 if (!context || !context->internal->initialized) {
455 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
461 /* Check that the buffer exists ... */
469 offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */
471 /*d_printf(">>>write: parsing %s\n", file->fname);*/
472 if (SMBC_parse_path(frame,
488 /*d_printf(">>>write: resolving %s\n", path);*/
489 status = cli_resolve_path(frame, "", context->internal->auth_info,
490 file->srv->cli, path,
491 &targetcli, &targetpath);
492 if (!NT_STATUS_IS_OK(status)) {
493 d_printf("Could not resolve %s\n", path);
498 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
500 status = cli_writeall(targetcli, file->cli_fd,
501 0, (const uint8_t *)buf, offset, count, NULL);
502 if (!NT_STATUS_IS_OK(status)) {
503 errno = map_errno_from_nt_status(status);
508 file->offset += count;
511 return count; /* Success, 0 bytes of data ... */
515 * Routine to close() a file ...
519 SMBC_close_ctx(SMBCCTX *context,
523 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
525 char *targetpath = NULL;
527 struct cli_state *targetcli = NULL;
528 TALLOC_CTX *frame = talloc_stackframe();
531 if (!context || !context->internal->initialized) {
537 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
546 return smbc_getFunctionClosedir(context)(context, file);
549 /*d_printf(">>>close: parsing %s\n", file->fname);*/
550 if (SMBC_parse_path(frame,
566 /*d_printf(">>>close: resolving %s\n", path);*/
567 status = cli_resolve_path(frame, "", context->internal->auth_info,
568 file->srv->cli, path,
569 &targetcli, &targetpath);
570 if (!NT_STATUS_IS_OK(status)) {
571 d_printf("Could not resolve %s\n", path);
576 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
578 if (!NT_STATUS_IS_OK(cli_close(targetcli, file->cli_fd))) {
579 DEBUG(3, ("cli_close failed on %s. purging server.\n",
581 /* Deallocate slot and remove the server
582 * from the server cache if unused */
583 errno = SMBC_errno(context, targetcli);
585 DLIST_REMOVE(context->internal->files, file);
586 SAFE_FREE(file->fname);
588 smbc_getFunctionRemoveUnusedServer(context)(context, srv);
593 DLIST_REMOVE(context->internal->files, file);
594 SAFE_FREE(file->fname);
601 * Get info from an SMB server on a file. Use a qpathinfo call first
602 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
605 SMBC_getatr(SMBCCTX * context,
610 struct timespec *create_time_ts,
611 struct timespec *access_time_ts,
612 struct timespec *write_time_ts,
613 struct timespec *change_time_ts,
616 char *fixedpath = NULL;
617 char *targetpath = NULL;
618 struct cli_state *targetcli = NULL;
620 TALLOC_CTX *frame = talloc_stackframe();
623 if (!context || !context->internal->initialized) {
629 /* path fixup for . and .. */
630 if (strequal(path, ".") || strequal(path, "..")) {
631 fixedpath = talloc_strdup(frame, "\\");
638 fixedpath = talloc_strdup(frame, path);
644 trim_string(fixedpath, NULL, "\\..");
645 trim_string(fixedpath, NULL, "\\.");
647 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
649 status = cli_resolve_path(frame, "", context->internal->auth_info,
651 &targetcli, &targetpath);
652 if (!NT_STATUS_IS_OK(status)) {
653 d_printf("Couldn't resolve %s\n", path);
659 if (!srv->no_pathinfo2 &&
660 NT_STATUS_IS_OK(cli_qpathinfo2(targetcli, targetpath,
670 srv->no_pathinfo2 = True;
672 if (!srv->no_pathinfo3 &&
673 NT_STATUS_IS_OK(cli_qpathinfo3(targetcli, targetpath,
683 srv->no_pathinfo3 = True;
685 /* if this is NT then don't bother with the getatr */
686 if (smb1cli_conn_capabilities(targetcli->conn) & CAP_NT_SMBS) {
690 if (NT_STATUS_IS_OK(cli_getatr(targetcli, targetpath, mode, size, &write_time))) {
691 struct timespec w_time_ts;
693 w_time_ts = convert_time_t_to_timespec(write_time);
694 if (write_time_ts != NULL) {
695 *write_time_ts = w_time_ts;
697 if (create_time_ts != NULL) {
698 *create_time_ts = w_time_ts;
700 if (access_time_ts != NULL) {
701 *access_time_ts = w_time_ts;
703 if (change_time_ts != NULL) {
704 *change_time_ts = w_time_ts;
714 srv->no_pathinfo2 = False;
715 srv->no_pathinfo3 = False;
723 * Set file info on an SMB server. Use setpathinfo call first. If that
724 * fails, use setattrE..
726 * Access and modification time parameters are always used and must be
727 * provided. Create time, if zero, will be determined from the actual create
728 * time of the file. If non-zero, the create time will be set as well.
730 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
733 SMBC_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
742 TALLOC_CTX *frame = talloc_stackframe();
745 * First, try setpathinfo (if qpathinfo succeeded), for it is the
746 * modern function for "new code" to be using, and it works given a
747 * filename rather than requiring that the file be opened to have its
748 * attributes manipulated.
750 if (srv->no_pathinfo ||
751 !NT_STATUS_IS_OK(cli_setpathinfo_basic(srv->cli, path,
759 * setpathinfo is not supported; go to plan B.
761 * cli_setatr() does not work on win98, and it also doesn't
762 * support setting the access time (only the modification
763 * time), so in all cases, we open the specified file and use
764 * cli_setattrE() which should work on all OS versions, and
765 * supports both times.
768 /* Don't try {q,set}pathinfo() again, with this server */
769 srv->no_pathinfo = True;
772 if (!NT_STATUS_IS_OK(cli_open(srv->cli, path, O_RDWR, DENY_NONE, &fd))) {
773 errno = SMBC_errno(context, srv->cli);
778 /* Set the new attributes */
779 ret = NT_STATUS_IS_OK(cli_setattrE(srv->cli, fd,
785 cli_close(srv->cli, fd);
788 * Unfortunately, setattrE() doesn't have a provision for
789 * setting the access mode (attributes). We'll have to try
790 * cli_setatr() for that, and with only this parameter, it
791 * seems to work on win98.
793 if (ret && mode != (uint16_t) -1) {
794 ret = NT_STATUS_IS_OK(cli_setatr(srv->cli, path, mode, 0));
798 errno = SMBC_errno(context, srv->cli);
809 * A routine to lseek() a file
813 SMBC_lseek_ctx(SMBCCTX *context,
819 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
821 char *targetpath = NULL;
822 struct cli_state *targetcli = NULL;
824 TALLOC_CTX *frame = talloc_stackframe();
827 if (!context || !context->internal->initialized) {
833 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
842 return -1; /* Can't lseek a dir ... */
847 file->offset = offset;
850 file->offset += offset;
853 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
854 if (SMBC_parse_path(frame,
870 /*d_printf(">>>lseek: resolving %s\n", path);*/
871 status = cli_resolve_path(
872 frame, "", context->internal->auth_info,
873 file->srv->cli, path, &targetcli, &targetpath);
874 if (!NT_STATUS_IS_OK(status)) {
875 d_printf("Could not resolve %s\n", path);
881 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
882 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
883 targetcli, file->cli_fd, NULL,
884 &size, NULL, NULL, NULL, NULL,
887 if (!NT_STATUS_IS_OK(cli_getattrE(targetcli, file->cli_fd,
888 NULL, &b_size, NULL, NULL, NULL))) {
895 file->offset = size + offset;
908 * Routine to truncate a file given by its file descriptor, to a specified size
912 SMBC_ftruncate_ctx(SMBCCTX *context,
920 char *password = NULL;
922 char *targetpath = NULL;
924 struct cli_state *targetcli = NULL;
925 TALLOC_CTX *frame = talloc_stackframe();
928 if (!context || !context->internal->initialized) {
934 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
946 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
947 if (SMBC_parse_path(frame,
963 /*d_printf(">>>fstat: resolving %s\n", path);*/
964 status = cli_resolve_path(frame, "", context->internal->auth_info,
965 file->srv->cli, path,
966 &targetcli, &targetpath);
967 if (!NT_STATUS_IS_OK(status)) {
968 d_printf("Could not resolve %s\n", path);
973 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
975 if (!NT_STATUS_IS_OK(cli_ftruncate(targetcli, file->cli_fd, (uint64_t)size))) {