Skip to content

Commit 313fd72

Browse files
committed
Major changes
Use single config.json for all configuration stuff Only update one IP address type (v4 or v6) at the same time (Make multiple requests to update both)
1 parent 5b0c3e7 commit 313fd72

File tree

8 files changed

+64
-227
lines changed

8 files changed

+64
-227
lines changed

.htaccess

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RewriteEngine On
2+
3+
RewriteRule ^config.json - [F,L]

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ Authorization is done using HTTP Basic Auth or using the *username* and *passwor
88

99
The full URL looks like *http://dyndns.example.com/?hostname=myhost.example.com* (HTTP Basic Auth) or *http://dyndns.example.com/?hostname=myhost.example.com&username=myuser&password=mypassword* (URL variable auth).
1010

11-
You may also specify the IP addresses using a GET variable (ipaddresses=address1,ipaddress2,...). Example: *http://dyndns.example.com/?hostname=myhost.example.com&ipaddresses=79.206.99.18,2003:66:ef47:3400:cb0:7f2c:6c50:567d*
11+
You may also specify the IP address using a GET variable (ipaddress=your.ip.address.here). Example: *http://dyndns.example.com/?hostname=myhost.example.com&ipaddress=79.206.99.18*
12+
13+
PHP DynDNS also supports IPv6! To update both, the IPv4 and IPv6 address, just make two requests (one with the IPv4 and one with the IPv6 address).
1214

1315
## Requirements
1416

@@ -19,12 +21,10 @@ You may also specify the IP addresses using a GET variable (ipaddresses=address1
1921
## Installation
2022

2123
* Clone this repository to your web directory from where you want to serve the files (e.g. /var/www/dyndns)
22-
* Create a file *config.json* in the data directory (See the *config.json* wiki page)
23-
* Configure the user provider you want to use (e.g. XML)
24+
* Copy *config.sample.json* to *config.json* (See the *config.json* wiki page for details)
25+
* Edit config.json to fit your needs
2426
* Configure your router to automatically do a request to the URL of your DynDNS service after reconnect (Or create a cronjob with curl/wget).
2527

26-
## User Configuration
27-
28-
All the user configuration stuff like allowed hostnames or post processing commands is configured via user providers.
28+
## Important
2929

30-
The default user provider reads the user configuration from a XML file (See the *XML User Provider* wiki page).
30+
Make sure the config.json is not readable via HTTP! On Apache this is already done using the .htaccess file.

config.sample.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"server" : "localhost",
3+
"ttl" : 60,
4+
"users" :
5+
{
6+
"myuser" :
7+
{
8+
"password" : "mypassword",
9+
"hosts" :
10+
{
11+
"myhost.example.com" :
12+
{
13+
"zone" : "example.com"
14+
},
15+
"anotherhost.example.com" :
16+
{
17+
"zone" : "anotherhost.example.com"
18+
}
19+
},
20+
"postprocess" : "nohup sudo /opt/some-script.sh %hostname% %ipaddress% %iptype%"
21+
}
22+
}
23+
}

data/.gitignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

data/.htaccess

Lines changed: 0 additions & 1 deletion
This file was deleted.

index.php

Lines changed: 31 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
define("ERROR_INVALID_IP", "iperror");// IP address is invalid
1111

