; password.asm- Password login manager for DOS based systems
; ------------- (c) Chris Evans 2005 gryphonphyre@gamebox.net  1-916-501-1423
; Program query user at console for username and if found prompts for
; assigned password, returns NZ errorlevel if success. Program behaves alike
; a linux/unix login.
; ---------------------------------------------------------------------------
; TO-DO:
;
; [ ] Checksum of user password.
; [ ] one file instead of multiple files (e.g. user.dat)
; 
; ---------------------------------------------------------------------------

LOGINRETRIES    equ 3

code            segment byte public
                assume  cs:code, ds:code
		org	100h
password        proc    far

start:          jmp     main
include         toolbox.asm
                _compile
                _argc dw ?
main:
                mov  bh, '/'
                mov  bl, '?'
                call scancmdline
                mov  _argc, cx
                or   cx, cx
                jz   _noparams
                cmp  dl, 1
                jl   _noparams
                _print help_msg, 1
                jmp  exit
_noparams:
                mov  cx, LOGINRETRIES
_main1:
                call cs:login
                cmp  al, 0
                ja   exit
                loop _main1
exit:           call dosexit
password        endp

; ===========================================================================
login           proc far
; ===========================================================================
                push cx

                _print username, 1
                lea  di, input
                mov  cx, Wi
                call getdata
                _newline

                _fileexist userdat
                jc _openexisting
                _fopen userdat, 0, 3fh, _READONLY
_rloop:
                mov  handle, ax
                lea  di, _buffer
                call freadln
                ; ds:di buffer cx =len
                mov  Wk, ds
                push di
                pop  si
                lea  di, input
                mov  dl, 20h
                call compare
                cmp  bx, 0
                jne  _rdiff
                lea  di, p
                mov  dl, 20h
                call copyz
                mov  Wi, cx
                jmp  _rdone
_rdiff:         mov  ax, _eof
                cmp  ax, 1
                jne  _rloop
_rdone:         _fclose handle

_openexisting:
                _print p, 1
                _newline

                _print pwmsg, 1
                lea  di, s
                mov  cx, Wi
                call getdata
                _newline

                _print s, 1
                _newline

                lea  di, p
                lea  si, s
                mov  cx, Wi
                mov  dl, 0ah
                call compare
                or   ax, ax
                jz   _passok
                _print wrongpw, 1
                mov  al, 0
                jmp  _lext
_passok:        _print rightpw, 1

                _fopen batchfile, 1, 3fh, _WRITEONLY
                mov  handle, ax
                lea  si, batch1
                call fwriteln 
                _fclose handle

                mov  al, 1
_lext:
                pop  cx
                ret

;               _openfile input, 0, 0
;               mov  handle, ax
;               call freadln                ; BX:SI CX
;               move i, cx
;               mov  j, si
;               mov  k, bx
;               _fclose handle

login           endp

handle          dw 0
username        db 'login: ',0
pwmsg           db 'password: ',0
wrongpw         db 'Wrong password entered.',0ffh,0
invalidusername db 'Invalid username.',0ffh,0
rightpw         db 'Password is correct.',0ffh,0
pwdefault       db 'password',0dh,0ah,0
pwtrailer       db '; ---------------------------------------------',0dh,0ah,0
userdat         db 'user.dat',0
batchfile       db 'setuser.bat',0
batch1          db '@set user='
input           db 255 dup(0)
batch2          db '',0ah,0dh,0
s               db 255 dup(0)
p               db 255 dup(0)
Wi              dw ?
Wj              dw ?
Wk              dw ?
help_msg        db 'Password Manager (c) 2005 Chris Evans; gryphonphyre@bluebottle.com',0ffh,0ffh
                db '/add ................................ Adds a user to database',0ffh
                db '/del ................................ Deletes a user from database',0ffh
                db '/reset .............................. Reset database',0ffh,0

code            ends

ProgramEnd      EQU $-01h

		end	start
