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")
75 from argparseactionnoyes import ActionNoYes
81 from sys import version_info
82 if version_info.major == 2: # for python2
84 sys.stdin = codecs.getreader('utf-8')(sys.stdin)
85 sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
87 open=lambda f,m='r': io.open(f,m,encoding='utf-8')
89 max={'rsa_bits':8200,'name':33,'dh_bits':8200,'algname':127}
93 print(repr(args), file=sys.stderr)
95 def base91s_encode(bindata):
96 return base91.encode(bindata).replace('"',"-")
98 def base91s_decode(string):
99 return base91.decode(string.replace("-",'"'))
102 def __init__(self,s,tline=None,tfile=None):
105 self._line=line if tline is None else tline
106 self._file=file if tfile is None else tfile
111 return not self.__eq__(e)
113 raise RuntimeError('direct use of Tainted value')
115 return 'Tainted(%s)' % repr(self._s)
117 def _bad(self,what,why):
118 assert(self._ok is not True)
120 complain('bad parameter: %s: %s' % (what, why))
123 def _max_ok(self,what,maxlen):
124 if len(self._s) > maxlen:
125 return self._bad(what,'too long (max %d)' % maxlen)
128 def _re_ok(self,bad,what,maxlen=None):
129 if maxlen is None: maxlen=max[what]
130 self._max_ok(what,maxlen)
131 if self._ok is False: return False
132 if bad.search(self._s):
133 #print(repr(self), file=sys.stderr)
134 return self._bad(what,'bad syntax')
137 def _rtnval(self, is_ok, ifgood, ifbad=''):
139 assert(self._ok is not False)
143 assert(self._ok is not True)
147 def _rtn(self, is_ok, ifbad=''):
148 return self._rtnval(is_ok, self._s, ifbad)
152 def raw_mark_ok(self):
153 # caller promises to throw if syntax was dangeorus
154 return self._rtn(True)
157 if self._ok is False: return ''
158 if self._ok is True: return self._s
159 print('%s:%d: unchecked/unknown additional data "%s"' %
160 (self._file,self._line,self._s),
164 bad_name=re.compile(r'^[^a-zA-Z]|[^-_0-9a-zA-Z]')
165 # secnet accepts _ at start of names, but we reserve that
167 def name(self,what='name'):
168 ok=self._re_ok(Tainted.bad_name,what)
170 '_line%d_%s' % (self._line, id(self)))
173 ok=self._s in keywords or self._s in levels
175 complain('unknown keyword %s' % self._s)
178 bad_hex=re.compile(r'[^0-9a-fA-F]')
179 def bignum_16(self,kind,what):
180 maxlen=(max[kind+'_bits']+3)/4
181 ok=self._re_ok(Tainted.bad_hex,what,maxlen)
184 bad_num=re.compile(r'[^0-9]')
185 def bignum_10(self,kind,what):
186 maxlen=math.ceil(max[kind+'_bits'] / math.log10(2))
187 ok=self._re_ok(Tainted.bad_num,what,maxlen)
190 def number(self,minn,maxx,what='number'):
192 ok=self._re_ok(Tainted.bad_num,what,10)
196 ok=self._bad(what,'out of range %d..%d'
198 return self._rtnval(ok,v,minn)
200 def hexid(self,byteslen,what):
201 ok=self._re_ok(Tainted.bad_hex,what,byteslen*2)
203 if len(self._s) < byteslen*2:
204 ok=self._bad(what,'too short')
205 return self._rtn(ok,ifbad='00'*byteslen)
207 bad_host=re.compile(r'[^-\][_.:0-9a-zA-Z]')
208 # We permit _ so we can refer to special non-host domains
209 # which have A and AAAA RRs. This is a crude check and we may
210 # still produce config files with syntactically invalid
211 # domains or addresses, but that is OK.
213 ok=self._re_ok(Tainted.bad_host,'host/address',255)
216 bad_email=re.compile(r'[^-._0-9a-z@!$%^&*=+~/]')
217 # ^ This does not accept all valid email addresses. That's
218 # not really possible with this input syntax. It accepts
219 # all ones that don't require quoting anywhere in email
220 # protocols (and also accepts some invalid ones).
222 ok=self._re_ok(Tainted.bad_email,'email address',1023)
225 bad_groupname=re.compile(r'^[^_A-Za-z]|[^-+_0-9A-Za-z]')
227 ok=self._re_ok(Tainted.bad_groupname,'group name',64)
230 bad_base91=re.compile(r'[^!-~]|[\'\"\\]')
231 def base91(self,what='base91'):
232 ok=self._re_ok(Tainted.bad_base91,what,4096)
235 class ArgActionLambda(argparse.Action):
236 def __init__(self, fn, **kwargs):
238 argparse.Action.__init__(self,**kwargs)
239 def __call__(self,ap,ns,values,option_string):
240 self.fn(values,ns,ap,option_string)
255 global output_version
257 ap = argparse.ArgumentParser(description='process secnet sites files')
258 ap.add_argument('--userv', '-u', action='store_true',
259 help='userv service fragment update mode')
260 ap.add_argument('--conf-key-prefix', action=ActionNoYes,
262 help='prefix conf file key names derived from sites data')
263 ap.add_argument('--output-version', nargs=1, type=int,
264 help='sites file output version',
265 default=[max_version])
266 ap.add_argument('--prefix', '-P', nargs=1,
268 ap.add_argument('--debug', '-D', action='count', default=0)
269 ap.add_argument('arg',nargs=argparse.REMAINDER)
271 debug_level = av.debug
273 service = 1 if av.userv else 0
274 prefix = '' if av.prefix is None else av.prefix[0]
275 key_prefix = av.conf_key_prefix
276 output_version = av.output_version[0]
279 print("Wrong number of arguments")
281 (header, groupfiledir, sitesfile, group) = av.arg
282 group = Tainted(group,0,'command line')
283 # untrusted argument from caller
284 if "USERV_USER" not in os.environ:
285 print("Environment variable USERV_USER not found")
287 user=os.environ["USERV_USER"]
288 # Check that group is in USERV_GROUP
289 if "USERV_GROUP" not in os.environ:
290 print("Environment variable USERV_GROUP not found")
292 ugs=os.environ["USERV_GROUP"]
294 for i in ugs.split():
297 print("caller not in group %s"%group)
301 print("Too many arguments")
303 (inputfile, outputfile) = (av.arg + [None]*2)[0:2]
307 # Classes describing possible datatypes in the configuration file
310 "Common protocol for configuration types."
312 complain("%s %s already has property %s defined"%
313 (obj.type,obj.name,w[0].raw()))
314 def forsites(self,version,copy,fs):
318 "A list of some kind of configuration type."
319 def __init__(self,subtype,w):
321 self.list=[subtype(w)]
323 self.list.append(self.subtype(w))
325 return ', '.join(map(str, self.list))
326 def forsites(self,version,copy,fs):
327 most_recent=self.list[len(self.list)-1]
328 return most_recent.forsites(version,copy,fs)
330 return lambda w: conflist(subtype, w)
332 class single_ipaddr (basetype):
334 def __init__(self,w):
335 self.addr=ipaddress.ip_address(w[1].raw_mark_ok())
337 return '"%s"'%self.addr
339 class networks (basetype):
340 "A set of IP addresses specified as a list of networks"
341 def __init__(self,w):
342 self.set=ipaddrset.IPAddressSet()
344 x=ipaddress.ip_network(i.raw_mark_ok(),strict=True)
347 return ",".join(map((lambda n: '"%s"'%n), self.set.networks()))
349 class dhgroup (basetype):
350 "A Diffie-Hellman group"
351 def __init__(self,w):
352 self.mod=w[1].bignum_16('dh','dh mod')
353 self.gen=w[2].bignum_16('dh','dh gen')
355 return 'diffie-hellman("%s","%s")'%(self.mod,self.gen)
357 class hash (basetype):
358 "A choice of hash function"
359 def __init__(self,w):
362 if (self.ht!='md5' and self.ht!='sha1'):
363 complain("unknown hash type %s"%(self.ht))
368 return '%s'%(self.ht)
370 class email (basetype):
372 def __init__(self,w):
373 self.addr=w[1].email()
375 return '<%s>'%(self.addr)
377 class boolean (basetype):
379 def __init__(self,w):
381 if re.match('[TtYy1]',v.raw()):
384 elif re.match('[FfNn0]',v.raw()):
388 complain("invalid boolean value");
390 return ['False','True'][self.b]
392 class num (basetype):
394 def __init__(self,w):
395 self.n=w[1].number(0,0x7fffffff)
399 class serial (basetype):
400 def __init__(self,w):
401 self.i=w[1].hexid(4,'serial')
404 def forsites(self,version,copy,fs):
405 if version < 2: return []
408 class address (basetype):
409 "A DNS name and UDP port number"
410 def __init__(self,w):
412 self.port=w[2].number(1,65536,'port')
414 return '"%s"; port %d'%(self.adr,self.port)
416 class pubkey (basetype):
417 "Some kind of publie key"
418 def __init__(self,w):
419 self.a=w[1].name('algname')
420 self.d=w[2].base91();
422 return 'make-public("%s","%s")'%(self.a,self.d)
424 class rsakey (pubkey):
426 def __init__(self,w):
427 self.l=w[1].number(0,max['rsa_bits'],'rsa len')
428 self.e=w[2].bignum_10('rsa','rsa e')
429 self.n=w[3].bignum_10('rsa','rsa n')
430 if len(w) >= 5: w[4].email()
432 return 'rsa-public("%s","%s")'%(self.e,self.n)
433 # this specialisation means we can generate files
434 # compatible with old secnet executables
444 # Possible properties of configuration nodes
446 'contact':(email,"Contact address"),
447 'dh':(dhgroup,"Diffie-Hellman group"),
448 'hash':(hash,"Hash function"),
449 'key-lifetime':(num,"Maximum key lifetime (ms)"),
450 'setup-timeout':(num,"Key setup timeout (ms)"),
451 'setup-retries':(num,"Maximum key setup packet retries"),
452 'wait-time':(num,"Time to wait after unsuccessful key setup (ms)"),
453 'renegotiate-time':(num,"Time after key setup to begin renegotiation (ms)"),
454 'restrict-nets':(networks,"Allowable networks"),
455 'networks':(networks,"Claimed networks"),
456 'pub':(listof(somepubkey),"new style public site key"),
457 'pubkey':(listof(somepubkey),"RSA public site key",'pub'),
458 'peer':(single_ipaddr,"Tunnel peer IP address"),
459 'address':(address,"External contact address and port"),
460 'mobile':(boolean,"Site is mobile"),
464 "Simply output a property - the default case"
465 return "%s %s;\n"%(name,value)
467 # All levels support these properties
469 'contact':(lambda name,value:"# Contact email address: %s\n"%(value)),
476 'renegotiate-time':sp,
477 'restrict-nets':(lambda name,value:"# restrict-nets %s\n"%value),
481 "A level in the configuration hierarchy"
485 require_properties={}
486 def __init__(self,w):
487 self.type=w[0].keyword()
488 self.name=w[1].name()
491 def indent(self,w,t):
493 def prop_out(self,n):
494 return self.allow_properties[n](n,str(self.properties[n]))
495 def output_props(self,w,ind):
496 for i in sorted(self.properties.keys()):
497 if self.allow_properties[i]:
499 w.write("%s"%self.prop_out(i))
501 return ((self.type[0].upper() if key_prefix else '')
503 def output_data(self,w,path):
506 w.write("%s {\n"%(self.kname()))
507 self.output_props(w,ind+2)
508 if self.depth==1: w.write("\n");
509 for k in sorted(self.children.keys()):
511 c.output_data(w,path+(c,))
515 class vpnlevel(level):
516 "VPN level in the configuration hierarchy"
520 allow_properties=global_properties.copy()
522 'contact':"VPN admin contact address"
524 def __init__(self,w):
525 level.__init__(self,w)
526 def output_vpnflat(self,w,path):
527 "Output flattened list of site names for this VPN"
530 w.write("%s {\n"%(self.kname()))
531 for i in self.children.keys():
532 self.children[i].output_vpnflat(w,path+(self,))
535 w.write("all-sites %s;\n"%
536 ','.join(map(lambda i: i.kname(),
537 self.children.values())))
541 class locationlevel(level):
542 "Location level in the configuration hierarchy"
546 allow_properties=global_properties.copy()
548 'contact':"Location admin contact address",
550 def __init__(self,w):
551 level.__init__(self,w)
552 self.group=w[2].groupname()
553 def output_vpnflat(self,w,path):
556 # The "path=path,self=self" abomination below exists because
557 # Python didn't support nested_scopes until version 2.1
560 w.write("%s %s;\n"%(self.kname(),','.join(
561 map(lambda x,path=path,self=self:
562 '/'.join([prefix+"vpn-data"] + list(map(
565 self.children.values()))))
567 class sitelevel(level):
568 "Site level (i.e. a leafnode) in the configuration hierarchy"
572 allow_properties=global_properties.copy()
573 allow_properties.update({
582 'dh':"Diffie-Hellman group",
583 'contact':"Site admin contact address",
584 'networks':"Networks claimed by the site",
585 'hash':"hash function",
586 'peer':"Gateway address of the site",
587 'pub':"public key of the site",
589 def __init__(self,w):
590 level.__init__(self,w)
591 def output_data(self,w,path):
593 np='/'.join(map(lambda i: i.name, path))
595 w.write("%s {\n"%(self.kname()))
597 w.write("name \"%s\";\n"%(np,))
599 w.write("key %s;\n"%str(self.properties["pub"].list[0]))
600 self.output_props(w,ind+2)
602 w.write("link netlink {\n");
604 w.write("routes %s;\n"%str(self.properties["networks"]))
606 w.write("ptp-address %s;\n"%str(self.properties["peer"]))
612 # Levels in the configuration file
614 levels={'vpn':vpnlevel, 'location':locationlevel, 'site':sitelevel}
617 "Complain about a particular input line"
618 moan(("%s line %d: "%(file,line))+msg)
620 "Complain about something in general"
623 if complaints is None: sys.exit(1)
624 complaints=complaints+1
626 class UntaintedRoot():
627 def __init__(self,s): self._s=s
628 def name(self): return self._s
629 def keyword(self): return self._s
631 root=level([UntaintedRoot(x) for x in ['root','root']])
632 # All vpns are children of this node
634 allow_defs=0 # Level above which new definitions are permitted
636 def set_property(obj,w):
637 "Set a property on a configuration node"
639 propname=prop.raw_mark_ok()
640 kw=keywords[propname]
641 if len(kw) >= 3: propname=kw[2] # for aliases
642 if propname in obj.properties:
643 obj.properties[propname].add(obj,w)
645 obj.properties[propname]=kw[0](w)
646 return obj.properties[propname]
652 # called when we enter a new node,
653 # in particular, at the start of each site
656 def pline(il,filterstate,allow_include=False):
657 "Process a configuration file line"
658 global allow_defs, obstack, root
659 w=il.rstrip('\n').split()
660 if len(w)==0: return ['']
661 w=list([Tainted(x) for x in w])
663 current=obstack[len(obstack)-1]
664 copyout_core=lambda: ' '.join([ww.output() for ww in w])
665 indent=' '*len(obstack)
666 copyout=lambda: [indent + copyout_core() + '\n']
667 if keyword=='end-definitions':
668 keyword.raw_mark_ok()
669 allow_defs=sitelevel.depth
672 if keyword=='include':
673 if not allow_include:
674 complain("include not permitted here")
677 complain("include requires one argument")
679 newfile=os.path.join(os.path.dirname(file),w[1].raw_mark_ok())
680 # ^ user of "include" is trusted so raw_mark_ok is good
681 return pfilepath(newfile,allow_include=allow_include)
682 if keyword.raw() in levels:
683 # We may go up any number of levels, but only down by one
684 newdepth=levels[keyword.raw_mark_ok()].depth
685 currentdepth=len(obstack) # actually +1...
686 if newdepth<=currentdepth:
687 obstack=obstack[:newdepth]
688 if newdepth>currentdepth:
689 complain("May not go from level %d to level %d"%
690 (currentdepth-1,newdepth))
691 # See if it's a new one (and whether that's permitted)
693 current=obstack[len(obstack)-1]
695 if tname in current.children:
697 current=current.children[tname]
698 if service and group and current.depth==2:
699 if group!=current.group:
700 complain("Incorrect group!")
704 # Ignore depth check for now
705 nl=levels[keyword.raw()](w)
706 if nl.depth<allow_defs:
707 complain("New definitions not allowed at "
709 # we risk crashing if we continue
711 current.children[tname]=nl
714 obstack.append(current)
716 if keyword.raw() not in current.allow_properties:
717 complain("Property %s not allowed at %s level"%
718 (keyword.raw(),current.type))
720 elif current.depth == vpnlevel.depth < allow_defs:
721 complain("Not allowed to set VPN properties here")
724 prop=set_property(current,w)
726 out=prop.forsites(output_version,out,filterstate)
727 if len(out)==0: return [indent + '#', copyout_core(), '\n']
728 return [indent + ' '.join(out) + '\n']
730 complain("unknown keyword '%s'"%(keyword.raw()))
732 def pfilepath(pathname,allow_include=False):
734 outlines=pfile(pathname,f.readlines(),allow_include=allow_include)
738 def pfile(name,lines,allow_include=False):
744 filterstate = FilterState()
747 if (i[0]=='#'): continue
748 outlines += pline(i,filterstate,allow_include=allow_include)
752 "Output include file for secnet configuration"
753 w.write("# secnet sites file autogenerated by make-secnet-sites "
754 +"version %s\n"%VERSION)
755 w.write("# %s\n"%time.asctime(time.localtime(time.time())))
756 w.write("# Command line: %s\n\n"%' '.join(sys.argv))
758 # Raw VPN data section of file
759 w.write(prefix+"vpn-data {\n")
760 for i in root.children.values():
761 i.output_data(w,(i,))
764 # Per-VPN flattened lists
765 w.write(prefix+"vpn {\n")
766 for i in root.children.values():
767 i.output_vpnflat(w,())
770 # Flattened list of sites
771 w.write(prefix+"all-sites %s;\n"%",".join(
772 map(lambda x:"%svpn/%s/all-sites"%(prefix,x.kname()),
773 root.children.values())))
779 # Sanity check section
780 # Delete nodes where leaf=0 that have no children
783 "Number of leafnodes below node n"
785 for i in n.children.keys():
786 if live(n.children[i]): return 1
789 "Delete nodes that have no leafnode children"
790 for i in list(n.children.keys()):
791 delempty(n.children[i])
792 if not live(n.children[i]):
795 # Check that all constraints are met (as far as I can tell
796 # restrict-nets/networks/peer are the only special cases)
798 def checkconstraints(n,p,ra):
800 new_p.update(n.properties)
801 for i in n.require_properties.keys():
803 moan("%s %s is missing property %s"%
805 for i in new_p.keys():
806 if i not in n.allow_properties:
807 moan("%s %s has forbidden property %s"%
809 # Check address range restrictions
810 if "restrict-nets" in n.properties:
811 new_ra=ra.intersection(n.properties["restrict-nets"].set)
814 if "networks" in n.properties:
815 if not n.properties["networks"].set <= new_ra:
816 moan("%s %s networks out of bounds"%(n.type,n.name))
817 if "peer" in n.properties:
818 if not n.properties["networks"].set.contains(
819 n.properties["peer"].addr):
820 moan("%s %s peer not in networks"%(n.type,n.name))
821 for i in n.children.keys():
822 checkconstraints(n.children[i],new_p,new_ra)
825 headerinput=pfilepath(header,allow_include=True)
826 userinput=sys.stdin.readlines()
827 pfile("user input",userinput)
829 if inputfile is None:
830 pfile("stdin",sys.stdin.readlines())
835 checkconstraints(root,{},ipaddrset.complete_set())
838 if complaints==1: print("There was 1 problem.")
839 else: print("There were %d problems."%(complaints))
841 complaints=None # arranges to crash if we complain later
844 # Put the user's input into their group file, and rebuild the main
846 f=open(groupfiledir+"/T"+group.groupname(),'w')
847 f.write("# Section submitted by user %s, %s\n"%
848 (user,time.asctime(time.localtime(time.time()))))
849 f.write("# Checked by make-secnet-sites version %s\n\n"%VERSION)
850 for i in userinput: f.write(i)
853 os.rename(groupfiledir+"/T"+group.groupname(),
854 groupfiledir+"/R"+group.groupname())
855 f=open(sitesfile+"-tmp",'w')
856 f.write("# sites file autogenerated by make-secnet-sites\n")
857 f.write("# generated %s, invoked by %s\n"%
858 (time.asctime(time.localtime(time.time())),user))
859 f.write("# use make-secnet-sites to turn this file into a\n")
860 f.write("# valid /etc/secnet/sites.conf file\n\n")
861 for i in headerinput: f.write(i)
862 files=os.listdir(groupfiledir)
865 j=open(groupfiledir+"/"+i)
868 f.write("# end of sites file\n")
870 os.rename(sitesfile+"-tmp",sitesfile)
872 if outputfile is None:
875 tmp_outputfile=outputfile+'~tmp~'
876 of=open(tmp_outputfile,'w')
878 if outputfile is not None:
879 os.rename(tmp_outputfile,outputfile)