05cd7ce2b8e81a689cb835398401eb9a697495f9
[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 "sprintf_irc.h"
54 #include "struct.h"
55 #include "support.h"
56 #include "sys.h"
57
58 #include <assert.h>
59 #include <arpa/inet.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 #ifndef INADDR_NONE
70 #define INADDR_NONE 0xffffffff
71 #endif
72
73 struct ConfItem* GlobalConfList  = 0;
74 int              GlobalConfCount = 0;
75
76 static struct LocalConf   localConf;
77 static struct CRuleConf*  cruleConfList;
78 static struct ServerConf* serverConfList;
79 static struct DenyConf*   denyConfList;
80
81 /*
82  * output the reason for being k lined from a file  - Mmmm
83  * sptr is client being dumped
84  * filename is the file that is to be output to the K lined client
85  */
86 static void killcomment(struct Client* sptr, const char* filename)
87 {
88   FBFILE*     file = 0;
89   char        line[80];
90   struct stat sb;
91   struct tm*  tm;
92
93   if (NULL == (file = fbopen(filename, "r"))) {
94     send_reply(sptr, ERR_NOMOTD);
95     send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
96                ":Connection from your host is refused on this server.");
97     return;
98   }
99   fbstat(&sb, file);
100   tm = localtime((time_t*) &sb.st_mtime);        /* NetBSD needs cast */
101   while (fbgets(line, sizeof(line) - 1, file)) {
102     char* end = line + strlen(line);
103     while (end > line) {
104       --end;
105       if ('\n' == *end || '\r' == *end)
106         *end = '\0';
107       else
108         break;
109     }
110     send_reply(sptr, RPL_MOTD, line);
111   }
112   send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
113              ":Connection from your host is refused on this server.");
114   fbclose(file);
115 }
116
117 struct ConfItem* make_conf(void)
118 {
119   struct ConfItem* aconf;
120
121   aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
122   assert(0 != aconf);
123 #ifdef        DEBUGMODE
124   ++GlobalConfCount;
125 #endif
126   memset(aconf, 0, sizeof(struct ConfItem));
127   aconf->status       = CONF_ILLEGAL;
128   aconf->ipnum.s_addr = INADDR_NONE;
129   return aconf;
130 }
131
132 void delist_conf(struct ConfItem *aconf)
133 {
134   if (aconf == GlobalConfList)
135     GlobalConfList = GlobalConfList->next;
136   else {
137     struct ConfItem *bconf;
138
139     for (bconf = GlobalConfList; aconf != bconf->next; bconf = bconf->next)
140       ;
141     bconf->next = aconf->next;
142   }
143   aconf->next = 0;
144 }
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->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 /*
166  * detach_conf - Disassociate configuration from the client.
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 /*
197  * conf_dns_callback - called when resolver query finishes
198  * if the query resulted in a successful search, hp will contain
199  * a non-null pointer, otherwise hp will be null.
200  * if successful save hp in the conf item it was called with
201  */
202 static void conf_dns_callback(void* vptr, struct DNSReply* reply)
203 {
204   struct ConfItem* aconf = (struct ConfItem*) vptr;
205   aconf->dns_pending = 0;
206   if (reply)
207     memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
208 }
209
210 /*
211  * conf_dns_lookup - do a nameserver lookup of the conf host
212  * if the conf entry is currently doing a ns lookup do nothing, otherwise
213  * if the lookup returns a null pointer, set the conf dns_pending flag
214  */
215 static struct DNSReply* conf_dns_lookup(struct ConfItem* aconf)
216 {
217   struct DNSReply* dns_reply = 0;
218   if (!aconf->dns_pending) {
219     char            buf[HOSTLEN + 1];
220     struct DNSQuery query;
221     query.vptr     = aconf;
222     query.callback = conf_dns_callback;
223     host_from_uh(buf, aconf->host, HOSTLEN);
224     buf[HOSTLEN] = '\0';
225
226     if (0 == (dns_reply = gethost_byname(buf, &query)))
227       aconf->dns_pending = 1;
228   }
229   return dns_reply;
230 }
231
232
233 /*
234  * lookup_confhost
235  *
236  * Do (start) DNS lookups of all hostnames in the conf line and convert
237  * an IP addresses in a.b.c.d number for to IP#s.
238  */
239 static void lookup_confhost(struct ConfItem *aconf)
240 {
241   struct DNSReply* reply;
242
243   if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
244     Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
245            aconf->host, aconf->name));
246     return;
247   }
248   /*
249    * Do name lookup now on hostnames given and store the
250    * ip numbers in conf structure.
251    */
252   if (IsDigit(*aconf->host)) {
253     /*
254      * rfc 1035 sez host names may not start with a digit
255      * XXX - this has changed code needs to be updated
256      */
257     aconf->ipnum.s_addr = inet_addr(aconf->host);
258     if (INADDR_NONE == aconf->ipnum.s_addr) {
259       Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
260             aconf->host, aconf->name));
261     }
262   }
263   else if ((reply = conf_dns_lookup(aconf)))
264     memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
265 }
266
267 /*
268  * conf_find_server - find a server by name or hostname
269  * returns a server conf item pointer if found, 0 otherwise
270  *
271  * NOTE: at some point when we don't have to scan the entire
272  * list it may be cheaper to look for server names and host
273  * names in separate loops (original code did it that way)
274  */
275 struct ConfItem* conf_find_server(const char* name)
276 {
277   struct ConfItem* conf;
278   assert(0 != name);
279
280   for (conf = GlobalConfList; conf; conf = conf->next) {
281     if (CONF_SERVER == conf->status) {
282       /*
283        * Check first servernames, then try hostnames.
284        * XXX - match returns 0 if there _is_ a match... guess they
285        * haven't decided what true is yet
286        */
287       if (0 == match(name, conf->name))
288         return conf;
289     }
290   }
291   return 0;
292 }
293
294 /*
295  * conf_eval_crule - evaluate connection rules
296  * returns the name of the rule triggered if found, 0 otherwise
297  *
298  * Evaluate connection rules...  If no rules found, allow the
299  * connect.   Otherwise stop with the first true rule (ie: rules
300  * are ored together.  Oper connects are effected only by D
301  * lines (CRULE_ALL) not d lines (CRULE_AUTO).
302  */
303 const char* conf_eval_crule(const char* name, int mask)
304 {
305   struct CRuleConf* p = cruleConfList;
306   assert(0 != name);
307
308   for ( ; p; p = p->next) {
309     if (0 != (p->type & mask) && 0 == match(p->hostmask, name)) {
310       if (crule_eval(p->node))
311         return p->rule;
312     }
313   }
314   return 0;
315 }
316
317 /*
318  * Remove all conf entries from the client except those which match
319  * the status field mask.
320  */
321 void det_confs_butmask(struct Client* cptr, int mask)
322 {
323   struct SLink* link;
324   struct SLink* next;
325   assert(0 != cptr);
326
327   for (link = cli_confs(cptr); link; link = next) {
328     next = link->next;
329     if ((link->value.aconf->status & mask) == 0)
330       detach_conf(cptr, link->value.aconf);
331   }
332 }
333
334 /*
335  * check_limit_and_attach - check client limits and attach I:line
336  *
337  * Made it accept 1 charactor, and 2 charactor limits (0->99 now), 
338  * and dislallow more than 255 people here as well as in ipcheck.
339  * removed the old "ONE" scheme too.
340  *  -- Isomer 2000-06-22
341  */
342 static enum AuthorizationCheckResult
343 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
344 {
345   int number = 255;
346   
347   if (aconf->passwd) {
348     if (IsDigit(*aconf->passwd) && !aconf->passwd[1])
349       number = *aconf->passwd-'0';
350     else if (IsDigit(*aconf->passwd) && IsDigit(aconf->passwd[1]) && 
351              !aconf->passwd[2])
352       number = (*aconf->passwd-'0')*10+(aconf->passwd[1]-'0');
353   }
354   if (IPcheck_nr(cptr) > number)
355     return ACR_TOO_MANY_FROM_IP;
356   return attach_conf(cptr, aconf);
357 }
358
359 /*
360  * Find the first (best) I line to attach.
361  */
362 enum AuthorizationCheckResult attach_iline(struct Client*  cptr)
363 {
364   struct ConfItem* aconf;
365   const char*      hname;
366   int              i;
367   static char      uhost[HOSTLEN + USERLEN + 3];
368   static char      fullname[HOSTLEN + 1];
369   struct hostent*  hp = 0;
370
371   assert(0 != cptr);
372
373   if (cli_dns_reply(cptr))
374     hp = cli_dns_reply(cptr)->hp;
375
376   for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
377     if (aconf->status != CONF_CLIENT)
378       continue;
379     if (aconf->port && aconf->port != cli_listener(cptr)->port)
380       continue;
381     if (!aconf->host || !aconf->name)
382       continue;
383     if (hp) {
384       for (i = 0, hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
385         ircd_strncpy(fullname, hname, HOSTLEN);
386         fullname[HOSTLEN] = '\0';
387
388         Debug((DEBUG_DNS, "a_il: %s->%s", cli_sockhost(cptr), fullname));
389
390         if (strchr(aconf->name, '@')) {
391           strcpy(uhost, cli_username(cptr));
392           strcat(uhost, "@");
393         }
394         else
395           *uhost = '\0';
396         strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
397         uhost[sizeof(uhost) - 1] = 0;
398         if (0 == match(aconf->name, uhost)) {
399           if (strchr(uhost, '@'))
400             cli_flags(cptr) |= FLAGS_DOID;
401           return check_limit_and_attach(cptr, aconf);
402         }
403       }
404     }
405     if (strchr(aconf->host, '@')) {
406       ircd_strncpy(uhost, cli_username(cptr), sizeof(uhost) - 2);
407       uhost[sizeof(uhost) - 2] = 0;
408       strcat(uhost, "@");
409     }
410     else
411       *uhost = '\0';
412     strncat(uhost, cli_sock_ip(cptr), sizeof(uhost) - 1 - strlen(uhost));
413     uhost[sizeof(uhost) - 1] = 0;
414     if (match(aconf->host, uhost))
415       continue;
416     if (strchr(uhost, '@'))
417       cli_flags(cptr) |= FLAGS_DOID;
418
419     return check_limit_and_attach(cptr, aconf);
420   }
421   return ACR_NO_AUTHORIZATION;
422 }
423
424 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
425 {
426   struct SLink *lp;
427
428   for (lp = cli_confs(cptr); lp; lp = lp->next) {
429     if (lp->value.aconf == aconf)
430       return 1;
431   }
432   return 0;
433 }
434
435 /*
436  * attach_conf
437  *
438  * Associate a specific configuration entry to a *local*
439  * client (this is the one which used in accepting the
440  * connection). Note, that this automaticly changes the
441  * attachment if there was an old one...
442  */
443 enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
444 {
445   struct SLink *lp;
446
447   if (is_attached(aconf, cptr))
448     return ACR_ALREADY_AUTHORIZED;
449   if (IsIllegal(aconf))
450     return ACR_NO_AUTHORIZATION;
451   if ((aconf->status & (CONF_LOCOP | CONF_OPERATOR | CONF_CLIENT)) &&
452       ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
453     return ACR_TOO_MANY_IN_CLASS;  /* Use this for printing error message */
454   lp = make_link();
455   lp->next = cli_confs(cptr);
456   lp->value.aconf = aconf;
457   cli_confs(cptr) = lp;
458   ++aconf->clients;
459   if (aconf->status & CONF_CLIENT_MASK)
460     ConfLinks(aconf)++;
461   return ACR_OK;
462 }
463
464 const struct LocalConf* conf_get_local(void)
465 {
466   return &localConf;
467 }
468
469 /*
470  * attach_confs_byname
471  *
472  * Attach a CONF line to a client if the name passed matches that for
473  * the conf file (for non-C/N lines) or is an exact match (C/N lines
474  * only).  The difference in behaviour is to stop C:*::* and N:*::*.
475  */
476 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
477                                      int statmask)
478 {
479   struct ConfItem* tmp;
480   struct ConfItem* first = NULL;
481
482   assert(0 != name);
483
484   if (HOSTLEN < strlen(name))
485     return 0;
486
487   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
488     if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
489       assert(0 != tmp->name);
490       if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) { 
491         if (ACR_OK == attach_conf(cptr, tmp) && !first)
492           first = tmp;
493       }
494     }
495   }
496   return first;
497 }
498
499 /*
500  * Added for new access check    meLazy
501  */
502 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
503                                      int statmask)
504 {
505   struct ConfItem* tmp;
506   struct ConfItem* first = 0;
507
508   assert(0 != host);
509   if (HOSTLEN < strlen(host))
510     return 0;
511
512   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
513     if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
514       assert(0 != tmp->host);
515       if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) { 
516         if (ACR_OK == attach_conf(cptr, tmp) && !first)
517           first = tmp;
518       }
519     }
520   }
521   return first;
522 }
523
524 /*
525  * find a conf entry which matches the hostname and has the same name.
526  */
527 struct ConfItem* find_conf_exact(const char* name, const char* user,
528                                  const char* host, int statmask)
529 {
530   struct ConfItem *tmp;
531   char userhost[USERLEN + HOSTLEN + 3];
532
533   if (user)
534     ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
535   else
536     ircd_strncpy(userhost, host, sizeof(userhost) - 1);
537
538   for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
539     if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
540         0 != ircd_strcmp(tmp->name, name))
541       continue;
542     /*
543      * Accept if the *real* hostname (usually sockecthost)
544      * socket host) matches *either* host or name field
545      * of the configuration.
546      */
547     if (match(tmp->host, userhost))
548       continue;
549     if (tmp->status & (CONF_OPERATOR | CONF_LOCOP)) {
550       if (tmp->clients < MaxLinks(tmp->conn_class))
551         return tmp;
552       else
553         continue;
554     }
555     else
556       return tmp;
557   }
558   return 0;
559 }
560
561 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
562                                   int statmask)
563 {
564   struct ConfItem* tmp;
565   assert(0 != name);
566
567   if (HOSTLEN < strlen(name))
568     return 0;
569
570   for (; lp; lp = lp->next) {
571     tmp = lp->value.aconf;
572     if (0 != (tmp->status & statmask)) {
573       assert(0 != tmp->name);
574       if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
575         return tmp;
576     }
577   }
578   return 0;
579 }
580
581 /*
582  * Added for new access check    meLazy
583  */
584 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
585                                   int statmask)
586 {
587   struct ConfItem* tmp = NULL;
588   assert(0 != host);
589
590   if (HOSTLEN < strlen(host))
591     return 0;
592
593   for (; lp; lp = lp->next) {
594     tmp = lp->value.aconf;
595     if (0 != (tmp->status & statmask)) {
596       assert(0 != tmp->host);
597       if (0 == match(tmp->host, host))
598         return tmp;
599     }
600   }
601   return 0;
602 }
603
604 /*
605  * find_conf_ip
606  *
607  * Find a conf line using the IP# stored in it to search upon.
608  * Added 1/8/92 by Avalon.
609  */
610 struct ConfItem* find_conf_byip(struct SLink* lp, const char* ip, 
611                                 int statmask)
612 {
613   struct ConfItem* tmp;
614
615   for (; lp; lp = lp->next) {
616     tmp = lp->value.aconf;
617     if (0 != (tmp->status & statmask)) {
618       if (0 == memcmp(&tmp->ipnum, ip, sizeof(struct in_addr)))
619         return tmp;
620     }
621   }
622   return 0;
623 }
624
625 /*
626  * find_conf_entry
627  *
628  * - looks for a match on all given fields.
629  */
630 static struct ConfItem *find_conf_entry(struct ConfItem *aconf,
631                                         unsigned int mask)
632 {
633   struct ConfItem *bconf;
634   assert(0 != aconf);
635
636   mask &= ~CONF_ILLEGAL;
637
638   for (bconf = GlobalConfList; bconf; bconf = bconf->next) {
639     if (!(bconf->status & mask) || (bconf->port != aconf->port))
640       continue;
641
642     if ((EmptyString(bconf->host) && !EmptyString(aconf->host)) ||
643         (EmptyString(aconf->host) && !EmptyString(bconf->host)))
644       continue;
645     if (!EmptyString(bconf->host) && 0 != ircd_strcmp(bconf->host, aconf->host))
646       continue;
647
648     if ((EmptyString(bconf->passwd) && !EmptyString(aconf->passwd)) ||
649         (EmptyString(aconf->passwd) && !EmptyString(bconf->passwd)))
650       continue;
651     if (!EmptyString(bconf->passwd) && (!IsDigit(*bconf->passwd) || bconf->passwd[1])
652         && 0 != ircd_strcmp(bconf->passwd, aconf->passwd))
653       continue;
654
655     if ((EmptyString(bconf->name) && !EmptyString(aconf->name)) ||
656         (EmptyString(aconf->name) && !EmptyString(bconf->name)))
657       continue;
658     if (!EmptyString(bconf->name) && 0 != ircd_strcmp(bconf->name, aconf->name))
659       continue;
660     break;
661   }
662   return bconf;
663 }
664
665
666 /*
667  * If conf line is a class definition, create a class entry
668  * for it and make the conf_line illegal and delete it.
669  */
670 void conf_add_class(const char* const* fields, int count)
671 {
672   if (count < 6)
673     return;
674   add_class(atoi(fields[1]), atoi(fields[2]), atoi(fields[3]),
675             atoi(fields[4]), atoi(fields[5]));
676 }
677
678 void conf_add_listener(const char* const* fields, int count)
679 {
680   int is_server = 0;
681   int is_hidden = 0;
682
683   /*
684    * need a port
685    */
686   if (count < 5 || EmptyString(fields[4]))
687     return;
688
689   if (!EmptyString(fields[3])) {
690     const char* x = fields[3];
691     if ('S' == ToUpper(*x))
692       is_server = 1;
693     ++x;
694     if ('H' == ToUpper(*x))
695       is_hidden = 1;
696   }
697   /*           port             vhost      mask  */
698   add_listener(atoi(fields[4]), fields[2], fields[1], is_server, is_hidden);
699 }
700
701 void conf_add_local(const char* const* fields, int count)
702 {
703   if (count < 6 || EmptyString(fields[1]) || EmptyString(fields[5])) {
704     log_write(LS_CONFIG, L_CRIT, 0, "Your M: line must have 6 fields!");
705     return;
706   }
707   /*
708    * these two can only be set the first time
709    */
710   if (0 == localConf.name) {
711     if (string_is_hostname(fields[1]))
712       DupString(localConf.name, fields[1]);
713   }
714   if (0 == localConf.numeric) {
715     localConf.numeric = atoi(fields[5]);
716     if (0 == localConf.numeric)
717       log_write(LS_CONFIG, L_WARNING, 0,
718                 "Your M: line must have a Numeric value greater than 0");
719   }
720   /*
721    * these two can be changed while the server is running
722    */
723   if (string_is_address(fields[2])) {
724     if (INADDR_NONE == (localConf.vhost_address.s_addr = inet_addr(fields[2])))
725       localConf.vhost_address.s_addr = INADDR_ANY;
726   }
727   MyFree(localConf.description);
728   DupString(localConf.description, fields[3]);
729   /*
730    * XXX - shouldn't be setting these directly here
731    */
732   ircd_strncpy(cli_info(&me), fields[3], REALLEN);
733   set_virtual_host(localConf.vhost_address);
734 }
735
736 void conf_add_admin(const char* const* fields, int count)
737 {
738   /*
739    * if you have one, it MUST have 3 lines
740    */
741   if (count < 4) {
742     log_write(LS_CONFIG, L_CRIT, 0, "Your A: line must have 4 fields!");
743     return;
744   }
745   MyFree(localConf.location1);
746   DupString(localConf.location1, fields[1]);
747
748   MyFree(localConf.location2);
749   DupString(localConf.location2, fields[2]);
750
751   MyFree(localConf.contact);
752   DupString(localConf.contact, fields[3]);
753 }
754
755 /*
756  * conf_add_crule - Create expression tree from connect rule and add it
757  * to the crule list
758  */
759 void conf_add_crule(const char* const* fields, int count, int type)
760 {
761   struct CRuleNode* node;
762   assert(0 != fields);
763   
764   if (count < 4 || EmptyString(fields[1]) || EmptyString(fields[3]))
765     return;
766   
767   if ((node = crule_parse(fields[3]))) {
768     struct CRuleConf* p = (struct CRuleConf*) MyMalloc(sizeof(struct CRuleConf));
769     assert(0 != p);
770
771     DupString(p->hostmask, fields[1]);
772     collapse(p->hostmask);
773
774     DupString(p->rule, fields[3]);
775
776     p->type = type;
777     p->node = node;
778     p->next = cruleConfList;
779     cruleConfList = p;
780   } 
781 }
782
783 void conf_erase_crule_list(void)
784 {
785   struct CRuleConf* next;
786   struct CRuleConf* p = cruleConfList;
787
788   for ( ; p; p = next) {
789     next = p->next;
790     crule_free(&p->node);
791     MyFree(p->hostmask);
792     MyFree(p->rule);
793     MyFree(p);
794   }
795   cruleConfList = 0;
796 }
797
798 const struct CRuleConf* conf_get_crule_list(void)
799 {
800   return cruleConfList;
801 }
802
803 void conf_add_server(const char* const* fields, int count)
804 {
805   struct ServerConf* server;
806   struct in_addr    addr;
807   assert(0 != fields);
808   /*
809    * missing host, password, or alias?
810    */
811   if (count < 6 || EmptyString(fields[1]) || EmptyString(fields[2]) || EmptyString(fields[3]))
812     return;
813   /*
814    * check the host
815    */
816   if (string_is_hostname(fields[1]))
817     addr.s_addr = INADDR_NONE;
818   else if (INADDR_NONE == (addr.s_addr = inet_addr(fields[1])))
819     return;
820
821   server = (struct ServerConf*) MyMalloc(sizeof(struct ServerConf));
822   assert(0 != server);
823   DupString(server->hostname, fields[1]);
824   DupString(server->passwd,   fields[2]);
825   DupString(server->alias,    fields[3]);
826   server->address.s_addr = addr.s_addr;
827   server->port           = atoi(fields[4]);
828   server->dns_pending    = 0;
829   server->connected      = 0;
830   server->hold           = 0;
831   server->conn_class      = find_class(atoi(fields[5]));
832
833   server->next = serverConfList;
834   serverConfList = server;
835
836   /* if (INADDR_NONE == server->address.s_addr) */
837     /* lookup_confhost(server); */
838 }
839
840 void conf_add_deny(const char* const* fields, int count, int ip_kill)
841 {
842   struct DenyConf* conf;
843
844   if (count < 4 || EmptyString(fields[1]) || EmptyString(fields[3]))
845     return;
846   
847   conf = (struct DenyConf*) MyMalloc(sizeof(struct DenyConf));
848   assert(0 != conf);
849   memset(conf, 0, sizeof(struct DenyConf));
850
851   if (fields[1][0] == '$' && fields[1][1] == 'R')
852     conf->flags |= DENY_FLAGS_REALNAME;
853
854   DupString(conf->hostmask, fields[1]);
855   collapse(conf->hostmask);
856
857   if (!EmptyString(fields[2])) {
858     const char* p = fields[2];
859     if ('!' == *p) {
860       conf->flags |= DENY_FLAGS_FILE;
861       ++p;
862     }
863     DupString(conf->message, p);
864   }
865   DupString(conf->usermask, fields[3]);
866   collapse(conf->usermask);
867
868   if (ip_kill) {
869     /* 
870      * Here we use the same kludge as in listener.c to parse
871      * a.b.c.d, or a.b.c.*, or a.b.c.d/e.
872      */
873     int  c_class;
874     char ipname[16];
875     int  ad[4] = { 0 };
876     int  bits2 = 0;
877     c_class = sscanf(conf->hostmask, "%d.%d.%d.%d/%d",
878                      &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
879     if (c_class != 5) {
880       conf->bits = c_class * 8;
881     }
882     else {
883       conf->bits = bits2;
884     }
885     sprintf_irc(ipname, "%d.%d.%d.%d", ad[0], ad[1], ad[2], ad[3]);
886     
887     /*
888      * This ensures endian correctness
889      */
890     conf->address = inet_addr(ipname);
891     Debug((DEBUG_DEBUG, "IPkill: %s = %08x/%i (%08x)", ipname,
892            conf->address, conf->bits, NETMASK(conf->bits)));
893     conf->flags |= DENY_FLAGS_IP;
894   }
895   conf->next = denyConfList;
896   denyConfList = conf;
897 }
898
899 void conf_erase_deny_list(void)
900 {
901   struct DenyConf* next;
902   struct DenyConf* p = denyConfList;
903   for ( ; p; p = next) {
904     next = p->next;
905     MyFree(p->hostmask);
906     MyFree(p->usermask);
907     MyFree(p->message);
908     MyFree(p);
909   }
910   denyConfList = 0;
911 }
912  
913 const struct DenyConf* conf_get_deny_list(void)
914 {
915   return denyConfList;
916 }
917
918 /*
919  * read_configuration_file
920  *
921  * Read configuration file.
922  *
923  * returns 0, if file cannot be opened
924  *         1, if file read
925  */
926
927 #define MAXCONFLINKS 150
928
929 int read_configuration_file(void)
930 {
931   enum { MAX_FIELDS = 15 };
932
933   char* src;
934   char* dest;
935   int quoted;
936   FBFILE *file;
937   char line[512];
938   int ccount = 0;
939   struct ConfItem *aconf = 0;
940   
941   int   field_count = 0;
942   const char* field_vector[MAX_FIELDS + 1];
943
944   Debug((DEBUG_DEBUG, "read_configuration_file: ircd.conf = %s", configfile));
945   if (0 == (file = fbopen(configfile, "r"))) {
946     return 0;
947   }
948
949   feature_unmark(); /* unmark all features for resetting later */
950
951   while (fbgets(line, sizeof(line) - 1, file)) {
952     if ('#' == *line || IsSpace(*line))
953       continue;
954
955     if ((src = strchr(line, '\n')))
956       *src = '\0';
957     
958     if (':' != line[1]) {
959       Debug((DEBUG_ERROR, "Bad config line: %s", line));
960       sendto_opmask_butone(0, SNO_OLDSNO,"Bad Config line");
961       continue;
962     }
963
964     /*
965      * do escapes, quoting, comments, and field breakup in place
966      * in one pass with a poor mans state machine
967      */
968     field_vector[0] = line;
969     field_count = 1;
970     quoted = 0;
971
972     for (src = line, dest = line; *src; ) {
973       switch (*src) {
974       case '\\':
975         ++src;
976         switch (*src) {
977         case 'b':
978           *dest++ = '\b';
979           ++src;
980           break;
981         case 'f':
982           *dest++ = '\f';
983           ++src;
984           break;
985         case 'n':
986           *dest++ = '\n';
987           ++src;
988           break;
989         case 'r':
990           *dest++ = '\r';      
991           ++src;
992           break;
993         case 't':
994           *dest++ = '\t';
995           ++src;
996           break;
997         case 'v':
998           *dest++ = '\v';
999           ++src;
1000           break;
1001         case '\\':
1002           *dest++ = '\\';
1003           ++src;
1004           break;
1005         case '\0':
1006           break;
1007         default:
1008           *dest++ = *src++;
1009           break;
1010         }
1011         break;
1012       case '"':
1013         if (quoted)
1014           quoted = 0;
1015         else
1016           quoted = 1;
1017         /*
1018          * strip quotes
1019          */
1020         ++src;
1021         break;
1022       case ':':
1023         if (quoted)
1024           *dest++ = *src++;
1025         else {
1026           *dest++ = '\0';
1027           field_vector[field_count++] = dest;
1028           if (field_count > MAX_FIELDS)
1029             *src = '\0';
1030           else  
1031             ++src;
1032         }
1033         break;
1034       case '#':
1035         *src = '\0';
1036         break;
1037       default:
1038         *dest++ = *src++;
1039         break;
1040       }
1041     }
1042     *dest = '\0';
1043
1044     if (field_count < 2 || EmptyString(field_vector[0]))
1045       continue;
1046
1047     if (aconf)
1048       free_conf(aconf);
1049
1050     aconf = make_conf();
1051
1052     switch (*field_vector[0]) {
1053     case 'A':                /* Name, e-mail address of administrator */
1054     case 'a':                /* of this server. CONF_ADMIN */
1055       conf_add_admin(field_vector, field_count);
1056       aconf->status = CONF_ILLEGAL;
1057       break;
1058     case 'C':                /* Server where I should try to connect */
1059     case 'c':                /* in case of lp failures             */
1060       ++ccount;
1061       aconf->status = CONF_SERVER;
1062       break;
1063       /* Connect rule */
1064     case 'D':  /* CONF_CRULEALL */
1065       conf_add_crule(field_vector, field_count, CRULE_ALL);
1066       aconf->status = CONF_ILLEGAL;
1067       break;
1068       /* Connect rule - autos only */
1069     case 'd':  /* CONF_CRULEAUTO */
1070       conf_add_crule(field_vector, field_count, CRULE_AUTO);
1071       aconf->status = CONF_ILLEGAL;
1072       break;
1073     case 'F':                /* Feature line */
1074     case 'f':
1075       feature_set(0, &field_vector[1], field_count - 1);
1076       aconf->status = CONF_ILLEGAL;
1077       break;
1078     case 'H':                /* Hub server line */
1079     case 'h':
1080       aconf->status = CONF_HUB;
1081       break;
1082     case 'I':                /* Just plain normal irc client trying  */
1083     case 'i':                /* to connect me */
1084       aconf->status = CONF_CLIENT;
1085       break;
1086     case 'K':                /* Kill user line on irc.conf           */
1087       conf_add_deny(field_vector, field_count, 0);
1088       aconf->status = CONF_ILLEGAL;
1089       break;
1090     case 'k':                /* Kill user line based on IP in ircd.conf */
1091       conf_add_deny(field_vector, field_count, 1);
1092       aconf->status = CONF_ILLEGAL;
1093       break;
1094       /* Operator. Line should contain at least */
1095       /* password and host where connection is  */
1096     case 'L':                /* guaranteed leaf server */
1097     case 'l':
1098       aconf->status = CONF_LEAF;
1099       break;
1100       /* Me. Host field is name used for this host */
1101       /* and port number is the number of the port */
1102     case 'M':
1103     case 'm':        /* CONF_ME */
1104       conf_add_local(field_vector, field_count);
1105       aconf->status = CONF_ILLEGAL;
1106       break;
1107     case 'O':
1108       aconf->status = CONF_OPERATOR;
1109       break;
1110       /* Local Operator, (limited privs --SRB) */
1111     case 'o':
1112       aconf->status = CONF_LOCOP;
1113       break;
1114     case 'P':                /* listen port line */
1115     case 'p':        /* CONF_LISTEN_PORT */
1116       conf_add_listener(field_vector, field_count);
1117       aconf->status = CONF_ILLEGAL;
1118       break;
1119     case 'T':                /* print out different motd's */
1120     case 't':                /* based on hostmask - CONF_TLINES */
1121       motd_add(field_vector[1], field_vector[2]);
1122       aconf->status = CONF_ILLEGAL;
1123       break;
1124     case 'U':      /* Underworld server, allowed to hack modes */
1125     case 'u':      /* *Every* server on the net must define the same !!! */
1126       aconf->status = CONF_UWORLD;
1127       break;
1128     case 'Y':
1129     case 'y':      /* CONF_CLASS */
1130       conf_add_class(field_vector, field_count);
1131       aconf->status = CONF_ILLEGAL;
1132       break;
1133     default:
1134       Debug((DEBUG_ERROR, "Error in config file: %s", line));
1135       sendto_opmask_butone(0, SNO_OLDSNO, "Unknown prefix in config file: %c",
1136                            *field_vector[0]);
1137       aconf->status = CONF_ILLEGAL;
1138       break;
1139     }
1140     if (IsIllegal(aconf))
1141       continue;
1142
1143     if (!EmptyString(field_vector[1]))
1144       DupString(aconf->host, field_vector[1]);
1145
1146     if (!EmptyString(field_vector[2]))
1147       DupString(aconf->passwd, field_vector[2]);
1148
1149     if (field_count > 3 && !EmptyString(field_vector[3]))
1150         DupString(aconf->name, field_vector[3]);
1151
1152     if (field_count > 4 && !EmptyString(field_vector[4]))
1153         aconf->port = atoi(field_vector[4]); 
1154
1155     if (field_count > 5 && !EmptyString(field_vector[5]))
1156       aconf->conn_class = find_class(atoi(field_vector[5]));
1157
1158     /*
1159      * Associate each conf line with a class by using a pointer
1160      * to the correct class record. -avalon
1161      */
1162     if (aconf->status & CONF_CLIENT_MASK) {
1163       if (aconf->conn_class == 0)
1164         aconf->conn_class = find_class(0);
1165     }
1166     if (aconf->status & CONF_CLIENT) {
1167       struct ConfItem *bconf;
1168
1169       if ((bconf = find_conf_entry(aconf, aconf->status))) {
1170         delist_conf(bconf);
1171         bconf->status &= ~CONF_ILLEGAL;
1172         if (aconf->status == CONF_CLIENT) {
1173           /*
1174            * copy the password field in case it changed
1175            */
1176           MyFree(bconf->passwd);
1177           bconf->passwd = aconf->passwd;
1178           aconf->passwd = 0;
1179
1180           ConfLinks(bconf) -= bconf->clients;
1181           bconf->conn_class = aconf->conn_class;
1182           if (bconf->conn_class)
1183             ConfLinks(bconf) += bconf->clients;
1184         }
1185         free_conf(aconf);
1186         aconf = bconf;
1187       }
1188     }
1189     if (aconf->status & CONF_SERVER) {
1190       if (ccount > MAXCONFLINKS || !aconf->host || strchr(aconf->host, '*') ||
1191           strchr(aconf->host, '?') || !aconf->name)
1192         continue;
1193     }
1194     if (aconf->status & (CONF_LOCOP | CONF_OPERATOR)) {
1195       if (!strchr(aconf->host, '@')) {
1196         char* newhost;
1197         int len = 3;                /* *@\0 = 3 */
1198
1199         len += strlen(aconf->host);
1200         newhost = (char*) MyMalloc(len);
1201         assert(0 != newhost);
1202         ircd_snprintf(0, newhost, len, "*@%s", aconf->host);
1203         MyFree(aconf->host);
1204         aconf->host = newhost;
1205       }
1206     }
1207     if (aconf->status & CONF_SERVER) {
1208       if (EmptyString(aconf->passwd))
1209         continue;
1210       lookup_confhost(aconf);
1211     }
1212     /*
1213      * Juped nicks are listed in the 'password' field of U:lines,
1214      * the list is comma separated and might be empty and/or contain
1215      * empty elements... the only limit is that it MUST be shorter
1216      * than 512 chars, or they will be cutted out :)
1217      */
1218     if ((aconf->status == CONF_UWORLD) && (aconf->passwd) && (*aconf->passwd))
1219       addNickJupes(aconf->passwd);
1220
1221     collapse(aconf->host);
1222     collapse(aconf->name);
1223     Debug((DEBUG_NOTICE,
1224         "Read Init: (%d) (%s) (%s) (%s) (%u) (%p)",
1225         aconf->status, aconf->host, aconf->passwd,
1226         aconf->name, aconf->port, aconf->conn_class));
1227     aconf->next = GlobalConfList;
1228     GlobalConfList = aconf;
1229     aconf = NULL;
1230   }
1231   if (aconf)
1232     free_conf(aconf);
1233   fbclose(file);
1234 /*    nextping = nextconnect = CurrentTime; */
1235   feature_mark(); /* reset unmarked features */
1236   return 1;
1237 }
1238
1239 /*
1240  * rehash
1241  *
1242  * Actual REHASH service routine. Called with sig == 0 if it has been called
1243  * as a result of an operator issuing this command, else assume it has been
1244  * called as a result of the server receiving a HUP signal.
1245  */
1246 int rehash(struct Client *cptr, int sig)
1247 {
1248   struct ConfItem** tmp = &GlobalConfList;
1249   struct ConfItem*  tmp2;
1250   struct Client*    acptr;
1251   int               i;
1252   int               ret = 0;
1253   int               found_g = 0;
1254
1255   if (1 == sig)
1256     sendto_opmask_butone(0, SNO_OLDSNO,
1257                          "Got signal SIGHUP, reloading ircd conf. file");
1258
1259   while ((tmp2 = *tmp)) {
1260     if (tmp2->clients) {
1261       /*
1262        * Configuration entry is still in use by some
1263        * local clients, cannot delete it--mark it so
1264        * that it will be deleted when the last client
1265        * exits...
1266        */
1267       if (CONF_CLIENT == (tmp2->status & CONF_CLIENT))
1268         tmp = &tmp2->next;
1269       else {
1270         *tmp = tmp2->next;
1271         tmp2->next = 0;
1272       }
1273       tmp2->status |= CONF_ILLEGAL;
1274     }
1275     else {
1276       *tmp = tmp2->next;
1277       free_conf(tmp2);
1278     }
1279   }
1280   conf_erase_crule_list();
1281   conf_erase_deny_list();
1282   motd_clear();
1283
1284   /*
1285    * delete the juped nicks list
1286    */
1287   clearNickJupes();
1288
1289   if (sig != 2)
1290     flush_resolver_cache();
1291
1292   class_mark_delete();
1293   mark_listeners_closing();
1294
1295   read_configuration_file();
1296
1297   log_reopen(); /* reopen log files */
1298
1299   close_listeners();
1300   class_delete_marked();         /* unless it fails */
1301
1302   /*
1303    * Flush out deleted I and P lines although still in use.
1304    */
1305   for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
1306     if (CONF_ILLEGAL == (tmp2->status & CONF_ILLEGAL)) {
1307       *tmp = tmp2->next;
1308       tmp2->next = NULL;
1309       if (!tmp2->clients)
1310         free_conf(tmp2);
1311     }
1312     else
1313       tmp = &tmp2->next;
1314   }
1315   for (i = 0; i <= HighestFd; i++) {
1316     if ((acptr = LocalClientArray[i])) {
1317       assert(!IsMe(acptr));
1318       if (IsServer(acptr)) {
1319         det_confs_butmask(acptr,
1320             ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
1321         attach_confs_byname(acptr, cli_name(acptr),
1322                             CONF_HUB | CONF_LEAF | CONF_UWORLD);
1323       }
1324       /* Because admin's are getting so uppity about people managing to
1325        * get past K/G's etc, we'll "fix" the bug by actually explaining
1326        * whats going on.
1327        */
1328       if ((found_g = find_kill(acptr))) {
1329         sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
1330                              found_g == -2 ? "G-line active for %s%s" :
1331                              "K-line active for %s%s",
1332                              IsUnknown(acptr) ? "Unregistered Client ":"",                     
1333                              get_client_name(acptr, HIDE_IP));
1334         if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
1335             "K-lined") == CPTR_KILLED)
1336           ret = CPTR_KILLED;
1337       }
1338     }
1339   }
1340   return ret;
1341 }
1342
1343 /*
1344  * init_conf
1345  *
1346  * Read configuration file.
1347  *
1348  * returns 0, if file cannot be opened
1349  *         1, if file read
1350  */
1351
1352 int init_conf(void)
1353 {
1354   if (read_configuration_file()) {
1355     /*
1356      * make sure we're sane to start if the config
1357      * file read didn't get everything we need.
1358      * XXX - should any of these abort the server?
1359      * TODO: add warning messages
1360      */
1361     if (0 == localConf.name || 0 == localConf.numeric)
1362       return 0;
1363
1364     if (0 == localConf.location1)
1365       DupString(localConf.location1, "");
1366     if (0 == localConf.location2)
1367       DupString(localConf.location2, "");
1368     if (0 == localConf.contact)
1369       DupString(localConf.contact, "");
1370     
1371     return 1;
1372   }
1373   return 0;
1374 }
1375
1376 /*
1377  * find_kill
1378  * input:
1379  *  client pointer
1380  * returns:
1381  *  0: Client may continue to try and connect
1382  * -1: Client was K/k:'d - sideeffect: reason was sent.
1383  * -2: Client was G/g:'d - sideeffect: reason was sent.
1384  * sideeffects:
1385  *  Client may have been sent a reason why they are denied, as above.
1386  */
1387 int find_kill(struct Client *cptr)
1388 {
1389   const char*      host;
1390   const char*      name;
1391   const char*      realname;
1392   struct DenyConf* deny;
1393   struct Gline*    agline = NULL;
1394
1395   assert(0 != cptr);
1396
1397   if (!cli_user(cptr))
1398     return 0;
1399
1400   host = cli_sockhost(cptr);
1401   name = cli_user(cptr)->username;
1402   realname = cli_info(cptr);
1403
1404   assert(strlen(host) <= HOSTLEN);
1405   assert((name ? strlen(name) : 0) <= HOSTLEN);
1406   assert((realname ? strlen(realname) : 0) <= REALLEN);
1407
1408   /* 2000-07-14: Rewrote this loop for massive speed increases.
1409    *             -- Isomer
1410    */
1411   for (deny = denyConfList; deny; deny = deny->next) {
1412     if (0 != match(deny->usermask, name))
1413       continue;
1414
1415     if (EmptyString(deny->hostmask))
1416       break;
1417
1418     if (deny->flags & DENY_FLAGS_REALNAME) { /* K: by real name */
1419       if (0 == match(deny->hostmask + 2, realname))
1420         break;
1421     } else if (deny->flags & DENY_FLAGS_IP) { /* k: by IP */
1422       Debug((DEBUG_DEBUG, "ip: %08x network: %08x/%i mask: %08x",
1423              cli_ip(cptr).s_addr, deny->address, deny->bits, NETMASK(deny->bits)));
1424       if ((cli_ip(cptr).s_addr & NETMASK(deny->bits)) == deny->address)
1425         break;
1426     }
1427     else if (0 == match(deny->hostmask, host))
1428       break;
1429   }
1430   if (deny) {
1431     if (EmptyString(deny->message))
1432       send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1433                  ":Connection from your host is refused on this server.");
1434     else {
1435       if (deny->flags & DENY_FLAGS_FILE)
1436         killcomment(cptr, deny->message);
1437       else
1438         send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", deny->message);
1439     }
1440   }
1441   else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline)) {
1442     /*
1443      * find active glines
1444      * added a check against the user's IP address to find_gline() -Kev
1445      */
1446     send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", GlineReason(agline));
1447   }
1448   else
1449     agline = 0;          /* if a gline was found, it was inactive */
1450
1451   if (deny)
1452     return -1;
1453   if (agline)
1454     return -2;
1455     
1456   return 0;
1457 }
1458
1459 /*
1460  * Ordinary client access check. Look for conf lines which have the same
1461  * status as the flags passed.
1462  */
1463 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1464 {
1465   enum AuthorizationCheckResult acr = ACR_OK;
1466
1467   ClearAccess(cptr);
1468
1469   if ((acr = attach_iline(cptr))) {
1470     Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]", 
1471           cli_name(cptr), cli_sockhost(cptr)));
1472     return acr;
1473   }
1474   return ACR_OK;
1475 }
1476
1477 /*
1478  * check_server()
1479  *
1480  * Check access for a server given its name (passed in cptr struct).
1481  * Must check for all C/N lines which have a name which matches the
1482  * name given and a host which matches. A host alias which is the
1483  * same as the server name is also acceptable in the host field of a
1484  * C/N line.
1485  *
1486  * Returns
1487  *  0 = Success
1488  * -1 = Access denied
1489  * -2 = Bad socket.
1490  */
1491 int conf_check_server(struct Client *cptr)
1492 {
1493   struct ConfItem* c_conf = NULL;
1494   struct SLink*    lp;
1495
1496   Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]", 
1497         cli_name(cptr), cli_sockhost(cptr)));
1498
1499   if (IsUnknown(cptr) && !attach_confs_byname(cptr, cli_name(cptr), CONF_SERVER)) {
1500     Debug((DEBUG_DNS, "No C/N lines for %s", cli_sockhost(cptr)));
1501     return -1;
1502   }
1503   lp = cli_confs(cptr);
1504   /*
1505    * We initiated this connection so the client should have a C and N
1506    * line already attached after passing through the connect_server()
1507    * function earlier.
1508    */
1509   if (IsConnecting(cptr) || IsHandshake(cptr)) {
1510     c_conf = find_conf_byname(lp, cli_name(cptr), CONF_SERVER);
1511     if (!c_conf) {
1512       sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1513                            cli_name(cptr));
1514       det_confs_butmask(cptr, 0);
1515       return -1;
1516     }
1517   }
1518
1519   ClearAccess(cptr);
1520
1521   if (!c_conf) {
1522     if (cli_dns_reply(cptr)) {
1523       int             i;
1524       struct hostent* hp = cli_dns_reply(cptr)->hp;
1525       const char*     name = hp->h_name;
1526       /*
1527        * If we are missing a C or N line from above, search for
1528        * it under all known hostnames we have for this ip#.
1529        */
1530       for (i = 0; name; name = hp->h_aliases[i++]) {
1531         if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1532           ircd_strncpy(cli_sockhost(cptr), name, HOSTLEN);
1533           break;
1534         }
1535       }
1536       if (!c_conf) {
1537         for (i = 0; hp->h_addr_list[i]; i++) {
1538           if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1539             break;
1540         }
1541       }
1542     }
1543     else {
1544       /*
1545        * Check for C lines with the hostname portion the ip number
1546        * of the host the server runs on. This also checks the case where
1547        * there is a server connecting from 'localhost'.
1548        */
1549       c_conf = find_conf_byhost(lp, cli_sockhost(cptr), CONF_SERVER);
1550     }
1551   }
1552   /*
1553    * Attach by IP# only if all other checks have failed.
1554    * It is quite possible to get here with the strange things that can
1555    * happen when using DNS in the way the irc server does. -avalon
1556    */
1557   if (!c_conf)
1558     c_conf = find_conf_byip(lp, (const char*) &(cli_ip(cptr)), CONF_SERVER);
1559   /*
1560    * detach all conf lines that got attached by attach_confs()
1561    */
1562   det_confs_butmask(cptr, 0);
1563   /*
1564    * if no C or no N lines, then deny access
1565    */
1566   if (!c_conf) {
1567     Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1568           cli_name(cptr), cli_username(cptr), cli_sockhost(cptr)));
1569     return -1;
1570   }
1571   ircd_strncpy(cli_name(cptr), c_conf->name, HOSTLEN);
1572   /*
1573    * attach the C and N lines to the client structure for later use.
1574    */
1575   attach_conf(cptr, c_conf);
1576   attach_confs_byname(cptr, cli_name(cptr), CONF_HUB | CONF_LEAF | CONF_UWORLD);
1577
1578   if (INADDR_NONE == c_conf->ipnum.s_addr)
1579     c_conf->ipnum.s_addr = cli_ip(cptr).s_addr;
1580
1581   Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cli_name(cptr), cli_sockhost(cptr)));
1582   return 0;
1583 }
1584