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 "libsmbclient.h"
27 #include "libsmb_internal.h"
31 * Routine to open() a file ...
35 SMBC_open_ctx(SMBCCTX
*context
,
43 char *password
= NULL
;
44 char *workgroup
= NULL
;
46 char *targetpath
= NULL
;
47 struct cli_state
*targetcli
= NULL
;
49 SMBCFILE
*file
= NULL
;
51 TALLOC_CTX
*frame
= talloc_stackframe();
53 if (!context
|| !context
->internal
->initialized
) {
55 errno
= EINVAL
; /* Best I can think of ... */
69 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
, share
, &workgroup
, &user
, &password
);
97 if (errno
== EPERM
) errno
= EACCES
;
99 return NULL
; /* SMBC_server sets errno */
102 /* Hmmm, the test for a directory is suspect here ... FIXME */
104 if (strlen(path
) > 0 && path
[strlen(path
) - 1] == '\\') {
107 file
= SMB_MALLOC_P(SMBCFILE
);
117 /*d_printf(">>>open: resolving %s\n", path);*/
118 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
120 &targetcli
, &targetpath
)) {
121 d_printf("Could not resolve %s\n", path
);
126 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
128 if ((fd
= cli_open(targetcli
, targetpath
, flags
,
129 context
->internal
->share_mode
)) < 0) {
131 /* Handle the error ... */
134 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
);
186 /* Check if opendir needed ... */
191 eno
= SMBC_errno(context
, srv
->cli
);
192 file
= smbc_getFunctionOpendir(context
)(context
, fname
);
193 if (!file
) errno
= eno
;
199 errno
= EINVAL
; /* FIXME, correct errno ? */
206 * Routine to create a file
210 SMBC_creat_ctx(SMBCCTX
*context
,
215 if (!context
|| !context
->internal
->initialized
) {
222 return SMBC_open_ctx(context
, path
,
223 O_WRONLY
| O_CREAT
| O_TRUNC
, mode
);
227 * Routine to read() a file ...
231 SMBC_read_ctx(SMBCCTX
*context
,
237 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
239 char *targetpath
= NULL
;
240 struct cli_state
*targetcli
= NULL
;
241 TALLOC_CTX
*frame
= talloc_stackframe();
246 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
247 * appears to pass file->offset (which is type off_t) differently than
248 * a local variable of type off_t. Using local variable "offset" in
249 * the call to cli_read() instead of file->offset fixes a problem
250 * retrieving data at an offset greater than 4GB.
254 if (!context
|| !context
->internal
->initialized
) {
262 DEBUG(4, ("smbc_read(%p, %d)\n", file
, (int)count
));
264 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
271 offset
= file
->offset
;
273 /* Check that the buffer exists ... */
282 /*d_printf(">>>read: parsing %s\n", file->fname);*/
283 if (SMBC_parse_path(frame
,
298 /*d_printf(">>>read: resolving %s\n", path);*/
299 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
300 file
->srv
->cli
, path
,
301 &targetcli
, &targetpath
)) {
302 d_printf("Could not resolve %s\n", path
);
306 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
308 ret
= cli_read(targetcli
, file
->cli_fd
, (char *)buf
, offset
, count
);
312 errno
= SMBC_errno(context
, targetcli
);
320 DEBUG(4, (" --> %d\n", ret
));
323 return ret
; /* Success, ret bytes of data ... */
328 * Routine to write() a file ...
332 SMBC_write_ctx(SMBCCTX
*context
,
339 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
341 char *targetpath
= NULL
;
342 struct cli_state
*targetcli
= NULL
;
343 TALLOC_CTX
*frame
= talloc_stackframe();
345 /* First check all pointers before dereferencing them */
347 if (!context
|| !context
->internal
->initialized
) {
355 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
361 /* Check that the buffer exists ... */
370 offset
= file
->offset
; /* See "offset" comment in SMBC_read_ctx() */
372 /*d_printf(">>>write: parsing %s\n", file->fname);*/
373 if (SMBC_parse_path(frame
,
388 /*d_printf(">>>write: resolving %s\n", path);*/
389 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
390 file
->srv
->cli
, path
,
391 &targetcli
, &targetpath
)) {
392 d_printf("Could not resolve %s\n", path
);
396 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
398 ret
= cli_write(targetcli
, file
->cli_fd
,
399 0, (char *)buf
, offset
, count
);
402 errno
= SMBC_errno(context
, targetcli
);
411 return ret
; /* Success, 0 bytes of data ... */
415 * Routine to close() a file ...
419 SMBC_close_ctx(SMBCCTX
*context
,
423 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
425 char *targetpath
= NULL
;
426 struct cli_state
*targetcli
= NULL
;
427 TALLOC_CTX
*frame
= talloc_stackframe();
429 if (!context
|| !context
->internal
->initialized
) {
436 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
445 return smbc_getFunctionClosedir(context
)(context
, file
);
448 /*d_printf(">>>close: parsing %s\n", file->fname);*/
449 if (SMBC_parse_path(frame
,
464 /*d_printf(">>>close: resolving %s\n", path);*/
465 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
466 file
->srv
->cli
, path
,
467 &targetcli
, &targetpath
)) {
468 d_printf("Could not resolve %s\n", path
);
472 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
474 if (!cli_close(targetcli
, file
->cli_fd
)) {
476 DEBUG(3, ("cli_close failed on %s. purging server.\n",
478 /* Deallocate slot and remove the server
479 * from the server cache if unused */
480 errno
= SMBC_errno(context
, targetcli
);
482 DLIST_REMOVE(context
->internal
->files
, file
);
483 SAFE_FREE(file
->fname
);
485 smbc_getFunctionRemoveUnusedServer(context
)(context
, srv
);
491 DLIST_REMOVE(context
->internal
->files
, file
);
492 SAFE_FREE(file
->fname
);
500 * Get info from an SMB server on a file. Use a qpathinfo call first
501 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
504 SMBC_getatr(SMBCCTX
* context
,
509 struct timespec
*create_time_ts
,
510 struct timespec
*access_time_ts
,
511 struct timespec
*write_time_ts
,
512 struct timespec
*change_time_ts
,
515 char *fixedpath
= NULL
;
516 char *targetpath
= NULL
;
517 struct cli_state
*targetcli
= NULL
;
519 TALLOC_CTX
*frame
= talloc_stackframe();
521 if (!context
|| !context
->internal
->initialized
) {
528 /* path fixup for . and .. */
529 if (strequal(path
, ".") || strequal(path
, "..")) {
530 fixedpath
= talloc_strdup(frame
, "\\");
537 fixedpath
= talloc_strdup(frame
, path
);
543 trim_string(fixedpath
, NULL
, "\\..");
544 trim_string(fixedpath
, NULL
, "\\.");
546 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
548 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
550 &targetcli
, &targetpath
)) {
551 d_printf("Couldn't resolve %s\n", path
);
556 if (!srv
->no_pathinfo2
&&
557 cli_qpathinfo2(targetcli
, targetpath
,
567 /* if this is NT then don't bother with the getatr */
568 if (targetcli
->capabilities
& CAP_NT_SMBS
) {
574 if (cli_getatr(targetcli
, targetpath
, mode
, size
, &write_time
)) {
576 struct timespec w_time_ts
;
578 w_time_ts
= convert_time_t_to_timespec(write_time
);
580 if (write_time_ts
!= NULL
) {
581 *write_time_ts
= w_time_ts
;
584 if (create_time_ts
!= NULL
) {
585 *create_time_ts
= w_time_ts
;
588 if (access_time_ts
!= NULL
) {
589 *access_time_ts
= w_time_ts
;
592 if (change_time_ts
!= NULL
) {
593 *change_time_ts
= w_time_ts
;
596 srv
->no_pathinfo2
= True
;
608 * Set file info on an SMB server. Use setpathinfo call first. If that
609 * fails, use setattrE..
611 * Access and modification time parameters are always used and must be
612 * provided. Create time, if zero, will be determined from the actual create
613 * time of the file. If non-zero, the create time will be set as well.
615 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
618 SMBC_setatr(SMBCCTX
* context
, SMBCSRV
*srv
, char *path
,
627 TALLOC_CTX
*frame
= talloc_stackframe();
630 * First, try setpathinfo (if qpathinfo succeeded), for it is the
631 * modern function for "new code" to be using, and it works given a
632 * filename rather than requiring that the file be opened to have its
633 * attributes manipulated.
635 if (srv
->no_pathinfo
||
636 ! cli_setpathinfo(srv
->cli
, path
,
644 * setpathinfo is not supported; go to plan B.
646 * cli_setatr() does not work on win98, and it also doesn't
647 * support setting the access time (only the modification
648 * time), so in all cases, we open the specified file and use
649 * cli_setattrE() which should work on all OS versions, and
650 * supports both times.
653 /* Don't try {q,set}pathinfo() again, with this server */
654 srv
->no_pathinfo
= True
;
657 if ((fd
= cli_open(srv
->cli
, path
, O_RDWR
, DENY_NONE
)) < 0) {
659 errno
= SMBC_errno(context
, srv
->cli
);
664 /* Set the new attributes */
665 ret
= cli_setattrE(srv
->cli
, fd
,
671 cli_close(srv
->cli
, fd
);
674 * Unfortunately, setattrE() doesn't have a provision for
675 * setting the access mode (attributes). We'll have to try
676 * cli_setatr() for that, and with only this parameter, it
677 * seems to work on win98.
679 if (ret
&& mode
!= (uint16
) -1) {
680 ret
= cli_setatr(srv
->cli
, path
, mode
, 0);
684 errno
= SMBC_errno(context
, srv
->cli
);
695 * A routine to lseek() a file
699 SMBC_lseek_ctx(SMBCCTX
*context
,
705 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
707 char *targetpath
= NULL
;
708 struct cli_state
*targetcli
= NULL
;
709 TALLOC_CTX
*frame
= talloc_stackframe();
711 if (!context
|| !context
->internal
->initialized
) {
718 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
730 return -1; /* Can't lseek a dir ... */
736 file
->offset
= offset
;
740 file
->offset
+= offset
;
744 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
745 if (SMBC_parse_path(frame
,
760 /*d_printf(">>>lseek: resolving %s\n", path);*/
761 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
762 file
->srv
->cli
, path
,
763 &targetcli
, &targetpath
)) {
764 d_printf("Could not resolve %s\n", path
);
768 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
770 if (!cli_qfileinfo(targetcli
, file
->cli_fd
, NULL
,
771 &size
, NULL
, NULL
, NULL
, NULL
, NULL
))
773 SMB_OFF_T b_size
= size
;
774 if (!cli_getattrE(targetcli
, file
->cli_fd
,
775 NULL
, &b_size
, NULL
, NULL
, NULL
))
783 file
->offset
= size
+ offset
;
799 * Routine to truncate a file given by its file descriptor, to a specified size
803 SMBC_ftruncate_ctx(SMBCCTX
*context
,
807 SMB_OFF_T size
= length
;
811 char *password
= NULL
;
813 char *targetpath
= NULL
;
814 struct cli_state
*targetcli
= NULL
;
815 TALLOC_CTX
*frame
= talloc_stackframe();
817 if (!context
|| !context
->internal
->initialized
) {
824 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
836 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
837 if (SMBC_parse_path(frame
,
852 /*d_printf(">>>fstat: resolving %s\n", path);*/
853 if (!cli_resolve_path(frame
, "", context
->internal
->auth_info
,
854 file
->srv
->cli
, path
,
855 &targetcli
, &targetpath
)) {
856 d_printf("Could not resolve %s\n", path
);
860 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
862 if (!cli_ftruncate(targetcli
, file
->cli_fd
, size
)) {