ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/s_serv.c
Revision: 69
Committed: Tue Oct 4 16:09:51 2005 UTC (20 years, 10 months ago) by adx
Content type: text/x-csrc
File size: 68864 byte(s)
Log Message:
- splitted ircd/libio, all headers connected with libio sources have been
  moved for internal use only. To use libio interface, include "libio.h"
  (which is already done in "stdinc.h")


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     static void send_tb(struct Client *client_p, struct Channel *chptr);
58    
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     attach_conf(client_p, server_conf);
635    
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     attach_conf(client_p, conf);
644     }
645    
646     DLINK_FOREACH(ptr, leaf_items.head)
647     {
648     conf = ptr->data;
649    
650     if (!match(name, conf->name))
651     continue;
652     attach_conf(client_p, conf);
653     }
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     ClearCap(client_p, CAP_TB);
667    
668     /* Don't unset CAP_HUB here even if the server isn't a hub,
669     * it only indicates if the server thinks it's lazylinks are
670     * leafs or not.. if you unset it, bad things will happen
671     */
672     if (aconf != NULL)
673     {
674     struct sockaddr_in *v4;
675     #ifdef IPV6
676     struct sockaddr_in6 *v6;
677     #endif
678     switch (aconf->aftype)
679     {
680     #ifdef IPV6
681     case AF_INET6:
682     v6 = (struct sockaddr_in6 *)&aconf->ipnum;
683    
684     if (IN6_IS_ADDR_UNSPECIFIED(&v6->sin6_addr))
685     memcpy(&aconf->ipnum, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
686     break;
687     #endif
688     case AF_INET:
689     v4 = (struct sockaddr_in *)&aconf->ipnum;
690    
691     if (v4->sin_addr.s_addr == INADDR_NONE)
692     memcpy(&aconf->ipnum, &client_p->localClient->ip, sizeof(struct irc_ssaddr));
693     break;
694     }
695     }
696    
697     return(0);
698     }
699    
700     /* add_capability()
701     *
702     * inputs - string name of CAPAB
703     * - int flag of capability
704     * output - NONE
705     * side effects - Adds given capability name and bit mask to
706     * current supported capabilities. This allows
707     * modules to dynamically add or subtract their capability.
708     */
709     void
710     add_capability(const char *capab_name, int cap_flag, int add_to_default)
711     {
712     struct Capability *cap;
713    
714     cap = (struct Capability *)MyMalloc(sizeof(*cap));
715     DupString(cap->name, capab_name);
716     cap->cap = cap_flag;
717     dlinkAdd(cap, &cap->node, &cap_list);
718     if (add_to_default)
719     default_server_capabs |= cap_flag;
720     }
721    
722     /* delete_capability()
723     *
724     * inputs - string name of CAPAB
725     * output - NONE
726     * side effects - delete given capability from ones known.
727     */
728     int
729     delete_capability(const char *capab_name)
730     {
731     dlink_node *ptr;
732     dlink_node *next_ptr;
733     struct Capability *cap;
734    
735     DLINK_FOREACH_SAFE(ptr, next_ptr, cap_list.head)
736     {
737     cap = ptr->data;
738    
739     if (cap->cap != 0)
740     {
741     if (irccmp(cap->name, capab_name) == 0)
742     {
743     default_server_capabs &= ~(cap->cap);
744     dlinkDelete(ptr, &cap_list);
745     MyFree(cap->name);
746     cap->name = NULL;
747     MyFree(cap);
748     }
749     }
750     }
751    
752     return(0);
753     }
754    
755     /*
756     * find_capability()
757     *
758     * inputs - string name of capab to find
759     * output - 0 if not found CAPAB otherwise
760     * side effects - none
761     */
762     int
763     find_capability(const char *capab)
764     {
765     dlink_node *ptr;
766     struct Capability *cap;
767    
768     DLINK_FOREACH(ptr, cap_list.head)
769     {
770     cap = ptr->data;
771    
772     if (cap->cap != 0)
773     {
774     if (irccmp(cap->name, capab) == 0)
775     return(cap->cap);
776     }
777     }
778     return(0);
779     }
780    
781     /* send_capabilities()
782     *
783     * inputs - Client pointer to send to
784     * - Pointer to AccessItem (for crypt)
785     * - int flag of capabilities that this server can send
786     * - int flag of encryption capabilities
787     * output - NONE
788     * side effects - send the CAPAB line to a server -orabidoo
789     *
790     */
791     void
792     send_capabilities(struct Client *client_p, struct AccessItem *aconf,
793     int cap_can_send, int enc_can_send)
794     {
795     struct Capability *cap=NULL;
796     char msgbuf[IRCD_BUFSIZE];
797     char *t;
798     int tl;
799     dlink_node *ptr;
800     #ifdef HAVE_LIBCRYPTO
801     struct EncCapability *epref;
802     char *capend;
803     int sent_cipher = 0;
804     #endif
805    
806     t = msgbuf;
807    
808     DLINK_FOREACH(ptr, cap_list.head)
809     {
810     cap = ptr->data;
811    
812     if (cap->cap & (cap_can_send|default_server_capabs))
813     {
814     tl = ircsprintf(t, "%s ", cap->name);
815     t += tl;
816     }
817     }
818     #ifdef HAVE_LIBCRYPTO
819     if (enc_can_send)
820     {
821     capend = t;
822     strcpy(t, "ENC:");
823     t += 4;
824    
825     /* use connect{} specific info if available */
826     if (aconf->cipher_preference)
827     epref = aconf->cipher_preference;
828     else
829     epref = ConfigFileEntry.default_cipher_preference;
830    
831     if ((epref->cap & enc_can_send))
832     {
833     /* Leave the space -- it is removed later. */
834     tl = ircsprintf(t, "%s ", epref->name);
835     t += tl;
836     sent_cipher = 1;
837     }
838    
839     if (!sent_cipher)
840     t = capend; /* truncate string before ENC:, below */
841     }
842     #endif
843     *(t-1) = '\0';
844     sendto_one(client_p, "CAPAB :%s", msgbuf);
845     }
846    
847     /* sendnick_TS()
848     *
849     * inputs - client (server) to send nick towards
850     * - client to send nick for
851     * output - NONE
852     * side effects - NICK message is sent towards given client_p
853     */
854     void
855     sendnick_TS(struct Client *client_p, struct Client *target_p)
856     {
857     static char ubuf[12];
858    
859     if (!IsClient(target_p))
860     return;
861    
862     send_umode(NULL, target_p, 0, IsOperHiddenAdmin(target_p) ?
863     SEND_UMODES & ~UMODE_ADMIN : SEND_UMODES, ubuf);
864    
865     if (ubuf[0] == '\0')
866     {
867     ubuf[0] = '+';
868     ubuf[1] = '\0';
869     }
870    
871     /* XXX Both of these need to have a :me.name or :mySID!?!?! */
872     if (HasID(target_p) && IsCapable(client_p, CAP_TS6))
873     sendto_one(client_p, ":%s UID %s %d %lu %s %s %s %s %s :%s",
874     target_p->servptr->id,
875     target_p->name, target_p->hopcount + 1,
876     (unsigned long) target_p->tsinfo,
877     ubuf, target_p->username, target_p->host,
878     ((MyClient(target_p)&&!IsIPSpoof(target_p))?target_p->sockhost:"0"),
879     target_p->id, target_p->info);
880     else
881     sendto_one(client_p, "NICK %s %d %lu %s %s %s %s :%s",
882     target_p->name, target_p->hopcount + 1,
883     (unsigned long) target_p->tsinfo,
884     ubuf, target_p->username, target_p->host,
885     target_p->servptr->name, target_p->info);
886     if (IsConfAwayBurst((struct AccessItem *)map_to_conf(client_p->serv->sconf)))
887     if (!EmptyString(target_p->away))
888     sendto_one(client_p, ":%s AWAY :%s", target_p->name,
889     target_p->away);
890    
891     }
892    
893     /* client_burst_if_needed()
894     *
895     * inputs - pointer to server
896     * - pointer to client to add
897     * output - NONE
898     * side effects - If this client is not known by this lazyleaf, send it
899     */
900     void
901     client_burst_if_needed(struct Client *client_p, struct Client *target_p)
902     {
903     if (!ServerInfo.hub)
904     return;
905     if (!MyConnect(client_p))
906     return;
907     if (!IsCapable(client_p,CAP_LL))
908     return;
909    
910     if ((target_p->lazyLinkClientExists & client_p->localClient->serverMask) == 0)
911     {
912     sendnick_TS(client_p, target_p);
913     add_lazylinkclient(client_p,target_p);
914     }
915     }
916    
917     /*
918     * show_capabilities - show current server capabilities
919     *
920     * inputs - pointer to a struct Client
921     * output - pointer to static string
922     * side effects - build up string representing capabilities of server listed
923     */
924     const char *
925     show_capabilities(struct Client *target_p)
926     {
927     static char msgbuf[IRCD_BUFSIZE];
928     char *t = msgbuf;
929     dlink_node *ptr;
930    
931     t += ircsprintf(msgbuf, "TS ");
932    
933     DLINK_FOREACH(ptr, cap_list.head)
934     {
935     const struct Capability *cap = ptr->data;
936    
937     if (IsCapable(target_p, cap->cap))
938     t += ircsprintf(t, "%s ", cap->name);
939     }
940     #ifdef HAVE_LIBCRYPTO
941     if (IsCapable(target_p, CAP_ENC) &&
942     target_p->localClient->in_cipher &&
943     target_p->localClient->out_cipher)
944     t += ircsprintf(t, "ENC:%s ",
945     target_p->localClient->in_cipher->name);
946     #endif
947     *(t - 1) = '\0';
948    
949     return(msgbuf);
950     }
951    
952     /* make_server()
953     *
954     * inputs - pointer to client struct
955     * output - pointer to struct Server
956     * side effects - add's an Server information block to a client
957     * if it was not previously allocated.
958     */
959     struct Server *
960     make_server(struct Client *client_p)
961     {
962     if (client_p->serv == NULL)
963     {
964     client_p->serv = MyMalloc(sizeof(struct Server));
965     client_p->serv->dep_servers = 1;
966     }
967    
968     return client_p->serv;
969     }
970    
971     /* server_estab()
972     *
973     * inputs - pointer to a struct Client
974     * output -
975     * side effects -
976     */
977     void
978     server_estab(struct Client *client_p)
979     {
980     struct Client *target_p;
981     struct ConfItem *conf;
982     struct AccessItem *aconf=NULL;
983     char *host;
984     const char *inpath;
985     static char inpath_ip[HOSTLEN * 2 + USERLEN + 6];
986     dlink_node *m;
987     dlink_node *ptr;
988    
989     assert(client_p != NULL);
990    
991     strlcpy(inpath_ip, get_client_name(client_p, SHOW_IP), sizeof(inpath_ip));
992    
993     inpath = get_client_name(client_p, MASK_IP); /* "refresh" inpath with host */
994     host = client_p->name;
995    
996     if ((conf = find_conf_name(&client_p->localClient->confs, host, SERVER_TYPE))
997     == NULL)
998     {
999     /* This shouldn't happen, better tell the ops... -A1kmm */
1000     sendto_realops_flags(UMODE_ALL, L_ALL, "Warning: Lost connect{} block "
1001     "for server %s(this shouldn't happen)!", host);
1002     exit_client(client_p, &me, "Lost connect{} block!");
1003     return;
1004     }
1005    
1006     MyFree(client_p->localClient->passwd);
1007     client_p->localClient->passwd = NULL;
1008    
1009     /* Its got identd, since its a server */
1010     SetGotId(client_p);
1011    
1012     /* If there is something in the serv_list, it might be this
1013     * connecting server..
1014     */
1015     if (!ServerInfo.hub && serv_list.head)
1016     {
1017     if (client_p != serv_list.head->data || serv_list.head->next)
1018     {
1019     ServerStats->is_ref++;
1020     sendto_one(client_p, "ERROR :I'm a leaf not a hub");
1021     exit_client(client_p, &me, "I'm a leaf");
1022     return;
1023     }
1024     }
1025    
1026     aconf = (struct AccessItem *)map_to_conf(conf);
1027    
1028     if (IsUnknown(client_p) && !IsConfCryptLink(aconf))
1029     {
1030     /* jdc -- 1. Use EmptyString(), not [0] index reference.
1031     * 2. Check aconf->spasswd, not aconf->passwd.
1032     */
1033     if (!EmptyString(aconf->spasswd))
1034     {
1035     /* only send ts6 format PASS if we have ts6 enabled */
1036     if (me.id[0] != '\0') /* Send TS 6 form only if id */
1037     sendto_one(client_p, "PASS %s TS %d %s",
1038     aconf->spasswd, TS_CURRENT, me.id);
1039     else
1040     sendto_one(client_p, "PASS %s TS 5",
1041     aconf->spasswd);
1042     }
1043    
1044     /* Pass my info to the new server
1045     *
1046     * If trying to negotiate LazyLinks, pass on CAP_LL
1047     * If this is a HUB, pass on CAP_HUB
1048     * Pass on ZIP if supported
1049     * Pass on TB if supported.
1050     * - Dianora
1051     */
1052    
1053     send_capabilities(client_p, aconf,
1054     (IsConfLazyLink(aconf) ? find_capability("LL") : 0)
1055     | (IsConfCompressed(aconf) ? find_capability("ZIP") : 0)
1056     | (IsConfTopicBurst(aconf) ? find_capability("TB") : 0)
1057     , 0);
1058    
1059     /* SERVER is the last command sent before switching to ziplinks.
1060     * We set TCPNODELAY on the socket to make sure it gets sent out
1061     * on the wire immediately. Otherwise, it could be sitting in
1062     * a kernel buffer when we start sending zipped data, and the
1063     * parser on the receiving side can't hand both unzipped and zipped
1064     * data in one packet. --Rodder
1065     *
1066     * currently we only need to call send_queued_write,
1067     * Nagle is already disabled at this point --adx
1068     */
1069     sendto_one(client_p, "SERVER %s 1 :%s%s",
1070     my_name_for_link(conf),
1071     ConfigServerHide.hidden ? "(H) " : "",
1072     (me.info[0]) ? (me.info) : "IRCers United");
1073     send_queued_write(client_p);
1074     }
1075    
1076     /* Hand the server off to servlink now */
1077     if (IsCapable(client_p, CAP_ENC) || IsCapable(client_p, CAP_ZIP))
1078     {
1079     if (fork_server(client_p) < 0)
1080     {
1081     sendto_realops_flags(UMODE_ALL, L_ADMIN,
1082     "Warning: fork failed for server %s -- check servlink_path (%s)",
1083     get_client_name(client_p, HIDE_IP), ConfigFileEntry.servlink_path);
1084     sendto_realops_flags(UMODE_ALL, L_OPER, "Warning: fork failed for server "
1085     "%s -- check servlink_path (%s)",
1086     get_client_name(client_p, MASK_IP),
1087     ConfigFileEntry.servlink_path);
1088     exit_client(client_p, &me, "fork failed");
1089     return;
1090     }
1091    
1092     start_io(client_p);
1093     SetServlink(client_p);
1094     }
1095    
1096     /* only send ts6 format SVINFO if we have ts6 enabled */
1097     sendto_one(client_p, "SVINFO %d %d 0 :%lu",
1098     (me.id[0] ? TS_CURRENT : 5), TS_MIN,
1099     (unsigned long)CurrentTime);
1100    
1101     /* assumption here is if they passed the correct TS version, they also passed an SID */
1102     if (IsCapable(client_p, CAP_TS6))
1103     hash_add_id(client_p);
1104    
1105     /* XXX Does this ever happen? I don't think so -db */
1106     detach_conf(client_p, OPER_TYPE);
1107    
1108     /* *WARNING*
1109     ** In the following code in place of plain server's
1110     ** name we send what is returned by get_client_name
1111     ** which may add the "sockhost" after the name. It's
1112     ** *very* *important* that there is a SPACE between
1113     ** the name and sockhost (if present). The receiving
1114     ** server will start the information field from this
1115     ** first blank and thus puts the sockhost into info.
1116     ** ...a bit tricky, but you have been warned, besides
1117     ** code is more neat this way... --msa
1118     */
1119     client_p->servptr = &me;
1120    
1121     if (IsClosing(client_p))
1122     return;
1123    
1124     SetServer(client_p);
1125    
1126     /* Update the capability combination usage counts. -A1kmm */
1127     set_chcap_usage_counts(client_p);
1128    
1129     /* Some day, all these lists will be consolidated *sigh* */
1130     dlinkAdd(client_p, &client_p->lnode, &me.serv->servers);
1131    
1132     m = dlinkFind(&unknown_list, client_p);
1133     assert(NULL != m);
1134    
1135     dlinkDelete(m, &unknown_list);
1136     dlinkAdd(client_p, m, &serv_list);
1137    
1138     Count.myserver++;
1139    
1140     dlinkAdd(client_p, make_dlink_node(), &global_serv_list);
1141     hash_add_client(client_p);
1142    
1143     /* doesnt duplicate client_p->serv if allocated this struct already */
1144     make_server(client_p);
1145    
1146     /* fixing eob timings.. -gnp */
1147     client_p->firsttime = CurrentTime;
1148    
1149     /* Show the real host/IP to admins */
1150     sendto_realops_flags(UMODE_ALL, L_ADMIN,
1151     "Link with %s established: (%s) link",
1152     inpath_ip,show_capabilities(client_p));
1153     /* Now show the masked hostname/IP to opers */
1154     sendto_realops_flags(UMODE_ALL, L_OPER,
1155     "Link with %s established: (%s) link",
1156     inpath,show_capabilities(client_p));
1157     ilog(L_NOTICE, "Link with %s established: (%s) link",
1158     inpath_ip, show_capabilities(client_p));
1159    
1160     client_p->serv->sconf = conf;
1161    
1162     if (HasServlink(client_p))
1163     {
1164     /* we won't overflow FD_DESC_SZ here, as it can hold
1165     * client_p->name + 64
1166     */
1167     fd_note(&client_p->localClient->fd, "slink data: %s", client_p->name);
1168     fd_note(&client_p->localClient->ctrlfd, "slink ctrl: %s", client_p->name);
1169     }
1170     else
1171     fd_note(&client_p->localClient->fd, "Server: %s", client_p->name);
1172    
1173     /* Old sendto_serv_but_one() call removed because we now
1174     ** need to send different names to different servers
1175     ** (domain name matching) Send new server to other servers.
1176     */
1177     DLINK_FOREACH(ptr, serv_list.head)
1178     {
1179     target_p = ptr->data;
1180    
1181     if (target_p == client_p)
1182     continue;
1183    
1184     if ((conf = target_p->serv->sconf) &&
1185     match(my_name_for_link(conf), client_p->name))
1186     continue;
1187    
1188     if (IsCapable(target_p, CAP_TS6) && HasID(client_p))
1189     sendto_one(target_p, ":%s SID %s 2 %s :%s%s",
1190     me.id, client_p->name, client_p->id,
1191     IsHidden(client_p) ? "(H) " : "",
1192     client_p->info);
1193     else
1194     sendto_one(target_p,":%s SERVER %s 2 :%s%s",
1195     me.name, client_p->name,
1196     IsHidden(client_p) ? "(H) " : "",
1197     client_p->info);
1198     }
1199    
1200     /* Pass on my client information to the new server
1201     **
1202     ** First, pass only servers (idea is that if the link gets
1203     ** cancelled beacause the server was already there,
1204     ** there are no NICK's to be cancelled...). Of course,
1205     ** if cancellation occurs, all this info is sent anyway,
1206     ** and I guess the link dies when a read is attempted...? --msa
1207     **
1208     ** Note: Link cancellation to occur at this point means
1209     ** that at least two servers from my fragment are building
1210     ** up connection this other fragment at the same time, it's
1211     ** a race condition, not the normal way of operation...
1212     **
1213     ** ALSO NOTE: using the get_client_name for server names--
1214     ** see previous *WARNING*!!! (Also, original inpath
1215     ** is destroyed...)
1216     */
1217    
1218     conf = client_p->serv->sconf;
1219    
1220     DLINK_FOREACH_PREV(ptr, global_serv_list.tail)
1221     {
1222     target_p = ptr->data;
1223    
1224     /* target_p->from == target_p for target_p == client_p */
1225     if (target_p->from == client_p)
1226     continue;
1227    
1228     if (match(my_name_for_link(conf), target_p->name))
1229     continue;
1230    
1231     if (IsCapable(client_p, CAP_TS6))
1232     {
1233     if (HasID(target_p))
1234     sendto_one(client_p, ":%s SID %s %d %s :%s%s",
1235     ID(target_p->servptr), target_p->name, target_p->hopcount+1,
1236     target_p->id, IsHidden(target_p) ? "(H) " : "",
1237     target_p->info);
1238     else /* introducing non-ts6 server */
1239     sendto_one(client_p, ":%s SERVER %s %d :%s%s",
1240     ID(target_p->servptr), target_p->name, target_p->hopcount+1,
1241     IsHidden(target_p) ? "(H) " : "", target_p->info);
1242     }
1243     else
1244     sendto_one(client_p, ":%s SERVER %s %d :%s%s",
1245     target_p->servptr->name, target_p->name, target_p->hopcount+1,
1246     IsHidden(target_p) ? "(H) " : "", target_p->info);
1247     }
1248    
1249     if ((ServerInfo.hub == 0) && MyConnect(client_p))
1250     uplink = client_p;
1251    
1252     server_burst(client_p);
1253     }
1254    
1255     static void
1256     start_io(struct Client *server)
1257     {
1258     struct LocalUser *lserver = server->localClient;
1259     int alloclen = 1;
1260     char *buf;
1261     dlink_node *ptr;
1262     struct dbuf_block *block;
1263    
1264     /* calculate how many bytes to allocate */
1265     if (IsCapable(server, CAP_ZIP))
1266     alloclen += 6;
1267     #ifdef HAVE_LIBCRYPTO
1268     if (IsCapable(server, CAP_ENC))
1269     alloclen += 16 + lserver->in_cipher->keylen + lserver->out_cipher->keylen;
1270     #endif
1271     alloclen += dbuf_length(&lserver->buf_recvq);
1272     alloclen += dlink_list_length(&lserver->buf_recvq.blocks) * 3;
1273     alloclen += dbuf_length(&lserver->buf_sendq);
1274     alloclen += dlink_list_length(&lserver->buf_sendq.blocks) * 3;
1275    
1276     /* initialize servlink control sendq */
1277     lserver->slinkq = buf = MyMalloc(alloclen);
1278     lserver->slinkq_ofs = 0;
1279     lserver->slinkq_len = alloclen;
1280    
1281     if (IsCapable(server, CAP_ZIP))
1282     {
1283     /* ziplink */
1284     *buf++ = SLINKCMD_SET_ZIP_OUT_LEVEL;
1285     *buf++ = 0; /* | */
1286     *buf++ = 1; /* \ len is 1 */
1287     *buf++ = ConfigFileEntry.compression_level;
1288     *buf++ = SLINKCMD_START_ZIP_IN;
1289     *buf++ = SLINKCMD_START_ZIP_OUT;
1290     }
1291     #ifdef HAVE_LIBCRYPTO
1292     if (IsCapable(server, CAP_ENC))
1293     {
1294     /* Decryption settings */
1295     *buf++ = SLINKCMD_SET_CRYPT_IN_CIPHER;
1296     *buf++ = 0; /* / (upper 8-bits of len) */
1297     *buf++ = 1; /* \ cipher id is 1 byte (lower 8-bits of len) */
1298     *buf++ = lserver->in_cipher->cipherid;
1299     *buf++ = SLINKCMD_SET_CRYPT_IN_KEY;
1300     *buf++ = 0; /* keylen < 256 */
1301     *buf++ = lserver->in_cipher->keylen;
1302     memcpy(buf, lserver->in_key, lserver->in_cipher->keylen);
1303     buf += lserver->in_cipher->keylen;
1304     /* Encryption settings */
1305     *buf++ = SLINKCMD_SET_CRYPT_OUT_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->out_cipher->cipherid;
1309     *buf++ = SLINKCMD_SET_CRYPT_OUT_KEY;
1310     *buf++ = 0; /* keylen < 256 */
1311     *buf++ = lserver->out_cipher->keylen;
1312     memcpy(buf, lserver->out_key, lserver->out_cipher->keylen);
1313     buf += lserver->out_cipher->keylen;
1314     *buf++ = SLINKCMD_START_CRYPT_IN;
1315     *buf++ = SLINKCMD_START_CRYPT_OUT;
1316     }
1317     #endif
1318    
1319     /* pass the whole recvq to servlink */
1320     DLINK_FOREACH (ptr, lserver->buf_recvq.blocks.head)
1321     {
1322     block = ptr->data;
1323     *buf++ = SLINKCMD_INJECT_RECVQ;
1324     *buf++ = (block->size >> 8);
1325     *buf++ = (block->size & 0xff);
1326     memcpy(buf, &block->data[0], block->size);
1327     buf += block->size;
1328     }
1329     dbuf_clear(&lserver->buf_recvq);
1330    
1331     /* pass the whole sendq to servlink */
1332     DLINK_FOREACH (ptr, lserver->buf_sendq.blocks.head)
1333     {
1334     block = ptr->data;
1335     *buf++ = SLINKCMD_INJECT_SENDQ;
1336     *buf++ = (block->size >> 8);
1337     *buf++ = (block->size & 0xff);
1338     memcpy(buf, &block->data[0], block->size);
1339     buf += block->size;
1340     }
1341     dbuf_clear(&lserver->buf_sendq);
1342    
1343     /* start io */
1344     *buf++ = SLINKCMD_INIT;
1345    
1346     /* schedule a write */
1347     send_queued_slink_write(server);
1348     }
1349    
1350     /* fork_server()
1351     *
1352     * inputs - struct Client *server
1353     * output - success: 0 / failure: -1
1354     * side effect - fork, and exec SERVLINK to handle this connection
1355     */
1356     static int
1357     fork_server(struct Client *server)
1358     {
1359     #ifndef HAVE_SOCKETPAIR
1360     return -1;
1361     #else
1362     int i;
1363     int slink_fds[2][2];
1364     /* 0? - ctrl | 1? - data
1365     * ?0 - child | ?1 - parent */
1366    
1367     if (socketpair(AF_UNIX, SOCK_STREAM, 0, slink_fds[0]) < 0)
1368     return -1;
1369     if (socketpair(AF_UNIX, SOCK_STREAM, 0, slink_fds[1]) < 0)
1370     goto free_ctrl_fds;
1371    
1372     if ((i = fork()) < 0)
1373     {
1374     close(slink_fds[1][0]); close(slink_fds[1][1]);
1375     free_ctrl_fds:
1376     close(slink_fds[0][0]); close(slink_fds[0][1]);
1377     return -1;
1378     }
1379    
1380     if (i == 0)
1381     {
1382     char fd_str[3][6]; /* store 3x sizeof("65535") */
1383     char *kid_argv[7];
1384    
1385     #ifdef O_ASYNC
1386     fcntl(server->localClient->fd.fd, F_SETFL,
1387     fcntl(server->localClient->fd.fd, F_GETFL, 0) & ~O_ASYNC);
1388     #endif
1389     close_fds(&server->localClient->fd);
1390     close(slink_fds[0][1]);
1391     close(slink_fds[1][1]);
1392    
1393     sprintf(fd_str[0], "%d", slink_fds[0][0]);
1394     sprintf(fd_str[1], "%d", slink_fds[1][0]);
1395     sprintf(fd_str[2], "%d", server->localClient->fd.fd);
1396    
1397     kid_argv[0] = "-slink";
1398     kid_argv[1] = kid_argv[2] = fd_str[0]; /* ctrl */
1399     kid_argv[3] = kid_argv[4] = fd_str[1]; /* data */
1400     kid_argv[5] = fd_str[2]; /* network */
1401     kid_argv[6] = NULL;
1402    
1403     execv(ConfigFileEntry.servlink_path, kid_argv);
1404    
1405     _exit(1);
1406     }
1407    
1408     /* close the network fd and the child ends of the pipes */
1409     fd_close(&server->localClient->fd);
1410     close(slink_fds[0][0]);
1411     close(slink_fds[1][0]);
1412    
1413     execute_callback(setup_socket_cb, slink_fds[0][1]);
1414     execute_callback(setup_socket_cb, slink_fds[1][1]);
1415    
1416     fd_open(&server->localClient->ctrlfd, slink_fds[0][1], 1, "slink ctrl");
1417     fd_open(&server->localClient->fd, slink_fds[1][1], 1, "slink data");
1418    
1419     read_ctrl_packet(&server->localClient->ctrlfd, server);
1420     read_packet(&server->localClient->fd, server);
1421    
1422     return 0;
1423     #endif
1424     }
1425    
1426     /* server_burst()
1427     *
1428     * inputs - struct Client pointer server
1429     * -
1430     * output - none
1431     * side effects - send a server burst
1432     * bugs - still too long
1433     */
1434     static void
1435     server_burst(struct Client *client_p)
1436     {
1437     /* Send it in the shortened format with the TS, if
1438     ** it's a TS server; walk the list of channels, sending
1439     ** all the nicks that haven't been sent yet for each
1440     ** channel, then send the channel itself -- it's less
1441     ** obvious than sending all nicks first, but on the
1442     ** receiving side memory will be allocated more nicely
1443     ** saving a few seconds in the handling of a split
1444     ** -orabidoo
1445     */
1446    
1447     /* On a "lazy link" hubs send nothing.
1448     * Leafs always have to send nicks plus channels
1449     */
1450     if (IsCapable(client_p, CAP_LL))
1451     {
1452     if (!ServerInfo.hub)
1453     {
1454     /* burst all our info */
1455     burst_all(client_p);
1456    
1457     /* Now, ask for channel info on all our current channels */
1458     cjoin_all(client_p);
1459     }
1460     }
1461     else
1462     {
1463     burst_all(client_p);
1464     }
1465    
1466     /* EOB stuff is now in burst_all */
1467     /* Always send a PING after connect burst is done */
1468     sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p));
1469     }
1470    
1471     /* burst_all()
1472     *
1473     * inputs - pointer to server to send burst to
1474     * output - NONE
1475     * side effects - complete burst of channels/nicks is sent to client_p
1476     */
1477     static void
1478     burst_all(struct Client *client_p)
1479     {
1480     struct Client *target_p;
1481     struct Channel *chptr;
1482     dlink_node *gptr;
1483     dlink_node *ptr;
1484    
1485     DLINK_FOREACH(gptr, global_channel_list.head)
1486     {
1487     chptr = gptr->data;
1488    
1489     if (dlink_list_length(&chptr->members) != 0)
1490     {
1491     burst_members(client_p, chptr);
1492     send_channel_modes(client_p, chptr);
1493     if (IsCapable(client_p, CAP_TB))
1494     send_tb(client_p, chptr);
1495     }
1496     }
1497    
1498     /* also send out those that are not on any channel
1499     */
1500     DLINK_FOREACH(ptr, global_client_list.head)
1501     {
1502     target_p = ptr->data;
1503    
1504     if (!IsBursted(target_p) && target_p->from != client_p)
1505     sendnick_TS(client_p, target_p);
1506    
1507     ClearBursted(target_p);
1508     }
1509    
1510     /* We send the time we started the burst, and let the remote host determine an EOB time,
1511     ** as otherwise we end up sending a EOB of 0 Sending here means it gets sent last -- fl
1512     */
1513     /* Its simpler to just send EOB and use the time its been connected.. --fl_ */
1514     if (IsCapable(client_p, CAP_EOB))
1515     sendto_one(client_p, ":%s EOB", ID_or_name(&me, client_p));
1516     }
1517    
1518     /*
1519     * send_tb
1520     *
1521     * inputs - pointer to Client
1522     * - pointer to channel
1523     * output - NONE
1524     * side effects - Called on a server burst when
1525     * server is CAP_TB capable
1526     */
1527     static void
1528     send_tb(struct Client *client_p, struct Channel *chptr)
1529     {
1530     if (chptr->topic != NULL && IsCapable(client_p, CAP_TB))
1531     {
1532     if (ConfigChannel.burst_topicwho)
1533     {
1534     sendto_one(client_p, ":%s TB %s %lu %s :%s",
1535     me.name, chptr->chname,
1536     (unsigned long)chptr->topic_time,
1537     chptr->topic_info, chptr->topic);
1538     }
1539     else
1540     {
1541     sendto_one(client_p, ":%s TB %s %lu :%s",
1542     me.name, chptr->chname,
1543     (unsigned long)chptr->topic_time, chptr->topic);
1544     }
1545     }
1546     }
1547    
1548     /* cjoin_all()
1549     *
1550     * inputs - server to ask for channel info from
1551     * output - NONE
1552     * side effects - CJOINS for all the leafs known channels is sent
1553     */
1554     static void
1555     cjoin_all(struct Client *client_p)
1556     {
1557     const dlink_node *gptr = NULL;
1558    
1559     DLINK_FOREACH(gptr, global_channel_list.head)
1560     {
1561     const struct Channel *chptr = gptr->data;
1562     sendto_one(client_p, ":%s CBURST %s",
1563     me.name, chptr->chname);
1564     }
1565     }
1566    
1567     /* burst_channel()
1568     *
1569     * inputs - pointer to server to send sjoins to
1570     * - channel pointer
1571     * output - none
1572     * side effects - All sjoins for channel(s) given by chptr are sent
1573     * for all channel members. ONLY called by hub on
1574     * behalf of a lazylink so client_p is always guarunteed
1575     * to be a LL leaf.
1576     */
1577     void
1578     burst_channel(struct Client *client_p, struct Channel *chptr)
1579     {
1580     burst_ll_members(client_p, chptr);
1581    
1582     send_channel_modes(client_p, chptr);
1583     add_lazylinkchannel(client_p,chptr);
1584    
1585     if (chptr->topic != NULL && chptr->topic_info != NULL)
1586     {
1587     sendto_one(client_p, ":%s TOPIC %s %s %lu :%s",
1588     me.name, chptr->chname, chptr->topic_info,
1589     (unsigned long)chptr->topic_time, chptr->topic);
1590     }
1591     }
1592    
1593     /* add_lazlinkchannel()
1594     *
1595     * inputs - pointer to directly connected leaf server
1596     * being introduced to this hub
1597     * - pointer to channel structure being introduced
1598     * output - NONE
1599     * side effects - The channel pointed to by chptr is now known
1600     * to be on lazyleaf server given by local_server_p.
1601     * mark that in the bit map and add to the list
1602     * of channels to examine after this newly introduced
1603     * server is squit off.
1604     */
1605     static void
1606     add_lazylinkchannel(struct Client *local_server_p, struct Channel *chptr)
1607     {
1608     assert(MyConnect(local_server_p));
1609    
1610     chptr->lazyLinkChannelExists |= local_server_p->localClient->serverMask;
1611     dlinkAdd(chptr, make_dlink_node(), &lazylink_channels);
1612     }
1613    
1614     /* add_lazylinkclient()
1615     *
1616     * inputs - pointer to directly connected leaf server
1617     * being introduced to this hub
1618     * - pointer to client being introduced
1619     * output - NONE
1620     * side effects - The client pointed to by client_p is now known
1621     * to be on lazyleaf server given by local_server_p.
1622     * mark that in the bit map and add to the list
1623     * of clients to examine after this newly introduced
1624     * server is squit off.
1625     */
1626     void
1627     add_lazylinkclient(struct Client *local_server_p, struct Client *client_p)
1628     {
1629     assert(MyConnect(local_server_p));
1630     client_p->lazyLinkClientExists |= local_server_p->localClient->serverMask;
1631     }
1632    
1633     /* remove_lazylink_flags()
1634     *
1635     * inputs - pointer to server quitting
1636     * output - NONE
1637     * side effects - All the channels on the lazylink channel list are examined
1638     * If they hold a bit corresponding to the servermask
1639     * attached to client_p, clear that bit. If this bitmask
1640     * goes to 0, then the channel is no longer known to
1641     * be on any lazylink server, and can be removed from the
1642     * link list.
1643     *
1644     * Similar is done for lazylink clients
1645     *
1646     * This function must be run by the HUB on any exiting
1647     * lazylink leaf server, while the pointer is still valid.
1648     * Hence must be run from client.c in exit_one_client()
1649     *
1650     * The old code scanned all channels, this code only
1651     * scans channels/clients on the lazylink_channels
1652     * lazylink_clients lists.
1653     */
1654     void
1655     remove_lazylink_flags(unsigned long mask)
1656     {
1657     dlink_node *ptr;
1658     dlink_node *next_ptr;
1659     struct Channel *chptr;
1660     struct Client *target_p;
1661     unsigned long clear_mask;
1662    
1663     if (!mask) /* On 0 mask, don't do anything */
1664     return;
1665    
1666     clear_mask = ~mask;
1667     freeMask |= mask;
1668    
1669     DLINK_FOREACH_SAFE(ptr, next_ptr, lazylink_channels.head)
1670     {
1671     chptr = ptr->data;
1672    
1673     chptr->lazyLinkChannelExists &= clear_mask;
1674    
1675     if (chptr->lazyLinkChannelExists == 0)
1676     {
1677     dlinkDelete(ptr, &lazylink_channels);
1678     free_dlink_node(ptr);
1679     }
1680     }
1681    
1682     DLINK_FOREACH(ptr, global_client_list.head)
1683     {
1684     target_p = ptr->data;
1685     target_p->lazyLinkClientExists &= clear_mask;
1686     }
1687     }
1688    
1689     /* burst_members()
1690     *
1691     * inputs - pointer to server to send members to
1692     * - dlink_list pointer to membership list to send
1693     * output - NONE
1694     * side effects -
1695     */
1696     static void
1697     burst_members(struct Client *client_p, struct Channel *chptr)
1698     {
1699     struct Client *target_p;
1700     struct Membership *ms;
1701     dlink_node *ptr;
1702    
1703     DLINK_FOREACH(ptr, chptr->members.head)
1704     {
1705     ms = ptr->data;
1706     target_p = ms->client_p;
1707    
1708     if (!IsBursted(target_p))
1709     {
1710     SetBursted(target_p);
1711    
1712     if (target_p->from != client_p)
1713     sendnick_TS(client_p, target_p);
1714     }
1715     }
1716     }
1717    
1718     /* burst_ll_members()
1719     *
1720     * inputs - pointer to server to send members to
1721     * - dlink_list pointer to membership list to send
1722     * output - NONE
1723     * side effects - This version also has to check the bitmap for lazylink
1724     */
1725     static void
1726     burst_ll_members(struct Client *client_p, struct Channel *chptr)
1727     {
1728     struct Client *target_p;
1729     struct Membership *ms;
1730     dlink_node *ptr;
1731    
1732     DLINK_FOREACH(ptr, chptr->members.head)
1733     {
1734     ms = ptr->data;
1735     target_p = ms->client_p;
1736    
1737     if ((target_p->lazyLinkClientExists & client_p->localClient->serverMask) == 0)
1738     {
1739     if (target_p->from != client_p)
1740     {
1741     add_lazylinkclient(client_p,target_p);
1742     sendnick_TS(client_p, target_p);
1743     }
1744     }
1745     }
1746     }
1747    
1748     /* set_autoconn()
1749     *
1750     * inputs - struct Client pointer to oper requesting change
1751     * output - none
1752     * side effects - set autoconnect mode
1753     */
1754     void
1755     set_autoconn(struct Client *source_p, const char *name, int newval)
1756     {
1757     struct ConfItem *conf;
1758     struct AccessItem *aconf;
1759    
1760     if (name != NULL)
1761     {
1762     conf = find_exact_name_conf(SERVER_TYPE, name, NULL, NULL);
1763     if (conf != NULL)
1764     {
1765     aconf = (struct AccessItem *)map_to_conf(conf);
1766     if (newval)
1767     SetConfAllowAutoConn(aconf);
1768     else
1769     ClearConfAllowAutoConn(aconf);
1770    
1771     sendto_realops_flags(UMODE_ALL, L_ALL,
1772     "%s has changed AUTOCONN for %s to %i",
1773     source_p->name, name, newval);
1774     sendto_one(source_p,
1775     ":%s NOTICE %s :AUTOCONN for %s is now set to %i",
1776     me.name, source_p->name, name, newval);
1777     }
1778     else
1779     {
1780     sendto_one(source_p, ":%s NOTICE %s :Can't find %s",
1781     me.name, source_p->name, name);
1782     }
1783     }
1784     else
1785     {
1786     sendto_one(source_p, ":%s NOTICE %s :Please specify a server name!",
1787     me.name, source_p->name);
1788     }
1789     }
1790    
1791     void
1792     initServerMask(void)
1793     {
1794     freeMask = 0xFFFFFFFFUL;
1795     }
1796    
1797     /* nextFreeMask()
1798     *
1799     * inputs - NONE
1800     * output - unsigned long next unused mask for use in LL
1801     * side effects -
1802     */
1803     unsigned long
1804     nextFreeMask(void)
1805     {
1806     int i;
1807     unsigned long mask = 1;
1808    
1809     for (i = 0; i < 32; i++)
1810     {
1811     if (mask & freeMask)
1812     {
1813     freeMask &= ~mask;
1814     return(mask);
1815     }
1816    
1817     mask <<= 1;
1818     }
1819    
1820     return(0L); /* watch this special case ... */
1821     }
1822    
1823     /* New server connection code
1824     * Based upon the stuff floating about in s_bsd.c
1825     * -- adrian
1826     */
1827    
1828     /* serv_connect() - initiate a server connection
1829     *
1830     * inputs - pointer to conf
1831     * - pointer to client doing the connect
1832     * output -
1833     * side effects -
1834     *
1835     * This code initiates a connection to a server. It first checks to make
1836     * sure the given server exists. If this is the case, it creates a socket,
1837     * creates a client, saves the socket information in the client, and
1838     * initiates a connection to the server through comm_connect_tcp(). The
1839     * completion of this goes through serv_completed_connection().
1840     *
1841     * We return 1 if the connection is attempted, since we don't know whether
1842     * it suceeded or not, and 0 if it fails in here somewhere.
1843     */
1844     int
1845     serv_connect(struct AccessItem *aconf, struct Client *by)
1846     {
1847     struct ConfItem *conf;
1848     struct Client *client_p;
1849     char buf[HOSTIPLEN];
1850    
1851     /* conversion structs */
1852     struct sockaddr_in *v4;
1853     /* Make sure aconf is useful */
1854     assert(aconf != NULL);
1855    
1856     if(aconf == NULL)
1857     return (0);
1858    
1859     /* XXX should be passing struct ConfItem in the first place */
1860     conf = unmap_conf_item(aconf);
1861    
1862     /* log */
1863     irc_getnameinfo((struct sockaddr*)&aconf->ipnum, aconf->ipnum.ss_len,
1864     buf, HOSTIPLEN, NULL, 0, NI_NUMERICHOST);
1865     ilog(L_NOTICE, "Connect to %s[%s] @%s", aconf->user, aconf->host,
1866     buf);
1867    
1868     /* Still processing a DNS lookup? -> exit */
1869     if (aconf->dns_query != NULL)
1870     {
1871     sendto_realops_flags(UMODE_ALL, L_OPER,
1872     "Error connecting to %s: Error during DNS lookup", conf->name);
1873     return (0);
1874     }
1875    
1876     /* Make sure this server isn't already connected
1877     * Note: aconf should ALWAYS be a valid C: line
1878     */
1879     if ((client_p = find_server(conf->name)) != NULL)
1880     {
1881     sendto_realops_flags(UMODE_ALL, L_ADMIN,
1882     "Server %s already present from %s",
1883     conf->name, get_client_name(client_p, SHOW_IP));
1884     sendto_realops_flags(UMODE_ALL, L_OPER,
1885     "Server %s already present from %s",
1886     conf->name, get_client_name(client_p, MASK_IP));
1887     if (by && IsClient(by) && !MyClient(by))
1888     sendto_one(by, ":%s NOTICE %s :Server %s already present from %s",
1889     me.name, by->name, conf->name,
1890     get_client_name(client_p, MASK_IP));
1891     return (0);
1892     }
1893    
1894     /* Create a local client */
1895     client_p = make_client(NULL);
1896    
1897     /* Copy in the server, hostname, fd */
1898     strlcpy(client_p->name, conf->name, sizeof(client_p->name));
1899     strlcpy(client_p->host, aconf->host, sizeof(client_p->host));
1900    
1901     /* We already converted the ip once, so lets use it - stu */
1902     strlcpy(client_p->sockhost, buf, HOSTIPLEN);
1903    
1904     /* create a socket for the server connection */
1905     if (comm_open(&client_p->localClient->fd, aconf->ipnum.ss.ss_family,
1906     SOCK_STREAM, 0, NULL) < 0)
1907     {
1908     /* Eek, failure to create the socket */
1909     report_error(L_ALL,
1910     "opening stream socket to %s: %s", conf->name, errno);
1911     SetDead(client_p);
1912     exit_client(client_p, &me, "Connection failed");
1913     return (0);
1914     }
1915    
1916     /* servernames are always guaranteed under HOSTLEN chars */
1917     fd_note(&client_p->localClient->fd, "Server: %s", conf->name);
1918    
1919     /* Attach config entries to client here rather than in
1920     * serv_connect_callback(). This to avoid null pointer references.
1921     */
1922     if (!attach_connect_block(client_p, conf->name, aconf->host))
1923     {
1924     sendto_realops_flags(UMODE_ALL, L_ALL,
1925     "Host %s is not enabled for connecting:no C/N-line",
1926     conf->name);
1927     if (by && IsClient(by) && !MyClient(by))
1928     sendto_one(by, ":%s NOTICE %s :Connect to host %s failed.",
1929     me.name, by->name, client_p->name);
1930     SetDead(client_p);
1931     exit_client(client_p, client_p, "Connection failed");
1932     return (0);
1933     }
1934    
1935     /* at this point we have a connection in progress and C/N lines
1936     * attached to the client, the socket info should be saved in the
1937     * client and it should either be resolved or have a valid address.
1938     *
1939     * The socket has been connected or connect is in progress.
1940     */
1941     make_server(client_p);
1942    
1943     if (by && IsClient(by))
1944     strlcpy(client_p->serv->by, by->name, sizeof(client_p->serv->by));
1945     else
1946     strlcpy(client_p->serv->by, "AutoConn.", sizeof(client_p->serv->by));
1947    
1948     SetConnecting(client_p);
1949     dlinkAdd(client_p, &client_p->node, &global_client_list);
1950     /* from def_fam */
1951     client_p->localClient->aftype = aconf->aftype;
1952    
1953     /* Now, initiate the connection */
1954     /* XXX assume that a non 0 type means a specific bind address
1955     * for this connect.
1956     */
1957     switch (aconf->aftype)
1958     {
1959     case AF_INET:
1960     v4 = (struct sockaddr_in*)&aconf->my_ipnum;
1961     if (v4->sin_addr.s_addr != 0)
1962     {
1963     struct irc_ssaddr ipn;
1964     memset(&ipn, 0, sizeof(struct irc_ssaddr));
1965     ipn.ss.ss_family = AF_INET;
1966     ipn.ss_port = 0;
1967     memcpy(&ipn, &aconf->my_ipnum, sizeof(struct irc_ssaddr));
1968     comm_connect_tcp(&client_p->localClient->fd, aconf->host, aconf->port,
1969     (struct sockaddr *)&ipn, ipn.ss_len,
1970     serv_connect_callback, client_p, aconf->aftype,
1971     CONNECTTIMEOUT);
1972     }
1973     else if (ServerInfo.specific_ipv4_vhost)
1974     {
1975     struct irc_ssaddr ipn;
1976     memset(&ipn, 0, sizeof(struct irc_ssaddr));
1977     ipn.ss.ss_family = AF_INET;
1978     ipn.ss_port = 0;
1979     memcpy(&ipn, &ServerInfo.ip, sizeof(struct irc_ssaddr));
1980     comm_connect_tcp(&client_p->localClient->fd, aconf->host, aconf->port,
1981     (struct sockaddr *)&ipn, ipn.ss_len,
1982     serv_connect_callback, client_p, aconf->aftype,
1983     CONNECTTIMEOUT);
1984     }
1985     else
1986     comm_connect_tcp(&client_p->localClient->fd, aconf->host, aconf->port,
1987     NULL, 0, serv_connect_callback, client_p, aconf->aftype,
1988     CONNECTTIMEOUT);
1989     break;
1990     #ifdef IPV6
1991     case AF_INET6:
1992     {
1993     struct irc_ssaddr ipn;
1994     struct sockaddr_in6 *v6;
1995     struct sockaddr_in6 *v6conf;
1996    
1997     memset(&ipn, 0, sizeof(struct irc_ssaddr));
1998     v6conf = (struct sockaddr_in6 *)&aconf->my_ipnum;
1999     v6 = (struct sockaddr_in6 *)&ipn;
2000    
2001     if (memcmp(&v6conf->sin6_addr, &v6->sin6_addr,
2002     sizeof(struct in6_addr)) != 0)
2003     {
2004     memcpy(&ipn, &aconf->my_ipnum, sizeof(struct irc_ssaddr));
2005     ipn.ss.ss_family = AF_INET6;
2006     ipn.ss_port = 0;
2007     comm_connect_tcp(&client_p->localClient->fd,
2008     aconf->host, aconf->port,
2009     (struct sockaddr *)&ipn, ipn.ss_len,
2010     serv_connect_callback, client_p,
2011     aconf->aftype, CONNECTTIMEOUT);
2012     }
2013     else if (ServerInfo.specific_ipv6_vhost)
2014     {
2015     memcpy(&ipn, &ServerInfo.ip6, sizeof(struct irc_ssaddr));
2016     ipn.ss.ss_family = AF_INET6;
2017     ipn.ss_port = 0;
2018     comm_connect_tcp(&client_p->localClient->fd,
2019     aconf->host, aconf->port,
2020     (struct sockaddr *)&ipn, ipn.ss_len,
2021     serv_connect_callback, client_p,
2022     aconf->aftype, CONNECTTIMEOUT);
2023     }
2024     else
2025     comm_connect_tcp(&client_p->localClient->fd,
2026     aconf->host, aconf->port,
2027     NULL, 0, serv_connect_callback, client_p,
2028     aconf->aftype, CONNECTTIMEOUT);
2029     }
2030     #endif
2031     }
2032     return (1);
2033     }
2034    
2035     /* serv_connect_callback() - complete a server connection.
2036     *
2037     * This routine is called after the server connection attempt has
2038     * completed. If unsucessful, an error is sent to ops and the client
2039     * is closed. If sucessful, it goes through the initialisation/check
2040     * procedures, the capabilities are sent, and the socket is then
2041     * marked for reading.
2042     */
2043     static void
2044     serv_connect_callback(fde_t *fd, int status, void *data)
2045     {
2046     struct Client *client_p = data;
2047     struct ConfItem *conf=NULL;
2048     struct AccessItem *aconf=NULL;
2049    
2050     /* First, make sure its a real client! */
2051     assert(client_p != NULL);
2052     assert(&client_p->localClient->fd == fd);
2053    
2054     /* Next, for backward purposes, record the ip of the server */
2055     memcpy(&client_p->localClient->ip, &fd->connect.hostaddr,
2056     sizeof(struct irc_ssaddr));
2057     /* Check the status */
2058     if (status != COMM_OK)
2059     {
2060     /* We have an error, so report it and quit
2061     * Admins get to see any IP, mere opers don't *sigh*
2062     */
2063     if (ConfigServerHide.hide_server_ips)
2064     sendto_realops_flags(UMODE_ALL, L_ADMIN,
2065     "Error connecting to %s: %s",
2066     client_p->name, comm_errstr(status));
2067     else
2068     sendto_realops_flags(UMODE_ALL, L_ADMIN,
2069     "Error connecting to %s[%s]: %s", client_p->name,
2070     client_p->host, comm_errstr(status));
2071    
2072     sendto_realops_flags(UMODE_ALL, L_OPER,
2073     "Error connecting to %s: %s",
2074     client_p->name, comm_errstr(status));
2075    
2076     /* If a fd goes bad, call dead_link() the socket is no
2077     * longer valid for reading or writing.
2078     */
2079     dead_link_on_write(client_p, 0);
2080     return;
2081     }
2082    
2083     /* COMM_OK, so continue the connection procedure */
2084     /* Get the C/N lines */
2085     conf = find_conf_name(&client_p->localClient->confs,
2086     client_p->name, SERVER_TYPE);
2087     if (conf == NULL)
2088     {
2089     sendto_realops_flags(UMODE_ALL, L_ADMIN,
2090     "Lost connect{} block for %s", get_client_name(client_p, HIDE_IP));
2091     sendto_realops_flags(UMODE_ALL, L_OPER,
2092     "Lost connect{} block for %s", get_client_name(client_p, MASK_IP));
2093    
2094     exit_client(client_p, &me, "Lost connect{} block");
2095     return;
2096     }
2097    
2098     aconf = (struct AccessItem *)map_to_conf(conf);
2099     /* Next, send the initial handshake */
2100     SetHandshake(client_p);
2101    
2102     #ifdef HAVE_LIBCRYPTO
2103     /* Handle all CRYPTLINK links in cryptlink_init */
2104     if (IsConfCryptLink(aconf))
2105     {
2106     cryptlink_init(client_p, conf, fd);
2107     return;
2108     }
2109     #endif
2110    
2111     /* jdc -- Check and send spasswd, not passwd. */
2112     if (!EmptyString(aconf->spasswd) && (me.id[0] != '\0'))
2113     /* Send TS 6 form only if id */
2114     sendto_one(client_p, "PASS %s TS %d %s",
2115     aconf->spasswd, TS_CURRENT, me.id);
2116     else
2117     sendto_one(client_p, "PASS %s TS 5",
2118     aconf->spasswd);
2119    
2120     /* Pass my info to the new server
2121     *
2122     * If trying to negotiate LazyLinks, pass on CAP_LL
2123     * If this is a HUB, pass on CAP_HUB
2124     * Pass on ZIP if supported
2125     * Pass on TB if supported.
2126     * - Dianora
2127     */
2128     send_capabilities(client_p, aconf,
2129     (IsConfLazyLink(aconf) ? find_capability("LL") : 0)
2130     | (IsConfCompressed(aconf) ? find_capability("ZIP") : 0)
2131     | (IsConfTopicBurst(aconf) ? find_capability("TB") : 0)
2132     , 0);
2133    
2134     sendto_one(client_p, "SERVER %s 1 :%s%s",
2135     my_name_for_link(conf),
2136     ConfigServerHide.hidden ? "(H) " : "",
2137     me.info);
2138    
2139     /* If we've been marked dead because a send failed, just exit
2140     * here now and save everyone the trouble of us ever existing.
2141     */
2142     if (IsDead(client_p))
2143     {
2144     sendto_realops_flags(UMODE_ALL, L_ADMIN,
2145     "%s[%s] went dead during handshake",
2146     client_p->name,
2147     client_p->host);
2148     sendto_realops_flags(UMODE_ALL, L_OPER,
2149     "%s went dead during handshake", client_p->name);
2150     return;
2151     }
2152    
2153     /* don't move to serv_list yet -- we haven't sent a burst! */
2154     /* If we get here, we're ok, so lets start reading some data */
2155     comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0);
2156     }
2157    
2158     struct Client *
2159     find_servconn_in_progress(const char *name)
2160     {
2161     dlink_node *ptr;
2162     struct Client *cptr;
2163    
2164     DLINK_FOREACH(ptr, unknown_list.head)
2165     {
2166     cptr = ptr->data;
2167    
2168     if (cptr && cptr->name[0])
2169     if (match(cptr->name, name) || match(name, cptr->name))
2170     return cptr;
2171     }
2172    
2173     return NULL;
2174     }
2175    
2176     #ifdef HAVE_LIBCRYPTO
2177     /*
2178     * sends a CRYPTLINK SERV command.
2179     */
2180     void
2181     cryptlink_init(struct Client *client_p, struct ConfItem *conf, fde_t *fd)
2182     {
2183     struct AccessItem *aconf;
2184     char *encrypted;
2185     unsigned char *key_to_send;
2186     char randkey[CIPHERKEYLEN];
2187     int enc_len;
2188    
2189     /* get key */
2190     if ((!ServerInfo.rsa_private_key) ||
2191     (!RSA_check_key(ServerInfo.rsa_private_key)) )
2192     {
2193     cryptlink_error(client_p, "SERV", "Invalid RSA private key",
2194     "Invalid RSA private key");
2195     return;
2196     }
2197    
2198     aconf = (struct AccessItem *)map_to_conf(conf);
2199    
2200     if (aconf->rsa_public_key == NULL)
2201     {
2202     cryptlink_error(client_p, "SERV", "Invalid RSA public key",
2203     "Invalid RSA public key");
2204     return;
2205     }
2206    
2207     if (get_randomness((unsigned char *)randkey, CIPHERKEYLEN) != 1)
2208     {
2209     cryptlink_error(client_p, "SERV", "Couldn't generate keyphrase",
2210     "Couldn't generate keyphrase");
2211     return;
2212     }
2213    
2214     encrypted = MyMalloc(RSA_size(ServerInfo.rsa_private_key));
2215     enc_len = RSA_public_encrypt(CIPHERKEYLEN,
2216     (unsigned char *)randkey,
2217     (unsigned char *)encrypted,
2218     aconf->rsa_public_key,
2219     RSA_PKCS1_PADDING);
2220    
2221     memcpy(client_p->localClient->in_key, randkey, CIPHERKEYLEN);
2222    
2223     if (enc_len <= 0)
2224     {
2225     report_crypto_errors();
2226     MyFree(encrypted);
2227     cryptlink_error(client_p, "SERV", "Couldn't encrypt data",
2228     "Couldn't encrypt data");
2229     return;
2230     }
2231    
2232     if (!(base64_block(&key_to_send, encrypted, enc_len)))
2233     {
2234     MyFree(encrypted);
2235     cryptlink_error(client_p, "SERV", "Couldn't base64 encode key",
2236     "Couldn't base64 encode key");
2237     return;
2238     }
2239    
2240     send_capabilities(client_p, aconf,
2241     (IsConfLazyLink(aconf) ? find_capability("LL") : 0)
2242     | (IsConfCompressed(aconf) ? find_capability("ZIP") : 0)
2243     | (IsConfTopicBurst(aconf) ? find_capability("TB") : 0)
2244     , CAP_ENC_MASK);
2245    
2246 adx 41 if (me.id[0])
2247     sendto_one(client_p, "PASS . TS %d %s", TS_CURRENT, me.id);
2248    
2249 adx 30 sendto_one(client_p, "CRYPTLINK SERV %s %s :%s%s",
2250     my_name_for_link(conf), key_to_send,
2251     ConfigServerHide.hidden ? "(H) " : "", me.info);
2252    
2253     SetHandshake(client_p);
2254     SetWaitAuth(client_p);
2255    
2256     MyFree(encrypted);
2257     MyFree(key_to_send);
2258    
2259     if (IsDead(client_p))
2260     cryptlink_error(client_p, "SERV", "Went dead during handshake",
2261     "Went dead during handshake");
2262     else if (fd != NULL)
2263     /* If we get here, we're ok, so lets start reading some data */
2264     comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0);
2265     }
2266    
2267     void
2268     cryptlink_error(struct Client *client_p, const char *type,
2269     const char *reason, const char *client_reason)
2270     {
2271     sendto_realops_flags(UMODE_ALL, L_ADMIN, "%s: CRYPTLINK %s error - %s",
2272     get_client_name(client_p, SHOW_IP), type, reason);
2273     sendto_realops_flags(UMODE_ALL, L_OPER, "%s: CRYPTLINK %s error - %s",
2274     get_client_name(client_p, MASK_IP), type, reason);
2275     ilog(L_ERROR, "%s: CRYPTLINK %s error - %s",
2276     get_client_name(client_p, SHOW_IP), type, reason);
2277    
2278     /* If client_reason isn't NULL, then exit the client with the message
2279     * defined in the call.
2280     */
2281     if ((client_reason != NULL) && (!IsDead(client_p)))
2282     exit_client(client_p, &me, client_reason);
2283     }
2284    
2285     static char base64_chars[] =
2286     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
2287    
2288     static char base64_values[] =
2289     {
2290     /* 00-15 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2291     /* 16-31 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2292     /* 32-47 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
2293     /* 48-63 */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1,
2294     /* 64-79 */ -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
2295     /* 80-95 */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
2296     /* 96-111 */ -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
2297     /* 112-127 */ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
2298     /* 128-143 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2299     /* 144-159 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2300     /* 160-175 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2301     /* 186-191 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2302     /* 192-207 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2303     /* 208-223 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2304     /* 224-239 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2305     /* 240-255 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
2306     };
2307    
2308     /*
2309     * base64_block will allocate and return a new block of memory
2310     * using MyMalloc(). It should be freed after use.
2311     */
2312     int
2313     base64_block(unsigned char **output, char *data, int len)
2314     {
2315     unsigned char *out;
2316     unsigned char *in = (unsigned char*)data;
2317     unsigned long int q_in;
2318     int i;
2319     int count = 0;
2320    
2321     out = MyMalloc(((((len + 2) - ((len + 2) % 3)) / 3) * 4) + 1);
2322    
2323     /* process 24 bits at a time */
2324     for( i = 0; i < len; i += 3)
2325     {
2326     q_in = 0;
2327    
2328     if ( i + 2 < len )
2329     {
2330     q_in = (in[i+2] & 0xc0) << 2;
2331     q_in |= in[i+2];
2332     }
2333    
2334     if ( i + 1 < len )
2335     {
2336     q_in |= (in[i+1] & 0x0f) << 10;
2337     q_in |= (in[i+1] & 0xf0) << 12;
2338     }
2339    
2340     q_in |= (in[i] & 0x03) << 20;
2341     q_in |= in[i] << 22;
2342    
2343     q_in &= 0x3f3f3f3f;
2344    
2345     out[count++] = base64_chars[((q_in >> 24) )];
2346     out[count++] = base64_chars[((q_in >> 16) & 0xff)];
2347     out[count++] = base64_chars[((q_in >> 8) & 0xff)];
2348     out[count++] = base64_chars[((q_in ) & 0xff)];
2349     }
2350     if ( (i - len) > 0 )
2351     {
2352     out[count-1] = '=';
2353     if ( (i - len) > 1 )
2354     out[count-2] = '=';
2355     }
2356    
2357     out[count] = '\0';
2358     *output = out;
2359     return (count);
2360     }
2361    
2362     /*
2363     * unbase64_block will allocate and return a new block of memory
2364     * using MyMalloc(). It should be freed after use.
2365     */
2366     int
2367     unbase64_block(unsigned char **output, char *data, int len)
2368     {
2369     unsigned char *out;
2370     unsigned char *in = (unsigned char*)data;
2371     unsigned long int q_in;
2372     int i;
2373     int count = 0;
2374    
2375     if ((len % 4) != 0)
2376     return (0);
2377    
2378     out = MyMalloc(((len / 4) * 3) + 1);
2379    
2380     /* process 32 bits at a time */
2381     for( i = 0; (i + 3) < len; i+=4)
2382     {
2383     /* compress input (chars a, b, c and d) as follows:
2384     * (after converting ascii -> base64 value)
2385     *
2386     * |00000000aaaaaabbbbbbccccccdddddd|
2387     * | 765432 107654 321076 543210|
2388     */
2389    
2390     q_in = 0;
2391    
2392     if (base64_values[in[i+3]] > -1)
2393     q_in |= base64_values[in[i+3]] ;
2394     if (base64_values[in[i+2]] > -1)
2395     q_in |= base64_values[in[i+2]] << 6;
2396     if (base64_values[in[i+1]] > -1)
2397     q_in |= base64_values[in[i+1]] << 12;
2398     if (base64_values[in[i ]] > -1)
2399     q_in |= base64_values[in[i ]] << 18;
2400    
2401     out[count++] = (q_in >> 16) & 0xff;
2402     out[count++] = (q_in >> 8) & 0xff;
2403     out[count++] = (q_in ) & 0xff;
2404     }
2405    
2406     if (in[i-1] == '=') count--;
2407     if (in[i-2] == '=') count--;
2408    
2409     out[count] = '\0';
2410     *output = out;
2411     return (count);
2412     }
2413    
2414     #endif /* HAVE_LIBCRYPTO */
2415    

Properties

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