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