forked from chargebyte/ser2net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ser2net.c
334 lines (294 loc) · 7.83 KB
/
ser2net.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
/*
* ser2net - A program for allowing telnet connection to serial ports
* Copyright (C) 2001 Corey Minyard <[email protected]>
*
* 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
*/
/* This is the entry point for the ser2net program. It reads
parameters, initializes everything, then starts the select loop. */
/* TODO
*
* Add some type of security
*/
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include "ser2net.h"
#include "readconfig.h"
#include "controller.h"
#include "utils.h"
#include "selector.h"
#include "dataxfer.h"
static char *config_file = "/etc/ser2net.conf";
int config_port_from_cmdline = 0;
char *config_port = NULL; /* Can be set from readconfig, too. */
static char *pid_file = NULL;
static int detach = 1;
static int debug = 0;
#ifdef USE_UUCP_LOCKING
int uucp_locking_enabled = 1;
#endif
selector_t *ser2net_sel;
char *rfc2217_signature = "ser2net";
static char *help_string =
"%s: Valid parameters are:\n"
" -c <config file> - use a config file besides /etc/ser2net.conf\n"
" -C <config line> - Handle a single configuration line. This may be\n"
" specified multiple times for multiple lines. This is just like a\n"
" line in the config file. This disables the default config file,\n"
" you must specify a -c after the last -C to have it read a config\n"
" file, too.\n"
" -p <controller port> - Start a controller session on the given TCP port\n"
" -P <file> - set location of pid file\n"
" -n - Don't detach from the controlling terminal\n"
" -d - Don't detach and send debug I/O to standard output\n"
#ifdef USE_UUCP_LOCKING
" -u - Disable UUCP locking\n"
#endif
" -b - unused (was Do CISCO IOS baud-rate negotiation, instead of RFC2217)\n"
" -v - print the program's version and exit\n"
" -s - specify a default signature for RFC2217 protocol\n";
void
reread_config(void)
{
if (config_file) {
char *prev_config_port = config_port;
config_port = NULL;
syslog(LOG_INFO, "Got SIGHUP, re-reading configuration");
readconfig(config_file);
if (config_port_from_cmdline) {
/* Never override the config port from the command line. */
free(config_port);
config_port = prev_config_port;
goto config_port_unchanged;
}
if (config_port && prev_config_port
&& (strcmp(config_port, prev_config_port) == 0)) {
free(prev_config_port);
goto config_port_unchanged;
}
if (prev_config_port) {
controller_shutdown();
free(prev_config_port);
}
if (config_port) {
int rv = controller_init(config_port);
if (rv == CONTROLLER_INVALID_TCP_SPEC)
syslog(LOG_ERR, "Invalid control port specified: %s",
config_port);
else if (rv == CONTROLLER_OUT_OF_MEMORY)
syslog(LOG_ERR, "Out of memory opening control port: %s",
config_port);
else if (rv == CONTROLLER_CANT_OPEN_PORT)
syslog(LOG_ERR, "Can't open control port: %s",
config_port);
if (rv) {
syslog(LOG_ERR, "Control port is disabled");
free(config_port);
config_port = NULL;
}
}
}
config_port_unchanged:
return;
}
void
arg_error(char *name)
{
fprintf(stderr, help_string, name);
exit(1);
}
void
make_pidfile(char *pidfile)
{
FILE *fpidfile;
if (!pidfile)
return;
fpidfile = fopen(pidfile, "w");
if (!fpidfile) {
syslog(LOG_WARNING,
"Error opening pidfile '%s': %m, pidfile not created",
pidfile);
return;
}
fprintf(fpidfile, "%d\n", getpid());
fclose(fpidfile);
}
void
shutdown_cleanly(void)
{
shutdown_ports();
do {
if (check_ports_shutdown())
exit(1);
sel_select_once(ser2net_sel);
} while(1);
}
int
main(int argc, char *argv[])
{
int i;
int err;
err = sel_alloc_selector(&ser2net_sel);
if (err) {
fprintf(stderr,
"Could not initialize ser2net selector: '%s'\n",
strerror(err));
return -1;
}
for (i=1; i<argc; i++) {
if ((argv[i][0] != '-') || (strlen(argv[i]) != 2)) {
fprintf(stderr, "Invalid argument: '%s'\n", argv[i]);
arg_error(argv[0]);
}
switch (argv[i][1]) {
case 'n':
detach = 0;
break;
case 'd':
detach = 0;
debug = 1;
break;
case 'b':
break;
case 'C':
/* Get a config line. */
i++;
if (i == argc) {
fprintf(stderr, "No config line specified with -C\n");
arg_error(argv[0]);
}
handle_config_line(argv[i]);
config_file = NULL;
break;
case 'c':
/* Get a config file. */
i++;
if (i == argc) {
fprintf(stderr, "No config file specified with -c\n");
arg_error(argv[0]);
}
config_file = argv[i];
break;
case 'p':
/* Get the control port. */
i++;
if (i == argc) {
fprintf(stderr, "No control port specified with -p\n");
arg_error(argv[0]);
}
config_port = strdup(argv[i]);
if (!config_port) {
fprintf(stderr, "Could not allocate memory for -p\n");
exit(1);
}
config_port_from_cmdline = 1;
break;
case 'P':
i++;
if (i == argc) {
fprintf(stderr, "No pid file specified with -P\n");
arg_error(argv[0]);
}
pid_file = argv[i];
break;
#ifdef USE_UUCP_LOCKING
case 'u':
uucp_locking_enabled = 0;
break;
#endif
case 'v':
printf("%s version %s\n", argv[0], VERSION);
exit(0);
case 's':
i++;
if (i == argc) {
fprintf(stderr, "No signature specified\n");
exit(1);
}
rfc2217_signature = argv[i];
break;
default:
fprintf(stderr, "Invalid option: '%s'\n", argv[i]);
arg_error(argv[0]);
}
}
setup_signals();
if (debug && !detach)
openlog("ser2net", LOG_PID | LOG_CONS | LOG_PERROR, LOG_DAEMON);
if (config_file) {
if (readconfig(config_file) == -1) {
return 1;
}
}
if (config_port != NULL) {
int rv;
rv = controller_init(config_port);
if (rv == CONTROLLER_INVALID_TCP_SPEC) {
fprintf(stderr, "Invalid control port specified: %s\n",
config_port);
arg_error(argv[0]);
}
if (rv == CONTROLLER_CANT_OPEN_PORT) {
fprintf(stderr, "Unable to open control port, see syslog: %s\n",
config_port);
exit(1);
}
}
if (detach) {
int pid;
/* Detach from the calling terminal. */
openlog("ser2net", LOG_PID | LOG_CONS, LOG_DAEMON);
syslog(LOG_NOTICE, "ser2net startup");
if ((pid = fork()) > 0) {
exit(0);
} else if (pid < 0) {
syslog(LOG_ERR, "Error forking first fork: %s", strerror(errno));
exit(1);
} else {
/* setsid() is necessary if we really want to demonize */
setsid();
/* Second fork to really deamonize me. */
if ((pid = fork()) > 0) {
exit(0);
} else if (pid < 0) {
syslog(LOG_ERR, "Error forking second fork: %s",
strerror(errno));
exit(1);
}
}
/* Close all my standard I/O. */
if (chdir("/") < 0) {
syslog(LOG_ERR, "unable to chdir to '/': %s", strerror(errno));
exit(1);
}
close(0);
close(1);
close(2);
}
/* write pid file */
make_pidfile(pid_file);
/* Ignore SIGPIPEs so they don't kill us. */
signal(SIGPIPE, SIG_IGN);
set_signal_handler(SIGHUP, reread_config);
set_signal_handler(SIGINT, shutdown_cleanly);
sel_select_loop(ser2net_sel);
return 0;
}