11-- Warning, given a bug in this program, the client could potentially access files outside the folder
22-- Best to chroot/limit permissions for this server
3- local server , curclient , totalspace , curspace , port , label , change , debug , recurseCount
4- local sockets , hndls = {}, {}
53
64-- Configuration
7- totalspace = math.huge
8- curspace = 0
9- port = 14948
10- label = " netfs"
11- change = false
12- debug = false
5+ local totalspace = math.huge
6+ local curspace = 0
7+ local port = 14948
8+ local label = " netfs"
9+ local change = false
10+ local debug = false
11+ -- End of Configuration
1312
1413local socket = require (" socket" )
1514local lfs = require (" lfs" )
@@ -49,6 +48,7 @@ local function getDirectoryItems(path)
4948 return output
5049end
5150
51+ local recurseCount
5252function recurseCount (path )
5353 local count = 0
5454 local list = getDirectoryItems (path )
@@ -61,14 +61,12 @@ function recurseCount(path)
6161 print (path .. " /" .. list [i ])
6262 print (lfs .attributes (path .. " /" .. list [i ]," mode" ))
6363 end
64- count = count + (lfs . attributes ( path .. " / " .. list [ i ], " size" ) or 0 )
64+ count = count + (size or 0 )
6565 end
6666 end
6767 return count
6868end
6969
70- local arg = { ... }
71-
7270print (" Warning, I take no responsibility if a bug in this program eats your computer\n It's your fault for running it under such a permission\n Though, bug reports and fixes are welcomed ;)\n " )
7371
7472if change then
7876print (" Calculating current space usage ..." )
7977curspace = recurseCount (sanitizePath (" /" ))
8078
81- local stat
82- stat , server = pcall (assert ,socket .bind (" *" , port ))
79+ local stat , server = pcall (assert ,socket .bind (" *" , port ))
8380if not stat then
8481 print (" Failed to get default port " .. port .. " : " .. server )
8582 server = assert (socket .bind (" *" , 0 ))
8683end
8784local sID , sPort = server :getsockname ()
8885server :settimeout (0 )
89- sockets [1 ] = server
9086
9187print (" Listening on " .. sID .. " :" .. sPort )
9288
@@ -155,6 +151,7 @@ function unserialize(str)
155151 end
156152end
157153
154+ local curclient
158155local function sendData (msg )
159156 if debug then
160157 local ip ,port = curclient :getpeername ()
@@ -171,9 +168,22 @@ local function checkArg(pos,obj,what)
171168 return true
172169end
173170
171+ local function dprint (ctrl , line )
172+ print (" > " .. ctrl .. " ," .. line :gsub (" [^\32 -\126 ]" , function (a ) return " \\ " .. a :byte () end ))
173+ end
174+
175+ -- do not change order
176+ local ops = {" size" ," seek" ," read" ," isDirectory" ," open" ," spaceTotal" ," setLabel" ," lastModified" ," close" ," rename" ," isReadOnly" ," exists" ," getLabel" ," spaceUsed" ," makeDirectory" ," list" ," write" ," remove" }
177+
178+ local sockets = {server }
179+ local hndls = {}
174180local function update ()
175181 -- Check for new data or new clients
176- local ready , err , err2 = socket .select (sockets ,nil ) or {}
182+ local ready , _ , err = socket .select (sockets ,nil )
183+ if not ready then
184+ print (" select gave " .. tostring (err ))
185+ return
186+ end
177187 for _ , client in ipairs (ready ) do
178188 if client == server then
179189 client = server :accept ()
@@ -198,34 +208,36 @@ local function update()
198208 break
199209 end
200210 end
201- return
211+ break
202212 end
203213 local ctrl = line :byte (1 ,1 ) - 31
214+ ctrl = ops [ctrl ] or ctrl
215+ local line = line :sub (2 )
204216 if debug then
205- print ( " > " .. ctrl .. " , " .. line : sub ( 2 ) )
217+ dprint ( ctrl , line )
206218 end
207- local stat ,ret = pcall (unserialize ,line : sub ( 2 ) )
219+ local stat ,ret = pcall (unserialize , line )
208220 if not stat then
209221 if not debug then
210- print ( " > " .. ctrl .. " , " .. line : sub ( 2 ) )
222+ dprint ( ctrl , line )
211223 end
212224 print (" Bad Input: " .. ret )
213225 sendData (" {nil,\" bad input\" }" )
214226 return
215227 end
216228 if type (ret ) ~= " table" then
217229 if not debug then
218- print ( " > " .. ctrl .. " , " .. line : sub ( 2 ) )
230+ dprint ( ctrl , line )
219231 end
220232 print (" Bad Input (exec): " .. type (ret ))
221233 sendData (" {nil,\" bad input\" }" )
222234 return
223235 end
224- if ctrl == 1 then -- size
236+ if ctrl == " size " then
225237 if not checkArg (1 ,ret [1 ]," string" ) then return end
226238 local size = lfs .attributes (sanitizePath (ret [1 ])," size" )
227239 sendData (" {" .. (size or 0 ) .. " }" )
228- elseif ctrl == 2 then -- seek
240+ elseif ctrl == " seek " then
229241 if not checkArg (1 ,ret [1 ]," number" ) then return end
230242 if not checkArg (2 ,ret [2 ]," string" ) then return end
231243 if not checkArg (3 ,ret [3 ]," number" ) then return end
@@ -236,7 +248,7 @@ local function update()
236248 local new = hndls [fd ]:seek (ret [2 ],ret [3 ])
237249 sendData (" {" .. new .. " }" )
238250 end
239- elseif ctrl == 3 then -- read
251+ elseif ctrl == " read " then
240252 if not checkArg (1 ,ret [1 ]," number" ) then return end
241253 if not checkArg (2 ,ret [2 ]," number" ) then return end
242254 local fd = ret [1 ]
@@ -250,10 +262,10 @@ local function update()
250262 sendData (" {nil}" )
251263 end
252264 end
253- elseif ctrl == 4 then -- isDirectory
265+ elseif ctrl == " isDirectory " then
254266 if not checkArg (1 ,ret [1 ]," string" ) then return end
255267 sendData (" {" .. tostring (lfs .attributes (sanitizePath (ret [1 ])," mode" ) == " directory" ) .. " }" )
256- elseif ctrl == 5 then -- open
268+ elseif ctrl == " open " then
257269 if not checkArg (1 ,ret [1 ]," string" ) then return end
258270 if not checkArg (2 ,ret [2 ]," string" ) then return end
259271 local mode = ret [2 ]:sub (1 ,1 )
@@ -275,21 +287,21 @@ local function update()
275287 sendData (" {" .. randhand .. " }" )
276288 end
277289 end
278- elseif ctrl == 6 then -- spaceTotal
290+ elseif ctrl == " spaceTotal " then
279291 sendData (" {" .. tostring (totalspace ) .. " }" )
280- elseif ctrl == 7 then -- setLabel
292+ elseif ctrl == " setLabel " then
281293 if not checkArg (1 ,ret [1 ]," string" ) then return end
282294 if change then
283295 label = ret [1 ]
284296 sendData (" {\" " .. label .. " \" }" )
285297 else
286298 sendData (" label is read only" )
287299 end
288- elseif ctrl == 8 then -- lastModified
300+ elseif ctrl == " lastModified " then
289301 if not checkArg (1 ,ret [1 ]," string" ) then return end
290302 local modtime = lfs .attributes (sanitizePath (ret [1 ])," modification" )
291303 sendData (" {" .. (modtime or 0 ) .. " }" )
292- elseif ctrl == 9 then -- close
304+ elseif ctrl == " close " then
293305 if not checkArg (1 ,ret [1 ]," number" ) then return end
294306 local fd = ret [1 ]
295307 if hndls [fd ] == nil then
@@ -299,32 +311,32 @@ local function update()
299311 hndls [fd ] = nil
300312 sendData (" {}" )
301313 end
302- elseif ctrl == 10 then -- rename
314+ elseif ctrl == " rename " then
303315 if not checkArg (1 ,ret [1 ]," string" ) then return end
304316 if not checkArg (2 ,ret [2 ]," string" ) then return end
305317 if change then
306318 sendData (" {" .. tostring (os.rename (sanitizePath (ret [1 ]),sanitizePath (ret [2 ])) == true ) .. " }" )
307319 else
308320 sendData (" {false}" )
309321 end
310- elseif ctrl == 11 then -- isReadOnly
322+ elseif ctrl == " isReadOnly " then
311323 sendData (" {" .. tostring (not change ) .. " }" )
312- elseif ctrl == 12 then -- exists
324+ elseif ctrl == " exists " then
313325 if not checkArg (1 ,ret [1 ]," string" ) then return end
314326 sendData (" {" .. tostring (lfs .attributes (sanitizePath (ret [1 ])," mode" ) ~= nil ) .. " }" )
315- elseif ctrl == 13 then -- getLabel
327+ elseif ctrl == " getLabel " then
316328 sendData (" {\" " .. label .. " \" }" )
317- elseif ctrl == 14 then -- spaceUsed
329+ elseif ctrl == " spaceUsed " then
318330 -- TODO: Need to update this
319331 sendData (" {" .. curspace .. " }" )
320- elseif ctrl == 15 then -- makeDirectory
332+ elseif ctrl == " makeDirectory " then
321333 if not checkArg (1 ,ret [1 ]," string" ) then return end
322334 if change then
323335 sendData (" {" .. tostring (lfs .mkdir (sanitizePath (ret [1 ]))) .. " }" )
324336 else
325337 sendData (" {false}" )
326338 end
327- elseif ctrl == 16 then -- list
339+ elseif ctrl == " list " then
328340 if not checkArg (1 ,ret [1 ]," string" ) then return end
329341 ret [1 ] = sanitizePath (ret [1 ])
330342 local list = getDirectoryItems (ret [1 ])
@@ -339,7 +351,7 @@ local function update()
339351 end
340352 end
341353 sendData (" {{" .. out .. " }}" )
342- elseif ctrl == 17 then -- write
354+ elseif ctrl == " write " then
343355 if not checkArg (1 ,ret [1 ]," number" ) then return end
344356 if not checkArg (2 ,ret [2 ]," string" ) then return end
345357 local fd = ret [1 ]
@@ -349,7 +361,7 @@ local function update()
349361 local success = hndls [fd ]:write (ret [2 ])
350362 sendData (" {" .. tostring (success ) .. " }" )
351363 end
352- elseif ctrl == 18 then -- remove
364+ elseif ctrl == " remove " then
353365 -- TODO: Recursive remove
354366 if not checkArg (1 ,ret [1 ]," string" ) then return end
355367 if change then
0 commit comments