Debian Codegraph @

src/bnetd/handle_telnet.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2001 Ross Combs ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include "common/setup_before.h"
19 #include "handle_telnet.h"
20
21 #include <cstring>
22 #include <cctype>
23
24 #include "common/eventlog.h"
25 #include "common/packet.h"
26 #include "common/bnethash.h"
27 #include "common/xalloc.h"
28 #include "common/tag.h"
29 #include "common/xstring.h"
30
31 #include "connection.h"
32 #include "account.h"
33 #include "account_wrap.h"
34 #include "message.h"
35 #include "channel.h"
36 #include "command.h"
37 #ifdef WITH_LUA
38 #include "luainterface.h"
39 #endif
40 #include "common/setup_after.h"
41
42
43 namespace pvpgn
44 {
45
46 namespace bnetd
47 {
48
49 extern int handle_telnet_packet(t_connection * c, t_packet const * const packet)
50 {
51 t_packet * rpacket;
52
53 if (!c)
54 {
55 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL connection", conn_get_socket(c));
56 return -1;
57 }
58 if (!packet)
59 {
60 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL packet", conn_get_socket(c));
61 return -1;
62 }
63 if (packet_get_class(packet) != packet_class_raw)
64 {
65 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad packet (class {})", conn_get_socket(c), (int)packet_get_class(packet));
66 return -1;
67 }
68
69 {
70 char const * const linestr = packet_get_str_const(packet, 0, MAX_MESSAGE_LEN);
71
72 if (packet_get_size(packet) < 2) /* empty line */
73 return 0;
74 if (!linestr)
75 {
76 eventlog(eventlog_level_warn, __FUNCTION__, "[{}] line too long", conn_get_socket(c));
77 return 0;
78 }
79
80 switch (conn_get_state(c))
81 {
82 case conn_state_connected:
83 conn_add_flags(c, MF_PLUG);
84 conn_set_clienttag(c, CLIENTTAG_BNCHATBOT_UINT);
85
86 {
87 char const * temp = linestr;
88
89 if (temp[0] == '\004') /* FIXME: no echo, ignore for now (we always do no echo) */
90 temp = &temp[1];
91
92 if (temp[0] == '\0') /* empty line */
93 {
94 conn_set_state(c, conn_state_bot_username); /* don't look for ^D or reset tag and flags */
95 break;
96 }
97
98 conn_set_state(c, conn_state_bot_password);
99
100 if (conn_set_loggeduser(c, temp) < 0)
101 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not set username to \"{}\"", conn_get_socket(c), temp);
102
103 {
104 char const * const msg = "\r\nPassword: ";
105
106 if (!(rpacket = packet_create(packet_class_raw)))
107 {
108 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not create rpacket", conn_get_socket(c));
109 break;
110 }
111 #if 0 /* don't echo */
112 packet_append_ntstring(rpacket, conn_get_loggeduser(c));
113 #endif
114 packet_append_ntstring(rpacket, msg);
115 conn_push_outqueue(c, rpacket);
116 packet_del_ref(rpacket);
117 }
118 }
119 break;
120
121 case conn_state_bot_username:
122 conn_set_state(c, conn_state_bot_password);
123
124 if (conn_set_loggeduser(c, linestr) < 0)
125 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not set username to \"{}\"", conn_get_socket(c), linestr);
126
127 {
128 char const * const temp = "\r\nPassword: ";
129
130 if (!(rpacket = packet_create(packet_class_raw)))
131 {
132 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not create rpacket", conn_get_socket(c));
133 break;
134 }
135 #if 0 /* don't echo */
136 packet_append_ntstring(rpacket, linestr);
137 #endif
138 packet_append_ntstring(rpacket, temp);
139 conn_push_outqueue(c, rpacket);
140 packet_del_ref(rpacket);
141 }
142 break;
143
144 case conn_state_bot_password:
145 {
146 char const * const tempa = "\r\nLogin failed.\r\n\r\nUsername: ";
147 char const * const tempb = "\r\nAccount has no bot access.\r\n\r\nUsername: ";
148 char const * const loggeduser = conn_get_loggeduser(c);
149 t_account * account;
150 char const * oldstrhash1;
151 t_hash trypasshash1;
152 t_hash oldpasshash1;
153 char * testpass;
154
155 if (!loggeduser) /* error earlier in login */
156 {
157 /* no std::log message... */
158 conn_set_state(c, conn_state_bot_username);
159
160 if (!(rpacket = packet_create(packet_class_raw)))
161 {
162 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not create rpacket", conn_get_socket(c));
163 break;
164 }
165
166 packet_append_ntstring(rpacket, tempa);
167 conn_push_outqueue(c, rpacket);
168 packet_del_ref(rpacket);
169 break;
170 }
171 if (connlist_find_connection_by_accountname(loggeduser))
172 {
173 eventlog(eventlog_level_info, __FUNCTION__, "[{}] bot login for \"{}\" refused (already logged in)", conn_get_socket(c), loggeduser);
174 conn_set_state(c, conn_state_bot_username);
175
176 if (!(rpacket = packet_create(packet_class_raw)))
177 {
178 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not create rpacket", conn_get_socket(c));
179 break;
180 }
181
182 packet_append_ntstring(rpacket, tempa);
183 conn_push_outqueue(c, rpacket);
184 packet_del_ref(rpacket);
185 break;
186 }
187 if (!(account = accountlist_find_account(loggeduser)))
188 {
189 eventlog(eventlog_level_info, __FUNCTION__, "[{}] bot login for \"{}\" refused (bad account)", conn_get_socket(c), loggeduser);
190 conn_set_state(c, conn_state_bot_username);
191
192 if (!(rpacket = packet_create(packet_class_raw)))
193 {
194 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not create rpacket", conn_get_socket(c));
195 break;
196 }
197
198 packet_append_ntstring(rpacket, tempa);
199 conn_push_outqueue(c, rpacket);
200 packet_del_ref(rpacket);
201 break;
202 }
203 if ((oldstrhash1 = account_get_pass(account)))
204 {
205 if (hash_set_str(&oldpasshash1, oldstrhash1) < 0)
206 {
207 eventlog(eventlog_level_info, __FUNCTION__, "[{}] bot login for \"{}\" refused (corrupted passhash1?)", conn_get_socket(c), account_get_name(account));
208 conn_set_state(c, conn_state_bot_username);
209
210 if (!(rpacket = packet_create(packet_class_raw)))
211 {
212 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not create rpacket", conn_get_socket(c));
213 break;
214 }
215
216 packet_append_ntstring(rpacket, tempa);
217 conn_push_outqueue(c, rpacket);
218 packet_del_ref(rpacket);
219 break;
220 }
221
222 testpass = xstrdup(linestr);
223 {
224 strtolower(testpass);
225 }
226 if (bnet_hash(&trypasshash1, std::strlen(testpass), testpass) < 0) /* FIXME: force to lowercase */
227 {
228 eventlog(eventlog_level_info, __FUNCTION__, "[{}] bot login for \"{}\" refused (unable to hash password)", conn_get_socket(c), account_get_name(account));
229 xfree(testpass);
230
231 conn_set_state(c, conn_state_bot_username);
232
233 if (!(rpacket = packet_create(packet_class_raw)))
234 {
235 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not create rpacket", conn_get_socket(c));
236 break;
237 }
238
239 packet_append_ntstring(rpacket, tempa);
240 conn_push_outqueue(c, rpacket);
241 packet_del_ref(rpacket);
242 break;
243 }
244 xfree(testpass);
245 if (hash_eq(trypasshash1, oldpasshash1) != 1)
246 {
247 eventlog(eventlog_level_info, __FUNCTION__, "[{}] bot login for \"{}\" refused (wrong password)", conn_get_socket(c), account_get_name(account));
248 conn_set_state(c, conn_state_bot_username);
249
250 if (!(rpacket = packet_create(packet_class_raw)))
251 {
252 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not create rpacket", conn_get_socket(c));
253 break;
254 }
255
256 packet_append_ntstring(rpacket, tempa);
257 conn_push_outqueue(c, rpacket);
258 packet_del_ref(rpacket);
259 break;
260 }
261
262
263 if (account_get_auth_botlogin(account) != 1) /* default to false */
264 {
265 eventlog(eventlog_level_info, __FUNCTION__, "[{}] bot login for \"{}\" refused (no bot access)", conn_get_socket(c), account_get_name(account));
266 conn_set_state(c, conn_state_bot_username);
267
268 if (!(rpacket = packet_create(packet_class_raw)))
269 {
270 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not create rpacket", conn_get_socket(c));
271 break;
272 }
273
274 packet_append_ntstring(rpacket, tempb);
275 conn_push_outqueue(c, rpacket);
276 packet_del_ref(rpacket);
277 break;
278 }
279 else if (account_get_auth_lock(account) == 1) /* default to false */
280 {
281 eventlog(eventlog_level_info, __FUNCTION__, "[{}] bot login for \"{}\" refused (this account is locked)", conn_get_socket(c), account_get_name(account));
282 conn_set_state(c, conn_state_bot_username);
283
284 if (!(rpacket = packet_create(packet_class_raw)))
285 {
286 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not create rpacket", conn_get_socket(c));
287 break;
288 }
289
290 packet_append_ntstring(rpacket, tempb);
291 conn_push_outqueue(c, rpacket);
292 packet_del_ref(rpacket);
293 break;
294 }
295
296 eventlog(eventlog_level_info, __FUNCTION__, "[{}] \"{}\" bot logged in (correct password)", conn_get_socket(c), account_get_name(account));
297 #ifdef WITH_LUA
298 if (lua_handle_user(c, NULL, NULL, luaevent_user_login) == 1)
299 {
300 // feature to break login from Lua
301 conn_set_state(c, conn_state_destroy);
302 break;
303 }
304 #endif
305 }
306 else
307 eventlog(eventlog_level_info, __FUNCTION__, "[{}] \"{}\" bot logged in (no password)", conn_get_socket(c), account_get_name(account));
308
309 if (!(rpacket = packet_create(packet_class_raw))) /* if we got this far, let them std::log in even if this fails */
310 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not create rpacket", conn_get_socket(c));
311 else
312 {
313 packet_append_ntstring(rpacket, "\r\n");
314 conn_push_outqueue(c, rpacket);
315 packet_del_ref(rpacket);
316 }
317 message_send_text(c, message_type_uniqueid, c, loggeduser);
318
319 conn_login(c, account, loggeduser);
320
321 if (conn_set_channel(c, CHANNEL_NAME_CHAT) < 0)
322 conn_set_channel(c, CHANNEL_NAME_BANNED); /* should not fail */
323 }
324 break;
325
326 case conn_state_loggedin:
327 {
328 t_channel const * channel;
329
330 conn_set_idletime(c);
331
332 if ((channel = conn_get_channel(c)))
333 channel_message_log(channel, c, 1, linestr);
334 /* we don't log game commands currently */
335
336 if (linestr[0] == '/')
337 handle_command(c, linestr);
338 else
339 if (channel && !conn_quota_exceeded(c, linestr))
340 channel_message_send(channel, message_type_talk, c, linestr);
341 /* else discard */
342 }
343 break;
344
345 default:
346 eventlog(eventlog_level_error, __FUNCTION__, "[{}] unknown telnet connection state {}", conn_get_socket(c), (int)conn_get_state(c));
347 }
348 }
349
350 return 0;
351 }
352
353 }
354
355 }