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);
127 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
129 if ((fd = cli_open(targetcli, targetpath, flags,
130 context->internal->share_mode)) < 0) {
132 /* Handle the error ... */
135 errno = SMBC_errno(context, targetcli);
141 /* Fill in file struct */
144 file->fname = SMB_STRDUP(fname);
149 DLIST_ADD(context->internal->files, file);
152 * If the file was opened in O_APPEND mode, all write
153 * operations should be appended to the file. To do that,
154 * though, using this protocol, would require a getattrE()
155 * call for each and every write, to determine where the end
156 * of the file is. (There does not appear to be an append flag
157 * in the protocol.) Rather than add all of that overhead of
158 * retrieving the current end-of-file offset prior to each
159 * write operation, we'll assume that most append operations
160 * will continuously write, so we'll just set the offset to
161 * the end of the file now and hope that's adequate.
163 * Note to self: If this proves inadequate, and O_APPEND
164 * should, in some cases, be forced for each write, add a
165 * field in the context options structure, for
166 * "strict_append_mode" which would select between the current
167 * behavior (if FALSE) or issuing a getattrE() prior to each
168 * write and forcing the write to the end of the file (if
169 * TRUE). Adding that capability will likely require adding
170 * an "append" flag into the _SMBCFILE structure to track
171 * whether a file was opened in O_APPEND mode. -- djl
173 if (flags & O_APPEND) {
174 if (SMBC_lseek_ctx(context, file, 0, SEEK_END) < 0) {
175 (void) SMBC_close_ctx(context, file);
187 /* Check if opendir needed ... */
192 eno = SMBC_errno(context, srv->cli);
193 file = smbc_getFunctionOpendir(context)(context, fname);
194 if (!file) errno = eno;
200 errno = EINVAL; /* FIXME, correct errno ? */
207 * Routine to create a file
211 SMBC_creat_ctx(SMBCCTX *context,
216 if (!context || !context->internal->initialized) {
223 return SMBC_open_ctx(context, path,
224 O_WRONLY | O_CREAT | O_TRUNC, mode);
228 * Routine to read() a file ...
232 SMBC_read_ctx(SMBCCTX *context,
238 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
240 char *targetpath = NULL;
241 struct cli_state *targetcli = NULL;
242 TALLOC_CTX *frame = talloc_stackframe();
247 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
248 * appears to pass file->offset (which is type off_t) differently than
249 * a local variable of type off_t. Using local variable "offset" in
250 * the call to cli_read() instead of file->offset fixes a problem
251 * retrieving data at an offset greater than 4GB.
255 if (!context || !context->internal->initialized) {
263 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
265 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
272 offset = file->offset;
274 /* Check that the buffer exists ... */
283 /*d_printf(">>>read: parsing %s\n", file->fname);*/
284 if (SMBC_parse_path(frame,
299 /*d_printf(">>>read: resolving %s\n", path);*/
300 if (!cli_resolve_path(frame, "", context->internal->auth_info,
301 file->srv->cli, path,
302 &targetcli, &targetpath)) {
303 d_printf("Could not resolve %s\n", path);
308 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
310 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
314 errno = SMBC_errno(context, targetcli);
322 DEBUG(4, (" --> %d\n", ret));
325 return ret; /* Success, ret bytes of data ... */
330 * Routine to write() a file ...
334 SMBC_write_ctx(SMBCCTX *context,
341 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
343 char *targetpath = NULL;
344 struct cli_state *targetcli = NULL;
345 TALLOC_CTX *frame = talloc_stackframe();
347 /* First check all pointers before dereferencing them */
349 if (!context || !context->internal->initialized) {
357 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
363 /* Check that the buffer exists ... */
372 offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */
374 /*d_printf(">>>write: parsing %s\n", file->fname);*/
375 if (SMBC_parse_path(frame,
390 /*d_printf(">>>write: resolving %s\n", path);*/
391 if (!cli_resolve_path(frame, "", context->internal->auth_info,
392 file->srv->cli, path,
393 &targetcli, &targetpath)) {
394 d_printf("Could not resolve %s\n", path);
399 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
401 ret = cli_write(targetcli, file->cli_fd,
402 0, (char *)buf, offset, count);
405 errno = SMBC_errno(context, targetcli);
414 return ret; /* Success, 0 bytes of data ... */
418 * Routine to close() a file ...
422 SMBC_close_ctx(SMBCCTX *context,
426 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
428 char *targetpath = NULL;
429 struct cli_state *targetcli = NULL;
430 TALLOC_CTX *frame = talloc_stackframe();
432 if (!context || !context->internal->initialized) {
439 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
448 return smbc_getFunctionClosedir(context)(context, file);
451 /*d_printf(">>>close: parsing %s\n", file->fname);*/
452 if (SMBC_parse_path(frame,
467 /*d_printf(">>>close: resolving %s\n", path);*/
468 if (!cli_resolve_path(frame, "", context->internal->auth_info,
469 file->srv->cli, path,
470 &targetcli, &targetpath)) {
471 d_printf("Could not resolve %s\n", path);
476 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
478 if (!cli_close(targetcli, file->cli_fd)) {
480 DEBUG(3, ("cli_close failed on %s. purging server.\n",
482 /* Deallocate slot and remove the server
483 * from the server cache if unused */
484 errno = SMBC_errno(context, targetcli);
486 DLIST_REMOVE(context->internal->files, file);
487 SAFE_FREE(file->fname);
489 smbc_getFunctionRemoveUnusedServer(context)(context, srv);
495 DLIST_REMOVE(context->internal->files, file);
496 SAFE_FREE(file->fname);
504 * Get info from an SMB server on a file. Use a qpathinfo call first
505 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
508 SMBC_getatr(SMBCCTX * context,
513 struct timespec *create_time_ts,
514 struct timespec *access_time_ts,
515 struct timespec *write_time_ts,
516 struct timespec *change_time_ts,
519 char *fixedpath = NULL;
520 char *targetpath = NULL;
521 struct cli_state *targetcli = NULL;
523 TALLOC_CTX *frame = talloc_stackframe();
525 if (!context || !context->internal->initialized) {
532 /* path fixup for . and .. */
533 if (strequal(path, ".") || strequal(path, "..")) {
534 fixedpath = talloc_strdup(frame, "\\");
541 fixedpath = talloc_strdup(frame, path);
547 trim_string(fixedpath, NULL, "\\..");
548 trim_string(fixedpath, NULL, "\\.");
550 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
552 if (!cli_resolve_path(frame, "", context->internal->auth_info,
554 &targetcli, &targetpath)) {
555 d_printf("Couldn't resolve %s\n", path);
561 if (!srv->no_pathinfo2 &&
562 cli_qpathinfo2(targetcli, targetpath,
572 /* if this is NT then don't bother with the getatr */
573 if (targetcli->capabilities & CAP_NT_SMBS) {
579 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
581 struct timespec w_time_ts;
583 w_time_ts = convert_time_t_to_timespec(write_time);
585 if (write_time_ts != NULL) {
586 *write_time_ts = w_time_ts;
589 if (create_time_ts != NULL) {
590 *create_time_ts = w_time_ts;
593 if (access_time_ts != NULL) {
594 *access_time_ts = w_time_ts;
597 if (change_time_ts != NULL) {
598 *change_time_ts = w_time_ts;
601 srv->no_pathinfo2 = True;
613 * Set file info on an SMB server. Use setpathinfo call first. If that
614 * fails, use setattrE..
616 * Access and modification time parameters are always used and must be
617 * provided. Create time, if zero, will be determined from the actual create
618 * time of the file. If non-zero, the create time will be set as well.
620 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
623 SMBC_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
632 TALLOC_CTX *frame = talloc_stackframe();
635 * First, try setpathinfo (if qpathinfo succeeded), for it is the
636 * modern function for "new code" to be using, and it works given a
637 * filename rather than requiring that the file be opened to have its
638 * attributes manipulated.
640 if (srv->no_pathinfo ||
641 ! cli_setpathinfo(srv->cli, path,
649 * setpathinfo is not supported; go to plan B.
651 * cli_setatr() does not work on win98, and it also doesn't
652 * support setting the access time (only the modification
653 * time), so in all cases, we open the specified file and use
654 * cli_setattrE() which should work on all OS versions, and
655 * supports both times.
658 /* Don't try {q,set}pathinfo() again, with this server */
659 srv->no_pathinfo = True;
662 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
664 errno = SMBC_errno(context, srv->cli);
669 /* Set the new attributes */
670 ret = cli_setattrE(srv->cli, fd,
676 cli_close(srv->cli, fd);
679 * Unfortunately, setattrE() doesn't have a provision for
680 * setting the access mode (attributes). We'll have to try
681 * cli_setatr() for that, and with only this parameter, it
682 * seems to work on win98.
684 if (ret && mode != (uint16) -1) {
685 ret = cli_setatr(srv->cli, path, mode, 0);
689 errno = SMBC_errno(context, srv->cli);
700 * A routine to lseek() a file
704 SMBC_lseek_ctx(SMBCCTX *context,
710 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
712 char *targetpath = NULL;
713 struct cli_state *targetcli = NULL;
714 TALLOC_CTX *frame = talloc_stackframe();
716 if (!context || !context->internal->initialized) {
723 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
735 return -1; /* Can't lseek a dir ... */
741 file->offset = offset;
745 file->offset += offset;
749 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
750 if (SMBC_parse_path(frame,
765 /*d_printf(">>>lseek: resolving %s\n", path);*/
766 if (!cli_resolve_path(frame, "", context->internal->auth_info,
767 file->srv->cli, path,
768 &targetcli, &targetpath)) {
769 d_printf("Could not resolve %s\n", path);
774 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
776 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
777 &size, NULL, NULL, NULL, NULL, NULL))
779 SMB_OFF_T b_size = size;
780 if (!cli_getattrE(targetcli, file->cli_fd,
781 NULL, &b_size, NULL, NULL, NULL))
789 file->offset = size + offset;
805 * Routine to truncate a file given by its file descriptor, to a specified size
809 SMBC_ftruncate_ctx(SMBCCTX *context,
813 SMB_OFF_T size = length;
817 char *password = NULL;
819 char *targetpath = NULL;
820 struct cli_state *targetcli = NULL;
821 TALLOC_CTX *frame = talloc_stackframe();
823 if (!context || !context->internal->initialized) {
830 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
842 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
843 if (SMBC_parse_path(frame,
858 /*d_printf(">>>fstat: resolving %s\n", path);*/
859 if (!cli_resolve_path(frame, "", context->internal->auth_info,
860 file->srv->cli, path,
861 &targetcli, &targetpath)) {
862 d_printf("Could not resolve %s\n", path);
867 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
869 if (!cli_ftruncate(targetcli, file->cli_fd, size)) {