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 * targetcli is either equal to srv->cli or
149 * is a subsidiary DFS connection. Either way
150 * file->cli_fd belongs to it so we must cache
151 * it for read/write/close, not re-resolve each time.
152 * Re-resolving is both slow and incorrect.
154 file
->targetcli
= targetcli
;
156 DLIST_ADD(context
->internal
->files
, file
);
159 * If the file was opened in O_APPEND mode, all write
160 * operations should be appended to the file. To do that,
161 * though, using this protocol, would require a getattrE()
162 * call for each and every write, to determine where the end
163 * of the file is. (There does not appear to be an append flag
164 * in the protocol.) Rather than add all of that overhead of
165 * retrieving the current end-of-file offset prior to each
166 * write operation, we'll assume that most append operations
167 * will continuously write, so we'll just set the offset to
168 * the end of the file now and hope that's adequate.
170 * Note to self: If this proves inadequate, and O_APPEND
171 * should, in some cases, be forced for each write, add a
172 * field in the context options structure, for
173 * "strict_append_mode" which would select between the current
174 * behavior (if FALSE) or issuing a getattrE() prior to each
175 * write and forcing the write to the end of the file (if
176 * TRUE). Adding that capability will likely require adding
177 * an "append" flag into the _SMBCFILE structure to track
178 * whether a file was opened in O_APPEND mode. -- djl
180 if (flags
& O_APPEND
) {
181 if (SMBC_lseek_ctx(context
, file
, 0, SEEK_END
) < 0) {
182 (void) SMBC_close_ctx(context
, file
);
193 /* Check if opendir needed ... */
195 if (!NT_STATUS_IS_OK(status
)) {
198 eno
= SMBC_errno(context
, srv
->cli
);
199 file
= smbc_getFunctionOpendir(context
)(context
, fname
);
200 if (!file
) errno
= eno
;
205 errno
= EINVAL
; /* FIXME, correct errno ? */
211 * Routine to create a file
215 SMBC_creat_ctx(SMBCCTX
*context
,
219 if (!context
|| !context
->internal
->initialized
) {
224 return SMBC_open_ctx(context
, path
,
225 O_WRONLY
| O_CREAT
| O_TRUNC
, mode
);
229 * Routine to read() a file ...
233 SMBC_read_ctx(SMBCCTX
*context
,
239 TALLOC_CTX
*frame
= talloc_stackframe();
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.
253 if (!context
|| !context
->internal
->initialized
) {
259 DEBUG(4, ("smbc_read(%p, %d)\n", file
, (int)count
));
261 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
267 offset
= file
->offset
;
269 /* Check that the buffer exists ... */
277 status
= cli_read(file
->targetcli
, file
->cli_fd
, (char *)buf
, offset
,
279 if (!NT_STATUS_IS_OK(status
)) {
280 errno
= SMBC_errno(context
, file
->targetcli
);
287 DEBUG(4, (" --> %ld\n", (unsigned long)ret
));
290 return ret
; /* Success, ret bytes of data ... */
294 SMBC_splice_ctx(SMBCCTX
*context
,
298 int (*splice_cb
)(off_t n
, void *priv
),
302 TALLOC_CTX
*frame
= talloc_stackframe();
305 if (!context
|| !context
->internal
->initialized
) {
312 !SMBC_dlist_contains(context
->internal
->files
, srcfile
))
320 !SMBC_dlist_contains(context
->internal
->files
, dstfile
))
327 status
= cli_splice(srcfile
->targetcli
, dstfile
->targetcli
,
328 srcfile
->cli_fd
, dstfile
->cli_fd
,
329 count
, srcfile
->offset
, dstfile
->offset
, &written
,
331 if (!NT_STATUS_IS_OK(status
)) {
332 errno
= SMBC_errno(context
, srcfile
->targetcli
);
337 srcfile
->offset
+= written
;
338 dstfile
->offset
+= written
;
345 * Routine to write() a file ...
349 SMBC_write_ctx(SMBCCTX
*context
,
355 TALLOC_CTX
*frame
= talloc_stackframe();
358 /* First check all pointers before dereferencing them */
360 if (!context
|| !context
->internal
->initialized
) {
366 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
372 /* Check that the buffer exists ... */
380 offset
= file
->offset
; /* See "offset" comment in SMBC_read_ctx() */
382 status
= cli_writeall(file
->targetcli
, file
->cli_fd
,
383 0, (const uint8_t *)buf
, offset
, count
, NULL
);
384 if (!NT_STATUS_IS_OK(status
)) {
385 errno
= map_errno_from_nt_status(status
);
390 file
->offset
+= count
;
393 return count
; /* Success, 0 bytes of data ... */
397 * Routine to close() a file ...
401 SMBC_close_ctx(SMBCCTX
*context
,
404 TALLOC_CTX
*frame
= talloc_stackframe();
406 if (!context
|| !context
->internal
->initialized
) {
412 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
421 return smbc_getFunctionClosedir(context
)(context
, file
);
424 if (!NT_STATUS_IS_OK(cli_close(file
->targetcli
, file
->cli_fd
))) {
426 DEBUG(3, ("cli_close failed on %s. purging server.\n",
428 /* Deallocate slot and remove the server
429 * from the server cache if unused */
430 errno
= SMBC_errno(context
, file
->targetcli
);
432 DLIST_REMOVE(context
->internal
->files
, file
);
433 SAFE_FREE(file
->fname
);
435 smbc_getFunctionRemoveUnusedServer(context
)(context
, srv
);
440 DLIST_REMOVE(context
->internal
->files
, file
);
441 SAFE_FREE(file
->fname
);
448 * Get info from an SMB server on a file. Use a qpathinfo call first
449 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
452 SMBC_getatr(SMBCCTX
* context
,
457 char *fixedpath
= NULL
;
458 char *targetpath
= NULL
;
459 struct cli_state
*targetcli
= NULL
;
462 struct timespec create_time_ts
= {0};
463 struct timespec access_time_ts
= {0};
464 struct timespec write_time_ts
= {0};
465 struct timespec change_time_ts
= {0};
466 time_t write_time
= 0;
468 TALLOC_CTX
*frame
= talloc_stackframe();
471 if (!context
|| !context
->internal
->initialized
) {
477 /* path fixup for . and .. */
478 if (strequal(path
, ".") || strequal(path
, "..")) {
479 fixedpath
= talloc_strdup(frame
, "\\");
486 fixedpath
= talloc_strdup(frame
, path
);
492 trim_string(fixedpath
, NULL
, "\\..");
493 trim_string(fixedpath
, NULL
, "\\.");
495 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
497 status
= cli_resolve_path(frame
, "", context
->internal
->auth_info
,
499 &targetcli
, &targetpath
);
500 if (!NT_STATUS_IS_OK(status
)) {
501 d_printf("Couldn't resolve %s\n", path
);
507 if (!srv
->no_pathinfo2
) {
508 status
= cli_qpathinfo2(targetcli
,
517 if (NT_STATUS_IS_OK(status
)) {
522 srv
->no_pathinfo2
= True
;
524 if (!srv
->no_pathinfo3
) {
525 status
= cli_qpathinfo3(targetcli
,
534 if (NT_STATUS_IS_OK(status
)) {
539 srv
->no_pathinfo3
= True
;
541 /* if this is NT then don't bother with the getatr */
542 if (smb1cli_conn_capabilities(targetcli
->conn
) & CAP_NT_SMBS
) {
546 status
= cli_getatr(targetcli
, targetpath
, &mode
, &size
, &write_time
);
547 if (NT_STATUS_IS_OK(status
)) {
548 struct timespec w_time_ts
=
549 convert_time_t_to_timespec(write_time
);
551 access_time_ts
= change_time_ts
= write_time_ts
= w_time_ts
;
571 srv
->no_pathinfo2
= False
;
572 srv
->no_pathinfo3
= False
;
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 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
590 SMBC_setatr(SMBCCTX
* context
, SMBCSRV
*srv
, char *path
,
599 TALLOC_CTX
*frame
= talloc_stackframe();
602 * First, try setpathinfo (if qpathinfo succeeded), for it is the
603 * modern function for "new code" to be using, and it works given a
604 * filename rather than requiring that the file be opened to have its
605 * attributes manipulated.
607 if (srv
->no_pathinfo
||
608 !NT_STATUS_IS_OK(cli_setpathinfo_basic(srv
->cli
, path
,
616 * setpathinfo is not supported; go to plan B.
618 * cli_setatr() does not work on win98, and it also doesn't
619 * support setting the access time (only the modification
620 * time), so in all cases, we open the specified file and use
621 * cli_setattrE() which should work on all OS versions, and
622 * supports both times.
625 /* Don't try {q,set}pathinfo() again, with this server */
626 srv
->no_pathinfo
= True
;
629 if (!NT_STATUS_IS_OK(cli_open(srv
->cli
, path
, O_RDWR
, DENY_NONE
, &fd
))) {
630 errno
= SMBC_errno(context
, srv
->cli
);
635 /* Set the new attributes */
636 ret
= NT_STATUS_IS_OK(cli_setattrE(srv
->cli
, fd
,
642 cli_close(srv
->cli
, fd
);
645 * Unfortunately, setattrE() doesn't have a provision for
646 * setting the access mode (attributes). We'll have to try
647 * cli_setatr() for that, and with only this parameter, it
648 * seems to work on win98.
650 if (ret
&& mode
!= (uint16_t) -1) {
651 ret
= NT_STATUS_IS_OK(cli_setatr(srv
->cli
, path
, mode
, 0));
655 errno
= SMBC_errno(context
, srv
->cli
);
666 * A routine to lseek() a file
670 SMBC_lseek_ctx(SMBCCTX
*context
,
676 TALLOC_CTX
*frame
= talloc_stackframe();
678 if (!context
|| !context
->internal
->initialized
) {
684 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
693 return -1; /* Can't lseek a dir ... */
698 file
->offset
= offset
;
701 file
->offset
+= offset
;
704 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
705 file
->targetcli
, file
->cli_fd
, NULL
,
706 &size
, NULL
, NULL
, NULL
, NULL
,
709 if (!NT_STATUS_IS_OK(cli_getattrE(file
->targetcli
, file
->cli_fd
,
710 NULL
, &b_size
, NULL
, NULL
, NULL
))) {
717 file
->offset
= size
+ offset
;
730 * Routine to truncate a file given by its file descriptor, to a specified size
734 SMBC_ftruncate_ctx(SMBCCTX
*context
,
739 TALLOC_CTX
*frame
= talloc_stackframe();
741 if (!context
|| !context
->internal
->initialized
) {
747 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
759 if (!NT_STATUS_IS_OK(cli_ftruncate(file
->targetcli
, file
->cli_fd
, (uint64_t)size
))) {