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

[1.31+] ShellBoot w/ Passwords

Started by ComputerCraftFan11, 06 May 2012 - 08:29 PM
ComputerCraftFan11 #1
Posted 06 May 2012 - 10:29 PM
This my modified shell program. When you run it, everything is normal… BUT, you get 2 new commands:


shell.setDiskBootPassword("password here")
--and--
shell.setBootPassword("password here")

First off, lets talk about setDiskBootPassword.
SpoilerIf i type this on my PC:

CraftOS 1.3
> lua
Interactive Lua prompt.
Call exit() to exit.
lua> shell.setDiskBootPassword("example")
lua> exit()
> reboot
This will happen:


CraftOS 1.3
>

But if i insert a disk, this will happen:


CraftOS 1.3
Password:
(it keeps rebooting until the disk is ejected or until i get the password correct)
Now, for setBootPassword.

SpoilerIf i type this on my PC:

CraftOS 1.3
> lua
Interactive Lua prompt.
Call exit() to exit.
lua> shell.setBootPassword("example")
lua> exit()
> reboot
This will happen:


CraftOS 1.3
Password:
It will say this until you enter "Example" or whatever you set the password as.
If you get this right, it will print "> " then you can type again.
Print it wrong, the computer reboots and asks for the password again

Files Created/Deleted:
  • File Name: .diskboot
  • File Use: Store the password for the disk.
  • Created by: os.setDiskBootPassword()
  • File Name: .diskboot
  • File Use: Store the password for the startup.
  • Created by: os.setBootPassword()
Download:

local parentShell = shell
local bExit = false
local sDir = (parentShell and parentShell.dir()) or ""
local sPath = (parentShell and parentShell.path()) or ".:/rom/programs"
local tAliases = (parentShell and parentShell.aliases()) or {}
local tProgramStack = {}
local shell = {}
local tEnv = {
["shell"] = shell,
}
-- Install shell API
function shell.setDiskBootPassword(password)
if fs.exists(".diskboot") then
  fs.delete(".diskboot")
end
file = fs.open(".diskboot", "w")
file.write(password)
file.close()
end
function shell.setBootPassword(password)
if fs.exists(".boot") then
  fs.delete(".boot")
end
file = fs.open(".boot", "w")
file.write(password)
file.close()
end
function shell.run( _sCommand, ... )
local sPath = shell.resolveProgram( _sCommand )
if sPath ~= nil then
  tProgramStack[#tProgramStack + 1] = sPath
	 local result = os.run( tEnv, sPath, ... )
  tProgramStack[#tProgramStack] = nil
  return result
    else
	 print( "No such program" )
	 return false
    end
end
function shell.exit()
    bExit = true
end
function shell.dir()
return sDir
end
function shell.setDir( _sDir )
sDir = _sDir
end
function shell.path()
return sPath
end
function shell.setPath( _sPath )
sPath = _sPath
end
function shell.resolve( _sPath )
local sStartChar = string.sub( _sPath, 1, 1 )
if sStartChar == "/" or sStartChar == "" then
  return fs.combine( "", _sPath )
else
  return fs.combine( sDir, _sPath )
end
end
function shell.resolveProgram( _sCommand )
-- Substitute aliases firsts
if tAliases[ _sCommand ] ~= nil then
  _sCommand = tAliases[ _sCommand ]
end
    -- If the path is a global path, use it directly
    local sStartChar = string.sub( _sCommand, 1, 1 )
    if sStartChar == "/" or sStartChar == "" then
	 local sPath = fs.combine( "", _sCommand )
	 if fs.exists( sPath ) and not fs.isDir( sPath ) then
   return sPath
	 end
  return nil
    end
   
  -- Otherwise, look on the path variable
    for sPath in string.gmatch(sPath, "[^:]+") do
	 sPath = fs.combine( shell.resolve( sPath ), _sCommand )
	 if fs.exists( sPath ) and not fs.isDir( sPath ) then
   return sPath
	 end
    end

-- Not found
return nil
end
function shell.programs( _bIncludeHidden )
local tItems = {}

-- Add programs from the path
    for sPath in string.gmatch(sPath, "[^:]+") do
	 sPath = shell.resolve( sPath )
  if fs.isDir( sPath ) then
   local tList = fs.list( sPath )
   for n,sFile in pairs( tList ) do
    if not fs.isDir( fs.combine( sPath, sFile ) ) and
	   (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
	 tItems[ sFile ] = true
    end
   end
  end
    end
-- Sort and return
local tItemList = {}
for sItem, b in pairs( tItems ) do
  table.insert( tItemList, sItem )
end
table.sort( tItemList )
return tItemList
end
function shell.getRunningProgram()
if #tProgramStack > 0 then
  return tProgramStack[#tProgramStack]
end
return nil
end
function shell.setAlias( _sCommand, _sProgram )
tAliases[ _sCommand ] = _sProgram
end
function shell.clearAlias( _sCommand )
tAliases[ _sCommand ] = nil
end
function shell.aliases()
-- Add aliases
local tCopy = {}
for sAlias, sCommand in pairs( tAliases ) do
  tCopy[sAlias] = sCommand
end
return tCopy
end

print( os.version() )
if fs.exists(".boot") then
file = fs.open(".boot", "r")
password = file.readAll()
file.close()
write("Password: ")
if password == read("*") then else
  os.reboot()
end
end
   
-- If this is the toplevel shell, run the startup programs
if parentShell == nil then
-- Run the startup from the ROM first
local sRomStartup = shell.resolveProgram( "/rom/startup" )
if sRomStartup then
  shell.run( sRomStartup )
end

-- Then run the user created startup, from the disks or the root
local sUserStartup = shell.resolveProgram( "/startup" )
for n,sSide in pairs( redstone.getSides() ) do
  if disk.isPresent( sSide ) and disk.hasData( sSide ) then
   local sDiskStartup = shell.resolveProgram( fs.combine(disk.getMountPath( sSide ), "startup") )
   if sDiskStartup then
    if fs.exists(".diskboot") then
	 file = fs.open(".diskboot", "r")
	 password = file.readAll()
	 file.close()
	 write("Password: ")
	 input = read("*")
    end
    if input == password then
	 sUserStartup = sDiskStartup
    else
	 os.reboot()
    end
    break
   end
  end
end

if sUserStartup then
  shell.run( sUserStartup )
end
end
-- Run any programs passed in as arguments
local tArgs = { ... }
if #tArgs > 0 then
shell.run( ... )
end
-- Read commands and execute them
local tCommandHistory = {}
while not bExit do
write( shell.dir() .. "> " )
local sLine = read( nil, tCommandHistory )
table.insert( tCommandHistory, sLine )

local tWords = {}
for match in string.gmatch(sLine, "[^ t]+") do
  table.insert( tWords, match )
end
local sCommand = tWords[1]
if sCommand then
  shell.run( sCommand, unpack( tWords, 2 ) )
end
end
-- If this is the toplevel shell, run the shutdown program
if parentShell == nil then
if shell.resolveProgram( "shutdown" ) then
  shell.run( "shutdown" )
end
os.shutdown() -- just in case
end
Mazi30 #2
Posted 07 May 2012 - 09:53 PM
Now this is what I was looking for :D Thanks a lot!
PoLoMoTo #3
Posted 08 May 2012 - 11:50 PM
Nice work but couldn't you just modify the startup program to ask for the password similarly to a password door?
ComputerCraftFan11 #4
Posted 09 May 2012 - 02:45 AM
Nice work but couldn't you just modify the startup program to ask for the password similarly to a password door?

Yes, but if you insert a disk, that won't work
PoLoMoTo #5
Posted 09 May 2012 - 09:28 PM
Nice work but couldn't you just modify the startup program to ask for the password similarly to a password door?

Yes, but if you insert a disk, that won't work

True but I can't think of a use for booting from the floppy except installing an OS, but idk I'm new to this.
MysticT #6
Posted 09 May 2012 - 09:38 PM
Nice work but couldn't you just modify the startup program to ask for the password similarly to a password door?

Yes, but if you insert a disk, that won't work

True but I can't think of a use for booting from the floppy except installing an OS, but idk I'm new to this.
I think this is for servers, where there's hackers/griefers that use disk drives with floppies to break password locks and such to get access to the computer.
PoLoMoTo #7
Posted 09 May 2012 - 10:36 PM
Nice work but couldn't you just modify the startup program to ask for the password similarly to a password door?

Yes, but if you insert a disk, that won't work

True but I can't think of a use for booting from the floppy except installing an OS, but idk I'm new to this.
I think this is for servers, where there's hackers/griefers that use disk drives with floppies to break password locks and such to get access to the computer.

Oh, does the computer auto boot to the floppy?
Dirkus7 #8
Posted 14 May 2012 - 08:16 PM
Nice work but couldn't you just modify the startup program to ask for the password similarly to a password door?

Yes, but if you insert a disk, that won't work

True but I can't think of a use for booting from the floppy except installing an OS, but idk I'm new to this.
I think this is for servers, where there's hackers/griefers that use disk drives with floppies to break password locks and such to get access to the computer.

Oh, does the computer auto boot to the floppy?
Yes, if a disk is in a drive, it will boot the disk (if startup programs exists on the disk), else it will boot the computer's startup program
RockLegend2 #9
Posted 28 May 2012 - 07:03 AM
Thank you!

I'm not using all of this code for anything, but the "shell.run" portion of code you have in there helped me solve a problem. So… thanks. :)/>/>