1 # vim: set encoding=utf-8 et sw=4 sts=4 :
5 from pymin.seqtools import Sequence
6 from pymin.dispatcher import Handler, handler, HandlerError
7 from pymin.services.util import Restorable, ConfigWriter \
8 ,TransactionalHandler, DictSubHandler, call
10 __ALL__ = ('PppHandler',)
12 class ConnectionError(HandlerError, KeyError):
14 ConnectionError(hostname) -> ConnectionError instance
16 This is the base exception for all connection related errors.
19 def __init__(self, connection):
20 r"Initialize the object. See class documentation for more info."
21 self.message = u'Connection error: "%s"' % connection
23 class ConnectionNotFoundError(ConnectionError):
24 def __init__(self, connection):
25 r"Initialize the object. See class documentation for more info."
26 self.message = u'Connection not found error: "%s"' % connection
28 class Connection(Sequence):
30 def __init__(self, name, username, password, type, **kw):
32 self.username = username
33 self.password = password
36 if not 'device' in kw:
37 raise ConnectionError('Bad arguments for type=OE')
38 self.device = kw['device']
39 elif type == 'TUNNEL':
40 if not 'server' in kw:
41 raise ConnectionError('Bad arguments for type=TUNNEL')
42 self.server = kw['server']
43 self.username = self.username.replace('\\','\\\\')
45 if not 'device' in kw:
46 raise ConnectionError('Bad arguments for type=PPP')
47 self.device = kw['device']
49 raise ConnectionError('Bad arguments, unknown or unspecified type')
52 if self.type == 'TUNNEL':
53 return (self.name, self.username, self.password, self.type, self.server)
54 elif self.type == 'PPP' or self.type == 'OE':
55 return (self.name, self.username, self.password, self.type, self.device)
57 def update(self, device=None, username=None, password=None):
58 if device is not None:
60 if username is not None:
61 self.username = username
62 if password is not None:
63 self.password = password
66 class ConnectionHandler(DictSubHandler):
68 handler_help = u"Manages connections for the ppp service"
70 _cont_subhandler_attr = 'conns'
71 _cont_subhandler_class = Connection
73 class PppHandler(Restorable, ConfigWriter, TransactionalHandler):
75 handler_help = u"Manage ppp service"
77 _persistent_attrs = ('conns')
79 _restorable_defaults = dict(
83 _config_writer_files = ('options.X','pap-secrets','chap-secrets','nameX')
84 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
86 def __init__(self, pickle_dir='.', config_dir='.'):
87 r"Initialize Ppphandler object, see class documentation for details."
88 self._persistent_dir = pickle_dir
89 self._config_writer_cfg_dir = config_dir
90 self._config_build_templates()
92 self.conn = ConnectionHandler(self)
94 @handler('Starts the service')
95 def start(self, name):
96 if name in self.conns:
97 call(['pppd','call', name],stdout=None, stderr=None)
100 raise ConnectionNotFoundError(name)
102 @handler('Stops the service')
103 def stop(self, name):
104 if name in self.conns:
105 if path.exists('/var/run/ppp-' + name + '.pid'):
106 pid = file('/var/run/ppp-' + name + '.pid').readline().strip()
107 call(['kill',pid],stdout=None, stderr=None)
108 #print ('poff', name)
110 raise ConnectionNotFoundError(name)
112 @handler('Reloads the service')
114 for conn in self.conns.values():
116 self.start(conn.name)
118 def _write_config(self):
119 r"_write_config() -> None :: Generate all the configuration files."
120 #guardo los pass que van el pap-secrets
122 for conn in self.conns.values():
123 if conn.type == 'OE' or conn.type == 'PPP':
124 vars_pap[conn.name] = conn
125 vars = dict(conns=vars_pap)
126 self._write_single_config('pap-secrets','pap-secrets',vars)
127 #guardo los pass que van el chap-secrets
129 for conn in self.conns.values():
130 if conn.type == 'TUNNEL' :
131 vars_chap[conn.name] = conn
132 vars = dict(conns=vars_chap)
133 self._write_single_config('chap-secrets','chap-secrets',vars)
135 for conn in self.conns.values():
136 vars = dict(conn=conn)
137 self._write_single_config('nameX',conn.name, vars)
138 self._write_single_config('options.X','options.' + conn.name, vars)
141 if __name__ == '__main__':
143 p.conn.add('ppp_c','nico','nico',type='PPP',device='tty0')
144 p.conn.add('pppoe_c','fede','fede',type='OE',device='tty1')
145 p.conn.add('ppptunnel_c','dominio\luca','luca',type='TUNNEL',server='192.168.0.23')