This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: mailbox cannot modify mailboxes in system mail spool
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.2, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: akuchling, doko, mahmoud, olau, petri.lehtinen, pitrou, sdaoden
Priority: normal Keywords:

Created on 2009-11-19 16:15 by doko, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg95486 - (view) Author: Matthias Klose (doko) * (Python committer) Date: 2009-11-19 16:15
no change in behaviour in 2.6

the mailbox module in python 2.5 cannot modify mboxes in read-only
directories, e.g. the system mail spool.  This is because
mailbox._singlefileMailbox.flush() tries to write the modified mailbox
to a temporary file and then rename it.  See: 

penelope[tmp]$ python2.5
Python 2.5 (release25-maint, Dec  9 2006, 14:35:53) 
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-20)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mailbox
>>> mbox = mailbox.mbox("/var/mail/nikolaus")
>>> mbox.clear()
>>> mbox.close()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/mailbox.py", line 623, in close
    self.flush()
  File "/usr/lib/python2.5/mailbox.py", line 570, in flush
    new_file = _create_temporary(self._path)
  File "/usr/lib/python2.5/mailbox.py", line 1885, in _create_temporary
    os.getpid()))
  File "/usr/lib/python2.5/mailbox.py", line 1875, in _create_carefully
    fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR)
OSError: [Errno 13] Permission denied:
'/tmp/mail/nikolaus.1195061622.penelope.4241'
>>>
msg95522 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-11-20 01:34
The same remark as in issue7360, that is it is not robust at all to
overwrite the file rather than doing an atomic rename from a temporary
file. The only possible exception would be if you only /append/ to the
mbox file (and that's assuming all mailbox-reading software you use be
able to deal with a partially written message at the end of an mbox file).
msg122994 - (view) Author: Ole Laursen (olau) Date: 2010-12-01 13:40
Just got bitten by this too.

Renaming is good and all, but as far as I can tell, it will never work with the system spool. It's not just that you can't create a temporary file in the directory, you can't rename files into it either. If I create an empty file somewhere and try to rename it to overwrite my mailbox, I get a permission denied. Sad. :(

So I think you have to bite the bullet and write directly to the file. Either that or define that the module can't be used to work with system spool mailboxes, at least on Debian. But that would be even more sad.
msg135240 - (view) Author: Steffen Daode Nurpmeso (sdaoden) Date: 2011-05-05 20:37
Issue #11935 becomes gigantic and may even swamp this one over!
msg163112 - (view) Author: Petri Lehtinen (petri.lehtinen) * (Python committer) Date: 2012-06-18 18:35
Every program that accesses mailboxes in the system-wide mail spool directory needs to have write access to it. This is because dot-locking is achieved by creating additional files to that directory, and it must be used (in addition to fcntl() locking) to avoid messing up the mailboxes because of concurrent access.

In Debian, /var/mail is owned by root:mail with mode 2755, and every MUA is setgid mail. See the Debian Policy Manual section 11.6 for more information:

    http://www.debian.org/doc/debian-policy/ch-customized-programs.html#s-mail-transport-agents

If you write a MUA or MTA using Python's mailbox module, your program needs to have write access to /var/mail. That's the only way to do it correctly. It also makes the mailbox module's renaming behavior possible.
msg259828 - (view) Author: Mahmoud Hashemi (mahmoud) * Date: 2016-02-08 07:55
Got bit by this, and since it's not a bug, here's "not" a fix: http://boltons.readthedocs.org/en/latest/mboxutils.html#boltons.mboxutils.mbox_readonlydir

Been in production for a while, working like a charm. Might there be interest in including this in the standard lib?
History
Date User Action Args
2022-04-11 14:56:54adminsetgithub: 51608
2016-02-08 07:55:47mahmoudsetnosy: + mahmoud
messages: + msg259828
2012-06-18 18:35:41petri.lehtinensetstatus: open -> closed

nosy: + petri.lehtinen
messages: + msg163112

resolution: not a bug
stage: resolved
2011-05-05 20:37:07sdaodensetnosy: + sdaoden
messages: + msg135240
2010-12-01 13:40:40olausetnosy: + olau
messages: + msg122994
2009-11-20 01:34:37pitrousetpriority: normal
versions: + Python 2.7, Python 3.2
nosy: + akuchling, pitrou

messages: + msg95522
2009-11-19 16:15:24dokocreate