简体   繁体   English

procmail 和 python 脚本存在权限问题

[英]Having permissions issues with procmail and python script

So here is my procmalirc.所以这是我的procalirc。 The script seems to run as the "user" of the mailbox so the script CANT create locks or flush or remove messages because the procmail seems to have a lock on the mailbox.该脚本似乎以邮箱的“用户”身份运行,因此该脚本无法创建锁或刷新或删除消息,因为 procmail 似乎在邮箱上有锁。

SHELL = /bin/sh
LOGFILE = $HOME/pm.log
LOGABSTRACT = "All"
VERBOSE = "on"


:0
* ^From: .*address.*
* ^Subject:.*su to root.*
{
:0c:
/var/spool/mail/tdproxymail

:0ahi
| /usr/local/tdproxy/MAILSCRIPTS/script.py
}

I'm delivering the mail to the inbox and sending it to the script.我将邮件发送到收件箱并将其发送到脚本。 When I rrun the python script I loop through the mailbox...looking for the correct email...当我运行 python 脚本时,我会遍历邮箱...寻找正确的 email...

mbox = mailbox.mbox('/var/mail/tdproxymail')

for key, msg in mbox.iteritems():
    print(key)
    if "su to root" not in (msg['subject']):
        continue

Everything processes fine but when I get to一切都很好,但是当我到达时

mbox.remove(key)
mbox.flush()
mbox.close()

it is saying I don't have permissions on the lock from the procmail I think...它是说我没有来自procmail的锁的权限,我认为......

Subject: su to root Folder: /usr/local/tdproxy/MAILSCRIPTS/edwards_sudo.py 812 Traceback (most recent call last): File "/usr/local/tdproxy/MAILSCRIPTS/script.py", line 94, in mbox.lock() File "/usr/lib64/python2.7/mailbox.py", line 625, in lock _lock_file(self._file) File "/usr/lib64/python2.7/mailbox.py", line 1976, in _lock_file pre_lock = _create_temporary(f.name + '.lock') File "/usr/lib64/python2.7/mailbox.py", line 2025, in _create_temporary os.getpid())) File "/usr/lib64/python2.7/mailbox.py", line 2015, in _create_carefully fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0666) OSError: [Errno 13] Permission denied: '/var/mail/tdproxymail.lock.1571858501.tdproxy.91248'主题:su 到根文件夹:/usr/local/tdproxy/MAILSCRIPTS/edwards_sudo.py 812 Traceback(最近一次调用最后):文件“/usr/local/tdproxy/MAILSCRIPTS/script.py”,第 94 行,在 mbox 中。 lock() 文件“/usr/lib64/python2.7/mailbox.py”,第 625 行,在 lock _lock_file(self._file) 文件“/usr/lib64/python2.7/mailbox.py”,第 1976 行,在_lock_file pre_lock = _create_temporary(f.name + '.lock') 文件“/usr/lib64/python2.7/mailbox.py”,第 2025 行,在 _create_temporary os.getpid())) 文件“/usr/lib64/python2 .7/mailbox.py",第 2015 行,在 _create_carefully fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0666) OSError: [Errno 13] Permission denied: '/var/mail/ tdproxymail.lock.1571858501.tdproxy.91248'

I wanted to try to process the email just sys.stdin but I tried both:我想尝试处理 email 只是 sys.stdin 但我都尝试了:

#msg = email.message_from_file(sys.stdin)
#msg = email.parser.Parser().parse(sys.stdin)

and it says that is_multipart is false which I know is not the correct case...so in short if I access the mailbox it says there is an attachment but if I use the stdin of the pipe there supposedly is no attachment它说 is_multipart 是假的,我知道这不是正确的情况......所以简而言之,如果我访问邮箱,它说有一个附件,但如果我使用 pipe 的标准输入,则应该没有附件

QUESTION问题

How can I process and then delete the email from the mailbox since there seems to be a permissions issue in running the script as the user of the incoming mail.我如何处理然后从邮箱中删除 email,因为在以传入邮件的用户身份运行脚本时似乎存在权限问题。

You have an incorrect filename.您的文件名不正确。 /var/mail/tdproxymail.1571851019.tdproxy.81261 is not a file which exists or which you should have any permission to access. /var/mail/tdproxymail.1571851019.tdproxy.81261不是一个存在或您应该有权访问的文件。 Your mbox filename is /var/mail/tdproxymail and individual messages are slices within that file.您的 mbox 文件名是/var/mail/tdproxymail并且单个消息是该文件中的切片。

