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")
74 from argparseactionnoyes import ActionNoYes
78 from sys import version_info
79 if version_info.major == 2: # for python2
81 sys.stdin = codecs.getreader('utf-8')(sys.stdin)
82 sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
84 open=lambda f,m='r': io.open(f,m,encoding='utf-8')
86 max={'rsa_bits':8200,'name':33,'dh_bits':8200}
89 def __init__(self,s,tline=None,tfile=None):
92 self._line=line if tline is None else tline
93 self._file=file if tfile is None else tfile
98 return not self.__eq__(e)
100 raise RuntimeError('direct use of Tainted value')
102 return 'Tainted(%s)' % repr(self._s)
104 def _bad(self,what,why):
105 assert(self._ok is not True)
107 complain('bad parameter: %s: %s' % (what, why))
110 def _max_ok(self,what,maxlen):
111 if len(self._s) > maxlen:
112 return self._bad(what,'too long (max %d)' % maxlen)
115 def _re_ok(self,bad,what,maxlen=None):
116 if maxlen is None: maxlen=max[what]
117 self._max_ok(what,maxlen)
118 if self._ok is False: return False
119 if bad.search(self._s): return self._bad(what,'bad syntax')
122 def _rtnval(self, is_ok, ifgood, ifbad=''):
124 assert(self._ok is not False)
128 assert(self._ok is not True)
132 def _rtn(self, is_ok, ifbad=''):
133 return self._rtnval(is_ok, self._s, ifbad)
137 def raw_mark_ok(self):
138 # caller promises to throw if syntax was dangeorus
139 return self._rtn(True)
142 if self._ok is False: return ''
143 if self._ok is True: return self._s
144 print('%s:%d: unchecked/unknown additional data "%s"' %
145 (self._file,self._line,self._s),
149 bad_name=re.compile(r'^[^a-zA-Z]|[^-_0-9a-zA-Z]')
150 # secnet accepts _ at start of names, but we reserve that
152 def name(self,what='name'):
153 ok=self._re_ok(Tainted.bad_name,what)
155 '_line%d_%s' % (self._line, id(self)))
158 ok=self._s in keywords or self._s in levels
160 complain('unknown keyword %s' % self._s)
163 bad_hex=re.compile(r'[^0-9a-fA-F]')
164 def bignum_16(self,kind,what):
165 maxlen=(max[kind+'_bits']+3)/4
166 ok=self._re_ok(Tainted.bad_hex,what,maxlen)
169 bad_num=re.compile(r'[^0-9]')
170 def bignum_10(self,kind,what):
171 maxlen=math.ceil(max[kind+'_bits'] / math.log10(2))
172 ok=self._re_ok(Tainted.bad_num,what,maxlen)
175 def number(self,minn,maxx,what='number'):
177 ok=self._re_ok(Tainted.bad_num,what,10)
181 ok=self._bad(what,'out of range %d..%d'
183 return self._rtnval(ok,v,minn)
185 def hexid(self,byteslen,what):
186 ok=self._re_ok(Tainted.bad_hex,what,byteslen*2)
188 if len(self._s) < byteslen*2:
189 ok=self._bad(what,'too short')
190 return self._rtn(ok,ifbad='00'*byteslen)
192 bad_host=re.compile(r'[^-\][_.:0-9a-zA-Z]')
193 # We permit _ so we can refer to special non-host domains
194 # which have A and AAAA RRs. This is a crude check and we may
195 # still produce config files with syntactically invalid
196 # domains or addresses, but that is OK.
198 ok=self._re_ok(Tainted.bad_host,'host/address',255)
201 bad_email=re.compile(r'[^-._0-9a-z@!$%^&*=+~/]')
202 # ^ This does not accept all valid email addresses. That's
203 # not really possible with this input syntax. It accepts
204 # all ones that don't require quoting anywhere in email
205 # protocols (and also accepts some invalid ones).
207 ok=self._re_ok(Tainted.bad_email,'email address',1023)
210 bad_groupname=re.compile(r'^[^_A-Za-z]|[^-+_0-9A-Za-z]')
212 ok=self._re_ok(Tainted.bad_groupname,'group name',64)
215 bad_base91=re.compile(r'[^!-~]|[\'\"\\]')
216 def base91(self,what='base91'):
217 ok=self._re_ok(Tainted.bad_base91,what,4096)
233 ap = argparse.ArgumentParser(description='process secnet sites files')
234 ap.add_argument('--userv', '-u', action='store_true',
235 help='userv service fragment update mode')
236 ap.add_argument('--conf-key-prefix', action=ActionNoYes,
238 help='prefix conf file key names derived from sites data')
239 ap.add_argument('--prefix', '-P', nargs=1,
241 ap.add_argument('arg',nargs=argparse.REMAINDER)
243 #print(repr(av), file=sys.stderr)
244 service = 1 if av.userv else 0
245 prefix = '' if av.prefix is None else av.prefix[0]
246 key_prefix = av.conf_key_prefix
249 print("Wrong number of arguments")
251 (header, groupfiledir, sitesfile, group) = av.arg
252 group = Tainted(group,0,'command line')
253 # untrusted argument from caller
254 if "USERV_USER" not in os.environ:
255 print("Environment variable USERV_USER not found")
257 user=os.environ["USERV_USER"]
258 # Check that group is in USERV_GROUP
259 if "USERV_GROUP" not in os.environ:
260 print("Environment variable USERV_GROUP not found")
262 ugs=os.environ["USERV_GROUP"]
264 for i in ugs.split():
267 print("caller not in group %s"%group)
271 print("Too many arguments")
273 (inputfile, outputfile) = (av.arg + [None]*2)[0:2]
277 # Classes describing possible datatypes in the configuration file
280 "Common protocol for configuration types."
282 complain("%s %s already has property %s defined"%
283 (obj.type,obj.name,w[0].raw()))
286 "A list of some kind of configuration type."
287 def __init__(self,subtype,w):
289 self.list=[subtype(w)]
291 self.list.append(self.subtype(w))
293 return ', '.join(map(str, self.list))
295 return lambda w: conflist(subtype, w)
297 class single_ipaddr (basetype):
299 def __init__(self,w):
300 self.addr=ipaddress.ip_address(w[1].raw_mark_ok())
302 return '"%s"'%self.addr
304 class networks (basetype):
305 "A set of IP addresses specified as a list of networks"
306 def __init__(self,w):
307 self.set=ipaddrset.IPAddressSet()
309 x=ipaddress.ip_network(i.raw_mark_ok(),strict=True)
312 return ",".join(map((lambda n: '"%s"'%n), self.set.networks()))
314 class dhgroup (basetype):
315 "A Diffie-Hellman group"
316 def __init__(self,w):
317 self.mod=w[1].bignum_16('dh','dh mod')
318 self.gen=w[2].bignum_16('dh','dh gen')
320 return 'diffie-hellman("%s","%s")'%(self.mod,self.gen)
322 class hash (basetype):
323 "A choice of hash function"
324 def __init__(self,w):
327 if (self.ht!='md5' and self.ht!='sha1'):
328 complain("unknown hash type %s"%(self.ht))
333 return '%s'%(self.ht)
335 class email (basetype):
337 def __init__(self,w):
338 self.addr=w[1].email()
340 return '<%s>'%(self.addr)
342 class boolean (basetype):
344 def __init__(self,w):
346 if re.match('[TtYy1]',v.raw()):
349 elif re.match('[FfNn0]',v.raw()):
353 complain("invalid boolean value");
355 return ['False','True'][self.b]
357 class num (basetype):
359 def __init__(self,w):
360 self.n=w[1].number(0,0x7fffffff)
364 class address (basetype):
365 "A DNS name and UDP port number"
366 def __init__(self,w):
368 self.port=w[2].number(1,65536,'port')
370 return '"%s"; port %d'%(self.adr,self.port)
372 class rsakey (basetype):
374 def __init__(self,w):
375 self.l=w[1].number(0,max['rsa_bits'],'rsa len')
376 self.e=w[2].bignum_10('rsa','rsa e')
377 self.n=w[3].bignum_10('rsa','rsa n')
378 if len(w) >= 5: w[4].email()
380 return 'rsa-public("%s","%s")'%(self.e,self.n)
382 # Possible properties of configuration nodes
384 'contact':(email,"Contact address"),
385 'dh':(dhgroup,"Diffie-Hellman group"),
386 'hash':(hash,"Hash function"),
387 'key-lifetime':(num,"Maximum key lifetime (ms)"),
388 'setup-timeout':(num,"Key setup timeout (ms)"),
389 'setup-retries':(num,"Maximum key setup packet retries"),
390 'wait-time':(num,"Time to wait after unsuccessful key setup (ms)"),
391 'renegotiate-time':(num,"Time after key setup to begin renegotiation (ms)"),
392 'restrict-nets':(networks,"Allowable networks"),
393 'networks':(networks,"Claimed networks"),
394 'pubkey':(rsakey,"RSA public site key"),
395 'peer':(single_ipaddr,"Tunnel peer IP address"),
396 'address':(address,"External contact address and port"),
397 'mobile':(boolean,"Site is mobile"),
401 "Simply output a property - the default case"
402 return "%s %s;\n"%(name,value)
404 # All levels support these properties
406 'contact':(lambda name,value:"# Contact email address: %s\n"%(value)),
413 'renegotiate-time':sp,
414 'restrict-nets':(lambda name,value:"# restrict-nets %s\n"%value),
418 "A level in the configuration hierarchy"
422 require_properties={}
423 def __init__(self,w):
424 self.type=w[0].keyword()
425 self.name=w[1].name()
428 def indent(self,w,t):
430 def prop_out(self,n):
431 return self.allow_properties[n](n,str(self.properties[n]))
432 def output_props(self,w,ind):
433 for i in sorted(self.properties.keys()):
434 if self.allow_properties[i]:
436 w.write("%s"%self.prop_out(i))
438 return ((self.type[0].upper() if key_prefix else '')
440 def output_data(self,w,path):
443 w.write("%s {\n"%(self.kname()))
444 self.output_props(w,ind+2)
445 if self.depth==1: w.write("\n");
446 for k in sorted(self.children.keys()):
448 c.output_data(w,path+(c,))
452 class vpnlevel(level):
453 "VPN level in the configuration hierarchy"
457 allow_properties=global_properties.copy()
459 'contact':"VPN admin contact address"
461 def __init__(self,w):
462 level.__init__(self,w)
463 def output_vpnflat(self,w,path):
464 "Output flattened list of site names for this VPN"
467 w.write("%s {\n"%(self.kname()))
468 for i in self.children.keys():
469 self.children[i].output_vpnflat(w,path+(self,))
472 w.write("all-sites %s;\n"%
473 ','.join(map(lambda i: i.kname(),
474 self.children.values())))
478 class locationlevel(level):
479 "Location level in the configuration hierarchy"
483 allow_properties=global_properties.copy()
485 'contact':"Location admin contact address",
487 def __init__(self,w):
488 level.__init__(self,w)
489 self.group=w[2].groupname()
490 def output_vpnflat(self,w,path):
493 # The "path=path,self=self" abomination below exists because
494 # Python didn't support nested_scopes until version 2.1
497 w.write("%s %s;\n"%(self.kname(),','.join(
498 map(lambda x,path=path,self=self:
499 '/'.join([prefix+"vpn-data"] + list(map(
502 self.children.values()))))
504 class sitelevel(level):
505 "Site level (i.e. a leafnode) in the configuration hierarchy"
509 allow_properties=global_properties.copy()
510 allow_properties.update({
514 'pubkey':(lambda n,v:"key %s;\n"%v),
518 'dh':"Diffie-Hellman group",
519 'contact':"Site admin contact address",
520 'networks':"Networks claimed by the site",
521 'hash':"hash function",
522 'peer':"Gateway address of the site",
523 'pubkey':"RSA public key of the site",
525 def __init__(self,w):
526 level.__init__(self,w)
527 def output_data(self,w,path):
529 np='/'.join(map(lambda i: i.name, path))
531 w.write("%s {\n"%(self.kname()))
533 w.write("name \"%s\";\n"%(np,))
534 self.output_props(w,ind+2)
536 w.write("link netlink {\n");
538 w.write("routes %s;\n"%str(self.properties["networks"]))
540 w.write("ptp-address %s;\n"%str(self.properties["peer"]))
546 # Levels in the configuration file
548 levels={'vpn':vpnlevel, 'location':locationlevel, 'site':sitelevel}
551 "Complain about a particular input line"
552 moan(("%s line %d: "%(file,line))+msg)
554 "Complain about something in general"
557 if complaints is None: sys.exit(1)
558 complaints=complaints+1
560 class UntaintedRoot():
561 def __init__(self,s): self._s=s
562 def name(self): return self._s
563 def keyword(self): return self._s
565 root=level([UntaintedRoot(x) for x in ['root','root']])
566 # All vpns are children of this node
568 allow_defs=0 # Level above which new definitions are permitted
570 def set_property(obj,w):
571 "Set a property on a configuration node"
573 if prop.raw() in obj.properties:
574 obj.properties[prop.raw_mark_ok()].add(obj,w)
576 obj.properties[prop.raw()]=keywords[prop.raw_mark_ok()][0](w)
579 def pline(il,allow_include=False):
580 "Process a configuration file line"
581 global allow_defs, obstack, root
582 w=il.rstrip('\n').split()
583 if len(w)==0: return ['']
584 w=list([Tainted(x) for x in w])
586 current=obstack[len(obstack)-1]
587 copyout=lambda: [' '*len(obstack) +
588 ' '.join([ww.output() for ww in w]) +
590 if keyword=='end-definitions':
591 keyword.raw_mark_ok()
592 allow_defs=sitelevel.depth
595 if keyword=='include':
596 if not allow_include:
597 complain("include not permitted here")
600 complain("include requires one argument")
602 newfile=os.path.join(os.path.dirname(file),w[1].raw_mark_ok())
603 # ^ user of "include" is trusted so raw_mark_ok is good
604 return pfilepath(newfile,allow_include=allow_include)
605 if keyword.raw() in levels:
606 # We may go up any number of levels, but only down by one
607 newdepth=levels[keyword.raw_mark_ok()].depth
608 currentdepth=len(obstack) # actually +1...
609 if newdepth<=currentdepth:
610 obstack=obstack[:newdepth]
611 if newdepth>currentdepth:
612 complain("May not go from level %d to level %d"%
613 (currentdepth-1,newdepth))
614 # See if it's a new one (and whether that's permitted)
616 current=obstack[len(obstack)-1]
618 if tname in current.children:
620 current=current.children[tname]
621 if service and group and current.depth==2:
622 if group!=current.group:
623 complain("Incorrect group!")
627 # Ignore depth check for now
628 nl=levels[keyword.raw()](w)
629 if nl.depth<allow_defs:
630 complain("New definitions not allowed at "
632 # we risk crashing if we continue
634 current.children[tname]=nl
636 obstack.append(current)
638 if keyword.raw() not in current.allow_properties:
639 complain("Property %s not allowed at %s level"%
640 (keyword.raw(),current.type))
642 elif current.depth == vpnlevel.depth < allow_defs:
643 complain("Not allowed to set VPN properties here")
646 set_property(current,w)
649 complain("unknown keyword '%s'"%(keyword.raw()))
651 def pfilepath(pathname,allow_include=False):
653 outlines=pfile(pathname,f.readlines(),allow_include=allow_include)
657 def pfile(name,lines,allow_include=False):
665 if (i[0]=='#'): continue
666 outlines += pline(i,allow_include=allow_include)
670 "Output include file for secnet configuration"
671 w.write("# secnet sites file autogenerated by make-secnet-sites "
672 +"version %s\n"%VERSION)
673 w.write("# %s\n"%time.asctime(time.localtime(time.time())))
674 w.write("# Command line: %s\n\n"%' '.join(sys.argv))
676 # Raw VPN data section of file
677 w.write(prefix+"vpn-data {\n")
678 for i in root.children.values():
679 i.output_data(w,(i,))
682 # Per-VPN flattened lists
683 w.write(prefix+"vpn {\n")
684 for i in root.children.values():
685 i.output_vpnflat(w,())
688 # Flattened list of sites
689 w.write(prefix+"all-sites %s;\n"%",".join(
690 map(lambda x:"%svpn/%s/all-sites"%(prefix,x.kname()),
691 root.children.values())))
697 # Sanity check section
698 # Delete nodes where leaf=0 that have no children
701 "Number of leafnodes below node n"
703 for i in n.children.keys():
704 if live(n.children[i]): return 1
707 "Delete nodes that have no leafnode children"
708 for i in list(n.children.keys()):
709 delempty(n.children[i])
710 if not live(n.children[i]):
713 # Check that all constraints are met (as far as I can tell
714 # restrict-nets/networks/peer are the only special cases)
716 def checkconstraints(n,p,ra):
718 new_p.update(n.properties)
719 for i in n.require_properties.keys():
721 moan("%s %s is missing property %s"%
723 for i in new_p.keys():
724 if i not in n.allow_properties:
725 moan("%s %s has forbidden property %s"%
727 # Check address range restrictions
728 if "restrict-nets" in n.properties:
729 new_ra=ra.intersection(n.properties["restrict-nets"].set)
732 if "networks" in n.properties:
733 if not n.properties["networks"].set <= new_ra:
734 moan("%s %s networks out of bounds"%(n.type,n.name))
735 if "peer" in n.properties:
736 if not n.properties["networks"].set.contains(
737 n.properties["peer"].addr):
738 moan("%s %s peer not in networks"%(n.type,n.name))
739 for i in n.children.keys():
740 checkconstraints(n.children[i],new_p,new_ra)
743 headerinput=pfilepath(header,allow_include=True)
744 userinput=sys.stdin.readlines()
745 pfile("user input",userinput)
747 if inputfile is None:
748 pfile("stdin",sys.stdin.readlines())
753 checkconstraints(root,{},ipaddrset.complete_set())
756 if complaints==1: print("There was 1 problem.")
757 else: print("There were %d problems."%(complaints))
759 complaints=None # arranges to crash if we complain later
762 # Put the user's input into their group file, and rebuild the main
764 f=open(groupfiledir+"/T"+group.groupname(),'w')
765 f.write("# Section submitted by user %s, %s\n"%
766 (user,time.asctime(time.localtime(time.time()))))
767 f.write("# Checked by make-secnet-sites version %s\n\n"%VERSION)
768 for i in userinput: f.write(i)
771 os.rename(groupfiledir+"/T"+group.groupname(),
772 groupfiledir+"/R"+group.groupname())
773 f=open(sitesfile+"-tmp",'w')
774 f.write("# sites file autogenerated by make-secnet-sites\n")
775 f.write("# generated %s, invoked by %s\n"%
776 (time.asctime(time.localtime(time.time())),user))
777 f.write("# use make-secnet-sites to turn this file into a\n")
778 f.write("# valid /etc/secnet/sites.conf file\n\n")
779 for i in headerinput: f.write(i)
780 files=os.listdir(groupfiledir)
783 j=open(groupfiledir+"/"+i)
786 f.write("# end of sites file\n")
788 os.rename(sitesfile+"-tmp",sitesfile)
790 if outputfile is None:
793 tmp_outputfile=outputfile+'~tmp~'
794 of=open(tmp_outputfile,'w')
796 if outputfile is not None:
797 os.rename(tmp_outputfile,outputfile)