;;; portmaster.el       Livingston/Lucent PortMaster ComOS utilities
;;; Copyright (c) 2006 Devon Sean McCullough
;;; Licensed under the GPL (GNU Public License)

;;; Thanks to <pmsac@toxyn.org> for revealing at
;;;     http://www.toxyn.org/~pmsac/pm3/MZ/mz.c
;;; that the ComOS password override hash is MD5
;;; of a random challenge plus a constant text,
;;; where each octet is rendered as ASCII 0-Z.

(require 'cl)

(defalias 'mz 'pm-password-override)    ; M-x mz RET

(defun pm-password-override (challenge)
 "Livingston/Lucent PortMaster ComOS password override.
Connect to S0 (not S5 which is where you'd expect S0)
via null modem (25-pin male to 9-pin female RS-232)
at 9600 baud (8N1, i.e., 8 data, no parity, 1 stop)
in, e.g., MS-Win HyperTerm, MacOS ZTerm, Unix cu or tip.
Boot (power up) the PortMaster with DIP switch 1 raised
gently because these are delicate electronic components.

       PortMaster Console login: !root
       Password:override

       CHALLENGE: [[copy the challenge from here]]
       Invalid Login
       PortMaster Console login: !root
       Password:[[paste the response and press return]]
       Command> set password 123
       !root password changed from blablabla to 123
       Command> save all

Based on http://www.toxyn.org/~pmsac/pm3/MZ/mz.c"
 (interactive "sChallenge: ")
 (or (and (= 16 (length challenge))
          (every #'(lambda (c) (and (<= ?0 c) (<= c ?Z)))
                 challenge))
     (error "Bad challenge."))
 (message "Challenge: %s\nResponse: %s"
          challenge
          (map 'string #'(lambda (c) (+ ?0 (% c (1+ (- ?Z ?0)))))
               (let ((m (md5 (concat challenge
                                     "rror: Host Address must be in the format")))
                     (r '()))
                 (dotimes (i 16)
                   (push (string-to-number (substring m (+ i i) (+ i i 2)) 16) r))
                 (nreverse r)))))

