|
@@ -0,0 +1,109 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+
|
|
|
+DIR="/usr/local/src/lcm-unimi/lcmlog"
|
|
|
+
|
|
|
+import os
|
|
|
+import sys
|
|
|
+import pwd
|
|
|
+import logging
|
|
|
+import logging.handlers
|
|
|
+
|
|
|
+def main():
|
|
|
+
|
|
|
+ # Preparing the logger
|
|
|
+ logger = logging.getLogger(__name__)
|
|
|
+ file_formatter = logging.Formatter("%(asctime)s | %(levelname)8s | %(message)s")
|
|
|
+ logger.setLevel(logging.INFO)
|
|
|
+
|
|
|
+ # Logger handle to log all info
|
|
|
+ file_handler = logging.handlers.TimedRotatingFileHandler(filename = DIR + "/logs/logfile", when = "W6", backupCount = 10)
|
|
|
+ file_handler.setFormatter(file_formatter)
|
|
|
+
|
|
|
+ logger.addHandler(file_handler)
|
|
|
+
|
|
|
+ user_id = os.geteuid()
|
|
|
+ user_name = pwd.getpwuid(user_id).pw_name
|
|
|
+
|
|
|
+ logger.info("Started by user " + user_name + " (id " + str(user_id) + ")")
|
|
|
+
|
|
|
+ try:
|
|
|
+ method = input() # Can be GET or POST
|
|
|
+ logger.info("Method: " + method)
|
|
|
+ kind = input() # Can be 150 or Admin
|
|
|
+ logger.info("Kind: " + kind)
|
|
|
+ date = input()
|
|
|
+ logger.info("Date: " + date)
|
|
|
+
|
|
|
+ if kind != "150" and kind != "Admin":
|
|
|
+ raise KindError
|
|
|
+ if method == "POST":
|
|
|
+ auth(user_id, "POST", kind)
|
|
|
+ tags = input()
|
|
|
+ logger.info("Tags: " + tags)
|
|
|
+ log = sys.stdin.read() # Read the log content
|
|
|
+ post(user_name, kind, date, tags, log)
|
|
|
+ elif method == "GET":
|
|
|
+ auth(user_id, "GET", kind)
|
|
|
+ get(kind, date)
|
|
|
+ else:
|
|
|
+ raise MethodError
|
|
|
+
|
|
|
+ except EOFError as error:
|
|
|
+ logger.critical("1 Not enough input lines")
|
|
|
+ sys.exit(1)
|
|
|
+ except FileNotFoundError as error:
|
|
|
+ logger.critical("2 File not found")
|
|
|
+ sys.exit(2)
|
|
|
+ except FileExistsError as error:
|
|
|
+ logger.critical("3 File already exists")
|
|
|
+ sys.exit(3)
|
|
|
+ except OSError as error:
|
|
|
+ logger.critical("4 File error")
|
|
|
+ sys.exit(4)
|
|
|
+ except MethodError as error:
|
|
|
+ logger.critical("5 Undefined method")
|
|
|
+ sys.exit(5)
|
|
|
+ except KindError as error:
|
|
|
+ logger.critical("6 Undefined log kind")
|
|
|
+ sys.exit(6)
|
|
|
+ except AuthError as error:
|
|
|
+ logger.critical("7 Authentication error")
|
|
|
+ sys.exit(7)
|
|
|
+ except Exception as error:
|
|
|
+ logger.critical("8 Generic error: " + str(error))
|
|
|
+ sys.exit(8)
|
|
|
+
|
|
|
+ finally:
|
|
|
+ logger.info("End\n")
|
|
|
+
|
|
|
+# Print specified log on stdout
|
|
|
+def get(kind, date):
|
|
|
+ with open(DIR + "/data/" + kind + "/" + date, "r") as f:
|
|
|
+ sys.stdout.write("User: " + f.readline())
|
|
|
+ sys.stdout.write("Tags: " + f.readline())
|
|
|
+ sys.stdout.write("\n" + f.read())
|
|
|
+
|
|
|
+# Write log
|
|
|
+def post(user_name, kind, date, tags, log):
|
|
|
+ with open(DIR + "/data/" + kind + "/" + date, "x") as f:
|
|
|
+ f.write(user_name + "\n" + tags + "\n" + log)
|
|
|
+
|
|
|
+# Checks if the user has the permissions to use the requested method
|
|
|
+def auth(user_id, method, kind):
|
|
|
+ with open(DIR + "/auth/" + kind + "/" + method) as f:
|
|
|
+ for line in f:
|
|
|
+ if int(line) == user_id:
|
|
|
+ return
|
|
|
+ raise AuthError()
|
|
|
+
|
|
|
+# Error definitions
|
|
|
+class AuthError(Exception):
|
|
|
+ pass
|
|
|
+class KindError(Exception):
|
|
|
+ pass
|
|
|
+class MethodError(Exception):
|
|
|
+ pass
|
|
|
+
|
|
|
+# Starting point
|
|
|
+if __name__ == "__main__":
|
|
|
+ main()
|