ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/server.c
Revision: 1592
Committed: Sat Oct 27 21:02:32 2012 UTC (12 years, 10 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid/trunk/src/s_serv.c
File size: 44318 byte(s)
Log Message:
- Second time's the charm? Moving svnroot/ircd-hybrid-8 to
  svnroot/ircd-hybrid/trunk

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

Properties

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