chiark / gitweb /
Tag version 1.0.0pre8.1.
[tripe] / svc / conntrack.in
1 #! @PYTHON@
2 ### -*-python-*-
3 ###
4 ### Service for automatically tracking network connection status
5 ###
6 ### (c) 2010 Straylight/Edgeware
7 ###
8
9 ###----- Licensing notice ---------------------------------------------------
10 ###
11 ### This file is part of Trivial IP Encryption (TrIPE).
12 ###
13 ### TrIPE is free software; you can redistribute it and/or modify
14 ### it under the terms of the GNU General Public License as published by
15 ### the Free Software Foundation; either version 2 of the License, or
16 ### (at your option) any later version.
17 ###
18 ### TrIPE is distributed in the hope that it will be useful,
19 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ### GNU General Public License for more details.
22 ###
23 ### You should have received a copy of the GNU General Public License
24 ### along with TrIPE; if not, write to the Free Software Foundation,
25 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
27 VERSION = '@VERSION@'
28
29 ###--------------------------------------------------------------------------
30 ### External dependencies.
31
32 from ConfigParser import RawConfigParser
33 from optparse import OptionParser
34 import os as OS
35 import sys as SYS
36 import socket as S
37 import mLib as M
38 import tripe as T
39 import dbus as D
40 for i in ['mainloop', 'mainloop.glib']:
41   __import__('dbus.%s' % i)
42 import gobject as G
43 from struct import pack, unpack
44
45 SM = T.svcmgr
46 ##__import__('rmcr').__debug = True
47
48 ###--------------------------------------------------------------------------
49 ### Utilities.
50
51 class struct (object):
52   """A simple container object."""
53   def __init__(me, **kw):
54     me.__dict__.update(kw)
55
56 def toposort(cmp, things):
57   """
58   Generate the THINGS in an order consistent with a given partial order.
59
60   The function CMP(X, Y) should return true if X must precede Y, and false if
61   it doesn't care.  If X and Y are equal then it should return false.
62
63   The THINGS may be any finite iterable; it is converted to a list
64   internally.
65   """
66
67   ## Make sure we can index the THINGS, and prepare an ordering table.
68   ## What's going on?  The THINGS might not have a helpful equality
69   ## predicate, so it's easier to work with indices.  The ordering table will
70   ## remember which THINGS (by index) are considered greater than other
71   ## things.
72   things = list(things)
73   n = len(things)
74   order = [{} for i in xrange(n)]
75   rorder = [{} for i in xrange(n)]
76   for i in xrange(n):
77     for j in xrange(n):
78       if i != j and cmp(things[i], things[j]):
79         order[j][i] = True
80         rorder[i][j] = True
81
82   ## Now we can do the sort.
83   out = []
84   while True:
85     done = True
86     for i in xrange(n):
87       if order[i] is not None:
88         done = False
89         if len(order[i]) == 0:
90           for j in rorder[i]:
91             del order[j][i]
92           yield things[i]
93           order[i] = None
94     if done:
95       break
96
97 ###--------------------------------------------------------------------------
98 ### Parse the configuration file.
99
100 ## Hmm.  Should I try to integrate this with the peers database?  It's not a
101 ## good fit; it'd need special hacks in tripe-newpeers.  And the use case for
102 ## this service are largely going to be satellite notes, I don't think
103 ## scalability's going to be a problem.
104
105 class Config (object):
106   """
107   Represents a configuration file.
108
109   The most interesting thing is probably the `groups' slot, which stores a
110   list of pairs (NAME, PATTERNS); the NAME is a string, and the PATTERNS a
111   list of (TAG, PEER, ADDR, MASK) triples.  The implication is that there
112   should be precisely one peer with a name matching NAME-*, and that it
113   should be NAME-TAG, where (TAG, PEER, ADDR, MASK) is the first triple such
114   that the host's primary IP address (if PEER is None -- or the IP address it
115   would use for communicating with PEER) is within the network defined by
116   ADDR/MASK.
117   """
118
119   def __init__(me, file):
120     """
121     Construct a new Config object, reading the given FILE.
122     """
123     me._file = file
124     me._fwatch = M.FWatch(file)
125     me._update()
126
127   def check(me):
128     """
129     See whether the configuration file has been updated.
130     """
131     if me._fwatch.update():
132       me._update()
133
134   def _update(me):
135     """
136     Internal function to update the configuration from the underlying file.
137     """
138
139     ## Read the configuration.  We have no need of the fancy substitutions,
140     ## so turn them all off.
141     cp = RawConfigParser()
142     cp.read(me._file)
143
144     ## Save the test address.  Make sure it's vaguely sensible.  The default
145     ## is probably good for most cases, in fact, since that address isn't
146     ## actually in use.  Note that we never send packets to the test address;
147     ## we just use it to discover routing information.
148     if cp.has_option('DEFAULT', 'test-addr'):
149       testaddr = cp.get('DEFAULT', 'test-addr')
150       S.inet_aton(testaddr)
151     else:
152       testaddr = '1.2.3.4'
153
154     ## Scan the configuration file and build the groups structure.
155     groups = []
156     for sec in cp.sections():
157       pats = []
158       for tag in cp.options(sec):
159         spec = cp.get(sec, tag).split()
160
161         ## Parse the entry into peer and network.
162         if len(spec) == 1:
163           peer = None
164           net = spec[0]
165         else:
166           peer, net = spec
167
168         ## Syntax of a net is ADDRESS/MASK, where ADDRESS is a dotted-quad,
169         ## and MASK is either a dotted-quad or a single integer N indicating
170         ## a mask with N leading ones followed by trailing zeroes.
171         slash = net.index('/')
172         addr, = unpack('>L', S.inet_aton(net[:slash]))
173         if net.find('.', slash + 1) >= 0:
174           mask, = unpack('>L', S.inet_aton(net[:slash]))
175         else:
176           n = int(net[slash + 1:], 10)
177           mask = (1 << 32) - (1 << 32 - n)
178         pats.append((tag, peer, addr & mask, mask))
179
180       ## Annoyingly, RawConfigParser doesn't preserve the order of options.
181       ## In order to make things vaguely sane, we topologically sort the
182       ## patterns so that more specific patterns are checked first.
183       pats = list(toposort(lambda (t, p, a, m), (tt, pp, aa, mm): \
184                              (p and not pp) or \
185                              (p == pp and m == (m | mm) and aa == (a & mm)),
186                            pats))
187       groups.append((sec, pats))
188
189     ## Done.
190     me.testaddr = testaddr
191     me.groups = groups
192
193 ### This will be a configuration file.
194 CF = None
195
196 ###--------------------------------------------------------------------------
197 ### Responding to a network up/down event.
198
199 def localaddr(peer):
200   """
201   Return the local IP address used for talking to PEER.
202   """
203   sk = S.socket(S.AF_INET, S.SOCK_DGRAM)
204   try:
205     try:
206       sk.connect((peer, 1))
207       addr, _ = sk.getsockname()
208       addr, = unpack('>L', S.inet_aton(addr))
209       return addr
210     except S.error:
211       return None
212   finally:
213     sk.close()
214
215 _kick = T.Queue()
216 def kickpeers():
217   lastip = {}
218   while True:
219     upness, reason = _kick.get()
220
221     ## Make sure the configuration file is up-to-date.  Don't worry if we
222     ## can't do anything useful.
223     try:
224       CF.check()
225     except Exception, exc:
226       SM.warn('conntrack', 'config-file-error',
227               exc.__class__.__name__, str(exc))
228
229     ## Find the current list of peers.
230     peers = SM.list()
231
232     ## Work out the primary IP address.
233     if upness:
234       addr = localaddr(CF.testaddr)
235       if addr is None:
236         upness = False
237     else:
238       addr = None
239
240     ## Now decide what to do.
241     changes = []
242     for g, pp in CF.groups:
243
244       ## Find out which peer in the group ought to be active.
245       ip = None
246       map = {}
247       want = None
248       for t, p, a, m in pp:
249         if p is None or not upness:
250           ipq = addr
251         else:
252           ipq = localaddr(p)
253         if upness and ip is None and \
254               ipq is not None and (ipq & m) == a:
255           map[t] = 'up'
256           want = t
257           ip = ipq
258         else:
259           map[t] = 'down'
260
261       ## Shut down the wrong ones.
262       found = False
263       for p in peers:
264         what = map.get(p, 'leave')
265         if what == 'up':
266           found = True
267         elif what == 'down':
268           changes.append(lambda p=p: SM.kill(p))
269
270       ## Start the right one if necessary.
271       if want is not None and (not found or ip != lastip.get(g, None)):
272         changes.append(lambda: T._simple(SM.svcsubmit('connect', 'active',
273                                                       want)))
274       lastip[g] = ip
275
276     ## Commit the changes.
277     if changes:
278       SM.notify('conntrack', upness and 'up' or 'down', *reason)
279       for c in changes: c()
280
281 def netupdown(upness, reason):
282   """
283   Add or kill peers according to whether the network is up or down.
284
285   UPNESS is true if the network is up, or false if it's down.
286   """
287
288   _kick.put((upness, reason))
289
290 ###--------------------------------------------------------------------------
291 ### NetworkManager monitor.
292
293 NM_NAME = 'org.freedesktop.NetworkManager'
294 NM_PATH = '/org/freedesktop/NetworkManager'
295 NM_IFACE = NM_NAME
296 NMCA_IFACE = NM_NAME + '.Connection.Active'
297
298 NM_STATE_CONNECTED = 3
299
300 class NetworkManagerMonitor (object):
301   """
302   Watch NetworkManager signals for changes in network state.
303   """
304
305   ## Strategy.  There are two kinds of interesting state transitions for us.
306   ## The first one is the global are-we-connected state, which we'll use to
307   ## toggle network upness on a global level.  The second is which connection
308   ## has the default route, which we'll use to tweak which peer in the peer
309   ## group is active.  The former is most easily tracked using the signal
310   ## org.freedesktop.NetworkManager.StateChanged; for the latter, we track
311   ## org.freedesktop.NetworkManager.Connection.Active.PropertiesChanged and
312   ## look for when a new connection gains the default route.
313
314   def attach(me, bus):
315     try:
316       nm = bus.get_object(NM_NAME, NM_PATH)
317       state = nm.Get(NM_IFACE, 'State')
318       if state == NM_STATE_CONNECTED:
319         netupdown(True, ['nm', 'initially-connected'])
320       else:
321         netupdown(False, ['nm', 'initially-disconnected'])
322     except D.DBusException:
323       pass
324     bus.add_signal_receiver(me._nm_state, 'StateChanged', NM_IFACE,
325                             NM_NAME, NM_PATH)
326     bus.add_signal_receiver(me._nm_connchange,
327                             'PropertiesChanged', NMCA_IFACE,
328                             NM_NAME, None)
329
330   def _nm_state(me, state):
331     if state == NM_STATE_CONNECTED:
332       netupdown(True, ['nm', 'connected'])
333     else:
334       netupdown(False, ['nm', 'disconnected'])
335
336   def _nm_connchange(me, props):
337     if props.get('Default', False):
338       netupdown(True, ['nm', 'default-connection-change'])
339
340 ###--------------------------------------------------------------------------
341 ### Maemo monitor.
342
343 ICD_NAME = 'com.nokia.icd'
344 ICD_PATH = '/com/nokia/icd'
345 ICD_IFACE = ICD_NAME
346
347 class MaemoICdMonitor (object):
348   """
349   Watch ICd signals for changes in network state.
350   """
351
352   ## Strategy.  ICd only handles one connection at a time in steady state,
353   ## though when switching between connections, it tries to bring the new one
354   ## up before shutting down the old one.  This makes life a bit easier than
355   ## it is with NetworkManager.  On the other hand, the notifications are
356   ## relative to particular connections only, and the indicator that the old
357   ## connection is down (`IDLE') comes /after/ the new one comes up
358   ## (`CONNECTED'), so we have to remember which one is active.
359
360   def attach(me, bus):
361     try:
362       icd = bus.get_object(ICD_NAME, ICD_PATH)
363       try:
364         iap = icd.get_ipinfo(dbus_interface = ICD_IFACE)[0]
365         me._iap = iap
366         netupdown(True, ['icd', 'initially-connected', iap])
367       except D.DBusException:
368         me._iap = None
369         netupdown(False, ['icd', 'initially-disconnected'])
370     except D.DBusException:
371       me._iap = None
372     bus.add_signal_receiver(me._icd_state, 'status_changed', ICD_IFACE,
373                             ICD_NAME, ICD_PATH)
374
375   def _icd_state(me, iap, ty, state, hunoz):
376     if state == 'CONNECTED':
377       me._iap = iap
378       netupdown(True, ['icd', 'connected', iap])
379     elif state == 'IDLE' and iap == me._iap:
380       me._iap = None
381       netupdown(False, ['icd', 'idle'])
382
383 ###--------------------------------------------------------------------------
384 ### D-Bus connection tracking.
385
386 class DBusMonitor (object):
387   """
388   Maintains a connection to the system D-Bus, and watches for signals.
389
390   If the connection is initially down, or drops for some reason, we retry
391   periodically (every five seconds at the moment).  If the connection
392   resurfaces, we reattach the monitors.
393   """
394
395   def __init__(me):
396     """
397     Initialise the object and try to establish a connection to the bus.
398     """
399     me._mons = []
400     me._loop = D.mainloop.glib.DBusGMainLoop()
401     me._state = 'startup'
402     me._reconnect()
403
404   def addmon(me, mon):
405     """
406     Add a monitor object to watch for signals.
407
408     MON.attach(BUS) is called, with BUS being the connection to the system
409     bus.  MON should query its service's current status and watch for
410     relevant signals.
411     """
412     me._mons.append(mon)
413     if me._bus is not None:
414       mon.attach(me._bus)
415
416   def _reconnect(me, hunoz = None):
417     """
418     Start connecting to the bus.
419
420     If we fail the first time, retry periodically.
421     """
422     if me._state == 'startup':
423       T.aside(SM.notify, 'conntrack', 'dbus-connection', 'startup')
424     elif me._state == 'connected':
425       T.aside(SM.notify, 'conntrack', 'dbus-connection', 'lost')
426     else:
427       T.aside(SM.notify, 'conntrack', 'dbus-connection',
428               'state=%s' % me._state)
429     me._state == 'reconnecting'
430     me._bus = None
431     if me._try_connect():
432       G.timeout_add_seconds(5, me._try_connect)
433
434   def _try_connect(me):
435     """
436     Actually make a connection attempt.
437
438     If we succeed, attach the monitors.
439     """
440     try:
441       addr = OS.getenv('TRIPE_CONNTRACK_BUS')
442       if addr == 'SESSION':
443         bus = D.SessionBus(mainloop = me._loop, private = True)
444       elif addr is not None:
445         bus = D.bus.BusConnection(addr, mainloop = me._loop)
446       else:
447         bus = D.SystemBus(mainloop = me._loop, private = True)
448       for m in me._mons:
449         m.attach(bus)
450     except D.DBusException, e:
451       return True
452     me._bus = bus
453     me._state = 'connected'
454     bus.call_on_disconnection(me._reconnect)
455     T.aside(SM.notify, 'conntrack', 'dbus-connection', 'connected')
456     return False
457
458 ###--------------------------------------------------------------------------
459 ### TrIPE service.
460
461 class GIOWatcher (object):
462   """
463   Monitor I/O events using glib.
464   """
465   def __init__(me, conn, mc = G.main_context_default()):
466     me._conn = conn
467     me._watch = None
468     me._mc = mc
469   def connected(me, sock):
470     me._watch = G.io_add_watch(sock, G.IO_IN,
471                                lambda *hunoz: me._conn.receive())
472   def disconnected(me):
473     G.source_remove(me._watch)
474     me._watch = None
475   def iterate(me):
476     me._mc.iteration(True)
477
478 SM.iowatch = GIOWatcher(SM)
479
480 def init():
481   """
482   Service initialization.
483
484   Add the D-Bus monitor here, because we might send commands off immediately,
485   and we want to make sure the server connection is up.
486   """
487   global DBM
488   T.Coroutine(kickpeers, name = 'kickpeers').switch()
489   DBM = DBusMonitor()
490   DBM.addmon(NetworkManagerMonitor())
491   DBM.addmon(MaemoICdMonitor())
492   G.timeout_add_seconds(30, lambda: (netupdown(True, ['interval-timer'])
493                                      or True))
494
495 def parse_options():
496   """
497   Parse the command-line options.
498
499   Automatically changes directory to the requested configdir, and turns on
500   debugging.  Returns the options object.
501   """
502   op = OptionParser(usage = '%prog [-a FILE] [-d DIR]',
503                     version = '%%prog %s' % VERSION)
504
505   op.add_option('-a', '--admin-socket',
506                 metavar = 'FILE', dest = 'tripesock', default = T.tripesock,
507                 help = 'Select socket to connect to [default %default]')
508   op.add_option('-d', '--directory',
509                 metavar = 'DIR', dest = 'dir', default = T.configdir,
510                 help = 'Select current diretory [default %default]')
511   op.add_option('-c', '--config',
512                 metavar = 'FILE', dest = 'conf', default = 'conntrack.conf',
513                 help = 'Select configuration [default %default]')
514   op.add_option('--daemon', dest = 'daemon',
515                 default = False, action = 'store_true',
516                 help = 'Become a daemon after successful initialization')
517   op.add_option('--debug', dest = 'debug',
518                 default = False, action = 'store_true',
519                 help = 'Emit debugging trace information')
520   op.add_option('--startup', dest = 'startup',
521                 default = False, action = 'store_true',
522                 help = 'Being called as part of the server startup')
523
524   opts, args = op.parse_args()
525   if args: op.error('no arguments permitted')
526   OS.chdir(opts.dir)
527   T._debug = opts.debug
528   return opts
529
530 ## Service table, for running manually.
531 def cmd_updown(upness):
532   return lambda *args: T.defer(netupdown, upness, ['manual'] + list(args))
533 service_info = [('conntrack', VERSION, {
534   'up': (0, None, '', cmd_updown(True)),
535   'down': (0, None, '', cmd_updown(False))
536 })]
537
538 if __name__ == '__main__':
539   opts = parse_options()
540   CF = Config(opts.conf)
541   T.runservices(opts.tripesock, service_info,
542                 init = init, daemon = opts.daemon)
543
544 ###----- That's all, folks --------------------------------------------------