This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.c
227 lines (188 loc) · 5.85 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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <jansson.h>
#include "server.h"
#include "json.h"
#include "config.h"
#include "client.h"
#include "tls.h"
#include "main.h"
// signal Handler
int sckt;
void handleSigterm_S(int sig) {
if (sig == SIGTERM) {
term = 1;
shutdown(sckt,SHUT_RDWR);
}
}
void handleChild(int sig) {
if (sig == SIGCHLD) {
wait(NULL);
}
}
void server() {
// load Config File and Settings
fprintf(stdout, "Starting fidistat Server...\n");
openlog("fidistat-server", LOG_PID, LOG_DAEMON);
syslog(LOG_INFO, "Started Fidistat Server");
struct pidfh *pfh = daemon_start('s');
// Handle Signals
signal(SIGTERM, handleSigterm_S);
signal(SIGCHLD, handleChild);
// Open Socket
initConf();
tls_init();
struct tls* ctx = tls_server();
int sock = initTLS_S(ctx);
sckt = sock;
int connfd, pid;
listen(sock, 10);
// Destroy Config
destroyConf();
while(!term) {
connfd = accept(sock, (struct sockaddr*) NULL, NULL);
if (term) {
break;
}
pid = fork();
if (pid < 0) {
syslog(LOG_ERR, "forking new Worker failed");
} else if (pid == 0) {
close(sock);
syslog(LOG_INFO, "New incoming connection");
worker(connfd, ctx);
syslog(LOG_INFO, "Closing connection");
exit(0);
} else {
close(connfd);
}
}
syslog(LOG_INFO, "Shutting down Server");
close(sock);
tls_close(ctx);
tls_free(ctx);
tls_config_free(tlsServer_conf);
pidfile_remove(pfh);
syslog(LOG_INFO, "Stopped Fidistat Server");
closelog();
exit(0);
}
void worker(int connfd, struct tls* ctx) {
// Process HEADER
//--------------
struct tls* cctx = NULL;
tls_accept_socket(ctx, &cctx, connfd);
json_t *header = recvOverTLS(cctx);
const char* clientName = json_string_value(json_object_get(header, "from"));
for (size_t i = 0; i < sizeof(clientName); i++) {
if (clientName[i] == '/' || clientName[i] == '\\') {
syslog(LOG_ERR, "ERROR in clientName!, aborting");
return;
}
}
const char* clientAuth = json_string_value(json_object_get(header, "auth"));
if (strcmp(clientAuth, getClientAuth()) != 0) {
syslog(LOG_ERR, "Authentication failed");
return;
}
connType type = json_integer_value(json_object_get(header, "type"));
int size = json_integer_value(json_object_get(header, "size"));
// Process Payload
//----------------
// new Values for graphs
if (type == NEWDATA) {
for (int i = 0; i < size; i++) {
json_t* payload = recvOverTLS(cctx);
pasteJSON(payload, clientName);
}
}
// create .json or update displaysettings
if (type == CREATE || type == UPDATE) {
for (int i = 0; i < size; i++) {
json_t* payload = recvOverTLS(cctx);
const char * name = json_string_value(json_object_get(payload, "name"));
if (name == NULL) {
syslog(LOG_ERR, "Error in Message from Client");
return;
}
json_t *root = json_object_get(payload, "payload");
char* file = composeFileName(clientName, name, "json");
if (type == CREATE) {
dumpJSON(root, file);
} else {
mergeJSON(root,file);
}
}
}
// Look, which files are not available
if (type == HELLO) {
json_t *relist = json_array();
json_t *list = recvOverTLS(cctx);
for (int i = 0; i < size; i++) {
const char * name = json_string_value(json_array_get(list, i));
if (name == NULL) {
syslog(LOG_ERR, "Error in Message from Client");
return;
}
char* file = composeFileName(clientName, name, "json");
if (access( file, F_OK ) == -1) {
json_array_append_new(relist, json_string(name));
}
}
char * relistStr = json_dumps(relist, JSON_COMPACT);
sendOverTLS(cctx, relistStr);
free(relistStr);
}
// Delete all files from this client
if (type == DELETE) {
json_t *list = recvOverTLS(cctx);
for (int i = 0; i < size; i++) {
const char * name = json_string_value(json_array_get(list, i));
delete(clientName, name);
}
}
tls_close(cctx);
tls_free(cctx);
}
int initTLS_S(struct tls* ctx) {
tlsServer_conf = tls_config_new();
tls_config_set_cert_file(tlsServer_conf, getServerCertFile_v());
tls_config_set_key_file(tlsServer_conf, getServerCertFile_v());
tls_configure(ctx, tlsServer_conf);
int sock;
struct addrinfo *servinfo, *p;
servinfo = getAddrInfo();
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sock = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
syslog(LOG_ERR, "socket error");
continue;
}
if (bind(sock, p->ai_addr, p->ai_addrlen) == -1) {
close(sock);
syslog(LOG_ERR, "bind error");
continue;
}
break; // if we get here, we must have connected successfully
}
freeaddrinfo(servinfo);
syslog(LOG_DEBUG, "start socket\n");
return sock;
}
struct addrinfo* getAddrInfo() {
struct addrinfo hints, *servinfo;
memset(&hints, 0, sizeof hints);
if(getServerIPv6_v()) {
hints.ai_family = AF_INET6;
} else {
hints.ai_family = AF_INET;
}
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP address
getaddrinfo(NULL, getClientServerPort(), &hints, &servinfo);
return servinfo;
}