1

Edit: OK, I've made it much simpler.

I put the file first.pl into /usr/lib/cgi-bin (typed, not copied, hope it's error free)

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello, World.";
my $file = "/tmp/first";
open(FILE, '>'.$file);
print FILE "Hello me\n";
close FILE;

I run the file, it prints and creates the file /tmp/first. Now I go to my browser, to localhost/cgi-bin/first.pl. I see "Hello, World." I see no file /tmp/first created. I also tried a find / -name first 2>/dev/null to see if that would find the file. No luck.

Please, what's going on?

Another edit: I added code to print the environment to first.pl I don't see anything interesting

Hello, World.
DOCUMENT_ROOT = /var/www/html
REQUEST_URI = /cgi-bin/first.pl
QUERY_STRING =
HTTP_DNT = 1
HTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
REMOTE_PORT = 59742
HTTP_USER_AGENT = Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36
SERVER_ADMIN = webmaster@localhost
HTTP_ACCEPT_ENCODING = gzip, deflate
SCRIPT_FILENAME = /usr/lib/cgi-bin/first.pl
SCRIPT_NAME = /cgi-bin/first.pl
REQUEST_SCHEME = http
SERVER_PORT = 80
SERVER_NAME = se0
SERVER_SOFTWARE = Apache/2.4.38 (Raspbian)
HTTP_CACHE_CONTROL = max-age=0
HTTP_CONNECTION = keep-alive
PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
CONTEXT_PREFIX = /cgi-bin/
REMOTE_ADDR = 192.168.0.116
GATEWAY_INTERFACE = CGI/1.1
HTTP_HOST = se0
HTTP_UPGRADE_INSECURE_REQUESTS = 1
HTTP_ACCEPT_LANGUAGE = en-US,en;q=0.9
CONTEXT_DOCUMENT_ROOT = /usr/lib/cgi-bin/
SERVER_ADDR = 192.168.0.39
REQUEST_METHOD = GET
SERVER_SIGNATURE =
Apache/2.4.38 (Raspbian) Server at se0 Port 80

SERVER_PROTOCOL = HTTP/1.1

(original follows)

I have a cgi program that is supposed to be creating a file in /tmp, but it never shows up. This same program has worked properly on other Linux systems. The code in question is

if (strlen(ffileName)) {
    if (getTempFileName(tfileName) != cgiParseSuccess) {
    return cgiParseIO;
    }   
    outf = fopen(tfileName, "w+b");

I can use gdb to step through this code, like so:

Breakpoint 1, cgiParsePostMultipartInput () at cgic.c:535
535             outf = fopen(tfileName, "w+b");
(gdb) p tfileName
$3 = "/tmp/cgicyMuPrt\000\350\000\000\000\350\000\000\000\004\000\000\000\001\000\000\000\004\000\000\000\024\000\000\000\003\000\000\000GNU\000\251\f\316\022\362\317x\362w\276\246\211\345\265\031-\036\261\204\354\003\000\000\000\a\000\000\000\002\000\000\000\006\000\000\000\005@Ex\021\000\021\000\a\000\000\000\n\000\000\000\f\000\000\000\060\270\202\r\200\020ؽ\235\273\223\034\032\237ֽ\237\354В\220u\202\r%u\202\r", '\000' <repeats 20 times>, "t\003\000\000\000\000\000\000\003\000\t\000\000\000\000\000$@\001\000\000\000\000\000\003\000\023\000F", '\000' <repeats 11 times>, "\"\000\000\000\020", '\000' <repeats 11 times>...
(gdb) n
540         result = afterNextBoundary(mpp, outf, &out, &bodyLength, 0);
(gdb) p outf
$4 = (FILE *) 0x1968158

At this point, I do a ls of /tmp, and I do not see the file which should have been created. Yet it looks like there was no error. Where did my file go?

pi@raspberrypi:/etc $ ls -l / | grep tmp
drwxrwxrwt  18 root root  4096 Feb 10 20:50 tmp
pi@raspberrypi:/etc $ ls /tmp
dhcpcd-pi
ssh-ANJEEYvUhJSE
ssh-r2ofFbnIBLFA
systemd-private-0c86de6f48a34f97b2f57adac8054087-apache2.service-COknFK
systemd-private-0c86de6f48a34f97b2f57adac8054087-colord.service-yytb8l
systemd-private-0c86de6f48a34f97b2f57adac8054087-systemd-timesyncd.service-DM6doz

This is raspbian version 10 "Buster". I'm using apache2 to run my c .cgi program, which is doing other things properly. Some googling mentions SELinux. That does not seem to be installed. The code comes from https://github.com/boutell/cgic, version 2.05

rich
  • 425
  • 3
  • 17

1 Answers1

1

The answer to the question is here:

https://serverfault.com/questions/912094/apache2-saves-files-on-tmp-in-a-system-private-hash-instead-of-just-saving

Well, I found where the file went. For the perl program, it ended up in (oooh, I don't want to type the whole thing, I will abbreviate)(the raspbian browser doesn't seem to work at this site)

/tmp/systemd-private-0c86...4087-apache2.service-first/tmp/first

and my .cgi files are there too. That systemd-... directory is owned by root, with no non-root privileges. So I guess this has turned into a systemd question, and I will start googling that. Or, I will just create a directory named /temp and put the files there. That seems to work.

rich
  • 425
  • 3
  • 17
  • I was just pulling my hair out over this exact same behaviour. Thank you for following up on your issue – valentine Jan 22 '23 at 15:56