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 f_eval_catch -dk error_str wlanconfig ifconfig "ifconfig %s %s" \ 56 "$WLAN_IFACE" "$ifconfig_args" 57 error_str="${error_str#ifconfig: }" 58 if [ "$iface_up" ]; then 59 # Restart wpa_supplicant(8) (should not fail). 60 wpa_supplicant -B -i "$WLAN_IFACE" -c \ 61 "$BSDINSTALL_TMPETC/wpa_supplicant.conf" 62 fi 63 if [ "$error_str" ]; then 64 $DIALOG \ 65 --title "$msg_error" \ 66 --backtitle "$DIALOG_BACKTITLE" \ 67 --yes-label Change \ 68 --no-label Ignore \ 69 --yesno \ 70 "Error while applying chosen settings ($error_str)" \ 71 0 0 || return $SUCCESS # Skip 72 return $FAILURE # Restart 73 else 74 awk 'sub(/^\t\t/,"")||1' \ 75 > "$BSDINSTALL_TMPETC/rc.conf.net.wlan" <<-EOF 76 create_args_$WLAN_IFACE="$ifconfig_args" 77 EOF 78 fi 79 80 return $SUCCESS 81} 82 83dialog_country_select() 84{ 85 local input regdomains countries regdomain country prompt 86 local no_default="<not selected>" 87 local default_regdomain="${1:-$no_default}" 88 local default_country="${2:-$no_default}" 89 90 # 91 # Parse available countries/regdomains 92 # 93 input=$( ifconfig "$WLAN_IFACE" list countries | sed -e 's/DEBUG//gi' ) 94 regdomains=$( echo "$input" | awk ' 95 sub(/.*domains:/, ""), /[^[:alnum:][[:space:]]/ { 96 n = split($0, domains) 97 for (i = 1; i <= n; i++) 98 printf "'\''%s'\'' '\'\''", domains[i] 99 } 100 ' | sort ) 101 countries=$( echo "$input" | awk ' 102 sub(/Country codes:/, ""), sub(/Regulatory.*/, "") { 103 while (match($0, /[[:upper:]][[:upper:][:digit:]] /)) { 104 country = substr($0, RSTART) 105 sub(/ [[:upper:]][[:upper:][:digit:]].*/, "", country) 106 code = substr(country, 1, 2) 107 desc = substr(country, 4) 108 sub(/[[:space:]]*$/, "", desc) 109 printf "'\''%s'\'' '\''%s'\''\n", code, desc 110 $0 = substr($0, RSTART + RLENGTH) 111 } 112 } 113 ' | sort ) 114 115 f_dialog_title "Regdomain selection" 116 prompt="Select your regdomain." 117 eval f_dialog_menu_size height width rows \ 118 \"\$DIALOG_TITLE\" \"\$DIALOG_BACKTITLE\" \ 119 \"\$prompt\" \"\" $regdomains 120 regdomain=$( eval $DIALOG \ 121 --title \"\$DIALOG_TITLE\" \ 122 --backtitle \"\$DIALOG_BACKTITLE\" \ 123 --cancel-label \"\$msg_skip\" \ 124 --default-item \"\$default_regdomain\" \ 125 --menu \"\$prompt\" \ 126 $height $width $rows \ 127 $regdomains \ 128 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 129 ) 130 f_dialog_data_sanitize regdomain 131 132 f_dialog_title "Country selection" 133 prompt="Select your country." 134 eval f_dialog_menu_size height width rows \ 135 \"\$DIALOG_TITLE\" \"\$DIALOG_BACKTITLE\" \ 136 \"\$prompt\" \"\" $countries 137 country=$( eval $DIALOG \ 138 --title \"\$DIALOG_TITLE\" \ 139 --backtitle \"\$DIALOG_BACKTITLE\" \ 140 --cancel-label \"\$msg_skip\" \ 141 --default-item \"\$default_country\" \ 142 --menu \"\$prompt\" \ 143 $height $width $rows \ 144 $countries \ 145 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 146 ) 147 f_dialog_data_sanitize country 148 149 country_set "$regdomain" "$country" 150} 151 152############################################################ MAIN 153 154: > "$BSDINSTALL_TMPETC/wpa_supplicant.conf" 155chmod 0600 "$BSDINSTALL_TMPETC/wpa_supplicant.conf" 156 157cat >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" << EOF 158ctrl_interface=/var/run/wpa_supplicant 159eapol_version=2 160ap_scan=1 161fast_reauth=1 162 163EOF 164 165# 166# Try to reach wpa_supplicant. If it isn't running and we can modify the 167# existing system, start it. Otherwise, fail. 168# 169if ! f_eval_catch -d wlanconfig wpa_cli "wpa_cli ping"; then 170 if [ ! "$BSDINSTALL_CONFIGCURRENT" ]; then 171 f_show_err "Wireless cannot be configured without %s" \ 172 "making changes to the local system!" 173 exit 1 174 fi 175 f_eval_catch wlanconfig wpa_supplicant \ 176 'wpa_supplicant -B -i "%s" -c "%s/wpa_supplicant.conf"' \ 177 "$1" "$BSDINSTALL_TMPETC" || exit 1 178 179 f_eval_catch wlanconfig wpa_cli "wpa_cli ping" || exit 1 180fi 181 182# 183# There is no way to check country/regdomain without (possible) 184# interface state modification 185# 186if [ "$BSDINSTALL_CONFIGCURRENT" ]; then 187 # Get current country/regdomain for selected interface 188 WLAN_IFACE=$( wpa_cli ifname | tail -1 ) 189 INPUT=$( ifconfig "$WLAN_IFACE" list regdomain | head -1 ) 190 DEF_REGDOMAIN=$( echo "$INPUT" | cut -w -f 2 ) 191 DEF_COUNTRY=$( echo "$INPUT" | cut -w -f 4 ) 192 [ "$DEF_REGDOMAIN" = 0 ] && DEF_REGDOMAIN="<not selected>" 193 [ "$DEF_COUNTRY" = 0 ] && DEF_COUNTRY="<not selected>" 194 f_dialog_title "Regdomain/country" 195 f_yesno "Change regdomain/country (now $DEF_REGDOMAIN/$DEF_COUNTRY)?" 196 if [ $? -eq 0 ]; then 197 while :; do 198 dialog_country_select "$DEF_REGDOMAIN" "$DEF_COUNTRY" 199 if [ $? -eq $SUCCESS ]; then 200 break 201 fi 202 done 203 fi 204fi 205 206while :; do 207 SCANSSID=0 208 f_eval_catch -d wlanconfig wpa_cli "wpa_cli scan" 209 f_dialog_title "Scanning" 210 f_dialog_pause "Waiting 5 seconds to scan for wireless networks..." 5 || 211 exit 1 212 213 f_eval_catch -dk SCAN_RESULTS wlanconfig wpa_cli "wpa_cli scan_results" 214 NETWORKS=$( echo "$SCAN_RESULTS" | awk -F '\t' ' 215 /..:..:..:..:..:../ && $5 { printf "\"%s\"\t%s\n", $5, $4 } 216 ' | sort | uniq ) 217 218 if [ ! "$NETWORKS" ]; then 219 f_dialog_title "$msg_error" 220 f_yesno "No wireless networks were found. Rescan?" && continue 221 exit 1 222 fi 223 224 f_dialog_title "Network Selection" 225 prompt="Select a wireless network to connect to." 226 menu_list=$( echo $NETWORKS | tr '\n' ' ' ) 227 NETWORK=$( eval $DIALOG \ 228 --title \"\$DIALOG_TITLE\" \ 229 --backtitle \"\$DIALOG_BACKTITLE\" \ 230 --extra-button \ 231 --extra-label \"Rescan\" \ 232 --menu \"\$prompt\" \ 233 $height $width $rows \ 234 $menu_list \ 235 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 236 ) 237 retval=$? 238 f_dialog_data_sanitize NETWORK 239 case $retval in 240 $DIALOG_OK) break ;; 241 $DIALOG_CANCEL) 242 # Ask if the user wants to select network manually 243 f_dialog_title "Network Selection" 244 f_yesno "Do you want to select the network manually?" || exit 1 245 f_dialog_input NETWORK "Enter SSID" || exit 1 246 prompt="Select encryption type" 247 menu_list=" 248 '1 WPA/WPA2 PSK' '' 249 '2 WPA/WPA2 EAP' '' 250 '3 WEP' '' 251 '0 None' '' 252 " # END-QUOTE 253 eval f_dialog_menu_size height width rows \"\$DIALOG_TITLE\" \ 254 \"\$DIALOG_BACKTITLE\" \"\$prompt\" \"\" $menu_list 255 ENCRYPTION=$( eval $DIALOG \ 256 --title \"\$DIALOG_TITLE\" \ 257 --backtitle \"\$DIALOG_BACKTITLE\" \ 258 --menu \"\$prompt\" \ 259 $height $width $rows \ 260 $menu_list \ 261 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 262 ) || exit 1 263 SCANSSID=1 264 break 265 ;; 266 $DIALOG_EXTRA) # Rescan 267 ;; 268 esac 269done 270 271[ "$ENCRYPTION" ] || ENCRYPTION=$( echo "$NETWORKS" | 272 awk -F '\t' "/^\"$NETWORK\"\t/ { print \$2 }" ) 273 274if echo "$ENCRYPTION" | grep -q PSK; then 275 PASS=$( $DIALOG \ 276 --title "WPA Setup" \ 277 --backtitle "$DIALOG_BACKTITLE" \ 278 --insecure \ 279 --mixedform "" \ 280 0 0 0 \ 281 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \ 282 "Password" 2 0 "" 2 12 15 63 1 \ 283 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 284 ) || exec "$0" "$@" 285 awk 'sub(/^\t/,"")||1' \ 286 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF 287 network={ 288 ssid="$NETWORK" 289 scan_ssid=$SCANSSID 290 psk="$PASS" 291 priority=5 292 } 293 EOF 294elif echo "$ENCRYPTION" | grep -q EAP; then 295 USERPASS=$( $DIALOG \ 296 --title "WPA-Enterprise Setup" \ 297 --backtitle "$DIALOG_BACKTITLE" \ 298 --insecure \ 299 --mixedform "" \ 300 0 0 0 \ 301 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \ 302 "Username" 2 0 "" 2 12 25 63 0 \ 303 "Password" 3 0 "" 3 12 25 63 1 \ 304 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 305 ) || exec "$0" "$@" 306 awk 'sub(/^\t/,"")||1' \ 307 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF 308 network={ 309 ssid="$NETWORK" 310 scan_ssid=$SCANSSID 311 key_mgmt=WPA-EAP$( 312 echo "$USERPASS" | awk ' 313 NR == 1 { printf "\n\t\tidentity=\"%s\"", $1 } 314 NR == 2 { printf "\n\t\tpassword=\"%s\"", $1 } 315 ' ) 316 priority=5 317 } 318 EOF 319elif echo "$ENCRYPTION" | grep -q WEP; then 320 WEPKEY=$( $DIALOG \ 321 --title "WEP Setup" \ 322 --backtitle "$DIALOG_BACKTITLE" \ 323 --insecure \ 324 --mixedform "" \ 325 0 0 0 \ 326 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \ 327 "WEP Key 0" 2 0 "" 2 12 15 0 1 \ 328 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 329 ) || exec "$0" "$@" 330 awk 'sub(/^\t/,"")||1' \ 331 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF 332 network={ 333 ssid="$NETWORK" 334 scan_ssid=$SCANSSID 335 key_mgmt=NONE 336 wep_key0="$WEPKEY" 337 wep_tx_keyidx=0 338 priority=5 339 } 340 EOF 341else # Open 342 awk 'sub(/^\t/,"")||1' \ 343 >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" <<-EOF 344 network={ 345 ssid="$NETWORK" 346 scan_ssid=$SCANSSID 347 key_mgmt=NONE 348 priority=5 349 } 350 EOF 351fi 352 353# Connect to any open networks policy 354cat >> "$BSDINSTALL_TMPETC/wpa_supplicant.conf" << EOF 355network={ 356 priority=0 357 key_mgmt=NONE 358} 359EOF 360 361# Bring up new network 362if [ "$BSDINSTALL_CONFIGCURRENT" ]; then 363 f_eval_catch -d wlanconfig wpa_cli "wpa_cli reconfigure" 364fi 365 366exit $SUCCESS 367 368################################################################################ 369# END 370################################################################################ 371