dig.el 5.8 KB

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