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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
/*
* User Process PPP
*
* Written by Toshiharu OHNO (tony-o@iij.ad.jp)
*
* Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the Internet Initiative Japan, Inc. The name of the
* IIJ may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id: main.c,v 1.124 1998/05/23 22:24:43 brian Exp $
*
* TODO:
*/
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <sys/un.h>
#include <net/if_tun.h>
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>
#include "mbuf.h"
#include "log.h"
#include "defs.h"
#include "id.h"
#include "timer.h"
#include "fsm.h"
#include "lqr.h"
#include "hdlc.h"
#include "lcp.h"
#include "ccp.h"
#include "iplist.h"
#include "throughput.h"
#include "slcompress.h"
#include "ipcp.h"
#include "filter.h"
#include "descriptor.h"
#include "link.h"
#include "mp.h"
#include "bundle.h"
#include "loadalias.h"
#include "auth.h"
#include "systems.h"
#include "ip.h"
#include "sig.h"
#include "main.h"
#include "tun.h"
#include "server.h"
#include "prompt.h"
#include "chat.h"
#include "chap.h"
#include "datalink.h"
#ifndef O_NONBLOCK
#ifdef O_NDELAY
#define O_NONBLOCK O_NDELAY
#endif
#endif
static char pid_filename[MAXPATHLEN];
static void DoLoop(struct bundle *, struct prompt *);
static void TerminalStop(int);
static const char *ex_desc(int);
static struct bundle *SignalBundle;
static struct prompt *SignalPrompt;
void
Cleanup(int excode)
{
SignalBundle->CleaningUp = 1;
if (bundle_Phase(SignalBundle) != PHASE_DEAD)
bundle_Close(SignalBundle, NULL, 1);
}
void
AbortProgram(int excode)
{
server_Close(SignalBundle);
ID0unlink(pid_filename);
log_Printf(LogPHASE, "PPP Terminated (%s).\n", ex_desc(excode));
bundle_Close(SignalBundle, NULL, 1);
bundle_Destroy(SignalBundle);
log_Close();
exit(excode);
}
static void
CloseConnection(int signo)
{
/* NOTE, these are manual, we've done a setsid() */
sig_signal(SIGINT, SIG_IGN);
log_Printf(LogPHASE, "Caught signal %d, abort connection(s)\n", signo);
bundle_Down(SignalBundle);
sig_signal(SIGINT, CloseConnection);
}
static void
CloseSession(int signo)
{
log_Printf(LogPHASE, "Signal %d, terminate.\n", signo);
Cleanup(EX_TERM);
}
static pid_t BGPid = 0;
static void
KillChild(int signo)
{
log_Printf(LogPHASE, "Parent: Signal %d\n", signo);
kill(BGPid, SIGINT);
}
static void
TerminalCont(int signo)
{
signal(SIGCONT, SIG_DFL);
prompt_Continue(SignalPrompt);
}
static void
TerminalStop(int signo)
{
prompt_Suspend(SignalPrompt);
signal(SIGCONT, TerminalCont);
raise(SIGSTOP);
}
static void
BringDownServer(int signo)
{
/* Drops all child prompts too ! */
server_Close(SignalBundle);
}
static const char *
ex_desc(int ex)
{
static char num[12];
static const char *desc[] = {
"normal", "start", "sock", "modem", "dial", "dead", "done",
"reboot", "errdead", "hangup", "term", "nodial", "nologin"
};
if (ex >= 0 && ex < sizeof desc / sizeof *desc)
return desc[ex];
snprintf(num, sizeof num, "%d", ex);
return num;
}
static void
Usage(void)
{
fprintf(stderr,
"Usage: ppp [-auto | -background | -direct | -dedicated | -ddial ]"
#ifndef NOALIAS
" [ -alias ]"
#endif
" [system]\n");
exit(EX_START);
}
static char *
ProcessArgs(int argc, char **argv, int *mode)
{
int optc, labelrequired;
char *cp;
optc = labelrequired = 0;
*mode = PHYS_MANUAL;
while (argc > 0 && **argv == '-') {
cp = *argv + 1;
if (strcmp(cp, "auto") == 0) {
*mode = PHYS_DEMAND;
labelrequired = 1;
} else if (strcmp(cp, "background") == 0) {
*mode = PHYS_1OFF;
labelrequired = 1;
} else if (strcmp(cp, "direct") == 0)
*mode = PHYS_DIRECT;
else if (strcmp(cp, "dedicated") == 0)
*mode = PHYS_DEDICATED;
else if (strcmp(cp, "ddial") == 0) {
*mode = PHYS_PERM;
labelrequired = 1;
} else if (strcmp(cp, "alias") == 0) {
#ifndef NOALIAS
if (alias_Load() != 0)
#endif
log_Printf(LogWARN, "Cannot load alias library\n");
optc--; /* this option isn't exclusive */
} else
Usage();
optc++;
argv++;
argc--;
}
if (argc > 1) {
fprintf(stderr, "You may specify only one system label.\n");
exit(EX_START);
}
if (optc > 1) {
fprintf(stderr, "You may specify only one mode.\n");
exit(EX_START);
}
if (labelrequired && argc != 1) {
fprintf(stderr, "Destination system must be specified in"
" auto, background or ddial mode.\n");
exit(EX_START);
}
return argc == 1 ? *argv : NULL; /* Don't SetLabel yet ! */
}
int
main(int argc, char **argv)
{
FILE *lockfile;
char *name, *label;
int nfds, mode;
struct bundle *bundle;
struct prompt *prompt;
nfds = getdtablesize();
if (nfds >= FD_SETSIZE)
/*
* If we've got loads of file descriptors, make sure they're all
* closed. If they aren't, we may end up with a seg fault when our
* `fd_set's get too big when select()ing !
*/
while (--nfds > 2)
close(nfds);
name = strrchr(argv[0], '/');
log_Open(name ? name + 1 : argv[0]);
argc--;
argv++;
label = ProcessArgs(argc, argv, &mode);
#ifdef __FreeBSD__
/*
* A FreeBSD hack to dodge a bug in the tty driver that drops output
* occasionally.... I must find the real reason some time. To display
* the dodgy behaviour, comment out this bit, make yourself a large
* routing table and then run ppp in interactive mode. The `show route'
* command will drop chunks of data !!!
*/
if (mode == PHYS_MANUAL) {
close(STDIN_FILENO);
if (open(_PATH_TTY, O_RDONLY) != STDIN_FILENO) {
fprintf(stderr, "Cannot open %s for input !\n", _PATH_TTY);
return 2;
}
}
#endif
/* Allow output for the moment (except in direct mode) */
if (mode == PHYS_DIRECT)
prompt = NULL;
else {
const char *m;
SignalPrompt = prompt = prompt_Create(NULL, NULL, PROMPT_STD);
if (mode == PHYS_PERM)
m = "direct dial";
else if (mode & PHYS_1OFF)
m = "background";
else if (mode & PHYS_DEMAND)
m = "auto";
else if (mode & PHYS_DEDICATED)
m = "dedicated";
else if (mode & PHYS_MANUAL)
m = "interactive";
else
m = NULL;
if (m)
prompt_Printf(prompt, "Working in %s mode\n", m);
}
ID0init();
if (ID0realuid() != 0) {
char conf[200], *ptr;
snprintf(conf, sizeof conf, "%s/%s", _PATH_PPP, CONFFILE);
do {
if (!access(conf, W_OK)) {
log_Printf(LogALERT, "ppp: Access violation: Please protect %s\n", conf);
return -1;
}
ptr = conf + strlen(conf)-2;
while (ptr > conf && *ptr != '/')
*ptr-- = '\0';
} while (ptr >= conf);
}
if (!system_IsValid(label, prompt, mode)) {
fprintf(stderr, "You may not use ppp in this mode with this label\n");
if (mode == PHYS_DIRECT) {
const char *l;
l = label ? label : "default";
log_Printf(LogWARN, "Label %s rejected -direct connection\n", l);
}
log_Close();
return 1;
}
if ((bundle = bundle_Create(TUN_PREFIX, mode)) == NULL) {
log_Printf(LogWARN, "bundle_Create: %s\n", strerror(errno));
return EX_START;
}
if (prompt) {
prompt->bundle = bundle; /* couldn't do it earlier */
prompt_Printf(prompt, "Using interface: %s\n", bundle->ifp.Name);
}
SignalBundle = bundle;
if (system_Select(bundle, "default", CONFFILE, prompt) < 0)
prompt_Printf(prompt, "Warning: No default entry found in config file.\n");
sig_signal(SIGHUP, CloseSession);
sig_signal(SIGTERM, CloseSession);
sig_signal(SIGINT, CloseConnection);
sig_signal(SIGQUIT, CloseSession);
sig_signal(SIGALRM, SIG_IGN);
signal(SIGPIPE, SIG_IGN);
if (mode == PHYS_MANUAL)
sig_signal(SIGTSTP, TerminalStop);
sig_signal(SIGUSR2, BringDownServer);
if (label) {
/*
* Set label both before and after system_Select !
* This way, "set enddisc label" works during system_Select, and we
* also end up with the correct label if we have embedded load
* commands.
*/
bundle_SetLabel(bundle, label);
if (system_Select(bundle, label, CONFFILE, prompt) < 0) {
prompt_Printf(prompt, "Destination system (%s) not found.\n", label);
AbortProgram(EX_START);
}
bundle_SetLabel(bundle, label);
if (mode == PHYS_DEMAND &&
bundle->ncp.ipcp.cfg.peer_range.ipaddr.s_addr == INADDR_ANY) {
prompt_Printf(prompt, "You must \"set ifaddr\" with a peer address "
"in label %s for auto mode.\n", label);
AbortProgram(EX_START);
}
}
if (mode != PHYS_MANUAL) {
if (mode != PHYS_DIRECT) {
int bgpipe[2];
pid_t bgpid;
if (mode == PHYS_1OFF && pipe(bgpipe)) {
log_Printf(LogERROR, "pipe: %s\n", strerror(errno));
AbortProgram(EX_SOCK);
}
bgpid = fork();
if (bgpid == -1) {
log_Printf(LogERROR, "fork: %s\n", strerror(errno));
AbortProgram(EX_SOCK);
}
if (bgpid) {
char c = EX_NORMAL;
if (mode == PHYS_1OFF) {
close(bgpipe[1]);
BGPid = bgpid;
/* If we get a signal, kill the child */
signal(SIGHUP, KillChild);
signal(SIGTERM, KillChild);
signal(SIGINT, KillChild);
signal(SIGQUIT, KillChild);
/* Wait for our child to close its pipe before we exit */
if (read(bgpipe[0], &c, 1) != 1) {
prompt_Printf(prompt, "Child exit, no status.\n");
log_Printf(LogPHASE, "Parent: Child exit, no status.\n");
} else if (c == EX_NORMAL) {
prompt_Printf(prompt, "PPP enabled.\n");
log_Printf(LogPHASE, "Parent: PPP enabled.\n");
} else {
prompt_Printf(prompt, "Child failed (%s).\n", ex_desc((int) c));
log_Printf(LogPHASE, "Parent: Child failed (%s).\n",
ex_desc((int) c));
}
close(bgpipe[0]);
}
return c;
} else if (mode == PHYS_1OFF) {
close(bgpipe[0]);
bundle->notify.fd = bgpipe[1];
}
/* -auto, -dedicated, -ddial & -background */
prompt_Destroy(prompt, 0);
close(STDOUT_FILENO);
close(STDERR_FILENO);
close(STDIN_FILENO);
setsid();
} else {
/* -direct: STDIN_FILENO gets used by modem_Open */
prompt_TtyInit(NULL);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
} else {
/* Interactive mode */
close(STDERR_FILENO);
prompt_TtyInit(prompt);
prompt_TtyCommandMode(prompt);
prompt_Required(prompt);
}
snprintf(pid_filename, sizeof pid_filename, "%stun%d.pid",
_PATH_VARRUN, bundle->unit);
lockfile = ID0fopen(pid_filename, "w");
if (lockfile != NULL) {
fprintf(lockfile, "%d\n", (int) getpid());
fclose(lockfile);
}
#ifndef RELEASE_CRUNCH
else
log_Printf(LogALERT, "Warning: Can't create %s: %s\n",
pid_filename, strerror(errno));
#endif
log_Printf(LogPHASE, "PPP Started (%s mode).\n", mode2Nam(mode));
DoLoop(bundle, prompt);
AbortProgram(EX_NORMAL);
return EX_NORMAL;
}
static void
DoLoop(struct bundle *bundle, struct prompt *prompt)
{
fd_set rfds, wfds, efds;
int i, nfds;
do {
nfds = 0;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_ZERO(&efds);
/* All our datalinks, the tun device and the MP socket */
descriptor_UpdateSet(&bundle->desc, &rfds, &wfds, &efds, &nfds);
/* All our prompts and the diagnostic socket */
descriptor_UpdateSet(&server.desc, &rfds, NULL, NULL, &nfds);
if (bundle_IsDead(bundle))
/* Don't select - we'll be here forever */
break;
i = select(nfds, &rfds, &wfds, &efds, NULL);
if (i < 0 && errno != EINTR) {
log_Printf(LogERROR, "DoLoop: select(): %s\n", strerror(errno));
if (log_IsKept(LogTIMER)) {
struct timeval t;
for (i = 0; i <= nfds; i++) {
if (FD_ISSET(i, &rfds)) {
log_Printf(LogTIMER, "Read set contains %d\n", i);
FD_CLR(i, &rfds);
t.tv_sec = t.tv_usec = 0;
if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
log_Printf(LogTIMER, "The culprit !\n");
break;
}
}
if (FD_ISSET(i, &wfds)) {
log_Printf(LogTIMER, "Write set contains %d\n", i);
FD_CLR(i, &wfds);
t.tv_sec = t.tv_usec = 0;
if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
log_Printf(LogTIMER, "The culprit !\n");
break;
}
}
if (FD_ISSET(i, &efds)) {
log_Printf(LogTIMER, "Error set contains %d\n", i);
FD_CLR(i, &efds);
t.tv_sec = t.tv_usec = 0;
if (select(nfds, &rfds, &wfds, &efds, &t) != -1) {
log_Printf(LogTIMER, "The culprit !\n");
break;
}
}
}
}
break;
}
sig_Handle();
if (i <= 0)
continue;
for (i = 0; i <= nfds; i++)
if (FD_ISSET(i, &efds)) {
log_Printf(LogALERT, "Exception detected on descriptor %d\n", i);
break;
}
if (i <= nfds)
break;
if (descriptor_IsSet(&server.desc, &rfds))
descriptor_Read(&server.desc, bundle, &rfds);
if (descriptor_IsSet(&bundle->desc, &wfds))
descriptor_Write(&bundle->desc, bundle, &wfds);
if (descriptor_IsSet(&bundle->desc, &rfds))
descriptor_Read(&bundle->desc, bundle, &rfds);
} while (bundle_CleanDatalinks(bundle), !bundle_IsDead(bundle));
log_Printf(LogDEBUG, "DoLoop done.\n");
}
|