-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
357 lines (287 loc) · 7.46 KB
/
main.cpp
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/***************************************************************************
* Copyright (C) 2006 by Dmitriy Gorbenko *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <csignal>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "sys/wait.h"
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#include "define.h"
#include "dialog.h"
#include "config.h"
#include "message.h"
#include "params.h"
#include "str.h"
#include "tools.h"
#include "main.h"
#include "ping.h"
using namespace std;
extern struct params_def startup_params;
extern struct mess_state_def m_state;
extern struct config_data_def config_data;
extern struct mess_state_def m_state;
extern unsigned int broken_pipe;
struct { uid_t real; uid_t effective; } uid_startup;
unsigned int child_count = 0;
char * process_id;
void Signal_TERM(int sig)
{
if (sig == SIGINT)
p_error("Cought SIGINT\n");
else if (sig == SIGHUP)
p_error("Cought SIGHUP\n");
else if (sig == SIGTERM)
p_error("Cought SIGTERM\n");
else if (sig == SIGSEGV)
p_error("Cought SIGSEGV\n");
config_shutdown();
message_shutdown();
exit(0);
};
void Signal_PIPE(int sig)
{
if (sig == SIGPIPE) {
broken_pipe = TRUE;
}
return;
};
void Signal_CHILD(int sig)
{
if (sig == SIGCHLD) {
wait(NULL);
child_count--;
}
return;
};
void Set_Signal_Handler(void)
{
signal(SIGINT, Signal_TERM);
signal(SIGHUP, Signal_TERM);
signal(SIGTERM, Signal_TERM);
signal(SIGSEGV, Signal_TERM);
signal(SIGPIPE, Signal_PIPE);
signal(SIGCHLD, Signal_CHILD);
};
unsigned int become_root()
{
uid_t real;
uid_t effective;
real = getuid();
effective = geteuid();
uid_startup.real = real;
uid_startup.effective = effective;
if (real == 0) {
return TRUE;
}
if (effective != 0) {
p_error("You are not root or file haven't '+s' flag or file owner is not 'root'\n");
return FALSE;
}
// Becoming root...
if (setreuid(effective, effective) == -1) {
p_error("I need root privileges to do my work.\n");
return FALSE;
}
return TRUE;
};
void shutdown()
{
config_shutdown();
message_shutdown();
exit(0);
}
int main(int argc, char *argv[])
{
unsigned int result = FALSE;
int listenfd = 0;
int clientfd = 0;
int rr = 0;
int x = 1, n = 0;
int fork_res;
char ** IPs, *IP, ** copy, *tmp;
struct sockaddr_in listaddr;
struct sockaddr_in cliaddr;
socklen_t clilen;
Set_Signal_Handler();
// *********************************************
// Startup Init
m_state.log_init = FALSE;
process_id = strdup("main");
reset_params();
// startup params
if (parse_params(argc, argv) != TRUE) {
p_error("Failed on parse_params()\n");
exit(0);
}
if (startup_params.view_help == TRUE)
exit(0);
// become root
if (ROOT_COMPILE) {
if (become_root() != TRUE) {
p_error("Can't become root\n");
exit(0);
}
}
// log subsystem
if (startup_params.alternative_log == TRUE)
result = message_init(startup_params.log_file);
else
result = message_init(LOG_FILE);
if (result == FALSE) {
p_error("Failed on message_init()\n");
exit(0);
}
// config file
if (startup_params.alternative_config == TRUE)
result = config_init(startup_params.config_file);
else
result = config_init(CONFIG_FILE);
if (result == FALSE) {
p_error("Failed on config_init()\n");
shutdown();
}
result = read_config();
if (result == FALSE) {
p_error("Failed on read_config()\n");
shutdown();
}
if (check_config() == FALSE) {
p_error("Failed on check_config()\n");
shutdown();
}
// *********************************************
// Network Init
IPs = split(config_data.listen_ip, ',');
n = 0; copy = IPs;
while(*IPs) {
n++;
*IPs++;
}
IPs = copy;
if (n > 1) {
while(*IPs) {
fork_res = fork();
if (fork_res == 0){
tmp = int_to_string(n);
process_id = sts(&process_id, tmp);
safe_free(&tmp);
goto new_child;
}
*IPs++;
if (--n == 1)
break;
}
}
new_child:
IP = *IPs;
if (!IP) {
p_error("Bad IP address: null\n");
shutdown();
}
if (strlen(IP) == 0) {
p_error("Bad IP address: zero length\n");
shutdown();
}
listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenfd < 0) {
p_error("Failed on socket()\n");
shutdown();
}
rr = setsockopt (listenfd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof (x));
if (rr == -1) {
p_error("listenfd reuseaddr failed\n");
shutdown();
}
memset((void *) &listaddr, '\0', (size_t) sizeof(listaddr));
listaddr.sin_family = AF_INET;
result = get_s_addr(IP, &(listaddr.sin_addr));
if (result == FALSE) {
p_error("Failed on get_s_addr() (IP: %s)\n", IP);
shutdown();
}
listaddr.sin_port = htons(config_data.listen_port);
if (bind(listenfd, (struct sockaddr *) &listaddr, sizeof(listaddr)) < 0) {
p_error("Failed on bind() (IP: %s, port: %d)\n", config_data.listen_ip, config_data.listen_port);
shutdown();
}
if (listen(listenfd, 256) < 0) {
p_error("Failed on listen()\n");
shutdown();
}
// *********************************************
// Some isues
if (startup_params.become_daemon == TRUE) {
if (daemon(0, 1) == -1) {
p_error("Failed on daemon()\n");
shutdown();
}
}
// *********************************************
// Some isues
message("Server started...\n");
// *********************************************
// Main loop
for (;;) {
memset((void *) &cliaddr, '\0', sizeof(cliaddr));
clilen = (socklen_t) sizeof(cliaddr);
clientfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
if (clientfd != 0 && child_count < CHILD_MAX_COUNT) {
fork_res = fork();
if (fork_res == 0){
safe_free(&process_id);
process_id = create_unique_id();
begin_dialog(clientfd, cliaddr);
safe_free(&process_id);
exit(EXIT_SUCCESS);
}
if (fork_res != 0) {
child_count++;
close(clientfd);
}
}
else {
if (clientfd != 0)
close(clientfd);
}
close(clientfd);
}
// *********************************************
// Shutdown
config_shutdown();
message_shutdown();
safe_free(&process_id);
exit(0);
/* silence compiler warnings */
return EXIT_SUCCESS;
(void) argc;
(void) argv;
};