Author: Bleep <helveytw@home.com>
[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 "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 #if 0
63 int exit_new_server(struct Client* cptr, struct Client* sptr,
64                     const char* host, time_t timestamp, const 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); /* XXX DEAD */
76   va_end(vl);
77   MyFree(buf);
78   return 0;
79 }
80 #endif /* 0 */
81
82 int exit_new_server(struct Client *cptr, struct Client *sptr, const char *host,
83                     time_t timestamp, const char *pattern, ...)
84 {
85   struct VarData vd;
86   int retval = 0;
87
88   vd.vd_format = pattern;
89   va_start(vd.vd_args, pattern);
90
91   if (!IsServer(sptr))
92     retval = vexit_client_msg(cptr, cptr, &me, pattern, vd.vd_args);
93   else
94     sendcmdto_one(&me, CMD_SQUIT, cptr, "%s %Tu :%v", host, timestamp, &vd);
95
96   va_end(vd.vd_args);
97
98   return retval;
99 }
100
101 int a_kills_b_too(struct Client *a, struct Client *b)
102 {
103   for (; b != a && b != &me; b = b->serv->up);
104   return (a == b ? 1 : 0);
105 }
106
107 /*
108  * server_estab
109  *
110  * May only be called after a SERVER was received from cptr,
111  * and thus make_server was called, and serv->prot set. --Run
112  */
113 int server_estab(struct Client *cptr, struct ConfItem *aconf,
114                  struct Jupe *ajupe)
115 {
116   struct Client* acptr = 0;
117   const char*    inpath;
118   int split,     i;
119
120   assert(0 != cptr);
121   assert(0 != cptr->local);
122
123   split = (0 != ircd_strcmp(cptr->name, cptr->sockhost)
124       &&   0 != ircd_strncmp(cptr->info, "JUPE", 4));
125   inpath = cptr->name;
126
127   if (IsUnknown(cptr)) {
128     if (aconf->passwd[0])
129       sendrawto_one(cptr, MSG_PASS " :%s", aconf->passwd);
130     /*
131      *  Pass my info to the new server
132      */
133     sendrawto_one(cptr, MSG_SERVER " %s 1 %Tu %Tu J%s %s%s :%s", me.name,
134                   me.serv->timestamp, cptr->serv->timestamp, MAJOR_PROTOCOL,
135                   NumServCap(&me), *me.info ? me.info : "IRCers United");
136   }
137
138   det_confs_butmask(cptr, CONF_LEAF | CONF_HUB | CONF_SERVER | CONF_UWORLD);
139
140   if (!IsHandshake(cptr))
141     hAddClient(cptr);
142   SetServer(cptr);
143   cptr->handler = SERVER_HANDLER;
144   Count_unknownbecomesserver(UserStats);
145
146   release_dns_reply(cptr);
147
148   SetBurst(cptr);
149
150   nextping = CurrentTime;
151
152   /*
153    * NOTE: check for acptr->user == cptr->serv->user is necessary to insure
154    * that we got the same one... bleah
155    */
156   if (cptr->serv->user && *cptr->serv->by &&
157       (acptr = findNUser(cptr->serv->by))) {
158     if (acptr->user == cptr->serv->user) {
159       sendcmdto_one(&me, CMD_NOTICE, acptr, "%C :Link with %s established.",
160                     acptr, inpath);
161     }
162     else {
163       /*
164        * if not the same client, set by to empty string
165        */
166       acptr = 0;
167       *cptr->serv->by = '\0';
168     }
169   }
170
171   sendto_opmask_butone(acptr, SNO_OLDSNO, "Link with %s established.", inpath);
172   cptr->serv->up = &me;
173   cptr->serv->updown = add_dlink(&me.serv->down, cptr);
174   sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s", me.name,
175                        cptr->name);
176   SetJunction(cptr);
177   /*
178    * Old sendto_serv_but_one() call removed because we now
179    * need to send different names to different servers
180    * (domain name matching) Send new server to other servers.
181    */
182   for (i = 0; i <= HighestFd; i++)
183   {
184     if (!(acptr = LocalClientArray[i]) || !IsServer(acptr) ||
185         acptr == cptr || IsMe(acptr))
186       continue;
187     if (!match(me.name, cptr->name))
188       continue;
189     if (ajupe)
190       sendcmdto_one(&me, CMD_SERVER, acptr,
191                     "%s 2 0 %Tu J%02u %s%s 0 %%%Tu :%s", cptr->name,
192                     cptr->serv->timestamp, Protocol(cptr), NumServCap(cptr),
193                     JupeLastMod(ajupe), cptr->info);
194     else
195       sendcmdto_one(&me, CMD_SERVER, acptr,
196                     "%s 2 0 %Tu J%02u %s%s 0 :%s", cptr->name,
197                     cptr->serv->timestamp, Protocol(cptr), NumServCap(cptr),
198                     cptr->info);
199   }
200
201   /*
202    * Pass on my client information to the new server
203    *
204    * First, pass only servers (idea is that if the link gets
205    * cancelled beacause the server was already there,
206    * there are no NICK's to be cancelled...). Of course,
207    * if cancellation occurs, all this info is sent anyway,
208    * and I guess the link dies when a read is attempted...? --msa
209    *
210    * Note: Link cancellation to occur at this point means
211    * that at least two servers from my fragment are building
212    * up connection this other fragment at the same time, it's
213    * a race condition, not the normal way of operation...
214    */
215
216   for (acptr = &me; acptr; acptr = acptr->prev) {
217     /* acptr->from == acptr for acptr == cptr */
218     if (acptr->from == cptr)
219       continue;
220     if (IsServer(acptr)) {
221       const char* protocol_str;
222
223       if (Protocol(acptr) > 9)
224         protocol_str = IsBurst(acptr) ? "J" : "P";
225       else
226         protocol_str = IsBurst(acptr) ? "J0" : "P0";
227
228       if (0 == match(me.name, acptr->name))
229         continue;
230       split = (MyConnect(acptr) && 
231                0 != ircd_strcmp(acptr->name, acptr->sockhost) &&
232                0 != ircd_strncmp(acptr->info, "JUPE", 4));
233       if ((ajupe = jupe_find(acptr->name)) && !JupeIsLocal(ajupe))
234         sendcmdto_one(acptr->serv->up, CMD_SERVER, cptr,
235                       "%s %d 0 %Tu %s%u %s%s 0 %%%Tu :%s", acptr->name,
236                       acptr->hopcount + 1, acptr->serv->timestamp,
237                       protocol_str, Protocol(acptr), NumServCap(acptr),
238                       JupeLastMod(ajupe), acptr->info);
239       else
240         sendcmdto_one(acptr->serv->up, CMD_SERVER, cptr,
241                       "%s %d 0 %Tu %s%u %s%s 0 :%s", acptr->name,
242                       acptr->hopcount + 1, acptr->serv->timestamp,
243                       protocol_str, Protocol(acptr), NumServCap(acptr),
244                       acptr->info);
245     }
246   }
247
248   for (acptr = &me; acptr; acptr = acptr->prev)
249   {
250     /* acptr->from == acptr for acptr == cptr */
251     if (acptr->from == cptr)
252       continue;
253     if (IsUser(acptr))
254     {
255       char xxx_buf[8];
256       char *s = umode_str(acptr);
257       struct Gline *agline = 0;
258       if ((agline = gline_lookup(acptr, GLINE_GLOBAL | GLINE_LASTMOD)))
259         sendcmdto_one(acptr->user->server, CMD_NICK, cptr,
260                       "%s %d %Tu %s %s %s%s%s%%%Tu:%s@%s %s %s%s :%s",
261                       acptr->name, acptr->hopcount + 1, acptr->lastnick,
262                       acptr->user->username, acptr->user->host,
263                       *s ? "+" : "", s, *s ? " " : "",
264                       GlineLastMod(agline), GlineUser(agline),
265                       GlineHost(agline),
266                       inttobase64(xxx_buf, ntohl(acptr->ip.s_addr), 6),
267                       NumNick(acptr), acptr->info);
268       else
269         sendcmdto_one(acptr->user->server, CMD_NICK, cptr,
270                       "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
271                       acptr->name, acptr->hopcount + 1, acptr->lastnick,
272                       acptr->user->username, acptr->user->host,
273                       *s ? "+" : "", s, *s ? " " : "",
274                       inttobase64(xxx_buf, ntohl(acptr->ip.s_addr), 6),
275                       NumNick(acptr), acptr->info);
276     }
277   }
278   /*
279    * Last, send the BURST.
280    * (Or for 2.9 servers: pass all channels plus statuses)
281    */
282   {
283     struct Channel *chptr;
284     for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
285       send_channel_modes(cptr, chptr);
286   }
287   jupe_burst(cptr);
288   gline_burst(cptr);
289   sendcmdto_one(&me, CMD_END_OF_BURST, cptr, "");
290   return 0;
291 }
292