]> diplodocus.org Git - nmh/commitdiff
Fix up a few minor nits to make OAuth2 (finally!) work right in post(8).
authorKen Hornstein <kenh@pobox.com>
Mon, 5 Sep 2016 00:42:21 +0000 (20:42 -0400)
committerKen Hornstein <kenh@pobox.com>
Mon, 5 Sep 2016 00:42:21 +0000 (20:42 -0400)
Change the tests to support the new error output.

test/oauth/test-send
uip/post.c
uip/send.c
uip/sendsbr.c

index f41f896..98efb2b 100755 (executable)
@@ -163,29 +163,32 @@ setup_draft
 # TEST
 start_test 'no service definition'
 
-run_test "send -draft -server 127.0.0.1 -port ${smtp_port} -saslmech xoauth2 -authservice bogus -user nobody@example.com" 'send: incomplete OAuth2 service definition: scope is missing'
+run_test "send -draft -server 127.0.0.1 -port ${smtp_port} -saslmech xoauth2 -authservice bogus -user nobody@example.com" 'send: Unable to retrieve oauth profile entries: scope is missing'
 
 # TEST
 start_test 'no creds file -- should tell user to mhlogin'
 
 rm -f "${MHTMPDIR}/oauth-test"
 
-test_send_no_servers 'send: no credentials -- run mhlogin -saslmech xoauth2 -authservice test'
+test_send_only_fakesmtp 'post: no credentials -- run mhlogin -saslmech xoauth2 -authservice test
+send: message not delivered to anyone'
 
 # TEST
 start_test 'empty creds file -- should tell user to mhlogin'
 
 fake_creds < /dev/null
 
-test_send_no_servers 'send: user not found in cred file: nobody@example.com'
+test_send_only_fakesmtp 'post: user not found in cred file: nobody@example.com
+send: message not delivered to anyone'
 
 # TEST
 start_test 'garbage creds file'
 
 echo bork | fake_creds
 
-test_send_no_servers 'send: eof encountered in field "bork"
-send: error loading cred file'
+test_send_only_fakesmtp 'post: eof encountered in field "bork"
+post: error loading cred file
+send: message not delivered to anyone'
 
 # TEST
 start_test 'unexpected field in creds file'
@@ -195,7 +198,8 @@ bork: bork
 access-nobody@example.com: test-access
 EOF
 
-test_send_no_servers 'send: error loading cred file: unexpected field'
+test_send_only_fakesmtp 'post: error loading cred file: unexpected field
+send: message not delivered to anyone'
 
 # TEST
 start_test 'garbage expiration time'
@@ -205,7 +209,8 @@ access-nobody@example.com: test-access
 expire-nobody@example.com: 99999999999999999999999999999999
 EOF
 
-test_send_no_servers 'send: error loading cred file: invalid expiration time'
+test_send_only_fakesmtp 'post: error loading cred file: invalid expiration time
+send: message not delivered to anyone'
 
 # TEST
 start_test 'refresh response has no access token'
@@ -223,8 +228,9 @@ fake_json_response <<EOF
 }
 EOF
 
-test_send_only_fakehttp 'send: error refreshing OAuth2 token
-send: invalid response: no access token'
+test_send 'post: error refreshing OAuth2 token
+post: invalid response: no access token
+send: message not delivered to anyone'
 
 # TEST
 start_test 'expired access token, no refresh token -- tell user to mhlogin'
@@ -234,7 +240,8 @@ access-nobody@example.com: test-access
 expire-nobody@example.com: 1414303986
 EOF
 
-test_send_no_servers 'send: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test'
+test_send_only_fakesmtp 'post: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test
+send: message not delivered to anyone'
 
 # TEST
 start_test 'access token has no expiration, no refresh token -- tell user to mhlogin'
@@ -243,7 +250,8 @@ fake_creds <<EOF
 access-nobody@example.com: test-access
 EOF
 
-test_send_no_servers 'send: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test'
+test_send_only_fakesmtp 'post: no valid credentials -- run mhlogin -saslmech xoauth2 -authservice test
+send: message not delivered to anyone'
 
 # TEST
 start_test 'refresh finds no http server'
@@ -254,17 +262,19 @@ refresh-nobody@example.com: test-refresh
 EOF
 
 cat > "${testname}.expected-send-output" <<EOF
-send: error refreshing OAuth2 token
-send: error making HTTP request to OAuth2 authorization endpoint: [details]
+post: error refreshing OAuth2 token
+post: error making HTTP request to OAuth2 authorization endpoint: [details]
+send: message not delivered to anyone
 EOF
 
+start_fakesmtp
 run_prog send -draft -server 127.0.0.1 -port ${smtp_port} \
   -saslmech xoauth2 -authservice test -user nobody@example.com > "${testname}.send-output" 2>&1 || true
 # Clear out an error message we get from libcurl on some systems (seen at least
 # 3 different versions of this error message, on FreeBSD 10.1, Ubuntu 12.04, and
 # Ubuntu 14.04).
 f="${testname}.send-output"
-sed 's/\(send: error making HTTP request to OAuth2 authorization endpoint:\).*/\1 [details]/' "$f" > "$f".clean
+sed 's/\(post: error making HTTP request to OAuth2 authorization endpoint:\).*/\1 [details]/' "$f" > "$f".clean
 check "$f".clean "${testname}.expected-send-output"
 rm "$f"
 
@@ -279,8 +289,9 @@ Content-Type: text/html
 <html>doh!</htmxl>
 EOF
 
-test_send_only_fakehttp 'send: error refreshing OAuth2 token
-send: invalid response'
+test_send 'post: error refreshing OAuth2 token
+post: invalid response
+send: message not delivered to anyone'
 
 # TEST
 start_test 'refresh gets 500 response from http server'
@@ -293,8 +304,9 @@ Content-Type: text/html
 <html>doh!</html>
 EOF
 
-test_send_only_fakehttp 'send: error refreshing OAuth2 token
-send: invalid response'
+test_send 'post: error refreshing OAuth2 token
+post: invalid response
+send: message not delivered to anyone'
 
 # TEST
 start_test 'refresh gets proper error from http'
@@ -309,7 +321,8 @@ Content-Type: application/json
 }
 EOF
 
-test_send_only_fakehttp 'send: credentials rejected -- run mhlogin -saslmech xoauth2 -authservice test'
+test_send 'post: credentials rejected -- run mhlogin -saslmech xoauth2 -authservice test
+send: message not delivered to anyone'
 
 # TEST
 start_test 'refresh gets response too big'
@@ -331,8 +344,9 @@ EOF
 awk 'BEGIN { for (i = 0; i < 8192; i++) { print "." } }' \
     >> "${testname}.http-res"
 
-test_send_only_fakehttp 'send: error refreshing OAuth2 token
-send: refusing to process response body larger than 8192 bytes'
+test_send 'post: error refreshing OAuth2 token
+post: refusing to process response body larger than 8192 bytes
+send: message not delivered to anyone'
 
 # TEST
 start_test 'smtp server rejects token'
@@ -347,14 +361,6 @@ EOF
 test_send_only_fakesmtp 'post: problem initializing server; [BHST] Not no way, not no how!
 send: message not delivered to anyone'
 
-# TEST
-start_test "smtp server doesn't support oauth"
-
-unset XOAUTH
-
-test_send_only_fakesmtp 'post: problem initializing server; [BHST] SMTP server does not support SASL XOAUTH2
-send: message not delivered to anyone'
-
 # TEST
 start_test 'botched json response'
 
@@ -370,8 +376,17 @@ fake_json_response <<EOF
 }
 EOF
 
-test_send_only_fakehttp 'send: error refreshing OAuth2 token
-send: invalid response'
+test_send 'post: error refreshing OAuth2 token
+post: invalid response
+send: message not delivered to anyone'
+
+# TEST
+start_test "smtp server doesn't support oauth"
+
+unset XOAUTH
+
+test_send_only_fakesmtp 'post: problem initializing server; [BHST] SMTP server does not support SASL XOAUTH2
+send: message not delivered to anyone'
 
 clean_fakesmtp
 clean_fakehttp
