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