Allow cli session changes to happen if the session is timing out
[l2tpns.git] / l2tpns.c
index f4ffde9..95f1df4 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.14 2004/07/12 08:21:45 bodea Exp $";
+char const *cvs_id_l2tpns = "$Id: l2tpns.c,v 1.21 2004/08/02 06:06:28 fred_nerk Exp $";
 
 #include <arpa/inet.h>
 #include <assert.h>
@@ -94,12 +94,15 @@ 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),
        CONFIG("save_state", save_state, BOOL),
        CONFIG("primary_radius", radiusserver[0], IP),
        CONFIG("secondary_radius", radiusserver[1], IP),
+       CONFIG("primary_radius_port",radiusport[0], SHORT),
+       CONFIG("secondary_radius_port",radiusport[1], SHORT),
        CONFIG("radius_accounting", radius_accounting, BOOL),
        CONFIG("radius_secret", radiussecret, STRING),
        CONFIG("bind_address", bind_address, IP),
@@ -1112,7 +1115,7 @@ void sendipcp(tunnelidt t, sessionidt s)
        *(u16 *) (q + 2) = htons(10);
        q[4] = 3;
        q[5] = 6;
-       *(u32 *) (q + 6) = config->bind_address; // send my IP
+       *(u32 *) (q + 6) = config->bind_address ? config->bind_address : my_address; // send my IP
        tunnelsend(buf, 10 + (q - buf), t); // send it
        session[s].flags &= ~SF_IPCP_ACKED;     // Clear flag.
 }
@@ -1557,6 +1560,8 @@ void processudp(u8 * buf, int len, struct sockaddr_in *addr)
                                        memcpy(calling, b, (n >= MAXTEL) ? (MAXTEL-1) : n);
                                        log(4, ntohl(addr->sin_addr.s_addr), s, t, "   Calling <%s>\n", calling);
                                        break;
+                               case 23:    // subtype
+                                       break;
                                case 24:    // tx connect speed
                                        if (n == 4)
                                        {
@@ -2030,7 +2035,7 @@ int regular_cleanups(void)
                if (!session[s].tunnel) // Session isn't in use
                        continue;
 
-               if (!session[s].die && session[s].ip && !(session[s].flags & SF_IPCP_ACKED) )
+               if (!session[s].die && session[s].ip && !(session[s].flags & SF_IPCP_ACKED))
                {
                        // IPCP has not completed yet. Resend
                        log(3, session[s].ip, s, session[s].tunnel, "No ACK for initial IPCP ConfigReq... resending\n");
@@ -2074,7 +2079,6 @@ int regular_cleanups(void)
                                        (int)(time_now - session[s].last_packet));
                        tunnelsend(b, 24, session[s].tunnel); // send it
                        if (++count >= MAX_ACTIONS) break;
-                       continue;
                }
 
                // Check for actions requested from the CLI
@@ -2126,7 +2130,6 @@ int regular_cleanups(void)
                                cluster_send_session(s);
 
                        if (++count >= MAX_ACTIONS) break;
-                       continue;
                }
        }
 
@@ -2837,6 +2840,7 @@ void dump_acct_info()
 int main(int argc, char *argv[])
 {
        int o;
+       int optdebug = 0;
 
        _program_name = strdup(argv[0]);
 
@@ -2853,7 +2857,7 @@ int main(int argc, char *argv[])
                                if (fork()) exit(0);
                                break;
                        case 'v':
-                               config->debug++;
+                               optdebug++;
                                break;
                        case 'h':
                                snprintf(hostname, sizeof(hostname), "%s", optarg);
@@ -2881,6 +2885,9 @@ int main(int argc, char *argv[])
        initiptables();
        initplugins();
        initdata();
+
+       config->debug = optdebug;
+
        init_tbf();
        init_cli(hostname);
        read_config_file();
@@ -2987,6 +2994,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);
@@ -3321,7 +3332,21 @@ void update_config()
        // Update radius
        config->numradiusservers = 0;
        for (i = 0; i < MAXRADSERVER; i++)
-               if (config->radiusserver[i]) config->numradiusservers++;
+               if (config->radiusserver[i])
+               {
+                       config->numradiusservers++;
+                       // Set radius port: if not set, take the port from the
+                       // first radius server.  For the first radius server,
+                       // take the #defined default value from l2tpns.h
+
+                       // test twice, In case someone works with
+                       // a secondary radius server without defining
+                       // a primary one, this will work even then.
+                       if (i>0 && !config->radiusport[i])
+                               config->radiusport[i] = config->radiusport[i-1];
+                       if (!config->radiusport[i])
+                               config->radiusport[i] = RADPORT;
+               }
 
        if (!config->numradiusservers)
        {
@@ -3379,6 +3404,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;
 }
 
@@ -3454,7 +3495,9 @@ int sessionsetup(tunnelidt t, sessionidt s)
        for (r = 0; r < MAXROUTE && session[s].route[r].ip; r++)
                routeset(s, session[s].route[r].ip, session[s].route[r].mask, session[s].ip, 1);
 
-       if (!session[s].sid) {  // did this session just finish radius?
+       if (!session[s].sid)
+       {
+               // did this session just finish radius?
                log(3, session[s].ip, s, t, "Sending initial IPCP to client\n");
                sendipcp(t, s);
                session[s].sid = ++last_sid;