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