security.c 16 KB

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