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,
40 char *server = NULL, *share = NULL, *user = NULL, *password = NULL, *workgroup = NULL;
42 char *targetpath = NULL;
43 struct cli_state *targetcli = NULL;
45 SMBCFILE *file = NULL;
47 TALLOC_CTX *frame = talloc_stackframe();
49 if (!context || !context->initialized) {
51 errno = EINVAL; /* Best I can think of ... */
65 if (SMBC_parse_path(frame,
80 if (!user || user[0] == (char)0) {
81 user = talloc_strdup(frame, context->user);
89 srv = SMBC_server(frame, context, True,
90 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] == '\\') {
103 file = SMB_MALLOC_P(SMBCFILE);
113 /*d_printf(">>>open: resolving %s\n", path);*/
114 if (!cli_resolve_path(frame, "", srv->cli, path, &targetcli, &targetpath)) {
115 d_printf("Could not resolve %s\n", path);
120 /*d_printf(">>>open: resolved %s as %s\n", path, targetpath);*/
122 if ((fd = cli_open(targetcli, targetpath, flags,
123 context->share_mode)) < 0) {
125 /* Handle the error ... */
128 errno = SMBC_errno(context, targetcli);
134 /* Fill in file struct */
137 file->fname = SMB_STRDUP(fname);
142 DLIST_ADD(context->files, file);
145 * If the file was opened in O_APPEND mode, all write
146 * operations should be appended to the file. To do that,
147 * though, using this protocol, would require a getattrE()
148 * call for each and every write, to determine where the end
149 * of the file is. (There does not appear to be an append flag
150 * in the protocol.) Rather than add all of that overhead of
151 * retrieving the current end-of-file offset prior to each
152 * write operation, we'll assume that most append operations
153 * will continuously write, so we'll just set the offset to
154 * the end of the file now and hope that's adequate.
156 * Note to self: If this proves inadequate, and O_APPEND
157 * should, in some cases, be forced for each write, add a
158 * field in the context options structure, for
159 * "strict_append_mode" which would select between the current
160 * behavior (if FALSE) or issuing a getattrE() prior to each
161 * write and forcing the write to the end of the file (if
162 * TRUE). Adding that capability will likely require adding
163 * an "append" flag into the _SMBCFILE structure to track
164 * whether a file was opened in O_APPEND mode. -- djl
166 if (flags & O_APPEND) {
167 if (SMBC_lseek_ctx(context, file, 0, SEEK_END) < 0) {
168 (void) SMBC_close_ctx(context, file);
180 /* Check if opendir needed ... */
185 eno = SMBC_errno(context, srv->cli);
186 file = (context->posix_emu.opendir_fn)(context, fname);
187 if (!file) errno = eno;
193 errno = EINVAL; /* FIXME, correct errno ? */
200 * Routine to create a file
203 static int creat_bits = O_WRONLY | O_CREAT | O_TRUNC; /* FIXME: Do we need this */
206 SMBC_creat_ctx(SMBCCTX *context,
211 if (!context || !context->initialized) {
218 return SMBC_open_ctx(context, path, creat_bits, mode);
222 * Routine to read() a file ...
226 SMBC_read_ctx(SMBCCTX *context,
232 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
234 char *targetpath = NULL;
235 struct cli_state *targetcli = NULL;
236 TALLOC_CTX *frame = talloc_stackframe();
241 * Compiler bug (possibly) -- gcc (GCC) 3.3.5 (Debian 1:3.3.5-2) --
242 * appears to pass file->offset (which is type off_t) differently than
243 * a local variable of type off_t. Using local variable "offset" in
244 * the call to cli_read() instead of file->offset fixes a problem
245 * retrieving data at an offset greater than 4GB.
249 if (!context || !context->initialized) {
257 DEBUG(4, ("smbc_read(%p, %d)\n", file, (int)count));
259 if (!file || !SMBC_dlist_contains(context->files, file)) {
266 offset = file->offset;
268 /* Check that the buffer exists ... */
277 /*d_printf(">>>read: parsing %s\n", file->fname);*/
278 if (SMBC_parse_path(frame,
293 /*d_printf(">>>read: resolving %s\n", path);*/
294 if (!cli_resolve_path(frame, "", file->srv->cli, path,
295 &targetcli, &targetpath)) {
296 d_printf("Could not resolve %s\n", path);
300 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
302 ret = cli_read(targetcli, file->cli_fd, (char *)buf, offset, count);
306 errno = SMBC_errno(context, targetcli);
314 DEBUG(4, (" --> %d\n", ret));
317 return ret; /* Success, ret bytes of data ... */
322 * Routine to write() a file ...
326 SMBC_write_ctx(SMBCCTX *context,
333 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
335 char *targetpath = NULL;
336 struct cli_state *targetcli = NULL;
337 TALLOC_CTX *frame = talloc_stackframe();
339 /* First check all pointers before dereferencing them */
341 if (!context || !context->initialized) {
349 if (!file || !SMBC_dlist_contains(context->files, file)) {
355 /* Check that the buffer exists ... */
364 offset = file->offset; /* See "offset" comment in SMBC_read_ctx() */
366 /*d_printf(">>>write: parsing %s\n", file->fname);*/
367 if (SMBC_parse_path(frame,
382 /*d_printf(">>>write: resolving %s\n", path);*/
383 if (!cli_resolve_path(frame, "", file->srv->cli, path,
384 &targetcli, &targetpath)) {
385 d_printf("Could not resolve %s\n", path);
389 /*d_printf(">>>write: resolved path as %s\n", targetpath);*/
391 ret = cli_write(targetcli, file->cli_fd, 0, (char *)buf, offset, count);
394 errno = SMBC_errno(context, targetcli);
403 return ret; /* Success, 0 bytes of data ... */
407 * Routine to close() a file ...
411 SMBC_close_ctx(SMBCCTX *context,
415 char *server = NULL, *share = NULL, *user = NULL, *password = NULL;
417 char *targetpath = NULL;
418 struct cli_state *targetcli = NULL;
419 TALLOC_CTX *frame = talloc_stackframe();
421 if (!context || !context->initialized) {
428 if (!file || !SMBC_dlist_contains(context->files, file)) {
437 return (context->posix_emu.closedir_fn)(context, file);
440 /*d_printf(">>>close: parsing %s\n", file->fname);*/
441 if (SMBC_parse_path(frame,
456 /*d_printf(">>>close: resolving %s\n", path);*/
457 if (!cli_resolve_path(frame, "", file->srv->cli, path,
458 &targetcli, &targetpath)) {
459 d_printf("Could not resolve %s\n", path);
463 /*d_printf(">>>close: resolved path as %s\n", targetpath);*/
465 if (!cli_close(targetcli, file->cli_fd)) {
467 DEBUG(3, ("cli_close failed on %s. purging server.\n",
469 /* Deallocate slot and remove the server
470 * from the server cache if unused */
471 errno = SMBC_errno(context, targetcli);
473 DLIST_REMOVE(context->files, file);
474 SAFE_FREE(file->fname);
476 (context->server.remove_unused_server_fn)(context, srv);
482 DLIST_REMOVE(context->files, file);
483 SAFE_FREE(file->fname);
491 * Get info from an SMB server on a file. Use a qpathinfo call first
492 * and if that fails, use getatr, as Win95 sometimes refuses qpathinfo
495 SMBC_getatr(SMBCCTX * context,
500 struct timespec *create_time_ts,
501 struct timespec *access_time_ts,
502 struct timespec *write_time_ts,
503 struct timespec *change_time_ts,
506 char *fixedpath = NULL;
507 char *targetpath = NULL;
508 struct cli_state *targetcli = NULL;
510 TALLOC_CTX *frame = talloc_stackframe();
512 if (!context || !context->initialized) {
519 /* path fixup for . and .. */
520 if (strequal(path, ".") || strequal(path, "..")) {
521 fixedpath = talloc_strdup(frame, "\\");
528 fixedpath = talloc_strdup(frame, path);
534 trim_string(fixedpath, NULL, "\\..");
535 trim_string(fixedpath, NULL, "\\.");
537 DEBUG(4,("SMBC_getatr: sending qpathinfo\n"));
539 if (!cli_resolve_path(frame, "", srv->cli, fixedpath,
540 &targetcli, &targetpath)) {
541 d_printf("Couldn't resolve %s\n", path);
546 if (!srv->no_pathinfo2 &&
547 cli_qpathinfo2(targetcli, targetpath,
557 /* if this is NT then don't bother with the getatr */
558 if (targetcli->capabilities & CAP_NT_SMBS) {
564 if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
566 struct timespec w_time_ts;
568 w_time_ts = convert_time_t_to_timespec(write_time);
570 if (write_time_ts != NULL) {
571 *write_time_ts = w_time_ts;
574 if (create_time_ts != NULL) {
575 *create_time_ts = w_time_ts;
578 if (access_time_ts != NULL) {
579 *access_time_ts = w_time_ts;
582 if (change_time_ts != NULL) {
583 *change_time_ts = w_time_ts;
586 srv->no_pathinfo2 = True;
598 * Set file info on an SMB server. Use setpathinfo call first. If that
599 * fails, use setattrE..
601 * Access and modification time parameters are always used and must be
602 * provided. Create time, if zero, will be determined from the actual create
603 * time of the file. If non-zero, the create time will be set as well.
605 * "mode" (attributes) parameter may be set to -1 if it is not to be set.
608 SMBC_setatr(SMBCCTX * context, SMBCSRV *srv, char *path,
617 TALLOC_CTX *frame = talloc_stackframe();
620 * First, try setpathinfo (if qpathinfo succeeded), for it is the
621 * modern function for "new code" to be using, and it works given a
622 * filename rather than requiring that the file be opened to have its
623 * attributes manipulated.
625 if (srv->no_pathinfo ||
626 ! cli_setpathinfo(srv->cli, path,
634 * setpathinfo is not supported; go to plan B.
636 * cli_setatr() does not work on win98, and it also doesn't
637 * support setting the access time (only the modification
638 * time), so in all cases, we open the specified file and use
639 * cli_setattrE() which should work on all OS versions, and
640 * supports both times.
643 /* Don't try {q,set}pathinfo() again, with this server */
644 srv->no_pathinfo = True;
647 if ((fd = cli_open(srv->cli, path, O_RDWR, DENY_NONE)) < 0) {
649 errno = SMBC_errno(context, srv->cli);
654 /* Set the new attributes */
655 ret = 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 = 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;
699 TALLOC_CTX *frame = talloc_stackframe();
701 if (!context || !context->initialized) {
708 if (!file || !SMBC_dlist_contains(context->files, file)) {
720 return -1; /* Can't lseek a dir ... */
726 file->offset = offset;
730 file->offset += offset;
734 /*d_printf(">>>lseek: parsing %s\n", file->fname);*/
735 if (SMBC_parse_path(frame,
750 /*d_printf(">>>lseek: resolving %s\n", path);*/
751 if (!cli_resolve_path(frame, "", file->srv->cli, path,
752 &targetcli, &targetpath)) {
753 d_printf("Could not resolve %s\n", path);
757 /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/
759 if (!cli_qfileinfo(targetcli, file->cli_fd, NULL,
760 &size, NULL, NULL, NULL, NULL, NULL))
762 SMB_OFF_T b_size = size;
763 if (!cli_getattrE(targetcli, file->cli_fd,
764 NULL, &b_size, NULL, NULL, NULL))
772 file->offset = size + offset;
788 * Routine to truncate a file given by its file descriptor, to a specified size
792 SMBC_ftruncate_ctx(SMBCCTX *context,
796 SMB_OFF_T size = length;
800 char *password = NULL;
802 char *targetpath = NULL;
803 struct cli_state *targetcli = NULL;
804 TALLOC_CTX *frame = talloc_stackframe();
806 if (!context || !context->initialized) {
813 if (!file || !SMBC_dlist_contains(context->files, file)) {
825 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
826 if (SMBC_parse_path(frame,
841 /*d_printf(">>>fstat: resolving %s\n", path);*/
842 if (!cli_resolve_path(frame, "", file->srv->cli, path,
843 &targetcli, &targetpath)) {
844 d_printf("Could not resolve %s\n", path);
848 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
850 if (!cli_ftruncate(targetcli, file->cli_fd, size)) {