Haddock99
Posts: 42
Joined: Tue Apr 26, 2016 6:54 pm

Any "fopen for write" with owner-params?

Wed Dec 07, 2016 11:28 am

I have a RPI with Raspbian and I develop a project in Ansi-C. I use fopen() to write and read files. Are there any function where I can set the ownership and/or permissions in the same time I write the file? Like a fopen with more params....

User avatar
RaTTuS
Posts: 10594
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Any "fopen for write" with owner-params?

Wed Dec 07, 2016 11:32 am

chmod($fname, 0777);
chown($fname, 'user');
I take it you are running as root and want to create it as another user ?
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

Haddock99
Posts: 42
Joined: Tue Apr 26, 2016 6:54 pm

Re: Any "fopen for write" with owner-params?

Wed Dec 07, 2016 11:57 am

The program running as root (and creates files with root:root) and I'm a USER. The PHP/Apache2 have also problems to deal with those files.
I think I use the chmod/chown() already. As I remember, the files needed to be opened before calling chmod/chown?
Of course, I can do this when the files are opened for writing..

Thanks!

mutley
Posts: 61
Joined: Sat Jan 02, 2016 8:06 pm

Re: Any "fopen for write" with owner-params?

Thu Dec 08, 2016 7:35 pm

open (not fopen) will allow you to set permissions of the file.
setuid will allow you to set the uid of the process.

So, as an example (from memory not tested)

setuid(501) // assuming www-data is 501
fd = open(filename, O_RDRW | O_CREAT, 0644)

Haddock99
Posts: 42
Joined: Tue Apr 26, 2016 6:54 pm

Re: Any "fopen for write" with owner-params?

Wed Dec 14, 2016 7:38 am

Great. That's a very nice way of setting the ownership and the p-bits as I want them.

Return to “C/C++”