/[LeafOK_CVS]/pvpgn-1.7.4/src/bnetd/handle_bot.c
ViewVC logotype

Annotation of /pvpgn-1.7.4/src/bnetd/handle_bot.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (hide annotations) (vendor branch)
Tue Jun 6 03:41:37 2006 UTC (19 years, 9 months ago) by sysadm
Branch: GNU, MAIN
CVS Tags: pvpgn_1-7-4-0_MIL, arelease, HEAD
Changes since 1.1: +0 -0 lines
Content type: text/x-csrc
no message

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

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1