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