imap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000 Michael R. Elkins <me@mutt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <assert.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #include <netdb.h>
  31. #if HAVE_LIBSSL
  32. #include <openssl/err.h>
  33. #endif
  34. #include "isync.h"
  35. const char *Flags[] = {
  36. "\\Seen",
  37. "\\Answered",
  38. "\\Deleted",
  39. "\\Flagged",
  40. "\\Recent",
  41. "\\Draft"
  42. };
  43. #if HAVE_LIBSSL
  44. SSL_CTX *SSLContext = 0;
  45. static int
  46. init_ssl (config_t * conf)
  47. {
  48. if (!conf->cert_file)
  49. {
  50. puts ("Error, CertificateFile not defined");
  51. return -1;
  52. }
  53. SSL_library_init ();
  54. SSL_load_error_strings ();
  55. SSLContext = SSL_CTX_new (SSLv23_client_method ());
  56. if (!SSL_CTX_load_verify_locations (SSLContext, conf->cert_file, NULL))
  57. {
  58. printf ("Error, SSL_CTX_load_verify_locations: %s\n",
  59. ERR_error_string (ERR_get_error (), 0));
  60. return -1;
  61. }
  62. SSL_CTX_set_verify (SSLContext,
  63. SSL_VERIFY_PEER |
  64. SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
  65. SSL_VERIFY_CLIENT_ONCE, NULL);
  66. SSL_CTX_set_verify_depth (SSLContext, 1);
  67. return 0;
  68. }
  69. #endif
  70. static int
  71. socket_read (Socket_t * sock, char *buf, size_t len)
  72. {
  73. #if HAVE_LIBSSL
  74. if (sock->use_ssl)
  75. return SSL_read (sock->ssl, buf, len);
  76. #endif
  77. return read (sock->fd, buf, len);
  78. }
  79. static int
  80. socket_write (Socket_t * sock, char *buf, size_t len)
  81. {
  82. #if HAVE_LIBSSL
  83. if (sock->use_ssl)
  84. return SSL_write (sock->ssl, buf, len);
  85. #endif
  86. return write (sock->fd, buf, len);
  87. }
  88. /* simple line buffering */
  89. static int
  90. buffer_gets (buffer_t * b, char **s)
  91. {
  92. int n;
  93. int start = b->offset;
  94. *s = b->buf + start;
  95. for (;;)
  96. {
  97. if (b->offset + 2 > b->bytes)
  98. {
  99. /* shift down used bytes */
  100. *s = b->buf;
  101. assert (start <= b->bytes);
  102. n = b->bytes - start;
  103. if (n)
  104. memmove (b->buf, b->buf + start, n);
  105. b->offset = n;
  106. start = 0;
  107. n =
  108. socket_read (b->sock, b->buf + b->offset,
  109. sizeof (b->buf) - b->offset);
  110. if (n <= 0)
  111. {
  112. if (n == -1)
  113. perror ("read");
  114. else
  115. puts ("EOF");
  116. return -1;
  117. }
  118. b->bytes = b->offset + n;
  119. // printf ("buffer_gets:read %d bytes\n", n);
  120. }
  121. if (b->buf[b->offset] == '\r')
  122. {
  123. if (b->buf[b->offset + 1] == '\n')
  124. {
  125. b->buf[b->offset] = 0; /* terminate the string */
  126. b->offset += 2; /* next line */
  127. return 0;
  128. }
  129. }
  130. b->offset++;
  131. }
  132. /* not reached */
  133. }
  134. static int
  135. imap_exec (imap_t * imap, const char *fmt, ...)
  136. {
  137. va_list ap;
  138. char tmp[256];
  139. char buf[256];
  140. char *cmd;
  141. char *arg;
  142. char *arg1;
  143. message_t **cur = 0;
  144. message_t **rec = 0;
  145. va_start (ap, fmt);
  146. vsnprintf (tmp, sizeof (tmp), fmt, ap);
  147. va_end (ap);
  148. snprintf (buf, sizeof (buf), "%d %s\r\n", ++Tag, tmp);
  149. if (Verbose)
  150. fputs (buf, stdout);
  151. socket_write (imap->sock, buf, strlen (buf));
  152. for (;;)
  153. {
  154. if (buffer_gets (imap->buf, &cmd))
  155. return -1;
  156. if (Verbose)
  157. puts (cmd);
  158. arg = next_arg (&cmd);
  159. if (*arg == '*')
  160. {
  161. arg = next_arg (&cmd);
  162. arg1 = next_arg (&cmd);
  163. if (arg1 && !strcmp ("EXISTS", arg1))
  164. imap->count = atoi (arg);
  165. else if (arg1 && !strcmp ("RECENT", arg1))
  166. imap->recent = atoi (arg);
  167. else if (!strcmp ("SEARCH", arg))
  168. {
  169. if (!rec)
  170. {
  171. rec = &imap->recent_msgs;
  172. while (*rec)
  173. rec = &(*rec)->next;
  174. }
  175. /* need to add arg1 */
  176. *rec = calloc (1, sizeof (message_t));
  177. (*rec)->uid = atoi (arg1);
  178. rec = &(*rec)->next;
  179. /* parse rest of `cmd' */
  180. while ((arg = next_arg (&cmd)))
  181. {
  182. *rec = calloc (1, sizeof (message_t));
  183. (*rec)->uid = atoi (arg);
  184. rec = &(*rec)->next;
  185. }
  186. }
  187. else if (arg1 && !strcmp ("FETCH", arg1))
  188. {
  189. if (!cur)
  190. {
  191. cur = &imap->msgs;
  192. while (*cur)
  193. cur = &(*cur)->next;
  194. }
  195. /* new message
  196. * * <N> FETCH (UID <uid> FLAGS (...))
  197. */
  198. arg = next_arg (&cmd); /* (UID */
  199. arg = next_arg (&cmd); /* <uid> */
  200. *cur = calloc (1, sizeof (message_t));
  201. (*cur)->uid = atoi (arg);
  202. arg = next_arg (&cmd); /* FLAGS */
  203. if (!arg || strcmp ("FLAGS", arg))
  204. {
  205. printf ("FETCH parse error: expected FLAGS at %s\n", arg);
  206. return -1;
  207. }
  208. /* if we need to parse additional info, we should keep
  209. * a copy of this `arg' pointer
  210. */
  211. cmd++;
  212. arg = strchr (cmd, ')');
  213. if (!arg)
  214. {
  215. puts ("FETCH parse error");
  216. return -1;
  217. }
  218. *arg = 0;
  219. /* parse message flags */
  220. while ((arg = next_arg (&cmd)))
  221. {
  222. if (!strcmp ("\\Seen", arg))
  223. (*cur)->flags |= D_SEEN;
  224. else if (!strcmp ("\\Flagged", arg))
  225. (*cur)->flags |= D_FLAGGED;
  226. else if (!strcmp ("\\Deleted", arg))
  227. {
  228. (*cur)->flags |= D_DELETED;
  229. imap->deleted++;
  230. }
  231. else if (!strcmp ("\\Answered", arg))
  232. (*cur)->flags |= D_ANSWERED;
  233. else if (!strcmp ("\\Draft", arg))
  234. (*cur)->flags |= D_DRAFT;
  235. else if (!strcmp ("\\Recent", arg))
  236. (*cur)->flags |= D_RECENT;
  237. else
  238. printf ("warning, unknown flag %s\n", arg);
  239. }
  240. cur = &(*cur)->next;
  241. }
  242. }
  243. else if ((size_t) atol (arg) != Tag)
  244. {
  245. puts ("wrong tag");
  246. return -1;
  247. }
  248. else
  249. {
  250. arg = next_arg (&cmd);
  251. if (!strcmp ("OK", arg))
  252. return 0;
  253. return -1;
  254. }
  255. }
  256. /* not reached */
  257. }
  258. static int
  259. fetch_recent_flags (imap_t * imap)
  260. {
  261. char buf[1024];
  262. message_t **cur = &imap->recent_msgs;
  263. message_t *tmp;
  264. unsigned int start = -1;
  265. unsigned int last = -1;
  266. int ret = 0;
  267. buf[0] = 0;
  268. while (*cur)
  269. {
  270. tmp = *cur;
  271. if (last == (unsigned int) -1)
  272. {
  273. /* init */
  274. start = tmp->uid;
  275. last = tmp->uid;
  276. }
  277. else if (tmp->uid == last + 1)
  278. last++;
  279. else
  280. {
  281. /* out of sequence */
  282. if (start == last)
  283. ret = imap_exec (imap, "UID FETCH %d (UID FLAGS)", start);
  284. else
  285. ret =
  286. imap_exec (imap, "UID FETCH %d:%d (UID FLAGS)", start,
  287. last);
  288. start = tmp->uid;
  289. last = tmp->uid;
  290. }
  291. free (tmp);
  292. *cur = (*cur)->next;
  293. }
  294. if (start != (unsigned int) -1)
  295. {
  296. if (start == last)
  297. ret = imap_exec (imap, "UID FETCH %d (UID FLAGS)", start);
  298. else
  299. ret =
  300. imap_exec (imap, "UID FETCH %d:%d (UID FLAGS)", start, last);
  301. }
  302. return ret;
  303. }
  304. imap_t *
  305. imap_open (config_t * box, int fast)
  306. {
  307. int ret;
  308. imap_t *imap;
  309. int s;
  310. struct sockaddr_in sin;
  311. struct hostent *he;
  312. #if HAVE_LIBSSL
  313. int use_ssl = 0;
  314. #endif
  315. #if HAVE_LIBSSL
  316. /* initialize SSL */
  317. if (init_ssl (box))
  318. return 0;
  319. #endif
  320. /* open connection to IMAP server */
  321. memset (&sin, 0, sizeof (sin));
  322. sin.sin_port = htons (box->port);
  323. sin.sin_family = AF_INET;
  324. printf ("Resolving %s... ", box->host);
  325. fflush (stdout);
  326. he = gethostbyname (box->host);
  327. if (!he)
  328. {
  329. perror ("gethostbyname");
  330. return 0;
  331. }
  332. puts ("ok");
  333. sin.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
  334. s = socket (PF_INET, SOCK_STREAM, 0);
  335. printf ("Connecting to %s:%hu... ", inet_ntoa (sin.sin_addr),
  336. ntohs (sin.sin_port));
  337. fflush (stdout);
  338. if (connect (s, (struct sockaddr *) &sin, sizeof (sin)))
  339. {
  340. perror ("connect");
  341. exit (1);
  342. }
  343. puts ("ok");
  344. imap = calloc (1, sizeof (imap_t));
  345. imap->sock = calloc (1, sizeof (Socket_t));
  346. imap->sock->fd = s;
  347. imap->buf = calloc (1, sizeof (buffer_t));
  348. imap->buf->sock = imap->sock;
  349. imap->box = box;
  350. #if HAVE_LIBSSL
  351. if (!box->use_imaps)
  352. {
  353. /* always try to select SSL support if available */
  354. ret = imap_exec (imap, "STARTTLS");
  355. if (!ret)
  356. use_ssl = 1;
  357. else if (box->require_ssl)
  358. {
  359. puts ("Error, SSL support not available");
  360. return 0;
  361. }
  362. else
  363. puts ("Warning, SSL support not available");
  364. }
  365. else
  366. use_ssl = 1;
  367. if (use_ssl)
  368. {
  369. imap->sock->ssl = SSL_new (SSLContext);
  370. SSL_set_fd (imap->sock->ssl, imap->sock->fd);
  371. ret = SSL_connect (imap->sock->ssl);
  372. if (ret <= 0)
  373. {
  374. ret = SSL_get_error (imap->sock->ssl, ret);
  375. printf ("Error, SSL_connect: %s\n", ERR_error_string (ret, 0));
  376. return 0;
  377. }
  378. imap->sock->use_ssl = 1;
  379. puts ("SSL support enabled");
  380. }
  381. #endif
  382. puts ("Logging in...");
  383. ret = imap_exec (imap, "LOGIN %s %s", box->user, box->pass);
  384. if (!ret)
  385. {
  386. fputs ("Selecting mailbox... ", stdout);
  387. fflush (stdout);
  388. ret = imap_exec (imap, "SELECT %s", box->box);
  389. if (!ret)
  390. printf ("%d messages, %d recent\n", imap->count, imap->recent);
  391. }
  392. if (!ret)
  393. {
  394. if (fast)
  395. {
  396. if (imap->recent > 0)
  397. {
  398. puts ("Fetching info for recent messages");
  399. ret = imap_exec (imap, "UID SEARCH RECENT");
  400. if (!ret)
  401. ret = fetch_recent_flags (imap);
  402. }
  403. }
  404. else if (imap->count > 0)
  405. {
  406. puts ("Reading IMAP mailbox index");
  407. ret = imap_exec (imap, "FETCH 1:%d (UID FLAGS)", imap->count);
  408. }
  409. }
  410. if (ret)
  411. {
  412. imap_exec (imap, "LOGOUT");
  413. close (s);
  414. free (imap->buf);
  415. free (imap);
  416. imap = 0;
  417. }
  418. return imap;
  419. }
  420. void
  421. imap_close (imap_t * imap)
  422. {
  423. puts ("Closing IMAP connection");
  424. imap_exec (imap, "LOGOUT");
  425. }
  426. /* write a buffer stripping all \r bytes */
  427. static int
  428. write_strip (int fd, char *buf, size_t len)
  429. {
  430. size_t start = 0;
  431. size_t end = 0;
  432. while (start < len)
  433. {
  434. while (end < len && buf[end] != '\r')
  435. end++;
  436. write (fd, buf + start, end - start);
  437. end++;
  438. start = end;
  439. }
  440. return 0;
  441. }
  442. static void
  443. send_server (Socket_t * sock, const char *fmt, ...)
  444. {
  445. char buf[128];
  446. char cmd[128];
  447. va_list ap;
  448. va_start (ap, fmt);
  449. vsnprintf (buf, sizeof (buf), fmt, ap);
  450. va_end (ap);
  451. snprintf (cmd, sizeof (cmd), "%d %s\r\n", ++Tag, buf);
  452. socket_write (sock, cmd, strlen (cmd));
  453. if (Verbose)
  454. fputs (cmd, stdout);
  455. }
  456. int
  457. imap_fetch_message (imap_t * imap, unsigned int uid, int fd)
  458. {
  459. char *cmd;
  460. char *arg;
  461. size_t bytes;
  462. size_t n;
  463. char buf[1024];
  464. send_server (imap->sock, "UID FETCH %d RFC822.PEEK", uid);
  465. for (;;)
  466. {
  467. if (buffer_gets (imap->buf, &cmd))
  468. return -1;
  469. if (Verbose)
  470. puts (cmd);
  471. if (*cmd == '*')
  472. {
  473. /* need to figure out how long the message is
  474. * * <msgno> FETCH (RFC822 {<size>}
  475. */
  476. next_arg (&cmd); /* * */
  477. next_arg (&cmd); /* <msgno> */
  478. next_arg (&cmd); /* FETCH */
  479. next_arg (&cmd); /* (RFC822 */
  480. arg = next_arg (&cmd);
  481. if (*arg != '{')
  482. {
  483. puts ("parse error getting size");
  484. return -1;
  485. }
  486. bytes = strtol (arg + 1, 0, 10);
  487. // printf ("receiving %d byte message\n", bytes);
  488. /* dump whats left over in the input buffer */
  489. n = imap->buf->bytes - imap->buf->offset;
  490. if (n > bytes)
  491. {
  492. /* the entire message fit in the buffer */
  493. n = bytes;
  494. }
  495. /* ick. we have to strip out the \r\n line endings, so
  496. * i can't just dump the raw bytes to disk.
  497. */
  498. write_strip (fd, imap->buf->buf + imap->buf->offset, n);
  499. bytes -= n;
  500. // printf ("wrote %d buffered bytes\n", n);
  501. /* mark that we used part of the buffer */
  502. imap->buf->offset += n;
  503. /* now read the rest of the message */
  504. while (bytes > 0)
  505. {
  506. n = bytes;
  507. if (n > sizeof (buf))
  508. n = sizeof (buf);
  509. n = socket_read (imap->sock, buf, n);
  510. if (n > 0)
  511. {
  512. // printf("imap_fetch_message:%d:read %d bytes\n", __LINE__, n);
  513. write_strip (fd, buf, n);
  514. bytes -= n;
  515. }
  516. else
  517. {
  518. if (n == (size_t) - 1)
  519. perror ("read");
  520. else
  521. puts ("EOF");
  522. return -1;
  523. }
  524. }
  525. // puts ("finished fetching msg");
  526. buffer_gets (imap->buf, &cmd);
  527. if (Verbose)
  528. puts (cmd); /* last part of line */
  529. }
  530. else
  531. {
  532. arg = next_arg (&cmd);
  533. if (!arg || (size_t) atoi (arg) != Tag)
  534. {
  535. puts ("wrong tag");
  536. return -1;
  537. }
  538. break;
  539. }
  540. }
  541. return 0;
  542. }
  543. /* add flags to existing flags */
  544. int
  545. imap_set_flags (imap_t * imap, unsigned int uid, unsigned int flags)
  546. {
  547. char buf[256];
  548. int i;
  549. buf[0] = 0;
  550. for (i = 0; i < D_MAX; i++)
  551. {
  552. if (flags & (1 << i))
  553. snprintf (buf + strlen (buf),
  554. sizeof (buf) - strlen (buf), "%s%s",
  555. (buf[0] != 0) ? " " : "", Flags[i]);
  556. }
  557. return imap_exec (imap, "UID STORE %d +FLAGS.SILENT (%s)", uid, buf);
  558. }
  559. int
  560. imap_expunge (imap_t * imap)
  561. {
  562. return imap_exec (imap, "EXPUNGE");
  563. }