NEW
  AUTO 3,1
*--------------------------------------
* ZPPtr1 : UID
* TXTPTR : pName (Input)
* FORPNT : pPW (Output)
* ZPPtr3 : pDB
* USERNAME:PASSWORD:UID:GID:GECOS:HOME:SHELL\CR
* ....\CR
* \0
*--------------------------------------
PWDX.GetPWUID	sta ZPPtr1+1

				lda PWD.hUsrDB
				beq PWDX.GetPW.9

				jsr K.GetmemPtr
				>STYA ZPPtr3

.1				lda (ZPPtr3)
				beq PWDX.GetPW.99		EOF

				ldy #0
				jsr PWDX.NextS		skip USERNAME

				jsr PWDX.NextS		skip PASSWORD

				jsr PWDX.GetByte		get UID
				cmp ZPPtr1+1
				beq PWDX.GetPW.8

				jsr PWDX.NextL
				bra .1

PWDX.GetPW.8	ldx FORPNT+1
				beq  .8					Dry run, no output

				jmp PWDX.Rec2PW

.8				clc						a = UID
				rts

PWDX.GetPW.9	lda #E.IPWDDB
				.HS 2C					BIT ABS

PWDX.GetPW.99	lda #E.IUSR
				sec
				rts
*--------------------------------------
PWDX.GetPWName	lda PWD.hUsrDB
				beq PWDX.GetPW.9

				jsr K.GetmemPtr
				>STYA ZPPtr3

				ldy #0
				jsr SHARED.TXTPTRgY
				beq PWDX.GetPW.99

.1				lda (ZPPtr3)
				beq PWDX.GetPW.99

				ldy #$ff
.2				iny
				jsr SHARED.TXTPTRgY
				beq .4

				cmp (ZPPtr3),y
				beq .2

.3				jsr PWDX.NextL
				bra .1

.4				lda (ZPPtr3),y
				cmp #':'
				bne .3

				ldx FORPNT+1
				beq .8

				jmp PWDX.Rec2PW

.8				iny
				jsr PWDX.NextS		skip PASSWORD
				jmp PWDX.GetByte		get UID
*--------------------------------------
* TXTPTR : pPW (Input)
* ZPPtr3 : pDB
*--------------------------------------
PWDX.PutPW		lda PWD.hUsrDB
				bne PWDX.PutPW.1

				ldy #S.PW.UID
				jsr SHARED.TXTPTRgY
				bne PWDX.GetPW.99		Must be ROOT user

				jsr PWDX.GetRecLen
				tay
				lda #0

				>STYA PWD.UsrDBSize

				iny
				bne .1
				
				inc
				
.1				jsr K.getmem
				bcs .9

				>STYA ZPPtr3
				stx PWD.hUsrDB

				lda #0
				sta (ZPPtr3)

				jsr PWDX.AppRec

				lda PWD.hGrpDB
				beq .2

				jsr K.freemem

.2				>LDYAI PWDX.GROUP.Size+1
				>STYA PWD.GrpDBSize
				jsr K.getmem
				bcs .9

				>STYA ZPPtr3
				stx PWD.hGrpDB

				ldy #PWDX.GROUP.Size-1

.3				lda PWDX.GROUP,y
				sta (ZPPtr3),y
				dey
				bpl .3

				clc
.9				rts
*--------------------------------------
PWDX.PutPW.1	jsr K.GetmemPtr
				>STYA ZPPtr3

				ldy #S.PW.PASSWD
				jsr SHARED.TXTPTRgY
				bne .12

				jsr PWDX.FindRec
				bcs .99
				jmp PWDX.DelRec
*--------------------------------------
.12				jsr PWDX.FindRec
				bcs .11

				jsr PWDX.DelRec
				bcs .99
				bra .13
*--------------------------------------
.11				jsr PWDX.GetUID			new user, get UID
				bcs .99

				ldy #S.PW.UID
				sta CLRWRITEAUX
				sta (TXTPTR),y
				sta SETWRITEAUX

.13				jsr PWDX.GetRecLen

				clc
				adc PWD.UsrDBSize
				sta PWD.UsrDBSize
				tay

				lda #0
				adc PWD.UsrDBSize+1
				sta PWD.UsrDBSize+1

				iny
				bne .14
				
				inc
				
.14				jsr K.Getmem
				bcs .99

				>STYA ZPPtr3

				lda PWD.hUsrDB
				jsr K.GetMemPtr
				>STYA ZPPtr4

				ldy #0

