dig.el 6.0 KB

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