Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / m_connect.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_connect.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
26 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #if 0
83 /*
84  * No need to include handlers.h here the signatures must match
85  * and we don't need to force a rebuild of all the handlers everytime
86  * we add a new one to the list. --Bleep
87  */
88 #include "handlers.h"
89 #endif /* 0 */
90 #include "client.h"
91 #include "crule.h"
92 #include "hash.h"
93 #include "ircd.h"
94 #include "ircd_log.h"
95 #include "ircd_reply.h"
96 #include "ircd_string.h"
97 #include "jupe.h"
98 #include "match.h"
99 #include "msg.h"
100 #include "numeric.h"
101 #include "numnicks.h"
102 #include "s_bsd.h"
103 #include "s_conf.h"
104 #include "s_user.h"
105 #include "send.h"
106
107 #include <assert.h>
108 #include <stdlib.h>
109
110 /*
111  * ms_connect - server message handler
112  * - Added by Jto 11 Feb 1989
113  *
114  *    parv[0] = sender prefix
115  *    parv[1] = servername
116  *    parv[2] = port number
117  *    parv[3] = remote server
118  */
119 int ms_connect(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
120 {
121   unsigned short   port;
122   unsigned short   tmpport;
123   const char*      rule;
124   struct ConfItem* aconf;
125   struct Client*   acptr;
126   struct Jupe*     ajupe;
127
128   assert(0 != cptr);
129   assert(0 != sptr);
130
131   if (!IsPrivileged(sptr))
132     return send_reply(sptr, ERR_NOPRIVILEGES);
133
134   if (parc < 4) {
135     /*
136      * this is coming from a server which should have already
137      * checked it's args, if we don't have parc == 4, something
138      * isn't right.
139      */
140     return need_more_params(sptr, "CONNECT");
141   }
142
143   if (hunt_server_cmd(sptr, CMD_CONNECT, cptr, 1, "%s %s :%C", 3, parc, parv)
144       != HUNTED_ISME)
145     return 0;
146
147   /*
148    * need to find the conf entry first so we can use the server name from
149    * the conf entry instead of parv[1] to find out if the server is already
150    * present below. --Bleep
151    */
152   if (0 == (aconf = conf_find_server(parv[1]))) {
153     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Host %s not listed "
154                   "in ircd.conf", sptr, parv[1]);
155     return 0;
156   }
157   /*
158    * use aconf->name to look up the server
159    */
160   if ((acptr = FindServer(aconf->name))) {
161     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Server %s already "
162                   "exists from %s", sptr, parv[1], cli_name(cli_from(acptr)));
163     return 0;
164   }
165   /*
166    * Evaluate connection rules...  If no rules found, allow the
167    * connect.   Otherwise stop with the first true rule (ie: rules
168    * are ored together.  Oper connects are effected only by D
169    * lines (CRULEALL) not d lines (CRULEAUTO).
170    */
171   if ((rule = conf_eval_crule(aconf->name, CRULE_ALL))) {
172     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Disallowed by rule: %s", sptr, rule);
173     return 0;
174   }
175   /*
176    * Check to see if the server is juped; if it is, disallow the connect
177    */
178   if ((ajupe = jupe_find(aconf->name)) && JupeIsActive(ajupe)) {
179     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Server %s is juped: %s",
180                   sptr, JupeServer(ajupe), JupeReason(ajupe));
181     return 0;
182   }
183
184   /*
185    * Allow opers to /connect foo.* 0 bah.* to connect foo and bah
186    * using the conf's configured port
187    */
188   port = atoi(parv[2]);
189   /*
190    * save the old port
191    */
192   tmpport = aconf->port;
193   if (port) { 
194     aconf->port = port;
195   }
196   else {
197     port = aconf->port;
198   }
199   /*
200    * Notify all operators about remote connect requests
201    */
202   sendcmdto_flag_butone(&me, CMD_WALLOPS, 0, FLAGS_WALLOP,
203                         ":Remote CONNECT %s %s from %s", parv[1],
204                         parv[2] ? parv[2] : "",
205                         get_client_name(sptr, HIDE_IP));
206   log_write(LS_NETWORK, L_INFO, 0, "CONNECT From %C : %s %s", sptr, parv[1],
207             parv[2] ? parv[2] : "");
208
209   if (connect_server(aconf, sptr, 0)) {
210     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connecting to %s.", sptr,
211                   aconf->name);
212   }
213   else {
214     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connection to %s failed",
215                   sptr, aconf->name);
216   }
217   aconf->port = tmpport;
218   return 0;
219 }
220
221 /*
222  * mo_connect - oper message handler
223  * - Added by Jto 11 Feb 1989
224  *
225  *    parv[0] = sender prefix
226  *    parv[1] = servername
227  *    parv[2] = port number
228  *    parv[3] = remote server
229  */
230 int mo_connect(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
231 {
232   unsigned short   port;
233   unsigned short   tmpport;
234   const char*      rule;
235   struct ConfItem* aconf;
236   struct Client*   acptr;
237   struct Jupe*     ajupe;
238
239   assert(0 != cptr);
240   assert(cptr == sptr);
241   assert(IsAnOper(sptr));
242
243   if (parc < 2)
244     return need_more_params(sptr, "CONNECT");
245
246   if (parc > 3) {
247     /*
248      * if parc > 3, we are trying to connect two remote
249      * servers to each other
250      */
251     if (IsLocOp(sptr)) {
252       /*
253        * Only allow LocOps to make local CONNECTS --SRB
254        */
255       return 0;
256     }
257     else {
258       struct Client* acptr2;
259       struct Client* acptr3;
260
261       if (!(acptr3 = find_match_server(parv[3]))) {
262         send_reply(sptr, ERR_NOSUCHSERVER, parv[3]);
263         return 0;
264       }
265
266       /*
267        * Look for closest matching server 
268        * needed for "/connect blah 4400 *"?
269        */
270       for (acptr2 = acptr3; acptr2 != &me; acptr2 = cli_serv(acptr2)->up) {
271         if (!match(parv[3], cli_name(acptr2)))
272           acptr3 = acptr2;
273       }
274       parv[3] = cli_name(acptr3);
275       if (hunt_server_cmd(sptr, CMD_CONNECT, cptr, 1, "%s %s :%C", 3, parc,
276                           parv) != HUNTED_ISME)
277         return 0;
278     }
279   }
280   /*
281    * need to find the conf entry first so we can use the server name from
282    * the conf entry instead of parv[1] to find out if the server is already
283    * present below. --Bleep
284    */
285   if (0 == (aconf = conf_find_server(parv[1]))) {
286     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Host %s not listed "
287                   "in ircd.conf", sptr, parv[1]);
288     return 0;
289   }
290   /*
291    * use aconf->name to look up the server, see above
292    */
293   if ((acptr = FindServer(aconf->name))) {
294     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Server %s already "
295                   "exists from %s", sptr, parv[1], cli_name(cli_from(acptr)));
296     return 0;
297   }
298   /*
299    * Evaluate connection rules...  If no rules found, allow the
300    * connect.   Otherwise stop with the first true rule (ie: rules
301    * are ored together.  Oper connects are effected only by D
302    * lines (CRULEALL) not d lines (CRULEAUTO).
303    */
304   if ((rule = conf_eval_crule(aconf->name, CRULE_ALL))) {
305     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Disallowed by rule: %s", sptr, rule);
306     return 0;
307   }
308   /*
309    * Check to see if the server is juped; if it is, disallow the connect
310    */
311   if ((ajupe = jupe_find(aconf->name)) && JupeIsActive(ajupe)) {
312     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Server %s is juped: %s",
313                   sptr, JupeServer(ajupe), JupeReason(ajupe));
314     return 0;
315   }
316   /*
317    *  Get port number from user, if given. If not specified,
318    *  use the default from configuration structure. If missing
319    *  from there, then use the precompiled default.
320    */
321   port = aconf->port;
322   if (parc > 2) {
323     assert(0 != parv[2]);
324     if (0 == (port = atoi(parv[2]))) {
325       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Invalid port number",
326                     sptr);
327       return 0;
328     }
329   }
330   if (0 == port && 0 == (port = SERVER_PORT)) {
331     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: missing port number",
332                   sptr);
333     return 0;
334   }
335
336   tmpport = aconf->port;
337   aconf->port = port;
338
339   if (connect_server(aconf, sptr, 0)) {
340     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connecting to %s.", sptr,
341                   aconf->name);
342   }
343   else {
344     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connection to %s failed",
345                   sptr, aconf->name);
346   }
347   aconf->port = tmpport;
348   return 0;
349 }