allow leading whitespace in config files

now possible to sync multiple mailboxes by specifying multiple aliases on
the command line.  IMAP connections are reused if possible.

don't initialize ssl unless we are going to use it.
This commit is contained in:
Michael Elkins 2000-12-27 21:14:22 +00:00
parent 6f4cd030f8
commit acd674f93e
5 changed files with 319 additions and 205 deletions

169
imap.c
View File

@ -49,6 +49,21 @@ const char *Flags[] = {
SSL_CTX *SSLContext = 0; SSL_CTX *SSLContext = 0;
void
free_message (message_t * msg)
{
message_t *tmp;
while (msg)
{
tmp = msg;
msg = msg->next;
if (tmp->file)
free (tmp->file);
free (tmp);
}
}
/* this gets called when a certificate is to be verified */ /* this gets called when a certificate is to be verified */
static int static int
verify_cert (SSL * ssl) verify_cert (SSL * ssl)
@ -100,7 +115,6 @@ verify_cert (SSL * ssl)
puts ("\n*** Fine, but don't say I didn't warn you!\n"); puts ("\n*** Fine, but don't say I didn't warn you!\n");
} }
return ret; return ret;
} }
static int static int
@ -304,15 +318,6 @@ parse_fetch (imap_t * imap, list_t * list)
} }
} }
#if 0
if (uid == 221)
{
int loop = 1;
while (loop);
}
#endif
cur = calloc (1, sizeof (message_t)); cur = calloc (1, sizeof (message_t));
cur->next = imap->msgs; cur->next = imap->msgs;
imap->msgs = cur; imap->msgs = cur;
@ -485,24 +490,61 @@ imap_exec (imap_t * imap, const char *fmt, ...)
* mailbox. * mailbox.
*/ */
imap_t * imap_t *
imap_open (config_t * box, unsigned int minuid) imap_open (config_t * box, unsigned int minuid, imap_t * imap)
{ {
int ret; int ret;
imap_t *imap;
int s; int s;
struct sockaddr_in sin; struct sockaddr_in sin;
struct hostent *he; struct hostent *he;
char *ns_prefix = ""; char *ns_prefix = "";
int reuse = 0;
#if HAVE_LIBSSL #if HAVE_LIBSSL
int use_ssl = 0; int use_ssl = 0;
#endif #endif
if (imap)
{
/* determine whether or not we can reuse the existing session */
if (strcmp (box->host, imap->box->host) ||
strcmp (box->user, imap->box->user) ||
box->port != imap->box->port
#if HAVE_LIBSSL #if HAVE_LIBSSL
/* initialize SSL */ /* ensure that security requirements are met */
if (init_ssl (box)) || (box->require_ssl ^ imap->box->require_ssl)
return 0; || (box->require_cram ^ imap->box->require_cram)
#endif #endif
)
{
/* can't reuse */
imap_close (imap);
imap = 0;
}
else
{
reuse = 1;
/* reset mailbox-specific state info */
imap->recent = 0;
imap->deleted = 0;
imap->count = 0;
imap->maxuid = 0;
free_message (imap->msgs);
imap->msgs = 0;
}
}
if (!imap)
{
imap = calloc (1, sizeof (imap_t));
imap->sock = calloc (1, sizeof (Socket_t));
imap->buf = calloc (1, sizeof (buffer_t));
imap->buf->sock = imap->sock;
}
imap->box = box;
imap->minuid = minuid;
if (!reuse)
{
/* open connection to IMAP server */ /* open connection to IMAP server */
memset (&sin, 0, sizeof (sin)); memset (&sin, 0, sizeof (sin));
@ -533,30 +575,38 @@ imap_open (config_t * box, unsigned int minuid)
} }
puts ("ok"); puts ("ok");
imap = calloc (1, sizeof (imap_t));
imap->sock = calloc (1, sizeof (Socket_t));
imap->sock->fd = s; imap->sock->fd = s;
imap->buf = calloc (1, sizeof (buffer_t)); }
imap->buf->sock = imap->sock;
imap->box = box;
imap->minuid = minuid;
do
{
/* if we are reusing the existing connection, we can skip the
* authentication steps.
*/
if (!reuse)
{
#if HAVE_LIBSSL #if HAVE_LIBSSL
if (!box->use_imaps) if (!box->use_imaps)
{ {
/* let's see what this puppy can do... */ /* let's see what this puppy can do... */
ret = imap_exec (imap, "CAPABILITY"); if ((ret = imap_exec (imap, "CAPABILITY")))
break;
/* always try to select SSL support if available */ /* always try to select SSL support if available */
if (imap->have_starttls && !imap_exec (imap, "STARTTLS")) if (imap->have_starttls)
{
if ((ret = imap_exec (imap, "STARTTLS")))
break;
use_ssl = 1; use_ssl = 1;
}
if (!use_ssl) if (!use_ssl)
{ {
if (box->require_ssl) if (box->require_ssl)
{ {
puts ("Error, SSL support not available"); puts ("Error, SSL support not available");
return 0; ret = -1;
break;
} }
else else
puts ("Warning, SSL support not available"); puts ("Warning, SSL support not available");
@ -567,28 +617,36 @@ imap_open (config_t * box, unsigned int minuid)
if (use_ssl) if (use_ssl)
{ {
/* initialize SSL */
if (init_ssl (box))
return 0;
imap->sock->ssl = SSL_new (SSLContext); imap->sock->ssl = SSL_new (SSLContext);
SSL_set_fd (imap->sock->ssl, imap->sock->fd); SSL_set_fd (imap->sock->ssl, imap->sock->fd);
ret = SSL_connect (imap->sock->ssl); ret = SSL_connect (imap->sock->ssl);
if (ret <= 0) if (ret <= 0)
{ {
ret = SSL_get_error (imap->sock->ssl, ret); ret = SSL_get_error (imap->sock->ssl, ret);
printf ("Error, SSL_connect: %s\n", ERR_error_string (ret, 0)); printf ("Error, SSL_connect: %s\n",
return 0; ERR_error_string (ret, 0));
ret = -1;
break;
} }
/* verify the server certificate */ /* verify the server certificate */
if (verify_cert (imap->sock->ssl)) if ((ret = verify_cert (imap->sock->ssl)))
return 0; break;
imap->sock->use_ssl = 1; imap->sock->use_ssl = 1;
puts ("SSL support enabled"); puts ("SSL support enabled");
if (box->use_imaps) if (box->use_imaps)
ret = imap_exec (imap, "CAPABILITY"); if ((ret = imap_exec (imap, "CAPABILITY")))
break;
} }
#else #else
ret = imap_exec (imap, "CAPABILITY"); if ((ret = imap_exec (imap, "CAPABILITY")))
break;
#endif #endif
puts ("Logging in..."); puts ("Logging in...");
@ -597,7 +655,15 @@ imap_open (config_t * box, unsigned int minuid)
{ {
puts ("Authenticating with CRAM-MD5"); puts ("Authenticating with CRAM-MD5");
imap->cram = 1; imap->cram = 1;
ret = imap_exec (imap, "AUTHENTICATE CRAM-MD5"); if ((ret = imap_exec (imap, "AUTHENTICATE CRAM-MD5")))
break;
}
else if (imap->box->require_cram)
{
puts
("Error, CRAM-MD5 authentication is not supported by server");
ret = -1;
break;
} }
else else
#endif #endif
@ -605,41 +671,46 @@ imap_open (config_t * box, unsigned int minuid)
#if HAVE_LIBSSL #if HAVE_LIBSSL
if (!use_ssl) if (!use_ssl)
#endif #endif
puts ("*** Warning *** Password is being sent in the clear"); puts
ret = imap_exec (imap, "LOGIN \"%s\" \"%s\"", box->user, box->pass); ("*** Warning *** Password is being sent in the clear");
if (
(ret =
imap_exec (imap, "LOGIN \"%s\" \"%s\"", box->user,
box->pass)))
break;
} }
/* get NAMESPACE info */ /* get NAMESPACE info */
if (!ret && box->use_namespace && imap->have_namespace && if (box->use_namespace && imap->have_namespace)
!imap_exec (imap, "NAMESPACE"))
{ {
if ((ret = imap_exec (imap, "NAMESPACE")))
break;
}
} /* !reuse */
/* XXX for now assume personal namespace */ /* XXX for now assume personal namespace */
if (is_list (imap->ns_personal) && if (imap->box->use_namespace && is_list (imap->ns_personal) &&
is_list (imap->ns_personal->child) && is_list (imap->ns_personal->child) &&
is_atom (imap->ns_personal->child->child)) is_atom (imap->ns_personal->child->child))
{ {
ns_prefix = imap->ns_personal->child->child->val; ns_prefix = imap->ns_personal->child->child->val;
} }
}
if (!ret)
{
fputs ("Selecting mailbox... ", stdout); fputs ("Selecting mailbox... ", stdout);
fflush (stdout); fflush (stdout);
ret = imap_exec (imap, "SELECT %s%s", ns_prefix, box->box); if ((ret = imap_exec (imap, "SELECT %s%s", ns_prefix, box->box)))
if (!ret) break;
printf ("%d messages, %d recent\n", imap->count, imap->recent); printf ("%d messages, %d recent\n", imap->count, imap->recent);
}
if (!ret)
{
puts ("Reading IMAP mailbox index"); puts ("Reading IMAP mailbox index");
if (imap->count > 0) if (imap->count > 0)
{ {
ret = imap_exec (imap, "UID FETCH %d:* (FLAGS RFC822.SIZE)", if ((ret = imap_exec (imap, "UID FETCH %d:* (FLAGS RFC822.SIZE)",
imap->minuid); imap->minuid)))
break;
} }
} }
while (0);
if (ret) if (ret)
{ {
@ -658,6 +729,12 @@ imap_close (imap_t * imap)
{ {
puts ("Closing IMAP connection"); puts ("Closing IMAP connection");
imap_exec (imap, "LOGOUT"); imap_exec (imap, "LOGOUT");
close (imap->sock->fd);
free (imap->sock);
free (imap->buf);
free_message (imap->msgs);
memset (imap, 0xff, sizeof (imap_t));
free (imap);
} }
/* write a buffer stripping all \r bytes */ /* write a buffer stripping all \r bytes */

20
isync.1
View File

@ -16,7 +16,7 @@
\" along with this program; if not, write to the Free Software \" along with this program; if not, write to the Free Software
\" Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA \" Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
.. ..
.TH isync 1 "2000 Dec 21" .TH isync 1 "2000 Dec 27"
.. ..
.SH NAME .SH NAME
isync - synchronize IMAP4 and maildir mailboxes isync - synchronize IMAP4 and maildir mailboxes
@ -26,7 +26,10 @@ isync - synchronize IMAP4 and maildir mailboxes
[ [
.I options... .I options...
] ]
.I file .I mailbox
[
.I mailbox ...
]
.. ..
.SH DESCRIPTION .SH DESCRIPTION
.B isync .B isync
@ -171,6 +174,15 @@ This is useful with broken IMAP servers. (Default:
) )
.. ..
.TP .TP
\fBRequireCRAM\fR \fIyes|no\fR
If set to
.I yes
,
.B isync
will require that the server accept CRAM-MD5 intead of PLAIN to authenticate
the user.
..
.TP
\fBRequireSSL\fR \fIyes|no\fR \fBRequireSSL\fR \fIyes|no\fR
.B isync .B isync
will abort the connection if a TLS/SSL session to the IMAP will abort the connection if a TLS/SSL session to the IMAP
@ -236,6 +248,10 @@ relies on using the message UIDs that info must be inserted into the
filename in a way which will be interoperable with existing readers. So filename in a way which will be interoperable with existing readers. So
the UID is placed in the filename of the messages in the local maildir the UID is placed in the filename of the messages in the local maildir
mailbox rather than the :info field. mailbox rather than the :info field.
.P
When synchronizing multiple mailboxes on the same IMAP server, it is not
possible to select different SSL options for each mailbox. Only the options
from the first mailbox are applied since the SSL session is reused.
.SH SEE ALSO .SH SEE ALSO
mutt(1), maildir(5) mutt(1), maildir(5)
.P .P

View File

@ -63,6 +63,7 @@ struct config
unsigned int use_sslv2:1; unsigned int use_sslv2:1;
unsigned int use_sslv3:1; unsigned int use_sslv3:1;
unsigned int use_tlsv1:1; unsigned int use_tlsv1:1;
unsigned int require_cram:1;
#endif #endif
unsigned int use_namespace:1; unsigned int use_namespace:1;
}; };
@ -162,14 +163,16 @@ void imap_close (imap_t *);
int imap_fetch_message (imap_t *, unsigned int, int); int imap_fetch_message (imap_t *, unsigned int, int);
int imap_set_flags (imap_t *, unsigned int, unsigned int); int imap_set_flags (imap_t *, unsigned int, unsigned int);
int imap_expunge (imap_t *); int imap_expunge (imap_t *);
imap_t *imap_open (config_t *, unsigned int); imap_t *imap_open (config_t *, unsigned int, imap_t *);
mailbox_t *maildir_open (const char *, int fast); mailbox_t *maildir_open (const char *, int fast);
int maildir_expunge (mailbox_t *, int); int maildir_expunge (mailbox_t *, int);
int maildir_sync (mailbox_t *); int maildir_sync (mailbox_t *);
int maildir_set_uidvalidity (mailbox_t *, unsigned int uidvalidity); int maildir_set_uidvalidity (mailbox_t *, unsigned int uidvalidity);
void maildir_close (mailbox_t *);
message_t * find_msg (message_t * list, unsigned int uid); message_t * find_msg (message_t * list, unsigned int uid);
void free_message (message_t *);
/* parse an IMAP list construct */ /* parse an IMAP list construct */
list_t * parse_list (char *s, char **end); list_t * parse_list (char *s, char **end);

View File

@ -410,3 +410,12 @@ maildir_set_uidvalidity (mailbox_t * mbox, unsigned int uidvalidity)
return (ret); return (ret);
} }
void
maildir_close (mailbox_t * mbox)
{
free (mbox->path);
free_message (mbox->msgs);
memset (mbox, 0xff, sizeof (mailbox_t));
free (mbox);
}

