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