Stary2001

Untitled

Jan 7th, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. local args={...}
  2.  
  3. for k,v in pairs(rs.getSides()) do
  4. if peripheral.getType(v) == "modem" then
  5. rednetSide = v
  6. break
  7. end
  8. end
  9.  
  10. if not rednetSide then print("Rednet is needed!") return end
  11.  
  12. rednet.open(rednetSide)
  13.  
  14. local clients = {}
  15.  
  16. function getDir(file)
  17. return string.gsub(file,fs.getName(file)+"%/","")
  18. end
  19.  
  20. function fix(file)
  21. if file:sub(1,1) == "/" then
  22. file = file:sub(2)
  23. end
  24. return file
  25. end
  26.  
  27. function getPerms(file)
  28. if perms[fix(getDir(file))] then
  29. if perms[fix(file)] then
  30. return perms[fix(file)]
  31. else
  32. return perms[fix(getDir(file))]
  33. end
  34. else
  35. return {write="all",read="all"}
  36. end
  37. end
  38.  
  39. function hasRead(file, user)
  40. local perms = getPerms(file).read
  41. if perms == "anon" then
  42. return true
  43. elseif perms == "logged_in" then
  44. return user~=nil
  45. elseif type(perms) == "table" then
  46. for k,v in pairs(perms) do
  47. if user == v then return true end
  48. end
  49. return false
  50. end
  51. end
  52.  
  53. function hasWrite(file, user)
  54. local perms = getPerms(file).write
  55. if perms == "anon" then
  56. return true
  57. elseif perms == "logged_in" then
  58. return user~=nil
  59. elseif type(perms) == "table" then
  60. for k,v in pairs(perms) do
  61. if user == v then return true end
  62. end
  63. return false
  64. end
  65. end
  66.  
  67. function put(id,data)
  68. local filename ,contents = string.gmatch(data,"^(.-):(.+)$")()
  69.  
  70. if not hasWrite(clients[id].user) then
  71. print("User " .. clients[id].user .. " tried to write forbidden file " ..filename)
  72. return false
  73. else
  74. local f = fs.open(filename,"w")
  75. f.write(contents)
  76. f.close()
  77. return true
  78. end
  79. end
  80.  
  81. function get(id,data)
  82. local filename = data
  83.  
  84. if not hasRead(clients[id].user) then
  85. print("User " .. clients[id].user .. " tried to read forbidden file ".. filename)
  86. else
  87. local f = fs.open(filename,"r")
  88. rednet.send(id,"DATA " .. f.readAll())
  89. f.close()
  90. end
  91. end
  92.  
  93. function auth(id,data)
  94. local u , pass = string.gmatch(data,"^(.-):(.+)$")()
  95.  
  96. if pass == users[u] then
  97. print(u .. " connected with id ".. id)
  98. clients[id] = {user=u}
  99. return true
  100. else
  101. print("Attempt to log into " .. u .. " failed!")
  102. return false
  103. end
  104. end
  105.  
  106. function host()
  107. while true do
  108. local clientID,msg = rednet.receive()
  109. print(clientID," ",msg)
  110. if msg:sub(1,7) == "CONNECT" then
  111. if auth(clientID,msg:sub(9)) then
  112. rednet.send(clientID,"ACCEPT")
  113. else
  114. rednet.send(clientID,"DENY")
  115. end
  116. elseif msg:sub(1,3) == "GET" then
  117. get(clientID,msg:sub(5))
  118. elseif msg:sub(1,3) == "PUT" then
  119. put(clientID,msg:sub(5))
  120. elseif msg:sub(1,4) == "QUIT" then
  121. clients[clientID] = nil
  122. end
  123. end
  124. end
  125.  
  126. function client()
  127. while true do
  128. local serverID,msg = rednet.receive()
  129.  
  130. if msg == "ACCEPT" then
  131. print("Connection accepted.")
  132. elseif msg == "DENY" then
  133. print("Permission denied.")
  134. elseif msg:sub(1,4) == "DATA" then
  135. print(msg:sub(5))
  136. end
  137. end
  138. end
  139.  
  140. function readFile(name)
  141. local f = fs.open(name,"r")
  142. local r = f.readAll()
  143. f.close()
  144. return r
  145. end
  146.  
  147. function toCmd(line)
  148. if string.lower(line:sub(1,3)) == "get" then
  149. return "GET " .. line:sub(5)
  150. elseif string.lower(line:sub(1,3)) == "put" then
  151. return "PUT " .. line:sub(5) .. readFile(line:sub(5))
  152. end
  153. end
  154.  
  155. function input()
  156. while true do
  157. local line = io.read()
  158. rednet.send(server,toCmd(line))
  159. end
  160. end
  161.  
  162.  
  163. if #args >= 1 then
  164.  
  165. if args[1] == "host" then
  166. local f= fs.open("users","r")
  167. users = textutils.unserialize(f.readAll())
  168. f.close()
  169.  
  170. f = fs.open("perms","r")
  171. perms = textutils.unserialize(f.readAll())
  172. f.close()
  173. host()
  174. else
  175. local id = tonumber(args[1])
  176.  
  177. io.write("Username: ")
  178. local user = read()
  179. io.write("Password: ")
  180. local pass = read("*")
  181. rednet.send(id,"CONNECT "..user..":"..pass)
  182. parallel.waitForAny(client,input)
  183. end
  184. else
  185. print("Usage: ftp host|<id>")
  186. end
  187.  
  188. rednet.close(rednetSide)
Advertisement
Add Comment
Please, Sign In to add comment