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 "s_serv.h"
26 #include "IPcheck.h"
27 #include "channel.h"
28 #include "client.h"
29 #include "crule.h"
30 #include "gline.h"
31 #include "hash.h"
32 #include "ircd.h"
33 #include "ircd_alloc.h"
34 #include "ircd_reply.h"
35 #include "ircd_string.h"
36 #include "ircd_xopen.h"
37 #include "jupe.h"
38 #include "list.h"
39 #include "msg.h"
40 #include "match.h"
41 #include "numeric.h"
42 #include "numnicks.h"
43 #include "parse.h"
44 #include "querycmds.h"
45 #include "s_bsd.h"
46 #include "s_conf.h"
47 #include "s_debug.h"
48 #include "s_misc.h"
49 #include "s_user.h"
50 #include "send.h"
51 #include "sprintf_irc.h"
52 #include "struct.h"
53 #include "sys.h"
54 #include "userload.h"
55
56 #include <assert.h>
57 #include <stdlib.h>
58 #include <string.h>
59
60 unsigned int max_connection_count = 0;
61 unsigned int max_client_count = 0;
62
63 int exit_new_server(struct Client *cptr, struct Client *sptr,
64     char *host, time_t timestamp, char *fmt, ...)
65 {
66   va_list vl;
67   char *buf =
68       (char*) MyMalloc(strlen(me.name) + strlen(host) + 22 + strlen(fmt));
69   assert(0 != buf);
70   va_start(vl, fmt);
71   if (!IsServer(sptr))
72     return vexit_client_msg(cptr, cptr, &me, fmt, vl);
73   sprintf_irc(buf, ":%s " TOK_SQUIT " %s " TIME_T_FMT " :", me.name, host, timestamp);
74   strcat(buf, fmt);
75   vsendto_one(cptr, buf, vl);
76   va_end(vl);
77   MyFree(buf);
78   return 0;
79 }
80
81 int a_kills_b_too(struct Client *a, struct Client *b)
82 {
83   for (; b != a && b != &me; b = b->serv->up);
84   return (a == b ? 1 : 0);
85 }
86
87 /*
88  * server_estab
89  *
90  * May only be called after a SERVER was received from cptr,
91  * and thus make_server was called, and serv->prot set. --Run
92  */
93 int server_estab(struct Client *cptr, struct ConfItem *aconf)
94 {
95   struct Client* acptr = 0;
96   const char*    inpath;
97   int split,     i;
98
99   assert(0 != cptr);
100   assert(0 != cptr->local);
101
102   split = (0 != ircd_strcmp(cptr->name, cptr->sockhost)
103       &&   0 != ircd_strncmp(cptr->info, "JUPE", 4));
104   inpath = cptr->name;
105
106   if (IsUnknown(cptr)) {
107     if (aconf->passwd[0])
108       sendto_one(cptr, "PASS :%s", aconf->passwd);
109     /*
110      *  Pass my info to the new server
111      */
112     sendto_one(cptr, "SERVER %s 1 " TIME_T_FMT " " TIME_T_FMT " J%s %s%s :%s",
113         me.name, me.serv->timestamp, cptr->serv->timestamp,
114         MAJOR_PROTOCOL, NumServCap(&me),
115         (me.info[0]) ? (me.info) : "IRCers United");
116     /*
117      * Don't charge this IP# for connecting
118      * XXX - if this comes from a server port, it will not have been added
119      * to the IP check registry, see add_connection in s_bsd.c 
120      */
121     IPcheck_connect_fail(cptr->ip);
122   }
123
124   det_confs_butmask(cptr, CONF_LEAF | CONF_HUB | CONF_SERVER | CONF_UWORLD);
125
126   if (!IsHandshake(cptr))
127     hAddClient(cptr);
128   SetServer(cptr);
129   cptr->handler = SERVER_HANDLER;
130   Count_unknownbecomesserver(UserStats);
131
132   release_dns_reply(cptr);
133
134   SetBurst(cptr);
135
136   nextping = CurrentTime;
137
138   /*
139    * NOTE: check for acptr->user == cptr->serv->user is necessary to insure
140    * that we got the same one... bleah
141    */
142   if (cptr->serv->user && *cptr->serv->by &&
143       (acptr = findNUser(cptr->serv->by))) {
144     if (acptr->user == cptr->serv->user) {
145       if (MyUser(acptr))
146         sendto_one(acptr, ":%s NOTICE %s :Link with %s established.",
147                    me.name, acptr->name, inpath);
148       else
149         sendto_one(acptr, "%s NOTICE %s%s :Link with %s established.",
150                    NumServ(&me), NumNick(acptr), inpath);
151     }
152     else {
153       /*
154        * if not the same client, set by to empty string
155        */
156       acptr = 0;
157       *cptr->serv->by = '\0';
158     }
159   }
160
161   sendto_lops_butone(acptr, "Link with %s established.", inpath);
162   cptr->serv->up = &me;
163   cptr->serv->updown = add_dlink(&me.serv->down, cptr);
164   sendto_op_mask(SNO_NETWORK, "Net junction: %s %s", me.name, cptr->name);
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(me.name, cptr->name))
177       continue;
178     if (split)
179     {
180         sendto_one(acptr, "%s " TOK_SERVER " %s 2 0 " TIME_T_FMT " %s%u %s%s 0 :%s",
181             NumServ(&me), cptr->name, cptr->serv->timestamp,
182             (Protocol(cptr) > 9) ? "J" : "J0", Protocol(cptr),
183             NumServCap(cptr), cptr->info);
184     }
185     else
186     {
187         sendto_one(acptr, "%s " TOK_SERVER " %s 2 0 " TIME_T_FMT " %s%u %s%s 0 :%s",
188             NumServ(&me), cptr->name, cptr->serv->timestamp,
189             (Protocol(cptr) > 9) ? "J" : "J0", Protocol(cptr),
190             NumServCap(cptr), cptr->info);
191     }
192   }
193
194   /*
195    * Pass on my client information to the new server
196    *
197    * First, pass only servers (idea is that if the link gets
198    * cancelled beacause the server was already there,
199    * there are no NICK's to be cancelled...). Of course,
200    * if cancellation occurs, all this info is sent anyway,
201    * and I guess the link dies when a read is attempted...? --msa
202    *
203    * Note: Link cancellation to occur at this point means
204    * that at least two servers from my fragment are building
205    * up connection this other fragment at the same time, it's
206    * a race condition, not the normal way of operation...
207    */
208
209   for (acptr = &me; acptr; acptr = acptr->prev) {
210     /* acptr->from == acptr for acptr == cptr */
211     if (acptr->from == cptr)
212       continue;
213     if (IsServer(acptr)) {
214       const char* protocol_str;
215
216       if (Protocol(acptr) > 9)
217         protocol_str = IsBurst(acptr) ? "J" : "P";
218       else
219         protocol_str = IsBurst(acptr) ? "J0" : "P0";
220
221       if (0 == match(me.name, acptr->name))
222         continue;
223       split = (MyConnect(acptr) && 
224                0 != ircd_strcmp(acptr->name, acptr->sockhost) &&
225                0 != ircd_strncmp(acptr->info, "JUPE", 4));
226       if (split)
227       {
228           sendto_one(cptr,
229               "%s " TOK_SERVER " %s %d 0 " TIME_T_FMT " %s%u %s%s 0 :%s",
230               NumServ(acptr->serv->up), acptr->name,
231               acptr->hopcount + 1, acptr->serv->timestamp,
232               protocol_str, Protocol(acptr),
233               NumServCap(acptr), acptr->info);
234       }
235       else
236       {
237           sendto_one(cptr,
238               "%s " TOK_SERVER " %s %d 0 " TIME_T_FMT " %s%u %s%s 0 :%s",
239               NumServ(acptr->serv->up), acptr->name,
240               acptr->hopcount + 1, acptr->serv->timestamp,
241               protocol_str, Protocol(acptr), 
242               NumServCap(acptr), acptr->info);
243       }
244     }
245   }
246
247   for (acptr = &me; acptr; acptr = acptr->prev)
248   {
249     /* acptr->from == acptr for acptr == cptr */
250     if (acptr->from == cptr)
251       continue;
252     if (IsUser(acptr))
253     {
254       char xxx_buf[8];
255       char *s = umode_str(acptr);
256       sendto_one(cptr, *s ?
257             "%s N %s %d " TIME_T_FMT " %s %s +%s %s %s%s :%s" :
258             "%s N %s %d " TIME_T_FMT " %s %s %s%s %s%s :%s",
259             NumServ(acptr->user->server),
260             acptr->name, acptr->hopcount + 1, acptr->lastnick,
261             acptr->user->username, acptr->user->host,
262             s, inttobase64(xxx_buf, ntohl(acptr->ip.s_addr), 6),
263             NumNick(acptr), acptr->info);
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   sendto_one(cptr, "%s EB", NumServ(&me));
278   return 0;
279 }
280