summaryrefslogtreecommitdiffstats
path: root/ifupdown/if-pre-up
blob: d3f17f4d0b34022c5c92a4416413e04f152f47c3 (plain)
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
#!/bin/sh

set -eu

get_iface_opts()
{
	local opts_start_cb="${1}"
	local opt_cb="${2}"
	local opts_end_cb="${3}"
	local script=
	local opts=
	local opt=

	script="$(printf '
		/^iface[ \t][ \t]*%s/,/^[^ \t]/{
			/^$/q;
			s/^[ \t][ \t]*%s-//p;
		}
		' "${IFACE}" "${0##*/}")"

	opts="$(sed -n "${script}" /var/run/net-ifaces)"
	if [ "x${opts}" = 'x' ]; then
		return 0
	fi

	${opts_start_cb}
	while read -r opt; do
		${opt_cb} ${opt}
	done <<-EOF
		${opts}
		EOF
	${opts_end_cb}

	return 0
}

wpa_opts_start_cb()
{
	exec 3>"/var/run/wpa_supplicant.${IFACE}.conf"
	printf 'ctrl_interface=DIR=/var/run/wpa_supplicant.%s\ncountry=US\n\n' \
		"${IFACE}" >&3
	printf 'network={\n' >&3
}

wpa_opt_cb()
{
	local field="${1}"
	shift 1
	local value="${*}"

	field="$(printf '%s\n' "${field}" | tr '-' '_')"
	case "${field}" in
		ssid)
			if printf '%s\n' "${value}" | grep -q '[^0-9A-Fa-f:]'
			then
				if [ "x${value#\"}" = "x${value}" ]; then
					value="\"${value}\""
				fi
			fi
			;;
		psk)
			if printf '%s\n' "${value}" | grep -q '[^0-9A-Fa-f]'
			then
				if [ "x${value#\"}" = "x${value}" ]; then
					value="\"${value}\""
				fi
			fi
			;;
	esac
	printf '\t%s=%s\n' "${field}" "${value}" >&3
}

wpa_opts_end_cb()
{
	printf '}\n' >&3
	exec &3>-
	wpa_supplicant -B -s -Dnl80211 -i"${IFACE}" \
		-c"/var/run/wpa_supplicant.${IFACE}.conf"
}

if [ "x${IFACE#wlan}" != "x${IFACE}" ]; then
	get_iface_opts wpa_opts_start_cb wpa_opt_cb wpa_opts_end_cb
fi