Fix typos in comments and strings to reduce future slumming for credit.
[ircu2.10.12-pk.git] / ircd / s_serv.c
1 /*
2  * IRC - Internet Relay Chat, ircd/s_serv.c (formerly ircd/s_msg.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 server support functions.
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include "s_serv.h"
30 #include "IPcheck.h"
31 #include "channel.h"
32 #include "client.h"
33 #include "gline.h"
34 #include "hash.h"
35 #include "ircd.h"
36 #include "ircd_alloc.h"
37 #include "ircd_log.h"
38 #include "ircd_reply.h"
39 #include "ircd_string.h"
40 #include "ircd_snprintf.h"
41 #include "ircd_crypt.h"
42 #include "jupe.h"
43 #include "list.h"
44 #include "match.h"
45 #include "msg.h"
46 #include "msgq.h"
47 #include "numeric.h"
48 #include "numnicks.h"
49 #include "parse.h"
50 #include "querycmds.h"
51 #include "s_bsd.h"
52 #include "s_conf.h"
53 #include "s_debug.h"
54 #include "s_misc.h"
55 #include "s_user.h"
56 #include "send.h"
57 #include "struct.h"
58 #include "sys.h"
59 #include "userload.h"
60
61 /* #include <assert.h> -- Now using assert in ircd_log.h */
62 #include <stdlib.h>
63 #include <string.h>
64
65 /** Maximum connection count since last restart. */
66 unsigned int max_connection_count = 0;
67 /** Maximum (local) client count since last restart. */
68 unsigned int max_client_count = 0;
69
70 /** Squit a new (pre-burst) server.
71  * @param cptr Local client that tried to introduce the server.
72  * @param sptr Server to disconnect.
73  * @param host Name of server being disconnected.
74  * @param timestamp Link time of server being disconnected.
75  * @param pattern Format string for squit message.
76  * @return CPTR_KILLED if cptr == sptr, else 0.
77  */
78 int exit_new_server(struct Client *cptr, struct Client *sptr, const char *host,
79                     time_t timestamp, const char *pattern, ...)
80 {
81   struct VarData vd;
82   int retval = 0;
83
84   vd.vd_format = pattern;
85   va_start(vd.vd_args, pattern);
86
87   if (!IsServer(sptr))
88     retval = vexit_client_msg(cptr, cptr, &me, pattern, vd.vd_args);
89   else
90     sendcmdto_one(&me, CMD_SQUIT, cptr, "%s %Tu :%v", host, timestamp, &vd);
91
92   va_end(vd.vd_args);
93
94   return retval;
95 }
96
97 /** Indicate whether \a a is between \a b and #me (that is, \a b would
98  * be killed if \a a squits).
99  * @param a A server that may be between us and \a b.
100  * @param b A client that may be on the far side of \a a.
101  * @return Non-zero if \a a is between \a b and #me.
102  */
103 int a_kills_b_too(struct Client *a, struct Client *b)
104 {
105   for (; b != a && b != &me; b = cli_serv(b)->up);
106   return (a == b ? 1 : 0);
107 }
108
109 /** Handle a connection that has sent a valid PASS and SERVER.
110  * @param cptr New peer server.
111  * @param aconf Connect block for \a cptr.
112  * @return Zero.
113  */
114 int server_estab(struct Client *cptr, struct ConfItem *aconf)
115 {
116   struct Client* acptr = 0;
117   const char*    inpath;
118   int            i;
119
120   assert(0 != cptr);
121   assert(0 != cli_local(cptr));
122
123   inpath = cli_name(cptr);
124
125   if (IsUnknown(cptr)) {
126     if (aconf->passwd[0])
127       sendrawto_one(cptr, MSG_PASS " :%s", aconf->passwd);
128     /*
129      *  Pass my info to the new server
130      */
131     sendrawto_one(cptr, MSG_SERVER " %s 1 %Tu %Tu J%s %s%s +%s6 :%s",
132                   cli_name(&me), cli_serv(&me)->timestamp,
133                   cli_serv(cptr)->timestamp, MAJOR_PROTOCOL, NumServCap(&me),
134                   feature_bool(FEAT_HUB) ? "h" : "",
135                   *(cli_info(&me)) ? cli_info(&me) : "IRCers United");
136     /*
137      * Don't charge this IP# for connecting
138      * XXX - if this comes from a server port, it will not have been added
139      * to the IP check registry, see add_connection in s_bsd.c
140      */
141     IPcheck_connect_fail(cptr);
142   }
143
144   det_confs_butmask(cptr, CONF_SERVER | CONF_UWORLD);
145
146   if (!IsHandshake(cptr))
147     hAddClient(cptr);
148   SetServer(cptr);
149   cli_handler(cptr) = SERVER_HANDLER;
150   Count_unknownbecomesserver(UserStats);
151
152   release_dns_reply(cptr);
153
154   SetBurst(cptr);
155
156 /*    nextping = CurrentTime; */
157
158   /*
159    * NOTE: check for acptr->user == cptr->serv->user is necessary to insure
160    * that we got the same one... bleah
161    */
162   if (cli_serv(cptr)->user && *(cli_serv(cptr))->by &&
163       (acptr = findNUser(cli_serv(cptr)->by))) {
164     if (cli_user(acptr) == cli_serv(cptr)->user) {
165       sendcmdto_one(&me, CMD_NOTICE, acptr, "%C :Link with %s established.",
166                     acptr, inpath);
167     }
168     else {
169       /*
170        * if not the same client, set by to empty string
171        */
172       acptr = 0;
173       *(cli_serv(cptr))->by = '\0';
174     }
175   }
176
177   sendto_opmask_butone(acptr, SNO_OLDSNO, "Link with %s established.", inpath);
178   cli_serv(cptr)->up = &me;
179   cli_serv(cptr)->updown = add_dlink(&(cli_serv(&me))->down, cptr);
180   sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s", cli_name(&me),
181                        cli_name(cptr));
182   SetJunction(cptr);
183   /*
184    * Old sendto_serv_but_one() call removed because we now
185    * need to send different names to different servers
186    * (domain name matching) Send new server to other servers.
187    */
188   for (i = 0; i <= HighestFd; i++)
189   {
190     if (!(acptr = LocalClientArray[i]) || !IsServer(acptr) ||
191         acptr == cptr || IsMe(acptr))
192       continue;
193     if (!match(cli_name(&me), cli_name(cptr)))
194       continue;
195     sendcmdto_one(&me, CMD_SERVER, acptr,
196                   "%s 2 0 %Tu J%02u %s%s +%s%s :%s", cli_name(cptr),
197                   cli_serv(cptr)->timestamp, Protocol(cptr), NumServCap(cptr),
198                   IsHub(cptr) ? "h" : "", IsService(cptr) ? "s" : "",
199                   cli_info(cptr));
200   }
201
202   /* Send these as early as possible so that glined users/juped servers can
203    * be removed from the network while the remote server is still chewing
204    * our burst.
205    */
206   gline_burst(cptr);
207   jupe_burst(cptr);
208
209   /*
210    * Pass on my client information to the new server
211    *
212    * First, pass only servers (idea is that if the link gets
213    * canceled because the server was already there,
214    * there are no NICK's to be canceled...). Of course,
215    * if cancellation occurs, all this info is sent anyway,
216    * and I guess the link dies when a read is attempted...? --msa
217    *
218    * Note: Link cancellation to occur at this point means
219    * that at least two servers from my fragment are building
220    * up connection this other fragment at the same time, it's
221    * a race condition, not the normal way of operation...
222    */
223
224   for (acptr = &me; acptr; acptr = cli_prev(acptr)) {
225     /* acptr->from == acptr for acptr == cptr */
226     if (cli_from(acptr) == cptr)
227       continue;
228     if (IsServer(acptr)) {
229       const char* protocol_str;
230
231       if (Protocol(acptr) > 9)
232         protocol_str = IsBurst(acptr) ? "J" : "P";
233       else
234         protocol_str = IsBurst(acptr) ? "J0" : "P0";
235
236       if (0 == match(cli_name(&me), cli_name(acptr)))
237         continue;
238       sendcmdto_one(cli_serv(acptr)->up, CMD_SERVER, cptr,
239                     "%s %d 0 %Tu %s%u %s%s +%s%s :%s", cli_name(acptr),
240                     cli_hopcount(acptr) + 1, cli_serv(acptr)->timestamp,
241                     protocol_str, Protocol(acptr), NumServCap(acptr),
242                     IsHub(acptr) ? "h" : "", IsService(acptr) ? "s" : "",
243                     cli_info(acptr));
244     }
245   }
246
247   for (acptr = &me; acptr; acptr = cli_prev(acptr))
248   {
249     /* acptr->from == acptr for acptr == cptr */
250     if (cli_from(acptr) == cptr)
251       continue;
252     if (IsUser(acptr))
253     {
254       char xxx_buf[25];
255       char *s = umode_str(acptr);
256       sendcmdto_one(cli_user(acptr)->server, CMD_NICK, cptr,
257                     "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
258                     cli_name(acptr), cli_hopcount(acptr) + 1, cli_lastnick(acptr),
259                     cli_user(acptr)->username, cli_user(acptr)->realhost,
260                     *s ? "+" : "", s, *s ? " " : "",
261                     iptobase64(xxx_buf, &cli_ip(acptr), sizeof(xxx_buf), IsIPv6(cptr)),
262                     NumNick(acptr), cli_info(acptr));
263     }
264   }
265   /*
266    * Last, send the BURST.
267    * (Or for 2.9 servers: pass all channels plus statuses)
268    */
269   {
270     struct Channel *chptr;
271     for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
272       send_channel_modes(cptr, chptr);
273   }
274   sendcmdto_one(&me, CMD_END_OF_BURST, cptr, "");
275   return 0;
276 }
277