Browse Source

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.
0.9
Michael Elkins 24 years ago
parent
commit
acd674f93e
  1. 169
      imap.c
  2. 20
      isync.1
  3. 5
      isync.h
  4. 9
      maildir.c
  5. 59
      main.c

169
imap.c

@ -49,6 +49,21 @@ const char *Flags[] = {
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 */
static int
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");
}
return ret;
}
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->next = imap->msgs;
imap->msgs = cur;
@ -485,24 +490,61 @@ imap_exec (imap_t * imap, const char *fmt, ...)
* mailbox.
*/
imap_t *
imap_open (config_t * box, unsigned int minuid)
imap_open (config_t * box, unsigned int minuid, imap_t * imap)
{
int ret;
imap_t *imap;
int s;
struct sockaddr_in sin;
struct hostent *he;
char *ns_prefix = "";
int reuse = 0;
#if HAVE_LIBSSL
int use_ssl = 0;
#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
/* initialize SSL */
if (init_ssl (box))
return 0;
/* ensure that security requirements are met */
|| (box->require_ssl ^ imap->box->require_ssl)
|| (box->require_cram ^ imap->box->require_cram)
#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 */
memset (&sin, 0, sizeof (sin));
@ -533,30 +575,38 @@ imap_open (config_t * box, unsigned int minuid)
}
puts ("ok");
imap = calloc (1, sizeof (imap_t));
imap->sock = calloc (1, sizeof (Socket_t));
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 (!box->use_imaps)
{
/* 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 */
if (imap->have_starttls && !imap_exec (imap, "STARTTLS"))
if (imap->have_starttls)
{
if ((ret = imap_exec (imap, "STARTTLS")))
break;
use_ssl = 1;
}
if (!use_ssl)
{
if (box->require_ssl)
{
puts ("Error, SSL support not available");
return 0;
ret = -1;
break;
}
else
puts ("Warning, SSL support not available");
@ -567,28 +617,36 @@ imap_open (config_t * box, unsigned int minuid)
if (use_ssl)
{
/* initialize SSL */
if (init_ssl (box))
return 0;
imap->sock->ssl = SSL_new (SSLContext);
SSL_set_fd (imap->sock->ssl, imap->sock->fd);
ret = SSL_connect (imap->sock->ssl);
if (ret <= 0)
{
ret = SSL_get_error (imap->sock->ssl, ret);
printf ("Error, SSL_connect: %s\n", ERR_error_string (ret, 0));
return 0;
printf ("Error, SSL_connect: %s\n",
ERR_error_string (ret, 0));
ret = -1;
break;
}
/* verify the server certificate */
if (verify_cert (imap->sock->ssl))
return 0;
if ((ret = verify_cert (imap->sock->ssl)))
break;
imap->sock->use_ssl = 1;
puts ("SSL support enabled");
if (box->use_imaps)
ret = imap_exec (imap, "CAPABILITY");
if ((ret = imap_exec (imap, "CAPABILITY")))
break;
}
#else
ret = imap_exec (imap, "CAPABILITY");
if ((ret = imap_exec (imap, "CAPABILITY")))
break;
#endif
puts ("Logging in...");
@ -597,7 +655,15 @@ imap_open (config_t * box, unsigned int minuid)
{
puts ("Authenticating with CRAM-MD5");
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
#endif
@ -605,41 +671,46 @@ imap_open (config_t * box, unsigned int minuid)
#if HAVE_LIBSSL
if (!use_ssl)
#endif
puts ("*** Warning *** Password is being sent in the clear");
ret = imap_exec (imap, "LOGIN \"%s\" \"%s\"", box->user, box->pass);
puts
("*** Warning *** Password is being sent in the clear");
if (
(ret =
imap_exec (imap, "LOGIN \"%s\" \"%s\"", box->user,
box->pass)))
break;
}
/* get NAMESPACE info */
if (!ret && box->use_namespace && imap->have_namespace &&
!imap_exec (imap, "NAMESPACE"))
if (box->use_namespace && imap->have_namespace)
{
if ((ret = imap_exec (imap, "NAMESPACE")))
break;
}
} /* !reuse */
/* 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_atom (imap->ns_personal->child->child))
{
ns_prefix = imap->ns_personal->child->child->val;
}
}
if (!ret)
{
fputs ("Selecting mailbox... ", stdout);
fflush (stdout);
ret = imap_exec (imap, "SELECT %s%s", ns_prefix, box->box);
if (!ret)
if ((ret = imap_exec (imap, "SELECT %s%s", ns_prefix, box->box)))
break;
printf ("%d messages, %d recent\n", imap->count, imap->recent);
}
if (!ret)
{
puts ("Reading IMAP mailbox index");
if (imap->count > 0)
{
ret = imap_exec (imap, "UID FETCH %d:* (FLAGS RFC822.SIZE)",
imap->minuid);
if ((ret = imap_exec (imap, "UID FETCH %d:* (FLAGS RFC822.SIZE)",
imap->minuid)))
break;
}
}
while (0);
if (ret)
{
@ -658,6 +729,12 @@ imap_close (imap_t * imap)
{
puts ("Closing IMAP connection");
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 */

20
isync.1

@ -16,7 +16,7 @@
\" along with this program; if not, write to the Free Software
\" 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
isync - synchronize IMAP4 and maildir mailboxes
@ -26,7 +26,10 @@ isync - synchronize IMAP4 and maildir mailboxes
[
.I options...
]
.I file
.I mailbox
[
.I mailbox ...
]
..
.SH DESCRIPTION
.B isync
@ -171,6 +174,15 @@ This is useful with broken IMAP servers. (Default:
)
..
.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
.B isync
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
the UID is placed in the filename of the messages in the local maildir
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
mutt(1), maildir(5)
.P

5
isync.h

@ -63,6 +63,7 @@ struct config
unsigned int use_sslv2:1;
unsigned int use_sslv3:1;
unsigned int use_tlsv1:1;
unsigned int require_cram:1;
#endif
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_set_flags (imap_t *, unsigned int, unsigned int);
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);
int maildir_expunge (mailbox_t *, int);
int maildir_sync (mailbox_t *);
int maildir_set_uidvalidity (mailbox_t *, unsigned int uidvalidity);
void maildir_close (mailbox_t *);
message_t * find_msg (message_t * list, unsigned int uid);
void free_message (message_t *);
/* parse an IMAP list construct */
list_t * parse_list (char *s, char **end);

9
maildir.c

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

59
main.c

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

Loading…
Cancel
Save