1 # vim: set encoding=utf-8 et sw=4 sts=4 :
7 import logging ; log = logging.getLogger('pymin.services.vpn')
9 from pymin.seqtools import Sequence
10 from pymin.dispatcher import handler
11 from pymin.service.util import Restorable, ConfigWriter, InitdHandler, \
12 TransactionalHandler, DictSubHandler, \
15 from host import HostHandler
17 __all__ = ('VpnHandler',)
21 def __init__(self, vpn_src, vpn_dst, vpn_src_ip, vpn_src_mask,
22 pub_key=None, priv_key=None):
23 self.vpn_src = vpn_src
24 self.vpn_dst = vpn_dst
25 self.vpn_src_ip = vpn_src_ip
26 self.vpn_src_mask = vpn_src_mask
27 self.pub_key = pub_key
28 self.priv_key = priv_key
33 return(self.vpn_src, self.vpn_dst, self.vpn_src_ip, self.vpn_src_mask, self.pub_key, self.priv_key)
35 def update(self, vpn_dst=None, vpn_src_ip=None, vpn_src_mask=None):
36 if vpn_dst is not None:
37 self.vpn_dst = vpn_dst
38 if vpn_src_ip is not None:
39 self.vpn_src_ip = vpn_src_ip
40 if vpn_src_mask is not None:
41 self.vpn_src_mask = vpn_src_mask
44 class VpnHandler(Restorable, ConfigWriter,
45 TransactionalHandler, DictSubHandler):
47 handler_help = u"Manage vpn service"
49 _cont_subhandler_attr = 'vpns'
50 _cont_subhandler_class = Vpn
52 _persistent_attrs = ('vpns','hosts')
54 _restorable_defaults = dict(
59 _config_writer_files = ('tinc.conf','tinc-up','host')
60 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
62 def __init__(self, pickle_dir='.', config_dir='/etc/tinc'):
63 log.debug(u'VpnHandler(%r, %r)', pickle_dir, config_dir)
64 DictSubHandler.__init__(self, self)
65 self._config_writer_cfg_dir = config_dir
66 self._persistent_dir = pickle_dir
67 self._config_build_templates()
69 self.host = HostHandler(self)
71 @handler('usage: start <vpn_name>')
72 def start(self, vpn_src):
73 log.debug(u'VpnHandler.start(%r)', vpn_src)
74 if vpn_src in self.vpns:
75 call(('tincd','--net='+ vpn_src))
77 @handler('usage: stop <vpn_name>')
78 def stop(self, vpn_src):
79 log.debug(u'VpnHandler.stop(%r)', vpn_src)
80 if vpn_src in self.vpns:
81 pid_file = '/var/run/tinc.' + vpn_src + '.pid'
82 log.debug(u'VpnHandler.stop: getting pid from %r', pid_file)
83 if path.exists(pid_file):
84 pid = file(pid_file).readline()
85 pid = int(pid.strip())
87 log.debug(u'VpnHandler.stop: killing pid %r', pid)
88 os.kill(pid, signal.SIGTERM)
90 log.debug(u'VpnHandler.stop: error killing: %r', e)
92 log.debug(u'VpnHandler.stop: pid file not found')
94 def _write_config(self):
95 log.debug(u'VpnHandler._write_config()')
96 for v in self.vpns.values():
97 log.debug(u'VpnHandler._write_config: processing %r', v)
98 #chek whether it's been created or not.
100 if v.pub_key is None:
101 log.debug(u'VpnHandler._write_config: new VPN, generating '
104 log.debug(u'VpnHandler._write_config: creating dir %r',
105 path.join(self._config_writer_cfg_dir,
107 #first create the directory for the vpn
109 os.makedirs(path.join(self._config_writer_cfg_dir,
111 except (IOError, OSError), e:
112 if e.errno != errno.EEXIST:
113 raise HandlerError(u"Can't create VPN config "
114 "directory '%s' (%s)'"
115 % (e.filename, e.strerror))
116 #this command should generate 2 files inside the vpn
117 #dir, one rsa_key.priv and one rsa_key.pub
118 #for some reason debian does not work like this
119 # FIXME if the < /dev/null works, is magic!
120 log.debug(u'VpnHandler._write_config: creating key...')
121 call(('tincd', '-n', v.vpn_src, '-K', '<', '/dev/null'))
122 #open the created files and load the keys
124 f = file(path.join(self._config_writer_cfg_dir,
125 v.vpn_src, 'rsa_key.pub'),
129 except (IOError, OSError), e:
130 raise HandlerError(u"Can't read VPN key '%s' (%s)'"
131 % (e.filename, e.strerror))
135 except ExecutionError, e:
136 log.debug(u'VpnHandler._write_config: error executing '
137 'the command: %r', e)
142 self._write_single_config('tinc.conf',
143 path.join(v.vpn_src, 'tinc.conf'), vars)
144 self._write_single_config('tinc-up',
145 path.join(v.vpn_src, 'tinc-up'), vars)
146 for h in v.hosts.values():
151 self._write_single_config('host',
152 path.join(v.vpn_src, 'hosts', h.name), vars)
154 log.debug(u'VpnHandler._write_config: removing...')
156 # FIXME use os.unlink()
158 path.join(v.vpn_src, 'hosts', h.name)))
160 except ExecutionError, e:
161 log.debug(u'VpnHandler._write_config: error '
162 'removing files: %r', e)
164 #delete the vpn root at tinc dir
165 if path.exists('/etc/tinc/' + v.vpn_src):
167 call(('rm','-rf','/etc/tinc/' + v.vpn_src))
168 del self.vpns[v.vpn_src]
171 if __name__ == '__main__':
174 level = logging.DEBUG,
175 format = '%(asctime)s %(levelname)-8s %(message)s',
176 datefmt = '%H:%M:%S',
180 v.add('prueba','sarasa','192.168.0.188','255.255.255.0')
181 v.host.add('prueba', 'azazel' ,'192.168.0.77', '192.168.0.0',
182 'kjdhfkbdskljvkjblkbjeslkjbvkljbselvslberjhbvslbevlhb')