#!/usr/bin/python import cgi import os import re from config import readConfigFile from time import time from xml.dom.minidom import parse def getIds(): group_id='' host_id='' f=open('/etc/sysconfig/vpn', 'r') lines=f.readlines() f.close() for line in lines: if line[-1]=='\n': line=line[:-1] if 'GROUP_ID' in line: group_id=re.sub('^GROUP_ID="(.*)"', '\\1', line) if 'HOST_ID' in line: host_id=re.sub('^HOST_ID="(.*)"', '\\1', line) return (group_id, host_id) def getInterfaces(): try: f=open('/proc/net/dev', 'r') try: int_list=f.readlines() finally: f.close() #keep only interface's name int_list=[ int.split(':')[0] for int in int_list[2:] ] except IOError: int_list="" return int_list def removeLtGt(lines): newlines=[] for line in lines: line=re.sub('<','<',line) line=re.sub('>','>',line) newlines += [line] return newlines def executeCommand(command): try: f=os.popen(command, 'r') try: command_result=f.readlines() finally: f.close() except IOError: command_result="" return command_result #header print "Content-Type: text/html" print print ''' %s
''' % 'Nexedi-VPN web interface' form=cgi.FieldStorage() action=len(form.getlist('action')) and form.getlist('action')[0] or 'check' if action=='update': (group_id, host_id) = getIds() modem_type=len(form.getlist('modem_type')) and form.getlist('modem_type')[0] or '' modem_device=len(form.getlist('modem_device')) and form.getlist('modem_device')[0] or '' connection_type=len(form.getlist('connection_type')) and form.getlist('connection_type')[0] or '' ppp_user=len(form.getlist('ppp_user')) and form.getlist('ppp_user')[0] or '' ppp_password=len(form.getlist('ppp_password')) and form.getlist('ppp_password')[0] or '' conf_dict = {'modem_type':modem_type, 'modem_device':modem_device, 'connection_type':connection_type, 'ppp_user':ppp_user, 'ppp_password':ppp_password,} dom = parse('config.xml') hosts = dom.getElementsByTagName('host') for host in hosts: if host.getAttribute('id') == host_id: break changed = False for element in conf_dict.keys(): if host.getElementsByTagName(element)[0].childNodes[0].data != conf_dict[element]: host.getElementsByTagName(element)[0].childNodes[0].data = conf_dict[element] changed = True if changed: f=open('config.xml','w') dom.writexml(f) f.close() dom.unlink() print '''
%s
Back
''' % (changed and 'Changes have been made, rebuild is required' or 'No changes were made, rebuild is not required') elif action=='rebuild': (group_id, host_id) = getIds() print '''
''' build_error=False lines=executeCommand('sudo -u root /usr/bin/python /var/lib/thttpd/cgi-bin/autoconf.py %s %s 2>&1' % (group_id, host_id)) if len(lines) != 0: build_error=True print '' else: print '' if not build_error: lines=executeCommand('sudo -u root /bin/cp /var/lib/thttpd/cgi-bin/data/%s/%s/config.tgz /mnt/cf/config.tgz 2>&1' % (group_id, host_id)) lines += executeCommand('/bin/sync 2>&1') if len(lines) != 0: print '' else: print '' print'''
Rebuilding configuration

An error occured while building configuration

' print 'Output :
'
    print ''.join(lines),
    print '
Configuration rebuilt

An error occured while replacing configuration

' print 'Output :
'
      print ''.join(lines),
      print '
Configuration replaced, you can reboot the VPN
''' elif action=='check': print '''
''' modules = executeCommand("/sbin/lsmod | grep '\(usb\|hcd\)'") modules=removeLtGt(modules) if len(modules)==0: modules=['No USB module is loaded'] print '' % ''.join(modules) print '''''' devices = executeCommand("cat /proc/bus/usb/devices | grep '^S'") dev_names=[re.sub('^.*Product=(.*)$','\\1',name) for name in devices if 'Product' in name] dev_names=removeLtGt(dev_names) if len(dev_names)==0: dev_names=['No USB device is present'] print '' % ''.join(dev_names) print '''''' eagle_present=False for module in modules: if 'eagle' in module: eagle_present=True break if not eagle_present: print '' else: eagle = executeCommand("/usr/sbin/eaglestat") if len(eagle) > 4: eagle = [eagle[-2]] print '' % eagle[0] print '''''' pppd_log = executeCommand('(sudo -u root /bin/cat /var/log/messages) | grep pppd') if len(pppd_log)==0: pppd_log=['Pppd log is empty'] if len(pppd_log)>10: pppd_log=pppd_log[-10:] pppd_log=removeLtGt(pppd_log) print '' % ''.join(pppd_log) print '''''' int_list = getInterfaces() int_stat = [] for int in int_list: if 'ppp' in int: int_stat += executeCommand('/sbin/ip addr show dev %s' % int) if len(int_stat) == 0: int_stat = ['No ppp interface is up'] int_stat=removeLtGt(int_stat) print '' % ''.join(int_stat) print '''
Please check your WAN configuration
USB modules
%s
USB devices
%s
Eagle-usb status
Eagle-usb module not loaded
%s
Pppd messages log
%s
Currently running pppd
%s
''' else: #action=='display' (group_id, host_id) = getIds() config = readConfigFile('config.xml') group = config.find(group_id) host = group.find(host_id) select=lambda a, b : a==b and 'selected' or '' print '''
''' print '' % host.ppp_user print '''''' print '' % host.ppp_password print '''
Please check your WAN configuration
Modem type
Modem device
Connection type
Ppp username
Ppp password
Check WAN Status
''' #footer print '''
'''