1 #! /usr/bin/env python3
3 # This file is part of secnet.
4 # See README for full list of copyright holders.
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.
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.
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.
20 """VPN sites file manipulation.
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.
26 A database file can be turned into a secnet configuration file simply:
27 make-secnet-sites.py [infile [outfile]]
29 It would be wise to run secnet with the "--just-check-config" option
30 before installing the output on a live system.
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:
36 make-secnet-sites.py -u header-filename groupfiles-directory output-file \
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
46 cd ~/secnet/sites-test/
47 execute ~/secnet/make-secnet-sites.py -u vpnheader groupfiles sites
49 This program is part of secnet.
53 from __future__ import print_function
54 from __future__ import unicode_literals
55 from builtins import int
67 # entry 0 is "near the executable", or maybe from PYTHONPATH=.,
68 # which we don't want to preempt
69 sys.path.insert(1,"/usr/local/share/secnet")
70 sys.path.insert(1,"/usr/share/secnet")
75 from sys import version_info
76 if version_info.major == 2: # for python2
78 sys.stdin = codecs.getreader('utf-8')(sys.stdin)
79 sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
81 open=lambda f,m='r': io.open(f,m,encoding='utf-8')
93 ap = argparse.ArgumentParser(description='process secnet sites files')
94 ap.add_argument('--userv', '-u', action='store_true',
95 help='userv service fragment update mode')
96 ap.add_argument('--prefix', '-P', nargs=1,
98 ap.add_argument('arg',nargs=argparse.REMAINDER)
100 #print(repr(av), file=sys.stderr)
101 service = 1 if av.userv else 0
104 print("Wrong number of arguments")
106 (header, groupfiledir, sitesfile, group) = av.arg
107 if "USERV_USER" not in os.environ:
108 print("Environment variable USERV_USER not found")
110 user=os.environ["USERV_USER"]
111 # Check that group is in USERV_GROUP
112 if "USERV_GROUP" not in os.environ:
113 print("Environment variable USERV_GROUP not found")
115 ugs=os.environ["USERV_GROUP"]
117 for i in ugs.split():
120 print("caller not in group %s"%group)
124 print("Too many arguments")
126 (inputfile, outputfile) = (av.arg + [None]*2)[0:2]
127 if outputfile is None: of=sys.stdout
128 else: of=open(sys.argv[2],'w')
132 # Classes describing possible datatypes in the configuration file
135 "Common protocol for configuration types."
137 complain("%s %s already has property %s defined"%
138 (obj.type,obj.name,w[0]))
141 "A list of some kind of configuration type."
142 def __init__(self,subtype,w):
144 self.list=[subtype(w)]
146 self.list.append(self.subtype(w))
148 return ', '.join(map(str, self.list))
150 return lambda w: conflist(subtype, w)
152 class single_ipaddr (basetype):
154 def __init__(self,w):
155 self.addr=ipaddress.ip_address(w[1])
157 return '"%s"'%self.addr
159 class networks (basetype):
160 "A set of IP addresses specified as a list of networks"
161 def __init__(self,w):
162 self.set=ipaddrset.IPAddressSet()
164 x=ipaddress.ip_network(i,strict=True)
167 return ",".join(map((lambda n: '"%s"'%n), self.set.networks()))
169 class dhgroup (basetype):
170 "A Diffie-Hellman group"
171 def __init__(self,w):
175 return 'diffie-hellman("%s","%s")'%(self.mod,self.gen)
177 class hash (basetype):
178 "A choice of hash function"
179 def __init__(self,w):
182 if (self.ht!='md5' and self.ht!='sha1'):
183 complain("unknown hash type %s"%(self.ht))
185 return '%s'%(self.ht)
187 class email (basetype):
189 def __init__(self,w):
192 return '<%s>'%(self.addr)
194 class boolean (basetype):
196 def __init__(self,w):
197 if re.match('[TtYy1]',w[1]):
199 elif re.match('[FfNn0]',w[1]):
202 complain("invalid boolean value");
204 return ['False','True'][self.b]
206 class num (basetype):
208 def __init__(self,w):
213 class address (basetype):
214 "A DNS name and UDP port number"
215 def __init__(self,w):
218 if (self.port<1 or self.port>65535):
219 complain("invalid port number")
221 return '"%s"; port %d'%(self.adr,self.port)
223 class rsakey (basetype):
225 def __init__(self,w):
230 return 'rsa-public("%s","%s")'%(self.e,self.n)
232 # Possible properties of configuration nodes
234 'contact':(email,"Contact address"),
235 'dh':(dhgroup,"Diffie-Hellman group"),
236 'hash':(hash,"Hash function"),
237 'key-lifetime':(num,"Maximum key lifetime (ms)"),
238 'setup-timeout':(num,"Key setup timeout (ms)"),
239 'setup-retries':(num,"Maximum key setup packet retries"),
240 'wait-time':(num,"Time to wait after unsuccessful key setup (ms)"),
241 'renegotiate-time':(num,"Time after key setup to begin renegotiation (ms)"),
242 'restrict-nets':(networks,"Allowable networks"),
243 'networks':(networks,"Claimed networks"),
244 'pubkey':(rsakey,"RSA public site key"),
245 'peer':(single_ipaddr,"Tunnel peer IP address"),
246 'address':(address,"External contact address and port"),
247 'mobile':(boolean,"Site is mobile"),
251 "Simply output a property - the default case"
252 return "%s %s;\n"%(name,value)
254 # All levels support these properties
256 'contact':(lambda name,value:"# Contact email address: %s\n"%(value)),
263 'renegotiate-time':sp,
264 'restrict-nets':(lambda name,value:"# restrict-nets %s\n"%value),
268 "A level in the configuration hierarchy"
272 require_properties={}
273 def __init__(self,w):
278 def indent(self,w,t):
280 def prop_out(self,n):
281 return self.allow_properties[n](n,str(self.properties[n]))
282 def output_props(self,w,ind):
283 for i in self.properties.keys():
284 if self.allow_properties[i]:
286 w.write("%s"%self.prop_out(i))
287 def output_data(self,w,ind,np):
289 w.write("%s {\n"%(self.name))
290 self.output_props(w,ind+2)
291 if self.depth==1: w.write("\n");
292 for c in self.children.values():
293 c.output_data(w,ind+2,np+self.name+"/")
297 class vpnlevel(level):
298 "VPN level in the configuration hierarchy"
302 allow_properties=global_properties.copy()
304 'contact':"VPN admin contact address"
306 def __init__(self,w):
307 level.__init__(self,w)
308 def output_vpnflat(self,w,ind,h):
309 "Output flattened list of site names for this VPN"
311 w.write("%s {\n"%(self.name))
312 for i in self.children.keys():
313 self.children[i].output_vpnflat(w,ind+2,
314 h+"/"+self.name+"/"+i)
317 w.write("all-sites %s;\n"%
318 ','.join(self.children.keys()))
322 class locationlevel(level):
323 "Location level in the configuration hierarchy"
327 allow_properties=global_properties.copy()
329 'contact':"Location admin contact address",
331 def __init__(self,w):
332 level.__init__(self,w)
334 def output_vpnflat(self,w,ind,h):
336 # The "h=h,self=self" abomination below exists because
337 # Python didn't support nested_scopes until version 2.1
338 w.write("%s %s;\n"%(self.name,','.join(
339 map(lambda x,h=h,self=self:
340 h+"/"+x,self.children.keys()))))
342 class sitelevel(level):
343 "Site level (i.e. a leafnode) in the configuration hierarchy"
347 allow_properties=global_properties.copy()
348 allow_properties.update({
352 'pubkey':(lambda n,v:"key %s;\n"%v),
356 'dh':"Diffie-Hellman group",
357 'contact':"Site admin contact address",
358 'networks':"Networks claimed by the site",
359 'hash':"hash function",
360 'peer':"Gateway address of the site",
361 'pubkey':"RSA public key of the site",
363 def __init__(self,w):
364 level.__init__(self,w)
365 def output_data(self,w,ind,np):
367 w.write("%s {\n"%(self.name))
369 w.write("name \"%s\";\n"%(np+self.name))
370 self.output_props(w,ind+2)
372 w.write("link netlink {\n");
374 w.write("routes %s;\n"%str(self.properties["networks"]))
376 w.write("ptp-address %s;\n"%str(self.properties["peer"]))
382 # Levels in the configuration file
384 levels={'vpn':vpnlevel, 'location':locationlevel, 'site':sitelevel}
386 # Reserved vpn/location/site names
387 reserved={'all-sites':None}
388 reserved.update(keywords)
389 reserved.update(levels)
392 "Complain about a particular input line"
394 print(("%s line %d: "%(file,line))+msg)
395 complaints=complaints+1
397 "Complain about something in general"
400 complaints=complaints+1
402 root=level(['root','root']) # All vpns are children of this node
404 allow_defs=0 # Level above which new definitions are permitted
407 def set_property(obj,w):
408 "Set a property on a configuration node"
410 if prop in obj.properties:
411 obj.properties[prop].add(obj,w)
413 obj.properties[prop]=keywords[prop][0](w)
415 def pline(i,allow_include=False):
416 "Process a configuration file line"
417 global allow_defs, obstack, root
418 w=i.rstrip('\n').split()
419 if len(w)==0: return [i]
421 current=obstack[len(obstack)-1]
422 if keyword=='end-definitions':
423 allow_defs=sitelevel.depth
426 if keyword=='include':
427 if not allow_include:
428 complain("include not permitted here")
431 complain("include requires one argument")
433 newfile=os.path.join(os.path.dirname(file),w[1])
434 return pfilepath(newfile,allow_include=allow_include)
435 if keyword in levels:
436 # We may go up any number of levels, but only down by one
437 newdepth=levels[keyword].depth
438 currentdepth=len(obstack) # actually +1...
439 if newdepth<=currentdepth:
440 obstack=obstack[:newdepth]
441 if newdepth>currentdepth:
442 complain("May not go from level %d to level %d"%
443 (currentdepth-1,newdepth))
444 # See if it's a new one (and whether that's permitted)
446 current=obstack[len(obstack)-1]
448 if tname in current.children:
450 current=current.children[tname]
451 if service and group and current.depth==2:
452 if group!=current.group:
453 complain("Incorrect group!")
456 # Ignore depth check for now
457 nl=levels[keyword](w)
458 if nl.depth<allow_defs:
459 complain("New definitions not allowed at "
461 # we risk crashing if we continue
463 current.children[tname]=nl
465 obstack.append(current)
467 if keyword not in current.allow_properties:
468 complain("Property %s not allowed at %s level"%
469 (keyword,current.type))
471 elif current.depth == vpnlevel.depth < allow_defs:
472 complain("Not allowed to set VPN properties here")
475 set_property(current,w)
478 complain("unknown keyword '%s'"%(keyword))
480 def pfilepath(pathname,allow_include=False):
482 outlines=pfile(pathname,f.readlines(),allow_include=allow_include)
486 def pfile(name,lines,allow_include=False):
494 if (i[0]=='#'): continue
495 outlines += pline(i,allow_include=allow_include)
499 "Output include file for secnet configuration"
500 w.write("# secnet sites file autogenerated by make-secnet-sites "
501 +"version %s\n"%VERSION)
502 w.write("# %s\n"%time.asctime(time.localtime(time.time())))
503 w.write("# Command line: %s\n\n"%' '.join(sys.argv))
505 # Raw VPN data section of file
506 w.write(prefix+"vpn-data {\n")
507 for i in root.children.values():
508 i.output_data(w,2,"")
511 # Per-VPN flattened lists
512 w.write(prefix+"vpn {\n")
513 for i in root.children.values():
514 i.output_vpnflat(w,2,prefix+"vpn-data")
517 # Flattened list of sites
518 w.write(prefix+"all-sites %s;\n"%",".join(
519 map(lambda x:"%svpn/%s/all-sites"%(prefix,x),
520 root.children.keys())))
526 # Sanity check section
527 # Delete nodes where leaf=0 that have no children
530 "Number of leafnodes below node n"
532 for i in n.children.keys():
533 if live(n.children[i]): return 1
536 "Delete nodes that have no leafnode children"
537 for i in list(n.children.keys()):
538 delempty(n.children[i])
539 if not live(n.children[i]):
542 # Check that all constraints are met (as far as I can tell
543 # restrict-nets/networks/peer are the only special cases)
545 def checkconstraints(n,p,ra):
547 new_p.update(n.properties)
548 for i in n.require_properties.keys():
550 moan("%s %s is missing property %s"%
552 for i in new_p.keys():
553 if i not in n.allow_properties:
554 moan("%s %s has forbidden property %s"%
556 # Check address range restrictions
557 if "restrict-nets" in n.properties:
558 new_ra=ra.intersection(n.properties["restrict-nets"].set)
561 if "networks" in n.properties:
562 if not n.properties["networks"].set <= new_ra:
563 moan("%s %s networks out of bounds"%(n.type,n.name))
564 if "peer" in n.properties:
565 if not n.properties["networks"].set.contains(
566 n.properties["peer"].addr):
567 moan("%s %s peer not in networks"%(n.type,n.name))
568 for i in n.children.keys():
569 checkconstraints(n.children[i],new_p,new_ra)
572 headerinput=pfilepath(header,allow_include=True)
573 userinput=sys.stdin.readlines()
574 pfile("user input",userinput)
576 if inputfile is None:
577 pfile("stdin",sys.stdin.readlines())
582 checkconstraints(root,{},ipaddrset.complete_set())
585 if complaints==1: print("There was 1 problem.")
586 else: print("There were %d problems."%(complaints))
588 complaints=None # arranges to crash if we complain later
591 # Put the user's input into their group file, and rebuild the main
593 f=open(groupfiledir+"/T"+group,'w')
594 f.write("# Section submitted by user %s, %s\n"%
595 (user,time.asctime(time.localtime(time.time()))))
596 f.write("# Checked by make-secnet-sites version %s\n\n"%VERSION)
597 for i in userinput: f.write(i)
600 os.rename(groupfiledir+"/T"+group,groupfiledir+"/R"+group)
601 f=open(sitesfile+"-tmp",'w')
602 f.write("# sites file autogenerated by make-secnet-sites\n")
603 f.write("# generated %s, invoked by %s\n"%
604 (time.asctime(time.localtime(time.time())),user))
605 f.write("# use make-secnet-sites to turn this file into a\n")
606 f.write("# valid /etc/secnet/sites.conf file\n\n")
607 for i in headerinput: f.write(i)
608 files=os.listdir(groupfiledir)
611 j=open(groupfiledir+"/"+i)
614 f.write("# end of sites file\n")
616 os.rename(sitesfile+"-tmp",sitesfile)