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