security.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 - 2009, 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. #define _MPRINTF_REPLACE /* we want curl-functions instead of native ones */
  47. #include <curl/mprintf.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #ifdef HAVE_NETDB_H
  51. #include <netdb.h>
  52. #endif
  53. #ifdef HAVE_UNISTD_H
  54. #include <unistd.h>
  55. #endif
  56. #include "urldata.h"
  57. #include "krb4.h"
  58. #include "curl_base64.h"
  59. #include "sendf.h"
  60. #include "ftp.h"
  61. #include "curl_memory.h"
  62. #include "rawstr.h"
  63. /* The last #include file should be: */
  64. #include "memdebug.h"
  65. static const struct {
  66. enum protection_level level;
  67. const char *name;
  68. } level_names[] = {
  69. { prot_clear, "clear" },
  70. { prot_safe, "safe" },
  71. { prot_confidential, "confidential" },
  72. { prot_private, "private" }
  73. };
  74. static enum protection_level
  75. name_to_level(const char *name)
  76. {
  77. int i;
  78. for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++)
  79. if(checkprefix(name, level_names[i].name))
  80. return level_names[i].level;
  81. return (enum protection_level)-1;
  82. }
  83. static const struct Curl_sec_client_mech * const mechs[] = {
  84. #ifdef HAVE_GSSAPI
  85. &Curl_krb5_client_mech,
  86. #endif
  87. #ifdef HAVE_KRB4
  88. &Curl_krb4_client_mech,
  89. #endif
  90. NULL
  91. };
  92. /* TODO: This function isn't actually used anywhere and should be removed */
  93. int
  94. Curl_sec_getc(struct connectdata *conn, FILE *F)
  95. {
  96. if(conn->sec_complete && conn->data_prot) {
  97. char c;
  98. if(Curl_sec_read(conn, fileno(F), &c, 1) <= 0)
  99. return EOF;
  100. return c;
  101. }
  102. else
  103. return getc(F);
  104. }
  105. static int
  106. block_read(int fd, void *buf, size_t len)
  107. {
  108. unsigned char *p = buf;
  109. int b;
  110. while(len) {
  111. b = read(fd, p, len);
  112. if(b == 0)
  113. return 0;
  114. else if(b < 0 && (errno == EINTR || errno == EAGAIN))
  115. /* TODO: this will busy loop in the EAGAIN case */
  116. continue;
  117. else if(b < 0)
  118. return -1;
  119. len -= b;
  120. p += b;
  121. }
  122. return p - (unsigned char*)buf;
  123. }
  124. static int
  125. block_write(int fd, const void *buf, size_t len)
  126. {
  127. const unsigned char *p = buf;
  128. int b;
  129. while(len) {
  130. b = write(fd, p, len);
  131. if(b < 0 && (errno == EINTR || errno == EAGAIN))
  132. continue;
  133. else if(b < 0)
  134. return -1;
  135. len -= b;
  136. p += b;
  137. }
  138. return p - (unsigned char*)buf;
  139. }
  140. static int
  141. sec_get_data(struct connectdata *conn,
  142. int fd, struct krb4buffer *buf)
  143. {
  144. int len;
  145. int b;
  146. b = block_read(fd, &len, sizeof(len));
  147. if(b == 0)
  148. return 0;
  149. else if(b < 0)
  150. return -1;
  151. len = ntohl(len);
  152. /* TODO: This realloc will cause a memory leak in an out of memory
  153. * condition */
  154. buf->data = realloc(buf->data, len);
  155. b = buf->data ? block_read(fd, buf->data, len) : -1;
  156. if(b == 0)
  157. return 0;
  158. else if(b < 0)
  159. return -1;
  160. buf->size = (conn->mech->decode)(conn->app_data, buf->data, len,
  161. conn->data_prot, conn);
  162. buf->index = 0;
  163. return 0;
  164. }
  165. static size_t
  166. buffer_read(struct krb4buffer *buf, void *data, size_t len)
  167. {
  168. if(buf->size - buf->index < len)
  169. len = buf->size - buf->index;
  170. memcpy(data, (char*)buf->data + buf->index, len);
  171. buf->index += len;
  172. return len;
  173. }
  174. static size_t
  175. buffer_write(struct krb4buffer *buf, void *data, size_t len)
  176. {
  177. if(buf->index + len > buf->size) {
  178. void *tmp;
  179. if(buf->data == NULL)
  180. tmp = malloc(1024);
  181. else
  182. tmp = realloc(buf->data, buf->index + len);
  183. if(tmp == NULL)
  184. return -1;
  185. buf->data = tmp;
  186. buf->size = buf->index + len;
  187. }
  188. memcpy((char*)buf->data + buf->index, data, len);
  189. buf->index += len;
  190. return len;
  191. }
  192. int
  193. Curl_sec_read(struct connectdata *conn, int fd, void *buffer, int length)
  194. {
  195. size_t len;
  196. int rx = 0;
  197. if(conn->sec_complete == 0 || conn->data_prot == 0)
  198. return read(fd, buffer, length);
  199. if(conn->in_buffer.eof_flag){
  200. conn->in_buffer.eof_flag = 0;
  201. return 0;
  202. }
  203. len = buffer_read(&conn->in_buffer, buffer, length);
  204. length -= len;
  205. rx += len;
  206. buffer = (char*)buffer + len;
  207. while(length) {
  208. if(sec_get_data(conn, fd, &conn->in_buffer) < 0)
  209. return -1;
  210. if(conn->in_buffer.size == 0) {
  211. if(rx)
  212. conn->in_buffer.eof_flag = 1;
  213. return rx;
  214. }
  215. len = buffer_read(&conn->in_buffer, buffer, length);
  216. length -= len;
  217. rx += len;
  218. buffer = (char*)buffer + len;
  219. }
  220. return rx;
  221. }
  222. static int
  223. sec_send(struct connectdata *conn, int fd, const char *from, int length)
  224. {
  225. int bytes;
  226. void *buf;
  227. enum protection_level protlevel = conn->data_prot;
  228. int iscmd = protlevel == prot_cmd;
  229. if(iscmd) {
  230. if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
  231. protlevel = prot_private;
  232. else
  233. protlevel = conn->command_prot;
  234. }
  235. bytes = (conn->mech->encode)(conn->app_data, from, length, protlevel,
  236. &buf, conn);
  237. if(iscmd) {
  238. char *cmdbuf;
  239. bytes = Curl_base64_encode(conn->data, (char *)buf, bytes, &cmdbuf);
  240. if(bytes > 0) {
  241. if(protlevel == prot_private)
  242. block_write(fd, "ENC ", 4);
  243. else
  244. block_write(fd, "MIC ", 4);
  245. block_write(fd, cmdbuf, bytes);
  246. block_write(fd, "\r\n", 2);
  247. Curl_infof(conn->data, "%s %s\n",
  248. protlevel == prot_private ? "ENC" : "MIC", cmdbuf);
  249. free(cmdbuf);
  250. }
  251. }
  252. else {
  253. bytes = htonl(bytes);
  254. block_write(fd, &bytes, sizeof(bytes));
  255. block_write(fd, buf, ntohl(bytes));
  256. }
  257. free(buf);
  258. return length;
  259. }
  260. int
  261. Curl_sec_fflush_fd(struct connectdata *conn, int fd)
  262. {
  263. if(conn->data_prot != prot_clear) {
  264. if(conn->out_buffer.index > 0){
  265. Curl_sec_write(conn, fd,
  266. conn->out_buffer.data, conn->out_buffer.index);
  267. conn->out_buffer.index = 0;
  268. }
  269. sec_send(conn, fd, NULL, 0);
  270. }
  271. return 0;
  272. }
  273. int
  274. Curl_sec_write(struct connectdata *conn, int fd, const char *buffer, int length)
  275. {
  276. int len = conn->buffer_size;
  277. int tx = 0;
  278. if(conn->data_prot == prot_clear)
  279. return write(fd, buffer, length);
  280. len -= (conn->mech->overhead)(conn->app_data, conn->data_prot, len);
  281. if(len <= 0)
  282. len = length;
  283. while(length){
  284. if(length < len)
  285. len = length;
  286. sec_send(conn, fd, buffer, len);
  287. length -= len;
  288. buffer += len;
  289. tx += len;
  290. }
  291. return tx;
  292. }
  293. ssize_t
  294. Curl_sec_send(struct connectdata *conn, int num, const char *buffer, int length)
  295. {
  296. curl_socket_t fd = conn->sock[num];
  297. return (ssize_t)Curl_sec_write(conn, fd, buffer, length);
  298. }
  299. int
  300. Curl_sec_putc(struct connectdata *conn, int c, FILE *F)
  301. {
  302. char ch = c;
  303. if(conn->data_prot == prot_clear)
  304. return putc(c, F);
  305. buffer_write(&conn->out_buffer, &ch, 1);
  306. if(c == '\n' || conn->out_buffer.index >= 1024 /* XXX */) {
  307. Curl_sec_write(conn, fileno(F), conn->out_buffer.data,
  308. conn->out_buffer.index);
  309. conn->out_buffer.index = 0;
  310. }
  311. return c;
  312. }
  313. int
  314. Curl_sec_read_msg(struct connectdata *conn, char *s, int level)
  315. {
  316. int len;
  317. unsigned char *buf;
  318. int code;
  319. len = Curl_base64_decode(s + 4, &buf); /* XXX */
  320. if(len > 0)
  321. len = (conn->mech->decode)(conn->app_data, buf, len, level, conn);
  322. else
  323. return -1;
  324. if(len < 0) {
  325. free(buf);
  326. return -1;
  327. }
  328. if(conn->data->set.verbose) {
  329. buf[len] = '\n';
  330. Curl_debug(conn->data, CURLINFO_HEADER_IN, (char *)buf, len + 1, conn);
  331. }
  332. buf[len] = '\0';
  333. if(buf[3] == '-')
  334. code = 0;
  335. else
  336. sscanf((char *)buf, "%d", &code);
  337. if(buf[len-1] == '\n')
  338. buf[len-1] = '\0';
  339. strcpy(s, (char *)buf);
  340. free(buf);
  341. return code;
  342. }
  343. enum protection_level
  344. Curl_set_command_prot(struct connectdata *conn, enum protection_level level)
  345. {
  346. enum protection_level old = conn->command_prot;
  347. conn->command_prot = level;
  348. return old;
  349. }
  350. static int
  351. sec_prot_internal(struct connectdata *conn, int level)
  352. {
  353. char *p;
  354. unsigned int s = 1048576;
  355. ssize_t nread;
  356. if(!conn->sec_complete){
  357. infof(conn->data, "No security data exchange has taken place.\n");
  358. return -1;
  359. }
  360. if(level){
  361. int code;
  362. if(Curl_ftpsendf(conn, "PBSZ %u", s))
  363. return -1;
  364. if(Curl_GetFTPResponse(&nread, conn, &code))
  365. return -1;
  366. if(code/100 != 2){
  367. failf(conn->data, "Failed to set protection buffer size.");
  368. return -1;
  369. }
  370. conn->buffer_size = s;
  371. p = strstr(conn->data->state.buffer, "PBSZ=");
  372. if(p)
  373. sscanf(p, "PBSZ=%u", &s);
  374. if(s < conn->buffer_size)
  375. conn->buffer_size = s;
  376. }
  377. if(Curl_ftpsendf(conn, "PROT %c", level["CSEP"]))
  378. return -1;
  379. if(Curl_GetFTPResponse(&nread, conn, NULL))
  380. return -1;
  381. if(conn->data->state.buffer[0] != '2'){
  382. failf(conn->data, "Failed to set protection level.");
  383. return -1;
  384. }
  385. conn->data_prot = (enum protection_level)level;
  386. if(level == prot_private)
  387. conn->command_prot = (enum protection_level)level;
  388. return 0;
  389. }
  390. void
  391. Curl_sec_set_protection_level(struct connectdata *conn)
  392. {
  393. if(conn->sec_complete && conn->data_prot != conn->request_data_prot)
  394. sec_prot_internal(conn, conn->request_data_prot);
  395. }
  396. int
  397. Curl_sec_request_prot(struct connectdata *conn, const char *level)
  398. {
  399. int l = name_to_level(level);
  400. if(l == -1)
  401. return -1;
  402. conn->request_data_prot = (enum protection_level)l;
  403. return 0;
  404. }
  405. int
  406. Curl_sec_login(struct connectdata *conn)
  407. {
  408. int ret;
  409. const struct Curl_sec_client_mech * const *m;
  410. ssize_t nread;
  411. struct SessionHandle *data=conn->data;
  412. int ftpcode;
  413. for(m = mechs; *m && (*m)->name; m++) {
  414. void *tmp;
  415. tmp = realloc(conn->app_data, (*m)->size);
  416. if(tmp == NULL) {
  417. failf (data, "realloc %u failed", (*m)->size);
  418. return -1;
  419. }
  420. conn->app_data = tmp;
  421. if((*m)->init && (*(*m)->init)(conn->app_data) != 0) {
  422. infof(data, "Skipping %s...\n", (*m)->name);
  423. continue;
  424. }
  425. infof(data, "Trying %s...\n", (*m)->name);
  426. if(Curl_ftpsendf(conn, "AUTH %s", (*m)->name))
  427. return -1;
  428. if(Curl_GetFTPResponse(&nread, conn, &ftpcode))
  429. return -1;
  430. if(conn->data->state.buffer[0] != '3'){
  431. switch(ftpcode) {
  432. case 504:
  433. infof(data,
  434. "%s is not supported by the server.\n", (*m)->name);
  435. break;
  436. case 534:
  437. infof(data, "%s rejected as security mechanism.\n", (*m)->name);
  438. break;
  439. default:
  440. if(conn->data->state.buffer[0] == '5') {
  441. infof(data, "The server doesn't support the FTP "
  442. "security extensions.\n");
  443. return -1;
  444. }
  445. break;
  446. }
  447. continue;
  448. }
  449. ret = (*(*m)->auth)(conn->app_data, conn);
  450. if(ret == AUTH_CONTINUE)
  451. continue;
  452. else if(ret != AUTH_OK){
  453. /* mechanism is supposed to output error string */
  454. return -1;
  455. }
  456. conn->mech = *m;
  457. conn->sec_complete = 1;
  458. conn->command_prot = prot_safe;
  459. /* Set the requested protection level */
  460. /* BLOCKING */
  461. Curl_sec_set_protection_level(conn);
  462. break;
  463. }
  464. return *m == NULL;
  465. }
  466. void
  467. Curl_sec_end(struct connectdata *conn)
  468. {
  469. if(conn->mech != NULL) {
  470. if(conn->mech->end)
  471. (conn->mech->end)(conn->app_data);
  472. memset(conn->app_data, 0, conn->mech->size);
  473. free(conn->app_data);
  474. conn->app_data = NULL;
  475. }
  476. conn->sec_complete = 0;
  477. conn->data_prot = (enum protection_level)0;
  478. conn->mech=NULL;
  479. }
  480. #endif /* HAVE_KRB4 */
  481. #endif /* CURL_DISABLE_FTP */