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   nextping = nextconnect = CurrentTime;
1222   return 1;
1223 }
1224
1225 /*
1226  * rehash
1227  *
1228  * Actual REHASH service routine. Called with sig == 0 if it has been called
1229  * as a result of an operator issuing this command, else assume it has been
1230  * called as a result of the server receiving a HUP signal.
1231  */
1232 int rehash(struct Client *cptr, int sig)
1233 {
1234   struct ConfItem** tmp = &GlobalConfList;
1235   struct ConfItem*  tmp2;
1236   struct Client*    acptr;
1237   struct MotdItem*  temp;
1238   int               i;
1239   int               ret = 0;
1240   int               found_g = 0;
1241
1242   if (1 == sig)
1243     sendto_opmask_butone(0, SNO_OLDSNO,
1244                          "Got signal SIGHUP, reloading ircd conf. file");
1245
1246   while ((tmp2 = *tmp)) {
1247     if (tmp2->clients) {
1248       /*
1249        * Configuration entry is still in use by some
1250        * local clients, cannot delete it--mark it so
1251        * that it will be deleted when the last client
1252        * exits...
1253        */
1254       if (CONF_CLIENT == (tmp2->status & CONF_CLIENT))
1255         tmp = &tmp2->next;
1256       else {
1257         *tmp = tmp2->next;
1258         tmp2->next = 0;
1259       }
1260       tmp2->status |= CONF_ILLEGAL;
1261     }
1262     else {
1263       *tmp = tmp2->next;
1264       free_conf(tmp2);
1265     }
1266   }
1267   conf_erase_motd_list(&motdConfList);
1268   conf_erase_crule_list();
1269
1270   /*
1271    * delete the juped nicks list
1272    */
1273   clearNickJupes();
1274
1275   if (sig != 2)
1276     flush_resolver_cache();
1277
1278   class_mark_delete();
1279   mark_listeners_closing();
1280
1281   read_configuration_file();
1282
1283   close_listeners();
1284   class_delete_marked();         /* unless it fails */
1285
1286   /*
1287    * Flush out deleted I and P lines although still in use.
1288    */
1289   for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
1290     if (CONF_ILLEGAL == (tmp2->status & CONF_ILLEGAL)) {
1291       *tmp = tmp2->next;
1292       tmp2->next = NULL;
1293       if (!tmp2->clients)
1294         free_conf(tmp2);
1295     }
1296     else
1297       tmp = &tmp2->next;
1298   }
1299   for (i = 0; i <= HighestFd; i++) {
1300     if ((acptr = LocalClientArray[i])) {
1301       assert(!IsMe(acptr));
1302       if (IsServer(acptr)) {
1303         det_confs_butmask(acptr,
1304             ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
1305         attach_confs_byname(acptr, acptr->name,
1306                             CONF_HUB | CONF_LEAF | CONF_UWORLD);
1307       }
1308       /* Because admin's are getting so uppity about people managing to
1309        * get past K/G's etc, we'll "fix" the bug by actually explaining
1310        * whats going on.
1311        */
1312       if ((found_g = find_kill(acptr))) {
1313         sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
1314                              found_g == -2 ? "G-line active for %s%s" :
1315                              "K-line active for %s%s",
1316                              IsUnknown(acptr) ? "Unregistered Client ":"",                   
1317                              get_client_name(acptr, HIDE_IP));
1318         if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
1319             "K-lined") == CPTR_KILLED)
1320           ret = CPTR_KILLED;
1321       }
1322     }
1323   }
1324   /* 
1325    * free old motd structs
1326    */
1327   while (motd) {
1328     temp = motd->next;
1329     MyFree(motd);
1330     motd = temp;
1331   }
1332   while (rmotd) {
1333     temp = rmotd->next;
1334     MyFree(rmotd);
1335     rmotd = temp;
1336   }
1337   /* reload motd files */
1338   read_tlines();
1339   rmotd = read_motd(RPATH);
1340   motd = read_motd(MPATH);
1341   return ret;
1342 }
1343
1344 /*
1345  * init_conf
1346  *
1347  * Read configuration file.
1348  *
1349  * returns 0, if file cannot be opened
1350  *         1, if file read
1351  */
1352
1353 int init_conf(void)
1354 {
1355   if (read_configuration_file()) {
1356     /*
1357      * make sure we're sane to start if the config
1358      * file read didn't get everything we need.
1359      * XXX - should any of these abort the server?
1360      * TODO: add warning messages
1361      */
1362     if (0 == localConf.name || 0 == localConf.numeric)
1363       return 0;
1364
1365     if (0 == localConf.location1)
1366       DupString(localConf.location1, "");
1367     if (0 == localConf.location2)
1368       DupString(localConf.location2, "");
1369     if (0 == localConf.contact)
1370       DupString(localConf.contact, "");
1371     
1372     return 1;
1373   }
1374   return 0;
1375 }
1376
1377
1378 /* read_tlines 
1379  * Read info from T:lines into TRecords which include the file 
1380  * timestamp, the hostmask, and the contents of the motd file 
1381  * -Ghostwolf 7sep97
1382  */
1383 void read_tlines()
1384 {
1385   struct MotdConf* conf;
1386   struct TRecord *temp;
1387   struct TRecord *last = NULL;        /* Init. to avoid compiler warning */
1388   struct MotdItem *amotd;
1389
1390   /* Free the old trecords and the associated motd contents first */
1391   while (tdata) {
1392     last = tdata->next;
1393     while (tdata->tmotd) {
1394       amotd = tdata->tmotd->next;
1395       MyFree(tdata->tmotd);
1396       tdata->tmotd = amotd;
1397     }
1398     MyFree(tdata);
1399     tdata = last;
1400   }
1401
1402   for (conf = motdConfList; conf; conf = conf->next) {
1403     temp = (struct TRecord*) MyMalloc(sizeof(struct TRecord));
1404     assert(0 != temp);
1405
1406     temp->hostmask = conf->hostmask;
1407     temp->tmotd = read_motd(conf->path);
1408     temp->tmotd_tm = motd_tm;
1409     temp->next = 0;
1410
1411     if (!tdata)
1412       tdata = temp;
1413     else
1414       last->next = temp;
1415     last = temp;
1416   }
1417 }
1418
1419 /*
1420  * find_kill
1421  * input:
1422  *  client pointer
1423  * returns:
1424  *  0: Client may continue to try and connect
1425  * -1: Client was K/k:'d - sideeffect: reason was sent.
1426  * -2: Client was G/g:'d - sideeffect: reason was sent.
1427  * sideeffects:
1428  *  Client may have been sent a reason why they are denied, as above.
1429  */
1430 int find_kill(struct Client *cptr)
1431 {
1432   const char*      host;
1433   const char*      name;
1434   struct ConfItem* tmp;
1435   struct Gline*    agline = NULL;
1436
1437   assert(0 != cptr);
1438
1439   if (!cptr->user)
1440     return 0;
1441
1442   host = cptr->sockhost;
1443   name = cptr->user->username;
1444
1445   assert(strlen(host) <= HOSTLEN);
1446   assert((name ? strlen(name) : 0) <= HOSTLEN);
1447   
1448   /* 2000-07-14: Rewrote this loop for massive speed increases.
1449    *             -- Isomer
1450    */
1451   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1452   
1453     if ((tmp->status & CONF_KLINE) == 0)
1454         continue;
1455         
1456     /*
1457      * What is this for?  You can K: by port?!
1458      *   -- Isomer
1459      */
1460     if (tmp->port && tmp->port != cptr->listener->port)
1461         continue;
1462
1463     if (!tmp->name || match(tmp->name, name) != 0)
1464         continue;
1465         
1466     if (!tmp->host)
1467         break;
1468     
1469     if (tmp->status != CONF_IPKILL) {
1470         if (match(tmp->host, host) == 0)
1471           break;
1472     }
1473     else { /* k: by IP */
1474         Debug((DEBUG_DEBUG, "ip: %08x network: %08x/%i mask: %08x",
1475                 cptr->ip.s_addr, tmp->ipnum.s_addr, tmp->bits, 
1476                 NETMASK(tmp->bits)));
1477         if ((cptr->ip.s_addr & NETMASK(tmp->bits)) == tmp->ipnum.s_addr)
1478                 break;
1479     }
1480   }
1481   if (tmp) {
1482     if (EmptyString(tmp->passwd))
1483       send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1484                  ":Connection from your host is refused on this server.");
1485     else {
1486       if (*tmp->passwd == '"') {
1487         send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%*s.",
1488                    strlen(tmp->passwd + 1) - 1, tmp->passwd + 1);
1489       }
1490       else if (*tmp->passwd == '!')
1491         killcomment(cptr, cptr->name, &tmp->passwd[1]);
1492       else
1493 #ifdef COMMENT_IS_FILE
1494         killcomment(cptr, cptr->name, tmp->passwd);
1495 #else
1496         send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1497                    tmp->passwd);
1498 #endif
1499     }
1500   }
1501
1502   /* find active glines */
1503   /* added a check against the user's IP address to find_gline() -Kev */
1504   else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline))
1505     send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1506                GlineReason(agline));
1507   else
1508     agline = NULL;                /* if a gline was found, it was inactive */
1509
1510   if (tmp)
1511     return -1;
1512   if (agline)
1513     return -2;
1514     
1515   return 0;
1516 }
1517
1518 struct MotdItem* read_motd(const char* motdfile)
1519 {
1520   FBFILE*          file = NULL;
1521   struct MotdItem* temp;
1522   struct MotdItem* newmotd;
1523   struct MotdItem* last;
1524   struct stat      sb;
1525   char             line[80];
1526   char*            tmp;
1527
1528   if (NULL == (file = fbopen(motdfile, "r"))) {
1529     Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motdfile, strerror(errno)));
1530     return NULL;
1531   }
1532   if (-1 == fbstat(&sb, file)) {
1533     fbclose(file);
1534     return NULL;
1535   }
1536   newmotd = last = NULL;
1537   motd_tm = *localtime((time_t *) & sb.st_mtime);        /* NetBSD needs cast */
1538   while (fbgets(line, sizeof(line) - 1, file)) {
1539     if ((tmp = (char *)strchr(line, '\n')))
1540       *tmp = '\0';
1541     if ((tmp = (char *)strchr(line, '\r')))
1542       *tmp = '\0';
1543     temp = (struct MotdItem*) MyMalloc(sizeof(struct MotdItem));
1544     assert(0 != temp);
1545     strcpy(temp->line, line);
1546     temp->next = NULL;
1547     if (!newmotd)
1548       newmotd = temp;
1549     else
1550       last->next = temp;
1551     last = temp;
1552   }
1553   fbclose(file);
1554   return newmotd;
1555 }
1556
1557
1558 /*
1559  * Ordinary client access check. Look for conf lines which have the same
1560  * status as the flags passed.
1561  */
1562 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1563 {
1564   enum AuthorizationCheckResult acr = ACR_OK;
1565
1566   ClearAccess(cptr);
1567
1568   if ((acr = attach_iline(cptr))) {
1569     Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]", 
1570           cptr->name, cptr->sockhost));
1571     return acr;
1572   }
1573   return ACR_OK;
1574 }
1575
1576 /*
1577  * check_server()
1578  *
1579  * Check access for a server given its name (passed in cptr struct).
1580  * Must check for all C/N lines which have a name which matches the
1581  * name given and a host which matches. A host alias which is the
1582  * same as the server name is also acceptable in the host field of a
1583  * C/N line.
1584  *
1585  * Returns
1586  *  0 = Success
1587  * -1 = Access denied
1588  * -2 = Bad socket.
1589  */
1590 int conf_check_server(struct Client *cptr)
1591 {
1592   struct ConfItem* c_conf = NULL;
1593   struct SLink*    lp;
1594
1595   Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]", 
1596         cptr->name, cptr->sockhost));
1597
1598   if (IsUnknown(cptr) && !attach_confs_byname(cptr, cptr->name, CONF_SERVER)) {
1599     Debug((DEBUG_DNS, "No C/N lines for %s", cptr->sockhost));
1600     return -1;
1601   }
1602   lp = cptr->confs;
1603   /*
1604    * We initiated this connection so the client should have a C and N
1605    * line already attached after passing through the connect_server()
1606    * function earlier.
1607    */
1608   if (IsConnecting(cptr) || IsHandshake(cptr)) {
1609     c_conf = find_conf_byname(lp, cptr->name, CONF_SERVER);
1610     if (!c_conf) {
1611       sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1612                            cptr->name);
1613       det_confs_butmask(cptr, 0);
1614       return -1;
1615     }
1616   }
1617
1618   ClearAccess(cptr);
1619
1620   if (!c_conf) {
1621     if (cptr->dns_reply) {
1622       int             i;
1623       struct hostent* hp = cptr->dns_reply->hp;
1624       const char*     name = hp->h_name;
1625       /*
1626        * If we are missing a C or N line from above, search for
1627        * it under all known hostnames we have for this ip#.
1628        */
1629       for (i = 0; name; name = hp->h_aliases[i++]) {
1630         if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1631           ircd_strncpy(cptr->sockhost, name, HOSTLEN);
1632           break;
1633         }
1634       }
1635       if (!c_conf) {
1636         for (i = 0; hp->h_addr_list[i]; i++) {
1637           if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1638             break;
1639         }
1640       }
1641     }
1642     else {
1643       /*
1644        * Check for C lines with the hostname portion the ip number
1645        * of the host the server runs on. This also checks the case where
1646        * there is a server connecting from 'localhost'.
1647        */
1648       c_conf = find_conf_byhost(lp, cptr->sockhost, CONF_SERVER);
1649     }
1650   }
1651   /*
1652    * Attach by IP# only if all other checks have failed.
1653    * It is quite possible to get here with the strange things that can
1654    * happen when using DNS in the way the irc server does. -avalon
1655    */
1656   if (!c_conf)
1657     c_conf = find_conf_byip(lp, (const char*) &cptr->ip, CONF_SERVER);
1658   /*
1659    * detach all conf lines that got attached by attach_confs()
1660    */
1661   det_confs_butmask(cptr, 0);
1662   /*
1663    * if no C or no N lines, then deny access
1664    */
1665   if (!c_conf) {
1666     Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1667           cptr->name, cptr->username, cptr->sockhost));
1668     return -1;
1669   }
1670   ircd_strncpy(cptr->name, c_conf->name, HOSTLEN);
1671   /*
1672    * attach the C and N lines to the client structure for later use.
1673    */
1674   attach_conf(cptr, c_conf);
1675   attach_confs_byname(cptr, cptr->name, CONF_HUB | CONF_LEAF | CONF_UWORLD);
1676
1677   if (INADDR_NONE == c_conf->ipnum.s_addr)
1678     c_conf->ipnum.s_addr = cptr->ip.s_addr;
1679
1680   Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cptr->name, cptr->sockhost));
1681   return 0;
1682 }
1683