Recent

Author Topic: Apache2 Server and CGI  (Read 3932 times)

salvadorcg

  • Newbie
  • Posts: 4
Apache2 Server and CGI
« on: December 21, 2019, 12:39:34 pm »
Hello.

OS: linux (XUbuntu)

I'm running an Apacge2 server. Some Web Pages calls a CGI, done with Lazarus.

When the CGI runs it is possible to read a TextFile, but I cannot find the way to write any file or execute another program.

I've tested for files:
1) AssignFile; rewrite; writeln; CloseFile
2) TFileStream; TMemoryStream;

And to call another program to run: tprocess an the rest of possibilities shown in 'Executing External Programs' from free pascal web page.

But nothing works...

When I run the program directly (not as CGI called by Apache2) everything works fine...

Maybe it's because of pipes beavior (Does Apache2 call with pipes?).

Please, could anyone help me ??   :)

Thanks







 
« Last Edit: December 21, 2019, 12:50:23 pm by salvadorcg »

MarkMLl

  • Hero Member
  • *****
  • Posts: 2387
Re: Apache2 Server and CGI
« Reply #1 on: December 21, 2019, 01:01:59 pm »
Check what user and group is responsible for running Apache and the CGI (which will not necessarily be the same), and then find somewhere that they are allowed to write.

On my system (which is Debian rather than an Ubuntu derivative, and which is running old-school Perl CGIs) Apache is being run by www-data, www-data has /var/www as its home directory but only has read access (and the script is not necessarily CDed to that dirctory). You also need to consider where your cgi-bin directory really is and what the permissions are.

I suspect that allowing a CGI filesystem write access is frowned upon by unix and Apache doctrine, restricted database access is more common with extreme emphasis on "restricted": https://www.xkcd.com/327/ applies :-)

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

salvadorcg

  • Newbie
  • Posts: 4
Re: Apache2 Server and CGI
« Reply #2 on: December 21, 2019, 07:14:25 pm »
Thanks a lot.

As you said, maybe is because a permission question. I'm not a XUbuntu master, so I moved to Windows and it works perfectly !!!!



Problem Fixed  ;)

MarkMLl

  • Hero Member
  • *****
  • Posts: 2387
Re: Apache2 Server and CGI
« Reply #3 on: December 21, 2019, 07:47:54 pm »
I think that the way I'd have tackled it would have been by setting up a specific writeable directory, leaving me with something like /var/www/html, /var/www/html/cgi-bin (a symlink) and /var/www/html/localstate (owned by www-data). I'd also have tried to restrict the scripts that could access that to local users, but I've already got problems in some other stuff I'm doing caused by CGI environment variables not behaving quite as I'd like (and with the precise mix of variables being server-specific) so that might be unattainable.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1025
Re: Apache2 Server and CGI
« Reply #4 on: December 21, 2019, 08:00:05 pm »
Hello.
OS: linux (XUbuntu)
I'm running an Apacge2 server. Some Web Pages calls a CGI, done with Lazarus.
When the CGI runs it is possible to read a TextFile, but I cannot find the way to write any file or execute another program.
I've tested for files:
1) AssignFile; rewrite; writeln; CloseFile
2) TFileStream; TMemoryStream;
And to call another program to run: tprocess an the rest of possibilities shown in 'Executing External Programs' from free pascal web page.
But nothing works...
When I run the program directly (not as CGI called by Apache2) everything works fine...
Maybe it's because of pipes beavior (Does Apache2 call with pipes?).
Please, could anyone help me ??   :)
Thanks
Check what user and group is responsible for running Apache and the CGI (which will not necessarily be the same), and then find somewhere that they are allowed to write.
On my system (which is Debian rather than an Ubuntu derivative, and which is running old-school Perl CGIs) Apache is being run by www-data, www-data has /var/www as its home directory but only has read access (and the script is not necessarily CDed to that dirctory). You also need to consider where your cgi-bin directory really is and what the permissions are.
I suspect that allowing a CGI filesystem write access is frowned upon by unix and Apache doctrine, restricted database access is more common with extreme emphasis on "restricted": https://www.xkcd.com/327/ applies :-)
As Mark correctly said, you are facing a file permission problem:
--------------------------------------------------
https://httpd.apache.org/docs/2.4/howto/cgi.html
File permissions
Remember that the server does not run as you. That is, when the server starts up, it is running with the permissions of an unprivileged user - usually nobody, or www - and so it will need extra permissions to execute files that are owned by you. Usually, the way to give a file sufficient permissions to be executed by nobody is to give everyone execute permission on the file:
chmod a+x first.pl
Also, if your program reads from, or writes to, any other files, those files will need to have the correct permissions to permit this.
--------------------------------------------------

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1025
Re: Apache2 Server and CGI
« Reply #5 on: December 21, 2019, 08:09:03 pm »
Thanks a lot.
As you said, maybe is because a permission question. I'm not a XUbuntu master, so I moved to Windows and it works perfectly !!!!
Problem Fixed  ;)
Moving out from Linux is not a solution, learning how it works would be.
Once you overcome the first barriers and learn how to work on Linux, you will never leave it again.

GAN

  • Sr. Member
  • ****
  • Posts: 320
Re: Apache2 Server and CGI
« Reply #6 on: December 21, 2019, 08:52:08 pm »
Hi, this is the spanish forum...
Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite 3.32.3

https://searchlazarus.blogspot.com/

korba812

  • Full Member
  • ***
  • Posts: 159
Re: Apache2 Server and CGI
« Reply #7 on: December 21, 2019, 09:01:41 pm »
Hi, this is the spanish forum...
Code: Python  [Select][+][-]
  1. I didn't expect a kind of Spanish Inquisition

p.s. sorry about that

korba812

  • Full Member
  • ***
  • Posts: 159
Re: Apache2 Server and CGI
« Reply #8 on: December 21, 2019, 09:13:29 pm »
Returning to the question, maybe you should use -dUseCThread option? If I remember correctly TProcess uses threads.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1025
Re: Apache2 Server and CGI
« Reply #9 on: December 21, 2019, 10:10:17 pm »
Hi, this is the spanish forum...
Lo siento, ya que la pregunta estaba en inglés, la respondí sin tener en cuenta eso.

salvadorcg

  • Newbie
  • Posts: 4
Re: Apache2 Server and CGI
« Reply #10 on: December 22, 2019, 08:32:50 am »
I tried to give permission to the Apache2 CGI directory and..... IT WORKS !!!!

With sudo chmod a+rwx /dir it was enought to make CGI write a TextFile.
So, I keep XUbuntu as my Server-OS...  :D
Thanks a lot....


Disculpad los del foro en castellano, pero me equivoqué al poner el primer post... ains que verdecito estoy  %)
« Last Edit: December 22, 2019, 08:37:33 am by salvadorcg »

 

TinyPortal © 2005-2018