1
0

security.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. #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 "security.h"
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <netdb.h>
  49. #ifdef HAVE_UNISTD_H
  50. #include <unistd.h>
  51. #endif
  52. #include "base64.h"
  53. #include "sendf.h"
  54. #include "ftp.h"
  55. #include "curl_memory.h"
  56. /* The last #include file should be: */
  57. #include "memdebug.h"
  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 HAVE_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_putc(struct connectdata *conn, int c, FILE *F)
  253. {
  254. char ch = c;
  255. if(conn->data_prot == prot_clear)
  256. return putc(c, F);
  257. buffer_write(&conn->out_buffer, &ch, 1);
  258. if(c == '\n' || conn->out_buffer.index >= 1024 /* XXX */) {
  259. Curl_sec_write(conn, fileno(F), conn->out_buffer.data,
  260. conn->out_buffer.index);
  261. conn->out_buffer.index = 0;
  262. }
  263. return c;
  264. }
  265. int
  266. Curl_sec_read_msg(struct connectdata *conn, char *s, int level)
  267. {
  268. int len;
  269. char *buf;
  270. int code;
  271. buf = malloc(strlen(s));
  272. len = Curl_base64_decode(s + 4, buf); /* XXX */
  273. len = (conn->mech->decode)(conn->app_data, buf, len, level, conn);
  274. if(len < 0) {
  275. free(buf);
  276. return -1;
  277. }
  278. buf[len] = '\0';
  279. if(buf[3] == '-')
  280. code = 0;
  281. else
  282. sscanf(buf, "%d", &code);
  283. if(buf[len-1] == '\n')
  284. buf[len-1] = '\0';
  285. strcpy(s, buf);
  286. free(buf);
  287. return code;
  288. }
  289. enum protection_level
  290. Curl_set_command_prot(struct connectdata *conn, enum protection_level level)
  291. {
  292. enum protection_level old = conn->command_prot;
  293. conn->command_prot = level;
  294. return old;
  295. }
  296. static int
  297. sec_prot_internal(struct connectdata *conn, int level)
  298. {
  299. char *p;
  300. unsigned int s = 1048576;
  301. ssize_t nread;
  302. if(!conn->sec_complete){
  303. infof(conn->data, "No security data exchange has taken place.\n");
  304. return -1;
  305. }
  306. if(level){
  307. int code;
  308. if(Curl_ftpsendf(conn, "PBSZ %u", s))
  309. return -1;
  310. if(Curl_GetFTPResponse(&nread, conn, &code))
  311. return -1;
  312. if(code/100 != '2'){
  313. failf(conn->data, "Failed to set protection buffer size.");
  314. return -1;
  315. }
  316. conn->buffer_size = s;
  317. p = strstr(conn->data->state.buffer, "PBSZ=");
  318. if(p)
  319. sscanf(p, "PBSZ=%u", &s);
  320. if(s < conn->buffer_size)
  321. conn->buffer_size = s;
  322. }
  323. if(Curl_ftpsendf(conn, "PROT %c", level["CSEP"]))
  324. return -1;
  325. if(Curl_GetFTPResponse(&nread, conn, NULL))
  326. return -1;
  327. if(conn->data->state.buffer[0] != '2'){
  328. failf(conn->data, "Failed to set protection level.");
  329. return -1;
  330. }
  331. conn->data_prot = (enum protection_level)level;
  332. return 0;
  333. }
  334. void
  335. Curl_sec_set_protection_level(struct connectdata *conn)
  336. {
  337. if(conn->sec_complete && conn->data_prot != conn->request_data_prot)
  338. sec_prot_internal(conn, conn->request_data_prot);
  339. }
  340. int
  341. Curl_sec_request_prot(struct connectdata *conn, const char *level)
  342. {
  343. int l = name_to_level(level);
  344. if(l == -1)
  345. return -1;
  346. conn->request_data_prot = (enum protection_level)l;
  347. return 0;
  348. }
  349. int
  350. Curl_sec_login(struct connectdata *conn)
  351. {
  352. int ret;
  353. struct Curl_sec_client_mech **m;
  354. ssize_t nread;
  355. struct SessionHandle *data=conn->data;
  356. int ftpcode;
  357. for(m = mechs; *m && (*m)->name; m++) {
  358. void *tmp;
  359. tmp = realloc(conn->app_data, (*m)->size);
  360. if (tmp == NULL) {
  361. failf (data, "realloc %u failed", (*m)->size);
  362. return -1;
  363. }
  364. conn->app_data = tmp;
  365. if((*m)->init && (*(*m)->init)(conn->app_data) != 0) {
  366. infof(data, "Skipping %s...\n", (*m)->name);
  367. continue;
  368. }
  369. infof(data, "Trying %s...\n", (*m)->name);
  370. if(Curl_ftpsendf(conn, "AUTH %s", (*m)->name))
  371. return -1;
  372. if(Curl_GetFTPResponse(&nread, conn, &ftpcode))
  373. return -1;
  374. if(conn->data->state.buffer[0] != '3'){
  375. switch(ftpcode) {
  376. case 504:
  377. infof(data,
  378. "%s is not supported by the server.\n", (*m)->name);
  379. break;
  380. case 534:
  381. infof(data, "%s rejected as security mechanism.\n", (*m)->name);
  382. break;
  383. default:
  384. if(conn->data->state.buffer[0] == '5') {
  385. infof(data, "The server doesn't support the FTP "
  386. "security extensions.\n");
  387. return -1;
  388. }
  389. break;
  390. }
  391. continue;
  392. }
  393. ret = (*(*m)->auth)(conn->app_data, conn);
  394. if(ret == AUTH_CONTINUE)
  395. continue;
  396. else if(ret != AUTH_OK){
  397. /* mechanism is supposed to output error string */
  398. return -1;
  399. }
  400. conn->mech = *m;
  401. conn->sec_complete = 1;
  402. conn->command_prot = prot_safe;
  403. break;
  404. }
  405. return *m == NULL;
  406. }
  407. void
  408. Curl_sec_end(struct connectdata *conn)
  409. {
  410. if (conn->mech != NULL) {
  411. if(conn->mech->end)
  412. (conn->mech->end)(conn->app_data);
  413. memset(conn->app_data, 0, conn->mech->size);
  414. free(conn->app_data);
  415. conn->app_data = NULL;
  416. }
  417. conn->sec_complete = 0;
  418. conn->data_prot = (enum protection_level)0;
  419. conn->mech=NULL;
  420. }
  421. #endif /* HAVE_KRB4 */
  422. #endif /* CURL_DISABLE_FTP */