61
main.c
View File

@ -66,7 +66,7 @@ usage (void)
{ {
printf ("%s %s IMAP4 to maildir synchronizer\n", PACKAGE, VERSION); printf ("%s %s IMAP4 to maildir synchronizer\n", PACKAGE, VERSION);
puts ("Copyright (C) 2000 Michael R. Elkins <me@mutt.org>"); puts ("Copyright (C) 2000 Michael R. Elkins <me@mutt.org>");
printf ("usage: %s [ flags ] mailbox\n", PACKAGE); printf ("usage: %s [ flags ] mailbox [mailbox ...]\n", PACKAGE);
puts puts
(" -c, --config CONFIG read an alternate config file (default: ~/.isyncrc)"); (" -c, --config CONFIG read an alternate config file (default: ~/.isyncrc)");
puts puts
@ -113,7 +113,7 @@ load_config (char *where)
char buf[1024]; char buf[1024];
struct passwd *pw; struct passwd *pw;
config_t **cur = &box; config_t **cur = &box;
char *p; char *p, *q;
int line = 0; int line = 0;
FILE *fp; FILE *fp;
@ -138,15 +138,12 @@ load_config (char *where)
{ {
if (buf[0]) if (buf[0])
buf[strlen (buf) - 1] = 0; buf[strlen (buf) - 1] = 0;
line++;
if (buf[0] == '#')
continue;
p = buf; p = buf;
while (*p && !isspace ((unsigned char) *p)) q = next_arg (&p);
p++; line++;
while (isspace ((unsigned char) *p)) if (!q || *q == '#')
p++; continue;
if (!strncasecmp ("mailbox", buf, 7)) if (!strncasecmp ("mailbox", q, 7))
{ {
if (*cur) if (*cur)
cur = &(*cur)->next; cur = &(*cur)->next;
@ -154,7 +151,7 @@ load_config (char *where)
config_defaults (*cur); config_defaults (*cur);
(*cur)->path = strdup (p); (*cur)->path = strdup (p);
} }
else if (!strncasecmp ("host", buf, 4)) else if (!strncasecmp ("host", q, 4))
{ {
#if HAVE_LIBSSL #if HAVE_LIBSSL
if (!strncasecmp ("imaps:", p, 6)) if (!strncasecmp ("imaps:", p, 6))
@ -177,47 +174,47 @@ load_config (char *where)
else else
global.host = strdup (p); global.host = strdup (p);
} }
else if (!strncasecmp ("user", buf, 4)) else if (!strncasecmp ("user", q, 4))
{ {
if (*cur) if (*cur)
(*cur)->user = strdup (p); (*cur)->user = strdup (p);
else else
global.user = strdup (p); global.user = strdup (p);
} }
else if (!strncasecmp ("pass", buf, 4)) else if (!strncasecmp ("pass", q, 4))
{ {
if (*cur) if (*cur)
(*cur)->pass = strdup (p); (*cur)->pass = strdup (p);
else else
global.pass = strdup (p); global.pass = strdup (p);
} }
else if (!strncasecmp ("port", buf, 4)) else if (!strncasecmp ("port", q, 4))
{ {
if (*cur) if (*cur)
(*cur)->port = atoi (p); (*cur)->port = atoi (p);
else else
global.port = atoi (p); global.port = atoi (p);
} }
else if (!strncasecmp ("box", buf, 3)) else if (!strncasecmp ("box", q, 3))
{ {
if (*cur) if (*cur)
(*cur)->box = strdup (p); (*cur)->box = strdup (p);
else else
global.box = strdup (p); global.box = strdup (p);
} }
else if (!strncasecmp ("alias", buf, 5)) else if (!strncasecmp ("alias", q, 5))
{ {
if (*cur) if (*cur)
(*cur)->alias = strdup (p); (*cur)->alias = strdup (p);
} }
else if (!strncasecmp ("maxsize", buf, 7)) else if (!strncasecmp ("maxsize", q, 7))
{ {
if (*cur) if (*cur)
(*cur)->max_size = atol (p); (*cur)->max_size = atol (p);
else else
global.max_size = atol (p); global.max_size = atol (p);
} }
else if (!strncasecmp ("UseNamespace", buf, 12)) else if (!strncasecmp ("UseNamespace", q, 12))
{ {
if (*cur) if (*cur)
(*cur)->use_namespace = (strcasecmp (p, "yes") == 0); (*cur)->use_namespace = (strcasecmp (p, "yes") == 0);
@ -225,44 +222,51 @@ load_config (char *where)
global.use_namespace = (strcasecmp (p, "yes") == 0); global.use_namespace = (strcasecmp (p, "yes") == 0);
} }
#if HAVE_LIBSSL #if HAVE_LIBSSL
else if (!strncasecmp ("CertificateFile", buf, 15)) else if (!strncasecmp ("CertificateFile", q, 15))
{ {
if (*cur) if (*cur)
(*cur)->cert_file = strdup (p); (*cur)->cert_file = strdup (p);
else else
global.cert_file = strdup (p); global.cert_file = strdup (p);
} }
else if (!strncasecmp ("RequireSSL", buf, 10)) else if (!strncasecmp ("RequireSSL", q, 10))
{ {
if (*cur) if (*cur)
(*cur)->require_ssl = (strcasecmp (p, "yes") == 0); (*cur)->require_ssl = (strcasecmp (p, "yes") == 0);
else else
global.require_ssl = (strcasecmp (p, "yes") == 0); global.require_ssl = (strcasecmp (p, "yes") == 0);
} }
else if (!strncasecmp ("UseSSLv2", buf, 8)) else if (!strncasecmp ("UseSSLv2", q, 8))
{ {
if (*cur) if (*cur)
(*cur)->use_sslv2 = (strcasecmp (p, "yes") == 0); (*cur)->use_sslv2 = (strcasecmp (p, "yes") == 0);
else else
global.use_sslv2 = (strcasecmp (p, "yes") == 0); global.use_sslv2 = (strcasecmp (p, "yes") == 0);
} }
else if (!strncasecmp ("UseSSLv3", buf, 8)) else if (!strncasecmp ("UseSSLv3", q, 8))
{ {
if (*cur) if (*cur)
(*cur)->use_sslv3 = (strcasecmp (p, "yes") == 0); (*cur)->use_sslv3 = (strcasecmp (p, "yes") == 0);
else else
global.use_sslv3 = (strcasecmp (p, "yes") == 0); global.use_sslv3 = (strcasecmp (p, "yes") == 0);
} }
else if (!strncasecmp ("UseTLSv1", buf, 8)) else if (!strncasecmp ("UseTLSv1", q, 8))
{ {
if (*cur) if (*cur)
(*cur)->use_tlsv1 = (strcasecmp (p, "yes") == 0); (*cur)->use_tlsv1 = (strcasecmp (p, "yes") == 0);
else else
global.use_tlsv1 = (strcasecmp (p, "yes") == 0); global.use_tlsv1 = (strcasecmp (p, "yes") == 0);
} }
else if (!strncasecmp ("RequireCRAM", q, 11))
{
if (*cur)
(*cur)->require_cram = (strcasecmp (p, "yes") == 0);
else
global.require_cram = (strcasecmp (p, "yes") == 0);
}
#endif #endif
else if (buf[0]) else if (buf[0])
printf ("%s:%d:unknown command:%s", path, line, buf); printf ("%s:%d:unknown command:%s", path, line, q);
} }
fclose (fp); fclose (fp);
} }
@ -310,7 +314,7 @@ main (int argc, char **argv)
int i; int i;
config_t *box; config_t *box;
mailbox_t *mail; mailbox_t *mail;
imap_t *imap; imap_t *imap = 0;
int expunge = 0; /* by default, don't delete anything */ int expunge = 0; /* by default, don't delete anything */
int fast = 0; int fast = 0;
int delete = 0; int delete = 0;
@ -389,6 +393,8 @@ main (int argc, char **argv)
load_config (config); load_config (config);
for (; argv[optind]; optind++)
{
box = find_box (argv[optind]); box = find_box (argv[optind]);
if (!box) if (!box)
{ {
@ -424,7 +430,7 @@ main (int argc, char **argv)
exit (1); exit (1);
} }
imap = imap_open (box, fast ? mail->maxuid + 1 : 1); imap = imap_open (box, fast ? mail->maxuid + 1 : 1, imap);
if (!imap) if (!imap)
exit (1); exit (1);
@ -460,6 +466,9 @@ main (int argc, char **argv)
exit (1); exit (1);
} }
maildir_close (mail);
}
/* gracefully close connection to the IMAP server */ /* gracefully close connection to the IMAP server */
imap_close (imap); imap_close (imap);