]> gitweb.michael.orlitzky.com - untangle-https-backup.git/blob - src/untangle/untangle.py
Reorganize the source code into a package.
[untangle-https-backup.git] / src / untangle / untangle.py
1 import configparser
2 import http.cookiejar
3 import ssl
4 import urllib.parse
5 import urllib.request
6
7 class Untangle:
8
9 def __init__(self, s):
10 """
11 Initialize this Untangle object with a ConfigParser section.
12 """
13 self.name = s.name
14 self.host = s['host']
15 self.username = s.get('username', 'admin')
16 self.password = s['password']
17 self.version = int(s.get('version', '11'))
18 self.base_url = 'https://' + self.host + '/' # This never changes
19
20 # Sanity check the numerical version.
21 if self.version not in [9, 11]:
22 msg = 'Invalid version "' + str(self.version) + '" '
23 msg += 'in section "' + s.name + '"'
24 raise configparser.ParsingError(msg)
25
26 # Sanity check the boolean verify_cert parameter.
27 vc = s.get('verify_cert', 'False')
28 if vc == 'True':
29 self.verify_cert = True
30 elif vc == 'False':
31 self.verify_cert = False
32 else:
33 msg = 'Invalid value "' + vc + '" for verify_cert '
34 msg += 'in section "' + s.name + '"'
35 raise configparser.ParsingError(msg)
36
37 #
38 # Finally, create a URL opener to make HTTPS requests.
39 #
40 # First, create a cookie jar that we'll attach to our URL
41 # opener thingy.
42 cj = http.cookiejar.CookieJar()
43 cookie_proc = urllib.request.HTTPCookieProcessor(cj)
44
45 # SSL mumbo jumbo to make it ignore the certificate's hostname
46 # when verify_cert = False.
47 if self.verify_cert:
48 ssl_ctx = ssl.create_default_context()
49 else:
50 ssl_ctx = ssl._create_unverified_context()
51
52 https_handler = urllib.request.HTTPSHandler(context=ssl_ctx)
53
54 # Now Create a URL opener, and tell it to use our cookie jar
55 # and SSL context. We keep this around for future requests.
56 self.opener = urllib.request.build_opener(https_handler, cookie_proc)
57
58
59 def login(self):
60 login_path = 'auth/login?url=/setup/welcome.do&realm=Administrator'
61 url = self.base_url + login_path
62 post_vars = {'username': self.username, 'password': self.password }
63 post_data = urllib.parse.urlencode(post_vars).encode('ascii')
64 self.opener.open(url, post_data)
65
66
67 def get_backup(self):
68 if self.version == 9:
69 return self.get_backup_v9()
70 elif self.version == 11:
71 return self.get_backup_v11()
72
73
74 def get_backup_v9(self):
75 url = self.base_url + '/webui/backup'
76 post_vars = {'action': 'requestBackup'}
77 post_data = urllib.parse.urlencode(post_vars).encode('ascii')
78 self.opener.open(url, post_data)
79
80 url = self.base_url + 'webui/backup?action=initiateDownload'
81 with self.opener.open(url) as response:
82 return response.read()
83
84
85 def get_backup_v11(self):
86 url = self.base_url + '/webui/download?type=backup'
87 post_vars = {'type': 'backup'}
88 post_data = urllib.parse.urlencode(post_vars).encode('ascii')
89 with self.opener.open(url, post_data) as response:
90 return response.read()