Stary2001

Untitled

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