ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/parse.c
Revision: 3614
Committed: Thu May 22 18:40:26 2014 UTC (11 years, 3 months ago) by michael
Content type: text/x-csrc
File size: 21128 byte(s)
Log Message:
- parse.c:mod_del_cmd(): make sure the command actually exists in the trie before
  attempting to remove it. We could end up in a corrupted trie otherwise.

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2014 ircd-hybrid development team
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 * USA
20 */
21
22 /*! \file parse.c
23 * \brief The message parser.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "client.h"
29 #include "parse.h"
30 #include "channel.h"
31 #include "hash.h"
32 #include "irc_string.h"
33 #include "ircd.h"
34 #include "numeric.h"
35 #include "log.h"
36 #include "send.h"
37 #include "conf.h"
38 #include "memory.h"
39 #include "user.h"
40 #include "server.h"
41
42
43 /*
44 * (based on orabidoo's parser code)
45 *
46 * This has always just been a trie. Look at volume III of Knuth ACP
47 *
48 *
49 * ok, you start out with an array of pointers, each one corresponds
50 * to a letter at the current position in the command being examined.
51 *
52 * so roughly you have this for matching 'trie' or 'tie'
53 *
54 * 't' points -> [MessageTree *] 'r' -> [MessageTree *] -> 'i'
55 * -> [MessageTree *] -> [MessageTree *] -> 'e' and matches
56 *
57 * 'i' -> [MessageTree *] -> 'e' and matches
58 *
59 * BUGS (Limitations!)
60 *
61 * I designed this trie to parse ircd commands. Hence it currently
62 * casefolds. This is trivial to fix by increasing MAXPTRLEN.
63 * This trie also "folds" '{' etc. down. This means, the input to this
64 * trie must be alpha tokens only. This again, is a limitation that
65 * can be overcome by increasing MAXPTRLEN to include upper/lower case
66 * at the expense of more memory. At the extreme end, you could make
67 * MAXPTRLEN 128.
68 *
69 * This is also not a patricia trie. On short ircd tokens, this is
70 * not likely going to matter.
71 *
72 * Diane Bruce (Dianora), June 6 2003
73 */
74
75 #define MAXPTRLEN 32
76 /* Must be a power of 2, and
77 * larger than 26 [a-z]|[A-Z]
78 * its used to allocate the set
79 * of pointers at each node of the tree
80 * There are MAXPTRLEN pointers at each node.
81 * Obviously, there have to be more pointers
82 * Than ASCII letters. 32 is a nice number
83 * since there is then no need to shift
84 * 'A'/'a' to base 0 index, at the expense
85 * of a few never used pointers. For a small
86 * parser like this, this is a good compromise
87 * and does make it somewhat faster.
88 *
89 * - Dianora
90 */
91
92 struct MessageTree
93 {
94 int links; /* Count of all pointers (including msg) at this node
95 * used as reference count for deletion of _this_ node.
96 */
97 struct Message *msg;
98 struct MessageTree *pointers[MAXPTRLEN];
99 };
100
101 static struct MessageTree msg_tree;
102
103 static char *para[MAXPARA + 2]; /* <command> + <params> + NULL */
104
105 static int cancel_clients(struct Client *, struct Client *, char *);
106 static void remove_unknown(struct Client *, char *, char *);
107 static void handle_numeric(unsigned int, struct Client *, int, char *[]);
108 static void handle_command(struct Message *, struct Client *, unsigned int, char *[]);
109
110
111 /*
112 * parse a buffer.
113 *
114 * NOTE: parse() should not be called recusively by any other functions!
115 */
116 void
117 parse(struct Client *client_p, char *pbuffer, char *bufend)
118 {
119 struct Client *from = client_p;
120 struct Message *msg_ptr = NULL;
121 char *ch = NULL;
122 char *s = NULL;
123 unsigned int numeric = 0;
124 unsigned int parc = 0;
125 unsigned int paramcount;
126
127 if (IsDefunct(client_p))
128 return;
129
130 assert(client_p->localClient->fd.flags.open);
131 assert((bufend - pbuffer) < IRCD_BUFSIZE);
132
133 for (ch = pbuffer; *ch == ' '; ++ch) /* skip spaces */
134 /* null statement */ ;
135
136 if (*ch == ':')
137 {
138 /*
139 * Copy the prefix to 'sender' assuming it terminates
140 * with SPACE (or NULL, which is an error, though).
141 */
142 char *sender = ++ch;
143
144 if ((s = strchr(ch, ' ')))
145 {
146 *s = '\0';
147 ch = ++s;
148 }
149
150 if (*sender && IsServer(client_p))
151 {
152 if ((from = find_person(client_p, sender)) == NULL)
153 from = hash_find_server(sender);
154
155 /*
156 * Hmm! If the client corresponding to the prefix is not found--what is
157 * the correct action??? Now, I will ignore the message (old IRC just
158 * let it through as if the prefix just wasn't there...) --msa
159 */
160 if (from == NULL)
161 {
162 ++ServerStats.is_unpf;
163 remove_unknown(client_p, sender, pbuffer);
164 return;
165 }
166
167 if (from->from != client_p)
168 {
169 ++ServerStats.is_wrdi;
170 cancel_clients(client_p, from, pbuffer);
171 return;
172 }
173 }
174
175 while (*ch == ' ')
176 ++ch;
177 }
178
179 if (*ch == '\0')
180 {
181 ++ServerStats.is_empt;
182 return;
183 }
184
185 /*
186 * Extract the command code from the packet. Point s to the end
187 * of the command code and calculate the length using pointer
188 * arithmetic. Note: only need length for numerics and *all*
189 * numerics must have parameters and thus a space after the command
190 * code. -avalon
191 */
192
193 /* EOB is 3 chars long but is not a numeric */
194 if (*(ch + 3) == ' ' && /* ok, lets see if its a possible numeric.. */
195 IsDigit(*ch) && IsDigit(*(ch + 1)) && IsDigit(*(ch + 2)))
196 {
197 numeric = (*ch - '0') * 100 + (*(ch + 1) - '0') * 10 + (*(ch + 2) - '0');
198 paramcount = 2; /* destination, and the rest of it */
199 ++ServerStats.is_num;
200 s = ch + 3; /* I know this is ' ' from above if */
201 *s++ = '\0'; /* blow away the ' ', and point s to next part */
202 }
203 else
204 {
205 unsigned int ii = 0;
206
207 if ((s = strchr(ch, ' ')))
208 *s++ = '\0';
209
210 if ((msg_ptr = find_command(ch)) == NULL)
211 {
212 /*
213 * Note: Give error message *only* to recognized
214 * persons. It's a nightmare situation to have
215 * two programs sending "Unknown command"'s or
216 * equivalent to each other at full blast....
217 * If it has got to person state, it at least
218 * seems to be well behaving. Perhaps this message
219 * should never be generated, though... --msa
220 * Hm, when is the buffer empty -- if a command
221 * code has been found ?? -Armin
222 */
223 if (*pbuffer)
224 if (IsClient(from))
225 sendto_one_numeric(from, &me, ERR_UNKNOWNCOMMAND, ch);
226
227 ++ServerStats.is_unco;
228 return;
229 }
230
231 assert(msg_ptr->cmd);
232
233 paramcount = msg_ptr->args_max;
234 ii = bufend - ((s) ? s : ch);
235 msg_ptr->bytes += ii;
236 }
237
238 /*
239 * Must the following loop really be so devious? On surface it
240 * splits the message to parameters from blank spaces. But, if
241 * paramcount has been reached, the rest of the message goes into
242 * this last parameter (about same effect as ":" has...) --msa
243 */
244
245 /* Note initially true: s == NULL || *(s - 1) == '\0' !! */
246
247 para[parc] = ch;
248
249 if (s)
250 {
251 if (paramcount > MAXPARA)
252 paramcount = MAXPARA;
253
254 while (1)
255 {
256 while (*s == ' ')
257 *s++ = '\0';
258
259 if (*s == '\0')
260 break;
261
262 if (*s == ':')
263 {
264 /* The rest is a single parameter */
265 para[++parc] = s + (!numeric); /* Keep the colon if it's a numeric */
266 break;
267 }
268
269 para[++parc] = s;
270
271 if (parc >= paramcount)
272 break;
273
274 while (*s && *s != ' ')
275 ++s;
276 }
277 }
278
279 para[++parc] = NULL;
280
281 if (msg_ptr)
282 handle_command(msg_ptr, from, parc, para);
283 else
284 handle_numeric(numeric, from, parc, para);
285 }
286
287 /* handle_command()
288 *
289 * inputs - pointer to message block
290 * - pointer to client
291 * - pointer to client message is from
292 * - count of number of args
293 * - pointer to argv[] array
294 * output - -1 if error from server
295 * side effects -
296 */
297 static void
298 handle_command(struct Message *mptr, struct Client *source_p,
299 unsigned int i, char *hpara[])
300 {
301 if (IsServer(source_p->from))
302 ++mptr->rcount;
303
304 ++mptr->count;
305
306 /* Check right amount of params is passed... --is */
307 if (i < mptr->args_min)
308 sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, mptr->cmd);
309 else
310 mptr->handlers[source_p->from->handler](source_p, i, hpara);
311 }
312
313 /* add_msg_element()
314 *
315 * inputs - pointer to MessageTree
316 * - pointer to Message to add for given command
317 * - pointer to current portion of command being added
318 * output - NONE
319 * side effects - recursively build the Message Tree ;-)
320 */
321 /*
322 * How this works.
323 *
324 * The code first checks to see if its reached the end of the command
325 * If so, that struct MessageTree has a msg pointer updated and the links
326 * count incremented, since a msg pointer is a reference.
327 * Then the code descends recursively, building the trie.
328 * If a pointer index inside the struct MessageTree is NULL a new
329 * child struct MessageTree has to be allocated.
330 * The links (reference count) is incremented as they are created
331 * in the parent.
332 */
333 static void
334 add_msg_element(struct MessageTree *mtree_p, struct Message *msg_p,
335 const char *cmd)
336 {
337 struct MessageTree *ntree_p;
338
339 if (*cmd == '\0')
340 {
341 mtree_p->msg = msg_p;
342 mtree_p->links++; /* Have msg pointer, so up ref count */
343 }
344 else
345 {
346 /*
347 * *cmd & (MAXPTRLEN-1)
348 * convert the char pointed to at *cmd from ASCII to an integer
349 * between 0 and MAXPTRLEN.
350 * Thus 'A' -> 0x1 'B' -> 0x2 'c' -> 0x3 etc.
351 */
352 if ((ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN - 1)]) == NULL)
353 {
354 ntree_p = MyCalloc(sizeof(struct MessageTree));
355 mtree_p->pointers[*cmd & (MAXPTRLEN - 1)] = ntree_p;
356
357 mtree_p->links++; /* Have new pointer, so up ref count */
358 }
359
360 add_msg_element(ntree_p, msg_p, cmd + 1);
361 }
362 }
363
364 /* del_msg_element()
365 *
366 * inputs - Pointer to MessageTree to delete from
367 * - pointer to command name to delete
368 * output - NONE
369 * side effects - recursively deletes a token from the Message Tree ;-)
370 */
371 /*
372 * How this works.
373 *
374 * Well, first off, the code recursively descends into the trie
375 * until it finds the terminating letter of the command being removed.
376 * Once it has done that, it marks the msg pointer as NULL then
377 * reduces the reference count on that allocated struct MessageTree
378 * since a command counts as a reference.
379 *
380 * Then it pops up the recurse stack. As it comes back up the recurse
381 * The code checks to see if the child now has no pointers or msg
382 * i.e. the links count has gone to zero. If its no longer used, the
383 * child struct MessageTree can be deleted. The parent reference
384 * to this child is then removed and the parents link count goes down.
385 * Thus, we continue to go back up removing all unused MessageTree(s)
386 */
387 static void
388 del_msg_element(struct MessageTree *mtree_p, const char *cmd)
389 {
390 struct MessageTree *ntree_p;
391
392 /*
393 * In case this is called for a nonexistent command
394 * check that there is a msg pointer here, else links-- goes -ve
395 * -db
396 */
397 if (*cmd == '\0' && mtree_p->msg)
398 {
399 mtree_p->msg = NULL;
400 mtree_p->links--;
401 }
402 else
403 {
404 if ((ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN - 1)]))
405 {
406 del_msg_element(ntree_p, cmd + 1);
407
408 if (ntree_p->links == 0)
409 {
410 mtree_p->pointers[*cmd & (MAXPTRLEN - 1)] = NULL;
411 mtree_p->links--;
412 MyFree(ntree_p);
413 }
414 }
415 }
416 }
417
418 /* msg_tree_parse()
419 *
420 * inputs - Pointer to command to find
421 * - Pointer to MessageTree root
422 * output - Find given command returning Message * if found NULL if not
423 * side effects - none
424 */
425 static struct Message *
426 msg_tree_parse(const char *cmd)
427 {
428 struct MessageTree *mtree = &msg_tree;
429
430 assert(cmd && *cmd);
431
432 while (IsAlpha(*cmd) && (mtree = mtree->pointers[*cmd & (MAXPTRLEN - 1)]))
433 if (*++cmd == '\0')
434 return mtree->msg;
435
436 return NULL;
437 }
438
439 /* mod_add_cmd()
440 *
441 * inputs - pointer to struct Message
442 * output - none
443 * side effects - load this one command name
444 * msg->count msg->bytes is modified in place, in
445 * modules address space. Might not want to do that...
446 */
447 void
448 mod_add_cmd(struct Message *msg)
449 {
450 assert(msg && msg->cmd);
451
452 /* Command already added? */
453 if (msg_tree_parse(msg->cmd))
454 return;
455
456 add_msg_element(&msg_tree, msg, msg->cmd);
457 msg->count = msg->rcount = msg->bytes = 0;
458 }
459
460 /* mod_del_cmd()
461 *
462 * inputs - pointer to struct Message
463 * output - none
464 * side effects - unload this one command name
465 */
466 void
467 mod_del_cmd(struct Message *msg)
468 {
469 assert(msg && msg->cmd);
470
471 if (!msg_tree_parse(msg->cmd))
472 return;
473
474 del_msg_element(&msg_tree, msg->cmd);
475 }
476
477 /* find_command()
478 *
479 * inputs - command name
480 * output - pointer to struct Message
481 * side effects - none
482 */
483 struct Message *
484 find_command(const char *cmd)
485 {
486 return msg_tree_parse(cmd);
487 }
488
489 static void
490 recurse_report_messages(struct Client *source_p, const struct MessageTree *mtree)
491 {
492 if (mtree->msg)
493 sendto_one_numeric(source_p, &me, RPL_STATSCOMMANDS,
494 mtree->msg->cmd,
495 mtree->msg->count, mtree->msg->bytes,
496 mtree->msg->rcount);
497
498 for (unsigned int i = 0; i < MAXPTRLEN; ++i)
499 if (mtree->pointers[i])
500 recurse_report_messages(source_p, mtree->pointers[i]);
501 }
502
503 /* report_messages()
504 *
505 * inputs - pointer to client to report to
506 * output - NONE
507 * side effects - client is shown list of commands
508 */
509 void
510 report_messages(struct Client *source_p)
511 {
512 const struct MessageTree *mtree = &msg_tree;
513
514 for (unsigned int i = 0; i < MAXPTRLEN; ++i)
515 if (mtree->pointers[i])
516 recurse_report_messages(source_p, mtree->pointers[i]);
517 }
518
519 /* cancel_clients()
520 *
521 * inputs -
522 * output -
523 * side effects -
524 */
525 static int
526 cancel_clients(struct Client *client_p, struct Client *source_p, char *cmd)
527 {
528 /*
529 * Kill all possible points that are causing confusion here,
530 * I'm not sure I've got this all right...
531 * - avalon
532 *
533 * Knowing avalon, probably not.
534 */
535
536 /*
537 * With TS, fake prefixes are a common thing, during the
538 * connect burst when there's a nick collision, and they
539 * must be ignored rather than killed because one of the
540 * two is surviving.. so we don't bother sending them to
541 * all ops everytime, as this could send 'private' stuff
542 * from lagged clients. we do send the ones that cause
543 * servers to be dropped though, as well as the ones from
544 * non-TS servers -orabidoo
545 */
546 /*
547 * Incorrect prefix for a server from some connection. If it is a
548 * client trying to be annoying, just QUIT them, if it is a server
549 * then the same deal.
550 */
551 if (IsServer(source_p) || IsMe(source_p))
552 {
553 sendto_realops_flags(UMODE_DEBUG, L_ADMIN, SEND_NOTICE,
554 "Message for %s[%s] from %s",
555 source_p->name, source_p->from->name,
556 get_client_name(client_p, SHOW_IP));
557 sendto_realops_flags(UMODE_DEBUG, L_OPER, SEND_NOTICE,
558 "Message for %s[%s] from %s",
559 source_p->name, source_p->from->name,
560 get_client_name(client_p, MASK_IP));
561 sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
562 "Not dropping server %s (%s) for Fake Direction",
563 client_p->name, source_p->name);
564 return -1;
565 /* return exit_client(client_p, client_p, &me, "Fake Direction");*/
566 }
567
568 /*
569 * Ok, someone is trying to impose as a client and things are
570 * confused. If we got the wrong prefix from a server, send out a
571 * kill, else just exit the lame client.
572 */
573 /*
574 * If the fake prefix is coming from a TS server, discard it
575 * silently -orabidoo
576 *
577 * all servers must be TS these days --is
578 */
579 sendto_realops_flags(UMODE_DEBUG, L_ADMIN, SEND_NOTICE,
580 "Message for %s[%s@%s!%s] from %s (TS, ignored)",
581 source_p->name, source_p->username, source_p->host,
582 source_p->from->name, get_client_name(client_p, SHOW_IP));
583 sendto_realops_flags(UMODE_DEBUG, L_OPER, SEND_NOTICE,
584 "Message for %s[%s@%s!%s] from %s (TS, ignored)",
585 source_p->name, source_p->username, source_p->host,
586 source_p->from->name, get_client_name(client_p, MASK_IP));
587 return 0;
588 }
589
590 /* remove_unknown()
591 *
592 * inputs -
593 * output -
594 * side effects -
595 */
596 static void
597 remove_unknown(struct Client *client_p, char *lsender, char *lbuffer)
598 {
599 /*
600 * Do kill if it came from a server because it means there is a ghost
601 * user on the other server which needs to be removed. -avalon
602 * Tell opers about this. -Taner
603 */
604 /*
605 * '[0-9]something' is an ID (KILL/SQUIT depending on its length)
606 * 'nodots' is a nickname (KILL)
607 * 'no.dot.at.start' is a server (SQUIT)
608 */
609 if ((IsDigit(*lsender) && strlen(lsender) <= IRC_MAXSID) || strchr(lsender, '.'))
610 {
611 sendto_realops_flags(UMODE_DEBUG, L_ADMIN, SEND_NOTICE,
612 "Unknown prefix (%s) from %s, Squitting %s",
613 lbuffer, get_client_name(client_p, SHOW_IP), lsender);
614 sendto_realops_flags(UMODE_DEBUG, L_OPER, SEND_NOTICE,
615 "Unknown prefix (%s) from %s, Squitting %s",
616 lbuffer, client_p->name, lsender);
617 sendto_one(client_p, ":%s SQUIT %s :(Unknown prefix (%s) from %s)",
618 me.id, lsender, lbuffer, client_p->name);
619 }
620 else
621 sendto_one(client_p, ":%s KILL %s :%s (Unknown Client)",
622 me.id, lsender, me.name);
623 }
624
625 /*
626 *
627 * parc number of arguments ('sender' counted as one!)
628 * parv[0] pointer to 'sender' (may point to empty string) (not used)
629 * parv[1]..parv[parc-1]
630 * pointers to additional parameters, this is a NULL
631 * terminated list (parv[parc] == NULL).
632 *
633 * *WARNING*
634 * Numerics are mostly error reports. If there is something
635 * wrong with the message, just *DROP* it! Don't even think of
636 * sending back a neat error message -- big danger of creating
637 * a ping pong error message...
638 *
639 * Rewritten by Nemesi, Jan 1999, to support numeric nicks in parv[1]
640 *
641 * Called when we get a numeric message from a remote _server_ and we are
642 * supposed to forward it somewhere. Note that we always ignore numerics sent
643 * to 'me' and simply drop the message if we can't handle with this properly:
644 * the savvy approach is NEVER generate an error in response to an... error :)
645 */
646 static void
647 handle_numeric(unsigned int numeric, struct Client *source_p, int parc, char *parv[])
648 {
649 struct Client *target_p = NULL;
650 struct Channel *chptr = NULL;
651
652 /*
653 * Avoid trash, we need it to come from a server and have a target
654 */
655 if (parc < 2 || !IsServer(source_p))
656 return;
657
658 /*
659 * Who should receive this message ? Will we do something with it ?
660 * Note that we use findUser functions, so the target can't be neither
661 * a server, nor a channel (?) nor a list of targets (?) .. u2.10
662 * should never generate numeric replies to non-users anyway
663 * Ahem... it can be a channel actually, csc bots use it :\ --Nem
664 */
665 if (IsChanPrefix(*parv[1]))
666 chptr = hash_find_channel(parv[1]);
667 else
668 target_p = find_person(source_p, parv[1]);
669
670 if (((!target_p) || (target_p->from == source_p->from)) && !chptr)
671 return;
672
673 /*
674 * Remap low number numerics, not that I understand WHY.. --Nemesi
675 */
676 /*
677 * Numerics below 100 talk about the current 'connection', you're not
678 * connected to a remote server so it doesn't make sense to send them
679 * remotely - but the information they contain may be useful, so we
680 * remap them up. Weird, but true. -- Isomer
681 */
682 if (numeric < 100)
683 numeric += 100;
684
685 if (target_p)
686 {
687 /* Fake it for server hiding, if its our client */
688 if (ConfigServerHide.hide_servers && MyClient(target_p) &&
689 !HasUMode(target_p, UMODE_OPER))
690 sendto_one_numeric(target_p, &me, numeric|SND_EXPLICIT, "%s", parv[2]);
691 else
692 sendto_one_numeric(target_p, source_p, numeric|SND_EXPLICIT, "%s", parv[2]);
693 }
694 else
695 sendto_channel_butone(source_p, source_p, chptr, 0, "%u %s %s",
696 numeric, chptr->chname, parv[2]);
697 }
698
699 /* m_not_oper()
700 * inputs -
701 * output -
702 * side effects - just returns a nastyogram to given user
703 */
704 int
705 m_not_oper(struct Client *source_p, int parc, char *parv[])
706 {
707 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
708 return 0;
709 }
710
711 int
712 m_unregistered(struct Client *source_p, int parc, char *parv[])
713 {
714 sendto_one_numeric(source_p, &me, ERR_NOTREGISTERED);
715 return 0;
716 }
717
718 int
719 m_registered(struct Client *source_p, int parc, char *parv[])
720 {
721 sendto_one_numeric(source_p, &me, ERR_ALREADYREGISTRED);
722 return 0;
723 }
724
725 int
726 m_ignore(struct Client *source_p, int parc, char *parv[])
727 {
728 return 0;
729 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision