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 if (!cli_resolve_path(frame, "", context->internal->auth_info,
116 &targetcli, &targetpath)) {
117 d_printf("Could not resolve %s\n", path);
123 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
125 status = cli_open(targetcli, targetpath, flags,
126 context->internal->share_mode, &fd);
127 if (!NT_STATUS_IS_OK(status)) {
129 /* Handle the error ... */
132 errno = SMBC_errno(context, targetcli);
137 /* Fill in file struct */
140 file->fname = SMB_STRDUP(fname);
145 DLIST_ADD(context->internal->files, file);
148 * If the file was opened in O_APPEND mode, all write
149 * operations should be appended to the file. To do that,
150 * though, using this protocol, would require a getattrE()
151 * call for each and every write, to determine where the end
152 * of the file is. (There does not appear to be an append flag
153 * in the protocol.) Rather than add all of that overhead of
154 * retrieving the current end-of-file offset prior to each
155 * write operation, we'll assume that most append operations
156 * will continuously write, so we'll just set the offset to
157 * the end of the file now and hope that's adequate.
159 * Note to self: If this proves inadequate, and O_APPEND
160 * should, in some cases, be forced for each write, add a
161 * field in the context options structure, for
162 * "strict_append_mode" which would select between the current
163 * behavior (if FALSE) or issuing a getattrE() prior to each
164 * write and forcing the write to the end of the file (if
165 * TRUE). Adding that capability will likely require adding
166 * an "append" flag into the _SMBCFILE structure to track
167 * whether a file was opened in O_APPEND mode. -- djl
169 if (flags & O_APPEND) {
170 if (SMBC_lseek_ctx(context, file, 0, SEEK_END) < 0) {
171 (void) SMBC_close_ctx(context, file);
182 /* Check if opendir needed ... */
184 if (!NT_STATUS_IS_OK(status)) {
187 eno = SMBC_errno(context, srv->cli);
188 file = smbc_getFunctionOpendir(context)(context, fname);
189 if (!file) errno = eno;
194 errno = EINVAL; /* FIXME, correct errno ? */
200 * Routine to create a file
204 SMBC_creat_ctx(SMBCCTX *context,
208 if (!context || !context->internal->initialized) {
213 return SMBC_open_ctx(context, path,
214 O_WRONLY | O_CREAT | O_TRUNC, mode);
218 * Routine to read() a file ...
222 SMBC_read_ctx(SMBCCTX *context,
228 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
230 char *targetpath = NULL;
231 struct cli_state *targetcli = NULL;
232 TALLOC_CTX *frame = talloc_stackframe();
237 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
238 * appears to pass file->offset (which is type off_t) differently than
239 * a local variable of type off_t. Using local variable "offset" in
240 * the call to cli_read() instead of file->offset fixes a problem
241 * retrieving data at an offset greater than 4GB.
245 if (!context || !context->internal->initialized) {
251 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
253 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
259 offset = file->offset;
261 /* Check that the buffer exists ... */
269 /*d_printf(">>>read: parsing %s\n", file->fname);*/
270 if (SMBC_parse_path(frame,
285 /*d_printf(">>>read: resolving %s\n", path);*/
286 if (!cli_resolve_path(frame, "", context->internal->auth_info,
287 file->srv->cli, path,
288 &targetcli, &targetpath)) {
289 d_printf("Could not resolve %s\n", path);
294 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
296 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
299 errno = SMBC_errno(context, targetcli);
306 DEBUG(4, (" --> %d\n", ret));
309 return ret; /* Success, ret bytes of data ... */
313 * Routine to write() a file ...
317 SMBC_write_ctx(SMBCCTX *context,
323 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
325 char *targetpath = NULL;
326 struct cli_state *targetcli = NULL;
327 TALLOC_CTX *frame = talloc_stackframe();
330 /* First check all pointers before dereferencing them */
332 if (!context || !context->internal->initialized) {
338 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
344 /* Check that the buffer exists ... */
352 offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */
354 /*d_printf(">>>write: parsing %s\n", file->fname);*/
355 if (SMBC_parse_path(frame,
370 /*d_printf(">>>write: resolving %s\n", path);*/
371 if (!cli_resolve_path(frame, "", context->internal->auth_info,
372 file->srv->cli, path,
373 &targetcli, &targetpath)) {
374 d_printf("Could not resolve %s\n", path);
379 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
381 status = cli_writeall(targetcli, file->cli_fd,
382 0, (const uint8_t *)buf, offset, count, NULL);
383 if (!NT_STATUS_IS_OK(status)) {
384 errno = map_errno_from_nt_status(status);
389 file->offset += count;
392 return count; /* Success, 0 bytes of data ... */
396 * Routine to close() a file ...
400 SMBC_close_ctx(SMBCCTX *context,
404 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
406 char *targetpath = NULL;
407 struct cli_state *targetcli = NULL;
408 TALLOC_CTX *frame = talloc_stackframe();
410 if (!context || !context->internal->initialized) {
416 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
425 return smbc_getFunctionClosedir(context)(context, file);
428 /*d_printf(">>>close: parsing %s\n", file->fname);*/
429 if (SMBC_parse_path(frame,
444 /*d_printf(">>>close: resolving %s\n", path);*/
445 if (!cli_resolve_path(frame, "", context->internal->auth_info,
446 file->srv->cli, path,
447 &targetcli, &targetpath)) {
448 d_printf("Could not resolve %s\n", path);
453 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
455 if (!NT_STATUS_IS_OK(cli_close(targetcli, file->cli_fd))) {
456 DEBUG(3, ("cli_close failed on %s. purging server.\n",
458 /* Deallocate slot and remove the server
459 * from the server cache if unused */
460 errno = SMBC_errno(context, targetcli);
462 DLIST_REMOVE(context->internal->files, file);
463 SAFE_FREE(file->fname);
465 smbc_getFunctionRemoveUnusedServer(context)(context, srv);
470 DLIST_REMOVE(context->internal->files, file);
471 SAFE_FREE(file->fname);
478 * Get info from an SMB server on a file. Use a qpathinfo call first
479 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
482 SMBC_getatr(SMBCCTX * context,
487 struct timespec *create_time_ts,
488 struct timespec *access_time_ts,
489 struct timespec *write_time_ts,
490 struct timespec *change_time_ts,
493 char *fixedpath = NULL;
494 char *targetpath = NULL;
495 struct cli_state *targetcli = NULL;
497 TALLOC_CTX *frame = talloc_stackframe();
499 if (!context || !context->internal->initialized) {
505 /* path fixup for . and .. */
506 if (strequal(path, ".") || strequal(path, "..")) {
507 fixedpath = talloc_strdup(frame, "\\");
514 fixedpath = talloc_strdup(frame, path);
520 trim_string(fixedpath, NULL, "\\..");
521 trim_string(fixedpath, NULL, "\\.");
523 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
525 if (!cli_resolve_path(frame, "", context->internal->auth_info,
527 &targetcli, &targetpath)) {
528 d_printf("Couldn't resolve %s\n", path);
534 if (!srv->no_pathinfo2 &&
535 NT_STATUS_IS_OK(cli_qpathinfo2(targetcli, targetpath,
545 /* if this is NT then don't bother with the getatr */
546 if (targetcli->capabilities & CAP_NT_SMBS) {
552 if (NT_STATUS_IS_OK(cli_getatr(targetcli, targetpath, mode, size, &write_time))) {
553 struct timespec w_time_ts;
555 w_time_ts = convert_time_t_to_timespec(write_time);
556 if (write_time_ts != NULL) {
557 *write_time_ts = w_time_ts;
559 if (create_time_ts != NULL) {
560 *create_time_ts = w_time_ts;
562 if (access_time_ts != NULL) {
563 *access_time_ts = w_time_ts;
565 if (change_time_ts != NULL) {
566 *change_time_ts = w_time_ts;
568 srv->no_pathinfo2 = True;
579 * Set file info on an SMB server. Use setpathinfo call first. If that
580 * fails, use setattrE..
582 * Access and modification time parameters are always used and must be
583 * provided. Create time, if zero, will be determined from the actual create
584 * time of the file. If non-zero, the create time will be set as well.
586 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
589 SMBC_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
598 TALLOC_CTX *frame = talloc_stackframe();
601 * First, try setpathinfo (if qpathinfo succeeded), for it is the
602 * modern function for "new code" to be using, and it works given a
603 * filename rather than requiring that the file be opened to have its
604 * attributes manipulated.
606 if (srv->no_pathinfo ||
607 !NT_STATUS_IS_OK(cli_setpathinfo_basic(srv->cli, path,
615 * setpathinfo is not supported; go to plan B.
617 * cli_setatr() does not work on win98, and it also doesn't
618 * support setting the access time (only the modification
619 * time), so in all cases, we open the specified file and use
620 * cli_setattrE() which should work on all OS versions, and
621 * supports both times.
624 /* Don't try {q,set}pathinfo() again, with this server */
625 srv->no_pathinfo = True;
628 if (!NT_STATUS_IS_OK(cli_open(srv->cli, path, O_RDWR, DENY_NONE, &fd))) {
629 errno = SMBC_errno(context, srv->cli);
634 /* Set the new attributes */
635 ret = NT_STATUS_IS_OK(cli_setattrE(srv->cli, fd,
641 cli_close(srv->cli, fd);
644 * Unfortunately, setattrE() doesn't have a provision for
645 * setting the access mode (attributes). We'll have to try
646 * cli_setatr() for that, and with only this parameter, it
647 * seems to work on win98.
649 if (ret && mode != (uint16) -1) {
650 ret = NT_STATUS_IS_OK(cli_setatr(srv->cli, path, mode, 0));
654 errno = SMBC_errno(context, srv->cli);
665 * A routine to lseek() a file
669 SMBC_lseek_ctx(SMBCCTX *context,
675 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
677 char *targetpath = NULL;
678 struct cli_state *targetcli = NULL;
679 TALLOC_CTX *frame = talloc_stackframe();
681 if (!context || !context->internal->initialized) {
687 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
696 return -1; /* Can't lseek a dir ... */
701 file->offset = offset;
704 file->offset += offset;
707 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
708 if (SMBC_parse_path(frame,
723 /*d_printf(">>>lseek: resolving %s\n", path);*/
724 if (!cli_resolve_path(frame, "", context->internal->auth_info,
725 file->srv->cli, path,
726 &targetcli, &targetpath)) {
727 d_printf("Could not resolve %s\n", path);
733 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
734 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
735 targetcli, file->cli_fd, NULL,
736 &size, NULL, NULL, NULL, NULL,
738 SMB_OFF_T b_size = size;
739 if (!NT_STATUS_IS_OK(cli_getattrE(targetcli, file->cli_fd,
740 NULL, &b_size, NULL, NULL, NULL))) {
747 file->offset = size + offset;
760 * Routine to truncate a file given by its file descriptor, to a specified size
764 SMBC_ftruncate_ctx(SMBCCTX *context,
768 SMB_OFF_T size = length;
772 char *password = NULL;
774 char *targetpath = NULL;
775 struct cli_state *targetcli = NULL;
776 TALLOC_CTX *frame = talloc_stackframe();
778 if (!context || !context->internal->initialized) {
784 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
796 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
797 if (SMBC_parse_path(frame,
812 /*d_printf(">>>fstat: resolving %s\n", path);*/
813 if (!cli_resolve_path(frame, "", context->internal->auth_info,
814 file->srv->cli, path,
815 &targetcli, &targetpath)) {
816 d_printf("Could not resolve %s\n", path);
821 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
823 if (!NT_STATUS_IS_OK(cli_ftruncate(targetcli, file->cli_fd, (uint64_t)size))) {