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
, "", srv
->cli
, path
,
119 &targetcli
, &targetpath
)) {
120 d_printf("Could not resolve %s\n", path
);
125 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
127 if ((fd
= cli_open(targetcli
, targetpath
, flags
,
128 context
->internal
->share_mode
)) < 0) {
130 /* Handle the error ... */
133 errno
= SMBC_errno(context
, targetcli
);
139 /* Fill in file struct */
142 file
->fname
= SMB_STRDUP(fname
);
147 DLIST_ADD(context
->internal
->files
, file
);
150 * If the file was opened in O_APPEND mode, all write
151 * operations should be appended to the file. To do that,
152 * though, using this protocol, would require a getattrE()
153 * call for each and every write, to determine where the end
154 * of the file is. (There does not appear to be an append flag
155 * in the protocol.) Rather than add all of that overhead of
156 * retrieving the current end-of-file offset prior to each
157 * write operation, we'll assume that most append operations
158 * will continuously write, so we'll just set the offset to
159 * the end of the file now and hope that's adequate.
161 * Note to self: If this proves inadequate, and O_APPEND
162 * should, in some cases, be forced for each write, add a
163 * field in the context options structure, for
164 * "strict_append_mode" which would select between the current
165 * behavior (if FALSE) or issuing a getattrE() prior to each
166 * write and forcing the write to the end of the file (if
167 * TRUE). Adding that capability will likely require adding
168 * an "append" flag into the _SMBCFILE structure to track
169 * whether a file was opened in O_APPEND mode. -- djl
171 if (flags
& O_APPEND
) {
172 if (SMBC_lseek_ctx(context
, file
, 0, SEEK_END
) < 0) {
173 (void) SMBC_close_ctx(context
, file
);
185 /* Check if opendir needed ... */
190 eno
= SMBC_errno(context
, srv
->cli
);
191 file
= smbc_getFunctionOpendir(context
)(context
, fname
);
192 if (!file
) errno
= eno
;
198 errno
= EINVAL
; /* FIXME, correct errno ? */
205 * Routine to create a file
209 SMBC_creat_ctx(SMBCCTX
*context
,
214 if (!context
|| !context
->internal
->initialized
) {
221 return SMBC_open_ctx(context
, path
,
222 O_WRONLY
| O_CREAT
| O_TRUNC
, mode
);
226 * Routine to read() a file ...
230 SMBC_read_ctx(SMBCCTX
*context
,
236 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
238 char *targetpath
= NULL
;
239 struct cli_state
*targetcli
= NULL
;
240 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
) {
261 DEBUG(4, ("smbc_read(%p, %d)\n", file
, (int)count
));
263 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
270 offset
= file
->offset
;
272 /* Check that the buffer exists ... */
281 /*d_printf(">>>read: parsing %s\n", file->fname);*/
282 if (SMBC_parse_path(frame
,
297 /*d_printf(">>>read: resolving %s\n", path);*/
298 if (!cli_resolve_path(frame
, "", file
->srv
->cli
, path
,
299 &targetcli
, &targetpath
)) {
300 d_printf("Could not resolve %s\n", path
);
304 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
306 ret
= cli_read(targetcli
, file
->cli_fd
, (char *)buf
, offset
, count
);
310 errno
= SMBC_errno(context
, targetcli
);
318 DEBUG(4, (" --> %d\n", ret
));
321 return ret
; /* Success, ret bytes of data ... */
326 * Routine to write() a file ...
330 SMBC_write_ctx(SMBCCTX
*context
,
337 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
339 char *targetpath
= NULL
;
340 struct cli_state
*targetcli
= NULL
;
341 TALLOC_CTX
*frame
= talloc_stackframe();
343 /* First check all pointers before dereferencing them */
345 if (!context
|| !context
->internal
->initialized
) {
353 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
359 /* Check that the buffer exists ... */
368 offset
= file
->offset
; /* See "offset" comment in SMBC_read_ctx() */
370 /*d_printf(">>>write: parsing %s\n", file->fname);*/
371 if (SMBC_parse_path(frame
,
386 /*d_printf(">>>write: resolving %s\n", path);*/
387 if (!cli_resolve_path(frame
, "", file
->srv
->cli
, path
,
388 &targetcli
, &targetpath
)) {
389 d_printf("Could not resolve %s\n", path
);
393 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
395 ret
= cli_write(targetcli
, file
->cli_fd
,
396 0, (char *)buf
, offset
, count
);
399 errno
= SMBC_errno(context
, targetcli
);
408 return ret
; /* Success, 0 bytes of data ... */
412 * Routine to close() a file ...
416 SMBC_close_ctx(SMBCCTX
*context
,
420 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
422 char *targetpath
= NULL
;
423 struct cli_state
*targetcli
= NULL
;
424 TALLOC_CTX
*frame
= talloc_stackframe();
426 if (!context
|| !context
->internal
->initialized
) {
433 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
442 return smbc_getFunctionClosedir(context
)(context
, file
);
445 /*d_printf(">>>close: parsing %s\n", file->fname);*/
446 if (SMBC_parse_path(frame
,
461 /*d_printf(">>>close: resolving %s\n", path);*/
462 if (!cli_resolve_path(frame
, "", file
->srv
->cli
, path
,
463 &targetcli
, &targetpath
)) {
464 d_printf("Could not resolve %s\n", path
);
468 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
470 if (!cli_close(targetcli
, file
->cli_fd
)) {
472 DEBUG(3, ("cli_close failed on %s. purging server.\n",
474 /* Deallocate slot and remove the server
475 * from the server cache if unused */
476 errno
= SMBC_errno(context
, targetcli
);
478 DLIST_REMOVE(context
->internal
->files
, file
);
479 SAFE_FREE(file
->fname
);
481 smbc_getFunctionRemoveUnusedServer(context
)(context
, srv
);
487 DLIST_REMOVE(context
->internal
->files
, file
);
488 SAFE_FREE(file
->fname
);
496 * Get info from an SMB server on a file. Use a qpathinfo call first
497 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
500 SMBC_getatr(SMBCCTX
* context
,
505 struct timespec
*create_time_ts
,
506 struct timespec
*access_time_ts
,
507 struct timespec
*write_time_ts
,
508 struct timespec
*change_time_ts
,
511 char *fixedpath
= NULL
;
512 char *targetpath
= NULL
;
513 struct cli_state
*targetcli
= NULL
;
515 TALLOC_CTX
*frame
= talloc_stackframe();
517 if (!context
|| !context
->internal
->initialized
) {
524 /* path fixup for . and .. */
525 if (strequal(path
, ".") || strequal(path
, "..")) {
526 fixedpath
= talloc_strdup(frame
, "\\");
533 fixedpath
= talloc_strdup(frame
, path
);
539 trim_string(fixedpath
, NULL
, "\\..");
540 trim_string(fixedpath
, NULL
, "\\.");
542 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
544 if (!cli_resolve_path(frame
, "", srv
->cli
, fixedpath
,
545 &targetcli
, &targetpath
)) {
546 d_printf("Couldn't resolve %s\n", path
);
551 if (!srv
->no_pathinfo2
&&
552 cli_qpathinfo2(targetcli
, targetpath
,
562 /* if this is NT then don't bother with the getatr */
563 if (targetcli
->capabilities
& CAP_NT_SMBS
) {
569 if (cli_getatr(targetcli
, targetpath
, mode
, size
, &write_time
)) {
571 struct timespec w_time_ts
;
573 w_time_ts
= convert_time_t_to_timespec(write_time
);
575 if (write_time_ts
!= NULL
) {
576 *write_time_ts
= w_time_ts
;
579 if (create_time_ts
!= NULL
) {
580 *create_time_ts
= w_time_ts
;
583 if (access_time_ts
!= NULL
) {
584 *access_time_ts
= w_time_ts
;
587 if (change_time_ts
!= NULL
) {
588 *change_time_ts
= w_time_ts
;
591 srv
->no_pathinfo2
= True
;
603 * Set file info on an SMB server. Use setpathinfo call first. If that
604 * fails, use setattrE..
606 * Access and modification time parameters are always used and must be
607 * provided. Create time, if zero, will be determined from the actual create
608 * time of the file. If non-zero, the create time will be set as well.
610 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
613 SMBC_setatr(SMBCCTX
* context
, SMBCSRV
*srv
, char *path
,
622 TALLOC_CTX
*frame
= talloc_stackframe();
625 * First, try setpathinfo (if qpathinfo succeeded), for it is the
626 * modern function for "new code" to be using, and it works given a
627 * filename rather than requiring that the file be opened to have its
628 * attributes manipulated.
630 if (srv
->no_pathinfo
||
631 ! cli_setpathinfo(srv
->cli
, path
,
639 * setpathinfo is not supported; go to plan B.
641 * cli_setatr() does not work on win98, and it also doesn't
642 * support setting the access time (only the modification
643 * time), so in all cases, we open the specified file and use
644 * cli_setattrE() which should work on all OS versions, and
645 * supports both times.
648 /* Don't try {q,set}pathinfo() again, with this server */
649 srv
->no_pathinfo
= True
;
652 if ((fd
= cli_open(srv
->cli
, path
, O_RDWR
, DENY_NONE
)) < 0) {
654 errno
= SMBC_errno(context
, srv
->cli
);
659 /* Set the new attributes */
660 ret
= cli_setattrE(srv
->cli
, fd
,
666 cli_close(srv
->cli
, fd
);
669 * Unfortunately, setattrE() doesn't have a provision for
670 * setting the access mode (attributes). We'll have to try
671 * cli_setatr() for that, and with only this parameter, it
672 * seems to work on win98.
674 if (ret
&& mode
!= (uint16
) -1) {
675 ret
= cli_setatr(srv
->cli
, path
, mode
, 0);
679 errno
= SMBC_errno(context
, srv
->cli
);
690 * A routine to lseek() a file
694 SMBC_lseek_ctx(SMBCCTX
*context
,
700 char *server
= NULL
, *share
= NULL
, *user
= NULL
, *password
= NULL
;
702 char *targetpath
= NULL
;
703 struct cli_state
*targetcli
= NULL
;
704 TALLOC_CTX
*frame
= talloc_stackframe();
706 if (!context
|| !context
->internal
->initialized
) {
713 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
725 return -1; /* Can't lseek a dir ... */
731 file
->offset
= offset
;
735 file
->offset
+= offset
;
739 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
740 if (SMBC_parse_path(frame
,
755 /*d_printf(">>>lseek: resolving %s\n", path);*/
756 if (!cli_resolve_path(frame
, "", file
->srv
->cli
, path
,
757 &targetcli
, &targetpath
)) {
758 d_printf("Could not resolve %s\n", path
);
762 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
764 if (!cli_qfileinfo(targetcli
, file
->cli_fd
, NULL
,
765 &size
, NULL
, NULL
, NULL
, NULL
, NULL
))
767 SMB_OFF_T b_size
= size
;
768 if (!cli_getattrE(targetcli
, file
->cli_fd
,
769 NULL
, &b_size
, NULL
, NULL
, NULL
))
777 file
->offset
= size
+ offset
;
793 * Routine to truncate a file given by its file descriptor, to a specified size
797 SMBC_ftruncate_ctx(SMBCCTX
*context
,
801 SMB_OFF_T size
= length
;
805 char *password
= NULL
;
807 char *targetpath
= NULL
;
808 struct cli_state
*targetcli
= NULL
;
809 TALLOC_CTX
*frame
= talloc_stackframe();
811 if (!context
|| !context
->internal
->initialized
) {
818 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
830 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
831 if (SMBC_parse_path(frame
,
846 /*d_printf(">>>fstat: resolving %s\n", path);*/
847 if (!cli_resolve_path(frame
, "", file
->srv
->cli
, path
,
848 &targetcli
, &targetpath
)) {
849 d_printf("Could not resolve %s\n", path
);
853 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
855 if (!cli_ftruncate(targetcli
, file
->cli_fd
, size
)) {