]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/sys-linux.c
use shadow password stuff
[ppp.git] / pppd / sys-linux.c
index 425662dc14061a83d3a37eb2dca8ca1b84f15581..c86d668d8ddda4a6e07e1909019b0cdd806771ca 100644 (file)
@@ -24,6 +24,7 @@
 #include <sys/time.h>
 #include <sys/errno.h>
 #include <sys/file.h>
+#include <sys/stat.h>
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -46,6 +47,7 @@
 #define MAX_ADDR_LEN 7
 #endif
 
+#include <linux/version.h>
 #include <net/if.h>
 #include <linux/ppp_defs.h>
 #include <net/if_arp.h>
 #include "fsm.h"
 #include "ipcp.h"
 
+#ifndef RTF_DEFAULT  /* Normally in <linux/route.h> from <net/route.h> */
+#define RTF_DEFAULT  0
+#endif
+
 #ifdef IPX_CHANGE
 #include "ipxcp.h"
 #endif
 
+#ifdef LOCKLIB
+#include <sys/locks.h>
+#endif
+
 #define ok_error(num) ((num)==EIO)
 
 static int tty_disc = N_TTY;   /* The TTY discipline */
@@ -231,7 +241,7 @@ void sys_cleanup(void)
  */
     if (default_route_gateway != 0)
       {
-       cifdefaultroute(0, default_route_gateway);
+       cifdefaultroute(0, 0, default_route_gateway);
       }
 
     if (has_proxy_arp)
@@ -317,11 +327,8 @@ void establish_ppp (int tty_fd)
  */
     if (demand && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0)
       {
-       if ( ! ok_error ( errno ))
-         {
-           syslog(LOG_ERR, "ioctl(transfer ppp unit): %m(%d)", errno);
-           die(1);
-         }
+       syslog(LOG_ERR, "ioctl(transfer ppp unit): %m(%d)", errno);
+       die(1);
       }
 /*
  * Set the current tty to the PPP discpline
@@ -356,14 +363,14 @@ void establish_ppp (int tty_fd)
                   ifunit, x);
            die(1);
          }
-       ioctl(slave_fd, TIOCSETD, &tty_disc);
       }
 
     ifunit = x;
 /*
  * Enable debug in the driver if requested.
  */
-    set_kdebugflag (kdebugflag);
+    if (!demand)
+      set_kdebugflag (kdebugflag);
 
     set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 |
                               SC_RCV_EVNP | SC_RCV_ODDP));
