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