1 /* Virtual File System: Midnight Commander file system.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
4 Free Software Foundation, Inc.
6 Written by Wayne Roberts <wroberts1@home.com>, 1997
7 Andrew V. Samoilov <sav@bcs.zp.ua> 2002, 2003
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public License
11 as published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /* Namespace: exports init_smbfs, smbfs_set_debug(), smbfs_set_debugf() */
26 #include <sys/types.h>
28 #undef USE_NCURSES /* Don't include *curses.h */
29 #include "../src/global.h"
30 #include "../src/tty.h" /* enable/disable interrupt key */
31 #include "../src/wtools.h" /* message() */
32 #include "../src/main.h" /* print_vfs_message */
35 #undef PACKAGE_BUGREPORT
38 #undef PACKAGE_TARNAME
39 #undef PACKAGE_VERSION
41 #include "samba/include/config.h"
42 /* don't load crap in "samba/include/includes.h" we don't use and which
43 conflicts with definitions in other includes */
44 #undef HAVE_LIBREADLINE
48 #include "samba/include/includes.h"
56 #define SMBFS_MAX_CONNECTIONS 16
57 static const char * const IPC
= "IPC$";
58 static const char * const URL_HEADER
= "/#smb:";
64 /* stuff that is same with each connection */
65 extern int DEBUGLEVEL
;
66 extern pstring myhostname
;
67 extern struct in_addr ipzero
;
68 static mode_t myumask
= 0755;
69 extern pstring global_myname
;
70 static int smbfs_open_connections
= 0;
71 static gboolean got_user
= FALSE
;
72 static gboolean got_pass
= FALSE
;
73 static pstring password
;
74 static pstring username
;
75 static struct vfs_class vfs_smbfs_ops
;
77 static struct _smbfs_connection
{
78 struct cli_state
*cli
;
79 struct in_addr dest_ip
;
81 char *host
; /* server name */
82 char *service
; /* share name */
90 } smbfs_connections
[SMBFS_MAX_CONNECTIONS
];
91 /* unique to each connection */
94 static struct cli_state
* smbfs_do_connect (const char *server
, char *share
);
96 typedef struct _smbfs_connection smbfs_connection
;
97 static smbfs_connection
*current_bucket
;
100 struct cli_state
*cli
;
106 static GSList
*auth_list
;
108 /* this function allows you to write:
109 * char *s = g_strdup("hello, world");
110 * s = free_after(g_strconcat(s, s, (char *)0), s);
113 free_after (char *result
, char *string_to_free
)
115 g_free(string_to_free
);
121 smbfs_auth_free (struct smb_authinfo
const *a
)
127 wipe_password (a
->password
);
131 smbfs_auth_free_all (void)
134 g_slist_foreach (auth_list
, (GFunc
)smbfs_auth_free
, 0);
135 g_slist_free (auth_list
);
141 smbfs_auth_cmp_host_and_share (gconstpointer _a
, gconstpointer _b
)
143 struct smb_authinfo
const *a
= (struct smb_authinfo
const *)_a
;
144 struct smb_authinfo
const *b
= (struct smb_authinfo
const *)_b
;
146 if (!a
->host
|| !a
->share
|| !b
->host
|| !b
->share
)
148 if (strcmp (a
->host
, b
->host
) != 0)
150 if (strcmp (a
->share
, b
->share
) != 0)
156 smbfs_auth_cmp_host (gconstpointer _a
, gconstpointer _b
)
158 struct smb_authinfo
const *a
= (struct smb_authinfo
const *)_a
;
159 struct smb_authinfo
const *b
= (struct smb_authinfo
const *)_b
;
161 if (!a
->host
|| !b
->host
)
163 if (strcmp (a
->host
, b
->host
) != 0)
165 if (strcmp (a
->share
, IPC
) != 0)
171 smbfs_auth_add (const char *host
, const char *share
, const char *domain
,
172 const char *user
, const char *password
)
174 struct smb_authinfo
*auth
= g_new (struct smb_authinfo
, 1);
179 /* Don't check for NULL, g_strdup already does. */
180 auth
->host
= g_strdup (host
);
181 auth
->share
= g_strdup (share
);
182 auth
->domain
= g_strdup (domain
);
183 auth
->user
= g_strdup (user
);
184 auth
->password
= g_strdup (password
);
185 auth_list
= g_slist_prepend (auth_list
, auth
);
189 smbfs_auth_remove (const char *host
, const char *share
)
191 struct smb_authinfo data
;
192 struct smb_authinfo
*auth
;
195 data
.host
= g_strdup (host
);
196 data
.share
= g_strdup (share
);
197 list
= g_slist_find_custom (auth_list
,
199 smbfs_auth_cmp_host_and_share
);
205 auth_list
= g_slist_remove (auth_list
, auth
);
206 smbfs_auth_free (auth
);
209 /* Set authentication information in bucket. Return 1 if successful, else 0 */
210 /* Information in auth_list overrides user if pass is NULL. */
211 /* bucket->host and bucket->service must be valid. */
213 smbfs_bucket_set_authinfo (smbfs_connection
*bucket
,
214 const char *domain
, const char *user
, const char *pass
,
215 int fallback_to_host
)
217 struct smb_authinfo data
;
218 struct smb_authinfo
*auth
;
221 if (domain
&& user
&& pass
) {
222 g_free (bucket
->domain
);
223 g_free (bucket
->user
);
224 g_free (bucket
->password
);
225 bucket
->domain
= g_strdup (domain
);
226 bucket
->user
= g_strdup (user
);
227 bucket
->password
= g_strdup (pass
);
228 smbfs_auth_remove (bucket
->host
, bucket
->service
);
229 smbfs_auth_add (bucket
->host
, bucket
->service
,
234 data
.host
= bucket
->host
;
235 data
.share
= bucket
->service
;
236 list
= g_slist_find_custom (auth_list
, &data
, smbfs_auth_cmp_host_and_share
);
237 if (!list
&& fallback_to_host
)
238 list
= g_slist_find_custom (auth_list
, &data
, smbfs_auth_cmp_host
);
241 bucket
->domain
= g_strdup (auth
->domain
);
242 bucket
->user
= g_strdup (auth
->user
);
243 bucket
->password
= g_strdup (auth
->password
);
248 bucket
->domain
= g_strdup (lp_workgroup ());
249 bucket
->user
= g_strdup (got_user
? username
: user
);
250 bucket
->password
= g_strdup (password
);
254 auth
= vfs_smb_get_authinfo (bucket
->host
,
256 (domain
? domain
: lp_workgroup ()),
259 g_free (bucket
->domain
);
260 g_free (bucket
->user
);
261 g_free (bucket
->password
);
262 bucket
->domain
= g_strdup (auth
->domain
);
263 bucket
->user
= g_strdup (auth
->user
);
264 bucket
->password
= g_strdup (auth
->password
);
265 smbfs_auth_remove (bucket
->host
, bucket
->service
);
266 auth_list
= g_slist_prepend (auth_list
, auth
);
273 smbfs_set_debug (int arg
)
279 smbfs_set_debugf (const char *filename
)
281 extern pstring debugf
;
283 if (DEBUGLEVEL
> 0) {
284 FILE *outfile
= fopen (filename
, "w");
286 setup_logging ("", True
); /* No needs for timestamp for each message */
289 pstrcpy (debugf
, filename
);
294 /********************** The callbacks ******************************/
296 smbfs_init (struct vfs_class
* me
)
298 const char *servicesf
= CONFIGDIR PATH_SEP_STR
"smb.conf";
300 /* DEBUGLEVEL = 4; */
303 charset_initialise ();
305 DEBUG (3, ("smbfs_init(%s)\n", me
->name
));
307 if (!get_myname (myhostname
, NULL
))
308 DEBUG (0, ("Failed to get my hostname.\n"));
310 if (!lp_load (servicesf
, True
, False
, False
))
311 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf
));
313 codepage_initialise (lp_client_code_page ());
321 if (getenv ("USER")) {
324 pstrcpy (username
, getenv ("USER"));
326 DEBUG (3, ("smbfs_init(): $USER:%s\n", username
));
327 if ((p
= strchr (username
, '%'))) {
329 pstrcpy (password
, p
+ 1);
331 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password
));
332 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
333 username
, password
));
337 if (getenv ("PASSWD")) {
338 pstrcpy (password
, getenv ("PASSWD"));
345 smbfs_fill_names (struct vfs_class
*me
, fill_names_f func
)
352 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
353 if (smbfs_connections
[i
].cli
) {
354 path
= g_strconcat (URL_HEADER
,
355 smbfs_connections
[i
].user
, "@",
356 smbfs_connections
[i
].host
,
357 "/", smbfs_connections
[i
].service
,
365 #define CNV_LANG(s) dos_to_unix(s,False)
366 #define GNAL_VNC(s) unix_to_dos(s,False)
367 /* does same as do_get() in client.c */
368 /* called from vfs.c:1080, count = buffer size */
370 smbfs_read (void *data
, char *buffer
, int count
)
372 smbfs_handle
*info
= (smbfs_handle
*) data
;
375 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
376 info
->fnum
, (int)info
->nread
, count
));
377 n
= cli_read(info
->cli
, info
->fnum
, buffer
, info
->nread
, count
);
384 smbfs_write (void *data
, const char *buf
, int nbyte
)
386 smbfs_handle
*info
= (smbfs_handle
*) data
;
389 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
390 info
->fnum
, (int)info
->nread
, nbyte
));
391 n
= cli_write(info
->cli
, info
->fnum
, 0, buf
, info
->nread
, nbyte
);
398 smbfs_close (void *data
)
400 smbfs_handle
*info
= (smbfs_handle
*) data
;
401 DEBUG (3, ("smbfs_close(fnum:%d)\n", info
->fnum
));
403 /* FIXME: Why too different cli have the same outbuf
404 * if file is copied to share
406 if (info
->cli
->outbuf
== NULL
) {
411 /* if imlementing archive_level: add rname to smbfs_handle */
412 if (archive_level
>= 2 && (inf
->attr
& aARCH
)) {
413 cli_setatr (info
->cli
, rname
, info
->attr
& ~(uint16
) aARCH
, 0);
416 return (cli_close (info
->cli
, info
->fnum
) == True
) ? 0 : -1;
420 smbfs_errno (struct vfs_class
*me
)
424 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno
)));
428 typedef struct dir_entry
{
430 struct dir_entry
*next
;
436 gboolean server_list
;
438 char *path
; /* the dir originally passed to smbfs_opendir */
439 smbfs_connection
*conn
;
448 *current_server_info
;
450 static gboolean first_direntry
;
453 smbfs_new_dir_entry (const char *name
)
455 static int inode_counter
;
456 dir_entry
*new_entry
;
457 new_entry
= g_new0 (dir_entry
, 1);
458 new_entry
->text
= dos_to_unix (g_strdup (name
), 1);
460 if (first_direntry
) {
461 current_info
->entries
= new_entry
;
462 first_direntry
= FALSE
;
464 current_info
->current
->next
= new_entry
;
466 current_info
->current
= new_entry
;
467 new_entry
->my_stat
.st_ino
= inode_counter
++;
472 /* browse for shares on server */
474 smbfs_browsing_helper (const char *name
, uint32 type
, const char *comment
, void *state
)
476 const char *typestr
= "";
477 dir_entry
*new_entry
= smbfs_new_dir_entry (name
);
484 /* show this as dir */
485 new_entry
->my_stat
.st_mode
=
486 (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
|
499 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name
, typestr
, comment
));
503 smbfs_loaddir_helper (file_info
* finfo
, const char *mask
, void *entry
)
505 dir_entry
*new_entry
= (dir_entry
*) entry
;
506 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
510 #if 0 /* I want to see dot files */
511 if (finfo
->mode
& aHIDDEN
)
512 return; /* don't bother with hidden files, "~$" screws up mc */
515 new_entry
= smbfs_new_dir_entry (finfo
->name
);
517 new_entry
->my_stat
.st_size
= finfo
->size
;
518 new_entry
->my_stat
.st_mtime
= finfo
->mtime
;
519 new_entry
->my_stat
.st_atime
= finfo
->atime
;
520 new_entry
->my_stat
.st_ctime
= finfo
->ctime
;
521 new_entry
->my_stat
.st_uid
= finfo
->uid
;
522 new_entry
->my_stat
.st_gid
= finfo
->gid
;
524 new_entry
->my_stat
.st_mode
= /* rw-rw-rw */
525 S_IRUSR
| S_IRGRP
| S_IROTH
| S_IWUSR
| S_IWGRP
| S_IWOTH
;
527 /* if (finfo->mode & aVOLID); nothing similar in real world */
528 if (finfo
->mode
& aDIR
)
529 new_entry
->my_stat
.st_mode
|= /* drwxrwxrwx */
530 S_IFDIR
| S_IXUSR
| S_IXGRP
| S_IXOTH
;
532 new_entry
->my_stat
.st_mode
|= S_IFREG
; /* if not dir, regular file? */
533 /* if (finfo->mode & aARCH); DOS archive */
534 /* if (finfo->mode & aHIDDEN); like a dot file? */
535 /* if (finfo->mode & aSYSTEM); like a kernel? */
536 if (finfo
->mode
& aRONLY
)
537 new_entry
->my_stat
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
538 new_entry
->my_stat
.st_mode
&= myumask
;
540 DEBUG (entry
? 3 : 6, (" %-30s%7.7s%8.0f %s",
541 CNV_LANG (finfo
->name
),
542 attrib_string (finfo
->mode
),
543 (double) finfo
->size
,
544 asctime (LocalTime (&t
))));
547 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
549 smbfs_convert_path (const char *remote_file
, gboolean trailing_asterik
)
551 const char *p
, *my_remote
;
554 my_remote
= remote_file
;
555 if (strncmp (my_remote
, URL_HEADER
, HEADER_LEN
) == 0) { /* if passed directly */
556 my_remote
+= HEADER_LEN
;
557 if (*my_remote
== '/') /* from server browsing */
559 p
= strchr(my_remote
, '/');
561 my_remote
= p
+1; /* advance to end of server name */
564 if (*my_remote
== '/')
565 my_remote
++; /* strip off leading '/' */
566 p
= strchr(my_remote
, '/');
568 my_remote
= p
; /* strip off share/service name */
569 /* create remote filename as understood by smb clientgen */
570 result
= g_strconcat (my_remote
, trailing_asterik
? "/*" : "", 0);
571 unix_to_dos (result
, /* inplace = */ 1); /* code page conversion */
572 str_replace(result
, '/', '\\');
577 smbfs_srv_browsing_helper (const char *name
, uint32 m
, const char *comment
,
580 dir_entry
*new_entry
= smbfs_new_dir_entry (name
);
585 /* show this as dir */
586 new_entry
->my_stat
.st_mode
=
587 (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
|
590 DEBUG (3, ("\t%-16.16s %s\n", name
, comment
));
594 smbfs_reconnect(smbfs_connection
*conn
, int *retries
)
597 DEBUG(3, ("RECONNECT\n"));
599 if (*(conn
->host
) == 0)
600 host
= g_strdup(conn
->cli
->desthost
); /* server browsing */
602 host
= g_strdup(conn
->host
);
604 cli_shutdown(conn
->cli
);
606 if (!(conn
->cli
= smbfs_do_connect(host
, conn
->service
))) {
607 message (1, MSG_ERROR
,
608 _(" reconnect to %s failed\n "), conn
->host
);
613 if (++(*retries
) == 2)
619 smbfs_send(struct cli_state
*cli
)
625 len
= smb_len(cli
->outbuf
) + 4;
627 while (nwritten
< len
) {
628 ret
= write_socket(cli
->fd
, cli
->outbuf
+nwritten
, len
- nwritten
);
639 /****************************************************************************
640 See if server has cut us off by checking for EPIPE when writing.
641 Taken from cli_chkpath()
642 ****************************************************************************/
644 smbfs_chkpath(struct cli_state
*cli
, const char *path
, BOOL send_only
)
650 unix_to_dos (path2
, 1);
651 trim_string(path2
,NULL
,"\\");
652 if (!*path2
) *path2
= '\\';
654 memset(cli
->outbuf
,'\0',smb_size
);
655 set_message(cli
->outbuf
,0,4 + strlen(path2
),True
);
656 SCVAL(cli
->outbuf
,smb_com
,SMBchkpth
);
657 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
661 SSVAL(cli
->outbuf
,smb_pid
,cli
->pid
);
662 SSVAL(cli
->outbuf
,smb_uid
,cli
->vuid
);
663 SSVAL(cli
->outbuf
,smb_mid
,cli
->mid
);
664 if (cli
->protocol
> PROTOCOL_CORE
) {
665 SCVAL(cli
->outbuf
,smb_flg
,0x8);
666 SSVAL(cli
->outbuf
,smb_flg2
,0x1);
669 p
= smb_buf(cli
->outbuf
);
673 if (!smbfs_send(cli
)) {
674 DEBUG(3, ("smbfs_chkpath: couldnt send\n"));
678 client_receive_smb(cli
->fd
, cli
->inbuf
, cli
->timeout
);
679 DEBUG(3, ("smbfs_chkpath: send only OK\n"));
680 return True
; /* just testing for EPIPE */
682 if (!client_receive_smb(cli
->fd
, cli
->inbuf
, cli
->timeout
)) {
683 DEBUG(3, ("smbfs_chkpath: receive error\n"));
686 if ((my_errno
= cli_error(cli
, NULL
, NULL
, NULL
))) {
687 if (my_errno
== 20 || my_errno
== 13)
688 return True
; /* ignore if 'not a directory' error */
689 DEBUG(3, ("smbfs_chkpath: cli_error: %s\n", g_strerror(my_errno
)));
698 smbfs_fs (const char *text
)
700 const char *p
= text
;
703 while ((p
= strchr(p
, '/')) != NULL
) {
714 smbfs_loaddir (opendir_info
*smbfs_info
)
716 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
717 int servlen
= strlen (smbfs_info
->conn
->service
);
718 const char *info_dirname
= smbfs_info
->dirname
;
721 DEBUG (3, ("smbfs_loaddir: dirname:%s\n", info_dirname
));
722 first_direntry
= TRUE
;
726 ("smbfs_loaddir: new:'%s', cached:'%s'\n", info_dirname
,
727 current_info
->dirname
));
728 /* if new desired dir is longer than cached in current_info */
729 if (smbfs_fs (info_dirname
) > smbfs_fs (current_info
->dirname
)) {
730 DEBUG (3, ("saving to previous_info\n"));
731 previous_info
= current_info
;
735 current_info
= smbfs_info
;
737 if (strcmp (info_dirname
, "/") == 0) {
738 if (!strcmp (smbfs_info
->path
, URL_HEADER
)) {
739 DEBUG (6, ("smbfs_loaddir: browsing %s\n", IPC
));
740 /* browse for servers */
741 if (!cli_NetServerEnum
742 (smbfs_info
->conn
->cli
, smbfs_info
->conn
->domain
,
743 SV_TYPE_ALL
, smbfs_srv_browsing_helper
, NULL
))
746 current_server_info
= smbfs_info
;
747 smbfs_info
->server_list
= TRUE
;
749 /* browse for shares */
750 if (cli_RNetShareEnum
751 (smbfs_info
->conn
->cli
, smbfs_browsing_helper
, NULL
) < 1)
754 current_share_info
= smbfs_info
;
759 /* do regular directory listing */
760 if (strncmp (smbfs_info
->conn
->service
, info_dirname
+ 1, servlen
) == 0) {
761 /* strip share name from dir */
762 my_dirname
= g_strdup (info_dirname
+ servlen
);
764 my_dirname
= free_after(smbfs_convert_path (my_dirname
, TRUE
), my_dirname
);
766 my_dirname
= smbfs_convert_path (info_dirname
, TRUE
);
768 DEBUG (6, ("smbfs_loaddir: service: %s\n", smbfs_info
->conn
->service
));
770 ("smbfs_loaddir: cli->share: %s\n",
771 smbfs_info
->conn
->cli
->share
));
773 ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname
));
774 /* do file listing: cli_list returns number of files */
776 (smbfs_info
->conn
->cli
, my_dirname
, attribute
,
777 smbfs_loaddir_helper
, NULL
) < 0) {
778 /* cli_list returns -1 if directory empty or cannot read socket */
779 my_errno
= cli_error (smbfs_info
->conn
->cli
, NULL
, &err
, NULL
);
783 if (*(my_dirname
) == 0)
784 smbfs_info
->dirname
= smbfs_info
->conn
->service
;
789 /* current_info->parent = smbfs_info->dirname; */
791 smbfs_info
->current
= smbfs_info
->entries
;
792 return 1; /* 1 = ok */
795 #ifdef SMBFS_FREE_DIR
797 smbfs_free_dir (dir_entry
*de
)
801 smbfs_free_dir (de
->next
);
808 /* The readdir routine loads the complete directory */
809 /* It's too slow to ask the server each time */
810 /* It now also sends the complete lstat information for each file */
812 smbfs_readdir(void *info
)
814 static union vfs_dirent smbfs_readdir_data
;
815 static char *const dirent_dest
= smbfs_readdir_data
.dent
.d_name
;
816 opendir_info
*smbfs_info
= (opendir_info
*) info
;
818 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info
->dirname
));
820 if (!smbfs_info
->entries
)
821 if (!smbfs_loaddir(smbfs_info
))
824 if (smbfs_info
->current
== 0) { /* reached end of dir entries */
825 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
826 #ifdef SMBFS_FREE_DIR
827 smbfs_free_dir(smbfs_info
->entries
);
828 smbfs_info
->entries
= 0;
832 g_strlcpy(dirent_dest
, smbfs_info
->current
->text
, MC_MAXPATHLEN
);
833 smbfs_info
->current
= smbfs_info
->current
->next
;
835 compute_namelen(&smbfs_readdir_data
.dent
);
837 return &smbfs_readdir_data
;
841 smbfs_closedir (void *info
)
843 opendir_info
*smbfs_info
= (opendir_info
*) info
;
844 /* dir_entry *p, *q; */
846 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info
->dirname
));
849 /* for (p = smbfs_info->entries; p;){
860 smbfs_chmod (struct vfs_class
*me
, const char *path
, int mode
)
864 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path
, mode
));
865 /* my_errno = EOPNOTSUPP;
866 return -1; */ /* cannot chmod on smb filesystem */
867 return 0; /* make mc happy */
871 smbfs_chown (struct vfs_class
*me
, const char *path
, int owner
, int group
)
875 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path
, owner
, group
));
876 my_errno
= EOPNOTSUPP
; /* ready for your labotomy? */
881 smbfs_utime (struct vfs_class
*me
, const char *path
, struct utimbuf
*times
)
886 DEBUG(3, ("smbfs_utime(path:%s)\n", path
));
887 my_errno
= EOPNOTSUPP
;
892 smbfs_readlink (struct vfs_class
*me
, const char *path
, char *buf
, size_t size
)
897 ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path
, buf
,
899 my_errno
= EOPNOTSUPP
;
900 return -1; /* no symlinks on smb filesystem? */
904 smbfs_symlink (struct vfs_class
*me
, const char *n1
, const char *n2
)
908 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1
, n2
));
909 my_errno
= EOPNOTSUPP
;
910 return -1; /* no symlinks on smb filesystem? */
913 /* Extract the hostname and username from the path */
914 /* path is in the form: [user@]hostname/share/remote-dir */
915 #define smbfs_get_host_and_username(path, host, user, port, pass) \
916 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
918 /*****************************************************
919 return a connection to a SMB server
920 current_bucket needs to be set before calling
921 *******************************************************/
922 static struct cli_state
*
923 smbfs_do_connect (const char *server
, char *share
)
926 struct nmb_name called
, calling
;
929 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server
, share
));
930 if (*share
== '\\') {
932 share
= strchr(server
,'\\');
933 if (!share
) return NULL
;
938 make_nmb_name(&calling
, global_myname
, 0x0);
939 make_nmb_name(&called
, server
, current_bucket
->name_type
);
943 ip
= (current_bucket
->have_ip
) ? current_bucket
->dest_ip
: ipzero
;
945 /* have to open a new connection */
946 if (!(c
= cli_initialise(NULL
))) {
951 pwd_init(&(c
->pwd
)); /* should be moved into cli_initialise()? */
952 pwd_set_cleartext(&(c
->pwd
), current_bucket
->password
);
954 if ((cli_set_port(c
, current_bucket
->port
) == 0) ||
955 !cli_connect(c
, server
, &ip
)) {
956 DEBUG(1, ("Connection to %s failed\n", server
));
960 if (!cli_session_request(c
, &calling
, &called
)) {
961 my_errno
= cli_error(c
, NULL
, &err
, NULL
);
962 DEBUG(1, ("session request to %s failed\n", called
.name
));
964 if (strcmp(called
.name
, "*SMBSERVER")) {
965 make_nmb_name(&called
, "*SMBSERVER", 0x20);
971 DEBUG(3, (" session request ok\n"));
973 if (!cli_negprot(c
)) {
974 DEBUG(1, ("protocol negotiation failed\n"));
978 if (!cli_session_setup(c
, current_bucket
->user
,
979 current_bucket
->password
, strlen(current_bucket
->password
),
980 current_bucket
->password
, strlen(current_bucket
->password
),
981 current_bucket
->domain
)) {
982 DEBUG(1,("session setup failed: %s\n", cli_errstr(c
)));
983 smbfs_auth_remove (server
, share
);
987 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
988 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
989 c
->server_domain
,c
->server_os
,c
->server_type
));
991 DEBUG(3, (" session setup ok\n"));
993 if (!cli_send_tconX(c
, share
, "?????",
994 current_bucket
->password
, strlen(current_bucket
->password
)+1)) {
995 DEBUG(1,("%s: tree connect failed: %s\n", share
, cli_errstr(c
)));
999 DEBUG(3, (" tconx ok\n"));
1005 my_errno
= cli_error(c
, NULL
, &err
, NULL
);
1012 smbfs_get_master_browser(char **host
)
1014 static char so_broadcast
[] = "SO_BROADCAST";
1016 struct in_addr
*ip_list
, bcast_addr
;
1018 /* does port = 137 for win95 master browser? */
1019 int fd
= open_socket_in( SOCK_DGRAM
, 0, 3,
1020 interpret_addr(lp_socket_address()), True
);
1023 set_socket_options(fd
, so_broadcast
);
1024 ip_list
= iface_bcast(ipzero
);
1025 bcast_addr
= *ip_list
;
1026 if ((ip_list
= name_query(fd
, "\01\02__MSBROWSE__\02", 1, True
,
1027 True
, bcast_addr
, &count
, NULL
))) {
1030 /* just return first master browser */
1031 *host
= g_strdup(inet_ntoa(ip_list
[0]));
1038 smbfs_free_bucket (smbfs_connection
*bucket
)
1040 g_free (bucket
->host
);
1041 g_free (bucket
->service
);
1042 g_free (bucket
->domain
);
1043 g_free (bucket
->user
);
1044 wipe_password (bucket
->password
);
1045 g_free (bucket
->home
);
1046 memset (bucket
, 0, sizeof (smbfs_connection
));
1049 static smbfs_connection
*
1050 smbfs_get_free_bucket (void)
1054 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++)
1055 if (!smbfs_connections
[i
].cli
) return &smbfs_connections
[i
];
1057 { /* search for most dormant connection */
1058 int oldest
= 0; /* index */
1059 time_t oldest_time
= smbfs_connections
[0].last_use
;
1060 for (i
= 1; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1061 if (smbfs_connections
[i
].last_use
< oldest_time
) {
1062 oldest_time
= smbfs_connections
[i
].last_use
;
1066 cli_shutdown(smbfs_connections
[oldest
].cli
);
1067 smbfs_free_bucket (&smbfs_connections
[oldest
]);
1068 return &smbfs_connections
[oldest
];
1071 /* This can't happend, since we have checked for max connections before */
1072 vfs_die("Internal error: smbfs_get_free_bucket");
1073 return 0; /* shut up, stupid gcc */
1076 /* This routine keeps track of open connections */
1077 /* Returns a connected socket to host */
1078 static smbfs_connection
*
1079 smbfs_open_link (char *host
, char *path
, const char *user
, int *port
,
1083 smbfs_connection
*bucket
;
1085 struct in_addr
*dest_ip
= NULL
;
1087 DEBUG (3, ("smbfs_open_link(host:%s, path:%s)\n", host
, path
));
1089 if (strcmp (host
, path
) == 0) /* if host & path are same: */
1090 pstrcpy (service
, IPC
); /* setup for browse */
1091 else { /* get share name from path, path starts with server name */
1093 if ((p
= strchr (path
, '/'))) /* get share aka */
1094 pstrcpy (service
, ++p
); /* service name from path */
1096 pstrcpy (service
, "");
1097 /* now check for trailing directory/filenames */
1098 p
= strchr (service
, '/');
1100 *p
= 0; /* cut off dir/files: sharename only */
1102 pstrcpy (service
, IPC
); /* setup for browse */
1103 DEBUG (6, ("smbfs_open_link: service from path:%s\n", service
));
1107 user
= username
; /* global from getenv */
1109 /* Is the link actually open? */
1110 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1111 if (!smbfs_connections
[i
].cli
)
1113 if ((strcmp (host
, smbfs_connections
[i
].host
) == 0) &&
1114 (strcmp (user
, smbfs_connections
[i
].user
) == 0) &&
1115 (strcmp (service
, smbfs_connections
[i
].service
) == 0)) {
1117 BOOL inshare
= (*host
!= 0 && *path
!= 0 && strchr (path
, '/'));
1118 /* check if this connection has died */
1119 while (!smbfs_chkpath (smbfs_connections
[i
].cli
, "\\", !inshare
)) {
1120 if (!smbfs_reconnect (&smbfs_connections
[i
], &retries
))
1123 DEBUG (6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i
));
1124 current_bucket
= &smbfs_connections
[i
];
1125 smbfs_connections
[i
].last_use
= time (NULL
);
1126 return &smbfs_connections
[i
];
1128 /* connection not found, find if we have ip for new connection */
1129 if (strcmp (host
, smbfs_connections
[i
].host
) == 0)
1130 dest_ip
= &smbfs_connections
[i
].cli
->dest_ip
;
1133 /* make new connection */
1134 bucket
= smbfs_get_free_bucket ();
1135 bucket
->name_type
= 0x20;
1137 bucket
->port
= *port
;
1138 bucket
->have_ip
= False
;
1140 bucket
->have_ip
= True
;
1141 bucket
->dest_ip
= *dest_ip
;
1143 current_bucket
= bucket
;
1145 bucket
->user
= g_strdup (user
);
1146 bucket
->service
= g_strdup (service
);
1148 if (!(*host
)) { /* if blank host name, browse for servers */
1149 if (!smbfs_get_master_browser (&host
)) /* set host to ip of master browser */
1150 return 0; /* could not find master browser? */
1152 bucket
->host
= g_strdup (""); /* blank host means master browser */
1154 bucket
->host
= g_strdup (host
);
1156 if (!smbfs_bucket_set_authinfo (bucket
, 0, /* domain currently not used */
1157 user
, this_pass
, 1))
1160 /* connect to share */
1161 while (!(bucket
->cli
= smbfs_do_connect (host
, service
))) {
1163 if (my_errno
!= EPERM
)
1165 message (1, MSG_ERROR
, _(" Authentication failed "));
1167 /* authentication failed, try again */
1168 smbfs_auth_remove (bucket
->host
, bucket
->service
);
1169 if (!smbfs_bucket_set_authinfo (bucket
, bucket
->domain
, bucket
->user
, 0, 0))
1174 smbfs_open_connections
++;
1175 DEBUG (3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1176 smbfs_open_connections
));
1181 smbfs_get_path (smbfs_connection
** sc
, const char *path
)
1183 char *user
, *host
, *remote_path
, *pass
;
1184 int port
= SMB_PORT
;
1186 DEBUG (3, ("smbfs_get_path(%s)\n", path
));
1187 if (strncmp (path
, URL_HEADER
, HEADER_LEN
))
1191 if (*path
== '/') /* '/' leading server name */
1192 path
++; /* probably came from server browsing */
1195 smbfs_get_host_and_username (&path
, &host
, &user
, &port
, &pass
)))
1197 smbfs_open_link (host
, remote_path
, user
, &port
, pass
)) == NULL
) {
1198 g_free (remote_path
);
1204 wipe_password (pass
);
1209 /* NOTE: tildes are deprecated. See ftpfs.c */
1211 int f
= !strcmp (remote_path
, "/~");
1212 if (f
|| !strncmp (remote_path
, "/~/", 3)) {
1214 s
= concat_dir_and_file ((*sc
)->home
, remote_path
+ 3 - f
);
1215 g_free (remote_path
);
1224 is_error (int result
, int errno_num
)
1226 if (!(result
== -1))
1227 return my_errno
= 0;
1229 my_errno
= errno_num
;
1235 smbfs_opendir (struct vfs_class
*me
, const char *dirname
)
1237 opendir_info
*smbfs_info
;
1238 smbfs_connection
*sc
;
1243 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname
));
1245 if (!(remote_dir
= smbfs_get_path (&sc
, dirname
)))
1248 /* FIXME: where freed? */
1249 smbfs_info
= g_new (opendir_info
, 1);
1250 smbfs_info
->server_list
= FALSE
;
1251 smbfs_info
->path
= g_strdup(dirname
); /* keep original */
1252 smbfs_info
->dirname
= remote_dir
;
1253 smbfs_info
->conn
= sc
;
1254 smbfs_info
->entries
= 0;
1255 smbfs_info
->current
= 0;
1261 smbfs_fake_server_stat (const char *server_url
, const char *path
, struct stat
*buf
)
1268 if ((p
= strrchr (path
, '/')))
1269 path
= p
+ 1; /* advance until last '/' */
1271 if (!current_info
->entries
) {
1272 if (!smbfs_loaddir (current_info
)) /* browse host */
1276 if (current_info
->server_list
== True
) {
1277 dentry
= current_info
->entries
;
1278 DEBUG (4, ("fake stat for SERVER \"%s\"\n", path
));
1280 if (strcmp (dentry
->text
, path
) == 0) {
1281 DEBUG (4, ("smbfs_fake_server_stat: %s:%4o\n",
1282 dentry
->text
, (int)dentry
->my_stat
.st_mode
));
1283 memcpy (buf
, &dentry
->my_stat
, sizeof (struct stat
));
1286 dentry
= dentry
->next
;
1294 smbfs_fake_share_stat (const char *server_url
, const char *path
, struct stat
*buf
)
1297 if (strlen (path
) < strlen (server_url
))
1300 if (!current_share_info
) { /* Server was not stat()ed */
1301 /* Make sure there is such share at server */
1302 smbfs_connection
*sc
;
1304 p
= smbfs_get_path (&sc
, path
);
1307 memset (buf
, 0, sizeof (*buf
));
1308 /* show this as dir */
1310 (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
|
1317 path
+= strlen (server_url
); /* we only want share name */
1320 if (*path
== '/') /* '/' leading server name */
1321 path
++; /* probably came from server browsing */
1323 if (!current_share_info
->entries
) {
1324 if (!smbfs_loaddir (current_share_info
)) /* browse host */
1327 dentry
= current_share_info
->entries
;
1328 DEBUG (3, ("smbfs_fake_share_stat: %s on %s\n", path
, server_url
));
1330 if (strcmp (dentry
->text
, path
) == 0) {
1331 DEBUG (6, ("smbfs_fake_share_stat: %s:%4o\n",
1332 dentry
->text
, (int) dentry
->my_stat
.st_mode
));
1333 memcpy (buf
, &dentry
->my_stat
, sizeof (struct stat
));
1336 dentry
= dentry
->next
;
1342 /* stat a single file, smbfs_get_remote_stat callback */
1343 static dir_entry
*single_entry
;
1345 /* stat a single file */
1347 smbfs_get_remote_stat (smbfs_connection
* sc
, const char *path
, struct stat
*buf
)
1349 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
1352 DEBUG (3, ("smbfs_get_remote_stat(): mypath:%s\n", path
));
1354 mypath
= smbfs_convert_path (path
, FALSE
);
1356 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1357 single_entry
= g_new (dir_entry
, 1);
1359 single_entry
->text
= dos_to_unix (g_strdup (finfo
->name
), 1);
1361 single_entry
->next
= 0;
1364 single_entry
= g_new0 (dir_entry
, 1);
1367 (sc
->cli
, mypath
, attribute
, smbfs_loaddir_helper
, single_entry
) < 1) {
1370 return -1; /* cli_list returns number of files */
1373 memcpy (buf
, &single_entry
->my_stat
, sizeof (struct stat
));
1375 /* don't free here, use for smbfs_fstat() */
1376 /* g_free(single_entry->text);
1377 g_free(single_entry); */
1383 smbfs_search_dir_entry (dir_entry
*dentry
, const char *text
, struct stat
*buf
)
1386 if (strcmp(text
, dentry
->text
) == 0) {
1387 memcpy(buf
, &dentry
->my_stat
, sizeof(struct stat
));
1388 memcpy(&single_entry
->my_stat
, &dentry
->my_stat
,
1389 sizeof(struct stat
));
1392 dentry
= dentry
->next
;
1398 smbfs_get_stat_info (smbfs_connection
* sc
, const char *path
, struct stat
*buf
)
1402 dir_entry
*dentry
= current_info
->entries
;
1404 const char *mypath
= path
;
1406 mypath
++; /* cut off leading '/' */
1407 if ((p
= strrchr (mypath
, '/')))
1408 mypath
= p
+ 1; /* advance until last file/dir name */
1409 DEBUG (3, ("smbfs_get_stat_info: mypath:%s, current_info->dirname:%s\n",
1410 mypath
, current_info
->dirname
));
1413 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1414 current_info
->dirname
, path
));
1418 if (!single_entry
) /* when found, this will be written too */
1419 single_entry
= g_new (dir_entry
, 1);
1420 if (smbfs_search_dir_entry (current_info
->entries
, mypath
, buf
) == 0) {
1423 /* now try to identify mypath as PARENT dir */
1427 mdp
= mydir
= g_strdup (current_info
->dirname
);
1428 if ((p
= strrchr (mydir
, '/')))
1429 *p
= 0; /* advance util last '/' */
1430 if ((p
= strrchr (mydir
, '/')))
1431 mydir
= p
+ 1; /* advance util last '/' */
1432 if (strcmp (mydir
, mypath
) == 0) { /* fake a stat for ".." */
1433 memset (buf
, 0, sizeof (struct stat
));
1434 buf
->st_mode
= (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
) & myumask
;
1435 memcpy (&single_entry
->my_stat
, buf
, sizeof (struct stat
));
1437 DEBUG (1, (" PARENT:found in %s\n", current_info
->dirname
));
1442 /* now try to identify as CURRENT dir? */
1444 char *dnp
= current_info
->dirname
;
1445 DEBUG (6, ("smbfs_get_stat_info: is %s current dir? this dir is: %s\n",
1446 mypath
, current_info
->dirname
));
1452 if (strcmp (mypath
, dnp
) == 0) {
1453 memset (buf
, 0, sizeof (struct stat
));
1454 buf
->st_mode
= (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
) & myumask
;
1455 memcpy (&single_entry
->my_stat
, buf
, sizeof (struct stat
));
1456 DEBUG (1, (" CURRENT:found in %s\n", current_info
->dirname
));
1460 DEBUG (3, ("'%s' not found in current_info '%s'\n", path
,
1461 current_info
->dirname
));
1462 /* try to find this in the PREVIOUS listing */
1463 if (previous_info
) {
1464 if (smbfs_search_dir_entry (previous_info
->entries
, mypath
, buf
) == 0)
1466 DEBUG (3, ("'%s' not found in previous_info '%s'\n", path
,
1467 previous_info
->dirname
));
1469 /* try to find this in the SHARE listing */
1470 if (current_share_info
) {
1471 if (smbfs_search_dir_entry (current_share_info
->entries
, mypath
, buf
) == 0)
1473 DEBUG (3, ("'%s' not found in share_info '%s'\n", path
,
1474 current_share_info
->dirname
));
1476 /* try to find this in the SERVER listing */
1477 if (current_server_info
) {
1478 if (smbfs_search_dir_entry (current_server_info
->entries
, mypath
, buf
) == 0)
1480 DEBUG (3, ("'%s' not found in server_info '%s'\n", path
,
1481 current_server_info
->dirname
));
1483 /* nothing found. get stat file info from server */
1484 return smbfs_get_remote_stat (sc
, path
, buf
);
1488 smbfs_chdir (struct vfs_class
*me
, const char *path
)
1491 smbfs_connection
*sc
;
1495 DEBUG (3, ("smbfs_chdir(path:%s)\n", path
));
1496 if (!(remote_dir
= smbfs_get_path (&sc
, path
)))
1498 g_free (remote_dir
);
1504 smbfs_loaddir_by_name (struct vfs_class
*me
, const char *path
)
1509 mypath
= g_strdup(path
);
1510 p
= strrchr(mypath
, '/');
1514 DEBUG(6, ("smbfs_loaddir_by_name(%s)\n", mypath
));
1515 smbfs_chdir(me
, mypath
);
1516 info
= smbfs_opendir (me
, mypath
);
1520 smbfs_readdir(info
);
1521 smbfs_loaddir(info
);
1526 smbfs_stat (struct vfs_class
* me
, const char *path
, struct stat
*buf
)
1528 smbfs_connection
*sc
;
1530 char *service
, *pp
, *at
;
1533 DEBUG (3, ("smbfs_stat(path:%s)\n", path
));
1535 if (!current_info
) {
1536 DEBUG (1, ("current_info = NULL: "));
1537 if (smbfs_loaddir_by_name (me
, path
) < 0)
1541 /* check if stating server */
1543 if (strncmp (p
, URL_HEADER
, HEADER_LEN
)) {
1544 DEBUG (1, ("'%s' doesnt start with '%s' (length %d)\n",
1545 p
, URL_HEADER
, HEADER_LEN
));
1553 pp
= strchr (p
, '/'); /* advance past next '/' */
1554 at
= strchr (p
, '@');
1555 pstrcpy (server_url
, URL_HEADER
);
1556 if (at
&& at
< pp
) { /* user@server */
1557 char *z
= &(server_url
[sizeof (server_url
) - 1]);
1560 at
= &(server_url
[HEADER_LEN
]) + (at
- p
+ 1);
1563 at
= &(server_url
[HEADER_LEN
]);
1568 pstrcat (server_url
, current_bucket
->host
);
1571 if (!current_info
->server_list
) {
1572 if (smbfs_loaddir_by_name (me
, path
) < 0)
1575 return smbfs_fake_server_stat (server_url
, path
, buf
);
1578 if (!strchr (++pp
, '/')) {
1579 return smbfs_fake_share_stat (server_url
, path
, buf
);
1582 /* stating inside share at this point */
1583 if (!(service
= smbfs_get_path (&sc
, path
))) /* connects if necessary */
1586 int hostlen
= strlen (current_bucket
->host
);
1587 char *ppp
= service
+ strlen (service
) - hostlen
;
1588 char *sp
= server_url
+ strlen (server_url
) - hostlen
;
1590 if (strcmp (sp
, ppp
) == 0) {
1591 /* make server name appear as directory */
1592 DEBUG (1, ("smbfs_stat: showing server as directory\n"));
1593 memset (buf
, 0, sizeof (struct stat
));
1594 buf
->st_mode
= (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
) & myumask
;
1599 /* check if current_info is in share requested */
1601 pp
= strchr (p
, '/');
1603 p
= ++pp
; /* advance past server name */
1604 pp
= strchr (p
, '/');
1607 *pp
= 0; /* cut off everthing after service name */
1609 p
= IPC
; /* browsing for services */
1610 pp
= current_info
->dirname
;
1613 if (strncmp (p
, pp
, strlen (p
)) != 0) {
1614 DEBUG (6, ("desired '%s' is not loaded, we have '%s'\n", p
, pp
));
1615 if (smbfs_loaddir_by_name (me
, path
) < 0) {
1619 DEBUG (6, ("loaded dir: '%s'\n", current_info
->dirname
));
1622 /* stat dirs & files under shares now */
1623 return smbfs_get_stat_info (sc
, path
, buf
);
1626 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1629 smbfs_lseek (void *data
, off_t offset
, int whence
)
1631 smbfs_handle
*info
= (smbfs_handle
*) data
;
1635 ("smbfs_lseek(info->nread => %d, offset => %d, whence => %d) \n",
1636 (int) info
->nread
, (int) offset
, whence
));
1640 info
->nread
= offset
;
1643 info
->nread
+= offset
;
1646 if (!cli_qfileinfo (info
->cli
, info
->fnum
,
1647 NULL
, &size
, NULL
, NULL
, NULL
,
1649 !cli_getattrE (info
->cli
, info
->fnum
,
1650 NULL
, &size
, NULL
, NULL
, NULL
)) {
1654 info
->nread
= size
+ offset
;
1662 smbfs_mknod (struct vfs_class
*me
, const char *path
, int mode
, int dev
)
1666 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path
, mode
, dev
));
1667 my_errno
= EOPNOTSUPP
;
1672 smbfs_mkdir (struct vfs_class
* me
, const char *path
, mode_t mode
)
1674 smbfs_connection
*sc
;
1680 DEBUG (3, ("smbfs_mkdir(path:%s, mode:%d)\n", path
, (int) mode
));
1681 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1683 g_free (remote_file
);
1684 cpath
= smbfs_convert_path (path
, FALSE
);
1686 if (!cli_mkdir (sc
->cli
, cpath
)) {
1687 my_errno
= cli_error (sc
->cli
, NULL
, &err
, NULL
);
1688 message (1, MSG_ERROR
, _(" Error %s creating directory %s "),
1689 cli_errstr (sc
->cli
), CNV_LANG (cpath
));
1698 smbfs_rmdir (struct vfs_class
*me
, const char *path
)
1700 smbfs_connection
*sc
;
1706 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path
));
1707 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1709 g_free (remote_file
);
1710 cpath
= smbfs_convert_path (path
, FALSE
);
1712 if (!cli_rmdir(sc
->cli
, cpath
)) {
1713 my_errno
= cli_error(sc
->cli
, NULL
, &err
, NULL
);
1714 message (1, MSG_ERROR
, _(" Error %s removing directory %s "),
1715 cli_errstr(sc
->cli
), CNV_LANG(cpath
));
1725 smbfs_link (struct vfs_class
*me
, const char *p1
, const char *p2
)
1729 DEBUG (3, ("smbfs_link(p1:%s, p2:%s)\n", p1
, p2
));
1730 my_errno
= EOPNOTSUPP
;
1735 smbfs_free (vfsid id
)
1737 DEBUG (3, ("smbfs_free(%p)\n", id
));
1738 smbfs_auth_free_all ();
1741 /* Gives up on a socket and reopens the connection, the child own the socket
1745 smbfs_forget (const char *path
)
1747 char *host
, *user
, *p
;
1750 if (strncmp (path
, URL_HEADER
, HEADER_LEN
))
1753 DEBUG (3, ("smbfs_forget(path:%s)\n", path
));
1756 if (path
[0] == '/' && path
[1] == '/')
1759 if ((p
= smbfs_get_host_and_username (&path
, &host
, &user
, &port
, NULL
))) {
1761 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1762 if (smbfs_connections
[i
].cli
1763 && (strcmp (host
, smbfs_connections
[i
].host
) == 0)
1764 && (strcmp (user
, smbfs_connections
[i
].user
) == 0)
1765 && (port
== smbfs_connections
[i
].port
)) {
1767 /* close socket: the child owns it now */
1768 cli_shutdown (smbfs_connections
[i
].cli
);
1770 /* reopen the connection */
1771 smbfs_connections
[i
].cli
=
1772 smbfs_do_connect (host
, smbfs_connections
[i
].service
);
1781 smbfs_setctl (struct vfs_class
*me
, const char *path
, int ctlop
, void *arg
)
1786 DEBUG (3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path
, ctlop
));
1788 case VFS_SETCTL_FORGET
:
1789 smbfs_forget (path
);
1796 static smbfs_handle
*
1797 smbfs_open_readwrite (smbfs_handle
*remote_handle
, char *rname
, int flags
, int mode
)
1803 if (flags
& O_TRUNC
) /* if it exists truncate to zero */
1804 DEBUG (3, ("smbfs_open: O_TRUNC\n"));
1806 remote_handle
->fnum
=
1807 #if 1 /* Don't play with flags, it is cli_open() headache */
1808 cli_open (remote_handle
->cli
, rname
, flags
, DENY_NONE
);
1809 #else /* What's a reasons to has this code ? */
1810 cli_open (remote_handle
->cli
, rname
, ((flags
& O_CREAT
)
1812 (O_WRONLY
| O_APPEND
))) ?
1813 flags
: O_RDONLY
, DENY_NONE
);
1815 if (remote_handle
->fnum
== -1) {
1816 message (1, MSG_ERROR
, _(" %s opening remote file %s "),
1817 cli_errstr (remote_handle
->cli
), CNV_LANG (rname
));
1818 DEBUG (1, ("smbfs_open(rname:%s) error:%s\n",
1819 rname
, cli_errstr (remote_handle
->cli
)));
1820 my_errno
= cli_error (remote_handle
->cli
, NULL
, &err
, NULL
);
1824 if (flags
& O_CREAT
)
1825 return remote_handle
;
1827 if (!cli_qfileinfo (remote_handle
->cli
, remote_handle
->fnum
,
1828 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
, NULL
,
1830 && !cli_getattrE (remote_handle
->cli
, remote_handle
->fnum
,
1831 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
)) {
1832 message (1, MSG_ERROR
, " getattrib: %s ",
1833 cli_errstr (remote_handle
->cli
));
1835 ("smbfs_open(rname:%s) getattrib:%s\n", rname
,
1836 cli_errstr (remote_handle
->cli
)));
1837 my_errno
= cli_error (remote_handle
->cli
, NULL
, &err
, NULL
);
1838 cli_close (remote_handle
->cli
, remote_handle
->fnum
);
1842 if ((flags
== (O_WRONLY
| O_APPEND
)) /* file.c:copy_file_file() -> do_append */
1843 && smbfs_lseek (remote_handle
, 0, SEEK_END
) == -1) {
1844 cli_close (remote_handle
->cli
, remote_handle
->fnum
);
1848 return remote_handle
;
1852 smbfs_open (struct vfs_class
*me
, const char *file
, int flags
, int mode
)
1856 smbfs_connection
*sc
;
1857 smbfs_handle
*remote_handle
;
1861 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%o)\n", file
, flags
, mode
));
1863 if (!(remote_file
= smbfs_get_path (&sc
, file
)))
1866 remote_file
= free_after(smbfs_convert_path (remote_file
, FALSE
), remote_file
);
1868 remote_handle
= g_new (smbfs_handle
, 2);
1869 remote_handle
->cli
= sc
->cli
;
1870 remote_handle
->nread
= 0;
1872 ret
= smbfs_open_readwrite (remote_handle
, remote_file
, flags
, mode
);
1874 g_free (remote_file
);
1876 g_free (remote_handle
);
1882 smbfs_unlink (struct vfs_class
*me
, const char *path
)
1884 smbfs_connection
*sc
;
1889 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1892 remote_file
= free_after(smbfs_convert_path (remote_file
, FALSE
), remote_file
);
1894 if (!cli_unlink(sc
->cli
, remote_file
)) {
1895 message (1, MSG_ERROR
, _(" %s removing remote file %s "),
1896 cli_errstr(sc
->cli
), CNV_LANG(remote_file
));
1897 g_free (remote_file
);
1900 g_free (remote_file
);
1905 smbfs_rename (struct vfs_class
*me
, const char *a
, const char *b
)
1907 smbfs_connection
*sc
;
1913 if ((ra
= smbfs_get_path (&sc
, a
)) == 0)
1916 if ((rb
= smbfs_get_path (&sc
, b
)) == 0) {
1921 ra
= free_after (smbfs_convert_path (ra
, FALSE
), ra
);
1922 rb
= free_after (smbfs_convert_path (rb
, FALSE
), rb
);
1924 retval
= cli_rename(sc
->cli
, ra
, rb
);
1930 message (1, MSG_ERROR
, _(" %s renaming files\n"),
1931 cli_errstr(sc
->cli
));
1938 smbfs_fstat (void *data
, struct stat
*buf
)
1940 smbfs_handle
*remote_handle
= (smbfs_handle
*)data
;
1942 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle
->fnum
));
1944 /* use left over from previous smbfs_get_remote_stat, if available */
1946 memcpy(buf
, &single_entry
->my_stat
, sizeof(struct stat
));
1947 else { /* single_entry not set up: bug */
1957 vfs_smbfs_ops
.name
= "smbfs";
1958 vfs_smbfs_ops
.prefix
= "smb:";
1959 vfs_smbfs_ops
.flags
= VFSF_NOLINKS
;
1960 vfs_smbfs_ops
.init
= smbfs_init
;
1961 vfs_smbfs_ops
.fill_names
= smbfs_fill_names
;
1962 vfs_smbfs_ops
.open
= smbfs_open
;
1963 vfs_smbfs_ops
.close
= smbfs_close
;
1964 vfs_smbfs_ops
.read
= smbfs_read
;
1965 vfs_smbfs_ops
.write
= smbfs_write
;
1966 vfs_smbfs_ops
.opendir
= smbfs_opendir
;
1967 vfs_smbfs_ops
.readdir
= smbfs_readdir
;
1968 vfs_smbfs_ops
.closedir
= smbfs_closedir
;
1969 vfs_smbfs_ops
.stat
= smbfs_stat
;
1970 vfs_smbfs_ops
.lstat
= smbfs_lstat
;
1971 vfs_smbfs_ops
.fstat
= smbfs_fstat
;
1972 vfs_smbfs_ops
.chmod
= smbfs_chmod
;
1973 vfs_smbfs_ops
.chown
= smbfs_chown
;
1974 vfs_smbfs_ops
.utime
= smbfs_utime
;
1975 vfs_smbfs_ops
.readlink
= smbfs_readlink
;
1976 vfs_smbfs_ops
.symlink
= smbfs_symlink
;
1977 vfs_smbfs_ops
.link
= smbfs_link
;
1978 vfs_smbfs_ops
.unlink
= smbfs_unlink
;
1979 vfs_smbfs_ops
.rename
= smbfs_rename
;
1980 vfs_smbfs_ops
.chdir
= smbfs_chdir
;
1981 vfs_smbfs_ops
.ferrno
= smbfs_errno
;
1982 vfs_smbfs_ops
.lseek
= smbfs_lseek
;
1983 vfs_smbfs_ops
.mknod
= smbfs_mknod
;
1984 vfs_smbfs_ops
.free
= smbfs_free
;
1985 vfs_smbfs_ops
.mkdir
= smbfs_mkdir
;
1986 vfs_smbfs_ops
.rmdir
= smbfs_rmdir
;
1987 vfs_smbfs_ops
.setctl
= smbfs_setctl
;
1988 vfs_register_class (&vfs_smbfs_ops
);