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