]> jfr.im git - irc/quakenet/snircd.git/blame - ircd/s_misc.c
import of 2.10.12.07
[irc/quakenet/snircd.git] / ircd / s_misc.c
CommitLineData
189935b1 1/*
2 * IRC - Internet Relay Chat, ircd/s_misc.c (formerly ircd/date.c)
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
5 *
6 * See file AUTHORS in IRC package for additional names of
7 * the programmers.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 1, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23/** @file
24 * @brief Miscellaneous support functions.
9f8856e9 25 * @version $Id: s_misc.c,v 1.50.2.1 2006/02/16 03:49:54 entrope Exp $
189935b1 26 */
27#include "config.h"
28
29#include "s_misc.h"
30#include "IPcheck.h"
31#include "channel.h"
32#include "client.h"
33#include "hash.h"
34#include "ircd.h"
35#include "ircd_alloc.h"
189935b1 36#include "ircd_features.h"
37#include "ircd_log.h"
38#include "ircd_reply.h"
39#include "ircd_snprintf.h"
40#include "ircd_string.h"
41#include "list.h"
42#include "match.h"
43#include "msg.h"
44#include "numeric.h"
45#include "numnicks.h"
46#include "parse.h"
47#include "querycmds.h"
48#include "res.h"
9f8856e9 49#include "s_auth.h"
189935b1 50#include "s_bsd.h"
51#include "s_conf.h"
52#include "s_debug.h"
53#include "s_stats.h"
54#include "s_user.h"
55#include "send.h"
56#include "struct.h"
57#include "sys.h"
58#include "uping.h"
59#include "userload.h"
60
61/* #include <assert.h> -- Now using assert in ircd_log.h */
62#include <fcntl.h>
63#include <netdb.h>
64#include <stdio.h>
65#include <string.h>
66#include <sys/stat.h>
67#include <unistd.h>
68
69/** Array of English month names (0 = January). */
70static char *months[] = {
71 "January", "February", "March", "April",
72 "May", "June", "July", "August",
73 "September", "October", "November", "December"
74};
75
76/** Array of English day names (0 = Sunday). */
77static char *weekdays[] = {
78 "Sunday", "Monday", "Tuesday", "Wednesday",
79 "Thursday", "Friday", "Saturday"
80};
81
82/*
83 * stats stuff
84 */
85/** Global statistics structure. */
86static struct ServerStatistics ircst;
87/** Public pointer to global statistics structure. */
88struct ServerStatistics* ServerStats = &ircst;
89
90/** Formats a Unix time as a readable string.
91 * @param clock Unix time to format (0 means #CurrentTime).
92 * @return Pointer to a static buffer containing something like
93 * "Sunday January 1 2000 -- 09:30 +01:00"
94 */
95char *date(time_t clock)
96{
97 static char buf[80], plus;
98 struct tm *lt, *gm;
99 struct tm gmbuf;
100 int minswest;
101
102 if (!clock)
103 clock = CurrentTime;
104 gm = gmtime(&clock);
105 memcpy(&gmbuf, gm, sizeof(gmbuf));
106 gm = &gmbuf;
107 lt = localtime(&clock);
108
109 /* There is unfortunately no clean portable way to extract time zone
110 * offset information, so do ugly things.
111 */
112 minswest = (gm->tm_hour - lt->tm_hour) * 60 + (gm->tm_min - lt->tm_min);
113 if (lt->tm_yday != gm->tm_yday)
114 {
115 if ((lt->tm_yday > gm->tm_yday && lt->tm_year == gm->tm_year) ||
116 (lt->tm_yday < gm->tm_yday && lt->tm_year != gm->tm_year))
117 minswest -= 24 * 60;
118 else
119 minswest += 24 * 60;
120 }
121
122 plus = (minswest > 0) ? '-' : '+';
123 if (minswest < 0)
124 minswest = -minswest;
125
126 sprintf(buf, "%s %s %d %d -- %02d:%02d %c%02d:%02d",
127 weekdays[lt->tm_wday], months[lt->tm_mon], lt->tm_mday,
128 1900 + lt->tm_year, lt->tm_hour, lt->tm_min,
129 plus, minswest / 60, minswest % 60);
130
131 return buf;
132}
133
134/** Like ctime() but with no trailing newline. Also, it takes
135 * the time value as parameter, instead of pointer to it.
136 * @param value Unix time to format.
137 * @return Pointer to a static buffer containing formatted time.
138 */
139char *myctime(time_t value)
140{
141 /* Use a secondary buffer in case ctime() would not replace an
142 * overwritten newline.
143 */
144 static char buf[28];
145 char *p;
146
147 strcpy(buf, ctime(&value));
148 if ((p = strchr(buf, '\n')) != NULL)
149 *p = '\0';
150
151 return buf;
152}
153
154/** Return the name of the client for various tracking and admin
155 * purposes. The main purpose of this function is to return the
156 * "socket host" name of the client, if that differs from the
157 * advertised name (other than case). But, this can be used on any
158 * client structure.
159 * @param sptr Client to operate on.
160 * @param showip If non-zero, append [username\@text-ip] to name.
161 * @return Either cli_name(\a sptr) or a static buffer.
162 */
163const char* get_client_name(const struct Client* sptr, int showip)
164{
165 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
166
167 if (!MyConnect(sptr) || !showip)
168 return cli_name(sptr);
169 ircd_snprintf(0, nbuf, sizeof(nbuf), "%s[%s@%s]", cli_name(sptr),
170 IsIdented(sptr) ? cli_username(sptr) : "",
171 cli_sock_ip(sptr));
172 return nbuf;
173}
174
175/**
176 * Exit one client, local or remote. Assuming for local client that
177 * all dependents already have been removed, and socket is closed.
178 * @param bcptr Client being (s)quitted.
179 * @param comment The QUIT comment to send.
180 */
181/* Rewritten by Run - 24 sept 94 */
182static void exit_one_client(struct Client* bcptr, const char* comment)
183{
184 struct SLink *lp;
185 struct Ban *bp;
186
187 if (cli_serv(bcptr) && cli_serv(bcptr)->client_list) /* Was SetServerYXX called ? */
188 ClearServerYXX(bcptr); /* Removes server from server_list[] */
189 if (IsUser(bcptr)) {
190 /*
191 * clear out uping requests
192 */
193 if (IsUPing(bcptr))
194 uping_cancel(bcptr, 0);
195 /*
196 * Stop a running /LIST clean
197 */
198 if (MyUser(bcptr) && cli_listing(bcptr)) {
199 MyFree(cli_listing(bcptr));
200 cli_listing(bcptr) = NULL;
201 }
202 /*
203 * If a person is on a channel, send a QUIT notice
204 * to every client (person) on the same channel (so
205 * that the client can show the "**signoff" message).
206 * (Note: The notice is to the local clients *only*)
207 */
208 sendcmdto_common_channels_butone(bcptr, CMD_QUIT, NULL, ":%s", comment);
209
210 remove_user_from_all_channels(bcptr);
211
212 /* Clean up invitefield */
213 while ((lp = cli_user(bcptr)->invited))
214 del_invite(bcptr, lp->value.chptr);
215
216 /* Clean up silencefield */
217 while ((bp = cli_user(bcptr)->silence)) {
218 cli_user(bcptr)->silence = bp->next;
219 free_ban(bp);
220 }
221
222 /* Clean up snotice lists */
223 if (MyUser(bcptr))
224 set_snomask(bcptr, ~0, SNO_DEL);
225
226 if (IsInvisible(bcptr))
227 --UserStats.inv_clients;
228 if (IsOper(bcptr))
229 --UserStats.opers;
230 if (MyConnect(bcptr))
231 Count_clientdisconnects(bcptr, UserStats);
232 else
233 Count_remoteclientquits(UserStats, bcptr);
234 }
235 else if (IsServer(bcptr))
236 {
237 /* Remove downlink list node of uplink */
238 remove_dlink(&(cli_serv(cli_serv(bcptr)->up))->down, cli_serv(bcptr)->updown);
239 cli_serv(bcptr)->updown = 0;
240
241 if (MyConnect(bcptr))
242 Count_serverdisconnects(UserStats);
243 else
244 Count_remoteserverquits(UserStats);
245 }
246 else if (IsMe(bcptr))
247 {
248 sendto_opmask_butone(0, SNO_OLDSNO, "ERROR: tried to exit me! : %s",
249 comment);
250 return; /* ...must *never* exit self! */
251 }
252 else if (IsUnknown(bcptr) || IsConnecting(bcptr) || IsHandshake(bcptr))
253 Count_unknowndisconnects(UserStats);
254
255 /*
256 * Update IPregistry
257 */
258 if (IsIPChecked(bcptr))
259 IPcheck_disconnect(bcptr);
260
261 /*
262 * Remove from serv->client_list
263 * NOTE: user is *always* NULL if this is a server
264 */
265 if (cli_user(bcptr)) {
266 assert(!IsServer(bcptr));
267 /* bcptr->user->server->serv->client_list[IndexYXX(bcptr)] = NULL; */
268 RemoveYXXClient(cli_user(bcptr)->server, cli_yxx(bcptr));
189935b1 269 }
270
271 /* Remove bcptr from the client list */
272#ifdef DEBUGMODE
273 if (hRemClient(bcptr) != 0)
274 Debug((DEBUG_ERROR, "%p !in tab %s[%s] %p %p %p %d %d %p",
275 bcptr, cli_name(bcptr), cli_from(bcptr) ? cli_sockhost(cli_from(bcptr)) : "??host",
276 cli_from(bcptr), cli_next(bcptr), cli_prev(bcptr), cli_fd(bcptr),
277 cli_status(bcptr), cli_user(bcptr)));
278#else
279 hRemClient(bcptr);
280#endif
281 remove_client_from_list(bcptr);
282}
283
284/* exit_downlinks - added by Run 25-9-94 */
285/**
286 * Removes all clients and downlinks (+clients) of any server
287 * QUITs are generated and sent to local users.
288 * @param cptr server that must have all dependents removed
289 * @param sptr source who thought that this was a good idea
290 * @param comment comment sent as sign off message to local clients
291 */
292static void exit_downlinks(struct Client *cptr, struct Client *sptr, char *comment)
293{
294 struct Client *acptr;
295 struct DLink *next;
296 struct DLink *lp;
297 struct Client **acptrp;
298 int i;
299
300 /* Run over all its downlinks */
301 for (lp = cli_serv(cptr)->down; lp; lp = next)
302 {
303 next = lp->next;
304 acptr = lp->value.cptr;
305 /* Remove the downlinks and client of the downlink */
306 exit_downlinks(acptr, sptr, comment);
307 /* Remove the downlink itself */
308 exit_one_client(acptr, cli_name(&me));
309 }
310 /* Remove all clients of this server */
311 acptrp = cli_serv(cptr)->client_list;
312 for (i = 0; i <= cli_serv(cptr)->nn_mask; ++acptrp, ++i) {
313 if (*acptrp)
314 exit_one_client(*acptrp, comment);
315 }
316}
317
318/* exit_client, rewritten 25-9-94 by Run */
319/**
320 * Exits a client of *any* type (user, server, etc)
321 * from this server. Also, this generates all necessary prototol
322 * messages that this exit may cause.
323 *
324 * This function implicitly exits all other clients depending on
325 * this connection.
326 *
327 * For convenience, this function returns a suitable value for
328 * m_function return value:
329 *
330 * CPTR_KILLED if (cptr == bcptr)
331 * 0 if (cptr != bcptr)
332 *
333 * This function can be called in two ways:
334 * 1) From before or in parse(), exiting the 'cptr', in which case it was
335 * invoked as exit_client(cptr, cptr, &me,...), causing it to always
336 * return CPTR_KILLED.
337 * 2) Via parse from a m_function call, in which case it was invoked as
338 * exit_client(cptr, acptr, sptr, ...). Here 'sptr' is known; the client
339 * that generated the message in a way that we can assume he already
340 * did remove acptr from memory himself (or in other cases we don't mind
341 * because he will be delinked.) Or invoked as:
342 * exit_client(cptr, acptr/sptr, &me, ...) when WE decide this one should
343 * be removed.
344 * In general: No generated SQUIT or QUIT should be sent to source link
345 * sptr->from. And CPTR_KILLED should be returned if cptr got removed (too).
346 *
347 * --Run
348 * @param cptr Connection currently being handled by read_message.
349 * @param victim Client being killed.
350 * @param killer Client that made the decision to remove \a victim.
351 * @param comment Reason for the exit.
352 * @return CPTR_KILLED if cptr == bcptr, else 0.
353 */
354int exit_client(struct Client *cptr,
355 struct Client* victim,
356 struct Client* killer,
357 const char* comment)
358{
359 struct Client* acptr = 0;
360 struct DLink *dlp;
361 time_t on_for;
362
363 char comment1[HOSTLEN + HOSTLEN + 2];
364 assert(killer);
365 if (MyConnect(victim))
366 {
367 SetFlag(victim, FLAG_CLOSING);
368
369 if (feature_bool(FEAT_CONNEXIT_NOTICES) && IsUser(victim))
370 sendto_opmask_butone(0, SNO_CONNEXIT,
371 "Client exiting: %s (%s@%s) [%s] [%s] <%s%s>",
372 cli_name(victim), cli_user(victim)->username,
373 cli_user(victim)->host, comment,
374 ircd_ntoa(&cli_ip(victim)),
375 NumNick(victim) /* two %s's */);
376 update_load();
377
378 on_for = CurrentTime - cli_firsttime(victim);
379
9f8856e9 380 if (IsUser(victim) || IsUserPort(victim))
381 auth_send_exit(victim);
382
189935b1 383 if (IsUser(victim))
384 log_write(LS_USER, L_TRACE, 0, "%Tu %i %s@%s %s %s %s%s %s :%s",
385 cli_firsttime(victim), on_for,
386 cli_user(victim)->username, cli_sockhost(victim),
387 ircd_ntoa(&cli_ip(victim)),
388 IsAccount(victim) ? cli_username(victim) : "0",
389 NumNick(victim), /* two %s's */
390 cli_name(victim), cli_info(victim));
391
392 if (victim != cli_from(killer) /* The source knows already */
393 && IsClient(victim)) /* Not a Ping struct or Log file */
394 {
395 if (IsServer(victim) || IsHandshake(victim))
396 sendcmdto_one(killer, CMD_SQUIT, victim, "%s 0 :%s", cli_name(&me), comment);
397 else if (!IsConnecting(victim)) {
398 if (!IsDead(victim)) {
399 if (IsServer(victim))
400 sendcmdto_one(killer, CMD_ERROR, victim,
401 ":Closing Link: %s by %s (%s)", cli_name(victim),
402 cli_name(killer), comment);
403 else
404 sendrawto_one(victim, MSG_ERROR " :Closing Link: %s by %s (%s)",
405 cli_name(victim),
406 cli_name(IsServer(killer) ? &his : killer),
407 comment);
408 }
409 }
410 if ((IsServer(victim) || IsHandshake(victim) || IsConnecting(victim)) &&
411 (killer == &me || (IsServer(killer) &&
412 (strncmp(comment, "Leaf-only link", 14) ||
413 strncmp(comment, "Non-Hub link", 12)))))
414 {
415 /*
416 * Note: check user == user needed to make sure we have the same
417 * client
418 */
419 if (cli_serv(victim)->user && *(cli_serv(victim))->by &&
420 (acptr = findNUser(cli_serv(victim)->by))) {
421 if (cli_user(acptr) == cli_serv(victim)->user) {
422 sendcmdto_one(&me, CMD_NOTICE, acptr,
423 "%C :Link with %s canceled: %s", acptr,
424 cli_name(victim), comment);
425 }
426 else {
427 /*
428 * not right client, set by to empty string
429 */
430 acptr = 0;
431 *(cli_serv(victim))->by = '\0';
432 }
433 }
434 if (killer == &me)
435 sendto_opmask_butone(acptr, SNO_OLDSNO, "Link with %s canceled: %s",
436 cli_name(victim), comment);
437 }
438 }
439 /*
440 * Close the Client connection first.
441 */
442 close_connection(victim);
443 }
444
445 if (IsServer(victim))
446 {
447 if (feature_bool(FEAT_HIS_NETSPLIT))
448 strcpy(comment1, "*.net *.split");
449 else
450 {
451 strcpy(comment1, cli_name(cli_serv(victim)->up));
452 strcat(comment1, " ");
453 strcat(comment1, cli_name(victim));
454 }
455
456 if (IsUser(killer))
457 sendto_opmask_butone(killer, SNO_OLDSNO, "%s SQUIT by %s [%s]:",
458 (cli_user(killer)->server == victim ||
459 cli_user(killer)->server == cli_serv(victim)->up) ?
460 "Local" : "Remote",
461 get_client_name(killer, HIDE_IP),
462 cli_name(cli_user(killer)->server));
463 else if (killer != &me && cli_serv(victim)->up != killer)
464 sendto_opmask_butone(0, SNO_OLDSNO, "Received SQUIT %s from %s :",
465 cli_name(victim), IsServer(killer) ? cli_name(killer) :
466 get_client_name(killer, HIDE_IP));
467 sendto_opmask_butone(0, SNO_NETWORK, "Net break: %C %C (%s)",
468 cli_serv(victim)->up, victim, comment);
469 }
470
471 /*
472 * First generate the needed protocol for the other server links
473 * except the source:
474 */
475 for (dlp = cli_serv(&me)->down; dlp; dlp = dlp->next) {
476 if (dlp->value.cptr != cli_from(killer) && dlp->value.cptr != victim)
477 {
478 if (IsServer(victim))
479 sendcmdto_one(killer, CMD_SQUIT, dlp->value.cptr, "%s %Tu :%s",
480 cli_name(victim), cli_serv(victim)->timestamp, comment);
481 else if (IsUser(victim) && !HasFlag(victim, FLAG_KILLED))
482 sendcmdto_one(victim, CMD_QUIT, dlp->value.cptr, ":%s", comment);
483 }
484 }
485 /* Then remove the client structures */
486 if (IsServer(victim))
487 exit_downlinks(victim, killer, comment1);
488 exit_one_client(victim, comment);
489
490 /*
491 * cptr can only have been killed if it was cptr itself that got killed here,
492 * because cptr can never have been a dependent of victim --Run
493 */
494 return (cptr == victim) ? CPTR_KILLED : 0;
495}
496
497/**
498 * Exit client with formatted va_list message.
499 * Thin wrapper around exit_client().
500 * @param cptr Connection being processed.
501 * @param bcptr Connection being closed.
502 * @param sptr Connection who asked to close the victim.
503 * @param pattern Format string for message.
504 * @param vl Stdargs argument list.
505 * @return Has a tail call to exit_client().
506 */
507/* added 25-9-94 by Run */
508int vexit_client_msg(struct Client *cptr, struct Client *bcptr, struct Client *sptr,
509 const char *pattern, va_list vl)
510{
511 char msgbuf[1024];
512 ircd_vsnprintf(0, msgbuf, sizeof(msgbuf), pattern, vl);
513 return exit_client(cptr, bcptr, sptr, msgbuf);
514}
515
516/**
517 * Exit client with formatted message using a variable-length argument list.
518 * Thin wrapper around exit_client().
519 * @param cptr Connection being processed.
520 * @param bcptr Connection being closed.
521 * @param sptr Connection who asked to close the victim.
522 * @param pattern Format string for message.
523 * @return Has a tail call to exit_client().
524 */
525int exit_client_msg(struct Client *cptr, struct Client *bcptr,
526 struct Client *sptr, const char *pattern, ...)
527{
528 va_list vl;
529 char msgbuf[1024];
530
531 va_start(vl, pattern);
532 ircd_vsnprintf(0, msgbuf, sizeof(msgbuf), pattern, vl);
533 va_end(vl);
534
535 return exit_client(cptr, bcptr, sptr, msgbuf);
536}
537
538/** Initialize global server statistics. */
539/* (Kind of pointless since C guarantees it's already zero'ed, but... */
540void initstats(void)
541{
542 memset(&ircst, 0, sizeof(ircst));
543}
544
545/** Report server statistics to a client.
546 * @param cptr Client who wants statistics.
547 * @param sd StatDesc structure being looked up (unused).
548 * @param param Extra parameter passed by user (unused).
549 */
550void tstats(struct Client *cptr, const struct StatDesc *sd, char *param)
551{
552 struct Client *acptr;
553 int i;
554 struct ServerStatistics *sp;
555 struct ServerStatistics tmp;
556
557 sp = &tmp;
558 memcpy(sp, ServerStats, sizeof(struct ServerStatistics));
559 for (i = 0; i < MAXCONNECTIONS; i++)
560 {
561 if (!(acptr = LocalClientArray[i]))
562 continue;
563 if (IsServer(acptr))
564 {
565 sp->is_sbs += cli_sendB(acptr);
566 sp->is_sbr += cli_receiveB(acptr);
567 sp->is_sti += CurrentTime - cli_firsttime(acptr);
568 sp->is_sv++;
569 }
570 else if (IsUser(acptr))
571 {
572 sp->is_cbs += cli_sendB(acptr);
573 sp->is_cbr += cli_receiveB(acptr);
574 sp->is_cti += CurrentTime - cli_firsttime(acptr);
575 sp->is_cl++;
576 }
577 else if (IsUnknown(acptr))
578 sp->is_ni++;
579 }
580
581 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":accepts %u refused %u",
582 sp->is_ac, sp->is_ref);
583 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
584 ":unknown commands %u prefixes %u", sp->is_unco, sp->is_unpf);
585 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
586 ":nick collisions %u unknown closes %u", sp->is_kill, sp->is_ni);
587 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
588 ":wrong direction %u empty %u", sp->is_wrdi, sp->is_empt);
589 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
590 ":numerics seen %u mode fakes %u", sp->is_num, sp->is_fake);
591 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
592 ":auth successes %u fails %u", sp->is_asuc, sp->is_abad);
593 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":local connections %u",
594 sp->is_loc);
595 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Client server");
596 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":connected %u %u",
597 sp->is_cl, sp->is_sv);
598 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":bytes sent %Lu %Lu",
599 sp->is_cbs, sp->is_sbs);
600 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":bytes recv %Lu %Lu",
601 sp->is_cbr, sp->is_sbr);
602 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":time connected %Lu %Lu",
603 sp->is_cti, sp->is_sti);
604}