forked from cSploit/daemon
/
cSploitd.c
233 lines (193 loc) · 4.89 KB
/
cSploitd.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
/* cSploit - a simple penetration testing suite
* Copyright (C) 2014 Massimo Dragano aka tux_mind <tux_mind@csploit.org>
*
* cSploit 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 3 of the License, or
* (at your option) any later version.
*
* cSploit 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 cSploit. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <csploit/message.h>
#include <csploit/logger.h>
#include <csploit/file_logger.h>
#include "cSploitd.h"
#include "connection.h"
#include "cleanup.h"
#include "handler.h"
#include "reaper.h"
#include "authenticator.h"
int sockfd;
/**
* @brief delete an old socket
* @returns 0 if cannot delete it, -1 otherwise.
*/
int remove_old_socket() {
struct stat s;
if(!stat(SOCKET_PATH, &s)) {
// socket already exists
if(S_ISSOCK(s.st_mode)) {
if(unlink(SOCKET_PATH)) {
print( FATAL, "unlink: %s", strerror(errno) );
return -1;
}
} else {
print( FATAL, "'%s' is not a socket, remove it manually", SOCKET_PATH );
return -1;
}
}
return 0;
}
/**
* @brief initialize lists controls
* @returns 0 on success, -1 on error.
*/
int init_structs() {
if( control_init(&(connections.control)) ||
control_init(&(graveyard.control)) )
return -1;
return 0;
}
/**
* @brief destroy lists controls
*/
void destroy_structs() {
control_destroy(&(connections.control));
control_destroy(&(graveyard.control));
}
/**
* @brief stop the main thread by closing the main socket
*/
void stop_daemon() {
shutdown(sockfd, SHUT_RD);
}
int main(int argc, char **argv) {
struct sockaddr_un addr;
pid_t pid, sid;
int pipefd[2];
int clfd;
char deamonize;
if(argc==2 && !strncmp(argv[1], "-f", 3)) {
deamonize=0;
} else {
deamonize=1;
}
if(deamonize) {
if(pipe2(pipefd, O_CLOEXEC)) {
print( FATAL, "pipe2: %s", strerror(errno) );
return EXIT_FAILURE;
}
pid = fork();
if(pid<0) {
print( FATAL, "fork: %s", strerror(errno) );
return EXIT_FAILURE;
} else if(pid) {
close(pipefd[1]);
if(!read(pipefd[0], &clfd, 1))
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
close(pipefd[0]);
umask(0);
if(open_logfile(LOG_PATH)) {
print( FATAL, "cannot open logfile");
return EXIT_FAILURE;
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
set_logger(file_logger);
sid = setsid();
if(sid<0) {
print( FATAL, "setsid: %s", strerror(errno) );
return EXIT_FAILURE;
}
}
if(init_structs())
return EXIT_FAILURE;
if(load_handlers())
return EXIT_FAILURE;
if(load_users())
return EXIT_FAILURE;
if(remove_old_socket())
return EXIT_FAILURE;
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if(sockfd < 0) {
print( FATAL, "socket: %s", strerror(errno) );
return EXIT_FAILURE;
}
if(register_signal_handlers()) {
close(sockfd);
return EXIT_FAILURE;
}
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path)-1);
if(bind(sockfd, (struct sockaddr*)&addr, sizeof(addr))) {
print( FATAL, "bind: %s", strerror(errno) );
close(sockfd);
unlink(SOCKET_PATH);
return EXIT_FAILURE;
}
if(listen(sockfd, 5)) {
print( FATAL, "listen: %s", strerror(errno) );
close(sockfd);
unlink(SOCKET_PATH);
return EXIT_FAILURE;
}
if(start_reaper()) {
close(sockfd);
unlink(SOCKET_PATH);
return EXIT_FAILURE;
}
#ifndef NDEBUG
chmod(SOCKET_PATH, 0666);
#endif
if(deamonize) {
if(write(pipefd[1], "!", 1) != 1) {
print( FATAL, "cannot notify that daemon started" );
return EXIT_FAILURE;
}
close(pipefd[1]);
}
while(1) {
if((clfd=accept(sockfd, NULL, NULL)) < 0) {
if(errno == EINVAL) {
#ifndef NDEBUG
print( DEBUG, "socket closed" );
#endif
}
print( ERROR, "accept: %s", strerror(errno) );
break;
}
if(serve_new_client(clfd)) {
print( WARNING, "cannot serve new connection" );
close(clfd);
}
}
unlink(SOCKET_PATH);
close_connections();
stop_reaper();
destroy_structs();
unload_users();
unload_handlers();
return EXIT_SUCCESS;
}