imap.c 22 KB

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