1 /* Virtual File System: Midnight Commander file system.
3 Copyright (C) 1995, 1996, 1997 The Free Software Foundation
5 Written by Wayne Roberts <wroberts1@home.com>, 1997
6 Andrew V. Samoilov <sav@bcs.zp.ua> 2002, 2003
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License
10 as published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Namespace: exports init_smbfs, smbfs_set_debug(), smbfs_set_debugf() */
25 #include <sys/types.h>
27 #undef USE_NCURSES /* Don't include *curses.h */
30 #undef PACKAGE_BUGREPORT
33 #undef PACKAGE_TARNAME
34 #undef PACKAGE_VERSION
36 #include "samba/include/config.h"
37 /* don't load crap in "samba/include/includes.h" we don't use and which
38 conflicts with definitions in other includes */
39 #undef HAVE_LIBREADLINE
43 #include "samba/include/includes.h"
51 #define SMBFS_MAX_CONNECTIONS 16
52 static const char * const IPC
= "IPC$";
53 static const char * const URL_HEADER
= "/#smb:";
59 /* stuff that is same with each connection */
60 extern int DEBUGLEVEL
;
61 extern pstring myhostname
;
62 static mode_t myumask
= 0755;
63 extern pstring global_myname
;
64 static int smbfs_open_connections
= 0;
65 static gboolean got_user
= FALSE
;
66 static gboolean got_pass
= FALSE
;
67 static pstring password
;
68 static pstring username
;
69 static struct vfs_class vfs_smbfs_ops
;
71 static struct _smbfs_connection
{
72 struct cli_state
*cli
;
73 struct in_addr dest_ip
;
75 char *host
; /* server name */
76 char *service
; /* share name */
84 } smbfs_connections
[SMBFS_MAX_CONNECTIONS
];
85 /* unique to each connection */
88 static struct cli_state
* smbfs_do_connect (const char *server
, char *share
);
90 typedef struct _smbfs_connection smbfs_connection
;
91 static smbfs_connection
*current_bucket
;
94 struct cli_state
*cli
;
100 static GSList
*auth_list
;
102 static inline BOOL
dbghdr_wrapper (int level
, const char *file
, const char *func
, int line
)
104 return dbghdr (level
, const_cast(char *, file
), const_cast(char *, func
), line
);
106 #define dbghdr dbghdr_wrapper
109 smbfs_auth_free (struct smb_authinfo
const *a
)
115 wipe_password (a
->password
);
119 smbfs_auth_free_all (void)
122 g_slist_foreach (auth_list
, (GFunc
)smbfs_auth_free
, 0);
123 g_slist_free (auth_list
);
129 smbfs_auth_cmp_host_and_share (gconstpointer _a
, gconstpointer _b
)
131 struct smb_authinfo
const *a
= (struct smb_authinfo
const *)_a
;
132 struct smb_authinfo
const *b
= (struct smb_authinfo
const *)_b
;
134 if (!a
->host
|| !a
->share
|| !b
->host
|| !b
->share
)
136 if (strcmp (a
->host
, b
->host
) != 0)
138 if (strcmp (a
->share
, b
->share
) != 0)
144 smbfs_auth_cmp_host (gconstpointer _a
, gconstpointer _b
)
146 struct smb_authinfo
const *a
= (struct smb_authinfo
const *)_a
;
147 struct smb_authinfo
const *b
= (struct smb_authinfo
const *)_b
;
149 if (!a
->host
|| !b
->host
)
151 if (strcmp (a
->host
, b
->host
) != 0)
153 if (strcmp (a
->share
, IPC
) != 0)
159 smbfs_auth_add (const char *host
, const char *share
, const char *domain
,
160 const char *user
, const char *password
)
162 struct smb_authinfo
*auth
= g_new (struct smb_authinfo
, 1);
167 /* Don't check for NULL, g_strdup already does. */
168 auth
->host
= g_strdup (host
);
169 auth
->share
= g_strdup (share
);
170 auth
->domain
= g_strdup (domain
);
171 auth
->user
= g_strdup (user
);
172 auth
->password
= g_strdup (password
);
173 auth_list
= g_slist_prepend (auth_list
, auth
);
177 smbfs_auth_remove (const char *host
, const char *share
)
179 struct smb_authinfo data
;
180 struct smb_authinfo
*auth
;
183 data
.host
= g_strdup (host
);
184 data
.share
= g_strdup (share
);
185 list
= g_slist_find_custom (auth_list
,
187 smbfs_auth_cmp_host_and_share
);
193 auth_list
= g_slist_remove (auth_list
, auth
);
194 smbfs_auth_free (auth
);
197 /* Set authentication information in bucket. Return 1 if successful, else 0 */
198 /* Information in auth_list overrides user if pass is NULL. */
199 /* bucket->host and bucket->service must be valid. */
201 smbfs_bucket_set_authinfo (smbfs_connection
*bucket
,
202 const char *domain
, const char *user
, const char *pass
,
203 int fallback_to_host
)
205 struct smb_authinfo data
;
206 struct smb_authinfo
*auth
;
209 if (domain
&& user
&& pass
) {
210 g_free (bucket
->domain
);
211 g_free (bucket
->user
);
212 g_free (bucket
->password
);
213 bucket
->domain
= g_strdup (domain
);
214 bucket
->user
= g_strdup (user
);
215 bucket
->password
= g_strdup (pass
);
216 smbfs_auth_remove (bucket
->host
, bucket
->service
);
217 smbfs_auth_add (bucket
->host
, bucket
->service
,
222 data
.host
= bucket
->host
;
223 data
.share
= bucket
->service
;
224 list
= g_slist_find_custom (auth_list
, &data
, smbfs_auth_cmp_host_and_share
);
225 if (!list
&& fallback_to_host
)
226 list
= g_slist_find_custom (auth_list
, &data
, smbfs_auth_cmp_host
);
229 bucket
->domain
= g_strdup (auth
->domain
);
230 bucket
->user
= g_strdup (auth
->user
);
231 bucket
->password
= g_strdup (auth
->password
);
236 bucket
->domain
= g_strdup (lp_workgroup ());
237 bucket
->user
= g_strdup (got_user
? username
: user
);
238 bucket
->password
= g_strdup (password
);
242 auth
= vfs_smb_get_authinfo (bucket
->host
,
244 (domain
? domain
: lp_workgroup ()),
247 g_free (bucket
->domain
);
248 g_free (bucket
->user
);
249 g_free (bucket
->password
);
250 bucket
->domain
= g_strdup (auth
->domain
);
251 bucket
->user
= g_strdup (auth
->user
);
252 bucket
->password
= g_strdup (auth
->password
);
253 smbfs_auth_remove (bucket
->host
, bucket
->service
);
254 auth_list
= g_slist_prepend (auth_list
, auth
);
261 smbfs_set_debug (int arg
)
267 smbfs_set_debugf (const char *filename
)
269 extern pstring debugf
;
271 if (DEBUGLEVEL
> 0) {
272 FILE *outfile
= fopen (filename
, "w");
274 setup_logging (const_cast(char *, ""), True
); /* No needs for timestamp for each message */
277 pstrcpy (debugf
, filename
);
282 /********************** The callbacks ******************************/
284 smbfs_init (struct vfs_class
* me
)
286 const char *servicesf
= CONFIGDIR PATH_SEP_STR
"smb.conf";
288 /* DEBUGLEVEL = 4; */
291 charset_initialise ();
293 DEBUG (3, ("smbfs_init(%s)\n", me
->name
));
295 if (!get_myname (myhostname
, NULL
))
296 DEBUG (0, ("Failed to get my hostname.\n"));
298 if (!lp_load (const_cast(char *, servicesf
), True
, False
, False
))
299 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf
));
301 codepage_initialise (lp_client_code_page ());
309 if (getenv ("USER")) {
312 pstrcpy (username
, getenv ("USER"));
314 DEBUG (3, ("smbfs_init(): $USER:%s\n", username
));
315 if ((p
= strchr (username
, '%'))) {
317 pstrcpy (password
, p
+ 1);
319 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password
));
320 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n",
321 username
, password
));
325 if (getenv ("PASSWD")) {
326 pstrcpy (password
, getenv ("PASSWD"));
333 smbfs_fill_names (struct vfs_class
*me
, fill_names_f func
)
337 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
338 if (smbfs_connections
[i
].cli
) {
339 path
= g_strconcat (URL_HEADER
,
340 smbfs_connections
[i
].user
, "@",
341 smbfs_connections
[i
].host
,
342 "/", smbfs_connections
[i
].service
,
350 #define CNV_LANG(s) dos_to_unix(s,False)
351 #define GNAL_VNC(s) unix_to_dos(s,False)
352 /* does same as do_get() in client.c */
353 /* called from vfs.c:1080, count = buffer size */
355 smbfs_read (void *data
, char *buffer
, int count
)
357 smbfs_handle
*info
= (smbfs_handle
*) data
;
360 DEBUG(3, ("smbfs_read(fnum:%d, nread:%d, count:%d)\n",
361 info
->fnum
, (int)info
->nread
, count
));
362 n
= cli_read(info
->cli
, info
->fnum
, buffer
, info
->nread
, count
);
369 smbfs_write (void *data
, const char *buf
, int nbyte
)
371 smbfs_handle
*info
= (smbfs_handle
*) data
;
374 DEBUG(3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%d)\n",
375 info
->fnum
, (int)info
->nread
, nbyte
));
376 n
= cli_write(info
->cli
, info
->fnum
, 0, const_cast(char *, buf
), info
->nread
, nbyte
);
383 smbfs_close (void *data
)
385 smbfs_handle
*info
= (smbfs_handle
*) data
;
386 DEBUG (3, ("smbfs_close(fnum:%d)\n", info
->fnum
));
388 /* FIXME: Why too different cli have the same outbuf
389 * if file is copied to share
391 if (info
->cli
->outbuf
== NULL
) {
396 /* if imlementing archive_level: add rname to smbfs_handle */
397 if (archive_level
>= 2 && (inf
->attr
& aARCH
)) {
398 cli_setatr (info
->cli
, rname
, info
->attr
& ~(uint16
) aARCH
, 0);
401 return (cli_close (info
->cli
, info
->fnum
) == True
) ? 0 : -1;
405 smbfs_errno (struct vfs_class
*me
)
407 DEBUG(3, ("smbfs_errno: %s\n", g_strerror(my_errno
)));
411 typedef struct dir_entry
{
413 struct dir_entry
*next
;
419 gboolean server_list
;
421 char *path
; /* the dir originally passed to smbfs_opendir */
422 smbfs_connection
*conn
;
431 *current_server_info
;
433 static gboolean first_direntry
;
436 smbfs_new_dir_entry (const char *name
)
438 static int inode_counter
;
439 dir_entry
*new_entry
;
440 new_entry
= g_new0 (dir_entry
, 1);
441 new_entry
->text
= dos_to_unix (g_strdup (name
), 1);
443 if (first_direntry
) {
444 current_info
->entries
= new_entry
;
445 first_direntry
= FALSE
;
447 current_info
->current
->next
= new_entry
;
449 current_info
->current
= new_entry
;
450 new_entry
->my_stat
.st_ino
= inode_counter
++;
455 /* browse for shares on server */
457 smbfs_browsing_helper (const char *name
, uint32 type
, const char *comment
, void *state
)
459 const char *typestr
= "";
461 dir_entry
*new_entry
= smbfs_new_dir_entry (name
);
466 /* show this as dir */
467 new_entry
->my_stat
.st_mode
=
468 (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
|
481 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name
, typestr
, comment
));
485 smbfs_loaddir_helper (file_info
* finfo
, const char *mask
, void *entry
)
487 dir_entry
*new_entry
= (dir_entry
*) entry
;
488 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
489 #if 0 /* I want to see dot files */
490 if (finfo
->mode
& aHIDDEN
)
491 return; /* don't bother with hidden files, "~$" screws up mc */
494 new_entry
= smbfs_new_dir_entry (finfo
->name
);
496 new_entry
->my_stat
.st_size
= finfo
->size
;
497 new_entry
->my_stat
.st_mtime
= finfo
->mtime
;
498 new_entry
->my_stat
.st_atime
= finfo
->atime
;
499 new_entry
->my_stat
.st_ctime
= finfo
->ctime
;
500 new_entry
->my_stat
.st_uid
= finfo
->uid
;
501 new_entry
->my_stat
.st_gid
= finfo
->gid
;
503 new_entry
->my_stat
.st_mode
= /* rw-rw-rw */
504 S_IRUSR
| S_IRGRP
| S_IROTH
| S_IWUSR
| S_IWGRP
| S_IWOTH
;
506 /* if (finfo->mode & aVOLID); nothing similar in real world */
507 if (finfo
->mode
& aDIR
)
508 new_entry
->my_stat
.st_mode
|= /* drwxrwxrwx */
509 S_IFDIR
| S_IXUSR
| S_IXGRP
| S_IXOTH
;
511 new_entry
->my_stat
.st_mode
|= S_IFREG
; /* if not dir, regular file? */
512 /* if (finfo->mode & aARCH); DOS archive */
513 /* if (finfo->mode & aHIDDEN); like a dot file? */
514 /* if (finfo->mode & aSYSTEM); like a kernel? */
515 if (finfo
->mode
& aRONLY
)
516 new_entry
->my_stat
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
517 new_entry
->my_stat
.st_mode
&= myumask
;
519 DEBUG (entry
? 3 : 6, (" %-30s%7.7s%8.0f %s",
520 CNV_LANG (finfo
->name
),
521 attrib_string (finfo
->mode
),
522 (double) finfo
->size
,
523 asctime (LocalTime (&t
))));
526 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
528 smbfs_convert_path (const char *remote_file
, gboolean trailing_asterik
)
530 const char *p
, *my_remote
;
533 my_remote
= remote_file
;
534 if (strncmp (my_remote
, URL_HEADER
, HEADER_LEN
) == 0) { /* if passed directly */
535 my_remote
+= HEADER_LEN
;
536 if (*my_remote
== '/') /* from server browsing */
538 p
= strchr(my_remote
, '/');
540 my_remote
= p
+1; /* advance to end of server name */
543 if (*my_remote
== '/')
544 my_remote
++; /* strip off leading '/' */
545 p
= strchr(my_remote
, '/');
547 my_remote
= p
; /* strip off share/service name */
548 /* create remote filename as understood by smb clientgen */
549 result
= g_strconcat (my_remote
, trailing_asterik
? "/*" : "", 0);
550 unix_to_dos (result
, /* inplace = */ 1); /* code page conversion */
551 str_replace(result
, '/', '\\');
556 smbfs_srv_browsing_helper (const char *name
, uint32 m
, const char *comment
,
559 dir_entry
*new_entry
= smbfs_new_dir_entry (name
);
561 /* show this as dir */
562 new_entry
->my_stat
.st_mode
=
563 (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
|
566 DEBUG (3, ("\t%-16.16s %s\n", name
, comment
));
570 smbfs_reconnect(smbfs_connection
*conn
, int *retries
)
573 DEBUG(3, ("RECONNECT\n"));
575 if (*(conn
->host
) == 0)
576 host
= g_strdup(conn
->cli
->desthost
); /* server browsing */
578 host
= g_strdup(conn
->host
);
580 cli_shutdown(conn
->cli
);
582 if (!(conn
->cli
= smbfs_do_connect(host
, conn
->service
))) {
583 message (1, MSG_ERROR
,
584 _(" reconnect to %s failed\n "), conn
->host
);
589 if (++(*retries
) == 2)
595 smbfs_send(struct cli_state
*cli
)
601 len
= smb_len(cli
->outbuf
) + 4;
603 while (nwritten
< len
) {
604 ret
= write_socket(cli
->fd
, cli
->outbuf
+nwritten
, len
- nwritten
);
615 /****************************************************************************
616 See if server has cut us off by checking for EPIPE when writing.
617 Taken from cli_chkpath()
618 ****************************************************************************/
620 smbfs_chkpath(struct cli_state
*cli
, const char *path
, BOOL send_only
)
626 unix_to_dos (path2
, 1);
627 trim_string(path2
,NULL
,"\\");
628 if (!*path2
) *path2
= '\\';
630 memset(cli
->outbuf
,'\0',smb_size
);
631 set_message(cli
->outbuf
,0,4 + strlen(path2
),True
);
632 SCVAL(cli
->outbuf
,smb_com
,SMBchkpth
);
633 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
637 SSVAL(cli
->outbuf
,smb_pid
,cli
->pid
);
638 SSVAL(cli
->outbuf
,smb_uid
,cli
->vuid
);
639 SSVAL(cli
->outbuf
,smb_mid
,cli
->mid
);
640 if (cli
->protocol
> PROTOCOL_CORE
) {
641 SCVAL(cli
->outbuf
,smb_flg
,0x8);
642 SSVAL(cli
->outbuf
,smb_flg2
,0x1);
645 p
= smb_buf(cli
->outbuf
);
649 if (!smbfs_send(cli
)) {
650 DEBUG(3, ("smbfs_chkpath: couldnt send\n"));
654 client_receive_smb(cli
->fd
, cli
->inbuf
, cli
->timeout
);
655 DEBUG(3, ("smbfs_chkpath: send only OK\n"));
656 return True
; /* just testing for EPIPE */
658 if (!client_receive_smb(cli
->fd
, cli
->inbuf
, cli
->timeout
)) {
659 DEBUG(3, ("smbfs_chkpath: receive error\n"));
662 if ((my_errno
= cli_error(cli
, NULL
, NULL
, NULL
))) {
663 if (my_errno
== 20 || my_errno
== 13)
664 return True
; /* ignore if 'not a directory' error */
665 DEBUG(3, ("smbfs_chkpath: cli_error: %s\n", g_strerror(my_errno
)));
674 smbfs_fs (const char *text
)
676 const char *p
= text
;
679 while ((p
= strchr(p
, '/')) != NULL
) {
690 smbfs_loaddir (opendir_info
*smbfs_info
)
692 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
693 int servlen
= strlen (smbfs_info
->conn
->service
);
694 const char *info_dirname
= smbfs_info
->dirname
;
697 DEBUG (3, ("smbfs_loaddir: dirname:%s\n", info_dirname
));
698 first_direntry
= TRUE
;
702 ("smbfs_loaddir: new:'%s', cached:'%s'\n", info_dirname
,
703 current_info
->dirname
));
704 /* if new desired dir is longer than cached in current_info */
705 if (smbfs_fs (info_dirname
) > smbfs_fs (current_info
->dirname
)) {
706 DEBUG (3, ("saving to previous_info\n"));
707 previous_info
= current_info
;
711 current_info
= smbfs_info
;
713 if (strcmp (info_dirname
, "/") == 0) {
714 if (!strcmp (smbfs_info
->path
, URL_HEADER
)) {
715 DEBUG (6, ("smbfs_loaddir: browsing %s\n", IPC
));
716 /* browse for servers */
717 if (!cli_NetServerEnum
718 (smbfs_info
->conn
->cli
, smbfs_info
->conn
->domain
,
719 SV_TYPE_ALL
, smbfs_srv_browsing_helper
, NULL
))
722 current_server_info
= smbfs_info
;
723 smbfs_info
->server_list
= TRUE
;
725 /* browse for shares */
726 if (cli_RNetShareEnum
727 (smbfs_info
->conn
->cli
, smbfs_browsing_helper
, NULL
) < 1)
730 current_share_info
= smbfs_info
;
735 /* do regular directory listing */
736 if (strncmp (smbfs_info
->conn
->service
, info_dirname
+ 1, servlen
) == 0) {
737 /* strip share name from dir */
738 my_dirname
= g_strdup (info_dirname
+ servlen
);
740 my_dirname
= free_after(smbfs_convert_path (my_dirname
, TRUE
), my_dirname
);
742 my_dirname
= smbfs_convert_path (info_dirname
, TRUE
);
744 DEBUG (6, ("smbfs_loaddir: service: %s\n", smbfs_info
->conn
->service
));
746 ("smbfs_loaddir: cli->share: %s\n",
747 smbfs_info
->conn
->cli
->share
));
749 ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname
));
750 /* do file listing: cli_list returns number of files */
752 (smbfs_info
->conn
->cli
, my_dirname
, attribute
,
753 smbfs_loaddir_helper
, NULL
) < 0) {
754 /* cli_list returns -1 if directory empty or cannot read socket */
755 my_errno
= cli_error (smbfs_info
->conn
->cli
, NULL
, &err
, NULL
);
759 if (*(my_dirname
) == 0)
760 smbfs_info
->dirname
= smbfs_info
->conn
->service
;
765 /* current_info->parent = smbfs_info->dirname; */
767 smbfs_info
->current
= smbfs_info
->entries
;
768 return 1; /* 1 = ok */
771 #ifdef SMBFS_FREE_DIR
773 smbfs_free_dir (dir_entry
*de
)
777 smbfs_free_dir (de
->next
);
784 /* The readdir routine loads the complete directory */
785 /* It's too slow to ask the server each time */
786 /* It now also sends the complete lstat information for each file */
788 smbfs_readdir(void *info
)
790 static union vfs_dirent smbfs_readdir_data
;
791 static char *const dirent_dest
= smbfs_readdir_data
.dent
.d_name
;
792 opendir_info
*smbfs_info
= (opendir_info
*) info
;
794 DEBUG(4, ("smbfs_readdir(%s)\n", smbfs_info
->dirname
));
796 if (!smbfs_info
->entries
)
797 if (!smbfs_loaddir(smbfs_info
))
800 if (smbfs_info
->current
== 0) { /* reached end of dir entries */
801 DEBUG(3, ("smbfs_readdir: smbfs_info->current = 0\n"));
802 #ifdef SMBFS_FREE_DIR
803 smbfs_free_dir(smbfs_info
->entries
);
804 smbfs_info
->entries
= 0;
808 g_strlcpy(dirent_dest
, smbfs_info
->current
->text
, MC_MAXPATHLEN
);
809 smbfs_info
->current
= smbfs_info
->current
->next
;
811 compute_namelen(&smbfs_readdir_data
.dent
);
813 return &smbfs_readdir_data
;
817 smbfs_closedir (void *info
)
819 opendir_info
*smbfs_info
= (opendir_info
*) info
;
820 /* dir_entry *p, *q; */
822 DEBUG(3, ("smbfs_closedir(%s)\n", smbfs_info
->dirname
));
825 /* for (p = smbfs_info->entries; p;){
836 smbfs_chmod (struct vfs_class
*me
, const char *path
, int mode
)
838 DEBUG(3, ("smbfs_chmod(path:%s, mode:%d)\n", path
, mode
));
839 /* my_errno = EOPNOTSUPP;
840 return -1; */ /* cannot chmod on smb filesystem */
841 return 0; /* make mc happy */
845 smbfs_chown (struct vfs_class
*me
, const char *path
, int owner
, int group
)
847 DEBUG(3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path
, owner
, group
));
848 my_errno
= EOPNOTSUPP
; /* ready for your labotomy? */
853 smbfs_utime (struct vfs_class
*me
, const char *path
, struct utimbuf
*times
)
855 DEBUG(3, ("smbfs_utime(path:%s)\n", path
));
856 my_errno
= EOPNOTSUPP
;
861 smbfs_readlink (struct vfs_class
*me
, const char *path
, char *buf
, size_t size
)
864 ("smbfs_readlink(path:%s, buf:%s, size:%d)\n", path
, buf
,
866 my_errno
= EOPNOTSUPP
;
867 return -1; /* no symlinks on smb filesystem? */
871 smbfs_symlink (struct vfs_class
*me
, const char *n1
, const char *n2
)
873 DEBUG(3, ("smbfs_symlink(n1:%s, n2:%s)\n", n1
, n2
));
874 my_errno
= EOPNOTSUPP
;
875 return -1; /* no symlinks on smb filesystem? */
878 /* Extract the hostname and username from the path */
879 /* path is in the form: [user@]hostname/share/remote-dir */
880 #define smbfs_get_host_and_username(path, host, user, port, pass) \
881 vfs_split_url (*path, host, user, port, pass, SMB_PORT, 0)
883 /*****************************************************
884 return a connection to a SMB server
885 current_bucket needs to be set before calling
886 *******************************************************/
887 static struct cli_state
*
888 smbfs_do_connect (const char *server
, char *share
)
891 struct nmb_name called
, calling
;
893 extern struct in_addr ipzero
;
895 DEBUG(3, ("smbfs_do_connect(%s, %s)\n", server
, share
));
896 if (*share
== '\\') {
898 share
= strchr(server
,'\\');
899 if (!share
) return NULL
;
904 make_nmb_name(&calling
, global_myname
, 0x0);
905 make_nmb_name(&called
, server
, current_bucket
->name_type
);
909 ip
= (current_bucket
->have_ip
) ? current_bucket
->dest_ip
: ipzero
;
911 /* have to open a new connection */
912 if (!(c
= cli_initialise(NULL
))) {
917 pwd_init(&(c
->pwd
)); /* should be moved into cli_initialise()? */
918 pwd_set_cleartext(&(c
->pwd
), current_bucket
->password
);
920 if ((cli_set_port(c
, current_bucket
->port
) == 0) ||
921 !cli_connect(c
, server
, &ip
)) {
922 DEBUG(1, ("Connection to %s failed\n", server
));
926 if (!cli_session_request(c
, &calling
, &called
)) {
927 my_errno
= cli_error(c
, NULL
, &err
, NULL
);
928 DEBUG(1, ("session request to %s failed\n", called
.name
));
930 if (strcmp(called
.name
, "*SMBSERVER")) {
931 make_nmb_name(&called
, "*SMBSERVER", 0x20);
937 DEBUG(3, (" session request ok\n"));
939 if (!cli_negprot(c
)) {
940 DEBUG(1, ("protocol negotiation failed\n"));
944 if (!cli_session_setup(c
, current_bucket
->user
,
945 current_bucket
->password
, strlen(current_bucket
->password
),
946 current_bucket
->password
, strlen(current_bucket
->password
),
947 current_bucket
->domain
)) {
948 DEBUG(1,("session setup failed: %s\n", cli_errstr(c
)));
949 smbfs_auth_remove (server
, share
);
953 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
954 DEBUG(5,("Domain=[%s] OS=[%s] Server=[%s]\n",
955 c
->server_domain
,c
->server_os
,c
->server_type
));
957 DEBUG(3, (" session setup ok\n"));
959 if (!cli_send_tconX(c
, share
, const_cast(char *, "?????"),
960 current_bucket
->password
, strlen(current_bucket
->password
)+1)) {
961 DEBUG(1,("%s: tree connect failed: %s\n", share
, cli_errstr(c
)));
965 DEBUG(3, (" tconx ok\n"));
971 my_errno
= cli_error(c
, NULL
, &err
, NULL
);
978 smbfs_get_master_browser(char **host
)
981 struct in_addr
*ip_list
, bcast_addr
;
982 extern struct in_addr ipzero
;
984 /* does port = 137 for win95 master browser? */
985 int fd
= open_socket_in( SOCK_DGRAM
, 0, 3,
986 interpret_addr(lp_socket_address()), True
);
989 set_socket_options(fd
, const_cast(char *, "SO_BROADCAST"));
990 ip_list
= iface_bcast(ipzero
);
991 bcast_addr
= *ip_list
;
992 if ((ip_list
= name_query(fd
, "\01\02__MSBROWSE__\02", 1, True
,
993 True
, bcast_addr
, &count
, NULL
))) {
996 /* just return first master browser */
997 *host
= g_strdup(inet_ntoa(ip_list
[0]));
1004 smbfs_free_bucket (smbfs_connection
*bucket
)
1006 g_free (bucket
->host
);
1007 g_free (bucket
->service
);
1008 g_free (bucket
->domain
);
1009 g_free (bucket
->user
);
1010 wipe_password (bucket
->password
);
1011 g_free (bucket
->home
);
1012 memset (bucket
, 0, sizeof (smbfs_connection
));
1015 static smbfs_connection
*
1016 smbfs_get_free_bucket (void)
1020 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++)
1021 if (!smbfs_connections
[i
].cli
) return &smbfs_connections
[i
];
1023 { /* search for most dormant connection */
1024 int oldest
= 0; /* index */
1025 time_t oldest_time
= smbfs_connections
[0].last_use
;
1026 for (i
= 1; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1027 if (smbfs_connections
[i
].last_use
< oldest_time
) {
1028 oldest_time
= smbfs_connections
[i
].last_use
;
1032 cli_shutdown(smbfs_connections
[oldest
].cli
);
1033 smbfs_free_bucket (&smbfs_connections
[oldest
]);
1034 return &smbfs_connections
[oldest
];
1037 /* This can't happend, since we have checked for max connections before */
1038 vfs_die("Internal error: smbfs_get_free_bucket");
1039 return 0; /* shut up, stupid gcc */
1042 /* This routine keeps track of open connections */
1043 /* Returns a connected socket to host */
1044 static smbfs_connection
*
1045 smbfs_open_link (char *host
, char *path
, const char *user
, int *port
,
1049 smbfs_connection
*bucket
;
1051 struct in_addr
*dest_ip
= NULL
;
1053 DEBUG (3, ("smbfs_open_link(host:%s, path:%s)\n", host
, path
));
1055 if (strcmp (host
, path
) == 0) /* if host & path are same: */
1056 pstrcpy (service
, IPC
); /* setup for browse */
1057 else { /* get share name from path, path starts with server name */
1059 if ((p
= strchr (path
, '/'))) /* get share aka */
1060 pstrcpy (service
, ++p
); /* service name from path */
1062 pstrcpy (service
, "");
1063 /* now check for trailing directory/filenames */
1064 p
= strchr (service
, '/');
1066 *p
= 0; /* cut off dir/files: sharename only */
1068 pstrcpy (service
, IPC
); /* setup for browse */
1069 DEBUG (6, ("smbfs_open_link: service from path:%s\n", service
));
1073 user
= username
; /* global from getenv */
1075 /* Is the link actually open? */
1076 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1077 if (!smbfs_connections
[i
].cli
)
1079 if ((strcmp (host
, smbfs_connections
[i
].host
) == 0) &&
1080 (strcmp (user
, smbfs_connections
[i
].user
) == 0) &&
1081 (strcmp (service
, smbfs_connections
[i
].service
) == 0)) {
1083 BOOL inshare
= (*host
!= 0 && *path
!= 0 && strchr (path
, '/'));
1084 /* check if this connection has died */
1085 while (!smbfs_chkpath (smbfs_connections
[i
].cli
, "\\", !inshare
)) {
1086 if (!smbfs_reconnect (&smbfs_connections
[i
], &retries
))
1089 DEBUG (6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i
));
1090 current_bucket
= &smbfs_connections
[i
];
1091 smbfs_connections
[i
].last_use
= time (NULL
);
1092 return &smbfs_connections
[i
];
1094 /* connection not found, find if we have ip for new connection */
1095 if (strcmp (host
, smbfs_connections
[i
].host
) == 0)
1096 dest_ip
= &smbfs_connections
[i
].cli
->dest_ip
;
1099 /* make new connection */
1100 bucket
= smbfs_get_free_bucket ();
1101 bucket
->name_type
= 0x20;
1103 bucket
->port
= *port
;
1104 bucket
->have_ip
= False
;
1106 bucket
->have_ip
= True
;
1107 bucket
->dest_ip
= *dest_ip
;
1109 current_bucket
= bucket
;
1111 bucket
->user
= g_strdup (user
);
1112 bucket
->service
= g_strdup (service
);
1114 if (!(*host
)) { /* if blank host name, browse for servers */
1115 if (!smbfs_get_master_browser (&host
)) /* set host to ip of master browser */
1116 return 0; /* could not find master browser? */
1118 bucket
->host
= g_strdup (""); /* blank host means master browser */
1120 bucket
->host
= g_strdup (host
);
1122 if (!smbfs_bucket_set_authinfo (bucket
, 0, /* domain currently not used */
1123 user
, this_pass
, 1))
1126 /* connect to share */
1127 while (!(bucket
->cli
= smbfs_do_connect (host
, service
))) {
1129 if (my_errno
!= EPERM
)
1131 message (1, MSG_ERROR
, _(" Authentication failed "));
1133 /* authentication failed, try again */
1134 smbfs_auth_remove (bucket
->host
, bucket
->service
);
1135 if (!smbfs_bucket_set_authinfo (bucket
, bucket
->domain
, bucket
->user
, 0, 0))
1140 smbfs_open_connections
++;
1141 DEBUG (3, ("smbfs_open_link:smbfs_open_connections: %d\n",
1142 smbfs_open_connections
));
1147 smbfs_get_path (smbfs_connection
** sc
, const char *path
)
1149 char *user
, *host
, *remote_path
, *pass
;
1150 int port
= SMB_PORT
;
1152 DEBUG (3, ("smbfs_get_path(%s)\n", path
));
1153 if (strncmp (path
, URL_HEADER
, HEADER_LEN
))
1157 if (*path
== '/') /* '/' leading server name */
1158 path
++; /* probably came from server browsing */
1161 smbfs_get_host_and_username (&path
, &host
, &user
, &port
, &pass
)))
1163 smbfs_open_link (host
, remote_path
, user
, &port
, pass
)) == NULL
) {
1164 g_free (remote_path
);
1170 wipe_password (pass
);
1175 /* NOTE: tildes are deprecated. See ftpfs.c */
1177 int f
= !strcmp (remote_path
, "/~");
1178 if (f
|| !strncmp (remote_path
, "/~/", 3)) {
1180 s
= concat_dir_and_file ((*sc
)->home
, remote_path
+ 3 - f
);
1181 g_free (remote_path
);
1190 is_error (int result
, int errno_num
)
1192 if (!(result
== -1))
1193 return my_errno
= 0;
1195 my_errno
= errno_num
;
1201 smbfs_opendir (struct vfs_class
*me
, const char *dirname
)
1203 opendir_info
*smbfs_info
;
1204 smbfs_connection
*sc
;
1207 DEBUG(3, ("smbfs_opendir(dirname:%s)\n", dirname
));
1209 if (!(remote_dir
= smbfs_get_path (&sc
, dirname
)))
1212 /* FIXME: where freed? */
1213 smbfs_info
= g_new (opendir_info
, 1);
1214 smbfs_info
->server_list
= FALSE
;
1215 smbfs_info
->path
= g_strdup(dirname
); /* keep original */
1216 smbfs_info
->dirname
= remote_dir
;
1217 smbfs_info
->conn
= sc
;
1218 smbfs_info
->entries
= 0;
1219 smbfs_info
->current
= 0;
1225 smbfs_fake_server_stat (const char *server_url
, const char *path
, struct stat
*buf
)
1230 if ((p
= strrchr (path
, '/')))
1231 path
= p
+ 1; /* advance until last '/' */
1233 if (!current_info
->entries
) {
1234 if (!smbfs_loaddir (current_info
)) /* browse host */
1238 if (current_info
->server_list
== True
) {
1239 dentry
= current_info
->entries
;
1240 DEBUG (4, ("fake stat for SERVER \"%s\"\n", path
));
1242 if (strcmp (dentry
->text
, path
) == 0) {
1243 DEBUG (4, ("smbfs_fake_server_stat: %s:%4o\n",
1244 dentry
->text
, (int)dentry
->my_stat
.st_mode
));
1245 memcpy (buf
, &dentry
->my_stat
, sizeof (struct stat
));
1248 dentry
= dentry
->next
;
1256 smbfs_fake_share_stat (const char *server_url
, const char *path
, struct stat
*buf
)
1259 if (strlen (path
) < strlen (server_url
))
1262 if (!current_share_info
) { /* Server was not stat()ed */
1263 /* Make sure there is such share at server */
1264 smbfs_connection
*sc
;
1266 p
= smbfs_get_path (&sc
, path
);
1269 memset (buf
, 0, sizeof (*buf
));
1270 /* show this as dir */
1272 (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
|
1279 path
+= strlen (server_url
); /* we only want share name */
1282 if (*path
== '/') /* '/' leading server name */
1283 path
++; /* probably came from server browsing */
1285 if (!current_share_info
->entries
) {
1286 if (!smbfs_loaddir (current_share_info
)) /* browse host */
1289 dentry
= current_share_info
->entries
;
1290 DEBUG (3, ("smbfs_fake_share_stat: %s on %s\n", path
, server_url
));
1292 if (strcmp (dentry
->text
, path
) == 0) {
1293 DEBUG (6, ("smbfs_fake_share_stat: %s:%4o\n",
1294 dentry
->text
, (int) dentry
->my_stat
.st_mode
));
1295 memcpy (buf
, &dentry
->my_stat
, sizeof (struct stat
));
1298 dentry
= dentry
->next
;
1304 /* stat a single file, smbfs_get_remote_stat callback */
1305 static dir_entry
*single_entry
;
1307 /* stat a single file */
1309 smbfs_get_remote_stat (smbfs_connection
* sc
, const char *path
, struct stat
*buf
)
1311 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
1314 DEBUG (3, ("smbfs_get_remote_stat(): mypath:%s\n", path
));
1316 mypath
= smbfs_convert_path (path
, FALSE
);
1318 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1319 single_entry
= g_new (dir_entry
, 1);
1321 single_entry
->text
= dos_to_unix (g_strdup (finfo
->name
), 1);
1323 single_entry
->next
= 0;
1326 single_entry
= g_new0 (dir_entry
, 1);
1329 (sc
->cli
, mypath
, attribute
, smbfs_loaddir_helper
, single_entry
) < 1) {
1332 return -1; /* cli_list returns number of files */
1335 memcpy (buf
, &single_entry
->my_stat
, sizeof (struct stat
));
1337 /* don't free here, use for smbfs_fstat() */
1338 /* g_free(single_entry->text);
1339 g_free(single_entry); */
1345 smbfs_search_dir_entry (dir_entry
*dentry
, const char *text
, struct stat
*buf
)
1348 if (strcmp(text
, dentry
->text
) == 0) {
1349 memcpy(buf
, &dentry
->my_stat
, sizeof(struct stat
));
1350 memcpy(&single_entry
->my_stat
, &dentry
->my_stat
,
1351 sizeof(struct stat
));
1354 dentry
= dentry
->next
;
1360 smbfs_get_stat_info (smbfs_connection
* sc
, const char *path
, struct stat
*buf
)
1364 dir_entry
*dentry
= current_info
->entries
;
1366 const char *mypath
= path
;
1368 mypath
++; /* cut off leading '/' */
1369 if ((p
= strrchr (mypath
, '/')))
1370 mypath
= p
+ 1; /* advance until last file/dir name */
1371 DEBUG (3, ("smbfs_get_stat_info: mypath:%s, current_info->dirname:%s\n",
1372 mypath
, current_info
->dirname
));
1375 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1376 current_info
->dirname
, path
));
1380 if (!single_entry
) /* when found, this will be written too */
1381 single_entry
= g_new (dir_entry
, 1);
1382 if (smbfs_search_dir_entry (current_info
->entries
, mypath
, buf
) == 0) {
1385 /* now try to identify mypath as PARENT dir */
1389 mdp
= mydir
= g_strdup (current_info
->dirname
);
1390 if ((p
= strrchr (mydir
, '/')))
1391 *p
= 0; /* advance util last '/' */
1392 if ((p
= strrchr (mydir
, '/')))
1393 mydir
= p
+ 1; /* advance util last '/' */
1394 if (strcmp (mydir
, mypath
) == 0) { /* fake a stat for ".." */
1395 memset (buf
, 0, sizeof (struct stat
));
1396 buf
->st_mode
= (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
) & myumask
;
1397 memcpy (&single_entry
->my_stat
, buf
, sizeof (struct stat
));
1399 DEBUG (1, (" PARENT:found in %s\n", current_info
->dirname
));
1404 /* now try to identify as CURRENT dir? */
1406 char *dnp
= current_info
->dirname
;
1407 DEBUG (6, ("smbfs_get_stat_info: is %s current dir? this dir is: %s\n",
1408 mypath
, current_info
->dirname
));
1414 if (strcmp (mypath
, dnp
) == 0) {
1415 memset (buf
, 0, sizeof (struct stat
));
1416 buf
->st_mode
= (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
) & myumask
;
1417 memcpy (&single_entry
->my_stat
, buf
, sizeof (struct stat
));
1418 DEBUG (1, (" CURRENT:found in %s\n", current_info
->dirname
));
1422 DEBUG (3, ("'%s' not found in current_info '%s'\n", path
,
1423 current_info
->dirname
));
1424 /* try to find this in the PREVIOUS listing */
1425 if (previous_info
) {
1426 if (smbfs_search_dir_entry (previous_info
->entries
, mypath
, buf
) == 0)
1428 DEBUG (3, ("'%s' not found in previous_info '%s'\n", path
,
1429 previous_info
->dirname
));
1431 /* try to find this in the SHARE listing */
1432 if (current_share_info
) {
1433 if (smbfs_search_dir_entry (current_share_info
->entries
, mypath
, buf
) == 0)
1435 DEBUG (3, ("'%s' not found in share_info '%s'\n", path
,
1436 current_share_info
->dirname
));
1438 /* try to find this in the SERVER listing */
1439 if (current_server_info
) {
1440 if (smbfs_search_dir_entry (current_server_info
->entries
, mypath
, buf
) == 0)
1442 DEBUG (3, ("'%s' not found in server_info '%s'\n", path
,
1443 current_server_info
->dirname
));
1445 /* nothing found. get stat file info from server */
1446 return smbfs_get_remote_stat (sc
, path
, buf
);
1450 smbfs_chdir (struct vfs_class
*me
, const char *path
)
1453 smbfs_connection
*sc
;
1455 DEBUG (3, ("smbfs_chdir(path:%s)\n", path
));
1456 if (!(remote_dir
= smbfs_get_path (&sc
, path
)))
1458 g_free (remote_dir
);
1464 smbfs_loaddir_by_name (struct vfs_class
*me
, const char *path
)
1469 mypath
= g_strdup(path
);
1470 p
= strrchr(mypath
, '/');
1474 DEBUG(6, ("smbfs_loaddir_by_name(%s)\n", mypath
));
1475 smbfs_chdir(me
, mypath
);
1476 info
= smbfs_opendir (me
, mypath
);
1480 smbfs_readdir(info
);
1481 smbfs_loaddir(info
);
1486 smbfs_stat (struct vfs_class
* me
, const char *path
, struct stat
*buf
)
1488 smbfs_connection
*sc
;
1490 char *service
, *pp
, *at
;
1493 DEBUG (3, ("smbfs_stat(path:%s)\n", path
));
1495 if (!current_info
) {
1496 DEBUG (1, ("current_info = NULL: "));
1497 if (smbfs_loaddir_by_name (me
, path
) < 0)
1501 /* check if stating server */
1503 if (strncmp (p
, URL_HEADER
, HEADER_LEN
)) {
1504 DEBUG (1, ("'%s' doesnt start with '%s' (length %d)\n",
1505 p
, URL_HEADER
, HEADER_LEN
));
1513 pp
= strchr (p
, '/'); /* advance past next '/' */
1514 at
= strchr (p
, '@');
1515 pstrcpy (server_url
, URL_HEADER
);
1516 if (at
&& at
< pp
) { /* user@server */
1517 char *z
= &(server_url
[sizeof (server_url
) - 1]);
1520 at
= &(server_url
[HEADER_LEN
]) + (at
- p
+ 1);
1523 at
= &(server_url
[HEADER_LEN
]);
1528 pstrcat (server_url
, current_bucket
->host
);
1531 if (!current_info
->server_list
) {
1532 if (smbfs_loaddir_by_name (me
, path
) < 0)
1535 return smbfs_fake_server_stat (server_url
, path
, buf
);
1538 if (!strchr (++pp
, '/')) {
1539 return smbfs_fake_share_stat (server_url
, path
, buf
);
1542 /* stating inside share at this point */
1543 if (!(service
= smbfs_get_path (&sc
, path
))) /* connects if necessary */
1546 int hostlen
= strlen (current_bucket
->host
);
1547 char *ppp
= service
+ strlen (service
) - hostlen
;
1548 char *sp
= server_url
+ strlen (server_url
) - hostlen
;
1550 if (strcmp (sp
, ppp
) == 0) {
1551 /* make server name appear as directory */
1552 DEBUG (1, ("smbfs_stat: showing server as directory\n"));
1553 memset (buf
, 0, sizeof (struct stat
));
1554 buf
->st_mode
= (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
) & myumask
;
1559 /* check if current_info is in share requested */
1561 pp
= strchr (p
, '/');
1563 p
= ++pp
; /* advance past server name */
1564 pp
= strchr (p
, '/');
1567 *pp
= 0; /* cut off everthing after service name */
1569 p
= IPC
; /* browsing for services */
1570 pp
= current_info
->dirname
;
1573 if (strncmp (p
, pp
, strlen (p
)) != 0) {
1574 DEBUG (6, ("desired '%s' is not loaded, we have '%s'\n", p
, pp
));
1575 if (smbfs_loaddir_by_name (me
, path
) < 0) {
1579 DEBUG (6, ("loaded dir: '%s'\n", current_info
->dirname
));
1582 /* stat dirs & files under shares now */
1583 return smbfs_get_stat_info (sc
, path
, buf
);
1586 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
1589 smbfs_lseek (void *data
, off_t offset
, int whence
)
1591 smbfs_handle
*info
= (smbfs_handle
*) data
;
1595 ("smbfs_lseek(info->nread => %d, offset => %d, whence => %d) \n",
1596 (int) info
->nread
, (int) offset
, whence
));
1600 info
->nread
= offset
;
1603 info
->nread
+= offset
;
1606 if (!cli_qfileinfo (info
->cli
, info
->fnum
,
1607 NULL
, &size
, NULL
, NULL
, NULL
,
1609 !cli_getattrE (info
->cli
, info
->fnum
,
1610 NULL
, &size
, NULL
, NULL
, NULL
)) {
1614 info
->nread
= size
+ offset
;
1622 smbfs_mknod (struct vfs_class
*me
, const char *path
, int mode
, int dev
)
1624 DEBUG(3, ("smbfs_mknod(path:%s, mode:%d, dev:%d)\n", path
, mode
, dev
));
1625 my_errno
= EOPNOTSUPP
;
1630 smbfs_mkdir (struct vfs_class
* me
, const char *path
, mode_t mode
)
1632 smbfs_connection
*sc
;
1636 DEBUG (3, ("smbfs_mkdir(path:%s, mode:%d)\n", path
, (int) mode
));
1637 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1639 g_free (remote_file
);
1640 cpath
= smbfs_convert_path (path
, FALSE
);
1642 if (!cli_mkdir (sc
->cli
, cpath
)) {
1643 my_errno
= cli_error (sc
->cli
, NULL
, &err
, NULL
);
1644 message (1, MSG_ERROR
, _(" Error %s creating directory %s "),
1645 cli_errstr (sc
->cli
), CNV_LANG (cpath
));
1654 smbfs_rmdir (struct vfs_class
*me
, const char *path
)
1656 smbfs_connection
*sc
;
1660 DEBUG(3, ("smbfs_rmdir(path:%s)\n", path
));
1661 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1663 g_free (remote_file
);
1664 cpath
= smbfs_convert_path (path
, FALSE
);
1666 if (!cli_rmdir(sc
->cli
, cpath
)) {
1667 my_errno
= cli_error(sc
->cli
, NULL
, &err
, NULL
);
1668 message (1, MSG_ERROR
, _(" Error %s removing directory %s "),
1669 cli_errstr(sc
->cli
), CNV_LANG(cpath
));
1679 smbfs_link (struct vfs_class
*me
, const char *p1
, const char *p2
)
1681 DEBUG (3, ("smbfs_link(p1:%s, p2:%s)\n", p1
, p2
));
1682 my_errno
= EOPNOTSUPP
;
1687 smbfs_free (vfsid id
)
1689 DEBUG (3, ("smbfs_free(%p)\n", id
));
1690 smbfs_auth_free_all ();
1693 /* Gives up on a socket and reopens the connection, the child own the socket
1697 smbfs_forget (const char *path
)
1699 char *host
, *user
, *p
;
1702 if (strncmp (path
, URL_HEADER
, HEADER_LEN
))
1705 DEBUG (3, ("smbfs_forget(path:%s)\n", path
));
1708 if (path
[0] == '/' && path
[1] == '/')
1711 if ((p
= smbfs_get_host_and_username (&path
, &host
, &user
, &port
, NULL
))) {
1713 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++) {
1714 if (smbfs_connections
[i
].cli
1715 && (strcmp (host
, smbfs_connections
[i
].host
) == 0)
1716 && (strcmp (user
, smbfs_connections
[i
].user
) == 0)
1717 && (port
== smbfs_connections
[i
].port
)) {
1719 /* close socket: the child owns it now */
1720 cli_shutdown (smbfs_connections
[i
].cli
);
1722 /* reopen the connection */
1723 smbfs_connections
[i
].cli
=
1724 smbfs_do_connect (host
, smbfs_connections
[i
].service
);
1733 smbfs_setctl (struct vfs_class
*me
, const char *path
, int ctlop
, void *arg
)
1735 DEBUG (3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path
, ctlop
));
1737 case VFS_SETCTL_FORGET
:
1738 smbfs_forget (path
);
1745 static smbfs_handle
*
1746 smbfs_open_readwrite (smbfs_handle
*remote_handle
, char *rname
, int flags
, int mode
)
1750 if (flags
& O_TRUNC
) /* if it exists truncate to zero */
1751 DEBUG (3, ("smbfs_open: O_TRUNC\n"));
1753 remote_handle
->fnum
=
1754 #if 1 /* Don't play with flags, it is cli_open() headache */
1755 cli_open (remote_handle
->cli
, rname
, flags
, DENY_NONE
);
1756 #else /* What's a reasons to has this code ? */
1757 cli_open (remote_handle
->cli
, rname
, ((flags
& O_CREAT
)
1759 (O_WRONLY
| O_APPEND
))) ?
1760 flags
: O_RDONLY
, DENY_NONE
);
1762 if (remote_handle
->fnum
== -1) {
1763 message (1, MSG_ERROR
, _(" %s opening remote file %s "),
1764 cli_errstr (remote_handle
->cli
), CNV_LANG (rname
));
1765 DEBUG (1, ("smbfs_open(rname:%s) error:%s\n",
1766 rname
, cli_errstr (remote_handle
->cli
)));
1767 my_errno
= cli_error (remote_handle
->cli
, NULL
, &err
, NULL
);
1771 if (flags
& O_CREAT
)
1772 return remote_handle
;
1774 if (!cli_qfileinfo (remote_handle
->cli
, remote_handle
->fnum
,
1775 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
, NULL
,
1777 && !cli_getattrE (remote_handle
->cli
, remote_handle
->fnum
,
1778 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
)) {
1779 message (1, MSG_ERROR
, " getattrib: %s ",
1780 cli_errstr (remote_handle
->cli
));
1782 ("smbfs_open(rname:%s) getattrib:%s\n", rname
,
1783 cli_errstr (remote_handle
->cli
)));
1784 my_errno
= cli_error (remote_handle
->cli
, NULL
, &err
, NULL
);
1785 cli_close (remote_handle
->cli
, remote_handle
->fnum
);
1789 if ((flags
== (O_WRONLY
| O_APPEND
)) /* file.c:copy_file_file() -> do_append */
1790 && smbfs_lseek (remote_handle
, 0, SEEK_END
) == -1) {
1791 cli_close (remote_handle
->cli
, remote_handle
->fnum
);
1795 return remote_handle
;
1799 smbfs_open (struct vfs_class
*me
, const char *file
, int flags
, int mode
)
1803 smbfs_connection
*sc
;
1804 smbfs_handle
*remote_handle
;
1806 DEBUG(3, ("smbfs_open(file:%s, flags:%d, mode:%o)\n", file
, flags
, mode
));
1808 if (!(remote_file
= smbfs_get_path (&sc
, file
)))
1811 remote_file
= free_after(smbfs_convert_path (remote_file
, FALSE
), remote_file
);
1813 remote_handle
= g_new (smbfs_handle
, 2);
1814 remote_handle
->cli
= sc
->cli
;
1815 remote_handle
->nread
= 0;
1817 ret
= smbfs_open_readwrite (remote_handle
, remote_file
, flags
, mode
);
1819 g_free (remote_file
);
1821 g_free (remote_handle
);
1827 smbfs_unlink (struct vfs_class
*me
, const char *path
)
1829 smbfs_connection
*sc
;
1832 if ((remote_file
= smbfs_get_path (&sc
, path
)) == 0)
1835 remote_file
= free_after(smbfs_convert_path (remote_file
, FALSE
), remote_file
);
1837 if (!cli_unlink(sc
->cli
, remote_file
)) {
1838 message (1, MSG_ERROR
, _(" %s removing remote file %s "),
1839 cli_errstr(sc
->cli
), CNV_LANG(remote_file
));
1840 g_free (remote_file
);
1843 g_free (remote_file
);
1848 smbfs_rename (struct vfs_class
*me
, const char *a
, const char *b
)
1850 smbfs_connection
*sc
;
1854 if ((ra
= smbfs_get_path (&sc
, a
)) == 0)
1857 if ((rb
= smbfs_get_path (&sc
, b
)) == 0) {
1862 ra
= free_after (smbfs_convert_path (ra
, FALSE
), ra
);
1863 rb
= free_after (smbfs_convert_path (rb
, FALSE
), rb
);
1865 retval
= cli_rename(sc
->cli
, ra
, rb
);
1871 message (1, MSG_ERROR
, _(" %s renaming files\n"),
1872 cli_errstr(sc
->cli
));
1879 smbfs_fstat (void *data
, struct stat
*buf
)
1881 smbfs_handle
*remote_handle
= (smbfs_handle
*)data
;
1883 DEBUG(3, ("smbfs_fstat(fnum:%d)\n", remote_handle
->fnum
));
1885 /* use left over from previous smbfs_get_remote_stat, if available */
1887 memcpy(buf
, &single_entry
->my_stat
, sizeof(struct stat
));
1888 else { /* single_entry not set up: bug */
1898 vfs_smbfs_ops
.name
= "smbfs";
1899 vfs_smbfs_ops
.prefix
= "smb:";
1900 vfs_smbfs_ops
.flags
= VFSF_NOLINKS
;
1901 vfs_smbfs_ops
.init
= smbfs_init
;
1902 vfs_smbfs_ops
.fill_names
= smbfs_fill_names
;
1903 vfs_smbfs_ops
.open
= smbfs_open
;
1904 vfs_smbfs_ops
.close
= smbfs_close
;
1905 vfs_smbfs_ops
.read
= smbfs_read
;
1906 vfs_smbfs_ops
.write
= smbfs_write
;
1907 vfs_smbfs_ops
.opendir
= smbfs_opendir
;
1908 vfs_smbfs_ops
.readdir
= smbfs_readdir
;
1909 vfs_smbfs_ops
.closedir
= smbfs_closedir
;
1910 vfs_smbfs_ops
.stat
= smbfs_stat
;
1911 vfs_smbfs_ops
.lstat
= smbfs_lstat
;
1912 vfs_smbfs_ops
.fstat
= smbfs_fstat
;
1913 vfs_smbfs_ops
.chmod
= smbfs_chmod
;
1914 vfs_smbfs_ops
.chown
= smbfs_chown
;
1915 vfs_smbfs_ops
.utime
= smbfs_utime
;
1916 vfs_smbfs_ops
.readlink
= smbfs_readlink
;
1917 vfs_smbfs_ops
.symlink
= smbfs_symlink
;
1918 vfs_smbfs_ops
.link
= smbfs_link
;
1919 vfs_smbfs_ops
.unlink
= smbfs_unlink
;
1920 vfs_smbfs_ops
.rename
= smbfs_rename
;
1921 vfs_smbfs_ops
.chdir
= smbfs_chdir
;
1922 vfs_smbfs_ops
.ferrno
= smbfs_errno
;
1923 vfs_smbfs_ops
.lseek
= smbfs_lseek
;
1924 vfs_smbfs_ops
.mknod
= smbfs_mknod
;
1925 vfs_smbfs_ops
.free
= smbfs_free
;
1926 vfs_smbfs_ops
.mkdir
= smbfs_mkdir
;
1927 vfs_smbfs_ops
.rmdir
= smbfs_rmdir
;
1928 vfs_smbfs_ops
.setctl
= smbfs_setctl
;
1929 vfs_register_class (&vfs_smbfs_ops
);