Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / s_conf.c
1 /*
2  * IRC - Internet Relay Chat, ircd/s_conf.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 1, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * $Id$
21  */
22 #include "s_conf.h"
23
24 #include "IPcheck.h"
25 #include "class.h"
26 #include "client.h"
27 #include "crule.h"
28 #include "fileio.h"
29 #include "gline.h"
30 #include "hash.h"
31 #include "ircd.h"
32 #include "ircd_alloc.h"
33 #include "ircd_chattr.h"
34 #include "ircd_log.h"
35 #include "ircd_reply.h"
36 #include "ircd_snprintf.h"
37 #include "ircd_string.h"
38 #include "list.h"
39 #include "listener.h"
40 #include "match.h"
41 #include "motd.h"
42 #include "numeric.h"
43 #include "numnicks.h"
44 #include "opercmds.h"
45 #include "parse.h"
46 #include "res.h"
47 #include "s_bsd.h"
48 #include "s_debug.h"
49 #include "s_misc.h"
50 #include "send.h"
51 #include "sprintf_irc.h"
52 #include "struct.h"
53 #include "support.h"
54 #include "sys.h"
55
56 #include <assert.h>
57 #include <arpa/inet.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #include <netdb.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <sys/stat.h>
65 #include <unistd.h>
66
67 #ifndef INADDR_NONE
68 #define INADDR_NONE 0xffffffff
69 #endif
70
71 struct ConfItem* GlobalConfList  = 0;
72 int              GlobalConfCount = 0;
73
74 static struct LocalConf   localConf;
75 static struct CRuleConf*  cruleConfList;
76 static struct ServerConf* serverConfList;
77 static struct DenyConf*   denyConfList;
78
79 /*
80  * output the reason for being k lined from a file  - Mmmm
81  * sptr is client being dumped
82  * filename is the file that is to be output to the K lined client
83  */
84 static void killcomment(struct Client* sptr, const char* filename)
85 {
86   FBFILE*     file = 0;
87   char        line[80];
88   struct stat sb;
89   struct tm*  tm;
90
91   if (NULL == (file = fbopen(filename, "r"))) {
92     send_reply(sptr, ERR_NOMOTD);
93     send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
94                ":Connection from your host is refused on this server.");
95     return;
96   }
97   fbstat(&sb, file);
98   tm = localtime((time_t*) &sb.st_mtime);        /* NetBSD needs cast */
99   while (fbgets(line, sizeof(line) - 1, file)) {
100     char* end = line + strlen(line);
101     while (end > line) {
102       --end;
103       if ('\n' == *end || '\r' == *end)
104         *end = '\0';
105       else
106         break;
107     }
108     send_reply(sptr, RPL_MOTD, line);
109   }
110   send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
111              ":Connection from your host is refused on this server.");
112   fbclose(file);
113 }
114
115 struct ConfItem* make_conf(void)
116 {
117   struct ConfItem* aconf;
118
119   aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
120   assert(0 != aconf);
121 #ifdef        DEBUGMODE
122   ++GlobalConfCount;
123 #endif
124   memset(aconf, 0, sizeof(struct ConfItem));
125   aconf->status       = CONF_ILLEGAL;
126   aconf->ipnum.s_addr = INADDR_NONE;
127   return aconf;
128 }
129
130 void delist_conf(struct ConfItem *aconf)
131 {
132   if (aconf == GlobalConfList)
133     GlobalConfList = GlobalConfList->next;
134   else {
135     struct ConfItem *bconf;
136
137     for (bconf = GlobalConfList; aconf != bconf->next; bconf = bconf->next)
138       ;
139     bconf->next = aconf->next;
140   }
141   aconf->next = 0;
142 }
143
144 void free_conf(struct ConfItem *aconf)
145 {
146   Debug((DEBUG_DEBUG, "free_conf: %s %s %d",
147          aconf->host ? aconf->host : "*",
148          aconf->name ? aconf->name : "*",
149          aconf->port));
150   if (aconf->dns_pending)
151     delete_resolver_queries(aconf);
152   MyFree(aconf->host);
153   if (aconf->passwd)
154     memset(aconf->passwd, 0, strlen(aconf->passwd));
155   MyFree(aconf->passwd);
156   MyFree(aconf->name);
157   MyFree(aconf);
158 #ifdef        DEBUGMODE
159   --GlobalConfCount;
160 #endif
161 }
162
163 /*
164  * detach_conf - Disassociate configuration from the client.
165  */
166 static void detach_conf(struct Client* cptr, struct ConfItem* aconf)
167 {
168   struct SLink** lp;
169   struct SLink*  tmp;
170
171   assert(0 != aconf);
172   assert(0 != cptr);
173   assert(0 < aconf->clients);
174
175   lp = &(cptr->confs);
176
177   while (*lp) {
178     if ((*lp)->value.aconf == aconf) {
179       if (aconf->conn_class && (aconf->status & CONF_CLIENT_MASK) && ConfLinks(aconf) > 0)
180         --ConfLinks(aconf);
181
182       assert(0 < aconf->clients);
183       if (0 == --aconf->clients && IsIllegal(aconf))
184         free_conf(aconf);
185       tmp = *lp;
186       *lp = tmp->next;
187       free_link(tmp);
188       return;
189     }
190     lp = &((*lp)->next);
191   }
192 }
193
194 /*
195  * conf_dns_callback - called when resolver query finishes
196  * if the query resulted in a successful search, hp will contain
197  * a non-null pointer, otherwise hp will be null.
198  * if successful save hp in the conf item it was called with
199  */
200 static void conf_dns_callback(void* vptr, struct DNSReply* reply)
201 {
202   struct ConfItem* aconf = (struct ConfItem*) vptr;
203   aconf->dns_pending = 0;
204   if (reply)
205     memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
206 }
207
208 /*
209  * conf_dns_lookup - do a nameserver lookup of the conf host
210  * if the conf entry is currently doing a ns lookup do nothing, otherwise
211  * if the lookup returns a null pointer, set the conf dns_pending flag
212  */
213 static struct DNSReply* conf_dns_lookup(struct ConfItem* aconf)
214 {
215   struct DNSReply* dns_reply = 0;
216   if (!aconf->dns_pending) {
217     char            buf[HOSTLEN + 1];
218     struct DNSQuery query;
219     query.vptr     = aconf;
220     query.callback = conf_dns_callback;
221     host_from_uh(buf, aconf->host, HOSTLEN);
222     buf[HOSTLEN] = '\0';
223
224     if (0 == (dns_reply = gethost_byname(buf, &query)))
225       aconf->dns_pending = 1;
226   }
227   return dns_reply;
228 }
229
230
231 /*
232  * lookup_confhost
233  *
234  * Do (start) DNS lookups of all hostnames in the conf line and convert
235  * an IP addresses in a.b.c.d number for to IP#s.
236  */
237 static void lookup_confhost(struct ConfItem *aconf)
238 {
239   struct DNSReply* reply;
240
241   if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
242     Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
243            aconf->host, aconf->name));
244     return;
245   }
246   /*
247    * Do name lookup now on hostnames given and store the
248    * ip numbers in conf structure.
249    */
250   if (IsDigit(*aconf->host)) {
251     /*
252      * rfc 1035 sez host names may not start with a digit
253      * XXX - this has changed code needs to be updated
254      */
255     aconf->ipnum.s_addr = inet_addr(aconf->host);
256     if (INADDR_NONE == aconf->ipnum.s_addr) {
257       Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
258             aconf->host, aconf->name));
259     }
260   }
261   else if ((reply = conf_dns_lookup(aconf)))
262     memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
263 }
264
265 /*
266  * conf_find_server - find a server by name or hostname
267  * returns a server conf item pointer if found, 0 otherwise
268  *
269  * NOTE: at some point when we don't have to scan the entire
270  * list it may be cheaper to look for server names and host
271  * names in separate loops (original code did it that way)
272  */
273 struct ConfItem* conf_find_server(const char* name)
274 {
275   struct ConfItem* conf;
276   assert(0 != name);
277
278   for (conf = GlobalConfList; conf; conf = conf->next) {
279     if (CONF_SERVER == conf->status) {
280       /*
281        * Check first servernames, then try hostnames.
282        * XXX - match returns 0 if there _is_ a match... guess they
283        * haven't decided what true is yet
284        */
285       if (0 == match(name, conf->name))
286         return conf;
287     }
288   }
289   return 0;
290 }
291
292 /*
293  * conf_eval_crule - evaluate connection rules
294  * returns the name of the rule triggered if found, 0 otherwise
295  *
296  * Evaluate connection rules...  If no rules found, allow the
297  * connect.   Otherwise stop with the first true rule (ie: rules
298  * are ored together.  Oper connects are effected only by D
299  * lines (CRULE_ALL) not d lines (CRULE_AUTO).
300  */
301 const char* conf_eval_crule(const char* name, int mask)
302 {
303   struct CRuleConf* p = cruleConfList;
304   assert(0 != name);
305
306   for ( ; p; p = p->next) {
307     if (0 != (p->type & mask) && 0 == match(p->hostmask, name)) {
308       if (crule_eval(p->node))
309         return p->rule;
310     }
311   }
312   return 0;
313 }
314
315 /*
316  * Remove all conf entries from the client except those which match
317  * the status field mask.
318  */
319 void det_confs_butmask(struct Client* cptr, int mask)
320 {
321   struct SLink* link;
322   struct SLink* next;
323   assert(0 != cptr);
324
325   for (link = cptr->confs; link; link = next) {
326     next = link->next;
327     if ((link->value.aconf->status & mask) == 0)
328       detach_conf(cptr, link->value.aconf);
329   }
330 }
331
332 /*
333  * check_limit_and_attach - check client limits and attach I:line
334  *
335  * Made it accept 1 charactor, and 2 charactor limits (0->99 now), 
336  * and dislallow more than 255 people here as well as in ipcheck.
337  * removed the old "ONE" scheme too.
338  *  -- Isomer 2000-06-22
339  */
340 static enum AuthorizationCheckResult
341 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
342 {
343   int number = 255;
344   
345   if (aconf->passwd) {
346     if (IsDigit(*aconf->passwd) && !aconf->passwd[1])
347       number = *aconf->passwd-'0';
348     else if (IsDigit(*aconf->passwd) && IsDigit(aconf->passwd[1]) && 
349              !aconf->passwd[2])
350       number = (*aconf->passwd-'0')*10+(aconf->passwd[1]-'0');
351   }
352   if (IPcheck_nr(cptr) > number)
353     return ACR_TOO_MANY_FROM_IP;
354   return attach_conf(cptr, aconf);
355 }
356
357 /*
358  * Find the first (best) I line to attach.
359  */
360 enum AuthorizationCheckResult attach_iline(struct Client*  cptr)
361 {
362   struct ConfItem* aconf;
363   const char*      hname;
364   int              i;
365   static char      uhost[HOSTLEN + USERLEN + 3];
366   static char      fullname[HOSTLEN + 1];
367   struct hostent*  hp = 0;
368
369   assert(0 != cptr);
370
371   if (cptr->dns_reply)
372     hp = cptr->dns_reply->hp;
373
374   for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
375     if (aconf->status != CONF_CLIENT)
376       continue;
377     if (aconf->port && aconf->port != cptr->listener->port)
378       continue;
379     if (!aconf->host || !aconf->name)
380       continue;
381     if (hp) {
382       for (i = 0, hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
383         ircd_strncpy(fullname, hname, HOSTLEN);
384         fullname[HOSTLEN] = '\0';
385
386         Debug((DEBUG_DNS, "a_il: %s->%s", cptr->sockhost, fullname));
387
388         if (strchr(aconf->name, '@')) {
389           strcpy(uhost, cptr->username);
390           strcat(uhost, "@");
391         }
392         else
393           *uhost = '\0';
394         strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
395         uhost[sizeof(uhost) - 1] = 0;
396         if (0 == match(aconf->name, uhost)) {
397           if (strchr(uhost, '@'))
398             cptr->flags |= FLAGS_DOID;
399           return check_limit_and_attach(cptr, aconf);
400         }
401       }
402     }
403     if (strchr(aconf->host, '@')) {
404       ircd_strncpy(uhost, cptr->username, sizeof(uhost) - 2);
405       uhost[sizeof(uhost) - 2] = 0;
406       strcat(uhost, "@");
407     }
408     else
409       *uhost = '\0';
410     strncat(uhost, cptr->sock_ip, sizeof(uhost) - 1 - strlen(uhost));
411     uhost[sizeof(uhost) - 1] = 0;
412     if (match(aconf->host, uhost))
413       continue;
414     if (strchr(uhost, '@'))
415       cptr->flags |= FLAGS_DOID;
416
417     return check_limit_and_attach(cptr, aconf);
418   }
419   return ACR_NO_AUTHORIZATION;
420 }
421
422 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
423 {
424   struct SLink *lp;
425
426   for (lp = cptr->confs; lp; lp = lp->next) {
427     if (lp->value.aconf == aconf)
428       return 1;
429   }
430   return 0;
431 }
432
433 /*
434  * attach_conf
435  *
436  * Associate a specific configuration entry to a *local*
437  * client (this is the one which used in accepting the
438  * connection). Note, that this automaticly changes the
439  * attachment if there was an old one...
440  */
441 enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
442 {
443   struct SLink *lp;
444
445   if (is_attached(aconf, cptr))
446     return ACR_ALREADY_AUTHORIZED;
447   if (IsIllegal(aconf))
448     return ACR_NO_AUTHORIZATION;
449   if ((aconf->status & (CONF_LOCOP | CONF_OPERATOR | CONF_CLIENT)) &&
450       ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
451     return ACR_TOO_MANY_IN_CLASS;  /* Use this for printing error message */
452   lp = make_link();
453   lp->next = cptr->confs;
454   lp->value.aconf = aconf;
455   cptr->confs = lp;
456   ++aconf->clients;
457   if (aconf->status & CONF_CLIENT_MASK)
458     ConfLinks(aconf)++;
459   return ACR_OK;
460 }
461
462 const struct LocalConf* conf_get_local(void)
463 {
464   return &localConf;
465 }
466
467 /*
468  * attach_confs_byname
469  *
470  * Attach a CONF line to a client if the name passed matches that for
471  * the conf file (for non-C/N lines) or is an exact match (C/N lines
472  * only).  The difference in behaviour is to stop C:*::* and N:*::*.
473  */
474 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
475                                      int statmask)
476 {
477   struct ConfItem* tmp;
478   struct ConfItem* first = NULL;
479
480   assert(0 != name);
481
482   if (HOSTLEN < strlen(name))
483     return 0;
484
485   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
486     if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
487       assert(0 != tmp->name);
488       if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) { 
489         if (ACR_OK == attach_conf(cptr, tmp) && !first)
490           first = tmp;
491       }
492     }
493   }
494   return first;
495 }
496
497 /*
498  * Added for new access check    meLazy
499  */
500 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
501                                      int statmask)
502 {
503   struct ConfItem* tmp;
504   struct ConfItem* first = 0;
505
506   assert(0 != host);
507   if (HOSTLEN < strlen(host))
508     return 0;
509
510   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
511     if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
512       assert(0 != tmp->host);
513       if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) { 
514         if (ACR_OK == attach_conf(cptr, tmp) && !first)
515           first = tmp;
516       }
517     }
518   }
519   return first;
520 }
521
522 /*
523  * find a conf entry which matches the hostname and has the same name.
524  */
525 struct ConfItem* find_conf_exact(const char* name, const char* user,
526                                  const char* host, int statmask)
527 {
528   struct ConfItem *tmp;
529   char userhost[USERLEN + HOSTLEN + 3];
530
531   if (user)
532     ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
533   else
534     ircd_strncpy(userhost, host, sizeof(userhost) - 1);
535
536   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
537     if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
538         0 != ircd_strcmp(tmp->name, name))
539       continue;
540     /*
541      * Accept if the *real* hostname (usually sockecthost)
542      * socket host) matches *either* host or name field
543      * of the configuration.
544      */
545     if (match(tmp->host, userhost))
546       continue;
547     if (tmp->status & (CONF_OPERATOR | CONF_LOCOP)) {
548       if (tmp->clients < MaxLinks(tmp->conn_class))
549         return tmp;
550       else
551         continue;
552     }
553     else
554       return tmp;
555   }
556   return 0;
557 }
558
559 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
560                                   int statmask)
561 {
562   struct ConfItem* tmp;
563   assert(0 != name);
564
565   if (HOSTLEN < strlen(name))
566     return 0;
567
568   for (; lp; lp = lp->next) {
569     tmp = lp->value.aconf;
570     if (0 != (tmp->status & statmask)) {
571       assert(0 != tmp->name);
572       if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
573         return tmp;
574     }
575   }
576   return 0;
577 }
578
579 /*
580  * Added for new access check    meLazy
581  */
582 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
583                                   int statmask)
584 {
585   struct ConfItem* tmp = NULL;
586   assert(0 != host);
587
588   if (HOSTLEN < strlen(host))
589     return 0;
590
591   for (; lp; lp = lp->next) {
592     tmp = lp->value.aconf;
593     if (0 != (tmp->status & statmask)) {
594       assert(0 != tmp->host);
595       if (0 == match(tmp->host, host))
596         return tmp;
597     }
598   }
599   return 0;
600 }
601
602 /*
603  * find_conf_ip
604  *
605  * Find a conf line using the IP# stored in it to search upon.
606  * Added 1/8/92 by Avalon.
607  */
608 struct ConfItem* find_conf_byip(struct SLink* lp, const char* ip, 
609                                 int statmask)
610 {
611   struct ConfItem* tmp;
612
613   for (; lp; lp = lp->next) {
614     tmp = lp->value.aconf;
615     if (0 != (tmp->status & statmask)) {
616       if (0 == memcmp(&tmp->ipnum, ip, sizeof(struct in_addr)))
617         return tmp;
618     }
619   }
620   return 0;
621 }
622
623 /*
624  * find_conf_entry
625  *
626  * - looks for a match on all given fields.
627  */
628 static struct ConfItem *find_conf_entry(struct ConfItem *aconf,
629                                         unsigned int mask)
630 {
631   struct ConfItem *bconf;
632   assert(0 != aconf);
633
634   mask &= ~CONF_ILLEGAL;
635
636   for (bconf = GlobalConfList; bconf; bconf = bconf->next) {
637     if (!(bconf->status & mask) || (bconf->port != aconf->port))
638       continue;
639
640     if ((EmptyString(bconf->host) && !EmptyString(aconf->host)) ||
641         (EmptyString(aconf->host) && !EmptyString(bconf->host)))
642       continue;
643     if (!EmptyString(bconf->host) && 0 != ircd_strcmp(bconf->host, aconf->host))
644       continue;
645
646     if ((EmptyString(bconf->passwd) && !EmptyString(aconf->passwd)) ||
647         (EmptyString(aconf->passwd) && !EmptyString(bconf->passwd)))
648       continue;
649     if (!EmptyString(bconf->passwd) && (!IsDigit(*bconf->passwd) || bconf->passwd[1])
650 #ifdef USEONE
651         && 0 != ircd_strcmp(bconf->passwd, "ONE")
652 #endif
653         && 0 != ircd_strcmp(bconf->passwd, aconf->passwd))
654       continue;
655
656     if ((EmptyString(bconf->name) && !EmptyString(aconf->name)) ||
657         (EmptyString(aconf->name) && !EmptyString(bconf->name)))
658       continue;
659     if (!EmptyString(bconf->name) && 0 != ircd_strcmp(bconf->name, aconf->name))
660       continue;
661     break;
662   }
663   return bconf;
664 }
665
666
667 /*
668  * If conf line is a class definition, create a class entry
669  * for it and make the conf_line illegal and delete it.
670  */
671 void conf_add_class(const char* const* fields, int count)
672 {
673   if (count < 6)
674     return;
675   add_class(atoi(fields[1]), atoi(fields[2]), atoi(fields[3]),
676             atoi(fields[4]), atoi(fields[5]));
677 }
678
679 void conf_add_listener(const char* const* fields, int count)
680 {
681   int is_server = 0;
682   int is_hidden = 0;
683
684   /*
685    * need a port
686    */
687   if (count < 5 || EmptyString(fields[4]))
688     return;
689
690   if (!EmptyString(fields[3])) {
691     const char* x = fields[3];
692     if ('S' == ToUpper(*x))
693       is_server = 1;
694     ++x;
695     if ('H' == ToUpper(*x))
696       is_hidden = 1;
697   }
698   /*           port             vhost      mask  */
699   add_listener(atoi(fields[4]), fields[2], fields[1], is_server, is_hidden);
700 }
701
702 void conf_add_local(const char* const* fields, int count)
703 {
704   if (count < 6 || EmptyString(fields[1]) || EmptyString(fields[5])) {
705     ircd_log(L_CRIT, "Your M: line must have 6 fields!\n");
706     return;
707   }
708   /*
709    * these two can only be set the first time
710    */
711   if (0 == localConf.name) {
712     if (string_is_hostname(fields[1]))
713       DupString(localConf.name, fields[1]);
714   }
715   if (0 == localConf.numeric) {
716     localConf.numeric = atoi(fields[5]);
717     if (0 == localConf.numeric)
718       ircd_log(L_WARNING, "Your M: line must have a Numeric value greater than 0\n");
719   }
720   /*
721    * these two can be changed while the server is running
722    */
723   if (string_is_address(fields[2])) {
724     if (INADDR_NONE == (localConf.vhost_address.s_addr = inet_addr(fields[2])))
725       localConf.vhost_address.s_addr = INADDR_ANY;
726   }
727   MyFree(localConf.description);
728   DupString(localConf.description, fields[3]);
729   /*
730    * XXX - shouldn't be setting these directly here
731    */
732   ircd_strncpy(me.info, fields[3], REALLEN);
733   set_virtual_host(localConf.vhost_address);
734 }
735
736 void conf_add_admin(const char* const* fields, int count)
737 {
738   /*
739    * if you have one, it MUST have 3 lines
740    */
741   if (count < 4) {
742     ircd_log(L_CRIT, "Your A: line must have 4 fields!\n");
743     return;
744   }
745   MyFree(localConf.location1);
746   DupString(localConf.location1, fields[1]);
747
748   MyFree(localConf.location2);
749   DupString(localConf.location2, fields[2]);
750
751   MyFree(localConf.contact);
752   DupString(localConf.contact, fields[3]);
753 }
754
755 /*
756  * conf_add_crule - Create expression tree from connect rule and add it
757  * to the crule list
758  */
759 void conf_add_crule(const char* const* fields, int count, int type)
760 {
761   struct CRuleNode* node;
762   assert(0 != fields);
763   
764   if (count < 4 || EmptyString(fields[1]) || EmptyString(fields[3]))
765     return;
766   
767   if ((node = crule_parse(fields[3]))) {
768     struct CRuleConf* p = (struct CRuleConf*) MyMalloc(sizeof(struct CRuleConf));
769     assert(0 != p);
770
771     DupString(p->hostmask, fields[1]);
772     collapse(p->hostmask);
773
774     DupString(p->rule, fields[3]);
775
776     p->type = type;
777     p->node = node;
778     p->next = cruleConfList;
779     cruleConfList = p;
780   } 
781 }
782
783 void conf_erase_crule_list(void)
784 {
785   struct CRuleConf* next;
786   struct CRuleConf* p = cruleConfList;
787
788   for ( ; p; p = next) {
789     next = p->next;
790     crule_free(&p->node);
791     MyFree(p->hostmask);
792     MyFree(p->rule);
793     MyFree(p);
794   }
795   cruleConfList = 0;
796 }
797
798 const struct CRuleConf* conf_get_crule_list(void)
799 {
800   return cruleConfList;
801 }
802
803 void conf_add_server(const char* const* fields, int count)
804 {
805   struct ServerConf* server;
806   struct in_addr    addr;
807   assert(0 != fields);
808   /*
809    * missing host, password, or alias?
810    */
811   if (count < 6 || EmptyString(fields[1]) || EmptyString(fields[2]) || EmptyString(fields[3]))
812     return;
813   /*
814    * check the host
815    */
816   if (string_is_hostname(fields[1]))
817     addr.s_addr = INADDR_NONE;
818   else if (INADDR_NONE == (addr.s_addr = inet_addr(fields[1])))
819     return;
820
821   server = (struct ServerConf*) MyMalloc(sizeof(struct ServerConf));
822   assert(0 != server);
823   DupString(server->hostname, fields[1]);
824   DupString(server->passwd,   fields[2]);
825   DupString(server->alias,    fields[3]);
826   server->address.s_addr = addr.s_addr;
827   server->port           = atoi(fields[4]);
828   server->dns_pending    = 0;
829   server->connected      = 0;
830   server->hold           = 0;
831   server->conn_class      = find_class(atoi(fields[5]));
832
833   server->next = serverConfList;
834   serverConfList = server;
835
836   // if (INADDR_NONE == server->address.s_addr)
837     // lookup_confhost(server);
838 }
839
840 void conf_add_deny(const char* const* fields, int count, int ip_kill)
841 {
842   struct DenyConf* conf;
843
844   if (count < 4 || EmptyString(fields[1]) || EmptyString(fields[3]))
845     return;
846   
847   conf = (struct DenyConf*) MyMalloc(sizeof(struct DenyConf));
848   assert(0 != conf);
849   memset(conf, 0, sizeof(struct DenyConf));
850
851   DupString(conf->hostmask, fields[1]);
852   collapse(conf->hostmask);
853
854   if (!EmptyString(fields[2])) {
855     const char* p = fields[2];
856     if ('!' == *p) {
857       conf->is_file = 1;
858       ++p;
859     }
860     DupString(conf->message, p);
861   }
862   DupString(conf->usermask, fields[3]);
863   collapse(conf->usermask);
864
865   if (ip_kill) {
866     /* 
867      * Here we use the same kludge as in listener.c to parse
868      * a.b.c.d, or a.b.c.*, or a.b.c.d/e.
869      */
870     int  c_class;
871     char ipname[16];
872     int  ad[4] = { 0 };
873     int  bits2 = 0;
874     c_class = sscanf(conf->hostmask, "%d.%d.%d.%d/%d",
875                      &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
876     if (c_class != 5) {
877       conf->bits = c_class * 8;
878     }
879     else {
880       conf->bits = bits2;
881     }
882     sprintf_irc(ipname, "%d.%d.%d.%d", ad[0], ad[1], ad[2], ad[3]);
883     
884     /*
885      * This ensures endian correctness
886      */
887     conf->s_addr = inet_addr(ipname);
888     Debug((DEBUG_DEBUG, "IPkill: %s = %08x/%i (%08x)", ipname,
889            conf->s_addr, conf->bits, NETMASK(conf->bits)));
890   }
891   conf->next = denyConfList;
892   denyConfList = conf;
893 }
894
895 void conf_erase_deny_list(void)
896 {
897   struct DenyConf* next;
898   struct DenyConf* p = denyConfList;
899   for ( ; p; p = next) {
900     next = p->next;
901     MyFree(p->hostmask);
902     MyFree(p->usermask);
903     MyFree(p->message);
904     MyFree(p);
905   }
906   denyConfList = 0;
907 }
908  
909 const struct DenyConf* conf_get_deny_list(void)
910 {
911   return denyConfList;
912 }
913
914 /*
915  * read_configuration_file
916  *
917  * Read configuration file.
918  *
919  * returns 0, if file cannot be opened
920  *         1, if file read
921  */
922
923 #define MAXCONFLINKS 150
924
925 int read_configuration_file(void)
926 {
927   enum { MAX_FIELDS = 15 };
928
929   char* src;
930   char* dest;
931   int quoted;
932   FBFILE *file;
933   char line[512];
934   int ccount = 0;
935   struct ConfItem *aconf = 0;
936   
937   int   field_count = 0;
938   const char* field_vector[MAX_FIELDS + 1];
939
940   Debug((DEBUG_DEBUG, "read_configuration_file: ircd.conf = %s", configfile));
941   if (0 == (file = fbopen(configfile, "r"))) {
942     return 0;
943   }
944
945   while (fbgets(line, sizeof(line) - 1, file)) {
946     if ('#' == *line || IsSpace(*line))
947       continue;
948
949     if ((src = strchr(line, '\n')))
950       *src = '\0';
951     
952     if (':' != line[1]) {
953       Debug((DEBUG_ERROR, "Bad config line: %s", line));
954       sendto_op_mask(SNO_OLDSNO,"Bad Config line");
955       continue;
956     }
957
958     /*
959      * do escapes, quoting, comments, and field breakup in place
960      * in one pass with a poor mans state machine
961      */
962     field_vector[0] = line;
963     field_count = 1;
964     quoted = 0;
965
966     for (src = line, dest = line; *src; ) {
967       switch (*src) {
968       case '\\':
969         ++src;
970         switch (*src) {
971         case 'b':
972           *dest++ = '\b';
973           ++src;
974           break;
975         case 'f':
976           *dest++ = '\f';
977           ++src;
978           break;
979         case 'n':
980           *dest++ = '\n';
981           ++src;
982           break;
983         case 'r':
984           *dest++ = '\r';      
985           ++src;
986           break;
987         case 't':
988           *dest++ = '\t';
989           ++src;
990           break;
991         case 'v':
992           *dest++ = '\v';
993           ++src;
994           break;
995         case '\\':
996           *dest++ = '\\';
997           ++src;
998           break;
999         case '\0':
1000           break;
1001         default:
1002           *dest++ = *src++;
1003           break;
1004         }
1005         break;
1006       case '"':
1007         if (quoted)
1008           quoted = 0;
1009         else
1010           quoted = 1;
1011         /*
1012          * strip quotes
1013          */
1014         ++src;
1015         break;
1016       case ':':
1017         if (quoted)
1018           *dest++ = *src++;
1019         else {
1020           *dest++ = '\0';
1021           field_vector[field_count++] = dest;
1022           if (field_count > MAX_FIELDS)
1023             *src = '\0';
1024           else  
1025             ++src;
1026         }
1027         break;
1028       case '#':
1029         *src = '\0';
1030         break;
1031       default:
1032         *dest++ = *src++;
1033         break;
1034       }
1035     }
1036     *dest = '\0';
1037
1038     if (field_count < 2 || EmptyString(field_vector[0]))
1039       continue;
1040
1041     if (aconf)
1042       free_conf(aconf);
1043
1044     aconf = make_conf();
1045
1046     switch (*field_vector[0]) {
1047     case 'A':                /* Name, e-mail address of administrator */
1048     case 'a':                /* of this server. CONF_ADMIN */
1049       conf_add_admin(field_vector, field_count);
1050       aconf->status = CONF_ILLEGAL;
1051       break;
1052     case 'C':                /* Server where I should try to connect */
1053     case 'c':                /* in case of lp failures             */
1054       ++ccount;
1055       aconf->status = CONF_SERVER;
1056       break;
1057       /* Connect rule */
1058     case 'D':  /* CONF_CRULEALL */
1059       conf_add_crule(field_vector, field_count, CRULE_ALL);
1060       aconf->status = CONF_ILLEGAL;
1061       break;
1062       /* Connect rule - autos only */
1063     case 'd':  /* CONF_CRULEAUTO */
1064       conf_add_crule(field_vector, field_count, CRULE_AUTO);
1065       aconf->status = CONF_ILLEGAL;
1066       break;
1067     case 'H':                /* Hub server line */
1068     case 'h':
1069       aconf->status = CONF_HUB;
1070       break;
1071     case 'I':                /* Just plain normal irc client trying  */
1072     case 'i':                /* to connect me */
1073       aconf->status = CONF_CLIENT;
1074       break;
1075     case 'K':                /* Kill user line on irc.conf           */
1076       conf_add_deny(field_vector, field_count, 0);
1077       aconf->status = CONF_ILLEGAL;
1078       break;
1079     case 'k':                /* Kill user line based on IP in ircd.conf */
1080       conf_add_deny(field_vector, field_count, 1);
1081       aconf->status = CONF_ILLEGAL;
1082       break;
1083       /* Operator. Line should contain at least */
1084       /* password and host where connection is  */
1085     case 'L':                /* guaranteed leaf server */
1086     case 'l':
1087       aconf->status = CONF_LEAF;
1088       break;
1089       /* Me. Host field is name used for this host */
1090       /* and port number is the number of the port */
1091     case 'M':
1092     case 'm':        /* CONF_ME */
1093       conf_add_local(field_vector, field_count);
1094       aconf->status = CONF_ILLEGAL;
1095       break;
1096     case 'O':
1097       aconf->status = CONF_OPERATOR;
1098       break;
1099       /* Local Operator, (limited privs --SRB) */
1100     case 'o':
1101       aconf->status = CONF_LOCOP;
1102       break;
1103     case 'P':                /* listen port line */
1104     case 'p':        /* CONF_LISTEN_PORT */
1105       conf_add_listener(field_vector, field_count);
1106       aconf->status = CONF_ILLEGAL;
1107       break;
1108     case 'T':                /* print out different motd's */
1109     case 't':                /* based on hostmask - CONF_TLINES */
1110       motd_add(field_vector[1], field_vector[2]);
1111       aconf->status = CONF_ILLEGAL;
1112       break;
1113     case 'U':      /* Underworld server, allowed to hack modes */
1114     case 'u':      /* *Every* server on the net must define the same !!! */
1115       aconf->status = CONF_UWORLD;
1116       break;
1117     case 'Y':
1118     case 'y':      /* CONF_CLASS */
1119       conf_add_class(field_vector, field_count);
1120       aconf->status = CONF_ILLEGAL;
1121       break;
1122     default:
1123       Debug((DEBUG_ERROR, "Error in config file: %s", line));
1124       sendto_op_mask(SNO_OLDSNO,"Unknown prefix in config file: %c", *field_vector[0]);
1125       aconf->status = CONF_ILLEGAL;
1126       break;
1127     }
1128     if (IsIllegal(aconf))
1129       continue;
1130
1131     if (!EmptyString(field_vector[1]))
1132       DupString(aconf->host, field_vector[1]);
1133
1134     if (!EmptyString(field_vector[2]))
1135       DupString(aconf->passwd, field_vector[2]);
1136
1137     if (field_count > 3 && !EmptyString(field_vector[3]))
1138         DupString(aconf->name, field_vector[3]);
1139
1140     if (field_count > 4 && !EmptyString(field_vector[4]))
1141         aconf->port = atoi(field_vector[4]); 
1142
1143     if (field_count > 5 && !EmptyString(field_vector[5]))
1144       aconf->conn_class = find_class(atoi(field_vector[5]));
1145
1146     /*
1147      * Associate each conf line with a class by using a pointer
1148      * to the correct class record. -avalon
1149      */
1150     if (aconf->status & CONF_CLIENT_MASK) {
1151       if (aconf->conn_class == 0)
1152         aconf->conn_class = find_class(0);
1153     }
1154     if (aconf->status & CONF_CLIENT) {
1155       struct ConfItem *bconf;
1156
1157       if ((bconf = find_conf_entry(aconf, aconf->status))) {
1158         delist_conf(bconf);
1159         bconf->status &= ~CONF_ILLEGAL;
1160         if (aconf->status == CONF_CLIENT) {
1161           /*
1162            * copy the password field in case it changed
1163            */
1164           MyFree(bconf->passwd);
1165           bconf->passwd = aconf->passwd;
1166           aconf->passwd = 0;
1167
1168           ConfLinks(bconf) -= bconf->clients;
1169           bconf->conn_class = aconf->conn_class;
1170           if (bconf->conn_class)
1171             ConfLinks(bconf) += bconf->clients;
1172         }
1173         free_conf(aconf);
1174         aconf = bconf;
1175       }
1176     }
1177     if (aconf->status & CONF_SERVER) {
1178       if (ccount > MAXCONFLINKS || !aconf->host || strchr(aconf->host, '*') ||
1179           strchr(aconf->host, '?') || !aconf->name)
1180         continue;
1181     }
1182     if (aconf->status & (CONF_LOCOP | CONF_OPERATOR)) {
1183       if (!strchr(aconf->host, '@')) {
1184         char* newhost;
1185         int len = 3;                /* *@\0 = 3 */
1186
1187         len += strlen(aconf->host);
1188         newhost = (char*) MyMalloc(len);
1189         assert(0 != newhost);
1190         ircd_snprintf(0, newhost, len, "*@%s", aconf->host);
1191         MyFree(aconf->host);
1192         aconf->host = newhost;
1193       }
1194     }
1195     if (aconf->status & CONF_SERVER) {
1196       if (EmptyString(aconf->passwd))
1197         continue;
1198       lookup_confhost(aconf);
1199     }
1200     /*
1201      * Juped nicks are listed in the 'password' field of U:lines,
1202      * the list is comma separated and might be empty and/or contain
1203      * empty elements... the only limit is that it MUST be shorter
1204      * than 512 chars, or they will be cutted out :)
1205      */
1206     if ((aconf->status == CONF_UWORLD) && (aconf->passwd) && (*aconf->passwd))
1207       addNickJupes(aconf->passwd);
1208
1209     collapse(aconf->host);
1210     collapse(aconf->name);
1211     Debug((DEBUG_NOTICE,
1212         "Read Init: (%d) (%s) (%s) (%s) (%u) (%p)",
1213         aconf->status, aconf->host, aconf->passwd,
1214         aconf->name, aconf->port, aconf->conn_class));
1215     aconf->next = GlobalConfList;
1216     GlobalConfList = aconf;
1217     aconf = NULL;
1218   }
1219   if (aconf)
1220     free_conf(aconf);
1221   fbclose(file);
1222   nextping = nextconnect = CurrentTime;
1223   return 1;
1224 }
1225
1226 /*
1227  * rehash
1228  *
1229  * Actual REHASH service routine. Called with sig == 0 if it has been called
1230  * as a result of an operator issuing this command, else assume it has been
1231  * called as a result of the server receiving a HUP signal.
1232  */
1233 int rehash(struct Client *cptr, int sig)
1234 {
1235   struct ConfItem** tmp = &GlobalConfList;
1236   struct ConfItem*  tmp2;
1237   struct Client*    acptr;
1238   int               i;
1239   int               ret = 0;
1240   int               found_g = 0;
1241
1242   if (1 == sig)
1243     sendto_opmask_butone(0, SNO_OLDSNO,
1244                          "Got signal SIGHUP, reloading ircd conf. file");
1245
1246   while ((tmp2 = *tmp)) {
1247     if (tmp2->clients) {
1248       /*
1249        * Configuration entry is still in use by some
1250        * local clients, cannot delete it--mark it so
1251        * that it will be deleted when the last client
1252        * exits...
1253        */
1254       if (CONF_CLIENT == (tmp2->status & CONF_CLIENT))
1255         tmp = &tmp2->next;
1256       else {
1257         *tmp = tmp2->next;
1258         tmp2->next = 0;
1259       }
1260       tmp2->status |= CONF_ILLEGAL;
1261     }
1262     else {
1263       *tmp = tmp2->next;
1264       free_conf(tmp2);
1265     }
1266   }
1267   conf_erase_crule_list();
1268   conf_erase_deny_list();
1269   motd_clear();
1270
1271   /*
1272    * delete the juped nicks list
1273    */
1274   clearNickJupes();
1275
1276   if (sig != 2)
1277     flush_resolver_cache();
1278
1279   class_mark_delete();
1280   mark_listeners_closing();
1281
1282   read_configuration_file();
1283
1284   log_reopen(); /* reopen log files */
1285
1286   close_listeners();
1287   class_delete_marked();         /* unless it fails */
1288
1289   /*
1290    * Flush out deleted I and P lines although still in use.
1291    */
1292   for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
1293     if (CONF_ILLEGAL == (tmp2->status & CONF_ILLEGAL)) {
1294       *tmp = tmp2->next;
1295       tmp2->next = NULL;
1296       if (!tmp2->clients)
1297         free_conf(tmp2);
1298     }
1299     else
1300       tmp = &tmp2->next;
1301   }
1302   for (i = 0; i <= HighestFd; i++) {
1303     if ((acptr = LocalClientArray[i])) {
1304       assert(!IsMe(acptr));
1305       if (IsServer(acptr)) {
1306         det_confs_butmask(acptr,
1307             ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
1308         attach_confs_byname(acptr, acptr->name,
1309                             CONF_HUB | CONF_LEAF | CONF_UWORLD);
1310       }
1311       /* Because admin's are getting so uppity about people managing to
1312        * get past K/G's etc, we'll "fix" the bug by actually explaining
1313        * whats going on.
1314        */
1315       if ((found_g = find_kill(acptr))) {
1316         sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
1317                              found_g == -2 ? "G-line active for %s%s" :
1318                              "K-line active for %s%s",
1319                              IsUnknown(acptr) ? "Unregistered Client ":"",                     
1320                              get_client_name(acptr, HIDE_IP));
1321         if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
1322             "K-lined") == CPTR_KILLED)
1323           ret = CPTR_KILLED;
1324       }
1325     }
1326   }
1327   return ret;
1328 }
1329
1330 /*
1331  * init_conf
1332  *
1333  * Read configuration file.
1334  *
1335  * returns 0, if file cannot be opened
1336  *         1, if file read
1337  */
1338
1339 int init_conf(void)
1340 {
1341   if (read_configuration_file()) {
1342     /*
1343      * make sure we're sane to start if the config
1344      * file read didn't get everything we need.
1345      * XXX - should any of these abort the server?
1346      * TODO: add warning messages
1347      */
1348     if (0 == localConf.name || 0 == localConf.numeric)
1349       return 0;
1350
1351     if (0 == localConf.location1)
1352       DupString(localConf.location1, "");
1353     if (0 == localConf.location2)
1354       DupString(localConf.location2, "");
1355     if (0 == localConf.contact)
1356       DupString(localConf.contact, "");
1357     
1358     return 1;
1359   }
1360   return 0;
1361 }
1362
1363 /*
1364  * find_kill
1365  * input:
1366  *  client pointer
1367  * returns:
1368  *  0: Client may continue to try and connect
1369  * -1: Client was K/k:'d - sideeffect: reason was sent.
1370  * -2: Client was G/g:'d - sideeffect: reason was sent.
1371  * sideeffects:
1372  *  Client may have been sent a reason why they are denied, as above.
1373  */
1374 int find_kill(struct Client *cptr)
1375 {
1376   const char*      host;
1377   const char*      name;
1378   struct DenyConf* deny;
1379   struct Gline*    agline = NULL;
1380
1381   assert(0 != cptr);
1382
1383   if (!cptr->user)
1384     return 0;
1385
1386   host = cptr->sockhost;
1387   name = cptr->user->username;
1388
1389   assert(strlen(host) <= HOSTLEN);
1390   assert((name ? strlen(name) : 0) <= HOSTLEN);
1391   
1392   /* 2000-07-14: Rewrote this loop for massive speed increases.
1393    *             -- Isomer
1394    */
1395   for (deny = denyConfList; deny; deny = deny->next) {
1396     if (0 != match(deny->usermask, name))
1397       continue;
1398             
1399     if (EmptyString(deny->hostmask))
1400       break;
1401     
1402     if (deny->ip_kill) { /* k: by IP */
1403       Debug((DEBUG_DEBUG, "ip: %08x network: %08x/%i mask: %08x",
1404              cptr->ip.s_addr, deny->s_addr, deny->bits, NETMASK(deny->bits)));
1405       if ((cptr->ip.s_addr & NETMASK(deny->bits)) == deny->s_addr)
1406         break;
1407     }
1408     else if (0 == match(deny->hostmask, host))
1409       break;
1410   }
1411   if (deny) {
1412     if (EmptyString(deny->message))
1413       send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1414                  ":Connection from your host is refused on this server.");
1415     else {
1416       if (deny->is_file)
1417         killcomment(cptr, deny->message);
1418       else
1419         send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", deny->message);
1420     }
1421   }
1422   else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline)) {
1423     /*
1424      * find active glines
1425      * added a check against the user's IP address to find_gline() -Kev
1426      */
1427     send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", GlineReason(agline));
1428   }
1429   else
1430     agline = 0;          /* if a gline was found, it was inactive */
1431
1432   if (deny)
1433     return -1;
1434   if (agline)
1435     return -2;
1436     
1437   return 0;
1438 }
1439
1440 /*
1441  * Ordinary client access check. Look for conf lines which have the same
1442  * status as the flags passed.
1443  */
1444 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1445 {
1446   enum AuthorizationCheckResult acr = ACR_OK;
1447
1448   ClearAccess(cptr);
1449
1450   if ((acr = attach_iline(cptr))) {
1451     Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]", 
1452           cptr->name, cptr->sockhost));
1453     return acr;
1454   }
1455   return ACR_OK;
1456 }
1457
1458 /*
1459  * check_server()
1460  *
1461  * Check access for a server given its name (passed in cptr struct).
1462  * Must check for all C/N lines which have a name which matches the
1463  * name given and a host which matches. A host alias which is the
1464  * same as the server name is also acceptable in the host field of a
1465  * C/N line.
1466  *
1467  * Returns
1468  *  0 = Success
1469  * -1 = Access denied
1470  * -2 = Bad socket.
1471  */
1472 int conf_check_server(struct Client *cptr)
1473 {
1474   struct ConfItem* c_conf = NULL;
1475   struct SLink*    lp;
1476
1477   Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]", 
1478         cptr->name, cptr->sockhost));
1479
1480   if (IsUnknown(cptr) && !attach_confs_byname(cptr, cptr->name, CONF_SERVER)) {
1481     Debug((DEBUG_DNS, "No C/N lines for %s", cptr->sockhost));
1482     return -1;
1483   }
1484   lp = cptr->confs;
1485   /*
1486    * We initiated this connection so the client should have a C and N
1487    * line already attached after passing through the connect_server()
1488    * function earlier.
1489    */
1490   if (IsConnecting(cptr) || IsHandshake(cptr)) {
1491     c_conf = find_conf_byname(lp, cptr->name, CONF_SERVER);
1492     if (!c_conf) {
1493       sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1494                            cptr->name);
1495       det_confs_butmask(cptr, 0);
1496       return -1;
1497     }
1498   }
1499
1500   ClearAccess(cptr);
1501
1502   if (!c_conf) {
1503     if (cptr->dns_reply) {
1504       int             i;
1505       struct hostent* hp = cptr->dns_reply->hp;
1506       const char*     name = hp->h_name;
1507       /*
1508        * If we are missing a C or N line from above, search for
1509        * it under all known hostnames we have for this ip#.
1510        */
1511       for (i = 0; name; name = hp->h_aliases[i++]) {
1512         if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1513           ircd_strncpy(cptr->sockhost, name, HOSTLEN);
1514           break;
1515         }
1516       }
1517       if (!c_conf) {
1518         for (i = 0; hp->h_addr_list[i]; i++) {
1519           if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1520             break;
1521         }
1522       }
1523     }
1524     else {
1525       /*
1526        * Check for C lines with the hostname portion the ip number
1527        * of the host the server runs on. This also checks the case where
1528        * there is a server connecting from 'localhost'.
1529        */
1530       c_conf = find_conf_byhost(lp, cptr->sockhost, CONF_SERVER);
1531     }
1532   }
1533   /*
1534    * Attach by IP# only if all other checks have failed.
1535    * It is quite possible to get here with the strange things that can
1536    * happen when using DNS in the way the irc server does. -avalon
1537    */
1538   if (!c_conf)
1539     c_conf = find_conf_byip(lp, (const char*) &cptr->ip, CONF_SERVER);
1540   /*
1541    * detach all conf lines that got attached by attach_confs()
1542    */
1543   det_confs_butmask(cptr, 0);
1544   /*
1545    * if no C or no N lines, then deny access
1546    */
1547   if (!c_conf) {
1548     Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1549           cptr->name, cptr->username, cptr->sockhost));
1550     return -1;
1551   }
1552   ircd_strncpy(cptr->name, c_conf->name, HOSTLEN);
1553   /*
1554    * attach the C and N lines to the client structure for later use.
1555    */
1556   attach_conf(cptr, c_conf);
1557   attach_confs_byname(cptr, cptr->name, CONF_HUB | CONF_LEAF | CONF_UWORLD);
1558
1559   if (INADDR_NONE == c_conf->ipnum.s_addr)
1560     c_conf->ipnum.s_addr = cptr->ip.s_addr;
1561
1562   Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cptr->name, cptr->sockhost));
1563   return 0;
1564 }
1565