(That's how mbox works; other folders have different structures, and in fact 1571851019.tdproxy.81261 looks vaguely like what an individual message file in a maildir folder directory might look like.) (这就是 mbox 的工作原理;其他文件夹具有不同的结构,实际上1571851019.tdproxy.81261看起来有点像 maildir 文件夹目录中单个消息文件的样子。)

Looping over the entire mailbox looking for the latest message is completely crazy anyway.遍历整个邮箱以查找最新消息无论如何都是完全疯狂的。 Accepting the message on standard input is by far the more sensible, robust, and efficient approach, so I would instead explore what you can do to fix that (probably something simple; but also probably best posted as a separate question).接受标准输入上的消息是迄今为止更明智、更健壮和更有效的方法,所以我会改为探索你可以做些什么来解决这个问题(可能是一些简单的事情;但也可能最好作为一个单独的问题发布)。 If you can't solve that, a better quick and dirty - but still pretty desperate - workaround might be to write the message to an individual temporary file, and pass that to Python.如果你不能解决这个问题,一个更好的快速和肮脏的 - 但仍然非常绝望 - 解决方法可能是将消息写入一个单独的临时文件,并将其传递给 Python。

Just to be explicit, the hypothesis that this is related to mailbox locking seems false.明确地说,这与邮箱锁定有关的假设似乎是错误的。 Locking an mbox happens by other means than a lockfile on most architectures (typically flock or fcntl ; but check the output of procmail -v to see what's the compiled-in behavior on your system) - precisely because normal users don't have permission to create new files in the directory where their inbox lives.在大多数体系结构上,锁定 mbox 的方式不是锁定文件(通常是flockfcntl ;但请检查procmail -v的 output 以查看系统上的编译行为) - 正是因为普通用户没有权限在收件箱所在的目录中创建新文件。

Probably the Python mailbox code tries to copy the mbox file to a different location because manipulating it in memory is usually not a good idea (though in this particular case it could perhaps work; but again, really, don't do that).可能Python mailbox代码试图将mbox文件复制到不同的位置,因为在 memory 中操作它通常不是一个好主意(尽管在这种特殊情况下它可能会起作用;但同样,真的,不要那样做)。 I have not tried that code;我没有尝试过该代码; but a typical arrangement is for library functions to examine whether os.environ['TMPDIR'] is set and, if so, use that for temporary files.但一个典型的安排是库函数检查是否设置了os.environ['TMPDIR'] ,如果设置了,则将其用于临时文件。

But in fact, I guess the root cause of your problem is that you added the h flag to the recipe which writes to Python.但事实上,我猜你问题的根本原因是你将h标志添加到写入 Python 的配方中。 Of course you don't get a multipart message then;当然,那时您不会收到多部分消息; you get no body at all, because you told Procmail to only give the headers to your script.您根本没有正文,因为您告诉 Procmail 只将标题提供给您的脚本。

As an aside, the spaces around the equals signs are syntax errors.顺便说一句,等号周围的空格是语法错误。 Procmail, like the shell, requires assignments to be of the form variable=string or variable="quoted string" with no whitespace on either side of the = character. Procmail 和 shell 一样,要求赋值的形式为variable=stringvariable="quoted string" ,在=字符的两边都没有空格。

# Fix incorrect assignment syntax, no spaces around =
SHELL=/bin/sh
LOGFILE=$HOME/pm.log
LOGABSTRACT="All"
VERBOSE="on"

# Drop the h flag and then also the copying and the a flag
# Also, trailing wildcard is unnecessary in regexes; Procmail matches on any substring
:0i
* ^From: .*address
* ^Subject:.*su to root
| /usr/local/tdproxy/MAILSCRIPTS/script.py

If you desperately want to save the message to a file first, write it to a directory like /tmp/ and capture the file name from Procmail's LASTFOLDER variable.如果您非常想先将消息保存到文件中,请将其写入/tmp/之类的目录,然后从 Procmail 的LASTFOLDER变量中捕获文件名。 But with the fix to remove the h flag, I trust that will be unnecessary.但是通过修复删除h标志,我相信这将是不必要的。

(You will still of course need to remove the code to attempt to manipulate the mbox file from Python and just accept the message on standard input.) (当然,您仍然需要删除代码以尝试从 Python 操作mbox文件,并且只需接受标准输入上的消息。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM