smtp proxy in python requires root authentication to run
Question: I have the following simple python code on Linux

    
    
    import smtpd
    proxy = smtpd.PureProxy(('0.0.0.0',25), None)
    

which runs fine when run as sudo, but gives an `socket.error: [Errno 13]
Permission denied` error when running as standard user. My question: How can I
make this snippet to run with a normal user.

Thanks,

Alex


Answer: If you want to bind to port 25 (or any port under 1024) you MUST be root. You
can however, change to a different user once the socket bind is complete.

    
    
    >>> import os
    >>> import smtpd
    >>> import pwd
    >>> uid = pwd.getpwnam('nobody').pw_uid
    >>> gid = pwd.getpwnam('nobody').pw_gid
    >>> proxy = smtpd.PureProxy(('0.0.0.0',25), None)
    >>> os.setgid(gid)
    >>> os.setuid(uid)
    >>> os.system('whoami')
    nobody
    0
    >>> 
    

This is, of course, a one way operation. Once you change to another user, you
can't change users again.

