2 * IRC - Internet Relay Chat, ircd/s_conf.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
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)
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.
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.
21 * @brief ircd configuration file driver
31 #include "ircd_features.h"
36 #include "ircd_alloc.h"
37 #include "ircd_auth.h"
38 #include "ircd_chattr.h"
40 #include "ircd_reply.h"
41 #include "ircd_snprintf.h"
42 #include "ircd_string.h"
60 #include <arpa/inet.h>
70 /** Global list of all ConfItem structures. */
71 struct ConfItem *GlobalConfList;
72 /** Count of items in #GlobalConfLis. */
74 /** Global list of service mappings. */
75 struct s_map *GlobalServiceMapList;
76 /** Global list of channel quarantines. */
77 struct qline *GlobalQuarantineList;
79 /** Current line number in scanner input. */
82 /** Configuration information for #me. */
83 struct LocalConf localConf;
84 /** Global list of connection rules. */
85 struct CRuleConf* cruleConfList;
86 /** Global list of K-lines. */
87 struct DenyConf* denyConfList;
89 /** Tell a user that they are banned, dumping the message from a file.
90 * @param sptr Client being rejected
91 * @param filename Send this file's contents to \a sptr
93 static void killcomment(struct Client* sptr, const char* filename)
100 if (NULL == (file = fbopen(filename, "r"))) {
101 send_reply(sptr, ERR_NOMOTD);
102 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
103 ":Connection from your host is refused on this server.");
107 tm = localtime((time_t*) &sb.st_mtime); /* NetBSD needs cast */
108 while (fbgets(line, sizeof(line) - 1, file)) {
109 char* end = line + strlen(line);
112 if ('\n' == *end || '\r' == *end)
117 send_reply(sptr, RPL_MOTD, line);
119 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
120 ":Connection from your host is refused on this server.");
124 /** Allocate a new struct ConfItem and link it to #GlobalConfList.
125 * @return Newly allocated structure.
127 struct ConfItem* make_conf(int type)
129 struct ConfItem* aconf;
131 aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
136 memset(aconf, 0, sizeof(struct ConfItem));
137 aconf->status = type;
138 aconf->next = GlobalConfList;
139 GlobalConfList = aconf;
143 /** Free a struct ConfItem and any resources it owns.
144 * @param aconf Item to free.
146 void free_conf(struct ConfItem *aconf)
148 Debug((DEBUG_DEBUG, "free_conf: %s %s %d",
149 aconf->host ? aconf->host : "*",
150 aconf->name ? aconf->name : "*",
151 aconf->address.port));
152 if (aconf->dns_pending)
153 delete_resolver_queries(aconf);
156 memset(aconf->passwd, 0, strlen(aconf->passwd));
157 MyFree(aconf->passwd);
165 /** Disassociate configuration from the client.
166 * @param cptr Client to operate on.
167 * @param aconf ConfItem to detach.
169 static void detach_conf(struct Client* cptr, struct ConfItem* aconf)
176 assert(0 < aconf->clients);
178 lp = &(cli_confs(cptr));
181 if ((*lp)->value.aconf == aconf) {
182 if (aconf->conn_class && (aconf->status & CONF_CLIENT_MASK) && ConfLinks(aconf) > 0)
185 assert(0 < aconf->clients);
186 if (0 == --aconf->clients && IsIllegal(aconf))
197 /** Copies a completed DNS query into its ConfItem.
198 * @param vptr Pointer to struct ConfItem for the block.
199 * @param hp DNS reply, or NULL if the lookup failed.
201 static void conf_dns_callback(void* vptr, struct DNSReply* hp)
203 struct ConfItem* aconf = (struct ConfItem*) vptr;
205 aconf->dns_pending = 0;
207 memcpy(&aconf->address.addr, &hp->addr, sizeof(aconf->address.addr));
212 /** Start a nameserver lookup of the conf host. If the conf entry is
213 * currently doing a lookup, do nothing.
214 * @param aconf ConfItem for which to start a request.
216 static void conf_dns_lookup(struct ConfItem* aconf)
218 if (!aconf->dns_pending) {
219 char buf[HOSTLEN + 1];
220 struct DNSQuery query;
222 query.callback = conf_dns_callback;
223 host_from_uh(buf, aconf->host, HOSTLEN);
226 gethost_byname(buf, &query);
227 aconf->dns_pending = 1;
232 /** Start lookups of all addresses in the conf line. The origin must
233 * be a numeric IP address. If the remote host field is not an IP
234 * address, start a DNS lookup for it.
235 * @param aconf Connection to do lookups for.
238 lookup_confhost(struct ConfItem *aconf)
240 if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
241 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
242 aconf->host, aconf->name));
245 if (aconf->origin_name
246 && !ircd_aton(&aconf->origin.addr, aconf->origin_name)) {
247 Debug((DEBUG_ERROR, "Origin name error: (%s) (%s)",
248 aconf->origin_name, aconf->name));
251 * Do name lookup now on hostnames given and store the
252 * ip numbers in conf structure.
254 if (IsIP6Char(*aconf->host)) {
255 if (!ircd_aton(&aconf->address.addr, aconf->host)) {
256 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
257 aconf->host, aconf->name));
261 conf_dns_lookup(aconf);
264 /** Find a server by name or hostname.
265 * @param name Server name to find.
266 * @return Pointer to the corresponding ConfItem, or NULL if none exists.
268 struct ConfItem* conf_find_server(const char* name)
270 struct ConfItem* conf;
273 for (conf = GlobalConfList; conf; conf = conf->next) {
274 if (CONF_SERVER == conf->status) {
276 * Check first servernames, then try hostnames.
277 * XXX - match returns 0 if there _is_ a match... guess they
278 * haven't decided what true is yet
280 if (0 == match(name, conf->name))
287 /** Evaluate connection rules.
288 * @param name Name of server to check
289 * @param mask Filter for CRule types (only consider if type & \a mask != 0).
290 * @return Name of rule that forbids the connection; NULL if no prohibitions.
292 const char* conf_eval_crule(const char* name, int mask)
294 struct CRuleConf* p = cruleConfList;
297 for ( ; p; p = p->next) {
298 if (0 != (p->type & mask) && 0 == match(p->hostmask, name)) {
299 if (crule_eval(p->node))
306 /** Remove all conf entries from the client except those which match
307 * the status field mask.
308 * @param cptr Client to operate on.
309 * @param mask ConfItem types to keep.
311 void det_confs_butmask(struct Client* cptr, int mask)
317 for (link = cli_confs(cptr); link; link = next) {
319 if ((link->value.aconf->status & mask) == 0)
320 detach_conf(cptr, link->value.aconf);
324 /** Check client limits and attach Client block.
325 * If the password field consists of one or two digits, use that
326 * as the per-IP connection limit; otherwise use 255.
327 * @param cptr Client getting \a aconf.
328 * @param aconf Configuration item to attach.
329 * @return Authorization check result.
331 static enum AuthorizationCheckResult
332 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
337 if (IsDigit(*aconf->passwd) && !aconf->passwd[1])
338 number = *aconf->passwd-'0';
339 else if (IsDigit(*aconf->passwd) && IsDigit(aconf->passwd[1]) &&
341 number = (*aconf->passwd-'0')*10+(aconf->passwd[1]-'0');
343 if (IPcheck_nr(cptr) > number)
344 return ACR_TOO_MANY_FROM_IP;
345 return attach_conf(cptr, aconf);
348 /** Find the first (best) Client block to attach.
349 * @param cptr Client for whom to check rules.
350 * @return Authorization check result.
352 enum AuthorizationCheckResult attach_iline(struct Client* cptr)
354 struct ConfItem* aconf;
355 static char uhost[HOSTLEN + USERLEN + 3];
356 static char fullname[HOSTLEN + 1];
361 hp = cli_dns_reply(cptr);
362 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
363 if (aconf->status != CONF_CLIENT)
365 if (aconf->address.port && aconf->address.port != cli_listener(cptr)->addr.port)
367 if (!aconf->host || !aconf->name)
370 ircd_strncpy(fullname, hp->h_name, HOSTLEN);
371 fullname[HOSTLEN] = '\0';
373 Debug((DEBUG_DNS, "a_il: %s->%s", cli_sockhost(cptr), fullname));
375 if (strchr(aconf->name, '@')) {
376 strcpy(uhost, cli_username(cptr));
381 strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
382 uhost[sizeof(uhost) - 1] = 0;
383 if (0 == match(aconf->name, uhost)) {
384 if (strchr(uhost, '@'))
385 SetFlag(cptr, FLAG_DOID);
386 return check_limit_and_attach(cptr, aconf);
389 if (strchr(aconf->host, '@')) {
390 ircd_strncpy(uhost, cli_username(cptr), sizeof(uhost) - 2);
391 uhost[sizeof(uhost) - 2] = 0;
396 strncat(uhost, cli_sock_ip(cptr), sizeof(uhost) - 1 - strlen(uhost));
397 uhost[sizeof(uhost) - 1] = 0;
398 if (match(aconf->host, uhost))
400 if (strchr(uhost, '@'))
401 SetFlag(cptr, FLAG_DOID);
403 return check_limit_and_attach(cptr, aconf);
405 return ACR_NO_AUTHORIZATION;
408 /** Check whether a particular ConfItem is already attached to a
410 * @param aconf ConfItem to search for
411 * @param cptr Client to check
412 * @return Non-zero if \a aconf is attached to \a cptr, zero if not.
414 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
418 for (lp = cli_confs(cptr); lp; lp = lp->next) {
419 if (lp->value.aconf == aconf)
425 /** Associate a specific configuration entry to a *local* client (this
426 * is the one which used in accepting the connection). Note, that this
427 * automaticly changes the attachment if there was an old one...
428 * @param cptr Client to attach \a aconf to
429 * @param aconf ConfItem to attach
430 * @return Authorization check result.
432 enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
436 if (is_attached(aconf, cptr))
437 return ACR_ALREADY_AUTHORIZED;
438 if (IsIllegal(aconf))
439 return ACR_NO_AUTHORIZATION;
440 if ((aconf->status & (CONF_OPERATOR | CONF_CLIENT)) &&
441 ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
442 return ACR_TOO_MANY_IN_CLASS; /* Use this for printing error message */
444 lp->next = cli_confs(cptr);
445 lp->value.aconf = aconf;
446 cli_confs(cptr) = lp;
448 if (aconf->status & CONF_CLIENT_MASK)
453 /** Return our LocalConf configuration structure.
454 * @return A pointer to #localConf.
456 const struct LocalConf* conf_get_local(void)
461 /** Attach ConfItems to a client if the name passed matches that for
462 * the ConfItems or is an exact match for them.
463 * @param cptr Client getting the ConfItem attachments.
464 * @param name Filter to match ConfItem::name.
465 * @param statmask Filter to limit ConfItem::status.
466 * @return First ConfItem attached to \a cptr.
468 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
471 struct ConfItem* tmp;
472 struct ConfItem* first = NULL;
476 if (HOSTLEN < strlen(name))
479 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
480 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
481 assert(0 != tmp->name);
482 if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) {
483 if (ACR_OK == attach_conf(cptr, tmp) && !first)
491 /** Attach ConfItems to a client if the host passed matches that for
492 * the ConfItems or is an exact match for them.
493 * @param cptr Client getting the ConfItem attachments.
494 * @param host Filter to match ConfItem::host.
495 * @param statmask Filter to limit ConfItem::status.
496 * @return First ConfItem attached to \a cptr.
498 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
501 struct ConfItem* tmp;
502 struct ConfItem* first = 0;
505 if (HOSTLEN < strlen(host))
508 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
509 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
510 assert(0 != tmp->host);
511 if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) {
512 if (ACR_OK == attach_conf(cptr, tmp) && !first)
520 /** Find a ConfItem that has the same name and user+host fields as
521 * specified. Requires an exact match for \a name.
522 * @param name Name to match
523 * @param user User part of match (or NULL)
524 * @param host Hostname part of match
525 * @param statmask Filter for ConfItem::status
526 * @return First found matching ConfItem.
528 struct ConfItem* find_conf_exact(const char* name, const char* user,
529 const char* host, int statmask)
531 struct ConfItem *tmp;
532 char userhost[USERLEN + HOSTLEN + 3];
535 ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
537 ircd_strncpy(userhost, host, sizeof(userhost) - 1);
539 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
540 if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
541 0 != ircd_strcmp(tmp->name, name))
544 * Accept if the *real* hostname (usually sockecthost)
545 * socket host) matches *either* host or name field
546 * of the configuration.
548 if (match(tmp->host, userhost))
550 if (tmp->status & CONF_OPERATOR) {
551 if (tmp->clients < MaxLinks(tmp->conn_class))
562 /** Find a ConfItem from a list that has a name that matches \a name.
563 * @param lp List to search in.
564 * @param name Filter for ConfItem::name field; matches either exactly
566 * @param statmask Filter for ConfItem::status.
567 * @return First matching ConfItem from \a lp.
569 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
572 struct ConfItem* tmp;
575 if (HOSTLEN < strlen(name))
578 for (; lp; lp = lp->next) {
579 tmp = lp->value.aconf;
580 if (0 != (tmp->status & statmask)) {
581 assert(0 != tmp->name);
582 if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
589 /** Find a ConfItem from a list that has a host that matches \a host.
590 * @param lp List to search in.
591 * @param host Filter for ConfItem::host field; matches as a glob.
592 * @param statmask Filter for ConfItem::status.
593 * @return First matching ConfItem from \a lp.
595 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
598 struct ConfItem* tmp = NULL;
601 if (HOSTLEN < strlen(host))
604 for (; lp; lp = lp->next) {
605 tmp = lp->value.aconf;
606 if (0 != (tmp->status & statmask)) {
607 assert(0 != tmp->host);
608 if (0 == match(tmp->host, host))
615 /** Find a ConfItem from a list that has an address equal to \a ip.
616 * @param lp List to search in.
617 * @param ip Filter for ConfItem::address field; matches exactly.
618 * @param statmask Filter for ConfItem::status.
619 * @return First matching ConfItem from \a lp.
621 struct ConfItem* find_conf_byip(struct SLink* lp, const struct irc_in_addr* ip,
624 struct ConfItem* tmp;
626 for (; lp; lp = lp->next) {
627 tmp = lp->value.aconf;
628 if (0 != (tmp->status & statmask)
629 && !irc_in_addr_cmp(&tmp->address.addr, ip))
635 /** Free all CRules from #cruleConfList. */
636 void conf_erase_crule_list(void)
638 struct CRuleConf* next;
639 struct CRuleConf* p = cruleConfList;
641 for ( ; p; p = next) {
643 crule_free(&p->node);
651 /** Return #cruleConfList.
652 * @return #cruleConfList
654 const struct CRuleConf* conf_get_crule_list(void)
656 return cruleConfList;
659 /** Free all deny rules from #denyConfList. */
660 void conf_erase_deny_list(void)
662 struct DenyConf* next;
663 struct DenyConf* p = denyConfList;
664 for ( ; p; p = next) {
674 /** Return #denyConfList.
675 * @return #denyConfList
677 const struct DenyConf* conf_get_deny_list(void)
682 /** Find any existing quarantine for the named channel.
683 * @param chname Channel name to search for.
684 * @return Reason for channel's quarantine, or NULL if none exists.
687 find_quarantine(const char *chname)
691 for (qline = GlobalQuarantineList; qline; qline = qline->next)
692 if (!ircd_strcmp(qline->chname, chname))
693 return qline->reason;
697 /** Free all qline structs from #GlobalQuarantineList. */
698 void clear_quarantines(void)
701 while ((qline = GlobalQuarantineList))
703 GlobalQuarantineList = qline->next;
704 MyFree(qline->reason);
705 MyFree(qline->chname);
710 /** When non-zero, indicates that a configuration error has been seen in this pass. */
711 static int conf_error;
712 /** When non-zero, indicates that the configuration file was loaded at least once. */
713 static int conf_already_read;
715 extern void yyparse(void);
716 extern void init_lexer(void);
718 /** Read configuration file.
719 * @return Zero on failure, non-zero on success. */
720 int read_configuration_file(void)
723 feature_unmark(); /* unmark all features for resetting later */
724 /* Now just open an fd. The buffering isn't really needed... */
729 feature_mark(); /* reset unmarked features */
730 conf_already_read = 1;
734 /** Report an error message about the configuration file.
735 * @param msg The error to report.
738 yyerror(const char *msg)
740 sendto_opmask_butone(0, SNO_ALL, "Config file parse error line %d: %s",
742 log_write(LS_CONFIG, L_ERROR, 0, "Config file parse error line %d: %s",
744 if (!conf_already_read)
745 fprintf(stderr, "Config file parse error line %d: %s\n", lineno, msg);
749 /** Reload the configuration file.
750 * @param cptr Client that requested rehash (if a signal, &me).
751 * @param sig Type of rehash (0 = oper-requested, 1 = signal, 2 =
752 * oper-requested but do not restart resolver)
753 * @return CPTR_KILLED if any client was K/G-lined because of the
754 * rehash; otherwise 0.
756 int rehash(struct Client *cptr, int sig)
758 struct ConfItem** tmp = &GlobalConfList;
759 struct ConfItem* tmp2;
760 struct Client* acptr;
766 sendto_opmask_butone(0, SNO_OLDSNO,
767 "Got signal SIGHUP, reloading ircd conf. file");
769 while ((tmp2 = *tmp)) {
772 * Configuration entry is still in use by some
773 * local clients, cannot delete it--mark it so
774 * that it will be deleted when the last client
777 if (CONF_CLIENT == (tmp2->status & CONF_CLIENT))
783 tmp2->status |= CONF_ILLEGAL;
790 conf_erase_crule_list();
791 conf_erase_deny_list();
795 * delete the juped nicks list
805 mark_listeners_closing();
806 iauth_mark_closing();
808 read_configuration_file();
810 log_reopen(); /* reopen log files */
812 iauth_close_unused();
814 class_delete_marked(); /* unless it fails */
817 * Flush out deleted I and P lines although still in use.
819 for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
820 if (CONF_ILLEGAL == (tmp2->status & CONF_ILLEGAL)) {
830 for (i = 0; i <= HighestFd; i++) {
831 if ((acptr = LocalClientArray[i])) {
832 assert(!IsMe(acptr));
833 if (IsServer(acptr)) {
834 det_confs_butmask(acptr, ~(CONF_UWORLD | CONF_ILLEGAL));
835 attach_confs_byname(acptr, cli_name(acptr), CONF_UWORLD);
837 /* Because admin's are getting so uppity about people managing to
838 * get past K/G's etc, we'll "fix" the bug by actually explaining
841 if ((found_g = find_kill(acptr))) {
842 sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
843 found_g == -2 ? "G-line active for %s%s" :
844 "K-line active for %s%s",
845 IsUnknown(acptr) ? "Unregistered Client ":"",
846 get_client_name(acptr, SHOW_IP));
847 if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
848 "K-lined") == CPTR_KILLED)
857 /** Read configuration file for the very first time.
858 * @return Non-zero on success, zero on failure.
863 if (read_configuration_file()) {
865 * make sure we're sane to start if the config
866 * file read didn't get everything we need.
867 * XXX - should any of these abort the server?
868 * TODO: add warning messages
870 if (0 == localConf.name || 0 == localConf.numeric)
875 if (0 == localConf.location1)
876 DupString(localConf.location1, "");
877 if (0 == localConf.location2)
878 DupString(localConf.location2, "");
879 if (0 == localConf.contact)
880 DupString(localConf.contact, "");
887 /** Searches for a K/G-line for a client. If one is found, notify the
888 * user and disconnect them.
889 * @param cptr Client to search for.
890 * @return 0 if client is accepted; -1 if client was locally denied
891 * (K-line); -2 if client was globally denied (G-line).
893 int find_kill(struct Client *cptr)
897 const char* realname;
898 struct DenyConf* deny;
899 struct Gline* agline = NULL;
906 host = cli_sockhost(cptr);
907 name = cli_user(cptr)->username;
908 realname = cli_info(cptr);
910 assert(strlen(host) <= HOSTLEN);
911 assert((name ? strlen(name) : 0) <= HOSTLEN);
912 assert((realname ? strlen(realname) : 0) <= REALLEN);
914 /* 2000-07-14: Rewrote this loop for massive speed increases.
917 for (deny = denyConfList; deny; deny = deny->next) {
918 if (0 != match(deny->usermask, name))
921 if (EmptyString(deny->hostmask))
924 if (deny->flags & DENY_FLAGS_REALNAME) { /* K: by real name */
925 if (0 == match(deny->hostmask + 2, realname))
927 } else if (deny->flags & DENY_FLAGS_IP) { /* k: by IP */
929 char tbuf1[SOCKIPLEN], tbuf2[SOCKIPLEN];
930 Debug((DEBUG_DEBUG, "ip: %s network: %s/%u",
931 ircd_ntoa_r(tbuf1, &cli_ip(cptr)), ircd_ntoa_r(tbuf2, &deny->address), deny->bits));
933 if (ipmask_check(&cli_ip(cptr), &deny->address, deny->bits))
936 else if (0 == match(deny->hostmask, host))
940 if (EmptyString(deny->message))
941 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
942 ":Connection from your host is refused on this server.");
944 if (deny->flags & DENY_FLAGS_FILE)
945 killcomment(cptr, deny->message);
947 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", deny->message);
950 else if ((agline = gline_lookup(cptr, 0))) {
953 * added a check against the user's IP address to find_gline() -Kev
955 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", GlineReason(agline));
966 /** Attempt to attach Client blocks to \a cptr. If attach_iline()
967 * fails for the client, emit a debugging message.
968 * @param cptr Client to check for access.
969 * @return Access check result.
971 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
973 enum AuthorizationCheckResult acr = ACR_OK;
977 if ((acr = attach_iline(cptr))) {
978 Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]",
979 cli_name(cptr), cli_sockhost(cptr)));
985 /** Check access for a server given its name (passed in cptr struct).
986 * Must check for all C/N lines which have a name which matches the
987 * name given and a host which matches. A host alias which is the
988 * same as the server name is also acceptable in the host field of a
990 * @param cptr Peer server to check.
991 * @return 0 if accepted, -1 if access denied.
993 int conf_check_server(struct Client *cptr)
995 struct ConfItem* c_conf = NULL;
998 Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]",
999 cli_name(cptr), cli_sockhost(cptr)));
1001 if (IsUnknown(cptr) && !attach_confs_byname(cptr, cli_name(cptr), CONF_SERVER)) {
1002 Debug((DEBUG_DNS, "No C/N lines for %s", cli_sockhost(cptr)));
1005 lp = cli_confs(cptr);
1007 * We initiated this connection so the client should have a C and N
1008 * line already attached after passing through the connect_server()
1011 if (IsConnecting(cptr) || IsHandshake(cptr)) {
1012 c_conf = find_conf_byname(lp, cli_name(cptr), CONF_SERVER);
1014 sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1016 det_confs_butmask(cptr, 0);
1024 if (cli_dns_reply(cptr)) {
1025 struct DNSReply* hp = cli_dns_reply(cptr);
1026 const char* name = hp->h_name;
1028 * If we are missing a C or N line from above, search for
1029 * it under all known hostnames we have for this ip#.
1031 if ((c_conf = find_conf_byhost(lp, hp->h_name, CONF_SERVER)))
1032 ircd_strncpy(cli_sockhost(cptr), name, HOSTLEN);
1034 c_conf = find_conf_byip(lp, &hp->addr, CONF_SERVER);
1038 * Check for C lines with the hostname portion the ip number
1039 * of the host the server runs on. This also checks the case where
1040 * there is a server connecting from 'localhost'.
1042 c_conf = find_conf_byhost(lp, cli_sockhost(cptr), CONF_SERVER);
1046 * Attach by IP# only if all other checks have failed.
1047 * It is quite possible to get here with the strange things that can
1048 * happen when using DNS in the way the irc server does. -avalon
1051 c_conf = find_conf_byip(lp, &cli_ip(cptr), CONF_SERVER);
1053 * detach all conf lines that got attached by attach_confs()
1055 det_confs_butmask(cptr, 0);
1057 * if no Connect block, then deny access
1060 Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1061 cli_name(cptr), cli_username(cptr), cli_sockhost(cptr)));
1065 * attach the Connect block to the client structure for later use.
1067 attach_conf(cptr, c_conf);
1068 attach_confs_byname(cptr, cli_name(cptr), CONF_UWORLD);
1070 if (!irc_in_addr_valid(&c_conf->address.addr))
1071 memcpy(&c_conf->address.addr, &cli_ip(cptr), sizeof(c_conf->address.addr));
1073 Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]",
1074 cli_name(cptr), cli_sockhost(cptr)));