changeset 389:b39a312323ec

2003-2-3 Brian Masney <masneyb@gftp.org> * lib/Makefile.am lib/gftp.h lib/options.h lib/rfc959.c lib/ftpcommon.h lib/ftps.c - added support for the FTPS protocol. This currently is only for the control connection. (draft-murray-auth-ftp-ssl-09.txt)
author masneyb
date Wed, 04 Feb 2004 01:53:06 +0000
parents 371695551cfa
children 0444232dc494
files ChangeLog lib/Makefile.am lib/ftpcommon.h lib/ftps.c lib/gftp.h lib/options.h lib/rfc959.c
diffstat 7 files changed, 189 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Feb 03 02:34:01 2004 +0000
+++ b/ChangeLog	Wed Feb 04 01:53:06 2004 +0000
@@ -1,3 +1,8 @@
+2003-2-3 Brian Masney <masneyb@gftp.org>
+	* lib/Makefile.am lib/gftp.h lib/options.h lib/rfc959.c lib/ftpcommon.h
+	lib/ftps.c - added support for the FTPS protocol. This currently is only
+	for the control connection. (draft-murray-auth-ftp-ssl-09.txt)
+
 2003-2-2 Brian Masney <masneyb@gftp.org>
 	* docs/website/index.html.in - updated main gftp website
 
@@ -2148,7 +2153,7 @@
 
 	* cvsclean - added this script
 
-	* *.[ch] - added $Id: ChangeLog,v 1.215 2004/02/03 02:34:00 masneyb Exp $ tags
+	* *.[ch] - added $Id: ChangeLog,v 1.216 2004/02/04 01:53:02 masneyb Exp $ tags
 
 	* debian/* - updated files from Debian maintainer
 
--- a/lib/Makefile.am	Tue Feb 03 02:34:01 2004 +0000
+++ b/lib/Makefile.am	Wed Feb 04 01:53:06 2004 +0000
@@ -1,7 +1,7 @@
 ## Process this file with automake to produce Makefile.in 
 
 noinst_LIBRARIES = libgftp.a
-libgftp_a_SOURCES=bookmark.c cache.c config_file.c https.c local.c misc.c \
+libgftp_a_SOURCES=bookmark.c cache.c config_file.c ftps.c https.c local.c misc.c \
                   protocols.c pty.c rfc959.c rfc2068.c sshv2.c sslcommon.c
 INCLUDES=@GLIB_CFLAGS@ @PTHREAD_CFLAGS@ -I../intl -DSHARE_DIR=\"$(datadir)/gftp\" -DLOCALE_DIR=\"$(localedir)\"
 noinst_HEADERS=gftp.h httpcommon.h options.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/ftpcommon.h	Wed Feb 04 01:53:06 2004 +0000
@@ -0,0 +1,39 @@
+/*****************************************************************************/
+/*  ftpcommon.h - common data structures for RFC959 and FTPS                 */
+/*  Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org>                  */
+/*                                                                           */
+/*  This program is free software; you can redistribute it and/or modify     */
+/*  it under the terms of the GNU General Public License as published by     */
+/*  the Free Software Foundation; either version 2 of the License, or        */
+/*  (at your option) any later version.                                      */
+/*                                                                           */
+/*  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            */
+/*  GNU General Public License for more details.                             */
+/*                                                                           */
+/*  You should have received a copy of the GNU General Public License        */
+/*  along with this program; if not, write to the Free Software              */
+/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
+/*****************************************************************************/
+
+/* $Id$ */
+
+#include "gftp.h"
+
+struct rfc959_params_tag
+{  
+  gftp_getline_buffer * datafd_rbuf,
+                      * dataconn_rbuf;
+  int data_connection;
+  unsigned int is_ascii_transfer : 1,
+               sent_retr : 1; 
+  int (*auth_tls_start) (gftp_request * request);
+};
+
+typedef struct rfc959_params_tag rfc959_parms;
+
+int rfc959_send_command 		( gftp_request * request,
+					  const char *command,
+					  int read_response );
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/ftps.c	Wed Feb 04 01:53:06 2004 +0000
@@ -0,0 +1,95 @@
+/*****************************************************************************/
+/*  ftps.c - General purpose routines for the FTPS protocol                  */
+/*  Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org>                  */
+/*                                                                           */
+/*  This program is free software; you can redistribute it and/or modify     */
+/*  it under the terms of the GNU General Public License as published by     */
+/*  the Free Software Foundation; either version 2 of the License, or        */
+/*  (at your option) any later version.                                      */
+/*                                                                           */
+/*  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            */
+/*  GNU General Public License for more details.                             */
+/*                                                                           */
+/*  You should have received a copy of the GNU General Public License        */
+/*  along with this program; if not, write to the Free Software              */
+/*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA      */
+/*****************************************************************************/
+
+#include "gftp.h"
+#include "ftpcommon.h"
+
+static const char cvsid[] = "$Id$";
+
+void
+ftps_register_module (void)
+{
+#ifdef USE_SSL
+  ssl_register_module ();
+#endif
+}
+
+
+static int 
+ftps_auth_tls_start (gftp_request * request)
+{
+  rfc959_parms * params;
+  int ret;
+
+  params = request->protocol_data;
+
+  ret = rfc959_send_command (request, "AUTH TLS\r\n", 1);
+  if (ret < 0)
+    return (ret);
+  else if (ret != '2')
+    return (0);
+
+  if ((ret = gftp_ssl_session_setup (request)) < 0)
+    return (ret);
+
+  request->read_function = gftp_ssl_read;
+  request->write_function = gftp_ssl_write;
+
+  ret = rfc959_send_command (request, "PBSZ 0\r\n", 1);
+  if (ret < 0)
+    return (ret);
+
+  ret = rfc959_send_command (request, "PROT C\r\n", 1);
+  if (ret < 0)
+    return (ret);
+
+  return (0);
+}
+
+
+int
+ftps_init (gftp_request * request)
+{
+#ifdef USE_SSL
+  rfc959_parms * params;
+  int ret;
+
+  g_return_val_if_fail (request != NULL, GFTP_EFATAL);
+
+  if ((ret = gftp_protocols[GFTP_FTP_NUM].init (request)) < 0)
+    return (ret);
+
+  params = request->protocol_data;
+  params->auth_tls_start = ftps_auth_tls_start;
+  request->init = ftps_init;
+  request->post_connect = NULL;
+  request->url_prefix = g_strdup ("ftps");
+
+  if ((ret = gftp_ssl_startup (NULL)) < 0)
+    return (ret);
+
+  return (0);
+#else
+  request->logging_function (gftp_logging_error, request,
+                             _("FTPS Support unavailable since SSL support was not compiled in. Aborting connection.\n"));
+
+  return (GFTP_EFATAL);
+#endif
+}
+
--- a/lib/gftp.h	Tue Feb 03 02:34:01 2004 +0000
+++ b/lib/gftp.h	Wed Feb 04 01:53:06 2004 +0000
@@ -747,11 +747,12 @@
 
 /* protocols.c */
 #define GFTP_FTP_NUM				0
