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
68 # entry 0 is "near the executable", or maybe from PYTHONPATH=.,
69 # which we don't want to preempt
70 sys.path.insert(1,"/usr/local/share/secnet")
71 sys.path.insert(1,"/usr/share/secnet")
76 from sys import version_info
77 if version_info.major == 2: # for python2
79 sys.stdin = codecs.getreader('utf-8')(sys.stdin)
80 sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
82 open=lambda f,m='r': io.open(f,m,encoding='utf-8')
84 max={'rsa_bits':8200,'name':33,'dh_bits':8200}
87 def __init__(self,s,tline=None,tfile=None):
90 self._line=line if tline is None else tline
91 self._file=file if tfile is None else tfile
96 return not self.__eq__(e)
98 raise RuntimeError('direct use of Tainted value')
100 return 'Tainted(%s)' % repr(self._s)
102 def _bad(self,what,why):
103 assert(self._ok is not True)
105 complain('bad parameter: %s: %s' % (what, why))
108 def _max_ok(self,what,maxlen):
109 if len(self._s) > maxlen:
110 self._bad(what,'too long (max %d)' % maxlen)
113 def _re_ok(self,bad,what,maxlen=None):
114 if maxlen is None: maxlen=max[what]
115 self._max_ok(what,maxlen)
116 if self._ok is False: return self
117 if bad.search(self._s): return self._bad(what,'bad syntax')
120 def _rtnval(self, is_ok, ifgood, ifbad=''):
122 assert(self._ok is not False)
126 assert(self._ok is not True)
130 def _rtn(self, is_ok, ifbad=''):
131 return self._rtnval(is_ok, self._s, ifbad)
135 def raw_mark_ok(self):
136 # caller promises to throw if syntax was dangeorus
137 return self._rtn(True)
140 if self._ok is False: return ''
141 if self._ok is True: return self._s
142 print('%s:%d: unchecked/unknown additional data "%s"' %
143 (self._file,self._line,self._s),
147 bad_name=re.compile(r'^[^a-zA-Z]|[^-_0-9a-zA-Z]')
148 # secnet accepts _ at start of names, but we reserve that
151 ok=self._re_ok(Tainted.bad_name,'name')
153 '_line%d_%s' % (self._line, id(self)))
156 ok=self._s in keywords or self._s in levels
158 complain('unknown keyword %s' % self._s)
161 bad_hex=re.compile(r'[^0-9a-fA-F]')
162 def bignum_16(self,kind,what):
163 maxlen=(max[kind+'_bits']+3)/4
164 ok=self._re_ok(Tainted.bad_hex,what,maxlen)
167 bad_num=re.compile(r'[^0-9]')
168 def bignum_10(self,kind,what):
169 maxlen=math.ceil(max[kind+'_bits'] / math.log10(2))
170 ok=self._re_ok(Tainted.bad_num,what,maxlen)
173 def number(self,minn,maxx,what='number'):
175 ok=self._re_ok(Tainted.bad_num,what,10)
179 ok=self._bad(what,'out of range %d..%d'
181 return self._rtnval(ok,v,minn)
183 bad_host=re.compile(r'[^-\][_.:0-9a-zA-Z]')
184 # We permit _ so we can refer to special non-host domains
185 # which have A and AAAA RRs. This is a crude check and we may
186 # still produce config files with syntactically invalid
187 # domains or addresses, but that is OK.
189 ok=self._re_ok(Tainted.bad_host,'host/address',255)
192 bad_email=re.compile(r'[^-._0-9a-z@!$%^&*=+~/]')
193 # ^ This does not accept all valid email addresses. That's
194 # not really possible with this input syntax. It accepts
195 # all ones that don't require quoting anywhere in email
196 # protocols (and also accepts some invalid ones).
198 ok=self._re_ok(Tainted.bad_email,'email address',1023)
201 bad_groupname=re.compile(r'^[^_A-Za-z]|[^-+_0-9A-Za-z]')
203 ok=self._re_ok(Tainted.bad_groupname,'group name',64)
216 ap = argparse.ArgumentParser(description='process secnet sites files')
217 ap.add_argument('--userv', '-u', action='store_true',
218 help='userv service fragment update mode')
219 ap.add_argument('--prefix', '-P', nargs=1,
221 ap.add_argument('arg',nargs=argparse.REMAINDER)
223 #print(repr(av), file=sys.stderr)
224 service = 1 if av.userv else 0
227 print("Wrong number of arguments")
229 (header, groupfiledir, sitesfile, group) = av.arg
230 group = Tainted(group,0,'command line')
231 # untrusted argument from caller
232 if "USERV_USER" not in os.environ:
233 print("Environment variable USERV_USER not found")
235 user=os.environ["USERV_USER"]
236 # Check that group is in USERV_GROUP
237 if "USERV_GROUP" not in os.environ:
238 print("Environment variable USERV_GROUP not found")
240 ugs=os.environ["USERV_GROUP"]
242 for i in ugs.split():
245 print("caller not in group %s"%group)
249 print("Too many arguments")
251 (inputfile, outputfile) = (av.arg + [None]*2)[0:2]
252 if outputfile is None: of=sys.stdout
253 else: of=open(outputfile,'w')
257 # Classes describing possible datatypes in the configuration file
260 "Common protocol for configuration types."
262 complain("%s %s already has property %s defined"%
263 (obj.type,obj.name,w[0].raw()))
266 "A list of some kind of configuration type."
267 def __init__(self,subtype,w):
269 self.list=[subtype(w)]
271 self.list.append(self.subtype(w))
273 return ', '.join(map(str, self.list))
275 return lambda w: conflist(subtype, w)
277 class single_ipaddr (basetype):
279 def __init__(self,w):
280 self.addr=ipaddress.ip_address(w[1].raw_mark_ok())
282 return '"%s"'%self.addr
284 class networks (basetype):
285 "A set of IP addresses specified as a list of networks"
286 def __init__(self,w):
287 self.set=ipaddrset.IPAddressSet()
289 x=ipaddress.ip_network(i.raw_mark_ok(),strict=True)
292 return ",".join(map((lambda n: '"%s"'%n), self.set.networks()))
294 class dhgroup (basetype):
295 "A Diffie-Hellman group"
296 def __init__(self,w):
297 self.mod=w[1].bignum_16('dh','dh mod')
298 self.gen=w[2].bignum_16('dh','dh gen')
300 return 'diffie-hellman("%s","%s")'%(self.mod,self.gen)
302 class hash (basetype):
303 "A choice of hash function"
304 def __init__(self,w):
307 if (self.ht!='md5' and self.ht!='sha1'):
308 complain("unknown hash type %s"%(self.ht))
313 return '%s'%(self.ht)
315 class email (basetype):
317 def __init__(self,w):
318 self.addr=w[1].email()
320 return '<%s>'%(self.addr)
322 class boolean (basetype):
324 def __init__(self,w):
326 if re.match('[TtYy1]',v.raw()):
329 elif re.match('[FfNn0]',v.raw()):
333 complain("invalid boolean value");
335 return ['False','True'][self.b]
337 class num (basetype):
339 def __init__(self,w):
340 self.n=w[1].number(0,0x7fffffff)
344 class address (basetype):
345 "A DNS name and UDP port number"
346 def __init__(self,w):
348 self.port=w[2].number(1,65536,'port')
350 return '"%s"; port %d'%(self.adr,self.port)
352 class rsakey (basetype):
354 def __init__(self,w):
355 self.l=w[1].number(0,max['rsa_bits'],'rsa len')
356 self.e=w[2].bignum_10('rsa','rsa e')
357 self.n=w[3].bignum_10('rsa','rsa n')
358 if len(w) >= 5: w[4].email()
360 return 'rsa-public("%s","%s")'%(self.e,self.n)
362 # Possible properties of configuration nodes
364 'contact':(email,"Contact address"),
365 'dh':(dhgroup,"Diffie-Hellman group"),
366 'hash':(hash,"Hash function"),
367 'key-lifetime':(num,"Maximum key lifetime (ms)"),
368 'setup-timeout':(num,"Key setup timeout (ms)"),
369 'setup-retries':(num,"Maximum key setup packet retries"),
370 'wait-time':(num,"Time to wait after unsuccessful key setup (ms)"),
371 'renegotiate-time':(num,"Time after key setup to begin renegotiation (ms)"),
372 'restrict-nets':(networks,"Allowable networks"),
373 'networks':(networks,"Claimed networks"),
374 'pubkey':(rsakey,"RSA public site key"),
375 'peer':(single_ipaddr,"Tunnel peer IP address"),
376 'address':(address,"External contact address and port"),
377 'mobile':(boolean,"Site is mobile"),
381 "Simply output a property - the default case"
382 return "%s %s;\n"%(name,value)
384 # All levels support these properties
386 'contact':(lambda name,value:"# Contact email address: %s\n"%(value)),
393 'renegotiate-time':sp,
394 'restrict-nets':(lambda name,value:"# restrict-nets %s\n"%value),
398 "A level in the configuration hierarchy"
402 require_properties={}
403 def __init__(self,w):
404 self.type=w[0].keyword()
405 self.name=w[1].name()
408 def indent(self,w,t):
410 def prop_out(self,n):
411 return self.allow_properties[n](n,str(self.properties[n]))
412 def output_props(self,w,ind):
413 for i in self.properties.keys():
414 if self.allow_properties[i]:
416 w.write("%s"%self.prop_out(i))
417 def output_data(self,w,ind,np):
419 w.write("%s {\n"%(self.name))
420 self.output_props(w,ind+2)
421 if self.depth==1: w.write("\n");
422 for c in self.children.values():
423 c.output_data(w,ind+2,np+self.name+"/")
427 class vpnlevel(level):
428 "VPN level in the configuration hierarchy"
432 allow_properties=global_properties.copy()
434 'contact':"VPN admin contact address"
436 def __init__(self,w):
437 level.__init__(self,w)
438 def output_vpnflat(self,w,path):
439 "Output flattened list of site names for this VPN"
442 w.write("%s {\n"%(self.name))
443 for i in self.children.keys():
444 self.children[i].output_vpnflat(w,path+(self,))
447 w.write("all-sites %s;\n"%
448 ','.join(self.children.keys()))
452 class locationlevel(level):
453 "Location level in the configuration hierarchy"
457 allow_properties=global_properties.copy()
459 'contact':"Location admin contact address",
461 def __init__(self,w):
462 level.__init__(self,w)
463 self.group=w[2].groupname()
464 def output_vpnflat(self,w,path):
467 # The "path=path,self=self" abomination below exists because
468 # Python didn't support nested_scopes until version 2.1
471 w.write("%s %s;\n"%(self.name,','.join(
472 map(lambda x,path=path,self=self:
473 '/'.join([prefix+"vpn-data"] + list(map(
476 self.children.values()))))
478 class sitelevel(level):
479 "Site level (i.e. a leafnode) in the configuration hierarchy"
483 allow_properties=global_properties.copy()
484 allow_properties.update({
488 'pubkey':(lambda n,v:"key %s;\n"%v),
492 'dh':"Diffie-Hellman group",
493 'contact':"Site admin contact address",
494 'networks':"Networks claimed by the site",
495 'hash':"hash function",
496 'peer':"Gateway address of the site",
497 'pubkey':"RSA public key of the site",
499 def __init__(self,w):
500 level.__init__(self,w)
501 def output_data(self,w,ind,np):
503 w.write("%s {\n"%(self.name))
505 w.write("name \"%s\";\n"%(np+self.name))
506 self.output_props(w,ind+2)
508 w.write("link netlink {\n");
510 w.write("routes %s;\n"%str(self.properties["networks"]))
512 w.write("ptp-address %s;\n"%str(self.properties["peer"]))
518 # Levels in the configuration file
520 levels={'vpn':vpnlevel, 'location':locationlevel, 'site':sitelevel}
522 # Reserved vpn/location/site names
523 reserved={'all-sites':None}
524 reserved.update(keywords)
525 reserved.update(levels)
528 "Complain about a particular input line"
530 print(("%s line %d: "%(file,line))+msg)
531 complaints=complaints+1
533 "Complain about something in general"
536 complaints=complaints+1
538 class UntaintedRoot():
539 def __init__(self,s): self._s=s
540 def name(self): return self._s
541 def keyword(self): return self._s
543 root=level([UntaintedRoot(x) for x in ['root','root']])
544 # All vpns are children of this node
546 allow_defs=0 # Level above which new definitions are permitted
549 def set_property(obj,w):
550 "Set a property on a configuration node"
552 if prop.raw() in obj.properties:
553 obj.properties[prop.raw_mark_ok()].add(obj,w)
555 obj.properties[prop.raw()]=keywords[prop.raw_mark_ok()][0](w)
558 def pline(il,allow_include=False):
559 "Process a configuration file line"
560 global allow_defs, obstack, root
561 w=il.rstrip('\n').split()
562 if len(w)==0: return ['']
563 w=list([Tainted(x) for x in w])
565 current=obstack[len(obstack)-1]
566 copyout=lambda: [' '*len(obstack) +
567 ' '.join([ww.output() for ww in w]) +
569 if keyword=='end-definitions':
570 keyword.raw_mark_ok()
571 allow_defs=sitelevel.depth
574 if keyword=='include':
575 if not allow_include:
576 complain("include not permitted here")
579 complain("include requires one argument")
581 newfile=os.path.join(os.path.dirname(file),w[1].raw_mark_ok())
582 # ^ user of "include" is trusted so raw_mark_ok is good
583 return pfilepath(newfile,allow_include=allow_include)
584 if keyword.raw() in levels:
585 # We may go up any number of levels, but only down by one
586 newdepth=levels[keyword.raw_mark_ok()].depth
587 currentdepth=len(obstack) # actually +1...
588 if newdepth<=currentdepth:
589 obstack=obstack[:newdepth]
590 if newdepth>currentdepth:
591 complain("May not go from level %d to level %d"%
592 (currentdepth-1,newdepth))
593 # See if it's a new one (and whether that's permitted)
595 current=obstack[len(obstack)-1]
597 if tname in current.children:
599 current=current.children[tname]
600 if service and group and current.depth==2:
601 if group!=current.group:
602 complain("Incorrect group!")
606 # Ignore depth check for now
607 nl=levels[keyword.raw()](w)
608 if nl.depth<allow_defs:
609 complain("New definitions not allowed at "
611 # we risk crashing if we continue
613 current.children[tname]=nl
615 obstack.append(current)
617 if keyword.raw() not in current.allow_properties:
618 complain("Property %s not allowed at %s level"%
619 (keyword.raw(),current.type))
621 elif current.depth == vpnlevel.depth < allow_defs:
622 complain("Not allowed to set VPN properties here")
625 set_property(current,w)
628 complain("unknown keyword '%s'"%(keyword.raw()))
630 def pfilepath(pathname,allow_include=False):
632 outlines=pfile(pathname,f.readlines(),allow_include=allow_include)
636 def pfile(name,lines,allow_include=False):
644 if (i[0]=='#'): continue
645 outlines += pline(i,allow_include=allow_include)
649 "Output include file for secnet configuration"
650 w.write("# secnet sites file autogenerated by make-secnet-sites "
651 +"version %s\n"%VERSION)
652 w.write("# %s\n"%time.asctime(time.localtime(time.time())))
653 w.write("# Command line: %s\n\n"%' '.join(sys.argv))
655 # Raw VPN data section of file
656 w.write(prefix+"vpn-data {\n")
657 for i in root.children.values():
658 i.output_data(w,2,"")
661 # Per-VPN flattened lists
662 w.write(prefix+"vpn {\n")
663 for i in root.children.values():
664 i.output_vpnflat(w,())
667 # Flattened list of sites
668 w.write(prefix+"all-sites %s;\n"%",".join(
669 map(lambda x:"%svpn/%s/all-sites"%(prefix,x),
670 root.children.keys())))
676 # Sanity check section
677 # Delete nodes where leaf=0 that have no children
680 "Number of leafnodes below node n"
682 for i in n.children.keys():
683 if live(n.children[i]): return 1
686 "Delete nodes that have no leafnode children"
687 for i in list(n.children.keys()):
688 delempty(n.children[i])
689 if not live(n.children[i]):
692 # Check that all constraints are met (as far as I can tell
693 # restrict-nets/networks/peer are the only special cases)
695 def checkconstraints(n,p,ra):
697 new_p.update(n.properties)
698 for i in n.require_properties.keys():
700 moan("%s %s is missing property %s"%
702 for i in new_p.keys():
703 if i not in n.allow_properties:
704 moan("%s %s has forbidden property %s"%
706 # Check address range restrictions
707 if "restrict-nets" in n.properties:
708 new_ra=ra.intersection(n.properties["restrict-nets"].set)
711 if "networks" in n.properties:
712 if not n.properties["networks"].set <= new_ra:
713 moan("%s %s networks out of bounds"%(n.type,n.name))
714 if "peer" in n.properties:
715 if not n.properties["networks"].set.contains(
716 n.properties["peer"].addr):
717 moan("%s %s peer not in networks"%(n.type,n.name))
718 for i in n.children.keys():
719 checkconstraints(n.children[i],new_p,new_ra)
722 headerinput=pfilepath(header,allow_include=True)
723 userinput=sys.stdin.readlines()
724 pfile("user input",userinput)
726 if inputfile is None:
727 pfile("stdin",sys.stdin.readlines())
732 checkconstraints(root,{},ipaddrset.complete_set())
735 if complaints==1: print("There was 1 problem.")
736 else: print("There were %d problems."%(complaints))
738 complaints=None # arranges to crash if we complain later
741 # Put the user's input into their group file, and rebuild the main
743 f=open(groupfiledir+"/T"+group.groupname(),'w')
744 f.write("# Section submitted by user %s, %s\n"%
745 (user,time.asctime(time.localtime(time.time()))))
746 f.write("# Checked by make-secnet-sites version %s\n\n"%VERSION)
747 for i in userinput: f.write(i)
750 os.rename(groupfiledir+"/T"+group.groupname(),
751 groupfiledir+"/R"+group.groupname())
752 f=open(sitesfile+"-tmp",'w')
753 f.write("# sites file autogenerated by make-secnet-sites\n")
754 f.write("# generated %s, invoked by %s\n"%
755 (time.asctime(time.localtime(time.time())),user))
756 f.write("# use make-secnet-sites to turn this file into a\n")
757 f.write("# valid /etc/secnet/sites.conf file\n\n")
758 for i in headerinput: f.write(i)
759 files=os.listdir(groupfiledir)
762 j=open(groupfiledir+"/"+i)
765 f.write("# end of sites file\n")
767 os.rename(sitesfile+"-tmp",sitesfile)