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_features.h"
95 #include "ircd_log.h"
96 #include "ircd_reply.h"
97 #include "ircd_string.h"
98 #include "jupe.h"
99 #include "match.h"
100 #include "msg.h"
101 #include "numeric.h"
102 #include "numnicks.h"
103 #include "s_bsd.h"
104 #include "s_conf.h"
105 #include "s_user.h"
106 #include "send.h"
107
108 #include <assert.h>
109 #include <stdlib.h>
110
111 /*
112  * ms_connect - server message handler
113  * - Added by Jto 11 Feb 1989
114  *
115  *    parv[0] = sender prefix
116  *    parv[1] = servername
117  *    parv[2] = port number
118  *    parv[3] = remote server
119  */
120 int ms_connect(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
121 {
122   unsigned short   port;
123   unsigned short   tmpport;
124   const char*      rule;
125   struct ConfItem* aconf;
126   struct Client*   acptr;
127   struct Jupe*     ajupe;
128
129   assert(0 != cptr);
130   assert(0 != sptr);
131
132   if (!IsPrivileged(sptr))
133     return send_reply(sptr, ERR_NOPRIVILEGES);
134
135   if (parc < 4) {
136     /*
137      * this is coming from a server which should have already
138      * checked it's args, if we don't have parc == 4, something
139      * isn't right.
140      */
141     return need_more_params(sptr, "CONNECT");
142   }
143
144   if (hunt_server_cmd(sptr, CMD_CONNECT, cptr, 1, "%s %s :%C", 3, parc, parv)
145       != HUNTED_ISME)
146     return 0;
147
148   /*
149    * need to find the conf entry first so we can use the server name from
150    * the conf entry instead of parv[1] to find out if the server is already
151    * present below. --Bleep
152    */
153   if (0 == (aconf = conf_find_server(parv[1]))) {
154     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Host %s not listed "
155                   "in ircd.conf", sptr, parv[1]);
156     return 0;
157   }
158   /*
159    * use aconf->name to look up the server
160    */
161   if ((acptr = FindServer(aconf->name))) {
162     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Server %s already "
163                   "exists from %s", sptr, parv[1], cli_name(cli_from(acptr)));
164     return 0;
165   }
166   /*
167    * Evaluate connection rules...  If no rules found, allow the
168    * connect.   Otherwise stop with the first true rule (ie: rules
169    * are ored together.  Oper connects are effected only by D
170    * lines (CRULEALL) not d lines (CRULEAUTO).
171    */
172   if ((rule = conf_eval_crule(aconf->name, CRULE_ALL))) {
173     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Disallowed by rule: %s", sptr, rule);
174     return 0;
175   }
176   /*
177    * Check to see if the server is juped; if it is, disallow the connect
178    */
179   if ((ajupe = jupe_find(aconf->name)) && JupeIsActive(ajupe)) {
180     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Server %s is juped: %s",
181                   sptr, JupeServer(ajupe), JupeReason(ajupe));
182     return 0;
183   }
184
185   /*
186    * Allow opers to /connect foo.* 0 bah.* to connect foo and bah
187    * using the conf's configured port
188    */
189   port = atoi(parv[2]);
190   /*
191    * save the old port
192    */
193   tmpport = aconf->port;
194   if (port) { 
195     aconf->port = port;
196   }
197   else {
198     port = aconf->port;
199   }
200   /*
201    * Notify all operators about remote connect requests
202    */
203   sendcmdto_flag_butone(&me, CMD_WALLOPS, 0, FLAGS_WALLOP,
204                         ":Remote CONNECT %s %s from %s", parv[1],
205                         parv[2] ? parv[2] : "",
206                         get_client_name(sptr, HIDE_IP));
207   log_write(LS_NETWORK, L_INFO, 0, "CONNECT From %C : %s %s", sptr, parv[1],
208             parv[2] ? parv[2] : "");
209
210   if (connect_server(aconf, sptr, 0)) {
211     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connecting to %s.", sptr,
212                   aconf->name);
213   }
214   else {
215     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connection to %s failed",
216                   sptr, aconf->name);
217   }
218   aconf->port = tmpport;
219   return 0;
220 }
221
222 /*
223  * mo_connect - oper message handler
224  * - Added by Jto 11 Feb 1989
225  *
226  *    parv[0] = sender prefix
227  *    parv[1] = servername
228  *    parv[2] = port number
229  *    parv[3] = remote server
230  */
231 int mo_connect(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
232 {
233   unsigned short   port;
234   unsigned short   tmpport;
235   const char*      rule;
236   struct ConfItem* aconf;
237   struct Client*   acptr;
238   struct Jupe*     ajupe;
239
240   assert(0 != cptr);
241   assert(cptr == sptr);
242   assert(IsAnOper(sptr));
243
244   if (parc < 2)
245     return need_more_params(sptr, "CONNECT");
246
247   if (parc > 3) {
248     /*
249      * if parc > 3, we are trying to connect two remote
250      * servers to each other
251      */
252     if (IsLocOp(sptr)) {
253       /*
254        * Only allow LocOps to make local CONNECTS --SRB
255        */
256       return 0;
257     }
258     else {
259       struct Client* acptr2;
260       struct Client* acptr3;
261
262       if (!(acptr3 = find_match_server(parv[3]))) {
263         send_reply(sptr, ERR_NOSUCHSERVER, parv[3]);
264         return 0;
265       }
266
267       /*
268        * Look for closest matching server 
269        * needed for "/connect blah 4400 *"?
270        */
271       for (acptr2 = acptr3; acptr2 != &me; acptr2 = cli_serv(acptr2)->up) {
272         if (!match(parv[3], cli_name(acptr2)))
273           acptr3 = acptr2;
274       }
275       parv[3] = cli_name(acptr3);
276       if (hunt_server_cmd(sptr, CMD_CONNECT, cptr, 1, "%s %s :%C", 3, parc,
277                           parv) != HUNTED_ISME)
278         return 0;
279     }
280   }
281   /*
282    * need to find the conf entry first so we can use the server name from
283    * the conf entry instead of parv[1] to find out if the server is already
284    * present below. --Bleep
285    */
286   if (0 == (aconf = conf_find_server(parv[1]))) {
287     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Host %s not listed "
288                   "in ircd.conf", sptr, parv[1]);
289     return 0;
290   }
291   /*
292    * use aconf->name to look up the server, see above
293    */
294   if ((acptr = FindServer(aconf->name))) {
295     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Server %s already "
296                   "exists from %s", sptr, parv[1], cli_name(cli_from(acptr)));
297     return 0;
298   }
299   /*
300    * Evaluate connection rules...  If no rules found, allow the
301    * connect.   Otherwise stop with the first true rule (ie: rules
302    * are ored together.  Oper connects are effected only by D
303    * lines (CRULEALL) not d lines (CRULEAUTO).
304    */
305   if ((rule = conf_eval_crule(aconf->name, CRULE_ALL))) {
306     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Disallowed by rule: %s", sptr, rule);
307     return 0;
308   }
309   /*
310    * Check to see if the server is juped; if it is, disallow the connect
311    */
312   if ((ajupe = jupe_find(aconf->name)) && JupeIsActive(ajupe)) {
313     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Server %s is juped: %s",
314                   sptr, JupeServer(ajupe), JupeReason(ajupe));
315     return 0;
316   }
317   /*
318    *  Get port number from user, if given. If not specified,
319    *  use the default from configuration structure. If missing
320    *  from there, then use the precompiled default.
321    */
322   port = aconf->port;
323   if (parc > 2) {
324     assert(0 != parv[2]);
325     if (0 == (port = atoi(parv[2]))) {
326       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Invalid port number",
327                     sptr);
328       return 0;
329     }
330   }
331   if (0 == port && 0 == (port = feature_int(FEAT_SERVER_PORT))) {
332     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: missing port number",
333                   sptr);
334     return 0;
335   }
336
337   tmpport = aconf->port;
338   aconf->port = port;
339
340   if (connect_server(aconf, sptr, 0)) {
341     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connecting to %s.", sptr,
342                   aconf->name);
343   }
344   else {
345     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connection to %s failed",
346                   sptr, aconf->name);
347   }
348   aconf->port = tmpport;
349   return 0;
350 }