summaryrefslogtreecommitdiff
path: root/pam/pam_mfa.py
blob: 5a5a112096f6b19b0cdeb89c879525852791d028 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/python3
import socket
import ssl
import argparse
import time
import sys
import configparser
import os

# Sends authentication request to MFA server
# Receive either pass or fail response from MFA server
# Returns status to PAM

HEADER_LENGTH = 64
KEY_LENGTH = 64
ACK_LENGTH = 3
DISCONNECT_MESSAGE = "DISCONNECT"
FORMAT = "utf-8"
RESPONSE_LENGTH = 1
PAM_SUCCESS = 0
PAM_AUTH_ERR = 7

def die(msg):
    print(msg)
    sys.exit(1)

def parse_arguments():
    # Parse command line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("--user",type=str,help="PAM username",required=True)
    parser.add_argument("--service",type=str,help="PAM service",required=True)
    parser.add_argument("--host",type=str,help="PAM hostname")
    parser.add_argument("--config",type=str,help="Path to config file",\
                        default="/etc/mfa/mfa.conf")
    parser.add_argument("--server",type=str,help="MFA server address")
    parser.add_argument("--port",type=str,help="MFA server PAM connection port")
    parser.add_argument("--insecure",action="store_true",
                        help="Accept invalid TLS certificates")
    return parser.parse_args()

def init_connection(mfa_server, pam_port, insecure):
    # Attempts to connect to MFA server with provided address and port
    # Repeats connection attempts once per second until timeout is reached
    # Returns the socket if connection was successful or None otherwise
    connection = None
    timeout = 0
    timeout_length = 5
    sleep_length = 1
    context = ssl.create_default_context()
    if insecure:
        context.check_hostname = False
        context.verify_mode = 0
    while connection == None and timeout < timeout_length:
        try:
            #connection = socket.create_connection((mfa_server,client_port))
            connection = context.wrap_socket(socket.socket(socket.AF_INET),
                                             server_hostname=mfa_server)
            connection.connect((mfa_server,int(pam_port)))
            return connection
        except (ConnectionError,ConnectionRefusedError):
            time.sleep(sleep_length)
            timeout += sleep_length
        except ssl.SSLCertVerificationError:
            die("error: server presented invalid certificate")
    return None


def read_config(config_file):
    # Read config file for server and port info
    # Return tuple (server,port)
    server = ""
    port = 0
    with open(config_file) as conf:
        line = None
        while line != "":
            line = conf.readline()
            if line.startswith("server ="):
                server = line.split("=")[1].strip()
            if line.startswith("port ="):
                port = int(line.split("=")[1].strip())
    return (server,port)


def read_config(config_file):
    parser = configparser.ConfigParser(inline_comment_prefixes="#")
    parser.read(config_file)
    return parser


def get_vars(args,confparser):
    if not os.path.exists(args.config):
        print("Unable to open config file")
        sys.exit(1)

    server = None
    port = None
    insecure = None

    # Set values from config file first
    if confparser.has_section("pam"):
        server = confparser.get("pam","server",fallback=None) 
        port = confparser.get("pam","port",fallback=None) 
        insecure = bool(confparser.get("pam","insecure",fallback=False))
        
    # Let command line args overwrite any values
    if args.server != None:
        server = args.server
    if args.port != None:
        port = args.port
    if args.insecure:
        insecure = args.insecure

    # Exit if any value is null
    if None in [server,port]:
        print("error: one or more items unspecified")
        sys.exit(1)

    return server,port,insecure


def main():
    authed = "0"
    failed = "1"

    # Get arguments
    args = parse_arguments()
    confparser = read_config(args.config)
    mfa_server,pam_port,insecure = get_vars(args,confparser)
    user = args.user
    service = args.service

    # Compile data to send to server
    # Read server and port from config file but allow command line options
    # to override those settings
    if args.server != None:
        mfa_server = args.server
    if args.port != None:
        pam_port = args.port
    # Get hostname if not given on command line
    if args.host == None:
        with open("/etc/hostname") as f:
            hostname = f.read().strip()
    else:
        hostname = args.host
    data = user + "," + hostname + "," + service 
    

    # Initalize connection to MFA server.  Quit if unable to connect.
    connection = init_connection(mfa_server,pam_port,insecure)
    if connection == None:
        print(failed)
        sys.exit(1)

    # Send authentication data to MFA server
    data_length = len(data)
    length_msg = str(data_length)
    length_msg += ' ' * (HEADER_LENGTH - len(length_msg))
    connection.send(length_msg.encode(FORMAT))
    connection.send(data.encode(FORMAT))

    # Listen for response from MFA server
    # Response will be 0 for authenticated and 1 for denied
    response = connection.recv(RESPONSE_LENGTH).decode(FORMAT)
    if response == "":
        # lost connection to server
        response = 1

    # Print success/failure for PAM module
    print(response)

if __name__ == '__main__':
    main()