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) {
55 errno = EINVAL; /* Best I can think of ... */
66 if (SMBC_parse_path(frame,
81 if (!user || user[0] == (char)0) {
82 user = talloc_strdup(frame, smbc_getUser(context));
90 srv = SMBC_server(frame, context, True,
91 server, share, &workgroup, &user, &password);
93 if (errno == EPERM) errno = EACCES;
95 return NULL; /* SMBC_server sets errno */
98 /* Hmmm, the test for a directory is suspect here ... FIXME */
100 if (strlen(path) > 0 && path[strlen(path) - 1] == '\\') {
101 status = NT_STATUS_OBJECT_PATH_INVALID;
103 file = SMB_MALLOC_P(SMBCFILE);
112 /*d_printf(">>>open: resolving %s\n", path);*/
113 if (!cli_resolve_path(frame, "", context->internal->auth_info,
115 &targetcli, &targetpath)) {
116 d_printf("Could not resolve %s\n", path);
122 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
124 status = cli_open(targetcli, targetpath, flags,
125 context->internal->share_mode, &fd);
126 if (!NT_STATUS_IS_OK(status)) {
128 /* Handle the error ... */
131 errno = SMBC_errno(context, targetcli);
136 /* Fill in file struct */
139 file->fname = SMB_STRDUP(fname);
144 DLIST_ADD(context->internal->files, file);
147 * If the file was opened in O_APPEND mode, all write
148 * operations should be appended to the file. To do that,
149 * though, using this protocol, would require a getattrE()
150 * call for each and every write, to determine where the end
151 * of the file is. (There does not appear to be an append flag
152 * in the protocol.) Rather than add all of that overhead of
153 * retrieving the current end-of-file offset prior to each
154 * write operation, we'll assume that most append operations
155 * will continuously write, so we'll just set the offset to
156 * the end of the file now and hope that's adequate.
158 * Note to self: If this proves inadequate, and O_APPEND
159 * should, in some cases, be forced for each write, add a
160 * field in the context options structure, for
161 * "strict_append_mode" which would select between the current
162 * behavior (if FALSE) or issuing a getattrE() prior to each
163 * write and forcing the write to the end of the file (if
164 * TRUE). Adding that capability will likely require adding
165 * an "append" flag into the _SMBCFILE structure to track
166 * whether a file was opened in O_APPEND mode. -- djl
168 if (flags & O_APPEND) {
169 if (SMBC_lseek_ctx(context, file, 0, SEEK_END) < 0) {
170 (void) SMBC_close_ctx(context, file);
181 /* Check if opendir needed ... */
183 if (!NT_STATUS_IS_OK(status)) {
186 eno = SMBC_errno(context, srv->cli);
187 file = smbc_getFunctionOpendir(context)(context, fname);
188 if (!file) errno = eno;
193 errno = EINVAL; /* FIXME, correct errno ? */
199 * Routine to create a file
203 SMBC_creat_ctx(SMBCCTX *context,
207 if (!context || !context->internal->initialized) {
212 return SMBC_open_ctx(context, path,
213 O_WRONLY | O_CREAT | O_TRUNC, mode);
217 * Routine to read() a file ...
221 SMBC_read_ctx(SMBCCTX *context,
227 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
229 char *targetpath = NULL;
230 struct cli_state *targetcli = NULL;
231 TALLOC_CTX *frame = talloc_stackframe();
236 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
237 * appears to pass file->offset (which is type off_t) differently than
238 * a local variable of type off_t. Using local variable "offset" in
239 * the call to cli_read() instead of file->offset fixes a problem
240 * retrieving data at an offset greater than 4GB.
244 if (!context || !context->internal->initialized) {
250 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
252 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
258 offset = file->offset;
260 /* Check that the buffer exists ... */
268 /*d_printf(">>>read: parsing %s\n", file->fname);*/
269 if (SMBC_parse_path(frame,
284 /*d_printf(">>>read: resolving %s\n", path);*/
285 if (!cli_resolve_path(frame, "", context->internal->auth_info,
286 file->srv->cli, path,
287 &targetcli, &targetpath)) {
288 d_printf("Could not resolve %s\n", path);
293 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
295 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
298 errno = SMBC_errno(context, targetcli);
305 DEBUG(4, (" --> %d\n", ret));
308 return ret; /* Success, ret bytes of data ... */
312 * Routine to write() a file ...
316 SMBC_write_ctx(SMBCCTX *context,
322 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
324 char *targetpath = NULL;
325 struct cli_state *targetcli = NULL;
326 TALLOC_CTX *frame = talloc_stackframe();
329 /* First check all pointers before dereferencing them */
331 if (!context || !context->internal->initialized) {
337 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
343 /* Check that the buffer exists ... */
351 offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */
353 /*d_printf(">>>write: parsing %s\n", file->fname);*/
354 if (SMBC_parse_path(frame,
369 /*d_printf(">>>write: resolving %s\n", path);*/
370 if (!cli_resolve_path(frame, "", context->internal->auth_info,
371 file->srv->cli, path,
372 &targetcli, &targetpath)) {
373 d_printf("Could not resolve %s\n", path);
378 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
380 status = cli_writeall(targetcli, file->cli_fd,
381 0, (const uint8_t *)buf, offset, count, NULL);
382 if (!NT_STATUS_IS_OK(status)) {
383 errno = map_errno_from_nt_status(status);
388 file->offset += count;
391 return count; /* Success, 0 bytes of data ... */
395 * Routine to close() a file ...
399 SMBC_close_ctx(SMBCCTX *context,
403 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
405 char *targetpath = NULL;
406 struct cli_state *targetcli = NULL;
407 TALLOC_CTX *frame = talloc_stackframe();
409 if (!context || !context->internal->initialized) {
415 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
424 return smbc_getFunctionClosedir(context)(context, file);
427 /*d_printf(">>>close: parsing %s\n", file->fname);*/
428 if (SMBC_parse_path(frame,
443 /*d_printf(">>>close: resolving %s\n", path);*/
444 if (!cli_resolve_path(frame, "", context->internal->auth_info,
445 file->srv->cli, path,
446 &targetcli, &targetpath)) {
447 d_printf("Could not resolve %s\n", path);
452 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
454 if (!NT_STATUS_IS_OK(cli_close(targetcli, file->cli_fd))) {
455 DEBUG(3, ("cli_close failed on %s. purging server.\n",
457 /* Deallocate slot and remove the server
458 * from the server cache if unused */
459 errno = SMBC_errno(context, targetcli);
461 DLIST_REMOVE(context->internal->files, file);
462 SAFE_FREE(file->fname);
464 smbc_getFunctionRemoveUnusedServer(context)(context, srv);
469 DLIST_REMOVE(context->internal->files, file);
470 SAFE_FREE(file->fname);
477 * Get info from an SMB server on a file. Use a qpathinfo call first
478 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
481 SMBC_getatr(SMBCCTX * context,
486 struct timespec *create_time_ts,
487 struct timespec *access_time_ts,
488 struct timespec *write_time_ts,
489 struct timespec *change_time_ts,
492 char *fixedpath = NULL;
493 char *targetpath = NULL;
494 struct cli_state *targetcli = NULL;
496 TALLOC_CTX *frame = talloc_stackframe();
498 if (!context || !context->internal->initialized) {
504 /* path fixup for . and .. */
505 if (strequal(path, ".") || strequal(path, "..")) {
506 fixedpath = talloc_strdup(frame, "\\");
513 fixedpath = talloc_strdup(frame, path);
519 trim_string(fixedpath, NULL, "\\..");
520 trim_string(fixedpath, NULL, "\\.");
522 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
524 if (!cli_resolve_path(frame, "", context->internal->auth_info,
526 &targetcli, &targetpath)) {
527 d_printf("Couldn't resolve %s\n", path);
533 if (!srv->no_pathinfo2 &&
534 NT_STATUS_IS_OK(cli_qpathinfo2(targetcli, targetpath,
544 /* if this is NT then don't bother with the getatr */
545 if (targetcli->capabilities & CAP_NT_SMBS) {
551 if (NT_STATUS_IS_OK(cli_getatr(targetcli, targetpath, mode, size, &write_time))) {
552 struct timespec w_time_ts;
554 w_time_ts = convert_time_t_to_timespec(write_time);
555 if (write_time_ts != NULL) {
556 *write_time_ts = w_time_ts;
558 if (create_time_ts != NULL) {
559 *create_time_ts = w_time_ts;
561 if (access_time_ts != NULL) {
562 *access_time_ts = w_time_ts;
564 if (change_time_ts != NULL) {
565 *change_time_ts = w_time_ts;
567 srv->no_pathinfo2 = True;
578 * Set file info on an SMB server. Use setpathinfo call first. If that
579 * fails, use setattrE..
581 * Access and modification time parameters are always used and must be
582 * provided. Create time, if zero, will be determined from the actual create
583 * time of the file. If non-zero, the create time will be set as well.
585 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
588 SMBC_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
597 TALLOC_CTX *frame = talloc_stackframe();
600 * First, try setpathinfo (if qpathinfo succeeded), for it is the
601 * modern function for "new code" to be using, and it works given a
602 * filename rather than requiring that the file be opened to have its
603 * attributes manipulated.
605 if (srv->no_pathinfo ||
606 !NT_STATUS_IS_OK(cli_setpathinfo_basic(srv->cli, path,
614 * setpathinfo is not supported; go to plan B.
616 * cli_setatr() does not work on win98, and it also doesn't
617 * support setting the access time (only the modification
618 * time), so in all cases, we open the specified file and use
619 * cli_setattrE() which should work on all OS versions, and
620 * supports both times.
623 /* Don't try {q,set}pathinfo() again, with this server */
624 srv->no_pathinfo = True;
627 if (!NT_STATUS_IS_OK(cli_open(srv->cli, path, O_RDWR, DENY_NONE, &fd))) {
628 errno = SMBC_errno(context, srv->cli);
633 /* Set the new attributes */
634 ret = NT_STATUS_IS_OK(cli_setattrE(srv->cli, fd,
640 cli_close(srv->cli, fd);
643 * Unfortunately, setattrE() doesn't have a provision for
644 * setting the access mode (attributes). We'll have to try
645 * cli_setatr() for that, and with only this parameter, it
646 * seems to work on win98.
648 if (ret && mode != (uint16) -1) {
649 ret = NT_STATUS_IS_OK(cli_setatr(srv->cli, path, mode, 0));
653 errno = SMBC_errno(context, srv->cli);
664 * A routine to lseek() a file
668 SMBC_lseek_ctx(SMBCCTX *context,
674 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
676 char *targetpath = NULL;
677 struct cli_state *targetcli = NULL;
678 TALLOC_CTX *frame = talloc_stackframe();
680 if (!context || !context->internal->initialized) {
686 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
695 return -1; /* Can't lseek a dir ... */
700 file->offset = offset;
703 file->offset += offset;
706 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
707 if (SMBC_parse_path(frame,
722 /*d_printf(">>>lseek: resolving %s\n", path);*/
723 if (!cli_resolve_path(frame, "", context->internal->auth_info,
724 file->srv->cli, path,
725 &targetcli, &targetpath)) {
726 d_printf("Could not resolve %s\n", path);
732 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
733 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
734 targetcli, file->cli_fd, NULL,
735 &size, NULL, NULL, NULL, NULL,
737 SMB_OFF_T b_size = size;
738 if (!NT_STATUS_IS_OK(cli_getattrE(targetcli, file->cli_fd,
739 NULL, &b_size, NULL, NULL, NULL))) {
746 file->offset = size + offset;
759 * Routine to truncate a file given by its file descriptor, to a specified size
763 SMBC_ftruncate_ctx(SMBCCTX *context,
767 SMB_OFF_T size = length;
771 char *password = NULL;
773 char *targetpath = NULL;
774 struct cli_state *targetcli = NULL;
775 TALLOC_CTX *frame = talloc_stackframe();
777 if (!context || !context->internal->initialized) {
783 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
795 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
796 if (SMBC_parse_path(frame,
811 /*d_printf(">>>fstat: resolving %s\n", path);*/
812 if (!cli_resolve_path(frame, "", context->internal->auth_info,
813 file->srv->cli, path,
814 &targetcli, &targetpath)) {
815 d_printf("Could not resolve %s\n", path);
820 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
822 if (!NT_STATUS_IS_OK(cli_ftruncate(targetcli, file->cli_fd, (uint64_t)size))) {