Merge branch 'da/imap-send-use-credential-helper'

"git imap-send" learns to ask the credential helper for
authentication material.

* da/imap-send-use-credential-helper:
  imap-send: use git-credential
This commit is contained in:
Junio C Hamano 2014-06-06 11:17:56 -07:00
commit 89080fcd9a

View File

@ -23,9 +23,9 @@
*/ */
#include "cache.h" #include "cache.h"
#include "credential.h"
#include "exec_cmd.h" #include "exec_cmd.h"
#include "run-command.h" #include "run-command.h"
#include "prompt.h"
#ifdef NO_OPENSSL #ifdef NO_OPENSSL
typedef void *SSL; typedef void *SSL;
#endif #endif
@ -946,6 +946,7 @@ static int auth_cram_md5(struct imap_store *ctx, struct imap_cmd *cmd, const cha
static struct imap_store *imap_open_store(struct imap_server_conf *srvc) static struct imap_store *imap_open_store(struct imap_server_conf *srvc)
{ {
struct credential cred = CREDENTIAL_INIT;
struct imap_store *ctx; struct imap_store *ctx;
struct imap *imap; struct imap *imap;
char *arg, *rsp; char *arg, *rsp;
@ -1096,25 +1097,23 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc)
} }
#endif #endif
imap_info("Logging in...\n"); imap_info("Logging in...\n");
if (!srvc->user) { if (!srvc->user || !srvc->pass) {
fprintf(stderr, "Skipping server %s, no user\n", srvc->host); cred.protocol = xstrdup(srvc->use_ssl ? "imaps" : "imap");
goto bail; cred.host = xstrdup(srvc->host);
}
if (!srvc->pass) { if (srvc->user)
struct strbuf prompt = STRBUF_INIT; cred.username = xstrdup(srvc->user);
strbuf_addf(&prompt, "Password (%s@%s): ", srvc->user, srvc->host); if (srvc->pass)
arg = git_getpass(prompt.buf); cred.password = xstrdup(srvc->pass);
strbuf_release(&prompt);
if (!*arg) { credential_fill(&cred);
fprintf(stderr, "Skipping account %s@%s, no password\n", srvc->user, srvc->host);
goto bail; if (!srvc->user)
} srvc->user = xstrdup(cred.username);
/* if (!srvc->pass)
* getpass() returns a pointer to a static buffer. make a copy srvc->pass = xstrdup(cred.password);
* for long term storage.
*/
srvc->pass = xstrdup(arg);
} }
if (CAP(NOLOGIN)) { if (CAP(NOLOGIN)) {
fprintf(stderr, "Skipping account %s@%s, server forbids LOGIN\n", srvc->user, srvc->host); fprintf(stderr, "Skipping account %s@%s, server forbids LOGIN\n", srvc->user, srvc->host);
goto bail; goto bail;
@ -1153,10 +1152,18 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc)
} }
} /* !preauth */ } /* !preauth */
if (cred.username)
credential_approve(&cred);
credential_clear(&cred);
ctx->prefix = ""; ctx->prefix = "";
return ctx; return ctx;
bail: bail:
if (cred.username)
credential_reject(&cred);
credential_clear(&cred);
imap_close_store(ctx); imap_close_store(ctx);
return NULL; return NULL;
} }