security.c 16 KB

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