/
transport.c
304 lines (242 loc) · 7.63 KB
/
transport.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
/*
* Licensed to Cloudkick, Inc under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* Cloudkick licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _GNU_SOURCE
#include "ckl.h"
#include "ckl_version.h"
#include "extern/liboauth/src/oauth.h"
static void base_post_data(ckl_transport_t *t,
ckl_conf_t *conf,
const char *hostname)
{
curl_formadd(&t->formpost,
&t->lastptr,
CURLFORM_COPYNAME, "hostname",
CURLFORM_COPYCONTENTS, hostname,
CURLFORM_END);
if (conf->secret) {
curl_formadd(&t->formpost,
&t->lastptr,
CURLFORM_COPYNAME, "secret",
CURLFORM_COPYCONTENTS, conf->secret,
CURLFORM_END);
}
}
static int msg_to_post_data(ckl_transport_t *t,
ckl_conf_t *conf,
ckl_msg_t* m)
{
char buf[128];
snprintf(buf, sizeof(buf), "%d", (int)m->ts);
base_post_data(t, conf, m->hostname);
curl_formadd(&t->formpost,
&t->lastptr,
CURLFORM_COPYNAME, "username",
CURLFORM_COPYCONTENTS, m->username,
CURLFORM_END);
curl_formadd(&t->formpost,
&t->lastptr,
CURLFORM_COPYNAME, "msg",
CURLFORM_COPYCONTENTS, m->msg,
CURLFORM_END);
curl_formadd(&t->formpost,
&t->lastptr,
CURLFORM_COPYNAME, "ts",
CURLFORM_COPYCONTENTS, buf,
CURLFORM_END);
return 0;
}
static int list_to_post_data(ckl_transport_t *t,
ckl_conf_t *conf,
int count)
{
char buf[128];
const char *hostname = ckl_hostname();
snprintf(buf, sizeof(buf), "%d", count);
base_post_data(t, conf, hostname);
free((char*)hostname);
curl_formadd(&t->formpost,
&t->lastptr,
CURLFORM_COPYNAME, "count",
CURLFORM_COPYCONTENTS, buf,
CURLFORM_END);
return 0;
}
static int detail_to_post_data(ckl_transport_t *t,
ckl_conf_t *conf,
const char *slug)
{
const char *hostname = ckl_hostname();
base_post_data(t, conf, hostname);
free((char*)hostname);
curl_formadd(&t->formpost,
&t->lastptr,
CURLFORM_COPYNAME, "id",
CURLFORM_COPYCONTENTS, slug,
CURLFORM_END);
return 0;
}
static char *strappend(const char *a, const char *b)
{
size_t la = strlen(a);
size_t lb = strlen(b);
char *c = malloc(la + lb + 1);
memcpy(c, a, la);
memcpy(c+la, b, lb);
c[la+lb] = '\0';
return c;
}
static int ckl_transport_run(ckl_transport_t *t, ckl_conf_t *conf, ckl_msg_t* m)
{
long httprc = -1;
CURLcode res;
char *url = strdup(conf->endpoint);
char *postarg = NULL;
if (t->append_url) {
free(url);
url = strappend(conf->endpoint, t->append_url);
}
if (conf->oauth_key && conf->oauth_secret) {
int i;
int argc;
char **argv = NULL;
char *url2;
struct curl_httppost *tmp;
argc = oauth_split_post_paramters(url, &argv, 0);
for (tmp = t->formpost; tmp != NULL; tmp = tmp->next) {
char *p = NULL;
if (asprintf(&p, "%s=%s", tmp->name, tmp->contents) < 0) {
fprintf(stderr, "Broken asprintf: %s = %s\n",
tmp->name, tmp->contents);
return -1;
}
oauth_add_param_to_array(&argc, &argv, p);
free(p);
}
url2 = oauth_sign_array2(&argc, &argv, NULL,
OA_HMAC, "POST",
conf->oauth_key, conf->oauth_secret,
"", "");
//fprintf(stderr, "url = %s\n", url2);
free(url2);
curl_formfree(t->formpost);
t->formpost = NULL;
t->lastptr = NULL;
for (i = 1; i < argc; i++) {
char *s = strdup(argv[i]);
char *p = strchr(s, '=');
if (p == NULL) {
fprintf(stderr, "Broken argv: %s = %s\n",
tmp->name, tmp->contents);
return -1;
}
*p = '\0';
p++;
//fprintf(stderr, "argv[%d]: %s = %s\n", i, s, p);
curl_formadd(&t->formpost,
&t->lastptr,
CURLFORM_COPYNAME, s,
CURLFORM_COPYCONTENTS, p,
CURLFORM_END);
}
oauth_free_array(&argc, &argv);
}
if (m && m->script_log != NULL) {
curl_formadd(&t->formpost,
&t->lastptr,
CURLFORM_COPYNAME, "scriptlog",
CURLFORM_FILE, m->script_log,
CURLFORM_FILENAME, "script.log",
CURLFORM_CONTENTTYPE, "text/plain", CURLFORM_END);
}
curl_easy_setopt(t->curl, CURLOPT_HTTPPOST, t->formpost);
curl_easy_setopt(t->curl, CURLOPT_URL, url);
res = curl_easy_perform(t->curl);
if (res != 0) {
fprintf(stderr, "Failed talking to endpoint %s: (%d) %s\n\n",
conf->endpoint, res, curl_easy_strerror(res));
return -1;
}
curl_easy_getinfo(t->curl, CURLINFO_RESPONSE_CODE, &httprc);
if (httprc >299 || httprc <= 199) {
fprintf(stderr, "Endpoint %s returned HTTP %d\n",
conf->endpoint, (int)httprc);
if (httprc == 403) {
fprintf(stderr, "Are you sure your secret is correct?\n");
}
return -1;
}
free(url);
return 0;
}
int ckl_transport_msg_send(ckl_transport_t *t,
ckl_conf_t *conf,
ckl_msg_t* m)
{
int rv = msg_to_post_data(t, conf, m);
if (rv < 0) {
return rv;
}
return ckl_transport_run(t, conf, m);
}
int ckl_transport_list(ckl_transport_t *t,
ckl_conf_t *conf,
int count)
{
int rv = list_to_post_data(t, conf, count);
if (rv < 0) {
return rv;
}
t->append_url = "/list";
return ckl_transport_run(t, conf, NULL);
}
int ckl_transport_detail(ckl_transport_t *t,
ckl_conf_t *conf,
const char *slug)
{
int rv = detail_to_post_data(t, conf, slug);
if (rv < 0) {
return rv;
}
t->append_url = "/detail";
return ckl_transport_run(t, conf, NULL);
}
int ckl_transport_init(ckl_transport_t *t, ckl_conf_t *conf)
{
char uabuf[255];
static const char buf[] = "Expect:";
t->curl = curl_easy_init();
snprintf(uabuf, sizeof(uabuf), "ckl/%d.%d.%d (Changelog Client)",
CKL_VERSION_MAJOR, CKL_VERSION_MINOR, CKL_VERSION_PATCH);
curl_easy_setopt(t->curl, CURLOPT_USERAGENT, uabuf);
t->append_url = "/";
#ifdef CKL_DEBUG
curl_easy_setopt(t->curl, CURLOPT_VERBOSE, 1);
#endif
/* TODO: this is less than optimal */
curl_easy_setopt(t->curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(t->curl, CURLOPT_SSL_VERIFYHOST, 0L);
t->headerlist = curl_slist_append(t->headerlist, buf);
curl_easy_setopt(t->curl, CURLOPT_HTTPHEADER, t->headerlist);
return 0;
}
void ckl_transport_free(ckl_transport_t *t)
{
curl_easy_cleanup(t->curl);
curl_formfree(t->formpost);
curl_slist_free_all(t->headerlist);
free(t);
}