summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorSam Chudnick <sam@chudnick.com>2022-07-02 15:35:50 -0400
committerSam Chudnick <sam@chudnick.com>2022-07-02 15:35:50 -0400
commit8472b394ee44cd46cc36fd4fe0a4882364cab602 (patch)
tree301fcb5e0becbebff4486b556e561afac61e11ca /client
parent01c24eb1f6f6a54bb780940c7665acd280b42aaf (diff)
Read options from config file
Set a standardized configuration file location and read options from there. Allow for specifiying alternate location on command line. Options can still be specified on the command line, and any command line options take priority over those given in the configuration file.
Diffstat (limited to 'client')
-rwxr-xr-xclient/client.py64
1 files changed, 41 insertions, 23 deletions
diff --git a/client/client.py b/client/client.py
index b2429b6..1c7e155 100755
--- a/client/client.py
+++ b/client/client.py
@@ -4,6 +4,7 @@ import socket
4import time 4import time
5import argparse 5import argparse
6import sys 6import sys
7import os
7 8
8HEADER_LENGTH = 64 9HEADER_LENGTH = 64
9KEY_LENGTH = 64 10KEY_LENGTH = 64
@@ -11,6 +12,7 @@ DISCONNECT_LENGTH = ACK_LENGTH = 3
11ACK_MESSAGE = "ACK" 12ACK_MESSAGE = "ACK"
12DISCONNECT_MESSAGE = "BYE" 13DISCONNECT_MESSAGE = "BYE"
13FORMAT = "utf-8" 14FORMAT = "utf-8"
15import configparser
14 16
15def parse_arguments(): 17def parse_arguments():
16 parser = argparse.ArgumentParser() 18 parser = argparse.ArgumentParser()
@@ -18,7 +20,7 @@ def parse_arguments():
18 parser.add_argument("--port",type=int,help="Port to connect to") 20 parser.add_argument("--port",type=int,help="Port to connect to")
19 parser.add_argument("--config",type=str,help="Path to config file",\ 21 parser.add_argument("--config",type=str,help="Path to config file",\
20 default="/etc/mfa/mfa.conf") 22 default="/etc/mfa/mfa.conf")
21 parser.add_argument("--key",type=str,help="Client connection key",required=True) 23 parser.add_argument("--key",type=str,help="Client connection key")
22 return parser.parse_args() 24 return parser.parse_args()
23 25
24def prompt_user(prompt): 26def prompt_user(prompt):
@@ -53,32 +55,48 @@ def init_connection(mfa_server, client_port, client_key):
53 55
54 56
55def read_config(config_file): 57def read_config(config_file):
56 # Read config file for server and port info 58 parser = configparser.ConfigParser(inline_comment_prefixes="#")
57 # Return tuple (server,port) 59 parser.read(config_file)
58 server = "" 60 return parser
59 port = 0 61
60 with open(config_file) as conf: 62
61 line = None 63def get_vars(args,confparser):
62 while line != "": 64 if not os.path.exists(args.config):
63 line = conf.readline() 65 print("Unable to open config file")
64 if line.startswith("server ="): 66 sys.exit(1)
65 server = line.split("=")[1].strip() 67
66 if line.startswith("port ="): 68 server = None
67 port = int(line.split("=")[1].strip()) 69 port = None
68 return (server,port) 70 key = None
71
72 # Set values from config file first
73 if confparser.has_section("client"):
74 server = confparser.get("client","server",fallback=None)
75 port = confparser.get("client","port",fallback=None)
76 key = confparser.get("client","key",fallback=None)
77
78 # Let command line args overwrite any values
79 if args.server:
80 server = args.server
81 if args.port:
82 port = args.port
83 if args.key:
84 key = args.key
85
86 # Exit if any value is null
87 if None in [server,port,key]:
88 print("error: one or more items unspecified")
89 sys.exit(1)
90
91 return server,port,key
92
69 93
70def main(): 94def main():
71 # Get arguments, exit if unable to connect 95 # Get arguments, exit if unable to connect
72 args = parse_arguments() 96 args = parse_arguments()
73 client_key = args.key 97 confparser = read_config(args.config)
74 98
75 # Read server and port from config file but allow command line options 99 mfa_server,client_port,client_key = get_vars(args,confparser)
76 # to override those settings
77 mfa_server, client_port = read_config(args.config)
78 if args.server != None:
79 mfa_server = args.server
80 if args.port != None:
81 client_port = args.port
82 100
83 # Exit if invalid key is provided 101 # Exit if invalid key is provided
84 if len(client_key) != KEY_LENGTH: 102 if len(client_key) != KEY_LENGTH: