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