imap-send: use git-credential

git-imap-send was directly prompting for a password rather than using git-credential. git-send-email, on the other hand, supports git-credential. This is a necessary improvement for users that use two factor authentication, as they should not be expected to remember all of their app specific passwords. Signed-off-by: Dan Albert <danalbert@google.com> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Dan Albert committed Apr 28, 2014 at 20:00 UTC 791643a865ac15a381e629a35f252293911b0fb0
1 file changed +26 -19
imap-send.c
+26 -19
@@ -23,9 +23,9 @@
23 */
24
25 #include "cache.h"
26 +#include "credential.h"
27 #include "exec_cmd.h"
28 #include "run-command.h"
28 -#include "prompt.h"
29 #ifdef NO_OPENSSL
30 typedef void *SSL;
31 #endif
@@ -946,6 +946,7 @@ static int auth_cram_md5(struct imap_store *ctx, struct imap_cmd *cmd, const cha
946
947 static struct imap_store *imap_open_store(struct imap_server_conf *srvc)
948 {
949 + struct credential cred = CREDENTIAL_INIT;
950 struct imap_store *ctx;
951 struct imap *imap;
952 char *arg, *rsp;
@@ -1096,25 +1097,23 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc)
1097 }
1098 #endif
1099 imap_info("Logging in...\n");
1099 - if (!srvc->user) {
1100 - fprintf(stderr, "Skipping server %s, no user\n", srvc->host);
1101 - goto bail;
1102 - }
1103 - if (!srvc->pass) {
1104 - struct strbuf prompt = STRBUF_INIT;
1105 - strbuf_addf(&prompt, "Password (%s@%s): ", srvc->user, srvc->host);
1106 - arg = git_getpass(prompt.buf);
1107 - strbuf_release(&prompt);
1108 - if (!*arg) {
1109 - fprintf(stderr, "Skipping account %s@%s, no password\n", srvc->user, srvc->host);
1110 - goto bail;
1111 - }
1112 - /*
1113 - * getpass() returns a pointer to a static buffer. make a copy
1114 - * for long term storage.
1115 - */
1116 - srvc->pass = xstrdup(arg);
1100 + if (!srvc->user || !srvc->pass) {
1101 + cred.protocol = xstrdup(srvc->use_ssl ? "imaps" : "imap");
1102 + cred.host = xstrdup(srvc->host);
1103 +
1104 + if (srvc->user)
1105 + cred.username = xstrdup(srvc->user);
1106 + if (srvc->pass)
1107 + cred.password = xstrdup(srvc->pass);
1108 +
1109 + credential_fill(&cred);
1110 +
1111 + if (!srvc->user)
1112 + srvc->user = xstrdup(cred.username);
1113 + if (!srvc->pass)
1114 + srvc->pass = xstrdup(cred.password);
1115 }
1116 +
1117 if (CAP(NOLOGIN)) {
1118 fprintf(stderr, "Skipping account %s@%s, server forbids LOGIN\n", srvc->user, srvc->host);
1119 goto bail;
@@ -1153,10 +1152,18 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc)
1152 }
1153 } /* !preauth */
1154
1155 + if (cred.username)
1156 + credential_approve(&cred);
1157 + credential_clear(&cred);
1158 +
1159 ctx->prefix = "";
1160 return ctx;
1161
1162 bail:
1163 + if (cred.username)
1164 + credential_reject(&cred);
1165 + credential_clear(&cred);
1166 +
1167 imap_close_store(ctx);
1168 return NULL;
1169 }