ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/s_serv.c
Revision: 121
Committed: Thu Oct 13 10:34:56 2005 UTC (20 years, 10 months ago) by michael
Content type: text/x-csrc
File size: 69735 byte(s)
Log Message:
- Moved ms_tburst() to m_tb.c as suggested by adx
- Fixed problem where we would send both TB and TBURST to the same server

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     #include "channel.h"
31     #include "channel_mode.h"
32     #include "client.h"
33     #include "common.h"
34     #include "hash.h"
35     #include "ircd.h"
36     #include "ircd_defs.h"
37     #include "numeric.h"
38     #include "packet.h"
39     #include "s_conf.h"
40     #include "s_serv.h"
41     #include "s_stats.h"
42     #include "s_user.h"
43     #include "send.h"
44     #include "channel.h" /* chcap_usage_counts stuff...*/
45    
46     #define MIN_CONN_FREQ 300
47    
48     struct Client *uplink = NULL;
49    
50    
51     static dlink_list cap_list = { NULL, NULL, 0 };
52     static unsigned long freeMask;
53     static void server_burst(struct Client *);
54     static int fork_server(struct Client *);
55     static void burst_all(struct Client *);
56     static void cjoin_all(struct Client *);
57 michael 120 static void send_tb(struct Client *, const struct Channel *);
58 adx 30
59     static CNCB serv_connect_callback;
60    
61     static void start_io(struct Client *);
62     static void burst_members(struct Client *, struct Channel *);
63     static void burst_ll_members(struct Client *, struct Channel *);
64     static void add_lazylinkchannel(struct Client *, struct Channel *);
65    
66     static SlinkRplHnd slink_error;
67     static SlinkRplHnd slink_zipstats;
68    
69    
70     #ifdef HAVE_LIBCRYPTO
71     struct EncCapability CipherTable[] =
72     {
73     #ifdef HAVE_EVP_BF_CFB
74     { "BF/168", CAP_ENC_BF_168, 24, CIPHER_BF },
75     { "BF/128", CAP_ENC_BF_128, 16, CIPHER_BF },
76     #endif
77     #ifdef HAVE_EVP_CAST5_CFB
78     { "CAST/128", CAP_ENC_CAST_128, 16, CIPHER_CAST },
79     #endif
80     #ifdef HAVE_EVP_IDEA_CFB
81     { "IDEA/128", CAP_ENC_IDEA_128, 16, CIPHER_IDEA },
82     #endif
83     #ifdef HAVE_EVP_RC5_32_12_16_CFB
84     { "RC5.16/128", CAP_ENC_RC5_16_128, 16, CIPHER_RC5_16 },
85     { "RC5.12/128", CAP_ENC_RC5_12_128, 16, CIPHER_RC5_12 },
86     { "RC5.8/128", CAP_ENC_RC5_8_128, 16, CIPHER_RC5_8 },
87     #endif
88     #ifdef HAVE_EVP_DES_EDE3_CFB
89     { "3DES/168", CAP_ENC_3DES_168, 24, CIPHER_3DES },
90     #endif
91     #ifdef HAVE_EVP_DES_CFB
92     { "DES/56", CAP_ENC_DES_56, 8, CIPHER_DES },
93     #endif
94     { 0, 0, 0, 0 }
95     };
96     #endif
97    
98     struct SlinkRplDef slinkrpltab[] = {
99     { SLINKRPL_ERROR, slink_error, SLINKRPL_FLAG_DATA },
100     { SLINKRPL_ZIPSTATS, slink_zipstats, SLINKRPL_FLAG_DATA },
101     { 0, 0, 0 },
102     };
103    
104    
105     void
106     slink_error(unsigned int rpl, unsigned int len, unsigned char *data,
107     struct Client *server_p)
108     {
109     assert(rpl == SLINKRPL_ERROR);
110     assert(len < 256);
111    
112     data[len-1] = '\0';
113    
114     sendto_realops_flags(UMODE_ALL, L_ALL, "SlinkError for %s: %s",
115     server_p->name, data);
116     /* XXX should this be exit_client? */
117     exit_client(server_p, &me, "servlink error -- terminating link");
118     }
119    
120     void
121     slink_zipstats(unsigned int rpl, unsigned int len, unsigned char *data,
122     struct Client *server_p)
123     {
124     struct ZipStats zipstats;
125     unsigned long in = 0, in_wire = 0, out = 0, out_wire = 0;
126     int i = 0;
127    
128     assert(rpl == SLINKRPL_ZIPSTATS);
129     assert(len == 16);
130     assert(IsCapable(server_p, CAP_ZIP));
131    
132     /* Yes, it needs to be done this way, no we cannot let the compiler
133     * work with the pointer to the structure. This works around a GCC
134     * bug on SPARC that affects all versions at the time of this writing.
135     * I will feed you to the creatures living in RMS's beard if you do
136     * not leave this as is, without being sure that you are not causing
137     * regression for most of our installed SPARC base.
138     * -jmallett, 04/27/2002
139     */
140     memcpy(&zipstats, &server_p->localClient->zipstats, sizeof(struct ZipStats));
141    
142     in |= (data[i++] << 24);
143     in |= (data[i++] << 16);
144     in |= (data[i++] << 8);
145     in |= (data[i++] );
146    
147     in_wire |= (data[i++] << 24);
148     in_wire |= (data[i++] << 16);
149     in_wire |= (data[i++] << 8);
150     in_wire |= (data[i++] );
151    
152     out |= (data[i++] << 24);
153     out |= (data[i++] << 16);
154     out |= (data[i++] << 8);
155     out |= (data[i++] );
156    
157     out_wire |= (data[i++] << 24);
158     out_wire |= (data[i++] << 16);
159     out_wire |= (data[i++] << 8);
160     out_wire |= (data[i++] );
161    
162     /* This macro adds b to a if a plus b is not an overflow, and sets the
163     * value of a to b if it is.
164     * Add and Set if No Overflow.
165     */
166     #define ASNO(a, b) a = (a + b >= a ? a + b : b)
167    
168     ASNO(zipstats.in, in);
169     ASNO(zipstats.out, out);
170     ASNO(zipstats.in_wire, in_wire);
171     ASNO(zipstats.out_wire, out_wire);
172    
173     if (zipstats.in > 0)
174     zipstats.in_ratio = (((double)(zipstats.in - zipstats.in_wire) /
175     (double)zipstats.in) * 100.00);
176     else
177     zipstats.in_ratio = 0;
178    
179     if (zipstats.out > 0)
180     zipstats.out_ratio = (((double)(zipstats.out - zipstats.out_wire) /
181     (double)zipstats.out) * 100.00);
182     else
183     zipstats.out_ratio = 0;
184    
185     memcpy(&server_p->localClient->zipstats, &zipstats, sizeof (struct ZipStats));
186     }
187    
188     void
189     collect_zipstats(void *unused)
190     {
191     dlink_node *ptr;
192     struct Client *target_p;
193    
194     DLINK_FOREACH(ptr, serv_list.head)
195     {
196     target_p = ptr->data;
197    
198     if (IsCapable(target_p, CAP_ZIP))
199     {
200     /* only bother if we haven't already got something queued... */
201     if (!target_p->localClient->slinkq)
202     {
203     target_p->localClient->slinkq = MyMalloc(1); /* sigh.. */
204     target_p->localClient->slinkq[0] = SLINKCMD_ZIPSTATS;
205     target_p->localClient->slinkq_ofs = 0;
206     target_p->localClient->slinkq_len = 1;
207     send_queued_slink_write(target_p);
208     }
209     }
210     }
211     }
212    
213     #ifdef HAVE_LIBCRYPTO
214     struct EncCapability *
215     check_cipher(struct Client *client_p, struct AccessItem *aconf)
216     {
217     struct EncCapability *epref;
218    
219     /* Use connect{} specific info if available */
220     if (aconf->cipher_preference)
221     epref = aconf->cipher_preference;
222     else
223     epref = ConfigFileEntry.default_cipher_preference;
224    
225     /* If the server supports the capability in hand, return the matching
226     * conf struct. Otherwise, return NULL (an error).
227     */
228     if (IsCapableEnc(client_p, epref->cap))
229     return(epref);
230    
231     return(NULL);
232     }
233     #endif /* HAVE_LIBCRYPTO */
234    
235     /* my_name_for_link()
236     * return wildcard name of my server name
237     * according to given config entry --Jto
238     */
239     const char *
240     my_name_for_link(struct ConfItem *conf)
241     {
242     struct AccessItem *aconf;
243    
244     aconf = (struct AccessItem *)map_to_conf(conf);
245     if (aconf->fakename != NULL)
246     return(aconf->fakename);
247     else
248     return(me.name);
249     }
250    
251     /*
252     * write_links_file
253     *
254     * inputs - void pointer which is not used
255     * output - NONE
256     * side effects - called from an event, write out list of linked servers
257     * but in no particular order.
258     */
259     void
260     write_links_file(void* notused)
261     {
262     MessageFileLine *next_mptr = 0;
263     MessageFileLine *mptr = 0;
264     MessageFileLine *currentMessageLine = 0;
265     MessageFileLine *newMessageLine = 0;
266     MessageFile *MessageFileptr;
267     const char *p;
268     FBFILE *file;
269     char buff[512];
270     dlink_node *ptr;
271    
272     MessageFileptr = &ConfigFileEntry.linksfile;
273    
274     if ((file = fbopen(MessageFileptr->fileName, "w")) == NULL)
275     return;
276    
277     for (mptr = MessageFileptr->contentsOfFile; mptr; mptr = next_mptr)
278     {
279     next_mptr = mptr->next;
280     MyFree(mptr);
281     }
282    
283     MessageFileptr->contentsOfFile = NULL;
284     currentMessageLine = NULL;
285    
286     DLINK_FOREACH(ptr, global_serv_list.head)
287     {
288     size_t nbytes = 0;
289     struct Client *target_p = ptr->data;
290    
291     /* skip ourselves, we send ourselves in /links */
292     if (IsMe(target_p))
293     continue;
294    
295     /* skip hidden servers */
296     if (IsHidden(target_p) && !ConfigServerHide.disable_hidden)
297     continue;
298    
299     if (target_p->info[0])
300     p = target_p->info;
301     else
302     p = "(Unknown Location)";
303    
304     newMessageLine = MyMalloc(sizeof(MessageFileLine));
305    
306     /* Attempt to format the file in such a way it follows the usual links output
307     * ie "servername uplink :hops info"
308     * Mostly for aesthetic reasons - makes it look pretty in mIRC ;)
309     * - madmax
310     */
311    
312     /*
313     * For now, check this ircsprintf wont overflow - it shouldnt on a
314     * default config but it is configurable..
315     * This should be changed to an snprintf at some point, but I'm wanting to
316     * know if this is a cause of a bug - cryogen
317     */
318     assert(strlen(target_p->name) + strlen(me.name) + 6 + strlen(p) <=
319     MESSAGELINELEN);
320     ircsprintf(newMessageLine->line, "%s %s :1 %s",
321     target_p->name, me.name, p);
322     newMessageLine->next = NULL;
323    
324     if (MessageFileptr->contentsOfFile)
325     {
326     if (currentMessageLine)
327     currentMessageLine->next = newMessageLine;
328     currentMessageLine = newMessageLine;
329     }
330     else
331     {
332     MessageFileptr->contentsOfFile = newMessageLine;
333     currentMessageLine = newMessageLine;
334     }
335    
336     nbytes = ircsprintf(buff, "%s %s :1 %s\n", target_p->name, me.name, p);
337     fbputs(buff, file, nbytes);
338     }
339    
340     fbclose(file);
341     }
342    
343     /* hunt_server()
344     * Do the basic thing in delivering the message (command)
345     * across the relays to the specific server (server) for
346     * actions.
347     *
348     * Note: The command is a format string and *MUST* be
349     * of prefixed style (e.g. ":%s COMMAND %s ...").
350     * Command can have only max 8 parameters.
351     *
352     * server parv[server] is the parameter identifying the
353     * target server.
354     *
355     * *WARNING*
356     * parv[server] is replaced with the pointer to the
357     * real servername from the matched client (I'm lazy
358     * now --msa).
359     *
360     * returns: (see #defines)
361     */
362     int
363     hunt_server(struct Client *client_p, struct Client *source_p, const char *command,
364     int server, int parc, char *parv[])
365     {
366     struct Client *target_p = NULL;
367     struct Client *target_tmp = NULL;
368     dlink_node *ptr;
369     int wilds;
370    
371     /* Assume it's me, if no server
372     */
373     if (parc <= server || EmptyString(parv[server]) ||
374     match(me.name, parv[server]) ||
375     match(parv[server], me.name) ||
376     !strcmp(parv[server], me.id))
377     return(HUNTED_ISME);
378    
379     /* These are to pickup matches that would cause the following
380     * message to go in the wrong direction while doing quick fast
381     * non-matching lookups.
382     */
383     if (MyClient(source_p))
384     target_p = find_client(parv[server]);
385     else
386     target_p = find_person(client_p, parv[server]);
387    
388     if (target_p)
389     if (target_p->from == source_p->from && !MyConnect(target_p))
390     target_p = NULL;
391    
392     if (target_p == NULL && (target_p = find_server(parv[server])))
393     if (target_p->from == source_p->from && !MyConnect(target_p))
394     target_p = NULL;
395    
396     collapse(parv[server]);
397     wilds = (strchr(parv[server], '?') || strchr(parv[server], '*'));
398    
399     /* Again, if there are no wild cards involved in the server
400     * name, use the hash lookup
401     */
402     if (target_p == NULL)
403     {
404     if (!wilds)
405     {
406     if (!(target_p = find_server(parv[server])))
407     {
408     sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
409     me.name, parv[0], parv[server]);
410     return(HUNTED_NOSUCH);
411     }
412     }
413     else
414     {
415     DLINK_FOREACH(ptr, global_client_list.head)
416     {
417     target_tmp = ptr->data;
418    
419     if (match(parv[server], target_tmp->name))
420     {
421     if (target_tmp->from == source_p->from && !MyConnect(target_tmp))
422     continue;
423     target_p = ptr->data;
424    
425     if (IsRegistered(target_p) && (target_p != client_p))
426     break;
427     }
428     }
429     }
430     }
431    
432     if (target_p != NULL)
433     {
434     if(!IsRegistered(target_p))
435     {
436     sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
437     me.name, parv[0], parv[server]);
438     return HUNTED_NOSUCH;
439     }
440    
441     if (IsMe(target_p) || MyClient(target_p))
442     return HUNTED_ISME;
443    
444     if (!match(target_p->name, parv[server]))
445     parv[server] = target_p->name;
446    
447     /* Deal with lazylinks */
448     client_burst_if_needed(target_p, source_p);
449    
450     /* This is a little kludgy but should work... */
451     if (IsClient(source_p) &&
452     ((MyConnect(target_p) && IsCapable(target_p, CAP_TS6)) ||
453     (!MyConnect(target_p) && IsCapable(target_p->from, CAP_TS6))))
454     parv[0] = ID(source_p);
455    
456     sendto_one(target_p, command, parv[0],
457     parv[1], parv[2], parv[3], parv[4],
458     parv[5], parv[6], parv[7], parv[8]);
459     return(HUNTED_PASS);
460     }
461    
462     sendto_one(source_p, form_str(ERR_NOSUCHSERVER),
463     me.name, parv[0], parv[server]);
464     return(HUNTED_NOSUCH);
465     }
466    
467     /* try_connections()
468     *
469     * inputs - void pointer which is not used
470     * output - NONE
471     * side effects -
472     * scan through configuration and try new connections.
473     * Returns the calendar time when the next call to this
474     * function should be made latest. (No harm done if this
475     * is called earlier or later...)
476     */
477     void
478     try_connections(void *unused)
479     {
480     dlink_node *ptr;
481     struct ConfItem *conf;
482     struct AccessItem *aconf;
483     struct ClassItem *cltmp;
484     int confrq;
485    
486     /* TODO: change this to set active flag to 0 when added to event! --Habeeb */
487     if (GlobalSetOptions.autoconn == 0)
488     return;
489    
490     DLINK_FOREACH(ptr, server_items.head)
491     {
492     conf = ptr->data;
493     aconf = (struct AccessItem *)map_to_conf(conf);
494    
495     /* Also when already connecting! (update holdtimes) --SRB
496     */
497     if (!(aconf->status & CONF_SERVER) || aconf->port <= 0 ||
498     !(IsConfAllowAutoConn(aconf)))
499     continue;
500    
501     cltmp = (struct ClassItem *)map_to_conf(aconf->class_ptr);
502    
503     /* Skip this entry if the use of it is still on hold until
504     * future. Otherwise handle this entry (and set it on hold
505     * until next time). Will reset only hold times, if already
506     * made one successfull connection... [this algorithm is
507     * a bit fuzzy... -- msa >;) ]
508     */
509     if (aconf->hold > CurrentTime)
510     continue;
511    
512     if (cltmp == NULL)
513     confrq = DEFAULT_CONNECTFREQUENCY;
514     else
515     {
516     confrq = ConFreq(cltmp);
517     if (confrq < MIN_CONN_FREQ )
518     confrq = MIN_CONN_FREQ;
519     }
520    
521     aconf->hold = CurrentTime + confrq;
522    
523     /* Found a CONNECT config with port specified, scan clients
524     * and see if this server is already connected?
525     */
526     if (find_server(conf->name) != NULL)
527     continue;
528    
529     if (CurrUserCount(cltmp) < MaxTotal(cltmp))
530     {
531     /* Go to the end of the list, if not already last */
532     if (ptr->next != NULL)
533     {
534     dlinkDelete(ptr, &server_items);
535     dlinkAddTail(conf, &conf->node, &server_items);
536     }
537    
538     if (find_servconn_in_progress(conf->name))
539     return;
540    
541     /* We used to only print this if serv_connect() actually
542     * succeeded, but since comm_tcp_connect() can call the callback
543     * immediately if there is an error, we were getting error messages
544     * in the wrong order. SO, we just print out the activated line,
545     * and let serv_connect() / serv_connect_callback() print an
546     * error afterwards if it fails.
547     * -- adrian
548     */
549     if (ConfigServerHide.hide_server_ips)
550     sendto_realops_flags(UMODE_ALL, L_ALL, "Connection to %s activated.",
551     conf->name);
552     else
553     sendto_realops_flags(UMODE_ALL, L_ALL, "Connection to %s[%s] activated.",
554     conf->name, aconf->host);
555    
556     serv_connect(aconf, NULL);
557     /* We connect only one at time... */
558     return;
559     }
560     }
561     }
562    
563     int
564     check_server(const char *name, struct Client *client_p, int cryptlink)
565     {
566     dlink_node *ptr;
567     struct ConfItem *conf = NULL;
568     struct ConfItem *server_conf = NULL;
569     struct AccessItem *server_aconf = NULL;
570     struct AccessItem *aconf = NULL;
571     int error = -1;
572    
573     assert(client_p != NULL);
574    
575     if (client_p == NULL)
576     return(error);
577    
578     if (strlen(name) > HOSTLEN)
579     return(-4);
580    
581     /* loop through looking for all possible connect items that might work */
582     DLINK_FOREACH(ptr, server_items.head)
583     {
584     conf = ptr->data;
585     aconf = (struct AccessItem *)map_to_conf(conf);
586    
587     if (!match(name, conf->name))
588     continue;
589    
590     error = -3;
591    
592     /* XXX: Fix me for IPv6 */
593     /* XXX sockhost is the IPv4 ip as a string */
594     if (match(aconf->host, client_p->host) ||
595     match(aconf->host, client_p->sockhost))
596     {
597     error = -2;
598     #ifdef HAVE_LIBCRYPTO
599     if (cryptlink && IsConfCryptLink(aconf))
600     {
601     if (aconf->rsa_public_key)
602     server_conf = conf;
603     }
604     else if (!(cryptlink || IsConfCryptLink(aconf)))
605     #endif /* HAVE_LIBCRYPTO */
606     {
607     /* A NULL password is as good as a bad one */
608     if (EmptyString(client_p->localClient->passwd))
609     return(-2);
610    
611     /* code in s_conf.c should not have allowed this to be NULL */
612     if (aconf->passwd == NULL)
613     return(-2);
614    
615     if (IsConfEncrypted(aconf))
616     {
617     if (strcmp(aconf->passwd,
618     (const char *)crypt(client_p->localClient->passwd,
619     aconf->passwd)) == 0)
620     server_conf = conf;
621     }
622     else
623     {
624     if (strcmp(aconf->passwd, client_p->localClient->passwd) == 0)
625     server_conf = conf;
626     }
627     }
628     }
629     }
630    
631     if (server_conf == NULL)
632     return(error);
633    
634 db 101 attach_iline(client_p, server_conf);
635 adx 30
636     /* Now find all leaf or hub config items for this server */
637     DLINK_FOREACH(ptr, hub_items.head)
638     {
639     conf = ptr->data;
640    
641     if (!match(name, conf->name))
642     continue;
643 db 101 attach_leaf_hub(client_p, conf);
644 adx 30 }
645    
646     DLINK_FOREACH(ptr, leaf_items.head)
647     {
648     conf = ptr->data;
649    
650     if (!match(name, conf->name))
651     continue;
652 db 101 attach_leaf_hub(client_p, conf);
653 adx 30 }
654    
655     server_aconf = (struct AccessItem *)map_to_conf(server_conf);
656    
657     if (!IsConfLazyLink(server_aconf))
658     ClearCap(client_p, CAP_LL);
659     #ifdef HAVE_LIBZ /* otherwise, clear it unconditionally */
660     if (!IsConfCompressed(server_aconf))
661     #endif
662     ClearCap(client_p, CAP_ZIP);
663     if (!IsConfCryptLink(server_aconf))
664     ClearCap(client_p, CAP_ENC);
665     if (!IsConfTopicBurst(server_aconf))
666 michael 120 {
667 adx 30 ClearCap(client_p, CAP_TB);
668 michael 120 ClearCap(client_p, CAP_TBURST);
669     }
670 adx 30
671 michael 121 /*
672     * Make sure to unset CAP_TB if server is capable of TBURST.
673     * This is to prevent sending both TB and TBURST messages to
674     * the same server in m_tb:set_topic
675     */
676     if (IsCapable(client_p, CAP_TBURST))
677     ClearCap(client_p, CAP_TB);
678    
679 adx 30 /* Don't unset CAP_HUB here even if the server isn't a hub,
680     * it only indicates if the server thinks it's lazylinks are
681     * leafs or not.. if you unset it, bad things will happen
682     */
683     if (aconf != NULL)
684     {
685     struct sockaddr_in *v4;
686     #ifdef IPV6
687     struct sockaddr_in6 *v6;
688     #endif
689     switch (aconf->aftype)
690     {
691     #ifdef IPV6
692     case AF_INET6:
693     v6 = (struct sockaddr_in6 *)&aconf->ipnum;
694    
695     if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr))
696     memcpy(&aconf->ipnum, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
697     break;
698     #endif
699     case AF_INET:
700     v4 = (struct sockaddr_in *)&aconf->ipnum;
701    
702     if (v4->sin_addr.s_addr == INADDR_NONE)
703     memcpy(&aconf->ipnum, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
704     break;
705     }
706     }
707    
708     return(0);
709     }
710    
711     /* add_capability()
712     *
713     * inputs - string name of CAPAB
714     * - int flag of capability
715     * output - NONE
716     * side effects - Adds given capability name and bit mask to
717     * current supported capabilities. This allows
718     * modules to dynamically add or subtract their capability.
719     */
720     void
721     add_capability(const char *capab_name, int cap_flag, int add_to_default)
722     {
723     struct Capability *cap;
724    
725     cap = (struct Capability *)MyMalloc(sizeof(*cap));
726     DupString(cap->name, capab_name);
727     cap->cap = cap_flag;
728     dlinkAdd(cap, &cap->node, &cap_list);
729     if (add_to_default)
730     default_server_capabs |= cap_flag;
731     }
732    
733     /* delete_capability()
734     *
735     * inputs - string name of CAPAB
736     * output - NONE
737     * side effects - delete given capability from ones known.
738     */
739     int
740     delete_capability(const char *capab_name)
741     {
742     dlink_node *ptr;
743     dlink_node *next_ptr;
744     struct Capability *cap;
745    
746     DLINK_FOREACH_SAFE(ptr, next_ptr, cap_list.head)
747     {
748     cap = ptr->data;
749    
750     if (cap->cap != 0)
751     {
752     if (irccmp(cap->name, capab_name) == 0)
753     {
754     default_server_capabs &= ~(cap->cap);
755     dlinkDelete(ptr, &cap_list);
756     MyFree(cap->name);
757     cap->name = NULL;
758     MyFree(cap);
759     }
760     }
761     }
762    
763     return(0);
764     }
765    
766     /*
767     * find_capability()
768     *
769     * inputs - string name of capab to find
770     * output - 0 if not found CAPAB otherwise
771     * side effects - none
772     */
773     int
774     find_capability(const char *capab)
775     {
776     dlink_node *ptr;
777     struct Capability *cap;
778    
779     DLINK_FOREACH(ptr, cap_list.head)
780     {
781     cap = ptr->data;
782    
783     if (cap->cap != 0)
784     {
785     if (irccmp(cap->name, capab) == 0)
786     return(cap->cap);
787     }
788     }
789     return(0);
790     }
791    
792     /* send_capabilities()
793     *
794     * inputs - Client pointer to send to
795     * - Pointer to AccessItem (for crypt)
796     * - int flag of capabilities that this server can send
797     * - int flag of encryption capabilities
798     * output - NONE
799     * side effects - send the CAPAB line to a server -orabidoo
800     *
801     */
802     void
803     send_capabilities(struct Client *client_p, struct AccessItem *aconf,
804     int cap_can_send, int enc_can_send)
805     {
806     struct Capability *cap=NULL;
807     char msgbuf[IRCD_BUFSIZE];
808     char *t;
809     int tl;
810     dlink_node *ptr;
811     #ifdef HAVE_LIBCRYPTO
812     struct EncCapability *epref;
813     char *capend;
814     int sent_cipher = 0;
815     #endif
816    
817     t = msgbuf;
818    
819     DLINK_FOREACH(ptr, cap_list.head)
820     {
821     cap = ptr->data;
822    
823     if (cap->cap & (cap_can_send|default_server_capabs))
824     {
825     tl = ircsprintf(t, "%s ", cap->name);
826     t += tl;
827     }
828     }
829     #ifdef HAVE_LIBCRYPTO
830     if (enc_can_send)
831     {
832     capend = t;
833     strcpy(t, "ENC:");
834     t += 4;
835    
836     /* use connect{} specific info if available */
837     if (aconf->cipher_preference)
838     epref = aconf->cipher_preference;
839     else
840     epref = ConfigFileEntry.default_cipher_preference;
841    
842 michael 120 if (epref->cap & enc_can_send)
843 adx 30 {
844     /* Leave the space -- it is removed later. */
845     tl = ircsprintf(t, "%s ", epref->name);
846     t += tl;
847     sent_cipher = 1;
848     }
849    
850     if (!sent_cipher)
851     t = capend; /* truncate string before ENC:, below */
852     }
853     #endif
854     *(t-1) = '\0';
855     sendto_one(client_p, "CAPAB :%s", msgbuf);
856     }
857    
858     /* sendnick_TS()
859     *
860     * inputs - client (server) to send nick towards
861     * - client to send nick for
862     * output - NONE
863     * side effects - NICK message is sent towards given client_p
864     */
865     void
866     sendnick_TS(struct Client *client_p, struct Client *target_p)
867     {
868     static char ubuf[12];
869    
870     if (!IsClient(target_p))
871     return;
872    
873     send_umode(NULL, target_p, 0, IsOperHiddenAdmin(target_p) ?
874     SEND_UMODES & ~UMODE_ADMIN : SEND_UMODES, ubuf);
875    
876     if (ubuf[0] == '\0')
877     {
878     ubuf[0] = '+';
879     ubuf[1] = '\0';
880     }
881    
882     /* XXX Both of these need to have a :me.name or :mySID!?!?! */
883     if (HasID(target_p) && IsCapable(client_p, CAP_TS6))
884     sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s :%s",
885     target_p->servptr->id,
886     target_p->name, target_p->hopcount + 1,
887     (unsigned long) target_p->tsinfo,
888     ubuf, target_p->username, target_p->host,
889     ((MyClient(target_p)&&!IsIPSpoof(target_p))?target_p->sockhost:"0"),
890     target_p->id, target_p->info);
891     else
892     sendto_one(client_p, "NICK %s %d %lu %s %s %s %s :%s",
893     target_p->name, target_p->hopcount + 1,
894     (unsigned long) target_p->tsinfo,
895     ubuf, target_p->username, target_p->host,
896     target_p->servptr->name, target_p->info);
897     if (IsConfAwayBurst((struct AccessItem *)map_to_conf(client_p->serv->sconf)))
898     if (!EmptyString(target_p->away))
899     sendto_one(client_p, ":%s AWAY :%s", target_p->name,
900     target_p->away);
901    
902     }
903    
904     /* client_burst_if_needed()
905     *
906     * inputs - pointer to server
907     * - pointer to client to add
908     * output - NONE
909     * side effects - If this client is not known by this lazyleaf, send it
910     */
911     void
912     client_burst_if_needed(struct Client *client_p, struct Client *target_p)
913     {
914     if (!ServerInfo.hub)
915     return;
916     if (!MyConnect(client_p))
917     return;
918     if (!IsCapable(client_p,CAP_LL))
919     return;
920    
921     if ((target_p->lazyLinkClientExists & client_p->localClient->serverMask) == 0)
922     {
923     sendnick_TS(client_p, target_p);
924     add_lazylinkclient(client_p,target_p);
925     }
926     }
927    
928     /*
929     * show_capabilities - show current server capabilities
930     *
931     * inputs - pointer to a struct Client
932     * output - pointer to static string
933     * side effects - build up string representing capabilities of server listed
934     */
935     const char *
936     show_capabilities(struct Client *target_p)
937     {
938     static char msgbuf[IRCD_BUFSIZE];
939     char *t = msgbuf;
940     dlink_node *ptr;
941    
942     t += ircsprintf(msgbuf, "TS ");
943    
944     DLINK_FOREACH(ptr, cap_list.head)
945     {
946     const struct Capability *cap = ptr->data;
947    
948     if (IsCapable(target_p, cap->cap))
949     t += ircsprintf(t, "%s ", cap->name);
950     }
951     #ifdef HAVE_LIBCRYPTO
952     if (IsCapable(target_p, CAP_ENC) &&
953     target_p->localClient->in_cipher &&
954     target_p->localClient->out_cipher)
955     t += ircsprintf(t, "ENC:%s ",
956     target_p->localClient->in_cipher->name);
957     #endif
958     *(t - 1) = '\0';
959    
960     return(msgbuf);
961     }
962    
963     /* make_server()
964     *
965     * inputs - pointer to client struct
966     * output - pointer to struct Server
967     * side effects - add's an Server information block to a client
968     * if it was not previously allocated.
969     */
970     struct Server *
971     make_server(struct Client *client_p)
972     {
973     if (client_p->serv == NULL)
974     {
975     client_p->serv = MyMalloc(sizeof(struct Server));
976     client_p->serv->dep_servers = 1;
977     }
978    
979     return client_p->serv;
980     }
981    
982     /* server_estab()
983     *
984     * inputs - pointer to a struct Client
985     * output -
986     * side effects -
987     */
988     void
989     server_estab(struct Client *client_p)
990     {
991     struct Client *target_p;
992     struct ConfItem *conf;
993     struct AccessItem *aconf=NULL;
994     char *host;
995     const char *inpath;
996     static char inpath_ip[HOSTLEN * 2 + USERLEN + 6];
997     dlink_node *m;
998     dlink_node *ptr;
999    
1000     assert(client_p != NULL);
1001    
1002     strlcpy(inpath_ip, get_client_name(client_p, SHOW_IP), sizeof(inpath_ip));
1003    
1004     inpath = get_client_name(client_p, MASK_IP); /* "refresh" inpath with host */
1005     host = client_p->name;
1006    
1007 db 101 if ((conf = client_p->localClient->iline) == NULL)
1008 adx 30 {
1009     /* This shouldn't happen, better tell the ops... -A1kmm */
1010     sendto_realops_flags(UMODE_ALL, L_ALL, "Warning: Lost connect{} block "
1011     "for server %s(this shouldn't happen)!", host);
1012     exit_client(client_p, &me, "Lost connect{} block!");
1013     return;
1014     }
1015    
1016     MyFree(client_p->localClient->passwd);
1017     client_p->localClient->passwd = NULL;
1018    
1019     /* Its got identd, since its a server */
1020     SetGotId(client_p);
1021    
1022     /* If there is something in the serv_list, it might be this
1023     * connecting server..
1024     */
1025     if (!ServerInfo.hub && serv_list.head)
1026     {
1027     if (client_p != serv_list.head->data || serv_list.head->next)
1028     {
1029     ServerStats->is_ref++;
1030     sendto_one(client_p, "ERROR :I'm a leaf not a hub");
1031     exit_client(client_p, &me, "I'm a leaf");
1032     return;
1033     }
1034     }
1035    
1036     aconf = (struct AccessItem *)map_to_conf(conf);
1037    
1038     if (IsUnknown(client_p) && !IsConfCryptLink(aconf))
1039     {
1040     /* jdc -- 1. Use EmptyString(), not [0] index reference.
1041     * 2. Check aconf->spasswd, not aconf->passwd.
1042     */
1043     if (!EmptyString(aconf->spasswd))
1044     {
1045     /* only send ts6 format PASS if we have ts6 enabled */
1046     if (me.id[0] != '\0') /* Send TS 6 form only if id */
1047     sendto_one(client_p, "PASS %s TS %d %s",
1048     aconf->spasswd, TS_CURRENT, me.id);
1049     else
1050     sendto_one(client_p, "PASS %s TS 5",
1051     aconf->spasswd);
1052     }
1053    
1054     /* Pass my info to the new server
1055     *
1056     * If trying to negotiate LazyLinks, pass on CAP_LL
1057     * If this is a HUB, pass on CAP_HUB
1058     * Pass on ZIP if supported
1059     * Pass on TB if supported.
1060     * - Dianora
1061     */
1062    
1063     send_capabilities(client_p, aconf,
1064     (IsConfLazyLink(aconf) ? find_capability("LL") : 0)
1065     | (IsConfCompressed(aconf) ? find_capability("ZIP") : 0)
1066 michael 120 | (IsConfTopicBurst(aconf) ? find_capability("TBURST")|find_capability("TB") : 0)
1067 adx 30 , 0);
1068    
1069     /* SERVER is the last command sent before switching to ziplinks.
1070     * We set TCPNODELAY on the socket to make sure it gets sent out
1071     * on the wire immediately. Otherwise, it could be sitting in
1072     * a kernel buffer when we start sending zipped data, and the
1073     * parser on the receiving side can't hand both unzipped and zipped
1074     * data in one packet. --Rodder
1075     *
1076     * currently we only need to call send_queued_write,
1077     * Nagle is already disabled at this point --adx
1078     */
1079     sendto_one(client_p, "SERVER %s 1 :%s%s",
1080     my_name_for_link(conf),
1081     ConfigServerHide.hidden ? "(H) " : "",
1082     (me.info[0]) ? (me.info) : "IRCers United");
1083     send_queued_write(client_p);
1084     }
1085    
1086     /* Hand the server off to servlink now */
1087     if (IsCapable(client_p, CAP_ENC) || IsCapable(client_p, CAP_ZIP))
1088     {
1089     if (fork_server(client_p) < 0)
1090     {
1091     sendto_realops_flags(UMODE_ALL, L_ADMIN,
1092     "Warning: fork failed for server %s -- check servlink_path (%s)",
1093     get_client_name(client_p, HIDE_IP), ConfigFileEntry.servlink_path);
1094     sendto_realops_flags(UMODE_ALL, L_OPER, "Warning: fork failed for server "
1095     "%s -- check servlink_path (%s)",
1096     get_client_name(client_p, MASK_IP),
1097     ConfigFileEntry.servlink_path);
1098     exit_client(client_p, &me, "fork failed");
1099     return;
1100     }
1101    
1102     start_io(client_p);
1103     SetServlink(client_p);
1104     }
1105    
1106     /* only send ts6 format SVINFO if we have ts6 enabled */
1107     sendto_one(client_p, "SVINFO %d %d 0 :%lu",
1108     (me.id[0] ? TS_CURRENT : 5), TS_MIN,
1109     (unsigned long)CurrentTime);
1110    
1111     /* assumption here is if they passed the correct TS version, they also passed an SID */
1112     if (IsCapable(client_p, CAP_TS6))
1113     hash_add_id(client_p);
1114    
1115     /* XXX Does this ever happen? I don't think so -db */
1116     detach_conf(client_p, OPER_TYPE);
1117    
1118     /* *WARNING*
1119     ** In the following code in place of plain server's
1120     ** name we send what is returned by get_client_name
1121     ** which may add the "sockhost" after the name. It's
1122     ** *very* *important* that there is a SPACE between
1123     ** the name and sockhost (if present). The receiving
1124     ** server will start the information field from this
1125     ** first blank and thus puts the sockhost into info.
1126     ** ...a bit tricky, but you have been warned, besides
1127     ** code is more neat this way... --msa
1128     */
1129     client_p->servptr = &me;
1130    
1131     if (IsClosing(client_p))
1132     return;
1133    
1134     SetServer(client_p);
1135    
1136     /* Update the capability combination usage counts. -A1kmm */
1137     set_chcap_usage_counts(client_p);
1138    
1139     /* Some day, all these lists will be consolidated *sigh* */
1140     dlinkAdd(client_p, &client_p->lnode, &me.serv->servers);
1141    
1142     m = dlinkFind(&unknown_list, client_p);
1143     assert(NULL != m);
1144    
1145     dlinkDelete(m, &unknown_list);
1146     dlinkAdd(client_p, m, &serv_list);
1147    
1148     Count.myserver++;
1149    
1150     dlinkAdd(client_p, make_dlink_node(), &global_serv_list);
1151     hash_add_client(client_p);
1152    
1153     /* doesnt duplicate client_p->serv if allocated this struct already */
1154     make_server(client_p);
1155    
1156     /* fixing eob timings.. -gnp */
1157     client_p->firsttime = CurrentTime;
1158    
1159     /* Show the real host/IP to admins */
1160     sendto_realops_flags(UMODE_ALL, L_ADMIN,
1161     "Link with %s established: (%s) link",
1162     inpath_ip,show_capabilities(client_p));
1163     /* Now show the masked hostname/IP to opers */
1164     sendto_realops_flags(UMODE_ALL, L_OPER,
1165     "Link with %s established: (%s) link",
1166     inpath,show_capabilities(client_p));
1167     ilog(L_NOTICE, "Link with %s established: (%s) link",
1168     inpath_ip, show_capabilities(client_p));
1169    
1170     client_p->serv->sconf = conf;
1171    
1172     if (HasServlink(client_p))
1173     {
1174     /* we won't overflow FD_DESC_SZ here, as it can hold
1175     * client_p->name + 64
1176     */
1177     fd_note(&client_p->localClient->fd, "slink data: %s", client_p->name);
1178     fd_note(&client_p->localClient->ctrlfd, "slink ctrl: %s", client_p->name);
1179     }
1180     else
1181     fd_note(&client_p->localClient->fd, "Server: %s", client_p->name);
1182    
1183     /* Old sendto_serv_but_one() call removed because we now
1184     ** need to send different names to different servers
1185     ** (domain name matching) Send new server to other servers.
1186     */
1187     DLINK_FOREACH(ptr, serv_list.head)
1188     {
1189     target_p = ptr->data;
1190    
1191     if (target_p == client_p)
1192     continue;
1193    
1194     if ((conf = target_p->serv->sconf) &&
1195     match(my_name_for_link(conf), client_p->name))
1196     continue;
1197    
1198     if (IsCapable(target_p, CAP_TS6) && HasID(client_p))
1199     sendto_one(target_p, ":%s SID %s 2 %s :%s%s",
1200     me.id, client_p->name, client_p->id,
1201     IsHidden(client_p) ? "(H) " : "",
1202     client_p->info);
1203     else
1204     sendto_one(target_p,":%s SERVER %s 2 :%s%s",
1205     me.name, client_p->name,
1206     IsHidden(client_p) ? "(H) " : "",
1207     client_p->info);
1208     }
1209    
1210     /* Pass on my client information to the new server
1211     **
1212     ** First, pass only servers (idea is that if the link gets
1213     ** cancelled beacause the server was already there,
1214     ** there are no NICK's to be cancelled...). Of course,
1215     ** if cancellation occurs, all this info is sent anyway,
1216     ** and I guess the link dies when a read is attempted...? --msa
1217     **
1218     ** Note: Link cancellation to occur at this point means
1219     ** that at least two servers from my fragment are building
1220     ** up connection this other fragment at the same time, it's
1221     ** a race condition, not the normal way of operation...
1222     **
1223     ** ALSO NOTE: using the get_client_name for server names--
1224     ** see previous *WARNING*!!! (Also, original inpath
1225     ** is destroyed...)
1226     */
1227    
1228     conf = client_p->serv->sconf;
1229    
1230     DLINK_FOREACH_PREV(ptr, global_serv_list.tail)
1231     {
1232     target_p = ptr->data;
1233    
1234     /* target_p->from == target_p for target_p == client_p */
1235     if (target_p->from == client_p)
1236     continue;
1237    
1238     if (match(my_name_for_link(conf), target_p->name))
1239     continue;
1240    
1241     if (IsCapable(client_p, CAP_TS6))
1242     {
1243     if (HasID(target_p))
1244     sendto_one(client_p, ":%s SID %s %d %s :%s%s",
1245     ID(target_p->servptr), target_p->name, target_p->hopcount+1,
1246     target_p->id, IsHidden(target_p) ? "(H) " : "",
1247     target_p->info);
1248     else /* introducing non-ts6 server */
1249     sendto_one(client_p, ":%s SERVER %s %d :%s%s",
1250     ID(target_p->servptr), target_p->name, target_p->hopcount+1,
1251     IsHidden(target_p) ? "(H) " : "", target_p->info);
1252     }
1253     else
1254     sendto_one(client_p, ":%s SERVER %s %d :%s%s",
1255     target_p->servptr->name, target_p->name, target_p->hopcount+1,
1256     IsHidden(target_p) ? "(H) " : "", target_p->info);
1257     }
1258    
1259 michael 120 if (!ServerInfo.hub && MyConnect(client_p))
1260 adx 30 uplink = client_p;
1261    
1262     server_burst(client_p);
1263     }
1264    
1265     static void
1266     start_io(struct Client *server)
1267     {
1268     struct LocalUser *lserver = server->localClient;
1269     int alloclen = 1;
1270     char *buf;
1271     dlink_node *ptr;
1272     struct dbuf_block *block;
1273    
1274     /* calculate how many bytes to allocate */
1275     if (IsCapable(server, CAP_ZIP))
1276     alloclen += 6;
1277     #ifdef HAVE_LIBCRYPTO
1278     if (IsCapable(server, CAP_ENC))
1279     alloclen += 16 + lserver->in_cipher->keylen + lserver->out_cipher->keylen;
1280     #endif
1281     alloclen += dbuf_length(&lserver->buf_recvq);
1282     alloclen += dlink_list_length(&lserver->buf_recvq.blocks) * 3;
1283     alloclen += dbuf_length(&lserver->buf_sendq);
1284     alloclen += dlink_list_length(&lserver->buf_sendq.blocks) * 3;
1285    
1286     /* initialize servlink control sendq */
1287     lserver->slinkq = buf = MyMalloc(alloclen);
1288     lserver->slinkq_ofs = 0;
1289     lserver->slinkq_len = alloclen;
1290    
1291     if (IsCapable(server, CAP_ZIP))
1292     {
1293     /* ziplink */
1294     *buf++ = SLINKCMD_SET_ZIP_OUT_LEVEL;
1295     *buf++ = 0; /* | */
1296     *buf++ = 1; /* \ len is 1 */
1297     *buf++ = ConfigFileEntry.compression_level;
1298     *buf++ = SLINKCMD_START_ZIP_IN;
1299     *buf++ = SLINKCMD_START_ZIP_OUT;
1300     }
1301     #ifdef HAVE_LIBCRYPTO
1302     if (IsCapable(server, CAP_ENC))
1303     {
1304     /* Decryption settings */
1305     *buf++ = SLINKCMD_SET_CRYPT_IN_CIPHER;
1306     *buf++ = 0; /* / (upper 8-bits of len) */
1307     *buf++ = 1; /* \ cipher id is 1 byte (lower 8-bits of len) */
1308     *buf++ = lserver->in_cipher->cipherid;
1309     *buf++ = SLINKCMD_SET_CRYPT_IN_KEY;
1310     *buf++ = 0; /* keylen < 256 */
1311     *buf++ = lserver->in_cipher->keylen;
1312     memcpy(buf, lserver->in_key, lserver->in_cipher->keylen);
1313     buf += lserver->in_cipher->keylen;
1314     /* Encryption settings */
1315     *buf++ = SLINKCMD_SET_CRYPT_OUT_CIPHER;
1316     *buf++ = 0; /* / (upper 8-bits of len) */
1317     *buf++ = 1; /* \ cipher id is 1 byte (lower 8-bits of len) */
1318     *buf++ = lserver->out_cipher->cipherid;
1319     *buf++ = SLINKCMD_SET_CRYPT_OUT_KEY;
1320     *buf++ = 0; /* keylen < 256 */
1321     *buf++ = lserver->out_cipher->keylen;
1322     memcpy(buf, lserver->out_key, lserver->out_cipher->keylen);
1323     buf += lserver->out_cipher->keylen;
1324     *buf++ = SLINKCMD_START_CRYPT_IN;
1325     *buf++ = SLINKCMD_START_CRYPT_OUT;
1326     }
1327     #endif
1328    
1329     /* pass the whole recvq to servlink */
1330     DLINK_FOREACH (ptr, lserver->buf_recvq.blocks.head)
1331     {
1332     block = ptr->data;
1333     *buf++ = SLINKCMD_INJECT_RECVQ;
1334     *buf++ = (block->size >> 8);
1335     *buf++ = (block->size & 0xff);
1336     memcpy(buf, &block->data[0], block->size);
1337     buf += block->size;
1338     }
1339     dbuf_clear(&lserver->buf_recvq);
1340    
1341     /* pass the whole sendq to servlink */
1342     DLINK_FOREACH (ptr, lserver->buf_sendq.blocks.head)
1343     {
1344     block = ptr->data;
1345     *buf++ = SLINKCMD_INJECT_SENDQ;
1346     *buf++ = (block->size >> 8);
1347     *buf++ = (block->size & 0xff);
1348     memcpy(buf, &block->data[0], block->size);
1349     buf += block->size;
1350     }
1351     dbuf_clear(&lserver->buf_sendq);
1352    
1353     /* start io */
1354     *buf++ = SLINKCMD_INIT;
1355    
1356     /* schedule a write */
1357     send_queued_slink_write(server);
1358     }
1359    
1360     /* fork_server()
1361     *
1362     * inputs - struct Client *server
1363     * output - success: 0 / failure: -1
1364     * side effect - fork, and exec SERVLINK to handle this connection
1365     */
1366     static int
1367     fork_server(struct Client *server)
1368     {
1369     #ifndef HAVE_SOCKETPAIR
1370     return -1;
1371     #else
1372     int i;
1373     int slink_fds[2][2];
1374     /* 0? - ctrl | 1? - data
1375     * ?0 - child | ?1 - parent */
1376    
1377     if (socketpair(AF_UNIX, SOCK_STREAM, 0, slink_fds[0]) < 0)
1378     return -1;
1379     if (socketpair(AF_UNIX, SOCK_STREAM, 0, slink_fds[1]) < 0)
1380     goto free_ctrl_fds;
1381    
1382     if ((i = fork()) < 0)
1383     {
1384     close(slink_fds[1][0]); close(slink_fds[1][1]);
1385     free_ctrl_fds:
1386     close(slink_fds[0][0]); close(slink_fds[0][1]);
1387     return -1;
1388     }
1389    
1390     if (i == 0)
1391     {
1392     char fd_str[3][6]; /* store 3x sizeof("65535") */
1393     char *kid_argv[7];
1394    
1395     #ifdef O_ASYNC
1396     fcntl(server->localClient->fd.fd, F_SETFL,
1397     fcntl(server->localClient->fd.fd, F_GETFL, 0) & ~O_ASYNC);
1398     #endif
1399     close_fds(&server->localClient->fd);
1400     close(slink_fds[0][1]);
1401     close(slink_fds[1][1]);
1402    
1403     sprintf(fd_str[0], "%d", slink_fds[0][0]);
1404     sprintf(fd_str[1], "%d", slink_fds[1][0]);
1405     sprintf(fd_str[2], "%d", server->localClient->fd.fd);
1406    
1407     kid_argv[0] = "-slink";
1408     kid_argv[1] = kid_argv[2] = fd_str[0]; /* ctrl */
1409     kid_argv[3] = kid_argv[4] = fd_str[1]; /* data */
1410     kid_argv[5] = fd_str[2]; /* network */
1411     kid_argv[6] = NULL;
1412    
1413     execv(ConfigFileEntry.servlink_path, kid_argv);
1414    
1415     _exit(1);
1416     }
1417    
1418     /* close the network fd and the child ends of the pipes */
1419     fd_close(&server->localClient->fd);
1420     close(slink_fds[0][0]);
1421     close(slink_fds[1][0]);
1422    
1423     execute_callback(setup_socket_cb, slink_fds[0][1]);
1424     execute_callback(setup_socket_cb, slink_fds[1][1]);
1425    
1426     fd_open(&server->localClient->ctrlfd, slink_fds[0][1], 1, "slink ctrl");
1427     fd_open(&server->localClient->fd, slink_fds[1][1], 1, "slink data");
1428    
1429     read_ctrl_packet(&server->localClient->ctrlfd, server);
1430     read_packet(&server->localClient->fd, server);
1431    
1432     return 0;
1433     #endif
1434     }
1435    
1436     /* server_burst()
1437     *
1438     * inputs - struct Client pointer server
1439     * -
1440     * output - none
1441     * side effects - send a server burst
1442     * bugs - still too long
1443     */
1444     static void
1445     server_burst(struct Client *client_p)
1446     {
1447     /* Send it in the shortened format with the TS, if
1448     ** it's a TS server; walk the list of channels, sending
1449     ** all the nicks that haven't been sent yet for each
1450     ** channel, then send the channel itself -- it's less
1451     ** obvious than sending all nicks first, but on the
1452     ** receiving side memory will be allocated more nicely
1453     ** saving a few seconds in the handling of a split
1454     ** -orabidoo
1455     */
1456    
1457     /* On a "lazy link" hubs send nothing.
1458     * Leafs always have to send nicks plus channels
1459     */
1460     if (IsCapable(client_p, CAP_LL))
1461     {
1462     if (!ServerInfo.hub)
1463     {
1464     /* burst all our info */
1465     burst_all(client_p);
1466    
1467     /* Now, ask for channel info on all our current channels */
1468     cjoin_all(client_p);
1469     }
1470     }
1471     else
1472     {
1473     burst_all(client_p);
1474     }
1475    
1476     /* EOB stuff is now in burst_all */
1477     /* Always send a PING after connect burst is done */
1478     sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p));
1479     }
1480    
1481     /* burst_all()
1482     *
1483     * inputs - pointer to server to send burst to
1484     * output - NONE
1485     * side effects - complete burst of channels/nicks is sent to client_p
1486     */
1487     static void
1488     burst_all(struct Client *client_p)
1489     {
1490 michael 120 dlink_node *ptr = NULL;
1491 adx 30
1492 michael 120 DLINK_FOREACH(ptr, global_channel_list.head)
1493 adx 30 {
1494 michael 120 struct Channel *chptr = ptr->data;
1495 adx 30
1496     if (dlink_list_length(&chptr->members) != 0)
1497     {
1498     burst_members(client_p, chptr);
1499     send_channel_modes(client_p, chptr);
1500 michael 120
1501     if (IsCapable(client_p, CAP_TBURST) ||
1502     IsCapable(client_p, CAP_TB))
1503 adx 30 send_tb(client_p, chptr);
1504     }
1505     }
1506    
1507     /* also send out those that are not on any channel
1508     */
1509     DLINK_FOREACH(ptr, global_client_list.head)
1510     {
1511 michael 120 struct Client *target_p = ptr->data;
1512 adx 30
1513     if (!IsBursted(target_p) && target_p->from != client_p)
1514     sendnick_TS(client_p, target_p);
1515    
1516     ClearBursted(target_p);
1517     }
1518    
1519     /* We send the time we started the burst, and let the remote host determine an EOB time,
1520     ** as otherwise we end up sending a EOB of 0 Sending here means it gets sent last -- fl
1521     */
1522     /* Its simpler to just send EOB and use the time its been connected.. --fl_ */
1523     if (IsCapable(client_p, CAP_EOB))
1524     sendto_one(client_p, ":%s EOB", ID_or_name(&me, client_p));
1525     }
1526    
1527     /*
1528     * send_tb
1529     *
1530     * inputs - pointer to Client
1531     * - pointer to channel
1532     * output - NONE
1533     * side effects - Called on a server burst when
1534 michael 120 * server is CAP_TB|CAP_TBURST capable
1535 adx 30 */
1536     static void
1537 michael 120 send_tb(struct Client *client_p, const struct Channel *chptr)
1538 adx 30 {
1539 michael 120 /*
1540     * XXX - Logic here isn't right either. What if the topic got unset?
1541     * We would need to send an empty TOPIC to reset the topic from the
1542     * other side
1543     */
1544     if (chptr->topic != NULL)
1545 adx 30 {
1546 michael 120 if (IsCapable(client_p, CAP_TBURST))
1547     sendto_one(client_p, ":%s TBURST %lu %s %lu %s :%s",
1548     me.name, (unsigned long)chptr->channelts, chptr->chname,
1549     (unsigned long)chptr->topic_time, chptr->topic_info,
1550     chptr->topic);
1551     else if (IsCapable(client_p, CAP_TB))
1552 adx 30 {
1553 michael 120 if (ConfigChannel.burst_topicwho)
1554     {
1555     sendto_one(client_p, ":%s TB %s %lu %s :%s",
1556     me.name, chptr->chname,
1557     (unsigned long)chptr->topic_time,
1558     chptr->topic_info, chptr->topic);
1559     }
1560     else
1561     {
1562     sendto_one(client_p, ":%s TB %s %lu :%s",
1563     me.name, chptr->chname,
1564     (unsigned long)chptr->topic_time, chptr->topic);
1565     }
1566 adx 30 }
1567     }
1568     }
1569    
1570     /* cjoin_all()
1571     *
1572     * inputs - server to ask for channel info from
1573     * output - NONE
1574     * side effects - CJOINS for all the leafs known channels is sent
1575     */
1576     static void
1577     cjoin_all(struct Client *client_p)
1578     {
1579     const dlink_node *gptr = NULL;
1580    
1581     DLINK_FOREACH(gptr, global_channel_list.head)
1582     {
1583     const struct Channel *chptr = gptr->data;
1584     sendto_one(client_p, ":%s CBURST %s",
1585     me.name, chptr->chname);
1586     }
1587     }
1588    
1589     /* burst_channel()
1590     *
1591     * inputs - pointer to server to send sjoins to
1592     * - channel pointer
1593     * output - none
1594     * side effects - All sjoins for channel(s) given by chptr are sent
1595     * for all channel members. ONLY called by hub on
1596     * behalf of a lazylink so client_p is always guarunteed
1597     * to be a LL leaf.
1598     */
1599     void
1600     burst_channel(struct Client *client_p, struct Channel *chptr)
1601     {
1602     burst_ll_members(client_p, chptr);
1603    
1604     send_channel_modes(client_p, chptr);
1605     add_lazylinkchannel(client_p,chptr);
1606    
1607     if (chptr->topic != NULL && chptr->topic_info != NULL)
1608     {
1609     sendto_one(client_p, ":%s TOPIC %s %s %lu :%s",
1610     me.name, chptr->chname, chptr->topic_info,
1611     (unsigned long)chptr->topic_time, chptr->topic);
1612     }
1613     }
1614    
1615     /* add_lazlinkchannel()
1616     *
1617     * inputs - pointer to directly connected leaf server
1618     * being introduced to this hub
1619     * - pointer to channel structure being introduced
1620     * output - NONE
1621     * side effects - The channel pointed to by chptr is now known
1622     * to be on lazyleaf server given by local_server_p.
1623     * mark that in the bit map and add to the list
1624     * of channels to examine after this newly introduced
1625     * server is squit off.
1626     */
1627     static void
1628     add_lazylinkchannel(struct Client *local_server_p, struct Channel *chptr)
1629     {
1630     assert(MyConnect(local_server_p));
1631    
1632     chptr->lazyLinkChannelExists |= local_server_p->localClient->serverMask;
1633     dlinkAdd(chptr, make_dlink_node(), &lazylink_channels);
1634     }
1635    
1636     /* add_lazylinkclient()
1637     *
1638     * inputs - pointer to directly connected leaf server
1639     * being introduced to this hub
1640     * - pointer to client being introduced
1641     * output - NONE
1642     * side effects - The client pointed to by client_p is now known
1643     * to be on lazyleaf server given by local_server_p.
1644     * mark that in the bit map and add to the list
1645     * of clients to examine after this newly introduced
1646     * server is squit off.
1647     */
1648     void
1649     add_lazylinkclient(struct Client *local_server_p, struct Client *client_p)
1650     {
1651     assert(MyConnect(local_server_p));
1652     client_p->lazyLinkClientExists |= local_server_p->localClient->serverMask;
1653     }
1654    
1655     /* remove_lazylink_flags()
1656     *
1657     * inputs - pointer to server quitting
1658     * output - NONE
1659     * side effects - All the channels on the lazylink channel list are examined
1660     * If they hold a bit corresponding to the servermask
1661     * attached to client_p, clear that bit. If this bitmask
1662     * goes to 0, then the channel is no longer known to
1663     * be on any lazylink server, and can be removed from the
1664     * link list.
1665     *
1666     * Similar is done for lazylink clients
1667     *
1668     * This function must be run by the HUB on any exiting
1669     * lazylink leaf server, while the pointer is still valid.
1670     * Hence must be run from client.c in exit_one_client()
1671     *
1672     * The old code scanned all channels, this code only
1673     * scans channels/clients on the lazylink_channels
1674     * lazylink_clients lists.
1675     */
1676     void
1677     remove_lazylink_flags(unsigned long mask)
1678     {
1679     dlink_node *ptr;
1680     dlink_node *next_ptr;
1681     struct Channel *chptr;
1682     struct Client *target_p;
1683     unsigned long clear_mask;
1684    
1685     if (!mask) /* On 0 mask, don't do anything */
1686     return;
1687    
1688     clear_mask = ~mask;
1689     freeMask |= mask;
1690    
1691     DLINK_FOREACH_SAFE(ptr, next_ptr, lazylink_channels.head)
1692     {
1693     chptr = ptr->data;
1694    
1695     chptr->lazyLinkChannelExists &= clear_mask;
1696    
1697     if (chptr->lazyLinkChannelExists == 0)
1698     {
1699     dlinkDelete(ptr, &lazylink_channels);
1700     free_dlink_node(ptr);
1701     }
1702     }
1703    
1704     DLINK_FOREACH(ptr, global_client_list.head)
1705     {
1706     target_p = ptr->data;
1707     target_p->lazyLinkClientExists &= clear_mask;
1708     }
1709     }
1710    
1711     /* burst_members()
1712     *
1713     * inputs - pointer to server to send members to
1714     * - dlink_list pointer to membership list to send
1715     * output - NONE
1716     * side effects -
1717     */
1718     static void
1719     burst_members(struct Client *client_p, struct Channel *chptr)
1720     {
1721     struct Client *target_p;
1722     struct Membership *ms;
1723     dlink_node *ptr;
1724    
1725     DLINK_FOREACH(ptr, chptr->members.head)
1726     {
1727     ms = ptr->data;
1728     target_p = ms->client_p;
1729    
1730     if (!IsBursted(target_p))
1731     {
1732     SetBursted(target_p);
1733    
1734     if (target_p->from != client_p)
1735     sendnick_TS(client_p, target_p);
1736     }
1737     }
1738     }
1739    
1740     /* burst_ll_members()
1741     *
1742     * inputs - pointer to server to send members to
1743     * - dlink_list pointer to membership list to send
1744     * output - NONE
1745     * side effects - This version also has to check the bitmap for lazylink
1746     */
1747     static void
1748     burst_ll_members(struct Client *client_p, struct Channel *chptr)
1749     {
1750     struct Client *target_p;
1751     struct Membership *ms;
1752     dlink_node *ptr;
1753    
1754     DLINK_FOREACH(ptr, chptr->members.head)
1755     {
1756     ms = ptr->data;
1757     target_p = ms->client_p;
1758    
1759     if ((target_p->lazyLinkClientExists & client_p->localClient->serverMask) == 0)
1760     {
1761     if (target_p->from != client_p)
1762     {
1763     add_lazylinkclient(client_p,target_p);
1764     sendnick_TS(client_p, target_p);
1765     }
1766     }
1767     }
1768     }
1769    
1770     /* set_autoconn()
1771     *
1772     * inputs - struct Client pointer to oper requesting change
1773     * output - none
1774     * side effects - set autoconnect mode
1775     */
1776     void
1777     set_autoconn(struct Client *source_p, const char *name, int newval)
1778     {
1779     struct ConfItem *conf;
1780     struct AccessItem *aconf;
1781    
1782     if (name != NULL)
1783     {
1784     conf = find_exact_name_conf(SERVER_TYPE, name, NULL, NULL);
1785     if (conf != NULL)
1786     {
1787     aconf = (struct AccessItem *)map_to_conf(conf);
1788     if (newval)
1789     SetConfAllowAutoConn(aconf);
1790     else
1791     ClearConfAllowAutoConn(aconf);
1792    
1793     sendto_realops_flags(UMODE_ALL, L_ALL,
1794     "%s has changed AUTOCONN for %s to %i",
1795     source_p->name, name, newval);
1796     sendto_one(source_p,
1797     ":%s NOTICE %s :AUTOCONN for %s is now set to %i",
1798     me.name, source_p->name, name, newval);
1799     }
1800     else
1801     {
1802     sendto_one(source_p, ":%s NOTICE %s :Can't find %s",
1803     me.name, source_p->name, name);
1804     }
1805     }
1806     else
1807     {
1808     sendto_one(source_p, ":%s NOTICE %s :Please specify a server name!",
1809     me.name, source_p->name);
1810     }
1811     }
1812    
1813     void
1814     initServerMask(void)
1815     {
1816     freeMask = 0xFFFFFFFFUL;
1817     }
1818    
1819     /* nextFreeMask()
1820     *
1821     * inputs - NONE
1822     * output - unsigned long next unused mask for use in LL
1823     * side effects -
1824     */
1825     unsigned long
1826     nextFreeMask(void)
1827     {
1828     int i;
1829     unsigned long mask = 1;
1830    
1831     for (i = 0; i < 32; i++)
1832     {
1833     if (mask & freeMask)
1834     {
1835     freeMask &= ~mask;
1836     return(mask);
1837     }
1838    
1839     mask <<= 1;
1840     }
1841    
1842     return(0L); /* watch this special case ... */
1843     }
1844    
1845     /* New server connection code
1846     * Based upon the stuff floating about in s_bsd.c
1847     * -- adrian
1848     */
1849    
1850     /* serv_connect() - initiate a server connection
1851     *
1852     * inputs - pointer to conf
1853     * - pointer to client doing the connect
1854     * output -
1855     * side effects -
1856     *
1857     * This code initiates a connection to a server. It first checks to make
1858     * sure the given server exists. If this is the case, it creates a socket,
1859     * creates a client, saves the socket information in the client, and
1860     * initiates a connection to the server through comm_connect_tcp(). The
1861     * completion of this goes through serv_completed_connection().
1862     *
1863     * We return 1 if the connection is attempted, since we don't know whether
1864     * it suceeded or not, and 0 if it fails in here somewhere.
1865     */
1866     int
1867     serv_connect(struct AccessItem *aconf, struct Client *by)
1868     {
1869     struct ConfItem *conf;
1870     struct Client *client_p;
1871     char buf[HOSTIPLEN];
1872    
1873     /* conversion structs */
1874     struct sockaddr_in *v4;
1875     /* Make sure aconf is useful */
1876     assert(aconf != NULL);
1877    
1878     if(aconf == NULL)
1879     return (0);
1880    
1881     /* XXX should be passing struct ConfItem in the first place */
1882     conf = unmap_conf_item(aconf);
1883    
1884     /* log */
1885     irc_getnameinfo((struct sockaddr*)&aconf->ipnum, aconf->ipnum.ss_len,
1886     buf, HOSTIPLEN, NULL, 0, NI_NUMERICHOST);
1887     ilog(L_NOTICE, "Connect to %s[%s] @%s", aconf->user, aconf->host,
1888     buf);
1889    
1890     /* Still processing a DNS lookup? -> exit */
1891     if (aconf->dns_query != NULL)
1892     {
1893     sendto_realops_flags(UMODE_ALL, L_OPER,
1894     "Error connecting to %s: Error during DNS lookup", conf->name);
1895     return (0);
1896     }
1897    
1898     /* Make sure this server isn't already connected
1899     * Note: aconf should ALWAYS be a valid C: line
1900     */
1901     if ((client_p = find_server(conf->name)) != NULL)
1902     {
1903     sendto_realops_flags(UMODE_ALL, L_ADMIN,
1904     "Server %s already present from %s",
1905     conf->name, get_client_name(client_p, SHOW_IP));
1906     sendto_realops_flags(UMODE_ALL, L_OPER,
1907     "Server %s already present from %s",
1908     conf->name, get_client_name(client_p, MASK_IP));
1909     if (by && IsClient(by) && !MyClient(by))
1910     sendto_one(by, ":%s NOTICE %s :Server %s already present from %s",
1911     me.name, by->name, conf->name,
1912     get_client_name(client_p, MASK_IP));
1913     return (0);
1914     }
1915    
1916     /* Create a local client */
1917     client_p = make_client(NULL);
1918    
1919     /* Copy in the server, hostname, fd */
1920     strlcpy(client_p->name, conf->name, sizeof(client_p->name));
1921     strlcpy(client_p->host, aconf->host, sizeof(client_p->host));
1922    
1923     /* We already converted the ip once, so lets use it - stu */
1924     strlcpy(client_p->sockhost, buf, HOSTIPLEN);
1925    
1926     /* create a socket for the server connection */
1927     if (comm_open(&client_p->localClient->fd, aconf->ipnum.ss.ss_family,
1928     SOCK_STREAM, 0, NULL) < 0)
1929     {
1930     /* Eek, failure to create the socket */
1931     report_error(L_ALL,
1932     "opening stream socket to %s: %s", conf->name, errno);
1933     SetDead(client_p);
1934     exit_client(client_p, &me, "Connection failed");
1935     return (0);
1936     }
1937    
1938     /* servernames are always guaranteed under HOSTLEN chars */
1939     fd_note(&client_p->localClient->fd, "Server: %s", conf->name);
1940    
1941     /* Attach config entries to client here rather than in
1942     * serv_connect_callback(). This to avoid null pointer references.
1943     */
1944     if (!attach_connect_block(client_p, conf->name, aconf->host))
1945     {
1946     sendto_realops_flags(UMODE_ALL, L_ALL,
1947     "Host %s is not enabled for connecting:no C/N-line",
1948     conf->name);
1949     if (by && IsClient(by) && !MyClient(by))
1950     sendto_one(by, ":%s NOTICE %s :Connect to host %s failed.",
1951     me.name, by->name, client_p->name);
1952     SetDead(client_p);
1953     exit_client(client_p, client_p, "Connection failed");
1954     return (0);
1955     }
1956    
1957     /* at this point we have a connection in progress and C/N lines
1958     * attached to the client, the socket info should be saved in the
1959     * client and it should either be resolved or have a valid address.
1960     *
1961     * The socket has been connected or connect is in progress.
1962     */
1963     make_server(client_p);
1964    
1965     if (by && IsClient(by))
1966     strlcpy(client_p->serv->by, by->name, sizeof(client_p->serv->by));
1967     else
1968     strlcpy(client_p->serv->by, "AutoConn.", sizeof(client_p->serv->by));
1969    
1970     SetConnecting(client_p);
1971     dlinkAdd(client_p, &client_p->node, &global_client_list);
1972     /* from def_fam */
1973     client_p->localClient->aftype = aconf->aftype;
1974    
1975     /* Now, initiate the connection */
1976     /* XXX assume that a non 0 type means a specific bind address
1977     * for this connect.
1978     */
1979     switch (aconf->aftype)
1980     {
1981     case AF_INET:
1982     v4 = (struct sockaddr_in*)&aconf->my_ipnum;
1983     if (v4->sin_addr.s_addr != 0)
1984     {
1985     struct irc_ssaddr ipn;
1986     memset(&ipn, 0, sizeof(struct irc_ssaddr));
1987     ipn.ss.ss_family = AF_INET;
1988     ipn.ss_port = 0;
1989     memcpy(&ipn, &aconf->my_ipnum, sizeof(struct irc_ssaddr));
1990     comm_connect_tcp(&client_p->localClient->fd, aconf->host, aconf->port,
1991     (struct sockaddr *)&ipn, ipn.ss_len,
1992     serv_connect_callback, client_p, aconf->aftype,
1993     CONNECTTIMEOUT);
1994     }
1995     else if (ServerInfo.specific_ipv4_vhost)
1996     {
1997     struct irc_ssaddr ipn;
1998     memset(&ipn, 0, sizeof(struct irc_ssaddr));
1999     ipn.ss.ss_family = AF_INET;
2000     ipn.ss_port = 0;
2001     memcpy(&ipn, &ServerInfo.ip, sizeof(struct irc_ssaddr));
2002     comm_connect_tcp(&client_p->localClient->fd, aconf->host, aconf->port,
2003     (struct sockaddr *)&ipn, ipn.ss_len,
2004     serv_connect_callback, client_p, aconf->aftype,
2005     CONNECTTIMEOUT);
2006     }
2007     else
2008     comm_connect_tcp(&client_p->localClient->fd, aconf->host, aconf->port,
2009     NULL, 0, serv_connect_callback, client_p, aconf->aftype,
2010     CONNECTTIMEOUT);
2011     break;
2012     #ifdef IPV6
2013     case AF_INET6:
2014     {
2015     struct irc_ssaddr ipn;
2016     struct sockaddr_in6 *v6;
2017     struct sockaddr_in6 *v6conf;
2018    
2019     memset(&ipn, 0, sizeof(struct irc_ssaddr));
2020     v6conf = (struct sockaddr_in6 *)&aconf->my_ipnum;
2021     v6 = (struct sockaddr_in6 *)&ipn;
2022    
2023     if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr,
2024     sizeof(struct in6_addr)) != 0)
2025     {
2026     memcpy(&ipn, &aconf->my_ipnum, sizeof(struct irc_ssaddr));
2027     ipn.ss.ss_family = AF_INET6;
2028     ipn.ss_port = 0;
2029     comm_connect_tcp(&client_p->localClient->fd,
2030     aconf->host, aconf->port,
2031     (struct sockaddr *)&ipn, ipn.ss_len,
2032     serv_connect_callback, client_p,
2033     aconf->aftype, CONNECTTIMEOUT);
2034     }
2035     else if (ServerInfo.specific_ipv6_vhost)
2036     {
2037     memcpy(&ipn, &ServerInfo.ip6, sizeof(struct irc_ssaddr));
2038     ipn.ss.ss_family = AF_INET6;
2039     ipn.ss_port = 0;
2040     comm_connect_tcp(&client_p->localClient->fd,
2041     aconf->host, aconf->port,
2042     (struct sockaddr *)&ipn, ipn.ss_len,
2043     serv_connect_callback, client_p,
2044     aconf->aftype, CONNECTTIMEOUT);
2045     }
2046     else
2047     comm_connect_tcp(&client_p->localClient->fd,
2048     aconf->host, aconf->port,
2049     NULL, 0, serv_connect_callback, client_p,
2050     aconf->aftype, CONNECTTIMEOUT);
2051     }
2052     #endif
2053     }
2054     return (1);
2055     }
2056    
2057     /* serv_connect_callback() - complete a server connection.
2058     *
2059     * This routine is called after the server connection attempt has
2060     * completed. If unsucessful, an error is sent to ops and the client
2061     * is closed. If sucessful, it goes through the initialisation/check
2062     * procedures, the capabilities are sent, and the socket is then
2063     * marked for reading.
2064     */
2065     static void
2066     serv_connect_callback(fde_t *fd, int status, void *data)
2067     {
2068     struct Client *client_p = data;
2069     struct ConfItem *conf=NULL;
2070     struct AccessItem *aconf=NULL;
2071    
2072     /* First, make sure its a real client! */
2073     assert(client_p != NULL);
2074     assert(&client_p->localClient->fd == fd);
2075    
2076     /* Next, for backward purposes, record the ip of the server */
2077     memcpy(&client_p->localClient->ip, &fd->connect.hostaddr,
2078     sizeof(struct irc_ssaddr));
2079     /* Check the status */
2080     if (status != COMM_OK)
2081     {
2082     /* We have an error, so report it and quit
2083     * Admins get to see any IP, mere opers don't *sigh*
2084     */
2085     if (ConfigServerHide.hide_server_ips)
2086     sendto_realops_flags(UMODE_ALL, L_ADMIN,
2087     "Error connecting to %s: %s",
2088     client_p->name, comm_errstr(status));
2089     else
2090     sendto_realops_flags(UMODE_ALL, L_ADMIN,
2091     "Error connecting to %s[%s]: %s", client_p->name,
2092     client_p->host, comm_errstr(status));
2093    
2094     sendto_realops_flags(UMODE_ALL, L_OPER,
2095     "Error connecting to %s: %s",
2096     client_p->name, comm_errstr(status));
2097    
2098     /* If a fd goes bad, call dead_link() the socket is no
2099     * longer valid for reading or writing.
2100     */
2101     dead_link_on_write(client_p, 0);
2102     return;
2103     }
2104    
2105     /* COMM_OK, so continue the connection procedure */
2106     /* Get the C/N lines */
2107 db 101 conf = client_p->localClient->iline;
2108 adx 30 if (conf == NULL)
2109     {
2110     sendto_realops_flags(UMODE_ALL, L_ADMIN,
2111     "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
2112     sendto_realops_flags(UMODE_ALL, L_OPER,
2113     "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
2114    
2115     exit_client(client_p, &me, "Lost connect{} block");
2116     return;
2117     }
2118    
2119     aconf = (struct AccessItem *)map_to_conf(conf);
2120     /* Next, send the initial handshake */
2121     SetHandshake(client_p);
2122    
2123     #ifdef HAVE_LIBCRYPTO
2124     /* Handle all CRYPTLINK links in cryptlink_init */
2125     if (IsConfCryptLink(aconf))
2126     {
2127     cryptlink_init(client_p, conf, fd);
2128     return;
2129     }
2130     #endif
2131    
2132     /* jdc -- Check and send spasswd, not passwd. */
2133     if (!EmptyString(aconf->spasswd) && (me.id[0] != '\0'))
2134     /* Send TS 6 form only if id */
2135     sendto_one(client_p, "PASS %s TS %d %s",
2136     aconf->spasswd, TS_CURRENT, me.id);
2137     else
2138     sendto_one(client_p, "PASS %s TS 5",
2139     aconf->spasswd);
2140    
2141     /* Pass my info to the new server
2142     *
2143     * If trying to negotiate LazyLinks, pass on CAP_LL
2144     * If this is a HUB, pass on CAP_HUB
2145     * Pass on ZIP if supported
2146     * Pass on TB if supported.
2147     * - Dianora
2148     */
2149     send_capabilities(client_p, aconf,
2150     (IsConfLazyLink(aconf) ? find_capability("LL") : 0)
2151     | (IsConfCompressed(aconf) ? find_capability("ZIP") : 0)
2152 michael 120 | (IsConfTopicBurst(aconf) ? find_capability("TBURST")|find_capability("TB") : 0)
2153 adx 30 , 0);
2154    
2155     sendto_one(client_p, "SERVER %s 1 :%s%s",
2156     my_name_for_link(conf),
2157     ConfigServerHide.hidden ? "(H) " : "",
2158     me.info);
2159    
2160     /* If we've been marked dead because a send failed, just exit
2161     * here now and save everyone the trouble of us ever existing.
2162     */
2163     if (IsDead(client_p))
2164     {
2165     sendto_realops_flags(UMODE_ALL, L_ADMIN,
2166     "%s[%s] went dead during handshake",
2167     client_p->name,
2168     client_p->host);
2169     sendto_realops_flags(UMODE_ALL, L_OPER,
2170     "%s went dead during handshake", client_p->name);
2171     return;
2172     }
2173    
2174     /* don't move to serv_list yet -- we haven't sent a burst! */
2175     /* If we get here, we're ok, so lets start reading some data */
2176     comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0);
2177     }
2178    
2179     struct Client *
2180     find_servconn_in_progress(const char *name)
2181     {
2182     dlink_node *ptr;
2183     struct Client *cptr;
2184    
2185     DLINK_FOREACH(ptr, unknown_list.head)
2186     {
2187     cptr = ptr->data;
2188    
2189     if (cptr && cptr->name[0])
2190     if (match(cptr->name, name) || match(name, cptr->name))
2191     return cptr;
2192     }
2193    
2194     return NULL;
2195     }
2196    
2197     #ifdef HAVE_LIBCRYPTO
2198     /*
2199     * sends a CRYPTLINK SERV command.
2200     */
2201     void
2202     cryptlink_init(struct Client *client_p, struct ConfItem *conf, fde_t *fd)
2203     {
2204     struct AccessItem *aconf;
2205     char *encrypted;
2206     unsigned char *key_to_send;
2207     char randkey[CIPHERKEYLEN];
2208     int enc_len;
2209    
2210     /* get key */
2211     if ((!ServerInfo.rsa_private_key) ||
2212     (!RSA_check_key(ServerInfo.rsa_private_key)) )
2213     {
2214     cryptlink_error(client_p, "SERV", "Invalid RSA private key",
2215     "Invalid RSA private key");
2216     return;
2217     }
2218    
2219     aconf = (struct AccessItem *)map_to_conf(conf);
2220    
2221     if (aconf->rsa_public_key == NULL)
2222     {
2223     cryptlink_error(client_p, "SERV", "Invalid RSA public key",
2224     "Invalid RSA public key");
2225     return;
2226     }
2227    
2228     if (get_randomness((unsigned char *)randkey, CIPHERKEYLEN) != 1)
2229     {
2230     cryptlink_error(client_p, "SERV", "Couldn't generate keyphrase",
2231     "Couldn't generate keyphrase");
2232     return;
2233     }
2234    
2235     encrypted = MyMalloc(RSA_size(ServerInfo.rsa_private_key));
2236     enc_len = RSA_public_encrypt(CIPHERKEYLEN,
2237     (unsigned char *)randkey,
2238     (unsigned char *)encrypted,
2239     aconf->rsa_public_key,
2240     RSA_PKCS1_PADDING);
2241    
2242     memcpy(client_p->localClient->in_key, randkey, CIPHERKEYLEN);
2243    
2244     if (enc_len <= 0)
2245     {
2246     report_crypto_errors();
2247     MyFree(encrypted);
2248     cryptlink_error(client_p, "SERV", "Couldn't encrypt data",
2249     "Couldn't encrypt data");
2250     return;
2251     }
2252    
2253     if (!(base64_block(&key_to_send, encrypted, enc_len)))
2254     {
2255     MyFree(encrypted);
2256     cryptlink_error(client_p, "SERV", "Couldn't base64 encode key",
2257     "Couldn't base64 encode key");
2258     return;
2259     }
2260    
2261     send_capabilities(client_p, aconf,
2262     (IsConfLazyLink(aconf) ? find_capability("LL") : 0)
2263     | (IsConfCompressed(aconf) ? find_capability("ZIP") : 0)
2264 michael 120 | (IsConfTopicBurst(aconf) ? find_capability("TBURST")|find_capability("TB") : 0)
2265 adx 30 , CAP_ENC_MASK);
2266    
2267 michael 120 if (me.id[0] != '\0')
2268 adx 41 sendto_one(client_p, "PASS . TS %d %s", TS_CURRENT, me.id);
2269    
2270 adx 30 sendto_one(client_p, "CRYPTLINK SERV %s %s :%s%s",
2271     my_name_for_link(conf), key_to_send,
2272     ConfigServerHide.hidden ? "(H) " : "", me.info);
2273    
2274     SetHandshake(client_p);
2275     SetWaitAuth(client_p);
2276    
2277     MyFree(encrypted);
2278     MyFree(key_to_send);
2279    
2280     if (IsDead(client_p))
2281     cryptlink_error(client_p, "SERV", "Went dead during handshake",
2282     "Went dead during handshake");
2283     else if (fd != NULL)
2284     /* If we get here, we're ok, so lets start reading some data */
2285     comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0);
2286     }
2287    
2288     void
2289     cryptlink_error(struct Client *client_p, const char *type,
2290     const char *reason, const char *client_reason)
2291     {
2292     sendto_realops_flags(UMODE_ALL, L_ADMIN, "%s: CRYPTLINK %s error - %s",
2293     get_client_name(client_p, SHOW_IP), type, reason);
2294     sendto_realops_flags(UMODE_ALL, L_OPER, "%s: CRYPTLINK %s error - %s",
2295     get_client_name(client_p, MASK_IP), type, reason);
2296     ilog(L_ERROR, "%s: CRYPTLINK %s error - %s",
2297     get_client_name(client_p, SHOW_IP), type, reason);
2298    
2299     /* If client_reason isn't NULL, then exit the client with the message
2300     * defined in the call.
2301     */
2302     if ((client_reason != NULL) && (!IsDead(client_p)))
2303     exit_client(client_p, &me, client_reason);
2304     }
2305    
2306     static char base64_chars[] =
2307     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
2308    
2309     static char base64_values[] =
2310     {
2311     /* 00-15 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2312     /* 16-31 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2313     /* 32-47 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
2314     /* 48-63 */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1,
2315     /* 64-79 */ -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
2316     /* 80-95 */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
2317     /* 96-111 */ -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
2318     /* 112-127 */ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
2319     /* 128-143 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2320     /* 144-159 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2321     /* 160-175 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2322     /* 186-191 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2323     /* 192-207 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2324     /* 208-223 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2325     /* 224-239 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2326     /* 240-255 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
2327     };
2328    
2329     /*
2330     * base64_block will allocate and return a new block of memory
2331     * using MyMalloc(). It should be freed after use.
2332     */
2333     int
2334     base64_block(unsigned char **output, char *data, int len)
2335     {
2336     unsigned char *out;
2337     unsigned char *in = (unsigned char*)data;
2338     unsigned long int q_in;
2339     int i;
2340     int count = 0;
2341    
2342     out = MyMalloc(((((len + 2) - ((len + 2) % 3)) / 3) * 4) + 1);
2343    
2344     /* process 24 bits at a time */
2345     for( i = 0; i < len; i += 3)
2346     {
2347     q_in = 0;
2348    
2349     if ( i + 2 < len )
2350     {
2351     q_in = (in[i+2] & 0xc0) << 2;
2352     q_in |= in[i+2];
2353     }
2354    
2355     if ( i + 1 < len )
2356     {
2357     q_in |= (in[i+1] & 0x0f) << 10;
2358     q_in |= (in[i+1] & 0xf0) << 12;
2359     }
2360    
2361     q_in |= (in[i] & 0x03) << 20;
2362     q_in |= in[i] << 22;
2363    
2364     q_in &= 0x3f3f3f3f;
2365    
2366     out[count++] = base64_chars[((q_in >> 24) )];
2367     out[count++] = base64_chars[((q_in >> 16) & 0xff)];
2368     out[count++] = base64_chars[((q_in >> 8) & 0xff)];
2369     out[count++] = base64_chars[((q_in ) & 0xff)];
2370     }
2371     if ( (i - len) > 0 )
2372     {
2373     out[count-1] = '=';
2374     if ( (i - len) > 1 )
2375     out[count-2] = '=';
2376     }
2377    
2378     out[count] = '\0';
2379     *output = out;
2380     return (count);
2381     }
2382    
2383     /*
2384     * unbase64_block will allocate and return a new block of memory
2385     * using MyMalloc(). It should be freed after use.
2386     */
2387     int
2388     unbase64_block(unsigned char **output, char *data, int len)
2389     {
2390     unsigned char *out;
2391     unsigned char *in = (unsigned char*)data;
2392     unsigned long int q_in;
2393     int i;
2394     int count = 0;
2395    
2396     if ((len % 4) != 0)
2397     return (0);
2398    
2399     out = MyMalloc(((len / 4) * 3) + 1);
2400    
2401     /* process 32 bits at a time */
2402     for( i = 0; (i + 3) < len; i+=4)
2403     {
2404     /* compress input (chars a, b, c and d) as follows:
2405     * (after converting ascii -> base64 value)
2406     *
2407     * |00000000aaaaaabbbbbbccccccdddddd|
2408     * | 765432 107654 321076 543210|
2409     */
2410    
2411     q_in = 0;
2412    
2413     if (base64_values[in[i+3]] > -1)
2414     q_in |= base64_values[in[i+3]] ;
2415     if (base64_values[in[i+2]] > -1)
2416     q_in |= base64_values[in[i+2]] << 6;
2417     if (base64_values[in[i+1]] > -1)
2418     q_in |= base64_values[in[i+1]] << 12;
2419     if (base64_values[in[i ]] > -1)
2420     q_in |= base64_values[in[i ]] << 18;
2421    
2422     out[count++] = (q_in >> 16) & 0xff;
2423     out[count++] = (q_in >> 8) & 0xff;
2424     out[count++] = (q_in ) & 0xff;
2425     }
2426    
2427     if (in[i-1] == '=') count--;
2428     if (in[i-2] == '=') count--;
2429    
2430     out[count] = '\0';
2431     *output = out;
2432     return (count);
2433     }
2434    
2435     #endif /* HAVE_LIBCRYPTO */
2436    

Properties

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