File: protocol_ftps.c

package info (click to toggle)
gftp 2.9.1~beta-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,088 kB
  • sloc: ansic: 23,547; sh: 4,489; makefile: 182
file content (287 lines) | stat: -rw-r--r-- 8,620 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/***********************************************************************************/
/*  protocol_ftps.c - General purpose routines for the FTPS protocol               */
/*  Copyright (C) 1998-2007 Brian Masney <masneyb@gftp.org>                        */
/*                                                                                 */
/*  Permission is hereby granted, free of charge, to any person obtaining a copy   */
/*  of this software and associated documentation files (the "Software"), to deal  */
/*  in the Software without restriction, including without limitation the rights   */
/*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell      */
/*  copies of the Software, and to permit persons to whom the Software is          */
/*  furnished to do so, subject to the following conditions:                       */
/*                                                                                 */
/*  The above copyright notice and this permission notice shall be included in all */
/*  copies or substantial portions of the Software.                                */
/*                                                                                 */
/*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR     */
/*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,       */
/*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE    */
/*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER         */
/*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  */
/*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  */
/*  SOFTWARE.                                                                      */
/***********************************************************************************/

#include "gftp.h"
#include "protocol_ftp.h"

//=======================================================================
//                          EXPLICIT TLS
//=======================================================================

#ifdef USE_SSL

static int
ftps_get_next_file (gftp_request * request, gftp_file * fle, int fd)
{
  ftp_protocol_data * ftpdat;
  int resetptr = 0, resetdata = 0;
  size_t ret;

  ftpdat = request->protocol_data;
  if (request->cached && request->read_function == gftp_ssl_read)
    {
      request->read_function = gftp_fd_read;
      request->write_function = gftp_fd_write;
      resetptr = 1;
      if (ftpdat->data_conn_read == gftp_ssl_read)
      {
         ftpdat->data_conn_read  = gftp_fd_read;
         ftpdat->data_conn_write = gftp_fd_write;
         resetdata = 1;
      }
    }

  ret = ftp_get_next_file (request, fle, fd);

  if (resetptr)
    {
      request->read_function  = gftp_ssl_read;
      request->write_function = gftp_ssl_write;
      if (resetdata)
      {
         ftpdat->data_conn_read  = gftp_ssl_read;
         ftpdat->data_conn_write = gftp_ssl_write;
      }
    }

  return (ret);
}


static int  ftps_data_conn_tls_start (gftp_request * request)
{
  DEBUG_PRINT_FUNC
  ftp_protocol_data * ftpdat = request->protocol_data;
  return gftp_ssl_session_setup_ex (request, ftpdat->data_connection);
}


static void ftps_data_conn_tls_close (gftp_request * request)
{
  DEBUG_PRINT_FUNC
  ftp_protocol_data * ftpdat = request->protocol_data;
  gftp_ssl_session_close_ex (request, ftpdat->data_connection);
}


static int ftps_auth_tls_start (gftp_request * request)
{
  DEBUG_PRINT_FUNC
  ftp_protocol_data * ftpdat;
  int ret;

  ftpdat = request->protocol_data;

  ret = ftp_send_command (request, "AUTH TLS\r\n", -1, 1, 0);
  if (ret < 0)
    return (ret);
  else if (ret != '2')
    return (GFTP_EFATAL);

  if ((ret = gftp_ssl_session_setup (request)) < 0)
    return (ret);

  request->read_function  = gftp_ssl_read;
  request->write_function = gftp_ssl_write;

  ret = ftp_send_command (request, "PBSZ 0\r\n", -1, 1, 0);
  if (ret < 0)
    return (ret);

  ret = ftp_send_command (request, "PROT P\r\n", -1, 1, 0);
  if (ret < 0)
    return (ret);
  else if (ret == '2')
    {
      ftpdat->data_conn_tls_start = ftps_data_conn_tls_start;
      ftpdat->data_conn_read      = gftp_ssl_read;
      ftpdat->data_conn_write     = gftp_ssl_write;
      ftpdat->data_conn_tls_close = ftps_data_conn_tls_close;
    }
  else
    {
      ret = ftp_send_command (request, "PROT C\r\n", -1, 1, 0);
      if (ret < 0)
        return (ret);
      else if (ret != '2')
        {
          gftp_disconnect (request);
          return (GFTP_ERETRYABLE);
        }

      ftpdat->data_conn_read  = gftp_fd_read;
      ftpdat->data_conn_write = gftp_fd_write;
    }

  return (0);
}