-#define GFTP_HTTP_NUM				1
-#define GFTP_HTTPS_NUM				2
-#define GFTP_LOCAL_NUM				3
-#define GFTP_SSHV2_NUM				4
-#define GFTP_BOOKMARK_NUM			5
+#define GFTP_FTPS_NUM				1
+#define GFTP_HTTP_NUM				2
+#define GFTP_HTTPS_NUM				3
+#define GFTP_LOCAL_NUM				4
+#define GFTP_SSHV2_NUM				5
+#define GFTP_BOOKMARK_NUM			6
 
 #define GFTP_IS_CONNECTED(request)		((request) != NULL && \
                                                  ((request)->datafd > 0 || \
@@ -767,6 +768,10 @@
 					  gftp_file *fle, 
 					  int fd );
 
+int ftps_init 				( gftp_request * request );
+
+void ftps_register_module		( void );
+
 int rfc2068_init 			( gftp_request * request );
 
 void rfc2068_register_module		( void );
--- a/lib/options.h	Tue Feb 03 02:34:01 2004 +0000
+++ b/lib/options.h	Wed Feb 04 01:53:06 2004 +0000
@@ -213,15 +213,27 @@
 supported_gftp_protocols gftp_protocols[] =
 {
   {N_("FTP"), rfc959_init, rfc959_register_module, "ftp", 1, 1},
+
+#ifdef USE_SSL
+  {N_("FTPS"), ftps_init, ftps_register_module, "ftps", 1, 1},
+#else
+  {N_("FTPS"), ftps_init, ftps_register_module, "ftps", 0, 1},
+#endif
+
   {N_("HTTP"), rfc2068_init, rfc2068_register_module, "http", 1, 1},
+
 #ifdef USE_SSL
   {N_("HTTPS"), https_init, https_register_module, "https", 1, 1},
 #else
   {N_("HTTPS"), https_init, https_register_module, "https", 0, 1},
 #endif
+
   {N_("Local"), local_init, local_register_module, "file", 1, 0},
+
   {N_("SSH2"), sshv2_init, sshv2_register_module, "ssh2", 1, 1},
+
   {N_("Bookmark"), bookmark_init, bookmark_register_module, "bookmark", 0, 0},
+
   {NULL, NULL, NULL, NULL, 0}
 };
 
