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 Kungliga Tekniska Högskolan
11 * (Royal Institute of Technology, Stockholm, Sweden).
13 * Copyright (C) 2001 - 2011, 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
46 #ifndef CURL_DISABLE_FTP
47 #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
62 #include "curl_base64.h"
63 #include "curl_memory.h"
70 /* The last #include file should be: */
74 enum protection_level level;
77 { PROT_CLEAR, "clear" },
78 { PROT_SAFE, "safe" },
79 { PROT_CONFIDENTIAL, "confidential" },
80 { PROT_PRIVATE, "private" }
83 static enum protection_level
84 name_to_level(const char *name)
87 for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++)
88 if(checkprefix(name, level_names[i].name))
89 return level_names[i].level;
93 /* Convert a protocol |level| to its char representation.
94 We take an int to catch programming mistakes. */
95 static char level_to_char(int level) {
101 case PROT_CONFIDENTIAL:
108 /* Those 2 cases should not be reached! */
112 /* Default to the most secure alternative. */
116 static const struct Curl_sec_client_mech * const mechs[] = {
117 #if defined(HAVE_GSSAPI)
118 &Curl_krb5_client_mech,
120 #if defined(HAVE_KRB4)
121 &Curl_krb4_client_mech,
126 /* Send an FTP command defined by |message| and the optional arguments. The
127 function returns the ftp_code. If an error occurs, -1 is returned. */
128 static int ftp_send_command(struct connectdata *conn, const char *message, ...)
133 char print_buffer[50];
135 va_start(args, message);
136 vsnprintf(print_buffer, sizeof(print_buffer), message, args);
139 if(Curl_ftpsendf(conn, print_buffer) != CURLE_OK) {
143 if(Curl_GetFTPResponse(&nread, conn, &ftp_code) != CURLE_OK)
147 (void)nread; /* Unused */
151 /* Read |len| from the socket |fd| and store it in |to|. Return a CURLcode
152 saying whether an error occurred or CURLE_OK if |len| was read. */
154 socket_read(curl_socket_t fd, void *to, size_t len)
161 code = Curl_read_plain(fd, to_p, len, &nread);
162 if(code == CURLE_OK) {
167 /* FIXME: We are doing a busy wait */
168 if(code == CURLE_AGAIN)
177 /* Write |len| bytes from the buffer |to| to the socket |fd|. Return a
178 CURLcode saying whether an error occurred or CURLE_OK if |len| was
181 socket_write(struct connectdata *conn, curl_socket_t fd, const void *to,
184 const char *to_p = to;
189 code = Curl_write_plain(conn, fd, to_p, len, &written);
190 if(code == CURLE_OK) {
195 /* FIXME: We are doing a busy wait */
196 if(code == CURLE_AGAIN)
204 static CURLcode read_data(struct connectdata *conn,
206 struct krb4buffer *buf)
212 ret = socket_read(fd, &len, sizeof(len));
217 tmp = realloc(buf->data, len);
219 return CURLE_OUT_OF_MEMORY;
222 ret = socket_read(fd, buf->data, len);
225 buf->size = conn->mech->decode(conn->app_data, buf->data, len,
226 conn->data_prot, conn);
232 buffer_read(struct krb4buffer *buf, void *data, size_t len)
234 if(buf->size - buf->index < len)
235 len = buf->size - buf->index;
236 memcpy(data, (char*)buf->data + buf->index, len);
241 /* Matches Curl_recv signature */
242 static ssize_t sec_recv(struct connectdata *conn, int sockindex,
243 char *buffer, size_t len, CURLcode *err)
246 size_t total_read = 0;
247 curl_socket_t fd = conn->sock[sockindex];
251 /* Handle clear text response. */
252 if(conn->sec_complete == 0 || conn->data_prot == PROT_CLEAR)
253 return read(fd, buffer, len);
255 if(conn->in_buffer.eof_flag) {
256 conn->in_buffer.eof_flag = 0;
260 bytes_read = buffer_read(&conn->in_buffer, buffer, len);
262 total_read += bytes_read;
263 buffer += bytes_read;
266 if(read_data(conn, fd, &conn->in_buffer) != CURLE_OK)
268 if(conn->in_buffer.size == 0) {
270 conn->in_buffer.eof_flag = 1;
273 bytes_read = buffer_read(&conn->in_buffer, buffer, len);
275 total_read += bytes_read;
276 buffer += bytes_read;
278 /* FIXME: Check for overflow */
282 /* Send |length| bytes from |from| to the |fd| socket taking care of encoding
283 and negociating with the server. |from| can be NULL. */
284 /* FIXME: We don't check for errors nor report any! */
285 static void do_sec_send(struct connectdata *conn, curl_socket_t fd,
286 const char *from, int length)
288 int bytes, htonl_bytes; /* 32-bit integers for htonl */
293 enum protection_level prot_level = conn->data_prot;
294 bool iscmd = (prot_level == PROT_CMD)?TRUE:FALSE;
296 DEBUGASSERT(prot_level > PROT_NONE && prot_level < PROT_LAST);
299 if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
300 prot_level = PROT_PRIVATE;
302 prot_level = conn->command_prot;
304 bytes = conn->mech->encode(conn->app_data, from, length, prot_level,
305 (void**)&buffer, conn);
306 if(!buffer || bytes <= 0)
310 error = Curl_base64_encode(conn->data, buffer, curlx_sitouz(bytes),
311 &cmd_buffer, &cmd_size);
317 static const char *enc = "ENC ";
318 static const char *mic = "MIC ";
319 if(prot_level == PROT_PRIVATE)
320 socket_write(conn, fd, enc, 4);
322 socket_write(conn, fd, mic, 4);
324 socket_write(conn, fd, cmd_buffer, cmd_size);
325 socket_write(conn, fd, "\r\n", 2);
326 infof(conn->data, "Send: %s%s\n", prot_level == PROT_PRIVATE?enc:mic,
332 htonl_bytes = htonl(bytes);
333 socket_write(conn, fd, &htonl_bytes, sizeof(htonl_bytes));
334 socket_write(conn, fd, buffer, curlx_sitouz(bytes));
339 static ssize_t sec_write(struct connectdata *conn, curl_socket_t fd,
340 const char *buffer, size_t length)
342 /* FIXME: Check for overflow */
343 ssize_t len = conn->buffer_size;
346 len -= conn->mech->overhead(conn->app_data, conn->data_prot, len);
350 if(len >= 0 || length < (size_t)len) {
351 /* FIXME: Check for overflow. */
354 do_sec_send(conn, fd, buffer, len);
362 /* Matches Curl_send signature */
363 static ssize_t sec_send(struct connectdata *conn, int sockindex,
364 const void *buffer, size_t len, CURLcode *err)
366 curl_socket_t fd = conn->sock[sockindex];
368 return sec_write(conn, fd, buffer, len);
371 int Curl_sec_read_msg(struct connectdata *conn, char *buffer,
372 enum protection_level level)
374 /* decoded_len should be size_t or ssize_t but conn->mech->decode returns an
379 size_t decoded_sz = 0;
382 DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
384 error = Curl_base64_decode(buffer + 4, (unsigned char **)&buf, &decoded_sz);
385 if(error || decoded_sz == 0)
388 if(decoded_sz > (size_t)INT_MAX) {
392 decoded_len = curlx_uztosi(decoded_sz);
394 decoded_len = conn->mech->decode(conn->app_data, buf, decoded_len,
396 if(decoded_len <= 0) {
401 if(conn->data->set.verbose) {
402 buf[decoded_len] = '\n';
403 Curl_debug(conn->data, CURLINFO_HEADER_IN, buf, decoded_len + 1, conn);
406 buf[decoded_len] = '\0';
407 DEBUGASSERT(decoded_len > 3);
411 /* Check for error? */
412 sscanf(buf, "%d", &ret_code);
415 if(buf[decoded_len - 1] == '\n')
416 buf[decoded_len - 1] = '\0';
417 /* FIXME: Is |buffer| length always greater than |decoded_len|? */
423 /* FIXME: The error code returned here is never checked. */
424 static int sec_set_protection_level(struct connectdata *conn)
428 static unsigned int buffer_size = 1 << 20; /* 1048576 */
429 enum protection_level level = conn->request_data_prot;
431 DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
433 if(!conn->sec_complete) {
434 infof(conn->data, "Trying to change the protection level after the"
435 "completion of the data exchange.\n");
439 /* Bail out if we try to set up the same level */
440 if(conn->data_prot == level)
444 code = ftp_send_command(conn, "PBSZ %u", buffer_size);
449 failf(conn->data, "Failed to set the protection's buffer size.");
452 conn->buffer_size = buffer_size;
454 pbsz = strstr(conn->data->state.buffer, "PBSZ=");
456 /* FIXME: Checks for errors in sscanf? */
457 sscanf(pbsz, "PBSZ=%u", &buffer_size);
458 if(buffer_size < conn->buffer_size)
459 conn->buffer_size = buffer_size;
463 /* Now try to negiociate the protection level. */
464 code = ftp_send_command(conn, "PROT %c", level_to_char(level));
470 failf(conn->data, "Failed to set the protection level.");
474 conn->data_prot = level;
475 if(level == PROT_PRIVATE)
476 conn->command_prot = level;
482 Curl_sec_request_prot(struct connectdata *conn, const char *level)
484 enum protection_level l = name_to_level(level);
487 DEBUGASSERT(l > PROT_NONE && l < PROT_LAST);
488 conn->request_data_prot = l;
492 static CURLcode choose_mech(struct connectdata *conn)
495 struct SessionHandle *data = conn->data;
496 const struct Curl_sec_client_mech * const *mech;
497 void *tmp_allocation;
498 const char *mech_name;
500 for(mech = mechs; (*mech); ++mech) {
501 mech_name = (*mech)->name;
502 /* We have no mechanism with a NULL name but keep this check */
503 DEBUGASSERT(mech_name != NULL);
504 if(mech_name == NULL) {
505 infof(data, "Skipping mechanism with empty name (%p)\n", mech);
508 tmp_allocation = realloc(conn->app_data, (*mech)->size);
509 if(tmp_allocation == NULL) {
510 failf(data, "Failed realloc of size %u", (*mech)->size);
512 return CURLE_OUT_OF_MEMORY;
514 conn->app_data = tmp_allocation;
517 ret = (*mech)->init(conn->app_data);
519 infof(data, "Failed initialization for %s. Skipping it.\n", mech_name);
524 infof(data, "Trying mechanism %s...\n", mech_name);
525 ret = ftp_send_command(conn, "AUTH %s", mech_name);
527 /* FIXME: This error is too generic but it is OK for now. */
528 return CURLE_COULDNT_CONNECT;
533 infof(data, "Mechanism %s is not supported by the server (server "
534 "returned ftp code: 504).\n", mech_name);
537 infof(data, "Mechanism %s was rejected by the server (server returned "
538 "ftp code: 534).\n", mech_name);
542 infof(data, "server does not support the security extensions\n");
543 return CURLE_USE_SSL_FAILED;
551 ret = (*mech)->auth(conn->app_data, conn);
553 if(ret == AUTH_CONTINUE)
555 else if(ret != AUTH_OK) {
556 /* Mechanism has dumped the error to stderr, don't error here. */
559 DEBUGASSERT(ret == AUTH_OK);
562 conn->sec_complete = 1;
563 conn->recv[FIRSTSOCKET] = sec_recv;
564 conn->send[FIRSTSOCKET] = sec_send;
565 conn->recv[SECONDARYSOCKET] = sec_recv;
566 conn->send[SECONDARYSOCKET] = sec_send;
567 conn->command_prot = PROT_SAFE;
568 /* Set the requested protection level */
570 (void)sec_set_protection_level(conn);
574 return mech != NULL ? CURLE_OK : CURLE_FAILED_INIT;
578 Curl_sec_login(struct connectdata *conn)
580 return choose_mech(conn);
585 Curl_sec_end(struct connectdata *conn)
587 if(conn->mech != NULL && conn->mech->end)
588 conn->mech->end(conn->app_data);
590 free(conn->app_data);
591 conn->app_data = NULL;
593 if(conn->in_buffer.data) {
594 free(conn->in_buffer.data);
595 conn->in_buffer.data = NULL;
596 conn->in_buffer.size = 0;
597 conn->in_buffer.index = 0;
598 /* FIXME: Is this really needed? */
599 conn->in_buffer.eof_flag = 0;
601 conn->sec_complete = 0;
602 conn->data_prot = PROT_CLEAR;
606 #endif /* HAVE_KRB4 || HAVE_GSSAPI */
608 #endif /* CURL_DISABLE_FTP */