Maldaris

Untitled

Oct 11th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. --[[
  2. CC-NFS: Computer Craft Networked File System
  3. description - Authentication-based File Server allowing for
  4. storing files across multiple computers from a
  5. single computer, securely.
  6. author - Maldaris
  7. version - v0.5 Beta
  8. ]]
  9.  
  10.  
  11. os.loadAPI('./lib/persistence');
  12.  
  13. local directoryFormattedPaths = function(parent, children)
  14. local ret = {}
  15. for k,v in children do
  16. if fs.isDir(parent .. k) then
  17. ret[k] = v .. '/';
  18. end
  19. end
  20. return ret;
  21. end
  22.  
  23. nfs = {
  24. timeout = 300, --Reauth Required every 5 Minutes
  25. sendAs = os.getComputerID(),
  26. registeredUsers = {},
  27. userSessions = {},
  28. }
  29.  
  30. nfs.__index = nfs;
  31.  
  32. function nfs:createUserSession(user)
  33. if(self.registeredUsers[user] == nil) then
  34. return nil
  35. end
  36. local session = {};
  37. session.crypt = {};
  38. --TODO: load crypto here
  39.  
  40. session.lastRecieved = os.clock();
  41. return session;
  42. end -- function
  43.  
  44. function nfs:auth(user, encoded)
  45. local challenge = "Hello World!";
  46. self.userSessions[user] = createUserSession(user);
  47. if self.userSessions[user] == nil then
  48. return false
  49. else
  50. local msg = self.userSessions.crypto.dec(encoded);
  51. if msg == challenge then
  52. return true
  53. else
  54. return false
  55. end
  56. end
  57. end -- function
  58.  
  59. function nfs:isAuthenticated(user)
  60. if(userSessions and userSessions[user] ~= nil)
  61. local dt = os.clock();
  62. if(userSessions[user].lastRecieved - dt > self.timeout)
  63. return false
  64. else
  65. return userSessions[user].authenticated
  66. end
  67. else
  68. return false
  69. end
  70. end
  71. --[[
  72.  
  73. ]]
  74. function nfs:list(user, path)
  75. if(self.isAuthenticated(user)) then
  76. --ignore private directories
  77. if path.sub(0,0) == '.' then
  78. return false, 'invalid directory'
  79. end
  80. --check if the path exists
  81. if fs.exists(path) then
  82. local directory = fs.list(path)
  83. directory = directoryFormattedPaths(directory);
  84. return true, directory;
  85. else
  86. return false, 'invalid directory';
  87. end -- exists
  88. else
  89. return false, 'Unauthenticated';
  90. end -- auth
  91. end -- list function
  92. function nfs:get(user, file)
  93. if(self.isAuthenticated(user)) then
  94. if fs.exists(path .. file) then
  95. return true, fs.open(path, 'r').readAll();
  96. else
  97. return false, 'invalid file path';
  98. end
  99. else
  100. return false, 'Unauthenticated';
  101. end
  102. end -- function
  103.  
  104. function nfs:put(user, path, file, data, overwrite)
  105.  
  106. if(self.isAuthenticated(user)) then
  107. if fs.exists(path) == false then
  108. fs.makeDir(path)
  109. end
  110. local flag = '';
  111. if overwrite == true then
  112. flag = 'w';
  113. else
  114. flag = 'a';
  115. end
  116. local file = fs.open(path .. file, flag);
  117. file.write(data);
  118. file.close();
  119. return true, nil;
  120. else
  121. return false, 'Unauthenticated';
  122. end
  123. end
  124.  
  125. function nfs:setup(path, hostname)
  126. if fs.exists('./.&/users.tbl') then
  127. self.registeredUsers = persistence.load('./.&/users.tbl');
  128. else
  129. return nil;
  130. end
  131. if( fs.isDir(path) == false ) then
  132. return nil;
  133. else
  134. self.path = path;
  135. shell.path(self.path); -- set cwd
  136. end
  137. --last step, register the hostname
  138. self.hostname = hostname;
  139. rednet.host('nfs', hostname);
  140. self.setup = true;
  141. return self;
  142. end -- function
  143.  
  144. function nfs:teardown():
  145. rednet.unhost('nfs', self.hostname);
  146. end
  147.  
  148. function nfs:run()
  149. if(self.setup ~= true)
  150. return nil;
  151. else
  152. -- begin the event loop
  153. while true do
  154. id , msg = rednet.receive('nfs');
  155. if type(msg) == 'table' then
  156. local user = msg.user;
  157. local op = msg.op;
  158. if op == 'auth' then
  159. -- do auth
  160. local challenge = msg.challenge;
  161. rednet.send(id, self.auth(user, challenge), 'nfs');
  162. else if op == 'list' then
  163. -- do list
  164. local dir = msg.path;
  165. rednet.send(id, self.list(user, path), 'nfs');
  166. else if op == 'get' then
  167. -- do get
  168. local success, ret = self.get(user, msg.file);
  169. if success then
  170. rednet.send(id, ret, 'nfs');
  171. else
  172. rednet.send(id, 'Error:' .. ret, 'nfs');
  173. else if op == 'put' then
  174. -- do put
  175. local overwrite = msg.overwrite;
  176. local path = msg.path;
  177. local file = msg.file;
  178. local data = msg.data;
  179. local success, res = self.put(user, path, file, data, overwrite);
  180. if success then
  181. rednet.send(id, true, 'nfs');
  182. else
  183. rednet.send(id, 'Error:' .. res, 'nfs');
  184. else
  185. rednet.send(id, 'Malformed Command', 'nfs');
  186. end -- op
  187. end -- typeof
  188. end -- while
  189. end -- setup check
  190. end -- function
Advertisement
Add Comment
Please, Sign In to add comment