changeset 61:42df9e4be8e0

2002-11-21 Brian Masney <masneyb@gftp.org> * lib/misc.c lib/ssh.c lib/sshv2.c - improvements to the login sequence for SSH connections lib/sshv2.c - SSH transfers now works again
author masneyb
date Fri, 22 Nov 2002 15:47:07 +0000
parents 8a9324fb63a4
children 1af41fb08509
files ChangeLog lib/misc.c lib/ssh.c lib/sshv2.c
diffstat 4 files changed, 78 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Nov 22 00:54:38 2002 +0000
+++ b/ChangeLog	Fri Nov 22 15:47:07 2002 +0000
@@ -1,3 +1,9 @@
+2002-11-21 Brian Masney <masneyb@gftp.org>
+	* lib/misc.c lib/ssh.c lib/sshv2.c - improvements to the login
+	sequence for SSH connections
+
+	lib/sshv2.c - SSH transfers now works again
+	
 2002-11-21 Brian Masney <masneyb@gftp.org>
 	* lib/protocols.c (gftp_get_next_line) - fixed several bugs 
 
@@ -264,7 +270,7 @@
 
 	* cvsclean - added this script
 
-	* *.[ch] - added $Id: ChangeLog,v 1.35 2002/11/22 00:54:37 masneyb Exp $ tags
+	* *.[ch] - added $Id: ChangeLog,v 1.36 2002/11/22 15:47:06 masneyb Exp $ tags
 
 	* debian/* - updated files from Debian maintainer
 
--- a/lib/misc.c	Fri Nov 22 00:54:38 2002 +0000
+++ b/lib/misc.c	Fri Nov 22 15:47:07 2002 +0000
@@ -865,22 +865,25 @@
   return (args);
 }
 
+#define SSH_LOGIN_BUFSIZE	200
+#define SSH_ERROR_BADPASS	-1
+#define SSH_ERROR_QUESTION	-2
 
 char *
 ssh_start_login_sequence (gftp_request * request, int fd)
 {
+  char *tempstr, *pwstr, *key_pos, *tmppos;
   size_t rem, len, diff, lastdiff;
   int wrotepw, ok;
-  char *tempstr, *pwstr;
   ssize_t rd;
 
-  rem = len = 100;
-  tempstr = g_malloc0 (len);
+  rem = len = SSH_LOGIN_BUFSIZE;
+  key_pos = tempstr = g_malloc0 (len + 1);
   diff = lastdiff = 0;
   wrotepw = 0;
   ok = 1;
 
-  if (gftp_set_sockblocking (request, request->datafd, 1) == -1)
+  if (gftp_set_sockblocking (request, fd, 1) == -1)
     return (NULL);
 
   pwstr = g_strconcat (request->password, "\n", NULL);
@@ -888,29 +891,34 @@
   errno = 0;
   while (1)
     {
-      if ((rd = gftp_read (request, tempstr + diff, rem -1, fd)) <= 0)
+      if ((rd = gftp_read (request, tempstr + diff, rem - 1, fd)) <= 0)
         {
           ok = 0;
           break;
         }
+
       tempstr[diff + rd] = '\0';
       rem -= rd;
       diff += rd;
       if (rem <= 1)
         {
-          tempstr = g_realloc (tempstr, len + 100);
-          tempstr[diff] = '\0';
+          tempstr = g_realloc (tempstr, len + SSH_LOGIN_BUFSIZE);
+
           request->logging_function (gftp_logging_recv, request->user_data,
                                      "%s", tempstr + lastdiff);
           lastdiff = diff;
-          len += 100;
-          rem = 100;
+          len += SSH_LOGIN_BUFSIZE;
+          rem = SSH_LOGIN_BUFSIZE;
         }
 
-      if (!wrotepw && 
-          strlen (tempstr) > 11 && strcmp (tempstr + strlen (tempstr) - 10, 
-                                           "password: ") == 0)
+      if (diff > 11 && strcmp (tempstr + diff - 10, "password: ") == 0)
         {
+          if (wrotepw)
+            {
+              ok = SSH_ERROR_BADPASS;
+              break;
+            }
+              
           wrotepw = 1;
           if (gftp_write (request, pwstr, strlen (pwstr), fd) < 0)
             {
@@ -918,11 +926,16 @@
               break;
             }
         }
+      else if ((tmppos = strstr (key_pos, "Enter passphrase for RSA key")) != NULL ||
+               ((tmppos = strstr (key_pos, "Enter passphrase for key '")) != NULL))
+        {
+          key_pos = tmppos + 1;
+          if (wrotepw)
+            {
+              ok = SSH_ERROR_BADPASS;
+              break;
+            }
 
-      else if (!wrotepw && 
-               (strstr (tempstr, "Enter passphrase for RSA key") != NULL ||
-               strstr (tempstr, "Enter passphrase for key '") != NULL))
-        {
           wrotepw = 1;
           if (gftp_write (request, pwstr, strlen (pwstr), fd) < 0)
             {
@@ -930,18 +943,30 @@
               break;
             }
         }
-      else if (strlen (tempstr) >= 5 && 
-               strcmp (tempstr + strlen (tempstr) - 5, "xsftp") == 0)
+      else if (diff >= 10 && strcmp (tempstr + diff - 10, "(yes/no)? ") == 0)
+        {
+          ok = SSH_ERROR_QUESTION;
+          break;
+        }
+      else if (diff >= 5 && strcmp (tempstr + diff - 5, "xsftp") == 0)
         break;
     }
 
   g_free (pwstr);
-  tempstr[diff] = '\0';
-  request->logging_function (gftp_logging_recv, request->user_data,
-                             "%s\n", tempstr + lastdiff);
+
+  if (*(tempstr + lastdiff) != '\0')
+    request->logging_function (gftp_logging_recv, request->user_data,
+                               "%s\n", tempstr + lastdiff);
 
-  if (!ok)
+  if (ok <= 0)
     {
+      if (ok == SSH_ERROR_BADPASS)
+        request->logging_function (gftp_logging_error, request->user_data,
+                               _("Error: An incorrect password was entered\n"));
+      else if (ok == SSH_ERROR_QUESTION)
+        request->logging_function (gftp_logging_error, request->user_data,
+                               _("Please connect to this host with the command line SSH utility and answer this question appropriately.\n"));
+
       g_free (tempstr);
       return (NULL);
     }
--- a/lib/ssh.c	Fri Nov 22 00:54:38 2002 +0000
+++ b/lib/ssh.c	Fri Nov 22 15:47:07 2002 +0000
@@ -443,8 +443,6 @@
           !(strlen (tempstr) > 4 && strcmp (tempstr + strlen (tempstr) - 5, 
                                             "xsftp") == 0))
         {
-          request->logging_function (gftp_logging_error, request->user_data,
-		_("Error: Received wrong init string from server\n"));
           g_free (args);
           g_free (exepath);
           return (-2);
--- a/lib/sshv2.c	Fri Nov 22 00:54:38 2002 +0000
+++ b/lib/sshv2.c	Fri Nov 22 15:47:07 2002 +0000
@@ -298,8 +298,8 @@
 
 #ifdef DEBUG
   printf ("\rSending: ");
-  for (wrote=0; wrote<len + 5; wrote++)
-    printf ("%x ", buf[wrote]);
+  for (clen=0; clen<len + 5; clen++)
+    printf ("%x ", buf[clen] & 0xff);
   printf ("\n");
 #endif
 
@@ -316,21 +316,21 @@
 sshv2_read_response (gftp_request * request, sshv2_message * message,
                      int fd)
 {
-  ssize_t numread;
-  char buf[5];
+  ssize_t numread, rem;
+  char buf[5], *pos;
 
   if (fd <= 0)
     fd = request->sockfd;
 
-  if ((numread = gftp_read (request, buf, 5, fd)) < 0)
-    return (-2);
-
-/* #ifdef DEBUG*/
-  printf ("\rReceived: ");
-  for (numread=0; numread<5; numread++)
-    printf ("%x ", buf[numread]);
-  fflush (stdout);
-/* #endif*/
+  pos = buf;
+  rem = 5;
+  while (rem > 0)
+    {
+      if ((numread = gftp_read (request, pos, rem, fd)) < 0)
+        return (-2);
+      rem -= numread;
+      pos += numread;
+    }
 
   memcpy (&message->length, buf, 4);
   message->length = ntohl (message->length);
