ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_serv.c
Revision: 1618
Committed: Tue Oct 30 21:04:38 2012 UTC (12 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 44723 byte(s)
Log Message:
- Made m_globops() and ms_globops() use sendto_realops_flags()
- Added message-type parameter to sendto_realops_flags() which can be one of
  SEND_NOTICE, SEND_GLOBAL, SEND_LOCOPS
- Forward-port -r1617

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 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
345     "Connection to %s activated.",
346 adx 30 conf->name);
347     else
348 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
349     "Connection to %s[%s] activated.",
350 adx 30 conf->name, aconf->host);
351    
352     serv_connect(aconf, NULL);
353     /* We connect only one at time... */
354     return;
355     }
356     }
357     }
358    
359     int
360 michael 1115 valid_servname(const char *name)
361     {
362     unsigned int length = 0;
363     unsigned int dots = 0;
364     const char *p = name;
365    
366     for (; *p; ++p)
367     {
368     if (!IsServChar(*p))
369 michael 1118 return 0;
370 michael 1115
371     ++length;
372    
373     if (*p == '.')
374     ++dots;
375     }
376    
377 michael 1118 return dots != 0 && length <= HOSTLEN;
378 michael 1115 }
379    
380     int
381 michael 1302 check_server(const char *name, struct Client *client_p)
382 adx 30 {
383     dlink_node *ptr;
384     struct ConfItem *conf = NULL;
385     struct ConfItem *server_conf = NULL;
386     struct AccessItem *server_aconf = NULL;
387     struct AccessItem *aconf = NULL;
388     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 michael 1013 aconf = map_to_conf(conf);
397 adx 30
398     if (!match(name, conf->name))
399     continue;
400    
401     error = -3;
402    
403     /* XXX: Fix me for IPv6 */
404     /* XXX sockhost is the IPv4 ip as a string */
405     if (match(aconf->host, client_p->host) ||
406     match(aconf->host, client_p->sockhost))
407     {
408     error = -2;
409    
410 michael 1414 if (!match_conf_password(client_p->localClient->passwd, aconf))
411     return -2;
412 adx 30
413 michael 1414 server_conf = conf;
414 adx 30 }
415     }
416    
417     if (server_conf == NULL)
418     return(error);
419    
420     attach_conf(client_p, server_conf);
421    
422 michael 885 server_aconf = map_to_conf(server_conf);
423 adx 30
424     if (aconf != NULL)
425     {
426     struct sockaddr_in *v4;
427     #ifdef IPV6
428     struct sockaddr_in6 *v6;
429     #endif
430     switch (aconf->aftype)
431     {
432     #ifdef IPV6
433     case AF_INET6:
434 michael 1389 v6 = (struct sockaddr_in6 *)&aconf->addr;
435 adx 30
436     if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr))
437 michael 1389 memcpy(&aconf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
438 adx 30 break;
439     #endif
440     case AF_INET:
441 michael 1389 v4 = (struct sockaddr_in *)&aconf->addr;
442 adx 30
443     if (v4->sin_addr.s_addr == INADDR_NONE)
444 michael 1389 memcpy(&aconf->addr, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
445 adx 30 break;
446     }
447     }
448    
449     return(0);
450     }
451    
452     /* add_capability()
453     *
454     * inputs - string name of CAPAB
455     * - int flag of capability
456     * output - NONE
457     * side effects - Adds given capability name and bit mask to
458     * current supported capabilities. This allows
459     * modules to dynamically add or subtract their capability.
460     */
461     void
462     add_capability(const char *capab_name, int cap_flag, int add_to_default)
463     {
464 michael 885 struct Capability *cap = MyMalloc(sizeof(*cap));
465 adx 30
466     DupString(cap->name, capab_name);
467     cap->cap = cap_flag;
468     dlinkAdd(cap, &cap->node, &cap_list);
469 michael 885
470 adx 30 if (add_to_default)
471     default_server_capabs |= cap_flag;
472     }
473    
474     /* delete_capability()
475     *
476     * inputs - string name of CAPAB
477     * output - NONE
478     * side effects - delete given capability from ones known.
479     */
480     int
481     delete_capability(const char *capab_name)
482     {
483     dlink_node *ptr;
484     dlink_node *next_ptr;
485     struct Capability *cap;
486    
487     DLINK_FOREACH_SAFE(ptr, next_ptr, cap_list.head)
488     {
489     cap = ptr->data;
490    
491     if (cap->cap != 0)
492     {
493     if (irccmp(cap->name, capab_name) == 0)
494     {
495     default_server_capabs &= ~(cap->cap);
496     dlinkDelete(ptr, &cap_list);
497     MyFree(cap->name);
498     cap->name = NULL;
499     MyFree(cap);
500     }
501     }
502     }
503    
504 michael 896 return 0;
505 adx 30 }
506    
507     /*
508     * find_capability()
509     *
510     * inputs - string name of capab to find
511     * output - 0 if not found CAPAB otherwise
512     * side effects - none
513     */
514     int
515     find_capability(const char *capab)
516     {
517 michael 896 const dlink_node *ptr = NULL;
518 adx 30
519     DLINK_FOREACH(ptr, cap_list.head)
520     {
521 michael 896 const struct Capability *cap = ptr->data;
522 adx 30
523 michael 896 if (cap->cap && !irccmp(cap->name, capab))
524     return cap->cap;
525 adx 30 }
526 michael 896
527     return 0;
528 adx 30 }
529    
530     /* send_capabilities()
531     *
532     * inputs - Client pointer to send to
533     * - Pointer to AccessItem (for crypt)
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     send_capabilities(struct Client *client_p, struct AccessItem *aconf,
541 michael 1302 int cap_can_send)
542 adx 30 {
543     struct Capability *cap=NULL;
544     char msgbuf[IRCD_BUFSIZE];
545     char *t;
546     int tl;
547     dlink_node *ptr;
548    
549     t = msgbuf;
550    
551     DLINK_FOREACH(ptr, cap_list.head)
552     {
553     cap = ptr->data;
554    
555     if (cap->cap & (cap_can_send|default_server_capabs))
556     {
557     tl = ircsprintf(t, "%s ", cap->name);
558     t += tl;
559     }
560     }
561    
562 michael 319 *(t - 1) = '\0';
563 adx 30 sendto_one(client_p, "CAPAB :%s", msgbuf);
564     }
565    
566     /* sendnick_TS()
567     *
568     * inputs - client (server) to send nick towards
569 adx 386 * - client to send nick for
570 adx 30 * output - NONE
571     * side effects - NICK message is sent towards given client_p
572     */
573     void
574     sendnick_TS(struct Client *client_p, struct Client *target_p)
575     {
576     static char ubuf[12];
577    
578     if (!IsClient(target_p))
579     return;
580    
581 michael 1294 send_umode(NULL, target_p, 0, SEND_UMODES, ubuf);
582 adx 30
583     if (ubuf[0] == '\0')
584     {
585     ubuf[0] = '+';
586     ubuf[1] = '\0';
587     }
588    
589 michael 1196 if (IsCapable(client_p, CAP_SVS))
590     {
591     if (HasID(target_p) && IsCapable(client_p, CAP_TS6))
592 michael 1559 sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s %s :%s",
593 michael 1196 target_p->servptr->id,
594     target_p->name, target_p->hopcount + 1,
595     (unsigned long) target_p->tsinfo,
596     ubuf, target_p->username, target_p->host,
597     (MyClient(target_p) && IsIPSpoof(target_p)) ?
598     "0" : target_p->sockhost, target_p->id,
599 michael 1559 target_p->svid, target_p->info);
600 michael 1196 else
601 michael 1559 sendto_one(client_p, "NICK %s %d %lu %s %s %s %s %s :%s",
602 michael 1196 target_p->name, target_p->hopcount + 1,
603     (unsigned long) target_p->tsinfo,
604     ubuf, target_p->username, target_p->host,
605 michael 1559 target_p->servptr->name, target_p->svid,
606 michael 1196 target_p->info);
607     }
608 adx 30 else
609 michael 1196 {
610     if (HasID(target_p) && IsCapable(client_p, CAP_TS6))
611     sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s :%s",
612     target_p->servptr->id,
613     target_p->name, target_p->hopcount + 1,
614     (unsigned long) target_p->tsinfo,
615     ubuf, target_p->username, target_p->host,
616     (MyClient(target_p) && IsIPSpoof(target_p)) ?
617     "0" : target_p->sockhost, target_p->id, target_p->info);
618     else
619     sendto_one(client_p, "NICK %s %d %lu %s %s %s %s :%s",
620     target_p->name, target_p->hopcount + 1,
621     (unsigned long) target_p->tsinfo,
622     ubuf, target_p->username, target_p->host,
623     target_p->servptr->name, target_p->info);
624     }
625 adx 386
626 michael 1519 if (target_p->away[0])
627     sendto_one(client_p, ":%s AWAY :%s", ID_or_name(target_p, client_p),
628     target_p->away);
629 adx 30
630     }
631    
632     /*
633     * show_capabilities - show current server capabilities
634     *
635     * inputs - pointer to a struct Client
636     * output - pointer to static string
637     * side effects - build up string representing capabilities of server listed
638     */
639     const char *
640     show_capabilities(struct Client *target_p)
641     {
642     static char msgbuf[IRCD_BUFSIZE];
643     char *t = msgbuf;
644     dlink_node *ptr;
645    
646     t += ircsprintf(msgbuf, "TS ");
647    
648     DLINK_FOREACH(ptr, cap_list.head)
649     {
650     const struct Capability *cap = ptr->data;
651    
652     if (IsCapable(target_p, cap->cap))
653     t += ircsprintf(t, "%s ", cap->name);
654     }
655 michael 1302
656 adx 30 *(t - 1) = '\0';
657 michael 1302 return msgbuf;
658 adx 30 }
659    
660     /* make_server()
661     *
662     * inputs - pointer to client struct
663     * output - pointer to struct Server
664     * side effects - add's an Server information block to a client
665     * if it was not previously allocated.
666     */
667     struct Server *
668     make_server(struct Client *client_p)
669     {
670     if (client_p->serv == NULL)
671     client_p->serv = MyMalloc(sizeof(struct Server));
672    
673     return client_p->serv;
674     }
675    
676     /* server_estab()
677     *
678     * inputs - pointer to a struct Client
679     * output -
680     * side effects -
681     */
682     void
683     server_estab(struct Client *client_p)
684     {
685     struct Client *target_p;
686     struct ConfItem *conf;
687     struct AccessItem *aconf=NULL;
688     char *host;
689     const char *inpath;
690     static char inpath_ip[HOSTLEN * 2 + USERLEN + 6];
691     dlink_node *ptr;
692 michael 1305 #ifdef HAVE_LIBCRYPTO
693     const COMP_METHOD *compression = NULL, *expansion = NULL;
694     #endif
695 adx 30
696     assert(client_p != NULL);
697    
698     strlcpy(inpath_ip, get_client_name(client_p, SHOW_IP), sizeof(inpath_ip));
699    
700     inpath = get_client_name(client_p, MASK_IP); /* "refresh" inpath with host */
701     host = client_p->name;
702    
703     if ((conf = find_conf_name(&client_p->localClient->confs, host, SERVER_TYPE))
704     == NULL)
705     {
706     /* This shouldn't happen, better tell the ops... -A1kmm */
707 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
708     "Warning: Lost connect{} block "
709 adx 30 "for server %s(this shouldn't happen)!", host);
710     exit_client(client_p, &me, "Lost connect{} block!");
711     return;
712     }
713    
714     MyFree(client_p->localClient->passwd);
715     client_p->localClient->passwd = NULL;
716    
717     /* Its got identd, since its a server */
718     SetGotId(client_p);
719    
720     /* If there is something in the serv_list, it might be this
721     * connecting server..
722     */
723     if (!ServerInfo.hub && serv_list.head)
724     {
725     if (client_p != serv_list.head->data || serv_list.head->next)
726     {
727 michael 896 ++ServerStats.is_ref;
728 adx 30 sendto_one(client_p, "ERROR :I'm a leaf not a hub");
729     exit_client(client_p, &me, "I'm a leaf");
730     return;
731     }
732     }
733    
734 michael 1115 aconf = map_to_conf(conf);
735 adx 30
736 michael 1302 if (IsUnknown(client_p))
737 adx 30 {
738     /* jdc -- 1. Use EmptyString(), not [0] index reference.
739     * 2. Check aconf->spasswd, not aconf->passwd.
740     */
741     if (!EmptyString(aconf->spasswd))
742 michael 1115 sendto_one(client_p, "PASS %s TS %d %s",
743     aconf->spasswd, TS_CURRENT, me.id);
744 adx 30
745     /* Pass my info to the new server
746     *
747     * Pass on ZIP if supported
748     * Pass on TB if supported.
749     * - Dianora
750     */
751    
752 michael 1519 send_capabilities(client_p, aconf, 0);
753 adx 30
754     sendto_one(client_p, "SERVER %s 1 :%s%s",
755 michael 1118 me.name, ConfigServerHide.hidden ? "(H) " : "", me.info);
756 adx 30 }
757    
758 michael 1115 sendto_one(client_p, "SVINFO %d %d 0 :%lu", TS_CURRENT, TS_MIN,
759 adx 30 (unsigned long)CurrentTime);
760    
761     /* assumption here is if they passed the correct TS version, they also passed an SID */
762     if (IsCapable(client_p, CAP_TS6))
763     hash_add_id(client_p);
764    
765     /* XXX Does this ever happen? I don't think so -db */
766     detach_conf(client_p, OPER_TYPE);
767    
768     /* *WARNING*
769     ** In the following code in place of plain server's
770     ** name we send what is returned by get_client_name
771     ** which may add the "sockhost" after the name. It's
772     ** *very* *important* that there is a SPACE between
773     ** the name and sockhost (if present). The receiving
774     ** server will start the information field from this
775     ** first blank and thus puts the sockhost into info.
776     ** ...a bit tricky, but you have been warned, besides
777     ** code is more neat this way... --msa
778     */
779     client_p->servptr = &me;
780    
781     if (IsClosing(client_p))
782     return;
783    
784     SetServer(client_p);
785    
786     /* Update the capability combination usage counts. -A1kmm */
787     set_chcap_usage_counts(client_p);
788    
789     /* Some day, all these lists will be consolidated *sigh* */
790 michael 889 dlinkAdd(client_p, &client_p->lnode, &me.serv->server_list);
791 adx 30
792 michael 1126 assert(dlinkFind(&unknown_list, client_p));
793 adx 30
794 michael 1126 dlink_move_node(&client_p->localClient->lclient_node,
795     &unknown_list, &serv_list);
796 adx 30
797     Count.myserver++;
798    
799     dlinkAdd(client_p, make_dlink_node(), &global_serv_list);
800     hash_add_client(client_p);
801    
802     /* doesnt duplicate client_p->serv if allocated this struct already */
803     make_server(client_p);
804    
805     /* fixing eob timings.. -gnp */
806 michael 1241 client_p->localClient->firsttime = CurrentTime;
807 adx 30
808 michael 1157 if (find_matching_name_conf(SERVICE_TYPE, client_p->name, NULL, NULL, 0))
809 michael 1219 AddFlag(client_p, FLAGS_SERVICE);
810 michael 1157
811 adx 30 /* Show the real host/IP to admins */
812 michael 1305 #ifdef HAVE_LIBCRYPTO
813 michael 1303 if (client_p->localClient->fd.ssl)
814     {
815 michael 1305 compression = SSL_get_current_compression(client_p->localClient->fd.ssl);
816     expansion = SSL_get_current_expansion(client_p->localClient->fd.ssl);
817    
818 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
819 michael 1305 "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
820 michael 1303 inpath_ip, ssl_get_cipher(client_p->localClient->fd.ssl),
821 michael 1305 compression ? SSL_COMP_get_name(compression) : "NONE",
822     expansion ? SSL_COMP_get_name(expansion) : "NONE",
823 michael 1303 show_capabilities(client_p));
824     /* Now show the masked hostname/IP to opers */
825 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
826 michael 1305 "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
827 michael 1303 inpath, ssl_get_cipher(client_p->localClient->fd.ssl),
828 michael 1305 compression ? SSL_COMP_get_name(compression) : "NONE",
829     expansion ? SSL_COMP_get_name(expansion) : "NONE",
830 michael 1303 show_capabilities(client_p));
831 michael 1305 ilog(LOG_TYPE_IRCD, "Link with %s established: [SSL: %s, Compression/Expansion method: %s/%s] (Capabilities: %s)",
832 michael 1303 inpath_ip, ssl_get_cipher(client_p->localClient->fd.ssl),
833 michael 1305 compression ? SSL_COMP_get_name(compression) : "NONE",
834     expansion ? SSL_COMP_get_name(expansion) : "NONE",
835 michael 1303 show_capabilities(client_p));
836     }
837     else
838 michael 1305 #endif
839 michael 1303 {
840 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
841 michael 1303 "Link with %s established: (Capabilities: %s)",
842 michael 1618 inpath_ip, show_capabilities(client_p));
843 michael 1303 /* Now show the masked hostname/IP to opers */
844 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
845 michael 1303 "Link with %s established: (Capabilities: %s)",
846 michael 1618 inpath, show_capabilities(client_p));
847 michael 1303 ilog(LOG_TYPE_IRCD, "Link with %s established: (Capabilities: %s)",
848     inpath_ip, show_capabilities(client_p));
849     }
850 adx 30
851 michael 1302 fd_note(&client_p->localClient->fd, "Server: %s", client_p->name);
852 adx 30
853     /* Old sendto_serv_but_one() call removed because we now
854     ** need to send different names to different servers
855     ** (domain name matching) Send new server to other servers.
856     */
857     DLINK_FOREACH(ptr, serv_list.head)
858     {
859     target_p = ptr->data;
860    
861     if (target_p == client_p)
862     continue;
863    
864     if (IsCapable(target_p, CAP_TS6) && HasID(client_p))
865     sendto_one(target_p, ":%s SID %s 2 %s :%s%s",
866     me.id, client_p->name, client_p->id,
867     IsHidden(client_p) ? "(H) " : "",
868     client_p->info);
869     else
870     sendto_one(target_p,":%s SERVER %s 2 :%s%s",
871     me.name, client_p->name,
872     IsHidden(client_p) ? "(H) " : "",
873     client_p->info);
874     }
875    
876     /* Pass on my client information to the new server
877     **
878     ** First, pass only servers (idea is that if the link gets
879     ** cancelled beacause the server was already there,
880     ** there are no NICK's to be cancelled...). Of course,
881     ** if cancellation occurs, all this info is sent anyway,
882     ** and I guess the link dies when a read is attempted...? --msa
883     **
884     ** Note: Link cancellation to occur at this point means
885     ** that at least two servers from my fragment are building
886     ** up connection this other fragment at the same time, it's
887     ** a race condition, not the normal way of operation...
888     **
889     ** ALSO NOTE: using the get_client_name for server names--
890     ** see previous *WARNING*!!! (Also, original inpath
891     ** is destroyed...)
892     */
893    
894     DLINK_FOREACH_PREV(ptr, global_serv_list.tail)
895     {
896     target_p = ptr->data;
897    
898     /* target_p->from == target_p for target_p == client_p */
899 michael 1118 if (IsMe(target_p) || target_p->from == client_p)
900 adx 30 continue;
901    
902     if (IsCapable(client_p, CAP_TS6))
903     {
904     if (HasID(target_p))
905     sendto_one(client_p, ":%s SID %s %d %s :%s%s",
906     ID(target_p->servptr), target_p->name, target_p->hopcount+1,
907     target_p->id, IsHidden(target_p) ? "(H) " : "",
908     target_p->info);
909     else /* introducing non-ts6 server */
910     sendto_one(client_p, ":%s SERVER %s %d :%s%s",
911     ID(target_p->servptr), target_p->name, target_p->hopcount+1,
912     IsHidden(target_p) ? "(H) " : "", target_p->info);
913     }
914     else
915     sendto_one(client_p, ":%s SERVER %s %d :%s%s",
916     target_p->servptr->name, target_p->name, target_p->hopcount+1,
917     IsHidden(target_p) ? "(H) " : "", target_p->info);
918     }
919    
920     server_burst(client_p);
921     }
922    
923     /* server_burst()
924     *
925     * inputs - struct Client pointer server
926     * -
927     * output - none
928     * side effects - send a server burst
929     * bugs - still too long
930     */
931     static void
932     server_burst(struct Client *client_p)
933     {
934     /* Send it in the shortened format with the TS, if
935     ** it's a TS server; walk the list of channels, sending
936     ** all the nicks that haven't been sent yet for each
937     ** channel, then send the channel itself -- it's less
938     ** obvious than sending all nicks first, but on the
939     ** receiving side memory will be allocated more nicely
940     ** saving a few seconds in the handling of a split
941     ** -orabidoo
942     */
943    
944 michael 885 burst_all(client_p);
945 adx 30
946     /* EOB stuff is now in burst_all */
947     /* Always send a PING after connect burst is done */
948     sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p));
949     }
950    
951     /* burst_all()
952     *
953     * inputs - pointer to server to send burst to
954     * output - NONE
955     * side effects - complete burst of channels/nicks is sent to client_p
956     */
957     static void
958     burst_all(struct Client *client_p)
959     {
960 michael 329 dlink_node *ptr = NULL;
961 adx 30
962 michael 329 DLINK_FOREACH(ptr, global_channel_list.head)
963 adx 30 {
964 michael 329 struct Channel *chptr = ptr->data;
965 adx 30
966     if (dlink_list_length(&chptr->members) != 0)
967     {
968     burst_members(client_p, chptr);
969     send_channel_modes(client_p, chptr);
970 michael 329
971 michael 1472 if (IsCapable(client_p, CAP_TBURST))
972 adx 30 send_tb(client_p, chptr);
973     }
974     }
975    
976     /* also send out those that are not on any channel
977     */
978     DLINK_FOREACH(ptr, global_client_list.head)
979     {
980 michael 329 struct Client *target_p = ptr->data;
981 adx 30
982 michael 1219 if (!HasFlag(target_p, FLAGS_BURSTED) && target_p->from != client_p)
983 adx 30 sendnick_TS(client_p, target_p);
984    
985 michael 1219 DelFlag(target_p, FLAGS_BURSTED);
986 adx 30 }
987    
988     /* We send the time we started the burst, and let the remote host determine an EOB time,
989     ** as otherwise we end up sending a EOB of 0 Sending here means it gets sent last -- fl
990     */
991     /* Its simpler to just send EOB and use the time its been connected.. --fl_ */
992     if (IsCapable(client_p, CAP_EOB))
993     sendto_one(client_p, ":%s EOB", ID_or_name(&me, client_p));
994     }
995    
996     /*
997     * send_tb
998     *
999 michael 329 * inputs - pointer to Client
1000     * - pointer to channel
1001     * output - NONE
1002     * side effects - Called on a server burst when
1003 michael 1472 * server is CAP_TBURST capable
1004 adx 30 */
1005     static void
1006     send_tb(struct Client *client_p, struct Channel *chptr)
1007     {
1008 michael 329 /*
1009 michael 337 * We may also send an empty topic here, but only if topic_time isn't 0,
1010     * i.e. if we had a topic that got unset. This is required for syncing
1011     * topics properly.
1012     *
1013     * Imagine the following scenario: Our downlink introduces a channel
1014     * to us with a TS that is equal to ours, but the channel topic on
1015     * their side got unset while the servers were in splitmode, which means
1016     * their 'topic' is newer. They simply wanted to unset it, so we have to
1017     * deal with it in a more sophisticated fashion instead of just resetting
1018     * it to their old topic they had before. Read m_tburst.c:ms_tburst
1019     * for further information -Michael
1020 michael 329 */
1021 michael 337 if (chptr->topic_time != 0)
1022 michael 1472 sendto_one(client_p, ":%s TBURST %lu %s %lu %s :%s",
1023     ID_or_name(&me, client_p),
1024     (unsigned long)chptr->channelts, chptr->chname,
1025     (unsigned long)chptr->topic_time,
1026     chptr->topic_info,
1027     chptr->topic);
1028 adx 30 }
1029    
1030     /* burst_members()
1031     *
1032     * inputs - pointer to server to send members to
1033     * - dlink_list pointer to membership list to send
1034     * output - NONE
1035     * side effects -
1036     */
1037     static void
1038     burst_members(struct Client *client_p, struct Channel *chptr)
1039     {
1040     struct Client *target_p;
1041     struct Membership *ms;
1042     dlink_node *ptr;
1043    
1044     DLINK_FOREACH(ptr, chptr->members.head)
1045     {
1046     ms = ptr->data;
1047     target_p = ms->client_p;
1048    
1049 michael 1219 if (!HasFlag(target_p, FLAGS_BURSTED))
1050 adx 30 {
1051 michael 1219 AddFlag(target_p, FLAGS_BURSTED);
1052 adx 30
1053     if (target_p->from != client_p)
1054     sendnick_TS(client_p, target_p);
1055     }
1056     }
1057     }
1058    
1059     /* New server connection code
1060     * Based upon the stuff floating about in s_bsd.c
1061     * -- adrian
1062     */
1063    
1064     /* serv_connect() - initiate a server connection
1065     *
1066     * inputs - pointer to conf
1067     * - pointer to client doing the connect
1068     * output -
1069     * side effects -
1070     *
1071     * This code initiates a connection to a server. It first checks to make
1072     * sure the given server exists. If this is the case, it creates a socket,
1073     * creates a client, saves the socket information in the client, and
1074     * initiates a connection to the server through comm_connect_tcp(). The
1075     * completion of this goes through serv_completed_connection().
1076     *
1077     * We return 1 if the connection is attempted, since we don't know whether
1078     * it suceeded or not, and 0 if it fails in here somewhere.
1079     */
1080     int
1081     serv_connect(struct AccessItem *aconf, struct Client *by)
1082     {
1083     struct ConfItem *conf;
1084     struct Client *client_p;
1085 michael 1398 char buf[HOSTIPLEN + 1];
1086 adx 30
1087     /* conversion structs */
1088     struct sockaddr_in *v4;
1089     /* Make sure aconf is useful */
1090     assert(aconf != NULL);
1091    
1092     /* XXX should be passing struct ConfItem in the first place */
1093     conf = unmap_conf_item(aconf);
1094    
1095     /* log */
1096 michael 1389 getnameinfo((struct sockaddr *)&aconf->addr, aconf->addr.ss_len,
1097 michael 1123 buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
1098 michael 1413 ilog(LOG_TYPE_IRCD, "Connect to %s[%s] @%s", conf->name, aconf->host,
1099 adx 30 buf);
1100    
1101     /* Still processing a DNS lookup? -> exit */
1102 michael 992 if (aconf->dns_pending)
1103 adx 30 {
1104 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1105 michael 992 "Error connecting to %s: DNS lookup for connect{} in progress.",
1106     conf->name);
1107 adx 30 return (0);
1108     }
1109    
1110 michael 992 if (aconf->dns_failed)
1111     {
1112 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1113 michael 992 "Error connecting to %s: DNS lookup for connect{} failed.",
1114     conf->name);
1115     return (0);
1116     }
1117    
1118 adx 30 /* Make sure this server isn't already connected
1119     * Note: aconf should ALWAYS be a valid C: line
1120     */
1121 michael 1169 if ((client_p = hash_find_server(conf->name)) != NULL)
1122 adx 30 {
1123 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1124 adx 30 "Server %s already present from %s",
1125     conf->name, get_client_name(client_p, SHOW_IP));
1126 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1127 adx 30 "Server %s already present from %s",
1128     conf->name, get_client_name(client_p, MASK_IP));
1129     if (by && IsClient(by) && !MyClient(by))
1130     sendto_one(by, ":%s NOTICE %s :Server %s already present from %s",
1131     me.name, by->name, conf->name,
1132     get_client_name(client_p, MASK_IP));
1133     return (0);
1134     }
1135    
1136     /* Create a local client */
1137     client_p = make_client(NULL);
1138    
1139     /* Copy in the server, hostname, fd */
1140     strlcpy(client_p->name, conf->name, sizeof(client_p->name));
1141     strlcpy(client_p->host, aconf->host, sizeof(client_p->host));
1142    
1143     /* We already converted the ip once, so lets use it - stu */
1144 michael 1115 strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost));
1145 adx 30
1146     /* create a socket for the server connection */
1147 michael 1389 if (comm_open(&client_p->localClient->fd, aconf->addr.ss.ss_family,
1148 adx 30 SOCK_STREAM, 0, NULL) < 0)
1149     {
1150     /* Eek, failure to create the socket */
1151     report_error(L_ALL,
1152     "opening stream socket to %s: %s", conf->name, errno);
1153     SetDead(client_p);
1154     exit_client(client_p, &me, "Connection failed");
1155     return (0);
1156     }
1157    
1158     /* servernames are always guaranteed under HOSTLEN chars */
1159     fd_note(&client_p->localClient->fd, "Server: %s", conf->name);
1160    
1161     /* Attach config entries to client here rather than in
1162     * serv_connect_callback(). This to avoid null pointer references.
1163     */
1164     if (!attach_connect_block(client_p, conf->name, aconf->host))
1165     {
1166 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1167     "Host %s is not enabled for connecting: no connect{} block",
1168 adx 30 conf->name);
1169     if (by && IsClient(by) && !MyClient(by))
1170     sendto_one(by, ":%s NOTICE %s :Connect to host %s failed.",
1171     me.name, by->name, client_p->name);
1172     SetDead(client_p);
1173     exit_client(client_p, client_p, "Connection failed");
1174     return (0);
1175     }
1176    
1177     /* at this point we have a connection in progress and C/N lines
1178     * attached to the client, the socket info should be saved in the
1179     * client and it should either be resolved or have a valid address.
1180     *
1181     * The socket has been connected or connect is in progress.
1182     */
1183     make_server(client_p);
1184    
1185     if (by && IsClient(by))
1186     strlcpy(client_p->serv->by, by->name, sizeof(client_p->serv->by));
1187     else
1188     strlcpy(client_p->serv->by, "AutoConn.", sizeof(client_p->serv->by));
1189    
1190     SetConnecting(client_p);
1191     dlinkAdd(client_p, &client_p->node, &global_client_list);
1192     /* from def_fam */
1193     client_p->localClient->aftype = aconf->aftype;
1194    
1195     /* Now, initiate the connection */
1196     /* XXX assume that a non 0 type means a specific bind address
1197     * for this connect.
1198     */
1199     switch (aconf->aftype)
1200     {
1201     case AF_INET:
1202 michael 1389 v4 = (struct sockaddr_in*)&aconf->bind;
1203 adx 30 if (v4->sin_addr.s_addr != 0)
1204     {
1205     struct irc_ssaddr ipn;
1206     memset(&ipn, 0, sizeof(struct irc_ssaddr));
1207     ipn.ss.ss_family = AF_INET;
1208     ipn.ss_port = 0;
1209 michael 1389 memcpy(&ipn, &aconf->bind, sizeof(struct irc_ssaddr));
1210 adx 30 comm_connect_tcp(&client_p->localClient->fd, aconf->host, aconf->port,
1211     (struct sockaddr *)&ipn, ipn.ss_len,
1212     serv_connect_callback, client_p, aconf->aftype,
1213     CONNECTTIMEOUT);
1214     }
1215     else if (ServerInfo.specific_ipv4_vhost)
1216     {
1217     struct irc_ssaddr ipn;
1218     memset(&ipn, 0, sizeof(struct irc_ssaddr));
1219     ipn.ss.ss_family = AF_INET;
1220     ipn.ss_port = 0;
1221     memcpy(&ipn, &ServerInfo.ip, sizeof(struct irc_ssaddr));
1222     comm_connect_tcp(&client_p->localClient->fd, aconf->host, aconf->port,
1223     (struct sockaddr *)&ipn, ipn.ss_len,
1224     serv_connect_callback, client_p, aconf->aftype,
1225     CONNECTTIMEOUT);
1226     }
1227     else
1228     comm_connect_tcp(&client_p->localClient->fd, aconf->host, aconf->port,
1229     NULL, 0, serv_connect_callback, client_p, aconf->aftype,
1230     CONNECTTIMEOUT);
1231     break;
1232     #ifdef IPV6
1233     case AF_INET6:
1234     {
1235     struct irc_ssaddr ipn;
1236     struct sockaddr_in6 *v6;
1237     struct sockaddr_in6 *v6conf;
1238    
1239     memset(&ipn, 0, sizeof(struct irc_ssaddr));
1240 michael 1389 v6conf = (struct sockaddr_in6 *)&aconf->bind;
1241 adx 30 v6 = (struct sockaddr_in6 *)&ipn;
1242    
1243     if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr,
1244     sizeof(struct in6_addr)) != 0)
1245     {
1246 michael 1389 memcpy(&ipn, &aconf->bind, sizeof(struct irc_ssaddr));
1247 adx 30 ipn.ss.ss_family = AF_INET6;
1248     ipn.ss_port = 0;
1249     comm_connect_tcp(&client_p->localClient->fd,
1250     aconf->host, aconf->port,
1251     (struct sockaddr *)&ipn, ipn.ss_len,
1252     serv_connect_callback, client_p,
1253     aconf->aftype, CONNECTTIMEOUT);
1254     }
1255     else if (ServerInfo.specific_ipv6_vhost)
1256     {
1257     memcpy(&ipn, &ServerInfo.ip6, sizeof(struct irc_ssaddr));
1258     ipn.ss.ss_family = AF_INET6;
1259     ipn.ss_port = 0;
1260     comm_connect_tcp(&client_p->localClient->fd,
1261     aconf->host, aconf->port,
1262     (struct sockaddr *)&ipn, ipn.ss_len,
1263     serv_connect_callback, client_p,
1264     aconf->aftype, CONNECTTIMEOUT);
1265     }
1266     else
1267     comm_connect_tcp(&client_p->localClient->fd,
1268     aconf->host, aconf->port,
1269     NULL, 0, serv_connect_callback, client_p,
1270     aconf->aftype, CONNECTTIMEOUT);
1271     }
1272     #endif
1273     }
1274     return (1);
1275     }
1276    
1277 michael 1303 #ifdef HAVE_LIBCRYPTO
1278     static void
1279     finish_ssl_server_handshake(struct Client *client_p)
1280     {
1281     struct ConfItem *conf=NULL;
1282     struct AccessItem *aconf=NULL;
1283    
1284     conf = find_conf_name(&client_p->localClient->confs,
1285     client_p->name, SERVER_TYPE);
1286     if (conf == NULL)
1287     {
1288 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1289 michael 1303 "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
1290 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1291 michael 1303 "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
1292    
1293     exit_client(client_p, &me, "Lost connect{} block");
1294     return;
1295     }
1296    
1297     aconf = map_to_conf(conf);
1298    
1299     /* jdc -- Check and send spasswd, not passwd. */
1300     if (!EmptyString(aconf->spasswd))
1301     sendto_one(client_p, "PASS %s TS %d %s",
1302     aconf->spasswd, TS_CURRENT, me.id);
1303    
1304 michael 1519 send_capabilities(client_p, aconf, 0);
1305 michael 1303
1306     sendto_one(client_p, "SERVER %s 1 :%s%s",
1307     me.name, ConfigServerHide.hidden ? "(H) " : "",
1308     me.info);
1309    
1310     /* If we've been marked dead because a send failed, just exit
1311     * here now and save everyone the trouble of us ever existing.
1312     */
1313     if (IsDead(client_p))
1314     {
1315 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1316 michael 1303 "%s[%s] went dead during handshake",
1317     client_p->name,
1318     client_p->host);
1319 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1320 michael 1303 "%s went dead during handshake", client_p->name);
1321     return;
1322     }
1323    
1324     /* don't move to serv_list yet -- we haven't sent a burst! */
1325     /* If we get here, we're ok, so lets start reading some data */
1326     comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ, read_packet, client_p, 0);
1327     }
1328    
1329     static void
1330 michael 1306 ssl_server_handshake(fde_t *fd, struct Client *client_p)
1331 michael 1303 {
1332     int ret;
1333     int err;
1334    
1335     ret = SSL_connect(client_p->localClient->fd.ssl);
1336    
1337     if (ret <= 0)
1338     {
1339     switch ((err = SSL_get_error(client_p->localClient->fd.ssl, ret)))
1340     {
1341     case SSL_ERROR_WANT_WRITE:
1342     comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE,
1343 michael 1308 (PF *)ssl_server_handshake, client_p, 0);
1344 michael 1303 return;
1345     case SSL_ERROR_WANT_READ:
1346     comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ,
1347 michael 1308 (PF *)ssl_server_handshake, client_p, 0);
1348 michael 1303 return;
1349     default:
1350 michael 1308 {
1351     const char *sslerr = ERR_error_string(ERR_get_error(), NULL);
1352 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1353 michael 1308 "Error connecting to %s: %s", client_p->name,
1354     sslerr ? sslerr : "unknown SSL error");
1355 michael 1303 exit_client(client_p, client_p, "Error during SSL handshake");
1356     return;
1357 michael 1308 }
1358 michael 1303 }
1359     }
1360    
1361     finish_ssl_server_handshake(client_p);
1362     }
1363    
1364     static void
1365 michael 1306 ssl_connect_init(struct Client *client_p, struct AccessItem *aconf, fde_t *fd)
1366 michael 1303 {
1367     if ((client_p->localClient->fd.ssl = SSL_new(ServerInfo.client_ctx)) == NULL)
1368     {
1369     ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s",
1370     ERR_error_string(ERR_get_error(), NULL));
1371     SetDead(client_p);
1372     exit_client(client_p, client_p, "SSL_new failed");
1373     return;
1374     }
1375    
1376     SSL_set_fd(fd->ssl, fd->fd);
1377 michael 1306
1378     if (!EmptyString(aconf->cipher_list))
1379     SSL_set_cipher_list(client_p->localClient->fd.ssl, aconf->cipher_list);
1380    
1381     ssl_server_handshake(NULL, client_p);
1382 michael 1303 }
1383     #endif
1384    
1385 adx 30 /* serv_connect_callback() - complete a server connection.
1386     *
1387     * This routine is called after the server connection attempt has
1388     * completed. If unsucessful, an error is sent to ops and the client
1389     * is closed. If sucessful, it goes through the initialisation/check
1390     * procedures, the capabilities are sent, and the socket is then
1391     * marked for reading.
1392     */
1393     static void
1394     serv_connect_callback(fde_t *fd, int status, void *data)
1395     {
1396     struct Client *client_p = data;
1397     struct ConfItem *conf=NULL;
1398     struct AccessItem *aconf=NULL;
1399    
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 adx 30 "Error connecting to %s[%s]: %s", client_p->name,
1420     client_p->host, comm_errstr(status));
1421    
1422 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1423 adx 30 "Error connecting to %s: %s",
1424     client_p->name, comm_errstr(status));
1425    
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     client_p->name, SERVER_TYPE);
1437     if (conf == NULL)
1438     {
1439 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1440 adx 30 "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 adx 30 "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
1443    
1444     exit_client(client_p, &me, "Lost connect{} block");
1445     return;
1446     }
1447    
1448 michael 1303 aconf = map_to_conf(conf);
1449 adx 30 /* Next, send the initial handshake */
1450     SetHandshake(client_p);
1451    
1452     #ifdef HAVE_LIBCRYPTO
1453 michael 1303 if (IsConfSSL(aconf))
1454     {
1455 michael 1306 ssl_connect_init(client_p, aconf, fd);
1456 michael 1303 return;
1457     }
1458 adx 30 #endif
1459    
1460     /* jdc -- Check and send spasswd, not passwd. */
1461 michael 1115 if (!EmptyString(aconf->spasswd))
1462 adx 30 sendto_one(client_p, "PASS %s TS %d %s",
1463     aconf->spasswd, TS_CURRENT, me.id);
1464    
1465 michael 1519 send_capabilities(client_p, aconf, 0);
1466 adx 30
1467     sendto_one(client_p, "SERVER %s 1 :%s%s",
1468 michael 1118 me.name, ConfigServerHide.hidden ? "(H) " : "",
1469 adx 30 me.info);
1470    
1471     /* If we've been marked dead because a send failed, just exit
1472     * here now and save everyone the trouble of us ever existing.
1473     */
1474     if (IsDead(client_p))
1475     {
1476 michael 1618 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
1477 adx 30 "%s[%s] went dead during handshake",
1478     client_p->name,
1479     client_p->host);
1480 michael 1618 sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE,
1481 adx 30 "%s went dead during handshake", client_p->name);
1482     return;
1483     }
1484    
1485     /* don't move to serv_list yet -- we haven't sent a burst! */
1486     /* If we get here, we're ok, so lets start reading some data */
1487     comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0);
1488     }
1489    
1490     struct Client *
1491     find_servconn_in_progress(const char *name)
1492     {
1493     dlink_node *ptr;
1494     struct Client *cptr;
1495    
1496     DLINK_FOREACH(ptr, unknown_list.head)
1497     {
1498     cptr = ptr->data;
1499    
1500     if (cptr && cptr->name[0])
1501 michael 1118 if (match(name, cptr->name))
1502 adx 30 return cptr;
1503     }
1504    
1505     return NULL;
1506     }

Properties

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