def start_redir(lhost=None, lport=25, cport=9025): """start_redir(lhost=None, lport=25, cport=9025) Starts the redir program in such a way as to ensure that whatever listening port is specified, local or remote, connections get to the cport on the host where this is run. If remote execution is necessary then SSH will be used. lhost defaults to localhost.""" from pycopia import socket if lhost is None: lhost = socket.getfqdn() if socket.check_port(lhost, lport): raise RuntimeError, "something is already listening on (%r, %r)" % ( lhost, lport) if socket.islocal(lhost): if perm_check(): return redir(lport, cport) else: raise RuntimeError, "cannot run redir as non-root. check suid bit." else: myself = socket.getfqdn() # for this to work it assumes a lot... # 1. ssh is configured for public-key authentication (no # password). # 2. redir exists and is on the PATH on the remote host. # 3. redir is suid-root on the host. return remote_redir(lhost, lport, cport, caddr=myself)
def __init__(self, sock, workerclass, protocol): self._sock = sock _host, self.server_port = sock.getsockname() self.server_name = socket.getfqdn(_host) self._workerclass = workerclass self.protocol = protocol sock.setblocking(0) poller.register(self)
def ehlo(self, name=''): """ SMTP 'ehlo' command. Hostname to send for this command defaults to the FQDN of the local host. """ self.esmtp_features = {} name = name or self._bindto if name: self.putcmd(b"ehlo", name) else: self.putcmd(b"ehlo", socket.getfqdn()) (code, msg) = self.getreply() # According to RFC1869 some (badly written) # MTA's will disconnect on an ehlo. Toss an exception if # that happens -ddm if code == -1 and len(msg) == 0: self.close() raise SMTPServerDisconnected("Server not connected") self.ehlo_resp = msg if code != 250: return (code, msg) self.does_esmtp = 1 #parse the ehlo response -ddm resp = self.ehlo_resp.split('\n') del resp[0] for each in resp: # To be able to communicate with as many SMTP servers as possible, # we have to take the old-style auth advertisement into account, # because: # 1) Else our SMTP feature parser gets confused. # 2) There are some servers that only advertise the auth methods we # support using the old style. auth_match = OLDSTYLE_AUTH.match(each) if auth_match: # This doesn't remove duplicates, but that's no problem self.esmtp_features["auth"] = self.esmtp_features.get("auth", "") \ + " " + auth_match.groups(0)[0] continue # RFC 1869 requires a space between ehlo keyword and parameters. # It's actually stricter, in that only spaces are allowed between # parameters, but were not going to check for that here. Note # that the space isn't present if there are no parameters. m = re.match(r'(?P<feature>[A-Za-z0-9][A-Za-z0-9\-]*)', each) if m: feature = m.group("feature").lower() params = m.string[m.end("feature"):].strip() if feature == "auth": self.esmtp_features[feature] = self.esmtp_features.get(feature, "") \ + " " + params else: self.esmtp_features[feature] = params return (code, msg)
def ehlo(self, name=''): """ SMTP 'ehlo' command. Hostname to send for this command defaults to the FQDN of the local host. """ self.esmtp_features = {} name = name or self._bindto if name: self.putcmd(b"ehlo", name) else: self.putcmd(b"ehlo", socket.getfqdn()) (code,msg)=self.getreply() # According to RFC1869 some (badly written) # MTA's will disconnect on an ehlo. Toss an exception if # that happens -ddm if code == -1 and len(msg) == 0: self.close() raise SMTPServerDisconnected("Server not connected") self.ehlo_resp=msg if code != 250: return (code,msg) self.does_esmtp=1 #parse the ehlo response -ddm resp=self.ehlo_resp.split('\n') del resp[0] for each in resp: # To be able to communicate with as many SMTP servers as possible, # we have to take the old-style auth advertisement into account, # because: # 1) Else our SMTP feature parser gets confused. # 2) There are some servers that only advertise the auth methods we # support using the old style. auth_match = OLDSTYLE_AUTH.match(each) if auth_match: # This doesn't remove duplicates, but that's no problem self.esmtp_features["auth"] = self.esmtp_features.get("auth", "") \ + " " + auth_match.groups(0)[0] continue # RFC 1869 requires a space between ehlo keyword and parameters. # It's actually stricter, in that only spaces are allowed between # parameters, but were not going to check for that here. Note # that the space isn't present if there are no parameters. m=re.match(r'(?P<feature>[A-Za-z0-9][A-Za-z0-9\-]*)',each) if m: feature=m.group("feature").lower() params=m.string[m.end("feature"):].strip() if feature == "auth": self.esmtp_features[feature] = self.esmtp_features.get(feature, "") \ + " " + params else: self.esmtp_features[feature]=params return (code,msg)
def helo(self, name=''): """SMTP 'helo' command. Hostname to send for this command defaults to the FQDN of the local host. """ name = name or self._bindto if name: self.putcmd(b"helo", name) else: self.putcmd(b"helo", socket.getfqdn()) (code, msg) = self.getreply() self.helo_resp = msg return (code, msg)
def helo(self, name=''): """SMTP 'helo' command. Hostname to send for this command defaults to the FQDN of the local host. """ name = name or self._bindto if name: self.putcmd(b"helo", name) else: self.putcmd(b"helo", socket.getfqdn()) (code,msg)=self.getreply() self.helo_resp=msg return (code,msg)
def __init__(self, workerclass, protocol, port=None, host=None, processmodel=None, debug=False): port = port or workerclass.PORT or self.PORT host = host or "" self._procmanager = processmodel or ForkingModel() self.workerclass = workerclass self.protocol = protocol self.debug = debug self._sock = socket.tcp_listener((host, port), 5) _host, self.server_port = self._sock.getsockname() self.server_name = socket.getfqdn(_host) if debug: global debugger from pycopia import debugger
def start_redir(lhost=None, lport=25, cport=9025): """start_redir(lhost=None, lport=25, cport=9025) Starts the redir program in such a way as to ensure that whatever listening port is specified, local or remote, connections get to the cport on the host where this is run. If remote execution is necessary then SSH will be used. lhost defaults to localhost.""" from pycopia import socket if lhost is None: lhost = socket.getfqdn() if socket.check_port(lhost, lport): raise RuntimeError, "something is already listening on (%r, %r)" % (lhost, lport) if socket.islocal(lhost): if perm_check(): return redir(lport, cport) else: raise RuntimeError, "cannot run redir as non-root. check suid bit." else: myself = socket.getfqdn() # for this to work it assumes a lot... # 1. ssh is configured for public-key authentication (no # password). # 2. redir exists and is on the PATH on the remote host. # 3. redir is suid-root on the host. return remote_redir(lhost, lport, cport, caddr=myself)