I have an installation script written in Python (in Linux) that runs as root and needs to check whether certain files are readable by a non-root user.
For this reason I can't use os.path.exists() or open(filename) (and catch any exceptions).
Currently I'm thinking of checking the permission bit on each of the files, but the only problem is that I will have to check the permission bits on the path leading up to the filename as well (directories need r+x bits set), which could be very slow process if I have thousands of files.
Is my solution the best one, or are there better alternatives?
edit: I will need the script run as root after the files are checked, so dropping root permissions is not an option unfortunately.
D:\ is used to store data such as documents, images, etc. Essentially D:\ store any data of value.
Resolution. sudo (superuser do) allows you to configure non-root users to run root level commands without being root. Access can be given by the root level administrator through configuration of the /etc/sudoers file.
You can run test -r /path/to/file; echo "$?" to view the return code of the test command. Use test -w to test for write permission and test -x to test for execute permission.
You could use os.seteuid to change the effective user to some non-root user. Then try opening the file. An IOError
will be raised if permission is denied.
import os
os.seteuid(65534) # user 65534 is `nobody`
filename='/etc/passwd-'
try:
open(filename,'r')
except IOError as err:
print(err)
# [Errno 13] Permission denied: '/etc/passwd-'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With