User avatar
JohnBeardmore
Posts: 196
Joined: Thu Nov 15, 2012 11:03 pm
Location: Derbyshire UK.
Contact: Website

Using system() from CGI application

Mon Oct 16, 2017 7:24 pm

I want to be able to set the time on a remote Pi using a web interface.

The remote Pi has a web interface with a number of functions, and I've built a form which allows the user to set year, month, day, hour and minute. This is processed by come CGI written in C++ which lives in cgi-bin. This uses the system( const char* command ) to invoke the date command as you would at the command line.

A typical command would be

/bin/date 101619032017

which works fine at the command line, but returns 256 in my C++ code and fails to update the date.

I assume this is some kind of permissions problem, so two questions:

1) Is this the beat way to set the date from a CGI program written in C++ ?

2) Is it possible to use system() from a CGI program ?

3) What changes do I need to do to enable the system() command to work ?

I also want to be able to remount a partition under the control of this CGI to be able to make it temporarily writable. Again I was thinking of using the system() approach. Will I run into the same problems, and is there a better way ?

Many thanks, J/.
Author of oBeMS open source Building energy Management System.
Automatic Meter Reading (AMR), Building Management System (BMS),
Building Energy Management System (BEMS), Infrastructure Control System (ICS).
See: http://t4sustainability.co.uk/oBeMS/

User avatar
DougieLawson
Posts: 30431
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Using system() from CGI application

Tue Oct 17, 2017 5:46 pm

You need to be root to set the clock. So you could set the setuid bits on your cgi-program (as long as you understand the security problems with doing that).

Or you have to run a server on your RPi that has the permissions and transmit a request to it to set the clock.

Or just install htpdate (with sudo apt install htpdate) and point the wee beastie at http://wikipedia.org and http://raspberrypi.org to get the time using HTTP.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

User avatar
JohnBeardmore
Posts: 196
Joined: Thu Nov 15, 2012 11:03 pm
Location: Derbyshire UK.
Contact: Website

Re: Using system() from CGI application

Sat Oct 21, 2017 8:50 pm

Thanks Dougie !

I'd thought of running a server with permissions to set the clock, but didn't like the bit of latency that would introduce. Mind you, it wouldn't have been the end of the world.

Normally the system will get the time from a time server on booting. This mechanism is only included so that the system can schedule events when the internet is down, so the htpdate approach doesn't really help.

I'll try the setuid approach for now. Maybe a sever later.

Cheers, J/.
Author of oBeMS open source Building energy Management System.
Automatic Meter Reading (AMR), Building Management System (BMS),
Building Energy Management System (BEMS), Infrastructure Control System (ICS).
See: http://t4sustainability.co.uk/oBeMS/

User avatar
JohnBeardmore
Posts: 196
Joined: Thu Nov 15, 2012 11:03 pm
Location: Derbyshire UK.
Contact: Website

Re: Using system() from CGI application

Sun Oct 29, 2017 1:28 am

Should have said before - Should anyone be curious, setuid worked.

Cheers, J/.
Author of oBeMS open source Building energy Management System.
Automatic Meter Reading (AMR), Building Management System (BMS),
Building Energy Management System (BEMS), Infrastructure Control System (ICS).
See: http://t4sustainability.co.uk/oBeMS/

Return to “Raspbian”

Who is online

Users browsing this forum: No registered users and 20 guests