]> jfr.im git - irc/rqf/shadowircd.git/blame - src/parse.c
Wallops (and log) upon setting/unsetting of +M or any hidden chmode.
[irc/rqf/shadowircd.git] / src / parse.c
CommitLineData
212380e3 1/*
8ac75529 2 * charybdis: an advanced ircd.
212380e3 3 * parse.c: The message parser.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8ac75529 8 * Copyright (C) 2007 William Pitcock
212380e3 9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
212380e3 25 */
26
27#include "stdinc.h"
28#include "parse.h"
29#include "client.h"
30#include "channel.h"
31#include "common.h"
32#include "hash.h"
13ae2f4b 33#include "match.h"
212380e3 34#include "ircd.h"
35#include "numeric.h"
d3455e2c 36#include "logger.h"
212380e3 37#include "s_stats.h"
38#include "send.h"
39#include "msg.h"
40#include "s_conf.h"
212380e3 41#include "s_serv.h"
42#include "packet.h"
43
8ac75529
WP
44static struct Dictionary *cmd_dict = NULL;
45struct Dictionary *alias_dict = NULL;
46
ac408af6 47/* parv[0] is not used, and parv[LAST] == NULL */
212380e3 48static char *para[MAXPARA + 2];
49
50static void cancel_clients(struct Client *, struct Client *, char *);
51static void remove_unknown(struct Client *, char *, char *);
52
53static void do_numeric(char[], struct Client *, struct Client *, int, char **);
54static void do_alias(struct alias_entry *, struct Client *, char *);
55
56static int handle_command(struct Message *, struct Client *, struct Client *, int, const char**);
57
212380e3 58static char buffer[1024];
59
212380e3 60/* turn a string into a parc/parv pair */
61
62
63static inline int
64string_to_array(char *string, char **parv)
65{
66 char *p, *buf = string;
67 int x = 1;
68
69 parv[x] = NULL;
70 while (*buf == ' ') /* skip leading spaces */
71 buf++;
72 if(*buf == '\0') /* ignore all-space args */
73 return x;
74
75 do
76 {
77 if(*buf == ':') /* Last parameter */
78 {
79 buf++;
80 parv[x++] = buf;
81 parv[x] = NULL;
82 return x;
83 }
84 else
85 {
86 parv[x++] = buf;
87 parv[x] = NULL;
88 if((p = strchr(buf, ' ')) != NULL)
89 {
90 *p++ = '\0';
91 buf = p;
92 }
93 else
94 return x;
95 }
96 while (*buf == ' ')
97 buf++;
98 if(*buf == '\0')
99 return x;
100 }
ac408af6 101 /* we can go upto parv[MAXPARA], as parv[0] is skipped */
212380e3 102 while (x < MAXPARA);
103
104 if(*p == ':')
105 p++;
106
107 parv[x++] = p;
108 parv[x] = NULL;
109 return x;
110}
111
112/* parse()
113 *
78ab5aa4 114 * given a raw buffer, parses it and generates parv and parc
212380e3 115 */
116void
117parse(struct Client *client_p, char *pbuffer, char *bufend)
118{
119 struct Client *from = client_p;
78ab5aa4 120 char *sender;
212380e3 121 char *ch;
122 char *s;
123 char *end;
124 int i = 1;
125 char *numeric = 0;
126 struct Message *mptr;
127
128 s_assert(MyConnect(client_p));
6d4df0e3 129 s_assert(client_p->localClient->F != NULL);
212380e3 130 if(IsAnyDead(client_p))
131 return;
132
133 for (ch = pbuffer; *ch == ' '; ch++) /* skip spaces */
134 /* null statement */ ;
135
136 para[0] = from->name;
137
138 if(*ch == ':')
139 {
140 ch++;
141
142 /* point sender to the sender param */
143 sender = ch;
144
145 if((s = strchr(ch, ' ')))
146 {
147 *s = '\0';
148 s++;
149 ch = s;
150 }
151
152 if(*sender && IsServer(client_p))
153 {
f42e9ceb 154 from = find_client(sender);
212380e3 155
156 /* didnt find any matching client, issue a kill */
157 if(from == NULL)
158 {
83251205 159 ServerStats.is_unpf++;
212380e3 160 remove_unknown(client_p, sender, pbuffer);
161 return;
162 }
163
164 para[0] = from->name;
165
166 /* fake direction, hmm. */
167 if(from->from != client_p)
168 {
83251205 169 ServerStats.is_wrdi++;
212380e3 170 cancel_clients(client_p, from, pbuffer);
171 return;
172 }
173 }
174 while (*ch == ' ')
175 ch++;
176 }
177
178 if(*ch == '\0')
179 {
83251205 180 ServerStats.is_empt++;
212380e3 181 return;
182 }
183
184 /* at this point there must be some sort of command parameter */
185
186 /*
187 * Extract the command code from the packet. Point s to the end
188 * of the command code and calculate the length using pointer
189 * arithmetic. Note: only need length for numerics and *all*
190 * numerics must have parameters and thus a space after the command
191 * code. -avalon
192 */
193
194 /* EOB is 3 chars long but is not a numeric */
195
196 if(*(ch + 3) == ' ' && /* ok, lets see if its a possible numeric.. */
197 IsDigit(*ch) && IsDigit(*(ch + 1)) && IsDigit(*(ch + 2)))
198 {
199 mptr = NULL;
200 numeric = ch;
83251205 201 ServerStats.is_num++;
212380e3 202 s = ch + 3; /* I know this is ' ' from above if */
203 *s++ = '\0'; /* blow away the ' ', and point s to next part */
204 }
205 else
206 {
207 int ii = 0;
208
209 if((s = strchr(ch, ' ')))
210 *s++ = '\0';
211
90187f21 212 mptr = irc_dictionary_retrieve(cmd_dict, ch);
212380e3 213
214 /* no command or its encap only, error */
215 if(!mptr || !mptr->cmd)
216 {
217 /*
218 * Note: Give error message *only* to recognized
219 * persons. It's a nightmare situation to have
220 * two programs sending "Unknown command"'s or
221 * equivalent to each other at full blast....
222 * If it has got to person state, it at least
223 * seems to be well behaving. Perhaps this message
224 * should never be generated, though... --msa
225 * Hm, when is the buffer empty -- if a command
226 * code has been found ?? -Armin
227 */
228 if(pbuffer[0] != '\0')
229 {
230 if (IsPerson(client_p))
231 {
8ac75529 232 struct alias_entry *aptr = irc_dictionary_retrieve(alias_dict, ch);
212380e3 233 if (aptr != NULL)
234 {
235 do_alias(aptr, client_p, s);
236 return;
237 }
238 }
239 if(IsPerson(from))
240 {
241 sendto_one(from, form_str(ERR_UNKNOWNCOMMAND),
242 me.name, from->name, ch);
243 }
244 }
83251205 245 ServerStats.is_unco++;
212380e3 246 return;
247 }
248
249 ii = bufend - ((s) ? s : ch);
250 mptr->bytes += ii;
251 }
252
253 end = bufend - 1;
254
255 /* XXX this should be done before parse() is called */
256 if(*end == '\n')
257 *end-- = '\0';
258 if(*end == '\r')
259 *end = '\0';
260
261 if(s != NULL)
262 i = string_to_array(s, para);
263
264 if(mptr == NULL)
265 {
266 do_numeric(numeric, client_p, from, i, para);
267 return;
268 }
269
270 if(handle_command(mptr, client_p, from, i, /* XXX discards const!!! */ (const char **)para) < -1)
271 {
272 char *p;
273 for (p = pbuffer; p <= end; p += 8)
274 {
275 /* HACK HACK */
276 /* Its expected this nasty code can be removed
277 * or rewritten later if still needed.
278 */
279 if((unsigned long) (p + 8) > (unsigned long) end)
280 {
281 for (; p <= end; p++)
282 {
283 ilog(L_MAIN, "%02x |%c", p[0], p[0]);
284 }
285 }
286 else
287 ilog(L_MAIN,
288 "%02x %02x %02x %02x %02x %02x %02x %02x |%c%c%c%c%c%c%c%c",
289 p[0], p[1], p[2], p[3], p[4], p[5],
290 p[6], p[7], p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
291 }
292 }
293
294}
295
296/*
297 * handle_command
298 *
299 * inputs - pointer to message block
300 * - pointer to client
301 * - pointer to client message is from
302 * - count of number of args
303 * - pointer to argv[] array
304 * output - -1 if error from server
305 * side effects -
306 */
307static int
308handle_command(struct Message *mptr, struct Client *client_p,
309 struct Client *from, int i, const char** hpara)
310{
311 struct MessageEntry ehandler;
312 MessageHandler handler = 0;
313 char squitreason[80];
314
315 if(IsAnyDead(client_p))
316 return -1;
317
318 if(IsServer(client_p))
319 mptr->rcount++;
320
321 mptr->count++;
322
323 /* New patch to avoid server flooding from unregistered connects
324 - Pie-Man 07/27/2000 */
325
326 if(!IsRegistered(client_p))
327 {
328 /* if its from a possible server connection
329 * ignore it.. more than likely its a header thats sneaked through
330 */
331
332 if(IsAnyServer(client_p) && !(mptr->flags & MFLG_UNREG))
333 return (1);
334 }
335
336 ehandler = mptr->handlers[from->handler];
337 handler = ehandler.handler;
338
339 /* check right amount of params is passed... --is */
340 if(i < ehandler.min_para ||
341 (ehandler.min_para && EmptyString(hpara[ehandler.min_para - 1])))
342 {
343 if(!IsServer(client_p))
344 {
345 sendto_one(client_p, form_str(ERR_NEEDMOREPARAMS),
346 me.name,
347 EmptyString(client_p->name) ? "*" : client_p->name,
348 mptr->cmd);
349 if(MyClient(client_p))
350 return (1);
351 else
352 return (-1);
353 }
354
355 sendto_realops_snomask(SNO_GENERAL, L_ALL,
356 "Dropping server %s due to (invalid) command '%s'"
357 " with only %d arguments (expecting %d).",
358 client_p->name, mptr->cmd, i, ehandler.min_para);
359 ilog(L_SERVER,
360 "Insufficient parameters (%d < %d) for command '%s' from %s.",
361 i, ehandler.min_para, mptr->cmd, client_p->name);
5b0a5279 362 rb_snprintf(squitreason, sizeof squitreason,
212380e3 363 "Insufficient parameters (%d < %d) for command '%s'",
364 i, ehandler.min_para, mptr->cmd);
365 exit_client(client_p, client_p, client_p, squitreason);
366 return (-1);
367 }
368
369 (*handler) (client_p, from, i, hpara);
370 return (1);
371}
372
373void
374handle_encap(struct Client *client_p, struct Client *source_p,
375 const char *command, int parc, const char *parv[])
376{
377 struct Message *mptr;
378 struct MessageEntry ehandler;
379 MessageHandler handler = 0;
380
381 parv[0] = source_p->name;
382
8ac75529 383 mptr = irc_dictionary_retrieve(cmd_dict, command);
212380e3 384
385 if(mptr == NULL || mptr->cmd == NULL)
386 return;
387
388 ehandler = mptr->handlers[ENCAP_HANDLER];
389 handler = ehandler.handler;
390
391 if(parc < ehandler.min_para ||
392 (ehandler.min_para && EmptyString(parv[ehandler.min_para - 1])))
393 return;
394
395 (*handler) (client_p, source_p, parc, parv);
396}
397
398/*
399 * clear_hash_parse()
400 *
401 * inputs -
402 * output - NONE
403 * side effects - MUST MUST be called at startup ONCE before
404 * any other keyword hash routine is used.
405 *
406 */
407void
408clear_hash_parse()
409{
8ac75529 410 cmd_dict = irc_dictionary_create(strcasecmp);
212380e3 411}
412
413/* mod_add_cmd
414 *
415 * inputs - command name
416 * - pointer to struct Message
417 * output - none
418 * side effects - load this one command name
419 * msg->count msg->bytes is modified in place, in
420 * modules address space. Might not want to do that...
421 */
422void
423mod_add_cmd(struct Message *msg)
424{
212380e3 425 s_assert(msg != NULL);
426 if(msg == NULL)
427 return;
428
8ac75529
WP
429 if (irc_dictionary_find(cmd_dict, msg->cmd) != NULL)
430 return;
212380e3 431
432 msg->count = 0;
433 msg->rcount = 0;
434 msg->bytes = 0;
435
8ac75529 436 irc_dictionary_add(cmd_dict, msg->cmd, msg);
212380e3 437}
438
439/* mod_del_cmd
440 *
441 * inputs - command name
442 * output - none
443 * side effects - unload this one command name
444 */
445void
446mod_del_cmd(struct Message *msg)
447{
212380e3 448 s_assert(msg != NULL);
449 if(msg == NULL)
450 return;
451
8ac75529 452 irc_dictionary_delete(cmd_dict, msg->cmd);
212380e3 453}
454
455/*
456 * report_messages
457 *
458 * inputs - pointer to client to report to
459 * output - NONE
460 * side effects - NONE
461 */
462void
463report_messages(struct Client *source_p)
464{
8ac75529
WP
465 struct DictionaryIter iter;
466 struct Message *msg;
467 struct alias_entry *amsg;
212380e3 468
90187f21 469 DICTIONARY_FOREACH(msg, &iter, cmd_dict)
212380e3 470 {
8ac75529
WP
471 s_assert(msg->cmd != NULL);
472 sendto_one_numeric(source_p, RPL_STATSCOMMANDS,
473 form_str(RPL_STATSCOMMANDS),
474 msg->cmd, msg->count,
475 msg->bytes, msg->rcount);
476 }
212380e3 477
90187f21 478 DICTIONARY_FOREACH(amsg, &iter, alias_dict)
8ac75529
WP
479 {
480 s_assert(amsg->name != NULL);
481 sendto_one_numeric(source_p, RPL_STATSCOMMANDS,
482 form_str(RPL_STATSCOMMANDS),
483 amsg->name, amsg->hits, 0, 0);
212380e3 484 }
485}
486
487/* cancel_clients()
488 *
489 * inputs - client who sent us the message, client with fake
490 * direction, command
491 * outputs - a given warning about the fake direction
492 * side effects -
493 */
494static void
495cancel_clients(struct Client *client_p, struct Client *source_p, char *cmd)
496{
497 /* ok, fake prefix happens naturally during a burst on a nick
498 * collision with TS5, we cant kill them because one client has to
499 * survive, so we just send an error.
500 */
501 if(IsServer(source_p) || IsMe(source_p))
502 {
503 sendto_realops_snomask(SNO_DEBUG, L_ALL,
504 "Message for %s[%s] from %s",
505 source_p->name, source_p->from->name,
715b28fe 506 client_p->name);
212380e3 507 }
508 else
509 {
510 sendto_realops_snomask(SNO_DEBUG, L_ALL,
511 "Message for %s[%s@%s!%s] from %s (TS, ignored)",
512 source_p->name,
513 source_p->username,
514 source_p->host,
515 source_p->from->name,
715b28fe 516 client_p->name);
212380e3 517 }
518}
519
520/* remove_unknown()
521 *
522 * inputs - client who gave us message, supposed sender, buffer
523 * output -
524 * side effects - kills issued for clients, squits for servers
525 */
526static void
527remove_unknown(struct Client *client_p, char *lsender, char *lbuffer)
528{
529 int slen = strlen(lsender);
8d03179e
JT
530 char sid[4];
531 struct Client *server;
212380e3 532
3fde0c14 533 /* meepfoo is a nickname (ignore)
212380e3 534 * #XXXXXXXX is a UID (KILL)
535 * #XX is a SID (SQUIT)
536 * meep.foo is a server (SQUIT)
537 */
538 if((IsDigit(lsender[0]) && slen == 3) ||
539 (strchr(lsender, '.') != NULL))
540 {
541 sendto_realops_snomask(SNO_DEBUG, L_ALL,
542 "Unknown prefix (%s) from %s, Squitting %s",
715b28fe 543 lbuffer, client_p->name, lsender);
212380e3 544
545 sendto_one(client_p,
546 ":%s SQUIT %s :(Unknown prefix (%s) from %s)",
547 get_id(&me, client_p), lsender,
548 lbuffer, client_p->name);
549 }
8d03179e
JT
550 else if(!IsDigit(lsender[0]))
551 ;
552 else if(slen != 9)
553 sendto_realops_snomask(SNO_DEBUG, L_ALL,
554 "Invalid prefix (%s) from %s",
555 lbuffer, client_p->name);
556 else
557 {
558 memcpy(sid, lsender, 3);
559 sid[3] = '\0';
560 server = find_server(NULL, sid);
561 if (server != NULL && server->from == client_p)
562 sendto_one(client_p, ":%s KILL %s :%s (Unknown Client)",
563 get_id(&me, client_p), lsender, me.name);
564 }
212380e3 565}
566
567
568
569/*
570 *
571 * parc number of arguments ('sender' counted as one!)
212380e3 572 * parv[1]..parv[parc-1]
573 * pointers to additional parameters, this is a NULL
574 * terminated list (parv[parc] == NULL).
575 *
576 * *WARNING*
577 * Numerics are mostly error reports. If there is something
578 * wrong with the message, just *DROP* it! Don't even think of
579 * sending back a neat error message -- big danger of creating
580 * a ping pong error message...
581 */
582static void
583do_numeric(char numeric[], struct Client *client_p, struct Client *source_p, int parc, char *parv[])
584{
585 struct Client *target_p;
586 struct Channel *chptr;
587
588 if(parc < 2 || !IsServer(source_p))
589 return;
590
591 /* Remap low number numerics. */
592 if(numeric[0] == '0')
593 numeric[0] = '1';
594
595 /*
596 * Prepare the parameter portion of the message into 'buffer'.
597 * (Because the buffer is twice as large as the message buffer
598 * for the socket, no overflow can occur here... ...on current
599 * assumptions--bets are off, if these are changed --msa)
600 * Note: if buffer is non-empty, it will begin with SPACE.
601 */
602 if(parc > 1)
603 {
604 char *t = buffer; /* Current position within the buffer */
605 int i;
606 int tl; /* current length of presently being built string in t */
607 for (i = 2; i < (parc - 1); i++)
608 {
38e6acdd 609 tl = rb_sprintf(t, " %s", parv[i]);
212380e3 610 t += tl;
611 }
38e6acdd 612 rb_sprintf(t, " :%s", parv[parc - 1]);
212380e3 613 }
614
615 if((target_p = find_client(parv[1])) != NULL)
616 {
617 if(IsMe(target_p))
618 {
619 /*
620 * We shouldn't get numerics sent to us,
621 * any numerics we do get indicate a bug somewhere..
622 */
623 /* ugh. this is here because of nick collisions. when two servers
624 * relink, they burst each other their nicks, then perform collides.
625 * if there is a nick collision, BOTH servers will kill their own
626 * nicks, and BOTH will kill the other servers nick, which wont exist,
627 * because it will have been already killed by the local server.
628 *
629 * unfortunately, as we cant guarantee other servers will do the
630 * "right thing" on a nick collision, we have to keep both kills.
631 * ergo we need to ignore ERR_NOSUCHNICK. --fl_
632 */
633 /* quick comment. This _was_ tried. i.e. assume the other servers
634 * will do the "right thing" and kill a nick that is colliding.
635 * unfortunately, it did not work. --Dianora
636 */
637 /* note, now we send PING on server connect, we can
638 * also get ERR_NOSUCHSERVER..
639 */
640 if(atoi(numeric) != ERR_NOSUCHNICK &&
641 atoi(numeric) != ERR_NOSUCHSERVER)
642 sendto_realops_snomask(SNO_GENERAL, L_ADMIN,
643 "*** %s(via %s) sent a %s numeric to me: %s",
644 source_p->name,
645 client_p->name, numeric, buffer);
646 return;
647 }
648 else if(target_p->from == client_p)
649 {
650 /* This message changed direction (nick collision?)
651 * ignore it.
652 */
653 return;
654 }
655
656 /* csircd will send out unknown umode flag for +a (admin), drop it here. */
657 if((atoi(numeric) == ERR_UMODEUNKNOWNFLAG) && MyClient(target_p))
658 return;
659
660 /* Fake it for server hiding, if its our client */
661 sendto_one(target_p, ":%s %s %s%s",
662 get_id(source_p, target_p), numeric,
663 get_id(target_p, target_p), buffer);
664 return;
665 }
666 else if((chptr = find_channel(parv[1])) != NULL)
d23bc305
JT
667 sendto_channel_flags(client_p, ALL_MEMBERS, source_p, chptr,
668 "%s %s%s",
669 numeric, chptr->chname, buffer);
212380e3 670}
671
672static void do_alias(struct alias_entry *aptr, struct Client *source_p, char *text)
673{
674 char *p;
675 struct Client *target_p;
676
677 if (!IsFloodDone(source_p) && source_p->localClient->receiveM > 20)
678 flood_endgrace(source_p);
679
680 p = strchr(aptr->target, '@');
681 if (p != NULL)
682 {
683 /* user@server */
684 target_p = find_server(NULL, p + 1);
685 if (target_p != NULL && IsMe(target_p))
686 target_p = NULL;
687 }
688 else
689 {
690 /* nick, must be +S */
691 target_p = find_named_person(aptr->target);
692 if (target_p != NULL && !IsService(target_p))
693 target_p = NULL;
694 }
695
696 if (target_p == NULL)
697 {
698 sendto_one_numeric(source_p, ERR_SERVICESDOWN, form_str(ERR_SERVICESDOWN), aptr->target);
699 return;
700 }
701
702 if (text != NULL && *text == ':')
703 text++;
704 if (text == NULL || *text == '\0')
705 {
706 sendto_one(source_p, form_str(ERR_NOTEXTTOSEND), me.name, source_p->name);
707 return;
708 }
709
710 /* increment the hitcounter on this alias */
711 aptr->hits++;
712
713 sendto_one(target_p, ":%s PRIVMSG %s :%s",
714 get_id(source_p, target_p),
715 p != NULL ? aptr->target : get_id(target_p, target_p),
716 text);
717}
718
719int
720m_not_oper(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
721{
722 sendto_one_numeric(source_p, ERR_NOPRIVILEGES, form_str(ERR_NOPRIVILEGES));
723 return 0;
724}
725
726int
727m_unregistered(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
728{
729 /* bit of a hack.
730 * I don't =really= want to waste a bit in a flag
731 * number_of_nick_changes is only really valid after the client
732 * is fully registered..
733 */
734 if(client_p->localClient->number_of_nick_changes == 0)
735 {
736 sendto_one(client_p, form_str(ERR_NOTREGISTERED), me.name);
737 client_p->localClient->number_of_nick_changes++;
738 }
739
740 return 0;
741}
742
743int
744m_registered(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
745{
746 sendto_one(client_p, form_str(ERR_ALREADYREGISTRED), me.name, source_p->name);
747 return 0;
748}
749
750int
751m_ignore(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
752{
753 return 0;
754}