blob: ac39dae091469ba73c846820dfcdad4ffff17c18 [file] [log] [blame]
Kristian Monsen5ab50182010-05-14 18:53:44 +01001/* This source code was modified by Martin Hedenfalk <mhe@stacken.kth.se> for
2 * use in Curl. His latest changes were done 2000-09-18.
3 *
4 * It has since been patched and modified a lot by Daniel Stenberg
5 * <daniel@haxx.se> to make it better applied to curl conditions, and to make
6 * it not use globals, pollute name space and more. This source code awaits a
7 * rewrite to work around the paragraph 2 in the BSD licenses as explained
8 * below.
9 *
Alex Deymo486467e2017-12-19 19:04:07 +010010 * Copyright (c) 1998, 1999, 2017 Kungliga Tekniska Högskolan
Kristian Monsen5ab50182010-05-14 18:53:44 +010011 * (Royal Institute of Technology, Stockholm, Sweden).
12 *
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070013 * Copyright (C) 2001 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
Kristian Monsen5ab50182010-05-14 18:53:44 +010014 *
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 *
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 *
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * 3. Neither the name of the Institute nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE. */
43
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070044#include "curl_setup.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010045
46#ifndef CURL_DISABLE_FTP
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070047#ifdef HAVE_GSSAPI
Kristian Monsen5ab50182010-05-14 18:53:44 +010048
49#ifdef HAVE_NETDB_H
50#include <netdb.h>
51#endif
52
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070053#include <limits.h>
Kristian Monsen5ab50182010-05-14 18:53:44 +010054
55#include "urldata.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010056#include "curl_base64.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010057#include "curl_memory.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070058#include "curl_sec.h"
Lucas Eckels9bd90e62012-08-06 15:07:02 -070059#include "ftp.h"
60#include "sendf.h"
Elliott Hughescee03382017-06-23 12:17:18 -070061#include "strcase.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070062#include "warnless.h"
Elliott Hughescee03382017-06-23 12:17:18 -070063#include "strdup.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010064/* The last #include file should be: */
65#include "memdebug.h"
66
67static const struct {
68 enum protection_level level;
69 const char *name;
70} level_names[] = {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070071 { PROT_CLEAR, "clear" },
72 { PROT_SAFE, "safe" },
73 { PROT_CONFIDENTIAL, "confidential" },
74 { PROT_PRIVATE, "private" }
Kristian Monsen5ab50182010-05-14 18:53:44 +010075};
76
77static enum protection_level
78name_to_level(const char *name)
79{
80 int i;
81 for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++)
82 if(checkprefix(name, level_names[i].name))
83 return level_names[i].level;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070084 return PROT_NONE;
Kristian Monsen5ab50182010-05-14 18:53:44 +010085}
86
Lucas Eckels9bd90e62012-08-06 15:07:02 -070087/* Convert a protocol |level| to its char representation.
88 We take an int to catch programming mistakes. */
Elliott Hughescee03382017-06-23 12:17:18 -070089static char level_to_char(int level)
90{
Lucas Eckels9bd90e62012-08-06 15:07:02 -070091 switch(level) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070092 case PROT_CLEAR:
Lucas Eckels9bd90e62012-08-06 15:07:02 -070093 return 'C';
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070094 case PROT_SAFE:
Lucas Eckels9bd90e62012-08-06 15:07:02 -070095 return 'S';
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070096 case PROT_CONFIDENTIAL:
Lucas Eckels9bd90e62012-08-06 15:07:02 -070097 return 'E';
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070098 case PROT_PRIVATE:
Lucas Eckels9bd90e62012-08-06 15:07:02 -070099 return 'P';
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700100 case PROT_CMD:
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700101 /* Fall through */
102 default:
103 /* Those 2 cases should not be reached! */
104 break;
105 }
106 DEBUGASSERT(0);
107 /* Default to the most secure alternative. */
108 return 'P';
109}
110
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700111/* Send an FTP command defined by |message| and the optional arguments. The
112 function returns the ftp_code. If an error occurs, -1 is returned. */
113static int ftp_send_command(struct connectdata *conn, const char *message, ...)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100114{
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700115 int ftp_code;
Alex Deymo486467e2017-12-19 19:04:07 +0100116 ssize_t nread = 0;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700117 va_list args;
118 char print_buffer[50];
119
120 va_start(args, message);
121 vsnprintf(print_buffer, sizeof(print_buffer), message, args);
122 va_end(args);
123
Elliott Hughescee03382017-06-23 12:17:18 -0700124 if(Curl_ftpsend(conn, print_buffer)) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700125 ftp_code = -1;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100126 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700127 else {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700128 if(Curl_GetFTPResponse(&nread, conn, &ftp_code))
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700129 ftp_code = -1;
130 }
131
132 (void)nread; /* Unused */
133 return ftp_code;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100134}
135
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700136/* Read |len| from the socket |fd| and store it in |to|. Return a CURLcode
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700137 saying whether an error occurred or CURLE_OK if |len| was read. */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700138static CURLcode
139socket_read(curl_socket_t fd, void *to, size_t len)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100140{
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700141 char *to_p = to;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700142 CURLcode result;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700143 ssize_t nread;
144
145 while(len > 0) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700146 result = Curl_read_plain(fd, to_p, len, &nread);
147 if(!result) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700148 len -= nread;
149 to_p += nread;
150 }
151 else {
152 /* FIXME: We are doing a busy wait */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700153 if(result == CURLE_AGAIN)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700154 continue;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700155 return result;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700156 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100157 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700158 return CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100159}
160
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700161
162/* Write |len| bytes from the buffer |to| to the socket |fd|. Return a
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700163 CURLcode saying whether an error occurred or CURLE_OK if |len| was
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700164 written. */
165static CURLcode
166socket_write(struct connectdata *conn, curl_socket_t fd, const void *to,
167 size_t len)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100168{
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700169 const char *to_p = to;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700170 CURLcode result;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700171 ssize_t written;
172
173 while(len > 0) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700174 result = Curl_write_plain(conn, fd, to_p, len, &written);
175 if(!result) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700176 len -= written;
177 to_p += written;
178 }
179 else {
180 /* FIXME: We are doing a busy wait */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700181 if(result == CURLE_AGAIN)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700182 continue;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700183 return result;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700184 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100185 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700186 return CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100187}
188
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700189static CURLcode read_data(struct connectdata *conn,
190 curl_socket_t fd,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700191 struct krb5buffer *buf)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100192{
193 int len;
Elliott Hughescee03382017-06-23 12:17:18 -0700194 void *tmp = NULL;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700195 CURLcode result;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100196
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700197 result = socket_read(fd, &len, sizeof(len));
198 if(result)
199 return result;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700200
Elliott Hughescee03382017-06-23 12:17:18 -0700201 if(len) {
202 /* only realloc if there was a length */
203 len = ntohl(len);
204 tmp = Curl_saferealloc(buf->data, len);
205 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700206 if(tmp == NULL)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700207 return CURLE_OUT_OF_MEMORY;
208
209 buf->data = tmp;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700210 result = socket_read(fd, buf->data, len);
211 if(result)
212 return result;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700213 buf->size = conn->mech->decode(conn->app_data, buf->data, len,
214 conn->data_prot, conn);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100215 buf->index = 0;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700216 return CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100217}
218
219static size_t
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700220buffer_read(struct krb5buffer *buf, void *data, size_t len)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100221{
222 if(buf->size - buf->index < len)
223 len = buf->size - buf->index;
Elliott Hughescee03382017-06-23 12:17:18 -0700224 memcpy(data, (char *)buf->data + buf->index, len);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100225 buf->index += len;
226 return len;
227}
228
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700229/* Matches Curl_recv signature */
230static ssize_t sec_recv(struct connectdata *conn, int sockindex,
231 char *buffer, size_t len, CURLcode *err)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100232{
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700233 size_t bytes_read;
234 size_t total_read = 0;
235 curl_socket_t fd = conn->sock[sockindex];
Kristian Monsen5ab50182010-05-14 18:53:44 +0100236
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700237 *err = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100238
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700239 /* Handle clear text response. */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700240 if(conn->sec_complete == 0 || conn->data_prot == PROT_CLEAR)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700241 return read(fd, buffer, len);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100242
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700243 if(conn->in_buffer.eof_flag) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100244 conn->in_buffer.eof_flag = 0;
245 return 0;
246 }
247
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700248 bytes_read = buffer_read(&conn->in_buffer, buffer, len);
249 len -= bytes_read;
250 total_read += bytes_read;
251 buffer += bytes_read;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100252
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700253 while(len > 0) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700254 if(read_data(conn, fd, &conn->in_buffer))
Kristian Monsen5ab50182010-05-14 18:53:44 +0100255 return -1;
256 if(conn->in_buffer.size == 0) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700257 if(bytes_read > 0)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100258 conn->in_buffer.eof_flag = 1;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700259 return bytes_read;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100260 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700261 bytes_read = buffer_read(&conn->in_buffer, buffer, len);
262 len -= bytes_read;
263 total_read += bytes_read;
264 buffer += bytes_read;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100265 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700266 /* FIXME: Check for overflow */
267 return total_read;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100268}
269
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700270/* Send |length| bytes from |from| to the |fd| socket taking care of encoding
271 and negociating with the server. |from| can be NULL. */
272/* FIXME: We don't check for errors nor report any! */
273static void do_sec_send(struct connectdata *conn, curl_socket_t fd,
274 const char *from, int length)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100275{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700276 int bytes, htonl_bytes; /* 32-bit integers for htonl */
277 char *buffer = NULL;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700278 char *cmd_buffer;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700279 size_t cmd_size = 0;
280 CURLcode error;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700281 enum protection_level prot_level = conn->data_prot;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700282 bool iscmd = (prot_level == PROT_CMD)?TRUE:FALSE;
283
284 DEBUGASSERT(prot_level > PROT_NONE && prot_level < PROT_LAST);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100285
286 if(iscmd) {
287 if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700288 prot_level = PROT_PRIVATE;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100289 else
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700290 prot_level = conn->command_prot;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100291 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700292 bytes = conn->mech->encode(conn->app_data, from, length, prot_level,
Elliott Hughescee03382017-06-23 12:17:18 -0700293 (void **)&buffer);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700294 if(!buffer || bytes <= 0)
295 return; /* error */
296
Kristian Monsen5ab50182010-05-14 18:53:44 +0100297 if(iscmd) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700298 error = Curl_base64_encode(conn->data, buffer, curlx_sitouz(bytes),
299 &cmd_buffer, &cmd_size);
300 if(error) {
301 free(buffer);
302 return; /* error */
303 }
304 if(cmd_size > 0) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700305 static const char *enc = "ENC ";
306 static const char *mic = "MIC ";
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700307 if(prot_level == PROT_PRIVATE)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700308 socket_write(conn, fd, enc, 4);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100309 else
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700310 socket_write(conn, fd, mic, 4);
311
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700312 socket_write(conn, fd, cmd_buffer, cmd_size);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700313 socket_write(conn, fd, "\r\n", 2);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700314 infof(conn->data, "Send: %s%s\n", prot_level == PROT_PRIVATE?enc:mic,
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700315 cmd_buffer);
316 free(cmd_buffer);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100317 }
318 }
319 else {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700320 htonl_bytes = htonl(bytes);
321 socket_write(conn, fd, &htonl_bytes, sizeof(htonl_bytes));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700322 socket_write(conn, fd, buffer, curlx_sitouz(bytes));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100323 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700324 free(buffer);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100325}
326
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700327static ssize_t sec_write(struct connectdata *conn, curl_socket_t fd,
328 const char *buffer, size_t length)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100329{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700330 ssize_t tx = 0, len = conn->buffer_size;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100331
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700332 len -= conn->mech->overhead(conn->app_data, conn->data_prot,
333 curlx_sztosi(len));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100334 if(len <= 0)
335 len = length;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700336 while(length) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700337 if(length < (size_t)len)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100338 len = length;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700339
340 do_sec_send(conn, fd, buffer, curlx_sztosi(len));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100341 length -= len;
342 buffer += len;
343 tx += len;
344 }
345 return tx;
346}
347
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700348/* Matches Curl_send signature */
349static ssize_t sec_send(struct connectdata *conn, int sockindex,
350 const void *buffer, size_t len, CURLcode *err)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100351{
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700352 curl_socket_t fd = conn->sock[sockindex];
353 *err = CURLE_OK;
354 return sec_write(conn, fd, buffer, len);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100355}
356
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700357int Curl_sec_read_msg(struct connectdata *conn, char *buffer,
358 enum protection_level level)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100359{
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700360 /* decoded_len should be size_t or ssize_t but conn->mech->decode returns an
361 int */
362 int decoded_len;
363 char *buf;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700364 int ret_code = 0;
365 size_t decoded_sz = 0;
366 CURLcode error;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100367
Elliott Hughes82be86d2017-09-20 17:00:17 -0700368 if(!conn->mech)
369 /* not inititalized, return error */
370 return -1;
371
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700372 DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
373
374 error = Curl_base64_decode(buffer + 4, (unsigned char **)&buf, &decoded_sz);
375 if(error || decoded_sz == 0)
376 return -1;
377
378 if(decoded_sz > (size_t)INT_MAX) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700379 free(buf);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100380 return -1;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700381 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700382 decoded_len = curlx_uztosi(decoded_sz);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100383
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700384 decoded_len = conn->mech->decode(conn->app_data, buf, decoded_len,
385 level, conn);
386 if(decoded_len <= 0) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100387 free(buf);
388 return -1;
389 }
390
391 if(conn->data->set.verbose) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700392 buf[decoded_len] = '\n';
393 Curl_debug(conn->data, CURLINFO_HEADER_IN, buf, decoded_len + 1, conn);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100394 }
395
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700396 buf[decoded_len] = '\0';
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700397 if(decoded_len <= 3)
398 /* suspiciously short */
399 return 0;
400
401 if(buf[3] != '-')
402 /* safe to ignore return code */
403 (void)sscanf(buf, "%d", &ret_code);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700404
405 if(buf[decoded_len - 1] == '\n')
406 buf[decoded_len - 1] = '\0';
407 /* FIXME: Is |buffer| length always greater than |decoded_len|? */
408 strcpy(buffer, buf);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100409 free(buf);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700410 return ret_code;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100411}
412
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700413/* FIXME: The error code returned here is never checked. */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700414static int sec_set_protection_level(struct connectdata *conn)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100415{
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700416 int code;
Elliott Hughescee03382017-06-23 12:17:18 -0700417 char *pbsz;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700418 static unsigned int buffer_size = 1 << 20; /* 1048576 */
419 enum protection_level level = conn->request_data_prot;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100420
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700421 DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
422
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700423 if(!conn->sec_complete) {
424 infof(conn->data, "Trying to change the protection level after the"
425 "completion of the data exchange.\n");
Kristian Monsen5ab50182010-05-14 18:53:44 +0100426 return -1;
427 }
428
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700429 /* Bail out if we try to set up the same level */
430 if(conn->data_prot == level)
431 return 0;
432
433 if(level) {
434 code = ftp_send_command(conn, "PBSZ %u", buffer_size);
435 if(code < 0)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100436 return -1;
437
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700438 if(code/100 != 2) {
439 failf(conn->data, "Failed to set the protection's buffer size.");
Kristian Monsen5ab50182010-05-14 18:53:44 +0100440 return -1;
441 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700442 conn->buffer_size = buffer_size;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100443
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700444 pbsz = strstr(conn->data->state.buffer, "PBSZ=");
445 if(pbsz) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700446 /* ignore return code, use default value if it fails */
447 (void)sscanf(pbsz, "PBSZ=%u", &buffer_size);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700448 if(buffer_size < conn->buffer_size)
449 conn->buffer_size = buffer_size;
450 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100451 }
452
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700453 /* Now try to negiociate the protection level. */
454 code = ftp_send_command(conn, "PROT %c", level_to_char(level));
455
456 if(code < 0)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100457 return -1;
458
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700459 if(code/100 != 2) {
460 failf(conn->data, "Failed to set the protection level.");
Kristian Monsen5ab50182010-05-14 18:53:44 +0100461 return -1;
462 }
463
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700464 conn->data_prot = level;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700465 if(level == PROT_PRIVATE)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700466 conn->command_prot = level;
467
Kristian Monsen5ab50182010-05-14 18:53:44 +0100468 return 0;
469}
470
Kristian Monsen5ab50182010-05-14 18:53:44 +0100471int
472Curl_sec_request_prot(struct connectdata *conn, const char *level)
473{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700474 enum protection_level l = name_to_level(level);
475 if(l == PROT_NONE)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100476 return -1;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700477 DEBUGASSERT(l > PROT_NONE && l < PROT_LAST);
478 conn->request_data_prot = l;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100479 return 0;
480}
481
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700482static CURLcode choose_mech(struct connectdata *conn)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100483{
484 int ret;
Alex Deymoe3149cc2016-10-05 11:18:42 -0700485 struct Curl_easy *data = conn->data;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700486 void *tmp_allocation;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700487 const struct Curl_sec_client_mech *mech = &Curl_krb5_client_mech;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100488
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700489 tmp_allocation = realloc(conn->app_data, mech->size);
490 if(tmp_allocation == NULL) {
491 failf(data, "Failed realloc of size %u", mech->size);
492 mech = NULL;
493 return CURLE_OUT_OF_MEMORY;
494 }
495 conn->app_data = tmp_allocation;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100496
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700497 if(mech->init) {
498 ret = mech->init(conn->app_data);
499 if(ret) {
500 infof(data, "Failed initialization for %s. Skipping it.\n",
501 mech->name);
502 return CURLE_FAILED_INIT;
503 }
504 }
505
506 infof(data, "Trying mechanism %s...\n", mech->name);
507 ret = ftp_send_command(conn, "AUTH %s", mech->name);
508 if(ret < 0)
509 /* FIXME: This error is too generic but it is OK for now. */
510 return CURLE_COULDNT_CONNECT;
511
512 if(ret/100 != 3) {
513 switch(ret) {
514 case 504:
515 infof(data, "Mechanism %s is not supported by the server (server "
516 "returned ftp code: 504).\n", mech->name);
517 break;
518 case 534:
519 infof(data, "Mechanism %s was rejected by the server (server returned "
520 "ftp code: 534).\n", mech->name);
521 break;
522 default:
523 if(ret/100 == 5) {
524 infof(data, "server does not support the security extensions\n");
525 return CURLE_USE_SSL_FAILED;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700526 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700527 break;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700528 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700529 return CURLE_LOGIN_DENIED;
530 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100531
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700532 /* Authenticate */
533 ret = mech->auth(conn->app_data, conn);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100534
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700535 if(ret != AUTH_CONTINUE) {
536 if(ret != AUTH_OK) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700537 /* Mechanism has dumped the error to stderr, don't error here. */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100538 return -1;
539 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700540 DEBUGASSERT(ret == AUTH_OK);
541
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700542 conn->mech = mech;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100543 conn->sec_complete = 1;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700544 conn->recv[FIRSTSOCKET] = sec_recv;
545 conn->send[FIRSTSOCKET] = sec_send;
546 conn->recv[SECONDARYSOCKET] = sec_recv;
547 conn->send[SECONDARYSOCKET] = sec_send;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700548 conn->command_prot = PROT_SAFE;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100549 /* Set the requested protection level */
550 /* BLOCKING */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700551 (void)sec_set_protection_level(conn);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100552 }
553
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700554 return CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100555}
556
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700557CURLcode
558Curl_sec_login(struct connectdata *conn)
559{
560 return choose_mech(conn);
561}
562
563
Kristian Monsen5ab50182010-05-14 18:53:44 +0100564void
565Curl_sec_end(struct connectdata *conn)
566{
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700567 if(conn->mech != NULL && conn->mech->end)
568 conn->mech->end(conn->app_data);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700569 free(conn->app_data);
570 conn->app_data = NULL;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700571 if(conn->in_buffer.data) {
572 free(conn->in_buffer.data);
573 conn->in_buffer.data = NULL;
574 conn->in_buffer.size = 0;
575 conn->in_buffer.index = 0;
576 /* FIXME: Is this really needed? */
577 conn->in_buffer.eof_flag = 0;
578 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100579 conn->sec_complete = 0;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700580 conn->data_prot = PROT_CLEAR;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700581 conn->mech = NULL;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100582}
583
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700584#endif /* HAVE_GSSAPI */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700585
Kristian Monsen5ab50182010-05-14 18:53:44 +0100586#endif /* CURL_DISABLE_FTP */