cannot save file...

Hello! I have a problem:

I compile some function, which must save file:

// prog.cpp
void save_file(){
FILE *fs = fopen("file.txt", "w");
if(!fs) cerr<< "cannot save";
fprintf(fs, "This is a text file.");
fclose(fs)
}

If i compile it from command command line
...root]# g++ prog.cpp
...root]# ./a.out
It starts and saves file normally.
But if i run this function in a programm from as CGI
programm(in Apache) - it doesn't save file! Says "cannot save"...

Maybe it s a problem with permissions? I have neard, that
Apache executes everything in "cgi-bin" as a user "nobody" -
maybe this is a reason? If it is, how can I cnange the permissions
and to what? Is there a function in C++ for permissions changing?

In what directory are you trying to create the file? You can use the tmp directory (it should work with no changes to permissions).

For programs running as CGI scripts, the best bet is to create a directory, say 'data', which is only used to store dynamically-created files, and set the directory permission to 777. Make your program write into a file in that directory, then it should work.

What do you need this file for ? You can use certain facilities such as "cgiwrap" that can change to another user, but it would have to be setuid root. If they are just some temp files, use the /usr/tmp directory. You can also try running apache as another user besides 'nobody'. Create a generic login and store the apache files in that login's home directory. Then, just change the httpd.conf file and change the 'user=' line to specify the new login.

The saving file - is generated
"cpp" file, which must be compiled by this CGI-programm after it is saved.
And user can do it in any folder he wants (no possibility for using 'usr/temp/').
Maybe i have to change the directory's permissions? How to do it in C++?
Is there a function like "chmod()" in C++?

C++ is almost a superset of C. You can call chmod in C++ pretty much like you call it C. Main difference: C++ will demand a function prototype and you can get that from the include file.

You want to allow the user to save the file ANYWHERE they want ? This is not a good idea. Does you web server authenticate the user at all ? If you authenticate the user's identity, you can use a 'cgiwrap' program that can switch to their user id before saving the file. However, this would only give them the save permissions that they would have when logged into that UNIX machine.

The server is a simple XAMP Apache

  • don't worry - the cgi-programm that must allow the user to do this has a auth-system (user enters the password before he gets to the area of allowed actions). Actually I have already released this programm's version for windows - it was much easier, certainly because of NFS-problems absence.
    So I understand I must use "chmod()" - to change the directory's permissions. But how this programm will be able to do this, if it runs as a "nobody" :slight_smile: ? So I see the
    user will have to do this manually or I should find another solution...

OK. If you are authenticating them and you wish to run a command as another user besides 'nobody' then do some research on CGIWRAP. This is a "wrapper" program that is setuid root and can switch over to another user to do the type of thing you describe. In one of my past jobs, we actually used this to run UNIX commands as the actual user instead of the web server login.