| 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 |
(eval-when-compile (require 'cl)) |
|---|
| 43 |
|
|---|
| 44 |
(defgroup dig nil |
|---|
| 45 |
"Dig configuration." |
|---|
| 46 |
:group 'comm) |
|---|
| 47 |
|
|---|
| 48 |
(defcustom dig-program "dig" |
|---|
| 49 |
"Name of dig (domain information groper) binary." |
|---|
| 50 |
:type 'file |
|---|
| 51 |
:group 'dig) |
|---|
| 52 |
|
|---|
| 53 |
(defcustom dig-dns-server nil |
|---|
| 54 |
"DNS server to query. |
|---|
| 55 |
If nil, use system defaults." |
|---|
| 56 |
:type '(choice (const :tag "System defaults") |
|---|
| 57 |
string) |
|---|
| 58 |
:group 'dig) |
|---|
| 59 |
|
|---|
| 60 |
(defcustom dig-font-lock-keywords |
|---|
| 61 |
'(("^;; [A-Z]+ SECTION:" 0 font-lock-keyword-face) |
|---|
| 62 |
("^;;.*" 0 font-lock-comment-face) |
|---|
| 63 |
("^; <<>>.*" 0 font-lock-type-face) |
|---|
| 64 |
("^;.*" 0 font-lock-function-name-face)) |
|---|
| 65 |
"Default expressions to highlight in dig mode." |
|---|
| 66 |
:type 'sexp |
|---|
| 67 |
:group 'dig) |
|---|
| 68 |
|
|---|
| 69 |
(defun dig-invoke (domain &optional |
|---|
| 70 |
query-type query-class query-option |
|---|
| 71 |
dig-option server) |
|---|
| 72 |
"Call dig with given arguments and return buffer containing output. |
|---|
| 73 |
DOMAIN is a string with a DNS domain. QUERY-TYPE is an optional string |
|---|
| 74 |
with a DNS type. QUERY-CLASS is an optional string with a DNS class. |
|---|
| 75 |
QUERY-OPTION is an optional string with dig \"query options\". |
|---|
| 76 |
DIG-OPTIONS is an optional string with parameters for the dig program. |
|---|
| 77 |
SERVER is an optional string with a domain name server to query. |
|---|
| 78 |
|
|---|
| 79 |
Dig is an external program found in the BIND name server distribution, |
|---|
| 80 |
and is a commonly available debugging tool." |
|---|
| 81 |
(let (buf cmdline) |
|---|
| 82 |
(setq buf (generate-new-buffer "*dig output*")) |
|---|
| 83 |
(if dig-option (push dig-option cmdline)) |
|---|
| 84 |
(if query-option (push query-option cmdline)) |
|---|
| 85 |
(if query-class (push query-class cmdline)) |
|---|
| 86 |
(if query-type (push query-type cmdline)) |
|---|
| 87 |
(push domain cmdline) |
|---|
| 88 |
(if server (push (concat "@" server) cmdline) |
|---|
| 89 |
(if dig-dns-server (push (concat "@" dig-dns-server) cmdline))) |
|---|
| 90 |
(apply 'call-process dig-program nil buf nil cmdline) |
|---|
| 91 |
buf)) |
|---|
| 92 |
|
|---|
| 93 |
(defun dig-extract-rr (domain &optional type class) |
|---|
| 94 |
"Extract resource records for DOMAIN, TYPE and CLASS from buffer. |
|---|
| 95 |
Buffer should contain output generated by `dig-invoke'." |
|---|
| 96 |
(save-excursion |
|---|
| 97 |
(goto-char (point-min)) |
|---|
| 98 |
(if (re-search-forward |
|---|
| 99 |
(concat domain "\\.?[\t ]+[0-9wWdDhHmMsS]+[\t ]+" |
|---|
| 100 |
(upcase (or class "IN")) "[\t ]+" (upcase (or type "A"))) |
|---|
| 101 |
nil t) |
|---|
| 102 |
(let (b e) |
|---|
| 103 |
(end-of-line) |
|---|
| 104 |
(setq e (point)) |
|---|
| 105 |
(beginning-of-line) |
|---|
| 106 |
(setq b (point)) |
|---|
| 107 |
(when (search-forward " (" e t) |
|---|
| 108 |
(search-forward " )")) |
|---|
| 109 |
(end-of-line) |
|---|
| 110 |
(setq e (point)) |
|---|
| 111 |
(buffer-substring b e)) |
|---|
| 112 |
(and (re-search-forward (concat domain "\\.?[\t ]+[0-9wWdDhHmMsS]+[\t ]+" |
|---|
| 113 |
(upcase (or class "IN")) |
|---|
| 114 |
"[\t ]+CNAME[\t ]+\\(.*\\)$") nil t) |
|---|
| 115 |
(dig-extract-rr (match-string 1) type class))))) |
|---|
| 116 |
|
|---|
| 117 |
(defun dig-rr-get-pkix-cert (rr) |
|---|
| 118 |
(let (b e str) |
|---|
| 119 |
(string-match "[^\t ]+[\t ]+[0-9wWdDhHmMsS]+[\t ]+IN[\t ]+CERT[\t ]+\\(1\\|PKIX\\)[\t ]+[0-9]+[\t ]+[0-9]+[\t ]+(?" rr) |
|---|
| 120 |
(setq b (match-end 0)) |
|---|
| 121 |
(string-match ")" rr) |
|---|
| 122 |
(setq e (match-beginning 0)) |
|---|
| 123 |
(setq str (substring rr b e)) |
|---|
| 124 |
(while (string-match "[\t \n\r]" str) |
|---|
| 125 |
(setq str (replace-match "" nil nil str))) |
|---|
| 126 |
str)) |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
(put 'dig-mode 'font-lock-defaults '(dig-font-lock-keywords t)) |
|---|
| 131 |
|
|---|
| 132 |
(put 'dig-mode 'mode-class 'special) |
|---|
| 133 |
|
|---|
| 134 |
(defvar dig-mode-map nil) |
|---|
| 135 |
(unless dig-mode-map |
|---|
| 136 |
(setq dig-mode-map (make-sparse-keymap)) |
|---|
| 137 |
(suppress-keymap dig-mode-map) |
|---|
| 138 |
|
|---|
| 139 |
(define-key dig-mode-map "q" 'dig-exit)) |
|---|
| 140 |
|
|---|
| 141 |
(defun dig-mode () |
|---|
| 142 |
"Major mode for displaying dig output." |
|---|
| 143 |
(interactive) |
|---|
| 144 |
(kill-all-local-variables) |
|---|
| 145 |
(setq mode-name "dig") |
|---|
| 146 |
(setq major-mode 'dig-mode) |
|---|
| 147 |
(use-local-map dig-mode-map) |
|---|
| 148 |
(buffer-disable-undo) |
|---|
| 149 |
(unless (featurep 'xemacs) |
|---|
| 150 |
(set (make-local-variable 'font-lock-defaults) |
|---|
| 151 |
'(dig-font-lock-keywords t))) |
|---|
| 152 |
(when (featurep 'font-lock) |
|---|
| 153 |
(font-lock-set-defaults)) |
|---|
| 154 |
(gnus-run-mode-hooks 'dig-mode-hook)) |
|---|
| 155 |
|
|---|
| 156 |
(defun dig-exit () |
|---|
| 157 |
"Quit dig output buffer." |
|---|
| 158 |
(interactive) |
|---|
| 159 |
(kill-buffer (current-buffer))) |
|---|
| 160 |
|
|---|
| 161 |
(defun dig (domain &optional |
|---|
| 162 |
query-type query-class query-option dig-option server) |
|---|
| 163 |
"Query addresses of a DOMAIN using dig, by calling `dig-invoke'. |
|---|
| 164 |
Optional arguments are passed to `dig-invoke'." |
|---|
| 165 |
(interactive "sHost: ") |
|---|
| 166 |
(switch-to-buffer |
|---|
| 167 |
(dig-invoke domain query-type query-class query-option dig-option server)) |
|---|
| 168 |
(goto-char (point-min)) |
|---|
| 169 |
(and (search-forward ";; ANSWER SECTION:" nil t) |
|---|
| 170 |
(forward-line)) |
|---|
| 171 |
(dig-mode) |
|---|
| 172 |
(setq buffer-read-only t) |
|---|
| 173 |
(set-buffer-modified-p nil)) |
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
(defun query-dig (domain &optional |
|---|
| 177 |
query-type query-class query-option dig-option server) |
|---|
| 178 |
"Query addresses of a DOMAIN using dig. |
|---|
| 179 |
It works by calling `dig-invoke' and `dig-extract-rr'. Optional |
|---|
| 180 |
arguments are passed to `dig-invoke' and `dig-extract-rr'. Returns |
|---|
| 181 |
nil for domain/class/type queries that results in no data." |
|---|
| 182 |
(let ((buffer (dig-invoke domain query-type query-class |
|---|
| 183 |
query-option dig-option server))) |
|---|
| 184 |
(when buffer |
|---|
| 185 |
(switch-to-buffer buffer) |
|---|
| 186 |
(let ((digger (dig-extract-rr domain query-type query-class))) |
|---|
| 187 |
(kill-buffer buffer) |
|---|
| 188 |
digger)))) |
|---|
| 189 |
|
|---|
| 190 |
(provide 'dig) |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|