Re: 4.5.99a (mc-2002-04-26-03) .netrc patch



* Pavel Roskin <proski gnu org>, 26/04/2002, 15:54

> Thanks for your efforts, but I cannot apply the patch is this form.
> First off, it causes a warning because lookup_netrc() is called before
> being decared.  If you declare it, you still get a warning because
> login_server() is called in assumption that the password won't change,
> and lookup_netrc() is changing it.

Fixed.

> I ignored the warning and tested the .netrc support.  It didn't work
> correctly.  If I just enter the hostname without the username, MC
> should still find the username in .netrc instead of logging in as
> "anonymous". That would be consistent with the command line ftp client.

Fixed (the problem was vfs_split_url() in utilvfs.c, which would always
return "anonymous" as a username if none was specified in the URL, no
matter whether "use_netrc" was set in ~/.mc/ini or not).

Now, if there are multiple login/password entries in ~/.netrc for the
same host, and you don't enter a login name at all (ie. ftp://host/),
login and password will be picked from ~/.netrc on a first-match-wins
basis. As a consequence, you'll have to explicitly add "anonymous" or
"ftp" to the URL as a login name now if you want anonymous FTP to a
host listed in ~/.netrc.

OTOH, if there are multiple login/password entries for the same host,
and you *do* enter a login name, only the corresponding password will
be picked from ~/.netrc.

> Also it seems to me from the name of the third argument to
> login_server(), "netrcpass", that the password from .netrc should be
> determined earlier and sent to login_server().

Fixed (it is now determined in open_archive(), if use_netrc is set).

> First of all, it's not trivial.  I don't know if anybody noticed, but
> I didn't.

Sorry if I came across like a complete jerk. I meant that my fix (ok,
make that "quick & dirty hack" ;-) was trivial, as in "almost *too*
easy, too good to be true". And I certainly didn't wonder why "nobody
was able to fix that trivial problem" or why "nobody noticed that
trivial fix for the problem", I only wondered why nobody noticed for
such a long time that there was a problem with ~/.netrc handling at
all.

> If you want to clean it up, please do, but don't ignore warnings and
> test your patch.

Done. I tried to keep changes to the original code at a minimum, and
#ifdef'd the stuff I added where possible. There should be no changes
when compiled without #define USE_NETRC. The patch applies cleanly to
the latest snapshot (mc-2002-04-26-21), and compiles without warnings.
Multiple different login/password entries for the same host also work
as expected (or at least as *I* would expect them to work ;-).

Enabling and disabling ~/.netrc lookups (via use_netrc in ~/.mc/ini)
works fine as well. I haven't had a look at the menu code in boxes.c
yet, though, so you still have to edit ~/.mc/ini manually.

Again, sorry if my original mail came across too rude, it really wasn't
meant that way, and I certainly didn't mean to offend anyone working on
MC.

Bye,
Thomas
-- 
=-------------------------------------------------------------------------=
- Thomas "ZlatkO" Zajic   <zlatko gmx at>    Linux-2.4.18 & Mutt-1.2.5.1i -
-  "It is not easy to cut through a human head with a hacksaw."  (M. C.)  -
=-------------------------------------------------------------------------=
diff -ur mc-4.5.99a-orig/vfs/ftpfs.c mc-4.5.99a-patched/vfs/ftpfs.c
--- mc-4.5.99a-orig/vfs/ftpfs.c	Fri Feb  8 15:25:59 2002
+++ mc-4.5.99a-patched/vfs/ftpfs.c	Mon Apr 29 00:21:53 2002
@@ -162,6 +162,9 @@
     __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);
+#ifdef USE_NETRC
+int lookup_netrc (char *host, char **login, char **pass);
+#endif
 
 static char *
 translate_path (vfs *me, vfs_s_super *super, const char *remote_path)
@@ -812,6 +815,12 @@
     super->root = vfs_s_new_inode (me, super, vfs_s_default_stat(me, S_IFDIR | 0755)); 
     if (password)
 	SUP.password = password;
