imap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. parse_fetch (imap_t * imap, list_t * list, message_t *cur)
  136. {
  137. list_t *tmp;
  138. if (!is_list (list))
  139. return -1;
  140. for (tmp = list->child; tmp; tmp = tmp->next)
  141. {
  142. if (is_atom (tmp))
  143. {
  144. if (!strcmp ("UID", tmp->val))
  145. {
  146. tmp = tmp->next;
  147. if (is_atom (tmp))
  148. cur->uid = atoi (tmp->val);
  149. else
  150. puts ("Error, unable to parse UID");
  151. }
  152. else if (!strcmp ("FLAGS", tmp->val))
  153. {
  154. tmp = tmp->next;
  155. if (is_list (tmp))
  156. {
  157. list_t *flags = tmp->child;
  158. for (; flags; flags = flags->next)
  159. {
  160. if (is_atom (flags))
  161. {
  162. if (!strcmp ("\\Seen", flags->val))
  163. cur->flags |= D_SEEN;
  164. else if (!strcmp ("\\Flagged", flags->val))
  165. cur->flags |= D_FLAGGED;
  166. else if (!strcmp ("\\Deleted", flags->val))
  167. {
  168. cur->flags |= D_DELETED;
  169. imap->deleted++;
  170. }
  171. else if (!strcmp ("\\Answered", flags->val))
  172. cur->flags |= D_ANSWERED;
  173. else if (!strcmp ("\\Draft", flags->val))
  174. cur->flags |= D_DRAFT;
  175. else if (!strcmp ("\\Recent", flags->val))
  176. cur->flags |= D_RECENT;
  177. else
  178. printf ("Warning, unknown flag %s\n",flags->val);
  179. }
  180. else
  181. puts ("Error, unable to parse FLAGS list");
  182. }
  183. }
  184. else
  185. puts ("Error, unable to parse FLAGS");
  186. }
  187. }
  188. }
  189. return 0;
  190. }
  191. static int
  192. imap_exec (imap_t * imap, const char *fmt, ...)
  193. {
  194. va_list ap;
  195. char tmp[256];
  196. char buf[256];
  197. char *cmd;
  198. char *arg;
  199. char *arg1;
  200. message_t **cur = 0;
  201. message_t **rec = 0;
  202. va_start (ap, fmt);
  203. vsnprintf (tmp, sizeof (tmp), fmt, ap);
  204. va_end (ap);
  205. snprintf (buf, sizeof (buf), "%d %s\r\n", ++Tag, tmp);
  206. if (Verbose)
  207. fputs (buf, stdout);
  208. socket_write (imap->sock, buf, strlen (buf));
  209. for (;;)
  210. {
  211. if (buffer_gets (imap->buf, &cmd))
  212. return -1;
  213. if (Verbose)
  214. puts (cmd);
  215. arg = next_arg (&cmd);
  216. if (*arg == '*')
  217. {
  218. arg = next_arg (&cmd);
  219. if (!arg)
  220. {
  221. puts ("Error, unable to parse untagged command");
  222. return -1;
  223. }
  224. if (!strcmp ("NAMESPACE", arg))
  225. {
  226. imap->ns_personal = parse_list (cmd, &cmd);
  227. imap->ns_other = parse_list (cmd, &cmd);
  228. imap->ns_shared = parse_list (cmd, 0);
  229. }
  230. else if (!strcmp ("SEARCH", arg))
  231. {
  232. if (!rec)
  233. {
  234. rec = &imap->recent_msgs;
  235. while (*rec)
  236. rec = &(*rec)->next;
  237. }
  238. /* parse rest of `cmd' */
  239. while ((arg = next_arg (&cmd)))
  240. {
  241. *rec = calloc (1, sizeof (message_t));
  242. (*rec)->uid = atoi (arg);
  243. rec = &(*rec)->next;
  244. }
  245. }
  246. else if ((arg1 = next_arg (&cmd)))
  247. {
  248. if (!strcmp ("EXISTS", arg1))
  249. imap->count = atoi (arg);
  250. else if (!strcmp ("RECENT", arg1))
  251. imap->recent = atoi (arg);
  252. else if (!strcmp ("FETCH", arg1))
  253. {
  254. list_t *list;
  255. if (!cur)
  256. {
  257. cur = &imap->msgs;
  258. while (*cur)
  259. cur = &(*cur)->next;
  260. }
  261. list = parse_list (cmd, 0);
  262. *cur = calloc (1, sizeof(message_t));
  263. if (parse_fetch (imap, list, *cur))
  264. {
  265. free_list (list);
  266. return -1;
  267. }
  268. free_list (list);
  269. cur = &(*cur)->next;
  270. }
  271. }
  272. else
  273. {
  274. puts ("Error, unable to parse untagged command");
  275. return -1;
  276. }
  277. }
  278. else if ((size_t) atol (arg) != Tag)
  279. {
  280. puts ("wrong tag");
  281. return -1;
  282. }
  283. else
  284. {
  285. arg = next_arg (&cmd);
  286. if (!strcmp ("OK", arg))
  287. return 0;
  288. return -1;
  289. }
  290. }
  291. /* not reached */
  292. }
  293. static int
  294. fetch_recent_flags (imap_t * imap)
  295. {
  296. char buf[1024];
  297. message_t **cur = &imap->recent_msgs;
  298. message_t *tmp;
  299. unsigned int start = -1;
  300. unsigned int last = -1;
  301. int ret = 0;
  302. buf[0] = 0;
  303. while (*cur)
  304. {
  305. tmp = *cur;
  306. if (last == (unsigned int) -1)
  307. {
  308. /* init */
  309. start = tmp->uid;
  310. last = tmp->uid;
  311. }
  312. else if (tmp->uid == last + 1)
  313. last++;
  314. else
  315. {
  316. /* out of sequence */
  317. if (start == last)
  318. ret = imap_exec (imap, "UID FETCH %d (UID FLAGS)", start);
  319. else
  320. ret =
  321. imap_exec (imap, "UID FETCH %d:%d (UID FLAGS)", start,
  322. last);
  323. start = tmp->uid;
  324. last = tmp->uid;
  325. }
  326. free (tmp);
  327. *cur = (*cur)->next;
  328. }
  329. if (start != (unsigned int) -1)
  330. {
  331. if (start == last)
  332. ret = imap_exec (imap, "UID FETCH %d (UID FLAGS)", start);
  333. else
  334. ret =
  335. imap_exec (imap, "UID FETCH %d:%d (UID FLAGS)", start, last);
  336. }
  337. return ret;
  338. }
  339. imap_t *
  340. imap_open (config_t * box, int fast)
  341. {
  342. int ret;
  343. imap_t *imap;
  344. int s;
  345. struct sockaddr_in sin;
  346. struct hostent *he;
  347. char *ns_prefix = 0;
  348. #if HAVE_LIBSSL
  349. int use_ssl = 0;
  350. #endif
  351. #if HAVE_LIBSSL
  352. /* initialize SSL */
  353. if (init_ssl (box))
  354. return 0;
  355. #endif
  356. /* open connection to IMAP server */
  357. memset (&sin, 0, sizeof (sin));
  358. sin.sin_port = htons (box->port);
  359. sin.sin_family = AF_INET;
  360. printf ("Resolving %s... ", box->host);
  361. fflush (stdout);
  362. he = gethostbyname (box->host);
  363. if (!he)
  364. {
  365. perror ("gethostbyname");
  366. return 0;
  367. }
  368. puts ("ok");
  369. sin.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
  370. s = socket (PF_INET, SOCK_STREAM, 0);
  371. printf ("Connecting to %s:%hu... ", inet_ntoa (sin.sin_addr),
  372. ntohs (sin.sin_port));
  373. fflush (stdout);
  374. if (connect (s, (struct sockaddr *) &sin, sizeof (sin)))
  375. {
  376. perror ("connect");
  377. exit (1);
  378. }
  379. puts ("ok");
  380. imap = calloc (1, sizeof (imap_t));
  381. imap->sock = calloc (1, sizeof (Socket_t));
  382. imap->sock->fd = s;
  383. imap->buf = calloc (1, sizeof (buffer_t));
  384. imap->buf->sock = imap->sock;
  385. imap->box = box;
  386. #if HAVE_LIBSSL
  387. if (!box->use_imaps)
  388. {
  389. /* always try to select SSL support if available */
  390. ret = imap_exec (imap, "STARTTLS");
  391. if (!ret)
  392. use_ssl = 1;
  393. else if (box->require_ssl)
  394. {
  395. puts ("Error, SSL support not available");
  396. return 0;
  397. }
  398. else
  399. puts ("Warning, SSL support not available");
  400. }
  401. else
  402. use_ssl = 1;
  403. if (use_ssl)
  404. {
  405. imap->sock->ssl = SSL_new (SSLContext);
  406. SSL_set_fd (imap->sock->ssl, imap->sock->fd);
  407. ret = SSL_connect (imap->sock->ssl);
  408. if (ret <= 0)
  409. {
  410. ret = SSL_get_error (imap->sock->ssl, ret);
  411. printf ("Error, SSL_connect: %s\n", ERR_error_string (ret, 0));
  412. return 0;
  413. }
  414. imap->sock->use_ssl = 1;
  415. puts ("SSL support enabled");
  416. }
  417. #endif
  418. puts ("Logging in...");
  419. ret = imap_exec (imap, "LOGIN %s %s", box->user, box->pass);
  420. if (!ret)
  421. {
  422. /* get NAMESPACE info */
  423. if (!imap_exec (imap, "NAMESPACE"))
  424. {
  425. /* XXX for now assume personal namespace */
  426. if (is_list (imap->ns_personal) &&
  427. is_list(imap->ns_personal->child) &&
  428. is_atom(imap->ns_personal->child->child))
  429. {
  430. ns_prefix = imap->ns_personal->child->child->val;
  431. }
  432. }
  433. }
  434. if (!ret)
  435. {
  436. fputs ("Selecting mailbox... ", stdout);
  437. fflush (stdout);
  438. ret = imap_exec (imap, "SELECT %s%s",
  439. ns_prefix ? ns_prefix : "", box->box);
  440. if (!ret)
  441. printf ("%d messages, %d recent\n", imap->count, imap->recent);
  442. }
  443. if (!ret)
  444. {
  445. if (fast)
  446. {
  447. if (imap->recent > 0)
  448. {
  449. puts ("Fetching info for recent messages");
  450. ret = imap_exec (imap, "UID SEARCH RECENT");
  451. if (!ret)
  452. ret = fetch_recent_flags (imap);
  453. }
  454. }
  455. else if (imap->count > 0)
  456. {
  457. puts ("Reading IMAP mailbox index");
  458. ret = imap_exec (imap, "FETCH 1:%d (UID FLAGS)", imap->count);
  459. }
  460. }
  461. if (ret)
  462. {
  463. imap_exec (imap, "LOGOUT");
  464. close (s);
  465. free (imap->buf);
  466. free (imap);
  467. imap = 0;
  468. }
  469. return imap;
  470. }
  471. void
  472. imap_close (imap_t * imap)
  473. {
  474. puts ("Closing IMAP connection");
  475. imap_exec (imap, "LOGOUT");
  476. }
  477. /* write a buffer stripping all \r bytes */
  478. static int
  479. write_strip (int fd, char *buf, size_t len)
  480. {
  481. size_t start = 0;
  482. size_t end = 0;
  483. while (start < len)
  484. {
  485. while (end < len && buf[end] != '\r')
  486. end++;
  487. write (fd, buf + start, end - start);
  488. end++;
  489. start = end;
  490. }
  491. return 0;
  492. }
  493. static void
  494. send_server (Socket_t * sock, const char *fmt, ...)
  495. {
  496. char buf[128];
  497. char cmd[128];
  498. va_list ap;
  499. va_start (ap, fmt);
  500. vsnprintf (buf, sizeof (buf), fmt, ap);
  501. va_end (ap);
  502. snprintf (cmd, sizeof (cmd), "%d %s\r\n", ++Tag, buf);
  503. socket_write (sock, cmd, strlen (cmd));
  504. if (Verbose)
  505. fputs (cmd, stdout);
  506. }
  507. int
  508. imap_fetch_message (imap_t * imap, unsigned int uid, int fd)
  509. {
  510. char *cmd;
  511. char *arg;
  512. size_t bytes;
  513. size_t n;
  514. char buf[1024];
  515. send_server (imap->sock, "UID FETCH %d RFC822.PEEK", uid);
  516. for (;;)
  517. {
  518. if (buffer_gets (imap->buf, &cmd))
  519. return -1;
  520. if (Verbose)
  521. puts (cmd);
  522. if (*cmd == '*')
  523. {
  524. /* need to figure out how long the message is
  525. * * <msgno> FETCH (RFC822 {<size>}
  526. */
  527. next_arg (&cmd); /* * */
  528. next_arg (&cmd); /* <msgno> */
  529. next_arg (&cmd); /* FETCH */
  530. next_arg (&cmd); /* (RFC822 */
  531. arg = next_arg (&cmd);
  532. if (*arg != '{')
  533. {
  534. puts ("parse error getting size");
  535. return -1;
  536. }
  537. bytes = strtol (arg + 1, 0, 10);
  538. // printf ("receiving %d byte message\n", bytes);
  539. /* dump whats left over in the input buffer */
  540. n = imap->buf->bytes - imap->buf->offset;
  541. if (n > bytes)
  542. {
  543. /* the entire message fit in the buffer */
  544. n = bytes;
  545. }
  546. /* ick. we have to strip out the \r\n line endings, so
  547. * i can't just dump the raw bytes to disk.
  548. */
  549. write_strip (fd, imap->buf->buf + imap->buf->offset, n);
  550. bytes -= n;
  551. // printf ("wrote %d buffered bytes\n", n);
  552. /* mark that we used part of the buffer */
  553. imap->buf->offset += n;
  554. /* now read the rest of the message */
  555. while (bytes > 0)
  556. {
  557. n = bytes;
  558. if (n > sizeof (buf))
  559. n = sizeof (buf);
  560. n = socket_read (imap->sock, buf, n);
  561. if (n > 0)
  562. {
  563. // printf("imap_fetch_message:%d:read %d bytes\n", __LINE__, n);
  564. write_strip (fd, buf, n);
  565. bytes -= n;
  566. }
  567. else
  568. {
  569. if (n == (size_t) - 1)
  570. perror ("read");
  571. else
  572. puts ("EOF");
  573. return -1;
  574. }
  575. }
  576. // puts ("finished fetching msg");
  577. buffer_gets (imap->buf, &cmd);
  578. if (Verbose)
  579. puts (cmd); /* last part of line */
  580. }
  581. else
  582. {
  583. arg = next_arg (&cmd);
  584. if (!arg || (size_t) atoi (arg) != Tag)
  585. {
  586. puts ("wrong tag");
  587. return -1;
  588. }
  589. break;
  590. }
  591. }
  592. return 0;
  593. }
  594. /* add flags to existing flags */
  595. int
  596. imap_set_flags (imap_t * imap, unsigned int uid, unsigned int flags)
  597. {
  598. char buf[256];
  599. int i;
  600. buf[0] = 0;
  601. for (i = 0; i < D_MAX; i++)
  602. {
  603. if (flags & (1 << i))
  604. snprintf (buf + strlen (buf),
  605. sizeof (buf) - strlen (buf), "%s%s",
  606. (buf[0] != 0) ? " " : "", Flags[i]);
  607. }
  608. return imap_exec (imap, "UID STORE %d +FLAGS.SILENT (%s)", uid, buf);
  609. }
  610. int
  611. imap_expunge (imap_t * imap)
  612. {
  613. return imap_exec (imap, "EXPUNGE");
  614. }