d30151d6e3dabb110a4a295be59cbda700ff50b0
[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], acptr->from->name);
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    * Get port number from params, port must be non-zero if it comes from a
185    * server.
186    */
187   if ((port = atoi(parv[2])) == 0) {
188     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Invalid port number",
189                   sptr);
190     return 0;
191   }
192   /*
193    * save the old port
194    */
195   tmpport = aconf->port;
196   aconf->port = port;
197   /*
198    * Notify all operators about remote connect requests
199    */
200   sendcmdto_flag_butone(&me, CMD_WALLOPS, 0, FLAGS_WALLOP,
201                         ":Remote CONNECT %s %s from %s", parv[1],
202                         parv[2] ? parv[2] : "",
203                         get_client_name(sptr, HIDE_IP));
204   ircd_log(L_INFO, "CONNECT From %s : %s %d", parv[0], parv[1],
205            parv[2] ? parv[2] : "");
206
207   if (connect_server(aconf, sptr, 0)) {
208     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connecting to %s.", sptr,
209                   aconf->name);
210   }
211   else {
212     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connection to %s failed",
213                   sptr, aconf->name);
214   }
215   aconf->port = tmpport;
216   return 0;
217 }
218
219 /*
220  * mo_connect - oper message handler
221  * - Added by Jto 11 Feb 1989
222  *
223  *    parv[0] = sender prefix
224  *    parv[1] = servername
225  *    parv[2] = port number
226  *    parv[3] = remote server
227  */
228 int mo_connect(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
229 {
230   unsigned short   port;
231   unsigned short   tmpport;
232   const char*      rule;
233   struct ConfItem* aconf;
234   struct Client*   acptr;
235   struct Jupe*     ajupe;
236
237   assert(0 != cptr);
238   assert(cptr == sptr);
239   assert(IsAnOper(sptr));
240
241   if (parc < 2)
242     return need_more_params(sptr, "CONNECT");
243
244   if (parc > 3) {
245     /*
246      * if parc > 3, we are trying to connect two remote
247      * servers to each other
248      */
249     if (IsLocOp(sptr)) {
250       /*
251        * Only allow LocOps to make local CONNECTS --SRB
252        */
253       return 0;
254     }
255     else {
256       struct Client* acptr2;
257       struct Client* acptr3;
258
259       if (!(acptr3 = find_match_server(parv[3]))) {
260         send_reply(sptr, ERR_NOSUCHSERVER, parv[3]);
261         return 0;
262       }
263
264       /*
265        * Look for closest matching server 
266        * needed for "/connect blah 4400 *"?
267        */
268       for (acptr2 = acptr3; acptr2 != &me; acptr2 = acptr2->serv->up) {
269         if (!match(parv[3], acptr2->name))
270           acptr3 = acptr2;
271       }
272       parv[3] = acptr3->name;
273       if (hunt_server_cmd(sptr, CMD_CONNECT, cptr, 1, "%s %s :%C", 3, parc,
274                           parv) != HUNTED_ISME)
275         return 0;
276     }
277   }
278   /*
279    * need to find the conf entry first so we can use the server name from
280    * the conf entry instead of parv[1] to find out if the server is already
281    * present below. --Bleep
282    */
283   if (0 == (aconf = conf_find_server(parv[1]))) {
284     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Host %s not listed "
285                   "in ircd.conf", sptr, parv[1]);
286     return 0;
287   }
288   /*
289    * use aconf->name to look up the server, see above
290    */
291   if ((acptr = FindServer(aconf->name))) {
292     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Server %s already "
293                   "exists from %s", sptr, parv[1], acptr->from->name);
294     return 0;
295   }
296   /*
297    * Evaluate connection rules...  If no rules found, allow the
298    * connect.   Otherwise stop with the first true rule (ie: rules
299    * are ored together.  Oper connects are effected only by D
300    * lines (CRULEALL) not d lines (CRULEAUTO).
301    */
302   if ((rule = conf_eval_crule(aconf->name, CRULE_ALL))) {
303     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Disallowed by rule: %s", sptr, rule);
304     return 0;
305   }
306   /*
307    * Check to see if the server is juped; if it is, disallow the connect
308    */
309   if ((ajupe = jupe_find(aconf->name)) && JupeIsActive(ajupe)) {
310     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Server %s is juped: %s",
311                   sptr, JupeServer(ajupe), JupeReason(ajupe));
312     return 0;
313   }
314   /*
315    *  Get port number from user, if given. If not specified,
316    *  use the default from configuration structure. If missing
317    *  from there, then use the precompiled default.
318    */
319   port = aconf->port;
320   if (parc > 2) {
321     assert(0 != parv[2]);
322     if (0 == (port = atoi(parv[2]))) {
323       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Invalid port number",
324                     sptr);
325       return 0;
326     }
327   }
328   if (0 == port && 0 == (port = SERVER_PORT)) {
329     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: missing port number",
330                   sptr);
331     return 0;
332   }
333
334   tmpport = aconf->port;
335   aconf->port = port;
336
337   if (connect_server(aconf, sptr, 0)) {
338     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connecting to %s.", sptr,
339                   aconf->name);
340   }
341   else {
342     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connection to %s failed",
343                   sptr, aconf->name);
344   }
345   aconf->port = tmpport;
346   return 0;
347 }
348
349   
350 #if 0
351 /*
352  * XXX - remove when regression testing complete
353  *
354  *  m_connect                           - Added by Jto 11 Feb 1989
355  *
356  *    parv[0] = sender prefix
357  *    parv[1] = servername
358  *    parv[2] = port number
359  *    parv[3] = remote server
360  */
361 int m_connect(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
362 {
363   unsigned short   port;
364   unsigned short   tmpport;
365   struct ConfItem* aconf;
366   struct ConfItem* cconf;
367   struct Client*   acptr;
368   struct Jupe*     ajupe;
369   const char*      rule;
370
371   if (!IsPrivileged(sptr)) {
372     sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, parv[0]); /* XXX DEAD */
373     return -1;
374   }
375
376   if (IsLocOp(sptr) && parc > 3)        /* Only allow LocOps to make */
377     return 0;                   /* local CONNECTS --SRB      */
378
379   if (parc > 3 && MyUser(sptr)) {
380     struct Client* acptr2;
381     struct Client* acptr3;
382     if (!(acptr3 = find_match_server(parv[3]))) {
383       sendto_one(sptr, err_str(ERR_NOSUCHSERVER), me.name, parv[0], parv[3]); /* XXX DEAD */
384       return 0;
385     }
386
387     /* Look for closest matching server */
388     for (acptr2 = acptr3; acptr2 != &me; acptr2 = acptr2->serv->up)
389       if (!match(parv[3], acptr2->name))
390         acptr3 = acptr2;
391
392     parv[3] = acptr3->name;
393   }
394
395   if (hunt_server(1, cptr, sptr, /* XXX DEAD */
396                   "%s%s " TOK_CONNECT " %s %s :%s", 3, parc, parv) != HUNTED_ISME)
397     return 0;
398
399   if (parc < 2 || *parv[1] == '\0') {
400     return need_more_params(sptr, "CONNECT");
401 #if 0
402     return -1;
403 #endif
404   }
405
406   if ((acptr = FindServer(parv[1]))) {
407     if (MyUser(sptr))
408       sendto_one(sptr, ":%s NOTICE %s :Connect: Server %s %s %s.", /* XXX DEAD */
409           me.name, parv[0], parv[1], "already exists from", acptr->from->name);
410     else
411       sendto_one(sptr, "%s NOTICE %s%s :Connect: Server %s %s %s.", /* XXX DEAD */
412           NumServ(&me), NumNick(sptr), parv[1], "already exists from",
413           acptr->from->name);
414     return 0;
415   }
416
417   for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
418     if (CONF_SERVER == aconf->status && 0 == match(parv[1], aconf->name))
419       break;
420   }
421 #if 0
422   /*
423    * Checked first servernames, then try hostnames.
424    */
425   if (!aconf) {
426     for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
427       if (CONF_SERVER == aconf->status && 0 == match(parv[1], aconf->host))
428         break;
429     }
430   }
431 #endif
432   if (!aconf) {
433     if (MyUser(sptr))
434       sendto_one(sptr, ":%s NOTICE %s :Connect: Host %s not listed in ircd.conf", /* XXX DEAD */
435                  me.name, parv[0], parv[1]);
436     else
437       sendto_one(sptr, "%s NOTICE %s%s :Connect: Host %s not listed in ircd.conf", /* XXX DEAD */
438                  NumServ(&me), NumNick(sptr), parv[1]);
439     return 0;
440   }
441   /*
442    *  Get port number from user, if given. If not specified,
443    *  use the default from configuration structure. If missing
444    *  from there, then use the precompiled default.
445    */
446   tmpport = port = aconf->port;
447   if (parc > 2 && !BadPtr(parv[2])) {
448     if ((port = atoi(parv[2])) == 0) {
449       if (MyUser(sptr))
450         sendto_one(sptr, ":%s NOTICE %s :Connect: Invalid port number", me.name, parv[0]); /* XXX DEAD */
451       else
452         sendto_one(sptr, "%s NOTICE %s%s :Connect: Invalid port number", /* XXX DEAD */
453                    NumServ(&me), NumNick(sptr));
454       return 0;
455     }
456   }
457   else if (port == 0 && (port = PORTNUM) == 0) {
458     if (MyUser(sptr))
459       sendto_one(sptr, ":%s NOTICE %s :Connect: missing port number", /* XXX DEAD */
460                  me.name, parv[0]);
461     else
462       sendto_one(sptr, "%s NOTICE %s%s :Connect: missing port number", /* XXX DEAD */
463                  NumServ(&me), NumNick(sptr));
464     return 0;
465   }
466
467   /*
468    * Evaluate connection rules...  If no rules found, allow the
469    * connect.   Otherwise stop with the first true rule (ie: rules
470    * are ored together.  Oper connects are effected only by D
471    * lines (CRULEALL) not d lines (CRULEAUTO).
472    */
473   if ((rule = conf_eval_crule(aconf->name, CRULE_ALL))) {
474     if (MyUser(sptr))
475        sendto_one(sptr, ":%s NOTICE %s :Connect: Disallowed by rule: %s", /* XXX DEAD */
476                    me.name, parv[0], cconf->name);
477     else
478       sendto_one(sptr, "%s NOTICE %s%s :Connect: Disallowed by rule: %s", /* XXX DEAD */
479                    NumServ(&me), NumNick(sptr), cconf->name);
480     return 0;
481   }
482   /*
483    * Check to see if the server is juped; if it is, disallow the connect
484    */
485   if ((ajupe = jupe_find(aconf->name)) && JupeIsActive(ajupe)) {
486     sendto_one(sptr, "%s NOTICE %s%s :Connect: Server %s is juped: %s", /* XXX DEAD */
487                NumServ(&me), NumNick(sptr), JupeServer(ajupe),
488                JupeReason(ajupe));
489     return 0;
490   }
491
492   /*
493    * Notify all operators about remote connect requests
494    */
495   if (!IsAnOper(cptr)) {
496     sendto_ops_butone(0, &me, ":%s WALLOPS :Remote CONNECT %s %s from %s", /* XXX DEAD */
497                       me.name, parv[1], parv[2] ? parv[2] : "",
498                       get_client_name(sptr, HIDE_IP));
499     ircd_log(L_INFO, "CONNECT From %s : %s %d",
500              parv[0], parv[1], parv[2] ? parv[2] : "");
501   }
502   aconf->port = port;
503   if (connect_server(aconf, sptr, 0)) {
504     if (MyUser(sptr))
505       sendto_one(sptr, ":%s NOTICE %s :*** Connecting to %s.", /* XXX DEAD */
506                  me.name, parv[0], aconf->name);
507     else
508       sendto_one(sptr, "%s NOTICE %s%s :*** Connecting to %s.", /* XXX DEAD */
509                  NumServ(&me), NumNick(sptr), aconf->name);
510   }
511   else {
512     if (MyUser(sptr))
513       sendto_one(sptr, ":%s NOTICE %s :*** Connection to %s failed", /* XXX DEAD */
514                  me.name, parv[0], aconf->name);
515     else
516       sendto_one(sptr, "%s NOTICE %s%s :*** Connection to %s failed", /* XXX DEAD */
517                  NumServ(&me), NumNick(sptr), aconf->name);
518   }
519   aconf->port = tmpport;
520   return 0;
521 }
522 #endif /* 0 */
523