]> diplodocus.org Git - nmh/blobdiff - sbr/oauth.c
mhbuildsbr.c: Flip logic, moving goto to then-block; no need for else.
[nmh] / sbr / oauth.c
index 87ee7791bfc72607c21f938276eddd4d5c2ca3e5..8e3d6b05c103f2c74fb5ce3816629b62989564a9 100755 (executable)
@@ -1,4 +1,5 @@
-/*
+/* oauth.c -- OAuth 2.0 implementation for XOAUTH2 in SMTP and POP3.
+ *
  * This code is Copyright (c) 2014, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
@@ -23,6 +24,7 @@
 
 #include <h/oauth.h>
 #include <h/utils.h>
+#include "lock_file.h"
 
 #define JSON_TYPE "application/json"
 
@@ -138,7 +140,8 @@ mh_oauth_do_xoauth(const char *user, const char *svc, unsigned char **oauth_res,
     FILE *fp;
     char *client_res;
 
-    if (!mh_oauth_new (&ctx, svc)) adios(NULL, mh_oauth_get_err_string(ctx));
+    if (!mh_oauth_new (&ctx, svc))
+        adios(NULL, "%s", mh_oauth_get_err_string(ctx));
 
     if (log != NULL) mh_oauth_log_to(stderr, ctx);
 
@@ -155,7 +158,7 @@ mh_oauth_do_xoauth(const char *user, const char *svc, unsigned char **oauth_res,
     }
 
     if ((cred = mh_oauth_cred_load(fp, ctx, user)) == NULL) {
-        adios(NULL, mh_oauth_get_err_string(ctx));
+        adios(NULL, "%s", mh_oauth_get_err_string(ctx));
     }
 
     if (!mh_oauth_access_token_valid(time(NULL), cred)) {
@@ -166,13 +169,13 @@ mh_oauth_do_xoauth(const char *user, const char *svc, unsigned char **oauth_res,
             if (mh_oauth_get_err_code(ctx) == MH_OAUTH_BAD_GRANT) {
                 adios(NULL, "credentials rejected -- run mhlogin -saslmech xoauth2 -authservice %s", svc);
             }
-            advise(NULL, "error refreshing OAuth2 token");
-            adios(NULL, mh_oauth_get_err_string(ctx));
+            inform("error refreshing OAuth2 token");
+            adios(NULL, "%s", mh_oauth_get_err_string(ctx));
         }
 
         fseek(fp, 0, SEEK_SET);
         if (!mh_oauth_cred_save(fp, cred, user)) {
-            adios(NULL, mh_oauth_get_err_string(ctx));
+            adios(NULL, "%s", mh_oauth_get_err_string(ctx));
         }
     }
 
@@ -196,7 +199,7 @@ static boolean
 is_json(const char *content_type)
 {
     return content_type != NULL
-        && strncasecmp(content_type, JSON_TYPE, sizeof JSON_TYPE - 1) == 0;
+        && strncasecmp(content_type, JSON_TYPE, LEN(JSON_TYPE)) == 0;
 }
 
 static void
@@ -239,7 +242,7 @@ set_err_http(mh_oauth_ctx *ctx, const struct curl_ctx *curl_ctx)
 }
 
 static char *
-make_user_agent()
+make_user_agent(void)
 {
     const char *curl = curl_version_info(CURLVERSION_NOW)->version;
     return concat(user_agent, " libcurl/", curl, NULL);
@@ -613,7 +616,7 @@ load_creds(struct user_creds **result, FILE *fp, mh_oauth_ctx *ctx)
     boolean success = FALSE;
     char name[NAMESZ], value_buf[BUFSIZ];
     int state;
-    m_getfld_state_t getfld_ctx = 0;
+    m_getfld_state_t getfld_ctx;
 
     struct user_creds *user_creds;
     NEW(user_creds);
@@ -621,24 +624,25 @@ load_creds(struct user_creds **result, FILE *fp, mh_oauth_ctx *ctx)
     user_creds->len = 0;
     user_creds->creds = mh_xmalloc(user_creds->alloc * sizeof *user_creds->creds);
 
+    getfld_ctx = m_getfld_state_init(fp);
     for (;;) {
        int size = sizeof value_buf;
-       switch (state = m_getfld(&getfld_ctx, name, value_buf, &size, fp)) {
+       switch (state = m_getfld2(&getfld_ctx, name, value_buf, &size)) {
         case FLD:
         case FLDPLUS: {
             char **save, *expire;
             time_t *expires_at = NULL;
-            if (strncmp(name, "access-", 7) == 0) {
+            if (has_prefix(name, "access-")) {
                 const char *user = name + 7;
                 mh_oauth_cred *creds = find_or_alloc_user_creds(user_creds,
                                                                 user);
                 save = &creds->access_token;
-            } else if (strncmp(name, "refresh-", 8) == 0) {
+            } else if (has_prefix(name, "refresh-")) {
                 const char *user = name + 8;
                 mh_oauth_cred *creds = find_or_alloc_user_creds(user_creds,
                                                                 user);
                 save = &creds->refresh_token;
-            } else if (strncmp(name, "expire-", 7) == 0) {
+            } else if (has_prefix(name, "expire-")) {
                 const char *user = name + 7;
                 mh_oauth_cred *creds = find_or_alloc_user_creds(user_creds,
                                                                 user);
@@ -655,7 +659,7 @@ load_creds(struct user_creds **result, FILE *fp, mh_oauth_ctx *ctx)
                 char *tmp = getcpy(value_buf);
                 while (state == FLDPLUS) {
                     size = sizeof value_buf;
-                    state = m_getfld(&getfld_ctx, name, value_buf, &size, fp);
+                    state = m_getfld2(&getfld_ctx, name, value_buf, &size);
                     tmp = add(value_buf, tmp);
                 }
                 *save = trimcpy(tmp);
@@ -681,7 +685,7 @@ load_creds(struct user_creds **result, FILE *fp, mh_oauth_ctx *ctx)
             break;
 
         default:
-            /* Not adding details for LENERR/FMTERR because m_getfld already
+            /* Not adding details for LENERR/FMTERR because m_getfld2 already
              * wrote advise message to stderr. */
             set_err(ctx, MH_OAUTH_CRED_FILE);
             break;
@@ -877,7 +881,7 @@ make_query_url(char *s, size_t size, CURL *curl, const char *base_url, ...)
 }
 
 static int
-debug_callback(const CURL *handle, curl_infotype type, const char *data,
+debug_callback(CURL *handle, curl_infotype type, char *data,
                size_t size, void *userptr)
 {
     FILE *fp = userptr;
@@ -949,7 +953,7 @@ post(struct curl_ctx *ctx, const char *url, const char *req_body)
     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
     curl_easy_setopt(curl, CURLOPT_WRITEDATA, ctx);
 
-    if (strncmp(url, "http://127.0.0.1:", 17) == 0) {
+    if (has_prefix(url, "http://127.0.0.1:")) {
         /* Hack:  on Cygwin, curl doesn't fail to connect with ECONNREFUSED.
            Instead, it waits to timeout.  So set a really short timeout, but
            just on localhost (for convenience of the user, and the test