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