Update copyright year to 2014 by running admin/update-copyright.
[bpt/emacs.git] / lisp / net / dig.el
CommitLineData
4c1d5922
GM
1;;; dig.el --- Domain Name System dig interface
2
ba318903 3;; Copyright (C) 2000-2014 Free Software Foundation, Inc.
4c1d5922
GM
4
5;; Author: Simon Josefsson <simon@josefsson.org>
7589d38e 6;; Keywords: DNS BIND dig comm
4c1d5922
GM
7
8;; This file is part of GNU Emacs.
9
874a927a
GM
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.
4c1d5922 14
874a927a
GM
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.
4c1d5922
GM
19
20;; You should have received a copy of the GNU General Public License
874a927a 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
4c1d5922
GM
22
23;;; Commentary:
24
25;; This provide an interface for "dig".
26;;
27;; For interactive use, try M-x dig and type a hostname. Use `q' to quit
28;; dig buffer.
29;;
30;; For use in elisp programs, call `dig-invoke' and use
31;; `dig-extract-rr' to extract resource records.
32
33;;; Release history:
34
35;; 2000-10-28 posted on gnu.emacs.sources
36
37;;; Code:
38
39(eval-when-compile (require 'cl))
40
41(defgroup dig nil
42 "Dig configuration."
43 :group 'comm)
44
45(defcustom dig-program "dig"
46 "Name of dig (domain information groper) binary."
47 :type 'file
48 :group 'dig)
49
50(defcustom dig-dns-server nil
51 "DNS server to query.
52If nil, use system defaults."
53 :type '(choice (const :tag "System defaults")
54 string)
55 :group 'dig)
56
57(defcustom dig-font-lock-keywords
58 '(("^;; [A-Z]+ SECTION:" 0 font-lock-keyword-face)
59 ("^;;.*" 0 font-lock-comment-face)
60 ("^; <<>>.*" 0 font-lock-type-face)
61 ("^;.*" 0 font-lock-function-name-face))
62 "Default expressions to highlight in dig mode."
63 :type 'sexp
64 :group 'dig)
65
66(defun dig-invoke (domain &optional
67 query-type query-class query-option
68 dig-option server)
69 "Call dig with given arguments and return buffer containing output.
5a0c3f56
JB
70DOMAIN is a string with a DNS domain. QUERY-TYPE is an optional
71string with a DNS type. QUERY-CLASS is an optional string with a DNS
72class. QUERY-OPTION is an optional string with dig \"query options\".
73DIG-OPTION is an optional string with parameters for the dig program.
4c1d5922
GM
74SERVER is an optional string with a domain name server to query.
75
76Dig is an external program found in the BIND name server distribution,
77and is a commonly available debugging tool."
78 (let (buf cmdline)
79 (setq buf (generate-new-buffer "*dig output*"))
80 (if dig-option (push dig-option cmdline))
81 (if query-option (push query-option cmdline))
82 (if query-class (push query-class cmdline))
83 (if query-type (push query-type cmdline))
84 (push domain cmdline)
85 (if server (push (concat "@" server) cmdline)
86 (if dig-dns-server (push (concat "@" dig-dns-server) cmdline)))
87 (apply 'call-process dig-program nil buf nil cmdline)
88 buf))
89
90(defun dig-extract-rr (domain &optional type class)
91 "Extract resource records for DOMAIN, TYPE and CLASS from buffer.
92Buffer should contain output generated by `dig-invoke'."
93 (save-excursion
94 (goto-char (point-min))
95 (if (re-search-forward
96 (concat domain "\\.?[\t ]+[0-9wWdDhHmMsS]+[\t ]+"
97 (upcase (or class "IN")) "[\t ]+" (upcase (or type "A")))
98 nil t)
99 (let (b e)
100 (end-of-line)
101 (setq e (point))
102 (beginning-of-line)
103 (setq b (point))
104 (when (search-forward " (" e t)
105 (search-forward " )"))
106 (end-of-line)
107 (setq e (point))
108 (buffer-substring b e))
109 (and (re-search-forward (concat domain "\\.?[\t ]+[0-9wWdDhHmMsS]+[\t ]+"
110 (upcase (or class "IN"))
111 "[\t ]+CNAME[\t ]+\\(.*\\)$") nil t)
112 (dig-extract-rr (match-string 1) type class)))))
113
114(defun dig-rr-get-pkix-cert (rr)
115 (let (b e str)
116 (string-match "[^\t ]+[\t ]+[0-9wWdDhHmMsS]+[\t ]+IN[\t ]+CERT[\t ]+\\(1\\|PKIX\\)[\t ]+[0-9]+[\t ]+[0-9]+[\t ]+(?" rr)
117 (setq b (match-end 0))
118 (string-match ")" rr)
119 (setq e (match-beginning 0))
120 (setq str (substring rr b e))
121 (while (string-match "[\t \n\r]" str)
122 (setq str (replace-match "" nil nil str)))
123 str))
124
125;; XEmacs does it like this. For Emacs, we have to set the
126;; `font-lock-defaults' buffer-local variable.
127(put 'dig-mode 'font-lock-defaults '(dig-font-lock-keywords t))
128
129(put 'dig-mode 'mode-class 'special)
130
a0310a6c
DN
131(defvar dig-mode-map
132 (let ((map (make-sparse-keymap)))
133 (suppress-keymap map)
134 (define-key map "q" 'dig-exit)
135 map))
4c1d5922 136
5ac42715 137(define-derived-mode dig-mode nil "Dig"
4c1d5922 138 "Major mode for displaying dig output."
4c1d5922
GM
139 (buffer-disable-undo)
140 (unless (featurep 'xemacs)
141 (set (make-local-variable 'font-lock-defaults)
142 '(dig-font-lock-keywords t)))
143 (when (featurep 'font-lock)
5ac42715 144 ;; FIXME: what is this for?? --Stef
4c1d5922 145 (font-lock-set-defaults))
5ac42715 146 )
4c1d5922
GM
147
148(defun dig-exit ()
149 "Quit dig output buffer."
150 (interactive)
151 (kill-buffer (current-buffer)))
152
d54cc599 153;;;###autoload
4c1d5922
GM
154(defun dig (domain &optional
155 query-type query-class query-option dig-option server)
156 "Query addresses of a DOMAIN using dig, by calling `dig-invoke'.
157Optional arguments are passed to `dig-invoke'."
158 (interactive "sHost: ")
159 (switch-to-buffer
160 (dig-invoke domain query-type query-class query-option dig-option server))
161 (goto-char (point-min))
162 (and (search-forward ";; ANSWER SECTION:" nil t)
163 (forward-line))
164 (dig-mode)
165 (setq buffer-read-only t)
166 (set-buffer-modified-p nil))
167
168;; named for consistency with query-dns in dns.el
169(defun query-dig (domain &optional
170 query-type query-class query-option dig-option server)
171 "Query addresses of a DOMAIN using dig.
5a0c3f56
JB
172It works by calling `dig-invoke' and `dig-extract-rr'.
173Optional arguments are passed to `dig-invoke' and `dig-extract-rr'.
174Returns nil for domain/class/type queries that result in no data."
4c1d5922
GM
175(let ((buffer (dig-invoke domain query-type query-class
176 query-option dig-option server)))
177 (when buffer
178 (switch-to-buffer buffer)
179 (let ((digger (dig-extract-rr domain query-type query-class)))
180 (kill-buffer buffer)
181 digger))))
182
183(provide 'dig)
184
4c1d5922 185;;; dig.el ends here