284f01e64bbd25978ec5c82a45d43797e28f8990
[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  * Made it accept 1 charactor, and 2 charactor limits (0->99 now), 
491  * and dislallow more than 255 people here as well as in ipcheck.
492  * removed the old "ONE" scheme too.
493  *  -- Isomer 2000-06-22
494  */
495 static enum AuthorizationCheckResult
496 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
497 {
498   int number = 255;
499   
500   if (aconf->passwd) {
501     if (IsDigit(*aconf->passwd) && !aconf->passwd[1])
502       number = *aconf->passwd-'0';
503     else if (IsDigit(*aconf->passwd) && IsDigit(aconf->passwd[1]) && 
504              !aconf->passwd[2])
505       number = (*aconf->passwd-'0')*10+(aconf->passwd[1]-'0');
506   }
507   if (ip_registry_count(cptr->ip.s_addr) > number)
508     return ACR_TOO_MANY_FROM_IP;
509   return attach_conf(cptr, aconf);
510 }
511
512 /*
513  * Find the first (best) I line to attach.
514  */
515 enum AuthorizationCheckResult attach_iline(struct Client*  cptr)
516 {
517   struct ConfItem* aconf;
518   const char*      hname;
519   int              i;
520   static char      uhost[HOSTLEN + USERLEN + 3];
521   static char      fullname[HOSTLEN + 1];
522   struct hostent*  hp = 0;
523
524   if (cptr->dns_reply) {
525     hp = cptr->dns_reply->hp;
526     if (!validate_hostent(hp))
527       hp = 0;
528   }
529   for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
530     if (aconf->status != CONF_CLIENT)
531       continue;
532     if (aconf->port && aconf->port != cptr->listener->port)
533       continue;
534     if (!aconf->host || !aconf->name)
535       continue;
536     if (hp) {
537       for (i = 0, hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
538         ircd_strncpy(fullname, hname, HOSTLEN);
539         fullname[HOSTLEN] = '\0';
540
541         Debug((DEBUG_DNS, "a_il: %s->%s", cptr->sockhost, fullname));
542
543         if (strchr(aconf->name, '@')) {
544           strcpy(uhost, cptr->username);
545           strcat(uhost, "@");
546         }
547         else
548           *uhost = '\0';
549         strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
550         uhost[sizeof(uhost) - 1] = 0;
551         if (0 == match(aconf->name, uhost)) {
552           if (strchr(uhost, '@'))
553             cptr->flags |= FLAGS_DOID;
554           return check_limit_and_attach(cptr, aconf);
555         }
556       }
557     }
558     if (strchr(aconf->host, '@')) {
559       ircd_strncpy(uhost, cptr->username, sizeof(uhost) - 2);
560       uhost[sizeof(uhost) - 2] = 0;
561       strcat(uhost, "@");
562     }
563     else
564       *uhost = '\0';
565     strncat(uhost, cptr->sock_ip, sizeof(uhost) - 1 - strlen(uhost));
566     uhost[sizeof(uhost) - 1] = 0;
567     if (match(aconf->host, uhost))
568       continue;
569     if (strchr(uhost, '@'))
570       cptr->flags |= FLAGS_DOID;
571
572     return check_limit_and_attach(cptr, aconf);
573   }
574   return ACR_NO_AUTHORIZATION;
575 }
576
577 /*
578  * detach_conf - Disassociate configuration from the client.
579  */
580 int detach_conf(struct Client *cptr, struct ConfItem *aconf)
581 {
582   struct SLink** lp;
583   struct SLink*  tmp;
584
585   assert(0 != aconf);
586   assert(0 != cptr);
587   assert(0 < aconf->clients);
588
589   lp = &(cptr->confs);
590
591   while (*lp) {
592     if ((*lp)->value.aconf == aconf) {
593       if (aconf->confClass && (aconf->status & CONF_CLIENT_MASK) && 
594           ConfLinks(aconf) > 0)
595         --ConfLinks(aconf);
596       if (0 == --aconf->clients && IsIllegal(aconf))
597         free_conf(aconf);
598       tmp = *lp;
599       *lp = tmp->next;
600       free_link(tmp);
601       return 0;
602     }
603     else
604       lp = &((*lp)->next);
605   }
606   return -1;
607 }
608
609 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
610 {
611   struct SLink *lp;
612
613   for (lp = cptr->confs; lp; lp = lp->next)
614     if (lp->value.aconf == aconf)
615       break;
616
617   return (lp) ? 1 : 0;
618 }
619
620 /*
621  * attach_conf
622  *
623  * Associate a specific configuration entry to a *local*
624  * client (this is the one which used in accepting the
625  * connection). Note, that this automaticly changes the
626  * attachment if there was an old one...
627  */
628 enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
629 {
630   struct SLink *lp;
631
632   if (is_attached(aconf, cptr))
633     return ACR_ALREADY_AUTHORIZED;
634   if (IsIllegal(aconf))
635     return ACR_NO_AUTHORIZATION;
636   if ((aconf->status & (CONF_LOCOP | CONF_OPERATOR | CONF_CLIENT)) &&
637       ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
638     return ACR_TOO_MANY_IN_CLASS;  /* Use this for printing error message */
639   lp = make_link();
640   lp->next = cptr->confs;
641   lp->value.aconf = aconf;
642   cptr->confs = lp;
643   ++aconf->clients;
644   if (aconf->status & CONF_CLIENT_MASK)
645     ConfLinks(aconf)++;
646   return ACR_OK;
647 }
648
649 struct ConfItem *find_admin(void)
650 {
651   struct ConfItem *aconf;
652
653   for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
654     if (aconf->status & CONF_ADMIN)
655       break;
656   }
657   return aconf;
658 }
659
660 struct ConfItem* find_me(void)
661 {
662   struct ConfItem* aconf;
663   for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
664     if (aconf->status & CONF_ME)
665       break;
666   }
667   return aconf;
668 }
669
670 /*
671  * attach_confs_byname
672  *
673  * Attach a CONF line to a client if the name passed matches that for
674  * the conf file (for non-C/N lines) or is an exact match (C/N lines
675  * only).  The difference in behaviour is to stop C:*::* and N:*::*.
676  */
677 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
678                                      int statmask)
679 {
680   struct ConfItem* tmp;
681   struct ConfItem* first = NULL;
682
683   assert(0 != name);
684
685   if (HOSTLEN < strlen(name))
686     return 0;
687
688   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
689     if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
690       assert(0 != tmp->name);
691       if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) { 
692         if (ACR_OK == attach_conf(cptr, tmp) && !first)
693           first = tmp;
694       }
695     }
696   }
697   return first;
698 }
699
700 /*
701  * Added for new access check    meLazy
702  */
703 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
704                                      int statmask)
705 {
706   struct ConfItem* tmp;
707   struct ConfItem* first = 0;
708
709   assert(0 != host);
710   if (HOSTLEN < strlen(host))
711     return 0;
712
713   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
714     if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
715       assert(0 != tmp->host);
716       if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) { 
717         if (ACR_OK == attach_conf(cptr, tmp) && !first)
718           first = tmp;
719       }
720     }
721   }
722   return first;
723 }
724
725 /*
726  * find a conf entry which matches the hostname and has the same name.
727  */
728 struct ConfItem* find_conf_exact(const char* name, const char* user,
729                                  const char* host, int statmask)
730 {
731   struct ConfItem *tmp;
732   char userhost[USERLEN + HOSTLEN + 3];
733
734   if (user)
735     ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
736   else
737     ircd_strncpy(userhost, host, sizeof(userhost) - 1);
738
739   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
740     if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
741         0 != ircd_strcmp(tmp->name, name))
742       continue;
743     /*
744      * Accept if the *real* hostname (usually sockecthost)
745      * socket host) matches *either* host or name field
746      * of the configuration.
747      */
748     if (match(tmp->host, userhost))
749       continue;
750     if (tmp->status & (CONF_OPERATOR | CONF_LOCOP)) {
751       if (tmp->clients < MaxLinks(tmp->confClass))
752         return tmp;
753       else
754         continue;
755     }
756     else
757       return tmp;
758   }
759   return 0;
760 }
761
762 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
763                                   int statmask)
764 {
765   struct ConfItem* tmp;
766   assert(0 != name);
767
768   if (HOSTLEN < strlen(name))
769     return 0;
770
771   for (; lp; lp = lp->next) {
772     tmp = lp->value.aconf;
773     if (0 != (tmp->status & statmask)) {
774       assert(0 != tmp->name);
775       if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
776         return tmp;
777     }
778   }
779   return 0;
780 }
781
782 /*
783  * Added for new access check    meLazy
784  */
785 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
786                                   int statmask)
787 {
788   struct ConfItem* tmp;
789   assert(0 != host);
790
791   if (HOSTLEN < strlen(host))
792     return 0;
793
794   for (; lp; lp = lp->next) {
795     tmp = lp->value.aconf;
796     if (0 != (tmp->status & statmask)) {
797       assert(0 != tmp->host);
798       if (0 == match(tmp->host, host))
799         return tmp;
800     }
801   }
802   return 0;
803 }
804
805 /*
806  * find_conf_ip
807  *
808  * Find a conf line using the IP# stored in it to search upon.
809  * Added 1/8/92 by Avalon.
810  */
811 struct ConfItem* find_conf_byip(struct SLink* lp, const char* ip, 
812                                 int statmask)
813 {
814   struct ConfItem* tmp;
815
816   for (; lp; lp = lp->next) {
817     tmp = lp->value.aconf;
818     if (0 != (tmp->status & statmask)) {
819       if (0 == memcmp(&tmp->ipnum, ip, sizeof(struct in_addr)))
820         return tmp;
821     }
822   }
823   return 0;
824 }
825
826 /*
827  * find_conf_entry
828  *
829  * - looks for a match on all given fields.
830  */
831 static struct ConfItem *find_conf_entry(struct ConfItem *aconf,
832                                         unsigned int mask)
833 {
834   struct ConfItem *bconf;
835   assert(0 != aconf);
836
837   mask &= ~CONF_ILLEGAL;
838
839   for (bconf = GlobalConfList; bconf; bconf = bconf->next) {
840     if (!(bconf->status & mask) || (bconf->port != aconf->port))
841       continue;
842
843     if ((EmptyString(bconf->host) && !EmptyString(aconf->host)) ||
844         (EmptyString(aconf->host) && !EmptyString(bconf->host)))
845       continue;
846     if (!EmptyString(bconf->host) && 0 != ircd_strcmp(bconf->host, aconf->host))
847       continue;
848
849     if ((EmptyString(bconf->passwd) && !EmptyString(aconf->passwd)) ||
850         (EmptyString(aconf->passwd) && !EmptyString(bconf->passwd)))
851       continue;
852     if (!EmptyString(bconf->passwd) && (!IsDigit(*bconf->passwd) || bconf->passwd[1])
853 #ifdef USEONE
854         && 0 != ircd_strcmp(bconf->passwd, "ONE")
855 #endif
856         && 0 != ircd_strcmp(bconf->passwd, aconf->passwd))
857       continue;
858
859     if ((EmptyString(bconf->name) && !EmptyString(aconf->name)) ||
860         (EmptyString(aconf->name) && !EmptyString(bconf->name)))
861       continue;
862     if (!EmptyString(bconf->name) && 0 != ircd_strcmp(bconf->name, aconf->name))
863       continue;
864     break;
865   }
866   return bconf;
867 }
868
869 /*
870  * rehash
871  *
872  * Actual REHASH service routine. Called with sig == 0 if it has been called
873  * as a result of an operator issuing this command, else assume it has been
874  * called as a result of the server receiving a HUP signal.
875  */
876 int rehash(struct Client *cptr, int sig)
877 {
878   struct ConfItem** tmp = &GlobalConfList;
879   struct ConfItem*  tmp2;
880   struct ConfClass* cltmp;
881   struct Client*    acptr;
882   struct MotdItem*  temp;
883   int               i;
884   int               ret = 0;
885   int               found_g = 0;
886
887   if (1 == sig)
888     sendto_opmask_butone(0, SNO_OLDSNO,
889                          "Got signal SIGHUP, reloading ircd conf. file");
890
891   while ((tmp2 = *tmp)) {
892     if (tmp2->clients) {
893       /*
894        * Configuration entry is still in use by some
895        * local clients, cannot delete it--mark it so
896        * that it will be deleted when the last client
897        * exits...
898        */
899       if (!(tmp2->status & CONF_CLIENT)) {
900         *tmp = tmp2->next;
901         tmp2->next = 0;
902       }
903       else
904         tmp = &tmp2->next;
905       tmp2->status |= CONF_ILLEGAL;
906     }
907     else {
908       *tmp = tmp2->next;
909       /* free expression trees of connect rules */
910       if ((tmp2->status & (CONF_CRULEALL | CONF_CRULEAUTO)) &&
911           (tmp2->passwd != NULL))
912         crule_free(&(tmp2->passwd));
913       free_conf(tmp2);
914     }
915   }
916
917   /*
918    * We don't delete the class table, rather mark all entries
919    * for deletion. The table is cleaned up by check_class(). - avalon
920    */
921   for (cltmp = NextClass(FirstClass()); cltmp; cltmp = NextClass(cltmp))
922     MarkDelete(cltmp);
923
924   /*
925    * delete the juped nicks list
926    */
927   clearNickJupes();
928
929   if (sig != 2)
930     flush_resolver_cache();
931
932   mark_listeners_closing();
933
934   if (!conf_init())        /* This calls check_class(), */
935     check_class();         /* unless it fails */
936
937   /*
938    * make sure that the server listener is re-added so it doesn't get
939    * closed
940    */
941   close_listeners();
942
943   /*
944    * Flush out deleted I and P lines although still in use.
945    */
946   for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
947     if (!(tmp2->status & CONF_ILLEGAL))
948       tmp = &tmp2->next;
949     else
950     {
951       *tmp = tmp2->next;
952       tmp2->next = NULL;
953       if (!tmp2->clients)
954         free_conf(tmp2);
955     }
956   }
957   for (i = 0; i <= HighestFd; i++) {
958     if ((acptr = LocalClientArray[i])) {
959       assert(!IsMe(acptr));
960       if (IsServer(acptr)) {
961         det_confs_butmask(acptr,
962             ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
963         attach_confs_byname(acptr, acptr->name,
964                             CONF_HUB | CONF_LEAF | CONF_UWORLD);
965       }
966       if ((found_g = find_kill(acptr))) {
967         sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
968                              found_g == -2 ? "G-line active for %s" :
969                              "K-line active for %s",
970                              get_client_name(acptr, HIDE_IP));
971         if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
972             "K-lined") == CPTR_KILLED)
973           ret = CPTR_KILLED;
974       }
975     }
976   }
977   /* 
978    * free old motd structs
979    */
980   while (motd) {
981     temp = motd->next;
982     MyFree(motd);
983     motd = temp;
984   }
985   while (rmotd) {
986     temp = rmotd->next;
987     MyFree(rmotd);
988     rmotd = temp;
989   }
990   /* reload motd files */
991   read_tlines();
992   rmotd = read_motd(RPATH);
993   motd = read_motd(MPATH);
994   return ret;
995 }
996
997 /*
998  * conf_init
999  *
1000  * Read configuration file.
1001  *
1002  * returns 0, if file cannot be opened
1003  *         1, if file read
1004  */
1005
1006 #define MAXCONFLINKS 150
1007
1008
1009 int conf_init(void)
1010 {
1011   static char quotes[9][2] = {
1012     {'b', '\b'},
1013     {'f', '\f'},
1014     {'n', '\n'},
1015     {'r', '\r'},
1016     {'t', '\t'},
1017     {'v', '\v'},
1018     {'\\', '\\'},
1019     {0, 0}
1020   };
1021   char *tmp, *s;
1022   FBFILE *file;
1023   int i;
1024   char line[512];
1025   int ccount = 0;
1026   struct ConfItem *aconf = 0;
1027
1028   Debug((DEBUG_DEBUG, "conf_init: ircd.conf = %s", configfile));
1029   if (0 == (file = fbopen(configfile, "r"))) {
1030     return 0;
1031   }
1032   while (fbgets(line, sizeof(line) - 1, file)) {
1033     if ((tmp = strchr(line, '\n')))
1034       *tmp = '\0';
1035     /*
1036      * Do quoting of characters and # detection.
1037      */
1038     for (tmp = line; *tmp; tmp++) {
1039       if (*tmp == '\\') {
1040         for (i = 0; quotes[i][0]; i++) {
1041           if (quotes[i][0] == *(tmp + 1)) {
1042             *tmp = quotes[i][1];
1043             break;
1044           }
1045         }
1046         if (!quotes[i][0])
1047           *tmp = *(tmp + 1);
1048         if (!*(tmp + 1))
1049           break;
1050         else {
1051           for (s = tmp; (*s = *(s + 1)); s++)
1052             ;
1053         }
1054       }
1055       else if (*tmp == '#')
1056         *tmp = '\0';
1057     }
1058     if (!*line || line[0] == '#' || line[0] == '\n' ||
1059         line[0] == ' ' || line[0] == '\t')
1060       continue;
1061     /* Could we test if it's conf line at all?      -Vesa */
1062     if (line[1] != ':') {
1063       Debug((DEBUG_ERROR, "Bad config line: %s", line));
1064       continue;
1065     }
1066     if (aconf)
1067       free_conf(aconf);
1068     aconf = make_conf();
1069
1070     tmp = getfield(line, ':');
1071     if (!tmp)
1072       continue;
1073     switch (*tmp) {
1074     case 'A':                /* Name, e-mail address of administrator */
1075     case 'a':                /* of this server. */
1076       aconf->status = CONF_ADMIN;
1077       break;
1078     case 'C':                /* Server where I should try to connect */
1079     case 'c':                /* in case of lp failures             */
1080       ++ccount;
1081       aconf->status = CONF_SERVER;
1082       break;
1083       /* Connect rule */
1084     case 'D':
1085       aconf->status = CONF_CRULEALL;
1086       break;
1087       /* Connect rule - autos only */
1088     case 'd':
1089       aconf->status = CONF_CRULEAUTO;
1090       break;
1091     case 'H':                /* Hub server line */
1092     case 'h':
1093       aconf->status = CONF_HUB;
1094       break;
1095     case 'I':                /* Just plain normal irc client trying  */
1096     case 'i':                /* to connect me */
1097       aconf->status = CONF_CLIENT;
1098       break;
1099     case 'K':                /* Kill user line on irc.conf           */
1100       aconf->status = CONF_KILL;
1101       break;
1102     case 'k':                /* Kill user line based on IP in ircd.conf */
1103       aconf->status = CONF_IPKILL;
1104       break;
1105       /* Operator. Line should contain at least */
1106       /* password and host where connection is  */
1107     case 'L':                /* guaranteed leaf server */
1108     case 'l':
1109       aconf->status = CONF_LEAF;
1110       break;
1111       /* Me. Host field is name used for this host */
1112       /* and port number is the number of the port */
1113     case 'M':
1114     case 'm':
1115       aconf->status = CONF_ME;
1116       break;
1117     case 'O':
1118       aconf->status = CONF_OPERATOR;
1119       break;
1120       /* Local Operator, (limited privs --SRB) */
1121     case 'o':
1122       aconf->status = CONF_LOCOP;
1123       break;
1124     case 'P':                /* listen port line */
1125     case 'p':
1126       aconf->status = CONF_LISTEN_PORT;
1127       break;
1128     case 'T':                /* print out different motd's */
1129     case 't':                /* based on hostmask */
1130       aconf->status = CONF_TLINES;
1131       break;
1132     case 'U':      /* Underworld server, allowed to hack modes */
1133     case 'u':      /* *Every* server on the net must define the same !!! */
1134       aconf->status = CONF_UWORLD;
1135       break;
1136     case 'Y':
1137     case 'y':
1138       aconf->status = CONF_CLASS;
1139       break;
1140     default:
1141       Debug((DEBUG_ERROR, "Error in config file: %s", line));
1142       break;
1143     }
1144     if (IsIllegal(aconf))
1145       continue;
1146
1147     for (;;) {            /* Fake loop, that I can use break here --msa */
1148       if ((tmp = getfield(NULL, ':')) == NULL)
1149         break;
1150       DupString(aconf->host, tmp);
1151       if ((tmp = getfield(NULL, (aconf->status == CONF_KILL
1152           || aconf->status == CONF_IPKILL) ? '"' : ':')) == NULL)
1153         break;
1154       DupString(aconf->passwd, tmp);
1155       if ((tmp = getfield(NULL, ':')) == NULL)
1156         break;
1157       DupString(aconf->name, tmp);
1158       if ((tmp = getfield(NULL, ':')) == NULL)
1159         break;
1160       aconf->port = atoi(tmp);
1161       tmp = getfield(NULL, ':');
1162       if (aconf->status & CONF_ME) {
1163         if (!tmp) {
1164           Debug((DEBUG_FATAL, "Your M: line must have the Numeric, "
1165               "assigned to you by routing-com, behind the port number!\n"));
1166           ircd_log(L_WARNING, "Your M: line must have the Numeric, "
1167               "assigned to you by routing-com, behind the port number!\n");
1168           exit(-1);
1169         }
1170         SetYXXServerName(&me, atoi(tmp));        /* Our Numeric Nick */
1171       }
1172       else if (tmp)
1173         aconf->confClass = find_class(atoi(tmp));
1174       break;
1175     }
1176     /*
1177      * If conf line is a class definition, create a class entry
1178      * for it and make the conf_line illegal and delete it.
1179      */
1180     if (aconf->status & CONF_CLASS) {
1181       add_class(atoi(aconf->host), atoi(aconf->passwd),
1182           atoi(aconf->name), aconf->port, tmp ? atoi(tmp) : 0);
1183       continue;
1184     }
1185     /*
1186      * Associate each conf line with a class by using a pointer
1187      * to the correct class record. -avalon
1188      */
1189     if (aconf->status & CONF_CLIENT_MASK) {
1190       if (aconf->confClass == 0)
1191         aconf->confClass = find_class(0);
1192     }
1193     if (aconf->status & CONF_LISTEN_PORT) {
1194       int         is_server = 0;
1195       int         is_hidden = 0;
1196       if (!EmptyString(aconf->name)) {
1197         const char* x = aconf->name;
1198         if ('S' == ToUpper(*x))
1199           is_server = 1;
1200         ++x;
1201         if ('H' == ToUpper(*x))
1202           is_hidden = 1;
1203       }
1204       add_listener(aconf->port, aconf->passwd, aconf->host, 
1205                    is_server, is_hidden);
1206       continue;
1207     } 
1208     if (aconf->status & CONF_CLIENT) {
1209       struct ConfItem *bconf;
1210
1211       if ((bconf = find_conf_entry(aconf, aconf->status))) {
1212         delist_conf(bconf);
1213         bconf->status &= ~CONF_ILLEGAL;
1214         if (aconf->status == CONF_CLIENT) {
1215           /*
1216            * copy the password field in case it changed
1217            */
1218           MyFree(bconf->passwd);
1219           bconf->passwd = aconf->passwd;
1220           aconf->passwd = 0;
1221
1222           ConfLinks(bconf) -= bconf->clients;
1223           bconf->confClass = aconf->confClass;
1224           if (bconf->confClass)
1225             ConfLinks(bconf) += bconf->clients;
1226         }
1227         free_conf(aconf);
1228         aconf = bconf;
1229       }
1230     }
1231     if (aconf->status & CONF_SERVER) {
1232       if (ccount > MAXCONFLINKS || !aconf->host || strchr(aconf->host, '*') ||
1233           strchr(aconf->host, '?') || !aconf->name)
1234         continue;
1235     }
1236     if (aconf->status & (CONF_LOCOP | CONF_OPERATOR)) {
1237       if (!strchr(aconf->host, '@')) {
1238         char* newhost;
1239         int len = 3;                /* *@\0 = 3 */
1240
1241         len += strlen(aconf->host);
1242         newhost = (char*) MyMalloc(len);
1243         assert(0 != newhost);
1244         ircd_snprintf(0, newhost, len, "*@%s", aconf->host);
1245         MyFree(aconf->host);
1246         aconf->host = newhost;
1247       }
1248     }
1249     if (aconf->status & CONF_SERVER) {
1250       if (EmptyString(aconf->passwd))
1251         continue;
1252       lookup_confhost(aconf);
1253     }
1254
1255     /* Create expression tree from connect rule...
1256      * If there's a parsing error, nuke the conf structure */
1257     if (aconf->status & (CONF_CRULEALL | CONF_CRULEAUTO)) {
1258       MyFree(aconf->passwd);
1259       if ((aconf->passwd = (char *)crule_parse(aconf->name)) == NULL) {
1260         free_conf(aconf);
1261         aconf = NULL;
1262         continue;
1263       }
1264     }
1265
1266     /*
1267      * Own port and name cannot be changed after the startup.
1268      * (or could be allowed, but only if all links are closed first).
1269      * Configuration info does not override the name and port
1270      * if previously defined. Note, that "info"-field can be
1271      * changed by "/rehash".
1272      */
1273     if (aconf->status == CONF_ME) {
1274       ircd_strncpy(me.info, aconf->name, REALLEN);
1275     }
1276
1277     /*
1278      * Juped nicks are listed in the 'password' field of U:lines,
1279      * the list is comma separated and might be empty and/or contain
1280      * empty elements... the only limit is that it MUST be shorter
1281      * than 512 chars, or they will be cutted out :)
1282      */
1283     if ((aconf->status == CONF_UWORLD) && (aconf->passwd) && (*aconf->passwd))
1284       addNickJupes(aconf->passwd);
1285
1286     if (aconf->status & CONF_ADMIN) {
1287       if (!aconf->host || !aconf->passwd || !aconf->name) {
1288         Debug((DEBUG_FATAL, "Your A: line must have 4 fields!\n"));
1289         ircd_log(L_WARNING, "Your A: line must have 4 fields!\n");
1290         exit(-1);
1291       }
1292     }
1293     collapse(aconf->host);
1294     collapse(aconf->name);
1295     Debug((DEBUG_NOTICE,
1296         "Read Init: (%d) (%s) (%s) (%s) (%u) (%p)",
1297         aconf->status, aconf->host, aconf->passwd,
1298         aconf->name, aconf->port, aconf->confClass));
1299     aconf->next = GlobalConfList;
1300     GlobalConfList = aconf;
1301     aconf = NULL;
1302   }
1303   if (aconf)
1304     free_conf(aconf);
1305   fbclose(file);
1306   check_class();
1307   nextping = nextconnect = CurrentTime;
1308   return 1;
1309 }
1310
1311 /* read_tlines 
1312  * Read info from T:lines into TRecords which include the file 
1313  * timestamp, the hostmask, and the contents of the motd file 
1314  * -Ghostwolf 7sep97
1315  */
1316 void read_tlines()
1317 {
1318   struct ConfItem *tmp;
1319   struct TRecord *temp;
1320   struct TRecord *last = NULL;        /* Init. to avoid compiler warning */
1321   struct MotdItem *amotd;
1322
1323   /* Free the old trecords and the associated motd contents first */
1324   while (tdata)
1325   {
1326     last = tdata->next;
1327     while (tdata->tmotd)
1328     {
1329       amotd = tdata->tmotd->next;
1330       MyFree(tdata->tmotd);
1331       tdata->tmotd = amotd;
1332     }
1333     MyFree(tdata);
1334     tdata = last;
1335   }
1336
1337   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1338     if (tmp->status == CONF_TLINES && tmp->host && tmp->passwd) {
1339       temp = (struct TRecord*) MyMalloc(sizeof(struct TRecord));
1340       assert(0 != temp);
1341
1342       temp->hostmask = tmp->host;
1343       temp->tmotd = read_motd(tmp->passwd);
1344       temp->tmotd_tm = motd_tm;
1345       temp->next = NULL;
1346       if (!tdata)
1347         tdata = temp;
1348       else
1349         last->next = temp;
1350       last = temp;
1351     }
1352   }
1353 }
1354
1355 int find_kill(struct Client *cptr)
1356 {
1357   char             reply[256];
1358   const char*      host;
1359   const char*      name;
1360   struct ConfItem* tmp;
1361   struct Gline*    agline = NULL;
1362
1363   assert(0 != cptr);
1364
1365   if (!cptr->user)
1366     return 0;
1367
1368   host = cptr->sockhost;
1369   name = cptr->user->username;
1370
1371 #if 0
1372   /*
1373    * whee :)
1374    * XXX - if this ever happens, we're already screwed
1375    */
1376   if (strlen(host) > HOSTLEN ||
1377       (name ? strlen(name) : 0) > HOSTLEN)
1378     return (0);
1379 #endif
1380
1381   reply[0] = '\0';
1382
1383   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1384     /* Added a check against the user's IP address as well.
1385      * If the line is either CONF_KILL or CONF_IPKILL, check it; if and only
1386      * if it's CONF_IPKILL, check the IP address as well (the && below will
1387      * short circuit and the match won't even get run) -Kev
1388      */
1389     if ((tmp->status & CONF_KLINE) && tmp->host && tmp->name &&
1390         (match(tmp->host, host) == 0 ||
1391         ((tmp->status == CONF_IPKILL) &&
1392         match(tmp->host, ircd_ntoa((const char*) &cptr->ip)) == 0)) &&
1393         (!name || match(tmp->name, name) == 0) &&
1394         (!tmp->port || (tmp->port == cptr->listener->port)))
1395     {
1396       /*
1397        * Can short-circuit evaluation - not taking chances
1398        * because check_time_interval destroys tmp->passwd
1399        * - Mmmm
1400        */
1401       if (EmptyString(tmp->passwd))
1402         break;
1403       else if (is_comment(tmp->passwd))
1404         break;
1405       else if (check_time_interval(tmp->passwd, reply, sizeof(reply)))
1406         break;
1407     }
1408   }
1409   if (reply[0])
1410     send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, reply);
1411   else if (tmp) {
1412     if (EmptyString(tmp->passwd))
1413       send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1414                  ":Connection from your host is refused on this server.");
1415     else {
1416       if (*tmp->passwd == '"') {
1417         send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%*s.",
1418                    strlen(tmp->passwd + 1) - 1, tmp->passwd + 1);
1419       }
1420       else if (*tmp->passwd == '!')
1421         killcomment(cptr, cptr->name, &tmp->passwd[1]);
1422       else
1423 #ifdef COMMENT_IS_FILE
1424         killcomment(cptr, cptr->name, tmp->passwd);
1425 #else
1426         send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1427                    tmp->passwd);
1428 #endif
1429     }
1430   }
1431
1432   /* find active glines */
1433   /* added a check against the user's IP address to find_gline() -Kev */
1434   else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline))
1435     send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1436                GlineReason(agline));
1437   else
1438     agline = NULL;                /* if a gline was found, it was inactive */
1439
1440   return (tmp ? -1 : (agline ? -2 : 0));
1441 }
1442
1443 struct MotdItem* read_motd(const char* motdfile)
1444 {
1445   FBFILE*          file = NULL;
1446   struct MotdItem* temp;
1447   struct MotdItem* newmotd;
1448   struct MotdItem* last;
1449   struct stat      sb;
1450   char             line[80];
1451   char*            tmp;
1452
1453   if (NULL == (file = fbopen(motdfile, "r"))) {
1454     Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motdfile, strerror(errno)));
1455     return NULL;
1456   }
1457   if (-1 == fbstat(&sb, file)) {
1458     fbclose(file);
1459     return NULL;
1460   }
1461   newmotd = last = NULL;
1462   motd_tm = *localtime((time_t *) & sb.st_mtime);        /* NetBSD needs cast */
1463   while (fbgets(line, sizeof(line) - 1, file)) {
1464     if ((tmp = (char *)strchr(line, '\n')))
1465       *tmp = '\0';
1466     if ((tmp = (char *)strchr(line, '\r')))
1467       *tmp = '\0';
1468     temp = (struct MotdItem*) MyMalloc(sizeof(struct MotdItem));
1469     assert(0 != temp);
1470     strcpy(temp->line, line);
1471     temp->next = NULL;
1472     if (!newmotd)
1473       newmotd = temp;
1474     else
1475       last->next = temp;
1476     last = temp;
1477   }
1478   fbclose(file);
1479   return newmotd;
1480 }
1481
1482
1483 /*
1484  * Ordinary client access check. Look for conf lines which have the same
1485  * status as the flags passed.
1486  */
1487 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1488 {
1489   enum AuthorizationCheckResult acr = ACR_OK;
1490
1491   ClearAccess(cptr);
1492
1493   if ((acr = attach_iline(cptr))) {
1494     Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]", 
1495           cptr->name, cptr->sockhost));
1496     return acr;
1497   }
1498   return ACR_OK;
1499 }
1500
1501 /*
1502  * check_server()
1503  *
1504  * Check access for a server given its name (passed in cptr struct).
1505  * Must check for all C/N lines which have a name which matches the
1506  * name given and a host which matches. A host alias which is the
1507  * same as the server name is also acceptable in the host field of a
1508  * C/N line.
1509  *
1510  * Returns
1511  *  0 = Success
1512  * -1 = Access denied
1513  * -2 = Bad socket.
1514  */
1515 int conf_check_server(struct Client *cptr)
1516 {
1517   struct ConfItem* c_conf = NULL;
1518   struct SLink*    lp;
1519
1520   Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]", 
1521         cptr->name, cptr->sockhost));
1522
1523   if (IsUnknown(cptr) && !attach_confs_byname(cptr, cptr->name, CONF_SERVER)) {
1524     Debug((DEBUG_DNS, "No C/N lines for %s", cptr->sockhost));
1525     return -1;
1526   }
1527   lp = cptr->confs;
1528   /*
1529    * We initiated this connection so the client should have a C and N
1530    * line already attached after passing through the connect_server()
1531    * function earlier.
1532    */
1533   if (IsConnecting(cptr) || IsHandshake(cptr)) {
1534     c_conf = find_conf_byname(lp, cptr->name, CONF_SERVER);
1535     if (!c_conf) {
1536       sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1537                            cptr->name);
1538       det_confs_butmask(cptr, 0);
1539       return -1;
1540     }
1541   }
1542
1543   ClearAccess(cptr);
1544
1545   if (!c_conf) {
1546     if (cptr->dns_reply) {
1547       int             i;
1548       struct hostent* hp = cptr->dns_reply->hp;
1549       const char*     name = hp->h_name;
1550       /*
1551        * If we are missing a C or N line from above, search for
1552        * it under all known hostnames we have for this ip#.
1553        */
1554       for (i = 0; name; name = hp->h_aliases[i++]) {
1555         if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1556           ircd_strncpy(cptr->sockhost, name, HOSTLEN);
1557           break;
1558         }
1559       }
1560       if (!c_conf) {
1561         for (i = 0; hp->h_addr_list[i]; i++) {
1562           if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1563             break;
1564         }
1565       }
1566     }
1567     else {
1568       /*
1569        * Check for C lines with the hostname portion the ip number
1570        * of the host the server runs on. This also checks the case where
1571        * there is a server connecting from 'localhost'.
1572        */
1573       c_conf = find_conf_byhost(lp, cptr->sockhost, CONF_SERVER);
1574     }
1575   }
1576   /*
1577    * Attach by IP# only if all other checks have failed.
1578    * It is quite possible to get here with the strange things that can
1579    * happen when using DNS in the way the irc server does. -avalon
1580    */
1581   if (!c_conf)
1582     c_conf = find_conf_byip(lp, (const char*) &cptr->ip, CONF_SERVER);
1583   /*
1584    * detach all conf lines that got attached by attach_confs()
1585    */
1586   det_confs_butmask(cptr, 0);
1587   /*
1588    * if no C or no N lines, then deny access
1589    */
1590   if (!c_conf) {
1591     Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1592           cptr->name, cptr->username, cptr->sockhost));
1593     return -1;
1594   }
1595   ircd_strncpy(cptr->name, c_conf->name, HOSTLEN);
1596   /*
1597    * attach the C and N lines to the client structure for later use.
1598    */
1599   attach_conf(cptr, c_conf);
1600   attach_confs_byname(cptr, cptr->name, CONF_HUB | CONF_LEAF | CONF_UWORLD);
1601
1602   if (INADDR_NONE == c_conf->ipnum.s_addr)
1603     c_conf->ipnum.s_addr = cptr->ip.s_addr;
1604
1605   Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cptr->name, cptr->sockhost));
1606   return 0;
1607 }
1608