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