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"
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
;
52 NTSTATUS status
= NT_STATUS_OBJECT_PATH_INVALID
;
53 TALLOC_CTX
*frame
= talloc_stackframe();
55 if (!context
|| !context
->internal
->initialized
) {
56 errno
= EINVAL
; /* Best I can think of ... */
67 if (SMBC_parse_path(frame
,
82 if (!user
|| user
[0] == (char)0) {
83 user
= talloc_strdup(frame
, smbc_getUser(context
));
91 srv
= SMBC_server(frame
, context
, True
,
92 server
, share
, &workgroup
, &user
, &password
);
94 if (errno
== EPERM
) errno
= EACCES
;
96 return NULL
; /* SMBC_server sets errno */
99 /* Hmmm, the test for a directory is suspect here ... FIXME */
101 if (strlen(path
) > 0 && path
[strlen(path
) - 1] == '\\') {
102 status
= NT_STATUS_OBJECT_PATH_INVALID
;
104 file
= SMB_MALLOC_P(SMBCFILE
);
113 /*d_printf(">>>open: resolving %s\n", path);*/
114 status
= cli_resolve_path(
115 frame
, "", context
->internal
->auth_info
,
116 srv
->cli
, path
, &targetcli
, &targetpath
);
117 if (!NT_STATUS_IS_OK(status
)) {
118 d_printf("Could not resolve %s\n", path
);
124 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
126 status
= cli_open(targetcli
, targetpath
, flags
,
127 context
->internal
->share_mode
, &fd
);
128 if (!NT_STATUS_IS_OK(status
)) {
130 /* Handle the error ... */
133 errno
= SMBC_errno(context
, targetcli
);
138 /* Fill in file struct */
141 file
->fname
= SMB_STRDUP(fname
);
146 DLIST_ADD(context
->internal
->files
, file
);
149 * If the file was opened in O_APPEND mode, all write
150 * operations should be appended to the file. To do that,
151 * though, using this protocol, would require a getattrE()
152 * call for each and every write, to determine where the end
153 * of the file is. (There does not appear to be an append flag
154 * in the protocol.) Rather than add all of that overhead of
155 * retrieving the current end-of-file offset prior to each
156 * write operation, we'll assume that most append operations
157 * will continuously write, so we'll just set the offset to
158 * the end of the file now and hope that's adequate.
160 * Note to self: If this proves inadequate, and O_APPEND
161 * should, in some cases, be forced for each write, add a
162 * field in the context options structure, for
163 * "strict_append_mode" which would select between the current
164 * behavior (if FALSE) or issuing a getattrE() prior to each
165 * write and forcing the write to the end of the file (if
166 * TRUE). Adding that capability will likely require adding
167 * an "append" flag into the _SMBCFILE structure to track
168 * whether a file was opened in O_APPEND mode. -- djl
170 if (flags
& O_APPEND
) {
171 if (SMBC_lseek_ctx(context
, file
, 0, SEEK_END
) < 0) {
172 (void) SMBC_close_ctx(context
, file
);
183 /* Check if opendir needed ... */
185 if (!NT_STATUS_IS_OK(status
)) {
188 eno
= SMBC_errno(context
, srv
->cli
);
189 file
= smbc_getFunctionOpendir(context
)(context
, fname
);
190 if (!file
) errno
= eno
;
195 errno
= EINVAL
; /* FIXME, correct errno ? */
201 * Routine to create a file
205 SMBC_creat_ctx(SMBCCTX
*context
,
209 if (!context
|| !context
->internal
->initialized
) {
214 return SMBC_open_ctx(context
, path
,
215 O_WRONLY
| O_CREAT
| O_TRUNC
, mode
);
219 * Routine to read() a file ...
223 SMBC_read_ctx(SMBCCTX
*context
,
229 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
231 char *targetpath
= NULL
;
232 struct cli_state
*targetcli
= NULL
;
233 TALLOC_CTX
*frame
= talloc_stackframe();
239 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
240 * appears to pass file->offset (which is type off_t) differently than
241 * a local variable of type off_t. Using local variable "offset" in
242 * the call to cli_read() instead of file->offset fixes a problem
243 * retrieving data at an offset greater than 4GB.
247 if (!context
|| !context
->internal
->initialized
) {
253 DEBUG(4, ("smbc_read(%p, %d)\n", file
, (int)count
));
255 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
261 offset
= file
->offset
;
263 /* Check that the buffer exists ... */
271 /*d_printf(">>>read: parsing %s\n", file->fname);*/
272 if (SMBC_parse_path(frame
,
287 /*d_printf(">>>read: resolving %s\n", path);*/
288 status
= cli_resolve_path(frame
, "", context
->internal
->auth_info
,
289 file
->srv
->cli
, path
,
290 &targetcli
, &targetpath
);
291 if (!NT_STATUS_IS_OK(status
)) {
292 d_printf("Could not resolve %s\n", path
);
297 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
299 status
= cli_read(targetcli
, file
->cli_fd
, (char *)buf
, offset
,
301 if (!NT_STATUS_IS_OK(status
)) {
302 errno
= SMBC_errno(context
, targetcli
);
309 DEBUG(4, (" --> %ld\n", (unsigned long)ret
));
312 return ret
; /* Success, ret bytes of data ... */
316 * Routine to write() a file ...
320 SMBC_write_ctx(SMBCCTX
*context
,
326 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
328 char *targetpath
= NULL
;
329 struct cli_state
*targetcli
= NULL
;
330 TALLOC_CTX
*frame
= talloc_stackframe();
333 /* First check all pointers before dereferencing them */
335 if (!context
|| !context
->internal
->initialized
) {
341 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
347 /* Check that the buffer exists ... */
355 offset
= file
->offset
; /* See "offset" comment in SMBC_read_ctx() */
357 /*d_printf(">>>write: parsing %s\n", file->fname);*/
358 if (SMBC_parse_path(frame
,
373 /*d_printf(">>>write: resolving %s\n", path);*/
374 status
= cli_resolve_path(frame
, "", context
->internal
->auth_info
,
375 file
->srv
->cli
, path
,
376 &targetcli
, &targetpath
);
377 if (!NT_STATUS_IS_OK(status
)) {
378 d_printf("Could not resolve %s\n", path
);
383 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
385 status
= cli_writeall(targetcli
, file
->cli_fd
,
386 0, (const uint8_t *)buf
, offset
, count
, NULL
);
387 if (!NT_STATUS_IS_OK(status
)) {
388 errno
= map_errno_from_nt_status(status
);
393 file
->offset
+= count
;
396 return count
; /* Success, 0 bytes of data ... */
400 * Routine to close() a file ...
404 SMBC_close_ctx(SMBCCTX
*context
,
408 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
410 char *targetpath
= NULL
;
411 struct cli_state
*targetcli
= NULL
;
412 TALLOC_CTX
*frame
= talloc_stackframe();
415 if (!context
|| !context
->internal
->initialized
) {
421 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
430 return smbc_getFunctionClosedir(context
)(context
, file
);
433 /*d_printf(">>>close: parsing %s\n", file->fname);*/
434 if (SMBC_parse_path(frame
,
449 /*d_printf(">>>close: resolving %s\n", path);*/
450 status
= cli_resolve_path(frame
, "", context
->internal
->auth_info
,
451 file
->srv
->cli
, path
,
452 &targetcli
, &targetpath
);
453 if (!NT_STATUS_IS_OK(status
)) {
454 d_printf("Could not resolve %s\n", path
);
459 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
461 if (!NT_STATUS_IS_OK(cli_close(targetcli
, file
->cli_fd
))) {
462 DEBUG(3, ("cli_close failed on %s. purging server.\n",
464 /* Deallocate slot and remove the server
465 * from the server cache if unused */
466 errno
= SMBC_errno(context
, targetcli
);
468 DLIST_REMOVE(context
->internal
->files
, file
);
469 SAFE_FREE(file
->fname
);
471 smbc_getFunctionRemoveUnusedServer(context
)(context
, srv
);
476 DLIST_REMOVE(context
->internal
->files
, file
);
477 SAFE_FREE(file
->fname
);
484 * Get info from an SMB server on a file. Use a qpathinfo call first
485 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
488 SMBC_getatr(SMBCCTX
* context
,
493 struct timespec
*create_time_ts
,
494 struct timespec
*access_time_ts
,
495 struct timespec
*write_time_ts
,
496 struct timespec
*change_time_ts
,
499 char *fixedpath
= NULL
;
500 char *targetpath
= NULL
;
501 struct cli_state
*targetcli
= NULL
;
503 TALLOC_CTX
*frame
= talloc_stackframe();
506 if (!context
|| !context
->internal
->initialized
) {
512 /* path fixup for . and .. */
513 if (strequal(path
, ".") || strequal(path
, "..")) {
514 fixedpath
= talloc_strdup(frame
, "\\");
521 fixedpath
= talloc_strdup(frame
, path
);
527 trim_string(fixedpath
, NULL
, "\\..");
528 trim_string(fixedpath
, NULL
, "\\.");
530 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
532 status
= cli_resolve_path(frame
, "", context
->internal
->auth_info
,
534 &targetcli
, &targetpath
);
535 if (!NT_STATUS_IS_OK(status
)) {
536 d_printf("Couldn't resolve %s\n", path
);
542 if (!srv
->no_pathinfo2
&&
543 NT_STATUS_IS_OK(cli_qpathinfo2(targetcli
, targetpath
,
553 /* if this is NT then don't bother with the getatr */
554 if (cli_state_capabilities(targetcli
) & CAP_NT_SMBS
) {
560 if (NT_STATUS_IS_OK(cli_getatr(targetcli
, targetpath
, mode
, size
, &write_time
))) {
561 struct timespec w_time_ts
;
563 w_time_ts
= convert_time_t_to_timespec(write_time
);
564 if (write_time_ts
!= NULL
) {
565 *write_time_ts
= w_time_ts
;
567 if (create_time_ts
!= NULL
) {
568 *create_time_ts
= w_time_ts
;
570 if (access_time_ts
!= NULL
) {
571 *access_time_ts
= w_time_ts
;
573 if (change_time_ts
!= NULL
) {
574 *change_time_ts
= w_time_ts
;
576 srv
->no_pathinfo2
= True
;
587 * Set file info on an SMB server. Use setpathinfo call first. If that
588 * fails, use setattrE..
590 * Access and modification time parameters are always used and must be
591 * provided. Create time, if zero, will be determined from the actual create
592 * time of the file. If non-zero, the create time will be set as well.
594 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
597 SMBC_setatr(SMBCCTX
* context
, SMBCSRV
*srv
, char *path
,
606 TALLOC_CTX
*frame
= talloc_stackframe();
609 * First, try setpathinfo (if qpathinfo succeeded), for it is the
610 * modern function for "new code" to be using, and it works given a
611 * filename rather than requiring that the file be opened to have its
612 * attributes manipulated.
614 if (srv
->no_pathinfo
||
615 !NT_STATUS_IS_OK(cli_setpathinfo_basic(srv
->cli
, path
,
623 * setpathinfo is not supported; go to plan B.
625 * cli_setatr() does not work on win98, and it also doesn't
626 * support setting the access time (only the modification
627 * time), so in all cases, we open the specified file and use
628 * cli_setattrE() which should work on all OS versions, and
629 * supports both times.
632 /* Don't try {q,set}pathinfo() again, with this server */
633 srv
->no_pathinfo
= True
;
636 if (!NT_STATUS_IS_OK(cli_open(srv
->cli
, path
, O_RDWR
, DENY_NONE
, &fd
))) {
637 errno
= SMBC_errno(context
, srv
->cli
);
642 /* Set the new attributes */
643 ret
= NT_STATUS_IS_OK(cli_setattrE(srv
->cli
, fd
,
649 cli_close(srv
->cli
, fd
);
652 * Unfortunately, setattrE() doesn't have a provision for
653 * setting the access mode (attributes). We'll have to try
654 * cli_setatr() for that, and with only this parameter, it
655 * seems to work on win98.
657 if (ret
&& mode
!= (uint16
) -1) {
658 ret
= NT_STATUS_IS_OK(cli_setatr(srv
->cli
, path
, mode
, 0));
662 errno
= SMBC_errno(context
, srv
->cli
);
673 * A routine to lseek() a file
677 SMBC_lseek_ctx(SMBCCTX
*context
,
683 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
685 char *targetpath
= NULL
;
686 struct cli_state
*targetcli
= NULL
;
687 TALLOC_CTX
*frame
= talloc_stackframe();
690 if (!context
|| !context
->internal
->initialized
) {
696 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
705 return -1; /* Can't lseek a dir ... */
710 file
->offset
= offset
;
713 file
->offset
+= offset
;
716 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
717 if (SMBC_parse_path(frame
,
732 /*d_printf(">>>lseek: resolving %s\n", path);*/
733 status
= cli_resolve_path(
734 frame
, "", context
->internal
->auth_info
,
735 file
->srv
->cli
, path
, &targetcli
, &targetpath
);
736 if (!NT_STATUS_IS_OK(status
)) {
737 d_printf("Could not resolve %s\n", path
);
743 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
744 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
745 targetcli
, file
->cli_fd
, NULL
,
746 &size
, NULL
, NULL
, NULL
, NULL
,
748 SMB_OFF_T b_size
= size
;
749 if (!NT_STATUS_IS_OK(cli_getattrE(targetcli
, file
->cli_fd
,
750 NULL
, &b_size
, NULL
, NULL
, NULL
))) {
757 file
->offset
= size
+ offset
;
770 * Routine to truncate a file given by its file descriptor, to a specified size
774 SMBC_ftruncate_ctx(SMBCCTX
*context
,
778 SMB_OFF_T size
= length
;
782 char *password
= NULL
;
784 char *targetpath
= NULL
;
785 struct cli_state
*targetcli
= NULL
;
786 TALLOC_CTX
*frame
= talloc_stackframe();
789 if (!context
|| !context
->internal
->initialized
) {
795 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
807 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
808 if (SMBC_parse_path(frame
,
823 /*d_printf(">>>fstat: resolving %s\n", path);*/
824 status
= cli_resolve_path(frame
, "", context
->internal
->auth_info
,
825 file
->srv
->cli
, path
,
826 &targetcli
, &targetpath
);
827 if (!NT_STATUS_IS_OK(status
)) {
828 d_printf("Could not resolve %s\n", path
);
833 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
835 if (!NT_STATUS_IS_OK(cli_ftruncate(targetcli
, file
->cli_fd
, (uint64_t)size
))) {