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