libpushover.c 6.5 KB
Newer Older
Shawn Webb's avatar
Shawn Webb committed
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
/*-
 * Copyright (c) 2020 HardenedBSD Foundation Corp.
 * Author: Shawn Webb <shawn.webb@hardenedbsd.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "libpushover.h"

static char *msg_to_str(pushover_ctx_t *, pushover_message_t *, CURL *);
Shawn Webb's avatar
Shawn Webb committed
35
static size_t pushover_curl_write_data(void *, size_t, size_t, void *);
Shawn Webb's avatar
Shawn Webb committed
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

EXPORTED_SYM
pushover_ctx_t *
pushover_init_ctx(const char *token)
{
	pushover_ctx_t *res;

	res = calloc(1, sizeof(*res));
	if (res == NULL)
		return (NULL);

	res->psh_uri = strdup(PUSHOVER_URI);
	if (res->psh_uri == NULL) {
		free(res);
		res = NULL;
		goto out;
	}

	if (token != NULL)
		res->psh_token = strdup(token);

out:
	return (res);
}

Shawn Webb's avatar
Shawn Webb committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
EXPORTED_SYM
void
pushover_free_ctx(pushover_ctx_t **ctx)
{
	pushover_ctx_t *ctxp;

	if (ctx == NULL || *ctx == NULL)
		return;

	ctxp = *ctx;

	free(ctxp->psh_uri);
	free(ctxp->psh_token);
	free(ctxp);
	*ctx = NULL;
}

Shawn Webb's avatar
Shawn Webb committed
78
79
80
81
82
83
84
85
86
87
88
89
EXPORTED_SYM
bool
pushover_set_uri(pushover_ctx_t *ctx, const char *uri)
{

	assert(ctx != NULL);
	assert(uri != NULL);

	ctx->psh_uri = strdup(uri);
	return (ctx->psh_uri != NULL);
}

90
91
92
93
94
95
96
97
98
99
100
101
EXPORTED_SYM
bool
pushover_set_token(pushover_ctx_t *ctx, const char *token)
{

	assert(ctx != NULL);
	assert(token != NULL);

	ctx->psh_token = strdup(token);
	return (ctx->psh_token != NULL);
}

Shawn Webb's avatar
Shawn Webb committed
102
103
104
105
EXPORTED_SYM
pushover_message_t *
pushover_init_message(pushover_message_t *msg)
{
106
	uint64_t flags;
Shawn Webb's avatar
Shawn Webb committed
107

108
109
110
	flags = 0;

	if (msg == NULL) {
Shawn Webb's avatar
Shawn Webb committed
111
		msg = calloc(1, sizeof(*msg));
112
113
		flags |= PUSHOVER_FLAGS_ALLOC;
	} else {
Shawn Webb's avatar
Shawn Webb committed
114
		memset(msg, 0, sizeof(*msg));
115
	}
Shawn Webb's avatar
Shawn Webb committed
116
117
118
119

	if (msg == NULL)
		return (NULL);

Shawn Webb's avatar
Shawn Webb committed
120
121
	msg->psh_flags = flags;

Shawn Webb's avatar
Shawn Webb committed
122
123
124
	return (msg);
}

125
126
127
128
129
130
131
132
133
134
135
EXPORTED_SYM
void
pushover_free_message(pushover_message_t **msg)
{
	pushover_message_t *msgp;

	if (msg == NULL || *msg == NULL)
		return;

	msgp = *msg;

136
	free(msgp->psh_dest);
137
138
139
140
141
142
143
144
145
146
	free(msgp->psh_msg);
	free(msgp->psh_title);
	free(msgp->psh_device);

	if (msgp->psh_flags & PUSHOVER_FLAGS_ALLOC) {
		free(msgp);
		*msg = NULL;
	}
}

Shawn Webb's avatar
Shawn Webb committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
EXPORTED_SYM
bool
pushover_message_set_msg(pushover_message_t *msg, char *data)
{

	assert(msg != NULL);
	assert(data != NULL);

	msg->psh_msg = strdup(data);
	return (msg->psh_msg != NULL);
}

EXPORTED_SYM
bool
161
pushover_message_set_dest(pushover_message_t *msg, char *dest)
Shawn Webb's avatar
Shawn Webb committed
162
163
164
{

	assert(msg != NULL);
165
	assert(dest != NULL);
Shawn Webb's avatar
Shawn Webb committed
166

167
168
	msg->psh_dest = strdup(dest);
	return (msg->psh_dest != NULL);
Shawn Webb's avatar
Shawn Webb committed
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
}

EXPORTED_SYM
bool
pushover_message_set_title(pushover_message_t *msg, char *title)
{

	assert(msg != NULL);
	assert(title != NULL);

	msg->psh_title = strdup(title);
	return (msg->psh_title != NULL);
}

EXPORTED_SYM
bool
pushover_message_set_device(pushover_message_t *msg, char *device)
{

	assert(msg != NULL);
	assert(device != NULL);

	msg->psh_device = strdup(device);
	return (msg->psh_device != NULL);
}

EXPORTED_SYM
bool
pushover_message_set_priority(pushover_message_t *msg,
    pushover_priority_t prio)
{

	assert(msg != NULL);
202

Shawn Webb's avatar
Shawn Webb committed
203
204
205
206
207
208
	if (!pushover_message_priority_sane(msg->psh_priority))
		return (false);

	msg->psh_priority = prio;

	return (true);
Shawn Webb's avatar
Shawn Webb committed
209
210
211
212
213
214
215
216
}

EXPORTED_SYM
bool
pushover_submit_message(pushover_ctx_t *ctx, pushover_message_t *msg)
{
	CURLcode curl_code;
	char *post_str;
217
	CURL *curl;
Shawn Webb's avatar
Shawn Webb committed
218
219
220
221
222
	bool res;

	assert(ctx != NULL);
	assert(msg != NULL);
	assert(ctx->psh_token != NULL);
223
	assert(msg->psh_dest != NULL);
Shawn Webb's avatar
Shawn Webb committed
224
	assert(msg->psh_msg != NULL);
225
	assert(pushover_message_priority_sane(msg->psh_priority));
Shawn Webb's avatar
Shawn Webb committed
226
227

	res = false;
Shawn Webb's avatar
Shawn Webb committed
228
	post_str = NULL;
Shawn Webb's avatar
Shawn Webb committed
229
230
231
232
233
234
235
236

	curl = curl_easy_init();
	if (curl == NULL)
		goto end;

	curl_easy_setopt(curl, CURLOPT_URL, ctx->psh_uri);

	post_str = msg_to_str(ctx, msg, curl);
237
	if (post_str == NULL)
Shawn Webb's avatar
Shawn Webb committed
238
239
		goto end;
	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_str);
240
241
	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
	curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
Shawn Webb's avatar
Shawn Webb committed
242
243
	curl_easy_setopt(curl, CURLOPT_STDERR, NULL);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, pushover_curl_write_data);
Shawn Webb's avatar
Shawn Webb committed
244
245
246
247
248
249
250
251
252
253
254
255
256

	curl_code = curl_easy_perform(curl);
	res = (curl_code == CURLE_OK);

end:
	curl_easy_cleanup(curl);
	free(post_str);
	return (res);
}

static char *
msg_to_str(pushover_ctx_t *ctx, pushover_message_t *msg, CURL *curl)
{
257
	char *res, *t_device, *t_msg, *t_title, *t_token, *t_dest;
Shawn Webb's avatar
Shawn Webb committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271

	assert(ctx != NULL);
	assert(msg != NULL);

	res = NULL;

	t_device = curl_easy_escape(curl,
	    msg->psh_device ? msg->psh_device : "", 0);
	t_msg = curl_easy_escape(curl,
	    msg->psh_msg ? msg->psh_msg : "", 0);
	t_title = curl_easy_escape(curl,
	    msg->psh_title ? msg->psh_title : "", 0);
	t_token = curl_easy_escape(curl,
	    ctx->psh_token, 0);
272
273
	t_dest = curl_easy_escape(curl,
	    msg->psh_dest, 0);
Shawn Webb's avatar
Shawn Webb committed
274
275
276
277
278

	if (t_device == NULL ||
	    t_msg == NULL ||
	    t_title == NULL ||
	    t_token == NULL ||
279
	    t_dest == NULL) {
Shawn Webb's avatar
Shawn Webb committed
280
281
282
283
284
285
286
287
288
289
290
		goto end;
	}

	asprintf(&res,
	    "token=%s&"
	    "user=%s&"
	    "message=%s&"
	    "title=%s&"
	    "priority=%d&"
	    "device=%s",
	    t_token,
291
	    t_dest,
Shawn Webb's avatar
Shawn Webb committed
292
293
294
295
296
297
298
299
300
301
	    t_msg,
	    t_title,
	    msg->psh_priority,
	    t_device);

end:
	curl_free(t_device);
	curl_free(t_msg);
	curl_free(t_title);
	curl_free(t_token);
302
	curl_free(t_dest);
Shawn Webb's avatar
Shawn Webb committed
303
304
305
306

	return (res);
}

Shawn Webb's avatar
Shawn Webb committed
307
308
309
310
311
312
313
314
static size_t
pushover_curl_write_data(void *buffer, size_t sz, size_t nmemb,
    void *usrp)
{

	return (sz * nmemb);
}

Shawn Webb's avatar
Shawn Webb committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
__attribute__((constructor))
static void
init_libpushover(void)
{

	curl_global_init(CURL_GLOBAL_ALL);
}

__attribute__((destructor))
static void
destroy_libpushover(void)
{

	curl_global_cleanup();
}