chiark / gitweb /
make-secnet-sites: Fix python path manipulation
[secnet.git] / make-secnet-sites
1 #! /usr/bin/env python
2 #
3 # This file is part of secnet.
4 # See README for full list of copyright holders.
5 #
6 # secnet is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10
11 # secnet is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # version 3 along with secnet; if not, see
18 # https://www.gnu.org/licenses/gpl.html.
19
20 """VPN sites file manipulation.
21
22 This program enables VPN site descriptions to be submitted for
23 inclusion in a central database, and allows the resulting database to
24 be turned into a secnet configuration file.
25
26 A database file can be turned into a secnet configuration file simply:
27 make-secnet-sites.py [infile [outfile]]
28
29 It would be wise to run secnet with the "--just-check-config" option
30 before installing the output on a live system.
31
32 The program expects to be invoked via userv to manage the database; it
33 relies on the USERV_USER and USERV_GROUP environment variables. The
34 command line arguments for this invocation are:
35
36 make-secnet-sites.py -u header-filename groupfiles-directory output-file \
37   group
38
39 All but the last argument are expected to be set by userv; the 'group'
40 argument is provided by the user. A suitable userv configuration file
41 fragment is:
42
43 reset
44 no-disconnect-hup
45 no-suppress-args
46 cd ~/secnet/sites-test/
47 execute ~/secnet/make-secnet-sites.py -u vpnheader groupfiles sites
48
49 This program is part of secnet. It relies on the "ipaddr" library from
50 Cendio Systems AB.
51
52 """
53
54 from __future__ import print_function
55
56 import string
57 import time
58 import sys
59 import os
60 import getopt
61 import re
62
63 import ipaddr
64
65 # entry 0 is "near the executable", or maybe from PYTHONPATH=.,
66 # which we don't want to preempt
67 sys.path.insert(1,"/usr/local/share/secnet")
68 sys.path.insert(1,"/usr/share/secnet")
69 import ipaddrset
70
71 VERSION="0.1.18"
72
73 # Are we being invoked from userv?
74 service=0
75 # If we are, which group does the caller want to modify?
76 group=None
77
78 if len(sys.argv)<2:
79         inputfile=None
80         of=sys.stdout
81 else:
82         if sys.argv[1]=='-u':
83                 if len(sys.argv)!=6:
84                         print("Wrong number of arguments")
85                         sys.exit(1)
86                 service=1
87                 header=sys.argv[2]
88                 groupfiledir=sys.argv[3]
89                 sitesfile=sys.argv[4]
90                 group=sys.argv[5]
91                 if not os.environ.has_key("USERV_USER"):
92                         print("Environment variable USERV_USER not found")
93                         sys.exit(1)
94                 user=os.environ["USERV_USER"]
95                 # Check that group is in USERV_GROUP
96                 if not os.environ.has_key("USERV_GROUP"):
97                         print("Environment variable USERV_GROUP not found")
98                         sys.exit(1)
99                 ugs=os.environ["USERV_GROUP"]
100                 ok=0
101                 for i in string.split(ugs):
102                         if group==i: ok=1
103                 if not ok:
104                         print("caller not in group %s"%group)
105                         sys.exit(1)
106         else:
107                 if sys.argv[1]=='-P':
108                         prefix=sys.argv[2]
109                         sys.argv[1:3]=[]
110                 if len(sys.argv)>3:
111                         print("Too many arguments")
112                         sys.exit(1)
113                 inputfile=sys.argv[1]
114                 of=sys.stdout
115                 if len(sys.argv)>2:
116                         of=open(sys.argv[2],'w')
117
118 # Classes describing possible datatypes in the configuration file
119
120 class basetype:
121         "Common protocol for configuration types."
122         def add(self,obj,w):
123                 complain("%s %s already has property %s defined"%
124                         (obj.type,obj.name,w[0]))
125
126 class conflist:
127         "A list of some kind of configuration type."
128         def __init__(self,subtype,w):
129                 self.subtype=subtype
130                 self.list=[subtype(w)]
131         def add(self,obj,w):
132                 self.list.append(self.subtype(w))
133         def __str__(self):
134                 return ', '.join(map(str, self.list))
135 def listof(subtype):
136         return lambda w: conflist(subtype, w)
137
138 class single_ipaddr (basetype):
139         "An IP address"
140         def __init__(self,w):
141                 self.addr=ipaddr.IPAddress(w[1])
142         def __str__(self):
143                 return '"%s"'%self.addr
144
145 class networks (basetype):
146         "A set of IP addresses specified as a list of networks"
147         def __init__(self,w):
148                 self.set=ipaddrset.IPAddressSet()
149                 for i in w[1:]:
150                         x=ipaddr.IPNetwork(i,strict=True)
151                         self.set.append([x])
152         def __str__(self):
153                 return ",".join(map((lambda n: '"%s"'%n), self.set.networks()))
154
155 class dhgroup (basetype):
156         "A Diffie-Hellman group"
157         def __init__(self,w):
158                 self.mod=w[1]
159                 self.gen=w[2]
160         def __str__(self):
161                 return 'diffie-hellman("%s","%s")'%(self.mod,self.gen)
162
163 class hash (basetype):
164         "A choice of hash function"
165         def __init__(self,w):
166                 self.ht=w[1]
167                 if (self.ht!='md5' and self.ht!='sha1'):
168                         complain("unknown hash type %s"%(self.ht))
169         def __str__(self):
170                 return '%s'%(self.ht)
171
172 class email (basetype):
173         "An email address"
174         def __init__(self,w):
175                 self.addr=w[1]
176         def __str__(self):
177                 return '<%s>'%(self.addr)
178
179 class boolean (basetype):
180         "A boolean"
181         def __init__(self,w):
182                 if re.match('[TtYy1]',w[1]):
183                         self.b=True
184                 elif re.match('[FfNn0]',w[1]):
185                         self.b=False
186                 else:
187                         complain("invalid boolean value");
188         def __str__(self):
189                 return ['False','True'][self.b]
190
191 class num (basetype):
192         "A decimal number"
193         def __init__(self,w):
194                 self.n=string.atol(w[1])
195         def __str__(self):
196                 return '%d'%(self.n)
197
198 class address (basetype):
199         "A DNS name and UDP port number"
200         def __init__(self,w):
201                 self.adr=w[1]
202                 self.port=string.atoi(w[2])
203                 if (self.port<1 or self.port>65535):
204                         complain("invalid port number")
205         def __str__(self):
206                 return '"%s"; port %d'%(self.adr,self.port)
207
208 class rsakey (basetype):
209         "An RSA public key"
210         def __init__(self,w):
211                 self.l=string.atoi(w[1])
212                 self.e=w[2]
213                 self.n=w[3]
214         def __str__(self):
215                 return 'rsa-public("%s","%s")'%(self.e,self.n)
216
217 # Possible properties of configuration nodes
218 keywords={
219  'contact':(email,"Contact address"),
220  'dh':(dhgroup,"Diffie-Hellman group"),
221  'hash':(hash,"Hash function"),
222  'key-lifetime':(num,"Maximum key lifetime (ms)"),
223  'setup-timeout':(num,"Key setup timeout (ms)"),
224  'setup-retries':(num,"Maximum key setup packet retries"),
225  'wait-time':(num,"Time to wait after unsuccessful key setup (ms)"),
226  'renegotiate-time':(num,"Time after key setup to begin renegotiation (ms)"),
227  'restrict-nets':(networks,"Allowable networks"),
228  'networks':(networks,"Claimed networks"),
229  'pubkey':(rsakey,"RSA public site key"),
230  'peer':(single_ipaddr,"Tunnel peer IP address"),
231  'address':(address,"External contact address and port"),
232  'mobile':(boolean,"Site is mobile"),
233 }
234
235 def sp(name,value):
236         "Simply output a property - the default case"
237         return "%s %s;\n"%(name,value)
238
239 # All levels support these properties
240 global_properties={
241         'contact':(lambda name,value:"# Contact email address: %s\n"%(value)),
242         'dh':sp,
243         'hash':sp,
244         'key-lifetime':sp,
245         'setup-timeout':sp,
246         'setup-retries':sp,
247         'wait-time':sp,
248         'renegotiate-time':sp,
249         'restrict-nets':(lambda name,value:"# restrict-nets %s\n"%value),
250 }
251
252 class level:
253         "A level in the configuration hierarchy"
254         depth=0
255         leaf=0
256         allow_properties={}
257         require_properties={}
258         def __init__(self,w):
259                 self.name=w[1]
260                 self.properties={}
261                 self.children={}
262         def indent(self,w,t):
263                 w.write("                 "[:t])
264         def prop_out(self,n):
265                 return self.allow_properties[n](n,str(self.properties[n]))
266         def output_props(self,w,ind):
267                 for i in self.properties.keys():
268                         if self.allow_properties[i]:
269                                 self.indent(w,ind)
270                                 w.write("%s"%self.prop_out(i))
271         def output_data(self,w,ind,np):
272                 self.indent(w,ind)
273                 w.write("%s {\n"%(self.name))
274                 self.output_props(w,ind+2)
275                 if self.depth==1: w.write("\n");
276                 for c in self.children.values():
277                         c.output_data(w,ind+2,np+self.name+"/")
278                 self.indent(w,ind)
279                 w.write("};\n")
280
281 class vpnlevel(level):
282         "VPN level in the configuration hierarchy"
283         depth=1
284         leaf=0
285         type="vpn"
286         allow_properties=global_properties.copy()
287         require_properties={
288          'contact':"VPN admin contact address"
289         }
290         def __init__(self,w):
291                 level.__init__(self,w)
292         def output_vpnflat(self,w,ind,h):
293                 "Output flattened list of site names for this VPN"
294                 self.indent(w,ind)
295                 w.write("%s {\n"%(self.name))
296                 for i in self.children.keys():
297                         self.children[i].output_vpnflat(w,ind+2,
298                                 h+"/"+self.name+"/"+i)
299                 w.write("\n")
300                 self.indent(w,ind+2)
301                 w.write("all-sites %s;\n"%
302                         string.join(self.children.keys(),','))
303                 self.indent(w,ind)
304                 w.write("};\n")
305
306 class locationlevel(level):
307         "Location level in the configuration hierarchy"
308         depth=2
309         leaf=0
310         type="location"
311         allow_properties=global_properties.copy()
312         require_properties={
313          'contact':"Location admin contact address",
314         }
315         def __init__(self,w):
316                 level.__init__(self,w)
317                 self.group=w[2]
318         def output_vpnflat(self,w,ind,h):
319                 self.indent(w,ind)
320                 # The "h=h,self=self" abomination below exists because
321                 # Python didn't support nested_scopes until version 2.1
322                 w.write("%s %s;\n"%(self.name,string.join(
323                         map(lambda x,h=h,self=self:
324                                 h+"/"+x,self.children.keys()),',')))
325
326 class sitelevel(level):
327         "Site level (i.e. a leafnode) in the configuration hierarchy"
328         depth=3
329         leaf=1
330         type="site"
331         allow_properties=global_properties.copy()
332         allow_properties.update({
333          'address':sp,
334          'networks':None,
335          'peer':None,
336          'pubkey':(lambda n,v:"key %s;\n"%v),
337          'mobile':sp,
338         })
339         require_properties={
340          'dh':"Diffie-Hellman group",
341          'contact':"Site admin contact address",
342          'networks':"Networks claimed by the site",
343          'hash':"hash function",
344          'peer':"Gateway address of the site",
345          'pubkey':"RSA public key of the site",
346         }
347         def __init__(self,w):
348                 level.__init__(self,w)
349         def output_data(self,w,ind,np):
350                 self.indent(w,ind)
351                 w.write("%s {\n"%(self.name))
352                 self.indent(w,ind+2)
353                 w.write("name \"%s\";\n"%(np+self.name))
354                 self.output_props(w,ind+2)
355                 self.indent(w,ind+2)
356                 w.write("link netlink {\n");
357                 self.indent(w,ind+4)
358                 w.write("routes %s;\n"%str(self.properties["networks"]))
359                 self.indent(w,ind+4)
360                 w.write("ptp-address %s;\n"%str(self.properties["peer"]))
361                 self.indent(w,ind+2)
362                 w.write("};\n")
363                 self.indent(w,ind)
364                 w.write("};\n")
365
366 # Levels in the configuration file
367 # (depth,properties)
368 levels={'vpn':vpnlevel, 'location':locationlevel, 'site':sitelevel}
369
370 # Reserved vpn/location/site names
371 reserved={'all-sites':None}
372 reserved.update(keywords)
373 reserved.update(levels)
374
375 def complain(msg):
376         "Complain about a particular input line"
377         global complaints
378         print(("%s line %d: "%(file,line))+msg)
379         complaints=complaints+1
380 def moan(msg):
381         "Complain about something in general"
382         global complaints
383         print(msg);
384         complaints=complaints+1
385
386 root=level(['root','root'])   # All vpns are children of this node
387 obstack=[root]
388 allow_defs=0   # Level above which new definitions are permitted
389 prefix=''
390
391 def set_property(obj,w):
392         "Set a property on a configuration node"
393         if obj.properties.has_key(w[0]):
394                 obj.properties[w[0]].add(obj,w)
395         else:
396                 obj.properties[w[0]]=keywords[w[0]][0](w)
397
398 def pline(i,allow_include=False):
399         "Process a configuration file line"
400         global allow_defs, obstack, root
401         w=string.split(i.rstrip('\n'))
402         if len(w)==0: return [i]
403         keyword=w[0]
404         current=obstack[len(obstack)-1]
405         if keyword=='end-definitions':
406                 allow_defs=sitelevel.depth
407                 obstack=[root]
408                 return [i]
409         if keyword=='include':
410                 if not allow_include:
411                         complain("include not permitted here")
412                         return []
413                 if len(w) != 2:
414                         complain("include requires one argument")
415                         return []
416                 newfile=os.path.join(os.path.dirname(file),w[1])
417                 return pfilepath(newfile,allow_include=allow_include)
418         if levels.has_key(keyword):
419                 # We may go up any number of levels, but only down by one
420                 newdepth=levels[keyword].depth
421                 currentdepth=len(obstack) # actually +1...
422                 if newdepth<=currentdepth:
423                         obstack=obstack[:newdepth]
424                 if newdepth>currentdepth:
425                         complain("May not go from level %d to level %d"%
426                                 (currentdepth-1,newdepth))
427                 # See if it's a new one (and whether that's permitted)
428                 # or an existing one
429                 current=obstack[len(obstack)-1]
430                 if current.children.has_key(w[1]):
431                         # Not new
432                         current=current.children[w[1]]
433                         if service and group and current.depth==2:
434                                 if group!=current.group:
435                                         complain("Incorrect group!")
436                 else:
437                         # New
438                         # Ignore depth check for now
439                         nl=levels[keyword](w)
440                         if nl.depth<allow_defs:
441                                 complain("New definitions not allowed at "
442                                         "level %d"%nl.depth)
443                                 # we risk crashing if we continue
444                                 sys.exit(1)
445                         current.children[w[1]]=nl
446                         current=nl
447                 obstack.append(current)
448                 return [i]
449         if not current.allow_properties.has_key(keyword):
450                 complain("Property %s not allowed at %s level"%
451                         (keyword,current.type))
452                 return []
453         elif current.depth == vpnlevel.depth < allow_defs:
454                 complain("Not allowed to set VPN properties here")
455                 return []
456         else:
457                 set_property(current,w)
458                 return [i]
459
460         complain("unknown keyword '%s'"%(keyword))
461
462 def pfilepath(pathname,allow_include=False):
463         f=open(pathname)
464         outlines=pfile(pathname,f.readlines(),allow_include=allow_include)
465         f.close()
466         return outlines
467
468 def pfile(name,lines,allow_include=False):
469         "Process a file"
470         global file,line
471         file=name
472         line=0
473         outlines=[]
474         for i in lines:
475                 line=line+1
476                 if (i[0]=='#'): continue
477                 outlines += pline(i,allow_include=allow_include)
478         return outlines
479
480 def outputsites(w):
481         "Output include file for secnet configuration"
482         w.write("# secnet sites file autogenerated by make-secnet-sites "
483                 +"version %s\n"%VERSION)
484         w.write("# %s\n"%time.asctime(time.localtime(time.time())))
485         w.write("# Command line: %s\n\n"%string.join(sys.argv))
486
487         # Raw VPN data section of file
488         w.write(prefix+"vpn-data {\n")
489         for i in root.children.values():
490                 i.output_data(w,2,"")
491         w.write("};\n")
492
493         # Per-VPN flattened lists
494         w.write(prefix+"vpn {\n")
495         for i in root.children.values():
496                 i.output_vpnflat(w,2,prefix+"vpn-data")
497         w.write("};\n")
498
499         # Flattened list of sites
500         w.write(prefix+"all-sites %s;\n"%string.join(
501                 map(lambda x:"%svpn/%s/all-sites"%(prefix,x),
502                         root.children.keys()),","))
503
504 line=0
505 file=None
506 complaints=0
507
508 # Sanity check section
509 # Delete nodes where leaf=0 that have no children
510
511 def live(n):
512         "Number of leafnodes below node n"
513         if n.leaf: return 1
514         for i in n.children.keys():
515                 if live(n.children[i]): return 1
516         return 0
517 def delempty(n):
518         "Delete nodes that have no leafnode children"
519         for i in n.children.keys():
520                 delempty(n.children[i])
521                 if not live(n.children[i]):
522                         del n.children[i]
523
524 # Check that all constraints are met (as far as I can tell
525 # restrict-nets/networks/peer are the only special cases)
526
527 def checkconstraints(n,p,ra):
528         new_p=p.copy()
529         new_p.update(n.properties)
530         for i in n.require_properties.keys():
531                 if not new_p.has_key(i):
532                         moan("%s %s is missing property %s"%
533                                 (n.type,n.name,i))
534         for i in new_p.keys():
535                 if not n.allow_properties.has_key(i):
536                         moan("%s %s has forbidden property %s"%
537                                 (n.type,n.name,i))
538         # Check address range restrictions
539         if n.properties.has_key("restrict-nets"):
540                 new_ra=ra.intersection(n.properties["restrict-nets"].set)
541         else:
542                 new_ra=ra
543         if n.properties.has_key("networks"):
544                 if not n.properties["networks"].set <= new_ra:
545                         moan("%s %s networks out of bounds"%(n.type,n.name))
546                 if n.properties.has_key("peer"):
547                         if not n.properties["networks"].set.contains(
548                                 n.properties["peer"].addr):
549                                 moan("%s %s peer not in networks"%(n.type,n.name))
550         for i in n.children.keys():
551                 checkconstraints(n.children[i],new_p,new_ra)
552
553 if service:
554         headerinput=pfilepath(header,allow_include=True)
555         userinput=sys.stdin.readlines()
556         pfile("user input",userinput)
557 else:
558         if inputfile is None:
559                 pfile("stdin",sys.stdin.readlines())
560         else:
561                 pfilepath(inputfile)
562
563 delempty(root)
564 checkconstraints(root,{},ipaddrset.complete_set())
565
566 if complaints>0:
567         if complaints==1: print("There was 1 problem.")
568         else: print("There were %d problems."%(complaints))
569         sys.exit(1)
570
571 if service:
572         # Put the user's input into their group file, and rebuild the main
573         # sites file
574         f=open(groupfiledir+"/T"+group,'w')
575         f.write("# Section submitted by user %s, %s\n"%
576                 (user,time.asctime(time.localtime(time.time()))))
577         f.write("# Checked by make-secnet-sites version %s\n\n"%VERSION)
578         for i in userinput: f.write(i)
579         f.write("\n")
580         f.close()
581         os.rename(groupfiledir+"/T"+group,groupfiledir+"/R"+group)
582         f=open(sitesfile+"-tmp",'w')
583         f.write("# sites file autogenerated by make-secnet-sites\n")
584         f.write("# generated %s, invoked by %s\n"%
585                 (time.asctime(time.localtime(time.time())),user))
586         f.write("# use make-secnet-sites to turn this file into a\n")
587         f.write("# valid /etc/secnet/sites.conf file\n\n")
588         for i in headerinput: f.write(i)
589         files=os.listdir(groupfiledir)
590         for i in files:
591                 if i[0]=='R':
592                         j=open(groupfiledir+"/"+i)
593                         f.write(j.read())
594                         j.close()
595         f.write("# end of sites file\n")
596         f.close()
597         os.rename(sitesfile+"-tmp",sitesfile)
598 else:
599         outputsites(of)