Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 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 | * |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 10 | * Copyright (c) 1998, 1999, 2017 Kungliga Tekniska Högskolan |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 11 | * (Royal Institute of Technology, Stockholm, Sweden). |
| 12 | * |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 13 | * Copyright (C) 2001 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 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 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 44 | #include "curl_setup.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 45 | |
| 46 | #ifndef CURL_DISABLE_FTP |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 47 | #ifdef HAVE_GSSAPI |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 48 | |
| 49 | #ifdef HAVE_NETDB_H |
| 50 | #include <netdb.h> |
| 51 | #endif |
| 52 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 53 | #include <limits.h> |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 54 | |
| 55 | #include "urldata.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 56 | #include "curl_base64.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 57 | #include "curl_memory.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 58 | #include "curl_sec.h" |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 59 | #include "ftp.h" |
| 60 | #include "sendf.h" |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 61 | #include "strcase.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 62 | #include "warnless.h" |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 63 | #include "strdup.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 64 | /* The last #include file should be: */ |
| 65 | #include "memdebug.h" |
| 66 | |
| 67 | static const struct { |
| 68 | enum protection_level level; |
| 69 | const char *name; |
| 70 | } level_names[] = { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 71 | { PROT_CLEAR, "clear" }, |
| 72 | { PROT_SAFE, "safe" }, |
| 73 | { PROT_CONFIDENTIAL, "confidential" }, |
| 74 | { PROT_PRIVATE, "private" } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | static enum protection_level |
| 78 | name_to_level(const char *name) |
| 79 | { |
| 80 | int i; |
| 81 | for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++) |
| 82 | if(checkprefix(name, level_names[i].name)) |
| 83 | return level_names[i].level; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 84 | return PROT_NONE; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 87 | /* Convert a protocol |level| to its char representation. |
| 88 | We take an int to catch programming mistakes. */ |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 89 | static char level_to_char(int level) |
| 90 | { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 91 | switch(level) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 92 | case PROT_CLEAR: |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 93 | return 'C'; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 94 | case PROT_SAFE: |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 95 | return 'S'; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 96 | case PROT_CONFIDENTIAL: |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 97 | return 'E'; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 98 | case PROT_PRIVATE: |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 99 | return 'P'; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 100 | case PROT_CMD: |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 101 | /* Fall through */ |
| 102 | default: |
| 103 | /* Those 2 cases should not be reached! */ |
| 104 | break; |
| 105 | } |
| 106 | DEBUGASSERT(0); |
| 107 | /* Default to the most secure alternative. */ |
| 108 | return 'P'; |
| 109 | } |
| 110 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 111 | /* Send an FTP command defined by |message| and the optional arguments. The |
| 112 | function returns the ftp_code. If an error occurs, -1 is returned. */ |
| 113 | static int ftp_send_command(struct connectdata *conn, const char *message, ...) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 114 | { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 115 | int ftp_code; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 116 | ssize_t nread = 0; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 117 | va_list args; |
| 118 | char print_buffer[50]; |
| 119 | |
| 120 | va_start(args, message); |
| 121 | vsnprintf(print_buffer, sizeof(print_buffer), message, args); |
| 122 | va_end(args); |
| 123 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 124 | if(Curl_ftpsend(conn, print_buffer)) { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 125 | ftp_code = -1; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 126 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 127 | else { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 128 | if(Curl_GetFTPResponse(&nread, conn, &ftp_code)) |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 129 | ftp_code = -1; |
| 130 | } |
| 131 | |
| 132 | (void)nread; /* Unused */ |
| 133 | return ftp_code; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 136 | /* Read |len| from the socket |fd| and store it in |to|. Return a CURLcode |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 137 | saying whether an error occurred or CURLE_OK if |len| was read. */ |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 138 | static CURLcode |
| 139 | socket_read(curl_socket_t fd, void *to, size_t len) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 140 | { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 141 | char *to_p = to; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 142 | CURLcode result; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 143 | ssize_t nread; |
| 144 | |
| 145 | while(len > 0) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 146 | result = Curl_read_plain(fd, to_p, len, &nread); |
| 147 | if(!result) { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 148 | len -= nread; |
| 149 | to_p += nread; |
| 150 | } |
| 151 | else { |
| 152 | /* FIXME: We are doing a busy wait */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 153 | if(result == CURLE_AGAIN) |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 154 | continue; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 155 | return result; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 156 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 157 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 158 | return CURLE_OK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 161 | |
| 162 | /* Write |len| bytes from the buffer |to| to the socket |fd|. Return a |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 163 | CURLcode saying whether an error occurred or CURLE_OK if |len| was |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 164 | written. */ |
| 165 | static CURLcode |
| 166 | socket_write(struct connectdata *conn, curl_socket_t fd, const void *to, |
| 167 | size_t len) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 168 | { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 169 | const char *to_p = to; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 170 | CURLcode result; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 171 | ssize_t written; |
| 172 | |
| 173 | while(len > 0) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 174 | result = Curl_write_plain(conn, fd, to_p, len, &written); |
| 175 | if(!result) { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 176 | len -= written; |
| 177 | to_p += written; |
| 178 | } |
| 179 | else { |
| 180 | /* FIXME: We are doing a busy wait */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 181 | if(result == CURLE_AGAIN) |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 182 | continue; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 183 | return result; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 184 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 185 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 186 | return CURLE_OK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 187 | } |
| 188 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 189 | static CURLcode read_data(struct connectdata *conn, |
| 190 | curl_socket_t fd, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 191 | struct krb5buffer *buf) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 192 | { |
| 193 | int len; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 194 | void *tmp = NULL; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 195 | CURLcode result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 196 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 197 | result = socket_read(fd, &len, sizeof(len)); |
| 198 | if(result) |
| 199 | return result; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 200 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 201 | if(len) { |
| 202 | /* only realloc if there was a length */ |
| 203 | len = ntohl(len); |
| 204 | tmp = Curl_saferealloc(buf->data, len); |
| 205 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 206 | if(tmp == NULL) |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 207 | return CURLE_OUT_OF_MEMORY; |
| 208 | |
| 209 | buf->data = tmp; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 210 | result = socket_read(fd, buf->data, len); |
| 211 | if(result) |
| 212 | return result; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 213 | buf->size = conn->mech->decode(conn->app_data, buf->data, len, |
| 214 | conn->data_prot, conn); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 215 | buf->index = 0; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 216 | return CURLE_OK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static size_t |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 220 | buffer_read(struct krb5buffer *buf, void *data, size_t len) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 221 | { |
| 222 | if(buf->size - buf->index < len) |
| 223 | len = buf->size - buf->index; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 224 | memcpy(data, (char *)buf->data + buf->index, len); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 225 | buf->index += len; |
| 226 | return len; |
| 227 | } |
| 228 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 229 | /* Matches Curl_recv signature */ |
| 230 | static ssize_t sec_recv(struct connectdata *conn, int sockindex, |
| 231 | char *buffer, size_t len, CURLcode *err) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 232 | { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 233 | size_t bytes_read; |
| 234 | size_t total_read = 0; |
| 235 | curl_socket_t fd = conn->sock[sockindex]; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 236 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 237 | *err = CURLE_OK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 238 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 239 | /* Handle clear text response. */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 240 | if(conn->sec_complete == 0 || conn->data_prot == PROT_CLEAR) |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 241 | return read(fd, buffer, len); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 242 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 243 | if(conn->in_buffer.eof_flag) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 244 | conn->in_buffer.eof_flag = 0; |
| 245 | return 0; |
| 246 | } |
| 247 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 248 | bytes_read = buffer_read(&conn->in_buffer, buffer, len); |
| 249 | len -= bytes_read; |
| 250 | total_read += bytes_read; |
| 251 | buffer += bytes_read; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 252 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 253 | while(len > 0) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 254 | if(read_data(conn, fd, &conn->in_buffer)) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 255 | return -1; |
| 256 | if(conn->in_buffer.size == 0) { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 257 | if(bytes_read > 0) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 258 | conn->in_buffer.eof_flag = 1; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 259 | return bytes_read; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 260 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 261 | bytes_read = buffer_read(&conn->in_buffer, buffer, len); |
| 262 | len -= bytes_read; |
| 263 | total_read += bytes_read; |
| 264 | buffer += bytes_read; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 265 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 266 | /* FIXME: Check for overflow */ |
| 267 | return total_read; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 270 | /* Send |length| bytes from |from| to the |fd| socket taking care of encoding |
| 271 | and negociating with the server. |from| can be NULL. */ |
| 272 | /* FIXME: We don't check for errors nor report any! */ |
| 273 | static void do_sec_send(struct connectdata *conn, curl_socket_t fd, |
| 274 | const char *from, int length) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 275 | { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 276 | int bytes, htonl_bytes; /* 32-bit integers for htonl */ |
| 277 | char *buffer = NULL; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 278 | char *cmd_buffer; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 279 | size_t cmd_size = 0; |
| 280 | CURLcode error; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 281 | enum protection_level prot_level = conn->data_prot; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 282 | bool iscmd = (prot_level == PROT_CMD)?TRUE:FALSE; |
| 283 | |
| 284 | DEBUGASSERT(prot_level > PROT_NONE && prot_level < PROT_LAST); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 285 | |
| 286 | if(iscmd) { |
| 287 | if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5)) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 288 | prot_level = PROT_PRIVATE; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 289 | else |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 290 | prot_level = conn->command_prot; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 291 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 292 | bytes = conn->mech->encode(conn->app_data, from, length, prot_level, |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 293 | (void **)&buffer); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 294 | if(!buffer || bytes <= 0) |
| 295 | return; /* error */ |
| 296 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 297 | if(iscmd) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 298 | error = Curl_base64_encode(conn->data, buffer, curlx_sitouz(bytes), |
| 299 | &cmd_buffer, &cmd_size); |
| 300 | if(error) { |
| 301 | free(buffer); |
| 302 | return; /* error */ |
| 303 | } |
| 304 | if(cmd_size > 0) { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 305 | static const char *enc = "ENC "; |
| 306 | static const char *mic = "MIC "; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 307 | if(prot_level == PROT_PRIVATE) |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 308 | socket_write(conn, fd, enc, 4); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 309 | else |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 310 | socket_write(conn, fd, mic, 4); |
| 311 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 312 | socket_write(conn, fd, cmd_buffer, cmd_size); |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 313 | socket_write(conn, fd, "\r\n", 2); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 314 | infof(conn->data, "Send: %s%s\n", prot_level == PROT_PRIVATE?enc:mic, |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 315 | cmd_buffer); |
| 316 | free(cmd_buffer); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | else { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 320 | htonl_bytes = htonl(bytes); |
| 321 | socket_write(conn, fd, &htonl_bytes, sizeof(htonl_bytes)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 322 | socket_write(conn, fd, buffer, curlx_sitouz(bytes)); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 323 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 324 | free(buffer); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 325 | } |
| 326 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 327 | static ssize_t sec_write(struct connectdata *conn, curl_socket_t fd, |
| 328 | const char *buffer, size_t length) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 329 | { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 330 | ssize_t tx = 0, len = conn->buffer_size; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 331 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 332 | len -= conn->mech->overhead(conn->app_data, conn->data_prot, |
| 333 | curlx_sztosi(len)); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 334 | if(len <= 0) |
| 335 | len = length; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 336 | while(length) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 337 | if(length < (size_t)len) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 338 | len = length; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 339 | |
| 340 | do_sec_send(conn, fd, buffer, curlx_sztosi(len)); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 341 | length -= len; |
| 342 | buffer += len; |
| 343 | tx += len; |
| 344 | } |
| 345 | return tx; |
| 346 | } |
| 347 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 348 | /* Matches Curl_send signature */ |
| 349 | static ssize_t sec_send(struct connectdata *conn, int sockindex, |
| 350 | const void *buffer, size_t len, CURLcode *err) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 351 | { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 352 | curl_socket_t fd = conn->sock[sockindex]; |
| 353 | *err = CURLE_OK; |
| 354 | return sec_write(conn, fd, buffer, len); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 355 | } |
| 356 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 357 | int Curl_sec_read_msg(struct connectdata *conn, char *buffer, |
| 358 | enum protection_level level) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 359 | { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 360 | /* decoded_len should be size_t or ssize_t but conn->mech->decode returns an |
| 361 | int */ |
| 362 | int decoded_len; |
| 363 | char *buf; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 364 | int ret_code = 0; |
| 365 | size_t decoded_sz = 0; |
| 366 | CURLcode error; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 367 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 368 | if(!conn->mech) |
| 369 | /* not inititalized, return error */ |
| 370 | return -1; |
| 371 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 372 | DEBUGASSERT(level > PROT_NONE && level < PROT_LAST); |
| 373 | |
| 374 | error = Curl_base64_decode(buffer + 4, (unsigned char **)&buf, &decoded_sz); |
| 375 | if(error || decoded_sz == 0) |
| 376 | return -1; |
| 377 | |
| 378 | if(decoded_sz > (size_t)INT_MAX) { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 379 | free(buf); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 380 | return -1; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 381 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 382 | decoded_len = curlx_uztosi(decoded_sz); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 383 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 384 | decoded_len = conn->mech->decode(conn->app_data, buf, decoded_len, |
| 385 | level, conn); |
| 386 | if(decoded_len <= 0) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 387 | free(buf); |
| 388 | return -1; |
| 389 | } |
| 390 | |
| 391 | if(conn->data->set.verbose) { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 392 | buf[decoded_len] = '\n'; |
| 393 | Curl_debug(conn->data, CURLINFO_HEADER_IN, buf, decoded_len + 1, conn); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 394 | } |
| 395 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 396 | buf[decoded_len] = '\0'; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 397 | if(decoded_len <= 3) |
| 398 | /* suspiciously short */ |
| 399 | return 0; |
| 400 | |
| 401 | if(buf[3] != '-') |
| 402 | /* safe to ignore return code */ |
| 403 | (void)sscanf(buf, "%d", &ret_code); |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 404 | |
| 405 | if(buf[decoded_len - 1] == '\n') |
| 406 | buf[decoded_len - 1] = '\0'; |
| 407 | /* FIXME: Is |buffer| length always greater than |decoded_len|? */ |
| 408 | strcpy(buffer, buf); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 409 | free(buf); |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 410 | return ret_code; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 413 | /* FIXME: The error code returned here is never checked. */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 414 | static int sec_set_protection_level(struct connectdata *conn) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 415 | { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 416 | int code; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 417 | char *pbsz; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 418 | static unsigned int buffer_size = 1 << 20; /* 1048576 */ |
| 419 | enum protection_level level = conn->request_data_prot; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 420 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 421 | DEBUGASSERT(level > PROT_NONE && level < PROT_LAST); |
| 422 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 423 | if(!conn->sec_complete) { |
| 424 | infof(conn->data, "Trying to change the protection level after the" |
| 425 | "completion of the data exchange.\n"); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 426 | return -1; |
| 427 | } |
| 428 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 429 | /* Bail out if we try to set up the same level */ |
| 430 | if(conn->data_prot == level) |
| 431 | return 0; |
| 432 | |
| 433 | if(level) { |
| 434 | code = ftp_send_command(conn, "PBSZ %u", buffer_size); |
| 435 | if(code < 0) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 436 | return -1; |
| 437 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 438 | if(code/100 != 2) { |
| 439 | failf(conn->data, "Failed to set the protection's buffer size."); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 440 | return -1; |
| 441 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 442 | conn->buffer_size = buffer_size; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 443 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 444 | pbsz = strstr(conn->data->state.buffer, "PBSZ="); |
| 445 | if(pbsz) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 446 | /* ignore return code, use default value if it fails */ |
| 447 | (void)sscanf(pbsz, "PBSZ=%u", &buffer_size); |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 448 | if(buffer_size < conn->buffer_size) |
| 449 | conn->buffer_size = buffer_size; |
| 450 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 451 | } |
| 452 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 453 | /* Now try to negiociate the protection level. */ |
| 454 | code = ftp_send_command(conn, "PROT %c", level_to_char(level)); |
| 455 | |
| 456 | if(code < 0) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 457 | return -1; |
| 458 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 459 | if(code/100 != 2) { |
| 460 | failf(conn->data, "Failed to set the protection level."); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 461 | return -1; |
| 462 | } |
| 463 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 464 | conn->data_prot = level; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 465 | if(level == PROT_PRIVATE) |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 466 | conn->command_prot = level; |
| 467 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 468 | return 0; |
| 469 | } |
| 470 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 471 | int |
| 472 | Curl_sec_request_prot(struct connectdata *conn, const char *level) |
| 473 | { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 474 | enum protection_level l = name_to_level(level); |
| 475 | if(l == PROT_NONE) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 476 | return -1; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 477 | DEBUGASSERT(l > PROT_NONE && l < PROT_LAST); |
| 478 | conn->request_data_prot = l; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 479 | return 0; |
| 480 | } |
| 481 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 482 | static CURLcode choose_mech(struct connectdata *conn) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 483 | { |
| 484 | int ret; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 485 | struct Curl_easy *data = conn->data; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 486 | void *tmp_allocation; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 487 | const struct Curl_sec_client_mech *mech = &Curl_krb5_client_mech; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 488 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 489 | tmp_allocation = realloc(conn->app_data, mech->size); |
| 490 | if(tmp_allocation == NULL) { |
| 491 | failf(data, "Failed realloc of size %u", mech->size); |
| 492 | mech = NULL; |
| 493 | return CURLE_OUT_OF_MEMORY; |
| 494 | } |
| 495 | conn->app_data = tmp_allocation; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 496 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 497 | if(mech->init) { |
| 498 | ret = mech->init(conn->app_data); |
| 499 | if(ret) { |
| 500 | infof(data, "Failed initialization for %s. Skipping it.\n", |
| 501 | mech->name); |
| 502 | return CURLE_FAILED_INIT; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | infof(data, "Trying mechanism %s...\n", mech->name); |
| 507 | ret = ftp_send_command(conn, "AUTH %s", mech->name); |
| 508 | if(ret < 0) |
| 509 | /* FIXME: This error is too generic but it is OK for now. */ |
| 510 | return CURLE_COULDNT_CONNECT; |
| 511 | |
| 512 | if(ret/100 != 3) { |
| 513 | switch(ret) { |
| 514 | case 504: |
| 515 | infof(data, "Mechanism %s is not supported by the server (server " |
| 516 | "returned ftp code: 504).\n", mech->name); |
| 517 | break; |
| 518 | case 534: |
| 519 | infof(data, "Mechanism %s was rejected by the server (server returned " |
| 520 | "ftp code: 534).\n", mech->name); |
| 521 | break; |
| 522 | default: |
| 523 | if(ret/100 == 5) { |
| 524 | infof(data, "server does not support the security extensions\n"); |
| 525 | return CURLE_USE_SSL_FAILED; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 526 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 527 | break; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 528 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 529 | return CURLE_LOGIN_DENIED; |
| 530 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 531 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 532 | /* Authenticate */ |
| 533 | ret = mech->auth(conn->app_data, conn); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 534 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 535 | if(ret != AUTH_CONTINUE) { |
| 536 | if(ret != AUTH_OK) { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 537 | /* Mechanism has dumped the error to stderr, don't error here. */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 538 | return -1; |
| 539 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 540 | DEBUGASSERT(ret == AUTH_OK); |
| 541 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 542 | conn->mech = mech; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 543 | conn->sec_complete = 1; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 544 | conn->recv[FIRSTSOCKET] = sec_recv; |
| 545 | conn->send[FIRSTSOCKET] = sec_send; |
| 546 | conn->recv[SECONDARYSOCKET] = sec_recv; |
| 547 | conn->send[SECONDARYSOCKET] = sec_send; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 548 | conn->command_prot = PROT_SAFE; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 549 | /* Set the requested protection level */ |
| 550 | /* BLOCKING */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 551 | (void)sec_set_protection_level(conn); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 554 | return CURLE_OK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 555 | } |
| 556 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 557 | CURLcode |
| 558 | Curl_sec_login(struct connectdata *conn) |
| 559 | { |
| 560 | return choose_mech(conn); |
| 561 | } |
| 562 | |
| 563 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 564 | void |
| 565 | Curl_sec_end(struct connectdata *conn) |
| 566 | { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 567 | if(conn->mech != NULL && conn->mech->end) |
| 568 | conn->mech->end(conn->app_data); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 569 | free(conn->app_data); |
| 570 | conn->app_data = NULL; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 571 | if(conn->in_buffer.data) { |
| 572 | free(conn->in_buffer.data); |
| 573 | conn->in_buffer.data = NULL; |
| 574 | conn->in_buffer.size = 0; |
| 575 | conn->in_buffer.index = 0; |
| 576 | /* FIXME: Is this really needed? */ |
| 577 | conn->in_buffer.eof_flag = 0; |
| 578 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 579 | conn->sec_complete = 0; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 580 | conn->data_prot = PROT_CLEAR; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 581 | conn->mech = NULL; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 582 | } |
| 583 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 584 | #endif /* HAVE_GSSAPI */ |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 585 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 586 | #endif /* CURL_DISABLE_FTP */ |