Browse Source

merged python files; added install script

master
Robin Thoni 8 years ago
parent
commit
c0a57c4a92
3 changed files with 338 additions and 329 deletions
  1. 9
    0
      install
  2. 329
    9
      vpngen-cli.py
  3. 0
    320
      vpngen.py

+ 9
- 0
install View File

@@ -0,0 +1,9 @@
1
+#! /usr/bin/env sh
2
+
3
+dir="$(dirname $(readlink -f "${0}"))"
4
+
5
+if [ ! -d /etc/vpngen ]
6
+then
7
+  cp -r "${dir}/vpngen" /etc/vpngen
8
+fi &&
9
+cp "${dir}/vpngen-cli.py" /usr/local/bin/vpngen

+ 329
- 9
vpngen-cli.py View File

@@ -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))

+ 0
- 320
vpngen.py View File

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

Loading…
Cancel
Save