This patch series does the following things: Firstly it basically makes the imap-send command usable again since it was broken because of not being able to correctly parse the config file. Further it adds support for OAuth2.0 and PLAIN authentication to git imap-send. Last, it does some minor improvements including adding the ability to specify the folder using the command line and ability to list the available folders by adding a `--list` option. P.S.: I am surprised this thing even exists xD. v2: - Added support for OAuth2.0 with curl. - Fixed the memory leak in case auth_cram_md5 fails. v3: - Improve wording in first patch - Change misleading message if OAuth2.0 is used without OpenSSL v4: - Add PLAIN authentication mechanism for OpenSSL - Improved wording in the first patch a bit more v5: - Add ability to specify destination folder using the command line - Add ability to set a default between curl and openssl using the config v6: - Fix minor mistakes in --folder documentation v7: - Fix spelling and grammar mistakes in logs shown to the user when running imap-send - Display port alongwith host when git credential is invoked and asks for a password - Display the destination mailbox when sending a message v8: - Drop the patch that enabled user to choose between libcurl and openssl using the config - Add ability to list the available folders by adding a `--list` option v9: - Encourage users to use OAuth2.0 for Gmail (similar change done for send-email docs). v10: - Fix comment styles - Fix failing tests v11: - Use lower case letters for the first word of a sendtence in an error message and avoid using full stops at the end of a sentence. v12: - Gracefully exit PLAIN, CRAM-MD5, OAUTHBEARER and XOAUTH2 authentication methods if OpenSSL support is not compiled in, but is requested by the user. - Use backticks for string literals. - Wrap documentation text to 75 columns. - End the last member of enum CAPABILITY with a trailing comma. v13: - Fix logic error which was using || instead of && when checking if the authentication method is neither XOAUTH2 nor OAUTHBEARER. v14: - Specify why we are not using CURLOPT_PASSWORD for OAuth2.0 methods using a comment. - Add a function try_auth_method() to reduce code duplication when trying to authenticate using a specific method. Aditya Garg (10): imap-send: fix bug causing cfg->folder being set to NULL imap-send: add support for OAuth2.0 authentication imap-send: add PLAIN authentication method to OpenSSL imap-send: fix memory leak in case auth_cram_md5 fails imap-send: gracefully fail if CRAM-MD5 authentication is requested without OpenSSL imap-send: enable specifying the folder using the command line imap-send: fix minor mistakes in the logs imap-send: display port alongwith host when git credential is invoked imap-send: display the destination mailbox when sending a message imap-send: add ability to list the available folders Documentation/config/imap.adoc | 11 +- Documentation/git-imap-send.adoc | 68 ++++- imap-send.c | 412 ++++++++++++++++++++++++++----- 3 files changed, 414 insertions(+), 77 deletions(-) Range-diff against v13: -: ---------- > 1: 3e3ddf7077 imap-send: fix bug causing cfg->folder being set to NULL 1: 0d28e337cf ! 2: 34d56c3b57 imap-send: add support for OAuth2.0 authentication @@ imap-send.c: static char *cram(const char *challenge_64 UNUSED, #endif static int auth_cram_md5(struct imap_store *ctx, const char *prompt) +@@ imap-send.c: static void server_fill_credential(struct imap_server_conf *srvc, struct credent + srvc->pass = xstrdup(cred->password); + } + ++static int try_auth_method(struct imap_server_conf *srvc, ++ struct imap_store *ctx, ++ struct imap *imap, ++ const char *auth_method, ++ enum CAPABILITY cap, ++ int (*fn)(struct imap_store *, const char *)) ++{ ++ struct imap_cmd_cb cb = {0}; ++ ++ if (!CAP(cap)) { ++ fprintf(stderr, "You specified " ++ "%s as authentication method, " ++ "but %s doesn't support it.\n", ++ auth_method, srvc->host); ++ return -1; ++ } ++ cb.cont = fn; ++ ++ if (NOT_CONSTANT(!cb.cont)) { ++ fprintf(stderr, "If you want to use %s authentication mechanism, " ++ "you have to build git-imap-send with OpenSSL library.", ++ auth_method); ++ return -1; ++ } ++ if (imap_exec(ctx, &cb, "AUTHENTICATE %s", auth_method) != RESP_OK) { ++ fprintf(stderr, "IMAP error: AUTHENTICATE %s failed\n", ++ auth_method); ++ return -1; ++ } ++ return 0; ++} ++ + static struct imap_store *imap_open_store(struct imap_server_conf *srvc, const char *folder) + { + struct credential cred = CREDENTIAL_INIT; @@ imap-send.c: static struct imap_store *imap_open_store(struct imap_server_conf *srvc, const c fprintf(stderr, "IMAP error: AUTHENTICATE CRAM-MD5 failed\n"); goto bail; } + } else if (!strcmp(srvc->auth_method, "OAUTHBEARER")) { -+ if (!CAP(AUTH_OAUTHBEARER)) { -+ fprintf(stderr, "You specified " -+ "OAUTHBEARER as authentication method, " -+ "but %s doesn't support it.\n", srvc->host); ++ if (try_auth_method(srvc, ctx, imap, "OAUTHBEARER", AUTH_OAUTHBEARER, auth_oauthbearer)) + goto bail; -+ } -+ -+ #ifdef NO_OPENSSL -+ fprintf(stderr, "You are trying to use OAUTHBEARER authentication mechanism " -+ "with OpenSSL library, but its support has not been compiled in."); -+ goto bail; -+ #endif -+ -+ /* OAUTHBEARER */ -+ -+ memset(&cb, 0, sizeof(cb)); -+ cb.cont = auth_oauthbearer; -+ if (imap_exec(ctx, &cb, "AUTHENTICATE OAUTHBEARER") != RESP_OK) { -+ fprintf(stderr, "IMAP error: AUTHENTICATE OAUTHBEARER failed\n"); -+ goto bail; -+ } + } else if (!strcmp(srvc->auth_method, "XOAUTH2")) { -+ if (!CAP(AUTH_XOAUTH2)) { -+ fprintf(stderr, "You specified " -+ "XOAUTH2 as authentication method, " -+ "but %s doesn't support it.\n", srvc->host); -+ goto bail; -+ } -+ -+ #ifdef NO_OPENSSL -+ fprintf(stderr, "You are trying to use XOAUTH2 authentication mechanism " -+ "with OpenSSL library, but its support has not been compiled in."); -+ goto bail; -+ #endif -+ -+ /* XOAUTH2 */ -+ -+ memset(&cb, 0, sizeof(cb)); -+ cb.cont = auth_xoauth2; -+ if (imap_exec(ctx, &cb, "AUTHENTICATE XOAUTH2") != RESP_OK) { -+ fprintf(stderr, "IMAP error: AUTHENTICATE XOAUTH2 failed\n"); ++ if (try_auth_method(srvc, ctx, imap, "XOAUTH2", AUTH_XOAUTH2, auth_xoauth2)) + goto bail; -+ } } else { fprintf(stderr, "Unknown authentication method:%s\n", srvc->host); goto bail; @@ imap-send.c: static CURL *setup_curl(struct imap_server_conf *srvc, struct crede curl_easy_setopt(curl, CURLOPT_USERNAME, srvc->user); - curl_easy_setopt(curl, CURLOPT_PASSWORD, srvc->pass); + ++ /* ++ * Use CURLOPT_PASSWORD irrespective of whether there is ++ * an auth method specified or not, unless it's OAuth2.0, ++ * where we use CURLOPT_XOAUTH2_BEARER. ++ */ + if (!srvc->auth_method || + (strcmp(srvc->auth_method, "XOAUTH2") && + strcmp(srvc->auth_method, "OAUTHBEARER"))) 2: d934bdcb82 ! 3: 69fb8f63f1 imap-send: add PLAIN authentication method to OpenSSL @@ imap-send.c: static struct imap_store *imap_open_store(struct imap_server_conf * - if (!strcmp(srvc->auth_method, "CRAM-MD5")) { + if (!strcmp(srvc->auth_method, "PLAIN")) { -+ if (!CAP(AUTH_PLAIN)) { -+ fprintf(stderr, "You specified " -+ "PLAIN as authentication method, " -+ "but %s doesn't support it.\n", srvc->host); ++ if (try_auth_method(srvc, ctx, imap, "PLAIN", AUTH_PLAIN, auth_plain)) + goto bail; -+ } -+ -+ #ifdef NO_OPENSSL -+ fprintf(stderr, "You are trying to use PLAIN authentication mechanism " -+ "with OpenSSL library, but its support has not been compiled in."); -+ goto bail; -+ #endif -+ -+ /* PLAIN */ -+ -+ memset(&cb, 0, sizeof(cb)); -+ cb.cont = auth_plain; -+ if (imap_exec(ctx, &cb, "AUTHENTICATE PLAIN") != RESP_OK) { -+ fprintf(stderr, "IMAP error: AUTHENTICATE PLAIN failed\n"); -+ goto bail; -+ } + } else if (!strcmp(srvc->auth_method, "CRAM-MD5")) { if (!CAP(AUTH_CRAM_MD5)) { fprintf(stderr, "You specified " 3: f2773c646f = 4: 1510127888 imap-send: fix memory leak in case auth_cram_md5 fails 4: c111ee6bc1 ! 5: 731fcbb602 imap-send: gracefully fail if CRAM-MD5 authentication is requested without OpenSSL @@ imap-send.c: static int auth_xoauth2(struct imap_store *ctx, const char *prompt { if (srvc->user && srvc->pass) @@ imap-send.c: static struct imap_store *imap_open_store(struct imap_server_conf *srvc, const c - "but %s doesn't support it.\n", srvc->host); - goto bail; - } -+ -+ #ifdef NO_OPENSSL -+ fprintf(stderr, "If you want to use CRAM-MD5 authentication mechanism, " -+ "you have to build git-imap-send with OpenSSL library."); -+ goto bail; -+ #endif -+ - /* CRAM-MD5 */ + server_fill_credential(srvc, &cred); - memset(&cb, 0, sizeof(cb)); + if (srvc->auth_method) { +- struct imap_cmd_cb cb; +- + if (!strcmp(srvc->auth_method, "PLAIN")) { + if (try_auth_method(srvc, ctx, imap, "PLAIN", AUTH_PLAIN, auth_plain)) + goto bail; + } else if (!strcmp(srvc->auth_method, "CRAM-MD5")) { +- if (!CAP(AUTH_CRAM_MD5)) { +- fprintf(stderr, "You specified " +- "CRAM-MD5 as authentication method, " +- "but %s doesn't support it.\n", srvc->host); +- goto bail; +- } +- /* CRAM-MD5 */ +- +- memset(&cb, 0, sizeof(cb)); +- cb.cont = auth_cram_md5; +- if (imap_exec(ctx, &cb, "AUTHENTICATE CRAM-MD5") != RESP_OK) { +- fprintf(stderr, "IMAP error: AUTHENTICATE CRAM-MD5 failed\n"); ++ if (try_auth_method(srvc, ctx, imap, "CRAM-MD5", AUTH_CRAM_MD5, auth_cram_md5)) + goto bail; +- } + } else if (!strcmp(srvc->auth_method, "OAUTHBEARER")) { + if (try_auth_method(srvc, ctx, imap, "OAUTHBEARER", AUTH_OAUTHBEARER, auth_oauthbearer)) + goto bail; 5: f12713f24b = 6: 36154d3276 imap-send: enable specifying the folder using the command line 6: d38caeae5e ! 7: 85ce1205ca imap-send: fix minor mistakes in the logs @@ imap-send.c: static int ssl_socket_connect(struct imap_socket *sock UNUSED, int use_tls_only UNUSED) { - fprintf(stderr, "SSL requested but SSL support not compiled in\n"); -+ fprintf(stderr, "SSL requested, but SSL support is not compiled in.\n"); ++ fprintf(stderr, "SSL requested, but SSL support is not compiled in\n"); return -1; } @@ imap-send.c: static struct imap_store *imap_open_store(struct imap_server_conf * /* read the greeting string */ @@ imap-send.c: static struct imap_store *imap_open_store(struct imap_server_conf *srvc, const c + if (try_auth_method(srvc, ctx, imap, "XOAUTH2", AUTH_XOAUTH2, auth_xoauth2)) goto bail; - } } else { - fprintf(stderr, "Unknown authentication method:%s\n", srvc->host); + fprintf(stderr, "unknown authentication method:%s\n", srvc->host); 7: 3ba02f2b0c = 8: 8dd19a4613 imap-send: display port alongwith host when git credential is invoked 8: 6dbd0bf0bc = 9: cc1398bb7c imap-send: display the destination mailbox when sending a message 9: f77f2423e1 = 10: 0975df9fc0 imap-send: add ability to list the available folders -- 2.49.0