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