lcmlog-server 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. DIR="/usr/local/src/lcm-unimi/lcmlog"
  3. import os
  4. import sys
  5. import pwd
  6. import logging
  7. import logging.handlers
  8. def main():
  9. # Preparing the logger
  10. logger = logging.getLogger(__name__)
  11. file_formatter = logging.Formatter("%(asctime)s | %(levelname)8s | %(message)s")
  12. logger.setLevel(logging.INFO)
  13. # Logger handle to log all info
  14. file_handler = logging.handlers.TimedRotatingFileHandler(filename = DIR + "/logs/logfile", when = "W6", backupCount = 10)
  15. file_handler.setFormatter(file_formatter)
  16. logger.addHandler(file_handler)
  17. user_id = os.geteuid()
  18. user_name = pwd.getpwuid(user_id).pw_name
  19. logger.info("Started by user " + user_name + " (id " + str(user_id) + ")")
  20. try:
  21. method = input() # Can be GET or POST
  22. logger.info("Method: " + method)
  23. kind = input() # Can be 150 or Admin
  24. logger.info("Kind: " + kind)
  25. date = input()
  26. logger.info("Date: " + date)
  27. if kind != "150" and kind != "Admin":
  28. raise KindError
  29. if method == "POST":
  30. auth(user_id, "POST", kind)
  31. tags = input()
  32. logger.info("Tags: " + tags)
  33. log = sys.stdin.read() # Read the log content
  34. post(user_name, kind, date, tags, log)
  35. elif method == "GET":
  36. auth(user_id, "GET", kind)
  37. get(kind, date)
  38. else:
  39. raise MethodError
  40. except EOFError as error:
  41. logger.critical("1 Not enough input lines")
  42. sys.exit(1)
  43. except FileNotFoundError as error:
  44. logger.critical("2 File not found")
  45. sys.exit(2)
  46. except FileExistsError as error:
  47. logger.critical("3 File already exists")
  48. sys.exit(3)
  49. except OSError as error:
  50. logger.critical("4 File error")
  51. sys.exit(4)
  52. except MethodError as error:
  53. logger.critical("5 Undefined method")
  54. sys.exit(5)
  55. except KindError as error:
  56. logger.critical("6 Undefined log kind")
  57. sys.exit(6)
  58. except AuthError as error:
  59. logger.critical("7 Authentication error")
  60. sys.exit(7)
  61. except Exception as error:
  62. logger.critical("8 Generic error: " + str(error))
  63. sys.exit(8)
  64. finally:
  65. logger.info("End\n")
  66. # Print specified log on stdout
  67. def get(kind, date):
  68. with open(DIR + "/data/" + kind + "/" + date, "r") as f:
  69. sys.stdout.write("User: " + f.readline())
  70. sys.stdout.write("Tags: " + f.readline())
  71. sys.stdout.write("\n" + f.read())
  72. # Write log
  73. def post(user_name, kind, date, tags, log):
  74. with open(DIR + "/data/" + kind + "/" + date, "x") as f:
  75. f.write(user_name + "\n" + tags + "\n" + log)
  76. # Checks if the user has the permissions to use the requested method
  77. def auth(user_id, method, kind):
  78. with open(DIR + "/auth/" + kind + "/" + method) as f:
  79. for line in f:
  80. if int(line) == user_id:
  81. return
  82. raise AuthError()
  83. # Error definitions
  84. class AuthError(Exception):
  85. pass
  86. class KindError(Exception):
  87. pass
  88. class MethodError(Exception):
  89. pass
  90. # Starting point
  91. if __name__ == "__main__":
  92. main()