diff options
author | Sam Chudnick <sam@chudnick.com> | 2022-07-02 15:35:50 -0400 |
---|---|---|
committer | Sam Chudnick <sam@chudnick.com> | 2022-07-02 15:35:50 -0400 |
commit | 8472b394ee44cd46cc36fd4fe0a4882364cab602 (patch) | |
tree | 301fcb5e0becbebff4486b556e561afac61e11ca /pam/pam.py | |
parent | 01c24eb1f6f6a54bb780940c7665acd280b42aaf (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 'pam/pam.py')
-rwxr-xr-x | pam/pam.py | 40 |
1 files changed, 38 insertions, 2 deletions
@@ -3,6 +3,8 @@ import socket | |||
3 | import argparse | 3 | import argparse |
4 | import time | 4 | import time |
5 | import sys | 5 | import sys |
6 | import configparser | ||
7 | import os | ||
6 | 8 | ||
7 | # Sends authentication request to MFA server | 9 | # Sends authentication request to MFA server |
8 | # Receive either pass or fail response from MFA server | 10 | # Receive either pass or fail response from MFA server |
@@ -40,7 +42,6 @@ def init_connection(mfa_server, pam_port): | |||
40 | while connection == None and timeout < timeout_length: | 42 | while connection == None and timeout < timeout_length: |
41 | try: | 43 | try: |
42 | connection = socket.create_connection((mfa_server,pam_port)) | 44 | connection = socket.create_connection((mfa_server,pam_port)) |
43 | print("connected to mfa server") | ||
44 | return connection | 45 | return connection |
45 | except (ConnectionError,ConnectionRefusedError): | 46 | except (ConnectionError,ConnectionRefusedError): |
46 | time.sleep(sleep_length) | 47 | time.sleep(sleep_length) |
@@ -63,19 +64,54 @@ def read_config(config_file): | |||
63 | port = int(line.split("=")[1].strip()) | 64 | port = int(line.split("=")[1].strip()) |
64 | return (server,port) | 65 | return (server,port) |
65 | 66 | ||
67 | |||
68 | def read_config(config_file): | ||
69 | parser = configparser.ConfigParser(inline_comment_prefixes="#") | ||
70 | parser.read(config_file) | ||
71 | return parser | ||
72 | |||
73 | |||
74 | def get_vars(args,confparser): | ||
75 | if not os.path.exists(args.config): | ||
76 | print("Unable to open config file") | ||
77 | sys.exit(1) | ||
78 | |||
79 | server = None | ||
80 | port = None | ||
81 | |||
82 | # Set values from config file first | ||
83 | if confparser.has_section("pam"): | ||
84 | server = confparser.get("pam","server",fallback=None) | ||
85 | port = confparser.get("pam","port",fallback=None) | ||
86 | |||
87 | # Let command line args overwrite any values | ||
88 | if args.server: | ||
89 | server = args.server | ||
90 | if args.port: | ||
91 | port = args.port | ||
92 | |||
93 | # Exit if any value is null | ||
94 | if None in [server,port]: | ||
95 | print("error: one or more items unspecified") | ||
96 | sys.exit(1) | ||
97 | |||
98 | return server,port | ||
99 | |||
100 | |||
66 | def main(): | 101 | def main(): |
67 | authed = "0" | 102 | authed = "0" |
68 | failed = "1" | 103 | failed = "1" |
69 | 104 | ||
70 | # Get arguments | 105 | # Get arguments |
71 | args = parse_arguments() | 106 | args = parse_arguments() |
107 | confparser = read_config(args.config) | ||
108 | mfa_server,pam_port = get_vars(args,confparser) | ||
72 | user = args.user | 109 | user = args.user |
73 | service = args.service | 110 | service = args.service |
74 | 111 | ||
75 | # Compile data to send to server | 112 | # Compile data to send to server |
76 | # Read server and port from config file but allow command line options | 113 | # Read server and port from config file but allow command line options |
77 | # to override those settings | 114 | # to override those settings |
78 | mfa_server, pam_port = read_config(args.config) | ||
79 | if args.server != None: | 115 | if args.server != None: |
80 | mfa_server = args.server | 116 | mfa_server = args.server |
81 | if args.port != None: | 117 | if args.port != None: |