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