.3				lda (ZPPtr4),y
				sta (ZPPtr3),y
				beq .4

				iny
				bne .3
				inc ZPPtr4+1
				inc ZPPtr3+1
				bra .3

.4				lda PWD.hUsrDB
				stx PWD.hUsrDB
				jsr K.Freemem

				lda PWD.hUsrDB
				jsr K.GetMemPtr
				>STYA ZPPtr3

				jsr PWDX.AppRec

				lda #0
				jsr PWDX.StoreC

				clc
				rts

.9				lda #E.IPWDDB
				sec
.99				rts
*--------------------------------------
PWDX.GetGRGID	sta ZPPtr1+1

				lda PWD.hGrpDB
				beq PWDX.GetGR.9

				jsr K.GetmemPtr
				>STYA ZPPtr3

.1				lda (ZPPtr3)
				beq PWDX.GetGR.99		EOF

				ldy #0
				jsr PWDX.NextS			skip USERNAME

				jsr PWDX.GetByte		get UID
				cmp ZPPtr1+1
				beq PWDX.GetGR.8

				jsr PWDX.NextL
				bra .1

PWDX.GetGR.8	ldx FORPNT+1
				beq  .8					Dry run, no output

				jmp PWDX.Rec2GR

.8				clc						a = UID
				rts

PWDX.GetGR.9	lda #E.IPWDDB
				.HS 2C					BIT ABS

PWDX.GetGR.99	lda #E.IUSR
				sec
				rts
*--------------------------------------
PWDX.GetGRName	lda PWD.hGrpDB
				beq PWDX.GetGR.9

				jsr K.GetmemPtr
				>STYA ZPPtr3

				ldy #0
				jsr SHARED.TXTPTRgY
				beq PWDX.GetGR.99

.1				lda (ZPPtr3)
				beq PWDX.GetGR.99

				ldy #$ff
.2				iny
				jsr SHARED.TXTPTRgY
				beq .4

				cmp (ZPPtr3),y
				beq .2

.3				jsr PWDX.NextL
				bra .1

.4				lda (ZPPtr3),y
				cmp #':'
				bne .3

				ldx FORPNT+1
				beq .8

				jmp PWDX.Rec2GR

.8				iny
				jmp PWDX.GetByte		get GID
*--------------------------------------
PWDX.PutGR		lda PWD.hGrpDB
				beq PWDX.GetGR.9

				jsr K.GetmemPtr
				>STYA ZPPtr3


				lda #E.IPWDDB
				sec
				rts
*--------------------------------------
PWDX.OpenSession
				lda PWD.hUsrDB
				beq .1

				lda TXTPTR+1
				bne .3

.1				>LDYAI PWDX.ROOT.Size
				jsr K.GetMem
				bcs .9

				>STYA ZPPtr1

				ldy #PWDX.ROOT.Size-1

.2				lda PWDX.ROOT,y
				sta (ZPPtr1),y
				dey
				bpl .2

				txa
				clc
				rts
*--------------------------------------
.3				jsr PWDX.FindRec
				bcs .9

				tya						Y = Name length +1

*				clc
				adc ZPPtr3
				sta ZPPtr4

				lda ZPPtr3+1
				adc #0
				sta ZPPtr4+1

				ldy #31

				sec

.5				jsr SHARED.FORPNTgY
				eor (ZPPtr4),y			Check MD5
				bne .9

				dey
				bpl .5

				jsr PWDX.GetSessionLen

				jsr K.Getmem
				bcs .9

				>STYA ZPPtr2
				phx

				jsr PWDX.Record2Session

				pla
				clc
.9				rts
*--------------------------------------
PWDX.CloseSession
				jmp K.Freemem
*--------------------------------------
* PRIVATE
*--------------------------------------
PWDX.GetUID		lda PWD.hUsrDB

				jsr K.GetmemPtr
				>STYA ZPPtr3

				stz .8+1
.1				lda (ZPPtr3)
				beq .8					End of DB

				ldy #0
				jsr	PWDX.NextS		skip name
				jsr PWDX.NextS		skip password
				jsr PWDX.GetByte
				cmp .8+1
				bcc .4

				sta .8+1

.4				jsr PWDX.NextL
				bra .1

.8				lda #$ff				SELF MODIFIED
				inc
				beq .9

				clc
				rts

.9 				lda #E.IUSR
				sec
				rts
