1 /* NetBSD: main.c,v 1.2 2013/11/28 22:33:42 christos Exp */
2
3 /*
4 * main.c - Point-to-Point Protocol main module
5 *
6 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. The name "Carnegie Mellon University" must not be used to
21 * endorse or promote products derived from this software without
22 * prior written permission. For permission or any legal
23 * details, please contact
24 * Office of Technology Transfer
25 * Carnegie Mellon University
26 * 5000 Forbes Avenue
27 * Pittsburgh, PA 15213-3890
28 * (412) 268-4387, fax: (412) 268-7395
29 * tech-transfer@andrew.cmu.edu
30 *
31 * 4. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by Computing Services
34 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
35 *
36 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
37 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
38 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
39 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
40 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
41 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
42 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
43 *
44 * Copyright (c) 1999-2020 Paul Mackerras. All rights reserved.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 *
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 *
53 * 2. The name(s) of the authors of this software must not be used to
54 * endorse or promote products derived from this software without
55 * prior written permission.
56 *
57 * 3. Redistributions of any form whatsoever must retain the following
58 * acknowledgment:
59 * "This product includes software developed by Paul Mackerras
60 * <paulus@samba.org>".
61 *
62 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
63 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
64 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
65 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
66 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
67 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
68 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
69 */
70
71 #include <sys/cdefs.h>
72 __RCSID("NetBSD: main.c,v 1.2 2013/11/28 22:33:42 christos Exp ");
73
74 #include <stdio.h>
75 #include <ctype.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <unistd.h>
79 #include <signal.h>
80 #include <errno.h>
81 #include <fcntl.h>
82 #include <syslog.h>
83 #include <netdb.h>
84 #include <utmp.h>
85 #include <pwd.h>
86 #include <sys/param.h>
87 #include <sys/types.h>
88 #include <sys/wait.h>
89 #include <sys/time.h>
90 #include <sys/resource.h>
91 #include <sys/stat.h>
92 #include <sys/socket.h>
93 #include <netinet/in.h>
94 #include <arpa/inet.h>
95
96 #include "pppd.h"
97 #include "magic.h"
98 #include "fsm.h"
99 #include "lcp.h"
100 #include "ipcp.h"
101 #ifdef INET6
102 #include "ipv6cp.h"
103 #endif
104 #include "upap.h"
105 #include "chap-new.h"
106 #include "eap.h"
107 #include "ccp.h"
108 #include "ecp.h"
109 #include "pathnames.h"
110
111 #ifdef USE_TDB
112 #include "tdb.h"
113 #endif
114
115 #ifdef CBCP_SUPPORT
116 #include "cbcp.h"
117 #endif
118
119 #ifdef IPX_CHANGE
120 #include "ipxcp.h"
121 #endif /* IPX_CHANGE */
122 #ifdef AT_CHANGE
123 #include "atcp.h"
124 #endif
125
126
127 /* interface vars */
128 char ifname[MAXIFNAMELEN]; /* Interface name */
129 int ifunit; /* Interface unit number */
130
131 struct channel *the_channel;
132
133 char *progname; /* Name of this program */
134 char hostname[MAXNAMELEN]; /* Our hostname */
135 static char pidfilename[MAXPATHLEN]; /* name of pid file */
136 static char linkpidfile[MAXPATHLEN]; /* name of linkname pid file */
137 char ppp_devnam[MAXPATHLEN]; /* name of PPP tty (maybe ttypx) */
138 uid_t uid; /* Our real user-id */
139 struct notifier *pidchange = NULL;
140 struct notifier *phasechange = NULL;
141 struct notifier *exitnotify = NULL;
142 struct notifier *sigreceived = NULL;
143 struct notifier *fork_notifier = NULL;
144
145 int hungup; /* terminal has been hung up */
146 int privileged; /* we're running as real uid root */
147 int need_holdoff; /* need holdoff period before restarting */
148 int detached; /* have detached from terminal */
149 volatile int status; /* exit status for pppd */
150 int unsuccess; /* # unsuccessful connection attempts */
151 int do_callback; /* != 0 if we should do callback next */
152 int doing_callback; /* != 0 if we are doing callback */
153 int ppp_session_number; /* Session number, for channels with such a
154 concept (eg PPPoE) */
155 int childwait_done; /* have timed out waiting for children */
156
157 #ifdef USE_TDB
158 TDB_CONTEXT *pppdb; /* database for storing status etc. */
159 #endif
160
161 char db_key[32];
162
163 int (*holdoff_hook)(void) = NULL;
164 int (*new_phase_hook)(int) = NULL;
165 void (*snoop_recv_hook)(unsigned char *p, int len) = NULL;
166 void (*snoop_send_hook)(unsigned char *p, int len) = NULL;
167
168 static int conn_running; /* we have a [dis]connector running */
169 static int fd_loop; /* fd for getting demand-dial packets */
170
171 int fd_devnull; /* fd for /dev/null */
172 int devfd = -1; /* fd of underlying device */
173 int fd_ppp = -1; /* fd for talking PPP */
174 int phase; /* where the link is at */
175 int kill_link;
176 int asked_to_quit;
177 int open_ccp_flag;
178 int listen_time;
179 int got_sigusr2;
180 int got_sigterm;
181 int got_sighup;
182
183 static sigset_t signals_handled;
184 static int waiting;
185 static int sigpipe[2];
186
187 char **script_env; /* Env. variable values for scripts */
188 int s_env_nalloc; /* # words avail at script_env */
189
190 u_char outpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for outgoing packet */
191 u_char inpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for incoming packet */
192
193 static int n_children; /* # child processes still running */
194 static int got_sigchld; /* set if we have received a SIGCHLD */
195
196 int privopen; /* don't lock, open device as root */
197
198 char *no_ppp_msg = "Sorry - this system lacks PPP kernel support\n";
199
200 GIDSET_TYPE groups[NGROUPS_MAX];/* groups the user is in */
201 int ngroups; /* How many groups valid in groups */
202
203 static struct timeval start_time; /* Time when link was started. */
204
205 static struct pppd_stats old_link_stats;
206 struct pppd_stats link_stats;
207 unsigned link_connect_time;
208 int link_stats_valid;
209
210 int error_count;
211
212 bool bundle_eof;
213 bool bundle_terminating;
214
215 /*
216 * We maintain a list of child process pids and
217 * functions to call when they exit.
218 */
219 struct subprocess {
220 pid_t pid;
221 char *prog;
222 void (*done)(void *);
223 void *arg;
224 int killable;
225 struct subprocess *next;
226 };
227
228 static struct subprocess *children;
229
230 /* Prototypes for procedures local to this file. */
231
232 static void setup_signals(void);
233 static void create_pidfile(int pid);
234 static void create_linkpidfile(int pid);
235 static void cleanup(void);
236 static void get_input(void);
237 static void calltimeout(void);
238 static struct timeval *timeleft(struct timeval *);
239 static void kill_my_pg(int);
240 static void hup(int);
241 static void term(int);
242 static void chld(int);
243 static void toggle_debug(int);
244 static void open_ccp(int);
245 static void bad_signal(int);
246 static void holdoff_end(void *);
247 static void forget_child(int pid, int status);
248 static int reap_kids(void);
249 static void childwait_end(void *);
250
251 #ifdef USE_TDB
252 static void update_db_entry(void);
253 static void add_db_key(const char *);
254 static void delete_db_key(const char *);
255 static void cleanup_db(void);
256 #endif
257
258 static void handle_events(void);
259 void print_link_stats(void);
260
261 extern char *getlogin(void);
262 int main(int, char *[]);
263
264 /*
265 * PPP Data Link Layer "protocol" table.
266 * One entry per supported protocol.
267 * The last entry must be NULL.
268 */
269 struct protent *protocols[] = {
270 &lcp_protent,
271 &pap_protent,
272 &chap_protent,
273 #ifdef CBCP_SUPPORT
274 &cbcp_protent,
275 #endif
276 &ipcp_protent,
277 #ifdef INET6
278 &ipv6cp_protent,
279 #endif
280 &ccp_protent,
281 &ecp_protent,
282 #ifdef IPX_CHANGE
283 &ipxcp_protent,
284 #endif
285 #ifdef AT_CHANGE
286 &atcp_protent,
287 #endif
288 &eap_protent,
289 NULL
290 };
291
292 int
main(int argc,char * argv[])293 main(int argc, char *argv[])
294 {
295 int i, t;
296 char *p;
297 struct passwd *pw;
298 struct protent *protp;
299 char numbuf[16];
300
301 strlcpy(path_ipup, _PATH_IPUP, sizeof(path_ipup));
302 strlcpy(path_ipdown, _PATH_IPDOWN, sizeof(path_ipdown));
303
304 link_stats_valid = 0;
305 new_phase(PHASE_INITIALIZE);
306
307 script_env = NULL;
308
309 /* Initialize syslog facilities */
310 reopen_log();
311
312 if (gethostname(hostname, MAXNAMELEN) < 0 ) {
313 option_error("Couldn't get hostname: %m");
314 exit(1);
315 }
316 hostname[MAXNAMELEN-1] = 0;
317
318 /* make sure we don't create world or group writable files. */
319 umask(umask(0777) | 022);
320
321 uid = getuid();
322 privileged = uid == 0;
323 slprintf(numbuf, sizeof(numbuf), "%d", uid);
324 script_setenv("ORIG_UID", numbuf, 0);
325
326 ngroups = getgroups(NGROUPS_MAX, groups);
327
328 /*
329 * Initialize magic number generator now so that protocols may
330 * use magic numbers in initialization.
331 */
332 magic_init();
333
334 /*
335 * Initialize each protocol.
336 */
337 for (i = 0; (protp = protocols[i]) != NULL; ++i)
338 (*protp->init)(0);
339
340 /*
341 * Initialize the default channel.
342 */
343 tty_init();
344
345 progname = *argv;
346
347 /*
348 * Parse, in order, the system options file, the user's options file,
349 * and the command line arguments.
350 */
351 if (!options_from_file(_PATH_SYSOPTIONS, !privileged, 0, 1)
352 || !options_from_user()
353 || !parse_args(argc-1, argv+1))
354 exit(EXIT_OPTION_ERROR);
355 devnam_fixed = 1; /* can no longer change device name */
356
357 /*
358 * Work out the device name, if it hasn't already been specified,
359 * and parse the tty's options file.
360 */
361 if (the_channel->process_extra_options)
362 (*the_channel->process_extra_options)();
363
364 if (debug)
365 setlogmask(LOG_UPTO(LOG_DEBUG));
366
367 /*
368 * Check that we are running as root.
369 */
370 if (geteuid() != 0) {
371 option_error("must be root to run %s, since it is not setuid-root",
372 argv[0]);
373 exit(EXIT_NOT_ROOT);
374 }
375
376 if (!ppp_available()) {
377 option_error("%s", no_ppp_msg);
378 exit(EXIT_NO_KERNEL_SUPPORT);
379 }
380
381 /*
382 * Check that the options given are valid and consistent.
383 */
384 check_options();
385 if (!sys_check_options())
386 exit(EXIT_OPTION_ERROR);
387 auth_check_options();
388 #ifdef HAVE_MULTILINK
389 mp_check_options();
390 #endif
391 for (i = 0; (protp = protocols[i]) != NULL; ++i)
392 if (protp->check_options != NULL)
393 (*protp->check_options)();
394 if (the_channel->check_options)
395 (*the_channel->check_options)();
396
397
398 if (dump_options || dryrun) {
399 init_pr_log(NULL, LOG_INFO);
400 print_options(pr_log, NULL);
401 end_pr_log();
402 }
403
404 if (dryrun)
405 die(0);
406
407 /* Make sure fds 0, 1, 2 are open to somewhere. */
408 fd_devnull = open(_PATH_DEVNULL, O_RDWR);
409 if (fd_devnull < 0)
410 fatal("Couldn't open %s: %m", _PATH_DEVNULL);
411 while (fd_devnull <= 2) {
412 i = dup(fd_devnull);
413 if (i < 0)
414 fatal("Critical shortage of file descriptors: dup failed: %m");
415 fd_devnull = i;
416 }
417
418 /*
419 * Initialize system-dependent stuff.
420 */
421 sys_init();
422
423 #ifdef USE_TDB
424 pppdb = tdb_open(_PATH_PPPDB, 0, 0, O_RDWR|O_CREAT, 0644);
425 if (pppdb != NULL) {
426 slprintf(db_key, sizeof(db_key), "pppd%d", getpid());
427 update_db_entry();
428 } else {
429 warn("Warning: couldn't open ppp database %s", _PATH_PPPDB);
430 if (multilink) {
431 warn("Warning: disabling multilink");
432 multilink = 0;
433 }
434 }
435 #endif
436
437 /*
438 * Detach ourselves from the terminal, if required,
439 * and identify who is running us.
440 */
441 if (!nodetach && !updetach)
442 detach();
443 p = getlogin();
444 if (p == NULL) {
445 pw = getpwuid(uid);
446 if (pw != NULL && pw->pw_name != NULL)
447 p = pw->pw_name;
448 else
449 p = "(unknown)";
450 }
451 syslog(LOG_NOTICE, "pppd %s started by %s, uid %d", VERSION, p, uid);
452 script_setenv("PPPLOGNAME", p, 0);
453
454 if (devnam[0])
455 script_setenv("DEVICE", devnam, 1);
456 slprintf(numbuf, sizeof(numbuf), "%d", getpid());
457 script_setenv("PPPD_PID", numbuf, 1);
458
459 setup_signals();
460
461 create_linkpidfile(getpid());
462
463 waiting = 0;
464
465 /*
466 * If we're doing dial-on-demand, set up the interface now.
467 */
468 if (demand) {
469 /*
470 * Open the loopback channel and set it up to be the ppp interface.
471 */
472 fd_loop = open_ppp_loopback();
473 set_ifunit(1);
474 /*
475 * Configure the interface and mark it up, etc.
476 */
477 demand_conf();
478 }
479
480 do_callback = 0;
481 for (;;) {
482
483 bundle_eof = 0;
484 bundle_terminating = 0;
485 listen_time = 0;
486 need_holdoff = 1;
487 devfd = -1;
488 status = EXIT_OK;
489 ++unsuccess;
490 doing_callback = do_callback;
491 do_callback = 0;
492
493 if (demand && !doing_callback) {
494 /*
495 * Don't do anything until we see some activity.
496 */
497 new_phase(PHASE_DORMANT);
498 demand_unblock();
499 add_fd(fd_loop);
500 for (;;) {
501 handle_events();
502 if (asked_to_quit)
503 break;
504 if (get_loop_output())
505 break;
506 }
507 remove_fd(fd_loop);
508 if (asked_to_quit)
509 break;
510
511 /*
512 * Now we want to bring up the link.
513 */
514 demand_block();
515 info("Starting link");
516 }
517
518 get_time(&start_time);
519 script_unsetenv("CONNECT_TIME");
520 script_unsetenv("BYTES_SENT");
521 script_unsetenv("BYTES_RCVD");
522
523 lcp_open(0); /* Start protocol */
524 start_link(0);
525 while (phase != PHASE_DEAD) {
526 handle_events();
527 get_input();
528 if (kill_link)
529 lcp_close(0, "User request");
530 if (asked_to_quit) {
531 bundle_terminating = 1;
532 if (phase == PHASE_MASTER)
533 mp_bundle_terminated();
534 }
535 if (open_ccp_flag) {
536 if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) {
537 ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */
538 (*ccp_protent.open)(0);
539 }
540 }
541 }
542 /* restore FSMs to original state */
543 lcp_close(0, "");
544
545 if (!persist || asked_to_quit || (maxfail > 0 && unsuccess >= maxfail))
546 break;
547
548 if (demand)
549 demand_discard();
550 t = need_holdoff? holdoff: 0;
551 if (holdoff_hook)
552 t = (*holdoff_hook)();
553 if (t > 0) {
554 new_phase(PHASE_HOLDOFF);
555 TIMEOUT(holdoff_end, NULL, t);
556 do {
557 handle_events();
558 if (kill_link)
559 new_phase(PHASE_DORMANT); /* allow signal to end holdoff */
560 } while (phase == PHASE_HOLDOFF);
561 if (!persist)
562 break;
563 }
564 }
565
566 /* Wait for scripts to finish */
567 reap_kids();
568 if (n_children > 0) {
569 if (child_wait > 0)
570 TIMEOUT(childwait_end, NULL, child_wait);
571 if (debug) {
572 struct subprocess *chp;
573 dbglog("Waiting for %d child processes...", n_children);
574 for (chp = children; chp != NULL; chp = chp->next)
575 dbglog(" script %s, pid %d", chp->prog, chp->pid);
576 }
577 while (n_children > 0 && !childwait_done) {
578 handle_events();
579 if (kill_link && !childwait_done)
580 childwait_end(NULL);
581 }
582 }
583
584 die(status);
585 return 0;
586 }
587
588 /*
589 * handle_events - wait for something to happen and respond to it.
590 */
591 static void
handle_events(void)592 handle_events(void)
593 {
594 struct timeval timo;
595 unsigned char buf[16];
596
597 kill_link = open_ccp_flag = 0;
598
599 /* alert via signal pipe */
600 waiting = 1;
601 /* flush signal pipe */
602 for (; read(sigpipe[0], buf, sizeof(buf)) > 0; );
603 add_fd(sigpipe[0]);
604 /* wait if necessary */
605 if (!(got_sighup || got_sigterm || got_sigusr2 || got_sigchld))
606 wait_input(timeleft(&timo));
607 waiting = 0;
608 remove_fd(sigpipe[0]);
609
610 calltimeout();
611 if (got_sighup) {
612 info("Hangup (SIGHUP)");
613 kill_link = 1;
614 got_sighup = 0;
615 if (status != EXIT_HANGUP)
616 status = EXIT_USER_REQUEST;
617 }
618 if (got_sigterm) {
619 info("Terminating on signal %d", got_sigterm);
620 kill_link = 1;
621 asked_to_quit = 1;
622 persist = 0;
623 status = EXIT_USER_REQUEST;
624 got_sigterm = 0;
625 }
626 if (got_sigchld) {
627 got_sigchld = 0;
628 reap_kids(); /* Don't leave dead kids lying around */
629 }
630 if (got_sigusr2) {
631 open_ccp_flag = 1;
632 got_sigusr2 = 0;
633 }
634 }
635
636 /*
637 * setup_signals - initialize signal handling.
638 */
639 static void
setup_signals(void)640 setup_signals(void)
641 {
642 struct sigaction sa;
643
644 /* create pipe to wake up event handler from signal handler */
645 if (pipe(sigpipe) < 0)
646 fatal("Couldn't create signal pipe: %m");
647 fcntl(sigpipe[0], F_SETFD, fcntl(sigpipe[0], F_GETFD) | FD_CLOEXEC);
648 fcntl(sigpipe[1], F_SETFD, fcntl(sigpipe[1], F_GETFD) | FD_CLOEXEC);
649 fcntl(sigpipe[0], F_SETFL, fcntl(sigpipe[0], F_GETFL) | O_NONBLOCK);
650 fcntl(sigpipe[1], F_SETFL, fcntl(sigpipe[1], F_GETFL) | O_NONBLOCK);
651
652 /*
653 * Compute mask of all interesting signals and install signal handlers
654 * for each. Only one signal handler may be active at a time. Therefore,
655 * all other signals should be masked when any handler is executing.
656 */
657 sigemptyset(&signals_handled);
658 sigaddset(&signals_handled, SIGHUP);
659 sigaddset(&signals_handled, SIGINT);
660 sigaddset(&signals_handled, SIGTERM);
661 sigaddset(&signals_handled, SIGCHLD);
662 sigaddset(&signals_handled, SIGUSR2);
663
664 #define SIGNAL(s, handler) do { \
665 sa.sa_handler = handler; \
666 if (sigaction(s, &sa, NULL) < 0) \
667 fatal("Couldn't establish signal handler (%d): %m", s); \
668 } while (0)
669
670 sa.sa_mask = signals_handled;
671 sa.sa_flags = 0;
672 SIGNAL(SIGHUP, hup); /* Hangup */
673 SIGNAL(SIGINT, term); /* Interrupt */
674 SIGNAL(SIGTERM, term); /* Terminate */
675 SIGNAL(SIGCHLD, chld);
676
677 SIGNAL(SIGUSR1, toggle_debug); /* Toggle debug flag */
678 SIGNAL(SIGUSR2, open_ccp); /* Reopen CCP */
679
680 /*
681 * Install a handler for other signals which would otherwise
682 * cause pppd to exit without cleaning up.
683 */
684 SIGNAL(SIGABRT, bad_signal);
685 SIGNAL(SIGALRM, bad_signal);
686 SIGNAL(SIGFPE, bad_signal);
687 SIGNAL(SIGILL, bad_signal);
688 SIGNAL(SIGPIPE, bad_signal);
689 SIGNAL(SIGQUIT, bad_signal);
690 SIGNAL(SIGSEGV, bad_signal);
691 #ifdef SIGBUS
692 SIGNAL(SIGBUS, bad_signal);
693 #endif
694 #ifdef SIGEMT
695 SIGNAL(SIGEMT, bad_signal);
696 #endif
697 #ifdef SIGPOLL
698 SIGNAL(SIGPOLL, bad_signal);
699 #endif
700 #ifdef SIGPROF
701 SIGNAL(SIGPROF, bad_signal);
702 #endif
703 #ifdef SIGSYS
704 SIGNAL(SIGSYS, bad_signal);
705 #endif
706 #ifdef SIGTRAP
707 SIGNAL(SIGTRAP, bad_signal);
708 #endif
709 #ifdef SIGVTALRM
710 SIGNAL(SIGVTALRM, bad_signal);
711 #endif
712 #ifdef SIGXCPU
713 SIGNAL(SIGXCPU, bad_signal);
714 #endif
715 #ifdef SIGXFSZ
716 SIGNAL(SIGXFSZ, bad_signal);
717 #endif
718
719 /*
720 * Apparently we can get a SIGPIPE when we call syslog, if
721 * syslogd has died and been restarted. Ignoring it seems
722 * be sufficient.
723 */
724 signal(SIGPIPE, SIG_IGN);
725 }
726
727 /*
728 * set_ifunit - do things we need to do once we know which ppp
729 * unit we are using.
730 */
731 void
set_ifunit(int iskey)732 set_ifunit(int iskey)
733 {
734 char ifkey[32];
735
736 if (req_ifname[0] != '\0')
737 slprintf(ifname, sizeof(ifname), "%s", req_ifname);
738 else
739 slprintf(ifname, sizeof(ifname), "%s%d", PPP_DRV_NAME, ifunit);
740 info("Using interface %s", ifname);
741 script_setenv("IFNAME", ifname, iskey);
742 slprintf(ifkey, sizeof(ifkey), "%d", ifunit);
743 script_setenv("UNIT", ifkey, iskey);
744 if (iskey) {
745 create_pidfile(getpid()); /* write pid to file */
746 create_linkpidfile(getpid());
747 }
748 }
749
750 /*
751 * detach - detach us from the controlling terminal.
752 */
753 void
detach(void)754 detach(void)
755 {
756 int pid;
757 char numbuf[16];
758 int pipefd[2];
759
760 if (detached)
761 return;
762 if (pipe(pipefd) == -1)
763 pipefd[0] = pipefd[1] = -1;
764 if ((pid = fork()) < 0) {
765 error("Couldn't detach (fork failed: %m)");
766 die(1); /* or just return? */
767 }
768 if (pid != 0) {
769 /* parent */
770 notify(pidchange, pid);
771 /* update pid files if they have been written already */
772 if (pidfilename[0])
773 create_pidfile(pid);
774 create_linkpidfile(pid);
775 exit(0); /* parent dies */
776 }
777 setsid();
778 chdir("/");
779 dup2(fd_devnull, 0);
780 dup2(fd_devnull, 1);
781 dup2(fd_devnull, 2);
782 detached = 1;
783 if (log_default)
784 log_to_fd = -1;
785 slprintf(numbuf, sizeof(numbuf), "%d", getpid());
786 script_setenv("PPPD_PID", numbuf, 1);
787
788 /* wait for parent to finish updating pid & lock files and die */
789 close(pipefd[1]);
790 complete_read(pipefd[0], numbuf, 1);
791 close(pipefd[0]);
792 }
793
794 /*
795 * reopen_log - (re)open our connection to syslog.
796 */
797 void
reopen_log(void)798 reopen_log(void)
799 {
800 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
801 setlogmask(LOG_UPTO(LOG_INFO));
802 }
803
804 /*
805 * Create a file containing our process ID.
806 */
807 static void
create_pidfile(int pid)808 create_pidfile(int pid)
809 {
810 FILE *pidfile;
811
812 slprintf(pidfilename, sizeof(pidfilename), "%s%s.pid",
813 _PATH_VARRUN, ifname);
814 if ((pidfile = fopen(pidfilename, "w")) != NULL) {
815 fprintf(pidfile, "%d\n", pid);
816 (void) fclose(pidfile);
817 } else {
818 error("Failed to create pid file %s: %m", pidfilename);
819 pidfilename[0] = 0;
820 }
821 }
822
823 void
create_linkpidfile(int pid)824 create_linkpidfile(int pid)
825 {
826 FILE *pidfile;
827
828 if (linkname[0] == 0)
829 return;
830 script_setenv("LINKNAME", linkname, 1);
831 slprintf(linkpidfile, sizeof(linkpidfile), "%sppp-%s.pid",
832 _PATH_VARRUN, linkname);
833 if ((pidfile = fopen(linkpidfile, "w")) != NULL) {
834 fprintf(pidfile, "%d\n", pid);
835 if (ifname[0])
836 fprintf(pidfile, "%s\n", ifname);
837 (void) fclose(pidfile);
838 } else {
839 error("Failed to create pid file %s: %m", linkpidfile);
840 linkpidfile[0] = 0;
841 }
842 }
843
844 /*
845 * remove_pidfile - remove our pid files
846 */
remove_pidfiles(void)847 void remove_pidfiles(void)
848 {
849 if (pidfilename[0] != 0 && unlink(pidfilename) < 0 && errno != ENOENT)
850 warn("unable to delete pid file %s: %m", pidfilename);
851 pidfilename[0] = 0;
852 if (linkpidfile[0] != 0 && unlink(linkpidfile) < 0 && errno != ENOENT)
853 warn("unable to delete pid file %s: %m", linkpidfile);
854 linkpidfile[0] = 0;
855 }
856
857 /*
858 * holdoff_end - called via a timeout when the holdoff period ends.
859 */
860 static void
holdoff_end(void * arg)861 holdoff_end(void *arg)
862 {
863 new_phase(PHASE_DORMANT);
864 }
865
866 /* List of protocol names, to make our messages a little more informative. */
867 struct protocol_list {
868 u_short proto;
869 const char *name;
870 } protocol_list[] = {
871 { 0x21, "IP" },
872 { 0x23, "OSI Network Layer" },
873 { 0x25, "Xerox NS IDP" },
874 { 0x27, "DECnet Phase IV" },
875 { 0x29, "Appletalk" },
876 { 0x2b, "Novell IPX" },
877 { 0x2d, "VJ compressed TCP/IP" },
878 { 0x2f, "VJ uncompressed TCP/IP" },
879 { 0x31, "Bridging PDU" },
880 { 0x33, "Stream Protocol ST-II" },
881 { 0x35, "Banyan Vines" },
882 { 0x39, "AppleTalk EDDP" },
883 { 0x3b, "AppleTalk SmartBuffered" },
884 { 0x3d, "Multi-Link" },
885 { 0x3f, "NETBIOS Framing" },
886 { 0x41, "Cisco Systems" },
887 { 0x43, "Ascom Timeplex" },
888 { 0x45, "Fujitsu Link Backup and Load Balancing (LBLB)" },
889 { 0x47, "DCA Remote Lan" },
890 { 0x49, "Serial Data Transport Protocol (PPP-SDTP)" },
891 { 0x4b, "SNA over 802.2" },
892 { 0x4d, "SNA" },
893 { 0x4f, "IP6 Header Compression" },
894 { 0x51, "KNX Bridging Data" },
895 { 0x53, "Encryption" },
896 { 0x55, "Individual Link Encryption" },
897 { 0x57, "IPv6" },
898 { 0x59, "PPP Muxing" },
899 { 0x5b, "Vendor-Specific Network Protocol" },
900 { 0x61, "RTP IPHC Full Header" },
901 { 0x63, "RTP IPHC Compressed TCP" },
902 { 0x65, "RTP IPHC Compressed non-TCP" },
903 { 0x67, "RTP IPHC Compressed UDP 8" },
904 { 0x69, "RTP IPHC Compressed RTP 8" },
905 { 0x6f, "Stampede Bridging" },
906 { 0x73, "MP+" },
907 { 0xc1, "NTCITS IPI" },
908 { 0xfb, "single-link compression" },
909 { 0xfd, "Compressed Datagram" },
910 { 0x0201, "802.1d Hello Packets" },
911 { 0x0203, "IBM Source Routing BPDU" },
912 { 0x0205, "DEC LANBridge100 Spanning Tree" },
913 { 0x0207, "Cisco Discovery Protocol" },
914 { 0x0209, "Netcs Twin Routing" },
915 { 0x020b, "STP - Scheduled Transfer Protocol" },
916 { 0x020d, "EDP - Extreme Discovery Protocol" },
917 { 0x0211, "Optical Supervisory Channel Protocol" },
918 { 0x0213, "Optical Supervisory Channel Protocol" },
919 { 0x0231, "Luxcom" },
920 { 0x0233, "Sigma Network Systems" },
921 { 0x0235, "Apple Client Server Protocol" },
922 { 0x0281, "MPLS Unicast" },
923 { 0x0283, "MPLS Multicast" },
924 { 0x0285, "IEEE p1284.4 standard - data packets" },
925 { 0x0287, "ETSI TETRA Network Protocol Type 1" },
926 { 0x0289, "Multichannel Flow Treatment Protocol" },
927 { 0x2063, "RTP IPHC Compressed TCP No Delta" },
928 { 0x2065, "RTP IPHC Context State" },
929 { 0x2067, "RTP IPHC Compressed UDP 16" },
930 { 0x2069, "RTP IPHC Compressed RTP 16" },
931 { 0x4001, "Cray Communications Control Protocol" },
932 { 0x4003, "CDPD Mobile Network Registration Protocol" },
933 { 0x4005, "Expand accelerator protocol" },
934 { 0x4007, "ODSICP NCP" },
935 { 0x4009, "DOCSIS DLL" },
936 { 0x400B, "Cetacean Network Detection Protocol" },
937 { 0x4021, "Stacker LZS" },
938 { 0x4023, "RefTek Protocol" },
939 { 0x4025, "Fibre Channel" },
940 { 0x4027, "EMIT Protocols" },
941 { 0x405b, "Vendor-Specific Protocol (VSP)" },
942 { 0x8021, "Internet Protocol Control Protocol" },
943 { 0x8023, "OSI Network Layer Control Protocol" },
944 { 0x8025, "Xerox NS IDP Control Protocol" },
945 { 0x8027, "DECnet Phase IV Control Protocol" },
946 { 0x8029, "Appletalk Control Protocol" },
947 { 0x802b, "Novell IPX Control Protocol" },
948 { 0x8031, "Bridging NCP" },
949 { 0x8033, "Stream Protocol Control Protocol" },
950 { 0x8035, "Banyan Vines Control Protocol" },
951 { 0x803d, "Multi-Link Control Protocol" },
952 { 0x803f, "NETBIOS Framing Control Protocol" },
953 { 0x8041, "Cisco Systems Control Protocol" },
954 { 0x8043, "Ascom Timeplex" },
955 { 0x8045, "Fujitsu LBLB Control Protocol" },
956 { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" },
957 { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" },
958 { 0x804b, "SNA over 802.2 Control Protocol" },
959 { 0x804d, "SNA Control Protocol" },
960 { 0x804f, "IP6 Header Compression Control Protocol" },
961 { 0x8051, "KNX Bridging Control Protocol" },
962 { 0x8053, "Encryption Control Protocol" },
963 { 0x8055, "Individual Link Encryption Control Protocol" },
964 { 0x8057, "IPv6 Control Protocol" },
965 { 0x8059, "PPP Muxing Control Protocol" },
966 { 0x805b, "Vendor-Specific Network Control Protocol (VSNCP)" },
967 { 0x806f, "Stampede Bridging Control Protocol" },
968 { 0x8073, "MP+ Control Protocol" },
969 { 0x80c1, "NTCITS IPI Control Protocol" },
970 { 0x80fb, "Single Link Compression Control Protocol" },
971 { 0x80fd, "Compression Control Protocol" },
972 { 0x8207, "Cisco Discovery Protocol Control" },
973 { 0x8209, "Netcs Twin Routing" },
974 { 0x820b, "STP - Control Protocol" },
975 { 0x820d, "EDPCP - Extreme Discovery Protocol Ctrl Prtcl" },
976 { 0x8235, "Apple Client Server Protocol Control" },
977 { 0x8281, "MPLSCP" },
978 { 0x8285, "IEEE p1284.4 standard - Protocol Control" },
979 { 0x8287, "ETSI TETRA TNP1 Control Protocol" },
980 { 0x8289, "Multichannel Flow Treatment Protocol" },
981 { 0xc021, "Link Control Protocol" },
982 { 0xc023, "Password Authentication Protocol" },
983 { 0xc025, "Link Quality Report" },
984 { 0xc027, "Shiva Password Authentication Protocol" },
985 { 0xc029, "CallBack Control Protocol (CBCP)" },
986 { 0xc02b, "BACP Bandwidth Allocation Control Protocol" },
987 { 0xc02d, "BAP" },
988 { 0xc05b, "Vendor-Specific Authentication Protocol (VSAP)" },
989 { 0xc081, "Container Control Protocol" },
990 { 0xc223, "Challenge Handshake Authentication Protocol" },
991 { 0xc225, "RSA Authentication Protocol" },
992 { 0xc227, "Extensible Authentication Protocol" },
993 { 0xc229, "Mitsubishi Security Info Exch Ptcl (SIEP)" },
994 { 0xc26f, "Stampede Bridging Authorization Protocol" },
995 { 0xc281, "Proprietary Authentication Protocol" },
996 { 0xc283, "Proprietary Authentication Protocol" },
997 { 0xc481, "Proprietary Node ID Authentication Protocol" },
998 { 0, NULL },
999 };
1000
1001 /*
1002 * protocol_name - find a name for a PPP protocol.
1003 */
1004 const char *
protocol_name(int proto)1005 protocol_name(int proto)
1006 {
1007 struct protocol_list *lp;
1008
1009 for (lp = protocol_list; lp->proto != 0; ++lp)
1010 if (proto == lp->proto)
1011 return lp->name;
1012 return NULL;
1013 }
1014
1015 /*
1016 * get_input - called when incoming data is available.
1017 */
1018 static void
get_input(void)1019 get_input(void)
1020 {
1021 int len, i;
1022 u_char *p;
1023 u_short protocol;
1024 struct protent *protp;
1025
1026 p = inpacket_buf; /* point to beginning of packet buffer */
1027
1028 len = read_packet(inpacket_buf);
1029 if (len < 0)
1030 return;
1031
1032 if (len == 0) {
1033 if (bundle_eof && multilink_master) {
1034 notice("Last channel has disconnected");
1035 mp_bundle_terminated();
1036 return;
1037 }
1038 notice("Modem hangup");
1039 hungup = 1;
1040 status = EXIT_HANGUP;
1041 lcp_lowerdown(0); /* serial link is no longer available */
1042 link_terminated(0);
1043 return;
1044 }
1045
1046 if (len < PPP_HDRLEN) {
1047 dbglog("received short packet:%.*B", len, p);
1048 return;
1049 }
1050
1051 dump_packet("rcvd", p, len);
1052 if (snoop_recv_hook) snoop_recv_hook(p, len);
1053
1054 p += 2; /* Skip address and control */
1055 GETSHORT(protocol, p);
1056 len -= PPP_HDRLEN;
1057
1058 /*
1059 * Toss all non-LCP packets unless LCP is OPEN.
1060 */
1061 if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) {
1062 dbglog("Discarded non-LCP packet when LCP not open");
1063 return;
1064 }
1065
1066 /*
1067 * Until we get past the authentication phase, toss all packets
1068 * except LCP, LQR and authentication packets.
1069 */
1070 if (phase <= PHASE_AUTHENTICATE
1071 && !(protocol == PPP_LCP || protocol == PPP_LQR
1072 || protocol == PPP_PAP || protocol == PPP_CHAP ||
1073 protocol == PPP_EAP)) {
1074 dbglog("discarding proto 0x%x in phase %d",
1075 protocol, phase);
1076 return;
1077 }
1078
1079 /*
1080 * Upcall the proper protocol input routine.
1081 */
1082 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
1083 if (protp->protocol == protocol && protp->enabled_flag) {
1084 (*protp->input)(0, p, len);
1085 return;
1086 }
1087 if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag
1088 && protp->datainput != NULL) {
1089 (*protp->datainput)(0, p, len);
1090 return;
1091 }
1092 }
1093
1094 if (debug) {
1095 const char *pname = protocol_name(protocol);
1096 if (pname != NULL)
1097 warn("Unsupported protocol '%s' (0x%x) received", pname, protocol);
1098 else
1099 warn("Unsupported protocol 0x%x received", protocol);
1100 }
1101 lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN);
1102 }
1103
1104 /*
1105 * ppp_send_config - configure the transmit-side characteristics of
1106 * the ppp interface. Returns -1, indicating an error, if the channel
1107 * send_config procedure called error() (or incremented error_count
1108 * itself), otherwise 0.
1109 */
1110 int
ppp_send_config(int unit,int mtu,u_int32_t accm,int pcomp,int accomp)1111 ppp_send_config(int unit, int mtu, u_int32_t accm, int pcomp, int accomp)
1112 {
1113 int errs;
1114
1115 if (the_channel->send_config == NULL)
1116 return 0;
1117 errs = error_count;
1118 (*the_channel->send_config)(mtu, accm, pcomp, accomp);
1119 return (error_count != errs)? -1: 0;
1120 }
1121
1122 /*
1123 * ppp_recv_config - configure the receive-side characteristics of
1124 * the ppp interface. Returns -1, indicating an error, if the channel
1125 * recv_config procedure called error() (or incremented error_count
1126 * itself), otherwise 0.
1127 */
1128 int
ppp_recv_config(int unit,int mru,u_int32_t accm,int pcomp,int accomp)1129 ppp_recv_config(int unit, int mru, u_int32_t accm, int pcomp, int accomp)
1130 {
1131 int errs;
1132
1133 if (the_channel->recv_config == NULL)
1134 return 0;
1135 errs = error_count;
1136 (*the_channel->recv_config)(mru, accm, pcomp, accomp);
1137 return (error_count != errs)? -1: 0;
1138 }
1139
1140 /*
1141 * new_phase - signal the start of a new phase of pppd's operation.
1142 */
1143 void
new_phase(int p)1144 new_phase(int p)
1145 {
1146 phase = p;
1147 if (new_phase_hook)
1148 (*new_phase_hook)(p);
1149 notify(phasechange, p);
1150 }
1151
1152 /*
1153 * die - clean up state and exit with the specified status.
1154 */
1155 void
die(int status)1156 die(int status)
1157 {
1158 if (!doing_multilink || multilink_master)
1159 print_link_stats();
1160 cleanup();
1161 notify(exitnotify, status);
1162 syslog(LOG_INFO, "Exit.");
1163 exit(status);
1164 }
1165
1166 /*
1167 * cleanup - restore anything which needs to be restored before we exit
1168 */
1169 /* ARGSUSED */
1170 static void
cleanup(void)1171 cleanup(void)
1172 {
1173 sys_cleanup();
1174
1175 if (fd_ppp >= 0)
1176 the_channel->disestablish_ppp(devfd);
1177 if (the_channel->cleanup)
1178 (*the_channel->cleanup)();
1179 remove_pidfiles();
1180
1181 #ifdef USE_TDB
1182 if (pppdb != NULL)
1183 cleanup_db();
1184 #endif
1185
1186 }
1187
1188 void
print_link_stats(void)1189 print_link_stats(void)
1190 {
1191 /*
1192 * Print connect time and statistics.
1193 */
1194 if (link_stats_valid) {
1195 int t = (link_connect_time + 5) / 6; /* 1/10ths of minutes */
1196 info("Connect time %d.%d minutes.", t/10, t%10);
1197 info("Sent %u bytes, received %u bytes.",
1198 link_stats.bytes_out, link_stats.bytes_in);
1199 link_stats_valid = 0;
1200 }
1201 }
1202
1203 /*
1204 * reset_link_stats - "reset" stats when link goes up.
1205 */
1206 void
reset_link_stats(int u)1207 reset_link_stats(int u)
1208 {
1209 if (!get_ppp_stats(u, &old_link_stats))
1210 return;
1211 get_time(&start_time);
1212 }
1213
1214 /*
1215 * update_link_stats - get stats at link termination.
1216 */
1217 void
update_link_stats(int u)1218 update_link_stats(int u)
1219 {
1220 struct timeval now;
1221 char numbuf[32];
1222
1223 if (!get_ppp_stats(u, &link_stats)
1224 || get_time(&now) < 0)
1225 return;
1226 link_connect_time = now.tv_sec - start_time.tv_sec;
1227 link_stats_valid = 1;
1228
1229 link_stats.bytes_in -= old_link_stats.bytes_in;
1230 link_stats.bytes_out -= old_link_stats.bytes_out;
1231 link_stats.pkts_in -= old_link_stats.pkts_in;
1232 link_stats.pkts_out -= old_link_stats.pkts_out;
1233
1234 slprintf(numbuf, sizeof(numbuf), "%u", link_connect_time);
1235 script_setenv("CONNECT_TIME", numbuf, 0);
1236 slprintf(numbuf, sizeof(numbuf), "%u", link_stats.bytes_out);
1237 script_setenv("BYTES_SENT", numbuf, 0);
1238 slprintf(numbuf, sizeof(numbuf), "%u", link_stats.bytes_in);
1239 script_setenv("BYTES_RCVD", numbuf, 0);
1240 }
1241
1242
1243 struct callout {
1244 struct timeval c_time; /* time at which to call routine */
1245 void *c_arg; /* argument to routine */
1246 void (*c_func)(void *); /* routine */
1247 struct callout *c_next;
1248 };
1249
1250 static struct callout *callout = NULL; /* Callout list */
1251 static struct timeval timenow; /* Current time */
1252
1253 /*
1254 * timeout - Schedule a timeout.
1255 */
1256 void
timeout(void (* func)(void *),void * arg,int secs,int usecs)1257 timeout(void (*func)(void *), void *arg, int secs, int usecs)
1258 {
1259 struct callout *newp, *p, **pp;
1260
1261 /*
1262 * Allocate timeout.
1263 */
1264 if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL)
1265 fatal("Out of memory in timeout()!");
1266 newp->c_arg = arg;
1267 newp->c_func = func;
1268 get_time(&timenow);
1269 newp->c_time.tv_sec = timenow.tv_sec + secs;
1270 newp->c_time.tv_usec = timenow.tv_usec + usecs;
1271 if (newp->c_time.tv_usec >= 1000000) {
1272 newp->c_time.tv_sec += newp->c_time.tv_usec / 1000000;
1273 newp->c_time.tv_usec %= 1000000;
1274 }
1275
1276 /*
1277 * Find correct place and link it in.
1278 */
1279 for (pp = &callout; (p = *pp); pp = &p->c_next)
1280 if (newp->c_time.tv_sec < p->c_time.tv_sec
1281 || (newp->c_time.tv_sec == p->c_time.tv_sec
1282 && newp->c_time.tv_usec < p->c_time.tv_usec))
1283 break;
1284 newp->c_next = p;
1285 *pp = newp;
1286 }
1287
1288
1289 /*
1290 * untimeout - Unschedule a timeout.
1291 */
1292 void
untimeout(void (* func)(void *),void * arg)1293 untimeout(void (*func)(void *), void *arg)
1294 {
1295 struct callout **copp, *freep;
1296
1297 /*
1298 * Find first matching timeout and remove it from the list.
1299 */
1300 for (copp = &callout; (freep = *copp); copp = &freep->c_next)
1301 if (freep->c_func == func && freep->c_arg == arg) {
1302 *copp = freep->c_next;
1303 free((char *) freep);
1304 break;
1305 }
1306 }
1307
1308
1309 /*
1310 * calltimeout - Call any timeout routines which are now due.
1311 */
1312 static void
calltimeout(void)1313 calltimeout(void)
1314 {
1315 struct callout *p;
1316
1317 while (callout != NULL) {
1318 p = callout;
1319
1320 if (get_time(&timenow) < 0)
1321 fatal("Failed to get time of day: %m");
1322 if (!(p->c_time.tv_sec < timenow.tv_sec
1323 || (p->c_time.tv_sec == timenow.tv_sec
1324 && p->c_time.tv_usec <= timenow.tv_usec)))
1325 break; /* no, it's not time yet */
1326
1327 callout = p->c_next;
1328 (*p->c_func)(p->c_arg);
1329
1330 free((char *) p);
1331 }
1332 }
1333
1334
1335 /*
1336 * timeleft - return the length of time until the next timeout is due.
1337 */
1338 static struct timeval *
timeleft(struct timeval * tvp)1339 timeleft(struct timeval *tvp)
1340 {
1341 if (callout == NULL)
1342 return NULL;
1343
1344 get_time(&timenow);
1345 tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec;
1346 tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec;
1347 if (tvp->tv_usec < 0) {
1348 tvp->tv_usec += 1000000;
1349 tvp->tv_sec -= 1;
1350 }
1351 if (tvp->tv_sec < 0)
1352 tvp->tv_sec = tvp->tv_usec = 0;
1353
1354 return tvp;
1355 }
1356
1357
1358 /*
1359 * kill_my_pg - send a signal to our process group, and ignore it ourselves.
1360 * We assume that sig is currently blocked.
1361 */
1362 static void
kill_my_pg(int sig)1363 kill_my_pg(int sig)
1364 {
1365 struct sigaction act, oldact;
1366 struct subprocess *chp;
1367
1368 if (!detached) {
1369 /*
1370 * There might be other things in our process group that we
1371 * didn't start that would get hit if we did a kill(0), so
1372 * just send the signal individually to our children.
1373 */
1374 for (chp = children; chp != NULL; chp = chp->next)
1375 if (chp->killable)
1376 kill(chp->pid, sig);
1377 return;
1378 }
1379
1380 /* We've done a setsid(), so we can just use a kill(0) */
1381 sigemptyset(&act.sa_mask); /* unnecessary in fact */
1382 act.sa_handler = SIG_IGN;
1383 act.sa_flags = 0;
1384 kill(0, sig);
1385 /*
1386 * The kill() above made the signal pending for us, as well as
1387 * the rest of our process group, but we don't want it delivered
1388 * to us. It is blocked at the moment. Setting it to be ignored
1389 * will cause the pending signal to be discarded. If we did the
1390 * kill() after setting the signal to be ignored, it is unspecified
1391 * (by POSIX) whether the signal is immediately discarded or left
1392 * pending, and in fact Linux would leave it pending, and so it
1393 * would be delivered after the current signal handler exits,
1394 * leading to an infinite loop.
1395 */
1396 sigaction(sig, &act, &oldact);
1397 sigaction(sig, &oldact, NULL);
1398 }
1399
1400
1401 /*
1402 * hup - Catch SIGHUP signal.
1403 *
1404 * Indicates that the physical layer has been disconnected.
1405 * We don't rely on this indication; if the user has sent this
1406 * signal, we just take the link down.
1407 */
1408 static void
hup(int sig)1409 hup(int sig)
1410 {
1411 /* can't log a message here, it can deadlock */
1412 got_sighup = 1;
1413 if (conn_running)
1414 /* Send the signal to the [dis]connector process(es) also */
1415 kill_my_pg(sig);
1416 notify(sigreceived, sig);
1417 if (waiting)
1418 write(sigpipe[1], &sig, sizeof(sig));
1419 }
1420
1421
1422 /*
1423 * term - Catch SIGTERM signal and SIGINT signal (^C/del).
1424 *
1425 * Indicates that we should initiate a graceful disconnect and exit.
1426 */
1427 /*ARGSUSED*/
1428 static void
term(int sig)1429 term(int sig)
1430 {
1431 /* can't log a message here, it can deadlock */
1432 got_sigterm = sig;
1433 if (conn_running)
1434 /* Send the signal to the [dis]connector process(es) also */
1435 kill_my_pg(sig);
1436 notify(sigreceived, sig);
1437 if (waiting)
1438 write(sigpipe[1], &sig, sizeof(sig));
1439 }
1440
1441
1442 /*
1443 * chld - Catch SIGCHLD signal.
1444 * Sets a flag so we will call reap_kids in the mainline.
1445 */
1446 static void
chld(int sig)1447 chld(int sig)
1448 {
1449 got_sigchld = 1;
1450 if (waiting)
1451 write(sigpipe[1], &sig, sizeof(sig));
1452 }
1453
1454
1455 /*
1456 * toggle_debug - Catch SIGUSR1 signal.
1457 *
1458 * Toggle debug flag.
1459 */
1460 /*ARGSUSED*/
1461 static void
toggle_debug(int sig)1462 toggle_debug(int sig)
1463 {
1464 debug = !debug;
1465 if (debug) {
1466 setlogmask(LOG_UPTO(LOG_DEBUG));
1467 } else {
1468 setlogmask(LOG_UPTO(LOG_WARNING));
1469 }
1470 }
1471
1472
1473 /*
1474 * open_ccp - Catch SIGUSR2 signal.
1475 *
1476 * Try to (re)negotiate compression.
1477 */
1478 /*ARGSUSED*/
1479 static void
open_ccp(int sig)1480 open_ccp(int sig)
1481 {
1482 got_sigusr2 = 1;
1483 if (waiting)
1484 write(sigpipe[1], &sig, sizeof(sig));
1485 }
1486
1487
1488 /*
1489 * bad_signal - We've caught a fatal signal. Clean up state and exit.
1490 */
1491 static void
bad_signal(int sig)1492 bad_signal(int sig)
1493 {
1494 static int crashed = 0;
1495
1496 if (crashed)
1497 _exit(127);
1498 crashed = 1;
1499 error("Fatal signal %d", sig);
1500 if (conn_running)
1501 kill_my_pg(SIGTERM);
1502 notify(sigreceived, sig);
1503 die(127);
1504 }
1505
1506 /*
1507 * safe_fork - Create a child process. The child closes all the
1508 * file descriptors that we don't want to leak to a script.
1509 * The parent waits for the child to do this before returning.
1510 * This also arranges for the specified fds to be dup'd to
1511 * fds 0, 1, 2 in the child.
1512 */
1513 pid_t
safe_fork(int infd,int outfd,int errfd)1514 safe_fork(int infd, int outfd, int errfd)
1515 {
1516 pid_t pid;
1517 int fd, pipefd[2];
1518 char buf[1];
1519
1520 /* make sure fds 0, 1, 2 are occupied (probably not necessary) */
1521 while ((fd = dup(fd_devnull)) >= 0) {
1522 if (fd > 2) {
1523 close(fd);
1524 break;
1525 }
1526 }
1527
1528 if (pipe(pipefd) == -1)
1529 pipefd[0] = pipefd[1] = -1;
1530 pid = fork();
1531 if (pid < 0) {
1532 error("fork failed: %m");
1533 return -1;
1534 }
1535 if (pid > 0) {
1536 /* parent */
1537 close(pipefd[1]);
1538 /* this read() blocks until the close(pipefd[1]) below */
1539 complete_read(pipefd[0], buf, 1);
1540 close(pipefd[0]);
1541 return pid;
1542 }
1543
1544 /* Executing in the child */
1545 sys_close();
1546 #ifdef USE_TDB
1547 if (pppdb != NULL)
1548 tdb_close(pppdb);
1549 #endif
1550
1551 /* make sure infd, outfd and errfd won't get tromped on below */
1552 if (infd == 1 || infd == 2)
1553 infd = dup(infd);
1554 if (outfd == 0 || outfd == 2)
1555 outfd = dup(outfd);
1556 if (errfd == 0 || errfd == 1)
1557 errfd = dup(errfd);
1558
1559 closelog();
1560
1561 /* dup the in, out, err fds to 0, 1, 2 */
1562 if (infd != 0)
1563 dup2(infd, 0);
1564 if (outfd != 1)
1565 dup2(outfd, 1);
1566 if (errfd != 2)
1567 dup2(errfd, 2);
1568
1569 if (log_to_fd > 2)
1570 close(log_to_fd);
1571 if (the_channel->close)
1572 (*the_channel->close)();
1573 else
1574 close(devfd); /* some plugins don't have a close function */
1575 close(fd_ppp);
1576 close(fd_devnull);
1577 if (infd != 0)
1578 close(infd);
1579 if (outfd != 1)
1580 close(outfd);
1581 if (errfd != 2)
1582 close(errfd);
1583
1584 notify(fork_notifier, 0);
1585 close(pipefd[0]);
1586 /* this close unblocks the read() call above in the parent */
1587 close(pipefd[1]);
1588
1589 return 0;
1590 }
1591
1592 static bool
add_script_env(int pos,char * newstring)1593 add_script_env(int pos, char *newstring)
1594 {
1595 if (pos + 1 >= s_env_nalloc) {
1596 int new_n = pos + 17;
1597 char **newenv = realloc(script_env, new_n * sizeof(char *));
1598 if (newenv == NULL) {
1599 free(newstring - 1);
1600 return 0;
1601 }
1602 script_env = newenv;
1603 s_env_nalloc = new_n;
1604 }
1605 script_env[pos] = newstring;
1606 script_env[pos + 1] = NULL;
1607 return 1;
1608 }
1609
1610 static void
remove_script_env(int pos)1611 remove_script_env(int pos)
1612 {
1613 free(script_env[pos] - 1);
1614 while ((script_env[pos] = script_env[pos + 1]) != NULL)
1615 pos++;
1616 }
1617
1618 /*
1619 * update_system_environment - process the list of set/unset options
1620 * and update the system environment.
1621 */
1622 static void
update_system_environment(void)1623 update_system_environment(void)
1624 {
1625 struct userenv *uep;
1626
1627 for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1628 if (uep->ue_isset)
1629 setenv(uep->ue_name, uep->ue_value, 1);
1630 else
1631 unsetenv(uep->ue_name);
1632 }
1633 }
1634
1635 /*
1636 * device_script - run a program to talk to the specified fds
1637 * (e.g. to run the connector or disconnector script).
1638 * stderr gets connected to the log fd or to the _PATH_CONNERRS file.
1639 */
1640 int
device_script(char * program,int in,int out,int dont_wait)1641 device_script(char *program, int in, int out, int dont_wait)
1642 {
1643 int pid;
1644 int status = -1;
1645 int errfd;
1646
1647 if (log_to_fd >= 0)
1648 errfd = log_to_fd;
1649 else {
1650 errfd = open(_PATH_CONNERRS, O_WRONLY | O_APPEND | O_CREAT, 0600);
1651 if (errfd == -1) {
1652 error("Cannot open `%s': %m", _PATH_CONNERRS);
1653 return -1;
1654 }
1655 }
1656
1657 ++conn_running;
1658 pid = safe_fork(in, out, errfd);
1659
1660 if (pid != 0 && log_to_fd < 0)
1661 close(errfd);
1662
1663 if (pid < 0) {
1664 --conn_running;
1665 error("Failed to create child process: %m");
1666 return -1;
1667 }
1668
1669 if (pid != 0) {
1670 record_child(pid, program, NULL, NULL, 1);
1671 status = 0;
1672 if (!dont_wait) {
1673 while (waitpid(pid, &status, 0) < 0) {
1674 if (errno == EINTR)
1675 continue;
1676 fatal("error waiting for (dis)connection process: %m");
1677 }
1678 forget_child(pid, status);
1679 --conn_running;
1680 }
1681 return (status == 0 ? 0 : -1);
1682 }
1683
1684 /* here we are executing in the child */
1685
1686 setgid(getgid());
1687 setuid(uid);
1688 if (getuid() != uid) {
1689 fprintf(stderr, "pppd: setuid failed\n");
1690 exit(1);
1691 }
1692 update_system_environment();
1693 execl("/bin/sh", "sh", "-c", program, (char *)0);
1694 perror("pppd: could not exec /bin/sh");
1695 _exit(99);
1696 /* NOTREACHED */
1697 }
1698
1699
1700 /*
1701 * update_script_environment - process the list of set/unset options
1702 * and update the script environment. Note that we intentionally do
1703 * not update the TDB. These changes are layered on top right before
1704 * exec. It is not possible to use script_setenv() or
1705 * script_unsetenv() safely after this routine is run.
1706 */
1707 static void
update_script_environment(void)1708 update_script_environment(void)
1709 {
1710 struct userenv *uep;
1711
1712 for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1713 int i;
1714 char *p, *newstring;
1715 int nlen = strlen(uep->ue_name);
1716
1717 for (i = 0; (p = script_env[i]) != NULL; i++) {
1718 if (strncmp(p, uep->ue_name, nlen) == 0 && p[nlen] == '=')
1719 break;
1720 }
1721 if (uep->ue_isset) {
1722 nlen += strlen(uep->ue_value) + 2;
1723 newstring = malloc(nlen + 1);
1724 if (newstring == NULL)
1725 continue;
1726 *newstring++ = 0;
1727 slprintf(newstring, nlen, "%s=%s", uep->ue_name, uep->ue_value);
1728 if (p != NULL)
1729 script_env[i] = newstring;
1730 else
1731 add_script_env(i, newstring);
1732 } else if (p != NULL) {
1733 remove_script_env(i);
1734 }
1735 }
1736 }
1737
1738 /*
1739 * run_program - execute a program with given arguments,
1740 * but don't wait for it unless wait is non-zero.
1741 * If the program can't be executed, logs an error unless
1742 * must_exist is 0 and the program file doesn't exist.
1743 * Returns -1 if it couldn't fork, 0 if the file doesn't exist
1744 * or isn't an executable plain file, or the process ID of the child.
1745 * If done != NULL, (*done)(arg) will be called later (within
1746 * reap_kids) iff the return value is > 0.
1747 */
1748 pid_t
run_program(char * prog,char ** args,int must_exist,void (* done)(void *),void * arg,int wait)1749 run_program(char *prog, char **args, int must_exist, void (*done)(void *), void *arg, int wait)
1750 {
1751 int pid, status;
1752 struct stat sbuf;
1753
1754 /*
1755 * First check if the file exists and is executable.
1756 * We don't use access() because that would use the
1757 * real user-id, which might not be root, and the script
1758 * might be accessible only to root.
1759 */
1760 errno = EINVAL;
1761 if (stat(prog, &sbuf) < 0 || !S_ISREG(sbuf.st_mode)
1762 || (sbuf.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0) {
1763 if (must_exist || errno != ENOENT)
1764 warn("Can't execute %s: %m", prog);
1765 return 0;
1766 }
1767
1768 pid = safe_fork(fd_devnull, fd_devnull, fd_devnull);
1769 if (pid == -1) {
1770 error("Failed to create child process for %s: %m", prog);
1771 return -1;
1772 }
1773 if (pid != 0) {
1774 if (debug)
1775 dbglog("Script %s started (pid %d)", prog, pid);
1776 record_child(pid, prog, done, arg, 0);
1777 if (wait) {
1778 while (waitpid(pid, &status, 0) < 0) {
1779 if (errno == EINTR)
1780 continue;
1781 fatal("error waiting for script %s: %m", prog);
1782 }
1783 forget_child(pid, status);
1784 }
1785 return pid;
1786 }
1787
1788 /* Leave the current location */
1789 (void) setsid(); /* No controlling tty. */
1790 (void) umask (S_IRWXG|S_IRWXO);
1791 (void) chdir ("/"); /* no current directory. */
1792 setuid(0); /* set real UID = root */
1793 setgid(getegid());
1794
1795 #ifdef BSD
1796 /* Force the priority back to zero if pppd is running higher. */
1797 if (setpriority (PRIO_PROCESS, 0, 0) < 0)
1798 warn("can't reset priority to 0: %m");
1799 #endif
1800
1801 /* run the program */
1802 update_script_environment();
1803 execve(prog, args, script_env);
1804 if (must_exist || errno != ENOENT) {
1805 /* have to reopen the log, there's nowhere else
1806 for the message to go. */
1807 reopen_log();
1808 syslog(LOG_ERR, "Can't execute %s: %m", prog);
1809 closelog();
1810 }
1811 _exit(99);
1812 }
1813
1814
1815 /*
1816 * record_child - add a child process to the list for reap_kids
1817 * to use.
1818 */
1819 void
record_child(int pid,char * prog,void (* done)(void *),void * arg,int killable)1820 record_child(int pid, char *prog, void (*done)(void *), void *arg, int killable)
1821 {
1822 struct subprocess *chp;
1823
1824 ++n_children;
1825
1826 chp = (struct subprocess *) malloc(sizeof(struct subprocess));
1827 if (chp == NULL) {
1828 warn("losing track of %s process", prog);
1829 } else {
1830 chp->pid = pid;
1831 chp->prog = prog;
1832 chp->done = done;
1833 chp->arg = arg;
1834 chp->next = children;
1835 chp->killable = killable;
1836 children = chp;
1837 }
1838 }
1839
1840 /*
1841 * childwait_end - we got fed up waiting for the child processes to
1842 * exit, send them all a SIGTERM.
1843 */
1844 static void
childwait_end(void * arg)1845 childwait_end(void *arg)
1846 {
1847 struct subprocess *chp;
1848
1849 for (chp = children; chp != NULL; chp = chp->next) {
1850 if (debug)
1851 dbglog("sending SIGTERM to process %d", chp->pid);
1852 kill(chp->pid, SIGTERM);
1853 }
1854 childwait_done = 1;
1855 }
1856
1857 /*
1858 * forget_child - clean up after a dead child
1859 */
1860 static void
forget_child(int pid,int status)1861 forget_child(int pid, int status)
1862 {
1863 struct subprocess *chp, **prevp;
1864
1865 for (prevp = &children; (chp = *prevp) != NULL; prevp = &chp->next) {
1866 if (chp->pid == pid) {
1867 --n_children;
1868 *prevp = chp->next;
1869 break;
1870 }
1871 }
1872 if (WIFSIGNALED(status)) {
1873 warn("Child process %s (pid %d) terminated with signal %d",
1874 (chp? chp->prog: "??"), pid, WTERMSIG(status));
1875 } else if (debug)
1876 dbglog("Script %s finished (pid %d), status = 0x%x",
1877 (chp? chp->prog: "??"), pid,
1878 WIFEXITED(status) ? WEXITSTATUS(status) : status);
1879 if (chp && chp->done)
1880 (*chp->done)(chp->arg);
1881 if (chp)
1882 free(chp);
1883 }
1884
1885 /*
1886 * reap_kids - get status from any dead child processes,
1887 * and log a message for abnormal terminations.
1888 */
1889 static int
reap_kids(void)1890 reap_kids(void)
1891 {
1892 int pid, status;
1893
1894 if (n_children == 0)
1895 return 0;
1896 while ((pid = waitpid(-1, &status, WNOHANG)) != -1 && pid != 0) {
1897 forget_child(pid, status);
1898 }
1899 if (pid == -1) {
1900 if (errno == ECHILD)
1901 return -1;
1902 if (errno != EINTR)
1903 error("Error waiting for child process: %m");
1904 }
1905 return 0;
1906 }
1907
1908 /*
1909 * add_notifier - add a new function to be called when something happens.
1910 */
1911 void
add_notifier(struct notifier ** notif,notify_func func,void * arg)1912 add_notifier(struct notifier **notif, notify_func func, void *arg)
1913 {
1914 struct notifier *np;
1915
1916 np = malloc(sizeof(struct notifier));
1917 if (np == 0)
1918 novm("notifier struct");
1919 np->next = *notif;
1920 np->func = func;
1921 np->arg = arg;
1922 *notif = np;
1923 }
1924
1925 /*
1926 * remove_notifier - remove a function from the list of things to
1927 * be called when something happens.
1928 */
1929 void
remove_notifier(struct notifier ** notif,notify_func func,void * arg)1930 remove_notifier(struct notifier **notif, notify_func func, void *arg)
1931 {
1932 struct notifier *np;
1933
1934 for (; (np = *notif) != 0; notif = &np->next) {
1935 if (np->func == func && np->arg == arg) {
1936 *notif = np->next;
1937 free(np);
1938 break;
1939 }
1940 }
1941 }
1942
1943 /*
1944 * notify - call a set of functions registered with add_notifier.
1945 */
1946 void
notify(struct notifier * notif,int val)1947 notify(struct notifier *notif, int val)
1948 {
1949 struct notifier *np;
1950
1951 while ((np = notif) != 0) {
1952 notif = np->next;
1953 (*np->func)(np->arg, val);
1954 }
1955 }
1956
1957 /*
1958 * novm - log an error message saying we ran out of memory, and die.
1959 */
1960 void
novm(char * msg)1961 novm(char *msg)
1962 {
1963 fatal("Virtual memory exhausted allocating %s\n", msg);
1964 }
1965
1966 /*
1967 * script_setenv - set an environment variable value to be used
1968 * for scripts that we run (e.g. ip-up, auth-up, etc.)
1969 */
1970 void
script_setenv(char * var,char * value,int iskey)1971 script_setenv(char *var, char *value, int iskey)
1972 {
1973 size_t varl = strlen(var);
1974 size_t vl = varl + strlen(value) + 2;
1975 int i;
1976 char *p, *newstring;
1977
1978 newstring = (char *) malloc(vl+1);
1979 if (newstring == 0)
1980 return;
1981 *newstring++ = iskey;
1982 slprintf(newstring, vl, "%s=%s", var, value);
1983
1984 /* check if this variable is already set */
1985 if (script_env != 0) {
1986 for (i = 0; (p = script_env[i]) != 0; ++i) {
1987 if (strncmp(p, var, varl) == 0 && p[varl] == '=') {
1988 #ifdef USE_TDB
1989 if (p[-1] && pppdb != NULL)
1990 delete_db_key(p);
1991 #endif
1992 free(p-1);
1993 script_env[i] = newstring;
1994 #ifdef USE_TDB
1995 if (pppdb != NULL) {
1996 if (iskey)
1997 add_db_key(newstring);
1998 update_db_entry();
1999 }
2000 #endif
2001 return;
2002 }
2003 }
2004 } else {
2005 /* no space allocated for script env. ptrs. yet */
2006 i = 0;
2007 script_env = malloc(16 * sizeof(char *));
2008 if (script_env == 0) {
2009 free(newstring - 1);
2010 return;
2011 }
2012 s_env_nalloc = 16;
2013 }
2014
2015 if (!add_script_env(i, newstring))
2016 return;
2017
2018 #ifdef USE_TDB
2019 if (pppdb != NULL) {
2020 if (iskey)
2021 add_db_key(newstring);
2022 update_db_entry();
2023 }
2024 #endif
2025 }
2026
2027 /*
2028 * script_unsetenv - remove a variable from the environment
2029 * for scripts.
2030 */
2031 void
script_unsetenv(char * var)2032 script_unsetenv(char *var)
2033 {
2034 int vl = strlen(var);
2035 int i;
2036 char *p;
2037
2038 if (script_env == 0)
2039 return;
2040 for (i = 0; (p = script_env[i]) != 0; ++i) {
2041 if (strncmp(p, var, vl) == 0 && p[vl] == '=') {
2042 #ifdef USE_TDB
2043 if (p[-1] && pppdb != NULL)
2044 delete_db_key(p);
2045 #endif
2046 remove_script_env(i);
2047 break;
2048 }
2049 }
2050 #ifdef USE_TDB
2051 if (pppdb != NULL)
2052 update_db_entry();
2053 #endif
2054 }
2055
2056 /*
2057 * Any arbitrary string used as a key for locking the database.
2058 * It doesn't matter what it is as long as all pppds use the same string.
2059 */
2060 #define PPPD_LOCK_KEY "pppd lock"
2061
2062 /*
2063 * lock_db - get an exclusive lock on the TDB database.
2064 * Used to ensure atomicity of various lookup/modify operations.
2065 */
lock_db(void)2066 void lock_db(void)
2067 {
2068 #ifdef USE_TDB
2069 TDB_DATA key;
2070
2071 key.dptr = PPPD_LOCK_KEY;
2072 key.dsize = strlen(key.dptr);
2073 tdb_chainlock(pppdb, key);
2074 #endif
2075 }
2076
2077 /*
2078 * unlock_db - remove the exclusive lock obtained by lock_db.
2079 */
unlock_db(void)2080 void unlock_db(void)
2081 {
2082 #ifdef USE_TDB
2083 TDB_DATA key;
2084
2085 key.dptr = PPPD_LOCK_KEY;
2086 key.dsize = strlen(key.dptr);
2087 tdb_chainunlock(pppdb, key);
2088 #endif
2089 }
2090
2091 #ifdef USE_TDB
2092 /*
2093 * update_db_entry - update our entry in the database.
2094 */
2095 static void
update_db_entry(void)2096 update_db_entry(void)
2097 {
2098 TDB_DATA key, dbuf;
2099 int vlen, i;
2100 char *p, *q, *vbuf;
2101
2102 if (script_env == NULL)
2103 return;
2104 vlen = 0;
2105 for (i = 0; (p = script_env[i]) != 0; ++i)
2106 vlen += strlen(p) + 1;
2107 vbuf = malloc(vlen + 1);
2108 if (vbuf == 0)
2109 novm("database entry");
2110 q = vbuf;
2111 for (i = 0; (p = script_env[i]) != 0; ++i)
2112 q += slprintf(q, vbuf + vlen - q, "%s;", p);
2113
2114 key.dptr = db_key;
2115 key.dsize = strlen(db_key);
2116 dbuf.dptr = vbuf;
2117 dbuf.dsize = vlen;
2118 if (tdb_store(pppdb, key, dbuf, TDB_REPLACE))
2119 error("tdb_store failed: %s", tdb_errorstr(pppdb));
2120
2121 if (vbuf)
2122 free(vbuf);
2123
2124 }
2125
2126 /*
2127 * add_db_key - add a key that we can use to look up our database entry.
2128 */
2129 static void
add_db_key(const char * str)2130 add_db_key(const char *str)
2131 {
2132 TDB_DATA key, dbuf;
2133
2134 key.dptr = (char *) str;
2135 key.dsize = strlen(str);
2136 dbuf.dptr = db_key;
2137 dbuf.dsize = strlen(db_key);
2138 if (tdb_store(pppdb, key, dbuf, TDB_REPLACE))
2139 error("tdb_store key failed: %s", tdb_errorstr(pppdb));
2140 }
2141
2142 /*
2143 * delete_db_key - delete a key for looking up our database entry.
2144 */
2145 static void
delete_db_key(const char * str)2146 delete_db_key(const char *str)
2147 {
2148 TDB_DATA key;
2149
2150 key.dptr = (char *) str;
2151 key.dsize = strlen(str);
2152 tdb_delete(pppdb, key);
2153 }
2154
2155 /*
2156 * cleanup_db - delete all the entries we put in the database.
2157 */
2158 static void
cleanup_db(void)2159 cleanup_db(void)
2160 {
2161 TDB_DATA key;
2162 int i;
2163 char *p;
2164
2165 key.dptr = db_key;
2166 key.dsize = strlen(db_key);
2167 tdb_delete(pppdb, key);
2168 for (i = 0; (p = script_env[i]) != 0; ++i)
2169 if (p[-1])
2170 delete_db_key(p);
2171 }
2172 #endif /* USE_TDB */
2173