-
Notifications
You must be signed in to change notification settings - Fork 73
/
rotator.c
394 lines (335 loc) · 9.35 KB
/
rotator.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
* ser2net - A program for allowing telnet connection to serial ports
* Copyright (C) 2001-2020 Corey Minyard <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-only
*
* In addition, as a special exception, the copyright holders of
* ser2net give you permission to combine ser2net with free software
* programs or libraries that are released under the GNU LGPL and
* with code included in the standard release of OpenSSL under the
* OpenSSL license (or modified versions of such code, with unchanged
* license). You may copy and distribute such a system following the
* terms of the GNU GPL for ser2net and the licenses of the other code
* concerned, provided that you include the source code of that
* other code when and as the GNU GPL requires distribution of source
* code.
*
* Note that people who make modified versions of ser2net are not
* obligated to grant this special exception for their modified
* versions; it is their choice whether to do so. The GNU General
* Public License gives permission to release a modified version
* without this exception; this exception also makes it possible to
* release a modified version which carries forward this exception.
*/
#include <stdlib.h>
#include <string.h>
#include <gensio/gensio.h>
#include <gensio/argvutils.h>
#include "ser2net.h"
#include "port.h"
#include "defaults.h"
typedef struct rotator
{
/* Rotators use the ports_lock for mutex. */
int curr_port;
const char **portv;
int portc;
char *name;
char *accstr;
struct gensio_accepter *accepter;
char *authdir;
char *pamauth;
struct gensio_list *allowed_users;
char *default_allowed_users;
unsigned int accepter_retry_time;
/* If the rotator fails startup, start time timer to retry it. */
struct gensio_timer *restart_timer;
struct rotator *next;
} rotator_t;
static rotator_t *rotators = NULL;
/* Returns with the port locked, if non-NULL. */
static port_info_t *
find_rotator_port(const char *portname, struct gensio *net,
unsigned int *netconnum)
{
port_info_t *port = ports;
while (port) {
if (strcmp(port->name, portname) == 0) {
unsigned int i;
struct sockaddr_storage addr;
gensiods socklen;
int err;
so->lock(port->lock);
if (!port->enabled)
goto next;
if (port->dev_to_net_state == PORT_CLOSING ||
port->dev_to_net_state == PORT_CLOSED)
goto next;
err = net_raddr(net, &addr, &socklen);
if (err)
goto next;
if (!remaddr_check(port->remaddrs,
(struct sockaddr *) &addr, socklen))
goto next;
if (port->net_to_dev_state == PORT_UNCONNECTED &&
is_device_already_inuse(port))
goto next;
for (i = 0; i < port->max_connections; i++) {
if (!port->netcons[i].net) {
*netconnum = i;
return port;
}
}
next:
so->unlock(port->lock);
}
port = port->next;
}
return NULL;
}
/* A connection request has come in on a port. */
static int
rot_new_con(rotator_t *rot, struct gensio *net)
{
int i;
const char *err;
so->lock(ports_lock);
i = rot->curr_port;
do {
unsigned int netconnum = 0;
port_info_t *port = find_rotator_port(rot->portv[i], net, &netconnum);
if (++i >= rot->portc)
i = 0;
if (port) {
rot->curr_port = i;
so->unlock(ports_lock);
handle_new_net(port, net, &port->netcons[netconnum]);
so->unlock(port->lock);
return 0;
}
} while (i != rot->curr_port);
so->unlock(ports_lock);
err = "No free port found\r\n";
gensio_write(net, NULL, err, strlen(err), NULL);
gensio_free(net);
return 0;
}
static int
handle_rot_child_event(struct gensio_accepter *accepter, void *user_data,
int event, void *data)
{
rotator_t *rot = user_data;
if (event == GENSIO_ACC_EVENT_LOG) {
do_gensio_log(rot->accstr, data);
return 0;
}
switch (event) {
case GENSIO_ACC_EVENT_NEW_CONNECTION:
return rot_new_con(rot, data);
#ifdef GENSIO_ACC_EVENT_PARMLOG
case GENSIO_ACC_EVENT_PARMLOG: {
struct gensio_parmlog_data *d = (struct gensio_parmlog_data *) data;
seout.vout(&seout, d->log, d->args);
return 0;
}
#endif
default:
return handle_acc_auth_event(rot->authdir, rot->pamauth,
rot->allowed_users, event, data);
}
}
static struct gensio_waiter *rotator_shutdown_wait;
static void
handle_rot_shutdown_done(struct gensio_accepter *accepter, void *cb_data)
{
so->wake(rotator_shutdown_wait);
}
static void
rot_timer_shutdown_done(struct gensio_timer *timer, void *cb_data)
{
so->wake(rotator_shutdown_wait);
}
static void
free_rotator(rotator_t *rot)
{
int err;
unsigned int free_count = 0;
if (rot->accepter) {
err = gensio_acc_shutdown(rot->accepter, handle_rot_shutdown_done, rot);
if (!err)
free_count++;
}
if (rot->restart_timer) {
err = so->stop_timer_with_done(rot->restart_timer,
rot_timer_shutdown_done, rot);
if (err != GE_TIMEDOUT)
free_count++;
}
if (free_count)
so->wait(rotator_shutdown_wait, free_count, NULL);
if (rot->accepter)
gensio_acc_free(rot->accepter);
if (rot->authdir)
free(rot->authdir);
if (rot->pamauth)
free(rot->pamauth);
free_user_list(rot->allowed_users);
if (rot->default_allowed_users)
free(rot->default_allowed_users);
if (rot->name)
free(rot->name);
if (rot->accstr)
free(rot->accstr);
if (rot->portv)
gensio_argv_free(so, rot->portv);
if (rot->restart_timer)
so->free_timer(rot->restart_timer);
free(rot);
}
void
free_rotators(void)
{
rotator_t *rot, *next;
rot = rotators;
while (rot) {
next = rot->next;
free_rotator(rot);
rot = next;
}
rotators = NULL;
}
static void
rot_timeout(struct gensio_timer *timer, void *cb_data)
{
rotator_t *rot = cb_data;
int rv;
rv = gensio_acc_startup(rot->accepter);
if (rv) {
gensio_time timeout = { rot->accepter_retry_time, 0 };
seout.out(&seout, "Failed to start rotator: %s", gensio_err_to_str(rv));
so->start_timer(rot->restart_timer, &timeout);
}
}
void
shutdown_rotators(void)
{
if (rotator_shutdown_wait)
so->free_waiter(rotator_shutdown_wait);
}
int
init_rotators(void)
{
rotator_shutdown_wait = so->alloc_waiter(so);
if (!rotator_shutdown_wait)
return GE_NOMEM;
return 0;
}
int
add_rotator(struct absout *eout, const char *name, const char *accstr,
int portc, const char **ports, const char **options, int lineno)
{
rotator_t *rot;
int rv;
rot = malloc(sizeof(*rot));
if (!rot)
return GE_NOMEM;
memset(rot, 0, sizeof(*rot));
rot->name = strdup(name);
if (!rot->name)
goto out_nomem;
rot->accstr = strdup(accstr);
if (!rot->accstr)
goto out_nomem;
if (find_default_str("authdir", &rot->authdir))
goto out_nomem;
if (find_default_str("pamauth", &rot->pamauth))
goto out_nomem;
if (find_default_str("allowed-users", &rot->default_allowed_users))
goto out_nomem;
rot->accepter_retry_time = find_default_int("accepter-retry-time");
if (options) {
unsigned int i;
const char *str;
for (i = 0; options[i]; i++) {
if (gensio_check_keyvalue(options[i], "authdir", &str) > 0) {
if (rot->authdir)
free(rot->authdir);
rot->authdir = strdup(str);
if (!rot->authdir) {
eout->out(eout, "Out of memory allocating rotator"
" authdir on line %d\n", lineno);
goto out_nomem;
}
continue;
} else if (gensio_check_keyvalue(options[i], "pamauth", &str) > 0) {
if (rot->pamauth)
free(rot->pamauth);
rot->pamauth = strdup(str);
if (!rot->pamauth) {
eout->out(eout, "Out of memory allocating rotator"
" pamauth on line %d\n", lineno);
goto out_nomem;
}
continue;
} else if (gensio_check_keyvalue(options[i],
"allowed-users", &str) > 0) {
rv = add_allowed_users(&rot->allowed_users, str, eout);
if (rv)
goto out_err;
continue;
} else if (gensio_check_keyuint(options[i], "accepter-retry-time",
&rot->accepter_retry_time) > 0) {
if (rot->accepter_retry_time < 1)
rot->accepter_retry_time = 1;
continue;
}
free_rotator(rot);
eout->out(eout, "Invalid option %s for rotator on line %d\n",
options[i], lineno);
return GE_INVAL;
}
}
rot->restart_timer = so->alloc_timer(so, rot_timeout, rot);
if (!rot->restart_timer) {
eout->out(eout, "Unable to allocate timer on line %d", lineno);
goto out_nomem;
}
rot->portc = portc;
rot->portv = ports;
rv = str_to_gensio_accepter(rot->accstr, so,
handle_rot_child_event, rot, &rot->accepter);
if (rv) {
eout->out(eout, "accepter was invalid on line %d", lineno);
goto out_err;
}
if (!rot->allowed_users && rot->default_allowed_users) {
rv = add_allowed_users(&rot->allowed_users, rot->default_allowed_users,
eout);
if (rv)
goto out_err;
}
if (rot->default_allowed_users) {
free(rot->default_allowed_users);
rot->default_allowed_users = NULL;
}
rot->next = rotators;
rotators = rot;
rv = gensio_acc_startup(rot->accepter);
if (rv) {
gensio_time timeout = { rot->accepter_retry_time, 0 };
eout->out(eout, "Failed to start rotator on line %d: %s", lineno,
gensio_err_to_str(rv));
so->start_timer(rot->restart_timer, &timeout);
/* Don't error out, retry. */
}
return 0;
out_nomem:
rv = GE_NOMEM;
out_err:
/* If we fail, the user should free these. */
rot->portc = 0;
rot->portv = NULL;
free_rotator(rot);
return rv;
}