-
Notifications
You must be signed in to change notification settings - Fork 30
/
vty.c
261 lines (205 loc) · 5.36 KB
/
vty.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
254
255
256
257
258
259
260
261
#include "config.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include "utils.h"
#include "vty.h"
#include "vty_auth.h"
#include "vty_utils.h"
STATIC pthread_mutex_t knet_vty_mutex = PTHREAD_MUTEX_INITIALIZER;
STATIC int vty_max_connections = KNET_VTY_DEFAULT_MAX_CONN;
STATIC int vty_current_connections = 0;
STATIC pthread_t vty_thread[KNET_VTY_TOTAL_MAX_CONN];
STATIC int daemon_quit = 0;
static void sigterm_handler(int sig)
{
daemon_quit = 1;
}
static void sigpipe_handler(int sig)
{
return;
}
static void *vty_accept_thread(void *arg)
{
int vty_sock = *(int *)arg;
knet_vty_print_banner(vty_sock);
if (knet_vty_auth_user(vty_sock) < 0)
goto out_clean;
out_clean:
pthread_mutex_lock(&knet_vty_mutex);
close(vty_sock);
vty_current_connections--;
pthread_mutex_unlock(&knet_vty_mutex);
return NULL;
}
/*
* mainloop is not thread safe as there should only be one
*/
int knet_vty_main_loop(const char *configfile, const char *ip_addr,
const unsigned short port)
{
int vty_listener_fd;
int vty_accept_fd;
struct sockaddr incoming_sa;
socklen_t salen;
fd_set rfds;
int se_result = 0;
struct timeval tv;
int err = 0;
signal(SIGTERM, sigterm_handler);
signal(SIGPIPE, sigpipe_handler);
// read and process config file here
vty_listener_fd = knet_vty_init_listener(ip_addr, port);
if (vty_listener_fd < 0) {
log_error("Unable to setup vty listener");
return -1;
}
while (se_result >= 0 && !daemon_quit) {
FD_ZERO (&rfds);
FD_SET (vty_listener_fd, &rfds);
tv.tv_sec = 1;
tv.tv_usec = 0;
se_result = select((vty_listener_fd + 1), &rfds, 0, 0, &tv);
if ((se_result == -1) && (daemon_quit)) {
log_info("Got a SIGTERM, goodbye");
goto out;
}
if (se_result == -1) {
err = se_result;
log_error("Unable to select on vty listener socket!");
goto out;
}
if ((se_result == 0) || (!FD_ISSET(vty_listener_fd, &rfds)))
continue;
memset(&incoming_sa, 0, sizeof(struct sockaddr));
salen = 0;
vty_accept_fd = accept(vty_listener_fd, &incoming_sa, &salen);
if (vty_accept_fd < 0) {
log_error("Unable to accept connection to vty");
continue;
}
// check for ip address access list here against incoming_sa
pthread_mutex_lock(&knet_vty_mutex);
if (vty_current_connections == vty_max_connections) {
errno = ECONNREFUSED;
log_error("Too many connections to VTY");
close(vty_accept_fd);
pthread_mutex_unlock(&knet_vty_mutex);
continue;
}
vty_current_connections++;
err = pthread_create(&vty_thread[vty_current_connections],
NULL, vty_accept_thread,
(void *)&vty_accept_fd);
if (err < 0) {
log_error("Unable to spawn vty thread");
vty_current_connections--;
}
pthread_mutex_unlock(&knet_vty_mutex);
}
out:
knet_vty_close_listener(vty_listener_fd);
// reverse running config to close/release resources;
return err;
}
void knet_vty_set_max_connections(const int max_connections)
{
pthread_mutex_lock(&knet_vty_mutex);
vty_max_connections = max_connections;
pthread_mutex_unlock(&knet_vty_mutex);
}
int knet_vty_init_listener(const char *ip_addr, const unsigned short port)
{
int sockfd = -1, sockopt = 1;
int socktype = SOCK_STREAM;
int af_family = AF_INET6;
int salen = 0, err = 0;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
pthread_mutex_lock(&knet_vty_mutex);
/* handle sigpipe if we decide to use KEEPALIVE */
/*
* I REALLY HATE MYSELF FOR WRITING THIS PIECE OF CRAP
* but it gets the job done
*/
if ((ip_addr) &&
(strlen(ip_addr)) &&
(!strchr(ip_addr, ':'))) {
af_family = AF_INET;
}
sockfd = socket(af_family, socktype, 0);
if ((sockfd < 0) &&
(errno == EAFNOSUPPORT) &&
(af_family = AF_INET6)) {
af_family = AF_INET;
sockfd = socket(af_family, socktype, 0);
}
if (sockfd < 0) {
err = sockfd;
goto out_clean;
}
err = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(void *)&sockopt, sizeof(sockopt));
if (err)
goto out_clean;
if (knet_fdset_cloexec(sockfd) < 0) {
err = -1;
goto out_clean;
}
if (af_family == AF_INET) {
salen = sizeof(struct sockaddr_in);
memset(&sin, 0, salen);
sin.sin_family = af_family;
sin.sin_port = htons(port);
if ((!ip_addr) || (!strlen(ip_addr)))
sin.sin_addr.s_addr = htonl(INADDR_ANY);
else
if (inet_pton(af_family, ip_addr, &sin.sin_addr) <= 0) {
err = -1;
goto out_clean;
}
sin.sin_port = htons(port);
err = bind(sockfd, (struct sockaddr *)&sin, salen);
} else {
salen = sizeof(struct sockaddr_in6);
memset(&sin6, 0, salen);
sin6.sin6_family = af_family;
sin6.sin6_port = htons(port);
if ((!ip_addr) || (!strlen(ip_addr)))
memcpy(&sin6.sin6_addr, &in6addr_any, sizeof(struct in6_addr));
else
if (inet_pton(af_family, ip_addr, &sin6.sin6_addr) <= 0) {
err = -1;
goto out_clean;
}
err = bind(sockfd, (struct sockaddr *)&sin6, salen);
}
if (err)
goto out_clean;
err = listen(sockfd, 0);
if (err)
goto out_clean;
pthread_mutex_unlock(&knet_vty_mutex);
return sockfd;
out_clean:
if (sockfd >= 0)
close(sockfd);
pthread_mutex_unlock(&knet_vty_mutex);
return err;
}
void knet_vty_close_listener(int listener_fd)
{
pthread_mutex_lock(&knet_vty_mutex);
if (listener_fd <= 0)
goto out_clean;
close(listener_fd);
listener_fd = 0;
out_clean:
pthread_mutex_unlock(&knet_vty_mutex);
return;
}