static int ftps_connect (gftp_request * request)
{
  DEBUG_PRINT_FUNC
  if (request->datafd > 0) {
      return (0);
  }
  // need to send AUTH TLS in clear text
  request->read_function  = gftp_fd_read;
  request->write_function = gftp_fd_write;

  return (ftp_connect (request));
}

#endif


void ftps_register_module (void)
{
  DEBUG_PRINT_FUNC
#ifdef USE_SSL
  ssl_register_module ();
#endif
}


int ftps_init (gftp_request * request)
{
  DEBUG_PRINT_FUNC
#ifdef USE_SSL
  ftp_protocol_data * ftpdat;
  int ret;

  g_return_val_if_fail (request != NULL, GFTP_EFATAL);

  /* init FTP first */
  ret = gftp_protocols[GFTP_PROTOCOL_FTP].init (request);
  if (ret < 0) {
     return (ret);
  }

  /* don't change protonum */
  //request->protonum = GFTP_PROTOCOL_FTPS;
  request->url_prefix = gftp_protocols[GFTP_PROTOCOL_FTPS].url_prefix;

  ftpdat = (ftp_protocol_data *) request->protocol_data;
  request->init = ftps_init;
  request->connect = ftps_connect;
  ftpdat->auth_tls_start = ftps_auth_tls_start;
  request->get_next_file = ftps_get_next_file;
  request->post_connect = NULL;

  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
}



//=======================================================================
//                          IMPLICIT TLS
//=======================================================================
// Use the FTPS stuff and just override default values and functions where needed
// Establish encrypted connection before sending or receiving any message

void ftpsi_register_module (void)
{
   DEBUG_PRINT_FUNC
}


static int ftpsi_connect (gftp_request * request)
{
   DEBUG_PRINT_FUNC
   if (request->datafd > 0) {
      return (0);
   }
   // this is just to override default port
   // default port for FTPSi is 990..
   if (!request->port) {
      request->port = gftp_protocols[GFTP_PROTOCOL_FTPSi].default_port;
   }
   return (ftp_connect (request));
}


static int ftpsi_auth_tls_start (gftp_request * request)
{
   DEBUG_PRINT_FUNC
   int ret;
   // buggy servers may require this (vsftpd)
   // this is not needed for servers with proper implicit SSL support
   //  (pure-FTPd, Rebex FTPS)
   ret = ftp_send_command (request, "PBSZ 0\r\n", -1, 1, 0);
   ret = ftp_send_command (request, "PROT P\r\n", -1, 1, 0);
   if (ret < 0) {
      return (ret);
   }
   return 0;
}


int ftpsi_init (gftp_request * request)
{
   DEBUG_PRINT_FUNC
#ifdef USE_SSL
   int ret;
   ftp_protocol_data * ftpdat;
   g_return_val_if_fail (request != NULL, GFTP_EFATAL);

   // init funcs (FTP->FTPS->FTPSi)
   ret = gftp_protocols[GFTP_PROTOCOL_FTPS].init (request);
   if (ret < 0) {
      return (ret);
   }

   /* don't change protonum */
   //request->protonum = GFTP_PROTOCOL_FTPSi;
   request->url_prefix = gftp_protocols[GFTP_PROTOCOL_FTPSi].url_prefix;

   ftpdat = (ftp_protocol_data *) request->protocol_data;
   ftpdat->data_conn_tls_start = ftps_data_conn_tls_start;
   ftpdat->data_conn_read      = gftp_ssl_read;
   ftpdat->data_conn_write     = gftp_ssl_write;
   ftpdat->data_conn_tls_close = ftps_data_conn_tls_close;
   request->read_function      = gftp_ssl_read;
   request->write_function     = gftp_ssl_write;

   request->init               = ftpsi_init;
   request->connect            = ftpsi_connect;
   ftpdat->auth_tls_start      = ftpsi_auth_tls_start;
   request->post_connect       = gftp_ssl_session_setup; /* socket-connect.c */

   return 0;
#else
   request->logging_function (gftp_logging_error, request,
                             _("FTPSi (Implicit SSL) Support unavailable since SSL support was not compiled in. Aborting connection.\n"));

   return (GFTP_EFATAL);
#endif
}