From: Ken Hornstein Date: Sun, 21 Aug 2016 04:39:36 +0000 (-0400) Subject: Very close, but it doesn't passes the tests just yet. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/f81bb3b8dae1b305ad394657b8420acc7ba45462?ds=inline;hp=-c Very close, but it doesn't passes the tests just yet. --- f81bb3b8dae1b305ad394657b8420acc7ba45462 diff --git a/h/oauth.h b/h/oauth.h index d1c9c220..274ca9de 100644 --- a/h/oauth.h +++ b/h/oauth.h @@ -218,7 +218,7 @@ mh_oauth_cred_free(mh_oauth_cred *cred); * Never returns NULL. */ const char * -mh_oauth_cred_fn(mh_oauth_ctx *ctx); +mh_oauth_cred_fn(const char *svc_name); /* * Serialize OAuth tokens to file. @@ -258,5 +258,5 @@ boolean mh_oauth_get_service_info(const char *svc_name, mh_oauth_service_info *svcinfo, char *errbuf, size_t errbuflen); -char * -mh_oauth_node_name_for_svc(const char *entry, const char *svc_name); +char *mh_oauth_get_svc_name(mh_oauth_ctx *ctx); +void mh_oauth_set_cred_fn(mh_oauth_ctx *ctx, char *filename); diff --git a/h/prototypes.h b/h/prototypes.h index b5f088c9..5c1b5cf1 100644 --- a/h/prototypes.h +++ b/h/prototypes.h @@ -451,7 +451,7 @@ int sc_length(void); int sc_width(void); int build_form (char *, char *, int *, char *, char *, char *, char *, char *, char *); -int sendsbr (char **, int, char *, char *, struct stat *, int); +int sendsbr (char **, int, char *, char *, struct stat *, int, const char *); int SOprintf (char *, ...); int what_now (char *, int, int, char *, char *, int, struct msgs *, char *, int, char *, int); diff --git a/sbr/oauth.c b/sbr/oauth.c index e86aefe4..ae745fcf 100644 --- a/sbr/oauth.c +++ b/sbr/oauth.c @@ -143,7 +143,7 @@ mh_oauth_do_xoauth(const char *user, const char *svc, FILE *log) if (log != NULL) mh_oauth_log_to(stderr, ctx); - fn = getcpy(mh_oauth_cred_fn(ctx)); + fn = getcpy(mh_oauth_cred_fn(svc)); fp = lkfopendata(fn, "r+", &failed_to_lock); if (fp == NULL) { if (errno == ENOENT) { @@ -569,36 +569,6 @@ mh_oauth_cred_free(mh_oauth_cred *cred) free(cred); } -const char * -mh_oauth_cred_fn(mh_oauth_ctx *ctx) -{ - char *result, *result_if_allocated; - const char *svc = ctx->svc.name; - - char *component = mh_oauth_node_name_for_svc("credential-file", svc); - result = context_find(component); - free(component); - - if (result == NULL) { - result = mh_xmalloc(sizeof "oauth-" - 1 - + strlen(svc) - + 1 /* '\0' */); - sprintf(result, "oauth-%s", svc); - result_if_allocated = result; - } else { - result_if_allocated = NULL; - } - - if (result[0] != '/') { - const char *tmp = m_maildir(result); - free(result_if_allocated); - result = getcpy(tmp); - } - - free(ctx->cred_fn); - return ctx->cred_fn = result; -} - /* for loading multi-user cred files */ struct user_creds { mh_oauth_cred *creds; @@ -1165,5 +1135,4 @@ get_json_strings(const char *input, size_t input_len, FILE *log, ...) free(tokens); return result; } - #endif diff --git a/sbr/oauth_prof.c b/sbr/oauth_prof.c index 4f9e2a2f..dc9bf2d0 100644 --- a/sbr/oauth_prof.c +++ b/sbr/oauth_prof.c @@ -134,4 +134,32 @@ mh_oauth_get_service_info(const char *svc_name, mh_oauth_service_info *svcinfo, return TRUE; } + +const char * +mh_oauth_cred_fn(const char *svc) +{ + char *result, *result_if_allocated; + + char *component = mh_oauth_node_name_for_svc("credential-file", svc); + result = context_find(component); + free(component); + + if (result == NULL) { + result = mh_xmalloc(sizeof "oauth-" - 1 + + strlen(svc) + + 1 /* '\0' */); + sprintf(result, "oauth-%s", svc); + result_if_allocated = result; + } else { + result_if_allocated = NULL; + } + + if (result[0] != '/') { + const char *tmp = m_maildir(result); + free(result_if_allocated); + result = getcpy(tmp); + } + + return result; +} #endif diff --git a/uip/mhlogin.c b/uip/mhlogin.c index 6ce414f2..aabe9a3d 100644 --- a/uip/mhlogin.c +++ b/uip/mhlogin.c @@ -71,7 +71,7 @@ do_login(const char *svc, const char *user, const char *browser, int snoop) mh_oauth_log_to(stderr, ctx); } - fn = getcpy(mh_oauth_cred_fn(ctx)); + fn = getcpy(mh_oauth_cred_fn(svc)); if ((url = mh_oauth_get_authorize_url(ctx)) == NULL) { adios(NULL, mh_oauth_get_err_string(ctx)); diff --git a/uip/send.c b/uip/send.c index 03a54698..8632891a 100644 --- a/uip/send.c +++ b/uip/send.c @@ -459,13 +459,6 @@ go_to_it: if (user == NULL) { adios (NULL, "must specify -user with -saslmech xoauth2"); } - - vec[vecp++] = "-authservice"; - if (saslmech && ! strcasecmp(saslmech, "xoauth2")) { - vec[vecp++] = mh_oauth_do_xoauth (user, auth_svc, snoop ? stderr : NULL); - } else { - vec[vecp++] = auth_svc; - } } #else NMH_UNUSED(auth_svc); @@ -485,7 +478,7 @@ go_to_it: closefds (3); for (msgnum = 0; msgnum < msgp; msgnum++) { - switch (sendsbr (vec, vecp, program, msgs[msgnum], &st, 1)) { + switch (sendsbr (vec, vecp, program, msgs[msgnum], &st, 1, auth_svc)) { case DONE: done (++status); case NOTOK: diff --git a/uip/sendsbr.c b/uip/sendsbr.c index 62229340..c61ea052 100644 --- a/uip/sendsbr.c +++ b/uip/sendsbr.c @@ -26,7 +26,7 @@ #ifdef OAUTH_SUPPORT #include -static int setup_oauth_params(char *[], int *, const char **); +static int setup_oauth_params(char *[], int *, const char *, const char **); #endif /* OAUTH_SUPPORT */ int debugsw = 0; /* global */ @@ -52,7 +52,7 @@ static void anno (int, struct stat *); static void annoaux (int); static int splitmsg (char **, int, char *, char *, struct stat *, int); static int sendaux (char **, int, char *, char *, struct stat *); -static void handle_sendfrom(char **, int *, char *); +static void handle_sendfrom(char **, int *, char *, const char *); static int get_from_header_info(const char *, const char **, const char **, const char **); static const char *get_message_header_info(FILE *, char *); static void merge_profile_entry(const char *, const char *, char *[], int *); @@ -64,7 +64,7 @@ static void armed_done (int) NORETURN; int sendsbr (char **vec, int vecp, char *program, char *draft, struct stat *st, - int rename_drft) + int rename_drft, const char *auth_svc) { int status, i; pid_t child; @@ -124,12 +124,22 @@ sendsbr (char **vec, int vecp, char *program, char *draft, struct stat *st, drft = file; } + /* + * Add in any necessary profile entries for xoauth + */ + + if (auth_svc) { + const char *errmsg; + if (!setup_oauth_params(vec, nvecsp, auth_svc, &errmsg)) + adios(NULL, "%s", errmsg); + } + /* * Rework the vec based on From: header in draft, as specified * by sendfrom-address entries in profile. */ if (context_find_prefix("sendfrom-")) { - handle_sendfrom(vec, nvecsp, draft); + handle_sendfrom(vec, nvecsp, draft, auth_svc); } /* @@ -727,7 +737,7 @@ oops: static void -handle_sendfrom(char **vec, int *vecp, char *draft) { +handle_sendfrom(char **vec, int *vecp, char *draft, const char *auth_svc) { const char *addr, *host; const char *message; @@ -749,7 +759,7 @@ handle_sendfrom(char **vec, int *vecp, char *draft) { for (vp = vec; *vp; ++vp) { if (strcmp(*vp, "xoauth2") == 0) { #ifdef OAUTH_SUPPORT - if (setup_oauth_params(vec, vecp, &message) != OK) { + if (setup_oauth_params(vec, vecp, auth_svc, &message) != OK) { adios(NULL, message); } break; @@ -768,8 +778,9 @@ handle_sendfrom(char **vec, int *vecp, char *draft) { * For XOAUTH2, append profile entries so post can do the heavy lifting */ static int -setup_oauth_params(char *vec[], int *vecp, const char **message) { - const char *saslmech = NULL, *user = NULL, *auth_svc = NULL; +setup_oauth_params(char *vec[], int *vecp, const char *auth_svc, + const char **message) { + const char *saslmech = NULL, *user = NULL; mh_oauth_service_info svc; char errbuf[256]; int i; @@ -798,12 +809,26 @@ setup_oauth_params(char *vec[], int *vecp, const char **message) { return NOTOK; } - - vec[(*vecp)++] = getcpy("-authservice"); if (saslmech && ! strcasecmp(saslmech, "xoauth2")) { - vec[(*vecp)++] = mh_oauth_do_xoauth(user, auth_svc, snoop ? stderr : NULL); - } else { - vec[(*vecp)++] = getcpy(auth_svc); + if (! mh_oauth_get_service_info(auth_svc, &svc, errbuf, + sizeof(errbuf))) + adios(NULL, "Unable to retrieve oauth profile entries: %s", + errbuf); + + vec[(*vecp)++] = getcpy("-oauthcredfile"); + vec[(*vecp)++] = getcpy(mh_oauth_cred_fn(auth_svc)); + vec[(*vecp)++] = getcpy("-oauthclientid"); + vec[(*vecp)++] = getcpy(svc.client_id); + vec[(*vecp)++] = getcpy("-oauthclientsecret"); + vec[(*vecp)++] = getcpy(svc.client_secret); + vec[(*vecp)++] = getcpy("-oauthauthendpoint"); + vec[(*vecp)++] = getcpy(svc.auth_endpoint); + vec[(*vecp)++] = getcpy("-oauthredirect"); + vec[(*vecp)++] = getcpy(svc.redirect_uri); + vec[(*vecp)++] = getcpy("-oauthtokenendpoint"); + vec[(*vecp)++] = getcpy(svc.token_endpoint); + vec[(*vecp)++] = getcpy("-oauthscope"); + vec[(*vecp)++] = getcpy(svc.scope); } } diff --git a/uip/viamail.c b/uip/viamail.c index 050ca0f6..f80980d6 100644 --- a/uip/viamail.c +++ b/uip/viamail.c @@ -213,7 +213,7 @@ via_mail (char *mailsw, char *subjsw, char *parmsw, char *descsw, vec[vecp++] = cp; } - switch (sendsbr (vec, vecp, program, tmpfil, &st, 0)) { + switch (sendsbr (vec, vecp, program, tmpfil, &st, 0, NULL)) { case DONE: case NOTOK: status++; diff --git a/uip/whatnowsbr.c b/uip/whatnowsbr.c index d0423f53..03c0bbba 100644 --- a/uip/whatnowsbr.c +++ b/uip/whatnowsbr.c @@ -1255,13 +1255,6 @@ sendit (char *sp, char **arg, char *file, int pushed) if (user == NULL) { adios (NULL, "must specify -user with -saslmech xoauth2"); } - - vec[vecp++] = "-authservice"; - if (saslmech && ! strcasecmp(saslmech, "xoauth2")) { - vec[vecp++] = mh_oauth_do_xoauth (user, auth_svc, snoop ? stderr : NULL); - } else { - vec[vecp++] = auth_svc; - } } #else NMH_UNUSED(saslmech); @@ -1277,7 +1270,7 @@ sendit (char *sp, char **arg, char *file, int pushed) closefds (3); - if (sendsbr (vec, vecp, program, file, &st, 1) == OK) + if (sendsbr (vec, vecp, program, file, &st, 1, auth_svc) == OK) done (0); }