-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpad_svr3.c
462 lines (389 loc) · 9.12 KB
/
pad_svr3.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* 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.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <pty.h>
#include <sys/socket.h>
#include <linux/x25.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/errno.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <linux/sockios.h>
#include <pthread.h>
#include <sys/wait.h>
#include "all.h"
static void set_defaults(void);
static void init_env();
static int do_login(char *name);
static void get_myname(char *nm);
static void go_daemon();
static int do_inter (void);
char *envinit[3];
extern char **environ;
int Listen_sock = -1;
int Accept_sock = -1;
int master = -1;
long Watchdog_timeout = 10;
long Net_timeout = 3;
int Log_level = 3;
char *Chaddr;
char *Name_log_file;
char *Name_cfg_file;
int Winsize_in = 2;//X.25
int Pacsize_in = 7;//X.25
int Throughput = 0xCC;//X.25
int Forwardtimer = 1;//X.3 parametr 4
int DatalengthMin = 128;//Real packet size X.25
int Buffer_size = 32000;
//for pts-addr table
int Flag_do_pts_addr = 1;
char Name_fifo[MAXFILENAME];
int Flag_Iamchild = 0;
int Flag_pts_addr_send_status = 0;
//end for pts-addr table
#define DEF_ADDR "35071"
#define DEF_NAME_LOG "/var/log/pad_svr.log"
#define DEF_NAME_CFG "/etc/pad_svr/pad_svr.cfg"
extern int Flag_Hup;
extern int Flag_Chld;
extern int Flag_Alrm;
extern int Flag_Exit;
extern int Flag_Idle;
char *Myname;
int Flag_Iamboss = 0;
int Flag_Logopnd = 0;
int Done = 0;
struct x29_par X29_profile[MAXX29PROFILE];
int X29_profile_len = 0;
//This profile sended by EICON card
const struct x29_par X29_profile_default[] = {
0x01,0x00,
0x02,0x00,
0x03,0x00,
0x04,0x01,//idle timer delay.
0x0d,0x04,
0x0f,0x00};
const int X29_profile_default_len = 6;
pid_t Pid_login;
void Exit(int kod) {
if (Flag_Iamboss == 1) {
clear_pid();
if (Flag_do_pts_addr == 1 && *Name_fifo != 0) {
unlink(Name_fifo);
}
}
else if (Flag_Iamchild == 1) {
if (Flag_pts_addr_send_status == 1) {
send_pts_addr(getpid(),"-","-");
}
}
exit(kod);
}
static void
set_defaults(void) {
Chaddr = malloc(20);
Name_log_file = malloc(MAXFILENAME);
Name_cfg_file = malloc(MAXFILENAME);
if (Chaddr == NULL || Name_cfg_file == NULL || Name_log_file == NULL) {
Perror("Unable alloc memory:");
Exit(1);
}
strcpy(Chaddr,DEF_ADDR);
strcpy(Name_log_file,DEF_NAME_LOG);
strcpy(Name_cfg_file,DEF_NAME_CFG);
DatalengthMin = paclen(Pacsize_in);
// DatalengthMax = paclen(Pacsize_in);
*Name_fifo = 0;
}
static void
init_env() {
// char **envp;
//
// envp = envinit;
// if (*envp = getenv("TZ"))
// *envp++ -= 3;
//
// *envp = 0;
environ = envinit;
// putenv("TERM=network");
}
static int
do_login(char *name) {
//struct termios ot;
int slave;
if ((slave = get_slave_pty(name)) < 0) {
Perror("Cant open slave side pty:");
Exit(1);
}
free(name);
if (setsid() < 0) {
Perror("Cant set new session:");
Exit(1);
}
if (ioctl(slave, TIOCSCTTY, NULL)) {
Perror("Cant set control tty:");
Exit(1);
}
// tcgetattr(slave, &ot);
// // ot.c_lflag &= ~(ISIG | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT);;
// // ot.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT);
// ot.c_iflag |= IGNBRK;
// ot.c_oflag |= OPOST;
// ot.c_oflag |= ONLCR;
// ot.c_oflag |= OCRNL;
//
// ot.c_cc[VMIN] = 128;
// ot.c_cc[VTIME] = 20;
//
// tcsetattr(slave, TCSANOW, &ot);
/* ÓÄÅÌÁÔØ ÐÏÄÞÉÎÅÎÎÙÊ pty ÓÔÁÎÄÁÒÔÎÙÍ ÕÓÔÒÏÊÓÔ×ÏÍ ××ÏÄÁ, ×Ù×ÏÄÁ É ÏÛÉÂÏË */
// tcflush(STDIN_FILENO,TCIOFLUSH);
// tcflush(STDOUT_FILENO,TCIOFLUSH);
// tcflush(STDERR_FILENO,TCIOFLUSH);
// close(STDIN_FILENO);
// close(STDOUT_FILENO);
// close(STDERR_FILENO);
sig_block();
dup2(slave, STDIN_FILENO);
dup2(slave, STDOUT_FILENO);
dup2(slave, STDERR_FILENO);
/* × ÜÔÏÊ ÔÏÞËÅ ÐÏÄÞÉÎÅÎÎÙÊ pty ÄÏÌÖÅÎ ÂÙÔØ ÓÔÁÎÄÁÒÔÎÙÍ ÕÓÔÒÏÊÓÔ×ÏÍ ××ÏÄÁ */
if (slave > 2) {
close(slave);
}
sig_unblock();
issue_out();
/* ÚÁÐÕÓÔÉÔØ ÏÂÏÌÏÞËÕ */
//execl("/bin/bash", "/bin/bash",0);
execl("/bin/login", "/bin/login","--",0);
/* ÓÀÄÁ ÕÐÒÁ×ÌÅÎÉÅ ÎÉËÏÇÄÁ ÎÅ ÐÏÐÁÄÅÔ */
Exit(1);
}
static void
get_myname(char *nm) {
char *s;
if ( (s = strrchr(nm,'/')) != NULL) {
nm = s + 1;
}
else {
}
Myname = malloc(strlen(nm));
if (Myname == NULL) {
Perror("Unable alloc memory:");
Exit(1);
}
strcpy(Myname,nm);
}
static void
go_daemon() {
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
Exit(1);
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0) {
Exit(0);
}
umask(0);
/* Open any logs here */
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
Perror("Create new session and group:");
Exit(1);
}
/* Change the current working directory */
if ((chdir("/")) < 0) {
Perror("Change current dir:");
Exit(1);
}
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
int nullfd = 99;
nullfd = open("/dev/null", O_RDWR);
if (nullfd < 0) {
Perror("Cant open /dev/null (but it very needed) :");
Exit(1);
}
dup2(nullfd, STDIN_FILENO);
dup2(nullfd, STDOUT_FILENO);
dup2(nullfd, STDERR_FILENO);
//printf("go daemon %i.",nullfd);
}
static int
do_inter (void) {
if (Flag_Chld == 1) {
pid_t pid;
int stat;
while ( (pid = waitpid(-1, &stat, WNOHANG)) > 0) {
if (WIFEXITED(stat) != 0) {
Printf(5,"Child %d finished. Exit status %i\n", pid,WEXITSTATUS(stat) );
}
else {
Printf(5,"Child %d terminated\n", pid);
}
}
Flag_Chld = 0;
}
if (Flag_Hup == 1) {
readcfg();
closeFdlog();
openFdlog();
echocfg();
Flag_Hup = 0;
}
if (Flag_Alrm == 1) {
//do nothing
Flag_Alrm = 0;
}
if (Flag_Exit == 1) {
Printf(1,"Received exit signal.Exiting...\n");
Done = 1;
Flag_Exit = 0;
}
}
void
close_sock(int fd){
// while(close(fd) == -1 && errno == EINTR) {
int kod;
// Printf(0,"Close sock %i\n",fd);
while((kod = close(fd)) == -1) {
if (errno != EINTR) {
break;
}
}
//Printf(0,"Close sock kod %i\n",kod);
}
void
wait_signal(void) {//for experement only
sigset_t smask;
sigemptyset(&smask);
sigsuspend(&smask);
}
void
do_main_work(void) {
struct sockaddr_x25 clientAddr;
pid_t pidsrv;
char *name;
//Printf(0,"Listen_sock %i\n",Listen_sock);
//wait_signal();
boss_setsignal();
open_listen();
Done = 0;
while(!Done) {
Printf(5,"Accepting for calls\n");
bzero((char *)&clientAddr, sizeof(clientAddr));
socklen_t client_addr_len = sizeof clientAddr;
Accept_sock = -1;
Accept_sock = accept(Listen_sock, (struct sockaddr *)&clientAddr, &client_addr_len);
//Printf(0,"Accept_sock %i\n",Accept_sock);
//wait_signal();
if(Accept_sock == -1) {
if (errno == EINTR) {
//signal arrived
do_inter();
continue;
}
else {
Perror("accept call:");
Exit(1);
}
}
//send_call_accept(Accept_sock);
if ( (pidsrv = fork()) < 0) {
Perror("fork ");
Exit(1);
}
//wait_signal();
if (pidsrv == 0) {
//we are is child svr
sig_block();
Flag_Iamchild = 1;
close_sock(Listen_sock);
//echocfg();
if ((master = get_master_pty(&name)) < 0) {
Perror("Cant open master side pty:");
Exit(1);
}
open_fifo_wr();
Flag_pts_addr_send_status = 1;
send_pts_addr(getpid(),name,clientAddr.sx25_addr.x25_addr);
Printf(2,"Accepted call from address: %s. Pts: %s\n", clientAddr.sx25_addr.x25_addr,name);
echo_x25(Accept_sock);
add_utmp(name,clientAddr.sx25_addr.x25_addr);
sig_unblock();
x29set_default(Accept_sock);
if ((Pid_login = fork()) < 0) {
Perror("Cant fork:");
Exit(1);
}
if (Pid_login == 0) {
//We are in child/login
Flag_Iamchild = 0;
init_env();
close(master);
do_login(name);
}
chil_setsignal();
do_copy(master,Accept_sock);
Printf(4,"Finished.\n");
Exit(0);
}//chld srv end
//------------------
close_sock(Accept_sock);
}//while(!Done) end
close_sock(Listen_sock);
}
int
main(int argc,char *argv[]) {
Flag_Iamboss = 0;
Flag_Logopnd = 0;
get_myname(argv[0]);
set_defaults();
gopt(argc,argv);
if (exist_pid() != 0) {
printf("Process %s already running.\n",Myname);
Exit(1);
}
printf("Name of cfg file: %s\n",Name_cfg_file);
if (!readcfg()) {
print_usage();
Exit(1);
}
openFdlog();
echocfg();
Printf(1,"Server %s starting...\n",Myname);
go_daemon();
Flag_Iamboss = 1;
leave_pid();
setup_pts_addr();
moni_setsignal();
do_monitor();
Exit(0);
}
//