ircu2.10.12 pk910 fork
[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: s_serv.c 1438 2005-06-28 00:42:06Z entrope $
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
138   det_confs_butmask(cptr, CONF_SERVER | CONF_UWORLD);
139
140   if (!IsHandshake(cptr))
141     hAddClient(cptr);
142   SetServer(cptr);
143   cli_handler(cptr) = SERVER_HANDLER;
144   Count_unknownbecomesserver(UserStats);
145   SetBurst(cptr);
146
147 /*    nextping = CurrentTime; */
148
149   /*
150    * NOTE: check for acptr->user == cptr->serv->user is necessary to insure
151    * that we got the same one... bleah
152    */
153   if (cli_serv(cptr)->user && *(cli_serv(cptr))->by &&
154       (acptr = findNUser(cli_serv(cptr)->by))) {
155     if (cli_user(acptr) == cli_serv(cptr)->user) {
156       sendcmdto_one(&me, CMD_NOTICE, acptr, "%C :Link with %s established.",
157                     acptr, inpath);
158     }
159     else {
160       /*
161        * if not the same client, set by to empty string
162        */
163       acptr = 0;
164       *(cli_serv(cptr))->by = '\0';
165     }
166   }
167
168   sendto_opmask_butone(acptr, SNO_OLDSNO, "Link with %s established.", inpath);
169   cli_serv(cptr)->up = &me;
170   cli_serv(cptr)->updown = add_dlink(&(cli_serv(&me))->down, cptr);
171   sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s", cli_name(&me),
172                        cli_name(cptr));
173   SetJunction(cptr);
174   /*
175    * Old sendto_serv_but_one() call removed because we now
176    * need to send different names to different servers
177    * (domain name matching) Send new server to other servers.
178    */
179   for (i = 0; i <= HighestFd; i++)
180   {
181     if (!(acptr = LocalClientArray[i]) || !IsServer(acptr) ||
182         acptr == cptr || IsMe(acptr))
183       continue;
184     if (!match(cli_name(&me), cli_name(cptr)))
185       continue;
186     sendcmdto_one(&me, CMD_SERVER, acptr,
187                   "%s 2 0 %Tu J%02u %s%s +%s%s%s :%s", cli_name(cptr),
188                   cli_serv(cptr)->timestamp, Protocol(cptr), NumServCap(cptr),
189                   IsHub(cptr) ? "h" : "", IsService(cptr) ? "s" : "",
190                   IsIPv6(cptr) ? "6" : "", cli_info(cptr));
191   }
192
193   /* Send these as early as possible so that glined users/juped servers can
194    * be removed from the network while the remote server is still chewing
195    * our burst.
196    */
197   gline_burst(cptr);
198   jupe_burst(cptr);
199
200   /*
201    * Pass on my client information to the new server
202    *
203    * First, pass only servers (idea is that if the link gets
204    * canceled because the server was already there,
205    * there are no NICK's to be canceled...). Of course,
206    * if cancellation occurs, all this info is sent anyway,
207    * and I guess the link dies when a read is attempted...? --msa
208    *
209    * Note: Link cancellation to occur at this point means
210    * that at least two servers from my fragment are building
211    * up connection this other fragment at the same time, it's
212    * a race condition, not the normal way of operation...
213    */
214
215   for (acptr = &me; acptr; acptr = cli_prev(acptr)) {
216     /* acptr->from == acptr for acptr == cptr */
217     if (cli_from(acptr) == cptr)
218       continue;
219     if (IsServer(acptr)) {
220       const char* protocol_str;
221
222       if (Protocol(acptr) > 9)
223         protocol_str = IsBurst(acptr) ? "J" : "P";
224       else
225         protocol_str = IsBurst(acptr) ? "J0" : "P0";
226
227       if (0 == match(cli_name(&me), cli_name(acptr)))
228         continue;
229       sendcmdto_one(cli_serv(acptr)->up, CMD_SERVER, cptr,
230                     "%s %d 0 %Tu %s%u %s%s +%s%s%s :%s", cli_name(acptr),
231                     cli_hopcount(acptr) + 1, cli_serv(acptr)->timestamp,
232                     protocol_str, Protocol(acptr), NumServCap(acptr),
233                     IsHub(acptr) ? "h" : "", IsService(acptr) ? "s" : "",
234                     IsIPv6(acptr) ? "6" : "", cli_info(acptr));
235     }
236   }
237
238   for (acptr = &me; acptr; acptr = cli_prev(acptr))
239   {
240     /* acptr->from == acptr for acptr == cptr */
241     if (cli_from(acptr) == cptr)
242       continue;
243     if (IsUser(acptr))
244     {
245       char xxx_buf[25];
246       char *s = umode_str(acptr);
247       sendcmdto_one(cli_user(acptr)->server, CMD_NICK, cptr,
248                     "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
249                     cli_name(acptr), cli_hopcount(acptr) + 1, cli_lastnick(acptr),
250                     cli_user(acptr)->username, cli_user(acptr)->realhost,
251                     *s ? "+" : "", s, *s ? " " : "",
252                     iptobase64(xxx_buf, &cli_ip(acptr), sizeof(xxx_buf), IsIPv6(cptr)),
253                     NumNick(acptr), cli_info(acptr));
254       if(feature_bool(FEAT_AWAY_BURST) && cli_user(acptr)->away)
255         sendcmdto_one(acptr, CMD_AWAY, cptr, ":%s", cli_user(acptr)->away);
256     }
257   }
258   /*
259    * Last, send the BURST.
260    * (Or for 2.9 servers: pass all channels plus statuses)
261    */
262   {
263     struct Channel *chptr;
264     for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
265       send_channel_modes(cptr, chptr);
266   }
267   sendcmdto_one(&me, CMD_END_OF_BURST, cptr, "");
268   return 0;
269 }
270