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