ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/server.c
Revision: 3274
Committed: Sun Apr 6 12:22:23 2014 UTC (11 years, 4 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid/trunk/src/s_serv.c
File size: 42307 byte(s)
Log Message:
- Clean up redundant/unused header includes

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 2916 * Copyright (c) 1997-2014 ircd-hybrid development team
5 adx 30 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19     * USA
20     */
21    
22 michael 2916 /*! \file s_serv.c
23     * \brief Server related functions.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28     #ifdef HAVE_LIBCRYPTO
29     #include <openssl/rsa.h>
30     #include "rsa.h"
31     #endif
32 michael 1011 #include "list.h"
33 adx 30 #include "client.h"
34     #include "event.h"
35     #include "hash.h"
36     #include "irc_string.h"
37     #include "ircd.h"
38     #include "ircd_defs.h"
39     #include "s_bsd.h"
40     #include "numeric.h"
41     #include "packet.h"
42 michael 1309 #include "conf.h"
43 adx 30 #include "s_serv.h"
44 michael 1309 #include "log.h"
45 michael 1303 #include "s_misc.h"
46 adx 30 #include "s_user.h"
47     #include "send.h"
48     #include "memory.h"
49 michael 2916 #include "channel.h"
50 michael 1243 #include "parse.h"
51 adx 30
52     #define MIN_CONN_FREQ 300
53    
54 michael 2156 dlink_list flatten_links;
55 adx 30 static dlink_list cap_list = { NULL, NULL, 0 };
56     static void server_burst(struct Client *);
57     static void burst_all(struct Client *);
58     static void send_tb(struct Client *client_p, struct Channel *chptr);
59    
60     static CNCB serv_connect_callback;
61    
62     static void burst_members(struct Client *, struct Channel *);
63    
64     /*
65     * write_links_file
66     *
67     * inputs - void pointer which is not used
68     * output - NONE
69     * side effects - called from an event, write out list of linked servers
70     * but in no particular order.
71     */
72     void
73 michael 2156 write_links_file(void *notused)
74 adx 30 {
75 michael 2156 FILE *file = NULL;
76 michael 2216 dlink_node *ptr = NULL, *ptr_next = NULL;
77 michael 3215 char buff[IRCD_BUFSIZE] = "";
78 adx 30
79 michael 2156 if ((file = fopen(LIPATH, "w")) == NULL)
80 adx 30 return;
81    
82 michael 2156 DLINK_FOREACH_SAFE(ptr, ptr_next, flatten_links.head)
83 adx 30 {
84 michael 2156 dlinkDelete(ptr, &flatten_links);
85     MyFree(ptr->data);
86     free_dlink_node(ptr);
87 adx 30 }
88    
89     DLINK_FOREACH(ptr, global_serv_list.head)
90     {
91 michael 1325 const struct Client *target_p = ptr->data;
92 adx 30
93 michael 2156 /*
94     * Skip hidden servers, aswell as ourselves, since we already send
95     * ourselves in /links
96     */
97     if (IsHidden(target_p) || IsMe(target_p))
98 adx 30 continue;
99    
100 michael 1851 if (HasFlag(target_p, FLAGS_SERVICE) && ConfigServerHide.hide_services)
101     continue;
102    
103 michael 1545 /*
104     * Attempt to format the file in such a way it follows the usual links output
105 adx 30 * ie "servername uplink :hops info"
106     * Mostly for aesthetic reasons - makes it look pretty in mIRC ;)
107     * - madmax
108     */
109 michael 2156 snprintf(buff, sizeof(buff), "%s %s :1 %s", target_p->name,
110     me.name, target_p->info);
111 michael 2212 dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
112 michael 2156 snprintf(buff, sizeof(buff), "%s %s :1 %s\n", target_p->name,
113     me.name, target_p->info);
114 adx 30
115 michael 1325 fputs(buff, file);
116 adx 30 }
117    
118 michael 1325 fclose(file);
119 adx 30 }
120    
121 michael 2216 void
122     read_links_file(void)
123     {
124     FILE *file = NULL;
125     char *p = NULL;
126 michael 3215 char buff[IRCD_BUFSIZE] = "";
127 michael 2216
128     if ((file = fopen(LIPATH, "r")) == NULL)
129     return;
130    
131     while (fgets(buff, sizeof(buff), file))
132     {
133 michael 3246 if ((p = strchr(buff, '\n')))
134 michael 2216 *p = '\0';
135    
136     dlinkAddTail(xstrdup(buff), make_dlink_node(), &flatten_links);
137     }
138    
139     fclose(file);
140     }
141    
142 adx 30 /* hunt_server()
143     * Do the basic thing in delivering the message (command)
144     * across the relays to the specific server (server) for
145     * actions.
146     *
147     * Note: The command is a format string and *MUST* be
148     * of prefixed style (e.g. ":%s COMMAND %s ...").
149     * Command can have only max 8 parameters.
150     *
151     * server parv[server] is the parameter identifying the
152     * target server.
153     *
154     * *WARNING*
155     * parv[server] is replaced with the pointer to the
156     * real servername from the matched client (I'm lazy
157     * now --msa).
158     *
159     * returns: (see #defines)
160     */
161     int
162 michael 3156 hunt_server(struct Client *source_p, const char *command,
163 michael 1857 const int server, const int parc, char *parv[])
164 adx 30 {
165     struct Client *target_p = NULL;
166     struct Client *target_tmp = NULL;
167     dlink_node *ptr;
168     int wilds;
169    
170 michael 1344 /* Assume it's me, if no server */
171     if (parc <= server || EmptyString(parv[server]))
172     return HUNTED_ISME;
173 adx 30
174 michael 1652 if (!strcmp(parv[server], me.id) || !match(parv[server], me.name))
175 michael 1344 return HUNTED_ISME;
176    
177 adx 30 /* These are to pickup matches that would cause the following
178     * message to go in the wrong direction while doing quick fast
179     * non-matching lookups.
180     */
181     if (MyClient(source_p))
182 michael 1169 target_p = hash_find_client(parv[server]);
183 adx 30 else
184 michael 3156 target_p = find_person(source_p, parv[server]);
185 adx 30
186     if (target_p)
187     if (target_p->from == source_p->from && !MyConnect(target_p))
188     target_p = NULL;
189    
190 michael 1169 if (target_p == NULL && (target_p = hash_find_server(parv[server])))
191 adx 30 if (target_p->from == source_p->from && !MyConnect(target_p))
192     target_p = NULL;
193    
194 michael 1400 wilds = has_wildcards(parv[server]);
195 adx 30
196     /* Again, if there are no wild cards involved in the server
197     * name, use the hash lookup
198     */
199     if (target_p == NULL)
200     {
201     if (!wilds)
202     {
203 michael 1169 if (!(target_p = hash_find_server(parv[server])))
204 adx 30 {
205 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOSUCHSERVER, parv[server]);
206 michael 2182 return HUNTED_NOSUCH;
207 adx 30 }
208     }
209     else
210     {
211     DLINK_FOREACH(ptr, global_client_list.head)
212     {
213     target_tmp = ptr->data;
214    
215 michael 1652 if (!match(parv[server], target_tmp->name))
216 adx 30 {
217     if (target_tmp->from == source_p->from && !MyConnect(target_tmp))
218     continue;
219     target_p = ptr->data;
220    
221 michael 3156 if (IsRegistered(target_p) && (target_p != source_p->from))
222 adx 30 break;
223     }
224     }
225     }
226     }
227    
228 michael 3246 if (target_p)
229 adx 30 {
230 michael 3246 if (!IsRegistered(target_p))
231 adx 30 {
232 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOSUCHSERVER, parv[server]);
233 adx 30 return HUNTED_NOSUCH;
234     }
235    
236     if (IsMe(target_p) || MyClient(target_p))
237     return HUNTED_ISME;
238    
239 michael 1652 if (match(target_p->name, parv[server]))
240 adx 30 parv[server] = target_p->name;
241    
242 michael 3136 /* This is a little kludgy but should work... */
243 michael 3096 sendto_one(target_p, command, ID_or_name(source_p, target_p),
244 adx 30 parv[1], parv[2], parv[3], parv[4],
245     parv[5], parv[6], parv[7], parv[8]);
246 michael 2134 return HUNTED_PASS;
247 michael 2345 }
248 adx 30
249 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOSUCHSERVER, parv[server]);
250 michael 2134 return HUNTED_NOSUCH;
251 adx 30 }
252    
253     /* try_connections()
254     *
255     * inputs - void pointer which is not used
256     * output - NONE
257     * side effects -
258     * scan through configuration and try new connections.
259     * Returns the calendar time when the next call to this
260     * function should be made latest. (No harm done if this
261     * is called earlier or later...)
262     */
263     void
264     try_connections(void *unused)
265     {
266 michael 1632 dlink_node *ptr = NULL;
267     struct MaskItem *conf;
268 adx 30 int confrq;
269    
270     /* TODO: change this to set active flag to 0 when added to event! --Habeeb */
271     if (GlobalSetOptions.autoconn == 0)
272     return;
273    
274     DLINK_FOREACH(ptr, server_items.head)
275     {
276     conf = ptr->data;
277    
278 michael 1636 assert(conf->type == CONF_SERVER);
279 michael 1632
280 michael 2345 /* Also when already connecting! (update holdtimes) --SRB
281 adx 30 */
282 michael 1632 if (!conf->port ||!IsConfAllowAutoConn(conf))
283 adx 30 continue;
284    
285    
286     /* Skip this entry if the use of it is still on hold until
287     * future. Otherwise handle this entry (and set it on hold
288     * until next time). Will reset only hold times, if already
289     * made one successfull connection... [this algorithm is
290     * a bit fuzzy... -- msa >;) ]
291     */
292 michael 1649 if (conf->until > CurrentTime)
293 adx 30 continue;
294    
295 michael 1632 if (conf->class == NULL)
296 adx 30 confrq = DEFAULT_CONNECTFREQUENCY;
297     else
298     {
299 michael 1632 confrq = conf->class->con_freq;
300 michael 1377 if (confrq < MIN_CONN_FREQ)
301 michael 2134 confrq = MIN_CONN_FREQ;
302 adx 30 }
303    
304 michael 1649 conf->until = CurrentTime + confrq;
305 adx 30
306 michael 3246 /*
307     * Found a CONNECT config with port specified, scan clients
308 adx 30 * and see if this server is already connected?
309     */
310 michael 3246 if (hash_find_server(conf->name))
311 adx 30 continue;
312    
313 michael 1632 if (conf->class->ref_count < conf->class->max_total)
314 adx 30 {
315     /* Go to the end of the list, if not already last */
316 michael 3246 if (ptr->next)
317 adx 30 {
318     dlinkDelete(ptr, &server_items);
319     dlinkAddTail(conf, &conf->node, &server_items);
320     }
321    
322     if (find_servconn_in_progress(conf->name))
323     return;
324    
325 michael 3246 /*
326     * We used to only print this if serv_connect() actually
327 adx 30 * succeeded, but since comm_tcp_connect() can call the callback
328     * immediately if there is an error, we were getting error messages
329     * in the wrong order. SO, we just print out the activated line,
330     * and let serv_connect() / serv_connect_callback() print an
331     * error afterwards if it fails.
332     * -- adrian
333     */
334     if (ConfigServerHide.hide_server_ips)
335 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
336     "Connection to %s activated.",
337 adx 30 conf->name);
338     else
339 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
340     "Connection to %s[%s] activated.",
341 michael 1632 conf->name, conf->host);
342 adx 30
343 michael 1632 serv_connect(conf, NULL);
344 adx 30 /* We connect only one at time... */
345     return;
346     }
347     }
348     }
349    
350     int
351 michael 1115 valid_servname(const char *name)
352     {
353     unsigned int length = 0;
354     unsigned int dots = 0;
355     const char *p = name;
356    
357     for (; *p; ++p)
358     {
359     if (!IsServChar(*p))
360 michael 1118 return 0;
361 michael 1115
362     ++length;
363    
364     if (*p == '.')
365     ++dots;
366     }
367    
368 michael 3246 return dots && length <= HOSTLEN;
369 michael 1115 }
370    
371     int
372 michael 1302 check_server(const char *name, struct Client *client_p)
373 adx 30 {
374     dlink_node *ptr;
375 michael 1632 struct MaskItem *conf = NULL;
376     struct MaskItem *server_conf = NULL;
377 adx 30 int error = -1;
378    
379 michael 3246 assert(client_p);
380 adx 30
381     /* loop through looking for all possible connect items that might work */
382     DLINK_FOREACH(ptr, server_items.head)
383     {
384     conf = ptr->data;
385    
386 michael 1652 if (match(name, conf->name))
387 adx 30 continue;
388    
389     error = -3;
390    
391     /* XXX: Fix me for IPv6 */
392     /* XXX sockhost is the IPv4 ip as a string */
393 michael 2345 if (!match(conf->host, client_p->host) ||
394 michael 1652 !match(conf->host, client_p->sockhost))
395 adx 30 {
396     error = -2;
397    
398 michael 1632 if (!match_conf_password(client_p->localClient->passwd, conf))
399 michael 1414 return -2;
400 adx 30
401 michael 2228 if (!EmptyString(conf->certfp))
402 michael 2229 if (EmptyString(client_p->certfp) || strcasecmp(client_p->certfp, conf->certfp))
403 michael 2228 return -4;
404    
405 michael 1414 server_conf = conf;
406 adx 30 }
407     }
408    
409     if (server_conf == NULL)
410 michael 2182 return error;
411 adx 30
412     attach_conf(client_p, server_conf);
413    
414    
415 michael 3246 if (server_conf)
416 adx 30 {
417     struct sockaddr_in *v4;
418     #ifdef IPV6
419     struct sockaddr_in6 *v6;
420     #endif
421 michael 1632 switch (server_conf->aftype)
422 adx 30 {
423     #ifdef IPV6
424 michael 2345 case AF_INET6:
425 michael 1632 v6 = (struct sockaddr_in6 *)&server_conf->addr;
426 adx 30
427     if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr))
428 michael 1632 memcpy(&server_conf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
429 adx 30 break;
430     #endif
431     case AF_INET:
432 michael 1632 v4 = (struct sockaddr_in *)&server_conf->addr;
433 adx 30
434     if (v4->sin_addr.s_addr == INADDR_NONE)
435 michael 2345 memcpy(&server_conf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
436 adx 30 break;
437     }
438     }
439    
440 michael 2182 return 0;
441 adx 30 }
442    
443     /* add_capability()
444     *
445     * inputs - string name of CAPAB
446     * - int flag of capability
447     * output - NONE
448     * side effects - Adds given capability name and bit mask to
449     * current supported capabilities. This allows
450     * modules to dynamically add or subtract their capability.
451     */
452     void
453     add_capability(const char *capab_name, int cap_flag, int add_to_default)
454     {
455 michael 885 struct Capability *cap = MyMalloc(sizeof(*cap));
456 adx 30
457 michael 1646 cap->name = xstrdup(capab_name);
458 adx 30 cap->cap = cap_flag;
459     dlinkAdd(cap, &cap->node, &cap_list);
460 michael 885
461 adx 30 if (add_to_default)
462     default_server_capabs |= cap_flag;
463     }
464    
465     /* delete_capability()
466     *
467     * inputs - string name of CAPAB
468     * output - NONE
469     * side effects - delete given capability from ones known.
470     */
471     int
472     delete_capability(const char *capab_name)
473     {
474     dlink_node *ptr;
475     dlink_node *next_ptr;
476     struct Capability *cap;
477    
478     DLINK_FOREACH_SAFE(ptr, next_ptr, cap_list.head)
479     {
480     cap = ptr->data;
481    
482     if (cap->cap != 0)
483     {
484     if (irccmp(cap->name, capab_name) == 0)
485     {
486 michael 2134 default_server_capabs &= ~(cap->cap);
487     dlinkDelete(ptr, &cap_list);
488     MyFree(cap->name);
489     MyFree(cap);
490 adx 30 }
491     }
492     }
493    
494 michael 896 return 0;
495 adx 30 }
496    
497     /*
498     * find_capability()
499     *
500     * inputs - string name of capab to find
501     * output - 0 if not found CAPAB otherwise
502     * side effects - none
503     */
504 michael 1877 unsigned int
505 adx 30 find_capability(const char *capab)
506     {
507 michael 896 const dlink_node *ptr = NULL;
508 adx 30
509     DLINK_FOREACH(ptr, cap_list.head)
510     {
511 michael 896 const struct Capability *cap = ptr->data;
512 adx 30
513 michael 896 if (cap->cap && !irccmp(cap->name, capab))
514     return cap->cap;
515 adx 30 }
516 michael 896
517     return 0;
518 adx 30 }
519    
520     /* send_capabilities()
521     *
522     * inputs - Client pointer to send to
523     * - int flag of capabilities that this server can send
524     * output - NONE
525     * side effects - send the CAPAB line to a server -orabidoo
526     *
527     */
528     void
529 michael 1632 send_capabilities(struct Client *client_p, int cap_can_send)
530 adx 30 {
531 michael 3246 char msgbuf[IRCD_BUFSIZE] = "";
532     char *t = msgbuf;
533 adx 30 int tl;
534 michael 3246 dlink_node *ptr = NULL;
535 adx 30
536     DLINK_FOREACH(ptr, cap_list.head)
537     {
538 michael 3246 struct Capability *cap = ptr->data;
539 adx 30
540     if (cap->cap & (cap_can_send|default_server_capabs))
541     {
542 michael 1793 tl = sprintf(t, "%s ", cap->name);
543 adx 30 t += tl;
544     }
545     }
546    
547 michael 319 *(t - 1) = '\0';
548 adx 30 sendto_one(client_p, "CAPAB :%s", msgbuf);
549     }
550    
551     /* sendnick_TS()
552 michael 2345 *
553 adx 30 * inputs - client (server) to send nick towards
554 adx 386 * - client to send nick for
555 adx 30 * output - NONE
556     * side effects - NICK message is sent towards given client_p
557     */
558     void
559     sendnick_TS(struct Client *client_p, struct Client *target_p)
560     {
561 michael 3246 char ubuf[IRCD_BUFSIZE] = "";
562 adx 30
563     if (!IsClient(target_p))
564     return;
565    
566 michael 1294 send_umode(NULL, target_p, 0, SEND_UMODES, ubuf);
567 adx 30
568     if (ubuf[0] == '\0')
569     {
570     ubuf[0] = '+';
571     ubuf[1] = '\0';
572     }
573    
574 michael 1196 if (IsCapable(client_p, CAP_SVS))
575 michael 3135 sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s %s :%s",
576     target_p->servptr->id,
577     target_p->name, target_p->hopcount + 1,
578     (unsigned long) target_p->tsinfo,
579     ubuf, target_p->username, target_p->host,
580     (MyClient(target_p) && IsIPSpoof(target_p)) ?
581     "0" : target_p->sockhost, target_p->id,
582     target_p->svid, target_p->info);
583 adx 30 else
584 michael 3135 sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s :%s",
585     target_p->servptr->id,
586     target_p->name, target_p->hopcount + 1,
587     (unsigned long) target_p->tsinfo,
588     ubuf, target_p->username, target_p->host,
589     (MyClient(target_p) && IsIPSpoof(target_p)) ?
590     "0" : target_p->sockhost, target_p->id, target_p->info);
591 adx 386
592 michael 2229 if (!EmptyString(target_p->certfp))
593 michael 3184 sendto_one(client_p, ":%s CERTFP %s", target_p->id, target_p->certfp);
594 michael 2228
595 michael 1519 if (target_p->away[0])
596 michael 3184 sendto_one(client_p, ":%s AWAY :%s", target_p->id, target_p->away);
597 adx 30
598     }
599    
600     /*
601     * show_capabilities - show current server capabilities
602     *
603     * inputs - pointer to a struct Client
604     * output - pointer to static string
605     * side effects - build up string representing capabilities of server listed
606     */
607     const char *
608 michael 2282 show_capabilities(const struct Client *target_p)
609 adx 30 {
610 michael 2309 static char msgbuf[IRCD_BUFSIZE] = "";
611 michael 2282 const dlink_node *ptr = NULL;
612 adx 30
613 michael 2309 strlcpy(msgbuf, "TS", sizeof(msgbuf));
614    
615 adx 30 DLINK_FOREACH(ptr, cap_list.head)
616     {
617     const struct Capability *cap = ptr->data;
618    
619 michael 2282 if (!IsCapable(target_p, cap->cap))
620     continue;
621    
622     strlcat(msgbuf, " ", sizeof(msgbuf));
623     strlcat(msgbuf, cap->name, sizeof(msgbuf));
624 adx 30 }
625 michael 1302
626     return msgbuf;
627 adx 30 }
628    
629     /* make_server()
630     *
631     * inputs - pointer to client struct
632     * output - pointer to struct Server
633     * side effects - add's an Server information block to a client
634     * if it was not previously allocated.
635     */
636     struct Server *
637     make_server(struct Client *client_p)
638     {
639     if (client_p->serv == NULL)
640     client_p->serv = MyMalloc(sizeof(struct Server));
641    
642     return client_p->serv;
643     }
644    
645     /* server_estab()
646     *
647     * inputs - pointer to a struct Client
648     * output -
649     * side effects -
650     */
651     void
652     server_estab(struct Client *client_p)
653     {
654 michael 1632 struct MaskItem *conf = NULL;
655 adx 30 char *host;
656     const char *inpath;
657     static char inpath_ip[HOSTLEN * 2 + USERLEN + 6];
658     dlink_node *ptr;
659 michael 1305 #ifdef HAVE_LIBCRYPTO
660     const COMP_METHOD *compression = NULL, *expansion = NULL;
661     #endif
662 adx 30
663 michael 3246 assert(client_p);
664 adx 30
665     strlcpy(inpath_ip, get_client_name(client_p, SHOW_IP), sizeof(inpath_ip));
666    
667     inpath = get_client_name(client_p, MASK_IP); /* "refresh" inpath with host */
668     host = client_p->name;
669    
670 michael 1632 if ((conf = find_conf_name(&client_p->localClient->confs, host, CONF_SERVER))
671 adx 30 == NULL)
672     {
673     /* This shouldn't happen, better tell the ops... -A1kmm */
674 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
675     "Warning: Lost connect{} block "
676 adx 30 "for server %s(this shouldn't happen)!", host);
677 michael 3171 exit_client(client_p, "Lost connect{} block!");
678 adx 30 return;
679     }
680    
681     MyFree(client_p->localClient->passwd);
682     client_p->localClient->passwd = NULL;
683    
684     /* Its got identd, since its a server */
685     SetGotId(client_p);
686    
687     /* If there is something in the serv_list, it might be this
688     * connecting server..
689     */
690 michael 2345 if (!ServerInfo.hub && serv_list.head)
691 adx 30 {
692     if (client_p != serv_list.head->data || serv_list.head->next)
693     {
694 michael 896 ++ServerStats.is_ref;
695 adx 30 sendto_one(client_p, "ERROR :I'm a leaf not a hub");
696 michael 3171 exit_client(client_p, "I'm a leaf");
697 adx 30 return;
698     }
699     }
700    
701 michael 1302 if (IsUnknown(client_p))
702 adx 30 {
703 michael 2134 sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
704 adx 30
705 michael 1632 send_capabilities(client_p, 0);
706 adx 30
707     sendto_one(client_p, "SERVER %s 1 :%s%s",
708 michael 1118 me.name, ConfigServerHide.hidden ? "(H) " : "", me.info);
709 adx 30 }
710    
711 michael 1115 sendto_one(client_p, "SVINFO %d %d 0 :%lu", TS_CURRENT, TS_MIN,
712 adx 30 (unsigned long)CurrentTime);
713    
714     /* XXX Does this ever happen? I don't think so -db */
715 michael 1632 detach_conf(client_p, CONF_OPER);
716 adx 30
717     /* *WARNING*
718     ** In the following code in place of plain server's
719     ** name we send what is returned by get_client_name
720     ** which may add the "sockhost" after the name. It's
721     ** *very* *important* that there is a SPACE between
722     ** the name and sockhost (if present). The receiving
723     ** server will start the information field from this
724     ** first blank and thus puts the sockhost into info.
725     ** ...a bit tricky, but you have been warned, besides
726     ** code is more neat this way... --msa
727     */
728     client_p->servptr = &me;
729    
730     if (IsClosing(client_p))
731     return;
732    
733     SetServer(client_p);
734    
735     /* Some day, all these lists will be consolidated *sigh* */
736 michael 889 dlinkAdd(client_p, &client_p->lnode, &me.serv->server_list);
737 adx 30
738 michael 1126 assert(dlinkFind(&unknown_list, client_p));
739 adx 30
740 michael 1126 dlink_move_node(&client_p->localClient->lclient_node,
741     &unknown_list, &serv_list);
742 adx 30
743     Count.myserver++;
744    
745     dlinkAdd(client_p, make_dlink_node(), &global_serv_list);
746     hash_add_client(client_p);
747 michael 3187 hash_add_id(client_p);
748 adx 30
749     /* doesnt duplicate client_p->serv if allocated this struct already */
750     make_server(client_p);
751    
752     /* fixing eob timings.. -gnp */
753 michael 1241 client_p->localClient->firsttime = CurrentTime;
754 adx 30
755 michael 1632 if (find_matching_name_conf(CONF_SERVICE, client_p->name, NULL, NULL, 0))
756 michael 1219 AddFlag(client_p, FLAGS_SERVICE);
757 michael 1157
758 adx 30 /* Show the real host/IP to admins */
759 michael 1305 #ifdef HAVE_LIBCRYPTO
760 michael 1303 if (client_p->localClient->fd.ssl)
761     {
762 michael 1305 compression = SSL_get_current_compression(client_p->localClient->fd.ssl);
763     expansion = SSL_get_current_expansion(client_p->localClient->fd.ssl);
764    
765 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
766 michael 1305 "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
767 michael 1303 inpath_ip, ssl_get_cipher(client_p->localClient->fd.ssl),
768 michael 1305 compression ? SSL_COMP_get_name(compression) : "NONE",
769     expansion ? SSL_COMP_get_name(expansion) : "NONE",
770 michael 1303 show_capabilities(client_p));
771     /* Now show the masked hostname/IP to opers */
772 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
773 michael 1305 "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
774 michael 1303 inpath, ssl_get_cipher(client_p->localClient->fd.ssl),
775 michael 1305 compression ? SSL_COMP_get_name(compression) : "NONE",
776     expansion ? SSL_COMP_get_name(expansion) : "NONE",
777 michael 1303 show_capabilities(client_p));
778 michael 1305 ilog(LOG_TYPE_IRCD, "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
779 michael 1303 inpath_ip, ssl_get_cipher(client_p->localClient->fd.ssl),
780 michael 1305 compression ? SSL_COMP_get_name(compression) : "NONE",
781     expansion ? SSL_COMP_get_name(expansion) : "NONE",
782 michael 1303 show_capabilities(client_p));
783     }
784     else
785 michael 1305 #endif
786 michael 1303 {
787 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
788 michael 1303 "Link with %s established: (Capabilities: %s)",
789 michael 1618 inpath_ip, show_capabilities(client_p));
790 michael 1303 /* Now show the masked hostname/IP to opers */
791 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
792 michael 1303 "Link with %s established: (Capabilities: %s)",
793 michael 1618 inpath, show_capabilities(client_p));
794 michael 1303 ilog(LOG_TYPE_IRCD, "Link with %s established: (Capabilities: %s)",
795     inpath_ip, show_capabilities(client_p));
796     }
797 adx 30
798 michael 1302 fd_note(&client_p->localClient->fd, "Server: %s", client_p->name);
799 adx 30
800 michael 3136 sendto_server(client_p, NOCAPS, NOCAPS, ":%s SID %s 2 %s :%s%s",
801     me.id, client_p->name, client_p->id,
802     IsHidden(client_p) ? "(H) " : "", client_p->info);
803 adx 30
804 michael 2345 /*
805     * Pass on my client information to the new server
806     *
807     * First, pass only servers (idea is that if the link gets
808     * cancelled beacause the server was already there,
809     * there are no NICK's to be cancelled...). Of course,
810     * if cancellation occurs, all this info is sent anyway,
811     * and I guess the link dies when a read is attempted...? --msa
812     *
813     * Note: Link cancellation to occur at this point means
814     * that at least two servers from my fragment are building
815     * up connection this other fragment at the same time, it's
816     * a race condition, not the normal way of operation...
817     *
818     * ALSO NOTE: using the get_client_name for server names--
819     * see previous *WARNING*!!! (Also, original inpath
820     * is destroyed...)
821     */
822 adx 30
823     DLINK_FOREACH_PREV(ptr, global_serv_list.tail)
824     {
825 michael 3137 struct Client *target_p = ptr->data;
826 adx 30
827     /* target_p->from == target_p for target_p == client_p */
828 michael 1118 if (IsMe(target_p) || target_p->from == client_p)
829 adx 30 continue;
830    
831 michael 3135 sendto_one(client_p, ":%s SID %s %d %s :%s%s",
832 michael 3186 target_p->servptr->id, target_p->name, target_p->hopcount+1,
833 michael 3135 target_p->id, IsHidden(target_p) ? "(H) " : "",
834     target_p->info);
835 michael 1972
836     if (HasFlag(target_p, FLAGS_EOB))
837 michael 3184 sendto_one(client_p, ":%s EOB", target_p->id, client_p);
838 adx 30 }
839    
840     server_burst(client_p);
841     }
842    
843     /* server_burst()
844     *
845     * inputs - struct Client pointer server
846     * -
847     * output - none
848     * side effects - send a server burst
849     * bugs - still too long
850     */
851     static void
852     server_burst(struct Client *client_p)
853     {
854     /* Send it in the shortened format with the TS, if
855     ** it's a TS server; walk the list of channels, sending
856     ** all the nicks that haven't been sent yet for each
857     ** channel, then send the channel itself -- it's less
858     ** obvious than sending all nicks first, but on the
859     ** receiving side memory will be allocated more nicely
860     ** saving a few seconds in the handling of a split
861     ** -orabidoo
862     */
863    
864 michael 885 burst_all(client_p);
865 adx 30
866     /* EOB stuff is now in burst_all */
867     /* Always send a PING after connect burst is done */
868 michael 3184 sendto_one(client_p, "PING :%s", me.id);
869 adx 30 }
870    
871     /* burst_all()
872     *
873 michael 2345 * inputs - pointer to server to send burst to
874 adx 30 * output - NONE
875     * side effects - complete burst of channels/nicks is sent to client_p
876     */
877     static void
878     burst_all(struct Client *client_p)
879     {
880 michael 329 dlink_node *ptr = NULL;
881 adx 30
882 michael 329 DLINK_FOREACH(ptr, global_channel_list.head)
883 adx 30 {
884 michael 329 struct Channel *chptr = ptr->data;
885 adx 30
886 michael 3246 if (dlink_list_length(&chptr->members))
887 adx 30 {
888     burst_members(client_p, chptr);
889     send_channel_modes(client_p, chptr);
890 michael 329
891 michael 1472 if (IsCapable(client_p, CAP_TBURST))
892 michael 2134 send_tb(client_p, chptr);
893 adx 30 }
894     }
895    
896     /* also send out those that are not on any channel
897     */
898     DLINK_FOREACH(ptr, global_client_list.head)
899     {
900 michael 329 struct Client *target_p = ptr->data;
901 adx 30
902 michael 1219 if (!HasFlag(target_p, FLAGS_BURSTED) && target_p->from != client_p)
903 adx 30 sendnick_TS(client_p, target_p);
904 michael 2345
905 michael 1219 DelFlag(target_p, FLAGS_BURSTED);
906 adx 30 }
907    
908     if (IsCapable(client_p, CAP_EOB))
909 michael 3184 sendto_one(client_p, ":%s EOB", me.id);
910 adx 30 }
911    
912     /*
913     * send_tb
914     *
915 michael 329 * inputs - pointer to Client
916     * - pointer to channel
917     * output - NONE
918     * side effects - Called on a server burst when
919 michael 1472 * server is CAP_TBURST capable
920 adx 30 */
921     static void
922     send_tb(struct Client *client_p, struct Channel *chptr)
923     {
924 michael 329 /*
925 michael 337 * We may also send an empty topic here, but only if topic_time isn't 0,
926     * i.e. if we had a topic that got unset. This is required for syncing
927     * topics properly.
928     *
929     * Imagine the following scenario: Our downlink introduces a channel
930     * to us with a TS that is equal to ours, but the channel topic on
931     * their side got unset while the servers were in splitmode, which means
932     * their 'topic' is newer. They simply wanted to unset it, so we have to
933     * deal with it in a more sophisticated fashion instead of just resetting
934     * it to their old topic they had before. Read m_tburst.c:ms_tburst
935     * for further information -Michael
936 michael 329 */
937 michael 3246 if (chptr->topic_time)
938 michael 3184 sendto_one(client_p, ":%s TBURST %lu %s %lu %s :%s", me.id,
939 michael 1472 (unsigned long)chptr->channelts, chptr->chname,
940     (unsigned long)chptr->topic_time,
941     chptr->topic_info,
942     chptr->topic);
943 adx 30 }
944    
945     /* burst_members()
946     *
947     * inputs - pointer to server to send members to
948     * - dlink_list pointer to membership list to send
949     * output - NONE
950     * side effects -
951     */
952     static void
953     burst_members(struct Client *client_p, struct Channel *chptr)
954     {
955     struct Client *target_p;
956     struct Membership *ms;
957     dlink_node *ptr;
958    
959     DLINK_FOREACH(ptr, chptr->members.head)
960     {
961     ms = ptr->data;
962     target_p = ms->client_p;
963    
964 michael 1219 if (!HasFlag(target_p, FLAGS_BURSTED))
965 adx 30 {
966 michael 1219 AddFlag(target_p, FLAGS_BURSTED);
967 adx 30
968     if (target_p->from != client_p)
969     sendnick_TS(client_p, target_p);
970     }
971     }
972     }
973    
974     /* New server connection code
975     * Based upon the stuff floating about in s_bsd.c
976     * -- adrian
977     */
978    
979     /* serv_connect() - initiate a server connection
980     *
981 michael 2345 * inputs - pointer to conf
982 adx 30 * - pointer to client doing the connect
983     * output -
984     * side effects -
985     *
986     * This code initiates a connection to a server. It first checks to make
987     * sure the given server exists. If this is the case, it creates a socket,
988     * creates a client, saves the socket information in the client, and
989     * initiates a connection to the server through comm_connect_tcp(). The
990     * completion of this goes through serv_completed_connection().
991     *
992     * We return 1 if the connection is attempted, since we don't know whether
993     * it suceeded or not, and 0 if it fails in here somewhere.
994     */
995     int
996 michael 1632 serv_connect(struct MaskItem *conf, struct Client *by)
997 adx 30 {
998 michael 3246 struct Client *client_p = NULL;
999     char buf[HOSTIPLEN + 1] = "";
1000 adx 30
1001     /* conversion structs */
1002     struct sockaddr_in *v4;
1003 michael 3246
1004 michael 1632 /* Make sure conf is useful */
1005 michael 3246 assert(conf);
1006 adx 30
1007 michael 1632 getnameinfo((struct sockaddr *)&conf->addr, conf->addr.ss_len,
1008 michael 1123 buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
1009 michael 1632 ilog(LOG_TYPE_IRCD, "Connect to %s[%s] @%s", conf->name, conf->host,
1010 adx 30 buf);
1011    
1012     /* Still processing a DNS lookup? -> exit */
1013 michael 1632 if (conf->dns_pending)
1014 adx 30 {
1015 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1016 michael 992 "Error connecting to %s: DNS lookup for connect{} in progress.",
1017     conf->name);
1018 michael 3246 return 0;
1019 adx 30 }
1020    
1021 michael 1632 if (conf->dns_failed)
1022 michael 992 {
1023 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1024 michael 992 "Error connecting to %s: DNS lookup for connect{} failed.",
1025     conf->name);
1026 michael 3246 return 0;
1027 michael 992 }
1028    
1029 adx 30 /* Make sure this server isn't already connected
1030 michael 1632 * Note: conf should ALWAYS be a valid C: line
1031 adx 30 */
1032 michael 3246 if ((client_p = hash_find_server(conf->name)))
1033 michael 2345 {
1034 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1035 michael 2134 "Server %s already present from %s",
1036     conf->name, get_client_name(client_p, SHOW_IP));
1037 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1038 michael 2134 "Server %s already present from %s",
1039     conf->name, get_client_name(client_p, MASK_IP));
1040 adx 30 if (by && IsClient(by) && !MyClient(by))
1041 michael 3110 sendto_one_notice(by, &me, ":Server %s already present from %s",
1042     conf->name, get_client_name(client_p, MASK_IP));
1043 michael 2134 return 0;
1044 adx 30 }
1045 michael 2345
1046 adx 30 /* Create a local client */
1047     client_p = make_client(NULL);
1048    
1049     /* Copy in the server, hostname, fd */
1050     strlcpy(client_p->name, conf->name, sizeof(client_p->name));
1051 michael 1632 strlcpy(client_p->host, conf->host, sizeof(client_p->host));
1052 adx 30
1053     /* We already converted the ip once, so lets use it - stu */
1054 michael 1115 strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost));
1055 adx 30
1056 michael 2345 /* create a socket for the server connection */
1057 michael 1632 if (comm_open(&client_p->localClient->fd, conf->addr.ss.ss_family,
1058 adx 30 SOCK_STREAM, 0, NULL) < 0)
1059     {
1060     /* Eek, failure to create the socket */
1061 michael 2134 report_error(L_ALL, "opening stream socket to %s: %s",
1062     conf->name, errno);
1063 adx 30 SetDead(client_p);
1064 michael 3171 exit_client(client_p, "Connection failed");
1065 michael 2134 return 0;
1066 adx 30 }
1067    
1068     /* servernames are always guaranteed under HOSTLEN chars */
1069     fd_note(&client_p->localClient->fd, "Server: %s", conf->name);
1070    
1071     /* Attach config entries to client here rather than in
1072     * serv_connect_callback(). This to avoid null pointer references.
1073     */
1074 michael 1632 if (!attach_connect_block(client_p, conf->name, conf->host))
1075 adx 30 {
1076 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1077 michael 2134 "Host %s is not enabled for connecting: no connect{} block",
1078     conf->name);
1079 michael 2345 if (by && IsClient(by) && !MyClient(by))
1080 michael 3110 sendto_one_notice(by, &me, ":Connect to host %s failed.", client_p->name);
1081    
1082 adx 30 SetDead(client_p);
1083 michael 3171 exit_client(client_p, "Connection failed");
1084 michael 2134 return 0;
1085 adx 30 }
1086    
1087     /* at this point we have a connection in progress and C/N lines
1088     * attached to the client, the socket info should be saved in the
1089     * client and it should either be resolved or have a valid address.
1090     *
1091     * The socket has been connected or connect is in progress.
1092     */
1093     make_server(client_p);
1094    
1095     if (by && IsClient(by))
1096     strlcpy(client_p->serv->by, by->name, sizeof(client_p->serv->by));
1097     else
1098     strlcpy(client_p->serv->by, "AutoConn.", sizeof(client_p->serv->by));
1099    
1100     SetConnecting(client_p);
1101     dlinkAdd(client_p, &client_p->node, &global_client_list);
1102     /* from def_fam */
1103 michael 1632 client_p->localClient->aftype = conf->aftype;
1104 adx 30
1105     /* Now, initiate the connection */
1106 michael 2345 /* XXX assume that a non 0 type means a specific bind address
1107 adx 30 * for this connect.
1108     */
1109 michael 1632 switch (conf->aftype)
1110 adx 30 {
1111     case AF_INET:
1112 michael 1632 v4 = (struct sockaddr_in*)&conf->bind;
1113 michael 3246 if (v4->sin_addr.s_addr)
1114 adx 30 {
1115     struct irc_ssaddr ipn;
1116     memset(&ipn, 0, sizeof(struct irc_ssaddr));
1117     ipn.ss.ss_family = AF_INET;
1118     ipn.ss_port = 0;
1119 michael 1632 memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
1120 michael 2134 comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
1121 michael 2345 (struct sockaddr *)&ipn, ipn.ss_len,
1122 michael 2134 serv_connect_callback, client_p, conf->aftype,
1123     CONNECTTIMEOUT);
1124 adx 30 }
1125     else if (ServerInfo.specific_ipv4_vhost)
1126     {
1127     struct irc_ssaddr ipn;
1128     memset(&ipn, 0, sizeof(struct irc_ssaddr));
1129     ipn.ss.ss_family = AF_INET;
1130     ipn.ss_port = 0;
1131     memcpy(&ipn, &ServerInfo.ip, sizeof(struct irc_ssaddr));
1132 michael 1632 comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
1133 adx 30 (struct sockaddr *)&ipn, ipn.ss_len,
1134 michael 1632 serv_connect_callback, client_p, conf->aftype,
1135 michael 2134 CONNECTTIMEOUT);
1136 adx 30 }
1137     else
1138 michael 2345 comm_connect_tcp(&client_p->localClient->fd, conf->host, conf->port,
1139     NULL, 0, serv_connect_callback, client_p, conf->aftype,
1140 adx 30 CONNECTTIMEOUT);
1141     break;
1142     #ifdef IPV6
1143     case AF_INET6:
1144     {
1145 michael 2182 struct irc_ssaddr ipn;
1146     struct sockaddr_in6 *v6;
1147     struct sockaddr_in6 *v6conf;
1148 adx 30
1149 michael 2182 memset(&ipn, 0, sizeof(struct irc_ssaddr));
1150     v6conf = (struct sockaddr_in6 *)&conf->bind;
1151     v6 = (struct sockaddr_in6 *)&ipn;
1152 adx 30
1153 michael 3246 if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr, sizeof(struct in6_addr)))
1154 adx 30 {
1155 michael 2182 memcpy(&ipn, &conf->bind, sizeof(struct irc_ssaddr));
1156     ipn.ss.ss_family = AF_INET6;
1157     ipn.ss_port = 0;
1158     comm_connect_tcp(&client_p->localClient->fd,
1159     conf->host, conf->port,
1160 michael 2345 (struct sockaddr *)&ipn, ipn.ss_len,
1161 michael 2182 serv_connect_callback, client_p,
1162     conf->aftype, CONNECTTIMEOUT);
1163     }
1164     else if (ServerInfo.specific_ipv6_vhost)
1165     {
1166     memcpy(&ipn, &ServerInfo.ip6, sizeof(struct irc_ssaddr));
1167     ipn.ss.ss_family = AF_INET6;
1168     ipn.ss_port = 0;
1169     comm_connect_tcp(&client_p->localClient->fd,
1170     conf->host, conf->port,
1171     (struct sockaddr *)&ipn, ipn.ss_len,
1172     serv_connect_callback, client_p,
1173     conf->aftype, CONNECTTIMEOUT);
1174     }
1175     else
1176     comm_connect_tcp(&client_p->localClient->fd,
1177 michael 2345 conf->host, conf->port,
1178 michael 2182 NULL, 0, serv_connect_callback, client_p,
1179     conf->aftype, CONNECTTIMEOUT);
1180 adx 30 }
1181     #endif
1182     }
1183 michael 2182 return 1;
1184 adx 30 }
1185    
1186 michael 1303 #ifdef HAVE_LIBCRYPTO
1187     static void
1188     finish_ssl_server_handshake(struct Client *client_p)
1189     {
1190 michael 1632 struct MaskItem *conf = NULL;
1191 michael 1303
1192     conf = find_conf_name(&client_p->localClient->confs,
1193 michael 1632 client_p->name, CONF_SERVER);
1194 michael 1303 if (conf == NULL)
1195     {
1196 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1197 michael 1303 "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
1198 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1199 michael 1303 "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
1200    
1201 michael 3171 exit_client(client_p, "Lost connect{} block");
1202 michael 1303 return;
1203     }
1204    
1205 michael 2134 sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
1206 michael 1303
1207 michael 1632 send_capabilities(client_p, 0);
1208 michael 1303
1209     sendto_one(client_p, "SERVER %s 1 :%s%s",
1210     me.name, ConfigServerHide.hidden ? "(H) " : "",
1211     me.info);
1212    
1213     /* If we've been marked dead because a send failed, just exit
1214     * here now and save everyone the trouble of us ever existing.
1215     */
1216     if (IsDead(client_p))
1217     {
1218 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1219 michael 1303 "%s[%s] went dead during handshake",
1220     client_p->name,
1221     client_p->host);
1222 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1223 michael 1303 "%s went dead during handshake", client_p->name);
1224     return;
1225     }
1226    
1227     /* don't move to serv_list yet -- we haven't sent a burst! */
1228     /* If we get here, we're ok, so lets start reading some data */
1229     comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ, read_packet, client_p, 0);
1230     }
1231    
1232     static void
1233 michael 1306 ssl_server_handshake(fde_t *fd, struct Client *client_p)
1234 michael 1303 {
1235 michael 2463 X509 *cert = NULL;
1236     int ret = 0;
1237 michael 1303
1238 michael 2463 if ((ret = SSL_connect(client_p->localClient->fd.ssl)) <= 0)
1239 michael 1303 {
1240 michael 2463 switch (SSL_get_error(client_p->localClient->fd.ssl, ret))
1241 michael 1303 {
1242     case SSL_ERROR_WANT_WRITE:
1243     comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE,
1244 michael 1308 (PF *)ssl_server_handshake, client_p, 0);
1245 michael 1303 return;
1246     case SSL_ERROR_WANT_READ:
1247     comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ,
1248 michael 1308 (PF *)ssl_server_handshake, client_p, 0);
1249 michael 1303 return;
1250     default:
1251 michael 1308 {
1252     const char *sslerr = ERR_error_string(ERR_get_error(), NULL);
1253 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1254 michael 1308 "Error connecting to %s: %s", client_p->name,
1255     sslerr ? sslerr : "unknown SSL error");
1256 michael 3171 exit_client(client_p, "Error during SSL handshake");
1257 michael 1303 return;
1258 michael 1308 }
1259 michael 1303 }
1260     }
1261    
1262 michael 2463 if ((cert = SSL_get_peer_certificate(client_p->localClient->fd.ssl)))
1263     {
1264     int res = SSL_get_verify_result(client_p->localClient->fd.ssl);
1265     char buf[EVP_MAX_MD_SIZE * 2 + 1] = { '\0' };
1266     unsigned char md[EVP_MAX_MD_SIZE] = { '\0' };
1267    
1268     if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN ||
1269     res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE ||
1270     res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
1271     {
1272     unsigned int i = 0, n = 0;
1273    
1274     if (X509_digest(cert, EVP_sha256(), md, &n))
1275     {
1276     for (; i < n; ++i)
1277     snprintf(buf + 2 * i, 3, "%02X", md[i]);
1278     client_p->certfp = xstrdup(buf);
1279     }
1280     }
1281     else
1282     ilog(LOG_TYPE_IRCD, "Server %s!%s@%s gave bad SSL client certificate: %d",
1283     client_p->name, client_p->username, client_p->host, res);
1284     X509_free(cert);
1285     }
1286    
1287 michael 1303 finish_ssl_server_handshake(client_p);
1288     }
1289    
1290     static void
1291 michael 1632 ssl_connect_init(struct Client *client_p, struct MaskItem *conf, fde_t *fd)
1292 michael 1303 {
1293     if ((client_p->localClient->fd.ssl = SSL_new(ServerInfo.client_ctx)) == NULL)
1294     {
1295     ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
1296     ERR_error_string(ERR_get_error(), NULL));
1297     SetDead(client_p);
1298 michael 3171 exit_client(client_p, "SSL_new failed");
1299 michael 1303 return;
1300     }
1301    
1302     SSL_set_fd(fd->ssl, fd->fd);
1303 michael 1306
1304 michael 1632 if (!EmptyString(conf->cipher_list))
1305     SSL_set_cipher_list(client_p->localClient->fd.ssl, conf->cipher_list);
1306 michael 1306
1307     ssl_server_handshake(NULL, client_p);
1308 michael 1303 }
1309     #endif
1310    
1311 adx 30 /* serv_connect_callback() - complete a server connection.
1312 michael 2345 *
1313 adx 30 * This routine is called after the server connection attempt has
1314     * completed. If unsucessful, an error is sent to ops and the client
1315     * is closed. If sucessful, it goes through the initialisation/check
1316     * procedures, the capabilities are sent, and the socket is then
1317     * marked for reading.
1318     */
1319     static void
1320     serv_connect_callback(fde_t *fd, int status, void *data)
1321     {
1322     struct Client *client_p = data;
1323 michael 1632 struct MaskItem *conf = NULL;
1324 adx 30
1325     /* First, make sure its a real client! */
1326 michael 3246 assert(client_p);
1327 adx 30 assert(&client_p->localClient->fd == fd);
1328    
1329     /* Next, for backward purposes, record the ip of the server */
1330     memcpy(&client_p->localClient->ip, &fd->connect.hostaddr,
1331     sizeof(struct irc_ssaddr));
1332 michael 3246
1333 adx 30 /* Check the status */
1334     if (status != COMM_OK)
1335     {
1336     /* We have an error, so report it and quit
1337     * Admins get to see any IP, mere opers don't *sigh*
1338     */
1339     if (ConfigServerHide.hide_server_ips)
1340 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1341 adx 30 "Error connecting to %s: %s",
1342     client_p->name, comm_errstr(status));
1343     else
1344 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1345 michael 2134 "Error connecting to %s[%s]: %s", client_p->name,
1346     client_p->host, comm_errstr(status));
1347 adx 30
1348 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1349 michael 2134 "Error connecting to %s: %s",
1350     client_p->name, comm_errstr(status));
1351 adx 30
1352     /* If a fd goes bad, call dead_link() the socket is no
1353     * longer valid for reading or writing.
1354     */
1355     dead_link_on_write(client_p, 0);
1356     return;
1357     }
1358    
1359     /* COMM_OK, so continue the connection procedure */
1360     /* Get the C/N lines */
1361     conf = find_conf_name(&client_p->localClient->confs,
1362 michael 2134 client_p->name, CONF_SERVER);
1363 adx 30 if (conf == NULL)
1364     {
1365 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1366 michael 2134 "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
1367 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1368 michael 2134 "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
1369 adx 30
1370 michael 3171 exit_client(client_p, "Lost connect{} block");
1371 adx 30 return;
1372     }
1373    
1374     /* Next, send the initial handshake */
1375     SetHandshake(client_p);
1376    
1377     #ifdef HAVE_LIBCRYPTO
1378 michael 1632 if (IsConfSSL(conf))
1379 michael 1303 {
1380 michael 1632 ssl_connect_init(client_p, conf, fd);
1381 michael 1303 return;
1382     }
1383 adx 30 #endif
1384    
1385 michael 2134 sendto_one(client_p, "PASS %s TS %d %s", conf->spasswd, TS_CURRENT, me.id);
1386 adx 30
1387 michael 1632 send_capabilities(client_p, 0);
1388 adx 30
1389 michael 2134 sendto_one(client_p, "SERVER %s 1 :%s%s", me.name,
1390     ConfigServerHide.hidden ? "(H) " : "", me.info);
1391 adx 30
1392     /* If we've been marked dead because a send failed, just exit
1393     * here now and save everyone the trouble of us ever existing.
1394     */
1395 michael 2345 if (IsDead(client_p))
1396 adx 30 {
1397 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1398 michael 2134 "%s[%s] went dead during handshake",
1399 adx 30 client_p->name,
1400 michael 2134 client_p->host);
1401 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1402 michael 2134 "%s went dead during handshake", client_p->name);
1403 adx 30 return;
1404     }
1405    
1406     /* don't move to serv_list yet -- we haven't sent a burst! */
1407     /* If we get here, we're ok, so lets start reading some data */
1408     comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0);
1409     }
1410    
1411     struct Client *
1412     find_servconn_in_progress(const char *name)
1413     {
1414     dlink_node *ptr;
1415     struct Client *cptr;
1416    
1417     DLINK_FOREACH(ptr, unknown_list.head)
1418     {
1419     cptr = ptr->data;
1420    
1421     if (cptr && cptr->name[0])
1422 michael 1652 if (!match(name, cptr->name))
1423 adx 30 return cptr;
1424     }
1425 michael 2345
1426 adx 30 return NULL;
1427     }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision