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