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