Search code examples
pythonlinuxsocketscredentials

Unix socket credential passing in Python


How is Unix socket credential passing accomplished in Python?


Solution

  • Internet searches on this topic came up with surprisingly few results. I figured I'd post the question and answer here for others interested in this topic.

    The following client and server applications demonstrate how to accomplish this on Linux with the standard python interpreter. No extensions are required but, due to the use of embedded constants, the code is Linux-specific.

    Server:

    #!/usr/bin/env python
    
    import struct
    from socket import socket, AF_UNIX, SOCK_STREAM, SOL_SOCKET
    
    SO_PEERCRED = 17 # Pulled from /usr/include/asm-generic/socket.h
    
    s = socket(AF_UNIX, SOCK_STREAM)
    
    s.bind('/tmp/pass_cred')
    s.listen(1)
    
    conn, addr = s.accept()
    
    creds = conn.getsockopt(SOL_SOCKET, SO_PEERCRED, struct.calcsize('3i'))
    
    pid, uid, gid = struct.unpack('3i',creds)
    
    print 'pid: %d, uid: %d, gid %d' % (pid, uid, gid)
    

    Client:

    #!/usr/bin/env python
    
    from socket import socket, AF_UNIX, SOCK_STREAM, SOL_SOCKET
    
    SO_PASSCRED = 16 # Pulled from /usr/include/asm-generic/socket.h
    
    s = socket(AF_UNIX, SOCK_STREAM)
    
    s.setsockopt(SOL_SOCKET, SO_PASSCRED, 1)
    
    s.connect('/tmp/pass_cred')
    
    s.close()
    

    Unfortunately, the SO_PEERCRED and SO_PASSCRED constants are not exported by python's socket module so they must be entered by hand. Although these value are unlikely to change it is possible. This should be considered by any applications using this approach.