imap.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000-1 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. void
  44. free_message (message_t * msg)
  45. {
  46. message_t *tmp;
  47. while (msg)
  48. {
  49. tmp = msg;
  50. msg = msg->next;
  51. if (tmp->file)
  52. free (tmp->file);
  53. free (tmp);
  54. }
  55. }
  56. #if HAVE_LIBSSL
  57. #define MAX_DEPTH 1
  58. SSL_CTX *SSLContext = 0;
  59. /* this gets called when a certificate is to be verified */
  60. static int
  61. verify_cert (SSL * ssl)
  62. {
  63. X509 *cert;
  64. int err;
  65. char buf[256];
  66. int ret = -1;
  67. BIO *bio;
  68. cert = SSL_get_peer_certificate (ssl);
  69. if (!cert)
  70. {
  71. puts ("Error, no server certificate");
  72. return -1;
  73. }
  74. err = SSL_get_verify_result (ssl);
  75. if (err == X509_V_OK)
  76. return 0;
  77. printf ("Error, can't verify certificate: %s (%d)\n",
  78. X509_verify_cert_error_string (err), err);
  79. X509_NAME_oneline (X509_get_subject_name (cert), buf, sizeof (buf));
  80. printf ("\nSubject: %s\n", buf);
  81. X509_NAME_oneline (X509_get_issuer_name (cert), buf, sizeof (buf));
  82. printf ("Issuer: %s\n", buf);
  83. bio = BIO_new (BIO_s_mem ());
  84. ASN1_TIME_print (bio, X509_get_notBefore (cert));
  85. memset (buf, 0, sizeof (buf));
  86. BIO_read (bio, buf, sizeof (buf) - 1);
  87. printf ("Valid from: %s\n", buf);
  88. ASN1_TIME_print (bio, X509_get_notAfter (cert));
  89. memset (buf, 0, sizeof (buf));
  90. BIO_read (bio, buf, sizeof (buf) - 1);
  91. BIO_free (bio);
  92. printf (" to: %s\n", buf);
  93. printf
  94. ("\n*** WARNING *** There is no way to verify this certificate. It is\n"
  95. " possible that a hostile attacker has replaced the\n"
  96. " server certificate. Continue at your own risk!\n");
  97. printf ("\nAccept this certificate anyway? [no]: ");
  98. fflush (stdout);
  99. if (fgets (buf, sizeof (buf), stdin) && (buf[0] == 'y' || buf[0] == 'Y'))
  100. {
  101. ret = 0;
  102. puts ("\n*** Fine, but don't say I didn't warn you!\n");
  103. }
  104. return ret;
  105. }
  106. static int
  107. init_ssl (config_t * conf)
  108. {
  109. SSL_METHOD *method;
  110. int options = 0;
  111. if (!conf->cert_file)
  112. {
  113. puts ("Error, CertificateFile not defined");
  114. return -1;
  115. }
  116. SSL_library_init ();
  117. SSL_load_error_strings ();
  118. if (conf->use_tlsv1 && !conf->use_sslv2 && !conf->use_sslv3)
  119. method = TLSv1_client_method ();
  120. else
  121. method = SSLv23_client_method ();
  122. SSLContext = SSL_CTX_new (method);
  123. if (access (conf->cert_file, F_OK))
  124. {
  125. if (errno != ENOENT)
  126. {
  127. perror ("access");
  128. return -1;
  129. }
  130. puts
  131. ("*** Warning, CertificateFile doesn't exist, can't verify server certificates");
  132. }
  133. else
  134. if (!SSL_CTX_load_verify_locations
  135. (SSLContext, conf->cert_file, NULL))
  136. {
  137. printf ("Error, SSL_CTX_load_verify_locations: %s\n",
  138. ERR_error_string (ERR_get_error (), 0));
  139. return -1;
  140. }
  141. if (!conf->use_sslv2)
  142. options |= SSL_OP_NO_SSLv2;
  143. if (!conf->use_sslv3)
  144. options |= SSL_OP_NO_SSLv3;
  145. if (!conf->use_tlsv1)
  146. options |= SSL_OP_NO_TLSv1;
  147. SSL_CTX_set_options (SSLContext, options);
  148. /* we check the result of the verification after SSL_connect() */
  149. SSL_CTX_set_verify (SSLContext, SSL_VERIFY_NONE, 0);
  150. return 0;
  151. }
  152. #endif /* HAVE_LIBSSL */
  153. static int
  154. socket_read (Socket_t * sock, char *buf, size_t len)
  155. {
  156. #if HAVE_LIBSSL
  157. if (sock->use_ssl)
  158. return SSL_read (sock->ssl, buf, len);
  159. #endif
  160. return read (sock->fd, buf, len);
  161. }
  162. static int
  163. socket_write (Socket_t * sock, char *buf, size_t len)
  164. {
  165. #if HAVE_LIBSSL
  166. if (sock->use_ssl)
  167. return SSL_write (sock->ssl, buf, len);
  168. #endif
  169. return write (sock->fd, buf, len);
  170. }
  171. static void
  172. socket_perror (const char *func, Socket_t *sock, int ret)
  173. {
  174. #if HAVE_LIBSSL
  175. int err;
  176. if (sock->use_ssl)
  177. {
  178. switch ((err = SSL_get_error (sock->ssl, ret)))
  179. {
  180. case SSL_ERROR_SYSCALL:
  181. case SSL_ERROR_SSL:
  182. if ((err = ERR_get_error ()) == 0)
  183. {
  184. if (ret == 0)
  185. fprintf (stderr, "SSL_%s:got EOF\n", func);
  186. else
  187. fprintf (stderr, "SSL_%s:%d:%s\n", func,
  188. errno, strerror (errno));
  189. }
  190. else
  191. fprintf (stderr, "SSL_%s:%d:%s\n", func, err,
  192. ERR_error_string (err, 0));
  193. return;
  194. default:
  195. fprintf (stderr, "SSL_%s:%d:unhandled SSL error\n", func, err);
  196. break;
  197. }
  198. return;
  199. }
  200. #else
  201. (void) sock;
  202. (void) ret;
  203. #endif
  204. perror (func);
  205. }
  206. /* simple line buffering */
  207. static int
  208. buffer_gets (buffer_t * b, char **s)
  209. {
  210. int n;
  211. int start = b->offset;
  212. *s = b->buf + start;
  213. for (;;)
  214. {
  215. /* make sure we have enough data to read the \r\n sequence */
  216. if (b->offset + 1 >= b->bytes)
  217. {
  218. if (start != 0)
  219. {
  220. /* shift down used bytes */
  221. *s = b->buf;
  222. assert (start <= b->bytes);
  223. n = b->bytes - start;
  224. if (n)
  225. memmove (b->buf, b->buf + start, n);
  226. b->offset -= start;
  227. b->bytes = n;
  228. start = 0;
  229. }
  230. n =
  231. socket_read (b->sock, b->buf + b->bytes,
  232. sizeof (b->buf) - b->bytes);
  233. if (n <= 0)
  234. {
  235. socket_perror ("read", b->sock, n);
  236. return -1;
  237. }
  238. b->bytes += n;
  239. }
  240. if (b->buf[b->offset] == '\r')
  241. {
  242. assert (b->offset + 1 < b->bytes);
  243. if (b->buf[b->offset + 1] == '\n')
  244. {
  245. b->buf[b->offset] = 0; /* terminate the string */
  246. b->offset += 2; /* next line */
  247. // assert (strchr (*s, '\r') == 0);
  248. return 0;
  249. }
  250. }
  251. b->offset++;
  252. }
  253. /* not reached */
  254. }
  255. static int
  256. parse_fetch (imap_t * imap, list_t * list)
  257. {
  258. list_t *tmp;
  259. unsigned int uid = 0;
  260. unsigned int mask = 0;
  261. unsigned int size = 0;
  262. message_t *cur;
  263. if (!is_list (list))
  264. return -1;
  265. for (tmp = list->child; tmp; tmp = tmp->next)
  266. {
  267. if (is_atom (tmp))
  268. {
  269. if (!strcmp ("UID", tmp->val))
  270. {
  271. tmp = tmp->next;
  272. if (is_atom (tmp))
  273. {
  274. uid = atoi (tmp->val);
  275. if (uid < imap->minuid)
  276. {
  277. /* already saw this message */
  278. return 0;
  279. }
  280. else if (uid > imap->maxuid)
  281. imap->maxuid = uid;
  282. }
  283. else
  284. puts ("Error, unable to parse UID");
  285. }
  286. else if (!strcmp ("FLAGS", tmp->val))
  287. {
  288. tmp = tmp->next;
  289. if (is_list (tmp))
  290. {
  291. list_t *flags = tmp->child;
  292. for (; flags; flags = flags->next)
  293. {
  294. if (is_atom (flags))
  295. {
  296. if (!strcmp ("\\Seen", flags->val))
  297. mask |= D_SEEN;
  298. else if (!strcmp ("\\Flagged", flags->val))
  299. mask |= D_FLAGGED;
  300. else if (!strcmp ("\\Deleted", flags->val))
  301. mask |= D_DELETED;
  302. else if (!strcmp ("\\Answered", flags->val))
  303. mask |= D_ANSWERED;
  304. else if (!strcmp ("\\Draft", flags->val))
  305. mask |= D_DRAFT;
  306. else if (!strcmp ("\\Recent", flags->val))
  307. mask |= D_RECENT;
  308. else
  309. printf ("Warning, unknown flag %s\n",
  310. flags->val);
  311. }
  312. else
  313. puts ("Error, unable to parse FLAGS list");
  314. }
  315. }
  316. else
  317. puts ("Error, unable to parse FLAGS");
  318. }
  319. else if (!strcmp ("RFC822.SIZE", tmp->val))
  320. {
  321. tmp = tmp->next;
  322. if (is_atom (tmp))
  323. size = atol (tmp->val);
  324. }
  325. }
  326. }
  327. cur = calloc (1, sizeof (message_t));
  328. cur->next = imap->msgs;
  329. imap->msgs = cur;
  330. if (mask & D_DELETED)
  331. imap->deleted++;
  332. cur->uid = uid;
  333. cur->flags = mask;
  334. cur->size = size;
  335. return 0;
  336. }
  337. static void
  338. parse_response_code (imap_t * imap, char *s)
  339. {
  340. char *arg;
  341. if (*s != '[')
  342. return; /* no response code */
  343. s++;
  344. arg = next_arg (&s);
  345. if (!strcmp ("UIDVALIDITY", arg))
  346. {
  347. arg = next_arg (&s);
  348. imap->uidvalidity = atol (arg);
  349. }
  350. else if (!strcmp ("ALERT", arg))
  351. {
  352. /* RFC2060 says that these messages MUST be displayed
  353. * to the user
  354. */
  355. fputs ("***ALERT*** ", stdout);
  356. puts (s);
  357. }
  358. }
  359. static int
  360. imap_exec (imap_t * imap, const char *fmt, ...)
  361. {
  362. va_list ap;
  363. char tmp[256];
  364. char buf[256];
  365. char *cmd;
  366. char *arg;
  367. char *arg1;
  368. int n;
  369. va_start (ap, fmt);
  370. vsnprintf (tmp, sizeof (tmp), fmt, ap);
  371. va_end (ap);
  372. snprintf (buf, sizeof (buf), "%d %s\r\n", ++Tag, tmp);
  373. if (Verbose)
  374. {
  375. fputs (">>> ", stdout);
  376. fputs (buf, stdout);
  377. }
  378. n = socket_write (imap->sock, buf, strlen (buf));
  379. if (n <= 0)
  380. {
  381. socket_perror ("write", imap->sock, n);
  382. return -1;
  383. }
  384. for (;;)
  385. {
  386. if (buffer_gets (imap->buf, &cmd))
  387. return -1;
  388. if (Verbose)
  389. puts (cmd);
  390. arg = next_arg (&cmd);
  391. if (*arg == '*')
  392. {
  393. arg = next_arg (&cmd);
  394. if (!arg)
  395. {
  396. puts ("Error, unable to parse untagged command");
  397. return -1;
  398. }
  399. if (!strcmp ("NAMESPACE", arg))
  400. {
  401. imap->ns_personal = parse_list (cmd, &cmd);
  402. imap->ns_other = parse_list (cmd, &cmd);
  403. imap->ns_shared = parse_list (cmd, 0);
  404. }
  405. else if (!strcmp ("OK", arg) || !strcmp ("BAD", arg) ||
  406. !strcmp ("NO", arg) || !strcmp ("PREAUTH", arg) ||
  407. !strcmp ("BYE", arg))
  408. {
  409. parse_response_code (imap, cmd);
  410. }
  411. else if (!strcmp ("CAPABILITY", arg))
  412. {
  413. #if HAVE_LIBSSL
  414. while ((arg = next_arg (&cmd)))
  415. {
  416. if (!strcmp ("STARTTLS", arg))
  417. imap->have_starttls = 1;
  418. else if (!strcmp ("AUTH=CRAM-MD5", arg))
  419. imap->have_cram = 1;
  420. else if (!strcmp ("NAMESPACE", arg))
  421. imap->have_namespace = 1;
  422. }
  423. #endif
  424. }
  425. else if ((arg1 = next_arg (&cmd)))
  426. {
  427. if (!strcmp ("EXISTS", arg1))
  428. imap->count = atoi (arg);
  429. else if (!strcmp ("RECENT", arg1))
  430. imap->recent = atoi (arg);
  431. else if (!strcmp ("FETCH", arg1))
  432. {
  433. list_t *list;
  434. list = parse_list (cmd, 0);
  435. if (parse_fetch (imap, list))
  436. {
  437. free_list (list);
  438. return -1;
  439. }
  440. free_list (list);
  441. }
  442. }
  443. else
  444. {
  445. puts ("Error, unable to parse untagged command");
  446. return -1;
  447. }
  448. }
  449. #if HAVE_LIBSSL
  450. else if (*arg == '+')
  451. {
  452. char *resp;
  453. if (!imap->cram)
  454. {
  455. puts ("Error, not doing CRAM-MD5 authentication");
  456. return -1;
  457. }
  458. resp = cram (cmd, imap->box->user, imap->box->pass);
  459. n = socket_write (imap->sock, resp, strlen (resp));
  460. if (n <= 0)
  461. {
  462. socket_perror ("write", imap->sock, n);
  463. return -1;
  464. }
  465. if (Verbose)
  466. puts (resp);
  467. n = socket_write (imap->sock, "\r\n", 2);
  468. if (n <= 0)
  469. {
  470. socket_perror ("write", imap->sock, n);
  471. return -1;
  472. }
  473. free (resp);
  474. imap->cram = 0;
  475. }
  476. #endif
  477. else if ((size_t) atol (arg) != Tag)
  478. {
  479. puts ("wrong tag");
  480. return -1;
  481. }
  482. else
  483. {
  484. arg = next_arg (&cmd);
  485. parse_response_code (imap, cmd);
  486. if (!strcmp ("OK", arg))
  487. return 0;
  488. return -1;
  489. }
  490. }
  491. /* not reached */
  492. }
  493. /* `box' is the config info for the maildrop to sync. `minuid' is the
  494. * minimum UID to consider. in normal mode this will be 1, but in --fast
  495. * mode we only fetch messages newer than the last one seen in the local
  496. * mailbox.
  497. */
  498. imap_t *
  499. imap_open (config_t * box, unsigned int minuid, imap_t * imap)
  500. {
  501. int ret;
  502. int s;
  503. struct sockaddr_in sin;
  504. struct hostent *he;
  505. int reuse = 0;
  506. #if HAVE_LIBSSL
  507. int use_ssl = 0;
  508. #endif
  509. if (imap)
  510. {
  511. /* determine whether or not we can reuse the existing session */
  512. if (strcmp (box->host, imap->box->host) ||
  513. strcmp (box->user, imap->box->user) ||
  514. box->port != imap->box->port
  515. #if HAVE_LIBSSL
  516. /* ensure that security requirements are met */
  517. || (box->require_ssl ^ imap->box->require_ssl)
  518. || (box->require_cram ^ imap->box->require_cram)
  519. #endif
  520. )
  521. {
  522. /* can't reuse */
  523. imap_close (imap);
  524. imap = 0;
  525. }
  526. else
  527. {
  528. reuse = 1;
  529. /* reset mailbox-specific state info */
  530. imap->recent = 0;
  531. imap->deleted = 0;
  532. imap->count = 0;
  533. imap->maxuid = 0;
  534. free_message (imap->msgs);
  535. imap->msgs = 0;
  536. }
  537. }
  538. if (!imap)
  539. {
  540. imap = calloc (1, sizeof (imap_t));
  541. imap->sock = calloc (1, sizeof (Socket_t));
  542. imap->buf = calloc (1, sizeof (buffer_t));
  543. imap->buf->sock = imap->sock;
  544. }
  545. imap->box = box;
  546. imap->minuid = minuid;
  547. imap->prefix = "";
  548. if (!reuse)
  549. {
  550. /* open connection to IMAP server */
  551. memset (&sin, 0, sizeof (sin));
  552. sin.sin_port = htons (box->port);
  553. sin.sin_family = AF_INET;
  554. printf ("Resolving %s... ", box->host);
  555. fflush (stdout);
  556. he = gethostbyname (box->host);
  557. if (!he)
  558. {
  559. perror ("gethostbyname");
  560. return 0;
  561. }
  562. puts ("ok");
  563. sin.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
  564. s = socket (PF_INET, SOCK_STREAM, 0);
  565. printf ("Connecting to %s:%hu... ", inet_ntoa (sin.sin_addr),
  566. ntohs (sin.sin_port));
  567. fflush (stdout);
  568. if (connect (s, (struct sockaddr *) &sin, sizeof (sin)))
  569. {
  570. perror ("connect");
  571. exit (1);
  572. }
  573. puts ("ok");
  574. imap->sock->fd = s;
  575. }
  576. do
  577. {
  578. /* if we are reusing the existing connection, we can skip the
  579. * authentication steps.
  580. */
  581. if (!reuse)
  582. {
  583. #if HAVE_LIBSSL
  584. if (box->use_imaps)
  585. use_ssl = 1;
  586. else
  587. {
  588. /* let's see what this puppy can do... */
  589. if ((ret = imap_exec (imap, "CAPABILITY")))
  590. break;
  591. if (box->use_sslv2 || box->use_sslv3 || box->use_tlsv1)
  592. {
  593. /* always try to select SSL support if available */
  594. if (imap->have_starttls)
  595. {
  596. if ((ret = imap_exec (imap, "STARTTLS")))
  597. break;
  598. use_ssl = 1;
  599. }
  600. }
  601. }
  602. if (!use_ssl)
  603. {
  604. if (box->require_ssl)
  605. {
  606. puts ("Error, SSL support not available");
  607. ret = -1;
  608. break;
  609. }
  610. else
  611. puts ("Warning, SSL support not available");
  612. }
  613. else
  614. {
  615. /* initialize SSL */
  616. if (init_ssl (box))
  617. {
  618. ret = -1;
  619. break;
  620. }
  621. imap->sock->ssl = SSL_new (SSLContext);
  622. SSL_set_fd (imap->sock->ssl, imap->sock->fd);
  623. ret = SSL_connect (imap->sock->ssl);
  624. if (ret <= 0)
  625. {
  626. socket_perror ("connect", imap->sock, ret);
  627. break;
  628. }
  629. /* verify the server certificate */
  630. if ((ret = verify_cert (imap->sock->ssl)))
  631. break;
  632. /* to conform to RFC2595 we need to forget all information
  633. * retrieved from CAPABILITY invocations before STARTTLS.
  634. */
  635. imap->have_namespace = 0;
  636. imap->have_cram = 0;
  637. imap->have_starttls = 0;
  638. imap->sock->use_ssl = 1;
  639. puts ("SSL support enabled");
  640. if ((ret = imap_exec (imap, "CAPABILITY")))
  641. break;
  642. }
  643. #else
  644. if ((ret = imap_exec (imap, "CAPABILITY")))
  645. break;
  646. #endif
  647. puts ("Logging in...");
  648. #if HAVE_LIBSSL
  649. if (imap->have_cram)
  650. {
  651. puts ("Authenticating with CRAM-MD5");
  652. imap->cram = 1;
  653. if ((ret = imap_exec (imap, "AUTHENTICATE CRAM-MD5")))
  654. break;
  655. }
  656. else if (imap->box->require_cram)
  657. {
  658. puts
  659. ("Error, CRAM-MD5 authentication is not supported by server");
  660. ret = -1;
  661. break;
  662. }
  663. else
  664. #endif
  665. {
  666. #if HAVE_LIBSSL
  667. if (!use_ssl)
  668. #endif
  669. puts
  670. ("*** Warning *** Password is being sent in the clear");
  671. if (
  672. (ret =
  673. imap_exec (imap, "LOGIN \"%s\" \"%s\"", box->user,
  674. box->pass)))
  675. {
  676. puts ("Error, LOGIN failed");
  677. break;
  678. }
  679. }
  680. /* get NAMESPACE info */
  681. if (box->use_namespace && imap->have_namespace)
  682. {
  683. if ((ret = imap_exec (imap, "NAMESPACE")))
  684. break;
  685. }
  686. } /* !reuse */
  687. /* XXX for now assume personal namespace */
  688. if (imap->box->use_namespace && is_list (imap->ns_personal) &&
  689. is_list (imap->ns_personal->child) &&
  690. is_atom (imap->ns_personal->child->child))
  691. {
  692. imap->prefix = imap->ns_personal->child->child->val;
  693. }
  694. fputs ("Selecting mailbox... ", stdout);
  695. fflush (stdout);
  696. if (
  697. (ret =
  698. imap_exec (imap, "SELECT \"%s%s\"", imap->prefix, box->box)))
  699. break;
  700. printf ("%d messages, %d recent\n", imap->count, imap->recent);
  701. puts ("Reading IMAP mailbox index");
  702. if (imap->count > 0)
  703. {
  704. if ((ret = imap_exec (imap, "UID FETCH %d:* (FLAGS RFC822.SIZE)",
  705. imap->minuid)))
  706. break;
  707. }
  708. }
  709. while (0);
  710. if (ret)
  711. {
  712. imap_close (imap);
  713. imap = 0;
  714. }
  715. return imap;
  716. }
  717. void
  718. imap_close (imap_t * imap)
  719. {
  720. if (imap)
  721. {
  722. imap_exec (imap, "LOGOUT");
  723. close (imap->sock->fd);
  724. free (imap->sock);
  725. free (imap->buf);
  726. free_message (imap->msgs);
  727. memset (imap, 0xff, sizeof (imap_t));
  728. free (imap);
  729. }
  730. }
  731. /* write a buffer stripping all \r bytes */
  732. static int
  733. write_strip (int fd, char *buf, size_t len)
  734. {
  735. size_t start = 0;
  736. size_t end = 0;
  737. while (start < len)
  738. {
  739. while (end < len && buf[end] != '\r')
  740. end++;
  741. write (fd, buf + start, end - start);
  742. end++;
  743. start = end;
  744. }
  745. return 0;
  746. }
  747. static int
  748. send_server (Socket_t * sock, const char *fmt, ...)
  749. {
  750. char buf[128];
  751. char cmd[128];
  752. va_list ap;
  753. int n;
  754. va_start (ap, fmt);
  755. vsnprintf (buf, sizeof (buf), fmt, ap);
  756. va_end (ap);
  757. snprintf (cmd, sizeof (cmd), "%d %s\r\n", ++Tag, buf);
  758. n = socket_write (sock, cmd, strlen (cmd));
  759. if (n <= 0)
  760. {
  761. socket_perror ("write", sock, n);
  762. return -1;
  763. }
  764. if (Verbose)
  765. fputs (cmd, stdout);
  766. return 0;
  767. }
  768. int
  769. imap_fetch_message (imap_t * imap, unsigned int uid, int fd)
  770. {
  771. char *cmd;
  772. char *arg;
  773. size_t bytes;
  774. size_t n;
  775. char buf[1024];
  776. send_server (imap->sock, "UID FETCH %d BODY.PEEK[]", uid);
  777. for (;;)
  778. {
  779. if (buffer_gets (imap->buf, &cmd))
  780. return -1;
  781. if (Verbose)
  782. puts (cmd);
  783. if (*cmd == '*')
  784. {
  785. /* need to figure out how long the message is
  786. * * <msgno> FETCH (RFC822 {<size>}
  787. */
  788. next_arg (&cmd); /* * */
  789. next_arg (&cmd); /* <msgno> */
  790. arg = next_arg (&cmd); /* FETCH */
  791. if (strcasecmp ("FETCH", arg) != 0)
  792. {
  793. /* this is likely an untagged response, such as when new
  794. * mail arrives in the middle of the session. just skip
  795. * it for now.
  796. *
  797. * eg.,
  798. * "* 4000 EXISTS"
  799. * "* 2 RECENT"
  800. *
  801. */
  802. printf ("skipping untagged response: %s\n", arg);
  803. continue;
  804. }
  805. while ((arg = next_arg (&cmd)) && *arg != '{')
  806. ;
  807. if (!arg)
  808. {
  809. puts ("parse error getting size");
  810. return -1;
  811. }
  812. bytes = strtol (arg + 1, 0, 10);
  813. // printf ("receiving %d byte message\n", bytes);
  814. /* dump whats left over in the input buffer */
  815. n = imap->buf->bytes - imap->buf->offset;
  816. if (n > bytes)
  817. {
  818. /* the entire message fit in the buffer */
  819. n = bytes;
  820. }
  821. /* ick. we have to strip out the \r\n line endings, so
  822. * i can't just dump the raw bytes to disk.
  823. */
  824. write_strip (fd, imap->buf->buf + imap->buf->offset, n);
  825. bytes -= n;
  826. // printf ("wrote %d buffered bytes\n", n);
  827. /* mark that we used part of the buffer */
  828. imap->buf->offset += n;
  829. /* now read the rest of the message */
  830. while (bytes > 0)
  831. {
  832. n = bytes;
  833. if (n > sizeof (buf))
  834. n = sizeof (buf);
  835. n = socket_read (imap->sock, buf, n);
  836. if (n > 0)
  837. {
  838. // printf("imap_fetch_message:%d:read %d bytes\n", __LINE__, n);
  839. write_strip (fd, buf, n);
  840. bytes -= n;
  841. }
  842. else
  843. {
  844. socket_perror ("read", imap->sock, n);
  845. return -1;
  846. }
  847. }
  848. // puts ("finished fetching msg");
  849. buffer_gets (imap->buf, &cmd);
  850. if (Verbose)
  851. puts (cmd); /* last part of line */
  852. }
  853. else
  854. {
  855. arg = next_arg (&cmd);
  856. if (!arg || (size_t) atoi (arg) != Tag)
  857. {
  858. puts ("wrong tag");
  859. return -1;
  860. }
  861. arg = next_arg (&cmd);
  862. if (!strcmp ("OK", arg))
  863. return 0;
  864. return -1;
  865. }
  866. }
  867. /* not reached */
  868. }
  869. /* add flags to existing flags */
  870. int
  871. imap_set_flags (imap_t * imap, unsigned int uid, unsigned int flags)
  872. {
  873. char buf[256];
  874. int i;
  875. buf[0] = 0;
  876. for (i = 0; i < D_MAX; i++)
  877. {
  878. if (flags & (1 << i))
  879. snprintf (buf + strlen (buf),
  880. sizeof (buf) - strlen (buf), "%s%s",
  881. (buf[0] != 0) ? " " : "", Flags[i]);
  882. }
  883. return imap_exec (imap, "UID STORE %d +FLAGS.SILENT (%s)", uid, buf);
  884. }
  885. int
  886. imap_expunge (imap_t * imap)
  887. {
  888. return imap_exec (imap, "EXPUNGE");
  889. }
  890. int
  891. imap_copy_message (imap_t * imap, unsigned int uid, const char *mailbox)
  892. {
  893. return imap_exec (imap, "UID COPY %u \"%s%s\"", uid, imap->prefix,
  894. mailbox);
  895. }
  896. int
  897. imap_append_message (imap_t * imap, int fd, message_t * msg)
  898. {
  899. char buf[1024];
  900. size_t len;
  901. size_t sofar = 0;
  902. int lines = 0;
  903. char flagstr[128];
  904. char *s;
  905. size_t i;
  906. size_t start, end;
  907. char *arg;
  908. /* ugh, we need to count the number of newlines */
  909. while (sofar < msg->size)
  910. {
  911. len = msg->size - sofar;
  912. if (len > sizeof (buf))
  913. len = sizeof (buf);
  914. len = read (fd, buf, len);
  915. if (len == (size_t) - 1)
  916. {
  917. perror ("read");
  918. return -1;
  919. }
  920. for (i = 0; i < len; i++)
  921. if (buf[i] == '\n')
  922. lines++;
  923. sofar += len;
  924. }
  925. flagstr[0] = 0;
  926. if (msg->flags)
  927. {
  928. strcpy (flagstr, "(");
  929. if (msg->flags & D_DELETED)
  930. snprintf (flagstr + strlen (flagstr),
  931. sizeof (flagstr) - strlen (flagstr), "%s\\Deleted",
  932. flagstr[1] ? " " : "");
  933. if (msg->flags & D_ANSWERED)
  934. snprintf (flagstr + strlen (flagstr),
  935. sizeof (flagstr) - strlen (flagstr), "%s\\Answered",
  936. flagstr[1] ? " " : "");
  937. if (msg->flags & D_SEEN)
  938. snprintf (flagstr + strlen (flagstr),
  939. sizeof (flagstr) - strlen (flagstr), "%s\\Seen",
  940. flagstr[1] ? " " : "");
  941. if (msg->flags & D_FLAGGED)
  942. snprintf (flagstr + strlen (flagstr),
  943. sizeof (flagstr) - strlen (flagstr), "%s\\Flagged",
  944. flagstr[1] ? " " : "");
  945. if (msg->flags & D_DRAFT)
  946. snprintf (flagstr + strlen (flagstr),
  947. sizeof (flagstr) - strlen (flagstr), "%s\\Draft",
  948. flagstr[1] ? " " : "");
  949. snprintf (flagstr + strlen (flagstr),
  950. sizeof (flagstr) - strlen (flagstr), ") ");
  951. }
  952. send_server (imap->sock, "APPEND %s%s %s{%d}",
  953. imap->prefix, imap->box->box, flagstr, msg->size + lines);
  954. if (buffer_gets (imap->buf, &s))
  955. return -1;
  956. if (Verbose)
  957. puts (s);
  958. if (*s != '+')
  959. {
  960. puts ("Error, expected `+' from server (aborting)");
  961. return -1;
  962. }
  963. /* rewind */
  964. lseek (fd, 0, 0);
  965. sofar = 0;
  966. while (sofar < msg->size)
  967. {
  968. len = msg->size - sofar;
  969. if (len > sizeof (buf))
  970. len = sizeof (buf);
  971. len = read (fd, buf, len);
  972. if (len == (size_t) - 1)
  973. return -1;
  974. start = 0;
  975. while (start < len)
  976. {
  977. end = start;
  978. while (end < len && buf[end] != '\n')
  979. end++;
  980. if (start != end)
  981. socket_write (imap->sock, buf + start, end - start);
  982. /* only send a crlf if we actually hit the end of a line. we
  983. * might be in the middle of a line in which case we don't
  984. * send one.
  985. */
  986. if (end != len)
  987. socket_write (imap->sock, "\r\n", 2);
  988. start = end + 1;
  989. }
  990. sofar += len;
  991. }
  992. socket_write (imap->sock, "\r\n", 2);
  993. for (;;)
  994. {
  995. if (buffer_gets (imap->buf, &s))
  996. return -1;
  997. if (Verbose)
  998. puts (s);
  999. arg = next_arg (&s);
  1000. if (*arg == '*')
  1001. {
  1002. /* XXX just ignore it for now */
  1003. }
  1004. else if (atoi (arg) != (int) Tag)
  1005. {
  1006. puts ("wrong tag");
  1007. return -1;
  1008. }
  1009. else
  1010. {
  1011. int uid;
  1012. arg = next_arg (&s);
  1013. if (strcmp (arg, "OK"))
  1014. return -1;
  1015. arg = next_arg (&s);
  1016. if (*arg != '[')
  1017. break;
  1018. arg++;
  1019. if (strcasecmp ("APPENDUID", arg))
  1020. {
  1021. puts ("Error, expected APPENDUID");
  1022. break;
  1023. }
  1024. arg = next_arg (&s);
  1025. if (!arg)
  1026. break;
  1027. if (atoi (arg) != (int) imap->uidvalidity)
  1028. {
  1029. puts ("Error, UIDVALIDITY doesn't match APPENDUID");
  1030. return -1;
  1031. }
  1032. arg = next_arg (&s);
  1033. if (!arg)
  1034. break;
  1035. uid = strtol (arg, &s, 10);
  1036. if (*s != ']')
  1037. {
  1038. /* parse error */
  1039. break;
  1040. }
  1041. return uid;
  1042. }
  1043. }
  1044. return 0;
  1045. }