imap.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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. fputs (buf, stdout);
  375. n = socket_write (imap->sock, buf, strlen (buf));
  376. if (n <= 0)
  377. {
  378. socket_perror ("write", imap->sock, n);
  379. return -1;
  380. }
  381. for (;;)
  382. {
  383. if (buffer_gets (imap->buf, &cmd))
  384. return -1;
  385. if (Verbose)
  386. puts (cmd);
  387. arg = next_arg (&cmd);
  388. if (*arg == '*')
  389. {
  390. arg = next_arg (&cmd);
  391. if (!arg)
  392. {
  393. puts ("Error, unable to parse untagged command");
  394. return -1;
  395. }
  396. if (!strcmp ("NAMESPACE", arg))
  397. {
  398. imap->ns_personal = parse_list (cmd, &cmd);
  399. imap->ns_other = parse_list (cmd, &cmd);
  400. imap->ns_shared = parse_list (cmd, 0);
  401. }
  402. else if (!strcmp ("OK", arg) || !strcmp ("BAD", arg) ||
  403. !strcmp ("NO", arg) || !strcmp ("PREAUTH", arg) ||
  404. !strcmp ("BYE", arg))
  405. {
  406. parse_response_code (imap, cmd);
  407. }
  408. else if (!strcmp ("CAPABILITY", arg))
  409. {
  410. #if HAVE_LIBSSL
  411. while ((arg = next_arg (&cmd)))
  412. {
  413. if (!strcmp ("STARTTLS", arg))
  414. imap->have_starttls = 1;
  415. else if (!strcmp ("AUTH=CRAM-MD5", arg))
  416. imap->have_cram = 1;
  417. else if (!strcmp ("NAMESPACE", arg))
  418. imap->have_namespace = 1;
  419. }
  420. #endif
  421. }
  422. else if ((arg1 = next_arg (&cmd)))
  423. {
  424. if (!strcmp ("EXISTS", arg1))
  425. imap->count = atoi (arg);
  426. else if (!strcmp ("RECENT", arg1))
  427. imap->recent = atoi (arg);
  428. else if (!strcmp ("FETCH", arg1))
  429. {
  430. list_t *list;
  431. list = parse_list (cmd, 0);
  432. if (parse_fetch (imap, list))
  433. {
  434. free_list (list);
  435. return -1;
  436. }
  437. free_list (list);
  438. }
  439. }
  440. else
  441. {
  442. puts ("Error, unable to parse untagged command");
  443. return -1;
  444. }
  445. }
  446. #if HAVE_LIBSSL
  447. else if (*arg == '+')
  448. {
  449. char *resp;
  450. if (!imap->cram)
  451. {
  452. puts ("Error, not doing CRAM-MD5 authentication");
  453. return -1;
  454. }
  455. resp = cram (cmd, imap->box->user, imap->box->pass);
  456. n = socket_write (imap->sock, resp, strlen (resp));
  457. if (n <= 0)
  458. {
  459. socket_perror ("write", imap->sock, n);
  460. return -1;
  461. }
  462. if (Verbose)
  463. puts (resp);
  464. n = socket_write (imap->sock, "\r\n", 2);
  465. if (n <= 0)
  466. {
  467. socket_perror ("write", imap->sock, n);
  468. return -1;
  469. }
  470. free (resp);
  471. imap->cram = 0;
  472. }
  473. #endif
  474. else if ((size_t) atol (arg) != Tag)
  475. {
  476. puts ("wrong tag");
  477. return -1;
  478. }
  479. else
  480. {
  481. arg = next_arg (&cmd);
  482. parse_response_code (imap, cmd);
  483. if (!strcmp ("OK", arg))
  484. return 0;
  485. return -1;
  486. }
  487. }
  488. /* not reached */
  489. }
  490. /* `box' is the config info for the maildrop to sync. `minuid' is the
  491. * minimum UID to consider. in normal mode this will be 1, but in --fast
  492. * mode we only fetch messages newer than the last one seen in the local
  493. * mailbox.
  494. */
  495. imap_t *
  496. imap_open (config_t * box, unsigned int minuid, imap_t * imap)
  497. {
  498. int ret;
  499. int s;
  500. struct sockaddr_in sin;
  501. struct hostent *he;
  502. int reuse = 0;
  503. #if HAVE_LIBSSL
  504. int use_ssl = 0;
  505. #endif
  506. if (imap)
  507. {
  508. /* determine whether or not we can reuse the existing session */
  509. if (strcmp (box->host, imap->box->host) ||
  510. strcmp (box->user, imap->box->user) ||
  511. box->port != imap->box->port
  512. #if HAVE_LIBSSL
  513. /* ensure that security requirements are met */
  514. || (box->require_ssl ^ imap->box->require_ssl)
  515. || (box->require_cram ^ imap->box->require_cram)
  516. #endif
  517. )
  518. {
  519. /* can't reuse */
  520. imap_close (imap);
  521. imap = 0;
  522. }
  523. else
  524. {
  525. reuse = 1;
  526. /* reset mailbox-specific state info */
  527. imap->recent = 0;
  528. imap->deleted = 0;
  529. imap->count = 0;
  530. imap->maxuid = 0;
  531. free_message (imap->msgs);
  532. imap->msgs = 0;
  533. }
  534. }
  535. if (!imap)
  536. {
  537. imap = calloc (1, sizeof (imap_t));
  538. imap->sock = calloc (1, sizeof (Socket_t));
  539. imap->buf = calloc (1, sizeof (buffer_t));
  540. imap->buf->sock = imap->sock;
  541. }
  542. imap->box = box;
  543. imap->minuid = minuid;
  544. imap->prefix = "";
  545. if (!reuse)
  546. {
  547. /* open connection to IMAP server */
  548. memset (&sin, 0, sizeof (sin));
  549. sin.sin_port = htons (box->port);
  550. sin.sin_family = AF_INET;
  551. printf ("Resolving %s... ", box->host);
  552. fflush (stdout);
  553. he = gethostbyname (box->host);
  554. if (!he)
  555. {
  556. perror ("gethostbyname");
  557. return 0;
  558. }
  559. puts ("ok");
  560. sin.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
  561. s = socket (PF_INET, SOCK_STREAM, 0);
  562. printf ("Connecting to %s:%hu... ", inet_ntoa (sin.sin_addr),
  563. ntohs (sin.sin_port));
  564. fflush (stdout);
  565. if (connect (s, (struct sockaddr *) &sin, sizeof (sin)))
  566. {
  567. perror ("connect");
  568. exit (1);
  569. }
  570. puts ("ok");
  571. imap->sock->fd = s;
  572. }
  573. do
  574. {
  575. /* if we are reusing the existing connection, we can skip the
  576. * authentication steps.
  577. */
  578. if (!reuse)
  579. {
  580. #if HAVE_LIBSSL
  581. if (box->use_imaps)
  582. use_ssl = 1;
  583. else if (box->use_sslv2 || box->use_sslv3 || box->use_tlsv1)
  584. {
  585. /* let's see what this puppy can do... */
  586. if ((ret = imap_exec (imap, "CAPABILITY")))
  587. break;
  588. /* always try to select SSL support if available */
  589. if (imap->have_starttls)
  590. {
  591. if ((ret = imap_exec (imap, "STARTTLS")))
  592. break;
  593. use_ssl = 1;
  594. }
  595. }
  596. if (!use_ssl)
  597. {
  598. if (box->require_ssl)
  599. {
  600. puts ("Error, SSL support not available");
  601. ret = -1;
  602. break;
  603. }
  604. else
  605. puts ("Warning, SSL support not available");
  606. }
  607. else
  608. {
  609. /* initialize SSL */
  610. if (init_ssl (box))
  611. {
  612. ret = -1;
  613. break;
  614. }
  615. imap->sock->ssl = SSL_new (SSLContext);
  616. SSL_set_fd (imap->sock->ssl, imap->sock->fd);
  617. ret = SSL_connect (imap->sock->ssl);
  618. if (ret <= 0)
  619. {
  620. socket_perror ("connect", imap->sock, ret);
  621. break;
  622. }
  623. /* verify the server certificate */
  624. if ((ret = verify_cert (imap->sock->ssl)))
  625. break;
  626. /* to conform to RFC2595 we need to forget all information
  627. * retrieved from CAPABILITY invocations before STARTTLS.
  628. */
  629. imap->have_namespace = 0;
  630. imap->have_cram = 0;
  631. imap->have_starttls = 0;
  632. imap->sock->use_ssl = 1;
  633. puts ("SSL support enabled");
  634. if ((ret = imap_exec (imap, "CAPABILITY")))
  635. break;
  636. }
  637. #else
  638. if ((ret = imap_exec (imap, "CAPABILITY")))
  639. break;
  640. #endif
  641. puts ("Logging in...");
  642. #if HAVE_LIBSSL
  643. if (imap->have_cram)
  644. {
  645. puts ("Authenticating with CRAM-MD5");
  646. imap->cram = 1;
  647. if ((ret = imap_exec (imap, "AUTHENTICATE CRAM-MD5")))
  648. break;
  649. }
  650. else if (imap->box->require_cram)
  651. {
  652. puts
  653. ("Error, CRAM-MD5 authentication is not supported by server");
  654. ret = -1;
  655. break;
  656. }
  657. else
  658. #endif
  659. {
  660. #if HAVE_LIBSSL
  661. if (!use_ssl)
  662. #endif
  663. puts
  664. ("*** Warning *** Password is being sent in the clear");
  665. if (
  666. (ret =
  667. imap_exec (imap, "LOGIN \"%s\" \"%s\"", box->user,
  668. box->pass)))
  669. {
  670. puts ("Error, LOGIN failed");
  671. break;
  672. }
  673. }
  674. /* get NAMESPACE info */
  675. if (box->use_namespace && imap->have_namespace)
  676. {
  677. if ((ret = imap_exec (imap, "NAMESPACE")))
  678. break;
  679. }
  680. } /* !reuse */
  681. /* XXX for now assume personal namespace */
  682. if (imap->box->use_namespace && is_list (imap->ns_personal) &&
  683. is_list (imap->ns_personal->child) &&
  684. is_atom (imap->ns_personal->child->child))
  685. {
  686. imap->prefix = imap->ns_personal->child->child->val;
  687. }
  688. fputs ("Selecting mailbox... ", stdout);
  689. fflush (stdout);
  690. if (
  691. (ret =
  692. imap_exec (imap, "SELECT \"%s%s\"", imap->prefix, box->box)))
  693. break;
  694. printf ("%d messages, %d recent\n", imap->count, imap->recent);
  695. puts ("Reading IMAP mailbox index");
  696. if (imap->count > 0)
  697. {
  698. if ((ret = imap_exec (imap, "UID FETCH %d:* (FLAGS RFC822.SIZE)",
  699. imap->minuid)))
  700. break;
  701. }
  702. }
  703. while (0);
  704. if (ret)
  705. {
  706. imap_close (imap);
  707. imap = 0;
  708. }
  709. return imap;
  710. }
  711. void
  712. imap_close (imap_t * imap)
  713. {
  714. imap_exec (imap, "LOGOUT");
  715. close (imap->sock->fd);
  716. free (imap->sock);
  717. free (imap->buf);
  718. free_message (imap->msgs);
  719. memset (imap, 0xff, sizeof (imap_t));
  720. free (imap);
  721. }
  722. /* write a buffer stripping all \r bytes */
  723. static int
  724. write_strip (int fd, char *buf, size_t len)
  725. {
  726. size_t start = 0;
  727. size_t end = 0;
  728. while (start < len)
  729. {
  730. while (end < len && buf[end] != '\r')
  731. end++;
  732. write (fd, buf + start, end - start);
  733. end++;
  734. start = end;
  735. }
  736. return 0;
  737. }
  738. static int
  739. send_server (Socket_t * sock, const char *fmt, ...)
  740. {
  741. char buf[128];
  742. char cmd[128];
  743. va_list ap;
  744. int n;
  745. va_start (ap, fmt);
  746. vsnprintf (buf, sizeof (buf), fmt, ap);
  747. va_end (ap);
  748. snprintf (cmd, sizeof (cmd), "%d %s\r\n", ++Tag, buf);
  749. n = socket_write (sock, cmd, strlen (cmd));
  750. if (n <= 0)
  751. {
  752. socket_perror ("write", sock, n);
  753. return -1;
  754. }
  755. if (Verbose)
  756. fputs (cmd, stdout);
  757. return 0;
  758. }
  759. int
  760. imap_fetch_message (imap_t * imap, unsigned int uid, int fd)
  761. {
  762. char *cmd;
  763. char *arg;
  764. size_t bytes;
  765. size_t n;
  766. char buf[1024];
  767. send_server (imap->sock, "UID FETCH %d BODY.PEEK[]", uid);
  768. for (;;)
  769. {
  770. if (buffer_gets (imap->buf, &cmd))
  771. return -1;
  772. if (Verbose)
  773. puts (cmd);
  774. if (*cmd == '*')
  775. {
  776. /* need to figure out how long the message is
  777. * * <msgno> FETCH (RFC822 {<size>}
  778. */
  779. next_arg (&cmd); /* * */
  780. next_arg (&cmd); /* <msgno> */
  781. next_arg (&cmd); /* FETCH */
  782. while ((arg = next_arg (&cmd)) && *arg != '{')
  783. ;
  784. if (!arg)
  785. {
  786. puts ("parse error getting size");
  787. return -1;
  788. }
  789. bytes = strtol (arg + 1, 0, 10);
  790. // printf ("receiving %d byte message\n", bytes);
  791. /* dump whats left over in the input buffer */
  792. n = imap->buf->bytes - imap->buf->offset;
  793. if (n > bytes)
  794. {
  795. /* the entire message fit in the buffer */
  796. n = bytes;
  797. }
  798. /* ick. we have to strip out the \r\n line endings, so
  799. * i can't just dump the raw bytes to disk.
  800. */
  801. write_strip (fd, imap->buf->buf + imap->buf->offset, n);
  802. bytes -= n;
  803. // printf ("wrote %d buffered bytes\n", n);
  804. /* mark that we used part of the buffer */
  805. imap->buf->offset += n;
  806. /* now read the rest of the message */
  807. while (bytes > 0)
  808. {
  809. n = bytes;
  810. if (n > sizeof (buf))
  811. n = sizeof (buf);
  812. n = socket_read (imap->sock, buf, n);
  813. if (n > 0)
  814. {
  815. // printf("imap_fetch_message:%d:read %d bytes\n", __LINE__, n);
  816. write_strip (fd, buf, n);
  817. bytes -= n;
  818. }
  819. else
  820. {
  821. socket_perror ("read", imap->sock, n);
  822. return -1;
  823. }
  824. }
  825. // puts ("finished fetching msg");
  826. buffer_gets (imap->buf, &cmd);
  827. if (Verbose)
  828. puts (cmd); /* last part of line */
  829. }
  830. else
  831. {
  832. arg = next_arg (&cmd);
  833. if (!arg || (size_t) atoi (arg) != Tag)
  834. {
  835. puts ("wrong tag");
  836. return -1;
  837. }
  838. arg = next_arg (&cmd);
  839. if (!strcmp ("OK", arg))
  840. return 0;
  841. return -1;
  842. }
  843. }
  844. /* not reached */
  845. }
  846. /* add flags to existing flags */
  847. int
  848. imap_set_flags (imap_t * imap, unsigned int uid, unsigned int flags)
  849. {
  850. char buf[256];
  851. int i;
  852. buf[0] = 0;
  853. for (i = 0; i < D_MAX; i++)
  854. {
  855. if (flags & (1 << i))
  856. snprintf (buf + strlen (buf),
  857. sizeof (buf) - strlen (buf), "%s%s",
  858. (buf[0] != 0) ? " " : "", Flags[i]);
  859. }
  860. return imap_exec (imap, "UID STORE %d +FLAGS.SILENT (%s)", uid, buf);
  861. }
  862. int
  863. imap_expunge (imap_t * imap)
  864. {
  865. return imap_exec (imap, "EXPUNGE");
  866. }
  867. int
  868. imap_copy_message (imap_t * imap, unsigned int uid, const char *mailbox)
  869. {
  870. return imap_exec (imap, "UID COPY %u \"%s%s\"", uid, imap->prefix,
  871. mailbox);
  872. }
  873. int
  874. imap_append_message (imap_t * imap, int fd, message_t * msg)
  875. {
  876. char buf[1024];
  877. size_t len;
  878. size_t sofar = 0;
  879. int lines = 0;
  880. char flagstr[128];
  881. char *s;
  882. size_t i;
  883. size_t start, end;
  884. char *arg;
  885. /* ugh, we need to count the number of newlines */
  886. while (sofar < msg->size)
  887. {
  888. len = msg->size - sofar;
  889. if (len > sizeof (buf))
  890. len = sizeof (buf);
  891. len = read (fd, buf, len);
  892. if (len == (size_t) - 1)
  893. {
  894. perror ("read");
  895. return -1;
  896. }
  897. for (i = 0; i < len; i++)
  898. if (buf[i] == '\n')
  899. lines++;
  900. sofar += len;
  901. }
  902. flagstr[0] = 0;
  903. if (msg->flags)
  904. {
  905. strcpy (flagstr, "(");
  906. if (msg->flags & D_DELETED)
  907. snprintf (flagstr + strlen (flagstr),
  908. sizeof (flagstr) - strlen (flagstr), "%s\\Deleted",
  909. flagstr[1] ? " " : "");
  910. if (msg->flags & D_ANSWERED)
  911. snprintf (flagstr + strlen (flagstr),
  912. sizeof (flagstr) - strlen (flagstr), "%s\\Answered",
  913. flagstr[1] ? " " : "");
  914. if (msg->flags & D_SEEN)
  915. snprintf (flagstr + strlen (flagstr),
  916. sizeof (flagstr) - strlen (flagstr), "%s\\Seen",
  917. flagstr[1] ? " " : "");
  918. if (msg->flags & D_FLAGGED)
  919. snprintf (flagstr + strlen (flagstr),
  920. sizeof (flagstr) - strlen (flagstr), "%s\\Flagged",
  921. flagstr[1] ? " " : "");
  922. if (msg->flags & D_DRAFT)
  923. snprintf (flagstr + strlen (flagstr),
  924. sizeof (flagstr) - strlen (flagstr), "%s\\Draft",
  925. flagstr[1] ? " " : "");
  926. snprintf (flagstr + strlen (flagstr),
  927. sizeof (flagstr) - strlen (flagstr), ") ");
  928. }
  929. send_server (imap->sock, "APPEND %s%s %s{%d}",
  930. imap->prefix, imap->box->box, flagstr, msg->size + lines);
  931. if (buffer_gets (imap->buf, &s))
  932. return -1;
  933. if (Verbose)
  934. puts (s);
  935. if (*s != '+')
  936. {
  937. puts ("Error, expected `+' from server (aborting)");
  938. return -1;
  939. }
  940. /* rewind */
  941. lseek (fd, 0, 0);
  942. sofar = 0;
  943. while (sofar < msg->size)
  944. {
  945. len = msg->size - sofar;
  946. if (len > sizeof (buf))
  947. len = sizeof (buf);
  948. len = read (fd, buf, len);
  949. if (len == (size_t) - 1)
  950. return -1;
  951. start = 0;
  952. while (start < len)
  953. {
  954. end = start;
  955. while (end < len && buf[end] != '\n')
  956. end++;
  957. if (start != end)
  958. socket_write (imap->sock, buf + start, end - start);
  959. /* only send a crlf if we actually hit the end of a line. we
  960. * might be in the middle of a line in which case we don't
  961. * send one.
  962. */
  963. if (end != len)
  964. socket_write (imap->sock, "\r\n", 2);
  965. start = end + 1;
  966. }
  967. sofar += len;
  968. }
  969. socket_write (imap->sock, "\r\n", 2);
  970. for (;;)
  971. {
  972. if (buffer_gets (imap->buf, &s))
  973. return -1;
  974. if (Verbose)
  975. puts (s);
  976. arg = next_arg (&s);
  977. if (*arg == '*')
  978. {
  979. /* XXX just ignore it for now */
  980. }
  981. else if (atoi (arg) != (int) Tag)
  982. {
  983. puts ("wrong tag");
  984. return -1;
  985. }
  986. else
  987. {
  988. int uid;
  989. arg = next_arg (&s);
  990. if (strcmp (arg, "OK"))
  991. return -1;
  992. arg = next_arg (&s);
  993. if (*arg != '[')
  994. break;
  995. arg++;
  996. if (strcasecmp ("APPENDUID", arg))
  997. {
  998. puts ("Error, expected APPENDUID");
  999. break;
  1000. }
  1001. arg = next_arg (&s);
  1002. if (!arg)
  1003. break;
  1004. if (atoi (arg) != (int) imap->uidvalidity)
  1005. {
  1006. puts ("Error, UIDVALIDITY doesn't match APPENDUID");
  1007. return -1;
  1008. }
  1009. arg = next_arg (&s);
  1010. if (!arg)
  1011. break;
  1012. uid = strtol (arg, &s, 10);
  1013. if (*s != ']')
  1014. {
  1015. /* parse error */
  1016. break;
  1017. }
  1018. return uid;
  1019. }
  1020. }
  1021. return 0;
  1022. }