security.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /* This source code was modified by Martin Hedenfalk <[email protected]> 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. * <[email protected]> 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. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. *
  25. * 3. Neither the name of the Institute nor the names of its contributors
  26. * may be used to endorse or promote products derived from this software
  27. * without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  30. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  33. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  37. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  38. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  39. * SUCH DAMAGE. */
  40. #include "setup.h"
  41. #ifdef KRB4
  42. #define _MPRINTF_REPLACE /* we want curl-functions instead of native ones */
  43. #include <curl/mprintf.h>
  44. #include "security.h"
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <netdb.h>
  48. #ifdef HAVE_UNISTD_H
  49. #include <unistd.h>
  50. #endif
  51. #include "base64.h"
  52. #include "sendf.h"
  53. #include "ftp.h"
  54. /* The last #include file should be: */
  55. #ifdef MALLOCDEBUG
  56. #include "memdebug.h"
  57. #endif
  58. #define min(a, b) ((a) < (b) ? (a) : (b))
  59. static struct {
  60. enum protection_level level;
  61. const char *name;
  62. } level_names[] = {
  63. { prot_clear, "clear" },
  64. { prot_safe, "safe" },
  65. { prot_confidential, "confidential" },
  66. { prot_private, "private" }
  67. };
  68. static enum protection_level
  69. name_to_level(const char *name)
  70. {
  71. int i;
  72. for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++)
  73. if(!strncasecmp(level_names[i].name, name, strlen(name)))
  74. return level_names[i].level;
  75. return (enum protection_level)-1;
  76. }
  77. static struct Curl_sec_client_mech *mechs[] = {
  78. #ifdef KRB5
  79. /* not supported */
  80. #endif
  81. #ifdef KRB4
  82. &Curl_krb4_client_mech,
  83. #endif
  84. NULL
  85. };
  86. int
  87. Curl_sec_getc(struct connectdata *conn, FILE *F)
  88. {
  89. if(conn->sec_complete && conn->data_prot) {
  90. char c;
  91. if(Curl_sec_read(conn, fileno(F), &c, 1) <= 0)
  92. return EOF;
  93. return c;
  94. }
  95. else
  96. return getc(F);
  97. }
  98. static int
  99. block_read(int fd, void *buf, size_t len)
  100. {
  101. unsigned char *p = buf;
  102. int b;
  103. while(len) {
  104. b = read(fd, p, len);
  105. if (b == 0)
  106. return 0;
  107. else if (b < 0)
  108. return -1;
  109. len -= b;
  110. p += b;
  111. }
  112. return p - (unsigned char*)buf;
  113. }
  114. static int
  115. block_write(int fd, void *buf, size_t len)
  116. {
  117. unsigned char *p = buf;
  118. int b;
  119. while(len) {
  120. b = write(fd, p, len);
  121. if(b < 0)
  122. return -1;
  123. len -= b;
  124. p += b;
  125. }
  126. return p - (unsigned char*)buf;
  127. }
  128. static int
  129. sec_get_data(struct connectdata *conn,
  130. int fd, struct krb4buffer *buf)
  131. {
  132. int len;
  133. int b;
  134. b = block_read(fd, &len, sizeof(len));
  135. if (b == 0)
  136. return 0;
  137. else if (b < 0)
  138. return -1;
  139. len = ntohl(len);
  140. buf->data = realloc(buf->data, len);
  141. b = block_read(fd, buf->data, len);
  142. if (b == 0)
  143. return 0;
  144. else if (b < 0)
  145. return -1;
  146. buf->size = (conn->mech->decode)(conn->app_data, buf->data, len,
  147. conn->data_prot, conn);
  148. buf->index = 0;
  149. return 0;
  150. }
  151. static size_t
  152. buffer_read(struct krb4buffer *buf, void *data, size_t len)
  153. {
  154. len = min(len, buf->size - buf->index);
  155. memcpy(data, (char*)buf->data + buf->index, len);
  156. buf->index += len;
  157. return len;
  158. }
  159. static size_t
  160. buffer_write(struct krb4buffer *buf, void *data, size_t len)
  161. {
  162. if(buf->index + len > buf->size) {
  163. void *tmp;
  164. if(buf->data == NULL)
  165. tmp = malloc(1024);
  166. else
  167. tmp = realloc(buf->data, buf->index + len);
  168. if(tmp == NULL)
  169. return -1;
  170. buf->data = tmp;
  171. buf->size = buf->index + len;
  172. }
  173. memcpy((char*)buf->data + buf->index, data, len);
  174. buf->index += len;
  175. return len;
  176. }
  177. int
  178. Curl_sec_read(struct connectdata *conn, int fd, void *buffer, int length)
  179. {
  180. size_t len;
  181. int rx = 0;
  182. if(conn->sec_complete == 0 || conn->data_prot == 0)
  183. return read(fd, buffer, length);
  184. if(conn->in_buffer.eof_flag){
  185. conn->in_buffer.eof_flag = 0;
  186. return 0;
  187. }
  188. len = buffer_read(&conn->in_buffer, buffer, length);
  189. length -= len;
  190. rx += len;
  191. buffer = (char*)buffer + len;
  192. while(length) {
  193. if(sec_get_data(conn, fd, &conn->in_buffer) < 0)
  194. return -1;
  195. if(conn->in_buffer.size == 0) {
  196. if(rx)
  197. conn->in_buffer.eof_flag = 1;
  198. return rx;
  199. }
  200. len = buffer_read(&conn->in_buffer, buffer, length);
  201. length -= len;
  202. rx += len;
  203. buffer = (char*)buffer + len;
  204. }
  205. return rx;
  206. }
  207. static int
  208. sec_send(struct connectdata *conn, int fd, char *from, int length)
  209. {
  210. int bytes;
  211. void *buf;
  212. bytes = (conn->mech->encode)(conn->app_data, from, length, conn->data_prot,
  213. &buf, conn);
  214. bytes = htonl(bytes);
  215. block_write(fd, &bytes, sizeof(bytes));
  216. block_write(fd, buf, ntohl(bytes));
  217. free(buf);
  218. return length;
  219. }
  220. int
  221. Curl_sec_fflush_fd(struct connectdata *conn, int fd)
  222. {
  223. if(conn->data_prot != prot_clear) {
  224. if(conn->out_buffer.index > 0){
  225. Curl_sec_write(conn, fd,
  226. conn->out_buffer.data, conn->out_buffer.index);
  227. conn->out_buffer.index = 0;
  228. }
  229. sec_send(conn, fd, NULL, 0);
  230. }
  231. return 0;
  232. }
  233. int
  234. Curl_sec_write(struct connectdata *conn, int fd, char *buffer, int length)
  235. {
  236. int len = conn->buffer_size;
  237. int tx = 0;
  238. if(conn->data_prot == prot_clear)
  239. return write(fd, buffer, length);
  240. len -= (conn->mech->overhead)(conn->app_data, conn->data_prot, len);
  241. while(length){
  242. if(length < len)
  243. len = length;
  244. sec_send(conn, fd, buffer, len);
  245. length -= len;
  246. buffer += len;
  247. tx += len;
  248. }
  249. return tx;
  250. }
  251. int
  252. Curl_sec_vfprintf2(struct connectdata *conn, FILE *f, const char *fmt, va_list ap)
  253. {
  254. char *buf;
  255. int ret;
  256. if(conn->data_prot == prot_clear)
  257. return vfprintf(f, fmt, ap);
  258. else {
  259. buf = aprintf(fmt, ap);
  260. ret = buffer_write(&conn->out_buffer, buf, strlen(buf));
  261. free(buf);
  262. return ret;
  263. }
  264. }
  265. int
  266. Curl_sec_fprintf2(struct connectdata *conn, FILE *f, const char *fmt, ...)
  267. {
  268. int ret;
  269. va_list ap;
  270. va_start(ap, fmt);
  271. ret = Curl_sec_vfprintf2(conn, f, fmt, ap);
  272. va_end(ap);
  273. return ret;
  274. }
  275. int
  276. Curl_sec_putc(struct connectdata *conn, int c, FILE *F)
  277. {
  278. char ch = c;
  279. if(conn->data_prot == prot_clear)
  280. return putc(c, F);
  281. buffer_write(&conn->out_buffer, &ch, 1);
  282. if(c == '\n' || conn->out_buffer.index >= 1024 /* XXX */) {
  283. Curl_sec_write(conn, fileno(F), conn->out_buffer.data, conn->out_buffer.index);
  284. conn->out_buffer.index = 0;
  285. }
  286. return c;
  287. }
  288. int
  289. Curl_sec_read_msg(struct connectdata *conn, char *s, int level)
  290. {
  291. int len;
  292. char *buf;
  293. int code;
  294. buf = malloc(strlen(s));
  295. len = Curl_base64_decode(s + 4, buf); /* XXX */
  296. len = (conn->mech->decode)(conn->app_data, buf, len, level, conn);
  297. if(len < 0)
  298. return -1;
  299. buf[len] = '\0';
  300. if(buf[3] == '-')
  301. code = 0;
  302. else
  303. sscanf(buf, "%d", &code);
  304. if(buf[len-1] == '\n')
  305. buf[len-1] = '\0';
  306. strcpy(s, buf);
  307. free(buf);
  308. return code;
  309. }
  310. /* modified to return how many bytes written, or -1 on error ***/
  311. int
  312. Curl_sec_vfprintf(struct connectdata *conn, FILE *f, const char *fmt, va_list ap)
  313. {
  314. int ret = 0;
  315. char *buf;
  316. void *enc;
  317. int len;
  318. if(!conn->sec_complete)
  319. return vfprintf(f, fmt, ap);
  320. buf = aprintf(fmt, ap);
  321. len = (conn->mech->encode)(conn->app_data, buf, strlen(buf),
  322. conn->command_prot, &enc,
  323. conn);
  324. free(buf);
  325. if(len < 0) {
  326. failf(conn->data, "Failed to encode command.");
  327. return -1;
  328. }
  329. if(Curl_base64_encode(enc, len, &buf) < 0){
  330. failf(conn->data, "Out of memory base64-encoding.");
  331. return -1;
  332. }
  333. if(conn->command_prot == prot_safe)
  334. ret = fprintf(f, "MIC %s", buf);
  335. else if(conn->command_prot == prot_private)
  336. ret = fprintf(f, "ENC %s", buf);
  337. else if(conn->command_prot == prot_confidential)
  338. ret = fprintf(f, "CONF %s", buf);
  339. free(buf);
  340. return ret;
  341. }
  342. int
  343. Curl_sec_fprintf(struct connectdata *conn, FILE *f, const char *fmt, ...)
  344. {
  345. va_list ap;
  346. int ret;
  347. va_start(ap, fmt);
  348. ret = Curl_sec_vfprintf(conn, f, fmt, ap);
  349. va_end(ap);
  350. return ret;
  351. }
  352. enum protection_level
  353. Curl_set_command_prot(struct connectdata *conn, enum protection_level level)
  354. {
  355. enum protection_level old = conn->command_prot;
  356. conn->command_prot = level;
  357. return old;
  358. }
  359. static int
  360. sec_prot_internal(struct connectdata *conn, int level)
  361. {
  362. char *p;
  363. unsigned int s = 1048576;
  364. ssize_t nread;
  365. if(!conn->sec_complete){
  366. infof(conn->data, "No security data exchange has taken place.\n");
  367. return -1;
  368. }
  369. if(level){
  370. if(Curl_ftpsendf(conn, "PBSZ %u", s))
  371. return -1;
  372. nread = Curl_GetFTPResponse(conn->data->state.buffer, conn, NULL);
  373. if(nread < 0)
  374. return -1;
  375. if(conn->data->state.buffer[0] != '2'){
  376. failf(conn->data, "Failed to set protection buffer size.");
  377. return -1;
  378. }
  379. conn->buffer_size = s;
  380. p = strstr(conn->data->state.buffer, "PBSZ=");
  381. if(p)
  382. sscanf(p, "PBSZ=%u", &s);
  383. if(s < conn->buffer_size)
  384. conn->buffer_size = s;
  385. }
  386. if(Curl_ftpsendf(conn, "PROT %c", level["CSEP"]))
  387. return -1;
  388. nread = Curl_GetFTPResponse(conn->data->state.buffer, conn, NULL);
  389. if(nread < 0)
  390. return -1;
  391. if(conn->data->state.buffer[0] != '2'){
  392. failf(conn->data, "Failed to set protection level.");
  393. return -1;
  394. }
  395. conn->data_prot = (enum protection_level)level;
  396. return 0;
  397. }
  398. void
  399. Curl_sec_set_protection_level(struct connectdata *conn)
  400. {
  401. if(conn->sec_complete && conn->data_prot != conn->request_data_prot)
  402. sec_prot_internal(conn, conn->request_data_prot);
  403. }
  404. int
  405. Curl_sec_request_prot(struct connectdata *conn, const char *level)
  406. {
  407. int l = name_to_level(level);
  408. if(l == -1)
  409. return -1;
  410. conn->request_data_prot = (enum protection_level)l;
  411. return 0;
  412. }
  413. int
  414. Curl_sec_login(struct connectdata *conn)
  415. {
  416. int ret;
  417. struct Curl_sec_client_mech **m;
  418. ssize_t nread;
  419. struct SessionHandle *data=conn->data;
  420. int ftpcode;
  421. for(m = mechs; *m && (*m)->name; m++) {
  422. void *tmp;
  423. tmp = realloc(conn->app_data, (*m)->size);
  424. if (tmp == NULL) {
  425. failf (data, "realloc %u failed", (*m)->size);
  426. return -1;
  427. }
  428. conn->app_data = tmp;
  429. if((*m)->init && (*(*m)->init)(conn->app_data) != 0) {
  430. infof(data, "Skipping %s...\n", (*m)->name);
  431. continue;
  432. }
  433. infof(data, "Trying %s...\n", (*m)->name);
  434. if(Curl_ftpsendf(conn, "AUTH %s", (*m)->name))
  435. return -1;
  436. nread = Curl_GetFTPResponse(conn->data->state.buffer, conn, &ftpcode);
  437. if(nread < 0)
  438. return -1;
  439. if(conn->data->state.buffer[0] != '3'){
  440. switch(ftpcode) {
  441. case 504:
  442. infof(data,
  443. "%s is not supported by the server.\n", (*m)->name);
  444. break;
  445. case 534:
  446. infof(data, "%s rejected as security mechanism.\n", (*m)->name);
  447. break;
  448. default:
  449. if(conn->data->state.buffer[0] == '5') {
  450. infof(data, "The server doesn't support the FTP "
  451. "security extensions.\n");
  452. return -1;
  453. }
  454. break;
  455. }
  456. continue;
  457. }
  458. ret = (*(*m)->auth)(conn->app_data, conn);
  459. if(ret == AUTH_CONTINUE)
  460. continue;
  461. else if(ret != AUTH_OK){
  462. /* mechanism is supposed to output error string */
  463. return -1;
  464. }
  465. conn->mech = *m;
  466. conn->sec_complete = 1;
  467. conn->command_prot = prot_safe;
  468. break;
  469. }
  470. return *m == NULL;
  471. }
  472. void
  473. Curl_sec_end(struct connectdata *conn)
  474. {
  475. if (conn->mech != NULL) {
  476. if(conn->mech->end)
  477. (conn->mech->end)(conn->app_data);
  478. memset(conn->app_data, 0, conn->mech->size);
  479. free(conn->app_data);
  480. conn->app_data = NULL;
  481. }
  482. conn->sec_complete = 0;
  483. conn->data_prot = (enum protection_level)0;
  484. conn->mech=NULL;
  485. }
  486. #endif /* KRB4 */
  487. /*
  488. * local variables:
  489. * eval: (load-file "../curl-mode.el")
  490. * end:
  491. * vim600: fdm=marker
  492. * vim: et sw=2 ts=2 sts=2 tw=78
  493. */