This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Jahmaican's profile picture

Access cards - complex solution

Started by Jahmaican, 26 August 2012 - 01:41 AM
Jahmaican #1
Posted 26 August 2012 - 03:41 AM
Hi, this is my first attempt on CC programming. I know there's a lot of similiar programs but this one is quite complex and (I hope) extremely secure. Plus I made its huge part out of pure boredom when my internet connection went down, so the only api reference I had was ingame.

It was improved since the initial release (here as the official CC forums were down at the time).

You will need
  • a computer :D/>/>
  • disk drive
  • double monitor (optional but recommended)
  • redstone output (default: bottom)
  • floppy disk
First let's take a look at floppy disk - this is our access card. The only thing we need here is a little ID file:
username (only for welcoming message)
password (the same for every user, different between single and permanent mode)

eg. my permanent card
Jahmaican
dupa.8
eg. guest card (one use only)
guest
admin1

Okay, we've got a card, now we need a terminal:

(please ignore it's a wooden door :P/>/>)

As of the version 1.1 the program uses a little api that you have to save as api/devices if you want to use the code without any changes:

function find(name,prior)
  local sides = {
  "top";
  "front";
  "left";
  "right";
  "back";
  "bottom" }

  if prior==nil then
	p=1
  else
	p=prior
  end

  for n=1,6 do
	if peripheral.getType(sides[n])==name then
	  if p==1 then
		return sides[n]
	  else
		p=p-1
	  end
	end
  end
  return "none"
end
(also, it's very useful!)


Create a startup file on the computer and use this code:
-- CONFIG for your convenience :P/>/> --

rside="bottom"					-- redstone output side
itime=2							-- "access denied" message timeout
vtime=3							-- "access granted" message timeout
idfile="/disk/id"				-- ID filename
vpass="dupa.8"					-- permanent access password
spass="admin1"					-- single access password
trm=true						-- allow the program to be terminated with Ctrl-T
								-- please read further thoughts in "Notes" section of this post
-- END OF CONFIG --

os.loadAPI("api/devices")

dside=devices.find("drive")

function Main()
	print("Security access v1.1nby Jahmaicannrunning...")
	Monit()
end

function Ismon()							-- allows to destroy or connect the monitor
	local mside=devices.find("monitor")	  -- at any time without crashing the program
	if mside~="none" then
		mon=peripheral.wrap(mside)
		ismon=true
	else
		ismon=false
	end

	return ismon
end

function Monit()
	rs.setOutput(rside,false)

	if Ismon()==true then
		mon.clear()
		mon.setCursorPos(2,2)
		mon.write("Door locked")
		mon.setCursorPos(2,4)
		mon.write("Insert card")
	end
	
	while event~="disk" do
		event=os.pullEvent()
	end
	event=nil
	
	Check()
end

function Check()  
	if Ismon()==true then
		mon.clear()
	end

	if fs.exists(idfile)==false then
		Invalid()
	end

	plik=assert(fs.open(idfile,"r"))
	name=plik.readLine()
	pass=plik.readLine()
	plik.close()

	if pass==vpass then
		Valid()
		single=false
	elseif pass==spass then
		Valid()
		single=true
	else
		Invalid()
	end
end

function Invalid()
	if Ismon()==true then
		mon.clear()
		mon.setCursorPos(2,2)
		mon.write("Access denied")
		mon.setCursorPos(2,4)
		mon.write("Invalid card")
	end

	disk.eject(dside)  
	sleep(itime)
	Monit()
end

function Valid()
	if Ismon()==true then
		mon.clear()
		mon.setCursorPos(2,2)
		mon.write("Access granted")
		mon.setCursorPos(2,4)
		mon.write("Hi "..name.."!")
	end

	if single=="true" then				-- erases the single access card
		if fs.isReadOnly(idfile)==false then  
			fs.delete(idfile)  
			disk.setLabel(dside,"Expired_card")
		end
	end

	disk.eject(dside)  
	rs.setOutput(rside,true)
	sleep(vtime)
	Monit()
end

function os.pullEvent()
	local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
	if event == "terminate" then
		if trm==true then
			print("Terminated")
			error()
		else
			print("Look out, we got a badass over here!")
		end
	end
	return event, p1, p2, p3, p4, p5
end

Main()

To be able to activate "guest cards" quickly I also created a little program, but notice it was made rather for testing purposes and is highly insecure (until you run it on password protected terminal or something alike).

This is the code:
-- CONFIG --

vpass="haslo"			-- card admin access password
orfile="/files/id"		-- original ID filename
idfile="/disk/id"		-- where to copy original file

-- END OF CONFIG --

os.loadAPI("api/devices")
dside=devices.find("drive")

function Main()
	print("Guest card validator v1.0nby Jahmaicann")
	print("Password needed!")		-- obviously password protection here is quite pointless
	pass=read("*")   					   -- apart from looking neat :)/>/>
	if pass==vpass then
		Correct()
	else
		Incorrect()
	end
end

function Correct()
	print("Password correct!")
	print("Validating guest card...")
	if disk.isPresent(dside)==true then
		if fs.exists(idfile) then
			if fs.isReadOnly(idfile)==false then
				fs.delete(idfile)
			else
				print("[ID file is read only - card may not work]")
			end
		end
		fs.copy(orfile,idfile)
		disk.setLabel(dside,"Single_access")
		disk.eject(dside)
		print("All set!")
	else
		print("Disk error!")
	end
end

function Incorrect()
	print("Incorrect password!")
end

Main()

And /files/id content:
guest
admin1

Notes
  • Single-access cards shouldn't be trusted too much as they are very easy to duplicate
  • The whole system is pointless if other players have permissions to break any blocks around :)/>/>
  • I didn't test what will happen when you insert a disk with a "startup" file and reboot the computer with Ctrl-S or Ctrl-R, though these shortcuts are easy to disable the same way I did with Ctrl-T
  • As I recently joined a nice tekkit server plus it somewhat interferes with my educational path, I might expand this one into a complete API and a set of programs to suit all your CC security needs!
