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 #32					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.NOAUTH	.DA MSG.NOAUTH
L.MSG.LOGIN		.DA MSG.LOGIN
L.MSG.PASSWORD	.DA MSG.PASSWORD
L.MSG.BAD		.DA MSG.BAD
L.MSG.CRLF		.DA MSG.CRLF
L.MSG.BS		.DA MSG.BS
L.ETCISSUE		.DA ETCISSUE
L.ETCMOTD		.DA ETCMOTD
L.SHELL			.DA SHELL
				.DA 0
*--------------------------------------
CS.INIT			clc
CS.INIT.RTS		rts
*--------------------------------------
CS.RUN			>LDYA L.ETCISSUE
				jsr CS.RUN.DumpFile
				bcs CS.INIT.RTS

				lda #0
				>PUSHA					ROOT user
				>PUSHA
				>PUSHA					Dry Run, no output
				>SYSCALL GetPWUID
				bcc CS.RUN.AUTH			
*--------------------------------------
				>LDYA L.MSG.NOAUTH
				jsr CS.RUN.StrOut
				bcs CS.INIT.RTS
*--------------------------------------
CS.RUN.EXECPS	>LDYA L.ETCMOTD
				jsr CS.RUN.DumpFile
				jsr CS.CLEANUP

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

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

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

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

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

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

				>LDYA L.MSG.CRLF
				jsr CS.RUN.StrOut
				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 S.Table.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 S.Table.hSID-1,x

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

				jsr CS.RUN.EXECPS
				
				lda ZPNewSID
				
				>SYSCALL CloseSession
				
				stz ZPNewSID
				
.8				ldx ZPNewSession
				stz S.Table.hSID-1,x
				stz S.Table.hFile-1,x
				
				ldy #S.PS.hSession
				lda ZPOldSession
				sta (pPS),y
				
				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.StrOut
				bra .1

.8				ldy ZPGetLineLen
				lda #0
				sta (ZPGetLinePtr),y
				clc
.9				rts	
*--------------------------------------
CS.RUN.StrOut	>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 .9

				stx ZPhFile		
				>LDYAI 256
				>SYSCALL GetMem
				bcs .99

				>STYA ZPBufPtr
				stx ZPhBuf

.1				>PUSHB ZPhFile
				>PUSHW ZPBufPtr
				>PUSHWI	256
				>SYSCALL FGetS
				bcs .7

				>PUSHW ZPBufPtr
				>PUSHWI 0
				>SYSCALL Expand
				bcc .2

.99				php
				pha
				jsr .8
				pla
				plp
.9				rts

.2				phx
				>SYSCALL PutS
				pla
				>SYSCALL FreeMem
				bra .1

.7 				lda ZPhBuf
				>SYSCALL FreeMem

.8				lda ZPhFile
				>SYSCALL FClose
				rts
*--------------------------------------
CS.DOEVENT		
				sec
				rts		
*--------------------------------------
CS.QUIT			
CS.CLEANUP		
.8				clc
				rts
*--------------------------------------
CS.END
*--------------------------------------
MSG.NOAUTH		.AS "\r\n\r\nA2osX-Login:No ETC/PASSWD file present, Logged as ROOT.\r\n"
				.AS "Consider adding ROOT password with USERADD command.\r\n"
MSG.CRLF		.AZ "\r\n"
MSG.LOGIN		.AZ "\r\nlogin:"
MSG.PASSWORD	.AZ "\r\npassword:"
MSG.BAD			.AZ "Bad user or password"
MSG.BS			.DA #C.BS,#C.SPACE,#C.BS,#0
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
