ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/server.c
Revision: 2216
Committed: Thu Jun 6 17:35:45 2013 UTC (13 years, 4 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid/trunk/src/s_serv.c
File size: 44037 byte(s)
Log Message:
- Added read_links_file()

File Contents

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

Properties

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