]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/main.c
config: Include some extra files in the tarball
[ppp.git] / pppd / main.c
index 1a2a446cc5ffda2c141f28b5f13eeccc08d02019..3b3fc4542f3fa4c46645f85f8f26752abab65d39 100644 (file)
@@ -39,7 +39,7 @@
  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * Copyright (c) 1999-2020 Paul Mackerras. All rights reserved.
+ * Copyright (c) 1999-2024 Paul Mackerras. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
- * 2. The name(s) of the authors of this software must not be used to
- *    endorse or promote products derived from this software without
- *    prior written permission.
- *
- * 3. Redistributions of any form whatsoever must retain the following
- *    acknowledgment:
- *    "This product includes software developed by Paul Mackerras
- *     <paulus@samba.org>".
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
  *
  * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
@@ -248,6 +244,7 @@ static void holdoff_end(void *);
 static void forget_child(int pid, int status);
 static int reap_kids(void);
 static void childwait_end(void *);
+static void run_net_script(char* script, int wait);
 
 #ifdef PPP_WITH_TDB
 static void update_db_entry(void);
@@ -313,7 +310,7 @@ int ppp_get_ifname(char *buf, size_t bufsz)
 
 void ppp_set_ifname(const char *name)
 {
-    if (ifname) {
+    if (name) {
         strlcpy(ifname, name, sizeof(ifname));
     }
 }
@@ -362,10 +359,16 @@ main(int argc, char *argv[])
     struct protent *protp;
     char numbuf[16];
 
-    PPP_crypto_init();
+    strlcpy(path_upapfile, PPP_PATH_UPAPFILE, MAXPATHLEN);
+    strlcpy(path_chapfile, PPP_PATH_CHAPFILE, MAXPATHLEN);
+
+    strlcpy(path_net_init, PPP_PATH_NET_INIT, MAXPATHLEN);
+    strlcpy(path_net_preup, PPP_PATH_NET_PREUP, MAXPATHLEN);
+    strlcpy(path_net_down, PPP_PATH_NET_DOWN, MAXPATHLEN);
 
     strlcpy(path_ipup, PPP_PATH_IPUP, MAXPATHLEN);
     strlcpy(path_ipdown, PPP_PATH_IPDOWN, MAXPATHLEN);
+    strlcpy(path_ippreup, PPP_PATH_IPPREUP, MAXPATHLEN);
 
 #ifdef PPP_WITH_IPV6CP
     strlcpy(path_ipv6up, PPP_PATH_IPV6UP, MAXPATHLEN);
@@ -380,6 +383,11 @@ main(int argc, char *argv[])
     /* Initialize syslog facilities */
     reopen_log();
 
+    /* Initialize crypto libraries */
+    if (!PPP_crypto_init()) {
+        exit(1);
+    }
+
     if (gethostname(hostname, sizeof(hostname)) < 0 ) {
        ppp_option_error("Couldn't get hostname: %m");
        exit(1);
@@ -599,8 +607,10 @@ main(int argc, char *argv[])
        while (phase != PHASE_DEAD) {
            handle_events();
            get_input();
-           if (kill_link)
+           if (kill_link) {
                lcp_close(0, "User request");
+               need_holdoff = 0;
+           }
            if (asked_to_quit) {
                bundle_terminating = 1;
                if (phase == PHASE_MASTER)
@@ -799,6 +809,26 @@ setup_signals(void)
     signal(SIGPIPE, SIG_IGN);
 }
 
+/*
+ * net-* scripts to be run come through here.
+ */
+void run_net_script(char* script, int wait)
+{
+    char strspeed[32];
+    char *argv[6];
+
+    slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
+
+    argv[0] = script;
+    argv[1] = ifname;
+    argv[2] = devnam;
+    argv[3] = strspeed;
+    argv[4] = ipparam;
+    argv[5] = NULL;
+
+    run_program(script, argv, 0, NULL, NULL, wait);
+}
+
 /*
  * set_ifunit - do things we need to do once we know which ppp
  * unit we are using.
@@ -820,6 +850,9 @@ set_ifunit(int iskey)
        create_pidfile(getpid());       /* write pid to file */
        create_linkpidfile(getpid());
     }
+    if (*remote_number)
+        ppp_script_setenv("REMOTENUMBER", remote_number, 0);
+    run_net_script(path_net_init, 1);
 }
 
 /*
@@ -888,7 +921,8 @@ create_pidfile(int pid)
 {
     FILE *pidfile;
 
-    slprintf(pidfilename, sizeof(pidfilename), "%s%s.pid",
+    mkdir_recursive(PPP_PATH_VARRUN);
+    slprintf(pidfilename, sizeof(pidfilename), "%s/%s.pid",
             PPP_PATH_VARRUN, ifname);
     if ((pidfile = fopen(pidfilename, "w")) != NULL) {
        fprintf(pidfile, "%d\n", pid);
@@ -907,7 +941,7 @@ create_linkpidfile(int pid)
     if (linkname[0] == 0)
        return;
     ppp_script_setenv("LINKNAME", linkname, 1);
-    slprintf(linkpidfile, sizeof(linkpidfile), "%sppp-%s.pid",
+    slprintf(linkpidfile, sizeof(linkpidfile), "%s/ppp-%s.pid",
             PPP_PATH_VARRUN, linkname);
     if ((pidfile = fopen(linkpidfile, "w")) != NULL) {
        fprintf(pidfile, "%d\n", pid);
@@ -1117,6 +1151,7 @@ get_input(void)
        notice("Modem hangup");
        hungup = 1;
        code = EXIT_HANGUP;
+       need_holdoff = 0;
        lcp_lowerdown(0);       /* serial link is no longer available */
        link_terminated(0);
        return;
@@ -1222,6 +1257,23 @@ ppp_recv_config(int unit, int mru, u_int32_t accm, int pcomp, int accomp)
 void
 new_phase(ppp_phase_t p)
 {
+    switch (p) {
+    case PHASE_NETWORK:
+       if (phase <= PHASE_NETWORK) {
+           char iftmpname[IFNAMSIZ];
+           int ifindex = if_nametoindex(ifname);
+           run_net_script(path_net_preup, 1);
+           if (if_indextoname(ifindex, iftmpname) && strcmp(iftmpname, ifname)) {
+               info("Detected interface name change from %s to %s.", ifname, iftmpname);
+               strcpy(ifname, iftmpname);
+           }
+       }
+       break;
+    case PHASE_DISCONNECT:
+       run_net_script(path_net_down, 0);
+       break;
+    }
+
     phase = p;
     if (new_phase_hook)
        (*new_phase_hook)(p);
@@ -1280,7 +1332,7 @@ print_link_stats(void)
     if (link_stats_print && link_stats_valid) {
        int t = (link_connect_time + 5) / 6;    /* 1/10ths of minutes */
        info("Connect time %d.%d minutes.", t/10, t%10);
-       info("Sent %u bytes, received %u bytes.",
+       info("Sent %llu bytes, received %llu bytes.",
            link_stats.bytes_out, link_stats.bytes_in);
        link_stats_print = 0;
     }
@@ -1292,9 +1344,9 @@ print_link_stats(void)
 void
 reset_link_stats(int u)
 {
-    if (!get_ppp_stats(u, &old_link_stats))
-       return;
+    get_ppp_stats(u, &old_link_stats);
     ppp_get_time(&start_time);
+    link_stats_print = 1;
 }
 
 /*
@@ -2038,8 +2090,10 @@ struct notifier **get_notifier_by_type(ppp_notify_t type)
         [NF_SIGNALED    ] = &sigreceived,
         [NF_IP_UP       ] = &ip_up_notifier,
         [NF_IP_DOWN     ] = &ip_down_notifier,
+#ifdef PPP_WITH_IPV6CP
         [NF_IPV6_UP     ] = &ipv6_up_notifier,
         [NF_IPV6_DOWN   ] = &ipv6_down_notifier,
+#endif
         [NF_AUTH_UP     ] = &auth_up_notifier,
         [NF_LINK_DOWN   ] = &link_down_notifier,
         [NF_FORK        ] = &fork_notifier,
@@ -2109,7 +2163,7 @@ notify(struct notifier *notif, int val)
  * novm - log an error message saying we ran out of memory, and die.
  */
 void
-novm(char *msg)
+novm(const char *msg)
 {
     fatal("Virtual memory exhausted allocating %s\n", msg);
 }