diff --git a/vfs/ChangeLog b/vfs/ChangeLog index bca947e60..21f64b1dc 100644 --- a/vfs/ChangeLog +++ b/vfs/ChangeLog @@ -1,5 +1,12 @@ 2002-07-10 Pavel Roskin + * tcputil.h: Move use_netrc declaration ... + * vfs.h: ... here. + * ftpfs.c: Implement .netrc support unconditionally. + (lookup_netrc): Make static. + * utilvfs.c: Initialize default_is_netrc. + From Thomas Zajic + * direntry.c (vfs_s_open): Don't pass O_LINEAR to open() - this flag is for VFS only, and causes side effects in Cygwin. * extfs.c (extfs_open): Likewise. diff --git a/vfs/ftpfs.c b/vfs/ftpfs.c index 5ca42f364..1a1316c1d 100644 --- a/vfs/ftpfs.c +++ b/vfs/ftpfs.c @@ -162,6 +162,7 @@ static int command (vfs *me, vfs_s_super *super, int wait_reply, char *fmt, ...) __attribute__ ((format (printf, 4, 5))); static int ftpfs_open_socket (vfs *me, vfs_s_super *super); static int login_server (vfs *me, vfs_s_super *super, const char *netrcpass); +static int lookup_netrc (char *host, char **login, char **pass); static char * translate_path (vfs *me, vfs_s_super *super, const char *remote_path) @@ -814,6 +815,10 @@ open_archive (vfs *me, vfs_s_super *super, char *archive_name, char *op) super->root = vfs_s_new_inode (me, super, vfs_s_default_stat(me, S_IFDIR | 0755)); if (password) SUP.password = password; + /* try to get user and/or password from ~/.netrc */ + else if (use_netrc) + lookup_netrc(SUP.host, &SUP.user, &SUP.password); + return open_archive_int (me, super); } @@ -821,6 +826,7 @@ static int archive_same(vfs *me, vfs_s_super *super, char *archive_name, char *op, void *cookie) { char *host, *user; + char *pass = NULL; int port; op = vfs_split_url (strchr(op, ':')+1, &host, &user, &port, 0, 21, URL_DEFAULTANON); @@ -828,6 +834,10 @@ archive_same(vfs *me, vfs_s_super *super, char *archive_name, char *op, void *co if (op) g_free (op); + /* replace the dummy user with the one from ~/.netrc */ + if (use_netrc && !strcmp(user, "*netrc*")) + lookup_netrc(SUP.host, &user, &pass); + port = ((strcmp (host, SUP.host) == 0) && (strcmp (user, SUP.user) == 0) && (port == SUP.port)); @@ -1861,7 +1871,6 @@ void ftpfs_set_debug (const char *file) ftp_data.logfile = logfile; } -#ifdef USE_NETRC static char buffer[BUF_MEDIUM]; static char *netrc, *netrcp; @@ -1923,7 +1932,7 @@ static int netrc_has_incorrect_mode (char * netrcname, char * netrc) return 0; } -int lookup_netrc (char *host, char **login, char **pass) +static int lookup_netrc (char *host, char **login, char **pass) { char *netrcname, *tmp; char hostname[MAXHOSTNAMELEN], *domain; @@ -1936,13 +1945,16 @@ int lookup_netrc (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 = g_strdup (rupp->login); - if (rupp->pass != NULL) - *pass = g_strdup (rupp->pass); - return 0; - } + /* return from cache only if host AND user match! */ + if ((!strcmp (host, rupp->host)) && + (rupp->login != NULL) && + (*login != NULL) && + (!strcmp(rupp->login, *login))) { + *login = g_strdup (rupp->login); + if (rupp->pass != NULL) + *pass = g_strdup (rupp->pass); + return 0; + } netrcname = concat_dir_and_file (home_dir, ".netrc"); netrcp = netrc = load_file (netrcname); if (netrc == NULL) { @@ -1970,7 +1982,8 @@ int lookup_netrc (char *host, char **login, char **pass) switch (keyword) { case 3: if (netrc_next ()) { - if (*login == NULL) + /* replace the dummy user with the one from ~/.netrc */ + if ((*login == NULL) || !strcmp(*login, "*netrc*")) *login = g_strdup (buffer); else if (strcmp (*login, buffer)) keyword = 20; @@ -2015,8 +2028,12 @@ int lookup_netrc (char *host, char **login, char **pass) rupp->host = g_strdup (host); rupp->login = rupp->pass = 0; - if (*login != NULL) + if (*login != NULL) { + if (!strcmp(*login, "*netrc*")) + /* no match in ~/.netrc, try anonymous */ + *login = g_strdup("anonymous"); rupp->login = g_strdup (*login); + } if (*pass != NULL) rupp->pass = g_strdup (*pass); rupp->next = rup_cache; @@ -2024,5 +2041,3 @@ int lookup_netrc (char *host, char **login, char **pass) return 0; } - -#endif /* USE_NETRC */ diff --git a/vfs/ftpfs.h b/vfs/ftpfs.h index ddfb1f079..67a967f76 100644 --- a/vfs/ftpfs.h +++ b/vfs/ftpfs.h @@ -90,7 +90,6 @@ extern char *ftpfs_proxy_host; extern int ftpfs_directory_timeout; extern int ftpfs_always_use_proxy; -extern int use_netrc; extern int ftpfs_retry_seconds; extern int ftpfs_use_passive_connections; extern int ftpfs_use_unix_list_options; diff --git a/vfs/tcputil.h b/vfs/tcputil.h index 6b32efde4..1170478d9 100644 --- a/vfs/tcputil.h +++ b/vfs/tcputil.h @@ -25,5 +25,4 @@ char *get_host_and_username (char *path, char **host, char **user, int *port, int default_port, int default_to_anon, char **pass); extern int tcp_inited; -extern int use_netrc; extern int got_sigpipe; diff --git a/vfs/utilvfs.c b/vfs/utilvfs.c index 4455cf927..89dc6e9ae 100644 --- a/vfs/utilvfs.c +++ b/vfs/utilvfs.c @@ -69,6 +69,8 @@ char *vfs_split_url (const char *path, char **host, char **user, char *pcopy = g_strdup (path); char *pend = pcopy + strlen (pcopy); int default_is_anon = flags & URL_DEFAULTANON; + /* get user from ~/.netrc if we're supposed to */ + int default_is_netrc = use_netrc; if (pass) *pass = NULL; @@ -103,8 +105,11 @@ char *vfs_split_url (const char *path, char **host, char **user, } if (*pcopy != 0) *user = g_strdup (pcopy); - else + else { default_is_anon = 0; + /* don't lookup ~/.netrc, use login name instead */ + default_is_netrc = 0; + } if (pend == at+1) rest = at; @@ -113,6 +118,10 @@ char *vfs_split_url (const char *path, char **host, char **user, } else rest = pcopy; + /* dummy user to be replaced in lookup_netrc() in ftpfs.c */ + if (!*user && (default_is_netrc == 1)) + *user = g_strdup ("*netrc*"); + if (!*user){ if (default_is_anon) *user = g_strdup ("anonymous"); diff --git a/vfs/vfs.h b/vfs/vfs.h index 4ee493b74..eafd6bc18 100644 --- a/vfs/vfs.h +++ b/vfs/vfs.h @@ -167,6 +167,7 @@ #ifdef USE_NETCODE void ftpfs_hint_reread(int reread); void ftpfs_flushdir(void); + extern int use_netrc; #else # define ftpfs_flushdir() # define ftpfs_hint_reread(x)