#!/bin/bash
#
# wshare v1.0 - gui agnostic sharing of your network connections
#
# Copyright (C) 2008 Stefan Marsiske
#
# 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 3 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, see .
# default config
#------------------
### Common settings
# set internal network, over which the external is shared
# try to automatically select the first wireless device from /proc/net/wireless
innet=$(head -3 /proc/net/wireless 2>/dev/null | tail -1 | cut -f1 -d: | tr -d " ")
# if automatic detection fails, use ath0 as default
innet=${innet:-ath0}
# set the essid of the shared network master
essid=wshared
#------------------
### Master settings
# specify the subnet of the internal network
# this must be in the /24 range currently...
# so only specify networks with 3 of four octets
subnet=10.17.17
# specify which network to share, this is the device that connects to the
# internet
exnet=ppp0
# specify Master IP address
ownip=10.17.17.1
#------------------
# none of the files below might exist, so we route any error messages to our
# logging daemon /dev/null
# global config
[[ -r /etc/wshare.conf ]] && . /etc/wshare.conf 2>/dev/null
# personal config
[[ -r $HOME/.wshare.conf ]] && . $HOME/.wshare.conf 2>/dev/null
# testing config, same dir as script, shmagic.
[[ -r ${0%%wshare}wshare.conf ]] && . ${0%%wshare}wshare.conf 2>/dev/null
# default behaviour
client=true
usage="${0##*/} [-m] \
[-e ] \
[-n subnet <10.17.17>] \
[-o ] \
[-i ] \
[-a ] \
[-k ]"
# only root can do this
[[ $(id -u) -ne 0 ]] && {
echo $usage
echo "error: please run ${0##*/} as root"
exit 1;
}
while getopts ?m:e:n:o:i:a: opt; do
case $opt in
m) client=false;;
e) essid=$OPTARG;;
n) subnet=$OPTARG;;
o) exnet=$OPTARG;;
i) innet=$OPTARG;;
a) ownip=$OPTARG;;
k) key=$OPTARG;;
?|h) echo $usage; exit;;
esac
done
shift `expr $OPTIND - 1`
# switch MODE
echo switching ${innet} to mode: ad-hoc
if [[ ${innet} == "ath0" ]]; then
wlanconfig ${innet} destroy || exit 1
# madwifi calls the mode adhoc, not ad-hoc...
wlanconfig ath0 create wlandev wifi0 wlanmode adhoc
else
iwconfig ${innet} mode ad-hoc || exit 1
fi
# set ESSID
# TBA clients in ad-hoc and essid? necessary?
echo setting essid: ${essid}
iwconfig ${innet} essid ${essid} || exit 1
# set encryption KEY
if [[ -n "${key}" ]]; then
echo setting encryption key: ${key}
iwconfig ${innet} key ${key} || exit 1
fi
if $client; then
ifconfig ${innet} up || exit 1
dhclient ${innet} || exit 1
else
# start shared network
echo starting ${innet} @ ${ownip}
ifconfig ${innet} up ${ownip} || exit 1
# NET shared network to external network
echo enabling nat of ${innet} to ${exnet}
iptables --table nat --flush || exit 1
# Delete all chains that are not in default filter and nat table
iptables --table nat --delete-chain || exit 1
# Set up IP FORWARDing and Masquerading
iptables --table nat --append POSTROUTING --out-interface ${exnet} -j MASQUERADE || exit 1
iptables --append FORWARD --in-interface ${innet} -j ACCEPT || exit 1
# Enables packet forwarding by kernel
echo 1 > /proc/sys/net/ipv4/ip_forward || exit 1
# set up DHCP server for wshared
echo setting up dhcp server for ${subnet}.0 @ ${innet}
dhcpcfg=/tmp/dhcp-wshare-$$-${RANDOM}
cat >${dhcpcfg} <