NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF sbin/login
*--------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
				.INB inc/kernel.i
				.INB inc/mli.i
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPhBuf			.BS 1
ZPBufPtr		.BS 2

ZPRetryCnt		.BS 1
ZPbValidUser	.BS 1
ZPUID			.BS 1

ZPGetLinePtr	.BS 2
ZPbGetLineSecret	.BS 1
ZPbEsc			.BS 1
ZPGetLineLen	.BS 1
ZPGetLineMax	.BS 1

ZPNewSID		.BS 1
ZPOldSID		.BS 1

ZPNewSession	.BS 1
ZPOldSession	.BS 1

ZPhFile			.BS 1

ZS.END			.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA #S.PS.F.EVENT		S.PS.F
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data Segment Size
				.DA #16					Stack Size
				.DA #ZS.END-ZS.START	Zero Page Size
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT
				.DA	CS.QUIT
L.MSG.LOGIN		.DA MSG.LOGIN
L.MSG.PASSWORD	.DA MSG.PASSWORD
L.MSG.BAD		.DA MSG.BAD
L.MSG.BS		.DA MSG.BS
L.MSG.CRLF		.DA MSG.CRLF
L.ETCNOAUTH		.DA ETCNOAUTH
L.ETCISSUE		.DA ETCISSUE
L.ETCMOTD		.DA ETCMOTD
L.SHELL			.DA SHELL
				.DA 0
*--------------------------------------
CS.QUIT
*--------------------------------------
CS.INIT			clc
CS.INIT.RTS		rts
*--------------------------------------
CS.RUN			>LDYA L.ETCISSUE
				jsr CS.RUN.DumpFile

				lda #0
				>PUSHA					ROOT user
				>PUSHA
				>PUSHA					Dry Run, no output
				>SYSCALL GetPWUID
				bcc CS.RUN.AUTH			
*--------------------------------------
				>LDYA L.ETCNOAUTH
				jsr CS.RUN.DumpFile

				jsr CS.RUN.EXECPS
				bcs .9
				
				ldy #S.PS.RC
				lda (pPS),y

				sec

.9				rts
*--------------------------------------
CS.RUN.AUTH		lda #3
				sta ZPRetryCnt

.1				>LDYA L.MSG.LOGIN
				jsr CS.RUN.printf
				bcs .9

				>LEA.G Username
				ldx #16
				clc
				jsr CS.RUN.GetLine
				bcs .9

				>LDYA L.MSG.PASSWORD
				jsr CS.RUN.printf
				bcs .9

				>LDYA pData					>LEA.G Password
				ldx #32
				sec
				jsr CS.RUN.GetLine
				bcs .9

				>LDYA L.MSG.CRLF
				>SYSCALL PutS
				bcs .9
				
				>PUSHW pData				>PUSHEA.G Password
				>PUSHW pData				>PUSHEA.G Password
				
				>SYSCALL MD5
				
				jsr CS.RUN.EXEC
				bcc .9

				>LDYA L.MSG.BAD
				>SYSCALL PutS
				bcs .9

				dec ZPRetryCnt
				bne .1

				lda #E.IUSR
				sec
.9				rts
*--------------------------------------
CS.RUN.EXEC		>PUSHEA.G Username
				>PUSHW pData				>PUSHEA.G Password

				>SYSCALL OpenSession
				bcs .9

				sta ZPNewSID

				ldx #1

.1				lda USRs.hSID-1,x
				beq .2

				inx
				cpx #K.USR.MAX+1
				bne .1

				lda #E.OOH
*				sec
				rts	

.2				stx ZPNewSession
				ldy #S.PS.hSession
				lda (pPS),y
				sta ZPOldSession
				
				txa
				sta (pPS),y

				lda ZPNewSID
				sta USRs.hSID-1,x

				ldy #S.PS.hStdIn
				lda (pPS),y
				sta USRs.hFile-1,x

				jsr CS.RUN.EXECPS
				
				lda ZPNewSID
				
				>SYSCALL CloseSession
				
				stz ZPNewSID
				
