Commit 02c30fd3 authored by Jouke Witteveen's avatar Jouke Witteveen
Browse files

Code quality improvements

parent eaeacf18
[Unit]
Description=netctl sleep hook
Documentation=man:netctl.special(7)
Before=sleep.target
StopWhenUnneeded=yes
......
[Unit]
Description=(Re)store the netctl profile state
Documentation=man:netctl.special(7)
Before=network.target
[Service]
......
......@@ -15,7 +15,7 @@ case $ACTION in
CONNECT)
DhcpcdOptions+=" -K"
ip_set || exit 1
# JP: sandbox the eval
# Sandbox the eval
if ! ( eval $ExecUpPost ); then
# Failing ExecUpPost will take the connection down
netctl-auto stop "$INTERFACE"
......@@ -23,7 +23,7 @@ case $ACTION in
fi
;;
DISCONNECT)
# JP: sandbox the eval
# Sandbox the eval
if ! ( eval $ExecDownPre ); then
exit 1
fi
......
......@@ -45,7 +45,7 @@ in_array() {
}
## Check if a variable denotes a positive truth value
# $1: the variable to check, use is_yes ${VAR:-yes} to set a default
# $1: the variable to check, use is_yes "${VAR:-yes}" to set a default
is_yes() {
case ${1,,} in
yes|true|on|1)
......@@ -105,16 +105,15 @@ timeout_wait() {
## List all acceptable profiles names (assume they don't contain newlines)
list_profiles() {
# JP: follow aliases with -L, also skip profiles that end with '.conf' (so
# profile.conf can be the wpa.conf file for profile)
find -L "$PROFILE_DIR/" -maxdepth 1 -type f -not -name '.*' -not -name '*~' -not -name '*.action' -not -name '*.conf' -not -name '*.service' -printf "%f\n"
# Follow aliases with -L, skip forbidden/reserved names
find -L "$PROFILE_DIR/" -maxdepth 1 -type f -not -name '.*' -not -name '*~' -not -name $'*\n*' -not -name '*.action' -not -name '*.conf' -not -name '*.service' -printf "%f\n"
}
## Sources all hooks, a profile and any interface hook
# $1: profile name
load_profile() {
local hook
if [[ ! -r "$PROFILE_DIR/$1" ]]; then
if [[ -z $1 || ! -r "$PROFILE_DIR/$1" ]]; then
exit_error "Profile '$1' does not exist or is not readable"
fi
while read -r hook; do
......
......@@ -79,7 +79,7 @@ case $1 in
report_error "Failed to bring the network up for profile '$Profile'"
exit 1
fi
# JP: sandbox the eval
# Sandbox the eval
if ! ( eval $ExecUpPost ); then
report_error "ExecUpPost failed for network profile '$Profile'"
# Failing ExecUpPost will take the connection down
......@@ -90,7 +90,7 @@ case $1 in
;;
stop)
report_notice "Stopping network profile '$Profile'..."
# JP: sandbox the eval
# Sandbox the eval
if ! ( eval $ExecDownPre ); then
report_error "ExecDownPre failed for network profile '$Profile'"
# Failing ExecDownPre will leave the profile active
......
......@@ -81,7 +81,7 @@ restore() {
stop_all() {
# We cannot pipe to mapfile, as the end of a pipe is inside a subshell
mapfile -t Profiles < <(list_profiles)
[[ $Profiles ]] && do_debug sd_call stop "${Profiles[@]}" 2> \
(( ${#Profiles[@]} )) && do_debug sd_call stop "${Profiles[@]}" 2> \
>(grep -Fv "not loaded" >&2)
}
......@@ -99,7 +99,7 @@ switch_to() {
fi
mapfile -t AllProfiles < <(list_profiles)
mapfile -t Profiles < <(grep -Fl "$InterfaceLine" "${AllProfiles[@]}")
[[ $Profiles ]] && do_debug sd_call stop "${Profiles[@]}" 2> \
(( ${#Profiles[@]} )) && do_debug sd_call stop "${Profiles[@]}" 2> \
>(grep -Fv "not loaded" >&2)
do_debug sd_call start "$1"
}
......
......@@ -133,7 +133,11 @@ create_profile()
security=none
fi
if [[ "$flags" =~ PSK|WEP ]]; then
[[ "$OBSCURE" ]] && box="--insecure --passwordbox" || box="--inputbox"
if is_yes "${OBSCURE:-no}"; then
box="--insecure --passwordbox"
else
box="--inputbox"
fi
msg="Enter $security security key for\n'$1'"
key=$(dialog $box "$msg" 10 40 --stdout) || return $?
if [[ "${BASH_REMATCH[0]}" = "WEP" ]]; then
......@@ -143,7 +147,7 @@ create_profile()
key=$(quote_safe "$key")
fi
elif [[ "${#key}" -ge 8 && "${#key}" -le 63 ]]; then
if [[ "$OBSCURE" ]]; then
if is_yes "${OBSCURE:-no}"; then
key="\"$(wpa_passphrase "$1" "$key" | sed -n "s/^[[:space:]]*psk=//p")"
else
key=$(quote_safe "$key")
......@@ -175,17 +179,17 @@ connect_to_ssid()
PROFILE=$(ssid_to_profile "$1")
if [[ $? -ne 0 ]]; then
PROFILE=$(create_profile "$1") || return $?
NEW_PROFILE=1
NEW_PROFILE=yes
fi
clear
if systemctl is-active --quiet "netctl-auto@$INTERFACE.service"; then
report_notice "Interface '$INTERFACE' is controlled by netctl-auto"
if (( NEW_PROFILE )); then
if is_yes "${NEW_PROFILE:-no}"; then
do_debug systemctl restart "netctl-auto@$INTERFACE.service"
fi
do_debug netctl-auto switch-to "$PROFILE"
elif ! netctl switch-to "$PROFILE"; then
if (( NEW_PROFILE )); then
if is_yes "${NEW_PROFILE:-no}"; then
msg=" CONNECTING FAILED
Do you want to keep the generated profile ('$PROFILE')?"
......@@ -204,7 +208,7 @@ while [[ "$1" = -* ]]; do
exit
;;
-o|--obscure)
OBSCURE=1
OBSCURE=yes
shift
;;
-*)
......@@ -248,7 +252,7 @@ if [[ "$RFKill" && "$(rf_status "$INTERFACE" "$RFKill")" ]]; then
if ! rf_enable "$INTERFACE" "$RFKill"; then
exit_error "Could not unblock transmission on interface '$INTERFACE'"
fi
RF_UNBLOCKED=1
RF_UNBLOCKED=yes
fi
echo -n "Scanning for networks... "
......@@ -256,7 +260,7 @@ CONNECTION=$(wpa_call "$INTERFACE" status 2> /dev/null | sed -n "s/^ssid=//p")
NETWORKS=$(wpa_supplicant_scan "$INTERFACE" 3,4,5)
RETURN=$?
if (( RF_UNBLOCKED )); then
if is_yes "${RF_UNBLOCKED:-no}"; then
rf_disable "$INTERFACE" "$RFKill"
fi
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment