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
153 ok=self._re_ok(Tainted.bad_name,'name')
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 bad_host=re.compile(r'[^-\][_.:0-9a-zA-Z]')
186 # We permit _ so we can refer to special non-host domains
187 # which have A and AAAA RRs. This is a crude check and we may
188 # still produce config files with syntactically invalid
189 # domains or addresses, but that is OK.
191 ok=self._re_ok(Tainted.bad_host,'host/address',255)
194 bad_email=re.compile(r'[^-._0-9a-z@!$%^&*=+~/]')
195 # ^ This does not accept all valid email addresses. That's
196 # not really possible with this input syntax. It accepts
197 # all ones that don't require quoting anywhere in email
198 # protocols (and also accepts some invalid ones).
200 ok=self._re_ok(Tainted.bad_email,'email address',1023)
203 bad_groupname=re.compile(r'^[^_A-Za-z]|[^-+_0-9A-Za-z]')
205 ok=self._re_ok(Tainted.bad_groupname,'group name',64)
220 ap = argparse.ArgumentParser(description='process secnet sites files')
221 ap.add_argument('--userv', '-u', action='store_true',
222 help='userv service fragment update mode')
223 ap.add_argument('--conf-key-prefix', action=ActionNoYes,
225 help='prefix conf file key names derived from sites data')
226 ap.add_argument('--prefix', '-P', nargs=1,
228 ap.add_argument('arg',nargs=argparse.REMAINDER)
230 #print(repr(av), file=sys.stderr)
231 service = 1 if av.userv else 0
232 prefix = '' if av.prefix is None else av.prefix[0]
233 key_prefix = av.conf_key_prefix
236 print("Wrong number of arguments")
238 (header, groupfiledir, sitesfile, group) = av.arg
239 group = Tainted(group,0,'command line')
240 # untrusted argument from caller
241 if "USERV_USER" not in os.environ:
242 print("Environment variable USERV_USER not found")
244 user=os.environ["USERV_USER"]
245 # Check that group is in USERV_GROUP
246 if "USERV_GROUP" not in os.environ:
247 print("Environment variable USERV_GROUP not found")
249 ugs=os.environ["USERV_GROUP"]
251 for i in ugs.split():
254 print("caller not in group %s"%group)
258 print("Too many arguments")
260 (inputfile, outputfile) = (av.arg + [None]*2)[0:2]
261 if outputfile is None: of=sys.stdout
262 else: of=open(outputfile,'w')
266 # Classes describing possible datatypes in the configuration file
269 "Common protocol for configuration types."
271 complain("%s %s already has property %s defined"%
272 (obj.type,obj.name,w[0].raw()))
275 "A list of some kind of configuration type."
276 def __init__(self,subtype,w):
278 self.list=[subtype(w)]
280 self.list.append(self.subtype(w))
282 return ', '.join(map(str, self.list))
284 return lambda w: conflist(subtype, w)
286 class single_ipaddr (basetype):
288 def __init__(self,w):
289 self.addr=ipaddress.ip_address(w[1].raw_mark_ok())
291 return '"%s"'%self.addr
293 class networks (basetype):
294 "A set of IP addresses specified as a list of networks"
295 def __init__(self,w):
296 self.set=ipaddrset.IPAddressSet()
298 x=ipaddress.ip_network(i.raw_mark_ok(),strict=True)
301 return ",".join(map((lambda n: '"%s"'%n), self.set.networks()))
303 class dhgroup (basetype):
304 "A Diffie-Hellman group"
305 def __init__(self,w):
306 self.mod=w[1].bignum_16('dh','dh mod')
307 self.gen=w[2].bignum_16('dh','dh gen')
309 return 'diffie-hellman("%s","%s")'%(self.mod,self.gen)
311 class hash (basetype):
312 "A choice of hash function"
313 def __init__(self,w):
316 if (self.ht!='md5' and self.ht!='sha1'):
317 complain("unknown hash type %s"%(self.ht))
322 return '%s'%(self.ht)
324 class email (basetype):
326 def __init__(self,w):
327 self.addr=w[1].email()
329 return '<%s>'%(self.addr)
331 class boolean (basetype):
333 def __init__(self,w):
335 if re.match('[TtYy1]',v.raw()):
338 elif re.match('[FfNn0]',v.raw()):
342 complain("invalid boolean value");
344 return ['False','True'][self.b]
346 class num (basetype):
348 def __init__(self,w):
349 self.n=w[1].number(0,0x7fffffff)
353 class address (basetype):
354 "A DNS name and UDP port number"
355 def __init__(self,w):
357 self.port=w[2].number(1,65536,'port')
359 return '"%s"; port %d'%(self.adr,self.port)
361 class rsakey (basetype):
363 def __init__(self,w):
364 self.l=w[1].number(0,max['rsa_bits'],'rsa len')
365 self.e=w[2].bignum_10('rsa','rsa e')
366 self.n=w[3].bignum_10('rsa','rsa n')
367 if len(w) >= 5: w[4].email()
369 return 'rsa-public("%s","%s")'%(self.e,self.n)
371 # Possible properties of configuration nodes
373 'contact':(email,"Contact address"),
374 'dh':(dhgroup,"Diffie-Hellman group"),
375 'hash':(hash,"Hash function"),
376 'key-lifetime':(num,"Maximum key lifetime (ms)"),
377 'setup-timeout':(num,"Key setup timeout (ms)"),
378 'setup-retries':(num,"Maximum key setup packet retries"),
379 'wait-time':(num,"Time to wait after unsuccessful key setup (ms)"),
380 'renegotiate-time':(num,"Time after key setup to begin renegotiation (ms)"),
381 'restrict-nets':(networks,"Allowable networks"),
382 'networks':(networks,"Claimed networks"),
383 'pubkey':(rsakey,"RSA public site key"),
384 'peer':(single_ipaddr,"Tunnel peer IP address"),
385 'address':(address,"External contact address and port"),
386 'mobile':(boolean,"Site is mobile"),
390 "Simply output a property - the default case"
391 return "%s %s;\n"%(name,value)
393 # All levels support these properties
395 'contact':(lambda name,value:"# Contact email address: %s\n"%(value)),
402 'renegotiate-time':sp,
403 'restrict-nets':(lambda name,value:"# restrict-nets %s\n"%value),
407 "A level in the configuration hierarchy"
411 require_properties={}
412 def __init__(self,w):
413 self.type=w[0].keyword()
414 self.name=w[1].name()
417 def indent(self,w,t):
419 def prop_out(self,n):
420 return self.allow_properties[n](n,str(self.properties[n]))
421 def output_props(self,w,ind):
422 for i in sorted(self.properties.keys()):
423 if self.allow_properties[i]:
425 w.write("%s"%self.prop_out(i))
427 return ((self.type[0].upper() if key_prefix else '')
429 def output_data(self,w,path):
432 w.write("%s {\n"%(self.kname()))
433 self.output_props(w,ind+2)
434 if self.depth==1: w.write("\n");
435 for k in sorted(self.children.keys()):
437 c.output_data(w,path+(c,))
441 class vpnlevel(level):
442 "VPN level in the configuration hierarchy"
446 allow_properties=global_properties.copy()
448 'contact':"VPN admin contact address"
450 def __init__(self,w):
451 level.__init__(self,w)
452 def output_vpnflat(self,w,path):
453 "Output flattened list of site names for this VPN"
456 w.write("%s {\n"%(self.kname()))
457 for i in self.children.keys():
458 self.children[i].output_vpnflat(w,path+(self,))
461 w.write("all-sites %s;\n"%
462 ','.join(map(lambda i: i.kname(),
463 self.children.values())))
467 class locationlevel(level):
468 "Location level in the configuration hierarchy"
472 allow_properties=global_properties.copy()
474 'contact':"Location admin contact address",
476 def __init__(self,w):
477 level.__init__(self,w)
478 self.group=w[2].groupname()
479 def output_vpnflat(self,w,path):
482 # The "path=path,self=self" abomination below exists because
483 # Python didn't support nested_scopes until version 2.1
486 w.write("%s %s;\n"%(self.kname(),','.join(
487 map(lambda x,path=path,self=self:
488 '/'.join([prefix+"vpn-data"] + list(map(
491 self.children.values()))))
493 class sitelevel(level):
494 "Site level (i.e. a leafnode) in the configuration hierarchy"
498 allow_properties=global_properties.copy()
499 allow_properties.update({
503 'pubkey':(lambda n,v:"key %s;\n"%v),
507 'dh':"Diffie-Hellman group",
508 'contact':"Site admin contact address",
509 'networks':"Networks claimed by the site",
510 'hash':"hash function",
511 'peer':"Gateway address of the site",
512 'pubkey':"RSA public key of the site",
514 def __init__(self,w):
515 level.__init__(self,w)
516 def output_data(self,w,path):
518 np='/'.join(map(lambda i: i.name, path))
520 w.write("%s {\n"%(self.kname()))
522 w.write("name \"%s\";\n"%(np,))
523 self.output_props(w,ind+2)
525 w.write("link netlink {\n");
527 w.write("routes %s;\n"%str(self.properties["networks"]))
529 w.write("ptp-address %s;\n"%str(self.properties["peer"]))
535 # Levels in the configuration file
537 levels={'vpn':vpnlevel, 'location':locationlevel, 'site':sitelevel}
540 "Complain about a particular input line"
542 print(("%s line %d: "%(file,line))+msg)
543 complaints=complaints+1
545 "Complain about something in general"
548 complaints=complaints+1
550 class UntaintedRoot():
551 def __init__(self,s): self._s=s
552 def name(self): return self._s
553 def keyword(self): return self._s
555 root=level([UntaintedRoot(x) for x in ['root','root']])
556 # All vpns are children of this node
558 allow_defs=0 # Level above which new definitions are permitted
560 def set_property(obj,w):
561 "Set a property on a configuration node"
563 if prop.raw() in obj.properties:
564 obj.properties[prop.raw_mark_ok()].add(obj,w)
566 obj.properties[prop.raw()]=keywords[prop.raw_mark_ok()][0](w)
569 def pline(il,allow_include=False):
570 "Process a configuration file line"
571 global allow_defs, obstack, root
572 w=il.rstrip('\n').split()
573 if len(w)==0: return ['']
574 w=list([Tainted(x) for x in w])
576 current=obstack[len(obstack)-1]
577 copyout=lambda: [' '*len(obstack) +
578 ' '.join([ww.output() for ww in w]) +
580 if keyword=='end-definitions':
581 keyword.raw_mark_ok()
582 allow_defs=sitelevel.depth
585 if keyword=='include':
586 if not allow_include:
587 complain("include not permitted here")
590 complain("include requires one argument")
592 newfile=os.path.join(os.path.dirname(file),w[1].raw_mark_ok())
593 # ^ user of "include" is trusted so raw_mark_ok is good
594 return pfilepath(newfile,allow_include=allow_include)
595 if keyword.raw() in levels:
596 # We may go up any number of levels, but only down by one
597 newdepth=levels[keyword.raw_mark_ok()].depth
598 currentdepth=len(obstack) # actually +1...
599 if newdepth<=currentdepth:
600 obstack=obstack[:newdepth]
601 if newdepth>currentdepth:
602 complain("May not go from level %d to level %d"%
603 (currentdepth-1,newdepth))
604 # See if it's a new one (and whether that's permitted)
606 current=obstack[len(obstack)-1]
608 if tname in current.children:
610 current=current.children[tname]
611 if service and group and current.depth==2:
612 if group!=current.group:
613 complain("Incorrect group!")
617 # Ignore depth check for now
618 nl=levels[keyword.raw()](w)
619 if nl.depth<allow_defs:
620 complain("New definitions not allowed at "
622 # we risk crashing if we continue
624 current.children[tname]=nl
626 obstack.append(current)
628 if keyword.raw() not in current.allow_properties:
629 complain("Property %s not allowed at %s level"%
630 (keyword.raw(),current.type))
632 elif current.depth == vpnlevel.depth < allow_defs:
633 complain("Not allowed to set VPN properties here")
636 set_property(current,w)
639 complain("unknown keyword '%s'"%(keyword.raw()))
641 def pfilepath(pathname,allow_include=False):
643 outlines=pfile(pathname,f.readlines(),allow_include=allow_include)
647 def pfile(name,lines,allow_include=False):
655 if (i[0]=='#'): continue
656 outlines += pline(i,allow_include=allow_include)
660 "Output include file for secnet configuration"
661 w.write("# secnet sites file autogenerated by make-secnet-sites "
662 +"version %s\n"%VERSION)
663 w.write("# %s\n"%time.asctime(time.localtime(time.time())))
664 w.write("# Command line: %s\n\n"%' '.join(sys.argv))
666 # Raw VPN data section of file
667 w.write(prefix+"vpn-data {\n")
668 for i in root.children.values():
669 i.output_data(w,(i,))
672 # Per-VPN flattened lists
673 w.write(prefix+"vpn {\n")
674 for i in root.children.values():
675 i.output_vpnflat(w,())
678 # Flattened list of sites
679 w.write(prefix+"all-sites %s;\n"%",".join(
680 map(lambda x:"%svpn/%s/all-sites"%(prefix,x.kname()),
681 root.children.values())))
687 # Sanity check section
688 # Delete nodes where leaf=0 that have no children
691 "Number of leafnodes below node n"
693 for i in n.children.keys():
694 if live(n.children[i]): return 1
697 "Delete nodes that have no leafnode children"
698 for i in list(n.children.keys()):
699 delempty(n.children[i])
700 if not live(n.children[i]):
703 # Check that all constraints are met (as far as I can tell
704 # restrict-nets/networks/peer are the only special cases)
706 def checkconstraints(n,p,ra):
708 new_p.update(n.properties)
709 for i in n.require_properties.keys():
711 moan("%s %s is missing property %s"%
713 for i in new_p.keys():
714 if i not in n.allow_properties:
715 moan("%s %s has forbidden property %s"%
717 # Check address range restrictions
718 if "restrict-nets" in n.properties:
719 new_ra=ra.intersection(n.properties["restrict-nets"].set)
722 if "networks" in n.properties:
723 if not n.properties["networks"].set <= new_ra:
724 moan("%s %s networks out of bounds"%(n.type,n.name))
725 if "peer" in n.properties:
726 if not n.properties["networks"].set.contains(
727 n.properties["peer"].addr):
728 moan("%s %s peer not in networks"%(n.type,n.name))
729 for i in n.children.keys():
730 checkconstraints(n.children[i],new_p,new_ra)
733 headerinput=pfilepath(header,allow_include=True)
734 userinput=sys.stdin.readlines()
735 pfile("user input",userinput)
737 if inputfile is None:
738 pfile("stdin",sys.stdin.readlines())
743 checkconstraints(root,{},ipaddrset.complete_set())
746 if complaints==1: print("There was 1 problem.")
747 else: print("There were %d problems."%(complaints))
749 complaints=None # arranges to crash if we complain later
752 # Put the user's input into their group file, and rebuild the main
754 f=open(groupfiledir+"/T"+group.groupname(),'w')
755 f.write("# Section submitted by user %s, %s\n"%
756 (user,time.asctime(time.localtime(time.time()))))
757 f.write("# Checked by make-secnet-sites version %s\n\n"%VERSION)
758 for i in userinput: f.write(i)
761 os.rename(groupfiledir+"/T"+group.groupname(),
762 groupfiledir+"/R"+group.groupname())
763 f=open(sitesfile+"-tmp",'w')
764 f.write("# sites file autogenerated by make-secnet-sites\n")
765 f.write("# generated %s, invoked by %s\n"%
766 (time.asctime(time.localtime(time.time())),user))
767 f.write("# use make-secnet-sites to turn this file into a\n")
768 f.write("# valid /etc/secnet/sites.conf file\n\n")
769 for i in headerinput: f.write(i)
770 files=os.listdir(groupfiledir)
773 j=open(groupfiledir+"/"+i)
776 f.write("# end of sites file\n")
778 os.rename(sitesfile+"-tmp",sitesfile)