dns-mode.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. ;;; dns-mode.el --- a mode for viewing/editing Domain Name System master files
  2. ;; Copyright (C) 2000-2001, 2004-2017 Free Software Foundation, Inc.
  3. ;; Author: Simon Josefsson <simon@josefsson.org>
  4. ;; Keywords: DNS master zone file SOA comm
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Use M-x dns-mode RET to invoke in master files.
  18. ;;
  19. ;; C-c C-s Increment SOA serial.
  20. ;; Understands YYYYMMDDNN, Unix time, and serial number formats,
  21. ;; and complains if it fail to find SOA serial.
  22. ;;; References:
  23. ;; RFC 1034, "DOMAIN NAMES - CONCEPTS AND FACILITIES", P. Mockapetris.
  24. ;; RFC 1035, "DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION", P. Mockapetris.
  25. ;; RFC 5155, "DNS Security (DNSSEC) Hashed Authenticated Denial of Existence"
  26. ;; RFC 6698, "The DNS-Based Authentication of Named Entities (DANE)
  27. ;; Transport Layer Security (TLS) Protocol: TLSA"
  28. ;;; Release history:
  29. ;; 2004-09-11 Posted on gnu.emacs.sources.
  30. ;; 2004-09-13 Ported to XEmacs.
  31. ;; 2004-09-14 Installed in Emacs CVS.
  32. ;;; Code:
  33. (defgroup dns-mode nil
  34. "DNS master file mode configuration."
  35. :group 'data)
  36. (defconst dns-mode-control-entities '("INCLUDE" "ORIGIN" "TTL")
  37. "Lists of strings with known DNS control entities.")
  38. (defconst dns-mode-classes '("IN" "CS" "CH" "HS")
  39. "List of strings with known DNS classes.")
  40. (defconst dns-mode-types '("A" "NS" "MD" "MF" "CNAME" "SOA" "MB" "MG" "MR"
  41. "NULL" "WKS" "PTR" "HINFO" "MINFO" "MX" "TXT"
  42. "RP" "AFSDB" "X25" "ISDN" "RT" "NSAP"
  43. "SIG" "KEY" "PX" "GPOS" "AAAA" "LOC" "NXT"
  44. "EID" "NIMLOC" "SRV" "ATMA" "NAPTR" "KX" "CERT"
  45. "A6" "DNAME" "SINK" "OPT" "APL" "DS" "SSHFP"
  46. "RRSIG" "NSEC" "DNSKEY" "UINFO" "UID" "GID"
  47. "UNSPEC" "TKEY" "TSIG" "IXFR" "AXFR" "MAILB"
  48. "MAILA" "TLSA" "NSEC3")
  49. "List of strings with known DNS types.")
  50. (defface dns-mode-control-entity '((t :inherit font-lock-keyword-face))
  51. "Face used for DNS control entities, e.g. $ORIGIN."
  52. :version "26.1"
  53. :group 'dns-mode)
  54. (defface dns-mode-bad-control-entity '((t :inherit font-lock-warning-face))
  55. "Face used for non-standard DNS control entities, e.g. $FOO."
  56. :version "26.1"
  57. :group 'dns-mode)
  58. (defface dns-mode-type '((t :inherit font-lock-type-face))
  59. "Face used for DNS types, e.g., SOA."
  60. :version "26.1"
  61. :group 'dns-mode)
  62. (defface dns-mode-class '((t :inherit font-lock-constant-face))
  63. "Face used for DNS classes, e.g., IN."
  64. :version "26.1"
  65. :group 'dns-mode)
  66. (defvar dns-mode-control-entity-face ''dns-mode-control-entity
  67. "Name of face used for control entities, e.g. $ORIGIN.")
  68. (make-obsolete-variable 'dns-mode-control-entity-face
  69. "customize the face `dns-mode-control-entity' instead."
  70. "26.1" 'set)
  71. (defvar dns-mode-bad-control-entity-face ''dns-mode-bad-control-entity
  72. "Name of face used for non-standard control entities, e.g. $FOO.")
  73. (make-obsolete-variable
  74. 'dns-mode-bad-control-entity-face
  75. "customize the face `dns-mode-bad-control-entity' instead."
  76. "26.1" 'set)
  77. (defvar dns-mode-type-face ''dns-mode-type
  78. "Name of face used for DNS types, e.g., SOA.")
  79. (make-obsolete-variable 'dns-mode-type-face
  80. "customize the face `dns-mode-type' instead."
  81. "26.1" 'set)
  82. (defvar dns-mode-class-face ''dns-mode-class
  83. "Name of face used for DNS classes, e.g., IN.")
  84. (make-obsolete-variable 'dns-mode-class
  85. "customize the face `dns-mode-class' instead."
  86. "26.1" 'set)
  87. (defcustom dns-mode-font-lock-keywords
  88. `((,(concat "^$" (regexp-opt dns-mode-control-entities))
  89. 0 ,dns-mode-control-entity-face)
  90. ("^$[a-z0-9A-Z]+" 0 ,dns-mode-bad-control-entity-face)
  91. (,(regexp-opt dns-mode-classes) 0 ,dns-mode-class-face)
  92. (,(regexp-opt dns-mode-types) 0 ,dns-mode-type-face))
  93. "Font lock keywords used to highlight text in DNS master file mode."
  94. :version "26.1"
  95. :type 'sexp
  96. :group 'dns-mode)
  97. (defcustom dns-mode-soa-auto-increment-serial t
  98. "Whether to increment the SOA serial number automatically.
  99. If this variable is t, the serial number is incremented upon each save of
  100. the file. If it is `ask', Emacs asks for confirmation whether it should
  101. increment the serial upon saving. If nil, serials must be incremented
  102. manually with \\[dns-mode-soa-increment-serial]."
  103. :type '(choice (const :tag "Always" t)
  104. (const :tag "Ask" ask)
  105. (const :tag "Never" nil))
  106. :group 'dns-mode)
  107. ;; Syntax table.
  108. (defvar dns-mode-syntax-table
  109. (let ((table (make-syntax-table)))
  110. (modify-syntax-entry ?\; "< " table)
  111. (modify-syntax-entry ?\n "> " table)
  112. table)
  113. "Syntax table in use in DNS master file buffers.")
  114. ;; Keymap.
  115. (defvar dns-mode-map
  116. (let ((map (make-sparse-keymap)))
  117. (define-key map "\C-c\C-s" 'dns-mode-soa-increment-serial)
  118. (define-key map "\C-c\C-e" 'dns-mode-ipv6-to-nibbles)
  119. map)
  120. "Keymap for DNS master file mode.")
  121. ;; Menu.
  122. (defvar dns-mode-menu nil
  123. "Menubar used in DNS master file mode.")
  124. (easy-menu-define dns-mode-menu dns-mode-map
  125. "DNS Menu."
  126. '("DNS"
  127. ["Increment SOA serial" dns-mode-soa-increment-serial t]
  128. ["Convert IPv6 address to nibbles" dns-mode-ipv6-to-nibbles t]))
  129. ;; Mode.
  130. ;;;###autoload
  131. (define-derived-mode dns-mode text-mode "DNS"
  132. "Major mode for viewing and editing DNS master files.
  133. This mode is inherited from text mode. It add syntax
  134. highlighting, and some commands for handling DNS master files.
  135. Its keymap inherits from `text-mode' and it has the same
  136. variables for customizing indentation. It has its own abbrev
  137. table and its own syntax table.
  138. Turning on DNS mode runs `dns-mode-hook'."
  139. (set (make-local-variable 'comment-start) ";")
  140. (set (make-local-variable 'comment-end) "")
  141. (set (make-local-variable 'comment-start-skip) ";+ *")
  142. (unless (featurep 'xemacs)
  143. (set (make-local-variable 'font-lock-defaults)
  144. '(dns-mode-font-lock-keywords nil nil ((?_ . "w")))))
  145. (add-hook 'before-save-hook 'dns-mode-soa-maybe-increment-serial
  146. nil t)
  147. (easy-menu-add dns-mode-menu dns-mode-map))
  148. ;;;###autoload (defalias 'zone-mode 'dns-mode)
  149. ;; Tools.
  150. ;;;###autoload
  151. (defun dns-mode-soa-increment-serial ()
  152. "Locate SOA record and increment the serial field."
  153. (interactive)
  154. (save-excursion
  155. (goto-char (point-min))
  156. (unless (re-search-forward
  157. (concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+"
  158. "[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t)
  159. (error "Cannot locate SOA record"))
  160. (if (re-search-forward (concat "\\<\\("
  161. ;; year
  162. "\\(198\\|199\\|20[0-9]\\)[0-9]"
  163. ;; month
  164. "\\(0[0-9]\\|10\\|11\\|12\\)"
  165. ;; day
  166. "\\([012][0-9]\\|30\\|31\\)"
  167. ;; counter
  168. "\\([0-9]\\{1,3\\}\\)"
  169. "\\)\\>")
  170. nil t)
  171. ;; YYYYMMDDIII format, one to three I's.
  172. (let* ((serial (match-string 1))
  173. (counterstr (match-string 5))
  174. (counter (string-to-number counterstr))
  175. (now (format-time-string "%Y%m%d"))
  176. (nowandoldserial (concat now counterstr)))
  177. (if (string< serial nowandoldserial)
  178. (let ((new (format "%s00" now)))
  179. (replace-match new nil nil nil 1)
  180. (message "Replaced old serial %s with %s" serial new))
  181. (if (string= serial nowandoldserial)
  182. (let ((new (format (format "%%s%%0%dd" (length counterstr))
  183. now (1+ counter))))
  184. (replace-match new nil nil nil 1)
  185. (message "Replaced old serial %s with %s" serial new))
  186. (error "Current SOA serial is in the future"))))
  187. (if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t)
  188. ;; Unix time
  189. (let* ((serial (match-string 1))
  190. (new (format-time-string "%s")))
  191. (if (not (string< serial new))
  192. (error "Current SOA serial is in the future")
  193. (replace-match new nil nil nil 1)
  194. (message "Replaced old serial %s with %s" serial new)))
  195. (if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t)
  196. ;; Just any serial number.
  197. (let* ((serial (match-string 1))
  198. (new (format "%d" (1+ (string-to-number serial)))))
  199. (replace-match new nil nil nil 1)
  200. (message "Replaced old serial %s with %s" serial new))
  201. (error "Cannot locate serial number in SOA record"))))))
  202. (defun dns-mode-soa-maybe-increment-serial ()
  203. "Increment SOA serial if needed.
  204. This function is run from `before-save-hook'."
  205. (when (and (buffer-modified-p)
  206. dns-mode-soa-auto-increment-serial
  207. (or (eq dns-mode-soa-auto-increment-serial t)
  208. (y-or-n-p "Increment SOA serial? ")))
  209. ;; If `dns-mode-soa-increment-serial' signals an error saving will
  210. ;; fail but that probably means that the serial should be fixed to
  211. ;; comply with the RFC anyway! -rfr
  212. (progn (dns-mode-soa-increment-serial)
  213. ;; We return nil in case this is used in write-contents-functions.
  214. nil)))
  215. (defun dns-mode-ipv6-to-nibbles (&optional negate-prefix)
  216. "Convert an IPv6 address around or before point.
  217. Replace the address by its ip6.arpa-representation for use in
  218. reverse zone files, placing the original address in the kill ring.
  219. The address can be: a complete address (no prefix designator);
  220. with a normal prefix designator (e.g. /48), in which case only
  221. the required number of nibbles are output; or with a negative
  222. prefix designator (e.g. /-112), in which case only the part of
  223. the address *not* covered by the absolute value of the prefix
  224. length is output, as a relative address (without \".ip6.arpa.\" at
  225. the end). This is useful when $ORIGIN is specified in the zone file.
  226. Optional prefix argument NEGATE-PREFIX negates the value of the
  227. detected prefix length.
  228. Examples:
  229. 2001:db8::12 =>
  230. 2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.
  231. 2001:db8::12/32 =>
  232. 8.b.d.0.1.0.0.2.ip6.arpa.
  233. 2001:db8::12/-32 =>
  234. 2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0
  235. ::42/112 (with prefix argument) =>
  236. 2.4.0.0"
  237. (interactive "P")
  238. (skip-syntax-backward " ")
  239. (skip-syntax-backward "w_.")
  240. (re-search-forward "\\([[:xdigit:]:]+\\)\\(/-?[0-9]\\{2,3\\}\\)?")
  241. (kill-new (match-string 0))
  242. (let ((address (match-string 1))
  243. (prefix-length (match-string 2)))
  244. (when prefix-length
  245. (setq prefix-length (string-to-number (substring prefix-length 1)))
  246. (if negate-prefix
  247. (setq prefix-length (- prefix-length))))
  248. (replace-match
  249. (save-match-data
  250. (dns-mode-reverse-and-expand-ipv6 address prefix-length)))))
  251. (defun dns-mode-reverse-and-expand-ipv6 (address &optional prefix-length)
  252. "Convert an IPv6 address to (parts of) an ip6.arpa nibble format.
  253. ADDRESS is an IPv6 address in the usual colon-separated
  254. format, without a prefix designator at the end.
  255. Optional PREFIX-LENGTH is a number whose absolute value is the
  256. length in bits of the network part of the address. If nil,
  257. return an absolute address representing the full IPv6 address.
  258. If positive, return an absolute address representing the network
  259. prefix indicated. If negative, return a relative address
  260. representing the host parts of the address with respect to the
  261. indicated network prefix.
  262. See `dns-mode-ipv6-to-nibbles' for examples."
  263. (let* ((chunks (split-string address ":"))
  264. (prefix-length-nibbles (if prefix-length
  265. (ceiling (abs prefix-length) 4)
  266. 32))
  267. (filler-chunks (- 8 (length (remove "" chunks))))
  268. (expanded-address
  269. (apply #'concat
  270. (cl-loop with filler-done = nil
  271. for chunk in chunks
  272. if (and (not filler-done)
  273. (string= "" chunk))
  274. append (prog1
  275. (cl-loop repeat filler-chunks
  276. collect "0000")
  277. (setq filler-done t))
  278. else
  279. if (not (string= "" chunk))
  280. collect (format "%04x"
  281. (string-to-number chunk 16)))))
  282. (rev-address-nibbles
  283. (nreverse (if (and prefix-length
  284. (cl-minusp prefix-length))
  285. (substring expanded-address prefix-length-nibbles)
  286. (substring expanded-address 0 prefix-length-nibbles)))))
  287. (with-temp-buffer
  288. (cl-loop for char across rev-address-nibbles
  289. do
  290. (insert char)
  291. (insert "."))
  292. (if (and prefix-length
  293. (cl-minusp prefix-length))
  294. (delete-char -1)
  295. (insert "ip6.arpa."))
  296. (insert " ")
  297. (buffer-string))))
  298. (provide 'dns-mode)
  299. ;;; dns-mode.el ends here