Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local oldOSPullEvent = os.pullEvent
- local oldOSPullEventRaw = os.pullEventRaw
- local protectedFolders = {}
- local version = "MuSeOS v8.1"
- local debugMode = false
- os.pullEvent = oldOSPullEventRaw
- local currentUser = "root"
- local logo = function(startX, startY)
- term.clear()
- term.setCursorPos(startX,startY)
- term.write(" #### #### ###### ##### ######")
- term.setCursorPos(startX,startY+1)
- term.write("# # # # # # # ")
- term.setCursorPos(startX,startY+2)
- term.write("# # # # # # #### # # # ")
- term.setCursorPos(startX,startY+3)
- term.write("# # # # # ###### # # # # ######")
- term.setCursorPos(startX,startY+4)
- term.write("# # # # # # #### # # #")
- term.setCursorPos(startX,startY+5)
- term.write("# # # # # # # # # #")
- term.setCursorPos(startX,startY+6)
- term.write("# # # #### ###### #### ##### ######")
- term.setCursorPos(startX,startY+7)
- end
- local turtleLogo = function(startX, startY)
- term.clear()
- term.setCursorPos(startX,startY+1)
- term.write(" ##### ###### ## ###### ######")
- term.setCursorPos(startX,startY+2)
- term.write(" # # # # # # # # # ")
- term.setCursorPos(startX,startY+3)
- term.write(" # # # # # ###### #### # # ######")
- term.setCursorPos(startX,startY+4)
- term.write(" # # # # # # # # # #")
- term.setCursorPos(startX,startY+5)
- term.write(" # # # ### ###### #### ###### ######")
- term.setCursorPos(startX,startY+6)
- end
- local function getUserInfo()
- local oldUser = currentUser
- currentUser = "root"
- local users = {}
- local passwords = {}
- local ids = {}
- for i,v in ipairs(fs.list("os/users")) do
- local userHandle = fs.open("os/users/"..v, "r")
- local password = userHandle.readLine()
- table.insert(users, fs.getName(v))
- table.insert(passwords, password)
- if v == root then
- table.insert(ids, 0)
- else
- table.insert(ids, i)
- end
- if not fs.exists(v) then
- fs.makeDir(v)
- end
- userHandle.close()
- end
- currentUser = oldUser
- return users, passwords, ids
- end
- local users, passwords, ids = getUserInfo()
- local function checkUsers(users, passwords, username, password)
- for i,v in ipairs(users) do
- if username == v then
- if passwords[i] == password then
- return true
- else
- return false
- end
- end
- end
- return false
- end
- local function loadWrite(toPrint)
- if turtle then
- turtleLogo(1,2)
- else
- logo(4,5)
- end
- io.write(toPrint)
- end
- local function retrieve(code, file)
- local url = "http://pastebin.com/raw.php?i="..code
- local urlHandle = http.get(url)
- if urlHandle == nil then
- return nil
- end
- if file ~= nil then
- local fileHandle = fs.open(file, "w")
- fileHandle.write(urlHandle.readAll())
- fileHandle.close()
- urlHandle.close()
- return true
- else
- local data = urlHandle.readAll()
- urlHandle.close()
- return data
- end
- end
- loadWrite("Loading OS...")
- loadWrite("Checking for updates...")
- local versionHandle = http.get("http://pastebin.com/raw.php?i=j8qj82d1")
- local latestVersion = versionHandle.readAll()
- versionHandle.close()
- if fs.exists("os/version") then
- local versionHandle = fs.open("os/version", "r")
- local currentVersion = versionHandle.readAll()
- versionHandle.close()
- currentVersion = tonumber(currentVersion)
- latestVersion = tonumber(latestVersion)
- if latestVersion > currentVersion then
- local versionHandle = fs.open("os/version", "w")
- versionHandle.write(tostring(latestVersion))
- versionHandle.close()
- loadWrite("There is a new version available!")
- fs.delete("MuSeOS")
- fs.delete("bin/man")
- retrieve("fquZgJ5p", "MuSeOS")
- retrieve("G33dymxf", "programInstaller")
- retrieve("wfeGgwQH", "bin/man")
- shell.run("programInstaller")
- fs.delete("programInstaller")
- os.reboot()
- end
- else
- loadWrite("No version file found, skipping...")
- local versionHandle = fs.open("os/version", "w")
- versionHandle.write(latestVersion)
- versionHandle.close()
- end
- loadWrite("Installing security hooks...")
- local oldFSOpen = fs.open
- local function isSuperuser()
- if currentUser == "root" then
- return true
- else
- return false
- end
- end
- local function writeToLog(logFile, text)
- local logHandle = oldFSOpen("os/logs/"..logFile, "a")
- logHandle.writeLine("["..currentUser.."] "..text)
- logHandle.close()
- end
- os.sleep(1)
- loadWrite("Step 1: Installing FS hooks...")
- local protectedFolderList = fs.open("os/protected", "r")
- while true do
- local line = protectedFolderList.readLine()
- if line == nil then
- break
- else
- table.insert(protectedFolders, line)
- end
- end
- protectedFolderList.close()
- local function getBasePath(file)
- local location1, location2 = string.find(file, fs.getName(file))
- location1 = location1-1
- return string.sub(file,1,location1)
- end
- local function isInHomeFolder(file)
- local location1, location2 = string.find(file, "home/"..currentUser.."/")
- if location1 == 1 then
- return true
- else
- return false
- end
- end
- function checkInFolder(dir, file)
- for _,v in ipairs(fs.list(dir)) do
- if fs.getName(v) == file then
- if not isSuperuser() then
- return true
- else
- return false
- end
- end
- end
- end
- local function debugprint(text)
- if debugMode then
- print(text)
- end
- end
- local function pathIsProtected(file)
- fileName = fs.getName(file)
- if isSuperuser() then
- return false -- root sees all
- elseif fileName == "startup" then
- return true
- elseif checkInFolder("os", fileName) or getBasePath(file) == "os/" then
- return true
- elseif checkInFolder("os/logs", fileName) or getBasePath(file) == "os/logs/" then
- return true
- elseif checkInFolder("os/users", fileName) or getBasePath(file) == "os/users/" then
- return true
- elseif checkInFolder("os/userSettings/", fileName) or getBasePath(file) == "os/userSettings/" then
- return true -- user settings folder
- elseif checkInFolder("os/userSettings/"..currentUser) or getBasePath(file) == "os/userSettings/"..currentUser.."/" then
- return false -- current user's settings
- elseif checkInFolder(currentUser, fileName) or getBasePath(file) == currentUser.."/" then
- return false
- elseif fileName == "MuSeOS" then
- return true
- else
- for i,v in ipairs(protectedFolders) do
- if getBasePath(file) == v then
- return true
- end
- if checkInFolder(v, fileName) then
- return true
- end
- end
- return false
- end
- return false
- end
- local oldFSDelete = fs.delete
- local oldFSMakeDir = fs.makeDir
- local oldFSCopy = fs.copy
- local oldFSMove = fs.move
- local oldFSIsReadOnly = fs.isReadOnly
- fs.open = function(file, mode)
- if pathIsProtected(file) then
- print("ERROR: Not priviledged enough to modify protected folder!")
- writeToLog("OSLog", "Attempted to access protected folder!")
- return nil
- end
- writeToLog("fileLog", "Opened file:"..file.." with mode "..mode..".")
- return oldFSOpen(file,mode)
- end
- fs.delete = function(file)
- if file == "os" then -- idiot proofing
- print("ERROR: Cannot delete OS folder!")
- writeToLog("OSLog", "Attempted to delete OS folder!")
- return false
- end
- if pathIsProtected(file) then
- print("ERROR: Not priviledged enough to modify protected folder!")
- writeToLog("OSLog", "Attempted to access protected folder!")
- return false
- end
- writeToLog("fileLog", "Deleted file:"..file)
- oldFSDelete(file)
- end
- fs.makeDir = function(dir)
- writeToLog("fileLog", "Made directory:"..dir..".")
- oldFSMakeDir(dir)
- end
- fs.move = function(fromPath, toPath)
- if pathIsProtected(fromPath) or pathIsProtected(toPath) then
- print("ERROR: Not priviledged enough to modify protected folder!")
- writeToLog("OSLog", "Attempted to access protected folder!")
- return false
- end
- writeToLog("fileLog", "Moved file:"..fromPath.." to "..toPath..".")
- oldFSMove(fromPath, toPath)
- end
- fs.move = function(fromPath, toPath)
- if pathIsProtected(fromPath) or pathIsProtected(toPath) then
- print("ERROR: Not priviledged enough to modify protected folder!")
- writeToLog("OSLog", "Attempted to access protected folder!")
- return false
- end
- writeToLog("fileLog", "Moved file:"..fromPath.." to "..toPath..".")
- oldFSCopy(fromPath, toPath)
- end
- fs.isReadOnly = function(file)
- if pathIsProtected(file) and not currentUser == "root" then
- return true
- else
- return oldFSIsReadOnly(file)
- end
- end
- loadWrite("Step 2: Installing RS Hooks...")
- local oldRSSetOutput = rs.setOutput
- local oldRSSetBundledOutput = rs.setBundledOutput
- rs.setOutput = function(side, value)
- writeToLog("RSLog", "Set redstone output on "..side.." to "..tostring(value)..".")
- return oldRSSetOutput(side, value)
- end
- rs.setBundledOutput = function(side, value)
- writeToLog("RSLog", "Set bundled redstone output on "..side.." to "..tostring(value)..".")
- return oldRSSetBundledOutput(side, value)
- end
- redstone.setOutput = rs.setOutput
- redstone.setBundledOutput = rs.setBundledOutput
- if not http then
- loadWrite("HTTP disabled, not installing hooks.")
- else
- loadWrite("Step 3: Installing HTTP hooks...")
- local oldHTTPRequest = http.request
- local oldHTTPGet = http.get
- local oldHTTPPost = http.post
- http.request = function(url, postData)
- if postData == nil then
- writeToLog("HTTPLog", "Request sent to "..url..", no data sent.")
- else
- writeToLog("HTTPLog", "Request sent to "..url..", data:"..postData)
- end
- return oldHTTPRequest(url, postData)
- end
- http.get = function(url)
- writeToLog("HTTPLog", "Fetched data from "..url..".")
- return oldHTTPGet(url)
- end
- http.post = function(url, postData)
- writeToLog("HTTPLog", "Sent data:"..postData.." to url:"..url..".")
- return oldHTTPPost(url, postData)
- end
- print("Done.")
- end
- os.version = function()
- return version
- end
- loadWrite("Step 4: Installing shell hooks and new functions...")
- local oldShellRun = shell.run
- local function existsInFolder(dir, file)
- for _,v in ipairs(fs.list(dir)) do
- if v == file then
- return true -- found a match in the toplevel dir
- end
- end
- end
- shell.run = function(program, ...)
- writeToLog("OSLog", "shell.run called, program:"..program)
- if shell.resolveProgram(program) == nil then
- oldShellRun(fs.combine("bin/", program), unpack(arg))
- else
- oldShellRun(program, unpack(arg))
- end
- end
- loadWrite("Step 5: Installing Rednet Hooks...")
- local oldRednetSend = rednet.send
- local oldRednetBroadcast = rednet.broadcast
- local oldRednetAnnounce = rednet.announce
- rednet.send = function(senderID, message)
- writeToLog("RednetLog", "Sent message to id "..senderID.." with message "..message)
- return oldRednetSend(senderID, message)
- end
- rednet.broadcast = function(message)
- writeToLog("RednetLog", "Broadcasted message "..message)
- return oldRednetBroadcast(message)
- end
- rednet.announce = function()
- writeToLog("RednetLog", "Sent announce message.")
- return oldRednetAnnounce()
- end
- loadWrite("Step 6: Installing OS Hooks...")
- local oldOSShutdown = os.shutdown
- local oldOSReboot = os.reboot
- local oldOSLoadAPI = os.loadAPI
- local oldOSSetLabel = os.setComputerLabel
- os.shutdown = function()
- writeToLog("OSLog", "Shutting down MuSeOS. Uptime:"..tostring(os.clock()))
- oldOSShutdown()
- end
- os.reboot = function()
- writeToLog("OSLog", "Rebooting MuSeOS. Uptime:"..tostring(os.clock()))
- oldOSReboot()
- end
- os.loadAPI = function(api)
- writeToLog("OSLog", "Loading API: "..api)
- oldOSLoadAPI(api)
- end
- os.setComputerLabel = function(label)
- writeToLog("OSLog", "Setting computer label to:"..label)
- oldOSSetLabel(label)
- end
- if turtle then
- loadWrite("Step 7: Installing Turtle API hooks...")
- local settingsFile = fs.open("os/turtle", "r")
- local canMove = settingsFile.readLine()
- local canDig = settingsFile.readLine()
- local canPlace = settingsFile.readLine()
- local canSuck = settingsFile.readLine()
- settingsFile.close()
- local oldTurtleDig = turtle.dig
- local oldTurtleDigUp = turtle.digUp
- local oldTurtleDigDown = turtle.digDown
- local oldTurtlePlaceDown = turtle.placeDown
- local oldTurtlePlaceUp = turtle.placeUp
- local oldTurtlePlace = turtle.place
- local oldTurtleForward = turtle.forward
- local oldTurtleBack = turtle.back
- local oldTurtleTurnLeft = turtle.turnLeft
- local oldTurtleTurnRight = turtle.turnRight
- local oldTurtleUp = turtle.up
- local oldTurtleDown = turtle.down
- local oldTurtleSuck = turtle.suck
- local oldTurtleSuckUp = turtle.suckUp
- local oldTurtleSuckDown = turtle.suckDown
- local oldTurtleRefuel = turtle.refuel
- turtle.dig = function()
- if canDig == "true" or currentUser == "root" then
- writeToLog("TurtleLog", "Broke block in front.")
- return oldTurtleDig()
- else
- writeToLog("TurtleLog", "Tried to dig, but digging has been diabled.")
- return false
- end
- end
- turtle.digUp = function()
- if canDig == "true" or currentUser == "root" then
- writeToLog("TurtleLog", "Broke block on top.")
- return oldTurtleDigUp()
- else
- writeToLog("TurtleLog", "Tried to dig, but digging has been diabled.")
- return false
- end
- end
- turtle.digDown = function()
- if canDig == "true" or currentUser == "root" then
- writeToLog("TurtleLog", "Broke block under.")
- return oldTurtleDigDown()
- else
- writeToLog("TurtleLog", "Tried to dig, but digging has been diabled.")
- return false
- end
- end
- turtle.place = function()
- if canPlace == "true" or currentUser == "root" then
- writeToLog("TurtleLog", "Placed block in front.")
- return oldTurtlePlace()
- else
- writeToLog("TurtleLog", "Tried to place, but placing has been diabled.")
- return false
- end
- end
- turtle.placeDown = function()
- if canPlace == "true" or currentUser == "root" then
- writeToLog("TurtleLog", "Placed block under.")
- return oldTurtlePlaceDown()
- else
- writeToLog("TurtleLog", "Tried to place, but placing has been diabled.")
- return false
- end
- end
- turtle.placeUp = function()
- if canPlace == "true" or currentUser == "root" then
- writeToLog("TurtleLog", "Placed block on top.")
- return oldTurtlePlaceUp()
- else
- writeToLog("TurtleLog", "Tried to place, but placing has been diabled.")
- return false
- end
- end
- turtle.forward = function()
- if canMove == "true" or currentUser == "root" then
- writeToLog("TurtleLog", "Moved forward.")
- return oldTurtleForward()
- else
- writeToLog("TurtleLog", "Tried to move, but movement has been diabled.")
- return false
- end
- end
- turtle.back = function()
- if canMove == "true" or currentUser == "root" then
- writeToLog("TurtleLog", "Moved backwards.")
- return oldTurtleBack()
- else
- writeToLog("TurtleLog", "Tried to move, but movement has been diabled.")
- return false
- end
- end
- turtle.turnLeft = function()
- if canMove == "true" or currentUser == "root" then
- writeToLog("TurtleLog", "Turned left.")
- return oldTurtleTurnLeft()
- else
- writeToLog("TurtleLog", "Tried to move, but movement has been diabled.")
- return false
- end
- end
- turtle.up = function()
- if canMove == "true" or currentUser == "root" then
- writeToLog("TurtleLog", "Went up.")
- return oldTurtleUp()
- else
- writeToLog("TurtleLog", "Tried to move, but movement has been diabled.")
- return false
- end
- end
- turtle.down = function()
- if canMove == "true" or currentUser == "root" then
- writeToLog("TurtleLog", "Went down.")
- return oldTurtleDown()
- else
- writeToLog("TurtleLog", "Tried to move, but movement has been diabled.")
- return false
- end
- end
- turtle.turnRight = function()
- if canMove == "true" or currentUser == "root" then
- writeToLog("TurtleLog", "Turned right.")
- return oldTurtleTurnRight()
- else
- writeToLog("TurtleLog", "Tried to move, but movement has been diabled.")
- return false
- end
- end
- turtle.suck = function()
- if canSuck == "true" or currentUser == "root" then
- writeToLog("TurtleLog", "Retrieved items.")
- return oldTurtleSuck()
- else
- writeToLog("TurtleLog", "Tried to retrieve items, but item retrieval has been diabled.")
- return false
- end
- end
- turtle.suckUp = function()
- if canSuck == "true" or currentUser == "root" then
- writeToLog("TurtleLog", "Retrieved items from above.")
- return oldTurtleSuckUp()
- else
- writeToLog("TurtleLog", "Tried to retrieve items, but item retrieval has been diabled.")
- return false
- end
- end
- turtle.suckDown = function()
- if canSuck == "true" or currentUser == "root" then
- writeToLog("TurtleLog", "Retrieved items from underneath.")
- return oldTurtleSuckDown()
- else
- writeToLog("TurtleLog", "Tried to retrieve items, but item retrieval has been diabled.")
- return false
- end
- end
- turtle.refuel = function()
- writeToLog("TurtleLog", "Refueled turtle, current fuel level is: "..turtle.getFuelLevel())
- return oldTurtleRefuel()
- end
- end
- loadWrite("Loaded OS!")
- loadWrite("Loading usernames/passwords...")
- museOS = {}
- museOS.switchUser = function(password, user)
- local users, passwords = getUserInfo()
- if checkUsers(users,passwords,user,password) then
- writeToLog("OSLog", "User "..currentUser.." has switched to account "..user)
- currentUser = user
- return true
- else
- writeToLog("OSLog", "User "..currentUser.." attempted to switch to account "..user.." but the password was incorrect or the target user did not exist.")
- print("ERROR: User switch failed: target user does not exist/incorrect password!")
- return false
- end
- end
- museOS.sudo = function(password, program, ...)
- local users, passwords = getUserInfo()
- if checkUsers(users, passwords, "root", password) then
- writeToLog("OSLog", "sudo successful, raising to root...")
- local oldUser = currentUser
- currentUser = "root"
- shell.run(program, unpack(arg))
- currentUser = oldUser
- else
- writeToLog("OSLog", "sudo attempt failed!")
- print("ERROR: sudo attempt failed!")
- end
- end
- museOS.getUserID = function()
- local users, passwords, ids = getUserInfo()
- for i,v in ipairs(users) do
- if museOS.getCurrentUser() == v then
- return ids[i]
- end
- end
- end
- museOS.authenticate = function(user, password)
- writeToLog("OSLog", "Authentication request for user "..user..".")
- local users, passwords, ids = getUserInfo()
- return checkUsers(users,passwords,user,password)
- end
- museOS.getUsers = function()
- writeToLog("OSLog", "Userlist requested.")
- local users, passwords, ids = getUserInfo()
- return users, ids
- end
- museOS.getCurrentUser = function()
- return currentUser
- end
- local function showLoginMenu()
- local function readAdv(maxChars, startX, startY, replaceChar, returnToStart, backgroundChar)
- term.setCursorBlink(true)
- local function clearArea(clearLine, clearX, clearArea, replaceChar)
- local formerX,formerY = term.getCursorPos()
- for i=0, clearArea do
- term.setCursorPos(clearX+i, clearLine)
- if replaceChar ~= nil then
- io.write(replaceChar)
- else
- io.write(" ")
- end
- end
- term.setCursorPos(formerX,formerY)
- end
- local x,y = term.getCursorPos()
- term.setCursorPos(startX, startY)
- clearArea(startY, startX, maxChars, backgroundChar)
- local input = ""
- while true do
- local event, key = os.pullEvent()
- if event == "key" then
- if key == 28 or key == 156 then
- if returnToStart then
- term.setCursorPos(x,y)
- end
- return input
- elseif key == 14 then
- input = string.sub(input, 1, #input-1)
- term.setCursorPos(startX, startY)
- clearArea(startY, startX, maxChars, backgroundChar)
- if replaceChar == nil then
- io.write(input)
- else
- for i=1, #input do
- io.write(replaceChar)
- end
- end
- end
- elseif event == "char" then
- if maxChars ~= nil then
- if #input <= maxChars then
- input = input..key
- term.setCursorPos(startX, startY)
- clearArea(startY, startX, maxChars, backgroundChar)
- if replaceChar == nil then
- io.write(input)
- else
- for i=1, #input do
- io.write(replaceChar)
- end
- end
- end
- else
- input = input..key
- term.setCursorPos(startX, startY)
- clearArea(startY, startX, maxChars, backgroundChar)
- if replaceChar == nil then
- io.write(input)
- else
- for i=1, #input do
- io.write(replaceChar)
- end
- end
- end
- end
- end
- end
- local function printLoginScreen(startX, startY)
- term.setCursorPos(startX, startY)
- print("===========================")
- term.setCursorPos(startX, startY+1)
- print("| Please log in: |")
- term.setCursorPos(startX, startY+2)
- print("===========================")
- term.setCursorPos(startX, startY+3)
- print("| Username: |")
- term.setCursorPos(startX, startY+4)
- print("| |")
- term.setCursorPos(startX, startY+5)
- print("===========================")
- term.setCursorPos(startX, startY+6)
- print("| Password: |")
- term.setCursorPos(startX, startY+7)
- print("| |")
- term.setCursorPos(startX, startY+8)
- print("===========================")
- end
- --LOGINSCRN SPECS:
- --XLen: 27
- --YLen: 9
- --Textbox Space: 26
- --Username XStart:2
- --Username YStart:5
- --Password XStart:2
- --Password YStart:8
- term.clear()
- printLoginScreen(13,4)
- username = readAdv(24, 14, 8, nil, true, nil)
- password = readAdv(24, 14, 11, "#", true, nil)
- return username, password
- end
- term.clear()
- term.setCursorPos(1,1)
- loadWrite("Loading APIs...")
- for _,v in ipairs(fs.list("apis/")) do
- loadWrite("Loading api:"..v)
- os.loadAPI("apis/"..v)
- end
- local username, password = showLoginMenu()
- local successful = museOS.authenticate(username, password)
- if successful then
- writeToLog("OSLog", "User "..username.." has successfully logged in.")
- term.clear()
- term.setCursorPos(1,1)
- currentUser = username
- print(os.version())
- for _,v in ipairs(fs.list("modules/")) do
- shell.run("modules/"..v)
- end
- shell.setDir(currentUser)
- shell.setPath(shell.path()..":/bin")
- else
- writeToLog("OSLog", "An attempt was made to log into "..username..", but the password was incorrect, or the user was not found.")
- print("Incorrect password or username. Shutting down...")
- os.sleep(2)
- os.shutdown()
- end
- local function readCommands()
- local commandHistory = {}
- while true do
- write(museOS.getCurrentUser().."@")
- if os.getComputerLabel() == nil then
- write(tostring(os.getComputerID()))
- else
- write(os.getComputerLabel())
- end
- write(":")
- if museOS.getCurrentUser() == "root" then
- if shell.dir() == "" then
- write("-# ")
- else
- write("/"..shell.dir().."# ")
- end
- else
- if shell.dir() == "" then
- write("-$ ")
- else
- write("/"..shell.dir().."$ ")
- end
- end
- local input = read(nil, commandHistory)
- table.insert(commandHistory, input)
- local input_words = {}
- for i in input:gmatch("%S+") do
- table.insert(input_words, i)
- end
- if input_words[1] ~= nil then
- os.pullEvent = oldOSPullEvent
- shell.run(input_words[1], unpack(input_words, 2))
- os.pullEvent = oldOSPullEventRaw
- end
- os.sleep(0.1)
- end
- end
- readCommands()
- os.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement