2020-11-11 09:39:00 -08:00
|
|
|
;;;; backplane-dns.lisp
|
2020-11-04 12:34:28 -08:00
|
|
|
|
2020-11-11 09:39:00 -08:00
|
|
|
(in-package #:backplane-dns)
|
2020-11-04 12:34:28 -08:00
|
|
|
|
2020-12-03 21:09:07 -08:00
|
|
|
(defvar *dns-store* nil)
|
2020-11-04 12:34:28 -08:00
|
|
|
|
2020-11-24 11:51:03 -08:00
|
|
|
(defun symbolize (str) (-> str string-upcase (intern :KEYWORD)))
|
|
|
|
|
2020-12-02 16:07:06 -08:00
|
|
|
(defclass dns-request ()
|
|
|
|
((hostname :initarg :hostname)
|
|
|
|
(domain :initarg :domain)
|
2020-12-03 21:09:07 -08:00
|
|
|
(msg-id :initarg :msg-id
|
|
|
|
:reader msg-id)))
|
2020-12-02 16:07:06 -08:00
|
|
|
|
|
|
|
(defclass request-change-ipv4 (dns-request)
|
|
|
|
((ip-address :initarg :ip-address)))
|
|
|
|
|
|
|
|
(defclass request-change-ipv6 (dns-request)
|
|
|
|
((ip-address :initarg :ip-address)))
|
|
|
|
|
|
|
|
(defclass request-change-sshfp (dns-request)
|
|
|
|
((sshfp :initarg :sshfp)))
|
|
|
|
|
|
|
|
(defclass unknown-dns-request (dns-request)
|
2020-12-03 21:09:07 -08:00
|
|
|
((request-type :initarg :request-type
|
|
|
|
:reader request-type)))
|
2020-11-11 08:59:40 -08:00
|
|
|
|
2020-11-11 10:52:09 -08:00
|
|
|
(defparameter *hostname-rx*
|
2020-11-11 14:58:07 -08:00
|
|
|
"(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])")
|
2020-11-11 10:52:09 -08:00
|
|
|
|
2020-12-03 21:09:07 -08:00
|
|
|
(define-condition backplane-dns-error (error)
|
|
|
|
((msg :initarg :msg :initform nil :reader error-msg)))
|
2020-11-13 09:42:17 -08:00
|
|
|
|
2020-12-03 21:09:07 -08:00
|
|
|
(define-condition invalid-hostname (backplane-dns-error)
|
|
|
|
((hostname :initarg :hostname :reader invalid-hostname)))
|
2020-11-04 12:34:28 -08:00
|
|
|
|
|
|
|
(let ((hostname-extractor-rx
|
|
|
|
(cl-ppcre:create-scanner
|
|
|
|
`(:SEQUENCE :START-ANCHOR "host-"
|
|
|
|
(:REGISTER (:REGEX ,*hostname-rx*))
|
|
|
|
#\@
|
|
|
|
(:REGEX ,*hostname-rx*) :END-ANCHOR))))
|
|
|
|
(defun sender-hostname (sender)
|
2020-12-03 21:09:07 -08:00
|
|
|
(if-let ((hostname (cl-ppcre:register-groups-bind (extracted-hostname nil)
|
|
|
|
(hostname-extractor-rx sender)
|
|
|
|
extracted-hostname)))
|
|
|
|
hostname
|
|
|
|
(error 'invalid-hostname
|
|
|
|
:hostname sender
|
|
|
|
:msg (format nil "unable to extract hostname from ~A, expecting format host-<hostname>"
|
|
|
|
sender)))))
|
|
|
|
|
|
|
|
(defmethod backplane-server:handle-message ((message dns-request))
|
|
|
|
(handler-case
|
|
|
|
(if-let ((store *dns-store*))
|
|
|
|
(-> message
|
|
|
|
(handle-dns-message store)
|
|
|
|
(handle-dns-response (msg-id message)))
|
|
|
|
(make-error :msg "dns store is not initialized"
|
|
|
|
:msg-id (msg-id message)))
|
|
|
|
(domain-name-missing (err)
|
|
|
|
(make-error :msg (format nil "missing domain name: ~A"
|
|
|
|
(missing-domain err))
|
|
|
|
:msg-id (msg-id message)))
|
|
|
|
(invalid-ip (err)
|
|
|
|
(make-error :msg (error-msg err)
|
|
|
|
:msg-id (msg-id message)))
|
|
|
|
(invalid-sshfp (err)
|
|
|
|
(make-error :msg (format nil "invalid ssh fingerprint: ~A"
|
|
|
|
(invalid-sshfp err))
|
|
|
|
:msg-id (msg-id message)))
|
|
|
|
(error (e)
|
|
|
|
(declare (ignorable e))
|
|
|
|
(make-error :msg (format nil "an unknown error occurred: ~A"
|
|
|
|
e)
|
|
|
|
:msg-id (msg-id message)))))
|
|
|
|
|
|
|
|
(defgeneric handle-dns-message (message store))
|
|
|
|
|
|
|
|
(defmethod handle-dns-message ((message request-change-ipv4) store)
|
|
|
|
(with-slots (hostname domain ip-address) message
|
|
|
|
(backplane-dns-store:set-ipv4 store domain hostname ip-address)
|
|
|
|
(make-instance 'dns-success
|
|
|
|
:msg (format nil "successfully set ipv4 for ~A.~A to ~A"
|
|
|
|
hostname domain ip-address))))
|
|
|
|
|
|
|
|
(defmethod handle-dns-message ((message request-change-ipv6) store)
|
|
|
|
(with-slots (hostname domain ip-address) message
|
|
|
|
(backplane-dns-store:set-ipv6 store domain hostname ip-address)
|
|
|
|
(make-instance 'dns-success
|
|
|
|
:msg (format nil "successfully set ipv6 for ~A.~A to ~A"
|
|
|
|
hostname domain ip-address))))
|
|
|
|
|
|
|
|
(defmethod handle-dns-message ((message request-change-sshfp) store)
|
|
|
|
(with-slots (hostname domain sshfp) message
|
|
|
|
(backplane-dns-store:set-sshfp store domain hostname sshfp)
|
|
|
|
(make-instance 'dns-success
|
|
|
|
:msg (format nil "successfully set sshfps for ~A.~A"
|
|
|
|
hostname domain))))
|
|
|
|
|
|
|
|
(defmethod handle-dns-message ((message unknown-dns-request) store)
|
|
|
|
(make-instance 'dns-error
|
|
|
|
:msg (format nil "unknown request to the dns service: ~A"
|
|
|
|
(request-type message))))
|
2020-11-13 09:42:17 -08:00
|
|
|
|
2020-11-13 17:27:44 -08:00
|
|
|
(defgeneric parse-dns-message (sender request message msg-id)
|
2020-11-13 11:11:58 -08:00
|
|
|
(:documentation "Parse a DNS service message of type REQUEST"))
|
|
|
|
|
2020-11-13 17:27:44 -08:00
|
|
|
(defmethod parse-dns-message (sender (request (eql :CHANGE_IPV4)) message msg-id)
|
2020-12-02 16:07:06 -08:00
|
|
|
(make-instance 'request-change-ipv4
|
2020-11-13 11:11:58 -08:00
|
|
|
:hostname (sender-hostname sender)
|
|
|
|
:domain (cdr (assoc :DOMAIN message))
|
2020-12-02 16:07:06 -08:00
|
|
|
:ip-address (cdr (assoc :IP message))
|
|
|
|
:msg-id msg-id))
|
2020-11-13 11:11:58 -08:00
|
|
|
|
2020-11-13 17:27:44 -08:00
|
|
|
(defmethod parse-dns-message (sender (request (eql :CHANGE_IPV6)) message msg-id)
|
2020-12-02 16:07:06 -08:00
|
|
|
(make-instance 'request-change-ipv6
|
2020-11-13 11:11:58 -08:00
|
|
|
:hostname (sender-hostname sender)
|
|
|
|
:domain (cdr (assoc :DOMAIN message))
|
2020-12-02 16:07:06 -08:00
|
|
|
:ip-address (cdr (assoc :IP message))
|
|
|
|
:msg-id msg-id))
|
2020-11-13 11:11:58 -08:00
|
|
|
|
2020-11-17 14:27:19 -08:00
|
|
|
(defmethod parse-dns-message (sender (request (eql :CHANGE_SSHFP)) message msg-id)
|
2020-12-02 16:07:06 -08:00
|
|
|
(make-instance 'request-change-sshfp
|
2020-11-17 14:27:19 -08:00
|
|
|
:hostname (sender-hostname sender)
|
|
|
|
:domain (cdr (assoc :DOMAIN message))
|
2020-12-02 16:07:06 -08:00
|
|
|
:sshfp (cdr (assoc :SSHFP message))
|
|
|
|
:msg-id msg-id))
|
2020-11-17 14:27:19 -08:00
|
|
|
|
2020-11-13 17:27:44 -08:00
|
|
|
(defmethod parse-dns-message (sender request message msg-id)
|
2020-12-03 21:09:07 -08:00
|
|
|
(make-instance 'unknown-dns-request
|
2020-12-02 16:07:06 -08:00
|
|
|
:request-type request
|
|
|
|
:msg-id msg-id))
|
2020-11-13 11:11:58 -08:00
|
|
|
|
2020-12-02 15:22:31 -08:00
|
|
|
(defmethod backplane-server:parse-message (sender (service (eql :DNS)) api-version message msg-id)
|
2020-11-13 17:27:44 -08:00
|
|
|
(parse-dns-message sender (symbolize (cdr (assoc :REQUEST message))) message msg-id))
|
2020-11-04 12:34:28 -08:00
|
|
|
|
2020-12-03 21:09:07 -08:00
|
|
|
(defclass dns-response ()
|
|
|
|
((msg :initarg :msg :reader msg)))
|
|
|
|
(defclass dns-success (dns-response) ())
|
|
|
|
(defclass dns-error (dns-response) ())
|
|
|
|
|
|
|
|
(defgeneric handle-dns-response (resp msg-id))
|
|
|
|
(defmethod handle-dns-response ((resp dns-success) msg-id)
|
|
|
|
(make-success :msg (msg resp) :msg-id msg-id))
|
|
|
|
(defmethod handle-dns-response ((resp dns-error) msg-id)
|
|
|
|
(make-error :msg (msg resp) :msg-id msg-id))
|
|
|
|
|
2020-11-11 12:55:57 -08:00
|
|
|
(defun backplane-dns-listen (&key
|
|
|
|
xmpp-host
|
|
|
|
xmpp-username
|
|
|
|
xmpp-password
|
|
|
|
db-host
|
2022-02-01 10:06:54 -08:00
|
|
|
db-port
|
2020-11-11 12:55:57 -08:00
|
|
|
db-name
|
|
|
|
db-username
|
2022-02-01 10:06:54 -08:00
|
|
|
db-password
|
|
|
|
db-use-ssl)
|
2022-02-02 14:39:31 -08:00
|
|
|
(format t "Got XMPP password: ~A" xmpp-password)
|
2020-11-16 20:39:02 -08:00
|
|
|
(let ((postmodern:*ignore-unknown-columns* t)
|
2020-12-03 21:09:07 -08:00
|
|
|
(cl-postgres:*query-log* *standard-output*)
|
|
|
|
(*dns-store* (make-instance 'backplane-dns-store:postgres-dns-store)))
|
2022-02-01 10:28:48 -08:00
|
|
|
(postmodern:with-connection (list db-name db-username db-password db-host
|
|
|
|
:port db-port
|
|
|
|
:use-ssl (symbolize db-use-ssl))
|
2020-11-24 11:51:03 -08:00
|
|
|
(with-backplane (backplane (backplane-connect xmpp-host xmpp-username xmpp-password))
|
2020-12-03 21:09:07 -08:00
|
|
|
(start-listening backplane)))))
|
2020-11-04 12:34:28 -08:00
|
|
|
|
|
|
|
(defun read-file-line (filename)
|
|
|
|
(let ((input (open filename :if-does-not-exist nil)))
|
2020-11-16 20:11:45 -08:00
|
|
|
(if input
|
|
|
|
(read-line input)
|
|
|
|
(uiop:die 1 "unable to read file: ~A" filename))))
|
2020-11-04 12:34:28 -08:00
|
|
|
|
2020-12-03 21:09:07 -08:00
|
|
|
(defun getenv-or-fail (env-var &optional default)
|
|
|
|
(let ((value (uiop:getenv env-var)))
|
|
|
|
(if (null value)
|
|
|
|
(if default
|
|
|
|
default
|
|
|
|
(uiop:die 1 "unable to find required env var: ~A" env-var))
|
|
|
|
value)))
|
|
|
|
|
2020-11-11 10:52:09 -08:00
|
|
|
(defun start-listener-with-env ()
|
2020-11-11 11:56:02 -08:00
|
|
|
(backplane-dns-listen
|
2020-11-11 12:55:57 -08:00
|
|
|
:xmpp-host (getenv-or-fail "FUDO_DNS_BACKPLANE_XMPP_HOSTNAME")
|
|
|
|
:xmpp-username (getenv-or-fail "FUDO_DNS_BACKPLANE_XMPP_USERNAME")
|
|
|
|
:xmpp-password (read-file-line (getenv-or-fail "FUDO_DNS_BACKPLANE_XMPP_PASSWORD_FILE"))
|
|
|
|
|
|
|
|
:db-name (getenv-or-fail "FUDO_DNS_BACKPLANE_DATABASE_NAME")
|
|
|
|
:db-username (getenv-or-fail "FUDO_DNS_BACKPLANE_DATABASE_USERNAME")
|
|
|
|
:db-host (getenv-or-fail "FUDO_DNS_BACKPLANE_DATABASE_HOSTNAME")
|
2022-02-01 10:06:54 -08:00
|
|
|
:db-port (getenv-or-fail "FUDO_DNS_BACKPLANE_DATABASE_PORT" 5432)
|
|
|
|
:db-password (read-file-line (getenv-or-fail "FUDO_DNS_BACKPLANE_DATABASE_PASSWORD_FILE"))
|
2022-02-01 10:28:48 -08:00
|
|
|
:db-use-ssl (getenv-or-fail "FUDO_DNS_BACKPLANE_DATABASE_USE_SSL" "no")))
|