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