unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 88d9c6bee6cd9aad5b96f64f24a5d0f509aa69d4 12259 bytes (raw)
name: lisp/textmodes/dns-mode.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
 
;;; dns-mode.el --- a mode for viewing/editing Domain Name System master files

;; Copyright (C) 2000-2001, 2004-2017 Free Software Foundation, Inc.

;; Author: Simon Josefsson <simon@josefsson.org>
;; Keywords: DNS master zone file SOA comm

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Use M-x dns-mode RET to invoke in master files.
;;
;; C-c C-s  Increment SOA serial.
;;          Understands YYYYMMDDNN, Unix time, and serial number formats,
;;          and complains if it fail to find SOA serial.

;;; References:

;; RFC 1034, "DOMAIN NAMES - CONCEPTS AND FACILITIES", P. Mockapetris.
;; RFC 1035, "DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION", P. Mockapetris.
;; RFC 5155, "DNS Security (DNSSEC) Hashed Authenticated Denial of Existence"
;; RFC 6698, "The DNS-Based Authentication of Named Entities (DANE)
;;             Transport Layer Security (TLS) Protocol: TLSA"

;;; Release history:

;; 2004-09-11  Posted on gnu.emacs.sources.
;; 2004-09-13  Ported to XEmacs.
;; 2004-09-14  Installed in Emacs CVS.

;;; Code:

(defgroup dns-mode nil
  "DNS master file mode configuration."
  :group 'data)

(defconst dns-mode-classes '("IN" "CS" "CH" "HS")
  "List of strings with known DNS classes.")

(defconst dns-mode-types '("A" "NS" "MD" "MF" "CNAME" "SOA" "MB" "MG" "MR"
			   "NULL" "WKS" "PTR" "HINFO" "MINFO" "MX" "TXT"
			   "RP" "AFSDB" "X25" "ISDN" "RT" "NSAP"
			   "SIG" "KEY" "PX" "GPOS" "AAAA" "LOC" "NXT"
			   "EID" "NIMLOC" "SRV" "ATMA" "NAPTR" "KX" "CERT"
			   "A6" "DNAME" "SINK" "OPT" "APL" "DS" "SSHFP"
			   "RRSIG" "NSEC" "DNSKEY" "UINFO" "UID" "GID"
			   "UNSPEC" "TKEY" "TSIG" "IXFR" "AXFR" "MAILB"
			   "MAILA" "TLSA" "NSEC3")
  "List of strings with known DNS types.")

;; Font lock.

(defvar dns-mode-control-entity-face 'font-lock-keyword-face
  "Name of face used for control entities, e.g. $ORIGIN.")

(defvar dns-mode-bad-control-entity-face 'font-lock-warning-face
  "Name of face used for non-standard control entities, e.g. $FOO.")

(defvar dns-mode-type-face 'font-lock-type-face
  "Name of face used for DNS types, e.g., SOA.")

(defvar dns-mode-class-face 'font-lock-constant-face
  "Name of face used for DNS classes, e.g., IN.")

(defcustom dns-mode-font-lock-keywords
  `(("^$ORIGIN" 0 ,dns-mode-control-entity-face)
    ("^$INCLUDE" 0 ,dns-mode-control-entity-face)
    ("^$TTL" 0 ,dns-mode-control-entity-face)
    ("^$[a-z0-9A-Z]+" 0 ,dns-mode-bad-control-entity-face)
    (,(regexp-opt dns-mode-classes) 0 ,dns-mode-class-face)
    (,(regexp-opt dns-mode-types) 0 ,dns-mode-type-face))
  "Font lock keywords used to highlight text in DNS master file mode."
  :type 'sexp
  :group 'dns-mode)

(defcustom dns-mode-soa-auto-increment-serial t
  "Whether to increment the SOA serial number automatically.

If this variable is t, the serial number is incremented upon each save of
the file.  If it is `ask', Emacs asks for confirmation whether it should
increment the serial upon saving.  If nil, serials must be incremented
manually with \\[dns-mode-soa-increment-serial]."
  :type '(choice (const :tag "Always" t)
		 (const :tag "Ask" ask)
		 (const :tag "Never" nil))
  :group 'dns-mode)

;; Syntax table.

(defvar dns-mode-syntax-table
  (let ((table (make-syntax-table)))
    (modify-syntax-entry ?\; "<   " table)
    (modify-syntax-entry ?\n ">   " table)
    table)
  "Syntax table in use in DNS master file buffers.")

