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 #:bind-hostname #:bind-record #:bind-format-record
43 #:bind-record-type #:bind-record-format-args
44 #:timespec-seconds #:make-zone-serial))
48 ;;;--------------------------------------------------------------------------
49 ;;; Various random utilities.
52 "Convert X to an integer in the most straightforward way."
55 (defun from-mixed-base (base val)
56 "BASE is a list of the ranges for the `digits' of a mixed-base
57 representation. Convert VAL, a list of digits, into an integer."
58 (do ((base base (cdr base))
59 (val (cdr val) (cdr val))
60 (a (car val) (+ (* a (car base)) (car val))))
61 ((or (null base) (null val)) a)))
63 (defun to-mixed-base (base val)
64 "BASE is a list of the ranges for the `digits' of a mixed-base
65 representation. Convert VAL, an integer, into a list of digits."
66 (let ((base (reverse base))
72 (multiple-value-bind (q r) (floor val (pop base))
76 (defun timespec-seconds (ts)
77 "Convert a timespec TS to seconds. A timespec may be a real count of
78 seconds, or a list (COUNT UNIT): UNIT may be any of a number of obvious
81 ((realp ts) (floor ts))
83 (error "Unknown timespec format ~A" ts))
85 (timespec-seconds (car ts)))
86 (t (+ (to-integer (* (car ts)
87 (case (intern (string-upcase
88 (stringify (cadr ts)))
90 ((s sec secs second seconds) 1)
91 ((m min mins minute minutes) 60)
92 ((h hr hrs hour hours) #.(* 60 60))
93 ((d dy dys day days) #.(* 24 60 60))
94 ((w wk wks week weeks) #.(* 7 24 60 60))
95 ((y yr yrs year years) #.(* 365 24 60 60))
96 (t (error "Unknown time unit ~A"
98 (timespec-seconds (cddr ts))))))
100 (defun hash-table-keys (ht)
101 "Return a list of the keys in hashtable HT."
103 (maphash (lambda (key val) (declare (ignore val)) (collect key)) ht)))
105 (defun iso-date (&optional time &key datep timep (sep #\ ))
106 "Construct a textual date or time in ISO format. The TIME is the universal
107 time to convert, which defaults to now; DATEP is whether to emit the date;
108 TIMEP is whether to emit the time, and SEP (default is space) is how to
111 (sec min hr day mon yr dow dstp tz)
112 (decode-universal-time (if (or (null time) (eq time :now))
115 (declare (ignore dow dstp tz))
116 (with-output-to-string (s)
118 (format s "~4,'0D-~2,'0D-~2,'0D" yr mon day)
122 (format s "~2,'0D:~2,'0D:~2,'0D" hr min sec)))))
124 ;;;--------------------------------------------------------------------------
127 (defstruct (soa (:predicate soap))
128 "Start-of-authority record information."
137 (defstruct (mx (:predicate mxp))
138 "Mail-exchange record information."
142 (defstruct (zone (:predicate zonep))
149 ;;;--------------------------------------------------------------------------
150 ;;; Zone defaults. It is intended that scripts override these.
153 (cffi:defcfun gethostname :int
157 (defvar *default-zone-source*
158 (let ((hn #+cmu (unix:unix-gethostname)
159 #+clisp (unix:get-host-name)
160 #+ecl (cffi:with-foreign-pointer-as-string (buffer 256 len)
161 (let ((rc (gethostname buffer len)))
163 (error "gethostname(2) failed (rc = ~A)." rc))))))
164 (and hn (concatenate 'string (canonify-hostname hn) ".")))
165 "The default zone source: the current host's name.")
167 (defvar *default-zone-refresh* (* 24 60 60)
168 "Default zone refresh interval: one day.")
170 (defvar *default-zone-admin* nil
171 "Default zone administrator's email address.")
173 (defvar *default-zone-retry* (* 60 60)
174 "Default znoe retry interval: one hour.")
176 (defvar *default-zone-expire* (* 14 24 60 60)
177 "Default zone expiry time: two weeks.")
179 (defvar *default-zone-min-ttl* (* 4 60 60)
180 "Default zone minimum TTL/negative TTL: four hours.")
182 (defvar *default-zone-ttl* (* 8 60 60)
183 "Default zone TTL (for records without explicit TTLs): 8 hours.")
185 (defvar *default-mx-priority* 50
186 "Default MX priority.")
188 ;;;--------------------------------------------------------------------------
189 ;;; Zone variables and structures.
191 (defvar *zones* (make-hash-table :test #'equal)
192 "Map of known zones.")
194 (defun zone-find (name)
195 "Find a zone given its NAME."
196 (gethash (string-downcase (stringify name)) *zones*))
198 (defun (setf zone-find) (zone name)
199 "Make the zone NAME map to ZONE."
200 (setf (gethash (string-downcase (stringify name)) *zones*) zone))
202 (defstruct (zone-record (:conc-name zr-))
210 (defstruct (zone-subdomain (:conc-name zs-))
211 "A subdomain. Slightly weird. Used internally by zone-process-records
212 below, and shouldn't escape."
217 (defvar *zone-output-path* *default-pathname-defaults*
218 "Pathname defaults to merge into output files.")
220 (defvar *preferred-subnets* nil
221 "Subnets to prefer when selecting defaults.")
223 ;;;--------------------------------------------------------------------------
224 ;;; Zone infrastructure.
226 (defun zone-file-name (zone type)
227 "Choose a file name for a given ZONE and TYPE."
228 (merge-pathnames (make-pathname :name (string-downcase zone)
229 :type (string-downcase type))
232 (defun zone-preferred-subnet-p (name)
233 "Answer whether NAME (a string or symbol) names a preferred subnet."
234 (member name *preferred-subnets* :test #'string-equal))
236 (defmacro preferred-subnet-case (&body clauses)
237 "CLAUSES have the form (SUBNETS . FORMS) -- evaluate the first FORMS whose
238 SUBNETS (a list or single symbol, not evaluated) are considered preferred
239 by zone-preferred-subnet-p. If SUBNETS is the symbol t then the clause
242 ,@(mapcar (lambda (clause)
243 (let ((subnets (car clause)))
244 (cons (cond ((eq subnets t)
247 `(or ,@(mapcar (lambda (subnet)
248 `(zone-preferred-subnet-p
252 `(zone-preferred-subnet-p ',subnets)))
256 (defun zone-process-records (rec ttl func)
257 "Sort out the list of records in REC, calling FUNC for each one. TTL is
258 the default time-to-live for records which don't specify one."
259 (labels ((sift (rec ttl)
260 (collecting (top sub)
266 (setf ttl (pop rec)))
268 (collect (make-zone-record :type r
273 (dolist (name (listify (car r)))
274 (collect (make-zone-subdomain :name name
279 (error "Unexpected record form ~A" (car r))))))))
280 (process (rec dom ttl)
281 (multiple-value-bind (top sub) (sift rec ttl)
282 (if (and dom (null top) sub)
283 (let ((preferred nil))
285 (when (some #'zone-preferred-subnet-p
286 (listify (zs-name s)))
289 (setf preferred (car sub)))
291 (process (zs-records preferred)
293 (zs-ttl preferred))))
296 (join-strings #\. (reverse dom))))))
298 (setf (zr-name zr) name)
301 (process (zs-records s)
302 (cons (zs-name s) dom)
304 (process rec nil ttl)))
306 (defun zone-parse-host (f zname)
307 "Parse a host name F: if F ends in a dot then it's considered absolute;
308 otherwise it's relative to ZNAME."
309 (setf f (stringify f))
310 (cond ((string= f "@") (stringify zname))
311 ((and (plusp (length f))
312 (char= (char f (1- (length f))) #\.))
313 (string-downcase (subseq f 0 (1- (length f)))))
314 (t (string-downcase (concatenate 'string f "."
315 (stringify zname))))))
316 (defun default-rev-zone (base bytes)
317 "Return the default reverse-zone name for the given BASE address and number
318 of fixed leading BYTES."
319 (join-strings #\. (collecting ()
320 (loop for i from (- 3 bytes) downto 0
321 do (collect (ipaddr-byte base i)))
322 (collect "in-addr.arpa"))))
324 (defun zone-name-from-net (net &optional bytes)
325 "Given a NET, and maybe the BYTES to use, convert to the appropriate
326 subdomain of in-addr.arpa."
327 (let ((ipn (net-get-as-ipnet net)))
328 (with-ipnet (net mask) ipn
330 (setf bytes (- 4 (ipnet-changeable-bytes mask))))
333 for i from (- 4 bytes) below 4
334 collect (logand #xff (ash net (* -8 i))))
335 (list "in-addr.arpa"))))))
337 (defun zone-net-from-name (name)
338 "Given a NAME in the in-addr.arpa space, convert it to an ipnet."
339 (let* ((name (string-downcase (stringify name)))
341 (suffix ".in-addr.arpa")
342 (sufflen (length suffix))
345 (end (- len sufflen)))
346 (unless (and (> len sufflen)
347 (string= name suffix :start1 end))
348 (error "`~A' not in ~A." name suffix))
351 for dot = (position #\. name :start start :end end)
352 for byte = (parse-integer name
355 do (setf addr (logior addr (ash byte (* 8 n))))
358 do (error "Can't deduce network from ~A." name)
360 do (setf start (1+ dot)))
361 (setf addr (ash addr (* 8 (- 4 n))))
362 (make-ipnet addr (* 8 n))))
364 (defun zone-parse-net (net name)
365 "Given a NET, and the NAME of a domain to guess from if NET is null, return
366 the ipnet for the network."
368 (net-get-as-ipnet net)
369 (zone-net-from-name name)))
371 (defun zone-cidr-delg-default-name (ipn bytes)
372 "Given a delegated net IPN and the parent's number of changing BYTES,
373 return the default deletate zone prefix."
374 (with-ipnet (net mask) ipn
378 for i from (1- bytes) downto 0
379 until (zerop (logand mask (ash #xff (* 8 i))))
380 collect (logand #xff (ash net (* -8 i))))))))
382 ;;;--------------------------------------------------------------------------
383 ;;; Serial numbering.
385 (defun make-zone-serial (name)
386 "Given a zone NAME, come up with a new serial number. This will (very
387 carefully) update a file ZONE.serial in the current directory."
388 (let* ((file (zone-file-name name :serial))
389 (last (with-open-file (in file
391 :if-does-not-exist nil)
394 (now (multiple-value-bind
395 (sec min hr dy mon yr dow dstp tz)
397 (declare (ignore sec min hr dow dstp tz))
399 (seq (cond ((not (equal now (cdr last))) 0)
400 ((< (car last) 99) (1+ (car last)))
401 (t (error "Run out of sequence numbers for ~A" name)))))
402 (safely-writing (out file)
404 ";; Serial number file for zone ~A~%~
405 ;; (LAST-SEQ DAY MONTH YEAR)~%~
409 (from-mixed-base '(100 100 100) (reverse (cons seq now)))))
411 ;;;--------------------------------------------------------------------------
412 ;;; Zone form parsing.
414 (defun zone-parse-head (head)
415 "Parse the HEAD of a zone form. This has the form
417 (NAME &key :source :admin :refresh :retry
418 :expire :min-ttl :ttl :serial)
420 though a singleton NAME needn't be a list. Returns the default TTL and an
421 soa structure representing the zone head."
425 (source *default-zone-source*)
426 (admin (or *default-zone-admin*
427 (format nil "hostmaster@~A" zname)))
428 (refresh *default-zone-refresh*)
429 (retry *default-zone-retry*)
430 (expire *default-zone-expire*)
431 (min-ttl *default-zone-min-ttl*)
433 (serial (make-zone-serial zname)))
436 (timespec-seconds ttl)
437 (make-soa :admin admin
438 :source (zone-parse-host source zname)
439 :refresh (timespec-seconds refresh)
440 :retry (timespec-seconds retry)
441 :expire (timespec-seconds expire)
442 :min-ttl (timespec-seconds min-ttl)
445 (defun zone-make-name (prefix zone-name)
446 (if (or (not prefix) (string= prefix "@"))
448 (let ((len (length prefix)))
449 (if (or (zerop len) (char/= (char prefix (1- len)) #\.))
450 (join-strings #\. (list prefix zone-name))
453 (defmacro defzoneparse (types (name data list
454 &key (prefix (gensym "PREFIX"))
455 (zname (gensym "ZNAME"))
456 (ttl (gensym "TTL")))
458 "Define a new zone record type (or TYPES -- a list of synonyms is
459 permitted). The arguments are as follows:
461 NAME The name of the record to be added.
463 DATA The content of the record to be added (a single object,
466 LIST A function to add a record to the zone. See below.
468 PREFIX The prefix tag used in the original form.
470 ZNAME The name of the zone being constructed.
472 TTL The TTL for this record.
474 You get to choose your own names for these. ZNAME, PREFIX and TTL are
475 optional: you don't have to accept them if you're not interested.
477 The LIST argument names a function to be bound in the body to add a new
478 low-level record to the zone. It has the prototype
480 (LIST &key :name :type :data :ttl :make-ptr-p)
482 These (except MAKE-PTR-P, which defaults to nil) default to the above
483 arguments (even if you didn't accept the arguments)."
484 (setf types (listify types))
485 (let* ((type (car types))
486 (func (intern (format nil "ZONE-PARSE/~:@(~A~)" type))))
487 (with-parsed-body (body decls doc) body
488 (with-gensyms (col tname ttype tttl tdata tmakeptrp i)
491 (setf (get ,i 'zone-parse) ',func))
492 (defun ,func (,prefix ,zname ,data ,ttl ,col)
495 (let ((,name (zone-make-name ,prefix ,zname)))
496 (flet ((,list (&key ((:name ,tname) ,name)
497 ((:type ,ttype) ,type)
498 ((:data ,tdata) ,data)
500 ((:make-ptr-p ,tmakeptrp) nil))
501 #+cmu (declare (optimize ext:inhibit-warnings))
502 (collect (make-zone-record :name ,tname
506 :make-ptr-p ,tmakeptrp)
511 (defun zone-parse-records (zone records)
512 (let ((zname (zone-name zone)))
513 (with-collection (rec)
514 (flet ((parse-record (zr)
515 (let ((func (or (get (zr-type zr) 'zone-parse)
516 (error "No parser for record ~A."
518 (name (and (zr-name zr) (stringify (zr-name zr)))))
525 (zone-process-records records
526 (zone-default-ttl zone)
528 (setf (zone-records zone) (nconc (zone-records zone) rec)))))
530 (defun zone-parse (zf)
531 "Parse a ZONE form. The syntax of a zone form is as follows:
534 ZONE-HEAD ZONE-RECORD*
537 ((NAME*) ZONE-RECORD*)
539 (multiple-value-bind (zname ttl soa) (zone-parse-head (car zf))
540 (let ((zone (make-zone :name zname
544 (zone-parse-records zone (cdr zf))
547 (defun zone-create (zf)
548 "Zone construction function. Given a zone form ZF, construct the zone and
549 add it to the table."
550 (let* ((zone (zone-parse zf))
551 (name (zone-name zone)))
552 (setf (zone-find name) zone)
555 (defmacro defzone (soa &rest zf)
556 "Zone definition macro."
557 `(zone-create '(,soa ,@zf)))
559 (defmacro defrevzone (head &rest zf)
560 "Define a reverse zone, with the correct name."
565 (when (and soa-args (integerp (car soa-args)))
566 (setf bytes (pop soa-args)))
567 `(zone-create '((,(zone-name-from-net net bytes) ,@soa-args) ,@zf)))))
569 ;;;--------------------------------------------------------------------------
570 ;;; Zone record parsers.
572 (defzoneparse :a (name data rec)
574 (rec :data (parse-ipaddr data) :make-ptr-p t))
576 (defzoneparse :svc (name data rec)
578 (rec :type :a :data (parse-ipaddr data)))
580 (defzoneparse :ptr (name data rec :zname zname)
582 (rec :data (zone-parse-host data zname)))
584 (defzoneparse :cname (name data rec :zname zname)
586 (rec :data (zone-parse-host data zname)))
588 (defzoneparse :mx (name data rec :zname zname)
589 ":mx ((HOST :prio INT :ip IPADDR)*)"
590 (dolist (mx (listify data))
592 (mxname &key (prio *default-mx-priority*) ip)
594 (let ((host (zone-parse-host mxname zname)))
595 (when ip (rec :name host :type :a :data (parse-ipaddr ip)))
596 (rec :data (cons host prio))))))
598 (defzoneparse :ns (name data rec :zname zname)
599 ":ns ((HOST :ip IPADDR)*)"
600 (dolist (ns (listify data))
604 (let ((host (zone-parse-host nsname zname)))
605 (when ip (rec :name host :type :a :data (parse-ipaddr ip)))
608 (defzoneparse :alias (name data rec :zname zname)
610 (dolist (a (listify data))
611 (rec :name (zone-parse-host a zname)
615 (defzoneparse :net (name data rec)
617 (dolist (net (listify data))
618 (let ((n (net-get-as-ipnet net)))
619 (rec :name (zone-parse-host "net" name)
622 (rec :name (zone-parse-host "mask" name)
624 :data (ipnet-mask n))
625 (rec :name (zone-parse-host "broadcast" name)
627 :data (ipnet-broadcast n)))))
629 (defzoneparse (:rev :reverse) (name data rec)
630 ":reverse ((NET :bytes BYTES) ZONE*)"
631 (setf data (listify data))
635 (setf net (zone-parse-net net name))
637 (setf bytes (ipnet-changeable-bytes (ipnet-mask net))))
638 (let ((seen (make-hash-table :test #'equal)))
639 (dolist (z (or (cdr data)
640 (hash-table-keys *zones*)))
641 (dolist (zr (zone-records (zone-find z)))
642 (when (and (eq (zr-type zr) :a)
644 (ipaddr-networkp (zr-data zr) net))
645 (let ((name (string-downcase
650 (collect (logand #xff (ash (zr-data zr)
653 (unless (gethash name seen)
654 (rec :name name :type :ptr
655 :ttl (zr-ttl zr) :data (zr-name zr))
656 (setf (gethash name seen) t)))))))))
658 (defzoneparse (:cidr-delegation :cidr) (name data rec)
659 ":cidr-delegation ((NET :bytes BYTES) (TARGET-NET [TARGET-ZONE])*)"
663 (setf net (zone-parse-net net name))
665 (setf bytes (ipnet-changeable-bytes (ipnet-mask net))))
666 (dolist (map (cdr data))
668 (tnet &optional tdom)
670 (setf tnet (zone-parse-net tnet name))
671 (unless (ipnet-subnetp net tnet)
672 (error "~A is not a subnet of ~A."
676 (with-ipnet (net mask) tnet
680 (append (reverse (loop
681 for i from (1- bytes) downto 0
682 until (zerop (logand mask
686 (ash net (* -8 i)))))
688 (setf tdom (string-downcase tdom))
689 (dotimes (i (ipnet-hosts tnet))
690 (let* ((addr (ipnet-host tnet i))
691 (tail (join-strings #\.
693 for i from 0 below bytes
696 (ash addr (* 8 i)))))))
697 (rec :name (format nil "~A.~A" tail name)
699 :data (format nil "~A.~A" tail tdom))))))))
701 ;;;--------------------------------------------------------------------------
702 ;;; Zone file output.
704 (defgeneric zone-write (format zone stream)
705 (:documentation "Write ZONE's records to STREAM in the specified FORMAT."))
707 (defvar *writing-zone* nil
708 "The zone currently being written.")
710 (defvar *zone-output-stream* nil
711 "Stream to write zone data on.")
713 (defmethod zone-write :around (format zone stream)
714 (let ((*writing-zone* zone)
715 (*zone-output-stream* stream))
718 (defun zone-save (zones &key (format :bind))
719 "Write the named ZONES to files. If no zones are given, write all the
722 (setf zones (hash-table-keys *zones*)))
725 (let ((zz (zone-find z)))
727 (error "Unknown zone `~A'." z))
728 (let ((stream (safely-open-output-stream safe
729 (zone-file-name z :zone))))
730 (zone-write format zz stream))))))
732 ;;;--------------------------------------------------------------------------
733 ;;; Bind format output.
735 (defun bind-hostname (hostname)
738 (let* ((h (string-downcase (stringify hostname)))
740 (r (string-downcase (zone-name *writing-zone*)))
742 (cond ((string= r h) "@")
744 (char= (char h (- hl rl 1)) #\.)
745 (string= h r :start1 (- hl rl)))
746 (subseq h 0 (- hl rl 1)))
747 (t (concatenate 'string h "."))))))
749 (defmethod zone-write ((format (eql :bind)) zone stream)
751 ;;; Zone file `~(~A~)'
757 (iso-date :now :datep t :timep t)
758 (zone-default-ttl zone))
759 (let* ((soa (zone-soa zone))
760 (admin (let* ((name (soa-admin soa))
761 (at (position #\@ name))
762 (copy (format nil "~(~A~)." name)))
764 (setf (char copy at) #\.))
767 ~A~30TIN SOA~40T~A ~A (
769 ~45T~10D~60T ;refresh
772 ~45T~10D )~60T ;min-ttl~2%"
773 (bind-hostname (zone-name zone))
774 (bind-hostname (soa-source soa))
781 (dolist (zr (zone-records zone))
782 (bind-record (zr-type zr) zr)))
784 (defgeneric bind-record (type zr))
786 (defun bind-format-record (name ttl type format args)
787 (format *zone-output-stream*
788 "~A~20T~@[~8D~]~30TIN ~A~40T~?~%"
790 (and (/= ttl (zone-default-ttl *writing-zone*))
792 (string-upcase (symbol-name type))
795 (defmethod bind-record (type zr)
796 (destructuring-bind (format &rest args)
797 (bind-record-format-args type (zr-data zr))
798 (bind-format-record (zr-name zr)
800 (bind-record-type type)
803 (defgeneric bind-record-type (type)
804 (:method (type) type))
806 (defgeneric bind-record-format-args (type data)
807 (:method ((type (eql :a)) data) (list "~A" (ipaddr-string data)))
808 (:method ((type (eql :ptr)) data) (list "~A" (bind-hostname data)))
809 (:method ((type (eql :cname)) data) (list "~A" (bind-hostname data)))
810 (:method ((type (eql :ns)) data) (list "~A" (bind-hostname data)))
811 (:method ((type (eql :mx)) data)
812 (list "~2D ~A" (cdr data) (bind-hostname (car data))))
813 (:method ((type (eql :txt)) data) (list "~S" (stringify data))))
815 ;;;----- That's all, folks --------------------------------------------------