2 * IRC - Internet Relay Chat, ircd/m_connect.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
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)
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.
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.
27 * m_functions execute protocol messages on this server:
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...).
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.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
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 ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
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*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
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
95 #include "ircd_reply.h"
96 #include "ircd_string.h"
101 #include "numnicks.h"
111 * ms_connect - server message handler
112 * - Added by Jto 11 Feb 1989
114 * parv[0] = sender prefix
115 * parv[1] = servername
116 * parv[2] = port number
117 * parv[3] = remote server
119 int ms_connect(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
122 unsigned short tmpport;
123 const char* crule_name;
124 struct ConfItem* aconf;
125 struct Client* acptr;
131 if (!IsPrivileged(sptr))
132 return send_reply(sptr, ERR_NOPRIVILEGES);
136 * this is coming from a server which should have already
137 * checked it's args, if we don't have parc == 4, something
140 return need_more_params(sptr, "CONNECT");
143 if (hunt_server_cmd(sptr, CMD_CONNECT, cptr, 1, "%s %s :%C", 3, parc, parv)
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
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]);
158 * use aconf->name to look up the server
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);
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).
171 if ((crule_name = conf_eval_crule(aconf))) {
172 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Disallowed by rule: %s",
177 * Check to see if the server is juped; if it is, disallow the connect
179 if ((ajupe = jupe_find(aconf->name)) && JupeIsActive(ajupe)) {
180 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Server %s is juped: %s",
181 sptr, JupeServer(ajupe), JupeReason(ajupe));
185 * Get port number from params, port must be non-zero if it comes from a
188 if ((port = atoi(parv[2])) == 0) {
189 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Invalid port number",
196 tmpport = aconf->port;
199 * Notify all operators about remote connect requests
201 sendcmdto_flag_butone(&me, CMD_WALLOPS, 0, FLAGS_WALLOP,
202 ":Remote CONNECT %s %s from %s", parv[1],
203 parv[2] ? parv[2] : "",
204 get_client_name(sptr, HIDE_IP));
205 ircd_log(L_INFO, "CONNECT From %s : %s %d", parv[0], parv[1],
206 parv[2] ? parv[2] : "");
208 if (connect_server(aconf, sptr, 0)) {
209 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connecting to %s.", sptr,
213 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connection to %s failed",
216 aconf->port = tmpport;
221 * mo_connect - oper message handler
222 * - Added by Jto 11 Feb 1989
224 * parv[0] = sender prefix
225 * parv[1] = servername
226 * parv[2] = port number
227 * parv[3] = remote server
229 int mo_connect(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
232 unsigned short tmpport;
233 const char* crule_name;
234 struct ConfItem* aconf;
235 struct Client* acptr;
239 assert(cptr == sptr);
240 assert(IsAnOper(sptr));
243 return need_more_params(sptr, "CONNECT");
247 * if parc > 3, we are trying to connect two remote
248 * servers to each other
252 * Only allow LocOps to make local CONNECTS --SRB
257 struct Client* acptr2;
258 struct Client* acptr3;
260 if (!(acptr3 = find_match_server(parv[3]))) {
261 send_reply(sptr, ERR_NOSUCHSERVER, parv[3]);
266 * Look for closest matching server
267 * needed for "/connect blah 4400 *"?
269 for (acptr2 = acptr3; acptr2 != &me; acptr2 = acptr2->serv->up) {
270 if (!match(parv[3], acptr2->name))
273 parv[3] = acptr3->name;
274 if (hunt_server_cmd(sptr, CMD_CONNECT, cptr, 1, "%s %s :%C", 3, parc,
275 parv) != HUNTED_ISME)
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
284 if (0 == (aconf = conf_find_server(parv[1]))) {
285 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Host %s not listed "
286 "in ircd.conf", sptr, parv[1]);
290 * use aconf->name to look up the server, see above
292 if ((acptr = FindServer(aconf->name))) {
293 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Server %s already "
294 "exists from %s", sptr, parv[1], acptr->from->name);
298 * Evaluate connection rules... If no rules found, allow the
299 * connect. Otherwise stop with the first true rule (ie: rules
300 * are ored together. Oper connects are effected only by D
301 * lines (CRULEALL) not d lines (CRULEAUTO).
303 if ((crule_name = conf_eval_crule(aconf))) {
304 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Disallowed by rule: %s",
309 * Check to see if the server is juped; if it is, disallow the connect
311 if ((ajupe = jupe_find(aconf->name)) && JupeIsActive(ajupe)) {
312 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Server %s is juped: %s",
313 sptr, JupeServer(ajupe), JupeReason(ajupe));
317 * Get port number from user, if given. If not specified,
318 * use the default from configuration structure. If missing
319 * from there, then use the precompiled default.
323 assert(0 != parv[2]);
324 if (0 == (port = atoi(parv[2]))) {
325 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: Invalid port number",
330 if (0 == port && 0 == (port = SERVER_PORT)) {
331 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Connect: missing port number",
336 tmpport = aconf->port;
339 if (connect_server(aconf, sptr, 0)) {
340 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connecting to %s.", sptr,
344 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :*** Connection to %s failed",
347 aconf->port = tmpport;
354 * XXX - remove when regression testing complete
356 * m_connect - Added by Jto 11 Feb 1989
358 * parv[0] = sender prefix
359 * parv[1] = servername
360 * parv[2] = port number
361 * parv[3] = remote server
363 int m_connect(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
366 unsigned short tmpport;
367 struct ConfItem* aconf;
368 struct ConfItem* cconf;
369 struct Client* acptr;
372 if (!IsPrivileged(sptr)) {
373 sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, parv[0]); /* XXX DEAD */
377 if (IsLocOp(sptr) && parc > 3) /* Only allow LocOps to make */
378 return 0; /* local CONNECTS --SRB */
380 if (parc > 3 && MyUser(sptr)) {
381 struct Client* acptr2;
382 struct Client* acptr3;
383 if (!(acptr3 = find_match_server(parv[3]))) {
384 sendto_one(sptr, err_str(ERR_NOSUCHSERVER), me.name, parv[0], parv[3]); /* XXX DEAD */
388 /* Look for closest matching server */
389 for (acptr2 = acptr3; acptr2 != &me; acptr2 = acptr2->serv->up)
390 if (!match(parv[3], acptr2->name))
393 parv[3] = acptr3->name;
396 if (hunt_server(1, cptr, sptr, /* XXX DEAD */
397 "%s%s " TOK_CONNECT " %s %s :%s", 3, parc, parv) != HUNTED_ISME)
400 if (parc < 2 || *parv[1] == '\0') {
401 return need_more_params(sptr, "CONNECT");
407 if ((acptr = FindServer(parv[1]))) {
409 sendto_one(sptr, ":%s NOTICE %s :Connect: Server %s %s %s.", /* XXX DEAD */
410 me.name, parv[0], parv[1], "already exists from", acptr->from->name);
412 sendto_one(sptr, "%s NOTICE %s%s :Connect: Server %s %s %s.", /* XXX DEAD */
413 NumServ(&me), NumNick(sptr), parv[1], "already exists from",
418 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
419 if (CONF_SERVER == aconf->status && 0 == match(parv[1], aconf->name))
424 * Checked first servernames, then try hostnames.
427 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
428 if (CONF_SERVER == aconf->status && 0 == match(parv[1], aconf->host))
435 sendto_one(sptr, ":%s NOTICE %s :Connect: Host %s not listed in ircd.conf", /* XXX DEAD */
436 me.name, parv[0], parv[1]);
438 sendto_one(sptr, "%s NOTICE %s%s :Connect: Host %s not listed in ircd.conf", /* XXX DEAD */
439 NumServ(&me), NumNick(sptr), parv[1]);
443 * Get port number from user, if given. If not specified,
444 * use the default from configuration structure. If missing
445 * from there, then use the precompiled default.
447 tmpport = port = aconf->port;
448 if (parc > 2 && !BadPtr(parv[2])) {
449 if ((port = atoi(parv[2])) == 0) {
451 sendto_one(sptr, ":%s NOTICE %s :Connect: Invalid port number", me.name, parv[0]); /* XXX DEAD */
453 sendto_one(sptr, "%s NOTICE %s%s :Connect: Invalid port number", /* XXX DEAD */
454 NumServ(&me), NumNick(sptr));
458 else if (port == 0 && (port = PORTNUM) == 0) {
460 sendto_one(sptr, ":%s NOTICE %s :Connect: missing port number", /* XXX DEAD */
463 sendto_one(sptr, "%s NOTICE %s%s :Connect: missing port number", /* XXX DEAD */
464 NumServ(&me), NumNick(sptr));
469 * Evaluate connection rules... If no rules found, allow the
470 * connect. Otherwise stop with the first true rule (ie: rules
471 * are ored together. Oper connects are effected only by D
472 * lines (CRULEALL) not d lines (CRULEAUTO).
474 for (cconf = GlobalConfList; cconf; cconf = cconf->next) {
475 if ((CONF_CRULEALL == cconf->status) &&
476 (0 == match(cconf->host, aconf->name))) {
477 if (crule_eval(cconf->passwd)) {
479 sendto_one(sptr, ":%s NOTICE %s :Connect: Disallowed by rule: %s", /* XXX DEAD */
480 me.name, parv[0], cconf->name);
482 sendto_one(sptr, "%s NOTICE %s%s :Connect: Disallowed by rule: %s", /* XXX DEAD */
483 NumServ(&me), NumNick(sptr), cconf->name);
489 * Check to see if the server is juped; if it is, disallow the connect
491 if ((ajupe = jupe_find(aconf->name)) && JupeIsActive(ajupe)) {
492 sendto_one(sptr, "%s NOTICE %s%s :Connect: Server %s is juped: %s", /* XXX DEAD */
493 NumServ(&me), NumNick(sptr), JupeServer(ajupe),
499 * Notify all operators about remote connect requests
501 if (!IsAnOper(cptr)) {
502 sendto_ops_butone(0, &me, ":%s WALLOPS :Remote CONNECT %s %s from %s", /* XXX DEAD */
503 me.name, parv[1], parv[2] ? parv[2] : "",
504 get_client_name(sptr, HIDE_IP));
505 ircd_log(L_INFO, "CONNECT From %s : %s %d",
506 parv[0], parv[1], parv[2] ? parv[2] : "");
509 if (connect_server(aconf, sptr, 0)) {
511 sendto_one(sptr, ":%s NOTICE %s :*** Connecting to %s.", /* XXX DEAD */
512 me.name, parv[0], aconf->name);
514 sendto_one(sptr, "%s NOTICE %s%s :*** Connecting to %s.", /* XXX DEAD */
515 NumServ(&me), NumNick(sptr), aconf->name);
519 sendto_one(sptr, ":%s NOTICE %s :*** Connection to %s failed", /* XXX DEAD */
520 me.name, parv[0], aconf->name);
522 sendto_one(sptr, "%s NOTICE %s%s :*** Connection to %s failed", /* XXX DEAD */
523 NumServ(&me), NumNick(sptr), aconf->name);
525 aconf->port = tmpport;