forked from sunshowers/firewall-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firewall-auth.py
313 lines (272 loc) · 9.69 KB
/
firewall-auth.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/python
# Copyright (c) 2009 Siddharth Agarwal
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
import getpass
import httplib
import urllib
import urlparse
import re
from optparse import OptionParser
import sys
import logging
import time
import atexit
import socket
import gc
import netrc
class FirewallState:
Start, LoggedIn, End = range(3)
# Globals, set right in the beginning
username = None
password = None
def start_func():
"""
This is called when we're in the initial state. If we're already logged in, we
can't do anything much. If we're not, we should transition to the
not-logged-in state.
"""
ERROR_RETRY_SECS = 5
LOGGED_IN_RETRY_SECS = 5
logger = logging.getLogger("FirewallLogger")
try:
loginstate, data = login()
except (httplib.HTTPException, socket.error) as e:
logger.info("Exception |%s| while trying to log in. Retrying in %d seconds." %
(e, ERROR_RETRY_SECS))
return (FirewallState.Start, ERROR_RETRY_SECS, None)
# Check whether we're logged in
if loginstate == LoginState.AlreadyLoggedIn:
logger.info("You're already logged in (response code %d). Retrying in %d seconds." %
(data, LOGGED_IN_RETRY_SECS))
return (FirewallState.Start, LOGGED_IN_RETRY_SECS, None)
elif loginstate == LoginState.InvalidCredentials:
# Not much we can do.
return (FirewallState.End, 0, [3])
else:
# Yay, we logged in.
return (FirewallState.LoggedIn, 0, [data])
def logged_in_func(keepaliveurl):
"""
Keep the firewall authentication alive by pinging a keepalive URL every few
seconds. If there are any connection problems, keep trying with the same
URL. If the keepalive URL doesn't work any more, go back to the start state.
"""
logger = logging.getLogger("FirewallLogger")
ERROR_RETRY_SECS = 5
LOGGED_IN_SECS = 200
try:
keep_alive(keepaliveurl)
except httplib.BadStatusLine:
logger.info("The keepalive URL %s doesn't work. Attempting to log in again." %
keepaliveurl.geturl())
return (FirewallState.Start, 0, None)
except (httplib.HTTPException, socket.error) as e:
logger.info("Exception |%s| while trying to keep alive. Retrying in %d seconds." %
(e, ERROR_RETRY_SECS))
return (FirewallState.LoggedIn, ERROR_RETRY_SECS, [keepaliveurl])
# OK, the URL worked. That's good.
return (FirewallState.LoggedIn, LOGGED_IN_SECS, [keepaliveurl])
state_functions = {
FirewallState.Start: start_func,
FirewallState.LoggedIn: logged_in_func,
FirewallState.End: sys.exit
}
def run_state_machine():
"""
Runs the state machine defined above.
"""
state = FirewallState.Start
args = None
sleeptime = 0
def atexit_logout():
"""
Log out from firewall authentication. This is supposed to run whenever the
program exits.
"""
logger = logging.getLogger("FirewallLogger")
if state == FirewallState.LoggedIn:
url = args[0]
logouturl = urlparse.ParseResult(url.scheme, url.netloc, "/logout",
url.params, url.query, url.fragment)
try:
logger.info("Logging out with URL %s" % logouturl.geturl())
conn = httplib.HTTPSConnection(logouturl.netloc)
conn.request("GET", logouturl.path + "?" + logouturl.query)
response = conn.getresponse()
response.read()
except (httplib.HTTPException, socket.error) as e:
# Just print an error message
logger.info("Exception |%s| while logging out." % e)
finally:
conn.close()
atexit.register(atexit_logout)
while True:
statefunc = state_functions[state]
if args is None:
state, sleeptime, args = statefunc()
else:
state, sleeptime, args = statefunc(*args)
if sleeptime > 0:
time.sleep(sleeptime)
class LoginState:
AlreadyLoggedIn, InvalidCredentials, Successful = range(3)
def login():
"""
Attempt to log in. Returns AlreadyLoggedIn if we're already logged in,
InvalidCredentials if the username and password given are incorrect, and
Successful if we have managed to log in. Throws an exception if an error
occurs somewhere along the process.
"""
logger = logging.getLogger("FirewallLogger")
# Find out where to auth
try:
conn = httplib.HTTPConnection("74.125.236.51:80")
conn.request("GET", "/")
response = conn.getresponse()
# 303 leads to the auth page, so it means we're not logged in
if (response.status != 303):
return (LoginState.AlreadyLoggedIn, response.status)
authlocation = response.getheader("Location")
finally:
conn.close()
logger.info("The auth location is: %s" % authlocation)
# Make a connection to the auth location
parsedauthloc = urlparse.urlparse(authlocation)
try:
authconn = httplib.HTTPSConnection(parsedauthloc.netloc)
authconn.request("GET", parsedauthloc.path + "?" + parsedauthloc.query)
authResponse = authconn.getresponse()
data = authResponse.read()
finally:
authconn.close()
# Look for the right magic value in the data
match = re.search(r"VALUE=\"([0-9a-f]+)\"", data)
magicString = match.group(1)
logger.debug("The magic string is: " + magicString)
# Now construct a POST request
params = urllib.urlencode({'username': username, 'password': password,
'magic': magicString, '4Tredir': '/'})
headers = {"Content-Type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
try:
postconn = httplib.HTTPSConnection(parsedauthloc.netloc)
postconn.request("POST", "/", params, headers)
# Get the response
postResponse = postconn.getresponse()
postData = postResponse.read()
finally:
postconn.close()
# Look for the keepalive URL
keepaliveMatch = re.search(r"location.href=\"(.+?)\"", postData)
if keepaliveMatch is None:
# Whoops, unsuccessful -- probably the username and password didn't match
logger.fatal("Authentication unsuccessful, check your username and password.")
return (LoginState.InvalidCredentials, None)
keepaliveURL = keepaliveMatch.group(1)
logger.info("The keep alive URL is: " + keepaliveURL)
logger.debug(postData)
return (LoginState.Successful, urlparse.urlparse(keepaliveURL))
def keep_alive(url):
"""
Attempt to keep the connection alive by pinging a URL.
"""
logger = logging.getLogger("FirewallLogger")
logger.info("Sending request to keep alive.")
# Connect to the firewall
try:
conn = httplib.HTTPSConnection(url.netloc)
conn.request("GET", url.path + "?" + url.query)
# This line raises an exception if the URL stops working. We catch it in
# logged_in_func.
response = conn.getresponse()
logger.debug(str(response.status))
logger.debug(response.read())
finally:
conn.close()
gc.collect()
def get_credentials(options, args):
"""
Get the username and password, from netrc, command line args or interactively.
"""
username = None
password = None
if options.netrc:
logger = logging.getLogger("FirewallLogger")
try:
info = netrc.netrc()
cred = info.authenticators("172.31.1.251")
if cred:
return (cred[0], cred[2])
logger.info("Could not find credentials in netrc file.")
except:
logger.info("Could not read from netrc file.")
if len(args) == 0:
# Get the username from the input
print "Username: ",
username = sys.stdin.readline()[:-1]
else:
# First member of args
username = args[0]
if len(args) <= 1:
# Read the password without echoing it
password = getpass.getpass()
else:
password = args[1]
return (username, password)
def init_logger(options):
logger = logging.getLogger("FirewallLogger")
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
if options.verbose:
handler.setLevel(logging.DEBUG)
else:
handler.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
"""
Main function
"""
def main(argv = None):
if argv is None:
argv = sys.argv[1:]
# First generate help syntax
usage = "Usage: %prog [options] [username [password]]"
parser = OptionParser(usage = usage)
parser.add_option("-v", "--verbose", action = "store_true", dest = "verbose",
help = "Print lots of debugging information")
parser.add_option("-n", "--netrc", action = "store_true", dest = "netrc",
help = "Read credentials from netrc file")
# Parse arguments
(options, args) = parser.parse_args(argv)
if len(args) > 2:
parser.error("too many arguments")
return 1
init_logger(options)
# Try authenticating!
global username, password
username, password = get_credentials(options, args)
run_state_machine()
return 0
if __name__ == "__main__":
sys.exit(main())