ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/s_serv.c
Revision: 101
Committed: Mon Oct 10 03:52:14 2005 UTC (20 years, 10 months ago) by db
Content type: text/x-csrc
File size: 68781 byte(s)
Log Message:
- First pass at s_conf.c cleanup for attach clients
- attach_conf() is gone replaced with attach_iline()
  Clients/servers have one single I line "attached", i.e. the confs
  list is gone.
- Add attach_leaf_hub()
  Add a leaf or hub mask to the given server, ->serv must exist
  since there can be a list of hub masks and leaf masks, this continues
  to be a dlink list
- Removed redundant find_conf_exact()
  This function can be replaced in all cases with find_exact_name_conf()
  notably because all find_conf_exact did differently was count whether
  an oper conf would cause an already connected client to exceed class limits.
- oper_up() now takes an extra conf pointer pointing to the found
  oper conf, the oper flags are set in the client->iline as necessary
  in oper_up()
- Cleaned up more prototypes that should have been moved from s_conf.h
  to parse_aline.h notably. find_kill() and find_gkill()
- m_stats.c needs more cleanup since memory in attached "confs" is now
  not counted. This should be replaced with iline memory count and
  hub/leaf confs memory count.
  

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

Properties

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