From dca3eb7a6a0c7e8a71d26c4390d908f5f6c2f32f Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Wed, 8 Apr 2020 16:49:50 -0400 Subject: Use a real IMAP parser --- .gitignore | 1 + Makefile | 10 ++- imap.c | 217 +++++++++++++++++++++++++++++++++++++++++++++++++ imap.h | 129 +++++++++++++++++++++++++++++ imbox.c | 267 +++++++++++++++++++------------------------------------------ 5 files changed, 440 insertions(+), 184 deletions(-) create mode 100644 imap.c create mode 100644 imap.h diff --git a/.gitignore b/.gitignore index 0084246..10ce7e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.o config.mk git-fetch-email imbox diff --git a/Makefile b/Makefile index 2a9d1d9..b9b2152 100644 --- a/Makefile +++ b/Makefile @@ -12,10 +12,18 @@ MANS = ${BINS:=.1} -include config.mk +OBJS += imap.o +OBJS += imbox.o + all: ${BINS} +imbox: ${OBJS} + ${CC} ${LDFLAGS} ${OBJS} ${LDLIBS} -o $@ + +${OBJS}: imap.h + clean: - rm -f ${BINS} + rm -f ${BINS} ${OBJS} install: ${BINS} ${MANS} install -d ${PREFIX}/bin ${MANDIR}/man1 diff --git a/imap.c b/imap.c new file mode 100644 index 0000000..a5daaf6 --- /dev/null +++ b/imap.c @@ -0,0 +1,217 @@ +/* Copyright (C) 2020 C. McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "compat.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "imap.h" + +const char *Atoms[AtomCap] = { +#define X(id, str) [id] = str, + ENUM_ATOM +#undef X +}; + +bool imapVerbose; + +static int imapRead(void *_tls, char *ptr, int len) { + struct tls *tls = _tls; + ssize_t ret; + do { + ret = tls_read(tls, ptr, len); + } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT); + if (ret < 0) errx(EX_IOERR, "tls_read: %s", tls_error(tls)); + if (imapVerbose) fprintf(stderr, "%.*s", (int)ret, ptr); + return ret; +} + +static int imapWrite(void *_tls, const char *ptr, int len) { + struct tls *tls = _tls; + ssize_t ret; + do { + ret = tls_write(tls, ptr, len); + } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT); + if (ret < 0) errx(EX_IOERR, "tls_write: %s", tls_error(tls)); + if (imapVerbose) fprintf(stderr, "%.*s", (int)ret, ptr); + return ret; +} + +static int imapClose(void *_tls) { + struct tls *tls = _tls; + int error = tls_close(tls); + if (error) errx(EX_IOERR, "tls_close: %s", tls_error(tls)); + return error; +} + +FILE *imapOpen(const char *host, const char *port) { + struct tls *client = tls_client(); + if (!client) errx(EX_SOFTWARE, "tls_client"); + + struct tls_config *config = tls_config_new(); + if (!config) errx(EX_SOFTWARE, "tls_config_new"); + + int error = tls_configure(client, config); + if (error) errx(EX_SOFTWARE, "tls_configure: %s", tls_error(client)); + tls_config_free(config); + + error = tls_connect(client, host, port); + if (error) errx(EX_NOHOST, "tls_connect: %s", tls_error(client)); + + FILE *imap = funopen(client, imapRead, imapWrite, NULL, imapClose); + if (!imap) err(EX_SOFTWARE, "funopen"); + + setlinebuf(imap); + return imap; +} + +static size_t cap; +static char *buf; +static char *ptr; + +static void imapLine(FILE *imap) { + ssize_t len = getline(&buf, &cap, imap); + if (len < 0) errx(EX_PROTOCOL, "unexpected eof"); + if (len < 1 || buf[len - 1] != '\n') errx(EX_PROTOCOL, "missing LF"); + if (len < 2 || buf[len - 2] != '\r') errx(EX_PROTOCOL, "missing CR"); + buf[len - 2] = '\0'; + ptr = buf; +} + +static struct Data parseAtom(void) { + size_t len = strcspn(ptr, " ()[]{\""); + struct Data data = { + .type = Atom, + .atom = atomn(ptr, len), + }; + ptr += len; + return data; +} + +static struct Data parseNumber(void) { + return (struct Data) { + .type = Number, + .number = strtoull(ptr, &ptr, 10), + }; +} + +static struct Data parseQuoted(void) { + ptr++; + size_t len = strcspn(ptr, "\""); + if (ptr[len] != '"') errx(EX_PROTOCOL, "missing quoted string delimiter"); + struct Data data = { + .type = String, + .string = strndup(ptr, len), + }; + if (!data.string) err(EX_OSERR, "strndup"); + ptr += len + 1; + return data; +} + +static struct Data parseLiteral(FILE *imap) { + ptr++; + size_t len = strtoull(ptr, &ptr, 10); + if (*ptr != '}') errx(EX_PROTOCOL, "missing literal prefix delimiter"); + struct Data data = { + .type = String, + .string = malloc(len + 1), + }; + if (!data.string) err(EX_OSERR, "malloc"); + size_t n = fread(data.string, len, 1, imap); + if (!n) errx(EX_PROTOCOL, "truncated literal"); + imapLine(imap); + data.string[len] = '\0'; + return data; +} + +static struct Data parseData(FILE *imap); + +static struct Data parseList(FILE *imap, char close) { + if (*ptr) ptr++; + struct Data data = { .type = List }; + while (*ptr != close) { + if (data.list.len == data.list.cap) { + if (data.list.cap) { + data.list.cap *= 2; + } else { + data.list.cap = 4; + } + data.list.ptr = realloc( + data.list.ptr, sizeof(*data.list.ptr) * data.list.cap + ); + if (!data.list.ptr) err(EX_OSERR, "realloc"); + } + data.list.ptr[data.list.len++] = parseData(imap); + } + if (*ptr) ptr++; + return data; +} + +static struct Data parseData(FILE *imap) { + if (*ptr == ' ') ptr++; + if (*ptr == '"') return parseQuoted(); + if (*ptr == '{') return parseLiteral(imap); + if (*ptr == '(') return parseList(imap, ')'); + if (*ptr == '[') return parseList(imap, ']'); + if (*ptr >= '0' && *ptr <= '9') return parseNumber(); + if (*ptr) return parseAtom(); + errx(EX_PROTOCOL, "unexpected eof"); +} + +struct Resp imapResp(FILE *imap) { + struct Data data; + struct Resp resp = {0}; + imapLine(imap); + + data = parseData(imap); + if (data.type != Atom) errx(EX_PROTOCOL, "expected tag atom"); + resp.tag = data.atom; + + data = parseData(imap); + if (data.type == Number) { + resp.number = data.number; + data = parseData(imap); + } + if (data.type != Atom) errx(EX_PROTOCOL, "expected response atom"); + resp.resp = data.atom; + + if ( + resp.resp == AtomOk || + resp.resp == AtomNo || + resp.resp == AtomBad || + resp.resp == AtomPreauth || + resp.resp == AtomBye + ) { + if (*ptr == ' ') ptr++; + if (*ptr == '[') { + data = parseList(imap, ']'); + resp.code = data.list; + } + if (*ptr == ' ') ptr++; + resp.text = ptr; + } else { + data = parseList(imap, '\0'); + resp.data = data.list; + } + + return resp; +} diff --git a/imap.h b/imap.h new file mode 100644 index 0000000..0c9f6b9 --- /dev/null +++ b/imap.h @@ -0,0 +1,129 @@ +/* Copyright (C) 2020 C. McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +#define ENUM_ATOM \ + X(AtomNil, "NIL") \ + X(AtomOk, "OK") \ + X(AtomNo, "NO") \ + X(AtomBad, "BAD") \ + X(AtomPreauth, "PREAUTH") \ + X(AtomBye, "BYE") \ + X(AtomAlert, "ALERT") \ + X(AtomBadCharset, "BADCHARSET") \ + X(AtomCapability, "CAPABILITY") \ + X(AtomParse, "PARSE") \ + X(AtomPermanentFlags, "PERMANENTFLAGS") \ + X(AtomReadOnly, "READ-ONLY") \ + X(AtomReadWrite, "READ-WRITE") \ + X(AtomTryCreate, "TRYCREATE") \ + X(AtomUIDNext, "UIDNEXT") \ + X(AtomUIDValidity, "UIDVALIDITY") \ + X(AtomUnseen, "UNSEEN") \ + X(AtomList, "LIST") \ + X(AtomLSub, "LSUB") \ + X(AtomStatus, "STATUS") \ + X(AtomSearch, "SEARCH") \ + X(AtomFlags, "FLAGS") \ + X(AtomExists, "EXISTS") \ + X(AtomRecent, "RECENT") \ + X(AtomExpunge, "EXPUNGE") \ + X(AtomFetch, "FETCH") \ + X(AtomUntagged, "*") + +enum Atom { +#define X(id, str) id, + ENUM_ATOM +#undef X + AtomCap = 1024, +}; + +extern const char *Atoms[AtomCap]; + +static inline enum Atom atomn(const char *str, size_t len) { + enum Atom i; + for (i = 0; i < AtomCap; ++i) { + if (!Atoms[i]) break; + if (strlen(Atoms[i]) != len) continue; + if (!strncasecmp(Atoms[i], str, len)) return i; + } + if (i == AtomCap) errx(EX_SOFTWARE, "atom capacity exceeded"); + Atoms[i] = strndup(str, len); + if (!Atoms[i]) err(EX_OSERR, "strndup"); + return i; +} + +static inline enum Atom atom(const char *str) { + return atomn(str, strlen(str)); +} + +struct Data { + enum Type { + Atom, + Number, + String, + List, + } type; + union { + enum Atom atom; + uint32_t number; + char *string; + struct List { + size_t cap; + size_t len; + struct Data *ptr; + } list; + }; +}; + +static inline void dataFree(struct Data data) { + if (data.type == String) free(data.string); + if (data.type == List) { + for (size_t i = 0; i < data.list.len; ++i) { + dataFree(data.list.ptr[i]); + } + free(data.list.ptr); + } +} + +struct Resp { + enum Atom tag; + uint32_t number; + enum Atom resp; + struct List code; + struct List data; + const char *text; +}; + +static inline void respFree(struct Resp resp) { + for (size_t i = 0; i < resp.code.len; ++i) { + dataFree(resp.code.ptr[i]); + } + for (size_t i = 0; i < resp.data.len; ++i) { + dataFree(resp.data.ptr[i]); + } +} + +extern bool imapVerbose; +FILE *imapOpen(const char *host, const char *port); +struct Resp imapResp(FILE *imap); diff --git a/imbox.c b/imbox.c index 24ac67b..64bcfec 100644 --- a/imbox.c +++ b/imbox.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2019 C. McEnroe +/* Copyright (C) 2019, 2020 C. McEnroe * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,19 +18,21 @@ #include #include +#include #include #include #include #include #include #include -#include #include #ifndef NO_READPASSPHRASE_H #include #endif +#include "imap.h" + #if !defined(DIG_PATH) && !defined(DRILL_PATH) # ifdef __FreeBSD__ # define DRILL_PATH "/usr/bin/drill" @@ -39,6 +41,10 @@ # endif #endif +#define FETCH_HEADERS \ + "Date From To Cc Subject Message-Id In-Reply-To References " \ + "Content-Transfer-Encoding" + static void mboxrd(char *headers, char *body) { printf("From mboxrd@z Thu Jan 1 00:00:00 1970\n"); for (char *crlf; (crlf = strstr(headers, "\r\n")); headers = &crlf[2]) { @@ -124,87 +130,6 @@ static void lookup(const char **host, const char **port, const char *domain) { } } -static bool verbose; - -static int tlsRead(void *_tls, char *ptr, int len) { - struct tls *tls = _tls; - ssize_t ret; - do { - ret = tls_read(tls, ptr, len); - } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT); - if (ret < 0) errx(EX_IOERR, "tls_read: %s", tls_error(tls)); - if (verbose) fprintf(stderr, "%.*s", (int)ret, ptr); - return ret; -} - -static int tlsWrite(void *_tls, const char *ptr, int len) { - struct tls *tls = _tls; - ssize_t ret; - do { - ret = tls_write(tls, ptr, len); - } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT); - if (ret < 0) errx(EX_IOERR, "tls_write: %s", tls_error(tls)); - if (verbose) fprintf(stderr, "%.*s", (int)ret, ptr); - return ret; -} - -static int tlsClose(void *_tls) { - struct tls *tls = _tls; - int error = tls_close(tls); - if (error) errx(EX_IOERR, "tls_close: %s", tls_error(tls)); - return error; -} - -#define ENUM_ATOM \ - X(Unknown, "") \ - X(Untagged, "*") \ - X(Ok, "OK") \ - X(No, "NO") \ - X(Bad, "BAD") \ - X(Bye, "BYE") \ - X(Login, "LOGIN") \ - X(Examine, "EXAMINE") \ - X(Search, "SEARCH") \ - X(Fetch, "FETCH") - -enum Atom { -#define X(id, _) id, - ENUM_ATOM -#undef X - AtomsLen, -}; - -static const char *Atoms[AtomsLen] = { -#define X(id, str) [id] = str, - ENUM_ATOM -#undef X -}; - -static enum Atom atom(const char *str) { - if (!str) return Unknown; - for (enum Atom i = 0; i < AtomsLen; ++i) { - if (!strcmp(str, Atoms[i])) return i; - } - return Unknown; -} - -static char *readLiteral(FILE *imap, const char *line) { - char *prefix = strrchr(line, '{'); - if (!prefix) errx(EX_PROTOCOL, "no literal prefix"); - - size_t size = strtoul(prefix + 1, NULL, 10); - if (!size) errx(EX_PROTOCOL, "invalid literal size"); - - char *literal = malloc(size + 1); - if (!literal) err(EX_OSERR, "malloc"); - - size_t count = fread(literal, size, 1, imap); - if (!count) errx(EX_PROTOCOL, "could not read literal"); - - literal[size] = '\0'; - return literal; -} - int main(int argc, char *argv[]) { const char *host = NULL; const char *port = "imaps"; @@ -225,7 +150,7 @@ int main(int argc, char *argv[]) { break; case 'h': host = optarg; break; case 'm': mailbox = optarg; break; case 'p': port = optarg; - break; case 'v': verbose = true; + break; case 'v': imapVerbose = true; break; case 'w': rppFlags |= RPP_STDIN; break; default: return EX_USAGE; } @@ -247,122 +172,98 @@ int main(int argc, char *argv[]) { ); if (!pass) err(EX_UNAVAILABLE, "readpassphrase"); - struct tls *client = tls_client(); - if (!client) errx(EX_SOFTWARE, "tls_client"); - - struct tls_config *config = tls_config_new(); - if (!config) errx(EX_SOFTWARE, "tls_config_new"); - - int error = tls_configure(client, config); - if (error) errx(EX_SOFTWARE, "tls_configure: %s", tls_error(client)); - tls_config_free(config); + enum Atom login = 0; + enum Atom examine = atom("examine"); + enum Atom headerFields = atom("HEADER.FIELDS"); + enum Atom text = atom("TEXT"); - error = tls_connect(client, host, port); - if (error) errx(EX_NOHOST, "tls_connect: %s", tls_error(client)); - - FILE *imap = funopen(client, tlsRead, tlsWrite, NULL, tlsClose); - if (!imap) err(EX_SOFTWARE, "funopen"); - setlinebuf(imap); - - bool login = false; - char *nums = NULL; + FILE *imap = imapOpen(host, port); + for (struct Resp resp; resp = imapResp(imap), resp.resp != AtomBye;) { + if (resp.resp == AtomNo || resp.resp == AtomBad) { + errx(EX_CONFIG, "%s: %s", Atoms[resp.resp], resp.text); + } - char *line = NULL; - size_t cap = 0; - while (0 < getline(&line, &cap, imap)) { - char *cr = strchr(line, '\r'); - if (cr) *cr = '\0'; + if (!login) { + login = atom("login"); + fprintf( + imap, "%s LOGIN \"%s\" \"%s\"\r\n", + Atoms[login], user, pass + ); + } - char *rest = line; - enum Atom tag = atom(strsep(&rest, " ")); - if (rest && isdigit(rest[0])) { - strsep(&rest, " "); + if (resp.tag == login) { + fprintf(imap, "%s EXAMINE \"%s\"\r\n", Atoms[examine], mailbox); } - enum Atom resp = atom(strsep(&rest, " ")); - if (resp == No || resp == Bad || resp == Bye) { - errx( - EX_CONFIG, "%s: %s %s", - Atoms[tag], Atoms[resp], (rest ? rest : "") + if (resp.tag == examine) { + fprintf( + imap, + "%s SEARCH CHARSET UTF-8 OR " + "NOT HEADER Content-Type \"\" " + "HEADER Content-Type \"text/plain\"", + Atoms[AtomSearch] ); + if (subject) fprintf(imap, " SUBJECT \"%s\"", subject); + if (from) fprintf(imap, " FROM \"%s\"", from); + if (to) fprintf(imap, " TO \"%s\"", to); + if (cc) fprintf(imap, " CC \"%s\"", cc); + fprintf(imap, "\r\n"); } - switch (tag) { - break; case Untagged: { - if (login) break; - fprintf( - imap, "%s LOGIN \"%s\" \"%s\"\r\n", - Atoms[Login], user, pass - ); - login = true; - } - break; case Login: { - fprintf(imap, "%s EXAMINE \"%s\"\r\n", Atoms[Examine], mailbox); - } - break; case Examine: { - fprintf( - imap, - "%s SEARCH CHARSET UTF-8 OR " - "NOT HEADER Content-Type \"\" " - "HEADER Content-Type \"text/plain\"", - Atoms[Search] - ); - if (subject) fprintf(imap, " SUBJECT \"%s\"", subject); - if (from) fprintf(imap, " FROM \"%s\"", from); - if (to) fprintf(imap, " TO \"%s\"", to); - if (cc) fprintf(imap, " CC \"%s\"", cc); - fprintf(imap, "\r\n"); - } - break; case Search: { - if (!nums) errx(EX_PROTOCOL, "no search response"); - for (char *ch = nums; *ch; ++ch) { - if (*ch == ' ') *ch = ','; + if (resp.resp == AtomSearch) { + if (!resp.data.len) errx(EX_TEMPFAIL, "no matching messages"); + fprintf(imap, "%s FETCH ", Atoms[AtomFetch]); + for (size_t i = 0; i < resp.data.len; ++i) { + struct Data data = resp.data.ptr[i]; + if (data.type != Number) { + errx(EX_PROTOCOL, "invalid search result"); } - fprintf( - imap, - "%s FETCH %s (BODY[HEADER.FIELDS (" - "Date From To Cc Subject Message-Id In-Reply-To References " - "Content-Transfer-Encoding" - ")] BODY[TEXT])\r\n", - Atoms[Fetch], nums - ); - free(nums); - nums = NULL; + fprintf(imap, "%s%" PRIu32, (i ? "," : ""), data.number); } - break; case Fetch: { - fprintf(imap, "ayy LOGOUT\r\n"); - fclose(imap); - return EX_OK; - } - break; default:; + fprintf( + imap, + " (BODY[HEADER.FIELDS (" FETCH_HEADERS ")] BODY[TEXT])\r\n" + ); } - switch (resp) { - break; case Search: { - if (!rest) errx(EX_TEMPFAIL, "no matching messages"); - nums = strdup(rest); - if (!nums) err(EX_OSERR, "strdup"); + if (resp.resp == AtomFetch) { + if (!resp.data.len) { + errx(EX_PROTOCOL, "no fetch data"); + } + if (resp.data.ptr[0].type != List) { + errx(EX_PROTOCOL, "invalid fetch data"); } - break; case Fetch: { - char *headers = readLiteral(imap, rest); - - ssize_t len = getline(&line, &cap, imap); - if (len <= 0) errx(EX_PROTOCOL, "unexpected eof after headers"); - - char *body = readLiteral(imap, line); - len = getline(&line, &cap, imap); - if (len <= 0) errx(EX_PROTOCOL, "unexpected eof after body"); - if (strcmp(line, ")\r\n")) { - errx(EX_PROTOCOL, "trailing data after headers and body"); + struct Data headers = {0}; + struct Data body = {0}; + struct List items = resp.data.ptr[0].list; + for (size_t i = 0; i < items.len; ++i) { + struct Data item = items.ptr[i]; + if (item.type != List) continue; + if (!item.list.len) continue; + if (item.list.ptr[0].type != Atom) continue; + if (item.list.ptr[0].atom == headerFields) { + if (i + 1 < items.len) headers = items.ptr[i + 1]; } + if (item.list.ptr[0].atom == text) { + if (i + 1 < items.len) body = items.ptr[i + 1]; + } + } - mboxrd(headers, body); - free(headers); - free(body); + if (headers.type != String) { + errx(EX_PROTOCOL, "invalid header data"); } - break; default:; + if (body.type != String) { + errx(EX_PROTOCOL, "invalid body data"); + } + mboxrd(headers.string, body.string); + } + + if (resp.tag == AtomFetch) { + fprintf(imap, "ayy LOGOUT\r\n"); } + + respFree(resp); } - errx(EX_PROTOCOL, "unexpected eof"); + fclose(imap); } -- cgit 1.4.1