Stary2001

Untitled

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