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 - 2010, 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)
61 #include "curl_base64.h"
62 #include "curl_memory.h"
68 /* The last #include file should be: */
72 enum protection_level level;
75 { prot_clear, "clear" },
76 { prot_safe, "safe" },
77 { prot_confidential, "confidential" },
78 { prot_private, "private" }
81 static enum protection_level
82 name_to_level(const char *name)
85 for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++)
86 if(checkprefix(name, level_names[i].name))
87 return level_names[i].level;
88 return (enum protection_level)-1;
91 /* Convert a protocol |level| to its char representation.
92 We take an int to catch programming mistakes. */
93 static char level_to_char(int level) {
99 case prot_confidential:
106 /* Those 2 cases should not be reached! */
110 /* Default to the most secure alternative. */
114 static const struct Curl_sec_client_mech * const mechs[] = {
115 #if defined(HAVE_GSSAPI)
116 &Curl_krb5_client_mech,
118 #if defined(HAVE_KRB4)
119 &Curl_krb4_client_mech,
124 /* Send an FTP command defined by |message| and the optional arguments. The
125 function returns the ftp_code. If an error occurs, -1 is returned. */
126 static int ftp_send_command(struct connectdata *conn, const char *message, ...)
131 char print_buffer[50];
133 va_start(args, message);
134 vsnprintf(print_buffer, sizeof(print_buffer), message, args);
137 if(Curl_ftpsendf(conn, print_buffer) != CURLE_OK) {
141 if(Curl_GetFTPResponse(&nread, conn, &ftp_code) != CURLE_OK)
145 (void)nread; /* Unused */
149 /* Read |len| from the socket |fd| and store it in |to|. Return a CURLcode
150 saying whether an error occured or CURLE_OK if |len| was read. */
152 socket_read(curl_socket_t fd, void *to, size_t len)
159 code = Curl_read_plain(fd, to_p, len, &nread);
160 if(code == CURLE_OK) {
165 /* FIXME: We are doing a busy wait */
166 if(code == CURLE_AGAIN)
175 /* Write |len| bytes from the buffer |to| to the socket |fd|. Return a
176 CURLcode saying whether an error occured or CURLE_OK if |len| was
179 socket_write(struct connectdata *conn, curl_socket_t fd, const void *to,
182 const char *to_p = to;
187 code = Curl_write_plain(conn, fd, to_p, len, &written);
188 if(code == CURLE_OK) {
193 /* FIXME: We are doing a busy wait */
194 if(code == CURLE_AGAIN)
202 static CURLcode read_data(struct connectdata *conn,
204 struct krb4buffer *buf)
210 ret = socket_read(fd, &len, sizeof(len));
215 tmp = realloc(buf->data, len);
217 return CURLE_OUT_OF_MEMORY;
219 ret = socket_read(fd, buf->data, len);
222 buf->size = conn->mech->decode(conn->app_data, buf->data, len,
223 conn->data_prot, conn);
229 buffer_read(struct krb4buffer *buf, void *data, size_t len)
231 if(buf->size - buf->index < len)
232 len = buf->size - buf->index;
233 memcpy(data, (char*)buf->data + buf->index, len);
238 /* Matches Curl_recv signature */
239 static ssize_t sec_recv(struct connectdata *conn, int sockindex,
240 char *buffer, size_t len, CURLcode *err)
243 size_t total_read = 0;
244 curl_socket_t fd = conn->sock[sockindex];
248 if(conn->in_buffer.eof_flag) {
249 conn->in_buffer.eof_flag = 0;
253 bytes_read = buffer_read(&conn->in_buffer, buffer, len);
255 total_read += bytes_read;
256 buffer += bytes_read;
259 if(read_data(conn, fd, &conn->in_buffer) != CURLE_OK)
261 if(conn->in_buffer.size == 0) {
263 conn->in_buffer.eof_flag = 1;
266 bytes_read = buffer_read(&conn->in_buffer, buffer, len);
268 total_read += bytes_read;
269 buffer += bytes_read;
271 /* FIXME: Check for overflow */
275 /* Send |length| bytes from |from| to the |fd| socket taking care of encoding
276 and negociating with the server. |from| can be NULL. */
277 /* FIXME: We don't check for errors nor report any! */
278 static void do_sec_send(struct connectdata *conn, curl_socket_t fd,
279 const char *from, int length)
285 enum protection_level prot_level = conn->data_prot;
286 bool iscmd = prot_level == prot_cmd;
289 if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
290 prot_level = prot_private;
292 prot_level = conn->command_prot;
294 bytes = conn->mech->encode(conn->app_data, from, length, prot_level,
295 (void**)&buffer, conn);
297 bytes = Curl_base64_encode(conn->data, buffer, bytes, &cmd_buffer);
299 static const char *enc = "ENC ";
300 static const char *mic = "MIC ";
301 if(prot_level == prot_private)
302 socket_write(conn, fd, enc, 4);
304 socket_write(conn, fd, mic, 4);
306 socket_write(conn, fd, cmd_buffer, bytes);
307 socket_write(conn, fd, "\r\n", 2);
308 infof(conn->data, "Send: %s%s", prot_level == prot_private?enc:mic,
314 htonl_bytes = htonl(bytes);
315 socket_write(conn, fd, &htonl_bytes, sizeof(htonl_bytes));
316 socket_write(conn, fd, buffer, bytes);
321 static ssize_t sec_write(struct connectdata *conn, curl_socket_t fd,
322 const char *buffer, size_t length)
324 /* FIXME: Check for overflow */
325 ssize_t len = conn->buffer_size;
328 len -= conn->mech->overhead(conn->app_data, conn->data_prot, len);
332 if(len >= 0 || length < (size_t)len) {
333 /* FIXME: Check for overflow. */
336 do_sec_send(conn, fd, buffer, len);
344 /* FIXME: fd should be a curl_socket_t */
345 int Curl_sec_fflush_fd(struct connectdata *conn, int fd)
347 if(conn->data_prot == prot_clear)
350 /* Force a flush by trying to send no data */
351 do_sec_send(conn, fd, NULL, 0);
355 /* Matches Curl_send signature */
356 static ssize_t sec_send(struct connectdata *conn, int sockindex,
357 const void *buffer, size_t len, CURLcode *err)
359 curl_socket_t fd = conn->sock[sockindex];
361 return sec_write(conn, fd, buffer, len);
364 /* FIXME: |level| should not be an int but a struct protection_level */
365 int Curl_sec_read_msg(struct connectdata *conn, char *buffer, int level)
367 /* decoded_len should be size_t or ssize_t but conn->mech->decode returns an
373 decoded_len = Curl_base64_decode(buffer + 4, (unsigned char **)&buf);
374 if(decoded_len <= 0) {
379 decoded_len = conn->mech->decode(conn->app_data, buf, decoded_len,
381 if(decoded_len <= 0) {
386 if(conn->data->set.verbose) {
387 buf[decoded_len] = '\n';
388 Curl_debug(conn->data, CURLINFO_HEADER_IN, buf, decoded_len + 1, conn);
391 buf[decoded_len] = '\0';
392 DEBUGASSERT(decoded_len > 3);
396 /* Check for error? */
397 sscanf(buf, "%d", &ret_code);
400 if(buf[decoded_len - 1] == '\n')
401 buf[decoded_len - 1] = '\0';
402 /* FIXME: Is |buffer| length always greater than |decoded_len|? */
408 enum protection_level
409 Curl_set_command_prot(struct connectdata *conn, enum protection_level level)
411 enum protection_level old = conn->command_prot;
412 conn->command_prot = level;
416 /* FIXME: The error code returned here is never checked. */
417 int Curl_sec_set_protection_level(struct connectdata *conn)
421 static unsigned int buffer_size = 1 << 20; /* 1048576 */
422 enum protection_level level = conn->request_data_prot;
424 if(!conn->sec_complete) {
425 infof(conn->data, "Trying to change the protection level after the"
426 "completion of the data exchange.");
430 /* Bail out if we try to set up the same level */
431 if(conn->data_prot == level)
435 code = ftp_send_command(conn, "PSBZ %u", buffer_size);
440 failf(conn->data, "Failed to set the protection's buffer size.");
443 conn->buffer_size = buffer_size;
445 pbsz = strstr(conn->data->state.buffer, "PBSZ=");
447 /* FIXME: Checks for errors in sscanf? */
448 sscanf(pbsz, "PBSZ=%u", &buffer_size);
449 if(buffer_size < conn->buffer_size)
450 conn->buffer_size = buffer_size;
454 /* Now try to negiociate the protection level. */
455 code = ftp_send_command(conn, "PROT %c", level_to_char(level));
461 failf(conn->data, "Failed to set the protection level.");
465 conn->data_prot = level;
466 if(level == prot_private)
467 conn->command_prot = level;
473 Curl_sec_request_prot(struct connectdata *conn, const char *level)
475 int l = name_to_level(level);
478 conn->request_data_prot = (enum protection_level)l;
482 static CURLcode choose_mech(struct connectdata *conn)
485 struct SessionHandle *data = conn->data;
486 const struct Curl_sec_client_mech * const *mech;
487 void *tmp_allocation;
488 const char *mech_name;
490 for(mech = mechs; (*mech); ++mech) {
491 mech_name = (*mech)->name;
492 /* We have no mechanism with a NULL name but keep this check */
493 DEBUGASSERT(mech_name != NULL);
494 if(mech_name == NULL) {
495 infof(data, "Skipping mechanism with empty name (%p)", mech);
498 tmp_allocation = realloc(conn->app_data, (*mech)->size);
499 if(tmp_allocation == NULL) {
500 failf(data, "Failed realloc of size %u", (*mech)->size);
502 return CURLE_OUT_OF_MEMORY;
504 conn->app_data = tmp_allocation;
507 ret = (*mech)->init(conn);
509 infof(data, "Failed initialization for %s. Skipping it.", mech_name);
514 infof(data, "Trying mechanism %s...", mech_name);
515 ret = ftp_send_command(conn, "AUTH %s", mech_name);
517 /* FIXME: This error is too generic but it is OK for now. */
518 return CURLE_COULDNT_CONNECT;
523 infof(data, "Mechanism %s is not supported by the server (server "
524 "returned ftp code: 504).", mech_name);
527 infof(data, "Mechanism %s was rejected by the server (server returned "
528 "ftp code: 534).", mech_name);
532 infof(data, "The server does not support the security extensions.");
533 return CURLE_USE_SSL_FAILED;
541 ret = (*mech)->auth(conn->app_data, conn);
543 if(ret == AUTH_CONTINUE)
545 else if(ret != AUTH_OK) {
546 /* Mechanism has dumped the error to stderr, don't error here. */
549 DEBUGASSERT(ret == AUTH_OK);
552 conn->sec_complete = 1;
553 if (conn->data_prot != prot_clear) {
554 conn->recv[FIRSTSOCKET] = sec_recv;
555 conn->send[FIRSTSOCKET] = sec_send;
556 conn->recv[SECONDARYSOCKET] = sec_recv;
557 conn->send[SECONDARYSOCKET] = sec_send;
559 conn->command_prot = prot_safe;
560 /* Set the requested protection level */
562 Curl_sec_set_protection_level(conn);
566 return mech != NULL ? CURLE_OK : CURLE_FAILED_INIT;
570 Curl_sec_login(struct connectdata *conn)
572 CURLcode code = choose_mech(conn);
573 return code == CURLE_OK;
578 Curl_sec_end(struct connectdata *conn)
580 if(conn->mech != NULL) {
582 conn->mech->end(conn->app_data);
583 free(conn->app_data);
584 conn->app_data = NULL;
586 conn->sec_complete = 0;
587 conn->data_prot = (enum protection_level)0;
591 #endif /* HAVE_KRB4 || HAVE_GSSAPI */
593 #endif /* CURL_DISABLE_FTP */