*--------------------------------------
PWDX.DelRec
.1				iny
				lda (ZPPtr3),y
				cmp #C.CR
				bne .1

				sty .3+1
				tya
				sec
				adc ZPPtr3
				sta ZPPtr4
				lda ZPPtr3+1
				adc #0
				sta ZPPtr4+1

				ldy #$ff

.2				iny
				lda (ZPPtr4),y
				sta (ZPPtr3),y
				bne .2

				lda PWD.UsrDBSize
				sec
.3				sbc #$ff				SELF MODIFIED
				sta PWD.UsrDBSize
				bcs .8

				dec PWD.UsrDBSize+1

.8				clc

.9				rts
*--------------------------------------
PWDX.FindRec	lda PWD.hUsrDB
				jsr K.GetmemPtr

				>STYA ZPPtr3

.2				lda (ZPPtr3)
				beq .9					End of DB

				ldy #$ff

.3				iny
				jsr SHARED.TXTPTRgY
				beq .4

				cmp (ZPPtr3),y
				beq .3

.5				jsr PWDX.NextL
				bra .2

.4				lda (ZPPtr3),y
				cmp #':'
				bne .5

				iny						skip ":"

				clc
				rts

.9				lda #E.IUSR
				sec
				rts
*--------------------------------------
* Record : USERNAME:PASSWORD:UID:GID:GECOS:HOME:SHELL\CR
*--------------------------------------
PWDX.Rec2PW		ldy #0

				ldx #0					NAME

.1				lda (ZPPtr3),y
				iny
				cmp #':'
				beq .2

				jsr SHARED.FORPNTpn
				inx
				bra .1

.2				lda #0
				jsr SHARED.FORPNTpn
				inx
				cpx #S.PW.PASSWD
				bne .2

.3				lda (ZPPtr3),y
				iny
				cmp #':'
				beq .4

				jsr SHARED.FORPNTpn
				inx
				bra .3

.4				lda #0
				jsr SHARED.FORPNTpn
				inx
				cpx #S.PW.UID
				bne .4

				jsr PWDX.GetByte		UID
				jsr SHARED.FORPNTpn

				iny

				jsr PWDX.GetByte		GID
				jsr SHARED.FORPNTpn

				iny

				ldx #S.PW.GECOS

.5				lda (ZPPtr3),y
				iny
				cmp #':'
				beq .6
				jsr SHARED.FORPNTpn
				inx
				bra .5
.6				lda #0
				jsr SHARED.FORPNTpn
				inx
				cpx #S.PW.DIR
				bne .6

.7				lda (ZPPtr3),y
				iny
				cmp #':'
				beq .8
				jsr SHARED.FORPNTpn
				inx
				bra .7
.8				lda #0
				jsr SHARED.FORPNTpn
				inx
				cpx #S.PW.SHELL
				bne .8

.9				lda (ZPPtr3),y
				iny
				cmp #C.CR
				beq .10
				jsr SHARED.FORPNTpn
				inx
				bra .9
.10				lda #0
				jsr SHARED.FORPNTpn

				clc
				rts
*--------------------------------------
PWDX.Rec2GR		ldy #0

				ldx #0					NAME

.1				lda (ZPPtr3),y
				iny
				cmp #':'
				beq .2

				jsr SHARED.FORPNTpn
				inx
				bra .1

.2				lda #0
				jsr SHARED.FORPNTpn

				jsr PWDX.GetByte		UID
				jsr SHARED.FORPNTpn

				clc
				rts
*--------------------------------------
* Record : USERNAME:PASSWORD:UID:GID:GECOS:HOME:SHELL\CR
*--------------------------------------
PWDX.GetSessionLen
				ldy #0

				jsr PWDX.NextS			USERNAME
				tya
				clc
				adc #S.SESSION.NAME
				tax
				
				jsr PWDX.NextS			skip PASSWORD
				jsr PWDX.NextS			skip UID
				jsr PWDX.NextS			skip GID
				
.1				inx
				iny
				lda (ZPPtr3),y
				beq .8
				
				eor #C.CR
				bne .1
				
.8				phx						A = 0
				ply
				rts
*--------------------------------------
PWDX.Record2Session
				ldy #0

				jsr PWDX.NextS			skip USERNAME
				jsr PWDX.NextS			skip PASSWORD
				jsr PWDX.GetByte		get UID
				sta ZPPtr1

				iny

				jsr PWDX.GetByte		get GID
				sta ZPPtr1+1

				iny

				lda #0
				ldx ZPPtr1
				bne .1

				ora #S.SESSION.P.ROOT