index 43110f3..4cdc28f 100644 (file)
     X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
     X("credentials legacy|file:filename", 0, CREDENTIALSSW) \
     X("messageid localname|random", 2, MESSAGEIDSW) \
-    X("oauthcredfile", OAUTHminc(-7), OAUTHCREDFILESW) \
-    X("oauthclientid", OAUTHminc(-12), OAUTHCLIDSW) \
-    X("oauthclientsecret", OAUTHminc(-12), OAUTHCLSECSW) \
-    X("oauthauthendpoint", OAUTHminc(-6), OAUTHAUTHENDSW) \
-    X("oauthredirect", OAUTHminc(-6), OAUTHREDIRSW) \
-    X("oauthtokenendpoint", OAUTHminc(-6), OAUTHTOKENDSW) \
-    X("oauthscope", OAUTHminc(-6), OAUTHSCOPESW) \
+    X("authservice auth-service-name", OAUTHminc(-11), AUTHSERVICESW) \
+    X("oauthcredfile credential-file", OAUTHminc(-7), OAUTHCREDFILESW) \
+    X("oauthclientid client-id", OAUTHminc(-12), OAUTHCLIDSW) \
+    X("oauthclientsecret client-secret", OAUTHminc(-12), OAUTHCLSECSW) \
+    X("oauthauthendpoint authentication-endpoint", OAUTHminc(-6), OAUTHAUTHENDSW) \
+    X("oauthredirect redirect-uri", OAUTHminc(-6), OAUTHREDIRSW) \
+    X("oauthtokenendpoint token-endpoint", OAUTHminc(-6), OAUTHTOKENDSW) \
+    X("oauthscope scope", OAUTHminc(-6), OAUTHSCOPESW) \
 
 #define X(sw, minchars, id) id,
 DEFINE_SWITCH_ENUM(POST);
@@ -123,13 +124,13 @@ static struct oauth_profile {
     const char *profname;
     int switchnum;
 } oauthswitches[] = {
-    { "oauth-post-credential-file", OAUTHCREDFILESW },
-    { "oauth-post-client_id", OAUTHCLIDSW },
-    { "oauth-post-client_secret", OAUTHCLSECSW },
-    { "oauth-post-auth_endpoint", OAUTHAUTHENDSW },
-    { "oauth-post-redirect_url", OAUTHREDIRSW },
-    { "oauth-post-token_endpoint", OAUTHTOKENDSW },
-    { "oauth-post-scope", OAUTHSCOPESW },
+    { "oauth-%s-credential-file", OAUTHCREDFILESW },
+    { "oauth-%s-client_id", OAUTHCLIDSW },
+    { "oauth-%s-client_secret", OAUTHCLSECSW },
+    { "oauth-%s-auth_endpoint", OAUTHAUTHENDSW },
+    { "oauth-%s-redirect_uri", OAUTHREDIRSW },
+    { "oauth-%s-token_endpoint", OAUTHTOKENDSW },
+    { "oauth-%s-scope", OAUTHSCOPESW },
     { NULL, 0 }
 };
 
@@ -296,7 +297,7 @@ static void sigoff (void);
 static void p_refile (char *);
 static void fcc (char *, char *);
 static void die (char *, char *, ...);
-static void post (char *, int, int, char *, int);
+static void post (char *, int, int, char *, int, char *);
 static void do_text (char *file, int fd);
 static void do_an_address (struct mailname *, int);
 static void do_addresses (int, int);