;; Keymap.

(defvar dns-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "\C-c\C-s" 'dns-mode-soa-increment-serial)
    (define-key map "\C-c\C-e" 'dns-mode-ipv6-to-nibbles)
    map)
  "Keymap for DNS master file mode.")

;; Menu.

(defvar dns-mode-menu nil
  "Menubar used in DNS master file mode.")

(easy-menu-define dns-mode-menu dns-mode-map
  "DNS Menu."
  '("DNS"
    ["Increment SOA serial" dns-mode-soa-increment-serial t]
    ["Convert IPv6 address to nibbles" dns-mode-ipv6-to-nibbles t]))

;; Mode.

;;;###autoload
(define-derived-mode dns-mode text-mode "DNS"
  "Major mode for viewing and editing DNS master files.
This mode is inherited from text mode.  It add syntax
highlighting, and some commands for handling DNS master files.
Its keymap inherits from `text-mode' and it has the same
variables for customizing indentation.  It has its own abbrev
table and its own syntax table.

Turning on DNS mode runs `dns-mode-hook'."
  (set (make-local-variable 'comment-start) ";")
  (set (make-local-variable 'comment-end) "")
  (set (make-local-variable 'comment-start-skip) ";+ *")
  (unless (featurep 'xemacs)
    (set (make-local-variable 'font-lock-defaults)
	 '(dns-mode-font-lock-keywords nil nil ((?_ . "w")))))
  (add-hook 'before-save-hook 'dns-mode-soa-maybe-increment-serial
	    nil t)
  (easy-menu-add dns-mode-menu dns-mode-map))

;;;###autoload (defalias 'zone-mode 'dns-mode)

;; Tools.

;;;###autoload
(defun dns-mode-soa-increment-serial ()
  "Locate SOA record and increment the serial field."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (unless (re-search-forward
	     (concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+"
		     "[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t)
      (error "Cannot locate SOA record"))
    (if (re-search-forward (concat "\\<\\("
				   ;; year
				   "\\(198\\|199\\|20[0-9]\\)[0-9]"
				   ;; month
				   "\\(0[0-9]\\|10\\|11\\|12\\)"
				   ;; day
				   "\\([012][0-9]\\|30\\|31\\)"
				   ;; counter
				   "\\([0-9]\\{1,3\\}\\)"
				   "\\)\\>")
			   nil t)
	;; YYYYMMDDIII format, one to three I's.
	(let* ((serial (match-string 1))
	       (counterstr (match-string 5))
	       (counter (string-to-number counterstr))
	       (now (format-time-string "%Y%m%d"))
	       (nowandoldserial (concat now counterstr)))
	  (if (string< serial nowandoldserial)
	      (let ((new (format "%s00" now)))
		(replace-match new nil nil nil 1)
		(message "Replaced old serial %s with %s" serial new))
	    (if (string= serial nowandoldserial)
		(let ((new (format (format "%%s%%0%dd" (length counterstr))
				   now (1+ counter))))
		  (replace-match new nil nil nil 1)
		  (message "Replaced old serial %s with %s" serial new))
	      (error "Current SOA serial is in the future"))))
      (if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t)
	  ;; Unix time
	  (let* ((serial (match-string 1))
		 (new (format-time-string "%s")))
	    (if (not (string< serial new))
		(error "Current SOA serial is in the future")
	      (replace-match new nil nil nil 1)
	      (message "Replaced old serial %s with %s" serial new)))
	(if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t)
	    ;; Just any serial number.
	    (let* ((serial (match-string 1))
		   (new (format "%d" (1+ (string-to-number serial)))))
	      (replace-match new nil nil nil 1)
	      (message "Replaced old serial %s with %s" serial new))
	  (error "Cannot locate serial number in SOA record"))))))

(defun dns-mode-soa-maybe-increment-serial ()
  "Increment SOA serial if needed.

This function is run from `before-save-hook'."
  (when (and (buffer-modified-p)
	     dns-mode-soa-auto-increment-serial
	     (or (eq dns-mode-soa-auto-increment-serial t)
		 (y-or-n-p "Increment SOA serial? ")))
    ;; If `dns-mode-soa-increment-serial' signals an error saving will
    ;; fail but that probably means that the serial should be fixed to
    ;; comply with the RFC anyway! -rfr
    (progn (dns-mode-soa-increment-serial)
	   ;; We return nil in case this is used in write-contents-functions.
	   nil)))

;;;###autoload
(defun dns-mode-ipv6-to-nibbles (negate-prefix-p)
  "Replace an IPv6 address around or preceeding point by its
ip6.arpa-representation for use in reverse zone files.  The
replaced address is placed in the kill ring.

The address can be a complete address (no prefix designator), can
have a normal prefix designator (e.g. /48), in which case only
the required number of nibbles are output, or can have a negative
prefix designator (e.g. /-112), in which case only the parts of
the address *not* covered by the absolute value of the prefix
length is output, as a relative address (without .ip6.arpa. at
the end).  This is useful when $ORIGIN is specified in the zone
file.

If called with a prefix argument (C-u), the value of the detected
prefix length is negated.

Examples:

2001:db8::12  =>
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.

2001:db8::12/32  =>
8.b.d.0.1.0.0.2.ip6.arpa.

2001:db8::12/-32  =>
2.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0

::42/112 (with C-u prefix) =>
2.4.0.0"
  (interactive "P")
  (skip-syntax-backward " ")
  (skip-syntax-backward "w_.")
  (re-search-forward "\\([[:xdigit:]:]+\\)\\(/-?[0-9]\\{2,3\\}\\)?")
  (kill-new (match-string 0))
  (let ((address (match-string 1))
        (prefix-length (match-string 2)))
    (if prefix-length
        (progn
          (setq prefix-length (string-to-number (substring prefix-length 1)))
          (if negate-prefix-p
              (setq prefix-length (* -1 prefix-length)))))
    (replace-match (save-match-data
                     (dns-mode-reverse-and-expand-ipv6 address prefix-length)))))

(defun dns-mode-reverse-and-expand-ipv6 (address &optional prefix-length)
  "Convert an IPv6 address to (parts of) an ip6.arpa nibble format.

ADDRESS must be an IPv6 address in the usual colon-separaated
format, without a prefix designator at the end.  PREFIX-LENGTH,
if given, must be a number whose absolute value is the length in
bits of the network part of the address.

If PREFIX-LENGTH is `nil', an absolute address is returned
representing the full IPv6 address.

If PREFIX-LENGTH is positive, an absolute address is returned
representing the network prefix indicated.

If PREFIX-LENGTH is negative, a relative address is returned
representing the host parts of the address with respect to the
indicated network prefix.

See `dns-mode-ipv6-to-nibbles' for examples."
  (let* ((chunks (split-string address ":"))
         (prefix-length-nibbles (if prefix-length
                                    (ceiling (abs prefix-length) 4)
                                  32))
         (filler-chunks (- 8 (length (remove "" chunks))))
         (expanded-address (apply #'concat
                                  (cl-loop with filler-done = nil
                                           for chunk in chunks
                                           if (and (not filler-done)
                                                   (string= "" chunk))
                                           append (prog1
                                                      (cl-loop repeat filler-chunks
                                                               collect "0000")
                                                    (setq filler-done t))
                                           else
                                           if (not (string= "" chunk))
                                           collect (format "%04x" (string-to-number chunk 16)))))
         (rev-address-nibbles (nreverse (if (and prefix-length
                                                 (cl-minusp prefix-length))
                                            (substring expanded-address prefix-length-nibbles)
                                          (substring expanded-address 0 prefix-length-nibbles)))))
    (with-temp-buffer
      (cl-loop for char across rev-address-nibbles
               do
               (insert char)
               (insert "."))
      (if (and prefix-length
               (cl-minusp prefix-length))
          (delete-char -1)
        (insert "ip6.arpa."))
      (insert " ")
      (buffer-string))))

(provide 'dns-mode)

;;; dns-mode.el ends here

debug log:

solving 88d9c6bee6 ...
found 88d9c6bee6 in https://yhetil.org/emacs-bugs/m11srit24v.fsf@klingenberg.no/
found 01f509d913 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 01f509d9136aa926b0b2975fbf6f1ed8f789da97	lisp/textmodes/dns-mode.el

applying [1/1] https://yhetil.org/emacs-bugs/m11srit24v.fsf@klingenberg.no/
diff --git a/lisp/textmodes/dns-mode.el b/lisp/textmodes/dns-mode.el
index 01f509d913..88d9c6bee6 100644

Checking patch lisp/textmodes/dns-mode.el...
Applied patch lisp/textmodes/dns-mode.el cleanly.

index at:
100644 88d9c6bee6cd9aad5b96f64f24a5d0f509aa69d4	lisp/textmodes/dns-mode.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).