diff --git a/ap-setup.sh b/ap-setup.sh new file mode 100755 index 0000000..93eca0f --- /dev/null +++ b/ap-setup.sh @@ -0,0 +1,177 @@ +#!/bin/bash + +# ap-setup.sh +# OpenDRS Online Discrepancy Reporting System +# Copyright (C) 2022 Rod Wright + +# SPDX-License-Identifier: GPL-2.0 + +# Wireless Access Point Raspberry Pi Setup +function get_ap_params() { + echo "" + wapssidok=0 + while [ "$wapssidok" -ne 1 ] + do + echo "This access point will need an SSID. This is what will appear" + echo "as the name of this terminal when other devices connect." + echo -n "Please enter the desired SSID for this access point: " + read wapssid + if [ "$wapssid" = "" ] + then + echo "SSID cannot be blank. Try again." + else + wapssidok=1 + fi + done + wappassok=0 + while [ "$wappassok" -ne 1 ] + do + echo "You will need to set a passphrase for this access point. It should" + echo "be at least 8 characters in length and cannot be blank." + echo -n "Please enter the desired passphrase for this access point: " + read wappass + wappasslen=`echo -n $wappass | wc -m` + if [ "$wappass" = "" -o "$wappasslen" -lt 8 ] + then + echo "Passphrase doesn't satisfy requirements above. Try again." + else + wappassok=1 + fi + done + # Save parameters in config file + sed -i 's,'wapssid=.*','wapssid="\"$wapssid\""',' /etc/piwifiap.conf + sed -i 's,'wappass=.*','wappass="\"$wappass\""',' /etc/piwifiap.conf +} + +current_user=`whoami` +if [ "$current_user" != "root" ] +then + echo "You must have root privileges to run this setup." + echo "Try 'sudo $0'" + exit 1 +fi +echo "" +echo "" +echo "If this terminal connects to a network using ethernet, it can be configured" +echo "as a wireless access point for other terminals. Would you like to configure" +echo -n "this terminal as an access point? [y/N]: " +read wapconf +if [ "$wapconf" = "Y" -o "$wapconf" = "y" ] +then + # Determine if we are using NetworkManager or systemd-networkd + echo "" + echo "Copying files..." + chmod +x apsetup/usr/local/bin/* + cp -Rv apsetup/usr/local/bin/* /usr/local/bin + cp -Rv apsetup/etc/* /etc + if systemctl status dhcpcd.service|grep -q "Active: active (running)" + then + echo "Configuring as wireless access point using systemd-networkd." + get_ap_params + # Save network system in config file + sed -i 's,'network_system=.*','network_system="\"systemd-networkd\""',' /etc/piwifiap.conf + + apt install hostapd + systemctl unmask hostapd + systemctl enable hostapd + echo "[NetDev]" >/etc/systemd/network/bridge-br0.netdev + echo "Name=br0" >>/etc/systemd/network/bridge-br0.netdev + echo "Kind=bridge" >>/etc/systemd/network/bridge-br0.netdev + echo "[Match]" >/etc/systemd/network/br0-member-eth0.network + echo "Name=eth0" >>/etc/systemd/network/br0-member-eth0.network + echo "" >>/etc/systemd/network/br0-member-eth0.network + echo "[Network]" >>/etc/systemd/network/br0-member-eth0.network + echo "Bridge=br0" >>/etc/systemd/network/br0-member-eth0.network + systemctl enable systemd-networkd + mv /etc/dhcpcd.conf /etc/dhcpcd.conf.old + echo "denyinterfaces wlan0 eth0" >/etc/dhcpcd.conf + cat /etc/dhcpcd.conf.old >>/etc/dhcpcd.conf + echo "interface br0" >>/etc/dhcpcd.conf + rfkill unblock wlan + echo "country_code=US" >/etc/hostapd/hostapd.conf + echo "interface=wlan0" >>/etc/hostapd/hostapd.conf + echo "bridge=br0" >>/etc/hostapd/hostapd.conf + echo "ssid=$wapssid" >>/etc/hostapd/hostapd.conf + echo "hw_mode=g" >>/etc/hostapd/hostapd.conf + echo "channel=7" >>/etc/hostapd/hostapd.conf + echo "macaddr_acl=0" >>/etc/hostapd/hostapd.conf + echo "auth_algs=1" >>/etc/hostapd/hostapd.conf + echo "ignore_broadcast_ssid=0" >>/etc/hostapd/hostapd.conf + echo "wpa=2" >>/etc/hostapd/hostapd.conf + echo "wpa_passphrase=$wappass" >>/etc/hostapd/hostapd.conf + echo "wpa_key_mgmt=WPA-PSK" >>/etc/hostapd/hostapd.conf + echo "wpa_pairwise=TKIP" >>/etc/hostapd/hostapd.conf + echo "rsn_pairwise=CCMP" >>/etc/hostapd/hostapd.conf + if dpkg -l|grep webmin >/dev/null 2>&1 + then + wget -c http://github.com/pjz/webmin-modules/releases/download/v1.0/hostap.wbm.gz + gzip -d hostap.wbm.gz + /usr/share/webmin/install-module.pl hostap.wbm + fi + echo "" + echo "" + elif systemctl status NetworkManager.service|grep -q "Active: active (running)" + then + echo "Configuring as wireless access point using NetworkManager." + rfkill unblock wlan + get_ap_params + + # Clean up a possible failed previous attempt + nmcli con del piwifi-ap >/dev/null 2>&1 + nmcli con del piwifi-ethernet >/dev/null 2>&1 + nmcli con del piwifi-apbridge >/dev/null 2>&1 + + # Save network system in config file + sed -i 's,'network_system=.*','network_system="\"NetworkManager\""',' /etc/piwifiap.conf + + nmcli con add type bridge \ + con-name piwifi-apbridge \ + ifname bridge0 + + nmcli con add type ethernet \ + slave-type bridge \ + con-name piwifi-ethernet \ + ifname eth0 \ + master bridge0 + + nmcli con add type wifi \ + ifname wlan0 \ + con-name piwifi-ap \ + autoconnect yes \ + ssid "$wapssid" \ + slave-type bridge \ + master bridge0 \ + wifi-sec.key-mgmt wpa-psk \ + wifi-sec.psk "$wappass" \ + 802-11-wireless.mode ap \ + 802-11-wireless.band bg + + nmcli con up piwifi-apbridge + nmcli con up piwifi-ethernet + nmcli con up piwifi-ap + else + echo "Unable to determine what type of network configuration (systemd-networkd" + echo "or NetworkManager) your system uses. Automatic setup cannot continue." + exit 1 + fi + + echo "Wireless access point setup is complete. It will be" + echo "automatically enabled after reboot. To change the SSID or" + echo "passphrase in the future, run:" + echo " sudo wap-setssid" + echo "or:" + echo " sudo wap-setpassphrase" + echo "at a command prompt." + echo "" + echo "To disable the access point altogether in the future, run:" + echo " sudo wap-disable" + echo "at a command prompt and reboot the terminal. To re-enable it, run:" + echo " sudo wap-enable" + echo "at a command prompt and reboot the terminal." + echo "" + echo -n "Press enter key to continue..." + read cont +else + exit 2 +fi + diff --git a/apsetup/etc/piwifiap.conf b/apsetup/etc/piwifiap.conf new file mode 100644 index 0000000..6324d12 --- /dev/null +++ b/apsetup/etc/piwifiap.conf @@ -0,0 +1,7 @@ +# piwifiap.conf +# Configuration file for PiWifiAP + +network_system="unknown" + +wapssid="none" +wappass="none" diff --git a/apsetup/etc/webmin/custom/1630026603.cmd b/apsetup/etc/webmin/custom/1630026603.cmd new file mode 100644 index 0000000..4abfe65 --- /dev/null +++ b/apsetup/etc/webmin/custom/1630026603.cmd @@ -0,0 +1,3 @@ +sudo wap-enable +Enable Wireless Access Point +* 0 1 0 0 0 0 0 - diff --git a/apsetup/etc/webmin/custom/1630026603.html b/apsetup/etc/webmin/custom/1630026603.html new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/apsetup/etc/webmin/custom/1630026603.html @@ -0,0 +1 @@ + diff --git a/apsetup/etc/webmin/custom/1630026756.cmd b/apsetup/etc/webmin/custom/1630026756.cmd new file mode 100644 index 0000000..bc5c3ea --- /dev/null +++ b/apsetup/etc/webmin/custom/1630026756.cmd @@ -0,0 +1,3 @@ +sudo wap-disable +Disable Wireless Access Point +* 0 1 0 0 0 0 0 - diff --git a/apsetup/etc/webmin/custom/1630026756.html b/apsetup/etc/webmin/custom/1630026756.html new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/apsetup/etc/webmin/custom/1630026756.html @@ -0,0 +1 @@ + diff --git a/apsetup/etc/webmin/custom/1630027149.edit b/apsetup/etc/webmin/custom/1630027149.edit new file mode 100644 index 0000000..0c2e4aa --- /dev/null +++ b/apsetup/etc/webmin/custom/1630027149.edit @@ -0,0 +1,11 @@ +/etc/hostapd/hostapd.conf +Edit Wireless Access Point Parameters + + + + +systemctl restart hostapd +0 +0 + + diff --git a/apsetup/etc/webmin/custom/1630027149.html b/apsetup/etc/webmin/custom/1630027149.html new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/apsetup/etc/webmin/custom/1630027149.html @@ -0,0 +1 @@ + diff --git a/apsetup/etc/webmin/custom/config b/apsetup/etc/webmin/custom/config new file mode 100644 index 0000000..7e7dd2a --- /dev/null +++ b/apsetup/etc/webmin/custom/config @@ -0,0 +1,8 @@ +display_mode=1 +params_file=0 +params_cmd=0 +columns=1 +height= +wrap= +width= +sort= diff --git a/apsetup/usr/local/bin/wap-disable b/apsetup/usr/local/bin/wap-disable new file mode 100644 index 0000000..b5df648 --- /dev/null +++ b/apsetup/usr/local/bin/wap-disable @@ -0,0 +1,27 @@ +#!/bin/bash +# Disable PiWifiAP wireless access point + +current_user=`whoami` +if [ "$current_user" != "root" ] +then + echo "You must have root privileges to run this setup." + echo "Try 'sudo $0'" + exit 1 +fi + +source /etc/piwifiap.conf + +if [ "$network_system" = "systemd-networkd" ] +then + systemctl stop hostapd + systemctl disable hostapd +elif [ "$network_system" = "NetworkManager" ] +then + nmcli con down piwifi-ap + nmcli con mod piwifi-ap autoconnect no +else + echo "Unable to determine what type of network configuration (systemd-networkd" + echo "or NetworkManager) your system uses." + echo "Cannot disable wireless access point." + exit 2 +fi diff --git a/apsetup/usr/local/bin/wap-enable b/apsetup/usr/local/bin/wap-enable new file mode 100644 index 0000000..3ee85fc --- /dev/null +++ b/apsetup/usr/local/bin/wap-enable @@ -0,0 +1,28 @@ +#!/bin/bash +# Enable PiWifiAP wireless access point + +current_user=`whoami` +if [ "$current_user" != "root" ] +then + echo "You must have root privileges to run this setup." + echo "Try 'sudo $0'" + exit 1 +fi + +source /etc/piwifiap.conf + +if [ "$network_system" = "systemd-networkd" ] +then + systemctl enable hostapd + systemctl start hostapd +elif [ "$network_system" = "NetworkManager" ] +then + nmcli con mod piwifi-ap autoconnect yes + nmcli con up piwifi-ap +else + echo "Unable to determine what type of network configuration (systemd-networkd" + echo "or NetworkManager) your system uses." + echo "Cannot enable wireless access point." + exit 2 +fi + diff --git a/apsetup/usr/local/bin/wap-setpassphrase b/apsetup/usr/local/bin/wap-setpassphrase new file mode 100644 index 0000000..3ec4fdd --- /dev/null +++ b/apsetup/usr/local/bin/wap-setpassphrase @@ -0,0 +1,47 @@ +#!/bin/bash +# Set passphrase for PiWifiAP wireless access point + +current_user=`whoami` +if [ "$current_user" != "root" ] +then + echo "You must have root privileges to run this setup." + echo "Try 'sudo $0'" + exit 1 +fi + +source /etc/piwifiap.conf + +newppok=0 +while [ "$newppok" -ne 1 ] +do + echo -n "New passphrase: " + read newpp + newpplen=`echo -n $newpp | wc -m` + if [ "$newpp" = "" -o "$newpplen" -lt 8 ] + then + echo "Passphrase didn't satisfy requirements. Try again or hit -c to abort." + else + newppok=1 + fi +done + +if [ "$network_system" = "systemd-networkd" ] +then + sed -i "/wpa_passphrase=.*/c wpa_passphrase=$newpp" /etc/hostapd/hostapd.conf + systemctl restart hostapd +elif [ "$network_system" = "NetworkManager" ] +then + nmcli con down piwifi-ap + nmcli con mod piwifi-ap wifi-sec.psk "$newpp" + nmcli con up piwifi-ap +else + echo "Unable to determine what type of network configuration (systemd-networkd" + echo "or NetworkManager) your system uses." + echo "Cannot modify wireless access point parameters." + exit 2 +fi + +sed -i 's,'wappass=.*','wappass="\"$newpp\""',' /etc/piwifiap.conf + + + diff --git a/apsetup/usr/local/bin/wap-setssid b/apsetup/usr/local/bin/wap-setssid new file mode 100644 index 0000000..ab3e5b5 --- /dev/null +++ b/apsetup/usr/local/bin/wap-setssid @@ -0,0 +1,45 @@ +#!/bin/bash +# Set SSID for PiWifiAP wireless access point + +current_user=`whoami` +if [ "$current_user" != "root" ] +then + echo "You must have root privileges to run this setup." + echo "Try 'sudo $0'" + exit 1 +fi + +source /etc/piwifiap.conf + +newssidok=0 +while [ "$newssidok" -ne 1 ] +do + echo -n "New SSID: " + read newssid + if [ "$newssid" = "" ] + then + echo "SSID cannot be blank. Try again or hit -c to abort." + else + newssidok=1 + fi +done + +if [ "$network_system" = "systemd-networkd" ] +then + sed -i "/ssid=.*/c ssid=$newssid" /etc/hostapd/hostapd.conf + systemctl restart hostapd +elif [ "$network_system" = "NetworkManager" ] +then + nmcli con down piwifi-ap + nmcli con mod piwifi-ap ssid "$newssid" + nmcli con up piwifi-ap +else + echo "Unable to determine what type of network configuration (systemd-networkd" + echo "or NetworkManager) your system uses." + echo "Cannot modify wireless access point parameters." + exit 2 +fi + +sed -i 's,'wapssid=.*','wapssid="\"$newssid\""',' /etc/piwifiap.conf + +