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 */
43 #include "lib/global.h"
44 #include "lib/strutil.h"
46 #include "lib/widget.h" /* message() */
48 #undef PACKAGE_BUGREPORT
51 #undef PACKAGE_TARNAME
52 #undef PACKAGE_VERSION
54 #include "helpers/include/config.h"
55 /* don't load crap in "samba/include/includes.h" we don't use and which
56 conflicts with definitions in other includes */
57 #undef HAVE_LIBREADLINE
61 #include "helpers/include/includes.h"
63 #include "lib/vfs/vfs.h"
64 #include "lib/vfs/netutil.h"
65 #include "lib/vfs/utilvfs.h"
69 /*** global variables ****************************************************************************/
71 extern int DEBUGLEVEL
;
72 extern pstring myhostname
;
73 extern struct in_addr ipzero
;
74 extern pstring global_myname
;
75 extern pstring debugf
;
78 /*** file scope macro definitions ****************************************************************/
80 #define SMBFS_MAX_CONNECTIONS 16
84 #define CNV_LANG(s) dos_to_unix(s,False)
85 #define GNAL_VNC(s) unix_to_dos(s,False)
87 #define smbfs_lstat smbfs_stat /* no symlinks on smb filesystem? */
89 /*** file scope type declarations ****************************************************************/
91 typedef struct _smbfs_connection smbfs_connection
;
95 struct cli_state
*cli
;
101 typedef struct dir_entry
104 struct dir_entry
*next
;
111 gboolean server_list
;
113 char *path
; /* the dir originally passed to smbfs_opendir */
114 smbfs_connection
*conn
;
119 /*** file scope variables ************************************************************************/
121 static const char *const IPC
= "IPC$";
122 static const char *const URL_HEADER
= "/#smb:";
127 /* stuff that is same with each connection */
129 static mode_t myumask
= 0755;
130 static int smbfs_open_connections
= 0;
131 static gboolean got_user
= FALSE
;
132 static gboolean got_pass
= FALSE
;
133 static pstring password
;
134 static pstring username
;
135 static struct vfs_class vfs_smbfs_ops
;
137 static struct _smbfs_connection
139 struct cli_state
*cli
;
140 struct in_addr dest_ip
;
142 char *host
; /* server name */
143 char *service
; /* share name */
151 } smbfs_connections
[SMBFS_MAX_CONNECTIONS
];
152 /* unique to each connection */
154 static smbfs_connection
*current_bucket
;
156 static GSList
*auth_list
;
158 static opendir_info
*previous_info
, *current_info
, *current_share_info
, *current_server_info
;
160 static gboolean first_direntry
;
162 /* stat a single file, smbfs_get_remote_stat callback */
163 static dir_entry
*single_entry
;
165 /*** file scope functions ************************************************************************/
166 /* --------------------------------------------------------------------------------------------- */
168 /* modifies *share */
169 static struct cli_state
*smbfs_do_connect (const char *server
, char *share
);
171 /* --------------------------------------------------------------------------------------------- */
172 /* this function allows you to write:
173 * char *s = g_strdup("hello, world");
174 * s = free_after(g_strconcat(s, s, (char *)0), s);
178 free_after (char *result
, char *string_to_free
)
180 g_free (string_to_free
);
184 /* --------------------------------------------------------------------------------------------- */
187 smbfs_auth_free (struct smb_authinfo
const *a
)
193 wipe_password (a
->password
);
196 /* --------------------------------------------------------------------------------------------- */
199 smbfs_auth_free_all (void)
203 g_slist_foreach (auth_list
, (GFunc
) smbfs_auth_free
, 0);
204 g_slist_free (auth_list
);
209 /* --------------------------------------------------------------------------------------------- */
212 smbfs_auth_cmp_host_and_share (gconstpointer _a
, gconstpointer _b
)
214 struct smb_authinfo
const *a
= (struct smb_authinfo
const *) _a
;
215 struct smb_authinfo
const *b
= (struct smb_authinfo
const *) _b
;
217 if (!a
->host
|| !a
->share
|| !b
->host
|| !b
->share
)
219 if (strcmp (a
->host
, b
->host
) != 0)
221 if (strcmp (a
->share
, b
->share
) != 0)
226 /* --------------------------------------------------------------------------------------------- */
229 smbfs_auth_cmp_host (gconstpointer _a
, gconstpointer _b
)
231 struct smb_authinfo
const *a
= (struct smb_authinfo
const *) _a
;
232 struct smb_authinfo
const *b
= (struct smb_authinfo
const *) _b
;
234 if (!a
->host
|| !b
->host
)
236 if (strcmp (a
->host
, b
->host
) != 0)
238 if (strcmp (a
->share
, IPC
) != 0)
243 /* --------------------------------------------------------------------------------------------- */
246 smbfs_auth_add (const char *host
, const char *share
, const char *domain
,
247 const char *user
, const char *pass
)
249 struct smb_authinfo
*auth
;
251 auth
= vfs_smb_authinfo_new (host
, share
, domain
, user
, pass
);
254 auth_list
= g_slist_prepend (auth_list
, auth
);
257 /* --------------------------------------------------------------------------------------------- */
260 smbfs_auth_remove (const char *host
, const char *share
)
262 struct smb_authinfo data
;
263 struct smb_authinfo
*auth
;
266 data
.host
= g_strdup (host
);
267 data
.share
= g_strdup (share
);
268 list
= g_slist_find_custom (auth_list
, &data
, smbfs_auth_cmp_host_and_share
);
274 auth_list
= g_slist_remove (auth_list
, auth
);
275 smbfs_auth_free (auth
);
278 /* --------------------------------------------------------------------------------------------- */
279 /* Set authentication information in bucket. Return 1 if successful, else 0 */
280 /* Information in auth_list overrides user if pass is NULL. */
281 /* bucket->host and bucket->service must be valid. */
284 smbfs_bucket_set_authinfo (smbfs_connection
* bucket
,
285 const char *domain
, const char *user
, const char *pass
,
286 int fallback_to_host
)
288 struct smb_authinfo data
;
289 struct smb_authinfo
*auth
;
292 if (domain
&& user
&& pass
)
294 g_free (bucket
->domain
);
295 g_free (bucket
->user
);
296 g_free (bucket
->password
);
297 bucket
->domain
= g_strdup (domain
);
298 bucket
->user
= g_strdup (user
);
299 bucket
->password
= g_strdup (pass
);
300 smbfs_auth_remove (bucket
->host
, bucket
->service
);
301 smbfs_auth_add (bucket
->host
, bucket
->service
, domain
, user
, pass
);
305 data
.host
= bucket
->host
;
306 data
.share
= bucket
->service
;
307 list
= g_slist_find_custom (auth_list
, &data
, smbfs_auth_cmp_host_and_share
);
308 if (!list
&& fallback_to_host
)
309 list
= g_slist_find_custom (auth_list
, &data
, smbfs_auth_cmp_host
);
313 bucket
->domain
= g_strdup (auth
->domain
);
314 bucket
->user
= g_strdup (auth
->user
);
315 bucket
->password
= g_strdup (auth
->password
);
321 bucket
->domain
= g_strdup (lp_workgroup ());
322 bucket
->user
= g_strdup (got_user
? username
: user
);
323 bucket
->password
= g_strdup (password
);
327 auth
= vfs_smb_get_authinfo (bucket
->host
,
328 bucket
->service
, (domain
? domain
: lp_workgroup ()), user
);
331 g_free (bucket
->domain
);
332 g_free (bucket
->user
);
333 g_free (bucket
->password
);
334 bucket
->domain
= g_strdup (auth
->domain
);
335 bucket
->user
= g_strdup (auth
->user
);
336 bucket
->password
= g_strdup (auth
->password
);
337 smbfs_auth_remove (bucket
->host
, bucket
->service
);
338 auth_list
= g_slist_prepend (auth_list
, auth
);
344 /* --------------------------------------------------------------------------------------------- */
347 smbfs_set_debug (int arg
)
352 /* --------------------------------------------------------------------------------------------- */
355 smbfs_set_debugf (const char *filename
)
359 FILE *outfile
= fopen (filename
, "w");
362 setup_logging ("", True
); /* No needs for timestamp for each message */
365 pstrcpy (debugf
, filename
);
370 /* --------------------------------------------------------------------------------------------- */
371 /********************** The callbacks ******************************/
374 smbfs_init (struct vfs_class
*me
)
376 const char *servicesf
= CONFIGDIR PATH_SEP_STR
"smb.conf";
378 /* DEBUGLEVEL = 4; */
381 charset_initialise ();
383 DEBUG (3, ("smbfs_init(%s)\n", me
->name
));
385 if (!get_myname (myhostname
, NULL
))
386 DEBUG (0, ("Failed to get my hostname.\n"));
388 if (!lp_load (servicesf
, True
, False
, False
))
389 DEBUG (0, ("Cannot load %s - run testparm to debug it\n", servicesf
));
391 codepage_initialise (lp_client_code_page ());
403 pstrcpy (username
, getenv ("USER"));
405 DEBUG (3, ("smbfs_init(): $USER:%s\n", username
));
406 if ((p
= strchr (username
, '%')))
409 pstrcpy (password
, p
+ 1);
411 memset (strchr (getenv ("USER"), '%') + 1, 'X', strlen (password
));
412 DEBUG (3, ("smbfs_init(): $USER%%pass: %s%%%s\n", username
, password
));
416 if (getenv ("PASSWD"))
418 pstrcpy (password
, getenv ("PASSWD"));
424 /* --------------------------------------------------------------------------------------------- */
427 smbfs_fill_names (struct vfs_class
*me
, fill_names_f func
)
434 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++)
436 if (smbfs_connections
[i
].cli
)
438 path
= g_strconcat (URL_HEADER
,
439 smbfs_connections
[i
].user
, "@",
440 smbfs_connections
[i
].host
,
441 "/", smbfs_connections
[i
].service
, (char *) NULL
);
448 /* --------------------------------------------------------------------------------------------- */
449 /* does same as do_get() in client.c */
450 /* called from vfs.c:1080, count = buffer size */
453 smbfs_read (void *data
, char *buffer
, size_t count
)
455 smbfs_handle
*info
= (smbfs_handle
*) data
;
458 DEBUG (3, ("smbfs_read(fnum:%d, nread:%d, count:%zu)\n", info
->fnum
, (int) info
->nread
, count
));
459 n
= cli_read (info
->cli
, info
->fnum
, buffer
, info
->nread
, count
);
465 /* --------------------------------------------------------------------------------------------- */
468 smbfs_write (void *data
, const char *buf
, size_t nbyte
)
470 smbfs_handle
*info
= (smbfs_handle
*) data
;
473 DEBUG (3, ("smbfs_write(fnum:%d, nread:%d, nbyte:%zu)\n",
474 info
->fnum
, (int) info
->nread
, nbyte
));
475 n
= cli_write (info
->cli
, info
->fnum
, 0, buf
, info
->nread
, nbyte
);
481 /* --------------------------------------------------------------------------------------------- */
484 smbfs_close (void *data
)
486 smbfs_handle
*info
= (smbfs_handle
*) data
;
487 DEBUG (3, ("smbfs_close(fnum:%d)\n", info
->fnum
));
489 /* FIXME: Why too different cli have the same outbuf
490 * if file is copied to share
492 if (info
->cli
->outbuf
== NULL
)
498 /* if imlementing archive_level: add rname to smbfs_handle */
499 if (archive_level
>= 2 && (inf
->attr
& aARCH
))
501 cli_setatr (info
->cli
, rname
, info
->attr
& ~(uint16
) aARCH
, 0);
504 return (cli_close (info
->cli
, info
->fnum
) == True
) ? 0 : -1;
507 /* --------------------------------------------------------------------------------------------- */
510 smbfs_errno (struct vfs_class
*me
)
514 DEBUG (3, ("smbfs_errno: %s\n", unix_error_string (my_errno
)));
518 /* --------------------------------------------------------------------------------------------- */
521 smbfs_new_dir_entry (const char *name
)
523 static int inode_counter
;
524 dir_entry
*new_entry
;
525 new_entry
= g_new0 (dir_entry
, 1);
526 new_entry
->text
= dos_to_unix (g_strdup (name
), 1);
530 current_info
->entries
= new_entry
;
531 first_direntry
= FALSE
;
535 current_info
->current
->next
= new_entry
;
537 current_info
->current
= new_entry
;
538 new_entry
->my_stat
.st_ino
= inode_counter
++;
543 /* --------------------------------------------------------------------------------------------- */
544 /* browse for shares on server */
547 smbfs_browsing_helper (const char *name
, uint32 type
, const char *comment
, void *state
)
549 const char *typestr
= "";
550 dir_entry
*new_entry
= smbfs_new_dir_entry (name
);
558 /* show this as dir */
559 new_entry
->my_stat
.st_mode
=
560 (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
| S_IXOTH
) & myumask
;
572 DEBUG (3, ("\t%-15.15s%-10.10s%s\n", name
, typestr
, comment
));
575 /* --------------------------------------------------------------------------------------------- */
578 smbfs_loaddir_helper (file_info
* finfo
, const char *mask
, void *entry
)
580 dir_entry
*new_entry
= (dir_entry
*) entry
;
581 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
585 #if 0 /* I want to see dot files */
586 if (finfo
->mode
& aHIDDEN
)
587 return; /* don't bother with hidden files, "~$" screws up mc */
590 new_entry
= smbfs_new_dir_entry (finfo
->name
);
592 new_entry
->my_stat
.st_size
= finfo
->size
;
593 new_entry
->my_stat
.st_mtime
= finfo
->mtime
;
594 new_entry
->my_stat
.st_atime
= finfo
->atime
;
595 new_entry
->my_stat
.st_ctime
= finfo
->ctime
;
596 new_entry
->my_stat
.st_uid
= finfo
->uid
;
597 new_entry
->my_stat
.st_gid
= finfo
->gid
;
599 new_entry
->my_stat
.st_mode
= /* rw-rw-rw */
600 S_IRUSR
| S_IRGRP
| S_IROTH
| S_IWUSR
| S_IWGRP
| S_IWOTH
;
602 /* if (finfo->mode & aVOLID); nothing similar in real world */
603 if (finfo
->mode
& aDIR
)
604 new_entry
->my_stat
.st_mode
|= /* drwxrwxrwx */
605 S_IFDIR
| S_IXUSR
| S_IXGRP
| S_IXOTH
;
607 new_entry
->my_stat
.st_mode
|= S_IFREG
; /* if not dir, regular file? */
608 /* if (finfo->mode & aARCH); DOS archive */
609 /* if (finfo->mode & aHIDDEN); like a dot file? */
610 /* if (finfo->mode & aSYSTEM); like a kernel? */
611 if (finfo
->mode
& aRONLY
)
612 new_entry
->my_stat
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
613 new_entry
->my_stat
.st_mode
&= myumask
;
615 DEBUG (entry
? 3 : 6, (" %-30s%7.7s%8.0f %s",
616 CNV_LANG (finfo
->name
),
617 attrib_string (finfo
->mode
),
618 (double) finfo
->size
, asctime (LocalTime (&t
))));
621 /* --------------------------------------------------------------------------------------------- */
622 /* takes "/foo/bar/file" and gives malloced "\\foo\\bar\\file" */
625 smbfs_convert_path (const char *remote_file
, gboolean trailing_asterik
)
627 const char *p
, *my_remote
;
630 my_remote
= remote_file
;
631 if (strncmp (my_remote
, URL_HEADER
, HEADER_LEN
) == 0)
632 { /* if passed directly */
633 my_remote
+= HEADER_LEN
;
634 if (*my_remote
== '/') /* from server browsing */
636 p
= strchr (my_remote
, '/');
638 my_remote
= p
+ 1; /* advance to end of server name */
641 if (*my_remote
== '/')
642 my_remote
++; /* strip off leading '/' */
643 p
= strchr (my_remote
, '/');
645 my_remote
= p
; /* strip off share/service name */
646 /* create remote filename as understood by smb clientgen */
647 result
= g_strconcat (my_remote
, trailing_asterik
? "/*" : "", (char *) NULL
);
648 unix_to_dos (result
, /* inplace = */ 1); /* code page conversion */
649 str_replace (result
, '/', '\\');
653 /* --------------------------------------------------------------------------------------------- */
656 smbfs_srv_browsing_helper (const char *name
, uint32 m
, const char *comment
, void *state
)
658 dir_entry
*new_entry
= smbfs_new_dir_entry (name
);
663 /* show this as dir */
664 new_entry
->my_stat
.st_mode
=
665 (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
| S_IXOTH
) & myumask
;
667 DEBUG (3, ("\t%-16.16s %s\n", name
, comment
));
670 /* --------------------------------------------------------------------------------------------- */
673 smbfs_reconnect (smbfs_connection
* conn
, int *retries
)
676 DEBUG (3, ("RECONNECT\n"));
678 if (*(conn
->host
) == 0)
679 host
= g_strdup (conn
->cli
->desthost
); /* server browsing */
681 host
= g_strdup (conn
->host
);
683 cli_shutdown (conn
->cli
);
685 if (!(conn
->cli
= smbfs_do_connect (host
, conn
->service
)))
687 message (D_ERROR
, MSG_ERROR
, _("reconnect to %s failed"), conn
->host
);
692 if (++(*retries
) == 2)
697 /* --------------------------------------------------------------------------------------------- */
700 smbfs_send (struct cli_state
*cli
)
706 len
= smb_len (cli
->outbuf
) + 4;
708 while (nwritten
< len
)
710 ret
= write_socket (cli
->fd
, cli
->outbuf
+ nwritten
, len
- nwritten
);
723 /* --------------------------------------------------------------------------------------------- */
724 /****************************************************************************
725 See if server has cut us off by checking for EPIPE when writing.
726 Taken from cli_chkpath()
727 ****************************************************************************/
730 smbfs_chkpath (struct cli_state
*cli
, const char *path
, BOOL send_only
)
735 fstrcpy (path2
, path
);
736 unix_to_dos (path2
, 1);
737 trim_string (path2
, NULL
, "\\");
741 memset (cli
->outbuf
, '\0', smb_size
);
742 set_message (cli
->outbuf
, 0, 4 + strlen (path2
), True
);
743 SCVAL (cli
->outbuf
, smb_com
, SMBchkpth
);
744 SSVAL (cli
->outbuf
, smb_tid
, cli
->cnum
);
748 SSVAL (cli
->outbuf
, smb_pid
, cli
->pid
);
749 SSVAL (cli
->outbuf
, smb_uid
, cli
->vuid
);
750 SSVAL (cli
->outbuf
, smb_mid
, cli
->mid
);
751 if (cli
->protocol
> PROTOCOL_CORE
)
753 SCVAL (cli
->outbuf
, smb_flg
, 0x8);
754 SSVAL (cli
->outbuf
, smb_flg2
, 0x1);
757 p
= smb_buf (cli
->outbuf
);
761 if (!smbfs_send (cli
))
763 DEBUG (3, ("smbfs_chkpath: couldnt send\n"));
768 client_receive_smb (cli
->fd
, cli
->inbuf
, cli
->timeout
);
769 DEBUG (3, ("smbfs_chkpath: send only OK\n"));
770 return True
; /* just testing for EPIPE */
772 if (!client_receive_smb (cli
->fd
, cli
->inbuf
, cli
->timeout
))
774 DEBUG (3, ("smbfs_chkpath: receive error\n"));
777 if ((my_errno
= cli_error (cli
, NULL
, NULL
, NULL
)))
779 if (my_errno
== 20 || my_errno
== 13)
780 return True
; /* ignore if 'not a directory' error */
781 DEBUG (3, ("smbfs_chkpath: cli_error: %s\n", unix_error_string (my_errno
)));
788 /* --------------------------------------------------------------------------------------------- */
792 smbfs_fs (const char *text
)
794 const char *p
= text
;
797 while ((p
= strchr (p
, '/')) != NULL
)
803 return strlen (text
);
808 /* --------------------------------------------------------------------------------------------- */
811 smbfs_loaddir (opendir_info
* smbfs_info
)
813 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
814 int servlen
= strlen (smbfs_info
->conn
->service
);
815 const char *info_dirname
= smbfs_info
->dirname
;
818 DEBUG (3, ("smbfs_loaddir: dirname:%s\n", info_dirname
));
819 first_direntry
= TRUE
;
823 DEBUG (3, ("smbfs_loaddir: new:'%s', cached:'%s'\n", info_dirname
, current_info
->dirname
));
824 /* if new desired dir is longer than cached in current_info */
825 if (smbfs_fs (info_dirname
) > smbfs_fs (current_info
->dirname
))
827 DEBUG (3, ("saving to previous_info\n"));
828 previous_info
= current_info
;
832 current_info
= smbfs_info
;
834 if (strcmp (info_dirname
, "/") == 0)
836 if (!strcmp (smbfs_info
->path
, URL_HEADER
))
838 DEBUG (6, ("smbfs_loaddir: browsing %s\n", IPC
));
839 /* browse for servers */
840 if (!cli_NetServerEnum
841 (smbfs_info
->conn
->cli
, smbfs_info
->conn
->domain
,
842 SV_TYPE_ALL
, smbfs_srv_browsing_helper
, NULL
))
845 current_server_info
= smbfs_info
;
846 smbfs_info
->server_list
= TRUE
;
850 /* browse for shares */
851 if (cli_RNetShareEnum (smbfs_info
->conn
->cli
, smbfs_browsing_helper
, NULL
) < 1)
854 current_share_info
= smbfs_info
;
859 /* do regular directory listing */
860 if (strncmp (smbfs_info
->conn
->service
, info_dirname
+ 1, servlen
) == 0)
862 /* strip share name from dir */
863 my_dirname
= g_strdup (info_dirname
+ servlen
);
865 my_dirname
= free_after (smbfs_convert_path (my_dirname
, TRUE
), my_dirname
);
868 my_dirname
= smbfs_convert_path (info_dirname
, TRUE
);
870 DEBUG (6, ("smbfs_loaddir: service: %s\n", smbfs_info
->conn
->service
));
871 DEBUG (6, ("smbfs_loaddir: cli->share: %s\n", smbfs_info
->conn
->cli
->share
));
872 DEBUG (6, ("smbfs_loaddir: calling cli_list with mask %s\n", my_dirname
));
873 /* do file listing: cli_list returns number of files */
874 if (cli_list (smbfs_info
->conn
->cli
, my_dirname
, attribute
, smbfs_loaddir_helper
, NULL
) < 0)
876 /* cli_list returns -1 if directory empty or cannot read socket */
877 my_errno
= cli_error (smbfs_info
->conn
->cli
, NULL
, &err
, NULL
);
881 if (*(my_dirname
) == 0)
882 smbfs_info
->dirname
= smbfs_info
->conn
->service
;
887 /* current_info->parent = smbfs_info->dirname; */
889 smbfs_info
->current
= smbfs_info
->entries
;
890 return 1; /* 1 = ok */
893 /* --------------------------------------------------------------------------------------------- */
895 #ifdef SMBFS_FREE_DIR
897 smbfs_free_dir (dir_entry
* de
)
902 smbfs_free_dir (de
->next
);
908 /* --------------------------------------------------------------------------------------------- */
909 /* The readdir routine loads the complete directory */
910 /* It's too slow to ask the server each time */
911 /* It now also sends the complete lstat information for each file */
914 smbfs_readdir (void *info
)
916 static union vfs_dirent smbfs_readdir_data
;
917 static char *const dirent_dest
= smbfs_readdir_data
.dent
.d_name
;
918 opendir_info
*smbfs_info
= (opendir_info
*) info
;
920 DEBUG (4, ("smbfs_readdir(%s)\n", smbfs_info
->dirname
));
922 if (!smbfs_info
->entries
)
923 if (!smbfs_loaddir (smbfs_info
))
926 if (smbfs_info
->current
== 0)
927 { /* reached end of dir entries */
928 DEBUG (3, ("smbfs_readdir: smbfs_info->current = 0\n"));
929 #ifdef SMBFS_FREE_DIR
930 smbfs_free_dir (smbfs_info
->entries
);
931 smbfs_info
->entries
= 0;
935 g_strlcpy (dirent_dest
, smbfs_info
->current
->text
, MC_MAXPATHLEN
);
936 smbfs_info
->current
= smbfs_info
->current
->next
;
938 compute_namelen (&smbfs_readdir_data
.dent
);
940 return &smbfs_readdir_data
;
943 /* --------------------------------------------------------------------------------------------- */
946 smbfs_closedir (void *info
)
948 opendir_info
*smbfs_info
= (opendir_info
*) info
;
949 /* dir_entry *p, *q; */
951 DEBUG (3, ("smbfs_closedir(%s)\n", smbfs_info
->dirname
));
954 /* for (p = smbfs_info->entries; p;){
964 /* --------------------------------------------------------------------------------------------- */
967 smbfs_chmod (const vfs_path_t
* vpath
, int mode
)
969 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath
, -1);
971 DEBUG (3, ("smbfs_chmod(path:%s, mode:%d)\n", path_element
->path
, mode
));
972 /* my_errno = EOPNOTSUPP;
973 return -1; *//* cannot chmod on smb filesystem */
974 return 0; /* make mc happy */
977 /* --------------------------------------------------------------------------------------------- */
980 smbfs_chown (const vfs_path_t
* vpath
, uid_t owner
, gid_t group
)
982 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath
, -1);
984 DEBUG (3, ("smbfs_chown(path:%s, owner:%d, group:%d)\n", path_element
->path
, owner
, group
));
985 my_errno
= EOPNOTSUPP
; /* ready for your labotomy? */
989 /* --------------------------------------------------------------------------------------------- */
992 smbfs_utime (const vfs_path_t
* vpath
, struct utimbuf
*times
)
994 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath
, -1);
997 DEBUG (3, ("smbfs_utime(path:%s)\n", path_element
->path
));
998 my_errno
= EOPNOTSUPP
;
1002 /* --------------------------------------------------------------------------------------------- */
1005 smbfs_readlink (const vfs_path_t
* vpath
, char *buf
, size_t size
)
1007 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath
, -1);
1009 DEBUG (3, ("smbfs_readlink(path:%s, buf:%s, size:%zu)\n", path_element
->path
, buf
, size
));
1010 my_errno
= EOPNOTSUPP
;
1011 return -1; /* no symlinks on smb filesystem? */
1014 /* --------------------------------------------------------------------------------------------- */
1017 smbfs_symlink (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
1019 vfs_path_element_t
*path_element1
= vfs_path_get_by_index (vpath1
, -1);
1020 vfs_path_element_t
*path_element2
= vfs_path_get_by_index (vpath2
, -1);
1022 DEBUG (3, ("smbfs_symlink(n1:%s, n2:%s)\n", path_element1
->path
, path_element2
->path
));
1023 my_errno
= EOPNOTSUPP
;
1024 return -1; /* no symlinks on smb filesystem? */
1027 /* --------------------------------------------------------------------------------------------- */
1028 /*****************************************************
1029 return a connection to a SMB server
1030 current_bucket needs to be set before calling
1031 *******************************************************/
1033 static struct cli_state
*
1034 smbfs_do_connect (const char *server
, char *share
)
1036 struct cli_state
*c
;
1037 struct nmb_name called
, calling
;
1040 DEBUG (3, ("smbfs_do_connect(%s, %s)\n", server
, share
));
1044 share
= strchr (server
, '\\');
1051 make_nmb_name (&calling
, global_myname
, 0x0);
1052 make_nmb_name (&called
, server
, current_bucket
->name_type
);
1057 ip
= (current_bucket
->have_ip
) ? current_bucket
->dest_ip
: ipzero
;
1059 /* have to open a new connection */
1060 if (!(c
= cli_initialise (NULL
)))
1066 pwd_init (&(c
->pwd
)); /* should be moved into cli_initialise()? */
1067 pwd_set_cleartext (&(c
->pwd
), current_bucket
->password
);
1069 if ((cli_set_port (c
, current_bucket
->port
) == 0) || !cli_connect (c
, server
, &ip
))
1071 DEBUG (1, ("Connection to %s failed\n", server
));
1075 if (!cli_session_request (c
, &calling
, &called
))
1077 my_errno
= cli_error (c
, NULL
, &err
, NULL
);
1078 DEBUG (1, ("session request to %s failed\n", called
.name
));
1080 if (strcmp (called
.name
, "*SMBSERVER"))
1082 make_nmb_name (&called
, "*SMBSERVER", 0x20);
1088 DEBUG (3, (" session request ok\n"));
1090 if (!cli_negprot (c
))
1092 DEBUG (1, ("protocol negotiation failed\n"));
1096 if (!cli_session_setup (c
, current_bucket
->user
,
1097 current_bucket
->password
, strlen (current_bucket
->password
),
1098 current_bucket
->password
, strlen (current_bucket
->password
),
1099 current_bucket
->domain
))
1101 DEBUG (1, ("session setup failed: %s\n", cli_errstr (c
)));
1102 smbfs_auth_remove (server
, share
);
1106 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
1107 DEBUG (5, ("Domain=[%s] OS=[%s] Server=[%s]\n",
1108 c
->server_domain
, c
->server_os
, c
->server_type
));
1110 DEBUG (3, (" session setup ok\n"));
1112 if (!cli_send_tconX (c
, share
, "?????",
1113 current_bucket
->password
, strlen (current_bucket
->password
) + 1))
1115 DEBUG (1, ("%s: tree connect failed: %s\n", share
, cli_errstr (c
)));
1119 DEBUG (3, (" tconx ok\n"));
1125 my_errno
= cli_error (c
, NULL
, &err
, NULL
);
1131 /* --------------------------------------------------------------------------------------------- */
1134 smbfs_get_master_browser (char **host
)
1136 static char so_broadcast
[] = "SO_BROADCAST";
1138 struct in_addr
*ip_list
, bcast_addr
;
1140 /* does port = 137 for win95 master browser? */
1141 int fd
= open_socket_in (SOCK_DGRAM
, 0, 3,
1142 interpret_addr (lp_socket_address ()), True
);
1145 set_socket_options (fd
, so_broadcast
);
1146 ip_list
= iface_bcast (ipzero
);
1147 bcast_addr
= *ip_list
;
1148 if ((ip_list
= name_query (fd
, "\01\02__MSBROWSE__\02", 1, True
,
1149 True
, bcast_addr
, &count
, NULL
)))
1153 /* just return first master browser */
1154 *host
= g_strdup (inet_ntoa (ip_list
[0]));
1160 /* --------------------------------------------------------------------------------------------- */
1163 smbfs_free_bucket (smbfs_connection
* bucket
)
1165 g_free (bucket
->host
);
1166 g_free (bucket
->service
);
1167 g_free (bucket
->domain
);
1168 g_free (bucket
->user
);
1169 wipe_password (bucket
->password
);
1170 g_free (bucket
->home
);
1171 memset (bucket
, 0, sizeof (smbfs_connection
));
1174 /* --------------------------------------------------------------------------------------------- */
1176 static smbfs_connection
*
1177 smbfs_get_free_bucket (void)
1181 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++)
1182 if (!smbfs_connections
[i
].cli
)
1183 return &smbfs_connections
[i
];
1185 { /* search for most dormant connection */
1186 int oldest
= 0; /* index */
1187 time_t oldest_time
= smbfs_connections
[0].last_use
;
1188 for (i
= 1; i
< SMBFS_MAX_CONNECTIONS
; i
++)
1190 if (smbfs_connections
[i
].last_use
< oldest_time
)
1192 oldest_time
= smbfs_connections
[i
].last_use
;
1196 cli_shutdown (smbfs_connections
[oldest
].cli
);
1197 smbfs_free_bucket (&smbfs_connections
[oldest
]);
1198 return &smbfs_connections
[oldest
];
1201 /* This can't happend, since we have checked for max connections before */
1202 vfs_die ("Internal error: smbfs_get_free_bucket");
1203 return 0; /* shut up, stupid gcc */
1206 /* --------------------------------------------------------------------------------------------- */
1207 /* This routine keeps track of open connections */
1208 /* Returns a connected socket to host */
1210 static smbfs_connection
*
1211 smbfs_open_link (char *host
, char *path
, const char *user
, int *port
, char *this_pass
)
1214 smbfs_connection
*bucket
;
1216 struct in_addr
*dest_ip
= NULL
;
1218 DEBUG (3, ("smbfs_open_link(host:%s, path:%s)\n", host
, path
));
1220 if (strcmp (host
, path
) == 0) /* if host & path are same: */
1221 pstrcpy (service
, IPC
); /* setup for browse */
1223 { /* get share name from path, path starts with server name */
1225 if ((p
= strchr (path
, '/'))) /* get share aka */
1226 pstrcpy (service
, ++p
); /* service name from path */
1228 pstrcpy (service
, "");
1229 /* now check for trailing directory/filenames */
1230 p
= strchr (service
, '/');
1232 *p
= 0; /* cut off dir/files: sharename only */
1234 pstrcpy (service
, IPC
); /* setup for browse */
1235 DEBUG (6, ("smbfs_open_link: service from path:%s\n", service
));
1239 user
= username
; /* global from getenv */
1241 /* Is the link actually open? */
1242 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++)
1244 if (!smbfs_connections
[i
].cli
)
1246 if ((strcmp (host
, smbfs_connections
[i
].host
) == 0) &&
1247 (strcmp (user
, smbfs_connections
[i
].user
) == 0) &&
1248 (strcmp (service
, smbfs_connections
[i
].service
) == 0))
1251 BOOL inshare
= (*host
!= 0 && *path
!= 0 && strchr (path
, '/'));
1252 /* check if this connection has died */
1253 while (!smbfs_chkpath (smbfs_connections
[i
].cli
, "\\", !inshare
))
1255 if (!smbfs_reconnect (&smbfs_connections
[i
], &retries
))
1258 DEBUG (6, ("smbfs_open_link: returning smbfs_connection[%d]\n", i
));
1259 current_bucket
= &smbfs_connections
[i
];
1260 smbfs_connections
[i
].last_use
= time (NULL
);
1261 return &smbfs_connections
[i
];
1263 /* connection not found, find if we have ip for new connection */
1264 if (strcmp (host
, smbfs_connections
[i
].host
) == 0)
1265 dest_ip
= &smbfs_connections
[i
].cli
->dest_ip
;
1268 /* make new connection */
1269 bucket
= smbfs_get_free_bucket ();
1270 bucket
->name_type
= 0x20;
1272 bucket
->port
= *port
;
1273 bucket
->have_ip
= False
;
1276 bucket
->have_ip
= True
;
1277 bucket
->dest_ip
= *dest_ip
;
1279 current_bucket
= bucket
;
1281 bucket
->user
= g_strdup (user
);
1282 bucket
->service
= g_strdup (service
);
1285 { /* if blank host name, browse for servers */
1286 if (!smbfs_get_master_browser (&host
)) /* set host to ip of master browser */
1287 return 0; /* could not find master browser? */
1289 bucket
->host
= g_strdup (""); /* blank host means master browser */
1292 bucket
->host
= g_strdup (host
);
1294 if (!smbfs_bucket_set_authinfo (bucket
, 0, /* domain currently not used */
1295 user
, this_pass
, 1))
1298 /* connect to share */
1299 while (!(bucket
->cli
= smbfs_do_connect (host
, service
)))
1302 if (my_errno
!= EPERM
)
1304 message (D_ERROR
, MSG_ERROR
, _("Authentication failed"));
1306 /* authentication failed, try again */
1307 smbfs_auth_remove (bucket
->host
, bucket
->service
);
1308 if (!smbfs_bucket_set_authinfo (bucket
, bucket
->domain
, bucket
->user
, 0, 0))
1313 smbfs_open_connections
++;
1314 DEBUG (3, ("smbfs_open_link:smbfs_open_connections: %d\n", smbfs_open_connections
));
1318 /* --------------------------------------------------------------------------------------------- */
1321 smbfs_get_path (smbfs_connection
** sc
, const vfs_path_t
* vpath
)
1323 char *remote_path
= NULL
;
1324 vfs_path_element_t
*url
;
1325 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath
, -1);
1326 char *path
= path_element
->path
;
1328 DEBUG (3, ("smbfs_get_path(%s)\n", path
));
1329 if (strncmp (path
, URL_HEADER
, HEADER_LEN
) != 0)
1333 if (*path
== '/') /* '/' leading server name */
1334 path
++; /* probably came from server browsing */
1336 url
= vfs_url_split (path
, SMB_PORT
, URL_FLAGS_NONE
);
1340 *sc
= smbfs_open_link (url
->host
, url
->path
, url
->user
, &url
->port
, url
->password
);
1341 wipe_password (url
->password
);
1344 remote_path
= g_strdup (url
->path
);
1346 vfs_path_element_free (url
);
1349 if (remote_path
== NULL
)
1352 /* NOTE: tildes are deprecated. See ftpfs.c */
1354 int f
= strcmp (remote_path
, "/~") ? 0 : 1;
1356 if (f
!= 0 || strncmp (remote_path
, "/~/", 3) == 0)
1359 s
= concat_dir_and_file ((*sc
)->home
, remote_path
+ 3 - f
);
1360 g_free (remote_path
);
1368 /* --------------------------------------------------------------------------------------------- */
1372 is_error (int result
, int errno_num
)
1374 if (!(result
== -1))
1375 return my_errno
= 0;
1377 my_errno
= errno_num
;
1382 /* --------------------------------------------------------------------------------------------- */
1385 smbfs_opendir (const vfs_path_t
* vpath
)
1387 opendir_info
*smbfs_info
;
1388 smbfs_connection
*sc
;
1390 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath
, -1);
1392 DEBUG (3, ("smbfs_opendir(dirname:%s)\n", path_element
->path
));
1394 if (!(remote_dir
= smbfs_get_path (&sc
, vpath
)))
1397 /* FIXME: where freed? */
1398 smbfs_info
= g_new (opendir_info
, 1);
1399 smbfs_info
->server_list
= FALSE
;
1400 smbfs_info
->path
= g_strdup (path_element
->path
); /* keep original */
1401 smbfs_info
->dirname
= remote_dir
;
1402 smbfs_info
->conn
= sc
;
1403 smbfs_info
->entries
= 0;
1404 smbfs_info
->current
= 0;
1409 /* --------------------------------------------------------------------------------------------- */
1412 smbfs_fake_server_stat (const char *server_url
, const char *path
, struct stat
*buf
)
1419 if ((p
= strrchr (path
, '/')))
1420 path
= p
+ 1; /* advance until last '/' */
1422 if (!current_info
->entries
)
1424 if (!smbfs_loaddir (current_info
)) /* browse host */
1428 if (current_info
->server_list
== True
)
1430 dentry
= current_info
->entries
;
1431 DEBUG (4, ("fake stat for SERVER \"%s\"\n", path
));
1434 if (strcmp (dentry
->text
, path
) == 0)
1436 DEBUG (4, ("smbfs_fake_server_stat: %s:%4o\n",
1437 dentry
->text
, (int) dentry
->my_stat
.st_mode
));
1438 memcpy (buf
, &dentry
->my_stat
, sizeof (struct stat
));
1441 dentry
= dentry
->next
;
1448 /* --------------------------------------------------------------------------------------------- */
1451 smbfs_fake_share_stat (const char *server_url
, const char *path
, struct stat
*buf
)
1454 if (strlen (path
) < strlen (server_url
))
1457 if (!current_share_info
)
1458 { /* Server was not stat()ed */
1459 /* Make sure there is such share at server */
1460 smbfs_connection
*sc
;
1462 vfs_path_t
*vpath
= vfs_path_from_str (path
);
1464 p
= smbfs_get_path (&sc
, vpath
);
1465 vfs_path_free (vpath
);
1470 memset (buf
, 0, sizeof (*buf
));
1471 /* show this as dir */
1473 (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
| S_IXUSR
| S_IXGRP
| S_IXOTH
) & myumask
;
1479 path
+= strlen (server_url
); /* we only want share name */
1482 if (*path
== '/') /* '/' leading server name */
1483 path
++; /* probably came from server browsing */
1485 if (!current_share_info
->entries
)
1487 if (!smbfs_loaddir (current_share_info
)) /* browse host */
1490 dentry
= current_share_info
->entries
;
1491 DEBUG (3, ("smbfs_fake_share_stat: %s on %s\n", path
, server_url
));
1494 if (strcmp (dentry
->text
, path
) == 0)
1496 DEBUG (6, ("smbfs_fake_share_stat: %s:%4o\n",
1497 dentry
->text
, (int) dentry
->my_stat
.st_mode
));
1498 memcpy (buf
, &dentry
->my_stat
, sizeof (struct stat
));
1501 dentry
= dentry
->next
;
1507 /* --------------------------------------------------------------------------------------------- */
1508 /* stat a single file */
1511 smbfs_get_remote_stat (smbfs_connection
* sc
, const char *path
, struct stat
*buf
)
1513 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
1516 DEBUG (3, ("smbfs_get_remote_stat(): mypath:%s\n", path
));
1518 mypath
= smbfs_convert_path (path
, FALSE
);
1520 #if 0 /* single_entry is never free()d now. And only my_stat is used */
1521 single_entry
= g_new (dir_entry
, 1);
1523 single_entry
->text
= dos_to_unix (g_strdup (finfo
->name
), 1);
1525 single_entry
->next
= 0;
1528 single_entry
= g_new0 (dir_entry
, 1);
1530 if (cli_list (sc
->cli
, mypath
, attribute
, smbfs_loaddir_helper
, single_entry
) < 1)
1534 return -1; /* cli_list returns number of files */
1537 memcpy (buf
, &single_entry
->my_stat
, sizeof (struct stat
));
1539 /* don't free here, use for smbfs_fstat() */
1540 /* g_free(single_entry->text);
1541 g_free(single_entry); */
1546 /* --------------------------------------------------------------------------------------------- */
1549 smbfs_search_dir_entry (dir_entry
* dentry
, const char *text
, struct stat
*buf
)
1553 if (strcmp (text
, dentry
->text
) == 0)
1555 memcpy (buf
, &dentry
->my_stat
, sizeof (struct stat
));
1556 memcpy (&single_entry
->my_stat
, &dentry
->my_stat
, sizeof (struct stat
));
1559 dentry
= dentry
->next
;
1564 /* --------------------------------------------------------------------------------------------- */
1567 smbfs_get_stat_info (smbfs_connection
* sc
, const char *path
, struct stat
*buf
)
1571 dir_entry
*dentry
= current_info
->entries
;
1573 const char *mypath
= path
;
1575 mypath
++; /* cut off leading '/' */
1576 if ((p
= strrchr (mypath
, '/')))
1577 mypath
= p
+ 1; /* advance until last file/dir name */
1578 DEBUG (3, ("smbfs_get_stat_info: mypath:%s, current_info->dirname:%s\n",
1579 mypath
, current_info
->dirname
));
1583 DEBUG (1, ("No dir entries (empty dir) cached:'%s', wanted:'%s'\n",
1584 current_info
->dirname
, path
));
1588 if (!single_entry
) /* when found, this will be written too */
1589 single_entry
= g_new (dir_entry
, 1);
1590 if (smbfs_search_dir_entry (current_info
->entries
, mypath
, buf
) == 0)
1594 /* now try to identify mypath as PARENT dir */
1598 mdp
= mydir
= g_strdup (current_info
->dirname
);
1599 if ((p
= strrchr (mydir
, '/')))
1600 *p
= 0; /* advance util last '/' */
1601 if ((p
= strrchr (mydir
, '/')))
1602 mydir
= p
+ 1; /* advance util last '/' */
1603 if (strcmp (mydir
, mypath
) == 0)
1604 { /* fake a stat for ".." */
1605 memset (buf
, 0, sizeof (struct stat
));
1606 buf
->st_mode
= (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
) & myumask
;
1607 memcpy (&single_entry
->my_stat
, buf
, sizeof (struct stat
));
1609 DEBUG (1, (" PARENT:found in %s\n", current_info
->dirname
));
1614 /* now try to identify as CURRENT dir? */
1616 char *dnp
= current_info
->dirname
;
1617 DEBUG (6, ("smbfs_get_stat_info: is %s current dir? this dir is: %s\n",
1618 mypath
, current_info
->dirname
));
1625 if (strcmp (mypath
, dnp
) == 0)
1627 memset (buf
, 0, sizeof (struct stat
));
1628 buf
->st_mode
= (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
) & myumask
;
1629 memcpy (&single_entry
->my_stat
, buf
, sizeof (struct stat
));
1630 DEBUG (1, (" CURRENT:found in %s\n", current_info
->dirname
));
1634 DEBUG (3, ("'%s' not found in current_info '%s'\n", path
, current_info
->dirname
));
1635 /* try to find this in the PREVIOUS listing */
1638 if (smbfs_search_dir_entry (previous_info
->entries
, mypath
, buf
) == 0)
1640 DEBUG (3, ("'%s' not found in previous_info '%s'\n", path
, previous_info
->dirname
));
1642 /* try to find this in the SHARE listing */
1643 if (current_share_info
)
1645 if (smbfs_search_dir_entry (current_share_info
->entries
, mypath
, buf
) == 0)
1647 DEBUG (3, ("'%s' not found in share_info '%s'\n", path
, current_share_info
->dirname
));
1649 /* try to find this in the SERVER listing */
1650 if (current_server_info
)
1652 if (smbfs_search_dir_entry (current_server_info
->entries
, mypath
, buf
) == 0)
1654 DEBUG (3, ("'%s' not found in server_info '%s'\n", path
, current_server_info
->dirname
));
1656 /* nothing found. get stat file info from server */
1657 return smbfs_get_remote_stat (sc
, path
, buf
);
1660 /* --------------------------------------------------------------------------------------------- */
1663 smbfs_chdir (const vfs_path_t
* vpath
)
1666 smbfs_connection
*sc
;
1667 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath
, -1);
1669 DEBUG (3, ("smbfs_chdir(path:%s)\n", path_element
->path
));
1670 if (!(remote_dir
= smbfs_get_path (&sc
, vpath
)))
1672 g_free (remote_dir
);
1677 /* --------------------------------------------------------------------------------------------- */
1680 smbfs_loaddir_by_name (const vfs_path_t
* vpath
)
1684 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath
, -1);
1686 mypath
= g_strdup (path_element
->path
);
1687 p
= strrchr (mypath
, '/');
1691 DEBUG (6, ("smbfs_loaddir_by_name(%s)\n", mypath
));
1692 smbfs_chdir (vpath
);
1693 info
= smbfs_opendir (vpath
);
1697 smbfs_readdir (info
);
1698 smbfs_loaddir (info
);
1702 /* --------------------------------------------------------------------------------------------- */
1705 smbfs_stat (const vfs_path_t
* vpath
, struct stat
*buf
)
1707 smbfs_connection
*sc
;
1709 char *service
, *pp
, *at
;
1711 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath
, -1);
1713 DEBUG (3, ("smbfs_stat(path:%s)\n", path_element
->path
));
1717 DEBUG (1, ("current_info = NULL: "));
1718 if (smbfs_loaddir_by_name (vpath
) < 0)
1722 /* check if stating server */
1723 p
= path_element
->path
;
1724 if (strncmp (p
, URL_HEADER
, HEADER_LEN
))
1726 DEBUG (1, ("'%s' doesnt start with '%s' (length %d)\n", p
, URL_HEADER
, HEADER_LEN
));
1734 pp
= strchr (p
, '/'); /* advance past next '/' */
1735 at
= strchr (p
, '@');
1736 pstrcpy (server_url
, URL_HEADER
);
1739 char *z
= &(server_url
[sizeof (server_url
) - 1]);
1742 at
= &(server_url
[HEADER_LEN
]) + (at
- p
+ 1);
1745 at
= &(server_url
[HEADER_LEN
]);
1750 pstrcat (server_url
, current_bucket
->host
);
1754 if (!current_info
->server_list
)
1756 if (smbfs_loaddir_by_name (vpath
) < 0)
1759 return smbfs_fake_server_stat (server_url
, path_element
->path
, buf
);
1762 if (!strchr (++pp
, '/'))
1764 return smbfs_fake_share_stat (server_url
, path_element
->path
, buf
);
1767 /* stating inside share at this point */
1768 if (!(service
= smbfs_get_path (&sc
, vpath
))) /* connects if necessary */
1771 int hostlen
= strlen (current_bucket
->host
);
1772 char *ppp
= service
+ strlen (service
) - hostlen
;
1773 char *sp
= server_url
+ strlen (server_url
) - hostlen
;
1775 if (strcmp (sp
, ppp
) == 0)
1777 /* make server name appear as directory */
1778 DEBUG (1, ("smbfs_stat: showing server as directory\n"));
1779 memset (buf
, 0, sizeof (struct stat
));
1780 buf
->st_mode
= (S_IFDIR
| S_IRUSR
| S_IRGRP
| S_IROTH
) & myumask
;
1785 /* check if current_info is in share requested */
1787 pp
= strchr (p
, '/');
1790 p
= ++pp
; /* advance past server name */
1791 pp
= strchr (p
, '/');
1794 *pp
= 0; /* cut off everthing after service name */
1796 p
= IPC
; /* browsing for services */
1797 pp
= current_info
->dirname
;
1800 if (strncmp (p
, pp
, strlen (p
)) != 0)
1802 DEBUG (6, ("desired '%s' is not loaded, we have '%s'\n", p
, pp
));
1803 if (smbfs_loaddir_by_name (vpath
) < 0)
1808 DEBUG (6, ("loaded dir: '%s'\n", current_info
->dirname
));
1811 /* stat dirs & files under shares now */
1812 return smbfs_get_stat_info (sc
, path_element
->path
, buf
);
1815 /* --------------------------------------------------------------------------------------------- */
1818 smbfs_lseek (void *data
, off_t offset
, int whence
)
1820 smbfs_handle
*info
= (smbfs_handle
*) data
;
1824 ("smbfs_lseek(info->nread => %d, offset => %d, whence => %d) \n",
1825 (int) info
->nread
, (int) offset
, whence
));
1830 info
->nread
= offset
;
1833 info
->nread
+= offset
;
1836 if (!cli_qfileinfo (info
->cli
, info
->fnum
,
1837 NULL
, &size
, NULL
, NULL
, NULL
,
1839 !cli_getattrE (info
->cli
, info
->fnum
, NULL
, &size
, NULL
, NULL
, NULL
))
1844 info
->nread
= size
+ offset
;
1851 /* --------------------------------------------------------------------------------------------- */
1854 smbfs_mknod (const vfs_path_t
* vpath
, mode_t mode
, dev_t dev
)
1856 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath
, -1);
1859 ("smbfs_mknod(path:%s, mode:%d, dev:%u)\n", path_element
->path
, mode
,
1860 (unsigned int) dev
));
1861 my_errno
= EOPNOTSUPP
;
1865 /* --------------------------------------------------------------------------------------------- */
1868 smbfs_mkdir (const vfs_path_t
* vpath
, mode_t mode
)
1870 smbfs_connection
*sc
;
1873 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath
, -1);
1875 DEBUG (3, ("smbfs_mkdir(path:%s, mode:%d)\n", path_element
->path
, (int) mode
));
1876 if ((remote_file
= smbfs_get_path (&sc
, vpath
)) == 0)
1878 g_free (remote_file
);
1879 cpath
= smbfs_convert_path (path_element
->path
, FALSE
);
1881 if (!cli_mkdir (sc
->cli
, cpath
))
1883 my_errno
= cli_error (sc
->cli
, NULL
, &err
, NULL
);
1884 message (D_ERROR
, MSG_ERROR
, _("Error %s creating directory %s"),
1885 cli_errstr (sc
->cli
), CNV_LANG (cpath
));
1893 /* --------------------------------------------------------------------------------------------- */
1896 smbfs_rmdir (const vfs_path_t
* vpath
)
1898 smbfs_connection
*sc
;
1901 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath
, -1);
1903 DEBUG (3, ("smbfs_rmdir(path:%s)\n", path_element
->path
));
1904 if ((remote_file
= smbfs_get_path (&sc
, vpath
)) == 0)
1906 g_free (remote_file
);
1907 cpath
= smbfs_convert_path (path_element
->path
, FALSE
);
1909 if (!cli_rmdir (sc
->cli
, cpath
))
1911 my_errno
= cli_error (sc
->cli
, NULL
, &err
, NULL
);
1912 message (D_ERROR
, MSG_ERROR
, _("Error %s removing directory %s"),
1913 cli_errstr (sc
->cli
), CNV_LANG (cpath
));
1922 /* --------------------------------------------------------------------------------------------- */
1925 smbfs_link (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
1927 vfs_path_element_t
*path_element1
= vfs_path_get_by_index (vpath1
, -1);
1928 vfs_path_element_t
*path_element2
= vfs_path_get_by_index (vpath2
, -1);
1930 DEBUG (3, ("smbfs_link(p1:%s, p2:%s)\n", path_element1
->path
, path_element2
->path
));
1931 my_errno
= EOPNOTSUPP
;
1935 /* --------------------------------------------------------------------------------------------- */
1938 smbfs_free (vfsid id
)
1940 DEBUG (3, ("smbfs_free(%p)\n", id
));
1941 smbfs_auth_free_all ();
1944 /* --------------------------------------------------------------------------------------------- */
1945 /* Gives up on a socket and reopens the connection, the child own the socket
1950 smbfs_forget (const char *path
)
1952 vfs_path_element_t
*p
;
1954 if (strncmp (path
, URL_HEADER
, HEADER_LEN
) != 0)
1957 DEBUG (3, ("smbfs_forget(path:%s)\n", path
));
1960 if (path
[0] == '/' && path
[1] == '/')
1963 p
= vfs_url_split (path
, SMB_PORT
, URL_FLAGS_NONE
);
1968 for (i
= 0; i
< SMBFS_MAX_CONNECTIONS
; i
++)
1970 if (smbfs_connections
[i
].cli
1971 && (strcmp (p
->host
, smbfs_connections
[i
].host
) == 0)
1972 && (strcmp (p
->user
, smbfs_connections
[i
].user
) == 0)
1973 && (p
->port
== smbfs_connections
[i
].port
))
1976 /* close socket: the child owns it now */
1977 cli_shutdown (smbfs_connections
[i
].cli
);
1979 /* reopen the connection */
1980 smbfs_connections
[i
].cli
= smbfs_do_connect (p
->host
, smbfs_connections
[i
].service
);
1984 vfs_path_element_free (p
);
1988 /* --------------------------------------------------------------------------------------------- */
1991 smbfs_setctl (const vfs_path_t
* vpath
, int ctlop
, void *arg
)
1993 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath
, -1);
1996 DEBUG (3, ("smbfs_setctl(path:%s, ctlop:%d)\n", path_element
->path
, ctlop
));
1999 case VFS_SETCTL_FORGET
:
2000 smbfs_forget (path_element
->path
);
2006 /* --------------------------------------------------------------------------------------------- */
2008 static smbfs_handle
*
2009 smbfs_open_readwrite (smbfs_handle
* remote_handle
, char *rname
, int flags
, mode_t mode
)
2015 if (flags
& O_TRUNC
) /* if it exists truncate to zero */
2016 DEBUG (3, ("smbfs_open: O_TRUNC\n"));
2018 remote_handle
->fnum
=
2019 #if 1 /* Don't play with flags, it is cli_open() headache */
2020 cli_open (remote_handle
->cli
, rname
, flags
, DENY_NONE
);
2021 #else /* What's a reasons to has this code ? */
2022 cli_open (remote_handle
->cli
, rname
, ((flags
& O_CREAT
)
2024 (O_WRONLY
| O_APPEND
))) ?
2025 flags
: O_RDONLY
, DENY_NONE
);
2027 if (remote_handle
->fnum
== -1)
2029 message (D_ERROR
, MSG_ERROR
, _("%s opening remote file %s"),
2030 cli_errstr (remote_handle
->cli
), CNV_LANG (rname
));
2031 DEBUG (1, ("smbfs_open(rname:%s) error:%s\n", rname
, cli_errstr (remote_handle
->cli
)));
2032 my_errno
= cli_error (remote_handle
->cli
, NULL
, &err
, NULL
);
2036 if (flags
& O_CREAT
)
2037 return remote_handle
;
2039 if (!cli_qfileinfo (remote_handle
->cli
, remote_handle
->fnum
,
2040 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
, NULL
,
2042 && !cli_getattrE (remote_handle
->cli
, remote_handle
->fnum
,
2043 &remote_handle
->attr
, &size
, NULL
, NULL
, NULL
))
2045 message (D_ERROR
, MSG_ERROR
, "getattrib: %s", cli_errstr (remote_handle
->cli
));
2046 DEBUG (1, ("smbfs_open(rname:%s) getattrib:%s\n", rname
, cli_errstr (remote_handle
->cli
)));
2047 my_errno
= cli_error (remote_handle
->cli
, NULL
, &err
, NULL
);
2048 cli_close (remote_handle
->cli
, remote_handle
->fnum
);
2052 if ((flags
== (O_WRONLY
| O_APPEND
)) /* file.c:copy_file_file() -> do_append */
2053 && smbfs_lseek (remote_handle
, 0, SEEK_END
) == -1)
2055 cli_close (remote_handle
->cli
, remote_handle
->fnum
);
2059 return remote_handle
;
2062 /* --------------------------------------------------------------------------------------------- */
2065 smbfs_open (const vfs_path_t
* vpath
, int flags
, mode_t mode
)
2069 smbfs_connection
*sc
;
2070 smbfs_handle
*remote_handle
;
2071 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath
, -1);
2073 DEBUG (3, ("smbfs_open(file:%s, flags:%d, mode:%o)\n", path_element
->path
, flags
, mode
));
2075 if (!(remote_file
= smbfs_get_path (&sc
, vpath
)))
2078 remote_file
= free_after (smbfs_convert_path (remote_file
, FALSE
), remote_file
);
2080 remote_handle
= g_new (smbfs_handle
, 2);
2081 remote_handle
->cli
= sc
->cli
;
2082 remote_handle
->nread
= 0;
2084 ret
= smbfs_open_readwrite (remote_handle
, remote_file
, flags
, mode
);
2086 g_free (remote_file
);
2088 g_free (remote_handle
);
2093 /* --------------------------------------------------------------------------------------------- */
2096 smbfs_unlink (const vfs_path_t
* vpath
)
2098 smbfs_connection
*sc
;
2101 if ((remote_file
= smbfs_get_path (&sc
, vpath
)) == 0)
2104 remote_file
= free_after (smbfs_convert_path (remote_file
, FALSE
), remote_file
);
2106 if (!cli_unlink (sc
->cli
, remote_file
))
2108 message (D_ERROR
, MSG_ERROR
, _("%s removing remote file %s"),
2109 cli_errstr (sc
->cli
), CNV_LANG (remote_file
));
2110 g_free (remote_file
);
2113 g_free (remote_file
);
2117 /* --------------------------------------------------------------------------------------------- */
2120 smbfs_rename (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
2122 smbfs_connection
*sc
;
2126 if ((ra
= smbfs_get_path (&sc
, vpath1
)) == 0)
2129 if ((rb
= smbfs_get_path (&sc
, vpath2
)) == 0)
2135 ra
= free_after (smbfs_convert_path (ra
, FALSE
), ra
);
2136 rb
= free_after (smbfs_convert_path (rb
, FALSE
), rb
);
2138 retval
= cli_rename (sc
->cli
, ra
, rb
);
2145 message (D_ERROR
, MSG_ERROR
, _("%s renaming files\n"), cli_errstr (sc
->cli
));
2151 /* --------------------------------------------------------------------------------------------- */
2154 smbfs_fstat (void *data
, struct stat
*buf
)
2156 smbfs_handle
*remote_handle
= (smbfs_handle
*) data
;
2158 DEBUG (3, ("smbfs_fstat(fnum:%d)\n", remote_handle
->fnum
));
2160 /* use left over from previous smbfs_get_remote_stat, if available */
2162 memcpy (buf
, &single_entry
->my_stat
, sizeof (struct stat
));
2164 { /* single_entry not set up: bug */
2171 /* --------------------------------------------------------------------------------------------- */
2172 /*** public functions ****************************************************************************/
2173 /* --------------------------------------------------------------------------------------------- */
2176 vfs_smb_authinfo_new (const char *host
, const char *share
, const char *domain
,
2177 const char *user
, const char *pass
)
2181 auth
= g_try_new (struct smb_authinfo
, 1);
2185 auth
->host
= g_strdup (host
);
2186 auth
->share
= g_strdup (share
);
2187 auth
->domain
= g_strdup (domain
);
2188 auth
->user
= g_strdup (user
);
2189 auth
->password
= g_strdup (pass
);
2195 /* --------------------------------------------------------------------------------------------- */
2202 vfs_smbfs_ops
.name
= "smbfs";
2203 vfs_smbfs_ops
.prefix
= "smb";
2204 vfs_smbfs_ops
.flags
= VFSF_NOLINKS
;
2205 vfs_smbfs_ops
.init
= smbfs_init
;
2206 vfs_smbfs_ops
.fill_names
= smbfs_fill_names
;
2207 vfs_smbfs_ops
.open
= smbfs_open
;
2208 vfs_smbfs_ops
.close
= smbfs_close
;
2209 vfs_smbfs_ops
.read
= smbfs_read
;
2210 vfs_smbfs_ops
.write
= smbfs_write
;
2211 vfs_smbfs_ops
.opendir
= smbfs_opendir
;
2212 vfs_smbfs_ops
.readdir
= smbfs_readdir
;
2213 vfs_smbfs_ops
.closedir
= smbfs_closedir
;
2214 vfs_smbfs_ops
.stat
= smbfs_stat
;
2215 vfs_smbfs_ops
.lstat
= smbfs_lstat
;
2216 vfs_smbfs_ops
.fstat
= smbfs_fstat
;
2217 vfs_smbfs_ops
.chmod
= smbfs_chmod
;
2218 vfs_smbfs_ops
.chown
= smbfs_chown
;
2219 vfs_smbfs_ops
.utime
= smbfs_utime
;
2220 vfs_smbfs_ops
.readlink
= smbfs_readlink
;
2221 vfs_smbfs_ops
.symlink
= smbfs_symlink
;
2222 vfs_smbfs_ops
.link
= smbfs_link
;
2223 vfs_smbfs_ops
.unlink
= smbfs_unlink
;
2224 vfs_smbfs_ops
.rename
= smbfs_rename
;
2225 vfs_smbfs_ops
.chdir
= smbfs_chdir
;
2226 vfs_smbfs_ops
.ferrno
= smbfs_errno
;
2227 vfs_smbfs_ops
.lseek
= smbfs_lseek
;
2228 vfs_smbfs_ops
.mknod
= smbfs_mknod
;
2229 vfs_smbfs_ops
.free
= smbfs_free
;
2230 vfs_smbfs_ops
.mkdir
= smbfs_mkdir
;
2231 vfs_smbfs_ops
.rmdir
= smbfs_rmdir
;
2232 vfs_smbfs_ops
.setctl
= smbfs_setctl
;
2233 vfs_register_class (&vfs_smbfs_ops
);
2236 /* --------------------------------------------------------------------------------------------- */