imap.c 13 KB

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