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