- Write pid file if filename is set
authorfred_nerk <fred_nerk>
Mon, 2 Aug 2004 03:38:01 +0000 (03:38 +0000)
committerfred_nerk <fred_nerk>
Mon, 2 Aug 2004 03:38:01 +0000 (03:38 +0000)
- Add startup script and monitor script from Yuri
- Some logging correctness fixes from Iain Wade
- Combined LCP patches from Iain and Yuri. This should allow Windows 2k/XP
  clients to connect, as well Linksys DSL modems.

Changes
Docs/manual.html
l2tpns.c
l2tpns.h
ppp.c
scripts/l2tpns-monitor [new file with mode: 0644]
scripts/l2tpns.script [new file with mode: 0644]

diff --git a/Changes b/Changes
index f1e9da9..95bcd81 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,8 +1,14 @@
 * ??? 2.0.2
+- Combined LCP patches from Iain and Yuri. This should allow Windows 2k/XP
+  clients to connect, as well Linksys DSL modems.
 - Apply patch to fix -v option from Juergen Kammer.
 - Makefile fix from Juergen Kammer to not overwrite existing config files on
   make install
 - Configurable radius port patch from Juergen Kammer.
+- Send my_address if no bind_address when doing IPCP
+- Write pid file if filename is set
+- Add startup script and monitor script from Yuri
+- Some logging correctness fixes from Iain Wade
 
 * Tue Jul 13 2004 Brendan O'Dea <bod@optusnet.com.au> 2.0.1
 - Update INSTALL, Docs/manual.html documentation.
index f263f2e..1ac5d15 100644 (file)
@@ -173,6 +173,12 @@ is any one of the syslog logging facilities, such as local5.
 <P>
 </LI>
 
+<LI><B>pid_file</B> (string)<BR>
+If this is set, the process id will be written to this file. The filename must
+contain an absolute path.
+<P>
+</LI>
+
 <LI><B>l2tp_secret</B> (string)<BR>
 This sets the string that l2tpns will use for authenticating tunnel request.
 This must be the same as the LAC, or authentication will fail.  This will
index 1abe8ac..f27c7c2 100644 (file)
--- a/l2tpns.c
+++ b/l2tpns.c
@@ -4,7 +4,7 @@
 // Copyright (c) 2002 FireBrick (Andrews & Arnold Ltd / Watchfront Ltd) - GPL licenced
 // vim: sw=8 ts=8
 
-char const *cvs_id_l2tpns = "$Id: l2tpns.c,v 1.18 2004/07/28 06:12:30 fred_nerk Exp $";
+char const *cvs_id_l2tpns = "$Id: l2tpns.c,v 1.19 2004/08/02 03:38:01 fred_nerk Exp $";
 
 #include <arpa/inet.h>
 #include <assert.h>
@@ -94,6 +94,7 @@ int bgp_configured = 0;
 struct config_descriptt config_values[] = {
        CONFIG("debug", debug, INT),
        CONFIG("log_file", log_filename, STRING),
+       CONFIG("pid_file", pid_file, STRING),
        CONFIG("l2tp_secret", l2tpsecret, STRING),
        CONFIG("primary_dns", default_dns1, IP),
        CONFIG("secondary_dns", default_dns2, IP),
@@ -2995,6 +2996,10 @@ int main(int argc, char *argv[])
        /* remove plugins (so cleanup code gets run) */
        plugins_done();
 
+       // Remove the PID file if we wrote it
+       if (config->wrote_pid && *config->pid_file == '/')
+               unlink(config->pid_file);
+
        /* kill CLI children */
        signal(SIGTERM, SIG_IGN);
        kill(0, SIGTERM);
@@ -3401,6 +3406,22 @@ void update_config()
                timeout = config->cluster_hb_timeout;
        }
 
+       // Write PID file
+       if (*config->pid_file == '/' && !config->wrote_pid)
+       {
+               FILE *f;
+               if ((f = fopen(config->pid_file, "w")))
+               {
+                       fprintf(f, "%d\n", getpid());
+                       fclose(f);
+                       config->wrote_pid = 1;
+               }
+               else
+               {
+                       log(0, 0, 0, 0, "Can't write to PID file %s: %s\n", config->pid_file, strerror(errno));
+               }
+       }
+
        config->reload_config = 0;
 }
 
index 43187bf..3cae8de 100644 (file)
--- a/l2tpns.h
+++ b/l2tpns.h
@@ -1,5 +1,5 @@
 // L2TPNS Global Stuff
-// $Id: l2tpns.h,v 1.13 2004/07/26 00:20:41 fred_nerk Exp $
+// $Id: l2tpns.h,v 1.14 2004/08/02 03:38:01 fred_nerk Exp $
 
 #ifndef __L2TPNS_H__
 #define __L2TPNS_H__
