Skip to content

Instantly share code, notes, and snippets.

@superbob
Last active January 27, 2024 00:57
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save superbob/c8d44537c08ba1f7f22e9ea757e31595 to your computer and use it in GitHub Desktop.
Save superbob/c8d44537c08ba1f7f22e9ea757e31595 to your computer and use it in GitHub Desktop.
Namecheap Synology DSM DDNS provider
#Insert this at the end of /etc.defaults/ddns_provider.conf
[Namecheap]
modulepath=/usr/syno/bin/ddns/namecheap.php
queryurl=https://dynamicdns.park-your-domain.com/update
#!/usr/bin/php -d open_basedir=/usr/syno/bin/ddns
<?php
if ($argc !== 5) {
echo 'badparam';
exit();
}
$account = $argv[1];
$pwd = (string)$argv[2];
$hostname = (string)$argv[3];
$ip = (string)$argv[4];
// check the hostname contains '.'
if (strpos($hostname, '.') === false) {
echo 'badparam';
exit();
}
// only for IPv4 format
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
echo "badparam";
exit();
}
$url = 'https://dynamicdns.park-your-domain.com/update?host='.$account.'&domain='.$hostname.'&password='.$pwd.'&ip='.$ip;
$req = curl_init();
curl_setopt($req, CURLOPT_URL, $url);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
$badXmlString = curl_exec($req); // XML String returned has a wrong xml utf declaration
curl_close($req);
// Heading says content is UTF-16, but it is UTF-8, so it is manually changed here
$fixedXmlString = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $badXmlString);
$xml = new SimpleXMLElement($fixedXmlString);
if ($xml->ErrCount > 0) {
$error = $xml->errors[0]->Err1;
if (strcmp($error, "Domain name not found") === 0) {
echo "nohost";
} elseif (strcmp($error, "Passwords do not match") === 0) {
echo "badauth";
} elseif (strcmp($error, "No Records updated. A record not Found;") === 0) {
echo "nohost";
} else {
echo "911 [".$error."]";
}
} else {
echo "good";
}
@superbob
Copy link
Author

Today without any recent changes my DDNS was out of date and the status and last update time on the server say just "Loading" and Update Now does nothing. Found one other person asking having loading status in Synology forums on July 2020, there was never any response. I switched back to my DD-WRT DDNS updater, which worked, so there's no issue for Namecheap. Any quick fix besides a server reboot?

Yes it happens sometimes, the only thing to do then is to check the files previously described here. Sometimes they get missing.
I have no clear idea why it happens and currently no time to investigate this, sorry.

@24mu13
Copy link

24mu13 commented Jul 4, 2021

It happens to me recently when updating to DSM7 (make sense actually) and simply placing back namecheap.php and touching again ddns_provider.conf fixed the issue.

Today without any recent changes my DDNS was out of date and the status and last update time on the server say just "Loading" and Update Now does nothing. Found one other person asking having loading status in Synology forums on July 2020, there was never any response. I switched back to my DD-WRT DDNS updater, which worked, so there's no issue for Namecheap. Any quick fix besides a server reboot?

Yes it happens sometimes, the only thing to do then is to check the files previously described here. Sometimes they get missing.
I have no clear idea why it happens and currently no time to investigate this, sorry.

@kayserj
Copy link

kayserj commented Oct 23, 2021

Thank you superbob, Dade-R, and obviouscap. Been fighting with getting DDNS working on my synology with namecheap until I found this. Now it works perfectly, thanks again.

@tm1rules
Copy link

tm1rules commented Nov 18, 2021

Hi Superbob, Thanks for your work on this. The methods you created were working perfectly for a few weeks up until today. I rechecked the files namecheap.php and ddns_provider.conf and both files appear to be fine, but I am now receiving 'Unable to register IP Address' from my NAS running DSM7? Interestingly I created a DuckDNS provider also and this is still working fine, just wondering if you or anyone else is receiving the same issue updating namecheap A+ Dynamic DNS records.

EDIT: Further update - despite the connection failing in the DDNS menu on the NAS and receiving error notification, the Namecheap A+ Dynamic DNS records are still being updated. I confirmed this by manually changing the IP address against one of my sub-domains on the namecheap website and then running the DDNS update on the NAS.

@bloedboemmel
Copy link

Interesting!
It also stopped working last night for me, like @tm1rules
I will have a look at this

@Dade-R
Copy link

Dade-R commented Nov 19, 2021

I'm having the same issue now, it's failing to update. I am still on DSM 6.2.4, not DSM 7.

I tested a manual update by pasting this URL into the browser, like:
https://dynamicdns.park-your-domain.com/update?host=myhost&domain=mycustomdomain.com&password=00000000000000000000000000000000&ip=123.45.67.89

This is successful, and is basically what the php script is doing anyway. It appears Namecheap is still working. I confirmed the permission mask is still good (755) and the config file is the same. I'm not sure what's going on here. Going to look into curl. Any ideas @superbob ?

@bloedboemmel
Copy link

Hi Guys, I found the strange solution. Apparently namecheap changed it's respone-type-encoding.
This is my new working file. I added following code: $xml = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $res);
Here's the whole file.

#!/usr/bin/php 
<?php

if ($argc !== 5) {
  echo 'badparam';
  exit();
}

$account = $argv[1];
$pwd = (string)$argv[2];
$hostname = (string)$argv[3];
$ip = (string)$argv[4];

// check the hostname contains '.'
if (strpos($hostname, '.') === false) {
  echo 'badparam';
  exit();
}

// only for IPv4 format
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  echo "badparam";
  exit();
}

$url = 'https://dynamicdns.park-your-domain.com/update?host='.$account.'&domain='.$hostname.'&password='.$pwd.'&ip='.$ip;

$req = curl_init();
curl_setopt($req, CURLOPT_URL, $url);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($req);
curl_close($req);

$xml = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $res);  
$xml = new SimpleXMLElement($xml);
if ($xml->ErrCount > 0) {
  $error = $xml->errors[0]->Err1;
  if (strcmp($error, "Domain name not found") === 0) {
    echo "nohost";
  } elseif (strcmp($error, "Passwords do not match") === 0) {
    echo "badauth";
  } elseif (strcmp($error, "No Records updated. A record not Found;") === 0) {
    echo "nohost";
  } else {
    echo "911 [".$error."]";
  }
} else {
  echo "good";
}

@Dade-R
Copy link

Dade-R commented Nov 19, 2021

Hi Guys, I found the strange solution. Apparently namecheap changed it's respone-type-encoding. This is my new working file. I added following code: $xml = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $res); Here's the whole file.

Well done @bloedboemmel. Confirmed working on my system. Maybe submit this as a Pull Request to @superbob? Unless he wants to make the change directly himself.

@tm1rules
Copy link

tm1rules commented Nov 20, 2021

Great work @bloedboemmel, just updated and is working perfectly on my NAS! I would add to anyone else reading this, remember to update the second line also;
$xml = new SimpleXMLElement($xml);

@superbob
Copy link
Author

Thx for reporting the issues and finding the solution. I'll update the code accordingly 😊

@kayserj
Copy link

kayserj commented Nov 20, 2021

Script updates confirmed working on DSM 7.0.1-42218
Once again, many thanks.

@aalejaandro
Copy link

aalejaandro commented Jan 14, 2022

Hi @superbob, Im still with the Error message, I followed the @obviouscap instructions, and also Did the @bloedboemmel modifications, and still the same.

Im trying to the domain as main, not subdomains and have same settings on namecheap as you ( A+ Dynamic DNS ) and my log show this:

[2.5] 2022-01-14T12:50:43-06:00 HuleNas can_export[951]: can_export.cpp:24 MediaServer INFO can_export[951]: hyperbackup [2.5] 2022-01-14T12:50:57-06:00 HuleNas ddnsd[4310]: ddnsd.c:267 DDNS hostname update error: 2022-01-14T12:50:57-06:00 HuleNas ddnsd[4310]: ddnsd.c:1938 Fail to update [alejandrom.com] with IP [189.235.83.139] at [Namecheap2] 2022-01-14T12:50:59-06:00 HuleNas ddnsd[4310]: ddnsd.c:1933 Success to update [alejandro.media] with IP [189.235.83.139] at [Cloudflare] 2022-01-14T12:51:00-06:00 HuleNas ddnsd[4310]: ddnsd.c:1933 Success to update [hule.synology.me] with IP [189.235.83.139/2806:104e:19:40d0::4] at [Synology]

