Stary2001

Untitled

Jan 7th, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 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 == "all" 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. local user
  71. if clients[id] then
  72. user=clients[id].user
  73. end
  74. if not hasWrite(filename,user) then
  75. if not user then
  76. user = "an anonymous user"
  77. end
  78. print("User " .. user .. " tried to write forbidden file " ..filename)
  79. return false
  80. else
  81. local f = fs.open(filename,"w")
  82. f.write(contents)
  83. f.close()
  84. return true
  85. end
  86. end
  87.  
  88. function get(id,data)
  89. local filename = data
  90.  
  91. local user
  92. if clients[id] then
  93. user=clients[id].user
  94. end
  95.  
  96. if not hasRead(filename,user) then
  97. if not user then
  98. user = "an anonymous user"
  99. end
  100. print("User " .. user .. " tried to read forbidden file ".. filename)
  101. else
  102. local f = fs.open(filename,"r")
  103. rednet.send(id,"DATA " .. f.readAll())
  104. f.close()
  105. end
  106. end
  107.  
  108. function auth(id,data)
  109. local u , pass = string.gmatch(data,"^(.-):(.+)$")()
  110.  
  111. if pass == users[u] then
  112. print(u .. " connected with id ".. id)
  113. clients[id] = {user=u}
  114. return true
  115. else
  116. print("Attempt to log into " .. u .. " failed!")
  117. return false
  118. end
  119. end
  120.  
  121. function host()
  122. while true do
  123. local clientID,msg = rednet.receive()
  124. print(clientID," ",msg)
  125. if msg:sub(1,7) == "CONNECT" then
  126. if auth(clientID,msg:sub(9)) then
  127. rednet.send(clientID,"ACCEPT")
  128. else
  129. rednet.send(clientID,"DENY")
  130. end
  131. elseif msg:sub(1,3) == "GET" then
  132. get(clientID,msg:sub(5))
  133. elseif msg:sub(1,3) == "PUT" then
  134. put(clientID,msg:sub(5))
  135. elseif msg:sub(1,4) == "QUIT" then
  136. clients[clientID] = nil
  137. end
  138. end
  139. end
  140.  
  141. function client()
  142. while true do
  143. local serverID,msg = rednet.receive()
  144.  
  145. if msg == "ACCEPT" then
  146. print("Connection accepted.")
  147. elseif msg == "DENY" then
  148. print("Permission denied.")
  149. elseif msg:sub(1,4) == "DATA" then
  150. print(msg:sub(5))
  151. end
  152. end
  153. end
  154.  
  155. function readFile(name)
  156. local f = fs.open(name,"r")
  157. local r = f.readAll()
  158. f.close()
  159. return r
  160. end
  161.  
  162. function toCmd(line)
  163. if string.lower(line:sub(1,3)) == "get" then
  164. return "GET " .. line:sub(5)
  165. elseif string.lower(line:sub(1,3)) == "put" then
  166. return "PUT " .. line:sub(5) .. readFile(line:sub(5))
  167. end
  168. end
  169.  
  170. function input()
  171. while true do
  172. local line = io.read()
  173. rednet.send(server,toCmd(line))
  174. end
  175. end
  176.  
  177.  
  178. if #args >= 1 then
  179.  
  180. if args[1] == "host" then
  181. local f= fs.open("users","r")
  182. users = textutils.unserialize(f.readAll())
  183. f.close()
  184.  
  185. f = fs.open("perms","r")
  186. perms = textutils.unserialize(f.readAll())
  187. f.close()
  188. host()
  189. else
  190. local id = tonumber(args[1])
  191.  
  192. io.write("Username: ")
  193. local user = read()
  194. io.write("Password: ")
  195. local pass = read("*")
  196. rednet.send(id,"CONNECT "..user..":"..pass)
  197. parallel.waitForAny(client,input)
  198. end
  199. else
  200. print("Usage: ftp host|<id>")
  201. end
  202.  
  203. rednet.close(rednetSide)
Advertisement
Add Comment
Please, Sign In to add comment