Document reserved keys
[emacs.git] / lisp / textmodes / dns-mode.el
blob7223d525fa2d1c81f55800627793297e010dceb2
1 ;;; dns-mode.el --- a mode for viewing/editing Domain Name System master files
3 ;; Copyright (C) 2000-2001, 2004-2018 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 <https://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"
38 ;; RFC 6844, "DNS Certification Authority Authorization (CAA) Resource Record"
40 ;;; Release history:
42 ;; 2004-09-11 Posted on gnu.emacs.sources.
43 ;; 2004-09-13 Ported to XEmacs.
44 ;; 2004-09-14 Installed in Emacs CVS.
46 ;;; Code:
48 (defgroup dns-mode nil
49 "DNS master file mode configuration."
50 :group 'data)
52 (defconst dns-mode-control-entities '("INCLUDE" "ORIGIN" "TTL")
53 "Lists of strings with known DNS control entities.")
55 (defconst dns-mode-classes '("IN" "CS" "CH" "HS")
56 "List of strings with known DNS classes.")
58 (defconst dns-mode-types '("A" "NS" "MD" "MF" "CNAME" "SOA" "MB" "MG" "MR"
59 "NULL" "WKS" "PTR" "HINFO" "MINFO" "MX" "TXT"
60 "RP" "AFSDB" "X25" "ISDN" "RT" "NSAP"
61 "SIG" "KEY" "PX" "GPOS" "AAAA" "LOC" "NXT"
62 "EID" "NIMLOC" "SRV" "ATMA" "NAPTR" "KX" "CERT"
63 "A6" "DNAME" "SINK" "OPT" "APL" "DS" "SSHFP"
64 "RRSIG" "NSEC" "DNSKEY" "UINFO" "UID" "GID"
65 "UNSPEC" "TKEY" "TSIG" "IXFR" "AXFR" "MAILB"
66 "MAILA" "TLSA" "NSEC3" "CAA")
67 "List of strings with known DNS types.")
69 (defface dns-mode-control-entity '((t :inherit font-lock-keyword-face))
70 "Face used for DNS control entities, e.g. $ORIGIN."
71 :version "26.1"
72 :group 'dns-mode)
74 (defface dns-mode-bad-control-entity '((t :inherit font-lock-warning-face))
75 "Face used for non-standard DNS control entities, e.g. $FOO."
76 :version "26.1"
77 :group 'dns-mode)
79 (defface dns-mode-type '((t :inherit font-lock-type-face))
80 "Face used for DNS types, e.g., SOA."
81 :version "26.1"
82 :group 'dns-mode)
84 (defface dns-mode-class '((t :inherit font-lock-constant-face))
85 "Face used for DNS classes, e.g., IN."
86 :version "26.1"
87 :group 'dns-mode)
89 (defvar dns-mode-control-entity-face ''dns-mode-control-entity
90 "Name of face used for control entities, e.g. $ORIGIN.")
91 (make-obsolete-variable 'dns-mode-control-entity-face
92 "customize the face `dns-mode-control-entity' instead."
93 "26.1" 'set)
95 (defvar dns-mode-bad-control-entity-face ''dns-mode-bad-control-entity
96 "Name of face used for non-standard control entities, e.g. $FOO.")
97 (make-obsolete-variable
98 'dns-mode-bad-control-entity-face
99 "customize the face `dns-mode-bad-control-entity' instead."
100 "26.1" 'set)
102 (defvar dns-mode-type-face ''dns-mode-type
103 "Name of face used for DNS types, e.g., SOA.")
104 (make-obsolete-variable 'dns-mode-type-face
105 "customize the face `dns-mode-type' instead."
106 "26.1" 'set)
108 (defvar dns-mode-class-face ''dns-mode-class
109 "Name of face used for DNS classes, e.g., IN.")
110 (make-obsolete-variable 'dns-mode-class
111 "customize the face `dns-mode-class' instead."
112 "26.1" 'set)
114 (defcustom dns-mode-font-lock-keywords
115 `((,(concat "^$" (regexp-opt dns-mode-control-entities))
116 0 ,dns-mode-control-entity-face)
117 ("^$[a-z0-9A-Z]+" 0 ,dns-mode-bad-control-entity-face)
118 (,(regexp-opt dns-mode-classes) 0 ,dns-mode-class-face)
119 (,(regexp-opt dns-mode-types) 0 ,dns-mode-type-face))
120 "Font lock keywords used to highlight text in DNS master file mode."
121 :version "26.1"
122 :type 'sexp
123 :group 'dns-mode)
125 (defcustom dns-mode-soa-auto-increment-serial t
126 "Whether to increment the SOA serial number automatically.
128 If this variable is t, the serial number is incremented upon each save of
129 the file. If it is `ask', Emacs asks for confirmation whether it should
130 increment the serial upon saving. If nil, serials must be incremented
131 manually with \\[dns-mode-soa-increment-serial]."
132 :type '(choice (const :tag "Always" t)
133 (const :tag "Ask" ask)
134 (const :tag "Never" nil))
135 :group 'dns-mode)
137 ;; Syntax table.
139 (defvar dns-mode-syntax-table
140 (let ((table (make-syntax-table)))
141 (modify-syntax-entry ?\; "< " table)
142 (modify-syntax-entry ?\n "> " table)
143 table)
144 "Syntax table in use in DNS master file buffers.")
146 ;; Keymap.
148 (defvar dns-mode-map
149 (let ((map (make-sparse-keymap)))
150 (define-key map "\C-c\C-s" 'dns-mode-soa-increment-serial)
151 (define-key map "\C-c\C-e" 'dns-mode-ipv6-to-nibbles)
152 map)
153 "Keymap for DNS master file mode.")
155 ;; Menu.
157 (defvar dns-mode-menu nil
158 "Menubar used in DNS master file mode.")
160 (easy-menu-define dns-mode-menu dns-mode-map
161 "DNS Menu."
162 '("DNS"
163 ["Increment SOA serial" dns-mode-soa-increment-serial t]
164 ["Convert IPv6 address to nibbles" dns-mode-ipv6-to-nibbles t]))
166 ;; Mode.
168 ;;;###autoload
169 (define-derived-mode dns-mode text-mode "DNS"
170 "Major mode for viewing and editing DNS master files.
171 This mode is inherited from text mode. It add syntax
172 highlighting, and some commands for handling DNS master files.
173 Its keymap inherits from `text-mode' and it has the same
174 variables for customizing indentation. It has its own abbrev
175 table and its own syntax table.
177 Turning on DNS mode runs `dns-mode-hook'."
178 (set (make-local-variable 'comment-start) ";")
179 (set (make-local-variable 'comment-end) "")
180 (set (make-local-variable 'comment-start-skip) ";+ *")
181 (unless (featurep 'xemacs)
182 (set (make-local-variable 'font-lock-defaults)
183 '(dns-mode-font-lock-keywords nil nil ((?_ . "w")))))
184 (add-hook 'before-save-hook 'dns-mode-soa-maybe-increment-serial
185 nil t)
186 (easy-menu-add dns-mode-menu dns-mode-map))
188 ;;;###autoload (defalias 'zone-mode 'dns-mode)
190 ;; Tools.
192 ;;;###autoload
193 (defun dns-mode-soa-increment-serial ()
194 "Locate SOA record and increment the serial field."
195 (interactive)
196 (save-excursion
197 (goto-char (point-min))
198 (unless (re-search-forward
199 (concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+"
200 "[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t)
201 (error "Cannot locate SOA record"))
202 (if (re-search-forward (concat "\\<\\("
203 ;; year
204 "\\(198\\|199\\|20[0-9]\\)[0-9]"
205 ;; month
206 "\\(0[0-9]\\|10\\|11\\|12\\)"
207 ;; day
208 "\\([012][0-9]\\|30\\|31\\)"
209 ;; counter
210 "\\([0-9]\\{1,3\\}\\)"
211 "\\)\\>")
212 nil t)
213 ;; YYYYMMDDIII format, one to three I's.
214 (let* ((serial (match-string 1))
215 (counterstr (match-string 5))
216 (counter (string-to-number counterstr))
217 (now (format-time-string "%Y%m%d"))
218 (nowandoldserial (concat now counterstr)))
219 (if (string< serial nowandoldserial)
220 (let ((new (format "%s00" now)))
221 (replace-match new nil nil nil 1)
222 (message "Replaced old serial %s with %s" serial new))
223 (if (string= serial nowandoldserial)
224 (let ((new (format (format "%%s%%0%dd" (length counterstr))
225 now (1+ counter))))
226 (replace-match new nil nil nil 1)
227 (message "Replaced old serial %s with %s" serial new))
228 (error "Current SOA serial is in the future"))))
229 (if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t)
230 ;; Unix time
231 (let* ((serial (match-string 1))
232 (new (format-time-string "%s")))
233 (if (not (string< serial new))
234 (error "Current SOA serial is in the future")
235 (replace-match new nil nil nil 1)
236 (message "Replaced old serial %s with %s" serial new)))
237 (if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t)
238 ;; Just any serial number.
239 (let* ((serial (match-string 1))
240 (new (format "%d" (1+ (string-to-number serial)))))
241 (replace-match new nil nil nil 1)
242 (message "Replaced old serial %s with %s" serial new))
243 (error "Cannot locate serial number in SOA record"))))))
245 (defun dns-mode-soa-maybe-increment-serial ()
246 "Increment SOA serial if needed.
248 This function is run from `before-save-hook'."
249 (when (and (buffer-modified-p)
250 dns-mode-soa-auto-increment-serial
251 (or (eq dns-mode-soa-auto-increment-serial t)
252 (y-or-n-p "Increment SOA serial? ")))
253 ;; If `dns-mode-soa-increment-serial' signals an error saving will
254 ;; fail but that probably means that the serial should be fixed to
255 ;; comply with the RFC anyway! -rfr
256 (progn (dns-mode-soa-increment-serial)
257 ;; We return nil in case this is used in write-contents-functions.
258 nil)))
260 (defun dns-mode-ipv6-to-nibbles (&optional negate-prefix)
261 "Convert an IPv6 address around or before point.
262 Replace the address by its ip6.arpa-representation for use in
263 reverse zone files, placing the original address in the kill ring.
265 The address can be: a complete address (no prefix designator);
266 with a normal prefix designator (e.g. /48), in which case only
267 the required number of nibbles are output; or with a negative
268 prefix designator (e.g. /-112), in which case only the part of
269 the address *not* covered by the absolute value of the prefix
270 length is output, as a relative address (without \".ip6.arpa.\" at
271 the end). This is useful when $ORIGIN is specified in the zone file.
273 Optional prefix argument NEGATE-PREFIX negates the value of the
274 detected prefix length.
276 Examples:
278 2001:db8::12 =>
279 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.
281 2001:db8::12/32 =>
282 8.b.d.0.1.0.0.2.ip6.arpa.
284 2001:db8::12/-32 =>
285 2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0
287 ::42/112 (with prefix argument) =>
288 2.4.0.0"
289 (interactive "P")
290 (skip-syntax-backward " ")
291 (skip-syntax-backward "w_.")
292 (re-search-forward "\\([[:xdigit:]:]+\\)\\(/-?[0-9]\\{2,3\\}\\)?")
293 (kill-new (match-string 0))
294 (let ((address (match-string 1))
295 (prefix-length (match-string 2)))
296 (when prefix-length
297 (setq prefix-length (string-to-number (substring prefix-length 1)))
298 (if negate-prefix
299 (setq prefix-length (- prefix-length))))
300 (replace-match
301 (save-match-data
302 (dns-mode-reverse-and-expand-ipv6 address prefix-length)))))
304 (defun dns-mode-reverse-and-expand-ipv6 (address &optional prefix-length)
305 "Convert an IPv6 address to (parts of) an ip6.arpa nibble format.
306 ADDRESS is an IPv6 address in the usual colon-separated
307 format, without a prefix designator at the end.
309 Optional PREFIX-LENGTH is a number whose absolute value is the
310 length in bits of the network part of the address. If nil,
311 return an absolute address representing the full IPv6 address.
312 If positive, return an absolute address representing the network
313 prefix indicated. If negative, return a relative address
314 representing the host parts of the address with respect to the
315 indicated network prefix.
317 See `dns-mode-ipv6-to-nibbles' for examples."
318 (let* ((chunks (split-string address ":"))
319 (prefix-length-nibbles (if prefix-length
320 (ceiling (abs prefix-length) 4)
321 32))
322 (filler-chunks (- 8 (length (remove "" chunks))))
323 (expanded-address
324 (apply #'concat
325 (cl-loop with filler-done = nil
326 for chunk in chunks
327 if (and (not filler-done)
328 (string= "" chunk))
329 append (prog1
330 (cl-loop repeat filler-chunks
331 collect "0000")
332 (setq filler-done t))
333 else
334 if (not (string= "" chunk))
335 collect (format "%04x"
336 (string-to-number chunk 16)))))
337 (rev-address-nibbles
338 (nreverse (if (and prefix-length
339 (cl-minusp prefix-length))
340 (substring expanded-address prefix-length-nibbles)
341 (substring expanded-address 0 prefix-length-nibbles)))))
342 (with-temp-buffer
343 (cl-loop for char across rev-address-nibbles
345 (insert char)
346 (insert "."))
347 (if (and prefix-length
348 (cl-minusp prefix-length))
349 (delete-char -1)
350 (insert "ip6.arpa."))
351 (insert " ")
352 (buffer-string))))
354 (provide 'dns-mode)
356 ;;; dns-mode.el ends here