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. It relies on the "ipaddr" library from
54 from __future__ import print_function
55 from builtins import int
66 # entry 0 is "near the executable", or maybe from PYTHONPATH=.,
67 # which we don't want to preempt
68 sys.path.insert(1,"/usr/local/share/secnet")
69 sys.path.insert(1,"/usr/share/secnet")
74 # Are we being invoked from userv?
76 # If we are, which group does the caller want to modify?
85 print("Wrong number of arguments")
89 groupfiledir=sys.argv[3]
92 if "USERV_USER" not in os.environ:
93 print("Environment variable USERV_USER not found")
95 user=os.environ["USERV_USER"]
96 # Check that group is in USERV_GROUP
97 if "USERV_GROUP" not in os.environ:
98 print("Environment variable USERV_GROUP not found")
100 ugs=os.environ["USERV_GROUP"]
102 for i in ugs.split():
105 print("caller not in group %s"%group)
108 if sys.argv[1]=='-P':
112 print("Too many arguments")
114 inputfile=sys.argv[1]
117 of=open(sys.argv[2],'w')
119 # Classes describing possible datatypes in the configuration file
122 "Common protocol for configuration types."
124 complain("%s %s already has property %s defined"%
125 (obj.type,obj.name,w[0]))
128 "A list of some kind of configuration type."
129 def __init__(self,subtype,w):
131 self.list=[subtype(w)]
133 self.list.append(self.subtype(w))
135 return ', '.join(map(str, self.list))
137 return lambda w: conflist(subtype, w)
139 class single_ipaddr (basetype):
141 def __init__(self,w):
142 self.addr=ipaddr.IPAddress(w[1])
144 return '"%s"'%self.addr
146 class networks (basetype):
147 "A set of IP addresses specified as a list of networks"
148 def __init__(self,w):
149 self.set=ipaddrset.IPAddressSet()
151 x=ipaddr.IPNetwork(i,strict=True)
154 return ",".join(map((lambda n: '"%s"'%n), self.set.networks()))
156 class dhgroup (basetype):
157 "A Diffie-Hellman group"
158 def __init__(self,w):
162 return 'diffie-hellman("%s","%s")'%(self.mod,self.gen)
164 class hash (basetype):
165 "A choice of hash function"
166 def __init__(self,w):
168 if (self.ht!='md5' and self.ht!='sha1'):
169 complain("unknown hash type %s"%(self.ht))
171 return '%s'%(self.ht)
173 class email (basetype):
175 def __init__(self,w):
178 return '<%s>'%(self.addr)
180 class boolean (basetype):
182 def __init__(self,w):
183 if re.match('[TtYy1]',w[1]):
185 elif re.match('[FfNn0]',w[1]):
188 complain("invalid boolean value");
190 return ['False','True'][self.b]
192 class num (basetype):
194 def __init__(self,w):
199 class address (basetype):
200 "A DNS name and UDP port number"
201 def __init__(self,w):
204 if (self.port<1 or self.port>65535):
205 complain("invalid port number")
207 return '"%s"; port %d'%(self.adr,self.port)
209 class rsakey (basetype):
211 def __init__(self,w):
216 return 'rsa-public("%s","%s")'%(self.e,self.n)
218 # Possible properties of configuration nodes
220 'contact':(email,"Contact address"),
221 'dh':(dhgroup,"Diffie-Hellman group"),
222 'hash':(hash,"Hash function"),
223 'key-lifetime':(num,"Maximum key lifetime (ms)"),
224 'setup-timeout':(num,"Key setup timeout (ms)"),
225 'setup-retries':(num,"Maximum key setup packet retries"),
226 'wait-time':(num,"Time to wait after unsuccessful key setup (ms)"),
227 'renegotiate-time':(num,"Time after key setup to begin renegotiation (ms)"),
228 'restrict-nets':(networks,"Allowable networks"),
229 'networks':(networks,"Claimed networks"),
230 'pubkey':(rsakey,"RSA public site key"),
231 'peer':(single_ipaddr,"Tunnel peer IP address"),
232 'address':(address,"External contact address and port"),
233 'mobile':(boolean,"Site is mobile"),
237 "Simply output a property - the default case"
238 return "%s %s;\n"%(name,value)
240 # All levels support these properties
242 'contact':(lambda name,value:"# Contact email address: %s\n"%(value)),
249 'renegotiate-time':sp,
250 'restrict-nets':(lambda name,value:"# restrict-nets %s\n"%value),
254 "A level in the configuration hierarchy"
258 require_properties={}
259 def __init__(self,w):
263 def indent(self,w,t):
265 def prop_out(self,n):
266 return self.allow_properties[n](n,str(self.properties[n]))
267 def output_props(self,w,ind):
268 for i in self.properties.keys():
269 if self.allow_properties[i]:
271 w.write("%s"%self.prop_out(i))
272 def output_data(self,w,ind,np):
274 w.write("%s {\n"%(self.name))
275 self.output_props(w,ind+2)
276 if self.depth==1: w.write("\n");
277 for c in self.children.values():
278 c.output_data(w,ind+2,np+self.name+"/")
282 class vpnlevel(level):
283 "VPN level in the configuration hierarchy"
287 allow_properties=global_properties.copy()
289 'contact':"VPN admin contact address"
291 def __init__(self,w):
292 level.__init__(self,w)
293 def output_vpnflat(self,w,ind,h):
294 "Output flattened list of site names for this VPN"
296 w.write("%s {\n"%(self.name))
297 for i in self.children.keys():
298 self.children[i].output_vpnflat(w,ind+2,
299 h+"/"+self.name+"/"+i)
302 w.write("all-sites %s;\n"%
303 ','.join(self.children.keys()))
307 class locationlevel(level):
308 "Location level in the configuration hierarchy"
312 allow_properties=global_properties.copy()
314 'contact':"Location admin contact address",
316 def __init__(self,w):
317 level.__init__(self,w)
319 def output_vpnflat(self,w,ind,h):
321 # The "h=h,self=self" abomination below exists because
322 # Python didn't support nested_scopes until version 2.1
323 w.write("%s %s;\n"%(self.name,','.join(
324 map(lambda x,h=h,self=self:
325 h+"/"+x,self.children.keys()))))
327 class sitelevel(level):
328 "Site level (i.e. a leafnode) in the configuration hierarchy"
332 allow_properties=global_properties.copy()
333 allow_properties.update({
337 'pubkey':(lambda n,v:"key %s;\n"%v),
341 'dh':"Diffie-Hellman group",
342 'contact':"Site admin contact address",
343 'networks':"Networks claimed by the site",
344 'hash':"hash function",
345 'peer':"Gateway address of the site",
346 'pubkey':"RSA public key of the site",
348 def __init__(self,w):
349 level.__init__(self,w)
350 def output_data(self,w,ind,np):
352 w.write("%s {\n"%(self.name))
354 w.write("name \"%s\";\n"%(np+self.name))
355 self.output_props(w,ind+2)
357 w.write("link netlink {\n");
359 w.write("routes %s;\n"%str(self.properties["networks"]))
361 w.write("ptp-address %s;\n"%str(self.properties["peer"]))
367 # Levels in the configuration file
369 levels={'vpn':vpnlevel, 'location':locationlevel, 'site':sitelevel}
371 # Reserved vpn/location/site names
372 reserved={'all-sites':None}
373 reserved.update(keywords)
374 reserved.update(levels)
377 "Complain about a particular input line"
379 print(("%s line %d: "%(file,line))+msg)
380 complaints=complaints+1
382 "Complain about something in general"
385 complaints=complaints+1
387 root=level(['root','root']) # All vpns are children of this node
389 allow_defs=0 # Level above which new definitions are permitted
392 def set_property(obj,w):
393 "Set a property on a configuration node"
394 if w[0] in obj.properties:
395 obj.properties[w[0]].add(obj,w)
397 obj.properties[w[0]]=keywords[w[0]][0](w)
399 def pline(i,allow_include=False):
400 "Process a configuration file line"
401 global allow_defs, obstack, root
402 w=i.rstrip('\n').split()
403 if len(w)==0: return [i]
405 current=obstack[len(obstack)-1]
406 if keyword=='end-definitions':
407 allow_defs=sitelevel.depth
410 if keyword=='include':
411 if not allow_include:
412 complain("include not permitted here")
415 complain("include requires one argument")
417 newfile=os.path.join(os.path.dirname(file),w[1])
418 return pfilepath(newfile,allow_include=allow_include)
419 if keyword in levels:
420 # We may go up any number of levels, but only down by one
421 newdepth=levels[keyword].depth
422 currentdepth=len(obstack) # actually +1...
423 if newdepth<=currentdepth:
424 obstack=obstack[:newdepth]
425 if newdepth>currentdepth:
426 complain("May not go from level %d to level %d"%
427 (currentdepth-1,newdepth))
428 # See if it's a new one (and whether that's permitted)
430 current=obstack[len(obstack)-1]
431 if w[1] in current.children:
433 current=current.children[w[1]]
434 if service and group and current.depth==2:
435 if group!=current.group:
436 complain("Incorrect group!")
439 # Ignore depth check for now
440 nl=levels[keyword](w)
441 if nl.depth<allow_defs:
442 complain("New definitions not allowed at "
444 # we risk crashing if we continue
446 current.children[w[1]]=nl
448 obstack.append(current)
450 if keyword not in current.allow_properties:
451 complain("Property %s not allowed at %s level"%
452 (keyword,current.type))
454 elif current.depth == vpnlevel.depth < allow_defs:
455 complain("Not allowed to set VPN properties here")
458 set_property(current,w)
461 complain("unknown keyword '%s'"%(keyword))
463 def pfilepath(pathname,allow_include=False):
465 outlines=pfile(pathname,f.readlines(),allow_include=allow_include)
469 def pfile(name,lines,allow_include=False):
477 if (i[0]=='#'): continue
478 outlines += pline(i,allow_include=allow_include)
482 "Output include file for secnet configuration"
483 w.write("# secnet sites file autogenerated by make-secnet-sites "
484 +"version %s\n"%VERSION)
485 w.write("# %s\n"%time.asctime(time.localtime(time.time())))
486 w.write("# Command line: %s\n\n"%' '.join(sys.argv))
488 # Raw VPN data section of file
489 w.write(prefix+"vpn-data {\n")
490 for i in root.children.values():
491 i.output_data(w,2,"")
494 # Per-VPN flattened lists
495 w.write(prefix+"vpn {\n")
496 for i in root.children.values():
497 i.output_vpnflat(w,2,prefix+"vpn-data")
500 # Flattened list of sites
501 w.write(prefix+"all-sites %s;\n"%",".join(
502 map(lambda x:"%svpn/%s/all-sites"%(prefix,x),
503 root.children.keys())))
509 # Sanity check section
510 # Delete nodes where leaf=0 that have no children
513 "Number of leafnodes below node n"
515 for i in n.children.keys():
516 if live(n.children[i]): return 1
519 "Delete nodes that have no leafnode children"
520 for i in n.children.keys():
521 delempty(n.children[i])
522 if not live(n.children[i]):
525 # Check that all constraints are met (as far as I can tell
526 # restrict-nets/networks/peer are the only special cases)
528 def checkconstraints(n,p,ra):
530 new_p.update(n.properties)
531 for i in n.require_properties.keys():
533 moan("%s %s is missing property %s"%
535 for i in new_p.keys():
536 if i not in n.allow_properties:
537 moan("%s %s has forbidden property %s"%
539 # Check address range restrictions
540 if "restrict-nets" in n.properties:
541 new_ra=ra.intersection(n.properties["restrict-nets"].set)
544 if "networks" in n.properties:
545 if not n.properties["networks"].set <= new_ra:
546 moan("%s %s networks out of bounds"%(n.type,n.name))
547 if "peer" in n.properties:
548 if not n.properties["networks"].set.contains(
549 n.properties["peer"].addr):
550 moan("%s %s peer not in networks"%(n.type,n.name))
551 for i in n.children.keys():
552 checkconstraints(n.children[i],new_p,new_ra)
555 headerinput=pfilepath(header,allow_include=True)
556 userinput=sys.stdin.readlines()
557 pfile("user input",userinput)
559 if inputfile is None:
560 pfile("stdin",sys.stdin.readlines())
565 checkconstraints(root,{},ipaddrset.complete_set())
568 if complaints==1: print("There was 1 problem.")
569 else: print("There were %d problems."%(complaints))
573 # Put the user's input into their group file, and rebuild the main
575 f=open(groupfiledir+"/T"+group,'w')
576 f.write("# Section submitted by user %s, %s\n"%
577 (user,time.asctime(time.localtime(time.time()))))
578 f.write("# Checked by make-secnet-sites version %s\n\n"%VERSION)
579 for i in userinput: f.write(i)
582 os.rename(groupfiledir+"/T"+group,groupfiledir+"/R"+group)
583 f=open(sitesfile+"-tmp",'w')
584 f.write("# sites file autogenerated by make-secnet-sites\n")
585 f.write("# generated %s, invoked by %s\n"%
586 (time.asctime(time.localtime(time.time())),user))
587 f.write("# use make-secnet-sites to turn this file into a\n")
588 f.write("# valid /etc/secnet/sites.conf file\n\n")
589 for i in headerinput: f.write(i)
590 files=os.listdir(groupfiledir)
593 j=open(groupfiledir+"/"+i)
596 f.write("# end of sites file\n")
598 os.rename(sitesfile+"-tmp",sitesfile)