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
|
1998-02-27 07:54:42 +03:00
|
|
|
|
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
|
1998-02-27 07:54:42 +03:00
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
1998-10-13 02:07:53 +04:00
|
|
|
/* Namespace pollution: horrible */
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <ctype.h> /* For isdigit */
|
|
|
|
#ifdef SCO_FLAVOR
|
|
|
|
# include <sys/timeb.h> /* alex: for struct timeb definition */
|
|
|
|
#endif /* SCO_FLAVOR */
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#if defined(HAVE_UNISTD_H)
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_SELECT_H
|
|
|
|
# include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
#include "../src/fs.h"
|
|
|
|
#include "../src/mad.h"
|
|
|
|
#include "../src/setup.h"
|
|
|
|
#include "../src/tty.h" /* enable/disable interrupt key */
|
1998-08-27 00:23:10 +04:00
|
|
|
#include "../src/main.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>
|
|
|
|
#ifndef SCO_FLAVOR
|
|
|
|
# include <sys/time.h> /* alex: this redefines struct timeval */
|
|
|
|
#endif /* SCO_FLAVOR */
|
|
|
|
#include <sys/param.h>
|
1998-10-14 06:56:18 +04:00
|
|
|
#undef MIN
|
|
|
|
#undef MAX
|
|
|
|
#include <glib.h>
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
#ifdef USE_TERMNET
|
|
|
|
#include <termnet.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "../src/mem.h"
|
|
|
|
#include "vfs.h"
|
|
|
|
#include "tcputil.h"
|
|
|
|
#include "../src/util.h"
|
|
|
|
#include "../src/dialog.h"
|
|
|
|
#include "container.h"
|
|
|
|
#include "ftpfs.h"
|
|
|
|
#ifndef MAXHOSTNAMELEN
|
|
|
|
# define MAXHOSTNAMELEN 64
|
|
|
|
#endif
|
|
|
|
|
1998-10-23 12:26:25 +04:00
|
|
|
#define ERRNOR(x,y) do { my_errno = x; return y; } while(0)
|
1998-02-27 07:54:42 +03:00
|
|
|
#define UPLOAD_ZERO_LENGTH_FILE
|
|
|
|
|
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;
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
/* Use the ~/.netrc */
|
|
|
|
int use_netrc = 1;
|
|
|
|
|
|
|
|
extern char *home_dir;
|
|
|
|
|
|
|
|
/* Anonymous setup */
|
1998-05-26 04:53:24 +04:00
|
|
|
char *ftpfs_anonymous_passwd = 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
int ftpfs_directory_timeout;
|
|
|
|
|
|
|
|
/* Proxy host */
|
|
|
|
char *ftpfs_proxy_host = 0;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
static struct linklist *connections_list;
|
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-11 03:48:23 +03:00
|
|
|
static struct direntry *_get_file_entry (struct connection *bucket,
|
|
|
|
char *file_name, int op, int flags);
|
1998-10-14 06:56:18 +04:00
|
|
|
|
|
|
|
static char *ftpfs_get_current_directory (struct connection *bucket);
|
|
|
|
static int ftpfs_chdir_internal (struct connection *bucket,
|
|
|
|
char *remote_path);
|
|
|
|
static void free_bucket (void *data);
|
|
|
|
static void connection_destructor (void *data);
|
|
|
|
static void flush_all_directory (struct connection *bucket);
|
|
|
|
static int get_line (int sock, char *buf, int buf_len,
|
|
|
|
char term);
|
|
|
|
static char *get_path (struct connection **bucket,
|
|
|
|
char *path);
|
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
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
static char *
|
|
|
|
my_get_host_and_username (char *path, char **host, char **user, int *port, char **pass)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1998-11-21 22:36:01 +03:00
|
|
|
return vfs_split_url (path, host, user, port, pass, 21, URL_DEFAULTANON);
|
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
|
|
|
|
get_reply (int sock, char *string_buf, int string_len)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
char answer[1024];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (;;) {
|
1999-01-11 03:48:23 +03:00
|
|
|
if (!get_line (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-01-11 03:48:23 +03:00
|
|
|
if (!get_line (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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
static int
|
|
|
|
command (struct connection *bucket, 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;
|
1998-02-27 07:54:42 +03:00
|
|
|
int sock = qsock (bucket);
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
1998-11-21 22:36:01 +03:00
|
|
|
|
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
|
|
|
|
|
|
|
str = copy_strings (fmt_str, "\r\n", NULL);
|
|
|
|
g_free (fmt_str);
|
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){
|
1998-02-27 07:54:42 +03:00
|
|
|
char *tmp = "PASS <Password not logged>\r\n";
|
1998-10-30 20:51:28 +03:00
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
fwrite (tmp, strlen (tmp), 1, logfile);
|
1998-02-27 07:54:42 +03:00
|
|
|
} else
|
1998-10-30 20:51:28 +03:00
|
|
|
fwrite (str, strlen (str), 1, logfile);
|
|
|
|
|
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 ();
|
|
|
|
status = write (sock, str, strlen (str));
|
|
|
|
free (str);
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
if (status < 0){
|
|
|
|
code = 421;
|
1998-10-30 20:45:43 +03:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
if (errno == EPIPE){
|
|
|
|
got_sigpipe = 1;
|
|
|
|
}
|
1998-10-30 20:45:43 +03:00
|
|
|
disable_interrupt_key ();
|
1998-02-27 07:54:42 +03:00
|
|
|
return TRANSIENT;
|
|
|
|
}
|
1998-10-30 20:45:43 +03:00
|
|
|
disable_interrupt_key ();
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
if (wait_reply)
|
1998-09-13 14:40:43 +04:00
|
|
|
return get_reply (sock, (wait_reply & WANT_STRING) ? reply_str : NULL, sizeof (reply_str)-1);
|
1998-02-27 07:54:42 +03:00
|
|
|
return COMPLETE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-09-13 14:40:43 +04:00
|
|
|
connection_close (void *data)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1998-09-13 14:40:43 +04:00
|
|
|
struct connection *bucket = data;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
if (qsock (bucket) != -1){
|
1999-01-11 03:48:23 +03:00
|
|
|
print_vfs_message ("ftpfs: Disconnecting from %s", qhost (bucket));
|
1998-02-27 07:54:42 +03:00
|
|
|
command(bucket, NONE, "QUIT");
|
|
|
|
close(qsock(bucket));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
1998-09-13 14:40:43 +04:00
|
|
|
changetype (struct connection *bucket, int binary)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1998-03-02 21:09:16 +03:00
|
|
|
if (binary != bucket->isbinary) {
|
1999-01-11 03:48:23 +03:00
|
|
|
if (command (bucket, WAIT_REPLY, "TYPE %c", binary ? 'I' : 'A') != COMPLETE)
|
|
|
|
ERRNOR (EIO, -1);
|
1998-02-27 07:54:42 +03:00
|
|
|
bucket->isbinary = binary;
|
|
|
|
}
|
|
|
|
return binary;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This routine logs the user in */
|
|
|
|
static int
|
1998-09-13 14:40:43 +04:00
|
|
|
login_server (struct connection *bucket, 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;
|
1998-03-02 21:09:16 +03:00
|
|
|
|
|
|
|
bucket->isbinary = TYPE_UNKNOWN;
|
1998-02-27 07:54:42 +03:00
|
|
|
if (netrcpass)
|
|
|
|
op = strdup (netrcpass);
|
|
|
|
else {
|
1999-01-11 03:48:23 +03:00
|
|
|
if (!strcmp (quser (bucket), "anonymous") ||
|
|
|
|
!strcmp (quser (bucket), "ftp")) {
|
|
|
|
op = strdup (ftpfs_anonymous_passwd);
|
1998-02-27 07:54:42 +03:00
|
|
|
anon = 1;
|
|
|
|
} else {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (!bucket->password){
|
1999-01-11 03:48:23 +03:00
|
|
|
p = copy_strings (" FTP: Password required for ", quser (bucket),
|
1998-02-27 07:54:42 +03:00
|
|
|
" ", NULL);
|
1998-05-26 04:53:24 +04:00
|
|
|
op = vfs_get_password (p);
|
1998-02-27 07:54:42 +03:00
|
|
|
free (p);
|
1999-01-11 03:48:23 +03:00
|
|
|
if (op == NULL)
|
|
|
|
ERRNOR (EPERM, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
bucket->password = strdup (op);
|
|
|
|
} else
|
|
|
|
op = strdup (bucket->password);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
if (!anon || logfile)
|
1998-02-27 07:54:42 +03:00
|
|
|
pass = strdup (op);
|
|
|
|
else
|
|
|
|
pass = copy_strings ("-", op, 0);
|
|
|
|
wipe_password (op);
|
|
|
|
|
|
|
|
|
|
|
|
/* Proxy server accepts: username@host-we-want-to-connect*/
|
|
|
|
if (qproxy (bucket)){
|
|
|
|
#if defined(HSC_PROXY)
|
|
|
|
char *p, *host;
|
|
|
|
int port;
|
1999-01-11 03:48:23 +03:00
|
|
|
p = my_get_host_and_username (ftpfs_proxy_host, &host, &proxyname,
|
|
|
|
&port, &proxypass);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (p)
|
|
|
|
free (p);
|
|
|
|
|
1999-01-11 03:48:23 +03:00
|
|
|
free (host);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (proxypass)
|
|
|
|
wipe_password (proxypass);
|
1999-01-11 03:48:23 +03:00
|
|
|
p = copy_strings (" Proxy: Password required for ", proxyname, " ",
|
|
|
|
NULL);
|
1998-05-26 04:53:24 +04:00
|
|
|
proxypass = vfs_get_password (p);
|
1999-01-11 03:48:23 +03:00
|
|
|
free (p);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (proxypass == NULL) {
|
|
|
|
wipe_password (pass);
|
|
|
|
free (proxyname);
|
1998-09-18 15:02:04 +04:00
|
|
|
ERRNOR (EPERM, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
1999-01-11 03:48:23 +03:00
|
|
|
name = strdup (quser (bucket));
|
1998-02-27 07:54:42 +03:00
|
|
|
#else
|
1999-01-11 03:48:23 +03:00
|
|
|
name = copy_strings (quser (bucket), "@",
|
|
|
|
qhost (bucket)[0] == '!' ? qhost (bucket)+1 : qhost (bucket), 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
name = strdup (quser (bucket));
|
|
|
|
|
1999-01-11 03:48:23 +03:00
|
|
|
if (get_reply (qsock (bucket), NULL, 0) == COMPLETE) {
|
1998-02-27 07:54:42 +03:00
|
|
|
#if defined(HSC_PROXY)
|
1999-01-11 03:48:23 +03:00
|
|
|
if (qproxy (bucket)){
|
|
|
|
print_vfs_message ("ftpfs: sending proxy login name");
|
1998-10-23 12:26:25 +04:00
|
|
|
if (command (bucket, 1, "USER %s", proxyname) != CONTINUE)
|
|
|
|
goto proxyfail;
|
1999-01-11 03:48:23 +03:00
|
|
|
|
|
|
|
print_vfs_message ("ftpfs: sending proxy user password");
|
1998-10-23 12:26:25 +04:00
|
|
|
if (command (bucket, 1, "PASS %s", proxypass) != COMPLETE)
|
|
|
|
goto proxyfail;
|
1999-01-11 03:48:23 +03:00
|
|
|
|
|
|
|
print_vfs_message ("ftpfs: proxy authentication succeeded");
|
|
|
|
if (command (bucket, 1, "SITE %s", qhost (bucket)+1) != COMPLETE)
|
1998-10-23 12:26:25 +04:00
|
|
|
goto proxyfail;
|
1999-01-11 03:48:23 +03:00
|
|
|
|
|
|
|
print_vfs_message ("ftpfs: connected to %s", qhost (bucket)+1);
|
1998-10-23 12:26:25 +04:00
|
|
|
if (0) {
|
|
|
|
proxyfail:
|
1998-02-27 07:54:42 +03:00
|
|
|
bucket->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);
|
|
|
|
free (proxyname);
|
|
|
|
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);
|
|
|
|
free (proxyname);
|
|
|
|
}
|
|
|
|
#endif
|
1999-01-11 03:48:23 +03:00
|
|
|
print_vfs_message ("ftpfs: sending login name");
|
1998-02-27 07:54:42 +03:00
|
|
|
code = command (bucket, WAIT_REPLY, "USER %s", name);
|
|
|
|
|
|
|
|
switch (code){
|
|
|
|
case CONTINUE:
|
1999-01-11 03:48:23 +03:00
|
|
|
print_vfs_message ("ftpfs: sending user password");
|
1998-02-27 07:54:42 +03:00
|
|
|
if (command (bucket, WAIT_REPLY, "PASS %s", pass) != COMPLETE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COMPLETE:
|
1999-01-11 03:48:23 +03:00
|
|
|
print_vfs_message ("ftpfs: logged in");
|
1998-02-27 07:54:42 +03:00
|
|
|
wipe_password (pass);
|
|
|
|
free (name);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
bucket->failed_on_login = 1;
|
1998-09-13 14:40:43 +04:00
|
|
|
/* my_errno = E; */
|
1998-02-27 07:54:42 +03:00
|
|
|
if (bucket->password)
|
|
|
|
wipe_password (bucket->password);
|
|
|
|
bucket->password = 0;
|
|
|
|
|
1998-10-23 12:26:25 +04:00
|
|
|
goto login_fail;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
}
|
1999-01-11 03:48:23 +03:00
|
|
|
print_vfs_message ("ftpfs: Login incorrect for user %s ", quser (bucket));
|
1998-10-23 12:26:25 +04:00
|
|
|
login_fail:
|
1998-02-27 07:54:42 +03:00
|
|
|
wipe_password (pass);
|
|
|
|
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;
|
|
|
|
bzero (buffer, sizeof (buffer));
|
|
|
|
*ptr++ = IPOPT_LSRR;
|
|
|
|
*ptr++ = 3 + 8;
|
|
|
|
*ptr++ = 4; /* pointer */
|
|
|
|
|
|
|
|
/* First hop */
|
|
|
|
bcopy ((char *) &source_route, ptr, sizeof (int));
|
|
|
|
ptr += 4;
|
|
|
|
|
|
|
|
/* Second hop (ie, final destination) */
|
|
|
|
bcopy ((char *) &dest, ptr, sizeof (int));
|
|
|
|
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
|
|
|
|
load_no_proxy_list ()
|
|
|
|
{
|
|
|
|
/* FixMe: shouldn't be hardcoded!!! */
|
|
|
|
char s[258]; /* provide for 256 characters and nl */
|
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;
|
|
|
|
char *p;
|
1998-08-27 00:23:10 +04:00
|
|
|
char *mc_file;
|
1998-02-27 07:54:42 +03:00
|
|
|
static int loaded;
|
|
|
|
|
|
|
|
if (loaded)
|
|
|
|
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"))) {
|
1998-02-27 07:54:42 +03:00
|
|
|
while (fgets (s, 258, npf) || !(feof (npf) || ferror (npf))) {
|
|
|
|
if (!(p = strchr (s, '\n'))) { /* skip bogus entries */
|
|
|
|
while ((c = getc (npf)) != EOF && c != '\n')
|
|
|
|
;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p == s)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
p = xmalloc (strlen (s), "load_no_proxy_list:1");
|
|
|
|
np = xmalloc (sizeof (*np), "load_no_proxy_list:2");
|
|
|
|
strcpy (p, s);
|
|
|
|
np->domain = p;
|
|
|
|
np->next = 0;
|
|
|
|
if (no_proxy)
|
|
|
|
current->next = np;
|
|
|
|
else
|
|
|
|
no_proxy = np;
|
|
|
|
current = np;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (npf);
|
|
|
|
loaded = 1;
|
|
|
|
}
|
1998-08-27 00:23:10 +04:00
|
|
|
free (mc_file);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ftpfs_check_proxy (char *host)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
if (!strcasecmp (host, domain))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ftpfs_get_proxy_host_and_port (char *proxy, char **host, int *port)
|
|
|
|
{
|
|
|
|
char *user, *pass, *dir;
|
|
|
|
|
|
|
|
#if defined(HSC_PROXY)
|
1998-11-21 22:36:01 +03:00
|
|
|
#define PORT 9875
|
1998-02-27 07:54:42 +03:00
|
|
|
#else
|
1998-11-21 22:36:01 +03:00
|
|
|
#define PORT 21
|
1998-02-27 07:54:42 +03:00
|
|
|
#endif
|
1999-01-11 03:48:23 +03:00
|
|
|
dir = vfs_split_url (proxy, host, &user, port, &pass, PORT, URL_DEFAULTANON);
|
1998-11-21 22:36:01 +03:00
|
|
|
|
1999-01-11 03:48:23 +03:00
|
|
|
free (user);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (pass)
|
|
|
|
wipe_password (pass);
|
|
|
|
if (dir)
|
1999-01-11 03:48:23 +03:00
|
|
|
free (dir);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-01-11 03:48:23 +03:00
|
|
|
ftpfs_open_socket (struct connection *bucket)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
struct sockaddr_in server_address;
|
|
|
|
struct hostent *hp;
|
|
|
|
int my_socket;
|
|
|
|
char *host;
|
|
|
|
int port;
|
|
|
|
int free_host = 0;
|
|
|
|
|
|
|
|
/* Use a proxy host? */
|
1999-01-11 03:48:23 +03:00
|
|
|
host = qhost (bucket);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
if (!host || !*host){
|
|
|
|
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-01-11 03:48:23 +03:00
|
|
|
if (qproxy (bucket)){
|
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 */
|
|
|
|
bzero ((char *) &server_address, sizeof (server_address));
|
|
|
|
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-01-11 03:48:23 +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)
|
|
|
|
free (host);
|
|
|
|
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 */
|
|
|
|
bcopy ((char *) hp->h_addr, (char *) &server_address.sin_addr, 4);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define THE_PORT qproxy(bucket) ? port : qport (bucket)
|
|
|
|
|
|
|
|
server_address.sin_port = htons (THE_PORT);
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
free (host);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
setup_source_route (my_socket, server_address.sin_addr.s_addr);
|
|
|
|
|
1999-01-11 03:48:23 +03:00
|
|
|
print_vfs_message ("ftpfs: making connection to %s", host);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (free_host)
|
|
|
|
free (host);
|
|
|
|
|
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 ())
|
|
|
|
print_vfs_message ("ftpfs: connection interrupted by user");
|
1998-02-27 07:54:42 +03:00
|
|
|
else
|
1999-01-11 03:48:23 +03:00
|
|
|
print_vfs_message ("ftpfs: connection to server failed: %s",
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
static struct connection *
|
1998-02-27 07:54:42 +03:00
|
|
|
open_command_connection (char *host, char *user, int port, char *netrcpass)
|
|
|
|
{
|
1998-09-13 14:40:43 +04:00
|
|
|
struct connection *bucket;
|
1998-02-27 07:54:42 +03:00
|
|
|
int retry_seconds, count_down;
|
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
bucket = xmalloc(sizeof(struct connection),
|
|
|
|
"struct connection");
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-01-11 03:48:23 +03:00
|
|
|
if (bucket == NULL)
|
|
|
|
ERRNOR (ENOMEM, NULL);
|
1998-02-27 07:54:42 +03:00
|
|
|
#ifdef HAVE_MAD
|
|
|
|
{
|
|
|
|
extern void *watch_free_pointer;
|
|
|
|
|
|
|
|
if (!watch_free_pointer)
|
|
|
|
watch_free_pointer = host;
|
|
|
|
}
|
|
|
|
#endif
|
1999-01-11 03:48:23 +03:00
|
|
|
qhost (bucket) = strdup (host);
|
|
|
|
quser (bucket) = strdup (user);
|
|
|
|
qcdir (bucket) = NULL;
|
|
|
|
qport (bucket) = port;
|
|
|
|
qlock (bucket) = 0;
|
|
|
|
qhome (bucket) = NULL;
|
|
|
|
qproxy (bucket)= 0;
|
|
|
|
qupdir (bucket)= 0;
|
|
|
|
qdcache (bucket)=0;
|
1998-02-27 07:54:42 +03:00
|
|
|
bucket->__inode_counter = 0;
|
|
|
|
bucket->lock = 0;
|
|
|
|
bucket->use_proxy = ftpfs_check_proxy (host);
|
|
|
|
bucket->password = 0;
|
|
|
|
bucket->use_passive_connection = ftpfs_use_passive_connections | source_route;
|
|
|
|
bucket->use_source_route = source_route;
|
1998-09-15 23:41:22 +04:00
|
|
|
bucket->strict_rfc959_list_cmd = !ftpfs_use_unix_list_options;
|
1998-03-02 21:09:16 +03:00
|
|
|
bucket->isbinary = TYPE_UNKNOWN;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* We do not want to use the passive if we are using proxies */
|
|
|
|
if (bucket->use_proxy)
|
|
|
|
bucket->use_passive_connection = 0;
|
|
|
|
|
1999-01-11 03:48:23 +03:00
|
|
|
if ((qdcache (bucket) = linklist_init ()) == NULL) {
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = ENOMEM;
|
1999-01-11 03:48:23 +03:00
|
|
|
free (qhost (bucket));
|
|
|
|
free (quser (bucket));
|
1998-02-27 07:54:42 +03:00
|
|
|
free (bucket);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
retry_seconds = 0;
|
|
|
|
do {
|
|
|
|
bucket->failed_on_login = 0;
|
|
|
|
|
1999-01-11 03:48:23 +03:00
|
|
|
qsock (bucket) = ftpfs_open_socket (bucket);
|
|
|
|
if (qsock (bucket) == -1) {
|
1998-09-13 14:40:43 +04:00
|
|
|
free_bucket (bucket);
|
1998-02-27 07:54:42 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-01-11 03:48:23 +03:00
|
|
|
if (login_server (bucket, netrcpass)) {
|
1998-02-27 07:54:42 +03:00
|
|
|
/* Logged in, no need to retry the connection */
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (bucket->failed_on_login){
|
|
|
|
/* Close only the socket descriptor */
|
|
|
|
close (qsock (bucket));
|
|
|
|
} else {
|
1998-09-13 14:40:43 +04:00
|
|
|
connection_destructor (bucket);
|
1998-02-27 07:54:42 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (ftpfs_retry_seconds){
|
|
|
|
retry_seconds = ftpfs_retry_seconds;
|
|
|
|
enable_interrupt_key ();
|
|
|
|
for (count_down = retry_seconds; count_down; count_down--){
|
1998-09-23 14:54:54 +04: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 ();
|
1998-09-13 14:40:43 +04:00
|
|
|
free_bucket (bucket);
|
1998-02-27 07:54:42 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
disable_interrupt_key ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (retry_seconds);
|
|
|
|
|
1999-01-11 03:48:23 +03:00
|
|
|
qhome (bucket) = ftpfs_get_current_directory (bucket);
|
|
|
|
if (!qhome (bucket))
|
|
|
|
qhome (bucket) = strdup ("/");
|
|
|
|
qupdir (bucket) = strdup ("/"); /* FIXME: I changed behavior to ignore last_current_dir */
|
1998-02-27 07:54:42 +03:00
|
|
|
return bucket;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-01-11 03:48:23 +03:00
|
|
|
is_connection_closed (struct connection *bucket)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
fd_set rset;
|
|
|
|
struct timeval t;
|
|
|
|
|
|
|
|
if (got_sigpipe){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
t.tv_sec = 0;
|
|
|
|
t.tv_usec = 0;
|
1999-01-11 03:48:23 +03:00
|
|
|
FD_ZERO (&rset);
|
|
|
|
FD_SET (qsock (bucket), &rset);
|
1998-02-27 07:54:42 +03:00
|
|
|
while (1) {
|
1999-01-11 03:48:23 +03:00
|
|
|
if (select (qsock (bucket) + 1, &rset, NULL, NULL, &t) < 0)
|
1998-02-27 07:54:42 +03:00
|
|
|
if (errno != EINTR)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
#if 0
|
1999-01-11 03:48:23 +03:00
|
|
|
if (FD_ISSET (qsock(bucket), &rset)) {
|
|
|
|
n = read (qsock(bucket), &read_ahead, sizeof (read_ahead));
|
1998-02-27 07:54:42 +03:00
|
|
|
if (n <= 0)
|
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This routine keeps track of open connections */
|
|
|
|
/* Returns a connected socket to host */
|
1998-09-13 14:40:43 +04:00
|
|
|
static struct connection *
|
|
|
|
open_link (char *host, char *user, int port, char *netrcpass)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
int sock;
|
1998-09-13 14:40:43 +04:00
|
|
|
struct connection *bucket;
|
1998-02-27 07:54:42 +03:00
|
|
|
struct linklist *lptr;
|
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
for (lptr = connections_list->next;
|
|
|
|
lptr != connections_list; lptr = lptr->next) {
|
1998-02-27 07:54:42 +03:00
|
|
|
bucket = lptr->data;
|
1999-01-11 03:48:23 +03:00
|
|
|
if ((strcmp (host, qhost (bucket)) == 0) &&
|
|
|
|
(strcmp (user, quser (bucket)) == 0) &&
|
|
|
|
(port == qport (bucket))) {
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* check the connection is closed or not, just hack */
|
1999-01-11 03:48:23 +03:00
|
|
|
if (is_connection_closed (bucket)) {
|
|
|
|
flush_all_directory (bucket);
|
|
|
|
sock = ftpfs_open_socket (bucket);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (sock != -1) {
|
1999-01-11 03:48:23 +03:00
|
|
|
close (qsock (bucket));
|
|
|
|
qsock (bucket) = sock;
|
|
|
|
if (login_server (bucket, netrcpass))
|
1998-02-27 07:54:42 +03:00
|
|
|
return bucket;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* connection refused */
|
|
|
|
lptr->prev->next = lptr->next;
|
|
|
|
lptr->next->prev = lptr->prev;
|
1999-01-11 03:48:23 +03:00
|
|
|
connection_destructor (bucket);
|
1998-02-27 07:54:42 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return bucket;
|
|
|
|
}
|
|
|
|
}
|
1999-01-11 03:48:23 +03:00
|
|
|
bucket = open_command_connection (host, user, port, netrcpass);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (bucket == NULL)
|
|
|
|
return NULL;
|
1999-01-11 03:48:23 +03:00
|
|
|
if (!linklist_insert (connections_list, bucket)) {
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = ENOMEM;
|
1999-01-11 03:48:23 +03:00
|
|
|
connection_destructor (bucket);
|
1998-02-27 07:54:42 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return bucket;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The returned directory should always contain a trailing slash */
|
1999-01-11 03:48:23 +03:00
|
|
|
static char *
|
|
|
|
ftpfs_get_current_directory (struct connection *bucket)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
char buf[4096], *bufp, *bufq;
|
|
|
|
|
1999-01-11 03:48:23 +03:00
|
|
|
if (command (bucket, NONE, "PWD") == COMPLETE &&
|
1998-09-13 14:40:43 +04:00
|
|
|
get_reply(qsock(bucket), 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;
|
|
|
|
}
|
|
|
|
return strdup (bufp);
|
1998-09-18 15:02:04 +04:00
|
|
|
} else ERRNOR (EIO, 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
|
1998-09-13 14:40:43 +04:00
|
|
|
setup_passive (int my_socket, struct connection *bucket, 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;
|
|
|
|
|
|
|
|
if (command (bucket, WAIT_REPLY | WANT_STRING, "PASV") != COMPLETE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Parse remote parameters */
|
|
|
|
for (c = reply_str + 4; (*c) && (!isdigit (*c)); c++)
|
|
|
|
;
|
|
|
|
if (!*c)
|
|
|
|
return 0;
|
|
|
|
if (!isdigit (*c))
|
|
|
|
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;
|
|
|
|
|
|
|
|
bcopy ((void *)n, &(sa->sin_addr.s_addr), 4);
|
|
|
|
bcopy ((void *)&n[4], &(sa->sin_port), 2);
|
|
|
|
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
|
1998-09-13 14:40:43 +04:00
|
|
|
initconn (struct connection *bucket)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
struct sockaddr_in data_addr;
|
|
|
|
int data, len = sizeof(data_addr);
|
|
|
|
struct protoent *pe;
|
|
|
|
|
|
|
|
getsockname(qsock(bucket), (struct sockaddr *) &data_addr, &len);
|
|
|
|
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
|
|
|
|
|
|
|
#ifdef ORIGINAL_CONNECT_CODE
|
|
|
|
if (bucket->use_source_route){
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if ((c = setup_passive (data, bucket, &data_addr)))
|
|
|
|
return data;
|
|
|
|
print_vfs_message("ftpfs: could not setup passive mode for source routing");
|
|
|
|
bucket->use_source_route = 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (bucket->use_passive_connection){
|
|
|
|
if ((bucket->use_passive_connection = setup_passive (data, bucket, &data_addr)))
|
|
|
|
return data;
|
|
|
|
|
|
|
|
bucket->use_source_route = 0;
|
|
|
|
bucket->use_passive_connection = 0;
|
|
|
|
print_vfs_message ("ftpfs: could not setup passive mode");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* If passive setup fails, fallback to active connections */
|
|
|
|
/* Active FTP connection */
|
|
|
|
if (bind (data, (struct sockaddr *)&data_addr, len) < 0)
|
|
|
|
goto error_return;
|
|
|
|
getsockname(data, (struct sockaddr *) &data_addr, &len);
|
|
|
|
if (listen (data, 1) < 0)
|
|
|
|
goto error_return;
|
|
|
|
{
|
|
|
|
unsigned char *a = (unsigned char *)&data_addr.sin_addr;
|
|
|
|
unsigned char *p = (unsigned char *)&data_addr.sin_port;
|
|
|
|
|
|
|
|
if (command (bucket, WAIT_REPLY, "PORT %d,%d,%d,%d,%d,%d", a[0], a[1],
|
|
|
|
a[2], a[3], p[0], p[1]) != COMPLETE)
|
|
|
|
goto error_return;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
error_return:
|
|
|
|
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
|
1998-09-13 14:40:43 +04:00
|
|
|
open_data_connection (struct connection *bucket, 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);
|
|
|
|
|
|
|
|
if ((s = initconn (bucket)) == -1)
|
|
|
|
return -1;
|
|
|
|
if (changetype (bucket, isbinary) == -1)
|
|
|
|
return -1;
|
1998-10-13 02:07:53 +04:00
|
|
|
if (reget > 0){
|
|
|
|
j = command (bucket, WAIT_REPLY, "REST %d", reget);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (j != CONTINUE)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (remote)
|
|
|
|
j = command (bucket, WAIT_REPLY, "%s %s", cmd, remote);
|
|
|
|
else
|
|
|
|
j = command (bucket, WAIT_REPLY, "%s", cmd);
|
1999-01-11 03:48:23 +03:00
|
|
|
if (j != PRELIM)
|
|
|
|
ERRNOR (EPERM, -1);
|
1998-02-27 07:54:42 +03:00
|
|
|
enable_interrupt_key();
|
|
|
|
if (bucket->use_passive_connection)
|
|
|
|
data = s;
|
|
|
|
else {
|
|
|
|
data = accept (s, (struct sockaddr *)&from, &fromlen);
|
|
|
|
if (data < 0) {
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = errno;
|
1998-02-27 07:54:42 +03:00
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
}
|
|
|
|
disable_interrupt_key();
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-09-21 13:52:07 +04:00
|
|
|
my_abort (struct connection *bucket, int dsock)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
static unsigned char ipbuf[3] = { IAC, IP, IAC };
|
|
|
|
fd_set mask;
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
print_vfs_message("ftpfs: aborting transfer.");
|
|
|
|
if (send(qsock(bucket), ipbuf, sizeof(ipbuf), MSG_OOB) != sizeof(ipbuf)) {
|
|
|
|
print_vfs_message("ftpfs: abort error: %s", unix_error_string(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (command(bucket, NONE, "%cABOR", DM) != COMPLETE){
|
|
|
|
print_vfs_message ("ftpfs: abort failed");
|
|
|
|
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);
|
|
|
|
}
|
1998-09-13 14:40:43 +04:00
|
|
|
if ((get_reply(qsock(bucket), NULL, 0) == TRANSIENT) && (code == 426))
|
|
|
|
get_reply(qsock(bucket), NULL, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-11-24 00:13:08 +03:00
|
|
|
resolve_symlink_without_ls_options(struct connection *bucket, struct dir *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];
|
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;
|
|
|
|
if (S_ISLNK(fel->s.st_mode)) {
|
|
|
|
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
|
|
|
}
|
1998-09-15 23:41:22 +04:00
|
|
|
for ( ;; ) {
|
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 {
|
|
|
|
fel->l_stat = xmalloc(sizeof(struct stat),
|
|
|
|
"resolve_symlink: struct stat");
|
|
|
|
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
|
|
|
|
resolve_symlink_with_ls_options(struct connection *bucket, struct dir *dir)
|
|
|
|
{
|
|
|
|
char buffer[2048] = "", *filename;
|
|
|
|
int sock;
|
|
|
|
FILE *fp;
|
|
|
|
struct stat s;
|
|
|
|
struct linklist *flist;
|
|
|
|
struct direntry *fe;
|
|
|
|
|
|
|
|
dir->symlink_status = FTPFS_RESOLVED_SYMLINKS;
|
|
|
|
if (strchr (dir->remote_path, ' ')) {
|
|
|
|
if (ftpfs_chdir_internal (bucket, dir->remote_path) != COMPLETE) {
|
|
|
|
print_vfs_message("ftpfs: CWD failed.");
|
|
|
|
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) {
|
|
|
|
print_vfs_message("ftpfs: couldn't resolve symlink");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fdopen(sock, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
close(sock);
|
|
|
|
print_vfs_message("ftpfs: couldn't resolve symlink");
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (vfs_parse_ls_lga (buffer, &s, &filename, NULL)) {
|
|
|
|
int r = strcmp(fe->name, filename);
|
|
|
|
free(filename);
|
|
|
|
if (r == 0) {
|
|
|
|
fe->l_stat = xmalloc(sizeof(struct stat),
|
|
|
|
"resolve_symlink: struct stat");
|
|
|
|
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);
|
|
|
|
get_reply(qsock(bucket), NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
resolve_symlink(struct connection *bucket, struct dir *dir)
|
|
|
|
{
|
|
|
|
print_vfs_message("Resolving symlink...");
|
|
|
|
|
|
|
|
if (bucket->strict_rfc959_list_cmd)
|
|
|
|
resolve_symlink_without_ls_options(bucket, dir);
|
|
|
|
else
|
|
|
|
resolve_symlink_with_ls_options(bucket, dir);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-09-15 23:41:22 +04:00
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
#define X "ftp"
|
|
|
|
#define X_myname "/#ftp:"
|
1998-10-13 02:07:53 +04:00
|
|
|
#define vfs_X_ops vfs_ftpfs_ops
|
1998-09-13 14:40:43 +04:00
|
|
|
#define X_fill_names ftpfs_fill_names
|
|
|
|
#define X_hint_reread ftpfs_hint_reread
|
|
|
|
#define X_flushdir ftpfs_flushdir
|
|
|
|
#define X_done ftpfs_done
|
|
|
|
#include "shared_ftp_fish.c"
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-09-18 15:02:04 +04:00
|
|
|
static char*
|
|
|
|
get_path (struct connection **bucket, char *path)
|
|
|
|
{
|
|
|
|
return s_get_path (bucket, path, "/#ftp:");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
static struct dir *
|
1998-09-15 23:41:22 +04:00
|
|
|
retrieve_dir(struct connection *bucket, char *remote_path, int resolve_symlinks)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
#ifdef OLD_READ
|
|
|
|
FILE *fp;
|
|
|
|
#endif
|
|
|
|
int sock, has_symlinks;
|
|
|
|
struct linklist *file_list, *p;
|
1998-09-13 14:40:43 +04:00
|
|
|
struct direntry *fe;
|
1998-02-27 07:54:42 +03:00
|
|
|
char buffer[8192];
|
1998-09-13 14:40:43 +04:00
|
|
|
struct dir *dcache;
|
1998-02-27 07:54:42 +03:00
|
|
|
int got_intr = 0;
|
|
|
|
int has_spaces = (strchr (remote_path, ' ') != NULL);
|
|
|
|
|
1998-09-15 23:41:22 +04:00
|
|
|
canonicalize_pathname (remote_path);
|
1998-02-27 07:54:42 +03:00
|
|
|
for (p = qdcache(bucket)->next;p != qdcache(bucket);
|
|
|
|
p = p->next) {
|
|
|
|
dcache = p->data;
|
|
|
|
if (strcmp(dcache->remote_path, remote_path) == 0) {
|
|
|
|
struct timeval tim;
|
|
|
|
|
|
|
|
gettimeofday(&tim, NULL);
|
1998-10-23 00:17:49 +04:00
|
|
|
if (((tim.tv_sec < dcache->timestamp.tv_sec) && !force_expiration)
|
|
|
|
|| dcache->symlink_status == FTPFS_RESOLVING_SYMLINKS) {
|
1998-09-15 23:41:22 +04:00
|
|
|
if (resolve_symlinks && dcache->symlink_status == FTPFS_UNRESOLVED_SYMLINKS)
|
|
|
|
resolve_symlink(bucket, dcache);
|
1998-02-27 07:54:42 +03:00
|
|
|
return dcache;
|
1998-09-15 23:41:22 +04:00
|
|
|
} else {
|
1998-02-27 07:54:42 +03:00
|
|
|
force_expiration = 0;
|
|
|
|
p->next->prev = p->prev;
|
|
|
|
p->prev->next = p->next;
|
1998-09-13 14:40:43 +04:00
|
|
|
dir_destructor(dcache);
|
1998-02-27 07:54:42 +03:00
|
|
|
free (p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
has_symlinks = 0;
|
1998-09-15 23:41:22 +04:00
|
|
|
if (bucket->strict_rfc959_list_cmd)
|
1998-10-28 19:23:03 +03:00
|
|
|
print_vfs_message("ftpfs: Reading FTP directory %s... (don't use UNIX ls options)", remote_path);
|
1998-09-15 23:41:22 +04:00
|
|
|
else
|
1998-10-28 19:23:03 +03:00
|
|
|
print_vfs_message("ftpfs: Reading FTP directory %s...", remote_path);
|
1998-09-15 23:41:22 +04:00
|
|
|
if (has_spaces || bucket->strict_rfc959_list_cmd)
|
1998-10-14 06:56:18 +04:00
|
|
|
if (ftpfs_chdir_internal (bucket, remote_path) != COMPLETE) {
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = ENOENT;
|
1998-02-27 07:54:42 +03:00
|
|
|
print_vfs_message("ftpfs: CWD failed.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_list = linklist_init();
|
1999-01-11 03:48:23 +03:00
|
|
|
if (file_list == NULL)
|
|
|
|
ERRNOR (ENOMEM, NULL);
|
1998-09-13 14:40:43 +04:00
|
|
|
dcache = xmalloc(sizeof(struct dir),
|
|
|
|
"struct dir");
|
1998-02-27 07:54:42 +03:00
|
|
|
if (dcache == NULL) {
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = ENOMEM;
|
1998-02-27 07:54:42 +03:00
|
|
|
linklist_destroy(file_list, NULL);
|
|
|
|
print_vfs_message("ftpfs: FAIL");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
gettimeofday(&dcache->timestamp, NULL);
|
|
|
|
dcache->timestamp.tv_sec += ftpfs_directory_timeout;
|
|
|
|
dcache->file_list = file_list;
|
|
|
|
dcache->remote_path = strdup(remote_path);
|
|
|
|
dcache->count = 1;
|
1998-09-15 23:41:22 +04:00
|
|
|
dcache->symlink_status = FTPFS_NO_SYMLINKS;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-09-15 23:41:22 +04:00
|
|
|
if (bucket->strict_rfc959_list_cmd == 1)
|
1998-10-13 02:07:53 +04:00
|
|
|
sock = open_data_connection (bucket, "LIST", 0, TYPE_ASCII, 0);
|
1998-09-15 23:41:22 +04:00
|
|
|
else if (has_spaces)
|
1998-10-13 02:07:53 +04:00
|
|
|
sock = open_data_connection (bucket, "LIST -la", ".", TYPE_ASCII, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
else {
|
1998-06-23 12:23:05 +04:00
|
|
|
char *path = copy_strings (remote_path, PATH_SEP_STR, ".", (char *) 0);
|
1998-10-13 02:07:53 +04:00
|
|
|
sock = open_data_connection (bucket, "LIST -la", path, TYPE_ASCII, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
free (path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock == -1)
|
1998-10-28 19:39:53 +03:00
|
|
|
goto fallback;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
#ifdef OLD_READ
|
|
|
|
#define close_this_sock(x,y) fclose (x)
|
|
|
|
fp = fdopen(sock, "r");
|
|
|
|
if (fp == NULL) {
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = errno;
|
1998-02-27 07:54:42 +03:00
|
|
|
close(sock);
|
|
|
|
goto error_2;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define close_this_sock(x,y) close (y)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Clear the interrupt flag */
|
|
|
|
enable_interrupt_key ();
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
#ifdef OLD_READ
|
|
|
|
while (fgets (buffer, sizeof (buffer), fp) != NULL) {
|
|
|
|
if (got_intr = got_interrupt ())
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
while ((got_intr = get_line_interruptible (buffer, sizeof (buffer), sock)) != EINTR){
|
|
|
|
#endif
|
|
|
|
int eof = got_intr == 0;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
if (buffer [0] == 0 && eof)
|
|
|
|
break;
|
1998-09-13 14:40:43 +04:00
|
|
|
fe = xmalloc(sizeof(struct direntry), "struct direntry");
|
1998-02-27 07:54:42 +03:00
|
|
|
fe->freshly_created = 0;
|
1998-09-23 15:37:29 +04:00
|
|
|
fe->local_filename = NULL;
|
1998-02-27 07:54:42 +03:00
|
|
|
if (fe == NULL) {
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = ENOMEM;
|
1998-02-27 07:54:42 +03:00
|
|
|
goto error_1;
|
|
|
|
}
|
1998-10-13 02:07:53 +04:00
|
|
|
if (vfs_parse_ls_lga (buffer, &fe->s, &fe->name, &fe->linkname)) {
|
1998-02-27 07:54:42 +03:00
|
|
|
fe->count = 1;
|
|
|
|
fe->local_filename = fe->remote_filename = NULL;
|
|
|
|
fe->l_stat = NULL;
|
|
|
|
fe->bucket = bucket;
|
|
|
|
(fe->s).st_ino = bucket->__inode_counter++;
|
|
|
|
if (S_ISLNK(fe->s.st_mode))
|
|
|
|
has_symlinks = 1;
|
|
|
|
|
|
|
|
if (!linklist_insert(file_list, fe)) {
|
|
|
|
free(fe);
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = ENOMEM;
|
1998-02-27 07:54:42 +03:00
|
|
|
goto error_1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
free(fe);
|
|
|
|
if (eof)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (got_intr){
|
|
|
|
disable_interrupt_key();
|
|
|
|
print_vfs_message("ftpfs: reading FTP directory interrupt by user");
|
|
|
|
#ifdef OLD_READ
|
1998-09-21 13:52:07 +04:00
|
|
|
my_abort(bucket, fileno(fp));
|
1998-02-27 07:54:42 +03:00
|
|
|
#else
|
1998-09-21 13:52:07 +04:00
|
|
|
my_abort(bucket, sock);
|
1998-02-27 07:54:42 +03:00
|
|
|
#endif
|
|
|
|
close_this_sock(fp, sock);
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = EINTR;
|
1998-02-27 07:54:42 +03:00
|
|
|
goto error_3;
|
|
|
|
}
|
|
|
|
close_this_sock(fp, sock);
|
|
|
|
disable_interrupt_key();
|
1998-10-28 19:39:53 +03:00
|
|
|
if ( (get_reply (qsock (bucket), NULL, 0) != COMPLETE) || (file_list->next == file_list))
|
|
|
|
goto fallback;
|
1998-02-27 07:54:42 +03:00
|
|
|
if (!linklist_insert(qdcache(bucket), dcache)) {
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = ENOMEM;
|
1998-02-27 07:54:42 +03:00
|
|
|
goto error_3;
|
|
|
|
}
|
1998-09-15 23:41:22 +04:00
|
|
|
if (has_symlinks) {
|
|
|
|
if (resolve_symlinks)
|
|
|
|
resolve_symlink(bucket, dcache);
|
|
|
|
else
|
|
|
|
dcache->symlink_status = FTPFS_UNRESOLVED_SYMLINKS;
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
print_vfs_message("ftpfs: got listing");
|
|
|
|
return dcache;
|
|
|
|
error_1:
|
|
|
|
disable_interrupt_key();
|
|
|
|
close_this_sock(fp, sock);
|
|
|
|
#ifdef OLD_READ
|
|
|
|
error_2:
|
|
|
|
#endif
|
1998-09-13 14:40:43 +04:00
|
|
|
get_reply(qsock(bucket), NULL, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
error_3:
|
|
|
|
free(dcache->remote_path);
|
|
|
|
free(dcache);
|
1998-09-13 14:40:43 +04:00
|
|
|
linklist_destroy(file_list, direntry_destructor);
|
1998-02-27 07:54:42 +03:00
|
|
|
print_vfs_message("ftpfs: failed");
|
|
|
|
return NULL;
|
1998-10-28 19:39:53 +03:00
|
|
|
|
|
|
|
fallback:
|
|
|
|
if (bucket->__inode_counter == 0 && (!bucket->strict_rfc959_list_cmd)) {
|
|
|
|
/* It's our first attempt to get a directory listing from this
|
|
|
|
server (UNIX style LIST command) */
|
|
|
|
bucket->strict_rfc959_list_cmd = 1;
|
|
|
|
free(dcache->remote_path);
|
|
|
|
free(dcache);
|
|
|
|
linklist_destroy(file_list, direntry_destructor);
|
|
|
|
return retrieve_dir (bucket, remote_path, resolve_symlinks);
|
|
|
|
}
|
|
|
|
my_errno = EACCES;
|
|
|
|
free(dcache->remote_path);
|
|
|
|
free(dcache);
|
|
|
|
linklist_destroy(file_list, direntry_destructor);
|
|
|
|
print_vfs_message("ftpfs: failed; nowhere to fallback to");
|
|
|
|
return NULL;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1998-09-13 14:40:43 +04:00
|
|
|
store_file(struct direntry *fe)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
int local_handle, sock, n, total;
|
|
|
|
#ifdef HAVE_STRUCT_LINGER
|
|
|
|
struct linger li;
|
|
|
|
#else
|
|
|
|
int flag_one = 1;
|
|
|
|
#endif
|
|
|
|
char buffer[8192];
|
|
|
|
struct stat s;
|
|
|
|
|
|
|
|
local_handle = open(fe->local_filename, O_RDONLY);
|
|
|
|
unlink (fe->local_filename);
|
1999-01-11 03:48:23 +03:00
|
|
|
if (local_handle == -1)
|
|
|
|
ERRNOR (EIO, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
fstat(local_handle, &s);
|
1998-10-13 02:07:53 +04:00
|
|
|
sock = open_data_connection(fe->bucket, "STOR", fe->remote_filename, TYPE_BINARY, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (sock < 0) {
|
|
|
|
close(local_handle);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#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) {
|
|
|
|
while ((n = read(local_handle, buffer, sizeof(buffer))) < 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;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
print_vfs_message("ftpfs: storing file %d (%d)",
|
|
|
|
total, s.st_size);
|
|
|
|
}
|
|
|
|
disable_interrupt_key();
|
|
|
|
close(sock);
|
|
|
|
close(local_handle);
|
1999-01-11 03:48:23 +03:00
|
|
|
if (get_reply (qsock (fe->bucket), NULL, 0) != COMPLETE)
|
|
|
|
ERRNOR (EIO, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
return 1;
|
|
|
|
error_return:
|
|
|
|
disable_interrupt_key();
|
|
|
|
close(sock);
|
|
|
|
close(local_handle);
|
1998-09-13 14:40:43 +04:00
|
|
|
get_reply(qsock(fe->bucket), NULL, 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-09-21 13:52:07 +04:00
|
|
|
static int
|
1998-10-13 02:07:53 +04:00
|
|
|
linear_start(struct direntry *fe, int offset)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1998-09-21 13:52:07 +04:00
|
|
|
fe->local_stat.st_mtime = 0;
|
1998-10-13 02:07:53 +04:00
|
|
|
fe->data_sock = open_data_connection(fe->bucket, "RETR", fe->remote_filename, TYPE_BINARY, offset);
|
1998-09-21 13:52:07 +04:00
|
|
|
if (fe->data_sock == -1)
|
|
|
|
ERRNOR (EACCES, 0);
|
1998-10-13 02:07:53 +04:00
|
|
|
fe->linear_state = LS_LINEAR_OPEN;
|
1998-02-27 07:54:42 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
static void
|
1998-09-21 13:52:07 +04:00
|
|
|
linear_abort (struct direntry *fe)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1998-09-21 13:52:07 +04:00
|
|
|
my_abort(fe->bucket, fe->data_sock);
|
|
|
|
fe->data_sock = -1;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-09-21 13:52:07 +04:00
|
|
|
static int
|
|
|
|
linear_read (struct direntry *fe, void *buf, int len)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1998-09-21 13:52:07 +04:00
|
|
|
int n;
|
|
|
|
while ((n = read (fe->data_sock, buf, len))<0) {
|
|
|
|
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)
|
|
|
|
linear_abort(fe);
|
1998-09-21 13:52:07 +04:00
|
|
|
|
|
|
|
if (!n) {
|
|
|
|
if ((get_reply (qsock (fe->bucket), NULL, 0) != COMPLETE)) {
|
|
|
|
my_errno = EIO;
|
|
|
|
n=-1;
|
|
|
|
}
|
1998-10-15 19:18:12 +04:00
|
|
|
close (fe->data_sock);
|
1998-09-21 13:52:07 +04:00
|
|
|
fe->data_sock = -1;
|
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
|
1998-09-21 13:52:07 +04:00
|
|
|
linear_close (struct direntry *fe)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1998-09-21 13:52:07 +04:00
|
|
|
if (fe->data_sock != -1)
|
|
|
|
linear_abort(fe);
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-09-21 13:52:07 +04:00
|
|
|
int ftpfs_ctl (void *data, int ctlop, int arg)
|
|
|
|
{
|
|
|
|
struct filp *fp = data;
|
|
|
|
switch (ctlop) {
|
|
|
|
case MCCTL_IS_NOTREADY:
|
|
|
|
{
|
1998-10-13 02:07:53 +04:00
|
|
|
int v;
|
1998-09-21 13:52:07 +04:00
|
|
|
|
1998-10-13 02:07:53 +04:00
|
|
|
if (!fp->fe->linear_state)
|
|
|
|
vfs_die ("You may not do this");
|
|
|
|
if (fp->fe->linear_state == LS_LINEAR_CLOSED)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
v = select_on_two (fp->fe->data_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
send_ftp_command(char *filename, char *cmd, int flags)
|
|
|
|
{
|
|
|
|
char *remote_path;
|
1998-09-13 14:40:43 +04:00
|
|
|
struct connection *bucket;
|
1998-02-27 07:54:42 +03:00
|
|
|
int r;
|
1998-09-13 14:40:43 +04:00
|
|
|
int flush_directory_cache = (flags & OPT_FLUSH) && (normal_flush > 0);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
if (!(remote_path = get_path(&bucket, filename)))
|
1998-02-27 07:54:42 +03:00
|
|
|
return -1;
|
|
|
|
r = command (bucket, WAIT_REPLY, cmd, remote_path);
|
|
|
|
free(remote_path);
|
1998-10-13 02:07:53 +04:00
|
|
|
vfs_add_noncurrent_stamps (&vfs_ftpfs_ops, (vfsid) bucket, 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)
|
|
|
|
flush_all_directory(bucket);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This routine is called as the last step in load_setup */
|
|
|
|
void
|
|
|
|
ftpfs_init_passwd(void)
|
|
|
|
{
|
|
|
|
struct passwd *passwd_info;
|
|
|
|
char *p, hostname[MAXHOSTNAMELEN];
|
|
|
|
struct hostent *hp;
|
|
|
|
|
|
|
|
ftpfs_anonymous_passwd = load_anon_passwd ();
|
|
|
|
if (ftpfs_anonymous_passwd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((passwd_info = getpwuid (geteuid ())) == NULL)
|
1998-10-23 12:26:25 +04:00
|
|
|
p = "unknown";
|
1998-02-27 07:54:42 +03:00
|
|
|
else
|
|
|
|
p = passwd_info->pw_name;
|
|
|
|
gethostname(hostname, sizeof(hostname));
|
|
|
|
hp = gethostbyname(hostname);
|
|
|
|
if (hp != NULL)
|
|
|
|
ftpfs_anonymous_passwd = copy_strings (p, "@", hp->h_name, NULL);
|
|
|
|
else
|
|
|
|
ftpfs_anonymous_passwd = copy_strings (p, "@", hostname, NULL);
|
|
|
|
endpwent ();
|
|
|
|
}
|
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
int
|
|
|
|
ftpfs_init (vfs *me)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1998-09-13 14:40:43 +04:00
|
|
|
connections_list = linklist_init();
|
1998-02-27 07:54:42 +03:00
|
|
|
ftpfs_directory_timeout = FTPFS_DIRECTORY_TIMEOUT;
|
1998-09-27 23:27:58 +04:00
|
|
|
return 1;
|
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
|
|
|
{
|
|
|
|
char buf[40];
|
|
|
|
|
|
|
|
sprintf(buf, "SITE CHMOD %4.4o %%s", mode & 07777);
|
|
|
|
return send_ftp_command(path, buf, OPT_IGNORE_ERROR | OPT_FLUSH);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
1998-09-13 14:40:43 +04:00
|
|
|
return send_ftp_command(path, "DELE %s", OPT_FLUSH);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
/* Return true if path is the same directoy as the one we are on now */
|
|
|
|
static int
|
|
|
|
is_same_dir (char *path, struct connection *bucket)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1998-09-13 14:40:43 +04:00
|
|
|
if (!qcdir (bucket))
|
|
|
|
return 0;
|
|
|
|
if (strcmp (path, qcdir (bucket)) == 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1998-10-14 06:56:18 +04:00
|
|
|
ftpfs_chdir_internal (struct connection *bucket ,char *remote_path)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
if (!bucket->cwd_defered && is_same_dir (remote_path, bucket))
|
1998-02-27 07:54:42 +03:00
|
|
|
return COMPLETE;
|
|
|
|
|
|
|
|
r = command (bucket, WAIT_REPLY, "CWD %s", remote_path);
|
|
|
|
if (r != COMPLETE) {
|
1998-09-13 14:40:43 +04:00
|
|
|
my_errno = EIO;
|
1998-02-27 07:54:42 +03:00
|
|
|
} else {
|
|
|
|
if (qcdir(bucket))
|
|
|
|
free(qcdir(bucket));
|
|
|
|
qcdir(bucket) = strdup (remote_path);
|
1998-05-10 17:41:43 +04:00
|
|
|
bucket->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
|
|
|
{
|
1998-09-13 14:40:43 +04:00
|
|
|
send_ftp_command(path1, "RNFR %s", OPT_FLUSH);
|
|
|
|
return send_ftp_command(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
|
|
|
{
|
1998-09-13 14:40:43 +04:00
|
|
|
return send_ftp_command(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
|
|
|
{
|
1998-09-13 14:40:43 +04:00
|
|
|
return send_ftp_command(path, "RMD %s", OPT_FLUSH);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ftpfs_set_debug (char *file)
|
|
|
|
{
|
1998-09-13 14:40:43 +04:00
|
|
|
logfile = fopen (file, "w+");
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
static void my_forget (char *file)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
struct linklist *l;
|
|
|
|
char *host, *user, *pass, *rp;
|
|
|
|
int port;
|
|
|
|
|
1998-06-01 16:52:18 +04:00
|
|
|
#ifndef BROKEN_PATHS
|
|
|
|
if (strncmp (file, "/#ftp:", 6))
|
1998-06-10 18:57:19 +04:00
|
|
|
return; /* Normal: consider cd /bla/#ftp */
|
1998-06-01 16:52:18 +04:00
|
|
|
#else
|
|
|
|
if (!(file = strstr (file, "/#ftp:")))
|
1998-06-10 18:57:19 +04:00
|
|
|
return;
|
1998-06-01 16:52:18 +04:00
|
|
|
#endif
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
file += 6;
|
1998-09-13 14:40:43 +04:00
|
|
|
if (!(rp = my_get_host_and_username (file, &host, &user, &port, &pass))) {
|
1998-02-27 07:54:42 +03:00
|
|
|
free (host);
|
|
|
|
free (user);
|
|
|
|
if (pass)
|
|
|
|
wipe_password (pass);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we do not care about the path actually */
|
|
|
|
free (rp);
|
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
for (l = connections_list->next; l != connections_list; l = l->next){
|
|
|
|
struct connection *bucket = l->data;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
if ((strcmp (host, qhost (bucket)) == 0) &&
|
|
|
|
(strcmp (user, quser (bucket)) == 0) &&
|
|
|
|
(port == qport (bucket))){
|
|
|
|
|
|
|
|
/* close socket: the child owns it now */
|
|
|
|
close (bucket->sock);
|
|
|
|
bucket->sock = -1;
|
|
|
|
|
|
|
|
/* reopen the connection */
|
|
|
|
bucket->sock = ftpfs_open_socket (bucket);
|
|
|
|
if (bucket->sock != -1)
|
|
|
|
login_server (bucket, pass);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (host);
|
|
|
|
free (user);
|
|
|
|
if (pass)
|
|
|
|
wipe_password (pass);
|
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
"File Tranfer Protocol (ftp)",
|
|
|
|
F_NET, /* flags */
|
|
|
|
"ftp:", /* prefix */
|
|
|
|
NULL, /* data */
|
|
|
|
0, /* errno */
|
|
|
|
ftpfs_init,
|
|
|
|
ftpfs_done,
|
|
|
|
ftpfs_fill_names,
|
|
|
|
NULL,
|
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
s_open,
|
|
|
|
s_close,
|
|
|
|
s_read,
|
|
|
|
s_write,
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
s_opendir,
|
|
|
|
s_readdir,
|
|
|
|
s_closedir,
|
|
|
|
s_telldir,
|
|
|
|
s_seekdir,
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
s_stat,
|
|
|
|
s_lstat,
|
|
|
|
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,
|
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
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,
|
|
|
|
s_chdir,
|
|
|
|
s_errno,
|
|
|
|
s_lseek,
|
1998-08-31 14:05:42 +04:00
|
|
|
NULL,
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
s_getid,
|
|
|
|
s_nothingisopen,
|
|
|
|
s_free,
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-09-13 14:40:43 +04:00
|
|
|
s_getlocalcopy,
|
|
|
|
s_ungetlocalcopy,
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
ftpfs_mkdir,
|
|
|
|
ftpfs_rmdir,
|
|
|
|
ftpfs_ctl,
|
1998-09-27 23:27:58 +04:00
|
|
|
s_setctl
|
1998-10-13 02:07:53 +04:00
|
|
|
|
|
|
|
MMAPNULL
|
1998-02-27 07:54:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef USE_NETRC
|
|
|
|
static char buffer[100];
|
|
|
|
static char *netrc, *netrcp;
|
|
|
|
|
|
|
|
static int netrc_next (void)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int i;
|
|
|
|
static char *keywords [] = { "default", "machine",
|
|
|
|
"login", "password", "passwd",
|
|
|
|
"account", "macdef" };
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
netrcp = skip_separators (netrcp);
|
|
|
|
if (*netrcp != '\n')
|
|
|
|
break;
|
|
|
|
netrcp++;
|
|
|
|
}
|
|
|
|
if (!*netrcp)
|
|
|
|
return 0;
|
|
|
|
p = buffer;
|
|
|
|
if (*netrcp == '"') {
|
|
|
|
for (;*netrcp != '"' && *netrcp; netrcp++) {
|
|
|
|
if (*netrcp == '\\')
|
|
|
|
netrcp++;
|
|
|
|
*p++ = *netrcp;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (;*netrcp != '\n' && *netrcp != '\t' && *netrcp != ' ' &&
|
|
|
|
*netrcp != ',' && *netrcp; netrcp++) {
|
|
|
|
if (*netrcp == '\\')
|
|
|
|
netrcp++;
|
|
|
|
*p++ = *netrcp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
if (!*buffer)
|
|
|
|
return 0;
|
|
|
|
for (i = 0; i < sizeof (keywords) / sizeof (keywords [0]); i++)
|
|
|
|
if (!strcmp (keywords [i], buffer))
|
|
|
|
break;
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int lookup_netrc (char *host, char **login, char **pass)
|
|
|
|
{
|
|
|
|
char *netrcname, *tmp;
|
|
|
|
char hostname[MAXHOSTNAMELEN], *domain;
|
|
|
|
int keyword;
|
|
|
|
struct stat mystat;
|
|
|
|
static int be_angry = 1;
|
|
|
|
static struct rupcache {
|
|
|
|
struct rupcache *next;
|
|
|
|
char *host;
|
|
|
|
char *login;
|
|
|
|
char *pass;
|
|
|
|
} *rup_cache = NULL, *rupp;
|
|
|
|
|
|
|
|
for (rupp = rup_cache; rupp != NULL; rupp = rupp->next)
|
|
|
|
if (!strcmp (host, rupp->host)) {
|
|
|
|
if (rupp->login != NULL)
|
|
|
|
*login = strdup (rupp->login);
|
|
|
|
if (rupp->pass != NULL)
|
|
|
|
*pass = strdup (rupp->pass);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
netrcname = xmalloc (strlen (home_dir) + strlen ("/.netrc") + 1, "netrc");
|
|
|
|
strcpy (netrcname, home_dir);
|
|
|
|
strcat (netrcname, "/.netrc");
|
|
|
|
netrcp = netrc = load_file (netrcname);
|
|
|
|
if (netrc == NULL) {
|
|
|
|
free (netrcname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (gethostname (hostname, sizeof (hostname)) < 0)
|
|
|
|
*hostname = 0;
|
|
|
|
if (!(domain = strchr (hostname, '.')))
|
|
|
|
domain = "";
|
|
|
|
|
|
|
|
while ((keyword = netrc_next ())) {
|
|
|
|
if (keyword == 2) {
|
|
|
|
if (netrc_next () != 8)
|
|
|
|
continue;
|
|
|
|
if (strcasecmp (host, buffer) &&
|
|
|
|
((tmp = strchr (host, '.')) == NULL ||
|
|
|
|
strcasecmp (tmp, domain) ||
|
|
|
|
strncasecmp (host, buffer, tmp - host) ||
|
|
|
|
buffer [tmp - host]))
|
|
|
|
continue;
|
|
|
|
} else if (keyword != 1)
|
|
|
|
continue;
|
|
|
|
while ((keyword = netrc_next ()) > 2) {
|
|
|
|
switch (keyword) {
|
|
|
|
case 3:
|
|
|
|
if (netrc_next ())
|
|
|
|
if (*login == NULL)
|
|
|
|
*login = strdup (buffer);
|
|
|
|
else if (strcmp (*login, buffer))
|
|
|
|
keyword = 20;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
if (strcmp (*login, "anonymous") && strcmp (*login, "ftp") &&
|
|
|
|
stat (netrcname, &mystat) >= 0 &&
|
|
|
|
(mystat.st_mode & 077)) {
|
|
|
|
if (be_angry) {
|
1998-05-26 04:53:24 +04:00
|
|
|
message_1s (1, MSG_ERROR, _("~/.netrc file has not correct mode.\n"
|
|
|
|
"Remove password or correct mode."));
|
1998-02-27 07:54:42 +03:00
|
|
|
be_angry = 0;
|
|
|
|
}
|
|
|
|
free (netrc);
|
|
|
|
free (netrcname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (netrc_next () && *pass == NULL)
|
|
|
|
*pass = strdup (buffer);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
if (stat (netrcname, &mystat) >= 0 &&
|
|
|
|
(mystat.st_mode & 077)) {
|
|
|
|
if (be_angry) {
|
1998-05-26 04:53:24 +04:00
|
|
|
message_1s (1, MSG_ERROR, _("~/.netrc file has not correct mode.\n"
|
|
|
|
"Remove password or correct mode."));
|
1998-02-27 07:54:42 +03:00
|
|
|
be_angry = 0;
|
|
|
|
}
|
|
|
|
free (netrc);
|
|
|
|
free (netrcname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
netrc_next ();
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
for (;;) {
|
|
|
|
while (*netrcp != '\n' && *netrcp);
|
|
|
|
if (*netrcp != '\n')
|
|
|
|
break;
|
|
|
|
netrcp++;
|
|
|
|
if (*netrcp == '\n' || !*netrcp)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (keyword == 20)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (keyword == 20)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rupp = (struct rupcache *) xmalloc (sizeof (struct rupcache), "");
|
|
|
|
rupp->host = strdup (host);
|
|
|
|
rupp->login = rupp->pass = 0;
|
|
|
|
|
|
|
|
if (*login != NULL)
|
|
|
|
rupp->login = strdup (*login);
|
|
|
|
if (*pass != NULL)
|
|
|
|
rupp->pass = strdup (*pass);
|
|
|
|
rupp->next = rup_cache;
|
|
|
|
rup_cache = rupp;
|
|
|
|
|
|
|
|
free (netrc);
|
|
|
|
free (netrcname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef HAVE_STRNCASECMP
|
|
|
|
int strncasecmp (char *s, char *d, int l)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
while (l--){
|
|
|
|
if (result = (0x20 | *s) - (0x20 | *d))
|
|
|
|
break;
|
|
|
|
if (!*s)
|
|
|
|
return 0;
|
|
|
|
s++;
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* USE_NETRC */
|