NEW
  AUTO 3,1
			.LIST OFF
			.OP	65C02
			.OR	$2000
			.TF BIN/USERADD
*--------------------------------------
USER.MAX	.EQ 16
PASS.MAX	.EQ 64
*--------------------------------------
			.INB INC/MACROS.I
			.INB INC/A2OSX.I
			.INB INC/LIBCRYPT.I
*--------------------------------------
* Zero Page Segment, up to 32 bytes
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPWPtr			.BS 2
ZPhPW			.BS 1
ZPStrPtr		.BS 2
ZPhStr			.BS 1
ZPbStrSecret	.BS 1
ZPStrLen		.BS 1
ZPStrMax		.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 #0					S.PS.F.EVENT
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data SegmentSize
				.DA #64					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.LIBCRYPT		.DA LIBCRYPT
L.MSG.USAGE		.DA MSG.USAGE
L.MSG.ROOTPWD1	.DA MSG.ROOTPWD1
L.MSG.ROOTPWD2	.DA MSG.ROOTPWD2
L.MSG.BS		.DA MSG.BS
				.DA 0
*--------------------------------------
* Called once at process creation
* Put code for loading LIB here
*--------------------------------------
CS.INIT			>LDYA L.LIBCRYPT
				>SYSCALL LoadLib
				bcs .9
				
				sta hLIBCRYPT

.9				rts
*--------------------------------------
* Called until exit with CS
* if RUN exits with CC, RN entered again
*--------------------------------------
CS.RUN			>LDYAI S.PW
				>SYSCALL getmem
				bcs .99
				>STYA ZPPWPtr
				stx ZPhPW
				
				>LDYAI 256
				>SYSCALL getmem
				bcs .99
				>STYA ZPStrPtr
				stx ZPhStr
				
				>PUSHW ZPPWPtr
				lda #0
				>SYSCALL GetPWUID
				bcc .1
				
				jmp CS.RUN.ROOT
				
.1				
				
.8				lda #0
				sec
				rts
				
.9				lda #E.SYN
				sec
.99				rts
*--------------------------------------
CS.RUN.ROOT		sec
				ror ZPbStrSecret
				lda #127
				sta ZPStrMax

				>PUSHBI 0
				>LDYA L.MSG.ROOTPWD1
				>SYSCALL printf
				bcs .9
				
				jsr CS.RUN.GetStr
				bcs .9

				>PUSHEA.G PW+S.PW.PASSWD
				>PUSHW ZPStrPtr
				>LIBCALL hLIBCRYPT,LIBCRYPT.MD5
				
				>LEA.G PW+S.PW.PASSWD
				>SYSCALL puts
				
				lda #0
				sec
				
.9				rts
*--------------------------------------
CS.RUN.GetStr	stz ZPStrLen


.1				>SYSCALL getchar
				bcs .9
				
				cmp #C.SPACE
				bcc .2
				
				ldy ZPStrLen
				cpy ZPStrMax
				beq .1
				
				sta (ZPStrPtr),y
				iny
				sty ZPStrLen
				
				bit ZPbStrSecret
				bmi .1
				
				>SYSCALL putchar
				bra .1

.2				cmp #3					Ctrl-C
				beq .9

				cmp #C.CR
				beq .8
				
				cmp #C.BS
				bne .1
				
				ldy ZPStrLen
				beq .1
				dec ZPStrLen
				
				bit ZPbStrSecret
				bmi .1
				
				>PUSHBI 0
				>LDYA L.MSG.BS
				>SYSCALL printf
				bra .1
				
.8				ldy ZPStrLen
				lda #0
				sta (ZPStrPtr),y
				
				clc
				
.9				rts	
*--------------------------------------
* Called if option S.PS.F.EVENT enabled in Header
* Timer Event : every 10th seconds
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
* Called once, when RUN exited with CS
* Put code for unloading LIB here
*--------------------------------------
CS.QUIT			lda ZPhStr
				beq .1
				
				>SYSCALL freemem
				
.1				lda ZPhPW
				beq .2
				
				>SYSCALL freemem
				
.2				lda hLIBCRYPT
				beq .8
				
				>SYSCALL UnloadLib
				
.8				clc
				rts
*--------------------------------------
CS.END
*--------------------------------------
* Initialized DATA
*--------------------------------------
MSG.USAGE		.AZ "Usage : USERADD username\r\n"
MSG.ROOTPWD1	.AZ "\r\nPlease enter ROOT password : "
MSG.ROOTPWD2	.AZ "\r\nPlease retype ROOT password : "
MSG.BS			.DA #C.BS,#C.SPACE,#C.BS,#0
*--------------------------------------
LIBCRYPT		.AZ "libcrypt"
hLIBCRYPT		.BS 1
*--------------------------------------
* Per Process DATA segment
*--------------------------------------
				.DUMMY
				.OR 0
DS.START		
PW				.DA S.PW
DS.END
				.ED
*--------------------------------------
MAN
SAVE USR/SRC/BIN/USERADD.S
ASM
