From: Thomas Roessler Date: Thu, 6 Jul 2000 18:40:53 +0000 (+0000) Subject: More IMAP clean-up. X-Git-Tag: mutt-1-3-5-rel~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dd2824a29e579886aa92728170212073b1bb8ed9;p=mutt More IMAP clean-up. --- diff --git a/imap/auth.c b/imap/auth.c index c729da5d..0f6254d2 100644 --- a/imap/auth.c +++ b/imap/auth.c @@ -98,7 +98,7 @@ static int imap_auth_cram_md5 (IMAP_DATA* idata, const char* user, char ibuf[LONG_STRING], obuf[LONG_STRING]; unsigned char hmac_response[MD5_DIGEST_LEN]; int len; - char seq[16]; + char seq[SEQLEN+1]; dprint (2, (debugfile, "Attempting CRAM-MD5 login...\n")); mutt_message _("Authenticating (CRAM-MD5)..."); @@ -112,7 +112,7 @@ static int imap_auth_cram_md5 (IMAP_DATA* idata, const char* user, * primary host name of the server. The syntax of the unencoded form must * correspond to that of an RFC 822 'msg-id' [RFC822] as described in [POP3]. */ - if (mutt_socket_read_line_d (ibuf, LONG_STRING, idata->conn) < 0) + if (mutt_socket_readln (ibuf, LONG_STRING, idata->conn) < 0) { dprint (1, (debugfile, "Error receiving server response.\n")); @@ -162,7 +162,7 @@ static int imap_auth_cram_md5 (IMAP_DATA* idata, const char* user, strcpy (ibuf + strlen (ibuf), "\r\n"); mutt_socket_write (idata->conn, ibuf); - if (mutt_socket_read_line_d (ibuf, LONG_STRING, idata->conn) < 0) + if (mutt_socket_readln (ibuf, LONG_STRING, idata->conn) < 0) { dprint (1, (debugfile, "Error receiving server response.\n")); @@ -185,7 +185,7 @@ static int imap_auth_cram_md5 (IMAP_DATA* idata, const char* user, static int imap_auth_anon (IMAP_DATA* idata) { char ibuf[LONG_STRING], obuf[LONG_STRING]; - char seq[16]; + char seq[SEQLEN+1]; dprint (2, (debugfile, "Attempting anonymous login...\n")); mutt_message _("Authenticating (anonymous)..."); @@ -193,7 +193,7 @@ static int imap_auth_anon (IMAP_DATA* idata) snprintf (obuf, LONG_STRING, "%s AUTHENTICATE ANONYMOUS\r\n", seq); mutt_socket_write (idata->conn, obuf); - if (mutt_socket_read_line_d (ibuf, LONG_STRING, idata->conn) < 0) + if (mutt_socket_readln (ibuf, LONG_STRING, idata->conn) < 0) { dprint (1, (debugfile, "Error receiving server response.\n")); @@ -211,7 +211,7 @@ static int imap_auth_anon (IMAP_DATA* idata) mutt_socket_write (idata->conn, ibuf); - if (mutt_socket_read_line_d (ibuf, LONG_STRING, idata->conn) < 0) + if (mutt_socket_readln (ibuf, LONG_STRING, idata->conn) < 0) { dprint (1, (debugfile, "Error receiving server response.\n")); @@ -352,12 +352,11 @@ int imap_authenticate (IMAP_DATA *idata, CONNECTION *conn) if (!ImapPass) { pass[0]=0; - snprintf (buf, sizeof (buf), _("Password for %s@%s: "), user, conn->mx.host); + snprintf (buf, sizeof (buf), _("Password for %s@%s: "), user, + conn->mx.host); if (mutt_get_field (buf, pass, sizeof (pass), M_PASS) != 0 || !pass[0]) - { - return (-1); - } + return -1; } else strfcpy (pass, ImapPass, sizeof (pass)); @@ -369,8 +368,18 @@ int imap_authenticate (IMAP_DATA *idata, CONNECTION *conn) imap_quote_string (q_pass, sizeof (q_pass), pass); mutt_message _("Logging in..."); + +#ifdef DEBUG + /* don't print the password unless we're at the ungodly debugging level + * of 5 or higher */ + + if (debuglevel < IMAP_LOG_PASS) + dprint (2, (debugfile, "Sending LOGIN command for %s...\n", user)); +#endif + snprintf (buf, sizeof (buf), "LOGIN %s %s", q_user, q_pass); - r = imap_exec (buf, sizeof (buf), idata, buf, IMAP_OK_FAIL); + r = imap_exec (buf, sizeof (buf), idata, buf, + IMAP_CMD_FAIL_OK | IMAP_CMD_PASS); if (r == -1) { /* connection or protocol problem */ diff --git a/imap/auth_gss.c b/imap/auth_gss.c index 1b3e8550..d334a0ad 100644 --- a/imap/auth_gss.c +++ b/imap/auth_gss.c @@ -86,7 +86,7 @@ int imap_auth_gss (IMAP_DATA* idata, const char* user) mutt_socket_write (idata->conn, buf1); /* expect a null continuation response ("+") */ - if (mutt_socket_read_line_d (buf1, sizeof (buf1), idata->conn) < 0) + if (mutt_socket_readln (buf1, sizeof (buf1), idata->conn) < 0) { dprint (1, (debugfile, "Error receiving server response.\n")); gss_release_name (&min_stat, &target_name); @@ -128,7 +128,7 @@ int imap_auth_gss (IMAP_DATA* idata, const char* user) gss_release_name (&min_stat, &target_name); /* end authentication attempt */ mutt_socket_write (idata->conn, "*\r\n"); - mutt_socket_read_line_d (buf1, sizeof (buf1), idata->conn); + mutt_socket_readln (buf1, sizeof (buf1), idata->conn); return -1; } @@ -142,7 +142,7 @@ int imap_auth_gss (IMAP_DATA* idata, const char* user) if (maj_stat == GSS_S_CONTINUE_NEEDED) { - if (mutt_socket_read_line_d (buf1, sizeof (buf1), idata->conn) < 0) + if (mutt_socket_readln (buf1, sizeof (buf1), idata->conn) < 0) { dprint (1, (debugfile, "Error receiving server response.\n")); gss_release_name (&min_stat, &target_name); @@ -160,7 +160,7 @@ int imap_auth_gss (IMAP_DATA* idata, const char* user) gss_release_name (&min_stat, &target_name); /* get security flags and buffer size */ - if (mutt_socket_read_line_d (buf1, sizeof (buf1), idata->conn) < 0) + if (mutt_socket_readln (buf1, sizeof (buf1), idata->conn) < 0) { dprint (1, (debugfile, "Error receiving server response.\n")); @@ -226,7 +226,7 @@ int imap_auth_gss (IMAP_DATA* idata, const char* user) mutt_socket_write (idata->conn, buf1); /* Joy of victory or agony of defeat? */ - if (mutt_socket_read_line_d (buf1, GSS_BUFSIZE, idata->conn) < 0) + if (mutt_socket_readln (buf1, GSS_BUFSIZE, idata->conn) < 0) { dprint (1, (debugfile, "Error receiving server response.\n")); diff --git a/imap/browse.c b/imap/browse.c index 5e6c85b6..f08414b8 100644 --- a/imap/browse.c +++ b/imap/browse.c @@ -394,7 +394,7 @@ static int get_namespace (IMAP_DATA *idata, char *nsbuf, int nsblen, mutt_socket_write (idata->conn, buf); do { - if (mutt_socket_read_line_d (buf, sizeof (buf), idata->conn) < 0) + if (mutt_socket_readln (buf, sizeof (buf), idata->conn) < 0) return -1; if (buf[0] == '*') diff --git a/imap/command.c b/imap/command.c index 28c6bee9..97763670 100644 --- a/imap/command.c +++ b/imap/command.c @@ -101,9 +101,11 @@ int imap_code (const char *s) } /* imap_exec: execute a command, and wait for the response from the server. - * Also, handle untagged responses - * If flags == IMAP_OK_FAIL, then the calling procedure can handle a response - * failing, this is used for checking for a mailbox on append and login + * Also, handle untagged responses. + * Flags: + * IMAP_CMD_FAIL_OK: the calling procedure can handle failure. This is used + * for checking for a mailbox on append and login + * IMAP_CMD_PASS: command contains a password. Suppress logging. * Return 0 on success, -1 on Failure, -2 on OK Failure */ int imap_exec (char* buf, size_t buflen, IMAP_DATA* idata, const char* cmd, @@ -120,17 +122,18 @@ int imap_exec (char* buf, size_t buflen, IMAP_DATA* idata, const char* cmd, out = (char*) safe_malloc (outlen); snprintf (out, outlen, "%s %s\r\n", seq, cmd); - mutt_socket_write (idata->conn, out); + mutt_socket_write_d (idata->conn, out, + flags & IMAP_CMD_PASS ? IMAP_LOG_PASS : IMAP_LOG_CMD); safe_free ((void**) &out); do { - if (mutt_socket_read_line_d (buf, buflen, idata->conn) < 0) - return (-1); + if (mutt_socket_readln (buf, buflen, idata->conn) < 0) + return -1; if (buf[0] == '*' && imap_handle_untagged (idata, buf) != 0) - return (-1); + return -1; } while (mutt_strncmp (buf, seq, SEQLEN) != 0); @@ -140,15 +143,17 @@ int imap_exec (char* buf, size_t buflen, IMAP_DATA* idata, const char* cmd, { char *pc; - if (flags == IMAP_OK_FAIL) - return (-2); - dprint (1, (debugfile, "imap_exec(): command failed: %s\n", buf)); + if (flags & IMAP_CMD_FAIL_OK) + return -2; + + dprint (1, (debugfile, "imap_exec: command failed: %s\n", buf)); pc = buf + SEQLEN; SKIPWS (pc); pc = imap_next_word (pc); mutt_error (pc); sleep (1); - return (-1); + + return -1; } return 0; diff --git a/imap/imap.c b/imap/imap.c index 67a8ebbc..7ca19053 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -127,9 +127,10 @@ int imap_read_bytes (FILE *fp, CONNECTION *conn, long bytes) for (pos = 0; pos < bytes; ) { - len = mutt_socket_read_line (buf, sizeof (buf), conn); + len = mutt_socket_readln_d (buf, sizeof (buf), conn, IMAP_LOG_HDR); if (len < 0) - return (-1); + return -1; + pos += len; fputs (buf, fp); fputs ("\n", fp); @@ -212,7 +213,7 @@ int imap_reopen_mailbox (CONTEXT *ctx, int *index_hint) do { - if (mutt_socket_read_line_d (buf, sizeof (buf), CTX_DATA->conn) < 0) + if (mutt_socket_readln (buf, sizeof (buf), CTX_DATA->conn) < 0) break; if (buf[0] == '*') @@ -353,7 +354,7 @@ static int imap_get_delim (IMAP_DATA *idata, CONNECTION *conn) do { - if (mutt_socket_read_line_d (buf, sizeof (buf), conn) < 0) + if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) { return (-1); } @@ -426,7 +427,7 @@ int imap_open_connection (IMAP_DATA *idata, CONNECTION *conn) idata->state = IMAP_CONNECTED; - if (mutt_socket_read_line_d (buf, sizeof (buf), conn) < 0) + if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) { mutt_socket_close (conn); idata->state = IMAP_DISCONNECTED; @@ -590,7 +591,7 @@ int imap_open_mailbox (CONTEXT *ctx) { char *pc; - if (mutt_socket_read_line_d (buf, sizeof (buf), conn) < 0) + if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) break; if (buf[0] == '*') @@ -791,30 +792,27 @@ int imap_open_mailbox_append (CONTEXT *ctx) } else { - /* STATUS not supported - * The thing to do seems to be: - * - Open a *new* IMAP session, select, and then close it. Report the - * error if the mailbox did not exist. */ + /* STATUS not supported */ mutt_message _("Unable to append to IMAP mailboxes at this server"); + return -1; } - r = imap_exec (buf, sizeof (buf), idata, buf, IMAP_OK_FAIL); + r = imap_exec (buf, sizeof (buf), idata, buf, IMAP_CMD_FAIL_OK); if (r == -2) { /* command failed cause folder doesn't exist */ snprintf (buf, sizeof (buf), _("Create %s?"), mailbox); if (option (OPTCONFIRMCREATE) && mutt_yesorno (buf, 1) < 1) - return (-1); + return -1; if (imap_create_mailbox (ctx, mailbox) < 0) - return (-1); + return -1; } else if (r == -1) - { /* Hmm, some other failure */ return -1; - } + return 0; } @@ -833,7 +831,7 @@ int imap_close_connection (CONTEXT *ctx) mutt_socket_write (CTX_DATA->conn, buf); do { - if (mutt_socket_read_line_d (buf, sizeof (buf), CTX_DATA->conn) < 0) + if (mutt_socket_readln (buf, sizeof (buf), CTX_DATA->conn) < 0) break; } while (mutt_strncmp (seq, buf, SEQLEN) != 0); @@ -1254,7 +1252,7 @@ int imap_mailbox_check (char *path, int new) do { - if (mutt_socket_read_line_d (buf, sizeof (buf), conn) < 0) + if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) return -1; if (buf[0] == '*') @@ -1300,10 +1298,9 @@ int imap_parse_list_response(CONNECTION *conn, char *buf, int buflen, long bytes; *name = NULL; - if (mutt_socket_read_line_d (buf, buflen, conn) < 0) - { - return (-1); - } + + if (mutt_socket_readln (buf, buflen, conn) < 0) + return -1; if (buf[0] == '*') { @@ -1349,10 +1346,10 @@ int imap_parse_list_response(CONNECTION *conn, char *buf, int buflen, int len; if (imap_get_literal_count(buf, &bytes) < 0) - return (-1); - len = mutt_socket_read_line (buf, buflen, conn); + return -1; + len = mutt_socket_readln (buf, buflen, conn); if (len < 0) - return (-1); + return -1; *name = buf; } else @@ -1361,10 +1358,11 @@ int imap_parse_list_response(CONNECTION *conn, char *buf, int buflen, else { if (imap_handle_untagged (idata, buf) != 0) - return (-1); + return -1; } } - return (0); + + return 0; } int imap_subscribe (char *path, int subscribe) diff --git a/imap/imap_private.h b/imap/imap_private.h index ae5847da..08fd3412 100644 --- a/imap/imap_private.h +++ b/imap/imap_private.h @@ -27,6 +27,12 @@ #define IMAP_PORT 143 #define IMAP_SSL_PORT 993 +/* logging levels */ +#define IMAP_LOG_CMD 2 +#define IMAP_LOG_HDR 3 +#define IMAP_LOG_BODY 4 +#define IMAP_LOG_PASS 5 + /* number of entries in the hash table */ #define IMAP_CACHE_LEN 10 @@ -42,13 +48,16 @@ #define IMAP_REOPEN_PENDING (1<<1) #define IMAP_NEWMAIL_PENDING (1<<2) +/* imap_exec flags (see imap_exec) */ +#define IMAP_CMD_FAIL_OK (1<<0) +#define IMAP_CMD_PASS (1<<1) + enum { IMAP_FATAL = 1, IMAP_NEW_MAIL, IMAP_EXPUNGE, IMAP_BYE, - IMAP_OK_FAIL, IMAP_REOPENED, IMAP_LOGOUT }; diff --git a/imap/imap_socket.h b/imap/imap_socket.h index d868fd6e..a5145427 100644 --- a/imap/imap_socket.h +++ b/imap/imap_socket.h @@ -23,12 +23,13 @@ typedef struct _connection { IMAP_MBOX mx; - char *preconnect; /* Actually specific to server, not connection */ - int fd; char inbuf[LONG_STRING]; int bufpos; + + int fd; int available; void *data; + struct _connection *next; void *sockdata; @@ -38,15 +39,17 @@ typedef struct _connection int (*close) (struct _connection *conn); /* status bits */ - int up : 1; + + int up : 1; /* is the connection up? */ } CONNECTION; int mutt_socket_open (CONNECTION* conn); int mutt_socket_close (CONNECTION* conn); int mutt_socket_readchar (CONNECTION *conn, char *c); -int mutt_socket_read_line (char *buf, size_t buflen, CONNECTION *conn); -int mutt_socket_read_line_d (char *buf, size_t buflen, CONNECTION *conn); -int mutt_socket_write (CONNECTION *conn, const char *buf); +#define mutt_socket_readln(A,B,C) mutt_socket_readln_d(A,B,C,IMAP_LOG_CMD) +int mutt_socket_readln_d (char *buf, size_t buflen, CONNECTION *conn, int dbg); +#define mutt_socket_write(A,B) mutt_socket_write_d(A,B,IMAP_LOG_CMD); +int mutt_socket_write_d (CONNECTION *conn, const char *buf, int dbg); CONNECTION* mutt_socket_find (const IMAP_MBOX* mx, int newconn); diff --git a/imap/message.c b/imap/message.c index 197edba2..c6b46b7f 100644 --- a/imap/message.c +++ b/imap/message.c @@ -116,7 +116,7 @@ int imap_read_headers (CONTEXT *ctx, int msgbegin, int msgend) do { - if (mutt_socket_read_line_d (buf, sizeof (buf), CTX_DATA->conn) < 0) + if (mutt_socket_readln (buf, sizeof (buf), CTX_DATA->conn) < 0) { fclose (fp); return -1; @@ -174,7 +174,7 @@ int imap_read_headers (CONTEXT *ctx, int msgbegin, int msgend) * (eg Domino puts FLAGS here). Nothing wrong with that, either. * This all has to go - we should accept literals and nonliterals * interchangeably at any time. */ - if (mutt_socket_read_line_d (buf, sizeof (buf), CTX_DATA->conn) + if (mutt_socket_readln (buf, sizeof (buf), CTX_DATA->conn) < 0) { fclose (fp); @@ -324,7 +324,7 @@ int imap_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno) mutt_socket_write (CTX_DATA->conn, buf); do { - if (mutt_socket_read_line_d (buf, sizeof (buf), CTX_DATA->conn) < 0) + if (mutt_socket_readln (buf, sizeof (buf), CTX_DATA->conn) < 0) goto bail; if (buf[0] == '*') @@ -350,14 +350,16 @@ int imap_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno) } for (pos = 0; pos < bytes; ) { - len = mutt_socket_read_line (buf, sizeof (buf), CTX_DATA->conn); + len = mutt_socket_readln_d (buf, sizeof (buf), CTX_DATA->conn, + 3); if (len < 0) goto bail; pos += len; fputs (buf, msg->fp); fputs ("\n", msg->fp); } - if (mutt_socket_read_line (buf, sizeof (buf), CTX_DATA->conn) < 0) + if (mutt_socket_readln_d (buf, sizeof (buf), CTX_DATA->conn, 3) + < 0) goto bail; pc = buf; } @@ -455,7 +457,8 @@ bail: unlink (cache->path); FREE (&cache->path); } - return (-1); + + return -1; } int imap_append_message (CONTEXT *ctx, MESSAGE *msg) @@ -499,7 +502,7 @@ int imap_append_message (CONTEXT *ctx, MESSAGE *msg) do { - if (mutt_socket_read_line_d (buf, sizeof (buf), CTX_DATA->conn) < 0) + if (mutt_socket_readln (buf, sizeof (buf), CTX_DATA->conn) < 0) { fclose (fp); return (-1); @@ -549,7 +552,7 @@ int imap_append_message (CONTEXT *ctx, MESSAGE *msg) do { - if (mutt_socket_read_line_d (buf, sizeof (buf), CTX_DATA->conn) < 0) + if (mutt_socket_readln (buf, sizeof (buf), CTX_DATA->conn) < 0) return (-1); if (buf[0] == '*' && imap_handle_untagged (CTX_DATA, buf) != 0) @@ -624,7 +627,7 @@ int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete) /* let's get it on */ strncpy (mbox, cmd, sizeof (mbox)); snprintf (cmd, sizeof (cmd), "COPY %s \"%s\"", buf, mbox); - rc = imap_exec (buf, sizeof (buf), CTX_DATA, cmd, IMAP_OK_FAIL); + rc = imap_exec (buf, sizeof (buf), CTX_DATA, cmd, IMAP_CMD_FAIL_OK); if (rc == -2) { /* bail out if command failed for reasons other than nonexistent target */ diff --git a/imap/socket.c b/imap/socket.c index adf7b3e5..d0613156 100644 --- a/imap/socket.c +++ b/imap/socket.c @@ -62,9 +62,10 @@ int mutt_socket_close (CONNECTION* conn) return conn->close (conn); } -int mutt_socket_write (CONNECTION *conn, const char *buf) +int mutt_socket_write_d (CONNECTION *conn, const char *buf, int dbg) { - dprint (1,(debugfile,"> %s", buf)); + dprint (dbg, (debugfile,"> %s", buf)); + return conn->write (conn, buf); } @@ -83,7 +84,7 @@ int mutt_socket_readchar (CONNECTION *conn, char *c) return 1; } -int mutt_socket_read_line (char *buf, size_t buflen, CONNECTION *conn) +int mutt_socket_readln_d (char* buf, size_t buflen, CONNECTION* conn, int dbg) { char ch; int i; @@ -100,14 +101,10 @@ int mutt_socket_read_line (char *buf, size_t buflen, CONNECTION *conn) buf[i-1] = '\0'; else buf[i] = '\0'; - return (i + 1); -} -int mutt_socket_read_line_d (char *buf, size_t buflen, CONNECTION *conn) -{ - int r = mutt_socket_read_line (buf, buflen, conn); - dprint (1,(debugfile,"< %s\n", buf)); - return r; + dprint (dbg, (debugfile, "< %s\n", buf)); + + return (i + 1); } CONNECTION* mutt_socket_find (const IMAP_MBOX* mx, int newconn) @@ -174,7 +171,7 @@ void imap_logout_all (void) do { - if (mutt_socket_read_line_d (buf, sizeof (buf), conn) < 0) + if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) break; } while (mutt_strncmp (buf, seq, SEQLEN) != 0); @@ -229,7 +226,6 @@ static CONNECTION* socket_new_conn () CONNECTION* conn; conn = (CONNECTION *) safe_calloc (1, sizeof (CONNECTION)); - conn->preconnect = safe_strdup (ImapPreconnect); conn->fd = -1; return conn; @@ -257,8 +253,7 @@ int raw_socket_open (CONNECTION *conn) struct sockaddr_in sin; struct hostent *he; int verbose; - char *pc = conn->preconnect; - int do_preconnect = (pc && strlen (pc) > 0); + int do_preconnect = mutt_strlen (ImapPreconnect) > 0; /* This might be a config variable */ int first_try_without_preconnect = TRUE; @@ -286,15 +281,16 @@ int raw_socket_open (CONNECTION *conn) { int ret; - dprint (1,(debugfile,"Preconnect to server %s:\n", conn->mx.host)); - dprint (1,(debugfile,"\t%s\n", conn->preconnect)); + dprint (1, (debugfile, "Preconnect to server %s:\n", conn->mx.host)); + dprint (1, (debugfile, "\t%s\n", ImapPreconnect)); /* Execute preconnect command */ - ret = mutt_system (conn->preconnect) < 0; - dprint (1,(debugfile,"\t%s: %d\n", "Exit status", ret)); + ret = mutt_system (ImapPreconnect) < 0; + dprint (1, (debugfile, "\t%s: %d\n", "Exit status", ret)); if (ret < 0) { - mutt_perror(_("preconnect command failed")); + mutt_perror (_("IMAP Preconnect command failed")); sleep (1); + return ret; } }