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"
29 #include "../libcli/smb/smbXcli_base.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;
53 NTSTATUS status = NT_STATUS_OBJECT_PATH_INVALID;
54 TALLOC_CTX *frame = talloc_stackframe();
56 if (!context || !context->internal->initialized) {
57 errno = EINVAL; /* Best I can think of ... */
68 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, port, share, &workgroup, &user, &password);
96 if (errno == EPERM) errno = EACCES;
98 return NULL; /* SMBC_server sets errno */
101 /* Hmmm, the test for a directory is suspect here ... FIXME */
103 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
104 status = NT_STATUS_OBJECT_PATH_INVALID;
106 file = SMB_MALLOC_P(SMBCFILE);
115 /*d_printf(">>>open: resolving %s\n", path);*/
116 status = cli_resolve_path(
117 frame, "", context->internal->auth_info,
118 srv->cli, path, &targetcli, &targetpath);
119 if (!NT_STATUS_IS_OK(status)) {
120 d_printf("Could not resolve %s\n", path);
126 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
128 status = cli_open(targetcli, targetpath, flags,
129 context->internal->share_mode, &fd);
130 if (!NT_STATUS_IS_OK(status)) {
132 /* Handle the error ... */
135 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);
185 /* Check if opendir needed ... */
187 if (!NT_STATUS_IS_OK(status)) {
190 eno = SMBC_errno(context, srv->cli);
191 file = smbc_getFunctionOpendir(context)(context, fname);
192 if (!file) errno = eno;
197 errno = EINVAL; /* FIXME, correct errno ? */
203 * Routine to create a file
207 SMBC_creat_ctx(SMBCCTX *context,
211 if (!context || !context->internal->initialized) {
216 return SMBC_open_ctx(context, path,
217 O_WRONLY | O_CREAT | O_TRUNC, mode);
221 * Routine to read() a file ...
225 SMBC_read_ctx(SMBCCTX *context,
231 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
233 char *targetpath = NULL;
234 struct cli_state *targetcli = NULL;
236 TALLOC_CTX *frame = talloc_stackframe();
242 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
243 * appears to pass file->offset (which is type off_t) differently than
244 * a local variable of type off_t. Using local variable "offset" in
245 * the call to cli_read() instead of file->offset fixes a problem
246 * retrieving data at an offset greater than 4GB.
250 if (!context || !context->internal->initialized) {
256 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
258 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
264 offset = file->offset;
266 /* Check that the buffer exists ... */
274 /*d_printf(">>>read: parsing %s\n", file->fname);*/
275 if (SMBC_parse_path(frame,
291 /*d_printf(">>>read: resolving %s\n", path);*/
292 status = cli_resolve_path(frame, "", context->internal->auth_info,
293 file->srv->cli, path,
294 &targetcli, &targetpath);
295 if (!NT_STATUS_IS_OK(status)) {
296 d_printf("Could not resolve %s\n", path);
301 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
303 status = cli_read(targetcli, file->cli_fd, (char *)buf, offset,
305 if (!NT_STATUS_IS_OK(status)) {
306 errno = SMBC_errno(context, targetcli);
313 DEBUG(4, (" --> %ld\n", (unsigned long)ret));
316 return ret; /* Success, ret bytes of data ... */
320 * Routine to write() a file ...
324 SMBC_write_ctx(SMBCCTX *context,
330 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
332 char *targetpath = NULL;
333 struct cli_state *targetcli = NULL;
335 TALLOC_CTX *frame = talloc_stackframe();
338 /* First check all pointers before dereferencing them */
340 if (!context || !context->internal->initialized) {
346 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
352 /* Check that the buffer exists ... */
360 offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */
362 /*d_printf(">>>write: parsing %s\n", file->fname);*/
363 if (SMBC_parse_path(frame,
379 /*d_printf(">>>write: resolving %s\n", path);*/
380 status = cli_resolve_path(frame, "", context->internal->auth_info,
381 file->srv->cli, path,
382 &targetcli, &targetpath);
383 if (!NT_STATUS_IS_OK(status)) {
384 d_printf("Could not resolve %s\n", path);
389 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
391 status = cli_writeall(targetcli, file->cli_fd,
392 0, (const uint8_t *)buf, offset, count, NULL);
393 if (!NT_STATUS_IS_OK(status)) {
394 errno = map_errno_from_nt_status(status);
399 file->offset += count;
402 return count; /* Success, 0 bytes of data ... */
406 * Routine to close() a file ...
410 SMBC_close_ctx(SMBCCTX *context,
414 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
416 char *targetpath = NULL;
418 struct cli_state *targetcli = NULL;
419 TALLOC_CTX *frame = talloc_stackframe();
422 if (!context || !context->internal->initialized) {
428 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
437 return smbc_getFunctionClosedir(context)(context, file);
440 /*d_printf(">>>close: parsing %s\n", file->fname);*/
441 if (SMBC_parse_path(frame,
457 /*d_printf(">>>close: resolving %s\n", path);*/
458 status = cli_resolve_path(frame, "", context->internal->auth_info,
459 file->srv->cli, path,
460 &targetcli, &targetpath);
461 if (!NT_STATUS_IS_OK(status)) {
462 d_printf("Could not resolve %s\n", path);
467 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
469 if (!NT_STATUS_IS_OK(cli_close(targetcli, file->cli_fd))) {
470 DEBUG(3, ("cli_close failed on %s. purging server.\n",
472 /* Deallocate slot and remove the server
473 * from the server cache if unused */
474 errno = SMBC_errno(context, targetcli);
476 DLIST_REMOVE(context->internal->files, file);
477 SAFE_FREE(file->fname);
479 smbc_getFunctionRemoveUnusedServer(context)(context, srv);
484 DLIST_REMOVE(context->internal->files, file);
485 SAFE_FREE(file->fname);
492 * Get info from an SMB server on a file. Use a qpathinfo call first
493 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
496 SMBC_getatr(SMBCCTX * context,
501 struct timespec *create_time_ts,
502 struct timespec *access_time_ts,
503 struct timespec *write_time_ts,
504 struct timespec *change_time_ts,
507 char *fixedpath = NULL;
508 char *targetpath = NULL;
509 struct cli_state *targetcli = NULL;
511 TALLOC_CTX *frame = talloc_stackframe();
514 if (!context || !context->internal->initialized) {
520 /* path fixup for . and .. */
521 if (strequal(path, ".") || strequal(path, "..")) {
522 fixedpath = talloc_strdup(frame, "\\");
529 fixedpath = talloc_strdup(frame, path);
535 trim_string(fixedpath, NULL, "\\..");
536 trim_string(fixedpath, NULL, "\\.");
538 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
540 status = cli_resolve_path(frame, "", context->internal->auth_info,
542 &targetcli, &targetpath);
543 if (!NT_STATUS_IS_OK(status)) {
544 d_printf("Couldn't resolve %s\n", path);
550 if (!srv->no_pathinfo2 &&
551 NT_STATUS_IS_OK(cli_qpathinfo2(targetcli, targetpath,
561 srv->no_pathinfo2 = True;
563 /* if this is NT then don't bother with the getatr */
564 if (smb1cli_conn_capabilities(targetcli->conn) & CAP_NT_SMBS) {
570 if (NT_STATUS_IS_OK(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);
574 if (write_time_ts != NULL) {
575 *write_time_ts = w_time_ts;
577 if (create_time_ts != NULL) {
578 *create_time_ts = w_time_ts;
580 if (access_time_ts != NULL) {
581 *access_time_ts = w_time_ts;
583 if (change_time_ts != NULL) {
584 *change_time_ts = w_time_ts;
599 * Set file info on an SMB server. Use setpathinfo call first. If that
600 * fails, use setattrE..
602 * Access and modification time parameters are always used and must be
603 * provided. Create time, if zero, will be determined from the actual create
604 * time of the file. If non-zero, the create time will be set as well.
606 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
609 SMBC_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
618 TALLOC_CTX *frame = talloc_stackframe();
621 * First, try setpathinfo (if qpathinfo succeeded), for it is the
622 * modern function for "new code" to be using, and it works given a
623 * filename rather than requiring that the file be opened to have its
624 * attributes manipulated.
626 if (srv->no_pathinfo ||
627 !NT_STATUS_IS_OK(cli_setpathinfo_basic(srv->cli, path,
635 * setpathinfo is not supported; go to plan B.
637 * cli_setatr() does not work on win98, and it also doesn't
638 * support setting the access time (only the modification
639 * time), so in all cases, we open the specified file and use
640 * cli_setattrE() which should work on all OS versions, and
641 * supports both times.
644 /* Don't try {q,set}pathinfo() again, with this server */
645 srv->no_pathinfo = True;
648 if (!NT_STATUS_IS_OK(cli_open(srv->cli, path, O_RDWR, DENY_NONE, &fd))) {
649 errno = SMBC_errno(context, srv->cli);
654 /* Set the new attributes */
655 ret = NT_STATUS_IS_OK(cli_setattrE(srv->cli, fd,
661 cli_close(srv->cli, fd);
664 * Unfortunately, setattrE() doesn't have a provision for
665 * setting the access mode (attributes). We'll have to try
666 * cli_setatr() for that, and with only this parameter, it
667 * seems to work on win98.
669 if (ret && mode != (uint16) -1) {
670 ret = NT_STATUS_IS_OK(cli_setatr(srv->cli, path, mode, 0));
674 errno = SMBC_errno(context, srv->cli);
685 * A routine to lseek() a file
689 SMBC_lseek_ctx(SMBCCTX *context,
695 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
697 char *targetpath = NULL;
698 struct cli_state *targetcli = NULL;
700 TALLOC_CTX *frame = talloc_stackframe();
703 if (!context || !context->internal->initialized) {
709 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
718 return -1; /* Can't lseek a dir ... */
723 file->offset = offset;
726 file->offset += offset;
729 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
730 if (SMBC_parse_path(frame,
746 /*d_printf(">>>lseek: resolving %s\n", path);*/
747 status = cli_resolve_path(
748 frame, "", context->internal->auth_info,
749 file->srv->cli, path, &targetcli, &targetpath);
750 if (!NT_STATUS_IS_OK(status)) {
751 d_printf("Could not resolve %s\n", path);
757 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
758 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
759 targetcli, file->cli_fd, NULL,
760 &size, NULL, NULL, NULL, NULL,
763 if (!NT_STATUS_IS_OK(cli_getattrE(targetcli, file->cli_fd,
764 NULL, &b_size, NULL, NULL, NULL))) {
771 file->offset = size + offset;
784 * Routine to truncate a file given by its file descriptor, to a specified size
788 SMBC_ftruncate_ctx(SMBCCTX *context,
796 char *password = NULL;
798 char *targetpath = NULL;
800 struct cli_state *targetcli = NULL;
801 TALLOC_CTX *frame = talloc_stackframe();
804 if (!context || !context->internal->initialized) {
810 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
822 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
823 if (SMBC_parse_path(frame,
839 /*d_printf(">>>fstat: resolving %s\n", path);*/
840 status = cli_resolve_path(frame, "", context->internal->auth_info,
841 file->srv->cli, path,
842 &targetcli, &targetpath);
843 if (!NT_STATUS_IS_OK(status)) {
844 d_printf("Could not resolve %s\n", path);
849 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
851 if (!NT_STATUS_IS_OK(cli_ftruncate(targetcli, file->cli_fd, (uint64_t)size))) {