🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.readers
Article #13984

Re: gnus and authentication

#13984
From: Hallvard B Furuseth
Date: Fri, 23 Dec 1994 13:58
163 lines
6589 bytes
Here is a package I wrote recently.  A lot of the stuff
(unwind-protects, nntp-send-command-and-erase) just attempts to erase
the nntp password from memory (and thus from core dumps); it would be
much simpler without this.  I don't know how it fits into xemacs.


;;; nntp-auth.el -- use the nntp AUTHINFO command
;;
;; This file is in the public domain.
;;
;; NNTP will send the AUTHINFO command, and terminate if it fails.
;; However, the "500" (not implemented) error is ignored.
;;
;; Usage:
;;
;; (setq gnus-open-server-hook   '((lambda () (require 'nntp-auth))))
;; (setq nntp-server-opened-hook '(nntp-try-authinfo))
;;
;; Put your nntp password in ~/.nntp-authinfo and do `chmod og-rwx' on it.
;;
;; Last modified: Nov 1994, Hallvard B Furuseth <h.b.furuseth@usit.uio.no>
;;
;; BUGS:
;;	process-send-string or garbage collection may cause the nntp password
;;	to be retained in memory after it has been sent to nntp.

(require 'nntp)


(defvar nntp-authinfo-file "~/.nntp-authinfo"
  "The name of the file containing your nntp AUTHINFO password")


;; Override nntp.el: Add nntp-server-opened-hook.

(defvar nntp-server-opened-hook)

(defun nntp-open-server (host &optional service)
  "Open news server on HOST.
If HOST is nil, use value of environment variable `NNTPSERVER'.
If optional argument SERVICE is non-nil, open by the service name."
  (let ((host (or host (getenv "NNTPSERVER")))
	(status nil))
    (setq nntp-status-string "")
    (cond ((and host (nntp-open-server-internal host service))
	   (setq status (nntp-wait-for-response "^[23].*\r$"))
	   ;; Do check unexpected close of connection.
	   ;; Suggested by feldmark@hanako.stars.flab.fujitsu.junet.
	   (if status
	       (progn
		 (set-process-sentinel nntp-server-process
				       'nntp-default-sentinel)
		 ;; Added by h.b.furuseth@usit.uio.no
		 (run-hooks 'nntp-server-opened-hook))
	     ;; We have to close connection here, since function
	     ;;  `nntp-server-opened' may return incorrect status.
	     (nntp-close-server-internal)
	     ))
	  ((null host)
	   (setq nntp-status-string "NNTP server is not specified."))
	  )
    status
    ))

;; Copied from nntp-send-command

(defun nntp-send-command-and-erase (response cmd &rest args)
  "As `nntp-send-command', but tries not to leave temporary memory
containing the command or argument string.  Useful to send password commands."
  (save-excursion
    ;; Clear communication buffer.
    (set-buffer nntp-server-buffer)
    (erase-buffer)
    (let (str)
      (unwind-protect
	  (progn
	    (setq str (mapconcat 'identity (cons cmd (append args '("\r\n")))
				 " "))
	    (process-send-string nntp-server-process str))
	(nntp-auth-memzero str)))
    (if response
	(nntp-wait-for-response response)
      t)))


(defun nntp-try-authinfo ()
  (let (password success)
    (unwind-protect
	(progn
	  (cond ((assoc (setq password (nntp-read-authinfo)) '((nil) (""))))
		((not (and (nntp-send-command "^[23].*\r$" "AUTHINFO USER"
					      (user-real-login-name))
			   ;; 381 PASS required
			   (string-match "^3" nntp-status-string)))
		 ;;Fail unless AUTHINFO command was not implemented
		 (or (string-match "^500" nntp-status-string)
		     (error "Authinfo failed: %s" (nntp-status-text))))
		((not (and (nntp-send-command-and-erase
			    "^[23].*\r$" "AUTHINFO PASS" password)
			   (string-match "^2" nntp-status-string))) ; 281 Ok
		 (error "Authentication failed: %s" (nntp-status-text))))
	  (setq success t))
      (nntp-auth-memzero password)
      (or success
	  (nntp-close-server)))))

(defun nntp-status-text ()
  (substring nntp-status-string 0 (string-match "[\n\r]" nntp-status-string)))

(defun nntp-read-authinfo ()
  (let (buf password)
    ;; If file exists, and is not readable or writeable by group
    ;; or other and can be opened for reading and we get the
    ;; username, try the authinfo command.
    (cond ((assq (user-real-login-name) '((nil) ("unknown"))) ;got username?
	   nil)
	  ((not				;File does not exist
	    (setq buf (file-attributes nntp-authinfo-file)))
	   nil)
	  ((or (car buf)		;Not regular file
	       (not (string-match "^-r..--.--.$" (nth 8 buf))))	;unprotected
	   (error "Bad file protection or type on %s" nntp-authinfo-file))
	  ((> (nth 7 buf) 400)
	   ;; It's illegal to send NNTP lines longer than 512 chars incl. CRLF.
	   (error "Password too long in %s" nntp-authinfo-file))
	  (t
	   (prog1
	       ;; Get the password from the authinfo file.
	       ;; Overwrite the buffer memory afterwards.
	       (save-excursion
		 (let ((default-major-mode 'fundamental-mode))
		   (set-buffer (get-buffer-create " *gnus authinfo*")))
		 (setq buf (current-buffer))
		 (erase-buffer)
		 (buffer-flush-undo (current-buffer)) ;No passwds in undo list
		 ;; Insert the file in the middle of the buffer, not at
		 ;; one end.  Then the buffer gap can't fool us when we
		 ;; try to overwrite the password in the buffer's memory.
		 (insert "\n\n")
		 (forward-char -1)
		 (unwind-protect
		     (progn
		       ;; Insert the file, extract the password
		       (insert-file-contents nntp-authinfo-file nil)
		       (end-of-line)
		       (skip-chars-backward " \t\r")
		       (buffer-substring (1+ (point-min)) (point)))
		   ;; Overwrite the memory without moving the buffer gap
		   (insert-char (prog1 (- (buffer-size) 2)
				  (delete-region (1+ (point-min))
						 (1- (point-max)))) ?A)))
	     (kill-buffer buf))))))

(defun nntp-auth-memzero (array-variable)
  "Erase the contents of a string (typically a password)"
  (let ((i (length array-variable)))
    (while (>= (setq i (1- i)) 0)
      (aset array-variable i 0))))

(provide 'nntp-auth)

--
Hallvard
Technical Details
Message-ID: <HBF.94Dec23145824@durin.uio.no>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!archive!mbox2nntp-news.software.readers.mbox.zip!bga.com!news.sprintlink.net!pipex!sunic!trane.uninett.no!nntp.uio.no!usenet
References: <3d98ks$h6i@lorne.stir.ac.uk>

Show Full Headers
Complete Article Headers
Message-ID: <HBF.94Dec23145824@durin.uio.no>
Subject: Re: gnus and authentication
Date: Fri, 23 Dec 1994 13:58:27 +0000
From: h.b.furuseth@usit.uio.no (Hallvard B Furuseth)
References: <3d98ks$h6i@lorne.stir.ac.uk>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!archive!mbox2nntp-news.software.readers.mbox.zip!bga.com!news.sprintlink.net!pipex!sunic!trane.uninett.no!nntp.uio.no!usenet
Newsgroups: gnu.emacs.gnus,news.software.readers
Organization: University of Oslo, Norway
Lines: 163
NNTP-Posting-Host: durin.uio.no
In-reply-to: kmaguire@main.cs.qub.ac.uk's message of 21 Dec 1994 12:55:56 GMT