0

So I have a Raspberry Pi that I have set up to be an Access Point with hostapd and isc-dhcp-server. It broadcasts an SSID, I connect to it with my phone or laptop, go to 192.168.42.1 and it serves up a page where I have a form for SSID, PSK, and Device ID. The idea is that it should then connect to the network with the information that I have given it by invoking a bunch of subproccess calls, as it is, it doesn't.

This is the index.html:

<html>
<body>
<form enctype="multipart/form-data" action="/cgi-bin/set_wifi.py" method="POST">

ssid: <input type="text" size="70" name="ssid" value=""><br>
pass: <input type="text" size="70" name="pass" value=""><br>
devid: <input type="text" size="70" name="devid" value=""><br>

<input type="submit" value="Submit">  
<input type="reset" value="Reset">

</form>
</body>
</html>

The content of start_wifi.py is as follows:

#!/usr/bin/python

import cgi
import cgitb
import subprocess

cgitb.enable()

print "Content-type: text/html\n\n"

form=cgi.FieldStorage()

ssid_val=form['ssid'].value
pass_val=form['pass'].value
devid=form['devid'].value

#Write Device ID to devid.text
myfile= open("/home/pi/project/devid.txt","w")
myfile.write(form['devid'].value)
myfile.close()

#Write SSID and PSK to wpa_supplicant.conf
file = open('/etc/wpa_supplicant/wpa_supplicant.conf','a')
file.write('\n\nnetwork={\n ssid="')
file.write(ssid_val)
file.write('"\n     psk="')
file.write(pass_val)
file.write('"\n}')

file.close()

subprocess.call("sudo service hostapd stop", shell=True)
subprocess.call("sudo service isc-dhcp-server stop",shell=True)
subprocess.call("sudo ifdown wlan0",shell=True)
subprocess.call("sudo /etc/init.d/networking stop",shell=True)
subprocess.call("cp /etc/network/interfaces-manual /etc/network/interfaces",shell=True)
subprocess.call("sudo /etc/init.d/networking start",shell=True)
subprocess.call("sudo ifup wlan0",shell=True)

I have another python script called stopapd.py which is essentially everything after file.close() in the set_wifi.py script above. When I run it through the terminal it works fine. Stops networking, rewrites interfaces, restarts networking and re-establishes wlan0 if the network is already saved in wpa_supplicant.conf

The problem is that when set_wifi.py executes via cgi, the first half of the script works, it writes to devid.txt and wpa_supplicant.conf, but the only subproccess.call that I think is working correctly is the

cp /etc/network/interfaces-manual /etc/network/interfaces

which changes the interfaces file from static to dhcp...

None of the other commands seem to run...but when I run the same code as stopapd.py from the terminal it works...I don't know why...any help would be greatly appreciated.

Chris Lavan
  • 23
  • 1
  • 4

2 Answers2

0

This was driving me nuts but I kept working on it and here's what I came up with...

In set_wifi.py, I commented out everything after file.close() and added

file=open('/home/pi/project/set_wifi_ran.txt','w')
file.write('set_wifi_ran') 
file.close()

Then I have another script running all the time called stopapd.py...it monitors set_wifi_ran.txt, and if it ever changes it executes the subprocess.calls...which solves all my problems

while True:
    file=open('/home/pi/project/set_wifi_ran.txt')
    length=(file.readline())
    file.close()

    if length>0
        file=open('/home/pi/project/set_wifi_ran.txt','w')
        file.close()

        subprocess.call("sudo service hostapd stop", shell=True)
        subprocess.call("sudo service isc-dhcp-server stop",shell=True)
        subprocess.call("sudo ifdown wlan0",shell=True)
        subprocess.call("sudo /etc/init.d/networking stop",shell=True)
        subprocess.call("cp /etc/network/interfaces-manual /etc/network/interfaces",shell=True)
        subprocess.call("sudo /etc/init.d/networking start",shell=True)
        subprocess.call("sudo ifup wlan0",shell=True)

I'm sure this is not a very elegant way to do this...I'd appreciate any feedback and I'd still love to know why this doesn't work through the cgi script

Chris Lavan
  • 23
  • 1
  • 4
0

sudo will only work for users specified in the sudoers file (/etc/sudoers). Is is very likely that your web server is running with another user without the ability to sudo.

That would explain why only the non-sudo commands are executing.

Either use your existing watchdog solution or add your webserver user in your sudoers file. Or use a custom web server running as your user.

chaos.ct
  • 1,001
  • 1
  • 7
  • 18