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