-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
248 lines (216 loc) · 8.12 KB
/
server.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
// Based on server by Nigel Griffiths (nag@uk.ibm.com),
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define VERSION 1
#define BUFSIZE 8096
#define ERROR 42
#define LOG 44
#define FORBIDDEN 403
#define NOTFOUND 404
#define LOGFILE "web.log"
#define SETTING_MAX 80
#define TOKEN "AF2BE4"
// OSX req'd
#ifndef SIGCLD
# define SIGCLD SIGCHLD
#endif
#define DEAMONIZED
enum APP_KEYS {KEY_TOKEN,KEY_PORT,KEY_LAST};
char *settings [KEY_LAST];
char bufSettings[KEY_LAST * SETTING_MAX];
void logger(int type, char *s1, char *s2, int socket_fd)
{
int fd ;
char logbuffer[BUFSIZE*2];
switch (type) {
case ERROR: (void)sprintf(logbuffer,"ERROR: %s:%s Errno=%d exiting pid=%d",s1, s2, errno,getpid());
break;
case FORBIDDEN:
(void)write(socket_fd, "HTTP/1.1 403 Forbidden\nContent-Length: 185\nConnection: close\nContent-Type: text/html\n\n<html><head>\n<title>403 Forbidden</title>\n</head><body>\n<h1>Forbidden</h1>\nThe requested URL, file type or operation is not allowed on this simple static file webserver.\n</body></html>\n",271);
(void)sprintf(logbuffer,"FORBIDDEN: %s:%s",s1, s2);
break;
case NOTFOUND:
(void)write(socket_fd, "HTTP/1.1 404 Not Found\nContent-Length: 136\nConnection: close\nContent-Type: text/html\n\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\nThe requested URL was not found on this server.\n</body></html>\n",224);
(void)sprintf(logbuffer,"NOT FOUND: %s:%s",s1, s2);
break;
case LOG: (void)sprintf(logbuffer," INFO: %s:%s:%d",s1, s2,socket_fd); break;
}
/* No checks here, nothing can be done with a failure anyway */
if((fd = open(LOGFILE, O_CREAT| O_WRONLY | O_APPEND,0644)) >= 0) {
(void)write(fd,logbuffer,strlen(logbuffer));
(void)write(fd,"\n",1);
(void)close(fd);
}
if(type == ERROR || type == NOTFOUND || type == FORBIDDEN) exit(3);
}
/* this is a child web server process, so we can exit on errors */
void web(int fd, int hit)
{
int file_fd, buflen,method;
long i, ret, len;
char *idx, *script, *args = "";
char *fstr = "text/plain";
static char buffer[BUFSIZE+1];
static char header[BUFSIZE];
/* read Web request in one go */
ret =read(fd,buffer,BUFSIZE);
if(ret == 0 || ret == -1) {
logger(FORBIDDEN,"failed to read browser request","",fd);
}
/* return code is valid chars */
if(ret > 0 && ret < BUFSIZE)
buffer[ret]=0; /* terminate the buffer */
else buffer[0]=0;
/* remove CF and LF characters */
for(i=0;i<ret;i++) {
if(buffer[i] == '\r' || buffer[i] == '\n')
buffer[i]='*';
/* make it shell safe */
if(buffer[i] == '\'')
buffer[i]='"';
}
logger(LOG,"request",buffer,hit);
idx = buffer;
if( strncmp(idx,"GET ",4) && strncmp(idx,"get ",4) && strncmp(idx,"POST ",5) && strncmp(idx,"post ",5) ){
logger(FORBIDDEN,"Only simple POST and GET requests supported",buffer,fd);
}
/* check for token .. */
idx = strstr(buffer,"/webhook/") + 9;
if( strncmp(idx,settings[KEY_TOKEN],strlen(settings[KEY_TOKEN]))){
logger(FORBIDDEN,"Invalid token",idx,fd);
}
idx += strlen(settings[KEY_TOKEN]);
script = idx + 1;
idx = strstr(buffer,"HTTP/");
*(idx-1) = 0;
/* Nothing to do if no key */
if(!strlen(script)){
logger(ERROR,"No script",buffer,fd);
}
if( 0 == (strncmp(buffer,"POST ",5) && strncmp(buffer,"post ",5)) ) {
/* Only support non-multipart post requests */
idx = strstr(idx,"****");
if(idx){
args = idx + 4;
}else{
logger(ERROR,"failed to read POST request",buffer,ret);
}
}else{
/* It's a get request */
idx += 5;
/* null terminate slashes and semicolons */
for(i=script-buffer;i<BUFSIZE;i++) {
if((buffer[i] == '/' ) || (buffer[i] == ';')){
buffer[i] = 0;
}
/* Pass GET arguments to script */
if(buffer[i] == '?' ){
buffer[i] = 0;
args = &buffer[i + 1];
}
/* Ignore rest of url */
if(buffer[i] == ' ') {
buffer[i] = 0;
break;
}
}
}
/* Header + a blank line */
static char head1[] = "HTTP/1.0 200 OK\n"
"Server: webhook/%d.0\n"
"Content-Length: %ld\n"
"Content-Type: %s\n\n" "%s";
/* pass the payload as arguments to the shell script */
(void)sprintf(buffer,"./%s '%s' ",script, args);
(void)sprintf(header, head1, VERSION, strlen(buffer), fstr, buffer);
#ifdef DEBUG
logger(LOG,"Header: ",header,fd);
logger(LOG,"Buffer: ",buffer,fd);
#endif
for(ret = strlen(header); ret>0; ret -= write(fd,header,strlen(header)));
ret = system(buffer);
sleep(1); /* allow socket to drain before signalling the socket is closed */
close(fd);
exit(1);
}
int main(int argc, char **argv)
{
int i, port, pid, listenfd, socketfd, hit;
socklen_t length;
static struct sockaddr_in cli_addr; /* static = initialised to zeros */
static struct sockaddr_in serv_addr; /* static = initialised to zeros */
if( argc < 3 || argc > 4 || !strcmp(argv[1], "-?") ) {
(void)printf("hint: server Port-Number Top-Directory [Token]\t\tversion %d\n\n"
"\twebhook is a mini web server desinged to handle github's webhooks\n"
"\tThere are no fancy features = safe and secure.\n\n"
"\tExample: webhook 8181 /home/webhook/scripts ABC123 &\n\n", VERSION);
exit(0);
}
for(i=0;i<KEY_LAST;i++){
settings[i] = &bufSettings[i* SETTING_MAX];
}
strncpy(settings[KEY_PORT],argv[1],SETTING_MAX);
strncpy(settings[KEY_TOKEN],TOKEN,SETTING_MAX);
if(argc == 4)
strncpy(settings[KEY_TOKEN],argv[3],SETTING_MAX);
if( !strncmp(argv[2],"/" ,2 ) || !strncmp(argv[2],"/etc", 5 ) ||
!strncmp(argv[2],"/bin",5 ) || !strncmp(argv[2],"/lib", 5 ) ||
!strncmp(argv[2],"/tmp",5 ) || !strncmp(argv[2],"/usr", 5 ) ||
!strncmp(argv[2],"/dev",5 ) || !strncmp(argv[2],"/sbin",6) ){
(void)printf("ERROR: Bad top directory %s, see server -?\n",argv[2]);
exit(3);
}
if(chdir(argv[2]) == -1){
(void)printf("ERROR: Can't Change to directory %s\n",argv[2]);
exit(4);
}
#ifdef DEAMONIZED
/* Become deamon + unstopable and no zombies children (= no wait()) */
if(fork() != 0)
return 0; /* parent returns OK to shell */
(void)signal(SIGCLD, SIG_IGN); /* ignore child death */
(void)signal(SIGHUP, SIG_IGN); /* ignore terminal hangups */
for(i=0;i<32;i++)
(void)close(i); /* close open files */
(void)setpgrp(); /* break away from process group */
#endif
logger(LOG,"server starting",settings[KEY_TOKEN],getpid());
/* setup the network socket */
if((listenfd = socket(AF_INET, SOCK_STREAM,0)) <0)
logger(ERROR, "system call","socket",0);
port = atoi(settings[KEY_PORT]);
if(port < 0 || port >60000)
logger(ERROR,"Invalid port number (try 1->60000)",settings[KEY_PORT],0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
if(bind(listenfd, (struct sockaddr *)&serv_addr,sizeof(serv_addr)) <0)
logger(ERROR,"system call","bind",0);
if( listen(listenfd,64) <0)
logger(ERROR,"system call","listen",0);
for(hit=1; ;hit++) {
length = sizeof(cli_addr);
if((socketfd = accept(listenfd, (struct sockaddr *)&cli_addr, &length)) < 0)
logger(ERROR,"system call","accept",0);
if((pid = fork()) < 0) {
logger(ERROR,"system call","fork",0);
}
else {
if(pid == 0) { /* child */
(void)close(listenfd);
web(socketfd,hit); /* never returns */
} else { /* parent */
(void)close(socketfd);
}
}
}
}