12a5ac806fee359a969a2929f013daebe606f8a3
[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> -- Now using assert in ircd_log.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 #GlobalConfList. */
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 and link it to #GlobalConfList.
125  * @return Newly allocated structure.
126  */
127 struct ConfItem* make_conf(int type)
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  = type;
138   aconf->next    = GlobalConfList;
139   GlobalConfList = aconf;
140   return aconf;
141 }
142
143 /** Free a struct ConfItem and any resources it owns.
144  * @param aconf Item to free.
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 /** Disassociate configuration from the client.
166  * @param cptr Client to operate on.
167  * @param aconf ConfItem to detach.
168  */
169 static void detach_conf(struct Client* cptr, struct ConfItem* aconf)
170 {
171   struct SLink** lp;
172   struct SLink*  tmp;
173
174   assert(0 != aconf);
175   assert(0 != cptr);
176   assert(0 < aconf->clients);
177
178   lp = &(cli_confs(cptr));
179
180   while (*lp) {
181     if ((*lp)->value.aconf == aconf) {
182       if (aconf->conn_class && (aconf->status & CONF_CLIENT_MASK) && ConfLinks(aconf) > 0)
183         --ConfLinks(aconf);
184
185       assert(0 < aconf->clients);
186       if (0 == --aconf->clients && IsIllegal(aconf))
187         free_conf(aconf);
188       tmp = *lp;
189       *lp = tmp->next;
190       free_link(tmp);
191       return;
192     }
193     lp = &((*lp)->next);
194   }
195 }
196
197 /** Parse a user\@host mask into username and host or IP parts.
198  * If \a host contains no username part, set \a aconf->username to
199  * NULL.  If the host part of \a host looks like an IP mask, set \a
200  * aconf->addrbits and \a aconf->address to match.  Otherwise, set
201  * \a aconf->host, and set \a aconf->addrbits to -1.
202  * @param[in,out] aconf Configuration item to set.
203  * @param[in] host user\@host mask to parse.
204  */
205 void conf_parse_userhost(struct ConfItem *aconf, char *host)
206 {
207   char *host_part;
208   unsigned char addrbits;
209
210   MyFree(aconf->username);
211   MyFree(aconf->host);
212   host_part = strchr(host, '@');
213   if (host_part) {
214     *host_part = '\0';
215     DupString(aconf->username, host);
216     host_part++;
217   } else {
218     aconf->username = NULL;
219     host_part = host;
220   }
221   DupString(aconf->host, host_part);
222   if (ipmask_parse(aconf->host, &aconf->address.addr, &addrbits))
223     aconf->addrbits = addrbits;
224   else
225     aconf->addrbits = -1;
226   MyFree(host);
227 }
228
229 /** Copies a completed DNS query into its ConfItem.
230  * @param vptr Pointer to struct ConfItem for the block.
231  * @param hp DNS reply, or NULL if the lookup failed.
232  */
233 static void conf_dns_callback(void* vptr, struct DNSReply* hp)
234 {
235   struct ConfItem* aconf = (struct ConfItem*) vptr;
236   assert(aconf);
237   aconf->dns_pending = 0;
238   if (hp) {
239     memcpy(&aconf->address.addr, &hp->addr, sizeof(aconf->address.addr));
240     MyFree(hp);
241   }
242 }
243
244 /** Start a nameserver lookup of the conf host.  If the conf entry is
245  * currently doing a lookup, do nothing.
246  * @param aconf ConfItem for which to start a request.
247  */
248 static void conf_dns_lookup(struct ConfItem* aconf)
249 {
250   if (!aconf->dns_pending) {
251     char            buf[HOSTLEN + 1];
252     struct DNSQuery query;
253     query.vptr     = aconf;
254     query.callback = conf_dns_callback;
255     host_from_uh(buf, aconf->host, HOSTLEN);
256     buf[HOSTLEN] = '\0';
257
258     gethost_byname(buf, &query);
259     aconf->dns_pending = 1;
260   }
261 }
262
263
264 /** Start lookups of all addresses in the conf line.  The origin must
265  * be a numeric IP address.  If the remote host field is not an IP
266  * address, start a DNS lookup for it.
267  * @param aconf Connection to do lookups for.
268  */
269 void
270 lookup_confhost(struct ConfItem *aconf)
271 {
272   if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
273     Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
274            aconf->host, aconf->name));
275     return;
276   }
277   if (aconf->origin_name
278       && !ircd_aton(&aconf->origin.addr, aconf->origin_name)) {
279     Debug((DEBUG_ERROR, "Origin name error: (%s) (%s)",
280         aconf->origin_name, aconf->name));
281   }
282   /*
283    * Do name lookup now on hostnames given and store the
284    * ip numbers in conf structure.
285    */
286   if (IsIP6Char(*aconf->host)) {
287     if (!ircd_aton(&aconf->address.addr, aconf->host)) {
288       Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
289           aconf->host, aconf->name));
290     }
291   }
292   else
293     conf_dns_lookup(aconf);
294 }
295
296 /** Find a server by name or hostname.
297  * @param name Server name to find.
298  * @return Pointer to the corresponding ConfItem, or NULL if none exists.
299  */
300 struct ConfItem* conf_find_server(const char* name)
301 {
302   struct ConfItem* conf;
303   assert(0 != name);
304
305   for (conf = GlobalConfList; conf; conf = conf->next) {
306     if (CONF_SERVER == conf->status) {
307       /*
308        * Check first servernames, then try hostnames.
309        * XXX - match returns 0 if there _is_ a match... guess they
310        * haven't decided what true is yet
311        */
312       if (0 == match(name, conf->name))
313         return conf;
314     }
315   }
316   return 0;
317 }
318
319 /** Evaluate connection rules.
320  * @param name Name of server to check
321  * @param mask Filter for CRule types (only consider if type & \a mask != 0).
322  * @return Name of rule that forbids the connection; NULL if no prohibitions.
323  */
324 const char* conf_eval_crule(const char* name, int mask)
325 {
326   struct CRuleConf* p = cruleConfList;
327   assert(0 != name);
328
329   for ( ; p; p = p->next) {
330     if (0 != (p->type & mask) && 0 == match(p->hostmask, name)) {
331       if (crule_eval(p->node))
332         return p->rule;
333     }
334   }
335   return 0;
336 }
337
338 /** Remove all conf entries from the client except those which match
339  * the status field mask.
340  * @param cptr Client to operate on.
341  * @param mask ConfItem types to keep.
342  */
343 void det_confs_butmask(struct Client* cptr, int mask)
344 {
345   struct SLink* link;
346   struct SLink* next;
347   assert(0 != cptr);
348
349   for (link = cli_confs(cptr); link; link = next) {
350     next = link->next;
351     if ((link->value.aconf->status & mask) == 0)
352       detach_conf(cptr, link->value.aconf);
353   }
354 }
355
356 /** Check client limits and attach Client block.
357  * If there are more connections from the IP than \a aconf->maximum
358  * allows, return ACR_TOO_MANY_FROM_IP.  Otherwise, attach \a aconf to
359  * \a cptr.
360  * @param cptr Client getting \a aconf.
361  * @param aconf Configuration item to attach.
362  * @return Authorization check result.
363  */
364 static enum AuthorizationCheckResult
365 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
366 {
367   if (IPcheck_nr(cptr) > aconf->maximum)
368     return ACR_TOO_MANY_FROM_IP;
369   return attach_conf(cptr, aconf);
370 }
371
372 /** Find the first (best) Client block to attach.
373  * @param cptr Client for whom to check rules.
374  * @return Authorization check result.
375  */
376 enum AuthorizationCheckResult attach_iline(struct Client* cptr)
377 {
378   struct ConfItem* aconf;
379   struct DNSReply* hp;
380
381   assert(0 != cptr);
382
383   hp = cli_dns_reply(cptr);
384   for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
385     if (aconf->status != CONF_CLIENT || !aconf->host)
386       continue;
387     if (aconf->address.port && aconf->address.port != cli_listener(cptr)->addr.port)
388       continue;
389     if (aconf->username) {
390       SetFlag(cptr, FLAG_DOID);
391       if (match(aconf->username, cli_username(cptr)))
392         continue;
393     }
394     if (hp) {
395       Debug((DEBUG_DNS, "a_il: %s->%s", cli_sockhost(cptr), hp->h_name));
396       if (!match(aconf->host, hp->h_name))
397         return check_limit_and_attach(cptr, aconf);
398     }
399     if ((aconf->addrbits >= 0)
400         && ipmask_check(&cli_ip(cptr), &aconf->address.addr, aconf->addrbits))
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 cptr Client to match against
522  * @param statmask Filter for ConfItem::status
523  * @return First found matching ConfItem.
524  */
525 struct ConfItem* find_conf_exact(const char* name, struct Client *cptr, int statmask)
526 {
527   struct ConfItem *tmp;
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     if (tmp->username
534         && (EmptyString(cli_username(cptr))
535             || match(tmp->username, cli_username(cptr))))
536       continue;
537     if (tmp->addrbits < 0)
538     {
539       if (match(tmp->host, cli_sockhost(cptr)))
540         continue;
541     }
542     else if (!ipmask_check(&cli_ip(cptr), &tmp->address.addr, tmp->addrbits))
543       continue;
544     if ((tmp->status & CONF_OPERATOR)
545         && (tmp->clients >= MaxLinks(tmp->conn_class)))
546       continue;
547     return tmp;
548   }
549   return 0;
550 }
551
552 /** Find a ConfItem from a list that has a name that matches \a name.
553  * @param lp List to search in.
554  * @param name Filter for ConfItem::name field; matches either exactly
555  * or as a glob.
556  * @param statmask Filter for ConfItem::status.
557  * @return First matching ConfItem from \a lp.
558  */
559 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
560                                   int statmask)
561 {
562   struct ConfItem* tmp;
563   assert(0 != name);
564
565   if (HOSTLEN < strlen(name))
566     return 0;
567
568   for (; lp; lp = lp->next) {
569     tmp = lp->value.aconf;
570     if (0 != (tmp->status & statmask)) {
571       assert(0 != tmp->name);
572       if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
573         return tmp;
574     }
575   }
576   return 0;
577 }
578
579 /** Find a ConfItem from a list that has a host that matches \a host.
580  * @param lp List to search in.
581  * @param host Filter for ConfItem::host field; matches as a glob.
582  * @param statmask Filter for ConfItem::status.
583  * @return First matching ConfItem from \a lp.
584  */
585 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
586                                   int statmask)
587 {
588   struct ConfItem* tmp = NULL;
589   assert(0 != host);
590
591   if (HOSTLEN < strlen(host))
592     return 0;
593
594   for (; lp; lp = lp->next) {
595     tmp = lp->value.aconf;
596     if (0 != (tmp->status & statmask)) {
597       assert(0 != tmp->host);
598       if (0 == match(tmp->host, host))
599         return tmp;
600     }
601   }
602   return 0;
603 }
604
605 /** Find a ConfItem from a list that has an address equal to \a ip.
606  * @param lp List to search in.
607  * @param ip Filter for ConfItem::address field; matches exactly.
608  * @param statmask Filter for ConfItem::status.
609  * @return First matching ConfItem from \a lp.
610  */
611 struct ConfItem* find_conf_byip(struct SLink* lp, const struct irc_in_addr* ip,
612                                 int statmask)
613 {
614   struct ConfItem* tmp;
615
616   for (; lp; lp = lp->next) {
617     tmp = lp->value.aconf;
618     if (0 != (tmp->status & statmask)
619         && !irc_in_addr_cmp(&tmp->address.addr, ip))
620       return tmp;
621   }
622   return 0;
623 }
624
625 /** Free all CRules from #cruleConfList. */
626 void conf_erase_crule_list(void)
627 {
628   struct CRuleConf* next;
629   struct CRuleConf* p = cruleConfList;
630
631   for ( ; p; p = next) {
632     next = p->next;
633     crule_free(&p->node);
634     MyFree(p->hostmask);
635     MyFree(p->rule);
636     MyFree(p);
637   }
638   cruleConfList = 0;
639 }
640
641 /** Return #cruleConfList.
642  * @return #cruleConfList
643  */
644 const struct CRuleConf* conf_get_crule_list(void)
645 {
646   return cruleConfList;
647 }
648
649 /** Free all deny rules from #denyConfList. */
650 void conf_erase_deny_list(void)
651 {
652   struct DenyConf* next;
653   struct DenyConf* p = denyConfList;
654   for ( ; p; p = next) {
655     next = p->next;
656     MyFree(p->hostmask);
657     MyFree(p->usermask);
658     MyFree(p->message);
659     MyFree(p);
660   }
661   denyConfList = 0;
662 }
663
664 /** Return #denyConfList.
665  * @return #denyConfList
666  */
667 const struct DenyConf* conf_get_deny_list(void)
668 {
669   return denyConfList;
670 }
671
672 /** Find any existing quarantine for the named channel.
673  * @param chname Channel name to search for.
674  * @return Reason for channel's quarantine, or NULL if none exists.
675  */
676 const char*
677 find_quarantine(const char *chname)
678 {
679   struct qline *qline;
680
681   for (qline = GlobalQuarantineList; qline; qline = qline->next)
682     if (!ircd_strcmp(qline->chname, chname))
683       return qline->reason;
684   return NULL;
685 }
686
687 /** Free all qline structs from #GlobalQuarantineList. */
688 void clear_quarantines(void)
689 {
690   struct qline *qline;
691   while ((qline = GlobalQuarantineList))
692   {
693     GlobalQuarantineList = qline->next;
694     MyFree(qline->reason);
695     MyFree(qline->chname);
696     MyFree(qline);
697   }
698 }
699
700 /** When non-zero, indicates that a configuration error has been seen in this pass. */
701 static int conf_error;
702 /** When non-zero, indicates that the configuration file was loaded at least once. */
703 static int conf_already_read;
704 extern FILE *yyin;
705 extern void yyparse(void);
706 extern void init_lexer(void);
707
708 /** Read configuration file.
709  * @return Zero on failure, non-zero on success. */
710 int read_configuration_file(void)
711 {
712   conf_error = 0;
713   feature_unmark(); /* unmark all features for resetting later */
714   /* Now just open an fd. The buffering isn't really needed... */
715   init_lexer();
716   yyparse();
717   fclose(yyin);
718   yyin = NULL;
719   feature_mark(); /* reset unmarked features */
720   conf_already_read = 1;
721   return 1;
722 }
723
724 /** Report an error message about the configuration file.
725  * @param msg The error to report.
726  */
727 void
728 yyerror(const char *msg)
729 {
730  sendto_opmask_butone(0, SNO_ALL, "Config file parse error line %d: %s",
731                       lineno, msg);
732  log_write(LS_CONFIG, L_ERROR, 0, "Config file parse error line %d: %s",
733            lineno, msg);
734  if (!conf_already_read)
735    fprintf(stderr, "Config file parse error line %d: %s\n", lineno, msg);
736  conf_error = 1;
737 }
738
739 /** Reload the configuration file.
740  * @param cptr Client that requested rehash (if a signal, &me).
741  * @param sig Type of rehash (0 = oper-requested, 1 = signal, 2 =
742  *   oper-requested but do not restart resolver)
743  * @return CPTR_KILLED if any client was K/G-lined because of the
744  * rehash; otherwise 0.
745  */
746 int rehash(struct Client *cptr, int sig)
747 {
748   struct ConfItem** tmp = &GlobalConfList;
749   struct ConfItem*  tmp2;
750   struct Client*    acptr;
751   int               i;
752   int               ret = 0;
753   int               found_g = 0;
754
755   if (1 == sig)
756     sendto_opmask_butone(0, SNO_OLDSNO,
757                          "Got signal SIGHUP, reloading ircd conf. file");
758
759   while ((tmp2 = *tmp)) {
760     if (tmp2->clients) {
761       /*
762        * Configuration entry is still in use by some
763        * local clients, cannot delete it--mark it so
764        * that it will be deleted when the last client
765        * exits...
766        */
767       if (CONF_CLIENT == (tmp2->status & CONF_CLIENT))
768         tmp = &tmp2->next;
769       else {
770         *tmp = tmp2->next;
771         tmp2->next = 0;
772       }
773       tmp2->status |= CONF_ILLEGAL;
774     }
775     else {
776       *tmp = tmp2->next;
777       free_conf(tmp2);
778     }
779   }
780   conf_erase_crule_list();
781   conf_erase_deny_list();
782   motd_clear();
783
784   /*
785    * delete the juped nicks list
786    */
787   clearNickJupes();
788
789   clear_quarantines();
790
791   if (sig != 2)
792     restart_resolver();
793
794   class_mark_delete();
795   mark_listeners_closing();
796   iauth_mark_closing();
797
798   read_configuration_file();
799
800   log_reopen(); /* reopen log files */
801
802   iauth_close_unused();
803   close_listeners();
804   class_delete_marked();         /* unless it fails */
805
806   /*
807    * Flush out deleted I and P lines although still in use.
808    */
809   for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
810     if (CONF_ILLEGAL == (tmp2->status & CONF_ILLEGAL)) {
811       *tmp = tmp2->next;
812       tmp2->next = NULL;
813       if (!tmp2->clients)
814         free_conf(tmp2);
815     }
816     else
817       tmp = &tmp2->next;
818   }
819
820   for (i = 0; i <= HighestFd; i++) {
821     if ((acptr = LocalClientArray[i])) {
822       assert(!IsMe(acptr));
823       if (IsServer(acptr)) {
824         det_confs_butmask(acptr, ~(CONF_UWORLD | CONF_ILLEGAL));
825         attach_confs_byname(acptr, cli_name(acptr), CONF_UWORLD);
826       }
827       /* Because admin's are getting so uppity about people managing to
828        * get past K/G's etc, we'll "fix" the bug by actually explaining
829        * whats going on.
830        */
831       if ((found_g = find_kill(acptr))) {
832         sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
833                              found_g == -2 ? "G-line active for %s%s" :
834                              "K-line active for %s%s",
835                              IsUnknown(acptr) ? "Unregistered Client ":"",
836                              get_client_name(acptr, SHOW_IP));
837         if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
838             "K-lined") == CPTR_KILLED)
839           ret = CPTR_KILLED;
840       }
841     }
842   }
843
844   return ret;
845 }
846
847 /** Read configuration file for the very first time.
848  * @return Non-zero on success, zero on failure.
849  */
850
851 int init_conf(void)
852 {
853   if (read_configuration_file()) {
854     /*
855      * make sure we're sane to start if the config
856      * file read didn't get everything we need.
857      * XXX - should any of these abort the server?
858      * TODO: add warning messages
859      */
860     if (0 == localConf.name || 0 == localConf.numeric)
861       return 0;
862     if (conf_error)
863       return 0;
864
865     if (0 == localConf.location1)
866       DupString(localConf.location1, "");
867     if (0 == localConf.location2)
868       DupString(localConf.location2, "");
869     if (0 == localConf.contact)
870       DupString(localConf.contact, "");
871
872     return 1;
873   }
874   return 0;
875 }
876
877 /** Searches for a K/G-line for a client.  If one is found, notify the
878  * user and disconnect them.
879  * @param cptr Client to search for.
880  * @return 0 if client is accepted; -1 if client was locally denied
881  * (K-line); -2 if client was globally denied (G-line).
882  */
883 int find_kill(struct Client *cptr)
884 {
885   const char*      host;
886   const char*      name;
887   const char*      realname;
888   struct DenyConf* deny;
889   struct Gline*    agline = NULL;
890
891   assert(0 != cptr);
892
893   if (!cli_user(cptr))
894     return 0;
895
896   host = cli_sockhost(cptr);
897   name = cli_user(cptr)->username;
898   realname = cli_info(cptr);
899
900   assert(strlen(host) <= HOSTLEN);
901   assert((name ? strlen(name) : 0) <= HOSTLEN);
902   assert((realname ? strlen(realname) : 0) <= REALLEN);
903
904   /* 2000-07-14: Rewrote this loop for massive speed increases.
905    *             -- Isomer
906    */
907   for (deny = denyConfList; deny; deny = deny->next) {
908     if (0 != match(deny->usermask, name))
909       continue;
910
911     if (EmptyString(deny->hostmask))
912       break;
913
914     if (deny->flags & DENY_FLAGS_REALNAME) { /* K: by real name */
915       if (0 == match(deny->hostmask + 2, realname))
916         break;
917     } else if (deny->flags & DENY_FLAGS_IP) { /* k: by IP */
918 #ifdef DEBUGMODE
919       char tbuf1[SOCKIPLEN], tbuf2[SOCKIPLEN];
920       Debug((DEBUG_DEBUG, "ip: %s network: %s/%u",
921              ircd_ntoa_r(tbuf1, &cli_ip(cptr)), ircd_ntoa_r(tbuf2, &deny->address), deny->bits));
922 #endif
923       if (ipmask_check(&cli_ip(cptr), &deny->address, deny->bits))
924         break;
925     }
926     else if (0 == match(deny->hostmask, host))
927       break;
928   }
929   if (deny) {
930     if (EmptyString(deny->message))
931       send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
932                  ":Connection from your host is refused on this server.");
933     else {
934       if (deny->flags & DENY_FLAGS_FILE)
935         killcomment(cptr, deny->message);
936       else
937         send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", deny->message);
938     }
939   }
940   else if ((agline = gline_lookup(cptr, 0))) {
941     /*
942      * find active glines
943      * added a check against the user's IP address to find_gline() -Kev
944      */
945     send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", GlineReason(agline));
946   }
947
948   if (deny)
949     return -1;
950   if (agline)
951     return -2;
952
953   return 0;
954 }
955
956 /** Attempt to attach Client blocks to \a cptr.  If attach_iline()
957  * fails for the client, emit a debugging message.
958  * @param cptr Client to check for access.
959  * @return Access check result.
960  */
961 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
962 {
963   enum AuthorizationCheckResult acr = ACR_OK;
964
965   if ((acr = attach_iline(cptr))) {
966     Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]", 
967           cli_name(cptr), cli_sockhost(cptr)));
968     return acr;
969   }
970   return ACR_OK;
971 }
972
973 /** Check access for a server given its name (passed in cptr struct).
974  * Must check for all C/N lines which have a name which matches the
975  * name given and a host which matches. A host alias which is the
976  * same as the server name is also acceptable in the host field of a
977  * C/N line.
978  * @param cptr Peer server to check.
979  * @return 0 if accepted, -1 if access denied.
980  */
981 int conf_check_server(struct Client *cptr)
982 {
983   struct ConfItem* c_conf = NULL;
984   struct SLink*    lp;
985
986   Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]", 
987         cli_name(cptr), cli_sockhost(cptr)));
988
989   if (IsUnknown(cptr) && !attach_confs_byname(cptr, cli_name(cptr), CONF_SERVER)) {
990     Debug((DEBUG_DNS, "No C/N lines for %s", cli_sockhost(cptr)));
991     return -1;
992   }
993   lp = cli_confs(cptr);
994   /*
995    * We initiated this connection so the client should have a C and N
996    * line already attached after passing through the connect_server()
997    * function earlier.
998    */
999   if (IsConnecting(cptr) || IsHandshake(cptr)) {
1000     c_conf = find_conf_byname(lp, cli_name(cptr), CONF_SERVER);
1001     if (!c_conf) {
1002       sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1003                            cli_name(cptr));
1004       det_confs_butmask(cptr, 0);
1005       return -1;
1006     }
1007   }
1008
1009   if (!c_conf) {
1010     if (cli_dns_reply(cptr)) {
1011       struct DNSReply* hp = cli_dns_reply(cptr);
1012       const char*     name = hp->h_name;
1013       /*
1014        * If we are missing a C or N line from above, search for
1015        * it under all known hostnames we have for this ip#.
1016        */
1017       if ((c_conf = find_conf_byhost(lp, hp->h_name, CONF_SERVER)))
1018         ircd_strncpy(cli_sockhost(cptr), name, HOSTLEN);
1019       else
1020         c_conf = find_conf_byip(lp, &hp->addr, CONF_SERVER);
1021     }
1022     else {
1023       /*
1024        * Check for C lines with the hostname portion the ip number
1025        * of the host the server runs on. This also checks the case where
1026        * there is a server connecting from 'localhost'.
1027        */
1028       c_conf = find_conf_byhost(lp, cli_sockhost(cptr), CONF_SERVER);
1029     }
1030   }
1031   /*
1032    * Attach by IP# only if all other checks have failed.
1033    * It is quite possible to get here with the strange things that can
1034    * happen when using DNS in the way the irc server does. -avalon
1035    */
1036   if (!c_conf)
1037     c_conf = find_conf_byip(lp, &cli_ip(cptr), CONF_SERVER);
1038   /*
1039    * detach all conf lines that got attached by attach_confs()
1040    */
1041   det_confs_butmask(cptr, 0);
1042   /*
1043    * if no Connect block, then deny access
1044    */
1045   if (!c_conf) {
1046     Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1047           cli_name(cptr), cli_username(cptr), cli_sockhost(cptr)));
1048     return -1;
1049   }
1050   /*
1051    * attach the Connect block to the client structure for later use.
1052    */
1053   attach_conf(cptr, c_conf);
1054   attach_confs_byname(cptr, cli_name(cptr), CONF_UWORLD);
1055
1056   if (!irc_in_addr_valid(&c_conf->address.addr))
1057     memcpy(&c_conf->address.addr, &cli_ip(cptr), sizeof(c_conf->address.addr));
1058
1059   Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]",
1060          cli_name(cptr), cli_sockhost(cptr)));
1061   return 0;
1062 }
1063