* ftpfs.c: Introduce enum keyword_t to improve readability.

(netrc_next): Fix return value for unknown keywords.
(lookup_netrc): Remove special processing of "*netrc*".
This commit is contained in:
Pavel Roskin 2002-07-12 20:00:03 +00:00
parent 1accbb4789
commit 14bac1dda4
2 changed files with 65 additions and 29 deletions

View File

@ -1,5 +1,9 @@
2002-07-12 Pavel Roskin <proski@gnu.org> 2002-07-12 Pavel Roskin <proski@gnu.org>
* ftpfs.c: Introduce enum keyword_t to improve readability.
(netrc_next): Fix return value for unknown keywords.
(lookup_netrc): Remove special processing of "*netrc*".
* ftpfs.c (ftp_split_url): Move protection against pass being * ftpfs.c (ftp_split_url): Move protection against pass being
NULL ... NULL ...
(lookup_netrc): ... here. (lookup_netrc): ... here.

View File

@ -162,7 +162,7 @@ static int command (vfs *me, vfs_s_super *super, int wait_reply, char *fmt, ...)
__attribute__ ((format (printf, 4, 5))); __attribute__ ((format (printf, 4, 5)));
static int ftpfs_open_socket (vfs *me, vfs_s_super *super); 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 login_server (vfs *me, vfs_s_super *super, const char *netrcpass);
static int lookup_netrc (char *host, char **login, char **pass); static int lookup_netrc (const char *host, char **login, char **pass);
static char * static char *
translate_path (vfs *me, vfs_s_super *super, const char *remote_path) translate_path (vfs *me, vfs_s_super *super, const char *remote_path)
@ -1819,12 +1819,26 @@ void ftpfs_set_debug (const char *file)
static char buffer[BUF_MEDIUM]; static char buffer[BUF_MEDIUM];
static char *netrc, *netrcp; static char *netrc, *netrcp;
static int netrc_next (void) /* 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,
NETRC_UNKNOWN,
NETRC_BREAK = 20
} keyword_t;
static keyword_t netrc_next (void)
{ {
char *p; char *p;
int i; keyword_t i;
static const char * const keywords [] = { "default", "machine", static const char * const keywords [] = { "default", "machine",
"login", "password", "passwd", "account", "macdef" }; "login", "password", "passwd", "account", "macdef", NULL };
while (1) { while (1) {
@ -1834,7 +1848,7 @@ static int netrc_next (void)
netrcp++; netrcp++;
} }
if (!*netrcp) if (!*netrcp)
return 0; return NETRC_NONE;
p = buffer; p = buffer;
if (*netrcp == '"') { if (*netrcp == '"') {
for (netrcp++; *netrcp != '"' && *netrcp; netrcp++) { for (netrcp++; *netrcp != '"' && *netrcp; netrcp++) {
@ -1853,10 +1867,16 @@ static int netrc_next (void)
*p = 0; *p = 0;
if (!*buffer) if (!*buffer)
return 0; return 0;
for (i = 0; i < sizeof (keywords) / sizeof (keywords [0]); i++)
if (!strcmp (keywords [i], buffer)) i = NETRC_DEFAULT;
break; while (keywords [i - 1]) {
return i + 1; if (!strcmp (keywords [i - 1], buffer))
return i;
i++;
}
return NETRC_UNKNOWN;
} }
static int netrc_has_incorrect_mode (char * netrcname, char * netrc) static int netrc_has_incorrect_mode (char * netrcname, char * netrc)
@ -1877,12 +1897,13 @@ static int netrc_has_incorrect_mode (char * netrcname, char * netrc)
return 0; return 0;
} }
static int lookup_netrc (char *host, char **login, char **pass) /* Extract login and password from .netrc for the host */
static int lookup_netrc (const char *host, char **login, char **pass)
{ {
char *netrcname; char *netrcname;
char *tmp_pass = NULL; char *tmp_pass = NULL;
char hostname[MAXHOSTNAMELEN], *domain; char hostname[MAXHOSTNAMELEN], *domain;
int keyword; keyword_t keyword;
static struct rupcache { static struct rupcache {
struct rupcache *next; struct rupcache *next;
char *host; char *host;
@ -1890,7 +1911,8 @@ static int lookup_netrc (char *host, char **login, char **pass)
char *pass; char *pass;
} *rup_cache = NULL, *rupp; } *rup_cache = NULL, *rupp;
for (rupp = rup_cache; rupp != NULL; rupp = rupp->next) /* Look up in the cache first */
for (rupp = rup_cache; rupp != NULL; rupp = rupp->next) {
/* return from cache only if host AND user match! */ /* return from cache only if host AND user match! */
if ((!strcmp (host, rupp->host)) && if ((!strcmp (host, rupp->host)) &&
(rupp->login != NULL) && (rupp->login != NULL) &&
@ -1901,22 +1923,25 @@ static int lookup_netrc (char *host, char **login, char **pass)
*pass = g_strdup (rupp->pass); *pass = g_strdup (rupp->pass);
return 0; return 0;
} }
}
netrcname = concat_dir_and_file (home_dir, ".netrc"); netrcname = concat_dir_and_file (home_dir, ".netrc");
netrcp = netrc = load_file (netrcname); netrcp = netrc = load_file (netrcname);
if (netrc == NULL) { if (netrc == NULL) {
g_free (netrcname); g_free (netrcname);
return 0; return 0;
} }
if (gethostname (hostname, sizeof (hostname)) < 0) if (gethostname (hostname, sizeof (hostname)) < 0)
*hostname = 0; *hostname = 0;
if (!(domain = strchr (hostname, '.'))) if (!(domain = strchr (hostname, '.')))
domain = ""; domain = "";
while ((keyword = netrc_next ())) { while ((keyword = netrc_next ())) {
if (keyword == 2) { if (keyword == NETRC_MACHINE) {
char *tmp; char *tmp;
if (netrc_next () != 8) if (netrc_next () != NETRC_UNKNOWN)
continue; continue;
if (g_strcasecmp (host, buffer) && if (g_strcasecmp (host, buffer) &&
((tmp = strchr (host, '.')) == NULL || ((tmp = strchr (host, '.')) == NULL ||
@ -1924,21 +1949,22 @@ static int lookup_netrc (char *host, char **login, char **pass)
g_strncasecmp (host, buffer, tmp - host) || g_strncasecmp (host, buffer, tmp - host) ||
buffer [tmp - host])) buffer [tmp - host]))
continue; continue;
} else if (keyword != 1) } else if (keyword != NETRC_DEFAULT)
continue; continue;
while ((keyword = netrc_next ()) > 2) {
while ((keyword = netrc_next ()) > NETRC_MACHINE) {
switch (keyword) { switch (keyword) {
case 3: case NETRC_LOGIN:
if (netrc_next ()) { if (netrc_next ()) {
/* replace the dummy user with the one from ~/.netrc */ if (*login == NULL)
if ((*login == NULL) || !strcmp(*login, "*netrc*"))
*login = g_strdup (buffer); *login = g_strdup (buffer);
else if (strcmp (*login, buffer)) else if (strcmp (*login, buffer))
keyword = 20; keyword = NETRC_BREAK;
} }
break; break;
case 4:
case 5: case NETRC_PASSWORD:
case NETRC_PASSWD:
if (strcmp (*login, "anonymous") && strcmp (*login, "ftp") && if (strcmp (*login, "anonymous") && strcmp (*login, "ftp") &&
netrc_has_incorrect_mode (netrcname, netrc)) { netrc_has_incorrect_mode (netrcname, netrc)) {
return -1; return -1;
@ -1946,13 +1972,15 @@ static int lookup_netrc (char *host, char **login, char **pass)
if (netrc_next () && tmp_pass == NULL) if (netrc_next () && tmp_pass == NULL)
tmp_pass = g_strdup (buffer); tmp_pass = g_strdup (buffer);
break; break;
case 6:
case NETRC_ACCOUNT:
if (netrc_has_incorrect_mode (netrcname, netrc)) { if (netrc_has_incorrect_mode (netrcname, netrc)) {
return -1; return -1;
} }
netrc_next (); netrc_next ();
break; break;
case 7: /* macdef: skip it */
case NETRC_MACDEF: /* macdef: skip it */
do { do {
while (*netrcp && *netrcp != '\n') while (*netrcp && *netrcp != '\n')
netrcp++; netrcp++;
@ -1961,11 +1989,18 @@ static int lookup_netrc (char *host, char **login, char **pass)
netrcp++; netrcp++;
} while (*netrcp && *netrcp != '\n'); } while (*netrcp && *netrcp != '\n');
break; break;
}
if (keyword == 20) case NETRC_NONE:
case NETRC_DEFAULT:
case NETRC_MACHINE:
case NETRC_UNKNOWN:
case NETRC_BREAK:
break; break;
} }
if (keyword != 20) if (keyword == NETRC_BREAK)
break;
}
if (keyword != NETRC_BREAK)
break; break;
} }
@ -1977,9 +2012,6 @@ static int lookup_netrc (char *host, char **login, char **pass)
rupp->login = rupp->pass = 0; 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); rupp->login = g_strdup (*login);
} }
if (tmp_pass != NULL) if (tmp_pass != NULL)