ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/s_serv.c
Revision: 122
Committed: Thu Oct 13 10:57:26 2005 UTC (20 years, 9 months ago) by adx
Content type: text/x-csrc
File size: 69487 byte(s)
Log Message:
- it is probably more clean to do it this way..

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

Properties

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