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