1 | /* GSSAPI/krb5 support for FTP - loosely based on old krb4.c
|
---|
2 | *
|
---|
3 | * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska Högskolan
|
---|
4 | * (Royal Institute of Technology, Stockholm, Sweden).
|
---|
5 | * Copyright (C) Daniel Stenberg
|
---|
6 | * All rights reserved.
|
---|
7 | *
|
---|
8 | * SPDX-License-Identifier: BSD-3-Clause
|
---|
9 | *
|
---|
10 | * Redistribution and use in source and binary forms, with or without
|
---|
11 | * modification, are permitted provided that the following conditions
|
---|
12 | * are met:
|
---|
13 | *
|
---|
14 | * 1. Redistributions of source code must retain the above copyright
|
---|
15 | * notice, this list of conditions and the following disclaimer.
|
---|
16 | *
|
---|
17 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
18 | * notice, this list of conditions and the following disclaimer in the
|
---|
19 | * documentation and/or other materials provided with the distribution.
|
---|
20 | *
|
---|
21 | * 3. Neither the name of the Institute nor the names of its contributors
|
---|
22 | * may be used to endorse or promote products derived from this software
|
---|
23 | * without specific prior written permission.
|
---|
24 | *
|
---|
25 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
---|
26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
---|
29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
35 | * SUCH DAMAGE. */
|
---|
36 |
|
---|
37 | #include "curl_setup.h"
|
---|
38 |
|
---|
39 | #if defined(HAVE_GSSAPI) && !defined(CURL_DISABLE_FTP)
|
---|
40 |
|
---|
41 | #ifdef HAVE_NETDB_H
|
---|
42 | #include <netdb.h>
|
---|
43 | #endif
|
---|
44 | #ifdef HAVE_ARPA_INET_H
|
---|
45 | #include <arpa/inet.h>
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | #include "urldata.h"
|
---|
49 | #include "cfilters.h"
|
---|
50 | #include "cf-socket.h"
|
---|
51 | #include "curl_base64.h"
|
---|
52 | #include "ftp.h"
|
---|
53 | #include "curl_gssapi.h"
|
---|
54 | #include "sendf.h"
|
---|
55 | #include "transfer.h"
|
---|
56 | #include "curl_krb5.h"
|
---|
57 | #include "warnless.h"
|
---|
58 | #include "strcase.h"
|
---|
59 | #include "strdup.h"
|
---|
60 |
|
---|
61 | /* The last 3 #include files should be in this order */
|
---|
62 | #include "curl_printf.h"
|
---|
63 | #include "curl_memory.h"
|
---|
64 | #include "memdebug.h"
|
---|
65 |
|
---|
66 | static CURLcode ftpsend(struct Curl_easy *data, struct connectdata *conn,
|
---|
67 | const char *cmd)
|
---|
68 | {
|
---|
69 | size_t bytes_written;
|
---|
70 | #define SBUF_SIZE 1024
|
---|
71 | char s[SBUF_SIZE];
|
---|
72 | size_t write_len;
|
---|
73 | char *sptr = s;
|
---|
74 | CURLcode result = CURLE_OK;
|
---|
75 | #ifdef HAVE_GSSAPI
|
---|
76 | unsigned char data_sec = conn->data_prot;
|
---|
77 | #endif
|
---|
78 |
|
---|
79 | DEBUGASSERT(cmd);
|
---|
80 |
|
---|
81 | write_len = strlen(cmd);
|
---|
82 | if(!write_len || write_len > (sizeof(s) -3))
|
---|
83 | return CURLE_BAD_FUNCTION_ARGUMENT;
|
---|
84 |
|
---|
85 | memcpy(&s, cmd, write_len);
|
---|
86 | strcpy(&s[write_len], "\r\n"); /* append a trailing CRLF */
|
---|
87 | write_len += 2;
|
---|
88 | bytes_written = 0;
|
---|
89 |
|
---|
90 | for(;;) {
|
---|
91 | #ifdef HAVE_GSSAPI
|
---|
92 | conn->data_prot = PROT_CMD;
|
---|
93 | #endif
|
---|
94 | result = Curl_xfer_send(data, sptr, write_len, &bytes_written);
|
---|
95 | #ifdef HAVE_GSSAPI
|
---|
96 | DEBUGASSERT(data_sec > PROT_NONE && data_sec < PROT_LAST);
|
---|
97 | conn->data_prot = data_sec;
|
---|
98 | #endif
|
---|
99 |
|
---|
100 | if(result)
|
---|
101 | break;
|
---|
102 |
|
---|
103 | Curl_debug(data, CURLINFO_HEADER_OUT, sptr, bytes_written);
|
---|
104 |
|
---|
105 | if(bytes_written != write_len) {
|
---|
106 | write_len -= bytes_written;
|
---|
107 | sptr += bytes_written;
|
---|
108 | }
|
---|
109 | else
|
---|
110 | break;
|
---|
111 | }
|
---|
112 |
|
---|
113 | return result;
|
---|
114 | }
|
---|
115 |
|
---|
116 | static int
|
---|
117 | krb5_init(void *app_data)
|
---|
118 | {
|
---|
119 | gss_ctx_id_t *context = app_data;
|
---|
120 | /* Make sure our context is initialized for krb5_end. */
|
---|
121 | *context = GSS_C_NO_CONTEXT;
|
---|
122 | return 0;
|
---|
123 | }
|
---|
124 |
|
---|
125 | static int
|
---|
126 | krb5_check_prot(void *app_data, int level)
|
---|
127 | {
|
---|
128 | (void)app_data; /* unused */
|
---|
129 | if(level == PROT_CONFIDENTIAL)
|
---|
130 | return -1;
|
---|
131 | return 0;
|
---|
132 | }
|
---|
133 |
|
---|
134 | static int
|
---|
135 | krb5_decode(void *app_data, void *buf, int len,
|
---|
136 | int level UNUSED_PARAM,
|
---|
137 | struct connectdata *conn UNUSED_PARAM)
|
---|
138 | {
|
---|
139 | gss_ctx_id_t *context = app_data;
|
---|
140 | OM_uint32 maj, min;
|
---|
141 | gss_buffer_desc enc, dec;
|
---|
142 |
|
---|
143 | (void)level;
|
---|
144 | (void)conn;
|
---|
145 |
|
---|
146 | enc.value = buf;
|
---|
147 | enc.length = len;
|
---|
148 | maj = gss_unwrap(&min, *context, &enc, &dec, NULL, NULL);
|
---|
149 | if(maj != GSS_S_COMPLETE)
|
---|
150 | return -1;
|
---|
151 |
|
---|
152 | memcpy(buf, dec.value, dec.length);
|
---|
153 | len = curlx_uztosi(dec.length);
|
---|
154 | gss_release_buffer(&min, &dec);
|
---|
155 |
|
---|
156 | return len;
|
---|
157 | }
|
---|
158 |
|
---|
159 | static int
|
---|
160 | krb5_encode(void *app_data, const void *from, int length, int level, void **to)
|
---|
161 | {
|
---|
162 | gss_ctx_id_t *context = app_data;
|
---|
163 | gss_buffer_desc dec, enc;
|
---|
164 | OM_uint32 maj, min;
|
---|
165 | int state;
|
---|
166 | int len;
|
---|
167 |
|
---|
168 | /* NOTE that the cast is safe, neither of the krb5, gnu gss and heimdal
|
---|
169 | * libraries modify the input buffer in gss_wrap()
|
---|
170 | */
|
---|
171 | dec.value = (void *)from;
|
---|
172 | dec.length = length;
|
---|
173 | maj = gss_wrap(&min, *context,
|
---|
174 | level == PROT_PRIVATE,
|
---|
175 | GSS_C_QOP_DEFAULT,
|
---|
176 | &dec, &state, &enc);
|
---|
177 |
|
---|
178 | if(maj != GSS_S_COMPLETE)
|
---|
179 | return -1;
|
---|
180 |
|
---|
181 | /* malloc a new buffer, in case gss_release_buffer doesn't work as
|
---|
182 | expected */
|
---|
183 | *to = malloc(enc.length);
|
---|
184 | if(!*to)
|
---|
185 | return -1;
|
---|
186 | memcpy(*to, enc.value, enc.length);
|
---|
187 | len = curlx_uztosi(enc.length);
|
---|
188 | gss_release_buffer(&min, &enc);
|
---|
189 | return len;
|
---|
190 | }
|
---|
191 |
|
---|
192 | static int
|
---|
193 | krb5_auth(void *app_data, struct Curl_easy *data, struct connectdata *conn)
|
---|
194 | {
|
---|
195 | int ret = AUTH_OK;
|
---|
196 | char *p;
|
---|
197 | const char *host = conn->host.name;
|
---|
198 | ssize_t nread;
|
---|
199 | curl_socklen_t l = sizeof(conn->local_addr);
|
---|
200 | CURLcode result;
|
---|
201 | const char *service = data->set.str[STRING_SERVICE_NAME] ?
|
---|
202 | data->set.str[STRING_SERVICE_NAME] :
|
---|
203 | "ftp";
|
---|
204 | const char *srv_host = "host";
|
---|
205 | gss_buffer_desc input_buffer, output_buffer, _gssresp, *gssresp;
|
---|
206 | OM_uint32 maj, min;
|
---|
207 | gss_name_t gssname;
|
---|
208 | gss_ctx_id_t *context = app_data;
|
---|
209 | struct gss_channel_bindings_struct chan;
|
---|
210 | size_t base64_sz = 0;
|
---|
211 | struct sockaddr_in *remote_addr =
|
---|
212 | (struct sockaddr_in *)(void *)&conn->remote_addr->sa_addr;
|
---|
213 | char *stringp;
|
---|
214 |
|
---|
215 | if(getsockname(conn->sock[FIRSTSOCKET],
|
---|
216 | (struct sockaddr *)&conn->local_addr, &l) < 0)
|
---|
217 | perror("getsockname()");
|
---|
218 |
|
---|
219 | chan.initiator_addrtype = GSS_C_AF_INET;
|
---|
220 | chan.initiator_address.length = l - 4;
|
---|
221 | chan.initiator_address.value = &conn->local_addr.sin_addr.s_addr;
|
---|
222 | chan.acceptor_addrtype = GSS_C_AF_INET;
|
---|
223 | chan.acceptor_address.length = l - 4;
|
---|
224 | chan.acceptor_address.value = &remote_addr->sin_addr.s_addr;
|
---|
225 | chan.application_data.length = 0;
|
---|
226 | chan.application_data.value = NULL;
|
---|
227 |
|
---|
228 | /* this loop will execute twice (once for service, once for host) */
|
---|
229 | for(;;) {
|
---|
230 | /* this really shouldn't be repeated here, but can't help it */
|
---|
231 | if(service == srv_host) {
|
---|
232 | result = ftpsend(data, conn, "AUTH GSSAPI");
|
---|
233 | if(result)
|
---|
234 | return -2;
|
---|
235 |
|
---|
236 | if(Curl_GetFTPResponse(data, &nread, NULL))
|
---|
237 | return -1;
|
---|
238 | else {
|
---|
239 | struct pingpong *pp = &conn->proto.ftpc.pp;
|
---|
240 | char *line = Curl_dyn_ptr(&pp->recvbuf);
|
---|
241 | if(line[0] != '3')
|
---|
242 | return -1;
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | stringp = aprintf("%s@%s", service, host);
|
---|
247 | if(!stringp)
|
---|
248 | return -2;
|
---|
249 |
|
---|
250 | input_buffer.value = stringp;
|
---|
251 | input_buffer.length = strlen(stringp);
|
---|
252 | maj = gss_import_name(&min, &input_buffer, GSS_C_NT_HOSTBASED_SERVICE,
|
---|
253 | &gssname);
|
---|
254 | free(stringp);
|
---|
255 | if(maj != GSS_S_COMPLETE) {
|
---|
256 | gss_release_name(&min, &gssname);
|
---|
257 | if(service == srv_host) {
|
---|
258 | failf(data, "Error importing service name %s@%s", service, host);
|
---|
259 | return AUTH_ERROR;
|
---|
260 | }
|
---|
261 | service = srv_host;
|
---|
262 | continue;
|
---|
263 | }
|
---|
264 | /* We pass NULL as |output_name_type| to avoid a leak. */
|
---|
265 | gss_display_name(&min, gssname, &output_buffer, NULL);
|
---|
266 | infof(data, "Trying against %s", (char *)output_buffer.value);
|
---|
267 | gssresp = GSS_C_NO_BUFFER;
|
---|
268 | *context = GSS_C_NO_CONTEXT;
|
---|
269 |
|
---|
270 | do {
|
---|
271 | /* Release the buffer at each iteration to avoid leaking: the first time
|
---|
272 | we are releasing the memory from gss_display_name. The last item is
|
---|
273 | taken care by a final gss_release_buffer. */
|
---|
274 | gss_release_buffer(&min, &output_buffer);
|
---|
275 | ret = AUTH_OK;
|
---|
276 | maj = Curl_gss_init_sec_context(data,
|
---|
277 | &min,
|
---|
278 | context,
|
---|
279 | gssname,
|
---|
280 | &Curl_krb5_mech_oid,
|
---|
281 | &chan,
|
---|
282 | gssresp,
|
---|
283 | &output_buffer,
|
---|
284 | TRUE,
|
---|
285 | NULL);
|
---|
286 |
|
---|
287 | if(gssresp) {
|
---|
288 | free(_gssresp.value);
|
---|
289 | gssresp = NULL;
|
---|
290 | }
|
---|
291 |
|
---|
292 | if(GSS_ERROR(maj)) {
|
---|
293 | infof(data, "Error creating security context");
|
---|
294 | ret = AUTH_ERROR;
|
---|
295 | break;
|
---|
296 | }
|
---|
297 |
|
---|
298 | if(output_buffer.length) {
|
---|
299 | char *cmd;
|
---|
300 |
|
---|
301 | result = Curl_base64_encode((char *)output_buffer.value,
|
---|
302 | output_buffer.length, &p, &base64_sz);
|
---|
303 | if(result) {
|
---|
304 | infof(data, "base64-encoding: %s", curl_easy_strerror(result));
|
---|
305 | ret = AUTH_ERROR;
|
---|
306 | break;
|
---|
307 | }
|
---|
308 |
|
---|
309 | cmd = aprintf("ADAT %s", p);
|
---|
310 | if(cmd)
|
---|
311 | result = ftpsend(data, conn, cmd);
|
---|
312 | else
|
---|
313 | result = CURLE_OUT_OF_MEMORY;
|
---|
314 |
|
---|
315 | free(p);
|
---|
316 | free(cmd);
|
---|
317 |
|
---|
318 | if(result) {
|
---|
319 | ret = -2;
|
---|
320 | break;
|
---|
321 | }
|
---|
322 |
|
---|
323 | if(Curl_GetFTPResponse(data, &nread, NULL)) {
|
---|
324 | ret = -1;
|
---|
325 | break;
|
---|
326 | }
|
---|
327 | else {
|
---|
328 | struct pingpong *pp = &conn->proto.ftpc.pp;
|
---|
329 | size_t len = Curl_dyn_len(&pp->recvbuf);
|
---|
330 | p = Curl_dyn_ptr(&pp->recvbuf);
|
---|
331 | if((len < 4) || (p[0] != '2' && p[0] != '3')) {
|
---|
332 | infof(data, "Server didn't accept auth data");
|
---|
333 | ret = AUTH_ERROR;
|
---|
334 | break;
|
---|
335 | }
|
---|
336 | }
|
---|
337 |
|
---|
338 | _gssresp.value = NULL; /* make sure it is initialized */
|
---|
339 | p += 4; /* over '789 ' */
|
---|
340 | p = strstr(p, "ADAT=");
|
---|
341 | if(p) {
|
---|
342 | result = Curl_base64_decode(p + 5,
|
---|
343 | (unsigned char **)&_gssresp.value,
|
---|
344 | &_gssresp.length);
|
---|
345 | if(result) {
|
---|
346 | failf(data, "base64-decoding: %s", curl_easy_strerror(result));
|
---|
347 | ret = AUTH_CONTINUE;
|
---|
348 | break;
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | gssresp = &_gssresp;
|
---|
353 | }
|
---|
354 | } while(maj == GSS_S_CONTINUE_NEEDED);
|
---|
355 |
|
---|
356 | gss_release_name(&min, &gssname);
|
---|
357 | gss_release_buffer(&min, &output_buffer);
|
---|
358 |
|
---|
359 | if(gssresp)
|
---|
360 | free(_gssresp.value);
|
---|
361 |
|
---|
362 | if(ret == AUTH_OK || service == srv_host)
|
---|
363 | return ret;
|
---|
364 |
|
---|
365 | service = srv_host;
|
---|
366 | }
|
---|
367 | return ret;
|
---|
368 | }
|
---|
369 |
|
---|
370 | static void krb5_end(void *app_data)
|
---|
371 | {
|
---|
372 | OM_uint32 min;
|
---|
373 | gss_ctx_id_t *context = app_data;
|
---|
374 | if(*context != GSS_C_NO_CONTEXT) {
|
---|
375 | OM_uint32 maj = gss_delete_sec_context(&min, context, GSS_C_NO_BUFFER);
|
---|
376 | (void)maj;
|
---|
377 | DEBUGASSERT(maj == GSS_S_COMPLETE);
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | static const struct Curl_sec_client_mech Curl_krb5_client_mech = {
|
---|
382 | "GSSAPI",
|
---|
383 | sizeof(gss_ctx_id_t),
|
---|
384 | krb5_init,
|
---|
385 | krb5_auth,
|
---|
386 | krb5_end,
|
---|
387 | krb5_check_prot,
|
---|
388 |
|
---|
389 | krb5_encode,
|
---|
390 | krb5_decode
|
---|
391 | };
|
---|
392 |
|
---|
393 | static const struct {
|
---|
394 | unsigned char level;
|
---|
395 | const char *name;
|
---|
396 | } level_names[] = {
|
---|
397 | { PROT_CLEAR, "clear" },
|
---|
398 | { PROT_SAFE, "safe" },
|
---|
399 | { PROT_CONFIDENTIAL, "confidential" },
|
---|
400 | { PROT_PRIVATE, "private" }
|
---|
401 | };
|
---|
402 |
|
---|
403 | static unsigned char name_to_level(const char *name)
|
---|
404 | {
|
---|
405 | int i;
|
---|
406 | for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++)
|
---|
407 | if(curl_strequal(name, level_names[i].name))
|
---|
408 | return level_names[i].level;
|
---|
409 | return PROT_NONE;
|
---|
410 | }
|
---|
411 |
|
---|
412 | /* Convert a protocol |level| to its char representation.
|
---|
413 | We take an int to catch programming mistakes. */
|
---|
414 | static char level_to_char(int level)
|
---|
415 | {
|
---|
416 | switch(level) {
|
---|
417 | case PROT_CLEAR:
|
---|
418 | return 'C';
|
---|
419 | case PROT_SAFE:
|
---|
420 | return 'S';
|
---|
421 | case PROT_CONFIDENTIAL:
|
---|
422 | return 'E';
|
---|
423 | case PROT_PRIVATE:
|
---|
424 | return 'P';
|
---|
425 | case PROT_CMD:
|
---|
426 | default:
|
---|
427 | /* Those 2 cases should not be reached! */
|
---|
428 | break;
|
---|
429 | }
|
---|
430 | DEBUGASSERT(0);
|
---|
431 | /* Default to the most secure alternative. */
|
---|
432 | return 'P';
|
---|
433 | }
|
---|
434 |
|
---|
435 | /* Send an FTP command defined by |message| and the optional arguments. The
|
---|
436 | function returns the ftp_code. If an error occurs, -1 is returned. */
|
---|
437 | static int ftp_send_command(struct Curl_easy *data, const char *message, ...)
|
---|
438 | CURL_PRINTF(2, 3);
|
---|
439 |
|
---|
440 | static int ftp_send_command(struct Curl_easy *data, const char *message, ...)
|
---|
441 | {
|
---|
442 | int ftp_code;
|
---|
443 | ssize_t nread = 0;
|
---|
444 | va_list args;
|
---|
445 | char print_buffer[50];
|
---|
446 |
|
---|
447 | va_start(args, message);
|
---|
448 | mvsnprintf(print_buffer, sizeof(print_buffer), message, args);
|
---|
449 | va_end(args);
|
---|
450 |
|
---|
451 | if(ftpsend(data, data->conn, print_buffer)) {
|
---|
452 | ftp_code = -1;
|
---|
453 | }
|
---|
454 | else {
|
---|
455 | if(Curl_GetFTPResponse(data, &nread, &ftp_code))
|
---|
456 | ftp_code = -1;
|
---|
457 | }
|
---|
458 |
|
---|
459 | (void)nread; /* Unused */
|
---|
460 | return ftp_code;
|
---|
461 | }
|
---|
462 |
|
---|
463 | /* Read |len| from the socket |fd| and store it in |to|. Return a CURLcode
|
---|
464 | saying whether an error occurred or CURLE_OK if |len| was read. */
|
---|
465 | static CURLcode
|
---|
466 | socket_read(struct Curl_easy *data, int sockindex, void *to, size_t len)
|
---|
467 | {
|
---|
468 | char *to_p = to;
|
---|
469 | CURLcode result;
|
---|
470 | ssize_t nread = 0;
|
---|
471 |
|
---|
472 | while(len > 0) {
|
---|
473 | result = Curl_conn_recv(data, sockindex, to_p, len, &nread);
|
---|
474 | if(nread > 0) {
|
---|
475 | len -= nread;
|
---|
476 | to_p += nread;
|
---|
477 | }
|
---|
478 | else {
|
---|
479 | if(result == CURLE_AGAIN)
|
---|
480 | continue;
|
---|
481 | return result;
|
---|
482 | }
|
---|
483 | }
|
---|
484 | return CURLE_OK;
|
---|
485 | }
|
---|
486 |
|
---|
487 |
|
---|
488 | /* Write |len| bytes from the buffer |to| to the socket |fd|. Return a
|
---|
489 | CURLcode saying whether an error occurred or CURLE_OK if |len| was
|
---|
490 | written. */
|
---|
491 | static CURLcode
|
---|
492 | socket_write(struct Curl_easy *data, int sockindex, const void *to,
|
---|
493 | size_t len)
|
---|
494 | {
|
---|
495 | const char *to_p = to;
|
---|
496 | CURLcode result;
|
---|
497 | size_t written;
|
---|
498 |
|
---|
499 | while(len > 0) {
|
---|
500 | result = Curl_conn_send(data, sockindex, to_p, len, &written);
|
---|
501 | if(!result && written > 0) {
|
---|
502 | len -= written;
|
---|
503 | to_p += written;
|
---|
504 | }
|
---|
505 | else {
|
---|
506 | if(result == CURLE_AGAIN)
|
---|
507 | continue;
|
---|
508 | return result;
|
---|
509 | }
|
---|
510 | }
|
---|
511 | return CURLE_OK;
|
---|
512 | }
|
---|
513 |
|
---|
514 | static CURLcode read_data(struct Curl_easy *data, int sockindex,
|
---|
515 | struct krb5buffer *buf)
|
---|
516 | {
|
---|
517 | struct connectdata *conn = data->conn;
|
---|
518 | int len;
|
---|
519 | CURLcode result;
|
---|
520 | int nread;
|
---|
521 |
|
---|
522 | result = socket_read(data, sockindex, &len, sizeof(len));
|
---|
523 | if(result)
|
---|
524 | return result;
|
---|
525 |
|
---|
526 | if(len) {
|
---|
527 | /* only realloc if there was a length */
|
---|
528 | len = ntohl(len);
|
---|
529 | if(len > CURL_MAX_INPUT_LENGTH)
|
---|
530 | len = 0;
|
---|
531 | else
|
---|
532 | buf->data = Curl_saferealloc(buf->data, len);
|
---|
533 | }
|
---|
534 | if(!len || !buf->data)
|
---|
535 | return CURLE_OUT_OF_MEMORY;
|
---|
536 |
|
---|
537 | result = socket_read(data, sockindex, buf->data, len);
|
---|
538 | if(result)
|
---|
539 | return result;
|
---|
540 | nread = conn->mech->decode(conn->app_data, buf->data, len,
|
---|
541 | conn->data_prot, conn);
|
---|
542 | if(nread < 0)
|
---|
543 | return CURLE_RECV_ERROR;
|
---|
544 | buf->size = (size_t)nread;
|
---|
545 | buf->index = 0;
|
---|
546 | return CURLE_OK;
|
---|
547 | }
|
---|
548 |
|
---|
549 | static size_t
|
---|
550 | buffer_read(struct krb5buffer *buf, void *data, size_t len)
|
---|
551 | {
|
---|
552 | if(buf->size - buf->index < len)
|
---|
553 | len = buf->size - buf->index;
|
---|
554 | memcpy(data, (char *)buf->data + buf->index, len);
|
---|
555 | buf->index += len;
|
---|
556 | return len;
|
---|
557 | }
|
---|
558 |
|
---|
559 | /* Matches Curl_recv signature */
|
---|
560 | static ssize_t sec_recv(struct Curl_easy *data, int sockindex,
|
---|
561 | char *buffer, size_t len, CURLcode *err)
|
---|
562 | {
|
---|
563 | size_t bytes_read;
|
---|
564 | size_t total_read = 0;
|
---|
565 | struct connectdata *conn = data->conn;
|
---|
566 |
|
---|
567 | *err = CURLE_OK;
|
---|
568 |
|
---|
569 | /* Handle clear text response. */
|
---|
570 | if(conn->sec_complete == 0 || conn->data_prot == PROT_CLEAR) {
|
---|
571 | ssize_t nread;
|
---|
572 | *err = Curl_conn_recv(data, sockindex, buffer, len, &nread);
|
---|
573 | return nread;
|
---|
574 | }
|
---|
575 |
|
---|
576 | if(conn->in_buffer.eof_flag) {
|
---|
577 | conn->in_buffer.eof_flag = 0;
|
---|
578 | return 0;
|
---|
579 | }
|
---|
580 |
|
---|
581 | bytes_read = buffer_read(&conn->in_buffer, buffer, len);
|
---|
582 | len -= bytes_read;
|
---|
583 | total_read += bytes_read;
|
---|
584 | buffer += bytes_read;
|
---|
585 |
|
---|
586 | while(len > 0) {
|
---|
587 | if(read_data(data, sockindex, &conn->in_buffer))
|
---|
588 | return -1;
|
---|
589 | if(conn->in_buffer.size == 0) {
|
---|
590 | if(bytes_read > 0)
|
---|
591 | conn->in_buffer.eof_flag = 1;
|
---|
592 | return bytes_read;
|
---|
593 | }
|
---|
594 | bytes_read = buffer_read(&conn->in_buffer, buffer, len);
|
---|
595 | len -= bytes_read;
|
---|
596 | total_read += bytes_read;
|
---|
597 | buffer += bytes_read;
|
---|
598 | }
|
---|
599 | return total_read;
|
---|
600 | }
|
---|
601 |
|
---|
602 | /* Send |length| bytes from |from| to the |fd| socket taking care of encoding
|
---|
603 | and negotiating with the server. |from| can be NULL. */
|
---|
604 | static void do_sec_send(struct Curl_easy *data, struct connectdata *conn,
|
---|
605 | curl_socket_t fd, const char *from, int length)
|
---|
606 | {
|
---|
607 | int bytes, htonl_bytes; /* 32-bit integers for htonl */
|
---|
608 | char *buffer = NULL;
|
---|
609 | char *cmd_buffer;
|
---|
610 | size_t cmd_size = 0;
|
---|
611 | CURLcode error;
|
---|
612 | enum protection_level prot_level = conn->data_prot;
|
---|
613 | bool iscmd = (prot_level == PROT_CMD)?TRUE:FALSE;
|
---|
614 |
|
---|
615 | DEBUGASSERT(prot_level > PROT_NONE && prot_level < PROT_LAST);
|
---|
616 |
|
---|
617 | if(iscmd) {
|
---|
618 | if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
|
---|
619 | prot_level = PROT_PRIVATE;
|
---|
620 | else
|
---|
621 | prot_level = conn->command_prot;
|
---|
622 | }
|
---|
623 | bytes = conn->mech->encode(conn->app_data, from, length, prot_level,
|
---|
624 | (void **)&buffer);
|
---|
625 | if(!buffer || bytes <= 0)
|
---|
626 | return; /* error */
|
---|
627 |
|
---|
628 | if(iscmd) {
|
---|
629 | error = Curl_base64_encode(buffer, curlx_sitouz(bytes),
|
---|
630 | &cmd_buffer, &cmd_size);
|
---|
631 | if(error) {
|
---|
632 | free(buffer);
|
---|
633 | return; /* error */
|
---|
634 | }
|
---|
635 | if(cmd_size > 0) {
|
---|
636 | static const char *enc = "ENC ";
|
---|
637 | static const char *mic = "MIC ";
|
---|
638 | if(prot_level == PROT_PRIVATE)
|
---|
639 | socket_write(data, fd, enc, 4);
|
---|
640 | else
|
---|
641 | socket_write(data, fd, mic, 4);
|
---|
642 |
|
---|
643 | socket_write(data, fd, cmd_buffer, cmd_size);
|
---|
644 | socket_write(data, fd, "\r\n", 2);
|
---|
645 | infof(data, "Send: %s%s", prot_level == PROT_PRIVATE?enc:mic,
|
---|
646 | cmd_buffer);
|
---|
647 | free(cmd_buffer);
|
---|
648 | }
|
---|
649 | }
|
---|
650 | else {
|
---|
651 | htonl_bytes = htonl(bytes);
|
---|
652 | socket_write(data, fd, &htonl_bytes, sizeof(htonl_bytes));
|
---|
653 | socket_write(data, fd, buffer, curlx_sitouz(bytes));
|
---|
654 | }
|
---|
655 | free(buffer);
|
---|
656 | }
|
---|
657 |
|
---|
658 | static ssize_t sec_write(struct Curl_easy *data, struct connectdata *conn,
|
---|
659 | curl_socket_t fd, const char *buffer, size_t length)
|
---|
660 | {
|
---|
661 | ssize_t tx = 0, len = conn->buffer_size;
|
---|
662 |
|
---|
663 | if(len <= 0)
|
---|
664 | len = length;
|
---|
665 | while(length) {
|
---|
666 | if(length < (size_t)len)
|
---|
667 | len = length;
|
---|
668 |
|
---|
669 | do_sec_send(data, conn, fd, buffer, curlx_sztosi(len));
|
---|
670 | length -= len;
|
---|
671 | buffer += len;
|
---|
672 | tx += len;
|
---|
673 | }
|
---|
674 | return tx;
|
---|
675 | }
|
---|
676 |
|
---|
677 | /* Matches Curl_send signature */
|
---|
678 | static ssize_t sec_send(struct Curl_easy *data, int sockindex,
|
---|
679 | const void *buffer, size_t len, CURLcode *err)
|
---|
680 | {
|
---|
681 | struct connectdata *conn = data->conn;
|
---|
682 | curl_socket_t fd = conn->sock[sockindex];
|
---|
683 | *err = CURLE_OK;
|
---|
684 | return sec_write(data, conn, fd, buffer, len);
|
---|
685 | }
|
---|
686 |
|
---|
687 | int Curl_sec_read_msg(struct Curl_easy *data, struct connectdata *conn,
|
---|
688 | char *buffer, enum protection_level level)
|
---|
689 | {
|
---|
690 | /* decoded_len should be size_t or ssize_t but conn->mech->decode returns an
|
---|
691 | int */
|
---|
692 | int decoded_len;
|
---|
693 | char *buf;
|
---|
694 | int ret_code = 0;
|
---|
695 | size_t decoded_sz = 0;
|
---|
696 | CURLcode error;
|
---|
697 |
|
---|
698 | (void) data;
|
---|
699 |
|
---|
700 | if(!conn->mech)
|
---|
701 | /* not initialized, return error */
|
---|
702 | return -1;
|
---|
703 |
|
---|
704 | DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
|
---|
705 |
|
---|
706 | error = Curl_base64_decode(buffer + 4, (unsigned char **)&buf, &decoded_sz);
|
---|
707 | if(error || decoded_sz == 0)
|
---|
708 | return -1;
|
---|
709 |
|
---|
710 | if(decoded_sz > (size_t)INT_MAX) {
|
---|
711 | free(buf);
|
---|
712 | return -1;
|
---|
713 | }
|
---|
714 | decoded_len = curlx_uztosi(decoded_sz);
|
---|
715 |
|
---|
716 | decoded_len = conn->mech->decode(conn->app_data, buf, decoded_len,
|
---|
717 | level, conn);
|
---|
718 | if(decoded_len <= 0) {
|
---|
719 | free(buf);
|
---|
720 | return -1;
|
---|
721 | }
|
---|
722 |
|
---|
723 | {
|
---|
724 | buf[decoded_len] = '\n';
|
---|
725 | Curl_debug(data, CURLINFO_HEADER_IN, buf, decoded_len + 1);
|
---|
726 | }
|
---|
727 |
|
---|
728 | buf[decoded_len] = '\0';
|
---|
729 | if(decoded_len <= 3)
|
---|
730 | /* suspiciously short */
|
---|
731 | return 0;
|
---|
732 |
|
---|
733 | if(buf[3] != '-')
|
---|
734 | ret_code = atoi(buf);
|
---|
735 |
|
---|
736 | if(buf[decoded_len - 1] == '\n')
|
---|
737 | buf[decoded_len - 1] = '\0';
|
---|
738 | strcpy(buffer, buf);
|
---|
739 | free(buf);
|
---|
740 | return ret_code;
|
---|
741 | }
|
---|
742 |
|
---|
743 | static int sec_set_protection_level(struct Curl_easy *data)
|
---|
744 | {
|
---|
745 | int code;
|
---|
746 | struct connectdata *conn = data->conn;
|
---|
747 | unsigned char level = conn->request_data_prot;
|
---|
748 |
|
---|
749 | DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
|
---|
750 |
|
---|
751 | if(!conn->sec_complete) {
|
---|
752 | infof(data, "Trying to change the protection level after the"
|
---|
753 | " completion of the data exchange.");
|
---|
754 | return -1;
|
---|
755 | }
|
---|
756 |
|
---|
757 | /* Bail out if we try to set up the same level */
|
---|
758 | if(conn->data_prot == level)
|
---|
759 | return 0;
|
---|
760 |
|
---|
761 | if(level) {
|
---|
762 | char *pbsz;
|
---|
763 | unsigned int buffer_size = 1 << 20; /* 1048576 */
|
---|
764 | struct pingpong *pp = &conn->proto.ftpc.pp;
|
---|
765 | char *line;
|
---|
766 |
|
---|
767 | code = ftp_send_command(data, "PBSZ %u", buffer_size);
|
---|
768 | if(code < 0)
|
---|
769 | return -1;
|
---|
770 |
|
---|
771 | if(code/100 != 2) {
|
---|
772 | failf(data, "Failed to set the protection's buffer size.");
|
---|
773 | return -1;
|
---|
774 | }
|
---|
775 | conn->buffer_size = buffer_size;
|
---|
776 |
|
---|
777 | line = Curl_dyn_ptr(&pp->recvbuf);
|
---|
778 | pbsz = strstr(line, "PBSZ=");
|
---|
779 | if(pbsz) {
|
---|
780 | /* stick to default value if the check fails */
|
---|
781 | if(ISDIGIT(pbsz[5]))
|
---|
782 | buffer_size = atoi(&pbsz[5]);
|
---|
783 | if(buffer_size < conn->buffer_size)
|
---|
784 | conn->buffer_size = buffer_size;
|
---|
785 | }
|
---|
786 | }
|
---|
787 |
|
---|
788 | /* Now try to negotiate the protection level. */
|
---|
789 | code = ftp_send_command(data, "PROT %c", level_to_char(level));
|
---|
790 |
|
---|
791 | if(code < 0)
|
---|
792 | return -1;
|
---|
793 |
|
---|
794 | if(code/100 != 2) {
|
---|
795 | failf(data, "Failed to set the protection level.");
|
---|
796 | return -1;
|
---|
797 | }
|
---|
798 |
|
---|
799 | conn->data_prot = level;
|
---|
800 | if(level == PROT_PRIVATE)
|
---|
801 | conn->command_prot = level;
|
---|
802 |
|
---|
803 | return 0;
|
---|
804 | }
|
---|
805 |
|
---|
806 | int
|
---|
807 | Curl_sec_request_prot(struct connectdata *conn, const char *level)
|
---|
808 | {
|
---|
809 | unsigned char l = name_to_level(level);
|
---|
810 | if(l == PROT_NONE)
|
---|
811 | return -1;
|
---|
812 | DEBUGASSERT(l > PROT_NONE && l < PROT_LAST);
|
---|
813 | conn->request_data_prot = l;
|
---|
814 | return 0;
|
---|
815 | }
|
---|
816 |
|
---|
817 | static CURLcode choose_mech(struct Curl_easy *data, struct connectdata *conn)
|
---|
818 | {
|
---|
819 | int ret;
|
---|
820 | void *tmp_allocation;
|
---|
821 | const struct Curl_sec_client_mech *mech = &Curl_krb5_client_mech;
|
---|
822 |
|
---|
823 | tmp_allocation = realloc(conn->app_data, mech->size);
|
---|
824 | if(!tmp_allocation) {
|
---|
825 | failf(data, "Failed realloc of size %zu", mech->size);
|
---|
826 | mech = NULL;
|
---|
827 | return CURLE_OUT_OF_MEMORY;
|
---|
828 | }
|
---|
829 | conn->app_data = tmp_allocation;
|
---|
830 |
|
---|
831 | if(mech->init) {
|
---|
832 | ret = mech->init(conn->app_data);
|
---|
833 | if(ret) {
|
---|
834 | infof(data, "Failed initialization for %s. Skipping it.",
|
---|
835 | mech->name);
|
---|
836 | return CURLE_FAILED_INIT;
|
---|
837 | }
|
---|
838 | }
|
---|
839 |
|
---|
840 | infof(data, "Trying mechanism %s...", mech->name);
|
---|
841 | ret = ftp_send_command(data, "AUTH %s", mech->name);
|
---|
842 | if(ret < 0)
|
---|
843 | return CURLE_COULDNT_CONNECT;
|
---|
844 |
|
---|
845 | if(ret/100 != 3) {
|
---|
846 | switch(ret) {
|
---|
847 | case 504:
|
---|
848 | infof(data, "Mechanism %s is not supported by the server (server "
|
---|
849 | "returned ftp code: 504).", mech->name);
|
---|
850 | break;
|
---|
851 | case 534:
|
---|
852 | infof(data, "Mechanism %s was rejected by the server (server returned "
|
---|
853 | "ftp code: 534).", mech->name);
|
---|
854 | break;
|
---|
855 | default:
|
---|
856 | if(ret/100 == 5) {
|
---|
857 | infof(data, "server does not support the security extensions");
|
---|
858 | return CURLE_USE_SSL_FAILED;
|
---|
859 | }
|
---|
860 | break;
|
---|
861 | }
|
---|
862 | return CURLE_LOGIN_DENIED;
|
---|
863 | }
|
---|
864 |
|
---|
865 | /* Authenticate */
|
---|
866 | ret = mech->auth(conn->app_data, data, conn);
|
---|
867 |
|
---|
868 | if(ret != AUTH_CONTINUE) {
|
---|
869 | if(ret != AUTH_OK) {
|
---|
870 | /* Mechanism has dumped the error to stderr, don't error here. */
|
---|
871 | return CURLE_USE_SSL_FAILED;
|
---|
872 | }
|
---|
873 | DEBUGASSERT(ret == AUTH_OK);
|
---|
874 |
|
---|
875 | conn->mech = mech;
|
---|
876 | conn->sec_complete = 1;
|
---|
877 | conn->recv[FIRSTSOCKET] = sec_recv;
|
---|
878 | conn->send[FIRSTSOCKET] = sec_send;
|
---|
879 | conn->recv[SECONDARYSOCKET] = sec_recv;
|
---|
880 | conn->send[SECONDARYSOCKET] = sec_send;
|
---|
881 | conn->command_prot = PROT_SAFE;
|
---|
882 | /* Set the requested protection level */
|
---|
883 | /* BLOCKING */
|
---|
884 | (void)sec_set_protection_level(data);
|
---|
885 | }
|
---|
886 |
|
---|
887 | return CURLE_OK;
|
---|
888 | }
|
---|
889 |
|
---|
890 | CURLcode
|
---|
891 | Curl_sec_login(struct Curl_easy *data, struct connectdata *conn)
|
---|
892 | {
|
---|
893 | return choose_mech(data, conn);
|
---|
894 | }
|
---|
895 |
|
---|
896 |
|
---|
897 | void
|
---|
898 | Curl_sec_end(struct connectdata *conn)
|
---|
899 | {
|
---|
900 | if(conn->mech && conn->mech->end)
|
---|
901 | conn->mech->end(conn->app_data);
|
---|
902 | free(conn->app_data);
|
---|
903 | conn->app_data = NULL;
|
---|
904 | if(conn->in_buffer.data) {
|
---|
905 | free(conn->in_buffer.data);
|
---|
906 | conn->in_buffer.data = NULL;
|
---|
907 | conn->in_buffer.size = 0;
|
---|
908 | conn->in_buffer.index = 0;
|
---|
909 | conn->in_buffer.eof_flag = 0;
|
---|
910 | }
|
---|
911 | conn->sec_complete = 0;
|
---|
912 | conn->data_prot = PROT_CLEAR;
|
---|
913 | conn->mech = NULL;
|
---|
914 | }
|
---|
915 |
|
---|
916 | #endif /* HAVE_GSSAPI && !CURL_DISABLE_FTP */
|
---|