]> jfr.im git - irc/rqf/shadowircd.git/blame - modules/m_connect.c
[svn] Backport from early 3.x:
[irc/rqf/shadowircd.git] / modules / m_connect.c
CommitLineData
212380e3 1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_connect.c: Connects to a remote IRC server.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
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 2 of the License, or
12 * (at your option) 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
5366977b 24 * $Id: m_connect.c 3161 2007-01-25 07:23:01Z nenolod $
212380e3 25 */
26
27#include "stdinc.h"
28#include "client.h"
29#include "ircd.h"
30#include "irc_string.h"
31#include "numeric.h"
32#include "commio.h"
33#include "s_conf.h"
34#include "s_newconf.h"
35#include "s_log.h"
36#include "s_serv.h"
37#include "send.h"
38#include "msg.h"
39#include "parse.h"
40#include "hash.h"
41#include "modules.h"
42
43static int mo_connect(struct Client *, struct Client *, int, const char **);
44static int ms_connect(struct Client *, struct Client *, int, const char **);
45
46struct Message connect_msgtab = {
47 "CONNECT", 0, 0, 0, MFLG_SLOW,
48 {mg_unreg, mg_not_oper, {ms_connect, 4}, {ms_connect, 4}, mg_ignore, {mo_connect, 2}}
49};
50
51mapi_clist_av1 connect_clist[] = { &connect_msgtab, NULL };
5366977b 52DECLARE_MODULE_AV1(connect, NULL, NULL, connect_clist, NULL, NULL, "$Revision: 3161 $");
212380e3 53
54/*
55 * mo_connect - CONNECT command handler
56 *
57 * Added by Jto 11 Feb 1989
58 *
59 * m_connect
60 * parv[0] = sender prefix
61 * parv[1] = servername
62 * parv[2] = port number
63 * parv[3] = remote server
64 */
65static int
66mo_connect(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
67{
68 int port;
69 int tmpport;
70 struct server_conf *server_p;
71 struct Client *target_p;
72
73 /* always privileged with handlers */
74
75 if(MyConnect(source_p) && !IsOperRemote(source_p) && parc > 3)
76 {
77 sendto_one(source_p, form_str(ERR_NOPRIVS),
78 me.name, source_p->name, "remote");
79 return 0;
80 }
81
82 if(hunt_server(client_p, source_p, ":%s CONNECT %s %s :%s", 3, parc, parv) != HUNTED_ISME)
83 return 0;
84
85 if((target_p = find_server(source_p, parv[1])))
86 {
5366977b 87 sendto_one_notice(source_p, ":Connect: Server %s already exists from %s.", parv[1],
88 target_p->from->name);
212380e3 89 return 0;
90 }
91
92 /*
93 * try to find the name, then host, if both fail notify ops and bail
94 */
95 if((server_p = find_server_conf(parv[1])) == NULL)
96 {
5366977b 97 sendto_one_notice(source_p, ":Connect: Host %s not listed in ircd.conf", parv[1]);
212380e3 98 return 0;
99 }
100
101 /*
102 * Get port number from user, if given. If not specified,
103 * use the default form configuration structure. If missing
104 * from there, then use the precompiled default.
105 */
106 tmpport = port = server_p->port;
107 if(parc > 2 && !EmptyString(parv[2]))
108 {
109 if((port = atoi(parv[2])) <= 0)
110 {
5366977b 111 sendto_one_notice(source_p, ":Connect: Illegal port number");
212380e3 112 return 0;
113 }
114 }
115 else if(port <= 0 && (port = PORTNUM) <= 0)
116 {
5366977b 117 sendto_one_notice(source_p, ":Connect: missing port number");
212380e3 118 return 0;
119 }
120 /*
121 * Notify all operators about remote connect requests
122 */
123
124 ilog(L_SERVER, "CONNECT From %s : %s %s", parv[0], parv[1], parc > 2 ? parv[2] : "");
125
126 server_p->port = port;
127 /*
128 * at this point we should be calling connect_server with a valid
129 * C:line and a valid port in the C:line
130 */
131 if(serv_connect(server_p, source_p))
132 {
133#ifndef HIDE_SERVERS_IPS
5366977b 134 sendto_one_notice(source_p, ":*** Connecting to %s[%s].%d",
135 server_p->host, server_p->name, server_p->port);
212380e3 136#else
5366977b 137 sendto_one_notice(source_p, ":*** Connecting to %s.%d",
138 server_p->name, server_p->port);
212380e3 139#endif
140
141 }
142 else
143 {
5366977b 144 sendto_one_notice(source_p, ":*** Couldn't connect to %s.%d",
145 server_p->name, server_p->port);
212380e3 146 }
147
148 /*
149 * client is either connecting with all the data it needs or has been
150 * destroyed
151 */
152 server_p->port = tmpport;
153 return 0;
154}
155
156/*
157 * ms_connect - CONNECT command handler
158 *
159 * Added by Jto 11 Feb 1989
160 *
161 * m_connect
162 * parv[0] = sender prefix
163 * parv[1] = servername
164 * parv[2] = port number
165 * parv[3] = remote server
166 */
167static int
168ms_connect(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
169{
170 int port;
171 int tmpport;
172 struct server_conf *server_p;
173 struct Client *target_p;
174
175 if(hunt_server(client_p, source_p, ":%s CONNECT %s %s :%s", 3, parc, parv) != HUNTED_ISME)
176 return 0;
177
178 if((target_p = find_server(NULL, parv[1])))
179 {
180 sendto_one_notice(source_p, ":Connect: Server %s already exists from %s.",
181 parv[1], target_p->from->name);
182 return 0;
183 }
184
185 /*
186 * try to find the name, then host, if both fail notify ops and bail
187 */
188 if((server_p = find_server_conf(parv[1])) == NULL)
189 {
190 sendto_one_notice(source_p, ":Connect: Host %s not listed in ircd.conf",
191 parv[1]);
192 return 0;
193 }
194
195 /*
196 * Get port number from user, if given. If not specified,
197 * use the default form configuration structure. If missing
198 * from there, then use the precompiled default.
199 */
200 tmpport = server_p->port;
201
202 port = atoi(parv[2]);
203
204 /* if someone sends port 0, and we have a config port.. use it */
205 if(port == 0 && server_p->port)
206 port = server_p->port;
207 else if(port <= 0)
208 {
209 sendto_one_notice(source_p, ":Connect: Illegal port number");
210 return 0;
211 }
212
213 /*
214 * Notify all operators about remote connect requests
215 */
216 sendto_wallops_flags(UMODE_WALLOP, &me,
217 "Remote CONNECT %s %d from %s",
218 parv[1], port, source_p->name);
219 sendto_server(NULL, NULL, CAP_TS6, NOCAPS,
220 ":%s WALLOPS :Remote CONNECT %s %d from %s",
221 me.id, parv[1], port, source_p->name);
222 sendto_server(NULL, NULL, NOCAPS, CAP_TS6,
223 ":%s WALLOPS :Remote CONNECT %s %d from %s",
224 me.name, parv[1], port, source_p->name);
225
226 ilog(L_SERVER, "CONNECT From %s : %s %d", source_p->name, parv[1], port);
227
228 server_p->port = port;
229 /*
230 * at this point we should be calling connect_server with a valid
231 * C:line and a valid port in the C:line
232 */
233 if(serv_connect(server_p, source_p))
234 sendto_one_notice(source_p, ":*** Connecting to %s.%d",
235 server_p->name, server_p->port);
236 else
237 sendto_one_notice(source_p, ":*** Couldn't connect to %s.%d",
238 server_p->name, server_p->port);
239 /*
240 * client is either connecting with all the data it needs or has been
241 * destroyed
242 */
243 server_p->port = tmpport;
244 return 0;
245}