security.c 12 KB

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