forked from st3fan/dovecot-xaps-plugin
-
Notifications
You must be signed in to change notification settings - Fork 8
/
xaps-imap-plugin.c
324 lines (275 loc) · 10.8 KB
/
xaps-imap-plugin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Stefan Arentz <[email protected]>
* Copyright (c) 2017 Frederik Schwan <frederik dot schwan at linux dot com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <config.h>
#include <lib.h>
#include <str.h>
#include <imap-common.h>
#include <http-client-private.h>
#include "xaps-imap-plugin.h"
#include "xaps-utils.h"
const char *xapplepushservice_plugin_version = DOVECOT_ABI_VERSION;
static struct module *xaps_imap_module;
static imap_client_created_func_t *next_hook_client_created;
/**
* Command handler for the XAPPLEPUSHSERVICE command. The command is
* used by iOS clients to register for push notifications.
*
* We receive a list of key value pairs from the client, with the
* following keys:
*
* aps-version - always set to "2"
* aps-account-id - a unique id the iOS device has associated with this account
* aps-device-token - the APS device token
* aps-subtopic - always set to "com.apple.mobilemail"
* mailboxes - list of mailboxes to send notifications for
*
* For example:
*
* XAPPLEPUSHSERVICE aps-version 2 aps-account-id 0715A26B-CA09-4730-A419-793000CA982E
* aps-device-token 2918390218931890821908309283098109381029309829018310983092892829
* aps-subtopic com.apple.mobilemail mailboxes (INBOX Notes)
*
* To minimize the work that needs to be done inside the IMAP client,
* we only parse and validate the parameters and then simply push all
* of this to the supporting daemon that will record the mapping
* between the account and the iOS client.
*/
static bool parse_xapplepush(struct client_command_context *cmd, struct xaps_attr *xaps_attr) {
/*
* Parse arguments. We expect four key value pairs. We only take
* those that we understand for version 2 of this extension.
*/
const struct imap_arg *args;
const char *arg_key, *arg_val;
i_assert(xaps_attr != NULL);
xaps_attr->dovecot_username = get_real_mbox_user(cmd->client->user);
if (!client_read_args(cmd, 0, 0, &args)) {
client_send_command_error(cmd, "Invalid arguments.");
return FALSE;
}
for (int i = 0; i < 5; i++) {
if (!imap_arg_get_astring(&args[i * 2 + 0], &arg_key)) {
client_send_command_error(cmd, "Invalid arguments.");
return FALSE;
}
// i=4 is a list with which imap_arg_get_astring segfaults
if (i < 4 && !imap_arg_get_astring(&args[i * 2 + 1], &arg_val)) {
client_send_command_error(cmd, "Invalid arguments.");
return FALSE;
}
if (strcasecmp(arg_key, "aps-version") == 0) {
xaps_attr->aps_version = arg_val;
} else if (strcasecmp(arg_key, "aps-account-id") == 0) {
xaps_attr->aps_account_id = arg_val;
} else if (strcasecmp(arg_key, "aps-device-token") == 0) {
xaps_attr->aps_device_token = arg_val;
} else if (strcasecmp(arg_key, "aps-subtopic") == 0) {
xaps_attr->aps_subtopic = arg_val;
} else if (strcasecmp(arg_key, "mailboxes") == 0) {
if (!imap_arg_get_list(&args[i * 2 + 1], &(xaps_attr->mailboxes))) {
client_send_command_error(cmd, "Invalid arguments.");
return FALSE;
}
}
}
/*
* Check if this is a version we expect
*/
if (!xaps_attr->aps_version || strcmp(xaps_attr->aps_version, "2") != 0) {
client_send_command_error(cmd, "Unknown aps-version.");
return FALSE;
}
/*
* Check if all of the parameters are there.
*/
if (!xaps_attr->aps_account_id || strlen(xaps_attr->aps_account_id) == 0) {
client_send_command_error(cmd, "Incomplete or empty aps-account-id parameter.");
return FALSE;
}
if (!xaps_attr->aps_device_token || strlen(xaps_attr->aps_device_token) == 0) {
client_send_command_error(cmd, "Incomplete or empty aps-device-token parameter.");
return FALSE;
}
if (!xaps_attr->aps_subtopic || strlen(xaps_attr->aps_subtopic) == 0) {
client_send_command_error(cmd, "Incomplete or empty aps-subtopic parameter.");
return FALSE;
}
if(!xaps_attr->mailboxes) {
client_send_command_error(cmd, "Incomplete or empty mailboxes parameter.");
return FALSE;
}
return TRUE;
}
/**
* Send a registration request to the daemon, which will do all the
* hard work.
*/
int xaps_register(struct client_command_context *cmd, struct xaps_attr *xaps_attr) {
struct http_client_request *http_req;
struct istream *payload;
string_t *str;
i_assert(xaps_global != NULL);
i_assert(xaps_global->http_client != NULL);
http_req = http_client_request_url(
xaps_global->http_client, "POST", xaps_global->http_url,
xaps_register_callback, cmd->context);
http_client_request_add_header(http_req, "Content-Type",
"application/json; charset=utf-8");
str = str_new(default_pool, 256);
str_append(str, "{\"ApsAccountId\":\"");
json_append_escaped(str, xaps_attr->aps_account_id);
str_append(str, "\",\"ApsDeviceToken\":\"");
json_append_escaped(str, xaps_attr->aps_device_token);
str_append(str, "\",\"ApsSubtopic\":\"");
json_append_escaped(str, xaps_attr->aps_subtopic);
str_append(str, "\",\"Username\":\"");
json_append_escaped(str, xaps_attr->dovecot_username);
if (xaps_attr->mailboxes == NULL) {
str_append(str, "\",\"Mailboxes\": [\"INBOX\"]");
} else {
str_append(str, "\",\"Mailboxes\": [");
int first = 1;
for (int i = 0; !IMAP_ARG_IS_EOL(&xaps_attr->mailboxes[i]); i++) {
const char *mailbox;
if (!imap_arg_get_astring(&(xaps_attr->mailboxes[i]), &mailbox)) {
return -1;
}
if (!first) {
str_append(str, ",");
}
str_append(str, "\"");
json_append_escaped(str, mailbox);
str_append(str, "\"");
first = 0;
}
str_append(str, "]");
}
str_append(str, "}");
i_debug("Sending registration: %s", str_c(str));
payload = i_stream_create_from_data(str_data(str), str_len(str));
i_stream_add_destroy_callback(payload, str_free_i, str);
http_client_request_set_payload(http_req, payload, FALSE);
http_client_request_submit(http_req);
i_stream_unref(&payload);
return 0;
}
/*
* Register the client at the xapsd
*/
static bool register_client(struct client_command_context *cmd, struct xaps_attr *xaps_attr) {
/*
* Forward to the helper daemon. The helper will return the
* aps-topic, which in reality is the subject of the certificate.
*/
if (xaps_register(cmd, xaps_attr) != 0) {
client_send_command_error(cmd, "Registration failed.");
return FALSE;
}
/*
* Dovecot only supports asynchronous http calls. So we wait for the http call to complete and write the
* aps-topic into the xaps_global struct.
*/
http_client_wait(xaps_global->http_client);
/*
* Return success. We assume that aps_version and aps_topic do not
* contain anything that needs to be escaped.
*/
client_send_line(cmd->client,
t_strdup_printf("* XAPPLEPUSHSERVICE aps-version \"%s\" aps-topic \"%s\"", xaps_attr->aps_version,
xaps_global->aps_topic));
client_send_tagline(cmd, "OK XAPPLEPUSHSERVICE completed.");
return TRUE;
}
/*
* Handle any XAPPLEPUSHSERVICE command
*/
static bool cmd_xapplepushservice(struct client_command_context *cmd) {
struct xaps_attr xaps_attr;
xaps_init(cmd->client->user, "/register", cmd->pool);
if (!parse_xapplepush(cmd, &xaps_attr)) {
return FALSE;
}
if (!register_client(cmd, &xaps_attr)) {
return FALSE;
}
return TRUE;
}
/**
* HTTP callback function for /register call
*/
void xaps_register_callback(const struct http_response *response, void *context) {
size_t size;
switch (response->status / 100) {
case 2:
// Success.
i_debug("Notification sent successfully: %s", http_response_get_message(response));
i_stream_read_data(response->payload, &xaps_global->aps_topic, &size, 128);
i_assert(size > 50);
break;
default:
// Error.
i_error("Error when sending notification: %s", http_response_get_message(response));
break;
}
}
/**
* This hook is called when a client has connected but before the
* capability string has been sent. We simply add XAPPLEPUSHSERVICE to
* the capabilities. This will trigger the usage of the
* XAPPLEPUSHSERVICE command by iOS clients.
*/
static void xaps_client_created(struct client **client) {
if (mail_user_is_plugin_loaded((*client)->user, xaps_imap_module)) {
str_append((*client)->capability_string, " XAPPLEPUSHSERVICE");
}
if (next_hook_client_created != NULL) {
next_hook_client_created(client);
}
}
/**
* This plugin method is called when the plugin is globally
* initialized. We register a new command, XAPPLEPUSHSERVICE, and also
* setup the client_created hook so that we can modify the
* capabilities string.
*/
void xaps_imap_plugin_init(struct module *module) {
command_register("XAPPLEPUSHSERVICE", cmd_xapplepushservice, 0);
xaps_imap_module = module;
next_hook_client_created = imap_client_created_hook_set(xaps_client_created);
}
/**
* This plugin method is called when the plugin is globally
* deinitialized. We unregister our command and remove the
* client_created hook.
*/
void xaps_imap_plugin_deinit(void) {
imap_client_created_hook_set(next_hook_client_created);
command_unregister("XAPPLEPUSHSERVICE");
push_notification_driver_xaps_cleanup();
}
/**
* This plugin only makes sense in the context of IMAP.
*/
const char xaps_imap_plugin_binary_dependency[] = "imap";