1212
// Check whether the configuration file exists
13-
$configFile = __DIR__ . "/data/config.json";
13+
$configFile = __DIR__ . "/config.json";
1414
if (!file_exists($configFile))
1515
{
1616
header("HTTP/1.1 500 Internal Server Error");
@@ -20,95 +20,57 @@
2020

2121
// Read configuration file
2222
$config = json_decode(file_get_contents($configFile));
23+
$users = $config->users;
2324

2425
// Read user provided data
2526
$username = $_SERVER["PHP_AUTH_USER"] ?: $_GET["username"];
2627
$password = $_SERVER["PHP_AUTH_PW"] ?: $_GET["password"];
2728
$hostname = $_GET["hostname"];
28-
$ipAddresses = explode(",", $_GET["ipaddresses"] ?: $_SERVER["REMOTE_ADDR"]);
29-
30-
// Check whether the configured user provider exists
31-
$userProviderFile = __DIR__ . "/user-providers/" . $config->userProvider . ".class.php";
32-
if (!file_exists($userProviderFile))
33-
{
34-
header("HTTP/1.1 500 Internal Server Error");
35-
echo "No such user provider: " . $config->userProvider;
36-
exit;
37-
}
38-
39-
// Create user provider instance
40-
require_once $userProviderFile;
41-
42-
$userProviderClass = "UserProvider_" . $config->userProvider;
43-
44-
/** @var $userProvider AbstractUserProvider */
45-
$userProvider = new $userProviderClass($config->userProviderConfig->{$config->userProvider});
29+
$ipAddress = $_GET["ipaddress"] ?: $_SERVER["REMOTE_ADDR"];
4630

4731
// Check whether the given username and password match to an account
48-
if (!$userProvider->checkAuth($username, $password))
32+
if (!isset($users->{$username}) or $users->{$username}->password != $password)
4933
{
5034
header("WWW-Authenticate: Basic realm=\"DynDNS Update\"");
5135
header("HTTP/1.0 401 Unauthorized");
5236
echo ERROR_BADAUTH;
5337
exit;
5438
}
5539

40+
$user = $users->{$username};
41+
5642
// Check whether the given hostname is valid
57-
if (!$hostname or !$userProvider->checkHost($username, $hostname))
43+
if (!$hostname or !isset($user->hosts->{$hostname}))
5844
{
5945
header("HTTP/1.1 400 Bad Request");
6046
echo ERROR_INVALID_HOST;
6147
exit;
6248
}
6349

64-
// Build nsupdate commands
65-
$nsupdateCommands = array();
66-
$nsupdateCommands[] = "server localhost";
67-
$nsupdateCommands[] = "zone " . $userProvider->getZoneOfHost($username, $hostname);
68-
$nsupdateCommands[] = "update delete " . $hostname;
69-
70-
// Check IP addresses and add update commands for each given IP address
71-
$firstIPv4Address = "";
72-
$firstIPv6Address = "";
73-
foreach ($ipAddresses as $index => $ipAddress)
74-
{
75-
if (!$ipAddress or !filter_var($ipAddress, FILTER_VALIDATE_IP))
76-
{
77-
unset($ipAddress[$index]);
78-
}
79-
80-
// Is IPv4 address?
81-
if (preg_match("/^\d{1,3}(\.\d{1,3}){3,3}$/", $ipAddress))
82-
{
83-
$entryType = "A";
84-
85-
if (!$firstIPv4Address)
86-
{
87-
$firstIPv4Address = $ipAddress;
88-
}
89-
}
90-
else
91-
{
92-
$entryType = "AAAA";
93-
94-
if (!$firstIPv6Address)
95-
{
96-
$firstIPv6Address = $ipAddress;
97-
}
98-
}
99-
100-
$nsupdateCommands[] = "update add " . $hostname . " 60 " . $entryType . " " . $ipAddress;
101-
}
102-
103-
// Error if no valid IP address was given
104-
if (empty($ipAddresses))
50+
// Check whether the given IP address is valid
51+
if (!$ipAddress or !filter_var($ipAddress, FILTER_VALIDATE_IP))
10552
{
10653
header("HTTP/1.1 400 Bad Request");
10754
echo ERROR_INVALID_IP;
10855
exit;
10956
}
11057

111-
// Add "send" command
58+
// Is IPv4 address?
59+
if (preg_match("/^\d{1,3}(\.\d{1,3}){3,3}$/", $ipAddress))
60+
{
61+
$entryType = "A";
62+
}
63+
else
64+
{
65+
$entryType = "AAAA";
66+
}
67+
68+
// Build nsupdate commands
69+
$nsupdateCommands = array();
70+
$nsupdateCommands[] = "server " . $config->server;
71+
$nsupdateCommands[] = "zone " .$user->hosts->{$hostname}->zone;
72+
$nsupdateCommands[] = "update delete " . $hostname . " " . $entryType;
73+
$nsupdateCommands[] = "update add " . $hostname . " " . $config->ttl . " " . $entryType . " " . $ipAddress;
11274
$nsupdateCommands[] = "send";
11375

11476
// Execute nsupdate
@@ -121,33 +83,17 @@
12183
}
12284
else
12385
{
124-
// Run post process commands
125-
$postProcessCommands = $userProvider->getPostProcessCommands($username);
126-
foreach ($postProcessCommands as $postProcessCommand)
86+
// Run post process command
87+
if (isset($user->postprocess))
12788
{
128-
$commandLine = $postProcessCommand->command;
89+
$commandLine = $user->postprocess;
12990
$commandLine = str_replace("%username%", $username, $commandLine);
13091
$commandLine = str_replace("%hostname%", $hostname, $commandLine);
131-
$commandLine = str_replace("%ipv4address%", $firstIPv4Address, $commandLine);
132-
$commandLine = str_replace("%ipv6address%", $firstIPv6Address, $commandLine);
133-
134-
if ($postProcessCommand->noWait)
135-
{
136-
$commandLine .= " > /dev/null 2>&1 &";
137-
}
92+
$commandLine = str_replace("%ipaddress%", $ipAddress, $commandLine);
93+
$commandLine = str_replace("%iptype%", $entryType, $commandLine);
13894

13995
exec($commandLine);
14096
}
14197

142-
echo ERROR_OK . " ";
143-
144-
// Add either the IPv4 or IPv6 address
145-
if (preg_match("/^\d{1,3}(\.\d{1,3}){3,3}$/", $_SERVER["REMOTE_ADDR"]))
146-
{
147-
echo $firstIPv4Address;
148-
}
149-
else
150-
{
151-
echo $firstIPv6Address;
152-
}
98+
echo ERROR_OK . " " . $ipAddress;
15399
}

user-providers/AbstractUserProvider.class.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

user-providers/xml.class.php

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)