+#ifdef USE_NETRC
+    /* try to get user and/or password from ~/.netrc */
+    else if (use_netrc)
+	lookup_netrc(SUP.host, &SUP.user, &SUP.password);
+#endif
+
     return open_archive_int (me, super);
 }
 
@@ -819,6 +828,9 @@
 archive_same(vfs *me, vfs_s_super *super, char *archive_name, char *op, void *cookie)
 {	
     char *host, *user;
+#ifdef USE_NETRC
+    char *pass;
+#endif
     int port;
 
     op = vfs_split_url (strchr(op, ':')+1, &host, &user, &port, 0, 21, URL_DEFAULTANON);
@@ -826,6 +838,12 @@
     if (op)
 	g_free (op);
 
+#ifdef USE_NETRC
+    /* replace the dummy user with the one from ~/.netrc */
+    if (use_netrc && !strncmp(user, "*netrc*", 7))
+	lookup_netrc(SUP.host, &user, &pass);
+#endif
+
     port = ((strcmp (host, SUP.host) == 0) &&
 	    (strcmp (user, SUP.user) == 0) &&
 	    (port == SUP.port));
@@ -1872,13 +1890,16 @@
     } *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) {
@@ -1906,7 +1927,8 @@
 	    switch (keyword) {
 		case 3:
 		    if (netrc_next ()) {
-			if (*login == NULL)
+			/* replace the dummy user with the one from ~/.netrc */
+			if ((*login == NULL) || !strncmp(*login, "*netrc*", 7))
 			    *login = g_strdup (buffer);
 			else if (strcmp (*login, buffer))
 			    keyword = 20;
diff -ur mc-4.5.99a-orig/vfs/utilvfs.c mc-4.5.99a-patched/vfs/utilvfs.c
--- mc-4.5.99a-orig/vfs/utilvfs.c	Fri Aug  3 12:12:18 2001
+++ mc-4.5.99a-patched/vfs/utilvfs.c	Sun Apr 28 23:50:22 2002
@@ -69,6 +69,10 @@
     char *pcopy = g_strdup (path);
     char *pend  = pcopy + strlen (pcopy);
     int default_is_anon = flags & URL_DEFAULTANON;
+#ifdef USE_NETRC
+    /* get user from ~/.netrc if we're supposed to */
+    int default_is_netrc = use_netrc;
+#endif
     
     if (pass)
 	*pass = NULL;
@@ -103,8 +107,13 @@
 	}
 	if (*pcopy != 0)
 	    *user = g_strdup (pcopy);
-	else
+	else {
 	    default_is_anon = 0;
+#ifdef USE_NETRC
+	    /* don't lookup ~/.netrc, use login name instead */
+	    default_is_netrc = 0;
+#endif
+	}
 	
 	if (pend == at+1)
 	    rest = at;
@@ -112,6 +121,12 @@
 	    rest = at + 1;
     } else
 	rest = pcopy;
+
+#ifdef USE_NETRC
+    /* dummy user to be replaced in lookup_netrc() in ftpfs.c */
+    if (!*user && (default_is_netrc == 1))
+	    *user = g_strdup ("*netrc*");
+#endif
 
     if (!*user){
 	if (default_is_anon)
diff -ur mc-4.5.99a-orig/vfs/utilvfs.h mc-4.5.99a-patched/vfs/utilvfs.h
--- mc-4.5.99a-orig/vfs/utilvfs.h	Mon Jun 25 23:34:46 2001
+++ mc-4.5.99a-patched/vfs/utilvfs.h	Sat Apr 27 15:03:58 2002
@@ -3,3 +3,7 @@
 #include "../src/tty.h"		/* enable/disable interrupt key */
 #include "../src/background.h"
 #include "../src/main.h"
+
+#ifdef USE_NETRC
+extern int use_netrc;
+#endif

Attachment: pgpgktRNKKUje.pgp
Description: PGP signature



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]