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