Search code examples
pythonmultiplatform

Allow everyone write access through python


So, let's start with the base problem;

Program writes a file as user of elevated rights.

Later: Same program tries to overwrite the file without elevated rights.

In Windows this clashes with:

PermissionError: [Errno 13] Permission denied: 'filepath'

  • I need this to work on Linux/Windows/Mac, though it seems like windows is the only one exhibiting this issue currently, so I tried to fix it with windows methods on windows. CHMOD doesn't work under windows anyway.
  • So, the problem is, that the file is owned by Administrator or another user with extended rights when the program is run as that user. If I can just create the file with "everybody write" permission, then this would not be a problem.
  • I found no portable way to do so in pure python. I can only remove the read-only flag but it doesn't solve the ownership issue.

So I tried win32security However, the call to LookupAccountName is locale dependent:

>>> win32security.LookupAccountName(None, "Everyone")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pywintypes.error: (1332, 'LookupAccountName', 'Zuordnungen von Kontennamen und Sicherheitskennungen wurden nicht durchgeführt.')
>>> win32security.LookupAccountName(None, "Jeder")
(<PySID object at 0x00000184AA586560>, '', 5)

And therefore not portable. I also found no way of manually creating the "Everyone" SID, the docs are rather minimal in that respect. But I might also be following the wrong rabbit hole.

Edit: If it matters, in this particular case at least, all relevant files are under "%programdata%/programname/*" in windows.


Solution

  • This problem went away with one of the windows updates within the 1903 timeframe. Some additional digging seems to suggest this was a bug in windows IF that system is part of a domain and ownership would fall into a domain user.