From 8a91146faebd196b223f7653e470428130f26246 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Mon, 29 Apr 2019 20:14:29 -0700 Subject: [PATCH] Initial commit. --- .editorconfig | 2 + .gitattributes | 5 ++ .gitignore | 84 +++++++++++++++++++++ .idea/copyright/Erik_s_Copyright_Notice.xml | 6 ++ .idea/copyright/profiles_settings.xml | 7 ++ .idea/encodings.xml | 6 ++ .idea/misc.xml | 7 ++ .idea/modules.xml | 8 ++ .idea/namesilo-letsencrypt.iml | 13 ++++ LICENSE.md | 12 +++ README.md | 45 +++++++++++ authenticator.py | 83 ++++++++++++++++++++ cleanup.py | 80 ++++++++++++++++++++ config.py | 2 + 14 files changed, 360 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .idea/copyright/Erik_s_Copyright_Notice.xml create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/namesilo-letsencrypt.iml create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 authenticator.py create mode 100644 cleanup.py create mode 100644 config.py diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a6971e1 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,2 @@ +[*] +insert_final_newline=true diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6ec2ae2 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +# Set the default behavior, in case people don't have core.autocrlf set. +* text=auto + +# batch files are specific to windows and always crlf +*.bat eol=crlf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..636e7ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,84 @@ +__pycache__ +!.vscode/extensions.json +!.vscode/launch.json +!.vscode/settings.json +!.vscode/tasks.json +!gradle-wrapper.jar +.classpath +.DS_Store +.gradle +.history +.idea_modules/ +.idea/**/contentModel.xml +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/dataSources/ +.idea/**/dbnavigator.xml +.idea/**/dictionaries +.idea/**/dynamic.xml +.idea/**/gradle.xml +.idea/**/libraries +.idea/**/mongoSettings.xml +.idea/**/shelf +.idea/**/sqlDataSources.xml +.idea/**/tasks.xml +.idea/**/uiDesigner.xml +.idea/**/usage.statistics.xml +.idea/**/workspace.xml +.idea/caches/build_file_checksums.ser +.idea/httpRequests +.idea/replstate.xml +.kobalt +.mtj.tmp/ +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +.nb-gradle +.project +.scannerwork +.settings +.vscode/* +*.class +*.code-workspace +*.ctxt +*.ear +*.iws +*.jar +*.log +*.nar +*.rar +*.sublime-* +*.tar.gz +*.war +*.zip +atlassian-ide-plugin.xml +bin/ +build/ +cmake-build-*/ +com_crashlytics_export_strings.xml +crashlytics-build.properties +crashlytics.properties +dependency-reduced-pom.xml +deploy/ +dist/ +ehthumbs.db +fabric.properties +gen/ +gradle.properties +hs_err_pid* +kobaltBuild +kobaltw*-test +lib/kotlin* +libs/ +local.properties +out/ +pom.xml.next +pom.xml.releaseBackup +pom.xml.tag +pom.xml.versionsBackup +proguard-project.txt +project.properties +release.properties +target/ +test-output +Thumbs.db +venv diff --git a/.idea/copyright/Erik_s_Copyright_Notice.xml b/.idea/copyright/Erik_s_Copyright_Notice.xml new file mode 100644 index 0000000..08660a1 --- /dev/null +++ b/.idea/copyright/Erik_s_Copyright_Notice.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..818172c --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..cfbc99a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..645d407 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/namesilo-letsencrypt.iml b/.idea/namesilo-letsencrypt.iml new file mode 100644 index 0000000..85c7612 --- /dev/null +++ b/.idea/namesilo-letsencrypt.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..6f831df --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,12 @@ +### Copyright © 2019, Erik C. Thauvin ([erik@thauvin.net](mailto:erik@thauvin.net)) +#### All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +* Neither the name of this project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.* diff --git a/README.md b/README.md new file mode 100644 index 0000000..d441caf --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ +# NameSilo Let's Encrypt + +Python scripts (hook) to automate obtaining [Let's Encrypt](https://letsencrypt.org/) certificates, +using [Certbot](https://certbot.eff.org/) DNS-01 challenge validation for domains DNS hosted on +[NameSilo](https://www.namesilo.com/). + +## Configuration + +Add you [NameSilo API key](https://www.namesilo.com/account_api.php) +to the top of the `config.py` file: + +```python +# Get your API Key from: https://www.namesilo.com/account_api.php +apikey = "YOUR_API_KEY" +``` + +Alternatively, the API key can be set in the `NAMESILO_API` environment variable. + +## Using with Certbot + +To issue a new certificate using the hook scripts, try something like: + +
+certbot certonly --manual --email you@example.com \
+--agree-tos --manual-public-ip-logging-ok \
+--preferred-challenges=dns \
+--manual-auth-hook /path/to/authenticator.py \
+--manual-cleanup-hook /path/to/cleanup.py \
+-d *.example.com -d example.com
+
+ +Or to renew an existing certificate: + +
+certbot renew --manual --email you@example.com \
+--agree-tos --manual-public-ip-logging-ok \
+--preferred-challenges=dns \
+--manual-auth-hook /path/to/authenticator.py \
+--manual-cleanup-hook /path/to/cleanup.py \
+-d *.example.com -d example.com
+
+ +Please note that NameSilo DNS propagation takes up to **15 minutes**, +so the scripts will wait 16 minutes before completing. + diff --git a/authenticator.py b/authenticator.py new file mode 100644 index 0000000..30d88e6 --- /dev/null +++ b/authenticator.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3.7 + +# authenticator.py +# +# Copyright (c) 2019, Erik C. Thauvin (erik@thauvin.net) +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# Neither the name of this project nor the names of its contributors may be +# used to endorse or promote products derived from this software without +# specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +import sys +import tempfile +import time +import urllib.request + +import untangle + +from config import apikey + +domain = os.environ['CERTBOT_DOMAIN'] +validation = os.environ['CERTBOT_VALIDATION'] +tmpdir = os.path.join(tempfile.gettempdir(), f"CERTBOT_{domain}") + +if "NAMESILO_API" in os.environ: + apikey = os.environ['NAMESILO_API'] + +url = f"https://www.namesilo.com/api/dnsAddRecord?\ +version=1&type=xml&key={apikey}&domain={domain}&rrtype=TXT\ +&rrhost=_acme-challenge&rrvalue={validation}&rrttl=3600" + +req = urllib.request.Request( + url, + data=None, + headers={ + 'User-Agent': ('Mozilla/5.0 (X11; CrOS x86_64 11647.154.0) ' + 'AppleWebKit/537.36 (KHTML, like Gecko) ' + 'Chrome/73.0.3683.114 Safari/537.36') + } +) + +with urllib.request.urlopen(req) as response: + html = response.read() +xml = untangle.parse(str(html, 'utf-8')) + +if not os.path.exists(tmpdir): + os.mkdir(tmpdir, 0o700) + +if xml.namesilo.reply.code.cdata == '300': + f = open(os.path.join(tmpdir, "RECORD_ID"), "a+") + print(xml.namesilo.reply.record_id.cdata, file=f) + f.close() +else: + print("{}: {} ({})".format(domain, + xml.namesilo.reply.detail.cdata, + xml.namesilo.reply.code.cdata), file=sys.stderr) + sys.exit(1) + +# Sleep 16 minutes +time.sleep(960) diff --git a/cleanup.py b/cleanup.py new file mode 100644 index 0000000..27d38c4 --- /dev/null +++ b/cleanup.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3.7 + +# cleanup.py +# +# Copyright (c) 2019, Erik C. Thauvin (erik@thauvin.net) +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# Neither the name of this project nor the names of its contributors may be +# used to endorse or promote products derived from this software without +# specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +import sys +import tempfile +import urllib.request + +import untangle + +from config import apikey + +domain = os.environ['CERTBOT_DOMAIN'] +tmpdir = os.path.join(tempfile.gettempdir(), f"CERTBOT_{domain}") + +if "NAMESILO_API" in os.environ: + apikey = os.environ['NAMESILO_API'] + +url = f"https://www.namesilo.com/api/dnsDeleteRecord\ +?version=1&type=xml&key={apikey}&domain={domain}&rrid=" + + +def getrequest(record_id): + return urllib.request.Request( + url + record_id, + data=None, + headers={ + 'User-Agent': ('Mozilla/5.0 (X11; CrOS x86_64 11647.154.0) ' + 'AppleWebKit/537.36 (KHTML, like Gecko) ' + 'Chrome/73.0.3683.114 Safari/537.36') + } + ) + + +idFile = os.path.join(tmpdir, "RECORD_ID") +if os.path.isfile(idFile): + f = open(idFile, "r") + for line in f: + with urllib.request.urlopen(getrequest(line.rstrip())) as response: + html = response.read() + xml = untangle.parse(str(html, 'utf-8')) + if xml.namesilo.reply.code.cdata != '300': + print("{}: {} ({})".format( + domain, + xml.namesilo.reply.detail.cdata, + xml.namesilo.reply.code.cdata), file=sys.stderr) + if xml.namesilo.reply.code.cdata != '280': + sys.exit(1) + f.close() + os.remove(idFile) diff --git a/config.py b/config.py new file mode 100644 index 0000000..b30c1c3 --- /dev/null +++ b/config.py @@ -0,0 +1,2 @@ +# Get your API Key from: https://www.namesilo.com/account_api.php +apikey = "YOUR_API_KEY"