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.
3 *
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
8 * below.
9 *
10 * Copyright (c) 1998, 1999 Kungliga Tekniska H�gskolan
11 * (Royal Institute of Technology, Stockholm, Sweden).
12 *
13 * Copyright (C) 2001 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
14 *
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 *
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 *
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.
27 *
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.
31 *
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
42 * SUCH DAMAGE. */
43
44 #include "setup.h"
45
46 #ifndef CURL_DISABLE_FTP
47 #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
48
49 #define _MPRINTF_REPLACE /* we want curl-functions instead of native ones */
50 #include <curl/mprintf.h>
51
52 #include <stdlib.h>
53 #include <string.h>
54
55 #ifdef HAVE_NETDB_H
56 #include <netdb.h>
57 #endif
58
59 #ifdef HAVE_UNISTD_H
60 #include <unistd.h>
61 #endif
62
63 #include "urldata.h"
64 #include "krb4.h"
65 #include "curl_base64.h"
66 #include "sendf.h"
67 #include "ftp.h"
68 #include "memory.h"
69 #include "rawstr.h"
70
71 /* The last #include file should be: */
72 #include "memdebug.h"
73
74 static const struct {
75 enum protection_level level;
76 const char *name;
77 } level_names[] = {
78 { prot_clear, "clear" },
79 { prot_safe, "safe" },
80 { prot_confidential, "confidential" },
81 { prot_private, "private" }
82 };
83
84 static enum protection_level
name_to_level(const char * name)85 name_to_level(const char *name)
86 {
87 int i;
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;
92 }
93
94 static const struct Curl_sec_client_mech * const mechs[] = {
95 #ifdef HAVE_GSSAPI
96 &Curl_krb5_client_mech,
97 #endif
98 #ifdef HAVE_KRB4
99 &Curl_krb4_client_mech,
100 #endif
101 NULL
102 };
103
104 /* TODO: This function isn't actually used anywhere and should be removed */
105 int
Curl_sec_getc(struct connectdata * conn,FILE * F)106 Curl_sec_getc(struct connectdata *conn, FILE *F)
107 {
108 if(conn->sec_complete && conn->data_prot) {
109 char c;
110 if(Curl_sec_read(conn, fileno(F), &c, 1) <= 0)
111 return EOF;
112 return c;
113 }
114 else
115 return getc(F);
116 }
117
118 static int
block_read(int fd,void * buf,size_t len)119 block_read(int fd, void *buf, size_t len)
120 {
121 unsigned char *p = buf;
122 int b;
123 while(len) {
124 b = read(fd, p, len);
125 if(b == 0)
126 return 0;
127 else if(b < 0 && (errno == EINTR || errno == EAGAIN))
128 /* TODO: this will busy loop in the EAGAIN case */
129 continue;
130 else if(b < 0)
131 return -1;
132 len -= b;
133 p += b;
134 }
135 return p - (unsigned char*)buf;
136 }
137
138 static int
block_write(int fd,const void * buf,size_t len)139 block_write(int fd, const void *buf, size_t len)
140 {
141 const unsigned char *p = buf;
142 int b;
143 while(len) {
144 b = write(fd, p, len);
145 if(b < 0 && (errno == EINTR || errno == EAGAIN))
146 continue;
147 else if(b < 0)
148 return -1;
149 len -= b;
150 p += b;
151 }
152 return p - (unsigned char*)buf;
153 }
154
155 static int
sec_get_data(struct connectdata * conn,int fd,struct krb4buffer * buf)156 sec_get_data(struct connectdata *conn,
157 int fd, struct krb4buffer *buf)
158 {
159 int len;
160 int b;
161
162 b = block_read(fd, &len, sizeof(len));
163 if(b == 0)
164 return 0;
165 else if(b < 0)
166 return -1;
167 len = ntohl(len);
168 /* TODO: This realloc will cause a memory leak in an out of memory
169 * condition */
170 buf->data = realloc(buf->data, len);
171 b = buf->data ? block_read(fd, buf->data, len) : -1;
172 if(b == 0)
173 return 0;
174 else if(b < 0)
175 return -1;
176 buf->size = (conn->mech->decode)(conn->app_data, buf->data, len,
177 conn->data_prot, conn);
178 buf->index = 0;
179 return 0;
180 }
181
182 static size_t
buffer_read(struct krb4buffer * buf,void * data,size_t len)183 buffer_read(struct krb4buffer *buf, void *data, size_t len)
184 {
185 if(buf->size - buf->index < len)
186 len = buf->size - buf->index;
187 memcpy(data, (char*)buf->data + buf->index, len);
188 buf->index += len;
189 return len;
190 }
191
192 static size_t
buffer_write(struct krb4buffer * buf,void * data,size_t len)193 buffer_write(struct krb4buffer *buf, void *data, size_t len)
194 {
195 if(buf->index + len > buf->size) {
196 void *tmp;
197 if(buf->data == NULL)
198 tmp = malloc(1024);
199 else
200 tmp = realloc(buf->data, buf->index + len);
201 if(tmp == NULL)
202 return -1;
203 buf->data = tmp;
204 buf->size = buf->index + len;
205 }
206 memcpy((char*)buf->data + buf->index, data, len);
207 buf->index += len;
208 return len;
209 }
210
211 int
Curl_sec_read(struct connectdata * conn,int fd,void * buffer,int length)212 Curl_sec_read(struct connectdata *conn, int fd, void *buffer, int length)
213 {
214 size_t len;
215 int rx = 0;
216
217 if(conn->sec_complete == 0 || conn->data_prot == 0)
218 return read(fd, buffer, length);
219
220 if(conn->in_buffer.eof_flag){
221 conn->in_buffer.eof_flag = 0;
222 return 0;
223 }
224
225 len = buffer_read(&conn->in_buffer, buffer, length);
226 length -= len;
227 rx += len;
228 buffer = (char*)buffer + len;
229
230 while(length) {
231 if(sec_get_data(conn, fd, &conn->in_buffer) < 0)
232 return -1;
233 if(conn->in_buffer.size == 0) {
234 if(rx)
235 conn->in_buffer.eof_flag = 1;
236 return rx;
237 }
238 len = buffer_read(&conn->in_buffer, buffer, length);
239 length -= len;
240 rx += len;
241 buffer = (char*)buffer + len;
242 }
243 return rx;
244 }
245
246 static int
sec_send(struct connectdata * conn,int fd,const char * from,int length)247 sec_send(struct connectdata *conn, int fd, const char *from, int length)
248 {
249 int bytes;
250 void *buf;
251 enum protection_level protlevel = conn->data_prot;
252 int iscmd = protlevel == prot_cmd;
253
254 if(iscmd) {
255 if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
256 protlevel = prot_private;
257 else
258 protlevel = conn->command_prot;
259 }
260 bytes = (conn->mech->encode)(conn->app_data, from, length, protlevel,
261 &buf, conn);
262 if(iscmd) {
263 char *cmdbuf;
264
265 bytes = Curl_base64_encode(conn->data, (char *)buf, bytes, &cmdbuf);
266 if(bytes > 0) {
267 if(protlevel == prot_private)
268 block_write(fd, "ENC ", 4);
269 else
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);
275 free(cmdbuf);
276 }
277 }
278 else {
279 bytes = htonl(bytes);
280 block_write(fd, &bytes, sizeof(bytes));
281 block_write(fd, buf, ntohl(bytes));
282 }
283 free(buf);
284 return length;
285 }
286
287 int
Curl_sec_fflush_fd(struct connectdata * conn,int fd)288 Curl_sec_fflush_fd(struct connectdata *conn, int fd)
289 {
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;
295 }
296 sec_send(conn, fd, NULL, 0);
297 }
298 return 0;
299 }
300
301 int
Curl_sec_write(struct connectdata * conn,int fd,const char * buffer,int length)302 Curl_sec_write(struct connectdata *conn, int fd, const char *buffer, int length)
303 {
304 int len = conn->buffer_size;
305 int tx = 0;
306
307 if(conn->data_prot == prot_clear)
308 return write(fd, buffer, length);
309
310 len -= (conn->mech->overhead)(conn->app_data, conn->data_prot, len);
311 if(len <= 0)
312 len = length;
313 while(length){
314 if(length < len)
315 len = length;
316 sec_send(conn, fd, buffer, len);
317 length -= len;
318 buffer += len;
319 tx += len;
320 }
321 return tx;
322 }
323
324 ssize_t
Curl_sec_send(struct connectdata * conn,int num,const char * buffer,int length)325 Curl_sec_send(struct connectdata *conn, int num, const char *buffer, int length)
326 {
327 curl_socket_t fd = conn->sock[num];
328 return (ssize_t)Curl_sec_write(conn, fd, buffer, length);
329 }
330
331 int
Curl_sec_putc(struct connectdata * conn,int c,FILE * F)332 Curl_sec_putc(struct connectdata *conn, int c, FILE *F)
333 {
334 char ch = c;
335 if(conn->data_prot == prot_clear)
336 return putc(c, F);
337
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;
343 }
344 return c;
345 }
346
347 int
Curl_sec_read_msg(struct connectdata * conn,char * s,int level)348 Curl_sec_read_msg(struct connectdata *conn, char *s, int level)
349 {
350 int len;
351 unsigned char *buf;
352 int code;
353
354 len = Curl_base64_decode(s + 4, &buf); /* XXX */
355 if(len > 0)
356 len = (conn->mech->decode)(conn->app_data, buf, len, level, conn);
357 else
358 return -1;
359
360 if(len < 0) {
361 free(buf);
362 return -1;
363 }
364
365 if(conn->data->set.verbose) {
366 buf[len] = '\n';
367 Curl_debug(conn->data, CURLINFO_HEADER_IN, (char *)buf, len + 1, conn);
368 }
369
370 buf[len] = '\0';
371
372 if(buf[3] == '-')
373 code = 0;
374 else
375 sscanf((char *)buf, "%d", &code);
376 if(buf[len-1] == '\n')
377 buf[len-1] = '\0';
378 strcpy(s, (char *)buf);
379 free(buf);
380 return code;
381 }
382
383 enum protection_level
Curl_set_command_prot(struct connectdata * conn,enum protection_level level)384 Curl_set_command_prot(struct connectdata *conn, enum protection_level level)
385 {
386 enum protection_level old = conn->command_prot;
387 conn->command_prot = level;
388 return old;
389 }
390
391 static int
sec_prot_internal(struct connectdata * conn,int level)392 sec_prot_internal(struct connectdata *conn, int level)
393 {
394 char *p;
395 unsigned int s = 1048576;
396 ssize_t nread;
397
398 if(!conn->sec_complete){
399 infof(conn->data, "No security data exchange has taken place.\n");
400 return -1;
401 }
402
403 if(level){
404 int code;
405 if(Curl_ftpsendf(conn, "PBSZ %u", s))
406 return -1;
407
408 if(Curl_GetFTPResponse(&nread, conn, &code))
409 return -1;
410
411 if(code/100 != 2){
412 failf(conn->data, "Failed to set protection buffer size.");
413 return -1;
414 }
415 conn->buffer_size = s;
416
417 p = strstr(conn->data->state.buffer, "PBSZ=");
418 if(p)
419 sscanf(p, "PBSZ=%u", &s);
420 if(s < conn->buffer_size)
421 conn->buffer_size = s;
422 }
423
424 if(Curl_ftpsendf(conn, "PROT %c", level["CSEP"]))
425 return -1;
426
427 if(Curl_GetFTPResponse(&nread, conn, NULL))
428 return -1;
429
430 if(conn->data->state.buffer[0] != '2'){
431 failf(conn->data, "Failed to set protection level.");
432 return -1;
433 }
434
435 conn->data_prot = (enum protection_level)level;
436 if(level == prot_private)
437 conn->command_prot = (enum protection_level)level;
438 return 0;
439 }
440
441 void
Curl_sec_set_protection_level(struct connectdata * conn)442 Curl_sec_set_protection_level(struct connectdata *conn)
443 {
444 if(conn->sec_complete && conn->data_prot != conn->request_data_prot)
445 sec_prot_internal(conn, conn->request_data_prot);
446 }
447
448
449 int
Curl_sec_request_prot(struct connectdata * conn,const char * level)450 Curl_sec_request_prot(struct connectdata *conn, const char *level)
451 {
452 int l = name_to_level(level);
453 if(l == -1)
454 return -1;
455 conn->request_data_prot = (enum protection_level)l;
456 return 0;
457 }
458
459 int
Curl_sec_login(struct connectdata * conn)460 Curl_sec_login(struct connectdata *conn)
461 {
462 int ret;
463 const struct Curl_sec_client_mech * const *m;
464 ssize_t nread;
465 struct SessionHandle *data=conn->data;
466 int ftpcode;
467
468 for(m = mechs; *m && (*m)->name; m++) {
469 void *tmp;
470
471 tmp = realloc(conn->app_data, (*m)->size);
472 if(tmp == NULL) {
473 failf (data, "realloc %u failed", (*m)->size);
474 return -1;
475 }
476 conn->app_data = tmp;
477
478 if((*m)->init && (*(*m)->init)(conn->app_data) != 0) {
479 infof(data, "Skipping %s...\n", (*m)->name);
480 continue;
481 }
482 infof(data, "Trying %s...\n", (*m)->name);
483
484 if(Curl_ftpsendf(conn, "AUTH %s", (*m)->name))
485 return -1;
486
487 if(Curl_GetFTPResponse(&nread, conn, &ftpcode))
488 return -1;
489
490 if(conn->data->state.buffer[0] != '3'){
491 switch(ftpcode) {
492 case 504:
493 infof(data,
494 "%s is not supported by the server.\n", (*m)->name);
495 break;
496 case 534:
497 infof(data, "%s rejected as security mechanism.\n", (*m)->name);
498 break;
499 default:
500 if(conn->data->state.buffer[0] == '5') {
501 infof(data, "The server doesn't support the FTP "
502 "security extensions.\n");
503 return -1;
504 }
505 break;
506 }
507 continue;
508 }
509
510 ret = (*(*m)->auth)(conn->app_data, conn);
511
512 if(ret == AUTH_CONTINUE)
513 continue;
514 else if(ret != AUTH_OK){
515 /* mechanism is supposed to output error string */
516 return -1;
517 }
518 conn->mech = *m;
519 conn->sec_complete = 1;
520 conn->command_prot = prot_safe;
521 /* Set the requested protection level */
522 /* BLOCKING */
523 Curl_sec_set_protection_level(conn);
524 break;
525 }
526
527 return *m == NULL;
528 }
529
530 void
Curl_sec_end(struct connectdata * conn)531 Curl_sec_end(struct connectdata *conn)
532 {
533 if(conn->mech != NULL) {
534 if(conn->mech->end)
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;
539 }
540 conn->sec_complete = 0;
541 conn->data_prot = (enum protection_level)0;
542 conn->mech=NULL;
543 }
544
545 #endif /* HAVE_KRB4 */
546 #endif /* CURL_DISABLE_FTP */
547