# vim: set encoding=utf-8 et sw=4 sts=4 :
import os
+import errno
import signal
from os import path
import logging ; log = logging.getLogger('pymin.services.vpn')
self.ip = ip
self.src_net = vpn_src_net
self.pub_key = key
- self.dele = False
+ self._delete = False
def as_tuple(self):
return(self.name, self.ip, self.src_net, self.pub_key)
_comp_subhandler_attr = 'hosts'
_comp_subhandler_class = Host
- @handler('usage: add <vpn_src> <ip> <vpn_src_net> <key>')
- def delete(self, vpn_src, host):
- DictComposedSubHandler.delete(self, vpn_src, host)
- if vpn_src in parent.vpns:
- if host in parent.vpns[vpn_src].hosts:
- parent.vpns[vpn_src].hosts[host].dele = True
-
class Vpn(Sequence):
- def __init__(self, vpn_src, vpn_dst, vpn_src_ip, vpn_src_mask, pub_key, priv_key):
+ def __init__(self, vpn_src, vpn_dst, vpn_src_ip, vpn_src_mask,
+ pub_key=None, priv_key=None):
self.vpn_src = vpn_src
self.vpn_dst = vpn_dst
self.vpn_src_ip = vpn_src_ip
self.pub_key = pub_key
self.priv_key = priv_key
self.hosts = dict()
- self.dele = False
+ self._delete = False
def as_tuple(self):
return(self.vpn_src, self.vpn_dst, self.vpn_src_ip, self.vpn_src_mask, self.pub_key, self.priv_key)
self._restore()
self.host = HostHandler(self)
- @handler('usage : add <vpn_name> <vpn_dst> <vpn_src_ip> <vpn_src_mask>')
- def add(self, vpn_src, vpn_dst, vpn_src_ip, vpn_src_mask):
- log.debug(u'VpnHandler.add(%r, %r, %r, %r)', vpn_src, vpn_dst,
- vpn_src_ip, vpn_src_mask)
- if vpn_src in self.vpns:
- if self.vpns[vpn_src].dele:
- log.debug(u'VpnHandler.add: deleted, undeleting')
- self.vpns[vpn_src] = False
- else:
- DictSubHandler.add(self, vpn_src, vpn_dst, vpn_src_ip,
- vpn_src_mask, None, None)
-
- @handler('usage : delete <vpn_name>')
- def delete(self, vpn_src):
- log.debug(u'VpnHandler.delete(%r)', vpn_src)
- if vpn_src in self.vpns:
- self.vpns[vpn_src].dele = True;
-
-
@handler('usage: start <vpn_name>')
def start(self, vpn_src):
log.debug(u'VpnHandler.start(%r)', vpn_src)
for v in self.vpns.values():
log.debug(u'VpnHandler._write_config: processing %r', v)
#chek whether it's been created or not.
- if not v.dele:
+ if not v._delete:
if v.pub_key is None:
log.debug(u'VpnHandler._write_config: new VPN, generating '
'key...')
path.join(self._config_writer_cfg_dir,
v.vpn_src ,'hosts'))
#first create the directory for the vpn
- call(('mkdir', '-p', path.join(
- self._config_writer_cfg_dir,
- v.vpn_src, 'hosts')))
+ try:
+ os.makedirs(path.join(self._config_writer_cfg_dir,
+ v.vpn_src, 'hosts'))
+ except (IOError, OSError), e:
+ if e.errno != errno.EEXIST:
+ raise HandlerError(u"Can't create VPN config "
+ "directory '%s' (%s)'"
+ % (e.filename, e.strerror))
#this command should generate 2 files inside the vpn
#dir, one rsa_key.priv and one rsa_key.pub
#for some reason debian does not work like this
log.debug(u'VpnHandler._write_config: creating key...')
call(('tincd', '-n', v.vpn_src, '-K', '<', '/dev/null'))
#open the created files and load the keys
- f = file(path.join(self._config_writer_cfg_dir,
- v.vpn_src, 'rsa_key.priv'), 'r')
- priv = f.read()
- f.close()
- f = file(path.join(self._config_writer_cfg_dir,
- v.vpn_src, 'rsa_key.pub'), 'r')
- pub = f.read()
- f.close()
+ try:
+ f = file(path.join(self._config_writer_cfg_dir,
+ v.vpn_src, 'rsa_key.pub'),
+ 'r')
+ pub = f.read()
+ f.close()
+ except (IOError, OSError), e:
+ raise HandlerError(u"Can't read VPN key '%s' (%s)'"
+ % (e.filename, e.strerror))
+
v.pub_key = pub
v.priv_key = priv
except ExecutionError, e:
self._write_single_config('tinc-up',
path.join(v.vpn_src, 'tinc-up'), vars)
for h in v.hosts.values():
- if not h.dele:
+ if not h._delete:
vars = dict(
host = h,
)