Shorewall restart on PPP link change

I’ve found great joy running a Shorewall firewall on a Linux box but I came across the problem that when the PPP interface for an ADSL, WiMAX or VPN link goes up or down, Shorewall needs to be restarted to take the new IP address assignments into account. To this solve this problem I’ve written a few scripts to make it all work nicely for South African users.

Firstly put the following two scripts in the /usr/bin directory (or any directory of your choice):

/usr/bin/shorewall-flag-restart.sh (chmod u+x):

#!/bin/bash
set –e
set –u
RESTART_NEEDED=/var/lib/shorewall/shorewall-restartneeded
set -o noclobber
if [ ! -r $RESTART_NEEDED ]; then
    date > $RESTART_NEEDED 2>&1
fi

/usr/bin/shorewall-check-restart.sh (chmod u+x):

#!/bin/bash

set -e
set -u

RESTART_NEEDED=/var/lib/shorewall/shorewall-restartneeded
RESTARTING=/var/lib/shorewall/shorewall-restarting
RESTARTED=/var/lib/shorewall/shorewall-restarted

# The restart needed flag is put in place by the ip up/down scripts.  If
# it doesn’t exist or is older than the shorewall restart flag file, we
# don’t need to do anything.
if [ ! -r $RESTART_NEEDED ]; then
    exit 0
fi
if [ -r $RESTARTING ]; then
    exit 0
fi
if [ $RESTARTED -nt $RESTART_NEEDED ]; then
    rm -f $RESTART_NEEDED
    exit 0
fi

# Make a mutex – should exit the script if this file already exists,
# due to the combination of the set -e and noclobber options.
set -o noclobber
echo "$$: `date`" >$RESTARTING

## We only remove the $RESTART_NEEDED if the restart succeeds.
#if /sbin/shorewall restart >/dev/null 2>&1; then
#    rm -f $RESTARTING
#    rm -f $RESTART_NEEDED
#else
#    rm -f $RESTARTING
#fi

# Remove the $RESTART_NEEDED and replace if failure.
rm -f $RESTART_NEEDED
if /sbin/shorewall restart >/dev/null 2>&1; then
    rm -f $RESTARTING
else
    rm -f $RESTARTING
    if [ ! -r $RESTARTING ]; then
        echo "$$: `date`" >$RESTART_NEEDED
    fi
fi

Then symbolic link the shorewall-flag-restart.sh script in the /etc/ppp/ip-up.d/ and /etc/ppp-ip-down.d/ directories so that the firewall gets restarted when a PPP interface goes up or down:

ln -s /usr/bin/shorewall-flag-restart.sh /etc/ppp/ip-up.d/shorewall-flag-restart
ln -s /usr/bin/shorewall-flag-restart.sh /etc/ppp/ip-down.d/shorewall-flag-restart

Then schedule cron to check for the restart flag every minute:

/etc/cron.d/shorewall-restart:

MAILTO=root
*/1 * * * * root  [ -x /usr/bin/shorewall-check-restart.sh ] && /usr/bin/shorewall-check-restart.sh >/dev/null

These scripts were developed and tested on a Debian system. If anyone has any improvements or recommendations I’d appreciate to hear from you.

How to get Windows Live Installer to work behind a firewall and proxy

If you are like me and obsessed about security, then you might find that your PC is sitting behind a firewall with no direct outbound web access. That is other than through an proxy server of some sorts like the Linux based squid.

What happens is the Window Live Installer (WLInstaller.exe) searches your computer for installed Windows Live programs and then fails to check for and download the new versions. Why? Because it ignores the default proxy settings you’ve configured under Internet Explorer. To solve it simply execute proxycfg -u to import proxy settings from current user’s Microsoft Internet Explorer manual settings and try the installer again.

[tags]Windows Live Installer,Windows Live,howto[/tags]

How to switch Silverlight UserControls

One of the guys on the official Silverlight community forums posted an interesting question, namely how does one change the currently visible UserControl in a Silverlight 2.0 application. To put things in context the new Silverlight 2.0 project template creates two classes for you: a System.Windows.Application derived class named App with the associated App.xaml and App.xaml.cs files; as well as a System.Windows.Controls.UserControl derived class called Page with the associated Page.xaml and Page.xaml.cs files.

The App class takes care of the initialization and basically you assign a new instance of your Page class to the App.RootVisual property and through the power of Silverlight it appears on your screen. 🙂

So if you wanted to change the currently displayed UserControl for another instance naturally you’d try and set the App.RootVisual property to another instance of a UserControl. This sounds great in theory but it seems that its a single assignment property. Pete Brown’s blog post happens to make mention of this.

So in a quest to solve the problem I’ve implemented a very simple UserControlContainer class that allows the developer to switch controls at runtime without having to deal with the set once RootVisual property.

Simply create a new UserControl called UserControlContainer and copy the following into the UserControlContainer.xaml.cs file:

    public void SwitchControl(UserControl newControl)
    {
        LayoutRoot.Children.Clear();
        if (newControl != null)
        {
            Height = newControl.Height;
            Width = newControl.Width;
            LayoutRoot.Children.Add(newControl);
        }
    }

Next set your Application instance RootVisual to an instance of the new UserControlContainer in the Application_Startup method.

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        // Load the main control
        this.RootVisual = new UserControlContainer();
        Page page = new Page();
        ((UserControlContainer)this.RootVisual).SwitchControl(page);
    }

Then wherever you need to switch out the active control, simply call the following if you are inside App.xaml.cs:

    TestControl1 testControl = new TestControl1();
    ((UserControlContainer)this.RootVisual).SwitchControl(testControl);

Or the following if you need to switch controls from elsewhere:

    TestControl1 testControl = new TestControl1();
    ((UserControlContainer)Application.Current.RootVisual).SwitchControl(testControl);

If you find this useful or find any issues with this method, let me know so I can improve it for all.

 

Updated 2008-03-13: Added Application_Startup example. Thanks to Maurice for pointing this omission out.

[tags]Silverlight 2.0,code,example[/tags]