This is my ddns-provider.conf

[Namecheap2] modulepath=/usr/syno/bin/ddns/namecheap.php queryurl=https://dynamicdns.park-your-domain.com/update [Namecheap3] modulepath=/usr/syno/bin/ddns/namecheap-synology-ddns.php queryurl=https://dynamicdns.park-your-domain.com/

And namecheap.php
`#!/usr/bin/php

ErrCount > 0) { $error = $xml->errors[0]->Err1; if (strcmp($error, "Domain name not found") === 0) { echo "nohost"; } elseif (strcmp($error, "Passwords do not match") === 0) { echo "badauth"; } elseif (strcmp($error, "No Records updated. A record not Found;") === 0) { echo "nohost"; } else { echo "911 [".$error."]"; } } else { echo "good"; }` Looks like the IP is updated, but always shows the error message. DSM 7.0.1-42218

@superbob
Copy link
Author

@aalejaandro, I'm confused about the content of your ddns_provider.conf file, BTW, it is ddns_provider.conf, not ddns-provider.conf (underscore, not dash)
It should be only:

[Namecheap]
        modulepath=/usr/syno/bin/ddns/namecheap.php
        queryurl=https://dynamicdns.park-your-domain.com/update

I don't understand why there are Namecheap2 and Namecheap3 configurations.
I have changed my configuration after what @bloedboemmel found out and it works just fine (last DNS updated today).

@aalejaandro
Copy link

Thank you very much for your quick response @superbob.

I have 2 NameCheap configurations because I was trying with another Script, as well I wrote wrong the name of ddns_provider.conf when submit the post, thats why I have Namecheap2 and Namecheap3 with different queryurl and different modulepath.

Did you have any idea what Im doing wrong?

@bloedboemmel
Copy link

@aalejaandro Is this your whole namecheap.php?
As @tm1rules mentioned, make sure to add these 2 lines above the if-error-clauses:

$xml = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $res);  
$xml = new SimpleXMLElement($xml);

@aalejaandro
Copy link

@bloedboemmel I already add these 2 lines, this is my entire namecheap.php
Captura6

guess that its fine.

Do you have domain working fine, with succesufully connection?

@bloedboemmel
Copy link

bloedboemmel commented Jan 15, 2022

My dns is updating fine without any errors since I added these lines.
I'm very sorry, I don't know why your getting an error.
What I did to find the error, was to run the namecheap.php with a debugger on my laptop. Maybe you could try this, too.

@aalejaandro
Copy link

The script works. But because my DNS entry is with the hostname "@", it does not work in DSM. @ is not recognized as hostname. @ is not recognized as a character and the field is a required field. I have simply adjusted the URL in the script and have replaced the hostname with @, so that it does not matter in the DSM what I enter in the hostname field. Now also no error comes with me. Maybe this helps others here too.

My url: $url = 'https://dynamicdns.park-your-domain.com/update?host=@&domain='.$hostname.'&password='.$pwd.'&ip='.$ip;

Thanks!.

Now works properly, updating IP and no error message.

@kimsonvu
Copy link

kimsonvu commented Mar 19, 2022

Hi,

@aalejaandro Is this your whole namecheap.php? As @tm1rules mentioned, make sure to add these 2 lines above the if-error-clauses:

$xml = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $res);  
$xml = new SimpleXMLElement($xml);

Thanks you! These 2 lines help me update to my Namecheap account the Dynamic IP. The problem that Synology DSM still saying "failed to connect to the server...."

But i am surely IP already update. How to fix this?

image

image

@aalejaandro
Copy link

Hi, its me again.

Everything was working good, but since the last update (DSM 7.1-42661 Update 2) Now my ddns that I added doesn't work. I thought with the new update the ddns_provider.conf was restore the default, but I saw that have no more entrys in that file.

So I would like to know if Im the only one with this issue or more people have same problem.

@superbob
Copy link
Author

Hi,
It's a usual thing that some files disapear after DSM updates. I don't really know what I could do about that. You just have to copy back files when it happens. Sorry for the inconvenience.

