| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# $FreeBSD$ |
| 4 |
# |
| 5 |
# PROVIDE: tor |
| 6 |
# REQUIRE: DAEMON FILESYSTEMS |
| 7 |
# BEFORE: LOGIN |
| 8 |
# |
| 9 |
# Add the following lines to /etc/rc.conf to enable tor. |
| 10 |
# All these options will overide any settings in your local torrc as |
| 11 |
# they are command line options. |
| 12 |
# |
| 13 |
# tor_enable (bool): Set it to "YES" to enable tor. Default: NO |
| 14 |
# tor_instances (str): List of instances. Default: "" |
| 15 |
# tor_conf (str): Points to your torrc file. |
| 16 |
# Default: %%PREFIX%%/etc/tor/torrc |
| 17 |
# tor_user (str): Tor daemon user. Default: %%USER%% |
| 18 |
# tor_group (str): Tor group. Default: %%GROUP%% |
| 19 |
# tor_pidfile (str): Tor pid file. Default: /var/run/tor/tor.pid |
| 20 |
# tor_datadir (str): Tor datadir. Default: /var/db/tor |
| 21 |
# tor_disable_default_instance (str): Doesn't run the default instance. |
| 22 |
# Only valid when tor_instances is used. |
| 23 |
# Default: NO |
| 24 |
# tor_setuid (str): Runtime setuid. Default: NO |
| 25 |
# |
| 26 |
# The instance definition that tor_instances expects: |
| 27 |
# inst_name{:inst_conf:inst_user:inst_group:inst_pidfile:inst_data_dir} |
| 28 |
# |
| 29 |
|
| 30 |
. /etc/rc.subr |
| 31 |
|
| 32 |
name="tor" |
| 33 |
rcvar=tor_enable |
| 34 |
exit_code=0 |
| 35 |
|
| 36 |
load_rc_config ${name} |
| 37 |
|
| 38 |
: ${tor_enable="NO"} |
| 39 |
: ${tor_instances=""} |
| 40 |
: ${tor_conf="%%PREFIX%%/etc/tor/torrc"} |
| 41 |
: ${tor_user="%%USER%%"} |
| 42 |
: ${tor_group="%%GROUP%%"} |
| 43 |
: ${tor_pidfile="/var/run/tor/tor.pid"} |
| 44 |
: ${tor_datadir="/var/db/tor"} |
| 45 |
: ${tor_disable_default_instance="NO"} |
| 46 |
: ${tor_setuid="NO"} |
| 47 |
|
| 48 |
instance=${slave_instance} |
| 49 |
if [ -n "${instance}" ]; then |
| 50 |
inst_def=${instance} |
| 51 |
inst_name=${inst_def%%:*} |
| 52 |
[ "${inst_name}" != "main" ] || err 1 "${name} instance can't be named 'main'" |
| 53 |
inst_def=${inst_def#$inst_name} |
| 54 |
if [ -n "$inst_def" ]; then |
| 55 |
# extended instance: parameters are set explicitly |
| 56 |
inst_def=${inst_def#:} |
| 57 |
tor_conf=${inst_def%%:*} |
| 58 |
inst_def=${inst_def#$tor_conf:} |
| 59 |
tor_user=${inst_def%%:*} |
| 60 |
inst_def=${inst_def#$tor_user:} |
| 61 |
tor_group=${inst_def%%:*} |
| 62 |
inst_def=${inst_def#$tor_group:} |
| 63 |
tor_pidfile=${inst_def%%:*} |
| 64 |
tor_datadir=${inst_def#$tor_pidfile:} |
| 65 |
if [ -z "${tor_conf}" -o -z "${tor_user}" -o -z "${tor_group}" -o -z "${tor_pidfile}" -o -z "${tor_datadir}" ]; then |
| 66 |
warn "invalid tor instance ${inst_name} settings: ${instance}" |
| 67 |
exit 1 |
| 68 |
fi |
| 69 |
else |
| 70 |
# regular instance: default parameters are used |
| 71 |
tor_conf=${tor_conf}@${inst_name} |
| 72 |
tor_pidfile=${tor_pidfile}@${inst_name} |
| 73 |
tor_datadir=${tor_datadir}/instance@${inst_name} |
| 74 |
fi |
| 75 |
if ! [ -r ${tor_conf} ]; then |
| 76 |
warn "tor instance ${inst_name} config file ${tor_conf} doesn't exist or isn't readable" |
| 77 |
warn "you can copy the sample config %%PREFIX%%/etc/tor/torrc.sample and modify it" |
| 78 |
exit 1 |
| 79 |
fi |
| 80 |
if ! [ -d ${tor_datadir} ]; then |
| 81 |
mkdir -p ${tor_datadir} && |
| 82 |
chown ${tor_user}:${tor_group} ${tor_datadir} && |
| 83 |
chmod 0700 ${tor_datadir} && |
| 84 |
echo "${name}: created the instance data directory ${tor_datadir}" |
| 85 |
fi |
| 86 |
fi |
| 87 |
|
| 88 |
if [ -z "${instance}" -a -n "${tor_instances}" ]; then |
| 89 |
inst_only="$2" |
| 90 |
inst_done=0 |
| 91 |
for i in ${tor_instances}; do |
| 92 |
inst_name=${i%%:*} |
| 93 |
if [ -z "${inst_only}" -o "${inst_name}" = "${inst_only}" ]; then |
| 94 |
echo -n "${name} instance ${inst_name}: " |
| 95 |
if ! slave_instance=${i} %%PREFIX%%/etc/rc.d/tor "$1"; then |
| 96 |
exit_code=1 |
| 97 |
fi |
| 98 |
inst_done=$((inst_done+1)) |
| 99 |
fi |
| 100 |
done |
| 101 |
if [ -z "${inst_only}" -o "${inst_only}" = "main" ]; then |
| 102 |
checkyesno tor_disable_default_instance && return $exit_code |
| 103 |
echo -n "${name} main instance: " |
| 104 |
elif [ -n "${inst_only}" ]; then |
| 105 |
[ $inst_done -gt 0 ] || err 1 "${name} instance '$inst_only' isn't defined" |
| 106 |
return $exit_code |
| 107 |
fi |
| 108 |
fi |
| 109 |
|
| 110 |
required_files=${tor_conf} |
| 111 |
required_dirs=${tor_datadir} |
| 112 |
pidfile=${tor_pidfile} |
| 113 |
command="%%PREFIX%%/bin/${name}" |
| 114 |
command_args="-f ${tor_conf} --PidFile ${tor_pidfile} --RunAsDaemon 1 --DataDirectory ${tor_datadir}" |
| 115 |
extra_commands="reload" |
| 116 |
|
| 117 |
# clear user setting in conf file: it should be done through the command line |
| 118 |
if grep -q "^User ${tor_user}$" ${tor_conf}; then |
| 119 |
sed -i '' -e "s/^User ${tor_user}$//" ${tor_conf} |
| 120 |
fi |
| 121 |
|
| 122 |
if [ $tor_setuid = "YES" ]; then |
| 123 |
command_args="${command_args} --User ${tor_user}" |
| 124 |
tor_user="root" |
| 125 |
tor_group="wheel" |
| 126 |
fi |
| 127 |
|
| 128 |
if ! run_rc_command "$1"; then |
| 129 |
exit_code=1 |
| 130 |
fi |
| 131 |
|
| 132 |
return $exit_code |