@@ -345,22 +345,22 @@
     }
 
   message->command = buf[4];
-  message->buffer = g_malloc (message->length);
+  message->buffer = g_malloc (message->length + 1);
 
   message->pos = message->buffer;
   message->end = message->buffer + message->length - 1;
 
-  if ((numread = gftp_read (request, message->buffer, message->length -1, fd)) < 0)
-
-    return (-2);
-
-  message->buffer[message->length - 1] = '\0';
+  pos = message->buffer;
+  rem = message->length - 1;
+  while (rem > 0)
+    {
+      if ((numread = gftp_read (request, pos, rem, fd)) < 0)
+        return (-2);
+      rem -= numread;
+      pos += numread;
+    }
 
-/* #ifdef DEBUG*/
-  for (numread=0; numread<message->length - 1; numread++)
-    printf ("%x ", message->buffer[numread]);
-  printf ("\n");
-/* #endif*/
+  message->buffer[message->length] = '\0';
 
   sshv2_log_command (request, gftp_logging_recv, message->command, 
                      message->buffer, message->length);
@@ -617,8 +617,6 @@
               !(strlen (tempstr) > 4 && strcmp (tempstr + strlen (tempstr) - 5,
                                                 "xsftp") == 0))
             {
-              request->logging_function (gftp_logging_error, request->user_data,
-                         _("Error: Received wrong init string from server\n"));
               g_free (args);
               g_free (exepath);
               return (-2);