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