-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpn_account.py
executable file
·229 lines (184 loc) · 7.85 KB
/
vpn_account.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
#!/usr/bin/env python
# VPN account/network inventory
import argparse
import sqlite3
import json
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE, SIG_DFL)
DEFAULT_GROUP = 'prod'
DEFAULT_SUBNET = '0'
DEFAULT_NETWORK = '10.10.'
def get_chunks(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
def get_octets(address):
return str(address).split(".")
def printer(rows):
if ARGS["json"]:
print(json.dumps(rows, indent=4))
else:
for row in rows:
print(row)
def dict_factory(cursor, row):
dic = {}
for idx, col in enumerate(cursor.description):
dic[col[0]] = row[idx]
return dic
# def get_ippools_usage(conn):
# return {
# '.0.1': 0.40,
# '.0.4': 0.00
# }
def get_arguments():
parser = argparse.ArgumentParser(description='VPN account/network inventory.')
# User Actions
parser.add_argument('--create-account', help='Create a new VPN account.')
parser.add_argument('--create-account-hardcoded', help='Create a new VPN account with pre defined parameters.', nargs='*')
parser.add_argument('--revoke-account', help='Revoke an existing VPN account.')
# parser.add_argument('--modify', help='Modify an existing VPN account, e.g. move to another group or change details.')
# parser.add_argument('--password-reset', help='Reset a VPN account password.')
# Pool Actions
parser.add_argument('--create-pool', help='Create a new IP pool with provided subnet.', nargs='*')
# Usage View
parser.add_argument('--show-accounts', help='Shows accounts.', action='store_true')
parser.add_argument('--show-accounts-full', help='Shows accounts with respective IP pool info.', action='store_true')
parser.add_argument('--show-pools', help='Shows IP Pools.', action='store_true')
parser.add_argument('--show-pool-usage', help='Shows IP Pool usage.', action='store_true')
parser.add_argument('--show-subnet-usage', help='Shows subnet usage.', action='store_true')
# View format
parser.add_argument('--json', help='Prints data in JSON format.', action='store_true')
parsed_args = parser.parse_args()
# parser.print_help()
return vars(parsed_args)
def get_pools(subnet, name):
""" Generates list of IP pairs on a /20 available for the given subnet."""
subnet = int(subnet)
ret = []
for third_octete in range(subnet, subnet+16):
# each client needs 4 ips - must be divisible by 4
for forth_octete in get_chunks(range(1, 253), 4):
# Allows for 0.0/20 0.16/20 0.32/20
network = DEFAULT_NETWORK + str(third_octete)
ret.append([network + '.0', name, forth_octete[0], forth_octete[1], network + '.255', 0])
return ret
def get_accounts():
""" Get all user accounts """
sql = 'SELECT * FROM accounts'
CUR.execute(sql)
return CUR.fetchall()
def get_accounts_full():
""" Get all user accounts along with respective ip addresses assigned """
sql = 'SELECT * FROM accounts LEFT JOIN ippools on accounts.ippool_id = ippools.id'
CUR.execute(sql)
return CUR.fetchall()
def get_ippools():
""" Get all IP pools"""
sql = 'SELECT * FROM ippools'
CUR.execute(sql)
return CUR.fetchall()
def get_database():
""" Return instance of database connection"""
connection = sqlite3.connect('vpnaccount.db')
connection.row_factory = dict_factory
return connection
def create_tables():
""" Initialize database with new tables """
sql = """CREATE TABLE IF NOT EXISTS accounts
(id INTEGER PRIMARY KEY AUTOINCREMENT,
username VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
status INTEGER,
ippool_id INTEGER,
CONSTRAINT username_unique UNIQUE (username)
)"""
CONN.cursor().execute(sql)
sql = """CREATE TABLE IF NOT EXISTS ippools
(id INTEGER PRIMARY KEY AUTOINCREMENT,
network VARCHAR(100) NOT NULL,
subnet VARCHAR(100) NOT NULL,
first_ip VARCHAR(100) NOT NULL,
last_ip VARCHAR(100) NOT NULL,
broadcast VARCHAR(100) NOT NULL,
status INTEGER,
CONSTRAINT network_address UNIQUE (network, first_ip, last_ip)
)"""
CONN.cursor().execute(sql)
CONN.commit()
def get_available_ip():
""" Returns the first IP pair available """
cur = CONN.cursor()
cur.execute('SELECT * FROM ippools WHERE status = 0')
return cur.fetchone()
def get_ippool_id(first_ip, last_ip, network, subnet):
""" Returns IP pool id for given input parameters"""
cur = CONN.cursor()
cur.execute('SELECT * FROM ippools WHERE network = ? AND first_ip = ? AND last_ip = ? and subnet LIKE ?', [network, first_ip, last_ip, subnet])
return cur.fetchone()
def create_ippools(subnet=DEFAULT_SUBNET, name=DEFAULT_GROUP):
""" Populates IP pools database """
CONN.cursor().executemany('INSERT INTO ippools(network, subnet, first_ip, last_ip, broadcast, status) VALUES (?,?,?,?,?,?)', get_pools(subnet, name))
CONN.commit()
def create_user(email):
""" Create a new user and assign it an IP pair """
ip_address_id = get_available_ip()['id']
CONN.cursor().execute('UPDATE ippools SET status = 1 WHERE id = ?', (ip_address_id,))
CONN.cursor().execute('INSERT INTO accounts(username, email, status, ippool_id) VALUES (?,?,?,?)', [email, email, 1, ip_address_id])
CONN.commit()
def create_user_hardcoded(email, first_ip, last_ip, subnet):
""" Create a new user from existing system, preserving ip pairs """
network = DEFAULT_NETWORK + get_octets(first_ip)[2] + '.0'
first_ip = get_octets(first_ip)[3]
last_ip = get_octets(last_ip)[3]
print(first_ip, last_ip, network, subnet)
ip_address_id = get_ippool_id(first_ip, last_ip, network, subnet)['id']
CONN.cursor().execute('UPDATE ippools SET status = 1 WHERE id = ?', (ip_address_id,))
CONN.cursor().execute('INSERT INTO accounts(username, email, status, ippool_id) VALUES (?,?,?,?)', [email, email, 1, ip_address_id])
CONN.commit()
def revoke_user(email):
""" Disable user and free its IP pair """
cur = CONN.cursor()
cur.execute('SELECT * FROM accounts WHERE username LIKE ?', (email,))
account = cur.fetchone()
cur.execute('UPDATE accounts SET status = 0 WHERE id = ?', (int(account['id']), )) # also remove ippool_id?
cur.execute('UPDATE ippools SET status = 0 WHERE id = ?', (int(account['ippool_id']), ))
CONN.commit()
def handler_create_pool(arguments):
if len(arguments) == 2:
print('Creating subnet with provided name and range.')
create_ippools(arguments[0], arguments[1])
elif len(arguments) == 1:
print('You should provide no arguments for defaults or subnet and group name e.g. --create-pool 16 prod')
else:
print('Creating default subnet.')
create_ippools()
def handler_create_user_hardcoded(arguments):
if len(arguments) != 4:
print('You need to specify 4 params: EMAIL FISTIP LASTIP SUBNET')
return
email = arguments[0]
first_ip = arguments[1]
last_ip = arguments[2]
subnet = arguments[3]
create_user_hardcoded(email, first_ip, last_ip, subnet)
#
# main
#
CONN = get_database()
# if not database_initialized:
create_tables()
CUR = CONN.cursor()
ARGS = get_arguments()
if ARGS["show_accounts"]:
printer(get_accounts())
if ARGS["show_accounts_full"]:
printer(get_accounts_full())
if ARGS["show_pools"]:
printer(get_ippools())
if ARGS["create_account"] is not None:
create_user(ARGS["create_account"])
if ARGS["create_account_hardcoded"] is not None:
handler_create_user_hardcoded(ARGS["create_account_hardcoded"])
if ARGS["revoke_account"] is not None:
revoke_user(ARGS["revoke_account"])
if ARGS["create_pool"] is not None:
handler_create_pool(ARGS["create_pool"])
CONN.close()