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. */
25 * \brief Source: Virtual File System: smb file system
26 * \author Wayne Roberts <wroberts1@home.com>
27 * \author Andrew V. Samoilov <sav@bcs.zp.ua>
28 * \date 1997, 2002, 2003
30 * Namespace: exports init_smbfs, smbfs_set_debug(), smbfs_set_debugf()
36 #include <sys/types.h>
38 #undef USE_NCURSES /* Don't include *curses.h */
40 #include "../src/global.h"
41 #include "../src/tty.h" /* enable/disable interrupt key */
42 #include "../src/wtools.h" /* message() */
43 #include "../src/main.h" /* print_vfs_message */
46 #undef PACKAGE_BUGREPORT
49 #undef PACKAGE_TARNAME
50 #undef PACKAGE_VERSION
52 #include "samba/include/config.h"
53 /* don't load crap in "samba/include/includes.h" we don't use and which
54 conflicts with definitions in other includes */
55 #undef HAVE_LIBREADLINE
59 #include "samba/include/includes.h"
67 #define SMBFS_MAX_CONNECTIONS 16
68 static const char * const IPC
= "IPC$";
69 static const char * const URL_HEADER
= "/#smb:";
75 /* stuff that is same with each connection */
76 extern int DEBUGLEVEL
;
77 extern pstring myhostname
;
78 extern struct in_addr ipzero
;
79 static mode_t myumask
= 0755;
80 extern pstring global_myname
;
81 static int smbfs_open_connections
= 0;
82 static gboolean got_user
= FALSE
;
83 static gboolean got_pass
= FALSE
;
84 static pstring password
;
85 static pstring username
;
86 static struct vfs_class vfs_smbfs_ops
;
88 static struct _smbfs_connection
{
89 struct cli_state
*cli
;
90 struct in_addr dest_ip
;
92 char *host
; /* server name */
93 char *service
; /* share name */
101 } smbfs_connections
[SMBFS_MAX_CONNECTIONS
];
102 /* unique to each connection */
104 /* modifies *share */
105 static struct cli_state
* smbfs_do_connect (const char *server
, char *share
);
107 typedef struct _smbfs_connection smbfs_connection
;
108 static smbfs_connection
*current_bucket
;
111 struct cli_state
*cli
;
117 static GSList
*auth_list
;
119 /* this function allows you to write:
120 * char *s = g_strdup("hello, world");
121 * s = free_after(g_strconcat(s, s, (char *)0), s);
124 free_after (char *result
, char *string_to_free
)
126 g_free(string_to_free
);
132 smbfs_auth_free (struct smb_authinfo
const *a
)
138 wipe_password (a
->password
);
142 smbfs_auth_free_all (void)
145 g_slist_foreach (auth_list
, (GFunc
)smbfs_auth_free
, 0);
146 g_slist_free (auth_list
);
152 smbfs_auth_cmp_host_and_share (gconstpointer _a
, gconstpointer _b
)
154 struct smb_authinfo
const *a
= (struct smb_authinfo
const *)_a
;
155 struct smb_authinfo
const *b
= (struct smb_authinfo
const *)_b
;
157 if (!a
->host
|| !a
->share
|| !b
->host
|| !b
->share
)
159 if (strcmp (a
->host
, b
->host
) != 0)
161 if (strcmp (a
->share
, b
->share
) != 0)
167 smbfs_auth_cmp_host (gconstpointer _a
, gconstpointer _b
)
169 struct smb_authinfo
const *a
= (struct smb_authinfo
const *)_a
;
170 struct smb_authinfo
const *b
= (struct smb_authinfo
const *)_b
;
172 if (!a
->host
|| !b
->host
)
174 if (strcmp (a
->host
, b
->host
) != 0)
176 if (strcmp (a
->share
, IPC
) != 0)
182 smbfs_auth_add (const char *host
, const char *share
, const char *domain
,
183 const char *user
, const char *password
)
185 struct smb_authinfo
*auth
= g_new (struct smb_authinfo
, 1);
190 /* Don't check for NULL, g_strdup already does. */
191 auth
->host
= g_strdup (host
);
192 auth
->share
= g_strdup (share
);
193 auth
->domain
= g_strdup (domain
);
194 auth
->user
= g_strdup (user
);
195 auth
->password
= g_strdup (password
);
196 auth_list
= g_slist_prepend (auth_list
, auth
);
200 smbfs_auth_remove (const char *host
, const char *share
)
202 struct smb_authinfo data
;
203 struct smb_authinfo
*auth
;
206 data
.host
= g_strdup (host
);
207 data
.share
= g_strdup (share
);
208 list
= g_slist_find_custom (auth_list
,
210 smbfs_auth_cmp_host_and_share
);
216 auth_list
= g_slist_remove (auth_list
, auth
);
217 smbfs_auth_free (auth
);
220 /* Set authentication information in bucket. Return 1 if successful, else 0 */
221 /* Information in auth_list overrides user if pass is NULL. */
222 /* bucket->host and bucket->service must be valid. */
224 smbfs_bucket_set_authinfo (smbfs_connection
*bucket
,
225 const char *domain
, const char *user
, const char *pass
,
226 int fallback_to_host
)
228 struct smb_authinfo data
;
229 struct smb_authinfo
*auth
;
232 if (domain
&& user
&& pass
) {
233 g_free (bucket
->domain
);
234 g_free (bucket
->user
);
235 g_free (bucket
->password
);
236 bucket
->domain
= g_strdup (domain
);
237 bucket
->user
= g_strdup (user
);
238 bucket
->password
= g_strdup (pass
);
239 smbfs_auth_remove (bucket
->host
, bucket
->service
);
240 smbfs_auth_add (bucket
->host
, bucket
->service
,
245 data
.host
= bucket
->host
;
246 data
.share
= bucket
->service
;
247 list
= g_slist_find_custom (auth_list
, &data
, smbfs_auth_cmp_host_and_share
);
248 if (!list
&& fallback_to_host
)
249 list
= g_slist_find_custom (auth_list
, &data
, smbfs_auth_cmp_host
);
252 bucket
->domain
= g_strdup (auth
->domain
);
253 bucket
->user
= g_strdup (auth
->user
);
254 bucket
->password
= g_strdup (auth
->password
);
259 bucket
->domain
= g_strdup (lp_workgroup ());
260 bucket
->user
= g_strdup (got_user
? username
: user
);
261 bucket
->password
= g_strdup (password
);
265 auth
= vfs_smb_get_authinfo (bucket
->host
,
267 (domain
? domain
: lp_workgroup ()),
270 g_free (bucket
->domain
);
271 g_free (bucket
->user
);
272 g_free (bucket
->password
);
273 bucket
->domain
= g_strdup (auth
->domain
);
274 bucket
->user
= g_strdup (auth
->user
);
275 bucket
->password
= g_strdup (auth
->password
);
276 smbfs_auth_remove (bucket
->host
, bucket
->service
);
277 auth_list
= g_slist_prepend (auth_list
, auth
);
284 smbfs_set_debug (int arg
)
290 smbfs_set_debugf (const char *filename
)
292 extern pstring debugf
;
294 if (DEBUGLEVEL
> 0) {
295 FILE *outfile
= fopen (filename
, "w");
297 setup_logging ("", True
); /* No needs for timestamp for each message */
300 pstrcpy (debugf
, filename
);
305 /********************** The callbacks ******************************/
307 smbfs_init (struct vfs_class
* me
)
309 const char *servicesf
= CONFIGDIR PATH_SEP_STR
"smb.conf";
311 /* DEBUGLEVEL = 4; */
314 charset_initialise ();
316 DEBUG (3, ("smbfs_init(%s)\n", me
->name
));
318 if (!get_myname (myhostname
, NULL
))
319 DEBUG (0, ("Failed to get my hostname.\n"));
321 if (!lp_load (servicesf
, True
, False
, False
))
322 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf
));
324 codepage_initialise (lp_client_code_page ());
332 if (getenv ("USER")) {
335 pstrcpy (username
, getenv ("USER"));
337 DEBUG (3, ("smbfs_init(): $USER:%s\n", username
));
338 if ((p
= strchr (username
, '%'))) {
340 pstrcpy (password
, p
+ 1);
342 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password
));
343 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
344 username
, password
));
348 if (getenv ("PASSWD")) {
349 pstrcpy (password
, getenv ("PASSWD"));
356 smbfs_fill_names (struct vfs_class
*me
, fill_names_f func
)
363 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
364 if (smbfs_connections
[i
].cli
) {
365 path
= g_strconcat (URL_HEADER
,
366 smbfs_connections
[i
].user
, "@",
367 smbfs_connections
[i
].host
,
368 "/", smbfs_connections
[i
].service
,
376 #define CNV_LANG(s) dos_to_unix(s,False)
377 #define GNAL_VNC(s) unix_to_dos(s,False)
378 /* does same as do_get() in client.c */
379 /* called from vfs.c:1080, count = buffer size */
381 smbfs_read (void *data
, char *buffer
, int count
)
383 smbfs_handle
*info
= (smbfs_handle
*) data
;
386 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
387 info
->fnum
, (int)info
->nread
, count
));
388 n
= cli_read(info
->cli
, info
->fnum
, buffer
, info
->nread
, count
);
395 smbfs_write (void *data
, const char *buf
, int nbyte
)
397 smbfs_handle
*info
= (smbfs_handle
*) data
;
400 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
401 info
->fnum
, (int)info
->nread
, nbyte
));
402 n
= cli_write(info
->cli
, info
->fnum
, 0, buf
, info
->nread
, nbyte
);
409 smbfs_close (void *data
)
411 smbfs_handle
*info
= (smbfs_handle
*) data
;
412 DEBUG (3, ("smbfs_close(fnum:%d)\n", info
->fnum
));
414 /* FIXME: Why too different cli have the same outbuf
415 * if file is copied to share
417 if (info
->cli
->outbuf
== NULL
) {
422 /* if imlementing archive_level: add rname to smbfs_handle */
423 if (archive_level
>= 2 && (inf
->attr
& aARCH
)) {
424 cli_setatr (info
->cli
, rname
, info
->attr
& ~(uint16
) aARCH
, 0);
427 return (cli_close (info
->cli
, info
->fnum
) == True
) ? 0 : -1;
431 smbfs_errno (struct vfs_class
*me
)
435 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno
)));
439 typedef struct dir_entry
{
441 struct dir_entry
*next
;
447 gboolean server_list
;
449 char *path
; /* the dir originally passed to smbfs_opendir */
450 smbfs_connection
*conn
;
459 *current_server_info
;
461 static gboolean first_direntry
;
464 smbfs_new_dir_entry (const char *name
)
466 static int inode_counter
;
467 dir_entry
*new_entry
;
468 new_entry
= g_new0 (dir_entry
, 1);
469 new_entry
->text
= dos_to_unix (g_strdup (name
), 1);
471 if (first_direntry
) {
472 current_info
->entries
= new_entry
;
473 first_direntry
= FALSE
;
475 current_info
->current
->next
= new_entry
;
477 current_info
->current
= new_entry
;
478 new_entry
->my_stat
.st_ino
= inode_counter
++;
483 /* browse for shares on server */
485 smbfs_browsing_helper (const char *name
, uint32 type
, const char *comment
, void *state
)
487 const char *typestr
= "";
488 dir_entry
*new_entry
= smbfs_new_dir_entry (name
);
495 /* show this as dir */
496 new_entry
->my_stat
.st_mode
=
497 (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
|
510 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name
, typestr
, comment
));
514 smbfs_loaddir_helper (file_info
* finfo
, const char *mask
, void *entry
)
516 dir_entry
*new_entry
= (dir_entry
*) entry
;
517 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
521 #if 0 /* I want to see dot files */
522 if (finfo
->mode
& aHIDDEN
)
523 return; /* don't bother with hidden files, "~$" screws up mc */
526 new_entry
= smbfs_new_dir_entry (finfo
->name
);
528 new_entry
->my_stat
.st_size
= finfo
->size
;
529 new_entry
->my_stat
.st_mtime
= finfo
->mtime
;
530 new_entry
->my_stat
.st_atime
= finfo
->atime
;
531 new_entry
->my_stat
.st_ctime
= finfo
->ctime
;
532 new_entry
->my_stat
.st_uid
= finfo
->uid
;
533 new_entry
->my_stat
.st_gid
= finfo
->gid
;
535 new_entry
->my_stat
.st_mode
= /* rw-rw-rw */
536 S_IRUSR
| S_IRGRP
| S_IROTH
| S_IWUSR
| S_IWGRP
| S_IWOTH
;
538 /* if (finfo->mode & aVOLID); nothing similar in real world */
539 if (finfo
->mode
& aDIR
)
540 new_entry
->my_stat
.st_mode
|= /* drwxrwxrwx */
541 S_IFDIR
| S_IXUSR
| S_IXGRP
| S_IXOTH
;
543 new_entry
->my_stat
.st_mode
|= S_IFREG
; /* if not dir, regular file? */
544 /* if (finfo->mode & aARCH); DOS archive */
545 /* if (finfo->mode & aHIDDEN); like a dot file? */
546 /* if (finfo->mode & aSYSTEM); like a kernel? */
547 if (finfo
->mode
& aRONLY
)
548 new_entry
->my_stat
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
549 new_entry
->my_stat
.st_mode
&= myumask
;
551 DEBUG (entry
? 3 : 6, (" %-30s%7.7s%8.0f %s",
552 CNV_LANG (finfo
->name
),
553 attrib_string (finfo
->mode
),
554 (double) finfo
->size
,
555 asctime (LocalTime (&t
))));
558 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
560 smbfs_convert_path (const char *remote_file
, gboolean trailing_asterik
)
562 const char *p
, *my_remote
;
565 my_remote
= remote_file
;
566 if (strncmp (my_remote
, URL_HEADER
, HEADER_LEN
) == 0) { /* if passed directly */
567 my_remote
+= HEADER_LEN
;
568 if (*my_remote
== '/') /* from server browsing */
570 p
= strchr(my_remote
, '/');
572 my_remote
= p
+1; /* advance to end of server name */
575 if (*my_remote
== '/')
576 my_remote
++; /* strip off leading '/' */
577 p
= strchr(my_remote
, '/');
579 my_remote
= p
; /* strip off share/service name */
580 /* create remote filename as understood by smb clientgen */
581 result
= g_strconcat (my_remote
, trailing_asterik
? "/*" : "", (char *) NULL
);
582 unix_to_dos (result
, /* inplace = */ 1); /* code page conversion */
583 str_replace(result
, '/', '\\');
588 smbfs_srv_browsing_helper (const char *name
, uint32 m
, const char *comment
,
591 dir_entry
*new_entry
= smbfs_new_dir_entry (name
);
596 /* show this as dir */
597 new_entry
->my_stat
.st_mode
=
598 (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
|
601 DEBUG (3, ("\t%-16.16s %s\n", name
, comment
));
605 smbfs_reconnect(smbfs_connection
*conn
, int *retries
)
608 DEBUG(3, ("RECONNECT\n"));
610 if (*(conn
->host
) == 0)
611 host
= g_strdup(conn
->cli
->desthost
); /* server browsing */
613 host
= g_strdup(conn
->host
);
615 cli_shutdown(conn
->cli
);
617 if (!(conn
->cli
= smbfs_do_connect(host
, conn
->service
))) {
618 message (D_ERROR
, MSG_ERROR
,
619 _(" reconnect to %s failed\n "), conn
->host
);
624 if (++(*retries
) == 2)
630 smbfs_send(struct cli_state
*cli
)
636 len
= smb_len(cli
->outbuf
) + 4;
638 while (nwritten
< len
) {
639 ret
= write_socket(cli
->fd
, cli
->outbuf
+nwritten
, len
- nwritten
);
650 /****************************************************************************
651 See if server has cut us off by checking for EPIPE when writing.
652 Taken from cli_chkpath()
653 ****************************************************************************/
655 smbfs_chkpath(struct cli_state
*cli
, const char *path
, BOOL send_only
)
661 unix_to_dos (path2
, 1);
662 trim_string(path2
,NULL
,"\\");
663 if (!*path2
) *path2
= '\\';
665 memset(cli
->outbuf
,'\0',smb_size
);
666 set_message(cli
->outbuf
,0,4 + strlen(path2
),True
);
667 SCVAL(cli
->outbuf
,smb_com
,SMBchkpth
);
668 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
672 SSVAL(cli
->outbuf
,smb_pid
,cli
->pid
);
673 SSVAL(cli
->outbuf
,smb_uid
,cli
->vuid
);
674 SSVAL(cli
->outbuf
,smb_mid
,cli
->mid
);
675 if (cli
->protocol
> PROTOCOL_CORE
) {
676 SCVAL(cli
->outbuf
,smb_flg
,0x8);
677 SSVAL(cli
->outbuf
,smb_flg2
,0x1);
680 p
= smb_buf(cli
->outbuf
);
684 if (!smbfs_send(cli
)) {
685 DEBUG(3, ("smbfs_chkpath: couldnt send\n"));
689 client_receive_smb(cli
->fd
, cli
->inbuf
, cli
->timeout
);
690 DEBUG(3, ("smbfs_chkpath: send only OK\n"));
691 return True
; /* just testing for EPIPE */
693 if (!client_receive_smb(cli
->fd
, cli
->inbuf
, cli
->timeout
)) {
694 DEBUG(3, ("smbfs_chkpath: receive error\n"));
697 if ((my_errno
= cli_error(cli
, NULL
, NULL
, NULL
))) {
698 if (my_errno
== 20 || my_errno
== 13)
699 return True
; /* ignore if 'not a directory' error */
700 DEBUG(3, ("smbfs_chkpath: cli_error: %s\n", g_strerror(my_errno
)));
709 smbfs_fs (const char *text
)
711 const char *p
= text
;
714 while ((p
= strchr(p
, '/')) != NULL
) {
725 smbfs_loaddir (opendir_info
*smbfs_info
)
727 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
728 int servlen
= strlen (smbfs_info
->conn
->service
);
729 const char *info_dirname
= smbfs_info
->dirname
;
732 DEBUG (3, ("smbfs_loaddir: dirname:%s\n", info_dirname
));
733 first_direntry
= TRUE
;
737 ("smbfs_loaddir: new:'%s', cached:'%s'\n", info_dirname
,
738 current_info
->dirname
));
739 /* if new desired dir is longer than cached in current_info */
740 if (smbfs_fs (info_dirname
) > smbfs_fs (current_info
->dirname
)) {
741 DEBUG (3, ("saving to previous_info\n"));
742 previous_info
= current_info
;
746 current_info
= smbfs_info
;
748 if (strcmp (info_dirname
, "/") == 0) {
749 if (!strcmp (smbfs_info
->path
, URL_HEADER
)) {
750 DEBUG (6, ("smbfs_loaddir: browsing %s\n", IPC
));
751 /* browse for servers */
752 if (!cli_NetServerEnum
753 (smbfs_info
->conn
->cli
, smbfs_info
->conn
->domain
,
754 SV_TYPE_ALL
, smbfs_srv_browsing_helper
, NULL
))
757 current_server_info
= smbfs_info
;
758 smbfs_info
->server_list
= TRUE
;
760 /* browse for shares */
761 if (cli_RNetShareEnum
762 (smbfs_info
->conn
->cli
, smbfs_browsing_helper
, NULL
) < 1)
765 current_share_info
= smbfs_info
;
770 /* do regular directory listing */
771 if (strncmp (smbfs_info
->conn
->service
, info_dirname
+ 1, servlen
) == 0) {
772 /* strip share name from dir */
773 my_dirname
= g_strdup (info_dirname
+ servlen
);
775 my_dirname
= free_after(smbfs_convert_path (my_dirname
, TRUE
), my_dirname
);
777 my_dirname
= smbfs_convert_path (info_dirname
, TRUE
);
779 DEBUG (6, ("smbfs_loaddir: service: %s\n", smbfs_info
->conn
->service
));
781 ("smbfs_loaddir: cli->share: %s\n",
782 smbfs_info
->conn
->cli
->share
));
784 ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname
));
785 /* do file listing: cli_list returns number of files */
787 (smbfs_info
->conn
->cli
, my_dirname
, attribute
,
788 smbfs_loaddir_helper
, NULL
) < 0) {
789 /* cli_list returns -1 if directory empty or cannot read socket */
790 my_errno
= cli_error (smbfs_info
->conn
->cli
, NULL
, &err
, NULL
);
794 if (*(my_dirname
) == 0)
795 smbfs_info
->dirname
= smbfs_info
->conn
->service
;
800 /* current_info->parent = smbfs_info->dirname; */
802 smbfs_info
->current
= smbfs_info
->entries
;
803 return 1; /* 1 = ok */
806 #ifdef SMBFS_FREE_DIR
808 smbfs_free_dir (dir_entry
*de
)
812 smbfs_free_dir (de
->next
);
819 /* The readdir routine loads the complete directory */
820 /* It's too slow to ask the server each time */
821 /* It now also sends the complete lstat information for each file */
823 smbfs_readdir(void *info
)
825 static union vfs_dirent smbfs_readdir_data
;
826 static char *const dirent_dest
= smbfs_readdir_data
.dent
.d_name
;
827 opendir_info
*smbfs_info
= (opendir_info
*) info
;
829 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info
->dirname
));
831 if (!smbfs_info
->entries
)
832 if (!smbfs_loaddir(smbfs_info
))
835 if (smbfs_info
->current
== 0) { /* reached end of dir entries */
836 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
837 #ifdef SMBFS_FREE_DIR
838 smbfs_free_dir(smbfs_info
->entries
);
839 smbfs_info
->entries
= 0;
843 g_strlcpy(dirent_dest
, smbfs_info
->current
->text
, MC_MAXPATHLEN
);
844 smbfs_info
->current
= smbfs_info
->current
->next
;
846 compute_namelen(&smbfs_readdir_data
.dent
);
848 return &smbfs_readdir_data
;
852 smbfs_closedir (void *info
)
854 opendir_info
*smbfs_info
= (opendir_info
*) info
;
855 /* dir_entry *p, *q; */
857 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info
->dirname
));
860 /* for (p = smbfs_info->entries; p;){
871 smbfs_chmod (struct vfs_class
*me
, const char *path
, int mode
)
875 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path
, mode
));
876 /* my_errno = EOPNOTSUPP;
877 return -1; */ /* cannot chmod on smb filesystem */
878 return 0; /* make mc happy */
882 smbfs_chown (struct vfs_class
*me
, const char *path
, int owner
, int group
)
886 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path
, owner
, group
));
887 my_errno
= EOPNOTSUPP
; /* ready for your labotomy? */
892 smbfs_utime (struct vfs_class
*me
, const char *path
, struct utimbuf
*times
)
897 DEBUG(3, ("smbfs_utime(path:%s)\n", path
));
898 my_errno
= EOPNOTSUPP
;
903 smbfs_readlink (struct vfs_class
*me
, const char *path
, char *buf
, size_t size
)
908 ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path
, buf
,
910 my_errno
= EOPNOTSUPP
;
911 return -1; /* no symlinks on smb filesystem? */
915 smbfs_symlink (struct vfs_class
*me
, const char *n1
, const char *n2
)
919 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1
, n2
));
920 my_errno
= EOPNOTSUPP
;
921 return -1; /* no symlinks on smb filesystem? */
924 /* Extract the hostname and username from the path */
925 /* path is in the form: [user@]hostname/share/remote-dir */
926 #define smbfs_get_host_and_username(path, host, user, port, pass) \
927 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
929 /*****************************************************
930 return a connection to a SMB server
931 current_bucket needs to be set before calling
932 *******************************************************/
933 static struct cli_state
*
934 smbfs_do_connect (const char *server
, char *share
)
937 struct nmb_name called
, calling
;
940 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server
, share
));
941 if (*share
== '\\') {
943 share
= strchr(server
,'\\');
944 if (!share
) return NULL
;
949 make_nmb_name(&calling
, global_myname
, 0x0);
950 make_nmb_name(&called
, server
, current_bucket
->name_type
);
954 ip
= (current_bucket
->have_ip
) ? current_bucket
->dest_ip
: ipzero
;
956 /* have to open a new connection */
957 if (!(c
= cli_initialise(NULL
))) {
962 pwd_init(&(c
->pwd
)); /* should be moved into cli_initialise()? */
963 pwd_set_cleartext(&(c
->pwd
), current_bucket
->password
);
965 if ((cli_set_port(c
, current_bucket
->port
) == 0) ||
966 !cli_connect(c
, server
, &ip
)) {
967 DEBUG(1, ("Connection to %s failed\n", server
));
971 if (!cli_session_request(c
, &calling
, &called
)) {
972 my_errno
= cli_error(c
, NULL
, &err
, NULL
);
973 DEBUG(1, ("session request to %s failed\n", called
.name
));
975 if (strcmp(called
.name
, "*SMBSERVER")) {
976 make_nmb_name(&called
, "*SMBSERVER", 0x20);
982 DEBUG(3, (" session request ok\n"));
984 if (!cli_negprot(c
)) {
985 DEBUG(1, ("protocol negotiation failed\n"));
989 if (!cli_session_setup(c
, current_bucket
->user
,
990 current_bucket
->password
, strlen(current_bucket
->password
),
991 current_bucket
->password
, strlen(current_bucket
->password
),
992 current_bucket
->domain
)) {
993 DEBUG(1,("session setup failed: %s\n", cli_errstr(c
)));
994 smbfs_auth_remove (server
, share
);
998 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
999 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
1000 c
->server_domain
,c
->server_os
,c
->server_type
));
1002 DEBUG(3, (" session setup ok\n"));
1004 if (!cli_send_tconX(c
, share
, "?????",
1005 current_bucket
->password
, strlen(current_bucket
->password
)+1)) {
1006 DEBUG(1,("%s: tree connect failed: %s\n", share
, cli_errstr(c
)));
1010 DEBUG(3, (" tconx ok\n"));
1016 my_errno
= cli_error(c
, NULL
, &err
, NULL
);
1023 smbfs_get_master_browser(char **host
)
1025 static char so_broadcast
[] = "SO_BROADCAST";
1027 struct in_addr
*ip_list
, bcast_addr
;
1029 /* does port = 137 for win95 master browser? */
1030 int fd
= open_socket_in( SOCK_DGRAM
, 0, 3,
1031 interpret_addr(lp_socket_address()), True
);
1034 set_socket_options(fd
, so_broadcast
);
1035 ip_list
= iface_bcast(ipzero
);
1036 bcast_addr
= *ip_list
;
1037 if ((ip_list
= name_query(fd
, "\01\02__MSBROWSE__\02", 1, True
,
1038 True
, bcast_addr
, &count
, NULL
))) {
1041 /* just return first master browser */
1042 *host
= g_strdup(inet_ntoa(ip_list
[0]));
1049 smbfs_free_bucket (smbfs_connection
*bucket
)
1051 g_free (bucket
->host
);
1052 g_free (bucket
->service
);
1053 g_free (bucket
->domain
);
1054 g_free (bucket
->user
);
1055 wipe_password (bucket
->password
);
1056 g_free (bucket
->home
);
1057 memset (bucket
, 0, sizeof (smbfs_connection
));
1060 static smbfs_connection
*
1061 smbfs_get_free_bucket (void)
1065 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++)
1066 if (!smbfs_connections
[i
].cli
) return &smbfs_connections
[i
];
1068 { /* search for most dormant connection */
1069 int oldest
= 0; /* index */
1070 time_t oldest_time
= smbfs_connections
[0].last_use
;
1071 for (i
= 1; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1072 if (smbfs_connections
[i
].last_use
< oldest_time
) {
1073 oldest_time
= smbfs_connections
[i
].last_use
;
1077 cli_shutdown(smbfs_connections
[oldest
].cli
);
1078 smbfs_free_bucket (&smbfs_connections
[oldest
]);
1079 return &smbfs_connections
[oldest
];
1082 /* This can't happend, since we have checked for max connections before */
1083 vfs_die("Internal error: smbfs_get_free_bucket");
1084 return 0; /* shut up, stupid gcc */
1087 /* This routine keeps track of open connections */
1088 /* Returns a connected socket to host */
1089 static smbfs_connection
*
1090 smbfs_open_link (char *host
, char *path
, const char *user
, int *port
,
1094 smbfs_connection
*bucket
;
1096 struct in_addr
*dest_ip
= NULL
;
1098 DEBUG (3, ("smbfs_open_link(host:%s, path:%s)\n", host
, path
));
1100 if (strcmp (host
, path
) == 0) /* if host & path are same: */
1101 pstrcpy (service
, IPC
); /* setup for browse */
1102 else { /* get share name from path, path starts with server name */
1104 if ((p
= strchr (path
, '/'))) /* get share aka */
1105 pstrcpy (service
, ++p
); /* service name from path */
1107 pstrcpy (service
, "");
1108 /* now check for trailing directory/filenames */
1109 p
= strchr (service
, '/');
1111 *p
= 0; /* cut off dir/files: sharename only */
1113 pstrcpy (service
, IPC
); /* setup for browse */
1114 DEBUG (6, ("smbfs_open_link: service from path:%s\n", service
));
1118 user
= username
; /* global from getenv */
1120 /* Is the link actually open? */
1121 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1122 if (!smbfs_connections
[i
].cli
)
1124 if ((strcmp (host
, smbfs_connections
[i
].host
) == 0) &&
1125 (strcmp (user
, smbfs_connections
[i
].user
) == 0) &&
1126 (strcmp (service
, smbfs_connections
[i
].service
) == 0)) {
1128 BOOL inshare
= (*host
!= 0 && *path
!= 0 && strchr (path
, '/'));
1129 /* check if this connection has died */
1130 while (!smbfs_chkpath (smbfs_connections
[i
].cli
, "\\", !inshare
)) {
1131 if (!smbfs_reconnect (&smbfs_connections
[i
], &retries
))
1134 DEBUG (6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i
));
1135 current_bucket
= &smbfs_connections
[i
];
1136 smbfs_connections
[i
].last_use
= time (NULL
);
1137 return &smbfs_connections
[i
];
1139 /* connection not found, find if we have ip for new connection */
1140 if (strcmp (host
, smbfs_connections
[i
].host
) == 0)
1141 dest_ip
= &smbfs_connections
[i
].cli
->dest_ip
;
1144 /* make new connection */
1145 bucket
= smbfs_get_free_bucket ();
1146 bucket
->name_type
= 0x20;
1148 bucket
->port
= *port
;
1149 bucket
->have_ip
= False
;
1151 bucket
->have_ip
= True
;
1152 bucket
->dest_ip
= *dest_ip
;
1154 current_bucket
= bucket
;
1156 bucket
->user
= g_strdup (user
);
1157 bucket
->service
= g_strdup (service
);
1159 if (!(*host
)) { /* if blank host name, browse for servers */
1160 if (!smbfs_get_master_browser (&host
)) /* set host to ip of master browser */
1161 return 0; /* could not find master browser? */
1163 bucket
->host
= g_strdup (""); /* blank host means master browser */
1165 bucket
->host
= g_strdup (host
);
1167 if (!smbfs_bucket_set_authinfo (bucket
, 0, /* domain currently not used */
1168 user
, this_pass
, 1))
1171 /* connect to share */
1172 while (!(bucket
->cli
= smbfs_do_connect (host
, service
))) {
1174 if (my_errno
!= EPERM
)
1176 message (D_ERROR
, MSG_ERROR
, _(" Authentication failed "));
1178 /* authentication failed, try again */
1179 smbfs_auth_remove (bucket
->host
, bucket
->service
);
1180 if (!smbfs_bucket_set_authinfo (bucket
, bucket
->domain
, bucket
->user
, 0, 0))
1185 smbfs_open_connections
++;
1186 DEBUG (3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1187 smbfs_open_connections
));
1192 smbfs_get_path (smbfs_connection
** sc
, const char *path
)
1194 char *user
, *host
, *remote_path
, *pass
;
1195 int port
= SMB_PORT
;
1197 DEBUG (3, ("smbfs_get_path(%s)\n", path
));
1198 if (strncmp (path
, URL_HEADER
, HEADER_LEN
))
1202 if (*path
== '/') /* '/' leading server name */
1203 path
++; /* probably came from server browsing */
1206 smbfs_get_host_and_username (&path
, &host
, &user
, &port
, &pass
)))
1208 smbfs_open_link (host
, remote_path
, user
, &port
, pass
)) == NULL
) {
1209 g_free (remote_path
);
1215 wipe_password (pass
);
1220 /* NOTE: tildes are deprecated. See ftpfs.c */
1222 int f
= !strcmp (remote_path
, "/~");
1223 if (f
|| !strncmp (remote_path
, "/~/", 3)) {
1225 s
= concat_dir_and_file ((*sc
)->home
, remote_path
+ 3 - f
);
1226 g_free (remote_path
);
1235 is_error (int result
, int errno_num
)
1237 if (!(result
== -1))
1238 return my_errno
= 0;
1240 my_errno
= errno_num
;
1246 smbfs_opendir (struct vfs_class
*me
, const char *dirname
)
1248 opendir_info
*smbfs_info
;
1249 smbfs_connection
*sc
;
1254 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname
));
1256 if (!(remote_dir
= smbfs_get_path (&sc
, dirname
)))
1259 /* FIXME: where freed? */
1260 smbfs_info
= g_new (opendir_info
, 1);
1261 smbfs_info
->server_list
= FALSE
;
1262 smbfs_info
->path
= g_strdup(dirname
); /* keep original */
1263 smbfs_info
->dirname
= remote_dir
;
1264 smbfs_info
->conn
= sc
;
1265 smbfs_info
->entries
= 0;
1266 smbfs_info
->current
= 0;
1272 smbfs_fake_server_stat (const char *server_url
, const char *path
, struct stat
*buf
)
1279 if ((p
= strrchr (path
, '/')))
1280 path
= p
+ 1; /* advance until last '/' */
1282 if (!current_info
->entries
) {
1283 if (!smbfs_loaddir (current_info
)) /* browse host */
1287 if (current_info
->server_list
== True
) {
1288 dentry
= current_info
->entries
;
1289 DEBUG (4, ("fake stat for SERVER \"%s\"\n", path
));
1291 if (strcmp (dentry
->text
, path
) == 0) {
1292 DEBUG (4, ("smbfs_fake_server_stat: %s:%4o\n",
1293 dentry
->text
, (int)dentry
->my_stat
.st_mode
));
1294 memcpy (buf
, &dentry
->my_stat
, sizeof (struct stat
));
1297 dentry
= dentry
->next
;
1305 smbfs_fake_share_stat (const char *server_url
, const char *path
, struct stat
*buf
)
1308 if (strlen (path
) < strlen (server_url
))
1311 if (!current_share_info
) { /* Server was not stat()ed */
1312 /* Make sure there is such share at server */
1313 smbfs_connection
*sc
;
1315 p
= smbfs_get_path (&sc
, path
);
1318 memset (buf
, 0, sizeof (*buf
));
1319 /* show this as dir */
1321 (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
|
1328 path
+= strlen (server_url
); /* we only want share name */
1331 if (*path
== '/') /* '/' leading server name */
1332 path
++; /* probably came from server browsing */
1334 if (!current_share_info
->entries
) {
1335 if (!smbfs_loaddir (current_share_info
)) /* browse host */
1338 dentry
= current_share_info
->entries
;
1339 DEBUG (3, ("smbfs_fake_share_stat: %s on %s\n", path
, server_url
));
1341 if (strcmp (dentry
->text
, path
) == 0) {
1342 DEBUG (6, ("smbfs_fake_share_stat: %s:%4o\n",
1343 dentry
->text
, (int) dentry
->my_stat
.st_mode
));
1344 memcpy (buf
, &dentry
->my_stat
, sizeof (struct stat
));
1347 dentry
= dentry
->next
;
1353 /* stat a single file, smbfs_get_remote_stat callback */
1354 static dir_entry
*single_entry
;
1356 /* stat a single file */
1358 smbfs_get_remote_stat (smbfs_connection
* sc
, const char *path
, struct stat
*buf
)
1360 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
1363 DEBUG (3, ("smbfs_get_remote_stat(): mypath:%s\n", path
));
1365 mypath
= smbfs_convert_path (path
, FALSE
);
1367 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1368 single_entry
= g_new (dir_entry
, 1);
1370 single_entry
->text
= dos_to_unix (g_strdup (finfo
->name
), 1);
1372 single_entry
->next
= 0;
1375 single_entry
= g_new0 (dir_entry
, 1);
1378 (sc
->cli
, mypath
, attribute
, smbfs_loaddir_helper
, single_entry
) < 1) {
1381 return -1; /* cli_list returns number of files */
1384 memcpy (buf
, &single_entry
->my_stat
, sizeof (struct stat
));
1386 /* don't free here, use for smbfs_fstat() */
1387 /* g_free(single_entry->text);
1388 g_free(single_entry); */
1394 smbfs_search_dir_entry (dir_entry
*dentry
, const char *text
, struct stat
*buf
)
1397 if (strcmp(text
, dentry
->text
) == 0) {
1398 memcpy(buf
, &dentry
->my_stat
, sizeof(struct stat
));
1399 memcpy(&single_entry
->my_stat
, &dentry
->my_stat
,
1400 sizeof(struct stat
));
1403 dentry
= dentry
->next
;
1409 smbfs_get_stat_info (smbfs_connection
* sc
, const char *path
, struct stat
*buf
)
1413 dir_entry
*dentry
= current_info
->entries
;
1415 const char *mypath
= path
;
1417 mypath
++; /* cut off leading '/' */
1418 if ((p
= strrchr (mypath
, '/')))
1419 mypath
= p
+ 1; /* advance until last file/dir name */
1420 DEBUG (3, ("smbfs_get_stat_info: mypath:%s, current_info->dirname:%s\n",
1421 mypath
, current_info
->dirname
));
1424 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1425 current_info
->dirname
, path
));
1429 if (!single_entry
) /* when found, this will be written too */
1430 single_entry
= g_new (dir_entry
, 1);
1431 if (smbfs_search_dir_entry (current_info
->entries
, mypath
, buf
) == 0) {
1434 /* now try to identify mypath as PARENT dir */
1438 mdp
= mydir
= g_strdup (current_info
->dirname
);
1439 if ((p
= strrchr (mydir
, '/')))
1440 *p
= 0; /* advance util last '/' */
1441 if ((p
= strrchr (mydir
, '/')))
1442 mydir
= p
+ 1; /* advance util last '/' */
1443 if (strcmp (mydir
, mypath
) == 0) { /* fake a stat for ".." */
1444 memset (buf
, 0, sizeof (struct stat
));
1445 buf
->st_mode
= (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
) & myumask
;
1446 memcpy (&single_entry
->my_stat
, buf
, sizeof (struct stat
));
1448 DEBUG (1, (" PARENT:found in %s\n", current_info
->dirname
));
1453 /* now try to identify as CURRENT dir? */
1455 char *dnp
= current_info
->dirname
;
1456 DEBUG (6, ("smbfs_get_stat_info: is %s current dir? this dir is: %s\n",
1457 mypath
, current_info
->dirname
));
1463 if (strcmp (mypath
, dnp
) == 0) {
1464 memset (buf
, 0, sizeof (struct stat
));
1465 buf
->st_mode
= (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
) & myumask
;
1466 memcpy (&single_entry
->my_stat
, buf
, sizeof (struct stat
));
1467 DEBUG (1, (" CURRENT:found in %s\n", current_info
->dirname
));
1471 DEBUG (3, ("'%s' not found in current_info '%s'\n", path
,
1472 current_info
->dirname
));
1473 /* try to find this in the PREVIOUS listing */
1474 if (previous_info
) {
1475 if (smbfs_search_dir_entry (previous_info
->entries
, mypath
, buf
) == 0)
1477 DEBUG (3, ("'%s' not found in previous_info '%s'\n", path
,
1478 previous_info
->dirname
));
1480 /* try to find this in the SHARE listing */
1481 if (current_share_info
) {
1482 if (smbfs_search_dir_entry (current_share_info
->entries
, mypath
, buf
) == 0)
1484 DEBUG (3, ("'%s' not found in share_info '%s'\n", path
,
1485 current_share_info
->dirname
));
1487 /* try to find this in the SERVER listing */
1488 if (current_server_info
) {
1489 if (smbfs_search_dir_entry (current_server_info
->entries
, mypath
, buf
) == 0)
1491 DEBUG (3, ("'%s' not found in server_info '%s'\n", path
,
1492 current_server_info
->dirname
));
1494 /* nothing found. get stat file info from server */
1495 return smbfs_get_remote_stat (sc
, path
, buf
);
1499 smbfs_chdir (struct vfs_class
*me
, const char *path
)
1502 smbfs_connection
*sc
;
1506 DEBUG (3, ("smbfs_chdir(path:%s)\n", path
));
1507 if (!(remote_dir
= smbfs_get_path (&sc
, path
)))
1509 g_free (remote_dir
);
1515 smbfs_loaddir_by_name (struct vfs_class
*me
, const char *path
)
1520 mypath
= g_strdup(path
);
1521 p
= strrchr(mypath
, '/');
1525 DEBUG(6, ("smbfs_loaddir_by_name(%s)\n", mypath
));
1526 smbfs_chdir(me
, mypath
);
1527 info
= smbfs_opendir (me
, mypath
);
1531 smbfs_readdir(info
);
1532 smbfs_loaddir(info
);
1537 smbfs_stat (struct vfs_class
* me
, const char *path
, struct stat
*buf
)
1539 smbfs_connection
*sc
;
1541 char *service
, *pp
, *at
;
1544 DEBUG (3, ("smbfs_stat(path:%s)\n", path
));
1546 if (!current_info
) {
1547 DEBUG (1, ("current_info = NULL: "));
1548 if (smbfs_loaddir_by_name (me
, path
) < 0)
1552 /* check if stating server */
1554 if (strncmp (p
, URL_HEADER
, HEADER_LEN
)) {
1555 DEBUG (1, ("'%s' doesnt start with '%s' (length %d)\n",
1556 p
, URL_HEADER
, HEADER_LEN
));
1564 pp
= strchr (p
, '/'); /* advance past next '/' */
1565 at
= strchr (p
, '@');
1566 pstrcpy (server_url
, URL_HEADER
);
1567 if (at
&& at
< pp
) { /* user@server */
1568 char *z
= &(server_url
[sizeof (server_url
) - 1]);
1571 at
= &(server_url
[HEADER_LEN
]) + (at
- p
+ 1);
1574 at
= &(server_url
[HEADER_LEN
]);
1579 pstrcat (server_url
, current_bucket
->host
);
1582 if (!current_info
->server_list
) {
1583 if (smbfs_loaddir_by_name (me
, path
) < 0)
1586 return smbfs_fake_server_stat (server_url
, path
, buf
);
1589 if (!strchr (++pp
, '/')) {
1590 return smbfs_fake_share_stat (server_url
, path
, buf
);
1593 /* stating inside share at this point */
1594 if (!(service
= smbfs_get_path (&sc
, path
))) /* connects if necessary */
1597 int hostlen
= strlen (current_bucket
->host
);
1598 char *ppp
= service
+ strlen (service
) - hostlen
;
1599 char *sp
= server_url
+ strlen (server_url
) - hostlen
;
1601 if (strcmp (sp
, ppp
) == 0) {
1602 /* make server name appear as directory */
1603 DEBUG (1, ("smbfs_stat: showing server as directory\n"));
1604 memset (buf
, 0, sizeof (struct stat
));
1605 buf
->st_mode
= (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
) & myumask
;
1610 /* check if current_info is in share requested */
1612 pp
= strchr (p
, '/');
1614 p
= ++pp
; /* advance past server name */
1615 pp
= strchr (p
, '/');
1618 *pp
= 0; /* cut off everthing after service name */
1620 p
= IPC
; /* browsing for services */
1621 pp
= current_info
->dirname
;
1624 if (strncmp (p
, pp
, strlen (p
)) != 0) {
1625 DEBUG (6, ("desired '%s' is not loaded, we have '%s'\n", p
, pp
));
1626 if (smbfs_loaddir_by_name (me
, path
) < 0) {
1630 DEBUG (6, ("loaded dir: '%s'\n", current_info
->dirname
));
1633 /* stat dirs & files under shares now */
1634 return smbfs_get_stat_info (sc
, path
, buf
);
1637 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1640 smbfs_lseek (void *data
, off_t offset
, int whence
)
1642 smbfs_handle
*info
= (smbfs_handle
*) data
;
1646 ("smbfs_lseek(info->nread => %d, offset => %d, whence => %d) \n",
1647 (int) info
->nread
, (int) offset
, whence
));
1651 info
->nread
= offset
;
1654 info
->nread
+= offset
;
1657 if (!cli_qfileinfo (info
->cli
, info
->fnum
,
1658 NULL
, &size
, NULL
, NULL
, NULL
,
1660 !cli_getattrE (info
->cli
, info
->fnum
,
1661 NULL
, &size
, NULL
, NULL
, NULL
)) {
1665 info
->nread
= size
+ offset
;
1673 smbfs_mknod (struct vfs_class
*me
, const char *path
, int mode
, int dev
)
1677 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path
, mode
, dev
));
1678 my_errno
= EOPNOTSUPP
;
1683 smbfs_mkdir (struct vfs_class
* me
, const char *path
, mode_t mode
)
1685 smbfs_connection
*sc
;
1691 DEBUG (3, ("smbfs_mkdir(path:%s, mode:%d)\n", path
, (int) mode
));
1692 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1694 g_free (remote_file
);
1695 cpath
= smbfs_convert_path (path
, FALSE
);
1697 if (!cli_mkdir (sc
->cli
, cpath
)) {
1698 my_errno
= cli_error (sc
->cli
, NULL
, &err
, NULL
);
1699 message (D_ERROR
, MSG_ERROR
, _(" Error %s creating directory %s "),
1700 cli_errstr (sc
->cli
), CNV_LANG (cpath
));
1709 smbfs_rmdir (struct vfs_class
*me
, const char *path
)
1711 smbfs_connection
*sc
;
1717 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path
));
1718 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1720 g_free (remote_file
);
1721 cpath
= smbfs_convert_path (path
, FALSE
);
1723 if (!cli_rmdir(sc
->cli
, cpath
)) {
1724 my_errno
= cli_error(sc
->cli
, NULL
, &err
, NULL
);
1725 message (D_ERROR
, MSG_ERROR
, _(" Error %s removing directory %s "),
1726 cli_errstr(sc
->cli
), CNV_LANG(cpath
));
1736 smbfs_link (struct vfs_class
*me
, const char *p1
, const char *p2
)
1740 DEBUG (3, ("smbfs_link(p1:%s, p2:%s)\n", p1
, p2
));
1741 my_errno
= EOPNOTSUPP
;
1746 smbfs_free (vfsid id
)
1748 DEBUG (3, ("smbfs_free(%p)\n", id
));
1749 smbfs_auth_free_all ();
1752 /* Gives up on a socket and reopens the connection, the child own the socket
1756 smbfs_forget (const char *path
)
1758 char *host
, *user
, *p
;
1761 if (strncmp (path
, URL_HEADER
, HEADER_LEN
))
1764 DEBUG (3, ("smbfs_forget(path:%s)\n", path
));
1767 if (path
[0] == '/' && path
[1] == '/')
1770 if ((p
= smbfs_get_host_and_username (&path
, &host
, &user
, &port
, NULL
))) {
1772 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1773 if (smbfs_connections
[i
].cli
1774 && (strcmp (host
, smbfs_connections
[i
].host
) == 0)
1775 && (strcmp (user
, smbfs_connections
[i
].user
) == 0)
1776 && (port
== smbfs_connections
[i
].port
)) {
1778 /* close socket: the child owns it now */
1779 cli_shutdown (smbfs_connections
[i
].cli
);
1781 /* reopen the connection */
1782 smbfs_connections
[i
].cli
=
1783 smbfs_do_connect (host
, smbfs_connections
[i
].service
);
1792 smbfs_setctl (struct vfs_class
*me
, const char *path
, int ctlop
, void *arg
)
1797 DEBUG (3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path
, ctlop
));
1799 case VFS_SETCTL_FORGET
:
1800 smbfs_forget (path
);
1807 static smbfs_handle
*
1808 smbfs_open_readwrite (smbfs_handle
*remote_handle
, char *rname
, int flags
, int mode
)
1814 if (flags
& O_TRUNC
) /* if it exists truncate to zero */
1815 DEBUG (3, ("smbfs_open: O_TRUNC\n"));
1817 remote_handle
->fnum
=
1818 #if 1 /* Don't play with flags, it is cli_open() headache */
1819 cli_open (remote_handle
->cli
, rname
, flags
, DENY_NONE
);
1820 #else /* What's a reasons to has this code ? */
1821 cli_open (remote_handle
->cli
, rname
, ((flags
& O_CREAT
)
1823 (O_WRONLY
| O_APPEND
))) ?
1824 flags
: O_RDONLY
, DENY_NONE
);
1826 if (remote_handle
->fnum
== -1) {
1827 message (D_ERROR
, MSG_ERROR
, _(" %s opening remote file %s "),
1828 cli_errstr (remote_handle
->cli
), CNV_LANG (rname
));
1829 DEBUG (1, ("smbfs_open(rname:%s) error:%s\n",
1830 rname
, cli_errstr (remote_handle
->cli
)));
1831 my_errno
= cli_error (remote_handle
->cli
, NULL
, &err
, NULL
);
1835 if (flags
& O_CREAT
)
1836 return remote_handle
;
1838 if (!cli_qfileinfo (remote_handle
->cli
, remote_handle
->fnum
,
1839 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
, NULL
,
1841 && !cli_getattrE (remote_handle
->cli
, remote_handle
->fnum
,
1842 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
)) {
1843 message (D_ERROR
, MSG_ERROR
, " getattrib: %s ",
1844 cli_errstr (remote_handle
->cli
));
1846 ("smbfs_open(rname:%s) getattrib:%s\n", rname
,
1847 cli_errstr (remote_handle
->cli
)));
1848 my_errno
= cli_error (remote_handle
->cli
, NULL
, &err
, NULL
);
1849 cli_close (remote_handle
->cli
, remote_handle
->fnum
);
1853 if ((flags
== (O_WRONLY
| O_APPEND
)) /* file.c:copy_file_file() -> do_append */
1854 && smbfs_lseek (remote_handle
, 0, SEEK_END
) == -1) {
1855 cli_close (remote_handle
->cli
, remote_handle
->fnum
);
1859 return remote_handle
;
1863 smbfs_open (struct vfs_class
*me
, const char *file
, int flags
, int mode
)
1867 smbfs_connection
*sc
;
1868 smbfs_handle
*remote_handle
;
1872 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%o)\n", file
, flags
, mode
));
1874 if (!(remote_file
= smbfs_get_path (&sc
, file
)))
1877 remote_file
= free_after(smbfs_convert_path (remote_file
, FALSE
), remote_file
);
1879 remote_handle
= g_new (smbfs_handle
, 2);
1880 remote_handle
->cli
= sc
->cli
;
1881 remote_handle
->nread
= 0;
1883 ret
= smbfs_open_readwrite (remote_handle
, remote_file
, flags
, mode
);
1885 g_free (remote_file
);
1887 g_free (remote_handle
);
1893 smbfs_unlink (struct vfs_class
*me
, const char *path
)
1895 smbfs_connection
*sc
;
1900 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1903 remote_file
= free_after(smbfs_convert_path (remote_file
, FALSE
), remote_file
);
1905 if (!cli_unlink(sc
->cli
, remote_file
)) {
1906 message (D_ERROR
, MSG_ERROR
, _(" %s removing remote file %s "),
1907 cli_errstr(sc
->cli
), CNV_LANG(remote_file
));
1908 g_free (remote_file
);
1911 g_free (remote_file
);
1916 smbfs_rename (struct vfs_class
*me
, const char *a
, const char *b
)
1918 smbfs_connection
*sc
;
1924 if ((ra
= smbfs_get_path (&sc
, a
)) == 0)
1927 if ((rb
= smbfs_get_path (&sc
, b
)) == 0) {
1932 ra
= free_after (smbfs_convert_path (ra
, FALSE
), ra
);
1933 rb
= free_after (smbfs_convert_path (rb
, FALSE
), rb
);
1935 retval
= cli_rename(sc
->cli
, ra
, rb
);
1941 message (D_ERROR
, MSG_ERROR
, _(" %s renaming files\n"),
1942 cli_errstr(sc
->cli
));
1949 smbfs_fstat (void *data
, struct stat
*buf
)
1951 smbfs_handle
*remote_handle
= (smbfs_handle
*)data
;
1953 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle
->fnum
));
1955 /* use left over from previous smbfs_get_remote_stat, if available */
1957 memcpy(buf
, &single_entry
->my_stat
, sizeof(struct stat
));
1958 else { /* single_entry not set up: bug */
1968 vfs_smbfs_ops
.name
= "smbfs";
1969 vfs_smbfs_ops
.prefix
= "smb:";
1970 vfs_smbfs_ops
.flags
= VFSF_NOLINKS
;
1971 vfs_smbfs_ops
.init
= smbfs_init
;
1972 vfs_smbfs_ops
.fill_names
= smbfs_fill_names
;
1973 vfs_smbfs_ops
.open
= smbfs_open
;
1974 vfs_smbfs_ops
.close
= smbfs_close
;
1975 vfs_smbfs_ops
.read
= smbfs_read
;
1976 vfs_smbfs_ops
.write
= smbfs_write
;
1977 vfs_smbfs_ops
.opendir
= smbfs_opendir
;
1978 vfs_smbfs_ops
.readdir
= smbfs_readdir
;
1979 vfs_smbfs_ops
.closedir
= smbfs_closedir
;
1980 vfs_smbfs_ops
.stat
= smbfs_stat
;
1981 vfs_smbfs_ops
.lstat
= smbfs_lstat
;
1982 vfs_smbfs_ops
.fstat
= smbfs_fstat
;
1983 vfs_smbfs_ops
.chmod
= smbfs_chmod
;
1984 vfs_smbfs_ops
.chown
= smbfs_chown
;
1985 vfs_smbfs_ops
.utime
= smbfs_utime
;
1986 vfs_smbfs_ops
.readlink
= smbfs_readlink
;
1987 vfs_smbfs_ops
.symlink
= smbfs_symlink
;
1988 vfs_smbfs_ops
.link
= smbfs_link
;
1989 vfs_smbfs_ops
.unlink
= smbfs_unlink
;
1990 vfs_smbfs_ops
.rename
= smbfs_rename
;
1991 vfs_smbfs_ops
.chdir
= smbfs_chdir
;
1992 vfs_smbfs_ops
.ferrno
= smbfs_errno
;
1993 vfs_smbfs_ops
.lseek
= smbfs_lseek
;
1994 vfs_smbfs_ops
.mknod
= smbfs_mknod
;
1995 vfs_smbfs_ops
.free
= smbfs_free
;
1996 vfs_smbfs_ops
.mkdir
= smbfs_mkdir
;
1997 vfs_smbfs_ops
.rmdir
= smbfs_rmdir
;
1998 vfs_smbfs_ops
.setctl
= smbfs_setctl
;
1999 vfs_register_class (&vfs_smbfs_ops
);