security.c 15 KB

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