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