handle_telnet.c
上传用户:tany51
上传日期:2013-06-12
资源大小:1397k
文件大小:11k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*
  2.  * Copyright (C) 2001  Ross Combs (rocombs@cs.nmsu.edu)
  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. #ifdef HAVE_STDDEF_H
  20. # include <stddef.h>
  21. #else
  22. # ifndef NULL
  23. #  define NULL ((void *)0)
  24. # endif
  25. #endif
  26. #ifdef STDC_HEADERS
  27. # include <stdlib.h>
  28. #endif
  29. #ifdef HAVE_STRING_H
  30. # include <string.h>
  31. #else
  32. # ifdef HAVE_STRINGS_H
  33. #  include <strings.h>
  34. # endif
  35. # ifdef HAVE_MEMORY_H
  36. #  include <memory.h>
  37. # endif
  38. #endif
  39. #include "compat/strdup.h"
  40. #include <ctype.h>
  41. #include "common/packet.h"
  42. #include "common/tag.h"
  43. #include "message.h"
  44. #include "common/eventlog.h"
  45. #include "command.h"
  46. #include "account.h"
  47. #include "connection.h"
  48. #include "channel.h"
  49. #include "common/queue.h"
  50. #include "common/bnethash.h"
  51. #include "common/bnethashconv.h"
  52. #include "common/bn_type.h"
  53. #include "common/field_sizes.h"
  54. #include "common/list.h"
  55. #include "handle_telnet.h"
  56. #include "common/setup_after.h"
  57. extern int handle_telnet_packet(t_connection * c, t_packet const * const packet)
  58. {
  59.     t_packet * rpacket;
  60.     
  61.     if (!c)
  62.     {
  63. eventlog(eventlog_level_error,"handle_telnet_packet","[%d] got NULL connection",conn_get_socket(c));
  64. return -1;
  65.     }
  66.     if (!packet)
  67.     {
  68. eventlog(eventlog_level_error,"handle_telnet_packet","[%d] got NULL packet",conn_get_socket(c));
  69. return -1;
  70.     }
  71.     if (packet_get_class(packet)!=packet_class_raw)
  72.     {
  73.         eventlog(eventlog_level_error,"handle_telnet_packet","[%d] got bad packet (class %d)",conn_get_socket(c),(int)packet_get_class(packet));
  74.         return -1;
  75.     }
  76.     
  77.     {
  78. char const * const linestr=packet_get_str_const(packet,0,MAX_MESSAGE_LEN);
  79. if (packet_get_size(packet)<2) /* empty line */
  80.     return 0;
  81. if (!linestr)
  82. {
  83.     eventlog(eventlog_level_warn,"handle_telnet_packet","[%d] line too long",conn_get_socket(c));
  84.     return 0;
  85. }
  86. switch (conn_get_state(c))
  87. {
  88. case conn_state_connected:
  89.     conn_add_flags(c,MF_PLUG);
  90.     conn_set_clienttag(c,CLIENTTAG_BNCHATBOT);
  91.     
  92.     {
  93. char const * temp=linestr;
  94. if (temp[0]=='04') /* FIXME: no echo, ignore for now (we always do no echo) */
  95.     temp = &temp[1];
  96. if (temp[0]=='') /* empty line */
  97. {
  98.     conn_set_state(c,conn_state_bot_username); /* don't look for ^D or reset tag and flags */
  99.     break;
  100. }
  101. conn_set_state(c,conn_state_bot_password);
  102. if (conn_set_botuser(c,temp)<0)
  103.     eventlog(eventlog_level_error,"handle_telnet_packet","[%d] could not set username to "%s"",conn_get_socket(c),temp);
  104. {
  105.     char const * const msg="rnPassword: ";
  106.     
  107.     if (!(rpacket = packet_create(packet_class_raw)))
  108.     {
  109. eventlog(eventlog_level_error,"handle_telnet_packet","[%d] could not create rpacket",conn_get_socket(c));
  110. break;
  111.     }
  112. #if 0 /* don't echo */
  113.     packet_append_ntstring(rpacket,conn_get_botuser(c));
  114. #endif
  115.     packet_append_ntstring(rpacket,msg);
  116.     conn_push_outqueue(c,rpacket);
  117.     packet_del_ref(rpacket);
  118. }
  119.     }
  120.     break;
  121.     
  122. case conn_state_bot_username:
  123.     conn_set_state(c,conn_state_bot_password);
  124.     
  125.     if (conn_set_botuser(c,linestr)<0)
  126. eventlog(eventlog_level_error,"handle_telnet_packet","[%d] could not set username to "%s"",conn_get_socket(c),linestr);
  127.     
  128.     {
  129. char const * const temp="rnPassword: ";
  130.     
  131. if (!(rpacket = packet_create(packet_class_raw)))
  132. {
  133.     eventlog(eventlog_level_error,"handle_telnet_packet","[%d] could not create rpacket",conn_get_socket(c));
  134.     break;
  135. }
  136. #if 0 /* don't echo */
  137. packet_append_ntstring(rpacket,linestr);
  138. #endif
  139. packet_append_ntstring(rpacket,temp);
  140. conn_push_outqueue(c,rpacket);
  141. packet_del_ref(rpacket);
  142.     }
  143.     break;
  144.     
  145. case conn_state_bot_password:
  146.     {
  147. char const * const tempa="rnLogin failed.rnrnUsername: ";
  148. char const * const tempb="rnAccount has no bot access.rnrnUsername: ";
  149. char const * const botuser=conn_get_botuser(c);
  150. t_account *        account;
  151. char const *       oldstrhash1;
  152. t_hash             trypasshash1;
  153. t_hash             oldpasshash1;
  154. char const *       tname;
  155. char *             testpass;
  156. if (!botuser) /* error earlier in login */
  157. {
  158.     /* no log message... */
  159.     conn_set_state(c,conn_state_bot_username);
  160.     
  161.     if (!(rpacket = packet_create(packet_class_raw)))
  162.     {
  163. eventlog(eventlog_level_error,"handle_telnet_packet","[%d] could not create rpacket",conn_get_socket(c));
  164. break;
  165.     }
  166.     
  167.     packet_append_ntstring(rpacket,tempa);
  168.     conn_push_outqueue(c,rpacket);
  169.     packet_del_ref(rpacket);
  170.     break;
  171. }
  172. if (connlist_find_connection_by_accountname(botuser))
  173. {
  174.     eventlog(eventlog_level_info,"handle_telnet_packet","[%d] bot login for "%s" refused (already logged in)",conn_get_socket(c),botuser);
  175.     conn_set_state(c,conn_state_bot_username);
  176.     
  177.     if (!(rpacket = packet_create(packet_class_raw)))
  178.     {
  179. eventlog(eventlog_level_error,"handle_telnet_packet","[%d] could not create rpacket",conn_get_socket(c));
  180. break;
  181.     }
  182.     
  183.     packet_append_ntstring(rpacket,tempa);
  184.     conn_push_outqueue(c,rpacket);
  185.     packet_del_ref(rpacket);
  186.     break;
  187. }
  188. if (!(account = accountlist_find_account(botuser)))
  189. {
  190. eventlog(eventlog_level_info,"handle_telnet_packet","[%d] bot login for "%s" refused (bad account)",conn_get_socket(c),botuser);
  191. conn_set_state(c,conn_state_bot_username);
  192. if (!(rpacket = packet_create(packet_class_raw)))
  193. {
  194.     eventlog(eventlog_level_error,"handle_telnet_packet","[%d] could not create rpacket",conn_get_socket(c));
  195.     break;
  196. }
  197. packet_append_ntstring(rpacket,tempa);
  198. conn_push_outqueue(c,rpacket);
  199. packet_del_ref(rpacket);
  200.     break;
  201. }
  202. if ((oldstrhash1 = account_get_pass(account)))
  203. {
  204.     if (hash_set_str(&oldpasshash1,oldstrhash1)<0)
  205.     {
  206. account_unget_pass(oldstrhash1);
  207. eventlog(eventlog_level_info,"handle_telnet_packet","[%d] bot login for "%s" refused (corrupted passhash1?)",conn_get_socket(c),(tname = account_get_name(account)));
  208. account_unget_name(tname);
  209. conn_set_state(c,conn_state_bot_username);
  210. if (!(rpacket = packet_create(packet_class_raw)))
  211. {
  212.     eventlog(eventlog_level_error,"handle_telnet_packet","[%d] could not create rpacket",conn_get_socket(c));
  213.     break;
  214. }
  215. packet_append_ntstring(rpacket,tempa);
  216. conn_push_outqueue(c,rpacket);
  217. packet_del_ref(rpacket);
  218. break;
  219.     }
  220.     account_unget_pass(oldstrhash1);
  221.     
  222.     if (!(testpass = strdup(linestr)))
  223. break;
  224.     {
  225. unsigned int i;
  226. for (i=0; i<strlen(testpass); i++)
  227.     if (isupper((int)testpass[i]))
  228. testpass[i] = tolower((int)testpass[i]);
  229.     }
  230.     if (bnet_hash(&trypasshash1,strlen(testpass),testpass)<0) /* FIXME: force to lowercase */
  231.     {
  232. eventlog(eventlog_level_info,"handle_telnet_packet","[%d] bot login for "%s" refused (unable to hash password)",conn_get_socket(c),(tname = account_get_name(account)));
  233. account_unget_name(tname);
  234. free(testpass);
  235. conn_set_state(c,conn_state_bot_username);
  236. if (!(rpacket = packet_create(packet_class_raw)))
  237. {
  238.     eventlog(eventlog_level_error,"handle_telnet_packet","[%d] could not create rpacket",conn_get_socket(c));
  239.     break;
  240. }
  241. packet_append_ntstring(rpacket,tempa);
  242. conn_push_outqueue(c,rpacket);
  243. packet_del_ref(rpacket);
  244. break;
  245.     }
  246.     free(testpass);
  247.     if (hash_eq(trypasshash1,oldpasshash1)!=1)
  248.     {
  249. eventlog(eventlog_level_info,"handle_telnet_packet","[%d] bot login for "%s" refused (wrong password)",conn_get_socket(c),(tname = account_get_name(account)));
  250. account_unget_name(tname);
  251. conn_set_state(c,conn_state_bot_username);
  252. if (!(rpacket = packet_create(packet_class_raw)))
  253. {
  254.     eventlog(eventlog_level_error,"handle_telnet_packet","[%d] could not create rpacket",conn_get_socket(c));
  255.     break;
  256. }
  257. packet_append_ntstring(rpacket,tempa);
  258. conn_push_outqueue(c,rpacket);
  259. packet_del_ref(rpacket);
  260. break;
  261.     }
  262.     
  263.     
  264.     if (account_get_auth_botlogin(account)!=1) /* default to false */
  265.     {
  266. eventlog(eventlog_level_info,"handle_telnet_packet","[%d] bot login for "%s" refused (no bot access)",conn_get_socket(c),(tname = account_get_name(account)));
  267. account_unget_name(tname);
  268. conn_set_state(c,conn_state_bot_username);
  269. if (!(rpacket = packet_create(packet_class_raw)))
  270. {
  271.     eventlog(eventlog_level_error,"handle_telnet_packet","[%d] could not create rpacket",conn_get_socket(c));
  272.     break;
  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,"handle_telnet_packet","[%d] bot login for "%s" refused (this account is locked)",conn_get_socket(c),(tname = account_get_name(account)));
  282. account_unget_name(tname);
  283. conn_set_state(c,conn_state_bot_username);
  284. if (!(rpacket = packet_create(packet_class_raw)))
  285. {
  286.     eventlog(eventlog_level_error,"handle_telnet_packet","[%d] could not create rpacket",conn_get_socket(c));
  287.     break;
  288. }
  289. packet_append_ntstring(rpacket,tempb);
  290. conn_push_outqueue(c,rpacket);
  291. packet_del_ref(rpacket);
  292. break;
  293.     }
  294.     
  295.     eventlog(eventlog_level_info,"handle_telnet_packet","[%d] "%s" bot logged in (correct password)",conn_get_socket(c),(tname = account_get_name(account)));
  296.     account_unget_name(tname);
  297. }
  298. else
  299. {
  300.     eventlog(eventlog_level_info,"handle_telnet_packet","[%d] "%s" bot logged in (no password)",conn_get_socket(c),(tname = account_get_name(account)));
  301.     account_unget_name(tname);
  302. }
  303.     if (!(rpacket = packet_create(packet_class_raw))) /* if we got this far, let them log in even if this fails */
  304. eventlog(eventlog_level_error,"handle_telnet_packet","[%d] could not create rpacket",conn_get_socket(c));
  305.     else
  306.     {
  307. packet_append_ntstring(rpacket,"rn");
  308. conn_push_outqueue(c,rpacket);
  309. packet_del_ref(rpacket);
  310.     }
  311.     message_send_text(c,message_type_uniqueid,c,(tname = account_get_name(account)));
  312.     account_unget_name(tname);
  313.     
  314.     conn_set_account(c,account);
  315.     
  316.     if (conn_set_channel(c,CHANNEL_NAME_CHAT)<0)
  317. conn_set_channel(c,CHANNEL_NAME_BANNED); /* should not fail */
  318.     }
  319.     break;
  320.     
  321. case conn_state_loggedin:
  322.     {
  323. t_channel const * channel;
  324. conn_set_idletime(c);
  325. if ((channel = conn_get_channel(c)))
  326.     channel_message_log(channel,c,1,linestr);
  327. /* we don't log game commands currently */
  328. if (linestr[0]=='/')
  329.     handle_command(c,linestr);
  330. else
  331.     if (channel && !conn_quota_exceeded(c,linestr))
  332. channel_message_send(channel,message_type_talk,c,linestr);
  333.     /* else discard */
  334.     }
  335.     break;
  336.     
  337. default:
  338.     eventlog(eventlog_level_error,"handle_telnet_packet","[%d] unknown telnet connection state %d",conn_get_socket(c),(int)conn_get_state(c));
  339. }
  340.     }
  341.     
  342.     return 0;
  343. }