source: genesis/tools/gformat.py@ 10863

Last change on this file since 10863 was 10863, checked in by rick, 13 years ago

Related-To: nodefactory:ticket:177

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 48.8 KB
Line 
1#!/usr/bin/env python
2#
3# vim:ts=2:et:sw=2:ai
4# Wireless Leiden configuration generator, based on yaml files'
5#
6# XXX: This should be rewritten to make use of the ipaddr.py library.
7#
8# Sample apache configuration (mind the AcceptPathInfo!)
9# ScriptAlias /wleiden/config /usr/local/www/genesis/tools/gformat.py
10# <Directory /usr/local/www/genesis>
11# Allow from all
12# AcceptPathInfo On
13# </Directory>
14#
15# Rick van der Zwet <info@rickvanderzwet.nl>
16#
17
18# Hack to make the script directory is also threated as a module search path.
19import sys
20import os
21import re
22sys.path.append(os.path.dirname(__file__))
23
24import cgi
25import cgitb
26import copy
27import glob
28import socket
29import string
30import subprocess
31import time
32import rdnap
33import math
34import make_network_kml
35from pprint import pprint
36from collections import defaultdict
37try:
38 import yaml
39except ImportError, e:
40 print e
41 print "[ERROR] Please install the python-yaml or devel/py-yaml package"
42 exit(1)
43
44try:
45 from yaml import CLoader as Loader
46 from yaml import CDumper as Dumper
47except ImportError:
48 from yaml import Loader, Dumper
49
50from jinja2 import Environment, Template
51def yesorno(value):
52 return "YES" if bool(value) else "NO"
53env = Environment()
54env.filters['yesorno'] = yesorno
55def render_template(datadump, template):
56 result = env.from_string(template).render(datadump)
57 # Make it look pretty to the naked eye, as jinja templates are not so
58 # friendly when it comes to whitespace formatting
59 ## Remove extra whitespace at end of line lstrip() style.
60 result = re.sub(r'\n[\ ]+','\n', result)
61 ## Include only a single newline between an definition and a comment
62 result = re.sub(r'(["\'])\n+([a-z]|\n#\n)',r'\1\n\2', result)
63 ## Remove extra newlines after single comment
64 result = re.sub(r'(#\n)\n+([a-z])',r'\1\2', result)
65 return result
66
67import logging
68logging.basicConfig(format='# %(levelname)s: %(message)s' )
69logger = logging.getLogger()
70logger.setLevel(logging.DEBUG)
71
72
73if os.environ.has_key('CONFIGROOT'):
74 NODE_DIR = os.environ['CONFIGROOT']
75else:
76 NODE_DIR = os.path.abspath(os.path.dirname(__file__)) + '/../nodes'
77__version__ = '$Id: gformat.py 10863 2012-05-15 11:25:37Z rick $'
78
79
80files = [
81 'authorized_keys',
82 'dnsmasq.conf',
83 'dhcpd.conf',
84 'rc.conf.local',
85 'resolv.conf',
86 'motd',
87 'ntp.conf',
88 'pf.hybrid.conf.local',
89 'wleiden.yaml',
90 ]
91
92# Global variables uses
93OK = 10
94DOWN = 20
95UNKNOWN = 90
96
97datadump_cache = {}
98
99def get_yaml(item):
100 """ Get configuration yaml for 'item'"""
101 if datadump_cache.has_key(item):
102 return datadump_cache[item].copy()
103
104 gfile = os.path.join(NODE_DIR,item,'wleiden.yaml')
105
106 datadump = {}
107 f = open(gfile, 'r')
108 datadump.update(yaml.load(f,Loader=Loader))
109 if datadump['nodetype'] == 'Hybrid':
110 # Some values are defined implicitly
111 if datadump.has_key('rdr_rules') and datadump['rdr_rules'] and not datadump.has_key('service_incoming_rdr'):
112 datadump['service_incoming_rdr'] = True
113 # Use some boring defaults
114 defaults = {
115 'service_proxy_normal' : False,
116 'service_proxy_ileiden' : False,
117 'service_accesspoint' : True,
118 'service_incoming_rdr' : False
119 }
120 for (key,value) in defaults.iteritems():
121 if not datadump.has_key(key):
122 datadump[key] = value
123 f.close()
124
125 # Preformat certain needed variables for formatting and push those into special object
126 datadump['autogen_iface_keys'] = get_interface_keys(datadump)
127
128 wlan_count=0
129 for key in datadump['autogen_iface_keys']:
130 if datadump[key]['type'] in ['11a', '11b', '11g', 'wireless']:
131 datadump[key]['autogen_ifname'] = 'wlan%i' % wlan_count
132 wlan_count += 1
133 else:
134 datadump[key]['autogen_ifname'] = datadump[key]['interface'].split(':')[0]
135
136 dhcp_interfaces = [datadump[key]['autogen_ifname'] for key in datadump['autogen_iface_keys'] if (datadump[key].has_key('dhcp') and datadump[key]['dhcp'])]
137 datadump['autogen_dhcp_interfaces'] = dhcp_interfaces
138 datadump['autogen_item'] = item
139
140 datadump['autogen_realname'] = get_realname(datadump)
141 datadump['autogen_domain'] = datadump['domain'] if datadump.has_key('domain') else 'wleiden.net.'
142 datadump['autogen_fqdn'] = datadump['autogen_realname'] + '.' + datadump['autogen_domain']
143 datadump_cache[item] = datadump.copy()
144 return datadump
145
146
147def store_yaml(datadump, header=False):
148 """ Store configuration yaml for 'item'"""
149 item = datadump['autogen_item']
150 gfile = os.path.join(NODE_DIR,item,'wleiden.yaml')
151
152 f = open(gfile, 'w')
153 f.write(generate_wleiden_yaml(datadump, header))
154 f.close()
155
156
157def network(ip):
158 addr, mask = ip.split('/')
159 # Not parsing of these folks please
160 addr = parseaddr(addr)
161 mask = int(mask)
162 network = addr & ~((1 << (32 - mask)) - 1)
163 return network
164
165
166
167def make_relations(datadumps=None):
168 """ Process _ALL_ yaml files to get connection relations """
169 errors = []
170 poel = defaultdict(list)
171
172 if not datadumps:
173 for host in get_hostlist():
174 datadumps[host] = get_yaml(host)
175
176 for host, datadump in datadumps.iteritems():
177 try:
178 for iface_key in datadump['autogen_iface_keys']:
179 net_addr = network(datadump[iface_key]['ip'])
180 poel[net_addr] += [(host,datadump[iface_key])]
181 except (KeyError, ValueError), e:
182 errors.append("[FOUT] in '%s' interface '%s' (%s)" % (host,iface_key, e))
183 continue
184 return (poel, errors)
185
186
187
188def valid_addr(addr):
189 """ Show which address is valid in which are not """
190 return str(addr).startswith('172.')
191
192def get_system_list(prefix):
193 return sorted([os.path.basename(os.path.dirname(x)) for x in glob.glob("%s/%s*/wleiden.yaml" % (NODE_DIR, prefix))])
194
195get_hybridlist = lambda: get_system_list("Hybrid")
196get_nodelist = lambda: get_system_list("CNode")
197get_proxylist = lambda: get_system_list("Proxy")
198
199def get_hostlist():
200 """ Combined hosts and proxy list"""
201 return get_nodelist() + get_proxylist() + get_hybridlist()
202
203def angle_between_points(lat1,lat2,long1,long2):
204 """
205 Return Angle in radians between two GPS coordinates
206 See: http://stackoverflow.com/questions/3809179/angle-between-2-gps-coordinates
207 """
208 dy = lat2 - lat1
209 dx = math.cos(lat1)*(long2 - long1)
210 angle = math.atan2(dy,dx)
211 return angle
212
213
214
215def angle_to_cd(angle):
216 """ Return Dutch Cardinal Direction estimation in 'one digit' of radian angle """
217
218 # For easy conversion get positive degree
219 degrees = math.degrees(angle)
220 abs_degrees = 360 + degrees if degrees < 0 else degrees
221
222 # Numbers can be confusing calculate from the 4 main directions
223 p = 22.5
224 if abs_degrees < p:
225 cd = "n"
226 elif abs_degrees < (90 - p):
227 cd = "no"
228 elif abs_degrees < (90 + p):
229 cd = "o"
230 elif abs_degrees < (180 - p):
231 cd = "zo"
232 elif abs_degrees < (180 + p):
233 cd = "z"
234 elif abs_degrees < (270 - p):
235 cd = "zw"
236 elif abs_degrees < (270 + p):
237 cd = "w"
238 elif abs_degrees < (360 - p):
239 cd = "nw"
240 else:
241 cd = "n"
242 return cd
243
244
245
246def cd_between_hosts(hostA, hostB, datadumps):
247 # Using RDNAP coordinates
248 dx = float(int(datadumps[hostA]['rdnap_x']) - int(datadumps[hostB]['rdnap_x'])) * -1
249 dy = float(int(datadumps[hostA]['rdnap_y']) - int(datadumps[hostB]['rdnap_y'])) * -1
250 return angle_to_cd(math.atan2(dx,dy))
251
252 # GPS coordinates seems to fail somehow
253 #latA = float(datadumps[hostA]['latitude'])
254 #latB = float(datadumps[hostB]['latitude'])
255 #lonA = float(datadumps[hostA]['longitude'])
256 #lonB = float(datadumps[hostB]['longitude'])
257 #return angle_to_cd(angle_between_points(latA, latB, lonA, lonB))
258
259
260def generate_title(nodelist):
261 """ Main overview page """
262 items = {'root' : "." }
263 def fl(spaces, line):
264 return (' ' * spaces) + line + '\n'
265
266 output = """
267<html>
268 <head>
269 <title>Wireless leiden Configurator - GFormat</title>
270 <style type="text/css">
271 th {background-color: #999999}
272 tr:nth-child(odd) {background-color: #cccccc}
273 tr:nth-child(even) {background-color: #ffffff}
274 th, td {padding: 0.1em 1em}
275 </style>
276 </head>
277 <body>
278 <center>
279 <form type="GET" action="%(root)s">
280 <input type="hidden" name="action" value="update">
281 <input type="submit" value="Update Configuration Database (SVN)">
282 </form>
283 <table>
284 <caption><h3>Wireless Leiden Configurator</h3></caption>
285 """ % items
286
287 for node in nodelist:
288 items['node'] = node
289 output += fl(5, '<tr>') + fl(7,'<td><a href="%(root)s/%(node)s">%(node)s</a></td>' % items)
290 for config in files:
291 items['config'] = config
292 output += fl(7,'<td><a href="%(root)s/%(node)s/%(config)s">%(config)s</a></td>' % items)
293 output += fl(5, "</tr>")
294 output += """
295 </table>
296 <hr />
297 <em>%s</em>
298 </center>
299 </body>
300</html>
301 """ % __version__
302
303 return output
304
305
306
307def generate_node(node):
308 """ Print overview of all files available for node """
309 return "\n".join(files)
310
311def generate_node_overview(host):
312 """ Print overview of all files available for node """
313 datadump = get_yaml(host)
314 params = { 'host' : host }
315 output = "<em><a href='..'>Back to overview</a></em><hr />"
316 output += "<h2>Available files:</h2><ul>"
317 for cf in files:
318 params['cf'] = cf
319 output += '<li><a href="%(host)s/%(cf)s">%(cf)s</a></li>\n' % params
320 output += "</ul>"
321
322 # Generate and connection listing
323 output += "<h2>Connected To:</h2><ul>"
324 (poel, errors) = make_relations()
325 for network, hosts in poel.iteritems():
326 if host in [x[0] for x in hosts]:
327 if len(hosts) == 1:
328 # Single not connected interface
329 continue
330 for remote,ifacedump in hosts:
331 if remote == host:
332 # This side of the interface
333 continue
334 params = { 'remote': remote, 'remote_ip' : ifacedump['ip'] }
335 output += '<li><a href="%(remote)s">%(remote)s</a> -- %(remote_ip)s</li>\n' % params
336 output += "</ul>"
337 output += "<h2>MOTD details:</h2><pre>" + generate_motd(datadump) + "</pre>"
338
339 output += "<hr /><em><a href='..'>Back to overview</a></em>"
340 return output
341
342
343def generate_header(ctag="#"):
344 return """\
345%(ctag)s
346%(ctag)s DO NOT EDIT - Automatically generated by 'gformat'
347%(ctag)s Generated at %(date)s by %(host)s
348%(ctag)s
349""" % { 'ctag' : ctag, 'date' : time.ctime(), 'host' : socket.gethostname() }
350
351
352
353def parseaddr(s):
354 """ Process IPv4 CIDR notation addr to a (binary) number """
355 f = s.split('.')
356 return (long(f[0]) << 24L) + \
357 (long(f[1]) << 16L) + \
358 (long(f[2]) << 8L) + \
359 long(f[3])
360
361
362
363def showaddr(a):
364 """ Display IPv4 addr in (dotted) CIDR notation """
365 return "%d.%d.%d.%d" % ((a >> 24) & 0xff, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff)
366
367
368def is_member(ip, mask, canidate):
369 """ Return True if canidate is part of ip/mask block"""
370 ip_addr = parseaddr(ip)
371 ip_canidate = parseaddr(canidate)
372 mask = int(mask)
373 ip_addr = ip_addr & ~((1 << (32 - mask)) - 1)
374 ip_canidate = ip_canidate & ~((1 << (32 - mask)) - 1)
375 return ip_addr == ip_canidate
376
377
378
379def cidr2netmask(netmask):
380 """ Given a 'netmask' return corresponding CIDR """
381 return showaddr(0xffffffff & (0xffffffff << (32 - int(netmask))))
382
383def get_network(addr, mask):
384 return showaddr(parseaddr(addr) & ~((1 << (32 - int(mask))) - 1))
385
386
387def generate_dhcpd_conf(datadump):
388 """ Generate config file '/usr/local/etc/dhcpd.conf """
389 output = generate_header()
390 output += Template("""\
391# option definitions common to all supported networks...
392option domain-name "dhcp.{{ autogen_fqdn }}";
393
394default-lease-time 600;
395max-lease-time 7200;
396
397# Use this to enble / disable dynamic dns updates globally.
398#ddns-update-style none;
399
400# If this DHCP server is the official DHCP server for the local
401# network, the authoritative directive should be uncommented.
402authoritative;
403
404# Use this to send dhcp log messages to a different log file (you also
405# have to hack syslog.conf to complete the redirection).
406log-facility local7;
407
408#
409# Interface definitions
410#
411\n""").render(datadump)
412
413 dhcp_out = defaultdict(list)
414 for iface_key in datadump['autogen_iface_keys']:
415 ifname = datadump[iface_key]['autogen_ifname']
416 if not datadump[iface_key].has_key('comment'):
417 datadump[iface_key]['comment'] = None
418 dhcp_out[ifname].append(" ## %(interface)s - %(desc)s - %(comment)s\n" % datadump[iface_key])
419
420 (addr, mask) = datadump[iface_key]['ip'].split('/')
421 datadump[iface_key]['addr'] = addr
422 datadump[iface_key]['netmask'] = cidr2netmask(mask)
423 datadump[iface_key]['subnet'] = get_network(addr, mask)
424 try:
425 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
426 except (AttributeError, ValueError, KeyError):
427 dhcp_out[ifname].append(" subnet %(subnet)s netmask %(netmask)s {\n ### not autoritive\n }\n" % datadump[iface_key])
428 continue
429
430 dhcp_part = ".".join(addr.split('.')[0:3])
431 datadump[iface_key]['dhcp_start'] = dhcp_part + "." + dhcp_start
432 datadump[iface_key]['dhcp_stop'] = dhcp_part + "." + dhcp_stop
433 dhcp_out[ifname].append("""\
434 subnet %(subnet)s netmask %(netmask)s {
435 range %(dhcp_start)s %(dhcp_stop)s;
436 option routers %(addr)s;
437 option domain-name-servers %(addr)s;
438 }
439""" % datadump[iface_key])
440
441 for ifname,value in dhcp_out.iteritems():
442 output += ("shared-network %s {\n" % ifname) + ''.join(value) + '}\n\n'
443 return output
444
445
446
447def generate_dnsmasq_conf(datadump):
448 """ Generate configuration file '/usr/local/etc/dnsmasq.conf' """
449 output = generate_header()
450 output += Template("""\
451# DHCP server options
452dhcp-authoritative
453dhcp-fqdn
454domain=dhcp.{{ autogen_fqdn }}
455domain-needed
456expand-hosts
457log-async=100
458
459# Low memory footprint
460cache-size=10000
461
462\n""").render(datadump)
463
464 for iface_key in datadump['autogen_iface_keys']:
465 if not datadump[iface_key].has_key('comment'):
466 datadump[iface_key]['comment'] = None
467 output += "## %(interface)s - %(desc)s - %(comment)s\n" % datadump[iface_key]
468
469 try:
470 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
471 (ip, cidr) = datadump[iface_key]['ip'].split('/')
472 datadump[iface_key]['netmask'] = cidr2netmask(cidr)
473 except (AttributeError, ValueError, KeyError):
474 output += "# not autoritive\n\n"
475 continue
476
477 dhcp_part = ".".join(ip.split('.')[0:3])
478 datadump[iface_key]['dhcp_start'] = dhcp_part + "." + dhcp_start
479 datadump[iface_key]['dhcp_stop'] = dhcp_part + "." + dhcp_stop
480 output += "dhcp-range=%(interface)s,%(dhcp_start)s,%(dhcp_stop)s,%(netmask)s,24h\n\n" % datadump[iface_key]
481
482 return output
483
484
485ileiden_proxies = []
486normal_proxies = []
487rc_conf_local_cache = {}
488
489def generate_rc_conf_local(datadump):
490 """ Generate configuration file '/etc/rc.conf.local' """
491 item = datadump['autogen_item']
492 if rc_conf_local_cache.has_key(item):
493 return rc_conf_local_cache[item]
494
495 if not datadump.has_key('ileiden'):
496 datadump['autogen_ileiden_enable'] = False
497 else:
498 datadump['autogen_ileiden_enable'] = datadump['ileiden']
499
500 datadump['autogen_ileiden_enable'] = switchFormat(datadump['autogen_ileiden_enable'])
501
502 if not ileiden_proxies or not normal_proxies:
503 for proxy in get_proxylist():
504 proxydump = get_yaml(proxy)
505 if proxydump['ileiden']:
506 ileiden_proxies.append(proxydump)
507 else:
508 normal_proxies.append(proxydump)
509 for host in get_hybridlist():
510 hostdump = get_yaml(host)
511 if hostdump['service_proxy_ileiden']:
512 ileiden_proxies.append(hostdump)
513 if hostdump['service_proxy_normal']:
514 normal_proxies.append(hostdump)
515
516 datadump['autogen_ileiden_proxies'] = ileiden_proxies
517 datadump['autogen_normal_proxies'] = normal_proxies
518 datadump['autogen_ileiden_proxies_ips'] = ','.join([x['masterip'] for x in ileiden_proxies])
519 datadump['autogen_ileiden_proxies_names'] = ','.join([x['autogen_item'] for x in ileiden_proxies])
520 datadump['autogen_normal_proxies_ips'] = ','.join([x['masterip'] for x in normal_proxies])
521 datadump['autogen_normal_proxies_names'] = ','.join([x['autogen_item'] for x in normal_proxies])
522
523 output = generate_header("#");
524 output += render_template(datadump, """\
525hostname='{{ autogen_fqdn }}'
526location='{{ location }}'
527nodetype="{{ nodetype }}"
528
529#
530# Configured listings
531#
532captive_portal_whitelist=""
533{% if nodetype == "Proxy" %}
534#
535# Proxy Configuration
536#
537{% if gateway -%}
538defaultrouter="{{ gateway }}"
539{% else -%}
540#defaultrouter="NOTSET"
541{% endif -%}
542internalif="{{ internalif }}"
543ileiden_enable="{{ autogen_ileiden_enable }}"
544gateway_enable="{{ autogen_ileiden_enable }}"
545pf_enable="yes"
546pf_rules="/etc/pf.conf"
547{% if autogen_ileiden_enable -%}
548pf_flags="-D ext_if={{ externalif }} -D int_if={{ internalif }} -D publicnat={80,443}"
549lvrouted_enable="{{ autogen_ileiden_enable }}"
550lvrouted_flags="-u -s s00p3rs3kr3t -m 28"
551{% else -%}
552pf_flags="-D ext_if={{ externalif }} -D int_if={{ internalif }} -D publicnat={0}"
553{% endif -%}
554{% if internalroute -%}
555static_routes="wleiden"
556route_wleiden="-net 172.16.0.0/12 {{ internalroute }}"
557{% endif -%}
558
559{% elif nodetype == "Hybrid" %}
560 #
561 # Hybrid Configuration
562 #
563 list_ileiden_proxies="
564 {% for item in autogen_ileiden_proxies -%}
565 {{ "%-16s"|format(item.masterip) }} # {{ item.autogen_realname }}
566 {% endfor -%}
567 "
568 list_normal_proxies="
569 {% for item in autogen_normal_proxies -%}
570 {{ "%-16s"|format(item.masterip) }} # {{ item.autogen_realname }}
571 {% endfor -%}
572 "
573
574 captive_portal_interfaces="{{ autogen_dhcp_interfaces|join(',')|default('none', true) }}"
575 externalif="{{ externalif|default('vr0', true) }}"
576 masterip="{{ masterip }}"
577
578 # Defined services
579 service_proxy_ileiden="{{ service_proxy_ileiden|yesorno }}"
580 service_proxy_normal="{{ service_proxy_normal|yesorno }}"
581 service_accesspoint="{{ service_accesspoint|yesorno }}"
582 service_incoming_rdr="{{ service_incoming_rdr|yesorno }}"
583 #
584
585 {% if service_proxy_ileiden %}
586 pf_rules="/etc/pf.hybrid.conf"
587 pf_flags="-D ext_if=$externalif -D ext_if_net=$externalif:network -D masterip=$masterip"
588 pf_flags="$pf_flags -D publicnat=80,443"
589 {% elif service_proxy_normal or service_incoming_rdr %}
590 pf_rules="/etc/pf.hybrid.conf"
591 pf_flags="-D ext_if=$externalif -D ext_if_net=$externalif:network -D masterip=$masterip"
592 pf_flags="$pf_flags -D publicnat=0"
593 lvrouted_flags="$lvrouted_flags -z `make_list "$list_ileiden_proxies" ","`"
594 named_setfib="1"
595 tinyproxy_setfib="1"
596 dnsmasq_setfib="1"
597 sshd_setfib="1"
598 {% else %}
599 pf_rules="/etc/pf.node.conf"
600 pf_flags=""
601 lvrouted_flags="$lvrouted_flags -z `make_list "$list_ileiden_proxies" ","`"
602 {% endif %}
603
604 {% if service_proxy_normal %}
605 tinyproxy_enable="yes"
606 {% else %}
607 pen_wrapper_enable="yes"
608 {% endif %}
609
610 {% if service_accesspoint %}
611 pf_flags="$pf_flags -D captive_portal_interfaces=$captive_portal_interfaces"
612 {% endif %}
613
614 {% if board == "ALIX2" %}
615 #
616 # ''Fat'' configuration, board has 256MB RAM
617 #
618 dnsmasq_enable="NO"
619 named_enable="YES"
620 {% if autogen_dhcp_interfaces -%}
621 dhcpd_enable="YES"
622 dhcpd_flags="$dhcpd_flags {{ autogen_dhcp_interfaces|join(' ') }}"
623 {% endif -%}
624 {% endif -%}
625
626 {% if gateway %}
627 defaultrouter="{{ gateway }}"
628 {% endif %}
629{% elif nodetype == "CNode" %}
630#
631# NODE iLeiden Configuration
632#
633
634# iLeiden Proxies {{ autogen_ileiden_proxies_names }}
635list_ileiden_proxies="{{ autogen_ileiden_proxies_ips }}"
636# normal Proxies {{ autogen_normal_proxies_names }}
637list_normal_proxies="{{ autogen_normal_proxies_ips }}"
638
639captive_portal_interfaces="{{ autogen_dhcp_interfaces|join(',') }}"
640
641lvrouted_flags="-u -s s00p3rs3kr3t -m 28 -z $list_ileiden_proxies"
642{% endif %}
643
644#
645# Interface definitions
646#\n
647""")
648
649 # lo0 configuration:
650 # - 172.32.255.1/32 is the proxy.wleiden.net deflector
651 # - masterip is special as it needs to be assigned to at
652 # least one interface, so if not used assign to lo0
653 addrs_list = { 'lo0' : [("127.0.0.1/8", "LocalHost"), ("172.31.255.1/32","Proxy IP")] }
654 iface_map = {'lo0' : 'lo0'}
655 dhclient_if = {'lo0' : False}
656
657 masterip_used = False
658 for iface_key in datadump['autogen_iface_keys']:
659 if datadump[iface_key]['ip'].startswith(datadump['masterip']):
660 masterip_used = True
661 break
662 if not masterip_used:
663 addrs_list['lo0'].append((datadump['masterip'] + "/32", 'Master IP Not used in interface'))
664
665 for iface_key in datadump['autogen_iface_keys']:
666 ifacedump = datadump[iface_key]
667 ifname = ifacedump['autogen_ifname']
668
669 # Flag dhclient is possible
670 dhclient_if[ifname] = ifacedump.has_key('dhcpclient') and ifacedump['dhcpclient']
671
672 # Add interface IP to list
673 item = (ifacedump['ip'], ifacedump['desc'])
674 if addrs_list.has_key(ifname):
675 addrs_list[ifname].append(item)
676 else:
677 addrs_list[ifname] = [item]
678
679 # Alias only needs IP assignment for now, this might change if we
680 # are going to use virtual accesspoints
681 if "alias" in iface_key:
682 continue
683
684 # XXX: Might want to deduct type directly from interface name
685 if ifacedump['type'] in ['11a', '11b', '11g', 'wireless']:
686 # Default to station (client) mode
687 ifacedump['wlanmode'] = "sta"
688 if ifacedump['mode'] in ['master', 'master-wds', 'ap', 'ap-wds']:
689 ifacedump['wlanmode'] = "ap"
690 # Default to 802.11b mode
691 ifacedump['mode'] = '11b'
692 if ifacedump['type'] in ['11a', '11b' '11g']:
693 ifacedump['mode'] = ifacedump['type']
694
695 if not ifacedump.has_key('channel'):
696 if ifacedump['type'] == '11a':
697 ifacedump['channel'] = 36
698 else:
699 ifacedump['channel'] = 1
700
701 # Allow special hacks at the back like wds and stuff
702 if not ifacedump.has_key('extra'):
703 ifacedump['extra'] = 'regdomain ETSI country NL'
704
705 output += "wlans_%(interface)s='%(autogen_ifname)s'\n" % ifacedump
706 output += ("create_args_%(autogen_ifname)s='wlanmode %(wlanmode)s mode " +\
707 "%(mode)s ssid %(ssid)s %(extra)s channel %(channel)s'\n") % ifacedump
708
709 elif ifacedump['type'] in ['ethernet', 'eth']:
710 # No special config needed besides IP
711 pass
712 else:
713 assert False, "Unknown type " + ifacedump['type']
714
715 # Print IP address which needs to be assigned over here
716 output += "\n"
717 for iface,addrs in sorted(addrs_list.iteritems()):
718 for addr, comment in sorted(addrs,key=lambda x: parseaddr(x[0].split('/')[0])):
719 output += "# %s || %s || %s\n" % (iface, addr, comment)
720
721 # Write DHCLIENT entry
722 if dhclient_if[iface]:
723 output += "ifconfig_%s='SYNCDHCP'\n\n" % (iface)
724 else:
725 # Make sure the external address is always first as this is needed in the
726 # firewall setup
727 addrs = sorted(addrs,key=lambda x: x[0].split('.')[0], cmp=lambda x,y: cmp(1 if x == '172' else 0, 1 if y == '172' else 0))
728 output += "ipv4_addrs_%s='%s'\n\n" % (iface, " ".join([x[0] for x in addrs]))
729
730 rc_conf_local_cache[datadump['autogen_item']] = output
731 return output
732
733
734
735
736def get_all_configs():
737 """ Get dict with key 'host' with all configs present """
738 configs = dict()
739 for host in get_hostlist():
740 datadump = get_yaml(host)
741 configs[host] = datadump
742 return configs
743
744
745def get_interface_keys(config):
746 """ Quick hack to get all interface keys, later stage convert this to a iterator """
747 return sorted([elem for elem in config.keys() if (elem.startswith('iface_') and not "lo0" in elem)])
748
749
750def get_used_ips(configs):
751 """ Return array of all IPs used in config files"""
752 ip_list = []
753 for config in configs:
754 ip_list.append(config['masterip'])
755 for iface_key in get_interface_keys(config):
756 l = config[iface_key]['ip']
757 addr, mask = l.split('/')
758 # Special case do not process
759 if valid_addr(addr):
760 ip_list.append(addr)
761 else:
762 logger.error("## IP '%s' in '%s' not valid" % (addr, config['nodename']))
763 return sorted(ip_list)
764
765
766
767def generate_resolv_conf(datadump):
768 """ Generate configuration file '/etc/resolv.conf' """
769 # XXX: This should properly going to be an datastructure soon
770 datadump['autogen_header'] = generate_header("#")
771 datadump['autogen_edge_nameservers'] = ''
772 for host in get_proxylist():
773 hostdump = get_yaml(host)
774 datadump['autogen_edge_nameservers'] += "nameserver %(masterip)-15s # %(autogen_realname)s\n" % hostdump
775 for host in get_hybridlist():
776 hostdump = get_yaml(host)
777 if hostdump['service_proxy_ileiden'] or hostdump['service_proxy_normal']:
778 datadump['autogen_edge_nameservers'] += "nameserver %(masterip)-15s # %(autogen_realname)s\n" % hostdump
779
780 return Template("""\
781{{ autogen_header }}
782search wleiden.net
783
784# Try local (cache) first
785nameserver 127.0.0.1
786
787{% if service_proxy_normal or service_proxy_ileiden or nodetype == 'Proxy' -%}
788nameserver 8.8.8.8 # Google Public NameServer
789nameserver 8.8.4.4 # Google Public NameServer
790{% else -%}
791# START DYNAMIC LIST - updated by /tools/nameserver-shuffle
792{{ autogen_edge_nameservers }}
793{% endif -%}
794""").render(datadump)
795
796
797
798def generate_ntp_conf(datadump):
799 """ Generate configuration file '/etc/ntp.conf' """
800 # XXX: This should properly going to be an datastructure soon
801
802 datadump['autogen_header'] = generate_header("#")
803 datadump['autogen_ntp_servers'] = ''
804 for host in get_proxylist():
805 hostdump = get_yaml(host)
806 datadump['autogen_ntp_servers'] += "server %(masterip)-15s iburst maxpoll 9 # %(autogen_realname)s\n" % hostdump
807 for host in get_hybridlist():
808 hostdump = get_yaml(host)
809 if hostdump['service_proxy_ileiden'] or hostdump['service_proxy_normal']:
810 datadump['autogen_ntp_servers'] += "server %(masterip)-15s iburst maxpoll 9 # %(autogen_realname)s\n" % hostdump
811
812 return Template("""\
813{{ autogen_header }}
814
815{% if service_proxy_normal or service_proxy_ileiden or nodetype == 'Proxy' -%}
816# Machine hooked to internet.
817server 0.nl.pool.ntp.org iburst maxpoll 9
818server 1.nl.pool.ntp.org iburst maxpoll 9
819server 2.nl.pool.ntp.org iburst maxpoll 9
820server 3.nl.pool.ntp.org iburst maxpoll 9
821{% else -%}
822# Local Wireless Leiden NTP Servers.
823server 0.pool.ntp.wleiden.net iburst maxpoll 9
824server 1.pool.ntp.wleiden.net iburst maxpoll 9
825server 2.pool.ntp.wleiden.net iburst maxpoll 9
826server 3.pool.ntp.wleiden.net iburst maxpoll 9
827
828# All the configured NTP servers
829{{ autogen_ntp_servers }}
830{% endif %}
831
832# If a server loses sync with all upstream servers, NTP clients
833# no longer follow that server. The local clock can be configured
834# to provide a time source when this happens, but it should usually
835# be configured on just one server on a network. For more details see
836# http://support.ntp.org/bin/view/Support/UndisciplinedLocalClock
837# The use of Orphan Mode may be preferable.
838#
839server 127.127.1.0
840fudge 127.127.1.0 stratum 10
841""").render(datadump)
842
843
844def generate_pf_hybrid_conf_local(datadump):
845 """ Generate configuration file '/etc/pf.hybrid.conf.local' """
846 datadump['autogen_header'] = generate_header("#")
847 return Template("""\
848{{ autogen_header }}
849
850# Redirect some internal facing services outside (7)
851# INFO: {{ rdr_rules|count }} rdr_rules (outside to internal redirect rules) defined.
852{% for protocol, src_port,dest_ip,dest_port in rdr_rules -%}
853rdr on $ext_if inet proto {{ protocol }} from any to $ext_if port {{ src_port }} tag SRV -> {{ dest_ip }} port {{ dest_port }}
854{% endfor -%}
855""").render(datadump)
856
857def generate_motd(datadump):
858 """ Generate configuration file '/etc/motd' """
859 output = Template("""\
860FreeBSD run ``service motd onestart'' to make me look normal
861
862 WWW: {{ autogen_fqdn }} - http://www.wirelessleiden.nl
863 Loc: {{ location }}
864
865Services:
866{% if board == "ALIX2" -%}
867 - Core Node ({{ board }})
868{% else -%}
869 - Hulp Node ({{ board }})
870{% endif -%}
871{% if service_proxy_normal -%}
872 - Normal Proxy
873{% endif -%}
874{% if service_proxy_ileiden -%}
875 - iLeiden Proxy
876{% endif -%}
877{% if service_incoming_rdr -%}
878 - Incoming port redirects
879{% endif %}
880Interlinks:\n
881""").render(datadump)
882
883 # XXX: This is a hacky way to get the required data
884 for line in generate_rc_conf_local(datadump).split('\n'):
885 if '||' in line and not line[1:].split()[0] in ['lo0', 'ath0'] :
886 output += " - %s \n" % line[1:]
887 output += """\
888Attached bridges:
889"""
890 for iface_key in datadump['autogen_iface_keys']:
891 ifacedump = datadump[iface_key]
892 if ifacedump.has_key('ns_ip'):
893 output += " - %(interface)s || %(mode)s || %(ns_ip)s\n" % ifacedump
894
895 return output
896
897
898def format_yaml_value(value):
899 """ Get yaml value in right syntax for outputting """
900 if isinstance(value,str):
901 output = '"%s"' % value
902 else:
903 output = value
904 return output
905
906
907
908def format_wleiden_yaml(datadump):
909 """ Special formatting to ensure it is editable"""
910 output = "# Genesis config yaml style\n"
911 output += "# vim:ts=2:et:sw=2:ai\n"
912 output += "#\n"
913 iface_keys = [elem for elem in datadump.keys() if elem.startswith('iface_')]
914 for key in sorted(set(datadump.keys()) - set(iface_keys)):
915 if key == 'rdr_rules':
916 output += '%-10s:\n' % 'rdr_rules'
917 for rdr_rule in datadump[key]:
918 output += '- %s\n' % rdr_rule
919 else:
920 output += "%-10s: %s\n" % (key, format_yaml_value(datadump[key]))
921
922 output += "\n\n"
923
924 key_order = [ 'comment', 'interface', 'ip', 'desc', 'sdesc', 'mode', 'type',
925 'extra_type', 'channel', 'ssid', 'dhcp' ]
926
927 for iface_key in sorted(iface_keys):
928 output += "%s:\n" % iface_key
929 for key in key_order + list(sorted(set(datadump[iface_key].keys()) - set(key_order))):
930 if datadump[iface_key].has_key(key):
931 output += " %-11s: %s\n" % (key, format_yaml_value(datadump[iface_key][key]))
932 output += "\n\n"
933
934 return output
935
936
937
938def generate_wleiden_yaml(datadump, header=True):
939 """ Generate (petty) version of wleiden.yaml"""
940 for key in datadump.keys():
941 if key.startswith('autogen_'):
942 del datadump[key]
943 # Interface autogen cleanups
944 elif type(datadump[key]) == dict:
945 for key2 in datadump[key].keys():
946 if key2.startswith('autogen_'):
947 del datadump[key][key2]
948
949 output = generate_header("#") if header else ''
950 output += format_wleiden_yaml(datadump)
951 return output
952
953
954def generate_yaml(datadump):
955 return generate_config(datadump['nodename'], "wleiden.yaml", datadump)
956
957
958
959def generate_config(node, config, datadump=None):
960 """ Print configuration file 'config' of 'node' """
961 output = ""
962 try:
963 # Load config file
964 if datadump == None:
965 datadump = get_yaml(node)
966
967 if config == 'wleiden.yaml':
968 output += generate_wleiden_yaml(datadump)
969 elif config == 'authorized_keys':
970 f = open(os.path.join(NODE_DIR,"global_keys"), 'r')
971 output += f.read()
972 f.close()
973 elif config == 'dnsmasq.conf':
974 output += generate_dnsmasq_conf(datadump)
975 elif config == 'dhcpd.conf':
976 output += generate_dhcpd_conf(datadump)
977 elif config == 'rc.conf.local':
978 output += generate_rc_conf_local(datadump)
979 elif config == 'resolv.conf':
980 output += generate_resolv_conf(datadump)
981 elif config == 'ntp.conf':
982 output += generate_ntp_conf(datadump)
983 elif config == 'motd':
984 output += generate_motd(datadump)
985 elif config == 'pf.hybrid.conf.local':
986 output += generate_pf_hybrid_conf_local(datadump)
987 else:
988 assert False, "Config not found!"
989 except IOError, e:
990 output += "[ERROR] Config file not found"
991 return output
992
993
994
995def process_cgi_request():
996 """ When calling from CGI """
997 # Update repository if requested
998 form = cgi.FieldStorage()
999 if form.getvalue("action") == "update":
1000 print "Refresh: 5; url=."
1001 print "Content-type:text/plain\r\n\r\n",
1002 print "[INFO] Updating subverion, please wait..."
1003 print subprocess.Popen(['svn', 'cleanup', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0],
1004 print subprocess.Popen(['svn', 'up', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0],
1005 print "[INFO] All done, redirecting in 5 seconds"
1006 sys.exit(0)
1007
1008
1009 base_uri = os.environ['PATH_INFO']
1010 uri = base_uri.strip('/').split('/')
1011
1012 output = "Template Holder"
1013 content_type='text/plain'
1014 if base_uri.endswith('/create/network.kml'):
1015 content_type='application/vnd.google-earth.kml+xml'
1016 output = make_network_kml.make_graph()
1017 elif not uri[0]:
1018 if is_text_request():
1019 content_type = 'text/plain'
1020 output = '\n'.join(get_hostlist())
1021 else:
1022 content_type = 'text/html'
1023 output = generate_title(get_hostlist())
1024 elif len(uri) == 1:
1025 if is_text_request():
1026 content_type = 'text/plain'
1027 output = generate_node(uri[0])
1028 else:
1029 content_type = 'text/html'
1030 output = generate_node_overview(uri[0])
1031 elif len(uri) == 2:
1032 content_type = 'text/plain'
1033 output = generate_config(uri[0], uri[1])
1034 else:
1035 assert False, "Invalid option"
1036
1037 print "Content-Type: %s" % content_type
1038 print "Content-Length: %s" % len(output)
1039 print ""
1040 print output
1041
1042def get_realname(datadump):
1043 # Proxy naming convention is special, as the proxy name is also included in
1044 # the nodename, when it comes to the numbered proxies.
1045 if datadump['nodetype'] == 'Proxy':
1046 realname = datadump['nodetype'] + datadump['nodename'].replace('proxy','')
1047 else:
1048 # By default the full name is listed and also a shortname CNAME for easy use.
1049 realname = datadump['nodetype'] + datadump['nodename']
1050 return(realname)
1051
1052
1053
1054def make_dns(output_dir = 'dns', external = False):
1055 items = dict()
1056
1057 # hostname is key, IP is value
1058 wleiden_zone = defaultdict(list)
1059 wleiden_cname = dict()
1060
1061 pool = dict()
1062 for node in get_hostlist():
1063 datadump = get_yaml(node)
1064
1065 # Proxy naming convention is special
1066 fqdn = datadump['autogen_realname']
1067 if datadump['nodetype'] in ['CNode', 'Hybrid']:
1068 wleiden_cname[datadump['nodename']] = fqdn
1069
1070 if datadump.has_key('rdr_host'):
1071 remote_target = datadump['rdr_host']
1072 elif datadump.has_key('remote_access') and datadump['remote_access']:
1073 remote_target = datadump['remote_access'].split(':')[0]
1074 else:
1075 remote_target = None
1076
1077 if remote_target:
1078 try:
1079 parseaddr(remote_target)
1080 wleiden_zone[datadump['nodename'] + '.gw'].append((remote_target, False))
1081 except (IndexError, ValueError):
1082 wleiden_cname[datadump['nodename'] + '.gw'] = remote_target + '.'
1083
1084
1085 wleiden_zone[fqdn].append((datadump['masterip'], True))
1086
1087 # Hacking to get proper DHCP IPs and hostnames
1088 for iface_key in get_interface_keys(datadump):
1089 iface_name = datadump[iface_key]['interface'].replace(':',"-alias-")
1090 (ip, cidr) = datadump[iface_key]['ip'].split('/')
1091 try:
1092 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
1093 datadump[iface_key]['netmask'] = cidr2netmask(cidr)
1094 dhcp_part = ".".join(ip.split('.')[0:3])
1095 if ip != datadump['masterip']:
1096 wleiden_zone["dhcp-gateway-%s.%s" % (iface_name, fqdn)].append((ip, False))
1097 for i in range(int(dhcp_start), int(dhcp_stop) + 1):
1098 wleiden_zone["dhcp-%s-%s.%s" % (i, iface_name, fqdn)].append(("%s.%s" % (dhcp_part, i), True))
1099 except (AttributeError, ValueError, KeyError):
1100 # First push it into a pool, to indentify the counter-part later on
1101 addr = parseaddr(ip)
1102 cidr = int(cidr)
1103 addr = addr & ~((1 << (32 - cidr)) - 1)
1104 if pool.has_key(addr):
1105 pool[addr] += [(iface_name, fqdn, ip)]
1106 else:
1107 pool[addr] = [(iface_name, fqdn, ip)]
1108 continue
1109
1110
1111 def pool_to_name(fqdn, pool_members):
1112 """Convert the joined name to a usable pool name"""
1113
1114 def isplit(item):
1115 (prefix, name, number) = re.match('^(cnode|hybrid|proxy)([a-z]+)([0-9]*)$',item.lower()).group(1,2,3)
1116 return (prefix, name, number)
1117
1118 my_name = isplit(fqdn.split('.')[0])[1]
1119
1120 short_names = defaultdict(list)
1121 for node in sorted(pool_members):
1122 (prefix, name, number) = isplit(node)
1123 short_names[name].append((prefix,number))
1124
1125 return '-'.join(sorted(short_names.keys()))
1126
1127
1128 # WL uses an /29 to configure an interface. IP's are ordered like this:
1129 # MasterA (.1) -- DeviceA (.2) <<>> DeviceB (.3) --- SlaveB (.4)
1130
1131 sn = lambda x: re.sub(r'(?i)^cnode','',x)
1132
1133 # Automatic naming convention of interlinks namely 2 + remote.lower()
1134 for (key,value) in pool.iteritems():
1135 # Make sure they are sorted from low-ip to high-ip
1136 value = sorted(value, key=lambda x: parseaddr(x[2]))
1137
1138 if len(value) == 1:
1139 (iface_name, fqdn, ip) = value[0]
1140 wleiden_zone["2unused-%s.%s" % (iface_name, fqdn)].append((ip, True))
1141
1142 # Device DNS names
1143 if 'cnode' in fqdn.lower():
1144 wleiden_zone["d-at-%s.%s" % (iface_name, fqdn)].append((showaddr(parseaddr(ip) + 1), False))
1145 wleiden_cname["d-at-%s.%s" % (iface_name,sn(fqdn))] = "d-at-%s.%s" % ((iface_name, fqdn))
1146
1147 elif len(value) == 2:
1148 (a_iface_name, a_fqdn, a_ip) = value[0]
1149 (b_iface_name, b_fqdn, b_ip) = value[1]
1150 wleiden_zone["2%s.%s" % (b_fqdn,a_fqdn)].append((a_ip, True))
1151 wleiden_zone["2%s.%s" % (a_fqdn,b_fqdn)].append((b_ip, True))
1152
1153 # Device DNS names
1154 if 'cnode' in a_fqdn.lower() and 'cnode' in b_fqdn.lower():
1155 wleiden_zone["d-at-%s.%s" % (a_iface_name, a_fqdn)].append((showaddr(parseaddr(a_ip) + 1), False))
1156 wleiden_zone["d-at-%s.%s" % (b_iface_name, b_fqdn)].append((showaddr(parseaddr(b_ip) - 1), False))
1157 wleiden_cname["d-at-%s.%s" % (a_iface_name,sn(a_fqdn))] = "d-at-%s.%s" % (a_iface_name, a_fqdn)
1158 wleiden_cname["d-at-%s.%s" % (b_iface_name,sn(b_fqdn))] = "d-at-%s.%s" % (b_iface_name, b_fqdn)
1159 wleiden_cname["d2%s.%s" % (sn(b_fqdn),sn(a_fqdn))] = "d-at-%s.%s" % (a_iface_name, a_fqdn)
1160 wleiden_cname["d2%s.%s" % (sn(a_fqdn),sn(b_fqdn))] = "d-at-%s.%s" % (b_iface_name, b_fqdn)
1161
1162 else:
1163 pool_members = [k[1] for k in value]
1164 for item in value:
1165 (iface_name, fqdn, ip) = item
1166 pool_name = "2pool-" + pool_to_name(fqdn,pool_members)
1167 wleiden_zone["%s.%s" % (pool_name, fqdn)].append((ip, True))
1168
1169 # Include static DNS entries
1170 # XXX: Should they override the autogenerated results?
1171 # XXX: Convert input to yaml more useable.
1172 # Format:
1173 ##; this is a comment
1174 ## roomburgh=CNodeRoomburgh1
1175 ## apkerk1.CNodeVosko=172.17.176.8 ;this as well
1176 dns_list = yaml.load(open(os.path.join(NODE_DIR,'../dns/staticDNS.yaml'),'r'))
1177
1178 # Hack to allow special entries, for development
1179 wleiden_raw = {}
1180
1181 for line in dns_list:
1182 reverse = False
1183 k, items = line.items()[0]
1184 if type(items) == dict:
1185 if items.has_key('reverse'):
1186 reverse = items['reverse']
1187 items = items['a']
1188 else:
1189 items = items['cname']
1190 items = [items] if type(items) != list else items
1191 for item in items:
1192 if item.startswith('IN '):
1193 wleiden_raw[k] = item
1194 elif valid_addr(item):
1195 wleiden_zone[k].append((item, reverse))
1196 else:
1197 wleiden_cname[k] = item
1198
1199 details = dict()
1200 # 24 updates a day allowed
1201 details['serial'] = time.strftime('%Y%m%d%H')
1202
1203 if external:
1204 dns_masters = ['siteview.wirelessleiden.nl', 'ns1.vanderzwet.net']
1205 else:
1206 dns_masters = ['sunny.wleiden.net']
1207
1208 details['master'] = dns_masters[0]
1209 details['ns_servers'] = '\n'.join(['\tNS\t%s.' % x for x in dns_masters])
1210
1211 dns_header = '''
1212$TTL 3h
1213%(zone)s. SOA %(master)s. beheer.lijst.wirelessleiden.nl. ( %(serial)s 1d 12h 1w 60s )
1214 ; Serial, Refresh, Retry, Expire, Neg. cache TTL
1215
1216%(ns_servers)s
1217 \n'''
1218
1219
1220 if not os.path.isdir(output_dir):
1221 os.makedirs(output_dir)
1222 details['zone'] = 'wleiden.net'
1223 f = open(os.path.join(output_dir,"db." + details['zone']), "w")
1224 f.write(dns_header % details)
1225
1226 for host,items in wleiden_zone.iteritems():
1227 for ip,reverse in items:
1228 if ip not in ['0.0.0.0']:
1229 f.write("%s.wleiden.net. IN A %s \n" % (host.lower(), ip))
1230 for source,dest in wleiden_cname.iteritems():
1231 dest = dest if dest.endswith('.') else dest + ".wleiden.net."
1232 f.write("%s.wleiden.net. IN CNAME %s\n" % (source.lower(), dest.lower()))
1233 for source, dest in wleiden_raw.iteritems():
1234 f.write("%s.wleiden.net. %s\n" % (source, dest))
1235 f.close()
1236
1237 # Create whole bunch of specific sub arpa zones. To keep it compliant
1238 for s in range(16,32):
1239 details['zone'] = '%i.172.in-addr.arpa' % s
1240 f = open(os.path.join(output_dir,"db." + details['zone']), "w")
1241 f.write(dns_header % details)
1242
1243 #XXX: Not effient, fix to proper data structure and do checks at other
1244 # stages
1245 for host,items in wleiden_zone.iteritems():
1246 for ip,reverse in items:
1247 if not reverse:
1248 continue
1249 if valid_addr(ip):
1250 if valid_addr(ip):
1251 if int(ip.split('.')[1]) == s:
1252 rev_ip = '.'.join(reversed(ip.split('.')))
1253 f.write("%s.in-addr.arpa. IN PTR %s.wleiden.net.\n" % (rev_ip.lower(), host.lower()))
1254 f.close()
1255
1256
1257def usage():
1258 print """Usage: %(prog)s <argument>
1259Argument:
1260\tstandalone [port] = Run configurator webserver [8000]
1261\tdns [outputdir] = Generate BIND compliant zone files in dns [./dns]
1262\tfull-export = Generate yaml export script for heatmap.
1263\tstatic [outputdir] = Generate all config files and store on disk
1264\t with format ./<outputdir>/%%NODE%%/%%FILE%% [./static]
1265\ttest <node> <file> = Receive output of CGI script.
1266\tlist <status> <items> = List systems which have certain status
1267
1268Arguments:
1269\t<node> = NodeName (example: HybridRick)
1270\t<file> = %(files)s
1271\t<status> = all|up|down|planned
1272\t<items> = systems|nodes|proxies
1273
1274NOTE FOR DEVELOPERS; you can test your changes like this:
1275 BEFORE any changes in this code:
1276 $ ./gformat.py static /tmp/pre
1277 AFTER the changes:
1278 $ ./gformat.py static /tmp/post
1279 VIEW differences and VERIFY all are OK:
1280 $ diff -urI 'Generated' -r /tmp/pre /tmp/post
1281""" % { 'prog' : sys.argv[0], 'files' : '|'.join(files) }
1282 exit(0)
1283
1284
1285def is_text_request():
1286 """ Find out whether we are calling from the CLI or any text based CLI utility """
1287 try:
1288 return os.environ['HTTP_USER_AGENT'].split()[0] in ['curl', 'fetch', 'wget']
1289 except KeyError:
1290 return True
1291
1292def switchFormat(setting):
1293 if setting:
1294 return "YES"
1295 else:
1296 return "NO"
1297
1298def main():
1299 """Hard working sub"""
1300 # Allow easy hacking using the CLI
1301 if not os.environ.has_key('PATH_INFO'):
1302 if len(sys.argv) < 2:
1303 usage()
1304
1305 if sys.argv[1] == "standalone":
1306 import SocketServer
1307 import CGIHTTPServer
1308 # Hop to the right working directory.
1309 os.chdir(os.path.dirname(__file__))
1310 try:
1311 PORT = int(sys.argv[2])
1312 except (IndexError,ValueError):
1313 PORT = 8000
1314
1315 class MyCGIHTTPRequestHandler(CGIHTTPServer.CGIHTTPRequestHandler):
1316 """ Serve this CGI from the root of the webserver """
1317 def is_cgi(self):
1318 if "favicon" in self.path:
1319 return False
1320
1321 self.cgi_info = (os.path.basename(__file__), self.path)
1322 self.path = ''
1323 return True
1324 handler = MyCGIHTTPRequestHandler
1325 SocketServer.TCPServer.allow_reuse_address = True
1326 httpd = SocketServer.TCPServer(("", PORT), handler)
1327 httpd.server_name = 'localhost'
1328 httpd.server_port = PORT
1329
1330 logger.info("serving at port %s", PORT)
1331 try:
1332 httpd.serve_forever()
1333 except KeyboardInterrupt:
1334 httpd.shutdown()
1335 logger.info("All done goodbye")
1336 elif sys.argv[1] == "test":
1337 os.environ['PATH_INFO'] = "/".join(sys.argv[2:])
1338 os.environ['SCRIPT_NAME'] = __file__
1339 process_cgi_request()
1340 elif sys.argv[1] == "static":
1341 items = dict()
1342 items['output_dir'] = sys.argv[2] if len(sys.argv) > 2 else "./static"
1343 for node in get_hostlist():
1344 items['node'] = node
1345 items['wdir'] = "%(output_dir)s/%(node)s" % items
1346 if not os.path.isdir(items['wdir']):
1347 os.makedirs(items['wdir'])
1348 datadump = get_yaml(node)
1349 for config in files:
1350 items['config'] = config
1351 logger.info("## Generating %(node)s %(config)s" % items)
1352 f = open("%(wdir)s/%(config)s" % items, "w")
1353 f.write(generate_config(node, config, datadump))
1354 f.close()
1355 elif sys.argv[1] == "wind-export":
1356 items = dict()
1357 for node in get_hostlist():
1358 datadump = get_yaml(node)
1359 sql = """INSERT IGNORE INTO nodes (name, name_ns, longitude, latitude)
1360 VALUES ('%(nodename)s', '%(nodename)s', %(latitude)s, %(longitude)s);""" % datadump;
1361 sql = """INSERT IGNORE INTO users_nodes (user_id, node_id, owner)
1362 VALUES (
1363 (SELECT id FROM users WHERE username = 'rvdzwet'),
1364 (SELECT id FROM nodes WHERE name = '%(nodename)s'),
1365 'Y');""" % datadump
1366 #for config in files:
1367 # items['config'] = config
1368 # print "## Generating %(node)s %(config)s" % items
1369 # f = open("%(wdir)s/%(config)s" % items, "w")
1370 # f.write(generate_config(node, config, datadump))
1371 # f.close()
1372 for node in get_hostlist():
1373 datadump = get_yaml(node)
1374 for iface_key in sorted([elem for elem in datadump.keys() if elem.startswith('iface_')]):
1375 ifacedump = datadump[iface_key]
1376 if ifacedump.has_key('mode') and ifacedump['mode'] == 'ap-wds':
1377 ifacedump['nodename'] = datadump['nodename']
1378 if not ifacedump.has_key('channel') or not ifacedump['channel']:
1379 ifacedump['channel'] = 0
1380 sql = """INSERT INTO links (node_id, type, ssid, protocol, channel, status)
1381 VALUES ((SELECT id FROM nodes WHERE name = '%(nodename)s'), 'ap',
1382 '%(ssid)s', 'IEEE 802.11b', %(channel)s, 'active');""" % ifacedump
1383 elif sys.argv[1] == "full-export":
1384 hosts = {}
1385 for node in get_hostlist():
1386 datadump = get_yaml(node)
1387 hosts[datadump['nodename']] = datadump
1388 print yaml.dump(hosts)
1389
1390 elif sys.argv[1] == "dns":
1391 make_dns(sys.argv[2] if len(sys.argv) > 2 else 'dns', 'external' in sys.argv)
1392 elif sys.argv[1] == "cleanup":
1393 # First generate all datadumps
1394 datadumps = dict()
1395 ssid_to_node = dict()
1396 for host in get_hostlist():
1397 logger.info("# Processing: %s", host)
1398 # Set some boring default values
1399 datadump = { 'board' : 'UNKNOWN' }
1400 datadump.update(get_yaml(host))
1401 datadumps[datadump['autogen_realname']] = datadump
1402
1403 (poel, errors) = make_relations(datadumps)
1404 print "\n".join(["# WARNING: %s" % x for x in errors])
1405
1406 for host,datadump in datadumps.iteritems():
1407 # Convert all yes and no to boolean values
1408 def fix_boolean(dump):
1409 for key in dump.keys():
1410 if type(dump[key]) == dict:
1411 dump[key] = fix_boolean(dump[key])
1412 elif str(dump[key]).lower() in ["yes", "true"]:
1413 dump[key] = True
1414 elif str(dump[key]).lower() in ["no", "false"]:
1415 # Compass richting no (Noord Oost) is valid input
1416 if key != "compass": dump[key] = False
1417 return dump
1418 datadump = fix_boolean(datadump)
1419
1420 if datadump['rdnap_x'] and datadump['rdnap_y']:
1421 datadump['latitude'], datadump['longitude'] = rdnap.rd2etrs(datadump['rdnap_x'], datadump['rdnap_y'])
1422 elif datadump['latitude'] and datadump['longitude']:
1423 datadump['rdnap_x'], datadump['rdnap_y'] = rdnap.etrs2rd(datadump['latitude'], datadump['longitude'])
1424
1425 if datadump['nodename'].startswith('Proxy'):
1426 datadump['nodename'] = datadump['nodename'].lower()
1427
1428 for iface_key in datadump['autogen_iface_keys']:
1429 # All our normal wireless cards are normal APs now
1430 if datadump[iface_key]['type'] in ['11a', '11b', '11g', 'wireless']:
1431 datadump[iface_key]['mode'] = 'ap'
1432 # Wireless Leiden SSID have an consistent lowercase/uppercase
1433 if datadump[iface_key].has_key('ssid'):
1434 ssid = datadump[iface_key]['ssid']
1435 prefix = 'ap-WirelessLeiden-'
1436 if ssid.lower().startswith(prefix.lower()):
1437 datadump[iface_key]['ssid'] = prefix + ssid[len(prefix)].upper() + ssid[len(prefix) + 1:]
1438 if datadump[iface_key].has_key('ns_ip') and not datadump[iface_key].has_key('mode'):
1439 datadump[iface_key]['mode'] = 'autogen-FIXME'
1440 if not datadump[iface_key].has_key('desc'):
1441 datadump[iface_key]['desc'] = 'autogen-FIXME'
1442 # Set the compass value based on the angle between the poels
1443 if datadump[iface_key].has_key('ns_ip'):
1444 my_pool = poel[network(datadump[iface_key]['ip'])]
1445 remote_hosts = list(set([x[0] for x in my_pool]) - set([host]))
1446 if remote_hosts:
1447 compass_target = remote_hosts[0]
1448 datadump[iface_key]['compass'] = cd_between_hosts(host, compass_target, datadumps)
1449
1450 store_yaml(datadump)
1451 elif sys.argv[1] == "list":
1452 use_fqdn = False
1453 if len(sys.argv) < 4 or not sys.argv[2] in ["up", "down", "planned", "all"]:
1454 usage()
1455 if sys.argv[3] == "nodes":
1456 systems = get_nodelist()
1457 elif sys.argv[3] == "proxies":
1458 systems = get_proxylist()
1459 elif sys.argv[3] == "systems":
1460 systems = get_hostlist()
1461 else:
1462 usage()
1463 if len(sys.argv) > 4:
1464 if sys.argv[4] == "fqdn":
1465 use_fqdn = True
1466 else:
1467 usage()
1468
1469 for system in systems:
1470 datadump = get_yaml(system)
1471
1472 output = datadump['autogen_fqdn'] if use_fqdn else system
1473 if sys.argv[2] == "all":
1474 print output
1475 elif datadump['status'] == sys.argv[2]:
1476 print output
1477 elif sys.argv[1] == "create":
1478 if sys.argv[2] == "network.kml":
1479 print make_network_kml.make_graph()
1480 else:
1481 usage()
1482 usage()
1483 else:
1484 # Do not enable debugging for config requests as it highly clutters the output
1485 if not is_text_request():
1486 cgitb.enable()
1487 process_cgi_request()
1488
1489
1490if __name__ == "__main__":
1491 main()
Note: See TracBrowser for help on using the repository browser.