forked from diafygi/acme-tiny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenew_cron.py
122 lines (89 loc) · 2.81 KB
/
renew_cron.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import smtplib
import datetime
import sys
import os.path
from subprocess import Popen, PIPE
log_text = ""
domain = ""
def send_log():
global log_text
global domain
SERVER = "localhost"
FROM = "root@intx.cc"
TO = ["root"]
SUBJECT = "Certificate Renewal Information for " + domain + " [renew.py]"
message = "From: %s\nTo: %s\nSubject: %s\n\n%s" % (FROM, ", ".join(TO), SUBJECT, log_text)
server = smtplib.SMTP(SERVER)
server.set_debuglevel(0)
server.sendmail(FROM, TO, message)
server.quit()
def log(msg):
global log_text
log_text += "[%s] " % (datetime.datetime.now()) + msg + "\n"
def end(errn):
if (errn != 0):
log("Error [" + str(errn) + "] Abort Programm")
send_log()
exit()
def request_cert(req):
p = Popen(["python", "/home/letsencrypt/letsencrypt/acme_tiny.py", "--account-key", "/home/letsencrypt/letsencrypt/account.key", "--csr", req, "--acme-dir", "/srv/acme-challenge/"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
#p = Popen(["python", "./test.py", "--account-key ./account.key", "--csr " + req, "--acme-dir /srv/acme-challenge/"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate()
rc = p.returncode
log("acme_tiny.py [" + str(rc) + "] says:\n" + err)
if (rc != 0):
end(rc)
#log("Got cert: \n" + output + "\n")
return output
log("renew.py started")
argv = sys.argv
argc = len(argv)
if (argc < 2):
log("Missing command line arguments")
end(-1)
site = sys.argv[1]
log("Get certificate for: " + site)
domain = site
csrpath = "/home/letsencrypt/letsencrypt/requests/" + site + ".csr"
if (os.path.isfile(csrpath)):
log("CSR found in: " + csrpath)
else:
log("No CSR found in: " + csrpath)
end(-1)
crtpath = "/home/letsencrypt/certs/" + site + ".crt"
if (os.path.isfile(crtpath)):
log("Old cert found in: " + crtpath)
else:
log("No old cert found in: " + crtpath)
end(-1)
cert = request_cert(csrpath)
log("Save cert temporarily to /home/letsencrypt/letsencrypt/tmp.crt")
try:
f = open('/home/letsencrypt/letsencrypt/tmp.crt', 'w')
f.seek(0)
f.truncate()
f.seek(0)
f.write(cert)
f.close()
except IOError:
send_log()
raise
log("Open intermediate cert")
f = open("/home/letsencrypt/certs/intermediate.crt", "r")
icrt = f.read()
f.close()
#log("Intermediate cert:\n" + icrt)
log("Write both to path of old cert")
try:
f = open(crtpath, "w")
f.seek(0)
f.truncate()
f.seek(0)
f.write(cert)
f.write(icrt)
f.close
except IOError:
send_log()
raise
log("Certificate renewed. Server needs to be restarted now")
end(0)