1998-02-27 07:54:42 +03:00
|
|
|
/* Virtual File System: FTP file system.
|
|
|
|
Copyright (C) 1995 The Free Software Foundation
|
|
|
|
|
|
|
|
Written by: 1995 Ching Hui
|
|
|
|
1995 Jakub Jelinek
|
|
|
|
1995, 1996, 1997 Miguel de Icaza
|
|
|
|
1997 Norbert Warmuth
|
1998-05-26 04:53:24 +04:00
|
|
|
1998 Pavel Machek
|
2000-01-13 02:45:51 +03:00
|
|
|
|
1999-12-16 15:55:16 +03:00
|
|
|
$Id$
|
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2 of
|
|
|
|
the License, or (at your option) any later version.
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
1998-09-27 23:27:58 +04:00
|
|
|
GNU Library General Public License for more details.
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this program; if not, write to the Free Software
|
2000-08-23 02:50:00 +04:00
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* FTPfs TODO:
|
|
|
|
|
|
|
|
- make it more robust - all the connects etc. should handle EADDRINUSE and
|
|
|
|
ERETRY (have I spelled these names correctly?)
|
|
|
|
- make the user able to flush a connection - all the caches will get empty
|
|
|
|
etc., (tarfs as well), we should give there a user selectable timeout
|
|
|
|
and assign a key sequence.
|
|
|
|
- use hash table instead of linklist to cache ftpfs directory.
|
2000-01-24 15:35:52 +03:00
|
|
|
|
|
|
|
What to do with this?
|
|
|
|
|
|
|
|
|
|
|
|
* NOTE: Usage of tildes is deprecated, consider:
|
|
|
|
* cd /#ftp:pavel@hobit
|
|
|
|
* cd ~
|
|
|
|
* And now: what do I want to do? Do I want to go to /home/pavel or to
|
|
|
|
* /#ftp:hobit/home/pavel? I think first has better sense...
|
|
|
|
*
|
|
|
|
{
|
|
|
|
int f = !strcmp( remote_path, "/~" );
|
|
|
|
if (f || !strncmp( remote_path, "/~/", 3 )) {
|
|
|
|
char *s;
|
|
|
|
s = concat_dir_and_file( qhome (*bucket), remote_path +3-f );
|
|
|
|
g_free (remote_path);
|
|
|
|
remote_path = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
*/
|
|
|
|
|
1998-10-13 02:07:53 +04:00
|
|
|
/* Namespace pollution: horrible */
|
|
|
|
|
2001-06-26 01:34:46 +04:00
|
|
|
#include <config.h>
|
2000-03-20 04:57:58 +03:00
|
|
|
#include <sys/types.h> /* POSIX-required by sys/socket.h and netdb.h */
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <netdb.h> /* struct hostent */
|
|
|
|
#include <sys/socket.h> /* AF_INET */
|
|
|
|
#include <netinet/in.h> /* struct in_addr */
|
|
|
|
#ifdef HAVE_SETSOCKOPT
|
|
|
|
# include <netinet/ip.h> /* IP options */
|
|
|
|
#endif
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <arpa/ftp.h>
|
|
|
|
#include <arpa/telnet.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#ifdef USE_TERMNET
|
|
|
|
#include <termnet.h>
|
|
|
|
#endif
|
|
|
|
|
1999-01-21 01:01:11 +03:00
|
|
|
#include "utilvfs.h"
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
#include "xdirentry.h"
|
1998-02-27 07:54:42 +03:00
|
|
|
#include "vfs.h"
|
|
|
|
#include "tcputil.h"
|
|
|
|
#include "../src/dialog.h"
|
2000-04-05 17:08:42 +04:00
|
|
|
#include "../src/setup.h" /* for load_anon_passwd */
|
1998-02-27 07:54:42 +03:00
|
|
|
#include "container.h"
|
|
|
|
#include "ftpfs.h"
|
|
|
|
#ifndef MAXHOSTNAMELEN
|
|
|
|
# define MAXHOSTNAMELEN 64
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define UPLOAD_ZERO_LENGTH_FILE
|
1999-12-08 14:39:14 +03:00
|
|
|
#define SUP super->u.ftp
|
|
|
|
#define FH_SOCK fh->u.ftp.sock
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
static int my_errno;
|
1998-02-27 07:54:42 +03:00
|
|
|
static int code;
|
|
|
|
|
|
|
|
/* Delay to retry a connection */
|
|
|
|
int ftpfs_retry_seconds = 30;
|
|
|
|
|
|
|
|
/* Method to use to connect to ftp sites */
|
|
|
|
int ftpfs_use_passive_connections = 1;
|
|
|
|
|
1998-09-15 23:41:22 +04:00
|
|
|
/* Method used to get directory listings:
|
1998-10-14 06:56:18 +04:00
|
|
|
* 1: try 'LIST -la <path>', if it fails
|
|
|
|
* fall back to CWD <path>; LIST
|
|
|
|
* 0: always use CWD <path>; LIST
|
1998-09-15 23:41:22 +04:00
|
|
|
*/
|
|
|
|
int ftpfs_use_unix_list_options = 1;
|
|
|
|
|
1999-01-31 23:28:13 +03:00
|
|
|
/* First "CWD <path>", then "LIST -la ." */
|
|
|
|
int ftpfs_first_cd_then_ls;
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
/* Use the ~/.netrc */
|
|
|
|
int use_netrc = 1;
|
|
|
|
|
|
|
|
extern char *home_dir;
|
|
|
|
|
|
|
|
/* Anonymous setup */
|
1999-12-08 14:39:14 +03:00
|
|
|
char *ftpfs_anonymous_passwd = NULL;
|
2000-04-28 11:43:13 +04:00
|
|
|
int ftpfs_directory_timeout = 900;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* Proxy host */
|
1999-12-08 14:39:14 +03:00
|
|
|
char *ftpfs_proxy_host = NULL;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* wether we have to use proxy by default? */
|
|
|
|
int ftpfs_always_use_proxy;
|
|
|
|
|
|
|
|
/* source routing host */
|
|
|
|
extern int source_route;
|
|
|
|
|
|
|
|
/* Where we store the transactions */
|
1998-09-13 14:40:43 +04:00
|
|
|
static FILE *logfile = NULL;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* If true, the directory cache is forced to reload */
|
|
|
|
static int force_expiration = 0;
|
|
|
|
|
2001-05-15 03:34:43 +04:00
|
|
|
#ifdef FIXME_LATER_ALIGATOR
|
1998-09-13 14:40:43 +04:00
|
|
|
static struct linklist *connections_list;
|
2001-05-15 03:34:43 +04:00
|
|
|
#endif
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* command wait_flag: */
|
|
|
|
#define NONE 0x00
|
|
|
|
#define WAIT_REPLY 0x01
|
|
|
|
#define WANT_STRING 0x02
|
1998-09-13 14:40:43 +04:00
|
|
|
static char reply_str [80];
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-01-31 23:28:13 +03:00
|
|
|
/* char *translate_path (struct ftpfs_connection *bucket, char *remote_path)
|
|
|
|
Translate a Unix path, i.e. MC's internal path representation (e.g.
|
|
|
|
/somedir/somefile) to a path valid for the remote server. Every path
|
|
|
|
transfered to the remote server has to be mangled by this function
|
|
|
|
right prior to sending it.
|
|
|
|
Currently only Amiga ftp servers are handled in a special manner.
|
|
|
|
|
|
|
|
When the remote server is an amiga:
|
|
|
|
a) strip leading slash if necesarry
|
|
|
|
b) replace first occurance of ":/" with ":"
|
|
|
|
c) strip trailing "/."
|
|
|
|
*/
|
|
|
|
|
2000-02-25 15:53:06 +03:00
|
|
|
static char *ftpfs_get_current_directory (vfs *me, vfs_s_super *super);
|
|
|
|
static int ftpfs_chdir_internal (vfs *me, vfs_s_super *super, char *remote_path);
|
2001-10-20 11:12:55 +04:00
|
|
|
static int command (vfs *me, vfs_s_super *super, int wait_reply, char *fmt, ...)
|
|
|
|
__attribute__ ((format (printf, 4, 5)));
|
2002-01-29 11:09:20 +03:00
|
|
|
static int ftpfs_open_socket (vfs *me, vfs_s_super *super);
|
|
|
|
static int login_server (vfs *me, vfs_s_super *super, const char *netrcpass);
|
2002-07-13 00:00:03 +04:00
|
|
|
static int lookup_netrc (const char *host, char **login, char **pass);
|
2000-02-25 15:53:06 +03:00
|
|
|
|
1999-01-31 23:28:13 +03:00
|
|
|
static char *
|
1999-12-16 15:55:16 +03:00
|
|
|
translate_path (vfs *me, vfs_s_super *super, const char *remote_path)
|
1999-01-31 23:28:13 +03:00
|
|
|
{
|
1999-12-08 14:39:14 +03:00
|
|
|
if (!SUP.remote_is_amiga)
|
1999-12-16 15:55:16 +03:00
|
|
|
return g_strdup (remote_path);
|
1999-01-31 23:28:13 +03:00
|
|
|
else {
|
1999-12-16 15:55:16 +03:00
|
|
|
char *ret, *p;
|
1999-08-31 04:11:57 +04:00
|
|
|
|
1999-01-31 23:28:13 +03:00
|
|
|
if (logfile) {
|
|
|
|
fprintf (logfile, "MC -- translate_path: %s\n", remote_path);
|
|
|
|
fflush (logfile);
|
|
|
|
}
|
|
|
|
|
2002-02-01 15:21:55 +03:00
|
|
|
/* strip leading slash(es) */
|
|
|
|
while (*remote_path == '/')
|
|
|
|
remote_path++;
|
|
|
|
|
1999-08-31 04:11:57 +04:00
|
|
|
/*
|
|
|
|
* Don't change "/" into "", e.g. "CWD " would be
|
|
|
|
* invalid.
|
|
|
|
*/
|
2002-02-01 15:21:55 +03:00
|
|
|
if (*remote_path == '\0')
|
1999-08-31 04:11:57 +04:00
|
|
|
return g_strdup (".");
|
1999-01-31 23:28:13 +03:00
|
|
|
|
2002-02-01 15:21:55 +03:00
|
|
|
ret = g_strdup (remote_path);
|
1999-01-31 23:28:13 +03:00
|
|
|
|
|
|
|
/* replace first occurance of ":/" with ":" */
|
1999-08-31 04:11:57 +04:00
|
|
|
if ((p = strchr (ret, ':')) && *(p + 1) == '/')
|
1999-01-31 23:28:13 +03:00
|
|
|
strcpy (p + 1, p + 2);
|
|
|
|
|
|
|
|
/* strip trailing "/." */
|
1999-08-31 04:11:57 +04:00
|
|
|
if ((p = strrchr (ret, '/')) && *(p + 1) == '.' && *(p + 2) == '\0')
|
1999-01-31 23:28:13 +03:00
|
|
|
*p = '\0';
|
1999-08-31 04:11:57 +04:00
|
|
|
return ret;
|
1999-01-31 23:28:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
/* Extract the hostname and username from the path */
|
1998-10-14 06:56:18 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* path is in the form: [user@]hostname:port/remote-dir, e.g.:
|
1998-02-27 07:54:42 +03:00
|
|
|
* ftp://sunsite.unc.edu/pub/linux
|
|
|
|
* ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
|
|
|
|
* ftp://tsx-11.mit.edu:8192/
|
|
|
|
* ftp://joe@foo.edu:11321/private
|
|
|
|
* If the user is empty, e.g. ftp://@roxanne/private, then your login name
|
|
|
|
* is supplied.
|
1998-10-14 06:56:18 +04:00
|
|
|
*
|
|
|
|
*/
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2000-02-23 10:43:14 +03:00
|
|
|
#define FTP_COMMAND_PORT 21
|
|
|
|
#define HSC_PROXY_PORT 9875
|
1999-12-08 14:39:14 +03:00
|
|
|
|
2002-07-12 03:08:30 +04:00
|
|
|
static void
|
|
|
|
ftp_split_url(char *path, char **host, char **user, int *port, char **pass)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = vfs_split_url (path, host, user, port, pass, FTP_COMMAND_PORT,
|
|
|
|
URL_ALLOW_ANON);
|
|
|
|
|
|
|
|
if (!*user) {
|
2002-07-13 03:41:43 +04:00
|
|
|
/* Look up user and password in netrc */
|
2002-07-12 03:08:30 +04:00
|
|
|
if (use_netrc)
|
|
|
|
lookup_netrc (*host, user, pass);
|
2002-07-13 03:20:03 +04:00
|
|
|
if (!*user)
|
2002-07-12 03:08:30 +04:00
|
|
|
*user = g_strdup ("anonymous");
|
|
|
|
}
|
|
|
|
|
2002-07-13 03:41:43 +04:00
|
|
|
/* Look up password in netrc for known user */
|
|
|
|
if (use_netrc && *user && pass && !*pass) {
|
|
|
|
char *new_user;
|
|
|
|
|
|
|
|
lookup_netrc (*host, &new_user, pass);
|
|
|
|
|
|
|
|
/* If user is different, remove password */
|
|
|
|
if (new_user && strcmp (*user, new_user)) {
|
|
|
|
g_free (*pass);
|
|
|
|
*pass = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (new_user);
|
|
|
|
}
|
|
|
|
|
2002-07-12 03:08:30 +04:00
|
|
|
if (p)
|
|
|
|
g_free (p);
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
|
1998-10-14 06:56:18 +04:00
|
|
|
static int
|
1999-12-08 14:39:14 +03:00
|
|
|
get_reply (vfs *me, int sock, char *string_buf, int string_len)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2000-04-14 05:14:26 +04:00
|
|
|
char answer[BUF_1K];
|
1998-02-27 07:54:42 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (;;) {
|
1999-12-08 14:39:14 +03:00
|
|
|
if (!vfs_s_get_line (me, sock, answer, sizeof (answer), '\n')){
|
1998-02-27 07:54:42 +03:00
|
|
|
if (string_buf)
|
|
|
|
*string_buf = 0;
|
|
|
|
code = 421;
|
|
|
|
return 4;
|
|
|
|
}
|
1999-01-11 03:48:23 +03:00
|
|
|
switch (sscanf(answer, "%d", &code)){
|
1998-02-27 07:54:42 +03:00
|
|
|
case 0:
|
|
|
|
if (string_buf) {
|
1999-01-11 03:48:23 +03:00
|
|
|
strncpy (string_buf, answer, string_len - 1);
|
1998-02-27 07:54:42 +03:00
|
|
|
*(string_buf + string_len - 1) = 0;
|
|
|
|
}
|
|
|
|
code = 500;
|
|
|
|
return 5;
|
|
|
|
case 1:
|
|
|
|
if (answer[3] == '-') {
|
|
|
|
while (1) {
|
1999-12-08 14:39:14 +03:00
|
|
|
if (!vfs_s_get_line (me, sock, answer, sizeof(answer), '\n')){
|
1998-02-27 07:54:42 +03:00
|
|
|
if (string_buf)
|
|
|
|
*string_buf = 0;
|
|
|
|
code = 421;
|
|
|
|
return 4;
|
|
|
|
}
|
1999-01-11 03:48:23 +03:00
|
|
|
if ((sscanf (answer, "%d", &i) > 0) &&
|
1998-02-27 07:54:42 +03:00
|
|
|
(code == i) && (answer[3] == ' '))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-01-11 03:48:23 +03:00
|
|
|
if (string_buf){
|
|
|
|
strncpy (string_buf, answer, string_len - 1);
|
1998-02-27 07:54:42 +03:00
|
|
|
*(string_buf + string_len - 1) = 0;
|
|
|
|
}
|
|
|
|
return code / 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-29 11:09:20 +03:00
|
|
|
static int
|
|
|
|
reconnect (vfs *me, vfs_s_super *super)
|
|
|
|
{
|
|
|
|
int sock = ftpfs_open_socket (me, super);
|
|
|
|
if (sock != -1){
|
|
|
|
char *cwdir = SUP.cwdir;
|
|
|
|
close (SUP.sock);
|
|
|
|
SUP.sock = sock;
|
|
|
|
SUP.cwdir = NULL;
|
|
|
|
if (login_server (me, super, SUP.password)){
|
2002-05-17 17:32:02 +04:00
|
|
|
if (!cwdir)
|
|
|
|
return 1;
|
2002-01-29 11:09:20 +03:00
|
|
|
sock = ftpfs_chdir_internal (me, super, cwdir);
|
|
|
|
g_free (cwdir);
|
|
|
|
return sock == COMPLETE;
|
|
|
|
}
|
2002-02-06 15:28:08 +03:00
|
|
|
SUP.cwdir = cwdir;
|
2002-01-29 11:09:20 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
static int
|
1999-12-08 14:39:14 +03:00
|
|
|
command (vfs *me, vfs_s_super *super, int wait_reply, char *fmt, ...)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
va_list ap;
|
1998-10-30 20:45:43 +03:00
|
|
|
char *str, *fmt_str;
|
1999-01-11 03:48:23 +03:00
|
|
|
int status;
|
1999-12-08 14:39:14 +03:00
|
|
|
int sock = SUP.sock;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
va_start (ap, fmt);
|
1998-10-30 20:45:43 +03:00
|
|
|
fmt_str = g_strdup_vprintf (fmt, ap);
|
1998-02-27 07:54:42 +03:00
|
|
|
va_end (ap);
|
1998-10-30 20:45:43 +03:00
|
|
|
|
2000-10-16 22:37:54 +04:00
|
|
|
status = strlen (fmt_str);
|
|
|
|
str = g_realloc (fmt_str, status + 3);
|
|
|
|
strcpy (str + status, "\r\n");
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
if (logfile){
|
1998-10-30 20:45:43 +03:00
|
|
|
if (strncmp (str, "PASS ", 5) == 0){
|
2000-10-16 22:37:54 +04:00
|
|
|
fputs ("PASS <Password not logged>\r\n", logfile);
|
1998-02-27 07:54:42 +03:00
|
|
|
} else
|
2000-10-16 22:37:54 +04:00
|
|
|
fwrite (str, status + 2, 1, logfile);
|
1998-10-30 20:51:28 +03:00
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
fflush (logfile);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
1998-10-30 20:45:43 +03:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
got_sigpipe = 0;
|
1998-10-30 20:45:43 +03:00
|
|
|
enable_interrupt_key ();
|
2000-10-16 22:37:54 +04:00
|
|
|
status = write (SUP.sock, str, status + 2);
|
1998-10-30 20:45:43 +03:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
if (status < 0){
|
|
|
|
code = 421;
|
1998-10-30 20:45:43 +03:00
|
|
|
|
2002-01-29 11:09:20 +03:00
|
|
|
if (errno == EPIPE){ /* Remote server has closed connection */
|
|
|
|
static int level = 0; /* login_server() use command() */
|
|
|
|
if (level == 0){
|
|
|
|
level = 1;
|
|
|
|
status = reconnect (me, super);
|
|
|
|
level = 0;
|
|
|
|
if (status && write (SUP.sock, str, status + 2) > 0)
|
|
|
|
goto ok;
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
got_sigpipe = 1;
|
|
|
|
}
|
2002-01-29 11:09:20 +03:00
|
|
|
g_free (str);
|
1998-10-30 20:45:43 +03:00
|
|
|
disable_interrupt_key ();
|
1998-02-27 07:54:42 +03:00
|
|
|
return TRANSIENT;
|
|
|
|
}
|
2002-01-29 11:09:20 +03:00
|
|
|
ok:
|
|
|
|
g_free (str);
|
1998-10-30 20:45:43 +03:00
|
|
|
disable_interrupt_key ();
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
if (wait_reply)
|
1999-12-08 14:39:14 +03:00
|
|
|
return get_reply (me, sock, (wait_reply & WANT_STRING) ? reply_str : NULL, sizeof (reply_str)-1);
|
1998-02-27 07:54:42 +03:00
|
|
|
return COMPLETE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-12-08 14:39:14 +03:00
|
|
|
free_archive (vfs *me, vfs_s_super *super)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1999-12-08 14:39:14 +03:00
|
|
|
if (SUP.sock != -1){
|
|
|
|
print_vfs_message (_("ftpfs: Disconnecting from %s"), SUP.host);
|
|
|
|
command(me, super, NONE, "QUIT");
|
|
|
|
close(SUP.sock);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2002-07-19 20:04:27 +04:00
|
|
|
g_free (SUP.host);
|
|
|
|
g_free (SUP.user);
|
|
|
|
g_free (SUP.cwdir);
|
|
|
|
g_free (SUP.password);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-03-02 21:09:16 +03:00
|
|
|
/* some defines only used by changetype */
|
|
|
|
/* These two are valid values for the second parameter */
|
|
|
|
#define TYPE_ASCII 0
|
|
|
|
#define TYPE_BINARY 1
|
|
|
|
|
|
|
|
/* This one is only used to initialize bucket->isbinary, don't use it as
|
|
|
|
second parameter to changetype. */
|
|
|
|
#define TYPE_UNKNOWN -1
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
static int
|
1999-12-08 14:39:14 +03:00
|
|
|
changetype (vfs *me, vfs_s_super *super, int binary)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1999-12-08 14:39:14 +03:00
|
|
|
if (binary != SUP.isbinary) {
|
|
|
|
if (command (me, super, WAIT_REPLY, "TYPE %c", binary ? 'I' : 'A') != COMPLETE)
|
1999-01-11 03:48:23 +03:00
|
|
|
ERRNOR (EIO, -1);
|
1999-12-08 14:39:14 +03:00
|
|
|
SUP.isbinary = binary;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
return binary;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This routine logs the user in */
|
|
|
|
static int
|
2002-01-29 11:09:20 +03:00
|
|
|
login_server (vfs *me, vfs_s_super *super, const char *netrcpass)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
#if defined(HSC_PROXY)
|
|
|
|
char *proxypass, *proxyname;
|
|
|
|
#endif
|
|
|
|
char *pass;
|
|
|
|
char *op;
|
|
|
|
char *name; /* login user name */
|
|
|
|
int anon = 0;
|
2000-04-14 05:14:26 +04:00
|
|
|
char reply_string[BUF_MEDIUM];
|
1998-03-02 21:09:16 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
SUP.isbinary = TYPE_UNKNOWN;
|
1998-02-27 07:54:42 +03:00
|
|
|
if (netrcpass)
|
1999-01-21 01:01:11 +03:00
|
|
|
op = g_strdup (netrcpass);
|
1998-02-27 07:54:42 +03:00
|
|
|
else {
|
1999-12-08 14:39:14 +03:00
|
|
|
if (!strcmp (SUP.user, "anonymous") ||
|
|
|
|
!strcmp (SUP.user, "ftp")) {
|
|
|
|
if (!ftpfs_anonymous_passwd)
|
|
|
|
ftpfs_init_passwd();
|
1999-01-21 01:01:11 +03:00
|
|
|
op = g_strdup (ftpfs_anonymous_passwd);
|
1998-02-27 07:54:42 +03:00
|
|
|
anon = 1;
|
|
|
|
} else {
|
|
|
|
char *p;
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if (!SUP.password){
|
|
|
|
p = g_strconcat (_(" FTP: Password required for "), SUP.user,
|
1998-02-27 07:54:42 +03:00
|
|
|
" ", NULL);
|
1998-05-26 04:53:24 +04:00
|
|
|
op = vfs_get_password (p);
|
1999-01-21 01:01:11 +03:00
|
|
|
g_free (p);
|
1999-01-11 03:48:23 +03:00
|
|
|
if (op == NULL)
|
|
|
|
ERRNOR (EPERM, 0);
|
1999-12-08 14:39:14 +03:00
|
|
|
SUP.password = g_strdup (op);
|
1998-02-27 07:54:42 +03:00
|
|
|
} else
|
1999-12-08 14:39:14 +03:00
|
|
|
op = g_strdup (SUP.password);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
if (!anon || logfile)
|
2000-05-16 18:59:33 +04:00
|
|
|
pass = op;
|
|
|
|
else {
|
1999-01-27 03:49:11 +03:00
|
|
|
pass = g_strconcat ("-", op, NULL);
|
2000-05-16 18:59:33 +04:00
|
|
|
wipe_password (op);
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* Proxy server accepts: username@host-we-want-to-connect*/
|
1999-12-08 14:39:14 +03:00
|
|
|
if (SUP.proxy){
|
1998-02-27 07:54:42 +03:00
|
|
|
#if defined(HSC_PROXY)
|
2000-05-16 18:59:33 +04:00
|
|
|
char *p;
|
1998-02-27 07:54:42 +03:00
|
|
|
int port;
|
2002-07-13 04:22:49 +04:00
|
|
|
char *proxyhost = NULL;
|
2000-05-16 18:59:33 +04:00
|
|
|
|
2002-07-13 04:22:49 +04:00
|
|
|
ftp_split_url (ftpfs_proxy_host, &proxyhost, &proxyname, &port, 0);
|
|
|
|
g_free (proxyhost);
|
1999-03-24 15:16:52 +03:00
|
|
|
p = g_strconcat (_(" Proxy: Password required for "), proxyname, " ",
|
1999-01-11 03:48:23 +03:00
|
|
|
NULL);
|
1998-05-26 04:53:24 +04:00
|
|
|
proxypass = vfs_get_password (p);
|
1999-01-21 01:01:11 +03:00
|
|
|
g_free (p);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (proxypass == NULL) {
|
|
|
|
wipe_password (pass);
|
1999-01-21 01:01:11 +03:00
|
|
|
g_free (proxyname);
|
1998-09-18 15:02:04 +04:00
|
|
|
ERRNOR (EPERM, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
1999-12-08 14:39:14 +03:00
|
|
|
name = g_strdup (SUP.user);
|
1998-02-27 07:54:42 +03:00
|
|
|
#else
|
1999-12-08 14:39:14 +03:00
|
|
|
name = g_strconcat (SUP.user, "@",
|
|
|
|
SUP.host[0] == '!' ? SUP.host+1 : SUP.host, NULL);
|
1998-02-27 07:54:42 +03:00
|
|
|
#endif
|
|
|
|
} else
|
1999-12-08 14:39:14 +03:00
|
|
|
name = g_strdup (SUP.user);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if (get_reply (me, SUP.sock, reply_string, sizeof (reply_string) - 1) == COMPLETE) {
|
1999-01-31 23:28:13 +03:00
|
|
|
g_strup (reply_string);
|
1999-12-08 14:39:14 +03:00
|
|
|
SUP.remote_is_amiga = strstr (reply_string, "AMIGA") != 0;
|
1999-01-31 23:28:13 +03:00
|
|
|
if (logfile) {
|
1999-12-08 14:39:14 +03:00
|
|
|
fprintf (logfile, "MC -- remote_is_amiga = %d\n", SUP.remote_is_amiga);
|
1999-01-31 23:28:13 +03:00
|
|
|
fflush (logfile);
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
#if defined(HSC_PROXY)
|
1999-12-08 14:39:14 +03:00
|
|
|
if (SUP.proxy){
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message (_("ftpfs: sending proxy login name"));
|
1999-12-08 14:39:14 +03:00
|
|
|
if (command (me, super, 1, "USER %s", proxyname) != CONTINUE)
|
1998-10-23 12:26:25 +04:00
|
|
|
goto proxyfail;
|
1999-01-11 03:48:23 +03:00
|
|
|
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message (_("ftpfs: sending proxy user password"));
|
1999-12-08 14:39:14 +03:00
|
|
|
if (command (me, super, 1, "PASS %s", proxypass) != COMPLETE)
|
1998-10-23 12:26:25 +04:00
|
|
|
goto proxyfail;
|
1999-01-11 03:48:23 +03:00
|
|
|
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message (_("ftpfs: proxy authentication succeeded"));
|
1999-12-08 14:39:14 +03:00
|
|
|
if (command (me, super, 1, "SITE %s", SUP.host+1) != COMPLETE)
|
1998-10-23 12:26:25 +04:00
|
|
|
goto proxyfail;
|
1999-01-11 03:48:23 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
print_vfs_message (_("ftpfs: connected to %s"), SUP.host+1);
|
1998-10-23 12:26:25 +04:00
|
|
|
if (0) {
|
|
|
|
proxyfail:
|
1999-12-08 14:39:14 +03:00
|
|
|
SUP.failed_on_login = 1;
|
1998-09-13 14:40:43 +04:00
|
|
|
/* my_errno = E; */
|
1998-02-27 07:54:42 +03:00
|
|
|
if (proxypass)
|
|
|
|
wipe_password (proxypass);
|
|
|
|
wipe_password (pass);
|
1999-01-21 01:01:11 +03:00
|
|
|
g_free (proxyname);
|
|
|
|
g_free (name);
|
1998-10-23 12:26:25 +04:00
|
|
|
ERRNOR (EPERM, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
if (proxypass)
|
|
|
|
wipe_password (proxypass);
|
1999-01-21 01:01:11 +03:00
|
|
|
g_free (proxyname);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
#endif
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message (_("ftpfs: sending login name"));
|
1999-12-08 14:39:14 +03:00
|
|
|
code = command (me, super, WAIT_REPLY, "USER %s", name);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
switch (code){
|
|
|
|
case CONTINUE:
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message (_("ftpfs: sending user password"));
|
1999-12-08 14:39:14 +03:00
|
|
|
if (command (me, super, WAIT_REPLY, "PASS %s", pass) != COMPLETE)
|
1998-02-27 07:54:42 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COMPLETE:
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message (_("ftpfs: logged in"));
|
1998-02-27 07:54:42 +03:00
|
|
|
wipe_password (pass);
|
1999-01-21 01:01:11 +03:00
|
|
|
g_free (name);
|
1998-02-27 07:54:42 +03:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
default:
|
1999-12-08 14:39:14 +03:00
|
|
|
SUP.failed_on_login = 1;
|
1998-09-13 14:40:43 +04:00
|
|
|
/* my_errno = E; */
|
1999-12-08 14:39:14 +03:00
|
|
|
if (SUP.password)
|
|
|
|
wipe_password (SUP.password);
|
|
|
|
SUP.password = 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-10-23 12:26:25 +04:00
|
|
|
goto login_fail;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
}
|
2000-01-24 15:35:52 +03:00
|
|
|
message_2s (1, MSG_ERROR, _("ftpfs: Login incorrect for user %s "), SUP.user);
|
1998-10-23 12:26:25 +04:00
|
|
|
login_fail:
|
1998-02-27 07:54:42 +03:00
|
|
|
wipe_password (pass);
|
1999-01-21 01:01:11 +03:00
|
|
|
g_free (name);
|
1998-10-23 12:26:25 +04:00
|
|
|
ERRNOR (EPERM, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_SETSOCKOPT
|
|
|
|
static void
|
|
|
|
setup_source_route (int socket, int dest)
|
|
|
|
{
|
|
|
|
char buffer [20];
|
|
|
|
char *ptr = buffer;
|
|
|
|
|
|
|
|
if (!source_route)
|
|
|
|
return;
|
2002-07-15 09:31:43 +04:00
|
|
|
memset (buffer, 0, sizeof (buffer));
|
1998-02-27 07:54:42 +03:00
|
|
|
*ptr++ = IPOPT_LSRR;
|
|
|
|
*ptr++ = 3 + 8;
|
|
|
|
*ptr++ = 4; /* pointer */
|
|
|
|
|
|
|
|
/* First hop */
|
2002-07-15 09:31:43 +04:00
|
|
|
memcpy (ptr, (char *) &source_route, sizeof (int));
|
1998-02-27 07:54:42 +03:00
|
|
|
ptr += 4;
|
|
|
|
|
|
|
|
/* Second hop (ie, final destination) */
|
2002-07-15 09:31:43 +04:00
|
|
|
memcpy (ptr, (char *) &dest, sizeof (int));
|
1998-02-27 07:54:42 +03:00
|
|
|
ptr += 4;
|
|
|
|
while ((ptr - buffer) & 3)
|
|
|
|
ptr++;
|
|
|
|
if (setsockopt (socket, IPPROTO_IP, IP_OPTIONS,
|
|
|
|
buffer, ptr - buffer) < 0)
|
1998-05-26 04:53:24 +04:00
|
|
|
message_2s (1, MSG_ERROR, _(" Could not set source routing (%s)"), unix_error_string (errno));
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define setup_source_route(x,y)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static struct no_proxy_entry {
|
|
|
|
char *domain;
|
|
|
|
void *next;
|
|
|
|
} *no_proxy;
|
|
|
|
|
|
|
|
static void
|
2001-06-15 00:08:27 +04:00
|
|
|
load_no_proxy_list (void)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
/* FixMe: shouldn't be hardcoded!!! */
|
1999-01-21 01:01:11 +03:00
|
|
|
char s[BUF_LARGE]; /* provide for BUF_LARGE characters */
|
1998-05-26 04:53:24 +04:00
|
|
|
struct no_proxy_entry *np, *current = 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
FILE *npf;
|
|
|
|
int c;
|
2000-12-04 22:14:52 +03:00
|
|
|
char *p;
|
|
|
|
static char *mc_file;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2000-12-04 22:14:52 +03:00
|
|
|
if (mc_file)
|
1998-02-27 07:54:42 +03:00
|
|
|
return;
|
|
|
|
|
1998-08-27 00:23:10 +04:00
|
|
|
mc_file = concat_dir_and_file (mc_home, "mc.no_proxy");
|
|
|
|
if (exist_file (mc_file) &&
|
|
|
|
(npf = fopen (mc_file, "r"))) {
|
1999-01-21 01:01:11 +03:00
|
|
|
while (fgets (s, sizeof(s), npf) || !(feof (npf) || ferror (npf))) {
|
1998-02-27 07:54:42 +03:00
|
|
|
if (!(p = strchr (s, '\n'))) { /* skip bogus entries */
|
1999-01-21 01:01:11 +03:00
|
|
|
while ((c = fgetc (npf)) != EOF && c != '\n')
|
1998-02-27 07:54:42 +03:00
|
|
|
;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p == s)
|
|
|
|
continue;
|
|
|
|
|
1999-01-31 23:28:13 +03:00
|
|
|
*p = '\0';
|
1999-01-21 01:01:11 +03:00
|
|
|
|
|
|
|
np = g_new (struct no_proxy_entry, 1);
|
|
|
|
np->domain = g_strdup (s);
|
|
|
|
np->next = NULL;
|
1998-02-27 07:54:42 +03:00
|
|
|
if (no_proxy)
|
|
|
|
current->next = np;
|
|
|
|
else
|
|
|
|
no_proxy = np;
|
|
|
|
current = np;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (npf);
|
|
|
|
}
|
1999-01-21 01:01:11 +03:00
|
|
|
g_free (mc_file);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-04-10 16:00:43 +04:00
|
|
|
ftpfs_check_proxy (const char *host)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
struct no_proxy_entry *npe;
|
|
|
|
|
|
|
|
if (!ftpfs_proxy_host || !*ftpfs_proxy_host || !host || !*host)
|
|
|
|
return 0; /* sanity check */
|
|
|
|
|
|
|
|
if (*host == '!')
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!ftpfs_always_use_proxy)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!strchr (host, '.'))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
load_no_proxy_list ();
|
|
|
|
for (npe = no_proxy; npe; npe=npe->next) {
|
|
|
|
char *domain = npe->domain;
|
|
|
|
|
|
|
|
if (domain[0] == '.') {
|
|
|
|
int ld = strlen (domain);
|
|
|
|
int lh = strlen (host);
|
|
|
|
|
|
|
|
while (ld && lh && host[lh - 1] == domain[ld - 1]) {
|
|
|
|
ld--;
|
|
|
|
lh--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ld)
|
|
|
|
return 0;
|
|
|
|
} else
|
1999-01-21 01:01:11 +03:00
|
|
|
if (!g_strcasecmp (host, domain))
|
1998-02-27 07:54:42 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ftpfs_get_proxy_host_and_port (char *proxy, char **host, int *port)
|
|
|
|
{
|
2000-04-10 16:00:43 +04:00
|
|
|
char *user, *dir;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
#if defined(HSC_PROXY)
|
2002-07-12 03:08:30 +04:00
|
|
|
dir = vfs_split_url (proxy, host, &user, port, 0, HSC_PROXY_PORT, URL_ALLOW_ANON);
|
1998-02-27 07:54:42 +03:00
|
|
|
#else
|
2002-07-12 03:08:30 +04:00
|
|
|
dir = vfs_split_url (proxy, host, &user, port, 0, FTP_COMMAND_PORT, URL_ALLOW_ANON);
|
1998-02-27 07:54:42 +03:00
|
|
|
#endif
|
1998-11-21 22:36:01 +03:00
|
|
|
|
2002-07-12 03:08:30 +04:00
|
|
|
if (user)
|
|
|
|
g_free (user);
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
if (dir)
|
1999-01-21 01:01:11 +03:00
|
|
|
g_free (dir);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-12-08 14:39:14 +03:00
|
|
|
ftpfs_open_socket (vfs *me, vfs_s_super *super)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
struct sockaddr_in server_address;
|
|
|
|
struct hostent *hp;
|
|
|
|
int my_socket;
|
|
|
|
char *host;
|
1999-12-08 14:39:14 +03:00
|
|
|
int port = SUP.port;
|
1998-02-27 07:54:42 +03:00
|
|
|
int free_host = 0;
|
|
|
|
|
|
|
|
/* Use a proxy host? */
|
1999-12-08 14:39:14 +03:00
|
|
|
host = SUP.host;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
if (!host || !*host){
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message (_("ftpfs: Invalid host name."));
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = EINVAL;
|
1998-02-27 07:54:42 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Hosts to connect to that start with a ! should use proxy */
|
1999-12-08 14:39:14 +03:00
|
|
|
if (SUP.proxy){
|
1998-02-27 07:54:42 +03:00
|
|
|
ftpfs_get_proxy_host_and_port (ftpfs_proxy_host, &host, &port);
|
|
|
|
free_host = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get host address */
|
2002-07-15 09:31:43 +04:00
|
|
|
memset ((char *) &server_address, 0, sizeof (server_address));
|
1998-02-27 07:54:42 +03:00
|
|
|
server_address.sin_family = AF_INET;
|
|
|
|
server_address.sin_addr.s_addr = inet_addr (host);
|
|
|
|
if (server_address.sin_addr.s_addr != -1)
|
|
|
|
server_address.sin_family = AF_INET;
|
|
|
|
else {
|
1999-01-11 03:48:23 +03:00
|
|
|
hp = gethostbyname (host);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (hp == NULL){
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message (_("ftpfs: Invalid host address."));
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = EINVAL;
|
1998-02-27 07:54:42 +03:00
|
|
|
if (free_host)
|
1999-01-21 01:01:11 +03:00
|
|
|
g_free (host);
|
1998-02-27 07:54:42 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
server_address.sin_family = hp->h_addrtype;
|
1998-08-07 20:47:44 +04:00
|
|
|
|
|
|
|
/* We copy only 4 bytes, we can not trust hp->h_length, as it comes from the DNS */
|
2002-07-15 09:31:43 +04:00
|
|
|
memcpy ((char *) &server_address.sin_addr, (char *) hp->h_addr, 4);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
server_address.sin_port = htons (port);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* Connect */
|
|
|
|
if ((my_socket = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = errno;
|
1998-02-27 07:54:42 +03:00
|
|
|
if (free_host)
|
1999-01-21 01:01:11 +03:00
|
|
|
g_free (host);
|
1998-02-27 07:54:42 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
setup_source_route (my_socket, server_address.sin_addr.s_addr);
|
|
|
|
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message (_("ftpfs: making connection to %s"), host);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (free_host)
|
1999-01-21 01:01:11 +03:00
|
|
|
g_free (host);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-01-11 03:48:23 +03:00
|
|
|
enable_interrupt_key (); /* clear the interrupt flag */
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
if (connect (my_socket, (struct sockaddr *) &server_address,
|
|
|
|
sizeof (server_address)) < 0){
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = errno;
|
1999-01-11 03:48:23 +03:00
|
|
|
if (errno == EINTR && got_interrupt ())
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message (_("ftpfs: connection interrupted by user"));
|
1998-02-27 07:54:42 +03:00
|
|
|
else
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message (_("ftpfs: connection to server failed: %s"),
|
1999-01-11 03:48:23 +03:00
|
|
|
unix_error_string(errno));
|
1998-02-27 07:54:42 +03:00
|
|
|
disable_interrupt_key();
|
|
|
|
close (my_socket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
disable_interrupt_key();
|
|
|
|
return my_socket;
|
|
|
|
}
|
|
|
|
|
2001-06-15 00:08:27 +04:00
|
|
|
static int
|
1999-12-08 14:39:14 +03:00
|
|
|
open_archive_int (vfs *me, vfs_s_super *super)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
int retry_seconds, count_down;
|
|
|
|
|
|
|
|
/* We do not want to use the passive if we are using proxies */
|
2002-07-12 04:05:11 +04:00
|
|
|
if (SUP.proxy)
|
1999-12-08 14:39:14 +03:00
|
|
|
SUP.use_passive_connection = 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
retry_seconds = 0;
|
|
|
|
do {
|
1999-12-08 14:39:14 +03:00
|
|
|
SUP.failed_on_login = 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
SUP.sock = ftpfs_open_socket (me, super);
|
|
|
|
if (SUP.sock == -1)
|
|
|
|
return -1;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2000-05-19 17:09:41 +04:00
|
|
|
if (login_server (me, super, NULL)) {
|
1998-02-27 07:54:42 +03:00
|
|
|
/* Logged in, no need to retry the connection */
|
|
|
|
break;
|
|
|
|
} else {
|
1999-12-08 14:39:14 +03:00
|
|
|
if (SUP.failed_on_login){
|
1998-02-27 07:54:42 +03:00
|
|
|
/* Close only the socket descriptor */
|
1999-12-08 14:39:14 +03:00
|
|
|
close (SUP.sock);
|
1998-02-27 07:54:42 +03:00
|
|
|
} else {
|
1999-12-08 14:39:14 +03:00
|
|
|
return -1;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
if (ftpfs_retry_seconds){
|
|
|
|
retry_seconds = ftpfs_retry_seconds;
|
|
|
|
enable_interrupt_key ();
|
|
|
|
for (count_down = retry_seconds; count_down; count_down--){
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message (_("Waiting to retry... %d (Control-C to cancel)"), count_down);
|
1998-02-27 07:54:42 +03:00
|
|
|
sleep (1);
|
|
|
|
if (got_interrupt ()){
|
1998-09-13 14:40:43 +04:00
|
|
|
/* my_errno = E; */
|
1998-02-27 07:54:42 +03:00
|
|
|
disable_interrupt_key ();
|
1999-12-08 14:39:14 +03:00
|
|
|
return 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
disable_interrupt_key ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (retry_seconds);
|
|
|
|
|
2002-07-19 20:36:32 +04:00
|
|
|
SUP.cwdir = ftpfs_get_current_directory (me, super);
|
|
|
|
if (!SUP.cwdir)
|
|
|
|
SUP.cwdir = g_strdup (PATH_SEP_STR);
|
1999-12-08 14:39:14 +03:00
|
|
|
return 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-12-08 14:39:14 +03:00
|
|
|
open_archive (vfs *me, vfs_s_super *super, char *archive_name, char *op)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1999-12-08 14:39:14 +03:00
|
|
|
char *host, *user, *password;
|
|
|
|
int port;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2002-07-12 03:08:30 +04:00
|
|
|
ftp_split_url (strchr (op, ':') + 1, &host, &user, &port, &password);
|
2000-04-10 16:00:43 +04:00
|
|
|
|
|
|
|
SUP.host = host;
|
|
|
|
SUP.user = user;
|
1999-12-08 14:39:14 +03:00
|
|
|
SUP.port = port;
|
2002-07-19 20:36:32 +04:00
|
|
|
SUP.cwdir = NULL;
|
2002-07-12 04:05:11 +04:00
|
|
|
SUP.proxy = 0;
|
|
|
|
if (ftpfs_check_proxy (host))
|
|
|
|
SUP.proxy = ftpfs_proxy_host;
|
2002-07-12 03:08:30 +04:00
|
|
|
SUP.password = password;
|
1999-12-08 14:39:14 +03:00
|
|
|
SUP.use_passive_connection = ftpfs_use_passive_connections | source_route;
|
|
|
|
SUP.use_source_route = source_route;
|
2002-07-12 03:08:30 +04:00
|
|
|
SUP.strict = ftpfs_use_unix_list_options ? RFC_AUTODETECT : RFC_STRICT;
|
1999-12-08 14:39:14 +03:00
|
|
|
SUP.isbinary = TYPE_UNKNOWN;
|
|
|
|
SUP.remote_is_amiga = 0;
|
|
|
|
super->name = g_strdup("/");
|
|
|
|
super->root = vfs_s_new_inode (me, super, vfs_s_default_stat(me, S_IFDIR | 0755));
|
2002-07-11 01:26:55 +04:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
return open_archive_int (me, super);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
archive_same(vfs *me, vfs_s_super *super, char *archive_name, char *op, void *cookie)
|
|
|
|
{
|
2000-04-10 16:00:43 +04:00
|
|
|
char *host, *user;
|
1999-12-08 14:39:14 +03:00
|
|
|
int port;
|
2000-04-10 16:00:43 +04:00
|
|
|
|
2002-07-12 03:08:30 +04:00
|
|
|
ftp_split_url (strchr(op, ':') + 1, &host, &user, &port, 0);
|
2002-07-11 01:26:55 +04:00
|
|
|
|
2000-04-10 16:00:43 +04:00
|
|
|
port = ((strcmp (host, SUP.host) == 0) &&
|
1999-12-08 14:39:14 +03:00
|
|
|
(strcmp (user, SUP.user) == 0) &&
|
|
|
|
(port == SUP.port));
|
2000-04-10 16:00:43 +04:00
|
|
|
|
|
|
|
g_free (host);
|
|
|
|
g_free (user);
|
|
|
|
|
|
|
|
return port;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2002-02-07 22:22:20 +03:00
|
|
|
void
|
|
|
|
ftpfs_flushdir (void)
|
|
|
|
{
|
|
|
|
force_expiration = 1;
|
|
|
|
}
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
static int
|
|
|
|
dir_uptodate(vfs *me, vfs_s_inode *ino)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1999-12-08 14:39:14 +03:00
|
|
|
struct timeval tim;
|
|
|
|
|
|
|
|
if (force_expiration) {
|
|
|
|
force_expiration = 0;
|
|
|
|
return 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2002-05-17 17:32:02 +04:00
|
|
|
gettimeofday(&tim, NULL);
|
1999-12-08 14:39:14 +03:00
|
|
|
if (tim.tv_sec < ino->u.ftp.timestamp.tv_sec)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The returned directory should always contain a trailing slash */
|
1999-01-11 03:48:23 +03:00
|
|
|
static char *
|
1999-12-08 14:39:14 +03:00
|
|
|
ftpfs_get_current_directory (vfs *me, vfs_s_super *super)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2000-04-14 05:14:26 +04:00
|
|
|
char buf[BUF_8K], *bufp, *bufq;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if (command (me, super, NONE, "PWD") == COMPLETE &&
|
|
|
|
get_reply(me, SUP.sock, buf, sizeof(buf)) == COMPLETE) {
|
1998-02-27 07:54:42 +03:00
|
|
|
bufp = NULL;
|
|
|
|
for (bufq = buf; *bufq; bufq++)
|
|
|
|
if (*bufq == '"') {
|
|
|
|
if (!bufp) {
|
|
|
|
bufp = bufq + 1;
|
|
|
|
} else {
|
|
|
|
*bufq = 0;
|
|
|
|
if (*bufp) {
|
|
|
|
if (*(bufq - 1) != '/') {
|
|
|
|
*bufq++ = '/';
|
|
|
|
*bufq = 0;
|
|
|
|
}
|
1999-01-31 23:28:13 +03:00
|
|
|
if (*bufp == '/')
|
|
|
|
return g_strdup (bufp);
|
|
|
|
else {
|
|
|
|
/* If the remote server is an Amiga a leading slash
|
|
|
|
might be missing. MC needs it because it is used
|
|
|
|
as seperator between hostname and path internally. */
|
|
|
|
return g_strconcat( "/", bufp, 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
my_errno = EIO;
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = EIO;
|
1998-02-27 07:54:42 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Setup Passive ftp connection, we use it for source routed connections */
|
|
|
|
static int
|
1999-12-08 14:39:14 +03:00
|
|
|
setup_passive (vfs *me, vfs_s_super *super, int my_socket, struct sockaddr_in *sa)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
int xa, xb, xc, xd, xe, xf;
|
|
|
|
char n [6];
|
|
|
|
char *c = reply_str;
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if (command (me, super, WAIT_REPLY | WANT_STRING, "PASV") != COMPLETE)
|
1998-02-27 07:54:42 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Parse remote parameters */
|
2002-07-03 23:09:03 +04:00
|
|
|
for (c = reply_str + 4; (*c) && (!isdigit ((unsigned char) *c)); c++)
|
1998-02-27 07:54:42 +03:00
|
|
|
;
|
|
|
|
if (!*c)
|
|
|
|
return 0;
|
2002-07-03 23:09:03 +04:00
|
|
|
if (!isdigit ((unsigned char) *c))
|
1998-02-27 07:54:42 +03:00
|
|
|
return 0;
|
|
|
|
if (sscanf (c, "%d,%d,%d,%d,%d,%d", &xa, &xb, &xc, &xd, &xe, &xf) != 6)
|
|
|
|
return 0;
|
|
|
|
n [0] = (unsigned char) xa;
|
|
|
|
n [1] = (unsigned char) xb;
|
|
|
|
n [2] = (unsigned char) xc;
|
|
|
|
n [3] = (unsigned char) xd;
|
|
|
|
n [4] = (unsigned char) xe;
|
|
|
|
n [5] = (unsigned char) xf;
|
|
|
|
|
2002-07-15 09:31:43 +04:00
|
|
|
memcpy (&(sa->sin_addr.s_addr), (void *)n, 4);
|
|
|
|
memcpy (&(sa->sin_port), (void *)&n[4], 2);
|
1998-02-27 07:54:42 +03:00
|
|
|
setup_source_route (my_socket, sa->sin_addr.s_addr);
|
|
|
|
if (connect (my_socket, (struct sockaddr *) sa, sizeof (struct sockaddr_in)) < 0)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-12-08 14:39:14 +03:00
|
|
|
initconn (vfs *me, vfs_s_super *super)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
struct sockaddr_in data_addr;
|
|
|
|
int data, len = sizeof(data_addr);
|
|
|
|
struct protoent *pe;
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
getsockname(SUP.sock, (struct sockaddr *) &data_addr, &len);
|
1998-02-27 07:54:42 +03:00
|
|
|
data_addr.sin_port = 0;
|
|
|
|
|
|
|
|
pe = getprotobyname("tcp");
|
1999-01-11 03:48:23 +03:00
|
|
|
if (pe == NULL)
|
|
|
|
ERRNOR (EIO, -1);
|
1998-02-27 07:54:42 +03:00
|
|
|
data = socket (AF_INET, SOCK_STREAM, pe->p_proto);
|
1999-01-11 03:48:23 +03:00
|
|
|
if (data < 0)
|
|
|
|
ERRNOR (EIO, -1);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if (SUP.use_passive_connection){
|
|
|
|
if ((SUP.use_passive_connection = setup_passive (me, super, data, &data_addr)))
|
1998-02-27 07:54:42 +03:00
|
|
|
return data;
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
SUP.use_source_route = 0;
|
|
|
|
SUP.use_passive_connection = 0;
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message (_("ftpfs: could not setup passive mode"));
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
1999-12-08 14:39:14 +03:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
/* If passive setup fails, fallback to active connections */
|
|
|
|
/* Active FTP connection */
|
2002-02-01 15:21:55 +03:00
|
|
|
if ((bind (data, (struct sockaddr *)&data_addr, len) == 0) &&
|
2002-02-07 22:22:20 +03:00
|
|
|
(getsockname (data, (struct sockaddr *) &data_addr, &len) == 0) &&
|
2002-02-01 15:21:55 +03:00
|
|
|
(listen (data, 1) == 0))
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
unsigned char *a = (unsigned char *)&data_addr.sin_addr;
|
|
|
|
unsigned char *p = (unsigned char *)&data_addr.sin_port;
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if (command (me, super, WAIT_REPLY, "PORT %d,%d,%d,%d,%d,%d", a[0], a[1],
|
2002-02-01 15:21:55 +03:00
|
|
|
a[2], a[3], p[0], p[1]) == COMPLETE)
|
|
|
|
return data;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
close(data);
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = EIO;
|
1998-02-27 07:54:42 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-12-08 14:39:14 +03:00
|
|
|
open_data_connection (vfs *me, vfs_s_super *super, char *cmd, char *remote,
|
1998-10-13 02:07:53 +04:00
|
|
|
int isbinary, int reget)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
struct sockaddr_in from;
|
|
|
|
int s, j, data, fromlen = sizeof(from);
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if ((s = initconn (me, super)) == -1)
|
1998-02-27 07:54:42 +03:00
|
|
|
return -1;
|
1999-12-08 14:39:14 +03:00
|
|
|
if (changetype (me, super, isbinary) == -1)
|
1998-02-27 07:54:42 +03:00
|
|
|
return -1;
|
1998-10-13 02:07:53 +04:00
|
|
|
if (reget > 0){
|
1999-12-08 14:39:14 +03:00
|
|
|
j = command (me, super, WAIT_REPLY, "REST %d", reget);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (j != CONTINUE)
|
|
|
|
return -1;
|
|
|
|
}
|
1999-12-16 15:55:16 +03:00
|
|
|
if (remote) {
|
|
|
|
char * remote_path = translate_path (me, super, remote);
|
2000-10-16 22:37:54 +04:00
|
|
|
j = command (me, super, WAIT_REPLY, "%s /%s", cmd,
|
|
|
|
/* WarFtpD can't STORE //filename */
|
|
|
|
(*remote_path == '/') ? remote_path + 1 : remote_path);
|
1999-12-16 15:55:16 +03:00
|
|
|
g_free (remote_path);
|
|
|
|
} else
|
1999-12-08 14:39:14 +03:00
|
|
|
j = command (me, super, WAIT_REPLY, "%s", cmd);
|
1999-01-11 03:48:23 +03:00
|
|
|
if (j != PRELIM)
|
2000-01-13 02:45:51 +03:00
|
|
|
ERRNOR (EPERM, -1);
|
1998-02-27 07:54:42 +03:00
|
|
|
enable_interrupt_key();
|
1999-12-08 14:39:14 +03:00
|
|
|
if (SUP.use_passive_connection)
|
1998-02-27 07:54:42 +03:00
|
|
|
data = s;
|
|
|
|
else {
|
|
|
|
data = accept (s, (struct sockaddr *)&from, &fromlen);
|
2000-10-16 22:37:54 +04:00
|
|
|
close(s);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (data < 0) {
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = errno;
|
1998-02-27 07:54:42 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
disable_interrupt_key();
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-12-08 14:39:14 +03:00
|
|
|
linear_abort (vfs *me, vfs_s_fh *fh)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1999-12-08 14:39:14 +03:00
|
|
|
vfs_s_super *super = FH_SUPER;
|
2002-02-01 15:21:55 +03:00
|
|
|
static unsigned char const ipbuf[3] = { IAC, IP, IAC };
|
1998-02-27 07:54:42 +03:00
|
|
|
fd_set mask;
|
|
|
|
char buf[1024];
|
1999-12-08 14:39:14 +03:00
|
|
|
int dsock = FH_SOCK;
|
|
|
|
FH_SOCK = -1;
|
2002-05-17 17:32:02 +04:00
|
|
|
SUP.control_connection_buzy = 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message(_("ftpfs: aborting transfer."));
|
1999-12-08 14:39:14 +03:00
|
|
|
if (send(SUP.sock, ipbuf, sizeof(ipbuf), MSG_OOB) != sizeof(ipbuf)) {
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message(_("ftpfs: abort error: %s"), unix_error_string(errno));
|
1998-02-27 07:54:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if (command(me, super, NONE, "%cABOR", DM) != COMPLETE){
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message (_("ftpfs: abort failed"));
|
1998-02-27 07:54:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (dsock != -1) {
|
|
|
|
FD_ZERO(&mask);
|
|
|
|
FD_SET(dsock, &mask);
|
|
|
|
if (select(dsock + 1, &mask, NULL, NULL, NULL) > 0)
|
|
|
|
while (read(dsock, buf, sizeof(buf)) > 0);
|
|
|
|
}
|
1999-12-08 14:39:14 +03:00
|
|
|
if ((get_reply(me, SUP.sock, NULL, 0) == TRANSIENT) && (code == 426))
|
|
|
|
get_reply(me, SUP.sock, NULL, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
#if 0
|
1998-02-27 07:54:42 +03:00
|
|
|
static void
|
1999-12-08 14:39:14 +03:00
|
|
|
resolve_symlink_without_ls_options(vfs *me, vfs_s_super *super, vfs_s_inode *dir)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
struct linklist *flist;
|
1998-09-15 23:41:22 +04:00
|
|
|
struct direntry *fe, *fel;
|
|
|
|
char tmp[MC_MAXPATHLEN];
|
1999-08-30 10:01:37 +04:00
|
|
|
int depth;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-10-23 00:17:49 +04:00
|
|
|
dir->symlink_status = FTPFS_RESOLVING_SYMLINKS;
|
1998-09-15 23:41:22 +04:00
|
|
|
for (flist = dir->file_list->next; flist != dir->file_list; flist = flist->next) {
|
|
|
|
/* flist->data->l_stat is alread initialized with 0 */
|
|
|
|
fel = flist->data;
|
1999-08-30 10:01:37 +04:00
|
|
|
if (S_ISLNK(fel->s.st_mode) && fel->linkname) {
|
1998-09-15 23:41:22 +04:00
|
|
|
if (fel->linkname[0] == '/') {
|
|
|
|
if (strlen (fel->linkname) >= MC_MAXPATHLEN)
|
|
|
|
continue;
|
|
|
|
strcpy (tmp, fel->linkname);
|
|
|
|
} else {
|
|
|
|
if ((strlen (dir->remote_path) + strlen (fel->linkname)) >= MC_MAXPATHLEN)
|
|
|
|
continue;
|
|
|
|
strcpy (tmp, dir->remote_path);
|
|
|
|
if (tmp[1] != '\0')
|
|
|
|
strcat (tmp, "/");
|
|
|
|
strcat (tmp + 1, fel->linkname);
|
1998-05-10 17:41:43 +04:00
|
|
|
}
|
1999-08-30 10:01:37 +04:00
|
|
|
for ( depth = 0; depth < 100; depth++) { /* depth protects against recursive symbolic links */
|
1998-10-03 16:30:03 +04:00
|
|
|
canonicalize_pathname (tmp);
|
1998-09-15 23:41:22 +04:00
|
|
|
fe = _get_file_entry(bucket, tmp, 0, 0);
|
|
|
|
if (fe) {
|
|
|
|
if (S_ISLNK (fe->s.st_mode) && fe->l_stat == 0) {
|
|
|
|
/* Symlink points to link which isn't resolved, yet. */
|
|
|
|
if (fe->linkname[0] == '/') {
|
|
|
|
if (strlen (fe->linkname) >= MC_MAXPATHLEN)
|
|
|
|
break;
|
|
|
|
strcpy (tmp, fe->linkname);
|
|
|
|
} else {
|
|
|
|
/* at this point tmp looks always like this
|
|
|
|
/directory/filename, i.e. no need to check
|
|
|
|
strrchr's return value */
|
|
|
|
*(strrchr (tmp, '/') + 1) = '\0'; /* dirname */
|
|
|
|
if ((strlen (tmp) + strlen (fe->linkname)) >= MC_MAXPATHLEN)
|
|
|
|
break;
|
|
|
|
strcat (tmp, fe->linkname);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else {
|
1999-01-21 01:01:11 +03:00
|
|
|
fel->l_stat = g_new (struct stat, 1);
|
1998-09-15 23:41:22 +04:00
|
|
|
if ( S_ISLNK (fe->s.st_mode))
|
|
|
|
*fel->l_stat = *fe->l_stat;
|
|
|
|
else
|
|
|
|
*fel->l_stat = fe->s;
|
|
|
|
(*fel->l_stat).st_ino = bucket->__inode_counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
1998-09-15 23:41:22 +04:00
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
1998-10-23 00:17:49 +04:00
|
|
|
dir->symlink_status = FTPFS_RESOLVED_SYMLINKS;
|
1998-11-24 00:13:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-12-08 14:39:14 +03:00
|
|
|
resolve_symlink_with_ls_options(vfs *me, vfs_s_super *super, vfs_s_inode *dir)
|
1998-11-24 00:13:08 +03:00
|
|
|
{
|
|
|
|
char buffer[2048] = "", *filename;
|
|
|
|
int sock;
|
|
|
|
FILE *fp;
|
|
|
|
struct stat s;
|
|
|
|
struct linklist *flist;
|
|
|
|
struct direntry *fe;
|
1999-06-19 02:25:24 +04:00
|
|
|
int switch_method = 0;
|
1998-11-24 00:13:08 +03:00
|
|
|
|
|
|
|
dir->symlink_status = FTPFS_RESOLVED_SYMLINKS;
|
|
|
|
if (strchr (dir->remote_path, ' ')) {
|
|
|
|
if (ftpfs_chdir_internal (bucket, dir->remote_path) != COMPLETE) {
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message(_("ftpfs: CWD failed."));
|
1998-11-24 00:13:08 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
sock = open_data_connection (bucket, "LIST -lLa", ".", TYPE_ASCII, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sock = open_data_connection (bucket, "LIST -lLa",
|
|
|
|
dir->remote_path, TYPE_ASCII, 0);
|
1998-10-23 00:17:49 +04:00
|
|
|
|
1998-11-24 00:13:08 +03:00
|
|
|
if (sock == -1) {
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message(_("ftpfs: couldn't resolve symlink"));
|
1998-11-24 00:13:08 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fdopen(sock, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
close(sock);
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message(_("ftpfs: couldn't resolve symlink"));
|
1998-11-24 00:13:08 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
enable_interrupt_key();
|
|
|
|
flist = dir->file_list->next;
|
|
|
|
while (1) {
|
|
|
|
do {
|
|
|
|
if (flist == dir->file_list)
|
|
|
|
goto done;
|
|
|
|
fe = flist->data;
|
|
|
|
flist = flist->next;
|
|
|
|
} while (!S_ISLNK(fe->s.st_mode));
|
|
|
|
while (1) {
|
|
|
|
if (fgets (buffer, sizeof (buffer), fp) == NULL)
|
|
|
|
goto done;
|
|
|
|
if (logfile){
|
|
|
|
fputs (buffer, logfile);
|
|
|
|
fflush (logfile);
|
|
|
|
}
|
1999-12-08 14:39:14 +03:00
|
|
|
vfs_die("This code should be commented out\n");
|
1998-11-24 00:13:08 +03:00
|
|
|
if (vfs_parse_ls_lga (buffer, &s, &filename, NULL)) {
|
|
|
|
int r = strcmp(fe->name, filename);
|
1999-01-21 01:01:11 +03:00
|
|
|
g_free(filename);
|
1998-11-24 00:13:08 +03:00
|
|
|
if (r == 0) {
|
1999-06-19 02:25:24 +04:00
|
|
|
if (S_ISLNK (s.st_mode)) {
|
|
|
|
/* This server doesn't understand LIST -lLa */
|
|
|
|
switch_method = 1;
|
|
|
|
goto done;
|
|
|
|
}
|
1999-01-21 01:01:11 +03:00
|
|
|
fe->l_stat = g_new (struct stat, 1);
|
1998-11-24 00:13:08 +03:00
|
|
|
if (fe->l_stat == NULL)
|
|
|
|
goto done;
|
|
|
|
*fe->l_stat = s;
|
|
|
|
(*fe->l_stat).st_ino = bucket->__inode_counter++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (r < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
while (fgets(buffer, sizeof(buffer), fp) != NULL);
|
|
|
|
disable_interrupt_key();
|
|
|
|
fclose(fp);
|
1999-12-08 14:39:14 +03:00
|
|
|
get_reply(me, SUP.sock, NULL, 0);
|
1998-11-24 00:13:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-12-08 14:39:14 +03:00
|
|
|
resolve_symlink(vfs *me, vfs_s_super *super, vfs_s_inode *dir)
|
1998-11-24 00:13:08 +03:00
|
|
|
{
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message(_("Resolving symlink..."));
|
1998-11-24 00:13:08 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if (SUP.strict_rfc959_list_cmd)
|
|
|
|
resolve_symlink_without_ls_options(me, super, dir);
|
1998-11-24 00:13:08 +03:00
|
|
|
else
|
1999-12-08 14:39:14 +03:00
|
|
|
resolve_symlink_with_ls_options(me, super, dir);
|
1999-01-31 23:28:13 +03:00
|
|
|
}
|
1999-12-08 14:39:14 +03:00
|
|
|
#endif
|
1998-09-18 15:02:04 +04:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
static int
|
|
|
|
dir_load(vfs *me, vfs_s_inode *dir, char *remote_path)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1999-12-08 14:39:14 +03:00
|
|
|
vfs_s_entry *ent;
|
|
|
|
vfs_s_super *super = dir->super;
|
2001-05-15 03:34:43 +04:00
|
|
|
int sock, num_entries = 0;
|
|
|
|
#ifdef FIXME_LATER
|
|
|
|
int has_symlinks = 0;
|
|
|
|
#endif
|
2000-04-14 05:14:26 +04:00
|
|
|
char buffer[BUF_8K];
|
2002-07-19 20:36:32 +04:00
|
|
|
int cd_first;
|
2000-04-14 05:14:26 +04:00
|
|
|
|
2002-07-19 20:36:32 +04:00
|
|
|
cd_first = ftpfs_first_cd_then_ls || (strchr (remote_path, ' ') != NULL)
|
|
|
|
|| (SUP.strict == RFC_STRICT);
|
|
|
|
|
2002-01-29 11:09:20 +03:00
|
|
|
again:
|
2000-12-04 22:14:52 +03:00
|
|
|
print_vfs_message(_("ftpfs: Reading FTP directory %s... %s%s"), remote_path,
|
|
|
|
SUP.strict == RFC_STRICT ? _("(strict rfc959)") : "",
|
|
|
|
cd_first ? _("(chdir first)") : "");
|
1999-12-08 14:39:14 +03:00
|
|
|
|
|
|
|
if (cd_first) {
|
|
|
|
char *p;
|
2000-04-14 05:14:26 +04:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
p = translate_path (me, super, remote_path);
|
2000-04-14 05:14:26 +04:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if (ftpfs_chdir_internal (me, super, p) != COMPLETE) {
|
1999-12-16 15:55:16 +03:00
|
|
|
g_free (p);
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = ENOENT;
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message(_("ftpfs: CWD failed."));
|
1999-12-08 14:39:14 +03:00
|
|
|
return -1;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
1999-12-16 15:55:16 +03:00
|
|
|
g_free (p);
|
1999-01-31 23:28:13 +03:00
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
gettimeofday(&dir->u.ftp.timestamp, NULL);
|
2000-02-23 10:43:14 +03:00
|
|
|
dir->u.ftp.timestamp.tv_sec += ftpfs_directory_timeout;
|
1999-12-08 14:39:14 +03:00
|
|
|
|
|
|
|
if (SUP.strict == RFC_STRICT)
|
|
|
|
sock = open_data_connection (me, super, "LIST", 0, TYPE_ASCII, 0);
|
|
|
|
else if (cd_first)
|
2000-04-14 05:14:26 +04:00
|
|
|
/* Dirty hack to avoid autoprepending / to . */
|
2002-02-06 15:28:08 +03:00
|
|
|
/* Wu-ftpd produces strange output for '/' if 'LIST -la .' used */
|
|
|
|
sock = open_data_connection (me, super, "LIST -la", 0, TYPE_ASCII, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
else {
|
2002-08-21 12:22:33 +04:00
|
|
|
/* Trailing "/." is necessary if remote_path is a symlink */
|
|
|
|
char *path = concat_dir_and_file (remote_path, ".");
|
2002-02-06 15:28:08 +03:00
|
|
|
sock = open_data_connection (me, super, "LIST -la", path, TYPE_ASCII, 0);
|
2002-08-21 12:22:33 +04:00
|
|
|
g_free (path);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sock == -1)
|
1998-10-28 19:39:53 +03:00
|
|
|
goto fallback;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* Clear the interrupt flag */
|
|
|
|
enable_interrupt_key ();
|
2000-10-31 18:43:49 +03:00
|
|
|
|
|
|
|
#if 1
|
|
|
|
{
|
|
|
|
/* added 20001006 by gisburn
|
|
|
|
* add dots '.' and '..'. This must be _executed_ before scanning the dir as the
|
|
|
|
* code below may jump directly into error handling code (without executing
|
|
|
|
* remaining code). And C doesn't have try {...} finally {}; :-)
|
|
|
|
*/
|
|
|
|
vfs_s_inode *parent = dir->ent->dir;
|
|
|
|
|
|
|
|
if( parent==NULL )
|
|
|
|
parent = dir;
|
|
|
|
|
|
|
|
ent = vfs_s_generate_entry(me, ".", dir, 0);
|
|
|
|
ent->ino->st=dir->st;
|
|
|
|
num_entries++;
|
|
|
|
vfs_s_insert_entry(me, dir, ent);
|
|
|
|
|
|
|
|
ent = vfs_s_generate_entry(me, "..", parent, 0);
|
|
|
|
ent->ino->st=parent->st;
|
|
|
|
num_entries++;
|
|
|
|
vfs_s_insert_entry(me, dir, ent);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
while (1) {
|
2000-10-16 22:37:54 +04:00
|
|
|
int i;
|
1999-12-08 14:39:14 +03:00
|
|
|
int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), sock);
|
|
|
|
if (!res)
|
1998-02-27 07:54:42 +03:00
|
|
|
break;
|
2002-02-06 15:28:08 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if (res == EINTR) {
|
|
|
|
me->verrno = ECONNRESET;
|
2002-02-06 15:28:08 +03:00
|
|
|
close (sock);
|
|
|
|
disable_interrupt_key();
|
|
|
|
get_reply(me, SUP.sock, NULL, 0);
|
2002-07-19 08:29:37 +04:00
|
|
|
print_vfs_message (_("%s: failure"), me->name);
|
2002-02-06 15:28:08 +03:00
|
|
|
return -1;
|
1999-12-08 14:39:14 +03:00
|
|
|
}
|
2002-02-06 15:28:08 +03:00
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
if (logfile){
|
|
|
|
fputs (buffer, logfile);
|
|
|
|
fputs ("\n", logfile);
|
|
|
|
fflush (logfile);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
1999-12-08 14:39:14 +03:00
|
|
|
|
|
|
|
ent = vfs_s_generate_entry(me, NULL, dir, 0);
|
|
|
|
i = ent->ino->st.st_nlink;
|
2000-10-16 22:37:54 +04:00
|
|
|
if (!vfs_parse_ls_lga (buffer, &ent->ino->st, &ent->name, &ent->ino->linkname)) {
|
1999-12-08 14:39:14 +03:00
|
|
|
vfs_s_free_entry (me, ent);
|
|
|
|
continue;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2000-10-16 22:37:54 +04:00
|
|
|
ent->ino->st.st_nlink = i; /* Ouch, we need to preserve our counts :-( */
|
1999-12-08 14:39:14 +03:00
|
|
|
num_entries++;
|
|
|
|
if ((!strcmp(ent->name, ".")) || (!strcmp (ent->name, ".."))) {
|
2000-04-28 11:43:13 +04:00
|
|
|
g_free (ent->name);
|
1999-12-08 14:39:14 +03:00
|
|
|
ent->name = NULL; /* Ouch, vfs_s_free_entry "knows" about . and .. being special :-( */
|
|
|
|
vfs_s_free_entry (me, ent);
|
|
|
|
continue;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
1999-12-08 14:39:14 +03:00
|
|
|
|
|
|
|
vfs_s_insert_entry(me, dir, ent);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
1999-12-08 14:39:14 +03:00
|
|
|
|
2000-08-08 19:12:39 +04:00
|
|
|
/* vfs_s_add_dots(me, dir, NULL);
|
|
|
|
FIXME This really should be here; but we need to provide correct parent.
|
|
|
|
Disabled for now, please fix it. Pavel
|
|
|
|
*/
|
1999-12-08 14:39:14 +03:00
|
|
|
close(sock);
|
|
|
|
me->verrno = E_REMOTE;
|
|
|
|
if ((get_reply (me, SUP.sock, NULL, 0) != COMPLETE) || !num_entries)
|
1998-10-28 19:39:53 +03:00
|
|
|
goto fallback;
|
1999-01-31 23:28:13 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if (SUP.strict == RFC_AUTODETECT)
|
|
|
|
SUP.strict = RFC_DARING;
|
1999-01-31 23:28:13 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
#ifdef FIXME_LATER
|
1998-09-15 23:41:22 +04:00
|
|
|
if (has_symlinks) {
|
|
|
|
if (resolve_symlinks)
|
1999-12-08 14:39:14 +03:00
|
|
|
resolve_symlink(me, super, dcache);
|
1998-09-15 23:41:22 +04:00
|
|
|
else
|
|
|
|
dcache->symlink_status = FTPFS_UNRESOLVED_SYMLINKS;
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
#endif
|
2002-07-19 08:29:37 +04:00
|
|
|
print_vfs_message (_("%s: done."), me->name);
|
1999-12-08 14:39:14 +03:00
|
|
|
return 0;
|
|
|
|
|
1998-10-28 19:39:53 +03:00
|
|
|
fallback:
|
1999-12-08 14:39:14 +03:00
|
|
|
if (SUP.strict == RFC_AUTODETECT) {
|
1998-10-28 19:39:53 +03:00
|
|
|
/* It's our first attempt to get a directory listing from this
|
|
|
|
server (UNIX style LIST command) */
|
2002-01-29 11:09:20 +03:00
|
|
|
SUP.strict = RFC_STRICT;
|
|
|
|
/* I hate goto, but recursive call needs another 8K on stack */
|
|
|
|
/* return dir_load (me, dir, remote_path); */
|
|
|
|
cd_first = 1;
|
|
|
|
goto again;
|
1998-10-28 19:39:53 +03:00
|
|
|
}
|
1999-03-24 15:16:52 +03:00
|
|
|
print_vfs_message(_("ftpfs: failed; nowhere to fallback to"));
|
1999-12-08 14:39:14 +03:00
|
|
|
ERRNOR(-1, EACCES);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-05-22 21:19:24 +04:00
|
|
|
file_store(vfs *me, vfs_s_fh *fh, char *name, char *localname)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2002-02-08 12:42:38 +03:00
|
|
|
int h, sock, n;
|
|
|
|
off_t total;
|
1998-02-27 07:54:42 +03:00
|
|
|
#ifdef HAVE_STRUCT_LINGER
|
|
|
|
struct linger li;
|
|
|
|
#else
|
|
|
|
int flag_one = 1;
|
|
|
|
#endif
|
|
|
|
char buffer[8192];
|
|
|
|
struct stat s;
|
2002-05-22 21:19:24 +04:00
|
|
|
vfs_s_super *super = FH_SUPER;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
h = open(localname, O_RDONLY);
|
|
|
|
if (h == -1)
|
2000-02-23 10:43:14 +03:00
|
|
|
ERRNOR (EIO, -1);
|
1999-12-08 14:39:14 +03:00
|
|
|
fstat(h, &s);
|
2002-05-22 21:19:24 +04:00
|
|
|
sock = open_data_connection(me, super, fh->u.ftp.append ? "APPE" : "STOR", name, TYPE_BINARY, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (sock < 0) {
|
1999-12-08 14:39:14 +03:00
|
|
|
close(h);
|
2000-02-23 10:43:14 +03:00
|
|
|
return -1;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
#ifdef HAVE_STRUCT_LINGER
|
|
|
|
li.l_onoff = 1;
|
|
|
|
li.l_linger = 120;
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li));
|
|
|
|
#else
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_LINGER, &flag_one, sizeof (flag_one));
|
|
|
|
#endif
|
|
|
|
total = 0;
|
|
|
|
|
|
|
|
enable_interrupt_key();
|
|
|
|
while (1) {
|
1999-12-08 14:39:14 +03:00
|
|
|
while ((n = read(h, buffer, sizeof(buffer))) < 0) {
|
1998-02-27 07:54:42 +03:00
|
|
|
if (errno == EINTR) {
|
|
|
|
if (got_interrupt()) {
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = EINTR;
|
1998-02-27 07:54:42 +03:00
|
|
|
goto error_return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = errno;
|
1998-02-27 07:54:42 +03:00
|
|
|
goto error_return;
|
|
|
|
}
|
|
|
|
if (n == 0)
|
|
|
|
break;
|
|
|
|
while (write(sock, buffer, n) < 0) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
if (got_interrupt()) {
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = EINTR;
|
1998-02-27 07:54:42 +03:00
|
|
|
goto error_return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = errno;
|
1998-02-27 07:54:42 +03:00
|
|
|
goto error_return;
|
|
|
|
}
|
|
|
|
total += n;
|
2002-02-08 12:42:38 +03:00
|
|
|
print_vfs_message(_("ftpfs: storing file %lu (%lu)"),
|
|
|
|
(unsigned long) total, (unsigned long) s.st_size);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
disable_interrupt_key();
|
|
|
|
close(sock);
|
1999-12-08 14:39:14 +03:00
|
|
|
close(h);
|
|
|
|
if (get_reply (me, SUP.sock, NULL, 0) != COMPLETE)
|
2000-02-23 10:43:14 +03:00
|
|
|
ERRNOR (EIO, -1);
|
|
|
|
return 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
error_return:
|
|
|
|
disable_interrupt_key();
|
|
|
|
close(sock);
|
1999-12-08 14:39:14 +03:00
|
|
|
close(h);
|
|
|
|
get_reply(me, SUP.sock, NULL, 0);
|
|
|
|
return -1;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2002-05-22 21:19:24 +04:00
|
|
|
//#define FH_SOCK fh->u.ftp.sock
|
1999-12-08 14:39:14 +03:00
|
|
|
|
1998-09-21 13:52:07 +04:00
|
|
|
static int
|
1999-12-08 14:39:14 +03:00
|
|
|
linear_start(vfs *me, vfs_s_fh *fh, int offset)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1999-12-08 14:39:14 +03:00
|
|
|
char *name = vfs_s_fullpath (me, fh->ino);
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return 0;
|
|
|
|
FH_SOCK = open_data_connection(me, FH_SUPER, "RETR", name, TYPE_BINARY, offset);
|
2000-04-05 17:08:42 +04:00
|
|
|
g_free (name);
|
1999-12-08 14:39:14 +03:00
|
|
|
if (FH_SOCK == -1)
|
1998-09-21 13:52:07 +04:00
|
|
|
ERRNOR (EACCES, 0);
|
1999-12-08 14:39:14 +03:00
|
|
|
fh->linear = LS_LINEAR_OPEN;
|
2002-05-17 17:32:02 +04:00
|
|
|
FH_SUPER->u.ftp.control_connection_buzy = 1;
|
2002-05-22 21:19:24 +04:00
|
|
|
fh->u.ftp.append = 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1998-09-21 13:52:07 +04:00
|
|
|
static int
|
1999-12-08 14:39:14 +03:00
|
|
|
linear_read (vfs *me, vfs_s_fh *fh, void *buf, int len)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1998-09-21 13:52:07 +04:00
|
|
|
int n;
|
1999-12-08 14:39:14 +03:00
|
|
|
vfs_s_super *super = FH_SUPER;
|
|
|
|
|
|
|
|
while ((n = read (FH_SOCK, buf, len))<0) {
|
1998-09-21 13:52:07 +04:00
|
|
|
if ((errno == EINTR) && !got_interrupt())
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-01-11 03:48:23 +03:00
|
|
|
if (n<0)
|
1999-12-08 14:39:14 +03:00
|
|
|
linear_abort(me, fh);
|
1998-09-21 13:52:07 +04:00
|
|
|
|
|
|
|
if (!n) {
|
2002-05-21 21:10:50 +04:00
|
|
|
SUP.control_connection_buzy = 0;
|
1999-12-08 14:39:14 +03:00
|
|
|
close (FH_SOCK);
|
|
|
|
FH_SOCK = -1;
|
|
|
|
if ((get_reply (me, SUP.sock, NULL, 0) != COMPLETE))
|
|
|
|
ERRNOR (E_REMOTE, -1);
|
|
|
|
return 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
1998-09-21 13:52:07 +04:00
|
|
|
ERRNOR (errno, n);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
static void
|
1999-12-08 14:39:14 +03:00
|
|
|
linear_close (vfs *me, vfs_s_fh *fh)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1999-12-08 14:39:14 +03:00
|
|
|
if (FH_SOCK != -1)
|
|
|
|
linear_abort(me, fh);
|
1998-09-21 13:52:07 +04:00
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2001-06-15 00:08:27 +04:00
|
|
|
static int ftpfs_ctl (void *fh, int ctlop, int arg)
|
1998-09-21 13:52:07 +04:00
|
|
|
{
|
|
|
|
switch (ctlop) {
|
|
|
|
case MCCTL_IS_NOTREADY:
|
|
|
|
{
|
1998-10-13 02:07:53 +04:00
|
|
|
int v;
|
1998-09-21 13:52:07 +04:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if (!FH->linear)
|
1998-10-13 02:07:53 +04:00
|
|
|
vfs_die ("You may not do this");
|
1999-12-08 14:39:14 +03:00
|
|
|
if (FH->linear == LS_LINEAR_CLOSED)
|
1998-10-13 02:07:53 +04:00
|
|
|
return 0;
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
v = vfs_s_select_on_two (FH->u.ftp.sock, 0);
|
1998-09-21 13:52:07 +04:00
|
|
|
if (((v < 0) && (errno == EINTR)) || v == 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
1998-09-21 13:52:07 +04:00
|
|
|
default:
|
|
|
|
return 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
/* Warning: filename passed to this command is damaged */
|
1998-02-27 07:54:42 +03:00
|
|
|
static int
|
1999-12-08 14:39:14 +03:00
|
|
|
send_ftp_command(vfs *me, char *filename, char *cmd, int flags)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1999-12-08 14:39:14 +03:00
|
|
|
char *rpath, *p;
|
|
|
|
vfs_s_super *super;
|
1998-02-27 07:54:42 +03:00
|
|
|
int r;
|
1999-12-08 14:39:14 +03:00
|
|
|
int flush_directory_cache = (flags & OPT_FLUSH);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if (!(rpath = vfs_s_get_path_mangle(me, filename, &super, 0)))
|
1998-02-27 07:54:42 +03:00
|
|
|
return -1;
|
1999-12-08 14:39:14 +03:00
|
|
|
p = translate_path (me, super, rpath);
|
1999-12-16 15:55:16 +03:00
|
|
|
r = command (me, super, WAIT_REPLY, cmd, p);
|
|
|
|
g_free (p);
|
1999-12-08 14:39:14 +03:00
|
|
|
vfs_add_noncurrent_stamps (&vfs_ftpfs_ops, (vfsid) super, NULL);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (flags & OPT_IGNORE_ERROR)
|
|
|
|
r = COMPLETE;
|
1999-01-11 03:48:23 +03:00
|
|
|
if (r != COMPLETE)
|
|
|
|
ERRNOR (EPERM, -1);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (flush_directory_cache)
|
1999-12-08 14:39:14 +03:00
|
|
|
vfs_s_invalidate(me, super);
|
1998-02-27 07:54:42 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This routine is called as the last step in load_setup */
|
|
|
|
void
|
|
|
|
ftpfs_init_passwd(void)
|
|
|
|
{
|
|
|
|
ftpfs_anonymous_passwd = load_anon_passwd ();
|
|
|
|
if (ftpfs_anonymous_passwd)
|
|
|
|
return;
|
|
|
|
|
2002-07-02 21:08:10 +04:00
|
|
|
/* If there is no anonymous ftp password specified
|
|
|
|
* then we'll just use anonymous@
|
|
|
|
* We don't send any other thing because:
|
|
|
|
* - We want to remain anonymous
|
|
|
|
* - We want to stop SPAM
|
|
|
|
* - We don't want to let ftp sites to discriminate by the user,
|
|
|
|
* host or country.
|
|
|
|
*/
|
|
|
|
ftpfs_anonymous_passwd = g_strdup ("anonymous@");
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-10-13 02:07:53 +04:00
|
|
|
static int ftpfs_chmod (vfs *me, char *path, int mode)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1999-01-21 01:01:11 +03:00
|
|
|
char buf[BUF_SMALL];
|
2002-02-06 15:32:52 +03:00
|
|
|
|
2002-01-22 12:42:41 +03:00
|
|
|
g_snprintf(buf, sizeof(buf), "SITE CHMOD %4.4o /%%s", mode & 07777);
|
2002-02-06 15:32:52 +03:00
|
|
|
return send_ftp_command(me, path, buf, OPT_FLUSH);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-10-13 02:07:53 +04:00
|
|
|
static int ftpfs_chown (vfs *me, char *path, int owner, int group)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
#if 0
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = EPERM;
|
1998-02-27 07:54:42 +03:00
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
/* Everyone knows it is not possible to chown remotely, so why bother them.
|
|
|
|
If someone's root, then copy/move will always try to chown it... */
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
static int ftpfs_unlink (vfs *me, char *path)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2000-02-04 00:59:04 +03:00
|
|
|
return send_ftp_command(me, path, "DELE /%s", OPT_FLUSH);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2002-07-19 20:36:32 +04:00
|
|
|
/* Return 1 if path is the same directory as the one we are in now */
|
1998-09-13 14:40:43 +04:00
|
|
|
static int
|
2002-01-29 11:09:20 +03:00
|
|
|
is_same_dir (vfs *me, vfs_s_super *super, const char *path)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1999-12-08 14:39:14 +03:00
|
|
|
if (!SUP.cwdir)
|
1998-09-13 14:40:43 +04:00
|
|
|
return 0;
|
1999-12-08 14:39:14 +03:00
|
|
|
if (strcmp (path, SUP.cwdir) == 0)
|
1998-09-13 14:40:43 +04:00
|
|
|
return 1;
|
|
|
|
return 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-12-08 14:39:14 +03:00
|
|
|
ftpfs_chdir_internal (vfs *me, vfs_s_super *super, char *remote_path)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
int r;
|
1999-01-31 23:28:13 +03:00
|
|
|
char *p;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if (!SUP.cwd_defered && is_same_dir (me, super, remote_path))
|
1998-02-27 07:54:42 +03:00
|
|
|
return COMPLETE;
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
p = translate_path (me, super, remote_path);
|
|
|
|
r = command (me, super, WAIT_REPLY, "CWD /%s", p);
|
1999-12-16 15:55:16 +03:00
|
|
|
g_free (p);
|
1999-12-08 14:39:14 +03:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
if (r != COMPLETE) {
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = EIO;
|
1998-02-27 07:54:42 +03:00
|
|
|
} else {
|
2002-07-19 20:36:32 +04:00
|
|
|
g_free(SUP.cwdir);
|
1999-12-08 14:39:14 +03:00
|
|
|
SUP.cwdir = g_strdup (remote_path);
|
|
|
|
SUP.cwd_defered = 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
static int ftpfs_rename (vfs *me, char *path1, char *path2)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2000-02-04 00:59:04 +03:00
|
|
|
send_ftp_command(me, path1, "RNFR /%s", OPT_FLUSH);
|
|
|
|
return send_ftp_command(me, path2, "RNTO /%s", OPT_FLUSH);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
static int ftpfs_mkdir (vfs *me, char *path, mode_t mode)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2000-02-04 00:59:04 +03:00
|
|
|
return send_ftp_command(me, path, "MKD /%s", OPT_FLUSH);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
static int ftpfs_rmdir (vfs *me, char *path)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2000-02-04 00:59:04 +03:00
|
|
|
return send_ftp_command(me, path, "RMD /%s", OPT_FLUSH);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
static int ftpfs_fh_open (vfs *me, vfs_s_fh *fh, int flags, int mode)
|
|
|
|
{
|
2002-05-22 21:19:24 +04:00
|
|
|
fh->u.ftp.append = 0;
|
2002-05-17 17:32:02 +04:00
|
|
|
/* File will be written only, so no need to retrieve it from ftp server */
|
|
|
|
if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY|O_RDWR))){
|
|
|
|
#ifdef HAVE_STRUCT_LINGER
|
|
|
|
struct linger li;
|
|
|
|
#else
|
|
|
|
int li = 1;
|
|
|
|
#endif
|
|
|
|
char * name;
|
|
|
|
|
|
|
|
/* linear_start() called, so data will be written
|
|
|
|
* to local temporary file and stored to ftp server
|
|
|
|
* by vfs_s_close later
|
|
|
|
*/
|
2002-05-22 17:31:15 +04:00
|
|
|
if (FH_SUPER->u.ftp.control_connection_buzy){
|
|
|
|
if (!fh->ino->localname){
|
|
|
|
int handle = mc_mkstemps (&fh->ino->localname, me->name, NULL);
|
|
|
|
if (handle == -1)
|
|
|
|
return -1;
|
|
|
|
close (handle);
|
2002-05-22 21:19:24 +04:00
|
|
|
fh->u.ftp.append = flags & O_APPEND;
|
2002-05-22 17:31:15 +04:00
|
|
|
}
|
2002-05-17 17:32:02 +04:00
|
|
|
return 0;
|
2002-05-22 17:31:15 +04:00
|
|
|
}
|
2002-05-17 17:32:02 +04:00
|
|
|
name = vfs_s_fullpath (me, fh->ino);
|
|
|
|
if (!name)
|
|
|
|
return -1;
|
|
|
|
fh->handle = open_data_connection(me, fh->ino->super,
|
|
|
|
(flags & O_APPEND) ? "APPE" : "STOR", name, TYPE_BINARY, 0);
|
|
|
|
g_free (name);
|
|
|
|
|
|
|
|
if (fh->handle < 0)
|
|
|
|
return -1;
|
|
|
|
#ifdef HAVE_STRUCT_LINGER
|
|
|
|
li.l_onoff = 1;
|
|
|
|
li.l_linger = 120;
|
|
|
|
#endif
|
|
|
|
setsockopt(fh->handle, SOL_SOCKET, SO_LINGER, &li, sizeof(li));
|
|
|
|
|
|
|
|
if (fh->ino->localname){
|
|
|
|
unlink (fh->ino->localname);
|
|
|
|
g_free (fh->ino->localname);
|
|
|
|
fh->ino->localname = NULL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
if (!fh->ino->localname)
|
|
|
|
if (vfs_s_retrieve_file (me, fh->ino)==-1)
|
|
|
|
return -1;
|
|
|
|
if (!fh->ino->localname)
|
|
|
|
vfs_die( "retrieve_file failed to fill in localname" );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-17 17:32:02 +04:00
|
|
|
static int ftpfs_fh_close (vfs *me, vfs_s_fh *fh)
|
|
|
|
{
|
|
|
|
if (fh->handle != -1 && !fh->ino->localname){
|
|
|
|
close (fh->handle);
|
|
|
|
fh->handle = -1;
|
|
|
|
/* File is stored to destination already, so
|
|
|
|
* we prevent MEDATA->file_store() call from vfs_s_close ()
|
|
|
|
*/
|
|
|
|
fh->changed = 0;
|
|
|
|
if (get_reply (me, fh->ino->SUP.sock, NULL, 0) != COMPLETE)
|
|
|
|
ERRNOR (EIO, -1);
|
2002-05-21 21:10:50 +04:00
|
|
|
vfs_s_invalidate (me, FH_SUPER);
|
2002-05-17 17:32:02 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
static struct vfs_s_data ftp_data = {
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
0,
|
2000-10-16 22:37:54 +04:00
|
|
|
NULL, /* logfile */
|
1999-12-08 14:39:14 +03:00
|
|
|
|
|
|
|
NULL, /* init_inode */
|
|
|
|
NULL, /* free_inode */
|
|
|
|
NULL, /* init_entry */
|
|
|
|
|
|
|
|
NULL, /* archive_check */
|
|
|
|
archive_same,
|
|
|
|
open_archive,
|
|
|
|
free_archive,
|
|
|
|
|
|
|
|
ftpfs_fh_open, /* fh_open */
|
2002-05-17 17:32:02 +04:00
|
|
|
ftpfs_fh_close, /* fh_close */
|
1999-12-08 14:39:14 +03:00
|
|
|
|
|
|
|
vfs_s_find_entry_linear,
|
|
|
|
dir_load,
|
|
|
|
dir_uptodate,
|
|
|
|
file_store,
|
|
|
|
|
|
|
|
linear_start,
|
|
|
|
linear_read,
|
|
|
|
linear_close
|
|
|
|
};
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2001-08-06 19:39:08 +04:00
|
|
|
static void
|
|
|
|
ftpfs_fill_names (vfs *me, void (*func)(char *))
|
|
|
|
{
|
|
|
|
struct vfs_s_super * super = ftp_data.supers;
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
while (super){
|
|
|
|
name = g_strconcat ("/#ftp:", SUP.user, "@", SUP.host, "/", SUP.cwdir, NULL);
|
|
|
|
(*func)(name);
|
|
|
|
g_free (name);
|
|
|
|
super = super->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-13 02:07:53 +04:00
|
|
|
vfs vfs_ftpfs_ops = {
|
1998-09-27 23:27:58 +04:00
|
|
|
NULL, /* This is place of next pointer */
|
2001-06-12 01:58:58 +04:00
|
|
|
"ftpfs",
|
1998-09-27 23:27:58 +04:00
|
|
|
F_NET, /* flags */
|
|
|
|
"ftp:", /* prefix */
|
1999-12-08 14:39:14 +03:00
|
|
|
&ftp_data, /* data */
|
1998-09-27 23:27:58 +04:00
|
|
|
0, /* errno */
|
2000-10-16 22:37:54 +04:00
|
|
|
NULL, /* init */
|
|
|
|
NULL, /* done */
|
2001-08-06 19:39:08 +04:00
|
|
|
ftpfs_fill_names,
|
1998-09-27 23:27:58 +04:00
|
|
|
NULL,
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
vfs_s_open,
|
|
|
|
vfs_s_close,
|
|
|
|
vfs_s_read,
|
|
|
|
vfs_s_write,
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
vfs_s_opendir,
|
|
|
|
vfs_s_readdir,
|
|
|
|
vfs_s_closedir,
|
|
|
|
vfs_s_telldir,
|
|
|
|
vfs_s_seekdir,
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
vfs_s_stat,
|
|
|
|
vfs_s_lstat,
|
|
|
|
vfs_s_fstat,
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
ftpfs_chmod,
|
1998-08-31 14:05:42 +04:00
|
|
|
ftpfs_chown, /* not really implemented but returns success */
|
1998-02-27 07:54:42 +03:00
|
|
|
NULL,
|
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
vfs_s_readlink,
|
1998-08-31 14:05:42 +04:00
|
|
|
NULL,
|
|
|
|
NULL,
|
1998-02-27 07:54:42 +03:00
|
|
|
ftpfs_unlink,
|
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
ftpfs_rename,
|
1999-12-08 14:39:14 +03:00
|
|
|
vfs_s_chdir,
|
|
|
|
vfs_s_ferrno,
|
|
|
|
vfs_s_lseek,
|
1998-08-31 14:05:42 +04:00
|
|
|
NULL,
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
vfs_s_getid,
|
|
|
|
vfs_s_nothingisopen,
|
|
|
|
vfs_s_free,
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-12-08 14:39:14 +03:00
|
|
|
NULL,
|
|
|
|
NULL,
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
ftpfs_mkdir,
|
|
|
|
ftpfs_rmdir,
|
|
|
|
ftpfs_ctl,
|
1999-12-08 14:39:14 +03:00
|
|
|
vfs_s_setctl
|
1998-10-13 02:07:53 +04:00
|
|
|
|
|
|
|
MMAPNULL
|
1998-02-27 07:54:42 +03:00
|
|
|
};
|
|
|
|
|
2000-10-16 22:37:54 +04:00
|
|
|
void ftpfs_set_debug (const char *file)
|
|
|
|
{
|
|
|
|
logfile = fopen (file, "w+");
|
|
|
|
if (logfile)
|
|
|
|
ftp_data.logfile = logfile;
|
|
|
|
}
|
|
|
|
|
1999-01-21 01:01:11 +03:00
|
|
|
static char buffer[BUF_MEDIUM];
|
1998-02-27 07:54:42 +03:00
|
|
|
static char *netrc, *netrcp;
|
|
|
|
|
2002-07-13 00:00:03 +04:00
|
|
|
/* This should match the keywords[] array below */
|
|
|
|
typedef enum {
|
|
|
|
NETRC_NONE = 0,
|
|
|
|
NETRC_DEFAULT,
|
|
|
|
NETRC_MACHINE,
|
|
|
|
NETRC_LOGIN,
|
|
|
|
NETRC_PASSWORD,
|
|
|
|
NETRC_PASSWD,
|
|
|
|
NETRC_ACCOUNT,
|
|
|
|
NETRC_MACDEF,
|
2002-07-13 03:20:03 +04:00
|
|
|
NETRC_UNKNOWN
|
2002-07-13 00:00:03 +04:00
|
|
|
} keyword_t;
|
|
|
|
|
|
|
|
static keyword_t netrc_next (void)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
char *p;
|
2002-07-13 00:00:03 +04:00
|
|
|
keyword_t i;
|
2002-07-13 03:20:03 +04:00
|
|
|
static const char *const keywords[] = { "default", "machine",
|
|
|
|
"login", "password", "passwd", "account", "macdef", NULL
|
|
|
|
};
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
while (1) {
|
2002-07-13 03:20:03 +04:00
|
|
|
netrcp = skip_separators (netrcp);
|
|
|
|
if (*netrcp != '\n')
|
|
|
|
break;
|
|
|
|
netrcp++;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
if (!*netrcp)
|
2002-07-13 00:00:03 +04:00
|
|
|
return NETRC_NONE;
|
1998-02-27 07:54:42 +03:00
|
|
|
p = buffer;
|
|
|
|
if (*netrcp == '"') {
|
2000-01-24 15:35:52 +03:00
|
|
|
for (netrcp++; *netrcp != '"' && *netrcp; netrcp++) {
|
1998-02-27 07:54:42 +03:00
|
|
|
if (*netrcp == '\\')
|
|
|
|
netrcp++;
|
|
|
|
*p++ = *netrcp;
|
|
|
|
}
|
|
|
|
} else {
|
2002-07-13 03:20:03 +04:00
|
|
|
for (; *netrcp != '\n' && *netrcp != '\t' && *netrcp != ' ' &&
|
|
|
|
*netrcp != ',' && *netrcp; netrcp++) {
|
1998-02-27 07:54:42 +03:00
|
|
|
if (*netrcp == '\\')
|
|
|
|
netrcp++;
|
|
|
|
*p++ = *netrcp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
if (!*buffer)
|
|
|
|
return 0;
|
2002-07-13 00:00:03 +04:00
|
|
|
|
|
|
|
i = NETRC_DEFAULT;
|
2002-07-13 03:20:03 +04:00
|
|
|
while (keywords[i - 1]) {
|
|
|
|
if (!strcmp (keywords[i - 1], buffer))
|
2002-07-13 00:00:03 +04:00
|
|
|
return i;
|
|
|
|
|
2002-07-13 03:20:03 +04:00
|
|
|
i++;
|
2002-07-13 00:00:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NETRC_UNKNOWN;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2002-07-13 03:20:03 +04:00
|
|
|
static int netrc_has_incorrect_mode (char *netrcname, char *netrc)
|
2000-01-24 15:35:52 +03:00
|
|
|
{
|
|
|
|
static int be_angry = 1;
|
|
|
|
struct stat mystat;
|
|
|
|
|
|
|
|
if (stat (netrcname, &mystat) >= 0 && (mystat.st_mode & 077)) {
|
|
|
|
if (be_angry) {
|
2002-07-13 03:20:03 +04:00
|
|
|
message_1s (1, MSG_ERROR,
|
|
|
|
_("~/.netrc file has not correct mode.\n"
|
|
|
|
"Remove password or correct mode."));
|
2000-01-24 15:35:52 +03:00
|
|
|
be_angry = 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-13 03:20:03 +04:00
|
|
|
/* Scan .netrc until we find matching "machine" or "default"
|
|
|
|
* domain is used for additional matching
|
|
|
|
* No search is done after "default" in compliance with "man netrc"
|
|
|
|
* Return 0 if found, -1 otherwise */
|
|
|
|
static int find_machine (const char *host, const char *domain)
|
|
|
|
{
|
|
|
|
keyword_t keyword;
|
|
|
|
|
|
|
|
while ((keyword = netrc_next ()) != NETRC_NONE) {
|
|
|
|
if (keyword == NETRC_DEFAULT)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (keyword == NETRC_MACDEF) {
|
|
|
|
/* Scan for an empty line, which concludes "macdef" */
|
|
|
|
do {
|
|
|
|
while (*netrcp && *netrcp != '\n')
|
|
|
|
netrcp++;
|
|
|
|
if (*netrcp != '\n')
|
|
|
|
break;
|
|
|
|
netrcp++;
|
|
|
|
} while (*netrcp && *netrcp != '\n');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keyword != NETRC_MACHINE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Take machine name */
|
|
|
|
if (netrc_next () == NETRC_NONE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (g_strcasecmp (host, buffer)) {
|
|
|
|
/* Try adding our domain to short names in .netrc */
|
|
|
|
char *host_domain = strchr (host, '.');
|
|
|
|
if (!host_domain)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Compare domain part */
|
|
|
|
if (g_strcasecmp (host_domain, domain))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Compare local part */
|
|
|
|
if (g_strncasecmp (host, buffer, host_domain - host))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* end of .netrc */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract login and password from .netrc for the host.
|
|
|
|
* pass may be NULL.
|
|
|
|
* Returns 0 for success, -1 for error */
|
2002-07-13 00:00:03 +04:00
|
|
|
static int lookup_netrc (const char *host, char **login, char **pass)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2002-07-12 21:39:01 +04:00
|
|
|
char *netrcname;
|
|
|
|
char *tmp_pass = NULL;
|
1998-02-27 07:54:42 +03:00
|
|
|
char hostname[MAXHOSTNAMELEN], *domain;
|
2002-07-13 00:00:03 +04:00
|
|
|
keyword_t keyword;
|
1998-02-27 07:54:42 +03:00
|
|
|
static struct rupcache {
|
2002-07-13 03:20:03 +04:00
|
|
|
struct rupcache *next;
|
|
|
|
char *host;
|
|
|
|
char *login;
|
|
|
|
char *pass;
|
1998-02-27 07:54:42 +03:00
|
|
|
} *rup_cache = NULL, *rupp;
|
|
|
|
|
2002-07-13 03:20:03 +04:00
|
|
|
/* Initialize *login and *pass */
|
|
|
|
if (!login)
|
|
|
|
return 0;
|
|
|
|
*login = NULL;
|
|
|
|
if (pass)
|
|
|
|
*pass = NULL;
|
|
|
|
|
2002-07-13 00:00:03 +04:00
|
|
|
/* Look up in the cache first */
|
|
|
|
for (rupp = rup_cache; rupp != NULL; rupp = rupp->next) {
|
2002-07-13 03:20:03 +04:00
|
|
|
if (!strcmp (host, rupp->host)) {
|
|
|
|
if (rupp->login)
|
|
|
|
*login = g_strdup (rupp->login);
|
|
|
|
if (pass && rupp->pass)
|
|
|
|
*pass = g_strdup (rupp->pass);
|
|
|
|
return 0;
|
2002-07-11 01:26:55 +04:00
|
|
|
}
|
2002-07-13 00:00:03 +04:00
|
|
|
}
|
|
|
|
|
2002-07-13 03:20:03 +04:00
|
|
|
/* Load current .netrc */
|
1999-01-21 01:01:11 +03:00
|
|
|
netrcname = concat_dir_and_file (home_dir, ".netrc");
|
1998-02-27 07:54:42 +03:00
|
|
|
netrcp = netrc = load_file (netrcname);
|
|
|
|
if (netrc == NULL) {
|
2002-07-13 03:20:03 +04:00
|
|
|
g_free (netrcname);
|
1998-02-27 07:54:42 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2002-07-13 00:00:03 +04:00
|
|
|
|
2002-07-13 03:20:03 +04:00
|
|
|
/* Find our own domain name */
|
1998-02-27 07:54:42 +03:00
|
|
|
if (gethostname (hostname, sizeof (hostname)) < 0)
|
|
|
|
*hostname = 0;
|
|
|
|
if (!(domain = strchr (hostname, '.')))
|
|
|
|
domain = "";
|
|
|
|
|
2002-07-13 03:20:03 +04:00
|
|
|
/* Scan for "default" and matching "machine" keywords */
|
|
|
|
find_machine (host, domain);
|
2002-07-12 21:39:01 +04:00
|
|
|
|
2002-07-13 03:20:03 +04:00
|
|
|
/* Scan for keywords following "default" and "machine" */
|
|
|
|
while (1) {
|
|
|
|
int need_break = 0;
|
|
|
|
keyword = netrc_next ();
|
2002-07-13 00:00:03 +04:00
|
|
|
|
2002-07-13 03:20:03 +04:00
|
|
|
switch (keyword) {
|
|
|
|
case NETRC_LOGIN:
|
|
|
|
if (netrc_next () == NETRC_NONE) {
|
|
|
|
need_break = 1;
|
|
|
|
break;
|
|
|
|
}
|
2002-07-13 00:00:03 +04:00
|
|
|
|
2002-07-13 03:20:03 +04:00
|
|
|
/* We have another name already - should not happen */
|
|
|
|
if (*login) {
|
|
|
|
need_break = 1;
|
|
|
|
break;
|
|
|
|
}
|
2002-07-13 00:00:03 +04:00
|
|
|
|
2002-07-13 03:20:03 +04:00
|
|
|
/* We have login name now */
|
|
|
|
*login = g_strdup (buffer);
|
|
|
|
break;
|
2002-07-13 00:00:03 +04:00
|
|
|
|
2002-07-13 03:20:03 +04:00
|
|
|
case NETRC_PASSWORD:
|
|
|
|
case NETRC_PASSWD:
|
|
|
|
if (netrc_next () == NETRC_NONE) {
|
|
|
|
need_break = 1;
|
|
|
|
break;
|
|
|
|
}
|
2002-07-13 00:00:03 +04:00
|
|
|
|
2002-07-13 03:20:03 +04:00
|
|
|
/* Ignore unsafe passwords */
|
|
|
|
if (strcmp (*login, "anonymous") && strcmp (*login, "ftp")
|
|
|
|
&& netrc_has_incorrect_mode (netrcname, netrc)) {
|
|
|
|
need_break = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remember password. pass may be NULL, so use tmp_pass */
|
|
|
|
if (tmp_pass == NULL)
|
|
|
|
tmp_pass = g_strdup (buffer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NETRC_ACCOUNT:
|
|
|
|
/* "account" is followed by a token which we ignore */
|
|
|
|
if (netrc_next () == NETRC_NONE) {
|
|
|
|
need_break = 1;
|
|
|
|
break;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2002-07-13 03:20:03 +04:00
|
|
|
|
|
|
|
/* Ignore account, but warn user anyways */
|
|
|
|
netrc_has_incorrect_mode (netrcname, netrc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Unexpected keyword or end of file */
|
|
|
|
need_break = 1;
|
|
|
|
break;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2002-07-13 03:20:03 +04:00
|
|
|
|
|
|
|
if (need_break)
|
1998-02-27 07:54:42 +03:00
|
|
|
break;
|
|
|
|
}
|
2000-04-28 11:43:13 +04:00
|
|
|
|
|
|
|
g_free (netrc);
|
|
|
|
g_free (netrcname);
|
|
|
|
|
1999-01-21 01:01:11 +03:00
|
|
|
rupp = g_new (struct rupcache, 1);
|
|
|
|
rupp->host = g_strdup (host);
|
1998-02-27 07:54:42 +03:00
|
|
|
rupp->login = rupp->pass = 0;
|
2002-07-13 03:20:03 +04:00
|
|
|
|
2002-07-11 01:26:55 +04:00
|
|
|
if (*login != NULL) {
|
2002-07-13 03:20:03 +04:00
|
|
|
rupp->login = g_strdup (*login);
|
2002-07-11 01:26:55 +04:00
|
|
|
}
|
2002-07-12 21:39:01 +04:00
|
|
|
if (tmp_pass != NULL)
|
2002-07-13 03:20:03 +04:00
|
|
|
rupp->pass = g_strdup (tmp_pass);
|
1998-02-27 07:54:42 +03:00
|
|
|
rupp->next = rup_cache;
|
|
|
|
rup_cache = rupp;
|
2002-07-12 21:39:01 +04:00
|
|
|
|
|
|
|
if (pass)
|
|
|
|
*pass = tmp_pass;
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
return 0;
|
|
|
|
}
|