diff --git a/wg-client-management.sh b/wg-client-management.sh index e7f4b2f..4291d6e 100644 --- a/wg-client-management.sh +++ b/wg-client-management.sh @@ -14,9 +14,11 @@ # - existing /usr/local/bin/sendmail.py # # PARAMETERS: -# 1: "add|del" - add or delete client config -# 2: username - client username -# 3: address - client ip address +# 1: interface - define wireguard interface +# 2: "add|del" - add or delete client config +# 3: username - client username +# 4: address - client ip address +# 5: additional - client description # -f|--force - service will restart after username add|del # # FUNCTIONS: @@ -30,7 +32,7 @@ # 1: message to print and logging ####################################### addtologs() { - echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a "${logs}" + printf "%s\n" "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a "${logs}" } ####################################### @@ -42,11 +44,11 @@ addtologs() { ####################################### execquite() { addtologs "execution time is $(($(date +%s)-time)) seconds, exit" - exit + exit "${1}" } ####################################### -# Error exit procedure +# Error exit procedure. # Globals: # None # Arguments: @@ -54,7 +56,7 @@ execquite() { ####################################### execerror() { addtologs "error: $1" - execquite + execquite 1 } ####################################### @@ -75,7 +77,7 @@ checkroot() { } ####################################### -# Send email notification about client connect +# Send email notification about client config. # Globals: # clientname # faqprofile @@ -93,15 +95,14 @@ startsendmail() { --port "$(grep "port=" /usr/local/bin/sendmail.config | cut -d= -f2)" \ --stls "True" \ --subj "${subj}" \ - --text "${faqprofile}" \ - --file "/etc/wireguard/${clientname}.png,/etc/wireguard/${clientname}.conf" \ - >> /dev/null 2>&1 & - ) + --text "$(printf "%s\n" "${faqprofile}" | sed 's|`||g')" \ + --file "/etc/wireguard/${clientname}.png,/etc/wireguard/${clientname}.conf" + ) > /dev/null 2>&1 addtologs "sent mail with subject '${subj}'" } ####################################### -# Create wireguard client certificates +# Create wireguard client certificates. # Globals: # clientname # clientaddr @@ -114,19 +115,21 @@ createcert() { clientpublkey=$(cat "/etc/wireguard/pki/${clientname}-public.key") clientprivkey=$(cat "/etc/wireguard/pki/${clientname}-private.key") wg set "${iface_name}" peer "${clientpublkey}" \ - allowed-ips "${clientaddr}/32" \ - persistent-keepalive 5 + allowed-ips "${clientaddr}/32" \ + persistent-keepalive 5 { - echo -e "[Peer]" - echo -e " PublicKey = ${clientpublkey}" - echo -e " AllowedIPs = ${clientaddr}/32" - echo -e " PersistentKeepalive = 5" + printf "%s\n" "[Peer]" + printf "%s\n" " PublicKey = ${clientpublkey}" + printf "%s\n" " AllowedIPs = ${clientaddr}/32" + printf "%s\n" " PersistentKeepalive = 5" } >> "${servercfgname}" + addtologs "created ${clientname} wireguard certificate" ip -4 route add "${clientaddr}/32" dev "${iface_name}" + addtologs "created route to ${clientname} peer" } ####################################### -# Create wireguard client configuration +# Create wireguard client configuration. # Globals: # clientname # clientaddr @@ -143,26 +146,31 @@ createconf() { clientconf=${clientconf//serverpublkey/${serverpublkey}} clientconf=${clientconf//clientaddrs/${clientaddr}} printf "%s\n" "${clientconf}" > "/etc/wireguard/${clientname}.conf" + addtologs "created ${clientname} wireguard config file" } ####################################### -# Create wireguard client info, qr-code +# Create wireguard client info, qr-code. # Globals: # clientname # Arguments: # None ####################################### createinfo() { -faqprofile=$(cat </dev/null; then if [ -f "/etc/wireguard/${clientname}.conf" ]; then - deleteconf && addtologs "deleted wg config file for ${clientname}" + deleteconf fi if [ -f "/etc/wireguard/${clientname}.png" ]; then - deleteinfo && addtologs "deleted info file for ${clientname}" + deleteinfo fi if grep -q -w "${clientaddr}/32" "${servercfgname}"; then - deletecert && addtologs "deleted certificate for ${clientname}" + deletecert fi if [ "${resetforce}" -eq 1 ];then addtologs "restarting wg-quick@${iface_name}..." systemctl "restart wg-quick@${iface_name}" fi else - printf "%s\n" "Usage example: $0 'wg0' 'add' 'username(surname)' 'address(ww.xx.yy.zz) -f'" + printf "%s\n" "Usage example: $0 'wg0' 'add' 'username(surname)' 'address(ww.xx.yy.zz)'" + printf "%s\n" "Usage example: $0 'wg0' 'add' 'username(surname)' 'address(ww.xx.yy.zz) 'additional client description'" printf "%s\n" "Usage example: $0 'wg0' 'del' 'username(surname)' 'address(ww.xx.yy.zz)'" + printf "%s\n" "Usage example: $0 'wg0' 'del' 'username(surname)' 'address(ww.xx.yy.zz)' -f" fi else execerror "Restart this as root!" fi -execquite +execquite 0