2004-05-09 Michael Poole <mdpoole@troilus.org>
[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 "config.h"
23
24 #include "s_conf.h"
25 #include "IPcheck.h"
26 #include "class.h"
27 #include "client.h"
28 #include "crule.h"
29 #include "ircd_features.h"
30 #include "fileio.h"
31 #include "gline.h"
32 #include "hash.h"
33 #include "ircd.h"
34 #include "ircd_alloc.h"
35 #include "ircd_chattr.h"
36 #include "ircd_log.h"
37 #include "ircd_reply.h"
38 #include "ircd_snprintf.h"
39 #include "ircd_string.h"
40 #include "list.h"
41 #include "listener.h"
42 #include "match.h"
43 #include "motd.h"
44 #include "numeric.h"
45 #include "numnicks.h"
46 #include "opercmds.h"
47 #include "parse.h"
48 #include "res.h"
49 #include "s_bsd.h"
50 #include "s_debug.h"
51 #include "s_misc.h"
52 #include "send.h"
53 #include "struct.h"
54 #include "support.h"
55 #include "sys.h"
56
57 #include <assert.h>
58 #include <arpa/inet.h>
59 #include <errno.h>
60 #include <fcntl.h>
61 #include <netdb.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <sys/stat.h>
66 #include <unistd.h>
67
68 #ifndef INADDR_NONE
69 #define INADDR_NONE 0xffffffff
70 #endif
71
72 struct ConfItem  *GlobalConfList  = 0;
73 int              GlobalConfCount = 0;
74 struct s_map     *GlobalServiceMapList = 0;
75 struct qline     *GlobalQuarantineList = 0;
76
77 void yyparse(void);
78 int conf_fd, lineno;
79
80 struct LocalConf   localConf;
81 struct CRuleConf*  cruleConfList;
82 /* struct ServerConf* serverConfList; */
83 struct DenyConf*   denyConfList;
84
85 /*
86  * output the reason for being k lined from a file  - Mmmm
87  * sptr is client being dumped
88  * filename is the file that is to be output to the K lined client
89  */
90 static void killcomment(struct Client* sptr, const char* filename)
91 {
92   FBFILE*     file = 0;
93   char        line[80];
94   struct stat sb;
95   struct tm*  tm;
96
97   if (NULL == (file = fbopen(filename, "r"))) {
98     send_reply(sptr, ERR_NOMOTD);
99     send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
100                ":Connection from your host is refused on this server.");
101     return;
102   }
103   fbstat(&sb, file);
104   tm = localtime((time_t*) &sb.st_mtime);        /* NetBSD needs cast */
105   while (fbgets(line, sizeof(line) - 1, file)) {
106     char* end = line + strlen(line);
107     while (end > line) {
108       --end;
109       if ('\n' == *end || '\r' == *end)
110         *end = '\0';
111       else
112         break;
113     }
114     send_reply(sptr, RPL_MOTD, line);
115   }
116   send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
117              ":Connection from your host is refused on this server.");
118   fbclose(file);
119 }
120
121 struct ConfItem* make_conf(void)
122 {
123   struct ConfItem* aconf;
124
125   aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
126   assert(0 != aconf);
127 #ifdef        DEBUGMODE
128   ++GlobalConfCount;
129 #endif
130   memset(aconf, 0, sizeof(struct ConfItem));
131   aconf->status       = CONF_ILLEGAL;
132   aconf->ipnum.s_addr = INADDR_NONE;
133   return aconf;
134 }
135
136 void delist_conf(struct ConfItem *aconf)
137 {
138   if (aconf == GlobalConfList)
139     GlobalConfList = GlobalConfList->next;
140   else {
141     struct ConfItem *bconf;
142
143     for (bconf = GlobalConfList; aconf != bconf->next; bconf = bconf->next)
144       ;
145     bconf->next = aconf->next;
146   }
147   aconf->next = 0;
148 }
149
150 void free_conf(struct ConfItem *aconf)
151 {
152   Debug((DEBUG_DEBUG, "free_conf: %s %s %d",
153          aconf->host ? aconf->host : "*",
154          aconf->name ? aconf->name : "*",
155          aconf->port));
156   if (aconf->dns_pending)
157     delete_resolver_queries(aconf);
158   MyFree(aconf->host);
159   if (aconf->passwd)
160     memset(aconf->passwd, 0, strlen(aconf->passwd));
161   MyFree(aconf->passwd);
162   MyFree(aconf->name);
163   MyFree(aconf);
164 #ifdef        DEBUGMODE
165   --GlobalConfCount;
166 #endif
167 }
168
169 /*
170  * detach_conf - Disassociate configuration from the client.
171  */
172 static void detach_conf(struct Client* cptr, struct ConfItem* aconf)
173 {
174   struct SLink** lp;
175   struct SLink*  tmp;
176
177   assert(0 != aconf);
178   assert(0 != cptr);
179   assert(0 < aconf->clients);
180
181   lp = &(cli_confs(cptr));
182
183   while (*lp) {
184     if ((*lp)->value.aconf == aconf) {
185       if (aconf->conn_class && (aconf->status & CONF_CLIENT_MASK) && ConfLinks(aconf) > 0)
186         --ConfLinks(aconf);
187
188       assert(0 < aconf->clients);
189       if (0 == --aconf->clients && IsIllegal(aconf))
190         free_conf(aconf);
191       tmp = *lp;
192       *lp = tmp->next;
193       free_link(tmp);
194       return;
195     }
196     lp = &((*lp)->next);
197   }
198 }
199
200 /*
201  * conf_dns_callback - called when resolver query finishes
202  * if the query resulted in a successful search, hp will contain
203  * a non-null pointer, otherwise hp will be null.
204  * if successful save hp in the conf item it was called with
205  */
206 static void conf_dns_callback(void* vptr, struct hostent* hp)
207 {
208   struct ConfItem* aconf = (struct ConfItem*) vptr;
209   assert(aconf);
210   aconf->dns_pending = 0;
211   if (hp) {
212     memcpy(&aconf->ipnum, hp->h_addr, sizeof(struct in_addr));
213     MyFree(hp);
214   }
215 }
216
217 /*
218  * conf_dns_lookup - do a nameserver lookup of the conf host
219  * if the conf entry is currently doing a ns lookup do nothing, otherwise
220  * if the lookup returns a null pointer, set the conf dns_pending flag
221  */
222 static void conf_dns_lookup(struct ConfItem* aconf)
223 {
224   if (!aconf->dns_pending) {
225     char            buf[HOSTLEN + 1];
226     struct DNSQuery query;
227     query.vptr     = aconf;
228     query.callback = conf_dns_callback;
229     host_from_uh(buf, aconf->host, HOSTLEN);
230     buf[HOSTLEN] = '\0';
231
232     gethost_byname(buf, &query);
233     aconf->dns_pending = 1;
234   }
235 }
236
237
238 /*
239  * lookup_confhost
240  *
241  * Do (start) DNS lookups of all hostnames in the conf line and convert
242  * an IP addresses in a.b.c.d number for to IP#s.
243  */
244 void
245 lookup_confhost(struct ConfItem *aconf)
246 {
247   if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
248     Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
249            aconf->host, aconf->name));
250     return;
251   }
252   /*
253    * Do name lookup now on hostnames given and store the
254    * ip numbers in conf structure.
255    */
256   if (IsDigit(*aconf->host)) {
257     /*
258      * rfc 1035 sez host names may not start with a digit
259      * XXX - this has changed code needs to be updated
260      */
261     aconf->ipnum.s_addr = inet_addr(aconf->host);
262     if (INADDR_NONE == aconf->ipnum.s_addr) {
263       Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
264             aconf->host, aconf->name));
265     }
266   }
267   else 
268     conf_dns_lookup(aconf);
269 }
270
271 /*
272  * conf_find_server - find a server by name or hostname
273  * returns a server conf item pointer if found, 0 otherwise
274  *
275  * NOTE: at some point when we don't have to scan the entire
276  * list it may be cheaper to look for server names and host
277  * names in separate loops (original code did it that way)
278  */
279 struct ConfItem* conf_find_server(const char* name)
280 {
281   struct ConfItem* conf;
282   assert(0 != name);
283
284   for (conf = GlobalConfList; conf; conf = conf->next) {
285     if (CONF_SERVER == conf->status) {
286       /*
287        * Check first servernames, then try hostnames.
288        * XXX - match returns 0 if there _is_ a match... guess they
289        * haven't decided what true is yet
290        */
291       if (0 == match(name, conf->name))
292         return conf;
293     }
294   }
295   return 0;
296 }
297
298 /*
299  * conf_eval_crule - evaluate connection rules
300  * returns the name of the rule triggered if found, 0 otherwise
301  *
302  * Evaluate connection rules...  If no rules found, allow the
303  * connect.   Otherwise stop with the first true rule (ie: rules
304  * are ored together.  Oper connects are effected only by D
305  * lines (CRULE_ALL) not d lines (CRULE_AUTO).
306  */
307 const char* conf_eval_crule(const char* name, int mask)
308 {
309   struct CRuleConf* p = cruleConfList;
310   assert(0 != name);
311
312   for ( ; p; p = p->next) {
313     if (0 != (p->type & mask) && 0 == match(p->hostmask, name)) {
314       if (crule_eval(p->node))
315         return p->rule;
316     }
317   }
318   return 0;
319 }
320
321 /*
322  * Remove all conf entries from the client except those which match
323  * the status field mask.
324  */
325 void det_confs_butmask(struct Client* cptr, int mask)
326 {
327   struct SLink* link;
328   struct SLink* next;
329   assert(0 != cptr);
330
331   for (link = cli_confs(cptr); link; link = next) {
332     next = link->next;
333     if ((link->value.aconf->status & mask) == 0)
334       detach_conf(cptr, link->value.aconf);
335   }
336 }
337
338 /*
339  * check_limit_and_attach - check client limits and attach I:line
340  *
341  * Made it accept 1 charactor, and 2 charactor limits (0->99 now), 
342  * and dislallow more than 255 people here as well as in ipcheck.
343  * removed the old "ONE" scheme too.
344  *  -- Isomer 2000-06-22
345  */
346 static enum AuthorizationCheckResult
347 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
348 {
349   int number = 255;
350   
351   if (aconf->passwd) {
352     if (IsDigit(*aconf->passwd) && !aconf->passwd[1])
353       number = *aconf->passwd-'0';
354     else if (IsDigit(*aconf->passwd) && IsDigit(aconf->passwd[1]) && 
355              !aconf->passwd[2])
356       number = (*aconf->passwd-'0')*10+(aconf->passwd[1]-'0');
357   }
358   if (IPcheck_nr(cptr) > number)
359     return ACR_TOO_MANY_FROM_IP;
360   return attach_conf(cptr, aconf);
361 }
362
363 /*
364  * Find the first (best) I line to attach.
365  */
366 enum AuthorizationCheckResult attach_iline(struct Client*  cptr)
367 {
368   struct ConfItem* aconf;
369   const char*      hname;
370   int              i;
371   static char      uhost[HOSTLEN + USERLEN + 3];
372   static char      fullname[HOSTLEN + 1];
373   struct hostent*  hp = 0;
374
375   assert(0 != cptr);
376
377   if (cli_dns_reply(cptr))
378     hp = cli_dns_reply(cptr);
379
380   for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
381     if (aconf->status != CONF_CLIENT)
382       continue;
383     if (aconf->port && aconf->port != cli_listener(cptr)->port)
384       continue;
385     if (!aconf->host || !aconf->name)
386       continue;
387     if (hp) {
388       for (i = 0, hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
389         ircd_strncpy(fullname, hname, HOSTLEN);
390         fullname[HOSTLEN] = '\0';
391
392         Debug((DEBUG_DNS, "a_il: %s->%s", cli_sockhost(cptr), fullname));
393
394         if (strchr(aconf->name, '@')) {
395           strcpy(uhost, cli_username(cptr));
396           strcat(uhost, "@");
397         }
398         else
399           *uhost = '\0';
400         strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
401         uhost[sizeof(uhost) - 1] = 0;
402         if (0 == match(aconf->name, uhost)) {
403           if (strchr(uhost, '@'))
404             SetFlag(cptr, FLAG_DOID);
405           return check_limit_and_attach(cptr, aconf);
406         }
407       }
408     }
409     if (strchr(aconf->host, '@')) {
410       ircd_strncpy(uhost, cli_username(cptr), sizeof(uhost) - 2);
411       uhost[sizeof(uhost) - 2] = 0;
412       strcat(uhost, "@");
413     }
414     else
415       *uhost = '\0';
416     strncat(uhost, cli_sock_ip(cptr), sizeof(uhost) - 1 - strlen(uhost));
417     uhost[sizeof(uhost) - 1] = 0;
418     if (match(aconf->host, uhost))
419       continue;
420     if (strchr(uhost, '@'))
421       SetFlag(cptr, FLAG_DOID);
422
423     return check_limit_and_attach(cptr, aconf);
424   }
425   return ACR_NO_AUTHORIZATION;
426 }
427
428 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
429 {
430   struct SLink *lp;
431
432   for (lp = cli_confs(cptr); lp; lp = lp->next) {
433     if (lp->value.aconf == aconf)
434       return 1;
435   }
436   return 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 = cli_confs(cptr);
460   lp->value.aconf = aconf;
461   cli_confs(cptr) = 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 /*
474  * attach_confs_byname
475  *
476  * Attach a CONF line to a client if the name passed matches that for
477  * the conf file (for non-C/N lines) or is an exact match (C/N lines
478  * only).  The difference in behaviour is to stop C:*::* and N:*::*.
479  */
480 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
481                                      int statmask)
482 {
483   struct ConfItem* tmp;
484   struct ConfItem* first = NULL;
485
486   assert(0 != name);
487
488   if (HOSTLEN < strlen(name))
489     return 0;
490
491   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
492     if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
493       assert(0 != tmp->name);
494       if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) { 
495         if (ACR_OK == attach_conf(cptr, tmp) && !first)
496           first = tmp;
497       }
498     }
499   }
500   return first;
501 }
502
503 /*
504  * Added for new access check    meLazy
505  */
506 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
507                                      int statmask)
508 {
509   struct ConfItem* tmp;
510   struct ConfItem* first = 0;
511
512   assert(0 != host);
513   if (HOSTLEN < strlen(host))
514     return 0;
515
516   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
517     if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
518       assert(0 != tmp->host);
519       if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) { 
520         if (ACR_OK == attach_conf(cptr, tmp) && !first)
521           first = tmp;
522       }
523     }
524   }
525   return first;
526 }
527
528 /*
529  * find a conf entry which matches the hostname and has the same name.
530  */
531 struct ConfItem* find_conf_exact(const char* name, const char* user,
532                                  const char* host, int statmask)
533 {
534   struct ConfItem *tmp;
535   char userhost[USERLEN + HOSTLEN + 3];
536
537   if (user)
538     ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
539   else
540     ircd_strncpy(userhost, host, sizeof(userhost) - 1);
541
542   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
543     if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
544         0 != ircd_strcmp(tmp->name, name))
545       continue;
546     /*
547      * Accept if the *real* hostname (usually sockecthost)
548      * socket host) matches *either* host or name field
549      * of the configuration.
550      */
551     if (match(tmp->host, userhost))
552       continue;
553     if (tmp->status & (CONF_OPERATOR | CONF_LOCOP)) {
554       if (tmp->clients < MaxLinks(tmp->conn_class))
555         return tmp;
556       else
557         continue;
558     }
559     else
560       return tmp;
561   }
562   return 0;
563 }
564
565 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
566                                   int statmask)
567 {
568   struct ConfItem* tmp;
569   assert(0 != name);
570
571   if (HOSTLEN < strlen(name))
572     return 0;
573
574   for (; lp; lp = lp->next) {
575     tmp = lp->value.aconf;
576     if (0 != (tmp->status & statmask)) {
577       assert(0 != tmp->name);
578       if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
579         return tmp;
580     }
581   }
582   return 0;
583 }
584
585 /*
586  * Added for new access check    meLazy
587  */
588 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
589                                   int statmask)
590 {
591   struct ConfItem* tmp = NULL;
592   assert(0 != host);
593
594   if (HOSTLEN < strlen(host))
595     return 0;
596
597   for (; lp; lp = lp->next) {
598     tmp = lp->value.aconf;
599     if (0 != (tmp->status & statmask)) {
600       assert(0 != tmp->host);
601       if (0 == match(tmp->host, host))
602         return tmp;
603     }
604   }
605   return 0;
606 }
607
608 /*
609  * find_conf_ip
610  *
611  * Find a conf line using the IP# stored in it to search upon.
612  * Added 1/8/92 by Avalon.
613  */
614 struct ConfItem* find_conf_byip(struct SLink* lp, const char* ip, 
615                                 int statmask)
616 {
617   struct ConfItem* tmp;
618
619   for (; lp; lp = lp->next) {
620     tmp = lp->value.aconf;
621     if (0 != (tmp->status & statmask)) {
622       if (0 == memcmp(&tmp->ipnum, ip, sizeof(struct in_addr)))
623         return tmp;
624     }
625   }
626   return 0;
627 }
628
629 /*
630  * find_conf_entry
631  *
632  * - looks for a match on all given fields.
633  */
634 #if 0
635 static struct ConfItem *find_conf_entry(struct ConfItem *aconf,
636                                         unsigned int mask)
637 {
638   struct ConfItem *bconf;
639   assert(0 != aconf);
640
641   mask &= ~CONF_ILLEGAL;
642
643   for (bconf = GlobalConfList; bconf; bconf = bconf->next) {
644     if (!(bconf->status & mask) || (bconf->port != aconf->port))
645       continue;
646
647     if ((EmptyString(bconf->host) && !EmptyString(aconf->host)) ||
648         (EmptyString(aconf->host) && !EmptyString(bconf->host)))
649       continue;
650     if (!EmptyString(bconf->host) && 0 != ircd_strcmp(bconf->host, aconf->host))
651       continue;
652
653     if ((EmptyString(bconf->passwd) && !EmptyString(aconf->passwd)) ||
654         (EmptyString(aconf->passwd) && !EmptyString(bconf->passwd)))
655       continue;
656     if (!EmptyString(bconf->passwd) && (!IsDigit(*bconf->passwd) || bconf->passwd[1])
657         && 0 != ircd_strcmp(bconf->passwd, aconf->passwd))
658       continue;
659
660     if ((EmptyString(bconf->name) && !EmptyString(aconf->name)) ||
661         (EmptyString(aconf->name) && !EmptyString(bconf->name)))
662       continue;
663     if (!EmptyString(bconf->name) && 0 != ircd_strcmp(bconf->name, aconf->name))
664       continue;
665     break;
666   }
667   return bconf;
668 }
669
670 /*
671  * If conf line is a class definition, create a class entry
672  * for it and make the conf_line illegal and delete it.
673  */
674 void conf_add_class(const char* const* fields, int count)
675 {
676   if (count < 6)
677     return;
678   add_class(atoi(fields[1]), atoi(fields[2]), atoi(fields[3]),
679             atoi(fields[4]), atoi(fields[5]));
680 }
681
682 void conf_add_listener(const char* const* fields, int count)
683 {
684   int is_server = 0;
685   int is_hidden = 0;
686
687   /*
688    * need a port
689    */
690   if (count < 5 || EmptyString(fields[4]))
691     return;
692
693   if (!EmptyString(fields[3])) {
694     const char* x = fields[3];
695     if ('S' == ToUpper(*x))
696       is_server = 1;
697     ++x;
698     if ('H' == ToUpper(*x))
699       is_hidden = 1;
700   }
701   /*           port             vhost      mask  */
702   add_listener(atoi(fields[4]), fields[2], fields[1], is_server, is_hidden);
703 }
704
705 void conf_add_local(const char* const* fields, int count)
706 {
707   if (count < 6 || EmptyString(fields[1]) || EmptyString(fields[5])) {
708     log_write(LS_CONFIG, L_CRIT, 0, "Your M: line must have 6 fields!");
709     return;
710   }
711   /*
712    * these two can only be set the first time
713    */
714   if (0 == localConf.name) {
715     if (string_is_hostname(fields[1]))
716       DupString(localConf.name, fields[1]);
717   }
718   if (0 == localConf.numeric) {
719     localConf.numeric = atoi(fields[5]);
720     if (0 == localConf.numeric)
721       log_write(LS_CONFIG, L_WARNING, 0,
722                 "Your M: line must have a Numeric value greater than 0");
723   }
724   /*
725    * these two can be changed while the server is running
726    */
727   if (string_is_address(fields[2])) {
728     if (INADDR_NONE == (localConf.vhost_address.s_addr = inet_addr(fields[2])))
729       localConf.vhost_address.s_addr = INADDR_ANY;
730   }
731   MyFree(localConf.description);
732   DupString(localConf.description, fields[3]);
733   /*
734    * XXX - shouldn't be setting these directly here
735    */
736   ircd_strncpy(cli_info(&me), fields[3], REALLEN);
737   set_virtual_host(localConf.vhost_address);
738 }
739
740 void conf_add_admin(const char* const* fields, int count)
741 {
742   /*
743    * if you have one, it MUST have 3 lines
744    */
745   if (count < 4) {
746     log_write(LS_CONFIG, L_CRIT, 0, "Your A: line must have 4 fields!");
747     return;
748   }
749   MyFree(localConf.location1);
750   DupString(localConf.location1, fields[1]);
751
752   MyFree(localConf.location2);
753   DupString(localConf.location2, fields[2]);
754
755   MyFree(localConf.contact);
756   DupString(localConf.contact, fields[3]);
757 }
758
759 /*
760  * conf_add_crule - Create expression tree from connect rule and add it
761  * to the crule list
762  */
763 void conf_add_crule(const char* const* fields, int count, int type)
764 {
765   struct CRuleNode* node;
766   assert(0 != fields);
767   
768   if (count < 4 || EmptyString(fields[1]) || EmptyString(fields[3]))
769     return;
770   
771   if ((node = crule_parse(fields[3]))) {
772     struct CRuleConf* p = (struct CRuleConf*) MyMalloc(sizeof(struct CRuleConf));
773     assert(0 != p);
774
775     DupString(p->hostmask, fields[1]);
776     collapse(p->hostmask);
777
778     DupString(p->rule, fields[3]);
779
780     p->type = type;
781     p->node = node;
782     p->next = cruleConfList;
783     cruleConfList = p;
784   } 
785 }
786 #endif
787
788 void conf_erase_crule_list(void)
789 {
790   struct CRuleConf* next;
791   struct CRuleConf* p = cruleConfList;
792
793   for ( ; p; p = next) {
794     next = p->next;
795     crule_free(&p->node);
796     MyFree(p->hostmask);
797     MyFree(p->rule);
798     MyFree(p);
799   }
800   cruleConfList = 0;
801 }
802
803 const struct CRuleConf* conf_get_crule_list(void)
804 {
805   return cruleConfList;
806 }
807
808 #if 0
809 void conf_add_server(const char* const* fields, int count)
810 {
811   struct ServerConf* server;
812   struct in_addr    addr;
813   assert(0 != fields);
814   /*
815    * missing host, password, or alias?
816    */
817   if (count < 6 || EmptyString(fields[1]) || EmptyString(fields[2]) || EmptyString(fields[3]))
818     return;
819   /*
820    * check the host
821    */
822   if (string_is_hostname(fields[1]))
823     addr.s_addr = INADDR_NONE;
824   else if (INADDR_NONE == (addr.s_addr = inet_addr(fields[1])))
825     return;
826
827   server = (struct ServerConf*) MyMalloc(sizeof(struct ServerConf));
828   assert(0 != server);
829   DupString(server->hostname, fields[1]);
830   DupString(server->passwd,   fields[2]);
831   DupString(server->alias,    fields[3]);
832   server->address.s_addr = addr.s_addr;
833   server->port           = atoi(fields[4]);
834   server->dns_pending    = 0;
835   server->connected      = 0;
836   server->hold           = 0;
837   server->conn_class      = find_class(atoi(fields[5]));
838
839   server->next = serverConfList;
840   serverConfList = server;
841
842   /* if (INADDR_NONE == server->address.s_addr) */
843     /* lookup_confhost(server); */
844 }
845
846 void conf_add_deny(const char* const* fields, int count, int ip_kill)
847 {
848   struct DenyConf* conf;
849
850   if (count < 4 || EmptyString(fields[1]) || EmptyString(fields[3]))
851     return;
852   
853   conf = (struct DenyConf*) MyMalloc(sizeof(struct DenyConf));
854   assert(0 != conf);
855   memset(conf, 0, sizeof(struct DenyConf));
856
857   if (fields[1][0] == '$' && fields[1][1] == 'R')
858     conf->flags |= DENY_FLAGS_REALNAME;
859
860   DupString(conf->hostmask, fields[1]);
861   collapse(conf->hostmask);
862
863   if (!EmptyString(fields[2])) {
864     const char* p = fields[2];
865     if ('!' == *p) {
866       conf->flags |= DENY_FLAGS_FILE;
867       ++p;
868     }
869     DupString(conf->message, p);
870   }
871   DupString(conf->usermask, fields[3]);
872   collapse(conf->usermask);
873
874   if (ip_kill) {
875     /* 
876      * Here we use the same kludge as in listener.c to parse
877      * a.b.c.d, or a.b.c.*, or a.b.c.d/e.
878      */
879     int  c_class;
880     char ipname[16];
881     int  ad[4] = { 0 };
882     int  bits2 = 0;
883     c_class = sscanf(conf->hostmask, "%d.%d.%d.%d/%d",
884                      &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
885     if (c_class != 5) {
886       conf->bits = c_class * 8;
887     }
888     else {
889       conf->bits = bits2;
890     }
891     ircd_snprintf(0, ipname, sizeof(ipname), "%d.%d.%d.%d", ad[0], ad[1],
892                   ad[2], ad[3]);
893     
894     /*
895      * This ensures endian correctness
896      */
897     conf->address = inet_addr(ipname);
898     Debug((DEBUG_DEBUG, "IPkill: %s = %08x/%i (%08x)", ipname,
899            conf->address, conf->bits, NETMASK(conf->bits)));
900     conf->flags |= DENY_FLAGS_IP;
901   }
902   conf->next = denyConfList;
903   denyConfList = conf;
904 }
905 #endif
906
907
908 void conf_erase_deny_list(void)
909 {
910   struct DenyConf* next;
911   struct DenyConf* p = denyConfList;
912   for ( ; p; p = next) {
913     next = p->next;
914     MyFree(p->hostmask);
915     MyFree(p->usermask);
916     MyFree(p->message);
917     MyFree(p);
918   }
919   denyConfList = 0;
920 }
921  
922 const struct DenyConf* conf_get_deny_list(void)
923 {
924   return denyConfList;
925 }
926
927 #if 0
928 void conf_add_quarantine(const char *chname, const char *reason)
929 {
930   struct qline *qline;
931
932   qline = (struct qline *) MyMalloc(sizeof(struct qline));
933   DupString(qline->chname, chname);
934   DupString(qline->reason, reason);
935   qline->next = GlobalQuarantineList;
936   GlobalQuarantineList = qline;
937 }
938 #endif
939
940 const char*
941 find_quarantine(const char *chname)
942 {
943   struct qline *qline;
944   
945   for (qline = GlobalQuarantineList; qline; qline = qline->next)
946     if (!ircd_strcmp(qline->chname, chname))
947       return qline->reason;
948   return NULL;
949 }
950
951 void clear_quarantines(void)
952 {
953   struct qline *qline;
954   while ((qline = GlobalQuarantineList))
955   {
956     GlobalQuarantineList = qline->next;
957     MyFree(qline->reason);
958     MyFree(qline->chname);
959     MyFree(qline);
960   }
961 }
962
963
964 /*
965  * read_configuration_file
966  *
967  * Read configuration file.
968  *
969  * returns 0, if file cannot be opened
970  *         1, if file read
971  */
972
973 #define MAXCONFLINKS 150
974
975 static int conf_error;
976 static int conf_already_read;
977 extern FILE *yyin;
978 void init_lexer(void);
979
980 int read_configuration_file(void)
981 {
982   conf_error = 0;
983   feature_unmark(); /* unmark all features for resetting later */
984   /* Now just open an fd. The buffering isn't really needed... */
985   init_lexer();
986   yyparse();
987   fclose(yyin);
988   yyin = NULL;
989   feature_mark(); /* reset unmarked features */
990   conf_already_read = 1;
991   return 1;
992 }
993
994 void
995 yyerror(const char *msg)
996 {
997  sendto_opmask_butone(0, SNO_ALL, "Config file parse error line %d: %s",
998                       lineno, msg);
999  log_write(LS_CONFIG, L_ERROR, 0, "Config file parse error line %d: %s",
1000            lineno, msg);
1001  if (!conf_already_read)
1002    fprintf(stderr, "Config file parse error line %d: %s\n", lineno, msg);
1003  conf_error = 1;
1004 }
1005
1006 /*
1007  * rehash
1008  *
1009  * Actual REHASH service routine. Called with sig == 0 if it has been called
1010  * as a result of an operator issuing this command, else assume it has been
1011  * called as a result of the server receiving a HUP signal.
1012  */
1013 int rehash(struct Client *cptr, int sig)
1014 {
1015   struct ConfItem** tmp = &GlobalConfList;
1016   struct ConfItem*  tmp2;
1017   struct Client*    acptr;
1018   int               i;
1019   int               ret = 0;
1020   int               found_g = 0;
1021
1022   if (1 == sig)
1023     sendto_opmask_butone(0, SNO_OLDSNO,
1024                          "Got signal SIGHUP, reloading ircd conf. file");
1025
1026   while ((tmp2 = *tmp)) {
1027     if (tmp2->clients) {
1028       /*
1029        * Configuration entry is still in use by some
1030        * local clients, cannot delete it--mark it so
1031        * that it will be deleted when the last client
1032        * exits...
1033        */
1034       if (CONF_CLIENT == (tmp2->status & CONF_CLIENT))
1035         tmp = &tmp2->next;
1036       else {
1037         *tmp = tmp2->next;
1038         tmp2->next = 0;
1039       }
1040       tmp2->status |= CONF_ILLEGAL;
1041     }
1042     else {
1043       *tmp = tmp2->next;
1044       free_conf(tmp2);
1045     }
1046   }
1047   conf_erase_crule_list();
1048   conf_erase_deny_list();
1049   motd_clear();
1050
1051   /*
1052    * delete the juped nicks list
1053    */
1054   clearNickJupes();
1055
1056   if (sig != 2)
1057     restart_resolver();
1058
1059   class_mark_delete();
1060   mark_listeners_closing();
1061
1062   read_configuration_file();
1063
1064   log_reopen(); /* reopen log files */
1065
1066   close_listeners();
1067   class_delete_marked();         /* unless it fails */
1068
1069   /*
1070    * Flush out deleted I and P lines although still in use.
1071    */
1072   for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
1073     if (CONF_ILLEGAL == (tmp2->status & CONF_ILLEGAL)) {
1074       *tmp = tmp2->next;
1075       tmp2->next = NULL;
1076       if (!tmp2->clients)
1077         free_conf(tmp2);
1078     }
1079     else
1080       tmp = &tmp2->next;
1081   }
1082
1083   for (i = 0; i <= HighestFd; i++) {
1084     if ((acptr = LocalClientArray[i])) {
1085       assert(!IsMe(acptr));
1086       if (IsServer(acptr)) {
1087         det_confs_butmask(acptr,
1088             ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
1089         attach_confs_byname(acptr, cli_name(acptr),
1090                             CONF_HUB | CONF_LEAF | CONF_UWORLD);
1091       }
1092       /* Because admin's are getting so uppity about people managing to
1093        * get past K/G's etc, we'll "fix" the bug by actually explaining
1094        * whats going on.
1095        */
1096       if ((found_g = find_kill(acptr))) {
1097         sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
1098                              found_g == -2 ? "G-line active for %s%s" :
1099                              "K-line active for %s%s",
1100                              IsUnknown(acptr) ? "Unregistered Client ":"",                     
1101                              get_client_name(acptr, HIDE_IP));
1102         if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
1103             "K-lined") == CPTR_KILLED)
1104           ret = CPTR_KILLED;
1105       }
1106     }
1107   }
1108
1109   return ret;
1110 }
1111
1112 /*
1113  * init_conf
1114  *
1115  * Read configuration file.
1116  *
1117  * returns 0, if file cannot be opened
1118  *         1, if file read
1119  */
1120
1121 int init_conf(void)
1122 {
1123   if (read_configuration_file()) {
1124     /*
1125      * make sure we're sane to start if the config
1126      * file read didn't get everything we need.
1127      * XXX - should any of these abort the server?
1128      * TODO: add warning messages
1129      */
1130     if (0 == localConf.name || 0 == localConf.numeric)
1131       return 0;
1132     if (conf_error)
1133       return 0;
1134
1135     if (0 == localConf.location1)
1136       DupString(localConf.location1, "");
1137     if (0 == localConf.location2)
1138       DupString(localConf.location2, "");
1139     if (0 == localConf.contact)
1140       DupString(localConf.contact, "");
1141
1142     return 1;
1143   }
1144   return 0;
1145 }
1146
1147 /*
1148  * find_kill
1149  * input:
1150  *  client pointer
1151  * returns:
1152  *  0: Client may continue to try and connect
1153  * -1: Client was K/k:'d - sideeffect: reason was sent.
1154  * -2: Client was G/g:'d - sideeffect: reason was sent.
1155  * sideeffects:
1156  *  Client may have been sent a reason why they are denied, as above.
1157  */
1158 int find_kill(struct Client *cptr)
1159 {
1160   const char*      host;
1161   const char*      name;
1162   const char*      realname;
1163   struct DenyConf* deny;
1164   struct Gline*    agline = NULL;
1165
1166   assert(0 != cptr);
1167
1168   if (!cli_user(cptr))
1169     return 0;
1170
1171   host = cli_sockhost(cptr);
1172   name = cli_user(cptr)->username;
1173   realname = cli_info(cptr);
1174
1175   assert(strlen(host) <= HOSTLEN);
1176   assert((name ? strlen(name) : 0) <= HOSTLEN);
1177   assert((realname ? strlen(realname) : 0) <= REALLEN);
1178
1179   /* 2000-07-14: Rewrote this loop for massive speed increases.
1180    *             -- Isomer
1181    */
1182   for (deny = denyConfList; deny; deny = deny->next) {
1183     if (0 != match(deny->usermask, name))
1184       continue;
1185
1186     if (EmptyString(deny->hostmask))
1187       break;
1188
1189     if (deny->flags & DENY_FLAGS_REALNAME) { /* K: by real name */
1190       if (0 == match(deny->hostmask, realname))
1191         break;
1192     } else if (deny->flags & DENY_FLAGS_IP) { /* k: by IP */
1193       Debug((DEBUG_DEBUG, "ip: %08x network: %08x/%i mask: %08x",
1194              cli_ip(cptr).s_addr, deny->address, deny->bits, NETMASK(deny->bits)));
1195       if ((cli_ip(cptr).s_addr & NETMASK(deny->bits)) == deny->address)
1196         break;
1197     }
1198     else if (0 == match(deny->hostmask, host))
1199       break;
1200   }
1201   if (deny) {
1202     if (EmptyString(deny->message))
1203       send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1204                  ":Connection from your host is refused on this server.");
1205     else {
1206       if (deny->flags & DENY_FLAGS_FILE)
1207         killcomment(cptr, deny->message);
1208       else
1209         send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", deny->message);
1210     }
1211   }
1212   else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline)) {
1213     /*
1214      * find active glines
1215      * added a check against the user's IP address to find_gline() -Kev
1216      */
1217     send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", GlineReason(agline));
1218   }
1219   else
1220     agline = 0;          /* if a gline was found, it was inactive */
1221
1222   if (deny)
1223     return -1;
1224   if (agline)
1225     return -2;
1226     
1227   return 0;
1228 }
1229
1230 /*
1231  * Ordinary client access check. Look for conf lines which have the same
1232  * status as the flags passed.
1233  */
1234 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1235 {
1236   enum AuthorizationCheckResult acr = ACR_OK;
1237
1238   ClearAccess(cptr);
1239
1240   if ((acr = attach_iline(cptr))) {
1241     Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]", 
1242           cli_name(cptr), cli_sockhost(cptr)));
1243     return acr;
1244   }
1245   return ACR_OK;
1246 }
1247
1248 /*
1249  * check_server()
1250  *
1251  * Check access for a server given its name (passed in cptr struct).
1252  * Must check for all C/N lines which have a name which matches the
1253  * name given and a host which matches. A host alias which is the
1254  * same as the server name is also acceptable in the host field of a
1255  * C/N line.
1256  *
1257  * Returns
1258  *  0 = Success
1259  * -1 = Access denied
1260  * -2 = Bad socket.
1261  */
1262 int conf_check_server(struct Client *cptr)
1263 {
1264   struct ConfItem* c_conf = NULL;
1265   struct SLink*    lp;
1266
1267   Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]", 
1268         cli_name(cptr), cli_sockhost(cptr)));
1269
1270   if (IsUnknown(cptr) && !attach_confs_byname(cptr, cli_name(cptr), CONF_SERVER)) {
1271     Debug((DEBUG_DNS, "No C/N lines for %s", cli_sockhost(cptr)));
1272     return -1;
1273   }
1274   lp = cli_confs(cptr);
1275   /*
1276    * We initiated this connection so the client should have a C and N
1277    * line already attached after passing through the connect_server()
1278    * function earlier.
1279    */
1280   if (IsConnecting(cptr) || IsHandshake(cptr)) {
1281     c_conf = find_conf_byname(lp, cli_name(cptr), CONF_SERVER);
1282     if (!c_conf) {
1283       sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1284                            cli_name(cptr));
1285       det_confs_butmask(cptr, 0);
1286       return -1;
1287     }
1288   }
1289
1290   ClearAccess(cptr);
1291
1292   if (!c_conf) {
1293     if (cli_dns_reply(cptr)) {
1294       int             i;
1295       struct hostent* hp = cli_dns_reply(cptr);
1296       const char*     name = hp->h_name;
1297       /*
1298        * If we are missing a C or N line from above, search for
1299        * it under all known hostnames we have for this ip#.
1300        */
1301       for (i = 0; name; name = hp->h_aliases[i++]) {
1302         if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1303           ircd_strncpy(cli_sockhost(cptr), name, HOSTLEN);
1304           break;
1305         }
1306       }
1307       if (!c_conf) {
1308         for (i = 0; hp->h_addr_list[i]; i++) {
1309           if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1310             break;
1311         }
1312       }
1313     }
1314     else {
1315       /*
1316        * Check for C lines with the hostname portion the ip number
1317        * of the host the server runs on. This also checks the case where
1318        * there is a server connecting from 'localhost'.
1319        */
1320       c_conf = find_conf_byhost(lp, cli_sockhost(cptr), CONF_SERVER);
1321     }
1322   }
1323   /*
1324    * Attach by IP# only if all other checks have failed.
1325    * It is quite possible to get here with the strange things that can
1326    * happen when using DNS in the way the irc server does. -avalon
1327    */
1328   if (!c_conf)
1329     c_conf = find_conf_byip(lp, (const char*) &(cli_ip(cptr)), CONF_SERVER);
1330   /*
1331    * detach all conf lines that got attached by attach_confs()
1332    */
1333   det_confs_butmask(cptr, 0);
1334   /*
1335    * if no C or no N lines, then deny access
1336    */
1337   if (!c_conf) {
1338     Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1339           cli_name(cptr), cli_username(cptr), cli_sockhost(cptr)));
1340     return -1;
1341   }
1342   ircd_strncpy(cli_name(cptr), c_conf->name, HOSTLEN);
1343   /*
1344    * attach the C and N lines to the client structure for later use.
1345    */
1346   attach_conf(cptr, c_conf);
1347   attach_confs_byname(cptr, cli_name(cptr), CONF_HUB | CONF_LEAF | CONF_UWORLD);
1348
1349   if (INADDR_NONE == c_conf->ipnum.s_addr)
1350     c_conf->ipnum.s_addr = cli_ip(cptr).s_addr;
1351
1352   Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]",
1353          cli_name(cptr), cli_sockhost(cptr)));
1354   return 0;
1355 }
1356