Browse Source

Implement basic config system.

Félix Baylac-Jacqué 6 years ago
parent
commit
530c585de9
3 changed files with 47 additions and 23 deletions
  1. 6 0
      todo.txt
  2. 8 0
      wg-create.ini.sample
  3. 33 23
      wireguardCreate.py

+ 6 - 0
todo.txt

@@ -0,0 +1,6 @@
+TODO
+
+- Review config parser.
+- Implement tinfoil hat mode.
+- Write proper makefile to distribute.
+- Expose config via CLI flags.

+ 8 - 0
wg-create.ini.sample

@@ -0,0 +1,8 @@
+[smtp]
+username = baionet
+password = pass
+server = smtp.example.org
+from = from@example.org
+
+[wireguard-service]
+name = wg-quick@wg0.service

+ 33 - 23
wireguardCreate.py

@@ -1,5 +1,6 @@
 #!/usr/bin/env python3
 
+import configparser
 import os
 import smtplib
 import subprocess
@@ -10,6 +11,32 @@ from email.mime.multipart   import MIMEMultipart
 from email.mime.application import MIMEApplication 
 from email.mime.text        import MIMEText
 
+class Email:
+    """
+    Not really necessary, but I keep on forgetting most of an email arguments
+    and I'm tired of debugging this class of error... Too bad we don't have any
+    record type in this language.
+
+    PS: I hate python.
+    """
+    def __init__(self, username, passwd, from_addr, to_addr, server):
+        self.username  = username
+        self.passwd    = passwd
+        self.from_addr = from_addr
+        self.to_addr   = to_addr
+        self.server    = server
+
+class Config:
+    """
+    Same as Email
+    """
+    def __init__(self, config):
+        self.smtp_user = config['smtp']['username']
+        self.smtp_pass = config['smtp']['password']
+        self.smtp_server = config['smtp']['server']
+        self.smtp_from = config['smtp']['from']
+        self.wg_service = config['wireguard-service']['name']
+
 def _run_cmd(cmd):
     print("$ %s" % (cmd))
     subprocess.run(cmd, shell=True, check=True)
@@ -75,20 +102,6 @@ def update_wg_config (member_id, config_file, pubkey_path, psk_path):
     _run_cmd('sed -i "s/%PUBKEY%/$(cat %s)/" "%s"' % (pubkey_path, config_file))
     _run_cmd('sed -i "s/%PSK%/$(cat %s)/" "%s"' % (psk_path, config_file))
 
-class Email:
-    """
-    Not really necessary, but I keep on forgetting most of an email arguments
-    and I'm tired of debugging this class of error... Too bad we don't have any
-    record type in this language.
-
-    PS: I hate python.
-    """
-    def __init__(self, username, passwd, from_addr, to_addr, server):
-        self.username  = username
-        self.passwd    = passwd
-        self.from_addr = from_addr
-        self.to_addr   = to_addr
-        self.server    = server
 
 def send_mail(email, wgconfig_path):
     """
@@ -135,6 +148,9 @@ def send_mail(email, wgconfig_path):
 # 3- Crée/déploie la configuration wireguard.
 # 5- Envoie la clé à l'utilisateur (email/manuellement)
 if __name__ == '__main__':
+    cp = configparser.ConfigParser()
+    cp.read('/etc/wireguard/wg-create.ini')
+    config = Config(cp)
     member_email = input("EMail du nouveau membre: ")
     try:
         member_id = int(input("Numéro d'adhérant du nouveau membre: "))
@@ -142,8 +158,6 @@ if __name__ == '__main__':
         print("ERREUR: Le numéro d'adhérant est en théorie un entier entre 1 et 253.")
         sys.exit(1)
     try:
-        service_name  = os.environ['SYSTEMD_SERVICE']
-        wg_config_dir = os.environ['WG_CONF_PATH']
         check_env(member_id)
     except Exception as e:
         print("ERREUR: problème d'environnement: {}".format(e))
@@ -166,19 +180,15 @@ if __name__ == '__main__':
             sys.exit(1)
         print("[+] Chargement de la nouvelle interface réseau")
         try:
-            _run_cmd("systemctl restart %s" % (service_name)) 
+            _run_cmd("systemctl restart %s" % (config.wg_service)) 
         except Exception as e:
             print("ERREUR: Problème lors du redémarrage du service.")
             print(e)
         print("[+] Envoi de la clé privée au nouveau membre")
         try:
             if use_email:
-                username = os.environ['SMTP_USERNAME']
-                passwd   = os.environ['SMTP_PASSWD']
-                server   = os.environ['SMTP_SERVER']
-                server   = os.environ['SYSTEMD_SERVICE']
-                server   = os.environ['SYSTEMD_SERVICE']
-                email    = Email(username, passwd, "bureau@baionet.fr", member_email, server)
+                email = Email(config.smtp_user, config.smtp_pass, config.smtp_from,\
+                              member_email, config.smtp_server)
                 send_email(email, privkey_path)
             else:
                 print("Mode utilisateur avancé")