-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.c
253 lines (216 loc) · 6.57 KB
/
actions.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include "log.h"
#include "http.h"
#include "actions.h"
#include "./utf-8/utf.h"
static const char *HTTP_OK_RESPONSE =
"HTTP/1.1 204 OK\r\n" // Status-Line
"Connection: close\r\n" // Headers
"\r\n"; // Content break
static const char *HTTP_OK_TEXT_RESPONSE =
"HTTP/1.1 200 OK\r\n" // Status-Line
"Connection: close\r\n" // Headers
"Content-Length: 7\r\n"
"Content-Type: text/plain\r\n"
"\r\n" // Content break
"\n\n\tOk!\n"; // Content
static const char *HTTP_404_RESPONSE =
"HTTP/1.1 404 Not Found\r\n"
"Connection: close\r\n"
"\r\n";
static const char *HTTP_500_RESPONSE =
"HTTP/1.1 500 Internal Server Error\r\n"
"Connection: close\r\n"
"\r\n";
static const inline int file_exists(const char *path)
{
struct stat buffer;
return (stat(path, &buffer) == 0);
}
bool generic_action(int client_socket, HTTP_REQUEST req)
{
// Action data.
send(client_socket, HTTP_OK_RESPONSE,
strlen(HTTP_OK_RESPONSE), 0);
return true;
}
// A generic 204, let the client know that we accepted the message, but there
// is no content.
bool ok_action(int client_socket, HTTP_REQUEST req)
{
if (req.body)
{
DEBUG_LOG("We have a body\n%s\n", req.body);
}
// Action data.
send(client_socket, HTTP_OK_TEXT_RESPONSE,
strlen(HTTP_OK_TEXT_RESPONSE), 0);
return true;
}
// Error processing or no match, simply a 404.
bool not_found_action(int client_socket, HTTP_REQUEST req)
{
DEBUG_LOG("404 action!!\n");
// Action data.
send(client_socket, HTTP_404_RESPONSE,
strlen(HTTP_404_RESPONSE), 0);
return true;
}
bool sample_github_action(int client_socket, HTTP_REQUEST req)
{
static const char *repo_id = "276006277";
static const char *script_path = "../scripts/update_http_server.sh";
DEBUG_LOG("GitHub Webhook action for this project!\n");
// Parse the body, if it does not exist (not valid POST) return.
if (!req.body)
return false;
char *uncoded_body = (char *)malloc(sizeof(char *) * strlen(req.body));
convert_from_utf8(req.body, uncoded_body);
const char *needle = "repository\":{\"id\":";
char *id = strstr(uncoded_body, needle);
if (id == NULL)
{
free(uncoded_body);
return false;
}
id += strlen(needle);
char *end = strchr(id, ',');
if (end == NULL)
{
free(uncoded_body);
return false;
}
end[0] = '\0';
if (strcmp(id, repo_id) != 0)
{
DEBUG_LOG("GitHub id %s does not match %s\n", id, repo_id);
return false;
}
free(uncoded_body);
// Everything is good so far, perform the actual "action".
if (!file_exists(script_path))
{
ERROR_LOG("File %s not found\n", script_path);
send(client_socket, HTTP_500_RESPONSE,
strlen(HTTP_500_RESPONSE), 0);
return true;
}
int pid = fork();
if (pid == 0)
{
execl("/bin/sh", "sh", script_path, NULL);
// If exec fails we terminate the process.
kill(getpid(), SIGKILL);
}
DEBUG_LOG("Success! Letting GitHub know!\n");
send(client_socket, HTTP_OK_RESPONSE,
strlen(HTTP_OK_RESPONSE), 0);
return true;
}
bool update_server_action(int client_socket, HTTP_REQUEST req)
{
static const char *repo_id = "276006277";
static const char *script_path = "../scripts/update_http_server.sh";
DEBUG_LOG("GitHub Webhook action for this project!\n");
// Parse the body, if it does not exist (not valid POST) return.
if (!req.body)
return false;
char *uncoded_body = (char *)malloc(sizeof(char *) * strlen(req.body));
convert_from_utf8(req.body, uncoded_body);
const char *needle = "repository\":{\"id\":";
char *id = strstr(uncoded_body, needle);
if (id == NULL)
{
free(uncoded_body);
return false;
}
id += strlen(needle);
char *end = strchr(id, ',');
if (end == NULL)
{
free(uncoded_body);
return false;
}
end[0] = '\0';
if (strcmp(id, repo_id) != 0)
{
DEBUG_LOG("GitHub id %s does not match %s\n", id, repo_id);
free(uncoded_body);
return false;
}
free(uncoded_body);
// Everything is good so far, perform the actual "action".
if (!file_exists(script_path))
{
ERROR_LOG("File %s not found\n", script_path);
send(client_socket, HTTP_500_RESPONSE,
strlen(HTTP_500_RESPONSE), 0);
return true;
}
DEBUG_LOG("Success! Letting GitHub know!\n");
send(client_socket, HTTP_OK_RESPONSE,
strlen(HTTP_OK_RESPONSE), 0);
// Not great since we don't clean up but the alternative is a callback.
execl("/bin/sh", "sh", script_path, NULL);
return true;
}
bool update_website_action(int client_socket, HTTP_REQUEST req)
{
static const char *web_repo_id = "275560807";
static const char *blog_repo_id = "275565965";
static const char *script_path = "../scripts/update_website.sh";
DEBUG_LOG("GitHub Webhook action for website!\n");
// Parse the body, if it does not exist (not valid POST) return.
if (!req.body)
return false;
char *uncoded_body = (char *)malloc(sizeof(char *) * strlen(req.body));
convert_from_utf8(req.body, uncoded_body);
const char *needle = "repository\":{\"id\":";
char *id = strstr(uncoded_body, needle);
if (id == NULL)
{
free(uncoded_body);
return false;
}
id += strlen(needle);
char *end = strchr(id, ',');
if (end == NULL)
{
free(uncoded_body);
return false;
}
end[0] = '\0';
if (strcmp(id, web_repo_id) != 0 && strcmp(id, blog_repo_id) != 0)
{
free(uncoded_body);
DEBUG_LOG("GitHub id %s does not match provided repository ids\n", id);
return false;
}
free(uncoded_body);
// Everything is good so far, perform the actual "action".
if (!file_exists(script_path))
{
ERROR_LOG("File %s not found\n", script_path);
send(client_socket, HTTP_500_RESPONSE,
strlen(HTTP_500_RESPONSE), 0);
return true;
}
int pid = fork();
if (pid == 0)
{
execl("/bin/sh", "sh", script_path, NULL);
// If exec fails we terminate the process.
kill(getpid(), SIGKILL);
}
DEBUG_LOG("Success! Letting GitHub know!\n");
send(client_socket, HTTP_OK_RESPONSE,
strlen(HTTP_OK_RESPONSE), 0);
return true;
}