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