1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* s_serv.c: Server related functions. |
4 |
* |
5 |
* Copyright (C) 2005 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 |
#ifdef HAVE_LIBCRYPTO |
27 |
#include <openssl/rsa.h> |
28 |
#include "rsa.h" |
29 |
#endif |
30 |
#include "channel.h" |
31 |
#include "channel_mode.h" |
32 |
#include "client.h" |
33 |
#include "common.h" |
34 |
#include "hash.h" |
35 |
#include "ircd.h" |
36 |
#include "ircd_defs.h" |
37 |
#include "numeric.h" |
38 |
#include "packet.h" |
39 |
#include "s_conf.h" |
40 |
#include "s_serv.h" |
41 |
#include "s_user.h" |
42 |
#include "send.h" |
43 |
#include "channel.h" /* chcap_usage_counts stuff...*/ |
44 |
|
45 |
#define MIN_CONN_FREQ 300 |
46 |
|
47 |
static dlink_list cap_list = { NULL, NULL, 0 }; |
48 |
static void server_burst(struct Client *); |
49 |
static int fork_server(struct Client *); |
50 |
static void burst_all(struct Client *); |
51 |
static void send_tb(struct Client *, const struct Channel *); |
52 |
|
53 |
static CNCB serv_connect_callback; |
54 |
|
55 |
static void start_io(struct Client *); |
56 |
static void burst_members(struct Client *, struct Channel *); |
57 |
|
58 |
static SlinkRplHnd slink_error; |
59 |
static SlinkRplHnd slink_zipstats; |
60 |
|
61 |
|
62 |
#ifdef HAVE_LIBCRYPTO |
63 |
struct EncCapability CipherTable[] = { |
64 |
#ifdef HAVE_EVP_BF_CFB |
65 |
{ "BF/168", CAP_ENC_BF_168, 24, CIPHER_BF }, |
66 |
{ "BF/128", CAP_ENC_BF_128, 16, CIPHER_BF }, |
67 |
#endif |
68 |
#ifdef HAVE_EVP_CAST5_CFB |
69 |
{ "CAST/128", CAP_ENC_CAST_128, 16, CIPHER_CAST }, |
70 |
#endif |
71 |
#ifdef HAVE_EVP_IDEA_CFB |
72 |
{ "IDEA/128", CAP_ENC_IDEA_128, 16, CIPHER_IDEA }, |
73 |
#endif |
74 |
#ifdef HAVE_EVP_RC5_32_12_16_CFB |
75 |
{ "RC5.16/128", CAP_ENC_RC5_16_128, 16, CIPHER_RC5_16 }, |
76 |
{ "RC5.12/128", CAP_ENC_RC5_12_128, 16, CIPHER_RC5_12 }, |
77 |
{ "RC5.8/128", CAP_ENC_RC5_8_128, 16, CIPHER_RC5_8 }, |
78 |
#endif |
79 |
#ifdef HAVE_EVP_DES_EDE3_CFB |
80 |
{ "3DES/168", CAP_ENC_3DES_168, 24, CIPHER_3DES }, |
81 |
#endif |
82 |
#ifdef HAVE_EVP_DES_CFB |
83 |
{ "DES/56", CAP_ENC_DES_56, 8, CIPHER_DES }, |
84 |
#endif |
85 |
{ 0, 0, 0, 0 } |
86 |
}; |
87 |
#endif |
88 |
|
89 |
struct SlinkRplDef slinkrpltab[] = { |
90 |
{ SLINKRPL_ERROR, slink_error, SLINKRPL_FLAG_DATA }, |
91 |
{ SLINKRPL_ZIPSTATS, slink_zipstats, SLINKRPL_FLAG_DATA }, |
92 |
{ 0, 0, 0 }, |
93 |
}; |
94 |
|
95 |
|
96 |
void |
97 |
slink_error(unsigned int rpl, unsigned int len, unsigned char *data, |
98 |
struct Client *server_p) |
99 |
{ |
100 |
assert(rpl == SLINKRPL_ERROR); |
101 |
assert(len < 256); |
102 |
|
103 |
data[len-1] = '\0'; |
104 |
|
105 |
sendto_realops_flags(UMODE_ALL, L_ALL, "SlinkError for %s: %s", |
106 |
server_p->name, data); |
107 |
/* XXX should this be exit_client? */ |
108 |
exit_client(server_p, &me, "servlink error -- terminating link"); |
109 |
} |
110 |
|
111 |
/* |
112 |
* slink_zipstats |
113 |
* |
114 |
* inputs - rpl reply |
115 |
* len length of reply |
116 |
* data pointer to data |
117 |
* pointer to server |
118 |
* output - none |
119 |
* side effects - ziplink stats are calculated |
120 |
*/ |
121 |
void |
122 |
slink_zipstats(unsigned int rpl, unsigned int len, unsigned char *data, |
123 |
struct Client *server_p) |
124 |
{ |
125 |
struct ZipStats zipstats; |
126 |
unsigned long in = 0, in_wire = 0, out = 0, out_wire = 0; |
127 |
int i = 0; |
128 |
|
129 |
assert(rpl == SLINKRPL_ZIPSTATS); |
130 |
assert(len == 16); |
131 |
assert(IsCapable(server_p, CAP_ZIP)); |
132 |
|
133 |
/* Yes, it needs to be done this way, no we cannot let the compiler |
134 |
* work with the pointer to the structure. This works around a GCC |
135 |
* bug on SPARC that affects all versions at the time of this writing. |
136 |
* I will feed you to the creatures living in RMS's beard if you do |
137 |
* not leave this as is, without being sure that you are not causing |
138 |
* regression for most of our installed SPARC base. |
139 |
* -jmallett, 04/27/2002 |
140 |
*/ |
141 |
memcpy(&zipstats, &server_p->localClient->zipstats, sizeof(struct ZipStats)); |
142 |
|
143 |
in |= (data[i++] << 24); |
144 |
in |= (data[i++] << 16); |
145 |
in |= (data[i++] << 8); |
146 |
in |= (data[i++] ); |
147 |
|
148 |
in_wire |= (data[i++] << 24); |
149 |
in_wire |= (data[i++] << 16); |
150 |
in_wire |= (data[i++] << 8); |
151 |
in_wire |= (data[i++] ); |
152 |
|
153 |
out |= (data[i++] << 24); |
154 |
out |= (data[i++] << 16); |
155 |
out |= (data[i++] << 8); |
156 |
out |= (data[i++] ); |
157 |
|
158 |
out_wire |= (data[i++] << 24); |
159 |
out_wire |= (data[i++] << 16); |
160 |
out_wire |= (data[i++] << 8); |
161 |
out_wire |= (data[i++] ); |
162 |
|
163 |
/* This macro adds b to a if a plus b is not an overflow, and sets the |
164 |
* value of a to b if it is. |
165 |
* Add and Set if No Overflow. |
166 |
*/ |
167 |
#define ASNO(a, b) a = (a + b >= a ? a + b : b) |
168 |
|
169 |
ASNO(zipstats.in, in); |
170 |
ASNO(zipstats.out, out); |
171 |
ASNO(zipstats.in_wire, in_wire); |
172 |
ASNO(zipstats.out_wire, out_wire); |
173 |
|
174 |
if (zipstats.in > 0) |
175 |
zipstats.in_ratio = (((double)(zipstats.in - zipstats.in_wire) / |
176 |
(double)zipstats.in) * 100.00); |
177 |
else |
178 |
zipstats.in_ratio = 0; |
179 |
|
180 |
if (zipstats.out > 0) |
181 |
zipstats.out_ratio = (((double)(zipstats.out - zipstats.out_wire) / |
182 |
(double)zipstats.out) * 100.00); |
183 |
else |
184 |
zipstats.out_ratio = 0; |
185 |
|
186 |
memcpy(&server_p->localClient->zipstats, &zipstats, sizeof (struct ZipStats)); |
187 |
} |
188 |
|
189 |
void |
190 |
collect_zipstats(void *unused) |
191 |
{ |
192 |
dlink_node *ptr; |
193 |
struct Client *target_p; |
194 |
|
195 |
DLINK_FOREACH(ptr, serv_list.head) |
196 |
{ |
197 |
target_p = ptr->data; |
198 |
|
199 |
if (IsCapable(target_p, CAP_ZIP)) |
200 |
{ |
201 |
/* only bother if we haven't already got something queued... */ |
202 |
if (target_p->localClient->slinkq == NULL) |
203 |
{ |
204 |
target_p->localClient->slinkq = MyMalloc(1); /* sigh.. */ |
205 |
target_p->localClient->slinkq[0] = SLINKCMD_ZIPSTATS; |
206 |
target_p->localClient->slinkq_ofs = 0; |
207 |
target_p->localClient->slinkq_len = 1; |
208 |
send_queued_slink_write(target_p); |
209 |
} |
210 |
} |
211 |
} |
212 |
} |
213 |
|
214 |
#ifdef HAVE_LIBCRYPTO |
215 |
struct EncCapability * |
216 |
check_cipher(struct Client *client_p, struct AccessItem *aconf) |
217 |
{ |
218 |
struct EncCapability *epref = NULL; |
219 |
|
220 |
/* Use connect{} specific info if available */ |
221 |
if (aconf->cipher_preference) |
222 |
epref = aconf->cipher_preference; |
223 |
else if (ConfigFileEntry.default_cipher_preference) |
224 |
epref = ConfigFileEntry.default_cipher_preference; |
225 |
|
226 |
/* If the server supports the capability in hand, return the matching |
227 |
* conf struct. Otherwise, return NULL (an error). |
228 |
*/ |
229 |
if (epref && IsCapableEnc(client_p, epref->cap)) |
230 |
return epref; |
231 |
|
232 |
return NULL; |
233 |
} |
234 |
#endif /* HAVE_LIBCRYPTO */ |
235 |
|
236 |
/* my_name_for_link() |
237 |
* return wildcard name of my server name |
238 |
* according to given config entry --Jto |
239 |
*/ |
240 |
const char * |
241 |
my_name_for_link(struct AccessItem *aconf) |
242 |
{ |
243 |
if (aconf->fakename != NULL) |
244 |
return(aconf->fakename); |
245 |
else |
246 |
return(me.name); |
247 |
} |
248 |
|
249 |
/* |
250 |
* write_links_file |
251 |
* |
252 |
* inputs - void pointer which is not used |
253 |
* output - NONE |
254 |
* side effects - called from an event, write out list of linked servers |
255 |
* but in no particular order. |
256 |
*/ |
257 |
void |
258 |
write_links_file(void* notused) |
259 |
{ |
260 |
MessageFileLine *next_mptr = 0; |
261 |
MessageFileLine *mptr = 0; |
262 |
MessageFileLine *currentMessageLine = 0; |
263 |
MessageFileLine *newMessageLine = 0; |
264 |
MessageFile *MessageFileptr; |
265 |
const char *p; |
266 |
FBFILE *file; |
267 |
char buff[512]; |
268 |
dlink_node *ptr; |
269 |
|
270 |
MessageFileptr = &ConfigFileEntry.linksfile; |
271 |
|
272 |
if ((file = fbopen(MessageFileptr->fileName, "w")) == NULL) |
273 |
return; |
274 |
|
275 |
for (mptr = MessageFileptr->contentsOfFile; mptr; mptr = next_mptr) |
276 |
{ |
277 |
next_mptr = mptr->next; |
278 |
MyFree(mptr); |
279 |
} |
280 |
|
281 |
MessageFileptr->contentsOfFile = NULL; |
282 |
currentMessageLine = NULL; |
283 |
|
284 |
DLINK_FOREACH(ptr, global_serv_list.head) |
285 |
{ |
286 |
size_t nbytes = 0; |
287 |
struct Client *target_p = ptr->data; |
288 |
|
289 |
/* skip ourselves, we send ourselves in /links */ |
290 |
if (IsMe(target_p)) |
291 |
continue; |
292 |
|
293 |
/* skip hidden servers */ |
294 |
if (IsHidden(target_p) && !ConfigServerHide.disable_hidden) |
295 |
continue; |
296 |
|
297 |
if (target_p->info[0]) |
298 |
p = target_p->info; |
299 |
else |
300 |
p = "(Unknown Location)"; |
301 |
|
302 |
newMessageLine = MyMalloc(sizeof(MessageFileLine)); |
303 |
|
304 |
/* Attempt to format the file in such a way it follows the usual links output |
305 |
* ie "servername uplink :hops info" |
306 |
* Mostly for aesthetic reasons - makes it look pretty in mIRC ;) |
307 |
* - madmax |
308 |
*/ |
309 |
|
310 |
/* |
311 |
* For now, check this ircsprintf wont overflow - it shouldnt on a |
312 |
* default config but it is configurable.. |
313 |
* This should be changed to an snprintf at some point, but I'm wanting to |
314 |
* know if this is a cause of a bug - cryogen |
315 |
*/ |
316 |
assert(strlen(target_p->name) + strlen(me.name) + 6 + strlen(p) <= |
317 |
MESSAGELINELEN); |
318 |
ircsprintf(newMessageLine->line, "%s %s :1 %s", |
319 |
target_p->name, me.name, p); |
320 |
newMessageLine->next = NULL; |
321 |
|
322 |
if (MessageFileptr->contentsOfFile) |
323 |
{ |
324 |
if (currentMessageLine) |
325 |
currentMessageLine->next = newMessageLine; |
326 |
currentMessageLine = newMessageLine; |
327 |
} |
328 |
else |
329 |
{ |
330 |
MessageFileptr->contentsOfFile = newMessageLine; |
331 |
currentMessageLine = newMessageLine; |
332 |
} |
333 |
|
334 |
nbytes = ircsprintf(buff, "%s %s :1 %s\n", target_p->name, me.name, p); |
335 |
fbputs(buff, file, nbytes); |
336 |
} |
337 |
|
338 |
fbclose(file); |
339 |
} |
340 |
|
341 |
/* hunt_server() |
342 |
* Do the basic thing in delivering the message (command) |
343 |
* across the relays to the specific server (server) for |
344 |
* actions. |
345 |
* |
346 |
* Note: The command is a format string and *MUST* be |
347 |
* of prefixed style (e.g. ":%s COMMAND %s ..."). |
348 |
* Command can have only max 8 parameters. |
349 |
* |
350 |
* server parv[server] is the parameter identifying the |
351 |
* target server. |
352 |
* |
353 |
* *WARNING* |
354 |
* parv[server] is replaced with the pointer to the |
355 |
* real servername from the matched client (I'm lazy |
356 |
* now --msa). |
357 |
* |
358 |
* returns: (see #defines) |
359 |
*/ |
360 |
int |
361 |
hunt_server(struct Client *client_p, struct Client *source_p, const char *command, |
362 |
int server, int parc, char *parv[]) |
363 |
{ |
364 |
struct Client *target_p = NULL; |
365 |
struct Client *target_tmp = NULL; |
366 |
dlink_node *ptr; |
367 |
int wilds; |
368 |
|
369 |
/* Assume it's me, if no server |
370 |
*/ |
371 |
if (parc <= server || EmptyString(parv[server]) || |
372 |
match(me.name, parv[server]) || |
373 |
match(parv[server], me.name) || |
374 |
!strcmp(parv[server], me.id)) |
375 |
return(HUNTED_ISME); |
376 |
|
377 |
/* These are to pickup matches that would cause the following |
378 |
* message to go in the wrong direction while doing quick fast |
379 |
* non-matching lookups. |
380 |
*/ |
381 |
if (MyClient(source_p)) |
382 |
target_p = find_client(parv[server]); |
383 |
else |
384 |
target_p = find_person(client_p, parv[server]); |
385 |
|
386 |
if (target_p) |
387 |
if (target_p->from == source_p->from && !MyConnect(target_p)) |
388 |
target_p = NULL; |
389 |
|
390 |
if (target_p == NULL && (target_p = find_server(parv[server]))) |
391 |
if (target_p->from == source_p->from && !MyConnect(target_p)) |
392 |
target_p = NULL; |
393 |
|
394 |
collapse(parv[server]); |
395 |
wilds = (strchr(parv[server], '?') || strchr(parv[server], '*')); |
396 |
|
397 |
/* Again, if there are no wild cards involved in the server |
398 |
* name, use the hash lookup |
399 |
*/ |
400 |
if (target_p == NULL) |
401 |
{ |
402 |
if (!wilds) |
403 |
{ |
404 |
if (!(target_p = find_server(parv[server]))) |
405 |
{ |
406 |
sendto_one(source_p, form_str(ERR_NOSUCHSERVER), |
407 |
me.name, parv[0], parv[server]); |
408 |
return(HUNTED_NOSUCH); |
409 |
} |
410 |
} |
411 |
else |
412 |
{ |
413 |
DLINK_FOREACH(ptr, global_client_list.head) |
414 |
{ |
415 |
target_tmp = ptr->data; |
416 |
|
417 |
if (match(parv[server], target_tmp->name)) |
418 |
{ |
419 |
if (target_tmp->from == source_p->from && !MyConnect(target_tmp)) |
420 |
continue; |
421 |
target_p = ptr->data; |
422 |
|
423 |
if (IsRegistered(target_p) && (target_p != client_p)) |
424 |
break; |
425 |
} |
426 |
} |
427 |
} |
428 |
} |
429 |
|
430 |
if (target_p != NULL) |
431 |
{ |
432 |
if(!IsRegistered(target_p)) |
433 |
{ |
434 |
sendto_one(source_p, form_str(ERR_NOSUCHSERVER), |
435 |
me.name, parv[0], parv[server]); |
436 |
return HUNTED_NOSUCH; |
437 |
} |
438 |
|
439 |
if (IsMe(target_p) || MyClient(target_p)) |
440 |
return HUNTED_ISME; |
441 |
|
442 |
if (!match(target_p->name, parv[server])) |
443 |
parv[server] = target_p->name; |
444 |
|
445 |
/* This is a little kludgy but should work... */ |
446 |
if (IsClient(source_p) && |
447 |
((MyConnect(target_p) && IsCapable(target_p, CAP_TS6)) || |
448 |
(!MyConnect(target_p) && IsCapable(target_p->from, CAP_TS6)))) |
449 |
parv[0] = ID(source_p); |
450 |
|
451 |
sendto_one(target_p, command, parv[0], |
452 |
parv[1], parv[2], parv[3], parv[4], |
453 |
parv[5], parv[6], parv[7], parv[8]); |
454 |
return(HUNTED_PASS); |
455 |
} |
456 |
|
457 |
sendto_one(source_p, form_str(ERR_NOSUCHSERVER), |
458 |
me.name, parv[0], parv[server]); |
459 |
return(HUNTED_NOSUCH); |
460 |
} |
461 |
|
462 |
/* try_connections() |
463 |
* |
464 |
* inputs - void pointer which is not used |
465 |
* output - NONE |
466 |
* side effects - |
467 |
* scan through configuration and try new connections. |
468 |
* Returns the calendar time when the next call to this |
469 |
* function should be made latest. (No harm done if this |
470 |
* is called earlier or later...) |
471 |
*/ |
472 |
void |
473 |
try_connections(void *unused) |
474 |
{ |
475 |
dlink_node *ptr; |
476 |
struct ConfItem *conf; |
477 |
struct AccessItem *aconf; |
478 |
struct ClassItem *cltmp; |
479 |
int confrq; |
480 |
|
481 |
/* TODO: change this to set active flag to 0 when added to event! --Habeeb */ |
482 |
if (GlobalSetOptions.autoconn == 0) |
483 |
return; |
484 |
|
485 |
DLINK_FOREACH(ptr, server_items.head) |
486 |
{ |
487 |
conf = ptr->data; |
488 |
aconf = &conf->conf.AccessItem; |
489 |
|
490 |
/* Also when already connecting! (update holdtimes) --SRB |
491 |
*/ |
492 |
if (!(aconf->status & CONF_SERVER) || aconf->port <= 0 || |
493 |
!(IsConfAllowAutoConn(aconf))) |
494 |
continue; |
495 |
|
496 |
cltmp = &((struct ConfItem *)aconf->class_ptr)->conf.ClassItem; |
497 |
|
498 |
/* Skip this entry if the use of it is still on hold until |
499 |
* future. Otherwise handle this entry (and set it on hold |
500 |
* until next time). Will reset only hold times, if already |
501 |
* made one successfull connection... [this algorithm is |
502 |
* a bit fuzzy... -- msa >;) ] |
503 |
*/ |
504 |
if (aconf->hold > CurrentTime) |
505 |
continue; |
506 |
|
507 |
if (cltmp == NULL) |
508 |
confrq = DEFAULT_CONNECTFREQUENCY; |
509 |
else |
510 |
{ |
511 |
confrq = ConFreq(cltmp); |
512 |
if (confrq < MIN_CONN_FREQ ) |
513 |
confrq = MIN_CONN_FREQ; |
514 |
} |
515 |
|
516 |
aconf->hold = CurrentTime + confrq; |
517 |
|
518 |
/* Found a CONNECT config with port specified, scan clients |
519 |
* and see if this server is already connected? |
520 |
*/ |
521 |
if (find_server(conf->name) != NULL) |
522 |
continue; |
523 |
|
524 |
if (CurrUserCount(cltmp) < MaxTotal(cltmp)) |
525 |
{ |
526 |
/* Go to the end of the list, if not already last */ |
527 |
if (ptr->next != NULL) |
528 |
{ |
529 |
dlinkDelete(ptr, &server_items); |
530 |
dlinkAddTail(conf, &conf->node, &server_items); |
531 |
} |
532 |
|
533 |
if (find_servconn_in_progress(conf->name)) |
534 |
return; |
535 |
|
536 |
/* We used to only print this if serv_connect() actually |
537 |
* succeeded, but since comm_tcp_connect() can call the callback |
538 |
* immediately if there is an error, we were getting error messages |
539 |
* in the wrong order. SO, we just print out the activated line, |
540 |
* and let serv_connect() / serv_connect_callback() print an |
541 |
* error afterwards if it fails. |
542 |
* -- adrian |
543 |
*/ |
544 |
if (ConfigServerHide.hide_server_ips) |
545 |
sendto_realops_flags(UMODE_ALL, L_ALL, "Connection to %s activated.", |
546 |
conf->name); |
547 |
else |
548 |
sendto_realops_flags(UMODE_ALL, L_ALL, "Connection to %s[%s] activated.", |
549 |
conf->name, aconf->host); |
550 |
|
551 |
serv_connect(aconf, NULL, aconf->port); |
552 |
/* We connect only one at time... */ |
553 |
return; |
554 |
} |
555 |
} |
556 |
} |
557 |
|
558 |
/* bogus_host() |
559 |
* |
560 |
* inputs - hostname |
561 |
* output - 1 if a bogus hostname input, |
562 |
* - 0 if its valid |
563 |
* side effects - none |
564 |
*/ |
565 |
int |
566 |
bogus_host(const char *host) |
567 |
{ |
568 |
unsigned int length = 0; |
569 |
unsigned int dots = 0; |
570 |
const unsigned char *s = (const unsigned char *)host; |
571 |
|
572 |
for (; *s; ++s) |
573 |
{ |
574 |
if (!IsServChar(*s)) |
575 |
return 1; |
576 |
|
577 |
++length; |
578 |
|
579 |
if ('.' == *s) |
580 |
++dots; |
581 |
} |
582 |
|
583 |
return !dots || length > HOSTLEN; |
584 |
} |
585 |
|
586 |
/* |
587 |
* check_server |
588 |
* |
589 |
* inputs - name |
590 |
* - pointer to client struct |
591 |
* - int flag for cryptlink |
592 |
* output - return -'ve value for error |
593 |
* side effects - |
594 |
* |
595 |
* Called from mr_server when a client claiming to be a server |
596 |
* connects. check_server() verifies this client is or isn't a server and |
597 |
* returns an error if it isn't a server, it otherwise attaches the appropriate |
598 |
* server connect block to this client. |
599 |
*/ |
600 |
int |
601 |
check_server(const char *name, struct Client *client_p, int cryptlink) |
602 |
{ |
603 |
dlink_node *ptr; |
604 |
struct ConfItem *conf = NULL; |
605 |
struct ConfItem *server_conf = NULL; |
606 |
struct AccessItem *server_aconf = NULL; |
607 |
struct AccessItem *aconf = NULL; |
608 |
int error = -1; |
609 |
|
610 |
assert(client_p != NULL); |
611 |
|
612 |
if (strlen(name) > HOSTLEN) |
613 |
return -4; |
614 |
|
615 |
/* loop through looking for all possible connect items that might work */ |
616 |
DLINK_FOREACH(ptr, server_items.head) |
617 |
{ |
618 |
conf = ptr->data; |
619 |
aconf = &conf->conf.AccessItem; |
620 |
|
621 |
if (!match(name, conf->name)) |
622 |
continue; |
623 |
|
624 |
error = -3; |
625 |
|
626 |
/* XXX: Fix me for IPv6 */ |
627 |
/* XXX sockhost is the IPv4 ip as a string */ |
628 |
if (match(aconf->host, client_p->host) || |
629 |
match(aconf->host, client_p->sockhost)) |
630 |
{ |
631 |
error = -2; |
632 |
#ifdef HAVE_LIBCRYPTO |
633 |
if (cryptlink && IsConfCryptLink(aconf)) |
634 |
{ |
635 |
if (aconf->rsa_public_key) |
636 |
server_conf = conf; |
637 |
} |
638 |
else if (!(cryptlink || IsConfCryptLink(aconf))) |
639 |
#endif /* HAVE_LIBCRYPTO */ |
640 |
{ |
641 |
/* A NULL password is as good as a bad one */ |
642 |
if (EmptyString(client_p->localClient->passwd)) |
643 |
return(-2); |
644 |
|
645 |
/* code in s_conf.c should not have allowed this to be NULL */ |
646 |
if (aconf->passwd == NULL) |
647 |
return(-2); |
648 |
|
649 |
if (IsConfEncrypted(aconf)) |
650 |
{ |
651 |
if (strcmp(aconf->passwd, |
652 |
(const char *)crypt(client_p->localClient->passwd, |
653 |
aconf->passwd)) == 0) |
654 |
server_conf = conf; |
655 |
} |
656 |
else |
657 |
{ |
658 |
if (strcmp(aconf->passwd, client_p->localClient->passwd) == 0) |
659 |
server_conf = conf; |
660 |
} |
661 |
} |
662 |
} |
663 |
} |
664 |
|
665 |
if (server_conf == NULL) |
666 |
return error; |
667 |
|
668 |
make_server(client_p); |
669 |
attach_server_conf(client_p, server_conf); |
670 |
|
671 |
/* Now find all leaf or hub config items for this server */ |
672 |
DLINK_FOREACH(ptr, hub_items.head) |
673 |
{ |
674 |
conf = ptr->data; |
675 |
|
676 |
if (!match(name, conf->name)) |
677 |
continue; |
678 |
|
679 |
attach_leaf_hub(client_p, conf); |
680 |
} |
681 |
|
682 |
DLINK_FOREACH(ptr, leaf_items.head) |
683 |
{ |
684 |
conf = ptr->data; |
685 |
|
686 |
if (!match(name, conf->name)) |
687 |
continue; |
688 |
|
689 |
attach_leaf_hub(client_p, conf); |
690 |
} |
691 |
|
692 |
server_aconf = &server_conf->conf.AccessItem; |
693 |
|
694 |
#ifdef HAVE_LIBZ /* otherwise, clear it unconditionally */ |
695 |
if (!IsConfCompressed(server_aconf)) |
696 |
#endif |
697 |
ClearCap(client_p, CAP_ZIP); |
698 |
if (!IsConfCryptLink(server_aconf)) |
699 |
ClearCap(client_p, CAP_ENC); |
700 |
if (!IsConfTopicBurst(server_aconf)) |
701 |
{ |
702 |
ClearCap(client_p, CAP_TB); |
703 |
ClearCap(client_p, CAP_TBURST); |
704 |
} |
705 |
|
706 |
if (aconf != NULL) |
707 |
{ |
708 |
struct sockaddr_in *v4; |
709 |
#ifdef IPV6 |
710 |
struct sockaddr_in6 *v6; |
711 |
#endif |
712 |
switch (aconf->aftype) |
713 |
{ |
714 |
#ifdef IPV6 |
715 |
case AF_INET6: |
716 |
v6 = (struct sockaddr_in6 *)&aconf->ipnum; |
717 |
|
718 |
if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr)) |
719 |
memcpy(&aconf->ipnum, &client_p->localClient->ip, sizeof(struct irc_ssaddr)); |
720 |
break; |
721 |
#endif |
722 |
case AF_INET: |
723 |
v4 = (struct sockaddr_in *)&aconf->ipnum; |
724 |
|
725 |
if (v4->sin_addr.s_addr == INADDR_NONE) |
726 |
memcpy(&aconf->ipnum, &client_p->localClient->ip, sizeof(struct irc_ssaddr)); |
727 |
break; |
728 |
} |
729 |
} |
730 |
|
731 |
return 0; |
732 |
} |
733 |
|
734 |
/* add_capability() |
735 |
* |
736 |
* inputs - string name of CAPAB |
737 |
* - int flag of capability |
738 |
* output - NONE |
739 |
* side effects - Adds given capability name and bit mask to |
740 |
* current supported capabilities. This allows |
741 |
* modules to dynamically add or subtract their capability. |
742 |
*/ |
743 |
void |
744 |
add_capability(const char *capab_name, int cap_flag, int add_to_default) |
745 |
{ |
746 |
struct Capability *cap; |
747 |
|
748 |
cap = (struct Capability *)MyMalloc(sizeof(*cap)); |
749 |
DupString(cap->name, capab_name); |
750 |
cap->cap = cap_flag; |
751 |
dlinkAdd(cap, &cap->node, &cap_list); |
752 |
if (add_to_default) |
753 |
default_server_capabs |= cap_flag; |
754 |
} |
755 |
|
756 |
/* delete_capability() |
757 |
* |
758 |
* inputs - string name of CAPAB |
759 |
* output - NONE |
760 |
* side effects - delete given capability from ones known. |
761 |
*/ |
762 |
int |
763 |
delete_capability(const char *capab_name) |
764 |
{ |
765 |
dlink_node *ptr; |
766 |
dlink_node *next_ptr; |
767 |
struct Capability *cap; |
768 |
|
769 |
DLINK_FOREACH_SAFE(ptr, next_ptr, cap_list.head) |
770 |
{ |
771 |
cap = ptr->data; |
772 |
|
773 |
if (cap->cap != 0) |
774 |
{ |
775 |
if (!irccmp(cap->name, capab_name)) |
776 |
{ |
777 |
default_server_capabs &= ~(cap->cap); |
778 |
dlinkDelete(ptr, &cap_list); |
779 |
MyFree(cap->name); |
780 |
cap->name = NULL; |
781 |
MyFree(cap); |
782 |
} |
783 |
} |
784 |
} |
785 |
|
786 |
return 0; |
787 |
} |
788 |
|
789 |
/* |
790 |
* find_capability() |
791 |
* |
792 |
* inputs - string name of capab to find |
793 |
* output - 0 if not found CAPAB otherwise |
794 |
* side effects - none |
795 |
*/ |
796 |
int |
797 |
find_capability(const char *capab) |
798 |
{ |
799 |
dlink_node *ptr; |
800 |
struct Capability *cap; |
801 |
|
802 |
DLINK_FOREACH(ptr, cap_list.head) |
803 |
{ |
804 |
cap = ptr->data; |
805 |
|
806 |
if (cap->cap != 0) |
807 |
{ |
808 |
if (!irccmp(cap->name, capab)) |
809 |
return cap->cap; |
810 |
} |
811 |
} |
812 |
|
813 |
return 0; |
814 |
} |
815 |
|
816 |
/* send_capabilities() |
817 |
* |
818 |
* inputs - Client pointer to send to |
819 |
* - Pointer to AccessItem (for crypt) |
820 |
* - int flag of capabilities that this server can send |
821 |
* - int flag of encryption capabilities |
822 |
* output - NONE |
823 |
* side effects - send the CAPAB line to a server -orabidoo |
824 |
* |
825 |
*/ |
826 |
void |
827 |
send_capabilities(struct Client *client_p, struct AccessItem *aconf, |
828 |
int cap_can_send, int enc_can_send) |
829 |
{ |
830 |
struct Capability *cap=NULL; |
831 |
char msgbuf[IRCD_BUFSIZE]; |
832 |
char *t; |
833 |
int tl; |
834 |
dlink_node *ptr; |
835 |
#ifdef HAVE_LIBCRYPTO |
836 |
const struct EncCapability *epref = NULL; |
837 |
char *capend; |
838 |
int sent_cipher = 0; |
839 |
#endif |
840 |
|
841 |
t = msgbuf; |
842 |
|
843 |
DLINK_FOREACH(ptr, cap_list.head) |
844 |
{ |
845 |
cap = ptr->data; |
846 |
|
847 |
if (cap->cap & (cap_can_send|default_server_capabs)) |
848 |
{ |
849 |
tl = ircsprintf(t, "%s ", cap->name); |
850 |
t += tl; |
851 |
} |
852 |
} |
853 |
#ifdef HAVE_LIBCRYPTO |
854 |
if (enc_can_send) |
855 |
{ |
856 |
capend = t; |
857 |
strcpy(t, "ENC:"); |
858 |
t += 4; |
859 |
|
860 |
/* use connect{} specific info if available */ |
861 |
if (aconf->cipher_preference) |
862 |
epref = aconf->cipher_preference; |
863 |
else if (ConfigFileEntry.default_cipher_preference) |
864 |
epref = ConfigFileEntry.default_cipher_preference; |
865 |
|
866 |
if (epref && (epref->cap & enc_can_send)) |
867 |
{ |
868 |
/* Leave the space -- it is removed later. */ |
869 |
tl = ircsprintf(t, "%s ", epref->name); |
870 |
t += tl; |
871 |
sent_cipher = 1; |
872 |
} |
873 |
|
874 |
if (!sent_cipher) |
875 |
t = capend; /* truncate string before ENC:, below */ |
876 |
} |
877 |
#endif |
878 |
*(t - 1) = '\0'; |
879 |
sendto_one(client_p, "CAPAB :%s", msgbuf); |
880 |
} |
881 |
|
882 |
/* sendnick_TS() |
883 |
* |
884 |
* inputs - client (server) to send nick towards |
885 |
* - client to send nick for |
886 |
* output - NONE |
887 |
* side effects - NICK message is sent towards given client_p |
888 |
*/ |
889 |
void |
890 |
sendnick_TS(struct Client *client_p, struct Client *target_p) |
891 |
{ |
892 |
static char ubuf[12]; |
893 |
|
894 |
if (!IsClient(target_p)) |
895 |
return; |
896 |
|
897 |
send_umode(NULL, target_p, 0, IsOperHiddenAdmin(target_p) ? |
898 |
SEND_UMODES & ~UMODE_ADMIN : SEND_UMODES, ubuf); |
899 |
|
900 |
if (ubuf[0] == '\0') |
901 |
{ |
902 |
ubuf[0] = '+'; |
903 |
ubuf[1] = '\0'; |
904 |
} |
905 |
|
906 |
/* XXX Both of these need to have a :me.name or :mySID!?!?! */ |
907 |
if (HasID(target_p) && IsCapable(client_p, CAP_TS6)) |
908 |
sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s :%s", |
909 |
target_p->servptr->id, |
910 |
target_p->name, target_p->hopcount + 1, |
911 |
(unsigned long) target_p->tsinfo, |
912 |
ubuf, target_p->username, target_p->host, |
913 |
((MyClient(target_p)&&!IsIPSpoof(target_p))?target_p->sockhost:"0"), |
914 |
target_p->id, target_p->info); |
915 |
else |
916 |
sendto_one(client_p, "NICK %s %d %lu %s %s %s %s :%s", |
917 |
target_p->name, target_p->hopcount + 1, |
918 |
(unsigned long) target_p->tsinfo, |
919 |
ubuf, target_p->username, target_p->host, |
920 |
target_p->servptr->name, target_p->info); |
921 |
if (IsConfAwayBurst(client_p->serv->sconf)) |
922 |
if (!EmptyString(target_p->away)) |
923 |
sendto_one(client_p, ":%s AWAY :%s", target_p->name, |
924 |
target_p->away); |
925 |
|
926 |
} |
927 |
|
928 |
/* |
929 |
* show_capabilities - show current server capabilities |
930 |
* |
931 |
* inputs - pointer to a struct Client |
932 |
* output - pointer to static string |
933 |
* side effects - build up string representing capabilities of server listed |
934 |
*/ |
935 |
const char * |
936 |
show_capabilities(const struct Client *target_p) |
937 |
{ |
938 |
static char msgbuf[IRCD_BUFSIZE]; |
939 |
char *t = msgbuf; |
940 |
dlink_node *ptr; |
941 |
|
942 |
t += ircsprintf(msgbuf, "TS "); |
943 |
|
944 |
DLINK_FOREACH(ptr, cap_list.head) |
945 |
{ |
946 |
const struct Capability *cap = ptr->data; |
947 |
|
948 |
if (IsCapable(target_p, cap->cap)) |
949 |
t += ircsprintf(t, "%s ", cap->name); |
950 |
} |
951 |
#ifdef HAVE_LIBCRYPTO |
952 |
if (IsCapable(target_p, CAP_ENC) && |
953 |
target_p->localClient->in_cipher && |
954 |
target_p->localClient->out_cipher) |
955 |
t += ircsprintf(t, "ENC:%s ", |
956 |
target_p->localClient->in_cipher->name); |
957 |
#endif |
958 |
*(t - 1) = '\0'; |
959 |
|
960 |
return msgbuf; |
961 |
} |
962 |
|
963 |
/* make_server() |
964 |
* |
965 |
* inputs - pointer to client struct |
966 |
* output - pointer to struct Server |
967 |
* side effects - add's an Server information block to a client |
968 |
* if it was not previously allocated. |
969 |
*/ |
970 |
struct Server * |
971 |
make_server(struct Client *client_p) |
972 |
{ |
973 |
if (client_p->serv == NULL) |
974 |
{ |
975 |
client_p->serv = MyMalloc(sizeof(struct Server)); |
976 |
client_p->serv->dep_servers = 1; |
977 |
} |
978 |
|
979 |
return client_p->serv; |
980 |
} |
981 |
|
982 |
/* server_estab() |
983 |
* |
984 |
* inputs - pointer to a struct Client |
985 |
* output - |
986 |
* side effects - |
987 |
*/ |
988 |
void |
989 |
server_estab(struct Client *client_p) |
990 |
{ |
991 |
struct Client *target_p; |
992 |
struct ConfItem *conf; |
993 |
struct AccessItem *aconf=NULL; |
994 |
char *host; |
995 |
const char *inpath; |
996 |
static char inpath_ip[HOSTLEN * 2 + USERLEN + 6]; |
997 |
dlink_node *m; |
998 |
dlink_node *ptr; |
999 |
|
1000 |
assert(client_p != NULL); |
1001 |
|
1002 |
strlcpy(inpath_ip, get_client_name(client_p, SHOW_IP), sizeof(inpath_ip)); |
1003 |
|
1004 |
inpath = get_client_name(client_p, MASK_IP); /* "refresh" inpath with host */ |
1005 |
host = client_p->name; |
1006 |
|
1007 |
if ((conf = client_p->serv->sconf) == NULL) |
1008 |
{ |
1009 |
/* This shouldn't happen, better tell the ops... -A1kmm */ |
1010 |
sendto_realops_flags(UMODE_ALL, L_ALL, "Warning: Lost connect{} block " |
1011 |
"for server %s(this shouldn't happen)!", host); |
1012 |
exit_client(client_p, &me, "Lost connect{} block!"); |
1013 |
return; |
1014 |
} |
1015 |
|
1016 |
/* XXX */ |
1017 |
aconf = &conf->conf.AccessItem; |
1018 |
attach_class(client_p, aconf->class_ptr); |
1019 |
MyFree(client_p->localClient->passwd); |
1020 |
client_p->localClient->passwd = NULL; |
1021 |
|
1022 |
/* Its got identd, since its a server */ |
1023 |
SetGotId(client_p); |
1024 |
|
1025 |
/* If there is something in the serv_list, it might be this |
1026 |
* connecting server.. |
1027 |
*/ |
1028 |
if (!ServerInfo.hub && serv_list.head) |
1029 |
{ |
1030 |
if (client_p != serv_list.head->data || serv_list.head->next) |
1031 |
{ |
1032 |
++ServerStats.is_ref; |
1033 |
sendto_one(client_p, "ERROR :I'm a leaf not a hub"); |
1034 |
exit_client(client_p, &me, "I'm a leaf"); |
1035 |
return; |
1036 |
} |
1037 |
} |
1038 |
|
1039 |
if (IsUnknown(client_p) && !IsConfCryptLink(aconf)) |
1040 |
{ |
1041 |
/* jdc -- 1. Use EmptyString(), not [0] index reference. |
1042 |
* 2. Check aconf->spasswd, not aconf->passwd. |
1043 |
*/ |
1044 |
if (!EmptyString(aconf->spasswd)) |
1045 |
{ |
1046 |
/* only send ts6 format PASS if we have ts6 enabled */ |
1047 |
if (me.id[0] != '\0') /* Send TS 6 form only if id */ |
1048 |
sendto_one(client_p, "PASS %s TS %d %s", |
1049 |
aconf->spasswd, TS_CURRENT, me.id); |
1050 |
else |
1051 |
sendto_one(client_p, "PASS %s TS 5", |
1052 |
aconf->spasswd); |
1053 |
} |
1054 |
|
1055 |
/* Pass my info to the new server |
1056 |
* |
1057 |
* If this is a HUB, pass on CAP_HUB |
1058 |
* Pass on ZIP if supported |
1059 |
* Pass on TB if supported. |
1060 |
* - Dianora |
1061 |
*/ |
1062 |
|
1063 |
send_capabilities(client_p, aconf, (ServerInfo.hub ? CAP_HUB : 0) |
1064 |
| (IsConfCompressed(aconf) ? CAP_ZIP : 0) |
1065 |
| (IsConfTopicBurst(aconf) ? CAP_TBURST|CAP_TB : 0), 0); |
1066 |
|
1067 |
/* SERVER is the last command sent before switching to ziplinks. |
1068 |
* We set TCPNODELAY on the socket to make sure it gets sent out |
1069 |
* on the wire immediately. Otherwise, it could be sitting in |
1070 |
* a kernel buffer when we start sending zipped data, and the |
1071 |
* parser on the receiving side can't hand both unzipped and zipped |
1072 |
* data in one packet. --Rodder |
1073 |
* |
1074 |
* currently we only need to call send_queued_write, |
1075 |
* Nagle is already disabled at this point --adx |
1076 |
*/ |
1077 |
sendto_one(client_p, "SERVER %s 1 :%s%s", |
1078 |
my_name_for_link(aconf), /* XXX */ |
1079 |
ConfigServerHide.hidden ? "(H) " : "", |
1080 |
(me.info[0]) ? (me.info) : "IRCers United"); |
1081 |
send_queued_write(client_p); |
1082 |
} |
1083 |
|
1084 |
/* Hand the server off to servlink now */ |
1085 |
if (IsCapable(client_p, CAP_ENC) || IsCapable(client_p, CAP_ZIP)) |
1086 |
{ |
1087 |
if (fork_server(client_p) < 0) |
1088 |
{ |
1089 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, |
1090 |
"Warning: fork failed for server %s -- check servlink_path (%s)", |
1091 |
get_client_name(client_p, HIDE_IP), ConfigFileEntry.servlink_path); |
1092 |
sendto_realops_flags(UMODE_ALL, L_OPER, "Warning: fork failed for server " |
1093 |
"%s -- check servlink_path (%s)", |
1094 |
get_client_name(client_p, MASK_IP), |
1095 |
ConfigFileEntry.servlink_path); |
1096 |
exit_client(client_p, &me, "fork failed"); |
1097 |
return; |
1098 |
} |
1099 |
|
1100 |
start_io(client_p); |
1101 |
SetServlink(client_p); |
1102 |
} |
1103 |
|
1104 |
/* only send ts6 format SVINFO if we have ts6 enabled */ |
1105 |
sendto_one(client_p, "SVINFO %d %d 0 :%lu", |
1106 |
(me.id[0] ? TS_CURRENT : 5), TS_MIN, |
1107 |
(unsigned long)CurrentTime); |
1108 |
|
1109 |
/* assumption here is if they passed the correct TS version, they also passed an SID */ |
1110 |
if (IsCapable(client_p, CAP_TS6)) |
1111 |
hash_add_id(client_p); |
1112 |
|
1113 |
/* *WARNING* |
1114 |
** In the following code in place of plain server's |
1115 |
** name we send what is returned by get_client_name |
1116 |
** which may add the "sockhost" after the name. It's |
1117 |
** *very* *important* that there is a SPACE between |
1118 |
** the name and sockhost (if present). The receiving |
1119 |
** server will start the information field from this |
1120 |
** first blank and thus puts the sockhost into info. |
1121 |
** ...a bit tricky, but you have been warned, besides |
1122 |
** code is more neat this way... --msa |
1123 |
*/ |
1124 |
client_p->servptr = &me; |
1125 |
|
1126 |
if (IsClosing(client_p)) |
1127 |
return; |
1128 |
|
1129 |
SetServer(client_p); |
1130 |
|
1131 |
/* Update the capability combination usage counts. -A1kmm */ |
1132 |
set_chcap_usage_counts(client_p); |
1133 |
|
1134 |
dlinkAdd(client_p, &client_p->lnode, &me.serv->servers); |
1135 |
|
1136 |
m = dlinkFind(&unknown_list, client_p); |
1137 |
assert(NULL != m); |
1138 |
|
1139 |
dlinkDelete(m, &unknown_list); |
1140 |
dlinkAdd(client_p, m, &serv_list); |
1141 |
|
1142 |
Count.myserver++; |
1143 |
|
1144 |
dlinkAdd(client_p, make_dlink_node(), &global_serv_list); |
1145 |
hash_add_client(client_p); |
1146 |
|
1147 |
/* doesnt duplicate client_p->serv if this struct is allocated already */ |
1148 |
make_server(client_p); |
1149 |
|
1150 |
/* fixing eob timings.. -gnp */ |
1151 |
client_p->firsttime = CurrentTime; |
1152 |
|
1153 |
/* Show the real host/IP to admins */ |
1154 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, |
1155 |
"Link with %s established: (%s) link", |
1156 |
inpath_ip,show_capabilities(client_p)); |
1157 |
/* Now show the masked hostname/IP to opers */ |
1158 |
sendto_realops_flags(UMODE_ALL, L_OPER, |
1159 |
"Link with %s established: (%s) link", |
1160 |
inpath,show_capabilities(client_p)); |
1161 |
ilog(L_NOTICE, "Link with %s established: (%s) link", |
1162 |
inpath_ip, show_capabilities(client_p)); |
1163 |
|
1164 |
/* XXX */ |
1165 |
attach_server_conf(client_p, conf); |
1166 |
|
1167 |
if (HasServlink(client_p)) |
1168 |
{ |
1169 |
/* we won't overflow FD_DESC_SZ here, as it can hold |
1170 |
* client_p->name + 64 |
1171 |
*/ |
1172 |
fd_note(&client_p->localClient->fd, "slink data: %s", client_p->name); |
1173 |
fd_note(&client_p->localClient->ctrlfd, "slink ctrl: %s", client_p->name); |
1174 |
} |
1175 |
else |
1176 |
fd_note(&client_p->localClient->fd, "Server: %s", client_p->name); |
1177 |
|
1178 |
/* Old sendto_serv_but_one() call removed because we now |
1179 |
** need to send different names to different servers |
1180 |
** (domain name matching) Send new server to other servers. |
1181 |
*/ |
1182 |
DLINK_FOREACH(ptr, serv_list.head) |
1183 |
{ |
1184 |
target_p = ptr->data; |
1185 |
|
1186 |
if (target_p == client_p) |
1187 |
continue; |
1188 |
|
1189 |
if ((conf = target_p->serv->sconf) && |
1190 |
match(my_name_for_link(&conf->conf.AccessItem), client_p->name)) |
1191 |
continue; |
1192 |
|
1193 |
if (IsCapable(target_p, CAP_TS6) && HasID(client_p)) |
1194 |
sendto_one(target_p, ":%s SID %s 2 %s :%s%s", |
1195 |
me.id, client_p->name, client_p->id, |
1196 |
IsHidden(client_p) ? "(H) " : "", |
1197 |
client_p->info); |
1198 |
else |
1199 |
sendto_one(target_p,":%s SERVER %s 2 :%s%s", |
1200 |
me.name, client_p->name, |
1201 |
IsHidden(client_p) ? "(H) " : "", |
1202 |
client_p->info); |
1203 |
} |
1204 |
|
1205 |
/* Pass on my client information to the new server |
1206 |
** |
1207 |
** First, pass only servers (idea is that if the link gets |
1208 |
** cancelled beacause the server was already there, |
1209 |
** there are no NICK's to be cancelled...). Of course, |
1210 |
** if cancellation occurs, all this info is sent anyway, |
1211 |
** and I guess the link dies when a read is attempted...? --msa |
1212 |
** |
1213 |
** Note: Link cancellation to occur at this point means |
1214 |
** that at least two servers from my fragment are building |
1215 |
** up connection this other fragment at the same time, it's |
1216 |
** a race condition, not the normal way of operation... |
1217 |
** |
1218 |
** ALSO NOTE: using the get_client_name for server names-- |
1219 |
** see previous *WARNING*!!! (Also, original inpath |
1220 |
** is destroyed...) |
1221 |
*/ |
1222 |
|
1223 |
conf = client_p->serv->sconf; |
1224 |
|
1225 |
DLINK_FOREACH_PREV(ptr, global_serv_list.tail) |
1226 |
{ |
1227 |
target_p = ptr->data; |
1228 |
|
1229 |
/* target_p->from == target_p for target_p == client_p */ |
1230 |
if (target_p->from == client_p) |
1231 |
continue; |
1232 |
|
1233 |
if (match(my_name_for_link(&conf->conf.AccessItem), target_p->name)) |
1234 |
continue; |
1235 |
|
1236 |
if (IsCapable(client_p, CAP_TS6)) |
1237 |
{ |
1238 |
if (HasID(target_p)) |
1239 |
sendto_one(client_p, ":%s SID %s %d %s :%s%s", |
1240 |
ID(target_p->servptr), target_p->name, target_p->hopcount+1, |
1241 |
target_p->id, IsHidden(target_p) ? "(H) " : "", |
1242 |
target_p->info); |
1243 |
else /* introducing non-ts6 server */ |
1244 |
sendto_one(client_p, ":%s SERVER %s %d :%s%s", |
1245 |
ID(target_p->servptr), target_p->name, target_p->hopcount+1, |
1246 |
IsHidden(target_p) ? "(H) " : "", target_p->info); |
1247 |
} |
1248 |
else |
1249 |
sendto_one(client_p, ":%s SERVER %s %d :%s%s", |
1250 |
target_p->servptr->name, target_p->name, target_p->hopcount+1, |
1251 |
IsHidden(target_p) ? "(H) " : "", target_p->info); |
1252 |
} |
1253 |
|
1254 |
server_burst(client_p); |
1255 |
} |
1256 |
|
1257 |
static void |
1258 |
start_io(struct Client *server) |
1259 |
{ |
1260 |
struct LocalUser *lserver = server->localClient; |
1261 |
int alloclen = 1; |
1262 |
char *buf; |
1263 |
dlink_node *ptr; |
1264 |
struct dbuf_block *block; |
1265 |
|
1266 |
/* calculate how many bytes to allocate */ |
1267 |
if (IsCapable(server, CAP_ZIP)) |
1268 |
alloclen += 6; |
1269 |
#ifdef HAVE_LIBCRYPTO |
1270 |
if (IsCapable(server, CAP_ENC)) |
1271 |
alloclen += 16 + lserver->in_cipher->keylen + lserver->out_cipher->keylen; |
1272 |
#endif |
1273 |
alloclen += dbuf_length(&lserver->buf_recvq); |
1274 |
alloclen += dlink_list_length(&lserver->buf_recvq.blocks) * 3; |
1275 |
alloclen += dbuf_length(&lserver->buf_sendq); |
1276 |
alloclen += dlink_list_length(&lserver->buf_sendq.blocks) * 3; |
1277 |
|
1278 |
/* initialize servlink control sendq */ |
1279 |
lserver->slinkq = buf = MyMalloc(alloclen); |
1280 |
lserver->slinkq_ofs = 0; |
1281 |
lserver->slinkq_len = alloclen; |
1282 |
|
1283 |
if (IsCapable(server, CAP_ZIP)) |
1284 |
{ |
1285 |
/* ziplink */ |
1286 |
*buf++ = SLINKCMD_SET_ZIP_OUT_LEVEL; |
1287 |
*buf++ = 0; /* | */ |
1288 |
*buf++ = 1; /* \ len is 1 */ |
1289 |
*buf++ = ConfigFileEntry.compression_level; |
1290 |
*buf++ = SLINKCMD_START_ZIP_IN; |
1291 |
*buf++ = SLINKCMD_START_ZIP_OUT; |
1292 |
} |
1293 |
#ifdef HAVE_LIBCRYPTO |
1294 |
if (IsCapable(server, CAP_ENC)) |
1295 |
{ |
1296 |
/* Decryption settings */ |
1297 |
*buf++ = SLINKCMD_SET_CRYPT_IN_CIPHER; |
1298 |
*buf++ = 0; /* / (upper 8-bits of len) */ |
1299 |
*buf++ = 1; /* \ cipher id is 1 byte (lower 8-bits of len) */ |
1300 |
*buf++ = lserver->in_cipher->cipherid; |
1301 |
*buf++ = SLINKCMD_SET_CRYPT_IN_KEY; |
1302 |
*buf++ = 0; /* keylen < 256 */ |
1303 |
*buf++ = lserver->in_cipher->keylen; |
1304 |
memcpy(buf, lserver->in_key, lserver->in_cipher->keylen); |
1305 |
buf += lserver->in_cipher->keylen; |
1306 |
/* Encryption settings */ |
1307 |
*buf++ = SLINKCMD_SET_CRYPT_OUT_CIPHER; |
1308 |
*buf++ = 0; /* / (upper 8-bits of len) */ |
1309 |
*buf++ = 1; /* \ cipher id is 1 byte (lower 8-bits of len) */ |
1310 |
*buf++ = lserver->out_cipher->cipherid; |
1311 |
*buf++ = SLINKCMD_SET_CRYPT_OUT_KEY; |
1312 |
*buf++ = 0; /* keylen < 256 */ |
1313 |
*buf++ = lserver->out_cipher->keylen; |
1314 |
memcpy(buf, lserver->out_key, lserver->out_cipher->keylen); |
1315 |
buf += lserver->out_cipher->keylen; |
1316 |
*buf++ = SLINKCMD_START_CRYPT_IN; |
1317 |
*buf++ = SLINKCMD_START_CRYPT_OUT; |
1318 |
} |
1319 |
#endif |
1320 |
|
1321 |
/* pass the whole recvq to servlink */ |
1322 |
DLINK_FOREACH (ptr, lserver->buf_recvq.blocks.head) |
1323 |
{ |
1324 |
block = ptr->data; |
1325 |
*buf++ = SLINKCMD_INJECT_RECVQ; |
1326 |
*buf++ = (block->size >> 8); |
1327 |
*buf++ = (block->size & 0xff); |
1328 |
memcpy(buf, &block->data[0], block->size); |
1329 |
buf += block->size; |
1330 |
} |
1331 |
dbuf_clear(&lserver->buf_recvq); |
1332 |
|
1333 |
/* pass the whole sendq to servlink */ |
1334 |
DLINK_FOREACH (ptr, lserver->buf_sendq.blocks.head) |
1335 |
{ |
1336 |
block = ptr->data; |
1337 |
*buf++ = SLINKCMD_INJECT_SENDQ; |
1338 |
*buf++ = (block->size >> 8); |
1339 |
*buf++ = (block->size & 0xff); |
1340 |
memcpy(buf, &block->data[0], block->size); |
1341 |
buf += block->size; |
1342 |
} |
1343 |
dbuf_clear(&lserver->buf_sendq); |
1344 |
|
1345 |
/* start io */ |
1346 |
*buf++ = SLINKCMD_INIT; |
1347 |
|
1348 |
/* schedule a write */ |
1349 |
send_queued_slink_write(server); |
1350 |
} |
1351 |
|
1352 |
/* fork_server() |
1353 |
* |
1354 |
* inputs - struct Client *server |
1355 |
* output - success: 0 / failure: -1 |
1356 |
* side effect - fork, and exec SERVLINK to handle this connection |
1357 |
*/ |
1358 |
static int |
1359 |
fork_server(struct Client *server) |
1360 |
{ |
1361 |
#ifndef HAVE_SOCKETPAIR |
1362 |
return -1; |
1363 |
#else |
1364 |
int i; |
1365 |
int slink_fds[2][2]; |
1366 |
/* 0? - ctrl | 1? - data |
1367 |
* ?0 - child | ?1 - parent */ |
1368 |
|
1369 |
if (socketpair(AF_UNIX, SOCK_STREAM, 0, slink_fds[0]) < 0) |
1370 |
return -1; |
1371 |
if (socketpair(AF_UNIX, SOCK_STREAM, 0, slink_fds[1]) < 0) |
1372 |
goto free_ctrl_fds; |
1373 |
|
1374 |
if ((i = fork()) < 0) |
1375 |
{ |
1376 |
close(slink_fds[1][0]); close(slink_fds[1][1]); |
1377 |
free_ctrl_fds: |
1378 |
close(slink_fds[0][0]); close(slink_fds[0][1]); |
1379 |
return -1; |
1380 |
} |
1381 |
|
1382 |
if (i == 0) |
1383 |
{ |
1384 |
char fd_str[3][6]; /* store 3x sizeof("65535") */ |
1385 |
char *kid_argv[7]; |
1386 |
|
1387 |
#ifdef O_ASYNC |
1388 |
fcntl(server->localClient->fd.fd, F_SETFL, |
1389 |
fcntl(server->localClient->fd.fd, F_GETFL, 0) & ~O_ASYNC); |
1390 |
#endif |
1391 |
close_fds(&server->localClient->fd); |
1392 |
close(slink_fds[0][1]); |
1393 |
close(slink_fds[1][1]); |
1394 |
|
1395 |
sprintf(fd_str[0], "%d", slink_fds[0][0]); |
1396 |
sprintf(fd_str[1], "%d", slink_fds[1][0]); |
1397 |
sprintf(fd_str[2], "%d", server->localClient->fd.fd); |
1398 |
|
1399 |
kid_argv[0] = "-slink"; |
1400 |
kid_argv[1] = kid_argv[2] = fd_str[0]; /* ctrl */ |
1401 |
kid_argv[3] = kid_argv[4] = fd_str[1]; /* data */ |
1402 |
kid_argv[5] = fd_str[2]; /* network */ |
1403 |
kid_argv[6] = NULL; |
1404 |
|
1405 |
execv(ConfigFileEntry.servlink_path, kid_argv); |
1406 |
|
1407 |
_exit(1); |
1408 |
} |
1409 |
|
1410 |
/* close the network fd and the child ends of the pipes */ |
1411 |
fd_close(&server->localClient->fd); |
1412 |
close(slink_fds[0][0]); |
1413 |
close(slink_fds[1][0]); |
1414 |
|
1415 |
execute_callback(setup_socket_cb, slink_fds[0][1]); |
1416 |
execute_callback(setup_socket_cb, slink_fds[1][1]); |
1417 |
|
1418 |
fd_open(&server->localClient->ctrlfd, slink_fds[0][1], 1, "slink ctrl"); |
1419 |
fd_open(&server->localClient->fd, slink_fds[1][1], 1, "slink data"); |
1420 |
|
1421 |
read_ctrl_packet(&server->localClient->ctrlfd, server); |
1422 |
read_packet(&server->localClient->fd, server); |
1423 |
|
1424 |
return 0; |
1425 |
#endif |
1426 |
} |
1427 |
|
1428 |
/* server_burst() |
1429 |
* |
1430 |
* inputs - struct Client pointer server |
1431 |
* - |
1432 |
* output - none |
1433 |
* side effects - send a server burst |
1434 |
* bugs - still too long |
1435 |
*/ |
1436 |
static void |
1437 |
server_burst(struct Client *client_p) |
1438 |
{ |
1439 |
/* Send it in the shortened format with the TS, if |
1440 |
** it's a TS server; walk the list of channels, sending |
1441 |
** all the nicks that haven't been sent yet for each |
1442 |
** channel, then send the channel itself -- it's less |
1443 |
** obvious than sending all nicks first, but on the |
1444 |
** receiving side memory will be allocated more nicely |
1445 |
** saving a few seconds in the handling of a split |
1446 |
** -orabidoo |
1447 |
*/ |
1448 |
burst_all(client_p); |
1449 |
|
1450 |
/* EOB stuff is now in burst_all */ |
1451 |
/* Always send a PING after connect burst is done */ |
1452 |
sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p)); |
1453 |
} |
1454 |
|
1455 |
/* burst_all() |
1456 |
* |
1457 |
* inputs - pointer to server to send burst to |
1458 |
* output - NONE |
1459 |
* side effects - complete burst of channels/nicks is sent to client_p |
1460 |
*/ |
1461 |
static void |
1462 |
burst_all(struct Client *client_p) |
1463 |
{ |
1464 |
dlink_node *ptr = NULL; |
1465 |
|
1466 |
DLINK_FOREACH(ptr, global_channel_list.head) |
1467 |
{ |
1468 |
struct Channel *chptr = ptr->data; |
1469 |
|
1470 |
if (dlink_list_length(&chptr->members) != 0) |
1471 |
{ |
1472 |
burst_members(client_p, chptr); |
1473 |
send_channel_modes(client_p, chptr); |
1474 |
|
1475 |
if (IsCapable(client_p, CAP_TBURST) || |
1476 |
IsCapable(client_p, CAP_TB)) |
1477 |
send_tb(client_p, chptr); |
1478 |
} |
1479 |
} |
1480 |
|
1481 |
/* also send out those that are not on any channel |
1482 |
*/ |
1483 |
DLINK_FOREACH(ptr, global_client_list.head) |
1484 |
{ |
1485 |
struct Client *target_p = ptr->data; |
1486 |
|
1487 |
if (!IsBursted(target_p) && target_p->from != client_p) |
1488 |
sendnick_TS(client_p, target_p); |
1489 |
|
1490 |
ClearBursted(target_p); |
1491 |
} |
1492 |
|
1493 |
/* We send the time we started the burst, and let the remote host determine an EOB time, |
1494 |
** as otherwise we end up sending a EOB of 0 Sending here means it gets sent last -- fl |
1495 |
*/ |
1496 |
/* Its simpler to just send EOB and use the time its been connected.. --fl_ */ |
1497 |
if (IsCapable(client_p, CAP_EOB)) |
1498 |
sendto_one(client_p, ":%s EOB", ID_or_name(&me, client_p)); |
1499 |
} |
1500 |
|
1501 |
/* |
1502 |
* send_tb |
1503 |
* |
1504 |
* inputs - pointer to Client |
1505 |
* - pointer to channel |
1506 |
* output - NONE |
1507 |
* side effects - Called on a server burst when |
1508 |
* server is CAP_TB|CAP_TBURST capable |
1509 |
*/ |
1510 |
static void |
1511 |
send_tb(struct Client *client_p, const struct Channel *chptr) |
1512 |
{ |
1513 |
/* |
1514 |
* XXX - Logic here isn't right either. What if the topic got unset? |
1515 |
* We would need to send an empty TOPIC to reset the topic from the |
1516 |
* other side |
1517 |
*/ |
1518 |
if (chptr->topic != NULL) |
1519 |
{ |
1520 |
if (IsCapable(client_p, CAP_TBURST)) |
1521 |
sendto_one(client_p, ":%s TBURST %lu %s %lu %s :%s", |
1522 |
me.name, (unsigned long)chptr->channelts, chptr->chname, |
1523 |
(unsigned long)chptr->topic_time, chptr->topic_info, |
1524 |
chptr->topic); |
1525 |
else if (IsCapable(client_p, CAP_TB)) |
1526 |
{ |
1527 |
if (ConfigChannel.burst_topicwho) |
1528 |
{ |
1529 |
sendto_one(client_p, ":%s TB %s %lu %s :%s", |
1530 |
me.name, chptr->chname, |
1531 |
(unsigned long)chptr->topic_time, |
1532 |
chptr->topic_info, chptr->topic); |
1533 |
} |
1534 |
else |
1535 |
{ |
1536 |
sendto_one(client_p, ":%s TB %s %lu :%s", |
1537 |
me.name, chptr->chname, |
1538 |
(unsigned long)chptr->topic_time, chptr->topic); |
1539 |
} |
1540 |
} |
1541 |
} |
1542 |
} |
1543 |
|
1544 |
/* burst_members() |
1545 |
* |
1546 |
* inputs - pointer to server to send members to |
1547 |
* - dlink_list pointer to membership list to send |
1548 |
* output - NONE |
1549 |
* side effects - |
1550 |
*/ |
1551 |
static void |
1552 |
burst_members(struct Client *client_p, struct Channel *chptr) |
1553 |
{ |
1554 |
struct Client *target_p; |
1555 |
struct Membership *ms; |
1556 |
dlink_node *ptr; |
1557 |
|
1558 |
DLINK_FOREACH(ptr, chptr->members.head) |
1559 |
{ |
1560 |
ms = ptr->data; |
1561 |
target_p = ms->client_p; |
1562 |
|
1563 |
if (!IsBursted(target_p)) |
1564 |
{ |
1565 |
SetBursted(target_p); |
1566 |
|
1567 |
if (target_p->from != client_p) |
1568 |
sendnick_TS(client_p, target_p); |
1569 |
} |
1570 |
} |
1571 |
} |
1572 |
|
1573 |
/* New server connection code |
1574 |
* Based upon the stuff floating about in s_bsd.c |
1575 |
* -- adrian |
1576 |
*/ |
1577 |
|
1578 |
/* serv_connect() - initiate a server connection |
1579 |
* |
1580 |
* inputs - pointer to conf |
1581 |
* - pointer to client doing the connect |
1582 |
* output - |
1583 |
* side effects - |
1584 |
* |
1585 |
* This code initiates a connection to a server. It first checks to make |
1586 |
* sure the given server exists. If this is the case, it creates a socket, |
1587 |
* creates a client, saves the socket information in the client, and |
1588 |
* initiates a connection to the server through comm_connect_tcp(). The |
1589 |
* completion of this goes through serv_completed_connection(). |
1590 |
* |
1591 |
* We return 1 if the connection is attempted, since we don't know whether |
1592 |
* it suceeded or not, and 0 if it fails in here somewhere. |
1593 |
*/ |
1594 |
int |
1595 |
serv_connect(struct AccessItem *aconf, struct Client *by, int connect_port) |
1596 |
{ |
1597 |
struct ConfItem *conf = NULL; |
1598 |
struct Client *client_p = NULL; |
1599 |
char buf[HOSTIPLEN] = { '\0' }; |
1600 |
|
1601 |
/* conversion structs */ |
1602 |
struct sockaddr_in *v4; |
1603 |
/* Make sure aconf is useful */ |
1604 |
assert(aconf != NULL); |
1605 |
|
1606 |
conf = aconf->conf_ptr; |
1607 |
|
1608 |
irc_getnameinfo((struct sockaddr*)&aconf->ipnum, aconf->ipnum.ss_len, |
1609 |
buf, HOSTIPLEN, NULL, 0, NI_NUMERICHOST); |
1610 |
ilog(L_NOTICE, "Connect to %s[%s] @%s", aconf->user, aconf->host, |
1611 |
buf); |
1612 |
|
1613 |
/* Still processing a DNS lookup? -> exit */ |
1614 |
if (aconf->dns_query != NULL) |
1615 |
{ |
1616 |
sendto_realops_flags(UMODE_ALL, L_OPER, |
1617 |
"Error connecting to %s: Error during DNS lookup", conf->name); |
1618 |
return 0; |
1619 |
} |
1620 |
|
1621 |
/* |
1622 |
* Make sure this server isn't already connected |
1623 |
* Note: aconf should ALWAYS be a valid C: line |
1624 |
*/ |
1625 |
if ((client_p = find_server(conf->name)) != NULL) |
1626 |
{ |
1627 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, |
1628 |
"Server %s already present from %s", |
1629 |
conf->name, get_client_name(client_p, SHOW_IP)); |
1630 |
sendto_realops_flags(UMODE_ALL, L_OPER, |
1631 |
"Server %s already present from %s", |
1632 |
conf->name, get_client_name(client_p, MASK_IP)); |
1633 |
/* |
1634 |
* 'by' can be a NULL pointer if called by try_connections() |
1635 |
*/ |
1636 |
if (by && IsClient(by) && !MyClient(by)) |
1637 |
sendto_one(by, ":%s NOTICE %s :Server %s already present from %s", |
1638 |
me.name, by->name, conf->name, |
1639 |
get_client_name(client_p, MASK_IP)); |
1640 |
return 0; |
1641 |
} |
1642 |
|
1643 |
/* Create a local client */ |
1644 |
client_p = make_client(NULL); |
1645 |
|
1646 |
/* Copy in the server, hostname, fd */ |
1647 |
strlcpy(client_p->name, conf->name, sizeof(client_p->name)); |
1648 |
strlcpy(client_p->host, aconf->host, sizeof(client_p->host)); |
1649 |
|
1650 |
/* We already converted the ip once, so lets use it - stu */ |
1651 |
strlcpy(client_p->sockhost, buf, HOSTIPLEN); |
1652 |
|
1653 |
/* create a socket for the server connection */ |
1654 |
if (comm_open(&client_p->localClient->fd, aconf->ipnum.ss.ss_family, |
1655 |
SOCK_STREAM, 0, NULL) < 0) |
1656 |
{ |
1657 |
/* Eek, failure to create the socket */ |
1658 |
report_error(L_ALL, |
1659 |
"opening stream socket to %s: %s", conf->name, errno); |
1660 |
SetDead(client_p); |
1661 |
exit_client(client_p, &me, "Connection failed"); |
1662 |
return 0; |
1663 |
} |
1664 |
|
1665 |
/* servernames are always guaranteed under HOSTLEN chars */ |
1666 |
fd_note(&client_p->localClient->fd, "Server: %s", conf->name); |
1667 |
|
1668 |
/* Attach config entries to client here rather than in |
1669 |
* serv_connect_callback(). This to avoid null pointer references. |
1670 |
*/ |
1671 |
|
1672 |
/* make_server() has to be called here to have something to attach to */ |
1673 |
|
1674 |
make_server(client_p); |
1675 |
|
1676 |
if (!attach_connect_block(client_p, conf->name, aconf->host)) |
1677 |
{ |
1678 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
1679 |
"Host %s is not enabled for connecting:no connect-block", |
1680 |
conf->name); |
1681 |
if (by && IsClient(by) && !MyClient(by)) |
1682 |
sendto_one(by, ":%s NOTICE %s :Connect to host %s failed.", |
1683 |
me.name, by->name, client_p->name); |
1684 |
SetDead(client_p); |
1685 |
exit_client(client_p, client_p, "Connection failed"); |
1686 |
return 0; |
1687 |
} |
1688 |
|
1689 |
/* at this point we have a connection in progress and a connect block |
1690 |
* attached to the client, the socket info should be saved in the |
1691 |
* client and it should either be resolved or have a valid address. |
1692 |
* |
1693 |
* The socket has been connected or connect is in progress. |
1694 |
*/ |
1695 |
|
1696 |
if (by && IsClient(by)) |
1697 |
strlcpy(client_p->serv->by, by->name, sizeof(client_p->serv->by)); |
1698 |
else |
1699 |
strlcpy(client_p->serv->by, "AutoConn.", sizeof(client_p->serv->by)); |
1700 |
|
1701 |
SetConnecting(client_p); |
1702 |
dlinkAdd(client_p, &client_p->node, &global_client_list); |
1703 |
/* from def_fam */ |
1704 |
client_p->localClient->aftype = aconf->aftype; |
1705 |
|
1706 |
/* Now, initiate the connection */ |
1707 |
/* XXX assume that a non 0 type means a specific bind address |
1708 |
* for this connect. |
1709 |
*/ |
1710 |
switch (aconf->aftype) |
1711 |
{ |
1712 |
case AF_INET: |
1713 |
v4 = (struct sockaddr_in*)&aconf->my_ipnum; |
1714 |
if (v4->sin_addr.s_addr != 0) |
1715 |
{ |
1716 |
struct irc_ssaddr ipn; |
1717 |
memset(&ipn, 0, sizeof(struct irc_ssaddr)); |
1718 |
ipn.ss.ss_family = AF_INET; |
1719 |
ipn.ss_port = 0; |
1720 |
memcpy(&ipn, &aconf->my_ipnum, sizeof(struct irc_ssaddr)); |
1721 |
comm_connect_tcp(&client_p->localClient->fd, aconf->host, connect_port, |
1722 |
(struct sockaddr *)&ipn, ipn.ss_len, |
1723 |
serv_connect_callback, client_p, aconf->aftype, |
1724 |
CONNECTTIMEOUT); |
1725 |
} |
1726 |
else if (ServerInfo.specific_ipv4_vhost) |
1727 |
{ |
1728 |
struct irc_ssaddr ipn; |
1729 |
memset(&ipn, 0, sizeof(struct irc_ssaddr)); |
1730 |
ipn.ss.ss_family = AF_INET; |
1731 |
ipn.ss_port = 0; |
1732 |
memcpy(&ipn, &ServerInfo.ip, sizeof(struct irc_ssaddr)); |
1733 |
comm_connect_tcp(&client_p->localClient->fd, aconf->host, connect_port, |
1734 |
(struct sockaddr *)&ipn, ipn.ss_len, |
1735 |
serv_connect_callback, client_p, aconf->aftype, |
1736 |
CONNECTTIMEOUT); |
1737 |
} |
1738 |
else |
1739 |
comm_connect_tcp(&client_p->localClient->fd, aconf->host, connect_port, |
1740 |
NULL, 0, serv_connect_callback, client_p, aconf->aftype, |
1741 |
CONNECTTIMEOUT); |
1742 |
break; |
1743 |
#ifdef IPV6 |
1744 |
case AF_INET6: |
1745 |
{ |
1746 |
struct irc_ssaddr ipn; |
1747 |
struct sockaddr_in6 *v6; |
1748 |
struct sockaddr_in6 *v6conf; |
1749 |
|
1750 |
memset(&ipn, 0, sizeof(struct irc_ssaddr)); |
1751 |
v6conf = (struct sockaddr_in6 *)&aconf->my_ipnum; |
1752 |
v6 = (struct sockaddr_in6 *)&ipn; |
1753 |
|
1754 |
if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr, |
1755 |
sizeof(struct in6_addr)) != 0) |
1756 |
{ |
1757 |
memcpy(&ipn, &aconf->my_ipnum, sizeof(struct irc_ssaddr)); |
1758 |
ipn.ss.ss_family = AF_INET6; |
1759 |
ipn.ss_port = 0; |
1760 |
comm_connect_tcp(&client_p->localClient->fd, |
1761 |
aconf->host, connect_port, |
1762 |
(struct sockaddr *)&ipn, ipn.ss_len, |
1763 |
serv_connect_callback, client_p, |
1764 |
aconf->aftype, CONNECTTIMEOUT); |
1765 |
} |
1766 |
else if (ServerInfo.specific_ipv6_vhost) |
1767 |
{ |
1768 |
memcpy(&ipn, &ServerInfo.ip6, sizeof(struct irc_ssaddr)); |
1769 |
ipn.ss.ss_family = AF_INET6; |
1770 |
ipn.ss_port = 0; |
1771 |
comm_connect_tcp(&client_p->localClient->fd, |
1772 |
aconf->host, connect_port, |
1773 |
(struct sockaddr *)&ipn, ipn.ss_len, |
1774 |
serv_connect_callback, client_p, |
1775 |
aconf->aftype, CONNECTTIMEOUT); |
1776 |
} |
1777 |
else |
1778 |
comm_connect_tcp(&client_p->localClient->fd, |
1779 |
aconf->host, connect_port, |
1780 |
NULL, 0, serv_connect_callback, client_p, |
1781 |
aconf->aftype, CONNECTTIMEOUT); |
1782 |
} |
1783 |
#endif |
1784 |
} |
1785 |
|
1786 |
return 1; |
1787 |
} |
1788 |
|
1789 |
/* serv_connect_callback() - complete a server connection. |
1790 |
* |
1791 |
* This routine is called after the server connection attempt has |
1792 |
* completed. If unsucessful, an error is sent to ops and the client |
1793 |
* is closed. If sucessful, it goes through the initialisation/check |
1794 |
* procedures, the capabilities are sent, and the socket is then |
1795 |
* marked for reading. |
1796 |
*/ |
1797 |
static void |
1798 |
serv_connect_callback(fde_t *fd, int status, void *data) |
1799 |
{ |
1800 |
struct Client *client_p = data; |
1801 |
struct ConfItem *conf; |
1802 |
struct AccessItem *aconf=NULL; |
1803 |
|
1804 |
/* First, make sure its a real client! */ |
1805 |
assert(client_p != NULL); |
1806 |
assert(&client_p->localClient->fd == fd); |
1807 |
|
1808 |
/* Next, for backward purposes, record the ip of the server */ |
1809 |
memcpy(&client_p->localClient->ip, &fd->connect.hostaddr, |
1810 |
sizeof(struct irc_ssaddr)); |
1811 |
/* Check the status */ |
1812 |
if (status != COMM_OK) |
1813 |
{ |
1814 |
/* We have an error, so report it and quit |
1815 |
* Admins get to see any IP, mere opers don't *sigh* |
1816 |
*/ |
1817 |
if (ConfigServerHide.hide_server_ips) |
1818 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, |
1819 |
"Error connecting to %s: %s", |
1820 |
client_p->name, comm_errstr(status)); |
1821 |
else |
1822 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, |
1823 |
"Error connecting to %s[%s]: %s", client_p->name, |
1824 |
client_p->host, comm_errstr(status)); |
1825 |
|
1826 |
sendto_realops_flags(UMODE_ALL, L_OPER, |
1827 |
"Error connecting to %s: %s", |
1828 |
client_p->name, comm_errstr(status)); |
1829 |
|
1830 |
/* If a fd goes bad, call dead_link() the socket is no |
1831 |
* longer valid for reading or writing. |
1832 |
*/ |
1833 |
dead_link_on_write(client_p, 0); |
1834 |
return; |
1835 |
} |
1836 |
|
1837 |
/* COMM_OK, so continue the connection procedure */ |
1838 |
/* Get the connect block */ |
1839 |
conf = client_p->serv->sconf; |
1840 |
if (conf == NULL) |
1841 |
{ |
1842 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, |
1843 |
"Lost connect{} block for %s", get_client_name(client_p, HIDE_IP)); |
1844 |
sendto_realops_flags(UMODE_ALL, L_OPER, |
1845 |
"Lost connect{} block for %s", get_client_name(client_p, MASK_IP)); |
1846 |
|
1847 |
exit_client(client_p, &me, "Lost connect{} block"); |
1848 |
return; |
1849 |
} |
1850 |
|
1851 |
aconf = &conf->conf.AccessItem; |
1852 |
|
1853 |
/* Next, send the initial handshake */ |
1854 |
SetHandshake(client_p); |
1855 |
|
1856 |
#ifdef HAVE_LIBCRYPTO |
1857 |
/* Handle all CRYPTLINK links in cryptlink_init */ |
1858 |
if (IsConfCryptLink(aconf)) |
1859 |
{ |
1860 |
cryptlink_init(client_p, &conf->conf.AccessItem, fd); |
1861 |
return; |
1862 |
} |
1863 |
#endif |
1864 |
|
1865 |
/* jdc -- Check and send spasswd, not passwd. */ |
1866 |
if (!EmptyString(aconf->spasswd) && (me.id[0] != '\0')) |
1867 |
/* Send TS 6 form only if id */ |
1868 |
sendto_one(client_p, "PASS %s TS %d %s", |
1869 |
aconf->spasswd, TS_CURRENT, me.id); |
1870 |
else |
1871 |
sendto_one(client_p, "PASS %s TS 5", |
1872 |
aconf->spasswd); |
1873 |
|
1874 |
/* Pass my info to the new server |
1875 |
* |
1876 |
* If this is a HUB, pass on CAP_HUB |
1877 |
* Pass on ZIP if supported |
1878 |
* Pass on TB if supported. |
1879 |
* - Dianora |
1880 |
*/ |
1881 |
send_capabilities(client_p, aconf, (ServerInfo.hub ? CAP_HUB : 0) |
1882 |
| (IsConfCompressed(aconf) ? CAP_ZIP : 0) |
1883 |
| (IsConfTopicBurst(aconf) ? CAP_TBURST|CAP_TB : 0), 0); |
1884 |
|
1885 |
sendto_one(client_p, "SERVER %s 1 :%s%s", |
1886 |
my_name_for_link(aconf), /* XXX */ |
1887 |
ConfigServerHide.hidden ? "(H) " : "", |
1888 |
me.info); |
1889 |
|
1890 |
/* If we've been marked dead because a send failed, just exit |
1891 |
* here now and save everyone the trouble of us ever existing. |
1892 |
*/ |
1893 |
if (IsDead(client_p)) |
1894 |
{ |
1895 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, |
1896 |
"%s[%s] went dead during handshake", |
1897 |
client_p->name, |
1898 |
client_p->host); |
1899 |
sendto_realops_flags(UMODE_ALL, L_OPER, |
1900 |
"%s went dead during handshake", client_p->name); |
1901 |
return; |
1902 |
} |
1903 |
|
1904 |
/* don't move to serv_list yet -- we haven't sent a burst! */ |
1905 |
/* If we get here, we're ok, so lets start reading some data */ |
1906 |
comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0); |
1907 |
} |
1908 |
|
1909 |
struct Client * |
1910 |
find_servconn_in_progress(const char *name) |
1911 |
{ |
1912 |
dlink_node *ptr; |
1913 |
struct Client *cptr; |
1914 |
|
1915 |
DLINK_FOREACH(ptr, unknown_list.head) |
1916 |
{ |
1917 |
cptr = ptr->data; |
1918 |
|
1919 |
if (cptr && cptr->name[0]) |
1920 |
if (match(cptr->name, name) || match(name, cptr->name)) |
1921 |
return cptr; |
1922 |
} |
1923 |
|
1924 |
return NULL; |
1925 |
} |
1926 |
|
1927 |
#ifdef HAVE_LIBCRYPTO |
1928 |
/* |
1929 |
* sends a CRYPTLINK SERV command. |
1930 |
*/ |
1931 |
void |
1932 |
cryptlink_init(struct Client *client_p, struct AccessItem *aconf, fde_t *fd) |
1933 |
{ |
1934 |
char *encrypted; |
1935 |
unsigned char *key_to_send; |
1936 |
char randkey[CIPHERKEYLEN]; |
1937 |
int enc_len; |
1938 |
|
1939 |
/* get key */ |
1940 |
if ((!ServerInfo.rsa_private_key) || |
1941 |
(!RSA_check_key(ServerInfo.rsa_private_key)) ) |
1942 |
{ |
1943 |
cryptlink_error(client_p, "SERV", "Invalid RSA private key", |
1944 |
"Invalid RSA private key"); |
1945 |
return; |
1946 |
} |
1947 |
|
1948 |
if (aconf->rsa_public_key == NULL) |
1949 |
{ |
1950 |
cryptlink_error(client_p, "SERV", "Invalid RSA public key", |
1951 |
"Invalid RSA public key"); |
1952 |
return; |
1953 |
} |
1954 |
|
1955 |
if (get_randomness((unsigned char *)randkey, CIPHERKEYLEN) != 1) |
1956 |
{ |
1957 |
cryptlink_error(client_p, "SERV", "Couldn't generate keyphrase", |
1958 |
"Couldn't generate keyphrase"); |
1959 |
return; |
1960 |
} |
1961 |
|
1962 |
encrypted = MyMalloc(RSA_size(ServerInfo.rsa_private_key)); |
1963 |
enc_len = RSA_public_encrypt(CIPHERKEYLEN, |
1964 |
(unsigned char *)randkey, |
1965 |
(unsigned char *)encrypted, |
1966 |
aconf->rsa_public_key, |
1967 |
RSA_PKCS1_PADDING); |
1968 |
|
1969 |
memcpy(client_p->localClient->in_key, randkey, CIPHERKEYLEN); |
1970 |
|
1971 |
if (enc_len <= 0) |
1972 |
{ |
1973 |
report_crypto_errors(); |
1974 |
MyFree(encrypted); |
1975 |
cryptlink_error(client_p, "SERV", "Couldn't encrypt data", |
1976 |
"Couldn't encrypt data"); |
1977 |
return; |
1978 |
} |
1979 |
|
1980 |
if (!(base64_block(&key_to_send, encrypted, enc_len))) |
1981 |
{ |
1982 |
MyFree(encrypted); |
1983 |
cryptlink_error(client_p, "SERV", "Couldn't base64 encode key", |
1984 |
"Couldn't base64 encode key"); |
1985 |
return; |
1986 |
} |
1987 |
|
1988 |
send_capabilities(client_p, aconf, (ServerInfo.hub ? CAP_HUB : 0) |
1989 |
| (IsConfCompressed(aconf) ? CAP_ZIP : 0) |
1990 |
| (IsConfTopicBurst(aconf) ? CAP_TBURST|CAP_TB : 0), CAP_ENC_MASK); |
1991 |
|
1992 |
if (me.id[0] != '\0') |
1993 |
sendto_one(client_p, "PASS . TS %d %s", TS_CURRENT, me.id); |
1994 |
|
1995 |
sendto_one(client_p, "CRYPTLINK SERV %s %s :%s%s", |
1996 |
my_name_for_link(aconf), key_to_send, /* XXX */ |
1997 |
ConfigServerHide.hidden ? "(H) " : "", me.info); |
1998 |
|
1999 |
SetHandshake(client_p); |
2000 |
SetWaitAuth(client_p); |
2001 |
|
2002 |
MyFree(encrypted); |
2003 |
MyFree(key_to_send); |
2004 |
|
2005 |
if (IsDead(client_p)) |
2006 |
cryptlink_error(client_p, "SERV", "Went dead during handshake", |
2007 |
"Went dead during handshake"); |
2008 |
else if (fd != NULL) |
2009 |
/* If we get here, we're ok, so lets start reading some data */ |
2010 |
comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0); |
2011 |
} |
2012 |
|
2013 |
void |
2014 |
cryptlink_error(struct Client *client_p, const char *type, |
2015 |
const char *reason, const char *client_reason) |
2016 |
{ |
2017 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, "%s: CRYPTLINK %s error - %s", |
2018 |
get_client_name(client_p, SHOW_IP), type, reason); |
2019 |
sendto_realops_flags(UMODE_ALL, L_OPER, "%s: CRYPTLINK %s error - %s", |
2020 |
get_client_name(client_p, MASK_IP), type, reason); |
2021 |
ilog(L_ERROR, "%s: CRYPTLINK %s error - %s", |
2022 |
get_client_name(client_p, SHOW_IP), type, reason); |
2023 |
|
2024 |
/* If client_reason isn't NULL, then exit the client with the message |
2025 |
* defined in the call. |
2026 |
*/ |
2027 |
if ((client_reason != NULL) && (!IsDead(client_p))) |
2028 |
exit_client(client_p, &me, client_reason); |
2029 |
} |
2030 |
|
2031 |
static char base64_chars[] = |
2032 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; |
2033 |
|
2034 |
static char base64_values[] = |
2035 |
{ |
2036 |
/* 00-15 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
2037 |
/* 16-31 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
2038 |
/* 32-47 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, |
2039 |
/* 48-63 */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1, |
2040 |
/* 64-79 */ -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
2041 |
/* 80-95 */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, |
2042 |
/* 96-111 */ -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
2043 |
/* 112-127 */ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, |
2044 |
/* 128-143 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
2045 |
/* 144-159 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
2046 |
/* 160-175 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
2047 |
/* 186-191 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
2048 |
/* 192-207 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
2049 |
/* 208-223 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
2050 |
/* 224-239 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
2051 |
/* 240-255 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
2052 |
}; |
2053 |
|
2054 |
/* |
2055 |
* base64_block will allocate and return a new block of memory |
2056 |
* using MyMalloc(). It should be freed after use. |
2057 |
*/ |
2058 |
int |
2059 |
base64_block(unsigned char **output, char *data, int len) |
2060 |
{ |
2061 |
unsigned char *out; |
2062 |
unsigned char *in = (unsigned char*)data; |
2063 |
unsigned long int q_in; |
2064 |
int i; |
2065 |
int count = 0; |
2066 |
|
2067 |
out = MyMalloc(((((len + 2) - ((len + 2) % 3)) / 3) * 4) + 1); |
2068 |
|
2069 |
/* process 24 bits at a time */ |
2070 |
for( i = 0; i < len; i += 3) |
2071 |
{ |
2072 |
q_in = 0; |
2073 |
|
2074 |
if ( i + 2 < len ) |
2075 |
{ |
2076 |
q_in = (in[i+2] & 0xc0) << 2; |
2077 |
q_in |= in[i+2]; |
2078 |
} |
2079 |
|
2080 |
if ( i + 1 < len ) |
2081 |
{ |
2082 |
q_in |= (in[i+1] & 0x0f) << 10; |
2083 |
q_in |= (in[i+1] & 0xf0) << 12; |
2084 |
} |
2085 |
|
2086 |
q_in |= (in[i] & 0x03) << 20; |
2087 |
q_in |= in[i] << 22; |
2088 |
|
2089 |
q_in &= 0x3f3f3f3f; |
2090 |
|
2091 |
out[count++] = base64_chars[((q_in >> 24) )]; |
2092 |
out[count++] = base64_chars[((q_in >> 16) & 0xff)]; |
2093 |
out[count++] = base64_chars[((q_in >> 8) & 0xff)]; |
2094 |
out[count++] = base64_chars[((q_in ) & 0xff)]; |
2095 |
} |
2096 |
if ( (i - len) > 0 ) |
2097 |
{ |
2098 |
out[count-1] = '='; |
2099 |
if ( (i - len) > 1 ) |
2100 |
out[count-2] = '='; |
2101 |
} |
2102 |
|
2103 |
out[count] = '\0'; |
2104 |
*output = out; |
2105 |
return (count); |
2106 |
} |
2107 |
|
2108 |
/* |
2109 |
* unbase64_block will allocate and return a new block of memory |
2110 |
* using MyMalloc(). It should be freed after use. |
2111 |
*/ |
2112 |
int |
2113 |
unbase64_block(unsigned char **output, char *data, int len) |
2114 |
{ |
2115 |
unsigned char *out; |
2116 |
unsigned char *in = (unsigned char*)data; |
2117 |
unsigned long int q_in; |
2118 |
int i; |
2119 |
int count = 0; |
2120 |
|
2121 |
if ((len % 4) != 0) |
2122 |
return (0); |
2123 |
|
2124 |
out = MyMalloc(((len / 4) * 3) + 1); |
2125 |
|
2126 |
/* process 32 bits at a time */ |
2127 |
for( i = 0; (i + 3) < len; i+=4) |
2128 |
{ |
2129 |
/* compress input (chars a, b, c and d) as follows: |
2130 |
* (after converting ascii -> base64 value) |
2131 |
* |
2132 |
* |00000000aaaaaabbbbbbccccccdddddd| |
2133 |
* | 765432 107654 321076 543210| |
2134 |
*/ |
2135 |
|
2136 |
q_in = 0; |
2137 |
|
2138 |
if (base64_values[in[i+3]] > -1) |
2139 |
q_in |= base64_values[in[i+3]] ; |
2140 |
if (base64_values[in[i+2]] > -1) |
2141 |
q_in |= base64_values[in[i+2]] << 6; |
2142 |
if (base64_values[in[i+1]] > -1) |
2143 |
q_in |= base64_values[in[i+1]] << 12; |
2144 |
if (base64_values[in[i ]] > -1) |
2145 |
q_in |= base64_values[in[i ]] << 18; |
2146 |
|
2147 |
out[count++] = (q_in >> 16) & 0xff; |
2148 |
out[count++] = (q_in >> 8) & 0xff; |
2149 |
out[count++] = (q_in ) & 0xff; |
2150 |
} |
2151 |
|
2152 |
if (in[i-1] == '=') count--; |
2153 |
if (in[i-2] == '=') count--; |
2154 |
|
2155 |
out[count] = '\0'; |
2156 |
*output = out; |
2157 |
return (count); |
2158 |
} |
2159 |
|
2160 |
#endif /* HAVE_LIBCRYPTO */ |
2161 |
|