socks.el 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. ;;; socks.el --- A Socks v5 Client for Emacs
  2. ;; Copyright (C) 1996-2000, 2002, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: William M. Perry <wmperry@gnu.org>
  4. ;; Dave Love <fx@gnu.org>
  5. ;; Keywords: comm, firewalls
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This is an implementation of the SOCKS v5 protocol as defined in
  19. ;; RFC 1928.
  20. ;; TODO
  21. ;; - Finish the redirection rules stuff
  22. ;; - Implement composition of servers. Recursively evaluate the
  23. ;; redirection rules and do SOCKS-over-HTTP and SOCKS-in-SOCKS
  24. (eval-when-compile
  25. (require 'wid-edit))
  26. (require 'custom)
  27. ;; FIXME this is bad practice, and who is it for anyway, since Emacs
  28. ;; has split-string since at least 21.1.
  29. (if (not (fboundp 'split-string))
  30. (defun split-string (string &optional pattern)
  31. "Return a list of substrings of STRING which are separated by PATTERN.
  32. If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
  33. (or pattern
  34. (setq pattern "[ \f\t\n\r\v]+"))
  35. (let (parts (start 0))
  36. (while (string-match pattern string start)
  37. (setq parts (cons (substring string start (match-beginning 0)) parts)
  38. start (match-end 0)))
  39. (nreverse (cons (substring string start) parts)))))
  40. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  41. ;;; Custom widgets
  42. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  43. (define-widget 'dynamic-choice 'menu-choice
  44. "A pretty simple dynamic dropdown list"
  45. :format "%[%t%]: %v"
  46. :tag "Network"
  47. :case-fold t
  48. :void '(item :format "invalid (%t)\n")
  49. :value-create 's5-widget-value-create
  50. :value-delete 'widget-children-value-delete
  51. :value-get 'widget-choice-value-get
  52. :value-inline 'widget-choice-value-inline
  53. :mouse-down-action 'widget-choice-mouse-down-action
  54. :action 'widget-choice-action
  55. :error "Make a choice"
  56. :validate 'widget-choice-validate
  57. :match 's5-dynamic-choice-match
  58. :match-inline 's5-dynamic-choice-match-inline)
  59. (defun s5-dynamic-choice-match (widget value)
  60. (let ((choices (funcall (widget-get widget :choice-function)))
  61. current found)
  62. (while (and choices (not found))
  63. (setq current (car choices)
  64. choices (cdr choices)
  65. found (widget-apply current :match value)))
  66. found))
  67. (defun s5-dynamic-choice-match-inline (widget value)
  68. (let ((choices (funcall (widget-get widget :choice-function)))
  69. current found)
  70. (while (and choices (not found))
  71. (setq current (car choices)
  72. choices (cdr choices)
  73. found (widget-match-inline current value)))
  74. found))
  75. (defun s5-widget-value-create (widget)
  76. (let ((choices (funcall (widget-get widget :choice-function)))
  77. (value (widget-get widget :value)))
  78. (if (not value)
  79. (widget-put widget :value (widget-value (car choices))))
  80. (widget-put widget :args choices)
  81. (widget-choice-value-create widget)))
  82. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  83. ;;; Customization support
  84. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  85. (defgroup socks nil
  86. "SOCKS Support"
  87. :version "22.2"
  88. :prefix "socks-"
  89. :group 'processes)
  90. '(defcustom socks-server-aliases nil
  91. "A list of server aliases for use in access control and filtering rules."
  92. :group 'socks
  93. :type '(repeat (list :format "%v"
  94. :value ("" "" 1080 5)
  95. (string :tag "Alias")
  96. (string :tag "Hostname/IP Address")
  97. (integer :tag "Port #")
  98. (choice :tag "SOCKS Version"
  99. (integer :tag "SOCKS v4" :value 4)
  100. (integer :tag "SOCKS v5" :value 5)))))
  101. '(defcustom socks-network-aliases
  102. '(("Anywhere" (netmask "0.0.0.0" "0.0.0.0")))
  103. "A list of network aliases for use in subsequent rules."
  104. :group 'socks
  105. :type '(repeat (list :format "%v"
  106. :value (netmask "" "255.255.255.0")
  107. (string :tag "Alias")
  108. (radio-button-choice
  109. :format "%v"
  110. (list :tag "IP address range"
  111. (const :format "" :value range)
  112. (string :tag "From")
  113. (string :tag "To"))
  114. (list :tag "IP address/netmask"
  115. (const :format "" :value netmask)
  116. (string :tag "IP Address")
  117. (string :tag "Netmask"))
  118. (list :tag "Domain Name"
  119. (const :format "" :value domain)
  120. (string :tag "Domain name"))
  121. (list :tag "Unique hostname/IP address"
  122. (const :format "" :value exact)
  123. (string :tag "Hostname/IP Address"))))))
  124. '(defun s5-servers-filter ()
  125. (if socks-server-aliases
  126. (mapcar (lambda (x) (list 'const :tag (car x) :value (car x))) s5-server-aliases)
  127. '((const :tag "No aliases defined" :value nil))))
  128. '(defun s5-network-aliases-filter ()
  129. (mapcar (lambda (x) (list 'const :tag (car x) :value (car x)))
  130. socks-network-aliases))
  131. '(defcustom socks-redirection-rules
  132. nil
  133. "A list of redirection rules."
  134. :group 'socks
  135. :type '(repeat (list :format "%v"
  136. :value ("Anywhere" nil)
  137. (dynamic-choice :choice-function s5-network-aliases-filter
  138. :tag "Destination network")
  139. (radio-button-choice
  140. :tag "Connection type"
  141. (const :tag "Direct connection" :value nil)
  142. (dynamic-choice :format "%t: %[%v%]"
  143. :choice-function s5-servers-filter
  144. :tag "Proxy chain via")))))
  145. (defcustom socks-server
  146. (list "Default server" "socks" 1080 5)
  147. ""
  148. :group 'socks
  149. :type '(list
  150. (string :format "" :value "Default server")
  151. (string :tag "Server")
  152. (integer :tag "Port")
  153. (radio-button-choice :tag "SOCKS Version"
  154. :format "%t: %v"
  155. (const :tag "SOCKS v4 " :format "%t" :value 4)
  156. (const :tag "SOCKS v5" :format "%t" :value 5))))
  157. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  158. ;;; Get down to the nitty gritty
  159. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  160. (defconst socks-version 5)
  161. (defvar socks-debug nil)
  162. ;; Common socks v5 commands
  163. (defconst socks-connect-command 1)
  164. (defconst socks-bind-command 2)
  165. (defconst socks-udp-associate-command 3)
  166. ;; Miscellaneous other socks constants
  167. (defconst socks-authentication-null 0)
  168. (defconst socks-authentication-failure 255)
  169. ;; Response codes
  170. (defconst socks-response-success 0)
  171. (defconst socks-response-general-failure 1)
  172. (defconst socks-response-access-denied 2)
  173. (defconst socks-response-network-unreachable 3)
  174. (defconst socks-response-host-unreachable 4)
  175. (defconst socks-response-connection-refused 5)
  176. (defconst socks-response-ttl-expired 6)
  177. (defconst socks-response-cmd-not-supported 7)
  178. (defconst socks-response-address-not-supported 8)
  179. (defvar socks-errors
  180. '("Succeeded"
  181. "General SOCKS server failure"
  182. "Connection not allowed by ruleset"
  183. "Network unreachable"
  184. "Host unreachable"
  185. "Connection refused"
  186. "Time-to-live expired"
  187. "Command not supported"
  188. "Address type not supported"))
  189. ;; The socks v5 address types
  190. (defconst socks-address-type-v4 1)
  191. (defconst socks-address-type-name 3)
  192. (defconst socks-address-type-v6 4)
  193. ;; Base variables
  194. (defvar socks-timeout 5)
  195. (defvar socks-connections (make-hash-table :size 13))
  196. ;; Miscellaneous stuff for authentication
  197. (defvar socks-authentication-methods nil)
  198. (defvar socks-username (user-login-name))
  199. (defvar socks-password nil)
  200. (defun socks-register-authentication-method (id desc callback)
  201. (let ((old (assq id socks-authentication-methods)))
  202. (if old
  203. (setcdr old (cons desc callback))
  204. (setq socks-authentication-methods
  205. (cons (cons id (cons desc callback))
  206. socks-authentication-methods)))))
  207. (defun socks-unregister-authentication-method (id)
  208. (let ((old (assq id socks-authentication-methods)))
  209. (if old
  210. (setq socks-authentication-methods
  211. (delq old socks-authentication-methods)))))
  212. (socks-register-authentication-method 0 "No authentication" 'identity)
  213. (defun socks-build-auth-list ()
  214. (let ((num 0)
  215. (retval ""))
  216. (mapc
  217. (function
  218. (lambda (x)
  219. (if (fboundp (cdr (cdr x)))
  220. (setq retval (format "%s%c" retval (car x))
  221. num (1+ num)))))
  222. (reverse socks-authentication-methods))
  223. (format "%c%s" num retval)))
  224. (defconst socks-state-waiting-for-auth 0)
  225. (defconst socks-state-submethod-negotiation 1)
  226. (defconst socks-state-authenticated 2)
  227. (defconst socks-state-waiting 3)
  228. (defconst socks-state-connected 4)
  229. (defmacro socks-wait-for-state-change (proc htable cur-state)
  230. `(while (and (= (gethash 'state ,htable) ,cur-state)
  231. (memq (process-status ,proc) '(run open)))
  232. (accept-process-output ,proc socks-timeout)))
  233. (defun socks-filter (proc string)
  234. (let ((info (gethash proc socks-connections))
  235. state version desired-len)
  236. (or info (error "socks-filter called on non-SOCKS connection %S" proc))
  237. (setq state (gethash 'state info))
  238. (cond
  239. ((= state socks-state-waiting-for-auth)
  240. (puthash 'scratch (concat string (gethash 'scratch info)) info)
  241. (setq string (gethash 'scratch info))
  242. (if (< (length string) 2)
  243. nil ; We need to spin some more
  244. (puthash 'authtype (aref string 1) info)
  245. (puthash 'scratch (substring string 2 nil) info)
  246. (puthash 'state socks-state-submethod-negotiation info)))
  247. ((= state socks-state-submethod-negotiation)
  248. )
  249. ((= state socks-state-authenticated)
  250. )
  251. ((= state socks-state-waiting)
  252. (puthash 'scratch (concat string (gethash 'scratch info)) info)
  253. (setq string (gethash 'scratch info))
  254. (setq version (gethash 'server-protocol info))
  255. (cond
  256. ((equal version 'http)
  257. (if (not (string-match "\r\n\r\n" string))
  258. nil ; Need to spin some more
  259. (puthash 'state socks-state-connected info)
  260. (puthash 'reply 0 info)
  261. (puthash 'response string info)))
  262. ((equal version 4)
  263. (if (< (length string) 2)
  264. nil ; Can't know how much to read yet
  265. (setq desired-len
  266. (+ 4 ; address length
  267. 2 ; port
  268. 2 ; initial data
  269. ))
  270. (if (< (length string) desired-len)
  271. nil ; need to spin some more
  272. (let ((response (aref string 1)))
  273. (if (= response 90)
  274. (setq response 0))
  275. (puthash 'state socks-state-connected info)
  276. (puthash 'reply response info)
  277. (puthash 'response string info)))))
  278. ((equal version 5)
  279. (if (< (length string) 4)
  280. nil
  281. (setq desired-len
  282. (+ 6 ; Standard socks header
  283. (cond
  284. ((= (aref string 3) socks-address-type-v4) 4)
  285. ((= (aref string 3) socks-address-type-v6) 16)
  286. ((= (aref string 3) socks-address-type-name)
  287. (if (< (length string) 5)
  288. 255
  289. (+ 1 (aref string 4)))))))
  290. (if (< (length string) desired-len)
  291. nil ; Need to spin some more
  292. (puthash 'state socks-state-connected info)
  293. (puthash 'reply (aref string 1) info)
  294. (puthash 'response string info))))))
  295. ((= state socks-state-connected)
  296. )
  297. )
  298. )
  299. )
  300. (declare-function socks-original-open-network-stream "socks") ; fset
  301. ;; FIXME this is a terrible idea.
  302. ;; It is not even compatible with the argument spec of open-network-stream
  303. ;; in 24.1. If this is really necessary, open-network-stream
  304. ;; could get a wrapper hook, or defer to open-network-stream-function.
  305. (defvar socks-override-functions nil
  306. "*Whether to overwrite the open-network-stream function with the SOCKSified
  307. version.")
  308. (require 'network-stream)
  309. (if (fboundp 'socks-original-open-network-stream)
  310. nil ; Do nothing, we've been here already
  311. (defalias 'socks-original-open-network-stream
  312. (symbol-function 'open-network-stream))
  313. (if socks-override-functions
  314. (defalias 'open-network-stream 'socks-open-network-stream)))
  315. (defun socks-open-connection (server-info)
  316. (interactive)
  317. (save-excursion
  318. (let ((proc (socks-original-open-network-stream "socks"
  319. nil
  320. (nth 1 server-info)
  321. (nth 2 server-info)))
  322. (info (make-hash-table :size 13))
  323. (authtype nil)
  324. version)
  325. ;; Initialize process and info about the process
  326. (set-process-filter proc 'socks-filter)
  327. (set-process-query-on-exit-flag proc nil)
  328. (puthash proc info socks-connections)
  329. (puthash 'state socks-state-waiting-for-auth info)
  330. (puthash 'authtype socks-authentication-failure info)
  331. (puthash 'server-protocol (nth 3 server-info) info)
  332. (puthash 'server-name (nth 1 server-info) info)
  333. (setq version (nth 3 server-info))
  334. (cond
  335. ((equal version 'http)
  336. ;; Don't really have to do any connection setup under http
  337. nil)
  338. ((equal version 4)
  339. ;; Don't really have to do any connection setup under v4
  340. nil)
  341. ((equal version 5)
  342. ;; Need to handle all the authentication crap under v5
  343. ;; Send what we think we can handle for authentication types
  344. (process-send-string proc (format "%c%s" socks-version
  345. (socks-build-auth-list)))
  346. ;; Basically just do a select() until we change states.
  347. (socks-wait-for-state-change proc info socks-state-waiting-for-auth)
  348. (setq authtype (gethash 'authtype info))
  349. (cond
  350. ((= authtype socks-authentication-null)
  351. (and socks-debug (message "No authentication necessary")))
  352. ((= authtype socks-authentication-failure)
  353. (error "No acceptable authentication methods found"))
  354. (t
  355. (let* ((auth-type (gethash 'authtype info))
  356. (auth-handler (assoc auth-type socks-authentication-methods))
  357. (auth-func (and auth-handler (cdr (cdr auth-handler))))
  358. (auth-desc (and auth-handler (car (cdr auth-handler)))))
  359. (set-process-filter proc nil)
  360. (if (and auth-func (fboundp auth-func)
  361. (funcall auth-func proc))
  362. nil ; We succeeded!
  363. (delete-process proc)
  364. (error "Failed to use auth method: %s (%d)"
  365. (or auth-desc "Unknown") auth-type))
  366. )
  367. )
  368. )
  369. (puthash 'state socks-state-authenticated info)
  370. (set-process-filter proc 'socks-filter)))
  371. proc)))
  372. (defun socks-send-command (proc command atype address port)
  373. (let ((addr (cond
  374. ((or (= atype socks-address-type-v4)
  375. (= atype socks-address-type-v6))
  376. address)
  377. ((= atype socks-address-type-name)
  378. (format "%c%s" (length address) address))
  379. (t
  380. (error "Unknown address type: %d" atype))))
  381. (info (gethash proc socks-connections))
  382. request version)
  383. (or info (error "socks-send-command called on non-SOCKS connection %S"
  384. proc))
  385. (puthash 'state socks-state-waiting info)
  386. (setq version (gethash 'server-protocol info))
  387. (cond
  388. ((equal version 'http)
  389. (setq request (format (eval-when-compile
  390. (concat
  391. "CONNECT %s:%d HTTP/1.0\r\n"
  392. "User-Agent: Emacs/SOCKS v1.0\r\n"
  393. "\r\n"))
  394. (cond
  395. ((equal atype socks-address-type-name) address)
  396. (t
  397. (error "Unsupported address type for HTTP: %d" atype)))
  398. port)))
  399. ((equal version 4)
  400. (setq request (string-make-unibyte
  401. (format
  402. "%c%c%c%c%s%s%c"
  403. version ; version
  404. command ; command
  405. (lsh port -8) ; port, high byte
  406. (- port (lsh (lsh port -8) 8)) ; port, low byte
  407. addr ; address
  408. (user-full-name) ; username
  409. 0 ; terminate username
  410. ))))
  411. ((equal version 5)
  412. (setq request (string-make-unibyte
  413. (format
  414. "%c%c%c%c%s%c%c"
  415. version ; version
  416. command ; command
  417. 0 ; reserved
  418. atype ; address type
  419. addr ; address
  420. (lsh port -8) ; port, high byte
  421. (- port (lsh (lsh port -8) 8)) ; port, low byte
  422. ))))
  423. (t
  424. (error "Unknown protocol version: %d" version)))
  425. (process-send-string proc request)
  426. (socks-wait-for-state-change proc info socks-state-waiting)
  427. (process-status proc)
  428. (if (= (or (gethash 'reply info) 1) socks-response-success)
  429. nil ; Sweet sweet success!
  430. (delete-process proc)
  431. (error "SOCKS: %s" (nth (or (gethash 'reply info) 1) socks-errors)))
  432. proc))
  433. ;; Replacement functions for open-network-stream, etc.
  434. (defvar socks-noproxy nil
  435. "*List of regexps matching hosts that we should not socksify connections to")
  436. (defun socks-find-route (host service)
  437. (let ((route socks-server)
  438. (noproxy socks-noproxy))
  439. (while noproxy
  440. (if (eq ?! (aref (car noproxy) 0))
  441. (if (string-match (substring (car noproxy) 1) host)
  442. (setq noproxy nil))
  443. (if (string-match (car noproxy) host)
  444. (setq route nil
  445. noproxy nil)))
  446. (setq noproxy (cdr noproxy)))
  447. route))
  448. (defvar socks-services-file "/etc/services")
  449. (defvar socks-tcp-services (make-hash-table :size 13 :test 'equal))
  450. (defvar socks-udp-services (make-hash-table :size 13 :test 'equal))
  451. (defun socks-parse-services ()
  452. (if (not (and (file-exists-p socks-services-file)
  453. (file-readable-p socks-services-file)))
  454. (error "Could not find services file: %s" socks-services-file))
  455. (clrhash socks-tcp-services)
  456. (clrhash socks-udp-services)
  457. (with-current-buffer (get-buffer-create " *socks-tmp*")
  458. (erase-buffer)
  459. (insert-file-contents socks-services-file)
  460. ;; Nuke comments
  461. (goto-char (point-min))
  462. (while (re-search-forward "#.*" nil t)
  463. (replace-match ""))
  464. ;; Nuke empty lines
  465. (goto-char (point-min))
  466. (while (re-search-forward "^[ \t\n]+" nil t)
  467. (replace-match ""))
  468. ;; Now find all the lines
  469. (goto-char (point-min))
  470. (let (name port type)
  471. (while (re-search-forward "^\\([^ \t]+\\)[ \t]+\\([0-9]+\\)/\\([a-z]+\\)"
  472. nil t)
  473. (setq name (downcase (match-string 1))
  474. port (string-to-number (match-string 2))
  475. type (downcase (match-string 3)))
  476. (puthash name port (if (equal type "udp")
  477. socks-udp-services
  478. socks-tcp-services))))))
  479. (defun socks-find-services-entry (service &optional udp)
  480. "Return the port # associated with SERVICE"
  481. (if (= (hash-table-count socks-tcp-services) 0)
  482. (socks-parse-services))
  483. (gethash (downcase service)
  484. (if udp socks-udp-services socks-tcp-services)))
  485. (defun socks-open-network-stream (name buffer host service)
  486. (let* ((route (socks-find-route host service))
  487. proc info version atype)
  488. (if (not route)
  489. (socks-original-open-network-stream name buffer host service)
  490. (setq proc (socks-open-connection route)
  491. info (gethash proc socks-connections)
  492. version (gethash 'server-protocol info))
  493. (cond
  494. ((equal version 4)
  495. (setq host (socks-nslookup-host host))
  496. (if (not (listp host))
  497. (error "Could not get IP address for: %s" host))
  498. (setq host (apply 'format "%c%c%c%c" host))
  499. (setq atype socks-address-type-v4))
  500. (t
  501. (setq atype socks-address-type-name)))
  502. (socks-send-command proc
  503. socks-connect-command
  504. atype
  505. host
  506. (if (stringp service)
  507. (or
  508. (socks-find-services-entry service)
  509. (error "Unknown service: %s" service))
  510. service))
  511. (puthash 'buffer buffer info)
  512. (puthash 'host host info)
  513. (puthash 'service host info)
  514. (set-process-filter proc nil)
  515. (set-process-buffer proc (if buffer (get-buffer-create buffer)))
  516. proc)))
  517. ;; Authentication modules go here
  518. ;; Basic username/password authentication, ala RFC 1929
  519. (socks-register-authentication-method 2 "Username/Password"
  520. 'socks-username/password-auth)
  521. (defconst socks-username/password-auth-version 1)
  522. (defun socks-username/password-auth-filter (proc str)
  523. (let ((info (gethash proc socks-connections)))
  524. (or info (error "socks-filter called on non-SOCKS connection %S" proc))
  525. (puthash 'scratch (concat (gethash 'scratch info) str) info)
  526. (if (< (length (gethash 'scratch info)) 2)
  527. nil
  528. (puthash 'password-auth-status (aref (gethash 'scratch info) 1) info)
  529. (puthash 'state socks-state-authenticated info))))
  530. (defun socks-username/password-auth (proc)
  531. (let* ((info (gethash proc socks-connections))
  532. (state (gethash 'state info)))
  533. (if (not socks-password)
  534. (setq socks-password (read-passwd
  535. (format "Password for %s@%s: "
  536. socks-username
  537. (gethash 'server-name info)))))
  538. (puthash 'scratch "" info)
  539. (set-process-filter proc 'socks-username/password-auth-filter)
  540. (process-send-string proc
  541. (format "%c%c%s%c%s"
  542. socks-username/password-auth-version
  543. (length socks-username)
  544. socks-username
  545. (length socks-password)
  546. socks-password))
  547. (socks-wait-for-state-change proc info state)
  548. (= (gethash 'password-auth-status info) 0)))
  549. ;; More advanced GSS/API stuff, not yet implemented - volunteers?
  550. ;; (socks-register-authentication-method 1 "GSS/API" 'socks-gssapi-auth)
  551. (defun socks-gssapi-auth (proc)
  552. nil)
  553. ;; CHAP stuff
  554. ;; (socks-register-authentication-method 3 "CHAP" 'socks-chap-auth)
  555. (defun socks-chap-auth (proc)
  556. nil)
  557. ;; CRAM stuff
  558. ;; (socks-register-authentication-method 5 "CRAM" 'socks-cram-auth)
  559. (defun socks-cram-auth (proc)
  560. nil)
  561. (defcustom socks-nslookup-program "nslookup"
  562. "*If non-NIL then a string naming the nslookup program."
  563. :type '(choice (const :tag "None" :value nil) string)
  564. :group 'socks)
  565. (defun socks-nslookup-host (host)
  566. "Attempt to resolve the given HOSTNAME using nslookup if possible."
  567. (interactive "sHost: ")
  568. (if socks-nslookup-program
  569. (let ((proc (start-process " *nslookup*" " *nslookup*"
  570. socks-nslookup-program host))
  571. (res host))
  572. (set-process-query-on-exit-flag proc nil)
  573. (with-current-buffer (process-buffer proc)
  574. (while (progn
  575. (accept-process-output proc)
  576. (memq (process-status proc) '(run open))))
  577. (goto-char (point-min))
  578. (if (re-search-forward "Name:.*\nAddress\\(es\\)?: *\\([0-9.]+\\)$" nil t)
  579. (progn
  580. (setq res (buffer-substring (match-beginning 2)
  581. (match-end 2))
  582. res (mapcar 'string-to-int (split-string res "\\.")))))
  583. (kill-buffer (current-buffer)))
  584. res)
  585. host))
  586. (provide 'socks)
  587. ;;; socks.el ends here