1 ;;; dig.el --- Domain Name System dig interface
2 ;; Copyright (c) 2000, 2001, 2003 Free Software Foundation, Inc.
4 ;; Author: Simon Josefsson <simon@josefsson.org>
5 ;; Keywords: DNS BIND dig
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; This provide an interface for "dig".
28 ;; For interactive use, try M-x dig and type a hostname. Use `q' to quit
31 ;; For use in elisp programs, call `dig-invoke' and use
32 ;; `dig-extract-rr' to extract resource records.
36 ;; 2000-10-28 posted on gnu.emacs.sources
40 (eval-when-compile (require 'cl))
45 (defcustom dig-program "dig"
46 "Name of dig (domain information groper) binary."
50 (defcustom dig-dns-server nil
52 If nil, use system defaults."
53 :type '(choice (const :tag "System defaults")
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."
66 (defun dig-invoke (domain &optional
67 query-type query-class query-option
69 "Call dig with given arguments and return buffer containing output.
70 DOMAIN is a string with a DNS domain. QUERY-TYPE is an optional string
71 with a DNS type. QUERY-CLASS is an optional string with a DNS class.
72 QUERY-OPTION is an optional string with dig \"query options\".
73 DIG-OPTIONS is an optional string with parameters for the dig program.
74 SERVER is an optional string with a domain name server to query.
76 Dig is an external program found in the BIND name server distribution,
77 and is a commonly available debugging tool."
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))
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)
90 (defun dig-extract-rr (domain &optional type class)
91 "Extract resource records for DOMAIN, TYPE and CLASS from buffer.
92 Buffer should contain output generated by `dig-invoke'."
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")))
104 (when (search-forward " (" e t)
105 (search-forward " )"))
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)))))
114 (defun dig-rr-get-pkix-cert (rr)
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)))
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))
129 (put 'dig-mode 'mode-class 'special)
131 (defvar dig-mode-map nil)
133 (setq dig-mode-map (make-sparse-keymap))
134 (suppress-keymap dig-mode-map)
136 (define-key dig-mode-map "q" 'dig-exit))
139 "Major mode for displaying dig output."
141 (kill-all-local-variables)
142 (setq mode-name "dig")
143 (setq major-mode 'dig-mode)
144 (use-local-map dig-mode-map)
145 (buffer-disable-undo)
146 (unless (featurep 'xemacs)
147 (set (make-local-variable 'font-lock-defaults)
148 '(dig-font-lock-keywords t)))
149 (when (featurep 'font-lock)
150 (font-lock-set-defaults)))
153 "Quit dig output buffer."
155 (kill-buffer (current-buffer)))
157 (defun dig (domain &optional
158 query-type query-class query-option dig-option server)
159 "Query addresses of a DOMAIN using dig, by calling `dig-invoke'.
160 Optional arguments are passed to `dig-invoke'."
161 (interactive "sHost: ")
163 (dig-invoke domain query-type query-class query-option dig-option server))
164 (goto-char (point-min))
165 (and (search-forward ";; ANSWER SECTION:" nil t)
168 (setq buffer-read-only t)
169 (set-buffer-modified-p nil))
171 ;; named for consistency with query-dns in dns.el
172 (defun query-dig (domain &optional
173 query-type query-class query-option dig-option server)
174 "Query addresses of a DOMAIN using dig.
175 It works by calling `dig-invoke' and `dig-extract-rr'. Optional
176 arguments are passed to `dig-invoke' and `dig-extract-rr'. Returns
177 nil for domain/class/type queries that results in no data."
178 (let ((buffer (dig-invoke domain query-type query-class
179 query-option dig-option server)))
181 (switch-to-buffer buffer)
182 (let ((digger (dig-extract-rr domain query-type query-class)))