ff2863cf334e1b5a7e8996c18dcf21a12125e0ac
[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 /** @file
21  * @brief ircd configuration file driver
22  * @version $Id$
23  */
24 #include "config.h"
25
26 #include "s_conf.h"
27 #include "IPcheck.h"
28 #include "class.h"
29 #include "client.h"
30 #include "crule.h"
31 #include "ircd_features.h"
32 #include "fileio.h"
33 #include "gline.h"
34 #include "hash.h"
35 #include "ircd.h"
36 #include "ircd_alloc.h"
37 #include "ircd_auth.h"
38 #include "ircd_chattr.h"
39 #include "ircd_log.h"
40 #include "ircd_reply.h"
41 #include "ircd_snprintf.h"
42 #include "ircd_string.h"
43 #include "list.h"
44 #include "listener.h"
45 #include "match.h"
46 #include "motd.h"
47 #include "numeric.h"
48 #include "numnicks.h"
49 #include "opercmds.h"
50 #include "parse.h"
51 #include "res.h"
52 #include "s_bsd.h"
53 #include "s_debug.h"
54 #include "s_misc.h"
55 #include "send.h"
56 #include "struct.h"
57 #include "sys.h"
58
59 #include <assert.h>
60 #include <arpa/inet.h>
61 #include <errno.h>
62 #include <fcntl.h>
63 #include <netdb.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <sys/stat.h>
68 #include <unistd.h>
69
70 /** Global list of all ConfItem structures. */
71 struct ConfItem  *GlobalConfList;
72 /** Count of items in #GlobalConfLis. */
73 int              GlobalConfCount;
74 /** Global list of service mappings. */
75 struct s_map     *GlobalServiceMapList;
76 /** Global list of channel quarantines. */
77 struct qline     *GlobalQuarantineList;
78
79 /** Current line number in scanner input. */
80 int lineno;
81
82 /** Configuration information for #me. */
83 struct LocalConf   localConf;
84 /** Global list of connection rules. */
85 struct CRuleConf*  cruleConfList;
86 /** Global list of K-lines. */
87 struct DenyConf*   denyConfList;
88
89 /** Tell a user that they are banned, dumping the message from a file.
90  * @param sptr Client being rejected
91  * @param filename Send this file's contents to \a sptr
92  */
93 static void killcomment(struct Client* sptr, const char* filename)
94 {
95   FBFILE*     file = 0;
96   char        line[80];
97   struct stat sb;
98   struct tm*  tm;
99
100   if (NULL == (file = fbopen(filename, "r"))) {
101     send_reply(sptr, ERR_NOMOTD);
102     send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
103                ":Connection from your host is refused on this server.");
104     return;
105   }
106   fbstat(&sb, file);
107   tm = localtime((time_t*) &sb.st_mtime);        /* NetBSD needs cast */
108   while (fbgets(line, sizeof(line) - 1, file)) {
109     char* end = line + strlen(line);
110     while (end > line) {
111       --end;
112       if ('\n' == *end || '\r' == *end)
113         *end = '\0';
114       else
115         break;
116     }
117     send_reply(sptr, RPL_MOTD, line);
118   }
119   send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
120              ":Connection from your host is refused on this server.");
121   fbclose(file);
122 }
123
124 /** Allocate a new struct ConfItem.
125  * @return Newly allocated structure.
126  */
127 struct ConfItem* make_conf(void)
128 {
129   struct ConfItem* aconf;
130
131   aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
132   assert(0 != aconf);
133 #ifdef        DEBUGMODE
134   ++GlobalConfCount;
135 #endif
136   memset(aconf, 0, sizeof(struct ConfItem));
137   aconf->status       = CONF_ILLEGAL;
138   return aconf;
139 }
140
141 /** Free a struct ConfItem and any resources it owns.
142  * @param aconf Item to free.
143  */
144 void free_conf(struct ConfItem *aconf)
145 {
146   Debug((DEBUG_DEBUG, "free_conf: %s %s %d",
147          aconf->host ? aconf->host : "*",
148          aconf->name ? aconf->name : "*",
149          aconf->address.port));
150   if (aconf->dns_pending)
151     delete_resolver_queries(aconf);
152   MyFree(aconf->host);
153   if (aconf->passwd)
154     memset(aconf->passwd, 0, strlen(aconf->passwd));
155   MyFree(aconf->passwd);
156   MyFree(aconf->name);
157   MyFree(aconf);
158 #ifdef        DEBUGMODE
159   --GlobalConfCount;
160 #endif
161 }
162
163 /** Disassociate configuration from the client.
164  * @param cptr Client to operate on.
165  * @param aconf ConfItem to detach.
166  */
167 static void detach_conf(struct Client* cptr, struct ConfItem* aconf)
168 {
169   struct SLink** lp;
170   struct SLink*  tmp;
171
172   assert(0 != aconf);
173   assert(0 != cptr);
174   assert(0 < aconf->clients);
175
176   lp = &(cli_confs(cptr));
177
178   while (*lp) {
179     if ((*lp)->value.aconf == aconf) {
180       if (aconf->conn_class && (aconf->status & CONF_CLIENT_MASK) && ConfLinks(aconf) > 0)
181         --ConfLinks(aconf);
182
183       assert(0 < aconf->clients);
184       if (0 == --aconf->clients && IsIllegal(aconf))
185         free_conf(aconf);
186       tmp = *lp;
187       *lp = tmp->next;
188       free_link(tmp);
189       return;
190     }
191     lp = &((*lp)->next);
192   }
193 }
194
195 /** Copies a completed DNS query into its ConfItem.
196  * @param vptr Pointer to struct ConfItem for the block.
197  * @param hp DNS reply, or NULL if the lookup failed.
198  */
199 static void conf_dns_callback(void* vptr, struct DNSReply* hp)
200 {
201   struct ConfItem* aconf = (struct ConfItem*) vptr;
202   assert(aconf);
203   aconf->dns_pending = 0;
204   if (hp) {
205     memcpy(&aconf->address.addr, &hp->addr, sizeof(aconf->address.addr));
206     MyFree(hp);
207   }
208 }
209
210 /** Start a nameserver lookup of the conf host.  If the conf entry is
211  * currently doing a lookup, do nothing.
212  * @param aconf ConfItem for which to start a request.
213  */
214 static void conf_dns_lookup(struct ConfItem* aconf)
215 {
216   if (!aconf->dns_pending) {
217     char            buf[HOSTLEN + 1];
218     struct DNSQuery query;
219     query.vptr     = aconf;
220     query.callback = conf_dns_callback;
221     host_from_uh(buf, aconf->host, HOSTLEN);
222     buf[HOSTLEN] = '\0';
223
224     gethost_byname(buf, &query);
225     aconf->dns_pending = 1;
226   }
227 }
228
229
230 /** Start lookups of all addresses in the conf line.  The origin must
231  * be a numeric IP address.  If the remote host field is not an IP
232  * address, start a DNS lookup for it.
233  * @param aconf Connection to do lookups for.
234  */
235 void
236 lookup_confhost(struct ConfItem *aconf)
237 {
238   if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
239     Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
240            aconf->host, aconf->name));
241     return;
242   }
243   if (aconf->origin_name
244       && !ircd_aton(&aconf->origin.addr, aconf->origin_name)) {
245     Debug((DEBUG_ERROR, "Origin name error: (%s) (%s)",
246         aconf->origin_name, aconf->name));
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 /** Find a server by name or hostname.
263  * @param name Server name to find.
264  * @return Pointer to the corresponding ConfItem, or NULL if none exists.
265  */
266 struct ConfItem* conf_find_server(const char* name)
267 {
268   struct ConfItem* conf;
269   assert(0 != name);
270
271   for (conf = GlobalConfList; conf; conf = conf->next) {
272     if (CONF_SERVER == conf->status) {
273       /*
274        * Check first servernames, then try hostnames.
275        * XXX - match returns 0 if there _is_ a match... guess they
276        * haven't decided what true is yet
277        */
278       if (0 == match(name, conf->name))
279         return conf;
280     }
281   }
282   return 0;
283 }
284
285 /** Evaluate connection rules.
286  * @param name Name of server to check
287  * @param mask Filter for CRule types (only consider if type & \a mask != 0).
288  * @return Name of rule that forbids the connection; NULL if no prohibitions.
289  */
290 const char* conf_eval_crule(const char* name, int mask)
291 {
292   struct CRuleConf* p = cruleConfList;
293   assert(0 != name);
294
295   for ( ; p; p = p->next) {
296     if (0 != (p->type & mask) && 0 == match(p->hostmask, name)) {
297       if (crule_eval(p->node))
298         return p->rule;
299     }
300   }
301   return 0;
302 }
303
304 /** Remove all conf entries from the client except those which match
305  * the status field mask.
306  * @param cptr Client to operate on.
307  * @param mask ConfItem types to keep.
308  */
309 void det_confs_butmask(struct Client* cptr, int mask)
310 {
311   struct SLink* link;
312   struct SLink* next;
313   assert(0 != cptr);
314
315   for (link = cli_confs(cptr); link; link = next) {
316     next = link->next;
317     if ((link->value.aconf->status & mask) == 0)
318       detach_conf(cptr, link->value.aconf);
319   }
320 }
321
322 /** Check client limits and attach Client block.
323  * If the password field consists of one or two digits, use that
324  * as the per-IP connection limit; otherwise use 255.
325  * @param cptr Client getting \a aconf.
326  * @param aconf Configuration item to attach.
327  * @return Authorization check result.
328  */
329 static enum AuthorizationCheckResult
330 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
331 {
332   int number = 255;
333   
334   if (aconf->passwd) {
335     if (IsDigit(*aconf->passwd) && !aconf->passwd[1])
336       number = *aconf->passwd-'0';
337     else if (IsDigit(*aconf->passwd) && IsDigit(aconf->passwd[1]) && 
338              !aconf->passwd[2])
339       number = (*aconf->passwd-'0')*10+(aconf->passwd[1]-'0');
340   }
341   if (IPcheck_nr(cptr) > number)
342     return ACR_TOO_MANY_FROM_IP;
343   return attach_conf(cptr, aconf);
344 }
345
346 /** Find the first (best) Client block to attach.
347  * @param cptr Client for whom to check rules.
348  * @return Authorization check result.
349  */
350 enum AuthorizationCheckResult attach_iline(struct Client*  cptr)
351 {
352   struct ConfItem* aconf;
353   static char      uhost[HOSTLEN + USERLEN + 3];
354   static char      fullname[HOSTLEN + 1];
355   struct DNSReply* hp;
356
357   assert(0 != cptr);
358
359   hp = cli_dns_reply(cptr);
360   for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
361     if (aconf->status != CONF_CLIENT)
362       continue;
363     if (aconf->address.port && aconf->address.port != cli_listener(cptr)->addr.port)
364       continue;
365     if (!aconf->host || !aconf->name)
366       continue;
367     if (hp) {
368       ircd_strncpy(fullname, hp->h_name, HOSTLEN);
369       fullname[HOSTLEN] = '\0';
370
371       Debug((DEBUG_DNS, "a_il: %s->%s", cli_sockhost(cptr), fullname));
372
373       if (strchr(aconf->name, '@')) {
374         strcpy(uhost, cli_username(cptr));
375         strcat(uhost, "@");
376       }
377       else
378         *uhost = '\0';
379       strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
380       uhost[sizeof(uhost) - 1] = 0;
381       if (0 == match(aconf->name, uhost)) {
382         if (strchr(uhost, '@'))
383           SetFlag(cptr, FLAG_DOID);
384         return check_limit_and_attach(cptr, aconf);
385       }
386     }
387     if (strchr(aconf->host, '@')) {
388       ircd_strncpy(uhost, cli_username(cptr), sizeof(uhost) - 2);
389       uhost[sizeof(uhost) - 2] = 0;
390       strcat(uhost, "@");
391     }
392     else
393       *uhost = '\0';
394     strncat(uhost, cli_sock_ip(cptr), sizeof(uhost) - 1 - strlen(uhost));
395     uhost[sizeof(uhost) - 1] = 0;
396     if (match(aconf->host, uhost))
397       continue;
398     if (strchr(uhost, '@'))
399       SetFlag(cptr, FLAG_DOID);
400
401     return check_limit_and_attach(cptr, aconf);
402   }
403   return ACR_NO_AUTHORIZATION;
404 }
405
406 /** Check whether a particular ConfItem is already attached to a
407  * Client.
408  * @param aconf ConfItem to search for
409  * @param cptr Client to check
410  * @return Non-zero if \a aconf is attached to \a cptr, zero if not.
411  */
412 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
413 {
414   struct SLink *lp;
415
416   for (lp = cli_confs(cptr); lp; lp = lp->next) {
417     if (lp->value.aconf == aconf)
418       return 1;
419   }
420   return 0;
421 }
422
423 /** Associate a specific configuration entry to a *local* client (this
424  * is the one which used in accepting the connection). Note, that this
425  * automaticly changes the attachment if there was an old one...
426  * @param cptr Client to attach \a aconf to
427  * @param aconf ConfItem to attach
428  * @return Authorization check result.
429  */
430 enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
431 {
432   struct SLink *lp;
433
434   if (is_attached(aconf, cptr))
435     return ACR_ALREADY_AUTHORIZED;
436   if (IsIllegal(aconf))
437     return ACR_NO_AUTHORIZATION;
438   if ((aconf->status & (CONF_OPERATOR | CONF_CLIENT)) &&
439       ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
440     return ACR_TOO_MANY_IN_CLASS;  /* Use this for printing error message */
441   lp = make_link();
442   lp->next = cli_confs(cptr);
443   lp->value.aconf = aconf;
444   cli_confs(cptr) = lp;
445   ++aconf->clients;
446   if (aconf->status & CONF_CLIENT_MASK)
447     ConfLinks(aconf)++;
448   return ACR_OK;
449 }
450
451 /** Return our LocalConf configuration structure.
452  * @return A pointer to #localConf.
453  */
454 const struct LocalConf* conf_get_local(void)
455 {
456   return &localConf;
457 }
458
459 /** Attach ConfItems to a client if the name passed matches that for
460  * the ConfItems or is an exact match for them.
461  * @param cptr Client getting the ConfItem attachments.
462  * @param name Filter to match ConfItem::name.
463  * @param statmask Filter to limit ConfItem::status.
464  * @return First ConfItem attached to \a cptr.
465  */
466 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
467                                      int statmask)
468 {
469   struct ConfItem* tmp;
470   struct ConfItem* first = NULL;
471
472   assert(0 != name);
473
474   if (HOSTLEN < strlen(name))
475     return 0;
476
477   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
478     if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
479       assert(0 != tmp->name);
480       if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) { 
481         if (ACR_OK == attach_conf(cptr, tmp) && !first)
482           first = tmp;
483       }
484     }
485   }
486   return first;
487 }
488
489 /** Attach ConfItems to a client if the host passed matches that for
490  * the ConfItems or is an exact match for them.
491  * @param cptr Client getting the ConfItem attachments.
492  * @param host Filter to match ConfItem::host.
493  * @param statmask Filter to limit ConfItem::status.
494  * @return First ConfItem attached to \a cptr.
495  */
496 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
497                                      int statmask)
498 {
499   struct ConfItem* tmp;
500   struct ConfItem* first = 0;
501
502   assert(0 != host);
503   if (HOSTLEN < strlen(host))
504     return 0;
505
506   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
507     if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
508       assert(0 != tmp->host);
509       if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) { 
510         if (ACR_OK == attach_conf(cptr, tmp) && !first)
511           first = tmp;
512       }
513     }
514   }
515   return first;
516 }
517
518 /** Find a ConfItem that has the same name and user+host fields as
519  * specified.  Requires an exact match for \a name.
520  * @param name Name to match
521  * @param user User part of match (or NULL)
522  * @param host Hostname part of match
523  * @param statmask Filter for ConfItem::status
524  * @return First found matching ConfItem.
525  */
526 struct ConfItem* find_conf_exact(const char* name, const char* user,
527                                  const char* host, int statmask)
528 {
529   struct ConfItem *tmp;
530   char userhost[USERLEN + HOSTLEN + 3];
531
532   if (user)
533     ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
534   else
535     ircd_strncpy(userhost, host, sizeof(userhost) - 1);
536
537   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
538     if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
539         0 != ircd_strcmp(tmp->name, name))
540       continue;
541     /*
542      * Accept if the *real* hostname (usually sockecthost)
543      * socket host) matches *either* host or name field
544      * of the configuration.
545      */
546     if (match(tmp->host, userhost))
547       continue;
548     if (tmp->status & CONF_OPERATOR) {
549       if (tmp->clients < MaxLinks(tmp->conn_class))
550         return tmp;
551       else
552         continue;
553     }
554     else
555       return tmp;
556   }
557   return 0;
558 }
559
560 /** Find a ConfItem from a list that has a name that matches \a name.
561  * @param lp List to search in.
562  * @param name Filter for ConfItem::name field; matches either exactly
563  * or as a glob.
564  * @param statmask Filter for ConfItem::status.
565  * @return First matching ConfItem from \a lp.
566  */
567 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
568                                   int statmask)
569 {
570   struct ConfItem* tmp;
571   assert(0 != name);
572
573   if (HOSTLEN < strlen(name))
574     return 0;
575
576   for (; lp; lp = lp->next) {
577     tmp = lp->value.aconf;
578     if (0 != (tmp->status & statmask)) {
579       assert(0 != tmp->name);
580       if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
581         return tmp;
582     }
583   }
584   return 0;
585 }
586
587 /** Find a ConfItem from a list that has a host that matches \a host.
588  * @param lp List to search in.
589  * @param host Filter for ConfItem::host field; matches as a glob.
590  * @param statmask Filter for ConfItem::status.
591  * @return First matching ConfItem from \a lp.
592  */
593 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
594                                   int statmask)
595 {
596   struct ConfItem* tmp = NULL;
597   assert(0 != host);
598
599   if (HOSTLEN < strlen(host))
600     return 0;
601
602   for (; lp; lp = lp->next) {
603     tmp = lp->value.aconf;
604     if (0 != (tmp->status & statmask)) {
605       assert(0 != tmp->host);
606       if (0 == match(tmp->host, host))
607         return tmp;
608     }
609   }
610   return 0;
611 }
612
613 /** Find a ConfItem from a list that has an address equal to \a ip.
614  * @param lp List to search in.
615  * @param ip Filter for ConfItem::address field; matches exactly.
616  * @param statmask Filter for ConfItem::status.
617  * @return First matching ConfItem from \a lp.
618  */
619 struct ConfItem* find_conf_byip(struct SLink* lp, const struct irc_in_addr* ip,
620                                 int statmask)
621 {
622   struct ConfItem* tmp;
623
624   for (; lp; lp = lp->next) {
625     tmp = lp->value.aconf;
626     if (0 != (tmp->status & statmask)
627         && !irc_in_addr_cmp(&tmp->address.addr, ip))
628       return tmp;
629   }
630   return 0;
631 }
632
633 /** Free all CRules from #cruleConfList. */
634 void conf_erase_crule_list(void)
635 {
636   struct CRuleConf* next;
637   struct CRuleConf* p = cruleConfList;
638
639   for ( ; p; p = next) {
640     next = p->next;
641     crule_free(&p->node);
642     MyFree(p->hostmask);
643     MyFree(p->rule);
644     MyFree(p);
645   }
646   cruleConfList = 0;
647 }
648
649 /** Return #cruleConfList.
650  * @return #cruleConfList
651  */
652 const struct CRuleConf* conf_get_crule_list(void)
653 {
654   return cruleConfList;
655 }
656
657 /** Free all deny rules from #denyConfList. */
658 void conf_erase_deny_list(void)
659 {
660   struct DenyConf* next;
661   struct DenyConf* p = denyConfList;
662   for ( ; p; p = next) {
663     next = p->next;
664     MyFree(p->hostmask);
665     MyFree(p->usermask);
666     MyFree(p->message);
667     MyFree(p);
668   }
669   denyConfList = 0;
670 }
671
672 /** Return #denyConfList.
673  * @return #denyConfList
674  */
675 const struct DenyConf* conf_get_deny_list(void)
676 {
677   return denyConfList;
678 }
679
680 /** Find any existing quarantine for the named channel.
681  * @param chname Channel name to search for.
682  * @return Reason for channel's quarantine, or NULL if none exists.
683  */
684 const char*
685 find_quarantine(const char *chname)
686 {
687   struct qline *qline;
688
689   for (qline = GlobalQuarantineList; qline; qline = qline->next)
690     if (!ircd_strcmp(qline->chname, chname))
691       return qline->reason;
692   return NULL;
693 }
694
695 /** Free all qline structs from #GlobalQuarantineList. */
696 void clear_quarantines(void)
697 {
698   struct qline *qline;
699   while ((qline = GlobalQuarantineList))
700   {
701     GlobalQuarantineList = qline->next;
702     MyFree(qline->reason);
703     MyFree(qline->chname);
704     MyFree(qline);
705   }
706 }
707
708 /** When non-zero, indicates that a configuration error has been seen in this pass. */
709 static int conf_error;
710 /** When non-zero, indicates that the configuration file was loaded at least once. */
711 static int conf_already_read;
712 extern FILE *yyin;
713 extern void yyparse(void);
714 extern void init_lexer(void);
715
716 /** Read configuration file.
717  * @return Zero on failure, non-zero on success. */
718 int read_configuration_file(void)
719 {
720   conf_error = 0;
721   feature_unmark(); /* unmark all features for resetting later */
722   /* Now just open an fd. The buffering isn't really needed... */
723   init_lexer();
724   yyparse();
725   fclose(yyin);
726   yyin = NULL;
727   feature_mark(); /* reset unmarked features */
728   conf_already_read = 1;
729   return 1;
730 }
731
732 /** Report an error message about the configuration file.
733  * @param msg The error to report.
734  */
735 void
736 yyerror(const char *msg)
737 {
738  sendto_opmask_butone(0, SNO_ALL, "Config file parse error line %d: %s",
739                       lineno, msg);
740  log_write(LS_CONFIG, L_ERROR, 0, "Config file parse error line %d: %s",
741            lineno, msg);
742  if (!conf_already_read)
743    fprintf(stderr, "Config file parse error line %d: %s\n", lineno, msg);
744  conf_error = 1;
745 }
746
747 /** Reload the configuration file.
748  * @param cptr Client that requested rehash (if a signal, &me).
749  * @param sig Type of rehash (0 = oper-requested, 1 = signal, 2 =
750  *   oper-requested but do not restart resolver)
751  * @return CPTR_KILLED if any client was K/G-lined because of the
752  * rehash; otherwise 0.
753  */
754 int rehash(struct Client *cptr, int sig)
755 {
756   struct ConfItem** tmp = &GlobalConfList;
757   struct ConfItem*  tmp2;
758   struct Client*    acptr;
759   int               i;
760   int               ret = 0;
761   int               found_g = 0;
762
763   if (1 == sig)
764     sendto_opmask_butone(0, SNO_OLDSNO,
765                          "Got signal SIGHUP, reloading ircd conf. file");
766
767   while ((tmp2 = *tmp)) {
768     if (tmp2->clients) {
769       /*
770        * Configuration entry is still in use by some
771        * local clients, cannot delete it--mark it so
772        * that it will be deleted when the last client
773        * exits...
774        */
775       if (CONF_CLIENT == (tmp2->status & CONF_CLIENT))
776         tmp = &tmp2->next;
777       else {
778         *tmp = tmp2->next;
779         tmp2->next = 0;
780       }
781       tmp2->status |= CONF_ILLEGAL;
782     }
783     else {
784       *tmp = tmp2->next;
785       free_conf(tmp2);
786     }
787   }
788   conf_erase_crule_list();
789   conf_erase_deny_list();
790   motd_clear();
791
792   /*
793    * delete the juped nicks list
794    */
795   clearNickJupes();
796
797   clear_quarantines();
798
799   if (sig != 2)
800     restart_resolver();
801
802   class_mark_delete();
803   mark_listeners_closing();
804   iauth_mark_closing();
805
806   read_configuration_file();
807
808   log_reopen(); /* reopen log files */
809
810   iauth_close_unused();
811   close_listeners();
812   class_delete_marked();         /* unless it fails */
813
814   /*
815    * Flush out deleted I and P lines although still in use.
816    */
817   for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
818     if (CONF_ILLEGAL == (tmp2->status & CONF_ILLEGAL)) {
819       *tmp = tmp2->next;
820       tmp2->next = NULL;
821       if (!tmp2->clients)
822         free_conf(tmp2);
823     }
824     else
825       tmp = &tmp2->next;
826   }
827
828   for (i = 0; i <= HighestFd; i++) {
829     if ((acptr = LocalClientArray[i])) {
830       assert(!IsMe(acptr));
831       if (IsServer(acptr)) {
832         det_confs_butmask(acptr,
833             ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
834         attach_confs_byname(acptr, cli_name(acptr),
835                             CONF_HUB | CONF_LEAF | CONF_UWORLD);
836       }
837       /* Because admin's are getting so uppity about people managing to
838        * get past K/G's etc, we'll "fix" the bug by actually explaining
839        * whats going on.
840        */
841       if ((found_g = find_kill(acptr))) {
842         sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
843                              found_g == -2 ? "G-line active for %s%s" :
844                              "K-line active for %s%s",
845                              IsUnknown(acptr) ? "Unregistered Client ":"",
846                              get_client_name(acptr, SHOW_IP));
847         if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
848             "K-lined") == CPTR_KILLED)
849           ret = CPTR_KILLED;
850       }
851     }
852   }
853
854   return ret;
855 }
856
857 /** Read configuration file for the very first time.
858  * @return Non-zero on success, zero on failure.
859  */
860
861 int init_conf(void)
862 {
863   if (read_configuration_file()) {
864     /*
865      * make sure we're sane to start if the config
866      * file read didn't get everything we need.
867      * XXX - should any of these abort the server?
868      * TODO: add warning messages
869      */
870     if (0 == localConf.name || 0 == localConf.numeric)
871       return 0;
872     if (conf_error)
873       return 0;
874
875     if (0 == localConf.location1)
876       DupString(localConf.location1, "");
877     if (0 == localConf.location2)
878       DupString(localConf.location2, "");
879     if (0 == localConf.contact)
880       DupString(localConf.contact, "");
881
882     return 1;
883   }
884   return 0;
885 }
886
887 /** Searches for a K/G-line for a client.  If one is found, notify the
888  * user and disconnect them.
889  * @param cptr Client to search for.
890  * @return 0 if client is accepted; -1 if client was locally denied
891  * (K-line); -2 if client was globally denied (G-line).
892  */
893 int find_kill(struct Client *cptr)
894 {
895   const char*      host;
896   const char*      name;
897   const char*      realname;
898   struct DenyConf* deny;
899   struct Gline*    agline = NULL;
900
901   assert(0 != cptr);
902
903   if (!cli_user(cptr))
904     return 0;
905
906   host = cli_sockhost(cptr);
907   name = cli_user(cptr)->username;
908   realname = cli_info(cptr);
909
910   assert(strlen(host) <= HOSTLEN);
911   assert((name ? strlen(name) : 0) <= HOSTLEN);
912   assert((realname ? strlen(realname) : 0) <= REALLEN);
913
914   /* 2000-07-14: Rewrote this loop for massive speed increases.
915    *             -- Isomer
916    */
917   for (deny = denyConfList; deny; deny = deny->next) {
918     if (0 != match(deny->usermask, name))
919       continue;
920
921     if (EmptyString(deny->hostmask))
922       break;
923
924     if (deny->flags & DENY_FLAGS_REALNAME) { /* K: by real name */
925       if (0 == match(deny->hostmask + 2, realname))
926         break;
927     } else if (deny->flags & DENY_FLAGS_IP) { /* k: by IP */
928 #ifdef DEBUGMODE
929       char tbuf1[SOCKIPLEN], tbuf2[SOCKIPLEN];
930       Debug((DEBUG_DEBUG, "ip: %s network: %s/%u",
931              ircd_ntoa_r(tbuf1, &cli_ip(cptr)), ircd_ntoa_r(tbuf2, &deny->address), deny->bits));
932 #endif
933       if (ipmask_check(&cli_ip(cptr), &deny->address, deny->bits))
934         break;
935     }
936     else if (0 == match(deny->hostmask, host))
937       break;
938   }
939   if (deny) {
940     if (EmptyString(deny->message))
941       send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
942                  ":Connection from your host is refused on this server.");
943     else {
944       if (deny->flags & DENY_FLAGS_FILE)
945         killcomment(cptr, deny->message);
946       else
947         send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", deny->message);
948     }
949   }
950   else if ((agline = gline_lookup(cptr, 0))) {
951     /*
952      * find active glines
953      * added a check against the user's IP address to find_gline() -Kev
954      */
955     send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", GlineReason(agline));
956   }
957
958   if (deny)
959     return -1;
960   if (agline)
961     return -2;
962
963   return 0;
964 }
965
966 /** Attempt to attach Client blocks to \a cptr.  If attach_iline()
967  * fails for the client, emit a debugging message.
968  * @param cptr Client to check for access.
969  * @return Access check result.
970  */
971 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
972 {
973   enum AuthorizationCheckResult acr = ACR_OK;
974
975   ClearAccess(cptr);
976
977   if ((acr = attach_iline(cptr))) {
978     Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]", 
979           cli_name(cptr), cli_sockhost(cptr)));
980     return acr;
981   }
982   return ACR_OK;
983 }
984
985 /** Check access for a server given its name (passed in cptr struct).
986  * Must check for all C/N lines which have a name which matches the
987  * name given and a host which matches. A host alias which is the
988  * same as the server name is also acceptable in the host field of a
989  * C/N line.
990  * @param cptr Peer server to check.
991  * @return 0 if accepted, -1 if access denied.
992  */
993 int conf_check_server(struct Client *cptr)
994 {
995   struct ConfItem* c_conf = NULL;
996   struct SLink*    lp;
997
998   Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]", 
999         cli_name(cptr), cli_sockhost(cptr)));
1000
1001   if (IsUnknown(cptr) && !attach_confs_byname(cptr, cli_name(cptr), CONF_SERVER)) {
1002     Debug((DEBUG_DNS, "No C/N lines for %s", cli_sockhost(cptr)));
1003     return -1;
1004   }
1005   lp = cli_confs(cptr);
1006   /*
1007    * We initiated this connection so the client should have a C and N
1008    * line already attached after passing through the connect_server()
1009    * function earlier.
1010    */
1011   if (IsConnecting(cptr) || IsHandshake(cptr)) {
1012     c_conf = find_conf_byname(lp, cli_name(cptr), CONF_SERVER);
1013     if (!c_conf) {
1014       sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1015                            cli_name(cptr));
1016       det_confs_butmask(cptr, 0);
1017       return -1;
1018     }
1019   }
1020
1021   ClearAccess(cptr);
1022
1023   if (!c_conf) {
1024     if (cli_dns_reply(cptr)) {
1025       struct DNSReply* hp = cli_dns_reply(cptr);
1026       const char*     name = hp->h_name;
1027       /*
1028        * If we are missing a C or N line from above, search for
1029        * it under all known hostnames we have for this ip#.
1030        */
1031       if ((c_conf = find_conf_byhost(lp, hp->h_name, CONF_SERVER)))
1032         ircd_strncpy(cli_sockhost(cptr), name, HOSTLEN);
1033       else
1034         c_conf = find_conf_byip(lp, &hp->addr, CONF_SERVER);
1035     }
1036     else {
1037       /*
1038        * Check for C lines with the hostname portion the ip number
1039        * of the host the server runs on. This also checks the case where
1040        * there is a server connecting from 'localhost'.
1041        */
1042       c_conf = find_conf_byhost(lp, cli_sockhost(cptr), CONF_SERVER);
1043     }
1044   }
1045   /*
1046    * Attach by IP# only if all other checks have failed.
1047    * It is quite possible to get here with the strange things that can
1048    * happen when using DNS in the way the irc server does. -avalon
1049    */
1050   if (!c_conf)
1051     c_conf = find_conf_byip(lp, &cli_ip(cptr), CONF_SERVER);
1052   /*
1053    * detach all conf lines that got attached by attach_confs()
1054    */
1055   det_confs_butmask(cptr, 0);
1056   /*
1057    * if no C or no N lines, then deny access
1058    */
1059   if (!c_conf) {
1060     Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1061           cli_name(cptr), cli_username(cptr), cli_sockhost(cptr)));
1062     return -1;
1063   }
1064   ircd_strncpy(cli_name(cptr), c_conf->name, HOSTLEN);
1065   /*
1066    * attach the C and N lines to the client structure for later use.
1067    */
1068   attach_conf(cptr, c_conf);
1069   attach_confs_byname(cptr, cli_name(cptr), CONF_HUB | CONF_LEAF | CONF_UWORLD);
1070
1071   if (!irc_in_addr_valid(&c_conf->address.addr))
1072     memcpy(&c_conf->address.addr, &cli_ip(cptr), sizeof(c_conf->address.addr));
1073
1074   Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]",
1075          cli_name(cptr), cli_sockhost(cptr)));
1076   return 0;
1077 }
1078