5 ;;; DNS zone generation
7 ;;; (c) 2005 Straylight/Edgeware
10 ;;;----- Licensing notice ---------------------------------------------------
12 ;;; This program is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2 of the License, or
15 ;;; (at your option) any later version.
17 ;;; This program is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with this program; if not, write to the Free Software Foundation,
24 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 ;;;--------------------------------------------------------------------------
30 (:use #:common-lisp #:mdw.base #:mdw.str #:collect #:safely #:net)
31 (:export #:soa #:mx #:zone #:zone-record #:zone-subdomain
32 #:*default-zone-source* #:*default-zone-refresh*
33 #:*default-zone-retry* #:*default-zone-expire*
34 #:*default-zone-min-ttl* #:*default-zone-ttl*
35 #:*default-mx-priority* #:*default-zone-admin*
37 #:*preferred-subnets* #:zone-preferred-subnet-p
38 #:preferred-subnet-case
39 #:zone-find #:zone-parse #:zone-write #:zone-create #:defzone
40 #:defrevzone #:zone-save #:zone-make-name
41 #:defzoneparse #:zone-parse-host
42 #:timespec-seconds #:make-zone-serial))
46 ;;;--------------------------------------------------------------------------
47 ;;; Various random utilities.
50 "Convert X to an integer in the most straightforward way."
53 (defun from-mixed-base (base val)
54 "BASE is a list of the ranges for the `digits' of a mixed-base
55 representation. Convert VAL, a list of digits, into an integer."
56 (do ((base base (cdr base))
57 (val (cdr val) (cdr val))
58 (a (car val) (+ (* a (car base)) (car val))))
59 ((or (null base) (null val)) a)))
61 (defun to-mixed-base (base val)
62 "BASE is a list of the ranges for the `digits' of a mixed-base
63 representation. Convert VAL, an integer, into a list of digits."
64 (let ((base (reverse base))
70 (multiple-value-bind (q r) (floor val (pop base))
74 (defun timespec-seconds (ts)
75 "Convert a timespec TS to seconds. A timespec may be a real count of
76 seconds, or a list (COUNT UNIT): UNIT may be any of a number of obvious
79 ((realp ts) (floor ts))
81 (error "Unknown timespec format ~A" ts))
83 (timespec-seconds (car ts)))
84 (t (+ (to-integer (* (car ts)
85 (case (intern (string-upcase
86 (stringify (cadr ts)))
88 ((s sec secs second seconds) 1)
89 ((m min mins minute minutes) 60)
90 ((h hr hrs hour hours) #.(* 60 60))
91 ((d dy dys day days) #.(* 24 60 60))
92 ((w wk wks week weeks) #.(* 7 24 60 60))
93 ((y yr yrs year years) #.(* 365 24 60 60))
94 (t (error "Unknown time unit ~A"
96 (timespec-seconds (cddr ts))))))
98 (defun hash-table-keys (ht)
99 "Return a list of the keys in hashtable HT."
101 (maphash (lambda (key val) (declare (ignore val)) (collect key)) ht)))
103 (defun iso-date (&optional time &key datep timep (sep #\ ))
104 "Construct a textual date or time in ISO format. The TIME is the universal
105 time to convert, which defaults to now; DATEP is whether to emit the date;
106 TIMEP is whether to emit the time, and SEP (default is space) is how to
109 (sec min hr day mon yr dow dstp tz)
110 (decode-universal-time (if (or (null time) (eq time :now))
113 (declare (ignore dow dstp tz))
114 (with-output-to-string (s)
116 (format s "~4,'0D-~2,'0D-~2,'0D" yr mon day)
120 (format s "~2,'0D:~2,'0D:~2,'0D" hr min sec)))))
122 ;;;--------------------------------------------------------------------------
125 (defstruct (soa (:predicate soap))
126 "Start-of-authority record information."
135 (defstruct (mx (:predicate mxp))
136 "Mail-exchange record information."
140 (defstruct (zone (:predicate zonep))
147 ;;;--------------------------------------------------------------------------
148 ;;; Zone defaults. It is intended that scripts override these.
151 (cffi:defcfun gethostname :int
155 (defvar *default-zone-source*
156 (let ((hn #+cmu (unix:unix-gethostname)
157 #+clisp (unix:get-host-name)
158 #+ecl (cffi:with-foreign-pointer-as-string (buffer 256 len)
159 (let ((rc (gethostname buffer len)))
161 (error "gethostname(2) failed (rc = ~A)." rc))))))
162 (and hn (concatenate 'string (canonify-hostname hn) ".")))
163 "The default zone source: the current host's name.")
165 (defvar *default-zone-refresh* (* 24 60 60)
166 "Default zone refresh interval: one day.")
168 (defvar *default-zone-admin* nil
169 "Default zone administrator's email address.")
171 (defvar *default-zone-retry* (* 60 60)
172 "Default znoe retry interval: one hour.")
174 (defvar *default-zone-expire* (* 14 24 60 60)
175 "Default zone expiry time: two weeks.")
177 (defvar *default-zone-min-ttl* (* 4 60 60)
178 "Default zone minimum TTL/negative TTL: four hours.")
180 (defvar *default-zone-ttl* (* 8 60 60)
181 "Default zone TTL (for records without explicit TTLs): 8 hours.")
183 (defvar *default-mx-priority* 50
184 "Default MX priority.")
186 ;;;--------------------------------------------------------------------------
187 ;;; Zone variables and structures.
189 (defvar *zones* (make-hash-table :test #'equal)
190 "Map of known zones.")
192 (defun zone-find (name)
193 "Find a zone given its NAME."
194 (gethash (string-downcase (stringify name)) *zones*))
196 (defun (setf zone-find) (zone name)
197 "Make the zone NAME map to ZONE."
198 (setf (gethash (string-downcase (stringify name)) *zones*) zone))
200 (defstruct (zone-record (:conc-name zr-))
208 (defstruct (zone-subdomain (:conc-name zs-))
209 "A subdomain. Slightly weird. Used internally by zone-process-records
210 below, and shouldn't escape."
215 (defvar *zone-output-path* *default-pathname-defaults*
216 "Pathname defaults to merge into output files.")
218 (defvar *preferred-subnets* nil
219 "Subnets to prefer when selecting defaults.")
221 ;;;--------------------------------------------------------------------------
222 ;;; Zone infrastructure.
224 (defun zone-file-name (zone type)
225 "Choose a file name for a given ZONE and TYPE."
226 (merge-pathnames (make-pathname :name (string-downcase zone)
227 :type (string-downcase type))
230 (defun zone-preferred-subnet-p (name)
231 "Answer whether NAME (a string or symbol) names a preferred subnet."
232 (member name *preferred-subnets* :test #'string-equal))
234 (defmacro preferred-subnet-case (&body clauses)
235 "CLAUSES have the form (SUBNETS . FORMS) -- evaluate the first FORMS whose
236 SUBNETS (a list or single symbol, not evaluated) are considered preferred
237 by zone-preferred-subnet-p. If SUBNETS is the symbol t then the clause
240 ,@(mapcar (lambda (clause)
241 (let ((subnets (car clause)))
242 (cons (cond ((eq subnets t)
245 `(or ,@(mapcar (lambda (subnet)
246 `(zone-preferred-subnet-p
250 `(zone-preferred-subnet-p ',subnets)))
254 (defun zone-process-records (rec ttl func)
255 "Sort out the list of records in REC, calling FUNC for each one. TTL is
256 the default time-to-live for records which don't specify one."
257 (labels ((sift (rec ttl)
258 (collecting (top sub)
264 (setf ttl (pop rec)))
266 (collect (make-zone-record :type r
271 (dolist (name (listify (car r)))
272 (collect (make-zone-subdomain :name name
277 (error "Unexpected record form ~A" (car r))))))))
278 (process (rec dom ttl)
279 (multiple-value-bind (top sub) (sift rec ttl)
280 (if (and dom (null top) sub)
281 (let ((preferred nil))
283 (when (some #'zone-preferred-subnet-p
284 (listify (zs-name s)))
287 (setf preferred (car sub)))
289 (process (zs-records preferred)
291 (zs-ttl preferred))))
294 (join-strings #\. (reverse dom))))))
296 (setf (zr-name zr) name)
299 (process (zs-records s)
300 (cons (zs-name s) dom)
302 (process rec nil ttl)))
304 (defun zone-parse-host (f zname)
305 "Parse a host name F: if F ends in a dot then it's considered absolute;
306 otherwise it's relative to ZNAME."
307 (setf f (stringify f))
308 (cond ((string= f "@") (stringify zname))
309 ((and (plusp (length f))
310 (char= (char f (1- (length f))) #\.))
311 (string-downcase (subseq f 0 (1- (length f)))))
312 (t (string-downcase (concatenate 'string f "."
313 (stringify zname))))))
314 (defun default-rev-zone (base bytes)
315 "Return the default reverse-zone name for the given BASE address and number
316 of fixed leading BYTES."
317 (join-strings #\. (collecting ()
318 (loop for i from (- 3 bytes) downto 0
319 do (collect (ipaddr-byte base i)))
320 (collect "in-addr.arpa"))))
322 (defun zone-name-from-net (net &optional bytes)
323 "Given a NET, and maybe the BYTES to use, convert to the appropriate
324 subdomain of in-addr.arpa."
325 (let ((ipn (net-get-as-ipnet net)))
326 (with-ipnet (net mask) ipn
328 (setf bytes (- 4 (ipnet-changeable-bytes mask))))
331 for i from (- 4 bytes) below 4
332 collect (logand #xff (ash net (* -8 i))))
333 (list "in-addr.arpa"))))))
335 (defun zone-net-from-name (name)
336 "Given a NAME in the in-addr.arpa space, convert it to an ipnet."
337 (let* ((name (string-downcase (stringify name)))
339 (suffix ".in-addr.arpa")
340 (sufflen (length suffix))
343 (end (- len sufflen)))
344 (unless (and (> len sufflen)
345 (string= name suffix :start1 end))
346 (error "`~A' not in ~A." name suffix))
349 for dot = (position #\. name :start start :end end)
350 for byte = (parse-integer name
353 do (setf addr (logior addr (ash byte (* 8 n))))
356 do (error "Can't deduce network from ~A." name)
358 do (setf start (1+ dot)))
359 (setf addr (ash addr (* 8 (- 4 n))))
360 (make-ipnet addr (* 8 n))))
362 (defun zone-parse-net (net name)
363 "Given a NET, and the NAME of a domain to guess from if NET is null, return
364 the ipnet for the network."
366 (net-get-as-ipnet net)
367 (zone-net-from-name name)))
369 (defun zone-cidr-delg-default-name (ipn bytes)
370 "Given a delegated net IPN and the parent's number of changing BYTES,
371 return the default deletate zone prefix."
372 (with-ipnet (net mask) ipn
376 for i from (1- bytes) downto 0
377 until (zerop (logand mask (ash #xff (* 8 i))))
378 collect (logand #xff (ash net (* -8 i))))))))
380 (defun zone-cidr-delegation (data name ttl list)
381 "Given :cidr-delegation info DATA, for a record called NAME and the current
382 TTL, write lots of CNAME records to LIST."
386 (setf net (zone-parse-net net name))
388 (setf bytes (ipnet-changeable-bytes (ipnet-mask net))))
389 (dolist (map (cdr data))
391 (tnet &optional tdom)
393 (setf tnet (zone-parse-net tnet name))
394 (unless (ipnet-subnetp net tnet)
395 (error "~A is not a subnet of ~A."
401 (list (zone-cidr-delg-default-name tnet bytes)
403 (setf tdom (string-downcase tdom))
404 (dotimes (i (ipnet-hosts tnet))
405 (let* ((addr (ipnet-host tnet i))
406 (tail (join-strings #\.
408 for i from 0 below bytes
411 (ash addr (* 8 i)))))))
412 (collect (make-zone-record
413 :name (join-strings #\.
417 :data (join-strings #\. (list tail tdom)))
420 ;;;--------------------------------------------------------------------------
421 ;;; Serial numbering.
423 (defun make-zone-serial (name)
424 "Given a zone NAME, come up with a new serial number. This will (very
425 carefully) update a file ZONE.serial in the current directory."
426 (let* ((file (zone-file-name name :serial))
427 (last (with-open-file (in file
429 :if-does-not-exist nil)
432 (now (multiple-value-bind
433 (sec min hr dy mon yr dow dstp tz)
435 (declare (ignore sec min hr dow dstp tz))
437 (seq (cond ((not (equal now (cdr last))) 0)
438 ((< (car last) 99) (1+ (car last)))
439 (t (error "Run out of sequence numbers for ~A" name)))))
440 (safely-writing (out file)
442 ";; Serial number file for zone ~A~%~
443 ;; (LAST-SEQ DAY MONTH YEAR)~%~
447 (from-mixed-base '(100 100 100) (reverse (cons seq now)))))
449 ;;;--------------------------------------------------------------------------
450 ;;; Zone form parsing.
452 (defun zone-parse-head (head)
453 "Parse the HEAD of a zone form. This has the form
455 (NAME &key :source :admin :refresh :retry
456 :expire :min-ttl :ttl :serial)
458 though a singleton NAME needn't be a list. Returns the default TTL and an
459 soa structure representing the zone head."
463 (source *default-zone-source*)
464 (admin (or *default-zone-admin*
465 (format nil "hostmaster@~A" zname)))
466 (refresh *default-zone-refresh*)
467 (retry *default-zone-retry*)
468 (expire *default-zone-expire*)
469 (min-ttl *default-zone-min-ttl*)
471 (serial (make-zone-serial zname)))
474 (timespec-seconds ttl)
475 (make-soa :admin admin
476 :source (zone-parse-host source zname)
477 :refresh (timespec-seconds refresh)
478 :retry (timespec-seconds retry)
479 :expire (timespec-seconds expire)
480 :min-ttl (timespec-seconds min-ttl)
483 (defun zone-make-name (prefix zone-name)
484 (if (or (not prefix) (string= prefix "@"))
486 (let ((len (length prefix)))
487 (if (or (zerop len) (char/= (char prefix (1- len)) #\.))
488 (join-strings #\. (list prefix zone-name))
491 (defmacro defzoneparse (types (name data list
492 &key (prefix (gensym "PREFIX"))
493 (zname (gensym "ZNAME"))
494 (ttl (gensym "TTL")))
496 "Define a new zone record type (or TYPES -- a list of synonyms is
497 permitted). The arguments are as follows:
499 NAME The name of the record to be added.
501 DATA The content of the record to be added (a single object,
504 LIST A function to add a record to the zone. See below.
506 PREFIX The prefix tag used in the original form.
508 ZNAME The name of the zone being constructed.
510 TTL The TTL for this record.
512 You get to choose your own names for these. ZNAME, PREFIX and TTL are
513 optional: you don't have to accept them if you're not interested.
515 The LIST argument names a function to be bound in the body to add a new
516 low-level record to the zone. It has the prototype
518 (LIST &key :name :type :data :ttl :make-ptr-p)
520 These (except MAKE-PTR-P, which defaults to nil) default to the above
521 arguments (even if you didn't accept the arguments)."
522 (setf types (listify types))
523 (let* ((type (car types))
524 (func (intern (format nil "ZONE-PARSE/~:@(~A~)" type))))
525 (with-parsed-body (body decls doc) body
526 (with-gensyms (col tname ttype tttl tdata tmakeptrp i)
529 (setf (get ,i 'zone-parse) ',func))
530 (defun ,func (,prefix ,zname ,data ,ttl ,col)
533 (let ((,name (zone-make-name ,prefix ,zname)))
534 (flet ((,list (&key ((:name ,tname) ,name)
535 ((:type ,ttype) ,type)
536 ((:data ,tdata) ,data)
538 ((:make-ptr-p ,tmakeptrp) nil))
539 (collect (make-zone-record :name ,tname
543 :make-ptr-p ,tmakeptrp)
548 (defun zone-parse-records (zone records)
549 (let ((zname (zone-name zone)))
550 (with-collection (rec)
551 (flet ((parse-record (zr)
552 (let ((func (or (get (zr-type zr) 'zone-parse)
553 (error "No parser for record ~A."
555 (name (and (zr-name zr) (stringify (zr-name zr)))))
562 (zone-process-records records
563 (zone-default-ttl zone)
565 (setf (zone-records zone) (nconc (zone-records zone) rec)))))
567 (defun zone-parse (zf)
568 "Parse a ZONE form. The syntax of a zone form is as follows:
571 ZONE-HEAD ZONE-RECORD*
574 ((NAME*) ZONE-RECORD*)
576 (multiple-value-bind (zname ttl soa) (zone-parse-head (car zf))
577 (let ((zone (make-zone :name zname
581 (zone-parse-records zone (cdr zf))
584 (defun zone-create (zf)
585 "Zone construction function. Given a zone form ZF, construct the zone and
586 add it to the table."
587 (let* ((zone (zone-parse zf))
588 (name (zone-name zone)))
589 (setf (zone-find name) zone)
592 (defmacro defzone (soa &rest zf)
593 "Zone definition macro."
594 `(zone-create '(,soa ,@zf)))
596 (defmacro defrevzone (head &rest zf)
597 "Define a reverse zone, with the correct name."
602 (when (and soa-args (integerp (car soa-args)))
603 (setf bytes (pop soa-args)))
604 `(zone-create '((,(zone-name-from-net net bytes) ,@soa-args) ,@zf)))))
606 ;;;--------------------------------------------------------------------------
607 ;;; Zone record parsers.
609 (defzoneparse :a (name data rec)
611 (rec :data (parse-ipaddr data) :make-ptr-p t))
613 (defzoneparse :svc (name data rec)
615 (rec :type :a :data (parse-ipaddr data)))
617 (defzoneparse :ptr (name data rec :zname zname)
619 (rec :data (zone-parse-host data zname)))
621 (defzoneparse :cname (name data rec :zname zname)
623 (rec :data (zone-parse-host data zname)))
625 (defzoneparse :mx (name data rec :zname zname)
626 ":mx ((HOST :prio INT :ip IPADDR)*)"
627 (dolist (mx (listify data))
629 (mxname &key (prio *default-mx-priority*) ip)
631 (let ((host (zone-parse-host mxname zname)))
632 (when ip (rec :name host :type :a :data (parse-ipaddr ip)))
633 (rec :data (cons host prio))))))
635 (defzoneparse :ns (name data rec :zname zname)
636 ":ns ((HOST :ip IPADDR)*)"
637 (dolist (ns (listify data))
641 (let ((host (zone-parse-host nsname zname)))
642 (when ip (rec :name host :type :a :data (parse-ipaddr ip)))
645 (defzoneparse :alias (name data rec :zname zname)
647 (dolist (a (listify data))
648 (rec :name (zone-parse-host a zname)
652 (defzoneparse :net (name data rec)
654 (dolist (net (listify data))
655 (let ((n (net-get-as-ipnet net)))
656 (rec :name (zone-parse-host "net" name)
659 (rec :name (zone-parse-host "mask" name)
661 :data (ipnet-mask n))
662 (rec :name (zone-parse-host "broadcast" name)
664 :data (ipnet-broadcast n)))))
666 (defzoneparse (:rev :reverse) (name data rec)
667 ":reverse ((NET :bytes BYTES) ZONE*)"
668 (setf data (listify data))
672 (setf net (zone-parse-net net name))
674 (setf bytes (ipnet-changeable-bytes (ipnet-mask net))))
675 (let ((seen (make-hash-table :test #'equal)))
676 (dolist (z (or (cdr data)
677 (hash-table-keys *zones*)))
678 (dolist (zr (zone-records (zone-find z)))
679 (when (and (eq (zr-type zr) :a)
681 (ipaddr-networkp (zr-data zr) net))
682 (let ((name (string-downcase
687 (collect (logand #xff (ash (zr-data zr)
690 (unless (gethash name seen)
691 (rec :name name :type :ptr
692 :ttl (zr-ttl zr) :data (zr-name zr))
693 (setf (gethash name seen) t)))))))))
695 (defzoneparse (:cidr-delegation :cidr) (name data rec)
696 ":cidr-delegation ((NET :bytes BYTES) (TARGET-NET [TARGET-ZONE])*)"
700 (setf net (zone-parse-net net name))
702 (setf bytes (ipnet-changeable-bytes (ipnet-mask net))))
703 (dolist (map (cdr data))
705 (tnet &optional tdom)
707 (setf tnet (zone-parse-net tnet name))
708 (unless (ipnet-subnetp net tnet)
709 (error "~A is not a subnet of ~A."
713 (with-ipnet (net mask) tnet
717 (append (reverse (loop
718 for i from (1- bytes) downto 0
719 until (zerop (logand mask
723 (ash net (* -8 i)))))
725 (setf tdom (string-downcase tdom))
726 (dotimes (i (ipnet-hosts tnet))
727 (let* ((addr (ipnet-host tnet i))
728 (tail (join-strings #\.
730 for i from 0 below bytes
733 (ash addr (* 8 i)))))))
734 (rec :name (format nil "~A.~A" tail name)
736 :data (format nil "~A.~A" tail tdom))))))))
738 ;;;--------------------------------------------------------------------------
739 ;;; Zone file output.
741 (defun zone-write (zone &optional (stream *standard-output*))
742 "Write a ZONE's records to STREAM."
743 (labels ((fix-admin (a)
744 (let ((at (position #\@ a))
745 (s (concatenate 'string (string-downcase a) ".")))
747 (setf (char s at) #\.))
752 (let* ((h (string-downcase (stringify h)))
754 (r (string-downcase (zone-name zone)))
756 (cond ((string= r h) "@")
758 (char= (char h (- hl rl 1)) #\.)
759 (string= h r :start1 (- hl rl)))
760 (subseq h 0 (- hl rl 1)))
761 (t (concatenate 'string h "."))))))
763 (format stream "~A~20T~@[~8D~]~30TIN ~A~40T"
764 (fix-host (zr-name zr))
765 (and (/= (zr-ttl zr) (zone-default-ttl zone))
767 (string-upcase (symbol-name (zr-type zr))))))
769 ;;; Zone file `~(~A~)'
775 (iso-date :now :datep t :timep t)
776 (zone-default-ttl zone))
777 (let ((soa (zone-soa zone)))
779 ~A~30TIN SOA~40T~A ~A (
781 ~45T~10D~60T ;refresh
784 ~45T~10D )~60T ;min-ttl~2%"
785 (fix-host (zone-name zone))
786 (fix-host (soa-source soa))
787 (fix-admin (soa-admin soa))
793 (dolist (zr (zone-records zone))
797 (format stream "~A~%" (ipaddr-string (zr-data zr))))
800 (format stream "~A~%" (fix-host (zr-data zr))))
803 (let ((mx (zr-data zr)))
804 (format stream "~2D ~A~%" (cdr mx) (fix-host (car mx)))))
807 (format stream "~S~%" (stringify (zr-data zr))))))))
809 (defun zone-save (zones)
810 "Write the named ZONES to files. If no zones are given, write all the
813 (setf zones (hash-table-keys *zones*)))
816 (let ((zz (zone-find z)))
818 (error "Unknown zone `~A'." z))
819 (let ((stream (safely-open-output-stream safe
820 (zone-file-name z :zone))))
821 (zone-write zz stream))))))
823 ;;;----- That's all, folks --------------------------------------------------