@aalejaandro
Copy link

Hi,
It's a usual thing that some files disapear after DSM updates. I don't really know what I could do about that. You just have to copy back files when it happens. Sorry for the inconvenience.

Yes, but at this time I can't make it work..

That's my entire namecheap.php guess that everything looks fine....

<?php

if ($argc !== 5) {
  echo 'badparam';
  exit();
}

$account = $argv[1];
$pwd = (string)$argv[2];
$hostname = (string)$argv[3];
$ip = (string)$argv[4];

// check the hostname contains '.'
if (strpos($hostname, '.') === false) {
  echo 'badparam';
  exit();
}

// only for IPv4 format
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  echo "badparam";
  exit();
}


$url = 'https://dynamicdns.park-your-domain.com/update?host=@&domain='.$hostname.'&password='.$pwd.'&ip='.$ip;

$req = curl_init();
curl_setopt($req, CURLOPT_URL, $url);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($req);
curl_close($req);

$xml = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $res);
$xml = new SimpleXMLElement($xml);
if ($xml->ErrCount > 0) {
  $error = $xml->errors[0]->Err1;
  if (strcmp($error, "Domain name not found") === 0) {
    echo "nohost";
  } elseif (strcmp($error, "Passwords do not match") === 0) {
    echo "badauth";
  } elseif (strcmp($error, "No Records updated. A record not Found;") === 0) {
    echo "nohost";
  } else {
    echo "911 [".$error."]";
  }
} else {
  echo "good";
}

This is at the end on my ddns_provider.conf

[Namecheap2]
        modulepath=/usr/syno/bin/ddns/namecheap.php
        queryurl=https://dynamicdns.park-your-domain.com/update
[Namecheap]
        modulepath=/usr/syno/bin/ddns/namecheap.php
        queryurl=https://dynamicdns.park-your-domain.com/update

I would be grateful if you see any error or detail that could be the reason why I cannot do it again

@superbob
Copy link
Author

That should not be a blocker but, there should be only one Namecheap block in the ddns_provider.conf file, you have two of them.

I don't see anything particular in the main php file besides the $url line which is different in the example file :
Yours:

$url = 'https://dynamicdns.park-your-domain.com/update?host=@&domain='.$hostname.'&password='.$pwd.'&ip='.$ip;

Mine:

$url = 'https://dynamicdns.park-your-domain.com/update?host='.$account.'&domain='.$hostname.'&password='.$pwd.'&ip='.$ip;

@aalejaandro
Copy link

That should not be a blocker but, there should be only one Namecheap block in the ddns_provider.conf file, you have two of them.

I don't see anything particular in the main php file besides the $url line which is different in the example file : Yours:

$url = 'https://dynamicdns.park-your-domain.com/update?host=@&domain='.$hostname.'&password='.$pwd.'&ip='.$ip;

Mine:

$url = 'https://dynamicdns.park-your-domain.com/update?host='.$account.'&domain='.$hostname.'&password='.$pwd.'&ip='.$ip;

I had 2 Namecheap (with different name) because I have 2 Domains, and DSM doesn't allow to use same provider with different domain, so the solution is add same prover with different name (Namecheap2).

Yes the $url is little different as suggest @bonxz, was the only way to make it work properly before.

Now, I did the same things as previous time and I can't fix the problem.

@superbob
Copy link
Author

That should not be a blocker but, there should be only one Namecheap block in the ddns_provider.conf file, you have two of them.
I don't see anything particular in the main php file besides the $url line which is different in the example file : Yours:

$url = 'https://dynamicdns.park-your-domain.com/update?host=@&domain='.$hostname.'&password='.$pwd.'&ip='.$ip;

Mine:

$url = 'https://dynamicdns.park-your-domain.com/update?host='.$account.'&domain='.$hostname.'&password='.$pwd.'&ip='.$ip;

I had 2 Namecheap (with different name) because I have 2 Domains, and DSM doesn't allow to use same provider with different domain, so the solution is add same prover with different name (Namecheap2).

Yes the $url is little different as suggest @bonxz, was the only way to make it work properly before.

Now, I did the same things as previous time and I can't fix the problem.

I don't know what's wrong with your configuration.
I use the lastest version of the files provided here (namecheap.php, ddns_provider.conf) without any modification and it works for me without error.

