security.c 15 KB

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