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