1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2015 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file m_connect.c |
23 |
* \brief Includes required functions for processing the CONNECT command. |
24 |
* \version $Id$ |
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 "s_bsd.h" |
33 |
#include "conf.h" |
34 |
#include "log.h" |
35 |
#include "server.h" |
36 |
#include "send.h" |
37 |
#include "parse.h" |
38 |
#include "hash.h" |
39 |
#include "modules.h" |
40 |
|
41 |
|
42 |
/*! \brief CONNECT command handler |
43 |
* |
44 |
* \param source_p Pointer to allocated Client struct from which the message |
45 |
* originally comes from. This can be a local or remote client. |
46 |
* \param parc Integer holding the number of supplied arguments. |
47 |
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL |
48 |
* pointers. |
49 |
* \note Valid arguments for this command are: |
50 |
* - parv[0] = command |
51 |
* - parv[1] = target server |
52 |
* - parv[2] = port number |
53 |
* - parv[3] = nickname/servername |
54 |
*/ |
55 |
static int |
56 |
mo_connect(struct Client *source_p, int parc, char *parv[]) |
57 |
{ |
58 |
int port = 0, tmpport = 0; |
59 |
struct MaskItem *conf = NULL; |
60 |
const struct Client *target_p = NULL; |
61 |
|
62 |
if (EmptyString(parv[1])) |
63 |
{ |
64 |
sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, "CONNECT"); |
65 |
return 0; |
66 |
} |
67 |
|
68 |
if (parc > 3) |
69 |
{ |
70 |
if (!HasOFlag(source_p, OPER_FLAG_CONNECT_REMOTE)) |
71 |
{ |
72 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVS, "connect:remote"); |
73 |
return 0; |
74 |
} |
75 |
|
76 |
if (hunt_server(source_p, ":%s CONNECT %s %s :%s", 3, parc, parv) != HUNTED_ISME) |
77 |
return 0; |
78 |
} |
79 |
else if (!HasOFlag(source_p, OPER_FLAG_CONNECT)) |
80 |
{ |
81 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVS, "connect"); |
82 |
return 0; |
83 |
} |
84 |
|
85 |
/* |
86 |
* Try to find the name, then host, if both fail notify ops and bail |
87 |
*/ |
88 |
if (!(conf = find_matching_name_conf(CONF_SERVER, parv[1], NULL, NULL, 0)) && |
89 |
!(conf = find_matching_name_conf(CONF_SERVER, NULL, NULL, parv[1], 0))) |
90 |
{ |
91 |
sendto_one_notice(source_p, &me, ":Connect: Host %s not listed in configuration file", parv[1]); |
92 |
return 0; |
93 |
} |
94 |
|
95 |
if ((target_p = hash_find_server(conf->name))) |
96 |
{ |
97 |
sendto_one_notice(source_p, &me, ":Connect: Server %s already exists from %s.", |
98 |
target_p->name, target_p->from->name); |
99 |
return 0; |
100 |
} |
101 |
|
102 |
/* |
103 |
* Get port number from user, if given. If not specified, |
104 |
* use the default from configuration structure. If missing |
105 |
* from there, then use the precompiled default. |
106 |
*/ |
107 |
tmpport = port = conf->port; |
108 |
|
109 |
if (parc > 2 && !EmptyString(parv[2])) |
110 |
{ |
111 |
if ((port = atoi(parv[2])) <= 0) |
112 |
{ |
113 |
sendto_one_notice(source_p, &me, ":Connect: Illegal port number"); |
114 |
return 0; |
115 |
} |
116 |
} |
117 |
else if (port <= 0 && (port = PORTNUM) <= 0) |
118 |
{ |
119 |
sendto_one_notice(source_p, &me, ":Connect: missing port number"); |
120 |
return 0; |
121 |
} |
122 |
|
123 |
if (find_servconn_in_progress(conf->name)) |
124 |
{ |
125 |
sendto_one_notice(source_p, &me, ":Connect: a connection to %s " |
126 |
"is already in progress.", conf->name); |
127 |
return 0; |
128 |
} |
129 |
|
130 |
/* |
131 |
* Notify all operators about remote connect requests |
132 |
*/ |
133 |
ilog(LOG_TYPE_IRCD, "CONNECT From %s : %s %s", |
134 |
source_p->name, parv[1], parv[2] ? parv[2] : ""); |
135 |
|
136 |
conf->port = port; |
137 |
|
138 |
/* |
139 |
* At this point we should be calling connect_server with a valid |
140 |
* connect{} block and a valid port in the connect{} block |
141 |
*/ |
142 |
if (serv_connect(conf, source_p)) |
143 |
{ |
144 |
if (!ConfigServerHide.hide_server_ips && HasUMode(source_p, UMODE_ADMIN)) |
145 |
sendto_one_notice(source_p, &me, ":*** Connecting to %s[%s].%d", |
146 |
conf->host, conf->name, conf->port); |
147 |
else |
148 |
sendto_one_notice(source_p, &me, ":*** Connecting to %s.%d", |
149 |
conf->name, conf->port); |
150 |
} |
151 |
else |
152 |
sendto_one_notice(source_p, &me, ":*** Couldn't connect to %s.%d", |
153 |
conf->name, conf->port); |
154 |
|
155 |
/* |
156 |
* Client is either connecting with all the data it needs or has been |
157 |
* destroyed |
158 |
*/ |
159 |
conf->port = tmpport; |
160 |
return 0; |
161 |
} |
162 |
|
163 |
/*! \brief CONNECT command handler |
164 |
* |
165 |
* \param source_p Pointer to allocated Client struct from which the message |
166 |
* originally comes from. This can be a local or remote client. |
167 |
* \param parc Integer holding the number of supplied arguments. |
168 |
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL |
169 |
* pointers. |
170 |
* \note Valid arguments for this command are: |
171 |
* - parv[0] = command |
172 |
* - parv[1] = target server |
173 |
* - parv[2] = port number |
174 |
* - parv[3] = nickname/servername |
175 |
*/ |
176 |
static int |
177 |
ms_connect(struct Client *source_p, int parc, char *parv[]) |
178 |
{ |
179 |
int port = 0, tmpport = 0; |
180 |
struct MaskItem *conf = NULL; |
181 |
const struct Client *target_p = NULL; |
182 |
|
183 |
if (parc < 4 || EmptyString(parv[3])) |
184 |
{ |
185 |
sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, "CONNECT"); |
186 |
return 0; |
187 |
} |
188 |
|
189 |
if (hunt_server(source_p, ":%s CONNECT %s %s :%s", 3, parc, parv) != HUNTED_ISME) |
190 |
return 0; |
191 |
|
192 |
/* |
193 |
* Try to find the name, then host, if both fail notify ops and bail |
194 |
*/ |
195 |
if (!(conf = find_matching_name_conf(CONF_SERVER, parv[1], NULL, NULL, 0)) && |
196 |
!(conf = find_matching_name_conf(CONF_SERVER, NULL, NULL, parv[1], 0))) |
197 |
{ |
198 |
sendto_one_notice(source_p, &me, ":Connect: Host %s not listed in configuration file", parv[1]); |
199 |
return 0; |
200 |
} |
201 |
|
202 |
if ((target_p = hash_find_server(conf->name))) |
203 |
{ |
204 |
sendto_one_notice(source_p, &me, ":Connect: Server %s already exists from %s.", |
205 |
target_p->name, target_p->from->name); |
206 |
return 0; |
207 |
} |
208 |
|
209 |
/* |
210 |
* Get port number from user, if given. If not specified, |
211 |
* use the default from configuration structure. If missing |
212 |
* from there, then use the precompiled default. |
213 |
*/ |
214 |
tmpport = port = conf->port; |
215 |
|
216 |
if (parc > 2 && !EmptyString(parv[2])) |
217 |
{ |
218 |
port = atoi(parv[2]); |
219 |
|
220 |
/* |
221 |
* If someone sends port 0, and we have a config port.. use it |
222 |
*/ |
223 |
if (port == 0 && conf->port) |
224 |
port = conf->port; |
225 |
else if (port <= 0) |
226 |
{ |
227 |
sendto_one_notice(source_p, &me, ":Connect: Illegal port number"); |
228 |
return 0; |
229 |
} |
230 |
} |
231 |
else if (port <= 0 && (port = PORTNUM) <= 0) |
232 |
{ |
233 |
sendto_one_notice(source_p, &me, ":Connect: missing port number"); |
234 |
return 0; |
235 |
} |
236 |
|
237 |
if (find_servconn_in_progress(conf->name)) |
238 |
{ |
239 |
sendto_one_notice(source_p, &me, ":Connect: a connection to %s " |
240 |
"is already in progress.", conf->name); |
241 |
return 0; |
242 |
} |
243 |
|
244 |
/* |
245 |
* Notify all operators about remote connect requests |
246 |
*/ |
247 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_GLOBAL, "from %s: Remote CONNECT %s %d from %s", |
248 |
me.name, parv[1], port, source_p->name); |
249 |
sendto_server(NULL, 0, 0, |
250 |
":%s GLOBOPS :Remote CONNECT %s %d from %s", |
251 |
me.id, parv[1], port, source_p->name); |
252 |
|
253 |
ilog(LOG_TYPE_IRCD, "CONNECT From %s : %s %d", |
254 |
source_p->name, parv[1], port); |
255 |
|
256 |
conf->port = port; |
257 |
|
258 |
/* |
259 |
* At this point we should be calling connect_server with a valid |
260 |
* connect{} block and a valid port in the connect{} block |
261 |
*/ |
262 |
if (serv_connect(conf, source_p)) |
263 |
sendto_one_notice(source_p, &me, ":*** Connecting to %s.%d", |
264 |
conf->name, conf->port); |
265 |
else |
266 |
sendto_one_notice(source_p, &me, ":*** Couldn't connect to %s.%d", |
267 |
conf->name, conf->port); |
268 |
/* |
269 |
* Client is either connecting with all the data it needs or has been |
270 |
* destroyed |
271 |
*/ |
272 |
conf->port = tmpport; |
273 |
return 0; |
274 |
} |
275 |
|
276 |
static struct Message connect_msgtab = |
277 |
{ |
278 |
.cmd = "CONNECT", |
279 |
.args_min = 2, |
280 |
.args_max = MAXPARA, |
281 |
.handlers[UNREGISTERED_HANDLER] = m_unregistered, |
282 |
.handlers[CLIENT_HANDLER] = m_not_oper, |
283 |
.handlers[SERVER_HANDLER] = ms_connect, |
284 |
.handlers[ENCAP_HANDLER] = m_ignore, |
285 |
.handlers[OPER_HANDLER] = mo_connect |
286 |
}; |
287 |
|
288 |
static void |
289 |
module_init(void) |
290 |
{ |
291 |
mod_add_cmd(&connect_msgtab); |
292 |
} |
293 |
|
294 |
static void |
295 |
module_exit(void) |
296 |
{ |
297 |
mod_del_cmd(&connect_msgtab); |
298 |
} |
299 |
|
300 |
struct module module_entry = |
301 |
{ |
302 |
.version = "$Revision$", |
303 |
.modinit = module_init, |
304 |
.modexit = module_exit, |
305 |
}; |