services/dkim-key: do not overwrite existing keys

Also do not suggest publishing the private key.
This commit is contained in:
Timotej Lazar 2023-05-11 19:34:14 +02:00
parent 08003dc2aa
commit c7c0bdb7cb

View file

@ -23,19 +23,19 @@
(mkdir-p #$directory) (mkdir-p #$directory)
(let ((key-file (string-append #$directory "/" #$domain ".key")) (let ((key-file (string-append #$directory "/" #$domain ".key"))
(private (string-append #$domain "-" #$selector ".key"))) (private (string-append #$domain "-" #$selector ".key"))
(unless (file-exists? private) (public (string-append #$domain "-" #$selector ".pub")))
(with-directory-excursion #$directory (with-directory-excursion #$directory
(let ((openssl #$(file-append openssl "/bin/openssl")) (unless (file-exists? private)
(public (string-append #$domain "-" #$selector ".pub"))) (let ((openssl #$(file-append openssl "/bin/openssl")))
(system* openssl "genrsa" "-out" private (number->string #$key-length)) (system* openssl "genrsa" "-out" private (number->string #$key-length))
(chown private (passwd:uid (getpwnam "smtpd")) -1) (chown private (passwd:uid (getpwnam "smtpd")) -1)
(system* openssl "rsa" "-in" private "-pubout" "-out" public) (system* openssl "rsa" "-in" private "-pubout" "-out" public)
(system* "sed" "-i" (system* "sed" "-i"
"1s/.*/v=DKIM1;p=/;:nl;${s/-----.*//;q;};N;s/\\n//g;b nl;" "1s/.*/v=DKIM1;p=/;:nl;${s/-----.*//;q;};N;s/\\n//g;b nl;"
public)) public)))
(display (format #f "Create a TXT record at ~a._domainkey.~a with the contents of ~a~%" (display (format #f "Create a TXT record at ~a._domainkey.~a with the contents of ~a/~a~%"
#$selector #$domain key-file)))) #$selector #$domain #$directory public)))
(unless (equal? (false-if-exception (readlink current)) key-file) (unless (equal? (false-if-exception (readlink current)) key-file)
(delete-file key-file) (delete-file key-file)
(symlink private key-file)))))) (symlink private key-file))))))