Skip to content

Commit

Permalink
bugfixes and error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
psmsmets committed Nov 4, 2021
1 parent 6e6b712 commit 17bdc2d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 26 deletions.
42 changes: 32 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ Make sure that the DNS TTL (Time To Live) of the A-record is short (e.g., `TTL=6
Configure the UniFi controller and DirectAdmin accordingly.
All related variables should either be defined as shell (environment) variables or provided in a configuration file.

#### UniFi controller
### UniFi controller
Create a local user for the UniFi controller with read-only access.

Add the following UniFi Controller variables to your shell or the configuration file.
1. Add user
1. Set role to `Limited Admin`
1. Set account type to `Local Access Only`
1. Complete the first name, last name, local username and local password fields. All other fields can be left blank.
1. In application permission, set the Unifi Network to `View Only`. All other applications can either be `View Only` or `None`.


Add the UniFi Controller variables to your shell or the configuration file.
```
# UniFi controller configuration
UI_ADDRESS = https://url_or_ip_of_your_controller
Expand All @@ -21,17 +28,32 @@ UI_PASSWORD = '...'
UI_SITENAME = default
```

#### DirectAdmin
Create a DirectAdmin login key with `CMD_API_DNS_CONTROL` and `CMD_API_LOGIN_TEST` access.
### DirectAdmin
Create a DirectAdmin api-only login key.

1. Login to DirectAdmin
1. Go to _password management > Login Keys_ (`/user/login-keys`)
1. Create a new login key
* Type = key
* Name = dnsupdate
* Value =your own key or generate a random key by clicking the dices (don't forget to copy the value!)
* Expires = never
* Clear = unchecked
* HTM = unchecked
* Commands =
- `CMD_API_DNS_CONTROL`
- `CMD_API_LOGIN_TEST`
* Password = your DirectAdmin password

Add the following DirectAdmin variables to your shell or the configuration file.
Add the DirectAdmin variables to your shell or the configuration file.
```
# DirectAdmin configuration
DA_ADDRESS = https://root_url_to_your_directadmin
DA_USERNAME = username
DA_LOGINKEY = SOMETHING_VERY_LONG
DA_DOMAIN = example.com
DA_RECORD = sub
DA_ADDRESS = https://root_url_to_your_directadmin
DA_USERNAME = username
DA_LOGINKEY = SOMETHING_VERY_LONG
DA_DOMAIN = example.com
DA_RECORD = sub
DA_NAMESERVER = optional nameserver to query
```

## Usage
Expand Down
70 changes: 54 additions & 16 deletions dns-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,26 @@ function da_dns_address {
#
# Echo the DNS A record value from your default nameserver.
#
/usr/bin/dig ${DA_RECORD}.${DA_DOMAIN} +short
if [ x"${DA_NAMESERVER}" == "x" ]; then
/usr/bin/dig ${DA_RECORD}.${DA_DOMAIN} A +short
else
/usr/bin/dig @${DA_NAMESERVER} ${DA_RECORD}.${DA_DOMAIN} A +short
fi
}


function da_dns_dump {
#
# This function will update DNS A record in DirectAdmin to the new ip address
#
local new_ip=$1

/usr/bin/curl \
--request "POST" \
--user "${DA_USERNAME}:${DA_LOGINKEY}" \
--data domain=${DA_DOMAIN} \
--data json=yes \
${DA_ADDRESS}/CMD_API_DNS_CONTROL
}


Expand All @@ -183,15 +202,15 @@ function da_dns_update {

/usr/bin/curl \
--request "POST" \
--user "${da_user}:${da_token}" \
-d domain=${DA_DOMAIN} \
-d action=edit \
-d type=A \
-d arecs0=name%3D${DA_RECORD} \
-d name=${DA_RECORD} \
-d value=${new_ip} \
-d json=yes \
"${DA_ADDRESS}/CMD_API_DNS_CONTROL"
--user "${DA_USERNAME}:${DA_LOGINKEY}" \
--data domain=${DA_DOMAIN} \
--data action=edit \
--data type=A \
--data arecs0=name%3D${DA_RECORD} \
--data name=${DA_RECORD} \
--data value=${new_ip} \
--data json=yes \
${DA_ADDRESS}/CMD_API_DNS_CONTROL
}


Expand All @@ -208,19 +227,26 @@ if (($# > 1 )); then
badUsage "Illegal number of arguments"
fi


#
# Set UI and DA variables from configuration file and check if all are set.
# Set UI and DA variables
#

# Initialize defaults
UI_SITENAME="${UI_SITENAME:-default}"

# Parse config file
if (($# == 1 )); then
parse_config $1 UI_ADDRESS UI_USERNAME UI_PASSWORD UI_SITENAME
parse_config $1 DA_ADDRESS DA_USERNAME DA_LOGINKEY DA_DOMAIN DA_RECORD
parse_config $1 \
UI_ADDRESS UI_USERNAME UI_PASSWORD UI_SITENAME \
DA_ADDRESS DA_USERNAME DA_LOGINKEY DA_DOMAIN DA_RECORD DA_NAMESERVER
fi

# Check if mandatory variables are set
check_config UI_ADDRESS UI_USERNAME UI_PASSWORD UI_SITENAME
check_config DA_ADDRESS DA_USERNAME DA_LOGINKEY DA_DOMAIN DA_RECORD

#
# Construct derived variables
#
UI_COOKIE=$(mktemp)
UI_API="${UI_ADDRESS}/proxy/network/api"
UI_SITE_API="${UI_API}/s/${UI_SITENAME}"
Expand All @@ -233,11 +259,23 @@ DA_SUBDOMAIN="${DA_RECORD}.${DA_DOMAIN}"
#
#-------------------------------------------------------------------------------

# Get ui_wan_address
IP_UDM=$(ui_wan_address)
if [ $? != 0 ]; then
echo -n "$IP_UDM"
exit $?
fi

# Get da_dns_address
IP_DNS=$(da_dns_address)
if [ $? != 0 ]; then
echo -n "$IP_DNS"
exit $?
fi

# Update if address does not match
if [ "$IP_UDM" != "$IP_DNS" ]; then
echo "Update DNS ip address for ${DA_RECORD}.${DA_DOMAIN} -A to ${IP_UDM}. "
echo "Update DNS ip address for ${DA_RECORD}.${DA_DOMAIN} -A to ${IP_UDM}."
da_dns_update $IP_UDM
else
echo "No update needed for ${DA_RECORD}.${DA_DOMAIN} -A ${IP_DNS}."
Expand Down

0 comments on commit 17bdc2d

Please sign in to comment.