dns-mode.el 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. ;;; dns-mode.el --- a mode for viewing/editing Domain Name System master files
  2. ;; Copyright (C) 2000-2001, 2004-2012 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. ;;; Release history:
  26. ;; 2004-09-11 Posted on gnu.emacs.sources.
  27. ;; 2004-09-13 Ported to XEmacs.
  28. ;; 2004-09-14 Installed in Emacs CVS.
  29. ;;; Code:
  30. (defgroup dns-mode nil
  31. "DNS master file mode configuration."
  32. :group 'data)
  33. (defconst dns-mode-classes '("IN" "CS" "CH" "HS")
  34. "List of strings with known DNS classes.")
  35. (defconst dns-mode-types '("A" "NS" "MD" "MF" "CNAME" "SOA" "MB" "MG" "MR"
  36. "NULL" "WKS" "PTR" "HINFO" "MINFO" "MX" "TXT"
  37. "RP" "AFSDB" "X25" "ISDN" "RT" "NSAP" "NSAP"
  38. "SIG" "KEY" "PX" "GPOS" "AAAA" "LOC" "NXT"
  39. "EID" "NIMLOC" "SRV" "ATMA" "NAPTR" "KX" "CERT"
  40. "A6" "DNAME" "SINK" "OPT" "APL" "DS" "SSHFP"
  41. "RRSIG" "NSEC" "DNSKEY" "UINFO" "UID" "GID"
  42. "UNSPEC" "TKEY" "TSIG" "IXFR" "AXFR" "MAILB"
  43. "MAILA")
  44. "List of strings with known DNS types.")
  45. ;; Font lock.
  46. (defvar dns-mode-control-entity-face 'font-lock-keyword-face
  47. "Name of face used for control entities, e.g. $ORIGIN.")
  48. (defvar dns-mode-bad-control-entity-face 'font-lock-warning-face
  49. "Name of face used for non-standard control entities, e.g. $FOO.")
  50. (defvar dns-mode-type-face 'font-lock-type-face
  51. "Name of face used for DNS types, e.g., SOA.")
  52. (defvar dns-mode-class-face 'font-lock-constant-face
  53. "Name of face used for DNS classes, e.g., IN.")
  54. (defcustom dns-mode-font-lock-keywords
  55. `(("^$ORIGIN" 0 ,dns-mode-control-entity-face)
  56. ("^$INCLUDE" 0 ,dns-mode-control-entity-face)
  57. ("^$[a-z0-9A-Z]+" 0 ,dns-mode-bad-control-entity-face)
  58. (,(regexp-opt dns-mode-classes) 0 ,dns-mode-class-face)
  59. (,(regexp-opt dns-mode-types) 0 ,dns-mode-type-face))
  60. "Font lock keywords used to highlight text in DNS master file mode."
  61. :type 'sexp
  62. :group 'dns-mode)
  63. (defcustom dns-mode-soa-auto-increment-serial t
  64. "Whether to increment the SOA serial number automatically.
  65. If this variable is t, the serial number is incremented upon each save of
  66. the file. If it is `ask', Emacs asks for confirmation whether it should
  67. increment the serial upon saving. If nil, serials must be incremented
  68. manually with \\[dns-mode-soa-increment-serial]."
  69. :type '(choice (const :tag "Always" t)
  70. (const :tag "Ask" ask)
  71. (const :tag "Never" nil))
  72. :group 'dns-mode)
  73. ;; Syntax table.
  74. (defvar dns-mode-syntax-table
  75. (let ((table (make-syntax-table)))
  76. (modify-syntax-entry ?\; "< " table)
  77. (modify-syntax-entry ?\n "> " table)
  78. table)
  79. "Syntax table in use in DNS master file buffers.")
  80. ;; Keymap.
  81. (defvar dns-mode-map
  82. (let ((map (make-sparse-keymap)))
  83. (define-key map "\C-c\C-s" 'dns-mode-soa-increment-serial)
  84. map)
  85. "Keymap for DNS master file mode.")
  86. ;; Menu.
  87. (defvar dns-mode-menu nil
  88. "Menubar used in DNS master file mode.")
  89. (easy-menu-define dns-mode-menu dns-mode-map
  90. "DNS Menu."
  91. '("DNS"
  92. ["Increment SOA serial" dns-mode-soa-increment-serial t]))
  93. ;; Mode.
  94. ;;;###autoload
  95. (define-derived-mode dns-mode text-mode "DNS"
  96. "Major mode for viewing and editing DNS master files.
  97. This mode is inherited from text mode. It add syntax
  98. highlighting, and some commands for handling DNS master files.
  99. Its keymap inherits from `text-mode' and it has the same
  100. variables for customizing indentation. It has its own abbrev
  101. table and its own syntax table.
  102. Turning on DNS mode runs `dns-mode-hook'."
  103. (set (make-local-variable 'comment-start) ";")
  104. (set (make-local-variable 'comment-end) "")
  105. (set (make-local-variable 'comment-start-skip) ";+ *")
  106. (unless (featurep 'xemacs)
  107. (set (make-local-variable 'font-lock-defaults)
  108. '(dns-mode-font-lock-keywords nil nil ((?_ . "w")))))
  109. (add-hook 'before-save-hook 'dns-mode-soa-maybe-increment-serial
  110. nil t)
  111. (easy-menu-add dns-mode-menu dns-mode-map))
  112. ;;;###autoload (defalias 'zone-mode 'dns-mode)
  113. ;; Tools.
  114. ;;;###autoload
  115. (defun dns-mode-soa-increment-serial ()
  116. "Locate SOA record and increment the serial field."
  117. (interactive)
  118. (save-excursion
  119. (goto-char (point-min))
  120. (unless (re-search-forward
  121. (concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+"
  122. "[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t)
  123. (error "Cannot locate SOA record"))
  124. (if (re-search-forward (concat "\\<\\("
  125. ;; year
  126. "\\(198\\|199\\|20[0-9]\\)[0-9]"
  127. ;; month
  128. "\\(0[0-9]\\|10\\|11\\|12\\)"
  129. ;; day
  130. "\\([012][0-9]\\|30\\|31\\)"
  131. ;; counter
  132. "\\([0-9]\\{1,3\\}\\)"
  133. "\\)\\>")
  134. nil t)
  135. ;; YYYYMMDDIII format, one to three I's.
  136. (let* ((serial (match-string 1))
  137. (counterstr (match-string 5))
  138. (counter (string-to-number counterstr))
  139. (now (format-time-string "%Y%m%d"))
  140. (nowandoldserial (concat now counterstr)))
  141. (if (string< serial nowandoldserial)
  142. (let ((new (format "%s00" now)))
  143. (replace-match new nil nil nil 1)
  144. (message "Replaced old serial %s with %s" serial new))
  145. (if (string= serial nowandoldserial)
  146. (let ((new (format (format "%%s%%0%dd" (length counterstr))
  147. now (1+ counter))))
  148. (replace-match new nil nil nil 1)
  149. (message "Replaced old serial %s with %s" serial new))
  150. (error "Current SOA serial is in the future"))))
  151. (if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t)
  152. ;; Unix time
  153. (let* ((serial (match-string 1))
  154. (new (format-time-string "%s")))
  155. (if (not (string< serial new))
  156. (error "Current SOA serial is in the future")
  157. (replace-match new nil nil nil 1)
  158. (message "Replaced old serial %s with %s" serial new)))
  159. (if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t)
  160. ;; Just any serial number.
  161. (let* ((serial (match-string 1))
  162. (new (format "%d" (1+ (string-to-number serial)))))
  163. (replace-match new nil nil nil 1)
  164. (message "Replaced old serial %s with %s" serial new))
  165. (error "Cannot locate serial number in SOA record"))))))
  166. (defun dns-mode-soa-maybe-increment-serial ()
  167. "Increment SOA serial if needed.
  168. This function is run from `before-save-hook'."
  169. (when (and (buffer-modified-p)
  170. dns-mode-soa-auto-increment-serial
  171. (or (eq dns-mode-soa-auto-increment-serial t)
  172. (y-or-n-p "Increment SOA serial? ")))
  173. ;; If `dns-mode-soa-increment-serial' signals an error saving will
  174. ;; fail but that probably means that the serial should be fixed to
  175. ;; comply with the RFC anyway! -rfr
  176. (progn (dns-mode-soa-increment-serial)
  177. ;; We return nil in case this is used in write-contents-functions.
  178. nil)))
  179. (provide 'dns-mode)
  180. ;;; dns-mode.el ends here