1 /* 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.
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
10 * Copyright (c) 1998, 1999, 2017 Kungliga Tekniska Högskolan
11 * (Royal Institute of Technology, Stockholm, Sweden).
13 * Copyright (C) 2001 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
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.
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.
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
44 #include "curl_setup.h"
46 #ifndef CURL_DISABLE_FTP
56 #include "curl_base64.h"
57 #include "curl_memory.h"
64 /* The last #include file should be: */
68 enum protection_level level;
71 { PROT_CLEAR, "clear" },
72 { PROT_SAFE, "safe" },
73 { PROT_CONFIDENTIAL, "confidential" },
74 { PROT_PRIVATE, "private" }
77 static enum protection_level
78 name_to_level(const char *name)
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;
87 /* Convert a protocol |level| to its char representation.
88 We take an int to catch programming mistakes. */
89 static char level_to_char(int level)
96 case PROT_CONFIDENTIAL:
103 /* Those 2 cases should not be reached! */
107 /* Default to the most secure alternative. */
111 /* 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. */
113 static int ftp_send_command(struct connectdata *conn, const char *message, ...)
118 char print_buffer[50];
120 va_start(args, message);
121 vsnprintf(print_buffer, sizeof(print_buffer), message, args);
124 if(Curl_ftpsend(conn, print_buffer)) {
128 if(Curl_GetFTPResponse(&nread, conn, &ftp_code))
132 (void)nread; /* Unused */
136 /* Read |len| from the socket |fd| and store it in |to|. Return a CURLcode
137 saying whether an error occurred or CURLE_OK if |len| was read. */
139 socket_read(curl_socket_t fd, void *to, size_t len)
146 result = Curl_read_plain(fd, to_p, len, &nread);
152 /* FIXME: We are doing a busy wait */
153 if(result == CURLE_AGAIN)
162 /* Write |len| bytes from the buffer |to| to the socket |fd|. Return a
163 CURLcode saying whether an error occurred or CURLE_OK if |len| was
166 socket_write(struct connectdata *conn, curl_socket_t fd, const void *to,
169 const char *to_p = to;
174 result = Curl_write_plain(conn, fd, to_p, len, &written);
180 /* FIXME: We are doing a busy wait */
181 if(result == CURLE_AGAIN)
189 static CURLcode read_data(struct connectdata *conn,
191 struct krb5buffer *buf)
197 result = socket_read(fd, &len, sizeof(len));
202 /* only realloc if there was a length */
204 tmp = Curl_saferealloc(buf->data, len);
207 return CURLE_OUT_OF_MEMORY;
210 result = socket_read(fd, buf->data, len);
213 buf->size = conn->mech->decode(conn->app_data, buf->data, len,
214 conn->data_prot, conn);
220 buffer_read(struct krb5buffer *buf, void *data, size_t len)
222 if(buf->size - buf->index < len)
223 len = buf->size - buf->index;
224 memcpy(data, (char *)buf->data + buf->index, len);
229 /* Matches Curl_recv signature */
230 static ssize_t sec_recv(struct connectdata *conn, int sockindex,
231 char *buffer, size_t len, CURLcode *err)
234 size_t total_read = 0;
235 curl_socket_t fd = conn->sock[sockindex];
239 /* Handle clear text response. */
240 if(conn->sec_complete == 0 || conn->data_prot == PROT_CLEAR)
241 return read(fd, buffer, len);
243 if(conn->in_buffer.eof_flag) {
244 conn->in_buffer.eof_flag = 0;
248 bytes_read = buffer_read(&conn->in_buffer, buffer, len);
250 total_read += bytes_read;
251 buffer += bytes_read;
254 if(read_data(conn, fd, &conn->in_buffer))
256 if(conn->in_buffer.size == 0) {
258 conn->in_buffer.eof_flag = 1;
261 bytes_read = buffer_read(&conn->in_buffer, buffer, len);
263 total_read += bytes_read;
264 buffer += bytes_read;
266 /* FIXME: Check for overflow */
270 /* 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! */
273 static void do_sec_send(struct connectdata *conn, curl_socket_t fd,
274 const char *from, int length)
276 int bytes, htonl_bytes; /* 32-bit integers for htonl */
281 enum protection_level prot_level = conn->data_prot;
282 bool iscmd = (prot_level == PROT_CMD)?TRUE:FALSE;
284 DEBUGASSERT(prot_level > PROT_NONE && prot_level < PROT_LAST);
287 if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
288 prot_level = PROT_PRIVATE;
290 prot_level = conn->command_prot;
292 bytes = conn->mech->encode(conn->app_data, from, length, prot_level,
294 if(!buffer || bytes <= 0)
298 error = Curl_base64_encode(conn->data, buffer, curlx_sitouz(bytes),
299 &cmd_buffer, &cmd_size);
305 static const char *enc = "ENC ";
306 static const char *mic = "MIC ";
307 if(prot_level == PROT_PRIVATE)
308 socket_write(conn, fd, enc, 4);
310 socket_write(conn, fd, mic, 4);
312 socket_write(conn, fd, cmd_buffer, cmd_size);
313 socket_write(conn, fd, "\r\n", 2);
314 infof(conn->data, "Send: %s%s\n", prot_level == PROT_PRIVATE?enc:mic,
320 htonl_bytes = htonl(bytes);
321 socket_write(conn, fd, &htonl_bytes, sizeof(htonl_bytes));
322 socket_write(conn, fd, buffer, curlx_sitouz(bytes));
327 static ssize_t sec_write(struct connectdata *conn, curl_socket_t fd,
328 const char *buffer, size_t length)
330 ssize_t tx = 0, len = conn->buffer_size;
332 len -= conn->mech->overhead(conn->app_data, conn->data_prot,
337 if(length < (size_t)len)
340 do_sec_send(conn, fd, buffer, curlx_sztosi(len));
348 /* Matches Curl_send signature */
349 static ssize_t sec_send(struct connectdata *conn, int sockindex,
350 const void *buffer, size_t len, CURLcode *err)
352 curl_socket_t fd = conn->sock[sockindex];
354 return sec_write(conn, fd, buffer, len);
357 int Curl_sec_read_msg(struct connectdata *conn, char *buffer,
358 enum protection_level level)
360 /* decoded_len should be size_t or ssize_t but conn->mech->decode returns an
365 size_t decoded_sz = 0;
369 /* not inititalized, return error */
372 DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
374 error = Curl_base64_decode(buffer + 4, (unsigned char **)&buf, &decoded_sz);
375 if(error || decoded_sz == 0)
378 if(decoded_sz > (size_t)INT_MAX) {
382 decoded_len = curlx_uztosi(decoded_sz);
384 decoded_len = conn->mech->decode(conn->app_data, buf, decoded_len,
386 if(decoded_len <= 0) {
391 if(conn->data->set.verbose) {
392 buf[decoded_len] = '\n';
393 Curl_debug(conn->data, CURLINFO_HEADER_IN, buf, decoded_len + 1, conn);
396 buf[decoded_len] = '\0';
398 /* suspiciously short */
402 /* safe to ignore return code */
403 (void)sscanf(buf, "%d", &ret_code);
405 if(buf[decoded_len - 1] == '\n')
406 buf[decoded_len - 1] = '\0';
407 /* FIXME: Is |buffer| length always greater than |decoded_len|? */
413 /* FIXME: The error code returned here is never checked. */
414 static int sec_set_protection_level(struct connectdata *conn)
418 static unsigned int buffer_size = 1 << 20; /* 1048576 */
419 enum protection_level level = conn->request_data_prot;
421 DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
423 if(!conn->sec_complete) {
424 infof(conn->data, "Trying to change the protection level after the"
425 "completion of the data exchange.\n");
429 /* Bail out if we try to set up the same level */
430 if(conn->data_prot == level)
434 code = ftp_send_command(conn, "PBSZ %u", buffer_size);
439 failf(conn->data, "Failed to set the protection's buffer size.");
442 conn->buffer_size = buffer_size;
444 pbsz = strstr(conn->data->state.buffer, "PBSZ=");
446 /* ignore return code, use default value if it fails */
447 (void)sscanf(pbsz, "PBSZ=%u", &buffer_size);
448 if(buffer_size < conn->buffer_size)
449 conn->buffer_size = buffer_size;
453 /* Now try to negiociate the protection level. */
454 code = ftp_send_command(conn, "PROT %c", level_to_char(level));
460 failf(conn->data, "Failed to set the protection level.");
464 conn->data_prot = level;
465 if(level == PROT_PRIVATE)
466 conn->command_prot = level;
472 Curl_sec_request_prot(struct connectdata *conn, const char *level)
474 enum protection_level l = name_to_level(level);
477 DEBUGASSERT(l > PROT_NONE && l < PROT_LAST);
478 conn->request_data_prot = l;
482 static CURLcode choose_mech(struct connectdata *conn)
485 struct Curl_easy *data = conn->data;
486 void *tmp_allocation;
487 const struct Curl_sec_client_mech *mech = &Curl_krb5_client_mech;
489 tmp_allocation = realloc(conn->app_data, mech->size);
490 if(tmp_allocation == NULL) {
491 failf(data, "Failed realloc of size %u", mech->size);
493 return CURLE_OUT_OF_MEMORY;
495 conn->app_data = tmp_allocation;
498 ret = mech->init(conn->app_data);
500 infof(data, "Failed initialization for %s. Skipping it.\n",
502 return CURLE_FAILED_INIT;
506 infof(data, "Trying mechanism %s...\n", mech->name);
507 ret = ftp_send_command(conn, "AUTH %s", mech->name);
509 /* FIXME: This error is too generic but it is OK for now. */
510 return CURLE_COULDNT_CONNECT;
515 infof(data, "Mechanism %s is not supported by the server (server "
516 "returned ftp code: 504).\n", mech->name);
519 infof(data, "Mechanism %s was rejected by the server (server returned "
520 "ftp code: 534).\n", mech->name);
524 infof(data, "server does not support the security extensions\n");
525 return CURLE_USE_SSL_FAILED;
529 return CURLE_LOGIN_DENIED;
533 ret = mech->auth(conn->app_data, conn);
535 if(ret != AUTH_CONTINUE) {
537 /* Mechanism has dumped the error to stderr, don't error here. */
540 DEBUGASSERT(ret == AUTH_OK);
543 conn->sec_complete = 1;
544 conn->recv[FIRSTSOCKET] = sec_recv;
545 conn->send[FIRSTSOCKET] = sec_send;
546 conn->recv[SECONDARYSOCKET] = sec_recv;
547 conn->send[SECONDARYSOCKET] = sec_send;
548 conn->command_prot = PROT_SAFE;
549 /* Set the requested protection level */
551 (void)sec_set_protection_level(conn);
558 Curl_sec_login(struct connectdata *conn)
560 return choose_mech(conn);
565 Curl_sec_end(struct connectdata *conn)
567 if(conn->mech != NULL && conn->mech->end)
568 conn->mech->end(conn->app_data);
569 free(conn->app_data);
570 conn->app_data = NULL;
571 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;
579 conn->sec_complete = 0;
580 conn->data_prot = PROT_CLEAR;
584 #endif /* HAVE_GSSAPI */
586 #endif /* CURL_DISABLE_FTP */