.8				ldx ZPNewSession
				stz USRs.hSID-1,x
				stz USRs.hFile-1,x
				
				ldy #S.PS.hSession
				lda ZPOldSession
				sta (pPS),y
				
				ldy #S.PS.RC
				lda (pPS),y

				sec

.9				rts
*--------------------------------------
CS.RUN.EXECPS	>LDYA L.ETCMOTD
				jsr CS.RUN.DumpFile

				>PUSHW L.SHELL
				>PUSHBI S.PS.F.HOLD
				>SYSCALL ExecL
				bcs .9

				>SLEEP
*				clc
.9				rts
*--------------------------------------
CS.RUN.GetLine	>STYA ZPGetLinePtr
				stx ZPGetLineMax
				ror ZPbGetLineSecret

				stz ZPGetLineLen
				stz ZPbEsc

.1				>SYSCALL GetChar
				bcs .9

				bit ZPbEsc
				bpl .11

*				clc
				ror ZPbEsc
				cmp #'D'
				bne .1

				bra .3

.11				cmp #C.DEL
				beq .3

				cmp #C.SPACE
				bcc .2

				ldy ZPGetLineLen
				cpy ZPGetLineMax
				beq .1

				sta (ZPGetLinePtr),y
				inc ZPGetLineLen
				bit ZPbGetLineSecret
				bmi .1

				>SYSCALL PutChar
				bra .1

.2				cmp #3					Ctrl-C
				beq .9

				cmp #C.CR
				beq .8

				cmp #C.ESC
				bne .1

*				sec
				ror ZPbEsc
				bra .1

.3				ldy ZPGetLineLen
				beq .1

				dec ZPGetLineLen
				bit ZPbGetLineSecret
				bmi .1

				>LDYA L.MSG.BS
				jsr CS.RUN.printf
				bra .1

.8				ldy ZPGetLineLen
				lda #0
				sta (ZPGetLinePtr),y
				clc
.9				rts	
*--------------------------------------
CS.RUN.printf	>PUSHYA
				>PUSHBI 0
				>SYSCALL PrintF
				rts
*--------------------------------------
CS.RUN.DumpFile	>PUSHYA
				>PUSHBI	O.RDONLY+O.TEXT
				>PUSHBI S.FI.T.TXT
				>PUSHWZ				Aux type
				>SYSCALL FOpen
				bcs .99

				stx ZPhFile		

				>LDYAI 256
				>SYSCALL GetMem
				bcs .9

				>STYA ZPBufPtr
				stx ZPhBuf

.1				>PUSHB ZPhFile
				jsr .7
				>PUSHWI	255
				>SYSCALL FGetS
				bcs .8

				jsr .7
				jsr .7
				>SYSCALL Expand

				>LDYA ZPBufPtr
				>SYSCALL PutS
				bra .1

.8 				lda ZPhBuf
				>SYSCALL FreeMem
				
*				clc

.9				php
				pha
				lda ZPhFile
				>SYSCALL FClose
				pla
				plp
.99				rts

.7				>PUSHW ZPBufPtr
				rts
*--------------------------------------
CS.DOEVENT		sec
				rts		
*--------------------------------------
CS.END
*--------------------------------------
MSG.LOGIN		.CZ "\r\nlogin:"
MSG.PASSWORD	.CZ "\r\npassword:"
*--------------------------------------
MSG.BAD			.AZ "Bad user or password"
MSG.BS			.DA #C.BS,#C.SPACE,#C.BS
MSG.CRLF		.HS 00
ETCNOAUTH		.AZ "${ROOT}etc/noauth"
ETCISSUE		.AZ "${ROOT}etc/issue"
ETCMOTD			.AZ "${ROOT}etc/motd"
SHELL			.AZ "${SHELL}"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START		
Password		.BS 33
Username		.BS 17
DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/sbin/login.s
ASM
