1#!/bin/sh 2#- 3# Copyright (c) 2011 Nathan Whitehorn 4# Copyright (c) 2013-2016 Devin Teske 5# All rights reserved. 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions 9# are met: 10# 1. Redistributions of source code must retain the above copyright 11# notice, this list of conditions and the following disclaimer. 12# 2. Redistributions in binary form must reproduce the above copyright 13# notice, this list of conditions and the following disclaimer in the 14# documentation and/or other materials provided with the distribution. 15# 16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26# SUCH DAMAGE. 27# 28# $FreeBSD$ 29# 30############################################################ INCLUDES 31 32BSDCFG_SHARE="/usr/share/bsdconfig" 33. $BSDCFG_SHARE/common.subr || exit 1 34f_include $BSDCFG_SHARE/dialog.subr 35f_dialog_backtitle "FreeBSD Installer" 36 37############################################################ FUNCTIONS 38 39country_set() 40{ 41 local error_str iface_up ifconfig_args= 42 43 # 44 # Setup what was selected 45 # NB: Do not change order of arguments (or regdomain will be ignored) 46 # 47 [ "$2" ] && ifconfig_args="$ifconfig_args country $2" 48 [ "$1" ] && ifconfig_args="$ifconfig_args regdomain $1" 49 [ "$ifconfig_args" ] || return $SUCCESS # Nothing to do 50 ifconfig_args="${ifconfig_args# }" 51 52 # Regdomain/country cannot be applied while interface is running 53 iface_up=$( ifconfig -lu | grep -w "$WLAN_IFACE" ) 54 [ "$iface_up" ] && ifconfig "$WLAN_IFACE" down 55 error_str=$( ifconfig "$WLAN_IFACE" $ifconfig_args 2>&1 | 56 sed -e 's/ifconfig: //' ) 57 if [ "$iface_up" ]; then 58 # Restart wpa_supplicant(8) (should not fail). 59 wpa_supplicant -B -i "$WLAN_IFACE" -c \ 60 "$BSDINSTALL_TMPETC/wpa_supplicant.conf" 61 fi 62 if [ "$error_str" ]; then 63 $DIALOG \ 64 --title "$msg_error" \ 65 --backtitle "$DIALOG_BACKTITLE" \ 66 --yes-label Change \ 67 --no-label Ignore \ 68 --yesno \ 69 "Error while applying chosen settings ($error_str)" \ 70 0 0 71 if [ $? -eq $DIALOG_OK ]; then 72 return $FAILURE # Restart 73 else 74 return $SUCCESS # Skip 75 fi 76 else 77 awk 'sub(/^\t\t/,"")||1' \ 78 > "$BSDINSTALL_TMPETC/rc.conf.net.wlan" <<-EOF 79 create_args_$WLAN_IFACE="$ifconfig_args" 80 EOF 81 fi 82 83 return $SUCCESS 84} 85 86dialog_country_select() 87{ 88 local input regdomains countries regdomain country prompt 89 local no_default="<not selected>" 90 local default_regdomain="${1:-$no_default}" 91 local default_country="${2:-$no_default}" 92 93 # 94 # Parse available countries/regdomains 95 # 96 input=$( ifconfig "$WLAN_IFACE" list countries | sed -e 's/DEBUG//gi' ) 97 regdomains=$( echo $input | sed -e 's/.*domains://' | tr ' ' '\n' | 98 sort | tr '\n' ' ' ) 99 countries=$( echo "$input" | awk ' 100 sub(/Country codes:/, ""), sub(/Regulatory.*/, "") { 101 while (match($0, /[[:upper:]][[:upper:][:digit:]] /)) { 102 country = substr($0, RSTART) 103 sub(/ [[:upper:]][[:upper:][:digit:]].*/, "", country) 104 code = substr(country, 1, 2) 105 desc = substr(country, 4) 106 sub(/[[:space:]]*$/, "", desc) 107 printf "'\''%s'\'' '\''%s'\''\n", code, desc 108 $0 = substr($0, RSTART + RLENGTH) 109 } 110 } 111 ' | sort ) 112 113 f_dialog_title "Regdomain selection" 114 prompt="Select your regdomain." 115 f_dialog_menu_size height width rows "$DIALOG_TITLE" \ 116 "$DIALOG_BACKTITLE" "$prompt" "" $regdomains 117 regdomain=$( sh -c "$DIALOG \ 118 --title \"$DIALOG_TITLE\" \ 119 --backtitle \"$DIALOG_BACKTITLE\" \ 120 --cancel-label \"$msg_skip\" \ 121 --default-item \"$default_regdomain\" \ 122 --no-items \ 123 --stdout \ 124 --menu \"$prompt\" \ 125 $height $width $rows $regdomains" 126 ) 127 128 f_dialog_title "Country selection" 129 prompt="Select your country." 130 eval f_dialog_menu_size height width rows \ 131 \"\$DIALOG_TITLE\" \"\$DIALOG_BACKTITLE\" \ 132 \"\$prompt\" \"\" $countries 133 country=$( eval $DIALOG \ 134 --title \"\$DIALOG_TITLE\" \ 135 --backtitle \"\$DIALOG_BACKTITLE\" \ 136 --cancel-label \"\$msg_skip\" \ 137 --default-item \"\$default_country\" \ 138 --menu \"\$prompt\" \ 139 $height $width $rows \ 140 $countries \ 141 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 142 ) 143 144 country_set "$regdomain" "$country" 145} 146 147############################################################ MAIN 148 149: > "$BSDINSTALL_TMPETC/wpa_supplicant.conf" 150chmod 0600 "$BSDINSTALL_TMPETC/wpa_supplicant.conf" 151 152cat >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" << EOF 153ctrl_interface=/var/run/wpa_supplicant 154eapol_version=2 155ap_scan=1 156fast_reauth=1 157 158EOF 159 160# 161# Try to reach wpa_supplicant. If it isn't running and we can modify the 162# existing system, start it. Otherwise, fail. 163# 164(wpa_cli ping >/dev/null 2>/dev/null || ([ "$BSDINSTALL_CONFIGCURRENT" ] && 165 wpa_supplicant -B -i $1 -c "$BSDINSTALL_TMPETC/wpa_supplicant.conf")) || 166 ($DIALOG --backtitle "$DIALOG_BACKTITLE" --title "$msg_error" --msgbox \ 167 "Could not start wpa_supplicant!" 0 0; exit 1) || exit 1 168 169# See if we succeeded 170wpa_cli ping >/dev/null 2>/dev/null 171if [ $? -ne 0 -a ! "$BSDINSTALL_CONFIGCURRENT" ]; then 172 $DIALOG \ 173 --title "$msg_error" \ 174 --backtitle "$DIALOG_BACKTITLE" \ 175 --msgbox "Wireless cannot be configured without making changes to the local system!" \ 176 0 0 177 exit 1 178fi 179 180# 181# There is no way to check country/regdomain without (possible) 182# interface state modification 183# 184if [ "$BSDINSTALL_CONFIGCURRENT" ]; then 185 # Get current country/regdomain for selected interface 186 WLAN_IFACE=$( wpa_cli ifname | tail -1 ) 187 INPUT=$( ifconfig "$WLAN_IFACE" list regdomain | head -1 ) 188 DEF_REGDOMAIN=$( echo $INPUT | cut -w -f 2 ) 189 DEF_COUNTRY=$( echo $INPUT | cut -w -f 4 ) 190 [ "$DEF_REGDOMAIN" = 0 ] && DEF_REGDOMAIN="<not selected>" 191 [ "$DEF_COUNTRY" = 0 ] && DEF_COUNTRY="<not selected>" 192 f_dialog_title "Regdomain/country" 193 $DIALOG \ 194 --title "$DIALOG_TITLE" \ 195 --backtitle "$DIALOG_BACKTITLE" \ 196 --yesno "Change regdomain/country (now $DEF_REGDOMAIN/$DEF_COUNTRY)?" \ 197 0 0 198 if [ $? -eq 0 ]; then 199 while :; do 200 dialog_country_select "$DEF_REGDOMAIN" "$DEF_COUNTRY" 201 if [ $? -eq $SUCCESS ]; then 202 break 203 fi 204 done 205 fi 206fi 207 208while :; do 209 SCANSSID=0 210 output=$( wpa_cli scan 2>&1 ) 211 f_dprintf "%s" "$output" 212 f_dialog_title "Scanning" 213 $DIALOG \ 214 --title "$DIALOG_TITLE" \ 215 --backtitle "$DIALOG_BACKTITLE" \ 216 --ok-label "$msg_skip" \ 217 --pause "Waiting 5 seconds to scan for wireless networks..." \ 218 9 40 5 || exit 1 219 220 SCAN_RESULTS=$( wpa_cli scan_results ) 221 NETWORKS=$( echo "$SCAN_RESULTS" | awk -F '\t' ' 222 /..:..:..:..:..:../ && $5 { printf("\"%s\"\t%s\n", $5, $4) } 223 ' | sort | uniq ) 224 225 if [ ! "$NETWORKS" ]; then 226 f_dialog_title "$msg_error" 227 $DIALOG \ 228 --title "$DIALOG_TITLE" \ 229 --backtitle "$DIALOG_BACKTITLE" \ 230 --yesno "No wireless networks were found. Rescan?" \ 231 0 0 && continue 232 exit 1 233 fi 234 235 f_dialog_title "Network Selection" 236 NETWORK=$( sh -c "$DIALOG \ 237 --title \"$DIALOG_TITLE\" \ 238 --backtitle \"$DIALOG_BACKTITLE\" \ 239 --extra-button \ 240 --extra-label \"Rescan\" \ 241 --menu \"Select a wireless network to connect to.\" \ 242 0 0 0 \ 243 $( echo $NETWORKS | tr '\n' ' ' )" \ 244 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 245 ) 246 case $? in 247 $DIALOG_OK) break ;; 248 $DIALOG_CANCEL) 249 # here we ask if the user wants to select the network manually 250 f_dialog_title "Network Selection" 251 f_dialog_yesno "Do you want to select the network manually?" || exit 1 252 f_dialog_input NETWORK "Enter SSID" || exit 1 253 ENCRYPTION=$( $DIALOG \ 254 --title "$DIALOG_TITLE" \ 255 --backtitle "$DIALOG_BACKTITLE" \ 256 --menu "Select encryption type" \ 257 0 0 0 \ 258 "1 WPA/WPA2 PSK" "" \ 259 "2 WPA/WPA2 EAP" "" \ 260 "3 WEP" "" \ 261 "0 None" "" \ 262 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 263 ) || exit 1 264 SCANSSID=1 265 break 266 ;; 267 $DIALOG_EXTRA) # Rescan 268 ;; 269 esac 270done 271 272[ "$ENCRYPTION" ] || ENCRYPTION=$( echo "$NETWORKS" | 273 awk -F '\t' "/^\"$NETWORK\"\t/ { printf(\"%s\n\", \\\$2 ) }" ) 274 275if echo $ENCRYPTION | grep -q 'PSK'; then 276 PASS=$( $DIALOG \ 277 --title "WPA Setup" \ 278 --backtitle "$DIALOG_BACKTITLE" \ 279 --insecure \ 280 --mixedform "" \ 281 0 0 0 \ 282 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \ 283 "Password" 2 0 "" 2 12 15 63 1 \ 284 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 285 ) || exec "$0" "$@" 286 awk 'sub(/^\t/,"")||1' \ 287 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF 288 network={ 289 ssid="$NETWORK" 290 scan_ssid=$SCANSSID 291 psk="$PASS" 292 priority=5 293 } 294 EOF 295elif echo $ENCRYPTION | grep -q EAP; then 296 USERPASS=$( $DIALOG \ 297 --title "WPA-Enterprise Setup" \ 298 --backtitle "$DIALOG_BACKTITLE" \ 299 --insecure \ 300 --mixedform "" \ 301 0 0 0 \ 302 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \ 303 "Username" 2 0 "" 2 12 25 63 0 \ 304 "Password" 3 0 "" 3 12 25 63 1 \ 305 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 306 ) || exec "$0" "$@" 307 awk 'sub(/^\t/,"")||1' \ 308 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF 309 network={ 310 ssid="$NETWORK" 311 scan_ssid=$SCANSSID 312 key_mgmt=WPA-EAP$( 313 echo "$USERPASS" | awk ' 314 NR == 1 { printf "\n\t\tidentity=\"%s\"", $1 } 315 NR == 2 { printf "\n\t\tpassword=\"%s\"", $1 } 316 ' ) 317 priority=5 318 } 319 EOF 320elif echo $ENCRYPTION | grep -q WEP; then 321 WEPKEY=$( $DIALOG \ 322 --title "WEP Setup" \ 323 --backtitle "$DIALOG_BACKTITLE" \ 324 --insecure \ 325 --mixedform "" \ 326 0 0 0 \ 327 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \ 328 "WEP Key 0" 2 0 "" 2 12 15 0 1 \ 329 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 330 ) || exec "$0" "$@" 331 awk 'sub(/^\t/,"")||1' \ 332 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF 333 network={ 334 ssid="$NETWORK" 335 scan_ssid=$SCANSSID 336 key_mgmt=NONE 337 wep_key0="$WEPKEY" 338 wep_tx_keyidx=0 339 priority=5 340 } 341 EOF 342else # Open 343 awk 'sub(/^\t/,"")||1' \ 344 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF 345 network={ 346 ssid="$NETWORK" 347 scan_ssid=$SCANSSID 348 key_mgmt=NONE 349 priority=5 350 } 351 EOF 352fi 353 354# Connect to any open networks policy 355cat >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" << EOF 356network={ 357 priority=0 358 key_mgmt=NONE 359} 360EOF 361 362# Bring up new network 363if [ "$BSDINSTALL_CONFIGCURRENT" ]; then 364 output=$( wpa_cli reconfigure 2>&1 ) 365 f_dprintf "%s" "$output" 366fi 367 368exit $SUCCESS 369 370################################################################################ 371# END 372################################################################################ 373