14 # ToDo replace with ikarus
21 MINOPTS = { "Main" : ["Logfile", "Mountpoint", "Rootpath", "LocalScanserverURL", "RemoteScanserverURL", "ReadOnly"]}
23 CONFIG_NOT_READABLE = "Configfile is not readable"
24 CONFIG_WRONG = "Something is wrong with the config"
25 CONFIG_MISSING = "Section: \"%s\" Option: \"%s\" in configfile is missing"
27 LOCAL_SCANSERVER_URL = ""
28 REMOTE_SCANSERVER_URL = ""
30 STATUS_CODE_INFECTED = 210
31 STATUS_CODE_NOT_FOUND = 404
33 SYSTEM_FILE_COMMAND = "file"
36 def checkMinimumOptions (config):
37 for section, options in MINOPTS.iteritems ():
38 for option in options:
39 if (config.has_option(section, option) == False):
40 print (CONFIG_MISSING % (section, option))
45 print ("%s configfile mountpath ro/rw" % (sys.argv[0]))
51 if (len (sys.argv) < 4):
54 configfile = sys.argv[1]
55 config = ConfigParser.SafeConfigParser ()
57 if ((os.path.exists (configfile) == False) or (os.path.isfile (configfile) == False) or (os.access (configfile, os.R_OK) == False)):
58 print (CONFIG_NOT_READABLE)
62 config.read (sys.argv[1])
65 print ("Error: %s" % (e))
68 config.set("Main", "Mountpoint", sys.argv[2])
69 if (sys.argv[3] == "rw"):
70 config.set("Main", "ReadOnly", "false")
72 config.set("Main", "ReadOnly", "true")
74 checkMinimumOptions (config)
82 logfile = config.get("Main", "Logfile")
84 # ToDo move log level and maybe other things to config file
86 level = logging.DEBUG,
87 format = "%(asctime)s %(name)-12s %(funcName)-15s %(levelname)-8s %(message)s",
88 datefmt = "%Y-%m-%d %H:%M:%S",
92 LOG = logging.getLogger("fuse_main")
98 def rootPath (rootpath, path):
99 return "%s%s" % (rootpath, path)
101 def flag2mode (flags):
102 md = {os.O_RDONLY: 'r', os.O_WRONLY: 'w', os.O_RDWR: 'w+'}
103 m = md[flags & (os.O_RDONLY | os.O_WRONLY | os.O_RDWR)]
105 if flags | os.O_APPEND:
106 m = m.replace('w', 'a', 1)
110 def scanFileIkarus (path, fileobject):
112 LOG.debug ("Scan File: %s" % (path))
114 files = {'up_file': (path, fileobject)}
117 #TODO: change to remote server
118 r = requests.post(LOCAL_SCANSERVER_URL, files=files)
119 except requests.exceptions.ConnectionError:
120 #LOG.info("Remote scan server unreachable, using local scan server.")
123 # Here the local scan server should be contacted.
124 # The requests package does not upload content in the second post request,
125 # so no fallback server can be used right now (bug?)
126 # I did not a find a solution yet, maybe another http package has to be used.
130 # r = requests.post(LOCAL_SCANSERVER_URL, files=files)
131 #except requests.exceptions.ConnectionError:
133 LOG.error ("Connection to scan server could not be established.")
136 if r.status_code == STATUS_CODE_OK:
138 elif r.status_code == STATUS_CODE_INFECTED:
139 # Parse xml for info if desired
140 #contentXML = r.content
141 #root = ET.fromstring(contentXML)
142 #status = root[1][2].text
145 LOG.error ("Connection error to scan server.")
147 if (infected == True):
148 LOG.error ("Virus found, denying access.")
150 LOG.debug ("No virus found.")
154 def scanFileClamAV (path):
157 LOG.debug ("Scan File: %s" % (path))
159 # ToDo implement ikarus
160 result = pyclamav.scanfile (path)
161 LOG.debug ("Result of file \"%s\": %s" % (path, result))
165 if (infected == True):
166 LOG.error ("Virus found, deny Access %s" % (result,))
170 def whitelistFile (path):
173 LOG.debug ("Execute \"%s\" command on \"%s\"" %(SYSTEM_FILE_COMMAND, path))
177 result = subprocess.check_output ([SYSTEM_FILE_COMMAND, path]);
178 # ToDo replace with real whitelist
180 except Exception as e:
181 LOG.error ("Call returns with an error!")
184 LOG.debug ("Type: %s" %(result))
193 def __init__(self, rootpath, *args, **kw):
194 self.__rootpath = rootpath
195 Fuse.__init__ (self, *args, **kw)
196 LOG.debug ("Init complete.")
198 # defines that our working directory will be the __rootpath
200 os.chdir (self.__rootpath)
202 def getattr(self, path):
203 LOG.debug ("*** getattr (%s)" % (fixPath (path)))
204 return os.lstat (fixPath (path));
206 def getdir(self, path):
207 LOG.debug ("*** getdir (%s)" % (path));
208 return os.listdir (fixPath (path))
210 def readdir(self, path, offset):
211 LOG.debug ("*** readdir (%s %s)" % (path, offset));
212 for e in os.listdir (fixPath (path)):
213 yield fuse.Direntry(e)
215 def chmod (self, path, mode):
216 LOG.debug ("*** chmod %s %s" % (path, oct(mode)))
217 if (config.get("Main", "ReadOnly") == "true"):
219 os.chmod (fixPath (path), mode)
221 def chown (self, path, uid, gid):
222 LOG.debug ("*** chown %s %s %s" % (path, uid, gid))
223 if (config.get("Main", "ReadOnly") == "true"):
225 os.chown (fixPath (path), uid, gid)
227 def link (self, targetPath, linkPath):
228 LOG.debug ("*** link %s %s" % (targetPath, linkPath))
229 if (config.get("Main", "ReadOnly") == "true"):
231 os.link (fixPath (targetPath), fixPath (linkPath))
233 def mkdir (self, path, mode):
234 LOG.debug ("*** mkdir %s %s" % (path, oct(mode)))
235 if (config.get("Main", "ReadOnly") == "true"):
237 os.mkdir (fixPath (path), mode)
239 def mknod (self, path, mode, dev):
240 LOG.debug ("*** mknod %s %s %s" % (path, oct (mode), dev))
241 if (config.get("Main", "ReadOnly") == "true"):
243 os.mknod (fixPath (path), mode, dev)
245 # to implement virus scan
246 def open (self, path, flags):
247 LOG.debug ("*** open %s %s" % (path, oct (flags)))
248 self.file = os.fdopen (os.open (fixPath (path), flags), flag2mode (flags))
249 self.fd = self.file.fileno ()
251 infected = scanFileIkarus (rootPath(self.__rootpath, path), self.file)
252 #infected = scanFileClamAV (rootPath(self.__rootpath, path))
253 if (infected == True):
257 whitelisted = whitelistFile (rootPath(self.__rootpath, path))
258 if (whitelisted == False):
262 def read (self, path, length, offset):
263 LOG.debug ("*** read %s %s %s" % (path, length, offset))
264 self.file.seek (offset)
265 return self.file.read (length)
267 def readlink (self, path):
268 LOG.debug ("*** readlink %s" % (path))
269 return os.readlink (fixPath (path))
271 def release (self, path, flags):
272 LOG.debug ("*** release %s %s" % (path, oct (flags)))
275 def rename (self, oldPath, newPath):
276 LOG.debug ("*** rename %s %s %s" % (oldPath, newPath, config.get("Main", "ReadOnly")))
277 if (config.get("Main", "ReadOnly") == "true"):
279 os.rename (fixPath (oldPath), fixPath (newPath))
281 def rmdir (self, path):
282 LOG.debug ("*** rmdir %s %s" % (path, config.get("Main", "ReadOnly")))
283 if (config.get("Main", "ReadOnly") == "true"):
285 os.rmdir (fixPath (path))
288 LOG.debug ("*** statfs")
289 return os.statvfs(".")
291 def symlink (self, targetPath, linkPath):
292 LOG.debug ("*** symlink %s %s %s" % (targetPath, linkPath, config.get("Main", "ReadOnly")))
293 if (config.get("Main", "ReadOnly") == "true"):
295 os.symlink (fixPath (targetPath), fixPath (linkPath))
297 def truncate (self, path, length):
298 LOG.debug ("*** truncate %s %s %s" % (path, length, config.get("Main", "ReadOnly")))
299 if (config.get("Main", "ReadOnly") == "true"):
301 f = open (fixPath (path), "a")
305 def unlink (self, path):
306 LOG.debug ("*** unlink %s %s" % (path, config.get("Main", "ReadOnly")))
307 if (config.get("Main", "ReadOnly") == "true"):
309 os.unlink (fixPath (path))
311 def utime (self, path, times):
312 LOG.debug ("*** utime %s %s" % (path, times))
313 os.utime (fixPath (path), times)
315 def write (self, path, buf, offset):
316 LOG.debug ("*** write %s %s %s %s" % (path, buf, offset, config.get("Main", "ReadOnly")))
317 if (config.get("Main", "ReadOnly") == "true"):
320 self.file.seek (offset)
321 self.file.write (buf)
324 def access (self, path, mode):
325 LOG.debug ("*** access %s %s" % (path, oct (mode)))
326 if not os.access (fixPath (path), mode):
329 def create (self, path, flags, mode):
330 LOG.debug ("*** create %s %s %s %s %s" % (fixPath (path), oct (flags), oct (mode), flag2mode (flags), config.get("Main", "ReadOnly")))
331 if (config.get("Main", "ReadOnly") == "true"):
333 self.file = os.fdopen (os.open (fixPath (path), flags, mode), flag2mode (flags))
334 self.fd = self.file.fileno ()
337 if __name__ == "__main__":
339 fuse.fuse_python_api = (0, 2)
340 fuse.feature_assert ('stateful_files', 'has_init')
342 config = loadConfig ()
345 LOCAL_SCANSERVER_URL = config.get("Main", "LocalScanserverURL")
346 REMOTE_SCANSERVER_URL = config.get("Main", "RemoteScanserverURL")
348 osecfs = OsecFS (config.get ("Main", "Rootpath"))
350 osecfs.multithreaded = 0
352 # osecfs.parser.add_option (mountopt=config.get("Main", "Mountpoint"),
354 # default=config.get("Main", "Rootpath"),
355 # help="mirror filesystem from under PATH [default: %default]")
356 # osecfs.parse(values=osecfs, errex=1)
358 fuse_args = [sys.argv[0], config.get ("Main", "Mountpoint")];
359 osecfs.main (fuse_args)