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