83b0b0fae09bedb0d2b82dbc7e6729e63ef49b6a
[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 = NULL;
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       /* Because admin's are getting so uppity about people managing to
967        * get past K/G's etc, we'll "fix" the bug by actually explaining
968        * whats going on.
969        */
970       if ((found_g = find_kill(acptr))) {
971         sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
972                              found_g == -2 ? "G-line active for %s%s" :
973                              "K-line active for %s%s",
974                              IsUnknown(acptr) ? "Unregistered Client ":"",                   
975                              get_client_name(acptr, HIDE_IP));
976         if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
977             "K-lined") == CPTR_KILLED)
978           ret = CPTR_KILLED;
979       }
980     }
981   }
982   /* 
983    * free old motd structs
984    */
985   while (motd) {
986     temp = motd->next;
987     MyFree(motd);
988     motd = temp;
989   }
990   while (rmotd) {
991     temp = rmotd->next;
992     MyFree(rmotd);
993     rmotd = temp;
994   }
995   /* reload motd files */
996   read_tlines();
997   rmotd = read_motd(RPATH);
998   motd = read_motd(MPATH);
999   return ret;
1000 }
1001
1002 /*
1003  * conf_init
1004  *
1005  * Read configuration file.
1006  *
1007  * returns 0, if file cannot be opened
1008  *         1, if file read
1009  */
1010
1011 #define MAXCONFLINKS 150
1012
1013
1014 int conf_init(void)
1015 {
1016   static char quotes[9][2] = {
1017     {'b', '\b'},
1018     {'f', '\f'},
1019     {'n', '\n'},
1020     {'r', '\r'},
1021     {'t', '\t'},
1022     {'v', '\v'},
1023     {'\\', '\\'},
1024     {0, 0}
1025   };
1026   char *tmp, *s;
1027   FBFILE *file;
1028   int i;
1029   char line[512];
1030   int ccount = 0;
1031   struct ConfItem *aconf = 0;
1032
1033   Debug((DEBUG_DEBUG, "conf_init: ircd.conf = %s", configfile));
1034   if (0 == (file = fbopen(configfile, "r"))) {
1035     return 0;
1036   }
1037   while (fbgets(line, sizeof(line) - 1, file)) {
1038     if ((tmp = strchr(line, '\n')))
1039       *tmp = '\0';
1040     /*
1041      * Do quoting of characters and # detection.
1042      */
1043     for (tmp = line; *tmp; tmp++) {
1044       if (*tmp == '\\') {
1045         for (i = 0; quotes[i][0]; i++) {
1046           if (quotes[i][0] == *(tmp + 1)) {
1047             *tmp = quotes[i][1];
1048             break;
1049           }
1050         }
1051         if (!quotes[i][0])
1052           *tmp = *(tmp + 1);
1053         if (!*(tmp + 1))
1054           break;
1055         else {
1056           for (s = tmp; (*s = *(s + 1)); s++)
1057             ;
1058         }
1059       }
1060       else if (*tmp == '#')
1061         *tmp = '\0';
1062     }
1063     if (!*line || line[0] == '#' || line[0] == '\n' ||
1064         line[0] == ' ' || line[0] == '\t')
1065       continue;
1066     /* Could we test if it's conf line at all?      -Vesa */
1067     if (line[1] != ':') {
1068       Debug((DEBUG_ERROR, "Bad config line: %s", line));
1069       continue;
1070     }
1071     if (aconf)
1072       free_conf(aconf);
1073     aconf = make_conf();
1074
1075     tmp = getfield(line, ':');
1076     if (!tmp)
1077       continue;
1078     switch (*tmp) {
1079     case 'A':                /* Name, e-mail address of administrator */
1080     case 'a':                /* of this server. */
1081       aconf->status = CONF_ADMIN;
1082       break;
1083     case 'C':                /* Server where I should try to connect */
1084     case 'c':                /* in case of lp failures             */
1085       ++ccount;
1086       aconf->status = CONF_SERVER;
1087       break;
1088       /* Connect rule */
1089     case 'D':
1090       aconf->status = CONF_CRULEALL;
1091       break;
1092       /* Connect rule - autos only */
1093     case 'd':
1094       aconf->status = CONF_CRULEAUTO;
1095       break;
1096     case 'H':                /* Hub server line */
1097     case 'h':
1098       aconf->status = CONF_HUB;
1099       break;
1100     case 'I':                /* Just plain normal irc client trying  */
1101     case 'i':                /* to connect me */
1102       aconf->status = CONF_CLIENT;
1103       break;
1104     case 'K':                /* Kill user line on irc.conf           */
1105       aconf->status = CONF_KILL;
1106       break;
1107     case 'k':                /* Kill user line based on IP in ircd.conf */
1108       aconf->status = CONF_IPKILL;
1109       break;
1110       /* Operator. Line should contain at least */
1111       /* password and host where connection is  */
1112     case 'L':                /* guaranteed leaf server */
1113     case 'l':
1114       aconf->status = CONF_LEAF;
1115       break;
1116       /* Me. Host field is name used for this host */
1117       /* and port number is the number of the port */
1118     case 'M':
1119     case 'm':
1120       aconf->status = CONF_ME;
1121       break;
1122     case 'O':
1123       aconf->status = CONF_OPERATOR;
1124       break;
1125       /* Local Operator, (limited privs --SRB) */
1126     case 'o':
1127       aconf->status = CONF_LOCOP;
1128       break;
1129     case 'P':                /* listen port line */
1130     case 'p':
1131       aconf->status = CONF_LISTEN_PORT;
1132       break;
1133     case 'T':                /* print out different motd's */
1134     case 't':                /* based on hostmask */
1135       aconf->status = CONF_TLINES;
1136       break;
1137     case 'U':      /* Underworld server, allowed to hack modes */
1138     case 'u':      /* *Every* server on the net must define the same !!! */
1139       aconf->status = CONF_UWORLD;
1140       break;
1141     case 'Y':
1142     case 'y':
1143       aconf->status = CONF_CLASS;
1144       break;
1145     default:
1146       Debug((DEBUG_ERROR, "Error in config file: %s", line));
1147       break;
1148     }
1149     if (IsIllegal(aconf))
1150       continue;
1151
1152     for (;;) {            /* Fake loop, that I can use break here --msa */
1153       if ((tmp = getfield(NULL, ':')) == NULL)
1154         break;
1155       DupString(aconf->host, tmp);
1156       if ((tmp = getfield(NULL, (aconf->status == CONF_KILL
1157           || aconf->status == CONF_IPKILL) ? '"' : ':')) == NULL)
1158         break;
1159       DupString(aconf->passwd, tmp);
1160       if ((tmp = getfield(NULL, ':')) == NULL)
1161         break;
1162       DupString(aconf->name, tmp);
1163       if ((tmp = getfield(NULL, ':')) == NULL)
1164         break;
1165       aconf->port = atoi(tmp);
1166       tmp = getfield(NULL, ':');
1167       if (aconf->status & CONF_ME) {
1168         if (!tmp) {
1169           Debug((DEBUG_FATAL, "Your M: line must have the Numeric, "
1170               "assigned to you by routing-com, behind the port number!\n"));
1171           ircd_log(L_WARNING, "Your M: line must have the Numeric, "
1172               "assigned to you by routing-com, behind the port number!\n");
1173           exit(-1);
1174         }
1175         SetYXXServerName(&me, atoi(tmp));        /* Our Numeric Nick */
1176       }
1177       else if (tmp)
1178         aconf->confClass = find_class(atoi(tmp));
1179       break;
1180     }
1181     /*
1182      * If conf line is a class definition, create a class entry
1183      * for it and make the conf_line illegal and delete it.
1184      */
1185     if (aconf->status & CONF_CLASS) {
1186       add_class(atoi(aconf->host), atoi(aconf->passwd),
1187           atoi(aconf->name), aconf->port, tmp ? atoi(tmp) : 0);
1188       continue;
1189     }
1190     /*
1191      * Associate each conf line with a class by using a pointer
1192      * to the correct class record. -avalon
1193      */
1194     if (aconf->status & CONF_CLIENT_MASK) {
1195       if (aconf->confClass == 0)
1196         aconf->confClass = find_class(0);
1197     }
1198     if (aconf->status & CONF_LISTEN_PORT) {
1199       int         is_server = 0;
1200       int         is_hidden = 0;
1201       if (!EmptyString(aconf->name)) {
1202         const char* x = aconf->name;
1203         if ('S' == ToUpper(*x))
1204           is_server = 1;
1205         ++x;
1206         if ('H' == ToUpper(*x))
1207           is_hidden = 1;
1208       }
1209       add_listener(aconf->port, aconf->passwd, aconf->host, 
1210                    is_server, is_hidden);
1211       continue;
1212     } 
1213     if (aconf->status & CONF_CLIENT) {
1214       struct ConfItem *bconf;
1215
1216       if ((bconf = find_conf_entry(aconf, aconf->status))) {
1217         delist_conf(bconf);
1218         bconf->status &= ~CONF_ILLEGAL;
1219         if (aconf->status == CONF_CLIENT) {
1220           /*
1221            * copy the password field in case it changed
1222            */
1223           MyFree(bconf->passwd);
1224           bconf->passwd = aconf->passwd;
1225           aconf->passwd = 0;
1226
1227           ConfLinks(bconf) -= bconf->clients;
1228           bconf->confClass = aconf->confClass;
1229           if (bconf->confClass)
1230             ConfLinks(bconf) += bconf->clients;
1231         }
1232         free_conf(aconf);
1233         aconf = bconf;
1234       }
1235     }
1236     if (aconf->status & CONF_SERVER) {
1237       if (ccount > MAXCONFLINKS || !aconf->host || strchr(aconf->host, '*') ||
1238           strchr(aconf->host, '?') || !aconf->name)
1239         continue;
1240     }
1241     if (aconf->status & (CONF_LOCOP | CONF_OPERATOR)) {
1242       if (!strchr(aconf->host, '@')) {
1243         char* newhost;
1244         int len = 3;                /* *@\0 = 3 */
1245
1246         len += strlen(aconf->host);
1247         newhost = (char*) MyMalloc(len);
1248         assert(0 != newhost);
1249         ircd_snprintf(0, newhost, len, "*@%s", aconf->host);
1250         MyFree(aconf->host);
1251         aconf->host = newhost;
1252       }
1253     }
1254     if (aconf->status & CONF_SERVER) {
1255       if (EmptyString(aconf->passwd))
1256         continue;
1257       lookup_confhost(aconf);
1258     }
1259
1260     /* Create expression tree from connect rule...
1261      * If there's a parsing error, nuke the conf structure */
1262     if (aconf->status & (CONF_CRULEALL | CONF_CRULEAUTO)) {
1263       MyFree(aconf->passwd);
1264       if ((aconf->passwd = (char *)crule_parse(aconf->name)) == NULL) {
1265         free_conf(aconf);
1266         aconf = NULL;
1267         continue;
1268       }
1269     }
1270
1271     /*
1272      * Own port and name cannot be changed after the startup.
1273      * (or could be allowed, but only if all links are closed first).
1274      * Configuration info does not override the name and port
1275      * if previously defined. Note, that "info"-field can be
1276      * changed by "/rehash".
1277      */
1278     if (aconf->status == CONF_ME) {
1279       ircd_strncpy(me.info, aconf->name, REALLEN);
1280     }
1281
1282     /*
1283      * Juped nicks are listed in the 'password' field of U:lines,
1284      * the list is comma separated and might be empty and/or contain
1285      * empty elements... the only limit is that it MUST be shorter
1286      * than 512 chars, or they will be cutted out :)
1287      */
1288     if ((aconf->status == CONF_UWORLD) && (aconf->passwd) && (*aconf->passwd))
1289       addNickJupes(aconf->passwd);
1290
1291     if (aconf->status & CONF_ADMIN) {
1292       if (!aconf->host || !aconf->passwd || !aconf->name) {
1293         Debug((DEBUG_FATAL, "Your A: line must have 4 fields!\n"));
1294         ircd_log(L_WARNING, "Your A: line must have 4 fields!\n");
1295         exit(-1);
1296       }
1297     }
1298     collapse(aconf->host);
1299     collapse(aconf->name);
1300     Debug((DEBUG_NOTICE,
1301         "Read Init: (%d) (%s) (%s) (%s) (%u) (%p)",
1302         aconf->status, aconf->host, aconf->passwd,
1303         aconf->name, aconf->port, aconf->confClass));
1304     aconf->next = GlobalConfList;
1305     GlobalConfList = aconf;
1306     aconf = NULL;
1307   }
1308   if (aconf)
1309     free_conf(aconf);
1310   fbclose(file);
1311   check_class();
1312   nextping = nextconnect = CurrentTime;
1313   return 1;
1314 }
1315
1316 /* read_tlines 
1317  * Read info from T:lines into TRecords which include the file 
1318  * timestamp, the hostmask, and the contents of the motd file 
1319  * -Ghostwolf 7sep97
1320  */
1321 void read_tlines()
1322 {
1323   struct ConfItem *tmp;
1324   struct TRecord *temp;
1325   struct TRecord *last = NULL;        /* Init. to avoid compiler warning */
1326   struct MotdItem *amotd;
1327
1328   /* Free the old trecords and the associated motd contents first */
1329   while (tdata)
1330   {
1331     last = tdata->next;
1332     while (tdata->tmotd)
1333     {
1334       amotd = tdata->tmotd->next;
1335       MyFree(tdata->tmotd);
1336       tdata->tmotd = amotd;
1337     }
1338     MyFree(tdata);
1339     tdata = last;
1340   }
1341
1342   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1343     if (tmp->status == CONF_TLINES && tmp->host && tmp->passwd) {
1344       temp = (struct TRecord*) MyMalloc(sizeof(struct TRecord));
1345       assert(0 != temp);
1346
1347       temp->hostmask = tmp->host;
1348       temp->tmotd = read_motd(tmp->passwd);
1349       temp->tmotd_tm = motd_tm;
1350       temp->next = NULL;
1351       if (!tdata)
1352         tdata = temp;
1353       else
1354         last->next = temp;
1355       last = temp;
1356     }
1357   }
1358 }
1359
1360 int find_kill(struct Client *cptr)
1361 {
1362   char             reply[256];
1363   const char*      host;
1364   const char*      name;
1365   struct ConfItem* tmp;
1366   struct Gline*    agline = NULL;
1367
1368   assert(0 != cptr);
1369
1370   if (!cptr->user)
1371     return 0;
1372
1373   host = cptr->sockhost;
1374   name = cptr->user->username;
1375
1376 #if 0
1377   /*
1378    * whee :)
1379    * XXX - if this ever happens, we're already screwed
1380    */
1381   if (strlen(host) > HOSTLEN ||
1382       (name ? strlen(name) : 0) > HOSTLEN)
1383     return (0);
1384 #endif
1385
1386   reply[0] = '\0';
1387
1388   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1389     /* Added a check against the user's IP address as well.
1390      * If the line is either CONF_KILL or CONF_IPKILL, check it; if and only
1391      * if it's CONF_IPKILL, check the IP address as well (the && below will
1392      * short circuit and the match won't even get run) -Kev
1393      */
1394     if ((tmp->status & CONF_KLINE) && tmp->host && tmp->name &&
1395         (match(tmp->host, host) == 0 ||
1396         ((tmp->status == CONF_IPKILL) &&
1397         match(tmp->host, ircd_ntoa((const char*) &cptr->ip)) == 0)) &&
1398         (!name || match(tmp->name, name) == 0) &&
1399         (!tmp->port || (tmp->port == cptr->listener->port)))
1400     {
1401       /*
1402        * Can short-circuit evaluation - not taking chances
1403        * because check_time_interval destroys tmp->passwd
1404        * - Mmmm
1405        */
1406       if (EmptyString(tmp->passwd))
1407         break;
1408       else if (is_comment(tmp->passwd))
1409         break;
1410       else if (check_time_interval(tmp->passwd, reply, sizeof(reply)))
1411         break;
1412     }
1413   }
1414   if (reply[0])
1415     send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, reply);
1416   else if (tmp) {
1417     if (EmptyString(tmp->passwd))
1418       send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1419                  ":Connection from your host is refused on this server.");
1420     else {
1421       if (*tmp->passwd == '"') {
1422         send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%*s.",
1423                    strlen(tmp->passwd + 1) - 1, tmp->passwd + 1);
1424       }
1425       else if (*tmp->passwd == '!')
1426         killcomment(cptr, cptr->name, &tmp->passwd[1]);
1427       else
1428 #ifdef COMMENT_IS_FILE
1429         killcomment(cptr, cptr->name, tmp->passwd);
1430 #else
1431         send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1432                    tmp->passwd);
1433 #endif
1434     }
1435   }
1436
1437   /* find active glines */
1438   /* added a check against the user's IP address to find_gline() -Kev */
1439   else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline))
1440     send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1441                GlineReason(agline));
1442   else
1443     agline = NULL;                /* if a gline was found, it was inactive */
1444
1445   return (tmp ? -1 : (agline ? -2 : 0));
1446 }
1447
1448 struct MotdItem* read_motd(const char* motdfile)
1449 {
1450   FBFILE*          file = NULL;
1451   struct MotdItem* temp;
1452   struct MotdItem* newmotd;
1453   struct MotdItem* last;
1454   struct stat      sb;
1455   char             line[80];
1456   char*            tmp;
1457
1458   if (NULL == (file = fbopen(motdfile, "r"))) {
1459     Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motdfile, strerror(errno)));
1460     return NULL;
1461   }
1462   if (-1 == fbstat(&sb, file)) {
1463     fbclose(file);
1464     return NULL;
1465   }
1466   newmotd = last = NULL;
1467   motd_tm = *localtime((time_t *) & sb.st_mtime);        /* NetBSD needs cast */
1468   while (fbgets(line, sizeof(line) - 1, file)) {
1469     if ((tmp = (char *)strchr(line, '\n')))
1470       *tmp = '\0';
1471     if ((tmp = (char *)strchr(line, '\r')))
1472       *tmp = '\0';
1473     temp = (struct MotdItem*) MyMalloc(sizeof(struct MotdItem));
1474     assert(0 != temp);
1475     strcpy(temp->line, line);
1476     temp->next = NULL;
1477     if (!newmotd)
1478       newmotd = temp;
1479     else
1480       last->next = temp;
1481     last = temp;
1482   }
1483   fbclose(file);
1484   return newmotd;
1485 }
1486
1487
1488 /*
1489  * Ordinary client access check. Look for conf lines which have the same
1490  * status as the flags passed.
1491  */
1492 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1493 {
1494   enum AuthorizationCheckResult acr = ACR_OK;
1495
1496   ClearAccess(cptr);
1497
1498   if ((acr = attach_iline(cptr))) {
1499     Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]", 
1500           cptr->name, cptr->sockhost));
1501     return acr;
1502   }
1503   return ACR_OK;
1504 }
1505
1506 /*
1507  * check_server()
1508  *
1509  * Check access for a server given its name (passed in cptr struct).
1510  * Must check for all C/N lines which have a name which matches the
1511  * name given and a host which matches. A host alias which is the
1512  * same as the server name is also acceptable in the host field of a
1513  * C/N line.
1514  *
1515  * Returns
1516  *  0 = Success
1517  * -1 = Access denied
1518  * -2 = Bad socket.
1519  */
1520 int conf_check_server(struct Client *cptr)
1521 {
1522   struct ConfItem* c_conf = NULL;
1523   struct SLink*    lp;
1524
1525   Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]", 
1526         cptr->name, cptr->sockhost));
1527
1528   if (IsUnknown(cptr) && !attach_confs_byname(cptr, cptr->name, CONF_SERVER)) {
1529     Debug((DEBUG_DNS, "No C/N lines for %s", cptr->sockhost));
1530     return -1;
1531   }
1532   lp = cptr->confs;
1533   /*
1534    * We initiated this connection so the client should have a C and N
1535    * line already attached after passing through the connect_server()
1536    * function earlier.
1537    */
1538   if (IsConnecting(cptr) || IsHandshake(cptr)) {
1539     c_conf = find_conf_byname(lp, cptr->name, CONF_SERVER);
1540     if (!c_conf) {
1541       sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1542                            cptr->name);
1543       det_confs_butmask(cptr, 0);
1544       return -1;
1545     }
1546   }
1547
1548   ClearAccess(cptr);
1549
1550   if (!c_conf) {
1551     if (cptr->dns_reply) {
1552       int             i;
1553       struct hostent* hp = cptr->dns_reply->hp;
1554       const char*     name = hp->h_name;
1555       /*
1556        * If we are missing a C or N line from above, search for
1557        * it under all known hostnames we have for this ip#.
1558        */
1559       for (i = 0; name; name = hp->h_aliases[i++]) {
1560         if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1561           ircd_strncpy(cptr->sockhost, name, HOSTLEN);
1562           break;
1563         }
1564       }
1565       if (!c_conf) {
1566         for (i = 0; hp->h_addr_list[i]; i++) {
1567           if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1568             break;
1569         }
1570       }
1571     }
1572     else {
1573       /*
1574        * Check for C lines with the hostname portion the ip number
1575        * of the host the server runs on. This also checks the case where
1576        * there is a server connecting from 'localhost'.
1577        */
1578       c_conf = find_conf_byhost(lp, cptr->sockhost, CONF_SERVER);
1579     }
1580   }
1581   /*
1582    * Attach by IP# only if all other checks have failed.
1583    * It is quite possible to get here with the strange things that can
1584    * happen when using DNS in the way the irc server does. -avalon
1585    */
1586   if (!c_conf)
1587     c_conf = find_conf_byip(lp, (const char*) &cptr->ip, CONF_SERVER);
1588   /*
1589    * detach all conf lines that got attached by attach_confs()
1590    */
1591   det_confs_butmask(cptr, 0);
1592   /*
1593    * if no C or no N lines, then deny access
1594    */
1595   if (!c_conf) {
1596     Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1597           cptr->name, cptr->username, cptr->sockhost));
1598     return -1;
1599   }
1600   ircd_strncpy(cptr->name, c_conf->name, HOSTLEN);
1601   /*
1602    * attach the C and N lines to the client structure for later use.
1603    */
1604   attach_conf(cptr, c_conf);
1605   attach_confs_byname(cptr, cptr->name, CONF_HUB | CONF_LEAF | CONF_UWORLD);
1606
1607   if (INADDR_NONE == c_conf->ipnum.s_addr)
1608     c_conf->ipnum.s_addr = cptr->ip.s_addr;
1609
1610   Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cptr->name, cptr->sockhost));
1611   return 0;
1612 }
1613