| 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 |
(defgroup dns-mode nil |
|---|
| 53 |
"DNS master file mode configuration." |
|---|
| 54 |
:group 'data) |
|---|
| 55 |
|
|---|
| 56 |
(defconst dns-mode-classes '("IN" "CS" "CH" "HS") |
|---|
| 57 |
"List of strings with known DNS classes.") |
|---|
| 58 |
|
|---|
| 59 |
(defconst dns-mode-types '("A" "NS" "MD" "MF" "CNAME" "SOA" "MB" "MG" "MR" |
|---|
| 60 |
"NULL" "WKS" "PTR" "HINFO" "MINFO" "MX" "TXT" |
|---|
| 61 |
"RP" "AFSDB" "X25" "ISDN" "RT" "NSAP" "NSAP" |
|---|
| 62 |
"SIG" "KEY" "PX" "GPOS" "AAAA" "LOC" "NXT" |
|---|
| 63 |
"EID" "NIMLOC" "SRV" "ATMA" "NAPTR" "KX" "CERT" |
|---|
| 64 |
"A6" "DNAME" "SINK" "OPT" "APL" "DS" "SSHFP" |
|---|
| 65 |
"RRSIG" "NSEC" "DNSKEY" "UINFO" "UID" "GID" |
|---|
| 66 |
"UNSPEC" "TKEY" "TSIG" "IXFR" "AXFR" "MAILB" |
|---|
| 67 |
"MAILA") |
|---|
| 68 |
"List of strings with known DNS types.") |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
(defvar dns-mode-control-entity-face 'font-lock-keyword-face |
|---|
| 73 |
"Name of face used for control entities, e.g. $ORIGIN.") |
|---|
| 74 |
|
|---|
| 75 |
(defvar dns-mode-bad-control-entity-face 'font-lock-warning-face |
|---|
| 76 |
"Name of face used for non-standard control entities, e.g. $FOO.") |
|---|
| 77 |
|
|---|
| 78 |
(defvar dns-mode-type-face 'font-lock-type-face |
|---|
| 79 |
"Name of face used for DNS types, e.g., SOA.") |
|---|
| 80 |
|
|---|
| 81 |
(defvar dns-mode-class-face 'font-lock-constant-face |
|---|
| 82 |
"Name of face used for DNS classes, e.g., IN.") |
|---|
| 83 |
|
|---|
| 84 |
(defcustom dns-mode-font-lock-keywords |
|---|
| 85 |
`(("^$ORIGIN" 0 ,dns-mode-control-entity-face) |
|---|
| 86 |
("^$INCLUDE" 0 ,dns-mode-control-entity-face) |
|---|
| 87 |
("^$[a-z0-9A-Z]+" 0 ,dns-mode-bad-control-entity-face) |
|---|
| 88 |
(,(regexp-opt dns-mode-classes) 0 ,dns-mode-class-face) |
|---|
| 89 |
(,(regexp-opt dns-mode-types) 0 ,dns-mode-type-face)) |
|---|
| 90 |
"Font lock keywords used to highlight text in DNS master file mode." |
|---|
| 91 |
:type 'sexp |
|---|
| 92 |
:group 'dns-mode) |
|---|
| 93 |
|
|---|
| 94 |
(defcustom dns-mode-soa-auto-increment-serial t |
|---|
| 95 |
"Whether to increment the SOA serial number automatically. |
|---|
| 96 |
|
|---|
| 97 |
If this variable is t, the serial number is incremented upon each save of |
|---|
| 98 |
the file. If it is `ask', Emacs asks for confirmation whether it should |
|---|
| 99 |
increment the serial upon saving. If nil, serials must be incremented |
|---|
| 100 |
manually with \\[dns-mode-soa-increment-serial]." |
|---|
| 101 |
:type '(choice (const :tag "Always" t) |
|---|
| 102 |
(const :tag "Ask" ask) |
|---|
| 103 |
(const :tag "Never" nil)) |
|---|
| 104 |
:group 'dns-mode) |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
(defvar dns-mode-syntax-table |
|---|
| 109 |
(let ((table (make-syntax-table))) |
|---|
| 110 |
(modify-syntax-entry ?\ |
|---|
| 111 |
(modify-syntax-entry ?\n "> " table) |
|---|
| 112 |
table) |
|---|
| 113 |
"Syntax table in use in DNS master file buffers.") |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
(defvar dns-mode-map |
|---|
| 118 |
(let ((map (make-sparse-keymap))) |
|---|
| 119 |
(define-key map "\C-c\C-s" 'dns-mode-soa-increment-serial) |
|---|
| 120 |
map) |
|---|
| 121 |
"Keymap for DNS master file mode.") |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
(defvar dns-mode-menu nil |
|---|
| 126 |
"Menubar used in DNS master file mode.") |
|---|
| 127 |
|
|---|
| 128 |
(easy-menu-define dns-mode-menu dns-mode-map |
|---|
| 129 |
"DNS Menu." |
|---|
| 130 |
'("DNS" |
|---|
| 131 |
["Increment SOA serial" dns-mode-soa-increment-serial t])) |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
(define-derived-mode dns-mode text-mode "DNS" |
|---|
| 137 |
"Major mode for viewing and editing DNS master files. |
|---|
| 138 |
This mode is inherited from text mode. It add syntax |
|---|
| 139 |
highlighting, and some commands for handling DNS master files. |
|---|
| 140 |
Its keymap inherits from `text-mode' and it has the same |
|---|
| 141 |
variables for customizing indentation. It has its own abbrev |
|---|
| 142 |
table and its own syntax table. |
|---|
| 143 |
|
|---|
| 144 |
Turning on DNS mode runs `dns-mode-hook'." |
|---|
| 145 |
(set (make-local-variable 'comment-start) ";") |
|---|
| 146 |
(set (make-local-variable 'comment-end) "") |
|---|
| 147 |
(set (make-local-variable 'comment-start-skip) ";+ *") |
|---|
| 148 |
(unless (featurep 'xemacs) |
|---|
| 149 |
(set (make-local-variable 'font-lock-defaults) |
|---|
| 150 |
'(dns-mode-font-lock-keywords nil nil ((?_ . "w"))))) |
|---|
| 151 |
(add-hook 'before-save-hook 'dns-mode-soa-maybe-increment-serial |
|---|
| 152 |
nil t) |
|---|
| 153 |
(easy-menu-add dns-mode-menu dns-mode-map)) |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
(defun dns-mode-soa-increment-serial () |
|---|
| 161 |
"Locate SOA record and increment the serial field." |
|---|
| 162 |
(interactive) |
|---|
| 163 |
(save-excursion |
|---|
| 164 |
(goto-char (point-min)) |
|---|
| 165 |
(unless (re-search-forward |
|---|
| 166 |
(concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+" |
|---|
| 167 |
"[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t) |
|---|
| 168 |
(error "Cannot locate SOA record")) |
|---|
| 169 |
(if (re-search-forward (concat "\\<\\(" |
|---|
| 170 |
|
|---|
| 171 |
"\\(198\\|199\\|20[0-9]\\)[0-9]" |
|---|
| 172 |
|
|---|
| 173 |
"\\(0[0-9]\\|10\\|11\\|12\\)" |
|---|
| 174 |
|
|---|
| 175 |
"\\([012][0-9]\\|30\\|31\\)" |
|---|
| 176 |
|
|---|
| 177 |
"\\([0-9]\\{1,3\\}\\)" |
|---|
| 178 |
"\\)\\>") |
|---|
| 179 |
nil t) |
|---|
| 180 |
|
|---|
| 181 |
(let* ((serial (match-string 1)) |
|---|
| 182 |
(counterstr (match-string 5)) |
|---|
| 183 |
(counter (string-to-number counterstr)) |
|---|
| 184 |
(now (format-time-string "%Y%m%d")) |
|---|
| 185 |
(nowandoldserial (concat now counterstr))) |
|---|
| 186 |
(if (string< serial nowandoldserial) |
|---|
| 187 |
(let ((new (format "%s00" now))) |
|---|
| 188 |
(replace-match new nil nil nil 1) |
|---|
| 189 |
(message "Replaced old serial %s with %s" serial new)) |
|---|
| 190 |
(if (string= serial nowandoldserial) |
|---|
| 191 |
(let ((new (format (format "%%s%%0%dd" (length counterstr)) |
|---|
| 192 |
now (1+ counter)))) |
|---|
| 193 |
(replace-match new nil nil nil 1) |
|---|
| 194 |
(message "Replaced old serial %s with %s" serial new)) |
|---|
| 195 |
(error "Current SOA serial is in the future")))) |
|---|
| 196 |
(if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t) |
|---|
| 197 |
|
|---|
| 198 |
(let* ((serial (match-string 1)) |
|---|
| 199 |
(new (format-time-string "%s"))) |
|---|
| 200 |
(if (not (string< serial new)) |
|---|
| 201 |
(error "Current SOA serial is in the future") |
|---|
| 202 |
(replace-match new nil nil nil 1) |
|---|
| 203 |
(message "Replaced old serial %s with %s" serial new))) |
|---|
| 204 |
(if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t) |
|---|
| 205 |
|
|---|
| 206 |
(let* ((serial (match-string 1)) |
|---|
| 207 |
(new (format "%d" (1+ (string-to-number serial))))) |
|---|
| 208 |
(replace-match new nil nil nil 1) |
|---|
| 209 |
(message "Replaced old serial %s with %s" serial new)) |
|---|
| 210 |
(error "Cannot locate serial number in SOA record")))))) |
|---|
| 211 |
|
|---|
| 212 |
(defun dns-mode-soa-maybe-increment-serial () |
|---|
| 213 |
"Increment SOA serial if needed. |
|---|
| 214 |
|
|---|
| 215 |
This function is run from `before-save-hook'." |
|---|
| 216 |
(when (and (buffer-modified-p) |
|---|
| 217 |
dns-mode-soa-auto-increment-serial |
|---|
| 218 |
(or (eq dns-mode-soa-auto-increment-serial t) |
|---|
| 219 |
(y-or-n-p "Increment SOA serial? "))) |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
(progn (dns-mode-soa-increment-serial) |
|---|
| 224 |
|
|---|
| 225 |
nil))) |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
(provide 'dns-mode) |
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|