ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/server.c
Revision: 2916
Committed: Sat Jan 25 21:09:18 2014 UTC (10 years, 2 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid/trunk/src/s_serv.c
File size: 45217 byte(s)
Log Message:
- Clean up all files in include/ (fixed indentation, removed whitespaces/tabs)
- Fixed copyright years

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

Properties

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