Help getting started with PHP^H^H^HPython

Status
Not open for further replies.

Devilbunny

Ars Scholae Palatinae
1,156
I am just starting to try to do a project. I took a year of CS (taught on C++) in college back in the 90s, did pretty well at it but never kept up after that, so I'm very rusty.

I'm not asking for anyone to write this for me...

...but I'd really appreciate a little guidance about what keywords I need to be searching for in order to learn how to do this.

Situation: I have a Linux server (ultimately this will be my Synology, but in the meantime it's a VM so can be anything you recommend - currently Ubuntu 12.04 Desktop) with Apache, MySQL, and PHP installed (as well as phpMyAdmin). I am installing an Envisalink 3 interface to my home security system, which will essentially set up a telnet server on a high port that will pretend to be an RS232 port to the alarm system. Commands are to be issued as fairly short (mostly 10 bytes or less) strings, and I need to be able to process the output.

So: I need to have a program that attaches to the Envisalink to do two-way communication with it via telnet protocol. I want the commands to come from a PHP web page. I want the output to go to a log file that a PHP web page can read. I want to process any incoming data immediately and potentially generate other actions (e.g., notifications to me or my wife) if appropriate. What do I need to read up on in order to do this?
 

GohanIYIan

Ars Legatus Legionis
10,196
I'd say you need to start by writing a daemon to monitor the interface. You could do it in PHP if you're a real glutton for punishment but it wouldn't be my first choice. The important thing is that it isn't a web page. When the page finishes loading, PHP finishes running. That makes it bad for processing incoming data in real time. You can use javascript to constantly refresh the page or poll via AJAX requests but it's not a particularly robust solution. If the browser crashes or someone closes it accidentally your program stops.

You can write the daemon in such a way that you can interact with it via a web page. I think you're on the right track with a log file. That'll work for getting output from the daemon to PHP. To send commands, I would have the daemon listen via a named pipe or UDP port (UDP would be better if you want cross-platform support). For the PHP side of things you'd be looking into file I/O for the logs and socket functions for the commands.
 

GohanIYIan

Ars Legatus Legionis
10,196
SituationSoap":ld97r6a3 said:
I'm going to be snarky, but:

http://www.learnpython.org/

Python would be my first choice for actually interacting with the device regardless of what the web layer is written in. Threading is easy to use which will come in handy while simultaneously monitoring the device and listening for external commands. Some of the built-in modules make it really simple to put together a basic socket server, too.

I still think OP needs to start by moving most of the functionality out of the web and into a continuous background process.
 

Devilbunny

Ars Scholae Palatinae
1,156
Thanks for all the advice. I'm certainly looking to do this the easiest way - I am not a coder, and I do have a day job, so while it's an interesting hobby I don't want to spend all my time on it.

I was wondering how I'd get around the runs-when-called nature of the web page/PHP method. Looks like the code to daemonize Python is pretty easy (aka, it was probably incredibly difficult to write in the very clear and concise fashion shown here but is, by virtue of that effort, very easy for me to understand). So I should be able to :

1. Set up alarm daemon via sockets to talk to the monitoring device via TCP (this is the only supported interface I know of). Call it alarmd.

2. alarmd writes all output to a log (which needs to be included in log rotation of some kind - logrotate or cron, this I can handle).

3. alarmd processes inputs from a named pipe (this does appear to be a dead simple way to do this). This pipe is fed by the web page.

4. Web page presents the output of tail on the log and has a box in which raw commands can be typed (v1.0) progressing to a prettified version of the output of tail on the log and something that has basic arm/disarm commands available (v2.0) and thence to glorious CSS and proper HTML (yeah, right) and integrating it with Raspberry Pi to add garage door up/down status and control, possibly including thermostat duties(v3.0).

Does this sound like I have a handle on the basic problem? I really appreciate the feedback. ("So, what made you pick up programming at 38?" "Well, I really felt the need to control my garage door from across the world.")
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
Call it alarmd.
Please don't - alarmd is an established daemon, and your future self will appreciate not trying to figure out why stopping the unused KDE Alarm daemon is causing some other process to hork up and die. Right now, it's obvious to you, but it won't be when in a few short months.


#4 is demonstrably wrong. You need to whitelist commands and validate arguments. Otherwise someone will run a command that causes a shell to be spawned with a login process on it, or reboot the server, or write all 0's everywhere. Or if it can't escape it's python shell, it could still cause harm to your security system. Like, calling the cops when there's no fault. You really don't want the cops showing up because you wanted to make your security system available through a web page. Or open your garage door when you're not home or set the thermostat to 80F in the summer.

In fact, I'm not sure why you'd go to this effort when it appears that Envisalink includes these features already and has an API to allow third parties to manipulate it already. Presumably via some means other than raw interaction with telnet.
 

GohanIYIan

Ars Legatus Legionis
10,196
There is a fair amount of tinkering that falls under the umbrella of "because you can".

Command validation could be within the daemon process. If it's accepting messages via named pipes or UDP, there aren't any shell escaping issues. Messages might be more appropriate than commands; you wouldn't actually execute the input. It should be reasonably safe if OP isn't passing bytes directly from the web form to the device. I'd have the daemon look for human-readable messages (e.g., OpenGarageDoor) and then forward the appropriate command bytes to the device. Any unrecognized messages would simply be ignored.

Upon re-reading, "raw commands" does give me pause. That does sort of imply passing bytes directly from the web form to the device and I'd agree that's a bad idea.
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
Command validation could be within the daemon process. If it's accepting messages via named pipes or UDP, there aren't any shell escaping issues.
I am not saying that one cannot secure their own program, just that the amount of effort required to actually do so is usually significantly more than expected. Especially for a novice. Simply running the daemon as the wrong user - of which a novice will see plenty of warnings about, but no clear-cut solution to - could be a grievous error, and named pipes can be exploited as well without corresponding network controls (and usually combined with the aforementioned setuid issue). Assuming this is just one person working on something in their part time, they're likely to be outclassed. I don't want Devilbunny to come home to find his garage door open, all his valuables missing, and no cops alerted, or the cops alerted every sunday morning at 3am because some neighbor kid is a prick, that's all.
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
Your approach is fine, though I'd have the daemon talking to the alarm box save the last 10 (or whatever) log messages in a ring buffer, and provide a command over the named pipe to retrieve those messages for display on the web page. It's better than hassling with a log file.

Devilbunny":2k7g5894 said:
Looks like the code to daemonize Python is pretty easy (aka, it was probably incredibly difficult to write in the very clear and concise fashion shown here
While that code will work, it does a bunch of unnecessary, ugly stuff. Double-fork is not necessary on modern UNIX. Blindly resetting the umask is bad. The way it handles resetting stdin, stdout, stderr is too complicated.
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
ronelson":v6hfg586 said:
Call it alarmd.
Please don't - alarmd is an established daemon, and your future self will appreciate not trying to figure out why stopping the unused KDE Alarm daemon is causing some other process to hork up and die. Right now, it's obvious to you, but it won't be when in a few short months.
KDE daemons aren't system daemons, so this isn't a particularly relevant concern. I also don't think you'll run KDE on a Synology.

#4 is demonstrably wrong. You need to whitelist commands and validate arguments. Otherwise someone will run a command that causes a shell to be spawned with a login process on it, or reboot the server, or write all 0's everywhere.
Err, no. No validating commands sent to the daemon, thus sent to the alarm system, is vastly different from blindly accepting input and calling system(3) on it. The code would have to do the latter for what you describe to be possible; unless you're concerned about bugs in Python itself or whatever web library he uses. Of all the things to worry about, they wouldn't be all that high on my list.

Like, calling the cops when there's no fault. You really don't want the cops showing up because you wanted to make your security system available through a web page. Or open your garage door when you're not home or set the thermostat to 80F in the summer.
This is entirely an access and authorization problem, not a "web page lets you send commands directly to the alarm system" problem.

So yes, he needs to protect the page appropriately from outside access, but he'd need to do that even if it validated all commands sent to the alarm system to ensure they weren't malformed. Protection from malformed commands is a good idea, probably even a necessary one. But I'm not sure it's the first thing I'd worry about in a v1.0 implementation.

Leave the page behind a firewall always, and SSH into your home network and tunnel if you actually want remote access to it. That will be safer than any homegrown authentication and authorization stuff.

and named pipes can be exploited as well without corresponding network controls (and usually combined with the aforementioned setuid issue).
I'm not sure what "exploiting a named pipe" gets you without the ability to run arbitrary code on the server. If I can do that, I can think of a million better things to do then fuck with his homegrown alarm remote control.
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
I've been operated under the assumption that we're talking about intranet accessible only. Exposing this to the internet isn't a good idea, IMO.
Maybe, but I assume he has wifi, and thus it's exposed outside of the physical domain of his house. But, he's also got apache on there so it may be exposed to the internet to some extent if he runs a public web page on it.
But I'm not sure it's the first thing I'd worry about in a v1.0 implementation.
Yes, and you're not someone who took a single year of CS 20+ years ago, either.
 

Devilbunny

Ars Scholae Palatinae
1,156
The WiFi is secured. The application is not intended, ever, to be exposed to the Internet at large, much like nearly everything else on my network. I am inexperienced and will doubtless make somewhere north of ten million mistakes while writing this, but exposing an alarm-controlling application on my personal file server is not going to be one of them. Your concern is appreciated, but I'm not 13 - I've been using the Internet a long time, so unlike most programming newbs I'm aware of the folly of trying to roll my own security when the last project I did involved doubly linked lists.

The guys who make this already have an application that works for monitoring over the Internet (and it's free), but it runs on their servers and frankly I just find that a little too creepy - if I were paying someone for a service then there might actually be some protection for me and my info, but I really don't want someone else possessing data like when my doors open and close, etc., when they can plausibly claim that providing all that data is the only reason they let me use the software.

Again, thanks to all.
 
Status
Not open for further replies.