--- a/lib/rfc959.c	Tue Feb 03 02:34:01 2004 +0000
+++ b/lib/rfc959.c	Wed Feb 04 01:53:06 2004 +0000
@@ -18,6 +18,8 @@
 /*****************************************************************************/
 
 #include "gftp.h"
+#include "ftpcommon.h"
+
 static const char cvsid[] = "$Id$";
 
 static gftp_textcomboedt_data gftp_proxy_type[] = {
@@ -84,16 +86,6 @@
 };
 
          
-typedef struct rfc959_params_tag
-{
-  gftp_getline_buffer * datafd_rbuf,
-                      * dataconn_rbuf;
-  int data_connection;
-  unsigned int is_ascii_transfer : 1,
-               sent_retr : 1;
-} rfc959_parms;
-
-
 static int
 rfc959_read_response (gftp_request * request, int disconnect_on_42x)
 {
@@ -145,7 +137,7 @@
 }
 
 
-static int
+int
 rfc959_send_command (gftp_request * request, const char *command, 
                      int read_response)
 {
@@ -172,8 +164,8 @@
                                  command);
     }
 
-  if ((ret = gftp_fd_write (request, command, strlen (command), 
-                         request->datafd)) < 0)
+  if ((ret = request->write_function (request, command, strlen (command), 
+                                      request->datafd)) < 0)
     return (ret);
 
   if (read_response)
@@ -474,6 +466,15 @@
       return (ret);
     }
 
+  if (parms->auth_tls_start != NULL)
+    {
+      if ((ret = parms->auth_tls_start (request)) < 0)
+        {
+          gftp_disconnect (request);
+          return (ret);
+        }
+    }
+
   /* Login the proxy server if available */
   if (request->use_proxy)
     {
@@ -1225,7 +1226,7 @@
   g_free (tempstr);
 
   tempstr = g_strconcat ("RETR ", fromfile, "\r\n", NULL);
-  if ((ret = gftp_fd_write (fromreq, tempstr, strlen (tempstr), 
+  if ((ret = gftp_fd_write (fromreq, tempstr, strlen (tempstr), /* FIXME */
                          fromreq->datafd)) < 0)
     {
       g_free (tempstr);
@@ -1234,7 +1235,7 @@
   g_free (tempstr);
 
   tempstr = g_strconcat ("STOR ", tofile, "\r\n", NULL);
-  if ((ret = gftp_fd_write (toreq, tempstr, strlen (tempstr), 
+  if ((ret = gftp_fd_write (toreq, tempstr, strlen (tempstr),  /* FIXME */
                             toreq->datafd)) < 0)
     {
       g_free (tempstr);
@@ -1436,6 +1437,8 @@
 int
 rfc959_get_next_file (gftp_request * request, gftp_file * fle, int fd)
 {
+  ssize_t (*oldread_func) (gftp_request * request, void *ptr, size_t size,
+                           int fd);
   rfc959_parms * parms;
   char tempstr[1024];
   ssize_t len;
@@ -1458,8 +1461,13 @@
 
   do
     {
-      if ((len = gftp_get_line (request, &parms->dataconn_rbuf,
-                                tempstr, sizeof (tempstr), fd)) <= 0)
+      oldread_func = request->read_function;
+      request->read_function = gftp_fd_read;
+      len = gftp_get_line (request, &parms->dataconn_rbuf, tempstr,
+                           sizeof (tempstr), fd);
+      request->read_function = oldread_func;
+
+      if (len <= 0)
 	{
           gftp_file_destroy (fle);
 	  return ((int) len);
@@ -1783,6 +1791,7 @@
   request->protocol_data = g_malloc0 (sizeof (rfc959_parms));
   parms = request->protocol_data;
   parms->data_connection = -1; 
+  parms->auth_tls_start = NULL;
 
   return (gftp_set_config_options (request));
 }