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)
49 #define _MPRINTF_REPLACE /* we want curl-functions instead of native ones */
50 #include <curl/mprintf.h>
65 #include "curl_base64.h"
68 #include "curl_memory.h"
71 /* The last #include file should be: */
75 enum protection_level level;
78 { prot_clear, "clear" },
79 { prot_safe, "safe" },
80 { prot_confidential, "confidential" },
81 { prot_private, "private" }
84 static enum protection_level
85 name_to_level(const char *name)
88 for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++)
89 if(checkprefix(name, level_names[i].name))
90 return level_names[i].level;
91 return (enum protection_level)-1;
94 static const struct Curl_sec_client_mech * const mechs[] = {
95 #if defined(HAVE_GSSAPI)
96 &Curl_krb5_client_mech,
98 #if defined(HAVE_KRB4)
99 &Curl_krb4_client_mech,
105 /* Read |len| from the socket |fd| and store it in |to|. Return a
106 CURLcode saying whether an error occured or CURLE_OK if |len| was read. */
108 socket_read(curl_socket_t fd, void *to, size_t len)
115 code = Curl_read_plain(fd, to_p, len, &nread);
116 if(code == CURLE_OK) {
121 /* FIXME: We are doing a busy wait */
122 if(code == CURLE_AGAIN)
131 /* Write |len| bytes from the buffer |to| to the socket |fd|. Return a
132 CURLcode saying whether an error occured or CURLE_OK if |len| was written. */
134 socket_write(struct connectdata *conn, curl_socket_t fd, const void *to, size_t len)
136 const char *to_p = to;
141 code = Curl_write_plain(conn, fd, to_p, len, &written);
142 if(code == CURLE_OK) {
147 /* FIXME: We are doing a busy wait */
148 if(code == CURLE_AGAIN)
156 static CURLcode read_data(struct connectdata *conn,
158 struct krb4buffer *buf)
164 ret = socket_read(fd, &len, sizeof(len));
169 tmp = realloc(buf->data, len);
171 return CURLE_OUT_OF_MEMORY;
173 ret = socket_read(fd, buf->data, len);
176 buf->size = (conn->mech->decode)(conn->app_data, buf->data, len,
177 conn->data_prot, conn);
183 buffer_read(struct krb4buffer *buf, void *data, size_t len)
185 if(buf->size - buf->index < len)
186 len = buf->size - buf->index;
187 memcpy(data, (char*)buf->data + buf->index, len);
192 static ssize_t sec_read(struct connectdata *conn, int num,
193 char *buffer, size_t length, CURLcode *err)
197 curl_socket_t fd = conn->sock[num];
201 if(conn->in_buffer.eof_flag) {
202 conn->in_buffer.eof_flag = 0;
206 len = buffer_read(&conn->in_buffer, buffer, length);
209 buffer = (char*)buffer + len;
212 if(read_data(conn, fd, &conn->in_buffer) != CURLE_OK)
214 if(conn->in_buffer.size == 0) {
216 conn->in_buffer.eof_flag = 1;
219 len = buffer_read(&conn->in_buffer, buffer, length);
222 buffer = (char*)buffer + len;
228 sec_send(struct connectdata *conn, int fd, const char *from, int length)
232 enum protection_level protlevel = conn->data_prot;
233 int iscmd = protlevel == prot_cmd;
236 if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
237 protlevel = prot_private;
239 protlevel = conn->command_prot;
241 bytes = (conn->mech->encode)(conn->app_data, from, length, protlevel,
246 bytes = Curl_base64_encode(conn->data, (char *)buf, bytes, &cmdbuf);
248 if(protlevel == prot_private)
249 socket_write(conn, fd, "ENC ", 4);
251 socket_write(conn, fd, "MIC ", 4);
252 socket_write(conn, fd, cmdbuf, bytes);
253 socket_write(conn, fd, "\r\n", 2);
254 Curl_infof(conn->data, "%s %s\n",
255 protlevel == prot_private ? "ENC" : "MIC", cmdbuf);
260 bytes = htonl(bytes);
261 socket_write(conn, fd, &bytes, sizeof(bytes));
262 socket_write(conn, fd, buf, ntohl(bytes));
268 static ssize_t sec_write(struct connectdata *conn, int fd,
269 const char *buffer, int length)
271 int len = conn->buffer_size;
274 len -= (conn->mech->overhead)(conn->app_data, conn->data_prot, len);
280 sec_send(conn, fd, buffer, len);
289 Curl_sec_fflush_fd(struct connectdata *conn, int fd)
291 if(conn->data_prot != prot_clear) {
292 if(conn->out_buffer.index > 0){
293 sec_write(conn, fd, conn->out_buffer.data, conn->out_buffer.index);
294 conn->out_buffer.index = 0;
296 sec_send(conn, fd, NULL, 0);
301 static ssize_t _sec_send(struct connectdata *conn, int num,
302 const void *buffer, size_t length, CURLcode *err)
304 curl_socket_t fd = conn->sock[num];
306 return sec_write(conn, fd, buffer, length);
310 Curl_sec_read_msg(struct connectdata *conn, char *s, int level)
316 len = Curl_base64_decode(s + 4, &buf); /* XXX */
318 len = (conn->mech->decode)(conn->app_data, buf, len, level, conn);
327 if(conn->data->set.verbose) {
329 Curl_debug(conn->data, CURLINFO_HEADER_IN, (char *)buf, len + 1, conn);
337 sscanf((char *)buf, "%d", &code);
338 if(buf[len-1] == '\n')
340 strcpy(s, (char *)buf);
345 enum protection_level
346 Curl_set_command_prot(struct connectdata *conn, enum protection_level level)
348 enum protection_level old = conn->command_prot;
349 conn->command_prot = level;
354 sec_prot_internal(struct connectdata *conn, int level)
357 unsigned int s = 1048576;
360 if(!conn->sec_complete){
361 infof(conn->data, "No security data exchange has taken place.\n");
367 if(Curl_ftpsendf(conn, "PBSZ %u", s))
370 if(Curl_GetFTPResponse(&nread, conn, &code))
374 failf(conn->data, "Failed to set protection buffer size.");
377 conn->buffer_size = s;
379 p = strstr(conn->data->state.buffer, "PBSZ=");
381 sscanf(p, "PBSZ=%u", &s);
382 if(s < conn->buffer_size)
383 conn->buffer_size = s;
386 if(Curl_ftpsendf(conn, "PROT %c", level["CSEP"]))
389 if(Curl_GetFTPResponse(&nread, conn, NULL))
392 if(conn->data->state.buffer[0] != '2'){
393 failf(conn->data, "Failed to set protection level.");
397 conn->data_prot = (enum protection_level)level;
398 if(level == prot_private)
399 conn->command_prot = (enum protection_level)level;
404 Curl_sec_set_protection_level(struct connectdata *conn)
406 if(conn->sec_complete && conn->data_prot != conn->request_data_prot)
407 sec_prot_internal(conn, conn->request_data_prot);
412 Curl_sec_request_prot(struct connectdata *conn, const char *level)
414 int l = name_to_level(level);
417 conn->request_data_prot = (enum protection_level)l;
422 Curl_sec_login(struct connectdata *conn)
425 const struct Curl_sec_client_mech * const *m;
427 struct SessionHandle *data=conn->data;
430 for(m = mechs; *m && (*m)->name; m++) {
433 tmp = realloc(conn->app_data, (*m)->size);
435 failf (data, "realloc %u failed", (*m)->size);
438 conn->app_data = tmp;
440 if((*m)->init && (*(*m)->init)(conn->app_data) != 0) {
441 infof(data, "Skipping %s...\n", (*m)->name);
444 infof(data, "Trying %s...\n", (*m)->name);
446 if(Curl_ftpsendf(conn, "AUTH %s", (*m)->name))
449 if(Curl_GetFTPResponse(&nread, conn, &ftpcode))
452 if(conn->data->state.buffer[0] != '3'){
456 "%s is not supported by the server.\n", (*m)->name);
459 infof(data, "%s rejected as security mechanism.\n", (*m)->name);
462 if(conn->data->state.buffer[0] == '5') {
463 infof(data, "The server doesn't support the FTP "
464 "security extensions.\n");
472 ret = (*(*m)->auth)(conn->app_data, conn);
474 if(ret == AUTH_CONTINUE)
476 else if(ret != AUTH_OK){
477 /* mechanism is supposed to output error string */
481 conn->sec_complete = 1;
482 if (conn->data_prot != prot_clear) {
483 conn->recv[FIRSTSOCKET] = sec_read;
484 conn->send[FIRSTSOCKET] = _sec_send;
485 conn->recv[SECONDARYSOCKET] = sec_read;
486 conn->send[SECONDARYSOCKET] = _sec_send;
488 conn->command_prot = prot_safe;
489 /* Set the requested protection level */
491 Curl_sec_set_protection_level(conn);
499 Curl_sec_end(struct connectdata *conn)
501 if(conn->mech != NULL) {
503 (conn->mech->end)(conn->app_data);
504 memset(conn->app_data, 0, conn->mech->size);
505 free(conn->app_data);
506 conn->app_data = NULL;
508 conn->sec_complete = 0;
509 conn->data_prot = (enum protection_level)0;
513 #endif /* HAVE_KRB4 || HAVE_GSSAPI */
515 #endif /* CURL_DISABLE_FTP */