@@ -308,7 +309,7 @@ main (int argc, char **argv)
 {
     int state, compnum, dashstuff = 0, swnum, oauth_flag = 0;
     char *cp, *msg = NULL, **argp, **arguments, *envelope;
-    char buf[BUFSIZ], name[NAMESZ];
+    char buf[BUFSIZ], name[NAMESZ], *auth_svc = "post";
     FILE *in, *out;
     m_getfld_state_t gstate = 0;
 
@@ -472,6 +473,11 @@ main (int argc, char **argv)
                        adios (NULL, "missing argument to %s", argp[-2]);
                    continue;
 
+               case AUTHSERVICESW:
+                   if (!(auth_svc = *argp++) || *auth_svc == '-')
+                       adios (NULL, "missing argument to %s", argp[-2]);
+                   continue;
+
                case OAUTHCREDFILESW:
                case OAUTHCLIDSW:
                case OAUTHCLSECSW:
@@ -481,13 +487,17 @@ main (int argc, char **argv)
                case OAUTHSCOPESW:
                {
                    int i;
+                   char sbuf[128];
 
                    if (!(cp = *argp++) || *cp == '-')
                        adios (NULL, "missing argument to %s", argp[-2]);
 
                    for (i = 0; oauthswitches[i].profname != NULL; i++) {
                        if (oauthswitches[i].switchnum == swnum) {
-                           add_profile_entry(oauthswitches[i].profname, cp);
+                           snprintf(sbuf, sizeof(sbuf),
+                                    oauthswitches[i].profname, auth_svc);
+                           sbuf[sizeof(sbuf) - 1] = '\0';
+                           add_profile_entry(sbuf, cp);
                            break;
                        }
                    }
@@ -702,12 +712,12 @@ main (int argc, char **argv)
                   that spost didn't verify addresses. */
                verify_all_addresses (verbose, envelope, oauth_flag);
            }
-           post (tmpfil, 0, verbose, envelope, oauth_flag);
+           post (tmpfil, 0, verbose, envelope, oauth_flag, auth_svc);
        }
-       post (bccfil, 1, verbose, envelope, oauth_flag);
+       post (bccfil, 1, verbose, envelope, oauth_flag, auth_svc);
        (void) m_unlink (bccfil);
     } else {
-       post (tmpfil, 0, isatty (1), envelope, oauth_flag);
+       post (tmpfil, 0, isatty (1), envelope, oauth_flag, auth_svc);
     }
 
     p_refile (tmpfil);
@@ -1554,7 +1564,8 @@ do_addresses (int bccque, int talk)
  */
 
 static void
-post (char *file, int bccque, int talk, char *envelope, int oauth_flag)
+post (char *file, int bccque, int talk, char *envelope, int oauth_flag,
+      char *auth_svc)
 {
     int fd;
     int        retval, i;
@@ -1605,7 +1616,7 @@ post (char *file, int bccque, int talk, char *envelope, int oauth_flag)
         if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
                                         verbose, snoop, sasl, saslssf,
                                        saslmech, user,
-                                       oauth_flag ? "post" : NULL, tls))
+                                       oauth_flag ? auth_svc : NULL, tls))
             || rp_isbad (retval = sm_winit (envelope)))
            die (NULL, "problem initializing server; %s", rp_string (retval));
 
index 8632891..5dcdce0 100644 (file)
@@ -268,11 +268,6 @@ main (int argc, char **argv)
                     user = cp;
                    continue;
 
-               case SASLMECHSW:
-                   if (!(saslmech = *argp++) || *saslmech == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
-                   continue;
-
                case AUTHSERVICESW:
 #ifdef OAUTH_SUPPORT
                    if (!(auth_svc = *argp++) || *auth_svc == '-')
@@ -282,6 +277,11 @@ main (int argc, char **argv)
 #endif
                    continue;
 
+               case SASLMECHSW:
+                   if (!(saslmech = *argp) || *saslmech == '-')
+                       adios (NULL, "missing argument to %s", argp[-2]);
+                   /* Fall through */
+
                case ALIASW: 
                case FILTSW: 
                case WIDTHSW: 
index e55361c..ff203a4 100644 (file)
@@ -816,6 +816,8 @@ setup_oauth_params(char *vec[], int *vecp, const char *auth_svc,
                adios(NULL, "Unable to retrieve oauth profile entries: %s",
                      errbuf);
            
+           vec[(*vecp)++] = getcpy("-authservice");
+           vec[(*vecp)++] = getcpy(auth_svc);
            vec[(*vecp)++] = getcpy("-oauthcredfile");
            vec[(*vecp)++] = getcpy(mh_oauth_cred_fn(auth_svc));
            vec[(*vecp)++] = getcpy("-oauthclientid");