@@ -119,7 +119,7 @@ struct cli_session_actions {
 #define CLI_SESS_NOTHROTTLE    0x10
 
 struct cli_tunnel_actions {
-       char action;
+       char action;
 };
 
 #define CLI_TUN_KILL           0x01
@@ -195,6 +195,7 @@ typedef struct {
 
 #define        SESSIONPFC      1            // PFC negotiated flags
 #define        SESSIONACFC     2           // ACFC negotiated flags
+#define SESSIONLCPACK  4       // LCP negotiated
 
 // 168 bytes per tunnel
 typedef struct tunnels
@@ -379,6 +380,8 @@ struct configt
        int             debug;                          // debugging level
        time_t          start_time;                     // time when l2tpns was started
        char            bandwidth[256];                 // current bandwidth
+       char            pid_file[256];                  // file to write PID to on startup
+       int             wrote_pid;
        clockt          current_time;                   // 1/10ths of a second since the process started.
                                                        // means that we can only run a given process
                                                        // for 13 years without re-starting!
diff --git a/ppp.c b/ppp.c
index 202f650..3287f3d 100644 (file)
--- a/ppp.c
+++ b/ppp.c
@@ -1,6 +1,6 @@
 // L2TPNS PPP Stuff
 
-char const *cvs_id_ppp = "$Id: ppp.c,v 1.8 2004/07/11 07:57:35 bodea Exp $";
+char const *cvs_id_ppp = "$Id: ppp.c,v 1.9 2004/08/02 03:38:01 fred_nerk Exp $";
 
 #include <stdio.h>
 #include <string.h>
@@ -230,9 +230,9 @@ void dumplcp(u8 *p, int l)
        {
                int type = o[0];
                int length = o[1];
-               if (length == 0)
+               if (length < 2)
                {
-                       log(4, 0, 0, 0, "       Option length is 0...\n");
+                       log(4, 0, 0, 0, "       Option length is %d...\n", length);
                        break;
                }
                if (type == 0)
@@ -245,13 +245,22 @@ void dumplcp(u8 *p, int l)
                switch (type)
                {
                        case 1: // Maximum-Receive-Unit
+                               if (length == 4)
                                log(4, 0, 0, 0, "    %s %d\n", lcp_types[type], ntohs(*(u16 *)(o + 2)));
+                               else
+                                       log(4, 0, 0, 0, "    %s odd length %d\n", lcp_types[type], length);
                                break;
                        case 3: // Authentication-Protocol
                                {
+                                       if (length == 4)
+                                       {
                                        int proto = ntohs(*(u16 *)(o + 2));
-                                       log(4, 0, 0, 0, "    %s %s\n", lcp_types[type],
-                                                       proto == 0xC223 ? "CHAP" : "PAP");
+                                               log(4, 0, 0, 0, "   %s 0x%x (%s)\n", lcp_types[type], proto,
+                                                       proto == 0xC223 ? "CHAP" :
+                                                       proto == 0xC023 ? "PAP"  : "UNKNOWN");
+                                       }
+                                       else
+                                               log(4, 0, 0, 0, "   %s odd length %d\n", lcp_types[type], length);
                                        break;
                                }
                        case 4: // Quality-Protocol
@@ -262,20 +271,23 @@ void dumplcp(u8 *p, int l)
                                }
                        case 5: // Magic-Number
                                {
+                                       if (length == 6)
+                                       {
                                        u32 magicno = ntohl(*(u32 *)(o + 2));
                                        log(4, 0, 0, 0, "    %s %x\n", lcp_types[type], magicno);
+                                       }
+                                       else
+                                               log(4, 0, 0, 0, "   %s odd length %d\n", lcp_types[type], length);
                                        break;
                                }
                        case 7: // Protocol-Field-Compression
                                {
-                                       u32 pfc = ntohl(*(u32 *)(o + 2));
-                                       log(4, 0, 0, 0, "    %s %x\n", lcp_types[type], pfc);
+                                       log(4, 0, 0, 0, "    %s\n", lcp_types[type]);
                                        break;
                                }
                        case 8: // Address-And-Control-Field-Compression
                                {
-                                       u32 afc = ntohl(*(u32 *)(o + 2));
-                                       log(4, 0, 0, 0, "    %s %x\n", lcp_types[type], afc);
+                                       log(4, 0, 0, 0, "    %s\n", lcp_types[type]);
                                        break;
                                }
                        default:
@@ -306,6 +318,7 @@ void processlcp(tunnelidt t, sessionidt s, u8 * p, u16 l)
        if (*p == ConfigAck)
        {
                log(3, session[s].ip, s, t, "LCP: Discarding ConfigAck\n");
+               session[s].flags |= SESSIONLCPACK;
        }
        else if (*p == ConfigReq)
        {
@@ -392,15 +405,10 @@ void processlcp(tunnelidt t, sessionidt s, u8 * p, u16 l)
                        // Already built a ConfigNak... send it
                        log(3, session[s].ip, s, t, "Sending ConfigNak\n");
                        tunnelsend(b, l + (q - b), t);
-
-                       log(3, session[s].ip, s, t, "Sending ConfigReq, requesting PAP login\n");
-                       q = makeppp(b, sizeof(b), NULL, 0, t, s, PPPLCP);
-                       *q++ = ConfigReq;
-                       *(u8 *)(q++) = 3;
-                       *(u8 *)(q++) = 4;
-                       *(u16 *)(q += 2) = htons(0xC023);
-                       tunnelsend(b, l + (q - b), t);
                }
+
+               if (!(session[s].flags & SESSIONLCPACK))
+                       initlcp(t, s);
        }
        else if (*p == ConfigNak)
        {
diff --git a/scripts/l2tpns-monitor b/scripts/l2tpns-monitor
new file mode 100644 (file)
index 0000000..d17695e
--- /dev/null
@@ -0,0 +1,28 @@
+#!/bin/sh
+stopfile=/tmp/l2tpns.stop
+first=`date +%s`
+min_first_time=3
+restart_delay=5
+prog=${0##*/}
+
+while :
+do
+       echo "`date`: Starting l2tpns $@"
+       start=`date +%s`
+       /usr/sbin/l2tpns ${1+"$@"}
+       RETVAL=$?
+       stop=`date +%s`
+       t=$(($stop - $start));
+       first=$(($stop - $first));
+       echo "`date`: l2tpns exited after $t seconds, status $RETVAL"
+       if [ $first -lt $min_first_time ]; then
+               echo "`date`: l2tpns exited immediately, $prog exiting"
+               exit $RETVAL
+       fi
+       if [ -f $stopfile ]; then
+               ls -l $stopfile
+               echo "`date`: stop file found, $prog exiting"
+               exit
+       fi
+       sleep $restart_delay
+done >>/var/log/$prog 2>&1     &       # execute in background
diff --git a/scripts/l2tpns.script b/scripts/l2tpns.script
new file mode 100644 (file)
index 0000000..ba4cdf4
--- /dev/null
@@ -0,0 +1,93 @@
+#!/bin/bash
+#
+# Startup script for l2tpns
+#
+# chkconfig: 2345 83 25
+# description: l2tpns.
+# processname: l2tpns
+# pidfile: /var/run/l2tpns.pid
+# config: /etc/l2tpns
+
+# Source function library.
+. /etc/rc.d/init.d/functions
+
+if [ -f /etc/sysconfig/lt2pns ]; then
+        . /etc/sysconfig/lt2pns
+fi
+
+# Path to the l2tpns-monitor script, server binary, and short-form for messages.
+l2tpns_monitor=/usr/sbin/l2tpns-monitor
+l2tpns=/usr/sbin/l2tpns
+prog=${l2tpns##*/}
+RETVAL=0
+
+start() {
+        echo -n $"Starting $prog: "
+       rm -f /tmp/l2tpns.stop
+        daemon --check=$prog $l2tpns_monitor $OPTIONS
+        RETVAL=$?
+       echo
+       sleep 5
+       pid=`pidofproc $l2tpns_monitor`
+       if [ -z "$pid" ] || [ "$pid" -eq 0 ]; then
+               echo -n "Error starting $prog"
+               echo_failure
+               echo
+               return 99
+       fi
+        [ $RETVAL = 0 ] && touch /var/lock/subsys/l2tpns
+        return $RETVAL
+}
+stop() {
+       echo -n $"Stopping $prog: "
+       echo >/tmp/l2tpns.stop
+       killproc $l2tpns
+       RETVAL=$?
+       echo
+       [ $RETVAL = 0 ] && rm -f /var/lock/subsys/l2tpns /var/run/l2tpns.pid
+}
+reload() {
+       echo -n $"Reloading $prog: "
+       killproc $l2tpns -HUP
+       RETVAL=$?
+       echo
+}
+
+# See how we were called.
+case "$1" in
+  start)
+       start
+       ;;
+  stop)
+       stop
+       ;;
+  status)
+        status $l2tpns
+       RETVAL=$?
+       ;;
+  restart)
+       stop
+       sleep 5
+       start
+       ;;
+  condrestart)
+       if [ -f /var/run/l2tpns.pid ] ; then
+               stop
+               start
+       fi
+       ;;
+  reload)
+        reload
+       ;;
+  coldrestart)
+       stop
+       sleep 10
+       rm -f /tmp/l2tpns.dump
+       start
+       ;;
+  *)
+       echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|coldrestart}"
+       exit 1
+esac
+
+exit $RETVAL