Stary2001

Untitled

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