@tm1rules
Copy link

That should not be a blocker but, there should be only one Namecheap block in the ddns_provider.conf file, you have two of them.
I don't see anything particular in the main php file besides the $url line which is different in the example file : Yours:

$url = 'https://dynamicdns.park-your-domain.com/update?host=@&domain='.$hostname.'&password='.$pwd.'&ip='.$ip;

Mine:

$url = 'https://dynamicdns.park-your-domain.com/update?host='.$account.'&domain='.$hostname.'&password='.$pwd.'&ip='.$ip;

I had 2 Namecheap (with different name) because I have 2 Domains, and DSM doesn't allow to use same provider with different domain, so the solution is add same prover with different name (Namecheap2).

Yes the $url is little different as suggest @bonxz, was the only way to make it work properly before.

Now, I did the same things as previous time and I can't fix the problem.

Have you tried the following steps.. I just repeated them when my configuration broke upgrading to 7.1-42661 (Update 1):

  1. Open port 22 (see Terminal & SNMP in DSM) and open the firewall to the encrypted terminal service (See Security\Firewall in DSM)
  2. ssh into your admin account (I use powershell)
  3. sudo -i
  4. cd /usr/syno/bin/ddns/
  5. rm namecheap.php
  6. wget https://gist.github.com/superbob/c8d44537c08ba1f7f22e9ea757e31595/raw/51513ad51cf05b0b2be61f248fb674fe2c212013/namecheap.php
  7. chmod 755 namecheap.php
  8. exit
  9. exit
  10. Close port 22 (see Terminal & SNMP in DSM) and close the firewall to the encrypted terminal service (See Security\Firewall in DSM)

And I would then recommend just restarting your NAS..

@aalejaandro
Copy link

Thank you for your quick response.

Thanks for your Instructions/Steps.

Work this for you updating IP address with no error message on DSM?

Because I guess need to make some modifications to get it work, like add $xml = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $res); as @bloedboemmel says.

Before Update I remember to need to make this to get ir work.

@tm1rules
Copy link

Thank you for your quick response.

Thanks for your Instructions/Steps.

Work this for you updating IP address with no error message on DSM?

Because I guess need to make some modifications to get it work, like add $xml = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $res); as @bloedboemmel says.

Before Update I remember to need to make this to get ir work.

These are the steps I followed and my DDNS entries are being refreshed with no issues or errors. You do not need to amend the file if you follow the steps exactly.

@bloedboemmel
Copy link

That should not be a blocker but, there should be only one Namecheap block in the ddns_provider.conf file, you have two of them.

I don't see anything particular in the main php file besides the $url line which is different in the example file : Yours:

$url = 'https://dynamicdns.park-your-domain.com/update?host=@&domain='.$hostname.'&password='.$pwd.'&ip='.$ip;

Mine:

$url = 'https://dynamicdns.park-your-domain.com/update?host='.$account.'&domain='.$hostname.'&password='.$pwd.'&ip='.$ip;

Thank you for your quick response.

Thanks for your Instructions/Steps.

Work this for you updating IP address with no error message on DSM?

Because I guess need to make some modifications to get it work, like add $xml = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $res); as @bloedboemmel says.

Before Update I remember to need to make this to get ir work.

@aalejaandro Have you tried to change your url back to the original, because yours seems odd to me. Have you tried to paste the original url and your modified url in the browser? Maybe it shows another error.

My fix you mentioned shouldn't affect your issue, it fixed all our errors, when namecheap changed their return format. This time it strangely affects only one person.
My Synology hasn't updated yet, but I suggest to run the php file with a debugger and your input, so you can find any error.

@justinmcbride
Copy link

The script at the top of this gist (which now contains the modifications from @bloedboemmel) is working for me.
Couple of things that I found helpful:

  1. Reading the official Namecheap documentation about performing the HTTP GET call to the URL: https://www.namecheap.com/support/knowledgebase/article.aspx/29/11/how-to-dynamically-update-the-hosts-ip-with-an-http-request/
  2. The detailed instructions from @Dade-R which show the crucial step of adding the executable permission to the PHP file.

Great stuff, and thanks to all involved!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment