Advertisement
Chickenbreadlp

ccFTP (Rev2) [API]

Jun 19th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.03 KB | None | 0 0
  1. --[[
  2.     MIT License
  3.  
  4.     Copyright © 2016 Chickenbreadlp
  5.  
  6.     Permission is hereby granted, free of charge, to any person obtaining a copy
  7.     of this software and associated documentation files (the "Software"), to deal
  8.     in the Software without restriction, including without limitation the rights
  9.     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10.     copies of the Software, and to permit persons to whom the Software is
  11.     furnished to do so, subject to the following conditions:
  12.  
  13.     The above copyright notice and this permission notice shall be included in all
  14.     copies or substantial portions of the Software.
  15.  
  16.     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19.     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21.     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22.     SOFTWARE.
  23. ]]
  24.  
  25. local ccFTPRev = "Rev2"
  26. local runningFTPHost = nil
  27.  
  28.  -- GitHub File Downloader for Self-Suppling
  29. local function GITget(branch, file)
  30.     local response = http.get(
  31.         "https://raw.githubusercontent.com/Chickenbreadlp/ReboOSt/"..branch.."/"..textutils.urlEncode( file )
  32.     )
  33.  
  34.     if response then
  35.         local sResponse = response.readAll()
  36.         response.close()
  37.         return sResponse
  38.     else
  39.     end
  40. end
  41. local function GITdownload(branch, filename, sFile)
  42.     if fs.exists( sFile ) then
  43.         return
  44.     end
  45.  
  46.     local res = GITget(branch, filename)
  47.     if res then
  48.         local file = fs.open( sFile, "w" )
  49.         file.write( res )
  50.         file.close()
  51.     end
  52. end
  53.  
  54.  -- Pastebin Downloader for Self-Suppling
  55. local function PASget(paste)
  56.     local response = http.get(
  57.         "http://pastebin.com/raw/"..textutils.urlEncode( paste )
  58.     )
  59.  
  60.     if response then
  61.         local sResponse = response.readAll()
  62.         response.close()
  63.         return sResponse
  64.     else
  65.     end
  66. end
  67. local function PASdownload(paste, sFile)
  68.     if fs.exists( sFile ) then
  69.         return
  70.     end
  71.  
  72.     local res = PASget(paste)
  73.     if res then
  74.         local file = fs.open( sFile, "w" )
  75.         file.write( res )
  76.         file.close()
  77.     end
  78. end
  79.  
  80.  
  81.  -- Way to create a keypair for AES from single case-sensitive key for Server functions
  82. local function unfoldkeyS(keyhash)
  83.  math.randomseed(tonumber(keyhash, 36))
  84.  local key = {}
  85.  local iv = {}
  86.  for i=1, 16 do
  87.   key[i] = math.random(0, 0xFF)
  88.   iv[i] = math.random(0, 0xFF)
  89.  end
  90.  math.randomseed(os.clock())
  91.  return key, iv
  92. end
  93.  
  94.  -- Way to create a keypair for AES from single case-sensitive key for Clint functions
  95. local function unfoldkeyC(Key)
  96.  local keyhash = hash.sha256(Key)
  97.  math.randomseed(tonumber(keyhash, 36))
  98.  local key = {}
  99.  local iv = {}
  100.  for i=1, 16 do
  101.   key[i] = math.random(0, 0xFF)
  102.   iv[i] = math.random(0, 0xFF)
  103.  end
  104.  math.randomseed(os.clock())
  105.  return key, iv
  106. end
  107.  
  108.  
  109.  -- checks if needed APIs are available and (if not) do exist
  110. local function checkAPI()
  111.  if ccaes == nil then
  112.   if fs.exists("/.ccftp/ccaes") then
  113.    os.loadAPI("/.ccftp/ccaes")
  114.   else
  115.    PASdownload("rCYDnCxn", "/.ccftp/ccaes")
  116.    os.loadAPI("/.ccftp/ccaes")
  117.   end
  118.  end
  119.  if hash == nil then
  120.   if fs.exists("/.ccftp/hash") then
  121.    os.loadAPI("/.ccftp/hash")
  122.   else
  123.    GITdownload("release", "ReboOSt/APIs/encryption", "/.ccftp/hash")
  124.    os.loadAPI("/.ccftp/hash")
  125.   end
  126.  end
  127. end
  128.  
  129.  -- checks for a Modem
  130. local function checkModem()
  131.  if rednet.isOpen() then
  132.   return true
  133.  end
  134.  for _, side in ipairs( rs.getSides() ) do
  135.   if peripheral.isPresent( side ) and peripheral.getType( side ) == "modem" then
  136.    rednet.open( side )
  137.    return true
  138.   end
  139.  end
  140.  return false
  141. end
  142.  
  143.  
  144.  -- waits for receiving a message and unserialize it
  145. local function waitForMessage(typ)
  146.  local senderID = nil
  147.  local command = nil
  148.  if typ == "C" then
  149.   senderID, command = rednet.receive("ccFTP", 5)
  150.  elseif typ == "S" then
  151.   senderID, command = rednet.receive("ccFTP")
  152.  end
  153. -- command = textutils.unserialize(command)
  154.  return command, senderID
  155. end
  156.  
  157.  -- sendMessage function for Client tools
  158. local function sendMessageC(Hostname, UsrName, PassIV, command)
  159.  local command = { UsrName, PassIV, command }
  160. -- textutils.serialize(command)
  161.  local hostID = rednet.lookup("ccFTP", Hostname)
  162.  if hostID == nil then
  163.   return nil
  164.  else
  165.   rednet.send(hostID, command, "ccFTP")
  166.   return hostID
  167.  end
  168. end
  169.  
  170.  -- sendMessage function for Server tools
  171. local function sendMessageS(response, ID)
  172. -- command = textutils.serialize(command)
  173.  rednet.send(ID, response, "ccFTP")
  174. end
  175.  
  176.  
  177.  -- returns the local API Rev
  178. function getClientVer()
  179.  return ccFTPRev
  180. end
  181.  
  182.  -- returns the API Rev of Hostname or "nsh" if Hostname was not found
  183. function getServerVer(Hostname)
  184.  checkAPI()
  185.  if checkModem() then
  186.   local hosterID = sendMessageC(Hostname, "VerCheck", hash.sha256("VerCheck"), "VerCheck")
  187.   if hosterID then
  188.    local response, senderID = waitForMessage("C")
  189.    if senderID == hosterID then
  190.     return response
  191.    elseif senderID == nil then
  192.     return "nsh"
  193.    end
  194.   elseif hosterID == nil then
  195.    return "nsh"
  196.   end
  197.  end
  198. end
  199.  
  200.  
  201.   ---- Server Functions ----
  202.  
  203.  -- returns true and current hostname if hostname is still active else it returns false
  204. function getRunningHost()
  205.  if runningFTPHost ~= nil then
  206.   return true, runningFTPHost
  207.  else
  208.   return false
  209.  end
  210. end
  211.  
  212.  -- returns true if hostname was still active and is now shutdown, false if no hostname was running
  213. function closeServer()
  214.  if runningFTPHost ~= nil then
  215.   rednet.unhost("ccFTP", runningFTPHost)
  216.   runningFTPHost = nil
  217.   return true
  218.  else
  219.   return false
  220.  end
  221. end
  222.  
  223.  -- runs a FTP Server (Hostname sets the FTP-Server Name ; FtpDir sets the Directory which should be shared through ccFTP ; UsrDir sets the directory, where the Accounts will be saved)
  224.  -- It is not recommended to use Root Directory as FTP Share
  225. function runServer(Hostname, loopoff, FtpDir, UsrDir)
  226.  
  227.  if Hostname == nil then
  228.   error("Hostname missing!")
  229.  end
  230.  
  231.  local UsrDir = UsrDir
  232.  local FtpDir = FtpDir
  233.  local loopoff = loopoff
  234.  
  235.  if type(loopoff) ~= "boolean" and type(loopoff) ~= "nil" then
  236.   loopoff = false
  237.  end
  238.  
  239.  if UsrDir == nil then UsrDir = "/.ccftp/Users" end
  240.  if FtpDir == nil then FtpDir = "/FTP_Share" end
  241.  if not fs.exists(FtpDir) then fs.makeDir(FtpDir) end
  242.  
  243.  checkAPI()
  244.  
  245.  if checkModem() then
  246.   if runningFTPHost ~= Hostname and runningFTPHost ~= nil then
  247.    closeServer()
  248.   end
  249.   rednet.host("ccFTP", Hostname)
  250.   runningFTPHost = Hostname
  251.   while true do
  252.    
  253.    local commandtab = nil
  254.    local sender = nil
  255.    commandtab, sender = waitForMessage("S")
  256.    
  257.    if #commandtab >= 3 then
  258.     if commandtab[1] == "VerCheck" and commandtab[2] == hash.sha256("VerCheck") and commandtab[3] == "VerCheck" then
  259.      sendMessageS(ccFTPRev, sender)
  260.      if loopoff == true then
  261.       return true, sender, "VerCheck", ""
  262.      end
  263.     elseif fs.exists(UsrDir.."/"..commandtab[1]) then
  264.    
  265.      local file = fs.open(UsrDir.."/"..commandtab[1], "r")
  266.      local pass = file.readLine()
  267.      file.close()
  268.      
  269.      local key, iv = unfoldkeyS(pass)
  270.      
  271.      if hash.sha256("iv"..iv[15]) == commandtab[2] then
  272.      
  273.       if commandtab[3][1] == "DirCont" then
  274.        if #commandtab[3] >= 2 then
  275.         if fs.exists(FtpDir.."/"..commandtab[3][2]) and fs.isDir(FtpDir.."/"..commandtab[3][2]) then
  276.          local DirCont = fs.list(FtpDir.."/"..commandtab[3][2])
  277.          local DirIdx = {}
  278.          for _, content in ipairs(DirCont) do
  279.           if fs.isDir(FtpDir.."/"..content) then
  280.            table.insert(DirIdx, content)
  281.           end
  282.          end
  283.          sendMessageS( { DirCont, DirIdx } , sender)
  284.          if loopoff == true then
  285.           return true, sender, "DirCont", commandtab[3][2]
  286.          end
  287.         else
  288.          sendMessageS("ddne", sender)
  289.          if loopoff == true then
  290.           return false, sender, "DirCont", "ddne"
  291.          end
  292.         end
  293.        else
  294.         sendMessageS("nea", sender)
  295.         if loopoff == true then
  296.          return false, sender, "DirCont", "nea"
  297.         end
  298.        end
  299.      
  300.       elseif commandtab[3][1] == "getFile" then
  301.        if #commandtab[3] >= 2 then
  302.         if fs.exists(FtpDir.."/"..commandtab[3][2]) and not fs.isDir(FtpDir.."/"..commandtab[3][2]) then
  303.          local file = fs.open(FtpDir.."/"..commandtab[3][2], "rb")
  304.          local fileData = {}
  305.          local filebyte = file.read()
  306.          repeat
  307.           table.insert(fileData, filebyte)
  308.           filebyte =file.read()
  309.          until filebyte == nil
  310.          file.close()
  311.          fileData = ccaes.encrypt_bytestream(fileData, key, iv)
  312.          local response = { hash.sha256("iv"..iv[16]), fileData }
  313.          sendMessageS(response, sender)
  314.          if loopoff == true then
  315.           return true, sender, "getFile", commandtab[3][2]
  316.          end
  317.         else
  318.          sendMessageS("fdne", sender)
  319.          if loopoff == true then
  320.           return false, sender, "getFile", "fdne"
  321.          end
  322.         end
  323.        else
  324.         sendMessageS("nea", sender)
  325.         if loopoff == true then
  326.          return false, sender, "getFile", "nea"
  327.         end
  328.        end
  329.      
  330.       elseif commandtab[3][1] == "pushFile" then
  331.        if #commandtab[3] >= 3 then
  332.         local decdat = ccaes.decrypt_bytestream(commandtab[3][3], key, iv)
  333.         local file = fs.open(FtpDir.."/"..commandtab[3][2], "wb")
  334.         for i = 1, #decdat do
  335.          file.write(decdat[i])
  336.         end
  337.         file.close()
  338.         sendMessageS("OK", sender)
  339.         if loopoff == true then
  340.          return true, sender, "pushFile", commandtab[3][2]
  341.         end
  342.        
  343.        else
  344.         sendMessageS("nea", sender)
  345.         if loopoff == true then
  346.          return false, sender, "pushFile", "nea"
  347.         end
  348.        end
  349.      
  350.       elseif commandtab[3][1] == "delFile" then
  351.        if #commandtab[3] >= 2 then
  352.         if fs.exists(FtpDir.."/"..commandtab[3][2]) then
  353.          fs.delete(FtpDir.."/"..commandtab[3][2])
  354.          sendMessageS("OK", sender)
  355.          if loopoff == true then
  356.           return true, sender, "delFile", commandtab[3][2]
  357.          end
  358.         else
  359.          sendMessageS("fdne", sender)
  360.          if loopoff == true then
  361.           return false, sender, "delFile", "fdne"
  362.          end
  363.         end
  364.        
  365.        else
  366.         sendMessageS("nea", sender)
  367.         if loopoff == true then
  368.          return false, sender, "delFile", "nea"
  369.         end
  370.        end
  371.      
  372.       else
  373.        sendMessageS("nsc", sender)
  374.        if loopoff == true then
  375.         return false, sender, commandtab[3][1], "nsc"
  376.        end
  377.       end
  378.      
  379.      else
  380.       sendMessageS("pww", sender)
  381.       if loopoff == true then
  382.        return false, sender, commandtab[3][1], "pww"
  383.       end
  384.      end
  385.    
  386.     else
  387.      sendMessageS("nsu", sender)
  388.      if loopoff == true then
  389.       return false, sender, commandtab[3][1], "nsu"
  390.      end
  391.     end
  392.    
  393.    else
  394.     sendMessageS("nea", sender)
  395.     if loopoff == true then
  396.      return false, sender, "nea"
  397.     end
  398.    end
  399.  
  400.   end
  401.  
  402.  else
  403.   return "nMa"
  404.  end
  405.  
  406. end
  407.  
  408.  -- creates a new user (should only be run on ftp server) returns true when finishing succesfull; returns nUsrN when no user is specified; returns nUsrP when no Password is specified; returns UsrE when user already exists
  409. function createUser(UsrName, UsrPass, UsrDir)
  410.  checkAPI()
  411.  if UsrName == nil then return "nUsrN" end
  412.  if UsrPass == nil then return "nUsrP" end
  413.  local UsrDir = UsrDir or "/.ccftp/Users"
  414.  if fs.exists(UsrDir.."/"..UsrName) or UsrName == "VerCheck" then return "UsrE" end
  415.  local file = fs.open(UsrDir.."/"..UsrName, "w")
  416.  file.writeLine(hash.sha256(UsrPass))
  417.  file.close()
  418.  return true
  419. end
  420.  
  421.  -- renames an already existing User (should only be run on ftp server) returns true when successfull; returns noUsrN when oUsrName not specified; returns nnUsrN when nUsrName not specified; returns nUsrP when UsrPass not specified; returns nsUsr when User does not exists; returns nUsrE when target name exists;returns pww when password wrong
  422. function renameUser(oUsrName, nUsrName, UsrPass, UsrDir)
  423.  checkAPI()
  424.  if oUsrName == nil then return "noUsrN" end
  425.  if nUsrName == nil then return "nnUsrN" end
  426.  if UsrPass == nil then return "nUsrP" end
  427.  local UsrDir = UsrDir or "/.ccftp/Users"
  428.  if not fs.exists(UsrDir.."/"..oUsrName) then return "nsUsr" end
  429.  if fs.exists(UsrDir.."/"..nUsrName) or nUsrName == "VerCheck" then return "nUsrE" end
  430.  local file = fs.open(UserDir.."/"..oUsrName, "r")
  431.  local opass = file.readLine()
  432.  file.close()
  433.  if opass == hash.sha256(UsrPass) then
  434.   fs.move(UsrDir.."/"..oUsrName, UsrDir.."/"..nUsrName)
  435.   return true
  436.  else
  437.   return "pww"
  438.  end
  439. end
  440.  
  441.  -- changes the password from any existing User (should only be run on ftp server) returns true when successfull; returns nUsrN when UsrName not specified; returns noUsrP when oUsrPass not specified; returns nnUsrP when nUsrPass not specified; returns nsUsr when User does not exists; returns pww when password wrong
  442. function changeUserPass(UsrName, oUsrPass, nUsrPass, UsrDir)
  443.  checkAPI()
  444.  if UsrName == nil then return "nUsrN" end
  445.  if oUsrPass == nil then return "noUsrP" end
  446.  if nUsrPass == nil then return "nnUsrP" end
  447.  local UsrDir = UsrDir or "/.ccftp/Users"
  448.  if not fs.exists(UsrDir.."/"..UsrName) then return "nsUsr" end
  449.  local file = fs.open(UsrDir.."/"..UsrName, "r")
  450.  local opass = file.readLine()
  451.  file.close()
  452.  if opass == hash.sha256(oUsrPass) then
  453.   local file = fs.open(UsrDir.."/"..UsrName, "w")
  454.   file.writeLine(hash.sha256(nUsrPass))
  455.   file.close()
  456.   return true
  457.  else
  458.   return "pww"
  459.  end
  460. end
  461.  
  462.  -- removes any given user (should only be run on ftp server) returns turn when successfull; returns nUsrN when UsrName not specified; returns nUsrP when UsrPass not specified; returns nsUsr when User does not exists; returns pww when password wrong
  463. function removeUser(UsrName, UsrPass, UsrDir)
  464.  checkAPI()
  465.  if UsrName == nil then return "nUsrN" end
  466.  if UsrPass == nil then return "nUsrP" end
  467.  local UsrDir = UsrDir or "/.ccftp/Users"
  468.  if not fs.exists(UsrDir.."/"..UsrName) then return "nsUsr" end
  469.  local file = fs.open(UsrDir.."/"..UsrName)
  470.  local opass = file.readLine()
  471.  file.close()
  472.  if opass == hash.sha256(UsrPass) then
  473.   fs.delete(UsrDir.."/"..UsrName)
  474.   return true
  475.  else
  476.   return "pww"
  477.  end
  478. end
  479.  
  480.  
  481.  -- runs the Server Shell (should only be used in parralel with runServer) (currently broken; using code for example program)
  482. local function runServerShell()
  483.  local ServCol
  484.  if term.isColor() then ServCol = {colors.yellow, colors.lightBlue} else ServCol = {colors.lightGray, colors.white} end
  485.  local function clearShellScreen()
  486.   term.setBackgroundColor(colors.black)
  487.   term.clear()
  488.   term.setCursorPos(1,1)
  489.   term.setTextColor(ServCol[1])
  490.   print("ccFTP Server Shell")
  491.  end
  492.  clearShellScreen()
  493.  while(true) do
  494.   local opull = os.pullEvent
  495.   term.setTextColor(ServCol[2])
  496.   write("-> ")
  497.   term.setTextColor(colors.white)
  498.   os.pullEvent = os.pullEventRaw
  499.   local input = string.lower(read())
  500.   os.pullEvent = opull
  501.   if input == "reboot" then
  502.    os.reboot()
  503.   elseif input == "shutdown" then
  504.    os.shutdown()
  505.   elseif input == "close" then
  506.    rednet.unhost("ccFTP", runningHost)
  507.    break
  508.   elseif input == "clear" then
  509.    clearShellScreen()
  510.   elseif input == "change hostname" then
  511.    if runningHost ~= nil then
  512.     os.pullEvent = os.pullEventRaw
  513.     term.setTextColor(ServCol[2])
  514.     print("Current Hostname: "..runningHost)
  515.     write("New Hostname: ")
  516.     term.setTextColor(colors.white)
  517.     local input = read()
  518.     os.pullEvent = opull
  519.     if input ~= nil then
  520.      rednet.unhost("ccFTP", runningHost)
  521.      rednet.host("ccFTP", input)
  522.      runningHost = input
  523.     else
  524.      printError("Please enter a Hostname!")
  525.     end
  526.    else
  527.     print("There is no ccFTP Server running on this computer!")
  528.    end
  529.    os.pullEvent = opull
  530.   elseif input == "create User" then
  531.    os.pullEvent = os.pullEventRaw
  532.    term.setTextColor(ServCol[2])
  533.    print("User to create: ")
  534.    term.setTextColor(colors.white)
  535.    local user = read()
  536.    if user ~= "" then
  537.     term.setTextColor(ServCol[2])
  538.     print("Password: ")
  539.     term.setTextColor(colors.white)
  540.     local pass = read("*")
  541.     if pass ~= "" then
  542.      local res = createUser(user, pass)
  543.      if res == true then
  544.       term.setTextColor(ServCol[1])
  545.       print("User created!")
  546.      elseif res == "UsrE" then
  547.       printError("User already exists!")
  548.      end
  549.     else
  550.      printError("Please specify a Password!")
  551.     end
  552.    else
  553.     printError("Please specify a Username!")
  554.    end
  555.    os.pullEvent = opull
  556.   elseif input == "change Username" then
  557.    os.pullEvent = os.pullEventRaw
  558.    term.setTextColor(ServCol[2])
  559.    write("Current Username: ")
  560.    term.setTextColor(colors.white)
  561.    local oName = read()
  562.    if oName ~= "" then
  563.     term.setTextColor(ServCol[2])
  564.     write("Current Password: ")
  565.     term.setTextColor(colors.white)
  566.     local pass = read("*")
  567.     if pass ~= "" then
  568.      term.setTextColor(ServCol[2])
  569.      write("New Username: ")
  570.      term.setTextColor(colors.white)
  571.      local nName = read()
  572.      if nName ~= "" then
  573.       renameUser(oName, nName, pass)
  574.       if res == true then
  575.        term.setTextColor(ServCol[1])
  576.        print("Renamed "..oName.." to "..nName)
  577.       elseif res == "nsUsr" then
  578.        printError("User doesn not exist!")
  579.       elseif res == "nUsrE" then
  580.        printError("New Username is already occupied!")
  581.       elseif res == "pww" then
  582.        printError("Password wrong!")
  583.       end
  584.      else
  585.       printError("Please enter in the new Username!")
  586.      end
  587.     else
  588.      printError("Please enter in the current Password!")
  589.     end
  590.    else
  591.     printError("Please put in the current Username!")
  592.    end
  593.    os.pullEvent = opull
  594.   elseif input == "change Password" then
  595.    os.pullEvent = os.pullEventRaw
  596.    term.setTextColor(ServCol[2])
  597.    write("Username: ")
  598.    term.setTextColor(colors.white)
  599.    local Uname = read()
  600.    if Uname ~= "" then
  601.     term.setTextColor(ServCol[2])
  602.     write("Current Password: ")
  603.     term.setTextColor(colors.white)
  604.     local opass = read("*")
  605.     if opass ~= "" then
  606.      term.setTextColor(ServCol[2])
  607.      write("New Password: ")
  608.      term.setTextColor(colors.white)
  609.      local npass = read()
  610.      if nName ~= "" then
  611.       local res = changeUserPass(Uname, opass, npass)
  612.       if res == true then
  613.        term.setTextColor(ServCol[1])
  614.        print("Changed Password from "..Uname)
  615.       elseif res == "nsUsr" then
  616.        printError("User does not exist!")
  617.       elseif res == "pww" then
  618.        printError("Password wrong!")
  619.       end
  620.      else
  621.       printError("Please enter in the new Username!")
  622.      end
  623.     else
  624.      printError("Please enter in the current Password!")
  625.     end
  626.    else
  627.     printError("Please put in the current Username!")
  628.    end
  629.    os.pullEvent = opull
  630.   elseif input == "delete User" then
  631.    os.pullEvent = os.pullEventRaw
  632.    term.setTextColor(ServCol[2])
  633.    write("Username: ")
  634.    term.setTextColor(colors.white)
  635.    local Uname = read()
  636.    if Uname ~= "" then
  637.     term.setTextColor(ServCol[2])
  638.     write("Password: ")
  639.     term.setTextColor(colors.white)
  640.     local pass = read("*")
  641.     if pass ~= "" then
  642.      local res = removeUser(Uname, pass)
  643.      if res == true then
  644.       term.setTextColor(ServCol[1])
  645.       print("Deleted User "..Uname)
  646.      elseif res == "nsUsr" then
  647.       printError("User does not exists!")
  648.      elseif res == "pww" then
  649.       printError("Password wrong!")
  650.      end
  651.     else
  652.      printError("Please enter in a password!")
  653.     end
  654.    else
  655.     printError("Please specify the Username!")
  656.    end
  657.    os.pullEvent = opull
  658.   end
  659.  end
  660. end
  661.  
  662.  
  663.   ---- Client Functions ----
  664.  
  665.  -- gets the Directory Tree for the specified Subdirectory (optional)
  666. function getDirCont(Hostname, UsrName, UsrPass, Subdir)
  667.  if Hostname == nil then
  668.   return "nHn"
  669.  elseif UsrName == nil then
  670.   return "nUsrN"
  671.  elseif UsrPass == nil then
  672.   return "nUsrP"
  673.  end
  674.  Subdir = Subdir or ""
  675.  checkAPI()
  676.  if checkModem() then
  677.   local key, iv = unfoldkeyC(UsrPass)
  678.   local hosterID = sendMessageC(Hostname, UsrName, hash.sha256("iv"..iv[15]), {"DirCont", Subdir})
  679.   if hosterID then
  680.    local response, senderID = waitForMessage("C")
  681.    if senderID == hosterID then
  682.     if response[2] ~= nil then
  683.      return response[1], response[2]
  684.     else
  685.      return response
  686.     end
  687.    elseif senderID == nil then
  688.     return "nsh"
  689.    end
  690.   elseif hosterID == nil then
  691.    return "nsh"
  692.   end
  693.  else
  694.   return "nMa"
  695.  end
  696. end
  697.  
  698.  -- gets a single File from FTP-Server (FileHost is the full path to the file in the Share ; FileClient sets the path where the file should be saved)
  699. function getFile(Hostname, UsrName, UsrPass, FileHost, FileClient)
  700.  if Hostname == nil then
  701.   return "nHn"
  702.  elseif UsrName == nil then
  703.   return "nUsrN"
  704.  elseif UsrPass == nil then
  705.   return "nUsrP"
  706.  elseif FileHost == nil then
  707.   return "nFPH"
  708.  elseif FileClient == nil then
  709.   return "nFPC"
  710.  end
  711.  checkAPI()
  712.  if checkModem() then
  713.   local key, iv = unfoldkeyC(UsrPass)
  714.   local hosterID = sendMessageC(Hostname, UsrName, hash.sha256("iv"..iv[15]), {"getFile", FileHost})
  715.   if hosterID then
  716.    local response, senderID = waitForMessage("C")
  717.    if senderID == hosterID then
  718.     if response[1] == hash.sha256("iv"..iv[16]) then
  719.      local content = ccaes.decrypt_bytestream(response[2], key, iv)
  720.      local file = fs.open(FileClient, "wb")
  721.      for i = 1, #content do
  722.       file.write(content[i])
  723.      end
  724.      file.close()
  725.      return true
  726.     else
  727.      return response
  728.     end
  729.    elseif senderID == nil then
  730.     return "nsh"
  731.    end
  732.   else
  733.    return "nsh"
  734.   end
  735.  else
  736.   return "nMa"
  737.  end
  738. end
  739.  
  740.  -- puts a single file onto the FTP-Server (FileClient is the path of the file which should be uploaded ; FileHost sets the full path, where the file should be saved)
  741. function pushFile(Hostname, UsrName, UsrPass, FileClient, FileHost)
  742.  if Hostname == nil then
  743.   return "nHn"
  744.  elseif UsrName == nil then
  745.   return "nUsrN"
  746.  elseif UsrPass == nil then
  747.   return "nUsrP"
  748.  elseif FileHost == nil then
  749.   return "nFPH"
  750.  elseif FileClient == nil then
  751.   return "nFPC"
  752.  elseif not fs.exists(FileClient) then
  753.   return "FPCnE"
  754.  end
  755.  checkAPI()
  756.  if checkModem() then
  757.   local key, iv = unfoldkeyC(UsrPass)
  758.   local file = fs.open(FileClient, "rb")
  759.   local fileData = {}
  760.   local fileByte = file.read()
  761.   repeat
  762.   table.insert(fileData, fileByte)
  763.   fileByte = file.read()
  764.   until fileByte == nil
  765.   file.close()
  766.   fileData = ccaes.encrypt_bytestream(fileData, key, iv)
  767.   local hosterID = sendMessageC(Hostname, UsrName, hash.sha256("iv"..iv[15]), {"pushFile", FileHost, fileData})
  768.   if hosterID then
  769.    local response, senderID = waitForMessage("C")
  770.    if senderID == hosterID then
  771.     if response == "OK" then
  772.      return true
  773.     else
  774.      return response
  775.     end
  776.    elseif senderID == nil then
  777.     return "nsh"
  778.    end
  779.   else
  780.    return "nsh"
  781.   end
  782.  else
  783.   return "nMa"
  784.  end
  785. end
  786.  
  787.  -- removes a single File from the FTP-Server
  788. function removeFile(Hostname, UsrName, UsrPass, FileHost)
  789.  if Hostname == nil then
  790.   return "nHn"
  791.  elseif UsrName == nil then
  792.   return "nUsrN"
  793.  elseif UsrPass == nil then
  794.   return "nUsrP"
  795.  elseif FileHost == nil then
  796.   return "nFPH"
  797.  end
  798.  checkAPI()
  799.  if checkModem() then
  800.   local key, iv = unfoldkeyC(UsrPass)
  801.   local hosterID = sendMessageC(Hostname, UsrName, hash.sha256("iv"..iv[15]), {"delFile", FileHost})
  802.   if hosterID then
  803.    local response, senderID = waitForMessage("C")
  804.    if senderID == hosterID then
  805.     if response == "OK" then
  806.      return true
  807.     else
  808.      return response
  809.     end
  810.    elseif senderID == nil then
  811.     return "nsh"
  812.    end
  813.   elseif hosterID == nil then
  814.    return "nsh"
  815.   end
  816.  else
  817.   return "nMa"
  818.  end
  819. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement