#!/usr/bin/perl # # Copyright (c) 2011 Mathieu Roy # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA # # The original idea was to come up with a script that mounts NFS shares # when on the relevant network. # # NetworkManager crappy handling of /etc/dhcp/dhclient-*-hooks makes it # impossible to rely on these hooks like in the past, to keep the stuff # portable. # # In /etc/network/if-up.d/, in Debian, there's already a mountnfs script # shipped by the initscripts package. # Instead of reinventing this, we'll just remove "noauto" option from NFS # shares listed in /etc/fstab for the current network. use strict; use Socket; my $debug = 0; ## ## Quickly check whether there are NFS shares listed in fstab that we may ## want to work with: must contain nfs as filesystem and noauto option. ## open(FSTAB, "< /etc/fstab"); my $count = 0; my %hosts; while () { next if /^#/; next unless /\w/; next unless /\snfs/; next unless /noauto/; $count++; # identify the NFS host my $this_host = $1 if /^([^\:]*)\:/; $hosts{$this_host} = 1 unless $hosts{$this_host}; } close(FSTAB); print "DBG: $count noauto NFS share(s) listed in /etc/fstab\n" if $debug; exit if $count < 1; ## ## Check whether the NFS servers mentioned resolves with a local network IP ## (I guess this script could be used to mount NFS shares over internet, ## but for security issues, I would not do such thing automatically, so I ## bother about it) ## 192.168. or 10. $count = 0; while (my ($this_host,) = each(%hosts)) { my $packed_ip = gethostbyname($this_host); next unless defined $packed_ip, my $ip = inet_ntoa($packed_ip); next unless $ip =~ /^192\.168\./ or $ip =~ /^10\./; print "DBG: $this_host DNS record exists and points to LAN $ip\n" if $debug; # now set this host value to 2 as valid host $hosts{$this_host} = 2; $count++; } print "DBG: $count host(s) listed in /etc/fstab available\n" if $debug; exit if $count < 1; ## ## Now edit /etc/fstab so available NFS shares no longer get the noauto ## option. ## Mark the original lines so changes are easily reverted back. ### use File::Copy; use File::Temp qw(tempfile tempdir); $count = 0; # Create a (secure) temporary file my ($tempfilefh, $tempfile) = tempfile(UNLINK => 1); open(FSTAB, "< /etc/fstab"); while () { # copy current fstab while removing noauto for each host while (my ($this_host,$is_active) = each(%hosts)) { next unless $is_active eq 2; if (/^$this_host\:/) { # back up the change print $tempfilefh "#CLEVERNFSS###$_" if /\,?noauto/; # squish the noauto $count++ if s/\,?noauto//g; } } print $tempfilefh $_; } close(FSTAB); close($tempfilefh); print "DBG: $count line(s) modified in /etc/fstab\n" if $debug; exit if $count < 1; # replace fstab with the (readable to everybody) temporary file # (obviously, must be root to do so) system("chmod", "a+r", $tempfile); move($tempfile, "/etc/fstab") or die "unable to overwrite /etc/fstab, are you root? Exit"; print "DBG: /etc/fstab overwrote by $tempfile\n" if $debug; # Now /etc/network/if-up.d/mountnfs should do its magic # EOF