From 72d0d38a49b89a2da423e01ea21d09d99892a6dc Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Sat, 21 Dec 2019 11:39:54 -0500 Subject: Determine host by SRV lookup --- imbox.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) (limited to 'imbox.c') diff --git a/imbox.c b/imbox.c index 0328c34..f2c65e8 100644 --- a/imbox.c +++ b/imbox.c @@ -26,6 +26,14 @@ #include #include +#ifndef DIG_PATH +# ifdef __FreeBSD__ +# define DIG_PATH "/usr/bin/drill" +# else +# define DIG_PATH "dig" +# endif +#endif + static void compile(regex_t *regex, const char *pattern) { if (regex->re_nsub) return; int error = regcomp(regex, pattern, REG_EXTENDED | REG_NEWLINE); @@ -83,6 +91,56 @@ static void mboxrd(const char *headers, const char *body) { #undef MATCH } +static void lookup(const char **host, const char **port, const char *domain) { + static char buf[1024]; + snprintf(buf, sizeof(buf), "_imaps._tcp.%s", domain); + + int rw[2]; + int error = pipe(rw); + if (error) err(EX_OSERR, "pipe"); + + pid_t pid = fork(); + if (pid < 0) err(EX_OSERR, "fork"); + + if (!pid) { + close(rw[0]); + dup2(rw[1], STDOUT_FILENO); + dup2(rw[1], STDERR_FILENO); + close(rw[1]); + execlp(DIG_PATH, DIG_PATH, "-t", "SRV", "-q", buf, "+short", NULL); + err(EX_CONFIG, "%s", DIG_PATH); + } + + int status; + pid = wait(&status); + if (pid < 0) err(EX_OSERR, "wait"); + + close(rw[1]); + FILE *pipe = fdopen(rw[0], "r"); + if (!pipe) err(EX_IOERR, "fdopen"); + + fgets(buf, sizeof(buf), pipe); + if (ferror(pipe)) err(EX_IOERR, "fgets"); + fclose(pipe); + + if (!WIFEXITED(status) || WEXITSTATUS(status)) { + fprintf(stderr, "%s", buf); + exit(WEXITSTATUS(status)); + } + + char *ptr = buf; + char *dot = strrchr(ptr, '.'); + if (dot) *dot = '\0'; + strsep(&ptr, " \n"); // priority + strsep(&ptr, " \n"); // weight + *port = strsep(&ptr, " \n"); + *host = strsep(&ptr, " \n"); + if (!*host) { + *host = domain; + *port = "imaps"; + } +} + static bool verbose; int tlsRead(void *_tls, char *ptr, int len) { @@ -165,7 +223,7 @@ static char *readLiteral(FILE *imap, const char *line) { } int main(int argc, char *argv[]) { - const char *host = "imap.fastmail.com"; + const char *host = NULL; const char *port = "imaps"; const char *mailbox = "INBOX"; const char *subject = "[PATCH"; @@ -193,6 +251,12 @@ int main(int argc, char *argv[]) { const char *user = argv[optind]; if (!user) errx(EX_USAGE, "username required"); + if (!host) { + const char *domain = strchr(user, '@'); + if (!domain) errx(EX_USAGE, "no domain in username"); + lookup(&host, &port, &domain[1]); + } + char buf[1024]; char *pass = readpassphrase( (rppFlags & RPP_STDIN ? "" : "Password: "), -- cgit 1.4.1