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

Remote Password Protection [With failure events, auto-shutdown, and easy password change]

Started by Robd, 03 April 2012 - 10:08 PM
Robd #1
Posted 04 April 2012 - 12:08 AM
This is a very simple client/server program which allows for the user to make a terminal and a server (which holds and distributes the password.) The code is fairly simple but there are some interesting features that most door locks wouldn't include.

Code:
SpoilerServer:

modem = "back"

rednet.open(modem)
term.clear()
term.setCursorPos(1,1)
print("Serving Password Requests...")
print("['q' to quit 'c' to change password]")
file = io.open("disk/password.txt","r")
password = file:read()
file:close()
answer = ""
while(true) do
	event,param1,param2 = os.pullEvent()
	if(event == "rednet_message" and param2 == "password") then rednet.send(param1,password)
	elseif(event == "char") then
if(param1 == "q") then break
elseif(param1 == "c") then
	 write("New Password: ")
	 while(true) do
  event,param1,param2 = os.pullEvent()
  if(event == "char") then
	  answer = answer..param1
	  write(param1)
  elseif(event == "key") then
	  if(param1 == 28) then -- ENTER
   break
	  elseif(param1 == 14) then -- Backspace
   x,y = term.getCursorPos()
   term.clearLine()
   term.setCursorPos(1,y)
   write("New Password: ")
   answer = ""
	  end
  end
	 end
	 file = io.open("disk/password.txt","w")
	 file:write(answer)
	 print("")
	 print("Password Changed to: "..answer)
	 password = answer
	 file:close()
	 answer = ""
end
	end
end

Client:

username = "Cookies19"
door = "left"
modem = "back"
os.startTimer(30) --go to sleep after 30 seconds (see event)
rednet.open(modem)
rednet.broadcast("password")
id,message = rednet.receive(5)
password = message
term.clear()
term.setCursorPos(1,1)
print("Terminal of Awesomeness")
print("***********************")
print("User Name: ")
write("Password: ")
term.setCursorPos(12,3)
textutils.slowPrint(username)
term.setCursorPos(11,4)
answer = ""
while(true) do
	event,param1,param2 = os.pullEventRaw()
	if(event == "terminate") then print("")print("")print("Now why on earth would I let you do that?") sleep(3) os.shutdown()
	elseif(event == "char") then
answer = answer..param1
write(param1)
	elseif(event == "key")
then
if(param1 == 28) then --ENTER
	 break
elseif(param1 == 14) then --BKSPACE
	 term.setCursorPos(1,4)
	 term.clearLine()
	 write("Password: ")
	 answer = ""
end
	elseif(event == "timer") then term.clear() term.setCursorPos(1,1) print("Going to sleep...") sleep(1) os.shutdown()
	end
end
print("")
print("")
if(answer == password) then print("Access Granted!") redstone.setOutput(door,true) sleep(3) os.shutdown()
else print("Access Denied!") sleep(1) rednet.broadcast("bye") os.shutdown() end

Installation:
SpoilerServer:
  1. Copy the above code into a file called "startup" and place it in a disk near the server
  2. Set the variable "modem" to the side with your wireless modem
  3. Create a file called "passwords.txt" on the same disk

Client:
  1. Copy the above code directly to the system and call the file "startup"
  2. Change the variables "username", "door", and "modem" to the apropriate states (door is a variable for the side the door is on)
Failure Event [Advanced]:
  1. Create some kind of redstone powered mechanism to be activated on an incorrect password entry
  2. Place a system near the contraption, to turn it on
  3. Create a code like this and call it "startup" (you may also choose to put it on a disk near the system):

modem = "back"
trap = "front"
rednet.open(modem)
while(true) do
	event,param1,param2 = os.pullEvent()
	if(event == "rednet_message" and param2 == "bye") then redstone.setOutput(trap,true) sleep(3) redstone.setOutput(trap,false) end
end
print("hehe")
Note that "bye" is the failure message broadcasted by terminals.

Features:
  • Username entry (this is mostly just a novilty as the system automatically enters the username)
  • Termination protection (Ctrl+T won't work)
  • Failure events
  • Server side password storage
  • Password change without rebooting the server
  • Simple installation (at least I believe it's simple)
  • The word "cookies" in the code (come on, you know you love it)
  • Reading my beautiful post
As always, feedback is welcome
Jojo.bacon #2
Posted 05 April 2012 - 01:31 PM
can you make it so it wont use a disk to store the password?
ArcaneShade #3
Posted 10 April 2012 - 08:46 AM
ok, so the usernames and passwords is stored on a 'server' (the server i s just a regular computer, right?) and then when you access another computer with the cilent code you will need to enter the right username/password combo?
so the password dosnt gets stored at the cilent it simply asks the server if the combo is correct?

did i get this right if so you have created the most awesome protection!
ArcaneShade #4
Posted 10 April 2012 - 01:50 PM
do you got any idea why the client comes up with this error message?
bios:206: [string "startup"]:29: 'end' expected
(to close 'if' at line 27)
djblocksaway #5
Posted 10 April 2012 - 02:37 PM
This is a very simple client/server program which allows for the user to make a terminal and a server (which holds and distributes the password.) The code is fairly simple but there are some interesting features that most door locks wouldn't include.

Code:
SpoilerServer:

modem = "back"

rednet.open(modem)
term.clear()
term.setCursorPos(1,1)
print("Serving Password Requests...")
print("['q' to quit 'c' to change password]")
file = io.open("disk/password.txt","r")
password = file:read()
file:close()
answer = ""
while(true) do
	event,param1,param2 = os.pullEvent()
	if(event == "rednet_message" and param2 == "password") then rednet.send(param1,password)
	elseif(event == "char") then
if(param1 == "q") then break
elseif(param1 == "c") then
	 write("New Password: ")
	 while(true) do
  event,param1,param2 = os.pullEvent()
  if(event == "char") then
	  answer = answer..param1
	  write(param1)
  elseif(event == "key") then
	  if(param1 == 28) then -- ENTER
   break
	  elseif(param1 == 14) then -- Backspace
   x,y = term.getCursorPos()
   term.clearLine()
   term.setCursorPos(1,y)
   write("New Password: ")
   answer = ""
	  end
  end
	 end
	 file = io.open("disk/password.txt","w")
	 file:write(answer)
	 print("")
	 print("Password Changed to: "..answer)
	 password = answer
	 file:close()
	 answer = ""
end
	end
end

Client:

username = "Cookies19"
door = "left"
modem = "back"
os.startTimer(30) --go to sleep after 30 seconds (see event)
rednet.open(modem)
rednet.broadcast("password")
id,message = rednet.receive(5)
password = message
term.clear()
term.setCursorPos(1,1)
print("Terminal of Awesomeness")
print("***********************")
print("User Name: ")
write("Password: ")
term.setCursorPos(12,3)
textutils.slowPrint(username)
term.setCursorPos(11,4)
answer = ""
while(true) do
	event,param1,param2 = os.pullEventRaw()
	if(event == "terminate") then print("")print("")print("Now why on earth would I let you do that?") sleep(3) os.shutdown()
	elseif(event == "char") then
answer = answer..param1
write(param1)
	elseif(event == "key")
then
if(param1 == 28) then --ENTER
	 break
elseif(param1 == 14) then --BKSPACE
	 term.setCursorPos(1,4)
	 term.clearLine()
	 write("Password: ")
	 answer = ""
end
	elseif(event == "timer") then term.clear() term.setCursorPos(1,1) print("Going to sleep...") sleep(1) os.shutdown()
	end
end
print("")
print("")
if(answer == password) then print("Access Granted!") redstone.setOutput(door,true) sleep(3) os.shutdown()
else print("Access Denied!") sleep(1) rednet.broadcast("bye") os.shutdown() end

Installation:
SpoilerServer:
  1. Copy the above code into a file called "startup" and place it in a disk near the server
  2. Set the variable "modem" to the side with your wireless modem
  3. Create a file called "passwords.txt" on the same disk

Client:
  1. Copy the above code directly to the system and call the file "startup"
  2. Change the variables "username", "door", and "modem" to the apropriate states (door is a variable for the side the door is on)
Failure Event [Advanced]:
  1. Create some kind of redstone powered mechanism to be activated on an incorrect password entry
  2. Place a system near the contraption, to turn it on
  3. Create a code like this and call it "startup" (you may also choose to put it on a disk near the system):

modem = "back"
trap = "front"
rednet.open(modem)
while(true) do
	event,param1,param2 = os.pullEvent()
	if(event == "rednet_message" and param2 == "bye") then redstone.setOutput(trap,true) sleep(3) redstone.setOutput(trap,false) end
end
print("hehe")
Note that "bye" is the failure message broadcasted by terminals.

Features:
  • Username entry (this is mostly just a novilty as the system automatically enters the username)
  • Termination protection (Ctrl+T won't work)
  • Failure events
  • Server side password storage
  • Password change without rebooting the server
  • Simple installation (at least I believe it's simple)
  • The word "cookies" in the code (come on, you know you love it)
  • Reading my beautiful post
As always, feedback is welcome
this is cool but you should add photos to your post :P/>/> (gives people an idea of what it is)
use imgshack or a free photo file hosting site
Robd #6
Posted 21 April 2012 - 04:33 AM
do you got any idea why the client comes up with this error message?
bios:206: [string "startup"]:29: 'end' expected
(to close 'if' at line 27)
I'll look into the code if I get a chance, but I've tested these thoroughly. My immediate advice would be to re-install that system's files.
can you make it so it wont use a disk to store the password?
Go into the server system's files and change everything that says "disk/password.txt" to "password.txt" (make sure you get them all)

ok, so the usernames and passwords is stored on a 'server' (the server i s just a regular computer, right?) and then when you access another computer with the cilent code you will need to enter the right username/password combo?
so the password dosnt gets stored at the cilent it simply asks the server if the combo is correct?

did i get this right if so you have created the most awesome protection!

The "Username" field is actually automatically entered with textutils.slowPrint(), it was only added to make the interface a little more… interesting. The server is a regular computer though the password is technically stored on a disk adjacent to the server system. It also doesn't really ask the server if it is correct, it asks the server what the password is, then asks the user what the password is, the calculation to determine if the attempt is correct is client sided.
Latusy #7
Posted 19 May 2012 - 05:11 AM
Every time I try to enter the password, it says "access denied".

On the server computer, it says rednet:337:string expected.
my_hat_stinks #8
Posted 20 May 2012 - 01:03 AM
So, if I'm right, you send the password to anyone who broadcasts "password"?

I've not used rednet at all yet, but… Well, isn't that very insecure?
Robd #9
Posted 01 June 2012 - 09:56 PM
So, if I'm right, you send the password to anyone who broadcasts "password"?

I've not used rednet at all yet, but… Well, isn't that very insecure?
Quite
warfighter67 #10
Posted 02 June 2012 - 05:19 AM
I'm not sure about your server code, but I've fixed your client code:
Spoiler

username = "Username"
door = "left"
modem = "top"
os.startTimer(30) --go to sleep after 30 seconds (see event)
rednet.open(modem)
rednet.broadcast("password")
id,message = rednet.receive(5)
password = ""
term.clear()
term.setCursorPos(1,1)
print("Security Terminal 007")
print("***********************")
print("User Name: ")
write("Password: ")
term.setCursorPos(12,3)
textutils.slowPrint(username)
term.setCursorPos(11,4)
answer = "passwordAnswer"
while(true) do
	    event,param1,param2 = os.pullEventRaw()
	    if(event == "terminate") then print("")print("")print("Now why on earth would I let you do that?") sleep(3) os.shutdown()
	    elseif(event == "char") then
password = password..param1
write(param1)
	    elseif(event == "key")
then
if(param1 == 28) then --ENTER
		 break
elseif(param1 == 14) then --BKSPACE
		 term.setCursorPos(1,4)
		 term.clearLine()
		 write("Password: ")
		 password = ""
end
	    elseif(event == "timer") then term.clear() term.setCursorPos(1,1) print("Going to sleep...") sleep(1) os.shutdown()
	    end
end
print("")
print("")
if(answer == password) then print("Access Granted!") redstone.setOutput(door,true) sleep(3) os.shutdown()
else print("Access Denied!) sleep(1) rednet.broadcast("bye") os.shutdown() end

The code you currently have doesn't work, or at least it didn't for me.