; Spelling fixes
[emacs.git] / lisp / textmodes / dns-mode.el
blob02cb2a2876d6a5789106c7eaeba99b8f931c161c
1 ;;; dns-mode.el --- a mode for viewing/editing Domain Name System master files
3 ;; Copyright (C) 2000-2001, 2004-2017 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <simon@josefsson.org>
6 ;; Keywords: DNS master zone file SOA comm
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Use M-x dns-mode RET to invoke in master files.
27 ;; C-c C-s Increment SOA serial.
28 ;; Understands YYYYMMDDNN, Unix time, and serial number formats,
29 ;; and complains if it fail to find SOA serial.
31 ;;; References:
33 ;; RFC 1034, "DOMAIN NAMES - CONCEPTS AND FACILITIES", P. Mockapetris.
34 ;; RFC 1035, "DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION", P. Mockapetris.
35 ;; RFC 5155, "DNS Security (DNSSEC) Hashed Authenticated Denial of Existence"
36 ;; RFC 6698, "The DNS-Based Authentication of Named Entities (DANE)
37 ;; Transport Layer Security (TLS) Protocol: TLSA"
39 ;;; Release history:
41 ;; 2004-09-11 Posted on gnu.emacs.sources.
42 ;; 2004-09-13 Ported to XEmacs.
43 ;; 2004-09-14 Installed in Emacs CVS.
45 ;;; Code:
47 (defgroup dns-mode nil
48 "DNS master file mode configuration."
49 :group 'data)
51 (defconst dns-mode-classes '("IN" "CS" "CH" "HS")
52 "List of strings with known DNS classes.")
54 (defconst dns-mode-types '("A" "NS" "MD" "MF" "CNAME" "SOA" "MB" "MG" "MR"
55 "NULL" "WKS" "PTR" "HINFO" "MINFO" "MX" "TXT"
56 "RP" "AFSDB" "X25" "ISDN" "RT" "NSAP"
57 "SIG" "KEY" "PX" "GPOS" "AAAA" "LOC" "NXT"
58 "EID" "NIMLOC" "SRV" "ATMA" "NAPTR" "KX" "CERT"
59 "A6" "DNAME" "SINK" "OPT" "APL" "DS" "SSHFP"
60 "RRSIG" "NSEC" "DNSKEY" "UINFO" "UID" "GID"
61 "UNSPEC" "TKEY" "TSIG" "IXFR" "AXFR" "MAILB"
62 "MAILA" "TLSA" "NSEC3")
63 "List of strings with known DNS types.")
65 ;; Font lock.
67 (defvar dns-mode-control-entity-face 'font-lock-keyword-face
68 "Name of face used for control entities, e.g. $ORIGIN.")
70 (defvar dns-mode-bad-control-entity-face 'font-lock-warning-face
71 "Name of face used for non-standard control entities, e.g. $FOO.")
73 (defvar dns-mode-type-face 'font-lock-type-face
74 "Name of face used for DNS types, e.g., SOA.")
76 (defvar dns-mode-class-face 'font-lock-constant-face
77 "Name of face used for DNS classes, e.g., IN.")
79 (defcustom dns-mode-font-lock-keywords
80 `(("^$ORIGIN" 0 ,dns-mode-control-entity-face)
81 ("^$INCLUDE" 0 ,dns-mode-control-entity-face)
82 ("^$[a-z0-9A-Z]+" 0 ,dns-mode-bad-control-entity-face)
83 (,(regexp-opt dns-mode-classes) 0 ,dns-mode-class-face)
84 (,(regexp-opt dns-mode-types) 0 ,dns-mode-type-face))
85 "Font lock keywords used to highlight text in DNS master file mode."
86 :type 'sexp
87 :group 'dns-mode)
89 (defcustom dns-mode-soa-auto-increment-serial t
90 "Whether to increment the SOA serial number automatically.
92 If this variable is t, the serial number is incremented upon each save of
93 the file. If it is `ask', Emacs asks for confirmation whether it should
94 increment the serial upon saving. If nil, serials must be incremented
95 manually with \\[dns-mode-soa-increment-serial]."
96 :type '(choice (const :tag "Always" t)
97 (const :tag "Ask" ask)
98 (const :tag "Never" nil))
99 :group 'dns-mode)
101 ;; Syntax table.
103 (defvar dns-mode-syntax-table
104 (let ((table (make-syntax-table)))
105 (modify-syntax-entry ?\; "< " table)
106 (modify-syntax-entry ?\n "> " table)
107 table)
108 "Syntax table in use in DNS master file buffers.")
110 ;; Keymap.
112 (defvar dns-mode-map
113 (let ((map (make-sparse-keymap)))
114 (define-key map "\C-c\C-s" 'dns-mode-soa-increment-serial)
115 map)
116 "Keymap for DNS master file mode.")
118 ;; Menu.
120 (defvar dns-mode-menu nil
121 "Menubar used in DNS master file mode.")
123 (easy-menu-define dns-mode-menu dns-mode-map
124 "DNS Menu."
125 '("DNS"
126 ["Increment SOA serial" dns-mode-soa-increment-serial t]))
128 ;; 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.
139 Turning on DNS mode runs `dns-mode-hook'."
140 (set (make-local-variable 'comment-start) ";")
141 (set (make-local-variable 'comment-end) "")
142 (set (make-local-variable 'comment-start-skip) ";+ *")
143 (unless (featurep 'xemacs)
144 (set (make-local-variable 'font-lock-defaults)
145 '(dns-mode-font-lock-keywords nil nil ((?_ . "w")))))
146 (add-hook 'before-save-hook 'dns-mode-soa-maybe-increment-serial
147 nil t)
148 (easy-menu-add dns-mode-menu dns-mode-map))
150 ;;;###autoload (defalias 'zone-mode 'dns-mode)
152 ;; Tools.
154 ;;;###autoload
155 (defun dns-mode-soa-increment-serial ()
156 "Locate SOA record and increment the serial field."
157 (interactive)
158 (save-excursion
159 (goto-char (point-min))
160 (unless (re-search-forward
161 (concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+"
162 "[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t)
163 (error "Cannot locate SOA record"))
164 (if (re-search-forward (concat "\\<\\("
165 ;; year
166 "\\(198\\|199\\|20[0-9]\\)[0-9]"
167 ;; month
168 "\\(0[0-9]\\|10\\|11\\|12\\)"
169 ;; day
170 "\\([012][0-9]\\|30\\|31\\)"
171 ;; counter
172 "\\([0-9]\\{1,3\\}\\)"
173 "\\)\\>")
174 nil t)
175 ;; YYYYMMDDIII format, one to three I's.
176 (let* ((serial (match-string 1))
177 (counterstr (match-string 5))
178 (counter (string-to-number counterstr))
179 (now (format-time-string "%Y%m%d"))
180 (nowandoldserial (concat now counterstr)))
181 (if (string< serial nowandoldserial)
182 (let ((new (format "%s00" now)))
183 (replace-match new nil nil nil 1)
184 (message "Replaced old serial %s with %s" serial new))
185 (if (string= serial nowandoldserial)
186 (let ((new (format (format "%%s%%0%dd" (length counterstr))
187 now (1+ counter))))
188 (replace-match new nil nil nil 1)
189 (message "Replaced old serial %s with %s" serial new))
190 (error "Current SOA serial is in the future"))))
191 (if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t)
192 ;; Unix time
193 (let* ((serial (match-string 1))
194 (new (format-time-string "%s")))
195 (if (not (string< serial new))
196 (error "Current SOA serial is in the future")
197 (replace-match new nil nil nil 1)
198 (message "Replaced old serial %s with %s" serial new)))
199 (if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t)
200 ;; Just any serial number.
201 (let* ((serial (match-string 1))
202 (new (format "%d" (1+ (string-to-number serial)))))
203 (replace-match new nil nil nil 1)
204 (message "Replaced old serial %s with %s" serial new))
205 (error "Cannot locate serial number in SOA record"))))))
207 (defun dns-mode-soa-maybe-increment-serial ()
208 "Increment SOA serial if needed.
210 This function is run from `before-save-hook'."
211 (when (and (buffer-modified-p)
212 dns-mode-soa-auto-increment-serial
213 (or (eq dns-mode-soa-auto-increment-serial t)
214 (y-or-n-p "Increment SOA serial? ")))
215 ;; If `dns-mode-soa-increment-serial' signals an error saving will
216 ;; fail but that probably means that the serial should be fixed to
217 ;; comply with the RFC anyway! -rfr
218 (progn (dns-mode-soa-increment-serial)
219 ;; We return nil in case this is used in write-contents-functions.
220 nil)))
222 (provide 'dns-mode)
224 ;;; dns-mode.el ends here