]> diplodocus.org Git - nmh/blob - sbr/oauth_prof.c
4d91b8b2a7f4a829ca1e8e3944acca2a13a8fc5e
[nmh] / sbr / oauth_prof.c
1 /* oauth_prof.c -- OAuth 2.0 implementation for XOAUTH2 in SMTP and POP3.
2 *
3 * This code is Copyright (c) 2014, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "getcpy.h"
10 #include "context_find.h"
11
12 #ifdef OAUTH_SUPPORT
13
14 #include <sys/stat.h>
15
16 #include <stdarg.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <strings.h>
21 #include <unistd.h>
22
23 #include "h/oauth.h"
24 #include "h/utils.h"
25 #include "m_maildir.h"
26
27 static const struct mh_oauth_service_info SERVICES[] = {
28 /* https://developers.google.com/accounts/docs/OAuth2InstalledApp */
29 {
30 /* name */ "gmail",
31 /* display_name */ "Gmail",
32
33 /* client_id */ "91584523849-8lv9kgp1rvp8ahta6fa4b125tn2polcg.apps.googleusercontent.com",
34 /* client_secret */ "Ua8sX34xyv7hVrKM-U70dKI6",
35
36 /* auth_endpoint */ "https://accounts.google.com/o/oauth2/auth",
37 /* redirect_uri */ "urn:ietf:wg:oauth:2.0:oob",
38 /* token_endpoint */ "https://accounts.google.com/o/oauth2/token",
39 /* scope */ "https://mail.google.com/"
40 }
41 };
42
43 /* Copy service info so we don't have to free it only sometimes. */
44 static void
45 copy_svc(mh_oauth_service_info *to, const mh_oauth_service_info *from)
46 {
47 to->display_name = from->display_name;
48 #define copy(_field_) to->_field_ = getcpy(from->_field_)
49 copy(name);
50 copy(scope);
51 copy(client_id);
52 copy(client_secret);
53 copy(auth_endpoint);
54 copy(token_endpoint);
55 copy(redirect_uri);
56 #undef copy
57 }
58
59 /* Return profile component node name for a service parameter. */
60 char *
61 mh_oauth_node_name_for_svc(const char *base_name, const char *svc)
62 {
63 /* TODO: s/_/-/g ? */
64 return concat("oauth-", svc, "-", base_name, NULL);
65 }
66
67 /* Update one service_info field if overridden in profile. */
68 static void
69 update_svc_field(char **field, const char *base_name, const char *svc)
70 {
71 char *name = mh_oauth_node_name_for_svc(base_name, svc);
72 const char *value = context_find(name);
73 if (value != NULL) {
74 free(*field);
75 *field = mh_xstrdup(value);
76 }
77 free(name);
78 }
79
80 /* Update all service_info fields that are overridden in profile. */
81 static bool
82 update_svc(mh_oauth_service_info *svc, const char *svc_name, char *errbuf,
83 size_t errbuflen)
84 {
85 #define update(name) \
86 update_svc_field(&svc->name, #name, svc_name); \
87 if (svc->name == NULL) { \
88 snprintf(errbuf, errbuflen, "%s", #name " is missing"); \
89 errbuf[errbuflen - 1] = '\0'; \
90 return false; \
91 }
92 update(scope);
93 update(client_id);
94 update(client_secret);
95 update(auth_endpoint);
96 update(token_endpoint);
97 update(redirect_uri);
98 #undef update
99
100 if (svc->name == NULL) {
101 svc->name = getcpy(svc_name);
102 }
103
104 if (svc->display_name == NULL) {
105 svc->display_name = svc->name;
106 }
107
108 return true;
109 }
110
111 bool
112 mh_oauth_get_service_info(const char *svc_name, mh_oauth_service_info *svcinfo,
113 char *errbuf, size_t errbuflen)
114 {
115 int i;
116
117 svcinfo->name = svcinfo->display_name = NULL;
118 svcinfo->scope = svcinfo->client_id = NULL;
119 svcinfo->client_secret = svcinfo->auth_endpoint = NULL;
120 svcinfo->token_endpoint = svcinfo->redirect_uri = NULL;
121
122 for (i = 0; i < (int)DIM(SERVICES); i++) {
123 if (strcmp(SERVICES[i].name, svc_name) == 0) {
124 copy_svc(svcinfo, &SERVICES[i]);
125 break;
126 }
127 }
128
129 return update_svc(svcinfo, svc_name, errbuf, errbuflen);
130 }
131
132 /* Return value must be free(3)'d. */
133 char *
134 mh_oauth_cred_fn(const char *svc)
135 {
136 char *key = mh_oauth_node_name_for_svc("credential-file", svc);
137 char *value = context_find(key);
138 free(key);
139
140 bool found = value;
141 if (found) {
142 if (*value == '/')
143 return mh_xstrdup(value);
144 } else
145 value = concat("oauth-", svc, NULL);
146
147 const char *md = m_maildir(value);
148 if (!found)
149 free(value);
150
151 return mh_xstrdup(md);
152 }
153 #endif