treewide: change /sbin/sh hashbangs to /bin/sh
[unleashed-userland.git] / components / network / tor / files / tor.sh
blob98ab741a798f335e0276b2dc6a76ad07977a5a72
1 #!/bin/sh
3 # CDDL HEADER START
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
20 # CDDL HEADER END
22 # Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 # Use is subject to license terms.
25 #ident "@(#)tor.sh 1.1 09/05/14 SMI"
28 . /lib/svc/share/smf_include.sh
30 # SMF_FMRI is the name of the target service. This allows multiple instances
31 # to use the same script.
33 if [ -z "$SMF_FMRI" ]; then
34 echo "SMF framework variables are not initialized."
35 exit $SMF_EXIT_ERR
38 tor_start() {
39 # Raise the number of file descriptors
40 /usr/bin/ulimit -n 1024 2>&1 > /dev/null
41 /usr/lib/tor 2>&1 > /dev/null
44 tor_stop() {
45 /usr/bin/pkill -x tor 2>&1 > /dev/null
49 # Parse the various "*Port" and "*ListenAddress" parameters
50 # from the Tor config file (/etc/torrc) and create IPF rules.
52 find_ports()
54 port=$1
55 addr=$2
57 # If this is non-zero, it is in use.
58 p=`grep "^$port" /etc/torrc 2>/dev/null | \
59 awk '{print $2}'`
61 if [ ! -z "$p" ]; then
62 echo "# Tor $port rules" >> ipf_file
65 for i in $p; do
66 generate_rules $FMRI $policy "tcp" "any" $i $ipf_file
67 done
69 # Alternate *Address parameter may specify another port
70 a=`grep "^$addr" /etc/torrc 2>/dev/null | \
71 awk '{print $2}'`
73 if [ ! -z "$a" ]; then
74 echo "# Tor $addr rules" >> $ipf_file
77 for i in $a; do
78 p=`echo "$i" | sed -e 's/.*://'`
79 ip=`echo "$i" | sed -e 's/:.*//'`
81 # We don't need to add 0.0.0.0 IP
83 if [ "$ip" = "0.0.0.0" ]; then
84 ip=''
86 if [ ! -z "$p" ]; then
87 generate_rules $FMRI $policy "tcp" "any" $ip $p $ipf_file
89 done
90 return $?
93 create_ipf_rules()
95 FMRI=$1
98 # If Tor is not configured, there are no rules to add
100 if [ ! -f /etc/torrc ]; then
101 return;
104 ipf_file=`fmri_to_file ${FMRI} $IPF_SUFFIX`
105 policy=`get_policy ${FMRI}`
107 echo "# $FMRI" >$ipf_file
110 # Tor may be configured to listen on a specific Port or
111 # it may be configured to a specific interface:port combination
112 # so we look for both parameters for the various Tor ports in the
113 # config file.
115 find_ports SocksPort SocksListenAddress
117 find_ports ORPort ORListenAddress
119 find_ports DirPort DirListenAddress
121 find_ports HiddenServicePort __no_addr_param__
123 find_ports ControlPort __no_addr_param__
126 case "$1" in
127 'start')
128 tor_start
131 'stop')
132 tor_stop
135 'ipfilter')
136 create_ipf_rules $2
140 echo "Usage: $0 {start|stop|ipfilter}"
141 exit 1
144 esac
145 exit $SMF_EXIT_OK