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 NTSTATUS status = NT_STATUS_OBJECT_PATH_INVALID;
52 TALLOC_CTX *frame = talloc_stackframe();
54 if (!context || !context->internal->initialized) {
56 errno = EINVAL; /* Best I can think of ... */
70 if (SMBC_parse_path(frame,
85 if (!user || user[0] == (char)0) {
86 user = talloc_strdup(frame, smbc_getUser(context));
94 srv = SMBC_server(frame, context, True,
95 server, share, &workgroup, &user, &password);
98 if (errno == EPERM) errno = EACCES;
100 return NULL; /* SMBC_server sets errno */
103 /* Hmmm, the test for a directory is suspect here ... FIXME */
105 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
106 status = NT_STATUS_OBJECT_PATH_INVALID;
108 file = SMB_MALLOC_P(SMBCFILE);
118 /*d_printf(">>>open: resolving %s\n", path);*/
119 if (!cli_resolve_path(frame, "", context->internal->auth_info,
121 &targetcli, &targetpath)) {
122 d_printf("Could not resolve %s\n", path);
128 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
130 status = cli_open(targetcli, targetpath, flags,
131 context->internal->share_mode, &fd);
132 if (!NT_STATUS_IS_OK(status)) {
134 /* Handle the error ... */
137 errno = SMBC_errno(context, targetcli);
143 /* Fill in file struct */
146 file->fname = SMB_STRDUP(fname);
151 DLIST_ADD(context->internal->files, file);
154 * If the file was opened in O_APPEND mode, all write
155 * operations should be appended to the file. To do that,
156 * though, using this protocol, would require a getattrE()
157 * call for each and every write, to determine where the end
158 * of the file is. (There does not appear to be an append flag
159 * in the protocol.) Rather than add all of that overhead of
160 * retrieving the current end-of-file offset prior to each
161 * write operation, we'll assume that most append operations
162 * will continuously write, so we'll just set the offset to
163 * the end of the file now and hope that's adequate.
165 * Note to self: If this proves inadequate, and O_APPEND
166 * should, in some cases, be forced for each write, add a
167 * field in the context options structure, for
168 * "strict_append_mode" which would select between the current
169 * behavior (if FALSE) or issuing a getattrE() prior to each
170 * write and forcing the write to the end of the file (if
171 * TRUE). Adding that capability will likely require adding
172 * an "append" flag into the _SMBCFILE structure to track
173 * whether a file was opened in O_APPEND mode. -- djl
175 if (flags & O_APPEND) {
176 if (SMBC_lseek_ctx(context, file, 0, SEEK_END) < 0) {
177 (void) SMBC_close_ctx(context, file);
189 /* Check if opendir needed ... */
191 if (!NT_STATUS_IS_OK(status)) {
194 eno = SMBC_errno(context, srv->cli);
195 file = smbc_getFunctionOpendir(context)(context, fname);
196 if (!file) errno = eno;
202 errno = EINVAL; /* FIXME, correct errno ? */
209 * Routine to create a file
213 SMBC_creat_ctx(SMBCCTX *context,
218 if (!context || !context->internal->initialized) {
225 return SMBC_open_ctx(context, path,
226 O_WRONLY | O_CREAT | O_TRUNC, mode);
230 * Routine to read() a file ...
234 SMBC_read_ctx(SMBCCTX *context,
240 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
242 char *targetpath = NULL;
243 struct cli_state *targetcli = NULL;
244 TALLOC_CTX *frame = talloc_stackframe();
249 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
250 * appears to pass file->offset (which is type off_t) differently than
251 * a local variable of type off_t. Using local variable "offset" in
252 * the call to cli_read() instead of file->offset fixes a problem
253 * retrieving data at an offset greater than 4GB.
257 if (!context || !context->internal->initialized) {
265 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
267 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
274 offset = file->offset;
276 /* Check that the buffer exists ... */
285 /*d_printf(">>>read: parsing %s\n", file->fname);*/
286 if (SMBC_parse_path(frame,
301 /*d_printf(">>>read: resolving %s\n", path);*/
302 if (!cli_resolve_path(frame, "", context->internal->auth_info,
303 file->srv->cli, path,
304 &targetcli, &targetpath)) {
305 d_printf("Could not resolve %s\n", path);
310 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
312 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
316 errno = SMBC_errno(context, targetcli);
324 DEBUG(4, (" --> %d\n", ret));
327 return ret; /* Success, ret bytes of data ... */
332 * Routine to write() a file ...
336 SMBC_write_ctx(SMBCCTX *context,
343 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
345 char *targetpath = NULL;
346 struct cli_state *targetcli = NULL;
347 TALLOC_CTX *frame = talloc_stackframe();
349 /* First check all pointers before dereferencing them */
351 if (!context || !context->internal->initialized) {
359 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
365 /* Check that the buffer exists ... */
374 offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */
376 /*d_printf(">>>write: parsing %s\n", file->fname);*/
377 if (SMBC_parse_path(frame,
392 /*d_printf(">>>write: resolving %s\n", path);*/
393 if (!cli_resolve_path(frame, "", context->internal->auth_info,
394 file->srv->cli, path,
395 &targetcli, &targetpath)) {
396 d_printf("Could not resolve %s\n", path);
401 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
403 ret = cli_write(targetcli, file->cli_fd,
404 0, (char *)buf, offset, count);
407 errno = SMBC_errno(context, targetcli);
416 return ret; /* Success, 0 bytes of data ... */
420 * Routine to close() a file ...
424 SMBC_close_ctx(SMBCCTX *context,
428 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
430 char *targetpath = NULL;
431 struct cli_state *targetcli = NULL;
432 TALLOC_CTX *frame = talloc_stackframe();
434 if (!context || !context->internal->initialized) {
441 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
450 return smbc_getFunctionClosedir(context)(context, file);
453 /*d_printf(">>>close: parsing %s\n", file->fname);*/
454 if (SMBC_parse_path(frame,
469 /*d_printf(">>>close: resolving %s\n", path);*/
470 if (!cli_resolve_path(frame, "", context->internal->auth_info,
471 file->srv->cli, path,
472 &targetcli, &targetpath)) {
473 d_printf("Could not resolve %s\n", path);
478 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
480 if (!NT_STATUS_IS_OK(cli_close(targetcli, file->cli_fd))) {
482 DEBUG(3, ("cli_close failed on %s. purging server.\n",
484 /* Deallocate slot and remove the server
485 * from the server cache if unused */
486 errno = SMBC_errno(context, targetcli);
488 DLIST_REMOVE(context->internal->files, file);
489 SAFE_FREE(file->fname);
491 smbc_getFunctionRemoveUnusedServer(context)(context, srv);
497 DLIST_REMOVE(context->internal->files, file);
498 SAFE_FREE(file->fname);
506 * Get info from an SMB server on a file. Use a qpathinfo call first
507 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
510 SMBC_getatr(SMBCCTX * context,
515 struct timespec *create_time_ts,
516 struct timespec *access_time_ts,
517 struct timespec *write_time_ts,
518 struct timespec *change_time_ts,
521 char *fixedpath = NULL;
522 char *targetpath = NULL;
523 struct cli_state *targetcli = NULL;
525 TALLOC_CTX *frame = talloc_stackframe();
527 if (!context || !context->internal->initialized) {
534 /* path fixup for . and .. */
535 if (strequal(path, ".") || strequal(path, "..")) {
536 fixedpath = talloc_strdup(frame, "\\");
543 fixedpath = talloc_strdup(frame, path);
549 trim_string(fixedpath, NULL, "\\..");
550 trim_string(fixedpath, NULL, "\\.");
552 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
554 if (!cli_resolve_path(frame, "", context->internal->auth_info,
556 &targetcli, &targetpath)) {
557 d_printf("Couldn't resolve %s\n", path);
563 if (!srv->no_pathinfo2 &&
564 cli_qpathinfo2(targetcli, targetpath,
574 /* if this is NT then don't bother with the getatr */
575 if (targetcli->capabilities & CAP_NT_SMBS) {
581 if (NT_STATUS_IS_OK(cli_getatr(targetcli, targetpath, mode, size, &write_time))) {
583 struct timespec w_time_ts;
585 w_time_ts = convert_time_t_to_timespec(write_time);
587 if (write_time_ts != NULL) {
588 *write_time_ts = w_time_ts;
591 if (create_time_ts != NULL) {
592 *create_time_ts = w_time_ts;
595 if (access_time_ts != NULL) {
596 *access_time_ts = w_time_ts;
599 if (change_time_ts != NULL) {
600 *change_time_ts = w_time_ts;
603 srv->no_pathinfo2 = True;
615 * Set file info on an SMB server. Use setpathinfo call first. If that
616 * fails, use setattrE..
618 * Access and modification time parameters are always used and must be
619 * provided. Create time, if zero, will be determined from the actual create
620 * time of the file. If non-zero, the create time will be set as well.
622 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
625 SMBC_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
634 TALLOC_CTX *frame = talloc_stackframe();
637 * First, try setpathinfo (if qpathinfo succeeded), for it is the
638 * modern function for "new code" to be using, and it works given a
639 * filename rather than requiring that the file be opened to have its
640 * attributes manipulated.
642 if (srv->no_pathinfo ||
643 ! cli_setpathinfo(srv->cli, path,
651 * setpathinfo is not supported; go to plan B.
653 * cli_setatr() does not work on win98, and it also doesn't
654 * support setting the access time (only the modification
655 * time), so in all cases, we open the specified file and use
656 * cli_setattrE() which should work on all OS versions, and
657 * supports both times.
660 /* Don't try {q,set}pathinfo() again, with this server */
661 srv->no_pathinfo = True;
664 if (!NT_STATUS_IS_OK(cli_open(srv->cli, path, O_RDWR, DENY_NONE, &fd))) {
666 errno = SMBC_errno(context, srv->cli);
671 /* Set the new attributes */
672 ret = NT_STATUS_IS_OK(cli_setattrE(srv->cli, fd,
678 cli_close(srv->cli, fd);
681 * Unfortunately, setattrE() doesn't have a provision for
682 * setting the access mode (attributes). We'll have to try
683 * cli_setatr() for that, and with only this parameter, it
684 * seems to work on win98.
686 if (ret && mode != (uint16) -1) {
687 ret = cli_setatr(srv->cli, path, mode, 0);
691 errno = SMBC_errno(context, srv->cli);
702 * A routine to lseek() a file
706 SMBC_lseek_ctx(SMBCCTX *context,
712 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
714 char *targetpath = NULL;
715 struct cli_state *targetcli = NULL;
716 TALLOC_CTX *frame = talloc_stackframe();
718 if (!context || !context->internal->initialized) {
725 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
737 return -1; /* Can't lseek a dir ... */
743 file->offset = offset;
747 file->offset += offset;
751 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
752 if (SMBC_parse_path(frame,
767 /*d_printf(">>>lseek: resolving %s\n", path);*/
768 if (!cli_resolve_path(frame, "", context->internal->auth_info,
769 file->srv->cli, path,
770 &targetcli, &targetpath)) {
771 d_printf("Could not resolve %s\n", path);
776 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
778 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
779 &size, NULL, NULL, NULL, NULL, NULL))
781 SMB_OFF_T b_size = size;
782 if (!NT_STATUS_IS_OK(cli_getattrE(targetcli, file->cli_fd,
783 NULL, &b_size, NULL, NULL, NULL))) {
790 file->offset = size + offset;
806 * Routine to truncate a file given by its file descriptor, to a specified size
810 SMBC_ftruncate_ctx(SMBCCTX *context,
814 SMB_OFF_T size = length;
818 char *password = NULL;
820 char *targetpath = NULL;
821 struct cli_state *targetcli = NULL;
822 TALLOC_CTX *frame = talloc_stackframe();
824 if (!context || !context->internal->initialized) {
831 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
843 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
844 if (SMBC_parse_path(frame,
859 /*d_printf(">>>fstat: resolving %s\n", path);*/
860 if (!cli_resolve_path(frame, "", context->internal->auth_info,
861 file->srv->cli, path,
862 &targetcli, &targetpath)) {
863 d_printf("Could not resolve %s\n", path);
868 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
870 if (!cli_ftruncate(targetcli, file->cli_fd, size)) {