.1				ldx ZPPtr1+1
				bne .2

				ora #S.SESSION.P.SYSADMIN

.2				jsr SHARED.PutCP2

				lda ZPPtr1
				jsr SHARED.PutCP2	UID

				lda ZPPtr1+1
				jsr SHARED.PutCP2	GID

				phy

				ldy #$ff

.3				iny
				lda (ZPPtr3),y
				cmp #':'
				beq .4

				jsr SHARED.PutCP2
				bra .3

.4				lda #0
				jsr SHARED.PutCP2

				ply

.5				lda (ZPPtr3),y
				cmp #C.CR
				beq .8

				cmp #':'
				bne .6

				lda #0

.6				jsr SHARED.PutCP2
				iny
				bra .5

.8				lda #0
				jmp SHARED.PutCP2
*--------------------------------------
PWDX.NextS		lda (ZPPtr3),y
				beq .8

				iny
				cmp #':'
				bne PWDX.NextS
.8				rts
*--------------------------------------
PWDX.NextL		lda (ZPPtr3),y
				beq .8

				iny
				cmp #C.CR
				bne PWDX.NextL

				tya
				clc
				adc ZPPtr3
				sta ZPPtr3
				bcc .8
				inc ZPPtr3+1
.8				rts
*--------------------------------------
PWDX.GetByte	stz .8+1

.1				lda (ZPPtr3),y
				beq .8

				cmp #C.CR
				beq .8

				cmp #':'
				beq .8

				and #$f
				pha
				lda .8+1
				asl
				asl
				clc
				adc .8+1
				asl
				sta .8+1
				pla
				clc
				adc .8+1
				sta .8+1

				iny

				bra .1

.8				lda #$ff				SELF MODIFIED

				clc
				rts
*--------------------------------------
PWDX.AppRec
.1				lda (ZPPtr3)
				beq .8

				inc ZPPtr3
				bne .1
				inc ZPPtr3+1
				bra .1

.8				sec
				.HS 90					BCC
*--------------------------------------
PWDX.GetRecLen	clc
				ldx #0
				ldy #S.PW.NAME

				jsr PWDX.StoreS
				lda #':'
				jsr PWDX.StoreC
				ldy #S.PW.PASSWD

				jsr PWDX.StoreS

				lda #':'
				jsr PWDX.StoreC

				ldy #S.PW.UID
				jsr SHARED.TXTPTRgY
				jsr PWDX.StoreB

				lda #':'
				jsr PWDX.StoreC

				ldy #S.PW.GID
				jsr SHARED.TXTPTRgY
				jsr PWDX.StoreB
				lda #':'
				jsr PWDX.StoreC

				ldy #S.PW.GECOS

				jsr PWDX.StoreS
				lda #':'
				jsr PWDX.StoreC

				ldy #S.PW.DIR

				jsr PWDX.StoreS
				lda #':'
				jsr PWDX.StoreC

				ldy #S.PW.SHELL

				jsr PWDX.StoreS
				lda #C.CR
				jsr PWDX.StoreC

				txa
PWDX.RTS		rts
*--------------------------------------
PWDX.StoreS		jsr SHARED.TXTPTRgY
				beq PWDX.RTS
				jsr PWDX.StoreC
				iny
				bra PWDX.StoreS
*--------------------------------------
PWDX.StoreB		php
				phx
				jsr MATH.A2STR10NP
				plx
				plp

				ldy #0

.1				lda FOUTBuf,y
				beq PWDX.RTS
				iny
				jsr PWDX.StoreC
				bra .1
*--------------------------------------
PWDX.StoreC		inx
				bcc .9					Dry run, no store
				sta (ZPPtr3)
				inc ZPPtr3
				bne .9
				inc ZPPtr3+1
.9				rts
*--------------------------------------
PWDX.ROOT		.DA #S.SESSION.P.ROOT
				.DA #0					UID
				.DA #0					GID
				.AZ "root"				NAME
				.AZ "Root User"			GECOS
				.AZ "${ROOT}root/"		DIR
				.AZ "${ROOT}bin/sh"		SHELL
PWDX.ROOT.Size	.EQ	*-PWDX.ROOT
*--------------------------------------
PWDX.GROUP		.AS "root:0"
				.DA #C.CR
				.AS "users:1"
				.DA #C.CR
PWDX.GROUP.Size	.EQ *-PWDX.GROUP
*--------------------------------------
MAN
SAVE usr/src/sys/kernel.s.pwdx
LOAD usr/src/sys/kernel.s
ASM
