| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
"""TELNET implementation, with line-oriented command handling. |
|---|
| 7 |
""" |
|---|
| 8 |
|
|---|
| 9 |
import warnings |
|---|
| 10 |
warnings.warn( |
|---|
| 11 |
"As of Twisted 2.1, twisted.protocols.telnet is deprecated. " |
|---|
| 12 |
"See twisted.conch.telnet for the current, supported API.", |
|---|
| 13 |
DeprecationWarning, |
|---|
| 14 |
stacklevel=2) |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
try: |
|---|
| 19 |
from cStringIO import StringIO |
|---|
| 20 |
except ImportError: |
|---|
| 21 |
from StringIO import StringIO |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
from twisted import copyright |
|---|
| 25 |
from twisted.internet import protocol |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
ESC = chr(27) |
|---|
| 29 |
BOLD_MODE_ON = ESC+"[1m" |
|---|
| 30 |
BOLD_MODE_OFF= ESC+"[m" |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
NULL = chr(0) |
|---|
| 36 |
LF = chr(10) |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
CR = chr(13) |
|---|
| 40 |
|
|---|
| 41 |
BEL = chr(7) |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
BS = chr(8) |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
HT = chr(9) |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
VT = chr(11) |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
FF = chr(12) |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
SE = chr(240) |
|---|
| 63 |
NOP= chr(241) |
|---|
| 64 |
DM = chr(242) |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
BRK= chr(243) |
|---|
| 69 |
IP = chr(244) |
|---|
| 70 |
AO = chr(245) |
|---|
| 71 |
AYT= chr(246) |
|---|
| 72 |
EC = chr(247) |
|---|
| 73 |
EL = chr(248) |
|---|
| 74 |
GA = chr(249) |
|---|
| 75 |
SB = chr(250) |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
WILL = chr(251) |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
WONT = chr(252) |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
DO = chr(253) |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
DONT = chr(254) |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
IAC = chr(255) |
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
ECHO = chr(1) |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
SUPGA = chr(3) |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
LINEMODE = chr(34) |
|---|
| 110 |
|
|---|
| 111 |
HIDE = chr(133) |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
NOECHO= chr(131) |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
iacBytes = { |
|---|
| 134 |
DO: 'DO', |
|---|
| 135 |
DONT: 'DONT', |
|---|
| 136 |
WILL: 'WILL', |
|---|
| 137 |
WONT: 'WONT', |
|---|
| 138 |
IP: 'IP' |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
def multireplace(st, dct): |
|---|
| 142 |
for k, v in dct.items(): |
|---|
| 143 |
st = st.replace(k, v) |
|---|
| 144 |
return st |
|---|
| 145 |
|
|---|
| 146 |
class Telnet(protocol.Protocol): |
|---|
| 147 |
"""I am a Protocol for handling Telnet connections. I have two |
|---|
| 148 |
sets of special methods, telnet_* and iac_*. |
|---|
| 149 |
|
|---|
| 150 |
telnet_* methods get called on every line sent to me. The method |
|---|
| 151 |
to call is decided by the current mode. The initial mode is 'User'; |
|---|
| 152 |
this means that telnet_User is the first telnet_* method to be called. |
|---|
| 153 |
All telnet_* methods should return a string which specifies the mode |
|---|
| 154 |
to go into next; thus dictating which telnet_* method to call next. |
|---|
| 155 |
For example, the default telnet_User method returns 'Password' to go |
|---|
| 156 |
into Password mode, and the default telnet_Password method returns |
|---|
| 157 |
'Command' to go into Command mode. |
|---|
| 158 |
|
|---|
| 159 |
The iac_* methods are less-used; they are called when an IAC telnet |
|---|
| 160 |
byte is received. You can define iac_DO, iac_DONT, iac_WILL, iac_WONT, |
|---|
| 161 |
and iac_IP methods to do what you want when one of these bytes is |
|---|
| 162 |
received.""" |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
gotIAC = 0 |
|---|
| 166 |
iacByte = None |
|---|
| 167 |
lastLine = None |
|---|
| 168 |
buffer = '' |
|---|
| 169 |
echo = 0 |
|---|
| 170 |
delimiters = ['\r\n', '\r\000'] |
|---|
| 171 |
mode = "User" |
|---|
| 172 |
|
|---|
| 173 |
def write(self, data): |
|---|
| 174 |
"""Send the given data over my transport.""" |
|---|
| 175 |
self.transport.write(data) |
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
def connectionMade(self): |
|---|
| 179 |
"""I will write a welcomeMessage and loginPrompt to the client.""" |
|---|
| 180 |
self.write(self.welcomeMessage() + self.loginPrompt()) |
|---|
| 181 |
|
|---|
| 182 |
def welcomeMessage(self): |
|---|
| 183 |
"""Override me to return a string which will be sent to the client |
|---|
| 184 |
before login.""" |
|---|
| 185 |
x = self.factory.__class__ |
|---|
| 186 |
return ("\r\n" + x.__module__ + '.' + x.__name__ + |
|---|
| 187 |
'\r\nTwisted %s\r\n' % copyright.version |
|---|
| 188 |
) |
|---|
| 189 |
|
|---|
| 190 |
def loginPrompt(self): |
|---|
| 191 |
"""Override me to return a 'login:'-type prompt.""" |
|---|
| 192 |
return "username: " |
|---|
| 193 |
|
|---|
| 194 |
def iacSBchunk(self, chunk): |
|---|
| 195 |
pass |
|---|
| 196 |
|
|---|
| 197 |
def iac_DO(self, feature): |
|---|
| 198 |
pass |
|---|
| 199 |
|
|---|
| 200 |
def iac_DONT(self, feature): |
|---|
| 201 |
pass |
|---|
| 202 |
|
|---|
| 203 |
def iac_WILL(self, feature): |
|---|
| 204 |
pass |
|---|
| 205 |
|
|---|
| 206 |
def iac_WONT(self, feature): |
|---|
| 207 |
pass |
|---|
| 208 |
|
|---|
| 209 |
def iac_IP(self, feature): |
|---|
| 210 |
pass |
|---|
| 211 |
|
|---|
| 212 |
def processLine(self, line): |
|---|
| 213 |
"""I call a method that looks like 'telnet_*' where '*' is filled |
|---|
| 214 |
in by the current mode. telnet_* methods should return a string which |
|---|
| 215 |
will become the new mode. If None is returned, the mode will not change. |
|---|
| 216 |
""" |
|---|
| 217 |
mode = getattr(self, "telnet_"+self.mode)(line) |
|---|
| 218 |
if mode is not None: |
|---|
| 219 |
self.mode = mode |
|---|
| 220 |
|
|---|
| 221 |
def telnet_User(self, user): |
|---|
| 222 |
"""I take a username, set it to the 'self.username' attribute, |
|---|
| 223 |
print out a password prompt, and switch to 'Password' mode. If |
|---|
| 224 |
you want to do something else when the username is received (ie, |
|---|
| 225 |
create a new user if the user doesn't exist), override me.""" |
|---|
| 226 |
self.username = user |
|---|
| 227 |
self.write(IAC+WILL+ECHO+"password: ") |
|---|
| 228 |
return "Password" |
|---|
| 229 |
|
|---|
| 230 |
def telnet_Password(self, paswd): |
|---|
| 231 |
"""I accept a password as an argument, and check it with the |
|---|
| 232 |
checkUserAndPass method. If the login is successful, I call |
|---|
| 233 |
loggedIn().""" |
|---|
| 234 |
self.write(IAC+WONT+ECHO+"*****\r\n") |
|---|
| 235 |
try: |
|---|
| 236 |
checked = self.checkUserAndPass(self.username, paswd) |
|---|
| 237 |
except: |
|---|
| 238 |
return "Done" |
|---|
| 239 |
if not checked: |
|---|
| 240 |
return "Done" |
|---|
| 241 |
self.loggedIn() |
|---|
| 242 |
return "Command" |
|---|
| 243 |
|
|---|
| 244 |
def telnet_Command(self, cmd): |
|---|
| 245 |
"""The default 'command processing' mode. You probably want to |
|---|
| 246 |
override me.""" |
|---|
| 247 |
return "Command" |
|---|
| 248 |
|
|---|
| 249 |
def processChunk(self, chunk): |
|---|
| 250 |
"""I take a chunk of data and delegate out to telnet_* methods |
|---|
| 251 |
by way of processLine. If the current mode is 'Done', I'll close |
|---|
| 252 |
the connection. """ |
|---|
| 253 |
self.buffer = self.buffer + chunk |
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
for delim in self.delimiters: |
|---|
| 257 |
idx = self.buffer.find(delim) |
|---|
| 258 |
if idx != -1: |
|---|
| 259 |
break |
|---|
| 260 |
|
|---|
| 261 |
while idx != -1: |
|---|
| 262 |
buf, self.buffer = self.buffer[:idx], self.buffer[idx+2:] |
|---|
| 263 |
self.processLine(buf) |
|---|
| 264 |
if self.mode == 'Done': |
|---|
| 265 |
self.transport.loseConnection() |
|---|
| 266 |
|
|---|
| 267 |
for delim in self.delimiters: |
|---|
| 268 |
idx = self.buffer.find(delim) |
|---|
| 269 |
if idx != -1: |
|---|
| 270 |
break |
|---|
| 271 |
|
|---|
| 272 |
def dataReceived(self, data): |
|---|
| 273 |
chunk = StringIO() |
|---|
| 274 |
|
|---|
| 275 |
for char in data: |
|---|
| 276 |
if self.gotIAC: |
|---|
| 277 |
|
|---|
| 278 |
if self.iacByte: |
|---|
| 279 |
|
|---|
| 280 |
if self.iacByte == SB: |
|---|
| 281 |
if char == SE: |
|---|
| 282 |
self.iacSBchunk(chunk.getvalue()) |
|---|
| 283 |
chunk = StringIO() |
|---|
| 284 |
del self.iacByte |
|---|
| 285 |
del self.gotIAC |
|---|
| 286 |
else: |
|---|
| 287 |
chunk.write(char) |
|---|
| 288 |
else: |
|---|
| 289 |
|
|---|
| 290 |
try: |
|---|
| 291 |
getattr(self, 'iac_%s' % iacBytes[self.iacByte])(char) |
|---|
| 292 |
except KeyError: |
|---|
| 293 |
pass |
|---|
| 294 |
del self.iacByte |
|---|
| 295 |
del self.gotIAC |
|---|
| 296 |
else: |
|---|
| 297 |
|
|---|
| 298 |
self.iacByte = char |
|---|
| 299 |
elif char == IAC: |
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
c = chunk.getvalue() |
|---|
| 305 |
if c: |
|---|
| 306 |
why = self.processChunk(c) |
|---|
| 307 |
if why: |
|---|
| 308 |
return why |
|---|
| 309 |
chunk = StringIO() |
|---|
| 310 |
self.gotIAC = 1 |
|---|
| 311 |
else: |
|---|
| 312 |
chunk.write(char) |
|---|
| 313 |
|
|---|
| 314 |
c = chunk.getvalue() |
|---|
| 315 |
if c: |
|---|
| 316 |
why = self.processChunk(c) |
|---|
| 317 |
if why: |
|---|
| 318 |
return why |
|---|
| 319 |
|
|---|
| 320 |
def loggedIn(self): |
|---|
| 321 |
"""Called after the user succesfully logged in. |
|---|
| 322 |
|
|---|
| 323 |
Override in subclasses. |
|---|
| 324 |
""" |
|---|
| 325 |
pass |
|---|