security.c 16 KB

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