@@ -396,23 +403,16 @@ void disestablish_ppp(int tty_fd)
   {
     int x;
     char *s;
-/*
- * Do nothing if the PPP device is controlled by the loopback device
- */
-    if (tty_fd != ppp_fd)
-      {
-       return;
-      }
+
 /*
  * Attempt to restore the previous tty settings
  */
-    if (still_ppp())
+    if (!hungup)
       {
-       set_kdebugflag (0);
 /*
  * Restore the previous line discipline
  */
-       if (ioctl(ppp_fd, TIOCSETD, &tty_disc) < 0)
+       if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0)
          {
            if ( ! ok_error (errno))
              {
@@ -420,7 +420,7 @@ void disestablish_ppp(int tty_fd)
              }
          }
        
-       if (ioctl(ppp_fd, TIOCNXCL, 0) < 0)
+       if (ioctl(tty_fd, TIOCNXCL, 0) < 0)
          {
            if ( ! ok_error (errno))
              {
@@ -429,7 +429,7 @@ void disestablish_ppp(int tty_fd)
          }
 
        /* Reset non-blocking mode on fd. */
-       if (initfdflags != -1 && fcntl(ppp_fd, F_SETFL, initfdflags) < 0)
+       if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0)
          {
            if ( ! ok_error (errno))
              {
@@ -458,10 +458,6 @@ void clean_check(void)
            s = NULL;
            switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP))
              {
-             case SC_RCV_B7_0 | SC_RCV_B7_1 | SC_RCV_EVNP | SC_RCV_ODDP:
-               s = "nothing was received";
-               break;
-               
              case SC_RCV_B7_0:
              case SC_RCV_B7_0 | SC_RCV_EVNP:
              case SC_RCV_B7_0 | SC_RCV_ODDP:
@@ -1256,7 +1252,7 @@ static int defaultroute_exists (struct rtentry *rt)
  * sifdefaultroute - assign a default route through the address given.
  */
 
-int sifdefaultroute (int unit, u_int32_t gateway)
+int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
   {
     struct rtentry rt;
 
@@ -1278,9 +1274,15 @@ int sifdefaultroute (int unit, u_int32_t gateway)
     memset (&rt, '\0', sizeof (rt));
     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
+
+#if LINUX_VERSION_CODE > 0x020100
+    SET_SA_FAMILY (rt.rt_genmask, AF_INET);
+    ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
+#endif
+
     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
     
-    rt.rt_flags = RTF_UP | RTF_GATEWAY;
+    rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
     if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
       {
        if ( ! ok_error ( errno ))
@@ -1299,7 +1301,7 @@ int sifdefaultroute (int unit, u_int32_t gateway)
  * cifdefaultroute - delete a default route through the address given.
  */
 
-int cifdefaultroute (int unit, u_int32_t gateway)
+int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
   {
     struct rtentry rt;
 
@@ -1308,9 +1310,15 @@ int cifdefaultroute (int unit, u_int32_t gateway)
     memset (&rt, '\0', sizeof (rt));
     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
+
+#if LINUX_VERSION_CODE > 0x020100
+    SET_SA_FAMILY (rt.rt_genmask, AF_INET);
+    ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
+#endif
+
     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
     
-    rt.rt_flags = RTF_UP | RTF_GATEWAY;
+    rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
     if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
       {
        if (still_ppp())
@@ -1381,8 +1389,9 @@ int cifproxyarp (int unit, u_int32_t his_adr)
       {
        memset (&arpreq, '\0', sizeof(arpreq));
        SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
-    
        ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
+       arpreq.arp_flags = ATF_PERM | ATF_PUBL;
+
        if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0)
          {
            if ( ! ok_error ( errno ))
@@ -1409,7 +1418,6 @@ static int get_ether_addr (u_int32_t ipaddr,
     struct ifreq *ifr, *ifend, *ifp;
     int i;
     u_int32_t ina, mask;
-    struct sockaddr_dl *dla;
     struct ifreq ifreq;
     struct ifconf ifc;
     struct ifreq ifs[MAX_IFS];
@@ -1809,7 +1817,7 @@ int ppp_available(void)
  * Update the wtmp file with the appropriate user name and tty device.
  */
 
-int logwtmp (char *line, char *name, char *host)
+void logwtmp (const char *line, const char *name, const char *host)
   {
     int    wtmp;
     struct utmp ut, *utp;
@@ -1897,6 +1905,35 @@ int logwtmp (char *line, char *name, char *host)
 
 int lock (char *dev)
   {
+#ifdef LOCKLIB
+    int result;
+    lock_file = malloc(strlen(dev) + 1);
+    if (lock_file == NULL)
+      {
+       novm("lock file name");
+      }
+    strcpy (lock_file, dev);
+    result = mklock (dev, (void *) 0);
+
+    if (result > 0)
+      {
+        syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, result);
+       free (lock_file);
+       lock_file = NULL;
+       result = -1;
+      }
+    else
+      {
+        if (result < 0)
+         {
+           syslog (LOG_ERR, "Can't create lock file %s", lock_file);
+           free (lock_file);
+           lock_file = NULL;
+           result = -1;
+         }
+      }
+    return (result);
+#else
     char hdb_lock_buffer[12];
     int fd, n;
     int pid = getpid();
@@ -1967,7 +2004,8 @@ int lock (char *dev)
 #else
                pid = ((int *) hdb_lock_buffer)[0];
 #endif
-               if (pid == 0 || (kill(pid, 0) == -1 && errno == ESRCH))
+               if (pid == 0 || pid == getpid()
+                   || (kill(pid, 0) == -1 && errno == ESRCH))
                  {
                    n = 0;
                  }
@@ -1992,6 +2030,7 @@ int lock (char *dev)
     free(lock_file);
     lock_file = NULL;
     return -1;
+#endif
 }
 
 
@@ -2004,7 +2043,11 @@ void unlock(void)
   {
     if (lock_file)
       {
+#ifdef LOCKLIB
+       (void) rmlock (lock_file, (void *) 0);
+#else
        unlink(lock_file);
+#endif
        free(lock_file);
        lock_file = NULL;
       }
@@ -2174,14 +2217,20 @@ int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
 /*
  *  Add the device route
  */
+#if LINUX_VERSION_CODE < 0x020100+16           /* 2.1.16 */
     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
-    rt.rt_dev = ifname;  /* MJC */
+    rt.rt_dev = ifname;
 
     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L;
     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr     = his_adr;
     rt.rt_flags = RTF_UP | RTF_HOST;
 
+#if LINUX_VERSION_CODE > 0x020100
+    SET_SA_FAMILY (rt.rt_genmask, AF_INET);
+    ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
+#endif
+
     if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
       {
        if (! ok_error (errno))
@@ -2190,6 +2239,7 @@ int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
          }
         return (0);
       }
+#endif
     return 1;
   }
 
@@ -2201,6 +2251,7 @@ int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
 
 int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
   {
+#if LINUX_VERSION_CODE < 0x020100+16           /* 2.1.16 */
     struct rtentry rt;
 /*
  *  Delete the route through the device
@@ -2209,12 +2260,17 @@ int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
 
     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
-    rt.rt_dev = ifname;  /* MJC */
+    rt.rt_dev = ifname;
 
     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr     = his_adr;
     rt.rt_flags = RTF_UP | RTF_HOST;
 
+#if LINUX_VERSION_CODE > 0x020100
+    SET_SA_FAMILY (rt.rt_genmask, AF_INET);
+    ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
+#endif
+
     if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
       {
        if (still_ppp() && ! ok_error (errno))
@@ -2223,6 +2279,7 @@ int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
          }
        return (0);
       }
+#endif
     return 1;
   }
 
@@ -2297,81 +2354,23 @@ open_ppp_loopback(void)
  *
  * restore_loop - reattach the ppp unit to the loopback.
  *
- * The problem with the Linux variant is that the POSIX tty drivers will
- * sieze the line when it is disconnected. In addition, when the device
- * goes down all of the routes are deleted. This means that the tty needs
- * to be re-opened, reconfigured, and the device reconfigured and the routes
- * restored.
+ * The kernel ppp driver automatically reattaches the ppp unit to
+ * the loopback if the serial port is set to a line discipline other
+ * than ppp, or if it detects a modem hangup.  The former will happen
+ * in disestablish_ppp if the latter hasn't already happened, so we
+ * shouldn't need to do anything.
+ *
+ * Just to be sure, set the real serial port to the normal discipline.
  */
 
 void
 restore_loop(void)
   {
-    int  x;
-    int  fdflags;
-    char fname [30];
-/*
- * Take down the existing interface
- */
-    sifdown (0);
-    (void) ioctl(ppp_fd, TIOCSETD, &tty_disc);
-/*
- * Find the existing flags. This works even if the tty has stolen the
- * line discipline.
- */
-    fdflags = fcntl(ppp_fd, F_GETFL);
-    if (fdflags < 0)
-      {
-        syslog (LOG_ERR, "retrieve file flags failed: %m(%d)", errno);
-       fdflags = O_NONBLOCK | O_RDWR;
-      }
-/*
- * Re-open the file so the we can re-establish the previous discipline
- */
-    sprintf (fname, "/proc/self/fd/%d", ppp_fd);
-    x = open (fname, O_RDWR | O_NONBLOCK, 0);
-    if (x < 0)
+    if (ppp_fd != slave_fd)
       {
-        syslog (LOG_ERR, "reopen of tty file failed: %m(%d)", errno);
+       (void) ioctl(ppp_fd, TIOCSETD, &tty_disc);
+       set_ppp_fd(slave_fd);
       }
-/*
- * Transfer the newly opened file (to the same tty) back to the tty
- * file handle.
- */
-    else
-      {
-       dup2 (x, ppp_fd);
-       close (x);
-       fcntl (ppp_fd, F_SETFL, fdflags);
-       set_up_tty(ppp_fd, 0);
-      }
-/*
- * Switch to the tty slave and put that into the PPP discipline.
- */
-    set_ppp_fd(slave_fd);
-
-    if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
-      {
-       syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
-       die(1);
-      }
-/*
- * Fetch the current unit identifier.
- */
-    if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
-      {        
-       syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
-       die(1);
-      }
-/*
- * Restore the parameters for the PPP link.
- */
-    ppp_send_config(0, PPP_MRU, (u_int32_t) 0, 0, 0);
-    ppp_recv_config(0, PPP_MRU, (u_int32_t) 0, 0, 0);
-/*
- * Reconfigure the IP addresses for the demand dial system.
- */
-    (void) (ipcp_protent.demand_conf) (0);
   }
 
 /********************************************************************
@@ -2395,6 +2394,7 @@ sifnpmode(u, proto, mode)
          {
            syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE, %d, %d): %m(%d)",
                   proto, mode, errno);
+           syslog(LOG_ERR, "ppp_fd=%d slave_fd=%d\n", ppp_fd, slave_fd);
          }
        return 0;
       }
@@ -2411,13 +2411,14 @@ sifnpmode(u, proto, mode)
 
 int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
   {
-    int    skfd; 
     int    result = 1;
+
+#ifdef IPX_CHANGE
+    int    skfd; 
     struct sockaddr_ipx  ipx_addr;
     struct ifreq         ifr;
     struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
 
-#ifdef IPX_CHANGE
     skfd = socket (AF_IPX, SOCK_DGRAM, 0);
     if (skfd < 0)
       { 
@@ -2473,13 +2474,14 @@ int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
 
 int cipxfaddr (int unit)
   {
-    int    skfd; 
     int    result = 1;
+
+#ifdef IPX_CHANGE
+    int    skfd; 
     struct sockaddr_ipx  ipx_addr;
     struct ifreq         ifr;
     struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
 
-#ifdef IPX_CHANGE
     skfd = socket (AF_IPX, SOCK_DGRAM, 0);
     if (skfd < 0)
       {