| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
| 3 |
* parse.c: The message parser. |
| 4 |
* |
| 5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 20 |
* USA |
| 21 |
* |
| 22 |
* $Id$ |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "stdinc.h" |
| 26 |
#include "parse.h" |
| 27 |
#include "client.h" |
| 28 |
#include "channel.h" |
| 29 |
#include "handlers.h" |
| 30 |
#include "common.h" |
| 31 |
#include "hash.h" |
| 32 |
#include "irc_string.h" |
| 33 |
#include "sprintf_irc.h" |
| 34 |
#include "ircd.h" |
| 35 |
#include "numeric.h" |
| 36 |
#include "s_log.h" |
| 37 |
#include "s_stats.h" |
| 38 |
#include "send.h" |
| 39 |
#include "ircd_handler.h" |
| 40 |
#include "msg.h" |
| 41 |
#include "s_conf.h" |
| 42 |
#include "memory.h" |
| 43 |
#include "s_user.h" |
| 44 |
#include "s_serv.h" |
| 45 |
|
| 46 |
/* |
| 47 |
* (based on orabidoo's parser code) |
| 48 |
* |
| 49 |
* This has always just been a trie. Look at volume III of Knuth ACP |
| 50 |
* |
| 51 |
* |
| 52 |
* ok, you start out with an array of pointers, each one corresponds |
| 53 |
* to a letter at the current position in the command being examined. |
| 54 |
* |
| 55 |
* so roughly you have this for matching 'trie' or 'tie' |
| 56 |
* |
| 57 |
* 't' points -> [MessageTree *] 'r' -> [MessageTree *] -> 'i' |
| 58 |
* -> [MessageTree *] -> [MessageTree *] -> 'e' and matches |
| 59 |
* |
| 60 |
* 'i' -> [MessageTree *] -> 'e' and matches |
| 61 |
* |
| 62 |
* BUGS (Limitations!) |
| 63 |
* |
| 64 |
* I designed this trie to parse ircd commands. Hence it currently |
| 65 |
* casefolds. This is trivial to fix by increasing MAXPTRLEN. |
| 66 |
* This trie also "folds" '{' etc. down. This means, the input to this |
| 67 |
* trie must be alpha tokens only. This again, is a limitation that |
| 68 |
* can be overcome by increasing MAXPTRLEN to include upper/lower case |
| 69 |
* at the expense of more memory. At the extreme end, you could make |
| 70 |
* MAXPTRLEN 128. |
| 71 |
* |
| 72 |
* This is also not a patricia trie. On short ircd tokens, this is |
| 73 |
* not likely going to matter. |
| 74 |
* |
| 75 |
* Diane Bruce (Dianora), June 6 2003 |
| 76 |
*/ |
| 77 |
|
| 78 |
#define MAXPTRLEN 32 |
| 79 |
/* Must be a power of 2, and |
| 80 |
* larger than 26 [a-z]|[A-Z] |
| 81 |
* its used to allocate the set |
| 82 |
* of pointers at each node of the tree |
| 83 |
* There are MAXPTRLEN pointers at each node. |
| 84 |
* Obviously, there have to be more pointers |
| 85 |
* Than ASCII letters. 32 is a nice number |
| 86 |
* since there is then no need to shift |
| 87 |
* 'A'/'a' to base 0 index, at the expense |
| 88 |
* of a few never used pointers. For a small |
| 89 |
* parser like this, this is a good compromise |
| 90 |
* and does make it somewhat faster. |
| 91 |
* |
| 92 |
* - Dianora |
| 93 |
*/ |
| 94 |
|
| 95 |
struct MessageTree |
| 96 |
{ |
| 97 |
int links; /* Count of all pointers (including msg) at this node |
| 98 |
* used as reference count for deletion of _this_ node. |
| 99 |
*/ |
| 100 |
struct Message *msg; |
| 101 |
struct MessageTree *pointers[MAXPTRLEN]; |
| 102 |
}; |
| 103 |
|
| 104 |
static struct MessageTree msg_tree; |
| 105 |
|
| 106 |
/* |
| 107 |
* NOTE: parse() should not be called recursively by other functions! |
| 108 |
*/ |
| 109 |
static char *sender; |
| 110 |
static char *para[MAXPARA + 1]; |
| 111 |
static char buffer[1024]; |
| 112 |
|
| 113 |
static int cancel_clients(struct Client *, struct Client *, char *); |
| 114 |
static void remove_unknown(struct Client *, char *, char *); |
| 115 |
static void do_numeric(char[], struct Client *, struct Client *, int, char **); |
| 116 |
static void handle_command(struct Message *, struct Client *, struct Client *, unsigned int, char **); |
| 117 |
static void recurse_report_messages(struct Client *source_p, struct MessageTree *mtree); |
| 118 |
static void add_msg_element(struct MessageTree *mtree_p, struct Message *msg_p, const char *cmd); |
| 119 |
static void del_msg_element(struct MessageTree *mtree_p, const char *cmd); |
| 120 |
|
| 121 |
/* turn a string into a parc/parv pair */ |
| 122 |
static inline int |
| 123 |
string_to_array(char *string, char *parv[MAXPARA]) |
| 124 |
{ |
| 125 |
char *p; |
| 126 |
char *buf = string; |
| 127 |
int x = 1; |
| 128 |
|
| 129 |
parv[x] = NULL; |
| 130 |
|
| 131 |
while (*buf == ' ') /* skip leading spaces */ |
| 132 |
buf++; |
| 133 |
|
| 134 |
if (*buf == '\0') /* ignore all-space args */ |
| 135 |
return(x); |
| 136 |
|
| 137 |
do |
| 138 |
{ |
| 139 |
if (*buf == ':') /* Last parameter */ |
| 140 |
{ |
| 141 |
buf++; |
| 142 |
parv[x++] = buf; |
| 143 |
parv[x] = NULL; |
| 144 |
return(x); |
| 145 |
} |
| 146 |
else |
| 147 |
{ |
| 148 |
parv[x++] = buf; |
| 149 |
parv[x] = NULL; |
| 150 |
|
| 151 |
if ((p = strchr(buf, ' ')) != NULL) |
| 152 |
{ |
| 153 |
*p++ = '\0'; |
| 154 |
buf = p; |
| 155 |
} |
| 156 |
else |
| 157 |
return(x); |
| 158 |
} |
| 159 |
|
| 160 |
while (*buf == ' ') |
| 161 |
buf++; |
| 162 |
|
| 163 |
if (*buf == '\0') |
| 164 |
return(x); |
| 165 |
} while (x < MAXPARA - 1); |
| 166 |
|
| 167 |
if (*p == ':') |
| 168 |
p++; |
| 169 |
|
| 170 |
parv[x++] = p; |
| 171 |
parv[x] = NULL; |
| 172 |
return(x); |
| 173 |
} |
| 174 |
|
| 175 |
/* |
| 176 |
* parse a buffer. |
| 177 |
* |
| 178 |
* NOTE: parse() should not be called recusively by any other functions! |
| 179 |
*/ |
| 180 |
void |
| 181 |
parse(struct Client *client_p, char *pbuffer, char *bufend) |
| 182 |
{ |
| 183 |
struct Client *from = client_p; |
| 184 |
char *ch; |
| 185 |
char *s; |
| 186 |
char *numeric = 0; |
| 187 |
unsigned int i = 0; |
| 188 |
int paramcount; |
| 189 |
int mpara = 0; |
| 190 |
struct Message *mptr = NULL; |
| 191 |
|
| 192 |
if (IsDefunct(client_p)) |
| 193 |
return; |
| 194 |
|
| 195 |
assert(client_p->localClient->fd.flags.open); |
| 196 |
assert((bufend - pbuffer) < 512); |
| 197 |
|
| 198 |
for (ch = pbuffer; *ch == ' '; ch++) /* skip spaces */ |
| 199 |
/* null statement */ ; |
| 200 |
|
| 201 |
para[0] = from->name; |
| 202 |
|
| 203 |
if (*ch == ':') |
| 204 |
{ |
| 205 |
ch++; |
| 206 |
|
| 207 |
/* Copy the prefix to 'sender' assuming it terminates |
| 208 |
* with SPACE (or NULL, which is an error, though). |
| 209 |
*/ |
| 210 |
sender = ch; |
| 211 |
|
| 212 |
if ((s = strchr(ch, ' ')) != NULL) |
| 213 |
{ |
| 214 |
*s = '\0'; |
| 215 |
s++; |
| 216 |
ch = s; |
| 217 |
} |
| 218 |
|
| 219 |
if (*sender && IsServer(client_p)) |
| 220 |
{ |
| 221 |
/* |
| 222 |
* XXX it could be useful to know which of these occurs most frequently. |
| 223 |
* the ID check should always come first, though, since it is so easy. |
| 224 |
*/ |
| 225 |
if ((from = find_person(client_p, sender)) == NULL) |
| 226 |
{ |
| 227 |
from = find_server(sender); |
| 228 |
|
| 229 |
if (from == NULL && IsCapable(client_p, CAP_TS6) && |
| 230 |
client_p->name[0] == '*' && IsDigit(*sender) && strlen(sender) == 3) |
| 231 |
{ |
| 232 |
/* Dirty hack to allow messages from masked SIDs (i.e. the ones |
| 233 |
* hidden by fakename="..."). It shouldn't break anything, since |
| 234 |
* unknown SIDs don't happen during normal ircd work --adx |
| 235 |
*/ |
| 236 |
from = client_p; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/* Hmm! If the client corresponding to the |
| 241 |
* prefix is not found--what is the correct |
| 242 |
* action??? Now, I will ignore the message |
| 243 |
* (old IRC just let it through as if the |
| 244 |
* prefix just wasn't there...) --msa |
| 245 |
*/ |
| 246 |
if (from == NULL) |
| 247 |
{ |
| 248 |
ServerStats->is_unpf++; |
| 249 |
remove_unknown(client_p, sender, pbuffer); |
| 250 |
return; |
| 251 |
} |
| 252 |
|
| 253 |
para[0] = from->name; |
| 254 |
|
| 255 |
if (from->from != client_p) |
| 256 |
{ |
| 257 |
ServerStats->is_wrdi++; |
| 258 |
cancel_clients(client_p, from, pbuffer); |
| 259 |
return; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
while (*ch == ' ') |
| 264 |
ch++; |
| 265 |
} |
| 266 |
|
| 267 |
if (*ch == '\0') |
| 268 |
{ |
| 269 |
ServerStats->is_empt++; |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
/* Extract the command code from the packet. Point s to the end |
| 274 |
* of the command code and calculate the length using pointer |
| 275 |
* arithmetic. Note: only need length for numerics and *all* |
| 276 |
* numerics must have parameters and thus a space after the command |
| 277 |
* code. -avalon |
| 278 |
*/ |
| 279 |
|
| 280 |
/* EOB is 3 chars long but is not a numeric */ |
| 281 |
if (*(ch + 3) == ' ' && /* ok, lets see if its a possible numeric.. */ |
| 282 |
IsDigit(*ch) && IsDigit(*(ch + 1)) && IsDigit(*(ch + 2))) |
| 283 |
{ |
| 284 |
mptr = NULL; |
| 285 |
numeric = ch; |
| 286 |
paramcount = MAXPARA; |
| 287 |
ServerStats->is_num++; |
| 288 |
s = ch + 3; /* I know this is ' ' from above if */ |
| 289 |
*s++ = '\0'; /* blow away the ' ', and point s to next part */ |
| 290 |
} |
| 291 |
else |
| 292 |
{ |
| 293 |
int ii = 0; |
| 294 |
|
| 295 |
if ((s = strchr(ch, ' ')) != NULL) |
| 296 |
*s++ = '\0'; |
| 297 |
|
| 298 |
if ((mptr = find_command(ch)) == NULL) |
| 299 |
{ |
| 300 |
/* Note: Give error message *only* to recognized |
| 301 |
* persons. It's a nightmare situation to have |
| 302 |
* two programs sending "Unknown command"'s or |
| 303 |
* equivalent to each other at full blast.... |
| 304 |
* If it has got to person state, it at least |
| 305 |
* seems to be well behaving. Perhaps this message |
| 306 |
* should never be generated, though... --msa |
| 307 |
* Hm, when is the buffer empty -- if a command |
| 308 |
* code has been found ?? -Armin |
| 309 |
*/ |
| 310 |
if (pbuffer[0] != '\0') |
| 311 |
{ |
| 312 |
if (IsClient(from)) |
| 313 |
sendto_one(from, form_str(ERR_UNKNOWNCOMMAND), |
| 314 |
me.name, from->name, ch); |
| 315 |
} |
| 316 |
|
| 317 |
ServerStats->is_unco++; |
| 318 |
return; |
| 319 |
} |
| 320 |
|
| 321 |
assert(mptr->cmd != NULL); |
| 322 |
|
| 323 |
paramcount = mptr->parameters; |
| 324 |
mpara = mptr->maxpara; |
| 325 |
|
| 326 |
ii = bufend - ((s) ? s : ch); |
| 327 |
mptr->bytes += ii; |
| 328 |
} |
| 329 |
|
| 330 |
if (s != NULL) |
| 331 |
i = string_to_array(s, para); |
| 332 |
else |
| 333 |
{ |
| 334 |
i = 0; |
| 335 |
para[1] = NULL; |
| 336 |
} |
| 337 |
|
| 338 |
if (mptr == NULL) |
| 339 |
do_numeric(numeric, client_p, from, i, para); |
| 340 |
else |
| 341 |
handle_command(mptr, client_p, from, i, para); |
| 342 |
} |
| 343 |
|
| 344 |
/* handle_command() |
| 345 |
* |
| 346 |
* inputs - pointer to message block |
| 347 |
* - pointer to client |
| 348 |
* - pointer to client message is from |
| 349 |
* - count of number of args |
| 350 |
* - pointer to argv[] array |
| 351 |
* output - -1 if error from server |
| 352 |
* side effects - |
| 353 |
*/ |
| 354 |
static void |
| 355 |
handle_command(struct Message *mptr, struct Client *client_p, |
| 356 |
struct Client *from, unsigned int i, char *hpara[MAXPARA]) |
| 357 |
{ |
| 358 |
MessageHandler handler = 0; |
| 359 |
|
| 360 |
if (IsServer(client_p)) |
| 361 |
mptr->rcount++; |
| 362 |
|
| 363 |
mptr->count++; |
| 364 |
|
| 365 |
/* New patch to avoid server flooding from unregistered connects |
| 366 |
* - Pie-Man 07/27/2000 */ |
| 367 |
if (!IsRegistered(client_p)) |
| 368 |
{ |
| 369 |
/* if its from a possible server connection |
| 370 |
* ignore it.. more than likely its a header thats sneaked through |
| 371 |
*/ |
| 372 |
if ((IsHandshake(client_p) || IsConnecting(client_p) || |
| 373 |
IsServer(client_p)) && !(mptr->flags & MFLG_UNREG)) |
| 374 |
return; |
| 375 |
} |
| 376 |
|
| 377 |
handler = mptr->handlers[client_p->handler]; |
| 378 |
|
| 379 |
/* check right amount of params is passed... --is */ |
| 380 |
if (i < mptr->parameters) |
| 381 |
{ |
| 382 |
if (!IsServer(client_p)) |
| 383 |
{ |
| 384 |
sendto_one(client_p, form_str(ERR_NEEDMOREPARAMS), |
| 385 |
me.name, EmptyString(hpara[0]) ? "*" : hpara[0], mptr->cmd); |
| 386 |
} |
| 387 |
else |
| 388 |
{ |
| 389 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
| 390 |
"Dropping server %s due to (invalid) command '%s' " |
| 391 |
"with only %d arguments (expecting %d).", |
| 392 |
client_p->name, mptr->cmd, i, mptr->parameters); |
| 393 |
ilog(L_CRIT, "Insufficient parameters (%d) for command '%s' from %s.", |
| 394 |
i, mptr->cmd, client_p->name); |
| 395 |
exit_client(client_p, client_p, |
| 396 |
"Not enough arguments to server command."); |
| 397 |
} |
| 398 |
} |
| 399 |
else |
| 400 |
(*handler)(client_p, from, i, hpara); |
| 401 |
} |
| 402 |
|
| 403 |
/* clear_tree_parse() |
| 404 |
* |
| 405 |
* inputs - NONE |
| 406 |
* output - NONE |
| 407 |
* side effects - MUST MUST be called at startup ONCE before |
| 408 |
* any other keyword routine is used. |
| 409 |
*/ |
| 410 |
void |
| 411 |
clear_tree_parse(void) |
| 412 |
{ |
| 413 |
memset(&msg_tree, 0, sizeof(msg_tree)); |
| 414 |
} |
| 415 |
|
| 416 |
/* add_msg_element() |
| 417 |
* |
| 418 |
* inputs - pointer to MessageTree |
| 419 |
* - pointer to Message to add for given command |
| 420 |
* - pointer to current portion of command being added |
| 421 |
* output - NONE |
| 422 |
* side effects - recursively build the Message Tree ;-) |
| 423 |
*/ |
| 424 |
/* |
| 425 |
* How this works. |
| 426 |
* |
| 427 |
* The code first checks to see if its reached the end of the command |
| 428 |
* If so, that struct MessageTree has a msg pointer updated and the links |
| 429 |
* count incremented, since a msg pointer is a reference. |
| 430 |
* Then the code descends recursively, building the trie. |
| 431 |
* If a pointer index inside the struct MessageTree is NULL a new |
| 432 |
* child struct MessageTree has to be allocated. |
| 433 |
* The links (reference count) is incremented as they are created |
| 434 |
* in the parent. |
| 435 |
*/ |
| 436 |
static void |
| 437 |
add_msg_element(struct MessageTree *mtree_p, |
| 438 |
struct Message *msg_p, const char *cmd) |
| 439 |
{ |
| 440 |
struct MessageTree *ntree_p; |
| 441 |
|
| 442 |
if (*cmd == '\0') |
| 443 |
{ |
| 444 |
mtree_p->msg = msg_p; |
| 445 |
mtree_p->links++; /* Have msg pointer, so up ref count */ |
| 446 |
} |
| 447 |
else |
| 448 |
{ |
| 449 |
/* *cmd & (MAXPTRLEN-1) |
| 450 |
* convert the char pointed to at *cmd from ASCII to an integer |
| 451 |
* between 0 and MAXPTRLEN. |
| 452 |
* Thus 'A' -> 0x1 'B' -> 0x2 'c' -> 0x3 etc. |
| 453 |
*/ |
| 454 |
|
| 455 |
if ((ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN-1)]) == NULL) |
| 456 |
{ |
| 457 |
ntree_p = (struct MessageTree *)MyMalloc(sizeof(struct MessageTree)); |
| 458 |
mtree_p->pointers[*cmd & (MAXPTRLEN-1)] = ntree_p; |
| 459 |
|
| 460 |
mtree_p->links++; /* Have new pointer, so up ref count */ |
| 461 |
} |
| 462 |
add_msg_element(ntree_p, msg_p, cmd+1); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
/* del_msg_element() |
| 467 |
* |
| 468 |
* inputs - Pointer to MessageTree to delete from |
| 469 |
* - pointer to command name to delete |
| 470 |
* output - NONE |
| 471 |
* side effects - recursively deletes a token from the Message Tree ;-) |
| 472 |
*/ |
| 473 |
/* |
| 474 |
* How this works. |
| 475 |
* |
| 476 |
* Well, first off, the code recursively descends into the trie |
| 477 |
* until it finds the terminating letter of the command being removed. |
| 478 |
* Once it has done that, it marks the msg pointer as NULL then |
| 479 |
* reduces the reference count on that allocated struct MessageTree |
| 480 |
* since a command counts as a reference. |
| 481 |
* |
| 482 |
* Then it pops up the recurse stack. As it comes back up the recurse |
| 483 |
* The code checks to see if the child now has no pointers or msg |
| 484 |
* i.e. the links count has gone to zero. If its no longer used, the |
| 485 |
* child struct MessageTree can be deleted. The parent reference |
| 486 |
* to this child is then removed and the parents link count goes down. |
| 487 |
* Thus, we continue to go back up removing all unused MessageTree(s) |
| 488 |
*/ |
| 489 |
static void |
| 490 |
del_msg_element(struct MessageTree *mtree_p, const char *cmd) |
| 491 |
{ |
| 492 |
struct MessageTree *ntree_p; |
| 493 |
|
| 494 |
/* In case this is called for a nonexistent command |
| 495 |
* check that there is a msg pointer here, else links-- goes -ve |
| 496 |
* -db |
| 497 |
*/ |
| 498 |
|
| 499 |
if ((*cmd == '\0') && (mtree_p->msg != NULL)) |
| 500 |
{ |
| 501 |
mtree_p->msg = NULL; |
| 502 |
mtree_p->links--; |
| 503 |
} |
| 504 |
else |
| 505 |
{ |
| 506 |
if ((ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN-1)]) != NULL) |
| 507 |
{ |
| 508 |
del_msg_element(ntree_p, cmd+1); |
| 509 |
if (ntree_p->links == 0) |
| 510 |
{ |
| 511 |
mtree_p->pointers[*cmd & (MAXPTRLEN-1)] = NULL; |
| 512 |
mtree_p->links--; |
| 513 |
MyFree(ntree_p); |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
/* msg_tree_parse() |
| 520 |
* |
| 521 |
* inputs - Pointer to command to find |
| 522 |
* - Pointer to MessageTree root |
| 523 |
* output - Find given command returning Message * if found NULL if not |
| 524 |
* side effects - none |
| 525 |
*/ |
| 526 |
static struct Message * |
| 527 |
msg_tree_parse(const char *cmd, struct MessageTree *root) |
| 528 |
{ |
| 529 |
struct MessageTree *mtree; |
| 530 |
assert(cmd && *cmd); |
| 531 |
for (mtree = root->pointers[(*cmd) & (MAXPTRLEN-1)]; mtree != NULL; |
| 532 |
mtree = mtree->pointers[(*++cmd) & (MAXPTRLEN-1)]) |
| 533 |
{ |
| 534 |
if (!IsAlpha(*cmd)) |
| 535 |
return(NULL); |
| 536 |
if (*(cmd + 1) == '\0') |
| 537 |
return(mtree->msg); /* NULL if parsed invalid/unknown command */ |
| 538 |
|
| 539 |
} |
| 540 |
|
| 541 |
return(NULL); |
| 542 |
} |
| 543 |
|
| 544 |
/* mod_add_cmd() |
| 545 |
* |
| 546 |
* inputs - pointer to struct Message |
| 547 |
* output - none |
| 548 |
* side effects - load this one command name |
| 549 |
* msg->count msg->bytes is modified in place, in |
| 550 |
* modules address space. Might not want to do that... |
| 551 |
*/ |
| 552 |
void |
| 553 |
mod_add_cmd(struct Message *msg) |
| 554 |
{ |
| 555 |
struct Message *found_msg; |
| 556 |
|
| 557 |
if (msg == NULL) |
| 558 |
return; |
| 559 |
|
| 560 |
/* someone loaded a module with a bad messagetab */ |
| 561 |
assert(msg->cmd != NULL); |
| 562 |
|
| 563 |
/* command already added? */ |
| 564 |
if ((found_msg = msg_tree_parse(msg->cmd, &msg_tree)) != NULL) |
| 565 |
return; |
| 566 |
|
| 567 |
add_msg_element(&msg_tree, msg, msg->cmd); |
| 568 |
msg->count = msg->rcount = msg->bytes = 0; |
| 569 |
} |
| 570 |
|
| 571 |
/* mod_del_cmd() |
| 572 |
* |
| 573 |
* inputs - pointer to struct Message |
| 574 |
* output - none |
| 575 |
* side effects - unload this one command name |
| 576 |
*/ |
| 577 |
void |
| 578 |
mod_del_cmd(struct Message *msg) |
| 579 |
{ |
| 580 |
assert(msg != NULL); |
| 581 |
|
| 582 |
if (msg == NULL) |
| 583 |
return; |
| 584 |
|
| 585 |
del_msg_element(&msg_tree, msg->cmd); |
| 586 |
} |
| 587 |
|
| 588 |
/* find_command() |
| 589 |
* |
| 590 |
* inputs - command name |
| 591 |
* output - pointer to struct Message |
| 592 |
* side effects - none |
| 593 |
*/ |
| 594 |
struct Message * |
| 595 |
find_command(const char *cmd) |
| 596 |
{ |
| 597 |
return(msg_tree_parse(cmd, &msg_tree)); |
| 598 |
} |
| 599 |
|
| 600 |
/* report_messages() |
| 601 |
* |
| 602 |
* inputs - pointer to client to report to |
| 603 |
* output - NONE |
| 604 |
* side effects - client is shown list of commands |
| 605 |
*/ |
| 606 |
void |
| 607 |
report_messages(struct Client *source_p) |
| 608 |
{ |
| 609 |
struct MessageTree *mtree = &msg_tree; |
| 610 |
int i; |
| 611 |
|
| 612 |
for (i = 0; i < MAXPTRLEN; i++) |
| 613 |
{ |
| 614 |
if (mtree->pointers[i] != NULL) |
| 615 |
recurse_report_messages(source_p, mtree->pointers[i]); |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
static void |
| 620 |
recurse_report_messages(struct Client *source_p, struct MessageTree *mtree) |
| 621 |
{ |
| 622 |
int i; |
| 623 |
|
| 624 |
if (mtree->msg != NULL) |
| 625 |
{ |
| 626 |
sendto_one(source_p, form_str(RPL_STATSCOMMANDS), |
| 627 |
me.name, source_p->name, mtree->msg->cmd, |
| 628 |
mtree->msg->count, mtree->msg->bytes, |
| 629 |
mtree->msg->rcount); |
| 630 |
} |
| 631 |
|
| 632 |
for (i = 0; i < MAXPTRLEN; i++) |
| 633 |
{ |
| 634 |
if (mtree->pointers[i] != NULL) |
| 635 |
recurse_report_messages(source_p, mtree->pointers[i]); |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
/* cancel_clients() |
| 640 |
* |
| 641 |
* inputs - |
| 642 |
* output - |
| 643 |
* side effects - |
| 644 |
*/ |
| 645 |
static int |
| 646 |
cancel_clients(struct Client *client_p, struct Client *source_p, char *cmd) |
| 647 |
{ |
| 648 |
/* kill all possible points that are causing confusion here, |
| 649 |
* I'm not sure I've got this all right... |
| 650 |
* - avalon |
| 651 |
* |
| 652 |
* knowing avalon, probably not. |
| 653 |
*/ |
| 654 |
|
| 655 |
/* with TS, fake prefixes are a common thing, during the |
| 656 |
* connect burst when there's a nick collision, and they |
| 657 |
* must be ignored rather than killed because one of the |
| 658 |
* two is surviving.. so we don't bother sending them to |
| 659 |
* all ops everytime, as this could send 'private' stuff |
| 660 |
* from lagged clients. we do send the ones that cause |
| 661 |
* servers to be dropped though, as well as the ones from |
| 662 |
* non-TS servers -orabidoo |
| 663 |
*/ |
| 664 |
/* Incorrect prefix for a server from some connection. If it is a |
| 665 |
* client trying to be annoying, just QUIT them, if it is a server |
| 666 |
* then the same deal. |
| 667 |
*/ |
| 668 |
if (IsServer(source_p) || IsMe(source_p)) |
| 669 |
{ |
| 670 |
sendto_realops_flags(UMODE_DEBUG, L_ADMIN, "Message for %s[%s] from %s", |
| 671 |
source_p->name, source_p->from->name, |
| 672 |
get_client_name(client_p, SHOW_IP)); |
| 673 |
sendto_realops_flags(UMODE_DEBUG, L_OPER, "Message for %s[%s] from %s", |
| 674 |
source_p->name, source_p->from->name, |
| 675 |
get_client_name(client_p, MASK_IP)); |
| 676 |
sendto_realops_flags(UMODE_DEBUG, L_ALL, |
| 677 |
"Not dropping server %s (%s) for Fake Direction", |
| 678 |
client_p->name, source_p->name); |
| 679 |
return(-1); |
| 680 |
/* return exit_client(client_p, client_p, &me, "Fake Direction");*/ |
| 681 |
} |
| 682 |
|
| 683 |
/* Ok, someone is trying to impose as a client and things are |
| 684 |
* confused. If we got the wrong prefix from a server, send out a |
| 685 |
* kill, else just exit the lame client. |
| 686 |
*/ |
| 687 |
/* If the fake prefix is coming from a TS server, discard it |
| 688 |
* silently -orabidoo |
| 689 |
* |
| 690 |
* all servers must be TS these days --is |
| 691 |
*/ |
| 692 |
sendto_realops_flags(UMODE_DEBUG, L_ADMIN, |
| 693 |
"Message for %s[%s@%s!%s] from %s (TS, ignored)", |
| 694 |
source_p->name, source_p->username, source_p->host, |
| 695 |
source_p->from->name, get_client_name(client_p, SHOW_IP)); |
| 696 |
sendto_realops_flags(UMODE_DEBUG, L_OPER, |
| 697 |
"Message for %s[%s@%s!%s] from %s (TS, ignored)", |
| 698 |
source_p->name, source_p->username, source_p->host, |
| 699 |
source_p->from->name, get_client_name(client_p, MASK_IP)); |
| 700 |
|
| 701 |
return(0); |
| 702 |
} |
| 703 |
|
| 704 |
/* remove_unknown() |
| 705 |
* |
| 706 |
* inputs - |
| 707 |
* output - |
| 708 |
* side effects - |
| 709 |
*/ |
| 710 |
static void |
| 711 |
remove_unknown(struct Client *client_p, char *lsender, char *lbuffer) |
| 712 |
{ |
| 713 |
/* Do kill if it came from a server because it means there is a ghost |
| 714 |
* user on the other server which needs to be removed. -avalon |
| 715 |
* Tell opers about this. -Taner |
| 716 |
*/ |
| 717 |
/* '[0-9]something' is an ID (KILL/SQUIT depending on its length) |
| 718 |
* 'nodots' is a nickname (KILL) |
| 719 |
* 'no.dot.at.start' is a server (SQUIT) |
| 720 |
*/ |
| 721 |
if ((IsDigit(*lsender) && strlen(lsender) <= IRC_MAXSID) || |
| 722 |
strchr(lsender, '.') != NULL) |
| 723 |
{ |
| 724 |
sendto_realops_flags(UMODE_DEBUG, L_ADMIN, |
| 725 |
"Unknown prefix (%s) from %s, Squitting %s", |
| 726 |
lbuffer, get_client_name(client_p, SHOW_IP), lsender); |
| 727 |
sendto_realops_flags(UMODE_DEBUG, L_OPER, |
| 728 |
"Unknown prefix (%s) from %s, Squitting %s", |
| 729 |
lbuffer, client_p->name, lsender); |
| 730 |
sendto_one(client_p, ":%s SQUIT %s :(Unknown prefix (%s) from %s)", |
| 731 |
me.name, lsender, lbuffer, client_p->name); |
| 732 |
} |
| 733 |
else |
| 734 |
sendto_one(client_p, ":%s KILL %s :%s (Unknown Client)", |
| 735 |
me.name, lsender, me.name); |
| 736 |
} |
| 737 |
|
| 738 |
/* |
| 739 |
* |
| 740 |
* parc number of arguments ('sender' counted as one!) |
| 741 |
* parv[0] pointer to 'sender' (may point to empty string) (not used) |
| 742 |
* parv[1]..parv[parc-1] |
| 743 |
* pointers to additional parameters, this is a NULL |
| 744 |
* terminated list (parv[parc] == NULL). |
| 745 |
* |
| 746 |
* *WARNING* |
| 747 |
* Numerics are mostly error reports. If there is something |
| 748 |
* wrong with the message, just *DROP* it! Don't even think of |
| 749 |
* sending back a neat error message -- big danger of creating |
| 750 |
* a ping pong error message... |
| 751 |
*/ |
| 752 |
static void |
| 753 |
do_numeric(char numeric[], struct Client *client_p, struct Client *source_p, |
| 754 |
int parc, char *parv[]) |
| 755 |
{ |
| 756 |
struct Client *target_p; |
| 757 |
struct Channel *chptr; |
| 758 |
char *t; /* current position within the buffer */ |
| 759 |
int i, tl; /* current length of presently being built string in t */ |
| 760 |
|
| 761 |
if (parc < 2 || !IsServer(source_p)) |
| 762 |
return; |
| 763 |
|
| 764 |
/* Remap low number numerics. */ |
| 765 |
if (numeric[0] == '0') |
| 766 |
numeric[0] = '1'; |
| 767 |
|
| 768 |
/* Prepare the parameter portion of the message into 'buffer'. |
| 769 |
* (Because the buffer is twice as large as the message buffer |
| 770 |
* for the socket, no overflow can occur here... ...on current |
| 771 |
* assumptions--bets are off, if these are changed --msa) |
| 772 |
*/ |
| 773 |
t = buffer; |
| 774 |
for (i = 2; i < (parc - 1); i++) |
| 775 |
{ |
| 776 |
tl = ircsprintf(t, " %s", parv[i]); |
| 777 |
t += tl; |
| 778 |
} |
| 779 |
|
| 780 |
ircsprintf(t," :%s", parv[parc-1]); |
| 781 |
|
| 782 |
if (((target_p = find_person(client_p, parv[1])) != NULL) || |
| 783 |
((target_p = find_server(parv[1])) != NULL)) |
| 784 |
{ |
| 785 |
if (IsMe(target_p)) |
| 786 |
{ |
| 787 |
int num; |
| 788 |
|
| 789 |
/* |
| 790 |
* We shouldn't get numerics sent to us, |
| 791 |
* any numerics we do get indicate a bug somewhere.. |
| 792 |
*/ |
| 793 |
/* ugh. this is here because of nick collisions. when two servers |
| 794 |
* relink, they burst each other their nicks, then perform collides. |
| 795 |
* if there is a nick collision, BOTH servers will kill their own |
| 796 |
* nicks, and BOTH will kill the other servers nick, which wont exist, |
| 797 |
* because it will have been already killed by the local server. |
| 798 |
* |
| 799 |
* unfortunately, as we cant guarantee other servers will do the |
| 800 |
* "right thing" on a nick collision, we have to keep both kills. |
| 801 |
* ergo we need to ignore ERR_NOSUCHNICK. --fl_ |
| 802 |
*/ |
| 803 |
/* quick comment. This _was_ tried. i.e. assume the other servers |
| 804 |
* will do the "right thing" and kill a nick that is colliding. |
| 805 |
* unfortunately, it did not work. --Dianora |
| 806 |
*/ |
| 807 |
|
| 808 |
/* Yes, a good compiler would have optimised this, but |
| 809 |
* this is probably easier to read. -db |
| 810 |
*/ |
| 811 |
num = atoi(numeric); |
| 812 |
|
| 813 |
if ((num != ERR_NOSUCHNICK)) |
| 814 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, |
| 815 |
"*** %s(via %s) sent a %s numeric to me: %s", |
| 816 |
source_p->name, client_p->name, numeric, buffer); |
| 817 |
return; |
| 818 |
} |
| 819 |
else if (target_p->from == client_p) |
| 820 |
{ |
| 821 |
/* This message changed direction (nick collision?) |
| 822 |
* ignore it. |
| 823 |
*/ |
| 824 |
return; |
| 825 |
} |
| 826 |
|
| 827 |
/* csircd will send out unknown umode flag for +a (admin), drop it here. */ |
| 828 |
if ((atoi(numeric) == ERR_UMODEUNKNOWNFLAG) && MyClient(target_p)) |
| 829 |
return; |
| 830 |
|
| 831 |
/* Fake it for server hiding, if its our client */ |
| 832 |
if (ConfigServerHide.hide_servers && |
| 833 |
MyClient(target_p) && !IsOper(target_p)) |
| 834 |
sendto_one(target_p, ":%s %s %s%s", me.name, numeric, target_p->name, buffer); |
| 835 |
else if (!MyClient(target_p) && IsCapable(target_p->from, CAP_TS6) && HasID(source_p)) |
| 836 |
sendto_one(target_p, ":%s %s %s%s", source_p->id, numeric, target_p->id, buffer); |
| 837 |
else /* either it is our client, or a client linked throuh a non-ts6 server. must use names! */ |
| 838 |
sendto_one(target_p, ":%s %s %s%s", source_p->name, numeric, target_p->name, buffer); |
| 839 |
return; |
| 840 |
} |
| 841 |
else if ((chptr = hash_find_channel(parv[1])) != NULL) |
| 842 |
sendto_channel_local(ALL_MEMBERS, NO, chptr, |
| 843 |
":%s %s %s %s", |
| 844 |
source_p->name, |
| 845 |
numeric, chptr->chname, buffer); |
| 846 |
} |
| 847 |
|
| 848 |
/* m_not_oper() |
| 849 |
* inputs - |
| 850 |
* output - |
| 851 |
* side effects - just returns a nastyogram to given user |
| 852 |
*/ |
| 853 |
void |
| 854 |
m_not_oper(struct Client *client_p, struct Client *source_p, |
| 855 |
int parc, char *parv[]) |
| 856 |
{ |
| 857 |
sendto_one(source_p, form_str(ERR_NOPRIVILEGES), |
| 858 |
me.name, parv[0]); |
| 859 |
} |
| 860 |
|
| 861 |
void |
| 862 |
m_unregistered(struct Client *client_p, struct Client *source_p, |
| 863 |
int parc, char *parv[]) |
| 864 |
{ |
| 865 |
/* bit of a hack. |
| 866 |
* I don't =really= want to waste a bit in a flag |
| 867 |
* number_of_nick_changes is only really valid after the client |
| 868 |
* is fully registered.. |
| 869 |
*/ |
| 870 |
if (client_p->localClient->number_of_nick_changes == 0) |
| 871 |
{ |
| 872 |
sendto_one(client_p, ":%s %d * %s :Register first.", |
| 873 |
me.name, ERR_NOTREGISTERED, parv[0]); |
| 874 |
client_p->localClient->number_of_nick_changes++; |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
void |
| 879 |
m_registered(struct Client *client_p, struct Client *source_p, |
| 880 |
int parc, char *parv[]) |
| 881 |
{ |
| 882 |
sendto_one(client_p, form_str(ERR_ALREADYREGISTRED), |
| 883 |
me.name, parv[0]); |
| 884 |
} |
| 885 |
|
| 886 |
void |
| 887 |
m_ignore(struct Client *client_p, struct Client *source_p, |
| 888 |
int parc, char *parv[]) |
| 889 |
{ |
| 890 |
return; |
| 891 |
} |
| 892 |
|