Sockets and lemon curry

Carey Evans c.evans at clear.net.nz
Sun Jun 6 01:52:00 EDT 1999


"Ian King" <iking at killthewabbit.org> writes:

> I'm trying to implement an rshd server in Python.  The problem seems to be
> with establishing the secondary socket for stderr.

[snip]

> s2 = socket(AF_INET, SOCK_STREAM)
> s2.connect(addr[0], port2)    #addr tuple is host, port for conn
> 
> ....and that's where the client says, "Pining for the fjords???" and gives
> up.

I think you've missed something.  The OS will assign the local port
for this socket from 1024 up.  However, rsh requires that the remote
port that connects to it for stderr is less than 1024, to prove that
it's not just another user on the box pretending to be rshd, but a
process running as root.

You'll have to have the Python server bind() to a reserved port before 
trying to connect back to rsh.  Something like the following code may
be appropriate, and acts like the code the real rshd uses.

------------------------------------------------------------
import exceptions
import socket
import errno
import types

class RResvPortError(exceptions.Exception):
    def __str__(self):
        return 'No reserved ports available'

def rresvport():
    port = 1023
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    while port >= 1:
        try:
            s.bind('', port)
        except socket.error, detail:
            if type(detail) is not types.TupleType \
               or detail[0] != errno.EADDRINUSE:
                raise
        else:
            return s
        port = port - 1

    raise RResvPortError
------------------------------------------------------------

-- 
	 Carey Evans  http://home.clear.net.nz/pages/c.evans/

	     "I'm not a god.  I've just been misquoted."




More information about the Python-list mailing list