Stary2001

Untitled

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