def set_ip(self): # If we've made it this far, the user's decided to (re)configure a # network interface. Full steam ahead, damn the torpedoes! # First, take the wireless NIC offline so its mode can be changed. command = ['/sbin/ifconfig', self.mesh_interface, 'down'] output = subprocess.Popen(command) time.sleep(5) # Wrap this whole process in a loop to ensure that stubborn wireless # interfaces are configured reliably. The wireless NIC has to make it # all the way through one iteration of the loop without errors before # we can go on. while True: # Set the mode, ESSID and channel. command = ['/sbin/iwconfig', self.mesh_interface, 'mode ad-hoc'] output = subprocess.Popen(command) command = ['/sbin/iwconfig', self.mesh_interface, 'essid', self.essid] output = subprocess.Popen(command) command = ['/sbin/iwconfig', self.mesh_interface, 'channel', self.channel] output = subprocess.Popen(command) # Run iwconfig again and capture the current wireless configuration. command = ['/sbin/iwconfig', self.mesh_interface] output = subprocess.Popen(command).stdout configuration = output.readlines() # Test the interface by going through the captured text to see if # it's in ad-hoc mode. If it's not, put it in ad-hoc mode and go # back to the top of the loop to try again. for line in configuration: if 'Mode' in line: line = line.strip() mode = line.split(' ')[0].split(':')[1] if mode != 'Ad-Hoc': continue # Test the ESSID to see if it's been set properly. for line in configuration: if 'ESSID' in line: line = line.strip() essid = line.split(' ')[-1].split(':')[1] if essid != self.essid: continue # Check the wireless channel to see if it's been set properly. for line in configuration: if 'Frequency' in line: line = line.strip() frequency = line.split(' ')[2].split(':')[1] if frequency != self.frequency: continue # "Victory is mine!" # --Stewie, _Family Guy_ break # Call ifconfig and set up the network configuration information. command = ['/sbin/ifconfig', self.mesh_interface, self.mesh_ip, 'netmask', self.mesh_netmask, 'up'] output = subprocess.Popen(command) time.sleep(5) # Add the client interface. command = ['/sbin/ifconfig', self.client_interface, self.client_ip, 'up'] output = subprocess.Popen(command) template = ('yes', self.channel, self.essid, self.mesh_interface, self.client_interface, self.mesh_interface) _utils.set_wireless_db_entry(self.netconfdb, template) # Send this information to the methods that write the /etc/hosts and # dnsmasq config files. networkconfiguration.make_hosts(self.hosts_file, self.test, starting_ip=self.client_ip) networkconfiguration.configure_dnsmasq(self.dnsmasq_include_file, self.test, starting_ip=self.client_ip) # Render and display the page. try: page = self.templatelookup.get_template("/network/done.html") return page.render(title = "Network interface configured.", purpose_of_page = "Configured!", interface = self.mesh_interface, ip_address = self.mesh_ip, netmask = self.mesh_netmask, client_ip = self.client_ip, client_netmask = self.client_netmask) except: _utils.output_error_data()
def set_ip(self): # If we've made it this far, the user's decided to (re)configure a # network interface. Full steam ahead, damn the torpedoes! # First, take the wireless NIC offline so its mode can be changed. command = ['/sbin/ifconfig', self.mesh_interface, 'down'] output = subprocess.Popen(command) time.sleep(5) # Wrap this whole process in a loop to ensure that stubborn wireless # interfaces are configured reliably. The wireless NIC has to make it # all the way through one iteration of the loop without errors before # we can go on. while True: # Set the mode, ESSID and channel. command = ['/sbin/iwconfig', self.mesh_interface, 'mode ad-hoc'] output = subprocess.Popen(command) command = [ '/sbin/iwconfig', self.mesh_interface, 'essid', self.essid ] output = subprocess.Popen(command) command = [ '/sbin/iwconfig', self.mesh_interface, 'channel', self.channel ] output = subprocess.Popen(command) # Run iwconfig again and capture the current wireless configuration. command = ['/sbin/iwconfig', self.mesh_interface] output = subprocess.Popen(command).stdout configuration = output.readlines() # Test the interface by going through the captured text to see if # it's in ad-hoc mode. If it's not, put it in ad-hoc mode and go # back to the top of the loop to try again. for line in configuration: if 'Mode' in line: line = line.strip() mode = line.split(' ')[0].split(':')[1] if mode != 'Ad-Hoc': continue # Test the ESSID to see if it's been set properly. for line in configuration: if 'ESSID' in line: line = line.strip() essid = line.split(' ')[-1].split(':')[1] if essid != self.essid: continue # Check the wireless channel to see if it's been set properly. for line in configuration: if 'Frequency' in line: line = line.strip() frequency = line.split(' ')[2].split(':')[1] if frequency != self.frequency: continue # "Victory is mine!" # --Stewie, _Family Guy_ break # Call ifconfig and set up the network configuration information. command = [ '/sbin/ifconfig', self.mesh_interface, self.mesh_ip, 'netmask', self.mesh_netmask, 'up' ] output = subprocess.Popen(command) time.sleep(5) # Add the client interface. command = [ '/sbin/ifconfig', self.client_interface, self.client_ip, 'up' ] output = subprocess.Popen(command) template = ('yes', self.channel, self.essid, self.mesh_interface, self.client_interface, self.mesh_interface) _utils.set_wireless_db_entry(self.netconfdb, template) # Send this information to the methods that write the /etc/hosts and # dnsmasq config files. networkconfiguration.make_hosts(self.hosts_file, self.test, starting_ip=self.client_ip) networkconfiguration.configure_dnsmasq(self.dnsmasq_include_file, self.test, starting_ip=self.client_ip) # Render and display the page. try: page = self.templatelookup.get_template("/network/done.html") return page.render(title="Network interface configured.", purpose_of_page="Configured!", interface=self.mesh_interface, ip_address=self.mesh_ip, netmask=self.mesh_netmask, client_ip=self.client_ip, client_netmask=self.client_netmask) except: _utils.output_error_data()
def set_ip(self): logging.debug("Entered NetworkConfiguration.set_ip().") # Set up the error catcher variable. error = [] # Define the PID of the captive portal daemon in the topmost context # of this method. portal_pid = 0 # If we've made it this far, the user's decided to (re)configure a # network interface. Full steam ahead, damn the torpedoes! # First, take the wireless NIC offline so its mode can be changed. self.update_mesh_interface_status('down') time.sleep(5) # Wrap this whole process in a loop to ensure that stubborn wireless # interfaces are configured reliably. The wireless NIC has to make it # all the way through one iteration of the loop without errors before # we can go on. while True: logging.debug("At top of wireless configuration loop.") chunks = { "mode": ("mode", "ad-hoc"), "ESSID": ("essid", self.essid), "BSSID": ("ap", self.bssid), "channel": ("channel", self.channel) } for k, v in chunks.iteritems(): logging.debug("Configuring wireless interface: %s = %s", k, v) command = ['/sbin/iwconfig', self.mesh_interface] command.extend(v) if self.test: logging.debug( "NetworkConfiguration.set_ip() command to set the %s: %s", k, ' '.join(command)) else: subprocess.Popen(command) time.sleep(1) # Run iwconfig again and capture the current wireless configuration. command = ['/sbin/iwconfig', self.mesh_interface] configuration = '' if self.test: logging.debug( "NetworkConfiguration.set_ip()command to capture the current state of a network interface: %s", command) else: output = subprocess.Popen(command, stdout=subprocess.PIPE).stdout configuration = output.readlines() break_flag = False # Test the interface by going through the captured text to see if # it's in ad-hoc mode. If it's not, go back to the top of the # loop to try again. for line in configuration: if re.search("Mode|ESSID|Cell|Frequency", line): line = line.split(' ') else: continue if 'Mode' in line: mode = line[0].split(':')[1] if mode != 'Ad-Hoc': logging.debug( "Uh-oh! Not in ad-hoc mode! Starting over.") break_flag = True break # Test the ESSID to see if it's been set properly. if 'ESSID' in line: essid = line[-1].split(':')[1] if essid != self.essid: logging.debug( "Uh-oh! ESSID wasn't set! Starting over.") break_flag = True break # Test the BSSID to see if it's been set properly. if 'Cell' in line: bssid = line[-1] if bssid != self.bssid: logging.debug( "Uh-oh! BSSID wasn't set! Starting over.") break_flag = True break # Check the wireless channel to see if it's been set properly. if 'Frequency' in line: frequency = line[2].split(':')[1] if frequency != self.frequency: logging.debug( "Uh-oh! Wireless channel wasn't set! starting over." ) break_flag = True break logging.debug("Hit bottom of the wireless configuration loop.") # For the purpose of testing, exit after one iteration so we don't # get stuck in an infinite loop. if self.test: break # "Victory is mine!" # --Stewie, _Family Guy_ if not (break_flag): break logging.debug("Wireless interface configured successfully.") # Call ifconfig and set up the network configuration information. logging.debug( "Setting IP configuration information on wireless interface.") command = [ '/sbin/ifconfig', self.mesh_interface, self.mesh_ip, 'netmask', self.mesh_netmask, 'up' ] if self.test: logging.debug( "NetworkConfiguration.set_ip()command to set the IP configuration of the mesh interface: %s", command) else: subprocess.Popen(command) time.sleep(5) # Add the client interface. logging.debug("Adding client interface.") command = [ '/sbin/ifconfig', self.client_interface, self.client_ip, 'up' ] if self.test: logging.debug( "NetworkConfiguration.set_ip()command to set the IP configuration of the client interface: %s", command) else: subprocess.Popen(command) template = ('yes', self.channel, self.essid, self.mesh_interface, self.client_interface, self.mesh_interface) _utils.set_wireless_db_entry(self.netconfdb, template) # Start the captive portal daemon. This will also initialize the IP # tables ruleset for the client interface. logging.debug("Starting captive portal daemon.") captive_portal_daemon = [ '/usr/local/sbin/captive_portal.py', '-i', str(self.mesh_interface), '-a', self.client_ip, '-d' ] captive_portal_return = 0 if self.test: logging.debug( "NetworkConfiguration.set_ip() command to start the captive portal daemon: %s", captive_portal_daemon) captive_portal_return = 6 else: captive_portal_return = subprocess.Popen(captive_portal_daemon) logging.debug( "Sleeping for 5 seconds to see if a race condition is the reason we can't get the PID of the captive portal daemon." ) time.sleep(5) # Now do some error checking. warnings = "<p>WARNING! captive_portal.py exited with code %d - %s!</p>\n" if captive_portal_return == 1: error.append( warnings % (captive_portal_return, "insufficient command line arguments passed to daemon")) elif captive_portal_return == 2: error.append( warnings % (captive_portal_return, "bad arguments passed to daemon")) elif captive_portal_return == 3: error.append( warnings % (captive_portal_return, "bad IP tables commands during firewall initialization")) elif captive_portal_return == 4: error.append( warnings % (captive_portal_return, "bad parameters passed to IP tables")) elif captive_portal_return == 5: error.append( warnings % (captive_portal_return, "daemon already running on interface")) elif captive_portal_return == 6: error.append( "<p>NOTICE: captive_portal.py started in TEST mode - did not actually start up!</p>\n" ) else: logging.debug("Getting PID of captive portal daemon.") # If the captive portal daemon started successfully, get its PID. # Note that we have to take into account both regular and test mode. captive_portal_pidfile = 'captive_portal.' + self.mesh_interface if os.path.exists('/var/run/' + captive_portal_pidfile): captive_portal_pidfile = '/var/run/' + captive_portal_pidfile elif os.path.exists('/tmp/' + captive_portal_pidfile): captive_portal_pidfile = '/tmp/' + captive_portal_pidfile else: error.append( "<p>WARNING: Unable to open captive portal PID file " + captive_portal_pidfile + "</p>\n") logging.debug( "Unable to find PID file %s of captive portal daemon.", captive_portal_pidfile) # Try to open the PID file. logging.debug("Trying to open %s.", captive_portal_pidfile) portal_pid = 0 try: pidfile = open(str(captive_portal_pidfile), 'r') portal_pid = pidfile.readline() pidfile.close() except: error.append( "<p>WARNING: Unable to open captive portal PID file " + captive_portal_pidfile + "</p>\n") logging.debug("value of portal_pid is %s.", portal_pid) if self.test: logging.debug("Faking PID of captive_portal.py.") portal_pid = "Insert clever PID for captive_portal.py here." if not portal_pid: portal_pid = "ERROR: captive_portal.py failed, returned code " + str( captive_portal_return) + "." logging.debug( "Captive portal daemon failed to start. Exited with code %s.", str(captive_portal_return)) # Send this information to the methods that write the /etc/hosts and # dnsmasq config files. logging.debug("Generating dnsmasq configuration files.") problem = make_hosts(self.hosts_file, self.test, starting_ip=self.client_ip) if problem: error.append( "<p>WARNING! /etc/hosts.mesh not generated! Something went wrong!</p>" ) logging.debug("Couldn't generate /etc/hosts.mesh!") configure_dnsmasq(self.dnsmasq_include_file, self.test, starting_ip=self.client_ip) # Render and display the page. try: page = self.templatelookup.get_template("/network/done.html") return page.render(title="Network interface configured.", purpose_of_page="Configured!", error=''.join(error), interface=self.mesh_interface, ip_address=self.mesh_ip, netmask=self.mesh_netmask, portal_pid=portal_pid, client_ip=self.client_ip, client_netmask=self.client_netmask) except: _utils.output_error_data()
def set_ip(self): logging.debug("Entered NetworkConfiguration.set_ip().") # Set up the error catcher variable. error = [] # Define the PID of the captive portal daemon in the topmost context # of this method. portal_pid = 0 # If we've made it this far, the user's decided to (re)configure a # network interface. Full steam ahead, damn the torpedoes! # First, take the wireless NIC offline so its mode can be changed. self.update_mesh_interface_status('down') time.sleep(5) # Wrap this whole process in a loop to ensure that stubborn wireless # interfaces are configured reliably. The wireless NIC has to make it # all the way through one iteration of the loop without errors before # we can go on. while True: logging.debug("At top of wireless configuration loop.") chunks = {"mode": ("mode", "ad-hoc"), "ESSID": ("essid", self.essid), "BSSID": ("ap", self.bssid), "channel": ("channel", self.channel)} for k, v in chunks.iteritems(): logging.debug("Configuring wireless interface: %s = %s", k, v) command = ['/sbin/iwconfig', self.mesh_interface] command.extend(v) if self.test: logging.debug("NetworkConfiguration.set_ip() command to set the %s: %s", k, ' '.join(command)) else: subprocess.Popen(command) time.sleep(1) # Run iwconfig again and capture the current wireless configuration. command = ['/sbin/iwconfig', self.mesh_interface] configuration = '' if self.test: logging.debug("NetworkConfiguration.set_ip()command to capture the current state of a network interface: %s", command) else: output = subprocess.Popen(command, stdout=subprocess.PIPE).stdout configuration = output.readlines() break_flag = False # Test the interface by going through the captured text to see if # it's in ad-hoc mode. If it's not, go back to the top of the # loop to try again. for line in configuration: if re.search("Mode|ESSID|Cell|Frequency", line): line = line.split(' ') else: continue if 'Mode' in line: mode = line[0].split(':')[1] if mode != 'Ad-Hoc': logging.debug("Uh-oh! Not in ad-hoc mode! Starting over.") break_flag = True break # Test the ESSID to see if it's been set properly. if 'ESSID' in line: essid = line[-1].split(':')[1] if essid != self.essid: logging.debug("Uh-oh! ESSID wasn't set! Starting over.") break_flag = True break # Test the BSSID to see if it's been set properly. if 'Cell' in line: bssid = line[-1] if bssid != self.bssid: logging.debug("Uh-oh! BSSID wasn't set! Starting over.") break_flag = True break # Check the wireless channel to see if it's been set properly. if 'Frequency' in line: frequency = line[2].split(':')[1] if frequency != self.frequency: logging.debug("Uh-oh! Wireless channel wasn't set! starting over.") break_flag = True break logging.debug("Hit bottom of the wireless configuration loop.") # For the purpose of testing, exit after one iteration so we don't # get stuck in an infinite loop. if self.test: break # "Victory is mine!" # --Stewie, _Family Guy_ if not(break_flag): break logging.debug("Wireless interface configured successfully.") # Call ifconfig and set up the network configuration information. logging.debug("Setting IP configuration information on wireless interface.") command = ['/sbin/ifconfig', self.mesh_interface, self.mesh_ip, 'netmask', self.mesh_netmask, 'up'] if self.test: logging.debug("NetworkConfiguration.set_ip()command to set the IP configuration of the mesh interface: %s", command) else: subprocess.Popen(command) time.sleep(5) # Add the client interface. logging.debug("Adding client interface.") command = ['/sbin/ifconfig', self.client_interface, self.client_ip, 'up'] if self.test: logging.debug("NetworkConfiguration.set_ip()command to set the IP configuration of the client interface: %s", command) else: subprocess.Popen(command) template = ('yes', self.channel, self.essid, self.mesh_interface, self.client_interface, self.mesh_interface) _utils.set_wireless_db_entry(self.netconfdb, template) # Start the captive portal daemon. This will also initialize the IP # tables ruleset for the client interface. logging.debug("Starting captive portal daemon.") captive_portal_daemon = ['/usr/local/sbin/captive_portal.py', '-i', str(self.mesh_interface), '-a', self.client_ip, '-d' ] captive_portal_return = 0 if self.test: logging.debug("NetworkConfiguration.set_ip() command to start the captive portal daemon: %s", captive_portal_daemon) captive_portal_return = 6 else: captive_portal_return = subprocess.Popen(captive_portal_daemon) logging.debug("Sleeping for 5 seconds to see if a race condition is the reason we can't get the PID of the captive portal daemon.") time.sleep(5) # Now do some error checking. warnings = "<p>WARNING! captive_portal.py exited with code %d - %s!</p>\n" if captive_portal_return == 1: error.append(warnings % (captive_portal_return, "insufficient command line arguments passed to daemon")) elif captive_portal_return == 2: error.append(warnings % (captive_portal_return, "bad arguments passed to daemon")) elif captive_portal_return == 3: error.append(warnings % (captive_portal_return, "bad IP tables commands during firewall initialization")) elif captive_portal_return == 4: error.append(warnings % (captive_portal_return, "bad parameters passed to IP tables")) elif captive_portal_return == 5: error.append(warnings % (captive_portal_return, "daemon already running on interface")) elif captive_portal_return == 6: error.append("<p>NOTICE: captive_portal.py started in TEST mode - did not actually start up!</p>\n") else: logging.debug("Getting PID of captive portal daemon.") # If the captive portal daemon started successfully, get its PID. # Note that we have to take into account both regular and test mode. captive_portal_pidfile = 'captive_portal.' + self.mesh_interface if os.path.exists('/var/run/' + captive_portal_pidfile): captive_portal_pidfile = '/var/run/' + captive_portal_pidfile elif os.path.exists('/tmp/' + captive_portal_pidfile): captive_portal_pidfile = '/tmp/' + captive_portal_pidfile else: error.append("<p>WARNING: Unable to open captive portal PID file " + captive_portal_pidfile + "</p>\n") logging.debug("Unable to find PID file %s of captive portal daemon.", captive_portal_pidfile) # Try to open the PID file. logging.debug("Trying to open %s.", captive_portal_pidfile) portal_pid = 0 try: pidfile = open(str(captive_portal_pidfile), 'r') portal_pid = pidfile.readline() pidfile.close() except: error.append("<p>WARNING: Unable to open captive portal PID file " + captive_portal_pidfile + "</p>\n") logging.debug("value of portal_pid is %s.", portal_pid) if self.test: logging.debug("Faking PID of captive_portal.py.") portal_pid = "Insert clever PID for captive_portal.py here." if not portal_pid: portal_pid = "ERROR: captive_portal.py failed, returned code " + str(captive_portal_return) + "." logging.debug("Captive portal daemon failed to start. Exited with code %s.", str(captive_portal_return)) # Send this information to the methods that write the /etc/hosts and # dnsmasq config files. logging.debug("Generating dnsmasq configuration files.") problem = make_hosts(self.hosts_file, self.test, starting_ip=self.client_ip) if problem: error.append("<p>WARNING! /etc/hosts.mesh not generated! Something went wrong!</p>") logging.debug("Couldn't generate /etc/hosts.mesh!") configure_dnsmasq(self.dnsmasq_include_file, self.test, starting_ip=self.client_ip) # Render and display the page. try: page = self.templatelookup.get_template("/network/done.html") return page.render(title = "Network interface configured.", purpose_of_page = "Configured!", error = ''.join(error), interface = self.mesh_interface, ip_address = self.mesh_ip, netmask = self.mesh_netmask, portal_pid = portal_pid, client_ip = self.client_ip, client_netmask = self.client_netmask) except: _utils.output_error_data()