Finally, I hope you'll like it. Please let me know if you find any bugs - I'll try to fix them.
Kingdaro #2
Posted 26 August 2012 - 04:05 AM
this is a really good idea. I applaud you.
furrysalamander #3
Posted 28 August 2012 - 01:29 AM
You should get more credit, this is good.
FuzzyPurp #4
Posted 28 August 2012 - 02:19 AM
Nice work. Although, Ctrl+S and Ctrl+R are can not be easily disabled such as Ctrl+T
A startup would bypass this, but the terminal would have to be reset, make the terminal inaccessible
R3TRI8UTI0N #5
Posted 02 September 2012 - 11:28 PM
I have checked over the code 3 times, and all I get after rebooting is 'startup:4: attempt to call nil'
I really want to use this system as it seems awesome, but can't get past this issue. Any help appreciated.


EDIT > Apparently not well enough, I found some issues and it allowed it load until line 104, which is disk.eject(dside), and stops there (though it does eject my disk). I will keep looking for any mistakes, as I know it is my fault lol.

EDIT2 > Now it is saying line 105, which is rs.setOutput(rside,true). I don't understand what the problem is with this one…
Jahmaican #6
Posted 03 September 2012 - 01:31 AM
First thing that comes to my mind is that it could be an API issue. Please make sure you have the needed API saved as devices in folder api.

However, error ocurring in line 4 is still very strange, as this line is just declaring a variable - as long as you didn't skip the config section (and you shouldn't do it).

I tested this exact code right now and it works without any issues with peripherals connected in any possible configuration :D/>/>
R3TRI8UTI0N #7
Posted 03 September 2012 - 02:46 AM
Nope I solved line 4, it was a very stupid mistake I made by forgetting the =. I hadn't originally looked over the lines in the config because I figured I wouldn't have made a mistake there (lol).I have the API saved in the folder api, I also looked over that as well. I will look again once more and see if there's anything wrong.

edit > Oh yeah btw, it's not an error during startup, it's just an error when I put in a correct floppy disk. It grants access but doesn't activate the redstone and freezes at line 105. I checked the API file, it's basically a carbon copy of what you have on here. I'll look over the main coding but I think I've fixed all issues. Is the coding for the guest cards required? If so, I haven't done that.
cheekycharlie101 #8
Posted 08 September 2012 - 11:45 AM
I have checked over the code 3 times, and all I get after rebooting is 'startup:4: attempt to call nil'
I really want to use this system as it seems awesome, but can't get past this issue. Any help appreciated.


EDIT > Apparently not well enough, I found some issues and it allowed it load until line 104, which is disk.eject(dside), and stops there (though it does eject my disk). I will keep looking for any mistakes, as I know it is my fault lol.

EDIT2 > Now it is saying line 105, which is rs.setOutput(rside,true). I don't understand what the problem is with this one…

try this: rs.setOuput("right", true) that should work <–
123445kk #9
Posted 13 April 2014 - 06:00 PM
I know this is an old post, but this is one of the best keycard systems that i can find. So im asking if its possible to rewrite it to enable wired (or wireless) modems? If not could someone refer me to annother good program (that features monitors and welcoming messages + wired modems etc)?
Lyqyd #10
Posted 13 April 2014 - 09:27 PM
Enable how? What would it do with a modem? It's a pretty self-contained system that only needs a computer, two monitor blocks and a disk drive to work.
123445kk #11
Posted 14 April 2014 - 02:55 PM
You could then connect it to a monitor that is far (or atleast not touching it) from the actual computer.