|
@@ -5,7 +5,327 @@ import argparse
|
5
|
5
|
import json
|
6
|
6
|
import os
|
7
|
7
|
import sys
|
8
|
|
-import vpngen
|
|
8
|
+import glob
|
|
9
|
+import json
|
|
10
|
+import os
|
|
11
|
+import os.path
|
|
12
|
+import re
|
|
13
|
+import shutil
|
|
14
|
+from enum import Enum
|
|
15
|
+from subprocess import call
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+class VpnGenError(Enum):
|
|
19
|
+ Success = 0,
|
|
20
|
+ VpnAlreadyExists = 1,
|
|
21
|
+ VpnDoesNotExists = 2,
|
|
22
|
+ ClientAlreadyExists = 3,
|
|
23
|
+ ClientDoesNotExists = 4
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+class VpnGen:
|
|
27
|
+ default_config_base_dir = ""
|
|
28
|
+ default_config_file = ""
|
|
29
|
+ default_client_config_file = ""
|
|
30
|
+ ovpn_config_path = ""
|
|
31
|
+
|
|
32
|
+ def __init__(self, default_config_path, ovpn_config_path):
|
|
33
|
+ self.default_config_base_dir = os.path.abspath(default_config_path)
|
|
34
|
+ self.default_config_file = "%s.conf" % self.default_config_base_dir
|
|
35
|
+ self.default_client_config_file = "%s%sclients%sclient.conf" % (self.default_config_base_dir, os.sep, os.sep)
|
|
36
|
+ self.ovpn_config_path = os.path.abspath(ovpn_config_path)
|
|
37
|
+
|
|
38
|
+ def f7(self, seq):
|
|
39
|
+ seen = set()
|
|
40
|
+ seen_add = seen.add
|
|
41
|
+ return [x for x in seq if not (x in seen or seen_add(x))]
|
|
42
|
+
|
|
43
|
+ def get_vpn_vars(self):
|
|
44
|
+ with open(self.default_config_file, "r") as f:
|
|
45
|
+ default_config = f.read()
|
|
46
|
+
|
|
47
|
+ variables = re.findall('\$\{([^}]+)}', default_config)
|
|
48
|
+ variables += ["KEY_COUNTRY", "KEY_PROVINCE", "KEY_CITY", "KEY_ORG", "KEY_EMAIL"]
|
|
49
|
+ variables = self.f7(variables)
|
|
50
|
+
|
|
51
|
+ return variables
|
|
52
|
+
|
|
53
|
+ def get_client_vars(self, vpn_name):
|
|
54
|
+ default_client_config_path = self.get_client_default_config_path(vpn_name)
|
|
55
|
+ if not os.path.exists(default_client_config_path):
|
|
56
|
+ return None
|
|
57
|
+
|
|
58
|
+ with open(default_client_config_path, "r") as f:
|
|
59
|
+ default_config = f.read()
|
|
60
|
+ variables = re.findall('\$\{([^}]+)}', default_config)
|
|
61
|
+ variables = self.f7(variables)
|
|
62
|
+
|
|
63
|
+ vpn_variables = self.get_vpn_vars()
|
|
64
|
+ real_variables = []
|
|
65
|
+
|
|
66
|
+ for var in variables:
|
|
67
|
+ if var not in vpn_variables and var != "client":
|
|
68
|
+ real_variables.append(var)
|
|
69
|
+
|
|
70
|
+ return real_variables
|
|
71
|
+
|
|
72
|
+ def get_base_dir(self, vpn_name):
|
|
73
|
+ return "%s%s%s%s" % (self.ovpn_config_path, os.sep, vpn_name, os.sep)
|
|
74
|
+
|
|
75
|
+ def get_config_path(self, vpn_name):
|
|
76
|
+ return "%s%s%s.conf" % (self.ovpn_config_path, os.sep, vpn_name)
|
|
77
|
+
|
|
78
|
+ def get_vpn_variables_path(self, vpn_name):
|
|
79
|
+ base_dir = self.get_base_dir(vpn_name)
|
|
80
|
+ return "%svpngen.json" % base_dir
|
|
81
|
+
|
|
82
|
+ def get_easy_rsa_dir(self, vpn_name):
|
|
83
|
+ base_dir = self.get_base_dir(vpn_name)
|
|
84
|
+ return "%seasy-rsa%s" % (base_dir, os.sep)
|
|
85
|
+
|
|
86
|
+ def get_easy_rsa_key_dir(self, vpn_name):
|
|
87
|
+ easyrsadir = self.get_easy_rsa_dir(vpn_name)
|
|
88
|
+ return "%skeys%s" % (easyrsadir, os.sep)
|
|
89
|
+
|
|
90
|
+ def get_pkitool_path(self, vpn_name):
|
|
91
|
+ easyrsadir = self.get_easy_rsa_dir(vpn_name)
|
|
92
|
+ return "%spkitool" % easyrsadir
|
|
93
|
+
|
|
94
|
+ def get_client_default_config_path(self, vpn_name):
|
|
95
|
+ base_dir = self.get_base_dir(vpn_name)
|
|
96
|
+ return "%s%sclients%sclient.conf" % (base_dir, os.sep, os.sep)
|
|
97
|
+
|
|
98
|
+ def get_client_dir(self, vpn_name, client_name):
|
|
99
|
+ base_dir = self.get_base_dir(vpn_name)
|
|
100
|
+ return "%sclients%s%s-%s%s" % (base_dir, os.sep, client_name, vpn_name, os.sep)
|
|
101
|
+
|
|
102
|
+ def get_client_config_path(self, vpn_name, client_name):
|
|
103
|
+ client_dir = self.get_client_dir(vpn_name, client_name)
|
|
104
|
+ return "%s%s-%s.conf" % (client_dir, client_name, vpn_name)
|
|
105
|
+
|
|
106
|
+ def get_client_variables_path(self, vpn_name, client_name):
|
|
107
|
+ client_dir = self.get_client_dir(vpn_name, client_name)
|
|
108
|
+ return "%svpngen.json" % client_dir
|
|
109
|
+
|
|
110
|
+ def get_client_generated_files_paths(self, vpn_name, client_name):
|
|
111
|
+ keys_dir = self.get_easy_rsa_key_dir(vpn_name,)
|
|
112
|
+ return [
|
|
113
|
+ "%s%s.crt" % (keys_dir, client_name),
|
|
114
|
+ "%s%s.key" % (keys_dir, client_name)
|
|
115
|
+ ]
|
|
116
|
+
|
|
117
|
+ def get_client_tarball_path(self, vpn_name, client_name):
|
|
118
|
+ base_dir = self.get_base_dir(vpn_name)
|
|
119
|
+ return "%sclients%s%s-%s.tar.bz2" % (base_dir, os.sep, client_name, vpn_name)
|
|
120
|
+
|
|
121
|
+ def get_server_needed_files_paths(self, vpn_name):
|
|
122
|
+ keys_dir = self.get_easy_rsa_key_dir(vpn_name)
|
|
123
|
+ return [
|
|
124
|
+ "%sca.crt" % keys_dir,
|
|
125
|
+ "%sta.key" % keys_dir
|
|
126
|
+ ]
|
|
127
|
+
|
|
128
|
+ def get_client_misc_files_paths(self, vpn_name):
|
|
129
|
+ base_dir = self.get_base_dir(vpn_name)
|
|
130
|
+ return glob.glob("%smisc-files%s*" % (base_dir, os.sep))
|
|
131
|
+
|
|
132
|
+ def get_all_needed_files_paths(self, vpn_name, client_name):
|
|
133
|
+ return self.get_client_generated_files_paths(vpn_name, client_name) +\
|
|
134
|
+ self.get_server_needed_files_paths(vpn_name) +\
|
|
135
|
+ self.get_client_misc_files_paths(vpn_name)
|
|
136
|
+
|
|
137
|
+ def get_server_variables(self, vpn_name):
|
|
138
|
+ with open(self.get_vpn_variables_path(vpn_name), "r") as f:
|
|
139
|
+ return json.load(f)['variables']
|
|
140
|
+
|
|
141
|
+ def get_client_variables(self, vpn_name, client_name):
|
|
142
|
+ with open(self.get_client_variables_path(vpn_name, client_name), "r") as f:
|
|
143
|
+ return json.load(f)['variables']
|
|
144
|
+
|
|
145
|
+ def get_client_list(self, vpn_name):
|
|
146
|
+ base_dir = self.get_base_dir(vpn_name)
|
|
147
|
+ files_paths = glob.glob("%sclients%s*" % (base_dir, os.sep))
|
|
148
|
+ files_names = list(map(lambda file_path: os.path.basename(file_path), files_paths))
|
|
149
|
+
|
|
150
|
+ clients = []
|
|
151
|
+ for file_name in files_names:
|
|
152
|
+ if file_name != 'client.conf' and not file_name.endswith(".tar.bz2"):
|
|
153
|
+ clients.append(file_name[0:len(file_name) - len(vpn_name) - 1])
|
|
154
|
+ return clients
|
|
155
|
+
|
|
156
|
+ def setup_vars(self, vpn_name, variables):
|
|
157
|
+ os.environ["KEY_COUNTRY"] = variables['KEY_COUNTRY']
|
|
158
|
+ os.environ["KEY_PROVINCE"] = variables['KEY_PROVINCE']
|
|
159
|
+ os.environ["KEY_CITY"] = variables['KEY_CITY']
|
|
160
|
+ os.environ["KEY_ORG"] = variables['KEY_ORG']
|
|
161
|
+ os.environ["KEY_OU"] = variables['KEY_ORG']
|
|
162
|
+ os.environ["KEY_CN"] = variables['KEY_ORG']
|
|
163
|
+ os.environ["KEY_NAME"] = variables['KEY_ORG']
|
|
164
|
+ os.environ["KEY_EMAIL"] = variables['KEY_EMAIL']
|
|
165
|
+ os.environ["KEY_SIZE"] = variables['KEY_SIZE']
|
|
166
|
+ os.environ["CA_EXPIRE"] = variables['CA_EXPIRE']
|
|
167
|
+ os.environ["KEY_EXPIRE"] = variables['KEY_EXPIRE']
|
|
168
|
+
|
|
169
|
+ self.setup_vars_openssl(vpn_name)
|
|
170
|
+
|
|
171
|
+ def setup_vars_openssl(self, vpn_name):
|
|
172
|
+ easyrsadir = self.get_easy_rsa_dir(vpn_name)
|
|
173
|
+ os.environ["EASY_RSA"] = easyrsadir
|
|
174
|
+ os.environ["OPENSSL"] = "openssl"
|
|
175
|
+ os.environ["PKCS11TOOL"] = "pkcs11-tool"
|
|
176
|
+ os.environ["GREP"] = "grep"
|
|
177
|
+ os.environ["KEY_CONFIG"] = "%s%s" % (easyrsadir, "openssl.cnf")
|
|
178
|
+ os.environ["KEY_DIR"] = "%s%s" % (easyrsadir, "keys")
|
|
179
|
+ os.environ["PKCS11_MODULE_PATH"] = "dummy"
|
|
180
|
+ os.environ["PKCS11_PIN"] = "dummy"
|
|
181
|
+
|
|
182
|
+ def create_vpn(self, vpn_name, variables):
|
|
183
|
+ base_dir = self.get_base_dir(vpn_name)
|
|
184
|
+ conf_file = self.get_config_path(vpn_name)
|
|
185
|
+ conf_vpngen_file = self.get_vpn_variables_path(vpn_name)
|
|
186
|
+ if os.path.exists(base_dir) or os.path.exists(conf_file):
|
|
187
|
+ return VpnGenError.VpnAlreadyExists
|
|
188
|
+
|
|
189
|
+ with open(self.default_config_file, "r") as f:
|
|
190
|
+ default_config = f.read()
|
|
191
|
+
|
|
192
|
+ variables['name'] = vpn_name
|
|
193
|
+ for variable in variables:
|
|
194
|
+ default_config = default_config.replace("${%s}" % variable, variables[variable])
|
|
195
|
+
|
|
196
|
+ os.makedirs(base_dir)
|
|
197
|
+ with open(conf_file, "w") as f:
|
|
198
|
+ f.write(default_config)
|
|
199
|
+
|
|
200
|
+ os.rmdir(base_dir)
|
|
201
|
+ shutil.copytree(self.default_config_base_dir, base_dir)
|
|
202
|
+
|
|
203
|
+ curdir = os.curdir
|
|
204
|
+ easyrsadir = self.get_easy_rsa_dir(vpn_name)
|
|
205
|
+ pkitool = self.get_pkitool_path(vpn_name)
|
|
206
|
+ os.chdir(easyrsadir)
|
|
207
|
+
|
|
208
|
+ self.setup_vars(vpn_name, variables)
|
|
209
|
+
|
|
210
|
+ call([".%sclean-all" % os.sep])
|
|
211
|
+ call([pkitool, "--initca", "-batch"])
|
|
212
|
+ call([pkitool, "--server", "server", "-batch"])
|
|
213
|
+ call([".%sbuild-dh" % os.sep])
|
|
214
|
+ call(["openssl", "ca", "-gencrl",
|
|
215
|
+ "-keyfile", "keys%sca.key" % os.sep,
|
|
216
|
+ "-cert", "keys%sca.crt" % os.sep,
|
|
217
|
+ "-out", "keys%scrl.pem" % os.sep,
|
|
218
|
+ "-config", "openssl.cnf"])
|
|
219
|
+
|
|
220
|
+ del os.environ["KEY_OU"]
|
|
221
|
+ del os.environ["KEY_CN"]
|
|
222
|
+ del os.environ["KEY_NAME"]
|
|
223
|
+
|
|
224
|
+ call(["openvpn", "--genkey", "--secret", "keys%sta.key" % os.sep])
|
|
225
|
+
|
|
226
|
+ with open(conf_vpngen_file, "w") as f:
|
|
227
|
+ json.dump({'variables': variables}, f, indent=4, separators=(',', ': '))
|
|
228
|
+
|
|
229
|
+ os.chdir(curdir)
|
|
230
|
+
|
|
231
|
+ return VpnGenError.Success
|
|
232
|
+
|
|
233
|
+ def remove_vpn(self, vpn_name):
|
|
234
|
+ base_dir = self.get_base_dir(vpn_name)
|
|
235
|
+ conf_file = self.get_config_path(vpn_name)
|
|
236
|
+ if not os.path.exists(base_dir) and not os.path.exists(conf_file):
|
|
237
|
+ return VpnGenError.VpnDoesNotExists
|
|
238
|
+ os.remove(conf_file)
|
|
239
|
+ shutil.rmtree(base_dir)
|
|
240
|
+ return VpnGenError.Success
|
|
241
|
+
|
|
242
|
+ def create_client(self, vpn_name, client_name, variables):
|
|
243
|
+ base_dir = self.get_base_dir(vpn_name)
|
|
244
|
+ if not os.path.exists(base_dir):
|
|
245
|
+ return VpnGenError.VpnDoesNotExists
|
|
246
|
+ client_dir = self.get_client_dir(vpn_name, client_name)
|
|
247
|
+ if os.path.exists(client_dir):
|
|
248
|
+ return VpnGenError.ClientAlreadyExists
|
|
249
|
+
|
|
250
|
+ curdir = os.curdir
|
|
251
|
+ easyrsadir = self.get_easy_rsa_dir(vpn_name)
|
|
252
|
+ pkitool = self.get_pkitool_path(vpn_name)
|
|
253
|
+ os.chdir(easyrsadir)
|
|
254
|
+
|
|
255
|
+ self.setup_vars(vpn_name, variables)
|
|
256
|
+ os.environ["KEY_CN"] = client_name
|
|
257
|
+ os.environ["KEY_NAME"] = client_name
|
|
258
|
+
|
|
259
|
+ call([pkitool, client_name])
|
|
260
|
+
|
|
261
|
+ os.chdir(curdir)
|
|
262
|
+
|
|
263
|
+ os.makedirs(client_dir)
|
|
264
|
+
|
|
265
|
+ return self.rebuild_client(vpn_name, client_name, variables)
|
|
266
|
+
|
|
267
|
+ def remove_client(self, vpn_name, client_name):
|
|
268
|
+ base_dir = self.get_base_dir(vpn_name)
|
|
269
|
+ if not os.path.exists(base_dir):
|
|
270
|
+ return VpnGenError.VpnDoesNotExists
|
|
271
|
+ client_dir = self.get_client_dir(vpn_name, client_name)
|
|
272
|
+ if not os.path.exists(client_dir):
|
|
273
|
+ return VpnGenError.ClientDoesNotExists
|
|
274
|
+
|
|
275
|
+ self.setup_vars_openssl(vpn_name)
|
|
276
|
+
|
|
277
|
+ curdir = os.curdir
|
|
278
|
+ easyrsadir = self.get_easy_rsa_dir(vpn_name)
|
|
279
|
+ os.chdir(easyrsadir)
|
|
280
|
+
|
|
281
|
+ call(["./revoke-full", client_name])
|
|
282
|
+
|
|
283
|
+ os.chdir(curdir)
|
|
284
|
+
|
|
285
|
+ return VpnGenError.Success
|
|
286
|
+
|
|
287
|
+ def rebuild_client(self, vpn_name, client_name, variables):
|
|
288
|
+ variables["client"] = client_name
|
|
289
|
+ client_dir = self.get_client_dir(vpn_name, client_name)
|
|
290
|
+ client_conf_file = self.get_client_config_path(vpn_name, client_name)
|
|
291
|
+ client_default_config_path = self.get_client_default_config_path(vpn_name)
|
|
292
|
+ with open(client_default_config_path, "r") as f:
|
|
293
|
+ client_default_config = f.read()
|
|
294
|
+
|
|
295
|
+ for variable in variables:
|
|
296
|
+ client_default_config = client_default_config.replace("${%s}" % variable, variables[variable])
|
|
297
|
+
|
|
298
|
+ files_names = glob.glob("%s%s*" % (client_dir, os.sep))
|
|
299
|
+ for file_name in files_names:
|
|
300
|
+ os.remove(file_name)
|
|
301
|
+
|
|
302
|
+ with open(client_conf_file, "w") as f:
|
|
303
|
+ f.write(client_default_config)
|
|
304
|
+
|
|
305
|
+ files_paths = self.get_all_needed_files_paths(vpn_name, client_name)
|
|
306
|
+ for file_path in files_paths:
|
|
307
|
+ dest = "%s%s-%s-%s" % (client_dir, client_name, vpn_name, os.path.basename(file_path))
|
|
308
|
+ shutil.copy(file_path, dest)
|
|
309
|
+
|
|
310
|
+ split = os.path.splitext(client_conf_file)
|
|
311
|
+ client_ovpn_file = "%s.ovpn" % split[0]
|
|
312
|
+ shutil.copy(client_conf_file, client_ovpn_file)
|
|
313
|
+
|
|
314
|
+ files_names = glob.glob("%s%s*" % (client_dir, os.sep))
|
|
315
|
+ files_names = list(map(lambda file_path: os.path.basename(file_path), files_names))
|
|
316
|
+
|
|
317
|
+ call(["tar", "cfj", self.get_client_tarball_path(vpn_name, client_name),
|
|
318
|
+ "-C", client_dir] + files_names)
|
|
319
|
+
|
|
320
|
+ client_variables = {}
|
|
321
|
+ for variable in self.get_client_vars(vpn_name):
|
|
322
|
+ client_variables[variable] = variables[variable]
|
|
323
|
+
|
|
324
|
+ with open(self.get_client_variables_path(vpn_name, client_name), "w") as f:
|
|
325
|
+ json.dump({'variables': client_variables}, f, indent=4, separators=(',', ': '))
|
|
326
|
+
|
|
327
|
+ return VpnGenError.Success
|
|
328
|
+
|
9
|
329
|
|
10
|
330
|
|
11
|
331
|
def eprint(*args, **kwargs):
|
|
@@ -54,7 +374,7 @@ def main():
|
54
|
374
|
if client_name is not None:
|
55
|
375
|
client_name = config['clientPrefix'] + client_name + config['clientSuffix']
|
56
|
376
|
|
57
|
|
- vpng = vpngen.VpnGen(config['defaultConfigPath'], config['ovpnConfigPath'])
|
|
377
|
+ vpng = VpnGen(config['defaultConfigPath'], config['ovpnConfigPath'])
|
58
|
378
|
|
59
|
379
|
config_path = vpng.get_vpn_variables_path(vpn_name)
|
60
|
380
|
if os.path.exists(config_path):
|
|
@@ -66,14 +386,14 @@ def main():
|
66
|
386
|
default_variables = config['defaults'].copy()
|
67
|
387
|
variables = create_variables(vpng.get_vpn_vars(), default_variables)
|
68
|
388
|
res = vpng.create_vpn(vpn_name, variables)
|
69
|
|
- if res == vpngen.VpnGenError.Success:
|
|
389
|
+ if res == VpnGenError.Success:
|
70
|
390
|
print("VPN %s created successfully" % vpn_name)
|
71
|
391
|
else:
|
72
|
392
|
eprint("Failed to create VPN %s: %s" % (vpn_name, res))
|
73
|
393
|
exit(1)
|
74
|
394
|
elif args.remove:
|
75
|
395
|
res = vpng.remove_vpn(vpn_name)
|
76
|
|
- if res == vpngen.VpnGenError.Success:
|
|
396
|
+ if res == VpnGenError.Success:
|
77
|
397
|
print("VPN %s removed successfully" % vpn_name)
|
78
|
398
|
else:
|
79
|
399
|
eprint("Failed to remove VPN %s: %s" % (vpn_name, res))
|
|
@@ -83,17 +403,17 @@ def main():
|
83
|
403
|
default_variables.update(vpng.get_server_variables(vpn_name))
|
84
|
404
|
variables = create_variables(vpng.get_client_vars(vpn_name), default_variables)
|
85
|
405
|
if variables is None:
|
86
|
|
- res = vpngen.VpnGenError.VpnDoesNotExists
|
|
406
|
+ res = VpnGenError.VpnDoesNotExists
|
87
|
407
|
else:
|
88
|
408
|
res = vpng.create_client(vpn_name, client_name, variables)
|
89
|
|
- if res == vpngen.VpnGenError.Success:
|
|
409
|
+ if res == VpnGenError.Success:
|
90
|
410
|
print("Client %s created successfully on VPN %s" % (client_name, vpn_name))
|
91
|
411
|
else:
|
92
|
412
|
eprint("Failed to create client %s on VPN %s: %s" % (client_name, vpn_name, res))
|
93
|
413
|
exit(1)
|
94
|
414
|
elif args.remove_client:
|
95
|
415
|
res = vpng.remove_client(vpn_name, client_name)
|
96
|
|
- if res == vpngen.VpnGenError.Success:
|
|
416
|
+ if res == VpnGenError.Success:
|
97
|
417
|
print("Client %s removed successfully on VPN %s" % (client_name, vpn_name))
|
98
|
418
|
else:
|
99
|
419
|
eprint("Failed to remove client %s on VPN %s: %s" % (client_name, vpn_name, res))
|
|
@@ -104,7 +424,7 @@ def main():
|
104
|
424
|
default_variables.update(vpng.get_client_variables(vpn_name, client_name))
|
105
|
425
|
|
106
|
426
|
res = vpng.rebuild_client(vpn_name, client_name, default_variables)
|
107
|
|
- if res == vpngen.VpnGenError.Success:
|
|
427
|
+ if res == VpnGenError.Success:
|
108
|
428
|
print("Client %s configuration rebuilt successfully on VPN %s" % (client_name, vpn_name))
|
109
|
429
|
else:
|
110
|
430
|
eprint("Failed to rebuild client %s configuration on VPN %s: %s" % (client_name, vpn_name, res))
|
|
@@ -117,7 +437,7 @@ def main():
|
117
|
437
|
variables = default_variables.copy()
|
118
|
438
|
variables.update(vpng.get_client_variables(vpn_name, client_name))
|
119
|
439
|
res = vpng.rebuild_client(vpn_name, client_name, variables)
|
120
|
|
- if res == vpngen.VpnGenError.Success:
|
|
440
|
+ if res == VpnGenError.Success:
|
121
|
441
|
print("Client %s configuration rebuilt successfully on VPN %s" % (client_name, vpn_name))
|
122
|
442
|
else:
|
123
|
443
|
eprint("Failed to rebuild client %s configuration on VPN %s: %s" % (client_name, vpn_name, res))
|