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 - 2008, 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"
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[] = {
96 &Curl_krb5_client_mech,
99 &Curl_krb4_client_mech,
104 /* TODO: This function isn't actually used anywhere and should be removed */
106 Curl_sec_getc(struct connectdata *conn, FILE *F)
108 if(conn->sec_complete && conn->data_prot) {
110 if(Curl_sec_read(conn, fileno(F), &c, 1) <= 0)
119 block_read(int fd, void *buf, size_t len)
121 unsigned char *p = buf;
124 b = read(fd, p, len);
127 else if(b < 0 && (errno == EINTR || errno == EAGAIN))
128 /* TODO: this will busy loop in the EAGAIN case */
135 return p - (unsigned char*)buf;
139 block_write(int fd, const void *buf, size_t len)
141 const unsigned char *p = buf;
144 b = write(fd, p, len);
145 if(b < 0 && (errno == EINTR || errno == EAGAIN))
152 return p - (unsigned char*)buf;
156 sec_get_data(struct connectdata *conn,
157 int fd, struct krb4buffer *buf)
162 b = block_read(fd, &len, sizeof(len));
168 /* TODO: This realloc will cause a memory leak in an out of memory
170 buf->data = realloc(buf->data, len);
171 b = buf->data ? block_read(fd, buf->data, len) : -1;
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);
193 buffer_write(struct krb4buffer *buf, void *data, size_t len)
195 if(buf->index + len > buf->size) {
197 if(buf->data == NULL)
200 tmp = realloc(buf->data, buf->index + len);
204 buf->size = buf->index + len;
206 memcpy((char*)buf->data + buf->index, data, len);
212 Curl_sec_read(struct connectdata *conn, int fd, void *buffer, int length)
217 if(conn->sec_complete == 0 || conn->data_prot == 0)
218 return read(fd, buffer, length);
220 if(conn->in_buffer.eof_flag){
221 conn->in_buffer.eof_flag = 0;
225 len = buffer_read(&conn->in_buffer, buffer, length);
228 buffer = (char*)buffer + len;
231 if(sec_get_data(conn, fd, &conn->in_buffer) < 0)
233 if(conn->in_buffer.size == 0) {
235 conn->in_buffer.eof_flag = 1;
238 len = buffer_read(&conn->in_buffer, buffer, length);
241 buffer = (char*)buffer + len;
247 sec_send(struct connectdata *conn, int fd, const char *from, int length)
251 enum protection_level protlevel = conn->data_prot;
252 int iscmd = protlevel == prot_cmd;
255 if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
256 protlevel = prot_private;
258 protlevel = conn->command_prot;
260 bytes = (conn->mech->encode)(conn->app_data, from, length, protlevel,
265 bytes = Curl_base64_encode(conn->data, (char *)buf, bytes, &cmdbuf);
267 if(protlevel == prot_private)
268 block_write(fd, "ENC ", 4);
270 block_write(fd, "MIC ", 4);
271 block_write(fd, cmdbuf, bytes);
272 block_write(fd, "\r\n", 2);
273 Curl_infof(conn->data, "%s %s\n",
274 protlevel == prot_private ? "ENC" : "MIC", cmdbuf);
279 bytes = htonl(bytes);
280 block_write(fd, &bytes, sizeof(bytes));
281 block_write(fd, buf, ntohl(bytes));
288 Curl_sec_fflush_fd(struct connectdata *conn, int fd)
290 if(conn->data_prot != prot_clear) {
291 if(conn->out_buffer.index > 0){
292 Curl_sec_write(conn, fd,
293 conn->out_buffer.data, conn->out_buffer.index);
294 conn->out_buffer.index = 0;
296 sec_send(conn, fd, NULL, 0);
302 Curl_sec_write(struct connectdata *conn, int fd, const char *buffer, int length)
304 int len = conn->buffer_size;
307 if(conn->data_prot == prot_clear)
308 return write(fd, buffer, length);
310 len -= (conn->mech->overhead)(conn->app_data, conn->data_prot, len);
316 sec_send(conn, fd, buffer, len);
325 Curl_sec_send(struct connectdata *conn, int num, const char *buffer, int length)
327 curl_socket_t fd = conn->sock[num];
328 return (ssize_t)Curl_sec_write(conn, fd, buffer, length);
332 Curl_sec_putc(struct connectdata *conn, int c, FILE *F)
335 if(conn->data_prot == prot_clear)
338 buffer_write(&conn->out_buffer, &ch, 1);
339 if(c == '\n' || conn->out_buffer.index >= 1024 /* XXX */) {
340 Curl_sec_write(conn, fileno(F), conn->out_buffer.data,
341 conn->out_buffer.index);
342 conn->out_buffer.index = 0;
348 Curl_sec_read_msg(struct connectdata *conn, char *s, int level)
354 len = Curl_base64_decode(s + 4, &buf); /* XXX */
356 len = (conn->mech->decode)(conn->app_data, buf, len, level, conn);
365 if(conn->data->set.verbose) {
367 Curl_debug(conn->data, CURLINFO_HEADER_IN, (char *)buf, len + 1, conn);
375 sscanf((char *)buf, "%d", &code);
376 if(buf[len-1] == '\n')
378 strcpy(s, (char *)buf);
383 enum protection_level
384 Curl_set_command_prot(struct connectdata *conn, enum protection_level level)
386 enum protection_level old = conn->command_prot;
387 conn->command_prot = level;
392 sec_prot_internal(struct connectdata *conn, int level)
395 unsigned int s = 1048576;
398 if(!conn->sec_complete){
399 infof(conn->data, "No security data exchange has taken place.\n");
405 if(Curl_ftpsendf(conn, "PBSZ %u", s))
408 if(Curl_GetFTPResponse(&nread, conn, &code))
412 failf(conn->data, "Failed to set protection buffer size.");
415 conn->buffer_size = s;
417 p = strstr(conn->data->state.buffer, "PBSZ=");
419 sscanf(p, "PBSZ=%u", &s);
420 if(s < conn->buffer_size)
421 conn->buffer_size = s;
424 if(Curl_ftpsendf(conn, "PROT %c", level["CSEP"]))
427 if(Curl_GetFTPResponse(&nread, conn, NULL))
430 if(conn->data->state.buffer[0] != '2'){
431 failf(conn->data, "Failed to set protection level.");
435 conn->data_prot = (enum protection_level)level;
436 if(level == prot_private)
437 conn->command_prot = (enum protection_level)level;
442 Curl_sec_set_protection_level(struct connectdata *conn)
444 if(conn->sec_complete && conn->data_prot != conn->request_data_prot)
445 sec_prot_internal(conn, conn->request_data_prot);
450 Curl_sec_request_prot(struct connectdata *conn, const char *level)
452 int l = name_to_level(level);
455 conn->request_data_prot = (enum protection_level)l;
460 Curl_sec_login(struct connectdata *conn)
463 const struct Curl_sec_client_mech * const *m;
465 struct SessionHandle *data=conn->data;
468 for(m = mechs; *m && (*m)->name; m++) {
471 tmp = realloc(conn->app_data, (*m)->size);
473 failf (data, "realloc %u failed", (*m)->size);
476 conn->app_data = tmp;
478 if((*m)->init && (*(*m)->init)(conn->app_data) != 0) {
479 infof(data, "Skipping %s...\n", (*m)->name);
482 infof(data, "Trying %s...\n", (*m)->name);
484 if(Curl_ftpsendf(conn, "AUTH %s", (*m)->name))
487 if(Curl_GetFTPResponse(&nread, conn, &ftpcode))
490 if(conn->data->state.buffer[0] != '3'){
494 "%s is not supported by the server.\n", (*m)->name);
497 infof(data, "%s rejected as security mechanism.\n", (*m)->name);
500 if(conn->data->state.buffer[0] == '5') {
501 infof(data, "The server doesn't support the FTP "
502 "security extensions.\n");
510 ret = (*(*m)->auth)(conn->app_data, conn);
512 if(ret == AUTH_CONTINUE)
514 else if(ret != AUTH_OK){
515 /* mechanism is supposed to output error string */
519 conn->sec_complete = 1;
520 conn->command_prot = prot_safe;
521 /* Set the requested protection level */
523 Curl_sec_set_protection_level(conn);
531 Curl_sec_end(struct connectdata *conn)
533 if(conn->mech != NULL) {
535 (conn->mech->end)(conn->app_data);
536 memset(conn->app_data, 0, conn->mech->size);
537 free(conn->app_data);
538 conn->app_data = NULL;
540 conn->sec_complete = 0;
541 conn->data_prot = (enum protection_level)0;
545 #endif /* HAVE_KRB4 */
546 #endif /* CURL_DISABLE_FTP */