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