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