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