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