Stary2001

Untitled

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