summaryrefslogtreecommitdiff
path: root/net/socat/files/socat.in
blob: b1f588bd04ce54200b802a771ea5abdb3083b153 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/sh
#
# Author: Emanuel Haupt <ehaupt@FreeBSD.org>

# PROVIDE: socat
# REQUIRE: LOGIN
# KEYWORD: shutdown

# Add the following lines to /etc/rc.conf to enable socat:
# socat_enable="YES"
# socat_daemonuser="root" for priviledged ports e.g.
# socat_flags="<set as needed>" or create /usr/local/etc/socat-instances.conf

. /etc/rc.subr

name="socat"
rcvar=socat_enable

load_rc_config $name

: ${socat_enable="NO"}
: ${socat_daemonuser:=nobody}
: ${socat_config:=%%PREFIX%%/etc/socat-instances.conf}

start_precmd="socat_prestart"
pidfile=/var/run/socat.pid
procname="%%PREFIX%%/bin/socat"
command=/usr/sbin/daemon
command_args=' -f -p ${pidfile} -u ${socat_daemonuser} ${procname} ${socat_flags}'

[ -n "${2}" ] && socat_instance_arg=`echo "${2}" | tr '[:lower:]' '[:upper:]'`

socat_prestart()
{
	# socat_flags gets applied too early if we don't do this.
	# I didn't want to force people to update their rc.conf files
	# and change the socat_flags to something else.
	rc_flags=""
}

socat_parse_instances()
{
  local _line _section_search

  socat_instances=`grep -Eo "^[[:blank:]]*\[[[:alnum:]_]+\]" ${socat_config} |
    tr '[:lower:]' '[:upper:]'`
				
  if [ -n "${socat_instance_arg}" ] && ! echo "${socat_instances}" |
      grep -q -E '(^|[[:blank:]])\['${socat_instance_arg}'\]([[:blank:]]|$)'
  then
    echo -n "$name: Can't find instance definition " >&2
    echo "\"[${1}]\" in config file ${socat_config}." >&2
    return 1
  fi

  [ -n "${socat_instance_arg}" ] && socat_instances="[${socat_instance_arg}]"

  for i in ${socat_instances}; do
    _section_search=1
    _instance=${i#[}
    _instance=${_instance%]}

    # Process each line of the optional config file, which
    # matches the regex, defined at the end of the loop.
    # There we filter to only process definitions and section separators.
    while read -r _line; do

      # Look for ${i} section until found
      if [ ${_section_search} ]; then
        if echo "${_line}" | grep -qi "^[[:blank:]]*\[${_instance}\]"; then
          unset _section_search
          continue # Nothing to do with section identifiers
        else
          # Continue with next line s_instance we haven't reached our section yet
          continue
        fi
      fi

      # Stop processing if the current line is another section identifier.
      echo "${_line}" | grep -q "^[[:blank:]]*\[[^]]*\]" && break

      # Only proceed with lines which contain variable declaration.
      echo "${_line}" | grep -q -E \
                        -e "^[[:blank:]]*[[:alpha:]_][[:alnum:]_]{0,30}=" ||
                            continue
      # Filter malformed lines (which could cause command execution)
      #   (shell exits with test result, wich is false as soon as
      #   there's a 2nd argument (1st is considered as 0))
      eval sh -c \'[ \$# -eq 0 ]\' "${_line}" \|\| continue

      eval socat_${_instance}_${_line%%=*}=${_line#*=}

    done << EOCFF
$(cat "${socat_config}")
EOCFF
  done
}

# Check if daemon(8) handles title and syslog parameters
# (as in FreeBSD 11).
if [ "${1%start}" != "${1}" ]; then
	daemon_extended_args=" -l daemon"
	${command} -t "test" ${daemon_extended_args} -f -u nobody true \
		> /dev/null 2>&1 || unset daemon_extended_args
fi


# If we can read the config file, handle multiple instances,
# else just process a single instance.
if [ -r ${socat_config} ]; then

	# T O D O :   Check rc(8) how restarts are handled and make
	#		all-instaces restart working.
	#	For now refuse restart commands without instance argument.
	#
	if [ "${1%restart}" != "${1}" ] && [ -z "${socat_instance_arg}" ]; then
		echo -n "$name: Restart command requires a instance argument,"
		echo "since config file is in use."
		exit 1
	fi

	if [ -n "${socat_flags}" ]; then
		echo -n "${name}: WARNING:"
		echo -n " Ignoring \"socat_flags\" in rc.conf because"
                echo " \"${socat_config}\" is present."
	fi

	socat_parse_instances "${2}" || exit 1

	default_pidfile="${pidfile}"
	default_socat_daemonuser="${socat_daemonuser}"
	eval default_command_args=\'${command_args}\'

	for i in ${socat_instances}; do
		_instance=${i#[}
		_instance=${_instance%]}

		eval socat_flags=\"\$\{socat_${_instance}_flags\}\"

		# We need to have socat_flags to start, else skip start commands
		if [ "${1%start}" != "${1}" ] && [ -z "${socat_flags}" ]; then
			echo -n "$name: Missing \"flags\" definition for"
			echo " instance ${i}, skipping \"${1}\" command."
			continue
		fi

		eval pidfile=\"\$\{socat_${_instance}_pidfile\}\"
		[ -n "${pidfile}" ] ||
			pidfile="${default_pidfile%.pid}_${_instance}.pid"

		eval socat_daemonuser=\"\$\{socat_${_instance}_daemonuser\}\"
		[ -n "${socat_daemonuser}" ] ||
			socat_daemonuser="${default_socat_daemonuser}"

		eval command_args=\"${default_command_args}\"
		if [ -n "${daemon_extended_args}" ]; then
			# A bit confusing, but to keep 80 chars line break:
			command_args="(${_instance})\" ${command_args}"
			command_args="-t \"${procname} ${command_args}"
			command_args="${daemon_extended_args} ${command_args}"
		fi

		run_rc_command "$1"
	done

else

	if [ -n "${socat_instance_arg}" ]; then
		echo -n "$name: Missing config file (${socat_config}), " >&2
		echo "can't handle instance \"${2}\"." >&2
		exit 1
	fi

	eval command_args=\"${daemon_extended_args} ${command_args}\"
	run_rc_command "$1"

fi