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